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