Expr.h revision 2e7d352dbec06755105237afba183492d31d03cb
1b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 207b01ad205162a93dab42722caaa4dbb7ba43b3fhp.com!davidm// 3b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// The LLVM Compiler Infrastructure 4b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// 5b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// This file was developed by Chris Lattner and is distributed under 6b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// the University of Illinois Open Source License. See LICENSE.TXT for details. 7b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// 8b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm//===----------------------------------------------------------------------===// 9b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// 10b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// This file defines the Expr interface and subclasses. 11b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm// 12b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm//===----------------------------------------------------------------------===// 13b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm 14b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#ifndef LLVM_CLANG_AST_EXPR_H 15b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#define LLVM_CLANG_AST_EXPR_H 16b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm 17b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "clang/AST/Stmt.h" 18b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "clang/AST/Type.h" 19b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "clang/AST/Decl.h" 20b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "clang/Basic/IdentifierTable.h" 21b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "llvm/ADT/APSInt.h" 22b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm#include "llvm/ADT/APFloat.h" 23b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm 24b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidmnamespace clang { 25b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm class IdentifierInfo; 26b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm class Selector; 271787a2fd284a786b409af74047a12de02c644cd1Arun Sharma class Decl; 28b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm class ASTContext; 29b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm 3007b01ad205162a93dab42722caaa4dbb7ba43b3fhp.com!davidm/// Expr - This represents one expression. Note that Expr's are subclasses of 31b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm/// Stmt. This allows an expression to be transparently used any place a Stmt 32b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm/// is required. 33b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm/// 34f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferrisclass Expr : public Stmt { 35f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris QualType TR; 36f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferrisprotected: 37f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {} 38f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferrispublic: 39f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris QualType getType() const { return TR; } 40f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris void setType(QualType t) { TR = t; } 41f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 42f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// SourceLocation tokens are not useful in isolation - they are low level 43f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// value objects created/interpreted by SourceManager. We assume AST 44f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// clients will have a pointer to the respective SourceManager. 45f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris virtual SourceRange getSourceRange() const = 0; 46f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 47f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// getExprLoc - Return the preferred location for the arrow when diagnosing 48b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// a problem with a generic expression. 49b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm virtual SourceLocation getExprLoc() const { return getLocStart(); } 50f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 51b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// hasLocalSideEffect - Return true if this immediate expression has side 52b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// effects, not counting any sub-expressions. 53b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm bool hasLocalSideEffect() const; 54b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm 55b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 56b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// incomplete type other than void. Nonarray expressions that can be lvalues: 57b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// - name, where name must be a variable 58f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - e[i] 59f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - (e), where e must be an lvalue 60f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - e.name, where e must be an lvalue 61f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - e->name 62b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// - *e, the type of e cannot be a function type 63f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - string-constant 64f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// - reference type [C++ [expr]] 65b39b4346605025a959e7dba21c12e499856f6c81mostang.com!davidm /// 66f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris enum isLvalueResult { 67f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris LV_Valid, 68f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris LV_NotObjectType, 69f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris LV_IncompleteVoidType, 70f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris LV_DuplicateVectorComponents, 71f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris LV_InvalidExpression 72f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris }; 73f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris isLvalueResult isLvalue() const; 74f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 75f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 76f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// does not have an incomplete type, does not have a const-qualified type, 77f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// and if it is a structure or union, does not have any member (including, 78f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// recursively, any member or element of all contained aggregates or unions) 79f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// with a const-qualified type. 80f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris enum isModifiableLvalueResult { 81f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_Valid, 82f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_NotObjectType, 83f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_IncompleteVoidType, 84f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_DuplicateVectorComponents, 85f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_InvalidExpression, 86f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_IncompleteType, 87f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_ConstQualified, 88f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris MLV_ArrayType 89f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris }; 90f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris isModifiableLvalueResult isModifiableLvalue() const; 91f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 92f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris bool isNullPointerConstant(ASTContext &Ctx) const; 93f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 94f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// isIntegerConstantExpr - Return true if this expression is a valid integer 951787a2fd284a786b409af74047a12de02c644cd1Arun Sharma /// constant expression, and, if so, return its value in Result. If not a 96f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// valid i-c-e, return false and fill in Loc (if specified) with the location 97f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// of the invalid expression. 981787a2fd284a786b409af74047a12de02c644cd1Arun Sharma bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 99f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris SourceLocation *Loc = 0, 100f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris bool isEvaluated = true) const; 101f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 102f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris llvm::APSInt X(32); 103f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris return isIntegerConstantExpr(X, Ctx, Loc); 104f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris } 105f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris /// isConstantExpr - Return true if this expression is a valid constant expr. 106f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const; 107f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 108f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris static bool classof(const Stmt *T) { 109f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris return T->getStmtClass() >= firstExprConstant && 110f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris T->getStmtClass() <= lastExprConstant; 111f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris } 112f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris static bool classof(const Expr *) { return true; } 113f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 114f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris void Emit(llvm::Serializer& S) const { 115f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris llvm::SerializeTrait<Stmt>::Emit(S,*this); 116f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris } 117f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 1187d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris static inline Expr* Materialize(llvm::Deserializer& D) { 119f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris return cast<Expr>(llvm::SerializeTrait<Stmt>::Materialize(D)); 120f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris } 121f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris}; 122f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 123f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris//===----------------------------------------------------------------------===// 124f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris// Primary Expressions. 125f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris//===----------------------------------------------------------------------===// 126f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 127f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 128f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris/// enum, etc. 129f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferrisclass DeclRefExpr : public Expr { 130f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris ValueDecl *D; 1317d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris SourceLocation Loc; 1327d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferrispublic: 133f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 134f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris Expr(DeclRefExprClass, t), D(d), Loc(l) {} 1357d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris 136f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris ValueDecl *getDecl() { return D; } 1377d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris const ValueDecl *getDecl() const { return D; } 1387d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 139f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 140f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 1417d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris static bool classof(const Stmt *T) { 142f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris return T->getStmtClass() == DeclRefExprClass; 1437d46a21e0a2cb561e4cad57b101a7137e01023dcChristopher Ferris } 144f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris static bool classof(const DeclRefExpr *) { return true; } 145 146 // Iterators 147 virtual child_iterator child_begin(); 148 virtual child_iterator child_end(); 149}; 150 151/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__. 152class PreDefinedExpr : public Expr { 153public: 154 enum IdentType { 155 Func, 156 Function, 157 PrettyFunction 158 }; 159 160private: 161 SourceLocation Loc; 162 IdentType Type; 163public: 164 PreDefinedExpr(SourceLocation l, QualType type, IdentType IT) 165 : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {} 166 167 IdentType getIdentType() const { return Type; } 168 169 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 170 171 static bool classof(const Stmt *T) { 172 return T->getStmtClass() == PreDefinedExprClass; 173 } 174 static bool classof(const PreDefinedExpr *) { return true; } 175 176 // Iterators 177 virtual child_iterator child_begin(); 178 virtual child_iterator child_end(); 179}; 180 181class IntegerLiteral : public Expr { 182 llvm::APInt Value; 183 SourceLocation Loc; 184public: 185 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 186 // or UnsignedLongLongTy 187 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 188 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 189 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 190 } 191 const llvm::APInt &getValue() const { return Value; } 192 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 193 194 static bool classof(const Stmt *T) { 195 return T->getStmtClass() == IntegerLiteralClass; 196 } 197 static bool classof(const IntegerLiteral *) { return true; } 198 199 // Iterators 200 virtual child_iterator child_begin(); 201 virtual child_iterator child_end(); 202}; 203 204class CharacterLiteral : public Expr { 205 unsigned Value; 206 SourceLocation Loc; 207public: 208 // type should be IntTy 209 CharacterLiteral(unsigned value, QualType type, SourceLocation l) 210 : Expr(CharacterLiteralClass, type), Value(value), Loc(l) { 211 } 212 SourceLocation getLoc() const { return Loc; } 213 214 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 215 216 unsigned getValue() const { return Value; } 217 218 static bool classof(const Stmt *T) { 219 return T->getStmtClass() == CharacterLiteralClass; 220 } 221 static bool classof(const CharacterLiteral *) { return true; } 222 223 // Iterators 224 virtual child_iterator child_begin(); 225 virtual child_iterator child_end(); 226}; 227 228class FloatingLiteral : public Expr { 229 llvm::APFloat Value; 230 SourceLocation Loc; 231public: 232 FloatingLiteral(const llvm::APFloat &V, QualType Type, SourceLocation L) 233 : Expr(FloatingLiteralClass, Type), Value(V), Loc(L) {} 234 235 const llvm::APFloat &getValue() const { return Value; } 236 237 /// getValueAsDouble - This returns the value as an inaccurate double. Note 238 /// that this may cause loss of precision, but is useful for debugging dumps 239 /// etc. 240 double getValueAsDouble() const { 241 // FIXME: We need something for long double here. 242 if (cast<BuiltinType>(getType())->getKind() == BuiltinType::Float) 243 return Value.convertToFloat(); 244 else 245 return Value.convertToDouble(); 246 } 247 248 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 249 250 static bool classof(const Stmt *T) { 251 return T->getStmtClass() == FloatingLiteralClass; 252 } 253 static bool classof(const FloatingLiteral *) { return true; } 254 255 // Iterators 256 virtual child_iterator child_begin(); 257 virtual child_iterator child_end(); 258}; 259 260/// ImaginaryLiteral - We support imaginary integer and floating point literals, 261/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 262/// IntegerLiteral classes. Instances of this class always have a Complex type 263/// whose element type matches the subexpression. 264/// 265class ImaginaryLiteral : public Expr { 266 Expr *Val; 267public: 268 ImaginaryLiteral(Expr *val, QualType Ty) 269 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 270 271 const Expr *getSubExpr() const { return Val; } 272 Expr *getSubExpr() { return Val; } 273 274 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 275 static bool classof(const Stmt *T) { 276 return T->getStmtClass() == ImaginaryLiteralClass; 277 } 278 static bool classof(const ImaginaryLiteral *) { return true; } 279 280 // Iterators 281 virtual child_iterator child_begin(); 282 virtual child_iterator child_end(); 283}; 284 285/// StringLiteral - This represents a string literal expression, e.g. "foo" 286/// or L"bar" (wide strings). The actual string is returned by getStrData() 287/// is NOT null-terminated, and the length of the string is determined by 288/// calling getByteLength(). 289class StringLiteral : public Expr { 290 const char *StrData; 291 unsigned ByteLength; 292 bool IsWide; 293 // if the StringLiteral was composed using token pasting, both locations 294 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 295 // FIXME: if space becomes an issue, we should create a sub-class. 296 SourceLocation firstTokLoc, lastTokLoc; 297public: 298 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 299 QualType t, SourceLocation b, SourceLocation e); 300 virtual ~StringLiteral(); 301 302 const char *getStrData() const { return StrData; } 303 unsigned getByteLength() const { return ByteLength; } 304 bool isWide() const { return IsWide; } 305 306 virtual SourceRange getSourceRange() const { 307 return SourceRange(firstTokLoc,lastTokLoc); 308 } 309 static bool classof(const Stmt *T) { 310 return T->getStmtClass() == StringLiteralClass; 311 } 312 static bool classof(const StringLiteral *) { return true; } 313 314 // Iterators 315 virtual child_iterator child_begin(); 316 virtual child_iterator child_end(); 317}; 318 319/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 320/// AST node is only formed if full location information is requested. 321class ParenExpr : public Expr { 322 SourceLocation L, R; 323 Expr *Val; 324public: 325 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 326 : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {} 327 328 const Expr *getSubExpr() const { return Val; } 329 Expr *getSubExpr() { return Val; } 330 SourceRange getSourceRange() const { return SourceRange(L, R); } 331 332 static bool classof(const Stmt *T) { 333 return T->getStmtClass() == ParenExprClass; 334 } 335 static bool classof(const ParenExpr *) { return true; } 336 337 // Iterators 338 virtual child_iterator child_begin(); 339 virtual child_iterator child_end(); 340}; 341 342 343/// UnaryOperator - This represents the unary-expression's (except sizeof of 344/// types), the postinc/postdec operators from postfix-expression, and various 345/// extensions. 346/// 347/// Notes on various nodes: 348/// 349/// Real/Imag - These return the real/imag part of a complex operand. If 350/// applied to a non-complex value, the former returns its operand and the 351/// later returns zero in the type of the operand. 352/// 353/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 354/// subexpression is a compound literal with the various MemberExpr and 355/// ArraySubscriptExpr's applied to it. 356/// 357class UnaryOperator : public Expr { 358public: 359 // Note that additions to this should also update the StmtVisitor class. 360 enum Opcode { 361 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 362 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 363 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 364 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 365 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 366 SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. 367 Real, Imag, // "__real expr"/"__imag expr" Extension. 368 Extension, // __extension__ marker. 369 OffsetOf // __builtin_offsetof 370 }; 371private: 372 Expr *Val; 373 Opcode Opc; 374 SourceLocation Loc; 375public: 376 377 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 378 : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {} 379 380 Opcode getOpcode() const { return Opc; } 381 Expr *getSubExpr() const { return Val; } 382 383 /// getOperatorLoc - Return the location of the operator. 384 SourceLocation getOperatorLoc() const { return Loc; } 385 386 /// isPostfix - Return true if this is a postfix operation, like x++. 387 static bool isPostfix(Opcode Op); 388 389 bool isPostfix() const { return isPostfix(Opc); } 390 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 391 bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; } 392 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 393 394 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 395 /// corresponds to, e.g. "sizeof" or "[pre]++" 396 static const char *getOpcodeStr(Opcode Op); 397 398 virtual SourceRange getSourceRange() const { 399 if (isPostfix()) 400 return SourceRange(Val->getLocStart(), Loc); 401 else 402 return SourceRange(Loc, Val->getLocEnd()); 403 } 404 virtual SourceLocation getExprLoc() const { return Loc; } 405 406 static bool classof(const Stmt *T) { 407 return T->getStmtClass() == UnaryOperatorClass; 408 } 409 static bool classof(const UnaryOperator *) { return true; } 410 411 // Iterators 412 virtual child_iterator child_begin(); 413 virtual child_iterator child_end(); 414}; 415 416/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 417/// *types*. sizeof(expr) is handled by UnaryOperator. 418class SizeOfAlignOfTypeExpr : public Expr { 419 bool isSizeof; // true if sizeof, false if alignof. 420 QualType Ty; 421 SourceLocation OpLoc, RParenLoc; 422public: 423 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 424 SourceLocation op, SourceLocation rp) : 425 Expr(SizeOfAlignOfTypeExprClass, resultType), 426 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 427 428 bool isSizeOf() const { return isSizeof; } 429 QualType getArgumentType() const { return Ty; } 430 431 SourceLocation getOperatorLoc() const { return OpLoc; } 432 SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); } 433 434 static bool classof(const Stmt *T) { 435 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 436 } 437 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 438 439 // Iterators 440 virtual child_iterator child_begin(); 441 virtual child_iterator child_end(); 442}; 443 444//===----------------------------------------------------------------------===// 445// Postfix Operators. 446//===----------------------------------------------------------------------===// 447 448/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 449class ArraySubscriptExpr : public Expr { 450 enum { LHS, RHS, END_EXPR=2 }; 451 Expr* SubExprs[END_EXPR]; 452 SourceLocation RBracketLoc; 453public: 454 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 455 SourceLocation rbracketloc) 456 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 457 SubExprs[LHS] = lhs; 458 SubExprs[RHS] = rhs; 459 } 460 461 /// An array access can be written A[4] or 4[A] (both are equivalent). 462 /// - getBase() and getIdx() always present the normalized view: A[4]. 463 /// In this case getBase() returns "A" and getIdx() returns "4". 464 /// - getLHS() and getRHS() present the syntactic view. e.g. for 465 /// 4[A] getLHS() returns "4". 466 467 Expr *getLHS() { return SubExprs[LHS]; } 468 const Expr *getLHS() const { return SubExprs[LHS]; } 469 470 Expr *getRHS() { return SubExprs[RHS]; } 471 const Expr *getRHS() const { return SubExprs[RHS]; } 472 473 Expr *getBase() { 474 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 475 } 476 477 const Expr *getBase() const { 478 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 479 } 480 481 Expr *getIdx() { 482 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 483 } 484 485 const Expr *getIdx() const { 486 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 487 } 488 489 490 SourceRange getSourceRange() const { 491 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 492 } 493 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 494 495 static bool classof(const Stmt *T) { 496 return T->getStmtClass() == ArraySubscriptExprClass; 497 } 498 static bool classof(const ArraySubscriptExpr *) { return true; } 499 500 // Iterators 501 virtual child_iterator child_begin(); 502 virtual child_iterator child_end(); 503}; 504 505 506/// CallExpr - [C99 6.5.2.2] Function Calls. 507/// 508class CallExpr : public Expr { 509 enum { FN=0, ARGS_START=1 }; 510 Expr **SubExprs; 511 unsigned NumArgs; 512 SourceLocation RParenLoc; 513public: 514 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 515 SourceLocation rparenloc); 516 ~CallExpr() { 517 delete [] SubExprs; 518 } 519 520 const Expr *getCallee() const { return SubExprs[FN]; } 521 Expr *getCallee() { return SubExprs[FN]; } 522 523 /// getNumArgs - Return the number of actual arguments to this call. 524 /// 525 unsigned getNumArgs() const { return NumArgs; } 526 527 /// getArg - Return the specified argument. 528 Expr *getArg(unsigned Arg) { 529 assert(Arg < NumArgs && "Arg access out of range!"); 530 return SubExprs[Arg+ARGS_START]; 531 } 532 const Expr *getArg(unsigned Arg) const { 533 assert(Arg < NumArgs && "Arg access out of range!"); 534 return SubExprs[Arg+ARGS_START]; 535 } 536 /// setArg - Set the specified argument. 537 void setArg(unsigned Arg, Expr *ArgExpr) { 538 assert(Arg < NumArgs && "Arg access out of range!"); 539 SubExprs[Arg+ARGS_START] = ArgExpr; 540 } 541 /// getNumCommas - Return the number of commas that must have been present in 542 /// this function call. 543 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 544 545 bool isBuiltinClassifyType(llvm::APSInt &Result) const; 546 547 SourceRange getSourceRange() const { 548 return SourceRange(getCallee()->getLocStart(), RParenLoc); 549 } 550 551 static bool classof(const Stmt *T) { 552 return T->getStmtClass() == CallExprClass; 553 } 554 static bool classof(const CallExpr *) { return true; } 555 556 // Iterators 557 virtual child_iterator child_begin(); 558 virtual child_iterator child_end(); 559}; 560 561/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 562/// 563class MemberExpr : public Expr { 564 Expr *Base; 565 FieldDecl *MemberDecl; 566 SourceLocation MemberLoc; 567 bool IsArrow; // True if this is "X->F", false if this is "X.F". 568public: 569 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 570 : Expr(MemberExprClass, memberdecl->getType()), 571 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 572 573 Expr *getBase() const { return Base; } 574 FieldDecl *getMemberDecl() const { return MemberDecl; } 575 bool isArrow() const { return IsArrow; } 576 577 virtual SourceRange getSourceRange() const { 578 return SourceRange(getBase()->getLocStart(), MemberLoc); 579 } 580 virtual SourceLocation getExprLoc() const { return MemberLoc; } 581 582 static bool classof(const Stmt *T) { 583 return T->getStmtClass() == MemberExprClass; 584 } 585 static bool classof(const MemberExpr *) { return true; } 586 587 // Iterators 588 virtual child_iterator child_begin(); 589 virtual child_iterator child_end(); 590}; 591 592/// OCUVectorElementExpr - This represents access to specific elements of a 593/// vector, and may occur on the left hand side or right hand side. For example 594/// the following is legal: "V.xy = V.zw" if V is a 4 element ocu vector. 595/// 596class OCUVectorElementExpr : public Expr { 597 Expr *Base; 598 IdentifierInfo &Accessor; 599 SourceLocation AccessorLoc; 600public: 601 enum ElementType { 602 Point, // xywz 603 Color, // rgba 604 Texture // stpq 605 }; 606 OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 607 SourceLocation loc) 608 : Expr(OCUVectorElementExprClass, ty), 609 Base(base), Accessor(accessor), AccessorLoc(loc) {} 610 611 const Expr *getBase() const { return Base; } 612 Expr *getBase() { return Base; } 613 614 IdentifierInfo &getAccessor() const { return Accessor; } 615 616 /// getNumElements - Get the number of components being selected. 617 unsigned getNumElements() const; 618 619 /// getElementType - Determine whether the components of this access are 620 /// "point" "color" or "texture" elements. 621 ElementType getElementType() const; 622 623 /// containsDuplicateElements - Return true if any element access is 624 /// repeated. 625 bool containsDuplicateElements() const; 626 627 /// getEncodedElementAccess - Encode the elements accessed into a bit vector. 628 /// The encoding currently uses 2-bit bitfields, but clients should use the 629 /// accessors below to access them. 630 /// 631 unsigned getEncodedElementAccess() const; 632 633 /// getAccessedFieldNo - Given an encoded value and a result number, return 634 /// the input field number being accessed. 635 static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) { 636 return (EncodedVal >> (Idx*2)) & 3; 637 } 638 639 virtual SourceRange getSourceRange() const { 640 return SourceRange(getBase()->getLocStart(), AccessorLoc); 641 } 642 static bool classof(const Stmt *T) { 643 return T->getStmtClass() == OCUVectorElementExprClass; 644 } 645 static bool classof(const OCUVectorElementExpr *) { return true; } 646 647 // Iterators 648 virtual child_iterator child_begin(); 649 virtual child_iterator child_end(); 650}; 651 652/// CompoundLiteralExpr - [C99 6.5.2.5] 653/// 654class CompoundLiteralExpr : public Expr { 655 Expr *Init; 656public: 657 CompoundLiteralExpr(QualType ty, Expr *init) : 658 Expr(CompoundLiteralExprClass, ty), Init(init) {} 659 660 const Expr *getInitializer() const { return Init; } 661 Expr *getInitializer() { return Init; } 662 663 virtual SourceRange getSourceRange() const { 664 if (Init) 665 return Init->getSourceRange(); 666 return SourceRange(); 667 } 668 669 static bool classof(const Stmt *T) { 670 return T->getStmtClass() == CompoundLiteralExprClass; 671 } 672 static bool classof(const CompoundLiteralExpr *) { return true; } 673 674 // Iterators 675 virtual child_iterator child_begin(); 676 virtual child_iterator child_end(); 677}; 678 679/// ImplicitCastExpr - Allows us to explicitly represent implicit type 680/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 681/// float->double, short->int, etc. 682/// 683class ImplicitCastExpr : public Expr { 684 Expr *Op; 685public: 686 ImplicitCastExpr(QualType ty, Expr *op) : 687 Expr(ImplicitCastExprClass, ty), Op(op) {} 688 689 Expr *getSubExpr() { return Op; } 690 const Expr *getSubExpr() const { return Op; } 691 692 virtual SourceRange getSourceRange() const { return Op->getSourceRange(); } 693 694 static bool classof(const Stmt *T) { 695 return T->getStmtClass() == ImplicitCastExprClass; 696 } 697 static bool classof(const ImplicitCastExpr *) { return true; } 698 699 // Iterators 700 virtual child_iterator child_begin(); 701 virtual child_iterator child_end(); 702}; 703 704/// CastExpr - [C99 6.5.4] Cast Operators. 705/// 706class CastExpr : public Expr { 707 Expr *Op; 708 SourceLocation Loc; // the location of the left paren 709public: 710 CastExpr(QualType ty, Expr *op, SourceLocation l) : 711 Expr(CastExprClass, ty), Op(op), Loc(l) {} 712 713 SourceLocation getLParenLoc() const { return Loc; } 714 715 Expr *getSubExpr() const { return Op; } 716 717 virtual SourceRange getSourceRange() const { 718 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 719 } 720 static bool classof(const Stmt *T) { 721 return T->getStmtClass() == CastExprClass; 722 } 723 static bool classof(const CastExpr *) { return true; } 724 725 // Iterators 726 virtual child_iterator child_begin(); 727 virtual child_iterator child_end(); 728}; 729 730class BinaryOperator : public Expr { 731public: 732 enum Opcode { 733 // Operators listed in order of precedence. 734 // Note that additions to this should also update the StmtVisitor class. 735 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 736 Add, Sub, // [C99 6.5.6] Additive operators. 737 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 738 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 739 EQ, NE, // [C99 6.5.9] Equality operators. 740 And, // [C99 6.5.10] Bitwise AND operator. 741 Xor, // [C99 6.5.11] Bitwise XOR operator. 742 Or, // [C99 6.5.12] Bitwise OR operator. 743 LAnd, // [C99 6.5.13] Logical AND operator. 744 LOr, // [C99 6.5.14] Logical OR operator. 745 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 746 DivAssign, RemAssign, 747 AddAssign, SubAssign, 748 ShlAssign, ShrAssign, 749 AndAssign, XorAssign, 750 OrAssign, 751 Comma // [C99 6.5.17] Comma operator. 752 }; 753private: 754 enum { LHS, RHS, END_EXPR }; 755 Expr* SubExprs[END_EXPR]; 756 Opcode Opc; 757 SourceLocation OpLoc; 758public: 759 760 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 761 SourceLocation opLoc) 762 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 763 SubExprs[LHS] = lhs; 764 SubExprs[RHS] = rhs; 765 assert(!isCompoundAssignmentOp() && 766 "Use ArithAssignBinaryOperator for compound assignments"); 767 } 768 769 SourceLocation getOperatorLoc() const { return OpLoc; } 770 Opcode getOpcode() const { return Opc; } 771 Expr *getLHS() const { return SubExprs[LHS]; } 772 Expr *getRHS() const { return SubExprs[RHS]; } 773 virtual SourceRange getSourceRange() const { 774 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 775 } 776 777 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 778 /// corresponds to, e.g. "<<=". 779 static const char *getOpcodeStr(Opcode Op); 780 781 /// predicates to categorize the respective opcodes. 782 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 783 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 784 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 785 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 786 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 787 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 788 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 789 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 790 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 791 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 792 793 static bool classof(const Stmt *S) { 794 return S->getStmtClass() == BinaryOperatorClass || 795 S->getStmtClass() == CompoundAssignOperatorClass; 796 } 797 static bool classof(const BinaryOperator *) { return true; } 798 799 // Iterators 800 virtual child_iterator child_begin(); 801 virtual child_iterator child_end(); 802 803protected: 804 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 805 SourceLocation oploc, bool dead) 806 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 807 SubExprs[LHS] = lhs; 808 SubExprs[RHS] = rhs; 809 } 810}; 811 812/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 813/// track of the type the operation is performed in. Due to the semantics of 814/// these operators, the operands are promoted, the aritmetic performed, an 815/// implicit conversion back to the result type done, then the assignment takes 816/// place. This captures the intermediate type which the computation is done 817/// in. 818class CompoundAssignOperator : public BinaryOperator { 819 QualType ComputationType; 820public: 821 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 822 QualType ResType, QualType CompType, 823 SourceLocation OpLoc) 824 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 825 ComputationType(CompType) { 826 assert(isCompoundAssignmentOp() && 827 "Only should be used for compound assignments"); 828 } 829 830 QualType getComputationType() const { return ComputationType; } 831 832 static bool classof(const CompoundAssignOperator *) { return true; } 833 static bool classof(const Stmt *S) { 834 return S->getStmtClass() == CompoundAssignOperatorClass; 835 } 836}; 837 838/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 839/// GNU "missing LHS" extension is in use. 840/// 841class ConditionalOperator : public Expr { 842 enum { COND, LHS, RHS, END_EXPR }; 843 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 844public: 845 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 846 : Expr(ConditionalOperatorClass, t) { 847 SubExprs[COND] = cond; 848 SubExprs[LHS] = lhs; 849 SubExprs[RHS] = rhs; 850 } 851 852 Expr *getCond() const { return SubExprs[COND]; } 853 Expr *getLHS() const { return SubExprs[LHS]; } 854 Expr *getRHS() const { return SubExprs[RHS]; } 855 856 virtual SourceRange getSourceRange() const { 857 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 858 } 859 static bool classof(const Stmt *T) { 860 return T->getStmtClass() == ConditionalOperatorClass; 861 } 862 static bool classof(const ConditionalOperator *) { return true; } 863 864 // Iterators 865 virtual child_iterator child_begin(); 866 virtual child_iterator child_end(); 867}; 868 869/// AddrLabelExpr - The GNU address of label extension, representing &&label. 870class AddrLabelExpr : public Expr { 871 SourceLocation AmpAmpLoc, LabelLoc; 872 LabelStmt *Label; 873public: 874 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 875 QualType t) 876 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 877 878 virtual SourceRange getSourceRange() const { 879 return SourceRange(AmpAmpLoc, LabelLoc); 880 } 881 882 LabelStmt *getLabel() const { return Label; } 883 884 static bool classof(const Stmt *T) { 885 return T->getStmtClass() == AddrLabelExprClass; 886 } 887 static bool classof(const AddrLabelExpr *) { return true; } 888 889 // Iterators 890 virtual child_iterator child_begin(); 891 virtual child_iterator child_end(); 892}; 893 894/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 895/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 896/// takes the value of the last subexpression. 897class StmtExpr : public Expr { 898 CompoundStmt *SubStmt; 899 SourceLocation LParenLoc, RParenLoc; 900public: 901 StmtExpr(CompoundStmt *substmt, QualType T, 902 SourceLocation lp, SourceLocation rp) : 903 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 904 905 CompoundStmt *getSubStmt() { return SubStmt; } 906 const CompoundStmt *getSubStmt() const { return SubStmt; } 907 908 virtual SourceRange getSourceRange() const { 909 return SourceRange(LParenLoc, RParenLoc); 910 } 911 912 static bool classof(const Stmt *T) { 913 return T->getStmtClass() == StmtExprClass; 914 } 915 static bool classof(const StmtExpr *) { return true; } 916 917 // Iterators 918 virtual child_iterator child_begin(); 919 virtual child_iterator child_end(); 920}; 921 922/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 923/// This AST node represents a function that returns 1 if two *types* (not 924/// expressions) are compatible. The result of this built-in function can be 925/// used in integer constant expressions. 926class TypesCompatibleExpr : public Expr { 927 QualType Type1; 928 QualType Type2; 929 SourceLocation BuiltinLoc, RParenLoc; 930public: 931 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 932 QualType t1, QualType t2, SourceLocation RP) : 933 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 934 BuiltinLoc(BLoc), RParenLoc(RP) {} 935 936 QualType getArgType1() const { return Type1; } 937 QualType getArgType2() const { return Type2; } 938 939 virtual SourceRange getSourceRange() const { 940 return SourceRange(BuiltinLoc, RParenLoc); 941 } 942 static bool classof(const Stmt *T) { 943 return T->getStmtClass() == TypesCompatibleExprClass; 944 } 945 static bool classof(const TypesCompatibleExpr *) { return true; } 946 947 // Iterators 948 virtual child_iterator child_begin(); 949 virtual child_iterator child_end(); 950}; 951 952/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 953/// This AST node is similar to the conditional operator (?:) in C, with 954/// the following exceptions: 955/// - the test expression much be a constant expression. 956/// - the expression returned has it's type unaltered by promotion rules. 957/// - does not evaluate the expression that was not chosen. 958class ChooseExpr : public Expr { 959 enum { COND, LHS, RHS, END_EXPR }; 960 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 961 SourceLocation BuiltinLoc, RParenLoc; 962public: 963 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 964 SourceLocation RP) 965 : Expr(ChooseExprClass, t), 966 BuiltinLoc(BLoc), RParenLoc(RP) { 967 SubExprs[COND] = cond; 968 SubExprs[LHS] = lhs; 969 SubExprs[RHS] = rhs; 970 } 971 972 /// isConditionTrue - Return true if the condition is true. This is always 973 /// statically knowable for a well-formed choosexpr. 974 bool isConditionTrue(ASTContext &C) const; 975 976 Expr *getCond() const { return SubExprs[COND]; } 977 Expr *getLHS() const { return SubExprs[LHS]; } 978 Expr *getRHS() const { return SubExprs[RHS]; } 979 980 virtual SourceRange getSourceRange() const { 981 return SourceRange(BuiltinLoc, RParenLoc); 982 } 983 static bool classof(const Stmt *T) { 984 return T->getStmtClass() == ChooseExprClass; 985 } 986 static bool classof(const ChooseExpr *) { return true; } 987 988 // Iterators 989 virtual child_iterator child_begin(); 990 virtual child_iterator child_end(); 991}; 992 993/// VAArgExpr, used for the builtin function __builtin_va_start. 994class VAArgExpr : public Expr { 995 Expr *Val; 996 SourceLocation BuiltinLoc, RParenLoc; 997public: 998 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 999 : Expr(VAArgExprClass, t), 1000 Val(e), 1001 BuiltinLoc(BLoc), 1002 RParenLoc(RPLoc) { } 1003 1004 const Expr *getSubExpr() const { return Val; } 1005 Expr *getSubExpr() { return Val; } 1006 virtual SourceRange getSourceRange() const { 1007 return SourceRange(BuiltinLoc, RParenLoc); 1008 } 1009 static bool classof(const Stmt *T) { 1010 return T->getStmtClass() == VAArgExprClass; 1011 } 1012 static bool classof(const VAArgExpr *) { return true; } 1013 1014 // Iterators 1015 virtual child_iterator child_begin(); 1016 virtual child_iterator child_end(); 1017}; 1018 1019/// InitListExpr, used for struct and array initializers. 1020class InitListExpr : public Expr { 1021 Expr **InitExprs; 1022 unsigned NumInits; 1023 SourceLocation LBraceLoc, RBraceLoc; 1024public: 1025 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1026 SourceLocation rbraceloc); 1027 ~InitListExpr() { 1028 delete [] InitExprs; 1029 } 1030 1031 unsigned getNumInits() const { return NumInits; } 1032 1033 const Expr* getInit(unsigned Init) const { 1034 assert(Init < NumInits && "Initializer access out of range!"); 1035 return InitExprs[Init]; 1036 } 1037 1038 Expr* getInit(unsigned Init) { 1039 assert(Init < NumInits && "Initializer access out of range!"); 1040 return InitExprs[Init]; 1041 } 1042 1043 void setInit(unsigned Init, Expr *expr) { 1044 assert(Init < NumInits && "Initializer access out of range!"); 1045 InitExprs[Init] = expr; 1046 } 1047 1048 virtual SourceRange getSourceRange() const { 1049 return SourceRange(LBraceLoc, RBraceLoc); 1050 } 1051 static bool classof(const Stmt *T) { 1052 return T->getStmtClass() == InitListExprClass; 1053 } 1054 static bool classof(const InitListExpr *) { return true; } 1055 1056 // Iterators 1057 virtual child_iterator child_begin(); 1058 virtual child_iterator child_end(); 1059}; 1060 1061/// ObjCStringLiteral, used for Objective-C string literals 1062/// i.e. @"foo". 1063class ObjCStringLiteral : public Expr { 1064 StringLiteral *String; 1065public: 1066 ObjCStringLiteral(StringLiteral *SL, QualType T) 1067 : Expr(ObjCStringLiteralClass, T), String(SL) {} 1068 1069 StringLiteral* getString() { return String; } 1070 1071 const StringLiteral* getString() const { return String; } 1072 1073 virtual SourceRange getSourceRange() const { 1074 return String->getSourceRange(); 1075 } 1076 1077 static bool classof(const Stmt *T) { 1078 return T->getStmtClass() == ObjCStringLiteralClass; 1079 } 1080 static bool classof(const ObjCStringLiteral *) { return true; } 1081 1082 // Iterators 1083 virtual child_iterator child_begin(); 1084 virtual child_iterator child_end(); 1085}; 1086 1087/// ObjCEncodeExpr, used for @encode in Objective-C. 1088class ObjCEncodeExpr : public Expr { 1089 QualType EncType; 1090 SourceLocation AtLoc, RParenLoc; 1091public: 1092 ObjCEncodeExpr(QualType T, QualType ET, 1093 SourceLocation at, SourceLocation rp) 1094 : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {} 1095 1096 SourceLocation getAtLoc() const { return AtLoc; } 1097 SourceLocation getRParenLoc() const { return RParenLoc; } 1098 1099 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1100 1101 QualType getEncodedType() const { return EncType; } 1102 1103 static bool classof(const Stmt *T) { 1104 return T->getStmtClass() == ObjCEncodeExprClass; 1105 } 1106 static bool classof(const ObjCEncodeExpr *) { return true; } 1107 1108 // Iterators 1109 virtual child_iterator child_begin(); 1110 virtual child_iterator child_end(); 1111}; 1112 1113/// ObjCSelectorExpr used for @selector in Objective-C. 1114class ObjCSelectorExpr : public Expr { 1115 1116 Selector SelName; 1117 1118 SourceLocation AtLoc, RParenLoc; 1119public: 1120 ObjCSelectorExpr(QualType T, Selector selInfo, 1121 SourceLocation at, SourceLocation rp) 1122 : Expr(ObjCSelectorExprClass, T), SelName(selInfo), 1123 AtLoc(at), RParenLoc(rp) {} 1124 1125 const Selector &getSelector() const { return SelName; } 1126 Selector &getSelector() { return SelName; } 1127 1128 SourceLocation getAtLoc() const { return AtLoc; } 1129 SourceLocation getRParenLoc() const { return RParenLoc; } 1130 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1131 1132 /// getNumArgs - Return the number of actual arguments to this call. 1133 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1134 1135 static bool classof(const Stmt *T) { 1136 return T->getStmtClass() == ObjCSelectorExprClass; 1137 } 1138 static bool classof(const ObjCSelectorExpr *) { return true; } 1139 1140 // Iterators 1141 virtual child_iterator child_begin(); 1142 virtual child_iterator child_end(); 1143 1144}; 1145 1146/// ObjCProtocolExpr used for protocol in Objective-C. 1147class ObjCProtocolExpr : public Expr { 1148 1149 ObjcProtocolDecl *Protocol; 1150 1151 SourceLocation AtLoc, RParenLoc; 1152 public: 1153 ObjCProtocolExpr(QualType T, ObjcProtocolDecl *protocol, 1154 SourceLocation at, SourceLocation rp) 1155 : Expr(ObjCProtocolExprClass, T), Protocol(protocol), 1156 AtLoc(at), RParenLoc(rp) {} 1157 1158 ObjcProtocolDecl *getProtocol() const { return Protocol; } 1159 1160 SourceLocation getAtLoc() const { return AtLoc; } 1161 SourceLocation getRParenLoc() const { return RParenLoc; } 1162 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1163 1164 static bool classof(const Stmt *T) { 1165 return T->getStmtClass() == ObjCProtocolExprClass; 1166 } 1167 static bool classof(const ObjCProtocolExpr *) { return true; } 1168 1169 // Iterators 1170 virtual child_iterator child_begin(); 1171 virtual child_iterator child_end(); 1172 1173}; 1174 1175class ObjCMessageExpr : public Expr { 1176 enum { RECEIVER=0, ARGS_START=1 }; 1177 1178 Expr **SubExprs; 1179 1180 // A unigue name for this message. 1181 Selector SelName; 1182 1183 IdentifierInfo *ClassName; // optional - 0 for instance messages. 1184 1185 SourceLocation LBracloc, RBracloc; 1186public: 1187 // constructor for class messages. 1188 // FIXME: clsName should be typed to ObjCInterfaceType 1189 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1190 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1191 Expr **ArgExprs); 1192 // constructor for instance messages. 1193 ObjCMessageExpr(Expr *receiver, Selector selInfo, 1194 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1195 Expr **ArgExprs); 1196 ~ObjCMessageExpr() { 1197 delete [] SubExprs; 1198 } 1199 1200 const Expr *getReceiver() const { return SubExprs[RECEIVER]; } 1201 Expr *getReceiver() { return SubExprs[RECEIVER]; } 1202 1203 const Selector &getSelector() const { return SelName; } 1204 Selector &getSelector() { return SelName; } 1205 1206 const IdentifierInfo *getClassName() const { return ClassName; } 1207 IdentifierInfo *getClassName() { return ClassName; } 1208 1209 /// getNumArgs - Return the number of actual arguments to this call. 1210 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1211 1212/// getArg - Return the specified argument. 1213 Expr *getArg(unsigned Arg) { 1214 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1215 return SubExprs[Arg+ARGS_START]; 1216 } 1217 const Expr *getArg(unsigned Arg) const { 1218 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1219 return SubExprs[Arg+ARGS_START]; 1220 } 1221 /// setArg - Set the specified argument. 1222 void setArg(unsigned Arg, Expr *ArgExpr) { 1223 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1224 SubExprs[Arg+ARGS_START] = ArgExpr; 1225 } 1226 SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); } 1227 1228 static bool classof(const Stmt *T) { 1229 return T->getStmtClass() == ObjCMessageExprClass; 1230 } 1231 static bool classof(const ObjCMessageExpr *) { return true; } 1232 1233 // Iterators 1234 virtual child_iterator child_begin(); 1235 virtual child_iterator child_end(); 1236}; 1237 1238} // end namespace clang 1239 1240#endif 1241