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