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