Expr.h revision b648023da23e8b227cdda57a241db4c6f368726b
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 "llvm/ADT/StringRef.h" 24#include <vector> 25 26namespace clang { 27 class ASTContext; 28 class APValue; 29 class Decl; 30 class IdentifierInfo; 31 class ParmVarDecl; 32 class NamedDecl; 33 class ValueDecl; 34 class BlockDecl; 35 class CXXOperatorCallExpr; 36 class CXXMemberCallExpr; 37 38/// Expr - This represents one expression. Note that Expr's are subclasses of 39/// Stmt. This allows an expression to be transparently used any place a Stmt 40/// is required. 41/// 42class Expr : public Stmt { 43 QualType TR; 44 45protected: 46 /// TypeDependent - Whether this expression is type-dependent 47 /// (C++ [temp.dep.expr]). 48 bool TypeDependent : 1; 49 50 /// ValueDependent - Whether this expression is value-dependent 51 /// (C++ [temp.dep.constexpr]). 52 bool ValueDependent : 1; 53 54 // FIXME: Eventually, this constructor should go away and we should 55 // require every subclass to provide type/value-dependence 56 // information. 57 Expr(StmtClass SC, QualType T) 58 : Stmt(SC), TypeDependent(false), ValueDependent(false) { 59 setType(T); 60 } 61 62 Expr(StmtClass SC, QualType T, bool TD, bool VD) 63 : Stmt(SC), TypeDependent(TD), ValueDependent(VD) { 64 setType(T); 65 } 66 67 /// \brief Construct an empty expression. 68 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { } 69 70public: 71 /// \brief Increases the reference count for this expression. 72 /// 73 /// Invoke the Retain() operation when this expression 74 /// is being shared by another owner. 75 Expr *Retain() { 76 Stmt::Retain(); 77 return this; 78 } 79 80 QualType getType() const { return TR; } 81 void setType(QualType t) { 82 // In C++, the type of an expression is always adjusted so that it 83 // will not have reference type an expression will never have 84 // reference type (C++ [expr]p6). Use 85 // QualType::getNonReferenceType() to retrieve the non-reference 86 // type. Additionally, inspect Expr::isLvalue to determine whether 87 // an expression that is adjusted in this manner should be 88 // considered an lvalue. 89 assert((TR.isNull() || !TR->isReferenceType()) && 90 "Expressions can't have reference type"); 91 92 TR = t; 93 } 94 95 /// isValueDependent - Determines whether this expression is 96 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 97 /// array bound of "Chars" in the following example is 98 /// value-dependent. 99 /// @code 100 /// template<int Size, char (&Chars)[Size]> struct meta_string; 101 /// @endcode 102 bool isValueDependent() const { return ValueDependent; } 103 104 /// \brief Set whether this expression is value-dependent or not. 105 void setValueDependent(bool VD) { ValueDependent = VD; } 106 107 /// isTypeDependent - Determines whether this expression is 108 /// type-dependent (C++ [temp.dep.expr]), which means that its type 109 /// could change from one template instantiation to the next. For 110 /// example, the expressions "x" and "x + y" are type-dependent in 111 /// the following code, but "y" is not type-dependent: 112 /// @code 113 /// template<typename T> 114 /// void add(T x, int y) { 115 /// x + y; 116 /// } 117 /// @endcode 118 bool isTypeDependent() const { return TypeDependent; } 119 120 /// \brief Set whether this expression is type-dependent or not. 121 void setTypeDependent(bool TD) { TypeDependent = TD; } 122 123 /// SourceLocation tokens are not useful in isolation - they are low level 124 /// value objects created/interpreted by SourceManager. We assume AST 125 /// clients will have a pointer to the respective SourceManager. 126 virtual SourceRange getSourceRange() const = 0; 127 128 /// getExprLoc - Return the preferred location for the arrow when diagnosing 129 /// a problem with a generic expression. 130 virtual SourceLocation getExprLoc() const { return getLocStart(); } 131 132 /// isUnusedResultAWarning - Return true if this immediate expression should 133 /// be warned about if the result is unused. If so, fill in Loc and Ranges 134 /// with location to warn on and the source range[s] to report with the 135 /// warning. 136 bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, 137 SourceRange &R2) const; 138 139 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 140 /// incomplete type other than void. Nonarray expressions that can be lvalues: 141 /// - name, where name must be a variable 142 /// - e[i] 143 /// - (e), where e must be an lvalue 144 /// - e.name, where e must be an lvalue 145 /// - e->name 146 /// - *e, the type of e cannot be a function type 147 /// - string-constant 148 /// - reference type [C++ [expr]] 149 /// - b ? x : y, where x and y are lvalues of suitable types [C++] 150 /// 151 enum isLvalueResult { 152 LV_Valid, 153 LV_NotObjectType, 154 LV_IncompleteVoidType, 155 LV_DuplicateVectorComponents, 156 LV_InvalidExpression, 157 LV_MemberFunction 158 }; 159 isLvalueResult isLvalue(ASTContext &Ctx) const; 160 161 // Same as above, but excluding checks for non-object and void types in C 162 isLvalueResult isLvalueInternal(ASTContext &Ctx) const; 163 164 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 165 /// does not have an incomplete type, does not have a const-qualified type, 166 /// and if it is a structure or union, does not have any member (including, 167 /// recursively, any member or element of all contained aggregates or unions) 168 /// with a const-qualified type. 169 /// 170 /// \param Loc [in] [out] - A source location which *may* be filled 171 /// in with the location of the expression making this a 172 /// non-modifiable lvalue, if specified. 173 enum isModifiableLvalueResult { 174 MLV_Valid, 175 MLV_NotObjectType, 176 MLV_IncompleteVoidType, 177 MLV_DuplicateVectorComponents, 178 MLV_InvalidExpression, 179 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 180 MLV_IncompleteType, 181 MLV_ConstQualified, 182 MLV_ArrayType, 183 MLV_NotBlockQualified, 184 MLV_ReadonlyProperty, 185 MLV_NoSetterProperty, 186 MLV_MemberFunction 187 }; 188 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, 189 SourceLocation *Loc = 0) const; 190 191 /// \brief If this expression refers to a bit-field, retrieve the 192 /// declaration of that bit-field. 193 FieldDecl *getBitField(); 194 195 const FieldDecl *getBitField() const { 196 return const_cast<Expr*>(this)->getBitField(); 197 } 198 199 /// isIntegerConstantExpr - Return true if this expression is a valid integer 200 /// constant expression, and, if so, return its value in Result. If not a 201 /// valid i-c-e, return false and fill in Loc (if specified) with the location 202 /// of the invalid expression. 203 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 204 SourceLocation *Loc = 0, 205 bool isEvaluated = true) const; 206 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 207 llvm::APSInt X; 208 return isIntegerConstantExpr(X, Ctx, Loc); 209 } 210 /// isConstantInitializer - Returns true if this expression is a constant 211 /// initializer, which can be emitted at compile-time. 212 bool isConstantInitializer(ASTContext &Ctx) const; 213 214 /// EvalResult is a struct with detailed info about an evaluated expression. 215 struct EvalResult { 216 /// Val - This is the value the expression can be folded to. 217 APValue Val; 218 219 /// HasSideEffects - Whether the evaluated expression has side effects. 220 /// For example, (f() && 0) can be folded, but it still has side effects. 221 bool HasSideEffects; 222 223 /// Diag - If the expression is unfoldable, then Diag contains a note 224 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 225 /// position for the error, and DiagExpr is the expression that caused 226 /// the error. 227 /// If the expression is foldable, but not an integer constant expression, 228 /// Diag contains a note diagnostic that describes why it isn't an integer 229 /// constant expression. If the expression *is* an integer constant 230 /// expression, then Diag will be zero. 231 unsigned Diag; 232 const Expr *DiagExpr; 233 SourceLocation DiagLoc; 234 235 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 236 }; 237 238 /// Evaluate - Return true if this is a constant which we can fold using 239 /// any crazy technique (that has nothing to do with language standards) that 240 /// we want to. If this function returns true, it returns the folded constant 241 /// in Result. 242 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const; 243 244 /// isEvaluatable - Call Evaluate to see if this expression can be constant 245 /// folded, but discard the result. 246 bool isEvaluatable(ASTContext &Ctx) const; 247 248 /// EvaluateAsInt - Call Evaluate and return the folded integer. This 249 /// must be called on an expression that constant folds to an integer. 250 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const; 251 252 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue 253 /// with link time known address. 254 bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const; 255 256 /// EvaluateAsAnyLValue - The same as EvaluateAsLValue, except that it 257 /// also succeeds on stack based, immutable address lvalues. 258 bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const; 259 260 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 261 /// integer constant expression with the value zero, or if this is one that is 262 /// cast to void*. 263 bool isNullPointerConstant(ASTContext &Ctx) const; 264 265 /// isOBJCGCCandidate - Return true if this expression may be used in a read/ 266 /// write barrier. 267 bool isOBJCGCCandidate(ASTContext &Ctx) const; 268 269 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 270 /// its subexpression. If that subexpression is also a ParenExpr, 271 /// then this method recursively returns its subexpression, and so forth. 272 /// Otherwise, the method returns the current Expr. 273 Expr* IgnoreParens(); 274 275 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 276 /// or CastExprs, returning their operand. 277 Expr *IgnoreParenCasts(); 278 279 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the 280 /// value (including ptr->int casts of the same size). Strip off any 281 /// ParenExpr or CastExprs, returning their operand. 282 Expr *IgnoreParenNoopCasts(ASTContext &Ctx); 283 284 const Expr* IgnoreParens() const { 285 return const_cast<Expr*>(this)->IgnoreParens(); 286 } 287 const Expr *IgnoreParenCasts() const { 288 return const_cast<Expr*>(this)->IgnoreParenCasts(); 289 } 290 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const { 291 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx); 292 } 293 294 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 295 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 296 297 static bool classof(const Stmt *T) { 298 return T->getStmtClass() >= firstExprConstant && 299 T->getStmtClass() <= lastExprConstant; 300 } 301 static bool classof(const Expr *) { return true; } 302}; 303 304 305//===----------------------------------------------------------------------===// 306// Primary Expressions. 307//===----------------------------------------------------------------------===// 308 309/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 310/// enum, etc. 311class DeclRefExpr : public Expr { 312 NamedDecl *D; 313 SourceLocation Loc; 314 315protected: 316 // FIXME: Eventually, this constructor will go away and all subclasses 317 // will have to provide the type- and value-dependent flags. 318 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) : 319 Expr(SC, t), D(d), Loc(l) {} 320 321 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l, bool TD, 322 bool VD) : 323 Expr(SC, t, TD, VD), D(d), Loc(l) {} 324 325public: 326 // FIXME: Eventually, this constructor will go away and all clients 327 // will have to provide the type- and value-dependent flags. 328 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) : 329 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 330 331 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) : 332 Expr(DeclRefExprClass, t, TD, VD), D(d), Loc(l) {} 333 334 /// \brief Construct an empty declaration reference expression. 335 explicit DeclRefExpr(EmptyShell Empty) 336 : Expr(DeclRefExprClass, Empty) { } 337 338 NamedDecl *getDecl() { return D; } 339 const NamedDecl *getDecl() const { return D; } 340 void setDecl(NamedDecl *NewD) { D = NewD; } 341 342 SourceLocation getLocation() const { return Loc; } 343 void setLocation(SourceLocation L) { Loc = L; } 344 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 345 346 static bool classof(const Stmt *T) { 347 return T->getStmtClass() == DeclRefExprClass || 348 T->getStmtClass() == CXXConditionDeclExprClass || 349 T->getStmtClass() == QualifiedDeclRefExprClass; 350 } 351 static bool classof(const DeclRefExpr *) { return true; } 352 353 // Iterators 354 virtual child_iterator child_begin(); 355 virtual child_iterator child_end(); 356}; 357 358/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 359class PredefinedExpr : public Expr { 360public: 361 enum IdentType { 362 Func, 363 Function, 364 PrettyFunction 365 }; 366 367private: 368 SourceLocation Loc; 369 IdentType Type; 370public: 371 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 372 : Expr(PredefinedExprClass, type, type->isDependentType(), 373 type->isDependentType()), Loc(l), Type(IT) {} 374 375 /// \brief Construct an empty predefined expression. 376 explicit PredefinedExpr(EmptyShell Empty) 377 : Expr(PredefinedExprClass, Empty) { } 378 379 IdentType getIdentType() const { return Type; } 380 void setIdentType(IdentType IT) { Type = IT; } 381 382 SourceLocation getLocation() const { return Loc; } 383 void setLocation(SourceLocation L) { Loc = L; } 384 385 static std::string ComputeName(ASTContext &Context, IdentType IT, 386 const Decl *CurrentDecl); 387 388 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 389 390 static bool classof(const Stmt *T) { 391 return T->getStmtClass() == PredefinedExprClass; 392 } 393 static bool classof(const PredefinedExpr *) { return true; } 394 395 // Iterators 396 virtual child_iterator child_begin(); 397 virtual child_iterator child_end(); 398}; 399 400class IntegerLiteral : public Expr { 401 llvm::APInt Value; 402 SourceLocation Loc; 403public: 404 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 405 // or UnsignedLongLongTy 406 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 407 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 408 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 409 } 410 411 /// \brief Construct an empty integer literal. 412 explicit IntegerLiteral(EmptyShell Empty) 413 : Expr(IntegerLiteralClass, Empty) { } 414 415 const llvm::APInt &getValue() const { return Value; } 416 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 417 418 /// \brief Retrieve the location of the literal. 419 SourceLocation getLocation() const { return Loc; } 420 421 void setValue(const llvm::APInt &Val) { Value = Val; } 422 void setLocation(SourceLocation Location) { Loc = Location; } 423 424 static bool classof(const Stmt *T) { 425 return T->getStmtClass() == IntegerLiteralClass; 426 } 427 static bool classof(const IntegerLiteral *) { return true; } 428 429 // Iterators 430 virtual child_iterator child_begin(); 431 virtual child_iterator child_end(); 432}; 433 434class CharacterLiteral : public Expr { 435 unsigned Value; 436 SourceLocation Loc; 437 bool IsWide; 438public: 439 // type should be IntTy 440 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 441 : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) { 442 } 443 444 /// \brief Construct an empty character literal. 445 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 446 447 SourceLocation getLocation() const { return Loc; } 448 bool isWide() const { return IsWide; } 449 450 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 451 452 unsigned getValue() const { return Value; } 453 454 void setLocation(SourceLocation Location) { Loc = Location; } 455 void setWide(bool W) { IsWide = W; } 456 void setValue(unsigned Val) { Value = Val; } 457 458 static bool classof(const Stmt *T) { 459 return T->getStmtClass() == CharacterLiteralClass; 460 } 461 static bool classof(const CharacterLiteral *) { return true; } 462 463 // Iterators 464 virtual child_iterator child_begin(); 465 virtual child_iterator child_end(); 466}; 467 468class FloatingLiteral : public Expr { 469 llvm::APFloat Value; 470 bool IsExact : 1; 471 SourceLocation Loc; 472public: 473 FloatingLiteral(const llvm::APFloat &V, bool isexact, 474 QualType Type, SourceLocation L) 475 : Expr(FloatingLiteralClass, Type), Value(V), IsExact(isexact), Loc(L) {} 476 477 /// \brief Construct an empty floating-point literal. 478 explicit FloatingLiteral(EmptyShell Empty) 479 : Expr(FloatingLiteralClass, Empty), Value(0.0) { } 480 481 const llvm::APFloat &getValue() const { return Value; } 482 void setValue(const llvm::APFloat &Val) { Value = Val; } 483 484 bool isExact() const { return IsExact; } 485 void setExact(bool E) { IsExact = E; } 486 487 /// getValueAsApproximateDouble - This returns the value as an inaccurate 488 /// double. Note that this may cause loss of precision, but is useful for 489 /// debugging dumps, etc. 490 double getValueAsApproximateDouble() const; 491 492 SourceLocation getLocation() const { return Loc; } 493 void setLocation(SourceLocation L) { Loc = L; } 494 495 // FIXME: The logic for computing the value of a predefined expr should go 496 // into a method here that takes the inner-most code decl (a block, function 497 // or objc method) that the expr lives in. This would allow sema and codegen 498 // to be consistent for things like sizeof(__func__) etc. 499 500 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 501 502 static bool classof(const Stmt *T) { 503 return T->getStmtClass() == FloatingLiteralClass; 504 } 505 static bool classof(const FloatingLiteral *) { return true; } 506 507 // Iterators 508 virtual child_iterator child_begin(); 509 virtual child_iterator child_end(); 510}; 511 512/// ImaginaryLiteral - We support imaginary integer and floating point literals, 513/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 514/// IntegerLiteral classes. Instances of this class always have a Complex type 515/// whose element type matches the subexpression. 516/// 517class ImaginaryLiteral : public Expr { 518 Stmt *Val; 519public: 520 ImaginaryLiteral(Expr *val, QualType Ty) 521 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 522 523 /// \brief Build an empty imaginary literal. 524 explicit ImaginaryLiteral(EmptyShell Empty) 525 : Expr(ImaginaryLiteralClass, Empty) { } 526 527 const Expr *getSubExpr() const { return cast<Expr>(Val); } 528 Expr *getSubExpr() { return cast<Expr>(Val); } 529 void setSubExpr(Expr *E) { Val = E; } 530 531 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 532 static bool classof(const Stmt *T) { 533 return T->getStmtClass() == ImaginaryLiteralClass; 534 } 535 static bool classof(const ImaginaryLiteral *) { return true; } 536 537 // Iterators 538 virtual child_iterator child_begin(); 539 virtual child_iterator child_end(); 540}; 541 542/// StringLiteral - This represents a string literal expression, e.g. "foo" 543/// or L"bar" (wide strings). The actual string is returned by getStrData() 544/// is NOT null-terminated, and the length of the string is determined by 545/// calling getByteLength(). The C type for a string is always a 546/// ConstantArrayType. In C++, the char type is const qualified, in C it is 547/// not. 548/// 549/// Note that strings in C can be formed by concatenation of multiple string 550/// literal pptokens in translation phase #6. This keeps track of the locations 551/// of each of these pieces. 552/// 553/// Strings in C can also be truncated and extended by assigning into arrays, 554/// e.g. with constructs like: 555/// char X[2] = "foobar"; 556/// In this case, getByteLength() will return 6, but the string literal will 557/// have type "char[2]". 558class StringLiteral : public Expr { 559 const char *StrData; 560 unsigned ByteLength; 561 bool IsWide; 562 unsigned NumConcatenated; 563 SourceLocation TokLocs[1]; 564 565 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {} 566 567protected: 568 virtual void DoDestroy(ASTContext &C); 569 570public: 571 /// This is the "fully general" constructor that allows representation of 572 /// strings formed from multiple concatenated tokens. 573 static StringLiteral *Create(ASTContext &C, const char *StrData, 574 unsigned ByteLength, bool Wide, QualType Ty, 575 const SourceLocation *Loc, unsigned NumStrs); 576 577 /// Simple constructor for string literals made from one token. 578 static StringLiteral *Create(ASTContext &C, const char *StrData, 579 unsigned ByteLength, 580 bool Wide, QualType Ty, SourceLocation Loc) { 581 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1); 582 } 583 584 /// \brief Construct an empty string literal. 585 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 586 587 llvm::StringRef getString() const { 588 return llvm::StringRef(StrData, ByteLength); 589 } 590 // FIXME: These are deprecated, replace with StringRef. 591 const char *getStrData() const { return StrData; } 592 unsigned getByteLength() const { return ByteLength; } 593 594 /// \brief Sets the string data to the given string data. 595 void setString(ASTContext &C, llvm::StringRef Str); 596 597 bool isWide() const { return IsWide; } 598 void setWide(bool W) { IsWide = W; } 599 600 bool containsNonAsciiOrNull() const { 601 llvm::StringRef Str = getString(); 602 for (unsigned i = 0, e = Str.size(); i != e; ++i) 603 if (!isascii(Str[i]) || !Str[i]) 604 return true; 605 return false; 606 } 607 /// getNumConcatenated - Get the number of string literal tokens that were 608 /// concatenated in translation phase #6 to form this string literal. 609 unsigned getNumConcatenated() const { return NumConcatenated; } 610 611 SourceLocation getStrTokenLoc(unsigned TokNum) const { 612 assert(TokNum < NumConcatenated && "Invalid tok number"); 613 return TokLocs[TokNum]; 614 } 615 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 616 assert(TokNum < NumConcatenated && "Invalid tok number"); 617 TokLocs[TokNum] = L; 618 } 619 620 typedef const SourceLocation *tokloc_iterator; 621 tokloc_iterator tokloc_begin() const { return TokLocs; } 622 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 623 624 virtual SourceRange getSourceRange() const { 625 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 626 } 627 static bool classof(const Stmt *T) { 628 return T->getStmtClass() == StringLiteralClass; 629 } 630 static bool classof(const StringLiteral *) { return true; } 631 632 // Iterators 633 virtual child_iterator child_begin(); 634 virtual child_iterator child_end(); 635}; 636 637/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 638/// AST node is only formed if full location information is requested. 639class ParenExpr : public Expr { 640 SourceLocation L, R; 641 Stmt *Val; 642public: 643 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 644 : Expr(ParenExprClass, val->getType(), 645 val->isTypeDependent(), val->isValueDependent()), 646 L(l), R(r), Val(val) {} 647 648 /// \brief Construct an empty parenthesized expression. 649 explicit ParenExpr(EmptyShell Empty) 650 : Expr(ParenExprClass, Empty) { } 651 652 const Expr *getSubExpr() const { return cast<Expr>(Val); } 653 Expr *getSubExpr() { return cast<Expr>(Val); } 654 void setSubExpr(Expr *E) { Val = E; } 655 656 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 657 658 /// \brief Get the location of the left parentheses '('. 659 SourceLocation getLParen() const { return L; } 660 void setLParen(SourceLocation Loc) { L = Loc; } 661 662 /// \brief Get the location of the right parentheses ')'. 663 SourceLocation getRParen() const { return R; } 664 void setRParen(SourceLocation Loc) { R = Loc; } 665 666 static bool classof(const Stmt *T) { 667 return T->getStmtClass() == ParenExprClass; 668 } 669 static bool classof(const ParenExpr *) { return true; } 670 671 // Iterators 672 virtual child_iterator child_begin(); 673 virtual child_iterator child_end(); 674}; 675 676 677/// UnaryOperator - This represents the unary-expression's (except sizeof and 678/// alignof), the postinc/postdec operators from postfix-expression, and various 679/// extensions. 680/// 681/// Notes on various nodes: 682/// 683/// Real/Imag - These return the real/imag part of a complex operand. If 684/// applied to a non-complex value, the former returns its operand and the 685/// later returns zero in the type of the operand. 686/// 687/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 688/// subexpression is a compound literal with the various MemberExpr and 689/// ArraySubscriptExpr's applied to it. 690/// 691class UnaryOperator : public Expr { 692public: 693 // Note that additions to this should also update the StmtVisitor class. 694 enum Opcode { 695 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 696 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 697 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 698 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 699 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 700 Real, Imag, // "__real expr"/"__imag expr" Extension. 701 Extension, // __extension__ marker. 702 OffsetOf // __builtin_offsetof 703 }; 704private: 705 Stmt *Val; 706 Opcode Opc; 707 SourceLocation Loc; 708public: 709 710 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 711 : Expr(UnaryOperatorClass, type, 712 input->isTypeDependent() && opc != OffsetOf, 713 input->isValueDependent()), 714 Val(input), Opc(opc), Loc(l) {} 715 716 /// \brief Build an empty unary operator. 717 explicit UnaryOperator(EmptyShell Empty) 718 : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { } 719 720 Opcode getOpcode() const { return Opc; } 721 void setOpcode(Opcode O) { Opc = O; } 722 723 Expr *getSubExpr() const { return cast<Expr>(Val); } 724 void setSubExpr(Expr *E) { Val = E; } 725 726 /// getOperatorLoc - Return the location of the operator. 727 SourceLocation getOperatorLoc() const { return Loc; } 728 void setOperatorLoc(SourceLocation L) { Loc = L; } 729 730 /// isPostfix - Return true if this is a postfix operation, like x++. 731 static bool isPostfix(Opcode Op) { 732 return Op == PostInc || Op == PostDec; 733 } 734 735 /// isPostfix - Return true if this is a prefix operation, like --x. 736 static bool isPrefix(Opcode Op) { 737 return Op == PreInc || Op == PreDec; 738 } 739 740 bool isPrefix() const { return isPrefix(Opc); } 741 bool isPostfix() const { return isPostfix(Opc); } 742 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 743 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 744 bool isOffsetOfOp() const { return Opc == OffsetOf; } 745 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 746 bool isArithmeticOp() const { return isArithmeticOp(Opc); } 747 748 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 749 /// corresponds to, e.g. "sizeof" or "[pre]++" 750 static const char *getOpcodeStr(Opcode Op); 751 752 /// \brief Retrieve the unary opcode that corresponds to the given 753 /// overloaded operator. 754 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 755 756 /// \brief Retrieve the overloaded operator kind that corresponds to 757 /// the given unary opcode. 758 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 759 760 virtual SourceRange getSourceRange() const { 761 if (isPostfix()) 762 return SourceRange(Val->getLocStart(), Loc); 763 else 764 return SourceRange(Loc, Val->getLocEnd()); 765 } 766 virtual SourceLocation getExprLoc() const { return Loc; } 767 768 static bool classof(const Stmt *T) { 769 return T->getStmtClass() == UnaryOperatorClass; 770 } 771 static bool classof(const UnaryOperator *) { return true; } 772 773 // Iterators 774 virtual child_iterator child_begin(); 775 virtual child_iterator child_end(); 776}; 777 778/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 779/// types and expressions. 780class SizeOfAlignOfExpr : public Expr { 781 bool isSizeof : 1; // true if sizeof, false if alignof. 782 bool isType : 1; // true if operand is a type, false if an expression 783 union { 784 void *Ty; 785 Stmt *Ex; 786 } Argument; 787 SourceLocation OpLoc, RParenLoc; 788 789protected: 790 virtual void DoDestroy(ASTContext& C); 791 792public: 793 SizeOfAlignOfExpr(bool issizeof, QualType T, 794 QualType resultType, SourceLocation op, 795 SourceLocation rp) : 796 Expr(SizeOfAlignOfExprClass, resultType, 797 false, // Never type-dependent (C++ [temp.dep.expr]p3). 798 // Value-dependent if the argument is type-dependent. 799 T->isDependentType()), 800 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) { 801 Argument.Ty = T.getAsOpaquePtr(); 802 } 803 804 SizeOfAlignOfExpr(bool issizeof, Expr *E, 805 QualType resultType, SourceLocation op, 806 SourceLocation rp) : 807 Expr(SizeOfAlignOfExprClass, resultType, 808 false, // Never type-dependent (C++ [temp.dep.expr]p3). 809 // Value-dependent if the argument is type-dependent. 810 E->isTypeDependent()), 811 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) { 812 Argument.Ex = E; 813 } 814 815 /// \brief Construct an empty sizeof/alignof expression. 816 explicit SizeOfAlignOfExpr(EmptyShell Empty) 817 : Expr(SizeOfAlignOfExprClass, Empty) { } 818 819 bool isSizeOf() const { return isSizeof; } 820 void setSizeof(bool S) { isSizeof = S; } 821 822 bool isArgumentType() const { return isType; } 823 QualType getArgumentType() const { 824 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 825 return QualType::getFromOpaquePtr(Argument.Ty); 826 } 827 Expr *getArgumentExpr() { 828 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 829 return static_cast<Expr*>(Argument.Ex); 830 } 831 const Expr *getArgumentExpr() const { 832 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 833 } 834 835 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 836 void setArgument(QualType T) { 837 Argument.Ty = T.getAsOpaquePtr(); 838 isType = true; 839 } 840 841 /// Gets the argument type, or the type of the argument expression, whichever 842 /// is appropriate. 843 QualType getTypeOfArgument() const { 844 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 845 } 846 847 SourceLocation getOperatorLoc() const { return OpLoc; } 848 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 849 850 SourceLocation getRParenLoc() const { return RParenLoc; } 851 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 852 853 virtual SourceRange getSourceRange() const { 854 return SourceRange(OpLoc, RParenLoc); 855 } 856 857 static bool classof(const Stmt *T) { 858 return T->getStmtClass() == SizeOfAlignOfExprClass; 859 } 860 static bool classof(const SizeOfAlignOfExpr *) { return true; } 861 862 // Iterators 863 virtual child_iterator child_begin(); 864 virtual child_iterator child_end(); 865}; 866 867//===----------------------------------------------------------------------===// 868// Postfix Operators. 869//===----------------------------------------------------------------------===// 870 871/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 872class ArraySubscriptExpr : public Expr { 873 enum { LHS, RHS, END_EXPR=2 }; 874 Stmt* SubExprs[END_EXPR]; 875 SourceLocation RBracketLoc; 876public: 877 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 878 SourceLocation rbracketloc) 879 : Expr(ArraySubscriptExprClass, t, 880 lhs->isTypeDependent() || rhs->isTypeDependent(), 881 lhs->isValueDependent() || rhs->isValueDependent()), 882 RBracketLoc(rbracketloc) { 883 SubExprs[LHS] = lhs; 884 SubExprs[RHS] = rhs; 885 } 886 887 /// \brief Create an empty array subscript expression. 888 explicit ArraySubscriptExpr(EmptyShell Shell) 889 : Expr(ArraySubscriptExprClass, Shell) { } 890 891 /// An array access can be written A[4] or 4[A] (both are equivalent). 892 /// - getBase() and getIdx() always present the normalized view: A[4]. 893 /// In this case getBase() returns "A" and getIdx() returns "4". 894 /// - getLHS() and getRHS() present the syntactic view. e.g. for 895 /// 4[A] getLHS() returns "4". 896 /// Note: Because vector element access is also written A[4] we must 897 /// predicate the format conversion in getBase and getIdx only on the 898 /// the type of the RHS, as it is possible for the LHS to be a vector of 899 /// integer type 900 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 901 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 902 void setLHS(Expr *E) { SubExprs[LHS] = E; } 903 904 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 905 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 906 void setRHS(Expr *E) { SubExprs[RHS] = E; } 907 908 Expr *getBase() { 909 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 910 } 911 912 const Expr *getBase() const { 913 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 914 } 915 916 Expr *getIdx() { 917 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 918 } 919 920 const Expr *getIdx() const { 921 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 922 } 923 924 virtual SourceRange getSourceRange() const { 925 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 926 } 927 928 SourceLocation getRBracketLoc() const { return RBracketLoc; } 929 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 930 931 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 932 933 static bool classof(const Stmt *T) { 934 return T->getStmtClass() == ArraySubscriptExprClass; 935 } 936 static bool classof(const ArraySubscriptExpr *) { return true; } 937 938 // Iterators 939 virtual child_iterator child_begin(); 940 virtual child_iterator child_end(); 941}; 942 943 944/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 945/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 946/// while its subclasses may represent alternative syntax that (semantically) 947/// results in a function call. For example, CXXOperatorCallExpr is 948/// a subclass for overloaded operator calls that use operator syntax, e.g., 949/// "str1 + str2" to resolve to a function call. 950class CallExpr : public Expr { 951 enum { FN=0, ARGS_START=1 }; 952 Stmt **SubExprs; 953 unsigned NumArgs; 954 SourceLocation RParenLoc; 955 956protected: 957 // This version of the constructor is for derived classes. 958 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, 959 QualType t, SourceLocation rparenloc); 960 961 virtual void DoDestroy(ASTContext& C); 962 963public: 964 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 965 SourceLocation rparenloc); 966 967 /// \brief Build an empty call expression. 968 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 969 970 ~CallExpr() {} 971 972 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 973 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 974 void setCallee(Expr *F) { SubExprs[FN] = F; } 975 976 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 977 FunctionDecl *getDirectCallee(); 978 979 /// getNumArgs - Return the number of actual arguments to this call. 980 /// 981 unsigned getNumArgs() const { return NumArgs; } 982 983 /// getArg - Return the specified argument. 984 Expr *getArg(unsigned Arg) { 985 assert(Arg < NumArgs && "Arg access out of range!"); 986 return cast<Expr>(SubExprs[Arg+ARGS_START]); 987 } 988 const Expr *getArg(unsigned Arg) const { 989 assert(Arg < NumArgs && "Arg access out of range!"); 990 return cast<Expr>(SubExprs[Arg+ARGS_START]); 991 } 992 993 /// setArg - Set the specified argument. 994 void setArg(unsigned Arg, Expr *ArgExpr) { 995 assert(Arg < NumArgs && "Arg access out of range!"); 996 SubExprs[Arg+ARGS_START] = ArgExpr; 997 } 998 999 /// setNumArgs - This changes the number of arguments present in this call. 1000 /// Any orphaned expressions are deleted by this, and any new operands are set 1001 /// to null. 1002 void setNumArgs(ASTContext& C, unsigned NumArgs); 1003 1004 typedef ExprIterator arg_iterator; 1005 typedef ConstExprIterator const_arg_iterator; 1006 1007 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 1008 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 1009 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 1010 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 1011 1012 /// getNumCommas - Return the number of commas that must have been present in 1013 /// this function call. 1014 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1015 1016 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1017 /// not, return 0. 1018 unsigned isBuiltinCall(ASTContext &Context) const; 1019 1020 /// getCallReturnType - Get the return type of the call expr. This is not 1021 /// always the type of the expr itself, if the return type is a reference 1022 /// type. 1023 QualType getCallReturnType() const; 1024 1025 SourceLocation getRParenLoc() const { return RParenLoc; } 1026 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1027 1028 virtual SourceRange getSourceRange() const { 1029 return SourceRange(getCallee()->getLocStart(), RParenLoc); 1030 } 1031 1032 static bool classof(const Stmt *T) { 1033 return T->getStmtClass() == CallExprClass || 1034 T->getStmtClass() == CXXOperatorCallExprClass || 1035 T->getStmtClass() == CXXMemberCallExprClass; 1036 } 1037 static bool classof(const CallExpr *) { return true; } 1038 static bool classof(const CXXOperatorCallExpr *) { return true; } 1039 static bool classof(const CXXMemberCallExpr *) { return true; } 1040 1041 // Iterators 1042 virtual child_iterator child_begin(); 1043 virtual child_iterator child_end(); 1044}; 1045 1046/// \brief Represents the qualifier that may precede a C++ name, e.g., the 1047/// "std::" in "std::sort". 1048struct NameQualifier { 1049 /// \brief The nested name specifier. 1050 NestedNameSpecifier *NNS; 1051 1052 /// \brief The source range covered by the nested name specifier. 1053 SourceRange Range; 1054}; 1055 1056/// \brief Represents an explicit template argument list in C++, e.g., 1057/// the "<int>" in "sort<int>". 1058struct ExplicitTemplateArgumentList { 1059 /// \brief The source location of the left angle bracket ('<'); 1060 SourceLocation LAngleLoc; 1061 1062 /// \brief The source location of the right angle bracket ('>'); 1063 SourceLocation RAngleLoc; 1064 1065 /// \brief The number of template arguments in TemplateArgs. 1066 /// The actual template arguments (if any) are stored after the 1067 /// ExplicitTemplateArgumentList structure. 1068 unsigned NumTemplateArgs; 1069 1070 /// \brief Retrieve the template arguments 1071 TemplateArgument *getTemplateArgs() { 1072 return reinterpret_cast<TemplateArgument *> (this + 1); 1073 } 1074 1075 /// \brief Retrieve the template arguments 1076 const TemplateArgument *getTemplateArgs() const { 1077 return reinterpret_cast<const TemplateArgument *> (this + 1); 1078 } 1079}; 1080 1081/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1082/// 1083class MemberExpr : public Expr { 1084 /// Base - the expression for the base pointer or structure references. In 1085 /// X.F, this is "X". 1086 Stmt *Base; 1087 1088 /// MemberDecl - This is the decl being referenced by the field/member name. 1089 /// In X.F, this is the decl referenced by F. 1090 NamedDecl *MemberDecl; 1091 1092 /// MemberLoc - This is the location of the member name. 1093 SourceLocation MemberLoc; 1094 1095 /// IsArrow - True if this is "X->F", false if this is "X.F". 1096 bool IsArrow : 1; 1097 1098 /// \brief True if this member expression used a nested-name-specifier to 1099 /// refer to the member, e.g., "x->Base::f". When true, a NameQualifier 1100 /// structure is allocated immediately after the MemberExpr. 1101 bool HasQualifier : 1; 1102 1103 /// \brief True if this member expression specified a template argument list 1104 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1105 /// structure (and its TemplateArguments) are allocated immediately after 1106 /// the MemberExpr or, if the member expression also has a qualifier, after 1107 /// the NameQualifier structure. 1108 bool HasExplicitTemplateArgumentList : 1; 1109 1110 /// \brief Retrieve the qualifier that preceded the member name, if any. 1111 NameQualifier *getMemberQualifier() { 1112 if (!HasQualifier) 1113 return 0; 1114 1115 return reinterpret_cast<NameQualifier *> (this + 1); 1116 } 1117 1118 /// \brief Retrieve the qualifier that preceded the member name, if any. 1119 const NameQualifier *getMemberQualifier() const { 1120 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1121 } 1122 1123 /// \brief Retrieve the explicit template argument list that followed the 1124 /// member template name, if any. 1125 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1126 if (!HasExplicitTemplateArgumentList) 1127 return 0; 1128 1129 if (!HasQualifier) 1130 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1131 1132 return reinterpret_cast<ExplicitTemplateArgumentList *>( 1133 getMemberQualifier() + 1); 1134 } 1135 1136 /// \brief Retrieve the explicit template argument list that followed the 1137 /// member template name, if any. 1138 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1139 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList(); 1140 } 1141 1142 MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual, 1143 SourceRange qualrange, NamedDecl *memberdecl, SourceLocation l, 1144 bool has_explicit, SourceLocation langle, 1145 const TemplateArgument *targs, unsigned numtargs, 1146 SourceLocation rangle, QualType ty); 1147 1148public: 1149 MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l, 1150 QualType ty) 1151 : Expr(MemberExprClass, ty, 1152 base->isTypeDependent(), base->isValueDependent()), 1153 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow), 1154 HasQualifier(false), HasExplicitTemplateArgumentList(false) {} 1155 1156 /// \brief Build an empty member reference expression. 1157 explicit MemberExpr(EmptyShell Empty) 1158 : Expr(MemberExprClass, Empty), HasQualifier(false), 1159 HasExplicitTemplateArgumentList(false) { } 1160 1161 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 1162 NestedNameSpecifier *qual, SourceRange qualrange, 1163 NamedDecl *memberdecl, 1164 SourceLocation l, 1165 bool has_explicit, 1166 SourceLocation langle, 1167 const TemplateArgument *targs, 1168 unsigned numtargs, 1169 SourceLocation rangle, 1170 QualType ty); 1171 1172 void setBase(Expr *E) { Base = E; } 1173 Expr *getBase() const { return cast<Expr>(Base); } 1174 1175 /// \brief Retrieve the member declaration to which this expression refers. 1176 /// 1177 /// The returned declaration will either be a FieldDecl or (in C++) 1178 /// a CXXMethodDecl. 1179 NamedDecl *getMemberDecl() const { return MemberDecl; } 1180 void setMemberDecl(NamedDecl *D) { MemberDecl = D; } 1181 1182 /// \brief Determines whether this member expression actually had 1183 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1184 /// x->Base::foo. 1185 bool hasQualifier() const { return HasQualifier; } 1186 1187 /// \brief If the member name was qualified, retrieves the source range of 1188 /// the nested-name-specifier that precedes the member name. Otherwise, 1189 /// returns an empty source range. 1190 SourceRange getQualifierRange() const { 1191 if (!HasQualifier) 1192 return SourceRange(); 1193 1194 return getMemberQualifier()->Range; 1195 } 1196 1197 /// \brief If the member name was qualified, retrieves the 1198 /// nested-name-specifier that precedes the member name. Otherwise, returns 1199 /// NULL. 1200 NestedNameSpecifier *getQualifier() const { 1201 if (!HasQualifier) 1202 return 0; 1203 1204 return getMemberQualifier()->NNS; 1205 } 1206 1207 /// \brief Determines whether this member expression actually had a C++ 1208 /// template argument list explicitly specified, e.g., x.f<int>. 1209 bool hasExplicitTemplateArgumentList() { 1210 return HasExplicitTemplateArgumentList; 1211 } 1212 1213 /// \brief Retrieve the location of the left angle bracket following the 1214 /// member name ('<'), if any. 1215 SourceLocation getLAngleLoc() const { 1216 if (!HasExplicitTemplateArgumentList) 1217 return SourceLocation(); 1218 1219 return getExplicitTemplateArgumentList()->LAngleLoc; 1220 } 1221 1222 /// \brief Retrieve the template arguments provided as part of this 1223 /// template-id. 1224 const TemplateArgument *getTemplateArgs() const { 1225 if (!HasExplicitTemplateArgumentList) 1226 return 0; 1227 1228 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1229 } 1230 1231 /// \brief Retrieve the number of template arguments provided as part of this 1232 /// template-id. 1233 unsigned getNumTemplateArgs() const { 1234 if (!HasExplicitTemplateArgumentList) 1235 return 0; 1236 1237 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1238 } 1239 1240 /// \brief Retrieve the location of the right angle bracket following the 1241 /// template arguments ('>'). 1242 SourceLocation getRAngleLoc() const { 1243 if (!HasExplicitTemplateArgumentList) 1244 return SourceLocation(); 1245 1246 return getExplicitTemplateArgumentList()->RAngleLoc; 1247 } 1248 1249 bool isArrow() const { return IsArrow; } 1250 void setArrow(bool A) { IsArrow = A; } 1251 1252 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1253 /// location of 'F'. 1254 SourceLocation getMemberLoc() const { return MemberLoc; } 1255 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1256 1257 virtual SourceRange getSourceRange() const { 1258 // If we have an implicit base (like a C++ implicit this), 1259 // make sure not to return its location 1260 SourceLocation EndLoc = MemberLoc; 1261 if (HasExplicitTemplateArgumentList) 1262 EndLoc = getRAngleLoc(); 1263 1264 SourceLocation BaseLoc = getBase()->getLocStart(); 1265 if (BaseLoc.isInvalid()) 1266 return SourceRange(MemberLoc, EndLoc); 1267 return SourceRange(BaseLoc, EndLoc); 1268 } 1269 1270 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1271 1272 static bool classof(const Stmt *T) { 1273 return T->getStmtClass() == MemberExprClass; 1274 } 1275 static bool classof(const MemberExpr *) { return true; } 1276 1277 // Iterators 1278 virtual child_iterator child_begin(); 1279 virtual child_iterator child_end(); 1280}; 1281 1282/// CompoundLiteralExpr - [C99 6.5.2.5] 1283/// 1284class CompoundLiteralExpr : public Expr { 1285 /// LParenLoc - If non-null, this is the location of the left paren in a 1286 /// compound literal like "(int){4}". This can be null if this is a 1287 /// synthesized compound expression. 1288 SourceLocation LParenLoc; 1289 Stmt *Init; 1290 bool FileScope; 1291public: 1292 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 1293 bool fileScope) 1294 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 1295 FileScope(fileScope) {} 1296 1297 /// \brief Construct an empty compound literal. 1298 explicit CompoundLiteralExpr(EmptyShell Empty) 1299 : Expr(CompoundLiteralExprClass, Empty) { } 1300 1301 const Expr *getInitializer() const { return cast<Expr>(Init); } 1302 Expr *getInitializer() { return cast<Expr>(Init); } 1303 void setInitializer(Expr *E) { Init = E; } 1304 1305 bool isFileScope() const { return FileScope; } 1306 void setFileScope(bool FS) { FileScope = FS; } 1307 1308 SourceLocation getLParenLoc() const { return LParenLoc; } 1309 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1310 1311 virtual SourceRange getSourceRange() const { 1312 // FIXME: Init should never be null. 1313 if (!Init) 1314 return SourceRange(); 1315 if (LParenLoc.isInvalid()) 1316 return Init->getSourceRange(); 1317 return SourceRange(LParenLoc, Init->getLocEnd()); 1318 } 1319 1320 static bool classof(const Stmt *T) { 1321 return T->getStmtClass() == CompoundLiteralExprClass; 1322 } 1323 static bool classof(const CompoundLiteralExpr *) { return true; } 1324 1325 // Iterators 1326 virtual child_iterator child_begin(); 1327 virtual child_iterator child_end(); 1328}; 1329 1330/// CastExpr - Base class for type casts, including both implicit 1331/// casts (ImplicitCastExpr) and explicit casts that have some 1332/// representation in the source code (ExplicitCastExpr's derived 1333/// classes). 1334class CastExpr : public Expr { 1335public: 1336 /// CastKind - the kind of cast this represents. 1337 enum CastKind { 1338 /// CK_Unknown - Unknown cast kind. 1339 /// FIXME: The goal is to get rid of this and make all casts have a 1340 /// kind so that the AST client doesn't have to try to figure out what's 1341 /// going on. 1342 CK_Unknown, 1343 1344 /// CK_BitCast - Used for reinterpret_cast. 1345 CK_BitCast, 1346 1347 /// CK_NoOp - Used for const_cast. 1348 CK_NoOp, 1349 1350 /// CK_DerivedToBase - Derived to base class casts. 1351 CK_DerivedToBase, 1352 1353 /// CK_Dynamic - Dynamic cast. 1354 CK_Dynamic, 1355 1356 /// CK_ToUnion - Cast to union (GCC extension). 1357 CK_ToUnion, 1358 1359 /// CK_ArrayToPointerDecay - Array to pointer decay. 1360 CK_ArrayToPointerDecay, 1361 1362 // CK_FunctionToPointerDecay - Function to pointer decay. 1363 CK_FunctionToPointerDecay, 1364 1365 /// CK_NullToMemberPointer - Null pointer to member pointer. 1366 CK_NullToMemberPointer, 1367 1368 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to 1369 /// member pointer in derived class. 1370 CK_BaseToDerivedMemberPointer, 1371 1372 /// CK_UserDefinedConversion - Conversion using a user defined type 1373 /// conversion function. 1374 CK_UserDefinedConversion, 1375 1376 /// CK_ConstructorConversion - Conversion by constructor 1377 CK_ConstructorConversion, 1378 1379 /// CK_IntegralToPointer - Integral to pointer 1380 CK_IntegralToPointer, 1381 1382 /// CK_PointerToIntegral - Pointer to integral 1383 CK_PointerToIntegral 1384 }; 1385 1386private: 1387 CastKind Kind; 1388 Stmt *Op; 1389protected: 1390 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) : 1391 Expr(SC, ty, 1392 // Cast expressions are type-dependent if the type is 1393 // dependent (C++ [temp.dep.expr]p3). 1394 ty->isDependentType(), 1395 // Cast expressions are value-dependent if the type is 1396 // dependent or if the subexpression is value-dependent. 1397 ty->isDependentType() || (op && op->isValueDependent())), 1398 Kind(kind), Op(op) {} 1399 1400 /// \brief Construct an empty cast. 1401 CastExpr(StmtClass SC, EmptyShell Empty) 1402 : Expr(SC, Empty) { } 1403 1404public: 1405 CastKind getCastKind() const { return Kind; } 1406 void setCastKind(CastKind K) { Kind = K; } 1407 const char *getCastKindName() const; 1408 1409 Expr *getSubExpr() { return cast<Expr>(Op); } 1410 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1411 void setSubExpr(Expr *E) { Op = E; } 1412 1413 static bool classof(const Stmt *T) { 1414 StmtClass SC = T->getStmtClass(); 1415 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1416 return true; 1417 1418 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1419 return true; 1420 1421 return false; 1422 } 1423 static bool classof(const CastExpr *) { return true; } 1424 1425 // Iterators 1426 virtual child_iterator child_begin(); 1427 virtual child_iterator child_end(); 1428}; 1429 1430/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1431/// conversions, which have no direct representation in the original 1432/// source code. For example: converting T[]->T*, void f()->void 1433/// (*f)(), float->double, short->int, etc. 1434/// 1435/// In C, implicit casts always produce rvalues. However, in C++, an 1436/// implicit cast whose result is being bound to a reference will be 1437/// an lvalue. For example: 1438/// 1439/// @code 1440/// class Base { }; 1441/// class Derived : public Base { }; 1442/// void f(Derived d) { 1443/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1444/// } 1445/// @endcode 1446class ImplicitCastExpr : public CastExpr { 1447 /// LvalueCast - Whether this cast produces an lvalue. 1448 bool LvalueCast; 1449 1450public: 1451 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) : 1452 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { } 1453 1454 /// \brief Construct an empty implicit cast. 1455 explicit ImplicitCastExpr(EmptyShell Shell) 1456 : CastExpr(ImplicitCastExprClass, Shell) { } 1457 1458 1459 virtual SourceRange getSourceRange() const { 1460 return getSubExpr()->getSourceRange(); 1461 } 1462 1463 /// isLvalueCast - Whether this cast produces an lvalue. 1464 bool isLvalueCast() const { return LvalueCast; } 1465 1466 /// setLvalueCast - Set whether this cast produces an lvalue. 1467 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1468 1469 static bool classof(const Stmt *T) { 1470 return T->getStmtClass() == ImplicitCastExprClass; 1471 } 1472 static bool classof(const ImplicitCastExpr *) { return true; } 1473}; 1474 1475/// ExplicitCastExpr - An explicit cast written in the source 1476/// code. 1477/// 1478/// This class is effectively an abstract class, because it provides 1479/// the basic representation of an explicitly-written cast without 1480/// specifying which kind of cast (C cast, functional cast, static 1481/// cast, etc.) was written; specific derived classes represent the 1482/// particular style of cast and its location information. 1483/// 1484/// Unlike implicit casts, explicit cast nodes have two different 1485/// types: the type that was written into the source code, and the 1486/// actual type of the expression as determined by semantic 1487/// analysis. These types may differ slightly. For example, in C++ one 1488/// can cast to a reference type, which indicates that the resulting 1489/// expression will be an lvalue. The reference type, however, will 1490/// not be used as the type of the expression. 1491class ExplicitCastExpr : public CastExpr { 1492 /// TypeAsWritten - The type that this expression is casting to, as 1493 /// written in the source code. 1494 QualType TypeAsWritten; 1495 1496protected: 1497 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 1498 Expr *op, QualType writtenTy) 1499 : CastExpr(SC, exprTy, kind, op), TypeAsWritten(writtenTy) {} 1500 1501 /// \brief Construct an empty explicit cast. 1502 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 1503 : CastExpr(SC, Shell) { } 1504 1505public: 1506 /// getTypeAsWritten - Returns the type that this expression is 1507 /// casting to, as written in the source code. 1508 QualType getTypeAsWritten() const { return TypeAsWritten; } 1509 void setTypeAsWritten(QualType T) { TypeAsWritten = T; } 1510 1511 static bool classof(const Stmt *T) { 1512 StmtClass SC = T->getStmtClass(); 1513 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1514 return true; 1515 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1516 return true; 1517 1518 return false; 1519 } 1520 static bool classof(const ExplicitCastExpr *) { return true; } 1521}; 1522 1523/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1524/// cast in C++ (C++ [expr.cast]), which uses the syntax 1525/// (Type)expr. For example: @c (int)f. 1526class CStyleCastExpr : public ExplicitCastExpr { 1527 SourceLocation LPLoc; // the location of the left paren 1528 SourceLocation RPLoc; // the location of the right paren 1529public: 1530 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy, 1531 SourceLocation l, SourceLocation r) : 1532 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy), 1533 LPLoc(l), RPLoc(r) {} 1534 1535 /// \brief Construct an empty C-style explicit cast. 1536 explicit CStyleCastExpr(EmptyShell Shell) 1537 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 1538 1539 SourceLocation getLParenLoc() const { return LPLoc; } 1540 void setLParenLoc(SourceLocation L) { LPLoc = L; } 1541 1542 SourceLocation getRParenLoc() const { return RPLoc; } 1543 void setRParenLoc(SourceLocation L) { RPLoc = L; } 1544 1545 virtual SourceRange getSourceRange() const { 1546 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1547 } 1548 static bool classof(const Stmt *T) { 1549 return T->getStmtClass() == CStyleCastExprClass; 1550 } 1551 static bool classof(const CStyleCastExpr *) { return true; } 1552}; 1553 1554/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 1555/// 1556/// This expression node kind describes a builtin binary operation, 1557/// such as "x + y" for integer values "x" and "y". The operands will 1558/// already have been converted to appropriate types (e.g., by 1559/// performing promotions or conversions). 1560/// 1561/// In C++, where operators may be overloaded, a different kind of 1562/// expression node (CXXOperatorCallExpr) is used to express the 1563/// invocation of an overloaded operator with operator syntax. Within 1564/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 1565/// used to store an expression "x + y" depends on the subexpressions 1566/// for x and y. If neither x or y is type-dependent, and the "+" 1567/// operator resolves to a built-in operation, BinaryOperator will be 1568/// used to express the computation (x and y may still be 1569/// value-dependent). If either x or y is type-dependent, or if the 1570/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 1571/// be used to express the computation. 1572class BinaryOperator : public Expr { 1573public: 1574 enum Opcode { 1575 // Operators listed in order of precedence. 1576 // Note that additions to this should also update the StmtVisitor class. 1577 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 1578 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1579 Add, Sub, // [C99 6.5.6] Additive operators. 1580 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1581 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1582 EQ, NE, // [C99 6.5.9] Equality operators. 1583 And, // [C99 6.5.10] Bitwise AND operator. 1584 Xor, // [C99 6.5.11] Bitwise XOR operator. 1585 Or, // [C99 6.5.12] Bitwise OR operator. 1586 LAnd, // [C99 6.5.13] Logical AND operator. 1587 LOr, // [C99 6.5.14] Logical OR operator. 1588 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1589 DivAssign, RemAssign, 1590 AddAssign, SubAssign, 1591 ShlAssign, ShrAssign, 1592 AndAssign, XorAssign, 1593 OrAssign, 1594 Comma // [C99 6.5.17] Comma operator. 1595 }; 1596private: 1597 enum { LHS, RHS, END_EXPR }; 1598 Stmt* SubExprs[END_EXPR]; 1599 Opcode Opc; 1600 SourceLocation OpLoc; 1601public: 1602 1603 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1604 SourceLocation opLoc) 1605 : Expr(BinaryOperatorClass, ResTy, 1606 lhs->isTypeDependent() || rhs->isTypeDependent(), 1607 lhs->isValueDependent() || rhs->isValueDependent()), 1608 Opc(opc), OpLoc(opLoc) { 1609 SubExprs[LHS] = lhs; 1610 SubExprs[RHS] = rhs; 1611 assert(!isCompoundAssignmentOp() && 1612 "Use ArithAssignBinaryOperator for compound assignments"); 1613 } 1614 1615 /// \brief Construct an empty binary operator. 1616 explicit BinaryOperator(EmptyShell Empty) 1617 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 1618 1619 SourceLocation getOperatorLoc() const { return OpLoc; } 1620 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1621 1622 Opcode getOpcode() const { return Opc; } 1623 void setOpcode(Opcode O) { Opc = O; } 1624 1625 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1626 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1627 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1628 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1629 1630 virtual SourceRange getSourceRange() const { 1631 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1632 } 1633 1634 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1635 /// corresponds to, e.g. "<<=". 1636 static const char *getOpcodeStr(Opcode Op); 1637 1638 /// \brief Retrieve the binary opcode that corresponds to the given 1639 /// overloaded operator. 1640 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 1641 1642 /// \brief Retrieve the overloaded operator kind that corresponds to 1643 /// the given binary opcode. 1644 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1645 1646 /// predicates to categorize the respective opcodes. 1647 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1648 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1649 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 1650 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 1651 1652 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1653 bool isRelationalOp() const { return isRelationalOp(Opc); } 1654 1655 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1656 bool isEqualityOp() const { return isEqualityOp(Opc); } 1657 1658 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1659 bool isLogicalOp() const { return isLogicalOp(Opc); } 1660 1661 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1662 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1663 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1664 1665 static bool classof(const Stmt *S) { 1666 return S->getStmtClass() == BinaryOperatorClass || 1667 S->getStmtClass() == CompoundAssignOperatorClass; 1668 } 1669 static bool classof(const BinaryOperator *) { return true; } 1670 1671 // Iterators 1672 virtual child_iterator child_begin(); 1673 virtual child_iterator child_end(); 1674 1675protected: 1676 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1677 SourceLocation oploc, bool dead) 1678 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1679 SubExprs[LHS] = lhs; 1680 SubExprs[RHS] = rhs; 1681 } 1682 1683 BinaryOperator(StmtClass SC, EmptyShell Empty) 1684 : Expr(SC, Empty), Opc(MulAssign) { } 1685}; 1686 1687/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1688/// track of the type the operation is performed in. Due to the semantics of 1689/// these operators, the operands are promoted, the aritmetic performed, an 1690/// implicit conversion back to the result type done, then the assignment takes 1691/// place. This captures the intermediate type which the computation is done 1692/// in. 1693class CompoundAssignOperator : public BinaryOperator { 1694 QualType ComputationLHSType; 1695 QualType ComputationResultType; 1696public: 1697 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1698 QualType ResType, QualType CompLHSType, 1699 QualType CompResultType, 1700 SourceLocation OpLoc) 1701 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1702 ComputationLHSType(CompLHSType), 1703 ComputationResultType(CompResultType) { 1704 assert(isCompoundAssignmentOp() && 1705 "Only should be used for compound assignments"); 1706 } 1707 1708 /// \brief Build an empty compound assignment operator expression. 1709 explicit CompoundAssignOperator(EmptyShell Empty) 1710 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 1711 1712 // The two computation types are the type the LHS is converted 1713 // to for the computation and the type of the result; the two are 1714 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 1715 QualType getComputationLHSType() const { return ComputationLHSType; } 1716 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 1717 1718 QualType getComputationResultType() const { return ComputationResultType; } 1719 void setComputationResultType(QualType T) { ComputationResultType = T; } 1720 1721 static bool classof(const CompoundAssignOperator *) { return true; } 1722 static bool classof(const Stmt *S) { 1723 return S->getStmtClass() == CompoundAssignOperatorClass; 1724 } 1725}; 1726 1727/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1728/// GNU "missing LHS" extension is in use. 1729/// 1730class ConditionalOperator : public Expr { 1731 enum { COND, LHS, RHS, END_EXPR }; 1732 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1733 SourceLocation QuestionLoc, ColonLoc; 1734public: 1735 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 1736 SourceLocation CLoc, Expr *rhs, QualType t) 1737 : Expr(ConditionalOperatorClass, t, 1738 // FIXME: the type of the conditional operator doesn't 1739 // depend on the type of the conditional, but the standard 1740 // seems to imply that it could. File a bug! 1741 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1742 (cond->isValueDependent() || 1743 (lhs && lhs->isValueDependent()) || 1744 (rhs && rhs->isValueDependent()))), 1745 QuestionLoc(QLoc), 1746 ColonLoc(CLoc) { 1747 SubExprs[COND] = cond; 1748 SubExprs[LHS] = lhs; 1749 SubExprs[RHS] = rhs; 1750 } 1751 1752 /// \brief Build an empty conditional operator. 1753 explicit ConditionalOperator(EmptyShell Empty) 1754 : Expr(ConditionalOperatorClass, Empty) { } 1755 1756 // getCond - Return the expression representing the condition for 1757 // the ?: operator. 1758 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1759 void setCond(Expr *E) { SubExprs[COND] = E; } 1760 1761 // getTrueExpr - Return the subexpression representing the value of the ?: 1762 // expression if the condition evaluates to true. In most cases this value 1763 // will be the same as getLHS() except a GCC extension allows the left 1764 // subexpression to be omitted, and instead of the condition be returned. 1765 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1766 // is only evaluated once. 1767 Expr *getTrueExpr() const { 1768 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1769 } 1770 1771 // getTrueExpr - Return the subexpression representing the value of the ?: 1772 // expression if the condition evaluates to false. This is the same as getRHS. 1773 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1774 1775 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1776 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1777 1778 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1779 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1780 1781 SourceLocation getQuestionLoc() const { return QuestionLoc; } 1782 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 1783 1784 SourceLocation getColonLoc() const { return ColonLoc; } 1785 void setColonLoc(SourceLocation L) { ColonLoc = L; } 1786 1787 virtual SourceRange getSourceRange() const { 1788 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1789 } 1790 static bool classof(const Stmt *T) { 1791 return T->getStmtClass() == ConditionalOperatorClass; 1792 } 1793 static bool classof(const ConditionalOperator *) { return true; } 1794 1795 // Iterators 1796 virtual child_iterator child_begin(); 1797 virtual child_iterator child_end(); 1798}; 1799 1800/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1801class AddrLabelExpr : public Expr { 1802 SourceLocation AmpAmpLoc, LabelLoc; 1803 LabelStmt *Label; 1804public: 1805 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1806 QualType t) 1807 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1808 1809 /// \brief Build an empty address of a label expression. 1810 explicit AddrLabelExpr(EmptyShell Empty) 1811 : Expr(AddrLabelExprClass, Empty) { } 1812 1813 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 1814 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 1815 SourceLocation getLabelLoc() const { return LabelLoc; } 1816 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 1817 1818 virtual SourceRange getSourceRange() const { 1819 return SourceRange(AmpAmpLoc, LabelLoc); 1820 } 1821 1822 LabelStmt *getLabel() const { return Label; } 1823 void setLabel(LabelStmt *S) { Label = S; } 1824 1825 static bool classof(const Stmt *T) { 1826 return T->getStmtClass() == AddrLabelExprClass; 1827 } 1828 static bool classof(const AddrLabelExpr *) { return true; } 1829 1830 // Iterators 1831 virtual child_iterator child_begin(); 1832 virtual child_iterator child_end(); 1833}; 1834 1835/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1836/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1837/// takes the value of the last subexpression. 1838class StmtExpr : public Expr { 1839 Stmt *SubStmt; 1840 SourceLocation LParenLoc, RParenLoc; 1841public: 1842 StmtExpr(CompoundStmt *substmt, QualType T, 1843 SourceLocation lp, SourceLocation rp) : 1844 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1845 1846 /// \brief Build an empty statement expression. 1847 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 1848 1849 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1850 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1851 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 1852 1853 virtual SourceRange getSourceRange() const { 1854 return SourceRange(LParenLoc, RParenLoc); 1855 } 1856 1857 SourceLocation getLParenLoc() const { return LParenLoc; } 1858 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1859 SourceLocation getRParenLoc() const { return RParenLoc; } 1860 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1861 1862 static bool classof(const Stmt *T) { 1863 return T->getStmtClass() == StmtExprClass; 1864 } 1865 static bool classof(const StmtExpr *) { return true; } 1866 1867 // Iterators 1868 virtual child_iterator child_begin(); 1869 virtual child_iterator child_end(); 1870}; 1871 1872/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 1873/// This AST node represents a function that returns 1 if two *types* (not 1874/// expressions) are compatible. The result of this built-in function can be 1875/// used in integer constant expressions. 1876class TypesCompatibleExpr : public Expr { 1877 QualType Type1; 1878 QualType Type2; 1879 SourceLocation BuiltinLoc, RParenLoc; 1880public: 1881 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1882 QualType t1, QualType t2, SourceLocation RP) : 1883 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1884 BuiltinLoc(BLoc), RParenLoc(RP) {} 1885 1886 /// \brief Build an empty __builtin_type_compatible_p expression. 1887 explicit TypesCompatibleExpr(EmptyShell Empty) 1888 : Expr(TypesCompatibleExprClass, Empty) { } 1889 1890 QualType getArgType1() const { return Type1; } 1891 void setArgType1(QualType T) { Type1 = T; } 1892 QualType getArgType2() const { return Type2; } 1893 void setArgType2(QualType T) { Type2 = T; } 1894 1895 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1896 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1897 1898 SourceLocation getRParenLoc() const { return RParenLoc; } 1899 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1900 1901 virtual SourceRange getSourceRange() const { 1902 return SourceRange(BuiltinLoc, RParenLoc); 1903 } 1904 static bool classof(const Stmt *T) { 1905 return T->getStmtClass() == TypesCompatibleExprClass; 1906 } 1907 static bool classof(const TypesCompatibleExpr *) { return true; } 1908 1909 // Iterators 1910 virtual child_iterator child_begin(); 1911 virtual child_iterator child_end(); 1912}; 1913 1914/// ShuffleVectorExpr - clang-specific builtin-in function 1915/// __builtin_shufflevector. 1916/// This AST node represents a operator that does a constant 1917/// shuffle, similar to LLVM's shufflevector instruction. It takes 1918/// two vectors and a variable number of constant indices, 1919/// and returns the appropriately shuffled vector. 1920class ShuffleVectorExpr : public Expr { 1921 SourceLocation BuiltinLoc, RParenLoc; 1922 1923 // SubExprs - the list of values passed to the __builtin_shufflevector 1924 // function. The first two are vectors, and the rest are constant 1925 // indices. The number of values in this list is always 1926 // 2+the number of indices in the vector type. 1927 Stmt **SubExprs; 1928 unsigned NumExprs; 1929 1930protected: 1931 virtual void DoDestroy(ASTContext &C); 1932 1933public: 1934 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 1935 QualType Type, SourceLocation BLoc, 1936 SourceLocation RP) : 1937 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1938 RParenLoc(RP), NumExprs(nexpr) { 1939 1940 SubExprs = new (C) Stmt*[nexpr]; 1941 for (unsigned i = 0; i < nexpr; i++) 1942 SubExprs[i] = args[i]; 1943 } 1944 1945 /// \brief Build an empty vector-shuffle expression. 1946 explicit ShuffleVectorExpr(EmptyShell Empty) 1947 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 1948 1949 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1950 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1951 1952 SourceLocation getRParenLoc() const { return RParenLoc; } 1953 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1954 1955 virtual SourceRange getSourceRange() const { 1956 return SourceRange(BuiltinLoc, RParenLoc); 1957 } 1958 static bool classof(const Stmt *T) { 1959 return T->getStmtClass() == ShuffleVectorExprClass; 1960 } 1961 static bool classof(const ShuffleVectorExpr *) { return true; } 1962 1963 ~ShuffleVectorExpr() {} 1964 1965 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1966 /// constant expression, the actual arguments passed in, and the function 1967 /// pointers. 1968 unsigned getNumSubExprs() const { return NumExprs; } 1969 1970 /// getExpr - Return the Expr at the specified index. 1971 Expr *getExpr(unsigned Index) { 1972 assert((Index < NumExprs) && "Arg access out of range!"); 1973 return cast<Expr>(SubExprs[Index]); 1974 } 1975 const Expr *getExpr(unsigned Index) const { 1976 assert((Index < NumExprs) && "Arg access out of range!"); 1977 return cast<Expr>(SubExprs[Index]); 1978 } 1979 1980 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 1981 1982 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1983 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1984 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 1985 } 1986 1987 // Iterators 1988 virtual child_iterator child_begin(); 1989 virtual child_iterator child_end(); 1990}; 1991 1992/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1993/// This AST node is similar to the conditional operator (?:) in C, with 1994/// the following exceptions: 1995/// - the test expression must be a integer constant expression. 1996/// - the expression returned acts like the chosen subexpression in every 1997/// visible way: the type is the same as that of the chosen subexpression, 1998/// and all predicates (whether it's an l-value, whether it's an integer 1999/// constant expression, etc.) return the same result as for the chosen 2000/// sub-expression. 2001class ChooseExpr : public Expr { 2002 enum { COND, LHS, RHS, END_EXPR }; 2003 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2004 SourceLocation BuiltinLoc, RParenLoc; 2005public: 2006 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2007 SourceLocation RP) 2008 : Expr(ChooseExprClass, t), 2009 BuiltinLoc(BLoc), RParenLoc(RP) { 2010 SubExprs[COND] = cond; 2011 SubExprs[LHS] = lhs; 2012 SubExprs[RHS] = rhs; 2013 } 2014 2015 /// \brief Build an empty __builtin_choose_expr. 2016 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2017 2018 /// isConditionTrue - Return whether the condition is true (i.e. not 2019 /// equal to zero). 2020 bool isConditionTrue(ASTContext &C) const; 2021 2022 /// getChosenSubExpr - Return the subexpression chosen according to the 2023 /// condition. 2024 Expr *getChosenSubExpr(ASTContext &C) const { 2025 return isConditionTrue(C) ? getLHS() : getRHS(); 2026 } 2027 2028 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2029 void setCond(Expr *E) { SubExprs[COND] = E; } 2030 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2031 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2032 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2033 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2034 2035 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2036 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2037 2038 SourceLocation getRParenLoc() const { return RParenLoc; } 2039 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2040 2041 virtual SourceRange getSourceRange() const { 2042 return SourceRange(BuiltinLoc, RParenLoc); 2043 } 2044 static bool classof(const Stmt *T) { 2045 return T->getStmtClass() == ChooseExprClass; 2046 } 2047 static bool classof(const ChooseExpr *) { return true; } 2048 2049 // Iterators 2050 virtual child_iterator child_begin(); 2051 virtual child_iterator child_end(); 2052}; 2053 2054/// GNUNullExpr - Implements the GNU __null extension, which is a name 2055/// for a null pointer constant that has integral type (e.g., int or 2056/// long) and is the same size and alignment as a pointer. The __null 2057/// extension is typically only used by system headers, which define 2058/// NULL as __null in C++ rather than using 0 (which is an integer 2059/// that may not match the size of a pointer). 2060class GNUNullExpr : public Expr { 2061 /// TokenLoc - The location of the __null keyword. 2062 SourceLocation TokenLoc; 2063 2064public: 2065 GNUNullExpr(QualType Ty, SourceLocation Loc) 2066 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 2067 2068 /// \brief Build an empty GNU __null expression. 2069 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2070 2071 /// getTokenLocation - The location of the __null token. 2072 SourceLocation getTokenLocation() const { return TokenLoc; } 2073 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2074 2075 virtual SourceRange getSourceRange() const { 2076 return SourceRange(TokenLoc); 2077 } 2078 static bool classof(const Stmt *T) { 2079 return T->getStmtClass() == GNUNullExprClass; 2080 } 2081 static bool classof(const GNUNullExpr *) { return true; } 2082 2083 // Iterators 2084 virtual child_iterator child_begin(); 2085 virtual child_iterator child_end(); 2086}; 2087 2088/// VAArgExpr, used for the builtin function __builtin_va_start. 2089class VAArgExpr : public Expr { 2090 Stmt *Val; 2091 SourceLocation BuiltinLoc, RParenLoc; 2092public: 2093 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 2094 : Expr(VAArgExprClass, t), 2095 Val(e), 2096 BuiltinLoc(BLoc), 2097 RParenLoc(RPLoc) { } 2098 2099 /// \brief Create an empty __builtin_va_start expression. 2100 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2101 2102 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2103 Expr *getSubExpr() { return cast<Expr>(Val); } 2104 void setSubExpr(Expr *E) { Val = E; } 2105 2106 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2107 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2108 2109 SourceLocation getRParenLoc() const { return RParenLoc; } 2110 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2111 2112 virtual SourceRange getSourceRange() const { 2113 return SourceRange(BuiltinLoc, RParenLoc); 2114 } 2115 static bool classof(const Stmt *T) { 2116 return T->getStmtClass() == VAArgExprClass; 2117 } 2118 static bool classof(const VAArgExpr *) { return true; } 2119 2120 // Iterators 2121 virtual child_iterator child_begin(); 2122 virtual child_iterator child_end(); 2123}; 2124 2125/// @brief Describes an C or C++ initializer list. 2126/// 2127/// InitListExpr describes an initializer list, which can be used to 2128/// initialize objects of different types, including 2129/// struct/class/union types, arrays, and vectors. For example: 2130/// 2131/// @code 2132/// struct foo x = { 1, { 2, 3 } }; 2133/// @endcode 2134/// 2135/// Prior to semantic analysis, an initializer list will represent the 2136/// initializer list as written by the user, but will have the 2137/// placeholder type "void". This initializer list is called the 2138/// syntactic form of the initializer, and may contain C99 designated 2139/// initializers (represented as DesignatedInitExprs), initializations 2140/// of subobject members without explicit braces, and so on. Clients 2141/// interested in the original syntax of the initializer list should 2142/// use the syntactic form of the initializer list. 2143/// 2144/// After semantic analysis, the initializer list will represent the 2145/// semantic form of the initializer, where the initializations of all 2146/// subobjects are made explicit with nested InitListExpr nodes and 2147/// C99 designators have been eliminated by placing the designated 2148/// initializations into the subobject they initialize. Additionally, 2149/// any "holes" in the initialization, where no initializer has been 2150/// specified for a particular subobject, will be replaced with 2151/// implicitly-generated ImplicitValueInitExpr expressions that 2152/// value-initialize the subobjects. Note, however, that the 2153/// initializer lists may still have fewer initializers than there are 2154/// elements to initialize within the object. 2155/// 2156/// Given the semantic form of the initializer list, one can retrieve 2157/// the original syntactic form of that initializer list (if it 2158/// exists) using getSyntacticForm(). Since many initializer lists 2159/// have the same syntactic and semantic forms, getSyntacticForm() may 2160/// return NULL, indicating that the current initializer list also 2161/// serves as its syntactic form. 2162class InitListExpr : public Expr { 2163 // FIXME: Eliminate this vector in favor of ASTContext allocation 2164 std::vector<Stmt *> InitExprs; 2165 SourceLocation LBraceLoc, RBraceLoc; 2166 2167 /// Contains the initializer list that describes the syntactic form 2168 /// written in the source code. 2169 InitListExpr *SyntacticForm; 2170 2171 /// If this initializer list initializes a union, specifies which 2172 /// field within the union will be initialized. 2173 FieldDecl *UnionFieldInit; 2174 2175 /// Whether this initializer list originally had a GNU array-range 2176 /// designator in it. This is a temporary marker used by CodeGen. 2177 bool HadArrayRangeDesignator; 2178 2179public: 2180 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 2181 SourceLocation rbraceloc); 2182 2183 /// \brief Build an empty initializer list. 2184 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { } 2185 2186 unsigned getNumInits() const { return InitExprs.size(); } 2187 2188 const Expr* getInit(unsigned Init) const { 2189 assert(Init < getNumInits() && "Initializer access out of range!"); 2190 return cast_or_null<Expr>(InitExprs[Init]); 2191 } 2192 2193 Expr* getInit(unsigned Init) { 2194 assert(Init < getNumInits() && "Initializer access out of range!"); 2195 return cast_or_null<Expr>(InitExprs[Init]); 2196 } 2197 2198 void setInit(unsigned Init, Expr *expr) { 2199 assert(Init < getNumInits() && "Initializer access out of range!"); 2200 InitExprs[Init] = expr; 2201 } 2202 2203 /// \brief Reserve space for some number of initializers. 2204 void reserveInits(unsigned NumInits); 2205 2206 /// @brief Specify the number of initializers 2207 /// 2208 /// If there are more than @p NumInits initializers, the remaining 2209 /// initializers will be destroyed. If there are fewer than @p 2210 /// NumInits initializers, NULL expressions will be added for the 2211 /// unknown initializers. 2212 void resizeInits(ASTContext &Context, unsigned NumInits); 2213 2214 /// @brief Updates the initializer at index @p Init with the new 2215 /// expression @p expr, and returns the old expression at that 2216 /// location. 2217 /// 2218 /// When @p Init is out of range for this initializer list, the 2219 /// initializer list will be extended with NULL expressions to 2220 /// accomodate the new entry. 2221 Expr *updateInit(unsigned Init, Expr *expr); 2222 2223 /// \brief If this initializes a union, specifies which field in the 2224 /// union to initialize. 2225 /// 2226 /// Typically, this field is the first named field within the 2227 /// union. However, a designated initializer can specify the 2228 /// initialization of a different field within the union. 2229 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2230 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2231 2232 // Explicit InitListExpr's originate from source code (and have valid source 2233 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2234 bool isExplicit() { 2235 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2236 } 2237 2238 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2239 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2240 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2241 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2242 2243 /// @brief Retrieve the initializer list that describes the 2244 /// syntactic form of the initializer. 2245 /// 2246 /// 2247 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2248 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2249 2250 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2251 void sawArrayRangeDesignator(bool ARD = true) { 2252 HadArrayRangeDesignator = ARD; 2253 } 2254 2255 virtual SourceRange getSourceRange() const { 2256 return SourceRange(LBraceLoc, RBraceLoc); 2257 } 2258 static bool classof(const Stmt *T) { 2259 return T->getStmtClass() == InitListExprClass; 2260 } 2261 static bool classof(const InitListExpr *) { return true; } 2262 2263 // Iterators 2264 virtual child_iterator child_begin(); 2265 virtual child_iterator child_end(); 2266 2267 typedef std::vector<Stmt *>::iterator iterator; 2268 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 2269 2270 iterator begin() { return InitExprs.begin(); } 2271 iterator end() { return InitExprs.end(); } 2272 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2273 reverse_iterator rend() { return InitExprs.rend(); } 2274}; 2275 2276/// @brief Represents a C99 designated initializer expression. 2277/// 2278/// A designated initializer expression (C99 6.7.8) contains one or 2279/// more designators (which can be field designators, array 2280/// designators, or GNU array-range designators) followed by an 2281/// expression that initializes the field or element(s) that the 2282/// designators refer to. For example, given: 2283/// 2284/// @code 2285/// struct point { 2286/// double x; 2287/// double y; 2288/// }; 2289/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2290/// @endcode 2291/// 2292/// The InitListExpr contains three DesignatedInitExprs, the first of 2293/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2294/// designators, one array designator for @c [2] followed by one field 2295/// designator for @c .y. The initalization expression will be 1.0. 2296class DesignatedInitExpr : public Expr { 2297public: 2298 /// \brief Forward declaration of the Designator class. 2299 class Designator; 2300 2301private: 2302 /// The location of the '=' or ':' prior to the actual initializer 2303 /// expression. 2304 SourceLocation EqualOrColonLoc; 2305 2306 /// Whether this designated initializer used the GNU deprecated 2307 /// syntax rather than the C99 '=' syntax. 2308 bool GNUSyntax : 1; 2309 2310 /// The number of designators in this initializer expression. 2311 unsigned NumDesignators : 15; 2312 2313 /// \brief The designators in this designated initialization 2314 /// expression. 2315 Designator *Designators; 2316 2317 /// The number of subexpressions of this initializer expression, 2318 /// which contains both the initializer and any additional 2319 /// expressions used by array and array-range designators. 2320 unsigned NumSubExprs : 16; 2321 2322 2323 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 2324 const Designator *Designators, 2325 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2326 Expr **IndexExprs, unsigned NumIndexExprs, 2327 Expr *Init); 2328 2329 explicit DesignatedInitExpr(unsigned NumSubExprs) 2330 : Expr(DesignatedInitExprClass, EmptyShell()), 2331 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2332 2333protected: 2334 virtual void DoDestroy(ASTContext &C); 2335 2336public: 2337 /// A field designator, e.g., ".x". 2338 struct FieldDesignator { 2339 /// Refers to the field that is being initialized. The low bit 2340 /// of this field determines whether this is actually a pointer 2341 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2342 /// initially constructed, a field designator will store an 2343 /// IdentifierInfo*. After semantic analysis has resolved that 2344 /// name, the field designator will instead store a FieldDecl*. 2345 uintptr_t NameOrField; 2346 2347 /// The location of the '.' in the designated initializer. 2348 unsigned DotLoc; 2349 2350 /// The location of the field name in the designated initializer. 2351 unsigned FieldLoc; 2352 }; 2353 2354 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2355 struct ArrayOrRangeDesignator { 2356 /// Location of the first index expression within the designated 2357 /// initializer expression's list of subexpressions. 2358 unsigned Index; 2359 /// The location of the '[' starting the array range designator. 2360 unsigned LBracketLoc; 2361 /// The location of the ellipsis separating the start and end 2362 /// indices. Only valid for GNU array-range designators. 2363 unsigned EllipsisLoc; 2364 /// The location of the ']' terminating the array range designator. 2365 unsigned RBracketLoc; 2366 }; 2367 2368 /// @brief Represents a single C99 designator. 2369 /// 2370 /// @todo This class is infuriatingly similar to clang::Designator, 2371 /// but minor differences (storing indices vs. storing pointers) 2372 /// keep us from reusing it. Try harder, later, to rectify these 2373 /// differences. 2374 class Designator { 2375 /// @brief The kind of designator this describes. 2376 enum { 2377 FieldDesignator, 2378 ArrayDesignator, 2379 ArrayRangeDesignator 2380 } Kind; 2381 2382 union { 2383 /// A field designator, e.g., ".x". 2384 struct FieldDesignator Field; 2385 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2386 struct ArrayOrRangeDesignator ArrayOrRange; 2387 }; 2388 friend class DesignatedInitExpr; 2389 2390 public: 2391 Designator() {} 2392 2393 /// @brief Initializes a field designator. 2394 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2395 SourceLocation FieldLoc) 2396 : Kind(FieldDesignator) { 2397 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2398 Field.DotLoc = DotLoc.getRawEncoding(); 2399 Field.FieldLoc = FieldLoc.getRawEncoding(); 2400 } 2401 2402 /// @brief Initializes an array designator. 2403 Designator(unsigned Index, SourceLocation LBracketLoc, 2404 SourceLocation RBracketLoc) 2405 : Kind(ArrayDesignator) { 2406 ArrayOrRange.Index = Index; 2407 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2408 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2409 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2410 } 2411 2412 /// @brief Initializes a GNU array-range designator. 2413 Designator(unsigned Index, SourceLocation LBracketLoc, 2414 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2415 : Kind(ArrayRangeDesignator) { 2416 ArrayOrRange.Index = Index; 2417 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2418 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2419 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2420 } 2421 2422 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2423 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2424 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2425 2426 IdentifierInfo * getFieldName(); 2427 2428 FieldDecl *getField() { 2429 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2430 if (Field.NameOrField & 0x01) 2431 return 0; 2432 else 2433 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2434 } 2435 2436 void setField(FieldDecl *FD) { 2437 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2438 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2439 } 2440 2441 SourceLocation getDotLoc() const { 2442 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2443 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2444 } 2445 2446 SourceLocation getFieldLoc() const { 2447 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2448 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2449 } 2450 2451 SourceLocation getLBracketLoc() const { 2452 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2453 "Only valid on an array or array-range designator"); 2454 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2455 } 2456 2457 SourceLocation getRBracketLoc() const { 2458 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2459 "Only valid on an array or array-range designator"); 2460 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2461 } 2462 2463 SourceLocation getEllipsisLoc() const { 2464 assert(Kind == ArrayRangeDesignator && 2465 "Only valid on an array-range designator"); 2466 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2467 } 2468 2469 unsigned getFirstExprIndex() const { 2470 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2471 "Only valid on an array or array-range designator"); 2472 return ArrayOrRange.Index; 2473 } 2474 2475 SourceLocation getStartLocation() const { 2476 if (Kind == FieldDesignator) 2477 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2478 else 2479 return getLBracketLoc(); 2480 } 2481 }; 2482 2483 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2484 unsigned NumDesignators, 2485 Expr **IndexExprs, unsigned NumIndexExprs, 2486 SourceLocation EqualOrColonLoc, 2487 bool GNUSyntax, Expr *Init); 2488 2489 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 2490 2491 /// @brief Returns the number of designators in this initializer. 2492 unsigned size() const { return NumDesignators; } 2493 2494 // Iterator access to the designators. 2495 typedef Designator* designators_iterator; 2496 designators_iterator designators_begin() { return Designators; } 2497 designators_iterator designators_end() { 2498 return Designators + NumDesignators; 2499 } 2500 2501 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 2502 2503 void setDesignators(const Designator *Desigs, unsigned NumDesigs); 2504 2505 Expr *getArrayIndex(const Designator& D); 2506 Expr *getArrayRangeStart(const Designator& D); 2507 Expr *getArrayRangeEnd(const Designator& D); 2508 2509 /// @brief Retrieve the location of the '=' that precedes the 2510 /// initializer value itself, if present. 2511 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2512 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 2513 2514 /// @brief Determines whether this designated initializer used the 2515 /// deprecated GNU syntax for designated initializers. 2516 bool usesGNUSyntax() const { return GNUSyntax; } 2517 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 2518 2519 /// @brief Retrieve the initializer value. 2520 Expr *getInit() const { 2521 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2522 } 2523 2524 void setInit(Expr *init) { 2525 *child_begin() = init; 2526 } 2527 2528 /// \brief Retrieve the total number of subexpressions in this 2529 /// designated initializer expression, including the actual 2530 /// initialized value and any expressions that occur within array 2531 /// and array-range designators. 2532 unsigned getNumSubExprs() const { return NumSubExprs; } 2533 2534 Expr *getSubExpr(unsigned Idx) { 2535 assert(Idx < NumSubExprs && "Subscript out of range"); 2536 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2537 Ptr += sizeof(DesignatedInitExpr); 2538 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 2539 } 2540 2541 void setSubExpr(unsigned Idx, Expr *E) { 2542 assert(Idx < NumSubExprs && "Subscript out of range"); 2543 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2544 Ptr += sizeof(DesignatedInitExpr); 2545 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 2546 } 2547 2548 /// \brief Replaces the designator at index @p Idx with the series 2549 /// of designators in [First, Last). 2550 void ExpandDesignator(unsigned Idx, const Designator *First, 2551 const Designator *Last); 2552 2553 virtual SourceRange getSourceRange() const; 2554 2555 static bool classof(const Stmt *T) { 2556 return T->getStmtClass() == DesignatedInitExprClass; 2557 } 2558 static bool classof(const DesignatedInitExpr *) { return true; } 2559 2560 // Iterators 2561 virtual child_iterator child_begin(); 2562 virtual child_iterator child_end(); 2563}; 2564 2565/// \brief Represents an implicitly-generated value initialization of 2566/// an object of a given type. 2567/// 2568/// Implicit value initializations occur within semantic initializer 2569/// list expressions (InitListExpr) as placeholders for subobject 2570/// initializations not explicitly specified by the user. 2571/// 2572/// \see InitListExpr 2573class ImplicitValueInitExpr : public Expr { 2574public: 2575 explicit ImplicitValueInitExpr(QualType ty) 2576 : Expr(ImplicitValueInitExprClass, ty) { } 2577 2578 /// \brief Construct an empty implicit value initialization. 2579 explicit ImplicitValueInitExpr(EmptyShell Empty) 2580 : Expr(ImplicitValueInitExprClass, Empty) { } 2581 2582 static bool classof(const Stmt *T) { 2583 return T->getStmtClass() == ImplicitValueInitExprClass; 2584 } 2585 static bool classof(const ImplicitValueInitExpr *) { return true; } 2586 2587 virtual SourceRange getSourceRange() const { 2588 return SourceRange(); 2589 } 2590 2591 // Iterators 2592 virtual child_iterator child_begin(); 2593 virtual child_iterator child_end(); 2594}; 2595 2596 2597class ParenListExpr : public Expr { 2598 Stmt **Exprs; 2599 unsigned NumExprs; 2600 SourceLocation LParenLoc, RParenLoc; 2601 2602protected: 2603 virtual void DoDestroy(ASTContext& C); 2604 2605public: 2606 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 2607 unsigned numexprs, SourceLocation rparenloc); 2608 2609 ~ParenListExpr() {} 2610 2611 /// \brief Build an empty paren list. 2612 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 2613 2614 unsigned getNumExprs() const { return NumExprs; } 2615 2616 const Expr* getExpr(unsigned Init) const { 2617 assert(Init < getNumExprs() && "Initializer access out of range!"); 2618 return cast_or_null<Expr>(Exprs[Init]); 2619 } 2620 2621 Expr* getExpr(unsigned Init) { 2622 assert(Init < getNumExprs() && "Initializer access out of range!"); 2623 return cast_or_null<Expr>(Exprs[Init]); 2624 } 2625 2626 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 2627 2628 SourceLocation getLParenLoc() const { return LParenLoc; } 2629 SourceLocation getRParenLoc() const { return RParenLoc; } 2630 2631 virtual SourceRange getSourceRange() const { 2632 return SourceRange(LParenLoc, RParenLoc); 2633 } 2634 static bool classof(const Stmt *T) { 2635 return T->getStmtClass() == ParenListExprClass; 2636 } 2637 static bool classof(const ParenListExpr *) { return true; } 2638 2639 // Iterators 2640 virtual child_iterator child_begin(); 2641 virtual child_iterator child_end(); 2642}; 2643 2644 2645//===----------------------------------------------------------------------===// 2646// Clang Extensions 2647//===----------------------------------------------------------------------===// 2648 2649 2650/// ExtVectorElementExpr - This represents access to specific elements of a 2651/// vector, and may occur on the left hand side or right hand side. For example 2652/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2653/// 2654/// Note that the base may have either vector or pointer to vector type, just 2655/// like a struct field reference. 2656/// 2657class ExtVectorElementExpr : public Expr { 2658 Stmt *Base; 2659 IdentifierInfo *Accessor; 2660 SourceLocation AccessorLoc; 2661public: 2662 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2663 SourceLocation loc) 2664 : Expr(ExtVectorElementExprClass, ty), 2665 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 2666 2667 /// \brief Build an empty vector element expression. 2668 explicit ExtVectorElementExpr(EmptyShell Empty) 2669 : Expr(ExtVectorElementExprClass, Empty) { } 2670 2671 const Expr *getBase() const { return cast<Expr>(Base); } 2672 Expr *getBase() { return cast<Expr>(Base); } 2673 void setBase(Expr *E) { Base = E; } 2674 2675 IdentifierInfo &getAccessor() const { return *Accessor; } 2676 void setAccessor(IdentifierInfo *II) { Accessor = II; } 2677 2678 SourceLocation getAccessorLoc() const { return AccessorLoc; } 2679 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 2680 2681 /// getNumElements - Get the number of components being selected. 2682 unsigned getNumElements() const; 2683 2684 /// containsDuplicateElements - Return true if any element access is 2685 /// repeated. 2686 bool containsDuplicateElements() const; 2687 2688 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2689 /// aggregate Constant of ConstantInt(s). 2690 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2691 2692 virtual SourceRange getSourceRange() const { 2693 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2694 } 2695 2696 /// isArrow - Return true if the base expression is a pointer to vector, 2697 /// return false if the base expression is a vector. 2698 bool isArrow() const; 2699 2700 static bool classof(const Stmt *T) { 2701 return T->getStmtClass() == ExtVectorElementExprClass; 2702 } 2703 static bool classof(const ExtVectorElementExpr *) { return true; } 2704 2705 // Iterators 2706 virtual child_iterator child_begin(); 2707 virtual child_iterator child_end(); 2708}; 2709 2710 2711/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2712/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2713class BlockExpr : public Expr { 2714protected: 2715 BlockDecl *TheBlock; 2716 bool HasBlockDeclRefExprs; 2717public: 2718 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2719 : Expr(BlockExprClass, ty), 2720 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2721 2722 /// \brief Build an empty block expression. 2723 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 2724 2725 const BlockDecl *getBlockDecl() const { return TheBlock; } 2726 BlockDecl *getBlockDecl() { return TheBlock; } 2727 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 2728 2729 // Convenience functions for probing the underlying BlockDecl. 2730 SourceLocation getCaretLocation() const; 2731 const Stmt *getBody() const; 2732 Stmt *getBody(); 2733 2734 virtual SourceRange getSourceRange() const { 2735 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2736 } 2737 2738 /// getFunctionType - Return the underlying function type for this block. 2739 const FunctionType *getFunctionType() const; 2740 2741 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2742 /// inside of the block that reference values outside the block. 2743 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2744 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 2745 2746 static bool classof(const Stmt *T) { 2747 return T->getStmtClass() == BlockExprClass; 2748 } 2749 static bool classof(const BlockExpr *) { return true; } 2750 2751 // Iterators 2752 virtual child_iterator child_begin(); 2753 virtual child_iterator child_end(); 2754}; 2755 2756/// BlockDeclRefExpr - A reference to a declared variable, function, 2757/// enum, etc. 2758class BlockDeclRefExpr : public Expr { 2759 ValueDecl *D; 2760 SourceLocation Loc; 2761 bool IsByRef : 1; 2762 bool ConstQualAdded : 1; 2763public: 2764 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 2765 bool constAdded = false) : 2766 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef), 2767 ConstQualAdded(constAdded) {} 2768 2769 // \brief Build an empty reference to a declared variable in a 2770 // block. 2771 explicit BlockDeclRefExpr(EmptyShell Empty) 2772 : Expr(BlockDeclRefExprClass, Empty) { } 2773 2774 ValueDecl *getDecl() { return D; } 2775 const ValueDecl *getDecl() const { return D; } 2776 void setDecl(ValueDecl *VD) { D = VD; } 2777 2778 SourceLocation getLocation() const { return Loc; } 2779 void setLocation(SourceLocation L) { Loc = L; } 2780 2781 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2782 2783 bool isByRef() const { return IsByRef; } 2784 void setByRef(bool BR) { IsByRef = BR; } 2785 2786 bool isConstQualAdded() const { return ConstQualAdded; } 2787 void setConstQualAdded(bool C) { ConstQualAdded = C; } 2788 2789 static bool classof(const Stmt *T) { 2790 return T->getStmtClass() == BlockDeclRefExprClass; 2791 } 2792 static bool classof(const BlockDeclRefExpr *) { return true; } 2793 2794 // Iterators 2795 virtual child_iterator child_begin(); 2796 virtual child_iterator child_end(); 2797}; 2798 2799} // end namespace clang 2800 2801#endif 2802