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