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