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