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