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