clang API Documentation
00001 //===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the Expr interface and subclasses. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_EXPR_H 00015 #define LLVM_CLANG_AST_EXPR_H 00016 00017 #include "clang/AST/APValue.h" 00018 #include "clang/AST/ASTVector.h" 00019 #include "clang/AST/Decl.h" 00020 #include "clang/AST/DeclAccessPair.h" 00021 #include "clang/AST/OperationKinds.h" 00022 #include "clang/AST/Stmt.h" 00023 #include "clang/AST/TemplateBase.h" 00024 #include "clang/AST/Type.h" 00025 #include "clang/Basic/CharInfo.h" 00026 #include "clang/Basic/TypeTraits.h" 00027 #include "llvm/ADT/APFloat.h" 00028 #include "llvm/ADT/APSInt.h" 00029 #include "llvm/ADT/SmallVector.h" 00030 #include "llvm/ADT/StringRef.h" 00031 #include "llvm/Support/Compiler.h" 00032 00033 namespace clang { 00034 class APValue; 00035 class ASTContext; 00036 class BlockDecl; 00037 class CXXBaseSpecifier; 00038 class CXXMemberCallExpr; 00039 class CXXOperatorCallExpr; 00040 class CastExpr; 00041 class Decl; 00042 class IdentifierInfo; 00043 class MaterializeTemporaryExpr; 00044 class NamedDecl; 00045 class ObjCPropertyRefExpr; 00046 class OpaqueValueExpr; 00047 class ParmVarDecl; 00048 class StringLiteral; 00049 class TargetInfo; 00050 class ValueDecl; 00051 00052 /// \brief A simple array of base specifiers. 00053 typedef SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; 00054 00055 /// \brief An adjustment to be made to the temporary created when emitting a 00056 /// reference binding, which accesses a particular subobject of that temporary. 00057 struct SubobjectAdjustment { 00058 enum { 00059 DerivedToBaseAdjustment, 00060 FieldAdjustment, 00061 MemberPointerAdjustment 00062 } Kind; 00063 00064 00065 struct DTB { 00066 const CastExpr *BasePath; 00067 const CXXRecordDecl *DerivedClass; 00068 }; 00069 00070 struct P { 00071 const MemberPointerType *MPT; 00072 Expr *RHS; 00073 }; 00074 00075 union { 00076 struct DTB DerivedToBase; 00077 FieldDecl *Field; 00078 struct P Ptr; 00079 }; 00080 00081 SubobjectAdjustment(const CastExpr *BasePath, 00082 const CXXRecordDecl *DerivedClass) 00083 : Kind(DerivedToBaseAdjustment) { 00084 DerivedToBase.BasePath = BasePath; 00085 DerivedToBase.DerivedClass = DerivedClass; 00086 } 00087 00088 SubobjectAdjustment(FieldDecl *Field) 00089 : Kind(FieldAdjustment) { 00090 this->Field = Field; 00091 } 00092 00093 SubobjectAdjustment(const MemberPointerType *MPT, Expr *RHS) 00094 : Kind(MemberPointerAdjustment) { 00095 this->Ptr.MPT = MPT; 00096 this->Ptr.RHS = RHS; 00097 } 00098 }; 00099 00100 /// Expr - This represents one expression. Note that Expr's are subclasses of 00101 /// Stmt. This allows an expression to be transparently used any place a Stmt 00102 /// is required. 00103 /// 00104 class Expr : public Stmt { 00105 QualType TR; 00106 00107 protected: 00108 Expr(StmtClass SC, QualType T, ExprValueKind VK, ExprObjectKind OK, 00109 bool TD, bool VD, bool ID, bool ContainsUnexpandedParameterPack) 00110 : Stmt(SC) 00111 { 00112 ExprBits.TypeDependent = TD; 00113 ExprBits.ValueDependent = VD; 00114 ExprBits.InstantiationDependent = ID; 00115 ExprBits.ValueKind = VK; 00116 ExprBits.ObjectKind = OK; 00117 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 00118 setType(T); 00119 } 00120 00121 /// \brief Construct an empty expression. 00122 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { } 00123 00124 public: 00125 QualType getType() const { return TR; } 00126 void setType(QualType t) { 00127 // In C++, the type of an expression is always adjusted so that it 00128 // will not have reference type (C++ [expr]p6). Use 00129 // QualType::getNonReferenceType() to retrieve the non-reference 00130 // type. Additionally, inspect Expr::isLvalue to determine whether 00131 // an expression that is adjusted in this manner should be 00132 // considered an lvalue. 00133 assert((t.isNull() || !t->isReferenceType()) && 00134 "Expressions can't have reference type"); 00135 00136 TR = t; 00137 } 00138 00139 /// isValueDependent - Determines whether this expression is 00140 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 00141 /// array bound of "Chars" in the following example is 00142 /// value-dependent. 00143 /// @code 00144 /// template<int Size, char (&Chars)[Size]> struct meta_string; 00145 /// @endcode 00146 bool isValueDependent() const { return ExprBits.ValueDependent; } 00147 00148 /// \brief Set whether this expression is value-dependent or not. 00149 void setValueDependent(bool VD) { 00150 ExprBits.ValueDependent = VD; 00151 if (VD) 00152 ExprBits.InstantiationDependent = true; 00153 } 00154 00155 /// isTypeDependent - Determines whether this expression is 00156 /// type-dependent (C++ [temp.dep.expr]), which means that its type 00157 /// could change from one template instantiation to the next. For 00158 /// example, the expressions "x" and "x + y" are type-dependent in 00159 /// the following code, but "y" is not type-dependent: 00160 /// @code 00161 /// template<typename T> 00162 /// void add(T x, int y) { 00163 /// x + y; 00164 /// } 00165 /// @endcode 00166 bool isTypeDependent() const { return ExprBits.TypeDependent; } 00167 00168 /// \brief Set whether this expression is type-dependent or not. 00169 void setTypeDependent(bool TD) { 00170 ExprBits.TypeDependent = TD; 00171 if (TD) 00172 ExprBits.InstantiationDependent = true; 00173 } 00174 00175 /// \brief Whether this expression is instantiation-dependent, meaning that 00176 /// it depends in some way on a template parameter, even if neither its type 00177 /// nor (constant) value can change due to the template instantiation. 00178 /// 00179 /// In the following example, the expression \c sizeof(sizeof(T() + T())) is 00180 /// instantiation-dependent (since it involves a template parameter \c T), but 00181 /// is neither type- nor value-dependent, since the type of the inner 00182 /// \c sizeof is known (\c std::size_t) and therefore the size of the outer 00183 /// \c sizeof is known. 00184 /// 00185 /// \code 00186 /// template<typename T> 00187 /// void f(T x, T y) { 00188 /// sizeof(sizeof(T() + T()); 00189 /// } 00190 /// \endcode 00191 /// 00192 bool isInstantiationDependent() const { 00193 return ExprBits.InstantiationDependent; 00194 } 00195 00196 /// \brief Set whether this expression is instantiation-dependent or not. 00197 void setInstantiationDependent(bool ID) { 00198 ExprBits.InstantiationDependent = ID; 00199 } 00200 00201 /// \brief Whether this expression contains an unexpanded parameter 00202 /// pack (for C++11 variadic templates). 00203 /// 00204 /// Given the following function template: 00205 /// 00206 /// \code 00207 /// template<typename F, typename ...Types> 00208 /// void forward(const F &f, Types &&...args) { 00209 /// f(static_cast<Types&&>(args)...); 00210 /// } 00211 /// \endcode 00212 /// 00213 /// The expressions \c args and \c static_cast<Types&&>(args) both 00214 /// contain parameter packs. 00215 bool containsUnexpandedParameterPack() const { 00216 return ExprBits.ContainsUnexpandedParameterPack; 00217 } 00218 00219 /// \brief Set the bit that describes whether this expression 00220 /// contains an unexpanded parameter pack. 00221 void setContainsUnexpandedParameterPack(bool PP = true) { 00222 ExprBits.ContainsUnexpandedParameterPack = PP; 00223 } 00224 00225 /// getExprLoc - Return the preferred location for the arrow when diagnosing 00226 /// a problem with a generic expression. 00227 SourceLocation getExprLoc() const LLVM_READONLY; 00228 00229 /// isUnusedResultAWarning - Return true if this immediate expression should 00230 /// be warned about if the result is unused. If so, fill in expr, location, 00231 /// and ranges with expr to warn on and source locations/ranges appropriate 00232 /// for a warning. 00233 bool isUnusedResultAWarning(const Expr *&WarnExpr, SourceLocation &Loc, 00234 SourceRange &R1, SourceRange &R2, 00235 ASTContext &Ctx) const; 00236 00237 /// isLValue - True if this expression is an "l-value" according to 00238 /// the rules of the current language. C and C++ give somewhat 00239 /// different rules for this concept, but in general, the result of 00240 /// an l-value expression identifies a specific object whereas the 00241 /// result of an r-value expression is a value detached from any 00242 /// specific storage. 00243 /// 00244 /// C++11 divides the concept of "r-value" into pure r-values 00245 /// ("pr-values") and so-called expiring values ("x-values"), which 00246 /// identify specific objects that can be safely cannibalized for 00247 /// their resources. This is an unfortunate abuse of terminology on 00248 /// the part of the C++ committee. In Clang, when we say "r-value", 00249 /// we generally mean a pr-value. 00250 bool isLValue() const { return getValueKind() == VK_LValue; } 00251 bool isRValue() const { return getValueKind() == VK_RValue; } 00252 bool isXValue() const { return getValueKind() == VK_XValue; } 00253 bool isGLValue() const { return getValueKind() != VK_RValue; } 00254 00255 enum LValueClassification { 00256 LV_Valid, 00257 LV_NotObjectType, 00258 LV_IncompleteVoidType, 00259 LV_DuplicateVectorComponents, 00260 LV_InvalidExpression, 00261 LV_InvalidMessageExpression, 00262 LV_MemberFunction, 00263 LV_SubObjCPropertySetting, 00264 LV_ClassTemporary, 00265 LV_ArrayTemporary 00266 }; 00267 /// Reasons why an expression might not be an l-value. 00268 LValueClassification ClassifyLValue(ASTContext &Ctx) const; 00269 00270 enum isModifiableLvalueResult { 00271 MLV_Valid, 00272 MLV_NotObjectType, 00273 MLV_IncompleteVoidType, 00274 MLV_DuplicateVectorComponents, 00275 MLV_InvalidExpression, 00276 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 00277 MLV_IncompleteType, 00278 MLV_ConstQualified, 00279 MLV_ArrayType, 00280 MLV_NoSetterProperty, 00281 MLV_MemberFunction, 00282 MLV_SubObjCPropertySetting, 00283 MLV_InvalidMessageExpression, 00284 MLV_ClassTemporary, 00285 MLV_ArrayTemporary 00286 }; 00287 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 00288 /// does not have an incomplete type, does not have a const-qualified type, 00289 /// and if it is a structure or union, does not have any member (including, 00290 /// recursively, any member or element of all contained aggregates or unions) 00291 /// with a const-qualified type. 00292 /// 00293 /// \param Loc [in,out] - A source location which *may* be filled 00294 /// in with the location of the expression making this a 00295 /// non-modifiable lvalue, if specified. 00296 isModifiableLvalueResult 00297 isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc = nullptr) const; 00298 00299 /// \brief The return type of classify(). Represents the C++11 expression 00300 /// taxonomy. 00301 class Classification { 00302 public: 00303 /// \brief The various classification results. Most of these mean prvalue. 00304 enum Kinds { 00305 CL_LValue, 00306 CL_XValue, 00307 CL_Function, // Functions cannot be lvalues in C. 00308 CL_Void, // Void cannot be an lvalue in C. 00309 CL_AddressableVoid, // Void expression whose address can be taken in C. 00310 CL_DuplicateVectorComponents, // A vector shuffle with dupes. 00311 CL_MemberFunction, // An expression referring to a member function 00312 CL_SubObjCPropertySetting, 00313 CL_ClassTemporary, // A temporary of class type, or subobject thereof. 00314 CL_ArrayTemporary, // A temporary of array type. 00315 CL_ObjCMessageRValue, // ObjC message is an rvalue 00316 CL_PRValue // A prvalue for any other reason, of any other type 00317 }; 00318 /// \brief The results of modification testing. 00319 enum ModifiableType { 00320 CM_Untested, // testModifiable was false. 00321 CM_Modifiable, 00322 CM_RValue, // Not modifiable because it's an rvalue 00323 CM_Function, // Not modifiable because it's a function; C++ only 00324 CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext 00325 CM_NoSetterProperty,// Implicit assignment to ObjC property without setter 00326 CM_ConstQualified, 00327 CM_ArrayType, 00328 CM_IncompleteType 00329 }; 00330 00331 private: 00332 friend class Expr; 00333 00334 unsigned short Kind; 00335 unsigned short Modifiable; 00336 00337 explicit Classification(Kinds k, ModifiableType m) 00338 : Kind(k), Modifiable(m) 00339 {} 00340 00341 public: 00342 Classification() {} 00343 00344 Kinds getKind() const { return static_cast<Kinds>(Kind); } 00345 ModifiableType getModifiable() const { 00346 assert(Modifiable != CM_Untested && "Did not test for modifiability."); 00347 return static_cast<ModifiableType>(Modifiable); 00348 } 00349 bool isLValue() const { return Kind == CL_LValue; } 00350 bool isXValue() const { return Kind == CL_XValue; } 00351 bool isGLValue() const { return Kind <= CL_XValue; } 00352 bool isPRValue() const { return Kind >= CL_Function; } 00353 bool isRValue() const { return Kind >= CL_XValue; } 00354 bool isModifiable() const { return getModifiable() == CM_Modifiable; } 00355 00356 /// \brief Create a simple, modifiably lvalue 00357 static Classification makeSimpleLValue() { 00358 return Classification(CL_LValue, CM_Modifiable); 00359 } 00360 00361 }; 00362 /// \brief Classify - Classify this expression according to the C++11 00363 /// expression taxonomy. 00364 /// 00365 /// C++11 defines ([basic.lval]) a new taxonomy of expressions to replace the 00366 /// old lvalue vs rvalue. This function determines the type of expression this 00367 /// is. There are three expression types: 00368 /// - lvalues are classical lvalues as in C++03. 00369 /// - prvalues are equivalent to rvalues in C++03. 00370 /// - xvalues are expressions yielding unnamed rvalue references, e.g. a 00371 /// function returning an rvalue reference. 00372 /// lvalues and xvalues are collectively referred to as glvalues, while 00373 /// prvalues and xvalues together form rvalues. 00374 Classification Classify(ASTContext &Ctx) const { 00375 return ClassifyImpl(Ctx, nullptr); 00376 } 00377 00378 /// \brief ClassifyModifiable - Classify this expression according to the 00379 /// C++11 expression taxonomy, and see if it is valid on the left side 00380 /// of an assignment. 00381 /// 00382 /// This function extends classify in that it also tests whether the 00383 /// expression is modifiable (C99 6.3.2.1p1). 00384 /// \param Loc A source location that might be filled with a relevant location 00385 /// if the expression is not modifiable. 00386 Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{ 00387 return ClassifyImpl(Ctx, &Loc); 00388 } 00389 00390 /// getValueKindForType - Given a formal return or parameter type, 00391 /// give its value kind. 00392 static ExprValueKind getValueKindForType(QualType T) { 00393 if (const ReferenceType *RT = T->getAs<ReferenceType>()) 00394 return (isa<LValueReferenceType>(RT) 00395 ? VK_LValue 00396 : (RT->getPointeeType()->isFunctionType() 00397 ? VK_LValue : VK_XValue)); 00398 return VK_RValue; 00399 } 00400 00401 /// getValueKind - The value kind that this expression produces. 00402 ExprValueKind getValueKind() const { 00403 return static_cast<ExprValueKind>(ExprBits.ValueKind); 00404 } 00405 00406 /// getObjectKind - The object kind that this expression produces. 00407 /// Object kinds are meaningful only for expressions that yield an 00408 /// l-value or x-value. 00409 ExprObjectKind getObjectKind() const { 00410 return static_cast<ExprObjectKind>(ExprBits.ObjectKind); 00411 } 00412 00413 bool isOrdinaryOrBitFieldObject() const { 00414 ExprObjectKind OK = getObjectKind(); 00415 return (OK == OK_Ordinary || OK == OK_BitField); 00416 } 00417 00418 /// setValueKind - Set the value kind produced by this expression. 00419 void setValueKind(ExprValueKind Cat) { ExprBits.ValueKind = Cat; } 00420 00421 /// setObjectKind - Set the object kind produced by this expression. 00422 void setObjectKind(ExprObjectKind Cat) { ExprBits.ObjectKind = Cat; } 00423 00424 private: 00425 Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const; 00426 00427 public: 00428 00429 /// \brief Returns true if this expression is a gl-value that 00430 /// potentially refers to a bit-field. 00431 /// 00432 /// In C++, whether a gl-value refers to a bitfield is essentially 00433 /// an aspect of the value-kind type system. 00434 bool refersToBitField() const { return getObjectKind() == OK_BitField; } 00435 00436 /// \brief If this expression refers to a bit-field, retrieve the 00437 /// declaration of that bit-field. 00438 /// 00439 /// Note that this returns a non-null pointer in subtly different 00440 /// places than refersToBitField returns true. In particular, this can 00441 /// return a non-null pointer even for r-values loaded from 00442 /// bit-fields, but it will return null for a conditional bit-field. 00443 FieldDecl *getSourceBitField(); 00444 00445 const FieldDecl *getSourceBitField() const { 00446 return const_cast<Expr*>(this)->getSourceBitField(); 00447 } 00448 00449 /// \brief If this expression is an l-value for an Objective C 00450 /// property, find the underlying property reference expression. 00451 const ObjCPropertyRefExpr *getObjCProperty() const; 00452 00453 /// \brief Check if this expression is the ObjC 'self' implicit parameter. 00454 bool isObjCSelfExpr() const; 00455 00456 /// \brief Returns whether this expression refers to a vector element. 00457 bool refersToVectorElement() const; 00458 00459 /// \brief Returns whether this expression has a placeholder type. 00460 bool hasPlaceholderType() const { 00461 return getType()->isPlaceholderType(); 00462 } 00463 00464 /// \brief Returns whether this expression has a specific placeholder type. 00465 bool hasPlaceholderType(BuiltinType::Kind K) const { 00466 assert(BuiltinType::isPlaceholderTypeKind(K)); 00467 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getType())) 00468 return BT->getKind() == K; 00469 return false; 00470 } 00471 00472 /// isKnownToHaveBooleanValue - Return true if this is an integer expression 00473 /// that is known to return 0 or 1. This happens for _Bool/bool expressions 00474 /// but also int expressions which are produced by things like comparisons in 00475 /// C. 00476 bool isKnownToHaveBooleanValue() const; 00477 00478 /// isIntegerConstantExpr - Return true if this expression is a valid integer 00479 /// constant expression, and, if so, return its value in Result. If not a 00480 /// valid i-c-e, return false and fill in Loc (if specified) with the location 00481 /// of the invalid expression. 00482 /// 00483 /// Note: This does not perform the implicit conversions required by C++11 00484 /// [expr.const]p5. 00485 bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx, 00486 SourceLocation *Loc = nullptr, 00487 bool isEvaluated = true) const; 00488 bool isIntegerConstantExpr(const ASTContext &Ctx, 00489 SourceLocation *Loc = nullptr) const; 00490 00491 /// isCXX98IntegralConstantExpr - Return true if this expression is an 00492 /// integral constant expression in C++98. Can only be used in C++. 00493 bool isCXX98IntegralConstantExpr(const ASTContext &Ctx) const; 00494 00495 /// isCXX11ConstantExpr - Return true if this expression is a constant 00496 /// expression in C++11. Can only be used in C++. 00497 /// 00498 /// Note: This does not perform the implicit conversions required by C++11 00499 /// [expr.const]p5. 00500 bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result = nullptr, 00501 SourceLocation *Loc = nullptr) const; 00502 00503 /// isPotentialConstantExpr - Return true if this function's definition 00504 /// might be usable in a constant expression in C++11, if it were marked 00505 /// constexpr. Return false if the function can never produce a constant 00506 /// expression, along with diagnostics describing why not. 00507 static bool isPotentialConstantExpr(const FunctionDecl *FD, 00508 SmallVectorImpl< 00509 PartialDiagnosticAt> &Diags); 00510 00511 /// isPotentialConstantExprUnevaluted - Return true if this expression might 00512 /// be usable in a constant expression in C++11 in an unevaluated context, if 00513 /// it were in function FD marked constexpr. Return false if the function can 00514 /// never produce a constant expression, along with diagnostics describing 00515 /// why not. 00516 static bool isPotentialConstantExprUnevaluated(Expr *E, 00517 const FunctionDecl *FD, 00518 SmallVectorImpl< 00519 PartialDiagnosticAt> &Diags); 00520 00521 /// isConstantInitializer - Returns true if this expression can be emitted to 00522 /// IR as a constant, and thus can be used as a constant initializer in C. 00523 /// If this expression is not constant and Culprit is non-null, 00524 /// it is used to store the address of first non constant expr. 00525 bool isConstantInitializer(ASTContext &Ctx, bool ForRef, 00526 const Expr **Culprit = nullptr) const; 00527 00528 /// EvalStatus is a struct with detailed info about an evaluation in progress. 00529 struct EvalStatus { 00530 /// HasSideEffects - Whether the evaluated expression has side effects. 00531 /// For example, (f() && 0) can be folded, but it still has side effects. 00532 bool HasSideEffects; 00533 00534 /// Diag - If this is non-null, it will be filled in with a stack of notes 00535 /// indicating why evaluation failed (or why it failed to produce a constant 00536 /// expression). 00537 /// If the expression is unfoldable, the notes will indicate why it's not 00538 /// foldable. If the expression is foldable, but not a constant expression, 00539 /// the notes will describes why it isn't a constant expression. If the 00540 /// expression *is* a constant expression, no notes will be produced. 00541 SmallVectorImpl<PartialDiagnosticAt> *Diag; 00542 00543 EvalStatus() : HasSideEffects(false), Diag(nullptr) {} 00544 00545 // hasSideEffects - Return true if the evaluated expression has 00546 // side effects. 00547 bool hasSideEffects() const { 00548 return HasSideEffects; 00549 } 00550 }; 00551 00552 /// EvalResult is a struct with detailed info about an evaluated expression. 00553 struct EvalResult : EvalStatus { 00554 /// Val - This is the value the expression can be folded to. 00555 APValue Val; 00556 00557 // isGlobalLValue - Return true if the evaluated lvalue expression 00558 // is global. 00559 bool isGlobalLValue() const; 00560 }; 00561 00562 /// EvaluateAsRValue - Return true if this is a constant which we can fold to 00563 /// an rvalue using any crazy technique (that has nothing to do with language 00564 /// standards) that we want to, even if the expression has side-effects. If 00565 /// this function returns true, it returns the folded constant in Result. If 00566 /// the expression is a glvalue, an lvalue-to-rvalue conversion will be 00567 /// applied. 00568 bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const; 00569 00570 /// EvaluateAsBooleanCondition - Return true if this is a constant 00571 /// which we we can fold and convert to a boolean condition using 00572 /// any crazy technique that we want to, even if the expression has 00573 /// side-effects. 00574 bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx) const; 00575 00576 enum SideEffectsKind { SE_NoSideEffects, SE_AllowSideEffects }; 00577 00578 /// EvaluateAsInt - Return true if this is a constant which we can fold and 00579 /// convert to an integer, using any crazy technique that we want to. 00580 bool EvaluateAsInt(llvm::APSInt &Result, const ASTContext &Ctx, 00581 SideEffectsKind AllowSideEffects = SE_NoSideEffects) const; 00582 00583 /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be 00584 /// constant folded without side-effects, but discard the result. 00585 bool isEvaluatable(const ASTContext &Ctx) const; 00586 00587 /// HasSideEffects - This routine returns true for all those expressions 00588 /// which have any effect other than producing a value. Example is a function 00589 /// call, volatile variable read, or throwing an exception. 00590 bool HasSideEffects(const ASTContext &Ctx) const; 00591 00592 /// \brief Determine whether this expression involves a call to any function 00593 /// that is not trivial. 00594 bool hasNonTrivialCall(ASTContext &Ctx); 00595 00596 /// EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded 00597 /// integer. This must be called on an expression that constant folds to an 00598 /// integer. 00599 llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx, 00600 SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const; 00601 00602 void EvaluateForOverflow(const ASTContext &Ctx) const; 00603 00604 /// EvaluateAsLValue - Evaluate an expression to see if we can fold it to an 00605 /// lvalue with link time known address, with no side-effects. 00606 bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const; 00607 00608 /// EvaluateAsInitializer - Evaluate an expression as if it were the 00609 /// initializer of the given declaration. Returns true if the initializer 00610 /// can be folded to a constant, and produces any relevant notes. In C++11, 00611 /// notes will be produced if the expression is not a constant expression. 00612 bool EvaluateAsInitializer(APValue &Result, const ASTContext &Ctx, 00613 const VarDecl *VD, 00614 SmallVectorImpl<PartialDiagnosticAt> &Notes) const; 00615 00616 /// EvaluateWithSubstitution - Evaluate an expression as if from the context 00617 /// of a call to the given function with the given arguments, inside an 00618 /// unevaluated context. Returns true if the expression could be folded to a 00619 /// constant. 00620 bool EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, 00621 const FunctionDecl *Callee, 00622 ArrayRef<const Expr*> Args) const; 00623 00624 /// \brief Enumeration used to describe the kind of Null pointer constant 00625 /// returned from \c isNullPointerConstant(). 00626 enum NullPointerConstantKind { 00627 /// \brief Expression is not a Null pointer constant. 00628 NPCK_NotNull = 0, 00629 00630 /// \brief Expression is a Null pointer constant built from a zero integer 00631 /// expression that is not a simple, possibly parenthesized, zero literal. 00632 /// C++ Core Issue 903 will classify these expressions as "not pointers" 00633 /// once it is adopted. 00634 /// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 00635 NPCK_ZeroExpression, 00636 00637 /// \brief Expression is a Null pointer constant built from a literal zero. 00638 NPCK_ZeroLiteral, 00639 00640 /// \brief Expression is a C++11 nullptr. 00641 NPCK_CXX11_nullptr, 00642 00643 /// \brief Expression is a GNU-style __null constant. 00644 NPCK_GNUNull 00645 }; 00646 00647 /// \brief Enumeration used to describe how \c isNullPointerConstant() 00648 /// should cope with value-dependent expressions. 00649 enum NullPointerConstantValueDependence { 00650 /// \brief Specifies that the expression should never be value-dependent. 00651 NPC_NeverValueDependent = 0, 00652 00653 /// \brief Specifies that a value-dependent expression of integral or 00654 /// dependent type should be considered a null pointer constant. 00655 NPC_ValueDependentIsNull, 00656 00657 /// \brief Specifies that a value-dependent expression should be considered 00658 /// to never be a null pointer constant. 00659 NPC_ValueDependentIsNotNull 00660 }; 00661 00662 /// isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to 00663 /// a Null pointer constant. The return value can further distinguish the 00664 /// kind of NULL pointer constant that was detected. 00665 NullPointerConstantKind isNullPointerConstant( 00666 ASTContext &Ctx, 00667 NullPointerConstantValueDependence NPC) const; 00668 00669 /// isOBJCGCCandidate - Return true if this expression may be used in a read/ 00670 /// write barrier. 00671 bool isOBJCGCCandidate(ASTContext &Ctx) const; 00672 00673 /// \brief Returns true if this expression is a bound member function. 00674 bool isBoundMemberFunction(ASTContext &Ctx) const; 00675 00676 /// \brief Given an expression of bound-member type, find the type 00677 /// of the member. Returns null if this is an *overloaded* bound 00678 /// member expression. 00679 static QualType findBoundMemberType(const Expr *expr); 00680 00681 /// IgnoreImpCasts - Skip past any implicit casts which might 00682 /// surround this expression. Only skips ImplicitCastExprs. 00683 Expr *IgnoreImpCasts() LLVM_READONLY; 00684 00685 /// IgnoreImplicit - Skip past any implicit AST nodes which might 00686 /// surround this expression. 00687 Expr *IgnoreImplicit() LLVM_READONLY { 00688 return cast<Expr>(Stmt::IgnoreImplicit()); 00689 } 00690 00691 const Expr *IgnoreImplicit() const LLVM_READONLY { 00692 return const_cast<Expr*>(this)->IgnoreImplicit(); 00693 } 00694 00695 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 00696 /// its subexpression. If that subexpression is also a ParenExpr, 00697 /// then this method recursively returns its subexpression, and so forth. 00698 /// Otherwise, the method returns the current Expr. 00699 Expr *IgnoreParens() LLVM_READONLY; 00700 00701 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 00702 /// or CastExprs, returning their operand. 00703 Expr *IgnoreParenCasts() LLVM_READONLY; 00704 00705 /// Ignore casts. Strip off any CastExprs, returning their operand. 00706 Expr *IgnoreCasts() LLVM_READONLY; 00707 00708 /// IgnoreParenImpCasts - Ignore parentheses and implicit casts. Strip off 00709 /// any ParenExpr or ImplicitCastExprs, returning their operand. 00710 Expr *IgnoreParenImpCasts() LLVM_READONLY; 00711 00712 /// IgnoreConversionOperator - Ignore conversion operator. If this Expr is a 00713 /// call to a conversion operator, return the argument. 00714 Expr *IgnoreConversionOperator() LLVM_READONLY; 00715 00716 const Expr *IgnoreConversionOperator() const LLVM_READONLY { 00717 return const_cast<Expr*>(this)->IgnoreConversionOperator(); 00718 } 00719 00720 const Expr *IgnoreParenImpCasts() const LLVM_READONLY { 00721 return const_cast<Expr*>(this)->IgnoreParenImpCasts(); 00722 } 00723 00724 /// Ignore parentheses and lvalue casts. Strip off any ParenExpr and 00725 /// CastExprs that represent lvalue casts, returning their operand. 00726 Expr *IgnoreParenLValueCasts() LLVM_READONLY; 00727 00728 const Expr *IgnoreParenLValueCasts() const LLVM_READONLY { 00729 return const_cast<Expr*>(this)->IgnoreParenLValueCasts(); 00730 } 00731 00732 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the 00733 /// value (including ptr->int casts of the same size). Strip off any 00734 /// ParenExpr or CastExprs, returning their operand. 00735 Expr *IgnoreParenNoopCasts(ASTContext &Ctx) LLVM_READONLY; 00736 00737 /// Ignore parentheses and derived-to-base casts. 00738 Expr *ignoreParenBaseCasts() LLVM_READONLY; 00739 00740 const Expr *ignoreParenBaseCasts() const LLVM_READONLY { 00741 return const_cast<Expr*>(this)->ignoreParenBaseCasts(); 00742 } 00743 00744 /// \brief Determine whether this expression is a default function argument. 00745 /// 00746 /// Default arguments are implicitly generated in the abstract syntax tree 00747 /// by semantic analysis for function calls, object constructions, etc. in 00748 /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; 00749 /// this routine also looks through any implicit casts to determine whether 00750 /// the expression is a default argument. 00751 bool isDefaultArgument() const; 00752 00753 /// \brief Determine whether the result of this expression is a 00754 /// temporary object of the given class type. 00755 bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const; 00756 00757 /// \brief Whether this expression is an implicit reference to 'this' in C++. 00758 bool isImplicitCXXThis() const; 00759 00760 const Expr *IgnoreImpCasts() const LLVM_READONLY { 00761 return const_cast<Expr*>(this)->IgnoreImpCasts(); 00762 } 00763 const Expr *IgnoreParens() const LLVM_READONLY { 00764 return const_cast<Expr*>(this)->IgnoreParens(); 00765 } 00766 const Expr *IgnoreParenCasts() const LLVM_READONLY { 00767 return const_cast<Expr*>(this)->IgnoreParenCasts(); 00768 } 00769 /// Strip off casts, but keep parentheses. 00770 const Expr *IgnoreCasts() const LLVM_READONLY { 00771 return const_cast<Expr*>(this)->IgnoreCasts(); 00772 } 00773 00774 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const LLVM_READONLY { 00775 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx); 00776 } 00777 00778 static bool hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs); 00779 00780 /// \brief For an expression of class type or pointer to class type, 00781 /// return the most derived class decl the expression is known to refer to. 00782 /// 00783 /// If this expression is a cast, this method looks through it to find the 00784 /// most derived decl that can be inferred from the expression. 00785 /// This is valid because derived-to-base conversions have undefined 00786 /// behavior if the object isn't dynamically of the derived type. 00787 const CXXRecordDecl *getBestDynamicClassType() const; 00788 00789 /// Walk outwards from an expression we want to bind a reference to and 00790 /// find the expression whose lifetime needs to be extended. Record 00791 /// the LHSs of comma expressions and adjustments needed along the path. 00792 const Expr *skipRValueSubobjectAdjustments( 00793 SmallVectorImpl<const Expr *> &CommaLHS, 00794 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const; 00795 00796 static bool classof(const Stmt *T) { 00797 return T->getStmtClass() >= firstExprConstant && 00798 T->getStmtClass() <= lastExprConstant; 00799 } 00800 }; 00801 00802 00803 //===----------------------------------------------------------------------===// 00804 // Primary Expressions. 00805 //===----------------------------------------------------------------------===// 00806 00807 /// OpaqueValueExpr - An expression referring to an opaque object of a 00808 /// fixed type and value class. These don't correspond to concrete 00809 /// syntax; instead they're used to express operations (usually copy 00810 /// operations) on values whose source is generally obvious from 00811 /// context. 00812 class OpaqueValueExpr : public Expr { 00813 friend class ASTStmtReader; 00814 Expr *SourceExpr; 00815 SourceLocation Loc; 00816 00817 public: 00818 OpaqueValueExpr(SourceLocation Loc, QualType T, ExprValueKind VK, 00819 ExprObjectKind OK = OK_Ordinary, 00820 Expr *SourceExpr = nullptr) 00821 : Expr(OpaqueValueExprClass, T, VK, OK, 00822 T->isDependentType(), 00823 T->isDependentType() || 00824 (SourceExpr && SourceExpr->isValueDependent()), 00825 T->isInstantiationDependentType(), 00826 false), 00827 SourceExpr(SourceExpr), Loc(Loc) { 00828 } 00829 00830 /// Given an expression which invokes a copy constructor --- i.e. a 00831 /// CXXConstructExpr, possibly wrapped in an ExprWithCleanups --- 00832 /// find the OpaqueValueExpr that's the source of the construction. 00833 static const OpaqueValueExpr *findInCopyConstruct(const Expr *expr); 00834 00835 explicit OpaqueValueExpr(EmptyShell Empty) 00836 : Expr(OpaqueValueExprClass, Empty) { } 00837 00838 /// \brief Retrieve the location of this expression. 00839 SourceLocation getLocation() const { return Loc; } 00840 00841 SourceLocation getLocStart() const LLVM_READONLY { 00842 return SourceExpr ? SourceExpr->getLocStart() : Loc; 00843 } 00844 SourceLocation getLocEnd() const LLVM_READONLY { 00845 return SourceExpr ? SourceExpr->getLocEnd() : Loc; 00846 } 00847 SourceLocation getExprLoc() const LLVM_READONLY { 00848 if (SourceExpr) return SourceExpr->getExprLoc(); 00849 return Loc; 00850 } 00851 00852 child_range children() { return child_range(); } 00853 00854 /// The source expression of an opaque value expression is the 00855 /// expression which originally generated the value. This is 00856 /// provided as a convenience for analyses that don't wish to 00857 /// precisely model the execution behavior of the program. 00858 /// 00859 /// The source expression is typically set when building the 00860 /// expression which binds the opaque value expression in the first 00861 /// place. 00862 Expr *getSourceExpr() const { return SourceExpr; } 00863 00864 static bool classof(const Stmt *T) { 00865 return T->getStmtClass() == OpaqueValueExprClass; 00866 } 00867 }; 00868 00869 /// \brief A reference to a declared variable, function, enum, etc. 00870 /// [C99 6.5.1p2] 00871 /// 00872 /// This encodes all the information about how a declaration is referenced 00873 /// within an expression. 00874 /// 00875 /// There are several optional constructs attached to DeclRefExprs only when 00876 /// they apply in order to conserve memory. These are laid out past the end of 00877 /// the object, and flags in the DeclRefExprBitfield track whether they exist: 00878 /// 00879 /// DeclRefExprBits.HasQualifier: 00880 /// Specifies when this declaration reference expression has a C++ 00881 /// nested-name-specifier. 00882 /// DeclRefExprBits.HasFoundDecl: 00883 /// Specifies when this declaration reference expression has a record of 00884 /// a NamedDecl (different from the referenced ValueDecl) which was found 00885 /// during name lookup and/or overload resolution. 00886 /// DeclRefExprBits.HasTemplateKWAndArgsInfo: 00887 /// Specifies when this declaration reference expression has an explicit 00888 /// C++ template keyword and/or template argument list. 00889 /// DeclRefExprBits.RefersToEnclosingLocal 00890 /// Specifies when this declaration reference expression (validly) 00891 /// refers to a local variable from a different function. 00892 class DeclRefExpr : public Expr { 00893 /// \brief The declaration that we are referencing. 00894 ValueDecl *D; 00895 00896 /// \brief The location of the declaration name itself. 00897 SourceLocation Loc; 00898 00899 /// \brief Provides source/type location info for the declaration name 00900 /// embedded in D. 00901 DeclarationNameLoc DNLoc; 00902 00903 /// \brief Helper to retrieve the optional NestedNameSpecifierLoc. 00904 NestedNameSpecifierLoc &getInternalQualifierLoc() { 00905 assert(hasQualifier()); 00906 return *reinterpret_cast<NestedNameSpecifierLoc *>(this + 1); 00907 } 00908 00909 /// \brief Helper to retrieve the optional NestedNameSpecifierLoc. 00910 const NestedNameSpecifierLoc &getInternalQualifierLoc() const { 00911 return const_cast<DeclRefExpr *>(this)->getInternalQualifierLoc(); 00912 } 00913 00914 /// \brief Test whether there is a distinct FoundDecl attached to the end of 00915 /// this DRE. 00916 bool hasFoundDecl() const { return DeclRefExprBits.HasFoundDecl; } 00917 00918 /// \brief Helper to retrieve the optional NamedDecl through which this 00919 /// reference occurred. 00920 NamedDecl *&getInternalFoundDecl() { 00921 assert(hasFoundDecl()); 00922 if (hasQualifier()) 00923 return *reinterpret_cast<NamedDecl **>(&getInternalQualifierLoc() + 1); 00924 return *reinterpret_cast<NamedDecl **>(this + 1); 00925 } 00926 00927 /// \brief Helper to retrieve the optional NamedDecl through which this 00928 /// reference occurred. 00929 NamedDecl *getInternalFoundDecl() const { 00930 return const_cast<DeclRefExpr *>(this)->getInternalFoundDecl(); 00931 } 00932 00933 DeclRefExpr(const ASTContext &Ctx, 00934 NestedNameSpecifierLoc QualifierLoc, 00935 SourceLocation TemplateKWLoc, 00936 ValueDecl *D, bool refersToEnclosingLocal, 00937 const DeclarationNameInfo &NameInfo, 00938 NamedDecl *FoundD, 00939 const TemplateArgumentListInfo *TemplateArgs, 00940 QualType T, ExprValueKind VK); 00941 00942 /// \brief Construct an empty declaration reference expression. 00943 explicit DeclRefExpr(EmptyShell Empty) 00944 : Expr(DeclRefExprClass, Empty) { } 00945 00946 /// \brief Computes the type- and value-dependence flags for this 00947 /// declaration reference expression. 00948 void computeDependence(const ASTContext &C); 00949 00950 public: 00951 DeclRefExpr(ValueDecl *D, bool refersToEnclosingLocal, QualType T, 00952 ExprValueKind VK, SourceLocation L, 00953 const DeclarationNameLoc &LocInfo = DeclarationNameLoc()) 00954 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false), 00955 D(D), Loc(L), DNLoc(LocInfo) { 00956 DeclRefExprBits.HasQualifier = 0; 00957 DeclRefExprBits.HasTemplateKWAndArgsInfo = 0; 00958 DeclRefExprBits.HasFoundDecl = 0; 00959 DeclRefExprBits.HadMultipleCandidates = 0; 00960 DeclRefExprBits.RefersToEnclosingLocal = refersToEnclosingLocal; 00961 computeDependence(D->getASTContext()); 00962 } 00963 00964 static DeclRefExpr * 00965 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, 00966 SourceLocation TemplateKWLoc, ValueDecl *D, bool isEnclosingLocal, 00967 SourceLocation NameLoc, QualType T, ExprValueKind VK, 00968 NamedDecl *FoundD = nullptr, 00969 const TemplateArgumentListInfo *TemplateArgs = nullptr); 00970 00971 static DeclRefExpr * 00972 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, 00973 SourceLocation TemplateKWLoc, ValueDecl *D, bool isEnclosingLocal, 00974 const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK, 00975 NamedDecl *FoundD = nullptr, 00976 const TemplateArgumentListInfo *TemplateArgs = nullptr); 00977 00978 /// \brief Construct an empty declaration reference expression. 00979 static DeclRefExpr *CreateEmpty(const ASTContext &Context, 00980 bool HasQualifier, 00981 bool HasFoundDecl, 00982 bool HasTemplateKWAndArgsInfo, 00983 unsigned NumTemplateArgs); 00984 00985 ValueDecl *getDecl() { return D; } 00986 const ValueDecl *getDecl() const { return D; } 00987 void setDecl(ValueDecl *NewD) { D = NewD; } 00988 00989 DeclarationNameInfo getNameInfo() const { 00990 return DeclarationNameInfo(getDecl()->getDeclName(), Loc, DNLoc); 00991 } 00992 00993 SourceLocation getLocation() const { return Loc; } 00994 void setLocation(SourceLocation L) { Loc = L; } 00995 SourceLocation getLocStart() const LLVM_READONLY; 00996 SourceLocation getLocEnd() const LLVM_READONLY; 00997 00998 /// \brief Determine whether this declaration reference was preceded by a 00999 /// C++ nested-name-specifier, e.g., \c N::foo. 01000 bool hasQualifier() const { return DeclRefExprBits.HasQualifier; } 01001 01002 /// \brief If the name was qualified, retrieves the nested-name-specifier 01003 /// that precedes the name. Otherwise, returns NULL. 01004 NestedNameSpecifier *getQualifier() const { 01005 if (!hasQualifier()) 01006 return nullptr; 01007 01008 return getInternalQualifierLoc().getNestedNameSpecifier(); 01009 } 01010 01011 /// \brief If the name was qualified, retrieves the nested-name-specifier 01012 /// that precedes the name, with source-location information. 01013 NestedNameSpecifierLoc getQualifierLoc() const { 01014 if (!hasQualifier()) 01015 return NestedNameSpecifierLoc(); 01016 01017 return getInternalQualifierLoc(); 01018 } 01019 01020 /// \brief Get the NamedDecl through which this reference occurred. 01021 /// 01022 /// This Decl may be different from the ValueDecl actually referred to in the 01023 /// presence of using declarations, etc. It always returns non-NULL, and may 01024 /// simple return the ValueDecl when appropriate. 01025 NamedDecl *getFoundDecl() { 01026 return hasFoundDecl() ? getInternalFoundDecl() : D; 01027 } 01028 01029 /// \brief Get the NamedDecl through which this reference occurred. 01030 /// See non-const variant. 01031 const NamedDecl *getFoundDecl() const { 01032 return hasFoundDecl() ? getInternalFoundDecl() : D; 01033 } 01034 01035 bool hasTemplateKWAndArgsInfo() const { 01036 return DeclRefExprBits.HasTemplateKWAndArgsInfo; 01037 } 01038 01039 /// \brief Return the optional template keyword and arguments info. 01040 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 01041 if (!hasTemplateKWAndArgsInfo()) 01042 return nullptr; 01043 01044 if (hasFoundDecl()) 01045 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>( 01046 &getInternalFoundDecl() + 1); 01047 01048 if (hasQualifier()) 01049 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>( 01050 &getInternalQualifierLoc() + 1); 01051 01052 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>(this + 1); 01053 } 01054 01055 /// \brief Return the optional template keyword and arguments info. 01056 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 01057 return const_cast<DeclRefExpr*>(this)->getTemplateKWAndArgsInfo(); 01058 } 01059 01060 /// \brief Retrieve the location of the template keyword preceding 01061 /// this name, if any. 01062 SourceLocation getTemplateKeywordLoc() const { 01063 if (!hasTemplateKWAndArgsInfo()) return SourceLocation(); 01064 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 01065 } 01066 01067 /// \brief Retrieve the location of the left angle bracket starting the 01068 /// explicit template argument list following the name, if any. 01069 SourceLocation getLAngleLoc() const { 01070 if (!hasTemplateKWAndArgsInfo()) return SourceLocation(); 01071 return getTemplateKWAndArgsInfo()->LAngleLoc; 01072 } 01073 01074 /// \brief Retrieve the location of the right angle bracket ending the 01075 /// explicit template argument list following the name, if any. 01076 SourceLocation getRAngleLoc() const { 01077 if (!hasTemplateKWAndArgsInfo()) return SourceLocation(); 01078 return getTemplateKWAndArgsInfo()->RAngleLoc; 01079 } 01080 01081 /// \brief Determines whether the name in this declaration reference 01082 /// was preceded by the template keyword. 01083 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 01084 01085 /// \brief Determines whether this declaration reference was followed by an 01086 /// explicit template argument list. 01087 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 01088 01089 /// \brief Retrieve the explicit template argument list that followed the 01090 /// member template name. 01091 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 01092 assert(hasExplicitTemplateArgs()); 01093 return *getTemplateKWAndArgsInfo(); 01094 } 01095 01096 /// \brief Retrieve the explicit template argument list that followed the 01097 /// member template name. 01098 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 01099 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgs(); 01100 } 01101 01102 /// \brief Retrieves the optional explicit template arguments. 01103 /// This points to the same data as getExplicitTemplateArgs(), but 01104 /// returns null if there are no explicit template arguments. 01105 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 01106 if (!hasExplicitTemplateArgs()) return nullptr; 01107 return &getExplicitTemplateArgs(); 01108 } 01109 01110 /// \brief Copies the template arguments (if present) into the given 01111 /// structure. 01112 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 01113 if (hasExplicitTemplateArgs()) 01114 getExplicitTemplateArgs().copyInto(List); 01115 } 01116 01117 /// \brief Retrieve the template arguments provided as part of this 01118 /// template-id. 01119 const TemplateArgumentLoc *getTemplateArgs() const { 01120 if (!hasExplicitTemplateArgs()) 01121 return nullptr; 01122 01123 return getExplicitTemplateArgs().getTemplateArgs(); 01124 } 01125 01126 /// \brief Retrieve the number of template arguments provided as part of this 01127 /// template-id. 01128 unsigned getNumTemplateArgs() const { 01129 if (!hasExplicitTemplateArgs()) 01130 return 0; 01131 01132 return getExplicitTemplateArgs().NumTemplateArgs; 01133 } 01134 01135 /// \brief Returns true if this expression refers to a function that 01136 /// was resolved from an overloaded set having size greater than 1. 01137 bool hadMultipleCandidates() const { 01138 return DeclRefExprBits.HadMultipleCandidates; 01139 } 01140 /// \brief Sets the flag telling whether this expression refers to 01141 /// a function that was resolved from an overloaded set having size 01142 /// greater than 1. 01143 void setHadMultipleCandidates(bool V = true) { 01144 DeclRefExprBits.HadMultipleCandidates = V; 01145 } 01146 01147 /// Does this DeclRefExpr refer to a local declaration from an 01148 /// enclosing function scope? 01149 bool refersToEnclosingLocal() const { 01150 return DeclRefExprBits.RefersToEnclosingLocal; 01151 } 01152 01153 static bool classof(const Stmt *T) { 01154 return T->getStmtClass() == DeclRefExprClass; 01155 } 01156 01157 // Iterators 01158 child_range children() { return child_range(); } 01159 01160 friend class ASTStmtReader; 01161 friend class ASTStmtWriter; 01162 }; 01163 01164 /// \brief [C99 6.4.2.2] - A predefined identifier such as __func__. 01165 class PredefinedExpr : public Expr { 01166 public: 01167 enum IdentType { 01168 Func, 01169 Function, 01170 LFunction, // Same as Function, but as wide string. 01171 FuncDName, 01172 FuncSig, 01173 PrettyFunction, 01174 /// \brief The same as PrettyFunction, except that the 01175 /// 'virtual' keyword is omitted for virtual member functions. 01176 PrettyFunctionNoVirtual 01177 }; 01178 01179 private: 01180 SourceLocation Loc; 01181 IdentType Type; 01182 Stmt *FnName; 01183 01184 public: 01185 PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT, 01186 StringLiteral *SL); 01187 01188 /// \brief Construct an empty predefined expression. 01189 explicit PredefinedExpr(EmptyShell Empty) 01190 : Expr(PredefinedExprClass, Empty), Loc(), Type(Func), FnName(nullptr) {} 01191 01192 IdentType getIdentType() const { return Type; } 01193 01194 SourceLocation getLocation() const { return Loc; } 01195 void setLocation(SourceLocation L) { Loc = L; } 01196 01197 StringLiteral *getFunctionName(); 01198 const StringLiteral *getFunctionName() const { 01199 return const_cast<PredefinedExpr *>(this)->getFunctionName(); 01200 } 01201 01202 static StringRef getIdentTypeName(IdentType IT); 01203 static std::string ComputeName(IdentType IT, const Decl *CurrentDecl); 01204 01205 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 01206 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; } 01207 01208 static bool classof(const Stmt *T) { 01209 return T->getStmtClass() == PredefinedExprClass; 01210 } 01211 01212 // Iterators 01213 child_range children() { return child_range(&FnName, &FnName + 1); } 01214 01215 friend class ASTStmtReader; 01216 }; 01217 01218 /// \brief Used by IntegerLiteral/FloatingLiteral to store the numeric without 01219 /// leaking memory. 01220 /// 01221 /// For large floats/integers, APFloat/APInt will allocate memory from the heap 01222 /// to represent these numbers. Unfortunately, when we use a BumpPtrAllocator 01223 /// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with 01224 /// the APFloat/APInt values will never get freed. APNumericStorage uses 01225 /// ASTContext's allocator for memory allocation. 01226 class APNumericStorage { 01227 union { 01228 uint64_t VAL; ///< Used to store the <= 64 bits integer value. 01229 uint64_t *pVal; ///< Used to store the >64 bits integer value. 01230 }; 01231 unsigned BitWidth; 01232 01233 bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; } 01234 01235 APNumericStorage(const APNumericStorage &) LLVM_DELETED_FUNCTION; 01236 void operator=(const APNumericStorage &) LLVM_DELETED_FUNCTION; 01237 01238 protected: 01239 APNumericStorage() : VAL(0), BitWidth(0) { } 01240 01241 llvm::APInt getIntValue() const { 01242 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 01243 if (NumWords > 1) 01244 return llvm::APInt(BitWidth, NumWords, pVal); 01245 else 01246 return llvm::APInt(BitWidth, VAL); 01247 } 01248 void setIntValue(const ASTContext &C, const llvm::APInt &Val); 01249 }; 01250 01251 class APIntStorage : private APNumericStorage { 01252 public: 01253 llvm::APInt getValue() const { return getIntValue(); } 01254 void setValue(const ASTContext &C, const llvm::APInt &Val) { 01255 setIntValue(C, Val); 01256 } 01257 }; 01258 01259 class APFloatStorage : private APNumericStorage { 01260 public: 01261 llvm::APFloat getValue(const llvm::fltSemantics &Semantics) const { 01262 return llvm::APFloat(Semantics, getIntValue()); 01263 } 01264 void setValue(const ASTContext &C, const llvm::APFloat &Val) { 01265 setIntValue(C, Val.bitcastToAPInt()); 01266 } 01267 }; 01268 01269 class IntegerLiteral : public Expr, public APIntStorage { 01270 SourceLocation Loc; 01271 01272 /// \brief Construct an empty integer literal. 01273 explicit IntegerLiteral(EmptyShell Empty) 01274 : Expr(IntegerLiteralClass, Empty) { } 01275 01276 public: 01277 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 01278 // or UnsignedLongLongTy 01279 IntegerLiteral(const ASTContext &C, const llvm::APInt &V, QualType type, 01280 SourceLocation l); 01281 01282 /// \brief Returns a new integer literal with value 'V' and type 'type'. 01283 /// \param type - either IntTy, LongTy, LongLongTy, UnsignedIntTy, 01284 /// UnsignedLongTy, or UnsignedLongLongTy which should match the size of V 01285 /// \param V - the value that the returned integer literal contains. 01286 static IntegerLiteral *Create(const ASTContext &C, const llvm::APInt &V, 01287 QualType type, SourceLocation l); 01288 /// \brief Returns a new empty integer literal. 01289 static IntegerLiteral *Create(const ASTContext &C, EmptyShell Empty); 01290 01291 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 01292 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; } 01293 01294 /// \brief Retrieve the location of the literal. 01295 SourceLocation getLocation() const { return Loc; } 01296 01297 void setLocation(SourceLocation Location) { Loc = Location; } 01298 01299 static bool classof(const Stmt *T) { 01300 return T->getStmtClass() == IntegerLiteralClass; 01301 } 01302 01303 // Iterators 01304 child_range children() { return child_range(); } 01305 }; 01306 01307 class CharacterLiteral : public Expr { 01308 public: 01309 enum CharacterKind { 01310 Ascii, 01311 Wide, 01312 UTF16, 01313 UTF32 01314 }; 01315 01316 private: 01317 unsigned Value; 01318 SourceLocation Loc; 01319 public: 01320 // type should be IntTy 01321 CharacterLiteral(unsigned value, CharacterKind kind, QualType type, 01322 SourceLocation l) 01323 : Expr(CharacterLiteralClass, type, VK_RValue, OK_Ordinary, false, false, 01324 false, false), 01325 Value(value), Loc(l) { 01326 CharacterLiteralBits.Kind = kind; 01327 } 01328 01329 /// \brief Construct an empty character literal. 01330 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 01331 01332 SourceLocation getLocation() const { return Loc; } 01333 CharacterKind getKind() const { 01334 return static_cast<CharacterKind>(CharacterLiteralBits.Kind); 01335 } 01336 01337 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 01338 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; } 01339 01340 unsigned getValue() const { return Value; } 01341 01342 void setLocation(SourceLocation Location) { Loc = Location; } 01343 void setKind(CharacterKind kind) { CharacterLiteralBits.Kind = kind; } 01344 void setValue(unsigned Val) { Value = Val; } 01345 01346 static bool classof(const Stmt *T) { 01347 return T->getStmtClass() == CharacterLiteralClass; 01348 } 01349 01350 // Iterators 01351 child_range children() { return child_range(); } 01352 }; 01353 01354 class FloatingLiteral : public Expr, private APFloatStorage { 01355 SourceLocation Loc; 01356 01357 FloatingLiteral(const ASTContext &C, const llvm::APFloat &V, bool isexact, 01358 QualType Type, SourceLocation L); 01359 01360 /// \brief Construct an empty floating-point literal. 01361 explicit FloatingLiteral(const ASTContext &C, EmptyShell Empty); 01362 01363 public: 01364 static FloatingLiteral *Create(const ASTContext &C, const llvm::APFloat &V, 01365 bool isexact, QualType Type, SourceLocation L); 01366 static FloatingLiteral *Create(const ASTContext &C, EmptyShell Empty); 01367 01368 llvm::APFloat getValue() const { 01369 return APFloatStorage::getValue(getSemantics()); 01370 } 01371 void setValue(const ASTContext &C, const llvm::APFloat &Val) { 01372 assert(&getSemantics() == &Val.getSemantics() && "Inconsistent semantics"); 01373 APFloatStorage::setValue(C, Val); 01374 } 01375 01376 /// Get a raw enumeration value representing the floating-point semantics of 01377 /// this literal (32-bit IEEE, x87, ...), suitable for serialisation. 01378 APFloatSemantics getRawSemantics() const { 01379 return static_cast<APFloatSemantics>(FloatingLiteralBits.Semantics); 01380 } 01381 01382 /// Set the raw enumeration value representing the floating-point semantics of 01383 /// this literal (32-bit IEEE, x87, ...), suitable for serialisation. 01384 void setRawSemantics(APFloatSemantics Sem) { 01385 FloatingLiteralBits.Semantics = Sem; 01386 } 01387 01388 /// Return the APFloat semantics this literal uses. 01389 const llvm::fltSemantics &getSemantics() const; 01390 01391 /// Set the APFloat semantics this literal uses. 01392 void setSemantics(const llvm::fltSemantics &Sem); 01393 01394 bool isExact() const { return FloatingLiteralBits.IsExact; } 01395 void setExact(bool E) { FloatingLiteralBits.IsExact = E; } 01396 01397 /// getValueAsApproximateDouble - This returns the value as an inaccurate 01398 /// double. Note that this may cause loss of precision, but is useful for 01399 /// debugging dumps, etc. 01400 double getValueAsApproximateDouble() const; 01401 01402 SourceLocation getLocation() const { return Loc; } 01403 void setLocation(SourceLocation L) { Loc = L; } 01404 01405 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 01406 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; } 01407 01408 static bool classof(const Stmt *T) { 01409 return T->getStmtClass() == FloatingLiteralClass; 01410 } 01411 01412 // Iterators 01413 child_range children() { return child_range(); } 01414 }; 01415 01416 /// ImaginaryLiteral - We support imaginary integer and floating point literals, 01417 /// like "1.0i". We represent these as a wrapper around FloatingLiteral and 01418 /// IntegerLiteral classes. Instances of this class always have a Complex type 01419 /// whose element type matches the subexpression. 01420 /// 01421 class ImaginaryLiteral : public Expr { 01422 Stmt *Val; 01423 public: 01424 ImaginaryLiteral(Expr *val, QualType Ty) 01425 : Expr(ImaginaryLiteralClass, Ty, VK_RValue, OK_Ordinary, false, false, 01426 false, false), 01427 Val(val) {} 01428 01429 /// \brief Build an empty imaginary literal. 01430 explicit ImaginaryLiteral(EmptyShell Empty) 01431 : Expr(ImaginaryLiteralClass, Empty) { } 01432 01433 const Expr *getSubExpr() const { return cast<Expr>(Val); } 01434 Expr *getSubExpr() { return cast<Expr>(Val); } 01435 void setSubExpr(Expr *E) { Val = E; } 01436 01437 SourceLocation getLocStart() const LLVM_READONLY { return Val->getLocStart(); } 01438 SourceLocation getLocEnd() const LLVM_READONLY { return Val->getLocEnd(); } 01439 01440 static bool classof(const Stmt *T) { 01441 return T->getStmtClass() == ImaginaryLiteralClass; 01442 } 01443 01444 // Iterators 01445 child_range children() { return child_range(&Val, &Val+1); } 01446 }; 01447 01448 /// StringLiteral - This represents a string literal expression, e.g. "foo" 01449 /// or L"bar" (wide strings). The actual string is returned by getBytes() 01450 /// is NOT null-terminated, and the length of the string is determined by 01451 /// calling getByteLength(). The C type for a string is always a 01452 /// ConstantArrayType. In C++, the char type is const qualified, in C it is 01453 /// not. 01454 /// 01455 /// Note that strings in C can be formed by concatenation of multiple string 01456 /// literal pptokens in translation phase #6. This keeps track of the locations 01457 /// of each of these pieces. 01458 /// 01459 /// Strings in C can also be truncated and extended by assigning into arrays, 01460 /// e.g. with constructs like: 01461 /// char X[2] = "foobar"; 01462 /// In this case, getByteLength() will return 6, but the string literal will 01463 /// have type "char[2]". 01464 class StringLiteral : public Expr { 01465 public: 01466 enum StringKind { 01467 Ascii, 01468 Wide, 01469 UTF8, 01470 UTF16, 01471 UTF32 01472 }; 01473 01474 private: 01475 friend class ASTStmtReader; 01476 01477 union { 01478 const char *asChar; 01479 const uint16_t *asUInt16; 01480 const uint32_t *asUInt32; 01481 } StrData; 01482 unsigned Length; 01483 unsigned CharByteWidth : 4; 01484 unsigned Kind : 3; 01485 unsigned IsPascal : 1; 01486 unsigned NumConcatenated; 01487 SourceLocation TokLocs[1]; 01488 01489 StringLiteral(QualType Ty) : 01490 Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary, false, false, false, 01491 false) {} 01492 01493 static int mapCharByteWidth(TargetInfo const &target,StringKind k); 01494 01495 public: 01496 /// This is the "fully general" constructor that allows representation of 01497 /// strings formed from multiple concatenated tokens. 01498 static StringLiteral *Create(const ASTContext &C, StringRef Str, 01499 StringKind Kind, bool Pascal, QualType Ty, 01500 const SourceLocation *Loc, unsigned NumStrs); 01501 01502 /// Simple constructor for string literals made from one token. 01503 static StringLiteral *Create(const ASTContext &C, StringRef Str, 01504 StringKind Kind, bool Pascal, QualType Ty, 01505 SourceLocation Loc) { 01506 return Create(C, Str, Kind, Pascal, Ty, &Loc, 1); 01507 } 01508 01509 /// \brief Construct an empty string literal. 01510 static StringLiteral *CreateEmpty(const ASTContext &C, unsigned NumStrs); 01511 01512 StringRef getString() const { 01513 assert(CharByteWidth==1 01514 && "This function is used in places that assume strings use char"); 01515 return StringRef(StrData.asChar, getByteLength()); 01516 } 01517 01518 /// Allow access to clients that need the byte representation, such as 01519 /// ASTWriterStmt::VisitStringLiteral(). 01520 StringRef getBytes() const { 01521 // FIXME: StringRef may not be the right type to use as a result for this. 01522 if (CharByteWidth == 1) 01523 return StringRef(StrData.asChar, getByteLength()); 01524 if (CharByteWidth == 4) 01525 return StringRef(reinterpret_cast<const char*>(StrData.asUInt32), 01526 getByteLength()); 01527 assert(CharByteWidth == 2 && "unsupported CharByteWidth"); 01528 return StringRef(reinterpret_cast<const char*>(StrData.asUInt16), 01529 getByteLength()); 01530 } 01531 01532 void outputString(raw_ostream &OS) const; 01533 01534 uint32_t getCodeUnit(size_t i) const { 01535 assert(i < Length && "out of bounds access"); 01536 if (CharByteWidth == 1) 01537 return static_cast<unsigned char>(StrData.asChar[i]); 01538 if (CharByteWidth == 4) 01539 return StrData.asUInt32[i]; 01540 assert(CharByteWidth == 2 && "unsupported CharByteWidth"); 01541 return StrData.asUInt16[i]; 01542 } 01543 01544 unsigned getByteLength() const { return CharByteWidth*Length; } 01545 unsigned getLength() const { return Length; } 01546 unsigned getCharByteWidth() const { return CharByteWidth; } 01547 01548 /// \brief Sets the string data to the given string data. 01549 void setString(const ASTContext &C, StringRef Str, 01550 StringKind Kind, bool IsPascal); 01551 01552 StringKind getKind() const { return static_cast<StringKind>(Kind); } 01553 01554 01555 bool isAscii() const { return Kind == Ascii; } 01556 bool isWide() const { return Kind == Wide; } 01557 bool isUTF8() const { return Kind == UTF8; } 01558 bool isUTF16() const { return Kind == UTF16; } 01559 bool isUTF32() const { return Kind == UTF32; } 01560 bool isPascal() const { return IsPascal; } 01561 01562 bool containsNonAsciiOrNull() const { 01563 StringRef Str = getString(); 01564 for (unsigned i = 0, e = Str.size(); i != e; ++i) 01565 if (!isASCII(Str[i]) || !Str[i]) 01566 return true; 01567 return false; 01568 } 01569 01570 /// getNumConcatenated - Get the number of string literal tokens that were 01571 /// concatenated in translation phase #6 to form this string literal. 01572 unsigned getNumConcatenated() const { return NumConcatenated; } 01573 01574 SourceLocation getStrTokenLoc(unsigned TokNum) const { 01575 assert(TokNum < NumConcatenated && "Invalid tok number"); 01576 return TokLocs[TokNum]; 01577 } 01578 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 01579 assert(TokNum < NumConcatenated && "Invalid tok number"); 01580 TokLocs[TokNum] = L; 01581 } 01582 01583 /// getLocationOfByte - Return a source location that points to the specified 01584 /// byte of this string literal. 01585 /// 01586 /// Strings are amazingly complex. They can be formed from multiple tokens 01587 /// and can have escape sequences in them in addition to the usual trigraph 01588 /// and escaped newline business. This routine handles this complexity. 01589 /// 01590 SourceLocation getLocationOfByte(unsigned ByteNo, const SourceManager &SM, 01591 const LangOptions &Features, 01592 const TargetInfo &Target) const; 01593 01594 typedef const SourceLocation *tokloc_iterator; 01595 tokloc_iterator tokloc_begin() const { return TokLocs; } 01596 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 01597 01598 SourceLocation getLocStart() const LLVM_READONLY { return TokLocs[0]; } 01599 SourceLocation getLocEnd() const LLVM_READONLY { 01600 return TokLocs[NumConcatenated - 1]; 01601 } 01602 01603 static bool classof(const Stmt *T) { 01604 return T->getStmtClass() == StringLiteralClass; 01605 } 01606 01607 // Iterators 01608 child_range children() { return child_range(); } 01609 }; 01610 01611 /// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 01612 /// AST node is only formed if full location information is requested. 01613 class ParenExpr : public Expr { 01614 SourceLocation L, R; 01615 Stmt *Val; 01616 public: 01617 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 01618 : Expr(ParenExprClass, val->getType(), 01619 val->getValueKind(), val->getObjectKind(), 01620 val->isTypeDependent(), val->isValueDependent(), 01621 val->isInstantiationDependent(), 01622 val->containsUnexpandedParameterPack()), 01623 L(l), R(r), Val(val) {} 01624 01625 /// \brief Construct an empty parenthesized expression. 01626 explicit ParenExpr(EmptyShell Empty) 01627 : Expr(ParenExprClass, Empty) { } 01628 01629 const Expr *getSubExpr() const { return cast<Expr>(Val); } 01630 Expr *getSubExpr() { return cast<Expr>(Val); } 01631 void setSubExpr(Expr *E) { Val = E; } 01632 01633 SourceLocation getLocStart() const LLVM_READONLY { return L; } 01634 SourceLocation getLocEnd() const LLVM_READONLY { return R; } 01635 01636 /// \brief Get the location of the left parentheses '('. 01637 SourceLocation getLParen() const { return L; } 01638 void setLParen(SourceLocation Loc) { L = Loc; } 01639 01640 /// \brief Get the location of the right parentheses ')'. 01641 SourceLocation getRParen() const { return R; } 01642 void setRParen(SourceLocation Loc) { R = Loc; } 01643 01644 static bool classof(const Stmt *T) { 01645 return T->getStmtClass() == ParenExprClass; 01646 } 01647 01648 // Iterators 01649 child_range children() { return child_range(&Val, &Val+1); } 01650 }; 01651 01652 01653 /// UnaryOperator - This represents the unary-expression's (except sizeof and 01654 /// alignof), the postinc/postdec operators from postfix-expression, and various 01655 /// extensions. 01656 /// 01657 /// Notes on various nodes: 01658 /// 01659 /// Real/Imag - These return the real/imag part of a complex operand. If 01660 /// applied to a non-complex value, the former returns its operand and the 01661 /// later returns zero in the type of the operand. 01662 /// 01663 class UnaryOperator : public Expr { 01664 public: 01665 typedef UnaryOperatorKind Opcode; 01666 01667 private: 01668 unsigned Opc : 5; 01669 SourceLocation Loc; 01670 Stmt *Val; 01671 public: 01672 01673 UnaryOperator(Expr *input, Opcode opc, QualType type, 01674 ExprValueKind VK, ExprObjectKind OK, SourceLocation l) 01675 : Expr(UnaryOperatorClass, type, VK, OK, 01676 input->isTypeDependent() || type->isDependentType(), 01677 input->isValueDependent(), 01678 (input->isInstantiationDependent() || 01679 type->isInstantiationDependentType()), 01680 input->containsUnexpandedParameterPack()), 01681 Opc(opc), Loc(l), Val(input) {} 01682 01683 /// \brief Build an empty unary operator. 01684 explicit UnaryOperator(EmptyShell Empty) 01685 : Expr(UnaryOperatorClass, Empty), Opc(UO_AddrOf) { } 01686 01687 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 01688 void setOpcode(Opcode O) { Opc = O; } 01689 01690 Expr *getSubExpr() const { return cast<Expr>(Val); } 01691 void setSubExpr(Expr *E) { Val = E; } 01692 01693 /// getOperatorLoc - Return the location of the operator. 01694 SourceLocation getOperatorLoc() const { return Loc; } 01695 void setOperatorLoc(SourceLocation L) { Loc = L; } 01696 01697 /// isPostfix - Return true if this is a postfix operation, like x++. 01698 static bool isPostfix(Opcode Op) { 01699 return Op == UO_PostInc || Op == UO_PostDec; 01700 } 01701 01702 /// isPrefix - Return true if this is a prefix operation, like --x. 01703 static bool isPrefix(Opcode Op) { 01704 return Op == UO_PreInc || Op == UO_PreDec; 01705 } 01706 01707 bool isPrefix() const { return isPrefix(getOpcode()); } 01708 bool isPostfix() const { return isPostfix(getOpcode()); } 01709 01710 static bool isIncrementOp(Opcode Op) { 01711 return Op == UO_PreInc || Op == UO_PostInc; 01712 } 01713 bool isIncrementOp() const { 01714 return isIncrementOp(getOpcode()); 01715 } 01716 01717 static bool isDecrementOp(Opcode Op) { 01718 return Op == UO_PreDec || Op == UO_PostDec; 01719 } 01720 bool isDecrementOp() const { 01721 return isDecrementOp(getOpcode()); 01722 } 01723 01724 static bool isIncrementDecrementOp(Opcode Op) { return Op <= UO_PreDec; } 01725 bool isIncrementDecrementOp() const { 01726 return isIncrementDecrementOp(getOpcode()); 01727 } 01728 01729 static bool isArithmeticOp(Opcode Op) { 01730 return Op >= UO_Plus && Op <= UO_LNot; 01731 } 01732 bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); } 01733 01734 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 01735 /// corresponds to, e.g. "sizeof" or "[pre]++" 01736 static StringRef getOpcodeStr(Opcode Op); 01737 01738 /// \brief Retrieve the unary opcode that corresponds to the given 01739 /// overloaded operator. 01740 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 01741 01742 /// \brief Retrieve the overloaded operator kind that corresponds to 01743 /// the given unary opcode. 01744 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 01745 01746 SourceLocation getLocStart() const LLVM_READONLY { 01747 return isPostfix() ? Val->getLocStart() : Loc; 01748 } 01749 SourceLocation getLocEnd() const LLVM_READONLY { 01750 return isPostfix() ? Loc : Val->getLocEnd(); 01751 } 01752 SourceLocation getExprLoc() const LLVM_READONLY { return Loc; } 01753 01754 static bool classof(const Stmt *T) { 01755 return T->getStmtClass() == UnaryOperatorClass; 01756 } 01757 01758 // Iterators 01759 child_range children() { return child_range(&Val, &Val+1); } 01760 }; 01761 01762 /// OffsetOfExpr - [C99 7.17] - This represents an expression of the form 01763 /// offsetof(record-type, member-designator). For example, given: 01764 /// @code 01765 /// struct S { 01766 /// float f; 01767 /// double d; 01768 /// }; 01769 /// struct T { 01770 /// int i; 01771 /// struct S s[10]; 01772 /// }; 01773 /// @endcode 01774 /// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). 01775 01776 class OffsetOfExpr : public Expr { 01777 public: 01778 // __builtin_offsetof(type, identifier(.identifier|[expr])*) 01779 class OffsetOfNode { 01780 public: 01781 /// \brief The kind of offsetof node we have. 01782 enum Kind { 01783 /// \brief An index into an array. 01784 Array = 0x00, 01785 /// \brief A field. 01786 Field = 0x01, 01787 /// \brief A field in a dependent type, known only by its name. 01788 Identifier = 0x02, 01789 /// \brief An implicit indirection through a C++ base class, when the 01790 /// field found is in a base class. 01791 Base = 0x03 01792 }; 01793 01794 private: 01795 enum { MaskBits = 2, Mask = 0x03 }; 01796 01797 /// \brief The source range that covers this part of the designator. 01798 SourceRange Range; 01799 01800 /// \brief The data describing the designator, which comes in three 01801 /// different forms, depending on the lower two bits. 01802 /// - An unsigned index into the array of Expr*'s stored after this node 01803 /// in memory, for [constant-expression] designators. 01804 /// - A FieldDecl*, for references to a known field. 01805 /// - An IdentifierInfo*, for references to a field with a given name 01806 /// when the class type is dependent. 01807 /// - A CXXBaseSpecifier*, for references that look at a field in a 01808 /// base class. 01809 uintptr_t Data; 01810 01811 public: 01812 /// \brief Create an offsetof node that refers to an array element. 01813 OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, 01814 SourceLocation RBracketLoc) 01815 : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { } 01816 01817 /// \brief Create an offsetof node that refers to a field. 01818 OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, 01819 SourceLocation NameLoc) 01820 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 01821 Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { } 01822 01823 /// \brief Create an offsetof node that refers to an identifier. 01824 OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, 01825 SourceLocation NameLoc) 01826 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 01827 Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { } 01828 01829 /// \brief Create an offsetof node that refers into a C++ base class. 01830 explicit OffsetOfNode(const CXXBaseSpecifier *Base) 01831 : Range(), Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} 01832 01833 /// \brief Determine what kind of offsetof node this is. 01834 Kind getKind() const { 01835 return static_cast<Kind>(Data & Mask); 01836 } 01837 01838 /// \brief For an array element node, returns the index into the array 01839 /// of expressions. 01840 unsigned getArrayExprIndex() const { 01841 assert(getKind() == Array); 01842 return Data >> 2; 01843 } 01844 01845 /// \brief For a field offsetof node, returns the field. 01846 FieldDecl *getField() const { 01847 assert(getKind() == Field); 01848 return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); 01849 } 01850 01851 /// \brief For a field or identifier offsetof node, returns the name of 01852 /// the field. 01853 IdentifierInfo *getFieldName() const; 01854 01855 /// \brief For a base class node, returns the base specifier. 01856 CXXBaseSpecifier *getBase() const { 01857 assert(getKind() == Base); 01858 return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); 01859 } 01860 01861 /// \brief Retrieve the source range that covers this offsetof node. 01862 /// 01863 /// For an array element node, the source range contains the locations of 01864 /// the square brackets. For a field or identifier node, the source range 01865 /// contains the location of the period (if there is one) and the 01866 /// identifier. 01867 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 01868 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); } 01869 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); } 01870 }; 01871 01872 private: 01873 01874 SourceLocation OperatorLoc, RParenLoc; 01875 // Base type; 01876 TypeSourceInfo *TSInfo; 01877 // Number of sub-components (i.e. instances of OffsetOfNode). 01878 unsigned NumComps; 01879 // Number of sub-expressions (i.e. array subscript expressions). 01880 unsigned NumExprs; 01881 01882 OffsetOfExpr(const ASTContext &C, QualType type, 01883 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 01884 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs, 01885 SourceLocation RParenLoc); 01886 01887 explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) 01888 : Expr(OffsetOfExprClass, EmptyShell()), 01889 TSInfo(nullptr), NumComps(numComps), NumExprs(numExprs) {} 01890 01891 public: 01892 01893 static OffsetOfExpr *Create(const ASTContext &C, QualType type, 01894 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 01895 ArrayRef<OffsetOfNode> comps, 01896 ArrayRef<Expr*> exprs, SourceLocation RParenLoc); 01897 01898 static OffsetOfExpr *CreateEmpty(const ASTContext &C, 01899 unsigned NumComps, unsigned NumExprs); 01900 01901 /// getOperatorLoc - Return the location of the operator. 01902 SourceLocation getOperatorLoc() const { return OperatorLoc; } 01903 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 01904 01905 /// \brief Return the location of the right parentheses. 01906 SourceLocation getRParenLoc() const { return RParenLoc; } 01907 void setRParenLoc(SourceLocation R) { RParenLoc = R; } 01908 01909 TypeSourceInfo *getTypeSourceInfo() const { 01910 return TSInfo; 01911 } 01912 void setTypeSourceInfo(TypeSourceInfo *tsi) { 01913 TSInfo = tsi; 01914 } 01915 01916 const OffsetOfNode &getComponent(unsigned Idx) const { 01917 assert(Idx < NumComps && "Subscript out of range"); 01918 return reinterpret_cast<const OffsetOfNode *> (this + 1)[Idx]; 01919 } 01920 01921 void setComponent(unsigned Idx, OffsetOfNode ON) { 01922 assert(Idx < NumComps && "Subscript out of range"); 01923 reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON; 01924 } 01925 01926 unsigned getNumComponents() const { 01927 return NumComps; 01928 } 01929 01930 Expr* getIndexExpr(unsigned Idx) { 01931 assert(Idx < NumExprs && "Subscript out of range"); 01932 return reinterpret_cast<Expr **>( 01933 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx]; 01934 } 01935 const Expr *getIndexExpr(unsigned Idx) const { 01936 return const_cast<OffsetOfExpr*>(this)->getIndexExpr(Idx); 01937 } 01938 01939 void setIndexExpr(unsigned Idx, Expr* E) { 01940 assert(Idx < NumComps && "Subscript out of range"); 01941 reinterpret_cast<Expr **>( 01942 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E; 01943 } 01944 01945 unsigned getNumExpressions() const { 01946 return NumExprs; 01947 } 01948 01949 SourceLocation getLocStart() const LLVM_READONLY { return OperatorLoc; } 01950 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 01951 01952 static bool classof(const Stmt *T) { 01953 return T->getStmtClass() == OffsetOfExprClass; 01954 } 01955 01956 // Iterators 01957 child_range children() { 01958 Stmt **begin = 01959 reinterpret_cast<Stmt**>(reinterpret_cast<OffsetOfNode*>(this + 1) 01960 + NumComps); 01961 return child_range(begin, begin + NumExprs); 01962 } 01963 }; 01964 01965 /// UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) 01966 /// expression operand. Used for sizeof/alignof (C99 6.5.3.4) and 01967 /// vec_step (OpenCL 1.1 6.11.12). 01968 class UnaryExprOrTypeTraitExpr : public Expr { 01969 union { 01970 TypeSourceInfo *Ty; 01971 Stmt *Ex; 01972 } Argument; 01973 SourceLocation OpLoc, RParenLoc; 01974 01975 public: 01976 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, TypeSourceInfo *TInfo, 01977 QualType resultType, SourceLocation op, 01978 SourceLocation rp) : 01979 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 01980 false, // Never type-dependent (C++ [temp.dep.expr]p3). 01981 // Value-dependent if the argument is type-dependent. 01982 TInfo->getType()->isDependentType(), 01983 TInfo->getType()->isInstantiationDependentType(), 01984 TInfo->getType()->containsUnexpandedParameterPack()), 01985 OpLoc(op), RParenLoc(rp) { 01986 UnaryExprOrTypeTraitExprBits.Kind = ExprKind; 01987 UnaryExprOrTypeTraitExprBits.IsType = true; 01988 Argument.Ty = TInfo; 01989 } 01990 01991 UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, Expr *E, 01992 QualType resultType, SourceLocation op, 01993 SourceLocation rp) : 01994 Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, 01995 false, // Never type-dependent (C++ [temp.dep.expr]p3). 01996 // Value-dependent if the argument is type-dependent. 01997 E->isTypeDependent(), 01998 E->isInstantiationDependent(), 01999 E->containsUnexpandedParameterPack()), 02000 OpLoc(op), RParenLoc(rp) { 02001 UnaryExprOrTypeTraitExprBits.Kind = ExprKind; 02002 UnaryExprOrTypeTraitExprBits.IsType = false; 02003 Argument.Ex = E; 02004 } 02005 02006 /// \brief Construct an empty sizeof/alignof expression. 02007 explicit UnaryExprOrTypeTraitExpr(EmptyShell Empty) 02008 : Expr(UnaryExprOrTypeTraitExprClass, Empty) { } 02009 02010 UnaryExprOrTypeTrait getKind() const { 02011 return static_cast<UnaryExprOrTypeTrait>(UnaryExprOrTypeTraitExprBits.Kind); 02012 } 02013 void setKind(UnaryExprOrTypeTrait K) { UnaryExprOrTypeTraitExprBits.Kind = K;} 02014 02015 bool isArgumentType() const { return UnaryExprOrTypeTraitExprBits.IsType; } 02016 QualType getArgumentType() const { 02017 return getArgumentTypeInfo()->getType(); 02018 } 02019 TypeSourceInfo *getArgumentTypeInfo() const { 02020 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 02021 return Argument.Ty; 02022 } 02023 Expr *getArgumentExpr() { 02024 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 02025 return static_cast<Expr*>(Argument.Ex); 02026 } 02027 const Expr *getArgumentExpr() const { 02028 return const_cast<UnaryExprOrTypeTraitExpr*>(this)->getArgumentExpr(); 02029 } 02030 02031 void setArgument(Expr *E) { 02032 Argument.Ex = E; 02033 UnaryExprOrTypeTraitExprBits.IsType = false; 02034 } 02035 void setArgument(TypeSourceInfo *TInfo) { 02036 Argument.Ty = TInfo; 02037 UnaryExprOrTypeTraitExprBits.IsType = true; 02038 } 02039 02040 /// Gets the argument type, or the type of the argument expression, whichever 02041 /// is appropriate. 02042 QualType getTypeOfArgument() const { 02043 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 02044 } 02045 02046 SourceLocation getOperatorLoc() const { return OpLoc; } 02047 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 02048 02049 SourceLocation getRParenLoc() const { return RParenLoc; } 02050 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 02051 02052 SourceLocation getLocStart() const LLVM_READONLY { return OpLoc; } 02053 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 02054 02055 static bool classof(const Stmt *T) { 02056 return T->getStmtClass() == UnaryExprOrTypeTraitExprClass; 02057 } 02058 02059 // Iterators 02060 child_range children(); 02061 }; 02062 02063 //===----------------------------------------------------------------------===// 02064 // Postfix Operators. 02065 //===----------------------------------------------------------------------===// 02066 02067 /// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 02068 class ArraySubscriptExpr : public Expr { 02069 enum { LHS, RHS, END_EXPR=2 }; 02070 Stmt* SubExprs[END_EXPR]; 02071 SourceLocation RBracketLoc; 02072 public: 02073 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 02074 ExprValueKind VK, ExprObjectKind OK, 02075 SourceLocation rbracketloc) 02076 : Expr(ArraySubscriptExprClass, t, VK, OK, 02077 lhs->isTypeDependent() || rhs->isTypeDependent(), 02078 lhs->isValueDependent() || rhs->isValueDependent(), 02079 (lhs->isInstantiationDependent() || 02080 rhs->isInstantiationDependent()), 02081 (lhs->containsUnexpandedParameterPack() || 02082 rhs->containsUnexpandedParameterPack())), 02083 RBracketLoc(rbracketloc) { 02084 SubExprs[LHS] = lhs; 02085 SubExprs[RHS] = rhs; 02086 } 02087 02088 /// \brief Create an empty array subscript expression. 02089 explicit ArraySubscriptExpr(EmptyShell Shell) 02090 : Expr(ArraySubscriptExprClass, Shell) { } 02091 02092 /// An array access can be written A[4] or 4[A] (both are equivalent). 02093 /// - getBase() and getIdx() always present the normalized view: A[4]. 02094 /// In this case getBase() returns "A" and getIdx() returns "4". 02095 /// - getLHS() and getRHS() present the syntactic view. e.g. for 02096 /// 4[A] getLHS() returns "4". 02097 /// Note: Because vector element access is also written A[4] we must 02098 /// predicate the format conversion in getBase and getIdx only on the 02099 /// the type of the RHS, as it is possible for the LHS to be a vector of 02100 /// integer type 02101 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 02102 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 02103 void setLHS(Expr *E) { SubExprs[LHS] = E; } 02104 02105 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 02106 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 02107 void setRHS(Expr *E) { SubExprs[RHS] = E; } 02108 02109 Expr *getBase() { 02110 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 02111 } 02112 02113 const Expr *getBase() const { 02114 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 02115 } 02116 02117 Expr *getIdx() { 02118 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 02119 } 02120 02121 const Expr *getIdx() const { 02122 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 02123 } 02124 02125 SourceLocation getLocStart() const LLVM_READONLY { 02126 return getLHS()->getLocStart(); 02127 } 02128 SourceLocation getLocEnd() const LLVM_READONLY { return RBracketLoc; } 02129 02130 SourceLocation getRBracketLoc() const { return RBracketLoc; } 02131 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 02132 02133 SourceLocation getExprLoc() const LLVM_READONLY { 02134 return getBase()->getExprLoc(); 02135 } 02136 02137 static bool classof(const Stmt *T) { 02138 return T->getStmtClass() == ArraySubscriptExprClass; 02139 } 02140 02141 // Iterators 02142 child_range children() { 02143 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 02144 } 02145 }; 02146 02147 02148 /// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 02149 /// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 02150 /// while its subclasses may represent alternative syntax that (semantically) 02151 /// results in a function call. For example, CXXOperatorCallExpr is 02152 /// a subclass for overloaded operator calls that use operator syntax, e.g., 02153 /// "str1 + str2" to resolve to a function call. 02154 class CallExpr : public Expr { 02155 enum { FN=0, PREARGS_START=1 }; 02156 Stmt **SubExprs; 02157 unsigned NumArgs; 02158 SourceLocation RParenLoc; 02159 02160 protected: 02161 // These versions of the constructor are for derived classes. 02162 CallExpr(const ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs, 02163 ArrayRef<Expr*> args, QualType t, ExprValueKind VK, 02164 SourceLocation rparenloc); 02165 CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs, 02166 EmptyShell Empty); 02167 02168 Stmt *getPreArg(unsigned i) { 02169 assert(i < getNumPreArgs() && "Prearg access out of range!"); 02170 return SubExprs[PREARGS_START+i]; 02171 } 02172 const Stmt *getPreArg(unsigned i) const { 02173 assert(i < getNumPreArgs() && "Prearg access out of range!"); 02174 return SubExprs[PREARGS_START+i]; 02175 } 02176 void setPreArg(unsigned i, Stmt *PreArg) { 02177 assert(i < getNumPreArgs() && "Prearg access out of range!"); 02178 SubExprs[PREARGS_START+i] = PreArg; 02179 } 02180 02181 unsigned getNumPreArgs() const { return CallExprBits.NumPreArgs; } 02182 02183 public: 02184 CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args, QualType t, 02185 ExprValueKind VK, SourceLocation rparenloc); 02186 02187 /// \brief Build an empty call expression. 02188 CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty); 02189 02190 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 02191 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 02192 void setCallee(Expr *F) { SubExprs[FN] = F; } 02193 02194 Decl *getCalleeDecl(); 02195 const Decl *getCalleeDecl() const { 02196 return const_cast<CallExpr*>(this)->getCalleeDecl(); 02197 } 02198 02199 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 02200 FunctionDecl *getDirectCallee(); 02201 const FunctionDecl *getDirectCallee() const { 02202 return const_cast<CallExpr*>(this)->getDirectCallee(); 02203 } 02204 02205 /// getNumArgs - Return the number of actual arguments to this call. 02206 /// 02207 unsigned getNumArgs() const { return NumArgs; } 02208 02209 /// \brief Retrieve the call arguments. 02210 Expr **getArgs() { 02211 return reinterpret_cast<Expr **>(SubExprs+getNumPreArgs()+PREARGS_START); 02212 } 02213 const Expr *const *getArgs() const { 02214 return const_cast<CallExpr*>(this)->getArgs(); 02215 } 02216 02217 /// getArg - Return the specified argument. 02218 Expr *getArg(unsigned Arg) { 02219 assert(Arg < NumArgs && "Arg access out of range!"); 02220 return cast_or_null<Expr>(SubExprs[Arg + getNumPreArgs() + PREARGS_START]); 02221 } 02222 const Expr *getArg(unsigned Arg) const { 02223 assert(Arg < NumArgs && "Arg access out of range!"); 02224 return cast_or_null<Expr>(SubExprs[Arg + getNumPreArgs() + PREARGS_START]); 02225 } 02226 02227 /// setArg - Set the specified argument. 02228 void setArg(unsigned Arg, Expr *ArgExpr) { 02229 assert(Arg < NumArgs && "Arg access out of range!"); 02230 SubExprs[Arg+getNumPreArgs()+PREARGS_START] = ArgExpr; 02231 } 02232 02233 /// setNumArgs - This changes the number of arguments present in this call. 02234 /// Any orphaned expressions are deleted by this, and any new operands are set 02235 /// to null. 02236 void setNumArgs(const ASTContext& C, unsigned NumArgs); 02237 02238 typedef ExprIterator arg_iterator; 02239 typedef ConstExprIterator const_arg_iterator; 02240 typedef llvm::iterator_range<arg_iterator> arg_range; 02241 typedef llvm::iterator_range<const_arg_iterator> arg_const_range; 02242 02243 arg_range arguments() { return arg_range(arg_begin(), arg_end()); } 02244 arg_const_range arguments() const { 02245 return arg_const_range(arg_begin(), arg_end()); 02246 } 02247 02248 arg_iterator arg_begin() { return SubExprs+PREARGS_START+getNumPreArgs(); } 02249 arg_iterator arg_end() { 02250 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 02251 } 02252 const_arg_iterator arg_begin() const { 02253 return SubExprs+PREARGS_START+getNumPreArgs(); 02254 } 02255 const_arg_iterator arg_end() const { 02256 return SubExprs+PREARGS_START+getNumPreArgs()+getNumArgs(); 02257 } 02258 02259 /// This method provides fast access to all the subexpressions of 02260 /// a CallExpr without going through the slower virtual child_iterator 02261 /// interface. This provides efficient reverse iteration of the 02262 /// subexpressions. This is currently used for CFG construction. 02263 ArrayRef<Stmt*> getRawSubExprs() { 02264 return llvm::makeArrayRef(SubExprs, 02265 getNumPreArgs() + PREARGS_START + getNumArgs()); 02266 } 02267 02268 /// getNumCommas - Return the number of commas that must have been present in 02269 /// this function call. 02270 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 02271 02272 /// getBuiltinCallee - If this is a call to a builtin, return the builtin ID 02273 /// of the callee. If not, return 0. 02274 unsigned getBuiltinCallee() const; 02275 02276 /// \brief Returns \c true if this is a call to a builtin which does not 02277 /// evaluate side-effects within its arguments. 02278 bool isUnevaluatedBuiltinCall(ASTContext &Ctx) const; 02279 02280 /// getCallReturnType - Get the return type of the call expr. This is not 02281 /// always the type of the expr itself, if the return type is a reference 02282 /// type. 02283 QualType getCallReturnType() const; 02284 02285 SourceLocation getRParenLoc() const { return RParenLoc; } 02286 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 02287 02288 SourceLocation getLocStart() const LLVM_READONLY; 02289 SourceLocation getLocEnd() const LLVM_READONLY; 02290 02291 static bool classof(const Stmt *T) { 02292 return T->getStmtClass() >= firstCallExprConstant && 02293 T->getStmtClass() <= lastCallExprConstant; 02294 } 02295 02296 // Iterators 02297 child_range children() { 02298 return child_range(&SubExprs[0], 02299 &SubExprs[0]+NumArgs+getNumPreArgs()+PREARGS_START); 02300 } 02301 }; 02302 02303 /// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 02304 /// 02305 class MemberExpr : public Expr { 02306 /// Extra data stored in some member expressions. 02307 struct MemberNameQualifier { 02308 /// \brief The nested-name-specifier that qualifies the name, including 02309 /// source-location information. 02310 NestedNameSpecifierLoc QualifierLoc; 02311 02312 /// \brief The DeclAccessPair through which the MemberDecl was found due to 02313 /// name qualifiers. 02314 DeclAccessPair FoundDecl; 02315 }; 02316 02317 /// Base - the expression for the base pointer or structure references. In 02318 /// X.F, this is "X". 02319 Stmt *Base; 02320 02321 /// MemberDecl - This is the decl being referenced by the field/member name. 02322 /// In X.F, this is the decl referenced by F. 02323 ValueDecl *MemberDecl; 02324 02325 /// MemberDNLoc - Provides source/type location info for the 02326 /// declaration name embedded in MemberDecl. 02327 DeclarationNameLoc MemberDNLoc; 02328 02329 /// MemberLoc - This is the location of the member name. 02330 SourceLocation MemberLoc; 02331 02332 /// IsArrow - True if this is "X->F", false if this is "X.F". 02333 bool IsArrow : 1; 02334 02335 /// \brief True if this member expression used a nested-name-specifier to 02336 /// refer to the member, e.g., "x->Base::f", or found its member via a using 02337 /// declaration. When true, a MemberNameQualifier 02338 /// structure is allocated immediately after the MemberExpr. 02339 bool HasQualifierOrFoundDecl : 1; 02340 02341 /// \brief True if this member expression specified a template keyword 02342 /// and/or a template argument list explicitly, e.g., x->f<int>, 02343 /// x->template f, x->template f<int>. 02344 /// When true, an ASTTemplateKWAndArgsInfo structure and its 02345 /// TemplateArguments (if any) are allocated immediately after 02346 /// the MemberExpr or, if the member expression also has a qualifier, 02347 /// after the MemberNameQualifier structure. 02348 bool HasTemplateKWAndArgsInfo : 1; 02349 02350 /// \brief True if this member expression refers to a method that 02351 /// was resolved from an overloaded set having size greater than 1. 02352 bool HadMultipleCandidates : 1; 02353 02354 /// \brief Retrieve the qualifier that preceded the member name, if any. 02355 MemberNameQualifier *getMemberQualifier() { 02356 assert(HasQualifierOrFoundDecl); 02357 return reinterpret_cast<MemberNameQualifier *> (this + 1); 02358 } 02359 02360 /// \brief Retrieve the qualifier that preceded the member name, if any. 02361 const MemberNameQualifier *getMemberQualifier() const { 02362 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 02363 } 02364 02365 public: 02366 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 02367 const DeclarationNameInfo &NameInfo, QualType ty, 02368 ExprValueKind VK, ExprObjectKind OK) 02369 : Expr(MemberExprClass, ty, VK, OK, 02370 base->isTypeDependent(), 02371 base->isValueDependent(), 02372 base->isInstantiationDependent(), 02373 base->containsUnexpandedParameterPack()), 02374 Base(base), MemberDecl(memberdecl), MemberDNLoc(NameInfo.getInfo()), 02375 MemberLoc(NameInfo.getLoc()), IsArrow(isarrow), 02376 HasQualifierOrFoundDecl(false), HasTemplateKWAndArgsInfo(false), 02377 HadMultipleCandidates(false) { 02378 assert(memberdecl->getDeclName() == NameInfo.getName()); 02379 } 02380 02381 // NOTE: this constructor should be used only when it is known that 02382 // the member name can not provide additional syntactic info 02383 // (i.e., source locations for C++ operator names or type source info 02384 // for constructors, destructors and conversion operators). 02385 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 02386 SourceLocation l, QualType ty, 02387 ExprValueKind VK, ExprObjectKind OK) 02388 : Expr(MemberExprClass, ty, VK, OK, 02389 base->isTypeDependent(), base->isValueDependent(), 02390 base->isInstantiationDependent(), 02391 base->containsUnexpandedParameterPack()), 02392 Base(base), MemberDecl(memberdecl), MemberDNLoc(), MemberLoc(l), 02393 IsArrow(isarrow), 02394 HasQualifierOrFoundDecl(false), HasTemplateKWAndArgsInfo(false), 02395 HadMultipleCandidates(false) {} 02396 02397 static MemberExpr *Create(const ASTContext &C, Expr *base, bool isarrow, 02398 NestedNameSpecifierLoc QualifierLoc, 02399 SourceLocation TemplateKWLoc, 02400 ValueDecl *memberdecl, DeclAccessPair founddecl, 02401 DeclarationNameInfo MemberNameInfo, 02402 const TemplateArgumentListInfo *targs, 02403 QualType ty, ExprValueKind VK, ExprObjectKind OK); 02404 02405 void setBase(Expr *E) { Base = E; } 02406 Expr *getBase() const { return cast<Expr>(Base); } 02407 02408 /// \brief Retrieve the member declaration to which this expression refers. 02409 /// 02410 /// The returned declaration will either be a FieldDecl or (in C++) 02411 /// a CXXMethodDecl. 02412 ValueDecl *getMemberDecl() const { return MemberDecl; } 02413 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 02414 02415 /// \brief Retrieves the declaration found by lookup. 02416 DeclAccessPair getFoundDecl() const { 02417 if (!HasQualifierOrFoundDecl) 02418 return DeclAccessPair::make(getMemberDecl(), 02419 getMemberDecl()->getAccess()); 02420 return getMemberQualifier()->FoundDecl; 02421 } 02422 02423 /// \brief Determines whether this member expression actually had 02424 /// a C++ nested-name-specifier prior to the name of the member, e.g., 02425 /// x->Base::foo. 02426 bool hasQualifier() const { return getQualifier() != nullptr; } 02427 02428 /// \brief If the member name was qualified, retrieves the 02429 /// nested-name-specifier that precedes the member name. Otherwise, returns 02430 /// NULL. 02431 NestedNameSpecifier *getQualifier() const { 02432 if (!HasQualifierOrFoundDecl) 02433 return nullptr; 02434 02435 return getMemberQualifier()->QualifierLoc.getNestedNameSpecifier(); 02436 } 02437 02438 /// \brief If the member name was qualified, retrieves the 02439 /// nested-name-specifier that precedes the member name, with source-location 02440 /// information. 02441 NestedNameSpecifierLoc getQualifierLoc() const { 02442 if (!hasQualifier()) 02443 return NestedNameSpecifierLoc(); 02444 02445 return getMemberQualifier()->QualifierLoc; 02446 } 02447 02448 /// \brief Return the optional template keyword and arguments info. 02449 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 02450 if (!HasTemplateKWAndArgsInfo) 02451 return nullptr; 02452 02453 if (!HasQualifierOrFoundDecl) 02454 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>(this + 1); 02455 02456 return reinterpret_cast<ASTTemplateKWAndArgsInfo *>( 02457 getMemberQualifier() + 1); 02458 } 02459 02460 /// \brief Return the optional template keyword and arguments info. 02461 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 02462 return const_cast<MemberExpr*>(this)->getTemplateKWAndArgsInfo(); 02463 } 02464 02465 /// \brief Retrieve the location of the template keyword preceding 02466 /// the member name, if any. 02467 SourceLocation getTemplateKeywordLoc() const { 02468 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 02469 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 02470 } 02471 02472 /// \brief Retrieve the location of the left angle bracket starting the 02473 /// explicit template argument list following the member name, if any. 02474 SourceLocation getLAngleLoc() const { 02475 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 02476 return getTemplateKWAndArgsInfo()->LAngleLoc; 02477 } 02478 02479 /// \brief Retrieve the location of the right angle bracket ending the 02480 /// explicit template argument list following the member name, if any. 02481 SourceLocation getRAngleLoc() const { 02482 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 02483 return getTemplateKWAndArgsInfo()->RAngleLoc; 02484 } 02485 02486 /// Determines whether the member name was preceded by the template keyword. 02487 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 02488 02489 /// \brief Determines whether the member name was followed by an 02490 /// explicit template argument list. 02491 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 02492 02493 /// \brief Copies the template arguments (if present) into the given 02494 /// structure. 02495 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 02496 if (hasExplicitTemplateArgs()) 02497 getExplicitTemplateArgs().copyInto(List); 02498 } 02499 02500 /// \brief Retrieve the explicit template argument list that 02501 /// follow the member template name. This must only be called on an 02502 /// expression with explicit template arguments. 02503 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 02504 assert(hasExplicitTemplateArgs()); 02505 return *getTemplateKWAndArgsInfo(); 02506 } 02507 02508 /// \brief Retrieve the explicit template argument list that 02509 /// followed the member template name. This must only be called on 02510 /// an expression with explicit template arguments. 02511 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 02512 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgs(); 02513 } 02514 02515 /// \brief Retrieves the optional explicit template arguments. 02516 /// This points to the same data as getExplicitTemplateArgs(), but 02517 /// returns null if there are no explicit template arguments. 02518 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 02519 if (!hasExplicitTemplateArgs()) return nullptr; 02520 return &getExplicitTemplateArgs(); 02521 } 02522 02523 /// \brief Retrieve the template arguments provided as part of this 02524 /// template-id. 02525 const TemplateArgumentLoc *getTemplateArgs() const { 02526 if (!hasExplicitTemplateArgs()) 02527 return nullptr; 02528 02529 return getExplicitTemplateArgs().getTemplateArgs(); 02530 } 02531 02532 /// \brief Retrieve the number of template arguments provided as part of this 02533 /// template-id. 02534 unsigned getNumTemplateArgs() const { 02535 if (!hasExplicitTemplateArgs()) 02536 return 0; 02537 02538 return getExplicitTemplateArgs().NumTemplateArgs; 02539 } 02540 02541 /// \brief Retrieve the member declaration name info. 02542 DeclarationNameInfo getMemberNameInfo() const { 02543 return DeclarationNameInfo(MemberDecl->getDeclName(), 02544 MemberLoc, MemberDNLoc); 02545 } 02546 02547 bool isArrow() const { return IsArrow; } 02548 void setArrow(bool A) { IsArrow = A; } 02549 02550 /// getMemberLoc - Return the location of the "member", in X->F, it is the 02551 /// location of 'F'. 02552 SourceLocation getMemberLoc() const { return MemberLoc; } 02553 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 02554 02555 SourceLocation getLocStart() const LLVM_READONLY; 02556 SourceLocation getLocEnd() const LLVM_READONLY; 02557 02558 SourceLocation getExprLoc() const LLVM_READONLY { return MemberLoc; } 02559 02560 /// \brief Determine whether the base of this explicit is implicit. 02561 bool isImplicitAccess() const { 02562 return getBase() && getBase()->isImplicitCXXThis(); 02563 } 02564 02565 /// \brief Returns true if this member expression refers to a method that 02566 /// was resolved from an overloaded set having size greater than 1. 02567 bool hadMultipleCandidates() const { 02568 return HadMultipleCandidates; 02569 } 02570 /// \brief Sets the flag telling whether this expression refers to 02571 /// a method that was resolved from an overloaded set having size 02572 /// greater than 1. 02573 void setHadMultipleCandidates(bool V = true) { 02574 HadMultipleCandidates = V; 02575 } 02576 02577 static bool classof(const Stmt *T) { 02578 return T->getStmtClass() == MemberExprClass; 02579 } 02580 02581 // Iterators 02582 child_range children() { return child_range(&Base, &Base+1); } 02583 02584 friend class ASTReader; 02585 friend class ASTStmtWriter; 02586 }; 02587 02588 /// CompoundLiteralExpr - [C99 6.5.2.5] 02589 /// 02590 class CompoundLiteralExpr : public Expr { 02591 /// LParenLoc - If non-null, this is the location of the left paren in a 02592 /// compound literal like "(int){4}". This can be null if this is a 02593 /// synthesized compound expression. 02594 SourceLocation LParenLoc; 02595 02596 /// The type as written. This can be an incomplete array type, in 02597 /// which case the actual expression type will be different. 02598 /// The int part of the pair stores whether this expr is file scope. 02599 llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfoAndScope; 02600 Stmt *Init; 02601 public: 02602 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, 02603 QualType T, ExprValueKind VK, Expr *init, bool fileScope) 02604 : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary, 02605 tinfo->getType()->isDependentType(), 02606 init->isValueDependent(), 02607 (init->isInstantiationDependent() || 02608 tinfo->getType()->isInstantiationDependentType()), 02609 init->containsUnexpandedParameterPack()), 02610 LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) {} 02611 02612 /// \brief Construct an empty compound literal. 02613 explicit CompoundLiteralExpr(EmptyShell Empty) 02614 : Expr(CompoundLiteralExprClass, Empty) { } 02615 02616 const Expr *getInitializer() const { return cast<Expr>(Init); } 02617 Expr *getInitializer() { return cast<Expr>(Init); } 02618 void setInitializer(Expr *E) { Init = E; } 02619 02620 bool isFileScope() const { return TInfoAndScope.getInt(); } 02621 void setFileScope(bool FS) { TInfoAndScope.setInt(FS); } 02622 02623 SourceLocation getLParenLoc() const { return LParenLoc; } 02624 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 02625 02626 TypeSourceInfo *getTypeSourceInfo() const { 02627 return TInfoAndScope.getPointer(); 02628 } 02629 void setTypeSourceInfo(TypeSourceInfo *tinfo) { 02630 TInfoAndScope.setPointer(tinfo); 02631 } 02632 02633 SourceLocation getLocStart() const LLVM_READONLY { 02634 // FIXME: Init should never be null. 02635 if (!Init) 02636 return SourceLocation(); 02637 if (LParenLoc.isInvalid()) 02638 return Init->getLocStart(); 02639 return LParenLoc; 02640 } 02641 SourceLocation getLocEnd() const LLVM_READONLY { 02642 // FIXME: Init should never be null. 02643 if (!Init) 02644 return SourceLocation(); 02645 return Init->getLocEnd(); 02646 } 02647 02648 static bool classof(const Stmt *T) { 02649 return T->getStmtClass() == CompoundLiteralExprClass; 02650 } 02651 02652 // Iterators 02653 child_range children() { return child_range(&Init, &Init+1); } 02654 }; 02655 02656 /// CastExpr - Base class for type casts, including both implicit 02657 /// casts (ImplicitCastExpr) and explicit casts that have some 02658 /// representation in the source code (ExplicitCastExpr's derived 02659 /// classes). 02660 class CastExpr : public Expr { 02661 private: 02662 Stmt *Op; 02663 02664 bool CastConsistency() const; 02665 02666 const CXXBaseSpecifier * const *path_buffer() const { 02667 return const_cast<CastExpr*>(this)->path_buffer(); 02668 } 02669 CXXBaseSpecifier **path_buffer(); 02670 02671 void setBasePathSize(unsigned basePathSize) { 02672 CastExprBits.BasePathSize = basePathSize; 02673 assert(CastExprBits.BasePathSize == basePathSize && 02674 "basePathSize doesn't fit in bits of CastExprBits.BasePathSize!"); 02675 } 02676 02677 protected: 02678 CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, const CastKind kind, 02679 Expr *op, unsigned BasePathSize) 02680 : Expr(SC, ty, VK, OK_Ordinary, 02681 // Cast expressions are type-dependent if the type is 02682 // dependent (C++ [temp.dep.expr]p3). 02683 ty->isDependentType(), 02684 // Cast expressions are value-dependent if the type is 02685 // dependent or if the subexpression is value-dependent. 02686 ty->isDependentType() || (op && op->isValueDependent()), 02687 (ty->isInstantiationDependentType() || 02688 (op && op->isInstantiationDependent())), 02689 // An implicit cast expression doesn't (lexically) contain an 02690 // unexpanded pack, even if its target type does. 02691 ((SC != ImplicitCastExprClass && 02692 ty->containsUnexpandedParameterPack()) || 02693 (op && op->containsUnexpandedParameterPack()))), 02694 Op(op) { 02695 assert(kind != CK_Invalid && "creating cast with invalid cast kind"); 02696 CastExprBits.Kind = kind; 02697 setBasePathSize(BasePathSize); 02698 assert(CastConsistency()); 02699 } 02700 02701 /// \brief Construct an empty cast. 02702 CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize) 02703 : Expr(SC, Empty) { 02704 setBasePathSize(BasePathSize); 02705 } 02706 02707 public: 02708 CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } 02709 void setCastKind(CastKind K) { CastExprBits.Kind = K; } 02710 const char *getCastKindName() const; 02711 02712 Expr *getSubExpr() { return cast<Expr>(Op); } 02713 const Expr *getSubExpr() const { return cast<Expr>(Op); } 02714 void setSubExpr(Expr *E) { Op = E; } 02715 02716 /// \brief Retrieve the cast subexpression as it was written in the source 02717 /// code, looking through any implicit casts or other intermediate nodes 02718 /// introduced by semantic analysis. 02719 Expr *getSubExprAsWritten(); 02720 const Expr *getSubExprAsWritten() const { 02721 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 02722 } 02723 02724 typedef CXXBaseSpecifier **path_iterator; 02725 typedef const CXXBaseSpecifier * const *path_const_iterator; 02726 bool path_empty() const { return CastExprBits.BasePathSize == 0; } 02727 unsigned path_size() const { return CastExprBits.BasePathSize; } 02728 path_iterator path_begin() { return path_buffer(); } 02729 path_iterator path_end() { return path_buffer() + path_size(); } 02730 path_const_iterator path_begin() const { return path_buffer(); } 02731 path_const_iterator path_end() const { return path_buffer() + path_size(); } 02732 02733 void setCastPath(const CXXCastPath &Path); 02734 02735 static bool classof(const Stmt *T) { 02736 return T->getStmtClass() >= firstCastExprConstant && 02737 T->getStmtClass() <= lastCastExprConstant; 02738 } 02739 02740 // Iterators 02741 child_range children() { return child_range(&Op, &Op+1); } 02742 }; 02743 02744 /// ImplicitCastExpr - Allows us to explicitly represent implicit type 02745 /// conversions, which have no direct representation in the original 02746 /// source code. For example: converting T[]->T*, void f()->void 02747 /// (*f)(), float->double, short->int, etc. 02748 /// 02749 /// In C, implicit casts always produce rvalues. However, in C++, an 02750 /// implicit cast whose result is being bound to a reference will be 02751 /// an lvalue or xvalue. For example: 02752 /// 02753 /// @code 02754 /// class Base { }; 02755 /// class Derived : public Base { }; 02756 /// Derived &&ref(); 02757 /// void f(Derived d) { 02758 /// Base& b = d; // initializer is an ImplicitCastExpr 02759 /// // to an lvalue of type Base 02760 /// Base&& r = ref(); // initializer is an ImplicitCastExpr 02761 /// // to an xvalue of type Base 02762 /// } 02763 /// @endcode 02764 class ImplicitCastExpr : public CastExpr { 02765 private: 02766 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 02767 unsigned BasePathLength, ExprValueKind VK) 02768 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength) { 02769 } 02770 02771 /// \brief Construct an empty implicit cast. 02772 explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize) 02773 : CastExpr(ImplicitCastExprClass, Shell, PathSize) { } 02774 02775 public: 02776 enum OnStack_t { OnStack }; 02777 ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, 02778 ExprValueKind VK) 02779 : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0) { 02780 } 02781 02782 static ImplicitCastExpr *Create(const ASTContext &Context, QualType T, 02783 CastKind Kind, Expr *Operand, 02784 const CXXCastPath *BasePath, 02785 ExprValueKind Cat); 02786 02787 static ImplicitCastExpr *CreateEmpty(const ASTContext &Context, 02788 unsigned PathSize); 02789 02790 SourceLocation getLocStart() const LLVM_READONLY { 02791 return getSubExpr()->getLocStart(); 02792 } 02793 SourceLocation getLocEnd() const LLVM_READONLY { 02794 return getSubExpr()->getLocEnd(); 02795 } 02796 02797 static bool classof(const Stmt *T) { 02798 return T->getStmtClass() == ImplicitCastExprClass; 02799 } 02800 }; 02801 02802 inline Expr *Expr::IgnoreImpCasts() { 02803 Expr *e = this; 02804 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) 02805 e = ice->getSubExpr(); 02806 return e; 02807 } 02808 02809 /// ExplicitCastExpr - An explicit cast written in the source 02810 /// code. 02811 /// 02812 /// This class is effectively an abstract class, because it provides 02813 /// the basic representation of an explicitly-written cast without 02814 /// specifying which kind of cast (C cast, functional cast, static 02815 /// cast, etc.) was written; specific derived classes represent the 02816 /// particular style of cast and its location information. 02817 /// 02818 /// Unlike implicit casts, explicit cast nodes have two different 02819 /// types: the type that was written into the source code, and the 02820 /// actual type of the expression as determined by semantic 02821 /// analysis. These types may differ slightly. For example, in C++ one 02822 /// can cast to a reference type, which indicates that the resulting 02823 /// expression will be an lvalue or xvalue. The reference type, however, 02824 /// will not be used as the type of the expression. 02825 class ExplicitCastExpr : public CastExpr { 02826 /// TInfo - Source type info for the (written) type 02827 /// this expression is casting to. 02828 TypeSourceInfo *TInfo; 02829 02830 protected: 02831 ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, 02832 CastKind kind, Expr *op, unsigned PathSize, 02833 TypeSourceInfo *writtenTy) 02834 : CastExpr(SC, exprTy, VK, kind, op, PathSize), TInfo(writtenTy) {} 02835 02836 /// \brief Construct an empty explicit cast. 02837 ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 02838 : CastExpr(SC, Shell, PathSize) { } 02839 02840 public: 02841 /// getTypeInfoAsWritten - Returns the type source info for the type 02842 /// that this expression is casting to. 02843 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 02844 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 02845 02846 /// getTypeAsWritten - Returns the type that this expression is 02847 /// casting to, as written in the source code. 02848 QualType getTypeAsWritten() const { return TInfo->getType(); } 02849 02850 static bool classof(const Stmt *T) { 02851 return T->getStmtClass() >= firstExplicitCastExprConstant && 02852 T->getStmtClass() <= lastExplicitCastExprConstant; 02853 } 02854 }; 02855 02856 /// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 02857 /// cast in C++ (C++ [expr.cast]), which uses the syntax 02858 /// (Type)expr. For example: @c (int)f. 02859 class CStyleCastExpr : public ExplicitCastExpr { 02860 SourceLocation LPLoc; // the location of the left paren 02861 SourceLocation RPLoc; // the location of the right paren 02862 02863 CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, 02864 unsigned PathSize, TypeSourceInfo *writtenTy, 02865 SourceLocation l, SourceLocation r) 02866 : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, 02867 writtenTy), LPLoc(l), RPLoc(r) {} 02868 02869 /// \brief Construct an empty C-style explicit cast. 02870 explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize) 02871 : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { } 02872 02873 public: 02874 static CStyleCastExpr *Create(const ASTContext &Context, QualType T, 02875 ExprValueKind VK, CastKind K, 02876 Expr *Op, const CXXCastPath *BasePath, 02877 TypeSourceInfo *WrittenTy, SourceLocation L, 02878 SourceLocation R); 02879 02880 static CStyleCastExpr *CreateEmpty(const ASTContext &Context, 02881 unsigned PathSize); 02882 02883 SourceLocation getLParenLoc() const { return LPLoc; } 02884 void setLParenLoc(SourceLocation L) { LPLoc = L; } 02885 02886 SourceLocation getRParenLoc() const { return RPLoc; } 02887 void setRParenLoc(SourceLocation L) { RPLoc = L; } 02888 02889 SourceLocation getLocStart() const LLVM_READONLY { return LPLoc; } 02890 SourceLocation getLocEnd() const LLVM_READONLY { 02891 return getSubExpr()->getLocEnd(); 02892 } 02893 02894 static bool classof(const Stmt *T) { 02895 return T->getStmtClass() == CStyleCastExprClass; 02896 } 02897 }; 02898 02899 /// \brief A builtin binary operation expression such as "x + y" or "x <= y". 02900 /// 02901 /// This expression node kind describes a builtin binary operation, 02902 /// such as "x + y" for integer values "x" and "y". The operands will 02903 /// already have been converted to appropriate types (e.g., by 02904 /// performing promotions or conversions). 02905 /// 02906 /// In C++, where operators may be overloaded, a different kind of 02907 /// expression node (CXXOperatorCallExpr) is used to express the 02908 /// invocation of an overloaded operator with operator syntax. Within 02909 /// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 02910 /// used to store an expression "x + y" depends on the subexpressions 02911 /// for x and y. If neither x or y is type-dependent, and the "+" 02912 /// operator resolves to a built-in operation, BinaryOperator will be 02913 /// used to express the computation (x and y may still be 02914 /// value-dependent). If either x or y is type-dependent, or if the 02915 /// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 02916 /// be used to express the computation. 02917 class BinaryOperator : public Expr { 02918 public: 02919 typedef BinaryOperatorKind Opcode; 02920 02921 private: 02922 unsigned Opc : 6; 02923 02924 // Records the FP_CONTRACT pragma status at the point that this binary 02925 // operator was parsed. This bit is only meaningful for operations on 02926 // floating point types. For all other types it should default to 02927 // false. 02928 unsigned FPContractable : 1; 02929 SourceLocation OpLoc; 02930 02931 enum { LHS, RHS, END_EXPR }; 02932 Stmt* SubExprs[END_EXPR]; 02933 public: 02934 02935 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 02936 ExprValueKind VK, ExprObjectKind OK, 02937 SourceLocation opLoc, bool fpContractable) 02938 : Expr(BinaryOperatorClass, ResTy, VK, OK, 02939 lhs->isTypeDependent() || rhs->isTypeDependent(), 02940 lhs->isValueDependent() || rhs->isValueDependent(), 02941 (lhs->isInstantiationDependent() || 02942 rhs->isInstantiationDependent()), 02943 (lhs->containsUnexpandedParameterPack() || 02944 rhs->containsUnexpandedParameterPack())), 02945 Opc(opc), FPContractable(fpContractable), OpLoc(opLoc) { 02946 SubExprs[LHS] = lhs; 02947 SubExprs[RHS] = rhs; 02948 assert(!isCompoundAssignmentOp() && 02949 "Use CompoundAssignOperator for compound assignments"); 02950 } 02951 02952 /// \brief Construct an empty binary operator. 02953 explicit BinaryOperator(EmptyShell Empty) 02954 : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { } 02955 02956 SourceLocation getExprLoc() const LLVM_READONLY { return OpLoc; } 02957 SourceLocation getOperatorLoc() const { return OpLoc; } 02958 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 02959 02960 Opcode getOpcode() const { return static_cast<Opcode>(Opc); } 02961 void setOpcode(Opcode O) { Opc = O; } 02962 02963 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 02964 void setLHS(Expr *E) { SubExprs[LHS] = E; } 02965 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 02966 void setRHS(Expr *E) { SubExprs[RHS] = E; } 02967 02968 SourceLocation getLocStart() const LLVM_READONLY { 02969 return getLHS()->getLocStart(); 02970 } 02971 SourceLocation getLocEnd() const LLVM_READONLY { 02972 return getRHS()->getLocEnd(); 02973 } 02974 02975 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 02976 /// corresponds to, e.g. "<<=". 02977 static StringRef getOpcodeStr(Opcode Op); 02978 02979 StringRef getOpcodeStr() const { return getOpcodeStr(getOpcode()); } 02980 02981 /// \brief Retrieve the binary opcode that corresponds to the given 02982 /// overloaded operator. 02983 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 02984 02985 /// \brief Retrieve the overloaded operator kind that corresponds to 02986 /// the given binary opcode. 02987 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 02988 02989 /// predicates to categorize the respective opcodes. 02990 bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; } 02991 bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; } 02992 static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } 02993 bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } 02994 static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } 02995 bool isShiftOp() const { return isShiftOp(getOpcode()); } 02996 02997 static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } 02998 bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } 02999 03000 static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } 03001 bool isRelationalOp() const { return isRelationalOp(getOpcode()); } 03002 03003 static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } 03004 bool isEqualityOp() const { return isEqualityOp(getOpcode()); } 03005 03006 static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; } 03007 bool isComparisonOp() const { return isComparisonOp(getOpcode()); } 03008 03009 static Opcode negateComparisonOp(Opcode Opc) { 03010 switch (Opc) { 03011 default: 03012 llvm_unreachable("Not a comparsion operator."); 03013 case BO_LT: return BO_GE; 03014 case BO_GT: return BO_LE; 03015 case BO_LE: return BO_GT; 03016 case BO_GE: return BO_LT; 03017 case BO_EQ: return BO_NE; 03018 case BO_NE: return BO_EQ; 03019 } 03020 } 03021 03022 static Opcode reverseComparisonOp(Opcode Opc) { 03023 switch (Opc) { 03024 default: 03025 llvm_unreachable("Not a comparsion operator."); 03026 case BO_LT: return BO_GT; 03027 case BO_GT: return BO_LT; 03028 case BO_LE: return BO_GE; 03029 case BO_GE: return BO_LE; 03030 case BO_EQ: 03031 case BO_NE: 03032 return Opc; 03033 } 03034 } 03035 03036 static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } 03037 bool isLogicalOp() const { return isLogicalOp(getOpcode()); } 03038 03039 static bool isAssignmentOp(Opcode Opc) { 03040 return Opc >= BO_Assign && Opc <= BO_OrAssign; 03041 } 03042 bool isAssignmentOp() const { return isAssignmentOp(getOpcode()); } 03043 03044 static bool isCompoundAssignmentOp(Opcode Opc) { 03045 return Opc > BO_Assign && Opc <= BO_OrAssign; 03046 } 03047 bool isCompoundAssignmentOp() const { 03048 return isCompoundAssignmentOp(getOpcode()); 03049 } 03050 static Opcode getOpForCompoundAssignment(Opcode Opc) { 03051 assert(isCompoundAssignmentOp(Opc)); 03052 if (Opc >= BO_AndAssign) 03053 return Opcode(unsigned(Opc) - BO_AndAssign + BO_And); 03054 else 03055 return Opcode(unsigned(Opc) - BO_MulAssign + BO_Mul); 03056 } 03057 03058 static bool isShiftAssignOp(Opcode Opc) { 03059 return Opc == BO_ShlAssign || Opc == BO_ShrAssign; 03060 } 03061 bool isShiftAssignOp() const { 03062 return isShiftAssignOp(getOpcode()); 03063 } 03064 03065 static bool classof(const Stmt *S) { 03066 return S->getStmtClass() >= firstBinaryOperatorConstant && 03067 S->getStmtClass() <= lastBinaryOperatorConstant; 03068 } 03069 03070 // Iterators 03071 child_range children() { 03072 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 03073 } 03074 03075 // Set the FP contractability status of this operator. Only meaningful for 03076 // operations on floating point types. 03077 void setFPContractable(bool FPC) { FPContractable = FPC; } 03078 03079 // Get the FP contractability status of this operator. Only meaningful for 03080 // operations on floating point types. 03081 bool isFPContractable() const { return FPContractable; } 03082 03083 protected: 03084 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 03085 ExprValueKind VK, ExprObjectKind OK, 03086 SourceLocation opLoc, bool fpContractable, bool dead2) 03087 : Expr(CompoundAssignOperatorClass, ResTy, VK, OK, 03088 lhs->isTypeDependent() || rhs->isTypeDependent(), 03089 lhs->isValueDependent() || rhs->isValueDependent(), 03090 (lhs->isInstantiationDependent() || 03091 rhs->isInstantiationDependent()), 03092 (lhs->containsUnexpandedParameterPack() || 03093 rhs->containsUnexpandedParameterPack())), 03094 Opc(opc), FPContractable(fpContractable), OpLoc(opLoc) { 03095 SubExprs[LHS] = lhs; 03096 SubExprs[RHS] = rhs; 03097 } 03098 03099 BinaryOperator(StmtClass SC, EmptyShell Empty) 03100 : Expr(SC, Empty), Opc(BO_MulAssign) { } 03101 }; 03102 03103 /// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 03104 /// track of the type the operation is performed in. Due to the semantics of 03105 /// these operators, the operands are promoted, the arithmetic performed, an 03106 /// implicit conversion back to the result type done, then the assignment takes 03107 /// place. This captures the intermediate type which the computation is done 03108 /// in. 03109 class CompoundAssignOperator : public BinaryOperator { 03110 QualType ComputationLHSType; 03111 QualType ComputationResultType; 03112 public: 03113 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResType, 03114 ExprValueKind VK, ExprObjectKind OK, 03115 QualType CompLHSType, QualType CompResultType, 03116 SourceLocation OpLoc, bool fpContractable) 03117 : BinaryOperator(lhs, rhs, opc, ResType, VK, OK, OpLoc, fpContractable, 03118 true), 03119 ComputationLHSType(CompLHSType), 03120 ComputationResultType(CompResultType) { 03121 assert(isCompoundAssignmentOp() && 03122 "Only should be used for compound assignments"); 03123 } 03124 03125 /// \brief Build an empty compound assignment operator expression. 03126 explicit CompoundAssignOperator(EmptyShell Empty) 03127 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 03128 03129 // The two computation types are the type the LHS is converted 03130 // to for the computation and the type of the result; the two are 03131 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 03132 QualType getComputationLHSType() const { return ComputationLHSType; } 03133 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 03134 03135 QualType getComputationResultType() const { return ComputationResultType; } 03136 void setComputationResultType(QualType T) { ComputationResultType = T; } 03137 03138 static bool classof(const Stmt *S) { 03139 return S->getStmtClass() == CompoundAssignOperatorClass; 03140 } 03141 }; 03142 03143 /// AbstractConditionalOperator - An abstract base class for 03144 /// ConditionalOperator and BinaryConditionalOperator. 03145 class AbstractConditionalOperator : public Expr { 03146 SourceLocation QuestionLoc, ColonLoc; 03147 friend class ASTStmtReader; 03148 03149 protected: 03150 AbstractConditionalOperator(StmtClass SC, QualType T, 03151 ExprValueKind VK, ExprObjectKind OK, 03152 bool TD, bool VD, bool ID, 03153 bool ContainsUnexpandedParameterPack, 03154 SourceLocation qloc, 03155 SourceLocation cloc) 03156 : Expr(SC, T, VK, OK, TD, VD, ID, ContainsUnexpandedParameterPack), 03157 QuestionLoc(qloc), ColonLoc(cloc) {} 03158 03159 AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) 03160 : Expr(SC, Empty) { } 03161 03162 public: 03163 // getCond - Return the expression representing the condition for 03164 // the ?: operator. 03165 Expr *getCond() const; 03166 03167 // getTrueExpr - Return the subexpression representing the value of 03168 // the expression if the condition evaluates to true. 03169 Expr *getTrueExpr() const; 03170 03171 // getFalseExpr - Return the subexpression representing the value of 03172 // the expression if the condition evaluates to false. This is 03173 // the same as getRHS. 03174 Expr *getFalseExpr() const; 03175 03176 SourceLocation getQuestionLoc() const { return QuestionLoc; } 03177 SourceLocation getColonLoc() const { return ColonLoc; } 03178 03179 static bool classof(const Stmt *T) { 03180 return T->getStmtClass() == ConditionalOperatorClass || 03181 T->getStmtClass() == BinaryConditionalOperatorClass; 03182 } 03183 }; 03184 03185 /// ConditionalOperator - The ?: ternary operator. The GNU "missing 03186 /// middle" extension is a BinaryConditionalOperator. 03187 class ConditionalOperator : public AbstractConditionalOperator { 03188 enum { COND, LHS, RHS, END_EXPR }; 03189 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 03190 03191 friend class ASTStmtReader; 03192 public: 03193 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 03194 SourceLocation CLoc, Expr *rhs, 03195 QualType t, ExprValueKind VK, ExprObjectKind OK) 03196 : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, 03197 // FIXME: the type of the conditional operator doesn't 03198 // depend on the type of the conditional, but the standard 03199 // seems to imply that it could. File a bug! 03200 (lhs->isTypeDependent() || rhs->isTypeDependent()), 03201 (cond->isValueDependent() || lhs->isValueDependent() || 03202 rhs->isValueDependent()), 03203 (cond->isInstantiationDependent() || 03204 lhs->isInstantiationDependent() || 03205 rhs->isInstantiationDependent()), 03206 (cond->containsUnexpandedParameterPack() || 03207 lhs->containsUnexpandedParameterPack() || 03208 rhs->containsUnexpandedParameterPack()), 03209 QLoc, CLoc) { 03210 SubExprs[COND] = cond; 03211 SubExprs[LHS] = lhs; 03212 SubExprs[RHS] = rhs; 03213 } 03214 03215 /// \brief Build an empty conditional operator. 03216 explicit ConditionalOperator(EmptyShell Empty) 03217 : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } 03218 03219 // getCond - Return the expression representing the condition for 03220 // the ?: operator. 03221 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 03222 03223 // getTrueExpr - Return the subexpression representing the value of 03224 // the expression if the condition evaluates to true. 03225 Expr *getTrueExpr() const { return cast<Expr>(SubExprs[LHS]); } 03226 03227 // getFalseExpr - Return the subexpression representing the value of 03228 // the expression if the condition evaluates to false. This is 03229 // the same as getRHS. 03230 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 03231 03232 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 03233 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 03234 03235 SourceLocation getLocStart() const LLVM_READONLY { 03236 return getCond()->getLocStart(); 03237 } 03238 SourceLocation getLocEnd() const LLVM_READONLY { 03239 return getRHS()->getLocEnd(); 03240 } 03241 03242 static bool classof(const Stmt *T) { 03243 return T->getStmtClass() == ConditionalOperatorClass; 03244 } 03245 03246 // Iterators 03247 child_range children() { 03248 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 03249 } 03250 }; 03251 03252 /// BinaryConditionalOperator - The GNU extension to the conditional 03253 /// operator which allows the middle operand to be omitted. 03254 /// 03255 /// This is a different expression kind on the assumption that almost 03256 /// every client ends up needing to know that these are different. 03257 class BinaryConditionalOperator : public AbstractConditionalOperator { 03258 enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; 03259 03260 /// - the common condition/left-hand-side expression, which will be 03261 /// evaluated as the opaque value 03262 /// - the condition, expressed in terms of the opaque value 03263 /// - the left-hand-side, expressed in terms of the opaque value 03264 /// - the right-hand-side 03265 Stmt *SubExprs[NUM_SUBEXPRS]; 03266 OpaqueValueExpr *OpaqueValue; 03267 03268 friend class ASTStmtReader; 03269 public: 03270 BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, 03271 Expr *cond, Expr *lhs, Expr *rhs, 03272 SourceLocation qloc, SourceLocation cloc, 03273 QualType t, ExprValueKind VK, ExprObjectKind OK) 03274 : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, 03275 (common->isTypeDependent() || rhs->isTypeDependent()), 03276 (common->isValueDependent() || rhs->isValueDependent()), 03277 (common->isInstantiationDependent() || 03278 rhs->isInstantiationDependent()), 03279 (common->containsUnexpandedParameterPack() || 03280 rhs->containsUnexpandedParameterPack()), 03281 qloc, cloc), 03282 OpaqueValue(opaqueValue) { 03283 SubExprs[COMMON] = common; 03284 SubExprs[COND] = cond; 03285 SubExprs[LHS] = lhs; 03286 SubExprs[RHS] = rhs; 03287 assert(OpaqueValue->getSourceExpr() == common && "Wrong opaque value"); 03288 } 03289 03290 /// \brief Build an empty conditional operator. 03291 explicit BinaryConditionalOperator(EmptyShell Empty) 03292 : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } 03293 03294 /// \brief getCommon - Return the common expression, written to the 03295 /// left of the condition. The opaque value will be bound to the 03296 /// result of this expression. 03297 Expr *getCommon() const { return cast<Expr>(SubExprs[COMMON]); } 03298 03299 /// \brief getOpaqueValue - Return the opaque value placeholder. 03300 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } 03301 03302 /// \brief getCond - Return the condition expression; this is defined 03303 /// in terms of the opaque value. 03304 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 03305 03306 /// \brief getTrueExpr - Return the subexpression which will be 03307 /// evaluated if the condition evaluates to true; this is defined 03308 /// in terms of the opaque value. 03309 Expr *getTrueExpr() const { 03310 return cast<Expr>(SubExprs[LHS]); 03311 } 03312 03313 /// \brief getFalseExpr - Return the subexpression which will be 03314 /// evaluated if the condnition evaluates to false; this is 03315 /// defined in terms of the opaque value. 03316 Expr *getFalseExpr() const { 03317 return cast<Expr>(SubExprs[RHS]); 03318 } 03319 03320 SourceLocation getLocStart() const LLVM_READONLY { 03321 return getCommon()->getLocStart(); 03322 } 03323 SourceLocation getLocEnd() const LLVM_READONLY { 03324 return getFalseExpr()->getLocEnd(); 03325 } 03326 03327 static bool classof(const Stmt *T) { 03328 return T->getStmtClass() == BinaryConditionalOperatorClass; 03329 } 03330 03331 // Iterators 03332 child_range children() { 03333 return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); 03334 } 03335 }; 03336 03337 inline Expr *AbstractConditionalOperator::getCond() const { 03338 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 03339 return co->getCond(); 03340 return cast<BinaryConditionalOperator>(this)->getCond(); 03341 } 03342 03343 inline Expr *AbstractConditionalOperator::getTrueExpr() const { 03344 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 03345 return co->getTrueExpr(); 03346 return cast<BinaryConditionalOperator>(this)->getTrueExpr(); 03347 } 03348 03349 inline Expr *AbstractConditionalOperator::getFalseExpr() const { 03350 if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) 03351 return co->getFalseExpr(); 03352 return cast<BinaryConditionalOperator>(this)->getFalseExpr(); 03353 } 03354 03355 /// AddrLabelExpr - The GNU address of label extension, representing &&label. 03356 class AddrLabelExpr : public Expr { 03357 SourceLocation AmpAmpLoc, LabelLoc; 03358 LabelDecl *Label; 03359 public: 03360 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, 03361 QualType t) 03362 : Expr(AddrLabelExprClass, t, VK_RValue, OK_Ordinary, false, false, false, 03363 false), 03364 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 03365 03366 /// \brief Build an empty address of a label expression. 03367 explicit AddrLabelExpr(EmptyShell Empty) 03368 : Expr(AddrLabelExprClass, Empty) { } 03369 03370 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 03371 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 03372 SourceLocation getLabelLoc() const { return LabelLoc; } 03373 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 03374 03375 SourceLocation getLocStart() const LLVM_READONLY { return AmpAmpLoc; } 03376 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; } 03377 03378 LabelDecl *getLabel() const { return Label; } 03379 void setLabel(LabelDecl *L) { Label = L; } 03380 03381 static bool classof(const Stmt *T) { 03382 return T->getStmtClass() == AddrLabelExprClass; 03383 } 03384 03385 // Iterators 03386 child_range children() { return child_range(); } 03387 }; 03388 03389 /// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 03390 /// The StmtExpr contains a single CompoundStmt node, which it evaluates and 03391 /// takes the value of the last subexpression. 03392 /// 03393 /// A StmtExpr is always an r-value; values "returned" out of a 03394 /// StmtExpr will be copied. 03395 class StmtExpr : public Expr { 03396 Stmt *SubStmt; 03397 SourceLocation LParenLoc, RParenLoc; 03398 public: 03399 // FIXME: Does type-dependence need to be computed differently? 03400 // FIXME: Do we need to compute instantiation instantiation-dependence for 03401 // statements? (ugh!) 03402 StmtExpr(CompoundStmt *substmt, QualType T, 03403 SourceLocation lp, SourceLocation rp) : 03404 Expr(StmtExprClass, T, VK_RValue, OK_Ordinary, 03405 T->isDependentType(), false, false, false), 03406 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 03407 03408 /// \brief Build an empty statement expression. 03409 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 03410 03411 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 03412 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 03413 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 03414 03415 SourceLocation getLocStart() const LLVM_READONLY { return LParenLoc; } 03416 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 03417 03418 SourceLocation getLParenLoc() const { return LParenLoc; } 03419 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 03420 SourceLocation getRParenLoc() const { return RParenLoc; } 03421 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 03422 03423 static bool classof(const Stmt *T) { 03424 return T->getStmtClass() == StmtExprClass; 03425 } 03426 03427 // Iterators 03428 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 03429 }; 03430 03431 03432 /// ShuffleVectorExpr - clang-specific builtin-in function 03433 /// __builtin_shufflevector. 03434 /// This AST node represents a operator that does a constant 03435 /// shuffle, similar to LLVM's shufflevector instruction. It takes 03436 /// two vectors and a variable number of constant indices, 03437 /// and returns the appropriately shuffled vector. 03438 class ShuffleVectorExpr : public Expr { 03439 SourceLocation BuiltinLoc, RParenLoc; 03440 03441 // SubExprs - the list of values passed to the __builtin_shufflevector 03442 // function. The first two are vectors, and the rest are constant 03443 // indices. The number of values in this list is always 03444 // 2+the number of indices in the vector type. 03445 Stmt **SubExprs; 03446 unsigned NumExprs; 03447 03448 public: 03449 ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args, QualType Type, 03450 SourceLocation BLoc, SourceLocation RP); 03451 03452 /// \brief Build an empty vector-shuffle expression. 03453 explicit ShuffleVectorExpr(EmptyShell Empty) 03454 : Expr(ShuffleVectorExprClass, Empty), SubExprs(nullptr) { } 03455 03456 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 03457 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 03458 03459 SourceLocation getRParenLoc() const { return RParenLoc; } 03460 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 03461 03462 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 03463 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 03464 03465 static bool classof(const Stmt *T) { 03466 return T->getStmtClass() == ShuffleVectorExprClass; 03467 } 03468 03469 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 03470 /// constant expression, the actual arguments passed in, and the function 03471 /// pointers. 03472 unsigned getNumSubExprs() const { return NumExprs; } 03473 03474 /// \brief Retrieve the array of expressions. 03475 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 03476 03477 /// getExpr - Return the Expr at the specified index. 03478 Expr *getExpr(unsigned Index) { 03479 assert((Index < NumExprs) && "Arg access out of range!"); 03480 return cast<Expr>(SubExprs[Index]); 03481 } 03482 const Expr *getExpr(unsigned Index) const { 03483 assert((Index < NumExprs) && "Arg access out of range!"); 03484 return cast<Expr>(SubExprs[Index]); 03485 } 03486 03487 void setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs); 03488 03489 llvm::APSInt getShuffleMaskIdx(const ASTContext &Ctx, unsigned N) const { 03490 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 03491 return getExpr(N+2)->EvaluateKnownConstInt(Ctx); 03492 } 03493 03494 // Iterators 03495 child_range children() { 03496 return child_range(&SubExprs[0], &SubExprs[0]+NumExprs); 03497 } 03498 }; 03499 03500 /// ConvertVectorExpr - Clang builtin function __builtin_convertvector 03501 /// This AST node provides support for converting a vector type to another 03502 /// vector type of the same arity. 03503 class ConvertVectorExpr : public Expr { 03504 private: 03505 Stmt *SrcExpr; 03506 TypeSourceInfo *TInfo; 03507 SourceLocation BuiltinLoc, RParenLoc; 03508 03509 friend class ASTReader; 03510 friend class ASTStmtReader; 03511 explicit ConvertVectorExpr(EmptyShell Empty) : Expr(ConvertVectorExprClass, Empty) {} 03512 03513 public: 03514 ConvertVectorExpr(Expr* SrcExpr, TypeSourceInfo *TI, QualType DstType, 03515 ExprValueKind VK, ExprObjectKind OK, 03516 SourceLocation BuiltinLoc, SourceLocation RParenLoc) 03517 : Expr(ConvertVectorExprClass, DstType, VK, OK, 03518 DstType->isDependentType(), 03519 DstType->isDependentType() || SrcExpr->isValueDependent(), 03520 (DstType->isInstantiationDependentType() || 03521 SrcExpr->isInstantiationDependent()), 03522 (DstType->containsUnexpandedParameterPack() || 03523 SrcExpr->containsUnexpandedParameterPack())), 03524 SrcExpr(SrcExpr), TInfo(TI), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) {} 03525 03526 /// getSrcExpr - Return the Expr to be converted. 03527 Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } 03528 03529 /// getTypeSourceInfo - Return the destination type. 03530 TypeSourceInfo *getTypeSourceInfo() const { 03531 return TInfo; 03532 } 03533 void setTypeSourceInfo(TypeSourceInfo *ti) { 03534 TInfo = ti; 03535 } 03536 03537 /// getBuiltinLoc - Return the location of the __builtin_convertvector token. 03538 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 03539 03540 /// getRParenLoc - Return the location of final right parenthesis. 03541 SourceLocation getRParenLoc() const { return RParenLoc; } 03542 03543 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 03544 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 03545 03546 static bool classof(const Stmt *T) { 03547 return T->getStmtClass() == ConvertVectorExprClass; 03548 } 03549 03550 // Iterators 03551 child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } 03552 }; 03553 03554 /// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 03555 /// This AST node is similar to the conditional operator (?:) in C, with 03556 /// the following exceptions: 03557 /// - the test expression must be a integer constant expression. 03558 /// - the expression returned acts like the chosen subexpression in every 03559 /// visible way: the type is the same as that of the chosen subexpression, 03560 /// and all predicates (whether it's an l-value, whether it's an integer 03561 /// constant expression, etc.) return the same result as for the chosen 03562 /// sub-expression. 03563 class ChooseExpr : public Expr { 03564 enum { COND, LHS, RHS, END_EXPR }; 03565 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 03566 SourceLocation BuiltinLoc, RParenLoc; 03567 bool CondIsTrue; 03568 public: 03569 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, 03570 QualType t, ExprValueKind VK, ExprObjectKind OK, 03571 SourceLocation RP, bool condIsTrue, 03572 bool TypeDependent, bool ValueDependent) 03573 : Expr(ChooseExprClass, t, VK, OK, TypeDependent, ValueDependent, 03574 (cond->isInstantiationDependent() || 03575 lhs->isInstantiationDependent() || 03576 rhs->isInstantiationDependent()), 03577 (cond->containsUnexpandedParameterPack() || 03578 lhs->containsUnexpandedParameterPack() || 03579 rhs->containsUnexpandedParameterPack())), 03580 BuiltinLoc(BLoc), RParenLoc(RP), CondIsTrue(condIsTrue) { 03581 SubExprs[COND] = cond; 03582 SubExprs[LHS] = lhs; 03583 SubExprs[RHS] = rhs; 03584 } 03585 03586 /// \brief Build an empty __builtin_choose_expr. 03587 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 03588 03589 /// isConditionTrue - Return whether the condition is true (i.e. not 03590 /// equal to zero). 03591 bool isConditionTrue() const { 03592 assert(!isConditionDependent() && 03593 "Dependent condition isn't true or false"); 03594 return CondIsTrue; 03595 } 03596 void setIsConditionTrue(bool isTrue) { CondIsTrue = isTrue; } 03597 03598 bool isConditionDependent() const { 03599 return getCond()->isTypeDependent() || getCond()->isValueDependent(); 03600 } 03601 03602 /// getChosenSubExpr - Return the subexpression chosen according to the 03603 /// condition. 03604 Expr *getChosenSubExpr() const { 03605 return isConditionTrue() ? getLHS() : getRHS(); 03606 } 03607 03608 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 03609 void setCond(Expr *E) { SubExprs[COND] = E; } 03610 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 03611 void setLHS(Expr *E) { SubExprs[LHS] = E; } 03612 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 03613 void setRHS(Expr *E) { SubExprs[RHS] = E; } 03614 03615 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 03616 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 03617 03618 SourceLocation getRParenLoc() const { return RParenLoc; } 03619 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 03620 03621 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 03622 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 03623 03624 static bool classof(const Stmt *T) { 03625 return T->getStmtClass() == ChooseExprClass; 03626 } 03627 03628 // Iterators 03629 child_range children() { 03630 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 03631 } 03632 }; 03633 03634 /// GNUNullExpr - Implements the GNU __null extension, which is a name 03635 /// for a null pointer constant that has integral type (e.g., int or 03636 /// long) and is the same size and alignment as a pointer. The __null 03637 /// extension is typically only used by system headers, which define 03638 /// NULL as __null in C++ rather than using 0 (which is an integer 03639 /// that may not match the size of a pointer). 03640 class GNUNullExpr : public Expr { 03641 /// TokenLoc - The location of the __null keyword. 03642 SourceLocation TokenLoc; 03643 03644 public: 03645 GNUNullExpr(QualType Ty, SourceLocation Loc) 03646 : Expr(GNUNullExprClass, Ty, VK_RValue, OK_Ordinary, false, false, false, 03647 false), 03648 TokenLoc(Loc) { } 03649 03650 /// \brief Build an empty GNU __null expression. 03651 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 03652 03653 /// getTokenLocation - The location of the __null token. 03654 SourceLocation getTokenLocation() const { return TokenLoc; } 03655 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 03656 03657 SourceLocation getLocStart() const LLVM_READONLY { return TokenLoc; } 03658 SourceLocation getLocEnd() const LLVM_READONLY { return TokenLoc; } 03659 03660 static bool classof(const Stmt *T) { 03661 return T->getStmtClass() == GNUNullExprClass; 03662 } 03663 03664 // Iterators 03665 child_range children() { return child_range(); } 03666 }; 03667 03668 /// VAArgExpr, used for the builtin function __builtin_va_arg. 03669 class VAArgExpr : public Expr { 03670 Stmt *Val; 03671 TypeSourceInfo *TInfo; 03672 SourceLocation BuiltinLoc, RParenLoc; 03673 public: 03674 VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo, 03675 SourceLocation RPLoc, QualType t) 03676 : Expr(VAArgExprClass, t, VK_RValue, OK_Ordinary, 03677 t->isDependentType(), false, 03678 (TInfo->getType()->isInstantiationDependentType() || 03679 e->isInstantiationDependent()), 03680 (TInfo->getType()->containsUnexpandedParameterPack() || 03681 e->containsUnexpandedParameterPack())), 03682 Val(e), TInfo(TInfo), 03683 BuiltinLoc(BLoc), 03684 RParenLoc(RPLoc) { } 03685 03686 /// \brief Create an empty __builtin_va_arg expression. 03687 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 03688 03689 const Expr *getSubExpr() const { return cast<Expr>(Val); } 03690 Expr *getSubExpr() { return cast<Expr>(Val); } 03691 void setSubExpr(Expr *E) { Val = E; } 03692 03693 TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; } 03694 void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; } 03695 03696 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 03697 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 03698 03699 SourceLocation getRParenLoc() const { return RParenLoc; } 03700 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 03701 03702 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 03703 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 03704 03705 static bool classof(const Stmt *T) { 03706 return T->getStmtClass() == VAArgExprClass; 03707 } 03708 03709 // Iterators 03710 child_range children() { return child_range(&Val, &Val+1); } 03711 }; 03712 03713 /// @brief Describes an C or C++ initializer list. 03714 /// 03715 /// InitListExpr describes an initializer list, which can be used to 03716 /// initialize objects of different types, including 03717 /// struct/class/union types, arrays, and vectors. For example: 03718 /// 03719 /// @code 03720 /// struct foo x = { 1, { 2, 3 } }; 03721 /// @endcode 03722 /// 03723 /// Prior to semantic analysis, an initializer list will represent the 03724 /// initializer list as written by the user, but will have the 03725 /// placeholder type "void". This initializer list is called the 03726 /// syntactic form of the initializer, and may contain C99 designated 03727 /// initializers (represented as DesignatedInitExprs), initializations 03728 /// of subobject members without explicit braces, and so on. Clients 03729 /// interested in the original syntax of the initializer list should 03730 /// use the syntactic form of the initializer list. 03731 /// 03732 /// After semantic analysis, the initializer list will represent the 03733 /// semantic form of the initializer, where the initializations of all 03734 /// subobjects are made explicit with nested InitListExpr nodes and 03735 /// C99 designators have been eliminated by placing the designated 03736 /// initializations into the subobject they initialize. Additionally, 03737 /// any "holes" in the initialization, where no initializer has been 03738 /// specified for a particular subobject, will be replaced with 03739 /// implicitly-generated ImplicitValueInitExpr expressions that 03740 /// value-initialize the subobjects. Note, however, that the 03741 /// initializer lists may still have fewer initializers than there are 03742 /// elements to initialize within the object. 03743 /// 03744 /// After semantic analysis has completed, given an initializer list, 03745 /// method isSemanticForm() returns true if and only if this is the 03746 /// semantic form of the initializer list (note: the same AST node 03747 /// may at the same time be the syntactic form). 03748 /// Given the semantic form of the initializer list, one can retrieve 03749 /// the syntactic form of that initializer list (when different) 03750 /// using method getSyntacticForm(); the method returns null if applied 03751 /// to a initializer list which is already in syntactic form. 03752 /// Similarly, given the syntactic form (i.e., an initializer list such 03753 /// that isSemanticForm() returns false), one can retrieve the semantic 03754 /// form using method getSemanticForm(). 03755 /// Since many initializer lists have the same syntactic and semantic forms, 03756 /// getSyntacticForm() may return NULL, indicating that the current 03757 /// semantic initializer list also serves as its syntactic form. 03758 class InitListExpr : public Expr { 03759 // FIXME: Eliminate this vector in favor of ASTContext allocation 03760 typedef ASTVector<Stmt *> InitExprsTy; 03761 InitExprsTy InitExprs; 03762 SourceLocation LBraceLoc, RBraceLoc; 03763 03764 /// The alternative form of the initializer list (if it exists). 03765 /// The int part of the pair stores whether this initializer list is 03766 /// in semantic form. If not null, the pointer points to: 03767 /// - the syntactic form, if this is in semantic form; 03768 /// - the semantic form, if this is in syntactic form. 03769 llvm::PointerIntPair<InitListExpr *, 1, bool> AltForm; 03770 03771 /// \brief Either: 03772 /// If this initializer list initializes an array with more elements than 03773 /// there are initializers in the list, specifies an expression to be used 03774 /// for value initialization of the rest of the elements. 03775 /// Or 03776 /// If this initializer list initializes a union, specifies which 03777 /// field within the union will be initialized. 03778 llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; 03779 03780 public: 03781 InitListExpr(const ASTContext &C, SourceLocation lbraceloc, 03782 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc); 03783 03784 /// \brief Build an empty initializer list. 03785 explicit InitListExpr(EmptyShell Empty) 03786 : Expr(InitListExprClass, Empty) { } 03787 03788 unsigned getNumInits() const { return InitExprs.size(); } 03789 03790 /// \brief Retrieve the set of initializers. 03791 Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } 03792 03793 const Expr *getInit(unsigned Init) const { 03794 assert(Init < getNumInits() && "Initializer access out of range!"); 03795 return cast_or_null<Expr>(InitExprs[Init]); 03796 } 03797 03798 Expr *getInit(unsigned Init) { 03799 assert(Init < getNumInits() && "Initializer access out of range!"); 03800 return cast_or_null<Expr>(InitExprs[Init]); 03801 } 03802 03803 void setInit(unsigned Init, Expr *expr) { 03804 assert(Init < getNumInits() && "Initializer access out of range!"); 03805 InitExprs[Init] = expr; 03806 03807 if (expr) { 03808 ExprBits.TypeDependent |= expr->isTypeDependent(); 03809 ExprBits.ValueDependent |= expr->isValueDependent(); 03810 ExprBits.InstantiationDependent |= expr->isInstantiationDependent(); 03811 ExprBits.ContainsUnexpandedParameterPack |= 03812 expr->containsUnexpandedParameterPack(); 03813 } 03814 } 03815 03816 /// \brief Reserve space for some number of initializers. 03817 void reserveInits(const ASTContext &C, unsigned NumInits); 03818 03819 /// @brief Specify the number of initializers 03820 /// 03821 /// If there are more than @p NumInits initializers, the remaining 03822 /// initializers will be destroyed. If there are fewer than @p 03823 /// NumInits initializers, NULL expressions will be added for the 03824 /// unknown initializers. 03825 void resizeInits(const ASTContext &Context, unsigned NumInits); 03826 03827 /// @brief Updates the initializer at index @p Init with the new 03828 /// expression @p expr, and returns the old expression at that 03829 /// location. 03830 /// 03831 /// When @p Init is out of range for this initializer list, the 03832 /// initializer list will be extended with NULL expressions to 03833 /// accommodate the new entry. 03834 Expr *updateInit(const ASTContext &C, unsigned Init, Expr *expr); 03835 03836 /// \brief If this initializer list initializes an array with more elements 03837 /// than there are initializers in the list, specifies an expression to be 03838 /// used for value initialization of the rest of the elements. 03839 Expr *getArrayFiller() { 03840 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>(); 03841 } 03842 const Expr *getArrayFiller() const { 03843 return const_cast<InitListExpr *>(this)->getArrayFiller(); 03844 } 03845 void setArrayFiller(Expr *filler); 03846 03847 /// \brief Return true if this is an array initializer and its array "filler" 03848 /// has been set. 03849 bool hasArrayFiller() const { return getArrayFiller(); } 03850 03851 /// \brief If this initializes a union, specifies which field in the 03852 /// union to initialize. 03853 /// 03854 /// Typically, this field is the first named field within the 03855 /// union. However, a designated initializer can specify the 03856 /// initialization of a different field within the union. 03857 FieldDecl *getInitializedFieldInUnion() { 03858 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>(); 03859 } 03860 const FieldDecl *getInitializedFieldInUnion() const { 03861 return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion(); 03862 } 03863 void setInitializedFieldInUnion(FieldDecl *FD) { 03864 assert((FD == nullptr 03865 || getInitializedFieldInUnion() == nullptr 03866 || getInitializedFieldInUnion() == FD) 03867 && "Only one field of a union may be initialized at a time!"); 03868 ArrayFillerOrUnionFieldInit = FD; 03869 } 03870 03871 // Explicit InitListExpr's originate from source code (and have valid source 03872 // locations). Implicit InitListExpr's are created by the semantic analyzer. 03873 bool isExplicit() { 03874 return LBraceLoc.isValid() && RBraceLoc.isValid(); 03875 } 03876 03877 // Is this an initializer for an array of characters, initialized by a string 03878 // literal or an @encode? 03879 bool isStringLiteralInit() const; 03880 03881 SourceLocation getLBraceLoc() const { return LBraceLoc; } 03882 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 03883 SourceLocation getRBraceLoc() const { return RBraceLoc; } 03884 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 03885 03886 bool isSemanticForm() const { return AltForm.getInt(); } 03887 InitListExpr *getSemanticForm() const { 03888 return isSemanticForm() ? nullptr : AltForm.getPointer(); 03889 } 03890 InitListExpr *getSyntacticForm() const { 03891 return isSemanticForm() ? AltForm.getPointer() : nullptr; 03892 } 03893 03894 void setSyntacticForm(InitListExpr *Init) { 03895 AltForm.setPointer(Init); 03896 AltForm.setInt(true); 03897 Init->AltForm.setPointer(this); 03898 Init->AltForm.setInt(false); 03899 } 03900 03901 bool hadArrayRangeDesignator() const { 03902 return InitListExprBits.HadArrayRangeDesignator != 0; 03903 } 03904 void sawArrayRangeDesignator(bool ARD = true) { 03905 InitListExprBits.HadArrayRangeDesignator = ARD; 03906 } 03907 03908 SourceLocation getLocStart() const LLVM_READONLY; 03909 SourceLocation getLocEnd() const LLVM_READONLY; 03910 03911 static bool classof(const Stmt *T) { 03912 return T->getStmtClass() == InitListExprClass; 03913 } 03914 03915 // Iterators 03916 child_range children() { 03917 // FIXME: This does not include the array filler expression. 03918 if (InitExprs.empty()) return child_range(); 03919 return child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); 03920 } 03921 03922 typedef InitExprsTy::iterator iterator; 03923 typedef InitExprsTy::const_iterator const_iterator; 03924 typedef InitExprsTy::reverse_iterator reverse_iterator; 03925 typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; 03926 03927 iterator begin() { return InitExprs.begin(); } 03928 const_iterator begin() const { return InitExprs.begin(); } 03929 iterator end() { return InitExprs.end(); } 03930 const_iterator end() const { return InitExprs.end(); } 03931 reverse_iterator rbegin() { return InitExprs.rbegin(); } 03932 const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } 03933 reverse_iterator rend() { return InitExprs.rend(); } 03934 const_reverse_iterator rend() const { return InitExprs.rend(); } 03935 03936 friend class ASTStmtReader; 03937 friend class ASTStmtWriter; 03938 }; 03939 03940 /// @brief Represents a C99 designated initializer expression. 03941 /// 03942 /// A designated initializer expression (C99 6.7.8) contains one or 03943 /// more designators (which can be field designators, array 03944 /// designators, or GNU array-range designators) followed by an 03945 /// expression that initializes the field or element(s) that the 03946 /// designators refer to. For example, given: 03947 /// 03948 /// @code 03949 /// struct point { 03950 /// double x; 03951 /// double y; 03952 /// }; 03953 /// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 03954 /// @endcode 03955 /// 03956 /// The InitListExpr contains three DesignatedInitExprs, the first of 03957 /// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 03958 /// designators, one array designator for @c [2] followed by one field 03959 /// designator for @c .y. The initialization expression will be 1.0. 03960 class DesignatedInitExpr : public Expr { 03961 public: 03962 /// \brief Forward declaration of the Designator class. 03963 class Designator; 03964 03965 private: 03966 /// The location of the '=' or ':' prior to the actual initializer 03967 /// expression. 03968 SourceLocation EqualOrColonLoc; 03969 03970 /// Whether this designated initializer used the GNU deprecated 03971 /// syntax rather than the C99 '=' syntax. 03972 bool GNUSyntax : 1; 03973 03974 /// The number of designators in this initializer expression. 03975 unsigned NumDesignators : 15; 03976 03977 /// The number of subexpressions of this initializer expression, 03978 /// which contains both the initializer and any additional 03979 /// expressions used by array and array-range designators. 03980 unsigned NumSubExprs : 16; 03981 03982 /// \brief The designators in this designated initialization 03983 /// expression. 03984 Designator *Designators; 03985 03986 03987 DesignatedInitExpr(const ASTContext &C, QualType Ty, unsigned NumDesignators, 03988 const Designator *Designators, 03989 SourceLocation EqualOrColonLoc, bool GNUSyntax, 03990 ArrayRef<Expr*> IndexExprs, Expr *Init); 03991 03992 explicit DesignatedInitExpr(unsigned NumSubExprs) 03993 : Expr(DesignatedInitExprClass, EmptyShell()), 03994 NumDesignators(0), NumSubExprs(NumSubExprs), Designators(nullptr) { } 03995 03996 public: 03997 /// A field designator, e.g., ".x". 03998 struct FieldDesignator { 03999 /// Refers to the field that is being initialized. The low bit 04000 /// of this field determines whether this is actually a pointer 04001 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 04002 /// initially constructed, a field designator will store an 04003 /// IdentifierInfo*. After semantic analysis has resolved that 04004 /// name, the field designator will instead store a FieldDecl*. 04005 uintptr_t NameOrField; 04006 04007 /// The location of the '.' in the designated initializer. 04008 unsigned DotLoc; 04009 04010 /// The location of the field name in the designated initializer. 04011 unsigned FieldLoc; 04012 }; 04013 04014 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 04015 struct ArrayOrRangeDesignator { 04016 /// Location of the first index expression within the designated 04017 /// initializer expression's list of subexpressions. 04018 unsigned Index; 04019 /// The location of the '[' starting the array range designator. 04020 unsigned LBracketLoc; 04021 /// The location of the ellipsis separating the start and end 04022 /// indices. Only valid for GNU array-range designators. 04023 unsigned EllipsisLoc; 04024 /// The location of the ']' terminating the array range designator. 04025 unsigned RBracketLoc; 04026 }; 04027 04028 /// @brief Represents a single C99 designator. 04029 /// 04030 /// @todo This class is infuriatingly similar to clang::Designator, 04031 /// but minor differences (storing indices vs. storing pointers) 04032 /// keep us from reusing it. Try harder, later, to rectify these 04033 /// differences. 04034 class Designator { 04035 /// @brief The kind of designator this describes. 04036 enum { 04037 FieldDesignator, 04038 ArrayDesignator, 04039 ArrayRangeDesignator 04040 } Kind; 04041 04042 union { 04043 /// A field designator, e.g., ".x". 04044 struct FieldDesignator Field; 04045 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 04046 struct ArrayOrRangeDesignator ArrayOrRange; 04047 }; 04048 friend class DesignatedInitExpr; 04049 04050 public: 04051 Designator() {} 04052 04053 /// @brief Initializes a field designator. 04054 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 04055 SourceLocation FieldLoc) 04056 : Kind(FieldDesignator) { 04057 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 04058 Field.DotLoc = DotLoc.getRawEncoding(); 04059 Field.FieldLoc = FieldLoc.getRawEncoding(); 04060 } 04061 04062 /// @brief Initializes an array designator. 04063 Designator(unsigned Index, SourceLocation LBracketLoc, 04064 SourceLocation RBracketLoc) 04065 : Kind(ArrayDesignator) { 04066 ArrayOrRange.Index = Index; 04067 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 04068 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 04069 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 04070 } 04071 04072 /// @brief Initializes a GNU array-range designator. 04073 Designator(unsigned Index, SourceLocation LBracketLoc, 04074 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 04075 : Kind(ArrayRangeDesignator) { 04076 ArrayOrRange.Index = Index; 04077 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 04078 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 04079 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 04080 } 04081 04082 bool isFieldDesignator() const { return Kind == FieldDesignator; } 04083 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 04084 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 04085 04086 IdentifierInfo *getFieldName() const; 04087 04088 FieldDecl *getField() const { 04089 assert(Kind == FieldDesignator && "Only valid on a field designator"); 04090 if (Field.NameOrField & 0x01) 04091 return nullptr; 04092 else 04093 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 04094 } 04095 04096 void setField(FieldDecl *FD) { 04097 assert(Kind == FieldDesignator && "Only valid on a field designator"); 04098 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 04099 } 04100 04101 SourceLocation getDotLoc() const { 04102 assert(Kind == FieldDesignator && "Only valid on a field designator"); 04103 return SourceLocation::getFromRawEncoding(Field.DotLoc); 04104 } 04105 04106 SourceLocation getFieldLoc() const { 04107 assert(Kind == FieldDesignator && "Only valid on a field designator"); 04108 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 04109 } 04110 04111 SourceLocation getLBracketLoc() const { 04112 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 04113 "Only valid on an array or array-range designator"); 04114 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 04115 } 04116 04117 SourceLocation getRBracketLoc() const { 04118 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 04119 "Only valid on an array or array-range designator"); 04120 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 04121 } 04122 04123 SourceLocation getEllipsisLoc() const { 04124 assert(Kind == ArrayRangeDesignator && 04125 "Only valid on an array-range designator"); 04126 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 04127 } 04128 04129 unsigned getFirstExprIndex() const { 04130 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 04131 "Only valid on an array or array-range designator"); 04132 return ArrayOrRange.Index; 04133 } 04134 04135 SourceLocation getLocStart() const LLVM_READONLY { 04136 if (Kind == FieldDesignator) 04137 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 04138 else 04139 return getLBracketLoc(); 04140 } 04141 SourceLocation getLocEnd() const LLVM_READONLY { 04142 return Kind == FieldDesignator ? getFieldLoc() : getRBracketLoc(); 04143 } 04144 SourceRange getSourceRange() const LLVM_READONLY { 04145 return SourceRange(getLocStart(), getLocEnd()); 04146 } 04147 }; 04148 04149 static DesignatedInitExpr *Create(const ASTContext &C, 04150 Designator *Designators, 04151 unsigned NumDesignators, 04152 ArrayRef<Expr*> IndexExprs, 04153 SourceLocation EqualOrColonLoc, 04154 bool GNUSyntax, Expr *Init); 04155 04156 static DesignatedInitExpr *CreateEmpty(const ASTContext &C, 04157 unsigned NumIndexExprs); 04158 04159 /// @brief Returns the number of designators in this initializer. 04160 unsigned size() const { return NumDesignators; } 04161 04162 // Iterator access to the designators. 04163 typedef Designator *designators_iterator; 04164 designators_iterator designators_begin() { return Designators; } 04165 designators_iterator designators_end() { 04166 return Designators + NumDesignators; 04167 } 04168 04169 typedef const Designator *const_designators_iterator; 04170 const_designators_iterator designators_begin() const { return Designators; } 04171 const_designators_iterator designators_end() const { 04172 return Designators + NumDesignators; 04173 } 04174 04175 typedef llvm::iterator_range<designators_iterator> designators_range; 04176 designators_range designators() { 04177 return designators_range(designators_begin(), designators_end()); 04178 } 04179 04180 typedef llvm::iterator_range<const_designators_iterator> 04181 designators_const_range; 04182 designators_const_range designators() const { 04183 return designators_const_range(designators_begin(), designators_end()); 04184 } 04185 04186 typedef std::reverse_iterator<designators_iterator> 04187 reverse_designators_iterator; 04188 reverse_designators_iterator designators_rbegin() { 04189 return reverse_designators_iterator(designators_end()); 04190 } 04191 reverse_designators_iterator designators_rend() { 04192 return reverse_designators_iterator(designators_begin()); 04193 } 04194 04195 typedef std::reverse_iterator<const_designators_iterator> 04196 const_reverse_designators_iterator; 04197 const_reverse_designators_iterator designators_rbegin() const { 04198 return const_reverse_designators_iterator(designators_end()); 04199 } 04200 const_reverse_designators_iterator designators_rend() const { 04201 return const_reverse_designators_iterator(designators_begin()); 04202 } 04203 04204 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 04205 04206 void setDesignators(const ASTContext &C, const Designator *Desigs, 04207 unsigned NumDesigs); 04208 04209 Expr *getArrayIndex(const Designator &D) const; 04210 Expr *getArrayRangeStart(const Designator &D) const; 04211 Expr *getArrayRangeEnd(const Designator &D) const; 04212 04213 /// @brief Retrieve the location of the '=' that precedes the 04214 /// initializer value itself, if present. 04215 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 04216 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 04217 04218 /// @brief Determines whether this designated initializer used the 04219 /// deprecated GNU syntax for designated initializers. 04220 bool usesGNUSyntax() const { return GNUSyntax; } 04221 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 04222 04223 /// @brief Retrieve the initializer value. 04224 Expr *getInit() const { 04225 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 04226 } 04227 04228 void setInit(Expr *init) { 04229 *child_begin() = init; 04230 } 04231 04232 /// \brief Retrieve the total number of subexpressions in this 04233 /// designated initializer expression, including the actual 04234 /// initialized value and any expressions that occur within array 04235 /// and array-range designators. 04236 unsigned getNumSubExprs() const { return NumSubExprs; } 04237 04238 Expr *getSubExpr(unsigned Idx) const { 04239 assert(Idx < NumSubExprs && "Subscript out of range"); 04240 return cast<Expr>(reinterpret_cast<Stmt *const *>(this + 1)[Idx]); 04241 } 04242 04243 void setSubExpr(unsigned Idx, Expr *E) { 04244 assert(Idx < NumSubExprs && "Subscript out of range"); 04245 reinterpret_cast<Stmt **>(this + 1)[Idx] = E; 04246 } 04247 04248 /// \brief Replaces the designator at index @p Idx with the series 04249 /// of designators in [First, Last). 04250 void ExpandDesignator(const ASTContext &C, unsigned Idx, 04251 const Designator *First, const Designator *Last); 04252 04253 SourceRange getDesignatorsSourceRange() const; 04254 04255 SourceLocation getLocStart() const LLVM_READONLY; 04256 SourceLocation getLocEnd() const LLVM_READONLY; 04257 04258 static bool classof(const Stmt *T) { 04259 return T->getStmtClass() == DesignatedInitExprClass; 04260 } 04261 04262 // Iterators 04263 child_range children() { 04264 Stmt **begin = reinterpret_cast<Stmt**>(this + 1); 04265 return child_range(begin, begin + NumSubExprs); 04266 } 04267 }; 04268 04269 /// \brief Represents an implicitly-generated value initialization of 04270 /// an object of a given type. 04271 /// 04272 /// Implicit value initializations occur within semantic initializer 04273 /// list expressions (InitListExpr) as placeholders for subobject 04274 /// initializations not explicitly specified by the user. 04275 /// 04276 /// \see InitListExpr 04277 class ImplicitValueInitExpr : public Expr { 04278 public: 04279 explicit ImplicitValueInitExpr(QualType ty) 04280 : Expr(ImplicitValueInitExprClass, ty, VK_RValue, OK_Ordinary, 04281 false, false, ty->isInstantiationDependentType(), false) { } 04282 04283 /// \brief Construct an empty implicit value initialization. 04284 explicit ImplicitValueInitExpr(EmptyShell Empty) 04285 : Expr(ImplicitValueInitExprClass, Empty) { } 04286 04287 static bool classof(const Stmt *T) { 04288 return T->getStmtClass() == ImplicitValueInitExprClass; 04289 } 04290 04291 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); } 04292 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); } 04293 04294 // Iterators 04295 child_range children() { return child_range(); } 04296 }; 04297 04298 04299 class ParenListExpr : public Expr { 04300 Stmt **Exprs; 04301 unsigned NumExprs; 04302 SourceLocation LParenLoc, RParenLoc; 04303 04304 public: 04305 ParenListExpr(const ASTContext& C, SourceLocation lparenloc, 04306 ArrayRef<Expr*> exprs, SourceLocation rparenloc); 04307 04308 /// \brief Build an empty paren list. 04309 explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 04310 04311 unsigned getNumExprs() const { return NumExprs; } 04312 04313 const Expr* getExpr(unsigned Init) const { 04314 assert(Init < getNumExprs() && "Initializer access out of range!"); 04315 return cast_or_null<Expr>(Exprs[Init]); 04316 } 04317 04318 Expr* getExpr(unsigned Init) { 04319 assert(Init < getNumExprs() && "Initializer access out of range!"); 04320 return cast_or_null<Expr>(Exprs[Init]); 04321 } 04322 04323 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 04324 04325 SourceLocation getLParenLoc() const { return LParenLoc; } 04326 SourceLocation getRParenLoc() const { return RParenLoc; } 04327 04328 SourceLocation getLocStart() const LLVM_READONLY { return LParenLoc; } 04329 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 04330 04331 static bool classof(const Stmt *T) { 04332 return T->getStmtClass() == ParenListExprClass; 04333 } 04334 04335 // Iterators 04336 child_range children() { 04337 return child_range(&Exprs[0], &Exprs[0]+NumExprs); 04338 } 04339 04340 friend class ASTStmtReader; 04341 friend class ASTStmtWriter; 04342 }; 04343 04344 04345 /// \brief Represents a C11 generic selection. 04346 /// 04347 /// A generic selection (C11 6.5.1.1) contains an unevaluated controlling 04348 /// expression, followed by one or more generic associations. Each generic 04349 /// association specifies a type name and an expression, or "default" and an 04350 /// expression (in which case it is known as a default generic association). 04351 /// The type and value of the generic selection are identical to those of its 04352 /// result expression, which is defined as the expression in the generic 04353 /// association with a type name that is compatible with the type of the 04354 /// controlling expression, or the expression in the default generic association 04355 /// if no types are compatible. For example: 04356 /// 04357 /// @code 04358 /// _Generic(X, double: 1, float: 2, default: 3) 04359 /// @endcode 04360 /// 04361 /// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f 04362 /// or 3 if "hello". 04363 /// 04364 /// As an extension, generic selections are allowed in C++, where the following 04365 /// additional semantics apply: 04366 /// 04367 /// Any generic selection whose controlling expression is type-dependent or 04368 /// which names a dependent type in its association list is result-dependent, 04369 /// which means that the choice of result expression is dependent. 04370 /// Result-dependent generic associations are both type- and value-dependent. 04371 class GenericSelectionExpr : public Expr { 04372 enum { CONTROLLING, END_EXPR }; 04373 TypeSourceInfo **AssocTypes; 04374 Stmt **SubExprs; 04375 unsigned NumAssocs, ResultIndex; 04376 SourceLocation GenericLoc, DefaultLoc, RParenLoc; 04377 04378 public: 04379 GenericSelectionExpr(const ASTContext &Context, 04380 SourceLocation GenericLoc, Expr *ControllingExpr, 04381 ArrayRef<TypeSourceInfo*> AssocTypes, 04382 ArrayRef<Expr*> AssocExprs, 04383 SourceLocation DefaultLoc, SourceLocation RParenLoc, 04384 bool ContainsUnexpandedParameterPack, 04385 unsigned ResultIndex); 04386 04387 /// This constructor is used in the result-dependent case. 04388 GenericSelectionExpr(const ASTContext &Context, 04389 SourceLocation GenericLoc, Expr *ControllingExpr, 04390 ArrayRef<TypeSourceInfo*> AssocTypes, 04391 ArrayRef<Expr*> AssocExprs, 04392 SourceLocation DefaultLoc, SourceLocation RParenLoc, 04393 bool ContainsUnexpandedParameterPack); 04394 04395 explicit GenericSelectionExpr(EmptyShell Empty) 04396 : Expr(GenericSelectionExprClass, Empty) { } 04397 04398 unsigned getNumAssocs() const { return NumAssocs; } 04399 04400 SourceLocation getGenericLoc() const { return GenericLoc; } 04401 SourceLocation getDefaultLoc() const { return DefaultLoc; } 04402 SourceLocation getRParenLoc() const { return RParenLoc; } 04403 04404 const Expr *getAssocExpr(unsigned i) const { 04405 return cast<Expr>(SubExprs[END_EXPR+i]); 04406 } 04407 Expr *getAssocExpr(unsigned i) { return cast<Expr>(SubExprs[END_EXPR+i]); } 04408 04409 const TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) const { 04410 return AssocTypes[i]; 04411 } 04412 TypeSourceInfo *getAssocTypeSourceInfo(unsigned i) { return AssocTypes[i]; } 04413 04414 QualType getAssocType(unsigned i) const { 04415 if (const TypeSourceInfo *TS = getAssocTypeSourceInfo(i)) 04416 return TS->getType(); 04417 else 04418 return QualType(); 04419 } 04420 04421 const Expr *getControllingExpr() const { 04422 return cast<Expr>(SubExprs[CONTROLLING]); 04423 } 04424 Expr *getControllingExpr() { return cast<Expr>(SubExprs[CONTROLLING]); } 04425 04426 /// Whether this generic selection is result-dependent. 04427 bool isResultDependent() const { return ResultIndex == -1U; } 04428 04429 /// The zero-based index of the result expression's generic association in 04430 /// the generic selection's association list. Defined only if the 04431 /// generic selection is not result-dependent. 04432 unsigned getResultIndex() const { 04433 assert(!isResultDependent() && "Generic selection is result-dependent"); 04434 return ResultIndex; 04435 } 04436 04437 /// The generic selection's result expression. Defined only if the 04438 /// generic selection is not result-dependent. 04439 const Expr *getResultExpr() const { return getAssocExpr(getResultIndex()); } 04440 Expr *getResultExpr() { return getAssocExpr(getResultIndex()); } 04441 04442 SourceLocation getLocStart() const LLVM_READONLY { return GenericLoc; } 04443 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 04444 04445 static bool classof(const Stmt *T) { 04446 return T->getStmtClass() == GenericSelectionExprClass; 04447 } 04448 04449 child_range children() { 04450 return child_range(SubExprs, SubExprs+END_EXPR+NumAssocs); 04451 } 04452 04453 friend class ASTStmtReader; 04454 }; 04455 04456 //===----------------------------------------------------------------------===// 04457 // Clang Extensions 04458 //===----------------------------------------------------------------------===// 04459 04460 04461 /// ExtVectorElementExpr - This represents access to specific elements of a 04462 /// vector, and may occur on the left hand side or right hand side. For example 04463 /// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 04464 /// 04465 /// Note that the base may have either vector or pointer to vector type, just 04466 /// like a struct field reference. 04467 /// 04468 class ExtVectorElementExpr : public Expr { 04469 Stmt *Base; 04470 IdentifierInfo *Accessor; 04471 SourceLocation AccessorLoc; 04472 public: 04473 ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, 04474 IdentifierInfo &accessor, SourceLocation loc) 04475 : Expr(ExtVectorElementExprClass, ty, VK, 04476 (VK == VK_RValue ? OK_Ordinary : OK_VectorComponent), 04477 base->isTypeDependent(), base->isValueDependent(), 04478 base->isInstantiationDependent(), 04479 base->containsUnexpandedParameterPack()), 04480 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 04481 04482 /// \brief Build an empty vector element expression. 04483 explicit ExtVectorElementExpr(EmptyShell Empty) 04484 : Expr(ExtVectorElementExprClass, Empty) { } 04485 04486 const Expr *getBase() const { return cast<Expr>(Base); } 04487 Expr *getBase() { return cast<Expr>(Base); } 04488 void setBase(Expr *E) { Base = E; } 04489 04490 IdentifierInfo &getAccessor() const { return *Accessor; } 04491 void setAccessor(IdentifierInfo *II) { Accessor = II; } 04492 04493 SourceLocation getAccessorLoc() const { return AccessorLoc; } 04494 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 04495 04496 /// getNumElements - Get the number of components being selected. 04497 unsigned getNumElements() const; 04498 04499 /// containsDuplicateElements - Return true if any element access is 04500 /// repeated. 04501 bool containsDuplicateElements() const; 04502 04503 /// getEncodedElementAccess - Encode the elements accessed into an llvm 04504 /// aggregate Constant of ConstantInt(s). 04505 void getEncodedElementAccess(SmallVectorImpl<unsigned> &Elts) const; 04506 04507 SourceLocation getLocStart() const LLVM_READONLY { 04508 return getBase()->getLocStart(); 04509 } 04510 SourceLocation getLocEnd() const LLVM_READONLY { return AccessorLoc; } 04511 04512 /// isArrow - Return true if the base expression is a pointer to vector, 04513 /// return false if the base expression is a vector. 04514 bool isArrow() const; 04515 04516 static bool classof(const Stmt *T) { 04517 return T->getStmtClass() == ExtVectorElementExprClass; 04518 } 04519 04520 // Iterators 04521 child_range children() { return child_range(&Base, &Base+1); } 04522 }; 04523 04524 04525 /// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 04526 /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 04527 class BlockExpr : public Expr { 04528 protected: 04529 BlockDecl *TheBlock; 04530 public: 04531 BlockExpr(BlockDecl *BD, QualType ty) 04532 : Expr(BlockExprClass, ty, VK_RValue, OK_Ordinary, 04533 ty->isDependentType(), ty->isDependentType(), 04534 ty->isInstantiationDependentType() || BD->isDependentContext(), 04535 false), 04536 TheBlock(BD) {} 04537 04538 /// \brief Build an empty block expression. 04539 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 04540 04541 const BlockDecl *getBlockDecl() const { return TheBlock; } 04542 BlockDecl *getBlockDecl() { return TheBlock; } 04543 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 04544 04545 // Convenience functions for probing the underlying BlockDecl. 04546 SourceLocation getCaretLocation() const; 04547 const Stmt *getBody() const; 04548 Stmt *getBody(); 04549 04550 SourceLocation getLocStart() const LLVM_READONLY { return getCaretLocation(); } 04551 SourceLocation getLocEnd() const LLVM_READONLY { return getBody()->getLocEnd(); } 04552 04553 /// getFunctionType - Return the underlying function type for this block. 04554 const FunctionProtoType *getFunctionType() const; 04555 04556 static bool classof(const Stmt *T) { 04557 return T->getStmtClass() == BlockExprClass; 04558 } 04559 04560 // Iterators 04561 child_range children() { return child_range(); } 04562 }; 04563 04564 /// AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] 04565 /// This AST node provides support for reinterpreting a type to another 04566 /// type of the same size. 04567 class AsTypeExpr : public Expr { 04568 private: 04569 Stmt *SrcExpr; 04570 SourceLocation BuiltinLoc, RParenLoc; 04571 04572 friend class ASTReader; 04573 friend class ASTStmtReader; 04574 explicit AsTypeExpr(EmptyShell Empty) : Expr(AsTypeExprClass, Empty) {} 04575 04576 public: 04577 AsTypeExpr(Expr* SrcExpr, QualType DstType, 04578 ExprValueKind VK, ExprObjectKind OK, 04579 SourceLocation BuiltinLoc, SourceLocation RParenLoc) 04580 : Expr(AsTypeExprClass, DstType, VK, OK, 04581 DstType->isDependentType(), 04582 DstType->isDependentType() || SrcExpr->isValueDependent(), 04583 (DstType->isInstantiationDependentType() || 04584 SrcExpr->isInstantiationDependent()), 04585 (DstType->containsUnexpandedParameterPack() || 04586 SrcExpr->containsUnexpandedParameterPack())), 04587 SrcExpr(SrcExpr), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) {} 04588 04589 /// getSrcExpr - Return the Expr to be converted. 04590 Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } 04591 04592 /// getBuiltinLoc - Return the location of the __builtin_astype token. 04593 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 04594 04595 /// getRParenLoc - Return the location of final right parenthesis. 04596 SourceLocation getRParenLoc() const { return RParenLoc; } 04597 04598 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 04599 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 04600 04601 static bool classof(const Stmt *T) { 04602 return T->getStmtClass() == AsTypeExprClass; 04603 } 04604 04605 // Iterators 04606 child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } 04607 }; 04608 04609 /// PseudoObjectExpr - An expression which accesses a pseudo-object 04610 /// l-value. A pseudo-object is an abstract object, accesses to which 04611 /// are translated to calls. The pseudo-object expression has a 04612 /// syntactic form, which shows how the expression was actually 04613 /// written in the source code, and a semantic form, which is a series 04614 /// of expressions to be executed in order which detail how the 04615 /// operation is actually evaluated. Optionally, one of the semantic 04616 /// forms may also provide a result value for the expression. 04617 /// 04618 /// If any of the semantic-form expressions is an OpaqueValueExpr, 04619 /// that OVE is required to have a source expression, and it is bound 04620 /// to the result of that source expression. Such OVEs may appear 04621 /// only in subsequent semantic-form expressions and as 04622 /// sub-expressions of the syntactic form. 04623 /// 04624 /// PseudoObjectExpr should be used only when an operation can be 04625 /// usefully described in terms of fairly simple rewrite rules on 04626 /// objects and functions that are meant to be used by end-developers. 04627 /// For example, under the Itanium ABI, dynamic casts are implemented 04628 /// as a call to a runtime function called __dynamic_cast; using this 04629 /// class to describe that would be inappropriate because that call is 04630 /// not really part of the user-visible semantics, and instead the 04631 /// cast is properly reflected in the AST and IR-generation has been 04632 /// taught to generate the call as necessary. In contrast, an 04633 /// Objective-C property access is semantically defined to be 04634 /// equivalent to a particular message send, and this is very much 04635 /// part of the user model. The name of this class encourages this 04636 /// modelling design. 04637 class PseudoObjectExpr : public Expr { 04638 // PseudoObjectExprBits.NumSubExprs - The number of sub-expressions. 04639 // Always at least two, because the first sub-expression is the 04640 // syntactic form. 04641 04642 // PseudoObjectExprBits.ResultIndex - The index of the 04643 // sub-expression holding the result. 0 means the result is void, 04644 // which is unambiguous because it's the index of the syntactic 04645 // form. Note that this is therefore 1 higher than the value passed 04646 // in to Create, which is an index within the semantic forms. 04647 // Note also that ASTStmtWriter assumes this encoding. 04648 04649 Expr **getSubExprsBuffer() { return reinterpret_cast<Expr**>(this + 1); } 04650 const Expr * const *getSubExprsBuffer() const { 04651 return reinterpret_cast<const Expr * const *>(this + 1); 04652 } 04653 04654 friend class ASTStmtReader; 04655 04656 PseudoObjectExpr(QualType type, ExprValueKind VK, 04657 Expr *syntactic, ArrayRef<Expr*> semantic, 04658 unsigned resultIndex); 04659 04660 PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs); 04661 04662 unsigned getNumSubExprs() const { 04663 return PseudoObjectExprBits.NumSubExprs; 04664 } 04665 04666 public: 04667 /// NoResult - A value for the result index indicating that there is 04668 /// no semantic result. 04669 enum : unsigned { NoResult = ~0U }; 04670 04671 static PseudoObjectExpr *Create(const ASTContext &Context, Expr *syntactic, 04672 ArrayRef<Expr*> semantic, 04673 unsigned resultIndex); 04674 04675 static PseudoObjectExpr *Create(const ASTContext &Context, EmptyShell shell, 04676 unsigned numSemanticExprs); 04677 04678 /// Return the syntactic form of this expression, i.e. the 04679 /// expression it actually looks like. Likely to be expressed in 04680 /// terms of OpaqueValueExprs bound in the semantic form. 04681 Expr *getSyntacticForm() { return getSubExprsBuffer()[0]; } 04682 const Expr *getSyntacticForm() const { return getSubExprsBuffer()[0]; } 04683 04684 /// Return the index of the result-bearing expression into the semantics 04685 /// expressions, or PseudoObjectExpr::NoResult if there is none. 04686 unsigned getResultExprIndex() const { 04687 if (PseudoObjectExprBits.ResultIndex == 0) return NoResult; 04688 return PseudoObjectExprBits.ResultIndex - 1; 04689 } 04690 04691 /// Return the result-bearing expression, or null if there is none. 04692 Expr *getResultExpr() { 04693 if (PseudoObjectExprBits.ResultIndex == 0) 04694 return nullptr; 04695 return getSubExprsBuffer()[PseudoObjectExprBits.ResultIndex]; 04696 } 04697 const Expr *getResultExpr() const { 04698 return const_cast<PseudoObjectExpr*>(this)->getResultExpr(); 04699 } 04700 04701 unsigned getNumSemanticExprs() const { return getNumSubExprs() - 1; } 04702 04703 typedef Expr * const *semantics_iterator; 04704 typedef const Expr * const *const_semantics_iterator; 04705 semantics_iterator semantics_begin() { 04706 return getSubExprsBuffer() + 1; 04707 } 04708 const_semantics_iterator semantics_begin() const { 04709 return getSubExprsBuffer() + 1; 04710 } 04711 semantics_iterator semantics_end() { 04712 return getSubExprsBuffer() + getNumSubExprs(); 04713 } 04714 const_semantics_iterator semantics_end() const { 04715 return getSubExprsBuffer() + getNumSubExprs(); 04716 } 04717 Expr *getSemanticExpr(unsigned index) { 04718 assert(index + 1 < getNumSubExprs()); 04719 return getSubExprsBuffer()[index + 1]; 04720 } 04721 const Expr *getSemanticExpr(unsigned index) const { 04722 return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index); 04723 } 04724 04725 SourceLocation getExprLoc() const LLVM_READONLY { 04726 return getSyntacticForm()->getExprLoc(); 04727 } 04728 04729 SourceLocation getLocStart() const LLVM_READONLY { 04730 return getSyntacticForm()->getLocStart(); 04731 } 04732 SourceLocation getLocEnd() const LLVM_READONLY { 04733 return getSyntacticForm()->getLocEnd(); 04734 } 04735 04736 child_range children() { 04737 Stmt **cs = reinterpret_cast<Stmt**>(getSubExprsBuffer()); 04738 return child_range(cs, cs + getNumSubExprs()); 04739 } 04740 04741 static bool classof(const Stmt *T) { 04742 return T->getStmtClass() == PseudoObjectExprClass; 04743 } 04744 }; 04745 04746 /// AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, 04747 /// __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the 04748 /// similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>. 04749 /// All of these instructions take one primary pointer and at least one memory 04750 /// order. 04751 class AtomicExpr : public Expr { 04752 public: 04753 enum AtomicOp { 04754 #define BUILTIN(ID, TYPE, ATTRS) 04755 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) AO ## ID, 04756 #include "clang/Basic/Builtins.def" 04757 // Avoid trailing comma 04758 BI_First = 0 04759 }; 04760 04761 // The ABI values for various atomic memory orderings. 04762 enum AtomicOrderingKind { 04763 AO_ABI_memory_order_relaxed = 0, 04764 AO_ABI_memory_order_consume = 1, 04765 AO_ABI_memory_order_acquire = 2, 04766 AO_ABI_memory_order_release = 3, 04767 AO_ABI_memory_order_acq_rel = 4, 04768 AO_ABI_memory_order_seq_cst = 5 04769 }; 04770 04771 private: 04772 enum { PTR, ORDER, VAL1, ORDER_FAIL, VAL2, WEAK, END_EXPR }; 04773 Stmt* SubExprs[END_EXPR]; 04774 unsigned NumSubExprs; 04775 SourceLocation BuiltinLoc, RParenLoc; 04776 AtomicOp Op; 04777 04778 friend class ASTStmtReader; 04779 04780 public: 04781 AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args, QualType t, 04782 AtomicOp op, SourceLocation RP); 04783 04784 /// \brief Determine the number of arguments the specified atomic builtin 04785 /// should have. 04786 static unsigned getNumSubExprs(AtomicOp Op); 04787 04788 /// \brief Build an empty AtomicExpr. 04789 explicit AtomicExpr(EmptyShell Empty) : Expr(AtomicExprClass, Empty) { } 04790 04791 Expr *getPtr() const { 04792 return cast<Expr>(SubExprs[PTR]); 04793 } 04794 Expr *getOrder() const { 04795 return cast<Expr>(SubExprs[ORDER]); 04796 } 04797 Expr *getVal1() const { 04798 if (Op == AO__c11_atomic_init) 04799 return cast<Expr>(SubExprs[ORDER]); 04800 assert(NumSubExprs > VAL1); 04801 return cast<Expr>(SubExprs[VAL1]); 04802 } 04803 Expr *getOrderFail() const { 04804 assert(NumSubExprs > ORDER_FAIL); 04805 return cast<Expr>(SubExprs[ORDER_FAIL]); 04806 } 04807 Expr *getVal2() const { 04808 if (Op == AO__atomic_exchange) 04809 return cast<Expr>(SubExprs[ORDER_FAIL]); 04810 assert(NumSubExprs > VAL2); 04811 return cast<Expr>(SubExprs[VAL2]); 04812 } 04813 Expr *getWeak() const { 04814 assert(NumSubExprs > WEAK); 04815 return cast<Expr>(SubExprs[WEAK]); 04816 } 04817 04818 AtomicOp getOp() const { return Op; } 04819 unsigned getNumSubExprs() { return NumSubExprs; } 04820 04821 Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } 04822 04823 bool isVolatile() const { 04824 return getPtr()->getType()->getPointeeType().isVolatileQualified(); 04825 } 04826 04827 bool isCmpXChg() const { 04828 return getOp() == AO__c11_atomic_compare_exchange_strong || 04829 getOp() == AO__c11_atomic_compare_exchange_weak || 04830 getOp() == AO__atomic_compare_exchange || 04831 getOp() == AO__atomic_compare_exchange_n; 04832 } 04833 04834 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 04835 SourceLocation getRParenLoc() const { return RParenLoc; } 04836 04837 SourceLocation getLocStart() const LLVM_READONLY { return BuiltinLoc; } 04838 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 04839 04840 static bool classof(const Stmt *T) { 04841 return T->getStmtClass() == AtomicExprClass; 04842 } 04843 04844 // Iterators 04845 child_range children() { 04846 return child_range(SubExprs, SubExprs+NumSubExprs); 04847 } 04848 }; 04849 04850 /// TypoExpr - Internal placeholder for expressions where typo correction 04851 /// still needs to be performed and/or an error diagnostic emitted. 04852 class TypoExpr : public Expr { 04853 public: 04854 TypoExpr(QualType T) 04855 : Expr(TypoExprClass, T, VK_LValue, OK_Ordinary, 04856 /*isTypeDependent*/ true, 04857 /*isValueDependent*/ true, 04858 /*isInstantiationDependent*/ true, 04859 /*containsUnexpandedParameterPack*/ false) { 04860 assert(T->isDependentType() && "TypoExpr given a non-dependent type"); 04861 } 04862 04863 child_range children() { return child_range(); } 04864 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); } 04865 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); } 04866 }; 04867 } // end namespace clang 04868 04869 #endif