Expr.h revision 4c67834407ca6ab344dcf44fc599ad4938cfa96d
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/APValue.h" 18#include "clang/AST/Stmt.h" 19#include "clang/AST/Type.h" 20#include "llvm/ADT/APSInt.h" 21#include "llvm/ADT/APFloat.h" 22#include "llvm/ADT/SmallVector.h" 23#include <vector> 24 25namespace clang { 26 class ASTContext; 27 class APValue; 28 class Decl; 29 class IdentifierInfo; 30 class ParmVarDecl; 31 class NamedDecl; 32 class ValueDecl; 33 class BlockDecl; 34 class CXXOperatorCallExpr; 35 class CXXMemberCallExpr; 36 37/// Expr - This represents one expression. Note that Expr's are subclasses of 38/// Stmt. This allows an expression to be transparently used any place a Stmt 39/// is required. 40/// 41class Expr : public Stmt { 42 QualType TR; 43 44 /// TypeDependent - Whether this expression is type-dependent 45 /// (C++ [temp.dep.expr]). 46 bool TypeDependent : 1; 47 48 /// ValueDependent - Whether this expression is value-dependent 49 /// (C++ [temp.dep.constexpr]). 50 bool ValueDependent : 1; 51 52protected: 53 // FIXME: Eventually, this constructor should go away and we should 54 // require every subclass to provide type/value-dependence 55 // information. 56 Expr(StmtClass SC, QualType T) 57 : Stmt(SC), TypeDependent(false), ValueDependent(false) { 58 setType(T); 59 } 60 61 Expr(StmtClass SC, QualType T, bool TD, bool VD) 62 : Stmt(SC), TypeDependent(TD), ValueDependent(VD) { 63 setType(T); 64 } 65 66public: 67 QualType getType() const { return TR; } 68 void setType(QualType t) { 69 // In C++, the type of an expression is always adjusted so that it 70 // will not have reference type an expression will never have 71 // reference type (C++ [expr]p6). Use 72 // QualType::getNonReferenceType() to retrieve the non-reference 73 // type. Additionally, inspect Expr::isLvalue to determine whether 74 // an expression that is adjusted in this manner should be 75 // considered an lvalue. 76 assert((TR.isNull() || !TR->isReferenceType()) && 77 "Expressions can't have reference type"); 78 79 TR = t; 80 } 81 82 /// isValueDependent - Determines whether this expression is 83 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 84 /// array bound of "Chars" in the following example is 85 /// value-dependent. 86 /// @code 87 /// template<int Size, char (&Chars)[Size]> struct meta_string; 88 /// @endcode 89 bool isValueDependent() const { return ValueDependent; } 90 91 /// isTypeDependent - Determines whether this expression is 92 /// type-dependent (C++ [temp.dep.expr]), which means that its type 93 /// could change from one template instantiation to the next. For 94 /// example, the expressions "x" and "x + y" are type-dependent in 95 /// the following code, but "y" is not type-dependent: 96 /// @code 97 /// template<typename T> 98 /// void add(T x, int y) { 99 /// x + y; 100 /// } 101 /// @endcode 102 bool isTypeDependent() const { return TypeDependent; } 103 104 /// SourceLocation tokens are not useful in isolation - they are low level 105 /// value objects created/interpreted by SourceManager. We assume AST 106 /// clients will have a pointer to the respective SourceManager. 107 virtual SourceRange getSourceRange() const = 0; 108 109 /// getExprLoc - Return the preferred location for the arrow when diagnosing 110 /// a problem with a generic expression. 111 virtual SourceLocation getExprLoc() const { return getLocStart(); } 112 113 /// hasLocalSideEffect - Return true if this immediate expression has side 114 /// effects, not counting any sub-expressions. 115 bool hasLocalSideEffect() const; 116 117 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 118 /// incomplete type other than void. Nonarray expressions that can be lvalues: 119 /// - name, where name must be a variable 120 /// - e[i] 121 /// - (e), where e must be an lvalue 122 /// - e.name, where e must be an lvalue 123 /// - e->name 124 /// - *e, the type of e cannot be a function type 125 /// - string-constant 126 /// - reference type [C++ [expr]] 127 /// 128 enum isLvalueResult { 129 LV_Valid, 130 LV_NotObjectType, 131 LV_IncompleteVoidType, 132 LV_DuplicateVectorComponents, 133 LV_InvalidExpression, 134 LV_MemberFunction 135 }; 136 isLvalueResult isLvalue(ASTContext &Ctx) const; 137 138 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 139 /// does not have an incomplete type, does not have a const-qualified type, 140 /// and if it is a structure or union, does not have any member (including, 141 /// recursively, any member or element of all contained aggregates or unions) 142 /// with a const-qualified type. 143 enum isModifiableLvalueResult { 144 MLV_Valid, 145 MLV_NotObjectType, 146 MLV_IncompleteVoidType, 147 MLV_DuplicateVectorComponents, 148 MLV_InvalidExpression, 149 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 150 MLV_IncompleteType, 151 MLV_ConstQualified, 152 MLV_ArrayType, 153 MLV_NotBlockQualified, 154 MLV_ReadonlyProperty, 155 MLV_NoSetterProperty, 156 MLV_MemberFunction 157 }; 158 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const; 159 160 bool isBitField(); 161 162 /// getIntegerConstantExprValue() - Return the value of an integer 163 /// constant expression. The expression must be a valid integer 164 /// constant expression as determined by isIntegerConstantExpr. 165 llvm::APSInt getIntegerConstantExprValue(ASTContext &Ctx) const { 166 llvm::APSInt X; 167 bool success = isIntegerConstantExpr(X, Ctx); 168 success = success; 169 assert(success && "Illegal argument to getIntegerConstantExpr"); 170 return X; 171 } 172 173 /// isIntegerConstantExpr - Return true if this expression is a valid integer 174 /// constant expression, and, if so, return its value in Result. If not a 175 /// valid i-c-e, return false and fill in Loc (if specified) with the location 176 /// of the invalid expression. 177 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 178 SourceLocation *Loc = 0, 179 bool isEvaluated = true) const; 180 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 181 llvm::APSInt X; 182 return isIntegerConstantExpr(X, Ctx, Loc); 183 } 184 /// isConstantInitializer - Returns true if this expression is a constant 185 /// initializer, which can be emitted at compile-time. 186 bool isConstantInitializer(ASTContext &Ctx) const; 187 188 /// @brief Determines whether this expression (or any of its 189 /// subexpressions) has side effects. 190 bool hasSideEffects(ASTContext &Ctx) const; 191 192 /// EvalResult is a struct with detailed info about an evaluated expression. 193 struct EvalResult { 194 /// Val - This is the scalar value the expression can be folded to. 195 APValue Val; 196 197 /// HasSideEffects - Whether the evaluated expression has side effects. 198 /// For example, (f() && 0) can be folded, but it still has side effects. 199 bool HasSideEffects; 200 201 /// Diag - If the expression is unfoldable, then Diag contains a note 202 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 203 /// position for the error, and DiagExpr is the expression that caused 204 /// the error. 205 /// If the expression is foldable, but not an integer constant expression, 206 /// Diag contains a note diagnostic that describes why it isn't an integer 207 /// constant expression. If the expression *is* an integer constant 208 /// expression, then Diag will be zero. 209 unsigned Diag; 210 const Expr *DiagExpr; 211 SourceLocation DiagLoc; 212 213 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 214 }; 215 216 /// Evaluate - Return true if this is a constant which we can fold using 217 /// any crazy technique (that has nothing to do with language standards) that 218 /// we want to. If this function returns true, it returns the folded constant 219 /// in Result. 220 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const; 221 222 /// isEvaluatable - Call Evaluate to see if this expression can be constant 223 /// folded, but discard the result. 224 bool isEvaluatable(ASTContext &Ctx) const; 225 226 /// EvaluateAsInt - Call Evaluate and return the folded integer. This 227 /// must be called on an expression that constant folds to an integer. 228 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const; 229 230 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 231 /// integer constant expression with the value zero, or if this is one that is 232 /// cast to void*. 233 bool isNullPointerConstant(ASTContext &Ctx) const; 234 235 /// hasGlobalStorage - Return true if this expression has static storage 236 /// duration. This means that the address of this expression is a link-time 237 /// constant. 238 bool hasGlobalStorage() const; 239 240 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 241 /// its subexpression. If that subexpression is also a ParenExpr, 242 /// then this method recursively returns its subexpression, and so forth. 243 /// Otherwise, the method returns the current Expr. 244 Expr* IgnoreParens(); 245 246 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 247 /// or CastExprs, returning their operand. 248 Expr *IgnoreParenCasts(); 249 250 const Expr* IgnoreParens() const { 251 return const_cast<Expr*>(this)->IgnoreParens(); 252 } 253 const Expr *IgnoreParenCasts() const { 254 return const_cast<Expr*>(this)->IgnoreParenCasts(); 255 } 256 257 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 258 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 259 260 static bool classof(const Stmt *T) { 261 return T->getStmtClass() >= firstExprConstant && 262 T->getStmtClass() <= lastExprConstant; 263 } 264 static bool classof(const Expr *) { return true; } 265 266 static inline Expr* Create(llvm::Deserializer& D, ASTContext& C) { 267 return cast<Expr>(Stmt::Create(D, C)); 268 } 269}; 270 271 272//===----------------------------------------------------------------------===// 273// Primary Expressions. 274//===----------------------------------------------------------------------===// 275 276/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 277/// enum, etc. 278class DeclRefExpr : public Expr { 279 NamedDecl *D; 280 SourceLocation Loc; 281 282protected: 283 // FIXME: Eventually, this constructor will go away and all subclasses 284 // will have to provide the type- and value-dependent flags. 285 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) : 286 Expr(SC, t), D(d), Loc(l) {} 287 288 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l, bool TD, 289 bool VD) : 290 Expr(SC, t, TD, VD), D(d), Loc(l) {} 291 292public: 293 // FIXME: Eventually, this constructor will go away and all clients 294 // will have to provide the type- and value-dependent flags. 295 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) : 296 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 297 298 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) : 299 Expr(DeclRefExprClass, t, TD, VD), D(d), Loc(l) {} 300 301 NamedDecl *getDecl() { return D; } 302 const NamedDecl *getDecl() const { return D; } 303 void setDecl(NamedDecl *NewD) { D = NewD; } 304 305 SourceLocation getLocation() const { return Loc; } 306 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 307 308 static bool classof(const Stmt *T) { 309 return T->getStmtClass() == DeclRefExprClass || 310 T->getStmtClass() == CXXConditionDeclExprClass || 311 T->getStmtClass() == QualifiedDeclRefExprClass; 312 } 313 static bool classof(const DeclRefExpr *) { return true; } 314 315 // Iterators 316 virtual child_iterator child_begin(); 317 virtual child_iterator child_end(); 318 319 virtual void EmitImpl(llvm::Serializer& S) const; 320 static DeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 321}; 322 323/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 324class PredefinedExpr : public Expr { 325public: 326 enum IdentType { 327 Func, 328 Function, 329 PrettyFunction 330 }; 331 332private: 333 SourceLocation Loc; 334 IdentType Type; 335public: 336 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 337 : Expr(PredefinedExprClass, type), Loc(l), Type(IT) {} 338 339 IdentType getIdentType() const { return Type; } 340 341 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 342 343 static bool classof(const Stmt *T) { 344 return T->getStmtClass() == PredefinedExprClass; 345 } 346 static bool classof(const PredefinedExpr *) { return true; } 347 348 // Iterators 349 virtual child_iterator child_begin(); 350 virtual child_iterator child_end(); 351 352 virtual void EmitImpl(llvm::Serializer& S) const; 353 static PredefinedExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 354}; 355 356class IntegerLiteral : public Expr { 357 llvm::APInt Value; 358 SourceLocation Loc; 359public: 360 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 361 // or UnsignedLongLongTy 362 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 363 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 364 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 365 } 366 const llvm::APInt &getValue() const { return Value; } 367 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 368 369 static bool classof(const Stmt *T) { 370 return T->getStmtClass() == IntegerLiteralClass; 371 } 372 static bool classof(const IntegerLiteral *) { return true; } 373 374 // Iterators 375 virtual child_iterator child_begin(); 376 virtual child_iterator child_end(); 377 378 virtual void EmitImpl(llvm::Serializer& S) const; 379 static IntegerLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 380}; 381 382class CharacterLiteral : public Expr { 383 unsigned Value; 384 SourceLocation Loc; 385 bool IsWide; 386public: 387 // type should be IntTy 388 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 389 : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) { 390 } 391 SourceLocation getLoc() const { return Loc; } 392 bool isWide() const { return IsWide; } 393 394 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 395 396 unsigned getValue() const { return Value; } 397 398 static bool classof(const Stmt *T) { 399 return T->getStmtClass() == CharacterLiteralClass; 400 } 401 static bool classof(const CharacterLiteral *) { return true; } 402 403 // Iterators 404 virtual child_iterator child_begin(); 405 virtual child_iterator child_end(); 406 407 virtual void EmitImpl(llvm::Serializer& S) const; 408 static CharacterLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 409}; 410 411class FloatingLiteral : public Expr { 412 llvm::APFloat Value; 413 bool IsExact : 1; 414 SourceLocation Loc; 415public: 416 FloatingLiteral(const llvm::APFloat &V, bool* isexact, 417 QualType Type, SourceLocation L) 418 : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {} 419 420 const llvm::APFloat &getValue() const { return Value; } 421 422 bool isExact() const { return IsExact; } 423 424 /// getValueAsApproximateDouble - This returns the value as an inaccurate 425 /// double. Note that this may cause loss of precision, but is useful for 426 /// debugging dumps, etc. 427 double getValueAsApproximateDouble() const; 428 429 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 430 431 static bool classof(const Stmt *T) { 432 return T->getStmtClass() == FloatingLiteralClass; 433 } 434 static bool classof(const FloatingLiteral *) { return true; } 435 436 // Iterators 437 virtual child_iterator child_begin(); 438 virtual child_iterator child_end(); 439 440 virtual void EmitImpl(llvm::Serializer& S) const; 441 static FloatingLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 442}; 443 444/// ImaginaryLiteral - We support imaginary integer and floating point literals, 445/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 446/// IntegerLiteral classes. Instances of this class always have a Complex type 447/// whose element type matches the subexpression. 448/// 449class ImaginaryLiteral : public Expr { 450 Stmt *Val; 451public: 452 ImaginaryLiteral(Expr *val, QualType Ty) 453 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 454 455 const Expr *getSubExpr() const { return cast<Expr>(Val); } 456 Expr *getSubExpr() { return cast<Expr>(Val); } 457 458 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 459 static bool classof(const Stmt *T) { 460 return T->getStmtClass() == ImaginaryLiteralClass; 461 } 462 static bool classof(const ImaginaryLiteral *) { return true; } 463 464 // Iterators 465 virtual child_iterator child_begin(); 466 virtual child_iterator child_end(); 467 468 virtual void EmitImpl(llvm::Serializer& S) const; 469 static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 470}; 471 472/// StringLiteral - This represents a string literal expression, e.g. "foo" 473/// or L"bar" (wide strings). The actual string is returned by getStrData() 474/// is NOT null-terminated, and the length of the string is determined by 475/// calling getByteLength(). The C type for a string is always a 476/// ConstantArrayType. 477class StringLiteral : public Expr { 478 const char *StrData; 479 unsigned ByteLength; 480 bool IsWide; 481 // if the StringLiteral was composed using token pasting, both locations 482 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 483 // FIXME: if space becomes an issue, we should create a sub-class. 484 SourceLocation firstTokLoc, lastTokLoc; 485public: 486 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 487 QualType t, SourceLocation b, SourceLocation e); 488 virtual ~StringLiteral(); 489 490 const char *getStrData() const { return StrData; } 491 unsigned getByteLength() const { return ByteLength; } 492 bool isWide() const { return IsWide; } 493 494 virtual SourceRange getSourceRange() const { 495 return SourceRange(firstTokLoc,lastTokLoc); 496 } 497 static bool classof(const Stmt *T) { 498 return T->getStmtClass() == StringLiteralClass; 499 } 500 static bool classof(const StringLiteral *) { return true; } 501 502 // Iterators 503 virtual child_iterator child_begin(); 504 virtual child_iterator child_end(); 505 506 virtual void EmitImpl(llvm::Serializer& S) const; 507 static StringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 508}; 509 510/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 511/// AST node is only formed if full location information is requested. 512class ParenExpr : public Expr { 513 SourceLocation L, R; 514 Stmt *Val; 515public: 516 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 517 : Expr(ParenExprClass, val->getType(), 518 val->isTypeDependent(), val->isValueDependent()), 519 L(l), R(r), Val(val) {} 520 521 const Expr *getSubExpr() const { return cast<Expr>(Val); } 522 Expr *getSubExpr() { return cast<Expr>(Val); } 523 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 524 525 static bool classof(const Stmt *T) { 526 return T->getStmtClass() == ParenExprClass; 527 } 528 static bool classof(const ParenExpr *) { return true; } 529 530 // Iterators 531 virtual child_iterator child_begin(); 532 virtual child_iterator child_end(); 533 534 virtual void EmitImpl(llvm::Serializer& S) const; 535 static ParenExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 536}; 537 538 539/// UnaryOperator - This represents the unary-expression's (except sizeof and 540/// alignof), the postinc/postdec operators from postfix-expression, and various 541/// extensions. 542/// 543/// Notes on various nodes: 544/// 545/// Real/Imag - These return the real/imag part of a complex operand. If 546/// applied to a non-complex value, the former returns its operand and the 547/// later returns zero in the type of the operand. 548/// 549/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 550/// subexpression is a compound literal with the various MemberExpr and 551/// ArraySubscriptExpr's applied to it. 552/// 553class UnaryOperator : public Expr { 554public: 555 // Note that additions to this should also update the StmtVisitor class. 556 enum Opcode { 557 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 558 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 559 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 560 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 561 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 562 Real, Imag, // "__real expr"/"__imag expr" Extension. 563 Extension, // __extension__ marker. 564 OffsetOf // __builtin_offsetof 565 }; 566private: 567 Stmt *Val; 568 Opcode Opc; 569 SourceLocation Loc; 570public: 571 572 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 573 : Expr(UnaryOperatorClass, type, 574 input->isTypeDependent() && opc != OffsetOf, 575 input->isValueDependent()), 576 Val(input), Opc(opc), Loc(l) {} 577 578 Opcode getOpcode() const { return Opc; } 579 Expr *getSubExpr() const { return cast<Expr>(Val); } 580 581 /// getOperatorLoc - Return the location of the operator. 582 SourceLocation getOperatorLoc() const { return Loc; } 583 584 /// isPostfix - Return true if this is a postfix operation, like x++. 585 static bool isPostfix(Opcode Op); 586 587 /// isPostfix - Return true if this is a prefix operation, like --x. 588 static bool isPrefix(Opcode Op); 589 590 bool isPrefix() const { return isPrefix(Opc); } 591 bool isPostfix() const { return isPostfix(Opc); } 592 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 593 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 594 bool isOffsetOfOp() const { return Opc == OffsetOf; } 595 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 596 597 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 598 /// corresponds to, e.g. "sizeof" or "[pre]++" 599 static const char *getOpcodeStr(Opcode Op); 600 601 virtual SourceRange getSourceRange() const { 602 if (isPostfix()) 603 return SourceRange(Val->getLocStart(), Loc); 604 else 605 return SourceRange(Loc, Val->getLocEnd()); 606 } 607 virtual SourceLocation getExprLoc() const { return Loc; } 608 609 static bool classof(const Stmt *T) { 610 return T->getStmtClass() == UnaryOperatorClass; 611 } 612 static bool classof(const UnaryOperator *) { return true; } 613 614 int64_t evaluateOffsetOf(ASTContext& C) const; 615 616 // Iterators 617 virtual child_iterator child_begin(); 618 virtual child_iterator child_end(); 619 620 virtual void EmitImpl(llvm::Serializer& S) const; 621 static UnaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 622}; 623 624/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 625/// types and expressions. 626class SizeOfAlignOfExpr : public Expr { 627 bool isSizeof : 1; // true if sizeof, false if alignof. 628 bool isType : 1; // true if operand is a type, false if an expression 629 union { 630 void *Ty; 631 Stmt *Ex; 632 } Argument; 633 SourceLocation OpLoc, RParenLoc; 634public: 635 SizeOfAlignOfExpr(bool issizeof, bool istype, void *argument, 636 QualType resultType, SourceLocation op, 637 SourceLocation rp) : 638 Expr(SizeOfAlignOfExprClass, resultType), isSizeof(issizeof), 639 isType(istype), OpLoc(op), RParenLoc(rp) { 640 if (isType) 641 Argument.Ty = argument; 642 else 643 // argument was an Expr*, so cast it back to that to be safe 644 Argument.Ex = static_cast<Expr*>(argument); 645 } 646 647 virtual void Destroy(ASTContext& C); 648 649 bool isSizeOf() const { return isSizeof; } 650 bool isArgumentType() const { return isType; } 651 QualType getArgumentType() const { 652 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 653 return QualType::getFromOpaquePtr(Argument.Ty); 654 } 655 Expr *getArgumentExpr() { 656 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 657 return static_cast<Expr*>(Argument.Ex); 658 } 659 const Expr *getArgumentExpr() const { 660 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 661 } 662 663 /// Gets the argument type, or the type of the argument expression, whichever 664 /// is appropriate. 665 QualType getTypeOfArgument() const { 666 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 667 } 668 669 SourceLocation getOperatorLoc() const { return OpLoc; } 670 671 virtual SourceRange getSourceRange() const { 672 return SourceRange(OpLoc, RParenLoc); 673 } 674 675 static bool classof(const Stmt *T) { 676 return T->getStmtClass() == SizeOfAlignOfExprClass; 677 } 678 static bool classof(const SizeOfAlignOfExpr *) { return true; } 679 680 // Iterators 681 virtual child_iterator child_begin(); 682 virtual child_iterator child_end(); 683 684 virtual void EmitImpl(llvm::Serializer& S) const; 685 static SizeOfAlignOfExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 686}; 687 688//===----------------------------------------------------------------------===// 689// Postfix Operators. 690//===----------------------------------------------------------------------===// 691 692/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 693class ArraySubscriptExpr : public Expr { 694 enum { LHS, RHS, END_EXPR=2 }; 695 Stmt* SubExprs[END_EXPR]; 696 SourceLocation RBracketLoc; 697public: 698 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 699 SourceLocation rbracketloc) 700 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 701 SubExprs[LHS] = lhs; 702 SubExprs[RHS] = rhs; 703 } 704 705 /// An array access can be written A[4] or 4[A] (both are equivalent). 706 /// - getBase() and getIdx() always present the normalized view: A[4]. 707 /// In this case getBase() returns "A" and getIdx() returns "4". 708 /// - getLHS() and getRHS() present the syntactic view. e.g. for 709 /// 4[A] getLHS() returns "4". 710 /// Note: Because vector element access is also written A[4] we must 711 /// predicate the format conversion in getBase and getIdx only on the 712 /// the type of the RHS, as it is possible for the LHS to be a vector of 713 /// integer type 714 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 715 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 716 717 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 718 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 719 720 Expr *getBase() { 721 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 722 } 723 724 const Expr *getBase() const { 725 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 726 } 727 728 Expr *getIdx() { 729 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 730 } 731 732 const Expr *getIdx() const { 733 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 734 } 735 736 virtual SourceRange getSourceRange() const { 737 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 738 } 739 740 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 741 742 static bool classof(const Stmt *T) { 743 return T->getStmtClass() == ArraySubscriptExprClass; 744 } 745 static bool classof(const ArraySubscriptExpr *) { return true; } 746 747 // Iterators 748 virtual child_iterator child_begin(); 749 virtual child_iterator child_end(); 750 751 virtual void EmitImpl(llvm::Serializer& S) const; 752 static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 753}; 754 755 756/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 757/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 758/// while its subclasses may represent alternative syntax that (semantically) 759/// results in a function call. For example, CXXOperatorCallExpr is 760/// a subclass for overloaded operator calls that use operator syntax, e.g., 761/// "str1 + str2" to resolve to a function call. 762class CallExpr : public Expr { 763 enum { FN=0, ARGS_START=1 }; 764 Stmt **SubExprs; 765 unsigned NumArgs; 766 SourceLocation RParenLoc; 767 768 // This version of the ctor is for deserialization. 769 CallExpr(StmtClass SC, Stmt** subexprs, unsigned numargs, QualType t, 770 SourceLocation rparenloc) 771 : Expr(SC,t), SubExprs(subexprs), 772 NumArgs(numargs), RParenLoc(rparenloc) {} 773 774protected: 775 // This version of the constructor is for derived classes. 776 CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t, 777 SourceLocation rparenloc); 778 779public: 780 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 781 SourceLocation rparenloc); 782 ~CallExpr() { 783 delete [] SubExprs; 784 } 785 786 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 787 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 788 void setCallee(Expr *F) { SubExprs[FN] = F; } 789 790 /// getNumArgs - Return the number of actual arguments to this call. 791 /// 792 unsigned getNumArgs() const { return NumArgs; } 793 794 /// getArg - Return the specified argument. 795 Expr *getArg(unsigned Arg) { 796 assert(Arg < NumArgs && "Arg access out of range!"); 797 return cast<Expr>(SubExprs[Arg+ARGS_START]); 798 } 799 const Expr *getArg(unsigned Arg) const { 800 assert(Arg < NumArgs && "Arg access out of range!"); 801 return cast<Expr>(SubExprs[Arg+ARGS_START]); 802 } 803 /// setArg - Set the specified argument. 804 void setArg(unsigned Arg, Expr *ArgExpr) { 805 assert(Arg < NumArgs && "Arg access out of range!"); 806 SubExprs[Arg+ARGS_START] = ArgExpr; 807 } 808 809 /// setNumArgs - This changes the number of arguments present in this call. 810 /// Any orphaned expressions are deleted by this, and any new operands are set 811 /// to null. 812 void setNumArgs(unsigned NumArgs); 813 814 typedef ExprIterator arg_iterator; 815 typedef ConstExprIterator const_arg_iterator; 816 817 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 818 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 819 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 820 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 821 822 /// getNumCommas - Return the number of commas that must have been present in 823 /// this function call. 824 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 825 826 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 827 /// not, return 0. 828 unsigned isBuiltinCall() const; 829 830 SourceLocation getRParenLoc() const { return RParenLoc; } 831 832 virtual SourceRange getSourceRange() const { 833 return SourceRange(getCallee()->getLocStart(), RParenLoc); 834 } 835 836 static bool classof(const Stmt *T) { 837 return T->getStmtClass() == CallExprClass || 838 T->getStmtClass() == CXXOperatorCallExprClass || 839 T->getStmtClass() == CXXMemberCallExprClass; 840 } 841 static bool classof(const CallExpr *) { return true; } 842 static bool classof(const CXXOperatorCallExpr *) { return true; } 843 static bool classof(const CXXMemberCallExpr *) { return true; } 844 845 // Iterators 846 virtual child_iterator child_begin(); 847 virtual child_iterator child_end(); 848 849 virtual void EmitImpl(llvm::Serializer& S) const; 850 static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C, 851 StmtClass SC); 852}; 853 854/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 855/// 856class MemberExpr : public Expr { 857 Stmt *Base; 858 NamedDecl *MemberDecl; 859 SourceLocation MemberLoc; 860 bool IsArrow; // True if this is "X->F", false if this is "X.F". 861public: 862 MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l, 863 QualType ty) 864 : Expr(MemberExprClass, ty), 865 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 866 867 void setBase(Expr *E) { Base = E; } 868 Expr *getBase() const { return cast<Expr>(Base); } 869 NamedDecl *getMemberDecl() const { return MemberDecl; } 870 void setMemberDecl(NamedDecl *D) { MemberDecl = D; } 871 bool isArrow() const { return IsArrow; } 872 873 virtual SourceRange getSourceRange() const { 874 return SourceRange(getBase()->getLocStart(), MemberLoc); 875 } 876 877 virtual SourceLocation getExprLoc() const { return MemberLoc; } 878 879 static bool classof(const Stmt *T) { 880 return T->getStmtClass() == MemberExprClass; 881 } 882 static bool classof(const MemberExpr *) { return true; } 883 884 // Iterators 885 virtual child_iterator child_begin(); 886 virtual child_iterator child_end(); 887 888 virtual void EmitImpl(llvm::Serializer& S) const; 889 static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 890}; 891 892/// CompoundLiteralExpr - [C99 6.5.2.5] 893/// 894class CompoundLiteralExpr : public Expr { 895 /// LParenLoc - If non-null, this is the location of the left paren in a 896 /// compound literal like "(int){4}". This can be null if this is a 897 /// synthesized compound expression. 898 SourceLocation LParenLoc; 899 Stmt *Init; 900 bool FileScope; 901public: 902 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 903 bool fileScope) 904 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 905 FileScope(fileScope) {} 906 907 const Expr *getInitializer() const { return cast<Expr>(Init); } 908 Expr *getInitializer() { return cast<Expr>(Init); } 909 910 bool isFileScope() const { return FileScope; } 911 912 SourceLocation getLParenLoc() const { return LParenLoc; } 913 914 virtual SourceRange getSourceRange() const { 915 // FIXME: Init should never be null. 916 if (!Init) 917 return SourceRange(); 918 if (LParenLoc.isInvalid()) 919 return Init->getSourceRange(); 920 return SourceRange(LParenLoc, Init->getLocEnd()); 921 } 922 923 static bool classof(const Stmt *T) { 924 return T->getStmtClass() == CompoundLiteralExprClass; 925 } 926 static bool classof(const CompoundLiteralExpr *) { return true; } 927 928 // Iterators 929 virtual child_iterator child_begin(); 930 virtual child_iterator child_end(); 931 932 virtual void EmitImpl(llvm::Serializer& S) const; 933 static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 934}; 935 936/// CastExpr - Base class for type casts, including both implicit 937/// casts (ImplicitCastExpr) and explicit casts that have some 938/// representation in the source code (ExplicitCastExpr's derived 939/// classes). 940class CastExpr : public Expr { 941 Stmt *Op; 942protected: 943 CastExpr(StmtClass SC, QualType ty, Expr *op) : 944 Expr(SC, ty, 945 // Cast expressions are type-dependent if the type is 946 // dependent (C++ [temp.dep.expr]p3). 947 ty->isDependentType(), 948 // Cast expressions are value-dependent if the type is 949 // dependent or if the subexpression is value-dependent. 950 ty->isDependentType() || (op && op->isValueDependent())), 951 Op(op) {} 952 953public: 954 Expr *getSubExpr() { return cast<Expr>(Op); } 955 const Expr *getSubExpr() const { return cast<Expr>(Op); } 956 957 static bool classof(const Stmt *T) { 958 StmtClass SC = T->getStmtClass(); 959 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 960 return true; 961 962 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 963 return true; 964 965 return false; 966 } 967 static bool classof(const CastExpr *) { return true; } 968 969 // Iterators 970 virtual child_iterator child_begin(); 971 virtual child_iterator child_end(); 972}; 973 974/// ImplicitCastExpr - Allows us to explicitly represent implicit type 975/// conversions, which have no direct representation in the original 976/// source code. For example: converting T[]->T*, void f()->void 977/// (*f)(), float->double, short->int, etc. 978/// 979/// In C, implicit casts always produce rvalues. However, in C++, an 980/// implicit cast whose result is being bound to a reference will be 981/// an lvalue. For example: 982/// 983/// @code 984/// class Base { }; 985/// class Derived : public Base { }; 986/// void f(Derived d) { 987/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 988/// } 989/// @endcode 990class ImplicitCastExpr : public CastExpr { 991 /// LvalueCast - Whether this cast produces an lvalue. 992 bool LvalueCast; 993 994public: 995 ImplicitCastExpr(QualType ty, Expr *op, bool Lvalue) : 996 CastExpr(ImplicitCastExprClass, ty, op), LvalueCast(Lvalue) { } 997 998 virtual SourceRange getSourceRange() const { 999 return getSubExpr()->getSourceRange(); 1000 } 1001 1002 /// isLvalueCast - Whether this cast produces an lvalue. 1003 bool isLvalueCast() const { return LvalueCast; } 1004 1005 /// setLvalueCast - Set whether this cast produces an lvalue. 1006 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1007 1008 static bool classof(const Stmt *T) { 1009 return T->getStmtClass() == ImplicitCastExprClass; 1010 } 1011 static bool classof(const ImplicitCastExpr *) { return true; } 1012 1013 virtual void EmitImpl(llvm::Serializer& S) const; 1014 static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1015}; 1016 1017/// ExplicitCastExpr - An explicit cast written in the source 1018/// code. 1019/// 1020/// This class is effectively an abstract class, because it provides 1021/// the basic representation of an explicitly-written cast without 1022/// specifying which kind of cast (C cast, functional cast, static 1023/// cast, etc.) was written; specific derived classes represent the 1024/// particular style of cast and its location information. 1025/// 1026/// Unlike implicit casts, explicit cast nodes have two different 1027/// types: the type that was written into the source code, and the 1028/// actual type of the expression as determined by semantic 1029/// analysis. These types may differ slightly. For example, in C++ one 1030/// can cast to a reference type, which indicates that the resulting 1031/// expression will be an lvalue. The reference type, however, will 1032/// not be used as the type of the expression. 1033class ExplicitCastExpr : public CastExpr { 1034 /// TypeAsWritten - The type that this expression is casting to, as 1035 /// written in the source code. 1036 QualType TypeAsWritten; 1037 1038protected: 1039 ExplicitCastExpr(StmtClass SC, QualType exprTy, Expr *op, QualType writtenTy) 1040 : CastExpr(SC, exprTy, op), TypeAsWritten(writtenTy) {} 1041 1042public: 1043 /// getTypeAsWritten - Returns the type that this expression is 1044 /// casting to, as written in the source code. 1045 QualType getTypeAsWritten() const { return TypeAsWritten; } 1046 1047 static bool classof(const Stmt *T) { 1048 StmtClass SC = T->getStmtClass(); 1049 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1050 return true; 1051 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1052 return true; 1053 1054 return false; 1055 } 1056 static bool classof(const ExplicitCastExpr *) { return true; } 1057}; 1058 1059/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1060/// cast in C++ (C++ [expr.cast]), which uses the syntax 1061/// (Type)expr. For example: @c (int)f. 1062class CStyleCastExpr : public ExplicitCastExpr { 1063 SourceLocation LPLoc; // the location of the left paren 1064 SourceLocation RPLoc; // the location of the right paren 1065public: 1066 CStyleCastExpr(QualType exprTy, Expr *op, QualType writtenTy, 1067 SourceLocation l, SourceLocation r) : 1068 ExplicitCastExpr(CStyleCastExprClass, exprTy, op, writtenTy), 1069 LPLoc(l), RPLoc(r) {} 1070 1071 SourceLocation getLParenLoc() const { return LPLoc; } 1072 SourceLocation getRParenLoc() const { return RPLoc; } 1073 1074 virtual SourceRange getSourceRange() const { 1075 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1076 } 1077 static bool classof(const Stmt *T) { 1078 return T->getStmtClass() == CStyleCastExprClass; 1079 } 1080 static bool classof(const CStyleCastExpr *) { return true; } 1081 1082 virtual void EmitImpl(llvm::Serializer& S) const; 1083 static CStyleCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1084}; 1085 1086class BinaryOperator : public Expr { 1087public: 1088 enum Opcode { 1089 // Operators listed in order of precedence. 1090 // Note that additions to this should also update the StmtVisitor class. 1091 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1092 Add, Sub, // [C99 6.5.6] Additive operators. 1093 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1094 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1095 EQ, NE, // [C99 6.5.9] Equality operators. 1096 And, // [C99 6.5.10] Bitwise AND operator. 1097 Xor, // [C99 6.5.11] Bitwise XOR operator. 1098 Or, // [C99 6.5.12] Bitwise OR operator. 1099 LAnd, // [C99 6.5.13] Logical AND operator. 1100 LOr, // [C99 6.5.14] Logical OR operator. 1101 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1102 DivAssign, RemAssign, 1103 AddAssign, SubAssign, 1104 ShlAssign, ShrAssign, 1105 AndAssign, XorAssign, 1106 OrAssign, 1107 Comma // [C99 6.5.17] Comma operator. 1108 }; 1109private: 1110 enum { LHS, RHS, END_EXPR }; 1111 Stmt* SubExprs[END_EXPR]; 1112 Opcode Opc; 1113 SourceLocation OpLoc; 1114public: 1115 1116 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1117 SourceLocation opLoc) 1118 : Expr(BinaryOperatorClass, ResTy, 1119 lhs->isTypeDependent() || rhs->isTypeDependent(), 1120 lhs->isValueDependent() || rhs->isValueDependent()), 1121 Opc(opc), OpLoc(opLoc) { 1122 SubExprs[LHS] = lhs; 1123 SubExprs[RHS] = rhs; 1124 assert(!isCompoundAssignmentOp() && 1125 "Use ArithAssignBinaryOperator for compound assignments"); 1126 } 1127 1128 SourceLocation getOperatorLoc() const { return OpLoc; } 1129 Opcode getOpcode() const { return Opc; } 1130 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1131 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1132 virtual SourceRange getSourceRange() const { 1133 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1134 } 1135 1136 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1137 /// corresponds to, e.g. "<<=". 1138 static const char *getOpcodeStr(Opcode Op); 1139 1140 /// predicates to categorize the respective opcodes. 1141 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1142 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1143 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 1144 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 1145 1146 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1147 bool isRelationalOp() const { return isRelationalOp(Opc); } 1148 1149 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1150 bool isEqualityOp() const { return isEqualityOp(Opc); } 1151 1152 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1153 bool isLogicalOp() const { return isLogicalOp(Opc); } 1154 1155 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1156 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1157 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1158 1159 static bool classof(const Stmt *S) { 1160 return S->getStmtClass() == BinaryOperatorClass || 1161 S->getStmtClass() == CompoundAssignOperatorClass; 1162 } 1163 static bool classof(const BinaryOperator *) { return true; } 1164 1165 // Iterators 1166 virtual child_iterator child_begin(); 1167 virtual child_iterator child_end(); 1168 1169 virtual void EmitImpl(llvm::Serializer& S) const; 1170 static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1171 1172protected: 1173 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1174 SourceLocation oploc, bool dead) 1175 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1176 SubExprs[LHS] = lhs; 1177 SubExprs[RHS] = rhs; 1178 } 1179}; 1180 1181/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1182/// track of the type the operation is performed in. Due to the semantics of 1183/// these operators, the operands are promoted, the aritmetic performed, an 1184/// implicit conversion back to the result type done, then the assignment takes 1185/// place. This captures the intermediate type which the computation is done 1186/// in. 1187class CompoundAssignOperator : public BinaryOperator { 1188 QualType ComputationType; 1189public: 1190 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1191 QualType ResType, QualType CompType, 1192 SourceLocation OpLoc) 1193 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1194 ComputationType(CompType) { 1195 assert(isCompoundAssignmentOp() && 1196 "Only should be used for compound assignments"); 1197 } 1198 1199 QualType getComputationType() const { return ComputationType; } 1200 1201 static bool classof(const CompoundAssignOperator *) { return true; } 1202 static bool classof(const Stmt *S) { 1203 return S->getStmtClass() == CompoundAssignOperatorClass; 1204 } 1205 1206 virtual void EmitImpl(llvm::Serializer& S) const; 1207 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D, 1208 ASTContext& C); 1209}; 1210 1211/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1212/// GNU "missing LHS" extension is in use. 1213/// 1214class ConditionalOperator : public Expr { 1215 enum { COND, LHS, RHS, END_EXPR }; 1216 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1217public: 1218 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1219 : Expr(ConditionalOperatorClass, t, 1220 // FIXME: the type of the conditional operator doesn't 1221 // depend on the type of the conditional, but the standard 1222 // seems to imply that it could. File a bug! 1223 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1224 (cond->isValueDependent() || 1225 (lhs && lhs->isValueDependent()) || 1226 (rhs && rhs->isValueDependent()))) { 1227 SubExprs[COND] = cond; 1228 SubExprs[LHS] = lhs; 1229 SubExprs[RHS] = rhs; 1230 } 1231 1232 // getCond - Return the expression representing the condition for 1233 // the ?: operator. 1234 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1235 1236 // getTrueExpr - Return the subexpression representing the value of the ?: 1237 // expression if the condition evaluates to true. In most cases this value 1238 // will be the same as getLHS() except a GCC extension allows the left 1239 // subexpression to be omitted, and instead of the condition be returned. 1240 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1241 // is only evaluated once. 1242 Expr *getTrueExpr() const { 1243 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1244 } 1245 1246 // getTrueExpr - Return the subexpression representing the value of the ?: 1247 // expression if the condition evaluates to false. This is the same as getRHS. 1248 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1249 1250 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1251 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1252 1253 virtual SourceRange getSourceRange() const { 1254 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1255 } 1256 static bool classof(const Stmt *T) { 1257 return T->getStmtClass() == ConditionalOperatorClass; 1258 } 1259 static bool classof(const ConditionalOperator *) { return true; } 1260 1261 // Iterators 1262 virtual child_iterator child_begin(); 1263 virtual child_iterator child_end(); 1264 1265 virtual void EmitImpl(llvm::Serializer& S) const; 1266 static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1267}; 1268 1269/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1270class AddrLabelExpr : public Expr { 1271 SourceLocation AmpAmpLoc, LabelLoc; 1272 LabelStmt *Label; 1273public: 1274 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1275 QualType t) 1276 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1277 1278 virtual SourceRange getSourceRange() const { 1279 return SourceRange(AmpAmpLoc, LabelLoc); 1280 } 1281 1282 LabelStmt *getLabel() const { return Label; } 1283 1284 static bool classof(const Stmt *T) { 1285 return T->getStmtClass() == AddrLabelExprClass; 1286 } 1287 static bool classof(const AddrLabelExpr *) { return true; } 1288 1289 // Iterators 1290 virtual child_iterator child_begin(); 1291 virtual child_iterator child_end(); 1292 1293 virtual void EmitImpl(llvm::Serializer& S) const; 1294 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1295}; 1296 1297/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1298/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1299/// takes the value of the last subexpression. 1300class StmtExpr : public Expr { 1301 Stmt *SubStmt; 1302 SourceLocation LParenLoc, RParenLoc; 1303public: 1304 StmtExpr(CompoundStmt *substmt, QualType T, 1305 SourceLocation lp, SourceLocation rp) : 1306 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1307 1308 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1309 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1310 1311 virtual SourceRange getSourceRange() const { 1312 return SourceRange(LParenLoc, RParenLoc); 1313 } 1314 1315 static bool classof(const Stmt *T) { 1316 return T->getStmtClass() == StmtExprClass; 1317 } 1318 static bool classof(const StmtExpr *) { return true; } 1319 1320 // Iterators 1321 virtual child_iterator child_begin(); 1322 virtual child_iterator child_end(); 1323 1324 virtual void EmitImpl(llvm::Serializer& S) const; 1325 static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1326}; 1327 1328/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1329/// This AST node represents a function that returns 1 if two *types* (not 1330/// expressions) are compatible. The result of this built-in function can be 1331/// used in integer constant expressions. 1332class TypesCompatibleExpr : public Expr { 1333 QualType Type1; 1334 QualType Type2; 1335 SourceLocation BuiltinLoc, RParenLoc; 1336public: 1337 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1338 QualType t1, QualType t2, SourceLocation RP) : 1339 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1340 BuiltinLoc(BLoc), RParenLoc(RP) {} 1341 1342 QualType getArgType1() const { return Type1; } 1343 QualType getArgType2() const { return Type2; } 1344 1345 virtual SourceRange getSourceRange() const { 1346 return SourceRange(BuiltinLoc, RParenLoc); 1347 } 1348 static bool classof(const Stmt *T) { 1349 return T->getStmtClass() == TypesCompatibleExprClass; 1350 } 1351 static bool classof(const TypesCompatibleExpr *) { return true; } 1352 1353 // Iterators 1354 virtual child_iterator child_begin(); 1355 virtual child_iterator child_end(); 1356 1357 virtual void EmitImpl(llvm::Serializer& S) const; 1358 static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1359}; 1360 1361/// ShuffleVectorExpr - clang-specific builtin-in function 1362/// __builtin_shufflevector. 1363/// This AST node represents a operator that does a constant 1364/// shuffle, similar to LLVM's shufflevector instruction. It takes 1365/// two vectors and a variable number of constant indices, 1366/// and returns the appropriately shuffled vector. 1367class ShuffleVectorExpr : public Expr { 1368 SourceLocation BuiltinLoc, RParenLoc; 1369 1370 // SubExprs - the list of values passed to the __builtin_shufflevector 1371 // function. The first two are vectors, and the rest are constant 1372 // indices. The number of values in this list is always 1373 // 2+the number of indices in the vector type. 1374 Stmt **SubExprs; 1375 unsigned NumExprs; 1376 1377public: 1378 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1379 QualType Type, SourceLocation BLoc, 1380 SourceLocation RP) : 1381 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1382 RParenLoc(RP), NumExprs(nexpr) { 1383 1384 SubExprs = new Stmt*[nexpr]; 1385 for (unsigned i = 0; i < nexpr; i++) 1386 SubExprs[i] = args[i]; 1387 } 1388 1389 virtual SourceRange getSourceRange() const { 1390 return SourceRange(BuiltinLoc, RParenLoc); 1391 } 1392 static bool classof(const Stmt *T) { 1393 return T->getStmtClass() == ShuffleVectorExprClass; 1394 } 1395 static bool classof(const ShuffleVectorExpr *) { return true; } 1396 1397 ~ShuffleVectorExpr() { 1398 delete [] SubExprs; 1399 } 1400 1401 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1402 /// constant expression, the actual arguments passed in, and the function 1403 /// pointers. 1404 unsigned getNumSubExprs() const { return NumExprs; } 1405 1406 /// getExpr - Return the Expr at the specified index. 1407 Expr *getExpr(unsigned Index) { 1408 assert((Index < NumExprs) && "Arg access out of range!"); 1409 return cast<Expr>(SubExprs[Index]); 1410 } 1411 const Expr *getExpr(unsigned Index) const { 1412 assert((Index < NumExprs) && "Arg access out of range!"); 1413 return cast<Expr>(SubExprs[Index]); 1414 } 1415 1416 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1417 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1418 return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1419 } 1420 1421 // Iterators 1422 virtual child_iterator child_begin(); 1423 virtual child_iterator child_end(); 1424 1425 virtual void EmitImpl(llvm::Serializer& S) const; 1426 static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1427}; 1428 1429/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1430/// This AST node is similar to the conditional operator (?:) in C, with 1431/// the following exceptions: 1432/// - the test expression must be a constant expression. 1433/// - the expression returned has it's type unaltered by promotion rules. 1434/// - does not evaluate the expression that was not chosen. 1435class ChooseExpr : public Expr { 1436 enum { COND, LHS, RHS, END_EXPR }; 1437 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1438 SourceLocation BuiltinLoc, RParenLoc; 1439public: 1440 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1441 SourceLocation RP) 1442 : Expr(ChooseExprClass, t), 1443 BuiltinLoc(BLoc), RParenLoc(RP) { 1444 SubExprs[COND] = cond; 1445 SubExprs[LHS] = lhs; 1446 SubExprs[RHS] = rhs; 1447 } 1448 1449 /// isConditionTrue - Return true if the condition is true. This is always 1450 /// statically knowable for a well-formed choosexpr. 1451 bool isConditionTrue(ASTContext &C) const; 1452 1453 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1454 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1455 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1456 1457 virtual SourceRange getSourceRange() const { 1458 return SourceRange(BuiltinLoc, RParenLoc); 1459 } 1460 static bool classof(const Stmt *T) { 1461 return T->getStmtClass() == ChooseExprClass; 1462 } 1463 static bool classof(const ChooseExpr *) { return true; } 1464 1465 // Iterators 1466 virtual child_iterator child_begin(); 1467 virtual child_iterator child_end(); 1468 1469 virtual void EmitImpl(llvm::Serializer& S) const; 1470 static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1471}; 1472 1473/// GNUNullExpr - Implements the GNU __null extension, which is a name 1474/// for a null pointer constant that has integral type (e.g., int or 1475/// long) and is the same size and alignment as a pointer. The __null 1476/// extension is typically only used by system headers, which define 1477/// NULL as __null in C++ rather than using 0 (which is an integer 1478/// that may not match the size of a pointer). 1479class GNUNullExpr : public Expr { 1480 /// TokenLoc - The location of the __null keyword. 1481 SourceLocation TokenLoc; 1482 1483public: 1484 GNUNullExpr(QualType Ty, SourceLocation Loc) 1485 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 1486 1487 /// getTokenLocation - The location of the __null token. 1488 SourceLocation getTokenLocation() const { return TokenLoc; } 1489 1490 virtual SourceRange getSourceRange() const { 1491 return SourceRange(TokenLoc); 1492 } 1493 static bool classof(const Stmt *T) { 1494 return T->getStmtClass() == GNUNullExprClass; 1495 } 1496 static bool classof(const GNUNullExpr *) { return true; } 1497 1498 // Iterators 1499 virtual child_iterator child_begin(); 1500 virtual child_iterator child_end(); 1501 1502 virtual void EmitImpl(llvm::Serializer& S) const; 1503 static GNUNullExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1504}; 1505 1506/// OverloadExpr - Clang builtin function __builtin_overload. 1507/// This AST node provides a way to overload functions in C. 1508/// 1509/// The first argument is required to be a constant expression, for the number 1510/// of arguments passed to each candidate function. 1511/// 1512/// The next N arguments, where N is the value of the constant expression, 1513/// are the values to be passed as arguments. 1514/// 1515/// The rest of the arguments are values of pointer to function type, which 1516/// are the candidate functions for overloading. 1517/// 1518/// The result is a equivalent to a CallExpr taking N arguments to the 1519/// candidate function whose parameter types match the types of the N arguments. 1520/// 1521/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl); 1522/// If X and Y are long doubles, Z will assigned the result of modl(X, Y); 1523/// If X and Y are floats, Z will be assigned the result of modf(X, Y); 1524class OverloadExpr : public Expr { 1525 // SubExprs - the list of values passed to the __builtin_overload function. 1526 // SubExpr[0] is a constant expression 1527 // SubExpr[1-N] are the parameters to pass to the matching function call 1528 // SubExpr[N-...] are the candidate functions, of type pointer to function. 1529 Stmt **SubExprs; 1530 1531 // NumExprs - the size of the SubExprs array 1532 unsigned NumExprs; 1533 1534 // The index of the matching candidate function 1535 unsigned FnIndex; 1536 1537 SourceLocation BuiltinLoc; 1538 SourceLocation RParenLoc; 1539public: 1540 OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t, 1541 SourceLocation bloc, SourceLocation rploc) 1542 : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx), 1543 BuiltinLoc(bloc), RParenLoc(rploc) { 1544 SubExprs = new Stmt*[nexprs]; 1545 for (unsigned i = 0; i != nexprs; ++i) 1546 SubExprs[i] = args[i]; 1547 } 1548 ~OverloadExpr() { 1549 delete [] SubExprs; 1550 } 1551 1552 /// arg_begin - Return a pointer to the list of arguments that will be passed 1553 /// to the matching candidate function, skipping over the initial constant 1554 /// expression. 1555 typedef ConstExprIterator const_arg_iterator; 1556 const_arg_iterator arg_begin() const { return &SubExprs[0]+1; } 1557 const_arg_iterator arg_end(ASTContext& Ctx) const { 1558 return &SubExprs[0]+1+getNumArgs(Ctx); 1559 } 1560 1561 /// getNumArgs - Return the number of arguments to pass to the candidate 1562 /// functions. 1563 unsigned getNumArgs(ASTContext &Ctx) const { 1564 return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1565 } 1566 1567 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1568 /// constant expression, the actual arguments passed in, and the function 1569 /// pointers. 1570 unsigned getNumSubExprs() const { return NumExprs; } 1571 1572 /// getExpr - Return the Expr at the specified index. 1573 Expr *getExpr(unsigned Index) const { 1574 assert((Index < NumExprs) && "Arg access out of range!"); 1575 return cast<Expr>(SubExprs[Index]); 1576 } 1577 1578 /// getFn - Return the matching candidate function for this OverloadExpr. 1579 Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); } 1580 1581 virtual SourceRange getSourceRange() const { 1582 return SourceRange(BuiltinLoc, RParenLoc); 1583 } 1584 static bool classof(const Stmt *T) { 1585 return T->getStmtClass() == OverloadExprClass; 1586 } 1587 static bool classof(const OverloadExpr *) { return true; } 1588 1589 // Iterators 1590 virtual child_iterator child_begin(); 1591 virtual child_iterator child_end(); 1592 1593 virtual void EmitImpl(llvm::Serializer& S) const; 1594 static OverloadExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1595}; 1596 1597/// VAArgExpr, used for the builtin function __builtin_va_start. 1598class VAArgExpr : public Expr { 1599 Stmt *Val; 1600 SourceLocation BuiltinLoc, RParenLoc; 1601public: 1602 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1603 : Expr(VAArgExprClass, t), 1604 Val(e), 1605 BuiltinLoc(BLoc), 1606 RParenLoc(RPLoc) { } 1607 1608 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1609 Expr *getSubExpr() { return cast<Expr>(Val); } 1610 virtual SourceRange getSourceRange() const { 1611 return SourceRange(BuiltinLoc, RParenLoc); 1612 } 1613 static bool classof(const Stmt *T) { 1614 return T->getStmtClass() == VAArgExprClass; 1615 } 1616 static bool classof(const VAArgExpr *) { return true; } 1617 1618 // Iterators 1619 virtual child_iterator child_begin(); 1620 virtual child_iterator child_end(); 1621 1622 virtual void EmitImpl(llvm::Serializer& S) const; 1623 static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1624}; 1625 1626/// @brief Describes an C or C++ initializer list. 1627/// 1628/// InitListExpr describes an initializer list, which can be used to 1629/// initialize objects of different types, including 1630/// struct/class/union types, arrays, and vectors. For example: 1631/// 1632/// @code 1633/// struct foo x = { 1, { 2, 3 } }; 1634/// @endcode 1635/// 1636/// Prior to semantic analysis, an initializer list will represent the 1637/// initializer list as written by the user, but will have the 1638/// placeholder type "void". This initializer list is called the 1639/// syntactic form of the initializer, and may contain C99 designated 1640/// initializers (represented as DesignatedInitExprs), initializations 1641/// of subobject members without explicit braces, and so on. Clients 1642/// interested in the original syntax of the initializer list should 1643/// use the syntactic form of the initializer list. 1644/// 1645/// After semantic analysis, the initializer list will represent the 1646/// semantic form of the initializer, where the initializations of all 1647/// subobjects are made explicit with nested InitListExpr nodes and 1648/// C99 designators have been eliminated by placing the designated 1649/// initializations into the subobject they initialize. Additionally, 1650/// any "holes" in the initialization, where no initializer has been 1651/// specified for a particular subobject, will be replaced with 1652/// implicitly-generated CXXZeroInitValueExpr expressions that 1653/// value-initialize the subobjects. Note, however, that the 1654/// initializer lists may still have fewer initializers than there are 1655/// elements to initialize within the object. 1656/// 1657/// Given the semantic form of the initializer list, one can retrieve 1658/// the original syntactic form of that initializer list (if it 1659/// exists) using getSyntacticForm(). Since many initializer lists 1660/// have the same syntactic and semantic forms, getSyntacticForm() may 1661/// return NULL, indicating that the current initializer list also 1662/// serves as its syntactic form. 1663class InitListExpr : public Expr { 1664 std::vector<Stmt *> InitExprs; 1665 SourceLocation LBraceLoc, RBraceLoc; 1666 1667 /// Contains the initializer list that describes the syntactic form 1668 /// written in the source code. 1669 InitListExpr *SyntacticForm; 1670 1671public: 1672 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1673 SourceLocation rbraceloc); 1674 1675 unsigned getNumInits() const { return InitExprs.size(); } 1676 1677 const Expr* getInit(unsigned Init) const { 1678 assert(Init < getNumInits() && "Initializer access out of range!"); 1679 return cast_or_null<Expr>(InitExprs[Init]); 1680 } 1681 1682 Expr* getInit(unsigned Init) { 1683 assert(Init < getNumInits() && "Initializer access out of range!"); 1684 return cast_or_null<Expr>(InitExprs[Init]); 1685 } 1686 1687 void setInit(unsigned Init, Expr *expr) { 1688 assert(Init < getNumInits() && "Initializer access out of range!"); 1689 InitExprs[Init] = expr; 1690 } 1691 1692 /// @brief Specify the number of initializers 1693 /// 1694 /// If there are more than @p NumInits initializers, the remaining 1695 /// initializers will be destroyed. If there are fewer than @p 1696 /// NumInits initializers, NULL expressions will be added for the 1697 /// unknown initializers. 1698 void resizeInits(ASTContext &Context, unsigned NumInits); 1699 1700 /// @brief Updates the initializer at index @p Init with the new 1701 /// expression @p expr, and returns the old expression at that 1702 /// location. 1703 /// 1704 /// When @p Init is out of range for this initializer list, the 1705 /// initializer list will be extended with NULL expressions to 1706 /// accomodate the new entry. 1707 Expr *updateInit(unsigned Init, Expr *expr); 1708 1709 // Explicit InitListExpr's originate from source code (and have valid source 1710 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1711 bool isExplicit() { 1712 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1713 } 1714 1715 /// @brief Retrieve the initializer list that describes the 1716 /// syntactic form of the initializer. 1717 /// 1718 /// 1719 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 1720 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 1721 1722 virtual SourceRange getSourceRange() const { 1723 return SourceRange(LBraceLoc, RBraceLoc); 1724 } 1725 static bool classof(const Stmt *T) { 1726 return T->getStmtClass() == InitListExprClass; 1727 } 1728 static bool classof(const InitListExpr *) { return true; } 1729 1730 // Iterators 1731 virtual child_iterator child_begin(); 1732 virtual child_iterator child_end(); 1733 1734 typedef std::vector<Stmt *>::iterator iterator; 1735 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 1736 1737 iterator begin() { return InitExprs.begin(); } 1738 iterator end() { return InitExprs.end(); } 1739 reverse_iterator rbegin() { return InitExprs.rbegin(); } 1740 reverse_iterator rend() { return InitExprs.rend(); } 1741 1742 // Serailization. 1743 virtual void EmitImpl(llvm::Serializer& S) const; 1744 static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1745 1746private: 1747 // Used by serializer. 1748 InitListExpr() : Expr(InitListExprClass, QualType()) {} 1749}; 1750 1751/// @brief Represents a C99 designated initializer expression. 1752/// 1753/// A designated initializer expression (C99 6.7.8) contains one or 1754/// more designators (which can be field designators, array 1755/// designators, or GNU array-range designators) followed by an 1756/// expression that initializes the field or element(s) that the 1757/// designators refer to. For example, given: 1758/// 1759/// @code 1760/// struct point { 1761/// double x; 1762/// double y; 1763/// }; 1764/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 1765/// @endcode 1766/// 1767/// The InitListExpr contains three DesignatedInitExprs, the first of 1768/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 1769/// designators, one array designator for @c [2] followed by one field 1770/// designator for @c .y. The initalization expression will be 1.0. 1771class DesignatedInitExpr : public Expr { 1772 /// The location of the '=' or ':' prior to the actual initializer 1773 /// expression. 1774 SourceLocation EqualOrColonLoc; 1775 1776 /// Whether this designated initializer used the GNU deprecated ':' 1777 /// syntax rather than the C99 '=' syntax. 1778 bool UsesColonSyntax : 1; 1779 1780 /// The number of designators in this initializer expression. 1781 unsigned NumDesignators : 15; 1782 1783 /// The number of subexpressions of this initializer expression, 1784 /// which contains both the initializer and any additional 1785 /// expressions used by array and array-range designators. 1786 unsigned NumSubExprs : 16; 1787 1788 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 1789 SourceLocation EqualOrColonLoc, bool UsesColonSyntax, 1790 unsigned NumSubExprs) 1791 : Expr(DesignatedInitExprClass, Ty), 1792 EqualOrColonLoc(EqualOrColonLoc), UsesColonSyntax(UsesColonSyntax), 1793 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) { } 1794 1795public: 1796 /// A field designator, e.g., ".x". 1797 struct FieldDesignator { 1798 /// Refers to the field that is being initialized. The low bit 1799 /// of this field determines whether this is actually a pointer 1800 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 1801 /// initially constructed, a field designator will store an 1802 /// IdentifierInfo*. After semantic analysis has resolved that 1803 /// name, the field designator will instead store a FieldDecl*. 1804 uintptr_t NameOrField; 1805 1806 /// The location of the '.' in the designated initializer. 1807 unsigned DotLoc; 1808 1809 /// The location of the field name in the designated initializer. 1810 unsigned FieldLoc; 1811 }; 1812 1813 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1814 struct ArrayOrRangeDesignator { 1815 /// Location of the first index expression within the designated 1816 /// initializer expression's list of subexpressions. 1817 unsigned Index; 1818 /// The location of the '[' starting the array range designator. 1819 unsigned LBracketLoc; 1820 /// The location of the ellipsis separating the start and end 1821 /// indices. Only valid for GNU array-range designators. 1822 unsigned EllipsisLoc; 1823 /// The location of the ']' terminating the array range designator. 1824 unsigned RBracketLoc; 1825 }; 1826 1827 /// @brief Represents a single C99 designator. 1828 /// 1829 /// @todo This class is infuriatingly similar to clang::Designator, 1830 /// but minor differences (storing indices vs. storing pointers) 1831 /// keep us from reusing it. Try harder, later, to rectify these 1832 /// differences. 1833 class Designator { 1834 /// @brief The kind of designator this describes. 1835 enum { 1836 FieldDesignator, 1837 ArrayDesignator, 1838 ArrayRangeDesignator 1839 } Kind; 1840 1841 union { 1842 /// A field designator, e.g., ".x". 1843 struct FieldDesignator Field; 1844 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1845 struct ArrayOrRangeDesignator ArrayOrRange; 1846 }; 1847 friend class DesignatedInitExpr; 1848 1849 public: 1850 /// @brief Initializes a field designator. 1851 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 1852 SourceLocation FieldLoc) 1853 : Kind(FieldDesignator) { 1854 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 1855 Field.DotLoc = DotLoc.getRawEncoding(); 1856 Field.FieldLoc = FieldLoc.getRawEncoding(); 1857 } 1858 1859 /// @brief Initializes an array designator. 1860 Designator(unsigned Index, SourceLocation LBracketLoc, 1861 SourceLocation RBracketLoc) 1862 : Kind(ArrayDesignator) { 1863 ArrayOrRange.Index = Index; 1864 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1865 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 1866 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1867 } 1868 1869 /// @brief Initializes a GNU array-range designator. 1870 Designator(unsigned Index, SourceLocation LBracketLoc, 1871 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 1872 : Kind(ArrayRangeDesignator) { 1873 ArrayOrRange.Index = Index; 1874 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1875 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 1876 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1877 } 1878 1879 bool isFieldDesignator() const { return Kind == FieldDesignator; } 1880 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 1881 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 1882 1883 IdentifierInfo * getFieldName(); 1884 1885 FieldDecl *getField() { 1886 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1887 if (Field.NameOrField & 0x01) 1888 return 0; 1889 else 1890 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 1891 } 1892 1893 void setField(FieldDecl *FD) { 1894 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1895 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 1896 } 1897 1898 SourceLocation getDotLoc() const { 1899 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1900 return SourceLocation::getFromRawEncoding(Field.DotLoc); 1901 } 1902 1903 SourceLocation getFieldLoc() const { 1904 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1905 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 1906 } 1907 1908 SourceLocation getLBracketLoc() const { 1909 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 1910 "Only valid on an array or array-range designator"); 1911 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 1912 } 1913 1914 SourceLocation getRBracketLoc() const { 1915 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 1916 "Only valid on an array or array-range designator"); 1917 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 1918 } 1919 1920 SourceLocation getEllipsisLoc() const { 1921 assert(Kind == ArrayRangeDesignator && 1922 "Only valid on an array-range designator"); 1923 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 1924 } 1925 1926 SourceLocation getStartLocation() const { 1927 if (Kind == FieldDesignator) 1928 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 1929 else 1930 return getLBracketLoc(); 1931 } 1932 }; 1933 1934 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 1935 unsigned NumDesignators, 1936 Expr **IndexExprs, unsigned NumIndexExprs, 1937 SourceLocation EqualOrColonLoc, 1938 bool UsesColonSyntax, Expr *Init); 1939 1940 /// @brief Returns the number of designators in this initializer. 1941 unsigned size() const { return NumDesignators; } 1942 1943 // Iterator access to the designators. 1944 typedef Designator* designators_iterator; 1945 designators_iterator designators_begin(); 1946 designators_iterator designators_end(); 1947 1948 Expr *getArrayIndex(const Designator& D); 1949 Expr *getArrayRangeStart(const Designator& D); 1950 Expr *getArrayRangeEnd(const Designator& D); 1951 1952 /// @brief Retrieve the location of the '=' that precedes the 1953 /// initializer value itself, if present. 1954 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 1955 1956 /// @brief Determines whether this designated initializer used the 1957 /// GNU 'fieldname:' syntax or the C99 '=' syntax. 1958 bool usesColonSyntax() const { return UsesColonSyntax; } 1959 1960 /// @brief Retrieve the initializer value. 1961 Expr *getInit() const { 1962 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 1963 } 1964 1965 void setInit(Expr *init) { 1966 *child_begin() = init; 1967 } 1968 1969 virtual SourceRange getSourceRange() const; 1970 1971 static bool classof(const Stmt *T) { 1972 return T->getStmtClass() == DesignatedInitExprClass; 1973 } 1974 static bool classof(const DesignatedInitExpr *) { return true; } 1975 1976 // Iterators 1977 virtual child_iterator child_begin(); 1978 virtual child_iterator child_end(); 1979}; 1980 1981//===----------------------------------------------------------------------===// 1982// Clang Extensions 1983//===----------------------------------------------------------------------===// 1984 1985 1986/// ExtVectorElementExpr - This represents access to specific elements of a 1987/// vector, and may occur on the left hand side or right hand side. For example 1988/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 1989/// 1990class ExtVectorElementExpr : public Expr { 1991 Stmt *Base; 1992 IdentifierInfo &Accessor; 1993 SourceLocation AccessorLoc; 1994public: 1995 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 1996 SourceLocation loc) 1997 : Expr(ExtVectorElementExprClass, ty), 1998 Base(base), Accessor(accessor), AccessorLoc(loc) {} 1999 2000 const Expr *getBase() const { return cast<Expr>(Base); } 2001 Expr *getBase() { return cast<Expr>(Base); } 2002 2003 IdentifierInfo &getAccessor() const { return Accessor; } 2004 2005 /// getNumElements - Get the number of components being selected. 2006 unsigned getNumElements() const; 2007 2008 /// containsDuplicateElements - Return true if any element access is 2009 /// repeated. 2010 bool containsDuplicateElements() const; 2011 2012 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2013 /// aggregate Constant of ConstantInt(s). 2014 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2015 2016 virtual SourceRange getSourceRange() const { 2017 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2018 } 2019 2020 static bool classof(const Stmt *T) { 2021 return T->getStmtClass() == ExtVectorElementExprClass; 2022 } 2023 static bool classof(const ExtVectorElementExpr *) { return true; } 2024 2025 // Iterators 2026 virtual child_iterator child_begin(); 2027 virtual child_iterator child_end(); 2028 2029 virtual void EmitImpl(llvm::Serializer& S) const; 2030 static ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2031}; 2032 2033 2034/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2035/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2036class BlockExpr : public Expr { 2037protected: 2038 BlockDecl *TheBlock; 2039public: 2040 BlockExpr(BlockDecl *BD, QualType ty) : Expr(BlockExprClass, ty), 2041 TheBlock(BD) {} 2042 2043 BlockDecl *getBlockDecl() { return TheBlock; } 2044 2045 // Convenience functions for probing the underlying BlockDecl. 2046 SourceLocation getCaretLocation() const; 2047 const Stmt *getBody() const; 2048 Stmt *getBody(); 2049 2050 virtual SourceRange getSourceRange() const { 2051 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2052 } 2053 2054 /// getFunctionType - Return the underlying function type for this block. 2055 const FunctionType *getFunctionType() const; 2056 2057 static bool classof(const Stmt *T) { 2058 return T->getStmtClass() == BlockExprClass; 2059 } 2060 static bool classof(const BlockExpr *) { return true; } 2061 2062 // Iterators 2063 virtual child_iterator child_begin(); 2064 virtual child_iterator child_end(); 2065 2066 virtual void EmitImpl(llvm::Serializer& S) const; 2067 static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2068}; 2069 2070/// BlockDeclRefExpr - A reference to a declared variable, function, 2071/// enum, etc. 2072class BlockDeclRefExpr : public Expr { 2073 ValueDecl *D; 2074 SourceLocation Loc; 2075 bool IsByRef; 2076public: 2077 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 2078 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 2079 2080 ValueDecl *getDecl() { return D; } 2081 const ValueDecl *getDecl() const { return D; } 2082 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2083 2084 bool isByRef() const { return IsByRef; } 2085 2086 static bool classof(const Stmt *T) { 2087 return T->getStmtClass() == BlockDeclRefExprClass; 2088 } 2089 static bool classof(const BlockDeclRefExpr *) { return true; } 2090 2091 // Iterators 2092 virtual child_iterator child_begin(); 2093 virtual child_iterator child_end(); 2094 2095 virtual void EmitImpl(llvm::Serializer& S) const; 2096 static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2097}; 2098 2099} // end namespace clang 2100 2101#endif 2102