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