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