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