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