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