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