Expr.h revision 200121569dc6cff10a1fb6ed7500098770b9dd25
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 "clang/AST/DeclAccessPair.h" 21#include "clang/AST/OperationKinds.h" 22#include "clang/AST/ASTVector.h" 23#include "clang/AST/UsuallyTinyPtrVector.h" 24#include "llvm/ADT/APSInt.h" 25#include "llvm/ADT/APFloat.h" 26#include "llvm/ADT/SmallVector.h" 27#include "llvm/ADT/StringRef.h" 28#include <vector> 29 30namespace clang { 31 class ASTContext; 32 class APValue; 33 class Decl; 34 class IdentifierInfo; 35 class ParmVarDecl; 36 class NamedDecl; 37 class ValueDecl; 38 class BlockDecl; 39 class CXXBaseSpecifier; 40 class CXXOperatorCallExpr; 41 class CXXMemberCallExpr; 42 class TemplateArgumentLoc; 43 class TemplateArgumentListInfo; 44 45/// \brief A simple array of base specifiers. 46typedef llvm::SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; 47 48/// Expr - This represents one expression. Note that Expr's are subclasses of 49/// Stmt. This allows an expression to be transparently used any place a Stmt 50/// is required. 51/// 52class Expr : public Stmt { 53 QualType TR; 54 55 virtual void ANCHOR(); // key function. 56protected: 57 /// TypeDependent - Whether this expression is type-dependent 58 /// (C++ [temp.dep.expr]). 59 bool TypeDependent : 1; 60 61 /// ValueDependent - Whether this expression is value-dependent 62 /// (C++ [temp.dep.constexpr]). 63 bool ValueDependent : 1; 64 65 /// ValueKind - The value classification of this expression. 66 /// Only actually used by certain subclasses. 67 unsigned ValueKind : 2; 68 69 enum { BitsRemaining = 28 }; 70 71 Expr(StmtClass SC, QualType T, bool TD, bool VD) 72 : Stmt(SC), TypeDependent(TD), ValueDependent(VD), ValueKind(0) { 73 setType(T); 74 } 75 76 /// \brief Construct an empty expression. 77 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { } 78 79public: 80 /// \brief Increases the reference count for this expression. 81 /// 82 /// Invoke the Retain() operation when this expression 83 /// is being shared by another owner. 84 Expr *Retain() { 85 Stmt::Retain(); 86 return this; 87 } 88 89 QualType getType() const { return TR; } 90 void setType(QualType t) { 91 // In C++, the type of an expression is always adjusted so that it 92 // will not have reference type an expression will never have 93 // reference type (C++ [expr]p6). Use 94 // QualType::getNonReferenceType() to retrieve the non-reference 95 // type. Additionally, inspect Expr::isLvalue to determine whether 96 // an expression that is adjusted in this manner should be 97 // considered an lvalue. 98 assert((t.isNull() || !t->isReferenceType()) && 99 "Expressions can't have reference type"); 100 101 TR = t; 102 } 103 104 /// isValueDependent - Determines whether this expression is 105 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 106 /// array bound of "Chars" in the following example is 107 /// value-dependent. 108 /// @code 109 /// template<int Size, char (&Chars)[Size]> struct meta_string; 110 /// @endcode 111 bool isValueDependent() const { return ValueDependent; } 112 113 /// \brief Set whether this expression is value-dependent or not. 114 void setValueDependent(bool VD) { ValueDependent = VD; } 115 116 /// isTypeDependent - Determines whether this expression is 117 /// type-dependent (C++ [temp.dep.expr]), which means that its type 118 /// could change from one template instantiation to the next. For 119 /// example, the expressions "x" and "x + y" are type-dependent in 120 /// the following code, but "y" is not type-dependent: 121 /// @code 122 /// template<typename T> 123 /// void add(T x, int y) { 124 /// x + y; 125 /// } 126 /// @endcode 127 bool isTypeDependent() const { return TypeDependent; } 128 129 /// \brief Set whether this expression is type-dependent or not. 130 void setTypeDependent(bool TD) { TypeDependent = TD; } 131 132 /// SourceLocation tokens are not useful in isolation - they are low level 133 /// value objects created/interpreted by SourceManager. We assume AST 134 /// clients will have a pointer to the respective SourceManager. 135 virtual SourceRange getSourceRange() const = 0; 136 137 /// getExprLoc - Return the preferred location for the arrow when diagnosing 138 /// a problem with a generic expression. 139 virtual SourceLocation getExprLoc() const { return getLocStart(); } 140 141 /// isUnusedResultAWarning - Return true if this immediate expression should 142 /// be warned about if the result is unused. If so, fill in Loc and Ranges 143 /// with location to warn on and the source range[s] to report with the 144 /// warning. 145 bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, 146 SourceRange &R2, ASTContext &Ctx) const; 147 148 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 149 /// incomplete type other than void. Nonarray expressions that can be lvalues: 150 /// - name, where name must be a variable 151 /// - e[i] 152 /// - (e), where e must be an lvalue 153 /// - e.name, where e must be an lvalue 154 /// - e->name 155 /// - *e, the type of e cannot be a function type 156 /// - string-constant 157 /// - reference type [C++ [expr]] 158 /// - b ? x : y, where x and y are lvalues of suitable types [C++] 159 /// 160 enum isLvalueResult { 161 LV_Valid, 162 LV_NotObjectType, 163 LV_IncompleteVoidType, 164 LV_DuplicateVectorComponents, 165 LV_InvalidExpression, 166 LV_MemberFunction, 167 LV_SubObjCPropertySetting, 168 LV_ClassTemporary 169 }; 170 isLvalueResult isLvalue(ASTContext &Ctx) const; 171 172 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 173 /// does not have an incomplete type, does not have a const-qualified type, 174 /// and if it is a structure or union, does not have any member (including, 175 /// recursively, any member or element of all contained aggregates or unions) 176 /// with a const-qualified type. 177 /// 178 /// \param Loc [in] [out] - A source location which *may* be filled 179 /// in with the location of the expression making this a 180 /// non-modifiable lvalue, if specified. 181 enum isModifiableLvalueResult { 182 MLV_Valid, 183 MLV_NotObjectType, 184 MLV_IncompleteVoidType, 185 MLV_DuplicateVectorComponents, 186 MLV_InvalidExpression, 187 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 188 MLV_IncompleteType, 189 MLV_ConstQualified, 190 MLV_ArrayType, 191 MLV_NotBlockQualified, 192 MLV_ReadonlyProperty, 193 MLV_NoSetterProperty, 194 MLV_MemberFunction, 195 MLV_SubObjCPropertySetting, 196 MLV_ClassTemporary 197 }; 198 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, 199 SourceLocation *Loc = 0) const; 200 201 /// \brief The return type of classify(). Represents the C++0x expression 202 /// taxonomy. 203 class Classification { 204 public: 205 /// \brief The various classification results. Most of these mean prvalue. 206 enum Kinds { 207 CL_LValue, 208 CL_XValue, 209 CL_Function, // Functions cannot be lvalues in C. 210 CL_Void, // Void cannot be an lvalue in C. 211 CL_DuplicateVectorComponents, // A vector shuffle with dupes. 212 CL_MemberFunction, // An expression referring to a member function 213 CL_SubObjCPropertySetting, 214 CL_ClassTemporary, // A prvalue of class type 215 CL_PRValue // A prvalue for any other reason, of any other type 216 }; 217 /// \brief The results of modification testing. 218 enum ModifiableType { 219 CM_Untested, // testModifiable was false. 220 CM_Modifiable, 221 CM_RValue, // Not modifiable because it's an rvalue 222 CM_Function, // Not modifiable because it's a function; C++ only 223 CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext 224 CM_NotBlockQualified, // Not captured in the closure 225 CM_NoSetterProperty,// Implicit assignment to ObjC property without setter 226 CM_ConstQualified, 227 CM_ArrayType, 228 CM_IncompleteType 229 }; 230 231 private: 232 friend class Expr; 233 234 unsigned short Kind; 235 unsigned short Modifiable; 236 237 explicit Classification(Kinds k, ModifiableType m) 238 : Kind(k), Modifiable(m) 239 {} 240 241 public: 242 Classification() {} 243 244 Kinds getKind() const { return static_cast<Kinds>(Kind); } 245 ModifiableType getModifiable() const { 246 assert(Modifiable != CM_Untested && "Did not test for modifiability."); 247 return static_cast<ModifiableType>(Modifiable); 248 } 249 bool isLValue() const { return Kind == CL_LValue; } 250 bool isXValue() const { return Kind == CL_XValue; } 251 bool isGLValue() const { return Kind <= CL_XValue; } 252 bool isPRValue() const { return Kind >= CL_Function; } 253 bool isRValue() const { return Kind >= CL_XValue; } 254 bool isModifiable() const { return getModifiable() == CM_Modifiable; } 255 }; 256 /// \brief classify - Classify this expression according to the C++0x 257 /// expression taxonomy. 258 /// 259 /// C++0x defines ([basic.lval]) a new taxonomy of expressions to replace the 260 /// old lvalue vs rvalue. This function determines the type of expression this 261 /// is. There are three expression types: 262 /// - lvalues are classical lvalues as in C++03. 263 /// - prvalues are equivalent to rvalues in C++03. 264 /// - xvalues are expressions yielding unnamed rvalue references, e.g. a 265 /// function returning an rvalue reference. 266 /// lvalues and xvalues are collectively referred to as glvalues, while 267 /// prvalues and xvalues together form rvalues. 268 Classification Classify(ASTContext &Ctx) const { 269 return ClassifyImpl(Ctx, 0); 270 } 271 272 /// \brief classifyModifiable - Classify this expression according to the 273 /// C++0x expression taxonomy, and see if it is valid on the left side 274 /// of an assignment. 275 /// 276 /// This function extends classify in that it also tests whether the 277 /// expression is modifiable (C99 6.3.2.1p1). 278 /// \param Loc A source location that might be filled with a relevant location 279 /// if the expression is not modifiable. 280 Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{ 281 return ClassifyImpl(Ctx, &Loc); 282 } 283 284private: 285 Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const; 286 287public: 288 289 /// \brief If this expression refers to a bit-field, retrieve the 290 /// declaration of that bit-field. 291 FieldDecl *getBitField(); 292 293 const FieldDecl *getBitField() const { 294 return const_cast<Expr*>(this)->getBitField(); 295 } 296 297 /// \brief Returns whether this expression refers to a vector element. 298 bool refersToVectorElement() const; 299 300 /// isKnownToHaveBooleanValue - Return true if this is an integer expression 301 /// that is known to return 0 or 1. This happens for _Bool/bool expressions 302 /// but also int expressions which are produced by things like comparisons in 303 /// C. 304 bool isKnownToHaveBooleanValue() const; 305 306 /// isIntegerConstantExpr - Return true if this expression is a valid integer 307 /// constant expression, and, if so, return its value in Result. If not a 308 /// valid i-c-e, return false and fill in Loc (if specified) with the location 309 /// of the invalid expression. 310 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 311 SourceLocation *Loc = 0, 312 bool isEvaluated = true) const; 313 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 314 llvm::APSInt X; 315 return isIntegerConstantExpr(X, Ctx, Loc); 316 } 317 /// isConstantInitializer - Returns true if this expression is a constant 318 /// initializer, which can be emitted at compile-time. 319 bool isConstantInitializer(ASTContext &Ctx, bool ForRef) const; 320 321 /// EvalResult is a struct with detailed info about an evaluated expression. 322 struct EvalResult { 323 /// Val - This is the value the expression can be folded to. 324 APValue Val; 325 326 /// HasSideEffects - Whether the evaluated expression has side effects. 327 /// For example, (f() && 0) can be folded, but it still has side effects. 328 bool HasSideEffects; 329 330 /// Diag - If the expression is unfoldable, then Diag contains a note 331 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 332 /// position for the error, and DiagExpr is the expression that caused 333 /// the error. 334 /// If the expression is foldable, but not an integer constant expression, 335 /// Diag contains a note diagnostic that describes why it isn't an integer 336 /// constant expression. If the expression *is* an integer constant 337 /// expression, then Diag will be zero. 338 unsigned Diag; 339 const Expr *DiagExpr; 340 SourceLocation DiagLoc; 341 342 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 343 344 // isGlobalLValue - Return true if the evaluated lvalue expression 345 // is global. 346 bool isGlobalLValue() const; 347 // hasSideEffects - Return true if the evaluated expression has 348 // side effects. 349 bool hasSideEffects() const { 350 return HasSideEffects; 351 } 352 }; 353 354 /// Evaluate - Return true if this is a constant which we can fold using 355 /// any crazy technique (that has nothing to do with language standards) that 356 /// we want to. If this function returns true, it returns the folded constant 357 /// in Result. 358 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const; 359 360 /// EvaluateAsBooleanCondition - Return true if this is a constant 361 /// which we we can fold and convert to a boolean condition using 362 /// any crazy technique that we want to. 363 bool EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const; 364 365 /// isEvaluatable - Call Evaluate to see if this expression can be constant 366 /// folded, but discard the result. 367 bool isEvaluatable(ASTContext &Ctx) const; 368 369 /// HasSideEffects - This routine returns true for all those expressions 370 /// which must be evaluated each time and must not be optimization away 371 /// or evaluated at compile time. Example is a function call, volatile 372 /// variable read. 373 bool HasSideEffects(ASTContext &Ctx) const; 374 375 /// EvaluateAsInt - Call Evaluate and return the folded integer. This 376 /// must be called on an expression that constant folds to an integer. 377 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const; 378 379 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue 380 /// with link time known address. 381 bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const; 382 383 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue. 384 bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const; 385 386 /// \brief Enumeration used to describe how \c isNullPointerConstant() 387 /// should cope with value-dependent expressions. 388 enum NullPointerConstantValueDependence { 389 /// \brief Specifies that the expression should never be value-dependent. 390 NPC_NeverValueDependent = 0, 391 392 /// \brief Specifies that a value-dependent expression of integral or 393 /// dependent type should be considered a null pointer constant. 394 NPC_ValueDependentIsNull, 395 396 /// \brief Specifies that a value-dependent expression should be considered 397 /// to never be a null pointer constant. 398 NPC_ValueDependentIsNotNull 399 }; 400 401 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 402 /// integer constant expression with the value zero, or if this is one that is 403 /// cast to void*. 404 bool isNullPointerConstant(ASTContext &Ctx, 405 NullPointerConstantValueDependence NPC) const; 406 407 /// isOBJCGCCandidate - Return true if this expression may be used in a read/ 408 /// write barrier. 409 bool isOBJCGCCandidate(ASTContext &Ctx) const; 410 411 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 412 /// its subexpression. If that subexpression is also a ParenExpr, 413 /// then this method recursively returns its subexpression, and so forth. 414 /// Otherwise, the method returns the current Expr. 415 Expr *IgnoreParens(); 416 417 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 418 /// or CastExprs, returning their operand. 419 Expr *IgnoreParenCasts(); 420 421 /// IgnoreParenImpCasts - Ignore parentheses and implicit casts. Strip off any 422 /// ParenExpr or ImplicitCastExprs, returning their operand. 423 Expr *IgnoreParenImpCasts(); 424 425 const Expr *IgnoreParenImpCasts() const { 426 return const_cast<Expr*>(this)->IgnoreParenImpCasts(); 427 } 428 429 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the 430 /// value (including ptr->int casts of the same size). Strip off any 431 /// ParenExpr or CastExprs, returning their operand. 432 Expr *IgnoreParenNoopCasts(ASTContext &Ctx); 433 434 /// \brief Determine whether this expression is a default function argument. 435 /// 436 /// Default arguments are implicitly generated in the abstract syntax tree 437 /// by semantic analysis for function calls, object constructions, etc. in 438 /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; 439 /// this routine also looks through any implicit casts to determine whether 440 /// the expression is a default argument. 441 bool isDefaultArgument() const; 442 443 /// \brief Determine whether this expression directly creates a 444 /// temporary object (of class type). 445 bool isTemporaryObject() const { return getTemporaryObject() != 0; } 446 447 /// \brief If this expression directly creates a temporary object of 448 /// class type, return the expression that actually constructs that 449 /// temporary object. 450 const Expr *getTemporaryObject() const; 451 452 const Expr *IgnoreParens() const { 453 return const_cast<Expr*>(this)->IgnoreParens(); 454 } 455 const Expr *IgnoreParenCasts() const { 456 return const_cast<Expr*>(this)->IgnoreParenCasts(); 457 } 458 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const { 459 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx); 460 } 461 462 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 463 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 464 465 static bool classof(const Stmt *T) { 466 return T->getStmtClass() >= firstExprConstant && 467 T->getStmtClass() <= lastExprConstant; 468 } 469 static bool classof(const Expr *) { return true; } 470}; 471 472 473//===----------------------------------------------------------------------===// 474// Primary Expressions. 475//===----------------------------------------------------------------------===// 476 477/// \brief Represents the qualifier that may precede a C++ name, e.g., the 478/// "std::" in "std::sort". 479struct NameQualifier { 480 /// \brief The nested name specifier. 481 NestedNameSpecifier *NNS; 482 483 /// \brief The source range covered by the nested name specifier. 484 SourceRange Range; 485}; 486 487/// \brief Represents an explicit template argument list in C++, e.g., 488/// the "<int>" in "sort<int>". 489struct ExplicitTemplateArgumentList { 490 /// \brief The source location of the left angle bracket ('<'); 491 SourceLocation LAngleLoc; 492 493 /// \brief The source location of the right angle bracket ('>'); 494 SourceLocation RAngleLoc; 495 496 /// \brief The number of template arguments in TemplateArgs. 497 /// The actual template arguments (if any) are stored after the 498 /// ExplicitTemplateArgumentList structure. 499 unsigned NumTemplateArgs; 500 501 /// \brief Retrieve the template arguments 502 TemplateArgumentLoc *getTemplateArgs() { 503 return reinterpret_cast<TemplateArgumentLoc *> (this + 1); 504 } 505 506 /// \brief Retrieve the template arguments 507 const TemplateArgumentLoc *getTemplateArgs() const { 508 return reinterpret_cast<const TemplateArgumentLoc *> (this + 1); 509 } 510 511 void initializeFrom(const TemplateArgumentListInfo &List); 512 void copyInto(TemplateArgumentListInfo &List) const; 513 static std::size_t sizeFor(unsigned NumTemplateArgs); 514 static std::size_t sizeFor(const TemplateArgumentListInfo &List); 515}; 516 517/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 518/// enum, etc. 519class DeclRefExpr : public Expr { 520 enum { 521 // Flag on DecoratedD that specifies when this declaration reference 522 // expression has a C++ nested-name-specifier. 523 HasQualifierFlag = 0x01, 524 // Flag on DecoratedD that specifies when this declaration reference 525 // expression has an explicit C++ template argument list. 526 HasExplicitTemplateArgumentListFlag = 0x02 527 }; 528 529 // DecoratedD - The declaration that we are referencing, plus two bits to 530 // indicate whether (1) the declaration's name was explicitly qualified and 531 // (2) the declaration's name was followed by an explicit template 532 // argument list. 533 llvm::PointerIntPair<ValueDecl *, 2> DecoratedD; 534 535 // Loc - The location of the declaration name itself. 536 SourceLocation Loc; 537 538 /// DNLoc - Provides source/type location info for the 539 /// declaration name embedded in DecoratedD. 540 DeclarationNameLoc DNLoc; 541 542 /// \brief Retrieve the qualifier that preceded the declaration name, if any. 543 NameQualifier *getNameQualifier() { 544 if ((DecoratedD.getInt() & HasQualifierFlag) == 0) 545 return 0; 546 547 return reinterpret_cast<NameQualifier *> (this + 1); 548 } 549 550 /// \brief Retrieve the qualifier that preceded the member name, if any. 551 const NameQualifier *getNameQualifier() const { 552 return const_cast<DeclRefExpr *>(this)->getNameQualifier(); 553 } 554 555 DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 556 ValueDecl *D, SourceLocation NameLoc, 557 const TemplateArgumentListInfo *TemplateArgs, 558 QualType T); 559 560 DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 561 ValueDecl *D, const DeclarationNameInfo &NameInfo, 562 const TemplateArgumentListInfo *TemplateArgs, 563 QualType T); 564 565 /// \brief Construct an empty declaration reference expression. 566 explicit DeclRefExpr(EmptyShell Empty) 567 : Expr(DeclRefExprClass, Empty) { } 568 569 /// \brief Computes the type- and value-dependence flags for this 570 /// declaration reference expression. 571 void computeDependence(); 572 573public: 574 DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 575 Expr(DeclRefExprClass, t, false, false), DecoratedD(d, 0), Loc(l) { 576 computeDependence(); 577 } 578 579 static DeclRefExpr *Create(ASTContext &Context, 580 NestedNameSpecifier *Qualifier, 581 SourceRange QualifierRange, 582 ValueDecl *D, 583 SourceLocation NameLoc, 584 QualType T, 585 const TemplateArgumentListInfo *TemplateArgs = 0); 586 587 static DeclRefExpr *Create(ASTContext &Context, 588 NestedNameSpecifier *Qualifier, 589 SourceRange QualifierRange, 590 ValueDecl *D, 591 const DeclarationNameInfo &NameInfo, 592 QualType T, 593 const TemplateArgumentListInfo *TemplateArgs = 0); 594 595 /// \brief Construct an empty declaration reference expression. 596 static DeclRefExpr *CreateEmpty(ASTContext &Context, 597 bool HasQualifier, unsigned NumTemplateArgs); 598 599 ValueDecl *getDecl() { return DecoratedD.getPointer(); } 600 const ValueDecl *getDecl() const { return DecoratedD.getPointer(); } 601 void setDecl(ValueDecl *NewD) { DecoratedD.setPointer(NewD); } 602 603 DeclarationNameInfo getNameInfo() const { 604 return DeclarationNameInfo(getDecl()->getDeclName(), Loc, DNLoc); 605 } 606 607 SourceLocation getLocation() const { return Loc; } 608 void setLocation(SourceLocation L) { Loc = L; } 609 virtual SourceRange getSourceRange() const; 610 611 /// \brief Determine whether this declaration reference was preceded by a 612 /// C++ nested-name-specifier, e.g., \c N::foo. 613 bool hasQualifier() const { return DecoratedD.getInt() & HasQualifierFlag; } 614 615 /// \brief If the name was qualified, retrieves the source range of 616 /// the nested-name-specifier that precedes the name. Otherwise, 617 /// returns an empty source range. 618 SourceRange getQualifierRange() const { 619 if (!hasQualifier()) 620 return SourceRange(); 621 622 return getNameQualifier()->Range; 623 } 624 625 /// \brief If the name was qualified, retrieves the nested-name-specifier 626 /// that precedes the name. Otherwise, returns NULL. 627 NestedNameSpecifier *getQualifier() const { 628 if (!hasQualifier()) 629 return 0; 630 631 return getNameQualifier()->NNS; 632 } 633 634 bool hasExplicitTemplateArgs() const { 635 return (DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag); 636 } 637 638 /// \brief Retrieve the explicit template argument list that followed the 639 /// member template name. 640 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 641 assert(hasExplicitTemplateArgs()); 642 643 if ((DecoratedD.getInt() & HasQualifierFlag) == 0) 644 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 645 646 return *reinterpret_cast<ExplicitTemplateArgumentList *>( 647 getNameQualifier() + 1); 648 } 649 650 /// \brief Retrieve the explicit template argument list that followed the 651 /// member template name. 652 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 653 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgs(); 654 } 655 656 /// \brief Retrieves the optional explicit template arguments. 657 /// This points to the same data as getExplicitTemplateArgs(), but 658 /// returns null if there are no explicit template arguments. 659 const ExplicitTemplateArgumentList *getExplicitTemplateArgsOpt() const { 660 if (!hasExplicitTemplateArgs()) return 0; 661 return &getExplicitTemplateArgs(); 662 } 663 664 /// \brief Copies the template arguments (if present) into the given 665 /// structure. 666 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 667 if (hasExplicitTemplateArgs()) 668 getExplicitTemplateArgs().copyInto(List); 669 } 670 671 /// \brief Retrieve the location of the left angle bracket following the 672 /// member name ('<'), if any. 673 SourceLocation getLAngleLoc() const { 674 if (!hasExplicitTemplateArgs()) 675 return SourceLocation(); 676 677 return getExplicitTemplateArgs().LAngleLoc; 678 } 679 680 /// \brief Retrieve the template arguments provided as part of this 681 /// template-id. 682 const TemplateArgumentLoc *getTemplateArgs() const { 683 if (!hasExplicitTemplateArgs()) 684 return 0; 685 686 return getExplicitTemplateArgs().getTemplateArgs(); 687 } 688 689 /// \brief Retrieve the number of template arguments provided as part of this 690 /// template-id. 691 unsigned getNumTemplateArgs() const { 692 if (!hasExplicitTemplateArgs()) 693 return 0; 694 695 return getExplicitTemplateArgs().NumTemplateArgs; 696 } 697 698 /// \brief Retrieve the location of the right angle bracket following the 699 /// template arguments ('>'). 700 SourceLocation getRAngleLoc() const { 701 if (!hasExplicitTemplateArgs()) 702 return SourceLocation(); 703 704 return getExplicitTemplateArgs().RAngleLoc; 705 } 706 707 static bool classof(const Stmt *T) { 708 return T->getStmtClass() == DeclRefExprClass; 709 } 710 static bool classof(const DeclRefExpr *) { return true; } 711 712 // Iterators 713 virtual child_iterator child_begin(); 714 virtual child_iterator child_end(); 715 716 friend class ASTStmtReader; 717 friend class ASTStmtWriter; 718}; 719 720/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 721class PredefinedExpr : public Expr { 722public: 723 enum IdentType { 724 Func, 725 Function, 726 PrettyFunction, 727 /// PrettyFunctionNoVirtual - The same as PrettyFunction, except that the 728 /// 'virtual' keyword is omitted for virtual member functions. 729 PrettyFunctionNoVirtual 730 }; 731 732private: 733 SourceLocation Loc; 734 IdentType Type; 735public: 736 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 737 : Expr(PredefinedExprClass, type, type->isDependentType(), 738 type->isDependentType()), Loc(l), Type(IT) {} 739 740 /// \brief Construct an empty predefined expression. 741 explicit PredefinedExpr(EmptyShell Empty) 742 : Expr(PredefinedExprClass, Empty) { } 743 744 IdentType getIdentType() const { return Type; } 745 void setIdentType(IdentType IT) { Type = IT; } 746 747 SourceLocation getLocation() const { return Loc; } 748 void setLocation(SourceLocation L) { Loc = L; } 749 750 static std::string ComputeName(IdentType IT, const Decl *CurrentDecl); 751 752 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 753 754 static bool classof(const Stmt *T) { 755 return T->getStmtClass() == PredefinedExprClass; 756 } 757 static bool classof(const PredefinedExpr *) { return true; } 758 759 // Iterators 760 virtual child_iterator child_begin(); 761 virtual child_iterator child_end(); 762}; 763 764/// \brief Used by IntegerLiteral/FloatingLiteral to store the numeric without 765/// leaking memory. 766/// 767/// For large floats/integers, APFloat/APInt will allocate memory from the heap 768/// to represent these numbers. Unfortunately, when we use a BumpPtrAllocator 769/// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with 770/// the APFloat/APInt values will never get freed. APNumericStorage uses 771/// ASTContext's allocator for memory allocation. 772class APNumericStorage { 773 unsigned BitWidth; 774 union { 775 uint64_t VAL; ///< Used to store the <= 64 bits integer value. 776 uint64_t *pVal; ///< Used to store the >64 bits integer value. 777 }; 778 779 bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; } 780 781 APNumericStorage(const APNumericStorage&); // do not implement 782 APNumericStorage& operator=(const APNumericStorage&); // do not implement 783 784protected: 785 APNumericStorage() : BitWidth(0), VAL(0) { } 786 787 llvm::APInt getIntValue() const { 788 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 789 if (NumWords > 1) 790 return llvm::APInt(BitWidth, NumWords, pVal); 791 else 792 return llvm::APInt(BitWidth, VAL); 793 } 794 void setIntValue(ASTContext &C, const llvm::APInt &Val); 795}; 796 797class APIntStorage : public APNumericStorage { 798public: 799 llvm::APInt getValue() const { return getIntValue(); } 800 void setValue(ASTContext &C, const llvm::APInt &Val) { setIntValue(C, Val); } 801}; 802 803class APFloatStorage : public APNumericStorage { 804public: 805 llvm::APFloat getValue() const { return llvm::APFloat(getIntValue()); } 806 void setValue(ASTContext &C, const llvm::APFloat &Val) { 807 setIntValue(C, Val.bitcastToAPInt()); 808 } 809}; 810 811class IntegerLiteral : public Expr { 812 APIntStorage Num; 813 SourceLocation Loc; 814 815 /// \brief Construct an empty integer literal. 816 explicit IntegerLiteral(EmptyShell Empty) 817 : Expr(IntegerLiteralClass, Empty) { } 818 819public: 820 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 821 // or UnsignedLongLongTy 822 IntegerLiteral(ASTContext &C, const llvm::APInt &V, 823 QualType type, SourceLocation l) 824 : Expr(IntegerLiteralClass, type, false, false), Loc(l) { 825 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 826 setValue(C, V); 827 } 828 829 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 830 // or UnsignedLongLongTy 831 static IntegerLiteral *Create(ASTContext &C, const llvm::APInt &V, 832 QualType type, SourceLocation l); 833 static IntegerLiteral *Create(ASTContext &C, EmptyShell Empty); 834 835 llvm::APInt getValue() const { return Num.getValue(); } 836 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 837 838 /// \brief Retrieve the location of the literal. 839 SourceLocation getLocation() const { return Loc; } 840 841 void setValue(ASTContext &C, const llvm::APInt &Val) { Num.setValue(C, Val); } 842 void setLocation(SourceLocation Location) { Loc = Location; } 843 844 static bool classof(const Stmt *T) { 845 return T->getStmtClass() == IntegerLiteralClass; 846 } 847 static bool classof(const IntegerLiteral *) { return true; } 848 849 // Iterators 850 virtual child_iterator child_begin(); 851 virtual child_iterator child_end(); 852}; 853 854class CharacterLiteral : public Expr { 855 unsigned Value; 856 SourceLocation Loc; 857 bool IsWide; 858public: 859 // type should be IntTy 860 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 861 : Expr(CharacterLiteralClass, type, false, false), Value(value), Loc(l), 862 IsWide(iswide) { 863 } 864 865 /// \brief Construct an empty character literal. 866 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 867 868 SourceLocation getLocation() const { return Loc; } 869 bool isWide() const { return IsWide; } 870 871 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 872 873 unsigned getValue() const { return Value; } 874 875 void setLocation(SourceLocation Location) { Loc = Location; } 876 void setWide(bool W) { IsWide = W; } 877 void setValue(unsigned Val) { Value = Val; } 878 879 static bool classof(const Stmt *T) { 880 return T->getStmtClass() == CharacterLiteralClass; 881 } 882 static bool classof(const CharacterLiteral *) { return true; } 883 884 // Iterators 885 virtual child_iterator child_begin(); 886 virtual child_iterator child_end(); 887}; 888 889class FloatingLiteral : public Expr { 890 APFloatStorage Num; 891 bool IsExact : 1; 892 SourceLocation Loc; 893 894 FloatingLiteral(ASTContext &C, const llvm::APFloat &V, bool isexact, 895 QualType Type, SourceLocation L) 896 : Expr(FloatingLiteralClass, Type, false, false), 897 IsExact(isexact), Loc(L) { 898 setValue(C, V); 899 } 900 901 /// \brief Construct an empty floating-point literal. 902 explicit FloatingLiteral(EmptyShell Empty) 903 : Expr(FloatingLiteralClass, Empty), IsExact(false) { } 904 905public: 906 static FloatingLiteral *Create(ASTContext &C, const llvm::APFloat &V, 907 bool isexact, QualType Type, SourceLocation L); 908 static FloatingLiteral *Create(ASTContext &C, EmptyShell Empty); 909 910 llvm::APFloat getValue() const { return Num.getValue(); } 911 void setValue(ASTContext &C, const llvm::APFloat &Val) { 912 Num.setValue(C, Val); 913 } 914 915 bool isExact() const { return IsExact; } 916 void setExact(bool E) { IsExact = E; } 917 918 /// getValueAsApproximateDouble - This returns the value as an inaccurate 919 /// double. Note that this may cause loss of precision, but is useful for 920 /// debugging dumps, etc. 921 double getValueAsApproximateDouble() const; 922 923 SourceLocation getLocation() const { return Loc; } 924 void setLocation(SourceLocation L) { Loc = L; } 925 926 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 927 928 static bool classof(const Stmt *T) { 929 return T->getStmtClass() == FloatingLiteralClass; 930 } 931 static bool classof(const FloatingLiteral *) { return true; } 932 933 // Iterators 934 virtual child_iterator child_begin(); 935 virtual child_iterator child_end(); 936}; 937 938/// ImaginaryLiteral - We support imaginary integer and floating point literals, 939/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 940/// IntegerLiteral classes. Instances of this class always have a Complex type 941/// whose element type matches the subexpression. 942/// 943class ImaginaryLiteral : public Expr { 944 Stmt *Val; 945public: 946 ImaginaryLiteral(Expr *val, QualType Ty) 947 : Expr(ImaginaryLiteralClass, Ty, false, false), Val(val) {} 948 949 /// \brief Build an empty imaginary literal. 950 explicit ImaginaryLiteral(EmptyShell Empty) 951 : Expr(ImaginaryLiteralClass, Empty) { } 952 953 const Expr *getSubExpr() const { return cast<Expr>(Val); } 954 Expr *getSubExpr() { return cast<Expr>(Val); } 955 void setSubExpr(Expr *E) { Val = E; } 956 957 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 958 static bool classof(const Stmt *T) { 959 return T->getStmtClass() == ImaginaryLiteralClass; 960 } 961 static bool classof(const ImaginaryLiteral *) { return true; } 962 963 // Iterators 964 virtual child_iterator child_begin(); 965 virtual child_iterator child_end(); 966}; 967 968/// StringLiteral - This represents a string literal expression, e.g. "foo" 969/// or L"bar" (wide strings). The actual string is returned by getStrData() 970/// is NOT null-terminated, and the length of the string is determined by 971/// calling getByteLength(). The C type for a string is always a 972/// ConstantArrayType. In C++, the char type is const qualified, in C it is 973/// not. 974/// 975/// Note that strings in C can be formed by concatenation of multiple string 976/// literal pptokens in translation phase #6. This keeps track of the locations 977/// of each of these pieces. 978/// 979/// Strings in C can also be truncated and extended by assigning into arrays, 980/// e.g. with constructs like: 981/// char X[2] = "foobar"; 982/// In this case, getByteLength() will return 6, but the string literal will 983/// have type "char[2]". 984class StringLiteral : public Expr { 985 const char *StrData; 986 unsigned ByteLength; 987 bool IsWide; 988 unsigned NumConcatenated; 989 SourceLocation TokLocs[1]; 990 991 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty, false, false) {} 992 993public: 994 /// This is the "fully general" constructor that allows representation of 995 /// strings formed from multiple concatenated tokens. 996 static StringLiteral *Create(ASTContext &C, const char *StrData, 997 unsigned ByteLength, bool Wide, QualType Ty, 998 const SourceLocation *Loc, unsigned NumStrs); 999 1000 /// Simple constructor for string literals made from one token. 1001 static StringLiteral *Create(ASTContext &C, const char *StrData, 1002 unsigned ByteLength, 1003 bool Wide, QualType Ty, SourceLocation Loc) { 1004 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1); 1005 } 1006 1007 /// \brief Construct an empty string literal. 1008 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 1009 1010 llvm::StringRef getString() const { 1011 return llvm::StringRef(StrData, ByteLength); 1012 } 1013 1014 unsigned getByteLength() const { return ByteLength; } 1015 1016 /// \brief Sets the string data to the given string data. 1017 void setString(ASTContext &C, llvm::StringRef Str); 1018 1019 bool isWide() const { return IsWide; } 1020 void setWide(bool W) { IsWide = W; } 1021 1022 bool containsNonAsciiOrNull() const { 1023 llvm::StringRef Str = getString(); 1024 for (unsigned i = 0, e = Str.size(); i != e; ++i) 1025 if (!isascii(Str[i]) || !Str[i]) 1026 return true; 1027 return false; 1028 } 1029 /// getNumConcatenated - Get the number of string literal tokens that were 1030 /// concatenated in translation phase #6 to form this string literal. 1031 unsigned getNumConcatenated() const { return NumConcatenated; } 1032 1033 SourceLocation getStrTokenLoc(unsigned TokNum) const { 1034 assert(TokNum < NumConcatenated && "Invalid tok number"); 1035 return TokLocs[TokNum]; 1036 } 1037 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 1038 assert(TokNum < NumConcatenated && "Invalid tok number"); 1039 TokLocs[TokNum] = L; 1040 } 1041 1042 typedef const SourceLocation *tokloc_iterator; 1043 tokloc_iterator tokloc_begin() const { return TokLocs; } 1044 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 1045 1046 virtual SourceRange getSourceRange() const { 1047 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 1048 } 1049 static bool classof(const Stmt *T) { 1050 return T->getStmtClass() == StringLiteralClass; 1051 } 1052 static bool classof(const StringLiteral *) { return true; } 1053 1054 // Iterators 1055 virtual child_iterator child_begin(); 1056 virtual child_iterator child_end(); 1057}; 1058 1059/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 1060/// AST node is only formed if full location information is requested. 1061class ParenExpr : public Expr { 1062 SourceLocation L, R; 1063 Stmt *Val; 1064public: 1065 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 1066 : Expr(ParenExprClass, val->getType(), 1067 val->isTypeDependent(), val->isValueDependent()), 1068 L(l), R(r), Val(val) {} 1069 1070 /// \brief Construct an empty parenthesized expression. 1071 explicit ParenExpr(EmptyShell Empty) 1072 : Expr(ParenExprClass, Empty) { } 1073 1074 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1075 Expr *getSubExpr() { return cast<Expr>(Val); } 1076 void setSubExpr(Expr *E) { Val = E; } 1077 1078 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 1079 1080 /// \brief Get the location of the left parentheses '('. 1081 SourceLocation getLParen() const { return L; } 1082 void setLParen(SourceLocation Loc) { L = Loc; } 1083 1084 /// \brief Get the location of the right parentheses ')'. 1085 SourceLocation getRParen() const { return R; } 1086 void setRParen(SourceLocation Loc) { R = Loc; } 1087 1088 static bool classof(const Stmt *T) { 1089 return T->getStmtClass() == ParenExprClass; 1090 } 1091 static bool classof(const ParenExpr *) { return true; } 1092 1093 // Iterators 1094 virtual child_iterator child_begin(); 1095 virtual child_iterator child_end(); 1096}; 1097 1098 1099/// UnaryOperator - This represents the unary-expression's (except sizeof and 1100/// alignof), the postinc/postdec operators from postfix-expression, and various 1101/// extensions. 1102/// 1103/// Notes on various nodes: 1104/// 1105/// Real/Imag - These return the real/imag part of a complex operand. If 1106/// applied to a non-complex value, the former returns its operand and the 1107/// later returns zero in the type of the operand. 1108/// 1109class UnaryOperator : public Expr { 1110public: 1111 typedef UnaryOperatorKind Opcode; 1112 1113private: 1114 unsigned Opc : 5; 1115 SourceLocation Loc; 1116 Stmt *Val; 1117public: 1118 1119 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 1120 : Expr(UnaryOperatorClass, type, 1121 input->isTypeDependent() || type->isDependentType(), 1122 input->isValueDependent()), 1123 Opc(opc), Loc(l), Val(input) {} 1124 1125 /// \brief Build an empty unary operator. 1126 explicit UnaryOperator(EmptyShell Empty) 1127 : Expr(UnaryOperatorClass, Empty), Opc(UO_AddrOf) { } 1128 1129 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 1130 void setOpcode(Opcode O) { Opc = O; } 1131 1132 Expr *getSubExpr() const { return cast<Expr>(Val); } 1133 void setSubExpr(Expr *E) { Val = E; } 1134 1135 /// getOperatorLoc - Return the location of the operator. 1136 SourceLocation getOperatorLoc() const { return Loc; } 1137 void setOperatorLoc(SourceLocation L) { Loc = L; } 1138 1139 /// isPostfix - Return true if this is a postfix operation, like x++. 1140 static bool isPostfix(Opcode Op) { 1141 return Op == UO_PostInc || Op == UO_PostDec; 1142 } 1143 1144 /// isPostfix - Return true if this is a prefix operation, like --x. 1145 static bool isPrefix(Opcode Op) { 1146 return Op == UO_PreInc || Op == UO_PreDec; 1147 } 1148 1149 bool isPrefix() const { return isPrefix(getOpcode()); } 1150 bool isPostfix() const { return isPostfix(getOpcode()); } 1151 bool isIncrementOp() const { 1152 return Opc == UO_PreInc || Opc == UO_PostInc; 1153 } 1154 bool isIncrementDecrementOp() const { 1155 return Opc <= UO_PreDec; 1156 } 1157 static bool isArithmeticOp(Opcode Op) { 1158 return Op >= UO_Plus && Op <= UO_LNot; 1159 } 1160 bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); } 1161 1162 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1163 /// corresponds to, e.g. "sizeof" or "[pre]++" 1164 static const char *getOpcodeStr(Opcode Op); 1165 1166 /// \brief Retrieve the unary opcode that corresponds to the given 1167 /// overloaded operator. 1168 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 1169 1170 /// \brief Retrieve the overloaded operator kind that corresponds to 1171 /// the given unary opcode. 1172 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1173 1174 virtual SourceRange getSourceRange() const { 1175 if (isPostfix()) 1176 return SourceRange(Val->getLocStart(), Loc); 1177 else 1178 return SourceRange(Loc, Val->getLocEnd()); 1179 } 1180 virtual SourceLocation getExprLoc() const { return Loc; } 1181 1182 static bool classof(const Stmt *T) { 1183 return T->getStmtClass() == UnaryOperatorClass; 1184 } 1185 static bool classof(const UnaryOperator *) { return true; } 1186 1187 // Iterators 1188 virtual child_iterator child_begin(); 1189 virtual child_iterator child_end(); 1190}; 1191 1192/// OffsetOfExpr - [C99 7.17] - This represents an expression of the form 1193/// offsetof(record-type, member-designator). For example, given: 1194/// @code 1195/// struct S { 1196/// float f; 1197/// double d; 1198/// }; 1199/// struct T { 1200/// int i; 1201/// struct S s[10]; 1202/// }; 1203/// @endcode 1204/// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). 1205 1206class OffsetOfExpr : public Expr { 1207public: 1208 // __builtin_offsetof(type, identifier(.identifier|[expr])*) 1209 class OffsetOfNode { 1210 public: 1211 /// \brief The kind of offsetof node we have. 1212 enum Kind { 1213 /// \brief An index into an array. 1214 Array = 0x00, 1215 /// \brief A field. 1216 Field = 0x01, 1217 /// \brief A field in a dependent type, known only by its name. 1218 Identifier = 0x02, 1219 /// \brief An implicit indirection through a C++ base class, when the 1220 /// field found is in a base class. 1221 Base = 0x03 1222 }; 1223 1224 private: 1225 enum { MaskBits = 2, Mask = 0x03 }; 1226 1227 /// \brief The source range that covers this part of the designator. 1228 SourceRange Range; 1229 1230 /// \brief The data describing the designator, which comes in three 1231 /// different forms, depending on the lower two bits. 1232 /// - An unsigned index into the array of Expr*'s stored after this node 1233 /// in memory, for [constant-expression] designators. 1234 /// - A FieldDecl*, for references to a known field. 1235 /// - An IdentifierInfo*, for references to a field with a given name 1236 /// when the class type is dependent. 1237 /// - A CXXBaseSpecifier*, for references that look at a field in a 1238 /// base class. 1239 uintptr_t Data; 1240 1241 public: 1242 /// \brief Create an offsetof node that refers to an array element. 1243 OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, 1244 SourceLocation RBracketLoc) 1245 : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { } 1246 1247 /// \brief Create an offsetof node that refers to a field. 1248 OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, 1249 SourceLocation NameLoc) 1250 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1251 Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { } 1252 1253 /// \brief Create an offsetof node that refers to an identifier. 1254 OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, 1255 SourceLocation NameLoc) 1256 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1257 Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { } 1258 1259 /// \brief Create an offsetof node that refers into a C++ base class. 1260 explicit OffsetOfNode(const CXXBaseSpecifier *Base) 1261 : Range(), Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} 1262 1263 /// \brief Determine what kind of offsetof node this is. 1264 Kind getKind() const { 1265 return static_cast<Kind>(Data & Mask); 1266 } 1267 1268 /// \brief For an array element node, returns the index into the array 1269 /// of expressions. 1270 unsigned getArrayExprIndex() const { 1271 assert(getKind() == Array); 1272 return Data >> 2; 1273 } 1274 1275 /// \brief For a field offsetof node, returns the field. 1276 FieldDecl *getField() const { 1277 assert(getKind() == Field); 1278 return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); 1279 } 1280 1281 /// \brief For a field or identifier offsetof node, returns the name of 1282 /// the field. 1283 IdentifierInfo *getFieldName() const; 1284 1285 /// \brief For a base class node, returns the base specifier. 1286 CXXBaseSpecifier *getBase() const { 1287 assert(getKind() == Base); 1288 return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); 1289 } 1290 1291 /// \brief Retrieve the source range that covers this offsetof node. 1292 /// 1293 /// For an array element node, the source range contains the locations of 1294 /// the square brackets. For a field or identifier node, the source range 1295 /// contains the location of the period (if there is one) and the 1296 /// identifier. 1297 SourceRange getRange() const { return Range; } 1298 }; 1299 1300private: 1301 1302 SourceLocation OperatorLoc, RParenLoc; 1303 // Base type; 1304 TypeSourceInfo *TSInfo; 1305 // Number of sub-components (i.e. instances of OffsetOfNode). 1306 unsigned NumComps; 1307 // Number of sub-expressions (i.e. array subscript expressions). 1308 unsigned NumExprs; 1309 1310 OffsetOfExpr(ASTContext &C, QualType type, 1311 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1312 OffsetOfNode* compsPtr, unsigned numComps, 1313 Expr** exprsPtr, unsigned numExprs, 1314 SourceLocation RParenLoc); 1315 1316 explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) 1317 : Expr(OffsetOfExprClass, EmptyShell()), 1318 TSInfo(0), NumComps(numComps), NumExprs(numExprs) {} 1319 1320public: 1321 1322 static OffsetOfExpr *Create(ASTContext &C, QualType type, 1323 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1324 OffsetOfNode* compsPtr, unsigned numComps, 1325 Expr** exprsPtr, unsigned numExprs, 1326 SourceLocation RParenLoc); 1327 1328 static OffsetOfExpr *CreateEmpty(ASTContext &C, 1329 unsigned NumComps, unsigned NumExprs); 1330 1331 /// getOperatorLoc - Return the location of the operator. 1332 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1333 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1334 1335 /// \brief Return the location of the right parentheses. 1336 SourceLocation getRParenLoc() const { return RParenLoc; } 1337 void setRParenLoc(SourceLocation R) { RParenLoc = R; } 1338 1339 TypeSourceInfo *getTypeSourceInfo() const { 1340 return TSInfo; 1341 } 1342 void setTypeSourceInfo(TypeSourceInfo *tsi) { 1343 TSInfo = tsi; 1344 } 1345 1346 const OffsetOfNode &getComponent(unsigned Idx) { 1347 assert(Idx < NumComps && "Subscript out of range"); 1348 return reinterpret_cast<OffsetOfNode *> (this + 1)[Idx]; 1349 } 1350 1351 void setComponent(unsigned Idx, OffsetOfNode ON) { 1352 assert(Idx < NumComps && "Subscript out of range"); 1353 reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON; 1354 } 1355 1356 unsigned getNumComponents() const { 1357 return NumComps; 1358 } 1359 1360 Expr* getIndexExpr(unsigned Idx) { 1361 assert(Idx < NumExprs && "Subscript out of range"); 1362 return reinterpret_cast<Expr **>( 1363 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx]; 1364 } 1365 1366 void setIndexExpr(unsigned Idx, Expr* E) { 1367 assert(Idx < NumComps && "Subscript out of range"); 1368 reinterpret_cast<Expr **>( 1369 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E; 1370 } 1371 1372 unsigned getNumExpressions() const { 1373 return NumExprs; 1374 } 1375 1376 virtual SourceRange getSourceRange() const { 1377 return SourceRange(OperatorLoc, RParenLoc); 1378 } 1379 1380 static bool classof(const Stmt *T) { 1381 return T->getStmtClass() == OffsetOfExprClass; 1382 } 1383 1384 static bool classof(const OffsetOfExpr *) { return true; } 1385 1386 // Iterators 1387 virtual child_iterator child_begin(); 1388 virtual child_iterator child_end(); 1389}; 1390 1391/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 1392/// types and expressions. 1393class SizeOfAlignOfExpr : public Expr { 1394 bool isSizeof : 1; // true if sizeof, false if alignof. 1395 bool isType : 1; // true if operand is a type, false if an expression 1396 union { 1397 TypeSourceInfo *Ty; 1398 Stmt *Ex; 1399 } Argument; 1400 SourceLocation OpLoc, RParenLoc; 1401 1402public: 1403 SizeOfAlignOfExpr(bool issizeof, TypeSourceInfo *TInfo, 1404 QualType resultType, SourceLocation op, 1405 SourceLocation rp) : 1406 Expr(SizeOfAlignOfExprClass, resultType, 1407 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1408 // Value-dependent if the argument is type-dependent. 1409 TInfo->getType()->isDependentType()), 1410 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) { 1411 Argument.Ty = TInfo; 1412 } 1413 1414 SizeOfAlignOfExpr(bool issizeof, Expr *E, 1415 QualType resultType, SourceLocation op, 1416 SourceLocation rp) : 1417 Expr(SizeOfAlignOfExprClass, resultType, 1418 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1419 // Value-dependent if the argument is type-dependent. 1420 E->isTypeDependent()), 1421 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) { 1422 Argument.Ex = E; 1423 } 1424 1425 /// \brief Construct an empty sizeof/alignof expression. 1426 explicit SizeOfAlignOfExpr(EmptyShell Empty) 1427 : Expr(SizeOfAlignOfExprClass, Empty) { } 1428 1429 bool isSizeOf() const { return isSizeof; } 1430 void setSizeof(bool S) { isSizeof = S; } 1431 1432 bool isArgumentType() const { return isType; } 1433 QualType getArgumentType() const { 1434 return getArgumentTypeInfo()->getType(); 1435 } 1436 TypeSourceInfo *getArgumentTypeInfo() const { 1437 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 1438 return Argument.Ty; 1439 } 1440 Expr *getArgumentExpr() { 1441 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 1442 return static_cast<Expr*>(Argument.Ex); 1443 } 1444 const Expr *getArgumentExpr() const { 1445 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 1446 } 1447 1448 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 1449 void setArgument(TypeSourceInfo *TInfo) { 1450 Argument.Ty = TInfo; 1451 isType = true; 1452 } 1453 1454 /// Gets the argument type, or the type of the argument expression, whichever 1455 /// is appropriate. 1456 QualType getTypeOfArgument() const { 1457 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 1458 } 1459 1460 SourceLocation getOperatorLoc() const { return OpLoc; } 1461 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1462 1463 SourceLocation getRParenLoc() const { return RParenLoc; } 1464 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1465 1466 virtual SourceRange getSourceRange() const { 1467 return SourceRange(OpLoc, RParenLoc); 1468 } 1469 1470 static bool classof(const Stmt *T) { 1471 return T->getStmtClass() == SizeOfAlignOfExprClass; 1472 } 1473 static bool classof(const SizeOfAlignOfExpr *) { return true; } 1474 1475 // Iterators 1476 virtual child_iterator child_begin(); 1477 virtual child_iterator child_end(); 1478}; 1479 1480//===----------------------------------------------------------------------===// 1481// Postfix Operators. 1482//===----------------------------------------------------------------------===// 1483 1484/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 1485class ArraySubscriptExpr : public Expr { 1486 enum { LHS, RHS, END_EXPR=2 }; 1487 Stmt* SubExprs[END_EXPR]; 1488 SourceLocation RBracketLoc; 1489public: 1490 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 1491 SourceLocation rbracketloc) 1492 : Expr(ArraySubscriptExprClass, t, 1493 lhs->isTypeDependent() || rhs->isTypeDependent(), 1494 lhs->isValueDependent() || rhs->isValueDependent()), 1495 RBracketLoc(rbracketloc) { 1496 SubExprs[LHS] = lhs; 1497 SubExprs[RHS] = rhs; 1498 } 1499 1500 /// \brief Create an empty array subscript expression. 1501 explicit ArraySubscriptExpr(EmptyShell Shell) 1502 : Expr(ArraySubscriptExprClass, Shell) { } 1503 1504 /// An array access can be written A[4] or 4[A] (both are equivalent). 1505 /// - getBase() and getIdx() always present the normalized view: A[4]. 1506 /// In this case getBase() returns "A" and getIdx() returns "4". 1507 /// - getLHS() and getRHS() present the syntactic view. e.g. for 1508 /// 4[A] getLHS() returns "4". 1509 /// Note: Because vector element access is also written A[4] we must 1510 /// predicate the format conversion in getBase and getIdx only on the 1511 /// the type of the RHS, as it is possible for the LHS to be a vector of 1512 /// integer type 1513 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 1514 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1515 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1516 1517 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 1518 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1519 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1520 1521 Expr *getBase() { 1522 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1523 } 1524 1525 const Expr *getBase() const { 1526 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1527 } 1528 1529 Expr *getIdx() { 1530 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1531 } 1532 1533 const Expr *getIdx() const { 1534 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1535 } 1536 1537 virtual SourceRange getSourceRange() const { 1538 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 1539 } 1540 1541 SourceLocation getRBracketLoc() const { return RBracketLoc; } 1542 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 1543 1544 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 1545 1546 static bool classof(const Stmt *T) { 1547 return T->getStmtClass() == ArraySubscriptExprClass; 1548 } 1549 static bool classof(const ArraySubscriptExpr *) { return true; } 1550 1551 // Iterators 1552 virtual child_iterator child_begin(); 1553 virtual child_iterator child_end(); 1554}; 1555 1556 1557/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 1558/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 1559/// while its subclasses may represent alternative syntax that (semantically) 1560/// results in a function call. For example, CXXOperatorCallExpr is 1561/// a subclass for overloaded operator calls that use operator syntax, e.g., 1562/// "str1 + str2" to resolve to a function call. 1563class CallExpr : public Expr { 1564 enum { FN=0, ARGS_START=1 }; 1565 Stmt **SubExprs; 1566 unsigned NumArgs; 1567 SourceLocation RParenLoc; 1568 1569protected: 1570 // This version of the constructor is for derived classes. 1571 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, 1572 QualType t, SourceLocation rparenloc); 1573 1574public: 1575 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 1576 SourceLocation rparenloc); 1577 1578 /// \brief Build an empty call expression. 1579 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 1580 1581 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 1582 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 1583 void setCallee(Expr *F) { SubExprs[FN] = F; } 1584 1585 Decl *getCalleeDecl(); 1586 const Decl *getCalleeDecl() const { 1587 return const_cast<CallExpr*>(this)->getCalleeDecl(); 1588 } 1589 1590 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 1591 FunctionDecl *getDirectCallee(); 1592 const FunctionDecl *getDirectCallee() const { 1593 return const_cast<CallExpr*>(this)->getDirectCallee(); 1594 } 1595 1596 /// getNumArgs - Return the number of actual arguments to this call. 1597 /// 1598 unsigned getNumArgs() const { return NumArgs; } 1599 1600 /// getArg - Return the specified argument. 1601 Expr *getArg(unsigned Arg) { 1602 assert(Arg < NumArgs && "Arg access out of range!"); 1603 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1604 } 1605 const Expr *getArg(unsigned Arg) const { 1606 assert(Arg < NumArgs && "Arg access out of range!"); 1607 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1608 } 1609 1610 /// setArg - Set the specified argument. 1611 void setArg(unsigned Arg, Expr *ArgExpr) { 1612 assert(Arg < NumArgs && "Arg access out of range!"); 1613 SubExprs[Arg+ARGS_START] = ArgExpr; 1614 } 1615 1616 /// setNumArgs - This changes the number of arguments present in this call. 1617 /// Any orphaned expressions are deleted by this, and any new operands are set 1618 /// to null. 1619 void setNumArgs(ASTContext& C, unsigned NumArgs); 1620 1621 typedef ExprIterator arg_iterator; 1622 typedef ConstExprIterator const_arg_iterator; 1623 1624 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 1625 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 1626 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 1627 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 1628 1629 /// getNumCommas - Return the number of commas that must have been present in 1630 /// this function call. 1631 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1632 1633 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1634 /// not, return 0. 1635 unsigned isBuiltinCall(ASTContext &Context) const; 1636 1637 /// getCallReturnType - Get the return type of the call expr. This is not 1638 /// always the type of the expr itself, if the return type is a reference 1639 /// type. 1640 QualType getCallReturnType() const; 1641 1642 SourceLocation getRParenLoc() const { return RParenLoc; } 1643 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1644 1645 virtual SourceRange getSourceRange() const { 1646 return SourceRange(getCallee()->getLocStart(), RParenLoc); 1647 } 1648 1649 static bool classof(const Stmt *T) { 1650 return T->getStmtClass() >= firstCallExprConstant && 1651 T->getStmtClass() <= lastCallExprConstant; 1652 } 1653 static bool classof(const CallExpr *) { return true; } 1654 1655 // Iterators 1656 virtual child_iterator child_begin(); 1657 virtual child_iterator child_end(); 1658}; 1659 1660/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1661/// 1662class MemberExpr : public Expr { 1663 /// Extra data stored in some member expressions. 1664 struct MemberNameQualifier : public NameQualifier { 1665 DeclAccessPair FoundDecl; 1666 }; 1667 1668 /// Base - the expression for the base pointer or structure references. In 1669 /// X.F, this is "X". 1670 Stmt *Base; 1671 1672 /// MemberDecl - This is the decl being referenced by the field/member name. 1673 /// In X.F, this is the decl referenced by F. 1674 ValueDecl *MemberDecl; 1675 1676 /// MemberLoc - This is the location of the member name. 1677 SourceLocation MemberLoc; 1678 1679 /// MemberDNLoc - Provides source/type location info for the 1680 /// declaration name embedded in MemberDecl. 1681 DeclarationNameLoc MemberDNLoc; 1682 1683 /// IsArrow - True if this is "X->F", false if this is "X.F". 1684 bool IsArrow : 1; 1685 1686 /// \brief True if this member expression used a nested-name-specifier to 1687 /// refer to the member, e.g., "x->Base::f", or found its member via a using 1688 /// declaration. When true, a MemberNameQualifier 1689 /// structure is allocated immediately after the MemberExpr. 1690 bool HasQualifierOrFoundDecl : 1; 1691 1692 /// \brief True if this member expression specified a template argument list 1693 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1694 /// structure (and its TemplateArguments) are allocated immediately after 1695 /// the MemberExpr or, if the member expression also has a qualifier, after 1696 /// the MemberNameQualifier structure. 1697 bool HasExplicitTemplateArgumentList : 1; 1698 1699 /// \brief Retrieve the qualifier that preceded the member name, if any. 1700 MemberNameQualifier *getMemberQualifier() { 1701 assert(HasQualifierOrFoundDecl); 1702 return reinterpret_cast<MemberNameQualifier *> (this + 1); 1703 } 1704 1705 /// \brief Retrieve the qualifier that preceded the member name, if any. 1706 const MemberNameQualifier *getMemberQualifier() const { 1707 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1708 } 1709 1710public: 1711 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1712 const DeclarationNameInfo &NameInfo, QualType ty) 1713 : Expr(MemberExprClass, ty, 1714 base->isTypeDependent(), base->isValueDependent()), 1715 Base(base), MemberDecl(memberdecl), MemberLoc(NameInfo.getLoc()), 1716 MemberDNLoc(NameInfo.getInfo()), IsArrow(isarrow), 1717 HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) { 1718 assert(memberdecl->getDeclName() == NameInfo.getName()); 1719 } 1720 1721 // NOTE: this constructor should be used only when it is known that 1722 // the member name can not provide additional syntactic info 1723 // (i.e., source locations for C++ operator names or type source info 1724 // for constructors, destructors and conversion oeprators). 1725 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1726 SourceLocation l, QualType ty) 1727 : Expr(MemberExprClass, ty, 1728 base->isTypeDependent(), base->isValueDependent()), 1729 Base(base), MemberDecl(memberdecl), MemberLoc(l), MemberDNLoc(), 1730 IsArrow(isarrow), 1731 HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) {} 1732 1733 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 1734 NestedNameSpecifier *qual, SourceRange qualrange, 1735 ValueDecl *memberdecl, DeclAccessPair founddecl, 1736 DeclarationNameInfo MemberNameInfo, 1737 const TemplateArgumentListInfo *targs, 1738 QualType ty); 1739 1740 void setBase(Expr *E) { Base = E; } 1741 Expr *getBase() const { return cast<Expr>(Base); } 1742 1743 /// \brief Retrieve the member declaration to which this expression refers. 1744 /// 1745 /// The returned declaration will either be a FieldDecl or (in C++) 1746 /// a CXXMethodDecl. 1747 ValueDecl *getMemberDecl() const { return MemberDecl; } 1748 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 1749 1750 /// \brief Retrieves the declaration found by lookup. 1751 DeclAccessPair getFoundDecl() const { 1752 if (!HasQualifierOrFoundDecl) 1753 return DeclAccessPair::make(getMemberDecl(), 1754 getMemberDecl()->getAccess()); 1755 return getMemberQualifier()->FoundDecl; 1756 } 1757 1758 /// \brief Determines whether this member expression actually had 1759 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1760 /// x->Base::foo. 1761 bool hasQualifier() const { return getQualifier() != 0; } 1762 1763 /// \brief If the member name was qualified, retrieves the source range of 1764 /// the nested-name-specifier that precedes the member name. Otherwise, 1765 /// returns an empty source range. 1766 SourceRange getQualifierRange() const { 1767 if (!HasQualifierOrFoundDecl) 1768 return SourceRange(); 1769 1770 return getMemberQualifier()->Range; 1771 } 1772 1773 /// \brief If the member name was qualified, retrieves the 1774 /// nested-name-specifier that precedes the member name. Otherwise, returns 1775 /// NULL. 1776 NestedNameSpecifier *getQualifier() const { 1777 if (!HasQualifierOrFoundDecl) 1778 return 0; 1779 1780 return getMemberQualifier()->NNS; 1781 } 1782 1783 /// \brief Determines whether this member expression actually had a C++ 1784 /// template argument list explicitly specified, e.g., x.f<int>. 1785 bool hasExplicitTemplateArgs() const { 1786 return HasExplicitTemplateArgumentList; 1787 } 1788 1789 /// \brief Copies the template arguments (if present) into the given 1790 /// structure. 1791 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1792 if (hasExplicitTemplateArgs()) 1793 getExplicitTemplateArgs().copyInto(List); 1794 } 1795 1796 /// \brief Retrieve the explicit template argument list that 1797 /// follow the member template name. This must only be called on an 1798 /// expression with explicit template arguments. 1799 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 1800 assert(HasExplicitTemplateArgumentList); 1801 if (!HasQualifierOrFoundDecl) 1802 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1803 1804 return *reinterpret_cast<ExplicitTemplateArgumentList *>( 1805 getMemberQualifier() + 1); 1806 } 1807 1808 /// \brief Retrieve the explicit template argument list that 1809 /// followed the member template name. This must only be called on 1810 /// an expression with explicit template arguments. 1811 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1812 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgs(); 1813 } 1814 1815 /// \brief Retrieves the optional explicit template arguments. 1816 /// This points to the same data as getExplicitTemplateArgs(), but 1817 /// returns null if there are no explicit template arguments. 1818 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() const { 1819 if (!hasExplicitTemplateArgs()) return 0; 1820 return &getExplicitTemplateArgs(); 1821 } 1822 1823 /// \brief Retrieve the location of the left angle bracket following the 1824 /// member name ('<'), if any. 1825 SourceLocation getLAngleLoc() const { 1826 if (!HasExplicitTemplateArgumentList) 1827 return SourceLocation(); 1828 1829 return getExplicitTemplateArgs().LAngleLoc; 1830 } 1831 1832 /// \brief Retrieve the template arguments provided as part of this 1833 /// template-id. 1834 const TemplateArgumentLoc *getTemplateArgs() const { 1835 if (!HasExplicitTemplateArgumentList) 1836 return 0; 1837 1838 return getExplicitTemplateArgs().getTemplateArgs(); 1839 } 1840 1841 /// \brief Retrieve the number of template arguments provided as part of this 1842 /// template-id. 1843 unsigned getNumTemplateArgs() const { 1844 if (!HasExplicitTemplateArgumentList) 1845 return 0; 1846 1847 return getExplicitTemplateArgs().NumTemplateArgs; 1848 } 1849 1850 /// \brief Retrieve the location of the right angle bracket following the 1851 /// template arguments ('>'). 1852 SourceLocation getRAngleLoc() const { 1853 if (!HasExplicitTemplateArgumentList) 1854 return SourceLocation(); 1855 1856 return getExplicitTemplateArgs().RAngleLoc; 1857 } 1858 1859 /// \brief Retrieve the member declaration name info. 1860 DeclarationNameInfo getMemberNameInfo() const { 1861 return DeclarationNameInfo(MemberDecl->getDeclName(), 1862 MemberLoc, MemberDNLoc); 1863 } 1864 1865 bool isArrow() const { return IsArrow; } 1866 void setArrow(bool A) { IsArrow = A; } 1867 1868 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1869 /// location of 'F'. 1870 SourceLocation getMemberLoc() const { return MemberLoc; } 1871 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1872 1873 virtual SourceRange getSourceRange() const { 1874 // If we have an implicit base (like a C++ implicit this), 1875 // make sure not to return its location 1876 SourceLocation EndLoc = (HasExplicitTemplateArgumentList) 1877 ? getRAngleLoc() : getMemberNameInfo().getEndLoc(); 1878 1879 SourceLocation BaseLoc = getBase()->getLocStart(); 1880 if (BaseLoc.isInvalid()) 1881 return SourceRange(MemberLoc, EndLoc); 1882 return SourceRange(BaseLoc, EndLoc); 1883 } 1884 1885 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1886 1887 static bool classof(const Stmt *T) { 1888 return T->getStmtClass() == MemberExprClass; 1889 } 1890 static bool classof(const MemberExpr *) { return true; } 1891 1892 // Iterators 1893 virtual child_iterator child_begin(); 1894 virtual child_iterator child_end(); 1895}; 1896 1897/// CompoundLiteralExpr - [C99 6.5.2.5] 1898/// 1899class CompoundLiteralExpr : public Expr { 1900 /// LParenLoc - If non-null, this is the location of the left paren in a 1901 /// compound literal like "(int){4}". This can be null if this is a 1902 /// synthesized compound expression. 1903 SourceLocation LParenLoc; 1904 1905 /// The type as written. This can be an incomplete array type, in 1906 /// which case the actual expression type will be different. 1907 TypeSourceInfo *TInfo; 1908 Stmt *Init; 1909 bool FileScope; 1910public: 1911 // FIXME: Can compound literals be value-dependent? 1912 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, 1913 QualType T, Expr *init, bool fileScope) 1914 : Expr(CompoundLiteralExprClass, T, 1915 tinfo->getType()->isDependentType(), false), 1916 LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {} 1917 1918 /// \brief Construct an empty compound literal. 1919 explicit CompoundLiteralExpr(EmptyShell Empty) 1920 : Expr(CompoundLiteralExprClass, Empty) { } 1921 1922 const Expr *getInitializer() const { return cast<Expr>(Init); } 1923 Expr *getInitializer() { return cast<Expr>(Init); } 1924 void setInitializer(Expr *E) { Init = E; } 1925 1926 bool isFileScope() const { return FileScope; } 1927 void setFileScope(bool FS) { FileScope = FS; } 1928 1929 SourceLocation getLParenLoc() const { return LParenLoc; } 1930 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1931 1932 TypeSourceInfo *getTypeSourceInfo() const { return TInfo; } 1933 void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; } 1934 1935 virtual SourceRange getSourceRange() const { 1936 // FIXME: Init should never be null. 1937 if (!Init) 1938 return SourceRange(); 1939 if (LParenLoc.isInvalid()) 1940 return Init->getSourceRange(); 1941 return SourceRange(LParenLoc, Init->getLocEnd()); 1942 } 1943 1944 static bool classof(const Stmt *T) { 1945 return T->getStmtClass() == CompoundLiteralExprClass; 1946 } 1947 static bool classof(const CompoundLiteralExpr *) { return true; } 1948 1949 // Iterators 1950 virtual child_iterator child_begin(); 1951 virtual child_iterator child_end(); 1952}; 1953 1954/// CastExpr - Base class for type casts, including both implicit 1955/// casts (ImplicitCastExpr) and explicit casts that have some 1956/// representation in the source code (ExplicitCastExpr's derived 1957/// classes). 1958class CastExpr : public Expr { 1959public: 1960 typedef clang::CastKind CastKind; 1961 1962private: 1963 unsigned Kind : 5; 1964 unsigned BasePathSize : BitsRemaining - 5; 1965 Stmt *Op; 1966 1967 void CheckBasePath() const { 1968#ifndef NDEBUG 1969 switch (getCastKind()) { 1970 case CK_DerivedToBase: 1971 case CK_UncheckedDerivedToBase: 1972 case CK_DerivedToBaseMemberPointer: 1973 case CK_BaseToDerived: 1974 case CK_BaseToDerivedMemberPointer: 1975 assert(!path_empty() && "Cast kind should have a base path!"); 1976 break; 1977 1978 // These should not have an inheritance path. 1979 case CK_Unknown: 1980 case CK_BitCast: 1981 case CK_LValueBitCast: 1982 case CK_NoOp: 1983 case CK_Dynamic: 1984 case CK_ToUnion: 1985 case CK_ArrayToPointerDecay: 1986 case CK_FunctionToPointerDecay: 1987 case CK_NullToMemberPointer: 1988 case CK_UserDefinedConversion: 1989 case CK_ConstructorConversion: 1990 case CK_IntegralToPointer: 1991 case CK_PointerToIntegral: 1992 case CK_ToVoid: 1993 case CK_VectorSplat: 1994 case CK_IntegralCast: 1995 case CK_IntegralToFloating: 1996 case CK_FloatingToIntegral: 1997 case CK_FloatingCast: 1998 case CK_MemberPointerToBoolean: 1999 case CK_AnyPointerToObjCPointerCast: 2000 case CK_AnyPointerToBlockPointerCast: 2001 case CK_ObjCObjectLValueCast: 2002 assert(path_empty() && "Cast kind should not have a base path!"); 2003 break; 2004 } 2005#endif 2006 } 2007 2008 const CXXBaseSpecifier * const *path_buffer() const { 2009 return const_cast<CastExpr*>(this)->path_buffer(); 2010 } 2011 CXXBaseSpecifier **path_buffer(); 2012 2013protected: 2014 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op, 2015 unsigned BasePathSize) : 2016 Expr(SC, ty, 2017 // Cast expressions are type-dependent if the type is 2018 // dependent (C++ [temp.dep.expr]p3). 2019 ty->isDependentType(), 2020 // Cast expressions are value-dependent if the type is 2021 // dependent or if the subexpression is value-dependent. 2022 ty->isDependentType() || (op && op->isValueDependent())), 2023 Kind(kind), BasePathSize(BasePathSize), Op(op) { 2024 CheckBasePath(); 2025 } 2026 2027 /// \brief Construct an empty cast. 2028 CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize) 2029 : Expr(SC, Empty), BasePathSize(BasePathSize) { } 2030 2031public: 2032 CastKind getCastKind() const { return static_cast<CastKind>(Kind); } 2033 void setCastKind(CastKind K) { Kind = K; } 2034 const char *getCastKindName() const; 2035 2036 Expr *getSubExpr() { return cast<Expr>(Op); } 2037 const Expr *getSubExpr() const { return cast<Expr>(Op); } 2038 void setSubExpr(Expr *E) { Op = E; } 2039 2040 /// \brief Retrieve the cast subexpression as it was written in the source 2041 /// code, looking through any implicit casts or other intermediate nodes 2042 /// introduced by semantic analysis. 2043 Expr *getSubExprAsWritten(); 2044 const Expr *getSubExprAsWritten() const { 2045 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 2046 } 2047 2048 typedef CXXBaseSpecifier **path_iterator; 2049 typedef const CXXBaseSpecifier * const *path_const_iterator; 2050 bool path_empty() const { return BasePathSize == 0; } 2051 unsigned path_size() const { return BasePathSize; } 2052 path_iterator path_begin() { return path_buffer(); } 2053 path_iterator path_end() { return path_buffer() + path_size(); } 2054 path_const_iterator path_begin() const { return path_buffer(); } 2055 path_const_iterator path_end() const { return path_buffer() + path_size(); } 2056 2057 void setCastPath(const CXXCastPath &Path); 2058 2059 static bool classof(const Stmt *T) { 2060 return T->getStmtClass() >= firstCastExprConstant && 2061 T->getStmtClass() <= lastCastExprConstant; 2062 } 2063 static bool classof(const CastExpr *) { return true; } 2064 2065 // Iterators 2066 virtual child_iterator child_begin(); 2067 virtual child_iterator child_end(); 2068}; 2069 2070/// ImplicitCastExpr - Allows us to explicitly represent implicit type 2071/// conversions, which have no direct representation in the original 2072/// source code. For example: converting T[]->T*, void f()->void 2073/// (*f)(), float->double, short->int, etc. 2074/// 2075/// In C, implicit casts always produce rvalues. However, in C++, an 2076/// implicit cast whose result is being bound to a reference will be 2077/// an lvalue or xvalue. For example: 2078/// 2079/// @code 2080/// class Base { }; 2081/// class Derived : public Base { }; 2082/// Derived &&ref(); 2083/// void f(Derived d) { 2084/// Base& b = d; // initializer is an ImplicitCastExpr 2085/// // to an lvalue of type Base 2086/// Base&& r = ref(); // initializer is an ImplicitCastExpr 2087/// // to an xvalue of type Base 2088/// } 2089/// @endcode 2090class ImplicitCastExpr : public CastExpr { 2091private: 2092 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 2093 unsigned BasePathLength, ExprValueKind VK) 2094 : CastExpr(ImplicitCastExprClass, ty, kind, op, BasePathLength) { 2095 ValueKind = VK; 2096 } 2097 2098 /// \brief Construct an empty implicit cast. 2099 explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize) 2100 : CastExpr(ImplicitCastExprClass, Shell, PathSize) { } 2101 2102public: 2103 enum OnStack_t { OnStack }; 2104 ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, 2105 ExprValueKind VK) 2106 : CastExpr(ImplicitCastExprClass, ty, kind, op, 0) { 2107 ValueKind = VK; 2108 } 2109 2110 static ImplicitCastExpr *Create(ASTContext &Context, QualType T, 2111 CastKind Kind, Expr *Operand, 2112 const CXXCastPath *BasePath, 2113 ExprValueKind Cat); 2114 2115 static ImplicitCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2116 2117 virtual SourceRange getSourceRange() const { 2118 return getSubExpr()->getSourceRange(); 2119 } 2120 2121 /// getValueKind - The value kind that this cast produces. 2122 ExprValueKind getValueKind() const { 2123 return static_cast<ExprValueKind>(ValueKind); 2124 } 2125 2126 /// setValueKind - Set the value kind this cast produces. 2127 void setValueKind(ExprValueKind Cat) { ValueKind = Cat; } 2128 2129 static bool classof(const Stmt *T) { 2130 return T->getStmtClass() == ImplicitCastExprClass; 2131 } 2132 static bool classof(const ImplicitCastExpr *) { return true; } 2133}; 2134 2135/// ExplicitCastExpr - An explicit cast written in the source 2136/// code. 2137/// 2138/// This class is effectively an abstract class, because it provides 2139/// the basic representation of an explicitly-written cast without 2140/// specifying which kind of cast (C cast, functional cast, static 2141/// cast, etc.) was written; specific derived classes represent the 2142/// particular style of cast and its location information. 2143/// 2144/// Unlike implicit casts, explicit cast nodes have two different 2145/// types: the type that was written into the source code, and the 2146/// actual type of the expression as determined by semantic 2147/// analysis. These types may differ slightly. For example, in C++ one 2148/// can cast to a reference type, which indicates that the resulting 2149/// expression will be an lvalue or xvalue. The reference type, however, 2150/// will not be used as the type of the expression. 2151class ExplicitCastExpr : public CastExpr { 2152 /// TInfo - Source type info for the (written) type 2153 /// this expression is casting to. 2154 TypeSourceInfo *TInfo; 2155 2156protected: 2157 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 2158 Expr *op, unsigned PathSize, TypeSourceInfo *writtenTy) 2159 : CastExpr(SC, exprTy, kind, op, PathSize), TInfo(writtenTy) {} 2160 2161 /// \brief Construct an empty explicit cast. 2162 ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 2163 : CastExpr(SC, Shell, PathSize) { } 2164 2165public: 2166 /// getTypeInfoAsWritten - Returns the type source info for the type 2167 /// that this expression is casting to. 2168 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 2169 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 2170 2171 /// getTypeAsWritten - Returns the type that this expression is 2172 /// casting to, as written in the source code. 2173 QualType getTypeAsWritten() const { return TInfo->getType(); } 2174 2175 static bool classof(const Stmt *T) { 2176 return T->getStmtClass() >= firstExplicitCastExprConstant && 2177 T->getStmtClass() <= lastExplicitCastExprConstant; 2178 } 2179 static bool classof(const ExplicitCastExpr *) { return true; } 2180}; 2181 2182/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 2183/// cast in C++ (C++ [expr.cast]), which uses the syntax 2184/// (Type)expr. For example: @c (int)f. 2185class CStyleCastExpr : public ExplicitCastExpr { 2186 SourceLocation LPLoc; // the location of the left paren 2187 SourceLocation RPLoc; // the location of the right paren 2188 2189 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, 2190 unsigned PathSize, TypeSourceInfo *writtenTy, 2191 SourceLocation l, SourceLocation r) 2192 : ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, PathSize, 2193 writtenTy), LPLoc(l), RPLoc(r) {} 2194 2195 /// \brief Construct an empty C-style explicit cast. 2196 explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize) 2197 : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { } 2198 2199public: 2200 static CStyleCastExpr *Create(ASTContext &Context, QualType T, CastKind K, 2201 Expr *Op, const CXXCastPath *BasePath, 2202 TypeSourceInfo *WrittenTy, SourceLocation L, 2203 SourceLocation R); 2204 2205 static CStyleCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2206 2207 SourceLocation getLParenLoc() const { return LPLoc; } 2208 void setLParenLoc(SourceLocation L) { LPLoc = L; } 2209 2210 SourceLocation getRParenLoc() const { return RPLoc; } 2211 void setRParenLoc(SourceLocation L) { RPLoc = L; } 2212 2213 virtual SourceRange getSourceRange() const { 2214 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 2215 } 2216 static bool classof(const Stmt *T) { 2217 return T->getStmtClass() == CStyleCastExprClass; 2218 } 2219 static bool classof(const CStyleCastExpr *) { return true; } 2220}; 2221 2222/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 2223/// 2224/// This expression node kind describes a builtin binary operation, 2225/// such as "x + y" for integer values "x" and "y". The operands will 2226/// already have been converted to appropriate types (e.g., by 2227/// performing promotions or conversions). 2228/// 2229/// In C++, where operators may be overloaded, a different kind of 2230/// expression node (CXXOperatorCallExpr) is used to express the 2231/// invocation of an overloaded operator with operator syntax. Within 2232/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 2233/// used to store an expression "x + y" depends on the subexpressions 2234/// for x and y. If neither x or y is type-dependent, and the "+" 2235/// operator resolves to a built-in operation, BinaryOperator will be 2236/// used to express the computation (x and y may still be 2237/// value-dependent). If either x or y is type-dependent, or if the 2238/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 2239/// be used to express the computation. 2240class BinaryOperator : public Expr { 2241public: 2242 typedef BinaryOperatorKind Opcode; 2243 2244private: 2245 unsigned Opc : 6; 2246 SourceLocation OpLoc; 2247 2248 enum { LHS, RHS, END_EXPR }; 2249 Stmt* SubExprs[END_EXPR]; 2250public: 2251 2252 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2253 SourceLocation opLoc) 2254 : Expr(BinaryOperatorClass, ResTy, 2255 lhs->isTypeDependent() || rhs->isTypeDependent(), 2256 lhs->isValueDependent() || rhs->isValueDependent()), 2257 Opc(opc), OpLoc(opLoc) { 2258 SubExprs[LHS] = lhs; 2259 SubExprs[RHS] = rhs; 2260 assert(!isCompoundAssignmentOp() && 2261 "Use ArithAssignBinaryOperator for compound assignments"); 2262 } 2263 2264 /// \brief Construct an empty binary operator. 2265 explicit BinaryOperator(EmptyShell Empty) 2266 : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { } 2267 2268 SourceLocation getOperatorLoc() const { return OpLoc; } 2269 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 2270 2271 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 2272 void setOpcode(Opcode O) { Opc = O; } 2273 2274 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2275 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2276 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2277 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2278 2279 virtual SourceRange getSourceRange() const { 2280 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 2281 } 2282 2283 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 2284 /// corresponds to, e.g. "<<=". 2285 static const char *getOpcodeStr(Opcode Op); 2286 2287 const char *getOpcodeStr() const { return getOpcodeStr(getOpcode()); } 2288 2289 /// \brief Retrieve the binary opcode that corresponds to the given 2290 /// overloaded operator. 2291 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 2292 2293 /// \brief Retrieve the overloaded operator kind that corresponds to 2294 /// the given binary opcode. 2295 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 2296 2297 /// predicates to categorize the respective opcodes. 2298 bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; } 2299 bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; } 2300 static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } 2301 bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } 2302 static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } 2303 bool isShiftOp() const { return isShiftOp(getOpcode()); } 2304 2305 static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } 2306 bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } 2307 2308 static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } 2309 bool isRelationalOp() const { return isRelationalOp(getOpcode()); } 2310 2311 static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } 2312 bool isEqualityOp() const { return isEqualityOp(getOpcode()); } 2313 2314 static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; } 2315 bool isComparisonOp() const { return isComparisonOp(getOpcode()); } 2316 2317 static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } 2318 bool isLogicalOp() const { return isLogicalOp(getOpcode()); } 2319 2320 bool isAssignmentOp() const { return Opc >= BO_Assign && Opc <= BO_OrAssign; } 2321 bool isCompoundAssignmentOp() const { 2322 return Opc > BO_Assign && Opc <= BO_OrAssign; 2323 } 2324 bool isShiftAssignOp() const { 2325 return Opc == BO_ShlAssign || Opc == BO_ShrAssign; 2326 } 2327 2328 static bool classof(const Stmt *S) { 2329 return S->getStmtClass() >= firstBinaryOperatorConstant && 2330 S->getStmtClass() <= lastBinaryOperatorConstant; 2331 } 2332 static bool classof(const BinaryOperator *) { return true; } 2333 2334 // Iterators 2335 virtual child_iterator child_begin(); 2336 virtual child_iterator child_end(); 2337 2338protected: 2339 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2340 SourceLocation opLoc, bool dead) 2341 : Expr(CompoundAssignOperatorClass, ResTy, 2342 lhs->isTypeDependent() || rhs->isTypeDependent(), 2343 lhs->isValueDependent() || rhs->isValueDependent()), 2344 Opc(opc), OpLoc(opLoc) { 2345 SubExprs[LHS] = lhs; 2346 SubExprs[RHS] = rhs; 2347 } 2348 2349 BinaryOperator(StmtClass SC, EmptyShell Empty) 2350 : Expr(SC, Empty), Opc(BO_MulAssign) { } 2351}; 2352 2353/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 2354/// track of the type the operation is performed in. Due to the semantics of 2355/// these operators, the operands are promoted, the aritmetic performed, an 2356/// implicit conversion back to the result type done, then the assignment takes 2357/// place. This captures the intermediate type which the computation is done 2358/// in. 2359class CompoundAssignOperator : public BinaryOperator { 2360 QualType ComputationLHSType; 2361 QualType ComputationResultType; 2362public: 2363 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 2364 QualType ResType, QualType CompLHSType, 2365 QualType CompResultType, 2366 SourceLocation OpLoc) 2367 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 2368 ComputationLHSType(CompLHSType), 2369 ComputationResultType(CompResultType) { 2370 assert(isCompoundAssignmentOp() && 2371 "Only should be used for compound assignments"); 2372 } 2373 2374 /// \brief Build an empty compound assignment operator expression. 2375 explicit CompoundAssignOperator(EmptyShell Empty) 2376 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 2377 2378 // The two computation types are the type the LHS is converted 2379 // to for the computation and the type of the result; the two are 2380 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 2381 QualType getComputationLHSType() const { return ComputationLHSType; } 2382 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 2383 2384 QualType getComputationResultType() const { return ComputationResultType; } 2385 void setComputationResultType(QualType T) { ComputationResultType = T; } 2386 2387 static bool classof(const CompoundAssignOperator *) { return true; } 2388 static bool classof(const Stmt *S) { 2389 return S->getStmtClass() == CompoundAssignOperatorClass; 2390 } 2391}; 2392 2393/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 2394/// GNU "missing LHS" extension is in use. 2395/// 2396class ConditionalOperator : public Expr { 2397 enum { COND, LHS, RHS, END_EXPR }; 2398 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2399 Stmt* Save; 2400 SourceLocation QuestionLoc, ColonLoc; 2401public: 2402 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 2403 SourceLocation CLoc, Expr *rhs, Expr *save, QualType t) 2404 : Expr(ConditionalOperatorClass, t, 2405 // FIXME: the type of the conditional operator doesn't 2406 // depend on the type of the conditional, but the standard 2407 // seems to imply that it could. File a bug! 2408 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 2409 (cond->isValueDependent() || 2410 (lhs && lhs->isValueDependent()) || 2411 (rhs && rhs->isValueDependent()))), 2412 QuestionLoc(QLoc), 2413 ColonLoc(CLoc) { 2414 SubExprs[COND] = cond; 2415 SubExprs[LHS] = lhs; 2416 SubExprs[RHS] = rhs; 2417 Save = save; 2418 } 2419 2420 /// \brief Build an empty conditional operator. 2421 explicit ConditionalOperator(EmptyShell Empty) 2422 : Expr(ConditionalOperatorClass, Empty) { } 2423 2424 // getCond - Return the expression representing the condition for 2425 // the ?: operator. 2426 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2427 void setCond(Expr *E) { SubExprs[COND] = E; } 2428 2429 // getTrueExpr - Return the subexpression representing the value of the ?: 2430 // expression if the condition evaluates to true. 2431 Expr *getTrueExpr() const { 2432 return cast<Expr>(!Save ? SubExprs[LHS] : SubExprs[COND]); 2433 } 2434 2435 // getFalseExpr - Return the subexpression representing the value of the ?: 2436 // expression if the condition evaluates to false. This is the same as getRHS. 2437 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 2438 2439 // getSaveExpr - In most cases this value will be null. Except a GCC extension 2440 // allows the left subexpression to be omitted, and instead of that condition 2441 // be returned. e.g: x ?: y is shorthand for x ? x : y, except that the 2442 // expression "x" is only evaluated once. Under this senario, this function 2443 // returns the original, non-converted condition expression for the ?:operator 2444 Expr *getSaveExpr() const { return Save? cast<Expr>(Save) : (Expr*)0; } 2445 2446 Expr *getLHS() const { return Save ? 0 : cast<Expr>(SubExprs[LHS]); } 2447 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2448 2449 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2450 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2451 2452 Expr *getSAVE() const { return Save? cast<Expr>(Save) : (Expr*)0; } 2453 void setSAVE(Expr *E) { Save = E; } 2454 2455 SourceLocation getQuestionLoc() const { return QuestionLoc; } 2456 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 2457 2458 SourceLocation getColonLoc() const { return ColonLoc; } 2459 void setColonLoc(SourceLocation L) { ColonLoc = L; } 2460 2461 virtual SourceRange getSourceRange() const { 2462 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 2463 } 2464 static bool classof(const Stmt *T) { 2465 return T->getStmtClass() == ConditionalOperatorClass; 2466 } 2467 static bool classof(const ConditionalOperator *) { return true; } 2468 2469 // Iterators 2470 virtual child_iterator child_begin(); 2471 virtual child_iterator child_end(); 2472}; 2473 2474/// AddrLabelExpr - The GNU address of label extension, representing &&label. 2475class AddrLabelExpr : public Expr { 2476 SourceLocation AmpAmpLoc, LabelLoc; 2477 LabelStmt *Label; 2478public: 2479 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 2480 QualType t) 2481 : Expr(AddrLabelExprClass, t, false, false), 2482 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 2483 2484 /// \brief Build an empty address of a label expression. 2485 explicit AddrLabelExpr(EmptyShell Empty) 2486 : Expr(AddrLabelExprClass, Empty) { } 2487 2488 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 2489 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 2490 SourceLocation getLabelLoc() const { return LabelLoc; } 2491 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2492 2493 virtual SourceRange getSourceRange() const { 2494 return SourceRange(AmpAmpLoc, LabelLoc); 2495 } 2496 2497 LabelStmt *getLabel() const { return Label; } 2498 void setLabel(LabelStmt *S) { Label = S; } 2499 2500 static bool classof(const Stmt *T) { 2501 return T->getStmtClass() == AddrLabelExprClass; 2502 } 2503 static bool classof(const AddrLabelExpr *) { return true; } 2504 2505 // Iterators 2506 virtual child_iterator child_begin(); 2507 virtual child_iterator child_end(); 2508}; 2509 2510/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 2511/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 2512/// takes the value of the last subexpression. 2513class StmtExpr : public Expr { 2514 Stmt *SubStmt; 2515 SourceLocation LParenLoc, RParenLoc; 2516public: 2517 // FIXME: Does type-dependence need to be computed differently? 2518 StmtExpr(CompoundStmt *substmt, QualType T, 2519 SourceLocation lp, SourceLocation rp) : 2520 Expr(StmtExprClass, T, T->isDependentType(), false), 2521 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 2522 2523 /// \brief Build an empty statement expression. 2524 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 2525 2526 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 2527 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 2528 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 2529 2530 virtual SourceRange getSourceRange() const { 2531 return SourceRange(LParenLoc, RParenLoc); 2532 } 2533 2534 SourceLocation getLParenLoc() const { return LParenLoc; } 2535 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2536 SourceLocation getRParenLoc() const { return RParenLoc; } 2537 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2538 2539 static bool classof(const Stmt *T) { 2540 return T->getStmtClass() == StmtExprClass; 2541 } 2542 static bool classof(const StmtExpr *) { return true; } 2543 2544 // Iterators 2545 virtual child_iterator child_begin(); 2546 virtual child_iterator child_end(); 2547}; 2548 2549/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 2550/// This AST node represents a function that returns 1 if two *types* (not 2551/// expressions) are compatible. The result of this built-in function can be 2552/// used in integer constant expressions. 2553class TypesCompatibleExpr : public Expr { 2554 TypeSourceInfo *TInfo1; 2555 TypeSourceInfo *TInfo2; 2556 SourceLocation BuiltinLoc, RParenLoc; 2557public: 2558 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 2559 TypeSourceInfo *tinfo1, TypeSourceInfo *tinfo2, 2560 SourceLocation RP) : 2561 Expr(TypesCompatibleExprClass, ReturnType, false, false), 2562 TInfo1(tinfo1), TInfo2(tinfo2), BuiltinLoc(BLoc), RParenLoc(RP) {} 2563 2564 /// \brief Build an empty __builtin_type_compatible_p expression. 2565 explicit TypesCompatibleExpr(EmptyShell Empty) 2566 : Expr(TypesCompatibleExprClass, Empty) { } 2567 2568 TypeSourceInfo *getArgTInfo1() const { return TInfo1; } 2569 void setArgTInfo1(TypeSourceInfo *TInfo) { TInfo1 = TInfo; } 2570 TypeSourceInfo *getArgTInfo2() const { return TInfo2; } 2571 void setArgTInfo2(TypeSourceInfo *TInfo) { TInfo2 = TInfo; } 2572 2573 QualType getArgType1() const { return TInfo1->getType(); } 2574 QualType getArgType2() const { return TInfo2->getType(); } 2575 2576 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2577 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2578 2579 SourceLocation getRParenLoc() const { return RParenLoc; } 2580 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2581 2582 virtual SourceRange getSourceRange() const { 2583 return SourceRange(BuiltinLoc, RParenLoc); 2584 } 2585 static bool classof(const Stmt *T) { 2586 return T->getStmtClass() == TypesCompatibleExprClass; 2587 } 2588 static bool classof(const TypesCompatibleExpr *) { return true; } 2589 2590 // Iterators 2591 virtual child_iterator child_begin(); 2592 virtual child_iterator child_end(); 2593}; 2594 2595/// ShuffleVectorExpr - clang-specific builtin-in function 2596/// __builtin_shufflevector. 2597/// This AST node represents a operator that does a constant 2598/// shuffle, similar to LLVM's shufflevector instruction. It takes 2599/// two vectors and a variable number of constant indices, 2600/// and returns the appropriately shuffled vector. 2601class ShuffleVectorExpr : public Expr { 2602 SourceLocation BuiltinLoc, RParenLoc; 2603 2604 // SubExprs - the list of values passed to the __builtin_shufflevector 2605 // function. The first two are vectors, and the rest are constant 2606 // indices. The number of values in this list is always 2607 // 2+the number of indices in the vector type. 2608 Stmt **SubExprs; 2609 unsigned NumExprs; 2610 2611public: 2612 // FIXME: Can a shufflevector be value-dependent? Does type-dependence need 2613 // to be computed differently? 2614 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 2615 QualType Type, SourceLocation BLoc, 2616 SourceLocation RP) : 2617 Expr(ShuffleVectorExprClass, Type, Type->isDependentType(), false), 2618 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) { 2619 2620 SubExprs = new (C) Stmt*[nexpr]; 2621 for (unsigned i = 0; i < nexpr; i++) 2622 SubExprs[i] = args[i]; 2623 } 2624 2625 /// \brief Build an empty vector-shuffle expression. 2626 explicit ShuffleVectorExpr(EmptyShell Empty) 2627 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 2628 2629 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2630 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2631 2632 SourceLocation getRParenLoc() const { return RParenLoc; } 2633 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2634 2635 virtual SourceRange getSourceRange() const { 2636 return SourceRange(BuiltinLoc, RParenLoc); 2637 } 2638 static bool classof(const Stmt *T) { 2639 return T->getStmtClass() == ShuffleVectorExprClass; 2640 } 2641 static bool classof(const ShuffleVectorExpr *) { return true; } 2642 2643 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 2644 /// constant expression, the actual arguments passed in, and the function 2645 /// pointers. 2646 unsigned getNumSubExprs() const { return NumExprs; } 2647 2648 /// getExpr - Return the Expr at the specified index. 2649 Expr *getExpr(unsigned Index) { 2650 assert((Index < NumExprs) && "Arg access out of range!"); 2651 return cast<Expr>(SubExprs[Index]); 2652 } 2653 const Expr *getExpr(unsigned Index) const { 2654 assert((Index < NumExprs) && "Arg access out of range!"); 2655 return cast<Expr>(SubExprs[Index]); 2656 } 2657 2658 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 2659 2660 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 2661 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 2662 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 2663 } 2664 2665 // Iterators 2666 virtual child_iterator child_begin(); 2667 virtual child_iterator child_end(); 2668}; 2669 2670/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 2671/// This AST node is similar to the conditional operator (?:) in C, with 2672/// the following exceptions: 2673/// - the test expression must be a integer constant expression. 2674/// - the expression returned acts like the chosen subexpression in every 2675/// visible way: the type is the same as that of the chosen subexpression, 2676/// and all predicates (whether it's an l-value, whether it's an integer 2677/// constant expression, etc.) return the same result as for the chosen 2678/// sub-expression. 2679class ChooseExpr : public Expr { 2680 enum { COND, LHS, RHS, END_EXPR }; 2681 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2682 SourceLocation BuiltinLoc, RParenLoc; 2683public: 2684 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2685 SourceLocation RP, bool TypeDependent, bool ValueDependent) 2686 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent), 2687 BuiltinLoc(BLoc), RParenLoc(RP) { 2688 SubExprs[COND] = cond; 2689 SubExprs[LHS] = lhs; 2690 SubExprs[RHS] = rhs; 2691 } 2692 2693 /// \brief Build an empty __builtin_choose_expr. 2694 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2695 2696 /// isConditionTrue - Return whether the condition is true (i.e. not 2697 /// equal to zero). 2698 bool isConditionTrue(ASTContext &C) const; 2699 2700 /// getChosenSubExpr - Return the subexpression chosen according to the 2701 /// condition. 2702 Expr *getChosenSubExpr(ASTContext &C) const { 2703 return isConditionTrue(C) ? getLHS() : getRHS(); 2704 } 2705 2706 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2707 void setCond(Expr *E) { SubExprs[COND] = E; } 2708 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2709 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2710 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2711 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2712 2713 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2714 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2715 2716 SourceLocation getRParenLoc() const { return RParenLoc; } 2717 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2718 2719 virtual SourceRange getSourceRange() const { 2720 return SourceRange(BuiltinLoc, RParenLoc); 2721 } 2722 static bool classof(const Stmt *T) { 2723 return T->getStmtClass() == ChooseExprClass; 2724 } 2725 static bool classof(const ChooseExpr *) { return true; } 2726 2727 // Iterators 2728 virtual child_iterator child_begin(); 2729 virtual child_iterator child_end(); 2730}; 2731 2732/// GNUNullExpr - Implements the GNU __null extension, which is a name 2733/// for a null pointer constant that has integral type (e.g., int or 2734/// long) and is the same size and alignment as a pointer. The __null 2735/// extension is typically only used by system headers, which define 2736/// NULL as __null in C++ rather than using 0 (which is an integer 2737/// that may not match the size of a pointer). 2738class GNUNullExpr : public Expr { 2739 /// TokenLoc - The location of the __null keyword. 2740 SourceLocation TokenLoc; 2741 2742public: 2743 GNUNullExpr(QualType Ty, SourceLocation Loc) 2744 : Expr(GNUNullExprClass, Ty, false, false), TokenLoc(Loc) { } 2745 2746 /// \brief Build an empty GNU __null expression. 2747 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2748 2749 /// getTokenLocation - The location of the __null token. 2750 SourceLocation getTokenLocation() const { return TokenLoc; } 2751 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2752 2753 virtual SourceRange getSourceRange() const { 2754 return SourceRange(TokenLoc); 2755 } 2756 static bool classof(const Stmt *T) { 2757 return T->getStmtClass() == GNUNullExprClass; 2758 } 2759 static bool classof(const GNUNullExpr *) { return true; } 2760 2761 // Iterators 2762 virtual child_iterator child_begin(); 2763 virtual child_iterator child_end(); 2764}; 2765 2766/// VAArgExpr, used for the builtin function __builtin_va_arg. 2767class VAArgExpr : public Expr { 2768 Stmt *Val; 2769 TypeSourceInfo *TInfo; 2770 SourceLocation BuiltinLoc, RParenLoc; 2771public: 2772 VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo, 2773 SourceLocation RPLoc, QualType t) 2774 : Expr(VAArgExprClass, t, t->isDependentType(), false), 2775 Val(e), TInfo(TInfo), 2776 BuiltinLoc(BLoc), 2777 RParenLoc(RPLoc) { } 2778 2779 /// \brief Create an empty __builtin_va_arg expression. 2780 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2781 2782 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2783 Expr *getSubExpr() { return cast<Expr>(Val); } 2784 void setSubExpr(Expr *E) { Val = E; } 2785 2786 TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; } 2787 void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; } 2788 2789 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2790 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2791 2792 SourceLocation getRParenLoc() const { return RParenLoc; } 2793 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2794 2795 virtual SourceRange getSourceRange() const { 2796 return SourceRange(BuiltinLoc, RParenLoc); 2797 } 2798 static bool classof(const Stmt *T) { 2799 return T->getStmtClass() == VAArgExprClass; 2800 } 2801 static bool classof(const VAArgExpr *) { return true; } 2802 2803 // Iterators 2804 virtual child_iterator child_begin(); 2805 virtual child_iterator child_end(); 2806}; 2807 2808/// @brief Describes an C or C++ initializer list. 2809/// 2810/// InitListExpr describes an initializer list, which can be used to 2811/// initialize objects of different types, including 2812/// struct/class/union types, arrays, and vectors. For example: 2813/// 2814/// @code 2815/// struct foo x = { 1, { 2, 3 } }; 2816/// @endcode 2817/// 2818/// Prior to semantic analysis, an initializer list will represent the 2819/// initializer list as written by the user, but will have the 2820/// placeholder type "void". This initializer list is called the 2821/// syntactic form of the initializer, and may contain C99 designated 2822/// initializers (represented as DesignatedInitExprs), initializations 2823/// of subobject members without explicit braces, and so on. Clients 2824/// interested in the original syntax of the initializer list should 2825/// use the syntactic form of the initializer list. 2826/// 2827/// After semantic analysis, the initializer list will represent the 2828/// semantic form of the initializer, where the initializations of all 2829/// subobjects are made explicit with nested InitListExpr nodes and 2830/// C99 designators have been eliminated by placing the designated 2831/// initializations into the subobject they initialize. Additionally, 2832/// any "holes" in the initialization, where no initializer has been 2833/// specified for a particular subobject, will be replaced with 2834/// implicitly-generated ImplicitValueInitExpr expressions that 2835/// value-initialize the subobjects. Note, however, that the 2836/// initializer lists may still have fewer initializers than there are 2837/// elements to initialize within the object. 2838/// 2839/// Given the semantic form of the initializer list, one can retrieve 2840/// the original syntactic form of that initializer list (if it 2841/// exists) using getSyntacticForm(). Since many initializer lists 2842/// have the same syntactic and semantic forms, getSyntacticForm() may 2843/// return NULL, indicating that the current initializer list also 2844/// serves as its syntactic form. 2845class InitListExpr : public Expr { 2846 // FIXME: Eliminate this vector in favor of ASTContext allocation 2847 typedef ASTVector<Stmt *> InitExprsTy; 2848 InitExprsTy InitExprs; 2849 SourceLocation LBraceLoc, RBraceLoc; 2850 2851 /// Contains the initializer list that describes the syntactic form 2852 /// written in the source code. 2853 InitListExpr *SyntacticForm; 2854 2855 /// If this initializer list initializes a union, specifies which 2856 /// field within the union will be initialized. 2857 FieldDecl *UnionFieldInit; 2858 2859 /// Whether this initializer list originally had a GNU array-range 2860 /// designator in it. This is a temporary marker used by CodeGen. 2861 bool HadArrayRangeDesignator; 2862 2863public: 2864 InitListExpr(ASTContext &C, SourceLocation lbraceloc, 2865 Expr **initexprs, unsigned numinits, 2866 SourceLocation rbraceloc); 2867 2868 /// \brief Build an empty initializer list. 2869 explicit InitListExpr(ASTContext &C, EmptyShell Empty) 2870 : Expr(InitListExprClass, Empty), InitExprs(C) { } 2871 2872 unsigned getNumInits() const { return InitExprs.size(); } 2873 2874 const Expr* getInit(unsigned Init) const { 2875 assert(Init < getNumInits() && "Initializer access out of range!"); 2876 return cast_or_null<Expr>(InitExprs[Init]); 2877 } 2878 2879 Expr* getInit(unsigned Init) { 2880 assert(Init < getNumInits() && "Initializer access out of range!"); 2881 return cast_or_null<Expr>(InitExprs[Init]); 2882 } 2883 2884 void setInit(unsigned Init, Expr *expr) { 2885 assert(Init < getNumInits() && "Initializer access out of range!"); 2886 InitExprs[Init] = expr; 2887 } 2888 2889 /// \brief Reserve space for some number of initializers. 2890 void reserveInits(ASTContext &C, unsigned NumInits); 2891 2892 /// @brief Specify the number of initializers 2893 /// 2894 /// If there are more than @p NumInits initializers, the remaining 2895 /// initializers will be destroyed. If there are fewer than @p 2896 /// NumInits initializers, NULL expressions will be added for the 2897 /// unknown initializers. 2898 void resizeInits(ASTContext &Context, unsigned NumInits); 2899 2900 /// @brief Updates the initializer at index @p Init with the new 2901 /// expression @p expr, and returns the old expression at that 2902 /// location. 2903 /// 2904 /// When @p Init is out of range for this initializer list, the 2905 /// initializer list will be extended with NULL expressions to 2906 /// accomodate the new entry. 2907 Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr); 2908 2909 /// \brief If this initializes a union, specifies which field in the 2910 /// union to initialize. 2911 /// 2912 /// Typically, this field is the first named field within the 2913 /// union. However, a designated initializer can specify the 2914 /// initialization of a different field within the union. 2915 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2916 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2917 2918 // Explicit InitListExpr's originate from source code (and have valid source 2919 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2920 bool isExplicit() { 2921 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2922 } 2923 2924 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2925 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2926 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2927 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2928 2929 /// @brief Retrieve the initializer list that describes the 2930 /// syntactic form of the initializer. 2931 /// 2932 /// 2933 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2934 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2935 2936 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2937 void sawArrayRangeDesignator(bool ARD = true) { 2938 HadArrayRangeDesignator = ARD; 2939 } 2940 2941 virtual SourceRange getSourceRange() const { 2942 return SourceRange(LBraceLoc, RBraceLoc); 2943 } 2944 static bool classof(const Stmt *T) { 2945 return T->getStmtClass() == InitListExprClass; 2946 } 2947 static bool classof(const InitListExpr *) { return true; } 2948 2949 // Iterators 2950 virtual child_iterator child_begin(); 2951 virtual child_iterator child_end(); 2952 2953 typedef InitExprsTy::iterator iterator; 2954 typedef InitExprsTy::const_iterator const_iterator; 2955 typedef InitExprsTy::reverse_iterator reverse_iterator; 2956 typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; 2957 2958 iterator begin() { return InitExprs.begin(); } 2959 const_iterator begin() const { return InitExprs.begin(); } 2960 iterator end() { return InitExprs.end(); } 2961 const_iterator end() const { return InitExprs.end(); } 2962 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2963 const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } 2964 reverse_iterator rend() { return InitExprs.rend(); } 2965 const_reverse_iterator rend() const { return InitExprs.rend(); } 2966}; 2967 2968/// @brief Represents a C99 designated initializer expression. 2969/// 2970/// A designated initializer expression (C99 6.7.8) contains one or 2971/// more designators (which can be field designators, array 2972/// designators, or GNU array-range designators) followed by an 2973/// expression that initializes the field or element(s) that the 2974/// designators refer to. For example, given: 2975/// 2976/// @code 2977/// struct point { 2978/// double x; 2979/// double y; 2980/// }; 2981/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2982/// @endcode 2983/// 2984/// The InitListExpr contains three DesignatedInitExprs, the first of 2985/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2986/// designators, one array designator for @c [2] followed by one field 2987/// designator for @c .y. The initalization expression will be 1.0. 2988class DesignatedInitExpr : public Expr { 2989public: 2990 /// \brief Forward declaration of the Designator class. 2991 class Designator; 2992 2993private: 2994 /// The location of the '=' or ':' prior to the actual initializer 2995 /// expression. 2996 SourceLocation EqualOrColonLoc; 2997 2998 /// Whether this designated initializer used the GNU deprecated 2999 /// syntax rather than the C99 '=' syntax. 3000 bool GNUSyntax : 1; 3001 3002 /// The number of designators in this initializer expression. 3003 unsigned NumDesignators : 15; 3004 3005 /// \brief The designators in this designated initialization 3006 /// expression. 3007 Designator *Designators; 3008 3009 /// The number of subexpressions of this initializer expression, 3010 /// which contains both the initializer and any additional 3011 /// expressions used by array and array-range designators. 3012 unsigned NumSubExprs : 16; 3013 3014 3015 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators, 3016 const Designator *Designators, 3017 SourceLocation EqualOrColonLoc, bool GNUSyntax, 3018 Expr **IndexExprs, unsigned NumIndexExprs, 3019 Expr *Init); 3020 3021 explicit DesignatedInitExpr(unsigned NumSubExprs) 3022 : Expr(DesignatedInitExprClass, EmptyShell()), 3023 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 3024 3025public: 3026 /// A field designator, e.g., ".x". 3027 struct FieldDesignator { 3028 /// Refers to the field that is being initialized. The low bit 3029 /// of this field determines whether this is actually a pointer 3030 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 3031 /// initially constructed, a field designator will store an 3032 /// IdentifierInfo*. After semantic analysis has resolved that 3033 /// name, the field designator will instead store a FieldDecl*. 3034 uintptr_t NameOrField; 3035 3036 /// The location of the '.' in the designated initializer. 3037 unsigned DotLoc; 3038 3039 /// The location of the field name in the designated initializer. 3040 unsigned FieldLoc; 3041 }; 3042 3043 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3044 struct ArrayOrRangeDesignator { 3045 /// Location of the first index expression within the designated 3046 /// initializer expression's list of subexpressions. 3047 unsigned Index; 3048 /// The location of the '[' starting the array range designator. 3049 unsigned LBracketLoc; 3050 /// The location of the ellipsis separating the start and end 3051 /// indices. Only valid for GNU array-range designators. 3052 unsigned EllipsisLoc; 3053 /// The location of the ']' terminating the array range designator. 3054 unsigned RBracketLoc; 3055 }; 3056 3057 /// @brief Represents a single C99 designator. 3058 /// 3059 /// @todo This class is infuriatingly similar to clang::Designator, 3060 /// but minor differences (storing indices vs. storing pointers) 3061 /// keep us from reusing it. Try harder, later, to rectify these 3062 /// differences. 3063 class Designator { 3064 /// @brief The kind of designator this describes. 3065 enum { 3066 FieldDesignator, 3067 ArrayDesignator, 3068 ArrayRangeDesignator 3069 } Kind; 3070 3071 union { 3072 /// A field designator, e.g., ".x". 3073 struct FieldDesignator Field; 3074 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3075 struct ArrayOrRangeDesignator ArrayOrRange; 3076 }; 3077 friend class DesignatedInitExpr; 3078 3079 public: 3080 Designator() {} 3081 3082 /// @brief Initializes a field designator. 3083 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 3084 SourceLocation FieldLoc) 3085 : Kind(FieldDesignator) { 3086 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 3087 Field.DotLoc = DotLoc.getRawEncoding(); 3088 Field.FieldLoc = FieldLoc.getRawEncoding(); 3089 } 3090 3091 /// @brief Initializes an array designator. 3092 Designator(unsigned Index, SourceLocation LBracketLoc, 3093 SourceLocation RBracketLoc) 3094 : Kind(ArrayDesignator) { 3095 ArrayOrRange.Index = Index; 3096 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3097 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 3098 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3099 } 3100 3101 /// @brief Initializes a GNU array-range designator. 3102 Designator(unsigned Index, SourceLocation LBracketLoc, 3103 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 3104 : Kind(ArrayRangeDesignator) { 3105 ArrayOrRange.Index = Index; 3106 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3107 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 3108 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3109 } 3110 3111 bool isFieldDesignator() const { return Kind == FieldDesignator; } 3112 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 3113 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 3114 3115 IdentifierInfo * getFieldName(); 3116 3117 FieldDecl *getField() { 3118 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3119 if (Field.NameOrField & 0x01) 3120 return 0; 3121 else 3122 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 3123 } 3124 3125 void setField(FieldDecl *FD) { 3126 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3127 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 3128 } 3129 3130 SourceLocation getDotLoc() const { 3131 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3132 return SourceLocation::getFromRawEncoding(Field.DotLoc); 3133 } 3134 3135 SourceLocation getFieldLoc() const { 3136 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3137 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 3138 } 3139 3140 SourceLocation getLBracketLoc() const { 3141 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3142 "Only valid on an array or array-range designator"); 3143 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 3144 } 3145 3146 SourceLocation getRBracketLoc() const { 3147 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3148 "Only valid on an array or array-range designator"); 3149 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 3150 } 3151 3152 SourceLocation getEllipsisLoc() const { 3153 assert(Kind == ArrayRangeDesignator && 3154 "Only valid on an array-range designator"); 3155 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 3156 } 3157 3158 unsigned getFirstExprIndex() const { 3159 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3160 "Only valid on an array or array-range designator"); 3161 return ArrayOrRange.Index; 3162 } 3163 3164 SourceLocation getStartLocation() const { 3165 if (Kind == FieldDesignator) 3166 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 3167 else 3168 return getLBracketLoc(); 3169 } 3170 }; 3171 3172 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 3173 unsigned NumDesignators, 3174 Expr **IndexExprs, unsigned NumIndexExprs, 3175 SourceLocation EqualOrColonLoc, 3176 bool GNUSyntax, Expr *Init); 3177 3178 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 3179 3180 /// @brief Returns the number of designators in this initializer. 3181 unsigned size() const { return NumDesignators; } 3182 3183 // Iterator access to the designators. 3184 typedef Designator* designators_iterator; 3185 designators_iterator designators_begin() { return Designators; } 3186 designators_iterator designators_end() { 3187 return Designators + NumDesignators; 3188 } 3189 3190 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 3191 3192 void setDesignators(ASTContext &C, const Designator *Desigs, 3193 unsigned NumDesigs); 3194 3195 Expr *getArrayIndex(const Designator& D); 3196 Expr *getArrayRangeStart(const Designator& D); 3197 Expr *getArrayRangeEnd(const Designator& D); 3198 3199 /// @brief Retrieve the location of the '=' that precedes the 3200 /// initializer value itself, if present. 3201 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 3202 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 3203 3204 /// @brief Determines whether this designated initializer used the 3205 /// deprecated GNU syntax for designated initializers. 3206 bool usesGNUSyntax() const { return GNUSyntax; } 3207 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 3208 3209 /// @brief Retrieve the initializer value. 3210 Expr *getInit() const { 3211 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 3212 } 3213 3214 void setInit(Expr *init) { 3215 *child_begin() = init; 3216 } 3217 3218 /// \brief Retrieve the total number of subexpressions in this 3219 /// designated initializer expression, including the actual 3220 /// initialized value and any expressions that occur within array 3221 /// and array-range designators. 3222 unsigned getNumSubExprs() const { return NumSubExprs; } 3223 3224 Expr *getSubExpr(unsigned Idx) { 3225 assert(Idx < NumSubExprs && "Subscript out of range"); 3226 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3227 Ptr += sizeof(DesignatedInitExpr); 3228 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 3229 } 3230 3231 void setSubExpr(unsigned Idx, Expr *E) { 3232 assert(Idx < NumSubExprs && "Subscript out of range"); 3233 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3234 Ptr += sizeof(DesignatedInitExpr); 3235 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 3236 } 3237 3238 /// \brief Replaces the designator at index @p Idx with the series 3239 /// of designators in [First, Last). 3240 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First, 3241 const Designator *Last); 3242 3243 virtual SourceRange getSourceRange() const; 3244 3245 static bool classof(const Stmt *T) { 3246 return T->getStmtClass() == DesignatedInitExprClass; 3247 } 3248 static bool classof(const DesignatedInitExpr *) { return true; } 3249 3250 // Iterators 3251 virtual child_iterator child_begin(); 3252 virtual child_iterator child_end(); 3253}; 3254 3255/// \brief Represents an implicitly-generated value initialization of 3256/// an object of a given type. 3257/// 3258/// Implicit value initializations occur within semantic initializer 3259/// list expressions (InitListExpr) as placeholders for subobject 3260/// initializations not explicitly specified by the user. 3261/// 3262/// \see InitListExpr 3263class ImplicitValueInitExpr : public Expr { 3264public: 3265 explicit ImplicitValueInitExpr(QualType ty) 3266 : Expr(ImplicitValueInitExprClass, ty, false, false) { } 3267 3268 /// \brief Construct an empty implicit value initialization. 3269 explicit ImplicitValueInitExpr(EmptyShell Empty) 3270 : Expr(ImplicitValueInitExprClass, Empty) { } 3271 3272 static bool classof(const Stmt *T) { 3273 return T->getStmtClass() == ImplicitValueInitExprClass; 3274 } 3275 static bool classof(const ImplicitValueInitExpr *) { return true; } 3276 3277 virtual SourceRange getSourceRange() const { 3278 return SourceRange(); 3279 } 3280 3281 // Iterators 3282 virtual child_iterator child_begin(); 3283 virtual child_iterator child_end(); 3284}; 3285 3286 3287class ParenListExpr : public Expr { 3288 Stmt **Exprs; 3289 unsigned NumExprs; 3290 SourceLocation LParenLoc, RParenLoc; 3291 3292public: 3293 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 3294 unsigned numexprs, SourceLocation rparenloc); 3295 3296 /// \brief Build an empty paren list. 3297 explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 3298 3299 unsigned getNumExprs() const { return NumExprs; } 3300 3301 const Expr* getExpr(unsigned Init) const { 3302 assert(Init < getNumExprs() && "Initializer access out of range!"); 3303 return cast_or_null<Expr>(Exprs[Init]); 3304 } 3305 3306 Expr* getExpr(unsigned Init) { 3307 assert(Init < getNumExprs() && "Initializer access out of range!"); 3308 return cast_or_null<Expr>(Exprs[Init]); 3309 } 3310 3311 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 3312 3313 SourceLocation getLParenLoc() const { return LParenLoc; } 3314 SourceLocation getRParenLoc() const { return RParenLoc; } 3315 3316 virtual SourceRange getSourceRange() const { 3317 return SourceRange(LParenLoc, RParenLoc); 3318 } 3319 static bool classof(const Stmt *T) { 3320 return T->getStmtClass() == ParenListExprClass; 3321 } 3322 static bool classof(const ParenListExpr *) { return true; } 3323 3324 // Iterators 3325 virtual child_iterator child_begin(); 3326 virtual child_iterator child_end(); 3327 3328 friend class ASTStmtReader; 3329 friend class ASTStmtWriter; 3330}; 3331 3332 3333//===----------------------------------------------------------------------===// 3334// Clang Extensions 3335//===----------------------------------------------------------------------===// 3336 3337 3338/// ExtVectorElementExpr - This represents access to specific elements of a 3339/// vector, and may occur on the left hand side or right hand side. For example 3340/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 3341/// 3342/// Note that the base may have either vector or pointer to vector type, just 3343/// like a struct field reference. 3344/// 3345class ExtVectorElementExpr : public Expr { 3346 Stmt *Base; 3347 IdentifierInfo *Accessor; 3348 SourceLocation AccessorLoc; 3349public: 3350 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 3351 SourceLocation loc) 3352 : Expr(ExtVectorElementExprClass, ty, base->isTypeDependent(), 3353 base->isValueDependent()), 3354 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 3355 3356 /// \brief Build an empty vector element expression. 3357 explicit ExtVectorElementExpr(EmptyShell Empty) 3358 : Expr(ExtVectorElementExprClass, Empty) { } 3359 3360 const Expr *getBase() const { return cast<Expr>(Base); } 3361 Expr *getBase() { return cast<Expr>(Base); } 3362 void setBase(Expr *E) { Base = E; } 3363 3364 IdentifierInfo &getAccessor() const { return *Accessor; } 3365 void setAccessor(IdentifierInfo *II) { Accessor = II; } 3366 3367 SourceLocation getAccessorLoc() const { return AccessorLoc; } 3368 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 3369 3370 /// getNumElements - Get the number of components being selected. 3371 unsigned getNumElements() const; 3372 3373 /// containsDuplicateElements - Return true if any element access is 3374 /// repeated. 3375 bool containsDuplicateElements() const; 3376 3377 /// getEncodedElementAccess - Encode the elements accessed into an llvm 3378 /// aggregate Constant of ConstantInt(s). 3379 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 3380 3381 virtual SourceRange getSourceRange() const { 3382 return SourceRange(getBase()->getLocStart(), AccessorLoc); 3383 } 3384 3385 /// isArrow - Return true if the base expression is a pointer to vector, 3386 /// return false if the base expression is a vector. 3387 bool isArrow() const; 3388 3389 static bool classof(const Stmt *T) { 3390 return T->getStmtClass() == ExtVectorElementExprClass; 3391 } 3392 static bool classof(const ExtVectorElementExpr *) { return true; } 3393 3394 // Iterators 3395 virtual child_iterator child_begin(); 3396 virtual child_iterator child_end(); 3397}; 3398 3399 3400/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 3401/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 3402class BlockExpr : public Expr { 3403protected: 3404 BlockDecl *TheBlock; 3405 bool HasBlockDeclRefExprs; 3406public: 3407 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 3408 : Expr(BlockExprClass, ty, ty->isDependentType(), false), 3409 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 3410 3411 /// \brief Build an empty block expression. 3412 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 3413 3414 const BlockDecl *getBlockDecl() const { return TheBlock; } 3415 BlockDecl *getBlockDecl() { return TheBlock; } 3416 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 3417 3418 // Convenience functions for probing the underlying BlockDecl. 3419 SourceLocation getCaretLocation() const; 3420 const Stmt *getBody() const; 3421 Stmt *getBody(); 3422 3423 virtual SourceRange getSourceRange() const { 3424 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 3425 } 3426 3427 /// getFunctionType - Return the underlying function type for this block. 3428 const FunctionType *getFunctionType() const; 3429 3430 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 3431 /// inside of the block that reference values outside the block. 3432 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 3433 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 3434 3435 static bool classof(const Stmt *T) { 3436 return T->getStmtClass() == BlockExprClass; 3437 } 3438 static bool classof(const BlockExpr *) { return true; } 3439 3440 // Iterators 3441 virtual child_iterator child_begin(); 3442 virtual child_iterator child_end(); 3443}; 3444 3445/// BlockDeclRefExpr - A reference to a declared variable, function, 3446/// enum, etc. 3447class BlockDeclRefExpr : public Expr { 3448 ValueDecl *D; 3449 SourceLocation Loc; 3450 bool IsByRef : 1; 3451 bool ConstQualAdded : 1; 3452 Stmt *CopyConstructorVal; 3453public: 3454 // FIXME: Fix type/value dependence! 3455 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 3456 bool constAdded = false, 3457 Stmt *copyConstructorVal = 0) 3458 : Expr(BlockDeclRefExprClass, t, (!t.isNull() && t->isDependentType()),false), 3459 D(d), Loc(l), IsByRef(ByRef), 3460 ConstQualAdded(constAdded), CopyConstructorVal(copyConstructorVal) {} 3461 3462 // \brief Build an empty reference to a declared variable in a 3463 // block. 3464 explicit BlockDeclRefExpr(EmptyShell Empty) 3465 : Expr(BlockDeclRefExprClass, Empty) { } 3466 3467 ValueDecl *getDecl() { return D; } 3468 const ValueDecl *getDecl() const { return D; } 3469 void setDecl(ValueDecl *VD) { D = VD; } 3470 3471 SourceLocation getLocation() const { return Loc; } 3472 void setLocation(SourceLocation L) { Loc = L; } 3473 3474 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 3475 3476 bool isByRef() const { return IsByRef; } 3477 void setByRef(bool BR) { IsByRef = BR; } 3478 3479 bool isConstQualAdded() const { return ConstQualAdded; } 3480 void setConstQualAdded(bool C) { ConstQualAdded = C; } 3481 3482 const Expr *getCopyConstructorExpr() const 3483 { return cast_or_null<Expr>(CopyConstructorVal); } 3484 Expr *getCopyConstructorExpr() 3485 { return cast_or_null<Expr>(CopyConstructorVal); } 3486 void setCopyConstructorExpr(Expr *E) { CopyConstructorVal = E; } 3487 3488 static bool classof(const Stmt *T) { 3489 return T->getStmtClass() == BlockDeclRefExprClass; 3490 } 3491 static bool classof(const BlockDeclRefExpr *) { return true; } 3492 3493 // Iterators 3494 virtual child_iterator child_begin(); 3495 virtual child_iterator child_end(); 3496}; 3497 3498} // end namespace clang 3499 3500#endif 3501