Expr.h revision 20c77e995d5f1bad8fc59fe9e1883a563d94f535
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Expr interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_EXPR_H 15#define LLVM_CLANG_AST_EXPR_H 16 17#include "clang/AST/Stmt.h" 18#include "clang/AST/Type.h" 19#include "llvm/ADT/APSInt.h" 20#include "llvm/ADT/APFloat.h" 21#include "llvm/ADT/SmallVector.h" 22#include <vector> 23 24namespace clang { 25 class ASTContext; 26 class APValue; 27 class Decl; 28 class IdentifierInfo; 29 class ParmVarDecl; 30 class ValueDecl; 31 class BlockDecl; 32 33/// Expr - This represents one expression. Note that Expr's are subclasses of 34/// Stmt. This allows an expression to be transparently used any place a Stmt 35/// is required. 36/// 37class Expr : public Stmt { 38 QualType TR; 39protected: 40 Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {} 41public: 42 QualType getType() const { return TR; } 43 void setType(QualType t) { TR = t; } 44 45 /// SourceLocation tokens are not useful in isolation - they are low level 46 /// value objects created/interpreted by SourceManager. We assume AST 47 /// clients will have a pointer to the respective SourceManager. 48 virtual SourceRange getSourceRange() const = 0; 49 50 /// getExprLoc - Return the preferred location for the arrow when diagnosing 51 /// a problem with a generic expression. 52 virtual SourceLocation getExprLoc() const { return getLocStart(); } 53 54 /// hasLocalSideEffect - Return true if this immediate expression has side 55 /// effects, not counting any sub-expressions. 56 bool hasLocalSideEffect() const; 57 58 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 59 /// incomplete type other than void. Nonarray expressions that can be lvalues: 60 /// - name, where name must be a variable 61 /// - e[i] 62 /// - (e), where e must be an lvalue 63 /// - e.name, where e must be an lvalue 64 /// - e->name 65 /// - *e, the type of e cannot be a function type 66 /// - string-constant 67 /// - reference type [C++ [expr]] 68 /// 69 enum isLvalueResult { 70 LV_Valid, 71 LV_NotObjectType, 72 LV_IncompleteVoidType, 73 LV_DuplicateVectorComponents, 74 LV_InvalidExpression 75 }; 76 isLvalueResult isLvalue(ASTContext &Ctx) const; 77 78 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 79 /// does not have an incomplete type, does not have a const-qualified type, 80 /// and if it is a structure or union, does not have any member (including, 81 /// recursively, any member or element of all contained aggregates or unions) 82 /// with a const-qualified type. 83 enum isModifiableLvalueResult { 84 MLV_Valid, 85 MLV_NotObjectType, 86 MLV_IncompleteVoidType, 87 MLV_DuplicateVectorComponents, 88 MLV_InvalidExpression, 89 MLV_IncompleteType, 90 MLV_ConstQualified, 91 MLV_ArrayType, 92 MLV_NotBlockQualified 93 }; 94 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const; 95 96 bool isNullPointerConstant(ASTContext &Ctx) const; 97 98 /// getIntegerConstantExprValue() - Return the value of an integer 99 /// constant expression. The expression must be a valid integer 100 /// constant expression as determined by isIntegerConstantExpr. 101 llvm::APSInt getIntegerConstantExprValue(ASTContext &Ctx) const { 102 llvm::APSInt X; 103 bool success = isIntegerConstantExpr(X, Ctx); 104 success = success; 105 assert(success && "Illegal argument to getIntegerConstantExpr"); 106 return X; 107 } 108 109 /// isIntegerConstantExpr - Return true if this expression is a valid integer 110 /// constant expression, and, if so, return its value in Result. If not a 111 /// valid i-c-e, return false and fill in Loc (if specified) with the location 112 /// of the invalid expression. 113 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 114 SourceLocation *Loc = 0, 115 bool isEvaluated = true) const; 116 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 117 llvm::APSInt X; 118 return isIntegerConstantExpr(X, Ctx, Loc); 119 } 120 /// isConstantExpr - Return true if this expression is a valid constant expr. 121 bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const; 122 123 /// tryEvaluate - Return true if this is a constant which we can fold using 124 /// any crazy technique (that has nothing to do with language standards) that 125 /// we want to. If this function returns true, it returns the folded constant 126 /// in Result. 127 bool tryEvaluate(APValue& Result, ASTContext &Ctx) const; 128 129 /// isEvaluatable - Call tryEvaluate to see if this expression can be constant 130 /// folded, but discard the result. 131 bool isEvaluatable(ASTContext &Ctx) const; 132 133 /// hasGlobalStorage - Return true if this expression has static storage 134 /// duration. This means that the address of this expression is a link-time 135 /// constant. 136 bool hasGlobalStorage() const; 137 138 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 139 /// its subexpression. If that subexpression is also a ParenExpr, 140 /// then this method recursively returns its subexpression, and so forth. 141 /// Otherwise, the method returns the current Expr. 142 Expr* IgnoreParens(); 143 144 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 145 /// or CastExprs or ImplicitCastExprs, returning their operand. 146 Expr *IgnoreParenCasts(); 147 148 const Expr* IgnoreParens() const { 149 return const_cast<Expr*>(this)->IgnoreParens(); 150 } 151 const Expr *IgnoreParenCasts() const { 152 return const_cast<Expr*>(this)->IgnoreParenCasts(); 153 } 154 155 static bool classof(const Stmt *T) { 156 return T->getStmtClass() >= firstExprConstant && 157 T->getStmtClass() <= lastExprConstant; 158 } 159 static bool classof(const Expr *) { return true; } 160 161 static inline Expr* Create(llvm::Deserializer& D, ASTContext& C) { 162 return cast<Expr>(Stmt::Create(D, C)); 163 } 164}; 165 166//===----------------------------------------------------------------------===// 167// ExprIterator - Iterators for iterating over Stmt* arrays that contain 168// only Expr*. This is needed because AST nodes use Stmt* arrays to store 169// references to children (to be compatible with StmtIterator). 170//===----------------------------------------------------------------------===// 171 172class ExprIterator { 173 Stmt** I; 174public: 175 ExprIterator(Stmt** i) : I(i) {} 176 ExprIterator() : I(0) {} 177 ExprIterator& operator++() { ++I; return *this; } 178 ExprIterator operator-(size_t i) { return I-i; } 179 ExprIterator operator+(size_t i) { return I+i; } 180 Expr* operator[](size_t idx) { return cast<Expr>(I[idx]); } 181 // FIXME: Verify that this will correctly return a signed distance. 182 signed operator-(const ExprIterator& R) const { return I - R.I; } 183 Expr* operator*() const { return cast<Expr>(*I); } 184 Expr* operator->() const { return cast<Expr>(*I); } 185 bool operator==(const ExprIterator& R) const { return I == R.I; } 186 bool operator!=(const ExprIterator& R) const { return I != R.I; } 187 bool operator>(const ExprIterator& R) const { return I > R.I; } 188 bool operator>=(const ExprIterator& R) const { return I >= R.I; } 189}; 190 191class ConstExprIterator { 192 Stmt* const * I; 193public: 194 ConstExprIterator(Stmt* const* i) : I(i) {} 195 ConstExprIterator() : I(0) {} 196 ConstExprIterator& operator++() { ++I; return *this; } 197 ConstExprIterator operator+(size_t i) { return I+i; } 198 ConstExprIterator operator-(size_t i) { return I-i; } 199 Expr * operator[](size_t idx) const { return cast<Expr>(I[idx]); } 200 signed operator-(const ConstExprIterator& R) const { return I - R.I; } 201 Expr * operator*() const { return cast<Expr>(*I); } 202 Expr * operator->() const { return cast<Expr>(*I); } 203 bool operator==(const ConstExprIterator& R) const { return I == R.I; } 204 bool operator!=(const ConstExprIterator& R) const { return I != R.I; } 205 bool operator>(const ConstExprIterator& R) const { return I > R.I; } 206 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; } 207}; 208 209 210//===----------------------------------------------------------------------===// 211// Primary Expressions. 212//===----------------------------------------------------------------------===// 213 214/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 215/// enum, etc. 216class DeclRefExpr : public Expr { 217 ValueDecl *D; 218 SourceLocation Loc; 219 220protected: 221 DeclRefExpr(StmtClass SC, ValueDecl *d, QualType t, SourceLocation l) : 222 Expr(SC, t), D(d), Loc(l) {} 223 224public: 225 DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 226 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 227 228 ValueDecl *getDecl() { return D; } 229 const ValueDecl *getDecl() const { return D; } 230 SourceLocation getLocation() const { return Loc; } 231 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 232 233 234 static bool classof(const Stmt *T) { 235 return T->getStmtClass() == DeclRefExprClass || 236 T->getStmtClass() == CXXConditionDeclExprClass; 237 } 238 static bool classof(const DeclRefExpr *) { return true; } 239 240 // Iterators 241 virtual child_iterator child_begin(); 242 virtual child_iterator child_end(); 243 244 virtual void EmitImpl(llvm::Serializer& S) const; 245 static DeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 246}; 247 248/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 249class PredefinedExpr : public Expr { 250public: 251 enum IdentType { 252 Func, 253 Function, 254 PrettyFunction, 255 CXXThis, 256 ObjCSuper // super 257 }; 258 259private: 260 SourceLocation Loc; 261 IdentType Type; 262public: 263 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 264 : Expr(PredefinedExprClass, type), Loc(l), Type(IT) {} 265 266 IdentType getIdentType() const { return Type; } 267 268 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 269 270 static bool classof(const Stmt *T) { 271 return T->getStmtClass() == PredefinedExprClass; 272 } 273 static bool classof(const PredefinedExpr *) { return true; } 274 275 // Iterators 276 virtual child_iterator child_begin(); 277 virtual child_iterator child_end(); 278 279 virtual void EmitImpl(llvm::Serializer& S) const; 280 static PredefinedExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 281}; 282 283class IntegerLiteral : public Expr { 284 llvm::APInt Value; 285 SourceLocation Loc; 286public: 287 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 288 // or UnsignedLongLongTy 289 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 290 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 291 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 292 } 293 const llvm::APInt &getValue() const { return Value; } 294 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 295 296 static bool classof(const Stmt *T) { 297 return T->getStmtClass() == IntegerLiteralClass; 298 } 299 static bool classof(const IntegerLiteral *) { return true; } 300 301 // Iterators 302 virtual child_iterator child_begin(); 303 virtual child_iterator child_end(); 304 305 virtual void EmitImpl(llvm::Serializer& S) const; 306 static IntegerLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 307}; 308 309class CharacterLiteral : public Expr { 310 unsigned Value; 311 SourceLocation Loc; 312 bool IsWide; 313public: 314 // type should be IntTy 315 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 316 : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) { 317 } 318 SourceLocation getLoc() const { return Loc; } 319 bool isWide() const { return IsWide; } 320 321 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 322 323 unsigned getValue() const { return Value; } 324 325 static bool classof(const Stmt *T) { 326 return T->getStmtClass() == CharacterLiteralClass; 327 } 328 static bool classof(const CharacterLiteral *) { return true; } 329 330 // Iterators 331 virtual child_iterator child_begin(); 332 virtual child_iterator child_end(); 333 334 virtual void EmitImpl(llvm::Serializer& S) const; 335 static CharacterLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 336}; 337 338class FloatingLiteral : public Expr { 339 llvm::APFloat Value; 340 bool IsExact : 1; 341 SourceLocation Loc; 342public: 343 FloatingLiteral(const llvm::APFloat &V, bool* isexact, 344 QualType Type, SourceLocation L) 345 : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {} 346 347 const llvm::APFloat &getValue() const { return Value; } 348 349 bool isExact() const { return IsExact; } 350 351 /// getValueAsApproximateDouble - This returns the value as an inaccurate 352 /// double. Note that this may cause loss of precision, but is useful for 353 /// debugging dumps, etc. 354 double getValueAsApproximateDouble() const; 355 356 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 357 358 static bool classof(const Stmt *T) { 359 return T->getStmtClass() == FloatingLiteralClass; 360 } 361 static bool classof(const FloatingLiteral *) { return true; } 362 363 // Iterators 364 virtual child_iterator child_begin(); 365 virtual child_iterator child_end(); 366 367 virtual void EmitImpl(llvm::Serializer& S) const; 368 static FloatingLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 369}; 370 371/// ImaginaryLiteral - We support imaginary integer and floating point literals, 372/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 373/// IntegerLiteral classes. Instances of this class always have a Complex type 374/// whose element type matches the subexpression. 375/// 376class ImaginaryLiteral : public Expr { 377 Stmt *Val; 378public: 379 ImaginaryLiteral(Expr *val, QualType Ty) 380 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 381 382 const Expr *getSubExpr() const { return cast<Expr>(Val); } 383 Expr *getSubExpr() { return cast<Expr>(Val); } 384 385 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 386 static bool classof(const Stmt *T) { 387 return T->getStmtClass() == ImaginaryLiteralClass; 388 } 389 static bool classof(const ImaginaryLiteral *) { return true; } 390 391 // Iterators 392 virtual child_iterator child_begin(); 393 virtual child_iterator child_end(); 394 395 virtual void EmitImpl(llvm::Serializer& S) const; 396 static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 397}; 398 399/// StringLiteral - This represents a string literal expression, e.g. "foo" 400/// or L"bar" (wide strings). The actual string is returned by getStrData() 401/// is NOT null-terminated, and the length of the string is determined by 402/// calling getByteLength(). The C type for a string is always a 403/// ConstantArrayType. 404class StringLiteral : public Expr { 405 const char *StrData; 406 unsigned ByteLength; 407 bool IsWide; 408 // if the StringLiteral was composed using token pasting, both locations 409 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 410 // FIXME: if space becomes an issue, we should create a sub-class. 411 SourceLocation firstTokLoc, lastTokLoc; 412public: 413 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 414 QualType t, SourceLocation b, SourceLocation e); 415 virtual ~StringLiteral(); 416 417 const char *getStrData() const { return StrData; } 418 unsigned getByteLength() const { return ByteLength; } 419 bool isWide() const { return IsWide; } 420 421 virtual SourceRange getSourceRange() const { 422 return SourceRange(firstTokLoc,lastTokLoc); 423 } 424 static bool classof(const Stmt *T) { 425 return T->getStmtClass() == StringLiteralClass; 426 } 427 static bool classof(const StringLiteral *) { return true; } 428 429 // Iterators 430 virtual child_iterator child_begin(); 431 virtual child_iterator child_end(); 432 433 virtual void EmitImpl(llvm::Serializer& S) const; 434 static StringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 435}; 436 437/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 438/// AST node is only formed if full location information is requested. 439class ParenExpr : public Expr { 440 SourceLocation L, R; 441 Stmt *Val; 442public: 443 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 444 : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {} 445 446 const Expr *getSubExpr() const { return cast<Expr>(Val); } 447 Expr *getSubExpr() { return cast<Expr>(Val); } 448 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 449 450 static bool classof(const Stmt *T) { 451 return T->getStmtClass() == ParenExprClass; 452 } 453 static bool classof(const ParenExpr *) { return true; } 454 455 // Iterators 456 virtual child_iterator child_begin(); 457 virtual child_iterator child_end(); 458 459 virtual void EmitImpl(llvm::Serializer& S) const; 460 static ParenExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 461}; 462 463 464/// UnaryOperator - This represents the unary-expression's (except sizeof of 465/// types), the postinc/postdec operators from postfix-expression, and various 466/// extensions. 467/// 468/// Notes on various nodes: 469/// 470/// Real/Imag - These return the real/imag part of a complex operand. If 471/// applied to a non-complex value, the former returns its operand and the 472/// later returns zero in the type of the operand. 473/// 474/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 475/// subexpression is a compound literal with the various MemberExpr and 476/// ArraySubscriptExpr's applied to it. 477/// 478class UnaryOperator : public Expr { 479public: 480 // Note that additions to this should also update the StmtVisitor class. 481 enum Opcode { 482 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 483 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 484 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 485 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 486 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 487 SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. 488 Real, Imag, // "__real expr"/"__imag expr" Extension. 489 Extension, // __extension__ marker. 490 OffsetOf // __builtin_offsetof 491 }; 492private: 493 Stmt *Val; 494 Opcode Opc; 495 SourceLocation Loc; 496public: 497 498 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 499 : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {} 500 501 Opcode getOpcode() const { return Opc; } 502 Expr *getSubExpr() const { return cast<Expr>(Val); } 503 504 /// getOperatorLoc - Return the location of the operator. 505 SourceLocation getOperatorLoc() const { return Loc; } 506 507 /// isPostfix - Return true if this is a postfix operation, like x++. 508 static bool isPostfix(Opcode Op); 509 510 /// isPostfix - Return true if this is a prefix operation, like --x. 511 static bool isPrefix(Opcode Op); 512 513 bool isPrefix() const { return isPrefix(Opc); } 514 bool isPostfix() const { return isPostfix(Opc); } 515 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 516 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 517 bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; } 518 bool isOffsetOfOp() const { return Opc == OffsetOf; } 519 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 520 521 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 522 /// corresponds to, e.g. "sizeof" or "[pre]++" 523 static const char *getOpcodeStr(Opcode Op); 524 525 virtual SourceRange getSourceRange() const { 526 if (isPostfix()) 527 return SourceRange(Val->getLocStart(), Loc); 528 else 529 return SourceRange(Loc, Val->getLocEnd()); 530 } 531 virtual SourceLocation getExprLoc() const { return Loc; } 532 533 static bool classof(const Stmt *T) { 534 return T->getStmtClass() == UnaryOperatorClass; 535 } 536 static bool classof(const UnaryOperator *) { return true; } 537 538 int64_t evaluateOffsetOf(ASTContext& C) const; 539 540 // Iterators 541 virtual child_iterator child_begin(); 542 virtual child_iterator child_end(); 543 544 virtual void EmitImpl(llvm::Serializer& S) const; 545 static UnaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 546}; 547 548/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 549/// *types*. sizeof(expr) is handled by UnaryOperator. 550class SizeOfAlignOfTypeExpr : public Expr { 551 bool isSizeof; // true if sizeof, false if alignof. 552 QualType Ty; 553 SourceLocation OpLoc, RParenLoc; 554public: 555 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 556 SourceLocation op, SourceLocation rp) : 557 Expr(SizeOfAlignOfTypeExprClass, resultType), 558 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 559 560 virtual void Destroy(ASTContext& C); 561 562 bool isSizeOf() const { return isSizeof; } 563 QualType getArgumentType() const { return Ty; } 564 565 SourceLocation getOperatorLoc() const { return OpLoc; } 566 567 virtual SourceRange getSourceRange() const { 568 return SourceRange(OpLoc, RParenLoc); 569 } 570 571 static bool classof(const Stmt *T) { 572 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 573 } 574 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 575 576 // Iterators 577 virtual child_iterator child_begin(); 578 virtual child_iterator child_end(); 579 580 virtual void EmitImpl(llvm::Serializer& S) const; 581 static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 582}; 583 584//===----------------------------------------------------------------------===// 585// Postfix Operators. 586//===----------------------------------------------------------------------===// 587 588/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 589class ArraySubscriptExpr : public Expr { 590 enum { LHS, RHS, END_EXPR=2 }; 591 Stmt* SubExprs[END_EXPR]; 592 SourceLocation RBracketLoc; 593public: 594 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 595 SourceLocation rbracketloc) 596 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 597 SubExprs[LHS] = lhs; 598 SubExprs[RHS] = rhs; 599 } 600 601 /// An array access can be written A[4] or 4[A] (both are equivalent). 602 /// - getBase() and getIdx() always present the normalized view: A[4]. 603 /// In this case getBase() returns "A" and getIdx() returns "4". 604 /// - getLHS() and getRHS() present the syntactic view. e.g. for 605 /// 4[A] getLHS() returns "4". 606 /// Note: Because vector element access is also written A[4] we must 607 /// predicate the format conversion in getBase and getIdx only on the 608 /// the type of the RHS, as it is possible for the LHS to be a vector of 609 /// integer type 610 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 611 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 612 613 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 614 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 615 616 Expr *getBase() { 617 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 618 } 619 620 const Expr *getBase() const { 621 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 622 } 623 624 Expr *getIdx() { 625 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 626 } 627 628 const Expr *getIdx() const { 629 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 630 } 631 632 virtual SourceRange getSourceRange() const { 633 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 634 } 635 636 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 637 638 static bool classof(const Stmt *T) { 639 return T->getStmtClass() == ArraySubscriptExprClass; 640 } 641 static bool classof(const ArraySubscriptExpr *) { return true; } 642 643 // Iterators 644 virtual child_iterator child_begin(); 645 virtual child_iterator child_end(); 646 647 virtual void EmitImpl(llvm::Serializer& S) const; 648 static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 649}; 650 651 652/// CallExpr - [C99 6.5.2.2] Function Calls. 653/// 654class CallExpr : public Expr { 655 enum { FN=0, ARGS_START=1 }; 656 Stmt **SubExprs; 657 unsigned NumArgs; 658 SourceLocation RParenLoc; 659 660 // This version of the ctor is for deserialization. 661 CallExpr(Stmt** subexprs, unsigned numargs, QualType t, 662 SourceLocation rparenloc) 663 : Expr(CallExprClass,t), SubExprs(subexprs), 664 NumArgs(numargs), RParenLoc(rparenloc) {} 665 666public: 667 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 668 SourceLocation rparenloc); 669 ~CallExpr() { 670 delete [] SubExprs; 671 } 672 673 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 674 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 675 void setCallee(Expr *F) { SubExprs[FN] = F; } 676 677 /// getNumArgs - Return the number of actual arguments to this call. 678 /// 679 unsigned getNumArgs() const { return NumArgs; } 680 681 /// getArg - Return the specified argument. 682 Expr *getArg(unsigned Arg) { 683 assert(Arg < NumArgs && "Arg access out of range!"); 684 return cast<Expr>(SubExprs[Arg+ARGS_START]); 685 } 686 const Expr *getArg(unsigned Arg) const { 687 assert(Arg < NumArgs && "Arg access out of range!"); 688 return cast<Expr>(SubExprs[Arg+ARGS_START]); 689 } 690 /// setArg - Set the specified argument. 691 void setArg(unsigned Arg, Expr *ArgExpr) { 692 assert(Arg < NumArgs && "Arg access out of range!"); 693 SubExprs[Arg+ARGS_START] = ArgExpr; 694 } 695 696 /// setNumArgs - This changes the number of arguments present in this call. 697 /// Any orphaned expressions are deleted by this, and any new operands are set 698 /// to null. 699 void setNumArgs(unsigned NumArgs); 700 701 typedef ExprIterator arg_iterator; 702 typedef ConstExprIterator const_arg_iterator; 703 704 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 705 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 706 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 707 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 708 709 /// getNumCommas - Return the number of commas that must have been present in 710 /// this function call. 711 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 712 713 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 714 /// not, return 0. 715 unsigned isBuiltinCall() const; 716 717 SourceLocation getRParenLoc() const { return RParenLoc; } 718 719 virtual SourceRange getSourceRange() const { 720 return SourceRange(getCallee()->getLocStart(), RParenLoc); 721 } 722 723 static bool classof(const Stmt *T) { 724 return T->getStmtClass() == CallExprClass; 725 } 726 static bool classof(const CallExpr *) { return true; } 727 728 // Iterators 729 virtual child_iterator child_begin(); 730 virtual child_iterator child_end(); 731 732 virtual void EmitImpl(llvm::Serializer& S) const; 733 static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 734}; 735 736/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 737/// 738class MemberExpr : public Expr { 739 Stmt *Base; 740 FieldDecl *MemberDecl; 741 SourceLocation MemberLoc; 742 bool IsArrow; // True if this is "X->F", false if this is "X.F". 743public: 744 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l, 745 QualType ty) 746 : Expr(MemberExprClass, ty), 747 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 748 749 Expr *getBase() const { return cast<Expr>(Base); } 750 FieldDecl *getMemberDecl() const { return MemberDecl; } 751 bool isArrow() const { return IsArrow; } 752 753 virtual SourceRange getSourceRange() const { 754 return SourceRange(getBase()->getLocStart(), MemberLoc); 755 } 756 757 virtual SourceLocation getExprLoc() const { return MemberLoc; } 758 759 static bool classof(const Stmt *T) { 760 return T->getStmtClass() == MemberExprClass; 761 } 762 static bool classof(const MemberExpr *) { return true; } 763 764 // Iterators 765 virtual child_iterator child_begin(); 766 virtual child_iterator child_end(); 767 768 virtual void EmitImpl(llvm::Serializer& S) const; 769 static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 770}; 771 772/// CompoundLiteralExpr - [C99 6.5.2.5] 773/// 774class CompoundLiteralExpr : public Expr { 775 /// LParenLoc - If non-null, this is the location of the left paren in a 776 /// compound literal like "(int){4}". This can be null if this is a 777 /// synthesized compound expression. 778 SourceLocation LParenLoc; 779 Stmt *Init; 780 bool FileScope; 781public: 782 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 783 bool fileScope) 784 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 785 FileScope(fileScope) {} 786 787 const Expr *getInitializer() const { return cast<Expr>(Init); } 788 Expr *getInitializer() { return cast<Expr>(Init); } 789 790 bool isFileScope() const { return FileScope; } 791 792 SourceLocation getLParenLoc() const { return LParenLoc; } 793 794 virtual SourceRange getSourceRange() const { 795 // FIXME: Init should never be null. 796 if (!Init) 797 return SourceRange(); 798 if (LParenLoc.isInvalid()) 799 return Init->getSourceRange(); 800 return SourceRange(LParenLoc, Init->getLocEnd()); 801 } 802 803 static bool classof(const Stmt *T) { 804 return T->getStmtClass() == CompoundLiteralExprClass; 805 } 806 static bool classof(const CompoundLiteralExpr *) { return true; } 807 808 // Iterators 809 virtual child_iterator child_begin(); 810 virtual child_iterator child_end(); 811 812 virtual void EmitImpl(llvm::Serializer& S) const; 813 static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 814}; 815 816/// CastExpr - Base class for Cast Operators (explicit, implicit, etc.). 817/// Classes that derive from CastExpr are: 818/// 819/// ImplicitCastExpr 820/// ExplicitCastExpr 821/// 822class CastExpr : public Expr { 823 Stmt *Op; 824protected: 825 CastExpr(StmtClass SC, QualType ty, Expr *op) : 826 Expr(SC, ty), Op(op) {} 827 828public: 829 Expr *getSubExpr() { return cast<Expr>(Op); } 830 const Expr *getSubExpr() const { return cast<Expr>(Op); } 831 832 static bool classof(const Stmt *T) { 833 switch (T->getStmtClass()) { 834 case ImplicitCastExprClass: 835 case ExplicitCastExprClass: 836 case CXXFunctionalCastExprClass: 837 return true; 838 default: 839 return false; 840 } 841 } 842 static bool classof(const CastExpr *) { return true; } 843 844 // Iterators 845 virtual child_iterator child_begin(); 846 virtual child_iterator child_end(); 847}; 848 849/// ImplicitCastExpr - Allows us to explicitly represent implicit type 850/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 851/// float->double, short->int, etc. 852/// 853class ImplicitCastExpr : public CastExpr { 854public: 855 ImplicitCastExpr(QualType ty, Expr *op) : 856 CastExpr(ImplicitCastExprClass, ty, op) {} 857 858 virtual SourceRange getSourceRange() const { 859 return getSubExpr()->getSourceRange(); 860 } 861 862 static bool classof(const Stmt *T) { 863 return T->getStmtClass() == ImplicitCastExprClass; 864 } 865 static bool classof(const ImplicitCastExpr *) { return true; } 866 867 virtual void EmitImpl(llvm::Serializer& S) const; 868 static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 869}; 870 871/// ExplicitCastExpr - [C99 6.5.4] Cast Operators. 872/// 873class ExplicitCastExpr : public CastExpr { 874 SourceLocation Loc; // the location of the left paren 875public: 876 ExplicitCastExpr(QualType ty, Expr *op, SourceLocation l) : 877 CastExpr(ExplicitCastExprClass, ty, op), Loc(l) {} 878 879 SourceLocation getLParenLoc() const { return Loc; } 880 881 virtual SourceRange getSourceRange() const { 882 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 883 } 884 static bool classof(const Stmt *T) { 885 return T->getStmtClass() == ExplicitCastExprClass; 886 } 887 static bool classof(const ExplicitCastExpr *) { return true; } 888 889 virtual void EmitImpl(llvm::Serializer& S) const; 890 static ExplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 891}; 892 893class BinaryOperator : public Expr { 894public: 895 enum Opcode { 896 // Operators listed in order of precedence. 897 // Note that additions to this should also update the StmtVisitor class. 898 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 899 Add, Sub, // [C99 6.5.6] Additive operators. 900 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 901 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 902 EQ, NE, // [C99 6.5.9] Equality operators. 903 And, // [C99 6.5.10] Bitwise AND operator. 904 Xor, // [C99 6.5.11] Bitwise XOR operator. 905 Or, // [C99 6.5.12] Bitwise OR operator. 906 LAnd, // [C99 6.5.13] Logical AND operator. 907 LOr, // [C99 6.5.14] Logical OR operator. 908 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 909 DivAssign, RemAssign, 910 AddAssign, SubAssign, 911 ShlAssign, ShrAssign, 912 AndAssign, XorAssign, 913 OrAssign, 914 Comma // [C99 6.5.17] Comma operator. 915 }; 916private: 917 enum { LHS, RHS, END_EXPR }; 918 Stmt* SubExprs[END_EXPR]; 919 Opcode Opc; 920 SourceLocation OpLoc; 921public: 922 923 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 924 SourceLocation opLoc) 925 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 926 SubExprs[LHS] = lhs; 927 SubExprs[RHS] = rhs; 928 assert(!isCompoundAssignmentOp() && 929 "Use ArithAssignBinaryOperator for compound assignments"); 930 } 931 932 SourceLocation getOperatorLoc() const { return OpLoc; } 933 Opcode getOpcode() const { return Opc; } 934 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 935 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 936 virtual SourceRange getSourceRange() const { 937 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 938 } 939 940 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 941 /// corresponds to, e.g. "<<=". 942 static const char *getOpcodeStr(Opcode Op); 943 944 /// predicates to categorize the respective opcodes. 945 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 946 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 947 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 948 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 949 950 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 951 bool isRelationalOp() const { return isRelationalOp(Opc); } 952 953 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 954 bool isEqualityOp() const { return isEqualityOp(Opc); } 955 956 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 957 bool isLogicalOp() const { return isLogicalOp(Opc); } 958 959 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 960 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 961 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 962 963 static bool classof(const Stmt *S) { 964 return S->getStmtClass() == BinaryOperatorClass || 965 S->getStmtClass() == CompoundAssignOperatorClass; 966 } 967 static bool classof(const BinaryOperator *) { return true; } 968 969 // Iterators 970 virtual child_iterator child_begin(); 971 virtual child_iterator child_end(); 972 973 virtual void EmitImpl(llvm::Serializer& S) const; 974 static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 975 976protected: 977 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 978 SourceLocation oploc, bool dead) 979 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 980 SubExprs[LHS] = lhs; 981 SubExprs[RHS] = rhs; 982 } 983}; 984 985/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 986/// track of the type the operation is performed in. Due to the semantics of 987/// these operators, the operands are promoted, the aritmetic performed, an 988/// implicit conversion back to the result type done, then the assignment takes 989/// place. This captures the intermediate type which the computation is done 990/// in. 991class CompoundAssignOperator : public BinaryOperator { 992 QualType ComputationType; 993public: 994 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 995 QualType ResType, QualType CompType, 996 SourceLocation OpLoc) 997 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 998 ComputationType(CompType) { 999 assert(isCompoundAssignmentOp() && 1000 "Only should be used for compound assignments"); 1001 } 1002 1003 QualType getComputationType() const { return ComputationType; } 1004 1005 static bool classof(const CompoundAssignOperator *) { return true; } 1006 static bool classof(const Stmt *S) { 1007 return S->getStmtClass() == CompoundAssignOperatorClass; 1008 } 1009 1010 virtual void EmitImpl(llvm::Serializer& S) const; 1011 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D, 1012 ASTContext& C); 1013}; 1014 1015/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1016/// GNU "missing LHS" extension is in use. 1017/// 1018class ConditionalOperator : public Expr { 1019 enum { COND, LHS, RHS, END_EXPR }; 1020 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1021public: 1022 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1023 : Expr(ConditionalOperatorClass, t) { 1024 SubExprs[COND] = cond; 1025 SubExprs[LHS] = lhs; 1026 SubExprs[RHS] = rhs; 1027 } 1028 1029 // getCond - Return the expression representing the condition for 1030 // the ?: operator. 1031 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1032 1033 // getTrueExpr - Return the subexpression representing the value of the ?: 1034 // expression if the condition evaluates to true. In most cases this value 1035 // will be the same as getLHS() except a GCC extension allows the left 1036 // subexpression to be omitted, and instead of the condition be returned. 1037 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1038 // is only evaluated once. 1039 Expr *getTrueExpr() const { 1040 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1041 } 1042 1043 // getTrueExpr - Return the subexpression representing the value of the ?: 1044 // expression if the condition evaluates to false. This is the same as getRHS. 1045 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1046 1047 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1048 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1049 1050 virtual SourceRange getSourceRange() const { 1051 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1052 } 1053 static bool classof(const Stmt *T) { 1054 return T->getStmtClass() == ConditionalOperatorClass; 1055 } 1056 static bool classof(const ConditionalOperator *) { return true; } 1057 1058 // Iterators 1059 virtual child_iterator child_begin(); 1060 virtual child_iterator child_end(); 1061 1062 virtual void EmitImpl(llvm::Serializer& S) const; 1063 static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1064}; 1065 1066/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1067class AddrLabelExpr : public Expr { 1068 SourceLocation AmpAmpLoc, LabelLoc; 1069 LabelStmt *Label; 1070public: 1071 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1072 QualType t) 1073 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1074 1075 virtual SourceRange getSourceRange() const { 1076 return SourceRange(AmpAmpLoc, LabelLoc); 1077 } 1078 1079 LabelStmt *getLabel() const { return Label; } 1080 1081 static bool classof(const Stmt *T) { 1082 return T->getStmtClass() == AddrLabelExprClass; 1083 } 1084 static bool classof(const AddrLabelExpr *) { return true; } 1085 1086 // Iterators 1087 virtual child_iterator child_begin(); 1088 virtual child_iterator child_end(); 1089 1090 virtual void EmitImpl(llvm::Serializer& S) const; 1091 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1092}; 1093 1094/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1095/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1096/// takes the value of the last subexpression. 1097class StmtExpr : public Expr { 1098 Stmt *SubStmt; 1099 SourceLocation LParenLoc, RParenLoc; 1100public: 1101 StmtExpr(CompoundStmt *substmt, QualType T, 1102 SourceLocation lp, SourceLocation rp) : 1103 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1104 1105 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1106 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1107 1108 virtual SourceRange getSourceRange() const { 1109 return SourceRange(LParenLoc, RParenLoc); 1110 } 1111 1112 static bool classof(const Stmt *T) { 1113 return T->getStmtClass() == StmtExprClass; 1114 } 1115 static bool classof(const StmtExpr *) { return true; } 1116 1117 // Iterators 1118 virtual child_iterator child_begin(); 1119 virtual child_iterator child_end(); 1120 1121 virtual void EmitImpl(llvm::Serializer& S) const; 1122 static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1123}; 1124 1125/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1126/// This AST node represents a function that returns 1 if two *types* (not 1127/// expressions) are compatible. The result of this built-in function can be 1128/// used in integer constant expressions. 1129class TypesCompatibleExpr : public Expr { 1130 QualType Type1; 1131 QualType Type2; 1132 SourceLocation BuiltinLoc, RParenLoc; 1133public: 1134 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1135 QualType t1, QualType t2, SourceLocation RP) : 1136 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1137 BuiltinLoc(BLoc), RParenLoc(RP) {} 1138 1139 QualType getArgType1() const { return Type1; } 1140 QualType getArgType2() const { return Type2; } 1141 1142 virtual SourceRange getSourceRange() const { 1143 return SourceRange(BuiltinLoc, RParenLoc); 1144 } 1145 static bool classof(const Stmt *T) { 1146 return T->getStmtClass() == TypesCompatibleExprClass; 1147 } 1148 static bool classof(const TypesCompatibleExpr *) { return true; } 1149 1150 // Iterators 1151 virtual child_iterator child_begin(); 1152 virtual child_iterator child_end(); 1153 1154 virtual void EmitImpl(llvm::Serializer& S) const; 1155 static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1156}; 1157 1158/// ShuffleVectorExpr - clang-specific builtin-in function 1159/// __builtin_shufflevector. 1160/// This AST node represents a operator that does a constant 1161/// shuffle, similar to LLVM's shufflevector instruction. It takes 1162/// two vectors and a variable number of constant indices, 1163/// and returns the appropriately shuffled vector. 1164class ShuffleVectorExpr : public Expr { 1165 SourceLocation BuiltinLoc, RParenLoc; 1166 1167 // SubExprs - the list of values passed to the __builtin_shufflevector 1168 // function. The first two are vectors, and the rest are constant 1169 // indices. The number of values in this list is always 1170 // 2+the number of indices in the vector type. 1171 Stmt **SubExprs; 1172 unsigned NumExprs; 1173 1174public: 1175 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1176 QualType Type, SourceLocation BLoc, 1177 SourceLocation RP) : 1178 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1179 RParenLoc(RP), NumExprs(nexpr) { 1180 1181 SubExprs = new Stmt*[nexpr]; 1182 for (unsigned i = 0; i < nexpr; i++) 1183 SubExprs[i] = args[i]; 1184 } 1185 1186 virtual SourceRange getSourceRange() const { 1187 return SourceRange(BuiltinLoc, RParenLoc); 1188 } 1189 static bool classof(const Stmt *T) { 1190 return T->getStmtClass() == ShuffleVectorExprClass; 1191 } 1192 static bool classof(const ShuffleVectorExpr *) { return true; } 1193 1194 ~ShuffleVectorExpr() { 1195 delete [] SubExprs; 1196 } 1197 1198 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1199 /// constant expression, the actual arguments passed in, and the function 1200 /// pointers. 1201 unsigned getNumSubExprs() const { return NumExprs; } 1202 1203 /// getExpr - Return the Expr at the specified index. 1204 Expr *getExpr(unsigned Index) { 1205 assert((Index < NumExprs) && "Arg access out of range!"); 1206 return cast<Expr>(SubExprs[Index]); 1207 } 1208 const Expr *getExpr(unsigned Index) const { 1209 assert((Index < NumExprs) && "Arg access out of range!"); 1210 return cast<Expr>(SubExprs[Index]); 1211 } 1212 1213 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1214 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1215 return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1216 } 1217 1218 // Iterators 1219 virtual child_iterator child_begin(); 1220 virtual child_iterator child_end(); 1221 1222 virtual void EmitImpl(llvm::Serializer& S) const; 1223 static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1224}; 1225 1226/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1227/// This AST node is similar to the conditional operator (?:) in C, with 1228/// the following exceptions: 1229/// - the test expression much be a constant expression. 1230/// - the expression returned has it's type unaltered by promotion rules. 1231/// - does not evaluate the expression that was not chosen. 1232class ChooseExpr : public Expr { 1233 enum { COND, LHS, RHS, END_EXPR }; 1234 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1235 SourceLocation BuiltinLoc, RParenLoc; 1236public: 1237 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1238 SourceLocation RP) 1239 : Expr(ChooseExprClass, t), 1240 BuiltinLoc(BLoc), RParenLoc(RP) { 1241 SubExprs[COND] = cond; 1242 SubExprs[LHS] = lhs; 1243 SubExprs[RHS] = rhs; 1244 } 1245 1246 /// isConditionTrue - Return true if the condition is true. This is always 1247 /// statically knowable for a well-formed choosexpr. 1248 bool isConditionTrue(ASTContext &C) const; 1249 1250 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1251 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1252 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1253 1254 virtual SourceRange getSourceRange() const { 1255 return SourceRange(BuiltinLoc, RParenLoc); 1256 } 1257 static bool classof(const Stmt *T) { 1258 return T->getStmtClass() == ChooseExprClass; 1259 } 1260 static bool classof(const ChooseExpr *) { return true; } 1261 1262 // Iterators 1263 virtual child_iterator child_begin(); 1264 virtual child_iterator child_end(); 1265 1266 virtual void EmitImpl(llvm::Serializer& S) const; 1267 static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1268}; 1269 1270/// OverloadExpr - Clang builtin function __builtin_overload. 1271/// This AST node provides a way to overload functions in C. 1272/// 1273/// The first argument is required to be a constant expression, for the number 1274/// of arguments passed to each candidate function. 1275/// 1276/// The next N arguments, where N is the value of the constant expression, 1277/// are the values to be passed as arguments. 1278/// 1279/// The rest of the arguments are values of pointer to function type, which 1280/// are the candidate functions for overloading. 1281/// 1282/// The result is a equivalent to a CallExpr taking N arguments to the 1283/// candidate function whose parameter types match the types of the N arguments. 1284/// 1285/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl); 1286/// If X and Y are long doubles, Z will assigned the result of modl(X, Y); 1287/// If X and Y are floats, Z will be assigned the result of modf(X, Y); 1288class OverloadExpr : public Expr { 1289 // SubExprs - the list of values passed to the __builtin_overload function. 1290 // SubExpr[0] is a constant expression 1291 // SubExpr[1-N] are the parameters to pass to the matching function call 1292 // SubExpr[N-...] are the candidate functions, of type pointer to function. 1293 Stmt **SubExprs; 1294 1295 // NumExprs - the size of the SubExprs array 1296 unsigned NumExprs; 1297 1298 // The index of the matching candidate function 1299 unsigned FnIndex; 1300 1301 SourceLocation BuiltinLoc; 1302 SourceLocation RParenLoc; 1303public: 1304 OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t, 1305 SourceLocation bloc, SourceLocation rploc) 1306 : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx), 1307 BuiltinLoc(bloc), RParenLoc(rploc) { 1308 SubExprs = new Stmt*[nexprs]; 1309 for (unsigned i = 0; i != nexprs; ++i) 1310 SubExprs[i] = args[i]; 1311 } 1312 ~OverloadExpr() { 1313 delete [] SubExprs; 1314 } 1315 1316 /// arg_begin - Return a pointer to the list of arguments that will be passed 1317 /// to the matching candidate function, skipping over the initial constant 1318 /// expression. 1319 typedef ConstExprIterator const_arg_iterator; 1320 const_arg_iterator arg_begin() const { return &SubExprs[0]+1; } 1321 const_arg_iterator arg_end(ASTContext& Ctx) const { 1322 return &SubExprs[0]+1+getNumArgs(Ctx); 1323 } 1324 1325 /// getNumArgs - Return the number of arguments to pass to the candidate 1326 /// functions. 1327 unsigned getNumArgs(ASTContext &Ctx) const { 1328 return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1329 } 1330 1331 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1332 /// constant expression, the actual arguments passed in, and the function 1333 /// pointers. 1334 unsigned getNumSubExprs() const { return NumExprs; } 1335 1336 /// getExpr - Return the Expr at the specified index. 1337 Expr *getExpr(unsigned Index) const { 1338 assert((Index < NumExprs) && "Arg access out of range!"); 1339 return cast<Expr>(SubExprs[Index]); 1340 } 1341 1342 /// getFn - Return the matching candidate function for this OverloadExpr. 1343 Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); } 1344 1345 virtual SourceRange getSourceRange() const { 1346 return SourceRange(BuiltinLoc, RParenLoc); 1347 } 1348 static bool classof(const Stmt *T) { 1349 return T->getStmtClass() == OverloadExprClass; 1350 } 1351 static bool classof(const OverloadExpr *) { return true; } 1352 1353 // Iterators 1354 virtual child_iterator child_begin(); 1355 virtual child_iterator child_end(); 1356 1357 virtual void EmitImpl(llvm::Serializer& S) const; 1358 static OverloadExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1359}; 1360 1361/// VAArgExpr, used for the builtin function __builtin_va_start. 1362class VAArgExpr : public Expr { 1363 Stmt *Val; 1364 SourceLocation BuiltinLoc, RParenLoc; 1365public: 1366 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1367 : Expr(VAArgExprClass, t), 1368 Val(e), 1369 BuiltinLoc(BLoc), 1370 RParenLoc(RPLoc) { } 1371 1372 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1373 Expr *getSubExpr() { return cast<Expr>(Val); } 1374 virtual SourceRange getSourceRange() const { 1375 return SourceRange(BuiltinLoc, RParenLoc); 1376 } 1377 static bool classof(const Stmt *T) { 1378 return T->getStmtClass() == VAArgExprClass; 1379 } 1380 static bool classof(const VAArgExpr *) { return true; } 1381 1382 // Iterators 1383 virtual child_iterator child_begin(); 1384 virtual child_iterator child_end(); 1385 1386 virtual void EmitImpl(llvm::Serializer& S) const; 1387 static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1388}; 1389 1390/// InitListExpr - used for struct and array initializers, such as: 1391/// struct foo x = { 1, { 2, 3 } }; 1392/// 1393/// Because C is somewhat loose with braces, the AST does not necessarily 1394/// directly model the C source. Instead, the semantic analyzer aims to make 1395/// the InitListExprs match up with the type of the decl being initialized. We 1396/// have the following exceptions: 1397/// 1398/// 1. Elements at the end of the list may be dropped from the initializer. 1399/// These elements are defined to be initialized to zero. For example: 1400/// int x[20] = { 1 }; 1401/// 2. Initializers may have excess initializers which are to be ignored by the 1402/// compiler. For example: 1403/// int x[1] = { 1, 2 }; 1404/// 3. Redundant InitListExprs may be present around scalar elements. These 1405/// always have a single element whose type is the same as the InitListExpr. 1406/// this can only happen for Type::isScalarType() types. 1407/// int x = { 1 }; int y[2] = { {1}, {2} }; 1408/// 1409class InitListExpr : public Expr { 1410 std::vector<Stmt *> InitExprs; 1411 SourceLocation LBraceLoc, RBraceLoc; 1412public: 1413 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1414 SourceLocation rbraceloc); 1415 1416 unsigned getNumInits() const { return InitExprs.size(); } 1417 1418 const Expr* getInit(unsigned Init) const { 1419 assert(Init < getNumInits() && "Initializer access out of range!"); 1420 return cast<Expr>(InitExprs[Init]); 1421 } 1422 1423 Expr* getInit(unsigned Init) { 1424 assert(Init < getNumInits() && "Initializer access out of range!"); 1425 return cast<Expr>(InitExprs[Init]); 1426 } 1427 1428 void setInit(unsigned Init, Expr *expr) { 1429 assert(Init < getNumInits() && "Initializer access out of range!"); 1430 InitExprs[Init] = expr; 1431 } 1432 1433 // Dynamic removal/addition (for constructing implicit InitExpr's). 1434 void removeInit(unsigned Init) { 1435 InitExprs.erase(InitExprs.begin()+Init); 1436 } 1437 void addInit(unsigned Init, Expr *expr) { 1438 InitExprs.insert(InitExprs.begin()+Init, expr); 1439 } 1440 1441 // Explicit InitListExpr's originate from source code (and have valid source 1442 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1443 bool isExplicit() { 1444 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1445 } 1446 1447 virtual SourceRange getSourceRange() const { 1448 return SourceRange(LBraceLoc, RBraceLoc); 1449 } 1450 static bool classof(const Stmt *T) { 1451 return T->getStmtClass() == InitListExprClass; 1452 } 1453 static bool classof(const InitListExpr *) { return true; } 1454 1455 // Iterators 1456 virtual child_iterator child_begin(); 1457 virtual child_iterator child_end(); 1458 1459 virtual void EmitImpl(llvm::Serializer& S) const; 1460 static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1461 1462private: 1463 // Used by serializer. 1464 InitListExpr() : Expr(InitListExprClass, QualType()) {} 1465}; 1466 1467//===----------------------------------------------------------------------===// 1468// Clang Extensions 1469//===----------------------------------------------------------------------===// 1470 1471 1472/// ExtVectorElementExpr - This represents access to specific elements of a 1473/// vector, and may occur on the left hand side or right hand side. For example 1474/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 1475/// 1476class ExtVectorElementExpr : public Expr { 1477 Stmt *Base; 1478 IdentifierInfo &Accessor; 1479 SourceLocation AccessorLoc; 1480public: 1481 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 1482 SourceLocation loc) 1483 : Expr(ExtVectorElementExprClass, ty), 1484 Base(base), Accessor(accessor), AccessorLoc(loc) {} 1485 1486 const Expr *getBase() const { return cast<Expr>(Base); } 1487 Expr *getBase() { return cast<Expr>(Base); } 1488 1489 IdentifierInfo &getAccessor() const { return Accessor; } 1490 1491 /// getNumElements - Get the number of components being selected. 1492 unsigned getNumElements() const; 1493 1494 /// containsDuplicateElements - Return true if any element access is 1495 /// repeated. 1496 bool containsDuplicateElements() const; 1497 1498 /// getEncodedElementAccess - Encode the elements accessed into an llvm 1499 /// aggregate Constant of ConstantInt(s). 1500 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 1501 1502 virtual SourceRange getSourceRange() const { 1503 return SourceRange(getBase()->getLocStart(), AccessorLoc); 1504 } 1505 1506 static bool classof(const Stmt *T) { 1507 return T->getStmtClass() == ExtVectorElementExprClass; 1508 } 1509 static bool classof(const ExtVectorElementExpr *) { return true; } 1510 1511 // Iterators 1512 virtual child_iterator child_begin(); 1513 virtual child_iterator child_end(); 1514 1515 virtual void EmitImpl(llvm::Serializer& S) const; 1516 static ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1517}; 1518 1519 1520/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 1521/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1522class BlockExpr : public Expr { 1523protected: 1524 BlockDecl *TheBlock; 1525public: 1526 BlockExpr(BlockDecl *BD, QualType ty) : Expr(BlockExprClass, ty), 1527 TheBlock(BD) {} 1528 1529 BlockDecl *getBlockDecl() { return TheBlock; } 1530 1531 // Convenience functions for probing the underlying BlockDecl. 1532 SourceLocation getCaretLocation() const; 1533 const Stmt *getBody() const; 1534 Stmt *getBody(); 1535 1536 virtual SourceRange getSourceRange() const { 1537 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 1538 } 1539 1540 /// getFunctionType - Return the underlying function type for this block. 1541 const FunctionType *getFunctionType() const; 1542 1543 static bool classof(const Stmt *T) { 1544 return T->getStmtClass() == BlockExprClass; 1545 } 1546 static bool classof(const BlockExpr *) { return true; } 1547 1548 // Iterators 1549 virtual child_iterator child_begin(); 1550 virtual child_iterator child_end(); 1551 1552 virtual void EmitImpl(llvm::Serializer& S) const; 1553 static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1554}; 1555 1556/// BlockDeclRefExpr - A reference to a declared variable, function, 1557/// enum, etc. 1558class BlockDeclRefExpr : public Expr { 1559 ValueDecl *D; 1560 SourceLocation Loc; 1561 bool IsByRef; 1562public: 1563 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 1564 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 1565 1566 ValueDecl *getDecl() { return D; } 1567 const ValueDecl *getDecl() const { return D; } 1568 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 1569 1570 bool isByRef() const { return IsByRef; } 1571 1572 static bool classof(const Stmt *T) { 1573 return T->getStmtClass() == BlockDeclRefExprClass; 1574 } 1575 static bool classof(const BlockDeclRefExpr *) { return true; } 1576 1577 // Iterators 1578 virtual child_iterator child_begin(); 1579 virtual child_iterator child_end(); 1580 1581 virtual void EmitImpl(llvm::Serializer& S) const; 1582 static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1583}; 1584 1585} // end namespace clang 1586 1587#endif 1588