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