Expr.h revision dbd872f273a8dbf22e089b3def6c09f0a460965d
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<ValueDecl *, 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 ValueDecl *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, ValueDecl *d, QualType t, SourceLocation l) : 440 Expr(SC, t, false, false), DecoratedD(d, 0), Loc(l) { 441 computeDependence(); 442 } 443 444public: 445 DeclRefExpr(ValueDecl *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 ValueDecl *D, 458 SourceLocation NameLoc, 459 QualType T, 460 const TemplateArgumentListInfo *TemplateArgs = 0); 461 462 ValueDecl *getDecl() { return DecoratedD.getPointer(); } 463 const ValueDecl *getDecl() const { return DecoratedD.getPointer(); } 464 void setDecl(ValueDecl *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 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 690 691 static bool classof(const Stmt *T) { 692 return T->getStmtClass() == FloatingLiteralClass; 693 } 694 static bool classof(const FloatingLiteral *) { return true; } 695 696 // Iterators 697 virtual child_iterator child_begin(); 698 virtual child_iterator child_end(); 699}; 700 701/// ImaginaryLiteral - We support imaginary integer and floating point literals, 702/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 703/// IntegerLiteral classes. Instances of this class always have a Complex type 704/// whose element type matches the subexpression. 705/// 706class ImaginaryLiteral : public Expr { 707 Stmt *Val; 708public: 709 ImaginaryLiteral(Expr *val, QualType Ty) 710 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 711 712 /// \brief Build an empty imaginary literal. 713 explicit ImaginaryLiteral(EmptyShell Empty) 714 : Expr(ImaginaryLiteralClass, Empty) { } 715 716 const Expr *getSubExpr() const { return cast<Expr>(Val); } 717 Expr *getSubExpr() { return cast<Expr>(Val); } 718 void setSubExpr(Expr *E) { Val = E; } 719 720 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 721 static bool classof(const Stmt *T) { 722 return T->getStmtClass() == ImaginaryLiteralClass; 723 } 724 static bool classof(const ImaginaryLiteral *) { return true; } 725 726 // Iterators 727 virtual child_iterator child_begin(); 728 virtual child_iterator child_end(); 729}; 730 731/// StringLiteral - This represents a string literal expression, e.g. "foo" 732/// or L"bar" (wide strings). The actual string is returned by getStrData() 733/// is NOT null-terminated, and the length of the string is determined by 734/// calling getByteLength(). The C type for a string is always a 735/// ConstantArrayType. In C++, the char type is const qualified, in C it is 736/// not. 737/// 738/// Note that strings in C can be formed by concatenation of multiple string 739/// literal pptokens in translation phase #6. This keeps track of the locations 740/// of each of these pieces. 741/// 742/// Strings in C can also be truncated and extended by assigning into arrays, 743/// e.g. with constructs like: 744/// char X[2] = "foobar"; 745/// In this case, getByteLength() will return 6, but the string literal will 746/// have type "char[2]". 747class StringLiteral : public Expr { 748 const char *StrData; 749 unsigned ByteLength; 750 bool IsWide; 751 unsigned NumConcatenated; 752 SourceLocation TokLocs[1]; 753 754 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {} 755 756protected: 757 virtual void DoDestroy(ASTContext &C); 758 759public: 760 /// This is the "fully general" constructor that allows representation of 761 /// strings formed from multiple concatenated tokens. 762 static StringLiteral *Create(ASTContext &C, const char *StrData, 763 unsigned ByteLength, bool Wide, QualType Ty, 764 const SourceLocation *Loc, unsigned NumStrs); 765 766 /// Simple constructor for string literals made from one token. 767 static StringLiteral *Create(ASTContext &C, const char *StrData, 768 unsigned ByteLength, 769 bool Wide, QualType Ty, SourceLocation Loc) { 770 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1); 771 } 772 773 /// \brief Construct an empty string literal. 774 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 775 776 llvm::StringRef getString() const { 777 return llvm::StringRef(StrData, ByteLength); 778 } 779 // FIXME: These are deprecated, replace with StringRef. 780 const char *getStrData() const { return StrData; } 781 unsigned getByteLength() const { return ByteLength; } 782 783 /// \brief Sets the string data to the given string data. 784 void setString(ASTContext &C, llvm::StringRef Str); 785 786 bool isWide() const { return IsWide; } 787 void setWide(bool W) { IsWide = W; } 788 789 bool containsNonAsciiOrNull() const { 790 llvm::StringRef Str = getString(); 791 for (unsigned i = 0, e = Str.size(); i != e; ++i) 792 if (!isascii(Str[i]) || !Str[i]) 793 return true; 794 return false; 795 } 796 /// getNumConcatenated - Get the number of string literal tokens that were 797 /// concatenated in translation phase #6 to form this string literal. 798 unsigned getNumConcatenated() const { return NumConcatenated; } 799 800 SourceLocation getStrTokenLoc(unsigned TokNum) const { 801 assert(TokNum < NumConcatenated && "Invalid tok number"); 802 return TokLocs[TokNum]; 803 } 804 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 805 assert(TokNum < NumConcatenated && "Invalid tok number"); 806 TokLocs[TokNum] = L; 807 } 808 809 typedef const SourceLocation *tokloc_iterator; 810 tokloc_iterator tokloc_begin() const { return TokLocs; } 811 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 812 813 virtual SourceRange getSourceRange() const { 814 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 815 } 816 static bool classof(const Stmt *T) { 817 return T->getStmtClass() == StringLiteralClass; 818 } 819 static bool classof(const StringLiteral *) { return true; } 820 821 // Iterators 822 virtual child_iterator child_begin(); 823 virtual child_iterator child_end(); 824}; 825 826/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 827/// AST node is only formed if full location information is requested. 828class ParenExpr : public Expr { 829 SourceLocation L, R; 830 Stmt *Val; 831public: 832 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 833 : Expr(ParenExprClass, val->getType(), 834 val->isTypeDependent(), val->isValueDependent()), 835 L(l), R(r), Val(val) {} 836 837 /// \brief Construct an empty parenthesized expression. 838 explicit ParenExpr(EmptyShell Empty) 839 : Expr(ParenExprClass, Empty) { } 840 841 const Expr *getSubExpr() const { return cast<Expr>(Val); } 842 Expr *getSubExpr() { return cast<Expr>(Val); } 843 void setSubExpr(Expr *E) { Val = E; } 844 845 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 846 847 /// \brief Get the location of the left parentheses '('. 848 SourceLocation getLParen() const { return L; } 849 void setLParen(SourceLocation Loc) { L = Loc; } 850 851 /// \brief Get the location of the right parentheses ')'. 852 SourceLocation getRParen() const { return R; } 853 void setRParen(SourceLocation Loc) { R = Loc; } 854 855 static bool classof(const Stmt *T) { 856 return T->getStmtClass() == ParenExprClass; 857 } 858 static bool classof(const ParenExpr *) { return true; } 859 860 // Iterators 861 virtual child_iterator child_begin(); 862 virtual child_iterator child_end(); 863}; 864 865 866/// UnaryOperator - This represents the unary-expression's (except sizeof and 867/// alignof), the postinc/postdec operators from postfix-expression, and various 868/// extensions. 869/// 870/// Notes on various nodes: 871/// 872/// Real/Imag - These return the real/imag part of a complex operand. If 873/// applied to a non-complex value, the former returns its operand and the 874/// later returns zero in the type of the operand. 875/// 876/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 877/// subexpression is a compound literal with the various MemberExpr and 878/// ArraySubscriptExpr's applied to it. 879/// 880class UnaryOperator : public Expr { 881public: 882 // Note that additions to this should also update the StmtVisitor class. 883 enum Opcode { 884 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 885 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 886 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 887 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 888 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 889 Real, Imag, // "__real expr"/"__imag expr" Extension. 890 Extension, // __extension__ marker. 891 OffsetOf // __builtin_offsetof 892 }; 893private: 894 Stmt *Val; 895 Opcode Opc; 896 SourceLocation Loc; 897public: 898 899 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 900 : Expr(UnaryOperatorClass, type, 901 input->isTypeDependent() && opc != OffsetOf, 902 input->isValueDependent()), 903 Val(input), Opc(opc), Loc(l) {} 904 905 /// \brief Build an empty unary operator. 906 explicit UnaryOperator(EmptyShell Empty) 907 : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { } 908 909 Opcode getOpcode() const { return Opc; } 910 void setOpcode(Opcode O) { Opc = O; } 911 912 Expr *getSubExpr() const { return cast<Expr>(Val); } 913 void setSubExpr(Expr *E) { Val = E; } 914 915 /// getOperatorLoc - Return the location of the operator. 916 SourceLocation getOperatorLoc() const { return Loc; } 917 void setOperatorLoc(SourceLocation L) { Loc = L; } 918 919 /// isPostfix - Return true if this is a postfix operation, like x++. 920 static bool isPostfix(Opcode Op) { 921 return Op == PostInc || Op == PostDec; 922 } 923 924 /// isPostfix - Return true if this is a prefix operation, like --x. 925 static bool isPrefix(Opcode Op) { 926 return Op == PreInc || Op == PreDec; 927 } 928 929 bool isPrefix() const { return isPrefix(Opc); } 930 bool isPostfix() const { return isPostfix(Opc); } 931 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 932 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 933 bool isOffsetOfOp() const { return Opc == OffsetOf; } 934 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 935 bool isArithmeticOp() const { return isArithmeticOp(Opc); } 936 937 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 938 /// corresponds to, e.g. "sizeof" or "[pre]++" 939 static const char *getOpcodeStr(Opcode Op); 940 941 /// \brief Retrieve the unary opcode that corresponds to the given 942 /// overloaded operator. 943 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 944 945 /// \brief Retrieve the overloaded operator kind that corresponds to 946 /// the given unary opcode. 947 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 948 949 virtual SourceRange getSourceRange() const { 950 if (isPostfix()) 951 return SourceRange(Val->getLocStart(), Loc); 952 else 953 return SourceRange(Loc, Val->getLocEnd()); 954 } 955 virtual SourceLocation getExprLoc() const { return Loc; } 956 957 static bool classof(const Stmt *T) { 958 return T->getStmtClass() == UnaryOperatorClass; 959 } 960 static bool classof(const UnaryOperator *) { return true; } 961 962 // Iterators 963 virtual child_iterator child_begin(); 964 virtual child_iterator child_end(); 965}; 966 967/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 968/// types and expressions. 969class SizeOfAlignOfExpr : public Expr { 970 bool isSizeof : 1; // true if sizeof, false if alignof. 971 bool isType : 1; // true if operand is a type, false if an expression 972 union { 973 TypeSourceInfo *Ty; 974 Stmt *Ex; 975 } Argument; 976 SourceLocation OpLoc, RParenLoc; 977 978protected: 979 virtual void DoDestroy(ASTContext& C); 980 981public: 982 SizeOfAlignOfExpr(bool issizeof, TypeSourceInfo *TInfo, 983 QualType resultType, SourceLocation op, 984 SourceLocation rp) : 985 Expr(SizeOfAlignOfExprClass, resultType, 986 false, // Never type-dependent (C++ [temp.dep.expr]p3). 987 // Value-dependent if the argument is type-dependent. 988 TInfo->getType()->isDependentType()), 989 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) { 990 Argument.Ty = TInfo; 991 } 992 993 SizeOfAlignOfExpr(bool issizeof, Expr *E, 994 QualType resultType, SourceLocation op, 995 SourceLocation rp) : 996 Expr(SizeOfAlignOfExprClass, resultType, 997 false, // Never type-dependent (C++ [temp.dep.expr]p3). 998 // Value-dependent if the argument is type-dependent. 999 E->isTypeDependent()), 1000 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) { 1001 Argument.Ex = E; 1002 } 1003 1004 /// \brief Construct an empty sizeof/alignof expression. 1005 explicit SizeOfAlignOfExpr(EmptyShell Empty) 1006 : Expr(SizeOfAlignOfExprClass, Empty) { } 1007 1008 bool isSizeOf() const { return isSizeof; } 1009 void setSizeof(bool S) { isSizeof = S; } 1010 1011 bool isArgumentType() const { return isType; } 1012 QualType getArgumentType() const { 1013 return getArgumentTypeInfo()->getType(); 1014 } 1015 TypeSourceInfo *getArgumentTypeInfo() const { 1016 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 1017 return Argument.Ty; 1018 } 1019 Expr *getArgumentExpr() { 1020 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 1021 return static_cast<Expr*>(Argument.Ex); 1022 } 1023 const Expr *getArgumentExpr() const { 1024 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 1025 } 1026 1027 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 1028 void setArgument(TypeSourceInfo *TInfo) { 1029 Argument.Ty = TInfo; 1030 isType = true; 1031 } 1032 1033 /// Gets the argument type, or the type of the argument expression, whichever 1034 /// is appropriate. 1035 QualType getTypeOfArgument() const { 1036 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 1037 } 1038 1039 SourceLocation getOperatorLoc() const { return OpLoc; } 1040 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1041 1042 SourceLocation getRParenLoc() const { return RParenLoc; } 1043 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1044 1045 virtual SourceRange getSourceRange() const { 1046 return SourceRange(OpLoc, RParenLoc); 1047 } 1048 1049 static bool classof(const Stmt *T) { 1050 return T->getStmtClass() == SizeOfAlignOfExprClass; 1051 } 1052 static bool classof(const SizeOfAlignOfExpr *) { return true; } 1053 1054 // Iterators 1055 virtual child_iterator child_begin(); 1056 virtual child_iterator child_end(); 1057}; 1058 1059//===----------------------------------------------------------------------===// 1060// Postfix Operators. 1061//===----------------------------------------------------------------------===// 1062 1063/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 1064class ArraySubscriptExpr : public Expr { 1065 enum { LHS, RHS, END_EXPR=2 }; 1066 Stmt* SubExprs[END_EXPR]; 1067 SourceLocation RBracketLoc; 1068public: 1069 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 1070 SourceLocation rbracketloc) 1071 : Expr(ArraySubscriptExprClass, t, 1072 lhs->isTypeDependent() || rhs->isTypeDependent(), 1073 lhs->isValueDependent() || rhs->isValueDependent()), 1074 RBracketLoc(rbracketloc) { 1075 SubExprs[LHS] = lhs; 1076 SubExprs[RHS] = rhs; 1077 } 1078 1079 /// \brief Create an empty array subscript expression. 1080 explicit ArraySubscriptExpr(EmptyShell Shell) 1081 : Expr(ArraySubscriptExprClass, Shell) { } 1082 1083 /// An array access can be written A[4] or 4[A] (both are equivalent). 1084 /// - getBase() and getIdx() always present the normalized view: A[4]. 1085 /// In this case getBase() returns "A" and getIdx() returns "4". 1086 /// - getLHS() and getRHS() present the syntactic view. e.g. for 1087 /// 4[A] getLHS() returns "4". 1088 /// Note: Because vector element access is also written A[4] we must 1089 /// predicate the format conversion in getBase and getIdx only on the 1090 /// the type of the RHS, as it is possible for the LHS to be a vector of 1091 /// integer type 1092 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 1093 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1094 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1095 1096 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 1097 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1098 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1099 1100 Expr *getBase() { 1101 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1102 } 1103 1104 const Expr *getBase() const { 1105 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1106 } 1107 1108 Expr *getIdx() { 1109 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1110 } 1111 1112 const Expr *getIdx() const { 1113 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1114 } 1115 1116 virtual SourceRange getSourceRange() const { 1117 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 1118 } 1119 1120 SourceLocation getRBracketLoc() const { return RBracketLoc; } 1121 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 1122 1123 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 1124 1125 static bool classof(const Stmt *T) { 1126 return T->getStmtClass() == ArraySubscriptExprClass; 1127 } 1128 static bool classof(const ArraySubscriptExpr *) { return true; } 1129 1130 // Iterators 1131 virtual child_iterator child_begin(); 1132 virtual child_iterator child_end(); 1133}; 1134 1135 1136/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 1137/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 1138/// while its subclasses may represent alternative syntax that (semantically) 1139/// results in a function call. For example, CXXOperatorCallExpr is 1140/// a subclass for overloaded operator calls that use operator syntax, e.g., 1141/// "str1 + str2" to resolve to a function call. 1142class CallExpr : public Expr { 1143 enum { FN=0, ARGS_START=1 }; 1144 Stmt **SubExprs; 1145 unsigned NumArgs; 1146 SourceLocation RParenLoc; 1147 1148protected: 1149 // This version of the constructor is for derived classes. 1150 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, 1151 QualType t, SourceLocation rparenloc); 1152 1153 virtual void DoDestroy(ASTContext& C); 1154 1155public: 1156 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 1157 SourceLocation rparenloc); 1158 1159 /// \brief Build an empty call expression. 1160 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 1161 1162 ~CallExpr() {} 1163 1164 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 1165 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 1166 void setCallee(Expr *F) { SubExprs[FN] = F; } 1167 1168 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 1169 FunctionDecl *getDirectCallee(); 1170 const FunctionDecl *getDirectCallee() const { 1171 return const_cast<CallExpr*>(this)->getDirectCallee(); 1172 } 1173 1174 /// getNumArgs - Return the number of actual arguments to this call. 1175 /// 1176 unsigned getNumArgs() const { return NumArgs; } 1177 1178 /// getArg - Return the specified argument. 1179 Expr *getArg(unsigned Arg) { 1180 assert(Arg < NumArgs && "Arg access out of range!"); 1181 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1182 } 1183 const Expr *getArg(unsigned Arg) const { 1184 assert(Arg < NumArgs && "Arg access out of range!"); 1185 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1186 } 1187 1188 /// setArg - Set the specified argument. 1189 void setArg(unsigned Arg, Expr *ArgExpr) { 1190 assert(Arg < NumArgs && "Arg access out of range!"); 1191 SubExprs[Arg+ARGS_START] = ArgExpr; 1192 } 1193 1194 /// setNumArgs - This changes the number of arguments present in this call. 1195 /// Any orphaned expressions are deleted by this, and any new operands are set 1196 /// to null. 1197 void setNumArgs(ASTContext& C, unsigned NumArgs); 1198 1199 typedef ExprIterator arg_iterator; 1200 typedef ConstExprIterator const_arg_iterator; 1201 1202 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 1203 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 1204 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 1205 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 1206 1207 /// getNumCommas - Return the number of commas that must have been present in 1208 /// this function call. 1209 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1210 1211 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1212 /// not, return 0. 1213 unsigned isBuiltinCall(ASTContext &Context) const; 1214 1215 /// getCallReturnType - Get the return type of the call expr. This is not 1216 /// always the type of the expr itself, if the return type is a reference 1217 /// type. 1218 QualType getCallReturnType() const; 1219 1220 SourceLocation getRParenLoc() const { return RParenLoc; } 1221 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1222 1223 virtual SourceRange getSourceRange() const { 1224 return SourceRange(getCallee()->getLocStart(), RParenLoc); 1225 } 1226 1227 static bool classof(const Stmt *T) { 1228 return T->getStmtClass() == CallExprClass || 1229 T->getStmtClass() == CXXOperatorCallExprClass || 1230 T->getStmtClass() == CXXMemberCallExprClass; 1231 } 1232 static bool classof(const CallExpr *) { return true; } 1233 static bool classof(const CXXOperatorCallExpr *) { return true; } 1234 static bool classof(const CXXMemberCallExpr *) { return true; } 1235 1236 // Iterators 1237 virtual child_iterator child_begin(); 1238 virtual child_iterator child_end(); 1239}; 1240 1241/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1242/// 1243class MemberExpr : public Expr { 1244 /// Base - the expression for the base pointer or structure references. In 1245 /// X.F, this is "X". 1246 Stmt *Base; 1247 1248 /// MemberDecl - This is the decl being referenced by the field/member name. 1249 /// In X.F, this is the decl referenced by F. 1250 ValueDecl *MemberDecl; 1251 1252 /// MemberLoc - This is the location of the member name. 1253 SourceLocation MemberLoc; 1254 1255 /// IsArrow - True if this is "X->F", false if this is "X.F". 1256 bool IsArrow : 1; 1257 1258 /// \brief True if this member expression used a nested-name-specifier to 1259 /// refer to the member, e.g., "x->Base::f". When true, a NameQualifier 1260 /// structure is allocated immediately after the MemberExpr. 1261 bool HasQualifier : 1; 1262 1263 /// \brief True if this member expression specified a template argument list 1264 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1265 /// structure (and its TemplateArguments) are allocated immediately after 1266 /// the MemberExpr or, if the member expression also has a qualifier, after 1267 /// the NameQualifier structure. 1268 bool HasExplicitTemplateArgumentList : 1; 1269 1270 /// \brief Retrieve the qualifier that preceded the member name, if any. 1271 NameQualifier *getMemberQualifier() { 1272 if (!HasQualifier) 1273 return 0; 1274 1275 return reinterpret_cast<NameQualifier *> (this + 1); 1276 } 1277 1278 /// \brief Retrieve the qualifier that preceded the member name, if any. 1279 const NameQualifier *getMemberQualifier() const { 1280 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1281 } 1282 1283 /// \brief Retrieve the explicit template argument list that followed the 1284 /// member template name, if any. 1285 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1286 if (!HasExplicitTemplateArgumentList) 1287 return 0; 1288 1289 if (!HasQualifier) 1290 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1291 1292 return reinterpret_cast<ExplicitTemplateArgumentList *>( 1293 getMemberQualifier() + 1); 1294 } 1295 1296 /// \brief Retrieve the explicit template argument list that followed the 1297 /// member template name, if any. 1298 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1299 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList(); 1300 } 1301 1302 MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual, 1303 SourceRange qualrange, ValueDecl *memberdecl, SourceLocation l, 1304 const TemplateArgumentListInfo *targs, QualType ty); 1305 1306public: 1307 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1308 SourceLocation l, QualType ty) 1309 : Expr(MemberExprClass, ty, 1310 base->isTypeDependent(), base->isValueDependent()), 1311 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow), 1312 HasQualifier(false), HasExplicitTemplateArgumentList(false) {} 1313 1314 /// \brief Build an empty member reference expression. 1315 explicit MemberExpr(EmptyShell Empty) 1316 : Expr(MemberExprClass, Empty), HasQualifier(false), 1317 HasExplicitTemplateArgumentList(false) { } 1318 1319 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 1320 NestedNameSpecifier *qual, SourceRange qualrange, 1321 ValueDecl *memberdecl, 1322 SourceLocation l, 1323 const TemplateArgumentListInfo *targs, 1324 QualType ty); 1325 1326 void setBase(Expr *E) { Base = E; } 1327 Expr *getBase() const { return cast<Expr>(Base); } 1328 1329 /// \brief Retrieve the member declaration to which this expression refers. 1330 /// 1331 /// The returned declaration will either be a FieldDecl or (in C++) 1332 /// a CXXMethodDecl. 1333 ValueDecl *getMemberDecl() const { return MemberDecl; } 1334 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 1335 1336 /// \brief Determines whether this member expression actually had 1337 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1338 /// x->Base::foo. 1339 bool hasQualifier() const { return HasQualifier; } 1340 1341 /// \brief If the member name was qualified, retrieves the source range of 1342 /// the nested-name-specifier that precedes the member name. Otherwise, 1343 /// returns an empty source range. 1344 SourceRange getQualifierRange() const { 1345 if (!HasQualifier) 1346 return SourceRange(); 1347 1348 return getMemberQualifier()->Range; 1349 } 1350 1351 /// \brief If the member name was qualified, retrieves the 1352 /// nested-name-specifier that precedes the member name. Otherwise, returns 1353 /// NULL. 1354 NestedNameSpecifier *getQualifier() const { 1355 if (!HasQualifier) 1356 return 0; 1357 1358 return getMemberQualifier()->NNS; 1359 } 1360 1361 /// \brief Determines whether this member expression actually had a C++ 1362 /// template argument list explicitly specified, e.g., x.f<int>. 1363 bool hasExplicitTemplateArgumentList() const { 1364 return HasExplicitTemplateArgumentList; 1365 } 1366 1367 /// \brief Copies the template arguments (if present) into the given 1368 /// structure. 1369 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1370 if (hasExplicitTemplateArgumentList()) 1371 getExplicitTemplateArgumentList()->copyInto(List); 1372 } 1373 1374 /// \brief Retrieve the location of the left angle bracket following the 1375 /// member name ('<'), if any. 1376 SourceLocation getLAngleLoc() const { 1377 if (!HasExplicitTemplateArgumentList) 1378 return SourceLocation(); 1379 1380 return getExplicitTemplateArgumentList()->LAngleLoc; 1381 } 1382 1383 /// \brief Retrieve the template arguments provided as part of this 1384 /// template-id. 1385 const TemplateArgumentLoc *getTemplateArgs() const { 1386 if (!HasExplicitTemplateArgumentList) 1387 return 0; 1388 1389 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1390 } 1391 1392 /// \brief Retrieve the number of template arguments provided as part of this 1393 /// template-id. 1394 unsigned getNumTemplateArgs() const { 1395 if (!HasExplicitTemplateArgumentList) 1396 return 0; 1397 1398 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1399 } 1400 1401 /// \brief Retrieve the location of the right angle bracket following the 1402 /// template arguments ('>'). 1403 SourceLocation getRAngleLoc() const { 1404 if (!HasExplicitTemplateArgumentList) 1405 return SourceLocation(); 1406 1407 return getExplicitTemplateArgumentList()->RAngleLoc; 1408 } 1409 1410 bool isArrow() const { return IsArrow; } 1411 void setArrow(bool A) { IsArrow = A; } 1412 1413 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1414 /// location of 'F'. 1415 SourceLocation getMemberLoc() const { return MemberLoc; } 1416 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1417 1418 virtual SourceRange getSourceRange() const { 1419 // If we have an implicit base (like a C++ implicit this), 1420 // make sure not to return its location 1421 SourceLocation EndLoc = MemberLoc; 1422 if (HasExplicitTemplateArgumentList) 1423 EndLoc = getRAngleLoc(); 1424 1425 SourceLocation BaseLoc = getBase()->getLocStart(); 1426 if (BaseLoc.isInvalid()) 1427 return SourceRange(MemberLoc, EndLoc); 1428 return SourceRange(BaseLoc, EndLoc); 1429 } 1430 1431 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1432 1433 static bool classof(const Stmt *T) { 1434 return T->getStmtClass() == MemberExprClass; 1435 } 1436 static bool classof(const MemberExpr *) { return true; } 1437 1438 // Iterators 1439 virtual child_iterator child_begin(); 1440 virtual child_iterator child_end(); 1441}; 1442 1443/// CompoundLiteralExpr - [C99 6.5.2.5] 1444/// 1445class CompoundLiteralExpr : public Expr { 1446 /// LParenLoc - If non-null, this is the location of the left paren in a 1447 /// compound literal like "(int){4}". This can be null if this is a 1448 /// synthesized compound expression. 1449 SourceLocation LParenLoc; 1450 Stmt *Init; 1451 bool FileScope; 1452public: 1453 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 1454 bool fileScope) 1455 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 1456 FileScope(fileScope) {} 1457 1458 /// \brief Construct an empty compound literal. 1459 explicit CompoundLiteralExpr(EmptyShell Empty) 1460 : Expr(CompoundLiteralExprClass, Empty) { } 1461 1462 const Expr *getInitializer() const { return cast<Expr>(Init); } 1463 Expr *getInitializer() { return cast<Expr>(Init); } 1464 void setInitializer(Expr *E) { Init = E; } 1465 1466 bool isFileScope() const { return FileScope; } 1467 void setFileScope(bool FS) { FileScope = FS; } 1468 1469 SourceLocation getLParenLoc() const { return LParenLoc; } 1470 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1471 1472 virtual SourceRange getSourceRange() const { 1473 // FIXME: Init should never be null. 1474 if (!Init) 1475 return SourceRange(); 1476 if (LParenLoc.isInvalid()) 1477 return Init->getSourceRange(); 1478 return SourceRange(LParenLoc, Init->getLocEnd()); 1479 } 1480 1481 static bool classof(const Stmt *T) { 1482 return T->getStmtClass() == CompoundLiteralExprClass; 1483 } 1484 static bool classof(const CompoundLiteralExpr *) { return true; } 1485 1486 // Iterators 1487 virtual child_iterator child_begin(); 1488 virtual child_iterator child_end(); 1489}; 1490 1491/// CastExpr - Base class for type casts, including both implicit 1492/// casts (ImplicitCastExpr) and explicit casts that have some 1493/// representation in the source code (ExplicitCastExpr's derived 1494/// classes). 1495class CastExpr : public Expr { 1496public: 1497 /// CastKind - the kind of cast this represents. 1498 enum CastKind { 1499 /// CK_Unknown - Unknown cast kind. 1500 /// FIXME: The goal is to get rid of this and make all casts have a 1501 /// kind so that the AST client doesn't have to try to figure out what's 1502 /// going on. 1503 CK_Unknown, 1504 1505 /// CK_BitCast - Used for reinterpret_cast. 1506 CK_BitCast, 1507 1508 /// CK_NoOp - Used for const_cast. 1509 CK_NoOp, 1510 1511 /// CK_BaseToDerived - Base to derived class casts. 1512 CK_BaseToDerived, 1513 1514 /// CK_DerivedToBase - Derived to base class casts. 1515 CK_DerivedToBase, 1516 1517 /// CK_Dynamic - Dynamic cast. 1518 CK_Dynamic, 1519 1520 /// CK_ToUnion - Cast to union (GCC extension). 1521 CK_ToUnion, 1522 1523 /// CK_ArrayToPointerDecay - Array to pointer decay. 1524 CK_ArrayToPointerDecay, 1525 1526 // CK_FunctionToPointerDecay - Function to pointer decay. 1527 CK_FunctionToPointerDecay, 1528 1529 /// CK_NullToMemberPointer - Null pointer to member pointer. 1530 CK_NullToMemberPointer, 1531 1532 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to 1533 /// member pointer in derived class. 1534 CK_BaseToDerivedMemberPointer, 1535 1536 /// CK_DerivedToBaseMemberPointer - Member pointer in derived class to 1537 /// member pointer in base class. 1538 CK_DerivedToBaseMemberPointer, 1539 1540 /// CK_UserDefinedConversion - Conversion using a user defined type 1541 /// conversion function. 1542 CK_UserDefinedConversion, 1543 1544 /// CK_ConstructorConversion - Conversion by constructor 1545 CK_ConstructorConversion, 1546 1547 /// CK_IntegralToPointer - Integral to pointer 1548 CK_IntegralToPointer, 1549 1550 /// CK_PointerToIntegral - Pointer to integral 1551 CK_PointerToIntegral, 1552 1553 /// CK_ToVoid - Cast to void. 1554 CK_ToVoid, 1555 1556 /// CK_VectorSplat - Casting from an integer/floating type to an extended 1557 /// vector type with the same element type as the src type. Splats the 1558 /// src expression into the destination expression. 1559 CK_VectorSplat, 1560 1561 /// CK_IntegralCast - Casting between integral types of different size. 1562 CK_IntegralCast, 1563 1564 /// CK_IntegralToFloating - Integral to floating point. 1565 CK_IntegralToFloating, 1566 1567 /// CK_FloatingToIntegral - Floating point to integral. 1568 CK_FloatingToIntegral, 1569 1570 /// CK_FloatingCast - Casting between floating types of different size. 1571 CK_FloatingCast, 1572 1573 /// CK_MemberPointerToBoolean - Member pointer to boolean 1574 CK_MemberPointerToBoolean 1575 1576 }; 1577 1578private: 1579 CastKind Kind; 1580 Stmt *Op; 1581protected: 1582 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) : 1583 Expr(SC, ty, 1584 // Cast expressions are type-dependent if the type is 1585 // dependent (C++ [temp.dep.expr]p3). 1586 ty->isDependentType(), 1587 // Cast expressions are value-dependent if the type is 1588 // dependent or if the subexpression is value-dependent. 1589 ty->isDependentType() || (op && op->isValueDependent())), 1590 Kind(kind), Op(op) {} 1591 1592 /// \brief Construct an empty cast. 1593 CastExpr(StmtClass SC, EmptyShell Empty) 1594 : Expr(SC, Empty) { } 1595 1596public: 1597 CastKind getCastKind() const { return Kind; } 1598 void setCastKind(CastKind K) { Kind = K; } 1599 const char *getCastKindName() const; 1600 1601 Expr *getSubExpr() { return cast<Expr>(Op); } 1602 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1603 void setSubExpr(Expr *E) { Op = E; } 1604 1605 static bool classof(const Stmt *T) { 1606 StmtClass SC = T->getStmtClass(); 1607 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1608 return true; 1609 1610 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1611 return true; 1612 1613 return false; 1614 } 1615 static bool classof(const CastExpr *) { return true; } 1616 1617 // Iterators 1618 virtual child_iterator child_begin(); 1619 virtual child_iterator child_end(); 1620}; 1621 1622/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1623/// conversions, which have no direct representation in the original 1624/// source code. For example: converting T[]->T*, void f()->void 1625/// (*f)(), float->double, short->int, etc. 1626/// 1627/// In C, implicit casts always produce rvalues. However, in C++, an 1628/// implicit cast whose result is being bound to a reference will be 1629/// an lvalue. For example: 1630/// 1631/// @code 1632/// class Base { }; 1633/// class Derived : public Base { }; 1634/// void f(Derived d) { 1635/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1636/// } 1637/// @endcode 1638class ImplicitCastExpr : public CastExpr { 1639 /// LvalueCast - Whether this cast produces an lvalue. 1640 bool LvalueCast; 1641 1642public: 1643 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) : 1644 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { } 1645 1646 /// \brief Construct an empty implicit cast. 1647 explicit ImplicitCastExpr(EmptyShell Shell) 1648 : CastExpr(ImplicitCastExprClass, Shell) { } 1649 1650 1651 virtual SourceRange getSourceRange() const { 1652 return getSubExpr()->getSourceRange(); 1653 } 1654 1655 /// isLvalueCast - Whether this cast produces an lvalue. 1656 bool isLvalueCast() const { return LvalueCast; } 1657 1658 /// setLvalueCast - Set whether this cast produces an lvalue. 1659 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1660 1661 static bool classof(const Stmt *T) { 1662 return T->getStmtClass() == ImplicitCastExprClass; 1663 } 1664 static bool classof(const ImplicitCastExpr *) { return true; } 1665}; 1666 1667/// ExplicitCastExpr - An explicit cast written in the source 1668/// code. 1669/// 1670/// This class is effectively an abstract class, because it provides 1671/// the basic representation of an explicitly-written cast without 1672/// specifying which kind of cast (C cast, functional cast, static 1673/// cast, etc.) was written; specific derived classes represent the 1674/// particular style of cast and its location information. 1675/// 1676/// Unlike implicit casts, explicit cast nodes have two different 1677/// types: the type that was written into the source code, and the 1678/// actual type of the expression as determined by semantic 1679/// analysis. These types may differ slightly. For example, in C++ one 1680/// can cast to a reference type, which indicates that the resulting 1681/// expression will be an lvalue. The reference type, however, will 1682/// not be used as the type of the expression. 1683class ExplicitCastExpr : public CastExpr { 1684 /// TypeAsWritten - The type that this expression is casting to, as 1685 /// written in the source code. 1686 QualType TypeAsWritten; 1687 1688protected: 1689 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 1690 Expr *op, QualType writtenTy) 1691 : CastExpr(SC, exprTy, kind, op), TypeAsWritten(writtenTy) {} 1692 1693 /// \brief Construct an empty explicit cast. 1694 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 1695 : CastExpr(SC, Shell) { } 1696 1697public: 1698 /// getTypeAsWritten - Returns the type that this expression is 1699 /// casting to, as written in the source code. 1700 QualType getTypeAsWritten() const { return TypeAsWritten; } 1701 void setTypeAsWritten(QualType T) { TypeAsWritten = T; } 1702 1703 static bool classof(const Stmt *T) { 1704 StmtClass SC = T->getStmtClass(); 1705 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1706 return true; 1707 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1708 return true; 1709 1710 return false; 1711 } 1712 static bool classof(const ExplicitCastExpr *) { return true; } 1713}; 1714 1715/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1716/// cast in C++ (C++ [expr.cast]), which uses the syntax 1717/// (Type)expr. For example: @c (int)f. 1718class CStyleCastExpr : public ExplicitCastExpr { 1719 SourceLocation LPLoc; // the location of the left paren 1720 SourceLocation RPLoc; // the location of the right paren 1721public: 1722 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy, 1723 SourceLocation l, SourceLocation r) : 1724 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy), 1725 LPLoc(l), RPLoc(r) {} 1726 1727 /// \brief Construct an empty C-style explicit cast. 1728 explicit CStyleCastExpr(EmptyShell Shell) 1729 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 1730 1731 SourceLocation getLParenLoc() const { return LPLoc; } 1732 void setLParenLoc(SourceLocation L) { LPLoc = L; } 1733 1734 SourceLocation getRParenLoc() const { return RPLoc; } 1735 void setRParenLoc(SourceLocation L) { RPLoc = L; } 1736 1737 virtual SourceRange getSourceRange() const { 1738 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1739 } 1740 static bool classof(const Stmt *T) { 1741 return T->getStmtClass() == CStyleCastExprClass; 1742 } 1743 static bool classof(const CStyleCastExpr *) { return true; } 1744}; 1745 1746/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 1747/// 1748/// This expression node kind describes a builtin binary operation, 1749/// such as "x + y" for integer values "x" and "y". The operands will 1750/// already have been converted to appropriate types (e.g., by 1751/// performing promotions or conversions). 1752/// 1753/// In C++, where operators may be overloaded, a different kind of 1754/// expression node (CXXOperatorCallExpr) is used to express the 1755/// invocation of an overloaded operator with operator syntax. Within 1756/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 1757/// used to store an expression "x + y" depends on the subexpressions 1758/// for x and y. If neither x or y is type-dependent, and the "+" 1759/// operator resolves to a built-in operation, BinaryOperator will be 1760/// used to express the computation (x and y may still be 1761/// value-dependent). If either x or y is type-dependent, or if the 1762/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 1763/// be used to express the computation. 1764class BinaryOperator : public Expr { 1765public: 1766 enum Opcode { 1767 // Operators listed in order of precedence. 1768 // Note that additions to this should also update the StmtVisitor class. 1769 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 1770 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1771 Add, Sub, // [C99 6.5.6] Additive operators. 1772 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1773 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1774 EQ, NE, // [C99 6.5.9] Equality operators. 1775 And, // [C99 6.5.10] Bitwise AND operator. 1776 Xor, // [C99 6.5.11] Bitwise XOR operator. 1777 Or, // [C99 6.5.12] Bitwise OR operator. 1778 LAnd, // [C99 6.5.13] Logical AND operator. 1779 LOr, // [C99 6.5.14] Logical OR operator. 1780 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1781 DivAssign, RemAssign, 1782 AddAssign, SubAssign, 1783 ShlAssign, ShrAssign, 1784 AndAssign, XorAssign, 1785 OrAssign, 1786 Comma // [C99 6.5.17] Comma operator. 1787 }; 1788private: 1789 enum { LHS, RHS, END_EXPR }; 1790 Stmt* SubExprs[END_EXPR]; 1791 Opcode Opc; 1792 SourceLocation OpLoc; 1793public: 1794 1795 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1796 SourceLocation opLoc) 1797 : Expr(BinaryOperatorClass, ResTy, 1798 lhs->isTypeDependent() || rhs->isTypeDependent(), 1799 lhs->isValueDependent() || rhs->isValueDependent()), 1800 Opc(opc), OpLoc(opLoc) { 1801 SubExprs[LHS] = lhs; 1802 SubExprs[RHS] = rhs; 1803 assert(!isCompoundAssignmentOp() && 1804 "Use ArithAssignBinaryOperator for compound assignments"); 1805 } 1806 1807 /// \brief Construct an empty binary operator. 1808 explicit BinaryOperator(EmptyShell Empty) 1809 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 1810 1811 SourceLocation getOperatorLoc() const { return OpLoc; } 1812 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1813 1814 Opcode getOpcode() const { return Opc; } 1815 void setOpcode(Opcode O) { Opc = O; } 1816 1817 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1818 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1819 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1820 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1821 1822 virtual SourceRange getSourceRange() const { 1823 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1824 } 1825 1826 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1827 /// corresponds to, e.g. "<<=". 1828 static const char *getOpcodeStr(Opcode Op); 1829 1830 /// \brief Retrieve the binary opcode that corresponds to the given 1831 /// overloaded operator. 1832 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 1833 1834 /// \brief Retrieve the overloaded operator kind that corresponds to 1835 /// the given binary opcode. 1836 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1837 1838 /// predicates to categorize the respective opcodes. 1839 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1840 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1841 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; } 1842 bool isShiftOp() const { return isShiftOp(Opc); } 1843 1844 static bool isBitwiseOp(Opcode Opc) { return Opc >= And && Opc <= Or; } 1845 bool isBitwiseOp() const { return isBitwiseOp(Opc); } 1846 1847 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1848 bool isRelationalOp() const { return isRelationalOp(Opc); } 1849 1850 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1851 bool isEqualityOp() const { return isEqualityOp(Opc); } 1852 1853 static bool isComparisonOp(Opcode Opc) { return Opc >= LT && Opc <= NE; } 1854 bool isComparisonOp() const { return isComparisonOp(Opc); } 1855 1856 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1857 bool isLogicalOp() const { return isLogicalOp(Opc); } 1858 1859 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1860 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1861 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1862 1863 static bool classof(const Stmt *S) { 1864 return S->getStmtClass() == BinaryOperatorClass || 1865 S->getStmtClass() == CompoundAssignOperatorClass; 1866 } 1867 static bool classof(const BinaryOperator *) { return true; } 1868 1869 // Iterators 1870 virtual child_iterator child_begin(); 1871 virtual child_iterator child_end(); 1872 1873protected: 1874 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1875 SourceLocation oploc, bool dead) 1876 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1877 SubExprs[LHS] = lhs; 1878 SubExprs[RHS] = rhs; 1879 } 1880 1881 BinaryOperator(StmtClass SC, EmptyShell Empty) 1882 : Expr(SC, Empty), Opc(MulAssign) { } 1883}; 1884 1885/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1886/// track of the type the operation is performed in. Due to the semantics of 1887/// these operators, the operands are promoted, the aritmetic performed, an 1888/// implicit conversion back to the result type done, then the assignment takes 1889/// place. This captures the intermediate type which the computation is done 1890/// in. 1891class CompoundAssignOperator : public BinaryOperator { 1892 QualType ComputationLHSType; 1893 QualType ComputationResultType; 1894public: 1895 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1896 QualType ResType, QualType CompLHSType, 1897 QualType CompResultType, 1898 SourceLocation OpLoc) 1899 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1900 ComputationLHSType(CompLHSType), 1901 ComputationResultType(CompResultType) { 1902 assert(isCompoundAssignmentOp() && 1903 "Only should be used for compound assignments"); 1904 } 1905 1906 /// \brief Build an empty compound assignment operator expression. 1907 explicit CompoundAssignOperator(EmptyShell Empty) 1908 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 1909 1910 // The two computation types are the type the LHS is converted 1911 // to for the computation and the type of the result; the two are 1912 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 1913 QualType getComputationLHSType() const { return ComputationLHSType; } 1914 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 1915 1916 QualType getComputationResultType() const { return ComputationResultType; } 1917 void setComputationResultType(QualType T) { ComputationResultType = T; } 1918 1919 static bool classof(const CompoundAssignOperator *) { return true; } 1920 static bool classof(const Stmt *S) { 1921 return S->getStmtClass() == CompoundAssignOperatorClass; 1922 } 1923}; 1924 1925/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1926/// GNU "missing LHS" extension is in use. 1927/// 1928class ConditionalOperator : public Expr { 1929 enum { COND, LHS, RHS, END_EXPR }; 1930 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1931 SourceLocation QuestionLoc, ColonLoc; 1932public: 1933 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 1934 SourceLocation CLoc, Expr *rhs, QualType t) 1935 : Expr(ConditionalOperatorClass, t, 1936 // FIXME: the type of the conditional operator doesn't 1937 // depend on the type of the conditional, but the standard 1938 // seems to imply that it could. File a bug! 1939 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1940 (cond->isValueDependent() || 1941 (lhs && lhs->isValueDependent()) || 1942 (rhs && rhs->isValueDependent()))), 1943 QuestionLoc(QLoc), 1944 ColonLoc(CLoc) { 1945 SubExprs[COND] = cond; 1946 SubExprs[LHS] = lhs; 1947 SubExprs[RHS] = rhs; 1948 } 1949 1950 /// \brief Build an empty conditional operator. 1951 explicit ConditionalOperator(EmptyShell Empty) 1952 : Expr(ConditionalOperatorClass, Empty) { } 1953 1954 // getCond - Return the expression representing the condition for 1955 // the ?: operator. 1956 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1957 void setCond(Expr *E) { SubExprs[COND] = E; } 1958 1959 // getTrueExpr - Return the subexpression representing the value of the ?: 1960 // expression if the condition evaluates to true. In most cases this value 1961 // will be the same as getLHS() except a GCC extension allows the left 1962 // subexpression to be omitted, and instead of the condition be returned. 1963 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1964 // is only evaluated once. 1965 Expr *getTrueExpr() const { 1966 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1967 } 1968 1969 // getTrueExpr - Return the subexpression representing the value of the ?: 1970 // expression if the condition evaluates to false. This is the same as getRHS. 1971 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1972 1973 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1974 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1975 1976 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1977 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1978 1979 SourceLocation getQuestionLoc() const { return QuestionLoc; } 1980 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 1981 1982 SourceLocation getColonLoc() const { return ColonLoc; } 1983 void setColonLoc(SourceLocation L) { ColonLoc = L; } 1984 1985 virtual SourceRange getSourceRange() const { 1986 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1987 } 1988 static bool classof(const Stmt *T) { 1989 return T->getStmtClass() == ConditionalOperatorClass; 1990 } 1991 static bool classof(const ConditionalOperator *) { return true; } 1992 1993 // Iterators 1994 virtual child_iterator child_begin(); 1995 virtual child_iterator child_end(); 1996}; 1997 1998/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1999class AddrLabelExpr : public Expr { 2000 SourceLocation AmpAmpLoc, LabelLoc; 2001 LabelStmt *Label; 2002public: 2003 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 2004 QualType t) 2005 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 2006 2007 /// \brief Build an empty address of a label expression. 2008 explicit AddrLabelExpr(EmptyShell Empty) 2009 : Expr(AddrLabelExprClass, Empty) { } 2010 2011 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 2012 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 2013 SourceLocation getLabelLoc() const { return LabelLoc; } 2014 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2015 2016 virtual SourceRange getSourceRange() const { 2017 return SourceRange(AmpAmpLoc, LabelLoc); 2018 } 2019 2020 LabelStmt *getLabel() const { return Label; } 2021 void setLabel(LabelStmt *S) { Label = S; } 2022 2023 static bool classof(const Stmt *T) { 2024 return T->getStmtClass() == AddrLabelExprClass; 2025 } 2026 static bool classof(const AddrLabelExpr *) { return true; } 2027 2028 // Iterators 2029 virtual child_iterator child_begin(); 2030 virtual child_iterator child_end(); 2031}; 2032 2033/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 2034/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 2035/// takes the value of the last subexpression. 2036class StmtExpr : public Expr { 2037 Stmt *SubStmt; 2038 SourceLocation LParenLoc, RParenLoc; 2039public: 2040 StmtExpr(CompoundStmt *substmt, QualType T, 2041 SourceLocation lp, SourceLocation rp) : 2042 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 2043 2044 /// \brief Build an empty statement expression. 2045 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 2046 2047 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 2048 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 2049 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 2050 2051 virtual SourceRange getSourceRange() const { 2052 return SourceRange(LParenLoc, RParenLoc); 2053 } 2054 2055 SourceLocation getLParenLoc() const { return LParenLoc; } 2056 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2057 SourceLocation getRParenLoc() const { return RParenLoc; } 2058 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2059 2060 static bool classof(const Stmt *T) { 2061 return T->getStmtClass() == StmtExprClass; 2062 } 2063 static bool classof(const StmtExpr *) { return true; } 2064 2065 // Iterators 2066 virtual child_iterator child_begin(); 2067 virtual child_iterator child_end(); 2068}; 2069 2070/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 2071/// This AST node represents a function that returns 1 if two *types* (not 2072/// expressions) are compatible. The result of this built-in function can be 2073/// used in integer constant expressions. 2074class TypesCompatibleExpr : public Expr { 2075 QualType Type1; 2076 QualType Type2; 2077 SourceLocation BuiltinLoc, RParenLoc; 2078public: 2079 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 2080 QualType t1, QualType t2, SourceLocation RP) : 2081 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 2082 BuiltinLoc(BLoc), RParenLoc(RP) {} 2083 2084 /// \brief Build an empty __builtin_type_compatible_p expression. 2085 explicit TypesCompatibleExpr(EmptyShell Empty) 2086 : Expr(TypesCompatibleExprClass, Empty) { } 2087 2088 QualType getArgType1() const { return Type1; } 2089 void setArgType1(QualType T) { Type1 = T; } 2090 QualType getArgType2() const { return Type2; } 2091 void setArgType2(QualType T) { Type2 = T; } 2092 2093 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2094 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2095 2096 SourceLocation getRParenLoc() const { return RParenLoc; } 2097 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2098 2099 virtual SourceRange getSourceRange() const { 2100 return SourceRange(BuiltinLoc, RParenLoc); 2101 } 2102 static bool classof(const Stmt *T) { 2103 return T->getStmtClass() == TypesCompatibleExprClass; 2104 } 2105 static bool classof(const TypesCompatibleExpr *) { return true; } 2106 2107 // Iterators 2108 virtual child_iterator child_begin(); 2109 virtual child_iterator child_end(); 2110}; 2111 2112/// ShuffleVectorExpr - clang-specific builtin-in function 2113/// __builtin_shufflevector. 2114/// This AST node represents a operator that does a constant 2115/// shuffle, similar to LLVM's shufflevector instruction. It takes 2116/// two vectors and a variable number of constant indices, 2117/// and returns the appropriately shuffled vector. 2118class ShuffleVectorExpr : public Expr { 2119 SourceLocation BuiltinLoc, RParenLoc; 2120 2121 // SubExprs - the list of values passed to the __builtin_shufflevector 2122 // function. The first two are vectors, and the rest are constant 2123 // indices. The number of values in this list is always 2124 // 2+the number of indices in the vector type. 2125 Stmt **SubExprs; 2126 unsigned NumExprs; 2127 2128protected: 2129 virtual void DoDestroy(ASTContext &C); 2130 2131public: 2132 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 2133 QualType Type, SourceLocation BLoc, 2134 SourceLocation RP) : 2135 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 2136 RParenLoc(RP), NumExprs(nexpr) { 2137 2138 SubExprs = new (C) Stmt*[nexpr]; 2139 for (unsigned i = 0; i < nexpr; i++) 2140 SubExprs[i] = args[i]; 2141 } 2142 2143 /// \brief Build an empty vector-shuffle expression. 2144 explicit ShuffleVectorExpr(EmptyShell Empty) 2145 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 2146 2147 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2148 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2149 2150 SourceLocation getRParenLoc() const { return RParenLoc; } 2151 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2152 2153 virtual SourceRange getSourceRange() const { 2154 return SourceRange(BuiltinLoc, RParenLoc); 2155 } 2156 static bool classof(const Stmt *T) { 2157 return T->getStmtClass() == ShuffleVectorExprClass; 2158 } 2159 static bool classof(const ShuffleVectorExpr *) { return true; } 2160 2161 ~ShuffleVectorExpr() {} 2162 2163 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 2164 /// constant expression, the actual arguments passed in, and the function 2165 /// pointers. 2166 unsigned getNumSubExprs() const { return NumExprs; } 2167 2168 /// getExpr - Return the Expr at the specified index. 2169 Expr *getExpr(unsigned Index) { 2170 assert((Index < NumExprs) && "Arg access out of range!"); 2171 return cast<Expr>(SubExprs[Index]); 2172 } 2173 const Expr *getExpr(unsigned Index) const { 2174 assert((Index < NumExprs) && "Arg access out of range!"); 2175 return cast<Expr>(SubExprs[Index]); 2176 } 2177 2178 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 2179 2180 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 2181 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 2182 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 2183 } 2184 2185 // Iterators 2186 virtual child_iterator child_begin(); 2187 virtual child_iterator child_end(); 2188}; 2189 2190/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 2191/// This AST node is similar to the conditional operator (?:) in C, with 2192/// the following exceptions: 2193/// - the test expression must be a integer constant expression. 2194/// - the expression returned acts like the chosen subexpression in every 2195/// visible way: the type is the same as that of the chosen subexpression, 2196/// and all predicates (whether it's an l-value, whether it's an integer 2197/// constant expression, etc.) return the same result as for the chosen 2198/// sub-expression. 2199class ChooseExpr : public Expr { 2200 enum { COND, LHS, RHS, END_EXPR }; 2201 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2202 SourceLocation BuiltinLoc, RParenLoc; 2203public: 2204 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2205 SourceLocation RP, bool TypeDependent, bool ValueDependent) 2206 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent), 2207 BuiltinLoc(BLoc), RParenLoc(RP) { 2208 SubExprs[COND] = cond; 2209 SubExprs[LHS] = lhs; 2210 SubExprs[RHS] = rhs; 2211 } 2212 2213 /// \brief Build an empty __builtin_choose_expr. 2214 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2215 2216 /// isConditionTrue - Return whether the condition is true (i.e. not 2217 /// equal to zero). 2218 bool isConditionTrue(ASTContext &C) const; 2219 2220 /// getChosenSubExpr - Return the subexpression chosen according to the 2221 /// condition. 2222 Expr *getChosenSubExpr(ASTContext &C) const { 2223 return isConditionTrue(C) ? getLHS() : getRHS(); 2224 } 2225 2226 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2227 void setCond(Expr *E) { SubExprs[COND] = E; } 2228 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2229 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2230 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2231 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2232 2233 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2234 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2235 2236 SourceLocation getRParenLoc() const { return RParenLoc; } 2237 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2238 2239 virtual SourceRange getSourceRange() const { 2240 return SourceRange(BuiltinLoc, RParenLoc); 2241 } 2242 static bool classof(const Stmt *T) { 2243 return T->getStmtClass() == ChooseExprClass; 2244 } 2245 static bool classof(const ChooseExpr *) { return true; } 2246 2247 // Iterators 2248 virtual child_iterator child_begin(); 2249 virtual child_iterator child_end(); 2250}; 2251 2252/// GNUNullExpr - Implements the GNU __null extension, which is a name 2253/// for a null pointer constant that has integral type (e.g., int or 2254/// long) and is the same size and alignment as a pointer. The __null 2255/// extension is typically only used by system headers, which define 2256/// NULL as __null in C++ rather than using 0 (which is an integer 2257/// that may not match the size of a pointer). 2258class GNUNullExpr : public Expr { 2259 /// TokenLoc - The location of the __null keyword. 2260 SourceLocation TokenLoc; 2261 2262public: 2263 GNUNullExpr(QualType Ty, SourceLocation Loc) 2264 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 2265 2266 /// \brief Build an empty GNU __null expression. 2267 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2268 2269 /// getTokenLocation - The location of the __null token. 2270 SourceLocation getTokenLocation() const { return TokenLoc; } 2271 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2272 2273 virtual SourceRange getSourceRange() const { 2274 return SourceRange(TokenLoc); 2275 } 2276 static bool classof(const Stmt *T) { 2277 return T->getStmtClass() == GNUNullExprClass; 2278 } 2279 static bool classof(const GNUNullExpr *) { return true; } 2280 2281 // Iterators 2282 virtual child_iterator child_begin(); 2283 virtual child_iterator child_end(); 2284}; 2285 2286/// VAArgExpr, used for the builtin function __builtin_va_start. 2287class VAArgExpr : public Expr { 2288 Stmt *Val; 2289 SourceLocation BuiltinLoc, RParenLoc; 2290public: 2291 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 2292 : Expr(VAArgExprClass, t), 2293 Val(e), 2294 BuiltinLoc(BLoc), 2295 RParenLoc(RPLoc) { } 2296 2297 /// \brief Create an empty __builtin_va_start expression. 2298 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2299 2300 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2301 Expr *getSubExpr() { return cast<Expr>(Val); } 2302 void setSubExpr(Expr *E) { Val = E; } 2303 2304 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2305 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2306 2307 SourceLocation getRParenLoc() const { return RParenLoc; } 2308 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2309 2310 virtual SourceRange getSourceRange() const { 2311 return SourceRange(BuiltinLoc, RParenLoc); 2312 } 2313 static bool classof(const Stmt *T) { 2314 return T->getStmtClass() == VAArgExprClass; 2315 } 2316 static bool classof(const VAArgExpr *) { return true; } 2317 2318 // Iterators 2319 virtual child_iterator child_begin(); 2320 virtual child_iterator child_end(); 2321}; 2322 2323/// @brief Describes an C or C++ initializer list. 2324/// 2325/// InitListExpr describes an initializer list, which can be used to 2326/// initialize objects of different types, including 2327/// struct/class/union types, arrays, and vectors. For example: 2328/// 2329/// @code 2330/// struct foo x = { 1, { 2, 3 } }; 2331/// @endcode 2332/// 2333/// Prior to semantic analysis, an initializer list will represent the 2334/// initializer list as written by the user, but will have the 2335/// placeholder type "void". This initializer list is called the 2336/// syntactic form of the initializer, and may contain C99 designated 2337/// initializers (represented as DesignatedInitExprs), initializations 2338/// of subobject members without explicit braces, and so on. Clients 2339/// interested in the original syntax of the initializer list should 2340/// use the syntactic form of the initializer list. 2341/// 2342/// After semantic analysis, the initializer list will represent the 2343/// semantic form of the initializer, where the initializations of all 2344/// subobjects are made explicit with nested InitListExpr nodes and 2345/// C99 designators have been eliminated by placing the designated 2346/// initializations into the subobject they initialize. Additionally, 2347/// any "holes" in the initialization, where no initializer has been 2348/// specified for a particular subobject, will be replaced with 2349/// implicitly-generated ImplicitValueInitExpr expressions that 2350/// value-initialize the subobjects. Note, however, that the 2351/// initializer lists may still have fewer initializers than there are 2352/// elements to initialize within the object. 2353/// 2354/// Given the semantic form of the initializer list, one can retrieve 2355/// the original syntactic form of that initializer list (if it 2356/// exists) using getSyntacticForm(). Since many initializer lists 2357/// have the same syntactic and semantic forms, getSyntacticForm() may 2358/// return NULL, indicating that the current initializer list also 2359/// serves as its syntactic form. 2360class InitListExpr : public Expr { 2361 // FIXME: Eliminate this vector in favor of ASTContext allocation 2362 std::vector<Stmt *> InitExprs; 2363 SourceLocation LBraceLoc, RBraceLoc; 2364 2365 /// Contains the initializer list that describes the syntactic form 2366 /// written in the source code. 2367 InitListExpr *SyntacticForm; 2368 2369 /// If this initializer list initializes a union, specifies which 2370 /// field within the union will be initialized. 2371 FieldDecl *UnionFieldInit; 2372 2373 /// Whether this initializer list originally had a GNU array-range 2374 /// designator in it. This is a temporary marker used by CodeGen. 2375 bool HadArrayRangeDesignator; 2376 2377public: 2378 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 2379 SourceLocation rbraceloc); 2380 2381 /// \brief Build an empty initializer list. 2382 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { } 2383 2384 unsigned getNumInits() const { return InitExprs.size(); } 2385 2386 const Expr* getInit(unsigned Init) const { 2387 assert(Init < getNumInits() && "Initializer access out of range!"); 2388 return cast_or_null<Expr>(InitExprs[Init]); 2389 } 2390 2391 Expr* getInit(unsigned Init) { 2392 assert(Init < getNumInits() && "Initializer access out of range!"); 2393 return cast_or_null<Expr>(InitExprs[Init]); 2394 } 2395 2396 void setInit(unsigned Init, Expr *expr) { 2397 assert(Init < getNumInits() && "Initializer access out of range!"); 2398 InitExprs[Init] = expr; 2399 } 2400 2401 /// \brief Reserve space for some number of initializers. 2402 void reserveInits(unsigned NumInits); 2403 2404 /// @brief Specify the number of initializers 2405 /// 2406 /// If there are more than @p NumInits initializers, the remaining 2407 /// initializers will be destroyed. If there are fewer than @p 2408 /// NumInits initializers, NULL expressions will be added for the 2409 /// unknown initializers. 2410 void resizeInits(ASTContext &Context, unsigned NumInits); 2411 2412 /// @brief Updates the initializer at index @p Init with the new 2413 /// expression @p expr, and returns the old expression at that 2414 /// location. 2415 /// 2416 /// When @p Init is out of range for this initializer list, the 2417 /// initializer list will be extended with NULL expressions to 2418 /// accomodate the new entry. 2419 Expr *updateInit(unsigned Init, Expr *expr); 2420 2421 /// \brief If this initializes a union, specifies which field in the 2422 /// union to initialize. 2423 /// 2424 /// Typically, this field is the first named field within the 2425 /// union. However, a designated initializer can specify the 2426 /// initialization of a different field within the union. 2427 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2428 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2429 2430 // Explicit InitListExpr's originate from source code (and have valid source 2431 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2432 bool isExplicit() { 2433 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2434 } 2435 2436 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2437 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2438 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2439 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2440 2441 /// @brief Retrieve the initializer list that describes the 2442 /// syntactic form of the initializer. 2443 /// 2444 /// 2445 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2446 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2447 2448 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2449 void sawArrayRangeDesignator(bool ARD = true) { 2450 HadArrayRangeDesignator = ARD; 2451 } 2452 2453 virtual SourceRange getSourceRange() const { 2454 return SourceRange(LBraceLoc, RBraceLoc); 2455 } 2456 static bool classof(const Stmt *T) { 2457 return T->getStmtClass() == InitListExprClass; 2458 } 2459 static bool classof(const InitListExpr *) { return true; } 2460 2461 // Iterators 2462 virtual child_iterator child_begin(); 2463 virtual child_iterator child_end(); 2464 2465 typedef std::vector<Stmt *>::iterator iterator; 2466 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 2467 2468 iterator begin() { return InitExprs.begin(); } 2469 iterator end() { return InitExprs.end(); } 2470 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2471 reverse_iterator rend() { return InitExprs.rend(); } 2472}; 2473 2474/// @brief Represents a C99 designated initializer expression. 2475/// 2476/// A designated initializer expression (C99 6.7.8) contains one or 2477/// more designators (which can be field designators, array 2478/// designators, or GNU array-range designators) followed by an 2479/// expression that initializes the field or element(s) that the 2480/// designators refer to. For example, given: 2481/// 2482/// @code 2483/// struct point { 2484/// double x; 2485/// double y; 2486/// }; 2487/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2488/// @endcode 2489/// 2490/// The InitListExpr contains three DesignatedInitExprs, the first of 2491/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2492/// designators, one array designator for @c [2] followed by one field 2493/// designator for @c .y. The initalization expression will be 1.0. 2494class DesignatedInitExpr : public Expr { 2495public: 2496 /// \brief Forward declaration of the Designator class. 2497 class Designator; 2498 2499private: 2500 /// The location of the '=' or ':' prior to the actual initializer 2501 /// expression. 2502 SourceLocation EqualOrColonLoc; 2503 2504 /// Whether this designated initializer used the GNU deprecated 2505 /// syntax rather than the C99 '=' syntax. 2506 bool GNUSyntax : 1; 2507 2508 /// The number of designators in this initializer expression. 2509 unsigned NumDesignators : 15; 2510 2511 /// \brief The designators in this designated initialization 2512 /// expression. 2513 Designator *Designators; 2514 2515 /// The number of subexpressions of this initializer expression, 2516 /// which contains both the initializer and any additional 2517 /// expressions used by array and array-range designators. 2518 unsigned NumSubExprs : 16; 2519 2520 2521 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 2522 const Designator *Designators, 2523 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2524 Expr **IndexExprs, unsigned NumIndexExprs, 2525 Expr *Init); 2526 2527 explicit DesignatedInitExpr(unsigned NumSubExprs) 2528 : Expr(DesignatedInitExprClass, EmptyShell()), 2529 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2530 2531protected: 2532 virtual void DoDestroy(ASTContext &C); 2533 2534public: 2535 /// A field designator, e.g., ".x". 2536 struct FieldDesignator { 2537 /// Refers to the field that is being initialized. The low bit 2538 /// of this field determines whether this is actually a pointer 2539 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2540 /// initially constructed, a field designator will store an 2541 /// IdentifierInfo*. After semantic analysis has resolved that 2542 /// name, the field designator will instead store a FieldDecl*. 2543 uintptr_t NameOrField; 2544 2545 /// The location of the '.' in the designated initializer. 2546 unsigned DotLoc; 2547 2548 /// The location of the field name in the designated initializer. 2549 unsigned FieldLoc; 2550 }; 2551 2552 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2553 struct ArrayOrRangeDesignator { 2554 /// Location of the first index expression within the designated 2555 /// initializer expression's list of subexpressions. 2556 unsigned Index; 2557 /// The location of the '[' starting the array range designator. 2558 unsigned LBracketLoc; 2559 /// The location of the ellipsis separating the start and end 2560 /// indices. Only valid for GNU array-range designators. 2561 unsigned EllipsisLoc; 2562 /// The location of the ']' terminating the array range designator. 2563 unsigned RBracketLoc; 2564 }; 2565 2566 /// @brief Represents a single C99 designator. 2567 /// 2568 /// @todo This class is infuriatingly similar to clang::Designator, 2569 /// but minor differences (storing indices vs. storing pointers) 2570 /// keep us from reusing it. Try harder, later, to rectify these 2571 /// differences. 2572 class Designator { 2573 /// @brief The kind of designator this describes. 2574 enum { 2575 FieldDesignator, 2576 ArrayDesignator, 2577 ArrayRangeDesignator 2578 } Kind; 2579 2580 union { 2581 /// A field designator, e.g., ".x". 2582 struct FieldDesignator Field; 2583 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2584 struct ArrayOrRangeDesignator ArrayOrRange; 2585 }; 2586 friend class DesignatedInitExpr; 2587 2588 public: 2589 Designator() {} 2590 2591 /// @brief Initializes a field designator. 2592 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2593 SourceLocation FieldLoc) 2594 : Kind(FieldDesignator) { 2595 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2596 Field.DotLoc = DotLoc.getRawEncoding(); 2597 Field.FieldLoc = FieldLoc.getRawEncoding(); 2598 } 2599 2600 /// @brief Initializes an array designator. 2601 Designator(unsigned Index, SourceLocation LBracketLoc, 2602 SourceLocation RBracketLoc) 2603 : Kind(ArrayDesignator) { 2604 ArrayOrRange.Index = Index; 2605 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2606 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2607 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2608 } 2609 2610 /// @brief Initializes a GNU array-range designator. 2611 Designator(unsigned Index, SourceLocation LBracketLoc, 2612 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2613 : Kind(ArrayRangeDesignator) { 2614 ArrayOrRange.Index = Index; 2615 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2616 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2617 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2618 } 2619 2620 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2621 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2622 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2623 2624 IdentifierInfo * getFieldName(); 2625 2626 FieldDecl *getField() { 2627 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2628 if (Field.NameOrField & 0x01) 2629 return 0; 2630 else 2631 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2632 } 2633 2634 void setField(FieldDecl *FD) { 2635 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2636 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2637 } 2638 2639 SourceLocation getDotLoc() const { 2640 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2641 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2642 } 2643 2644 SourceLocation getFieldLoc() const { 2645 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2646 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2647 } 2648 2649 SourceLocation getLBracketLoc() const { 2650 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2651 "Only valid on an array or array-range designator"); 2652 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2653 } 2654 2655 SourceLocation getRBracketLoc() const { 2656 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2657 "Only valid on an array or array-range designator"); 2658 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2659 } 2660 2661 SourceLocation getEllipsisLoc() const { 2662 assert(Kind == ArrayRangeDesignator && 2663 "Only valid on an array-range designator"); 2664 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2665 } 2666 2667 unsigned getFirstExprIndex() const { 2668 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2669 "Only valid on an array or array-range designator"); 2670 return ArrayOrRange.Index; 2671 } 2672 2673 SourceLocation getStartLocation() const { 2674 if (Kind == FieldDesignator) 2675 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2676 else 2677 return getLBracketLoc(); 2678 } 2679 }; 2680 2681 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2682 unsigned NumDesignators, 2683 Expr **IndexExprs, unsigned NumIndexExprs, 2684 SourceLocation EqualOrColonLoc, 2685 bool GNUSyntax, Expr *Init); 2686 2687 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 2688 2689 /// @brief Returns the number of designators in this initializer. 2690 unsigned size() const { return NumDesignators; } 2691 2692 // Iterator access to the designators. 2693 typedef Designator* designators_iterator; 2694 designators_iterator designators_begin() { return Designators; } 2695 designators_iterator designators_end() { 2696 return Designators + NumDesignators; 2697 } 2698 2699 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 2700 2701 void setDesignators(const Designator *Desigs, unsigned NumDesigs); 2702 2703 Expr *getArrayIndex(const Designator& D); 2704 Expr *getArrayRangeStart(const Designator& D); 2705 Expr *getArrayRangeEnd(const Designator& D); 2706 2707 /// @brief Retrieve the location of the '=' that precedes the 2708 /// initializer value itself, if present. 2709 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2710 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 2711 2712 /// @brief Determines whether this designated initializer used the 2713 /// deprecated GNU syntax for designated initializers. 2714 bool usesGNUSyntax() const { return GNUSyntax; } 2715 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 2716 2717 /// @brief Retrieve the initializer value. 2718 Expr *getInit() const { 2719 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2720 } 2721 2722 void setInit(Expr *init) { 2723 *child_begin() = init; 2724 } 2725 2726 /// \brief Retrieve the total number of subexpressions in this 2727 /// designated initializer expression, including the actual 2728 /// initialized value and any expressions that occur within array 2729 /// and array-range designators. 2730 unsigned getNumSubExprs() const { return NumSubExprs; } 2731 2732 Expr *getSubExpr(unsigned Idx) { 2733 assert(Idx < NumSubExprs && "Subscript out of range"); 2734 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2735 Ptr += sizeof(DesignatedInitExpr); 2736 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 2737 } 2738 2739 void setSubExpr(unsigned Idx, Expr *E) { 2740 assert(Idx < NumSubExprs && "Subscript out of range"); 2741 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2742 Ptr += sizeof(DesignatedInitExpr); 2743 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 2744 } 2745 2746 /// \brief Replaces the designator at index @p Idx with the series 2747 /// of designators in [First, Last). 2748 void ExpandDesignator(unsigned Idx, const Designator *First, 2749 const Designator *Last); 2750 2751 virtual SourceRange getSourceRange() const; 2752 2753 static bool classof(const Stmt *T) { 2754 return T->getStmtClass() == DesignatedInitExprClass; 2755 } 2756 static bool classof(const DesignatedInitExpr *) { return true; } 2757 2758 // Iterators 2759 virtual child_iterator child_begin(); 2760 virtual child_iterator child_end(); 2761}; 2762 2763/// \brief Represents an implicitly-generated value initialization of 2764/// an object of a given type. 2765/// 2766/// Implicit value initializations occur within semantic initializer 2767/// list expressions (InitListExpr) as placeholders for subobject 2768/// initializations not explicitly specified by the user. 2769/// 2770/// \see InitListExpr 2771class ImplicitValueInitExpr : public Expr { 2772public: 2773 explicit ImplicitValueInitExpr(QualType ty) 2774 : Expr(ImplicitValueInitExprClass, ty) { } 2775 2776 /// \brief Construct an empty implicit value initialization. 2777 explicit ImplicitValueInitExpr(EmptyShell Empty) 2778 : Expr(ImplicitValueInitExprClass, Empty) { } 2779 2780 static bool classof(const Stmt *T) { 2781 return T->getStmtClass() == ImplicitValueInitExprClass; 2782 } 2783 static bool classof(const ImplicitValueInitExpr *) { return true; } 2784 2785 virtual SourceRange getSourceRange() const { 2786 return SourceRange(); 2787 } 2788 2789 // Iterators 2790 virtual child_iterator child_begin(); 2791 virtual child_iterator child_end(); 2792}; 2793 2794 2795class ParenListExpr : public Expr { 2796 Stmt **Exprs; 2797 unsigned NumExprs; 2798 SourceLocation LParenLoc, RParenLoc; 2799 2800protected: 2801 virtual void DoDestroy(ASTContext& C); 2802 2803public: 2804 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 2805 unsigned numexprs, SourceLocation rparenloc); 2806 2807 ~ParenListExpr() {} 2808 2809 /// \brief Build an empty paren list. 2810 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 2811 2812 unsigned getNumExprs() const { return NumExprs; } 2813 2814 const Expr* getExpr(unsigned Init) const { 2815 assert(Init < getNumExprs() && "Initializer access out of range!"); 2816 return cast_or_null<Expr>(Exprs[Init]); 2817 } 2818 2819 Expr* getExpr(unsigned Init) { 2820 assert(Init < getNumExprs() && "Initializer access out of range!"); 2821 return cast_or_null<Expr>(Exprs[Init]); 2822 } 2823 2824 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 2825 2826 SourceLocation getLParenLoc() const { return LParenLoc; } 2827 SourceLocation getRParenLoc() const { return RParenLoc; } 2828 2829 virtual SourceRange getSourceRange() const { 2830 return SourceRange(LParenLoc, RParenLoc); 2831 } 2832 static bool classof(const Stmt *T) { 2833 return T->getStmtClass() == ParenListExprClass; 2834 } 2835 static bool classof(const ParenListExpr *) { return true; } 2836 2837 // Iterators 2838 virtual child_iterator child_begin(); 2839 virtual child_iterator child_end(); 2840}; 2841 2842 2843//===----------------------------------------------------------------------===// 2844// Clang Extensions 2845//===----------------------------------------------------------------------===// 2846 2847 2848/// ExtVectorElementExpr - This represents access to specific elements of a 2849/// vector, and may occur on the left hand side or right hand side. For example 2850/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2851/// 2852/// Note that the base may have either vector or pointer to vector type, just 2853/// like a struct field reference. 2854/// 2855class ExtVectorElementExpr : public Expr { 2856 Stmt *Base; 2857 IdentifierInfo *Accessor; 2858 SourceLocation AccessorLoc; 2859public: 2860 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2861 SourceLocation loc) 2862 : Expr(ExtVectorElementExprClass, ty), 2863 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 2864 2865 /// \brief Build an empty vector element expression. 2866 explicit ExtVectorElementExpr(EmptyShell Empty) 2867 : Expr(ExtVectorElementExprClass, Empty) { } 2868 2869 const Expr *getBase() const { return cast<Expr>(Base); } 2870 Expr *getBase() { return cast<Expr>(Base); } 2871 void setBase(Expr *E) { Base = E; } 2872 2873 IdentifierInfo &getAccessor() const { return *Accessor; } 2874 void setAccessor(IdentifierInfo *II) { Accessor = II; } 2875 2876 SourceLocation getAccessorLoc() const { return AccessorLoc; } 2877 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 2878 2879 /// getNumElements - Get the number of components being selected. 2880 unsigned getNumElements() const; 2881 2882 /// containsDuplicateElements - Return true if any element access is 2883 /// repeated. 2884 bool containsDuplicateElements() const; 2885 2886 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2887 /// aggregate Constant of ConstantInt(s). 2888 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2889 2890 virtual SourceRange getSourceRange() const { 2891 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2892 } 2893 2894 /// isArrow - Return true if the base expression is a pointer to vector, 2895 /// return false if the base expression is a vector. 2896 bool isArrow() const; 2897 2898 static bool classof(const Stmt *T) { 2899 return T->getStmtClass() == ExtVectorElementExprClass; 2900 } 2901 static bool classof(const ExtVectorElementExpr *) { return true; } 2902 2903 // Iterators 2904 virtual child_iterator child_begin(); 2905 virtual child_iterator child_end(); 2906}; 2907 2908 2909/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2910/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2911class BlockExpr : public Expr { 2912protected: 2913 BlockDecl *TheBlock; 2914 bool HasBlockDeclRefExprs; 2915public: 2916 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2917 : Expr(BlockExprClass, ty), 2918 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2919 2920 /// \brief Build an empty block expression. 2921 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 2922 2923 const BlockDecl *getBlockDecl() const { return TheBlock; } 2924 BlockDecl *getBlockDecl() { return TheBlock; } 2925 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 2926 2927 // Convenience functions for probing the underlying BlockDecl. 2928 SourceLocation getCaretLocation() const; 2929 const Stmt *getBody() const; 2930 Stmt *getBody(); 2931 2932 virtual SourceRange getSourceRange() const { 2933 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2934 } 2935 2936 /// getFunctionType - Return the underlying function type for this block. 2937 const FunctionType *getFunctionType() const; 2938 2939 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2940 /// inside of the block that reference values outside the block. 2941 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2942 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 2943 2944 static bool classof(const Stmt *T) { 2945 return T->getStmtClass() == BlockExprClass; 2946 } 2947 static bool classof(const BlockExpr *) { return true; } 2948 2949 // Iterators 2950 virtual child_iterator child_begin(); 2951 virtual child_iterator child_end(); 2952}; 2953 2954/// BlockDeclRefExpr - A reference to a declared variable, function, 2955/// enum, etc. 2956class BlockDeclRefExpr : public Expr { 2957 ValueDecl *D; 2958 SourceLocation Loc; 2959 bool IsByRef : 1; 2960 bool ConstQualAdded : 1; 2961public: 2962 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 2963 bool constAdded = false) : 2964 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef), 2965 ConstQualAdded(constAdded) {} 2966 2967 // \brief Build an empty reference to a declared variable in a 2968 // block. 2969 explicit BlockDeclRefExpr(EmptyShell Empty) 2970 : Expr(BlockDeclRefExprClass, Empty) { } 2971 2972 ValueDecl *getDecl() { return D; } 2973 const ValueDecl *getDecl() const { return D; } 2974 void setDecl(ValueDecl *VD) { D = VD; } 2975 2976 SourceLocation getLocation() const { return Loc; } 2977 void setLocation(SourceLocation L) { Loc = L; } 2978 2979 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2980 2981 bool isByRef() const { return IsByRef; } 2982 void setByRef(bool BR) { IsByRef = BR; } 2983 2984 bool isConstQualAdded() const { return ConstQualAdded; } 2985 void setConstQualAdded(bool C) { ConstQualAdded = C; } 2986 2987 static bool classof(const Stmt *T) { 2988 return T->getStmtClass() == BlockDeclRefExprClass; 2989 } 2990 static bool classof(const BlockDeclRefExpr *) { return true; } 2991 2992 // Iterators 2993 virtual child_iterator child_begin(); 2994 virtual child_iterator child_end(); 2995}; 2996 2997} // end namespace clang 2998 2999#endif 3000