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