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