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