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