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