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