Expr.h revision 92ef5d75720c032808181133e4d7c5f82230237e
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 /// CK_AnyPointerToObjCPointerCast - Casting any pointer to objective-c 1577 /// pointer 1578 CK_AnyPointerToObjCPointerCast 1579 1580 }; 1581 1582private: 1583 CastKind Kind; 1584 Stmt *Op; 1585protected: 1586 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) : 1587 Expr(SC, ty, 1588 // Cast expressions are type-dependent if the type is 1589 // dependent (C++ [temp.dep.expr]p3). 1590 ty->isDependentType(), 1591 // Cast expressions are value-dependent if the type is 1592 // dependent or if the subexpression is value-dependent. 1593 ty->isDependentType() || (op && op->isValueDependent())), 1594 Kind(kind), Op(op) {} 1595 1596 /// \brief Construct an empty cast. 1597 CastExpr(StmtClass SC, EmptyShell Empty) 1598 : Expr(SC, Empty) { } 1599 1600public: 1601 CastKind getCastKind() const { return Kind; } 1602 void setCastKind(CastKind K) { Kind = K; } 1603 const char *getCastKindName() const; 1604 1605 Expr *getSubExpr() { return cast<Expr>(Op); } 1606 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1607 void setSubExpr(Expr *E) { Op = E; } 1608 1609 static bool classof(const Stmt *T) { 1610 StmtClass SC = T->getStmtClass(); 1611 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1612 return true; 1613 1614 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1615 return true; 1616 1617 return false; 1618 } 1619 static bool classof(const CastExpr *) { return true; } 1620 1621 // Iterators 1622 virtual child_iterator child_begin(); 1623 virtual child_iterator child_end(); 1624}; 1625 1626/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1627/// conversions, which have no direct representation in the original 1628/// source code. For example: converting T[]->T*, void f()->void 1629/// (*f)(), float->double, short->int, etc. 1630/// 1631/// In C, implicit casts always produce rvalues. However, in C++, an 1632/// implicit cast whose result is being bound to a reference will be 1633/// an lvalue. For example: 1634/// 1635/// @code 1636/// class Base { }; 1637/// class Derived : public Base { }; 1638/// void f(Derived d) { 1639/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1640/// } 1641/// @endcode 1642class ImplicitCastExpr : public CastExpr { 1643 /// LvalueCast - Whether this cast produces an lvalue. 1644 bool LvalueCast; 1645 1646public: 1647 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) : 1648 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { } 1649 1650 /// \brief Construct an empty implicit cast. 1651 explicit ImplicitCastExpr(EmptyShell Shell) 1652 : CastExpr(ImplicitCastExprClass, Shell) { } 1653 1654 1655 virtual SourceRange getSourceRange() const { 1656 return getSubExpr()->getSourceRange(); 1657 } 1658 1659 /// isLvalueCast - Whether this cast produces an lvalue. 1660 bool isLvalueCast() const { return LvalueCast; } 1661 1662 /// setLvalueCast - Set whether this cast produces an lvalue. 1663 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1664 1665 static bool classof(const Stmt *T) { 1666 return T->getStmtClass() == ImplicitCastExprClass; 1667 } 1668 static bool classof(const ImplicitCastExpr *) { return true; } 1669}; 1670 1671/// ExplicitCastExpr - An explicit cast written in the source 1672/// code. 1673/// 1674/// This class is effectively an abstract class, because it provides 1675/// the basic representation of an explicitly-written cast without 1676/// specifying which kind of cast (C cast, functional cast, static 1677/// cast, etc.) was written; specific derived classes represent the 1678/// particular style of cast and its location information. 1679/// 1680/// Unlike implicit casts, explicit cast nodes have two different 1681/// types: the type that was written into the source code, and the 1682/// actual type of the expression as determined by semantic 1683/// analysis. These types may differ slightly. For example, in C++ one 1684/// can cast to a reference type, which indicates that the resulting 1685/// expression will be an lvalue. The reference type, however, will 1686/// not be used as the type of the expression. 1687class ExplicitCastExpr : public CastExpr { 1688 /// TypeAsWritten - The type that this expression is casting to, as 1689 /// written in the source code. 1690 QualType TypeAsWritten; 1691 1692protected: 1693 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 1694 Expr *op, QualType writtenTy) 1695 : CastExpr(SC, exprTy, kind, op), TypeAsWritten(writtenTy) {} 1696 1697 /// \brief Construct an empty explicit cast. 1698 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 1699 : CastExpr(SC, Shell) { } 1700 1701public: 1702 /// getTypeAsWritten - Returns the type that this expression is 1703 /// casting to, as written in the source code. 1704 QualType getTypeAsWritten() const { return TypeAsWritten; } 1705 void setTypeAsWritten(QualType T) { TypeAsWritten = T; } 1706 1707 static bool classof(const Stmt *T) { 1708 StmtClass SC = T->getStmtClass(); 1709 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1710 return true; 1711 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1712 return true; 1713 1714 return false; 1715 } 1716 static bool classof(const ExplicitCastExpr *) { return true; } 1717}; 1718 1719/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1720/// cast in C++ (C++ [expr.cast]), which uses the syntax 1721/// (Type)expr. For example: @c (int)f. 1722class CStyleCastExpr : public ExplicitCastExpr { 1723 SourceLocation LPLoc; // the location of the left paren 1724 SourceLocation RPLoc; // the location of the right paren 1725public: 1726 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy, 1727 SourceLocation l, SourceLocation r) : 1728 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy), 1729 LPLoc(l), RPLoc(r) {} 1730 1731 /// \brief Construct an empty C-style explicit cast. 1732 explicit CStyleCastExpr(EmptyShell Shell) 1733 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 1734 1735 SourceLocation getLParenLoc() const { return LPLoc; } 1736 void setLParenLoc(SourceLocation L) { LPLoc = L; } 1737 1738 SourceLocation getRParenLoc() const { return RPLoc; } 1739 void setRParenLoc(SourceLocation L) { RPLoc = L; } 1740 1741 virtual SourceRange getSourceRange() const { 1742 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1743 } 1744 static bool classof(const Stmt *T) { 1745 return T->getStmtClass() == CStyleCastExprClass; 1746 } 1747 static bool classof(const CStyleCastExpr *) { return true; } 1748}; 1749 1750/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 1751/// 1752/// This expression node kind describes a builtin binary operation, 1753/// such as "x + y" for integer values "x" and "y". The operands will 1754/// already have been converted to appropriate types (e.g., by 1755/// performing promotions or conversions). 1756/// 1757/// In C++, where operators may be overloaded, a different kind of 1758/// expression node (CXXOperatorCallExpr) is used to express the 1759/// invocation of an overloaded operator with operator syntax. Within 1760/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 1761/// used to store an expression "x + y" depends on the subexpressions 1762/// for x and y. If neither x or y is type-dependent, and the "+" 1763/// operator resolves to a built-in operation, BinaryOperator will be 1764/// used to express the computation (x and y may still be 1765/// value-dependent). If either x or y is type-dependent, or if the 1766/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 1767/// be used to express the computation. 1768class BinaryOperator : public Expr { 1769public: 1770 enum Opcode { 1771 // Operators listed in order of precedence. 1772 // Note that additions to this should also update the StmtVisitor class. 1773 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 1774 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1775 Add, Sub, // [C99 6.5.6] Additive operators. 1776 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1777 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1778 EQ, NE, // [C99 6.5.9] Equality operators. 1779 And, // [C99 6.5.10] Bitwise AND operator. 1780 Xor, // [C99 6.5.11] Bitwise XOR operator. 1781 Or, // [C99 6.5.12] Bitwise OR operator. 1782 LAnd, // [C99 6.5.13] Logical AND operator. 1783 LOr, // [C99 6.5.14] Logical OR operator. 1784 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1785 DivAssign, RemAssign, 1786 AddAssign, SubAssign, 1787 ShlAssign, ShrAssign, 1788 AndAssign, XorAssign, 1789 OrAssign, 1790 Comma // [C99 6.5.17] Comma operator. 1791 }; 1792private: 1793 enum { LHS, RHS, END_EXPR }; 1794 Stmt* SubExprs[END_EXPR]; 1795 Opcode Opc; 1796 SourceLocation OpLoc; 1797public: 1798 1799 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1800 SourceLocation opLoc) 1801 : Expr(BinaryOperatorClass, ResTy, 1802 lhs->isTypeDependent() || rhs->isTypeDependent(), 1803 lhs->isValueDependent() || rhs->isValueDependent()), 1804 Opc(opc), OpLoc(opLoc) { 1805 SubExprs[LHS] = lhs; 1806 SubExprs[RHS] = rhs; 1807 assert(!isCompoundAssignmentOp() && 1808 "Use ArithAssignBinaryOperator for compound assignments"); 1809 } 1810 1811 /// \brief Construct an empty binary operator. 1812 explicit BinaryOperator(EmptyShell Empty) 1813 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 1814 1815 SourceLocation getOperatorLoc() const { return OpLoc; } 1816 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1817 1818 Opcode getOpcode() const { return Opc; } 1819 void setOpcode(Opcode O) { Opc = O; } 1820 1821 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1822 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1823 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1824 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1825 1826 virtual SourceRange getSourceRange() const { 1827 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1828 } 1829 1830 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1831 /// corresponds to, e.g. "<<=". 1832 static const char *getOpcodeStr(Opcode Op); 1833 1834 /// \brief Retrieve the binary opcode that corresponds to the given 1835 /// overloaded operator. 1836 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 1837 1838 /// \brief Retrieve the overloaded operator kind that corresponds to 1839 /// the given binary opcode. 1840 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1841 1842 /// predicates to categorize the respective opcodes. 1843 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1844 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1845 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; } 1846 bool isShiftOp() const { return isShiftOp(Opc); } 1847 1848 static bool isBitwiseOp(Opcode Opc) { return Opc >= And && Opc <= Or; } 1849 bool isBitwiseOp() const { return isBitwiseOp(Opc); } 1850 1851 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1852 bool isRelationalOp() const { return isRelationalOp(Opc); } 1853 1854 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1855 bool isEqualityOp() const { return isEqualityOp(Opc); } 1856 1857 static bool isComparisonOp(Opcode Opc) { return Opc >= LT && Opc <= NE; } 1858 bool isComparisonOp() const { return isComparisonOp(Opc); } 1859 1860 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1861 bool isLogicalOp() const { return isLogicalOp(Opc); } 1862 1863 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1864 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1865 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1866 1867 static bool classof(const Stmt *S) { 1868 return S->getStmtClass() == BinaryOperatorClass || 1869 S->getStmtClass() == CompoundAssignOperatorClass; 1870 } 1871 static bool classof(const BinaryOperator *) { return true; } 1872 1873 // Iterators 1874 virtual child_iterator child_begin(); 1875 virtual child_iterator child_end(); 1876 1877protected: 1878 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1879 SourceLocation oploc, bool dead) 1880 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1881 SubExprs[LHS] = lhs; 1882 SubExprs[RHS] = rhs; 1883 } 1884 1885 BinaryOperator(StmtClass SC, EmptyShell Empty) 1886 : Expr(SC, Empty), Opc(MulAssign) { } 1887}; 1888 1889/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1890/// track of the type the operation is performed in. Due to the semantics of 1891/// these operators, the operands are promoted, the aritmetic performed, an 1892/// implicit conversion back to the result type done, then the assignment takes 1893/// place. This captures the intermediate type which the computation is done 1894/// in. 1895class CompoundAssignOperator : public BinaryOperator { 1896 QualType ComputationLHSType; 1897 QualType ComputationResultType; 1898public: 1899 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1900 QualType ResType, QualType CompLHSType, 1901 QualType CompResultType, 1902 SourceLocation OpLoc) 1903 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1904 ComputationLHSType(CompLHSType), 1905 ComputationResultType(CompResultType) { 1906 assert(isCompoundAssignmentOp() && 1907 "Only should be used for compound assignments"); 1908 } 1909 1910 /// \brief Build an empty compound assignment operator expression. 1911 explicit CompoundAssignOperator(EmptyShell Empty) 1912 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 1913 1914 // The two computation types are the type the LHS is converted 1915 // to for the computation and the type of the result; the two are 1916 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 1917 QualType getComputationLHSType() const { return ComputationLHSType; } 1918 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 1919 1920 QualType getComputationResultType() const { return ComputationResultType; } 1921 void setComputationResultType(QualType T) { ComputationResultType = T; } 1922 1923 static bool classof(const CompoundAssignOperator *) { return true; } 1924 static bool classof(const Stmt *S) { 1925 return S->getStmtClass() == CompoundAssignOperatorClass; 1926 } 1927}; 1928 1929/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1930/// GNU "missing LHS" extension is in use. 1931/// 1932class ConditionalOperator : public Expr { 1933 enum { COND, LHS, RHS, END_EXPR }; 1934 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1935 SourceLocation QuestionLoc, ColonLoc; 1936public: 1937 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 1938 SourceLocation CLoc, Expr *rhs, QualType t) 1939 : Expr(ConditionalOperatorClass, t, 1940 // FIXME: the type of the conditional operator doesn't 1941 // depend on the type of the conditional, but the standard 1942 // seems to imply that it could. File a bug! 1943 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1944 (cond->isValueDependent() || 1945 (lhs && lhs->isValueDependent()) || 1946 (rhs && rhs->isValueDependent()))), 1947 QuestionLoc(QLoc), 1948 ColonLoc(CLoc) { 1949 SubExprs[COND] = cond; 1950 SubExprs[LHS] = lhs; 1951 SubExprs[RHS] = rhs; 1952 } 1953 1954 /// \brief Build an empty conditional operator. 1955 explicit ConditionalOperator(EmptyShell Empty) 1956 : Expr(ConditionalOperatorClass, Empty) { } 1957 1958 // getCond - Return the expression representing the condition for 1959 // the ?: operator. 1960 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1961 void setCond(Expr *E) { SubExprs[COND] = E; } 1962 1963 // getTrueExpr - Return the subexpression representing the value of the ?: 1964 // expression if the condition evaluates to true. In most cases this value 1965 // will be the same as getLHS() except a GCC extension allows the left 1966 // subexpression to be omitted, and instead of the condition be returned. 1967 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1968 // is only evaluated once. 1969 Expr *getTrueExpr() const { 1970 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1971 } 1972 1973 // getTrueExpr - Return the subexpression representing the value of the ?: 1974 // expression if the condition evaluates to false. This is the same as getRHS. 1975 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1976 1977 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1978 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1979 1980 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1981 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1982 1983 SourceLocation getQuestionLoc() const { return QuestionLoc; } 1984 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 1985 1986 SourceLocation getColonLoc() const { return ColonLoc; } 1987 void setColonLoc(SourceLocation L) { ColonLoc = L; } 1988 1989 virtual SourceRange getSourceRange() const { 1990 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1991 } 1992 static bool classof(const Stmt *T) { 1993 return T->getStmtClass() == ConditionalOperatorClass; 1994 } 1995 static bool classof(const ConditionalOperator *) { return true; } 1996 1997 // Iterators 1998 virtual child_iterator child_begin(); 1999 virtual child_iterator child_end(); 2000}; 2001 2002/// AddrLabelExpr - The GNU address of label extension, representing &&label. 2003class AddrLabelExpr : public Expr { 2004 SourceLocation AmpAmpLoc, LabelLoc; 2005 LabelStmt *Label; 2006public: 2007 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 2008 QualType t) 2009 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 2010 2011 /// \brief Build an empty address of a label expression. 2012 explicit AddrLabelExpr(EmptyShell Empty) 2013 : Expr(AddrLabelExprClass, Empty) { } 2014 2015 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 2016 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 2017 SourceLocation getLabelLoc() const { return LabelLoc; } 2018 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2019 2020 virtual SourceRange getSourceRange() const { 2021 return SourceRange(AmpAmpLoc, LabelLoc); 2022 } 2023 2024 LabelStmt *getLabel() const { return Label; } 2025 void setLabel(LabelStmt *S) { Label = S; } 2026 2027 static bool classof(const Stmt *T) { 2028 return T->getStmtClass() == AddrLabelExprClass; 2029 } 2030 static bool classof(const AddrLabelExpr *) { return true; } 2031 2032 // Iterators 2033 virtual child_iterator child_begin(); 2034 virtual child_iterator child_end(); 2035}; 2036 2037/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 2038/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 2039/// takes the value of the last subexpression. 2040class StmtExpr : public Expr { 2041 Stmt *SubStmt; 2042 SourceLocation LParenLoc, RParenLoc; 2043public: 2044 StmtExpr(CompoundStmt *substmt, QualType T, 2045 SourceLocation lp, SourceLocation rp) : 2046 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 2047 2048 /// \brief Build an empty statement expression. 2049 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 2050 2051 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 2052 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 2053 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 2054 2055 virtual SourceRange getSourceRange() const { 2056 return SourceRange(LParenLoc, RParenLoc); 2057 } 2058 2059 SourceLocation getLParenLoc() const { return LParenLoc; } 2060 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2061 SourceLocation getRParenLoc() const { return RParenLoc; } 2062 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2063 2064 static bool classof(const Stmt *T) { 2065 return T->getStmtClass() == StmtExprClass; 2066 } 2067 static bool classof(const StmtExpr *) { return true; } 2068 2069 // Iterators 2070 virtual child_iterator child_begin(); 2071 virtual child_iterator child_end(); 2072}; 2073 2074/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 2075/// This AST node represents a function that returns 1 if two *types* (not 2076/// expressions) are compatible. The result of this built-in function can be 2077/// used in integer constant expressions. 2078class TypesCompatibleExpr : public Expr { 2079 QualType Type1; 2080 QualType Type2; 2081 SourceLocation BuiltinLoc, RParenLoc; 2082public: 2083 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 2084 QualType t1, QualType t2, SourceLocation RP) : 2085 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 2086 BuiltinLoc(BLoc), RParenLoc(RP) {} 2087 2088 /// \brief Build an empty __builtin_type_compatible_p expression. 2089 explicit TypesCompatibleExpr(EmptyShell Empty) 2090 : Expr(TypesCompatibleExprClass, Empty) { } 2091 2092 QualType getArgType1() const { return Type1; } 2093 void setArgType1(QualType T) { Type1 = T; } 2094 QualType getArgType2() const { return Type2; } 2095 void setArgType2(QualType T) { Type2 = T; } 2096 2097 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2098 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2099 2100 SourceLocation getRParenLoc() const { return RParenLoc; } 2101 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2102 2103 virtual SourceRange getSourceRange() const { 2104 return SourceRange(BuiltinLoc, RParenLoc); 2105 } 2106 static bool classof(const Stmt *T) { 2107 return T->getStmtClass() == TypesCompatibleExprClass; 2108 } 2109 static bool classof(const TypesCompatibleExpr *) { return true; } 2110 2111 // Iterators 2112 virtual child_iterator child_begin(); 2113 virtual child_iterator child_end(); 2114}; 2115 2116/// ShuffleVectorExpr - clang-specific builtin-in function 2117/// __builtin_shufflevector. 2118/// This AST node represents a operator that does a constant 2119/// shuffle, similar to LLVM's shufflevector instruction. It takes 2120/// two vectors and a variable number of constant indices, 2121/// and returns the appropriately shuffled vector. 2122class ShuffleVectorExpr : public Expr { 2123 SourceLocation BuiltinLoc, RParenLoc; 2124 2125 // SubExprs - the list of values passed to the __builtin_shufflevector 2126 // function. The first two are vectors, and the rest are constant 2127 // indices. The number of values in this list is always 2128 // 2+the number of indices in the vector type. 2129 Stmt **SubExprs; 2130 unsigned NumExprs; 2131 2132protected: 2133 virtual void DoDestroy(ASTContext &C); 2134 2135public: 2136 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 2137 QualType Type, SourceLocation BLoc, 2138 SourceLocation RP) : 2139 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 2140 RParenLoc(RP), NumExprs(nexpr) { 2141 2142 SubExprs = new (C) Stmt*[nexpr]; 2143 for (unsigned i = 0; i < nexpr; i++) 2144 SubExprs[i] = args[i]; 2145 } 2146 2147 /// \brief Build an empty vector-shuffle expression. 2148 explicit ShuffleVectorExpr(EmptyShell Empty) 2149 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 2150 2151 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2152 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2153 2154 SourceLocation getRParenLoc() const { return RParenLoc; } 2155 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2156 2157 virtual SourceRange getSourceRange() const { 2158 return SourceRange(BuiltinLoc, RParenLoc); 2159 } 2160 static bool classof(const Stmt *T) { 2161 return T->getStmtClass() == ShuffleVectorExprClass; 2162 } 2163 static bool classof(const ShuffleVectorExpr *) { return true; } 2164 2165 ~ShuffleVectorExpr() {} 2166 2167 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 2168 /// constant expression, the actual arguments passed in, and the function 2169 /// pointers. 2170 unsigned getNumSubExprs() const { return NumExprs; } 2171 2172 /// getExpr - Return the Expr at the specified index. 2173 Expr *getExpr(unsigned Index) { 2174 assert((Index < NumExprs) && "Arg access out of range!"); 2175 return cast<Expr>(SubExprs[Index]); 2176 } 2177 const Expr *getExpr(unsigned Index) const { 2178 assert((Index < NumExprs) && "Arg access out of range!"); 2179 return cast<Expr>(SubExprs[Index]); 2180 } 2181 2182 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 2183 2184 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 2185 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 2186 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 2187 } 2188 2189 // Iterators 2190 virtual child_iterator child_begin(); 2191 virtual child_iterator child_end(); 2192}; 2193 2194/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 2195/// This AST node is similar to the conditional operator (?:) in C, with 2196/// the following exceptions: 2197/// - the test expression must be a integer constant expression. 2198/// - the expression returned acts like the chosen subexpression in every 2199/// visible way: the type is the same as that of the chosen subexpression, 2200/// and all predicates (whether it's an l-value, whether it's an integer 2201/// constant expression, etc.) return the same result as for the chosen 2202/// sub-expression. 2203class ChooseExpr : public Expr { 2204 enum { COND, LHS, RHS, END_EXPR }; 2205 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2206 SourceLocation BuiltinLoc, RParenLoc; 2207public: 2208 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2209 SourceLocation RP, bool TypeDependent, bool ValueDependent) 2210 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent), 2211 BuiltinLoc(BLoc), RParenLoc(RP) { 2212 SubExprs[COND] = cond; 2213 SubExprs[LHS] = lhs; 2214 SubExprs[RHS] = rhs; 2215 } 2216 2217 /// \brief Build an empty __builtin_choose_expr. 2218 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2219 2220 /// isConditionTrue - Return whether the condition is true (i.e. not 2221 /// equal to zero). 2222 bool isConditionTrue(ASTContext &C) const; 2223 2224 /// getChosenSubExpr - Return the subexpression chosen according to the 2225 /// condition. 2226 Expr *getChosenSubExpr(ASTContext &C) const { 2227 return isConditionTrue(C) ? getLHS() : getRHS(); 2228 } 2229 2230 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2231 void setCond(Expr *E) { SubExprs[COND] = E; } 2232 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2233 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2234 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2235 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2236 2237 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2238 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2239 2240 SourceLocation getRParenLoc() const { return RParenLoc; } 2241 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2242 2243 virtual SourceRange getSourceRange() const { 2244 return SourceRange(BuiltinLoc, RParenLoc); 2245 } 2246 static bool classof(const Stmt *T) { 2247 return T->getStmtClass() == ChooseExprClass; 2248 } 2249 static bool classof(const ChooseExpr *) { return true; } 2250 2251 // Iterators 2252 virtual child_iterator child_begin(); 2253 virtual child_iterator child_end(); 2254}; 2255 2256/// GNUNullExpr - Implements the GNU __null extension, which is a name 2257/// for a null pointer constant that has integral type (e.g., int or 2258/// long) and is the same size and alignment as a pointer. The __null 2259/// extension is typically only used by system headers, which define 2260/// NULL as __null in C++ rather than using 0 (which is an integer 2261/// that may not match the size of a pointer). 2262class GNUNullExpr : public Expr { 2263 /// TokenLoc - The location of the __null keyword. 2264 SourceLocation TokenLoc; 2265 2266public: 2267 GNUNullExpr(QualType Ty, SourceLocation Loc) 2268 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 2269 2270 /// \brief Build an empty GNU __null expression. 2271 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2272 2273 /// getTokenLocation - The location of the __null token. 2274 SourceLocation getTokenLocation() const { return TokenLoc; } 2275 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2276 2277 virtual SourceRange getSourceRange() const { 2278 return SourceRange(TokenLoc); 2279 } 2280 static bool classof(const Stmt *T) { 2281 return T->getStmtClass() == GNUNullExprClass; 2282 } 2283 static bool classof(const GNUNullExpr *) { return true; } 2284 2285 // Iterators 2286 virtual child_iterator child_begin(); 2287 virtual child_iterator child_end(); 2288}; 2289 2290/// VAArgExpr, used for the builtin function __builtin_va_start. 2291class VAArgExpr : public Expr { 2292 Stmt *Val; 2293 SourceLocation BuiltinLoc, RParenLoc; 2294public: 2295 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 2296 : Expr(VAArgExprClass, t), 2297 Val(e), 2298 BuiltinLoc(BLoc), 2299 RParenLoc(RPLoc) { } 2300 2301 /// \brief Create an empty __builtin_va_start expression. 2302 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2303 2304 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2305 Expr *getSubExpr() { return cast<Expr>(Val); } 2306 void setSubExpr(Expr *E) { Val = E; } 2307 2308 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2309 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2310 2311 SourceLocation getRParenLoc() const { return RParenLoc; } 2312 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2313 2314 virtual SourceRange getSourceRange() const { 2315 return SourceRange(BuiltinLoc, RParenLoc); 2316 } 2317 static bool classof(const Stmt *T) { 2318 return T->getStmtClass() == VAArgExprClass; 2319 } 2320 static bool classof(const VAArgExpr *) { return true; } 2321 2322 // Iterators 2323 virtual child_iterator child_begin(); 2324 virtual child_iterator child_end(); 2325}; 2326 2327/// @brief Describes an C or C++ initializer list. 2328/// 2329/// InitListExpr describes an initializer list, which can be used to 2330/// initialize objects of different types, including 2331/// struct/class/union types, arrays, and vectors. For example: 2332/// 2333/// @code 2334/// struct foo x = { 1, { 2, 3 } }; 2335/// @endcode 2336/// 2337/// Prior to semantic analysis, an initializer list will represent the 2338/// initializer list as written by the user, but will have the 2339/// placeholder type "void". This initializer list is called the 2340/// syntactic form of the initializer, and may contain C99 designated 2341/// initializers (represented as DesignatedInitExprs), initializations 2342/// of subobject members without explicit braces, and so on. Clients 2343/// interested in the original syntax of the initializer list should 2344/// use the syntactic form of the initializer list. 2345/// 2346/// After semantic analysis, the initializer list will represent the 2347/// semantic form of the initializer, where the initializations of all 2348/// subobjects are made explicit with nested InitListExpr nodes and 2349/// C99 designators have been eliminated by placing the designated 2350/// initializations into the subobject they initialize. Additionally, 2351/// any "holes" in the initialization, where no initializer has been 2352/// specified for a particular subobject, will be replaced with 2353/// implicitly-generated ImplicitValueInitExpr expressions that 2354/// value-initialize the subobjects. Note, however, that the 2355/// initializer lists may still have fewer initializers than there are 2356/// elements to initialize within the object. 2357/// 2358/// Given the semantic form of the initializer list, one can retrieve 2359/// the original syntactic form of that initializer list (if it 2360/// exists) using getSyntacticForm(). Since many initializer lists 2361/// have the same syntactic and semantic forms, getSyntacticForm() may 2362/// return NULL, indicating that the current initializer list also 2363/// serves as its syntactic form. 2364class InitListExpr : public Expr { 2365 // FIXME: Eliminate this vector in favor of ASTContext allocation 2366 std::vector<Stmt *> InitExprs; 2367 SourceLocation LBraceLoc, RBraceLoc; 2368 2369 /// Contains the initializer list that describes the syntactic form 2370 /// written in the source code. 2371 InitListExpr *SyntacticForm; 2372 2373 /// If this initializer list initializes a union, specifies which 2374 /// field within the union will be initialized. 2375 FieldDecl *UnionFieldInit; 2376 2377 /// Whether this initializer list originally had a GNU array-range 2378 /// designator in it. This is a temporary marker used by CodeGen. 2379 bool HadArrayRangeDesignator; 2380 2381public: 2382 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 2383 SourceLocation rbraceloc); 2384 2385 /// \brief Build an empty initializer list. 2386 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { } 2387 2388 unsigned getNumInits() const { return InitExprs.size(); } 2389 2390 const Expr* getInit(unsigned Init) const { 2391 assert(Init < getNumInits() && "Initializer access out of range!"); 2392 return cast_or_null<Expr>(InitExprs[Init]); 2393 } 2394 2395 Expr* getInit(unsigned Init) { 2396 assert(Init < getNumInits() && "Initializer access out of range!"); 2397 return cast_or_null<Expr>(InitExprs[Init]); 2398 } 2399 2400 void setInit(unsigned Init, Expr *expr) { 2401 assert(Init < getNumInits() && "Initializer access out of range!"); 2402 InitExprs[Init] = expr; 2403 } 2404 2405 /// \brief Reserve space for some number of initializers. 2406 void reserveInits(unsigned NumInits); 2407 2408 /// @brief Specify the number of initializers 2409 /// 2410 /// If there are more than @p NumInits initializers, the remaining 2411 /// initializers will be destroyed. If there are fewer than @p 2412 /// NumInits initializers, NULL expressions will be added for the 2413 /// unknown initializers. 2414 void resizeInits(ASTContext &Context, unsigned NumInits); 2415 2416 /// @brief Updates the initializer at index @p Init with the new 2417 /// expression @p expr, and returns the old expression at that 2418 /// location. 2419 /// 2420 /// When @p Init is out of range for this initializer list, the 2421 /// initializer list will be extended with NULL expressions to 2422 /// accomodate the new entry. 2423 Expr *updateInit(unsigned Init, Expr *expr); 2424 2425 /// \brief If this initializes a union, specifies which field in the 2426 /// union to initialize. 2427 /// 2428 /// Typically, this field is the first named field within the 2429 /// union. However, a designated initializer can specify the 2430 /// initialization of a different field within the union. 2431 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2432 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2433 2434 // Explicit InitListExpr's originate from source code (and have valid source 2435 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2436 bool isExplicit() { 2437 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2438 } 2439 2440 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2441 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2442 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2443 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2444 2445 /// @brief Retrieve the initializer list that describes the 2446 /// syntactic form of the initializer. 2447 /// 2448 /// 2449 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2450 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2451 2452 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2453 void sawArrayRangeDesignator(bool ARD = true) { 2454 HadArrayRangeDesignator = ARD; 2455 } 2456 2457 virtual SourceRange getSourceRange() const { 2458 return SourceRange(LBraceLoc, RBraceLoc); 2459 } 2460 static bool classof(const Stmt *T) { 2461 return T->getStmtClass() == InitListExprClass; 2462 } 2463 static bool classof(const InitListExpr *) { return true; } 2464 2465 // Iterators 2466 virtual child_iterator child_begin(); 2467 virtual child_iterator child_end(); 2468 2469 typedef std::vector<Stmt *>::iterator iterator; 2470 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 2471 2472 iterator begin() { return InitExprs.begin(); } 2473 iterator end() { return InitExprs.end(); } 2474 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2475 reverse_iterator rend() { return InitExprs.rend(); } 2476}; 2477 2478/// @brief Represents a C99 designated initializer expression. 2479/// 2480/// A designated initializer expression (C99 6.7.8) contains one or 2481/// more designators (which can be field designators, array 2482/// designators, or GNU array-range designators) followed by an 2483/// expression that initializes the field or element(s) that the 2484/// designators refer to. For example, given: 2485/// 2486/// @code 2487/// struct point { 2488/// double x; 2489/// double y; 2490/// }; 2491/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2492/// @endcode 2493/// 2494/// The InitListExpr contains three DesignatedInitExprs, the first of 2495/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2496/// designators, one array designator for @c [2] followed by one field 2497/// designator for @c .y. The initalization expression will be 1.0. 2498class DesignatedInitExpr : public Expr { 2499public: 2500 /// \brief Forward declaration of the Designator class. 2501 class Designator; 2502 2503private: 2504 /// The location of the '=' or ':' prior to the actual initializer 2505 /// expression. 2506 SourceLocation EqualOrColonLoc; 2507 2508 /// Whether this designated initializer used the GNU deprecated 2509 /// syntax rather than the C99 '=' syntax. 2510 bool GNUSyntax : 1; 2511 2512 /// The number of designators in this initializer expression. 2513 unsigned NumDesignators : 15; 2514 2515 /// \brief The designators in this designated initialization 2516 /// expression. 2517 Designator *Designators; 2518 2519 /// The number of subexpressions of this initializer expression, 2520 /// which contains both the initializer and any additional 2521 /// expressions used by array and array-range designators. 2522 unsigned NumSubExprs : 16; 2523 2524 2525 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 2526 const Designator *Designators, 2527 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2528 Expr **IndexExprs, unsigned NumIndexExprs, 2529 Expr *Init); 2530 2531 explicit DesignatedInitExpr(unsigned NumSubExprs) 2532 : Expr(DesignatedInitExprClass, EmptyShell()), 2533 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2534 2535protected: 2536 virtual void DoDestroy(ASTContext &C); 2537 2538public: 2539 /// A field designator, e.g., ".x". 2540 struct FieldDesignator { 2541 /// Refers to the field that is being initialized. The low bit 2542 /// of this field determines whether this is actually a pointer 2543 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2544 /// initially constructed, a field designator will store an 2545 /// IdentifierInfo*. After semantic analysis has resolved that 2546 /// name, the field designator will instead store a FieldDecl*. 2547 uintptr_t NameOrField; 2548 2549 /// The location of the '.' in the designated initializer. 2550 unsigned DotLoc; 2551 2552 /// The location of the field name in the designated initializer. 2553 unsigned FieldLoc; 2554 }; 2555 2556 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2557 struct ArrayOrRangeDesignator { 2558 /// Location of the first index expression within the designated 2559 /// initializer expression's list of subexpressions. 2560 unsigned Index; 2561 /// The location of the '[' starting the array range designator. 2562 unsigned LBracketLoc; 2563 /// The location of the ellipsis separating the start and end 2564 /// indices. Only valid for GNU array-range designators. 2565 unsigned EllipsisLoc; 2566 /// The location of the ']' terminating the array range designator. 2567 unsigned RBracketLoc; 2568 }; 2569 2570 /// @brief Represents a single C99 designator. 2571 /// 2572 /// @todo This class is infuriatingly similar to clang::Designator, 2573 /// but minor differences (storing indices vs. storing pointers) 2574 /// keep us from reusing it. Try harder, later, to rectify these 2575 /// differences. 2576 class Designator { 2577 /// @brief The kind of designator this describes. 2578 enum { 2579 FieldDesignator, 2580 ArrayDesignator, 2581 ArrayRangeDesignator 2582 } Kind; 2583 2584 union { 2585 /// A field designator, e.g., ".x". 2586 struct FieldDesignator Field; 2587 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2588 struct ArrayOrRangeDesignator ArrayOrRange; 2589 }; 2590 friend class DesignatedInitExpr; 2591 2592 public: 2593 Designator() {} 2594 2595 /// @brief Initializes a field designator. 2596 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2597 SourceLocation FieldLoc) 2598 : Kind(FieldDesignator) { 2599 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2600 Field.DotLoc = DotLoc.getRawEncoding(); 2601 Field.FieldLoc = FieldLoc.getRawEncoding(); 2602 } 2603 2604 /// @brief Initializes an array designator. 2605 Designator(unsigned Index, SourceLocation LBracketLoc, 2606 SourceLocation RBracketLoc) 2607 : Kind(ArrayDesignator) { 2608 ArrayOrRange.Index = Index; 2609 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2610 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2611 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2612 } 2613 2614 /// @brief Initializes a GNU array-range designator. 2615 Designator(unsigned Index, SourceLocation LBracketLoc, 2616 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2617 : Kind(ArrayRangeDesignator) { 2618 ArrayOrRange.Index = Index; 2619 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2620 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2621 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2622 } 2623 2624 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2625 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2626 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2627 2628 IdentifierInfo * getFieldName(); 2629 2630 FieldDecl *getField() { 2631 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2632 if (Field.NameOrField & 0x01) 2633 return 0; 2634 else 2635 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2636 } 2637 2638 void setField(FieldDecl *FD) { 2639 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2640 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2641 } 2642 2643 SourceLocation getDotLoc() const { 2644 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2645 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2646 } 2647 2648 SourceLocation getFieldLoc() const { 2649 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2650 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2651 } 2652 2653 SourceLocation getLBracketLoc() const { 2654 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2655 "Only valid on an array or array-range designator"); 2656 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2657 } 2658 2659 SourceLocation getRBracketLoc() const { 2660 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2661 "Only valid on an array or array-range designator"); 2662 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2663 } 2664 2665 SourceLocation getEllipsisLoc() const { 2666 assert(Kind == ArrayRangeDesignator && 2667 "Only valid on an array-range designator"); 2668 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2669 } 2670 2671 unsigned getFirstExprIndex() const { 2672 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2673 "Only valid on an array or array-range designator"); 2674 return ArrayOrRange.Index; 2675 } 2676 2677 SourceLocation getStartLocation() const { 2678 if (Kind == FieldDesignator) 2679 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2680 else 2681 return getLBracketLoc(); 2682 } 2683 }; 2684 2685 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2686 unsigned NumDesignators, 2687 Expr **IndexExprs, unsigned NumIndexExprs, 2688 SourceLocation EqualOrColonLoc, 2689 bool GNUSyntax, Expr *Init); 2690 2691 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 2692 2693 /// @brief Returns the number of designators in this initializer. 2694 unsigned size() const { return NumDesignators; } 2695 2696 // Iterator access to the designators. 2697 typedef Designator* designators_iterator; 2698 designators_iterator designators_begin() { return Designators; } 2699 designators_iterator designators_end() { 2700 return Designators + NumDesignators; 2701 } 2702 2703 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 2704 2705 void setDesignators(const Designator *Desigs, unsigned NumDesigs); 2706 2707 Expr *getArrayIndex(const Designator& D); 2708 Expr *getArrayRangeStart(const Designator& D); 2709 Expr *getArrayRangeEnd(const Designator& D); 2710 2711 /// @brief Retrieve the location of the '=' that precedes the 2712 /// initializer value itself, if present. 2713 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2714 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 2715 2716 /// @brief Determines whether this designated initializer used the 2717 /// deprecated GNU syntax for designated initializers. 2718 bool usesGNUSyntax() const { return GNUSyntax; } 2719 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 2720 2721 /// @brief Retrieve the initializer value. 2722 Expr *getInit() const { 2723 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2724 } 2725 2726 void setInit(Expr *init) { 2727 *child_begin() = init; 2728 } 2729 2730 /// \brief Retrieve the total number of subexpressions in this 2731 /// designated initializer expression, including the actual 2732 /// initialized value and any expressions that occur within array 2733 /// and array-range designators. 2734 unsigned getNumSubExprs() const { return NumSubExprs; } 2735 2736 Expr *getSubExpr(unsigned Idx) { 2737 assert(Idx < NumSubExprs && "Subscript out of range"); 2738 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2739 Ptr += sizeof(DesignatedInitExpr); 2740 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 2741 } 2742 2743 void setSubExpr(unsigned Idx, Expr *E) { 2744 assert(Idx < NumSubExprs && "Subscript out of range"); 2745 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2746 Ptr += sizeof(DesignatedInitExpr); 2747 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 2748 } 2749 2750 /// \brief Replaces the designator at index @p Idx with the series 2751 /// of designators in [First, Last). 2752 void ExpandDesignator(unsigned Idx, const Designator *First, 2753 const Designator *Last); 2754 2755 virtual SourceRange getSourceRange() const; 2756 2757 static bool classof(const Stmt *T) { 2758 return T->getStmtClass() == DesignatedInitExprClass; 2759 } 2760 static bool classof(const DesignatedInitExpr *) { return true; } 2761 2762 // Iterators 2763 virtual child_iterator child_begin(); 2764 virtual child_iterator child_end(); 2765}; 2766 2767/// \brief Represents an implicitly-generated value initialization of 2768/// an object of a given type. 2769/// 2770/// Implicit value initializations occur within semantic initializer 2771/// list expressions (InitListExpr) as placeholders for subobject 2772/// initializations not explicitly specified by the user. 2773/// 2774/// \see InitListExpr 2775class ImplicitValueInitExpr : public Expr { 2776public: 2777 explicit ImplicitValueInitExpr(QualType ty) 2778 : Expr(ImplicitValueInitExprClass, ty) { } 2779 2780 /// \brief Construct an empty implicit value initialization. 2781 explicit ImplicitValueInitExpr(EmptyShell Empty) 2782 : Expr(ImplicitValueInitExprClass, Empty) { } 2783 2784 static bool classof(const Stmt *T) { 2785 return T->getStmtClass() == ImplicitValueInitExprClass; 2786 } 2787 static bool classof(const ImplicitValueInitExpr *) { return true; } 2788 2789 virtual SourceRange getSourceRange() const { 2790 return SourceRange(); 2791 } 2792 2793 // Iterators 2794 virtual child_iterator child_begin(); 2795 virtual child_iterator child_end(); 2796}; 2797 2798 2799class ParenListExpr : public Expr { 2800 Stmt **Exprs; 2801 unsigned NumExprs; 2802 SourceLocation LParenLoc, RParenLoc; 2803 2804protected: 2805 virtual void DoDestroy(ASTContext& C); 2806 2807public: 2808 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 2809 unsigned numexprs, SourceLocation rparenloc); 2810 2811 ~ParenListExpr() {} 2812 2813 /// \brief Build an empty paren list. 2814 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 2815 2816 unsigned getNumExprs() const { return NumExprs; } 2817 2818 const Expr* getExpr(unsigned Init) const { 2819 assert(Init < getNumExprs() && "Initializer access out of range!"); 2820 return cast_or_null<Expr>(Exprs[Init]); 2821 } 2822 2823 Expr* getExpr(unsigned Init) { 2824 assert(Init < getNumExprs() && "Initializer access out of range!"); 2825 return cast_or_null<Expr>(Exprs[Init]); 2826 } 2827 2828 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 2829 2830 SourceLocation getLParenLoc() const { return LParenLoc; } 2831 SourceLocation getRParenLoc() const { return RParenLoc; } 2832 2833 virtual SourceRange getSourceRange() const { 2834 return SourceRange(LParenLoc, RParenLoc); 2835 } 2836 static bool classof(const Stmt *T) { 2837 return T->getStmtClass() == ParenListExprClass; 2838 } 2839 static bool classof(const ParenListExpr *) { return true; } 2840 2841 // Iterators 2842 virtual child_iterator child_begin(); 2843 virtual child_iterator child_end(); 2844}; 2845 2846 2847//===----------------------------------------------------------------------===// 2848// Clang Extensions 2849//===----------------------------------------------------------------------===// 2850 2851 2852/// ExtVectorElementExpr - This represents access to specific elements of a 2853/// vector, and may occur on the left hand side or right hand side. For example 2854/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2855/// 2856/// Note that the base may have either vector or pointer to vector type, just 2857/// like a struct field reference. 2858/// 2859class ExtVectorElementExpr : public Expr { 2860 Stmt *Base; 2861 IdentifierInfo *Accessor; 2862 SourceLocation AccessorLoc; 2863public: 2864 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2865 SourceLocation loc) 2866 : Expr(ExtVectorElementExprClass, ty), 2867 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 2868 2869 /// \brief Build an empty vector element expression. 2870 explicit ExtVectorElementExpr(EmptyShell Empty) 2871 : Expr(ExtVectorElementExprClass, Empty) { } 2872 2873 const Expr *getBase() const { return cast<Expr>(Base); } 2874 Expr *getBase() { return cast<Expr>(Base); } 2875 void setBase(Expr *E) { Base = E; } 2876 2877 IdentifierInfo &getAccessor() const { return *Accessor; } 2878 void setAccessor(IdentifierInfo *II) { Accessor = II; } 2879 2880 SourceLocation getAccessorLoc() const { return AccessorLoc; } 2881 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 2882 2883 /// getNumElements - Get the number of components being selected. 2884 unsigned getNumElements() const; 2885 2886 /// containsDuplicateElements - Return true if any element access is 2887 /// repeated. 2888 bool containsDuplicateElements() const; 2889 2890 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2891 /// aggregate Constant of ConstantInt(s). 2892 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2893 2894 virtual SourceRange getSourceRange() const { 2895 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2896 } 2897 2898 /// isArrow - Return true if the base expression is a pointer to vector, 2899 /// return false if the base expression is a vector. 2900 bool isArrow() const; 2901 2902 static bool classof(const Stmt *T) { 2903 return T->getStmtClass() == ExtVectorElementExprClass; 2904 } 2905 static bool classof(const ExtVectorElementExpr *) { return true; } 2906 2907 // Iterators 2908 virtual child_iterator child_begin(); 2909 virtual child_iterator child_end(); 2910}; 2911 2912 2913/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2914/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2915class BlockExpr : public Expr { 2916protected: 2917 BlockDecl *TheBlock; 2918 bool HasBlockDeclRefExprs; 2919public: 2920 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2921 : Expr(BlockExprClass, ty), 2922 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2923 2924 /// \brief Build an empty block expression. 2925 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 2926 2927 const BlockDecl *getBlockDecl() const { return TheBlock; } 2928 BlockDecl *getBlockDecl() { return TheBlock; } 2929 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 2930 2931 // Convenience functions for probing the underlying BlockDecl. 2932 SourceLocation getCaretLocation() const; 2933 const Stmt *getBody() const; 2934 Stmt *getBody(); 2935 2936 virtual SourceRange getSourceRange() const { 2937 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2938 } 2939 2940 /// getFunctionType - Return the underlying function type for this block. 2941 const FunctionType *getFunctionType() const; 2942 2943 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2944 /// inside of the block that reference values outside the block. 2945 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2946 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 2947 2948 static bool classof(const Stmt *T) { 2949 return T->getStmtClass() == BlockExprClass; 2950 } 2951 static bool classof(const BlockExpr *) { return true; } 2952 2953 // Iterators 2954 virtual child_iterator child_begin(); 2955 virtual child_iterator child_end(); 2956}; 2957 2958/// BlockDeclRefExpr - A reference to a declared variable, function, 2959/// enum, etc. 2960class BlockDeclRefExpr : public Expr { 2961 ValueDecl *D; 2962 SourceLocation Loc; 2963 bool IsByRef : 1; 2964 bool ConstQualAdded : 1; 2965public: 2966 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 2967 bool constAdded = false) : 2968 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef), 2969 ConstQualAdded(constAdded) {} 2970 2971 // \brief Build an empty reference to a declared variable in a 2972 // block. 2973 explicit BlockDeclRefExpr(EmptyShell Empty) 2974 : Expr(BlockDeclRefExprClass, Empty) { } 2975 2976 ValueDecl *getDecl() { return D; } 2977 const ValueDecl *getDecl() const { return D; } 2978 void setDecl(ValueDecl *VD) { D = VD; } 2979 2980 SourceLocation getLocation() const { return Loc; } 2981 void setLocation(SourceLocation L) { Loc = L; } 2982 2983 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2984 2985 bool isByRef() const { return IsByRef; } 2986 void setByRef(bool BR) { IsByRef = BR; } 2987 2988 bool isConstQualAdded() const { return ConstQualAdded; } 2989 void setConstQualAdded(bool C) { ConstQualAdded = C; } 2990 2991 static bool classof(const Stmt *T) { 2992 return T->getStmtClass() == BlockDeclRefExprClass; 2993 } 2994 static bool classof(const BlockDeclRefExpr *) { return true; } 2995 2996 // Iterators 2997 virtual child_iterator child_begin(); 2998 virtual child_iterator child_end(); 2999}; 3000 3001} // end namespace clang 3002 3003#endif 3004