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