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