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