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