Expr.h revision b107ce82d425939ce32fc3c02faf7c2364abc1a3
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 503 Expr *getLHS() { return SubExprs[LHS]; } 504 const Expr *getLHS() const { return SubExprs[LHS]; } 505 506 Expr *getRHS() { return SubExprs[RHS]; } 507 const Expr *getRHS() const { return SubExprs[RHS]; } 508 509 Expr *getBase() { 510 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 511 } 512 513 const Expr *getBase() const { 514 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 515 } 516 517 Expr *getIdx() { 518 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 519 } 520 521 const Expr *getIdx() const { 522 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 523 } 524 525 526 SourceRange getSourceRange() const { 527 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 528 } 529 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 530 531 static bool classof(const Stmt *T) { 532 return T->getStmtClass() == ArraySubscriptExprClass; 533 } 534 static bool classof(const ArraySubscriptExpr *) { return true; } 535 536 // Iterators 537 virtual child_iterator child_begin(); 538 virtual child_iterator child_end(); 539 540 virtual void EmitImpl(llvm::Serializer& S) const; 541 static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D); 542}; 543 544 545/// CallExpr - [C99 6.5.2.2] Function Calls. 546/// 547class CallExpr : public Expr { 548 enum { FN=0, ARGS_START=1 }; 549 Expr **SubExprs; 550 unsigned NumArgs; 551 SourceLocation RParenLoc; 552 553 // This version of the ctor is for deserialization. 554 CallExpr(Expr** subexprs, unsigned numargs, QualType t, 555 SourceLocation rparenloc) 556 : Expr(CallExprClass,t), SubExprs(subexprs), 557 NumArgs(numargs), RParenLoc(rparenloc) {} 558 559public: 560 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 561 SourceLocation rparenloc); 562 ~CallExpr() { 563 delete [] SubExprs; 564 } 565 566 const Expr *getCallee() const { return SubExprs[FN]; } 567 Expr *getCallee() { return SubExprs[FN]; } 568 569 /// getNumArgs - Return the number of actual arguments to this call. 570 /// 571 unsigned getNumArgs() const { return NumArgs; } 572 573 /// getArg - Return the specified argument. 574 Expr *getArg(unsigned Arg) { 575 assert(Arg < NumArgs && "Arg access out of range!"); 576 return SubExprs[Arg+ARGS_START]; 577 } 578 const Expr *getArg(unsigned Arg) const { 579 assert(Arg < NumArgs && "Arg access out of range!"); 580 return SubExprs[Arg+ARGS_START]; 581 } 582 /// setArg - Set the specified argument. 583 void setArg(unsigned Arg, Expr *ArgExpr) { 584 assert(Arg < NumArgs && "Arg access out of range!"); 585 SubExprs[Arg+ARGS_START] = ArgExpr; 586 } 587 /// getNumCommas - Return the number of commas that must have been present in 588 /// this function call. 589 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 590 591 bool isBuiltinClassifyType(llvm::APSInt &Result) const; 592 593 SourceRange getSourceRange() const { 594 return SourceRange(getCallee()->getLocStart(), RParenLoc); 595 } 596 597 static bool classof(const Stmt *T) { 598 return T->getStmtClass() == CallExprClass; 599 } 600 static bool classof(const CallExpr *) { return true; } 601 602 // Iterators 603 virtual child_iterator child_begin(); 604 virtual child_iterator child_end(); 605 606 virtual void EmitImpl(llvm::Serializer& S) const; 607 static CallExpr* CreateImpl(llvm::Deserializer& D); 608}; 609 610/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 611/// 612class MemberExpr : public Expr { 613 Expr *Base; 614 FieldDecl *MemberDecl; 615 SourceLocation MemberLoc; 616 bool IsArrow; // True if this is "X->F", false if this is "X.F". 617public: 618 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 619 : Expr(MemberExprClass, memberdecl->getType()), 620 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 621 622 Expr *getBase() const { return Base; } 623 FieldDecl *getMemberDecl() const { return MemberDecl; } 624 bool isArrow() const { return IsArrow; } 625 626 virtual SourceRange getSourceRange() const { 627 return SourceRange(getBase()->getLocStart(), MemberLoc); 628 } 629 virtual SourceLocation getExprLoc() const { return MemberLoc; } 630 631 static bool classof(const Stmt *T) { 632 return T->getStmtClass() == MemberExprClass; 633 } 634 static bool classof(const MemberExpr *) { return true; } 635 636 // Iterators 637 virtual child_iterator child_begin(); 638 virtual child_iterator child_end(); 639 640 virtual void EmitImpl(llvm::Serializer& S) const; 641 static MemberExpr* CreateImpl(llvm::Deserializer& D); 642}; 643 644/// OCUVectorElementExpr - This represents access to specific elements of a 645/// vector, and may occur on the left hand side or right hand side. For example 646/// the following is legal: "V.xy = V.zw" if V is a 4 element ocu vector. 647/// 648class OCUVectorElementExpr : public Expr { 649 Expr *Base; 650 IdentifierInfo &Accessor; 651 SourceLocation AccessorLoc; 652public: 653 enum ElementType { 654 Point, // xywz 655 Color, // rgba 656 Texture // stpq 657 }; 658 OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 659 SourceLocation loc) 660 : Expr(OCUVectorElementExprClass, ty), 661 Base(base), Accessor(accessor), AccessorLoc(loc) {} 662 663 const Expr *getBase() const { return Base; } 664 Expr *getBase() { return Base; } 665 666 IdentifierInfo &getAccessor() const { return Accessor; } 667 668 /// getNumElements - Get the number of components being selected. 669 unsigned getNumElements() const; 670 671 /// getElementType - Determine whether the components of this access are 672 /// "point" "color" or "texture" elements. 673 ElementType getElementType() const; 674 675 /// containsDuplicateElements - Return true if any element access is 676 /// repeated. 677 bool containsDuplicateElements() const; 678 679 /// getEncodedElementAccess - Encode the elements accessed into a bit vector. 680 /// The encoding currently uses 2-bit bitfields, but clients should use the 681 /// accessors below to access them. 682 /// 683 unsigned getEncodedElementAccess() const; 684 685 /// getAccessedFieldNo - Given an encoded value and a result number, return 686 /// the input field number being accessed. 687 static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) { 688 return (EncodedVal >> (Idx*2)) & 3; 689 } 690 691 virtual SourceRange getSourceRange() const { 692 return SourceRange(getBase()->getLocStart(), AccessorLoc); 693 } 694 static bool classof(const Stmt *T) { 695 return T->getStmtClass() == OCUVectorElementExprClass; 696 } 697 static bool classof(const OCUVectorElementExpr *) { return true; } 698 699 // Iterators 700 virtual child_iterator child_begin(); 701 virtual child_iterator child_end(); 702}; 703 704/// CompoundLiteralExpr - [C99 6.5.2.5] 705/// 706class CompoundLiteralExpr : public Expr { 707 Expr *Init; 708public: 709 CompoundLiteralExpr(QualType ty, Expr *init) : 710 Expr(CompoundLiteralExprClass, ty), Init(init) {} 711 712 const Expr *getInitializer() const { return Init; } 713 Expr *getInitializer() { return Init; } 714 715 virtual SourceRange getSourceRange() const { 716 if (Init) 717 return Init->getSourceRange(); 718 return SourceRange(); 719 } 720 721 static bool classof(const Stmt *T) { 722 return T->getStmtClass() == CompoundLiteralExprClass; 723 } 724 static bool classof(const CompoundLiteralExpr *) { return true; } 725 726 // Iterators 727 virtual child_iterator child_begin(); 728 virtual child_iterator child_end(); 729 730 virtual void EmitImpl(llvm::Serializer& S) const; 731 static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D); 732}; 733 734/// ImplicitCastExpr - Allows us to explicitly represent implicit type 735/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 736/// float->double, short->int, etc. 737/// 738class ImplicitCastExpr : public Expr { 739 Expr *Op; 740public: 741 ImplicitCastExpr(QualType ty, Expr *op) : 742 Expr(ImplicitCastExprClass, ty), Op(op) {} 743 744 Expr *getSubExpr() { return Op; } 745 const Expr *getSubExpr() const { return Op; } 746 747 virtual SourceRange getSourceRange() const { return Op->getSourceRange(); } 748 749 static bool classof(const Stmt *T) { 750 return T->getStmtClass() == ImplicitCastExprClass; 751 } 752 static bool classof(const ImplicitCastExpr *) { return true; } 753 754 // Iterators 755 virtual child_iterator child_begin(); 756 virtual child_iterator child_end(); 757 758 virtual void EmitImpl(llvm::Serializer& S) const; 759 static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D); 760}; 761 762/// CastExpr - [C99 6.5.4] Cast Operators. 763/// 764class CastExpr : public Expr { 765 Expr *Op; 766 SourceLocation Loc; // the location of the left paren 767public: 768 CastExpr(QualType ty, Expr *op, SourceLocation l) : 769 Expr(CastExprClass, ty), Op(op), Loc(l) {} 770 771 SourceLocation getLParenLoc() const { return Loc; } 772 773 Expr *getSubExpr() const { return Op; } 774 775 virtual SourceRange getSourceRange() const { 776 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 777 } 778 static bool classof(const Stmt *T) { 779 return T->getStmtClass() == CastExprClass; 780 } 781 static bool classof(const CastExpr *) { return true; } 782 783 // Iterators 784 virtual child_iterator child_begin(); 785 virtual child_iterator child_end(); 786 787 virtual void EmitImpl(llvm::Serializer& S) const; 788 static CastExpr* CreateImpl(llvm::Deserializer& D); 789}; 790 791class BinaryOperator : public Expr { 792public: 793 enum Opcode { 794 // Operators listed in order of precedence. 795 // Note that additions to this should also update the StmtVisitor class. 796 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 797 Add, Sub, // [C99 6.5.6] Additive operators. 798 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 799 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 800 EQ, NE, // [C99 6.5.9] Equality operators. 801 And, // [C99 6.5.10] Bitwise AND operator. 802 Xor, // [C99 6.5.11] Bitwise XOR operator. 803 Or, // [C99 6.5.12] Bitwise OR operator. 804 LAnd, // [C99 6.5.13] Logical AND operator. 805 LOr, // [C99 6.5.14] Logical OR operator. 806 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 807 DivAssign, RemAssign, 808 AddAssign, SubAssign, 809 ShlAssign, ShrAssign, 810 AndAssign, XorAssign, 811 OrAssign, 812 Comma // [C99 6.5.17] Comma operator. 813 }; 814private: 815 enum { LHS, RHS, END_EXPR }; 816 Expr* SubExprs[END_EXPR]; 817 Opcode Opc; 818 SourceLocation OpLoc; 819public: 820 821 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 822 SourceLocation opLoc) 823 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 824 SubExprs[LHS] = lhs; 825 SubExprs[RHS] = rhs; 826 assert(!isCompoundAssignmentOp() && 827 "Use ArithAssignBinaryOperator for compound assignments"); 828 } 829 830 SourceLocation getOperatorLoc() const { return OpLoc; } 831 Opcode getOpcode() const { return Opc; } 832 Expr *getLHS() const { return SubExprs[LHS]; } 833 Expr *getRHS() const { return SubExprs[RHS]; } 834 virtual SourceRange getSourceRange() const { 835 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 836 } 837 838 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 839 /// corresponds to, e.g. "<<=". 840 static const char *getOpcodeStr(Opcode Op); 841 842 /// predicates to categorize the respective opcodes. 843 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 844 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 845 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 846 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 847 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 848 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 849 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 850 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 851 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 852 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 853 854 static bool classof(const Stmt *S) { 855 return S->getStmtClass() == BinaryOperatorClass || 856 S->getStmtClass() == CompoundAssignOperatorClass; 857 } 858 static bool classof(const BinaryOperator *) { return true; } 859 860 // Iterators 861 virtual child_iterator child_begin(); 862 virtual child_iterator child_end(); 863 864 virtual void EmitImpl(llvm::Serializer& S) const; 865 static BinaryOperator* CreateImpl(llvm::Deserializer& D); 866 867protected: 868 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 869 SourceLocation oploc, bool dead) 870 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 871 SubExprs[LHS] = lhs; 872 SubExprs[RHS] = rhs; 873 } 874}; 875 876/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 877/// track of the type the operation is performed in. Due to the semantics of 878/// these operators, the operands are promoted, the aritmetic performed, an 879/// implicit conversion back to the result type done, then the assignment takes 880/// place. This captures the intermediate type which the computation is done 881/// in. 882class CompoundAssignOperator : public BinaryOperator { 883 QualType ComputationType; 884public: 885 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 886 QualType ResType, QualType CompType, 887 SourceLocation OpLoc) 888 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 889 ComputationType(CompType) { 890 assert(isCompoundAssignmentOp() && 891 "Only should be used for compound assignments"); 892 } 893 894 QualType getComputationType() const { return ComputationType; } 895 896 static bool classof(const CompoundAssignOperator *) { return true; } 897 static bool classof(const Stmt *S) { 898 return S->getStmtClass() == CompoundAssignOperatorClass; 899 } 900 901 virtual void EmitImpl(llvm::Serializer& S) const; 902 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D); 903}; 904 905/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 906/// GNU "missing LHS" extension is in use. 907/// 908class ConditionalOperator : public Expr { 909 enum { COND, LHS, RHS, END_EXPR }; 910 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 911public: 912 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 913 : Expr(ConditionalOperatorClass, t) { 914 SubExprs[COND] = cond; 915 SubExprs[LHS] = lhs; 916 SubExprs[RHS] = rhs; 917 } 918 919 // getCond - Return the expression representing the condition for 920 // the ?: operator. 921 Expr *getCond() const { return SubExprs[COND]; } 922 923 // getTrueExpr - Return the subexpression representing the value of the ?: 924 // expression if the condition evaluates to true. In most cases this value 925 // will be the same as getLHS() except a GCC extension allows the left 926 // subexpression to be omitted, and instead of the condition be returned. 927 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 928 // is only evaluated once. 929 Expr *getTrueExpr() const { 930 return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS]; 931 } 932 933 // getTrueExpr - Return the subexpression representing the value of the ?: 934 // expression if the condition evaluates to false. This is the same as getRHS. 935 Expr *getFalseExpr() const { return SubExprs[RHS]; } 936 937 Expr *getLHS() const { return SubExprs[LHS]; } 938 Expr *getRHS() const { return SubExprs[RHS]; } 939 940 virtual SourceRange getSourceRange() const { 941 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 942 } 943 static bool classof(const Stmt *T) { 944 return T->getStmtClass() == ConditionalOperatorClass; 945 } 946 static bool classof(const ConditionalOperator *) { return true; } 947 948 // Iterators 949 virtual child_iterator child_begin(); 950 virtual child_iterator child_end(); 951 952 virtual void EmitImpl(llvm::Serializer& S) const; 953 static ConditionalOperator* CreateImpl(llvm::Deserializer& D); 954}; 955 956/// AddrLabelExpr - The GNU address of label extension, representing &&label. 957class AddrLabelExpr : public Expr { 958 SourceLocation AmpAmpLoc, LabelLoc; 959 LabelStmt *Label; 960public: 961 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 962 QualType t) 963 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 964 965 virtual SourceRange getSourceRange() const { 966 return SourceRange(AmpAmpLoc, LabelLoc); 967 } 968 969 LabelStmt *getLabel() const { return Label; } 970 971 static bool classof(const Stmt *T) { 972 return T->getStmtClass() == AddrLabelExprClass; 973 } 974 static bool classof(const AddrLabelExpr *) { return true; } 975 976 // Iterators 977 virtual child_iterator child_begin(); 978 virtual child_iterator child_end(); 979 980 virtual void EmitImpl(llvm::Serializer& S) const; 981 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D); 982}; 983 984/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 985/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 986/// takes the value of the last subexpression. 987class StmtExpr : public Expr { 988 CompoundStmt *SubStmt; 989 SourceLocation LParenLoc, RParenLoc; 990public: 991 StmtExpr(CompoundStmt *substmt, QualType T, 992 SourceLocation lp, SourceLocation rp) : 993 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 994 995 CompoundStmt *getSubStmt() { return SubStmt; } 996 const CompoundStmt *getSubStmt() const { return SubStmt; } 997 998 virtual SourceRange getSourceRange() const { 999 return SourceRange(LParenLoc, RParenLoc); 1000 } 1001 1002 static bool classof(const Stmt *T) { 1003 return T->getStmtClass() == StmtExprClass; 1004 } 1005 static bool classof(const StmtExpr *) { return true; } 1006 1007 // Iterators 1008 virtual child_iterator child_begin(); 1009 virtual child_iterator child_end(); 1010 1011 virtual void EmitImpl(llvm::Serializer& S) const; 1012 static StmtExpr* CreateImpl(llvm::Deserializer& D); 1013}; 1014 1015/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1016/// This AST node represents a function that returns 1 if two *types* (not 1017/// expressions) are compatible. The result of this built-in function can be 1018/// used in integer constant expressions. 1019class TypesCompatibleExpr : public Expr { 1020 QualType Type1; 1021 QualType Type2; 1022 SourceLocation BuiltinLoc, RParenLoc; 1023public: 1024 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1025 QualType t1, QualType t2, SourceLocation RP) : 1026 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1027 BuiltinLoc(BLoc), RParenLoc(RP) {} 1028 1029 QualType getArgType1() const { return Type1; } 1030 QualType getArgType2() const { return Type2; } 1031 1032 virtual SourceRange getSourceRange() const { 1033 return SourceRange(BuiltinLoc, RParenLoc); 1034 } 1035 static bool classof(const Stmt *T) { 1036 return T->getStmtClass() == TypesCompatibleExprClass; 1037 } 1038 static bool classof(const TypesCompatibleExpr *) { return true; } 1039 1040 // Iterators 1041 virtual child_iterator child_begin(); 1042 virtual child_iterator child_end(); 1043}; 1044 1045/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1046/// This AST node is similar to the conditional operator (?:) in C, with 1047/// the following exceptions: 1048/// - the test expression much be a constant expression. 1049/// - the expression returned has it's type unaltered by promotion rules. 1050/// - does not evaluate the expression that was not chosen. 1051class ChooseExpr : public Expr { 1052 enum { COND, LHS, RHS, END_EXPR }; 1053 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1054 SourceLocation BuiltinLoc, RParenLoc; 1055public: 1056 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1057 SourceLocation RP) 1058 : Expr(ChooseExprClass, t), 1059 BuiltinLoc(BLoc), RParenLoc(RP) { 1060 SubExprs[COND] = cond; 1061 SubExprs[LHS] = lhs; 1062 SubExprs[RHS] = rhs; 1063 } 1064 1065 /// isConditionTrue - Return true if the condition is true. This is always 1066 /// statically knowable for a well-formed choosexpr. 1067 bool isConditionTrue(ASTContext &C) const; 1068 1069 Expr *getCond() const { return SubExprs[COND]; } 1070 Expr *getLHS() const { return SubExprs[LHS]; } 1071 Expr *getRHS() const { return SubExprs[RHS]; } 1072 1073 virtual SourceRange getSourceRange() const { 1074 return SourceRange(BuiltinLoc, RParenLoc); 1075 } 1076 static bool classof(const Stmt *T) { 1077 return T->getStmtClass() == ChooseExprClass; 1078 } 1079 static bool classof(const ChooseExpr *) { return true; } 1080 1081 // Iterators 1082 virtual child_iterator child_begin(); 1083 virtual child_iterator child_end(); 1084}; 1085 1086/// VAArgExpr, used for the builtin function __builtin_va_start. 1087class VAArgExpr : public Expr { 1088 Expr *Val; 1089 SourceLocation BuiltinLoc, RParenLoc; 1090public: 1091 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1092 : Expr(VAArgExprClass, t), 1093 Val(e), 1094 BuiltinLoc(BLoc), 1095 RParenLoc(RPLoc) { } 1096 1097 const Expr *getSubExpr() const { return Val; } 1098 Expr *getSubExpr() { return Val; } 1099 virtual SourceRange getSourceRange() const { 1100 return SourceRange(BuiltinLoc, RParenLoc); 1101 } 1102 static bool classof(const Stmt *T) { 1103 return T->getStmtClass() == VAArgExprClass; 1104 } 1105 static bool classof(const VAArgExpr *) { return true; } 1106 1107 // Iterators 1108 virtual child_iterator child_begin(); 1109 virtual child_iterator child_end(); 1110}; 1111 1112/// InitListExpr, used for struct and array initializers. 1113class InitListExpr : public Expr { 1114 Expr **InitExprs; 1115 unsigned NumInits; 1116 SourceLocation LBraceLoc, RBraceLoc; 1117public: 1118 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1119 SourceLocation rbraceloc); 1120 ~InitListExpr() { 1121 delete [] InitExprs; 1122 } 1123 1124 unsigned getNumInits() const { return NumInits; } 1125 1126 const Expr* getInit(unsigned Init) const { 1127 assert(Init < NumInits && "Initializer access out of range!"); 1128 return InitExprs[Init]; 1129 } 1130 1131 Expr* getInit(unsigned Init) { 1132 assert(Init < NumInits && "Initializer access out of range!"); 1133 return InitExprs[Init]; 1134 } 1135 1136 void setInit(unsigned Init, Expr *expr) { 1137 assert(Init < NumInits && "Initializer access out of range!"); 1138 InitExprs[Init] = expr; 1139 } 1140 1141 virtual SourceRange getSourceRange() const { 1142 return SourceRange(LBraceLoc, RBraceLoc); 1143 } 1144 static bool classof(const Stmt *T) { 1145 return T->getStmtClass() == InitListExprClass; 1146 } 1147 static bool classof(const InitListExpr *) { return true; } 1148 1149 // Iterators 1150 virtual child_iterator child_begin(); 1151 virtual child_iterator child_end(); 1152 1153 virtual void EmitImpl(llvm::Serializer& S) const; 1154 static InitListExpr* CreateImpl(llvm::Deserializer& D); 1155 1156private: 1157 // Used by serializer. 1158 InitListExpr() : Expr(InitListExprClass, QualType()), 1159 InitExprs(NULL), NumInits(0) {} 1160}; 1161 1162/// ObjCStringLiteral, used for Objective-C string literals 1163/// i.e. @"foo". 1164class ObjCStringLiteral : public Expr { 1165 StringLiteral *String; 1166 SourceLocation AtLoc; 1167public: 1168 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 1169 : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {} 1170 1171 StringLiteral* getString() { return String; } 1172 1173 const StringLiteral* getString() const { return String; } 1174 1175 virtual SourceRange getSourceRange() const { 1176 return SourceRange(AtLoc, String->getLocEnd()); 1177 } 1178 1179 static bool classof(const Stmt *T) { 1180 return T->getStmtClass() == ObjCStringLiteralClass; 1181 } 1182 static bool classof(const ObjCStringLiteral *) { return true; } 1183 1184 // Iterators 1185 virtual child_iterator child_begin(); 1186 virtual child_iterator child_end(); 1187 1188 virtual void EmitImpl(llvm::Serializer& S) const; 1189 static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D); 1190}; 1191 1192/// ObjCEncodeExpr, used for @encode in Objective-C. 1193class ObjCEncodeExpr : public Expr { 1194 QualType EncType; 1195 SourceLocation AtLoc, RParenLoc; 1196public: 1197 ObjCEncodeExpr(QualType T, QualType ET, 1198 SourceLocation at, SourceLocation rp) 1199 : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {} 1200 1201 SourceLocation getAtLoc() const { return AtLoc; } 1202 SourceLocation getRParenLoc() const { return RParenLoc; } 1203 1204 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1205 1206 QualType getEncodedType() const { return EncType; } 1207 1208 static bool classof(const Stmt *T) { 1209 return T->getStmtClass() == ObjCEncodeExprClass; 1210 } 1211 static bool classof(const ObjCEncodeExpr *) { return true; } 1212 1213 // Iterators 1214 virtual child_iterator child_begin(); 1215 virtual child_iterator child_end(); 1216}; 1217 1218/// ObjCSelectorExpr used for @selector in Objective-C. 1219class ObjCSelectorExpr : public Expr { 1220 1221 Selector SelName; 1222 1223 SourceLocation AtLoc, RParenLoc; 1224public: 1225 ObjCSelectorExpr(QualType T, Selector selInfo, 1226 SourceLocation at, SourceLocation rp) 1227 : Expr(ObjCSelectorExprClass, T), SelName(selInfo), 1228 AtLoc(at), RParenLoc(rp) {} 1229 1230 const Selector &getSelector() const { return SelName; } 1231 Selector &getSelector() { return SelName; } 1232 1233 SourceLocation getAtLoc() const { return AtLoc; } 1234 SourceLocation getRParenLoc() const { return RParenLoc; } 1235 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1236 1237 /// getNumArgs - Return the number of actual arguments to this call. 1238 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1239 1240 static bool classof(const Stmt *T) { 1241 return T->getStmtClass() == ObjCSelectorExprClass; 1242 } 1243 static bool classof(const ObjCSelectorExpr *) { return true; } 1244 1245 // Iterators 1246 virtual child_iterator child_begin(); 1247 virtual child_iterator child_end(); 1248 1249}; 1250 1251/// ObjCProtocolExpr used for protocol in Objective-C. 1252class ObjCProtocolExpr : public Expr { 1253 1254 ObjcProtocolDecl *Protocol; 1255 1256 SourceLocation AtLoc, RParenLoc; 1257 public: 1258 ObjCProtocolExpr(QualType T, ObjcProtocolDecl *protocol, 1259 SourceLocation at, SourceLocation rp) 1260 : Expr(ObjCProtocolExprClass, T), Protocol(protocol), 1261 AtLoc(at), RParenLoc(rp) {} 1262 1263 ObjcProtocolDecl *getProtocol() const { return Protocol; } 1264 1265 SourceLocation getAtLoc() const { return AtLoc; } 1266 SourceLocation getRParenLoc() const { return RParenLoc; } 1267 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1268 1269 static bool classof(const Stmt *T) { 1270 return T->getStmtClass() == ObjCProtocolExprClass; 1271 } 1272 static bool classof(const ObjCProtocolExpr *) { return true; } 1273 1274 // Iterators 1275 virtual child_iterator child_begin(); 1276 virtual child_iterator child_end(); 1277 1278}; 1279 1280/// ObjCIvarRefExpr - A reference to an ObjC instance variable. 1281class ObjCIvarRefExpr : public Expr { 1282 class ObjcIvarDecl *D; 1283 SourceLocation Loc; 1284 Expr *Base; 1285 bool IsArrow:1; // True if this is "X->F", false if this is "X.F". 1286 bool IsFreeIvar:1; // True if ivar reference has no base (self assumed). 1287 1288public: 1289 ObjCIvarRefExpr(ObjcIvarDecl *d, QualType t, SourceLocation l, Expr *base=0, 1290 bool arrow = false, bool freeIvar = false) : 1291 Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow), 1292 IsFreeIvar(freeIvar) {} 1293 1294 ObjcIvarDecl *getDecl() { return D; } 1295 const ObjcIvarDecl *getDecl() const { return D; } 1296 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 1297 Expr *const getBase() const { return Base; } 1298 const bool isArrow() const { return IsArrow; } 1299 const bool isFreeIvar() const { return IsFreeIvar; } 1300 1301 SourceLocation getLocation() const { return Loc; } 1302 1303 static bool classof(const Stmt *T) { 1304 return T->getStmtClass() == ObjCIvarRefExprClass; 1305 } 1306 static bool classof(const ObjCIvarRefExpr *) { return true; } 1307 1308 // Iterators 1309 virtual child_iterator child_begin(); 1310 virtual child_iterator child_end(); 1311 1312 virtual void EmitImpl(llvm::Serializer& S) const; 1313 static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D); 1314}; 1315 1316class ObjCMessageExpr : public Expr { 1317 enum { RECEIVER=0, ARGS_START=1 }; 1318 1319 Expr **SubExprs; 1320 1321 unsigned NumArgs; 1322 1323 // A unigue name for this message. 1324 Selector SelName; 1325 1326 // A method prototype for this message (optional). 1327 // FIXME: Since method decls contain the selector, and most messages have a 1328 // prototype, consider devising a scheme for unifying SelName/MethodProto. 1329 ObjcMethodDecl *MethodProto; 1330 1331 IdentifierInfo *ClassName; // optional - 0 for instance messages. 1332 1333 SourceLocation LBracloc, RBracloc; 1334public: 1335 // constructor for class messages. 1336 // FIXME: clsName should be typed to ObjCInterfaceType 1337 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1338 QualType retType, ObjcMethodDecl *methDecl, 1339 SourceLocation LBrac, SourceLocation RBrac, 1340 Expr **ArgExprs, unsigned NumArgs); 1341 // constructor for instance messages. 1342 ObjCMessageExpr(Expr *receiver, Selector selInfo, 1343 QualType retType, ObjcMethodDecl *methDecl, 1344 SourceLocation LBrac, SourceLocation RBrac, 1345 Expr **ArgExprs, unsigned NumArgs); 1346 ~ObjCMessageExpr() { 1347 delete [] SubExprs; 1348 } 1349 1350 const Expr *getReceiver() const { return SubExprs[RECEIVER]; } 1351 Expr *getReceiver() { return SubExprs[RECEIVER]; } 1352 1353 const Selector &getSelector() const { return SelName; } 1354 Selector &getSelector() { return SelName; } 1355 1356 const ObjcMethodDecl *getMethodDecl() const { return MethodProto; } 1357 ObjcMethodDecl *getMethodDecl() { return MethodProto; } 1358 1359 const IdentifierInfo *getClassName() const { return ClassName; } 1360 IdentifierInfo *getClassName() { return ClassName; } 1361 1362 /// getNumArgs - Return the number of actual arguments to this call. 1363 unsigned getNumArgs() const { return NumArgs; } 1364 1365/// getArg - Return the specified argument. 1366 Expr *getArg(unsigned Arg) { 1367 assert(Arg < NumArgs && "Arg access out of range!"); 1368 return SubExprs[Arg+ARGS_START]; 1369 } 1370 const Expr *getArg(unsigned Arg) const { 1371 assert(Arg < NumArgs && "Arg access out of range!"); 1372 return SubExprs[Arg+ARGS_START]; 1373 } 1374 /// setArg - Set the specified argument. 1375 void setArg(unsigned Arg, Expr *ArgExpr) { 1376 assert(Arg < NumArgs && "Arg access out of range!"); 1377 SubExprs[Arg+ARGS_START] = ArgExpr; 1378 } 1379 SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); } 1380 1381 static bool classof(const Stmt *T) { 1382 return T->getStmtClass() == ObjCMessageExprClass; 1383 } 1384 static bool classof(const ObjCMessageExpr *) { return true; } 1385 1386 // Iterators 1387 virtual child_iterator child_begin(); 1388 virtual child_iterator child_end(); 1389}; 1390 1391} // end namespace clang 1392 1393#endif 1394