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