Expr.h revision eb8f3063257a392f15aea48d42fb73ec51afc548
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 - [C99 6.5.2.2] Function Calls. 637/// 638class CallExpr : public Expr { 639 enum { FN=0, ARGS_START=1 }; 640 Stmt **SubExprs; 641 unsigned NumArgs; 642 SourceLocation RParenLoc; 643 644 // This version of the ctor is for deserialization. 645 CallExpr(Stmt** subexprs, unsigned numargs, QualType t, 646 SourceLocation rparenloc) 647 : Expr(CallExprClass,t), SubExprs(subexprs), 648 NumArgs(numargs), RParenLoc(rparenloc) {} 649 650public: 651 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 652 SourceLocation rparenloc); 653 ~CallExpr() { 654 delete [] SubExprs; 655 } 656 657 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 658 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 659 void setCallee(Expr *F) { SubExprs[FN] = F; } 660 661 /// getNumArgs - Return the number of actual arguments to this call. 662 /// 663 unsigned getNumArgs() const { return NumArgs; } 664 665 /// getArg - Return the specified argument. 666 Expr *getArg(unsigned Arg) { 667 assert(Arg < NumArgs && "Arg access out of range!"); 668 return cast<Expr>(SubExprs[Arg+ARGS_START]); 669 } 670 const Expr *getArg(unsigned Arg) const { 671 assert(Arg < NumArgs && "Arg access out of range!"); 672 return cast<Expr>(SubExprs[Arg+ARGS_START]); 673 } 674 /// setArg - Set the specified argument. 675 void setArg(unsigned Arg, Expr *ArgExpr) { 676 assert(Arg < NumArgs && "Arg access out of range!"); 677 SubExprs[Arg+ARGS_START] = ArgExpr; 678 } 679 680 /// setNumArgs - This changes the number of arguments present in this call. 681 /// Any orphaned expressions are deleted by this, and any new operands are set 682 /// to null. 683 void setNumArgs(unsigned NumArgs); 684 685 typedef ExprIterator arg_iterator; 686 typedef ConstExprIterator const_arg_iterator; 687 688 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 689 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 690 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 691 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 692 693 /// getNumCommas - Return the number of commas that must have been present in 694 /// this function call. 695 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 696 697 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 698 /// not, return 0. 699 unsigned isBuiltinCall() const; 700 701 SourceLocation getRParenLoc() const { return RParenLoc; } 702 703 virtual SourceRange getSourceRange() const { 704 return SourceRange(getCallee()->getLocStart(), RParenLoc); 705 } 706 707 static bool classof(const Stmt *T) { 708 return T->getStmtClass() == CallExprClass; 709 } 710 static bool classof(const CallExpr *) { return true; } 711 712 // Iterators 713 virtual child_iterator child_begin(); 714 virtual child_iterator child_end(); 715 716 virtual void EmitImpl(llvm::Serializer& S) const; 717 static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 718}; 719 720/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 721/// 722class MemberExpr : public Expr { 723 Stmt *Base; 724 FieldDecl *MemberDecl; 725 SourceLocation MemberLoc; 726 bool IsArrow; // True if this is "X->F", false if this is "X.F". 727public: 728 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l, 729 QualType ty) 730 : Expr(MemberExprClass, ty), 731 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 732 733 Expr *getBase() const { return cast<Expr>(Base); } 734 FieldDecl *getMemberDecl() const { return MemberDecl; } 735 bool isArrow() const { return IsArrow; } 736 737 virtual SourceRange getSourceRange() const { 738 return SourceRange(getBase()->getLocStart(), MemberLoc); 739 } 740 741 virtual SourceLocation getExprLoc() const { return MemberLoc; } 742 743 static bool classof(const Stmt *T) { 744 return T->getStmtClass() == MemberExprClass; 745 } 746 static bool classof(const MemberExpr *) { return true; } 747 748 // Iterators 749 virtual child_iterator child_begin(); 750 virtual child_iterator child_end(); 751 752 virtual void EmitImpl(llvm::Serializer& S) const; 753 static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 754}; 755 756/// CompoundLiteralExpr - [C99 6.5.2.5] 757/// 758class CompoundLiteralExpr : public Expr { 759 /// LParenLoc - If non-null, this is the location of the left paren in a 760 /// compound literal like "(int){4}". This can be null if this is a 761 /// synthesized compound expression. 762 SourceLocation LParenLoc; 763 Stmt *Init; 764 bool FileScope; 765public: 766 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 767 bool fileScope) 768 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 769 FileScope(fileScope) {} 770 771 const Expr *getInitializer() const { return cast<Expr>(Init); } 772 Expr *getInitializer() { return cast<Expr>(Init); } 773 774 bool isFileScope() const { return FileScope; } 775 776 SourceLocation getLParenLoc() const { return LParenLoc; } 777 778 virtual SourceRange getSourceRange() const { 779 // FIXME: Init should never be null. 780 if (!Init) 781 return SourceRange(); 782 if (LParenLoc.isInvalid()) 783 return Init->getSourceRange(); 784 return SourceRange(LParenLoc, Init->getLocEnd()); 785 } 786 787 static bool classof(const Stmt *T) { 788 return T->getStmtClass() == CompoundLiteralExprClass; 789 } 790 static bool classof(const CompoundLiteralExpr *) { return true; } 791 792 // Iterators 793 virtual child_iterator child_begin(); 794 virtual child_iterator child_end(); 795 796 virtual void EmitImpl(llvm::Serializer& S) const; 797 static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 798}; 799 800/// CastExpr - Base class for type casts, including both implicit 801/// casts (ImplicitCastExpr) and explicit casts that have some 802/// representation in the source code (ExplicitCastExpr's derived 803/// classes). 804class CastExpr : public Expr { 805 Stmt *Op; 806protected: 807 CastExpr(StmtClass SC, QualType ty, Expr *op) : 808 Expr(SC, ty), Op(op) {} 809 810public: 811 Expr *getSubExpr() { return cast<Expr>(Op); } 812 const Expr *getSubExpr() const { return cast<Expr>(Op); } 813 814 static bool classof(const Stmt *T) { 815 StmtClass SC = T->getStmtClass(); 816 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 817 return true; 818 819 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 820 return true; 821 822 return false; 823 } 824 static bool classof(const CastExpr *) { return true; } 825 826 // Iterators 827 virtual child_iterator child_begin(); 828 virtual child_iterator child_end(); 829}; 830 831/// ImplicitCastExpr - Allows us to explicitly represent implicit type 832/// conversions, which have no direct representation in the original 833/// source code. For example: converting T[]->T*, void f()->void 834/// (*f)(), float->double, short->int, etc. 835/// 836class ImplicitCastExpr : public CastExpr { 837 /// LvalueCast - Whether this cast produces an lvalue. 838 bool LvalueCast; 839 840public: 841 ImplicitCastExpr(QualType ty, Expr *op, bool Lvalue) : 842 CastExpr(ImplicitCastExprClass, ty, op), LvalueCast(Lvalue) {} 843 844 virtual SourceRange getSourceRange() const { 845 return getSubExpr()->getSourceRange(); 846 } 847 848 /// isLvalueCast - Whether this cast produces an lvalue. 849 bool isLvalueCast() const { return LvalueCast; } 850 851 /// setLvalueCast - Set whether this cast produces an lvalue. 852 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 853 854 static bool classof(const Stmt *T) { 855 return T->getStmtClass() == ImplicitCastExprClass; 856 } 857 static bool classof(const ImplicitCastExpr *) { return true; } 858 859 virtual void EmitImpl(llvm::Serializer& S) const; 860 static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 861}; 862 863/// ExplicitCastExpr - An explicit cast written in the source 864/// code. 865/// 866/// This class is effectively an abstract class, because it provides 867/// the basic representation of an explicitly-written cast without 868/// specifying which kind of cast (C cast, functional cast, static 869/// cast, etc.) was written; specific derived classes represent the 870/// particular style of cast and its location information. 871/// 872/// Unlike implicit casts, explicit cast nodes have two different 873/// types: the type that was written into the source code, and the 874/// actual type of the expression as determined by semantic 875/// analysis. These types may differ slightly. For example, in C++ one 876/// can cast to a reference type, which indicates that the resulting 877/// expression will be an lvalue. The reference type, however, will 878/// not be used as the type of the expression. 879class ExplicitCastExpr : public CastExpr { 880 /// TypeAsWritten - The type that this expression is casting to, as 881 /// written in the source code. 882 QualType TypeAsWritten; 883 884protected: 885 ExplicitCastExpr(StmtClass SC, QualType exprTy, Expr *op, QualType writtenTy) 886 : CastExpr(SC, exprTy, op), TypeAsWritten(writtenTy) {} 887 888public: 889 /// getTypeAsWritten - Returns the type that this expression is 890 /// casting to, as written in the source code. 891 QualType getTypeAsWritten() const { return TypeAsWritten; } 892 893 static bool classof(const Stmt *T) { 894 StmtClass SC = T->getStmtClass(); 895 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 896 return true; 897 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 898 return true; 899 900 return false; 901 } 902 static bool classof(const ExplicitCastExpr *) { return true; } 903}; 904 905/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 906/// cast in C++ (C++ [expr.cast]), which uses the syntax 907/// (Type)expr. For example: @c (int)f. 908class CStyleCastExpr : public ExplicitCastExpr { 909 SourceLocation LPLoc; // the location of the left paren 910 SourceLocation RPLoc; // the location of the right paren 911public: 912 CStyleCastExpr(QualType exprTy, Expr *op, QualType writtenTy, 913 SourceLocation l, SourceLocation r) : 914 ExplicitCastExpr(CStyleCastExprClass, exprTy, op, writtenTy), 915 LPLoc(l), RPLoc(r) {} 916 917 SourceLocation getLParenLoc() const { return LPLoc; } 918 SourceLocation getRParenLoc() const { return RPLoc; } 919 920 virtual SourceRange getSourceRange() const { 921 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 922 } 923 static bool classof(const Stmt *T) { 924 return T->getStmtClass() == CStyleCastExprClass; 925 } 926 static bool classof(const CStyleCastExpr *) { return true; } 927 928 virtual void EmitImpl(llvm::Serializer& S) const; 929 static CStyleCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 930}; 931 932class BinaryOperator : public Expr { 933public: 934 enum Opcode { 935 // Operators listed in order of precedence. 936 // Note that additions to this should also update the StmtVisitor class. 937 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 938 Add, Sub, // [C99 6.5.6] Additive operators. 939 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 940 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 941 EQ, NE, // [C99 6.5.9] Equality operators. 942 And, // [C99 6.5.10] Bitwise AND operator. 943 Xor, // [C99 6.5.11] Bitwise XOR operator. 944 Or, // [C99 6.5.12] Bitwise OR operator. 945 LAnd, // [C99 6.5.13] Logical AND operator. 946 LOr, // [C99 6.5.14] Logical OR operator. 947 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 948 DivAssign, RemAssign, 949 AddAssign, SubAssign, 950 ShlAssign, ShrAssign, 951 AndAssign, XorAssign, 952 OrAssign, 953 Comma // [C99 6.5.17] Comma operator. 954 }; 955private: 956 enum { LHS, RHS, END_EXPR }; 957 Stmt* SubExprs[END_EXPR]; 958 Opcode Opc; 959 SourceLocation OpLoc; 960public: 961 962 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 963 SourceLocation opLoc) 964 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 965 SubExprs[LHS] = lhs; 966 SubExprs[RHS] = rhs; 967 assert(!isCompoundAssignmentOp() && 968 "Use ArithAssignBinaryOperator for compound assignments"); 969 } 970 971 SourceLocation getOperatorLoc() const { return OpLoc; } 972 Opcode getOpcode() const { return Opc; } 973 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 974 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 975 virtual SourceRange getSourceRange() const { 976 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 977 } 978 979 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 980 /// corresponds to, e.g. "<<=". 981 static const char *getOpcodeStr(Opcode Op); 982 983 /// predicates to categorize the respective opcodes. 984 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 985 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 986 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 987 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 988 989 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 990 bool isRelationalOp() const { return isRelationalOp(Opc); } 991 992 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 993 bool isEqualityOp() const { return isEqualityOp(Opc); } 994 995 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 996 bool isLogicalOp() const { return isLogicalOp(Opc); } 997 998 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 999 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1000 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1001 1002 static bool classof(const Stmt *S) { 1003 return S->getStmtClass() == BinaryOperatorClass || 1004 S->getStmtClass() == CompoundAssignOperatorClass; 1005 } 1006 static bool classof(const BinaryOperator *) { return true; } 1007 1008 // Iterators 1009 virtual child_iterator child_begin(); 1010 virtual child_iterator child_end(); 1011 1012 virtual void EmitImpl(llvm::Serializer& S) const; 1013 static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1014 1015protected: 1016 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1017 SourceLocation oploc, bool dead) 1018 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1019 SubExprs[LHS] = lhs; 1020 SubExprs[RHS] = rhs; 1021 } 1022}; 1023 1024/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1025/// track of the type the operation is performed in. Due to the semantics of 1026/// these operators, the operands are promoted, the aritmetic performed, an 1027/// implicit conversion back to the result type done, then the assignment takes 1028/// place. This captures the intermediate type which the computation is done 1029/// in. 1030class CompoundAssignOperator : public BinaryOperator { 1031 QualType ComputationType; 1032public: 1033 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1034 QualType ResType, QualType CompType, 1035 SourceLocation OpLoc) 1036 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1037 ComputationType(CompType) { 1038 assert(isCompoundAssignmentOp() && 1039 "Only should be used for compound assignments"); 1040 } 1041 1042 QualType getComputationType() const { return ComputationType; } 1043 1044 static bool classof(const CompoundAssignOperator *) { return true; } 1045 static bool classof(const Stmt *S) { 1046 return S->getStmtClass() == CompoundAssignOperatorClass; 1047 } 1048 1049 virtual void EmitImpl(llvm::Serializer& S) const; 1050 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D, 1051 ASTContext& C); 1052}; 1053 1054/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1055/// GNU "missing LHS" extension is in use. 1056/// 1057class ConditionalOperator : public Expr { 1058 enum { COND, LHS, RHS, END_EXPR }; 1059 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1060public: 1061 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1062 : Expr(ConditionalOperatorClass, t) { 1063 SubExprs[COND] = cond; 1064 SubExprs[LHS] = lhs; 1065 SubExprs[RHS] = rhs; 1066 } 1067 1068 // getCond - Return the expression representing the condition for 1069 // the ?: operator. 1070 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1071 1072 // getTrueExpr - Return the subexpression representing the value of the ?: 1073 // expression if the condition evaluates to true. In most cases this value 1074 // will be the same as getLHS() except a GCC extension allows the left 1075 // subexpression to be omitted, and instead of the condition be returned. 1076 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1077 // is only evaluated once. 1078 Expr *getTrueExpr() const { 1079 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1080 } 1081 1082 // getTrueExpr - Return the subexpression representing the value of the ?: 1083 // expression if the condition evaluates to false. This is the same as getRHS. 1084 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1085 1086 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1087 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1088 1089 virtual SourceRange getSourceRange() const { 1090 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1091 } 1092 static bool classof(const Stmt *T) { 1093 return T->getStmtClass() == ConditionalOperatorClass; 1094 } 1095 static bool classof(const ConditionalOperator *) { return true; } 1096 1097 // Iterators 1098 virtual child_iterator child_begin(); 1099 virtual child_iterator child_end(); 1100 1101 virtual void EmitImpl(llvm::Serializer& S) const; 1102 static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1103}; 1104 1105/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1106class AddrLabelExpr : public Expr { 1107 SourceLocation AmpAmpLoc, LabelLoc; 1108 LabelStmt *Label; 1109public: 1110 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1111 QualType t) 1112 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1113 1114 virtual SourceRange getSourceRange() const { 1115 return SourceRange(AmpAmpLoc, LabelLoc); 1116 } 1117 1118 LabelStmt *getLabel() const { return Label; } 1119 1120 static bool classof(const Stmt *T) { 1121 return T->getStmtClass() == AddrLabelExprClass; 1122 } 1123 static bool classof(const AddrLabelExpr *) { return true; } 1124 1125 // Iterators 1126 virtual child_iterator child_begin(); 1127 virtual child_iterator child_end(); 1128 1129 virtual void EmitImpl(llvm::Serializer& S) const; 1130 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1131}; 1132 1133/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1134/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1135/// takes the value of the last subexpression. 1136class StmtExpr : public Expr { 1137 Stmt *SubStmt; 1138 SourceLocation LParenLoc, RParenLoc; 1139public: 1140 StmtExpr(CompoundStmt *substmt, QualType T, 1141 SourceLocation lp, SourceLocation rp) : 1142 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1143 1144 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1145 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1146 1147 virtual SourceRange getSourceRange() const { 1148 return SourceRange(LParenLoc, RParenLoc); 1149 } 1150 1151 static bool classof(const Stmt *T) { 1152 return T->getStmtClass() == StmtExprClass; 1153 } 1154 static bool classof(const StmtExpr *) { return true; } 1155 1156 // Iterators 1157 virtual child_iterator child_begin(); 1158 virtual child_iterator child_end(); 1159 1160 virtual void EmitImpl(llvm::Serializer& S) const; 1161 static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1162}; 1163 1164/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1165/// This AST node represents a function that returns 1 if two *types* (not 1166/// expressions) are compatible. The result of this built-in function can be 1167/// used in integer constant expressions. 1168class TypesCompatibleExpr : public Expr { 1169 QualType Type1; 1170 QualType Type2; 1171 SourceLocation BuiltinLoc, RParenLoc; 1172public: 1173 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1174 QualType t1, QualType t2, SourceLocation RP) : 1175 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1176 BuiltinLoc(BLoc), RParenLoc(RP) {} 1177 1178 QualType getArgType1() const { return Type1; } 1179 QualType getArgType2() const { return Type2; } 1180 1181 virtual SourceRange getSourceRange() const { 1182 return SourceRange(BuiltinLoc, RParenLoc); 1183 } 1184 static bool classof(const Stmt *T) { 1185 return T->getStmtClass() == TypesCompatibleExprClass; 1186 } 1187 static bool classof(const TypesCompatibleExpr *) { return true; } 1188 1189 // Iterators 1190 virtual child_iterator child_begin(); 1191 virtual child_iterator child_end(); 1192 1193 virtual void EmitImpl(llvm::Serializer& S) const; 1194 static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1195}; 1196 1197/// ShuffleVectorExpr - clang-specific builtin-in function 1198/// __builtin_shufflevector. 1199/// This AST node represents a operator that does a constant 1200/// shuffle, similar to LLVM's shufflevector instruction. It takes 1201/// two vectors and a variable number of constant indices, 1202/// and returns the appropriately shuffled vector. 1203class ShuffleVectorExpr : public Expr { 1204 SourceLocation BuiltinLoc, RParenLoc; 1205 1206 // SubExprs - the list of values passed to the __builtin_shufflevector 1207 // function. The first two are vectors, and the rest are constant 1208 // indices. The number of values in this list is always 1209 // 2+the number of indices in the vector type. 1210 Stmt **SubExprs; 1211 unsigned NumExprs; 1212 1213public: 1214 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1215 QualType Type, SourceLocation BLoc, 1216 SourceLocation RP) : 1217 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1218 RParenLoc(RP), NumExprs(nexpr) { 1219 1220 SubExprs = new Stmt*[nexpr]; 1221 for (unsigned i = 0; i < nexpr; i++) 1222 SubExprs[i] = args[i]; 1223 } 1224 1225 virtual SourceRange getSourceRange() const { 1226 return SourceRange(BuiltinLoc, RParenLoc); 1227 } 1228 static bool classof(const Stmt *T) { 1229 return T->getStmtClass() == ShuffleVectorExprClass; 1230 } 1231 static bool classof(const ShuffleVectorExpr *) { return true; } 1232 1233 ~ShuffleVectorExpr() { 1234 delete [] SubExprs; 1235 } 1236 1237 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1238 /// constant expression, the actual arguments passed in, and the function 1239 /// pointers. 1240 unsigned getNumSubExprs() const { return NumExprs; } 1241 1242 /// getExpr - Return the Expr at the specified index. 1243 Expr *getExpr(unsigned Index) { 1244 assert((Index < NumExprs) && "Arg access out of range!"); 1245 return cast<Expr>(SubExprs[Index]); 1246 } 1247 const Expr *getExpr(unsigned Index) const { 1248 assert((Index < NumExprs) && "Arg access out of range!"); 1249 return cast<Expr>(SubExprs[Index]); 1250 } 1251 1252 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1253 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1254 return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1255 } 1256 1257 // Iterators 1258 virtual child_iterator child_begin(); 1259 virtual child_iterator child_end(); 1260 1261 virtual void EmitImpl(llvm::Serializer& S) const; 1262 static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1263}; 1264 1265/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1266/// This AST node is similar to the conditional operator (?:) in C, with 1267/// the following exceptions: 1268/// - the test expression much be a constant expression. 1269/// - the expression returned has it's type unaltered by promotion rules. 1270/// - does not evaluate the expression that was not chosen. 1271class ChooseExpr : public Expr { 1272 enum { COND, LHS, RHS, END_EXPR }; 1273 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1274 SourceLocation BuiltinLoc, RParenLoc; 1275public: 1276 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1277 SourceLocation RP) 1278 : Expr(ChooseExprClass, t), 1279 BuiltinLoc(BLoc), RParenLoc(RP) { 1280 SubExprs[COND] = cond; 1281 SubExprs[LHS] = lhs; 1282 SubExprs[RHS] = rhs; 1283 } 1284 1285 /// isConditionTrue - Return true if the condition is true. This is always 1286 /// statically knowable for a well-formed choosexpr. 1287 bool isConditionTrue(ASTContext &C) const; 1288 1289 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1290 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1291 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1292 1293 virtual SourceRange getSourceRange() const { 1294 return SourceRange(BuiltinLoc, RParenLoc); 1295 } 1296 static bool classof(const Stmt *T) { 1297 return T->getStmtClass() == ChooseExprClass; 1298 } 1299 static bool classof(const ChooseExpr *) { return true; } 1300 1301 // Iterators 1302 virtual child_iterator child_begin(); 1303 virtual child_iterator child_end(); 1304 1305 virtual void EmitImpl(llvm::Serializer& S) const; 1306 static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1307}; 1308 1309/// OverloadExpr - Clang builtin function __builtin_overload. 1310/// This AST node provides a way to overload functions in C. 1311/// 1312/// The first argument is required to be a constant expression, for the number 1313/// of arguments passed to each candidate function. 1314/// 1315/// The next N arguments, where N is the value of the constant expression, 1316/// are the values to be passed as arguments. 1317/// 1318/// The rest of the arguments are values of pointer to function type, which 1319/// are the candidate functions for overloading. 1320/// 1321/// The result is a equivalent to a CallExpr taking N arguments to the 1322/// candidate function whose parameter types match the types of the N arguments. 1323/// 1324/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl); 1325/// If X and Y are long doubles, Z will assigned the result of modl(X, Y); 1326/// If X and Y are floats, Z will be assigned the result of modf(X, Y); 1327class OverloadExpr : public Expr { 1328 // SubExprs - the list of values passed to the __builtin_overload function. 1329 // SubExpr[0] is a constant expression 1330 // SubExpr[1-N] are the parameters to pass to the matching function call 1331 // SubExpr[N-...] are the candidate functions, of type pointer to function. 1332 Stmt **SubExprs; 1333 1334 // NumExprs - the size of the SubExprs array 1335 unsigned NumExprs; 1336 1337 // The index of the matching candidate function 1338 unsigned FnIndex; 1339 1340 SourceLocation BuiltinLoc; 1341 SourceLocation RParenLoc; 1342public: 1343 OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t, 1344 SourceLocation bloc, SourceLocation rploc) 1345 : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx), 1346 BuiltinLoc(bloc), RParenLoc(rploc) { 1347 SubExprs = new Stmt*[nexprs]; 1348 for (unsigned i = 0; i != nexprs; ++i) 1349 SubExprs[i] = args[i]; 1350 } 1351 ~OverloadExpr() { 1352 delete [] SubExprs; 1353 } 1354 1355 /// arg_begin - Return a pointer to the list of arguments that will be passed 1356 /// to the matching candidate function, skipping over the initial constant 1357 /// expression. 1358 typedef ConstExprIterator const_arg_iterator; 1359 const_arg_iterator arg_begin() const { return &SubExprs[0]+1; } 1360 const_arg_iterator arg_end(ASTContext& Ctx) const { 1361 return &SubExprs[0]+1+getNumArgs(Ctx); 1362 } 1363 1364 /// getNumArgs - Return the number of arguments to pass to the candidate 1365 /// functions. 1366 unsigned getNumArgs(ASTContext &Ctx) const { 1367 return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1368 } 1369 1370 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1371 /// constant expression, the actual arguments passed in, and the function 1372 /// pointers. 1373 unsigned getNumSubExprs() const { return NumExprs; } 1374 1375 /// getExpr - Return the Expr at the specified index. 1376 Expr *getExpr(unsigned Index) const { 1377 assert((Index < NumExprs) && "Arg access out of range!"); 1378 return cast<Expr>(SubExprs[Index]); 1379 } 1380 1381 /// getFn - Return the matching candidate function for this OverloadExpr. 1382 Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); } 1383 1384 virtual SourceRange getSourceRange() const { 1385 return SourceRange(BuiltinLoc, RParenLoc); 1386 } 1387 static bool classof(const Stmt *T) { 1388 return T->getStmtClass() == OverloadExprClass; 1389 } 1390 static bool classof(const OverloadExpr *) { return true; } 1391 1392 // Iterators 1393 virtual child_iterator child_begin(); 1394 virtual child_iterator child_end(); 1395 1396 virtual void EmitImpl(llvm::Serializer& S) const; 1397 static OverloadExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1398}; 1399 1400/// VAArgExpr, used for the builtin function __builtin_va_start. 1401class VAArgExpr : public Expr { 1402 Stmt *Val; 1403 SourceLocation BuiltinLoc, RParenLoc; 1404public: 1405 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1406 : Expr(VAArgExprClass, t), 1407 Val(e), 1408 BuiltinLoc(BLoc), 1409 RParenLoc(RPLoc) { } 1410 1411 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1412 Expr *getSubExpr() { return cast<Expr>(Val); } 1413 virtual SourceRange getSourceRange() const { 1414 return SourceRange(BuiltinLoc, RParenLoc); 1415 } 1416 static bool classof(const Stmt *T) { 1417 return T->getStmtClass() == VAArgExprClass; 1418 } 1419 static bool classof(const VAArgExpr *) { return true; } 1420 1421 // Iterators 1422 virtual child_iterator child_begin(); 1423 virtual child_iterator child_end(); 1424 1425 virtual void EmitImpl(llvm::Serializer& S) const; 1426 static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1427}; 1428 1429/// InitListExpr - used for struct and array initializers, such as: 1430/// struct foo x = { 1, { 2, 3 } }; 1431/// 1432/// Because C is somewhat loose with braces, the AST does not necessarily 1433/// directly model the C source. Instead, the semantic analyzer aims to make 1434/// the InitListExprs match up with the type of the decl being initialized. We 1435/// have the following exceptions: 1436/// 1437/// 1. Elements at the end of the list may be dropped from the initializer. 1438/// These elements are defined to be initialized to zero. For example: 1439/// int x[20] = { 1 }; 1440/// 2. Initializers may have excess initializers which are to be ignored by the 1441/// compiler. For example: 1442/// int x[1] = { 1, 2 }; 1443/// 3. Redundant InitListExprs may be present around scalar elements. These 1444/// always have a single element whose type is the same as the InitListExpr. 1445/// this can only happen for Type::isScalarType() types. 1446/// int x = { 1 }; int y[2] = { {1}, {2} }; 1447/// 1448class InitListExpr : public Expr { 1449 std::vector<Stmt *> InitExprs; 1450 SourceLocation LBraceLoc, RBraceLoc; 1451 1452 /// HadDesignators - Return true if there were any designators in this 1453 /// init list expr. FIXME: this should be replaced by storing the designators 1454 /// somehow and updating codegen. 1455 bool HadDesignators; 1456public: 1457 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1458 SourceLocation rbraceloc, bool HadDesignators); 1459 1460 unsigned getNumInits() const { return InitExprs.size(); } 1461 bool hadDesignators() const { return HadDesignators; } 1462 1463 const Expr* getInit(unsigned Init) const { 1464 assert(Init < getNumInits() && "Initializer access out of range!"); 1465 return cast<Expr>(InitExprs[Init]); 1466 } 1467 1468 Expr* getInit(unsigned Init) { 1469 assert(Init < getNumInits() && "Initializer access out of range!"); 1470 return cast<Expr>(InitExprs[Init]); 1471 } 1472 1473 void setInit(unsigned Init, Expr *expr) { 1474 assert(Init < getNumInits() && "Initializer access out of range!"); 1475 InitExprs[Init] = expr; 1476 } 1477 1478 // Dynamic removal/addition (for constructing implicit InitExpr's). 1479 void removeInit(unsigned Init) { 1480 InitExprs.erase(InitExprs.begin()+Init); 1481 } 1482 void addInit(unsigned Init, Expr *expr) { 1483 InitExprs.insert(InitExprs.begin()+Init, expr); 1484 } 1485 1486 // Explicit InitListExpr's originate from source code (and have valid source 1487 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1488 bool isExplicit() { 1489 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1490 } 1491 1492 virtual SourceRange getSourceRange() const { 1493 return SourceRange(LBraceLoc, RBraceLoc); 1494 } 1495 static bool classof(const Stmt *T) { 1496 return T->getStmtClass() == InitListExprClass; 1497 } 1498 static bool classof(const InitListExpr *) { return true; } 1499 1500 // Iterators 1501 virtual child_iterator child_begin(); 1502 virtual child_iterator child_end(); 1503 1504 typedef std::vector<Stmt *>::iterator iterator; 1505 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 1506 1507 iterator begin() { return InitExprs.begin(); } 1508 iterator end() { return InitExprs.end(); } 1509 reverse_iterator rbegin() { return InitExprs.rbegin(); } 1510 reverse_iterator rend() { return InitExprs.rend(); } 1511 1512 // Serailization. 1513 virtual void EmitImpl(llvm::Serializer& S) const; 1514 static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1515 1516private: 1517 // Used by serializer. 1518 InitListExpr() : Expr(InitListExprClass, QualType()) {} 1519}; 1520 1521//===----------------------------------------------------------------------===// 1522// Clang Extensions 1523//===----------------------------------------------------------------------===// 1524 1525 1526/// ExtVectorElementExpr - This represents access to specific elements of a 1527/// vector, and may occur on the left hand side or right hand side. For example 1528/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 1529/// 1530class ExtVectorElementExpr : public Expr { 1531 Stmt *Base; 1532 IdentifierInfo &Accessor; 1533 SourceLocation AccessorLoc; 1534public: 1535 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 1536 SourceLocation loc) 1537 : Expr(ExtVectorElementExprClass, ty), 1538 Base(base), Accessor(accessor), AccessorLoc(loc) {} 1539 1540 const Expr *getBase() const { return cast<Expr>(Base); } 1541 Expr *getBase() { return cast<Expr>(Base); } 1542 1543 IdentifierInfo &getAccessor() const { return Accessor; } 1544 1545 /// getNumElements - Get the number of components being selected. 1546 unsigned getNumElements() const; 1547 1548 /// containsDuplicateElements - Return true if any element access is 1549 /// repeated. 1550 bool containsDuplicateElements() const; 1551 1552 /// getEncodedElementAccess - Encode the elements accessed into an llvm 1553 /// aggregate Constant of ConstantInt(s). 1554 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 1555 1556 virtual SourceRange getSourceRange() const { 1557 return SourceRange(getBase()->getLocStart(), AccessorLoc); 1558 } 1559 1560 static bool classof(const Stmt *T) { 1561 return T->getStmtClass() == ExtVectorElementExprClass; 1562 } 1563 static bool classof(const ExtVectorElementExpr *) { 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 ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1571}; 1572 1573 1574/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 1575/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1576class BlockExpr : public Expr { 1577protected: 1578 BlockDecl *TheBlock; 1579public: 1580 BlockExpr(BlockDecl *BD, QualType ty) : Expr(BlockExprClass, ty), 1581 TheBlock(BD) {} 1582 1583 BlockDecl *getBlockDecl() { return TheBlock; } 1584 1585 // Convenience functions for probing the underlying BlockDecl. 1586 SourceLocation getCaretLocation() const; 1587 const Stmt *getBody() const; 1588 Stmt *getBody(); 1589 1590 virtual SourceRange getSourceRange() const { 1591 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 1592 } 1593 1594 /// getFunctionType - Return the underlying function type for this block. 1595 const FunctionType *getFunctionType() const; 1596 1597 static bool classof(const Stmt *T) { 1598 return T->getStmtClass() == BlockExprClass; 1599 } 1600 static bool classof(const BlockExpr *) { return true; } 1601 1602 // Iterators 1603 virtual child_iterator child_begin(); 1604 virtual child_iterator child_end(); 1605 1606 virtual void EmitImpl(llvm::Serializer& S) const; 1607 static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1608}; 1609 1610/// BlockDeclRefExpr - A reference to a declared variable, function, 1611/// enum, etc. 1612class BlockDeclRefExpr : public Expr { 1613 ValueDecl *D; 1614 SourceLocation Loc; 1615 bool IsByRef; 1616public: 1617 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 1618 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 1619 1620 ValueDecl *getDecl() { return D; } 1621 const ValueDecl *getDecl() const { return D; } 1622 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 1623 1624 bool isByRef() const { return IsByRef; } 1625 1626 static bool classof(const Stmt *T) { 1627 return T->getStmtClass() == BlockDeclRefExprClass; 1628 } 1629 static bool classof(const BlockDeclRefExpr *) { return true; } 1630 1631 // Iterators 1632 virtual child_iterator child_begin(); 1633 virtual child_iterator child_end(); 1634 1635 virtual void EmitImpl(llvm::Serializer& S) const; 1636 static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1637}; 1638 1639} // end namespace clang 1640 1641#endif 1642