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