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