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