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