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