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