Expr.h revision 0840f2378e9b5764755c5dffd27d7878c7606a0c
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 unsigned BitWidth; 1089 union { 1090 uint64_t VAL; ///< Used to store the <= 64 bits integer value. 1091 uint64_t *pVal; ///< Used to store the >64 bits integer value. 1092 }; 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() : BitWidth(0), VAL(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 : public 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 { 1129 APIntStorage Num; 1130 SourceLocation Loc; 1131 1132 /// \brief Construct an empty integer literal. 1133 explicit IntegerLiteral(EmptyShell Empty) 1134 : Expr(IntegerLiteralClass, Empty) { } 1135 1136public: 1137 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 1138 // or UnsignedLongLongTy 1139 IntegerLiteral(ASTContext &C, const llvm::APInt &V, 1140 QualType type, SourceLocation l) 1141 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false, 1142 false, false), 1143 Loc(l) { 1144 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 1145 assert(V.getBitWidth() == C.getIntWidth(type) && 1146 "Integer type is not the correct size for constant."); 1147 setValue(C, V); 1148 } 1149 1150 /// \brief Returns a new integer literal with value 'V' and type 'type'. 1151 /// \param type - either IntTy, LongTy, LongLongTy, UnsignedIntTy, 1152 /// UnsignedLongTy, or UnsignedLongLongTy which should match the size of V 1153 /// \param V - the value that the returned integer literal contains. 1154 static IntegerLiteral *Create(ASTContext &C, const llvm::APInt &V, 1155 QualType type, SourceLocation l); 1156 /// \brief Returns a new empty integer literal. 1157 static IntegerLiteral *Create(ASTContext &C, EmptyShell Empty); 1158 1159 llvm::APInt getValue() const { return Num.getValue(); } 1160 SourceRange getSourceRange() const { return SourceRange(Loc); } 1161 1162 /// \brief Retrieve the location of the literal. 1163 SourceLocation getLocation() const { return Loc; } 1164 1165 void setValue(ASTContext &C, const llvm::APInt &Val) { Num.setValue(C, Val); } 1166 void setLocation(SourceLocation Location) { Loc = Location; } 1167 1168 static bool classof(const Stmt *T) { 1169 return T->getStmtClass() == IntegerLiteralClass; 1170 } 1171 static bool classof(const IntegerLiteral *) { return true; } 1172 1173 // Iterators 1174 child_range children() { return child_range(); } 1175}; 1176 1177class CharacterLiteral : public Expr { 1178public: 1179 enum CharacterKind { 1180 Ascii, 1181 Wide, 1182 UTF16, 1183 UTF32 1184 }; 1185 1186private: 1187 unsigned Value; 1188 SourceLocation Loc; 1189 unsigned Kind : 2; 1190public: 1191 // type should be IntTy 1192 CharacterLiteral(unsigned value, CharacterKind kind, QualType type, 1193 SourceLocation l) 1194 : Expr(CharacterLiteralClass, type, VK_RValue, OK_Ordinary, false, false, 1195 false, false), 1196 Value(value), Loc(l), Kind(kind) { 1197 } 1198 1199 /// \brief Construct an empty character literal. 1200 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 1201 1202 SourceLocation getLocation() const { return Loc; } 1203 CharacterKind getKind() const { return static_cast<CharacterKind>(Kind); } 1204 1205 SourceRange getSourceRange() const { return SourceRange(Loc); } 1206 1207 unsigned getValue() const { return Value; } 1208 1209 void setLocation(SourceLocation Location) { Loc = Location; } 1210 void setKind(CharacterKind kind) { Kind = kind; } 1211 void setValue(unsigned Val) { Value = Val; } 1212 1213 static bool classof(const Stmt *T) { 1214 return T->getStmtClass() == CharacterLiteralClass; 1215 } 1216 static bool classof(const CharacterLiteral *) { return true; } 1217 1218 // Iterators 1219 child_range children() { return child_range(); } 1220}; 1221 1222class FloatingLiteral : public Expr { 1223 APFloatStorage Num; 1224 bool IsIEEE : 1; // Distinguishes between PPC128 and IEEE128. 1225 bool IsExact : 1; 1226 SourceLocation Loc; 1227 1228 FloatingLiteral(ASTContext &C, const llvm::APFloat &V, bool isexact, 1229 QualType Type, SourceLocation L) 1230 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false, 1231 false, false), 1232 IsIEEE(&C.getTargetInfo().getLongDoubleFormat() == 1233 &llvm::APFloat::IEEEquad), 1234 IsExact(isexact), Loc(L) { 1235 setValue(C, V); 1236 } 1237 1238 /// \brief Construct an empty floating-point literal. 1239 explicit FloatingLiteral(ASTContext &C, EmptyShell Empty) 1240 : Expr(FloatingLiteralClass, Empty), 1241 IsIEEE(&C.getTargetInfo().getLongDoubleFormat() == 1242 &llvm::APFloat::IEEEquad), 1243 IsExact(false) { } 1244 1245public: 1246 static FloatingLiteral *Create(ASTContext &C, const llvm::APFloat &V, 1247 bool isexact, QualType Type, SourceLocation L); 1248 static FloatingLiteral *Create(ASTContext &C, EmptyShell Empty); 1249 1250 llvm::APFloat getValue() const { return Num.getValue(IsIEEE); } 1251 void setValue(ASTContext &C, const llvm::APFloat &Val) { 1252 Num.setValue(C, Val); 1253 } 1254 1255 bool isExact() const { return IsExact; } 1256 void setExact(bool E) { IsExact = E; } 1257 1258 /// getValueAsApproximateDouble - This returns the value as an inaccurate 1259 /// double. Note that this may cause loss of precision, but is useful for 1260 /// debugging dumps, etc. 1261 double getValueAsApproximateDouble() const; 1262 1263 SourceLocation getLocation() const { return Loc; } 1264 void setLocation(SourceLocation L) { Loc = L; } 1265 1266 SourceRange getSourceRange() const { return SourceRange(Loc); } 1267 1268 static bool classof(const Stmt *T) { 1269 return T->getStmtClass() == FloatingLiteralClass; 1270 } 1271 static bool classof(const FloatingLiteral *) { return true; } 1272 1273 // Iterators 1274 child_range children() { return child_range(); } 1275}; 1276 1277/// ImaginaryLiteral - We support imaginary integer and floating point literals, 1278/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 1279/// IntegerLiteral classes. Instances of this class always have a Complex type 1280/// whose element type matches the subexpression. 1281/// 1282class ImaginaryLiteral : public Expr { 1283 Stmt *Val; 1284public: 1285 ImaginaryLiteral(Expr *val, QualType Ty) 1286 : Expr(ImaginaryLiteralClass, Ty, VK_RValue, OK_Ordinary, false, false, 1287 false, false), 1288 Val(val) {} 1289 1290 /// \brief Build an empty imaginary literal. 1291 explicit ImaginaryLiteral(EmptyShell Empty) 1292 : Expr(ImaginaryLiteralClass, Empty) { } 1293 1294 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1295 Expr *getSubExpr() { return cast<Expr>(Val); } 1296 void setSubExpr(Expr *E) { Val = E; } 1297 1298 SourceRange getSourceRange() const { return Val->getSourceRange(); } 1299 static bool classof(const Stmt *T) { 1300 return T->getStmtClass() == ImaginaryLiteralClass; 1301 } 1302 static bool classof(const ImaginaryLiteral *) { return true; } 1303 1304 // Iterators 1305 child_range children() { return child_range(&Val, &Val+1); } 1306}; 1307 1308/// StringLiteral - This represents a string literal expression, e.g. "foo" 1309/// or L"bar" (wide strings). The actual string is returned by getStrData() 1310/// is NOT null-terminated, and the length of the string is determined by 1311/// calling getByteLength(). The C type for a string is always a 1312/// ConstantArrayType. In C++, the char type is const qualified, in C it is 1313/// not. 1314/// 1315/// Note that strings in C can be formed by concatenation of multiple string 1316/// literal pptokens in translation phase #6. This keeps track of the locations 1317/// of each of these pieces. 1318/// 1319/// Strings in C can also be truncated and extended by assigning into arrays, 1320/// e.g. with constructs like: 1321/// char X[2] = "foobar"; 1322/// In this case, getByteLength() will return 6, but the string literal will 1323/// have type "char[2]". 1324class StringLiteral : public Expr { 1325public: 1326 enum StringKind { 1327 Ascii, 1328 Wide, 1329 UTF8, 1330 UTF16, 1331 UTF32 1332 }; 1333 1334private: 1335 friend class ASTStmtReader; 1336 1337 union { 1338 const char *asChar; 1339 const uint16_t *asUInt16; 1340 const uint32_t *asUInt32; 1341 } StrData; 1342 unsigned Length; 1343 unsigned CharByteWidth : 4; 1344 unsigned Kind : 3; 1345 unsigned IsPascal : 1; 1346 unsigned NumConcatenated; 1347 SourceLocation TokLocs[1]; 1348 1349 StringLiteral(QualType Ty) : 1350 Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary, false, false, false, 1351 false) {} 1352 1353 static int mapCharByteWidth(TargetInfo const &target,StringKind k); 1354 1355public: 1356 /// This is the "fully general" constructor that allows representation of 1357 /// strings formed from multiple concatenated tokens. 1358 static StringLiteral *Create(ASTContext &C, StringRef Str, StringKind Kind, 1359 bool Pascal, QualType Ty, 1360 const SourceLocation *Loc, unsigned NumStrs); 1361 1362 /// Simple constructor for string literals made from one token. 1363 static StringLiteral *Create(ASTContext &C, StringRef Str, StringKind Kind, 1364 bool Pascal, QualType Ty, 1365 SourceLocation Loc) { 1366 return Create(C, Str, Kind, Pascal, Ty, &Loc, 1); 1367 } 1368 1369 /// \brief Construct an empty string literal. 1370 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 1371 1372 StringRef getString() const { 1373 assert(CharByteWidth==1 1374 && "This function is used in places that assume strings use char"); 1375 return StringRef(StrData.asChar, getByteLength()); 1376 } 1377 1378 /// Allow clients that need the byte representation, such as ASTWriterStmt 1379 /// ::VisitStringLiteral(), access. 1380 StringRef getBytes() const { 1381 // FIXME: StringRef may not be the right type to use as a result for this. 1382 if (CharByteWidth == 1) 1383 return StringRef(StrData.asChar, getByteLength()); 1384 if (CharByteWidth == 4) 1385 return StringRef(reinterpret_cast<const char*>(StrData.asUInt32), 1386 getByteLength()); 1387 assert(CharByteWidth == 2 && "unsupported CharByteWidth"); 1388 return StringRef(reinterpret_cast<const char*>(StrData.asUInt16), 1389 getByteLength()); 1390 } 1391 1392 uint32_t getCodeUnit(size_t i) const { 1393 assert(i < Length && "out of bounds access"); 1394 if (CharByteWidth == 1) 1395 return static_cast<unsigned char>(StrData.asChar[i]); 1396 if (CharByteWidth == 4) 1397 return StrData.asUInt32[i]; 1398 assert(CharByteWidth == 2 && "unsupported CharByteWidth"); 1399 return StrData.asUInt16[i]; 1400 } 1401 1402 unsigned getByteLength() const { return CharByteWidth*Length; } 1403 unsigned getLength() const { return Length; } 1404 unsigned getCharByteWidth() const { return CharByteWidth; } 1405 1406 /// \brief Sets the string data to the given string data. 1407 void setString(ASTContext &C, StringRef Str, 1408 StringKind Kind, bool IsPascal); 1409 1410 StringKind getKind() const { return static_cast<StringKind>(Kind); } 1411 1412 1413 bool isAscii() const { return Kind == Ascii; } 1414 bool isWide() const { return Kind == Wide; } 1415 bool isUTF8() const { return Kind == UTF8; } 1416 bool isUTF16() const { return Kind == UTF16; } 1417 bool isUTF32() const { return Kind == UTF32; } 1418 bool isPascal() const { return IsPascal; } 1419 1420 bool containsNonAsciiOrNull() const { 1421 StringRef Str = getString(); 1422 for (unsigned i = 0, e = Str.size(); i != e; ++i) 1423 if (!isascii(Str[i]) || !Str[i]) 1424 return true; 1425 return false; 1426 } 1427 1428 /// getNumConcatenated - Get the number of string literal tokens that were 1429 /// concatenated in translation phase #6 to form this string literal. 1430 unsigned getNumConcatenated() const { return NumConcatenated; } 1431 1432 SourceLocation getStrTokenLoc(unsigned TokNum) const { 1433 assert(TokNum < NumConcatenated && "Invalid tok number"); 1434 return TokLocs[TokNum]; 1435 } 1436 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 1437 assert(TokNum < NumConcatenated && "Invalid tok number"); 1438 TokLocs[TokNum] = L; 1439 } 1440 1441 /// getLocationOfByte - Return a source location that points to the specified 1442 /// byte of this string literal. 1443 /// 1444 /// Strings are amazingly complex. They can be formed from multiple tokens 1445 /// and can have escape sequences in them in addition to the usual trigraph 1446 /// and escaped newline business. This routine handles this complexity. 1447 /// 1448 SourceLocation getLocationOfByte(unsigned ByteNo, const SourceManager &SM, 1449 const LangOptions &Features, 1450 const TargetInfo &Target) const; 1451 1452 typedef const SourceLocation *tokloc_iterator; 1453 tokloc_iterator tokloc_begin() const { return TokLocs; } 1454 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 1455 1456 SourceRange getSourceRange() const { 1457 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 1458 } 1459 static bool classof(const Stmt *T) { 1460 return T->getStmtClass() == StringLiteralClass; 1461 } 1462 static bool classof(const StringLiteral *) { return true; } 1463 1464 // Iterators 1465 child_range children() { return child_range(); } 1466}; 1467 1468/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 1469/// AST node is only formed if full location information is requested. 1470class ParenExpr : public Expr { 1471 SourceLocation L, R; 1472 Stmt *Val; 1473public: 1474 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 1475 : Expr(ParenExprClass, val->getType(), 1476 val->getValueKind(), val->getObjectKind(), 1477 val->isTypeDependent(), val->isValueDependent(), 1478 val->isInstantiationDependent(), 1479 val->containsUnexpandedParameterPack()), 1480 L(l), R(r), Val(val) {} 1481 1482 /// \brief Construct an empty parenthesized expression. 1483 explicit ParenExpr(EmptyShell Empty) 1484 : Expr(ParenExprClass, Empty) { } 1485 1486 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1487 Expr *getSubExpr() { return cast<Expr>(Val); } 1488 void setSubExpr(Expr *E) { Val = E; } 1489 1490 SourceRange getSourceRange() const { return SourceRange(L, R); } 1491 1492 /// \brief Get the location of the left parentheses '('. 1493 SourceLocation getLParen() const { return L; } 1494 void setLParen(SourceLocation Loc) { L = Loc; } 1495 1496 /// \brief Get the location of the right parentheses ')'. 1497 SourceLocation getRParen() const { return R; } 1498 void setRParen(SourceLocation Loc) { R = Loc; } 1499 1500 static bool classof(const Stmt *T) { 1501 return T->getStmtClass() == ParenExprClass; 1502 } 1503 static bool classof(const ParenExpr *) { return true; } 1504 1505 // Iterators 1506 child_range children() { return child_range(&Val, &Val+1); } 1507}; 1508 1509 1510/// UnaryOperator - This represents the unary-expression's (except sizeof and 1511/// alignof), the postinc/postdec operators from postfix-expression, and various 1512/// extensions. 1513/// 1514/// Notes on various nodes: 1515/// 1516/// Real/Imag - These return the real/imag part of a complex operand. If 1517/// applied to a non-complex value, the former returns its operand and the 1518/// later returns zero in the type of the operand. 1519/// 1520class UnaryOperator : public Expr { 1521public: 1522 typedef UnaryOperatorKind Opcode; 1523 1524private: 1525 unsigned Opc : 5; 1526 SourceLocation Loc; 1527 Stmt *Val; 1528public: 1529 1530 UnaryOperator(Expr *input, Opcode opc, QualType type, 1531 ExprValueKind VK, ExprObjectKind OK, SourceLocation l) 1532 : Expr(UnaryOperatorClass, type, VK, OK, 1533 input->isTypeDependent() || type->isDependentType(), 1534 input->isValueDependent(), 1535 (input->isInstantiationDependent() || 1536 type->isInstantiationDependentType()), 1537 input->containsUnexpandedParameterPack()), 1538 Opc(opc), Loc(l), Val(input) {} 1539 1540 /// \brief Build an empty unary operator. 1541 explicit UnaryOperator(EmptyShell Empty) 1542 : Expr(UnaryOperatorClass, Empty), Opc(UO_AddrOf) { } 1543 1544 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 1545 void setOpcode(Opcode O) { Opc = O; } 1546 1547 Expr *getSubExpr() const { return cast<Expr>(Val); } 1548 void setSubExpr(Expr *E) { Val = E; } 1549 1550 /// getOperatorLoc - Return the location of the operator. 1551 SourceLocation getOperatorLoc() const { return Loc; } 1552 void setOperatorLoc(SourceLocation L) { Loc = L; } 1553 1554 /// isPostfix - Return true if this is a postfix operation, like x++. 1555 static bool isPostfix(Opcode Op) { 1556 return Op == UO_PostInc || Op == UO_PostDec; 1557 } 1558 1559 /// isPrefix - Return true if this is a prefix operation, like --x. 1560 static bool isPrefix(Opcode Op) { 1561 return Op == UO_PreInc || Op == UO_PreDec; 1562 } 1563 1564 bool isPrefix() const { return isPrefix(getOpcode()); } 1565 bool isPostfix() const { return isPostfix(getOpcode()); } 1566 1567 static bool isIncrementOp(Opcode Op) { 1568 return Op == UO_PreInc || Op == UO_PostInc; 1569 } 1570 bool isIncrementOp() const { 1571 return isIncrementOp(getOpcode()); 1572 } 1573 1574 static bool isDecrementOp(Opcode Op) { 1575 return Op == UO_PreDec || Op == UO_PostDec; 1576 } 1577 bool isDecrementOp() const { 1578 return isDecrementOp(getOpcode()); 1579 } 1580 1581 static bool isIncrementDecrementOp(Opcode Op) { return Op <= UO_PreDec; } 1582 bool isIncrementDecrementOp() const { 1583 return isIncrementDecrementOp(getOpcode()); 1584 } 1585 1586 static bool isArithmeticOp(Opcode Op) { 1587 return Op >= UO_Plus && Op <= UO_LNot; 1588 } 1589 bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); } 1590 1591 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1592 /// corresponds to, e.g. "sizeof" or "[pre]++" 1593 static const char *getOpcodeStr(Opcode Op); 1594 1595 /// \brief Retrieve the unary opcode that corresponds to the given 1596 /// overloaded operator. 1597 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 1598 1599 /// \brief Retrieve the overloaded operator kind that corresponds to 1600 /// the given unary opcode. 1601 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1602 1603 SourceRange getSourceRange() const { 1604 if (isPostfix()) 1605 return SourceRange(Val->getLocStart(), Loc); 1606 else 1607 return SourceRange(Loc, Val->getLocEnd()); 1608 } 1609 SourceLocation getExprLoc() const { return Loc; } 1610 1611 static bool classof(const Stmt *T) { 1612 return T->getStmtClass() == UnaryOperatorClass; 1613 } 1614 static bool classof(const UnaryOperator *) { return true; } 1615 1616 // Iterators 1617 child_range children() { return child_range(&Val, &Val+1); } 1618}; 1619 1620/// OffsetOfExpr - [C99 7.17] - This represents an expression of the form 1621/// offsetof(record-type, member-designator). For example, given: 1622/// @code 1623/// struct S { 1624/// float f; 1625/// double d; 1626/// }; 1627/// struct T { 1628/// int i; 1629/// struct S s[10]; 1630/// }; 1631/// @endcode 1632/// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). 1633 1634class OffsetOfExpr : public Expr { 1635public: 1636 // __builtin_offsetof(type, identifier(.identifier|[expr])*) 1637 class OffsetOfNode { 1638 public: 1639 /// \brief The kind of offsetof node we have. 1640 enum Kind { 1641 /// \brief An index into an array. 1642 Array = 0x00, 1643 /// \brief A field. 1644 Field = 0x01, 1645 /// \brief A field in a dependent type, known only by its name. 1646 Identifier = 0x02, 1647 /// \brief An implicit indirection through a C++ base class, when the 1648 /// field found is in a base class. 1649 Base = 0x03 1650 }; 1651 1652 private: 1653 enum { MaskBits = 2, Mask = 0x03 }; 1654 1655 /// \brief The source range that covers this part of the designator. 1656 SourceRange Range; 1657 1658 /// \brief The data describing the designator, which comes in three 1659 /// different forms, depending on the lower two bits. 1660 /// - An unsigned index into the array of Expr*'s stored after this node 1661 /// in memory, for [constant-expression] designators. 1662 /// - A FieldDecl*, for references to a known field. 1663 /// - An IdentifierInfo*, for references to a field with a given name 1664 /// when the class type is dependent. 1665 /// - A CXXBaseSpecifier*, for references that look at a field in a 1666 /// base class. 1667 uintptr_t Data; 1668 1669 public: 1670 /// \brief Create an offsetof node that refers to an array element. 1671 OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, 1672 SourceLocation RBracketLoc) 1673 : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { } 1674 1675 /// \brief Create an offsetof node that refers to a field. 1676 OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, 1677 SourceLocation NameLoc) 1678 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1679 Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { } 1680 1681 /// \brief Create an offsetof node that refers to an identifier. 1682 OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, 1683 SourceLocation NameLoc) 1684 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1685 Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { } 1686 1687 /// \brief Create an offsetof node that refers into a C++ base class. 1688 explicit OffsetOfNode(const CXXBaseSpecifier *Base) 1689 : Range(), Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} 1690 1691 /// \brief Determine what kind of offsetof node this is. 1692 Kind getKind() const { 1693 return static_cast<Kind>(Data & Mask); 1694 } 1695 1696 /// \brief For an array element node, returns the index into the array 1697 /// of expressions. 1698 unsigned getArrayExprIndex() const { 1699 assert(getKind() == Array); 1700 return Data >> 2; 1701 } 1702 1703 /// \brief For a field offsetof node, returns the field. 1704 FieldDecl *getField() const { 1705 assert(getKind() == Field); 1706 return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); 1707 } 1708 1709 /// \brief For a field or identifier offsetof node, returns the name of 1710 /// the field. 1711 IdentifierInfo *getFieldName() const; 1712 1713 /// \brief For a base class node, returns the base specifier. 1714 CXXBaseSpecifier *getBase() const { 1715 assert(getKind() == Base); 1716 return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); 1717 } 1718 1719 /// \brief Retrieve the source range that covers this offsetof node. 1720 /// 1721 /// For an array element node, the source range contains the locations of 1722 /// the square brackets. For a field or identifier node, the source range 1723 /// contains the location of the period (if there is one) and the 1724 /// identifier. 1725 SourceRange getSourceRange() const { return Range; } 1726 }; 1727 1728private: 1729 1730 SourceLocation OperatorLoc, RParenLoc; 1731 // Base type; 1732 TypeSourceInfo *TSInfo; 1733 // Number of sub-components (i.e. instances of OffsetOfNode). 1734 unsigned NumComps; 1735 // Number of sub-expressions (i.e. array subscript expressions). 1736 unsigned NumExprs; 1737 1738 OffsetOfExpr(ASTContext &C, QualType type, 1739 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1740 OffsetOfNode* compsPtr, unsigned numComps, 1741 Expr** exprsPtr, unsigned numExprs, 1742 SourceLocation RParenLoc); 1743 1744 explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) 1745 : Expr(OffsetOfExprClass, EmptyShell()), 1746 TSInfo(0), NumComps(numComps), NumExprs(numExprs) {} 1747 1748public: 1749 1750 static OffsetOfExpr *Create(ASTContext &C, QualType type, 1751 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1752 OffsetOfNode* compsPtr, unsigned numComps, 1753 Expr** exprsPtr, unsigned numExprs, 1754 SourceLocation RParenLoc); 1755 1756 static OffsetOfExpr *CreateEmpty(ASTContext &C, 1757 unsigned NumComps, unsigned NumExprs); 1758 1759 /// getOperatorLoc - Return the location of the operator. 1760 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1761 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1762 1763 /// \brief Return the location of the right parentheses. 1764 SourceLocation getRParenLoc() const { return RParenLoc; } 1765 void setRParenLoc(SourceLocation R) { RParenLoc = R; } 1766 1767 TypeSourceInfo *getTypeSourceInfo() const { 1768 return TSInfo; 1769 } 1770 void setTypeSourceInfo(TypeSourceInfo *tsi) { 1771 TSInfo = tsi; 1772 } 1773 1774 const OffsetOfNode &getComponent(unsigned Idx) const { 1775 assert(Idx < NumComps && "Subscript out of range"); 1776 return reinterpret_cast<const OffsetOfNode *> (this + 1)[Idx]; 1777 } 1778 1779 void setComponent(unsigned Idx, OffsetOfNode ON) { 1780 assert(Idx < NumComps && "Subscript out of range"); 1781 reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON; 1782 } 1783 1784 unsigned getNumComponents() const { 1785 return NumComps; 1786 } 1787 1788 Expr* getIndexExpr(unsigned Idx) { 1789 assert(Idx < NumExprs && "Subscript out of range"); 1790 return reinterpret_cast<Expr **>( 1791 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx]; 1792 } 1793 const Expr *getIndexExpr(unsigned Idx) const { 1794 return const_cast<OffsetOfExpr*>(this)->getIndexExpr(Idx); 1795 } 1796 1797 void setIndexExpr(unsigned Idx, Expr* E) { 1798 assert(Idx < NumComps && "Subscript out of range"); 1799 reinterpret_cast<Expr **>( 1800 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E; 1801 } 1802 1803 unsigned getNumExpressions() const { 1804 return NumExprs; 1805 } 1806 1807 SourceRange getSourceRange() const { 1808 return SourceRange(OperatorLoc, RParenLoc); 1809 } 1810 1811 static bool classof(const Stmt *T) { 1812 return T->getStmtClass() == OffsetOfExprClass; 1813 } 1814 1815 static bool classof(const OffsetOfExpr *) { return true; } 1816 1817 // Iterators 1818 child_range children() { 1819 Stmt **begin = 1820 reinterpret_cast<Stmt**>(reinterpret_cast<OffsetOfNode*>(this + 1) 1821 + NumComps); 1822 return child_range(begin, begin + NumExprs); 1823 } 1824}; 1825 1826/// UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) 1827/// expression operand. Used for sizeof/alignof (C99 6.5.3.4) and 1828/// vec_step (OpenCL 1.1 6.11.12). 1829class UnaryExprOrTypeTraitExpr : public Expr { 1830 unsigned Kind : 2; 1831 bool isType : 1; // true if operand is a type, false if an expression 1832 union { 1833 TypeSourceInfo *Ty; 1834 Stmt *Ex; 1835 } Argument; 1836 SourceLocation OpLoc, RParenLoc; 1837 1838public: 1839 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, TypeSourceInfo *TInfo, 1840 QualType resultType, SourceLocation op, 1841 SourceLocation rp) : 1842 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 1843 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1844 // Value-dependent if the argument is type-dependent. 1845 TInfo->getType()->isDependentType(), 1846 TInfo->getType()->isInstantiationDependentType(), 1847 TInfo->getType()->containsUnexpandedParameterPack()), 1848 Kind(ExprKind), isType(true), OpLoc(op), RParenLoc(rp) { 1849 Argument.Ty = TInfo; 1850 } 1851 1852 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, Expr *E, 1853 QualType resultType, SourceLocation op, 1854 SourceLocation rp) : 1855 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 1856 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1857 // Value-dependent if the argument is type-dependent. 1858 E->isTypeDependent(), 1859 E->isInstantiationDependent(), 1860 E->containsUnexpandedParameterPack()), 1861 Kind(ExprKind), isType(false), OpLoc(op), RParenLoc(rp) { 1862 Argument.Ex = E; 1863 } 1864 1865 /// \brief Construct an empty sizeof/alignof expression. 1866 explicit UnaryExprOrTypeTraitExpr(EmptyShell Empty) 1867 : Expr(UnaryExprOrTypeTraitExprClass, Empty) { } 1868 1869 UnaryExprOrTypeTrait getKind() const { 1870 return static_cast<UnaryExprOrTypeTrait>(Kind); 1871 } 1872 void setKind(UnaryExprOrTypeTrait K) { Kind = K; } 1873 1874 bool isArgumentType() const { return isType; } 1875 QualType getArgumentType() const { 1876 return getArgumentTypeInfo()->getType(); 1877 } 1878 TypeSourceInfo *getArgumentTypeInfo() const { 1879 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 1880 return Argument.Ty; 1881 } 1882 Expr *getArgumentExpr() { 1883 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 1884 return static_cast<Expr*>(Argument.Ex); 1885 } 1886 const Expr *getArgumentExpr() const { 1887 return const_cast<UnaryExprOrTypeTraitExpr*>(this)->getArgumentExpr(); 1888 } 1889 1890 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 1891 void setArgument(TypeSourceInfo *TInfo) { 1892 Argument.Ty = TInfo; 1893 isType = true; 1894 } 1895 1896 /// Gets the argument type, or the type of the argument expression, whichever 1897 /// is appropriate. 1898 QualType getTypeOfArgument() const { 1899 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 1900 } 1901 1902 SourceLocation getOperatorLoc() const { return OpLoc; } 1903 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1904 1905 SourceLocation getRParenLoc() const { return RParenLoc; } 1906 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1907 1908 SourceRange getSourceRange() const { 1909 return SourceRange(OpLoc, RParenLoc); 1910 } 1911 1912 static bool classof(const Stmt *T) { 1913 return T->getStmtClass() == UnaryExprOrTypeTraitExprClass; 1914 } 1915 static bool classof(const UnaryExprOrTypeTraitExpr *) { return true; } 1916 1917 // Iterators 1918 child_range children(); 1919}; 1920 1921//===----------------------------------------------------------------------===// 1922// Postfix Operators. 1923//===----------------------------------------------------------------------===// 1924 1925/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 1926class ArraySubscriptExpr : public Expr { 1927 enum { LHS, RHS, END_EXPR=2 }; 1928 Stmt* SubExprs[END_EXPR]; 1929 SourceLocation RBracketLoc; 1930public: 1931 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 1932 ExprValueKind VK, ExprObjectKind OK, 1933 SourceLocation rbracketloc) 1934 : Expr(ArraySubscriptExprClass, t, VK, OK, 1935 lhs->isTypeDependent() || rhs->isTypeDependent(), 1936 lhs->isValueDependent() || rhs->isValueDependent(), 1937 (lhs->isInstantiationDependent() || 1938 rhs->isInstantiationDependent()), 1939 (lhs->containsUnexpandedParameterPack() || 1940 rhs->containsUnexpandedParameterPack())), 1941 RBracketLoc(rbracketloc) { 1942 SubExprs[LHS] = lhs; 1943 SubExprs[RHS] = rhs; 1944 } 1945 1946 /// \brief Create an empty array subscript expression. 1947 explicit ArraySubscriptExpr(EmptyShell Shell) 1948 : Expr(ArraySubscriptExprClass, Shell) { } 1949 1950 /// An array access can be written A[4] or 4[A] (both are equivalent). 1951 /// - getBase() and getIdx() always present the normalized view: A[4]. 1952 /// In this case getBase() returns "A" and getIdx() returns "4". 1953 /// - getLHS() and getRHS() present the syntactic view. e.g. for 1954 /// 4[A] getLHS() returns "4". 1955 /// Note: Because vector element access is also written A[4] we must 1956 /// predicate the format conversion in getBase and getIdx only on the 1957 /// the type of the RHS, as it is possible for the LHS to be a vector of 1958 /// integer type 1959 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 1960 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1961 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1962 1963 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 1964 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1965 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1966 1967 Expr *getBase() { 1968 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1969 } 1970 1971 const Expr *getBase() const { 1972 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1973 } 1974 1975 Expr *getIdx() { 1976 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1977 } 1978 1979 const Expr *getIdx() const { 1980 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1981 } 1982 1983 SourceRange getSourceRange() const { 1984 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 1985 } 1986 1987 SourceLocation getRBracketLoc() const { return RBracketLoc; } 1988 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 1989 1990 SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 1991 1992 static bool classof(const Stmt *T) { 1993 return T->getStmtClass() == ArraySubscriptExprClass; 1994 } 1995 static bool classof(const ArraySubscriptExpr *) { return true; } 1996 1997 // Iterators 1998 child_range children() { 1999 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2000 } 2001}; 2002 2003 2004/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 2005/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 2006/// while its subclasses may represent alternative syntax that (semantically) 2007/// results in a function call. For example, CXXOperatorCallExpr is 2008/// a subclass for overloaded operator calls that use operator syntax, e.g., 2009/// "str1 + str2" to resolve to a function call. 2010class CallExpr : public Expr { 2011 enum { FN=0, PREARGS_START=1 }; 2012 Stmt **SubExprs; 2013 unsigned NumArgs; 2014 SourceLocation RParenLoc; 2015 2016protected: 2017 // These versions of the constructor are for derived classes. 2018 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs, 2019 Expr **args, unsigned numargs, QualType t, ExprValueKind VK, 2020 SourceLocation rparenloc); 2021 CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs, EmptyShell Empty); 2022 2023 Stmt *getPreArg(unsigned i) { 2024 assert(i < getNumPreArgs() && "Prearg access out of range!"); 2025 return SubExprs[PREARGS_START+i]; 2026 } 2027 const Stmt *getPreArg(unsigned i) const { 2028 assert(i < getNumPreArgs() && "Prearg access out of range!"); 2029 return SubExprs[PREARGS_START+i]; 2030 } 2031 void setPreArg(unsigned i, Stmt *PreArg) { 2032 assert(i < getNumPreArgs() && "Prearg access out of range!"); 2033 SubExprs[PREARGS_START+i] = PreArg; 2034 } 2035 2036 unsigned getNumPreArgs() const { return CallExprBits.NumPreArgs; } 2037 2038public: 2039 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 2040 ExprValueKind VK, SourceLocation rparenloc); 2041 2042 /// \brief Build an empty call expression. 2043 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 2044 2045 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 2046 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 2047 void setCallee(Expr *F) { SubExprs[FN] = F; } 2048 2049 Decl *getCalleeDecl(); 2050 const Decl *getCalleeDecl() const { 2051 return const_cast<CallExpr*>(this)->getCalleeDecl(); 2052 } 2053 2054 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 2055 FunctionDecl *getDirectCallee(); 2056 const FunctionDecl *getDirectCallee() const { 2057 return const_cast<CallExpr*>(this)->getDirectCallee(); 2058 } 2059 2060 /// getNumArgs - Return the number of actual arguments to this call. 2061 /// 2062 unsigned getNumArgs() const { return NumArgs; } 2063 2064 /// \brief Retrieve the call arguments. 2065 Expr **getArgs() { 2066 return reinterpret_cast<Expr **>(SubExprs+getNumPreArgs()+PREARGS_START); 2067 } 2068 const Expr *const *getArgs() const { 2069 return const_cast<CallExpr*>(this)->getArgs(); 2070 } 2071 2072 /// getArg - Return the specified argument. 2073 Expr *getArg(unsigned Arg) { 2074 assert(Arg < NumArgs && "Arg access out of range!"); 2075 return cast<Expr>(SubExprs[Arg+getNumPreArgs()+PREARGS_START]); 2076 } 2077 const Expr *getArg(unsigned Arg) const { 2078 assert(Arg < NumArgs && "Arg access out of range!"); 2079 return cast<Expr>(SubExprs[Arg+getNumPreArgs()+PREARGS_START]); 2080 } 2081 2082 /// setArg - Set the specified argument. 2083 void setArg(unsigned Arg, Expr *ArgExpr) { 2084 assert(Arg < NumArgs && "Arg access out of range!"); 2085 SubExprs[Arg+getNumPreArgs()+PREARGS_START] = ArgExpr; 2086 } 2087 2088 /// setNumArgs - This changes the number of arguments present in this call. 2089 /// Any orphaned expressions are deleted by this, and any new operands are set 2090 /// to null. 2091 void setNumArgs(ASTContext& C, unsigned NumArgs); 2092 2093 typedef ExprIterator arg_iterator; 2094 typedef ConstExprIterator const_arg_iterator; 2095 2096 arg_iterator arg_begin() { return SubExprs+PREARGS_START+getNumPreArgs(); } 2097 arg_iterator arg_end() { 2098 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 2099 } 2100 const_arg_iterator arg_begin() const { 2101 return SubExprs+PREARGS_START+getNumPreArgs(); 2102 } 2103 const_arg_iterator arg_end() const { 2104 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 2105 } 2106 2107 /// getNumCommas - Return the number of commas that must have been present in 2108 /// this function call. 2109 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 2110 2111 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 2112 /// not, return 0. 2113 unsigned isBuiltinCall() const; 2114 2115 /// getCallReturnType - Get the return type of the call expr. This is not 2116 /// always the type of the expr itself, if the return type is a reference 2117 /// type. 2118 QualType getCallReturnType() const; 2119 2120 SourceLocation getRParenLoc() const { return RParenLoc; } 2121 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2122 2123 SourceRange getSourceRange() const; 2124 2125 static bool classof(const Stmt *T) { 2126 return T->getStmtClass() >= firstCallExprConstant && 2127 T->getStmtClass() <= lastCallExprConstant; 2128 } 2129 static bool classof(const CallExpr *) { return true; } 2130 2131 // Iterators 2132 child_range children() { 2133 return child_range(&SubExprs[0], 2134 &SubExprs[0]+NumArgs+getNumPreArgs()+PREARGS_START); 2135 } 2136}; 2137 2138/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 2139/// 2140class MemberExpr : public Expr { 2141 /// Extra data stored in some member expressions. 2142 struct MemberNameQualifier { 2143 /// \brief The nested-name-specifier that qualifies the name, including 2144 /// source-location information. 2145 NestedNameSpecifierLoc QualifierLoc; 2146 2147 /// \brief The DeclAccessPair through which the MemberDecl was found due to 2148 /// name qualifiers. 2149 DeclAccessPair FoundDecl; 2150 }; 2151 2152 /// Base - the expression for the base pointer or structure references. In 2153 /// X.F, this is "X". 2154 Stmt *Base; 2155 2156 /// MemberDecl - This is the decl being referenced by the field/member name. 2157 /// In X.F, this is the decl referenced by F. 2158 ValueDecl *MemberDecl; 2159 2160 /// MemberDNLoc - Provides source/type location info for the 2161 /// declaration name embedded in MemberDecl. 2162 DeclarationNameLoc MemberDNLoc; 2163 2164 /// MemberLoc - This is the location of the member name. 2165 SourceLocation MemberLoc; 2166 2167 /// IsArrow - True if this is "X->F", false if this is "X.F". 2168 bool IsArrow : 1; 2169 2170 /// \brief True if this member expression used a nested-name-specifier to 2171 /// refer to the member, e.g., "x->Base::f", or found its member via a using 2172 /// declaration. When true, a MemberNameQualifier 2173 /// structure is allocated immediately after the MemberExpr. 2174 bool HasQualifierOrFoundDecl : 1; 2175 2176 /// \brief True if this member expression specified a template keyword 2177 /// and/or a template argument list explicitly, e.g., x->f<int>, 2178 /// x->template f, x->template f<int>. 2179 /// When true, an ASTTemplateKWAndArgsInfo structure and its 2180 /// TemplateArguments (if any) are allocated immediately after 2181 /// the MemberExpr or, if the member expression also has a qualifier, 2182 /// after the MemberNameQualifier structure. 2183 bool HasTemplateKWAndArgsInfo : 1; 2184 2185 /// \brief True if this member expression refers to a method that 2186 /// was resolved from an overloaded set having size greater than 1. 2187 bool HadMultipleCandidates : 1; 2188 2189 /// \brief Retrieve the qualifier that preceded the member name, if any. 2190 MemberNameQualifier *getMemberQualifier() { 2191 assert(HasQualifierOrFoundDecl); 2192 return reinterpret_cast<MemberNameQualifier *> (this + 1); 2193 } 2194 2195 /// \brief Retrieve the qualifier that preceded the member name, if any. 2196 const MemberNameQualifier *getMemberQualifier() const { 2197 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 2198 } 2199 2200public: 2201 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 2202 const DeclarationNameInfo &NameInfo, QualType ty, 2203 ExprValueKind VK, ExprObjectKind OK) 2204 : Expr(MemberExprClass, ty, VK, OK, 2205 base->isTypeDependent(), 2206 base->isValueDependent(), 2207 base->isInstantiationDependent(), 2208 base->containsUnexpandedParameterPack()), 2209 Base(base), MemberDecl(memberdecl), MemberDNLoc(NameInfo.getInfo()), 2210 MemberLoc(NameInfo.getLoc()), IsArrow(isarrow), 2211 HasQualifierOrFoundDecl(false), HasTemplateKWAndArgsInfo(false), 2212 HadMultipleCandidates(false) { 2213 assert(memberdecl->getDeclName() == NameInfo.getName()); 2214 } 2215 2216 // NOTE: this constructor should be used only when it is known that 2217 // the member name can not provide additional syntactic info 2218 // (i.e., source locations for C++ operator names or type source info 2219 // for constructors, destructors and conversion operators). 2220 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 2221 SourceLocation l, QualType ty, 2222 ExprValueKind VK, ExprObjectKind OK) 2223 : Expr(MemberExprClass, ty, VK, OK, 2224 base->isTypeDependent(), base->isValueDependent(), 2225 base->isInstantiationDependent(), 2226 base->containsUnexpandedParameterPack()), 2227 Base(base), MemberDecl(memberdecl), MemberDNLoc(), MemberLoc(l), 2228 IsArrow(isarrow), 2229 HasQualifierOrFoundDecl(false), HasTemplateKWAndArgsInfo(false), 2230 HadMultipleCandidates(false) {} 2231 2232 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 2233 NestedNameSpecifierLoc QualifierLoc, 2234 SourceLocation TemplateKWLoc, 2235 ValueDecl *memberdecl, DeclAccessPair founddecl, 2236 DeclarationNameInfo MemberNameInfo, 2237 const TemplateArgumentListInfo *targs, 2238 QualType ty, ExprValueKind VK, ExprObjectKind OK); 2239 2240 void setBase(Expr *E) { Base = E; } 2241 Expr *getBase() const { return cast<Expr>(Base); } 2242 2243 /// \brief Retrieve the member declaration to which this expression refers. 2244 /// 2245 /// The returned declaration will either be a FieldDecl or (in C++) 2246 /// a CXXMethodDecl. 2247 ValueDecl *getMemberDecl() const { return MemberDecl; } 2248 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 2249 2250 /// \brief Retrieves the declaration found by lookup. 2251 DeclAccessPair getFoundDecl() const { 2252 if (!HasQualifierOrFoundDecl) 2253 return DeclAccessPair::make(getMemberDecl(), 2254 getMemberDecl()->getAccess()); 2255 return getMemberQualifier()->FoundDecl; 2256 } 2257 2258 /// \brief Determines whether this member expression actually had 2259 /// a C++ nested-name-specifier prior to the name of the member, e.g., 2260 /// x->Base::foo. 2261 bool hasQualifier() const { return getQualifier() != 0; } 2262 2263 /// \brief If the member name was qualified, retrieves the 2264 /// nested-name-specifier that precedes the member name. Otherwise, returns 2265 /// NULL. 2266 NestedNameSpecifier *getQualifier() const { 2267 if (!HasQualifierOrFoundDecl) 2268 return 0; 2269 2270 return getMemberQualifier()->QualifierLoc.getNestedNameSpecifier(); 2271 } 2272 2273 /// \brief If the member name was qualified, retrieves the 2274 /// nested-name-specifier that precedes the member name, with source-location 2275 /// information. 2276 NestedNameSpecifierLoc getQualifierLoc() const { 2277 if (!hasQualifier()) 2278 return NestedNameSpecifierLoc(); 2279 2280 return getMemberQualifier()->QualifierLoc; 2281 } 2282 2283 /// \brief Return the optional template keyword and arguments info. 2284 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2285 if (!HasTemplateKWAndArgsInfo) 2286 return 0; 2287 2288 if (!HasQualifierOrFoundDecl) 2289 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>(this + 1); 2290 2291 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>( 2292 getMemberQualifier() + 1); 2293 } 2294 2295 /// \brief Return the optional template keyword and arguments info. 2296 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2297 return const_cast<MemberExpr*>(this)->getTemplateKWAndArgsInfo(); 2298 } 2299 2300 /// \brief Retrieve the location of the template keyword preceding 2301 /// the member name, if any. 2302 SourceLocation getTemplateKeywordLoc() const { 2303 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2304 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2305 } 2306 2307 /// \brief Retrieve the location of the left angle bracket starting the 2308 /// explicit template argument list following the member name, if any. 2309 SourceLocation getLAngleLoc() const { 2310 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2311 return getTemplateKWAndArgsInfo()->LAngleLoc; 2312 } 2313 2314 /// \brief Retrieve the location of the right angle bracket ending the 2315 /// explicit template argument list following the member name, if any. 2316 SourceLocation getRAngleLoc() const { 2317 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2318 return getTemplateKWAndArgsInfo()->RAngleLoc; 2319 } 2320 2321 /// Determines whether the member name was preceded by the template keyword. 2322 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2323 2324 /// \brief Determines whether the member name was followed by an 2325 /// explicit template argument list. 2326 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2327 2328 /// \brief Copies the template arguments (if present) into the given 2329 /// structure. 2330 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2331 if (hasExplicitTemplateArgs()) 2332 getExplicitTemplateArgs().copyInto(List); 2333 } 2334 2335 /// \brief Retrieve the explicit template argument list that 2336 /// follow the member template name. This must only be called on an 2337 /// expression with explicit template arguments. 2338 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2339 assert(hasExplicitTemplateArgs()); 2340 return *getTemplateKWAndArgsInfo(); 2341 } 2342 2343 /// \brief Retrieve the explicit template argument list that 2344 /// followed the member template name. This must only be called on 2345 /// an expression with explicit template arguments. 2346 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2347 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgs(); 2348 } 2349 2350 /// \brief Retrieves the optional explicit template arguments. 2351 /// This points to the same data as getExplicitTemplateArgs(), but 2352 /// returns null if there are no explicit template arguments. 2353 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 2354 if (!hasExplicitTemplateArgs()) return 0; 2355 return &getExplicitTemplateArgs(); 2356 } 2357 2358 /// \brief Retrieve the template arguments provided as part of this 2359 /// template-id. 2360 const TemplateArgumentLoc *getTemplateArgs() const { 2361 if (!hasExplicitTemplateArgs()) 2362 return 0; 2363 2364 return getExplicitTemplateArgs().getTemplateArgs(); 2365 } 2366 2367 /// \brief Retrieve the number of template arguments provided as part of this 2368 /// template-id. 2369 unsigned getNumTemplateArgs() const { 2370 if (!hasExplicitTemplateArgs()) 2371 return 0; 2372 2373 return getExplicitTemplateArgs().NumTemplateArgs; 2374 } 2375 2376 /// \brief Retrieve the member declaration name info. 2377 DeclarationNameInfo getMemberNameInfo() const { 2378 return DeclarationNameInfo(MemberDecl->getDeclName(), 2379 MemberLoc, MemberDNLoc); 2380 } 2381 2382 bool isArrow() const { return IsArrow; } 2383 void setArrow(bool A) { IsArrow = A; } 2384 2385 /// getMemberLoc - Return the location of the "member", in X->F, it is the 2386 /// location of 'F'. 2387 SourceLocation getMemberLoc() const { return MemberLoc; } 2388 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 2389 2390 SourceRange getSourceRange() const; 2391 2392 SourceLocation getExprLoc() const { return MemberLoc; } 2393 2394 /// \brief Determine whether the base of this explicit is implicit. 2395 bool isImplicitAccess() const { 2396 return getBase() && getBase()->isImplicitCXXThis(); 2397 } 2398 2399 /// \brief Returns true if this member expression refers to a method that 2400 /// was resolved from an overloaded set having size greater than 1. 2401 bool hadMultipleCandidates() const { 2402 return HadMultipleCandidates; 2403 } 2404 /// \brief Sets the flag telling whether this expression refers to 2405 /// a method that was resolved from an overloaded set having size 2406 /// greater than 1. 2407 void setHadMultipleCandidates(bool V = true) { 2408 HadMultipleCandidates = V; 2409 } 2410 2411 static bool classof(const Stmt *T) { 2412 return T->getStmtClass() == MemberExprClass; 2413 } 2414 static bool classof(const MemberExpr *) { return true; } 2415 2416 // Iterators 2417 child_range children() { return child_range(&Base, &Base+1); } 2418 2419 friend class ASTReader; 2420 friend class ASTStmtWriter; 2421}; 2422 2423/// CompoundLiteralExpr - [C99 6.5.2.5] 2424/// 2425class CompoundLiteralExpr : public Expr { 2426 /// LParenLoc - If non-null, this is the location of the left paren in a 2427 /// compound literal like "(int){4}". This can be null if this is a 2428 /// synthesized compound expression. 2429 SourceLocation LParenLoc; 2430 2431 /// The type as written. This can be an incomplete array type, in 2432 /// which case the actual expression type will be different. 2433 /// The int part of the pair stores whether this expr is file scope. 2434 llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfoAndScope; 2435 Stmt *Init; 2436public: 2437 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, 2438 QualType T, ExprValueKind VK, Expr *init, bool fileScope) 2439 : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary, 2440 tinfo->getType()->isDependentType(), 2441 init->isValueDependent(), 2442 (init->isInstantiationDependent() || 2443 tinfo->getType()->isInstantiationDependentType()), 2444 init->containsUnexpandedParameterPack()), 2445 LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) {} 2446 2447 /// \brief Construct an empty compound literal. 2448 explicit CompoundLiteralExpr(EmptyShell Empty) 2449 : Expr(CompoundLiteralExprClass, Empty) { } 2450 2451 const Expr *getInitializer() const { return cast<Expr>(Init); } 2452 Expr *getInitializer() { return cast<Expr>(Init); } 2453 void setInitializer(Expr *E) { Init = E; } 2454 2455 bool isFileScope() const { return TInfoAndScope.getInt(); } 2456 void setFileScope(bool FS) { TInfoAndScope.setInt(FS); } 2457 2458 SourceLocation getLParenLoc() const { return LParenLoc; } 2459 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2460 2461 TypeSourceInfo *getTypeSourceInfo() const { 2462 return TInfoAndScope.getPointer(); 2463 } 2464 void setTypeSourceInfo(TypeSourceInfo *tinfo) { 2465 TInfoAndScope.setPointer(tinfo); 2466 } 2467 2468 SourceRange getSourceRange() const { 2469 // FIXME: Init should never be null. 2470 if (!Init) 2471 return SourceRange(); 2472 if (LParenLoc.isInvalid()) 2473 return Init->getSourceRange(); 2474 return SourceRange(LParenLoc, Init->getLocEnd()); 2475 } 2476 2477 static bool classof(const Stmt *T) { 2478 return T->getStmtClass() == CompoundLiteralExprClass; 2479 } 2480 static bool classof(const CompoundLiteralExpr *) { return true; } 2481 2482 // Iterators 2483 child_range children() { return child_range(&Init, &Init+1); } 2484}; 2485 2486/// CastExpr - Base class for type casts, including both implicit 2487/// casts (ImplicitCastExpr) and explicit casts that have some 2488/// representation in the source code (ExplicitCastExpr's derived 2489/// classes). 2490class CastExpr : public Expr { 2491public: 2492 typedef clang::CastKind CastKind; 2493 2494private: 2495 Stmt *Op; 2496 2497 void CheckCastConsistency() const; 2498 2499 const CXXBaseSpecifier * const *path_buffer() const { 2500 return const_cast<CastExpr*>(this)->path_buffer(); 2501 } 2502 CXXBaseSpecifier **path_buffer(); 2503 2504 void setBasePathSize(unsigned basePathSize) { 2505 CastExprBits.BasePathSize = basePathSize; 2506 assert(CastExprBits.BasePathSize == basePathSize && 2507 "basePathSize doesn't fit in bits of CastExprBits.BasePathSize!"); 2508 } 2509 2510protected: 2511 CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 2512 const CastKind kind, Expr *op, unsigned BasePathSize) : 2513 Expr(SC, ty, VK, OK_Ordinary, 2514 // Cast expressions are type-dependent if the type is 2515 // dependent (C++ [temp.dep.expr]p3). 2516 ty->isDependentType(), 2517 // Cast expressions are value-dependent if the type is 2518 // dependent or if the subexpression is value-dependent. 2519 ty->isDependentType() || (op && op->isValueDependent()), 2520 (ty->isInstantiationDependentType() || 2521 (op && op->isInstantiationDependent())), 2522 (ty->containsUnexpandedParameterPack() || 2523 op->containsUnexpandedParameterPack())), 2524 Op(op) { 2525 assert(kind != CK_Invalid && "creating cast with invalid cast kind"); 2526 CastExprBits.Kind = kind; 2527 setBasePathSize(BasePathSize); 2528#ifndef NDEBUG 2529 CheckCastConsistency(); 2530#endif 2531 } 2532 2533 /// \brief Construct an empty cast. 2534 CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize) 2535 : Expr(SC, Empty) { 2536 setBasePathSize(BasePathSize); 2537 } 2538 2539public: 2540 CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } 2541 void setCastKind(CastKind K) { CastExprBits.Kind = K; } 2542 const char *getCastKindName() const; 2543 2544 Expr *getSubExpr() { return cast<Expr>(Op); } 2545 const Expr *getSubExpr() const { return cast<Expr>(Op); } 2546 void setSubExpr(Expr *E) { Op = E; } 2547 2548 /// \brief Retrieve the cast subexpression as it was written in the source 2549 /// code, looking through any implicit casts or other intermediate nodes 2550 /// introduced by semantic analysis. 2551 Expr *getSubExprAsWritten(); 2552 const Expr *getSubExprAsWritten() const { 2553 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 2554 } 2555 2556 typedef CXXBaseSpecifier **path_iterator; 2557 typedef const CXXBaseSpecifier * const *path_const_iterator; 2558 bool path_empty() const { return CastExprBits.BasePathSize == 0; } 2559 unsigned path_size() const { return CastExprBits.BasePathSize; } 2560 path_iterator path_begin() { return path_buffer(); } 2561 path_iterator path_end() { return path_buffer() + path_size(); } 2562 path_const_iterator path_begin() const { return path_buffer(); } 2563 path_const_iterator path_end() const { return path_buffer() + path_size(); } 2564 2565 void setCastPath(const CXXCastPath &Path); 2566 2567 static bool classof(const Stmt *T) { 2568 return T->getStmtClass() >= firstCastExprConstant && 2569 T->getStmtClass() <= lastCastExprConstant; 2570 } 2571 static bool classof(const CastExpr *) { return true; } 2572 2573 // Iterators 2574 child_range children() { return child_range(&Op, &Op+1); } 2575}; 2576 2577/// ImplicitCastExpr - Allows us to explicitly represent implicit type 2578/// conversions, which have no direct representation in the original 2579/// source code. For example: converting T[]->T*, void f()->void 2580/// (*f)(), float->double, short->int, etc. 2581/// 2582/// In C, implicit casts always produce rvalues. However, in C++, an 2583/// implicit cast whose result is being bound to a reference will be 2584/// an lvalue or xvalue. For example: 2585/// 2586/// @code 2587/// class Base { }; 2588/// class Derived : public Base { }; 2589/// Derived &&ref(); 2590/// void f(Derived d) { 2591/// Base& b = d; // initializer is an ImplicitCastExpr 2592/// // to an lvalue of type Base 2593/// Base&& r = ref(); // initializer is an ImplicitCastExpr 2594/// // to an xvalue of type Base 2595/// } 2596/// @endcode 2597class ImplicitCastExpr : public CastExpr { 2598private: 2599 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 2600 unsigned BasePathLength, ExprValueKind VK) 2601 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength) { 2602 } 2603 2604 /// \brief Construct an empty implicit cast. 2605 explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize) 2606 : CastExpr(ImplicitCastExprClass, Shell, PathSize) { } 2607 2608public: 2609 enum OnStack_t { OnStack }; 2610 ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, 2611 ExprValueKind VK) 2612 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0) { 2613 } 2614 2615 static ImplicitCastExpr *Create(ASTContext &Context, QualType T, 2616 CastKind Kind, Expr *Operand, 2617 const CXXCastPath *BasePath, 2618 ExprValueKind Cat); 2619 2620 static ImplicitCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2621 2622 SourceRange getSourceRange() const { 2623 return getSubExpr()->getSourceRange(); 2624 } 2625 2626 static bool classof(const Stmt *T) { 2627 return T->getStmtClass() == ImplicitCastExprClass; 2628 } 2629 static bool classof(const ImplicitCastExpr *) { return true; } 2630}; 2631 2632inline Expr *Expr::IgnoreImpCasts() { 2633 Expr *e = this; 2634 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) 2635 e = ice->getSubExpr(); 2636 return e; 2637} 2638 2639/// ExplicitCastExpr - An explicit cast written in the source 2640/// code. 2641/// 2642/// This class is effectively an abstract class, because it provides 2643/// the basic representation of an explicitly-written cast without 2644/// specifying which kind of cast (C cast, functional cast, static 2645/// cast, etc.) was written; specific derived classes represent the 2646/// particular style of cast and its location information. 2647/// 2648/// Unlike implicit casts, explicit cast nodes have two different 2649/// types: the type that was written into the source code, and the 2650/// actual type of the expression as determined by semantic 2651/// analysis. These types may differ slightly. For example, in C++ one 2652/// can cast to a reference type, which indicates that the resulting 2653/// expression will be an lvalue or xvalue. The reference type, however, 2654/// will not be used as the type of the expression. 2655class ExplicitCastExpr : public CastExpr { 2656 /// TInfo - Source type info for the (written) type 2657 /// this expression is casting to. 2658 TypeSourceInfo *TInfo; 2659 2660protected: 2661 ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, 2662 CastKind kind, Expr *op, unsigned PathSize, 2663 TypeSourceInfo *writtenTy) 2664 : CastExpr(SC, exprTy, VK, kind, op, PathSize), TInfo(writtenTy) {} 2665 2666 /// \brief Construct an empty explicit cast. 2667 ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 2668 : CastExpr(SC, Shell, PathSize) { } 2669 2670public: 2671 /// getTypeInfoAsWritten - Returns the type source info for the type 2672 /// that this expression is casting to. 2673 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 2674 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 2675 2676 /// getTypeAsWritten - Returns the type that this expression is 2677 /// casting to, as written in the source code. 2678 QualType getTypeAsWritten() const { return TInfo->getType(); } 2679 2680 static bool classof(const Stmt *T) { 2681 return T->getStmtClass() >= firstExplicitCastExprConstant && 2682 T->getStmtClass() <= lastExplicitCastExprConstant; 2683 } 2684 static bool classof(const ExplicitCastExpr *) { return true; } 2685}; 2686 2687/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 2688/// cast in C++ (C++ [expr.cast]), which uses the syntax 2689/// (Type)expr. For example: @c (int)f. 2690class CStyleCastExpr : public ExplicitCastExpr { 2691 SourceLocation LPLoc; // the location of the left paren 2692 SourceLocation RPLoc; // the location of the right paren 2693 2694 CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, 2695 unsigned PathSize, TypeSourceInfo *writtenTy, 2696 SourceLocation l, SourceLocation r) 2697 : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, 2698 writtenTy), LPLoc(l), RPLoc(r) {} 2699 2700 /// \brief Construct an empty C-style explicit cast. 2701 explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize) 2702 : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { } 2703 2704public: 2705 static CStyleCastExpr *Create(ASTContext &Context, QualType T, 2706 ExprValueKind VK, CastKind K, 2707 Expr *Op, const CXXCastPath *BasePath, 2708 TypeSourceInfo *WrittenTy, SourceLocation L, 2709 SourceLocation R); 2710 2711 static CStyleCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2712 2713 SourceLocation getLParenLoc() const { return LPLoc; } 2714 void setLParenLoc(SourceLocation L) { LPLoc = L; } 2715 2716 SourceLocation getRParenLoc() const { return RPLoc; } 2717 void setRParenLoc(SourceLocation L) { RPLoc = L; } 2718 2719 SourceRange getSourceRange() const { 2720 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 2721 } 2722 static bool classof(const Stmt *T) { 2723 return T->getStmtClass() == CStyleCastExprClass; 2724 } 2725 static bool classof(const CStyleCastExpr *) { return true; } 2726}; 2727 2728/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 2729/// 2730/// This expression node kind describes a builtin binary operation, 2731/// such as "x + y" for integer values "x" and "y". The operands will 2732/// already have been converted to appropriate types (e.g., by 2733/// performing promotions or conversions). 2734/// 2735/// In C++, where operators may be overloaded, a different kind of 2736/// expression node (CXXOperatorCallExpr) is used to express the 2737/// invocation of an overloaded operator with operator syntax. Within 2738/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 2739/// used to store an expression "x + y" depends on the subexpressions 2740/// for x and y. If neither x or y is type-dependent, and the "+" 2741/// operator resolves to a built-in operation, BinaryOperator will be 2742/// used to express the computation (x and y may still be 2743/// value-dependent). If either x or y is type-dependent, or if the 2744/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 2745/// be used to express the computation. 2746class BinaryOperator : public Expr { 2747public: 2748 typedef BinaryOperatorKind Opcode; 2749 2750private: 2751 unsigned Opc : 6; 2752 SourceLocation OpLoc; 2753 2754 enum { LHS, RHS, END_EXPR }; 2755 Stmt* SubExprs[END_EXPR]; 2756public: 2757 2758 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2759 ExprValueKind VK, ExprObjectKind OK, 2760 SourceLocation opLoc) 2761 : Expr(BinaryOperatorClass, ResTy, VK, OK, 2762 lhs->isTypeDependent() || rhs->isTypeDependent(), 2763 lhs->isValueDependent() || rhs->isValueDependent(), 2764 (lhs->isInstantiationDependent() || 2765 rhs->isInstantiationDependent()), 2766 (lhs->containsUnexpandedParameterPack() || 2767 rhs->containsUnexpandedParameterPack())), 2768 Opc(opc), OpLoc(opLoc) { 2769 SubExprs[LHS] = lhs; 2770 SubExprs[RHS] = rhs; 2771 assert(!isCompoundAssignmentOp() && 2772 "Use ArithAssignBinaryOperator for compound assignments"); 2773 } 2774 2775 /// \brief Construct an empty binary operator. 2776 explicit BinaryOperator(EmptyShell Empty) 2777 : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { } 2778 2779 SourceLocation getExprLoc() const { return OpLoc; } 2780 SourceLocation getOperatorLoc() const { return OpLoc; } 2781 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 2782 2783 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 2784 void setOpcode(Opcode O) { Opc = O; } 2785 2786 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2787 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2788 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2789 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2790 2791 SourceRange getSourceRange() const { 2792 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 2793 } 2794 2795 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 2796 /// corresponds to, e.g. "<<=". 2797 static const char *getOpcodeStr(Opcode Op); 2798 2799 const char *getOpcodeStr() const { return getOpcodeStr(getOpcode()); } 2800 2801 /// \brief Retrieve the binary opcode that corresponds to the given 2802 /// overloaded operator. 2803 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 2804 2805 /// \brief Retrieve the overloaded operator kind that corresponds to 2806 /// the given binary opcode. 2807 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 2808 2809 /// predicates to categorize the respective opcodes. 2810 bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; } 2811 bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; } 2812 static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } 2813 bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } 2814 static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } 2815 bool isShiftOp() const { return isShiftOp(getOpcode()); } 2816 2817 static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } 2818 bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } 2819 2820 static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } 2821 bool isRelationalOp() const { return isRelationalOp(getOpcode()); } 2822 2823 static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } 2824 bool isEqualityOp() const { return isEqualityOp(getOpcode()); } 2825 2826 static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; } 2827 bool isComparisonOp() const { return isComparisonOp(getOpcode()); } 2828 2829 static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } 2830 bool isLogicalOp() const { return isLogicalOp(getOpcode()); } 2831 2832 static bool isAssignmentOp(Opcode Opc) { 2833 return Opc >= BO_Assign && Opc <= BO_OrAssign; 2834 } 2835 bool isAssignmentOp() const { return isAssignmentOp(getOpcode()); } 2836 2837 static bool isCompoundAssignmentOp(Opcode Opc) { 2838 return Opc > BO_Assign && Opc <= BO_OrAssign; 2839 } 2840 bool isCompoundAssignmentOp() const { 2841 return isCompoundAssignmentOp(getOpcode()); 2842 } 2843 static Opcode getOpForCompoundAssignment(Opcode Opc) { 2844 assert(isCompoundAssignmentOp(Opc)); 2845 if (Opc >= BO_AndAssign) 2846 return Opcode(unsigned(Opc) - BO_AndAssign + BO_And); 2847 else 2848 return Opcode(unsigned(Opc) - BO_MulAssign + BO_Mul); 2849 } 2850 2851 static bool isShiftAssignOp(Opcode Opc) { 2852 return Opc == BO_ShlAssign || Opc == BO_ShrAssign; 2853 } 2854 bool isShiftAssignOp() const { 2855 return isShiftAssignOp(getOpcode()); 2856 } 2857 2858 static bool classof(const Stmt *S) { 2859 return S->getStmtClass() >= firstBinaryOperatorConstant && 2860 S->getStmtClass() <= lastBinaryOperatorConstant; 2861 } 2862 static bool classof(const BinaryOperator *) { return true; } 2863 2864 // Iterators 2865 child_range children() { 2866 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2867 } 2868 2869protected: 2870 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2871 ExprValueKind VK, ExprObjectKind OK, 2872 SourceLocation opLoc, bool dead) 2873 : Expr(CompoundAssignOperatorClass, ResTy, VK, OK, 2874 lhs->isTypeDependent() || rhs->isTypeDependent(), 2875 lhs->isValueDependent() || rhs->isValueDependent(), 2876 (lhs->isInstantiationDependent() || 2877 rhs->isInstantiationDependent()), 2878 (lhs->containsUnexpandedParameterPack() || 2879 rhs->containsUnexpandedParameterPack())), 2880 Opc(opc), OpLoc(opLoc) { 2881 SubExprs[LHS] = lhs; 2882 SubExprs[RHS] = rhs; 2883 } 2884 2885 BinaryOperator(StmtClass SC, EmptyShell Empty) 2886 : Expr(SC, Empty), Opc(BO_MulAssign) { } 2887}; 2888 2889/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 2890/// track of the type the operation is performed in. Due to the semantics of 2891/// these operators, the operands are promoted, the arithmetic performed, an 2892/// implicit conversion back to the result type done, then the assignment takes 2893/// place. This captures the intermediate type which the computation is done 2894/// in. 2895class CompoundAssignOperator : public BinaryOperator { 2896 QualType ComputationLHSType; 2897 QualType ComputationResultType; 2898public: 2899 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResType, 2900 ExprValueKind VK, ExprObjectKind OK, 2901 QualType CompLHSType, QualType CompResultType, 2902 SourceLocation OpLoc) 2903 : BinaryOperator(lhs, rhs, opc, ResType, VK, OK, OpLoc, true), 2904 ComputationLHSType(CompLHSType), 2905 ComputationResultType(CompResultType) { 2906 assert(isCompoundAssignmentOp() && 2907 "Only should be used for compound assignments"); 2908 } 2909 2910 /// \brief Build an empty compound assignment operator expression. 2911 explicit CompoundAssignOperator(EmptyShell Empty) 2912 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 2913 2914 // The two computation types are the type the LHS is converted 2915 // to for the computation and the type of the result; the two are 2916 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 2917 QualType getComputationLHSType() const { return ComputationLHSType; } 2918 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 2919 2920 QualType getComputationResultType() const { return ComputationResultType; } 2921 void setComputationResultType(QualType T) { ComputationResultType = T; } 2922 2923 static bool classof(const CompoundAssignOperator *) { return true; } 2924 static bool classof(const Stmt *S) { 2925 return S->getStmtClass() == CompoundAssignOperatorClass; 2926 } 2927}; 2928 2929/// AbstractConditionalOperator - An abstract base class for 2930/// ConditionalOperator and BinaryConditionalOperator. 2931class AbstractConditionalOperator : public Expr { 2932 SourceLocation QuestionLoc, ColonLoc; 2933 friend class ASTStmtReader; 2934 2935protected: 2936 AbstractConditionalOperator(StmtClass SC, QualType T, 2937 ExprValueKind VK, ExprObjectKind OK, 2938 bool TD, bool VD, bool ID, 2939 bool ContainsUnexpandedParameterPack, 2940 SourceLocation qloc, 2941 SourceLocation cloc) 2942 : Expr(SC, T, VK, OK, TD, VD, ID, ContainsUnexpandedParameterPack), 2943 QuestionLoc(qloc), ColonLoc(cloc) {} 2944 2945 AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) 2946 : Expr(SC, Empty) { } 2947 2948public: 2949 // getCond - Return the expression representing the condition for 2950 // the ?: operator. 2951 Expr *getCond() const; 2952 2953 // getTrueExpr - Return the subexpression representing the value of 2954 // the expression if the condition evaluates to true. 2955 Expr *getTrueExpr() const; 2956 2957 // getFalseExpr - Return the subexpression representing the value of 2958 // the expression if the condition evaluates to false. This is 2959 // the same as getRHS. 2960 Expr *getFalseExpr() const; 2961 2962 SourceLocation getQuestionLoc() const { return QuestionLoc; } 2963 SourceLocation getColonLoc() const { return ColonLoc; } 2964 2965 static bool classof(const Stmt *T) { 2966 return T->getStmtClass() == ConditionalOperatorClass || 2967 T->getStmtClass() == BinaryConditionalOperatorClass; 2968 } 2969 static bool classof(const AbstractConditionalOperator *) { return true; } 2970}; 2971 2972/// ConditionalOperator - The ?: ternary operator. The GNU "missing 2973/// middle" extension is a BinaryConditionalOperator. 2974class ConditionalOperator : public AbstractConditionalOperator { 2975 enum { COND, LHS, RHS, END_EXPR }; 2976 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2977 2978 friend class ASTStmtReader; 2979public: 2980 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 2981 SourceLocation CLoc, Expr *rhs, 2982 QualType t, ExprValueKind VK, ExprObjectKind OK) 2983 : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, 2984 // FIXME: the type of the conditional operator doesn't 2985 // depend on the type of the conditional, but the standard 2986 // seems to imply that it could. File a bug! 2987 (lhs->isTypeDependent() || rhs->isTypeDependent()), 2988 (cond->isValueDependent() || lhs->isValueDependent() || 2989 rhs->isValueDependent()), 2990 (cond->isInstantiationDependent() || 2991 lhs->isInstantiationDependent() || 2992 rhs->isInstantiationDependent()), 2993 (cond->containsUnexpandedParameterPack() || 2994 lhs->containsUnexpandedParameterPack() || 2995 rhs->containsUnexpandedParameterPack()), 2996 QLoc, CLoc) { 2997 SubExprs[COND] = cond; 2998 SubExprs[LHS] = lhs; 2999 SubExprs[RHS] = rhs; 3000 } 3001 3002 /// \brief Build an empty conditional operator. 3003 explicit ConditionalOperator(EmptyShell Empty) 3004 : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } 3005 3006 // getCond - Return the expression representing the condition for 3007 // the ?: operator. 3008 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3009 3010 // getTrueExpr - Return the subexpression representing the value of 3011 // the expression if the condition evaluates to true. 3012 Expr *getTrueExpr() const { return cast<Expr>(SubExprs[LHS]); } 3013 3014 // getFalseExpr - Return the subexpression representing the value of 3015 // the expression if the condition evaluates to false. This is 3016 // the same as getRHS. 3017 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 3018 3019 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 3020 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 3021 3022 SourceRange getSourceRange() const { 3023 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 3024 } 3025 static bool classof(const Stmt *T) { 3026 return T->getStmtClass() == ConditionalOperatorClass; 3027 } 3028 static bool classof(const ConditionalOperator *) { return true; } 3029 3030 // Iterators 3031 child_range children() { 3032 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 3033 } 3034}; 3035 3036/// BinaryConditionalOperator - The GNU extension to the conditional 3037/// operator which allows the middle operand to be omitted. 3038/// 3039/// This is a different expression kind on the assumption that almost 3040/// every client ends up needing to know that these are different. 3041class BinaryConditionalOperator : public AbstractConditionalOperator { 3042 enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; 3043 3044 /// - the common condition/left-hand-side expression, which will be 3045 /// evaluated as the opaque value 3046 /// - the condition, expressed in terms of the opaque value 3047 /// - the left-hand-side, expressed in terms of the opaque value 3048 /// - the right-hand-side 3049 Stmt *SubExprs[NUM_SUBEXPRS]; 3050 OpaqueValueExpr *OpaqueValue; 3051 3052 friend class ASTStmtReader; 3053public: 3054 BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, 3055 Expr *cond, Expr *lhs, Expr *rhs, 3056 SourceLocation qloc, SourceLocation cloc, 3057 QualType t, ExprValueKind VK, ExprObjectKind OK) 3058 : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, 3059 (common->isTypeDependent() || rhs->isTypeDependent()), 3060 (common->isValueDependent() || rhs->isValueDependent()), 3061 (common->isInstantiationDependent() || 3062 rhs->isInstantiationDependent()), 3063 (common->containsUnexpandedParameterPack() || 3064 rhs->containsUnexpandedParameterPack()), 3065 qloc, cloc), 3066 OpaqueValue(opaqueValue) { 3067 SubExprs[COMMON] = common; 3068 SubExprs[COND] = cond; 3069 SubExprs[LHS] = lhs; 3070 SubExprs[RHS] = rhs; 3071 assert(OpaqueValue->getSourceExpr() == common && "Wrong opaque value"); 3072 } 3073 3074 /// \brief Build an empty conditional operator. 3075 explicit BinaryConditionalOperator(EmptyShell Empty) 3076 : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } 3077 3078 /// \brief getCommon - Return the common expression, written to the 3079 /// left of the condition. The opaque value will be bound to the 3080 /// result of this expression. 3081 Expr *getCommon() const { return cast<Expr>(SubExprs[COMMON]); } 3082 3083 /// \brief getOpaqueValue - Return the opaque value placeholder. 3084 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } 3085 3086 /// \brief getCond - Return the condition expression; this is defined 3087 /// in terms of the opaque value. 3088 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3089 3090 /// \brief getTrueExpr - Return the subexpression which will be 3091 /// evaluated if the condition evaluates to true; this is defined 3092 /// in terms of the opaque value. 3093 Expr *getTrueExpr() const { 3094 return cast<Expr>(SubExprs[LHS]); 3095 } 3096 3097 /// \brief getFalseExpr - Return the subexpression which will be 3098 /// evaluated if the condnition evaluates to false; this is 3099 /// defined in terms of the opaque value. 3100 Expr *getFalseExpr() const { 3101 return cast<Expr>(SubExprs[RHS]); 3102 } 3103 3104 SourceRange getSourceRange() const { 3105 return SourceRange(getCommon()->getLocStart(), getFalseExpr()->getLocEnd()); 3106 } 3107 static bool classof(const Stmt *T) { 3108 return T->getStmtClass() == BinaryConditionalOperatorClass; 3109 } 3110 static bool classof(const BinaryConditionalOperator *) { return true; } 3111 3112 // Iterators 3113 child_range children() { 3114 return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); 3115 } 3116}; 3117 3118inline Expr *AbstractConditionalOperator::getCond() const { 3119 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3120 return co->getCond(); 3121 return cast<BinaryConditionalOperator>(this)->getCond(); 3122} 3123 3124inline Expr *AbstractConditionalOperator::getTrueExpr() const { 3125 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3126 return co->getTrueExpr(); 3127 return cast<BinaryConditionalOperator>(this)->getTrueExpr(); 3128} 3129 3130inline Expr *AbstractConditionalOperator::getFalseExpr() const { 3131 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3132 return co->getFalseExpr(); 3133 return cast<BinaryConditionalOperator>(this)->getFalseExpr(); 3134} 3135 3136/// AddrLabelExpr - The GNU address of label extension, representing &&label. 3137class AddrLabelExpr : public Expr { 3138 SourceLocation AmpAmpLoc, LabelLoc; 3139 LabelDecl *Label; 3140public: 3141 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, 3142 QualType t) 3143 : Expr(AddrLabelExprClass, t, VK_RValue, OK_Ordinary, false, false, false, 3144 false), 3145 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 3146 3147 /// \brief Build an empty address of a label expression. 3148 explicit AddrLabelExpr(EmptyShell Empty) 3149 : Expr(AddrLabelExprClass, Empty) { } 3150 3151 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 3152 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 3153 SourceLocation getLabelLoc() const { return LabelLoc; } 3154 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 3155 3156 SourceRange getSourceRange() const { 3157 return SourceRange(AmpAmpLoc, LabelLoc); 3158 } 3159 3160 LabelDecl *getLabel() const { return Label; } 3161 void setLabel(LabelDecl *L) { Label = L; } 3162 3163 static bool classof(const Stmt *T) { 3164 return T->getStmtClass() == AddrLabelExprClass; 3165 } 3166 static bool classof(const AddrLabelExpr *) { return true; } 3167 3168 // Iterators 3169 child_range children() { return child_range(); } 3170}; 3171 3172/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 3173/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 3174/// takes the value of the last subexpression. 3175/// 3176/// A StmtExpr is always an r-value; values "returned" out of a 3177/// StmtExpr will be copied. 3178class StmtExpr : public Expr { 3179 Stmt *SubStmt; 3180 SourceLocation LParenLoc, RParenLoc; 3181public: 3182 // FIXME: Does type-dependence need to be computed differently? 3183 // FIXME: Do we need to compute instantiation instantiation-dependence for 3184 // statements? (ugh!) 3185 StmtExpr(CompoundStmt *substmt, QualType T, 3186 SourceLocation lp, SourceLocation rp) : 3187 Expr(StmtExprClass, T, VK_RValue, OK_Ordinary, 3188 T->isDependentType(), false, false, false), 3189 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 3190 3191 /// \brief Build an empty statement expression. 3192 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 3193 3194 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 3195 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 3196 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 3197 3198 SourceRange getSourceRange() const { 3199 return SourceRange(LParenLoc, RParenLoc); 3200 } 3201 3202 SourceLocation getLParenLoc() const { return LParenLoc; } 3203 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 3204 SourceLocation getRParenLoc() const { return RParenLoc; } 3205 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3206 3207 static bool classof(const Stmt *T) { 3208 return T->getStmtClass() == StmtExprClass; 3209 } 3210 static bool classof(const StmtExpr *) { return true; } 3211 3212 // Iterators 3213 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 3214}; 3215 3216 3217/// ShuffleVectorExpr - clang-specific builtin-in function 3218/// __builtin_shufflevector. 3219/// This AST node represents a operator that does a constant 3220/// shuffle, similar to LLVM's shufflevector instruction. It takes 3221/// two vectors and a variable number of constant indices, 3222/// and returns the appropriately shuffled vector. 3223class ShuffleVectorExpr : public Expr { 3224 SourceLocation BuiltinLoc, RParenLoc; 3225 3226 // SubExprs - the list of values passed to the __builtin_shufflevector 3227 // function. The first two are vectors, and the rest are constant 3228 // indices. The number of values in this list is always 3229 // 2+the number of indices in the vector type. 3230 Stmt **SubExprs; 3231 unsigned NumExprs; 3232 3233public: 3234 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 3235 QualType Type, SourceLocation BLoc, 3236 SourceLocation RP); 3237 3238 /// \brief Build an empty vector-shuffle expression. 3239 explicit ShuffleVectorExpr(EmptyShell Empty) 3240 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 3241 3242 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3243 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3244 3245 SourceLocation getRParenLoc() const { return RParenLoc; } 3246 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3247 3248 SourceRange getSourceRange() const { 3249 return SourceRange(BuiltinLoc, RParenLoc); 3250 } 3251 static bool classof(const Stmt *T) { 3252 return T->getStmtClass() == ShuffleVectorExprClass; 3253 } 3254 static bool classof(const ShuffleVectorExpr *) { return true; } 3255 3256 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 3257 /// constant expression, the actual arguments passed in, and the function 3258 /// pointers. 3259 unsigned getNumSubExprs() const { return NumExprs; } 3260 3261 /// \brief Retrieve the array of expressions. 3262 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 3263 3264 /// getExpr - Return the Expr at the specified index. 3265 Expr *getExpr(unsigned Index) { 3266 assert((Index < NumExprs) && "Arg access out of range!"); 3267 return cast<Expr>(SubExprs[Index]); 3268 } 3269 const Expr *getExpr(unsigned Index) const { 3270 assert((Index < NumExprs) && "Arg access out of range!"); 3271 return cast<Expr>(SubExprs[Index]); 3272 } 3273 3274 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 3275 3276 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 3277 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 3278 return getExpr(N+2)->EvaluateKnownConstInt(Ctx).getZExtValue(); 3279 } 3280 3281 // Iterators 3282 child_range children() { 3283 return child_range(&SubExprs[0], &SubExprs[0]+NumExprs); 3284 } 3285}; 3286 3287/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 3288/// This AST node is similar to the conditional operator (?:) in C, with 3289/// the following exceptions: 3290/// - the test expression must be a integer constant expression. 3291/// - the expression returned acts like the chosen subexpression in every 3292/// visible way: the type is the same as that of the chosen subexpression, 3293/// and all predicates (whether it's an l-value, whether it's an integer 3294/// constant expression, etc.) return the same result as for the chosen 3295/// sub-expression. 3296class ChooseExpr : public Expr { 3297 enum { COND, LHS, RHS, END_EXPR }; 3298 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 3299 SourceLocation BuiltinLoc, RParenLoc; 3300public: 3301 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, 3302 QualType t, ExprValueKind VK, ExprObjectKind OK, 3303 SourceLocation RP, bool TypeDependent, bool ValueDependent) 3304 : Expr(ChooseExprClass, t, VK, OK, TypeDependent, ValueDependent, 3305 (cond->isInstantiationDependent() || 3306 lhs->isInstantiationDependent() || 3307 rhs->isInstantiationDependent()), 3308 (cond->containsUnexpandedParameterPack() || 3309 lhs->containsUnexpandedParameterPack() || 3310 rhs->containsUnexpandedParameterPack())), 3311 BuiltinLoc(BLoc), RParenLoc(RP) { 3312 SubExprs[COND] = cond; 3313 SubExprs[LHS] = lhs; 3314 SubExprs[RHS] = rhs; 3315 } 3316 3317 /// \brief Build an empty __builtin_choose_expr. 3318 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 3319 3320 /// isConditionTrue - Return whether the condition is true (i.e. not 3321 /// equal to zero). 3322 bool isConditionTrue(const ASTContext &C) const; 3323 3324 /// getChosenSubExpr - Return the subexpression chosen according to the 3325 /// condition. 3326 Expr *getChosenSubExpr(const ASTContext &C) const { 3327 return isConditionTrue(C) ? getLHS() : getRHS(); 3328 } 3329 3330 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3331 void setCond(Expr *E) { SubExprs[COND] = E; } 3332 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 3333 void setLHS(Expr *E) { SubExprs[LHS] = E; } 3334 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 3335 void setRHS(Expr *E) { SubExprs[RHS] = E; } 3336 3337 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3338 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3339 3340 SourceLocation getRParenLoc() const { return RParenLoc; } 3341 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3342 3343 SourceRange getSourceRange() const { 3344 return SourceRange(BuiltinLoc, RParenLoc); 3345 } 3346 static bool classof(const Stmt *T) { 3347 return T->getStmtClass() == ChooseExprClass; 3348 } 3349 static bool classof(const ChooseExpr *) { return true; } 3350 3351 // Iterators 3352 child_range children() { 3353 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 3354 } 3355}; 3356 3357/// GNUNullExpr - Implements the GNU __null extension, which is a name 3358/// for a null pointer constant that has integral type (e.g., int or 3359/// long) and is the same size and alignment as a pointer. The __null 3360/// extension is typically only used by system headers, which define 3361/// NULL as __null in C++ rather than using 0 (which is an integer 3362/// that may not match the size of a pointer). 3363class GNUNullExpr : public Expr { 3364 /// TokenLoc - The location of the __null keyword. 3365 SourceLocation TokenLoc; 3366 3367public: 3368 GNUNullExpr(QualType Ty, SourceLocation Loc) 3369 : Expr(GNUNullExprClass, Ty, VK_RValue, OK_Ordinary, false, false, false, 3370 false), 3371 TokenLoc(Loc) { } 3372 3373 /// \brief Build an empty GNU __null expression. 3374 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 3375 3376 /// getTokenLocation - The location of the __null token. 3377 SourceLocation getTokenLocation() const { return TokenLoc; } 3378 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 3379 3380 SourceRange getSourceRange() const { 3381 return SourceRange(TokenLoc); 3382 } 3383 static bool classof(const Stmt *T) { 3384 return T->getStmtClass() == GNUNullExprClass; 3385 } 3386 static bool classof(const GNUNullExpr *) { return true; } 3387 3388 // Iterators 3389 child_range children() { return child_range(); } 3390}; 3391 3392/// VAArgExpr, used for the builtin function __builtin_va_arg. 3393class VAArgExpr : public Expr { 3394 Stmt *Val; 3395 TypeSourceInfo *TInfo; 3396 SourceLocation BuiltinLoc, RParenLoc; 3397public: 3398 VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo, 3399 SourceLocation RPLoc, QualType t) 3400 : Expr(VAArgExprClass, t, VK_RValue, OK_Ordinary, 3401 t->isDependentType(), false, 3402 (TInfo->getType()->isInstantiationDependentType() || 3403 e->isInstantiationDependent()), 3404 (TInfo->getType()->containsUnexpandedParameterPack() || 3405 e->containsUnexpandedParameterPack())), 3406 Val(e), TInfo(TInfo), 3407 BuiltinLoc(BLoc), 3408 RParenLoc(RPLoc) { } 3409 3410 /// \brief Create an empty __builtin_va_arg expression. 3411 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 3412 3413 const Expr *getSubExpr() const { return cast<Expr>(Val); } 3414 Expr *getSubExpr() { return cast<Expr>(Val); } 3415 void setSubExpr(Expr *E) { Val = E; } 3416 3417 TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; } 3418 void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; } 3419 3420 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3421 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3422 3423 SourceLocation getRParenLoc() const { return RParenLoc; } 3424 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3425 3426 SourceRange getSourceRange() const { 3427 return SourceRange(BuiltinLoc, RParenLoc); 3428 } 3429 static bool classof(const Stmt *T) { 3430 return T->getStmtClass() == VAArgExprClass; 3431 } 3432 static bool classof(const VAArgExpr *) { return true; } 3433 3434 // Iterators 3435 child_range children() { return child_range(&Val, &Val+1); } 3436}; 3437 3438/// @brief Describes an C or C++ initializer list. 3439/// 3440/// InitListExpr describes an initializer list, which can be used to 3441/// initialize objects of different types, including 3442/// struct/class/union types, arrays, and vectors. For example: 3443/// 3444/// @code 3445/// struct foo x = { 1, { 2, 3 } }; 3446/// @endcode 3447/// 3448/// Prior to semantic analysis, an initializer list will represent the 3449/// initializer list as written by the user, but will have the 3450/// placeholder type "void". This initializer list is called the 3451/// syntactic form of the initializer, and may contain C99 designated 3452/// initializers (represented as DesignatedInitExprs), initializations 3453/// of subobject members without explicit braces, and so on. Clients 3454/// interested in the original syntax of the initializer list should 3455/// use the syntactic form of the initializer list. 3456/// 3457/// After semantic analysis, the initializer list will represent the 3458/// semantic form of the initializer, where the initializations of all 3459/// subobjects are made explicit with nested InitListExpr nodes and 3460/// C99 designators have been eliminated by placing the designated 3461/// initializations into the subobject they initialize. Additionally, 3462/// any "holes" in the initialization, where no initializer has been 3463/// specified for a particular subobject, will be replaced with 3464/// implicitly-generated ImplicitValueInitExpr expressions that 3465/// value-initialize the subobjects. Note, however, that the 3466/// initializer lists may still have fewer initializers than there are 3467/// elements to initialize within the object. 3468/// 3469/// Given the semantic form of the initializer list, one can retrieve 3470/// the original syntactic form of that initializer list (if it 3471/// exists) using getSyntacticForm(). Since many initializer lists 3472/// have the same syntactic and semantic forms, getSyntacticForm() may 3473/// return NULL, indicating that the current initializer list also 3474/// serves as its syntactic form. 3475class InitListExpr : public Expr { 3476 // FIXME: Eliminate this vector in favor of ASTContext allocation 3477 typedef ASTVector<Stmt *> InitExprsTy; 3478 InitExprsTy InitExprs; 3479 SourceLocation LBraceLoc, RBraceLoc; 3480 3481 /// Contains the initializer list that describes the syntactic form 3482 /// written in the source code. 3483 InitListExpr *SyntacticForm; 3484 3485 /// \brief Either: 3486 /// If this initializer list initializes an array with more elements than 3487 /// there are initializers in the list, specifies an expression to be used 3488 /// for value initialization of the rest of the elements. 3489 /// Or 3490 /// If this initializer list initializes a union, specifies which 3491 /// field within the union will be initialized. 3492 llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; 3493 3494public: 3495 InitListExpr(ASTContext &C, SourceLocation lbraceloc, 3496 Expr **initexprs, unsigned numinits, 3497 SourceLocation rbraceloc); 3498 3499 /// \brief Build an empty initializer list. 3500 explicit InitListExpr(ASTContext &C, EmptyShell Empty) 3501 : Expr(InitListExprClass, Empty), InitExprs(C) { } 3502 3503 unsigned getNumInits() const { return InitExprs.size(); } 3504 3505 /// \brief Retrieve the set of initializers. 3506 Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } 3507 3508 const Expr *getInit(unsigned Init) const { 3509 assert(Init < getNumInits() && "Initializer access out of range!"); 3510 return cast_or_null<Expr>(InitExprs[Init]); 3511 } 3512 3513 Expr *getInit(unsigned Init) { 3514 assert(Init < getNumInits() && "Initializer access out of range!"); 3515 return cast_or_null<Expr>(InitExprs[Init]); 3516 } 3517 3518 void setInit(unsigned Init, Expr *expr) { 3519 assert(Init < getNumInits() && "Initializer access out of range!"); 3520 InitExprs[Init] = expr; 3521 } 3522 3523 /// \brief Reserve space for some number of initializers. 3524 void reserveInits(ASTContext &C, unsigned NumInits); 3525 3526 /// @brief Specify the number of initializers 3527 /// 3528 /// If there are more than @p NumInits initializers, the remaining 3529 /// initializers will be destroyed. If there are fewer than @p 3530 /// NumInits initializers, NULL expressions will be added for the 3531 /// unknown initializers. 3532 void resizeInits(ASTContext &Context, unsigned NumInits); 3533 3534 /// @brief Updates the initializer at index @p Init with the new 3535 /// expression @p expr, and returns the old expression at that 3536 /// location. 3537 /// 3538 /// When @p Init is out of range for this initializer list, the 3539 /// initializer list will be extended with NULL expressions to 3540 /// accommodate the new entry. 3541 Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr); 3542 3543 /// \brief If this initializer list initializes an array with more elements 3544 /// than there are initializers in the list, specifies an expression to be 3545 /// used for value initialization of the rest of the elements. 3546 Expr *getArrayFiller() { 3547 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>(); 3548 } 3549 const Expr *getArrayFiller() const { 3550 return const_cast<InitListExpr *>(this)->getArrayFiller(); 3551 } 3552 void setArrayFiller(Expr *filler); 3553 3554 /// \brief Return true if this is an array initializer and its array "filler" 3555 /// has been set. 3556 bool hasArrayFiller() const { return getArrayFiller(); } 3557 3558 /// \brief If this initializes a union, specifies which field in the 3559 /// union to initialize. 3560 /// 3561 /// Typically, this field is the first named field within the 3562 /// union. However, a designated initializer can specify the 3563 /// initialization of a different field within the union. 3564 FieldDecl *getInitializedFieldInUnion() { 3565 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>(); 3566 } 3567 const FieldDecl *getInitializedFieldInUnion() const { 3568 return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion(); 3569 } 3570 void setInitializedFieldInUnion(FieldDecl *FD) { 3571 ArrayFillerOrUnionFieldInit = FD; 3572 } 3573 3574 // Explicit InitListExpr's originate from source code (and have valid source 3575 // locations). Implicit InitListExpr's are created by the semantic analyzer. 3576 bool isExplicit() { 3577 return LBraceLoc.isValid() && RBraceLoc.isValid(); 3578 } 3579 3580 SourceLocation getLBraceLoc() const { return LBraceLoc; } 3581 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 3582 SourceLocation getRBraceLoc() const { return RBraceLoc; } 3583 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 3584 3585 /// @brief Retrieve the initializer list that describes the 3586 /// syntactic form of the initializer. 3587 /// 3588 /// 3589 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 3590 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 3591 3592 bool hadArrayRangeDesignator() const { 3593 return InitListExprBits.HadArrayRangeDesignator != 0; 3594 } 3595 void sawArrayRangeDesignator(bool ARD = true) { 3596 InitListExprBits.HadArrayRangeDesignator = ARD; 3597 } 3598 3599 bool initializesStdInitializerList() const { 3600 return InitListExprBits.InitializesStdInitializerList != 0; 3601 } 3602 void setInitializesStdInitializerList(bool ISIL = true) { 3603 InitListExprBits.InitializesStdInitializerList = ISIL; 3604 } 3605 3606 SourceRange getSourceRange() const; 3607 3608 static bool classof(const Stmt *T) { 3609 return T->getStmtClass() == InitListExprClass; 3610 } 3611 static bool classof(const InitListExpr *) { return true; } 3612 3613 // Iterators 3614 child_range children() { 3615 if (InitExprs.empty()) return child_range(); 3616 return child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); 3617 } 3618 3619 typedef InitExprsTy::iterator iterator; 3620 typedef InitExprsTy::const_iterator const_iterator; 3621 typedef InitExprsTy::reverse_iterator reverse_iterator; 3622 typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; 3623 3624 iterator begin() { return InitExprs.begin(); } 3625 const_iterator begin() const { return InitExprs.begin(); } 3626 iterator end() { return InitExprs.end(); } 3627 const_iterator end() const { return InitExprs.end(); } 3628 reverse_iterator rbegin() { return InitExprs.rbegin(); } 3629 const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } 3630 reverse_iterator rend() { return InitExprs.rend(); } 3631 const_reverse_iterator rend() const { return InitExprs.rend(); } 3632 3633 friend class ASTStmtReader; 3634 friend class ASTStmtWriter; 3635}; 3636 3637/// @brief Represents a C99 designated initializer expression. 3638/// 3639/// A designated initializer expression (C99 6.7.8) contains one or 3640/// more designators (which can be field designators, array 3641/// designators, or GNU array-range designators) followed by an 3642/// expression that initializes the field or element(s) that the 3643/// designators refer to. For example, given: 3644/// 3645/// @code 3646/// struct point { 3647/// double x; 3648/// double y; 3649/// }; 3650/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 3651/// @endcode 3652/// 3653/// The InitListExpr contains three DesignatedInitExprs, the first of 3654/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 3655/// designators, one array designator for @c [2] followed by one field 3656/// designator for @c .y. The initalization expression will be 1.0. 3657class DesignatedInitExpr : public Expr { 3658public: 3659 /// \brief Forward declaration of the Designator class. 3660 class Designator; 3661 3662private: 3663 /// The location of the '=' or ':' prior to the actual initializer 3664 /// expression. 3665 SourceLocation EqualOrColonLoc; 3666 3667 /// Whether this designated initializer used the GNU deprecated 3668 /// syntax rather than the C99 '=' syntax. 3669 bool GNUSyntax : 1; 3670 3671 /// The number of designators in this initializer expression. 3672 unsigned NumDesignators : 15; 3673 3674 /// The number of subexpressions of this initializer expression, 3675 /// which contains both the initializer and any additional 3676 /// expressions used by array and array-range designators. 3677 unsigned NumSubExprs : 16; 3678 3679 /// \brief The designators in this designated initialization 3680 /// expression. 3681 Designator *Designators; 3682 3683 3684 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators, 3685 const Designator *Designators, 3686 SourceLocation EqualOrColonLoc, bool GNUSyntax, 3687 Expr **IndexExprs, unsigned NumIndexExprs, 3688 Expr *Init); 3689 3690 explicit DesignatedInitExpr(unsigned NumSubExprs) 3691 : Expr(DesignatedInitExprClass, EmptyShell()), 3692 NumDesignators(0), NumSubExprs(NumSubExprs), Designators(0) { } 3693 3694public: 3695 /// A field designator, e.g., ".x". 3696 struct FieldDesignator { 3697 /// Refers to the field that is being initialized. The low bit 3698 /// of this field determines whether this is actually a pointer 3699 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 3700 /// initially constructed, a field designator will store an 3701 /// IdentifierInfo*. After semantic analysis has resolved that 3702 /// name, the field designator will instead store a FieldDecl*. 3703 uintptr_t NameOrField; 3704 3705 /// The location of the '.' in the designated initializer. 3706 unsigned DotLoc; 3707 3708 /// The location of the field name in the designated initializer. 3709 unsigned FieldLoc; 3710 }; 3711 3712 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3713 struct ArrayOrRangeDesignator { 3714 /// Location of the first index expression within the designated 3715 /// initializer expression's list of subexpressions. 3716 unsigned Index; 3717 /// The location of the '[' starting the array range designator. 3718 unsigned LBracketLoc; 3719 /// The location of the ellipsis separating the start and end 3720 /// indices. Only valid for GNU array-range designators. 3721 unsigned EllipsisLoc; 3722 /// The location of the ']' terminating the array range designator. 3723 unsigned RBracketLoc; 3724 }; 3725 3726 /// @brief Represents a single C99 designator. 3727 /// 3728 /// @todo This class is infuriatingly similar to clang::Designator, 3729 /// but minor differences (storing indices vs. storing pointers) 3730 /// keep us from reusing it. Try harder, later, to rectify these 3731 /// differences. 3732 class Designator { 3733 /// @brief The kind of designator this describes. 3734 enum { 3735 FieldDesignator, 3736 ArrayDesignator, 3737 ArrayRangeDesignator 3738 } Kind; 3739 3740 union { 3741 /// A field designator, e.g., ".x". 3742 struct FieldDesignator Field; 3743 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3744 struct ArrayOrRangeDesignator ArrayOrRange; 3745 }; 3746 friend class DesignatedInitExpr; 3747 3748 public: 3749 Designator() {} 3750 3751 /// @brief Initializes a field designator. 3752 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 3753 SourceLocation FieldLoc) 3754 : Kind(FieldDesignator) { 3755 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 3756 Field.DotLoc = DotLoc.getRawEncoding(); 3757 Field.FieldLoc = FieldLoc.getRawEncoding(); 3758 } 3759 3760 /// @brief Initializes an array designator. 3761 Designator(unsigned Index, SourceLocation LBracketLoc, 3762 SourceLocation RBracketLoc) 3763 : Kind(ArrayDesignator) { 3764 ArrayOrRange.Index = Index; 3765 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3766 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 3767 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3768 } 3769 3770 /// @brief Initializes a GNU array-range designator. 3771 Designator(unsigned Index, SourceLocation LBracketLoc, 3772 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 3773 : Kind(ArrayRangeDesignator) { 3774 ArrayOrRange.Index = Index; 3775 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3776 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 3777 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3778 } 3779 3780 bool isFieldDesignator() const { return Kind == FieldDesignator; } 3781 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 3782 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 3783 3784 IdentifierInfo *getFieldName() const; 3785 3786 FieldDecl *getField() const { 3787 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3788 if (Field.NameOrField & 0x01) 3789 return 0; 3790 else 3791 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 3792 } 3793 3794 void setField(FieldDecl *FD) { 3795 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3796 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 3797 } 3798 3799 SourceLocation getDotLoc() const { 3800 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3801 return SourceLocation::getFromRawEncoding(Field.DotLoc); 3802 } 3803 3804 SourceLocation getFieldLoc() const { 3805 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3806 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 3807 } 3808 3809 SourceLocation getLBracketLoc() const { 3810 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3811 "Only valid on an array or array-range designator"); 3812 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 3813 } 3814 3815 SourceLocation getRBracketLoc() const { 3816 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3817 "Only valid on an array or array-range designator"); 3818 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 3819 } 3820 3821 SourceLocation getEllipsisLoc() const { 3822 assert(Kind == ArrayRangeDesignator && 3823 "Only valid on an array-range designator"); 3824 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 3825 } 3826 3827 unsigned getFirstExprIndex() const { 3828 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3829 "Only valid on an array or array-range designator"); 3830 return ArrayOrRange.Index; 3831 } 3832 3833 SourceLocation getStartLocation() const { 3834 if (Kind == FieldDesignator) 3835 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 3836 else 3837 return getLBracketLoc(); 3838 } 3839 SourceLocation getEndLocation() const { 3840 return Kind == FieldDesignator ? getFieldLoc() : getRBracketLoc(); 3841 } 3842 SourceRange getSourceRange() const { 3843 return SourceRange(getStartLocation(), getEndLocation()); 3844 } 3845 }; 3846 3847 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 3848 unsigned NumDesignators, 3849 Expr **IndexExprs, unsigned NumIndexExprs, 3850 SourceLocation EqualOrColonLoc, 3851 bool GNUSyntax, Expr *Init); 3852 3853 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 3854 3855 /// @brief Returns the number of designators in this initializer. 3856 unsigned size() const { return NumDesignators; } 3857 3858 // Iterator access to the designators. 3859 typedef Designator *designators_iterator; 3860 designators_iterator designators_begin() { return Designators; } 3861 designators_iterator designators_end() { 3862 return Designators + NumDesignators; 3863 } 3864 3865 typedef const Designator *const_designators_iterator; 3866 const_designators_iterator designators_begin() const { return Designators; } 3867 const_designators_iterator designators_end() const { 3868 return Designators + NumDesignators; 3869 } 3870 3871 typedef std::reverse_iterator<designators_iterator> 3872 reverse_designators_iterator; 3873 reverse_designators_iterator designators_rbegin() { 3874 return reverse_designators_iterator(designators_end()); 3875 } 3876 reverse_designators_iterator designators_rend() { 3877 return reverse_designators_iterator(designators_begin()); 3878 } 3879 3880 typedef std::reverse_iterator<const_designators_iterator> 3881 const_reverse_designators_iterator; 3882 const_reverse_designators_iterator designators_rbegin() const { 3883 return const_reverse_designators_iterator(designators_end()); 3884 } 3885 const_reverse_designators_iterator designators_rend() const { 3886 return const_reverse_designators_iterator(designators_begin()); 3887 } 3888 3889 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 3890 3891 void setDesignators(ASTContext &C, const Designator *Desigs, 3892 unsigned NumDesigs); 3893 3894 Expr *getArrayIndex(const Designator& D); 3895 Expr *getArrayRangeStart(const Designator& D); 3896 Expr *getArrayRangeEnd(const Designator& D); 3897 3898 /// @brief Retrieve the location of the '=' that precedes the 3899 /// initializer value itself, if present. 3900 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 3901 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 3902 3903 /// @brief Determines whether this designated initializer used the 3904 /// deprecated GNU syntax for designated initializers. 3905 bool usesGNUSyntax() const { return GNUSyntax; } 3906 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 3907 3908 /// @brief Retrieve the initializer value. 3909 Expr *getInit() const { 3910 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 3911 } 3912 3913 void setInit(Expr *init) { 3914 *child_begin() = init; 3915 } 3916 3917 /// \brief Retrieve the total number of subexpressions in this 3918 /// designated initializer expression, including the actual 3919 /// initialized value and any expressions that occur within array 3920 /// and array-range designators. 3921 unsigned getNumSubExprs() const { return NumSubExprs; } 3922 3923 Expr *getSubExpr(unsigned Idx) { 3924 assert(Idx < NumSubExprs && "Subscript out of range"); 3925 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3926 Ptr += sizeof(DesignatedInitExpr); 3927 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 3928 } 3929 3930 void setSubExpr(unsigned Idx, Expr *E) { 3931 assert(Idx < NumSubExprs && "Subscript out of range"); 3932 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3933 Ptr += sizeof(DesignatedInitExpr); 3934 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 3935 } 3936 3937 /// \brief Replaces the designator at index @p Idx with the series 3938 /// of designators in [First, Last). 3939 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First, 3940 const Designator *Last); 3941 3942 SourceRange getDesignatorsSourceRange() const; 3943 3944 SourceRange getSourceRange() const; 3945 3946 static bool classof(const Stmt *T) { 3947 return T->getStmtClass() == DesignatedInitExprClass; 3948 } 3949 static bool classof(const DesignatedInitExpr *) { return true; } 3950 3951 // Iterators 3952 child_range children() { 3953 Stmt **begin = reinterpret_cast<Stmt**>(this + 1); 3954 return child_range(begin, begin + NumSubExprs); 3955 } 3956}; 3957 3958/// \brief Represents an implicitly-generated value initialization of 3959/// an object of a given type. 3960/// 3961/// Implicit value initializations occur within semantic initializer 3962/// list expressions (InitListExpr) as placeholders for subobject 3963/// initializations not explicitly specified by the user. 3964/// 3965/// \see InitListExpr 3966class ImplicitValueInitExpr : public Expr { 3967public: 3968 explicit ImplicitValueInitExpr(QualType ty) 3969 : Expr(ImplicitValueInitExprClass, ty, VK_RValue, OK_Ordinary, 3970 false, false, ty->isInstantiationDependentType(), false) { } 3971 3972 /// \brief Construct an empty implicit value initialization. 3973 explicit ImplicitValueInitExpr(EmptyShell Empty) 3974 : Expr(ImplicitValueInitExprClass, Empty) { } 3975 3976 static bool classof(const Stmt *T) { 3977 return T->getStmtClass() == ImplicitValueInitExprClass; 3978 } 3979 static bool classof(const ImplicitValueInitExpr *) { return true; } 3980 3981 SourceRange getSourceRange() const { 3982 return SourceRange(); 3983 } 3984 3985 // Iterators 3986 child_range children() { return child_range(); } 3987}; 3988 3989 3990class ParenListExpr : public Expr { 3991 Stmt **Exprs; 3992 unsigned NumExprs; 3993 SourceLocation LParenLoc, RParenLoc; 3994 3995public: 3996 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 3997 unsigned numexprs, SourceLocation rparenloc); 3998 3999 /// \brief Build an empty paren list. 4000 explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 4001 4002 unsigned getNumExprs() const { return NumExprs; } 4003 4004 const Expr* getExpr(unsigned Init) const { 4005 assert(Init < getNumExprs() && "Initializer access out of range!"); 4006 return cast_or_null<Expr>(Exprs[Init]); 4007 } 4008 4009 Expr* getExpr(unsigned Init) { 4010 assert(Init < getNumExprs() && "Initializer access out of range!"); 4011 return cast_or_null<Expr>(Exprs[Init]); 4012 } 4013 4014 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 4015 4016 SourceLocation getLParenLoc() const { return LParenLoc; } 4017 SourceLocation getRParenLoc() const { return RParenLoc; } 4018 4019 SourceRange getSourceRange() const { 4020 return SourceRange(LParenLoc, RParenLoc); 4021 } 4022 static bool classof(const Stmt *T) { 4023 return T->getStmtClass() == ParenListExprClass; 4024 } 4025 static bool classof(const ParenListExpr *) { return true; } 4026 4027 // Iterators 4028 child_range children() { 4029 return child_range(&Exprs[0], &Exprs[0]+NumExprs); 4030 } 4031 4032 friend class ASTStmtReader; 4033 friend class ASTStmtWriter; 4034}; 4035 4036 4037/// \brief Represents a C11 generic selection. 4038/// 4039/// A generic selection (C11 6.5.1.1) contains an unevaluated controlling 4040/// expression, followed by one or more generic associations. Each generic 4041/// association specifies a type name and an expression, or "default" and an 4042/// expression (in which case it is known as a default generic association). 4043/// The type and value of the generic selection are identical to those of its 4044/// result expression, which is defined as the expression in the generic 4045/// association with a type name that is compatible with the type of the 4046/// controlling expression, or the expression in the default generic association 4047/// if no types are compatible. For example: 4048/// 4049/// @code 4050/// _Generic(X, double: 1, float: 2, default: 3) 4051/// @endcode 4052/// 4053/// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f 4054/// or 3 if "hello". 4055/// 4056/// As an extension, generic selections are allowed in C++, where the following 4057/// additional semantics apply: 4058/// 4059/// Any generic selection whose controlling expression is type-dependent or 4060/// which names a dependent type in its association list is result-dependent, 4061/// which means that the choice of result expression is dependent. 4062/// Result-dependent generic associations are both type- and value-dependent. 4063class GenericSelectionExpr : public Expr { 4064 enum { CONTROLLING, END_EXPR }; 4065 TypeSourceInfo **AssocTypes; 4066 Stmt **SubExprs; 4067 unsigned NumAssocs, ResultIndex; 4068 SourceLocation GenericLoc, DefaultLoc, RParenLoc; 4069 4070public: 4071 GenericSelectionExpr(ASTContext &Context, 4072 SourceLocation GenericLoc, Expr *ControllingExpr, 4073 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 4074 unsigned NumAssocs, SourceLocation DefaultLoc, 4075 SourceLocation RParenLoc, 4076 bool ContainsUnexpandedParameterPack, 4077 unsigned ResultIndex); 4078 4079 /// This constructor is used in the result-dependent case. 4080 GenericSelectionExpr(ASTContext &Context, 4081 SourceLocation GenericLoc, Expr *ControllingExpr, 4082 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 4083 unsigned NumAssocs, SourceLocation DefaultLoc, 4084 SourceLocation RParenLoc, 4085 bool ContainsUnexpandedParameterPack); 4086 4087 explicit GenericSelectionExpr(EmptyShell Empty) 4088 : Expr(GenericSelectionExprClass, Empty) { } 4089 4090 unsigned getNumAssocs() const { return NumAssocs; } 4091 4092 SourceLocation getGenericLoc() const { return GenericLoc; } 4093 SourceLocation getDefaultLoc() const { return DefaultLoc; } 4094 SourceLocation getRParenLoc() const { return RParenLoc; } 4095 4096 const Expr *getAssocExpr(unsigned i) const { 4097 return cast<Expr>(SubExprs[END_EXPR+i]); 4098 } 4099 Expr *getAssocExpr(unsigned i) { return cast<Expr>(SubExprs[END_EXPR+i]); } 4100 4101 const TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) const { 4102 return AssocTypes[i]; 4103 } 4104 TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) { return AssocTypes[i]; } 4105 4106 QualType getAssocType(unsigned i) const { 4107 if (const TypeSourceInfo *TS = getAssocTypeSourceInfo(i)) 4108 return TS->getType(); 4109 else 4110 return QualType(); 4111 } 4112 4113 const Expr *getControllingExpr() const { 4114 return cast<Expr>(SubExprs[CONTROLLING]); 4115 } 4116 Expr *getControllingExpr() { return cast<Expr>(SubExprs[CONTROLLING]); } 4117 4118 /// Whether this generic selection is result-dependent. 4119 bool isResultDependent() const { return ResultIndex == -1U; } 4120 4121 /// The zero-based index of the result expression's generic association in 4122 /// the generic selection's association list. Defined only if the 4123 /// generic selection is not result-dependent. 4124 unsigned getResultIndex() const { 4125 assert(!isResultDependent() && "Generic selection is result-dependent"); 4126 return ResultIndex; 4127 } 4128 4129 /// The generic selection's result expression. Defined only if the 4130 /// generic selection is not result-dependent. 4131 const Expr *getResultExpr() const { return getAssocExpr(getResultIndex()); } 4132 Expr *getResultExpr() { return getAssocExpr(getResultIndex()); } 4133 4134 SourceRange getSourceRange() const { 4135 return SourceRange(GenericLoc, RParenLoc); 4136 } 4137 static bool classof(const Stmt *T) { 4138 return T->getStmtClass() == GenericSelectionExprClass; 4139 } 4140 static bool classof(const GenericSelectionExpr *) { return true; } 4141 4142 child_range children() { 4143 return child_range(SubExprs, SubExprs+END_EXPR+NumAssocs); 4144 } 4145 4146 friend class ASTStmtReader; 4147}; 4148 4149//===----------------------------------------------------------------------===// 4150// Clang Extensions 4151//===----------------------------------------------------------------------===// 4152 4153 4154/// ExtVectorElementExpr - This represents access to specific elements of a 4155/// vector, and may occur on the left hand side or right hand side. For example 4156/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 4157/// 4158/// Note that the base may have either vector or pointer to vector type, just 4159/// like a struct field reference. 4160/// 4161class ExtVectorElementExpr : public Expr { 4162 Stmt *Base; 4163 IdentifierInfo *Accessor; 4164 SourceLocation AccessorLoc; 4165public: 4166 ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, 4167 IdentifierInfo &accessor, SourceLocation loc) 4168 : Expr(ExtVectorElementExprClass, ty, VK, 4169 (VK == VK_RValue ? OK_Ordinary : OK_VectorComponent), 4170 base->isTypeDependent(), base->isValueDependent(), 4171 base->isInstantiationDependent(), 4172 base->containsUnexpandedParameterPack()), 4173 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 4174 4175 /// \brief Build an empty vector element expression. 4176 explicit ExtVectorElementExpr(EmptyShell Empty) 4177 : Expr(ExtVectorElementExprClass, Empty) { } 4178 4179 const Expr *getBase() const { return cast<Expr>(Base); } 4180 Expr *getBase() { return cast<Expr>(Base); } 4181 void setBase(Expr *E) { Base = E; } 4182 4183 IdentifierInfo &getAccessor() const { return *Accessor; } 4184 void setAccessor(IdentifierInfo *II) { Accessor = II; } 4185 4186 SourceLocation getAccessorLoc() const { return AccessorLoc; } 4187 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 4188 4189 /// getNumElements - Get the number of components being selected. 4190 unsigned getNumElements() const; 4191 4192 /// containsDuplicateElements - Return true if any element access is 4193 /// repeated. 4194 bool containsDuplicateElements() const; 4195 4196 /// getEncodedElementAccess - Encode the elements accessed into an llvm 4197 /// aggregate Constant of ConstantInt(s). 4198 void getEncodedElementAccess(SmallVectorImpl<unsigned> &Elts) const; 4199 4200 SourceRange getSourceRange() const { 4201 return SourceRange(getBase()->getLocStart(), AccessorLoc); 4202 } 4203 4204 /// isArrow - Return true if the base expression is a pointer to vector, 4205 /// return false if the base expression is a vector. 4206 bool isArrow() const; 4207 4208 static bool classof(const Stmt *T) { 4209 return T->getStmtClass() == ExtVectorElementExprClass; 4210 } 4211 static bool classof(const ExtVectorElementExpr *) { return true; } 4212 4213 // Iterators 4214 child_range children() { return child_range(&Base, &Base+1); } 4215}; 4216 4217 4218/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 4219/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 4220class BlockExpr : public Expr { 4221protected: 4222 BlockDecl *TheBlock; 4223public: 4224 BlockExpr(BlockDecl *BD, QualType ty) 4225 : Expr(BlockExprClass, ty, VK_RValue, OK_Ordinary, 4226 ty->isDependentType(), ty->isDependentType(), 4227 ty->isInstantiationDependentType() || BD->isDependentContext(), 4228 false), 4229 TheBlock(BD) {} 4230 4231 /// \brief Build an empty block expression. 4232 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 4233 4234 const BlockDecl *getBlockDecl() const { return TheBlock; } 4235 BlockDecl *getBlockDecl() { return TheBlock; } 4236 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 4237 4238 // Convenience functions for probing the underlying BlockDecl. 4239 SourceLocation getCaretLocation() const; 4240 const Stmt *getBody() const; 4241 Stmt *getBody(); 4242 4243 SourceRange getSourceRange() const { 4244 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 4245 } 4246 4247 /// getFunctionType - Return the underlying function type for this block. 4248 const FunctionProtoType *getFunctionType() const; 4249 4250 static bool classof(const Stmt *T) { 4251 return T->getStmtClass() == BlockExprClass; 4252 } 4253 static bool classof(const BlockExpr *) { return true; } 4254 4255 // Iterators 4256 child_range children() { return child_range(); } 4257}; 4258 4259/// BlockDeclRefExpr - A reference to a local variable declared in an 4260/// enclosing scope. 4261class BlockDeclRefExpr : public Expr { 4262 VarDecl *D; 4263 SourceLocation Loc; 4264 bool IsByRef : 1; 4265 bool ConstQualAdded : 1; 4266public: 4267 BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK, 4268 SourceLocation l, bool ByRef, bool constAdded = false); 4269 4270 // \brief Build an empty reference to a declared variable in a 4271 // block. 4272 explicit BlockDeclRefExpr(EmptyShell Empty) 4273 : Expr(BlockDeclRefExprClass, Empty) { } 4274 4275 VarDecl *getDecl() { return D; } 4276 const VarDecl *getDecl() const { return D; } 4277 void setDecl(VarDecl *VD) { D = VD; } 4278 4279 SourceLocation getLocation() const { return Loc; } 4280 void setLocation(SourceLocation L) { Loc = L; } 4281 4282 SourceRange getSourceRange() const { return SourceRange(Loc); } 4283 4284 bool isByRef() const { return IsByRef; } 4285 void setByRef(bool BR) { IsByRef = BR; } 4286 4287 bool isConstQualAdded() const { return ConstQualAdded; } 4288 void setConstQualAdded(bool C) { ConstQualAdded = C; } 4289 4290 static bool classof(const Stmt *T) { 4291 return T->getStmtClass() == BlockDeclRefExprClass; 4292 } 4293 static bool classof(const BlockDeclRefExpr *) { return true; } 4294 4295 // Iterators 4296 child_range children() { return child_range(); } 4297}; 4298 4299/// AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] 4300/// This AST node provides support for reinterpreting a type to another 4301/// type of the same size. 4302class AsTypeExpr : public Expr { // Should this be an ExplicitCastExpr? 4303private: 4304 Stmt *SrcExpr; 4305 SourceLocation BuiltinLoc, RParenLoc; 4306 4307 friend class ASTReader; 4308 friend class ASTStmtReader; 4309 explicit AsTypeExpr(EmptyShell Empty) : Expr(AsTypeExprClass, Empty) {} 4310 4311public: 4312 AsTypeExpr(Expr* SrcExpr, QualType DstType, 4313 ExprValueKind VK, ExprObjectKind OK, 4314 SourceLocation BuiltinLoc, SourceLocation RParenLoc) 4315 : Expr(AsTypeExprClass, DstType, VK, OK, 4316 DstType->isDependentType(), 4317 DstType->isDependentType() || SrcExpr->isValueDependent(), 4318 (DstType->isInstantiationDependentType() || 4319 SrcExpr->isInstantiationDependent()), 4320 (DstType->containsUnexpandedParameterPack() || 4321 SrcExpr->containsUnexpandedParameterPack())), 4322 SrcExpr(SrcExpr), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) {} 4323 4324 /// getSrcExpr - Return the Expr to be converted. 4325 Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } 4326 4327 /// getBuiltinLoc - Return the location of the __builtin_astype token. 4328 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 4329 4330 /// getRParenLoc - Return the location of final right parenthesis. 4331 SourceLocation getRParenLoc() const { return RParenLoc; } 4332 4333 SourceRange getSourceRange() const { 4334 return SourceRange(BuiltinLoc, RParenLoc); 4335 } 4336 4337 static bool classof(const Stmt *T) { 4338 return T->getStmtClass() == AsTypeExprClass; 4339 } 4340 static bool classof(const AsTypeExpr *) { return true; } 4341 4342 // Iterators 4343 child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } 4344}; 4345 4346/// PseudoObjectExpr - An expression which accesses a pseudo-object 4347/// l-value. A pseudo-object is an abstract object, accesses to which 4348/// are translated to calls. The pseudo-object expression has a 4349/// syntactic form, which shows how the expression was actually 4350/// written in the source code, and a semantic form, which is a series 4351/// of expressions to be executed in order which detail how the 4352/// operation is actually evaluated. Optionally, one of the semantic 4353/// forms may also provide a result value for the expression. 4354/// 4355/// If any of the semantic-form expressions is an OpaqueValueExpr, 4356/// that OVE is required to have a source expression, and it is bound 4357/// to the result of that source expression. Such OVEs may appear 4358/// only in subsequent semantic-form expressions and as 4359/// sub-expressions of the syntactic form. 4360/// 4361/// PseudoObjectExpr should be used only when an operation can be 4362/// usefully described in terms of fairly simple rewrite rules on 4363/// objects and functions that are meant to be used by end-developers. 4364/// For example, under the Itanium ABI, dynamic casts are implemented 4365/// as a call to a runtime function called __dynamic_cast; using this 4366/// class to describe that would be inappropriate because that call is 4367/// not really part of the user-visible semantics, and instead the 4368/// cast is properly reflected in the AST and IR-generation has been 4369/// taught to generate the call as necessary. In contrast, an 4370/// Objective-C property access is semantically defined to be 4371/// equivalent to a particular message send, and this is very much 4372/// part of the user model. The name of this class encourages this 4373/// modelling design. 4374class PseudoObjectExpr : public Expr { 4375 // PseudoObjectExprBits.NumSubExprs - The number of sub-expressions. 4376 // Always at least two, because the first sub-expression is the 4377 // syntactic form. 4378 4379 // PseudoObjectExprBits.ResultIndex - The index of the 4380 // sub-expression holding the result. 0 means the result is void, 4381 // which is unambiguous because it's the index of the syntactic 4382 // form. Note that this is therefore 1 higher than the value passed 4383 // in to Create, which is an index within the semantic forms. 4384 // Note also that ASTStmtWriter assumes this encoding. 4385 4386 Expr **getSubExprsBuffer() { return reinterpret_cast<Expr**>(this + 1); } 4387 const Expr * const *getSubExprsBuffer() const { 4388 return reinterpret_cast<const Expr * const *>(this + 1); 4389 } 4390 4391 friend class ASTStmtReader; 4392 4393 PseudoObjectExpr(QualType type, ExprValueKind VK, 4394 Expr *syntactic, ArrayRef<Expr*> semantic, 4395 unsigned resultIndex); 4396 4397 PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs); 4398 4399 unsigned getNumSubExprs() const { 4400 return PseudoObjectExprBits.NumSubExprs; 4401 } 4402 4403public: 4404 /// NoResult - A value for the result index indicating that there is 4405 /// no semantic result. 4406 enum { NoResult = ~0U }; 4407 4408 static PseudoObjectExpr *Create(ASTContext &Context, Expr *syntactic, 4409 ArrayRef<Expr*> semantic, 4410 unsigned resultIndex); 4411 4412 static PseudoObjectExpr *Create(ASTContext &Context, EmptyShell shell, 4413 unsigned numSemanticExprs); 4414 4415 /// Return the syntactic form of this expression, i.e. the 4416 /// expression it actually looks like. Likely to be expressed in 4417 /// terms of OpaqueValueExprs bound in the semantic form. 4418 Expr *getSyntacticForm() { return getSubExprsBuffer()[0]; } 4419 const Expr *getSyntacticForm() const { return getSubExprsBuffer()[0]; } 4420 4421 /// Return the index of the result-bearing expression into the semantics 4422 /// expressions, or PseudoObjectExpr::NoResult if there is none. 4423 unsigned getResultExprIndex() const { 4424 if (PseudoObjectExprBits.ResultIndex == 0) return NoResult; 4425 return PseudoObjectExprBits.ResultIndex - 1; 4426 } 4427 4428 /// Return the result-bearing expression, or null if there is none. 4429 Expr *getResultExpr() { 4430 if (PseudoObjectExprBits.ResultIndex == 0) 4431 return 0; 4432 return getSubExprsBuffer()[PseudoObjectExprBits.ResultIndex]; 4433 } 4434 const Expr *getResultExpr() const { 4435 return const_cast<PseudoObjectExpr*>(this)->getResultExpr(); 4436 } 4437 4438 unsigned getNumSemanticExprs() const { return getNumSubExprs() - 1; } 4439 4440 typedef Expr * const *semantics_iterator; 4441 typedef const Expr * const *const_semantics_iterator; 4442 semantics_iterator semantics_begin() { 4443 return getSubExprsBuffer() + 1; 4444 } 4445 const_semantics_iterator semantics_begin() const { 4446 return getSubExprsBuffer() + 1; 4447 } 4448 semantics_iterator semantics_end() { 4449 return getSubExprsBuffer() + getNumSubExprs(); 4450 } 4451 const_semantics_iterator semantics_end() const { 4452 return getSubExprsBuffer() + getNumSubExprs(); 4453 } 4454 Expr *getSemanticExpr(unsigned index) { 4455 assert(index + 1 < getNumSubExprs()); 4456 return getSubExprsBuffer()[index + 1]; 4457 } 4458 const Expr *getSemanticExpr(unsigned index) const { 4459 return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index); 4460 } 4461 4462 SourceLocation getExprLoc() const { 4463 return getSyntacticForm()->getExprLoc(); 4464 } 4465 SourceRange getSourceRange() const { 4466 return getSyntacticForm()->getSourceRange(); 4467 } 4468 4469 child_range children() { 4470 Stmt **cs = reinterpret_cast<Stmt**>(getSubExprsBuffer()); 4471 return child_range(cs, cs + getNumSubExprs()); 4472 } 4473 4474 static bool classof(const Stmt *T) { 4475 return T->getStmtClass() == PseudoObjectExprClass; 4476 } 4477 static bool classof(const PseudoObjectExpr *) { return true; } 4478}; 4479 4480/// AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, 4481/// __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the 4482/// similarly-named C++0x instructions. All of these instructions take one 4483/// primary pointer and at least one memory order. 4484class AtomicExpr : public Expr { 4485public: 4486 enum AtomicOp { Load, Store, CmpXchgStrong, CmpXchgWeak, Xchg, 4487 Add, Sub, And, Or, Xor, Init }; 4488private: 4489 enum { PTR, ORDER, VAL1, ORDER_FAIL, VAL2, END_EXPR }; 4490 Stmt* SubExprs[END_EXPR]; 4491 unsigned NumSubExprs; 4492 SourceLocation BuiltinLoc, RParenLoc; 4493 AtomicOp Op; 4494 4495public: 4496 AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr, QualType t, 4497 AtomicOp op, SourceLocation RP); 4498 4499 /// \brief Build an empty AtomicExpr. 4500 explicit AtomicExpr(EmptyShell Empty) : Expr(AtomicExprClass, Empty) { } 4501 4502 Expr *getPtr() const { 4503 return cast<Expr>(SubExprs[PTR]); 4504 } 4505 void setPtr(Expr *E) { 4506 SubExprs[PTR] = E; 4507 } 4508 Expr *getOrder() const { 4509 return cast<Expr>(SubExprs[ORDER]); 4510 } 4511 void setOrder(Expr *E) { 4512 SubExprs[ORDER] = E; 4513 } 4514 Expr *getVal1() const { 4515 if (Op == Init) 4516 return cast<Expr>(SubExprs[ORDER]); 4517 assert(NumSubExprs >= 3); 4518 return cast<Expr>(SubExprs[VAL1]); 4519 } 4520 void setVal1(Expr *E) { 4521 if (Op == Init) { 4522 SubExprs[ORDER] = E; 4523 return; 4524 } 4525 assert(NumSubExprs >= 3); 4526 SubExprs[VAL1] = E; 4527 } 4528 Expr *getOrderFail() const { 4529 assert(NumSubExprs == 5); 4530 return cast<Expr>(SubExprs[ORDER_FAIL]); 4531 } 4532 void setOrderFail(Expr *E) { 4533 assert(NumSubExprs == 5); 4534 SubExprs[ORDER_FAIL] = E; 4535 } 4536 Expr *getVal2() const { 4537 assert(NumSubExprs == 5); 4538 return cast<Expr>(SubExprs[VAL2]); 4539 } 4540 void setVal2(Expr *E) { 4541 assert(NumSubExprs == 5); 4542 SubExprs[VAL2] = E; 4543 } 4544 4545 AtomicOp getOp() const { return Op; } 4546 void setOp(AtomicOp op) { Op = op; } 4547 unsigned getNumSubExprs() { return NumSubExprs; } 4548 void setNumSubExprs(unsigned num) { NumSubExprs = num; } 4549 4550 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 4551 4552 bool isVolatile() const { 4553 return getPtr()->getType()->getPointeeType().isVolatileQualified(); 4554 } 4555 4556 bool isCmpXChg() const { 4557 return getOp() == AtomicExpr::CmpXchgStrong || 4558 getOp() == AtomicExpr::CmpXchgWeak; 4559 } 4560 4561 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 4562 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 4563 4564 SourceLocation getRParenLoc() const { return RParenLoc; } 4565 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 4566 4567 SourceRange getSourceRange() const { 4568 return SourceRange(BuiltinLoc, RParenLoc); 4569 } 4570 static bool classof(const Stmt *T) { 4571 return T->getStmtClass() == AtomicExprClass; 4572 } 4573 static bool classof(const AtomicExpr *) { return true; } 4574 4575 // Iterators 4576 child_range children() { 4577 return child_range(SubExprs, SubExprs+NumSubExprs); 4578 } 4579}; 4580} // end namespace clang 4581 4582#endif 4583