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