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