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