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