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