Expr.h revision 1dd986dff9ddfbec687975700770bb377988e9ed
19f23a27ed9961fd4575864e590eda328452895e7reed//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 29f23a27ed9961fd4575864e590eda328452895e7reed// 39f23a27ed9961fd4575864e590eda328452895e7reed// The LLVM Compiler Infrastructure 49f23a27ed9961fd4575864e590eda328452895e7reed// 59f23a27ed9961fd4575864e590eda328452895e7reed// This file is distributed under the University of Illinois Open Source 69f23a27ed9961fd4575864e590eda328452895e7reed// License. See LICENSE.TXT for details. 79f23a27ed9961fd4575864e590eda328452895e7reed// 89f23a27ed9961fd4575864e590eda328452895e7reed//===----------------------------------------------------------------------===// 99f23a27ed9961fd4575864e590eda328452895e7reed// 109f23a27ed9961fd4575864e590eda328452895e7reed// This file defines the Expr interface and subclasses. 119f23a27ed9961fd4575864e590eda328452895e7reed// 129f23a27ed9961fd4575864e590eda328452895e7reed//===----------------------------------------------------------------------===// 139f23a27ed9961fd4575864e590eda328452895e7reed 149f23a27ed9961fd4575864e590eda328452895e7reed#ifndef LLVM_CLANG_AST_EXPR_H 159f23a27ed9961fd4575864e590eda328452895e7reed#define LLVM_CLANG_AST_EXPR_H 169f23a27ed9961fd4575864e590eda328452895e7reed 179f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/APValue.h" 189f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/Stmt.h" 199f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/Type.h" 209f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/DeclAccessPair.h" 219f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/OperationKinds.h" 229f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/ASTVector.h" 239f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/AST/UsuallyTinyPtrVector.h" 249f23a27ed9961fd4575864e590eda328452895e7reed#include "clang/Basic/TypeTraits.h" 259f23a27ed9961fd4575864e590eda328452895e7reed#include "llvm/ADT/APSInt.h" 269f23a27ed9961fd4575864e590eda328452895e7reed#include "llvm/ADT/APFloat.h" 279f23a27ed9961fd4575864e590eda328452895e7reed#include "llvm/ADT/SmallVector.h" 289f23a27ed9961fd4575864e590eda328452895e7reed#include "llvm/ADT/StringRef.h" 299f23a27ed9961fd4575864e590eda328452895e7reed#include <cctype> 309f23a27ed9961fd4575864e590eda328452895e7reed 319f23a27ed9961fd4575864e590eda328452895e7reednamespace clang { 329f23a27ed9961fd4575864e590eda328452895e7reed class ASTContext; 339f23a27ed9961fd4575864e590eda328452895e7reed class APValue; 349f23a27ed9961fd4575864e590eda328452895e7reed class Decl; 359f23a27ed9961fd4575864e590eda328452895e7reed class IdentifierInfo; 369f23a27ed9961fd4575864e590eda328452895e7reed class ParmVarDecl; 379f23a27ed9961fd4575864e590eda328452895e7reed class NamedDecl; 389f23a27ed9961fd4575864e590eda328452895e7reed class ValueDecl; 399f23a27ed9961fd4575864e590eda328452895e7reed class BlockDecl; 409f23a27ed9961fd4575864e590eda328452895e7reed class CXXBaseSpecifier; 419f23a27ed9961fd4575864e590eda328452895e7reed class CXXOperatorCallExpr; 429f23a27ed9961fd4575864e590eda328452895e7reed class CXXMemberCallExpr; 439f23a27ed9961fd4575864e590eda328452895e7reed class ObjCPropertyRefExpr; 449f23a27ed9961fd4575864e590eda328452895e7reed class TemplateArgumentLoc; 459f23a27ed9961fd4575864e590eda328452895e7reed class TemplateArgumentListInfo; 469f23a27ed9961fd4575864e590eda328452895e7reed class OpaqueValueExpr; 479f23a27ed9961fd4575864e590eda328452895e7reed 489f23a27ed9961fd4575864e590eda328452895e7reed/// \brief A simple array of base specifiers. 499f23a27ed9961fd4575864e590eda328452895e7reedtypedef llvm::SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; 509f23a27ed9961fd4575864e590eda328452895e7reed 519f23a27ed9961fd4575864e590eda328452895e7reed/// Expr - This represents one expression. Note that Expr's are subclasses of 529f23a27ed9961fd4575864e590eda328452895e7reed/// Stmt. This allows an expression to be transparently used any place a Stmt 539f23a27ed9961fd4575864e590eda328452895e7reed/// is required. 549f23a27ed9961fd4575864e590eda328452895e7reed/// 559f23a27ed9961fd4575864e590eda328452895e7reedclass Expr : public Stmt { 569f23a27ed9961fd4575864e590eda328452895e7reed QualType TR; 579f23a27ed9961fd4575864e590eda328452895e7reed 589f23a27ed9961fd4575864e590eda328452895e7reedprotected: 599f23a27ed9961fd4575864e590eda328452895e7reed Expr(StmtClass SC, QualType T, ExprValueKind VK, ExprObjectKind OK, 609f23a27ed9961fd4575864e590eda328452895e7reed bool TD, bool VD, bool ContainsUnexpandedParameterPack) : Stmt(SC) { 619f23a27ed9961fd4575864e590eda328452895e7reed ExprBits.TypeDependent = TD; 62 ExprBits.ValueDependent = VD; 63 ExprBits.ValueKind = VK; 64 ExprBits.ObjectKind = OK; 65 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 66 setType(T); 67 } 68 69 /// \brief Construct an empty expression. 70 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { } 71 72public: 73 QualType getType() const { return TR; } 74 void setType(QualType t) { 75 // In C++, the type of an expression is always adjusted so that it 76 // will not have reference type an expression will never have 77 // reference type (C++ [expr]p6). Use 78 // QualType::getNonReferenceType() to retrieve the non-reference 79 // type. Additionally, inspect Expr::isLvalue to determine whether 80 // an expression that is adjusted in this manner should be 81 // considered an lvalue. 82 assert((t.isNull() || !t->isReferenceType()) && 83 "Expressions can't have reference type"); 84 85 TR = t; 86 } 87 88 /// isValueDependent - Determines whether this expression is 89 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 90 /// array bound of "Chars" in the following example is 91 /// value-dependent. 92 /// @code 93 /// template<int Size, char (&Chars)[Size]> struct meta_string; 94 /// @endcode 95 bool isValueDependent() const { return ExprBits.ValueDependent; } 96 97 /// \brief Set whether this expression is value-dependent or not. 98 void setValueDependent(bool VD) { ExprBits.ValueDependent = VD; } 99 100 /// isTypeDependent - Determines whether this expression is 101 /// type-dependent (C++ [temp.dep.expr]), which means that its type 102 /// could change from one template instantiation to the next. For 103 /// example, the expressions "x" and "x + y" are type-dependent in 104 /// the following code, but "y" is not type-dependent: 105 /// @code 106 /// template<typename T> 107 /// void add(T x, int y) { 108 /// x + y; 109 /// } 110 /// @endcode 111 bool isTypeDependent() const { return ExprBits.TypeDependent; } 112 113 /// \brief Set whether this expression is type-dependent or not. 114 void setTypeDependent(bool TD) { ExprBits.TypeDependent = TD; } 115 116 /// \brief Whether this expression contains an unexpanded parameter 117 /// pack (for C++0x variadic templates). 118 /// 119 /// Given the following function template: 120 /// 121 /// \code 122 /// template<typename F, typename ...Types> 123 /// void forward(const F &f, Types &&...args) { 124 /// f(static_cast<Types&&>(args)...); 125 /// } 126 /// \endcode 127 /// 128 /// The expressions \c args and \c static_cast<Types&&>(args) both 129 /// contain parameter packs. 130 bool containsUnexpandedParameterPack() const { 131 return ExprBits.ContainsUnexpandedParameterPack; 132 } 133 134 /// \brief Set the bit that describes whether this expression 135 /// contains an unexpanded parameter pack. 136 void setContainsUnexpandedParameterPack(bool PP = true) { 137 ExprBits.ContainsUnexpandedParameterPack = PP; 138 } 139 140 /// getExprLoc - Return the preferred location for the arrow when diagnosing 141 /// a problem with a generic expression. 142 SourceLocation getExprLoc() const; 143 144 /// isUnusedResultAWarning - Return true if this immediate expression should 145 /// be warned about if the result is unused. If so, fill in Loc and Ranges 146 /// with location to warn on and the source range[s] to report with the 147 /// warning. 148 bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, 149 SourceRange &R2, ASTContext &Ctx) const; 150 151 /// isLValue - True if this expression is an "l-value" according to 152 /// the rules of the current language. C and C++ give somewhat 153 /// different rules for this concept, but in general, the result of 154 /// an l-value expression identifies a specific object whereas the 155 /// result of an r-value expression is a value detached from any 156 /// specific storage. 157 /// 158 /// C++0x divides the concept of "r-value" into pure r-values 159 /// ("pr-values") and so-called expiring values ("x-values"), which 160 /// identify specific objects that can be safely cannibalized for 161 /// their resources. This is an unfortunate abuse of terminology on 162 /// the part of the C++ committee. In Clang, when we say "r-value", 163 /// we generally mean a pr-value. 164 bool isLValue() const { return getValueKind() == VK_LValue; } 165 bool isRValue() const { return getValueKind() == VK_RValue; } 166 bool isXValue() const { return getValueKind() == VK_XValue; } 167 bool isGLValue() const { return getValueKind() != VK_RValue; } 168 169 enum LValueClassification { 170 LV_Valid, 171 LV_NotObjectType, 172 LV_IncompleteVoidType, 173 LV_DuplicateVectorComponents, 174 LV_InvalidExpression, 175 LV_InvalidMessageExpression, 176 LV_MemberFunction, 177 LV_SubObjCPropertySetting, 178 LV_ClassTemporary 179 }; 180 /// Reasons why an expression might not be an l-value. 181 LValueClassification ClassifyLValue(ASTContext &Ctx) const; 182 183 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 184 /// does not have an incomplete type, does not have a const-qualified type, 185 /// and if it is a structure or union, does not have any member (including, 186 /// recursively, any member or element of all contained aggregates or unions) 187 /// with a const-qualified type. 188 /// 189 /// \param Loc [in] [out] - A source location which *may* be filled 190 /// in with the location of the expression making this a 191 /// non-modifiable lvalue, if specified. 192 enum isModifiableLvalueResult { 193 MLV_Valid, 194 MLV_NotObjectType, 195 MLV_IncompleteVoidType, 196 MLV_DuplicateVectorComponents, 197 MLV_InvalidExpression, 198 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 199 MLV_IncompleteType, 200 MLV_ConstQualified, 201 MLV_ArrayType, 202 MLV_NotBlockQualified, 203 MLV_ReadonlyProperty, 204 MLV_NoSetterProperty, 205 MLV_MemberFunction, 206 MLV_SubObjCPropertySetting, 207 MLV_InvalidMessageExpression, 208 MLV_ClassTemporary 209 }; 210 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, 211 SourceLocation *Loc = 0) const; 212 213 /// \brief The return type of classify(). Represents the C++0x expression 214 /// taxonomy. 215 class Classification { 216 public: 217 /// \brief The various classification results. Most of these mean prvalue. 218 enum Kinds { 219 CL_LValue, 220 CL_XValue, 221 CL_Function, // Functions cannot be lvalues in C. 222 CL_Void, // Void cannot be an lvalue in C. 223 CL_AddressableVoid, // Void expression whose address can be taken in C. 224 CL_DuplicateVectorComponents, // A vector shuffle with dupes. 225 CL_MemberFunction, // An expression referring to a member function 226 CL_SubObjCPropertySetting, 227 CL_ClassTemporary, // A prvalue of class type 228 CL_ObjCMessageRValue, // ObjC message is an rvalue 229 CL_PRValue // A prvalue for any other reason, of any other type 230 }; 231 /// \brief The results of modification testing. 232 enum ModifiableType { 233 CM_Untested, // testModifiable was false. 234 CM_Modifiable, 235 CM_RValue, // Not modifiable because it's an rvalue 236 CM_Function, // Not modifiable because it's a function; C++ only 237 CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext 238 CM_NotBlockQualified, // Not captured in the closure 239 CM_NoSetterProperty,// Implicit assignment to ObjC property without setter 240 CM_ConstQualified, 241 CM_ArrayType, 242 CM_IncompleteType 243 }; 244 245 private: 246 friend class Expr; 247 248 unsigned short Kind; 249 unsigned short Modifiable; 250 251 explicit Classification(Kinds k, ModifiableType m) 252 : Kind(k), Modifiable(m) 253 {} 254 255 public: 256 Classification() {} 257 258 Kinds getKind() const { return static_cast<Kinds>(Kind); } 259 ModifiableType getModifiable() const { 260 assert(Modifiable != CM_Untested && "Did not test for modifiability."); 261 return static_cast<ModifiableType>(Modifiable); 262 } 263 bool isLValue() const { return Kind == CL_LValue; } 264 bool isXValue() const { return Kind == CL_XValue; } 265 bool isGLValue() const { return Kind <= CL_XValue; } 266 bool isPRValue() const { return Kind >= CL_Function; } 267 bool isRValue() const { return Kind >= CL_XValue; } 268 bool isModifiable() const { return getModifiable() == CM_Modifiable; } 269 270 /// \brief Create a simple, modifiably lvalue 271 static Classification makeSimpleLValue() { 272 return Classification(CL_LValue, CM_Modifiable); 273 } 274 275 }; 276 /// \brief Classify - Classify this expression according to the C++0x 277 /// expression taxonomy. 278 /// 279 /// C++0x defines ([basic.lval]) a new taxonomy of expressions to replace the 280 /// old lvalue vs rvalue. This function determines the type of expression this 281 /// is. There are three expression types: 282 /// - lvalues are classical lvalues as in C++03. 283 /// - prvalues are equivalent to rvalues in C++03. 284 /// - xvalues are expressions yielding unnamed rvalue references, e.g. a 285 /// function returning an rvalue reference. 286 /// lvalues and xvalues are collectively referred to as glvalues, while 287 /// prvalues and xvalues together form rvalues. 288 Classification Classify(ASTContext &Ctx) const { 289 return ClassifyImpl(Ctx, 0); 290 } 291 292 /// \brief ClassifyModifiable - Classify this expression according to the 293 /// C++0x expression taxonomy, and see if it is valid on the left side 294 /// of an assignment. 295 /// 296 /// This function extends classify in that it also tests whether the 297 /// expression is modifiable (C99 6.3.2.1p1). 298 /// \param Loc A source location that might be filled with a relevant location 299 /// if the expression is not modifiable. 300 Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{ 301 return ClassifyImpl(Ctx, &Loc); 302 } 303 304 /// getValueKindForType - Given a formal return or parameter type, 305 /// give its value kind. 306 static ExprValueKind getValueKindForType(QualType T) { 307 if (const ReferenceType *RT = T->getAs<ReferenceType>()) 308 return (isa<LValueReferenceType>(RT) 309 ? VK_LValue 310 : (RT->getPointeeType()->isFunctionType() 311 ? VK_LValue : VK_XValue)); 312 return VK_RValue; 313 } 314 315 /// getValueKind - The value kind that this expression produces. 316 ExprValueKind getValueKind() const { 317 return static_cast<ExprValueKind>(ExprBits.ValueKind); 318 } 319 320 /// getObjectKind - The object kind that this expression produces. 321 /// Object kinds are meaningful only for expressions that yield an 322 /// l-value or x-value. 323 ExprObjectKind getObjectKind() const { 324 return static_cast<ExprObjectKind>(ExprBits.ObjectKind); 325 } 326 327 bool isOrdinaryOrBitFieldObject() const { 328 ExprObjectKind OK = getObjectKind(); 329 return (OK == OK_Ordinary || OK == OK_BitField); 330 } 331 332 /// setValueKind - Set the value kind produced by this expression. 333 void setValueKind(ExprValueKind Cat) { ExprBits.ValueKind = Cat; } 334 335 /// setObjectKind - Set the object kind produced by this expression. 336 void setObjectKind(ExprObjectKind Cat) { ExprBits.ObjectKind = Cat; } 337 338private: 339 Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const; 340 341public: 342 343 /// \brief If this expression refers to a bit-field, retrieve the 344 /// declaration of that bit-field. 345 FieldDecl *getBitField(); 346 347 const FieldDecl *getBitField() const { 348 return const_cast<Expr*>(this)->getBitField(); 349 } 350 351 /// \brief If this expression is an l-value for an Objective C 352 /// property, find the underlying property reference expression. 353 const ObjCPropertyRefExpr *getObjCProperty() const; 354 355 /// \brief Returns whether this expression refers to a vector element. 356 bool refersToVectorElement() const; 357 358 /// isKnownToHaveBooleanValue - Return true if this is an integer expression 359 /// that is known to return 0 or 1. This happens for _Bool/bool expressions 360 /// but also int expressions which are produced by things like comparisons in 361 /// C. 362 bool isKnownToHaveBooleanValue() const; 363 364 /// isIntegerConstantExpr - Return true if this expression is a valid integer 365 /// constant expression, and, if so, return its value in Result. If not a 366 /// valid i-c-e, return false and fill in Loc (if specified) with the location 367 /// of the invalid expression. 368 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 369 SourceLocation *Loc = 0, 370 bool isEvaluated = true) const; 371 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 372 llvm::APSInt X; 373 return isIntegerConstantExpr(X, Ctx, Loc); 374 } 375 /// isConstantInitializer - Returns true if this expression is a constant 376 /// initializer, which can be emitted at compile-time. 377 bool isConstantInitializer(ASTContext &Ctx, bool ForRef) const; 378 379 /// EvalResult is a struct with detailed info about an evaluated expression. 380 struct EvalResult { 381 /// Val - This is the value the expression can be folded to. 382 APValue Val; 383 384 /// HasSideEffects - Whether the evaluated expression has side effects. 385 /// For example, (f() && 0) can be folded, but it still has side effects. 386 bool HasSideEffects; 387 388 /// Diag - If the expression is unfoldable, then Diag contains a note 389 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 390 /// position for the error, and DiagExpr is the expression that caused 391 /// the error. 392 /// If the expression is foldable, but not an integer constant expression, 393 /// Diag contains a note diagnostic that describes why it isn't an integer 394 /// constant expression. If the expression *is* an integer constant 395 /// expression, then Diag will be zero. 396 unsigned Diag; 397 const Expr *DiagExpr; 398 SourceLocation DiagLoc; 399 400 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 401 402 // isGlobalLValue - Return true if the evaluated lvalue expression 403 // is global. 404 bool isGlobalLValue() const; 405 // hasSideEffects - Return true if the evaluated expression has 406 // side effects. 407 bool hasSideEffects() const { 408 return HasSideEffects; 409 } 410 }; 411 412 /// Evaluate - Return true if this is a constant which we can fold using 413 /// any crazy technique (that has nothing to do with language standards) that 414 /// we want to. If this function returns true, it returns the folded constant 415 /// in Result. 416 bool Evaluate(EvalResult &Result, const ASTContext &Ctx) const; 417 418 /// EvaluateAsBooleanCondition - Return true if this is a constant 419 /// which we we can fold and convert to a boolean condition using 420 /// any crazy technique that we want to. 421 bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx) const; 422 423 /// isEvaluatable - Call Evaluate to see if this expression can be constant 424 /// folded, but discard the result. 425 bool isEvaluatable(const ASTContext &Ctx) const; 426 427 /// HasSideEffects - This routine returns true for all those expressions 428 /// which must be evaluated each time and must not be optimized away 429 /// or evaluated at compile time. Example is a function call, volatile 430 /// variable read. 431 bool HasSideEffects(const ASTContext &Ctx) const; 432 433 /// EvaluateAsInt - Call Evaluate and return the folded integer. This 434 /// must be called on an expression that constant folds to an integer. 435 llvm::APSInt EvaluateAsInt(const ASTContext &Ctx) const; 436 437 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue 438 /// with link time known address. 439 bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const; 440 441 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue. 442 bool EvaluateAsAnyLValue(EvalResult &Result, const ASTContext &Ctx) const; 443 444 /// \brief Enumeration used to describe the kind of Null pointer constant 445 /// returned from \c isNullPointerConstant(). 446 enum NullPointerConstantKind { 447 /// \brief Expression is not a Null pointer constant. 448 NPCK_NotNull = 0, 449 450 /// \brief Expression is a Null pointer constant built from a zero integer. 451 NPCK_ZeroInteger, 452 453 /// \brief Expression is a C++0X nullptr. 454 NPCK_CXX0X_nullptr, 455 456 /// \brief Expression is a GNU-style __null constant. 457 NPCK_GNUNull 458 }; 459 460 /// \brief Enumeration used to describe how \c isNullPointerConstant() 461 /// should cope with value-dependent expressions. 462 enum NullPointerConstantValueDependence { 463 /// \brief Specifies that the expression should never be value-dependent. 464 NPC_NeverValueDependent = 0, 465 466 /// \brief Specifies that a value-dependent expression of integral or 467 /// dependent type should be considered a null pointer constant. 468 NPC_ValueDependentIsNull, 469 470 /// \brief Specifies that a value-dependent expression should be considered 471 /// to never be a null pointer constant. 472 NPC_ValueDependentIsNotNull 473 }; 474 475 /// isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to 476 /// a Null pointer constant. The return value can further distinguish the 477 /// kind of NULL pointer constant that was detected. 478 NullPointerConstantKind isNullPointerConstant( 479 ASTContext &Ctx, 480 NullPointerConstantValueDependence NPC) const; 481 482 /// isOBJCGCCandidate - Return true if this expression may be used in a read/ 483 /// write barrier. 484 bool isOBJCGCCandidate(ASTContext &Ctx) const; 485 486 /// \brief Returns true if this expression is a bound member function. 487 bool isBoundMemberFunction(ASTContext &Ctx) const; 488 489 /// \brief Given an expression of bound-member type, find the type 490 /// of the member. Returns null if this is an *overloaded* bound 491 /// member expression. 492 static QualType findBoundMemberType(const Expr *expr); 493 494 /// \brief Result type of CanThrow(). 495 enum CanThrowResult { 496 CT_Cannot, 497 CT_Dependent, 498 CT_Can 499 }; 500 /// \brief Test if this expression, if evaluated, might throw, according to 501 /// the rules of C++ [expr.unary.noexcept]. 502 CanThrowResult CanThrow(ASTContext &C) const; 503 504 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 505 /// its subexpression. If that subexpression is also a ParenExpr, 506 /// then this method recursively returns its subexpression, and so forth. 507 /// Otherwise, the method returns the current Expr. 508 Expr *IgnoreParens(); 509 510 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 511 /// or CastExprs, returning their operand. 512 Expr *IgnoreParenCasts(); 513 514 /// IgnoreParenImpCasts - Ignore parentheses and implicit casts. Strip off any 515 /// ParenExpr or ImplicitCastExprs, returning their operand. 516 Expr *IgnoreParenImpCasts(); 517 518 const Expr *IgnoreParenImpCasts() const { 519 return const_cast<Expr*>(this)->IgnoreParenImpCasts(); 520 } 521 522 /// Ignore parentheses and lvalue casts. Strip off any ParenExpr and 523 /// CastExprs that represent lvalue casts, returning their operand. 524 Expr *IgnoreParenLValueCasts(); 525 526 const Expr *IgnoreParenLValueCasts() const { 527 return const_cast<Expr*>(this)->IgnoreParenLValueCasts(); 528 } 529 530 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the 531 /// value (including ptr->int casts of the same size). Strip off any 532 /// ParenExpr or CastExprs, returning their operand. 533 Expr *IgnoreParenNoopCasts(ASTContext &Ctx); 534 535 /// \brief Determine whether this expression is a default function argument. 536 /// 537 /// Default arguments are implicitly generated in the abstract syntax tree 538 /// by semantic analysis for function calls, object constructions, etc. in 539 /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; 540 /// this routine also looks through any implicit casts to determine whether 541 /// the expression is a default argument. 542 bool isDefaultArgument() const; 543 544 /// \brief Determine whether the result of this expression is a 545 /// temporary object of the given class type. 546 bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const; 547 548 /// \brief Whether this expression is an implicit reference to 'this' in C++. 549 bool isImplicitCXXThis() const; 550 551 const Expr *IgnoreParens() const { 552 return const_cast<Expr*>(this)->IgnoreParens(); 553 } 554 const Expr *IgnoreParenCasts() const { 555 return const_cast<Expr*>(this)->IgnoreParenCasts(); 556 } 557 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const { 558 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx); 559 } 560 561 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 562 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 563 564 static bool classof(const Stmt *T) { 565 return T->getStmtClass() >= firstExprConstant && 566 T->getStmtClass() <= lastExprConstant; 567 } 568 static bool classof(const Expr *) { return true; } 569}; 570 571 572//===----------------------------------------------------------------------===// 573// Primary Expressions. 574//===----------------------------------------------------------------------===// 575 576/// OpaqueValueExpr - An expression referring to an opaque object of a 577/// fixed type and value class. These don't correspond to concrete 578/// syntax; instead they're used to express operations (usually copy 579/// operations) on values whose source is generally obvious from 580/// context. 581class OpaqueValueExpr : public Expr { 582 friend class ASTStmtReader; 583 Expr *SourceExpr; 584 SourceLocation Loc; 585 586public: 587 OpaqueValueExpr(SourceLocation Loc, QualType T, ExprValueKind VK, 588 ExprObjectKind OK = OK_Ordinary) 589 : Expr(OpaqueValueExprClass, T, VK, OK, 590 T->isDependentType(), T->isDependentType(), false), 591 SourceExpr(0), Loc(Loc) { 592 } 593 594 /// Given an expression which invokes a copy constructor --- i.e. a 595 /// CXXConstructExpr, possibly wrapped in an ExprWithCleanups --- 596 /// find the OpaqueValueExpr that's the source of the construction. 597 static const OpaqueValueExpr *findInCopyConstruct(const Expr *expr); 598 599 explicit OpaqueValueExpr(EmptyShell Empty) 600 : Expr(OpaqueValueExprClass, Empty) { } 601 602 /// \brief Retrieve the location of this expression. 603 SourceLocation getLocation() const { return Loc; } 604 605 SourceRange getSourceRange() const { 606 if (SourceExpr) return SourceExpr->getSourceRange(); 607 return Loc; 608 } 609 SourceLocation getExprLoc() const { 610 if (SourceExpr) return SourceExpr->getExprLoc(); 611 return Loc; 612 } 613 614 child_range children() { return child_range(); } 615 616 /// The source expression of an opaque value expression is the 617 /// expression which originally generated the value. This is 618 /// provided as a convenience for analyses that don't wish to 619 /// precisely model the execution behavior of the program. 620 /// 621 /// The source expression is typically set when building the 622 /// expression which binds the opaque value expression in the first 623 /// place. 624 Expr *getSourceExpr() const { return SourceExpr; } 625 void setSourceExpr(Expr *e) { SourceExpr = e; } 626 627 static bool classof(const Stmt *T) { 628 return T->getStmtClass() == OpaqueValueExprClass; 629 } 630 static bool classof(const OpaqueValueExpr *) { return true; } 631}; 632 633/// \brief Represents an explicit template argument list in C++, e.g., 634/// the "<int>" in "sort<int>". 635struct ExplicitTemplateArgumentList { 636 /// \brief The source location of the left angle bracket ('<'); 637 SourceLocation LAngleLoc; 638 639 /// \brief The source location of the right angle bracket ('>'); 640 SourceLocation RAngleLoc; 641 642 /// \brief The number of template arguments in TemplateArgs. 643 /// The actual template arguments (if any) are stored after the 644 /// ExplicitTemplateArgumentList structure. 645 unsigned NumTemplateArgs; 646 647 /// \brief Retrieve the template arguments 648 TemplateArgumentLoc *getTemplateArgs() { 649 return reinterpret_cast<TemplateArgumentLoc *> (this + 1); 650 } 651 652 /// \brief Retrieve the template arguments 653 const TemplateArgumentLoc *getTemplateArgs() const { 654 return reinterpret_cast<const TemplateArgumentLoc *> (this + 1); 655 } 656 657 void initializeFrom(const TemplateArgumentListInfo &List); 658 void initializeFrom(const TemplateArgumentListInfo &List, 659 bool &Dependent, bool &ContainsUnexpandedParameterPack); 660 void copyInto(TemplateArgumentListInfo &List) const; 661 static std::size_t sizeFor(unsigned NumTemplateArgs); 662 static std::size_t sizeFor(const TemplateArgumentListInfo &List); 663}; 664 665/// \brief A reference to a declared variable, function, enum, etc. 666/// [C99 6.5.1p2] 667/// 668/// This encodes all the information about how a declaration is referenced 669/// within an expression. 670/// 671/// There are several optional constructs attached to DeclRefExprs only when 672/// they apply in order to conserve memory. These are laid out past the end of 673/// the object, and flags in the DeclRefExprBitfield track whether they exist: 674/// 675/// DeclRefExprBits.HasQualifier: 676/// Specifies when this declaration reference expression has a C++ 677/// nested-name-specifier. 678/// DeclRefExprBits.HasFoundDecl: 679/// Specifies when this declaration reference expression has a record of 680/// a NamedDecl (different from the referenced ValueDecl) which was found 681/// during name lookup and/or overload resolution. 682/// DeclRefExprBits.HasExplicitTemplateArgs: 683/// Specifies when this declaration reference expression has an explicit 684/// C++ template argument list. 685class DeclRefExpr : public Expr { 686 /// \brief The declaration that we are referencing. 687 ValueDecl *D; 688 689 /// \brief The location of the declaration name itself. 690 SourceLocation Loc; 691 692 /// \brief Provides source/type location info for the declaration name 693 /// embedded in D. 694 DeclarationNameLoc DNLoc; 695 696 /// \brief Helper to retrieve the optional NestedNameSpecifierLoc. 697 NestedNameSpecifierLoc &getInternalQualifierLoc() { 698 assert(hasQualifier()); 699 return *reinterpret_cast<NestedNameSpecifierLoc *>(this + 1); 700 } 701 702 /// \brief Helper to retrieve the optional NestedNameSpecifierLoc. 703 const NestedNameSpecifierLoc &getInternalQualifierLoc() const { 704 return const_cast<DeclRefExpr *>(this)->getInternalQualifierLoc(); 705 } 706 707 /// \brief Test whether there is a distinct FoundDecl attached to the end of 708 /// this DRE. 709 bool hasFoundDecl() const { return DeclRefExprBits.HasFoundDecl; } 710 711 /// \brief Helper to retrieve the optional NamedDecl through which this 712 /// reference occured. 713 NamedDecl *&getInternalFoundDecl() { 714 assert(hasFoundDecl()); 715 if (hasQualifier()) 716 return *reinterpret_cast<NamedDecl **>(&getInternalQualifierLoc() + 1); 717 return *reinterpret_cast<NamedDecl **>(this + 1); 718 } 719 720 /// \brief Helper to retrieve the optional NamedDecl through which this 721 /// reference occured. 722 NamedDecl *getInternalFoundDecl() const { 723 return const_cast<DeclRefExpr *>(this)->getInternalFoundDecl(); 724 } 725 726 DeclRefExpr(NestedNameSpecifierLoc QualifierLoc, 727 ValueDecl *D, const DeclarationNameInfo &NameInfo, 728 NamedDecl *FoundD, 729 const TemplateArgumentListInfo *TemplateArgs, 730 QualType T, ExprValueKind VK); 731 732 /// \brief Construct an empty declaration reference expression. 733 explicit DeclRefExpr(EmptyShell Empty) 734 : Expr(DeclRefExprClass, Empty) { } 735 736 /// \brief Computes the type- and value-dependence flags for this 737 /// declaration reference expression. 738 void computeDependence(); 739 740public: 741 DeclRefExpr(ValueDecl *D, QualType T, ExprValueKind VK, SourceLocation L) 742 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false), 743 D(D), Loc(L) { 744 DeclRefExprBits.HasQualifier = 0; 745 DeclRefExprBits.HasExplicitTemplateArgs = 0; 746 DeclRefExprBits.HasFoundDecl = 0; 747 computeDependence(); 748 } 749 750 static DeclRefExpr *Create(ASTContext &Context, 751 NestedNameSpecifierLoc QualifierLoc, 752 ValueDecl *D, 753 SourceLocation NameLoc, 754 QualType T, ExprValueKind VK, 755 NamedDecl *FoundD = 0, 756 const TemplateArgumentListInfo *TemplateArgs = 0); 757 758 static DeclRefExpr *Create(ASTContext &Context, 759 NestedNameSpecifierLoc QualifierLoc, 760 ValueDecl *D, 761 const DeclarationNameInfo &NameInfo, 762 QualType T, ExprValueKind VK, 763 NamedDecl *FoundD = 0, 764 const TemplateArgumentListInfo *TemplateArgs = 0); 765 766 /// \brief Construct an empty declaration reference expression. 767 static DeclRefExpr *CreateEmpty(ASTContext &Context, 768 bool HasQualifier, 769 bool HasFoundDecl, 770 bool HasExplicitTemplateArgs, 771 unsigned NumTemplateArgs); 772 773 ValueDecl *getDecl() { return D; } 774 const ValueDecl *getDecl() const { return D; } 775 void setDecl(ValueDecl *NewD) { D = NewD; } 776 777 DeclarationNameInfo getNameInfo() const { 778 return DeclarationNameInfo(getDecl()->getDeclName(), Loc, DNLoc); 779 } 780 781 SourceLocation getLocation() const { return Loc; } 782 void setLocation(SourceLocation L) { Loc = L; } 783 SourceRange getSourceRange() const; 784 785 /// \brief Determine whether this declaration reference was preceded by a 786 /// C++ nested-name-specifier, e.g., \c N::foo. 787 bool hasQualifier() const { return DeclRefExprBits.HasQualifier; } 788 789 /// \brief If the name was qualified, retrieves the nested-name-specifier 790 /// that precedes the name. Otherwise, returns NULL. 791 NestedNameSpecifier *getQualifier() const { 792 if (!hasQualifier()) 793 return 0; 794 795 return getInternalQualifierLoc().getNestedNameSpecifier(); 796 } 797 798 /// \brief If the name was qualified, retrieves the nested-name-specifier 799 /// that precedes the name, with source-location information. 800 NestedNameSpecifierLoc getQualifierLoc() const { 801 if (!hasQualifier()) 802 return NestedNameSpecifierLoc(); 803 804 return getInternalQualifierLoc(); 805 } 806 807 /// \brief Get the NamedDecl through which this reference occured. 808 /// 809 /// This Decl may be different from the ValueDecl actually referred to in the 810 /// presence of using declarations, etc. It always returns non-NULL, and may 811 /// simple return the ValueDecl when appropriate. 812 NamedDecl *getFoundDecl() { 813 return hasFoundDecl() ? getInternalFoundDecl() : D; 814 } 815 816 /// \brief Get the NamedDecl through which this reference occurred. 817 /// See non-const variant. 818 const NamedDecl *getFoundDecl() const { 819 return hasFoundDecl() ? getInternalFoundDecl() : D; 820 } 821 822 /// \brief Determines whether this declaration reference was followed by an 823 /// explict template argument list. 824 bool hasExplicitTemplateArgs() const { 825 return DeclRefExprBits.HasExplicitTemplateArgs; 826 } 827 828 /// \brief Retrieve the explicit template argument list that followed the 829 /// member template name. 830 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 831 assert(hasExplicitTemplateArgs()); 832 if (hasFoundDecl()) 833 return *reinterpret_cast<ExplicitTemplateArgumentList *>( 834 &getInternalFoundDecl() + 1); 835 836 if (hasQualifier()) 837 return *reinterpret_cast<ExplicitTemplateArgumentList *>( 838 &getInternalQualifierLoc() + 1); 839 840 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 841 } 842 843 /// \brief Retrieve the explicit template argument list that followed the 844 /// member template name. 845 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 846 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgs(); 847 } 848 849 /// \brief Retrieves the optional explicit template arguments. 850 /// This points to the same data as getExplicitTemplateArgs(), but 851 /// returns null if there are no explicit template arguments. 852 const ExplicitTemplateArgumentList *getExplicitTemplateArgsOpt() const { 853 if (!hasExplicitTemplateArgs()) return 0; 854 return &getExplicitTemplateArgs(); 855 } 856 857 /// \brief Copies the template arguments (if present) into the given 858 /// structure. 859 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 860 if (hasExplicitTemplateArgs()) 861 getExplicitTemplateArgs().copyInto(List); 862 } 863 864 /// \brief Retrieve the location of the left angle bracket following the 865 /// member name ('<'), if any. 866 SourceLocation getLAngleLoc() const { 867 if (!hasExplicitTemplateArgs()) 868 return SourceLocation(); 869 870 return getExplicitTemplateArgs().LAngleLoc; 871 } 872 873 /// \brief Retrieve the template arguments provided as part of this 874 /// template-id. 875 const TemplateArgumentLoc *getTemplateArgs() const { 876 if (!hasExplicitTemplateArgs()) 877 return 0; 878 879 return getExplicitTemplateArgs().getTemplateArgs(); 880 } 881 882 /// \brief Retrieve the number of template arguments provided as part of this 883 /// template-id. 884 unsigned getNumTemplateArgs() const { 885 if (!hasExplicitTemplateArgs()) 886 return 0; 887 888 return getExplicitTemplateArgs().NumTemplateArgs; 889 } 890 891 /// \brief Retrieve the location of the right angle bracket following the 892 /// template arguments ('>'). 893 SourceLocation getRAngleLoc() const { 894 if (!hasExplicitTemplateArgs()) 895 return SourceLocation(); 896 897 return getExplicitTemplateArgs().RAngleLoc; 898 } 899 900 static bool classof(const Stmt *T) { 901 return T->getStmtClass() == DeclRefExprClass; 902 } 903 static bool classof(const DeclRefExpr *) { return true; } 904 905 // Iterators 906 child_range children() { return child_range(); } 907 908 friend class ASTStmtReader; 909 friend class ASTStmtWriter; 910}; 911 912/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 913class PredefinedExpr : public Expr { 914public: 915 enum IdentType { 916 Func, 917 Function, 918 PrettyFunction, 919 /// PrettyFunctionNoVirtual - The same as PrettyFunction, except that the 920 /// 'virtual' keyword is omitted for virtual member functions. 921 PrettyFunctionNoVirtual 922 }; 923 924private: 925 SourceLocation Loc; 926 IdentType Type; 927public: 928 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 929 : Expr(PredefinedExprClass, type, VK_LValue, OK_Ordinary, 930 type->isDependentType(), type->isDependentType(), 931 /*ContainsUnexpandedParameterPack=*/false), 932 Loc(l), Type(IT) {} 933 934 /// \brief Construct an empty predefined expression. 935 explicit PredefinedExpr(EmptyShell Empty) 936 : Expr(PredefinedExprClass, Empty) { } 937 938 IdentType getIdentType() const { return Type; } 939 void setIdentType(IdentType IT) { Type = IT; } 940 941 SourceLocation getLocation() const { return Loc; } 942 void setLocation(SourceLocation L) { Loc = L; } 943 944 static std::string ComputeName(IdentType IT, const Decl *CurrentDecl); 945 946 SourceRange getSourceRange() const { return SourceRange(Loc); } 947 948 static bool classof(const Stmt *T) { 949 return T->getStmtClass() == PredefinedExprClass; 950 } 951 static bool classof(const PredefinedExpr *) { return true; } 952 953 // Iterators 954 child_range children() { return child_range(); } 955}; 956 957/// \brief Used by IntegerLiteral/FloatingLiteral to store the numeric without 958/// leaking memory. 959/// 960/// For large floats/integers, APFloat/APInt will allocate memory from the heap 961/// to represent these numbers. Unfortunately, when we use a BumpPtrAllocator 962/// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with 963/// the APFloat/APInt values will never get freed. APNumericStorage uses 964/// ASTContext's allocator for memory allocation. 965class APNumericStorage { 966 unsigned BitWidth; 967 union { 968 uint64_t VAL; ///< Used to store the <= 64 bits integer value. 969 uint64_t *pVal; ///< Used to store the >64 bits integer value. 970 }; 971 972 bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; } 973 974 APNumericStorage(const APNumericStorage&); // do not implement 975 APNumericStorage& operator=(const APNumericStorage&); // do not implement 976 977protected: 978 APNumericStorage() : BitWidth(0), VAL(0) { } 979 980 llvm::APInt getIntValue() const { 981 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 982 if (NumWords > 1) 983 return llvm::APInt(BitWidth, NumWords, pVal); 984 else 985 return llvm::APInt(BitWidth, VAL); 986 } 987 void setIntValue(ASTContext &C, const llvm::APInt &Val); 988}; 989 990class APIntStorage : public APNumericStorage { 991public: 992 llvm::APInt getValue() const { return getIntValue(); } 993 void setValue(ASTContext &C, const llvm::APInt &Val) { setIntValue(C, Val); } 994}; 995 996class APFloatStorage : public APNumericStorage { 997public: 998 llvm::APFloat getValue() const { return llvm::APFloat(getIntValue()); } 999 void setValue(ASTContext &C, const llvm::APFloat &Val) { 1000 setIntValue(C, Val.bitcastToAPInt()); 1001 } 1002}; 1003 1004class IntegerLiteral : public Expr { 1005 APIntStorage Num; 1006 SourceLocation Loc; 1007 1008 /// \brief Construct an empty integer literal. 1009 explicit IntegerLiteral(EmptyShell Empty) 1010 : Expr(IntegerLiteralClass, Empty) { } 1011 1012public: 1013 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 1014 // or UnsignedLongLongTy 1015 IntegerLiteral(ASTContext &C, const llvm::APInt &V, 1016 QualType type, SourceLocation l) 1017 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false, 1018 false), 1019 Loc(l) { 1020 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 1021 assert(V.getBitWidth() == C.getIntWidth(type) && 1022 "Integer type is not the correct size for constant."); 1023 setValue(C, V); 1024 } 1025 1026 /// \brief Returns a new integer literal with value 'V' and type 'type'. 1027 /// \param type - either IntTy, LongTy, LongLongTy, UnsignedIntTy, 1028 /// UnsignedLongTy, or UnsignedLongLongTy which should match the size of V 1029 /// \param V - the value that the returned integer literal contains. 1030 static IntegerLiteral *Create(ASTContext &C, const llvm::APInt &V, 1031 QualType type, SourceLocation l); 1032 /// \brief Returns a new empty integer literal. 1033 static IntegerLiteral *Create(ASTContext &C, EmptyShell Empty); 1034 1035 llvm::APInt getValue() const { return Num.getValue(); } 1036 SourceRange getSourceRange() const { return SourceRange(Loc); } 1037 1038 /// \brief Retrieve the location of the literal. 1039 SourceLocation getLocation() const { return Loc; } 1040 1041 void setValue(ASTContext &C, const llvm::APInt &Val) { Num.setValue(C, Val); } 1042 void setLocation(SourceLocation Location) { Loc = Location; } 1043 1044 static bool classof(const Stmt *T) { 1045 return T->getStmtClass() == IntegerLiteralClass; 1046 } 1047 static bool classof(const IntegerLiteral *) { return true; } 1048 1049 // Iterators 1050 child_range children() { return child_range(); } 1051}; 1052 1053class CharacterLiteral : public Expr { 1054 unsigned Value; 1055 SourceLocation Loc; 1056 bool IsWide; 1057public: 1058 // type should be IntTy 1059 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 1060 : Expr(CharacterLiteralClass, type, VK_RValue, OK_Ordinary, false, false, 1061 false), 1062 Value(value), Loc(l), IsWide(iswide) { 1063 } 1064 1065 /// \brief Construct an empty character literal. 1066 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 1067 1068 SourceLocation getLocation() const { return Loc; } 1069 bool isWide() const { return IsWide; } 1070 1071 SourceRange getSourceRange() const { return SourceRange(Loc); } 1072 1073 unsigned getValue() const { return Value; } 1074 1075 void setLocation(SourceLocation Location) { Loc = Location; } 1076 void setWide(bool W) { IsWide = W; } 1077 void setValue(unsigned Val) { Value = Val; } 1078 1079 static bool classof(const Stmt *T) { 1080 return T->getStmtClass() == CharacterLiteralClass; 1081 } 1082 static bool classof(const CharacterLiteral *) { return true; } 1083 1084 // Iterators 1085 child_range children() { return child_range(); } 1086}; 1087 1088class FloatingLiteral : public Expr { 1089 APFloatStorage Num; 1090 bool IsExact : 1; 1091 SourceLocation Loc; 1092 1093 FloatingLiteral(ASTContext &C, const llvm::APFloat &V, bool isexact, 1094 QualType Type, SourceLocation L) 1095 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false, 1096 false), 1097 IsExact(isexact), Loc(L) { 1098 setValue(C, V); 1099 } 1100 1101 /// \brief Construct an empty floating-point literal. 1102 explicit FloatingLiteral(EmptyShell Empty) 1103 : Expr(FloatingLiteralClass, Empty), IsExact(false) { } 1104 1105public: 1106 static FloatingLiteral *Create(ASTContext &C, const llvm::APFloat &V, 1107 bool isexact, QualType Type, SourceLocation L); 1108 static FloatingLiteral *Create(ASTContext &C, EmptyShell Empty); 1109 1110 llvm::APFloat getValue() const { return Num.getValue(); } 1111 void setValue(ASTContext &C, const llvm::APFloat &Val) { 1112 Num.setValue(C, Val); 1113 } 1114 1115 bool isExact() const { return IsExact; } 1116 void setExact(bool E) { IsExact = E; } 1117 1118 /// getValueAsApproximateDouble - This returns the value as an inaccurate 1119 /// double. Note that this may cause loss of precision, but is useful for 1120 /// debugging dumps, etc. 1121 double getValueAsApproximateDouble() const; 1122 1123 SourceLocation getLocation() const { return Loc; } 1124 void setLocation(SourceLocation L) { Loc = L; } 1125 1126 SourceRange getSourceRange() const { return SourceRange(Loc); } 1127 1128 static bool classof(const Stmt *T) { 1129 return T->getStmtClass() == FloatingLiteralClass; 1130 } 1131 static bool classof(const FloatingLiteral *) { return true; } 1132 1133 // Iterators 1134 child_range children() { return child_range(); } 1135}; 1136 1137/// ImaginaryLiteral - We support imaginary integer and floating point literals, 1138/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 1139/// IntegerLiteral classes. Instances of this class always have a Complex type 1140/// whose element type matches the subexpression. 1141/// 1142class ImaginaryLiteral : public Expr { 1143 Stmt *Val; 1144public: 1145 ImaginaryLiteral(Expr *val, QualType Ty) 1146 : Expr(ImaginaryLiteralClass, Ty, VK_RValue, OK_Ordinary, false, false, 1147 false), 1148 Val(val) {} 1149 1150 /// \brief Build an empty imaginary literal. 1151 explicit ImaginaryLiteral(EmptyShell Empty) 1152 : Expr(ImaginaryLiteralClass, Empty) { } 1153 1154 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1155 Expr *getSubExpr() { return cast<Expr>(Val); } 1156 void setSubExpr(Expr *E) { Val = E; } 1157 1158 SourceRange getSourceRange() const { return Val->getSourceRange(); } 1159 static bool classof(const Stmt *T) { 1160 return T->getStmtClass() == ImaginaryLiteralClass; 1161 } 1162 static bool classof(const ImaginaryLiteral *) { return true; } 1163 1164 // Iterators 1165 child_range children() { return child_range(&Val, &Val+1); } 1166}; 1167 1168/// StringLiteral - This represents a string literal expression, e.g. "foo" 1169/// or L"bar" (wide strings). The actual string is returned by getStrData() 1170/// is NOT null-terminated, and the length of the string is determined by 1171/// calling getByteLength(). The C type for a string is always a 1172/// ConstantArrayType. In C++, the char type is const qualified, in C it is 1173/// not. 1174/// 1175/// Note that strings in C can be formed by concatenation of multiple string 1176/// literal pptokens in translation phase #6. This keeps track of the locations 1177/// of each of these pieces. 1178/// 1179/// Strings in C can also be truncated and extended by assigning into arrays, 1180/// e.g. with constructs like: 1181/// char X[2] = "foobar"; 1182/// In this case, getByteLength() will return 6, but the string literal will 1183/// have type "char[2]". 1184class StringLiteral : public Expr { 1185 friend class ASTStmtReader; 1186 1187 const char *StrData; 1188 unsigned ByteLength; 1189 bool IsWide; 1190 bool IsPascal; 1191 unsigned NumConcatenated; 1192 SourceLocation TokLocs[1]; 1193 1194 StringLiteral(QualType Ty) : 1195 Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary, false, false, false) {} 1196 1197public: 1198 /// This is the "fully general" constructor that allows representation of 1199 /// strings formed from multiple concatenated tokens. 1200 static StringLiteral *Create(ASTContext &C, const char *StrData, 1201 unsigned ByteLength, bool Wide, bool Pascal, 1202 QualType Ty, 1203 const SourceLocation *Loc, unsigned NumStrs); 1204 1205 /// Simple constructor for string literals made from one token. 1206 static StringLiteral *Create(ASTContext &C, const char *StrData, 1207 unsigned ByteLength, bool Wide, 1208 bool Pascal, QualType Ty, SourceLocation Loc) { 1209 return Create(C, StrData, ByteLength, Wide, Pascal, Ty, &Loc, 1); 1210 } 1211 1212 /// \brief Construct an empty string literal. 1213 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 1214 1215 llvm::StringRef getString() const { 1216 return llvm::StringRef(StrData, ByteLength); 1217 } 1218 1219 unsigned getByteLength() const { return ByteLength; } 1220 1221 /// \brief Sets the string data to the given string data. 1222 void setString(ASTContext &C, llvm::StringRef Str); 1223 1224 bool isWide() const { return IsWide; } 1225 bool isPascal() const { return IsPascal; } 1226 1227 bool containsNonAsciiOrNull() const { 1228 llvm::StringRef Str = getString(); 1229 for (unsigned i = 0, e = Str.size(); i != e; ++i) 1230 if (!isascii(Str[i]) || !Str[i]) 1231 return true; 1232 return false; 1233 } 1234 /// getNumConcatenated - Get the number of string literal tokens that were 1235 /// concatenated in translation phase #6 to form this string literal. 1236 unsigned getNumConcatenated() const { return NumConcatenated; } 1237 1238 SourceLocation getStrTokenLoc(unsigned TokNum) const { 1239 assert(TokNum < NumConcatenated && "Invalid tok number"); 1240 return TokLocs[TokNum]; 1241 } 1242 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 1243 assert(TokNum < NumConcatenated && "Invalid tok number"); 1244 TokLocs[TokNum] = L; 1245 } 1246 1247 /// getLocationOfByte - Return a source location that points to the specified 1248 /// byte of this string literal. 1249 /// 1250 /// Strings are amazingly complex. They can be formed from multiple tokens 1251 /// and can have escape sequences in them in addition to the usual trigraph 1252 /// and escaped newline business. This routine handles this complexity. 1253 /// 1254 SourceLocation getLocationOfByte(unsigned ByteNo, const SourceManager &SM, 1255 const LangOptions &Features, 1256 const TargetInfo &Target) const; 1257 1258 typedef const SourceLocation *tokloc_iterator; 1259 tokloc_iterator tokloc_begin() const { return TokLocs; } 1260 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 1261 1262 SourceRange getSourceRange() const { 1263 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 1264 } 1265 static bool classof(const Stmt *T) { 1266 return T->getStmtClass() == StringLiteralClass; 1267 } 1268 static bool classof(const StringLiteral *) { return true; } 1269 1270 // Iterators 1271 child_range children() { return child_range(); } 1272}; 1273 1274/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 1275/// AST node is only formed if full location information is requested. 1276class ParenExpr : public Expr { 1277 SourceLocation L, R; 1278 Stmt *Val; 1279public: 1280 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 1281 : Expr(ParenExprClass, val->getType(), 1282 val->getValueKind(), val->getObjectKind(), 1283 val->isTypeDependent(), val->isValueDependent(), 1284 val->containsUnexpandedParameterPack()), 1285 L(l), R(r), Val(val) {} 1286 1287 /// \brief Construct an empty parenthesized expression. 1288 explicit ParenExpr(EmptyShell Empty) 1289 : Expr(ParenExprClass, Empty) { } 1290 1291 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1292 Expr *getSubExpr() { return cast<Expr>(Val); } 1293 void setSubExpr(Expr *E) { Val = E; } 1294 1295 SourceRange getSourceRange() const { return SourceRange(L, R); } 1296 1297 /// \brief Get the location of the left parentheses '('. 1298 SourceLocation getLParen() const { return L; } 1299 void setLParen(SourceLocation Loc) { L = Loc; } 1300 1301 /// \brief Get the location of the right parentheses ')'. 1302 SourceLocation getRParen() const { return R; } 1303 void setRParen(SourceLocation Loc) { R = Loc; } 1304 1305 static bool classof(const Stmt *T) { 1306 return T->getStmtClass() == ParenExprClass; 1307 } 1308 static bool classof(const ParenExpr *) { return true; } 1309 1310 // Iterators 1311 child_range children() { return child_range(&Val, &Val+1); } 1312}; 1313 1314 1315/// UnaryOperator - This represents the unary-expression's (except sizeof and 1316/// alignof), the postinc/postdec operators from postfix-expression, and various 1317/// extensions. 1318/// 1319/// Notes on various nodes: 1320/// 1321/// Real/Imag - These return the real/imag part of a complex operand. If 1322/// applied to a non-complex value, the former returns its operand and the 1323/// later returns zero in the type of the operand. 1324/// 1325class UnaryOperator : public Expr { 1326public: 1327 typedef UnaryOperatorKind Opcode; 1328 1329private: 1330 unsigned Opc : 5; 1331 SourceLocation Loc; 1332 Stmt *Val; 1333public: 1334 1335 UnaryOperator(Expr *input, Opcode opc, QualType type, 1336 ExprValueKind VK, ExprObjectKind OK, SourceLocation l) 1337 : Expr(UnaryOperatorClass, type, VK, OK, 1338 input->isTypeDependent() || type->isDependentType(), 1339 input->isValueDependent(), 1340 input->containsUnexpandedParameterPack()), 1341 Opc(opc), Loc(l), Val(input) {} 1342 1343 /// \brief Build an empty unary operator. 1344 explicit UnaryOperator(EmptyShell Empty) 1345 : Expr(UnaryOperatorClass, Empty), Opc(UO_AddrOf) { } 1346 1347 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 1348 void setOpcode(Opcode O) { Opc = O; } 1349 1350 Expr *getSubExpr() const { return cast<Expr>(Val); } 1351 void setSubExpr(Expr *E) { Val = E; } 1352 1353 /// getOperatorLoc - Return the location of the operator. 1354 SourceLocation getOperatorLoc() const { return Loc; } 1355 void setOperatorLoc(SourceLocation L) { Loc = L; } 1356 1357 /// isPostfix - Return true if this is a postfix operation, like x++. 1358 static bool isPostfix(Opcode Op) { 1359 return Op == UO_PostInc || Op == UO_PostDec; 1360 } 1361 1362 /// isPrefix - Return true if this is a prefix operation, like --x. 1363 static bool isPrefix(Opcode Op) { 1364 return Op == UO_PreInc || Op == UO_PreDec; 1365 } 1366 1367 bool isPrefix() const { return isPrefix(getOpcode()); } 1368 bool isPostfix() const { return isPostfix(getOpcode()); } 1369 bool isIncrementOp() const { 1370 return Opc == UO_PreInc || Opc == UO_PostInc; 1371 } 1372 bool isIncrementDecrementOp() const { 1373 return Opc <= UO_PreDec; 1374 } 1375 static bool isArithmeticOp(Opcode Op) { 1376 return Op >= UO_Plus && Op <= UO_LNot; 1377 } 1378 bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); } 1379 1380 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1381 /// corresponds to, e.g. "sizeof" or "[pre]++" 1382 static const char *getOpcodeStr(Opcode Op); 1383 1384 /// \brief Retrieve the unary opcode that corresponds to the given 1385 /// overloaded operator. 1386 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 1387 1388 /// \brief Retrieve the overloaded operator kind that corresponds to 1389 /// the given unary opcode. 1390 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1391 1392 SourceRange getSourceRange() const { 1393 if (isPostfix()) 1394 return SourceRange(Val->getLocStart(), Loc); 1395 else 1396 return SourceRange(Loc, Val->getLocEnd()); 1397 } 1398 SourceLocation getExprLoc() const { return Loc; } 1399 1400 static bool classof(const Stmt *T) { 1401 return T->getStmtClass() == UnaryOperatorClass; 1402 } 1403 static bool classof(const UnaryOperator *) { return true; } 1404 1405 // Iterators 1406 child_range children() { return child_range(&Val, &Val+1); } 1407}; 1408 1409/// OffsetOfExpr - [C99 7.17] - This represents an expression of the form 1410/// offsetof(record-type, member-designator). For example, given: 1411/// @code 1412/// struct S { 1413/// float f; 1414/// double d; 1415/// }; 1416/// struct T { 1417/// int i; 1418/// struct S s[10]; 1419/// }; 1420/// @endcode 1421/// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). 1422 1423class OffsetOfExpr : public Expr { 1424public: 1425 // __builtin_offsetof(type, identifier(.identifier|[expr])*) 1426 class OffsetOfNode { 1427 public: 1428 /// \brief The kind of offsetof node we have. 1429 enum Kind { 1430 /// \brief An index into an array. 1431 Array = 0x00, 1432 /// \brief A field. 1433 Field = 0x01, 1434 /// \brief A field in a dependent type, known only by its name. 1435 Identifier = 0x02, 1436 /// \brief An implicit indirection through a C++ base class, when the 1437 /// field found is in a base class. 1438 Base = 0x03 1439 }; 1440 1441 private: 1442 enum { MaskBits = 2, Mask = 0x03 }; 1443 1444 /// \brief The source range that covers this part of the designator. 1445 SourceRange Range; 1446 1447 /// \brief The data describing the designator, which comes in three 1448 /// different forms, depending on the lower two bits. 1449 /// - An unsigned index into the array of Expr*'s stored after this node 1450 /// in memory, for [constant-expression] designators. 1451 /// - A FieldDecl*, for references to a known field. 1452 /// - An IdentifierInfo*, for references to a field with a given name 1453 /// when the class type is dependent. 1454 /// - A CXXBaseSpecifier*, for references that look at a field in a 1455 /// base class. 1456 uintptr_t Data; 1457 1458 public: 1459 /// \brief Create an offsetof node that refers to an array element. 1460 OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, 1461 SourceLocation RBracketLoc) 1462 : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { } 1463 1464 /// \brief Create an offsetof node that refers to a field. 1465 OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, 1466 SourceLocation NameLoc) 1467 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1468 Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { } 1469 1470 /// \brief Create an offsetof node that refers to an identifier. 1471 OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, 1472 SourceLocation NameLoc) 1473 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1474 Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { } 1475 1476 /// \brief Create an offsetof node that refers into a C++ base class. 1477 explicit OffsetOfNode(const CXXBaseSpecifier *Base) 1478 : Range(), Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} 1479 1480 /// \brief Determine what kind of offsetof node this is. 1481 Kind getKind() const { 1482 return static_cast<Kind>(Data & Mask); 1483 } 1484 1485 /// \brief For an array element node, returns the index into the array 1486 /// of expressions. 1487 unsigned getArrayExprIndex() const { 1488 assert(getKind() == Array); 1489 return Data >> 2; 1490 } 1491 1492 /// \brief For a field offsetof node, returns the field. 1493 FieldDecl *getField() const { 1494 assert(getKind() == Field); 1495 return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); 1496 } 1497 1498 /// \brief For a field or identifier offsetof node, returns the name of 1499 /// the field. 1500 IdentifierInfo *getFieldName() const; 1501 1502 /// \brief For a base class node, returns the base specifier. 1503 CXXBaseSpecifier *getBase() const { 1504 assert(getKind() == Base); 1505 return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); 1506 } 1507 1508 /// \brief Retrieve the source range that covers this offsetof node. 1509 /// 1510 /// For an array element node, the source range contains the locations of 1511 /// the square brackets. For a field or identifier node, the source range 1512 /// contains the location of the period (if there is one) and the 1513 /// identifier. 1514 SourceRange getSourceRange() const { return Range; } 1515 }; 1516 1517private: 1518 1519 SourceLocation OperatorLoc, RParenLoc; 1520 // Base type; 1521 TypeSourceInfo *TSInfo; 1522 // Number of sub-components (i.e. instances of OffsetOfNode). 1523 unsigned NumComps; 1524 // Number of sub-expressions (i.e. array subscript expressions). 1525 unsigned NumExprs; 1526 1527 OffsetOfExpr(ASTContext &C, QualType type, 1528 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1529 OffsetOfNode* compsPtr, unsigned numComps, 1530 Expr** exprsPtr, unsigned numExprs, 1531 SourceLocation RParenLoc); 1532 1533 explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) 1534 : Expr(OffsetOfExprClass, EmptyShell()), 1535 TSInfo(0), NumComps(numComps), NumExprs(numExprs) {} 1536 1537public: 1538 1539 static OffsetOfExpr *Create(ASTContext &C, QualType type, 1540 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1541 OffsetOfNode* compsPtr, unsigned numComps, 1542 Expr** exprsPtr, unsigned numExprs, 1543 SourceLocation RParenLoc); 1544 1545 static OffsetOfExpr *CreateEmpty(ASTContext &C, 1546 unsigned NumComps, unsigned NumExprs); 1547 1548 /// getOperatorLoc - Return the location of the operator. 1549 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1550 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1551 1552 /// \brief Return the location of the right parentheses. 1553 SourceLocation getRParenLoc() const { return RParenLoc; } 1554 void setRParenLoc(SourceLocation R) { RParenLoc = R; } 1555 1556 TypeSourceInfo *getTypeSourceInfo() const { 1557 return TSInfo; 1558 } 1559 void setTypeSourceInfo(TypeSourceInfo *tsi) { 1560 TSInfo = tsi; 1561 } 1562 1563 const OffsetOfNode &getComponent(unsigned Idx) { 1564 assert(Idx < NumComps && "Subscript out of range"); 1565 return reinterpret_cast<OffsetOfNode *> (this + 1)[Idx]; 1566 } 1567 1568 void setComponent(unsigned Idx, OffsetOfNode ON) { 1569 assert(Idx < NumComps && "Subscript out of range"); 1570 reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON; 1571 } 1572 1573 unsigned getNumComponents() const { 1574 return NumComps; 1575 } 1576 1577 Expr* getIndexExpr(unsigned Idx) { 1578 assert(Idx < NumExprs && "Subscript out of range"); 1579 return reinterpret_cast<Expr **>( 1580 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx]; 1581 } 1582 1583 void setIndexExpr(unsigned Idx, Expr* E) { 1584 assert(Idx < NumComps && "Subscript out of range"); 1585 reinterpret_cast<Expr **>( 1586 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E; 1587 } 1588 1589 unsigned getNumExpressions() const { 1590 return NumExprs; 1591 } 1592 1593 SourceRange getSourceRange() const { 1594 return SourceRange(OperatorLoc, RParenLoc); 1595 } 1596 1597 static bool classof(const Stmt *T) { 1598 return T->getStmtClass() == OffsetOfExprClass; 1599 } 1600 1601 static bool classof(const OffsetOfExpr *) { return true; } 1602 1603 // Iterators 1604 child_range children() { 1605 Stmt **begin = 1606 reinterpret_cast<Stmt**>(reinterpret_cast<OffsetOfNode*>(this + 1) 1607 + NumComps); 1608 return child_range(begin, begin + NumExprs); 1609 } 1610}; 1611 1612/// UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) 1613/// expression operand. Used for sizeof/alignof (C99 6.5.3.4) and 1614/// vec_step (OpenCL 1.1 6.11.12). 1615class UnaryExprOrTypeTraitExpr : public Expr { 1616 unsigned Kind : 2; 1617 bool isType : 1; // true if operand is a type, false if an expression 1618 union { 1619 TypeSourceInfo *Ty; 1620 Stmt *Ex; 1621 } Argument; 1622 SourceLocation OpLoc, RParenLoc; 1623 1624public: 1625 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, TypeSourceInfo *TInfo, 1626 QualType resultType, SourceLocation op, 1627 SourceLocation rp) : 1628 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 1629 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1630 // Value-dependent if the argument is type-dependent. 1631 TInfo->getType()->isDependentType(), 1632 TInfo->getType()->containsUnexpandedParameterPack()), 1633 Kind(ExprKind), isType(true), OpLoc(op), RParenLoc(rp) { 1634 Argument.Ty = TInfo; 1635 } 1636 1637 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, Expr *E, 1638 QualType resultType, SourceLocation op, 1639 SourceLocation rp) : 1640 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 1641 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1642 // Value-dependent if the argument is type-dependent. 1643 E->isTypeDependent(), 1644 E->containsUnexpandedParameterPack()), 1645 Kind(ExprKind), isType(false), OpLoc(op), RParenLoc(rp) { 1646 Argument.Ex = E; 1647 } 1648 1649 /// \brief Construct an empty sizeof/alignof expression. 1650 explicit UnaryExprOrTypeTraitExpr(EmptyShell Empty) 1651 : Expr(UnaryExprOrTypeTraitExprClass, Empty) { } 1652 1653 UnaryExprOrTypeTrait getKind() const { 1654 return static_cast<UnaryExprOrTypeTrait>(Kind); 1655 } 1656 void setKind(UnaryExprOrTypeTrait K) { Kind = K; } 1657 1658 bool isArgumentType() const { return isType; } 1659 QualType getArgumentType() const { 1660 return getArgumentTypeInfo()->getType(); 1661 } 1662 TypeSourceInfo *getArgumentTypeInfo() const { 1663 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 1664 return Argument.Ty; 1665 } 1666 Expr *getArgumentExpr() { 1667 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 1668 return static_cast<Expr*>(Argument.Ex); 1669 } 1670 const Expr *getArgumentExpr() const { 1671 return const_cast<UnaryExprOrTypeTraitExpr*>(this)->getArgumentExpr(); 1672 } 1673 1674 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 1675 void setArgument(TypeSourceInfo *TInfo) { 1676 Argument.Ty = TInfo; 1677 isType = true; 1678 } 1679 1680 /// Gets the argument type, or the type of the argument expression, whichever 1681 /// is appropriate. 1682 QualType getTypeOfArgument() const { 1683 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 1684 } 1685 1686 SourceLocation getOperatorLoc() const { return OpLoc; } 1687 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1688 1689 SourceLocation getRParenLoc() const { return RParenLoc; } 1690 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1691 1692 SourceRange getSourceRange() const { 1693 return SourceRange(OpLoc, RParenLoc); 1694 } 1695 1696 static bool classof(const Stmt *T) { 1697 return T->getStmtClass() == UnaryExprOrTypeTraitExprClass; 1698 } 1699 static bool classof(const UnaryExprOrTypeTraitExpr *) { return true; } 1700 1701 // Iterators 1702 child_range children(); 1703}; 1704 1705//===----------------------------------------------------------------------===// 1706// Postfix Operators. 1707//===----------------------------------------------------------------------===// 1708 1709/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 1710class ArraySubscriptExpr : public Expr { 1711 enum { LHS, RHS, END_EXPR=2 }; 1712 Stmt* SubExprs[END_EXPR]; 1713 SourceLocation RBracketLoc; 1714public: 1715 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 1716 ExprValueKind VK, ExprObjectKind OK, 1717 SourceLocation rbracketloc) 1718 : Expr(ArraySubscriptExprClass, t, VK, OK, 1719 lhs->isTypeDependent() || rhs->isTypeDependent(), 1720 lhs->isValueDependent() || rhs->isValueDependent(), 1721 (lhs->containsUnexpandedParameterPack() || 1722 rhs->containsUnexpandedParameterPack())), 1723 RBracketLoc(rbracketloc) { 1724 SubExprs[LHS] = lhs; 1725 SubExprs[RHS] = rhs; 1726 } 1727 1728 /// \brief Create an empty array subscript expression. 1729 explicit ArraySubscriptExpr(EmptyShell Shell) 1730 : Expr(ArraySubscriptExprClass, Shell) { } 1731 1732 /// An array access can be written A[4] or 4[A] (both are equivalent). 1733 /// - getBase() and getIdx() always present the normalized view: A[4]. 1734 /// In this case getBase() returns "A" and getIdx() returns "4". 1735 /// - getLHS() and getRHS() present the syntactic view. e.g. for 1736 /// 4[A] getLHS() returns "4". 1737 /// Note: Because vector element access is also written A[4] we must 1738 /// predicate the format conversion in getBase and getIdx only on the 1739 /// the type of the RHS, as it is possible for the LHS to be a vector of 1740 /// integer type 1741 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 1742 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1743 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1744 1745 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 1746 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1747 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1748 1749 Expr *getBase() { 1750 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1751 } 1752 1753 const Expr *getBase() const { 1754 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1755 } 1756 1757 Expr *getIdx() { 1758 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1759 } 1760 1761 const Expr *getIdx() const { 1762 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1763 } 1764 1765 SourceRange getSourceRange() const { 1766 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 1767 } 1768 1769 SourceLocation getRBracketLoc() const { return RBracketLoc; } 1770 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 1771 1772 SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 1773 1774 static bool classof(const Stmt *T) { 1775 return T->getStmtClass() == ArraySubscriptExprClass; 1776 } 1777 static bool classof(const ArraySubscriptExpr *) { return true; } 1778 1779 // Iterators 1780 child_range children() { 1781 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1782 } 1783}; 1784 1785 1786/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 1787/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 1788/// while its subclasses may represent alternative syntax that (semantically) 1789/// results in a function call. For example, CXXOperatorCallExpr is 1790/// a subclass for overloaded operator calls that use operator syntax, e.g., 1791/// "str1 + str2" to resolve to a function call. 1792class CallExpr : public Expr { 1793 enum { FN=0, PREARGS_START=1 }; 1794 Stmt **SubExprs; 1795 unsigned NumArgs; 1796 SourceLocation RParenLoc; 1797 1798protected: 1799 // These versions of the constructor are for derived classes. 1800 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs, 1801 Expr **args, unsigned numargs, QualType t, ExprValueKind VK, 1802 SourceLocation rparenloc); 1803 CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs, EmptyShell Empty); 1804 1805 Stmt *getPreArg(unsigned i) { 1806 assert(i < getNumPreArgs() && "Prearg access out of range!"); 1807 return SubExprs[PREARGS_START+i]; 1808 } 1809 const Stmt *getPreArg(unsigned i) const { 1810 assert(i < getNumPreArgs() && "Prearg access out of range!"); 1811 return SubExprs[PREARGS_START+i]; 1812 } 1813 void setPreArg(unsigned i, Stmt *PreArg) { 1814 assert(i < getNumPreArgs() && "Prearg access out of range!"); 1815 SubExprs[PREARGS_START+i] = PreArg; 1816 } 1817 1818 unsigned getNumPreArgs() const { return CallExprBits.NumPreArgs; } 1819 1820public: 1821 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 1822 ExprValueKind VK, SourceLocation rparenloc); 1823 1824 /// \brief Build an empty call expression. 1825 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 1826 1827 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 1828 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 1829 void setCallee(Expr *F) { SubExprs[FN] = F; } 1830 1831 Decl *getCalleeDecl(); 1832 const Decl *getCalleeDecl() const { 1833 return const_cast<CallExpr*>(this)->getCalleeDecl(); 1834 } 1835 1836 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 1837 FunctionDecl *getDirectCallee(); 1838 const FunctionDecl *getDirectCallee() const { 1839 return const_cast<CallExpr*>(this)->getDirectCallee(); 1840 } 1841 1842 /// getNumArgs - Return the number of actual arguments to this call. 1843 /// 1844 unsigned getNumArgs() const { return NumArgs; } 1845 1846 /// \brief Retrieve the call arguments. 1847 Expr **getArgs() { 1848 return reinterpret_cast<Expr **>(SubExprs+getNumPreArgs()+PREARGS_START); 1849 } 1850 1851 /// getArg - Return the specified argument. 1852 Expr *getArg(unsigned Arg) { 1853 assert(Arg < NumArgs && "Arg access out of range!"); 1854 return cast<Expr>(SubExprs[Arg+getNumPreArgs()+PREARGS_START]); 1855 } 1856 const Expr *getArg(unsigned Arg) const { 1857 assert(Arg < NumArgs && "Arg access out of range!"); 1858 return cast<Expr>(SubExprs[Arg+getNumPreArgs()+PREARGS_START]); 1859 } 1860 1861 /// setArg - Set the specified argument. 1862 void setArg(unsigned Arg, Expr *ArgExpr) { 1863 assert(Arg < NumArgs && "Arg access out of range!"); 1864 SubExprs[Arg+getNumPreArgs()+PREARGS_START] = ArgExpr; 1865 } 1866 1867 /// setNumArgs - This changes the number of arguments present in this call. 1868 /// Any orphaned expressions are deleted by this, and any new operands are set 1869 /// to null. 1870 void setNumArgs(ASTContext& C, unsigned NumArgs); 1871 1872 typedef ExprIterator arg_iterator; 1873 typedef ConstExprIterator const_arg_iterator; 1874 1875 arg_iterator arg_begin() { return SubExprs+PREARGS_START+getNumPreArgs(); } 1876 arg_iterator arg_end() { 1877 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 1878 } 1879 const_arg_iterator arg_begin() const { 1880 return SubExprs+PREARGS_START+getNumPreArgs(); 1881 } 1882 const_arg_iterator arg_end() const { 1883 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 1884 } 1885 1886 /// getNumCommas - Return the number of commas that must have been present in 1887 /// this function call. 1888 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1889 1890 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1891 /// not, return 0. 1892 unsigned isBuiltinCall(const ASTContext &Context) const; 1893 1894 /// getCallReturnType - Get the return type of the call expr. This is not 1895 /// always the type of the expr itself, if the return type is a reference 1896 /// type. 1897 QualType getCallReturnType() const; 1898 1899 SourceLocation getRParenLoc() const { return RParenLoc; } 1900 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1901 1902 SourceRange getSourceRange() const; 1903 1904 static bool classof(const Stmt *T) { 1905 return T->getStmtClass() >= firstCallExprConstant && 1906 T->getStmtClass() <= lastCallExprConstant; 1907 } 1908 static bool classof(const CallExpr *) { return true; } 1909 1910 // Iterators 1911 child_range children() { 1912 return child_range(&SubExprs[0], 1913 &SubExprs[0]+NumArgs+getNumPreArgs()+PREARGS_START); 1914 } 1915}; 1916 1917/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1918/// 1919class MemberExpr : public Expr { 1920 /// Extra data stored in some member expressions. 1921 struct MemberNameQualifier { 1922 /// \brief The nested-name-specifier that qualifies the name, including 1923 /// source-location information. 1924 NestedNameSpecifierLoc QualifierLoc; 1925 1926 /// \brief The DeclAccessPair through which the MemberDecl was found due to 1927 /// name qualifiers. 1928 DeclAccessPair FoundDecl; 1929 }; 1930 1931 /// Base - the expression for the base pointer or structure references. In 1932 /// X.F, this is "X". 1933 Stmt *Base; 1934 1935 /// MemberDecl - This is the decl being referenced by the field/member name. 1936 /// In X.F, this is the decl referenced by F. 1937 ValueDecl *MemberDecl; 1938 1939 /// MemberLoc - This is the location of the member name. 1940 SourceLocation MemberLoc; 1941 1942 /// MemberDNLoc - Provides source/type location info for the 1943 /// declaration name embedded in MemberDecl. 1944 DeclarationNameLoc MemberDNLoc; 1945 1946 /// IsArrow - True if this is "X->F", false if this is "X.F". 1947 bool IsArrow : 1; 1948 1949 /// \brief True if this member expression used a nested-name-specifier to 1950 /// refer to the member, e.g., "x->Base::f", or found its member via a using 1951 /// declaration. When true, a MemberNameQualifier 1952 /// structure is allocated immediately after the MemberExpr. 1953 bool HasQualifierOrFoundDecl : 1; 1954 1955 /// \brief True if this member expression specified a template argument list 1956 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1957 /// structure (and its TemplateArguments) are allocated immediately after 1958 /// the MemberExpr or, if the member expression also has a qualifier, after 1959 /// the MemberNameQualifier structure. 1960 bool HasExplicitTemplateArgumentList : 1; 1961 1962 /// \brief Retrieve the qualifier that preceded the member name, if any. 1963 MemberNameQualifier *getMemberQualifier() { 1964 assert(HasQualifierOrFoundDecl); 1965 return reinterpret_cast<MemberNameQualifier *> (this + 1); 1966 } 1967 1968 /// \brief Retrieve the qualifier that preceded the member name, if any. 1969 const MemberNameQualifier *getMemberQualifier() const { 1970 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1971 } 1972 1973public: 1974 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1975 const DeclarationNameInfo &NameInfo, QualType ty, 1976 ExprValueKind VK, ExprObjectKind OK) 1977 : Expr(MemberExprClass, ty, VK, OK, 1978 base->isTypeDependent(), base->isValueDependent(), 1979 base->containsUnexpandedParameterPack()), 1980 Base(base), MemberDecl(memberdecl), MemberLoc(NameInfo.getLoc()), 1981 MemberDNLoc(NameInfo.getInfo()), IsArrow(isarrow), 1982 HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) { 1983 assert(memberdecl->getDeclName() == NameInfo.getName()); 1984 } 1985 1986 // NOTE: this constructor should be used only when it is known that 1987 // the member name can not provide additional syntactic info 1988 // (i.e., source locations for C++ operator names or type source info 1989 // for constructors, destructors and conversion oeprators). 1990 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1991 SourceLocation l, QualType ty, 1992 ExprValueKind VK, ExprObjectKind OK) 1993 : Expr(MemberExprClass, ty, VK, OK, 1994 base->isTypeDependent(), base->isValueDependent(), 1995 base->containsUnexpandedParameterPack()), 1996 Base(base), MemberDecl(memberdecl), MemberLoc(l), MemberDNLoc(), 1997 IsArrow(isarrow), 1998 HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) {} 1999 2000 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 2001 NestedNameSpecifierLoc QualifierLoc, 2002 ValueDecl *memberdecl, DeclAccessPair founddecl, 2003 DeclarationNameInfo MemberNameInfo, 2004 const TemplateArgumentListInfo *targs, 2005 QualType ty, ExprValueKind VK, ExprObjectKind OK); 2006 2007 void setBase(Expr *E) { Base = E; } 2008 Expr *getBase() const { return cast<Expr>(Base); } 2009 2010 /// \brief Retrieve the member declaration to which this expression refers. 2011 /// 2012 /// The returned declaration will either be a FieldDecl or (in C++) 2013 /// a CXXMethodDecl. 2014 ValueDecl *getMemberDecl() const { return MemberDecl; } 2015 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 2016 2017 /// \brief Retrieves the declaration found by lookup. 2018 DeclAccessPair getFoundDecl() const { 2019 if (!HasQualifierOrFoundDecl) 2020 return DeclAccessPair::make(getMemberDecl(), 2021 getMemberDecl()->getAccess()); 2022 return getMemberQualifier()->FoundDecl; 2023 } 2024 2025 /// \brief Determines whether this member expression actually had 2026 /// a C++ nested-name-specifier prior to the name of the member, e.g., 2027 /// x->Base::foo. 2028 bool hasQualifier() const { return getQualifier() != 0; } 2029 2030 /// \brief If the member name was qualified, retrieves the 2031 /// nested-name-specifier that precedes the member name. Otherwise, returns 2032 /// NULL. 2033 NestedNameSpecifier *getQualifier() const { 2034 if (!HasQualifierOrFoundDecl) 2035 return 0; 2036 2037 return getMemberQualifier()->QualifierLoc.getNestedNameSpecifier(); 2038 } 2039 2040 /// \brief If the member name was qualified, retrieves the 2041 /// nested-name-specifier that precedes the member name, with source-location 2042 /// information. 2043 NestedNameSpecifierLoc getQualifierLoc() const { 2044 if (!hasQualifier()) 2045 return NestedNameSpecifierLoc(); 2046 2047 return getMemberQualifier()->QualifierLoc; 2048 } 2049 2050 /// \brief Determines whether this member expression actually had a C++ 2051 /// template argument list explicitly specified, e.g., x.f<int>. 2052 bool hasExplicitTemplateArgs() const { 2053 return HasExplicitTemplateArgumentList; 2054 } 2055 2056 /// \brief Copies the template arguments (if present) into the given 2057 /// structure. 2058 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2059 if (hasExplicitTemplateArgs()) 2060 getExplicitTemplateArgs().copyInto(List); 2061 } 2062 2063 /// \brief Retrieve the explicit template argument list that 2064 /// follow the member template name. This must only be called on an 2065 /// expression with explicit template arguments. 2066 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 2067 assert(HasExplicitTemplateArgumentList); 2068 if (!HasQualifierOrFoundDecl) 2069 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 2070 2071 return *reinterpret_cast<ExplicitTemplateArgumentList *>( 2072 getMemberQualifier() + 1); 2073 } 2074 2075 /// \brief Retrieve the explicit template argument list that 2076 /// followed the member template name. This must only be called on 2077 /// an expression with explicit template arguments. 2078 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 2079 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgs(); 2080 } 2081 2082 /// \brief Retrieves the optional explicit template arguments. 2083 /// This points to the same data as getExplicitTemplateArgs(), but 2084 /// returns null if there are no explicit template arguments. 2085 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() const { 2086 if (!hasExplicitTemplateArgs()) return 0; 2087 return &getExplicitTemplateArgs(); 2088 } 2089 2090 /// \brief Retrieve the location of the left angle bracket following the 2091 /// member name ('<'), if any. 2092 SourceLocation getLAngleLoc() const { 2093 if (!HasExplicitTemplateArgumentList) 2094 return SourceLocation(); 2095 2096 return getExplicitTemplateArgs().LAngleLoc; 2097 } 2098 2099 /// \brief Retrieve the template arguments provided as part of this 2100 /// template-id. 2101 const TemplateArgumentLoc *getTemplateArgs() const { 2102 if (!HasExplicitTemplateArgumentList) 2103 return 0; 2104 2105 return getExplicitTemplateArgs().getTemplateArgs(); 2106 } 2107 2108 /// \brief Retrieve the number of template arguments provided as part of this 2109 /// template-id. 2110 unsigned getNumTemplateArgs() const { 2111 if (!HasExplicitTemplateArgumentList) 2112 return 0; 2113 2114 return getExplicitTemplateArgs().NumTemplateArgs; 2115 } 2116 2117 /// \brief Retrieve the location of the right angle bracket following the 2118 /// template arguments ('>'). 2119 SourceLocation getRAngleLoc() const { 2120 if (!HasExplicitTemplateArgumentList) 2121 return SourceLocation(); 2122 2123 return getExplicitTemplateArgs().RAngleLoc; 2124 } 2125 2126 /// \brief Retrieve the member declaration name info. 2127 DeclarationNameInfo getMemberNameInfo() const { 2128 return DeclarationNameInfo(MemberDecl->getDeclName(), 2129 MemberLoc, MemberDNLoc); 2130 } 2131 2132 bool isArrow() const { return IsArrow; } 2133 void setArrow(bool A) { IsArrow = A; } 2134 2135 /// getMemberLoc - Return the location of the "member", in X->F, it is the 2136 /// location of 'F'. 2137 SourceLocation getMemberLoc() const { return MemberLoc; } 2138 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 2139 2140 SourceRange getSourceRange() const; 2141 2142 SourceLocation getExprLoc() const { return MemberLoc; } 2143 2144 /// \brief Determine whether the base of this explicit is implicit. 2145 bool isImplicitAccess() const { 2146 return getBase() && getBase()->isImplicitCXXThis(); 2147 } 2148 2149 static bool classof(const Stmt *T) { 2150 return T->getStmtClass() == MemberExprClass; 2151 } 2152 static bool classof(const MemberExpr *) { return true; } 2153 2154 // Iterators 2155 child_range children() { return child_range(&Base, &Base+1); } 2156 2157 friend class ASTReader; 2158 friend class ASTStmtWriter; 2159}; 2160 2161/// CompoundLiteralExpr - [C99 6.5.2.5] 2162/// 2163class CompoundLiteralExpr : public Expr { 2164 /// LParenLoc - If non-null, this is the location of the left paren in a 2165 /// compound literal like "(int){4}". This can be null if this is a 2166 /// synthesized compound expression. 2167 SourceLocation LParenLoc; 2168 2169 /// The type as written. This can be an incomplete array type, in 2170 /// which case the actual expression type will be different. 2171 TypeSourceInfo *TInfo; 2172 Stmt *Init; 2173 bool FileScope; 2174public: 2175 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, 2176 QualType T, ExprValueKind VK, Expr *init, bool fileScope) 2177 : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary, 2178 tinfo->getType()->isDependentType(), 2179 init->isValueDependent(), 2180 init->containsUnexpandedParameterPack()), 2181 LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {} 2182 2183 /// \brief Construct an empty compound literal. 2184 explicit CompoundLiteralExpr(EmptyShell Empty) 2185 : Expr(CompoundLiteralExprClass, Empty) { } 2186 2187 const Expr *getInitializer() const { return cast<Expr>(Init); } 2188 Expr *getInitializer() { return cast<Expr>(Init); } 2189 void setInitializer(Expr *E) { Init = E; } 2190 2191 bool isFileScope() const { return FileScope; } 2192 void setFileScope(bool FS) { FileScope = FS; } 2193 2194 SourceLocation getLParenLoc() const { return LParenLoc; } 2195 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2196 2197 TypeSourceInfo *getTypeSourceInfo() const { return TInfo; } 2198 void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; } 2199 2200 SourceRange getSourceRange() const { 2201 // FIXME: Init should never be null. 2202 if (!Init) 2203 return SourceRange(); 2204 if (LParenLoc.isInvalid()) 2205 return Init->getSourceRange(); 2206 return SourceRange(LParenLoc, Init->getLocEnd()); 2207 } 2208 2209 static bool classof(const Stmt *T) { 2210 return T->getStmtClass() == CompoundLiteralExprClass; 2211 } 2212 static bool classof(const CompoundLiteralExpr *) { return true; } 2213 2214 // Iterators 2215 child_range children() { return child_range(&Init, &Init+1); } 2216}; 2217 2218/// CastExpr - Base class for type casts, including both implicit 2219/// casts (ImplicitCastExpr) and explicit casts that have some 2220/// representation in the source code (ExplicitCastExpr's derived 2221/// classes). 2222class CastExpr : public Expr { 2223public: 2224 typedef clang::CastKind CastKind; 2225 2226private: 2227 Stmt *Op; 2228 2229 void CheckCastConsistency() const { 2230#ifndef NDEBUG 2231 switch (getCastKind()) { 2232 case CK_DerivedToBase: 2233 case CK_UncheckedDerivedToBase: 2234 case CK_DerivedToBaseMemberPointer: 2235 case CK_BaseToDerived: 2236 case CK_BaseToDerivedMemberPointer: 2237 assert(!path_empty() && "Cast kind should have a base path!"); 2238 break; 2239 2240 // These should not have an inheritance path. 2241 case CK_BitCast: 2242 case CK_Dynamic: 2243 case CK_ToUnion: 2244 case CK_ArrayToPointerDecay: 2245 case CK_FunctionToPointerDecay: 2246 case CK_NullToMemberPointer: 2247 case CK_NullToPointer: 2248 case CK_ConstructorConversion: 2249 case CK_IntegralToPointer: 2250 case CK_PointerToIntegral: 2251 case CK_ToVoid: 2252 case CK_VectorSplat: 2253 case CK_IntegralCast: 2254 case CK_IntegralToFloating: 2255 case CK_FloatingToIntegral: 2256 case CK_FloatingCast: 2257 case CK_AnyPointerToObjCPointerCast: 2258 case CK_AnyPointerToBlockPointerCast: 2259 case CK_ObjCObjectLValueCast: 2260 case CK_FloatingRealToComplex: 2261 case CK_FloatingComplexToReal: 2262 case CK_FloatingComplexCast: 2263 case CK_FloatingComplexToIntegralComplex: 2264 case CK_IntegralRealToComplex: 2265 case CK_IntegralComplexToReal: 2266 case CK_IntegralComplexCast: 2267 case CK_IntegralComplexToFloatingComplex: 2268 assert(!getType()->isBooleanType() && "unheralded conversion to bool"); 2269 // fallthrough to check for null base path 2270 2271 case CK_Dependent: 2272 case CK_LValueToRValue: 2273 case CK_GetObjCProperty: 2274 case CK_NoOp: 2275 case CK_PointerToBoolean: 2276 case CK_IntegralToBoolean: 2277 case CK_FloatingToBoolean: 2278 case CK_MemberPointerToBoolean: 2279 case CK_FloatingComplexToBoolean: 2280 case CK_IntegralComplexToBoolean: 2281 case CK_LValueBitCast: // -> bool& 2282 case CK_UserDefinedConversion: // operator bool() 2283 assert(path_empty() && "Cast kind should not have a base path!"); 2284 break; 2285 } 2286#endif 2287 } 2288 2289 const CXXBaseSpecifier * const *path_buffer() const { 2290 return const_cast<CastExpr*>(this)->path_buffer(); 2291 } 2292 CXXBaseSpecifier **path_buffer(); 2293 2294 void setBasePathSize(unsigned basePathSize) { 2295 CastExprBits.BasePathSize = basePathSize; 2296 assert(CastExprBits.BasePathSize == basePathSize && 2297 "basePathSize doesn't fit in bits of CastExprBits.BasePathSize!"); 2298 } 2299 2300protected: 2301 CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 2302 const CastKind kind, Expr *op, unsigned BasePathSize) : 2303 Expr(SC, ty, VK, OK_Ordinary, 2304 // Cast expressions are type-dependent if the type is 2305 // dependent (C++ [temp.dep.expr]p3). 2306 ty->isDependentType(), 2307 // Cast expressions are value-dependent if the type is 2308 // dependent or if the subexpression is value-dependent. 2309 ty->isDependentType() || (op && op->isValueDependent()), 2310 (ty->containsUnexpandedParameterPack() || 2311 op->containsUnexpandedParameterPack())), 2312 Op(op) { 2313 assert(kind != CK_Invalid && "creating cast with invalid cast kind"); 2314 CastExprBits.Kind = kind; 2315 setBasePathSize(BasePathSize); 2316 CheckCastConsistency(); 2317 } 2318 2319 /// \brief Construct an empty cast. 2320 CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize) 2321 : Expr(SC, Empty) { 2322 setBasePathSize(BasePathSize); 2323 } 2324 2325public: 2326 CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } 2327 void setCastKind(CastKind K) { CastExprBits.Kind = K; } 2328 const char *getCastKindName() const; 2329 2330 Expr *getSubExpr() { return cast<Expr>(Op); } 2331 const Expr *getSubExpr() const { return cast<Expr>(Op); } 2332 void setSubExpr(Expr *E) { Op = E; } 2333 2334 /// \brief Retrieve the cast subexpression as it was written in the source 2335 /// code, looking through any implicit casts or other intermediate nodes 2336 /// introduced by semantic analysis. 2337 Expr *getSubExprAsWritten(); 2338 const Expr *getSubExprAsWritten() const { 2339 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 2340 } 2341 2342 typedef CXXBaseSpecifier **path_iterator; 2343 typedef const CXXBaseSpecifier * const *path_const_iterator; 2344 bool path_empty() const { return CastExprBits.BasePathSize == 0; } 2345 unsigned path_size() const { return CastExprBits.BasePathSize; } 2346 path_iterator path_begin() { return path_buffer(); } 2347 path_iterator path_end() { return path_buffer() + path_size(); } 2348 path_const_iterator path_begin() const { return path_buffer(); } 2349 path_const_iterator path_end() const { return path_buffer() + path_size(); } 2350 2351 void setCastPath(const CXXCastPath &Path); 2352 2353 static bool classof(const Stmt *T) { 2354 return T->getStmtClass() >= firstCastExprConstant && 2355 T->getStmtClass() <= lastCastExprConstant; 2356 } 2357 static bool classof(const CastExpr *) { return true; } 2358 2359 // Iterators 2360 child_range children() { return child_range(&Op, &Op+1); } 2361}; 2362 2363/// ImplicitCastExpr - Allows us to explicitly represent implicit type 2364/// conversions, which have no direct representation in the original 2365/// source code. For example: converting T[]->T*, void f()->void 2366/// (*f)(), float->double, short->int, etc. 2367/// 2368/// In C, implicit casts always produce rvalues. However, in C++, an 2369/// implicit cast whose result is being bound to a reference will be 2370/// an lvalue or xvalue. For example: 2371/// 2372/// @code 2373/// class Base { }; 2374/// class Derived : public Base { }; 2375/// Derived &&ref(); 2376/// void f(Derived d) { 2377/// Base& b = d; // initializer is an ImplicitCastExpr 2378/// // to an lvalue of type Base 2379/// Base&& r = ref(); // initializer is an ImplicitCastExpr 2380/// // to an xvalue of type Base 2381/// } 2382/// @endcode 2383class ImplicitCastExpr : public CastExpr { 2384private: 2385 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 2386 unsigned BasePathLength, ExprValueKind VK) 2387 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength) { 2388 } 2389 2390 /// \brief Construct an empty implicit cast. 2391 explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize) 2392 : CastExpr(ImplicitCastExprClass, Shell, PathSize) { } 2393 2394public: 2395 enum OnStack_t { OnStack }; 2396 ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, 2397 ExprValueKind VK) 2398 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0) { 2399 } 2400 2401 static ImplicitCastExpr *Create(ASTContext &Context, QualType T, 2402 CastKind Kind, Expr *Operand, 2403 const CXXCastPath *BasePath, 2404 ExprValueKind Cat); 2405 2406 static ImplicitCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2407 2408 SourceRange getSourceRange() const { 2409 return getSubExpr()->getSourceRange(); 2410 } 2411 2412 static bool classof(const Stmt *T) { 2413 return T->getStmtClass() == ImplicitCastExprClass; 2414 } 2415 static bool classof(const ImplicitCastExpr *) { return true; } 2416}; 2417 2418/// ExplicitCastExpr - An explicit cast written in the source 2419/// code. 2420/// 2421/// This class is effectively an abstract class, because it provides 2422/// the basic representation of an explicitly-written cast without 2423/// specifying which kind of cast (C cast, functional cast, static 2424/// cast, etc.) was written; specific derived classes represent the 2425/// particular style of cast and its location information. 2426/// 2427/// Unlike implicit casts, explicit cast nodes have two different 2428/// types: the type that was written into the source code, and the 2429/// actual type of the expression as determined by semantic 2430/// analysis. These types may differ slightly. For example, in C++ one 2431/// can cast to a reference type, which indicates that the resulting 2432/// expression will be an lvalue or xvalue. The reference type, however, 2433/// will not be used as the type of the expression. 2434class ExplicitCastExpr : public CastExpr { 2435 /// TInfo - Source type info for the (written) type 2436 /// this expression is casting to. 2437 TypeSourceInfo *TInfo; 2438 2439protected: 2440 ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, 2441 CastKind kind, Expr *op, unsigned PathSize, 2442 TypeSourceInfo *writtenTy) 2443 : CastExpr(SC, exprTy, VK, kind, op, PathSize), TInfo(writtenTy) {} 2444 2445 /// \brief Construct an empty explicit cast. 2446 ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 2447 : CastExpr(SC, Shell, PathSize) { } 2448 2449public: 2450 /// getTypeInfoAsWritten - Returns the type source info for the type 2451 /// that this expression is casting to. 2452 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 2453 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 2454 2455 /// getTypeAsWritten - Returns the type that this expression is 2456 /// casting to, as written in the source code. 2457 QualType getTypeAsWritten() const { return TInfo->getType(); } 2458 2459 static bool classof(const Stmt *T) { 2460 return T->getStmtClass() >= firstExplicitCastExprConstant && 2461 T->getStmtClass() <= lastExplicitCastExprConstant; 2462 } 2463 static bool classof(const ExplicitCastExpr *) { return true; } 2464}; 2465 2466/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 2467/// cast in C++ (C++ [expr.cast]), which uses the syntax 2468/// (Type)expr. For example: @c (int)f. 2469class CStyleCastExpr : public ExplicitCastExpr { 2470 SourceLocation LPLoc; // the location of the left paren 2471 SourceLocation RPLoc; // the location of the right paren 2472 2473 CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, 2474 unsigned PathSize, TypeSourceInfo *writtenTy, 2475 SourceLocation l, SourceLocation r) 2476 : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, 2477 writtenTy), LPLoc(l), RPLoc(r) {} 2478 2479 /// \brief Construct an empty C-style explicit cast. 2480 explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize) 2481 : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { } 2482 2483public: 2484 static CStyleCastExpr *Create(ASTContext &Context, QualType T, 2485 ExprValueKind VK, CastKind K, 2486 Expr *Op, const CXXCastPath *BasePath, 2487 TypeSourceInfo *WrittenTy, SourceLocation L, 2488 SourceLocation R); 2489 2490 static CStyleCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize); 2491 2492 SourceLocation getLParenLoc() const { return LPLoc; } 2493 void setLParenLoc(SourceLocation L) { LPLoc = L; } 2494 2495 SourceLocation getRParenLoc() const { return RPLoc; } 2496 void setRParenLoc(SourceLocation L) { RPLoc = L; } 2497 2498 SourceRange getSourceRange() const { 2499 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 2500 } 2501 static bool classof(const Stmt *T) { 2502 return T->getStmtClass() == CStyleCastExprClass; 2503 } 2504 static bool classof(const CStyleCastExpr *) { return true; } 2505}; 2506 2507/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 2508/// 2509/// This expression node kind describes a builtin binary operation, 2510/// such as "x + y" for integer values "x" and "y". The operands will 2511/// already have been converted to appropriate types (e.g., by 2512/// performing promotions or conversions). 2513/// 2514/// In C++, where operators may be overloaded, a different kind of 2515/// expression node (CXXOperatorCallExpr) is used to express the 2516/// invocation of an overloaded operator with operator syntax. Within 2517/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 2518/// used to store an expression "x + y" depends on the subexpressions 2519/// for x and y. If neither x or y is type-dependent, and the "+" 2520/// operator resolves to a built-in operation, BinaryOperator will be 2521/// used to express the computation (x and y may still be 2522/// value-dependent). If either x or y is type-dependent, or if the 2523/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 2524/// be used to express the computation. 2525class BinaryOperator : public Expr { 2526public: 2527 typedef BinaryOperatorKind Opcode; 2528 2529private: 2530 unsigned Opc : 6; 2531 SourceLocation OpLoc; 2532 2533 enum { LHS, RHS, END_EXPR }; 2534 Stmt* SubExprs[END_EXPR]; 2535public: 2536 2537 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2538 ExprValueKind VK, ExprObjectKind OK, 2539 SourceLocation opLoc) 2540 : Expr(BinaryOperatorClass, ResTy, VK, OK, 2541 lhs->isTypeDependent() || rhs->isTypeDependent(), 2542 lhs->isValueDependent() || rhs->isValueDependent(), 2543 (lhs->containsUnexpandedParameterPack() || 2544 rhs->containsUnexpandedParameterPack())), 2545 Opc(opc), OpLoc(opLoc) { 2546 SubExprs[LHS] = lhs; 2547 SubExprs[RHS] = rhs; 2548 assert(!isCompoundAssignmentOp() && 2549 "Use ArithAssignBinaryOperator for compound assignments"); 2550 } 2551 2552 /// \brief Construct an empty binary operator. 2553 explicit BinaryOperator(EmptyShell Empty) 2554 : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { } 2555 2556 SourceLocation getOperatorLoc() const { return OpLoc; } 2557 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 2558 2559 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 2560 void setOpcode(Opcode O) { Opc = O; } 2561 2562 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2563 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2564 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2565 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2566 2567 SourceRange getSourceRange() const { 2568 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 2569 } 2570 2571 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 2572 /// corresponds to, e.g. "<<=". 2573 static const char *getOpcodeStr(Opcode Op); 2574 2575 const char *getOpcodeStr() const { return getOpcodeStr(getOpcode()); } 2576 2577 /// \brief Retrieve the binary opcode that corresponds to the given 2578 /// overloaded operator. 2579 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 2580 2581 /// \brief Retrieve the overloaded operator kind that corresponds to 2582 /// the given binary opcode. 2583 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 2584 2585 /// predicates to categorize the respective opcodes. 2586 bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; } 2587 bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; } 2588 static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } 2589 bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } 2590 static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } 2591 bool isShiftOp() const { return isShiftOp(getOpcode()); } 2592 2593 static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } 2594 bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } 2595 2596 static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } 2597 bool isRelationalOp() const { return isRelationalOp(getOpcode()); } 2598 2599 static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } 2600 bool isEqualityOp() const { return isEqualityOp(getOpcode()); } 2601 2602 static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; } 2603 bool isComparisonOp() const { return isComparisonOp(getOpcode()); } 2604 2605 static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } 2606 bool isLogicalOp() const { return isLogicalOp(getOpcode()); } 2607 2608 static bool isAssignmentOp(Opcode Opc) { 2609 return Opc >= BO_Assign && Opc <= BO_OrAssign; 2610 } 2611 bool isAssignmentOp() const { return isAssignmentOp(getOpcode()); } 2612 2613 static bool isCompoundAssignmentOp(Opcode Opc) { 2614 return Opc > BO_Assign && Opc <= BO_OrAssign; 2615 } 2616 bool isCompoundAssignmentOp() const { 2617 return isCompoundAssignmentOp(getOpcode()); 2618 } 2619 2620 static bool isShiftAssignOp(Opcode Opc) { 2621 return Opc == BO_ShlAssign || Opc == BO_ShrAssign; 2622 } 2623 bool isShiftAssignOp() const { 2624 return isShiftAssignOp(getOpcode()); 2625 } 2626 2627 static bool classof(const Stmt *S) { 2628 return S->getStmtClass() >= firstBinaryOperatorConstant && 2629 S->getStmtClass() <= lastBinaryOperatorConstant; 2630 } 2631 static bool classof(const BinaryOperator *) { return true; } 2632 2633 // Iterators 2634 child_range children() { 2635 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2636 } 2637 2638protected: 2639 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2640 ExprValueKind VK, ExprObjectKind OK, 2641 SourceLocation opLoc, bool dead) 2642 : Expr(CompoundAssignOperatorClass, ResTy, VK, OK, 2643 lhs->isTypeDependent() || rhs->isTypeDependent(), 2644 lhs->isValueDependent() || rhs->isValueDependent(), 2645 (lhs->containsUnexpandedParameterPack() || 2646 rhs->containsUnexpandedParameterPack())), 2647 Opc(opc), OpLoc(opLoc) { 2648 SubExprs[LHS] = lhs; 2649 SubExprs[RHS] = rhs; 2650 } 2651 2652 BinaryOperator(StmtClass SC, EmptyShell Empty) 2653 : Expr(SC, Empty), Opc(BO_MulAssign) { } 2654}; 2655 2656/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 2657/// track of the type the operation is performed in. Due to the semantics of 2658/// these operators, the operands are promoted, the aritmetic performed, an 2659/// implicit conversion back to the result type done, then the assignment takes 2660/// place. This captures the intermediate type which the computation is done 2661/// in. 2662class CompoundAssignOperator : public BinaryOperator { 2663 QualType ComputationLHSType; 2664 QualType ComputationResultType; 2665public: 2666 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResType, 2667 ExprValueKind VK, ExprObjectKind OK, 2668 QualType CompLHSType, QualType CompResultType, 2669 SourceLocation OpLoc) 2670 : BinaryOperator(lhs, rhs, opc, ResType, VK, OK, OpLoc, true), 2671 ComputationLHSType(CompLHSType), 2672 ComputationResultType(CompResultType) { 2673 assert(isCompoundAssignmentOp() && 2674 "Only should be used for compound assignments"); 2675 } 2676 2677 /// \brief Build an empty compound assignment operator expression. 2678 explicit CompoundAssignOperator(EmptyShell Empty) 2679 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 2680 2681 // The two computation types are the type the LHS is converted 2682 // to for the computation and the type of the result; the two are 2683 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 2684 QualType getComputationLHSType() const { return ComputationLHSType; } 2685 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 2686 2687 QualType getComputationResultType() const { return ComputationResultType; } 2688 void setComputationResultType(QualType T) { ComputationResultType = T; } 2689 2690 static bool classof(const CompoundAssignOperator *) { return true; } 2691 static bool classof(const Stmt *S) { 2692 return S->getStmtClass() == CompoundAssignOperatorClass; 2693 } 2694}; 2695 2696/// AbstractConditionalOperator - An abstract base class for 2697/// ConditionalOperator and BinaryConditionalOperator. 2698class AbstractConditionalOperator : public Expr { 2699 SourceLocation QuestionLoc, ColonLoc; 2700 friend class ASTStmtReader; 2701 2702protected: 2703 AbstractConditionalOperator(StmtClass SC, QualType T, 2704 ExprValueKind VK, ExprObjectKind OK, 2705 bool TD, bool VD, 2706 bool ContainsUnexpandedParameterPack, 2707 SourceLocation qloc, 2708 SourceLocation cloc) 2709 : Expr(SC, T, VK, OK, TD, VD, ContainsUnexpandedParameterPack), 2710 QuestionLoc(qloc), ColonLoc(cloc) {} 2711 2712 AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) 2713 : Expr(SC, Empty) { } 2714 2715public: 2716 // getCond - Return the expression representing the condition for 2717 // the ?: operator. 2718 Expr *getCond() const; 2719 2720 // getTrueExpr - Return the subexpression representing the value of 2721 // the expression if the condition evaluates to true. 2722 Expr *getTrueExpr() const; 2723 2724 // getFalseExpr - Return the subexpression representing the value of 2725 // the expression if the condition evaluates to false. This is 2726 // the same as getRHS. 2727 Expr *getFalseExpr() const; 2728 2729 SourceLocation getQuestionLoc() const { return QuestionLoc; } 2730 SourceLocation getColonLoc() const { return ColonLoc; } 2731 2732 static bool classof(const Stmt *T) { 2733 return T->getStmtClass() == ConditionalOperatorClass || 2734 T->getStmtClass() == BinaryConditionalOperatorClass; 2735 } 2736 static bool classof(const AbstractConditionalOperator *) { return true; } 2737}; 2738 2739/// ConditionalOperator - The ?: ternary operator. The GNU "missing 2740/// middle" extension is a BinaryConditionalOperator. 2741class ConditionalOperator : public AbstractConditionalOperator { 2742 enum { COND, LHS, RHS, END_EXPR }; 2743 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2744 2745 friend class ASTStmtReader; 2746public: 2747 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 2748 SourceLocation CLoc, Expr *rhs, 2749 QualType t, ExprValueKind VK, ExprObjectKind OK) 2750 : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, 2751 // FIXME: the type of the conditional operator doesn't 2752 // depend on the type of the conditional, but the standard 2753 // seems to imply that it could. File a bug! 2754 (lhs->isTypeDependent() || rhs->isTypeDependent()), 2755 (cond->isValueDependent() || lhs->isValueDependent() || 2756 rhs->isValueDependent()), 2757 (cond->containsUnexpandedParameterPack() || 2758 lhs->containsUnexpandedParameterPack() || 2759 rhs->containsUnexpandedParameterPack()), 2760 QLoc, CLoc) { 2761 SubExprs[COND] = cond; 2762 SubExprs[LHS] = lhs; 2763 SubExprs[RHS] = rhs; 2764 } 2765 2766 /// \brief Build an empty conditional operator. 2767 explicit ConditionalOperator(EmptyShell Empty) 2768 : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } 2769 2770 // getCond - Return the expression representing the condition for 2771 // the ?: operator. 2772 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2773 2774 // getTrueExpr - Return the subexpression representing the value of 2775 // the expression if the condition evaluates to true. 2776 Expr *getTrueExpr() const { return cast<Expr>(SubExprs[LHS]); } 2777 2778 // getFalseExpr - Return the subexpression representing the value of 2779 // the expression if the condition evaluates to false. This is 2780 // the same as getRHS. 2781 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 2782 2783 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2784 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2785 2786 SourceRange getSourceRange() const { 2787 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 2788 } 2789 static bool classof(const Stmt *T) { 2790 return T->getStmtClass() == ConditionalOperatorClass; 2791 } 2792 static bool classof(const ConditionalOperator *) { return true; } 2793 2794 // Iterators 2795 child_range children() { 2796 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2797 } 2798}; 2799 2800/// BinaryConditionalOperator - The GNU extension to the conditional 2801/// operator which allows the middle operand to be omitted. 2802/// 2803/// This is a different expression kind on the assumption that almost 2804/// every client ends up needing to know that these are different. 2805class BinaryConditionalOperator : public AbstractConditionalOperator { 2806 enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; 2807 2808 /// - the common condition/left-hand-side expression, which will be 2809 /// evaluated as the opaque value 2810 /// - the condition, expressed in terms of the opaque value 2811 /// - the left-hand-side, expressed in terms of the opaque value 2812 /// - the right-hand-side 2813 Stmt *SubExprs[NUM_SUBEXPRS]; 2814 OpaqueValueExpr *OpaqueValue; 2815 2816 friend class ASTStmtReader; 2817public: 2818 BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, 2819 Expr *cond, Expr *lhs, Expr *rhs, 2820 SourceLocation qloc, SourceLocation cloc, 2821 QualType t, ExprValueKind VK, ExprObjectKind OK) 2822 : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, 2823 (common->isTypeDependent() || rhs->isTypeDependent()), 2824 (common->isValueDependent() || rhs->isValueDependent()), 2825 (common->containsUnexpandedParameterPack() || 2826 rhs->containsUnexpandedParameterPack()), 2827 qloc, cloc), 2828 OpaqueValue(opaqueValue) { 2829 SubExprs[COMMON] = common; 2830 SubExprs[COND] = cond; 2831 SubExprs[LHS] = lhs; 2832 SubExprs[RHS] = rhs; 2833 2834 OpaqueValue->setSourceExpr(common); 2835 } 2836 2837 /// \brief Build an empty conditional operator. 2838 explicit BinaryConditionalOperator(EmptyShell Empty) 2839 : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } 2840 2841 /// \brief getCommon - Return the common expression, written to the 2842 /// left of the condition. The opaque value will be bound to the 2843 /// result of this expression. 2844 Expr *getCommon() const { return cast<Expr>(SubExprs[COMMON]); } 2845 2846 /// \brief getOpaqueValue - Return the opaque value placeholder. 2847 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } 2848 2849 /// \brief getCond - Return the condition expression; this is defined 2850 /// in terms of the opaque value. 2851 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2852 2853 /// \brief getTrueExpr - Return the subexpression which will be 2854 /// evaluated if the condition evaluates to true; this is defined 2855 /// in terms of the opaque value. 2856 Expr *getTrueExpr() const { 2857 return cast<Expr>(SubExprs[LHS]); 2858 } 2859 2860 /// \brief getFalseExpr - Return the subexpression which will be 2861 /// evaluated if the condnition evaluates to false; this is 2862 /// defined in terms of the opaque value. 2863 Expr *getFalseExpr() const { 2864 return cast<Expr>(SubExprs[RHS]); 2865 } 2866 2867 SourceRange getSourceRange() const { 2868 return SourceRange(getCommon()->getLocStart(), getFalseExpr()->getLocEnd()); 2869 } 2870 static bool classof(const Stmt *T) { 2871 return T->getStmtClass() == BinaryConditionalOperatorClass; 2872 } 2873 static bool classof(const BinaryConditionalOperator *) { return true; } 2874 2875 // Iterators 2876 child_range children() { 2877 return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); 2878 } 2879}; 2880 2881inline Expr *AbstractConditionalOperator::getCond() const { 2882 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 2883 return co->getCond(); 2884 return cast<BinaryConditionalOperator>(this)->getCond(); 2885} 2886 2887inline Expr *AbstractConditionalOperator::getTrueExpr() const { 2888 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 2889 return co->getTrueExpr(); 2890 return cast<BinaryConditionalOperator>(this)->getTrueExpr(); 2891} 2892 2893inline Expr *AbstractConditionalOperator::getFalseExpr() const { 2894 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 2895 return co->getFalseExpr(); 2896 return cast<BinaryConditionalOperator>(this)->getFalseExpr(); 2897} 2898 2899/// AddrLabelExpr - The GNU address of label extension, representing &&label. 2900class AddrLabelExpr : public Expr { 2901 SourceLocation AmpAmpLoc, LabelLoc; 2902 LabelDecl *Label; 2903public: 2904 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, 2905 QualType t) 2906 : Expr(AddrLabelExprClass, t, VK_RValue, OK_Ordinary, false, false, false), 2907 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 2908 2909 /// \brief Build an empty address of a label expression. 2910 explicit AddrLabelExpr(EmptyShell Empty) 2911 : Expr(AddrLabelExprClass, Empty) { } 2912 2913 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 2914 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 2915 SourceLocation getLabelLoc() const { return LabelLoc; } 2916 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2917 2918 SourceRange getSourceRange() const { 2919 return SourceRange(AmpAmpLoc, LabelLoc); 2920 } 2921 2922 LabelDecl *getLabel() const { return Label; } 2923 void setLabel(LabelDecl *L) { Label = L; } 2924 2925 static bool classof(const Stmt *T) { 2926 return T->getStmtClass() == AddrLabelExprClass; 2927 } 2928 static bool classof(const AddrLabelExpr *) { return true; } 2929 2930 // Iterators 2931 child_range children() { return child_range(); } 2932}; 2933 2934/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 2935/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 2936/// takes the value of the last subexpression. 2937/// 2938/// A StmtExpr is always an r-value; values "returned" out of a 2939/// StmtExpr will be copied. 2940class StmtExpr : public Expr { 2941 Stmt *SubStmt; 2942 SourceLocation LParenLoc, RParenLoc; 2943public: 2944 // FIXME: Does type-dependence need to be computed differently? 2945 StmtExpr(CompoundStmt *substmt, QualType T, 2946 SourceLocation lp, SourceLocation rp) : 2947 Expr(StmtExprClass, T, VK_RValue, OK_Ordinary, 2948 T->isDependentType(), false, false), 2949 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 2950 2951 /// \brief Build an empty statement expression. 2952 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 2953 2954 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 2955 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 2956 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 2957 2958 SourceRange getSourceRange() const { 2959 return SourceRange(LParenLoc, RParenLoc); 2960 } 2961 2962 SourceLocation getLParenLoc() const { return LParenLoc; } 2963 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2964 SourceLocation getRParenLoc() const { return RParenLoc; } 2965 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2966 2967 static bool classof(const Stmt *T) { 2968 return T->getStmtClass() == StmtExprClass; 2969 } 2970 static bool classof(const StmtExpr *) { return true; } 2971 2972 // Iterators 2973 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 2974}; 2975 2976 2977/// ShuffleVectorExpr - clang-specific builtin-in function 2978/// __builtin_shufflevector. 2979/// This AST node represents a operator that does a constant 2980/// shuffle, similar to LLVM's shufflevector instruction. It takes 2981/// two vectors and a variable number of constant indices, 2982/// and returns the appropriately shuffled vector. 2983class ShuffleVectorExpr : public Expr { 2984 SourceLocation BuiltinLoc, RParenLoc; 2985 2986 // SubExprs - the list of values passed to the __builtin_shufflevector 2987 // function. The first two are vectors, and the rest are constant 2988 // indices. The number of values in this list is always 2989 // 2+the number of indices in the vector type. 2990 Stmt **SubExprs; 2991 unsigned NumExprs; 2992 2993public: 2994 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 2995 QualType Type, SourceLocation BLoc, 2996 SourceLocation RP); 2997 2998 /// \brief Build an empty vector-shuffle expression. 2999 explicit ShuffleVectorExpr(EmptyShell Empty) 3000 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 3001 3002 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3003 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3004 3005 SourceLocation getRParenLoc() const { return RParenLoc; } 3006 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3007 3008 SourceRange getSourceRange() const { 3009 return SourceRange(BuiltinLoc, RParenLoc); 3010 } 3011 static bool classof(const Stmt *T) { 3012 return T->getStmtClass() == ShuffleVectorExprClass; 3013 } 3014 static bool classof(const ShuffleVectorExpr *) { return true; } 3015 3016 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 3017 /// constant expression, the actual arguments passed in, and the function 3018 /// pointers. 3019 unsigned getNumSubExprs() const { return NumExprs; } 3020 3021 /// \brief Retrieve the array of expressions. 3022 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 3023 3024 /// getExpr - Return the Expr at the specified index. 3025 Expr *getExpr(unsigned Index) { 3026 assert((Index < NumExprs) && "Arg access out of range!"); 3027 return cast<Expr>(SubExprs[Index]); 3028 } 3029 const Expr *getExpr(unsigned Index) const { 3030 assert((Index < NumExprs) && "Arg access out of range!"); 3031 return cast<Expr>(SubExprs[Index]); 3032 } 3033 3034 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 3035 3036 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 3037 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 3038 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 3039 } 3040 3041 // Iterators 3042 child_range children() { 3043 return child_range(&SubExprs[0], &SubExprs[0]+NumExprs); 3044 } 3045}; 3046 3047/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 3048/// This AST node is similar to the conditional operator (?:) in C, with 3049/// the following exceptions: 3050/// - the test expression must be a integer constant expression. 3051/// - the expression returned acts like the chosen subexpression in every 3052/// visible way: the type is the same as that of the chosen subexpression, 3053/// and all predicates (whether it's an l-value, whether it's an integer 3054/// constant expression, etc.) return the same result as for the chosen 3055/// sub-expression. 3056class ChooseExpr : public Expr { 3057 enum { COND, LHS, RHS, END_EXPR }; 3058 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 3059 SourceLocation BuiltinLoc, RParenLoc; 3060public: 3061 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, 3062 QualType t, ExprValueKind VK, ExprObjectKind OK, 3063 SourceLocation RP, bool TypeDependent, bool ValueDependent) 3064 : Expr(ChooseExprClass, t, VK, OK, TypeDependent, ValueDependent, 3065 (cond->containsUnexpandedParameterPack() || 3066 lhs->containsUnexpandedParameterPack() || 3067 rhs->containsUnexpandedParameterPack())), 3068 BuiltinLoc(BLoc), RParenLoc(RP) { 3069 SubExprs[COND] = cond; 3070 SubExprs[LHS] = lhs; 3071 SubExprs[RHS] = rhs; 3072 } 3073 3074 /// \brief Build an empty __builtin_choose_expr. 3075 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 3076 3077 /// isConditionTrue - Return whether the condition is true (i.e. not 3078 /// equal to zero). 3079 bool isConditionTrue(const ASTContext &C) const; 3080 3081 /// getChosenSubExpr - Return the subexpression chosen according to the 3082 /// condition. 3083 Expr *getChosenSubExpr(const ASTContext &C) const { 3084 return isConditionTrue(C) ? getLHS() : getRHS(); 3085 } 3086 3087 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 3088 void setCond(Expr *E) { SubExprs[COND] = E; } 3089 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 3090 void setLHS(Expr *E) { SubExprs[LHS] = E; } 3091 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 3092 void setRHS(Expr *E) { SubExprs[RHS] = E; } 3093 3094 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3095 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3096 3097 SourceLocation getRParenLoc() const { return RParenLoc; } 3098 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3099 3100 SourceRange getSourceRange() const { 3101 return SourceRange(BuiltinLoc, RParenLoc); 3102 } 3103 static bool classof(const Stmt *T) { 3104 return T->getStmtClass() == ChooseExprClass; 3105 } 3106 static bool classof(const ChooseExpr *) { return true; } 3107 3108 // Iterators 3109 child_range children() { 3110 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 3111 } 3112}; 3113 3114/// GNUNullExpr - Implements the GNU __null extension, which is a name 3115/// for a null pointer constant that has integral type (e.g., int or 3116/// long) and is the same size and alignment as a pointer. The __null 3117/// extension is typically only used by system headers, which define 3118/// NULL as __null in C++ rather than using 0 (which is an integer 3119/// that may not match the size of a pointer). 3120class GNUNullExpr : public Expr { 3121 /// TokenLoc - The location of the __null keyword. 3122 SourceLocation TokenLoc; 3123 3124public: 3125 GNUNullExpr(QualType Ty, SourceLocation Loc) 3126 : Expr(GNUNullExprClass, Ty, VK_RValue, OK_Ordinary, false, false, false), 3127 TokenLoc(Loc) { } 3128 3129 /// \brief Build an empty GNU __null expression. 3130 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 3131 3132 /// getTokenLocation - The location of the __null token. 3133 SourceLocation getTokenLocation() const { return TokenLoc; } 3134 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 3135 3136 SourceRange getSourceRange() const { 3137 return SourceRange(TokenLoc); 3138 } 3139 static bool classof(const Stmt *T) { 3140 return T->getStmtClass() == GNUNullExprClass; 3141 } 3142 static bool classof(const GNUNullExpr *) { return true; } 3143 3144 // Iterators 3145 child_range children() { return child_range(); } 3146}; 3147 3148/// VAArgExpr, used for the builtin function __builtin_va_arg. 3149class VAArgExpr : public Expr { 3150 Stmt *Val; 3151 TypeSourceInfo *TInfo; 3152 SourceLocation BuiltinLoc, RParenLoc; 3153public: 3154 VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo, 3155 SourceLocation RPLoc, QualType t) 3156 : Expr(VAArgExprClass, t, VK_RValue, OK_Ordinary, 3157 t->isDependentType(), false, 3158 (TInfo->getType()->containsUnexpandedParameterPack() || 3159 e->containsUnexpandedParameterPack())), 3160 Val(e), TInfo(TInfo), 3161 BuiltinLoc(BLoc), 3162 RParenLoc(RPLoc) { } 3163 3164 /// \brief Create an empty __builtin_va_arg expression. 3165 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 3166 3167 const Expr *getSubExpr() const { return cast<Expr>(Val); } 3168 Expr *getSubExpr() { return cast<Expr>(Val); } 3169 void setSubExpr(Expr *E) { Val = E; } 3170 3171 TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; } 3172 void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; } 3173 3174 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 3175 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 3176 3177 SourceLocation getRParenLoc() const { return RParenLoc; } 3178 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3179 3180 SourceRange getSourceRange() const { 3181 return SourceRange(BuiltinLoc, RParenLoc); 3182 } 3183 static bool classof(const Stmt *T) { 3184 return T->getStmtClass() == VAArgExprClass; 3185 } 3186 static bool classof(const VAArgExpr *) { return true; } 3187 3188 // Iterators 3189 child_range children() { return child_range(&Val, &Val+1); } 3190}; 3191 3192/// @brief Describes an C or C++ initializer list. 3193/// 3194/// InitListExpr describes an initializer list, which can be used to 3195/// initialize objects of different types, including 3196/// struct/class/union types, arrays, and vectors. For example: 3197/// 3198/// @code 3199/// struct foo x = { 1, { 2, 3 } }; 3200/// @endcode 3201/// 3202/// Prior to semantic analysis, an initializer list will represent the 3203/// initializer list as written by the user, but will have the 3204/// placeholder type "void". This initializer list is called the 3205/// syntactic form of the initializer, and may contain C99 designated 3206/// initializers (represented as DesignatedInitExprs), initializations 3207/// of subobject members without explicit braces, and so on. Clients 3208/// interested in the original syntax of the initializer list should 3209/// use the syntactic form of the initializer list. 3210/// 3211/// After semantic analysis, the initializer list will represent the 3212/// semantic form of the initializer, where the initializations of all 3213/// subobjects are made explicit with nested InitListExpr nodes and 3214/// C99 designators have been eliminated by placing the designated 3215/// initializations into the subobject they initialize. Additionally, 3216/// any "holes" in the initialization, where no initializer has been 3217/// specified for a particular subobject, will be replaced with 3218/// implicitly-generated ImplicitValueInitExpr expressions that 3219/// value-initialize the subobjects. Note, however, that the 3220/// initializer lists may still have fewer initializers than there are 3221/// elements to initialize within the object. 3222/// 3223/// Given the semantic form of the initializer list, one can retrieve 3224/// the original syntactic form of that initializer list (if it 3225/// exists) using getSyntacticForm(). Since many initializer lists 3226/// have the same syntactic and semantic forms, getSyntacticForm() may 3227/// return NULL, indicating that the current initializer list also 3228/// serves as its syntactic form. 3229class InitListExpr : public Expr { 3230 // FIXME: Eliminate this vector in favor of ASTContext allocation 3231 typedef ASTVector<Stmt *> InitExprsTy; 3232 InitExprsTy InitExprs; 3233 SourceLocation LBraceLoc, RBraceLoc; 3234 3235 /// Contains the initializer list that describes the syntactic form 3236 /// written in the source code. 3237 InitListExpr *SyntacticForm; 3238 3239 /// \brief Either: 3240 /// If this initializer list initializes an array with more elements than 3241 /// there are initializers in the list, specifies an expression to be used 3242 /// for value initialization of the rest of the elements. 3243 /// Or 3244 /// If this initializer list initializes a union, specifies which 3245 /// field within the union will be initialized. 3246 llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; 3247 3248 /// Whether this initializer list originally had a GNU array-range 3249 /// designator in it. This is a temporary marker used by CodeGen. 3250 bool HadArrayRangeDesignator; 3251 3252public: 3253 InitListExpr(ASTContext &C, SourceLocation lbraceloc, 3254 Expr **initexprs, unsigned numinits, 3255 SourceLocation rbraceloc); 3256 3257 /// \brief Build an empty initializer list. 3258 explicit InitListExpr(ASTContext &C, EmptyShell Empty) 3259 : Expr(InitListExprClass, Empty), InitExprs(C) { } 3260 3261 unsigned getNumInits() const { return InitExprs.size(); } 3262 3263 /// \brief Retrieve the set of initializers. 3264 Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } 3265 3266 const Expr *getInit(unsigned Init) const { 3267 assert(Init < getNumInits() && "Initializer access out of range!"); 3268 return cast_or_null<Expr>(InitExprs[Init]); 3269 } 3270 3271 Expr *getInit(unsigned Init) { 3272 assert(Init < getNumInits() && "Initializer access out of range!"); 3273 return cast_or_null<Expr>(InitExprs[Init]); 3274 } 3275 3276 void setInit(unsigned Init, Expr *expr) { 3277 assert(Init < getNumInits() && "Initializer access out of range!"); 3278 InitExprs[Init] = expr; 3279 } 3280 3281 /// \brief Reserve space for some number of initializers. 3282 void reserveInits(ASTContext &C, unsigned NumInits); 3283 3284 /// @brief Specify the number of initializers 3285 /// 3286 /// If there are more than @p NumInits initializers, the remaining 3287 /// initializers will be destroyed. If there are fewer than @p 3288 /// NumInits initializers, NULL expressions will be added for the 3289 /// unknown initializers. 3290 void resizeInits(ASTContext &Context, unsigned NumInits); 3291 3292 /// @brief Updates the initializer at index @p Init with the new 3293 /// expression @p expr, and returns the old expression at that 3294 /// location. 3295 /// 3296 /// When @p Init is out of range for this initializer list, the 3297 /// initializer list will be extended with NULL expressions to 3298 /// accommodate the new entry. 3299 Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr); 3300 3301 /// \brief If this initializer list initializes an array with more elements 3302 /// than there are initializers in the list, specifies an expression to be 3303 /// used for value initialization of the rest of the elements. 3304 Expr *getArrayFiller() { 3305 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>(); 3306 } 3307 void setArrayFiller(Expr *filler); 3308 3309 /// \brief If this initializes a union, specifies which field in the 3310 /// union to initialize. 3311 /// 3312 /// Typically, this field is the first named field within the 3313 /// union. However, a designated initializer can specify the 3314 /// initialization of a different field within the union. 3315 FieldDecl *getInitializedFieldInUnion() { 3316 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>(); 3317 } 3318 void setInitializedFieldInUnion(FieldDecl *FD) { 3319 ArrayFillerOrUnionFieldInit = FD; 3320 } 3321 3322 // Explicit InitListExpr's originate from source code (and have valid source 3323 // locations). Implicit InitListExpr's are created by the semantic analyzer. 3324 bool isExplicit() { 3325 return LBraceLoc.isValid() && RBraceLoc.isValid(); 3326 } 3327 3328 SourceLocation getLBraceLoc() const { return LBraceLoc; } 3329 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 3330 SourceLocation getRBraceLoc() const { return RBraceLoc; } 3331 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 3332 3333 /// @brief Retrieve the initializer list that describes the 3334 /// syntactic form of the initializer. 3335 /// 3336 /// 3337 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 3338 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 3339 3340 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 3341 void sawArrayRangeDesignator(bool ARD = true) { 3342 HadArrayRangeDesignator = ARD; 3343 } 3344 3345 SourceRange getSourceRange() const; 3346 3347 static bool classof(const Stmt *T) { 3348 return T->getStmtClass() == InitListExprClass; 3349 } 3350 static bool classof(const InitListExpr *) { return true; } 3351 3352 // Iterators 3353 child_range children() { 3354 if (InitExprs.empty()) return child_range(); 3355 return child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); 3356 } 3357 3358 typedef InitExprsTy::iterator iterator; 3359 typedef InitExprsTy::const_iterator const_iterator; 3360 typedef InitExprsTy::reverse_iterator reverse_iterator; 3361 typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; 3362 3363 iterator begin() { return InitExprs.begin(); } 3364 const_iterator begin() const { return InitExprs.begin(); } 3365 iterator end() { return InitExprs.end(); } 3366 const_iterator end() const { return InitExprs.end(); } 3367 reverse_iterator rbegin() { return InitExprs.rbegin(); } 3368 const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } 3369 reverse_iterator rend() { return InitExprs.rend(); } 3370 const_reverse_iterator rend() const { return InitExprs.rend(); } 3371 3372 friend class ASTStmtReader; 3373 friend class ASTStmtWriter; 3374}; 3375 3376/// @brief Represents a C99 designated initializer expression. 3377/// 3378/// A designated initializer expression (C99 6.7.8) contains one or 3379/// more designators (which can be field designators, array 3380/// designators, or GNU array-range designators) followed by an 3381/// expression that initializes the field or element(s) that the 3382/// designators refer to. For example, given: 3383/// 3384/// @code 3385/// struct point { 3386/// double x; 3387/// double y; 3388/// }; 3389/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 3390/// @endcode 3391/// 3392/// The InitListExpr contains three DesignatedInitExprs, the first of 3393/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 3394/// designators, one array designator for @c [2] followed by one field 3395/// designator for @c .y. The initalization expression will be 1.0. 3396class DesignatedInitExpr : public Expr { 3397public: 3398 /// \brief Forward declaration of the Designator class. 3399 class Designator; 3400 3401private: 3402 /// The location of the '=' or ':' prior to the actual initializer 3403 /// expression. 3404 SourceLocation EqualOrColonLoc; 3405 3406 /// Whether this designated initializer used the GNU deprecated 3407 /// syntax rather than the C99 '=' syntax. 3408 bool GNUSyntax : 1; 3409 3410 /// The number of designators in this initializer expression. 3411 unsigned NumDesignators : 15; 3412 3413 /// \brief The designators in this designated initialization 3414 /// expression. 3415 Designator *Designators; 3416 3417 /// The number of subexpressions of this initializer expression, 3418 /// which contains both the initializer and any additional 3419 /// expressions used by array and array-range designators. 3420 unsigned NumSubExprs : 16; 3421 3422 3423 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators, 3424 const Designator *Designators, 3425 SourceLocation EqualOrColonLoc, bool GNUSyntax, 3426 Expr **IndexExprs, unsigned NumIndexExprs, 3427 Expr *Init); 3428 3429 explicit DesignatedInitExpr(unsigned NumSubExprs) 3430 : Expr(DesignatedInitExprClass, EmptyShell()), 3431 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 3432 3433public: 3434 /// A field designator, e.g., ".x". 3435 struct FieldDesignator { 3436 /// Refers to the field that is being initialized. The low bit 3437 /// of this field determines whether this is actually a pointer 3438 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 3439 /// initially constructed, a field designator will store an 3440 /// IdentifierInfo*. After semantic analysis has resolved that 3441 /// name, the field designator will instead store a FieldDecl*. 3442 uintptr_t NameOrField; 3443 3444 /// The location of the '.' in the designated initializer. 3445 unsigned DotLoc; 3446 3447 /// The location of the field name in the designated initializer. 3448 unsigned FieldLoc; 3449 }; 3450 3451 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3452 struct ArrayOrRangeDesignator { 3453 /// Location of the first index expression within the designated 3454 /// initializer expression's list of subexpressions. 3455 unsigned Index; 3456 /// The location of the '[' starting the array range designator. 3457 unsigned LBracketLoc; 3458 /// The location of the ellipsis separating the start and end 3459 /// indices. Only valid for GNU array-range designators. 3460 unsigned EllipsisLoc; 3461 /// The location of the ']' terminating the array range designator. 3462 unsigned RBracketLoc; 3463 }; 3464 3465 /// @brief Represents a single C99 designator. 3466 /// 3467 /// @todo This class is infuriatingly similar to clang::Designator, 3468 /// but minor differences (storing indices vs. storing pointers) 3469 /// keep us from reusing it. Try harder, later, to rectify these 3470 /// differences. 3471 class Designator { 3472 /// @brief The kind of designator this describes. 3473 enum { 3474 FieldDesignator, 3475 ArrayDesignator, 3476 ArrayRangeDesignator 3477 } Kind; 3478 3479 union { 3480 /// A field designator, e.g., ".x". 3481 struct FieldDesignator Field; 3482 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 3483 struct ArrayOrRangeDesignator ArrayOrRange; 3484 }; 3485 friend class DesignatedInitExpr; 3486 3487 public: 3488 Designator() {} 3489 3490 /// @brief Initializes a field designator. 3491 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 3492 SourceLocation FieldLoc) 3493 : Kind(FieldDesignator) { 3494 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 3495 Field.DotLoc = DotLoc.getRawEncoding(); 3496 Field.FieldLoc = FieldLoc.getRawEncoding(); 3497 } 3498 3499 /// @brief Initializes an array designator. 3500 Designator(unsigned Index, SourceLocation LBracketLoc, 3501 SourceLocation RBracketLoc) 3502 : Kind(ArrayDesignator) { 3503 ArrayOrRange.Index = Index; 3504 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3505 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 3506 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3507 } 3508 3509 /// @brief Initializes a GNU array-range designator. 3510 Designator(unsigned Index, SourceLocation LBracketLoc, 3511 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 3512 : Kind(ArrayRangeDesignator) { 3513 ArrayOrRange.Index = Index; 3514 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 3515 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 3516 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 3517 } 3518 3519 bool isFieldDesignator() const { return Kind == FieldDesignator; } 3520 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 3521 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 3522 3523 IdentifierInfo * getFieldName(); 3524 3525 FieldDecl *getField() { 3526 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3527 if (Field.NameOrField & 0x01) 3528 return 0; 3529 else 3530 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 3531 } 3532 3533 void setField(FieldDecl *FD) { 3534 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3535 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 3536 } 3537 3538 SourceLocation getDotLoc() const { 3539 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3540 return SourceLocation::getFromRawEncoding(Field.DotLoc); 3541 } 3542 3543 SourceLocation getFieldLoc() const { 3544 assert(Kind == FieldDesignator && "Only valid on a field designator"); 3545 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 3546 } 3547 3548 SourceLocation getLBracketLoc() const { 3549 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3550 "Only valid on an array or array-range designator"); 3551 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 3552 } 3553 3554 SourceLocation getRBracketLoc() const { 3555 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3556 "Only valid on an array or array-range designator"); 3557 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 3558 } 3559 3560 SourceLocation getEllipsisLoc() const { 3561 assert(Kind == ArrayRangeDesignator && 3562 "Only valid on an array-range designator"); 3563 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 3564 } 3565 3566 unsigned getFirstExprIndex() const { 3567 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3568 "Only valid on an array or array-range designator"); 3569 return ArrayOrRange.Index; 3570 } 3571 3572 SourceLocation getStartLocation() const { 3573 if (Kind == FieldDesignator) 3574 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 3575 else 3576 return getLBracketLoc(); 3577 } 3578 SourceLocation getEndLocation() const { 3579 return Kind == FieldDesignator ? getFieldLoc() : getRBracketLoc(); 3580 } 3581 SourceRange getSourceRange() const { 3582 return SourceRange(getStartLocation(), getEndLocation()); 3583 } 3584 }; 3585 3586 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 3587 unsigned NumDesignators, 3588 Expr **IndexExprs, unsigned NumIndexExprs, 3589 SourceLocation EqualOrColonLoc, 3590 bool GNUSyntax, Expr *Init); 3591 3592 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 3593 3594 /// @brief Returns the number of designators in this initializer. 3595 unsigned size() const { return NumDesignators; } 3596 3597 // Iterator access to the designators. 3598 typedef Designator* designators_iterator; 3599 designators_iterator designators_begin() { return Designators; } 3600 designators_iterator designators_end() { 3601 return Designators + NumDesignators; 3602 } 3603 3604 typedef std::reverse_iterator<designators_iterator> 3605 reverse_designators_iterator; 3606 reverse_designators_iterator designators_rbegin() { 3607 return reverse_designators_iterator(designators_end()); 3608 } 3609 reverse_designators_iterator designators_rend() { 3610 return reverse_designators_iterator(designators_begin()); 3611 } 3612 3613 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 3614 3615 void setDesignators(ASTContext &C, const Designator *Desigs, 3616 unsigned NumDesigs); 3617 3618 Expr *getArrayIndex(const Designator& D); 3619 Expr *getArrayRangeStart(const Designator& D); 3620 Expr *getArrayRangeEnd(const Designator& D); 3621 3622 /// @brief Retrieve the location of the '=' that precedes the 3623 /// initializer value itself, if present. 3624 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 3625 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 3626 3627 /// @brief Determines whether this designated initializer used the 3628 /// deprecated GNU syntax for designated initializers. 3629 bool usesGNUSyntax() const { return GNUSyntax; } 3630 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 3631 3632 /// @brief Retrieve the initializer value. 3633 Expr *getInit() const { 3634 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 3635 } 3636 3637 void setInit(Expr *init) { 3638 *child_begin() = init; 3639 } 3640 3641 /// \brief Retrieve the total number of subexpressions in this 3642 /// designated initializer expression, including the actual 3643 /// initialized value and any expressions that occur within array 3644 /// and array-range designators. 3645 unsigned getNumSubExprs() const { return NumSubExprs; } 3646 3647 Expr *getSubExpr(unsigned Idx) { 3648 assert(Idx < NumSubExprs && "Subscript out of range"); 3649 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3650 Ptr += sizeof(DesignatedInitExpr); 3651 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 3652 } 3653 3654 void setSubExpr(unsigned Idx, Expr *E) { 3655 assert(Idx < NumSubExprs && "Subscript out of range"); 3656 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3657 Ptr += sizeof(DesignatedInitExpr); 3658 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 3659 } 3660 3661 /// \brief Replaces the designator at index @p Idx with the series 3662 /// of designators in [First, Last). 3663 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First, 3664 const Designator *Last); 3665 3666 SourceRange getDesignatorsSourceRange() const; 3667 3668 SourceRange getSourceRange() const; 3669 3670 static bool classof(const Stmt *T) { 3671 return T->getStmtClass() == DesignatedInitExprClass; 3672 } 3673 static bool classof(const DesignatedInitExpr *) { return true; } 3674 3675 // Iterators 3676 child_range children() { 3677 Stmt **begin = reinterpret_cast<Stmt**>(this + 1); 3678 return child_range(begin, begin + NumSubExprs); 3679 } 3680}; 3681 3682/// \brief Represents an implicitly-generated value initialization of 3683/// an object of a given type. 3684/// 3685/// Implicit value initializations occur within semantic initializer 3686/// list expressions (InitListExpr) as placeholders for subobject 3687/// initializations not explicitly specified by the user. 3688/// 3689/// \see InitListExpr 3690class ImplicitValueInitExpr : public Expr { 3691public: 3692 explicit ImplicitValueInitExpr(QualType ty) 3693 : Expr(ImplicitValueInitExprClass, ty, VK_RValue, OK_Ordinary, 3694 false, false, false) { } 3695 3696 /// \brief Construct an empty implicit value initialization. 3697 explicit ImplicitValueInitExpr(EmptyShell Empty) 3698 : Expr(ImplicitValueInitExprClass, Empty) { } 3699 3700 static bool classof(const Stmt *T) { 3701 return T->getStmtClass() == ImplicitValueInitExprClass; 3702 } 3703 static bool classof(const ImplicitValueInitExpr *) { return true; } 3704 3705 SourceRange getSourceRange() const { 3706 return SourceRange(); 3707 } 3708 3709 // Iterators 3710 child_range children() { return child_range(); } 3711}; 3712 3713 3714class ParenListExpr : public Expr { 3715 Stmt **Exprs; 3716 unsigned NumExprs; 3717 SourceLocation LParenLoc, RParenLoc; 3718 3719public: 3720 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 3721 unsigned numexprs, SourceLocation rparenloc); 3722 3723 /// \brief Build an empty paren list. 3724 explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 3725 3726 unsigned getNumExprs() const { return NumExprs; } 3727 3728 const Expr* getExpr(unsigned Init) const { 3729 assert(Init < getNumExprs() && "Initializer access out of range!"); 3730 return cast_or_null<Expr>(Exprs[Init]); 3731 } 3732 3733 Expr* getExpr(unsigned Init) { 3734 assert(Init < getNumExprs() && "Initializer access out of range!"); 3735 return cast_or_null<Expr>(Exprs[Init]); 3736 } 3737 3738 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 3739 3740 SourceLocation getLParenLoc() const { return LParenLoc; } 3741 SourceLocation getRParenLoc() const { return RParenLoc; } 3742 3743 SourceRange getSourceRange() const { 3744 return SourceRange(LParenLoc, RParenLoc); 3745 } 3746 static bool classof(const Stmt *T) { 3747 return T->getStmtClass() == ParenListExprClass; 3748 } 3749 static bool classof(const ParenListExpr *) { return true; } 3750 3751 // Iterators 3752 child_range children() { 3753 return child_range(&Exprs[0], &Exprs[0]+NumExprs); 3754 } 3755 3756 friend class ASTStmtReader; 3757 friend class ASTStmtWriter; 3758}; 3759 3760 3761/// \brief Represents a C1X generic selection. 3762/// 3763/// A generic selection (C1X 6.5.1.1) contains an unevaluated controlling 3764/// expression, followed by one or more generic associations. Each generic 3765/// association specifies a type name and an expression, or "default" and an 3766/// expression (in which case it is known as a default generic association). 3767/// The type and value of the generic selection are identical to those of its 3768/// result expression, which is defined as the expression in the generic 3769/// association with a type name that is compatible with the type of the 3770/// controlling expression, or the expression in the default generic association 3771/// if no types are compatible. For example: 3772/// 3773/// @code 3774/// _Generic(X, double: 1, float: 2, default: 3) 3775/// @endcode 3776/// 3777/// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f 3778/// or 3 if "hello". 3779/// 3780/// As an extension, generic selections are allowed in C++, where the following 3781/// additional semantics apply: 3782/// 3783/// Any generic selection whose controlling expression is type-dependent or 3784/// which names a dependent type in its association list is result-dependent, 3785/// which means that the choice of result expression is dependent. 3786/// Result-dependent generic associations are both type- and value-dependent. 3787class GenericSelectionExpr : public Expr { 3788 enum { CONTROLLING, END_EXPR }; 3789 TypeSourceInfo **AssocTypes; 3790 Stmt **SubExprs; 3791 unsigned NumAssocs, ResultIndex; 3792 SourceLocation GenericLoc, DefaultLoc, RParenLoc; 3793 3794public: 3795 GenericSelectionExpr(ASTContext &Context, 3796 SourceLocation GenericLoc, Expr *ControllingExpr, 3797 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 3798 unsigned NumAssocs, SourceLocation DefaultLoc, 3799 SourceLocation RParenLoc, 3800 bool ContainsUnexpandedParameterPack, 3801 unsigned ResultIndex); 3802 3803 /// This constructor is used in the result-dependent case. 3804 GenericSelectionExpr(ASTContext &Context, 3805 SourceLocation GenericLoc, Expr *ControllingExpr, 3806 TypeSourceInfo **AssocTypes, Expr **AssocExprs, 3807 unsigned NumAssocs, SourceLocation DefaultLoc, 3808 SourceLocation RParenLoc, 3809 bool ContainsUnexpandedParameterPack); 3810 3811 explicit GenericSelectionExpr(EmptyShell Empty) 3812 : Expr(GenericSelectionExprClass, Empty) { } 3813 3814 unsigned getNumAssocs() const { return NumAssocs; } 3815 3816 SourceLocation getGenericLoc() const { return GenericLoc; } 3817 SourceLocation getDefaultLoc() const { return DefaultLoc; } 3818 SourceLocation getRParenLoc() const { return RParenLoc; } 3819 3820 const Expr *getAssocExpr(unsigned i) const { 3821 return cast<Expr>(SubExprs[END_EXPR+i]); 3822 } 3823 Expr *getAssocExpr(unsigned i) { return cast<Expr>(SubExprs[END_EXPR+i]); } 3824 3825 const TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) const { 3826 return AssocTypes[i]; 3827 } 3828 TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) { return AssocTypes[i]; } 3829 3830 QualType getAssocType(unsigned i) const { 3831 if (const TypeSourceInfo *TS = getAssocTypeSourceInfo(i)) 3832 return TS->getType(); 3833 else 3834 return QualType(); 3835 } 3836 3837 const Expr *getControllingExpr() const { 3838 return cast<Expr>(SubExprs[CONTROLLING]); 3839 } 3840 Expr *getControllingExpr() { return cast<Expr>(SubExprs[CONTROLLING]); } 3841 3842 /// Whether this generic selection is result-dependent. 3843 bool isResultDependent() const { return ResultIndex == -1U; } 3844 3845 /// The zero-based index of the result expression's generic association in 3846 /// the generic selection's association list. Defined only if the 3847 /// generic selection is not result-dependent. 3848 unsigned getResultIndex() const { 3849 assert(!isResultDependent() && "Generic selection is result-dependent"); 3850 return ResultIndex; 3851 } 3852 3853 /// The generic selection's result expression. Defined only if the 3854 /// generic selection is not result-dependent. 3855 const Expr *getResultExpr() const { return getAssocExpr(getResultIndex()); } 3856 Expr *getResultExpr() { return getAssocExpr(getResultIndex()); } 3857 3858 SourceRange getSourceRange() const { 3859 return SourceRange(GenericLoc, RParenLoc); 3860 } 3861 static bool classof(const Stmt *T) { 3862 return T->getStmtClass() == GenericSelectionExprClass; 3863 } 3864 static bool classof(const GenericSelectionExpr *) { return true; } 3865 3866 child_range children() { 3867 return child_range(SubExprs, SubExprs+END_EXPR+NumAssocs); 3868 } 3869 3870 friend class ASTStmtReader; 3871}; 3872 3873//===----------------------------------------------------------------------===// 3874// Clang Extensions 3875//===----------------------------------------------------------------------===// 3876 3877 3878/// ExtVectorElementExpr - This represents access to specific elements of a 3879/// vector, and may occur on the left hand side or right hand side. For example 3880/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 3881/// 3882/// Note that the base may have either vector or pointer to vector type, just 3883/// like a struct field reference. 3884/// 3885class ExtVectorElementExpr : public Expr { 3886 Stmt *Base; 3887 IdentifierInfo *Accessor; 3888 SourceLocation AccessorLoc; 3889public: 3890 ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, 3891 IdentifierInfo &accessor, SourceLocation loc) 3892 : Expr(ExtVectorElementExprClass, ty, VK, 3893 (VK == VK_RValue ? OK_Ordinary : OK_VectorComponent), 3894 base->isTypeDependent(), base->isValueDependent(), 3895 base->containsUnexpandedParameterPack()), 3896 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 3897 3898 /// \brief Build an empty vector element expression. 3899 explicit ExtVectorElementExpr(EmptyShell Empty) 3900 : Expr(ExtVectorElementExprClass, Empty) { } 3901 3902 const Expr *getBase() const { return cast<Expr>(Base); } 3903 Expr *getBase() { return cast<Expr>(Base); } 3904 void setBase(Expr *E) { Base = E; } 3905 3906 IdentifierInfo &getAccessor() const { return *Accessor; } 3907 void setAccessor(IdentifierInfo *II) { Accessor = II; } 3908 3909 SourceLocation getAccessorLoc() const { return AccessorLoc; } 3910 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 3911 3912 /// getNumElements - Get the number of components being selected. 3913 unsigned getNumElements() const; 3914 3915 /// containsDuplicateElements - Return true if any element access is 3916 /// repeated. 3917 bool containsDuplicateElements() const; 3918 3919 /// getEncodedElementAccess - Encode the elements accessed into an llvm 3920 /// aggregate Constant of ConstantInt(s). 3921 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 3922 3923 SourceRange getSourceRange() const { 3924 return SourceRange(getBase()->getLocStart(), AccessorLoc); 3925 } 3926 3927 /// isArrow - Return true if the base expression is a pointer to vector, 3928 /// return false if the base expression is a vector. 3929 bool isArrow() const; 3930 3931 static bool classof(const Stmt *T) { 3932 return T->getStmtClass() == ExtVectorElementExprClass; 3933 } 3934 static bool classof(const ExtVectorElementExpr *) { return true; } 3935 3936 // Iterators 3937 child_range children() { return child_range(&Base, &Base+1); } 3938}; 3939 3940 3941/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 3942/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 3943class BlockExpr : public Expr { 3944protected: 3945 BlockDecl *TheBlock; 3946public: 3947 BlockExpr(BlockDecl *BD, QualType ty) 3948 : Expr(BlockExprClass, ty, VK_RValue, OK_Ordinary, 3949 ty->isDependentType(), false, false), 3950 TheBlock(BD) {} 3951 3952 /// \brief Build an empty block expression. 3953 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 3954 3955 const BlockDecl *getBlockDecl() const { return TheBlock; } 3956 BlockDecl *getBlockDecl() { return TheBlock; } 3957 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 3958 3959 // Convenience functions for probing the underlying BlockDecl. 3960 SourceLocation getCaretLocation() const; 3961 const Stmt *getBody() const; 3962 Stmt *getBody(); 3963 3964 SourceRange getSourceRange() const { 3965 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 3966 } 3967 3968 /// getFunctionType - Return the underlying function type for this block. 3969 const FunctionType *getFunctionType() const; 3970 3971 static bool classof(const Stmt *T) { 3972 return T->getStmtClass() == BlockExprClass; 3973 } 3974 static bool classof(const BlockExpr *) { return true; } 3975 3976 // Iterators 3977 child_range children() { return child_range(); } 3978}; 3979 3980/// BlockDeclRefExpr - A reference to a local variable declared in an 3981/// enclosing scope. 3982class BlockDeclRefExpr : public Expr { 3983 VarDecl *D; 3984 SourceLocation Loc; 3985 bool IsByRef : 1; 3986 bool ConstQualAdded : 1; 3987public: 3988 BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK, 3989 SourceLocation l, bool ByRef, bool constAdded = false); 3990 3991 // \brief Build an empty reference to a declared variable in a 3992 // block. 3993 explicit BlockDeclRefExpr(EmptyShell Empty) 3994 : Expr(BlockDeclRefExprClass, Empty) { } 3995 3996 VarDecl *getDecl() { return D; } 3997 const VarDecl *getDecl() const { return D; } 3998 void setDecl(VarDecl *VD) { D = VD; } 3999 4000 SourceLocation getLocation() const { return Loc; } 4001 void setLocation(SourceLocation L) { Loc = L; } 4002 4003 SourceRange getSourceRange() const { return SourceRange(Loc); } 4004 4005 bool isByRef() const { return IsByRef; } 4006 void setByRef(bool BR) { IsByRef = BR; } 4007 4008 bool isConstQualAdded() const { return ConstQualAdded; } 4009 void setConstQualAdded(bool C) { ConstQualAdded = C; } 4010 4011 static bool classof(const Stmt *T) { 4012 return T->getStmtClass() == BlockDeclRefExprClass; 4013 } 4014 static bool classof(const BlockDeclRefExpr *) { return true; } 4015 4016 // Iterators 4017 child_range children() { return child_range(); } 4018}; 4019 4020} // end namespace clang 4021 4022#endif 4023