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