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