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