Expr.h revision 82debc7d282e723e58d183bfa89ddc2500a8daaf
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 const FunctionDecl *getDirectCallee() const { 995 return const_cast<CallExpr*>(this)->getDirectCallee(); 996 } 997 998 /// getNumArgs - Return the number of actual arguments to this call. 999 /// 1000 unsigned getNumArgs() const { return NumArgs; } 1001 1002 /// getArg - Return the specified argument. 1003 Expr *getArg(unsigned Arg) { 1004 assert(Arg < NumArgs && "Arg access out of range!"); 1005 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1006 } 1007 const Expr *getArg(unsigned Arg) const { 1008 assert(Arg < NumArgs && "Arg access out of range!"); 1009 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1010 } 1011 1012 /// setArg - Set the specified argument. 1013 void setArg(unsigned Arg, Expr *ArgExpr) { 1014 assert(Arg < NumArgs && "Arg access out of range!"); 1015 SubExprs[Arg+ARGS_START] = ArgExpr; 1016 } 1017 1018 /// setNumArgs - This changes the number of arguments present in this call. 1019 /// Any orphaned expressions are deleted by this, and any new operands are set 1020 /// to null. 1021 void setNumArgs(ASTContext& C, unsigned NumArgs); 1022 1023 typedef ExprIterator arg_iterator; 1024 typedef ConstExprIterator const_arg_iterator; 1025 1026 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 1027 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 1028 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 1029 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 1030 1031 /// getNumCommas - Return the number of commas that must have been present in 1032 /// this function call. 1033 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1034 1035 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1036 /// not, return 0. 1037 unsigned isBuiltinCall(ASTContext &Context) const; 1038 1039 /// getCallReturnType - Get the return type of the call expr. This is not 1040 /// always the type of the expr itself, if the return type is a reference 1041 /// type. 1042 QualType getCallReturnType() const; 1043 1044 SourceLocation getRParenLoc() const { return RParenLoc; } 1045 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1046 1047 virtual SourceRange getSourceRange() const { 1048 return SourceRange(getCallee()->getLocStart(), RParenLoc); 1049 } 1050 1051 static bool classof(const Stmt *T) { 1052 return T->getStmtClass() == CallExprClass || 1053 T->getStmtClass() == CXXOperatorCallExprClass || 1054 T->getStmtClass() == CXXMemberCallExprClass; 1055 } 1056 static bool classof(const CallExpr *) { return true; } 1057 static bool classof(const CXXOperatorCallExpr *) { return true; } 1058 static bool classof(const CXXMemberCallExpr *) { return true; } 1059 1060 // Iterators 1061 virtual child_iterator child_begin(); 1062 virtual child_iterator child_end(); 1063}; 1064 1065/// \brief Represents the qualifier that may precede a C++ name, e.g., the 1066/// "std::" in "std::sort". 1067struct NameQualifier { 1068 /// \brief The nested name specifier. 1069 NestedNameSpecifier *NNS; 1070 1071 /// \brief The source range covered by the nested name specifier. 1072 SourceRange Range; 1073}; 1074 1075/// \brief Represents an explicit template argument list in C++, e.g., 1076/// the "<int>" in "sort<int>". 1077struct ExplicitTemplateArgumentList { 1078 /// \brief The source location of the left angle bracket ('<'); 1079 SourceLocation LAngleLoc; 1080 1081 /// \brief The source location of the right angle bracket ('>'); 1082 SourceLocation RAngleLoc; 1083 1084 /// \brief The number of template arguments in TemplateArgs. 1085 /// The actual template arguments (if any) are stored after the 1086 /// ExplicitTemplateArgumentList structure. 1087 unsigned NumTemplateArgs; 1088 1089 /// \brief Retrieve the template arguments 1090 TemplateArgument *getTemplateArgs() { 1091 return reinterpret_cast<TemplateArgument *> (this + 1); 1092 } 1093 1094 /// \brief Retrieve the template arguments 1095 const TemplateArgument *getTemplateArgs() const { 1096 return reinterpret_cast<const TemplateArgument *> (this + 1); 1097 } 1098}; 1099 1100/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1101/// 1102class MemberExpr : public Expr { 1103 /// Base - the expression for the base pointer or structure references. In 1104 /// X.F, this is "X". 1105 Stmt *Base; 1106 1107 /// MemberDecl - This is the decl being referenced by the field/member name. 1108 /// In X.F, this is the decl referenced by F. 1109 NamedDecl *MemberDecl; 1110 1111 /// MemberLoc - This is the location of the member name. 1112 SourceLocation MemberLoc; 1113 1114 /// IsArrow - True if this is "X->F", false if this is "X.F". 1115 bool IsArrow : 1; 1116 1117 /// \brief True if this member expression used a nested-name-specifier to 1118 /// refer to the member, e.g., "x->Base::f". When true, a NameQualifier 1119 /// structure is allocated immediately after the MemberExpr. 1120 bool HasQualifier : 1; 1121 1122 /// \brief True if this member expression specified a template argument list 1123 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1124 /// structure (and its TemplateArguments) are allocated immediately after 1125 /// the MemberExpr or, if the member expression also has a qualifier, after 1126 /// the NameQualifier structure. 1127 bool HasExplicitTemplateArgumentList : 1; 1128 1129 /// \brief Retrieve the qualifier that preceded the member name, if any. 1130 NameQualifier *getMemberQualifier() { 1131 if (!HasQualifier) 1132 return 0; 1133 1134 return reinterpret_cast<NameQualifier *> (this + 1); 1135 } 1136 1137 /// \brief Retrieve the qualifier that preceded the member name, if any. 1138 const NameQualifier *getMemberQualifier() const { 1139 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1140 } 1141 1142 /// \brief Retrieve the explicit template argument list that followed the 1143 /// member template name, if any. 1144 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1145 if (!HasExplicitTemplateArgumentList) 1146 return 0; 1147 1148 if (!HasQualifier) 1149 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1150 1151 return reinterpret_cast<ExplicitTemplateArgumentList *>( 1152 getMemberQualifier() + 1); 1153 } 1154 1155 /// \brief Retrieve the explicit template argument list that followed the 1156 /// member template name, if any. 1157 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1158 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList(); 1159 } 1160 1161 MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual, 1162 SourceRange qualrange, NamedDecl *memberdecl, SourceLocation l, 1163 bool has_explicit, SourceLocation langle, 1164 const TemplateArgument *targs, unsigned numtargs, 1165 SourceLocation rangle, QualType ty); 1166 1167public: 1168 MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l, 1169 QualType ty) 1170 : Expr(MemberExprClass, ty, 1171 base->isTypeDependent(), base->isValueDependent()), 1172 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow), 1173 HasQualifier(false), HasExplicitTemplateArgumentList(false) {} 1174 1175 /// \brief Build an empty member reference expression. 1176 explicit MemberExpr(EmptyShell Empty) 1177 : Expr(MemberExprClass, Empty), HasQualifier(false), 1178 HasExplicitTemplateArgumentList(false) { } 1179 1180 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 1181 NestedNameSpecifier *qual, SourceRange qualrange, 1182 NamedDecl *memberdecl, 1183 SourceLocation l, 1184 bool has_explicit, 1185 SourceLocation langle, 1186 const TemplateArgument *targs, 1187 unsigned numtargs, 1188 SourceLocation rangle, 1189 QualType ty); 1190 1191 void setBase(Expr *E) { Base = E; } 1192 Expr *getBase() const { return cast<Expr>(Base); } 1193 1194 /// \brief Retrieve the member declaration to which this expression refers. 1195 /// 1196 /// The returned declaration will either be a FieldDecl or (in C++) 1197 /// a CXXMethodDecl. 1198 NamedDecl *getMemberDecl() const { return MemberDecl; } 1199 void setMemberDecl(NamedDecl *D) { MemberDecl = D; } 1200 1201 /// \brief Determines whether this member expression actually had 1202 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1203 /// x->Base::foo. 1204 bool hasQualifier() const { return HasQualifier; } 1205 1206 /// \brief If the member name was qualified, retrieves the source range of 1207 /// the nested-name-specifier that precedes the member name. Otherwise, 1208 /// returns an empty source range. 1209 SourceRange getQualifierRange() const { 1210 if (!HasQualifier) 1211 return SourceRange(); 1212 1213 return getMemberQualifier()->Range; 1214 } 1215 1216 /// \brief If the member name was qualified, retrieves the 1217 /// nested-name-specifier that precedes the member name. Otherwise, returns 1218 /// NULL. 1219 NestedNameSpecifier *getQualifier() const { 1220 if (!HasQualifier) 1221 return 0; 1222 1223 return getMemberQualifier()->NNS; 1224 } 1225 1226 /// \brief Determines whether this member expression actually had a C++ 1227 /// template argument list explicitly specified, e.g., x.f<int>. 1228 bool hasExplicitTemplateArgumentList() { 1229 return HasExplicitTemplateArgumentList; 1230 } 1231 1232 /// \brief Retrieve the location of the left angle bracket following the 1233 /// member name ('<'), if any. 1234 SourceLocation getLAngleLoc() const { 1235 if (!HasExplicitTemplateArgumentList) 1236 return SourceLocation(); 1237 1238 return getExplicitTemplateArgumentList()->LAngleLoc; 1239 } 1240 1241 /// \brief Retrieve the template arguments provided as part of this 1242 /// template-id. 1243 const TemplateArgument *getTemplateArgs() const { 1244 if (!HasExplicitTemplateArgumentList) 1245 return 0; 1246 1247 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1248 } 1249 1250 /// \brief Retrieve the number of template arguments provided as part of this 1251 /// template-id. 1252 unsigned getNumTemplateArgs() const { 1253 if (!HasExplicitTemplateArgumentList) 1254 return 0; 1255 1256 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1257 } 1258 1259 /// \brief Retrieve the location of the right angle bracket following the 1260 /// template arguments ('>'). 1261 SourceLocation getRAngleLoc() const { 1262 if (!HasExplicitTemplateArgumentList) 1263 return SourceLocation(); 1264 1265 return getExplicitTemplateArgumentList()->RAngleLoc; 1266 } 1267 1268 bool isArrow() const { return IsArrow; } 1269 void setArrow(bool A) { IsArrow = A; } 1270 1271 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1272 /// location of 'F'. 1273 SourceLocation getMemberLoc() const { return MemberLoc; } 1274 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1275 1276 virtual SourceRange getSourceRange() const { 1277 // If we have an implicit base (like a C++ implicit this), 1278 // make sure not to return its location 1279 SourceLocation EndLoc = MemberLoc; 1280 if (HasExplicitTemplateArgumentList) 1281 EndLoc = getRAngleLoc(); 1282 1283 SourceLocation BaseLoc = getBase()->getLocStart(); 1284 if (BaseLoc.isInvalid()) 1285 return SourceRange(MemberLoc, EndLoc); 1286 return SourceRange(BaseLoc, EndLoc); 1287 } 1288 1289 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1290 1291 static bool classof(const Stmt *T) { 1292 return T->getStmtClass() == MemberExprClass; 1293 } 1294 static bool classof(const MemberExpr *) { return true; } 1295 1296 // Iterators 1297 virtual child_iterator child_begin(); 1298 virtual child_iterator child_end(); 1299}; 1300 1301/// CompoundLiteralExpr - [C99 6.5.2.5] 1302/// 1303class CompoundLiteralExpr : public Expr { 1304 /// LParenLoc - If non-null, this is the location of the left paren in a 1305 /// compound literal like "(int){4}". This can be null if this is a 1306 /// synthesized compound expression. 1307 SourceLocation LParenLoc; 1308 Stmt *Init; 1309 bool FileScope; 1310public: 1311 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 1312 bool fileScope) 1313 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 1314 FileScope(fileScope) {} 1315 1316 /// \brief Construct an empty compound literal. 1317 explicit CompoundLiteralExpr(EmptyShell Empty) 1318 : Expr(CompoundLiteralExprClass, Empty) { } 1319 1320 const Expr *getInitializer() const { return cast<Expr>(Init); } 1321 Expr *getInitializer() { return cast<Expr>(Init); } 1322 void setInitializer(Expr *E) { Init = E; } 1323 1324 bool isFileScope() const { return FileScope; } 1325 void setFileScope(bool FS) { FileScope = FS; } 1326 1327 SourceLocation getLParenLoc() const { return LParenLoc; } 1328 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1329 1330 virtual SourceRange getSourceRange() const { 1331 // FIXME: Init should never be null. 1332 if (!Init) 1333 return SourceRange(); 1334 if (LParenLoc.isInvalid()) 1335 return Init->getSourceRange(); 1336 return SourceRange(LParenLoc, Init->getLocEnd()); 1337 } 1338 1339 static bool classof(const Stmt *T) { 1340 return T->getStmtClass() == CompoundLiteralExprClass; 1341 } 1342 static bool classof(const CompoundLiteralExpr *) { return true; } 1343 1344 // Iterators 1345 virtual child_iterator child_begin(); 1346 virtual child_iterator child_end(); 1347}; 1348 1349/// CastExpr - Base class for type casts, including both implicit 1350/// casts (ImplicitCastExpr) and explicit casts that have some 1351/// representation in the source code (ExplicitCastExpr's derived 1352/// classes). 1353class CastExpr : public Expr { 1354public: 1355 /// CastKind - the kind of cast this represents. 1356 enum CastKind { 1357 /// CK_Unknown - Unknown cast kind. 1358 /// FIXME: The goal is to get rid of this and make all casts have a 1359 /// kind so that the AST client doesn't have to try to figure out what's 1360 /// going on. 1361 CK_Unknown, 1362 1363 /// CK_BitCast - Used for reinterpret_cast. 1364 CK_BitCast, 1365 1366 /// CK_NoOp - Used for const_cast. 1367 CK_NoOp, 1368 1369 /// CK_DerivedToBase - Derived to base class casts. 1370 CK_DerivedToBase, 1371 1372 /// CK_Dynamic - Dynamic cast. 1373 CK_Dynamic, 1374 1375 /// CK_ToUnion - Cast to union (GCC extension). 1376 CK_ToUnion, 1377 1378 /// CK_ArrayToPointerDecay - Array to pointer decay. 1379 CK_ArrayToPointerDecay, 1380 1381 // CK_FunctionToPointerDecay - Function to pointer decay. 1382 CK_FunctionToPointerDecay, 1383 1384 /// CK_NullToMemberPointer - Null pointer to member pointer. 1385 CK_NullToMemberPointer, 1386 1387 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to 1388 /// member pointer in derived class. 1389 CK_BaseToDerivedMemberPointer, 1390 1391 /// CK_UserDefinedConversion - Conversion using a user defined type 1392 /// conversion function. 1393 CK_UserDefinedConversion, 1394 1395 /// CK_ConstructorConversion - Conversion by constructor 1396 CK_ConstructorConversion, 1397 1398 /// CK_IntegralToPointer - Integral to pointer 1399 CK_IntegralToPointer, 1400 1401 /// CK_PointerToIntegral - Pointer to integral 1402 CK_PointerToIntegral, 1403 1404 /// CK_ToVoid - Cast to void. 1405 CK_ToVoid, 1406 1407 /// CK_VectorSplat - Casting from an integer/floating type to an extended 1408 /// vector type with the same element type as the src type. Splats the 1409 /// src expression into the destionation expression. 1410 CK_VectorSplat, 1411 1412 /// CK_IntegralCast - Casting between integral types of different size. 1413 CK_IntegralCast, 1414 1415 /// CK_IntegralToFloating - Integral to floating point. 1416 CK_IntegralToFloating, 1417 1418 /// CK_FloatingToIntegral - Floating point to integral. 1419 CK_FloatingToIntegral, 1420 1421 /// CK_FloatingCast - Casting between floating types of different size. 1422 CK_FloatingCast 1423 }; 1424 1425private: 1426 CastKind Kind; 1427 Stmt *Op; 1428protected: 1429 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) : 1430 Expr(SC, ty, 1431 // Cast expressions are type-dependent if the type is 1432 // dependent (C++ [temp.dep.expr]p3). 1433 ty->isDependentType(), 1434 // Cast expressions are value-dependent if the type is 1435 // dependent or if the subexpression is value-dependent. 1436 ty->isDependentType() || (op && op->isValueDependent())), 1437 Kind(kind), Op(op) {} 1438 1439 /// \brief Construct an empty cast. 1440 CastExpr(StmtClass SC, EmptyShell Empty) 1441 : Expr(SC, Empty) { } 1442 1443public: 1444 CastKind getCastKind() const { return Kind; } 1445 void setCastKind(CastKind K) { Kind = K; } 1446 const char *getCastKindName() const; 1447 1448 Expr *getSubExpr() { return cast<Expr>(Op); } 1449 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1450 void setSubExpr(Expr *E) { Op = E; } 1451 1452 static bool classof(const Stmt *T) { 1453 StmtClass SC = T->getStmtClass(); 1454 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1455 return true; 1456 1457 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1458 return true; 1459 1460 return false; 1461 } 1462 static bool classof(const CastExpr *) { return true; } 1463 1464 // Iterators 1465 virtual child_iterator child_begin(); 1466 virtual child_iterator child_end(); 1467}; 1468 1469/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1470/// conversions, which have no direct representation in the original 1471/// source code. For example: converting T[]->T*, void f()->void 1472/// (*f)(), float->double, short->int, etc. 1473/// 1474/// In C, implicit casts always produce rvalues. However, in C++, an 1475/// implicit cast whose result is being bound to a reference will be 1476/// an lvalue. For example: 1477/// 1478/// @code 1479/// class Base { }; 1480/// class Derived : public Base { }; 1481/// void f(Derived d) { 1482/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1483/// } 1484/// @endcode 1485class ImplicitCastExpr : public CastExpr { 1486 /// LvalueCast - Whether this cast produces an lvalue. 1487 bool LvalueCast; 1488 1489public: 1490 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) : 1491 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { } 1492 1493 /// \brief Construct an empty implicit cast. 1494 explicit ImplicitCastExpr(EmptyShell Shell) 1495 : CastExpr(ImplicitCastExprClass, Shell) { } 1496 1497 1498 virtual SourceRange getSourceRange() const { 1499 return getSubExpr()->getSourceRange(); 1500 } 1501 1502 /// isLvalueCast - Whether this cast produces an lvalue. 1503 bool isLvalueCast() const { return LvalueCast; } 1504 1505 /// setLvalueCast - Set whether this cast produces an lvalue. 1506 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1507 1508 static bool classof(const Stmt *T) { 1509 return T->getStmtClass() == ImplicitCastExprClass; 1510 } 1511 static bool classof(const ImplicitCastExpr *) { return true; } 1512}; 1513 1514/// ExplicitCastExpr - An explicit cast written in the source 1515/// code. 1516/// 1517/// This class is effectively an abstract class, because it provides 1518/// the basic representation of an explicitly-written cast without 1519/// specifying which kind of cast (C cast, functional cast, static 1520/// cast, etc.) was written; specific derived classes represent the 1521/// particular style of cast and its location information. 1522/// 1523/// Unlike implicit casts, explicit cast nodes have two different 1524/// types: the type that was written into the source code, and the 1525/// actual type of the expression as determined by semantic 1526/// analysis. These types may differ slightly. For example, in C++ one 1527/// can cast to a reference type, which indicates that the resulting 1528/// expression will be an lvalue. The reference type, however, will 1529/// not be used as the type of the expression. 1530class ExplicitCastExpr : public CastExpr { 1531 /// TypeAsWritten - The type that this expression is casting to, as 1532 /// written in the source code. 1533 QualType TypeAsWritten; 1534 1535protected: 1536 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 1537 Expr *op, QualType writtenTy) 1538 : CastExpr(SC, exprTy, kind, op), TypeAsWritten(writtenTy) {} 1539 1540 /// \brief Construct an empty explicit cast. 1541 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 1542 : CastExpr(SC, Shell) { } 1543 1544public: 1545 /// getTypeAsWritten - Returns the type that this expression is 1546 /// casting to, as written in the source code. 1547 QualType getTypeAsWritten() const { return TypeAsWritten; } 1548 void setTypeAsWritten(QualType T) { TypeAsWritten = T; } 1549 1550 static bool classof(const Stmt *T) { 1551 StmtClass SC = T->getStmtClass(); 1552 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1553 return true; 1554 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1555 return true; 1556 1557 return false; 1558 } 1559 static bool classof(const ExplicitCastExpr *) { return true; } 1560}; 1561 1562/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1563/// cast in C++ (C++ [expr.cast]), which uses the syntax 1564/// (Type)expr. For example: @c (int)f. 1565class CStyleCastExpr : public ExplicitCastExpr { 1566 SourceLocation LPLoc; // the location of the left paren 1567 SourceLocation RPLoc; // the location of the right paren 1568public: 1569 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy, 1570 SourceLocation l, SourceLocation r) : 1571 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy), 1572 LPLoc(l), RPLoc(r) {} 1573 1574 /// \brief Construct an empty C-style explicit cast. 1575 explicit CStyleCastExpr(EmptyShell Shell) 1576 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 1577 1578 SourceLocation getLParenLoc() const { return LPLoc; } 1579 void setLParenLoc(SourceLocation L) { LPLoc = L; } 1580 1581 SourceLocation getRParenLoc() const { return RPLoc; } 1582 void setRParenLoc(SourceLocation L) { RPLoc = L; } 1583 1584 virtual SourceRange getSourceRange() const { 1585 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1586 } 1587 static bool classof(const Stmt *T) { 1588 return T->getStmtClass() == CStyleCastExprClass; 1589 } 1590 static bool classof(const CStyleCastExpr *) { return true; } 1591}; 1592 1593/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 1594/// 1595/// This expression node kind describes a builtin binary operation, 1596/// such as "x + y" for integer values "x" and "y". The operands will 1597/// already have been converted to appropriate types (e.g., by 1598/// performing promotions or conversions). 1599/// 1600/// In C++, where operators may be overloaded, a different kind of 1601/// expression node (CXXOperatorCallExpr) is used to express the 1602/// invocation of an overloaded operator with operator syntax. Within 1603/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 1604/// used to store an expression "x + y" depends on the subexpressions 1605/// for x and y. If neither x or y is type-dependent, and the "+" 1606/// operator resolves to a built-in operation, BinaryOperator will be 1607/// used to express the computation (x and y may still be 1608/// value-dependent). If either x or y is type-dependent, or if the 1609/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 1610/// be used to express the computation. 1611class BinaryOperator : public Expr { 1612public: 1613 enum Opcode { 1614 // Operators listed in order of precedence. 1615 // Note that additions to this should also update the StmtVisitor class. 1616 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 1617 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1618 Add, Sub, // [C99 6.5.6] Additive operators. 1619 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1620 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1621 EQ, NE, // [C99 6.5.9] Equality operators. 1622 And, // [C99 6.5.10] Bitwise AND operator. 1623 Xor, // [C99 6.5.11] Bitwise XOR operator. 1624 Or, // [C99 6.5.12] Bitwise OR operator. 1625 LAnd, // [C99 6.5.13] Logical AND operator. 1626 LOr, // [C99 6.5.14] Logical OR operator. 1627 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1628 DivAssign, RemAssign, 1629 AddAssign, SubAssign, 1630 ShlAssign, ShrAssign, 1631 AndAssign, XorAssign, 1632 OrAssign, 1633 Comma // [C99 6.5.17] Comma operator. 1634 }; 1635private: 1636 enum { LHS, RHS, END_EXPR }; 1637 Stmt* SubExprs[END_EXPR]; 1638 Opcode Opc; 1639 SourceLocation OpLoc; 1640public: 1641 1642 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1643 SourceLocation opLoc) 1644 : Expr(BinaryOperatorClass, ResTy, 1645 lhs->isTypeDependent() || rhs->isTypeDependent(), 1646 lhs->isValueDependent() || rhs->isValueDependent()), 1647 Opc(opc), OpLoc(opLoc) { 1648 SubExprs[LHS] = lhs; 1649 SubExprs[RHS] = rhs; 1650 assert(!isCompoundAssignmentOp() && 1651 "Use ArithAssignBinaryOperator for compound assignments"); 1652 } 1653 1654 /// \brief Construct an empty binary operator. 1655 explicit BinaryOperator(EmptyShell Empty) 1656 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 1657 1658 SourceLocation getOperatorLoc() const { return OpLoc; } 1659 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1660 1661 Opcode getOpcode() const { return Opc; } 1662 void setOpcode(Opcode O) { Opc = O; } 1663 1664 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1665 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1666 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1667 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1668 1669 virtual SourceRange getSourceRange() const { 1670 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1671 } 1672 1673 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1674 /// corresponds to, e.g. "<<=". 1675 static const char *getOpcodeStr(Opcode Op); 1676 1677 /// \brief Retrieve the binary opcode that corresponds to the given 1678 /// overloaded operator. 1679 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 1680 1681 /// \brief Retrieve the overloaded operator kind that corresponds to 1682 /// the given binary opcode. 1683 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1684 1685 /// predicates to categorize the respective opcodes. 1686 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1687 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1688 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; } 1689 bool isShiftOp() const { return isShiftOp(Opc); } 1690 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 1691 1692 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1693 bool isRelationalOp() const { return isRelationalOp(Opc); } 1694 1695 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1696 bool isEqualityOp() const { return isEqualityOp(Opc); } 1697 1698 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1699 bool isLogicalOp() const { return isLogicalOp(Opc); } 1700 1701 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1702 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1703 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1704 1705 static bool classof(const Stmt *S) { 1706 return S->getStmtClass() == BinaryOperatorClass || 1707 S->getStmtClass() == CompoundAssignOperatorClass; 1708 } 1709 static bool classof(const BinaryOperator *) { return true; } 1710 1711 // Iterators 1712 virtual child_iterator child_begin(); 1713 virtual child_iterator child_end(); 1714 1715protected: 1716 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1717 SourceLocation oploc, bool dead) 1718 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1719 SubExprs[LHS] = lhs; 1720 SubExprs[RHS] = rhs; 1721 } 1722 1723 BinaryOperator(StmtClass SC, EmptyShell Empty) 1724 : Expr(SC, Empty), Opc(MulAssign) { } 1725}; 1726 1727/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1728/// track of the type the operation is performed in. Due to the semantics of 1729/// these operators, the operands are promoted, the aritmetic performed, an 1730/// implicit conversion back to the result type done, then the assignment takes 1731/// place. This captures the intermediate type which the computation is done 1732/// in. 1733class CompoundAssignOperator : public BinaryOperator { 1734 QualType ComputationLHSType; 1735 QualType ComputationResultType; 1736public: 1737 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1738 QualType ResType, QualType CompLHSType, 1739 QualType CompResultType, 1740 SourceLocation OpLoc) 1741 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1742 ComputationLHSType(CompLHSType), 1743 ComputationResultType(CompResultType) { 1744 assert(isCompoundAssignmentOp() && 1745 "Only should be used for compound assignments"); 1746 } 1747 1748 /// \brief Build an empty compound assignment operator expression. 1749 explicit CompoundAssignOperator(EmptyShell Empty) 1750 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 1751 1752 // The two computation types are the type the LHS is converted 1753 // to for the computation and the type of the result; the two are 1754 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 1755 QualType getComputationLHSType() const { return ComputationLHSType; } 1756 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 1757 1758 QualType getComputationResultType() const { return ComputationResultType; } 1759 void setComputationResultType(QualType T) { ComputationResultType = T; } 1760 1761 static bool classof(const CompoundAssignOperator *) { return true; } 1762 static bool classof(const Stmt *S) { 1763 return S->getStmtClass() == CompoundAssignOperatorClass; 1764 } 1765}; 1766 1767/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1768/// GNU "missing LHS" extension is in use. 1769/// 1770class ConditionalOperator : public Expr { 1771 enum { COND, LHS, RHS, END_EXPR }; 1772 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1773 SourceLocation QuestionLoc, ColonLoc; 1774public: 1775 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 1776 SourceLocation CLoc, Expr *rhs, QualType t) 1777 : Expr(ConditionalOperatorClass, t, 1778 // FIXME: the type of the conditional operator doesn't 1779 // depend on the type of the conditional, but the standard 1780 // seems to imply that it could. File a bug! 1781 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1782 (cond->isValueDependent() || 1783 (lhs && lhs->isValueDependent()) || 1784 (rhs && rhs->isValueDependent()))), 1785 QuestionLoc(QLoc), 1786 ColonLoc(CLoc) { 1787 SubExprs[COND] = cond; 1788 SubExprs[LHS] = lhs; 1789 SubExprs[RHS] = rhs; 1790 } 1791 1792 /// \brief Build an empty conditional operator. 1793 explicit ConditionalOperator(EmptyShell Empty) 1794 : Expr(ConditionalOperatorClass, Empty) { } 1795 1796 // getCond - Return the expression representing the condition for 1797 // the ?: operator. 1798 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1799 void setCond(Expr *E) { SubExprs[COND] = E; } 1800 1801 // getTrueExpr - Return the subexpression representing the value of the ?: 1802 // expression if the condition evaluates to true. In most cases this value 1803 // will be the same as getLHS() except a GCC extension allows the left 1804 // subexpression to be omitted, and instead of the condition be returned. 1805 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1806 // is only evaluated once. 1807 Expr *getTrueExpr() const { 1808 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1809 } 1810 1811 // getTrueExpr - Return the subexpression representing the value of the ?: 1812 // expression if the condition evaluates to false. This is the same as getRHS. 1813 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1814 1815 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1816 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1817 1818 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1819 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1820 1821 SourceLocation getQuestionLoc() const { return QuestionLoc; } 1822 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 1823 1824 SourceLocation getColonLoc() const { return ColonLoc; } 1825 void setColonLoc(SourceLocation L) { ColonLoc = L; } 1826 1827 virtual SourceRange getSourceRange() const { 1828 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1829 } 1830 static bool classof(const Stmt *T) { 1831 return T->getStmtClass() == ConditionalOperatorClass; 1832 } 1833 static bool classof(const ConditionalOperator *) { return true; } 1834 1835 // Iterators 1836 virtual child_iterator child_begin(); 1837 virtual child_iterator child_end(); 1838}; 1839 1840/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1841class AddrLabelExpr : public Expr { 1842 SourceLocation AmpAmpLoc, LabelLoc; 1843 LabelStmt *Label; 1844public: 1845 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1846 QualType t) 1847 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1848 1849 /// \brief Build an empty address of a label expression. 1850 explicit AddrLabelExpr(EmptyShell Empty) 1851 : Expr(AddrLabelExprClass, Empty) { } 1852 1853 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 1854 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 1855 SourceLocation getLabelLoc() const { return LabelLoc; } 1856 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 1857 1858 virtual SourceRange getSourceRange() const { 1859 return SourceRange(AmpAmpLoc, LabelLoc); 1860 } 1861 1862 LabelStmt *getLabel() const { return Label; } 1863 void setLabel(LabelStmt *S) { Label = S; } 1864 1865 static bool classof(const Stmt *T) { 1866 return T->getStmtClass() == AddrLabelExprClass; 1867 } 1868 static bool classof(const AddrLabelExpr *) { return true; } 1869 1870 // Iterators 1871 virtual child_iterator child_begin(); 1872 virtual child_iterator child_end(); 1873}; 1874 1875/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1876/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1877/// takes the value of the last subexpression. 1878class StmtExpr : public Expr { 1879 Stmt *SubStmt; 1880 SourceLocation LParenLoc, RParenLoc; 1881public: 1882 StmtExpr(CompoundStmt *substmt, QualType T, 1883 SourceLocation lp, SourceLocation rp) : 1884 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1885 1886 /// \brief Build an empty statement expression. 1887 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 1888 1889 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1890 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1891 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 1892 1893 virtual SourceRange getSourceRange() const { 1894 return SourceRange(LParenLoc, RParenLoc); 1895 } 1896 1897 SourceLocation getLParenLoc() const { return LParenLoc; } 1898 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1899 SourceLocation getRParenLoc() const { return RParenLoc; } 1900 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1901 1902 static bool classof(const Stmt *T) { 1903 return T->getStmtClass() == StmtExprClass; 1904 } 1905 static bool classof(const StmtExpr *) { return true; } 1906 1907 // Iterators 1908 virtual child_iterator child_begin(); 1909 virtual child_iterator child_end(); 1910}; 1911 1912/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 1913/// This AST node represents a function that returns 1 if two *types* (not 1914/// expressions) are compatible. The result of this built-in function can be 1915/// used in integer constant expressions. 1916class TypesCompatibleExpr : public Expr { 1917 QualType Type1; 1918 QualType Type2; 1919 SourceLocation BuiltinLoc, RParenLoc; 1920public: 1921 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1922 QualType t1, QualType t2, SourceLocation RP) : 1923 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1924 BuiltinLoc(BLoc), RParenLoc(RP) {} 1925 1926 /// \brief Build an empty __builtin_type_compatible_p expression. 1927 explicit TypesCompatibleExpr(EmptyShell Empty) 1928 : Expr(TypesCompatibleExprClass, Empty) { } 1929 1930 QualType getArgType1() const { return Type1; } 1931 void setArgType1(QualType T) { Type1 = T; } 1932 QualType getArgType2() const { return Type2; } 1933 void setArgType2(QualType T) { Type2 = T; } 1934 1935 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1936 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1937 1938 SourceLocation getRParenLoc() const { return RParenLoc; } 1939 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1940 1941 virtual SourceRange getSourceRange() const { 1942 return SourceRange(BuiltinLoc, RParenLoc); 1943 } 1944 static bool classof(const Stmt *T) { 1945 return T->getStmtClass() == TypesCompatibleExprClass; 1946 } 1947 static bool classof(const TypesCompatibleExpr *) { return true; } 1948 1949 // Iterators 1950 virtual child_iterator child_begin(); 1951 virtual child_iterator child_end(); 1952}; 1953 1954/// ShuffleVectorExpr - clang-specific builtin-in function 1955/// __builtin_shufflevector. 1956/// This AST node represents a operator that does a constant 1957/// shuffle, similar to LLVM's shufflevector instruction. It takes 1958/// two vectors and a variable number of constant indices, 1959/// and returns the appropriately shuffled vector. 1960class ShuffleVectorExpr : public Expr { 1961 SourceLocation BuiltinLoc, RParenLoc; 1962 1963 // SubExprs - the list of values passed to the __builtin_shufflevector 1964 // function. The first two are vectors, and the rest are constant 1965 // indices. The number of values in this list is always 1966 // 2+the number of indices in the vector type. 1967 Stmt **SubExprs; 1968 unsigned NumExprs; 1969 1970protected: 1971 virtual void DoDestroy(ASTContext &C); 1972 1973public: 1974 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 1975 QualType Type, SourceLocation BLoc, 1976 SourceLocation RP) : 1977 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1978 RParenLoc(RP), NumExprs(nexpr) { 1979 1980 SubExprs = new (C) Stmt*[nexpr]; 1981 for (unsigned i = 0; i < nexpr; i++) 1982 SubExprs[i] = args[i]; 1983 } 1984 1985 /// \brief Build an empty vector-shuffle expression. 1986 explicit ShuffleVectorExpr(EmptyShell Empty) 1987 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 1988 1989 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1990 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1991 1992 SourceLocation getRParenLoc() const { return RParenLoc; } 1993 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1994 1995 virtual SourceRange getSourceRange() const { 1996 return SourceRange(BuiltinLoc, RParenLoc); 1997 } 1998 static bool classof(const Stmt *T) { 1999 return T->getStmtClass() == ShuffleVectorExprClass; 2000 } 2001 static bool classof(const ShuffleVectorExpr *) { return true; } 2002 2003 ~ShuffleVectorExpr() {} 2004 2005 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 2006 /// constant expression, the actual arguments passed in, and the function 2007 /// pointers. 2008 unsigned getNumSubExprs() const { return NumExprs; } 2009 2010 /// getExpr - Return the Expr at the specified index. 2011 Expr *getExpr(unsigned Index) { 2012 assert((Index < NumExprs) && "Arg access out of range!"); 2013 return cast<Expr>(SubExprs[Index]); 2014 } 2015 const Expr *getExpr(unsigned Index) const { 2016 assert((Index < NumExprs) && "Arg access out of range!"); 2017 return cast<Expr>(SubExprs[Index]); 2018 } 2019 2020 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 2021 2022 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 2023 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 2024 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 2025 } 2026 2027 // Iterators 2028 virtual child_iterator child_begin(); 2029 virtual child_iterator child_end(); 2030}; 2031 2032/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 2033/// This AST node is similar to the conditional operator (?:) in C, with 2034/// the following exceptions: 2035/// - the test expression must be a integer constant expression. 2036/// - the expression returned acts like the chosen subexpression in every 2037/// visible way: the type is the same as that of the chosen subexpression, 2038/// and all predicates (whether it's an l-value, whether it's an integer 2039/// constant expression, etc.) return the same result as for the chosen 2040/// sub-expression. 2041class ChooseExpr : public Expr { 2042 enum { COND, LHS, RHS, END_EXPR }; 2043 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2044 SourceLocation BuiltinLoc, RParenLoc; 2045public: 2046 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2047 SourceLocation RP, bool TypeDependent, bool ValueDependent) 2048 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent), 2049 BuiltinLoc(BLoc), RParenLoc(RP) { 2050 SubExprs[COND] = cond; 2051 SubExprs[LHS] = lhs; 2052 SubExprs[RHS] = rhs; 2053 } 2054 2055 /// \brief Build an empty __builtin_choose_expr. 2056 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2057 2058 /// isConditionTrue - Return whether the condition is true (i.e. not 2059 /// equal to zero). 2060 bool isConditionTrue(ASTContext &C) const; 2061 2062 /// getChosenSubExpr - Return the subexpression chosen according to the 2063 /// condition. 2064 Expr *getChosenSubExpr(ASTContext &C) const { 2065 return isConditionTrue(C) ? getLHS() : getRHS(); 2066 } 2067 2068 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2069 void setCond(Expr *E) { SubExprs[COND] = E; } 2070 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2071 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2072 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2073 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2074 2075 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2076 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2077 2078 SourceLocation getRParenLoc() const { return RParenLoc; } 2079 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2080 2081 virtual SourceRange getSourceRange() const { 2082 return SourceRange(BuiltinLoc, RParenLoc); 2083 } 2084 static bool classof(const Stmt *T) { 2085 return T->getStmtClass() == ChooseExprClass; 2086 } 2087 static bool classof(const ChooseExpr *) { return true; } 2088 2089 // Iterators 2090 virtual child_iterator child_begin(); 2091 virtual child_iterator child_end(); 2092}; 2093 2094/// GNUNullExpr - Implements the GNU __null extension, which is a name 2095/// for a null pointer constant that has integral type (e.g., int or 2096/// long) and is the same size and alignment as a pointer. The __null 2097/// extension is typically only used by system headers, which define 2098/// NULL as __null in C++ rather than using 0 (which is an integer 2099/// that may not match the size of a pointer). 2100class GNUNullExpr : public Expr { 2101 /// TokenLoc - The location of the __null keyword. 2102 SourceLocation TokenLoc; 2103 2104public: 2105 GNUNullExpr(QualType Ty, SourceLocation Loc) 2106 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 2107 2108 /// \brief Build an empty GNU __null expression. 2109 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2110 2111 /// getTokenLocation - The location of the __null token. 2112 SourceLocation getTokenLocation() const { return TokenLoc; } 2113 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2114 2115 virtual SourceRange getSourceRange() const { 2116 return SourceRange(TokenLoc); 2117 } 2118 static bool classof(const Stmt *T) { 2119 return T->getStmtClass() == GNUNullExprClass; 2120 } 2121 static bool classof(const GNUNullExpr *) { return true; } 2122 2123 // Iterators 2124 virtual child_iterator child_begin(); 2125 virtual child_iterator child_end(); 2126}; 2127 2128/// VAArgExpr, used for the builtin function __builtin_va_start. 2129class VAArgExpr : public Expr { 2130 Stmt *Val; 2131 SourceLocation BuiltinLoc, RParenLoc; 2132public: 2133 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 2134 : Expr(VAArgExprClass, t), 2135 Val(e), 2136 BuiltinLoc(BLoc), 2137 RParenLoc(RPLoc) { } 2138 2139 /// \brief Create an empty __builtin_va_start expression. 2140 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2141 2142 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2143 Expr *getSubExpr() { return cast<Expr>(Val); } 2144 void setSubExpr(Expr *E) { Val = E; } 2145 2146 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2147 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2148 2149 SourceLocation getRParenLoc() const { return RParenLoc; } 2150 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2151 2152 virtual SourceRange getSourceRange() const { 2153 return SourceRange(BuiltinLoc, RParenLoc); 2154 } 2155 static bool classof(const Stmt *T) { 2156 return T->getStmtClass() == VAArgExprClass; 2157 } 2158 static bool classof(const VAArgExpr *) { return true; } 2159 2160 // Iterators 2161 virtual child_iterator child_begin(); 2162 virtual child_iterator child_end(); 2163}; 2164 2165/// @brief Describes an C or C++ initializer list. 2166/// 2167/// InitListExpr describes an initializer list, which can be used to 2168/// initialize objects of different types, including 2169/// struct/class/union types, arrays, and vectors. For example: 2170/// 2171/// @code 2172/// struct foo x = { 1, { 2, 3 } }; 2173/// @endcode 2174/// 2175/// Prior to semantic analysis, an initializer list will represent the 2176/// initializer list as written by the user, but will have the 2177/// placeholder type "void". This initializer list is called the 2178/// syntactic form of the initializer, and may contain C99 designated 2179/// initializers (represented as DesignatedInitExprs), initializations 2180/// of subobject members without explicit braces, and so on. Clients 2181/// interested in the original syntax of the initializer list should 2182/// use the syntactic form of the initializer list. 2183/// 2184/// After semantic analysis, the initializer list will represent the 2185/// semantic form of the initializer, where the initializations of all 2186/// subobjects are made explicit with nested InitListExpr nodes and 2187/// C99 designators have been eliminated by placing the designated 2188/// initializations into the subobject they initialize. Additionally, 2189/// any "holes" in the initialization, where no initializer has been 2190/// specified for a particular subobject, will be replaced with 2191/// implicitly-generated ImplicitValueInitExpr expressions that 2192/// value-initialize the subobjects. Note, however, that the 2193/// initializer lists may still have fewer initializers than there are 2194/// elements to initialize within the object. 2195/// 2196/// Given the semantic form of the initializer list, one can retrieve 2197/// the original syntactic form of that initializer list (if it 2198/// exists) using getSyntacticForm(). Since many initializer lists 2199/// have the same syntactic and semantic forms, getSyntacticForm() may 2200/// return NULL, indicating that the current initializer list also 2201/// serves as its syntactic form. 2202class InitListExpr : public Expr { 2203 // FIXME: Eliminate this vector in favor of ASTContext allocation 2204 std::vector<Stmt *> InitExprs; 2205 SourceLocation LBraceLoc, RBraceLoc; 2206 2207 /// Contains the initializer list that describes the syntactic form 2208 /// written in the source code. 2209 InitListExpr *SyntacticForm; 2210 2211 /// If this initializer list initializes a union, specifies which 2212 /// field within the union will be initialized. 2213 FieldDecl *UnionFieldInit; 2214 2215 /// Whether this initializer list originally had a GNU array-range 2216 /// designator in it. This is a temporary marker used by CodeGen. 2217 bool HadArrayRangeDesignator; 2218 2219public: 2220 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 2221 SourceLocation rbraceloc); 2222 2223 /// \brief Build an empty initializer list. 2224 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { } 2225 2226 unsigned getNumInits() const { return InitExprs.size(); } 2227 2228 const Expr* getInit(unsigned Init) const { 2229 assert(Init < getNumInits() && "Initializer access out of range!"); 2230 return cast_or_null<Expr>(InitExprs[Init]); 2231 } 2232 2233 Expr* getInit(unsigned Init) { 2234 assert(Init < getNumInits() && "Initializer access out of range!"); 2235 return cast_or_null<Expr>(InitExprs[Init]); 2236 } 2237 2238 void setInit(unsigned Init, Expr *expr) { 2239 assert(Init < getNumInits() && "Initializer access out of range!"); 2240 InitExprs[Init] = expr; 2241 } 2242 2243 /// \brief Reserve space for some number of initializers. 2244 void reserveInits(unsigned NumInits); 2245 2246 /// @brief Specify the number of initializers 2247 /// 2248 /// If there are more than @p NumInits initializers, the remaining 2249 /// initializers will be destroyed. If there are fewer than @p 2250 /// NumInits initializers, NULL expressions will be added for the 2251 /// unknown initializers. 2252 void resizeInits(ASTContext &Context, unsigned NumInits); 2253 2254 /// @brief Updates the initializer at index @p Init with the new 2255 /// expression @p expr, and returns the old expression at that 2256 /// location. 2257 /// 2258 /// When @p Init is out of range for this initializer list, the 2259 /// initializer list will be extended with NULL expressions to 2260 /// accomodate the new entry. 2261 Expr *updateInit(unsigned Init, Expr *expr); 2262 2263 /// \brief If this initializes a union, specifies which field in the 2264 /// union to initialize. 2265 /// 2266 /// Typically, this field is the first named field within the 2267 /// union. However, a designated initializer can specify the 2268 /// initialization of a different field within the union. 2269 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2270 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2271 2272 // Explicit InitListExpr's originate from source code (and have valid source 2273 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2274 bool isExplicit() { 2275 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2276 } 2277 2278 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2279 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2280 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2281 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2282 2283 /// @brief Retrieve the initializer list that describes the 2284 /// syntactic form of the initializer. 2285 /// 2286 /// 2287 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2288 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2289 2290 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2291 void sawArrayRangeDesignator(bool ARD = true) { 2292 HadArrayRangeDesignator = ARD; 2293 } 2294 2295 virtual SourceRange getSourceRange() const { 2296 return SourceRange(LBraceLoc, RBraceLoc); 2297 } 2298 static bool classof(const Stmt *T) { 2299 return T->getStmtClass() == InitListExprClass; 2300 } 2301 static bool classof(const InitListExpr *) { return true; } 2302 2303 // Iterators 2304 virtual child_iterator child_begin(); 2305 virtual child_iterator child_end(); 2306 2307 typedef std::vector<Stmt *>::iterator iterator; 2308 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 2309 2310 iterator begin() { return InitExprs.begin(); } 2311 iterator end() { return InitExprs.end(); } 2312 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2313 reverse_iterator rend() { return InitExprs.rend(); } 2314}; 2315 2316/// @brief Represents a C99 designated initializer expression. 2317/// 2318/// A designated initializer expression (C99 6.7.8) contains one or 2319/// more designators (which can be field designators, array 2320/// designators, or GNU array-range designators) followed by an 2321/// expression that initializes the field or element(s) that the 2322/// designators refer to. For example, given: 2323/// 2324/// @code 2325/// struct point { 2326/// double x; 2327/// double y; 2328/// }; 2329/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2330/// @endcode 2331/// 2332/// The InitListExpr contains three DesignatedInitExprs, the first of 2333/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2334/// designators, one array designator for @c [2] followed by one field 2335/// designator for @c .y. The initalization expression will be 1.0. 2336class DesignatedInitExpr : public Expr { 2337public: 2338 /// \brief Forward declaration of the Designator class. 2339 class Designator; 2340 2341private: 2342 /// The location of the '=' or ':' prior to the actual initializer 2343 /// expression. 2344 SourceLocation EqualOrColonLoc; 2345 2346 /// Whether this designated initializer used the GNU deprecated 2347 /// syntax rather than the C99 '=' syntax. 2348 bool GNUSyntax : 1; 2349 2350 /// The number of designators in this initializer expression. 2351 unsigned NumDesignators : 15; 2352 2353 /// \brief The designators in this designated initialization 2354 /// expression. 2355 Designator *Designators; 2356 2357 /// The number of subexpressions of this initializer expression, 2358 /// which contains both the initializer and any additional 2359 /// expressions used by array and array-range designators. 2360 unsigned NumSubExprs : 16; 2361 2362 2363 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 2364 const Designator *Designators, 2365 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2366 Expr **IndexExprs, unsigned NumIndexExprs, 2367 Expr *Init); 2368 2369 explicit DesignatedInitExpr(unsigned NumSubExprs) 2370 : Expr(DesignatedInitExprClass, EmptyShell()), 2371 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2372 2373protected: 2374 virtual void DoDestroy(ASTContext &C); 2375 2376public: 2377 /// A field designator, e.g., ".x". 2378 struct FieldDesignator { 2379 /// Refers to the field that is being initialized. The low bit 2380 /// of this field determines whether this is actually a pointer 2381 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2382 /// initially constructed, a field designator will store an 2383 /// IdentifierInfo*. After semantic analysis has resolved that 2384 /// name, the field designator will instead store a FieldDecl*. 2385 uintptr_t NameOrField; 2386 2387 /// The location of the '.' in the designated initializer. 2388 unsigned DotLoc; 2389 2390 /// The location of the field name in the designated initializer. 2391 unsigned FieldLoc; 2392 }; 2393 2394 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2395 struct ArrayOrRangeDesignator { 2396 /// Location of the first index expression within the designated 2397 /// initializer expression's list of subexpressions. 2398 unsigned Index; 2399 /// The location of the '[' starting the array range designator. 2400 unsigned LBracketLoc; 2401 /// The location of the ellipsis separating the start and end 2402 /// indices. Only valid for GNU array-range designators. 2403 unsigned EllipsisLoc; 2404 /// The location of the ']' terminating the array range designator. 2405 unsigned RBracketLoc; 2406 }; 2407 2408 /// @brief Represents a single C99 designator. 2409 /// 2410 /// @todo This class is infuriatingly similar to clang::Designator, 2411 /// but minor differences (storing indices vs. storing pointers) 2412 /// keep us from reusing it. Try harder, later, to rectify these 2413 /// differences. 2414 class Designator { 2415 /// @brief The kind of designator this describes. 2416 enum { 2417 FieldDesignator, 2418 ArrayDesignator, 2419 ArrayRangeDesignator 2420 } Kind; 2421 2422 union { 2423 /// A field designator, e.g., ".x". 2424 struct FieldDesignator Field; 2425 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2426 struct ArrayOrRangeDesignator ArrayOrRange; 2427 }; 2428 friend class DesignatedInitExpr; 2429 2430 public: 2431 Designator() {} 2432 2433 /// @brief Initializes a field designator. 2434 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2435 SourceLocation FieldLoc) 2436 : Kind(FieldDesignator) { 2437 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2438 Field.DotLoc = DotLoc.getRawEncoding(); 2439 Field.FieldLoc = FieldLoc.getRawEncoding(); 2440 } 2441 2442 /// @brief Initializes an array designator. 2443 Designator(unsigned Index, SourceLocation LBracketLoc, 2444 SourceLocation RBracketLoc) 2445 : Kind(ArrayDesignator) { 2446 ArrayOrRange.Index = Index; 2447 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2448 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2449 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2450 } 2451 2452 /// @brief Initializes a GNU array-range designator. 2453 Designator(unsigned Index, SourceLocation LBracketLoc, 2454 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2455 : Kind(ArrayRangeDesignator) { 2456 ArrayOrRange.Index = Index; 2457 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2458 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2459 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2460 } 2461 2462 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2463 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2464 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2465 2466 IdentifierInfo * getFieldName(); 2467 2468 FieldDecl *getField() { 2469 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2470 if (Field.NameOrField & 0x01) 2471 return 0; 2472 else 2473 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2474 } 2475 2476 void setField(FieldDecl *FD) { 2477 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2478 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2479 } 2480 2481 SourceLocation getDotLoc() const { 2482 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2483 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2484 } 2485 2486 SourceLocation getFieldLoc() const { 2487 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2488 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2489 } 2490 2491 SourceLocation getLBracketLoc() const { 2492 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2493 "Only valid on an array or array-range designator"); 2494 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2495 } 2496 2497 SourceLocation getRBracketLoc() const { 2498 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2499 "Only valid on an array or array-range designator"); 2500 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2501 } 2502 2503 SourceLocation getEllipsisLoc() const { 2504 assert(Kind == ArrayRangeDesignator && 2505 "Only valid on an array-range designator"); 2506 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2507 } 2508 2509 unsigned getFirstExprIndex() const { 2510 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2511 "Only valid on an array or array-range designator"); 2512 return ArrayOrRange.Index; 2513 } 2514 2515 SourceLocation getStartLocation() const { 2516 if (Kind == FieldDesignator) 2517 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2518 else 2519 return getLBracketLoc(); 2520 } 2521 }; 2522 2523 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2524 unsigned NumDesignators, 2525 Expr **IndexExprs, unsigned NumIndexExprs, 2526 SourceLocation EqualOrColonLoc, 2527 bool GNUSyntax, Expr *Init); 2528 2529 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 2530 2531 /// @brief Returns the number of designators in this initializer. 2532 unsigned size() const { return NumDesignators; } 2533 2534 // Iterator access to the designators. 2535 typedef Designator* designators_iterator; 2536 designators_iterator designators_begin() { return Designators; } 2537 designators_iterator designators_end() { 2538 return Designators + NumDesignators; 2539 } 2540 2541 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 2542 2543 void setDesignators(const Designator *Desigs, unsigned NumDesigs); 2544 2545 Expr *getArrayIndex(const Designator& D); 2546 Expr *getArrayRangeStart(const Designator& D); 2547 Expr *getArrayRangeEnd(const Designator& D); 2548 2549 /// @brief Retrieve the location of the '=' that precedes the 2550 /// initializer value itself, if present. 2551 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2552 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 2553 2554 /// @brief Determines whether this designated initializer used the 2555 /// deprecated GNU syntax for designated initializers. 2556 bool usesGNUSyntax() const { return GNUSyntax; } 2557 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 2558 2559 /// @brief Retrieve the initializer value. 2560 Expr *getInit() const { 2561 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2562 } 2563 2564 void setInit(Expr *init) { 2565 *child_begin() = init; 2566 } 2567 2568 /// \brief Retrieve the total number of subexpressions in this 2569 /// designated initializer expression, including the actual 2570 /// initialized value and any expressions that occur within array 2571 /// and array-range designators. 2572 unsigned getNumSubExprs() const { return NumSubExprs; } 2573 2574 Expr *getSubExpr(unsigned Idx) { 2575 assert(Idx < NumSubExprs && "Subscript out of range"); 2576 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2577 Ptr += sizeof(DesignatedInitExpr); 2578 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 2579 } 2580 2581 void setSubExpr(unsigned Idx, Expr *E) { 2582 assert(Idx < NumSubExprs && "Subscript out of range"); 2583 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2584 Ptr += sizeof(DesignatedInitExpr); 2585 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 2586 } 2587 2588 /// \brief Replaces the designator at index @p Idx with the series 2589 /// of designators in [First, Last). 2590 void ExpandDesignator(unsigned Idx, const Designator *First, 2591 const Designator *Last); 2592 2593 virtual SourceRange getSourceRange() const; 2594 2595 static bool classof(const Stmt *T) { 2596 return T->getStmtClass() == DesignatedInitExprClass; 2597 } 2598 static bool classof(const DesignatedInitExpr *) { return true; } 2599 2600 // Iterators 2601 virtual child_iterator child_begin(); 2602 virtual child_iterator child_end(); 2603}; 2604 2605/// \brief Represents an implicitly-generated value initialization of 2606/// an object of a given type. 2607/// 2608/// Implicit value initializations occur within semantic initializer 2609/// list expressions (InitListExpr) as placeholders for subobject 2610/// initializations not explicitly specified by the user. 2611/// 2612/// \see InitListExpr 2613class ImplicitValueInitExpr : public Expr { 2614public: 2615 explicit ImplicitValueInitExpr(QualType ty) 2616 : Expr(ImplicitValueInitExprClass, ty) { } 2617 2618 /// \brief Construct an empty implicit value initialization. 2619 explicit ImplicitValueInitExpr(EmptyShell Empty) 2620 : Expr(ImplicitValueInitExprClass, Empty) { } 2621 2622 static bool classof(const Stmt *T) { 2623 return T->getStmtClass() == ImplicitValueInitExprClass; 2624 } 2625 static bool classof(const ImplicitValueInitExpr *) { return true; } 2626 2627 virtual SourceRange getSourceRange() const { 2628 return SourceRange(); 2629 } 2630 2631 // Iterators 2632 virtual child_iterator child_begin(); 2633 virtual child_iterator child_end(); 2634}; 2635 2636 2637class ParenListExpr : public Expr { 2638 Stmt **Exprs; 2639 unsigned NumExprs; 2640 SourceLocation LParenLoc, RParenLoc; 2641 2642protected: 2643 virtual void DoDestroy(ASTContext& C); 2644 2645public: 2646 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 2647 unsigned numexprs, SourceLocation rparenloc); 2648 2649 ~ParenListExpr() {} 2650 2651 /// \brief Build an empty paren list. 2652 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 2653 2654 unsigned getNumExprs() const { return NumExprs; } 2655 2656 const Expr* getExpr(unsigned Init) const { 2657 assert(Init < getNumExprs() && "Initializer access out of range!"); 2658 return cast_or_null<Expr>(Exprs[Init]); 2659 } 2660 2661 Expr* getExpr(unsigned Init) { 2662 assert(Init < getNumExprs() && "Initializer access out of range!"); 2663 return cast_or_null<Expr>(Exprs[Init]); 2664 } 2665 2666 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 2667 2668 SourceLocation getLParenLoc() const { return LParenLoc; } 2669 SourceLocation getRParenLoc() const { return RParenLoc; } 2670 2671 virtual SourceRange getSourceRange() const { 2672 return SourceRange(LParenLoc, RParenLoc); 2673 } 2674 static bool classof(const Stmt *T) { 2675 return T->getStmtClass() == ParenListExprClass; 2676 } 2677 static bool classof(const ParenListExpr *) { return true; } 2678 2679 // Iterators 2680 virtual child_iterator child_begin(); 2681 virtual child_iterator child_end(); 2682}; 2683 2684 2685//===----------------------------------------------------------------------===// 2686// Clang Extensions 2687//===----------------------------------------------------------------------===// 2688 2689 2690/// ExtVectorElementExpr - This represents access to specific elements of a 2691/// vector, and may occur on the left hand side or right hand side. For example 2692/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2693/// 2694/// Note that the base may have either vector or pointer to vector type, just 2695/// like a struct field reference. 2696/// 2697class ExtVectorElementExpr : public Expr { 2698 Stmt *Base; 2699 IdentifierInfo *Accessor; 2700 SourceLocation AccessorLoc; 2701public: 2702 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2703 SourceLocation loc) 2704 : Expr(ExtVectorElementExprClass, ty), 2705 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 2706 2707 /// \brief Build an empty vector element expression. 2708 explicit ExtVectorElementExpr(EmptyShell Empty) 2709 : Expr(ExtVectorElementExprClass, Empty) { } 2710 2711 const Expr *getBase() const { return cast<Expr>(Base); } 2712 Expr *getBase() { return cast<Expr>(Base); } 2713 void setBase(Expr *E) { Base = E; } 2714 2715 IdentifierInfo &getAccessor() const { return *Accessor; } 2716 void setAccessor(IdentifierInfo *II) { Accessor = II; } 2717 2718 SourceLocation getAccessorLoc() const { return AccessorLoc; } 2719 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 2720 2721 /// getNumElements - Get the number of components being selected. 2722 unsigned getNumElements() const; 2723 2724 /// containsDuplicateElements - Return true if any element access is 2725 /// repeated. 2726 bool containsDuplicateElements() const; 2727 2728 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2729 /// aggregate Constant of ConstantInt(s). 2730 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2731 2732 virtual SourceRange getSourceRange() const { 2733 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2734 } 2735 2736 /// isArrow - Return true if the base expression is a pointer to vector, 2737 /// return false if the base expression is a vector. 2738 bool isArrow() const; 2739 2740 static bool classof(const Stmt *T) { 2741 return T->getStmtClass() == ExtVectorElementExprClass; 2742 } 2743 static bool classof(const ExtVectorElementExpr *) { return true; } 2744 2745 // Iterators 2746 virtual child_iterator child_begin(); 2747 virtual child_iterator child_end(); 2748}; 2749 2750 2751/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2752/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2753class BlockExpr : public Expr { 2754protected: 2755 BlockDecl *TheBlock; 2756 bool HasBlockDeclRefExprs; 2757public: 2758 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2759 : Expr(BlockExprClass, ty), 2760 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2761 2762 /// \brief Build an empty block expression. 2763 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 2764 2765 const BlockDecl *getBlockDecl() const { return TheBlock; } 2766 BlockDecl *getBlockDecl() { return TheBlock; } 2767 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 2768 2769 // Convenience functions for probing the underlying BlockDecl. 2770 SourceLocation getCaretLocation() const; 2771 const Stmt *getBody() const; 2772 Stmt *getBody(); 2773 2774 virtual SourceRange getSourceRange() const { 2775 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2776 } 2777 2778 /// getFunctionType - Return the underlying function type for this block. 2779 const FunctionType *getFunctionType() const; 2780 2781 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2782 /// inside of the block that reference values outside the block. 2783 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2784 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 2785 2786 static bool classof(const Stmt *T) { 2787 return T->getStmtClass() == BlockExprClass; 2788 } 2789 static bool classof(const BlockExpr *) { return true; } 2790 2791 // Iterators 2792 virtual child_iterator child_begin(); 2793 virtual child_iterator child_end(); 2794}; 2795 2796/// BlockDeclRefExpr - A reference to a declared variable, function, 2797/// enum, etc. 2798class BlockDeclRefExpr : public Expr { 2799 ValueDecl *D; 2800 SourceLocation Loc; 2801 bool IsByRef : 1; 2802 bool ConstQualAdded : 1; 2803public: 2804 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 2805 bool constAdded = false) : 2806 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef), 2807 ConstQualAdded(constAdded) {} 2808 2809 // \brief Build an empty reference to a declared variable in a 2810 // block. 2811 explicit BlockDeclRefExpr(EmptyShell Empty) 2812 : Expr(BlockDeclRefExprClass, Empty) { } 2813 2814 ValueDecl *getDecl() { return D; } 2815 const ValueDecl *getDecl() const { return D; } 2816 void setDecl(ValueDecl *VD) { D = VD; } 2817 2818 SourceLocation getLocation() const { return Loc; } 2819 void setLocation(SourceLocation L) { Loc = L; } 2820 2821 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2822 2823 bool isByRef() const { return IsByRef; } 2824 void setByRef(bool BR) { IsByRef = BR; } 2825 2826 bool isConstQualAdded() const { return ConstQualAdded; } 2827 void setConstQualAdded(bool C) { ConstQualAdded = C; } 2828 2829 static bool classof(const Stmt *T) { 2830 return T->getStmtClass() == BlockDeclRefExprClass; 2831 } 2832 static bool classof(const BlockDeclRefExpr *) { return true; } 2833 2834 // Iterators 2835 virtual child_iterator child_begin(); 2836 virtual child_iterator child_end(); 2837}; 2838 2839} // end namespace clang 2840 2841#endif 2842