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