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