Expr.h revision 13a140caba448a66ffcc5ff0d32a87d6e4f4ad3f
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; 1344 unsigned NumConcatenated; 1345 unsigned Kind : 3; 1346 bool IsPascal : 1; 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 /// MemberLoc - This is the location of the member name. 2161 SourceLocation MemberLoc; 2162 2163 /// MemberDNLoc - Provides source/type location info for the 2164 /// declaration name embedded in MemberDecl. 2165 DeclarationNameLoc MemberDNLoc; 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), MemberLoc(NameInfo.getLoc()), 2210 MemberDNLoc(NameInfo.getInfo()), 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), MemberLoc(l), MemberDNLoc(), 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 TypeSourceInfo *TInfo; 2434 Stmt *Init; 2435 bool FileScope; 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), TInfo(tinfo), Init(init), FileScope(fileScope) {} 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 FileScope; } 2456 void setFileScope(bool FS) { FileScope = FS; } 2457 2458 SourceLocation getLParenLoc() const { return LParenLoc; } 2459 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2460 2461 TypeSourceInfo *getTypeSourceInfo() const { return TInfo; } 2462 void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; } 2463 2464 SourceRange getSourceRange() const { 2465 // FIXME: Init should never be null. 2466 if (!Init) 2467 return SourceRange(); 2468 if (LParenLoc.isInvalid()) 2469 return Init->getSourceRange(); 2470 return SourceRange(LParenLoc, Init->getLocEnd()); 2471 } 2472 2473 static bool classof(const Stmt *T) { 2474 return T->getStmtClass() == CompoundLiteralExprClass; 2475 } 2476 static bool classof(const CompoundLiteralExpr *) { return true; } 2477 2478 // Iterators 2479 child_range children() { return child_range(&Init, &Init+1); } 2480}; 2481 2482/// CastExpr - Base class for type casts, including both implicit 2483/// casts (ImplicitCastExpr) and explicit casts that have some 2484/// representation in the source code (ExplicitCastExpr's derived 2485/// classes). 2486class CastExpr : public Expr { 2487public: 2488 typedef clang::CastKind CastKind; 2489 2490private: 2491 Stmt *Op; 2492 2493 void CheckCastConsistency() const; 2494 2495 const CXXBaseSpecifier * const *path_buffer() const { 2496 return const_cast<CastExpr*>(this)->path_buffer(); 2497 } 2498 CXXBaseSpecifier **path_buffer(); 2499 2500 void setBasePathSize(unsigned basePathSize) { 2501 CastExprBits.BasePathSize = basePathSize; 2502 assert(CastExprBits.BasePathSize == basePathSize && 2503 "basePathSize doesn't fit in bits of CastExprBits.BasePathSize!"); 2504 } 2505 2506protected: 2507 CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 2508 const CastKind kind, Expr *op, unsigned BasePathSize) : 2509 Expr(SC, ty, VK, OK_Ordinary, 2510 // Cast expressions are type-dependent if the type is 2511 // dependent (C++ [temp.dep.expr]p3). 2512 ty->isDependentType(), 2513 // Cast expressions are value-dependent if the type is 2514 // dependent or if the subexpression is value-dependent. 2515 ty->isDependentType() || (op && op->isValueDependent()), 2516 (ty->isInstantiationDependentType() || 2517 (op && op->isInstantiationDependent())), 2518 (ty->containsUnexpandedParameterPack() || 2519 op->containsUnexpandedParameterPack())), 2520 Op(op) { 2521 assert(kind != CK_Invalid && "creating cast with invalid cast kind"); 2522 CastExprBits.Kind = kind; 2523 setBasePathSize(BasePathSize); 2524#ifndef NDEBUG 2525 CheckCastConsistency(); 2526#endif 2527 } 2528 2529 /// \brief Construct an empty cast. 2530 CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize) 2531 : Expr(SC, Empty) { 2532 setBasePathSize(BasePathSize); 2533 } 2534 2535public: 2536 CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } 2537 void setCastKind(CastKind K) { CastExprBits.Kind = K; } 2538 const char *getCastKindName() const; 2539 2540 Expr *getSubExpr() { return cast<Expr>(Op); } 2541 const Expr *getSubExpr() const { return cast<Expr>(Op); } 2542 void setSubExpr(Expr *E) { Op = E; } 2543 2544 /// \brief Retrieve the cast subexpression as it was written in the source 2545 /// code, looking through any implicit casts or other intermediate nodes 2546 /// introduced by semantic analysis. 2547 Expr *getSubExprAsWritten(); 2548 const Expr *getSubExprAsWritten() const { 2549 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 2550 } 2551 2552 typedef CXXBaseSpecifier **path_iterator; 2553 typedef const CXXBaseSpecifier * const *path_const_iterator; 2554 bool path_empty() const { return CastExprBits.BasePathSize == 0; } 2555 unsigned path_size() const { return CastExprBits.BasePathSize; } 2556 path_iterator path_begin() { return path_buffer(); } 2557 path_iterator path_end() { return path_buffer() + path_size(); } 2558 path_const_iterator path_begin() const { return path_buffer(); } 2559 path_const_iterator path_end() const { return path_buffer() + path_size(); } 2560 2561 void setCastPath(const CXXCastPath &Path); 2562 2563 static bool classof(const Stmt *T) { 2564 return T->getStmtClass() >= firstCastExprConstant && 2565 T->getStmtClass() <= lastCastExprConstant; 2566 } 2567 static bool classof(const CastExpr *) { return true; } 2568 2569 // Iterators 2570 child_range children() { return child_range(&Op, &Op+1); } 2571}; 2572 2573/// ImplicitCastExpr - Allows us to explicitly represent implicit type 2574/// conversions, which have no direct representation in the original 2575/// source code. For example: converting T[]->T*, void f()->void 2576/// (*f)(), float->double, short->int, etc. 2577/// 2578/// In C, implicit casts always produce rvalues. However, in C++, an 2579/// implicit cast whose result is being bound to a reference will be 2580/// an lvalue or xvalue. For example: 2581/// 2582/// @code 2583/// class Base { }; 2584/// class Derived : public Base { }; 2585/// Derived &&ref(); 2586/// void f(Derived d) { 2587/// Base& b = d; // initializer is an ImplicitCastExpr 2588/// // to an lvalue of type Base 2589/// Base&& r = ref(); // initializer is an ImplicitCastExpr 2590/// // to an xvalue of type Base 2591/// } 2592/// @endcode 2593class ImplicitCastExpr : public CastExpr { 2594private: 2595 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 2596 unsigned BasePathLength, ExprValueKind VK) 2597 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength) { 2598 } 2599 2600 /// \brief Construct an empty implicit cast. 2601 explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize) 2602 : CastExpr(ImplicitCastExprClass, Shell, PathSize) { } 2603 2604public: 2605 enum OnStack_t { OnStack }; 2606 ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, 2607 ExprValueKind VK) 2608 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0) { 2609 } 2610 2611 static ImplicitCastExpr *Create(ASTContext &Context, QualType T, 2612 CastKind Kind, Expr *Operand, 2613 const CXXCastPath *BasePath, 2614 ExprValueKind Cat); 2615 2616 static ImplicitCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2617 2618 SourceRange getSourceRange() const { 2619 return getSubExpr()->getSourceRange(); 2620 } 2621 2622 static bool classof(const Stmt *T) { 2623 return T->getStmtClass() == ImplicitCastExprClass; 2624 } 2625 static bool classof(const ImplicitCastExpr *) { return true; } 2626}; 2627 2628inline Expr *Expr::IgnoreImpCasts() { 2629 Expr *e = this; 2630 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) 2631 e = ice->getSubExpr(); 2632 return e; 2633} 2634 2635/// ExplicitCastExpr - An explicit cast written in the source 2636/// code. 2637/// 2638/// This class is effectively an abstract class, because it provides 2639/// the basic representation of an explicitly-written cast without 2640/// specifying which kind of cast (C cast, functional cast, static 2641/// cast, etc.) was written; specific derived classes represent the 2642/// particular style of cast and its location information. 2643/// 2644/// Unlike implicit casts, explicit cast nodes have two different 2645/// types: the type that was written into the source code, and the 2646/// actual type of the expression as determined by semantic 2647/// analysis. These types may differ slightly. For example, in C++ one 2648/// can cast to a reference type, which indicates that the resulting 2649/// expression will be an lvalue or xvalue. The reference type, however, 2650/// will not be used as the type of the expression. 2651class ExplicitCastExpr : public CastExpr { 2652 /// TInfo - Source type info for the (written) type 2653 /// this expression is casting to. 2654 TypeSourceInfo *TInfo; 2655 2656protected: 2657 ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, 2658 CastKind kind, Expr *op, unsigned PathSize, 2659 TypeSourceInfo *writtenTy) 2660 : CastExpr(SC, exprTy, VK, kind, op, PathSize), TInfo(writtenTy) {} 2661 2662 /// \brief Construct an empty explicit cast. 2663 ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 2664 : CastExpr(SC, Shell, PathSize) { } 2665 2666public: 2667 /// getTypeInfoAsWritten - Returns the type source info for the type 2668 /// that this expression is casting to. 2669 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 2670 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 2671 2672 /// getTypeAsWritten - Returns the type that this expression is 2673 /// casting to, as written in the source code. 2674 QualType getTypeAsWritten() const { return TInfo->getType(); } 2675 2676 static bool classof(const Stmt *T) { 2677 return T->getStmtClass() >= firstExplicitCastExprConstant && 2678 T->getStmtClass() <= lastExplicitCastExprConstant; 2679 } 2680 static bool classof(const ExplicitCastExpr *) { return true; } 2681}; 2682 2683/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 2684/// cast in C++ (C++ [expr.cast]), which uses the syntax 2685/// (Type)expr. For example: @c (int)f. 2686class CStyleCastExpr : public ExplicitCastExpr { 2687 SourceLocation LPLoc; // the location of the left paren 2688 SourceLocation RPLoc; // the location of the right paren 2689 2690 CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, 2691 unsigned PathSize, TypeSourceInfo *writtenTy, 2692 SourceLocation l, SourceLocation r) 2693 : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, 2694 writtenTy), LPLoc(l), RPLoc(r) {} 2695 2696 /// \brief Construct an empty C-style explicit cast. 2697 explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize) 2698 : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { } 2699 2700public: 2701 static CStyleCastExpr *Create(ASTContext &Context, QualType T, 2702 ExprValueKind VK, CastKind K, 2703 Expr *Op, const CXXCastPath *BasePath, 2704 TypeSourceInfo *WrittenTy, SourceLocation L, 2705 SourceLocation R); 2706 2707 static CStyleCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2708 2709 SourceLocation getLParenLoc() const { return LPLoc; } 2710 void setLParenLoc(SourceLocation L) { LPLoc = L; } 2711 2712 SourceLocation getRParenLoc() const { return RPLoc; } 2713 void setRParenLoc(SourceLocation L) { RPLoc = L; } 2714 2715 SourceRange getSourceRange() const { 2716 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 2717 } 2718 static bool classof(const Stmt *T) { 2719 return T->getStmtClass() == CStyleCastExprClass; 2720 } 2721 static bool classof(const CStyleCastExpr *) { return true; } 2722}; 2723 2724/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 2725/// 2726/// This expression node kind describes a builtin binary operation, 2727/// such as "x + y" for integer values "x" and "y". The operands will 2728/// already have been converted to appropriate types (e.g., by 2729/// performing promotions or conversions). 2730/// 2731/// In C++, where operators may be overloaded, a different kind of 2732/// expression node (CXXOperatorCallExpr) is used to express the 2733/// invocation of an overloaded operator with operator syntax. Within 2734/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 2735/// used to store an expression "x + y" depends on the subexpressions 2736/// for x and y. If neither x or y is type-dependent, and the "+" 2737/// operator resolves to a built-in operation, BinaryOperator will be 2738/// used to express the computation (x and y may still be 2739/// value-dependent). If either x or y is type-dependent, or if the 2740/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 2741/// be used to express the computation. 2742class BinaryOperator : public Expr { 2743public: 2744 typedef BinaryOperatorKind Opcode; 2745 2746private: 2747 unsigned Opc : 6; 2748 SourceLocation OpLoc; 2749 2750 enum { LHS, RHS, END_EXPR }; 2751 Stmt* SubExprs[END_EXPR]; 2752public: 2753 2754 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2755 ExprValueKind VK, ExprObjectKind OK, 2756 SourceLocation opLoc) 2757 : Expr(BinaryOperatorClass, ResTy, VK, OK, 2758 lhs->isTypeDependent() || rhs->isTypeDependent(), 2759 lhs->isValueDependent() || rhs->isValueDependent(), 2760 (lhs->isInstantiationDependent() || 2761 rhs->isInstantiationDependent()), 2762 (lhs->containsUnexpandedParameterPack() || 2763 rhs->containsUnexpandedParameterPack())), 2764 Opc(opc), OpLoc(opLoc) { 2765 SubExprs[LHS] = lhs; 2766 SubExprs[RHS] = rhs; 2767 assert(!isCompoundAssignmentOp() && 2768 "Use ArithAssignBinaryOperator for compound assignments"); 2769 } 2770 2771 /// \brief Construct an empty binary operator. 2772 explicit BinaryOperator(EmptyShell Empty) 2773 : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { } 2774 2775 SourceLocation getExprLoc() const { return OpLoc; } 2776 SourceLocation getOperatorLoc() const { return OpLoc; } 2777 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 2778 2779 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 2780 void setOpcode(Opcode O) { Opc = O; } 2781 2782 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2783 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2784 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2785 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2786 2787 SourceRange getSourceRange() const { 2788 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 2789 } 2790 2791 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 2792 /// corresponds to, e.g. "<<=". 2793 static const char *getOpcodeStr(Opcode Op); 2794 2795 const char *getOpcodeStr() const { return getOpcodeStr(getOpcode()); } 2796 2797 /// \brief Retrieve the binary opcode that corresponds to the given 2798 /// overloaded operator. 2799 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 2800 2801 /// \brief Retrieve the overloaded operator kind that corresponds to 2802 /// the given binary opcode. 2803 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 2804 2805 /// predicates to categorize the respective opcodes. 2806 bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; } 2807 bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; } 2808 static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } 2809 bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } 2810 static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } 2811 bool isShiftOp() const { return isShiftOp(getOpcode()); } 2812 2813 static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } 2814 bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } 2815 2816 static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } 2817 bool isRelationalOp() const { return isRelationalOp(getOpcode()); } 2818 2819 static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } 2820 bool isEqualityOp() const { return isEqualityOp(getOpcode()); } 2821 2822 static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; } 2823 bool isComparisonOp() const { return isComparisonOp(getOpcode()); } 2824 2825 static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } 2826 bool isLogicalOp() const { return isLogicalOp(getOpcode()); } 2827 2828 static bool isAssignmentOp(Opcode Opc) { 2829 return Opc >= BO_Assign && Opc <= BO_OrAssign; 2830 } 2831 bool isAssignmentOp() const { return isAssignmentOp(getOpcode()); } 2832 2833 static bool isCompoundAssignmentOp(Opcode Opc) { 2834 return Opc > BO_Assign && Opc <= BO_OrAssign; 2835 } 2836 bool isCompoundAssignmentOp() const { 2837 return isCompoundAssignmentOp(getOpcode()); 2838 } 2839 static Opcode getOpForCompoundAssignment(Opcode Opc) { 2840 assert(isCompoundAssignmentOp(Opc)); 2841 if (Opc >= BO_AndAssign) 2842 return Opcode(unsigned(Opc) - BO_AndAssign + BO_And); 2843 else 2844 return Opcode(unsigned(Opc) - BO_MulAssign + BO_Mul); 2845 } 2846 2847 static bool isShiftAssignOp(Opcode Opc) { 2848 return Opc == BO_ShlAssign || Opc == BO_ShrAssign; 2849 } 2850 bool isShiftAssignOp() const { 2851 return isShiftAssignOp(getOpcode()); 2852 } 2853 2854 static bool classof(const Stmt *S) { 2855 return S->getStmtClass() >= firstBinaryOperatorConstant && 2856 S->getStmtClass() <= lastBinaryOperatorConstant; 2857 } 2858 static bool classof(const BinaryOperator *) { return true; } 2859 2860 // Iterators 2861 child_range children() { 2862 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2863 } 2864 2865protected: 2866 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2867 ExprValueKind VK, ExprObjectKind OK, 2868 SourceLocation opLoc, bool dead) 2869 : Expr(CompoundAssignOperatorClass, ResTy, VK, OK, 2870 lhs->isTypeDependent() || rhs->isTypeDependent(), 2871 lhs->isValueDependent() || rhs->isValueDependent(), 2872 (lhs->isInstantiationDependent() || 2873 rhs->isInstantiationDependent()), 2874 (lhs->containsUnexpandedParameterPack() || 2875 rhs->containsUnexpandedParameterPack())), 2876 Opc(opc), OpLoc(opLoc) { 2877 SubExprs[LHS] = lhs; 2878 SubExprs[RHS] = rhs; 2879 } 2880 2881 BinaryOperator(StmtClass SC, EmptyShell Empty) 2882 : Expr(SC, Empty), Opc(BO_MulAssign) { } 2883}; 2884 2885/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 2886/// track of the type the operation is performed in. Due to the semantics of 2887/// these operators, the operands are promoted, the arithmetic performed, an 2888/// implicit conversion back to the result type done, then the assignment takes 2889/// place. This captures the intermediate type which the computation is done 2890/// in. 2891class CompoundAssignOperator : public BinaryOperator { 2892 QualType ComputationLHSType; 2893 QualType ComputationResultType; 2894public: 2895 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResType, 2896 ExprValueKind VK, ExprObjectKind OK, 2897 QualType CompLHSType, QualType CompResultType, 2898 SourceLocation OpLoc) 2899 : BinaryOperator(lhs, rhs, opc, ResType, VK, OK, OpLoc, true), 2900 ComputationLHSType(CompLHSType), 2901 ComputationResultType(CompResultType) { 2902 assert(isCompoundAssignmentOp() && 2903 "Only should be used for compound assignments"); 2904 } 2905 2906 /// \brief Build an empty compound assignment operator expression. 2907 explicit CompoundAssignOperator(EmptyShell Empty) 2908 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 2909 2910 // The two computation types are the type the LHS is converted 2911 // to for the computation and the type of the result; the two are 2912 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 2913 QualType getComputationLHSType() const { return ComputationLHSType; } 2914 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 2915 2916 QualType getComputationResultType() const { return ComputationResultType; } 2917 void setComputationResultType(QualType T) { ComputationResultType = T; } 2918 2919 static bool classof(const CompoundAssignOperator *) { return true; } 2920 static bool classof(const Stmt *S) { 2921 return S->getStmtClass() == CompoundAssignOperatorClass; 2922 } 2923}; 2924 2925/// AbstractConditionalOperator - An abstract base class for 2926/// ConditionalOperator and BinaryConditionalOperator. 2927class AbstractConditionalOperator : public Expr { 2928 SourceLocation QuestionLoc, ColonLoc; 2929 friend class ASTStmtReader; 2930 2931protected: 2932 AbstractConditionalOperator(StmtClass SC, QualType T, 2933 ExprValueKind VK, ExprObjectKind OK, 2934 bool TD, bool VD, bool ID, 2935 bool ContainsUnexpandedParameterPack, 2936 SourceLocation qloc, 2937 SourceLocation cloc) 2938 : Expr(SC, T, VK, OK, TD, VD, ID, ContainsUnexpandedParameterPack), 2939 QuestionLoc(qloc), ColonLoc(cloc) {} 2940 2941 AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) 2942 : Expr(SC, Empty) { } 2943 2944public: 2945 // getCond - Return the expression representing the condition for 2946 // the ?: operator. 2947 Expr *getCond() const; 2948 2949 // getTrueExpr - Return the subexpression representing the value of 2950 // the expression if the condition evaluates to true. 2951 Expr *getTrueExpr() const; 2952 2953 // getFalseExpr - Return the subexpression representing the value of 2954 // the expression if the condition evaluates to false. This is 2955 // the same as getRHS. 2956 Expr *getFalseExpr() const; 2957 2958 SourceLocation getQuestionLoc() const { return QuestionLoc; } 2959 SourceLocation getColonLoc() const { return ColonLoc; } 2960 2961 static bool classof(const Stmt *T) { 2962 return T->getStmtClass() == ConditionalOperatorClass || 2963 T->getStmtClass() == BinaryConditionalOperatorClass; 2964 } 2965 static bool classof(const AbstractConditionalOperator *) { return true; } 2966}; 2967 2968/// ConditionalOperator - The ?: ternary operator. The GNU "missing 2969/// middle" extension is a BinaryConditionalOperator. 2970class ConditionalOperator : public AbstractConditionalOperator { 2971 enum { COND, LHS, RHS, END_EXPR }; 2972 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2973 2974 friend class ASTStmtReader; 2975public: 2976 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 2977 SourceLocation CLoc, Expr *rhs, 2978 QualType t, ExprValueKind VK, ExprObjectKind OK) 2979 : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, 2980 // FIXME: the type of the conditional operator doesn't 2981 // depend on the type of the conditional, but the standard 2982 // seems to imply that it could. File a bug! 2983 (lhs->isTypeDependent() || rhs->isTypeDependent()), 2984 (cond->isValueDependent() || lhs->isValueDependent() || 2985 rhs->isValueDependent()), 2986 (cond->isInstantiationDependent() || 2987 lhs->isInstantiationDependent() || 2988 rhs->isInstantiationDependent()), 2989 (cond->containsUnexpandedParameterPack() || 2990 lhs->containsUnexpandedParameterPack() || 2991 rhs->containsUnexpandedParameterPack()), 2992 QLoc, CLoc) { 2993 SubExprs[COND] = cond; 2994 SubExprs[LHS] = lhs; 2995 SubExprs[RHS] = rhs; 2996 } 2997 2998 /// \brief Build an empty conditional operator. 2999 explicit ConditionalOperator(EmptyShell Empty) 3000 : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } 3001 3002 // getCond - Return the expression representing the condition for 3003 // the ?: operator. 3004 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3005 3006 // getTrueExpr - Return the subexpression representing the value of 3007 // the expression if the condition evaluates to true. 3008 Expr *getTrueExpr() const { return cast<Expr>(SubExprs[LHS]); } 3009 3010 // getFalseExpr - Return the subexpression representing the value of 3011 // the expression if the condition evaluates to false. This is 3012 // the same as getRHS. 3013 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 3014 3015 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 3016 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 3017 3018 SourceRange getSourceRange() const { 3019 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 3020 } 3021 static bool classof(const Stmt *T) { 3022 return T->getStmtClass() == ConditionalOperatorClass; 3023 } 3024 static bool classof(const ConditionalOperator *) { return true; } 3025 3026 // Iterators 3027 child_range children() { 3028 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 3029 } 3030}; 3031 3032/// BinaryConditionalOperator - The GNU extension to the conditional 3033/// operator which allows the middle operand to be omitted. 3034/// 3035/// This is a different expression kind on the assumption that almost 3036/// every client ends up needing to know that these are different. 3037class BinaryConditionalOperator : public AbstractConditionalOperator { 3038 enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; 3039 3040 /// - the common condition/left-hand-side expression, which will be 3041 /// evaluated as the opaque value 3042 /// - the condition, expressed in terms of the opaque value 3043 /// - the left-hand-side, expressed in terms of the opaque value 3044 /// - the right-hand-side 3045 Stmt *SubExprs[NUM_SUBEXPRS]; 3046 OpaqueValueExpr *OpaqueValue; 3047 3048 friend class ASTStmtReader; 3049public: 3050 BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, 3051 Expr *cond, Expr *lhs, Expr *rhs, 3052 SourceLocation qloc, SourceLocation cloc, 3053 QualType t, ExprValueKind VK, ExprObjectKind OK) 3054 : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, 3055 (common->isTypeDependent() || rhs->isTypeDependent()), 3056 (common->isValueDependent() || rhs->isValueDependent()), 3057 (common->isInstantiationDependent() || 3058 rhs->isInstantiationDependent()), 3059 (common->containsUnexpandedParameterPack() || 3060 rhs->containsUnexpandedParameterPack()), 3061 qloc, cloc), 3062 OpaqueValue(opaqueValue) { 3063 SubExprs[COMMON] = common; 3064 SubExprs[COND] = cond; 3065 SubExprs[LHS] = lhs; 3066 SubExprs[RHS] = rhs; 3067 assert(OpaqueValue->getSourceExpr() == common && "Wrong opaque value"); 3068 } 3069 3070 /// \brief Build an empty conditional operator. 3071 explicit BinaryConditionalOperator(EmptyShell Empty) 3072 : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } 3073 3074 /// \brief getCommon - Return the common expression, written to the 3075 /// left of the condition. The opaque value will be bound to the 3076 /// result of this expression. 3077 Expr *getCommon() const { return cast<Expr>(SubExprs[COMMON]); } 3078 3079 /// \brief getOpaqueValue - Return the opaque value placeholder. 3080 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } 3081 3082 /// \brief getCond - Return the condition expression; this is defined 3083 /// in terms of the opaque value. 3084 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3085 3086 /// \brief getTrueExpr - Return the subexpression which will be 3087 /// evaluated if the condition evaluates to true; this is defined 3088 /// in terms of the opaque value. 3089 Expr *getTrueExpr() const { 3090 return cast<Expr>(SubExprs[LHS]); 3091 } 3092 3093 /// \brief getFalseExpr - Return the subexpression which will be 3094 /// evaluated if the condnition evaluates to false; this is 3095 /// defined in terms of the opaque value. 3096 Expr *getFalseExpr() const { 3097 return cast<Expr>(SubExprs[RHS]); 3098 } 3099 3100 SourceRange getSourceRange() const { 3101 return SourceRange(getCommon()->getLocStart(), getFalseExpr()->getLocEnd()); 3102 } 3103 static bool classof(const Stmt *T) { 3104 return T->getStmtClass() == BinaryConditionalOperatorClass; 3105 } 3106 static bool classof(const BinaryConditionalOperator *) { return true; } 3107 3108 // Iterators 3109 child_range children() { 3110 return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); 3111 } 3112}; 3113 3114inline Expr *AbstractConditionalOperator::getCond() const { 3115 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3116 return co->getCond(); 3117 return cast<BinaryConditionalOperator>(this)->getCond(); 3118} 3119 3120inline Expr *AbstractConditionalOperator::getTrueExpr() const { 3121 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3122 return co->getTrueExpr(); 3123 return cast<BinaryConditionalOperator>(this)->getTrueExpr(); 3124} 3125 3126inline Expr *AbstractConditionalOperator::getFalseExpr() const { 3127 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 3128 return co->getFalseExpr(); 3129 return cast<BinaryConditionalOperator>(this)->getFalseExpr(); 3130} 3131 3132/// AddrLabelExpr - The GNU address of label extension, representing &&label. 3133class AddrLabelExpr : public Expr { 3134 SourceLocation AmpAmpLoc, LabelLoc; 3135 LabelDecl *Label; 3136public: 3137 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, 3138 QualType t) 3139 : Expr(AddrLabelExprClass, t, VK_RValue, OK_Ordinary, false, false, false, 3140 false), 3141 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 3142 3143 /// \brief Build an empty address of a label expression. 3144 explicit AddrLabelExpr(EmptyShell Empty) 3145 : Expr(AddrLabelExprClass, Empty) { } 3146 3147 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 3148 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 3149 SourceLocation getLabelLoc() const { return LabelLoc; } 3150 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 3151 3152 SourceRange getSourceRange() const { 3153 return SourceRange(AmpAmpLoc, LabelLoc); 3154 } 3155 3156 LabelDecl *getLabel() const { return Label; } 3157 void setLabel(LabelDecl *L) { Label = L; } 3158 3159 static bool classof(const Stmt *T) { 3160 return T->getStmtClass() == AddrLabelExprClass; 3161 } 3162 static bool classof(const AddrLabelExpr *) { return true; } 3163 3164 // Iterators 3165 child_range children() { return child_range(); } 3166}; 3167 3168/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 3169/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 3170/// takes the value of the last subexpression. 3171/// 3172/// A StmtExpr is always an r-value; values "returned" out of a 3173/// StmtExpr will be copied. 3174class StmtExpr : public Expr { 3175 Stmt *SubStmt; 3176 SourceLocation LParenLoc, RParenLoc; 3177public: 3178 // FIXME: Does type-dependence need to be computed differently? 3179 // FIXME: Do we need to compute instantiation instantiation-dependence for 3180 // statements? (ugh!) 3181 StmtExpr(CompoundStmt *substmt, QualType T, 3182 SourceLocation lp, SourceLocation rp) : 3183 Expr(StmtExprClass, T, VK_RValue, OK_Ordinary, 3184 T->isDependentType(), false, false, false), 3185 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 3186 3187 /// \brief Build an empty statement expression. 3188 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 3189 3190 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 3191 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 3192 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 3193 3194 SourceRange getSourceRange() const { 3195 return SourceRange(LParenLoc, RParenLoc); 3196 } 3197 3198 SourceLocation getLParenLoc() const { return LParenLoc; } 3199 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 3200 SourceLocation getRParenLoc() const { return RParenLoc; } 3201 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3202 3203 static bool classof(const Stmt *T) { 3204 return T->getStmtClass() == StmtExprClass; 3205 } 3206 static bool classof(const StmtExpr *) { return true; } 3207 3208 // Iterators 3209 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 3210}; 3211 3212 3213/// ShuffleVectorExpr - clang-specific builtin-in function 3214/// __builtin_shufflevector. 3215/// This AST node represents a operator that does a constant 3216/// shuffle, similar to LLVM's shufflevector instruction. It takes 3217/// two vectors and a variable number of constant indices, 3218/// and returns the appropriately shuffled vector. 3219class ShuffleVectorExpr : public Expr { 3220 SourceLocation BuiltinLoc, RParenLoc; 3221 3222 // SubExprs - the list of values passed to the __builtin_shufflevector 3223 // function. The first two are vectors, and the rest are constant 3224 // indices. The number of values in this list is always 3225 // 2+the number of indices in the vector type. 3226 Stmt **SubExprs; 3227 unsigned NumExprs; 3228 3229public: 3230 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 3231 QualType Type, SourceLocation BLoc, 3232 SourceLocation RP); 3233 3234 /// \brief Build an empty vector-shuffle expression. 3235 explicit ShuffleVectorExpr(EmptyShell Empty) 3236 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 3237 3238 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3239 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3240 3241 SourceLocation getRParenLoc() const { return RParenLoc; } 3242 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3243 3244 SourceRange getSourceRange() const { 3245 return SourceRange(BuiltinLoc, RParenLoc); 3246 } 3247 static bool classof(const Stmt *T) { 3248 return T->getStmtClass() == ShuffleVectorExprClass; 3249 } 3250 static bool classof(const ShuffleVectorExpr *) { return true; } 3251 3252 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 3253 /// constant expression, the actual arguments passed in, and the function 3254 /// pointers. 3255 unsigned getNumSubExprs() const { return NumExprs; } 3256 3257 /// \brief Retrieve the array of expressions. 3258 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 3259 3260 /// getExpr - Return the Expr at the specified index. 3261 Expr *getExpr(unsigned Index) { 3262 assert((Index < NumExprs) && "Arg access out of range!"); 3263 return cast<Expr>(SubExprs[Index]); 3264 } 3265 const Expr *getExpr(unsigned Index) const { 3266 assert((Index < NumExprs) && "Arg access out of range!"); 3267 return cast<Expr>(SubExprs[Index]); 3268 } 3269 3270 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 3271 3272 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 3273 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 3274 return getExpr(N+2)->EvaluateKnownConstInt(Ctx).getZExtValue(); 3275 } 3276 3277 // Iterators 3278 child_range children() { 3279 return child_range(&SubExprs[0], &SubExprs[0]+NumExprs); 3280 } 3281}; 3282 3283/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 3284/// This AST node is similar to the conditional operator (?:) in C, with 3285/// the following exceptions: 3286/// - the test expression must be a integer constant expression. 3287/// - the expression returned acts like the chosen subexpression in every 3288/// visible way: the type is the same as that of the chosen subexpression, 3289/// and all predicates (whether it's an l-value, whether it's an integer 3290/// constant expression, etc.) return the same result as for the chosen 3291/// sub-expression. 3292class ChooseExpr : public Expr { 3293 enum { COND, LHS, RHS, END_EXPR }; 3294 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 3295 SourceLocation BuiltinLoc, RParenLoc; 3296public: 3297 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, 3298 QualType t, ExprValueKind VK, ExprObjectKind OK, 3299 SourceLocation RP, bool TypeDependent, bool ValueDependent) 3300 : Expr(ChooseExprClass, t, VK, OK, TypeDependent, ValueDependent, 3301 (cond->isInstantiationDependent() || 3302 lhs->isInstantiationDependent() || 3303 rhs->isInstantiationDependent()), 3304 (cond->containsUnexpandedParameterPack() || 3305 lhs->containsUnexpandedParameterPack() || 3306 rhs->containsUnexpandedParameterPack())), 3307 BuiltinLoc(BLoc), RParenLoc(RP) { 3308 SubExprs[COND] = cond; 3309 SubExprs[LHS] = lhs; 3310 SubExprs[RHS] = rhs; 3311 } 3312 3313 /// \brief Build an empty __builtin_choose_expr. 3314 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 3315 3316 /// isConditionTrue - Return whether the condition is true (i.e. not 3317 /// equal to zero). 3318 bool isConditionTrue(const ASTContext &C) const; 3319 3320 /// getChosenSubExpr - Return the subexpression chosen according to the 3321 /// condition. 3322 Expr *getChosenSubExpr(const ASTContext &C) const { 3323 return isConditionTrue(C) ? getLHS() : getRHS(); 3324 } 3325 3326 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3327 void setCond(Expr *E) { SubExprs[COND] = E; } 3328 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 3329 void setLHS(Expr *E) { SubExprs[LHS] = E; } 3330 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 3331 void setRHS(Expr *E) { SubExprs[RHS] = E; } 3332 3333 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3334 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3335 3336 SourceLocation getRParenLoc() const { return RParenLoc; } 3337 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3338 3339 SourceRange getSourceRange() const { 3340 return SourceRange(BuiltinLoc, RParenLoc); 3341 } 3342 static bool classof(const Stmt *T) { 3343 return T->getStmtClass() == ChooseExprClass; 3344 } 3345 static bool classof(const ChooseExpr *) { return true; } 3346 3347 // Iterators 3348 child_range children() { 3349 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 3350 } 3351}; 3352 3353/// GNUNullExpr - Implements the GNU __null extension, which is a name 3354/// for a null pointer constant that has integral type (e.g., int or 3355/// long) and is the same size and alignment as a pointer. The __null 3356/// extension is typically only used by system headers, which define 3357/// NULL as __null in C++ rather than using 0 (which is an integer 3358/// that may not match the size of a pointer). 3359class GNUNullExpr : public Expr { 3360 /// TokenLoc - The location of the __null keyword. 3361 SourceLocation TokenLoc; 3362 3363public: 3364 GNUNullExpr(QualType Ty, SourceLocation Loc) 3365 : Expr(GNUNullExprClass, Ty, VK_RValue, OK_Ordinary, false, false, false, 3366 false), 3367 TokenLoc(Loc) { } 3368 3369 /// \brief Build an empty GNU __null expression. 3370 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 3371 3372 /// getTokenLocation - The location of the __null token. 3373 SourceLocation getTokenLocation() const { return TokenLoc; } 3374 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 3375 3376 SourceRange getSourceRange() const { 3377 return SourceRange(TokenLoc); 3378 } 3379 static bool classof(const Stmt *T) { 3380 return T->getStmtClass() == GNUNullExprClass; 3381 } 3382 static bool classof(const GNUNullExpr *) { return true; } 3383 3384 // Iterators 3385 child_range children() { return child_range(); } 3386}; 3387 3388/// VAArgExpr, used for the builtin function __builtin_va_arg. 3389class VAArgExpr : public Expr { 3390 Stmt *Val; 3391 TypeSourceInfo *TInfo; 3392 SourceLocation BuiltinLoc, RParenLoc; 3393public: 3394 VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo, 3395 SourceLocation RPLoc, QualType t) 3396 : Expr(VAArgExprClass, t, VK_RValue, OK_Ordinary, 3397 t->isDependentType(), false, 3398 (TInfo->getType()->isInstantiationDependentType() || 3399 e->isInstantiationDependent()), 3400 (TInfo->getType()->containsUnexpandedParameterPack() || 3401 e->containsUnexpandedParameterPack())), 3402 Val(e), TInfo(TInfo), 3403 BuiltinLoc(BLoc), 3404 RParenLoc(RPLoc) { } 3405 3406 /// \brief Create an empty __builtin_va_arg expression. 3407 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 3408 3409 const Expr *getSubExpr() const { return cast<Expr>(Val); } 3410 Expr *getSubExpr() { return cast<Expr>(Val); } 3411 void setSubExpr(Expr *E) { Val = E; } 3412 3413 TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; } 3414 void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; } 3415 3416 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3417 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3418 3419 SourceLocation getRParenLoc() const { return RParenLoc; } 3420 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3421 3422 SourceRange getSourceRange() const { 3423 return SourceRange(BuiltinLoc, RParenLoc); 3424 } 3425 static bool classof(const Stmt *T) { 3426 return T->getStmtClass() == VAArgExprClass; 3427 } 3428 static bool classof(const VAArgExpr *) { return true; } 3429 3430 // Iterators 3431 child_range children() { return child_range(&Val, &Val+1); } 3432}; 3433 3434/// @brief Describes an C or C++ initializer list. 3435/// 3436/// InitListExpr describes an initializer list, which can be used to 3437/// initialize objects of different types, including 3438/// struct/class/union types, arrays, and vectors. For example: 3439/// 3440/// @code 3441/// struct foo x = { 1, { 2, 3 } }; 3442/// @endcode 3443/// 3444/// Prior to semantic analysis, an initializer list will represent the 3445/// initializer list as written by the user, but will have the 3446/// placeholder type "void". This initializer list is called the 3447/// syntactic form of the initializer, and may contain C99 designated 3448/// initializers (represented as DesignatedInitExprs), initializations 3449/// of subobject members without explicit braces, and so on. Clients 3450/// interested in the original syntax of the initializer list should 3451/// use the syntactic form of the initializer list. 3452/// 3453/// After semantic analysis, the initializer list will represent the 3454/// semantic form of the initializer, where the initializations of all 3455/// subobjects are made explicit with nested InitListExpr nodes and 3456/// C99 designators have been eliminated by placing the designated 3457/// initializations into the subobject they initialize. Additionally, 3458/// any "holes" in the initialization, where no initializer has been 3459/// specified for a particular subobject, will be replaced with 3460/// implicitly-generated ImplicitValueInitExpr expressions that 3461/// value-initialize the subobjects. Note, however, that the 3462/// initializer lists may still have fewer initializers than there are 3463/// elements to initialize within the object. 3464/// 3465/// Given the semantic form of the initializer list, one can retrieve 3466/// the original syntactic form of that initializer list (if it 3467/// exists) using getSyntacticForm(). Since many initializer lists 3468/// have the same syntactic and semantic forms, getSyntacticForm() may 3469/// return NULL, indicating that the current initializer list also 3470/// serves as its syntactic form. 3471class InitListExpr : public Expr { 3472 // FIXME: Eliminate this vector in favor of ASTContext allocation 3473 typedef ASTVector<Stmt *> InitExprsTy; 3474 InitExprsTy InitExprs; 3475 SourceLocation LBraceLoc, RBraceLoc; 3476 3477 /// Contains the initializer list that describes the syntactic form 3478 /// written in the source code. 3479 InitListExpr *SyntacticForm; 3480 3481 /// \brief Either: 3482 /// If this initializer list initializes an array with more elements than 3483 /// there are initializers in the list, specifies an expression to be used 3484 /// for value initialization of the rest of the elements. 3485 /// Or 3486 /// If this initializer list initializes a union, specifies which 3487 /// field within the union will be initialized. 3488 llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; 3489 3490public: 3491 InitListExpr(ASTContext &C, SourceLocation lbraceloc, 3492 Expr **initexprs, unsigned numinits, 3493 SourceLocation rbraceloc); 3494 3495 /// \brief Build an empty initializer list. 3496 explicit InitListExpr(ASTContext &C, EmptyShell Empty) 3497 : Expr(InitListExprClass, Empty), InitExprs(C) { } 3498 3499 unsigned getNumInits() const { return InitExprs.size(); } 3500 3501 /// \brief Retrieve the set of initializers. 3502 Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } 3503 3504 const Expr *getInit(unsigned Init) const { 3505 assert(Init < getNumInits() && "Initializer access out of range!"); 3506 return cast_or_null<Expr>(InitExprs[Init]); 3507 } 3508 3509 Expr *getInit(unsigned Init) { 3510 assert(Init < getNumInits() && "Initializer access out of range!"); 3511 return cast_or_null<Expr>(InitExprs[Init]); 3512 } 3513 3514 void setInit(unsigned Init, Expr *expr) { 3515 assert(Init < getNumInits() && "Initializer access out of range!"); 3516 InitExprs[Init] = expr; 3517 } 3518 3519 /// \brief Reserve space for some number of initializers. 3520 void reserveInits(ASTContext &C, unsigned NumInits); 3521 3522 /// @brief Specify the number of initializers 3523 /// 3524 /// If there are more than @p NumInits initializers, the remaining 3525 /// initializers will be destroyed. If there are fewer than @p 3526 /// NumInits initializers, NULL expressions will be added for the 3527 /// unknown initializers. 3528 void resizeInits(ASTContext &Context, unsigned NumInits); 3529 3530 /// @brief Updates the initializer at index @p Init with the new 3531 /// expression @p expr, and returns the old expression at that 3532 /// location. 3533 /// 3534 /// When @p Init is out of range for this initializer list, the 3535 /// initializer list will be extended with NULL expressions to 3536 /// accommodate the new entry. 3537 Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr); 3538 3539 /// \brief If this initializer list initializes an array with more elements 3540 /// than there are initializers in the list, specifies an expression to be 3541 /// used for value initialization of the rest of the elements. 3542 Expr *getArrayFiller() { 3543 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>(); 3544 } 3545 const Expr *getArrayFiller() const { 3546 return const_cast<InitListExpr *>(this)->getArrayFiller(); 3547 } 3548 void setArrayFiller(Expr *filler); 3549 3550 /// \brief Return true if this is an array initializer and its array "filler" 3551 /// has been set. 3552 bool hasArrayFiller() const { return getArrayFiller(); } 3553 3554 /// \brief If this initializes a union, specifies which field in the 3555 /// union to initialize. 3556 /// 3557 /// Typically, this field is the first named field within the 3558 /// union. However, a designated initializer can specify the 3559 /// initialization of a different field within the union. 3560 FieldDecl *getInitializedFieldInUnion() { 3561 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>(); 3562 } 3563 const FieldDecl *getInitializedFieldInUnion() const { 3564 return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion(); 3565 } 3566 void setInitializedFieldInUnion(FieldDecl *FD) { 3567 ArrayFillerOrUnionFieldInit = FD; 3568 } 3569 3570 // Explicit InitListExpr's originate from source code (and have valid source 3571 // locations). Implicit InitListExpr's are created by the semantic analyzer. 3572 bool isExplicit() { 3573 return LBraceLoc.isValid() && RBraceLoc.isValid(); 3574 } 3575 3576 SourceLocation getLBraceLoc() const { return LBraceLoc; } 3577 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 3578 SourceLocation getRBraceLoc() const { return RBraceLoc; } 3579 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 3580 3581 /// @brief Retrieve the initializer list that describes the 3582 /// syntactic form of the initializer. 3583 /// 3584 /// 3585 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 3586 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 3587 3588 bool hadArrayRangeDesignator() const { 3589 return InitListExprBits.HadArrayRangeDesignator != 0; 3590 } 3591 void sawArrayRangeDesignator(bool ARD = true) { 3592 InitListExprBits.HadArrayRangeDesignator = ARD; 3593 } 3594 3595 bool initializesStdInitializerList() const { 3596 return InitListExprBits.InitializesStdInitializerList != 0; 3597 } 3598 void setInitializesStdInitializerList(bool ISIL = true) { 3599 InitListExprBits.InitializesStdInitializerList = ISIL; 3600 } 3601 3602 SourceRange getSourceRange() const; 3603 3604 static bool classof(const Stmt *T) { 3605 return T->getStmtClass() == InitListExprClass; 3606 } 3607 static bool classof(const InitListExpr *) { return true; } 3608 3609 // Iterators 3610 child_range children() { 3611 if (InitExprs.empty()) return child_range(); 3612 return child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); 3613 } 3614 3615 typedef InitExprsTy::iterator iterator; 3616 typedef InitExprsTy::const_iterator const_iterator; 3617 typedef InitExprsTy::reverse_iterator reverse_iterator; 3618 typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; 3619 3620 iterator begin() { return InitExprs.begin(); } 3621 const_iterator begin() const { return InitExprs.begin(); } 3622 iterator end() { return InitExprs.end(); } 3623 const_iterator end() const { return InitExprs.end(); } 3624 reverse_iterator rbegin() { return InitExprs.rbegin(); } 3625 const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } 3626 reverse_iterator rend() { return InitExprs.rend(); } 3627 const_reverse_iterator rend() const { return InitExprs.rend(); } 3628 3629 friend class ASTStmtReader; 3630 friend class ASTStmtWriter; 3631}; 3632 3633/// @brief Represents a C99 designated initializer expression. 3634/// 3635/// A designated initializer expression (C99 6.7.8) contains one or 3636/// more designators (which can be field designators, array 3637/// designators, or GNU array-range designators) followed by an 3638/// expression that initializes the field or element(s) that the 3639/// designators refer to. For example, given: 3640/// 3641/// @code 3642/// struct point { 3643/// double x; 3644/// double y; 3645/// }; 3646/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 3647/// @endcode 3648/// 3649/// The InitListExpr contains three DesignatedInitExprs, the first of 3650/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 3651/// designators, one array designator for @c [2] followed by one field 3652/// designator for @c .y. The initalization expression will be 1.0. 3653class DesignatedInitExpr : public Expr { 3654public: 3655 /// \brief Forward declaration of the Designator class. 3656 class Designator; 3657 3658private: 3659 /// The location of the '=' or ':' prior to the actual initializer 3660 /// expression. 3661 SourceLocation EqualOrColonLoc; 3662 3663 /// Whether this designated initializer used the GNU deprecated 3664 /// syntax rather than the C99 '=' syntax. 3665 bool GNUSyntax : 1; 3666 3667 /// The number of designators in this initializer expression. 3668 unsigned NumDesignators : 15; 3669 3670 /// \brief The designators in this designated initialization 3671 /// expression. 3672 Designator *Designators; 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 3680 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators, 3681 const Designator *Designators, 3682 SourceLocation EqualOrColonLoc, bool GNUSyntax, 3683 Expr **IndexExprs, unsigned NumIndexExprs, 3684 Expr *Init); 3685 3686 explicit DesignatedInitExpr(unsigned NumSubExprs) 3687 : Expr(DesignatedInitExprClass, EmptyShell()), 3688 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 3689 3690public: 3691 /// A field designator, e.g., ".x". 3692 struct FieldDesignator { 3693 /// Refers to the field that is being initialized. The low bit 3694 /// of this field determines whether this is actually a pointer 3695 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 3696 /// initially constructed, a field designator will store an 3697 /// IdentifierInfo*. After semantic analysis has resolved that 3698 /// name, the field designator will instead store a FieldDecl*. 3699 uintptr_t NameOrField; 3700 3701 /// The location of the '.' in the designated initializer. 3702 unsigned DotLoc; 3703 3704 /// The location of the field name in the designated initializer. 3705 unsigned FieldLoc; 3706 }; 3707 3708 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3709 struct ArrayOrRangeDesignator { 3710 /// Location of the first index expression within the designated 3711 /// initializer expression's list of subexpressions. 3712 unsigned Index; 3713 /// The location of the '[' starting the array range designator. 3714 unsigned LBracketLoc; 3715 /// The location of the ellipsis separating the start and end 3716 /// indices. Only valid for GNU array-range designators. 3717 unsigned EllipsisLoc; 3718 /// The location of the ']' terminating the array range designator. 3719 unsigned RBracketLoc; 3720 }; 3721 3722 /// @brief Represents a single C99 designator. 3723 /// 3724 /// @todo This class is infuriatingly similar to clang::Designator, 3725 /// but minor differences (storing indices vs. storing pointers) 3726 /// keep us from reusing it. Try harder, later, to rectify these 3727 /// differences. 3728 class Designator { 3729 /// @brief The kind of designator this describes. 3730 enum { 3731 FieldDesignator, 3732 ArrayDesignator, 3733 ArrayRangeDesignator 3734 } Kind; 3735 3736 union { 3737 /// A field designator, e.g., ".x". 3738 struct FieldDesignator Field; 3739 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3740 struct ArrayOrRangeDesignator ArrayOrRange; 3741 }; 3742 friend class DesignatedInitExpr; 3743 3744 public: 3745 Designator() {} 3746 3747 /// @brief Initializes a field designator. 3748 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 3749 SourceLocation FieldLoc) 3750 : Kind(FieldDesignator) { 3751 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 3752 Field.DotLoc = DotLoc.getRawEncoding(); 3753 Field.FieldLoc = FieldLoc.getRawEncoding(); 3754 } 3755 3756 /// @brief Initializes an array designator. 3757 Designator(unsigned Index, SourceLocation LBracketLoc, 3758 SourceLocation RBracketLoc) 3759 : Kind(ArrayDesignator) { 3760 ArrayOrRange.Index = Index; 3761 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3762 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 3763 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3764 } 3765 3766 /// @brief Initializes a GNU array-range designator. 3767 Designator(unsigned Index, SourceLocation LBracketLoc, 3768 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 3769 : Kind(ArrayRangeDesignator) { 3770 ArrayOrRange.Index = Index; 3771 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3772 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 3773 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3774 } 3775 3776 bool isFieldDesignator() const { return Kind == FieldDesignator; } 3777 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 3778 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 3779 3780 IdentifierInfo *getFieldName() const; 3781 3782 FieldDecl *getField() const { 3783 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3784 if (Field.NameOrField & 0x01) 3785 return 0; 3786 else 3787 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 3788 } 3789 3790 void setField(FieldDecl *FD) { 3791 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3792 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 3793 } 3794 3795 SourceLocation getDotLoc() const { 3796 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3797 return SourceLocation::getFromRawEncoding(Field.DotLoc); 3798 } 3799 3800 SourceLocation getFieldLoc() const { 3801 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3802 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 3803 } 3804 3805 SourceLocation getLBracketLoc() const { 3806 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3807 "Only valid on an array or array-range designator"); 3808 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 3809 } 3810 3811 SourceLocation getRBracketLoc() const { 3812 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3813 "Only valid on an array or array-range designator"); 3814 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 3815 } 3816 3817 SourceLocation getEllipsisLoc() const { 3818 assert(Kind == ArrayRangeDesignator && 3819 "Only valid on an array-range designator"); 3820 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 3821 } 3822 3823 unsigned getFirstExprIndex() const { 3824 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3825 "Only valid on an array or array-range designator"); 3826 return ArrayOrRange.Index; 3827 } 3828 3829 SourceLocation getStartLocation() const { 3830 if (Kind == FieldDesignator) 3831 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 3832 else 3833 return getLBracketLoc(); 3834 } 3835 SourceLocation getEndLocation() const { 3836 return Kind == FieldDesignator ? getFieldLoc() : getRBracketLoc(); 3837 } 3838 SourceRange getSourceRange() const { 3839 return SourceRange(getStartLocation(), getEndLocation()); 3840 } 3841 }; 3842 3843 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 3844 unsigned NumDesignators, 3845 Expr **IndexExprs, unsigned NumIndexExprs, 3846 SourceLocation EqualOrColonLoc, 3847 bool GNUSyntax, Expr *Init); 3848 3849 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 3850 3851 /// @brief Returns the number of designators in this initializer. 3852 unsigned size() const { return NumDesignators; } 3853 3854 // Iterator access to the designators. 3855 typedef Designator *designators_iterator; 3856 designators_iterator designators_begin() { return Designators; } 3857 designators_iterator designators_end() { 3858 return Designators + NumDesignators; 3859 } 3860 3861 typedef const Designator *const_designators_iterator; 3862 const_designators_iterator designators_begin() const { return Designators; } 3863 const_designators_iterator designators_end() const { 3864 return Designators + NumDesignators; 3865 } 3866 3867 typedef std::reverse_iterator<designators_iterator> 3868 reverse_designators_iterator; 3869 reverse_designators_iterator designators_rbegin() { 3870 return reverse_designators_iterator(designators_end()); 3871 } 3872 reverse_designators_iterator designators_rend() { 3873 return reverse_designators_iterator(designators_begin()); 3874 } 3875 3876 typedef std::reverse_iterator<const_designators_iterator> 3877 const_reverse_designators_iterator; 3878 const_reverse_designators_iterator designators_rbegin() const { 3879 return const_reverse_designators_iterator(designators_end()); 3880 } 3881 const_reverse_designators_iterator designators_rend() const { 3882 return const_reverse_designators_iterator(designators_begin()); 3883 } 3884 3885 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 3886 3887 void setDesignators(ASTContext &C, const Designator *Desigs, 3888 unsigned NumDesigs); 3889 3890 Expr *getArrayIndex(const Designator& D); 3891 Expr *getArrayRangeStart(const Designator& D); 3892 Expr *getArrayRangeEnd(const Designator& D); 3893 3894 /// @brief Retrieve the location of the '=' that precedes the 3895 /// initializer value itself, if present. 3896 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 3897 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 3898 3899 /// @brief Determines whether this designated initializer used the 3900 /// deprecated GNU syntax for designated initializers. 3901 bool usesGNUSyntax() const { return GNUSyntax; } 3902 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 3903 3904 /// @brief Retrieve the initializer value. 3905 Expr *getInit() const { 3906 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 3907 } 3908 3909 void setInit(Expr *init) { 3910 *child_begin() = init; 3911 } 3912 3913 /// \brief Retrieve the total number of subexpressions in this 3914 /// designated initializer expression, including the actual 3915 /// initialized value and any expressions that occur within array 3916 /// and array-range designators. 3917 unsigned getNumSubExprs() const { return NumSubExprs; } 3918 3919 Expr *getSubExpr(unsigned Idx) { 3920 assert(Idx < NumSubExprs && "Subscript out of range"); 3921 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3922 Ptr += sizeof(DesignatedInitExpr); 3923 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 3924 } 3925 3926 void setSubExpr(unsigned Idx, Expr *E) { 3927 assert(Idx < NumSubExprs && "Subscript out of range"); 3928 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3929 Ptr += sizeof(DesignatedInitExpr); 3930 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 3931 } 3932 3933 /// \brief Replaces the designator at index @p Idx with the series 3934 /// of designators in [First, Last). 3935 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First, 3936 const Designator *Last); 3937 3938 SourceRange getDesignatorsSourceRange() const; 3939 3940 SourceRange getSourceRange() const; 3941 3942 static bool classof(const Stmt *T) { 3943 return T->getStmtClass() == DesignatedInitExprClass; 3944 } 3945 static bool classof(const DesignatedInitExpr *) { return true; } 3946 3947 // Iterators 3948 child_range children() { 3949 Stmt **begin = reinterpret_cast<Stmt**>(this + 1); 3950 return child_range(begin, begin + NumSubExprs); 3951 } 3952}; 3953 3954/// \brief Represents an implicitly-generated value initialization of 3955/// an object of a given type. 3956/// 3957/// Implicit value initializations occur within semantic initializer 3958/// list expressions (InitListExpr) as placeholders for subobject 3959/// initializations not explicitly specified by the user. 3960/// 3961/// \see InitListExpr 3962class ImplicitValueInitExpr : public Expr { 3963public: 3964 explicit ImplicitValueInitExpr(QualType ty) 3965 : Expr(ImplicitValueInitExprClass, ty, VK_RValue, OK_Ordinary, 3966 false, false, ty->isInstantiationDependentType(), false) { } 3967 3968 /// \brief Construct an empty implicit value initialization. 3969 explicit ImplicitValueInitExpr(EmptyShell Empty) 3970 : Expr(ImplicitValueInitExprClass, Empty) { } 3971 3972 static bool classof(const Stmt *T) { 3973 return T->getStmtClass() == ImplicitValueInitExprClass; 3974 } 3975 static bool classof(const ImplicitValueInitExpr *) { return true; } 3976 3977 SourceRange getSourceRange() const { 3978 return SourceRange(); 3979 } 3980 3981 // Iterators 3982 child_range children() { return child_range(); } 3983}; 3984 3985 3986class ParenListExpr : public Expr { 3987 Stmt **Exprs; 3988 unsigned NumExprs; 3989 SourceLocation LParenLoc, RParenLoc; 3990 3991public: 3992 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 3993 unsigned numexprs, SourceLocation rparenloc); 3994 3995 /// \brief Build an empty paren list. 3996 explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 3997 3998 unsigned getNumExprs() const { return NumExprs; } 3999 4000 const Expr* getExpr(unsigned Init) const { 4001 assert(Init < getNumExprs() && "Initializer access out of range!"); 4002 return cast_or_null<Expr>(Exprs[Init]); 4003 } 4004 4005 Expr* getExpr(unsigned Init) { 4006 assert(Init < getNumExprs() && "Initializer access out of range!"); 4007 return cast_or_null<Expr>(Exprs[Init]); 4008 } 4009 4010 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 4011 4012 SourceLocation getLParenLoc() const { return LParenLoc; } 4013 SourceLocation getRParenLoc() const { return RParenLoc; } 4014 4015 SourceRange getSourceRange() const { 4016 return SourceRange(LParenLoc, RParenLoc); 4017 } 4018 static bool classof(const Stmt *T) { 4019 return T->getStmtClass() == ParenListExprClass; 4020 } 4021 static bool classof(const ParenListExpr *) { return true; } 4022 4023 // Iterators 4024 child_range children() { 4025 return child_range(&Exprs[0], &Exprs[0]+NumExprs); 4026 } 4027 4028 friend class ASTStmtReader; 4029 friend class ASTStmtWriter; 4030}; 4031 4032 4033/// \brief Represents a C11 generic selection. 4034/// 4035/// A generic selection (C11 6.5.1.1) contains an unevaluated controlling 4036/// expression, followed by one or more generic associations. Each generic 4037/// association specifies a type name and an expression, or "default" and an 4038/// expression (in which case it is known as a default generic association). 4039/// The type and value of the generic selection are identical to those of its 4040/// result expression, which is defined as the expression in the generic 4041/// association with a type name that is compatible with the type of the 4042/// controlling expression, or the expression in the default generic association 4043/// if no types are compatible. For example: 4044/// 4045/// @code 4046/// _Generic(X, double: 1, float: 2, default: 3) 4047/// @endcode 4048/// 4049/// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f 4050/// or 3 if "hello". 4051/// 4052/// As an extension, generic selections are allowed in C++, where the following 4053/// additional semantics apply: 4054/// 4055/// Any generic selection whose controlling expression is type-dependent or 4056/// which names a dependent type in its association list is result-dependent, 4057/// which means that the choice of result expression is dependent. 4058/// Result-dependent generic associations are both type- and value-dependent. 4059class GenericSelectionExpr : public Expr { 4060 enum { CONTROLLING, END_EXPR }; 4061 TypeSourceInfo **AssocTypes; 4062 Stmt **SubExprs; 4063 unsigned NumAssocs, ResultIndex; 4064 SourceLocation GenericLoc, DefaultLoc, RParenLoc; 4065 4066public: 4067 GenericSelectionExpr(ASTContext &Context, 4068 SourceLocation GenericLoc, Expr *ControllingExpr, 4069 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 4070 unsigned NumAssocs, SourceLocation DefaultLoc, 4071 SourceLocation RParenLoc, 4072 bool ContainsUnexpandedParameterPack, 4073 unsigned ResultIndex); 4074 4075 /// This constructor is used in the result-dependent case. 4076 GenericSelectionExpr(ASTContext &Context, 4077 SourceLocation GenericLoc, Expr *ControllingExpr, 4078 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 4079 unsigned NumAssocs, SourceLocation DefaultLoc, 4080 SourceLocation RParenLoc, 4081 bool ContainsUnexpandedParameterPack); 4082 4083 explicit GenericSelectionExpr(EmptyShell Empty) 4084 : Expr(GenericSelectionExprClass, Empty) { } 4085 4086 unsigned getNumAssocs() const { return NumAssocs; } 4087 4088 SourceLocation getGenericLoc() const { return GenericLoc; } 4089 SourceLocation getDefaultLoc() const { return DefaultLoc; } 4090 SourceLocation getRParenLoc() const { return RParenLoc; } 4091 4092 const Expr *getAssocExpr(unsigned i) const { 4093 return cast<Expr>(SubExprs[END_EXPR+i]); 4094 } 4095 Expr *getAssocExpr(unsigned i) { return cast<Expr>(SubExprs[END_EXPR+i]); } 4096 4097 const TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) const { 4098 return AssocTypes[i]; 4099 } 4100 TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) { return AssocTypes[i]; } 4101 4102 QualType getAssocType(unsigned i) const { 4103 if (const TypeSourceInfo *TS = getAssocTypeSourceInfo(i)) 4104 return TS->getType(); 4105 else 4106 return QualType(); 4107 } 4108 4109 const Expr *getControllingExpr() const { 4110 return cast<Expr>(SubExprs[CONTROLLING]); 4111 } 4112 Expr *getControllingExpr() { return cast<Expr>(SubExprs[CONTROLLING]); } 4113 4114 /// Whether this generic selection is result-dependent. 4115 bool isResultDependent() const { return ResultIndex == -1U; } 4116 4117 /// The zero-based index of the result expression's generic association in 4118 /// the generic selection's association list. Defined only if the 4119 /// generic selection is not result-dependent. 4120 unsigned getResultIndex() const { 4121 assert(!isResultDependent() && "Generic selection is result-dependent"); 4122 return ResultIndex; 4123 } 4124 4125 /// The generic selection's result expression. Defined only if the 4126 /// generic selection is not result-dependent. 4127 const Expr *getResultExpr() const { return getAssocExpr(getResultIndex()); } 4128 Expr *getResultExpr() { return getAssocExpr(getResultIndex()); } 4129 4130 SourceRange getSourceRange() const { 4131 return SourceRange(GenericLoc, RParenLoc); 4132 } 4133 static bool classof(const Stmt *T) { 4134 return T->getStmtClass() == GenericSelectionExprClass; 4135 } 4136 static bool classof(const GenericSelectionExpr *) { return true; } 4137 4138 child_range children() { 4139 return child_range(SubExprs, SubExprs+END_EXPR+NumAssocs); 4140 } 4141 4142 friend class ASTStmtReader; 4143}; 4144 4145//===----------------------------------------------------------------------===// 4146// Clang Extensions 4147//===----------------------------------------------------------------------===// 4148 4149 4150/// ExtVectorElementExpr - This represents access to specific elements of a 4151/// vector, and may occur on the left hand side or right hand side. For example 4152/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 4153/// 4154/// Note that the base may have either vector or pointer to vector type, just 4155/// like a struct field reference. 4156/// 4157class ExtVectorElementExpr : public Expr { 4158 Stmt *Base; 4159 IdentifierInfo *Accessor; 4160 SourceLocation AccessorLoc; 4161public: 4162 ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, 4163 IdentifierInfo &accessor, SourceLocation loc) 4164 : Expr(ExtVectorElementExprClass, ty, VK, 4165 (VK == VK_RValue ? OK_Ordinary : OK_VectorComponent), 4166 base->isTypeDependent(), base->isValueDependent(), 4167 base->isInstantiationDependent(), 4168 base->containsUnexpandedParameterPack()), 4169 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 4170 4171 /// \brief Build an empty vector element expression. 4172 explicit ExtVectorElementExpr(EmptyShell Empty) 4173 : Expr(ExtVectorElementExprClass, Empty) { } 4174 4175 const Expr *getBase() const { return cast<Expr>(Base); } 4176 Expr *getBase() { return cast<Expr>(Base); } 4177 void setBase(Expr *E) { Base = E; } 4178 4179 IdentifierInfo &getAccessor() const { return *Accessor; } 4180 void setAccessor(IdentifierInfo *II) { Accessor = II; } 4181 4182 SourceLocation getAccessorLoc() const { return AccessorLoc; } 4183 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 4184 4185 /// getNumElements - Get the number of components being selected. 4186 unsigned getNumElements() const; 4187 4188 /// containsDuplicateElements - Return true if any element access is 4189 /// repeated. 4190 bool containsDuplicateElements() const; 4191 4192 /// getEncodedElementAccess - Encode the elements accessed into an llvm 4193 /// aggregate Constant of ConstantInt(s). 4194 void getEncodedElementAccess(SmallVectorImpl<unsigned> &Elts) const; 4195 4196 SourceRange getSourceRange() const { 4197 return SourceRange(getBase()->getLocStart(), AccessorLoc); 4198 } 4199 4200 /// isArrow - Return true if the base expression is a pointer to vector, 4201 /// return false if the base expression is a vector. 4202 bool isArrow() const; 4203 4204 static bool classof(const Stmt *T) { 4205 return T->getStmtClass() == ExtVectorElementExprClass; 4206 } 4207 static bool classof(const ExtVectorElementExpr *) { return true; } 4208 4209 // Iterators 4210 child_range children() { return child_range(&Base, &Base+1); } 4211}; 4212 4213 4214/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 4215/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 4216class BlockExpr : public Expr { 4217protected: 4218 BlockDecl *TheBlock; 4219public: 4220 BlockExpr(BlockDecl *BD, QualType ty) 4221 : Expr(BlockExprClass, ty, VK_RValue, OK_Ordinary, 4222 ty->isDependentType(), ty->isDependentType(), 4223 ty->isInstantiationDependentType() || BD->isDependentContext(), 4224 false), 4225 TheBlock(BD) {} 4226 4227 /// \brief Build an empty block expression. 4228 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 4229 4230 const BlockDecl *getBlockDecl() const { return TheBlock; } 4231 BlockDecl *getBlockDecl() { return TheBlock; } 4232 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 4233 4234 // Convenience functions for probing the underlying BlockDecl. 4235 SourceLocation getCaretLocation() const; 4236 const Stmt *getBody() const; 4237 Stmt *getBody(); 4238 4239 SourceRange getSourceRange() const { 4240 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 4241 } 4242 4243 /// getFunctionType - Return the underlying function type for this block. 4244 const FunctionProtoType *getFunctionType() const; 4245 4246 static bool classof(const Stmt *T) { 4247 return T->getStmtClass() == BlockExprClass; 4248 } 4249 static bool classof(const BlockExpr *) { return true; } 4250 4251 // Iterators 4252 child_range children() { return child_range(); } 4253}; 4254 4255/// BlockDeclRefExpr - A reference to a local variable declared in an 4256/// enclosing scope. 4257class BlockDeclRefExpr : public Expr { 4258 VarDecl *D; 4259 SourceLocation Loc; 4260 bool IsByRef : 1; 4261 bool ConstQualAdded : 1; 4262public: 4263 BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK, 4264 SourceLocation l, bool ByRef, bool constAdded = false); 4265 4266 // \brief Build an empty reference to a declared variable in a 4267 // block. 4268 explicit BlockDeclRefExpr(EmptyShell Empty) 4269 : Expr(BlockDeclRefExprClass, Empty) { } 4270 4271 VarDecl *getDecl() { return D; } 4272 const VarDecl *getDecl() const { return D; } 4273 void setDecl(VarDecl *VD) { D = VD; } 4274 4275 SourceLocation getLocation() const { return Loc; } 4276 void setLocation(SourceLocation L) { Loc = L; } 4277 4278 SourceRange getSourceRange() const { return SourceRange(Loc); } 4279 4280 bool isByRef() const { return IsByRef; } 4281 void setByRef(bool BR) { IsByRef = BR; } 4282 4283 bool isConstQualAdded() const { return ConstQualAdded; } 4284 void setConstQualAdded(bool C) { ConstQualAdded = C; } 4285 4286 static bool classof(const Stmt *T) { 4287 return T->getStmtClass() == BlockDeclRefExprClass; 4288 } 4289 static bool classof(const BlockDeclRefExpr *) { return true; } 4290 4291 // Iterators 4292 child_range children() { return child_range(); } 4293}; 4294 4295/// AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] 4296/// This AST node provides support for reinterpreting a type to another 4297/// type of the same size. 4298class AsTypeExpr : public Expr { // Should this be an ExplicitCastExpr? 4299private: 4300 Stmt *SrcExpr; 4301 SourceLocation BuiltinLoc, RParenLoc; 4302 4303 friend class ASTReader; 4304 friend class ASTStmtReader; 4305 explicit AsTypeExpr(EmptyShell Empty) : Expr(AsTypeExprClass, Empty) {} 4306 4307public: 4308 AsTypeExpr(Expr* SrcExpr, QualType DstType, 4309 ExprValueKind VK, ExprObjectKind OK, 4310 SourceLocation BuiltinLoc, SourceLocation RParenLoc) 4311 : Expr(AsTypeExprClass, DstType, VK, OK, 4312 DstType->isDependentType(), 4313 DstType->isDependentType() || SrcExpr->isValueDependent(), 4314 (DstType->isInstantiationDependentType() || 4315 SrcExpr->isInstantiationDependent()), 4316 (DstType->containsUnexpandedParameterPack() || 4317 SrcExpr->containsUnexpandedParameterPack())), 4318 SrcExpr(SrcExpr), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) {} 4319 4320 /// getSrcExpr - Return the Expr to be converted. 4321 Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } 4322 4323 /// getBuiltinLoc - Return the location of the __builtin_astype token. 4324 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 4325 4326 /// getRParenLoc - Return the location of final right parenthesis. 4327 SourceLocation getRParenLoc() const { return RParenLoc; } 4328 4329 SourceRange getSourceRange() const { 4330 return SourceRange(BuiltinLoc, RParenLoc); 4331 } 4332 4333 static bool classof(const Stmt *T) { 4334 return T->getStmtClass() == AsTypeExprClass; 4335 } 4336 static bool classof(const AsTypeExpr *) { return true; } 4337 4338 // Iterators 4339 child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } 4340}; 4341 4342/// PseudoObjectExpr - An expression which accesses a pseudo-object 4343/// l-value. A pseudo-object is an abstract object, accesses to which 4344/// are translated to calls. The pseudo-object expression has a 4345/// syntactic form, which shows how the expression was actually 4346/// written in the source code, and a semantic form, which is a series 4347/// of expressions to be executed in order which detail how the 4348/// operation is actually evaluated. Optionally, one of the semantic 4349/// forms may also provide a result value for the expression. 4350/// 4351/// If any of the semantic-form expressions is an OpaqueValueExpr, 4352/// that OVE is required to have a source expression, and it is bound 4353/// to the result of that source expression. Such OVEs may appear 4354/// only in subsequent semantic-form expressions and as 4355/// sub-expressions of the syntactic form. 4356/// 4357/// PseudoObjectExpr should be used only when an operation can be 4358/// usefully described in terms of fairly simple rewrite rules on 4359/// objects and functions that are meant to be used by end-developers. 4360/// For example, under the Itanium ABI, dynamic casts are implemented 4361/// as a call to a runtime function called __dynamic_cast; using this 4362/// class to describe that would be inappropriate because that call is 4363/// not really part of the user-visible semantics, and instead the 4364/// cast is properly reflected in the AST and IR-generation has been 4365/// taught to generate the call as necessary. In contrast, an 4366/// Objective-C property access is semantically defined to be 4367/// equivalent to a particular message send, and this is very much 4368/// part of the user model. The name of this class encourages this 4369/// modelling design. 4370class PseudoObjectExpr : public Expr { 4371 // PseudoObjectExprBits.NumSubExprs - The number of sub-expressions. 4372 // Always at least two, because the first sub-expression is the 4373 // syntactic form. 4374 4375 // PseudoObjectExprBits.ResultIndex - The index of the 4376 // sub-expression holding the result. 0 means the result is void, 4377 // which is unambiguous because it's the index of the syntactic 4378 // form. Note that this is therefore 1 higher than the value passed 4379 // in to Create, which is an index within the semantic forms. 4380 // Note also that ASTStmtWriter assumes this encoding. 4381 4382 Expr **getSubExprsBuffer() { return reinterpret_cast<Expr**>(this + 1); } 4383 const Expr * const *getSubExprsBuffer() const { 4384 return reinterpret_cast<const Expr * const *>(this + 1); 4385 } 4386 4387 friend class ASTStmtReader; 4388 4389 PseudoObjectExpr(QualType type, ExprValueKind VK, 4390 Expr *syntactic, ArrayRef<Expr*> semantic, 4391 unsigned resultIndex); 4392 4393 PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs); 4394 4395 unsigned getNumSubExprs() const { 4396 return PseudoObjectExprBits.NumSubExprs; 4397 } 4398 4399public: 4400 /// NoResult - A value for the result index indicating that there is 4401 /// no semantic result. 4402 enum { NoResult = ~0U }; 4403 4404 static PseudoObjectExpr *Create(ASTContext &Context, Expr *syntactic, 4405 ArrayRef<Expr*> semantic, 4406 unsigned resultIndex); 4407 4408 static PseudoObjectExpr *Create(ASTContext &Context, EmptyShell shell, 4409 unsigned numSemanticExprs); 4410 4411 /// Return the syntactic form of this expression, i.e. the 4412 /// expression it actually looks like. Likely to be expressed in 4413 /// terms of OpaqueValueExprs bound in the semantic form. 4414 Expr *getSyntacticForm() { return getSubExprsBuffer()[0]; } 4415 const Expr *getSyntacticForm() const { return getSubExprsBuffer()[0]; } 4416 4417 /// Return the index of the result-bearing expression into the semantics 4418 /// expressions, or PseudoObjectExpr::NoResult if there is none. 4419 unsigned getResultExprIndex() const { 4420 if (PseudoObjectExprBits.ResultIndex == 0) return NoResult; 4421 return PseudoObjectExprBits.ResultIndex - 1; 4422 } 4423 4424 /// Return the result-bearing expression, or null if there is none. 4425 Expr *getResultExpr() { 4426 if (PseudoObjectExprBits.ResultIndex == 0) 4427 return 0; 4428 return getSubExprsBuffer()[PseudoObjectExprBits.ResultIndex]; 4429 } 4430 const Expr *getResultExpr() const { 4431 return const_cast<PseudoObjectExpr*>(this)->getResultExpr(); 4432 } 4433 4434 unsigned getNumSemanticExprs() const { return getNumSubExprs() - 1; } 4435 4436 typedef Expr * const *semantics_iterator; 4437 typedef const Expr * const *const_semantics_iterator; 4438 semantics_iterator semantics_begin() { 4439 return getSubExprsBuffer() + 1; 4440 } 4441 const_semantics_iterator semantics_begin() const { 4442 return getSubExprsBuffer() + 1; 4443 } 4444 semantics_iterator semantics_end() { 4445 return getSubExprsBuffer() + getNumSubExprs(); 4446 } 4447 const_semantics_iterator semantics_end() const { 4448 return getSubExprsBuffer() + getNumSubExprs(); 4449 } 4450 Expr *getSemanticExpr(unsigned index) { 4451 assert(index + 1 < getNumSubExprs()); 4452 return getSubExprsBuffer()[index + 1]; 4453 } 4454 const Expr *getSemanticExpr(unsigned index) const { 4455 return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index); 4456 } 4457 4458 SourceLocation getExprLoc() const { 4459 return getSyntacticForm()->getExprLoc(); 4460 } 4461 SourceRange getSourceRange() const { 4462 return getSyntacticForm()->getSourceRange(); 4463 } 4464 4465 child_range children() { 4466 Stmt **cs = reinterpret_cast<Stmt**>(getSubExprsBuffer()); 4467 return child_range(cs, cs + getNumSubExprs()); 4468 } 4469 4470 static bool classof(const Stmt *T) { 4471 return T->getStmtClass() == PseudoObjectExprClass; 4472 } 4473 static bool classof(const PseudoObjectExpr *) { return true; } 4474}; 4475 4476/// AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, 4477/// __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the 4478/// similarly-named C++0x instructions. All of these instructions take one 4479/// primary pointer and at least one memory order. 4480class AtomicExpr : public Expr { 4481public: 4482 enum AtomicOp { Load, Store, CmpXchgStrong, CmpXchgWeak, Xchg, 4483 Add, Sub, And, Or, Xor, Init }; 4484private: 4485 enum { PTR, ORDER, VAL1, ORDER_FAIL, VAL2, END_EXPR }; 4486 Stmt* SubExprs[END_EXPR]; 4487 unsigned NumSubExprs; 4488 SourceLocation BuiltinLoc, RParenLoc; 4489 AtomicOp Op; 4490 4491public: 4492 AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr, QualType t, 4493 AtomicOp op, SourceLocation RP); 4494 4495 /// \brief Build an empty AtomicExpr. 4496 explicit AtomicExpr(EmptyShell Empty) : Expr(AtomicExprClass, Empty) { } 4497 4498 Expr *getPtr() const { 4499 return cast<Expr>(SubExprs[PTR]); 4500 } 4501 void setPtr(Expr *E) { 4502 SubExprs[PTR] = E; 4503 } 4504 Expr *getOrder() const { 4505 return cast<Expr>(SubExprs[ORDER]); 4506 } 4507 void setOrder(Expr *E) { 4508 SubExprs[ORDER] = E; 4509 } 4510 Expr *getVal1() const { 4511 if (Op == Init) 4512 return cast<Expr>(SubExprs[ORDER]); 4513 assert(NumSubExprs >= 3); 4514 return cast<Expr>(SubExprs[VAL1]); 4515 } 4516 void setVal1(Expr *E) { 4517 if (Op == Init) { 4518 SubExprs[ORDER] = E; 4519 return; 4520 } 4521 assert(NumSubExprs >= 3); 4522 SubExprs[VAL1] = E; 4523 } 4524 Expr *getOrderFail() const { 4525 assert(NumSubExprs == 5); 4526 return cast<Expr>(SubExprs[ORDER_FAIL]); 4527 } 4528 void setOrderFail(Expr *E) { 4529 assert(NumSubExprs == 5); 4530 SubExprs[ORDER_FAIL] = E; 4531 } 4532 Expr *getVal2() const { 4533 assert(NumSubExprs == 5); 4534 return cast<Expr>(SubExprs[VAL2]); 4535 } 4536 void setVal2(Expr *E) { 4537 assert(NumSubExprs == 5); 4538 SubExprs[VAL2] = E; 4539 } 4540 4541 AtomicOp getOp() const { return Op; } 4542 void setOp(AtomicOp op) { Op = op; } 4543 unsigned getNumSubExprs() { return NumSubExprs; } 4544 void setNumSubExprs(unsigned num) { NumSubExprs = num; } 4545 4546 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 4547 4548 bool isVolatile() const { 4549 return getPtr()->getType()->getPointeeType().isVolatileQualified(); 4550 } 4551 4552 bool isCmpXChg() const { 4553 return getOp() == AtomicExpr::CmpXchgStrong || 4554 getOp() == AtomicExpr::CmpXchgWeak; 4555 } 4556 4557 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 4558 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 4559 4560 SourceLocation getRParenLoc() const { return RParenLoc; } 4561 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 4562 4563 SourceRange getSourceRange() const { 4564 return SourceRange(BuiltinLoc, RParenLoc); 4565 } 4566 static bool classof(const Stmt *T) { 4567 return T->getStmtClass() == AtomicExprClass; 4568 } 4569 static bool classof(const AtomicExpr *) { return true; } 4570 4571 // Iterators 4572 child_range children() { 4573 return child_range(SubExprs, SubExprs+NumSubExprs); 4574 } 4575}; 4576} // end namespace clang 4577 4578#endif 4579