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