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