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