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