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