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