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