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