| 1 | //===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the Expr interface and subclasses. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CLANG_AST_EXPR_H |
| 14 | #define LLVM_CLANG_AST_EXPR_H |
| 15 | |
| 16 | #include "clang/AST/APNumericStorage.h" |
| 17 | #include "clang/AST/APValue.h" |
| 18 | #include "clang/AST/ASTVector.h" |
| 19 | #include "clang/AST/ComputeDependence.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclAccessPair.h" |
| 22 | #include "clang/AST/DependenceFlags.h" |
| 23 | #include "clang/AST/OperationKinds.h" |
| 24 | #include "clang/AST/Stmt.h" |
| 25 | #include "clang/AST/TemplateBase.h" |
| 26 | #include "clang/AST/Type.h" |
| 27 | #include "clang/Basic/CharInfo.h" |
| 28 | #include "clang/Basic/LangOptions.h" |
| 29 | #include "clang/Basic/SyncScope.h" |
| 30 | #include "clang/Basic/TypeTraits.h" |
| 31 | #include "llvm/ADT/APFloat.h" |
| 32 | #include "llvm/ADT/APSInt.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
| 34 | #include "llvm/ADT/StringRef.h" |
| 35 | #include "llvm/ADT/iterator.h" |
| 36 | #include "llvm/ADT/iterator_range.h" |
| 37 | #include "llvm/Support/AtomicOrdering.h" |
| 38 | #include "llvm/Support/Compiler.h" |
| 39 | #include "llvm/Support/TrailingObjects.h" |
| 40 | #include <optional> |
| 41 | |
| 42 | namespace clang { |
| 43 | class APValue; |
| 44 | class ASTContext; |
| 45 | class BlockDecl; |
| 46 | class CXXBaseSpecifier; |
| 47 | class CXXMemberCallExpr; |
| 48 | class CXXOperatorCallExpr; |
| 49 | class CastExpr; |
| 50 | class Decl; |
| 51 | class IdentifierInfo; |
| 52 | class MaterializeTemporaryExpr; |
| 53 | class NamedDecl; |
| 54 | class ObjCPropertyRefExpr; |
| 55 | class OpaqueValueExpr; |
| 56 | class ParmVarDecl; |
| 57 | class StringLiteral; |
| 58 | class TargetInfo; |
| 59 | class ValueDecl; |
| 60 | |
| 61 | /// A simple array of base specifiers. |
| 62 | typedef SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; |
| 63 | |
| 64 | /// An adjustment to be made to the temporary created when emitting a |
| 65 | /// reference binding, which accesses a particular subobject of that temporary. |
| 66 | struct SubobjectAdjustment { |
| 67 | enum { |
| 68 | DerivedToBaseAdjustment, |
| 69 | FieldAdjustment, |
| 70 | MemberPointerAdjustment |
| 71 | } Kind; |
| 72 | |
| 73 | struct DTB { |
| 74 | const CastExpr *BasePath; |
| 75 | const CXXRecordDecl *DerivedClass; |
| 76 | }; |
| 77 | |
| 78 | struct P { |
| 79 | const MemberPointerType *MPT; |
| 80 | Expr *RHS; |
| 81 | }; |
| 82 | |
| 83 | union { |
| 84 | struct DTB DerivedToBase; |
| 85 | const FieldDecl *Field; |
| 86 | struct P Ptr; |
| 87 | }; |
| 88 | |
| 89 | SubobjectAdjustment(const CastExpr *BasePath, |
| 90 | const CXXRecordDecl *DerivedClass) |
| 91 | : Kind(DerivedToBaseAdjustment) { |
| 92 | DerivedToBase.BasePath = BasePath; |
| 93 | DerivedToBase.DerivedClass = DerivedClass; |
| 94 | } |
| 95 | |
| 96 | SubobjectAdjustment(const FieldDecl *Field) : Kind(FieldAdjustment) { |
| 97 | this->Field = Field; |
| 98 | } |
| 99 | |
| 100 | SubobjectAdjustment(const MemberPointerType *MPT, Expr *RHS) |
| 101 | : Kind(MemberPointerAdjustment) { |
| 102 | this->Ptr.MPT = MPT; |
| 103 | this->Ptr.RHS = RHS; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | /// This represents one expression. Note that Expr's are subclasses of Stmt. |
| 108 | /// This allows an expression to be transparently used any place a Stmt is |
| 109 | /// required. |
| 110 | class Expr : public ValueStmt { |
| 111 | QualType TR; |
| 112 | |
| 113 | public: |
| 114 | Expr() = delete; |
| 115 | Expr(const Expr&) = delete; |
| 116 | Expr(Expr &&) = delete; |
| 117 | Expr &operator=(const Expr&) = delete; |
| 118 | Expr &operator=(Expr&&) = delete; |
| 119 | |
| 120 | protected: |
| 121 | Expr(StmtClass SC, QualType T, ExprValueKind VK, ExprObjectKind OK) |
| 122 | : ValueStmt(SC) { |
| 123 | ExprBits.Dependent = 0; |
| 124 | ExprBits.ValueKind = VK; |
| 125 | ExprBits.ObjectKind = OK; |
| 126 | assert(ExprBits.ObjectKind == OK && "truncated kind" ); |
| 127 | setType(T); |
| 128 | } |
| 129 | |
| 130 | /// Construct an empty expression. |
| 131 | explicit Expr(StmtClass SC, EmptyShell) : ValueStmt(SC) { } |
| 132 | |
| 133 | /// Each concrete expr subclass is expected to compute its dependence and call |
| 134 | /// this in the constructor. |
| 135 | void setDependence(ExprDependence Deps) { |
| 136 | ExprBits.Dependent = static_cast<unsigned>(Deps); |
| 137 | } |
| 138 | friend class ASTImporter; // Sets dependence directly. |
| 139 | friend class ASTStmtReader; // Sets dependence directly. |
| 140 | |
| 141 | public: |
| 142 | QualType getType() const { return TR; } |
| 143 | void setType(QualType t) { |
| 144 | // In C++, the type of an expression is always adjusted so that it |
| 145 | // will not have reference type (C++ [expr]p6). Use |
| 146 | // QualType::getNonReferenceType() to retrieve the non-reference |
| 147 | // type. Additionally, inspect Expr::isLvalue to determine whether |
| 148 | // an expression that is adjusted in this manner should be |
| 149 | // considered an lvalue. |
| 150 | assert((t.isNull() || !t->isReferenceType()) && |
| 151 | "Expressions can't have reference type" ); |
| 152 | |
| 153 | TR = t; |
| 154 | } |
| 155 | |
| 156 | /// If this expression is an enumeration constant, return the |
| 157 | /// enumeration type under which said constant was declared. |
| 158 | /// Otherwise return the expression's type. |
| 159 | /// Note this effectively circumvents the weak typing of C's enum constants |
| 160 | QualType getEnumCoercedType(const ASTContext &Ctx) const; |
| 161 | |
| 162 | ExprDependence getDependence() const { |
| 163 | return static_cast<ExprDependence>(ExprBits.Dependent); |
| 164 | } |
| 165 | |
| 166 | /// Determines whether the value of this expression depends on |
| 167 | /// - a template parameter (C++ [temp.dep.constexpr]) |
| 168 | /// - or an error, whose resolution is unknown |
| 169 | /// |
| 170 | /// For example, the array bound of "Chars" in the following example is |
| 171 | /// value-dependent. |
| 172 | /// @code |
| 173 | /// template<int Size, char (&Chars)[Size]> struct meta_string; |
| 174 | /// @endcode |
| 175 | bool isValueDependent() const { |
| 176 | return static_cast<bool>(getDependence() & ExprDependence::Value); |
| 177 | } |
| 178 | |
| 179 | /// Determines whether the type of this expression depends on |
| 180 | /// - a template parameter (C++ [temp.dep.expr], which means that its type |
| 181 | /// could change from one template instantiation to the next) |
| 182 | /// - or an error |
| 183 | /// |
| 184 | /// For example, the expressions "x" and "x + y" are type-dependent in |
| 185 | /// the following code, but "y" is not type-dependent: |
| 186 | /// @code |
| 187 | /// template<typename T> |
| 188 | /// void add(T x, int y) { |
| 189 | /// x + y; |
| 190 | /// } |
| 191 | /// @endcode |
| 192 | bool isTypeDependent() const { |
| 193 | return static_cast<bool>(getDependence() & ExprDependence::Type); |
| 194 | } |
| 195 | |
| 196 | /// Whether this expression is instantiation-dependent, meaning that |
| 197 | /// it depends in some way on |
| 198 | /// - a template parameter (even if neither its type nor (constant) value |
| 199 | /// can change due to the template instantiation) |
| 200 | /// - or an error |
| 201 | /// |
| 202 | /// In the following example, the expression \c sizeof(sizeof(T() + T())) is |
| 203 | /// instantiation-dependent (since it involves a template parameter \c T), but |
| 204 | /// is neither type- nor value-dependent, since the type of the inner |
| 205 | /// \c sizeof is known (\c std::size_t) and therefore the size of the outer |
| 206 | /// \c sizeof is known. |
| 207 | /// |
| 208 | /// \code |
| 209 | /// template<typename T> |
| 210 | /// void f(T x, T y) { |
| 211 | /// sizeof(sizeof(T() + T()); |
| 212 | /// } |
| 213 | /// \endcode |
| 214 | /// |
| 215 | /// \code |
| 216 | /// void func(int) { |
| 217 | /// func(); // the expression is instantiation-dependent, because it depends |
| 218 | /// // on an error. |
| 219 | /// } |
| 220 | /// \endcode |
| 221 | bool isInstantiationDependent() const { |
| 222 | return static_cast<bool>(getDependence() & ExprDependence::Instantiation); |
| 223 | } |
| 224 | |
| 225 | /// Whether this expression contains an unexpanded parameter |
| 226 | /// pack (for C++11 variadic templates). |
| 227 | /// |
| 228 | /// Given the following function template: |
| 229 | /// |
| 230 | /// \code |
| 231 | /// template<typename F, typename ...Types> |
| 232 | /// void forward(const F &f, Types &&...args) { |
| 233 | /// f(static_cast<Types&&>(args)...); |
| 234 | /// } |
| 235 | /// \endcode |
| 236 | /// |
| 237 | /// The expressions \c args and \c static_cast<Types&&>(args) both |
| 238 | /// contain parameter packs. |
| 239 | bool containsUnexpandedParameterPack() const { |
| 240 | return static_cast<bool>(getDependence() & ExprDependence::UnexpandedPack); |
| 241 | } |
| 242 | |
| 243 | /// Whether this expression contains subexpressions which had errors, e.g. a |
| 244 | /// TypoExpr. |
| 245 | bool containsErrors() const { |
| 246 | return static_cast<bool>(getDependence() & ExprDependence::Error); |
| 247 | } |
| 248 | |
| 249 | /// getExprLoc - Return the preferred location for the arrow when diagnosing |
| 250 | /// a problem with a generic expression. |
| 251 | SourceLocation getExprLoc() const LLVM_READONLY; |
| 252 | |
| 253 | /// Determine whether an lvalue-to-rvalue conversion should implicitly be |
| 254 | /// applied to this expression if it appears as a discarded-value expression |
| 255 | /// in C++11 onwards. This applies to certain forms of volatile glvalues. |
| 256 | bool isReadIfDiscardedInCPlusPlus11() const; |
| 257 | |
| 258 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 259 | /// be warned about if the result is unused. If so, fill in expr, location, |
| 260 | /// and ranges with expr to warn on and source locations/ranges appropriate |
| 261 | /// for a warning. |
| 262 | bool isUnusedResultAWarning(const Expr *&WarnExpr, SourceLocation &Loc, |
| 263 | SourceRange &R1, SourceRange &R2, |
| 264 | ASTContext &Ctx) const; |
| 265 | |
| 266 | /// isLValue - True if this expression is an "l-value" according to |
| 267 | /// the rules of the current language. C and C++ give somewhat |
| 268 | /// different rules for this concept, but in general, the result of |
| 269 | /// an l-value expression identifies a specific object whereas the |
| 270 | /// result of an r-value expression is a value detached from any |
| 271 | /// specific storage. |
| 272 | /// |
| 273 | /// C++11 divides the concept of "r-value" into pure r-values |
| 274 | /// ("pr-values") and so-called expiring values ("x-values"), which |
| 275 | /// identify specific objects that can be safely cannibalized for |
| 276 | /// their resources. |
| 277 | bool isLValue() const { return getValueKind() == VK_LValue; } |
| 278 | bool isPRValue() const { return getValueKind() == VK_PRValue; } |
| 279 | bool isXValue() const { return getValueKind() == VK_XValue; } |
| 280 | bool isGLValue() const { return getValueKind() != VK_PRValue; } |
| 281 | |
| 282 | enum LValueClassification { |
| 283 | LV_Valid, |
| 284 | LV_NotObjectType, |
| 285 | LV_IncompleteVoidType, |
| 286 | LV_DuplicateVectorComponents, |
| 287 | LV_InvalidExpression, |
| 288 | LV_InvalidMessageExpression, |
| 289 | LV_MemberFunction, |
| 290 | LV_SubObjCPropertySetting, |
| 291 | LV_ClassTemporary, |
| 292 | LV_ArrayTemporary |
| 293 | }; |
| 294 | /// Reasons why an expression might not be an l-value. |
| 295 | LValueClassification ClassifyLValue(ASTContext &Ctx) const; |
| 296 | |
| 297 | enum isModifiableLvalueResult { |
| 298 | MLV_Valid, |
| 299 | MLV_NotObjectType, |
| 300 | MLV_IncompleteVoidType, |
| 301 | MLV_DuplicateVectorComponents, |
| 302 | MLV_InvalidExpression, |
| 303 | MLV_LValueCast, // Specialized form of MLV_InvalidExpression. |
| 304 | MLV_IncompleteType, |
| 305 | MLV_ConstQualified, |
| 306 | MLV_ConstQualifiedField, |
| 307 | MLV_ConstAddrSpace, |
| 308 | MLV_ArrayType, |
| 309 | MLV_NoSetterProperty, |
| 310 | MLV_MemberFunction, |
| 311 | MLV_SubObjCPropertySetting, |
| 312 | MLV_InvalidMessageExpression, |
| 313 | MLV_ClassTemporary, |
| 314 | MLV_ArrayTemporary |
| 315 | }; |
| 316 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 317 | /// does not have an incomplete type, does not have a const-qualified type, |
| 318 | /// and if it is a structure or union, does not have any member (including, |
| 319 | /// recursively, any member or element of all contained aggregates or unions) |
| 320 | /// with a const-qualified type. |
| 321 | /// |
| 322 | /// \param Loc [in,out] - A source location which *may* be filled |
| 323 | /// in with the location of the expression making this a |
| 324 | /// non-modifiable lvalue, if specified. |
| 325 | isModifiableLvalueResult |
| 326 | isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc = nullptr) const; |
| 327 | |
| 328 | /// The return type of classify(). Represents the C++11 expression |
| 329 | /// taxonomy. |
| 330 | class Classification { |
| 331 | public: |
| 332 | /// The various classification results. Most of these mean prvalue. |
| 333 | enum Kinds { |
| 334 | CL_LValue, |
| 335 | CL_XValue, |
| 336 | CL_Function, // Functions cannot be lvalues in C. |
| 337 | CL_Void, // Void cannot be an lvalue in C. |
| 338 | CL_AddressableVoid, // Void expression whose address can be taken in C. |
| 339 | CL_DuplicateVectorComponents, // A vector shuffle with dupes. |
| 340 | CL_MemberFunction, // An expression referring to a member function |
| 341 | CL_SubObjCPropertySetting, |
| 342 | CL_ClassTemporary, // A temporary of class type, or subobject thereof. |
| 343 | CL_ArrayTemporary, // A temporary of array type. |
| 344 | CL_ObjCMessageRValue, // ObjC message is an rvalue |
| 345 | CL_PRValue // A prvalue for any other reason, of any other type |
| 346 | }; |
| 347 | /// The results of modification testing. |
| 348 | enum ModifiableType { |
| 349 | CM_Untested, // testModifiable was false. |
| 350 | CM_Modifiable, |
| 351 | CM_RValue, // Not modifiable because it's an rvalue |
| 352 | CM_Function, // Not modifiable because it's a function; C++ only |
| 353 | CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext |
| 354 | CM_NoSetterProperty,// Implicit assignment to ObjC property without setter |
| 355 | CM_ConstQualified, |
| 356 | CM_ConstQualifiedField, |
| 357 | CM_ConstAddrSpace, |
| 358 | CM_ArrayType, |
| 359 | CM_IncompleteType |
| 360 | }; |
| 361 | |
| 362 | private: |
| 363 | friend class Expr; |
| 364 | |
| 365 | unsigned short Kind; |
| 366 | unsigned short Modifiable; |
| 367 | |
| 368 | explicit Classification(Kinds k, ModifiableType m) |
| 369 | : Kind(k), Modifiable(m) |
| 370 | {} |
| 371 | |
| 372 | public: |
| 373 | Classification() {} |
| 374 | |
| 375 | Kinds getKind() const { return static_cast<Kinds>(Kind); } |
| 376 | ModifiableType getModifiable() const { |
| 377 | assert(Modifiable != CM_Untested && "Did not test for modifiability." ); |
| 378 | return static_cast<ModifiableType>(Modifiable); |
| 379 | } |
| 380 | bool isLValue() const { return Kind == CL_LValue; } |
| 381 | bool isXValue() const { return Kind == CL_XValue; } |
| 382 | bool isGLValue() const { return Kind <= CL_XValue; } |
| 383 | bool isPRValue() const { return Kind >= CL_Function; } |
| 384 | bool isRValue() const { return Kind >= CL_XValue; } |
| 385 | bool isModifiable() const { return getModifiable() == CM_Modifiable; } |
| 386 | |
| 387 | /// Create a simple, modifiable lvalue |
| 388 | static Classification makeSimpleLValue() { |
| 389 | return Classification(CL_LValue, CM_Modifiable); |
| 390 | } |
| 391 | |
| 392 | }; |
| 393 | /// Classify - Classify this expression according to the C++11 |
| 394 | /// expression taxonomy. |
| 395 | /// |
| 396 | /// C++11 defines ([basic.lval]) a new taxonomy of expressions to replace the |
| 397 | /// old lvalue vs rvalue. This function determines the type of expression this |
| 398 | /// is. There are three expression types: |
| 399 | /// - lvalues are classical lvalues as in C++03. |
| 400 | /// - prvalues are equivalent to rvalues in C++03. |
| 401 | /// - xvalues are expressions yielding unnamed rvalue references, e.g. a |
| 402 | /// function returning an rvalue reference. |
| 403 | /// lvalues and xvalues are collectively referred to as glvalues, while |
| 404 | /// prvalues and xvalues together form rvalues. |
| 405 | Classification Classify(ASTContext &Ctx) const { |
| 406 | return ClassifyImpl(Ctx, Loc: nullptr); |
| 407 | } |
| 408 | |
| 409 | /// ClassifyModifiable - Classify this expression according to the |
| 410 | /// C++11 expression taxonomy, and see if it is valid on the left side |
| 411 | /// of an assignment. |
| 412 | /// |
| 413 | /// This function extends classify in that it also tests whether the |
| 414 | /// expression is modifiable (C99 6.3.2.1p1). |
| 415 | /// \param Loc A source location that might be filled with a relevant location |
| 416 | /// if the expression is not modifiable. |
| 417 | Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{ |
| 418 | return ClassifyImpl(Ctx, Loc: &Loc); |
| 419 | } |
| 420 | |
| 421 | /// Returns the set of floating point options that apply to this expression. |
| 422 | /// Only meaningful for operations on floating point values. |
| 423 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const; |
| 424 | |
| 425 | /// getValueKindForType - Given a formal return or parameter type, |
| 426 | /// give its value kind. |
| 427 | static ExprValueKind getValueKindForType(QualType T) { |
| 428 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 429 | return (isa<LValueReferenceType>(Val: RT) |
| 430 | ? VK_LValue |
| 431 | : (RT->getPointeeType()->isFunctionType() |
| 432 | ? VK_LValue : VK_XValue)); |
| 433 | return VK_PRValue; |
| 434 | } |
| 435 | |
| 436 | /// getValueKind - The value kind that this expression produces. |
| 437 | ExprValueKind getValueKind() const { |
| 438 | return static_cast<ExprValueKind>(ExprBits.ValueKind); |
| 439 | } |
| 440 | |
| 441 | /// getObjectKind - The object kind that this expression produces. |
| 442 | /// Object kinds are meaningful only for expressions that yield an |
| 443 | /// l-value or x-value. |
| 444 | ExprObjectKind getObjectKind() const { |
| 445 | return static_cast<ExprObjectKind>(ExprBits.ObjectKind); |
| 446 | } |
| 447 | |
| 448 | bool isOrdinaryOrBitFieldObject() const { |
| 449 | ExprObjectKind OK = getObjectKind(); |
| 450 | return (OK == OK_Ordinary || OK == OK_BitField); |
| 451 | } |
| 452 | |
| 453 | /// setValueKind - Set the value kind produced by this expression. |
| 454 | void setValueKind(ExprValueKind Cat) { ExprBits.ValueKind = Cat; } |
| 455 | |
| 456 | /// setObjectKind - Set the object kind produced by this expression. |
| 457 | void setObjectKind(ExprObjectKind Cat) { ExprBits.ObjectKind = Cat; } |
| 458 | |
| 459 | private: |
| 460 | Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const; |
| 461 | |
| 462 | public: |
| 463 | |
| 464 | /// Returns true if this expression is a gl-value that |
| 465 | /// potentially refers to a bit-field. |
| 466 | /// |
| 467 | /// In C++, whether a gl-value refers to a bitfield is essentially |
| 468 | /// an aspect of the value-kind type system. |
| 469 | bool refersToBitField() const { return getObjectKind() == OK_BitField; } |
| 470 | |
| 471 | /// If this expression refers to a bit-field, retrieve the |
| 472 | /// declaration of that bit-field. |
| 473 | /// |
| 474 | /// Note that this returns a non-null pointer in subtly different |
| 475 | /// places than refersToBitField returns true. In particular, this can |
| 476 | /// return a non-null pointer even for r-values loaded from |
| 477 | /// bit-fields, but it will return null for a conditional bit-field. |
| 478 | FieldDecl *getSourceBitField(); |
| 479 | |
| 480 | /// If this expression refers to an enum constant, retrieve its declaration |
| 481 | EnumConstantDecl *getEnumConstantDecl(); |
| 482 | |
| 483 | const EnumConstantDecl *getEnumConstantDecl() const { |
| 484 | return const_cast<Expr *>(this)->getEnumConstantDecl(); |
| 485 | } |
| 486 | |
| 487 | const FieldDecl *getSourceBitField() const { |
| 488 | return const_cast<Expr*>(this)->getSourceBitField(); |
| 489 | } |
| 490 | |
| 491 | Decl *getReferencedDeclOfCallee(); |
| 492 | const Decl *getReferencedDeclOfCallee() const { |
| 493 | return const_cast<Expr*>(this)->getReferencedDeclOfCallee(); |
| 494 | } |
| 495 | |
| 496 | /// If this expression is an l-value for an Objective C |
| 497 | /// property, find the underlying property reference expression. |
| 498 | const ObjCPropertyRefExpr *getObjCProperty() const; |
| 499 | |
| 500 | /// Check if this expression is the ObjC 'self' implicit parameter. |
| 501 | bool isObjCSelfExpr() const; |
| 502 | |
| 503 | /// Returns whether this expression refers to a vector element. |
| 504 | bool refersToVectorElement() const; |
| 505 | |
| 506 | /// Returns whether this expression refers to a matrix element. |
| 507 | bool refersToMatrixElement() const { |
| 508 | return getObjectKind() == OK_MatrixComponent; |
| 509 | } |
| 510 | |
| 511 | /// Returns whether this expression refers to a global register |
| 512 | /// variable. |
| 513 | bool refersToGlobalRegisterVar() const; |
| 514 | |
| 515 | /// Returns whether this expression has a placeholder type. |
| 516 | bool hasPlaceholderType() const { |
| 517 | return getType()->isPlaceholderType(); |
| 518 | } |
| 519 | |
| 520 | /// Returns whether this expression has a specific placeholder type. |
| 521 | bool hasPlaceholderType(BuiltinType::Kind K) const { |
| 522 | assert(BuiltinType::isPlaceholderTypeKind(K)); |
| 523 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(Val: getType())) |
| 524 | return BT->getKind() == K; |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | /// isKnownToHaveBooleanValue - Return true if this is an integer expression |
| 529 | /// that is known to return 0 or 1. This happens for _Bool/bool expressions |
| 530 | /// but also int expressions which are produced by things like comparisons in |
| 531 | /// C. |
| 532 | /// |
| 533 | /// \param Semantic If true, only return true for expressions that are known |
| 534 | /// to be semantically boolean, which might not be true even for expressions |
| 535 | /// that are known to evaluate to 0/1. For instance, reading an unsigned |
| 536 | /// bit-field with width '1' will evaluate to 0/1, but doesn't necessarily |
| 537 | /// semantically correspond to a bool. |
| 538 | bool isKnownToHaveBooleanValue(bool Semantic = true) const; |
| 539 | |
| 540 | /// Check whether this array fits the idiom of a flexible array member, |
| 541 | /// depending on the value of -fstrict-flex-array. |
| 542 | /// When IgnoreTemplateOrMacroSubstitution is set, it doesn't consider sizes |
| 543 | /// resulting from the substitution of a macro or a template as special sizes. |
| 544 | bool isFlexibleArrayMemberLike( |
| 545 | const ASTContext &Context, |
| 546 | LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, |
| 547 | bool IgnoreTemplateOrMacroSubstitution = false) const; |
| 548 | |
| 549 | /// isIntegerConstantExpr - Return the value if this expression is a valid |
| 550 | /// integer constant expression. If not a valid i-c-e, return std::nullopt |
| 551 | /// and fill in Loc (if specified) with the location of the invalid |
| 552 | /// expression. |
| 553 | /// |
| 554 | /// Note: This does not perform the implicit conversions required by C++11 |
| 555 | /// [expr.const]p5. |
| 556 | std::optional<llvm::APSInt> |
| 557 | getIntegerConstantExpr(const ASTContext &Ctx, |
| 558 | SourceLocation *Loc = nullptr) const; |
| 559 | bool isIntegerConstantExpr(const ASTContext &Ctx, |
| 560 | SourceLocation *Loc = nullptr) const; |
| 561 | |
| 562 | /// isCXX98IntegralConstantExpr - Return true if this expression is an |
| 563 | /// integral constant expression in C++98. Can only be used in C++. |
| 564 | bool isCXX98IntegralConstantExpr(const ASTContext &Ctx) const; |
| 565 | |
| 566 | /// isCXX11ConstantExpr - Return true if this expression is a constant |
| 567 | /// expression in C++11. Can only be used in C++. |
| 568 | /// |
| 569 | /// Note: This does not perform the implicit conversions required by C++11 |
| 570 | /// [expr.const]p5. |
| 571 | bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result = nullptr, |
| 572 | SourceLocation *Loc = nullptr) const; |
| 573 | |
| 574 | /// isPotentialConstantExpr - Return true if this function's definition |
| 575 | /// might be usable in a constant expression in C++11, if it were marked |
| 576 | /// constexpr. Return false if the function can never produce a constant |
| 577 | /// expression, along with diagnostics describing why not. |
| 578 | static bool isPotentialConstantExpr(const FunctionDecl *FD, |
| 579 | SmallVectorImpl< |
| 580 | PartialDiagnosticAt> &Diags); |
| 581 | |
| 582 | /// isPotentialConstantExprUnevaluated - Return true if this expression might |
| 583 | /// be usable in a constant expression in C++11 in an unevaluated context, if |
| 584 | /// it were in function FD marked constexpr. Return false if the function can |
| 585 | /// never produce a constant expression, along with diagnostics describing |
| 586 | /// why not. |
| 587 | static bool isPotentialConstantExprUnevaluated(Expr *E, |
| 588 | const FunctionDecl *FD, |
| 589 | SmallVectorImpl< |
| 590 | PartialDiagnosticAt> &Diags); |
| 591 | |
| 592 | /// isConstantInitializer - Returns true if this expression can be emitted to |
| 593 | /// IR as a constant, and thus can be used as a constant initializer in C. |
| 594 | /// If this expression is not constant and Culprit is non-null, |
| 595 | /// it is used to store the address of first non constant expr. |
| 596 | bool isConstantInitializer(ASTContext &Ctx, bool ForRef, |
| 597 | const Expr **Culprit = nullptr) const; |
| 598 | |
| 599 | /// If this expression is an unambiguous reference to a single declaration, |
| 600 | /// in the style of __builtin_function_start, return that declaration. Note |
| 601 | /// that this may return a non-static member function or field in C++ if this |
| 602 | /// expression is a member pointer constant. |
| 603 | const ValueDecl *getAsBuiltinConstantDeclRef(const ASTContext &Context) const; |
| 604 | |
| 605 | /// EvalStatus is a struct with detailed info about an evaluation in progress. |
| 606 | struct EvalStatus { |
| 607 | /// Whether the evaluated expression has side effects. |
| 608 | /// For example, (f() && 0) can be folded, but it still has side effects. |
| 609 | bool HasSideEffects = false; |
| 610 | |
| 611 | /// Whether the evaluation hit undefined behavior. |
| 612 | /// For example, 1.0 / 0.0 can be folded to Inf, but has undefined behavior. |
| 613 | /// Likewise, INT_MAX + 1 can be folded to INT_MIN, but has UB. |
| 614 | bool HasUndefinedBehavior = false; |
| 615 | |
| 616 | /// Diag - If this is non-null, it will be filled in with a stack of notes |
| 617 | /// indicating why evaluation failed (or why it failed to produce a constant |
| 618 | /// expression). |
| 619 | /// If the expression is unfoldable, the notes will indicate why it's not |
| 620 | /// foldable. If the expression is foldable, but not a constant expression, |
| 621 | /// the notes will describes why it isn't a constant expression. If the |
| 622 | /// expression *is* a constant expression, no notes will be produced. |
| 623 | /// |
| 624 | /// FIXME: this causes significant performance concerns and should be |
| 625 | /// refactored at some point. Not all evaluations of the constant |
| 626 | /// expression interpreter will display the given diagnostics, this means |
| 627 | /// those kinds of uses are paying the expense of generating a diagnostic |
| 628 | /// (which may include expensive operations like converting APValue objects |
| 629 | /// to a string representation). |
| 630 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr; |
| 631 | |
| 632 | EvalStatus() = default; |
| 633 | |
| 634 | // hasSideEffects - Return true if the evaluated expression has |
| 635 | // side effects. |
| 636 | bool hasSideEffects() const { |
| 637 | return HasSideEffects; |
| 638 | } |
| 639 | }; |
| 640 | |
| 641 | /// EvalResult is a struct with detailed info about an evaluated expression. |
| 642 | struct EvalResult : EvalStatus { |
| 643 | /// Val - This is the value the expression can be folded to. |
| 644 | APValue Val; |
| 645 | |
| 646 | // isGlobalLValue - Return true if the evaluated lvalue expression |
| 647 | // is global. |
| 648 | bool isGlobalLValue() const; |
| 649 | }; |
| 650 | |
| 651 | /// EvaluateAsRValue - Return true if this is a constant which we can fold to |
| 652 | /// an rvalue using any crazy technique (that has nothing to do with language |
| 653 | /// standards) that we want to, even if the expression has side-effects. If |
| 654 | /// this function returns true, it returns the folded constant in Result. If |
| 655 | /// the expression is a glvalue, an lvalue-to-rvalue conversion will be |
| 656 | /// applied. |
| 657 | bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
| 658 | bool InConstantContext = false) const; |
| 659 | |
| 660 | /// EvaluateAsBooleanCondition - Return true if this is a constant |
| 661 | /// which we can fold and convert to a boolean condition using |
| 662 | /// any crazy technique that we want to, even if the expression has |
| 663 | /// side-effects. |
| 664 | bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
| 665 | bool InConstantContext = false) const; |
| 666 | |
| 667 | enum SideEffectsKind { |
| 668 | SE_NoSideEffects, ///< Strictly evaluate the expression. |
| 669 | SE_AllowUndefinedBehavior, ///< Allow UB that we can give a value, but not |
| 670 | ///< arbitrary unmodeled side effects. |
| 671 | SE_AllowSideEffects ///< Allow any unmodeled side effect. |
| 672 | }; |
| 673 | |
| 674 | /// EvaluateAsInt - Return true if this is a constant which we can fold and |
| 675 | /// convert to an integer, using any crazy technique that we want to. |
| 676 | bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
| 677 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, |
| 678 | bool InConstantContext = false) const; |
| 679 | |
| 680 | /// EvaluateAsFloat - Return true if this is a constant which we can fold and |
| 681 | /// convert to a floating point value, using any crazy technique that we |
| 682 | /// want to. |
| 683 | bool EvaluateAsFloat(llvm::APFloat &Result, const ASTContext &Ctx, |
| 684 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, |
| 685 | bool InConstantContext = false) const; |
| 686 | |
| 687 | /// EvaluateAsFixedPoint - Return true if this is a constant which we can fold |
| 688 | /// and convert to a fixed point value. |
| 689 | bool EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
| 690 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, |
| 691 | bool InConstantContext = false) const; |
| 692 | |
| 693 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 694 | /// constant folded without side-effects, but discard the result. |
| 695 | bool isEvaluatable(const ASTContext &Ctx, |
| 696 | SideEffectsKind AllowSideEffects = SE_NoSideEffects) const; |
| 697 | |
| 698 | /// HasSideEffects - This routine returns true for all those expressions |
| 699 | /// which have any effect other than producing a value. Example is a function |
| 700 | /// call, volatile variable read, or throwing an exception. If |
| 701 | /// IncludePossibleEffects is false, this call treats certain expressions with |
| 702 | /// potential side effects (such as function call-like expressions, |
| 703 | /// instantiation-dependent expressions, or invocations from a macro) as not |
| 704 | /// having side effects. |
| 705 | bool HasSideEffects(const ASTContext &Ctx, |
| 706 | bool IncludePossibleEffects = true) const; |
| 707 | |
| 708 | /// Determine whether this expression involves a call to any function |
| 709 | /// that is not trivial. |
| 710 | bool hasNonTrivialCall(const ASTContext &Ctx) const; |
| 711 | |
| 712 | /// EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded |
| 713 | /// integer. This must be called on an expression that constant folds to an |
| 714 | /// integer. |
| 715 | llvm::APSInt EvaluateKnownConstInt( |
| 716 | const ASTContext &Ctx, |
| 717 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const; |
| 718 | |
| 719 | llvm::APSInt EvaluateKnownConstIntCheckOverflow( |
| 720 | const ASTContext &Ctx, |
| 721 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const; |
| 722 | |
| 723 | void EvaluateForOverflow(const ASTContext &Ctx) const; |
| 724 | |
| 725 | /// EvaluateAsLValue - Evaluate an expression to see if we can fold it to an |
| 726 | /// lvalue with link time known address, with no side-effects. |
| 727 | bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
| 728 | bool InConstantContext = false) const; |
| 729 | |
| 730 | /// EvaluateAsInitializer - Evaluate an expression as if it were the |
| 731 | /// initializer of the given declaration. Returns true if the initializer |
| 732 | /// can be folded to a constant, and produces any relevant notes. In C++11, |
| 733 | /// notes will be produced if the expression is not a constant expression. |
| 734 | bool EvaluateAsInitializer(APValue &Result, const ASTContext &Ctx, |
| 735 | const VarDecl *VD, |
| 736 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
| 737 | bool IsConstantInitializer) const; |
| 738 | |
| 739 | /// EvaluateWithSubstitution - Evaluate an expression as if from the context |
| 740 | /// of a call to the given function with the given arguments, inside an |
| 741 | /// unevaluated context. Returns true if the expression could be folded to a |
| 742 | /// constant. |
| 743 | bool EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 744 | const FunctionDecl *Callee, |
| 745 | ArrayRef<const Expr*> Args, |
| 746 | const Expr *This = nullptr) const; |
| 747 | |
| 748 | enum class ConstantExprKind { |
| 749 | /// An integer constant expression (an array bound, enumerator, case value, |
| 750 | /// bit-field width, or similar) or similar. |
| 751 | Normal, |
| 752 | /// A non-class template argument. Such a value is only used for mangling, |
| 753 | /// not for code generation, so can refer to dllimported functions. |
| 754 | NonClassTemplateArgument, |
| 755 | /// A class template argument. Such a value is used for code generation. |
| 756 | ClassTemplateArgument, |
| 757 | /// An immediate invocation. The destruction of the end result of this |
| 758 | /// evaluation is not part of the evaluation, but all other temporaries |
| 759 | /// are destroyed. |
| 760 | ImmediateInvocation, |
| 761 | }; |
| 762 | |
| 763 | /// Evaluate an expression that is required to be a constant expression. Does |
| 764 | /// not check the syntactic constraints for C and C++98 constant expressions. |
| 765 | bool EvaluateAsConstantExpr( |
| 766 | EvalResult &Result, const ASTContext &Ctx, |
| 767 | ConstantExprKind Kind = ConstantExprKind::Normal) const; |
| 768 | |
| 769 | /// If the current Expr is a pointer, this will try to statically |
| 770 | /// determine the number of bytes available where the pointer is pointing. |
| 771 | /// Returns true if all of the above holds and we were able to figure out the |
| 772 | /// size, false otherwise. |
| 773 | /// |
| 774 | /// \param Type - How to evaluate the size of the Expr, as defined by the |
| 775 | /// "type" parameter of __builtin_object_size |
| 776 | bool tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
| 777 | unsigned Type) const; |
| 778 | |
| 779 | /// If the current Expr is a pointer, this will try to statically |
| 780 | /// determine the strlen of the string pointed to. |
| 781 | /// Returns true if all of the above holds and we were able to figure out the |
| 782 | /// strlen, false otherwise. |
| 783 | bool tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const; |
| 784 | |
| 785 | bool EvaluateCharRangeAsString(std::string &Result, |
| 786 | const Expr *SizeExpression, |
| 787 | const Expr *PtrExpression, ASTContext &Ctx, |
| 788 | EvalResult &Status) const; |
| 789 | |
| 790 | bool EvaluateCharRangeAsString(APValue &Result, const Expr *SizeExpression, |
| 791 | const Expr *PtrExpression, ASTContext &Ctx, |
| 792 | EvalResult &Status) const; |
| 793 | |
| 794 | /// If the current Expr can be evaluated to a pointer to a null-terminated |
| 795 | /// constant string, return the constant string (without the terminating |
| 796 | /// null). |
| 797 | std::optional<std::string> tryEvaluateString(ASTContext &Ctx) const; |
| 798 | |
| 799 | /// Enumeration used to describe the kind of Null pointer constant |
| 800 | /// returned from \c isNullPointerConstant(). |
| 801 | enum NullPointerConstantKind { |
| 802 | /// Expression is not a Null pointer constant. |
| 803 | NPCK_NotNull = 0, |
| 804 | |
| 805 | /// Expression is a Null pointer constant built from a zero integer |
| 806 | /// expression that is not a simple, possibly parenthesized, zero literal. |
| 807 | /// C++ Core Issue 903 will classify these expressions as "not pointers" |
| 808 | /// once it is adopted. |
| 809 | /// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 810 | NPCK_ZeroExpression, |
| 811 | |
| 812 | /// Expression is a Null pointer constant built from a literal zero. |
| 813 | NPCK_ZeroLiteral, |
| 814 | |
| 815 | /// Expression is a C++11 nullptr. |
| 816 | NPCK_CXX11_nullptr, |
| 817 | |
| 818 | /// Expression is a GNU-style __null constant. |
| 819 | NPCK_GNUNull |
| 820 | }; |
| 821 | |
| 822 | /// Enumeration used to describe how \c isNullPointerConstant() |
| 823 | /// should cope with value-dependent expressions. |
| 824 | enum NullPointerConstantValueDependence { |
| 825 | /// Specifies that the expression should never be value-dependent. |
| 826 | NPC_NeverValueDependent = 0, |
| 827 | |
| 828 | /// Specifies that a value-dependent expression of integral or |
| 829 | /// dependent type should be considered a null pointer constant. |
| 830 | NPC_ValueDependentIsNull, |
| 831 | |
| 832 | /// Specifies that a value-dependent expression should be considered |
| 833 | /// to never be a null pointer constant. |
| 834 | NPC_ValueDependentIsNotNull |
| 835 | }; |
| 836 | |
| 837 | /// isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to |
| 838 | /// a Null pointer constant. The return value can further distinguish the |
| 839 | /// kind of NULL pointer constant that was detected. |
| 840 | NullPointerConstantKind isNullPointerConstant( |
| 841 | ASTContext &Ctx, |
| 842 | NullPointerConstantValueDependence NPC) const; |
| 843 | |
| 844 | /// isOBJCGCCandidate - Return true if this expression may be used in a read/ |
| 845 | /// write barrier. |
| 846 | bool isOBJCGCCandidate(ASTContext &Ctx) const; |
| 847 | |
| 848 | /// Returns true if this expression is a bound member function. |
| 849 | bool isBoundMemberFunction(ASTContext &Ctx) const; |
| 850 | |
| 851 | /// Given an expression of bound-member type, find the type |
| 852 | /// of the member. Returns null if this is an *overloaded* bound |
| 853 | /// member expression. |
| 854 | static QualType findBoundMemberType(const Expr *expr); |
| 855 | |
| 856 | /// Skip past any invisible AST nodes which might surround this |
| 857 | /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes, |
| 858 | /// but also injected CXXMemberExpr and CXXConstructExpr which represent |
| 859 | /// implicit conversions. |
| 860 | Expr *IgnoreUnlessSpelledInSource(); |
| 861 | const Expr *IgnoreUnlessSpelledInSource() const { |
| 862 | return const_cast<Expr *>(this)->IgnoreUnlessSpelledInSource(); |
| 863 | } |
| 864 | |
| 865 | /// Skip past any implicit casts which might surround this expression until |
| 866 | /// reaching a fixed point. Skips: |
| 867 | /// * ImplicitCastExpr |
| 868 | /// * FullExpr |
| 869 | Expr *IgnoreImpCasts() LLVM_READONLY; |
| 870 | const Expr *IgnoreImpCasts() const { |
| 871 | return const_cast<Expr *>(this)->IgnoreImpCasts(); |
| 872 | } |
| 873 | |
| 874 | /// Skip past any casts which might surround this expression until reaching |
| 875 | /// a fixed point. Skips: |
| 876 | /// * CastExpr |
| 877 | /// * FullExpr |
| 878 | /// * MaterializeTemporaryExpr |
| 879 | /// * SubstNonTypeTemplateParmExpr |
| 880 | Expr *IgnoreCasts() LLVM_READONLY; |
| 881 | const Expr *IgnoreCasts() const { |
| 882 | return const_cast<Expr *>(this)->IgnoreCasts(); |
| 883 | } |
| 884 | |
| 885 | /// Skip past any implicit AST nodes which might surround this expression |
| 886 | /// until reaching a fixed point. Skips: |
| 887 | /// * What IgnoreImpCasts() skips |
| 888 | /// * MaterializeTemporaryExpr |
| 889 | /// * CXXBindTemporaryExpr |
| 890 | Expr *IgnoreImplicit() LLVM_READONLY; |
| 891 | const Expr *IgnoreImplicit() const { |
| 892 | return const_cast<Expr *>(this)->IgnoreImplicit(); |
| 893 | } |
| 894 | |
| 895 | /// Skip past any implicit AST nodes which might surround this expression |
| 896 | /// until reaching a fixed point. Same as IgnoreImplicit, except that it |
| 897 | /// also skips over implicit calls to constructors and conversion functions. |
| 898 | /// |
| 899 | /// FIXME: Should IgnoreImplicit do this? |
| 900 | Expr *IgnoreImplicitAsWritten() LLVM_READONLY; |
| 901 | const Expr *IgnoreImplicitAsWritten() const { |
| 902 | return const_cast<Expr *>(this)->IgnoreImplicitAsWritten(); |
| 903 | } |
| 904 | |
| 905 | /// Skip past any parentheses which might surround this expression until |
| 906 | /// reaching a fixed point. Skips: |
| 907 | /// * ParenExpr |
| 908 | /// * UnaryOperator if `UO_Extension` |
| 909 | /// * GenericSelectionExpr if `!isResultDependent()` |
| 910 | /// * ChooseExpr if `!isConditionDependent()` |
| 911 | /// * ConstantExpr |
| 912 | Expr *IgnoreParens() LLVM_READONLY; |
| 913 | const Expr *IgnoreParens() const { |
| 914 | return const_cast<Expr *>(this)->IgnoreParens(); |
| 915 | } |
| 916 | |
| 917 | /// Skip past any parentheses and implicit casts which might surround this |
| 918 | /// expression until reaching a fixed point. |
| 919 | /// FIXME: IgnoreParenImpCasts really ought to be equivalent to |
| 920 | /// IgnoreParens() + IgnoreImpCasts() until reaching a fixed point. However |
| 921 | /// this is currently not the case. Instead IgnoreParenImpCasts() skips: |
| 922 | /// * What IgnoreParens() skips |
| 923 | /// * What IgnoreImpCasts() skips |
| 924 | /// * MaterializeTemporaryExpr |
| 925 | /// * SubstNonTypeTemplateParmExpr |
| 926 | Expr *IgnoreParenImpCasts() LLVM_READONLY; |
| 927 | const Expr *IgnoreParenImpCasts() const { |
| 928 | return const_cast<Expr *>(this)->IgnoreParenImpCasts(); |
| 929 | } |
| 930 | |
| 931 | /// Skip past any parentheses and casts which might surround this expression |
| 932 | /// until reaching a fixed point. Skips: |
| 933 | /// * What IgnoreParens() skips |
| 934 | /// * What IgnoreCasts() skips |
| 935 | Expr *IgnoreParenCasts() LLVM_READONLY; |
| 936 | const Expr *IgnoreParenCasts() const { |
| 937 | return const_cast<Expr *>(this)->IgnoreParenCasts(); |
| 938 | } |
| 939 | |
| 940 | /// Skip conversion operators. If this Expr is a call to a conversion |
| 941 | /// operator, return the argument. |
| 942 | Expr *IgnoreConversionOperatorSingleStep() LLVM_READONLY; |
| 943 | const Expr *IgnoreConversionOperatorSingleStep() const { |
| 944 | return const_cast<Expr *>(this)->IgnoreConversionOperatorSingleStep(); |
| 945 | } |
| 946 | |
| 947 | /// Skip past any parentheses and lvalue casts which might surround this |
| 948 | /// expression until reaching a fixed point. Skips: |
| 949 | /// * What IgnoreParens() skips |
| 950 | /// * What IgnoreCasts() skips, except that only lvalue-to-rvalue |
| 951 | /// casts are skipped |
| 952 | /// FIXME: This is intended purely as a temporary workaround for code |
| 953 | /// that hasn't yet been rewritten to do the right thing about those |
| 954 | /// casts, and may disappear along with the last internal use. |
| 955 | Expr *IgnoreParenLValueCasts() LLVM_READONLY; |
| 956 | const Expr *IgnoreParenLValueCasts() const { |
| 957 | return const_cast<Expr *>(this)->IgnoreParenLValueCasts(); |
| 958 | } |
| 959 | |
| 960 | /// Skip past any parentheses and casts which do not change the value |
| 961 | /// (including ptr->int casts of the same size) until reaching a fixed point. |
| 962 | /// Skips: |
| 963 | /// * What IgnoreParens() skips |
| 964 | /// * CastExpr which do not change the value |
| 965 | /// * SubstNonTypeTemplateParmExpr |
| 966 | Expr *IgnoreParenNoopCasts(const ASTContext &Ctx) LLVM_READONLY; |
| 967 | const Expr *IgnoreParenNoopCasts(const ASTContext &Ctx) const { |
| 968 | return const_cast<Expr *>(this)->IgnoreParenNoopCasts(Ctx); |
| 969 | } |
| 970 | |
| 971 | /// Skip past any parentheses and derived-to-base casts until reaching a |
| 972 | /// fixed point. Skips: |
| 973 | /// * What IgnoreParens() skips |
| 974 | /// * CastExpr which represent a derived-to-base cast (CK_DerivedToBase, |
| 975 | /// CK_UncheckedDerivedToBase and CK_NoOp) |
| 976 | Expr *IgnoreParenBaseCasts() LLVM_READONLY; |
| 977 | const Expr *IgnoreParenBaseCasts() const { |
| 978 | return const_cast<Expr *>(this)->IgnoreParenBaseCasts(); |
| 979 | } |
| 980 | |
| 981 | /// Determine whether this expression is a default function argument. |
| 982 | /// |
| 983 | /// Default arguments are implicitly generated in the abstract syntax tree |
| 984 | /// by semantic analysis for function calls, object constructions, etc. in |
| 985 | /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; |
| 986 | /// this routine also looks through any implicit casts to determine whether |
| 987 | /// the expression is a default argument. |
| 988 | bool isDefaultArgument() const; |
| 989 | |
| 990 | /// Determine whether the result of this expression is a |
| 991 | /// temporary object of the given class type. |
| 992 | bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const; |
| 993 | |
| 994 | /// Whether this expression is an implicit reference to 'this' in C++. |
| 995 | bool isImplicitCXXThis() const; |
| 996 | |
| 997 | static bool hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs); |
| 998 | |
| 999 | /// For an expression of class type or pointer to class type, |
| 1000 | /// return the most derived class decl the expression is known to refer to. |
| 1001 | /// |
| 1002 | /// If this expression is a cast, this method looks through it to find the |
| 1003 | /// most derived decl that can be inferred from the expression. |
| 1004 | /// This is valid because derived-to-base conversions have undefined |
| 1005 | /// behavior if the object isn't dynamically of the derived type. |
| 1006 | const CXXRecordDecl *getBestDynamicClassType() const; |
| 1007 | |
| 1008 | /// Get the inner expression that determines the best dynamic class. |
| 1009 | /// If this is a prvalue, we guarantee that it is of the most-derived type |
| 1010 | /// for the object itself. |
| 1011 | const Expr *getBestDynamicClassTypeExpr() const; |
| 1012 | |
| 1013 | /// Walk outwards from an expression we want to bind a reference to and |
| 1014 | /// find the expression whose lifetime needs to be extended. Record |
| 1015 | /// the LHSs of comma expressions and adjustments needed along the path. |
| 1016 | const Expr *skipRValueSubobjectAdjustments( |
| 1017 | SmallVectorImpl<const Expr *> &CommaLHS, |
| 1018 | SmallVectorImpl<SubobjectAdjustment> &Adjustments) const; |
| 1019 | const Expr *skipRValueSubobjectAdjustments() const { |
| 1020 | SmallVector<const Expr *, 8> CommaLHSs; |
| 1021 | SmallVector<SubobjectAdjustment, 8> Adjustments; |
| 1022 | return skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
| 1023 | } |
| 1024 | |
| 1025 | /// Checks that the two Expr's will refer to the same value as a comparison |
| 1026 | /// operand. The caller must ensure that the values referenced by the Expr's |
| 1027 | /// are not modified between E1 and E2 or the result my be invalid. |
| 1028 | static bool isSameComparisonOperand(const Expr* E1, const Expr* E2); |
| 1029 | |
| 1030 | static bool classof(const Stmt *T) { |
| 1031 | return T->getStmtClass() >= firstExprConstant && |
| 1032 | T->getStmtClass() <= lastExprConstant; |
| 1033 | } |
| 1034 | }; |
| 1035 | // PointerLikeTypeTraits is specialized so it can be used with a forward-decl of |
| 1036 | // Expr. Verify that we got it right. |
| 1037 | static_assert(llvm::PointerLikeTypeTraits<Expr *>::NumLowBitsAvailable <= |
| 1038 | llvm::detail::ConstantLog2<alignof(Expr)>::value, |
| 1039 | "PointerLikeTypeTraits<Expr*> assumes too much alignment." ); |
| 1040 | |
| 1041 | using ConstantExprKind = Expr::ConstantExprKind; |
| 1042 | |
| 1043 | //===----------------------------------------------------------------------===// |
| 1044 | // Wrapper Expressions. |
| 1045 | //===----------------------------------------------------------------------===// |
| 1046 | |
| 1047 | /// FullExpr - Represents a "full-expression" node. |
| 1048 | class FullExpr : public Expr { |
| 1049 | protected: |
| 1050 | Stmt *SubExpr; |
| 1051 | |
| 1052 | FullExpr(StmtClass SC, Expr *subexpr) |
| 1053 | : Expr(SC, subexpr->getType(), subexpr->getValueKind(), |
| 1054 | subexpr->getObjectKind()), |
| 1055 | SubExpr(subexpr) { |
| 1056 | setDependence(computeDependence(E: this)); |
| 1057 | } |
| 1058 | FullExpr(StmtClass SC, EmptyShell Empty) |
| 1059 | : Expr(SC, Empty) {} |
| 1060 | public: |
| 1061 | const Expr *getSubExpr() const { return cast<Expr>(Val: SubExpr); } |
| 1062 | Expr *getSubExpr() { return cast<Expr>(Val: SubExpr); } |
| 1063 | |
| 1064 | /// As with any mutator of the AST, be very careful when modifying an |
| 1065 | /// existing AST to preserve its invariants. |
| 1066 | void setSubExpr(Expr *E) { SubExpr = E; } |
| 1067 | |
| 1068 | static bool classof(const Stmt *T) { |
| 1069 | return T->getStmtClass() >= firstFullExprConstant && |
| 1070 | T->getStmtClass() <= lastFullExprConstant; |
| 1071 | } |
| 1072 | }; |
| 1073 | |
| 1074 | /// Describes the kind of result that can be tail-allocated. |
| 1075 | enum class ConstantResultStorageKind { None, Int64, APValue }; |
| 1076 | |
| 1077 | /// ConstantExpr - An expression that occurs in a constant context and |
| 1078 | /// optionally the result of evaluating the expression. |
| 1079 | class ConstantExpr final |
| 1080 | : public FullExpr, |
| 1081 | private llvm::TrailingObjects<ConstantExpr, APValue, uint64_t> { |
| 1082 | static_assert(std::is_same<uint64_t, llvm::APInt::WordType>::value, |
| 1083 | "ConstantExpr assumes that llvm::APInt::WordType is uint64_t " |
| 1084 | "for tail-allocated storage" ); |
| 1085 | friend TrailingObjects; |
| 1086 | friend class ASTStmtReader; |
| 1087 | friend class ASTStmtWriter; |
| 1088 | |
| 1089 | size_t numTrailingObjects(OverloadToken<APValue>) const { |
| 1090 | return getResultStorageKind() == ConstantResultStorageKind::APValue; |
| 1091 | } |
| 1092 | size_t numTrailingObjects(OverloadToken<uint64_t>) const { |
| 1093 | return getResultStorageKind() == ConstantResultStorageKind::Int64; |
| 1094 | } |
| 1095 | |
| 1096 | uint64_t &Int64Result() { |
| 1097 | assert(getResultStorageKind() == ConstantResultStorageKind::Int64 && |
| 1098 | "invalid accessor" ); |
| 1099 | return *getTrailingObjects<uint64_t>(); |
| 1100 | } |
| 1101 | const uint64_t &Int64Result() const { |
| 1102 | return const_cast<ConstantExpr *>(this)->Int64Result(); |
| 1103 | } |
| 1104 | APValue &APValueResult() { |
| 1105 | assert(getResultStorageKind() == ConstantResultStorageKind::APValue && |
| 1106 | "invalid accessor" ); |
| 1107 | return *getTrailingObjects<APValue>(); |
| 1108 | } |
| 1109 | APValue &APValueResult() const { |
| 1110 | return const_cast<ConstantExpr *>(this)->APValueResult(); |
| 1111 | } |
| 1112 | |
| 1113 | ConstantExpr(Expr *SubExpr, ConstantResultStorageKind StorageKind, |
| 1114 | bool IsImmediateInvocation); |
| 1115 | ConstantExpr(EmptyShell Empty, ConstantResultStorageKind StorageKind); |
| 1116 | |
| 1117 | public: |
| 1118 | static ConstantExpr *Create(const ASTContext &Context, Expr *E, |
| 1119 | const APValue &Result); |
| 1120 | static ConstantExpr * |
| 1121 | Create(const ASTContext &Context, Expr *E, |
| 1122 | ConstantResultStorageKind Storage = ConstantResultStorageKind::None, |
| 1123 | bool IsImmediateInvocation = false); |
| 1124 | static ConstantExpr *CreateEmpty(const ASTContext &Context, |
| 1125 | ConstantResultStorageKind StorageKind); |
| 1126 | |
| 1127 | static ConstantResultStorageKind getStorageKind(const APValue &Value); |
| 1128 | static ConstantResultStorageKind getStorageKind(const Type *T, |
| 1129 | const ASTContext &Context); |
| 1130 | |
| 1131 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 1132 | return SubExpr->getBeginLoc(); |
| 1133 | } |
| 1134 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 1135 | return SubExpr->getEndLoc(); |
| 1136 | } |
| 1137 | |
| 1138 | static bool classof(const Stmt *T) { |
| 1139 | return T->getStmtClass() == ConstantExprClass; |
| 1140 | } |
| 1141 | |
| 1142 | void SetResult(APValue Value, const ASTContext &Context) { |
| 1143 | MoveIntoResult(Value, Context); |
| 1144 | } |
| 1145 | void MoveIntoResult(APValue &Value, const ASTContext &Context); |
| 1146 | |
| 1147 | APValue::ValueKind getResultAPValueKind() const { |
| 1148 | return static_cast<APValue::ValueKind>(ConstantExprBits.APValueKind); |
| 1149 | } |
| 1150 | ConstantResultStorageKind getResultStorageKind() const { |
| 1151 | return static_cast<ConstantResultStorageKind>(ConstantExprBits.ResultKind); |
| 1152 | } |
| 1153 | bool isImmediateInvocation() const { |
| 1154 | return ConstantExprBits.IsImmediateInvocation; |
| 1155 | } |
| 1156 | bool hasAPValueResult() const { |
| 1157 | return ConstantExprBits.APValueKind != APValue::None; |
| 1158 | } |
| 1159 | APValue getAPValueResult() const; |
| 1160 | llvm::APSInt getResultAsAPSInt() const; |
| 1161 | // Iterators |
| 1162 | child_range children() { return child_range(&SubExpr, &SubExpr+1); } |
| 1163 | const_child_range children() const { |
| 1164 | return const_child_range(&SubExpr, &SubExpr + 1); |
| 1165 | } |
| 1166 | }; |
| 1167 | |
| 1168 | //===----------------------------------------------------------------------===// |
| 1169 | // Primary Expressions. |
| 1170 | //===----------------------------------------------------------------------===// |
| 1171 | |
| 1172 | /// OpaqueValueExpr - An expression referring to an opaque object of a |
| 1173 | /// fixed type and value class. These don't correspond to concrete |
| 1174 | /// syntax; instead they're used to express operations (usually copy |
| 1175 | /// operations) on values whose source is generally obvious from |
| 1176 | /// context. |
| 1177 | class OpaqueValueExpr : public Expr { |
| 1178 | friend class ASTStmtReader; |
| 1179 | Expr *SourceExpr; |
| 1180 | |
| 1181 | public: |
| 1182 | OpaqueValueExpr(SourceLocation Loc, QualType T, ExprValueKind VK, |
| 1183 | ExprObjectKind OK = OK_Ordinary, Expr *SourceExpr = nullptr) |
| 1184 | : Expr(OpaqueValueExprClass, T, VK, OK), SourceExpr(SourceExpr) { |
| 1185 | setIsUnique(false); |
| 1186 | OpaqueValueExprBits.Loc = Loc; |
| 1187 | setDependence(computeDependence(E: this)); |
| 1188 | } |
| 1189 | |
| 1190 | /// Given an expression which invokes a copy constructor --- i.e. a |
| 1191 | /// CXXConstructExpr, possibly wrapped in an ExprWithCleanups --- |
| 1192 | /// find the OpaqueValueExpr that's the source of the construction. |
| 1193 | static const OpaqueValueExpr *findInCopyConstruct(const Expr *expr); |
| 1194 | |
| 1195 | explicit OpaqueValueExpr(EmptyShell Empty) |
| 1196 | : Expr(OpaqueValueExprClass, Empty) {} |
| 1197 | |
| 1198 | /// Retrieve the location of this expression. |
| 1199 | SourceLocation getLocation() const { return OpaqueValueExprBits.Loc; } |
| 1200 | |
| 1201 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 1202 | return SourceExpr ? SourceExpr->getBeginLoc() : getLocation(); |
| 1203 | } |
| 1204 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 1205 | return SourceExpr ? SourceExpr->getEndLoc() : getLocation(); |
| 1206 | } |
| 1207 | SourceLocation getExprLoc() const LLVM_READONLY { |
| 1208 | return SourceExpr ? SourceExpr->getExprLoc() : getLocation(); |
| 1209 | } |
| 1210 | |
| 1211 | child_range children() { |
| 1212 | return child_range(child_iterator(), child_iterator()); |
| 1213 | } |
| 1214 | |
| 1215 | const_child_range children() const { |
| 1216 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1217 | } |
| 1218 | |
| 1219 | /// The source expression of an opaque value expression is the |
| 1220 | /// expression which originally generated the value. This is |
| 1221 | /// provided as a convenience for analyses that don't wish to |
| 1222 | /// precisely model the execution behavior of the program. |
| 1223 | /// |
| 1224 | /// The source expression is typically set when building the |
| 1225 | /// expression which binds the opaque value expression in the first |
| 1226 | /// place. |
| 1227 | Expr *getSourceExpr() const { return SourceExpr; } |
| 1228 | |
| 1229 | void setIsUnique(bool V) { |
| 1230 | assert((!V || SourceExpr) && |
| 1231 | "unique OVEs are expected to have source expressions" ); |
| 1232 | OpaqueValueExprBits.IsUnique = V; |
| 1233 | } |
| 1234 | |
| 1235 | bool isUnique() const { return OpaqueValueExprBits.IsUnique; } |
| 1236 | |
| 1237 | static bool classof(const Stmt *T) { |
| 1238 | return T->getStmtClass() == OpaqueValueExprClass; |
| 1239 | } |
| 1240 | }; |
| 1241 | |
| 1242 | /// A reference to a declared variable, function, enum, etc. |
| 1243 | /// [C99 6.5.1p2] |
| 1244 | /// |
| 1245 | /// This encodes all the information about how a declaration is referenced |
| 1246 | /// within an expression. |
| 1247 | /// |
| 1248 | /// There are several optional constructs attached to DeclRefExprs only when |
| 1249 | /// they apply in order to conserve memory. These are laid out past the end of |
| 1250 | /// the object, and flags in the DeclRefExprBitfield track whether they exist: |
| 1251 | /// |
| 1252 | /// DeclRefExprBits.HasQualifier: |
| 1253 | /// Specifies when this declaration reference expression has a C++ |
| 1254 | /// nested-name-specifier. |
| 1255 | /// DeclRefExprBits.HasFoundDecl: |
| 1256 | /// Specifies when this declaration reference expression has a record of |
| 1257 | /// a NamedDecl (different from the referenced ValueDecl) which was found |
| 1258 | /// during name lookup and/or overload resolution. |
| 1259 | /// DeclRefExprBits.HasTemplateKWAndArgsInfo: |
| 1260 | /// Specifies when this declaration reference expression has an explicit |
| 1261 | /// C++ template keyword and/or template argument list. |
| 1262 | /// DeclRefExprBits.RefersToEnclosingVariableOrCapture |
| 1263 | /// Specifies when this declaration reference expression (validly) |
| 1264 | /// refers to an enclosed local or a captured variable. |
| 1265 | class DeclRefExpr final |
| 1266 | : public Expr, |
| 1267 | private llvm::TrailingObjects<DeclRefExpr, NestedNameSpecifierLoc, |
| 1268 | NamedDecl *, ASTTemplateKWAndArgsInfo, |
| 1269 | TemplateArgumentLoc> { |
| 1270 | friend class ASTStmtReader; |
| 1271 | friend class ASTStmtWriter; |
| 1272 | friend TrailingObjects; |
| 1273 | |
| 1274 | /// The declaration that we are referencing. |
| 1275 | ValueDecl *D; |
| 1276 | |
| 1277 | /// Provides source/type location info for the declaration name |
| 1278 | /// embedded in D. |
| 1279 | DeclarationNameLoc DNLoc; |
| 1280 | |
| 1281 | size_t numTrailingObjects(OverloadToken<NestedNameSpecifierLoc>) const { |
| 1282 | return hasQualifier(); |
| 1283 | } |
| 1284 | |
| 1285 | size_t numTrailingObjects(OverloadToken<NamedDecl *>) const { |
| 1286 | return hasFoundDecl(); |
| 1287 | } |
| 1288 | |
| 1289 | size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { |
| 1290 | return hasTemplateKWAndArgsInfo(); |
| 1291 | } |
| 1292 | |
| 1293 | /// Test whether there is a distinct FoundDecl attached to the end of |
| 1294 | /// this DRE. |
| 1295 | bool hasFoundDecl() const { return DeclRefExprBits.HasFoundDecl; } |
| 1296 | |
| 1297 | DeclRefExpr(const ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc, |
| 1298 | SourceLocation TemplateKWLoc, ValueDecl *D, |
| 1299 | bool RefersToEnclosingVariableOrCapture, |
| 1300 | const DeclarationNameInfo &NameInfo, NamedDecl *FoundD, |
| 1301 | const TemplateArgumentListInfo *TemplateArgs, QualType T, |
| 1302 | ExprValueKind VK, NonOdrUseReason NOUR); |
| 1303 | |
| 1304 | /// Construct an empty declaration reference expression. |
| 1305 | explicit DeclRefExpr(EmptyShell Empty) : Expr(DeclRefExprClass, Empty) {} |
| 1306 | |
| 1307 | public: |
| 1308 | DeclRefExpr(const ASTContext &Ctx, ValueDecl *D, |
| 1309 | bool RefersToEnclosingVariableOrCapture, QualType T, |
| 1310 | ExprValueKind VK, SourceLocation L, |
| 1311 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc(), |
| 1312 | NonOdrUseReason NOUR = NOUR_None); |
| 1313 | |
| 1314 | static DeclRefExpr * |
| 1315 | Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, |
| 1316 | SourceLocation TemplateKWLoc, ValueDecl *D, |
| 1317 | bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, |
| 1318 | QualType T, ExprValueKind VK, NamedDecl *FoundD = nullptr, |
| 1319 | const TemplateArgumentListInfo *TemplateArgs = nullptr, |
| 1320 | NonOdrUseReason NOUR = NOUR_None); |
| 1321 | |
| 1322 | static DeclRefExpr * |
| 1323 | Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, |
| 1324 | SourceLocation TemplateKWLoc, ValueDecl *D, |
| 1325 | bool RefersToEnclosingVariableOrCapture, |
| 1326 | const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK, |
| 1327 | NamedDecl *FoundD = nullptr, |
| 1328 | const TemplateArgumentListInfo *TemplateArgs = nullptr, |
| 1329 | NonOdrUseReason NOUR = NOUR_None); |
| 1330 | |
| 1331 | /// Construct an empty declaration reference expression. |
| 1332 | static DeclRefExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier, |
| 1333 | bool HasFoundDecl, |
| 1334 | bool HasTemplateKWAndArgsInfo, |
| 1335 | unsigned NumTemplateArgs); |
| 1336 | |
| 1337 | ValueDecl *getDecl() { return D; } |
| 1338 | const ValueDecl *getDecl() const { return D; } |
| 1339 | void setDecl(ValueDecl *NewD); |
| 1340 | |
| 1341 | DeclarationNameInfo getNameInfo() const { |
| 1342 | return DeclarationNameInfo(getDecl()->getDeclName(), getLocation(), DNLoc); |
| 1343 | } |
| 1344 | |
| 1345 | SourceLocation getLocation() const { return DeclRefExprBits.Loc; } |
| 1346 | void setLocation(SourceLocation L) { DeclRefExprBits.Loc = L; } |
| 1347 | |
| 1348 | SourceLocation getBeginLoc() const { |
| 1349 | if (hasQualifier()) |
| 1350 | return getQualifierLoc().getBeginLoc(); |
| 1351 | return DeclRefExprBits.Loc; |
| 1352 | } |
| 1353 | |
| 1354 | SourceLocation getEndLoc() const LLVM_READONLY; |
| 1355 | |
| 1356 | /// Determine whether this declaration reference was preceded by a |
| 1357 | /// C++ nested-name-specifier, e.g., \c N::foo. |
| 1358 | bool hasQualifier() const { return DeclRefExprBits.HasQualifier; } |
| 1359 | |
| 1360 | /// If the name was qualified, retrieves the nested-name-specifier |
| 1361 | /// that precedes the name, with source-location information. |
| 1362 | NestedNameSpecifierLoc getQualifierLoc() const { |
| 1363 | if (!hasQualifier()) |
| 1364 | return NestedNameSpecifierLoc(); |
| 1365 | return *getTrailingObjects<NestedNameSpecifierLoc>(); |
| 1366 | } |
| 1367 | |
| 1368 | /// If the name was qualified, retrieves the nested-name-specifier |
| 1369 | /// that precedes the name. Otherwise, returns NULL. |
| 1370 | NestedNameSpecifier *getQualifier() const { |
| 1371 | return getQualifierLoc().getNestedNameSpecifier(); |
| 1372 | } |
| 1373 | |
| 1374 | /// Get the NamedDecl through which this reference occurred. |
| 1375 | /// |
| 1376 | /// This Decl may be different from the ValueDecl actually referred to in the |
| 1377 | /// presence of using declarations, etc. It always returns non-NULL, and may |
| 1378 | /// simple return the ValueDecl when appropriate. |
| 1379 | |
| 1380 | NamedDecl *getFoundDecl() { |
| 1381 | return hasFoundDecl() ? *getTrailingObjects<NamedDecl *>() : D; |
| 1382 | } |
| 1383 | |
| 1384 | /// Get the NamedDecl through which this reference occurred. |
| 1385 | /// See non-const variant. |
| 1386 | const NamedDecl *getFoundDecl() const { |
| 1387 | return hasFoundDecl() ? *getTrailingObjects<NamedDecl *>() : D; |
| 1388 | } |
| 1389 | |
| 1390 | bool hasTemplateKWAndArgsInfo() const { |
| 1391 | return DeclRefExprBits.HasTemplateKWAndArgsInfo; |
| 1392 | } |
| 1393 | |
| 1394 | /// Retrieve the location of the template keyword preceding |
| 1395 | /// this name, if any. |
| 1396 | SourceLocation getTemplateKeywordLoc() const { |
| 1397 | if (!hasTemplateKWAndArgsInfo()) |
| 1398 | return SourceLocation(); |
| 1399 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; |
| 1400 | } |
| 1401 | |
| 1402 | /// Retrieve the location of the left angle bracket starting the |
| 1403 | /// explicit template argument list following the name, if any. |
| 1404 | SourceLocation getLAngleLoc() const { |
| 1405 | if (!hasTemplateKWAndArgsInfo()) |
| 1406 | return SourceLocation(); |
| 1407 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; |
| 1408 | } |
| 1409 | |
| 1410 | /// Retrieve the location of the right angle bracket ending the |
| 1411 | /// explicit template argument list following the name, if any. |
| 1412 | SourceLocation getRAngleLoc() const { |
| 1413 | if (!hasTemplateKWAndArgsInfo()) |
| 1414 | return SourceLocation(); |
| 1415 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; |
| 1416 | } |
| 1417 | |
| 1418 | /// Determines whether the name in this declaration reference |
| 1419 | /// was preceded by the template keyword. |
| 1420 | bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } |
| 1421 | |
| 1422 | /// Determines whether this declaration reference was followed by an |
| 1423 | /// explicit template argument list. |
| 1424 | bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } |
| 1425 | |
| 1426 | /// Copies the template arguments (if present) into the given |
| 1427 | /// structure. |
| 1428 | void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { |
| 1429 | if (hasExplicitTemplateArgs()) |
| 1430 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( |
| 1431 | getTrailingObjects<TemplateArgumentLoc>(), List); |
| 1432 | } |
| 1433 | |
| 1434 | /// Retrieve the template arguments provided as part of this |
| 1435 | /// template-id. |
| 1436 | const TemplateArgumentLoc *getTemplateArgs() const { |
| 1437 | if (!hasExplicitTemplateArgs()) |
| 1438 | return nullptr; |
| 1439 | return getTrailingObjects<TemplateArgumentLoc>(); |
| 1440 | } |
| 1441 | |
| 1442 | /// Retrieve the number of template arguments provided as part of this |
| 1443 | /// template-id. |
| 1444 | unsigned getNumTemplateArgs() const { |
| 1445 | if (!hasExplicitTemplateArgs()) |
| 1446 | return 0; |
| 1447 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; |
| 1448 | } |
| 1449 | |
| 1450 | ArrayRef<TemplateArgumentLoc> template_arguments() const { |
| 1451 | return {getTemplateArgs(), getNumTemplateArgs()}; |
| 1452 | } |
| 1453 | |
| 1454 | /// Returns true if this expression refers to a function that |
| 1455 | /// was resolved from an overloaded set having size greater than 1. |
| 1456 | bool hadMultipleCandidates() const { |
| 1457 | return DeclRefExprBits.HadMultipleCandidates; |
| 1458 | } |
| 1459 | /// Sets the flag telling whether this expression refers to |
| 1460 | /// a function that was resolved from an overloaded set having size |
| 1461 | /// greater than 1. |
| 1462 | void setHadMultipleCandidates(bool V = true) { |
| 1463 | DeclRefExprBits.HadMultipleCandidates = V; |
| 1464 | } |
| 1465 | |
| 1466 | /// Is this expression a non-odr-use reference, and if so, why? |
| 1467 | NonOdrUseReason isNonOdrUse() const { |
| 1468 | return static_cast<NonOdrUseReason>(DeclRefExprBits.NonOdrUseReason); |
| 1469 | } |
| 1470 | |
| 1471 | /// Does this DeclRefExpr refer to an enclosing local or a captured |
| 1472 | /// variable? |
| 1473 | bool refersToEnclosingVariableOrCapture() const { |
| 1474 | return DeclRefExprBits.RefersToEnclosingVariableOrCapture; |
| 1475 | } |
| 1476 | |
| 1477 | bool isImmediateEscalating() const { |
| 1478 | return DeclRefExprBits.IsImmediateEscalating; |
| 1479 | } |
| 1480 | |
| 1481 | void setIsImmediateEscalating(bool Set) { |
| 1482 | DeclRefExprBits.IsImmediateEscalating = Set; |
| 1483 | } |
| 1484 | |
| 1485 | bool isCapturedByCopyInLambdaWithExplicitObjectParameter() const { |
| 1486 | return DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter; |
| 1487 | } |
| 1488 | |
| 1489 | void setCapturedByCopyInLambdaWithExplicitObjectParameter( |
| 1490 | bool Set, const ASTContext &Context) { |
| 1491 | DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = Set; |
| 1492 | setDependence(computeDependence(E: this, Ctx: Context)); |
| 1493 | } |
| 1494 | |
| 1495 | static bool classof(const Stmt *T) { |
| 1496 | return T->getStmtClass() == DeclRefExprClass; |
| 1497 | } |
| 1498 | |
| 1499 | // Iterators |
| 1500 | child_range children() { |
| 1501 | return child_range(child_iterator(), child_iterator()); |
| 1502 | } |
| 1503 | |
| 1504 | const_child_range children() const { |
| 1505 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1506 | } |
| 1507 | }; |
| 1508 | |
| 1509 | class IntegerLiteral : public Expr, public APIntStorage { |
| 1510 | SourceLocation Loc; |
| 1511 | |
| 1512 | /// Construct an empty integer literal. |
| 1513 | explicit IntegerLiteral(EmptyShell Empty) |
| 1514 | : Expr(IntegerLiteralClass, Empty) { } |
| 1515 | |
| 1516 | public: |
| 1517 | // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, |
| 1518 | // or UnsignedLongLongTy |
| 1519 | IntegerLiteral(const ASTContext &C, const llvm::APInt &V, QualType type, |
| 1520 | SourceLocation l); |
| 1521 | |
| 1522 | /// Returns a new integer literal with value 'V' and type 'type'. |
| 1523 | /// \param type - either IntTy, LongTy, LongLongTy, UnsignedIntTy, |
| 1524 | /// UnsignedLongTy, or UnsignedLongLongTy which should match the size of V |
| 1525 | /// \param V - the value that the returned integer literal contains. |
| 1526 | static IntegerLiteral *Create(const ASTContext &C, const llvm::APInt &V, |
| 1527 | QualType type, SourceLocation l); |
| 1528 | /// Returns a new empty integer literal. |
| 1529 | static IntegerLiteral *Create(const ASTContext &C, EmptyShell Empty); |
| 1530 | |
| 1531 | SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } |
| 1532 | SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } |
| 1533 | |
| 1534 | /// Retrieve the location of the literal. |
| 1535 | SourceLocation getLocation() const { return Loc; } |
| 1536 | |
| 1537 | void setLocation(SourceLocation Location) { Loc = Location; } |
| 1538 | |
| 1539 | static bool classof(const Stmt *T) { |
| 1540 | return T->getStmtClass() == IntegerLiteralClass; |
| 1541 | } |
| 1542 | |
| 1543 | // Iterators |
| 1544 | child_range children() { |
| 1545 | return child_range(child_iterator(), child_iterator()); |
| 1546 | } |
| 1547 | const_child_range children() const { |
| 1548 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1549 | } |
| 1550 | }; |
| 1551 | |
| 1552 | class FixedPointLiteral : public Expr, public APIntStorage { |
| 1553 | SourceLocation Loc; |
| 1554 | unsigned Scale; |
| 1555 | |
| 1556 | /// \brief Construct an empty fixed-point literal. |
| 1557 | explicit FixedPointLiteral(EmptyShell Empty) |
| 1558 | : Expr(FixedPointLiteralClass, Empty) {} |
| 1559 | |
| 1560 | public: |
| 1561 | FixedPointLiteral(const ASTContext &C, const llvm::APInt &V, QualType type, |
| 1562 | SourceLocation l, unsigned Scale); |
| 1563 | |
| 1564 | // Store the int as is without any bit shifting. |
| 1565 | static FixedPointLiteral *CreateFromRawInt(const ASTContext &C, |
| 1566 | const llvm::APInt &V, |
| 1567 | QualType type, SourceLocation l, |
| 1568 | unsigned Scale); |
| 1569 | |
| 1570 | /// Returns an empty fixed-point literal. |
| 1571 | static FixedPointLiteral *Create(const ASTContext &C, EmptyShell Empty); |
| 1572 | |
| 1573 | /// Returns an internal integer representation of the literal. |
| 1574 | llvm::APInt getValue() const { return APIntStorage::getValue(); } |
| 1575 | |
| 1576 | SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } |
| 1577 | SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } |
| 1578 | |
| 1579 | /// \brief Retrieve the location of the literal. |
| 1580 | SourceLocation getLocation() const { return Loc; } |
| 1581 | |
| 1582 | void setLocation(SourceLocation Location) { Loc = Location; } |
| 1583 | |
| 1584 | unsigned getScale() const { return Scale; } |
| 1585 | void setScale(unsigned S) { Scale = S; } |
| 1586 | |
| 1587 | static bool classof(const Stmt *T) { |
| 1588 | return T->getStmtClass() == FixedPointLiteralClass; |
| 1589 | } |
| 1590 | |
| 1591 | std::string getValueAsString(unsigned Radix) const; |
| 1592 | |
| 1593 | // Iterators |
| 1594 | child_range children() { |
| 1595 | return child_range(child_iterator(), child_iterator()); |
| 1596 | } |
| 1597 | const_child_range children() const { |
| 1598 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1599 | } |
| 1600 | }; |
| 1601 | |
| 1602 | enum class CharacterLiteralKind { Ascii, Wide, UTF8, UTF16, UTF32 }; |
| 1603 | |
| 1604 | class CharacterLiteral : public Expr { |
| 1605 | unsigned Value; |
| 1606 | SourceLocation Loc; |
| 1607 | public: |
| 1608 | // type should be IntTy |
| 1609 | CharacterLiteral(unsigned value, CharacterLiteralKind kind, QualType type, |
| 1610 | SourceLocation l) |
| 1611 | : Expr(CharacterLiteralClass, type, VK_PRValue, OK_Ordinary), |
| 1612 | Value(value), Loc(l) { |
| 1613 | CharacterLiteralBits.Kind = llvm::to_underlying(E: kind); |
| 1614 | setDependence(ExprDependence::None); |
| 1615 | } |
| 1616 | |
| 1617 | /// Construct an empty character literal. |
| 1618 | CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } |
| 1619 | |
| 1620 | SourceLocation getLocation() const { return Loc; } |
| 1621 | CharacterLiteralKind getKind() const { |
| 1622 | return static_cast<CharacterLiteralKind>(CharacterLiteralBits.Kind); |
| 1623 | } |
| 1624 | |
| 1625 | SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } |
| 1626 | SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } |
| 1627 | |
| 1628 | unsigned getValue() const { return Value; } |
| 1629 | |
| 1630 | void setLocation(SourceLocation Location) { Loc = Location; } |
| 1631 | void setKind(CharacterLiteralKind kind) { |
| 1632 | CharacterLiteralBits.Kind = llvm::to_underlying(E: kind); |
| 1633 | } |
| 1634 | void setValue(unsigned Val) { Value = Val; } |
| 1635 | |
| 1636 | static bool classof(const Stmt *T) { |
| 1637 | return T->getStmtClass() == CharacterLiteralClass; |
| 1638 | } |
| 1639 | |
| 1640 | static void print(unsigned val, CharacterLiteralKind Kind, raw_ostream &OS); |
| 1641 | |
| 1642 | // Iterators |
| 1643 | child_range children() { |
| 1644 | return child_range(child_iterator(), child_iterator()); |
| 1645 | } |
| 1646 | const_child_range children() const { |
| 1647 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1648 | } |
| 1649 | }; |
| 1650 | |
| 1651 | class FloatingLiteral : public Expr, private APFloatStorage { |
| 1652 | SourceLocation Loc; |
| 1653 | |
| 1654 | FloatingLiteral(const ASTContext &C, const llvm::APFloat &V, bool isexact, |
| 1655 | QualType Type, SourceLocation L); |
| 1656 | |
| 1657 | /// Construct an empty floating-point literal. |
| 1658 | explicit FloatingLiteral(const ASTContext &C, EmptyShell Empty); |
| 1659 | |
| 1660 | public: |
| 1661 | static FloatingLiteral *Create(const ASTContext &C, const llvm::APFloat &V, |
| 1662 | bool isexact, QualType Type, SourceLocation L); |
| 1663 | static FloatingLiteral *Create(const ASTContext &C, EmptyShell Empty); |
| 1664 | |
| 1665 | llvm::APFloat getValue() const { |
| 1666 | return APFloatStorage::getValue(getSemantics()); |
| 1667 | } |
| 1668 | void setValue(const ASTContext &C, const llvm::APFloat &Val) { |
| 1669 | assert(&getSemantics() == &Val.getSemantics() && "Inconsistent semantics" ); |
| 1670 | APFloatStorage::setValue(C, Val); |
| 1671 | } |
| 1672 | |
| 1673 | /// Get a raw enumeration value representing the floating-point semantics of |
| 1674 | /// this literal (32-bit IEEE, x87, ...), suitable for serialization. |
| 1675 | llvm::APFloatBase::Semantics getRawSemantics() const { |
| 1676 | return static_cast<llvm::APFloatBase::Semantics>( |
| 1677 | FloatingLiteralBits.Semantics); |
| 1678 | } |
| 1679 | |
| 1680 | /// Set the raw enumeration value representing the floating-point semantics of |
| 1681 | /// this literal (32-bit IEEE, x87, ...), suitable for serialization. |
| 1682 | void setRawSemantics(llvm::APFloatBase::Semantics Sem) { |
| 1683 | FloatingLiteralBits.Semantics = Sem; |
| 1684 | } |
| 1685 | |
| 1686 | /// Return the APFloat semantics this literal uses. |
| 1687 | const llvm::fltSemantics &getSemantics() const { |
| 1688 | return llvm::APFloatBase::EnumToSemantics( |
| 1689 | S: static_cast<llvm::APFloatBase::Semantics>( |
| 1690 | FloatingLiteralBits.Semantics)); |
| 1691 | } |
| 1692 | |
| 1693 | /// Set the APFloat semantics this literal uses. |
| 1694 | void setSemantics(const llvm::fltSemantics &Sem) { |
| 1695 | FloatingLiteralBits.Semantics = llvm::APFloatBase::SemanticsToEnum(Sem); |
| 1696 | } |
| 1697 | |
| 1698 | bool isExact() const { return FloatingLiteralBits.IsExact; } |
| 1699 | void setExact(bool E) { FloatingLiteralBits.IsExact = E; } |
| 1700 | |
| 1701 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 1702 | /// double. Note that this may cause loss of precision, but is useful for |
| 1703 | /// debugging dumps, etc. |
| 1704 | double getValueAsApproximateDouble() const; |
| 1705 | |
| 1706 | SourceLocation getLocation() const { return Loc; } |
| 1707 | void setLocation(SourceLocation L) { Loc = L; } |
| 1708 | |
| 1709 | SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } |
| 1710 | SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } |
| 1711 | |
| 1712 | static bool classof(const Stmt *T) { |
| 1713 | return T->getStmtClass() == FloatingLiteralClass; |
| 1714 | } |
| 1715 | |
| 1716 | // Iterators |
| 1717 | child_range children() { |
| 1718 | return child_range(child_iterator(), child_iterator()); |
| 1719 | } |
| 1720 | const_child_range children() const { |
| 1721 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1722 | } |
| 1723 | }; |
| 1724 | |
| 1725 | /// ImaginaryLiteral - We support imaginary integer and floating point literals, |
| 1726 | /// like "1.0i". We represent these as a wrapper around FloatingLiteral and |
| 1727 | /// IntegerLiteral classes. Instances of this class always have a Complex type |
| 1728 | /// whose element type matches the subexpression. |
| 1729 | /// |
| 1730 | class ImaginaryLiteral : public Expr { |
| 1731 | Stmt *Val; |
| 1732 | public: |
| 1733 | ImaginaryLiteral(Expr *val, QualType Ty) |
| 1734 | : Expr(ImaginaryLiteralClass, Ty, VK_PRValue, OK_Ordinary), Val(val) { |
| 1735 | setDependence(ExprDependence::None); |
| 1736 | } |
| 1737 | |
| 1738 | /// Build an empty imaginary literal. |
| 1739 | explicit ImaginaryLiteral(EmptyShell Empty) |
| 1740 | : Expr(ImaginaryLiteralClass, Empty) { } |
| 1741 | |
| 1742 | const Expr *getSubExpr() const { return cast<Expr>(Val); } |
| 1743 | Expr *getSubExpr() { return cast<Expr>(Val); } |
| 1744 | void setSubExpr(Expr *E) { Val = E; } |
| 1745 | |
| 1746 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 1747 | return Val->getBeginLoc(); |
| 1748 | } |
| 1749 | SourceLocation getEndLoc() const LLVM_READONLY { return Val->getEndLoc(); } |
| 1750 | |
| 1751 | static bool classof(const Stmt *T) { |
| 1752 | return T->getStmtClass() == ImaginaryLiteralClass; |
| 1753 | } |
| 1754 | |
| 1755 | // Iterators |
| 1756 | child_range children() { return child_range(&Val, &Val+1); } |
| 1757 | const_child_range children() const { |
| 1758 | return const_child_range(&Val, &Val + 1); |
| 1759 | } |
| 1760 | }; |
| 1761 | |
| 1762 | enum class StringLiteralKind { |
| 1763 | Ordinary, |
| 1764 | Wide, |
| 1765 | UTF8, |
| 1766 | UTF16, |
| 1767 | UTF32, |
| 1768 | Unevaluated, |
| 1769 | // Binary kind of string literal is used for the data coming via #embed |
| 1770 | // directive. File's binary contents is transformed to a special kind of |
| 1771 | // string literal that in some cases may be used directly as an initializer |
| 1772 | // and some features of classic string literals are not applicable to this |
| 1773 | // kind of a string literal, for example finding a particular byte's source |
| 1774 | // location for better diagnosing. |
| 1775 | Binary |
| 1776 | }; |
| 1777 | |
| 1778 | /// StringLiteral - This represents a string literal expression, e.g. "foo" |
| 1779 | /// or L"bar" (wide strings). The actual string data can be obtained with |
| 1780 | /// getBytes() and is NOT null-terminated. The length of the string data is |
| 1781 | /// determined by calling getByteLength(). |
| 1782 | /// |
| 1783 | /// The C type for a string is always a ConstantArrayType. In C++, the char |
| 1784 | /// type is const qualified, in C it is not. |
| 1785 | /// |
| 1786 | /// Note that strings in C can be formed by concatenation of multiple string |
| 1787 | /// literal pptokens in translation phase #6. This keeps track of the locations |
| 1788 | /// of each of these pieces. |
| 1789 | /// |
| 1790 | /// Strings in C can also be truncated and extended by assigning into arrays, |
| 1791 | /// e.g. with constructs like: |
| 1792 | /// char X[2] = "foobar"; |
| 1793 | /// In this case, getByteLength() will return 6, but the string literal will |
| 1794 | /// have type "char[2]". |
| 1795 | class StringLiteral final |
| 1796 | : public Expr, |
| 1797 | private llvm::TrailingObjects<StringLiteral, unsigned, SourceLocation, |
| 1798 | char> { |
| 1799 | friend class ASTStmtReader; |
| 1800 | friend TrailingObjects; |
| 1801 | |
| 1802 | /// StringLiteral is followed by several trailing objects. They are in order: |
| 1803 | /// |
| 1804 | /// * A single unsigned storing the length in characters of this string. The |
| 1805 | /// length in bytes is this length times the width of a single character. |
| 1806 | /// Always present and stored as a trailing objects because storing it in |
| 1807 | /// StringLiteral would increase the size of StringLiteral by sizeof(void *) |
| 1808 | /// due to alignment requirements. If you add some data to StringLiteral, |
| 1809 | /// consider moving it inside StringLiteral. |
| 1810 | /// |
| 1811 | /// * An array of getNumConcatenated() SourceLocation, one for each of the |
| 1812 | /// token this string is made of. |
| 1813 | /// |
| 1814 | /// * An array of getByteLength() char used to store the string data. |
| 1815 | |
| 1816 | unsigned numTrailingObjects(OverloadToken<unsigned>) const { return 1; } |
| 1817 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
| 1818 | return getNumConcatenated(); |
| 1819 | } |
| 1820 | |
| 1821 | unsigned numTrailingObjects(OverloadToken<char>) const { |
| 1822 | return getByteLength(); |
| 1823 | } |
| 1824 | |
| 1825 | char *getStrDataAsChar() { return getTrailingObjects<char>(); } |
| 1826 | const char *getStrDataAsChar() const { return getTrailingObjects<char>(); } |
| 1827 | |
| 1828 | const uint16_t *getStrDataAsUInt16() const { |
| 1829 | return reinterpret_cast<const uint16_t *>(getTrailingObjects<char>()); |
| 1830 | } |
| 1831 | |
| 1832 | const uint32_t *getStrDataAsUInt32() const { |
| 1833 | return reinterpret_cast<const uint32_t *>(getTrailingObjects<char>()); |
| 1834 | } |
| 1835 | |
| 1836 | /// Build a string literal. |
| 1837 | StringLiteral(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind, |
| 1838 | bool Pascal, QualType Ty, const SourceLocation *Loc, |
| 1839 | unsigned NumConcatenated); |
| 1840 | |
| 1841 | /// Build an empty string literal. |
| 1842 | StringLiteral(EmptyShell Empty, unsigned NumConcatenated, unsigned Length, |
| 1843 | unsigned CharByteWidth); |
| 1844 | |
| 1845 | /// Map a target and string kind to the appropriate character width. |
| 1846 | static unsigned mapCharByteWidth(TargetInfo const &Target, |
| 1847 | StringLiteralKind SK); |
| 1848 | |
| 1849 | /// Set one of the string literal token. |
| 1850 | void setStrTokenLoc(unsigned TokNum, SourceLocation L) { |
| 1851 | assert(TokNum < getNumConcatenated() && "Invalid tok number" ); |
| 1852 | getTrailingObjects<SourceLocation>()[TokNum] = L; |
| 1853 | } |
| 1854 | |
| 1855 | public: |
| 1856 | /// This is the "fully general" constructor that allows representation of |
| 1857 | /// strings formed from multiple concatenated tokens. |
| 1858 | static StringLiteral *Create(const ASTContext &Ctx, StringRef Str, |
| 1859 | StringLiteralKind Kind, bool Pascal, QualType Ty, |
| 1860 | const SourceLocation *Loc, |
| 1861 | unsigned NumConcatenated); |
| 1862 | |
| 1863 | /// Simple constructor for string literals made from one token. |
| 1864 | static StringLiteral *Create(const ASTContext &Ctx, StringRef Str, |
| 1865 | StringLiteralKind Kind, bool Pascal, QualType Ty, |
| 1866 | SourceLocation Loc) { |
| 1867 | return Create(Ctx, Str, Kind, Pascal, Ty, Loc: &Loc, NumConcatenated: 1); |
| 1868 | } |
| 1869 | |
| 1870 | /// Construct an empty string literal. |
| 1871 | static StringLiteral *CreateEmpty(const ASTContext &Ctx, |
| 1872 | unsigned NumConcatenated, unsigned Length, |
| 1873 | unsigned CharByteWidth); |
| 1874 | |
| 1875 | StringRef getString() const { |
| 1876 | assert((isUnevaluated() || getCharByteWidth() == 1) && |
| 1877 | "This function is used in places that assume strings use char" ); |
| 1878 | return StringRef(getStrDataAsChar(), getByteLength()); |
| 1879 | } |
| 1880 | |
| 1881 | /// Allow access to clients that need the byte representation, such as |
| 1882 | /// ASTWriterStmt::VisitStringLiteral(). |
| 1883 | StringRef getBytes() const { |
| 1884 | // FIXME: StringRef may not be the right type to use as a result for this. |
| 1885 | return StringRef(getStrDataAsChar(), getByteLength()); |
| 1886 | } |
| 1887 | |
| 1888 | void outputString(raw_ostream &OS) const; |
| 1889 | |
| 1890 | uint32_t getCodeUnit(size_t i) const { |
| 1891 | assert(i < getLength() && "out of bounds access" ); |
| 1892 | switch (getCharByteWidth()) { |
| 1893 | case 1: |
| 1894 | return static_cast<unsigned char>(getStrDataAsChar()[i]); |
| 1895 | case 2: |
| 1896 | return getStrDataAsUInt16()[i]; |
| 1897 | case 4: |
| 1898 | return getStrDataAsUInt32()[i]; |
| 1899 | } |
| 1900 | llvm_unreachable("Unsupported character width!" ); |
| 1901 | } |
| 1902 | |
| 1903 | // Get code unit but preserve sign info. |
| 1904 | int64_t getCodeUnitS(size_t I, uint64_t BitWidth) const { |
| 1905 | int64_t V = getCodeUnit(i: I); |
| 1906 | if (isOrdinary() || isWide()) { |
| 1907 | // Ordinary and wide string literals have types that can be signed. |
| 1908 | // It is important for checking C23 constexpr initializers. |
| 1909 | unsigned Width = getCharByteWidth() * BitWidth; |
| 1910 | llvm::APInt AInt(Width, (uint64_t)V); |
| 1911 | V = AInt.getSExtValue(); |
| 1912 | } |
| 1913 | return V; |
| 1914 | } |
| 1915 | |
| 1916 | unsigned getByteLength() const { return getCharByteWidth() * getLength(); } |
| 1917 | unsigned getLength() const { return *getTrailingObjects<unsigned>(); } |
| 1918 | unsigned getCharByteWidth() const { return StringLiteralBits.CharByteWidth; } |
| 1919 | |
| 1920 | StringLiteralKind getKind() const { |
| 1921 | return static_cast<StringLiteralKind>(StringLiteralBits.Kind); |
| 1922 | } |
| 1923 | |
| 1924 | bool isOrdinary() const { return getKind() == StringLiteralKind::Ordinary; } |
| 1925 | bool isWide() const { return getKind() == StringLiteralKind::Wide; } |
| 1926 | bool isUTF8() const { return getKind() == StringLiteralKind::UTF8; } |
| 1927 | bool isUTF16() const { return getKind() == StringLiteralKind::UTF16; } |
| 1928 | bool isUTF32() const { return getKind() == StringLiteralKind::UTF32; } |
| 1929 | bool isUnevaluated() const { return getKind() == StringLiteralKind::Unevaluated; } |
| 1930 | bool isPascal() const { return StringLiteralBits.IsPascal; } |
| 1931 | |
| 1932 | bool containsNonAscii() const { |
| 1933 | for (auto c : getString()) |
| 1934 | if (!isASCII(c)) |
| 1935 | return true; |
| 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | bool containsNonAsciiOrNull() const { |
| 1940 | for (auto c : getString()) |
| 1941 | if (!isASCII(c) || !c) |
| 1942 | return true; |
| 1943 | return false; |
| 1944 | } |
| 1945 | |
| 1946 | /// getNumConcatenated - Get the number of string literal tokens that were |
| 1947 | /// concatenated in translation phase #6 to form this string literal. |
| 1948 | unsigned getNumConcatenated() const { |
| 1949 | return StringLiteralBits.NumConcatenated; |
| 1950 | } |
| 1951 | |
| 1952 | /// Get one of the string literal token. |
| 1953 | SourceLocation getStrTokenLoc(unsigned TokNum) const { |
| 1954 | assert(TokNum < getNumConcatenated() && "Invalid tok number" ); |
| 1955 | return getTrailingObjects<SourceLocation>()[TokNum]; |
| 1956 | } |
| 1957 | |
| 1958 | /// getLocationOfByte - Return a source location that points to the specified |
| 1959 | /// byte of this string literal. |
| 1960 | /// |
| 1961 | /// Strings are amazingly complex. They can be formed from multiple tokens |
| 1962 | /// and can have escape sequences in them in addition to the usual trigraph |
| 1963 | /// and escaped newline business. This routine handles this complexity. |
| 1964 | /// |
| 1965 | SourceLocation |
| 1966 | getLocationOfByte(unsigned ByteNo, const SourceManager &SM, |
| 1967 | const LangOptions &Features, const TargetInfo &Target, |
| 1968 | unsigned *StartToken = nullptr, |
| 1969 | unsigned *StartTokenByteOffset = nullptr) const; |
| 1970 | |
| 1971 | typedef const SourceLocation *tokloc_iterator; |
| 1972 | |
| 1973 | tokloc_iterator tokloc_begin() const { |
| 1974 | return getTrailingObjects<SourceLocation>(); |
| 1975 | } |
| 1976 | |
| 1977 | tokloc_iterator tokloc_end() const { |
| 1978 | return getTrailingObjects<SourceLocation>() + getNumConcatenated(); |
| 1979 | } |
| 1980 | |
| 1981 | SourceLocation getBeginLoc() const LLVM_READONLY { return *tokloc_begin(); } |
| 1982 | SourceLocation getEndLoc() const LLVM_READONLY { return *(tokloc_end() - 1); } |
| 1983 | |
| 1984 | static bool classof(const Stmt *T) { |
| 1985 | return T->getStmtClass() == StringLiteralClass; |
| 1986 | } |
| 1987 | |
| 1988 | // Iterators |
| 1989 | child_range children() { |
| 1990 | return child_range(child_iterator(), child_iterator()); |
| 1991 | } |
| 1992 | const_child_range children() const { |
| 1993 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 1994 | } |
| 1995 | }; |
| 1996 | |
| 1997 | enum class PredefinedIdentKind { |
| 1998 | Func, |
| 1999 | Function, |
| 2000 | LFunction, // Same as Function, but as wide string. |
| 2001 | FuncDName, |
| 2002 | FuncSig, |
| 2003 | LFuncSig, // Same as FuncSig, but as wide string |
| 2004 | PrettyFunction, |
| 2005 | /// The same as PrettyFunction, except that the |
| 2006 | /// 'virtual' keyword is omitted for virtual member functions. |
| 2007 | PrettyFunctionNoVirtual |
| 2008 | }; |
| 2009 | |
| 2010 | /// [C99 6.4.2.2] - A predefined identifier such as __func__. |
| 2011 | class PredefinedExpr final |
| 2012 | : public Expr, |
| 2013 | private llvm::TrailingObjects<PredefinedExpr, Stmt *> { |
| 2014 | friend class ASTStmtReader; |
| 2015 | friend TrailingObjects; |
| 2016 | |
| 2017 | // PredefinedExpr is optionally followed by a single trailing |
| 2018 | // "Stmt *" for the predefined identifier. It is present if and only if |
| 2019 | // hasFunctionName() is true and is always a "StringLiteral *". |
| 2020 | |
| 2021 | PredefinedExpr(SourceLocation L, QualType FNTy, PredefinedIdentKind IK, |
| 2022 | bool IsTransparent, StringLiteral *SL); |
| 2023 | |
| 2024 | explicit PredefinedExpr(EmptyShell Empty, bool HasFunctionName); |
| 2025 | |
| 2026 | /// True if this PredefinedExpr has storage for a function name. |
| 2027 | bool hasFunctionName() const { return PredefinedExprBits.HasFunctionName; } |
| 2028 | |
| 2029 | void setFunctionName(StringLiteral *SL) { |
| 2030 | assert(hasFunctionName() && |
| 2031 | "This PredefinedExpr has no storage for a function name!" ); |
| 2032 | *getTrailingObjects() = SL; |
| 2033 | } |
| 2034 | |
| 2035 | public: |
| 2036 | /// Create a PredefinedExpr. |
| 2037 | /// |
| 2038 | /// If IsTransparent, the PredefinedExpr is transparently handled as a |
| 2039 | /// StringLiteral. |
| 2040 | static PredefinedExpr *Create(const ASTContext &Ctx, SourceLocation L, |
| 2041 | QualType FNTy, PredefinedIdentKind IK, |
| 2042 | bool IsTransparent, StringLiteral *SL); |
| 2043 | |
| 2044 | /// Create an empty PredefinedExpr. |
| 2045 | static PredefinedExpr *CreateEmpty(const ASTContext &Ctx, |
| 2046 | bool HasFunctionName); |
| 2047 | |
| 2048 | PredefinedIdentKind getIdentKind() const { |
| 2049 | return static_cast<PredefinedIdentKind>(PredefinedExprBits.Kind); |
| 2050 | } |
| 2051 | |
| 2052 | bool isTransparent() const { return PredefinedExprBits.IsTransparent; } |
| 2053 | |
| 2054 | SourceLocation getLocation() const { return PredefinedExprBits.Loc; } |
| 2055 | void setLocation(SourceLocation L) { PredefinedExprBits.Loc = L; } |
| 2056 | |
| 2057 | StringLiteral *getFunctionName() { |
| 2058 | return hasFunctionName() |
| 2059 | ? static_cast<StringLiteral *>(*getTrailingObjects()) |
| 2060 | : nullptr; |
| 2061 | } |
| 2062 | |
| 2063 | const StringLiteral *getFunctionName() const { |
| 2064 | return hasFunctionName() |
| 2065 | ? static_cast<StringLiteral *>(*getTrailingObjects()) |
| 2066 | : nullptr; |
| 2067 | } |
| 2068 | |
| 2069 | static StringRef getIdentKindName(PredefinedIdentKind IK); |
| 2070 | StringRef getIdentKindName() const { |
| 2071 | return getIdentKindName(IK: getIdentKind()); |
| 2072 | } |
| 2073 | |
| 2074 | static std::string ComputeName(PredefinedIdentKind IK, |
| 2075 | const Decl *CurrentDecl, |
| 2076 | bool ForceElaboratedPrinting = false); |
| 2077 | |
| 2078 | SourceLocation getBeginLoc() const { return getLocation(); } |
| 2079 | SourceLocation getEndLoc() const { return getLocation(); } |
| 2080 | |
| 2081 | static bool classof(const Stmt *T) { |
| 2082 | return T->getStmtClass() == PredefinedExprClass; |
| 2083 | } |
| 2084 | |
| 2085 | // Iterators |
| 2086 | child_range children() { |
| 2087 | return child_range(getTrailingObjects(hasFunctionName())); |
| 2088 | } |
| 2089 | |
| 2090 | const_child_range children() const { |
| 2091 | return const_child_range(getTrailingObjects(hasFunctionName())); |
| 2092 | } |
| 2093 | }; |
| 2094 | |
| 2095 | /// This expression type represents an asterisk in an OpenACC Size-Expr, used in |
| 2096 | /// the 'tile' and 'gang' clauses. It is of 'int' type, but should not be |
| 2097 | /// evaluated. |
| 2098 | class OpenACCAsteriskSizeExpr final : public Expr { |
| 2099 | friend class ASTStmtReader; |
| 2100 | SourceLocation AsteriskLoc; |
| 2101 | |
| 2102 | OpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc, QualType IntTy) |
| 2103 | : Expr(OpenACCAsteriskSizeExprClass, IntTy, VK_PRValue, OK_Ordinary), |
| 2104 | AsteriskLoc(AsteriskLoc) {} |
| 2105 | |
| 2106 | void setAsteriskLocation(SourceLocation Loc) { AsteriskLoc = Loc; } |
| 2107 | |
| 2108 | public: |
| 2109 | static OpenACCAsteriskSizeExpr *Create(const ASTContext &C, |
| 2110 | SourceLocation Loc); |
| 2111 | static OpenACCAsteriskSizeExpr *CreateEmpty(const ASTContext &C); |
| 2112 | |
| 2113 | SourceLocation getBeginLoc() const { return AsteriskLoc; } |
| 2114 | SourceLocation getEndLoc() const { return AsteriskLoc; } |
| 2115 | SourceLocation getLocation() const { return AsteriskLoc; } |
| 2116 | |
| 2117 | static bool classof(const Stmt *T) { |
| 2118 | return T->getStmtClass() == OpenACCAsteriskSizeExprClass; |
| 2119 | } |
| 2120 | // Iterators |
| 2121 | child_range children() { |
| 2122 | return child_range(child_iterator(), child_iterator()); |
| 2123 | } |
| 2124 | |
| 2125 | const_child_range children() const { |
| 2126 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 2127 | } |
| 2128 | }; |
| 2129 | |
| 2130 | // This represents a use of the __builtin_sycl_unique_stable_name, which takes a |
| 2131 | // type-id, and at CodeGen time emits a unique string representation of the |
| 2132 | // type in a way that permits us to properly encode information about the SYCL |
| 2133 | // kernels. |
| 2134 | class SYCLUniqueStableNameExpr final : public Expr { |
| 2135 | friend class ASTStmtReader; |
| 2136 | SourceLocation OpLoc, LParen, RParen; |
| 2137 | TypeSourceInfo *TypeInfo; |
| 2138 | |
| 2139 | SYCLUniqueStableNameExpr(EmptyShell Empty, QualType ResultTy); |
| 2140 | SYCLUniqueStableNameExpr(SourceLocation OpLoc, SourceLocation LParen, |
| 2141 | SourceLocation RParen, QualType ResultTy, |
| 2142 | TypeSourceInfo *TSI); |
| 2143 | |
| 2144 | void setTypeSourceInfo(TypeSourceInfo *Ty) { TypeInfo = Ty; } |
| 2145 | |
| 2146 | void setLocation(SourceLocation L) { OpLoc = L; } |
| 2147 | void setLParenLocation(SourceLocation L) { LParen = L; } |
| 2148 | void setRParenLocation(SourceLocation L) { RParen = L; } |
| 2149 | |
| 2150 | public: |
| 2151 | TypeSourceInfo *getTypeSourceInfo() { return TypeInfo; } |
| 2152 | |
| 2153 | const TypeSourceInfo *getTypeSourceInfo() const { return TypeInfo; } |
| 2154 | |
| 2155 | static SYCLUniqueStableNameExpr * |
| 2156 | Create(const ASTContext &Ctx, SourceLocation OpLoc, SourceLocation LParen, |
| 2157 | SourceLocation RParen, TypeSourceInfo *TSI); |
| 2158 | |
| 2159 | static SYCLUniqueStableNameExpr *CreateEmpty(const ASTContext &Ctx); |
| 2160 | |
| 2161 | SourceLocation getBeginLoc() const { return getLocation(); } |
| 2162 | SourceLocation getEndLoc() const { return RParen; } |
| 2163 | SourceLocation getLocation() const { return OpLoc; } |
| 2164 | SourceLocation getLParenLocation() const { return LParen; } |
| 2165 | SourceLocation getRParenLocation() const { return RParen; } |
| 2166 | |
| 2167 | static bool classof(const Stmt *T) { |
| 2168 | return T->getStmtClass() == SYCLUniqueStableNameExprClass; |
| 2169 | } |
| 2170 | |
| 2171 | // Iterators |
| 2172 | child_range children() { |
| 2173 | return child_range(child_iterator(), child_iterator()); |
| 2174 | } |
| 2175 | |
| 2176 | const_child_range children() const { |
| 2177 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 2178 | } |
| 2179 | |
| 2180 | // Convenience function to generate the name of the currently stored type. |
| 2181 | std::string ComputeName(ASTContext &Context) const; |
| 2182 | |
| 2183 | // Get the generated name of the type. Note that this only works after all |
| 2184 | // kernels have been instantiated. |
| 2185 | static std::string ComputeName(ASTContext &Context, QualType Ty); |
| 2186 | }; |
| 2187 | |
| 2188 | /// ParenExpr - This represents a parenthesized expression, e.g. "(1)". This |
| 2189 | /// AST node is only formed if full location information is requested. |
| 2190 | class ParenExpr : public Expr { |
| 2191 | SourceLocation L, R; |
| 2192 | Stmt *Val; |
| 2193 | |
| 2194 | public: |
| 2195 | ParenExpr(SourceLocation l, SourceLocation r, Expr *val) |
| 2196 | : Expr(ParenExprClass, val->getType(), val->getValueKind(), |
| 2197 | val->getObjectKind()), |
| 2198 | L(l), R(r), Val(val) { |
| 2199 | ParenExprBits.ProducedByFoldExpansion = false; |
| 2200 | setDependence(computeDependence(E: this)); |
| 2201 | } |
| 2202 | |
| 2203 | /// Construct an empty parenthesized expression. |
| 2204 | explicit ParenExpr(EmptyShell Empty) |
| 2205 | : Expr(ParenExprClass, Empty) { } |
| 2206 | |
| 2207 | const Expr *getSubExpr() const { return cast<Expr>(Val); } |
| 2208 | Expr *getSubExpr() { return cast<Expr>(Val); } |
| 2209 | void setSubExpr(Expr *E) { Val = E; } |
| 2210 | |
| 2211 | SourceLocation getBeginLoc() const LLVM_READONLY { return L; } |
| 2212 | SourceLocation getEndLoc() const LLVM_READONLY { return R; } |
| 2213 | |
| 2214 | /// Get the location of the left parentheses '('. |
| 2215 | SourceLocation getLParen() const { return L; } |
| 2216 | void setLParen(SourceLocation Loc) { L = Loc; } |
| 2217 | |
| 2218 | /// Get the location of the right parentheses ')'. |
| 2219 | SourceLocation getRParen() const { return R; } |
| 2220 | void setRParen(SourceLocation Loc) { R = Loc; } |
| 2221 | |
| 2222 | static bool classof(const Stmt *T) { |
| 2223 | return T->getStmtClass() == ParenExprClass; |
| 2224 | } |
| 2225 | |
| 2226 | // Iterators |
| 2227 | child_range children() { return child_range(&Val, &Val+1); } |
| 2228 | const_child_range children() const { |
| 2229 | return const_child_range(&Val, &Val + 1); |
| 2230 | } |
| 2231 | |
| 2232 | bool isProducedByFoldExpansion() const { |
| 2233 | return ParenExprBits.ProducedByFoldExpansion != 0; |
| 2234 | } |
| 2235 | void setIsProducedByFoldExpansion(bool ProducedByFoldExpansion = true) { |
| 2236 | ParenExprBits.ProducedByFoldExpansion = ProducedByFoldExpansion; |
| 2237 | } |
| 2238 | }; |
| 2239 | |
| 2240 | /// UnaryOperator - This represents the unary-expression's (except sizeof and |
| 2241 | /// alignof), the postinc/postdec operators from postfix-expression, and various |
| 2242 | /// extensions. |
| 2243 | /// |
| 2244 | /// Notes on various nodes: |
| 2245 | /// |
| 2246 | /// Real/Imag - These return the real/imag part of a complex operand. If |
| 2247 | /// applied to a non-complex value, the former returns its operand and the |
| 2248 | /// later returns zero in the type of the operand. |
| 2249 | /// |
| 2250 | class UnaryOperator final |
| 2251 | : public Expr, |
| 2252 | private llvm::TrailingObjects<UnaryOperator, FPOptionsOverride> { |
| 2253 | Stmt *Val; |
| 2254 | |
| 2255 | FPOptionsOverride &getTrailingFPFeatures() { |
| 2256 | assert(UnaryOperatorBits.HasFPFeatures); |
| 2257 | return *getTrailingObjects(); |
| 2258 | } |
| 2259 | |
| 2260 | const FPOptionsOverride &getTrailingFPFeatures() const { |
| 2261 | assert(UnaryOperatorBits.HasFPFeatures); |
| 2262 | return *getTrailingObjects(); |
| 2263 | } |
| 2264 | |
| 2265 | public: |
| 2266 | typedef UnaryOperatorKind Opcode; |
| 2267 | |
| 2268 | protected: |
| 2269 | UnaryOperator(const ASTContext &Ctx, Expr *input, Opcode opc, QualType type, |
| 2270 | ExprValueKind VK, ExprObjectKind OK, SourceLocation l, |
| 2271 | bool CanOverflow, FPOptionsOverride FPFeatures); |
| 2272 | |
| 2273 | /// Build an empty unary operator. |
| 2274 | explicit UnaryOperator(bool HasFPFeatures, EmptyShell Empty) |
| 2275 | : Expr(UnaryOperatorClass, Empty) { |
| 2276 | UnaryOperatorBits.Opc = UO_AddrOf; |
| 2277 | UnaryOperatorBits.HasFPFeatures = HasFPFeatures; |
| 2278 | } |
| 2279 | |
| 2280 | public: |
| 2281 | static UnaryOperator *CreateEmpty(const ASTContext &C, bool hasFPFeatures); |
| 2282 | |
| 2283 | static UnaryOperator *Create(const ASTContext &C, Expr *input, Opcode opc, |
| 2284 | QualType type, ExprValueKind VK, |
| 2285 | ExprObjectKind OK, SourceLocation l, |
| 2286 | bool CanOverflow, FPOptionsOverride FPFeatures); |
| 2287 | |
| 2288 | Opcode getOpcode() const { |
| 2289 | return static_cast<Opcode>(UnaryOperatorBits.Opc); |
| 2290 | } |
| 2291 | void setOpcode(Opcode Opc) { UnaryOperatorBits.Opc = Opc; } |
| 2292 | |
| 2293 | Expr *getSubExpr() const { return cast<Expr>(Val); } |
| 2294 | void setSubExpr(Expr *E) { Val = E; } |
| 2295 | |
| 2296 | /// getOperatorLoc - Return the location of the operator. |
| 2297 | SourceLocation getOperatorLoc() const { return UnaryOperatorBits.Loc; } |
| 2298 | void setOperatorLoc(SourceLocation L) { UnaryOperatorBits.Loc = L; } |
| 2299 | |
| 2300 | /// Returns true if the unary operator can cause an overflow. For instance, |
| 2301 | /// signed int i = INT_MAX; i++; |
| 2302 | /// signed char c = CHAR_MAX; c++; |
| 2303 | /// Due to integer promotions, c++ is promoted to an int before the postfix |
| 2304 | /// increment, and the result is an int that cannot overflow. However, i++ |
| 2305 | /// can overflow. |
| 2306 | bool canOverflow() const { return UnaryOperatorBits.CanOverflow; } |
| 2307 | void setCanOverflow(bool C) { UnaryOperatorBits.CanOverflow = C; } |
| 2308 | |
| 2309 | /// Get the FP contractibility status of this operator. Only meaningful for |
| 2310 | /// operations on floating point types. |
| 2311 | bool isFPContractableWithinStatement(const LangOptions &LO) const { |
| 2312 | return getFPFeaturesInEffect(LO).allowFPContractWithinStatement(); |
| 2313 | } |
| 2314 | |
| 2315 | /// Get the FENV_ACCESS status of this operator. Only meaningful for |
| 2316 | /// operations on floating point types. |
| 2317 | bool isFEnvAccessOn(const LangOptions &LO) const { |
| 2318 | return getFPFeaturesInEffect(LO).getAllowFEnvAccess(); |
| 2319 | } |
| 2320 | |
| 2321 | /// isPostfix - Return true if this is a postfix operation, like x++. |
| 2322 | static bool isPostfix(Opcode Op) { |
| 2323 | return Op == UO_PostInc || Op == UO_PostDec; |
| 2324 | } |
| 2325 | |
| 2326 | /// isPrefix - Return true if this is a prefix operation, like --x. |
| 2327 | static bool isPrefix(Opcode Op) { |
| 2328 | return Op == UO_PreInc || Op == UO_PreDec; |
| 2329 | } |
| 2330 | |
| 2331 | bool isPrefix() const { return isPrefix(Op: getOpcode()); } |
| 2332 | bool isPostfix() const { return isPostfix(Op: getOpcode()); } |
| 2333 | |
| 2334 | static bool isIncrementOp(Opcode Op) { |
| 2335 | return Op == UO_PreInc || Op == UO_PostInc; |
| 2336 | } |
| 2337 | bool isIncrementOp() const { |
| 2338 | return isIncrementOp(Op: getOpcode()); |
| 2339 | } |
| 2340 | |
| 2341 | static bool isDecrementOp(Opcode Op) { |
| 2342 | return Op == UO_PreDec || Op == UO_PostDec; |
| 2343 | } |
| 2344 | bool isDecrementOp() const { |
| 2345 | return isDecrementOp(Op: getOpcode()); |
| 2346 | } |
| 2347 | |
| 2348 | static bool isIncrementDecrementOp(Opcode Op) { return Op <= UO_PreDec; } |
| 2349 | bool isIncrementDecrementOp() const { |
| 2350 | return isIncrementDecrementOp(Op: getOpcode()); |
| 2351 | } |
| 2352 | |
| 2353 | static bool isArithmeticOp(Opcode Op) { |
| 2354 | return Op >= UO_Plus && Op <= UO_LNot; |
| 2355 | } |
| 2356 | bool isArithmeticOp() const { return isArithmeticOp(Op: getOpcode()); } |
| 2357 | |
| 2358 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 2359 | /// corresponds to, e.g. "sizeof" or "[pre]++" |
| 2360 | static StringRef getOpcodeStr(Opcode Op); |
| 2361 | |
| 2362 | /// Retrieve the unary opcode that corresponds to the given |
| 2363 | /// overloaded operator. |
| 2364 | static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); |
| 2365 | |
| 2366 | /// Retrieve the overloaded operator kind that corresponds to |
| 2367 | /// the given unary opcode. |
| 2368 | static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); |
| 2369 | |
| 2370 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 2371 | return isPostfix() ? Val->getBeginLoc() : getOperatorLoc(); |
| 2372 | } |
| 2373 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 2374 | return isPostfix() ? getOperatorLoc() : Val->getEndLoc(); |
| 2375 | } |
| 2376 | SourceLocation getExprLoc() const { return getOperatorLoc(); } |
| 2377 | |
| 2378 | static bool classof(const Stmt *T) { |
| 2379 | return T->getStmtClass() == UnaryOperatorClass; |
| 2380 | } |
| 2381 | |
| 2382 | // Iterators |
| 2383 | child_range children() { return child_range(&Val, &Val+1); } |
| 2384 | const_child_range children() const { |
| 2385 | return const_child_range(&Val, &Val + 1); |
| 2386 | } |
| 2387 | |
| 2388 | /// Is FPFeatures in Trailing Storage? |
| 2389 | bool hasStoredFPFeatures() const { return UnaryOperatorBits.HasFPFeatures; } |
| 2390 | |
| 2391 | /// Get FPFeatures from trailing storage. |
| 2392 | FPOptionsOverride getStoredFPFeatures() const { |
| 2393 | return getTrailingFPFeatures(); |
| 2394 | } |
| 2395 | |
| 2396 | /// Get the store FPOptionsOverride or default if not stored. |
| 2397 | FPOptionsOverride getStoredFPFeaturesOrDefault() const { |
| 2398 | return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride(); |
| 2399 | } |
| 2400 | |
| 2401 | protected: |
| 2402 | /// Set FPFeatures in trailing storage, used by Serialization & ASTImporter. |
| 2403 | void setStoredFPFeatures(FPOptionsOverride F) { getTrailingFPFeatures() = F; } |
| 2404 | |
| 2405 | public: |
| 2406 | /// Get the FP features status of this operator. Only meaningful for |
| 2407 | /// operations on floating point types. |
| 2408 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { |
| 2409 | if (UnaryOperatorBits.HasFPFeatures) |
| 2410 | return getStoredFPFeatures().applyOverrides(LO); |
| 2411 | return FPOptions::defaultWithoutTrailingStorage(LO); |
| 2412 | } |
| 2413 | FPOptionsOverride getFPOptionsOverride() const { |
| 2414 | if (UnaryOperatorBits.HasFPFeatures) |
| 2415 | return getStoredFPFeatures(); |
| 2416 | return FPOptionsOverride(); |
| 2417 | } |
| 2418 | |
| 2419 | friend TrailingObjects; |
| 2420 | friend class ASTNodeImporter; |
| 2421 | friend class ASTReader; |
| 2422 | friend class ASTStmtReader; |
| 2423 | friend class ASTStmtWriter; |
| 2424 | }; |
| 2425 | |
| 2426 | /// Helper class for OffsetOfExpr. |
| 2427 | |
| 2428 | // __builtin_offsetof(type, identifier(.identifier|[expr])*) |
| 2429 | class OffsetOfNode { |
| 2430 | public: |
| 2431 | /// The kind of offsetof node we have. |
| 2432 | enum Kind { |
| 2433 | /// An index into an array. |
| 2434 | Array = 0x00, |
| 2435 | /// A field. |
| 2436 | Field = 0x01, |
| 2437 | /// A field in a dependent type, known only by its name. |
| 2438 | Identifier = 0x02, |
| 2439 | /// An implicit indirection through a C++ base class, when the |
| 2440 | /// field found is in a base class. |
| 2441 | Base = 0x03 |
| 2442 | }; |
| 2443 | |
| 2444 | private: |
| 2445 | enum { MaskBits = 2, Mask = 0x03 }; |
| 2446 | |
| 2447 | /// The source range that covers this part of the designator. |
| 2448 | SourceRange Range; |
| 2449 | |
| 2450 | /// The data describing the designator, which comes in three |
| 2451 | /// different forms, depending on the lower two bits. |
| 2452 | /// - An unsigned index into the array of Expr*'s stored after this node |
| 2453 | /// in memory, for [constant-expression] designators. |
| 2454 | /// - A FieldDecl*, for references to a known field. |
| 2455 | /// - An IdentifierInfo*, for references to a field with a given name |
| 2456 | /// when the class type is dependent. |
| 2457 | /// - A CXXBaseSpecifier*, for references that look at a field in a |
| 2458 | /// base class. |
| 2459 | uintptr_t Data; |
| 2460 | |
| 2461 | public: |
| 2462 | /// Create an offsetof node that refers to an array element. |
| 2463 | OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, |
| 2464 | SourceLocation RBracketLoc) |
| 2465 | : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) {} |
| 2466 | |
| 2467 | /// Create an offsetof node that refers to a field. |
| 2468 | OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, SourceLocation NameLoc) |
| 2469 | : Range(DotLoc.isValid() ? DotLoc : NameLoc, NameLoc), |
| 2470 | Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) {} |
| 2471 | |
| 2472 | /// Create an offsetof node that refers to an identifier. |
| 2473 | OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, |
| 2474 | SourceLocation NameLoc) |
| 2475 | : Range(DotLoc.isValid() ? DotLoc : NameLoc, NameLoc), |
| 2476 | Data(reinterpret_cast<uintptr_t>(Name) | Identifier) {} |
| 2477 | |
| 2478 | /// Create an offsetof node that refers into a C++ base class. |
| 2479 | explicit OffsetOfNode(const CXXBaseSpecifier *Base) |
| 2480 | : Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} |
| 2481 | |
| 2482 | /// Determine what kind of offsetof node this is. |
| 2483 | Kind getKind() const { return static_cast<Kind>(Data & Mask); } |
| 2484 | |
| 2485 | /// For an array element node, returns the index into the array |
| 2486 | /// of expressions. |
| 2487 | unsigned getArrayExprIndex() const { |
| 2488 | assert(getKind() == Array); |
| 2489 | return Data >> 2; |
| 2490 | } |
| 2491 | |
| 2492 | /// For a field offsetof node, returns the field. |
| 2493 | FieldDecl *getField() const { |
| 2494 | assert(getKind() == Field); |
| 2495 | return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); |
| 2496 | } |
| 2497 | |
| 2498 | /// For a field or identifier offsetof node, returns the name of |
| 2499 | /// the field. |
| 2500 | IdentifierInfo *getFieldName() const; |
| 2501 | |
| 2502 | /// For a base class node, returns the base specifier. |
| 2503 | CXXBaseSpecifier *getBase() const { |
| 2504 | assert(getKind() == Base); |
| 2505 | return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); |
| 2506 | } |
| 2507 | |
| 2508 | /// Retrieve the source range that covers this offsetof node. |
| 2509 | /// |
| 2510 | /// For an array element node, the source range contains the locations of |
| 2511 | /// the square brackets. For a field or identifier node, the source range |
| 2512 | /// contains the location of the period (if there is one) and the |
| 2513 | /// identifier. |
| 2514 | SourceRange getSourceRange() const LLVM_READONLY { return Range; } |
| 2515 | SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } |
| 2516 | SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } |
| 2517 | }; |
| 2518 | |
| 2519 | /// OffsetOfExpr - [C99 7.17] - This represents an expression of the form |
| 2520 | /// offsetof(record-type, member-designator). For example, given: |
| 2521 | /// @code |
| 2522 | /// struct S { |
| 2523 | /// float f; |
| 2524 | /// double d; |
| 2525 | /// }; |
| 2526 | /// struct T { |
| 2527 | /// int i; |
| 2528 | /// struct S s[10]; |
| 2529 | /// }; |
| 2530 | /// @endcode |
| 2531 | /// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). |
| 2532 | |
| 2533 | class OffsetOfExpr final |
| 2534 | : public Expr, |
| 2535 | private llvm::TrailingObjects<OffsetOfExpr, OffsetOfNode, Expr *> { |
| 2536 | SourceLocation OperatorLoc, RParenLoc; |
| 2537 | // Base type; |
| 2538 | TypeSourceInfo *TSInfo; |
| 2539 | // Number of sub-components (i.e. instances of OffsetOfNode). |
| 2540 | unsigned NumComps; |
| 2541 | // Number of sub-expressions (i.e. array subscript expressions). |
| 2542 | unsigned NumExprs; |
| 2543 | |
| 2544 | size_t numTrailingObjects(OverloadToken<OffsetOfNode>) const { |
| 2545 | return NumComps; |
| 2546 | } |
| 2547 | |
| 2548 | OffsetOfExpr(const ASTContext &C, QualType type, |
| 2549 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, |
| 2550 | ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs, |
| 2551 | SourceLocation RParenLoc); |
| 2552 | |
| 2553 | explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) |
| 2554 | : Expr(OffsetOfExprClass, EmptyShell()), |
| 2555 | TSInfo(nullptr), NumComps(numComps), NumExprs(numExprs) {} |
| 2556 | |
| 2557 | public: |
| 2558 | |
| 2559 | static OffsetOfExpr *Create(const ASTContext &C, QualType type, |
| 2560 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, |
| 2561 | ArrayRef<OffsetOfNode> comps, |
| 2562 | ArrayRef<Expr*> exprs, SourceLocation RParenLoc); |
| 2563 | |
| 2564 | static OffsetOfExpr *CreateEmpty(const ASTContext &C, |
| 2565 | unsigned NumComps, unsigned NumExprs); |
| 2566 | |
| 2567 | /// getOperatorLoc - Return the location of the operator. |
| 2568 | SourceLocation getOperatorLoc() const { return OperatorLoc; } |
| 2569 | void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } |
| 2570 | |
| 2571 | /// Return the location of the right parentheses. |
| 2572 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 2573 | void setRParenLoc(SourceLocation R) { RParenLoc = R; } |
| 2574 | |
| 2575 | TypeSourceInfo *getTypeSourceInfo() const { |
| 2576 | return TSInfo; |
| 2577 | } |
| 2578 | void setTypeSourceInfo(TypeSourceInfo *tsi) { |
| 2579 | TSInfo = tsi; |
| 2580 | } |
| 2581 | |
| 2582 | const OffsetOfNode &getComponent(unsigned Idx) const { |
| 2583 | return getTrailingObjects<OffsetOfNode>(NumComps)[Idx]; |
| 2584 | } |
| 2585 | |
| 2586 | void setComponent(unsigned Idx, OffsetOfNode ON) { |
| 2587 | getTrailingObjects<OffsetOfNode>(NumComps)[Idx] = ON; |
| 2588 | } |
| 2589 | |
| 2590 | unsigned getNumComponents() const { |
| 2591 | return NumComps; |
| 2592 | } |
| 2593 | |
| 2594 | Expr* getIndexExpr(unsigned Idx) { |
| 2595 | return getTrailingObjects<Expr *>(NumExprs)[Idx]; |
| 2596 | } |
| 2597 | |
| 2598 | const Expr *getIndexExpr(unsigned Idx) const { |
| 2599 | return getTrailingObjects<Expr *>(NumExprs)[Idx]; |
| 2600 | } |
| 2601 | |
| 2602 | void setIndexExpr(unsigned Idx, Expr* E) { |
| 2603 | getTrailingObjects<Expr *>(NumComps)[Idx] = E; |
| 2604 | } |
| 2605 | |
| 2606 | unsigned getNumExpressions() const { |
| 2607 | return NumExprs; |
| 2608 | } |
| 2609 | |
| 2610 | SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; } |
| 2611 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 2612 | |
| 2613 | static bool classof(const Stmt *T) { |
| 2614 | return T->getStmtClass() == OffsetOfExprClass; |
| 2615 | } |
| 2616 | |
| 2617 | // Iterators |
| 2618 | child_range children() { |
| 2619 | Stmt **begin = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>()); |
| 2620 | return child_range(begin, begin + NumExprs); |
| 2621 | } |
| 2622 | const_child_range children() const { |
| 2623 | Stmt *const *begin = |
| 2624 | reinterpret_cast<Stmt *const *>(getTrailingObjects<Expr *>()); |
| 2625 | return const_child_range(begin, begin + NumExprs); |
| 2626 | } |
| 2627 | friend TrailingObjects; |
| 2628 | }; |
| 2629 | |
| 2630 | /// UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) |
| 2631 | /// expression operand. Used for sizeof/alignof (C99 6.5.3.4) and |
| 2632 | /// vec_step (OpenCL 1.1 6.11.12). |
| 2633 | class UnaryExprOrTypeTraitExpr : public Expr { |
| 2634 | union { |
| 2635 | TypeSourceInfo *Ty; |
| 2636 | Stmt *Ex; |
| 2637 | } Argument; |
| 2638 | SourceLocation OpLoc, RParenLoc; |
| 2639 | |
| 2640 | public: |
| 2641 | UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, TypeSourceInfo *TInfo, |
| 2642 | QualType resultType, SourceLocation op, |
| 2643 | SourceLocation rp) |
| 2644 | : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_PRValue, |
| 2645 | OK_Ordinary), |
| 2646 | OpLoc(op), RParenLoc(rp) { |
| 2647 | assert(ExprKind <= UETT_Last && "invalid enum value!" ); |
| 2648 | UnaryExprOrTypeTraitExprBits.Kind = ExprKind; |
| 2649 | assert(static_cast<unsigned>(ExprKind) == |
| 2650 | UnaryExprOrTypeTraitExprBits.Kind && |
| 2651 | "UnaryExprOrTypeTraitExprBits.Kind overflow!" ); |
| 2652 | UnaryExprOrTypeTraitExprBits.IsType = true; |
| 2653 | Argument.Ty = TInfo; |
| 2654 | setDependence(computeDependence(E: this)); |
| 2655 | } |
| 2656 | |
| 2657 | UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, Expr *E, |
| 2658 | QualType resultType, SourceLocation op, |
| 2659 | SourceLocation rp); |
| 2660 | |
| 2661 | /// Construct an empty sizeof/alignof expression. |
| 2662 | explicit UnaryExprOrTypeTraitExpr(EmptyShell Empty) |
| 2663 | : Expr(UnaryExprOrTypeTraitExprClass, Empty) { } |
| 2664 | |
| 2665 | UnaryExprOrTypeTrait getKind() const { |
| 2666 | return static_cast<UnaryExprOrTypeTrait>(UnaryExprOrTypeTraitExprBits.Kind); |
| 2667 | } |
| 2668 | void setKind(UnaryExprOrTypeTrait K) { |
| 2669 | assert(K <= UETT_Last && "invalid enum value!" ); |
| 2670 | UnaryExprOrTypeTraitExprBits.Kind = K; |
| 2671 | assert(static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits.Kind && |
| 2672 | "UnaryExprOrTypeTraitExprBits.Kind overflow!" ); |
| 2673 | } |
| 2674 | |
| 2675 | bool isArgumentType() const { return UnaryExprOrTypeTraitExprBits.IsType; } |
| 2676 | QualType getArgumentType() const { |
| 2677 | return getArgumentTypeInfo()->getType(); |
| 2678 | } |
| 2679 | TypeSourceInfo *getArgumentTypeInfo() const { |
| 2680 | assert(isArgumentType() && "calling getArgumentType() when arg is expr" ); |
| 2681 | return Argument.Ty; |
| 2682 | } |
| 2683 | Expr *getArgumentExpr() { |
| 2684 | assert(!isArgumentType() && "calling getArgumentExpr() when arg is type" ); |
| 2685 | return static_cast<Expr*>(Argument.Ex); |
| 2686 | } |
| 2687 | const Expr *getArgumentExpr() const { |
| 2688 | return const_cast<UnaryExprOrTypeTraitExpr*>(this)->getArgumentExpr(); |
| 2689 | } |
| 2690 | |
| 2691 | void setArgument(Expr *E) { |
| 2692 | Argument.Ex = E; |
| 2693 | UnaryExprOrTypeTraitExprBits.IsType = false; |
| 2694 | } |
| 2695 | void setArgument(TypeSourceInfo *TInfo) { |
| 2696 | Argument.Ty = TInfo; |
| 2697 | UnaryExprOrTypeTraitExprBits.IsType = true; |
| 2698 | } |
| 2699 | |
| 2700 | /// Gets the argument type, or the type of the argument expression, whichever |
| 2701 | /// is appropriate. |
| 2702 | QualType getTypeOfArgument() const { |
| 2703 | return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); |
| 2704 | } |
| 2705 | |
| 2706 | SourceLocation getOperatorLoc() const { return OpLoc; } |
| 2707 | void setOperatorLoc(SourceLocation L) { OpLoc = L; } |
| 2708 | |
| 2709 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 2710 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 2711 | |
| 2712 | SourceLocation getBeginLoc() const LLVM_READONLY { return OpLoc; } |
| 2713 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 2714 | |
| 2715 | static bool classof(const Stmt *T) { |
| 2716 | return T->getStmtClass() == UnaryExprOrTypeTraitExprClass; |
| 2717 | } |
| 2718 | |
| 2719 | // Iterators |
| 2720 | child_range children(); |
| 2721 | const_child_range children() const; |
| 2722 | }; |
| 2723 | |
| 2724 | //===----------------------------------------------------------------------===// |
| 2725 | // Postfix Operators. |
| 2726 | //===----------------------------------------------------------------------===// |
| 2727 | |
| 2728 | /// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. |
| 2729 | class ArraySubscriptExpr : public Expr { |
| 2730 | enum { LHS, RHS, END_EXPR }; |
| 2731 | Stmt *SubExprs[END_EXPR]; |
| 2732 | |
| 2733 | bool lhsIsBase() const { return getRHS()->getType()->isIntegerType(); } |
| 2734 | |
| 2735 | public: |
| 2736 | ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, ExprValueKind VK, |
| 2737 | ExprObjectKind OK, SourceLocation rbracketloc) |
| 2738 | : Expr(ArraySubscriptExprClass, t, VK, OK) { |
| 2739 | SubExprs[LHS] = lhs; |
| 2740 | SubExprs[RHS] = rhs; |
| 2741 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = rbracketloc; |
| 2742 | setDependence(computeDependence(E: this)); |
| 2743 | } |
| 2744 | |
| 2745 | /// Create an empty array subscript expression. |
| 2746 | explicit ArraySubscriptExpr(EmptyShell Shell) |
| 2747 | : Expr(ArraySubscriptExprClass, Shell) { } |
| 2748 | |
| 2749 | /// An array access can be written A[4] or 4[A] (both are equivalent). |
| 2750 | /// - getBase() and getIdx() always present the normalized view: A[4]. |
| 2751 | /// In this case getBase() returns "A" and getIdx() returns "4". |
| 2752 | /// - getLHS() and getRHS() present the syntactic view. e.g. for |
| 2753 | /// 4[A] getLHS() returns "4". |
| 2754 | /// Note: Because vector element access is also written A[4] we must |
| 2755 | /// predicate the format conversion in getBase and getIdx only on the |
| 2756 | /// the type of the RHS, as it is possible for the LHS to be a vector of |
| 2757 | /// integer type |
| 2758 | Expr *getLHS() { return cast<Expr>(Val: SubExprs[LHS]); } |
| 2759 | const Expr *getLHS() const { return cast<Expr>(Val: SubExprs[LHS]); } |
| 2760 | void setLHS(Expr *E) { SubExprs[LHS] = E; } |
| 2761 | |
| 2762 | Expr *getRHS() { return cast<Expr>(Val: SubExprs[RHS]); } |
| 2763 | const Expr *getRHS() const { return cast<Expr>(Val: SubExprs[RHS]); } |
| 2764 | void setRHS(Expr *E) { SubExprs[RHS] = E; } |
| 2765 | |
| 2766 | Expr *getBase() { return lhsIsBase() ? getLHS() : getRHS(); } |
| 2767 | const Expr *getBase() const { return lhsIsBase() ? getLHS() : getRHS(); } |
| 2768 | |
| 2769 | Expr *getIdx() { return lhsIsBase() ? getRHS() : getLHS(); } |
| 2770 | const Expr *getIdx() const { return lhsIsBase() ? getRHS() : getLHS(); } |
| 2771 | |
| 2772 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 2773 | return getLHS()->getBeginLoc(); |
| 2774 | } |
| 2775 | SourceLocation getEndLoc() const { return getRBracketLoc(); } |
| 2776 | |
| 2777 | SourceLocation getRBracketLoc() const { |
| 2778 | return ArrayOrMatrixSubscriptExprBits.RBracketLoc; |
| 2779 | } |
| 2780 | void setRBracketLoc(SourceLocation L) { |
| 2781 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = L; |
| 2782 | } |
| 2783 | |
| 2784 | SourceLocation getExprLoc() const LLVM_READONLY { |
| 2785 | return getBase()->getExprLoc(); |
| 2786 | } |
| 2787 | |
| 2788 | static bool classof(const Stmt *T) { |
| 2789 | return T->getStmtClass() == ArraySubscriptExprClass; |
| 2790 | } |
| 2791 | |
| 2792 | // Iterators |
| 2793 | child_range children() { |
| 2794 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); |
| 2795 | } |
| 2796 | const_child_range children() const { |
| 2797 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 2798 | } |
| 2799 | }; |
| 2800 | |
| 2801 | /// MatrixSubscriptExpr - Matrix subscript expression for the MatrixType |
| 2802 | /// extension. |
| 2803 | /// MatrixSubscriptExpr can be either incomplete (only Base and RowIdx are set |
| 2804 | /// so far, the type is IncompleteMatrixIdx) or complete (Base, RowIdx and |
| 2805 | /// ColumnIdx refer to valid expressions). Incomplete matrix expressions only |
| 2806 | /// exist during the initial construction of the AST. |
| 2807 | class MatrixSubscriptExpr : public Expr { |
| 2808 | enum { BASE, ROW_IDX, COLUMN_IDX, END_EXPR }; |
| 2809 | Stmt *SubExprs[END_EXPR]; |
| 2810 | |
| 2811 | public: |
| 2812 | MatrixSubscriptExpr(Expr *Base, Expr *RowIdx, Expr *ColumnIdx, QualType T, |
| 2813 | SourceLocation RBracketLoc) |
| 2814 | : Expr(MatrixSubscriptExprClass, T, Base->getValueKind(), |
| 2815 | OK_MatrixComponent) { |
| 2816 | SubExprs[BASE] = Base; |
| 2817 | SubExprs[ROW_IDX] = RowIdx; |
| 2818 | SubExprs[COLUMN_IDX] = ColumnIdx; |
| 2819 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = RBracketLoc; |
| 2820 | setDependence(computeDependence(E: this)); |
| 2821 | } |
| 2822 | |
| 2823 | /// Create an empty matrix subscript expression. |
| 2824 | explicit MatrixSubscriptExpr(EmptyShell Shell) |
| 2825 | : Expr(MatrixSubscriptExprClass, Shell) {} |
| 2826 | |
| 2827 | bool isIncomplete() const { |
| 2828 | bool IsIncomplete = hasPlaceholderType(BuiltinType::IncompleteMatrixIdx); |
| 2829 | assert((SubExprs[COLUMN_IDX] || IsIncomplete) && |
| 2830 | "expressions without column index must be marked as incomplete" ); |
| 2831 | return IsIncomplete; |
| 2832 | } |
| 2833 | Expr *getBase() { return cast<Expr>(Val: SubExprs[BASE]); } |
| 2834 | const Expr *getBase() const { return cast<Expr>(Val: SubExprs[BASE]); } |
| 2835 | void setBase(Expr *E) { SubExprs[BASE] = E; } |
| 2836 | |
| 2837 | Expr *getRowIdx() { return cast<Expr>(Val: SubExprs[ROW_IDX]); } |
| 2838 | const Expr *getRowIdx() const { return cast<Expr>(Val: SubExprs[ROW_IDX]); } |
| 2839 | void setRowIdx(Expr *E) { SubExprs[ROW_IDX] = E; } |
| 2840 | |
| 2841 | Expr *getColumnIdx() { return cast_or_null<Expr>(Val: SubExprs[COLUMN_IDX]); } |
| 2842 | const Expr *getColumnIdx() const { |
| 2843 | assert(!isIncomplete() && |
| 2844 | "cannot get the column index of an incomplete expression" ); |
| 2845 | return cast<Expr>(Val: SubExprs[COLUMN_IDX]); |
| 2846 | } |
| 2847 | void setColumnIdx(Expr *E) { SubExprs[COLUMN_IDX] = E; } |
| 2848 | |
| 2849 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 2850 | return getBase()->getBeginLoc(); |
| 2851 | } |
| 2852 | |
| 2853 | SourceLocation getEndLoc() const { return getRBracketLoc(); } |
| 2854 | |
| 2855 | SourceLocation getExprLoc() const LLVM_READONLY { |
| 2856 | return getBase()->getExprLoc(); |
| 2857 | } |
| 2858 | |
| 2859 | SourceLocation getRBracketLoc() const { |
| 2860 | return ArrayOrMatrixSubscriptExprBits.RBracketLoc; |
| 2861 | } |
| 2862 | void setRBracketLoc(SourceLocation L) { |
| 2863 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = L; |
| 2864 | } |
| 2865 | |
| 2866 | static bool classof(const Stmt *T) { |
| 2867 | return T->getStmtClass() == MatrixSubscriptExprClass; |
| 2868 | } |
| 2869 | |
| 2870 | // Iterators |
| 2871 | child_range children() { |
| 2872 | return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 2873 | } |
| 2874 | const_child_range children() const { |
| 2875 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 2876 | } |
| 2877 | }; |
| 2878 | |
| 2879 | /// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). |
| 2880 | /// CallExpr itself represents a normal function call, e.g., "f(x, 2)", |
| 2881 | /// while its subclasses may represent alternative syntax that (semantically) |
| 2882 | /// results in a function call. For example, CXXOperatorCallExpr is |
| 2883 | /// a subclass for overloaded operator calls that use operator syntax, e.g., |
| 2884 | /// "str1 + str2" to resolve to a function call. |
| 2885 | class CallExpr : public Expr { |
| 2886 | enum { FN = 0, PREARGS_START = 1 }; |
| 2887 | |
| 2888 | /// The number of arguments in the call expression. |
| 2889 | unsigned NumArgs; |
| 2890 | |
| 2891 | /// The location of the right parentheses. This has a different meaning for |
| 2892 | /// the derived classes of CallExpr. |
| 2893 | SourceLocation RParenLoc; |
| 2894 | |
| 2895 | // CallExpr store some data in trailing objects. However since CallExpr |
| 2896 | // is used a base of other expression classes we cannot use |
| 2897 | // llvm::TrailingObjects. Instead we manually perform the pointer arithmetic |
| 2898 | // and casts. |
| 2899 | // |
| 2900 | // The trailing objects are in order: |
| 2901 | // |
| 2902 | // * A single "Stmt *" for the callee expression. |
| 2903 | // |
| 2904 | // * An array of getNumPreArgs() "Stmt *" for the pre-argument expressions. |
| 2905 | // |
| 2906 | // * An array of getNumArgs() "Stmt *" for the argument expressions. |
| 2907 | // |
| 2908 | // * An optional of type FPOptionsOverride. |
| 2909 | // |
| 2910 | // CallExpr subclasses are asssumed to be 32 bytes or less, and CallExpr |
| 2911 | // itself is 24 bytes. To avoid having to recompute or store the offset of the |
| 2912 | // trailing objects, we put it at 32 bytes (such that it is suitable for all |
| 2913 | // subclasses) We use the 8 bytes gap left for instances of CallExpr to store |
| 2914 | // the begin source location, which has a significant impact on perf as |
| 2915 | // getBeginLoc is assumed to be cheap. |
| 2916 | // The layourt is as follow: |
| 2917 | // CallExpr | Begin | 4 bytes left | Trailing Objects |
| 2918 | // CXXMemberCallExpr | Trailing Objects |
| 2919 | // A bit in CallExprBitfields indicates if source locations are present. |
| 2920 | |
| 2921 | protected: |
| 2922 | static constexpr unsigned OffsetToTrailingObjects = 32; |
| 2923 | template <typename T> |
| 2924 | static constexpr unsigned |
| 2925 | sizeToAllocateForCallExprSubclass(unsigned SizeOfTrailingObjects) { |
| 2926 | static_assert(sizeof(T) <= CallExpr::OffsetToTrailingObjects); |
| 2927 | return SizeOfTrailingObjects + CallExpr::OffsetToTrailingObjects; |
| 2928 | } |
| 2929 | |
| 2930 | private: |
| 2931 | /// Return a pointer to the start of the trailing array of "Stmt *". |
| 2932 | Stmt **getTrailingStmts() { |
| 2933 | return reinterpret_cast<Stmt **>(reinterpret_cast<char *>(this) + |
| 2934 | OffsetToTrailingObjects); |
| 2935 | } |
| 2936 | Stmt *const *getTrailingStmts() const { |
| 2937 | return const_cast<CallExpr *>(this)->getTrailingStmts(); |
| 2938 | } |
| 2939 | |
| 2940 | unsigned getSizeOfTrailingStmts() const { |
| 2941 | return (1 + getNumPreArgs() + getNumArgs()) * sizeof(Stmt *); |
| 2942 | } |
| 2943 | |
| 2944 | size_t getOffsetOfTrailingFPFeatures() const { |
| 2945 | assert(hasStoredFPFeatures()); |
| 2946 | return OffsetToTrailingObjects + getSizeOfTrailingStmts(); |
| 2947 | } |
| 2948 | |
| 2949 | public: |
| 2950 | enum class ADLCallKind : bool { NotADL, UsesADL }; |
| 2951 | static constexpr ADLCallKind NotADL = ADLCallKind::NotADL; |
| 2952 | static constexpr ADLCallKind UsesADL = ADLCallKind::UsesADL; |
| 2953 | |
| 2954 | protected: |
| 2955 | /// Build a call expression, assuming that appropriate storage has been |
| 2956 | /// allocated for the trailing objects. |
| 2957 | CallExpr(StmtClass SC, Expr *Fn, ArrayRef<Expr *> PreArgs, |
| 2958 | ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, |
| 2959 | SourceLocation RParenLoc, FPOptionsOverride FPFeatures, |
| 2960 | unsigned MinNumArgs, ADLCallKind UsesADL); |
| 2961 | |
| 2962 | /// Build an empty call expression, for deserialization. |
| 2963 | CallExpr(StmtClass SC, unsigned NumPreArgs, unsigned NumArgs, |
| 2964 | bool hasFPFeatures, EmptyShell Empty); |
| 2965 | |
| 2966 | /// Return the size in bytes needed for the trailing objects. |
| 2967 | /// Used by the derived classes to allocate the right amount of storage. |
| 2968 | static unsigned sizeOfTrailingObjects(unsigned NumPreArgs, unsigned NumArgs, |
| 2969 | bool HasFPFeatures) { |
| 2970 | return (1 + NumPreArgs + NumArgs) * sizeof(Stmt *) + |
| 2971 | HasFPFeatures * sizeof(FPOptionsOverride); |
| 2972 | } |
| 2973 | |
| 2974 | Stmt *getPreArg(unsigned I) { |
| 2975 | assert(I < getNumPreArgs() && "Prearg access out of range!" ); |
| 2976 | return getTrailingStmts()[PREARGS_START + I]; |
| 2977 | } |
| 2978 | const Stmt *getPreArg(unsigned I) const { |
| 2979 | assert(I < getNumPreArgs() && "Prearg access out of range!" ); |
| 2980 | return getTrailingStmts()[PREARGS_START + I]; |
| 2981 | } |
| 2982 | void setPreArg(unsigned I, Stmt *PreArg) { |
| 2983 | assert(I < getNumPreArgs() && "Prearg access out of range!" ); |
| 2984 | getTrailingStmts()[PREARGS_START + I] = PreArg; |
| 2985 | } |
| 2986 | |
| 2987 | unsigned getNumPreArgs() const { return CallExprBits.NumPreArgs; } |
| 2988 | |
| 2989 | /// Return a pointer to the trailing FPOptions |
| 2990 | FPOptionsOverride *getTrailingFPFeatures() { |
| 2991 | assert(hasStoredFPFeatures()); |
| 2992 | return reinterpret_cast<FPOptionsOverride *>( |
| 2993 | reinterpret_cast<char *>(this) + OffsetToTrailingObjects + |
| 2994 | getSizeOfTrailingStmts()); |
| 2995 | } |
| 2996 | const FPOptionsOverride *getTrailingFPFeatures() const { |
| 2997 | assert(hasStoredFPFeatures()); |
| 2998 | return reinterpret_cast<const FPOptionsOverride *>( |
| 2999 | reinterpret_cast<const char *>(this) + OffsetToTrailingObjects + |
| 3000 | getSizeOfTrailingStmts()); |
| 3001 | } |
| 3002 | |
| 3003 | public: |
| 3004 | /// Create a call expression. |
| 3005 | /// \param Fn The callee expression, |
| 3006 | /// \param Args The argument array, |
| 3007 | /// \param Ty The type of the call expression (which is *not* the return |
| 3008 | /// type in general), |
| 3009 | /// \param VK The value kind of the call expression (lvalue, rvalue, ...), |
| 3010 | /// \param RParenLoc The location of the right parenthesis in the call |
| 3011 | /// expression. |
| 3012 | /// \param FPFeatures Floating-point features associated with the call, |
| 3013 | /// \param MinNumArgs Specifies the minimum number of arguments. The actual |
| 3014 | /// number of arguments will be the greater of Args.size() |
| 3015 | /// and MinNumArgs. This is used in a few places to allocate |
| 3016 | /// enough storage for the default arguments. |
| 3017 | /// \param UsesADL Specifies whether the callee was found through |
| 3018 | /// argument-dependent lookup. |
| 3019 | /// |
| 3020 | /// Note that you can use CreateTemporary if you need a temporary call |
| 3021 | /// expression on the stack. |
| 3022 | static CallExpr *Create(const ASTContext &Ctx, Expr *Fn, |
| 3023 | ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, |
| 3024 | SourceLocation RParenLoc, |
| 3025 | FPOptionsOverride FPFeatures, unsigned MinNumArgs = 0, |
| 3026 | ADLCallKind UsesADL = NotADL); |
| 3027 | |
| 3028 | /// Create an empty call expression, for deserialization. |
| 3029 | static CallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs, |
| 3030 | bool HasFPFeatures, EmptyShell Empty); |
| 3031 | |
| 3032 | Expr *getCallee() { return cast<Expr>(Val: getTrailingStmts()[FN]); } |
| 3033 | const Expr *getCallee() const { return cast<Expr>(Val: getTrailingStmts()[FN]); } |
| 3034 | void setCallee(Expr *F) { getTrailingStmts()[FN] = F; } |
| 3035 | |
| 3036 | ADLCallKind getADLCallKind() const { |
| 3037 | return static_cast<ADLCallKind>(CallExprBits.UsesADL); |
| 3038 | } |
| 3039 | void setADLCallKind(ADLCallKind V = UsesADL) { |
| 3040 | CallExprBits.UsesADL = static_cast<bool>(V); |
| 3041 | } |
| 3042 | bool usesADL() const { return getADLCallKind() == UsesADL; } |
| 3043 | |
| 3044 | bool hasStoredFPFeatures() const { return CallExprBits.HasFPFeatures; } |
| 3045 | |
| 3046 | bool usesMemberSyntax() const { |
| 3047 | return CallExprBits.ExplicitObjectMemFunUsingMemberSyntax; |
| 3048 | } |
| 3049 | void setUsesMemberSyntax(bool V = true) { |
| 3050 | CallExprBits.ExplicitObjectMemFunUsingMemberSyntax = V; |
| 3051 | // Because the source location may be different for explicit |
| 3052 | // member, we reset the cached values. |
| 3053 | if (CallExprBits.HasTrailingSourceLoc) { |
| 3054 | CallExprBits.HasTrailingSourceLoc = false; |
| 3055 | updateTrailingSourceLoc(); |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | bool isCoroElideSafe() const { return CallExprBits.IsCoroElideSafe; } |
| 3060 | void setCoroElideSafe(bool V = true) { CallExprBits.IsCoroElideSafe = V; } |
| 3061 | |
| 3062 | Decl *getCalleeDecl() { return getCallee()->getReferencedDeclOfCallee(); } |
| 3063 | const Decl *getCalleeDecl() const { |
| 3064 | return getCallee()->getReferencedDeclOfCallee(); |
| 3065 | } |
| 3066 | |
| 3067 | /// If the callee is a FunctionDecl, return it. Otherwise return null. |
| 3068 | FunctionDecl *getDirectCallee() { |
| 3069 | return dyn_cast_or_null<FunctionDecl>(Val: getCalleeDecl()); |
| 3070 | } |
| 3071 | const FunctionDecl *getDirectCallee() const { |
| 3072 | return dyn_cast_or_null<FunctionDecl>(Val: getCalleeDecl()); |
| 3073 | } |
| 3074 | |
| 3075 | /// getNumArgs - Return the number of actual arguments to this call. |
| 3076 | unsigned getNumArgs() const { return NumArgs; } |
| 3077 | |
| 3078 | /// Retrieve the call arguments. |
| 3079 | Expr **getArgs() { |
| 3080 | return reinterpret_cast<Expr **>(getTrailingStmts() + PREARGS_START + |
| 3081 | getNumPreArgs()); |
| 3082 | } |
| 3083 | const Expr *const *getArgs() const { |
| 3084 | return reinterpret_cast<const Expr *const *>( |
| 3085 | getTrailingStmts() + PREARGS_START + getNumPreArgs()); |
| 3086 | } |
| 3087 | |
| 3088 | /// getArg - Return the specified argument. |
| 3089 | Expr *getArg(unsigned Arg) { |
| 3090 | assert(Arg < getNumArgs() && "Arg access out of range!" ); |
| 3091 | return getArgs()[Arg]; |
| 3092 | } |
| 3093 | const Expr *getArg(unsigned Arg) const { |
| 3094 | assert(Arg < getNumArgs() && "Arg access out of range!" ); |
| 3095 | return getArgs()[Arg]; |
| 3096 | } |
| 3097 | |
| 3098 | /// setArg - Set the specified argument. |
| 3099 | /// ! the dependence bits might be stale after calling this setter, it is |
| 3100 | /// *caller*'s responsibility to recompute them by calling |
| 3101 | /// computeDependence(). |
| 3102 | void setArg(unsigned Arg, Expr *ArgExpr) { |
| 3103 | assert(Arg < getNumArgs() && "Arg access out of range!" ); |
| 3104 | getArgs()[Arg] = ArgExpr; |
| 3105 | } |
| 3106 | |
| 3107 | /// Compute and set dependence bits. |
| 3108 | void computeDependence() { |
| 3109 | setDependence(clang::computeDependence( |
| 3110 | E: this, PreArgs: llvm::ArrayRef( |
| 3111 | reinterpret_cast<Expr **>(getTrailingStmts() + PREARGS_START), |
| 3112 | getNumPreArgs()))); |
| 3113 | } |
| 3114 | |
| 3115 | /// Reduce the number of arguments in this call expression. This is used for |
| 3116 | /// example during error recovery to drop extra arguments. There is no way |
| 3117 | /// to perform the opposite because: 1.) We don't track how much storage |
| 3118 | /// we have for the argument array 2.) This would potentially require growing |
| 3119 | /// the argument array, something we cannot support since the arguments are |
| 3120 | /// stored in a trailing array. |
| 3121 | void shrinkNumArgs(unsigned NewNumArgs) { |
| 3122 | assert((NewNumArgs <= getNumArgs()) && |
| 3123 | "shrinkNumArgs cannot increase the number of arguments!" ); |
| 3124 | NumArgs = NewNumArgs; |
| 3125 | } |
| 3126 | |
| 3127 | /// Bluntly set a new number of arguments without doing any checks whatsoever. |
| 3128 | /// Only used during construction of a CallExpr in a few places in Sema. |
| 3129 | /// FIXME: Find a way to remove it. |
| 3130 | void setNumArgsUnsafe(unsigned NewNumArgs) { NumArgs = NewNumArgs; } |
| 3131 | |
| 3132 | typedef ExprIterator arg_iterator; |
| 3133 | typedef ConstExprIterator const_arg_iterator; |
| 3134 | typedef llvm::iterator_range<arg_iterator> arg_range; |
| 3135 | typedef llvm::iterator_range<const_arg_iterator> const_arg_range; |
| 3136 | |
| 3137 | arg_range arguments() { return arg_range(arg_begin(), arg_end()); } |
| 3138 | const_arg_range arguments() const { |
| 3139 | return const_arg_range(arg_begin(), arg_end()); |
| 3140 | } |
| 3141 | |
| 3142 | arg_iterator arg_begin() { |
| 3143 | return getTrailingStmts() + PREARGS_START + getNumPreArgs(); |
| 3144 | } |
| 3145 | arg_iterator arg_end() { return arg_begin() + getNumArgs(); } |
| 3146 | |
| 3147 | const_arg_iterator arg_begin() const { |
| 3148 | return getTrailingStmts() + PREARGS_START + getNumPreArgs(); |
| 3149 | } |
| 3150 | const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); } |
| 3151 | |
| 3152 | /// This method provides fast access to all the subexpressions of |
| 3153 | /// a CallExpr without going through the slower virtual child_iterator |
| 3154 | /// interface. This provides efficient reverse iteration of the |
| 3155 | /// subexpressions. This is currently used for CFG construction. |
| 3156 | ArrayRef<Stmt *> getRawSubExprs() { |
| 3157 | return llvm::ArrayRef(getTrailingStmts(), |
| 3158 | PREARGS_START + getNumPreArgs() + getNumArgs()); |
| 3159 | } |
| 3160 | |
| 3161 | /// Get FPOptionsOverride from trailing storage. |
| 3162 | FPOptionsOverride getStoredFPFeatures() const { |
| 3163 | assert(hasStoredFPFeatures()); |
| 3164 | return *getTrailingFPFeatures(); |
| 3165 | } |
| 3166 | /// Set FPOptionsOverride in trailing storage. Used only by Serialization. |
| 3167 | void setStoredFPFeatures(FPOptionsOverride F) { |
| 3168 | assert(hasStoredFPFeatures()); |
| 3169 | *getTrailingFPFeatures() = F; |
| 3170 | } |
| 3171 | |
| 3172 | /// Get the store FPOptionsOverride or default if not stored. |
| 3173 | FPOptionsOverride getStoredFPFeaturesOrDefault() const { |
| 3174 | return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride(); |
| 3175 | } |
| 3176 | |
| 3177 | /// Get the FP features status of this operator. Only meaningful for |
| 3178 | /// operations on floating point types. |
| 3179 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { |
| 3180 | if (hasStoredFPFeatures()) |
| 3181 | return getStoredFPFeatures().applyOverrides(LO); |
| 3182 | return FPOptions::defaultWithoutTrailingStorage(LO); |
| 3183 | } |
| 3184 | |
| 3185 | FPOptionsOverride getFPFeatures() const { |
| 3186 | if (hasStoredFPFeatures()) |
| 3187 | return getStoredFPFeatures(); |
| 3188 | return FPOptionsOverride(); |
| 3189 | } |
| 3190 | |
| 3191 | /// getBuiltinCallee - If this is a call to a builtin, return the builtin ID |
| 3192 | /// of the callee. If not, return 0. |
| 3193 | unsigned getBuiltinCallee() const; |
| 3194 | |
| 3195 | /// Returns \c true if this is a call to a builtin which does not |
| 3196 | /// evaluate side-effects within its arguments. |
| 3197 | bool isUnevaluatedBuiltinCall(const ASTContext &Ctx) const; |
| 3198 | |
| 3199 | /// getCallReturnType - Get the return type of the call expr. This is not |
| 3200 | /// always the type of the expr itself, if the return type is a reference |
| 3201 | /// type. |
| 3202 | QualType getCallReturnType(const ASTContext &Ctx) const; |
| 3203 | |
| 3204 | /// Returns the WarnUnusedResultAttr that is either declared on the called |
| 3205 | /// function, or its return type declaration, together with a NamedDecl that |
| 3206 | /// refers to the declaration the attribute is attached onto. |
| 3207 | std::pair<const NamedDecl *, const Attr *> |
| 3208 | getUnusedResultAttr(const ASTContext &Ctx) const; |
| 3209 | |
| 3210 | /// Returns true if this call expression should warn on unused results. |
| 3211 | bool hasUnusedResultAttr(const ASTContext &Ctx) const { |
| 3212 | return getUnusedResultAttr(Ctx).second != nullptr; |
| 3213 | } |
| 3214 | |
| 3215 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 3216 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 3217 | |
| 3218 | SourceLocation getBeginLoc() const { |
| 3219 | if (CallExprBits.HasTrailingSourceLoc) { |
| 3220 | static_assert(sizeof(CallExpr) <= |
| 3221 | OffsetToTrailingObjects + sizeof(SourceLocation)); |
| 3222 | return *reinterpret_cast<const SourceLocation *>( |
| 3223 | reinterpret_cast<const char *>(this + 1)); |
| 3224 | } |
| 3225 | |
| 3226 | if (usesMemberSyntax()) |
| 3227 | if (auto FirstArgLoc = getArg(Arg: 0)->getBeginLoc(); FirstArgLoc.isValid()) |
| 3228 | return FirstArgLoc; |
| 3229 | |
| 3230 | // FIXME: Some builtins have no callee begin location |
| 3231 | SourceLocation begin = getCallee()->getBeginLoc(); |
| 3232 | if (begin.isInvalid() && getNumArgs() > 0 && getArg(Arg: 0)) |
| 3233 | begin = getArg(Arg: 0)->getBeginLoc(); |
| 3234 | return begin; |
| 3235 | } |
| 3236 | |
| 3237 | SourceLocation getEndLoc() const { return getRParenLoc(); } |
| 3238 | |
| 3239 | private: |
| 3240 | friend class ASTStmtReader; |
| 3241 | bool hasTrailingSourceLoc() const { |
| 3242 | return CallExprBits.HasTrailingSourceLoc; |
| 3243 | } |
| 3244 | |
| 3245 | void updateTrailingSourceLoc() { |
| 3246 | assert(!CallExprBits.HasTrailingSourceLoc && |
| 3247 | "Trailing source loc already set?" ); |
| 3248 | assert(getStmtClass() == CallExprClass && |
| 3249 | "Calling setTrailingSourceLocs on a subclass of CallExpr" ); |
| 3250 | static_assert(sizeof(CallExpr) <= |
| 3251 | OffsetToTrailingObjects + sizeof(SourceLocation)); |
| 3252 | |
| 3253 | SourceLocation *Locs = |
| 3254 | reinterpret_cast<SourceLocation *>(reinterpret_cast<char *>(this + 1)); |
| 3255 | new (Locs) SourceLocation(getBeginLoc()); |
| 3256 | CallExprBits.HasTrailingSourceLoc = true; |
| 3257 | } |
| 3258 | |
| 3259 | public: |
| 3260 | /// Return true if this is a call to __assume() or __builtin_assume() with |
| 3261 | /// a non-value-dependent constant parameter evaluating as false. |
| 3262 | bool isBuiltinAssumeFalse(const ASTContext &Ctx) const; |
| 3263 | |
| 3264 | /// Used by Sema to implement MSVC-compatible delayed name lookup. |
| 3265 | /// (Usually Exprs themselves should set dependence). |
| 3266 | void markDependentForPostponedNameLookup() { |
| 3267 | setDependence(getDependence() | ExprDependence::TypeValueInstantiation); |
| 3268 | } |
| 3269 | |
| 3270 | bool isCallToStdMove() const; |
| 3271 | |
| 3272 | static bool classof(const Stmt *T) { |
| 3273 | return T->getStmtClass() >= firstCallExprConstant && |
| 3274 | T->getStmtClass() <= lastCallExprConstant; |
| 3275 | } |
| 3276 | |
| 3277 | // Iterators |
| 3278 | child_range children() { |
| 3279 | return child_range(getTrailingStmts(), getTrailingStmts() + PREARGS_START + |
| 3280 | getNumPreArgs() + getNumArgs()); |
| 3281 | } |
| 3282 | |
| 3283 | const_child_range children() const { |
| 3284 | return const_child_range(getTrailingStmts(), |
| 3285 | getTrailingStmts() + PREARGS_START + |
| 3286 | getNumPreArgs() + getNumArgs()); |
| 3287 | } |
| 3288 | }; |
| 3289 | |
| 3290 | /// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. |
| 3291 | /// |
| 3292 | class MemberExpr final |
| 3293 | : public Expr, |
| 3294 | private llvm::TrailingObjects<MemberExpr, NestedNameSpecifierLoc, |
| 3295 | DeclAccessPair, ASTTemplateKWAndArgsInfo, |
| 3296 | TemplateArgumentLoc> { |
| 3297 | friend class ASTReader; |
| 3298 | friend class ASTStmtReader; |
| 3299 | friend class ASTStmtWriter; |
| 3300 | friend TrailingObjects; |
| 3301 | |
| 3302 | /// Base - the expression for the base pointer or structure references. In |
| 3303 | /// X.F, this is "X". |
| 3304 | Stmt *Base; |
| 3305 | |
| 3306 | /// MemberDecl - This is the decl being referenced by the field/member name. |
| 3307 | /// In X.F, this is the decl referenced by F. |
| 3308 | ValueDecl *MemberDecl; |
| 3309 | |
| 3310 | /// MemberDNLoc - Provides source/type location info for the |
| 3311 | /// declaration name embedded in MemberDecl. |
| 3312 | DeclarationNameLoc MemberDNLoc; |
| 3313 | |
| 3314 | /// MemberLoc - This is the location of the member name. |
| 3315 | SourceLocation MemberLoc; |
| 3316 | |
| 3317 | size_t numTrailingObjects(OverloadToken<NestedNameSpecifierLoc>) const { |
| 3318 | return hasQualifier(); |
| 3319 | } |
| 3320 | |
| 3321 | size_t numTrailingObjects(OverloadToken<DeclAccessPair>) const { |
| 3322 | return hasFoundDecl(); |
| 3323 | } |
| 3324 | |
| 3325 | size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { |
| 3326 | return hasTemplateKWAndArgsInfo(); |
| 3327 | } |
| 3328 | |
| 3329 | bool hasFoundDecl() const { return MemberExprBits.HasFoundDecl; } |
| 3330 | |
| 3331 | bool hasTemplateKWAndArgsInfo() const { |
| 3332 | return MemberExprBits.HasTemplateKWAndArgsInfo; |
| 3333 | } |
| 3334 | |
| 3335 | MemberExpr(Expr *Base, bool IsArrow, SourceLocation OperatorLoc, |
| 3336 | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
| 3337 | ValueDecl *MemberDecl, DeclAccessPair FoundDecl, |
| 3338 | const DeclarationNameInfo &NameInfo, |
| 3339 | const TemplateArgumentListInfo *TemplateArgs, QualType T, |
| 3340 | ExprValueKind VK, ExprObjectKind OK, NonOdrUseReason NOUR); |
| 3341 | MemberExpr(EmptyShell Empty) |
| 3342 | : Expr(MemberExprClass, Empty), Base(), MemberDecl() {} |
| 3343 | |
| 3344 | public: |
| 3345 | static MemberExpr *Create(const ASTContext &C, Expr *Base, bool IsArrow, |
| 3346 | SourceLocation OperatorLoc, |
| 3347 | NestedNameSpecifierLoc QualifierLoc, |
| 3348 | SourceLocation TemplateKWLoc, ValueDecl *MemberDecl, |
| 3349 | DeclAccessPair FoundDecl, |
| 3350 | DeclarationNameInfo MemberNameInfo, |
| 3351 | const TemplateArgumentListInfo *TemplateArgs, |
| 3352 | QualType T, ExprValueKind VK, ExprObjectKind OK, |
| 3353 | NonOdrUseReason NOUR); |
| 3354 | |
| 3355 | /// Create an implicit MemberExpr, with no location, qualifier, template |
| 3356 | /// arguments, and so on. Suitable only for non-static member access. |
| 3357 | static MemberExpr *CreateImplicit(const ASTContext &C, Expr *Base, |
| 3358 | bool IsArrow, ValueDecl *MemberDecl, |
| 3359 | QualType T, ExprValueKind VK, |
| 3360 | ExprObjectKind OK) { |
| 3361 | return Create(C, Base, IsArrow, OperatorLoc: SourceLocation(), QualifierLoc: NestedNameSpecifierLoc(), |
| 3362 | TemplateKWLoc: SourceLocation(), MemberDecl, |
| 3363 | FoundDecl: DeclAccessPair::make(D: MemberDecl, AS: MemberDecl->getAccess()), |
| 3364 | MemberNameInfo: DeclarationNameInfo(), TemplateArgs: nullptr, T, VK, OK, NOUR: NOUR_None); |
| 3365 | } |
| 3366 | |
| 3367 | static MemberExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier, |
| 3368 | bool HasFoundDecl, |
| 3369 | bool HasTemplateKWAndArgsInfo, |
| 3370 | unsigned NumTemplateArgs); |
| 3371 | |
| 3372 | void setBase(Expr *E) { Base = E; } |
| 3373 | Expr *getBase() const { return cast<Expr>(Val: Base); } |
| 3374 | |
| 3375 | /// Retrieve the member declaration to which this expression refers. |
| 3376 | /// |
| 3377 | /// The returned declaration will be a FieldDecl or (in C++) a VarDecl (for |
| 3378 | /// static data members), a CXXMethodDecl, or an EnumConstantDecl. |
| 3379 | ValueDecl *getMemberDecl() const { return MemberDecl; } |
| 3380 | void setMemberDecl(ValueDecl *D); |
| 3381 | |
| 3382 | /// Retrieves the declaration found by lookup. |
| 3383 | DeclAccessPair getFoundDecl() const { |
| 3384 | if (!hasFoundDecl()) |
| 3385 | return DeclAccessPair::make(D: getMemberDecl(), |
| 3386 | AS: getMemberDecl()->getAccess()); |
| 3387 | return *getTrailingObjects<DeclAccessPair>(); |
| 3388 | } |
| 3389 | |
| 3390 | /// Determines whether this member expression actually had |
| 3391 | /// a C++ nested-name-specifier prior to the name of the member, e.g., |
| 3392 | /// x->Base::foo. |
| 3393 | bool hasQualifier() const { return MemberExprBits.HasQualifier; } |
| 3394 | |
| 3395 | /// If the member name was qualified, retrieves the |
| 3396 | /// nested-name-specifier that precedes the member name, with source-location |
| 3397 | /// information. |
| 3398 | NestedNameSpecifierLoc getQualifierLoc() const { |
| 3399 | if (!hasQualifier()) |
| 3400 | return NestedNameSpecifierLoc(); |
| 3401 | return *getTrailingObjects<NestedNameSpecifierLoc>(); |
| 3402 | } |
| 3403 | |
| 3404 | /// If the member name was qualified, retrieves the |
| 3405 | /// nested-name-specifier that precedes the member name. Otherwise, returns |
| 3406 | /// NULL. |
| 3407 | NestedNameSpecifier *getQualifier() const { |
| 3408 | return getQualifierLoc().getNestedNameSpecifier(); |
| 3409 | } |
| 3410 | |
| 3411 | /// Retrieve the location of the template keyword preceding |
| 3412 | /// the member name, if any. |
| 3413 | SourceLocation getTemplateKeywordLoc() const { |
| 3414 | if (!hasTemplateKWAndArgsInfo()) |
| 3415 | return SourceLocation(); |
| 3416 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; |
| 3417 | } |
| 3418 | |
| 3419 | /// Retrieve the location of the left angle bracket starting the |
| 3420 | /// explicit template argument list following the member name, if any. |
| 3421 | SourceLocation getLAngleLoc() const { |
| 3422 | if (!hasTemplateKWAndArgsInfo()) |
| 3423 | return SourceLocation(); |
| 3424 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; |
| 3425 | } |
| 3426 | |
| 3427 | /// Retrieve the location of the right angle bracket ending the |
| 3428 | /// explicit template argument list following the member name, if any. |
| 3429 | SourceLocation getRAngleLoc() const { |
| 3430 | if (!hasTemplateKWAndArgsInfo()) |
| 3431 | return SourceLocation(); |
| 3432 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; |
| 3433 | } |
| 3434 | |
| 3435 | /// Determines whether the member name was preceded by the template keyword. |
| 3436 | bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } |
| 3437 | |
| 3438 | /// Determines whether the member name was followed by an |
| 3439 | /// explicit template argument list. |
| 3440 | bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } |
| 3441 | |
| 3442 | /// Copies the template arguments (if present) into the given |
| 3443 | /// structure. |
| 3444 | void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { |
| 3445 | if (hasExplicitTemplateArgs()) |
| 3446 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( |
| 3447 | getTrailingObjects<TemplateArgumentLoc>(), List); |
| 3448 | } |
| 3449 | |
| 3450 | /// Retrieve the template arguments provided as part of this |
| 3451 | /// template-id. |
| 3452 | const TemplateArgumentLoc *getTemplateArgs() const { |
| 3453 | if (!hasExplicitTemplateArgs()) |
| 3454 | return nullptr; |
| 3455 | |
| 3456 | return getTrailingObjects<TemplateArgumentLoc>(); |
| 3457 | } |
| 3458 | |
| 3459 | /// Retrieve the number of template arguments provided as part of this |
| 3460 | /// template-id. |
| 3461 | unsigned getNumTemplateArgs() const { |
| 3462 | if (!hasExplicitTemplateArgs()) |
| 3463 | return 0; |
| 3464 | |
| 3465 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; |
| 3466 | } |
| 3467 | |
| 3468 | ArrayRef<TemplateArgumentLoc> template_arguments() const { |
| 3469 | return {getTemplateArgs(), getNumTemplateArgs()}; |
| 3470 | } |
| 3471 | |
| 3472 | /// Retrieve the member declaration name info. |
| 3473 | DeclarationNameInfo getMemberNameInfo() const { |
| 3474 | return DeclarationNameInfo(MemberDecl->getDeclName(), |
| 3475 | MemberLoc, MemberDNLoc); |
| 3476 | } |
| 3477 | |
| 3478 | SourceLocation getOperatorLoc() const { return MemberExprBits.OperatorLoc; } |
| 3479 | |
| 3480 | bool isArrow() const { return MemberExprBits.IsArrow; } |
| 3481 | void setArrow(bool A) { MemberExprBits.IsArrow = A; } |
| 3482 | |
| 3483 | /// getMemberLoc - Return the location of the "member", in X->F, it is the |
| 3484 | /// location of 'F'. |
| 3485 | SourceLocation getMemberLoc() const { return MemberLoc; } |
| 3486 | void setMemberLoc(SourceLocation L) { MemberLoc = L; } |
| 3487 | |
| 3488 | SourceLocation getBeginLoc() const LLVM_READONLY; |
| 3489 | SourceLocation getEndLoc() const LLVM_READONLY; |
| 3490 | |
| 3491 | SourceLocation getExprLoc() const LLVM_READONLY { return MemberLoc; } |
| 3492 | |
| 3493 | /// Determine whether the base of this explicit is implicit. |
| 3494 | bool isImplicitAccess() const { |
| 3495 | return getBase() && getBase()->isImplicitCXXThis(); |
| 3496 | } |
| 3497 | |
| 3498 | /// Returns true if this member expression refers to a method that |
| 3499 | /// was resolved from an overloaded set having size greater than 1. |
| 3500 | bool hadMultipleCandidates() const { |
| 3501 | return MemberExprBits.HadMultipleCandidates; |
| 3502 | } |
| 3503 | /// Sets the flag telling whether this expression refers to |
| 3504 | /// a method that was resolved from an overloaded set having size |
| 3505 | /// greater than 1. |
| 3506 | void setHadMultipleCandidates(bool V = true) { |
| 3507 | MemberExprBits.HadMultipleCandidates = V; |
| 3508 | } |
| 3509 | |
| 3510 | /// Returns true if virtual dispatch is performed. |
| 3511 | /// If the member access is fully qualified, (i.e. X::f()), virtual |
| 3512 | /// dispatching is not performed. In -fapple-kext mode qualified |
| 3513 | /// calls to virtual method will still go through the vtable. |
| 3514 | bool performsVirtualDispatch(const LangOptions &LO) const { |
| 3515 | return LO.AppleKext || !hasQualifier(); |
| 3516 | } |
| 3517 | |
| 3518 | /// Is this expression a non-odr-use reference, and if so, why? |
| 3519 | /// This is only meaningful if the named member is a static member. |
| 3520 | NonOdrUseReason isNonOdrUse() const { |
| 3521 | return static_cast<NonOdrUseReason>(MemberExprBits.NonOdrUseReason); |
| 3522 | } |
| 3523 | |
| 3524 | static bool classof(const Stmt *T) { |
| 3525 | return T->getStmtClass() == MemberExprClass; |
| 3526 | } |
| 3527 | |
| 3528 | // Iterators |
| 3529 | child_range children() { return child_range(&Base, &Base+1); } |
| 3530 | const_child_range children() const { |
| 3531 | return const_child_range(&Base, &Base + 1); |
| 3532 | } |
| 3533 | }; |
| 3534 | |
| 3535 | /// CompoundLiteralExpr - [C99 6.5.2.5] |
| 3536 | /// |
| 3537 | class CompoundLiteralExpr : public Expr { |
| 3538 | /// LParenLoc - If non-null, this is the location of the left paren in a |
| 3539 | /// compound literal like "(int){4}". This can be null if this is a |
| 3540 | /// synthesized compound expression. |
| 3541 | SourceLocation LParenLoc; |
| 3542 | |
| 3543 | /// The type as written. This can be an incomplete array type, in |
| 3544 | /// which case the actual expression type will be different. |
| 3545 | /// The int part of the pair stores whether this expr is file scope. |
| 3546 | llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfoAndScope; |
| 3547 | Stmt *Init; |
| 3548 | public: |
| 3549 | CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, |
| 3550 | QualType T, ExprValueKind VK, Expr *init, bool fileScope) |
| 3551 | : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary), |
| 3552 | LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) { |
| 3553 | setDependence(computeDependence(E: this)); |
| 3554 | } |
| 3555 | |
| 3556 | /// Construct an empty compound literal. |
| 3557 | explicit CompoundLiteralExpr(EmptyShell Empty) |
| 3558 | : Expr(CompoundLiteralExprClass, Empty) { } |
| 3559 | |
| 3560 | const Expr *getInitializer() const { return cast<Expr>(Val: Init); } |
| 3561 | Expr *getInitializer() { return cast<Expr>(Val: Init); } |
| 3562 | void setInitializer(Expr *E) { Init = E; } |
| 3563 | |
| 3564 | bool isFileScope() const { return TInfoAndScope.getInt(); } |
| 3565 | void setFileScope(bool FS) { TInfoAndScope.setInt(FS); } |
| 3566 | |
| 3567 | SourceLocation getLParenLoc() const { return LParenLoc; } |
| 3568 | void setLParenLoc(SourceLocation L) { LParenLoc = L; } |
| 3569 | |
| 3570 | TypeSourceInfo *getTypeSourceInfo() const { |
| 3571 | return TInfoAndScope.getPointer(); |
| 3572 | } |
| 3573 | void setTypeSourceInfo(TypeSourceInfo *tinfo) { |
| 3574 | TInfoAndScope.setPointer(tinfo); |
| 3575 | } |
| 3576 | |
| 3577 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 3578 | // FIXME: Init should never be null. |
| 3579 | if (!Init) |
| 3580 | return SourceLocation(); |
| 3581 | if (LParenLoc.isInvalid()) |
| 3582 | return Init->getBeginLoc(); |
| 3583 | return LParenLoc; |
| 3584 | } |
| 3585 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 3586 | // FIXME: Init should never be null. |
| 3587 | if (!Init) |
| 3588 | return SourceLocation(); |
| 3589 | return Init->getEndLoc(); |
| 3590 | } |
| 3591 | |
| 3592 | static bool classof(const Stmt *T) { |
| 3593 | return T->getStmtClass() == CompoundLiteralExprClass; |
| 3594 | } |
| 3595 | |
| 3596 | // Iterators |
| 3597 | child_range children() { return child_range(&Init, &Init+1); } |
| 3598 | const_child_range children() const { |
| 3599 | return const_child_range(&Init, &Init + 1); |
| 3600 | } |
| 3601 | }; |
| 3602 | |
| 3603 | /// CastExpr - Base class for type casts, including both implicit |
| 3604 | /// casts (ImplicitCastExpr) and explicit casts that have some |
| 3605 | /// representation in the source code (ExplicitCastExpr's derived |
| 3606 | /// classes). |
| 3607 | class CastExpr : public Expr { |
| 3608 | Stmt *Op; |
| 3609 | |
| 3610 | bool CastConsistency() const; |
| 3611 | |
| 3612 | const CXXBaseSpecifier * const *path_buffer() const { |
| 3613 | return const_cast<CastExpr*>(this)->path_buffer(); |
| 3614 | } |
| 3615 | CXXBaseSpecifier **path_buffer(); |
| 3616 | |
| 3617 | friend class ASTStmtReader; |
| 3618 | |
| 3619 | protected: |
| 3620 | CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, const CastKind kind, |
| 3621 | Expr *op, unsigned BasePathSize, bool HasFPFeatures) |
| 3622 | : Expr(SC, ty, VK, OK_Ordinary), Op(op) { |
| 3623 | CastExprBits.Kind = kind; |
| 3624 | CastExprBits.PartOfExplicitCast = false; |
| 3625 | CastExprBits.BasePathSize = BasePathSize; |
| 3626 | assert((CastExprBits.BasePathSize == BasePathSize) && |
| 3627 | "BasePathSize overflow!" ); |
| 3628 | assert(CastConsistency()); |
| 3629 | CastExprBits.HasFPFeatures = HasFPFeatures; |
| 3630 | } |
| 3631 | |
| 3632 | /// Construct an empty cast. |
| 3633 | CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize, |
| 3634 | bool HasFPFeatures) |
| 3635 | : Expr(SC, Empty) { |
| 3636 | CastExprBits.PartOfExplicitCast = false; |
| 3637 | CastExprBits.BasePathSize = BasePathSize; |
| 3638 | CastExprBits.HasFPFeatures = HasFPFeatures; |
| 3639 | assert((CastExprBits.BasePathSize == BasePathSize) && |
| 3640 | "BasePathSize overflow!" ); |
| 3641 | } |
| 3642 | |
| 3643 | /// Return a pointer to the trailing FPOptions. |
| 3644 | /// \pre hasStoredFPFeatures() == true |
| 3645 | FPOptionsOverride *getTrailingFPFeatures(); |
| 3646 | const FPOptionsOverride *getTrailingFPFeatures() const { |
| 3647 | return const_cast<CastExpr *>(this)->getTrailingFPFeatures(); |
| 3648 | } |
| 3649 | |
| 3650 | public: |
| 3651 | CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } |
| 3652 | void setCastKind(CastKind K) { CastExprBits.Kind = K; } |
| 3653 | |
| 3654 | static const char *getCastKindName(CastKind CK); |
| 3655 | const char *getCastKindName() const { return getCastKindName(CK: getCastKind()); } |
| 3656 | |
| 3657 | Expr *getSubExpr() { return cast<Expr>(Val: Op); } |
| 3658 | const Expr *getSubExpr() const { return cast<Expr>(Val: Op); } |
| 3659 | void setSubExpr(Expr *E) { Op = E; } |
| 3660 | |
| 3661 | /// Retrieve the cast subexpression as it was written in the source |
| 3662 | /// code, looking through any implicit casts or other intermediate nodes |
| 3663 | /// introduced by semantic analysis. |
| 3664 | Expr *getSubExprAsWritten(); |
| 3665 | const Expr *getSubExprAsWritten() const { |
| 3666 | return const_cast<CastExpr *>(this)->getSubExprAsWritten(); |
| 3667 | } |
| 3668 | |
| 3669 | /// If this cast applies a user-defined conversion, retrieve the conversion |
| 3670 | /// function that it invokes. |
| 3671 | NamedDecl *getConversionFunction() const; |
| 3672 | |
| 3673 | typedef CXXBaseSpecifier **path_iterator; |
| 3674 | typedef const CXXBaseSpecifier *const *path_const_iterator; |
| 3675 | bool path_empty() const { return path_size() == 0; } |
| 3676 | unsigned path_size() const { return CastExprBits.BasePathSize; } |
| 3677 | path_iterator path_begin() { return path_buffer(); } |
| 3678 | path_iterator path_end() { return path_buffer() + path_size(); } |
| 3679 | path_const_iterator path_begin() const { return path_buffer(); } |
| 3680 | path_const_iterator path_end() const { return path_buffer() + path_size(); } |
| 3681 | |
| 3682 | /// Path through the class hierarchy taken by casts between base and derived |
| 3683 | /// classes (see implementation of `CastConsistency()` for a full list of |
| 3684 | /// cast kinds that have a path). |
| 3685 | /// |
| 3686 | /// For each derived-to-base edge in the path, the path contains a |
| 3687 | /// `CXXBaseSpecifier` for the base class of that edge; the entries are |
| 3688 | /// ordered from derived class to base class. |
| 3689 | /// |
| 3690 | /// For example, given classes `Base`, `Intermediate : public Base` and |
| 3691 | /// `Derived : public Intermediate`, the path for a cast from `Derived *` to |
| 3692 | /// `Base *` contains two entries: One for `Intermediate`, and one for `Base`, |
| 3693 | /// in that order. |
| 3694 | llvm::iterator_range<path_iterator> path() { |
| 3695 | return llvm::make_range(x: path_begin(), y: path_end()); |
| 3696 | } |
| 3697 | llvm::iterator_range<path_const_iterator> path() const { |
| 3698 | return llvm::make_range(x: path_begin(), y: path_end()); |
| 3699 | } |
| 3700 | |
| 3701 | const FieldDecl *getTargetUnionField() const { |
| 3702 | assert(getCastKind() == CK_ToUnion); |
| 3703 | return getTargetFieldForToUnionCast(getType(), getSubExpr()->getType()); |
| 3704 | } |
| 3705 | |
| 3706 | bool hasStoredFPFeatures() const { return CastExprBits.HasFPFeatures; } |
| 3707 | |
| 3708 | /// Get FPOptionsOverride from trailing storage. |
| 3709 | FPOptionsOverride getStoredFPFeatures() const { |
| 3710 | assert(hasStoredFPFeatures()); |
| 3711 | return *getTrailingFPFeatures(); |
| 3712 | } |
| 3713 | |
| 3714 | /// Get the store FPOptionsOverride or default if not stored. |
| 3715 | FPOptionsOverride getStoredFPFeaturesOrDefault() const { |
| 3716 | return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride(); |
| 3717 | } |
| 3718 | |
| 3719 | /// Get the FP features status of this operation. Only meaningful for |
| 3720 | /// operations on floating point types. |
| 3721 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { |
| 3722 | if (hasStoredFPFeatures()) |
| 3723 | return getStoredFPFeatures().applyOverrides(LO); |
| 3724 | return FPOptions::defaultWithoutTrailingStorage(LO); |
| 3725 | } |
| 3726 | |
| 3727 | FPOptionsOverride getFPFeatures() const { |
| 3728 | if (hasStoredFPFeatures()) |
| 3729 | return getStoredFPFeatures(); |
| 3730 | return FPOptionsOverride(); |
| 3731 | } |
| 3732 | |
| 3733 | /// Return |
| 3734 | // True : if this conversion changes the volatile-ness of a gl-value. |
| 3735 | // Qualification conversions on gl-values currently use CK_NoOp, but |
| 3736 | // it's important to recognize volatile-changing conversions in |
| 3737 | // clients code generation that normally eagerly peephole loads. Note |
| 3738 | // that the query is answering for this specific node; Sema may |
| 3739 | // produce multiple cast nodes for any particular conversion sequence. |
| 3740 | // False : Otherwise. |
| 3741 | bool changesVolatileQualification() const { |
| 3742 | return (isGLValue() && (getType().isVolatileQualified() != |
| 3743 | getSubExpr()->getType().isVolatileQualified())); |
| 3744 | } |
| 3745 | |
| 3746 | static const FieldDecl *getTargetFieldForToUnionCast(QualType unionType, |
| 3747 | QualType opType); |
| 3748 | static const FieldDecl *getTargetFieldForToUnionCast(const RecordDecl *RD, |
| 3749 | QualType opType); |
| 3750 | |
| 3751 | static bool classof(const Stmt *T) { |
| 3752 | return T->getStmtClass() >= firstCastExprConstant && |
| 3753 | T->getStmtClass() <= lastCastExprConstant; |
| 3754 | } |
| 3755 | |
| 3756 | // Iterators |
| 3757 | child_range children() { return child_range(&Op, &Op+1); } |
| 3758 | const_child_range children() const { return const_child_range(&Op, &Op + 1); } |
| 3759 | }; |
| 3760 | |
| 3761 | /// ImplicitCastExpr - Allows us to explicitly represent implicit type |
| 3762 | /// conversions, which have no direct representation in the original |
| 3763 | /// source code. For example: converting T[]->T*, void f()->void |
| 3764 | /// (*f)(), float->double, short->int, etc. |
| 3765 | /// |
| 3766 | /// In C, implicit casts always produce rvalues. However, in C++, an |
| 3767 | /// implicit cast whose result is being bound to a reference will be |
| 3768 | /// an lvalue or xvalue. For example: |
| 3769 | /// |
| 3770 | /// @code |
| 3771 | /// class Base { }; |
| 3772 | /// class Derived : public Base { }; |
| 3773 | /// Derived &&ref(); |
| 3774 | /// void f(Derived d) { |
| 3775 | /// Base& b = d; // initializer is an ImplicitCastExpr |
| 3776 | /// // to an lvalue of type Base |
| 3777 | /// Base&& r = ref(); // initializer is an ImplicitCastExpr |
| 3778 | /// // to an xvalue of type Base |
| 3779 | /// } |
| 3780 | /// @endcode |
| 3781 | class ImplicitCastExpr final |
| 3782 | : public CastExpr, |
| 3783 | private llvm::TrailingObjects<ImplicitCastExpr, CXXBaseSpecifier *, |
| 3784 | FPOptionsOverride> { |
| 3785 | |
| 3786 | ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, |
| 3787 | unsigned BasePathLength, FPOptionsOverride FPO, |
| 3788 | ExprValueKind VK) |
| 3789 | : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength, |
| 3790 | FPO.requiresTrailingStorage()) { |
| 3791 | setDependence(computeDependence(E: this)); |
| 3792 | if (hasStoredFPFeatures()) |
| 3793 | *getTrailingFPFeatures() = FPO; |
| 3794 | } |
| 3795 | |
| 3796 | /// Construct an empty implicit cast. |
| 3797 | explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize, |
| 3798 | bool HasFPFeatures) |
| 3799 | : CastExpr(ImplicitCastExprClass, Shell, PathSize, HasFPFeatures) {} |
| 3800 | |
| 3801 | unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const { |
| 3802 | return path_size(); |
| 3803 | } |
| 3804 | |
| 3805 | public: |
| 3806 | enum OnStack_t { OnStack }; |
| 3807 | ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, |
| 3808 | ExprValueKind VK, FPOptionsOverride FPO) |
| 3809 | : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0, |
| 3810 | FPO.requiresTrailingStorage()) { |
| 3811 | if (hasStoredFPFeatures()) |
| 3812 | *getTrailingFPFeatures() = FPO; |
| 3813 | } |
| 3814 | |
| 3815 | bool isPartOfExplicitCast() const { return CastExprBits.PartOfExplicitCast; } |
| 3816 | void setIsPartOfExplicitCast(bool PartOfExplicitCast) { |
| 3817 | CastExprBits.PartOfExplicitCast = PartOfExplicitCast; |
| 3818 | } |
| 3819 | |
| 3820 | static ImplicitCastExpr *Create(const ASTContext &Context, QualType T, |
| 3821 | CastKind Kind, Expr *Operand, |
| 3822 | const CXXCastPath *BasePath, |
| 3823 | ExprValueKind Cat, FPOptionsOverride FPO); |
| 3824 | |
| 3825 | static ImplicitCastExpr *CreateEmpty(const ASTContext &Context, |
| 3826 | unsigned PathSize, bool HasFPFeatures); |
| 3827 | |
| 3828 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 3829 | return getSubExpr()->getBeginLoc(); |
| 3830 | } |
| 3831 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 3832 | return getSubExpr()->getEndLoc(); |
| 3833 | } |
| 3834 | |
| 3835 | static bool classof(const Stmt *T) { |
| 3836 | return T->getStmtClass() == ImplicitCastExprClass; |
| 3837 | } |
| 3838 | |
| 3839 | friend TrailingObjects; |
| 3840 | friend class CastExpr; |
| 3841 | }; |
| 3842 | |
| 3843 | /// ExplicitCastExpr - An explicit cast written in the source |
| 3844 | /// code. |
| 3845 | /// |
| 3846 | /// This class is effectively an abstract class, because it provides |
| 3847 | /// the basic representation of an explicitly-written cast without |
| 3848 | /// specifying which kind of cast (C cast, functional cast, static |
| 3849 | /// cast, etc.) was written; specific derived classes represent the |
| 3850 | /// particular style of cast and its location information. |
| 3851 | /// |
| 3852 | /// Unlike implicit casts, explicit cast nodes have two different |
| 3853 | /// types: the type that was written into the source code, and the |
| 3854 | /// actual type of the expression as determined by semantic |
| 3855 | /// analysis. These types may differ slightly. For example, in C++ one |
| 3856 | /// can cast to a reference type, which indicates that the resulting |
| 3857 | /// expression will be an lvalue or xvalue. The reference type, however, |
| 3858 | /// will not be used as the type of the expression. |
| 3859 | class ExplicitCastExpr : public CastExpr { |
| 3860 | /// TInfo - Source type info for the (written) type |
| 3861 | /// this expression is casting to. |
| 3862 | TypeSourceInfo *TInfo; |
| 3863 | |
| 3864 | protected: |
| 3865 | ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, |
| 3866 | CastKind kind, Expr *op, unsigned PathSize, |
| 3867 | bool HasFPFeatures, TypeSourceInfo *writtenTy) |
| 3868 | : CastExpr(SC, exprTy, VK, kind, op, PathSize, HasFPFeatures), |
| 3869 | TInfo(writtenTy) { |
| 3870 | setDependence(computeDependence(E: this)); |
| 3871 | } |
| 3872 | |
| 3873 | /// Construct an empty explicit cast. |
| 3874 | ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize, |
| 3875 | bool HasFPFeatures) |
| 3876 | : CastExpr(SC, Shell, PathSize, HasFPFeatures) {} |
| 3877 | |
| 3878 | public: |
| 3879 | /// getTypeInfoAsWritten - Returns the type source info for the type |
| 3880 | /// that this expression is casting to. |
| 3881 | TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } |
| 3882 | void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } |
| 3883 | |
| 3884 | /// getTypeAsWritten - Returns the type that this expression is |
| 3885 | /// casting to, as written in the source code. |
| 3886 | QualType getTypeAsWritten() const { return TInfo->getType(); } |
| 3887 | |
| 3888 | static bool classof(const Stmt *T) { |
| 3889 | return T->getStmtClass() >= firstExplicitCastExprConstant && |
| 3890 | T->getStmtClass() <= lastExplicitCastExprConstant; |
| 3891 | } |
| 3892 | }; |
| 3893 | |
| 3894 | /// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style |
| 3895 | /// cast in C++ (C++ [expr.cast]), which uses the syntax |
| 3896 | /// (Type)expr. For example: @c (int)f. |
| 3897 | class CStyleCastExpr final |
| 3898 | : public ExplicitCastExpr, |
| 3899 | private llvm::TrailingObjects<CStyleCastExpr, CXXBaseSpecifier *, |
| 3900 | FPOptionsOverride> { |
| 3901 | SourceLocation LPLoc; // the location of the left paren |
| 3902 | SourceLocation RPLoc; // the location of the right paren |
| 3903 | |
| 3904 | CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, |
| 3905 | unsigned PathSize, FPOptionsOverride FPO, |
| 3906 | TypeSourceInfo *writtenTy, SourceLocation l, SourceLocation r) |
| 3907 | : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, |
| 3908 | FPO.requiresTrailingStorage(), writtenTy), |
| 3909 | LPLoc(l), RPLoc(r) { |
| 3910 | if (hasStoredFPFeatures()) |
| 3911 | *getTrailingFPFeatures() = FPO; |
| 3912 | } |
| 3913 | |
| 3914 | /// Construct an empty C-style explicit cast. |
| 3915 | explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize, |
| 3916 | bool HasFPFeatures) |
| 3917 | : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize, HasFPFeatures) {} |
| 3918 | |
| 3919 | unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const { |
| 3920 | return path_size(); |
| 3921 | } |
| 3922 | |
| 3923 | public: |
| 3924 | static CStyleCastExpr * |
| 3925 | Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind K, |
| 3926 | Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO, |
| 3927 | TypeSourceInfo *WrittenTy, SourceLocation L, SourceLocation R); |
| 3928 | |
| 3929 | static CStyleCastExpr *CreateEmpty(const ASTContext &Context, |
| 3930 | unsigned PathSize, bool HasFPFeatures); |
| 3931 | |
| 3932 | SourceLocation getLParenLoc() const { return LPLoc; } |
| 3933 | void setLParenLoc(SourceLocation L) { LPLoc = L; } |
| 3934 | |
| 3935 | SourceLocation getRParenLoc() const { return RPLoc; } |
| 3936 | void setRParenLoc(SourceLocation L) { RPLoc = L; } |
| 3937 | |
| 3938 | SourceLocation getBeginLoc() const LLVM_READONLY { return LPLoc; } |
| 3939 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 3940 | return getSubExpr()->getEndLoc(); |
| 3941 | } |
| 3942 | |
| 3943 | static bool classof(const Stmt *T) { |
| 3944 | return T->getStmtClass() == CStyleCastExprClass; |
| 3945 | } |
| 3946 | |
| 3947 | friend TrailingObjects; |
| 3948 | friend class CastExpr; |
| 3949 | }; |
| 3950 | |
| 3951 | /// A builtin binary operation expression such as "x + y" or "x <= y". |
| 3952 | /// |
| 3953 | /// This expression node kind describes a builtin binary operation, |
| 3954 | /// such as "x + y" for integer values "x" and "y". The operands will |
| 3955 | /// already have been converted to appropriate types (e.g., by |
| 3956 | /// performing promotions or conversions). |
| 3957 | /// |
| 3958 | /// In C++, where operators may be overloaded, a different kind of |
| 3959 | /// expression node (CXXOperatorCallExpr) is used to express the |
| 3960 | /// invocation of an overloaded operator with operator syntax. Within |
| 3961 | /// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is |
| 3962 | /// used to store an expression "x + y" depends on the subexpressions |
| 3963 | /// for x and y. If neither x or y is type-dependent, and the "+" |
| 3964 | /// operator resolves to a built-in operation, BinaryOperator will be |
| 3965 | /// used to express the computation (x and y may still be |
| 3966 | /// value-dependent). If either x or y is type-dependent, or if the |
| 3967 | /// "+" resolves to an overloaded operator, CXXOperatorCallExpr will |
| 3968 | /// be used to express the computation. |
| 3969 | class BinaryOperator : public Expr { |
| 3970 | enum { LHS, RHS, END_EXPR }; |
| 3971 | Stmt *SubExprs[END_EXPR]; |
| 3972 | |
| 3973 | public: |
| 3974 | typedef BinaryOperatorKind Opcode; |
| 3975 | |
| 3976 | protected: |
| 3977 | size_t offsetOfTrailingStorage() const; |
| 3978 | |
| 3979 | /// Return a pointer to the trailing FPOptions |
| 3980 | FPOptionsOverride *getTrailingFPFeatures() { |
| 3981 | assert(BinaryOperatorBits.HasFPFeatures); |
| 3982 | return reinterpret_cast<FPOptionsOverride *>( |
| 3983 | reinterpret_cast<char *>(this) + offsetOfTrailingStorage()); |
| 3984 | } |
| 3985 | const FPOptionsOverride *getTrailingFPFeatures() const { |
| 3986 | assert(BinaryOperatorBits.HasFPFeatures); |
| 3987 | return reinterpret_cast<const FPOptionsOverride *>( |
| 3988 | reinterpret_cast<const char *>(this) + offsetOfTrailingStorage()); |
| 3989 | } |
| 3990 | |
| 3991 | /// Build a binary operator, assuming that appropriate storage has been |
| 3992 | /// allocated for the trailing objects when needed. |
| 3993 | BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, Opcode opc, |
| 3994 | QualType ResTy, ExprValueKind VK, ExprObjectKind OK, |
| 3995 | SourceLocation opLoc, FPOptionsOverride FPFeatures); |
| 3996 | |
| 3997 | /// Construct an empty binary operator. |
| 3998 | explicit BinaryOperator(EmptyShell Empty) : Expr(BinaryOperatorClass, Empty) { |
| 3999 | BinaryOperatorBits.Opc = BO_Comma; |
| 4000 | BinaryOperatorBits.ExcludedOverflowPattern = false; |
| 4001 | } |
| 4002 | |
| 4003 | public: |
| 4004 | static BinaryOperator *CreateEmpty(const ASTContext &C, bool hasFPFeatures); |
| 4005 | |
| 4006 | static BinaryOperator *Create(const ASTContext &C, Expr *lhs, Expr *rhs, |
| 4007 | Opcode opc, QualType ResTy, ExprValueKind VK, |
| 4008 | ExprObjectKind OK, SourceLocation opLoc, |
| 4009 | FPOptionsOverride FPFeatures); |
| 4010 | SourceLocation getExprLoc() const { return getOperatorLoc(); } |
| 4011 | SourceLocation getOperatorLoc() const { return BinaryOperatorBits.OpLoc; } |
| 4012 | void setOperatorLoc(SourceLocation L) { BinaryOperatorBits.OpLoc = L; } |
| 4013 | |
| 4014 | Opcode getOpcode() const { |
| 4015 | return static_cast<Opcode>(BinaryOperatorBits.Opc); |
| 4016 | } |
| 4017 | void setOpcode(Opcode Opc) { BinaryOperatorBits.Opc = Opc; } |
| 4018 | |
| 4019 | Expr *getLHS() const { return cast<Expr>(Val: SubExprs[LHS]); } |
| 4020 | void setLHS(Expr *E) { SubExprs[LHS] = E; } |
| 4021 | Expr *getRHS() const { return cast<Expr>(Val: SubExprs[RHS]); } |
| 4022 | void setRHS(Expr *E) { SubExprs[RHS] = E; } |
| 4023 | |
| 4024 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 4025 | return getLHS()->getBeginLoc(); |
| 4026 | } |
| 4027 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 4028 | return getRHS()->getEndLoc(); |
| 4029 | } |
| 4030 | |
| 4031 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 4032 | /// corresponds to, e.g. "<<=". |
| 4033 | static StringRef getOpcodeStr(Opcode Op); |
| 4034 | |
| 4035 | StringRef getOpcodeStr() const { return getOpcodeStr(Op: getOpcode()); } |
| 4036 | |
| 4037 | /// Retrieve the binary opcode that corresponds to the given |
| 4038 | /// overloaded operator. |
| 4039 | static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); |
| 4040 | |
| 4041 | /// Retrieve the overloaded operator kind that corresponds to |
| 4042 | /// the given binary opcode. |
| 4043 | static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); |
| 4044 | |
| 4045 | /// predicates to categorize the respective opcodes. |
| 4046 | static bool isPtrMemOp(Opcode Opc) { |
| 4047 | return Opc == BO_PtrMemD || Opc == BO_PtrMemI; |
| 4048 | } |
| 4049 | bool isPtrMemOp() const { return isPtrMemOp(Opc: getOpcode()); } |
| 4050 | |
| 4051 | static bool isMultiplicativeOp(Opcode Opc) { |
| 4052 | return Opc >= BO_Mul && Opc <= BO_Rem; |
| 4053 | } |
| 4054 | bool isMultiplicativeOp() const { return isMultiplicativeOp(Opc: getOpcode()); } |
| 4055 | static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } |
| 4056 | bool isAdditiveOp() const { return isAdditiveOp(Opc: getOpcode()); } |
| 4057 | static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } |
| 4058 | bool isShiftOp() const { return isShiftOp(Opc: getOpcode()); } |
| 4059 | |
| 4060 | static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } |
| 4061 | bool isBitwiseOp() const { return isBitwiseOp(Opc: getOpcode()); } |
| 4062 | |
| 4063 | static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } |
| 4064 | bool isRelationalOp() const { return isRelationalOp(Opc: getOpcode()); } |
| 4065 | |
| 4066 | static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } |
| 4067 | bool isEqualityOp() const { return isEqualityOp(Opc: getOpcode()); } |
| 4068 | |
| 4069 | static bool isComparisonOp(Opcode Opc) { return Opc >= BO_Cmp && Opc<=BO_NE; } |
| 4070 | bool isComparisonOp() const { return isComparisonOp(Opc: getOpcode()); } |
| 4071 | |
| 4072 | static bool isCommaOp(Opcode Opc) { return Opc == BO_Comma; } |
| 4073 | bool isCommaOp() const { return isCommaOp(Opc: getOpcode()); } |
| 4074 | |
| 4075 | static Opcode negateComparisonOp(Opcode Opc) { |
| 4076 | switch (Opc) { |
| 4077 | default: |
| 4078 | llvm_unreachable("Not a comparison operator." ); |
| 4079 | case BO_LT: return BO_GE; |
| 4080 | case BO_GT: return BO_LE; |
| 4081 | case BO_LE: return BO_GT; |
| 4082 | case BO_GE: return BO_LT; |
| 4083 | case BO_EQ: return BO_NE; |
| 4084 | case BO_NE: return BO_EQ; |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | static Opcode reverseComparisonOp(Opcode Opc) { |
| 4089 | switch (Opc) { |
| 4090 | default: |
| 4091 | llvm_unreachable("Not a comparison operator." ); |
| 4092 | case BO_LT: return BO_GT; |
| 4093 | case BO_GT: return BO_LT; |
| 4094 | case BO_LE: return BO_GE; |
| 4095 | case BO_GE: return BO_LE; |
| 4096 | case BO_EQ: |
| 4097 | case BO_NE: |
| 4098 | return Opc; |
| 4099 | } |
| 4100 | } |
| 4101 | |
| 4102 | static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } |
| 4103 | bool isLogicalOp() const { return isLogicalOp(Opc: getOpcode()); } |
| 4104 | |
| 4105 | static bool isAssignmentOp(Opcode Opc) { |
| 4106 | return Opc >= BO_Assign && Opc <= BO_OrAssign; |
| 4107 | } |
| 4108 | bool isAssignmentOp() const { return isAssignmentOp(Opc: getOpcode()); } |
| 4109 | |
| 4110 | static bool isCompoundAssignmentOp(Opcode Opc) { |
| 4111 | return Opc > BO_Assign && Opc <= BO_OrAssign; |
| 4112 | } |
| 4113 | bool isCompoundAssignmentOp() const { |
| 4114 | return isCompoundAssignmentOp(Opc: getOpcode()); |
| 4115 | } |
| 4116 | static Opcode getOpForCompoundAssignment(Opcode Opc) { |
| 4117 | assert(isCompoundAssignmentOp(Opc)); |
| 4118 | if (Opc >= BO_AndAssign) |
| 4119 | return Opcode(unsigned(Opc) - BO_AndAssign + BO_And); |
| 4120 | else |
| 4121 | return Opcode(unsigned(Opc) - BO_MulAssign + BO_Mul); |
| 4122 | } |
| 4123 | |
| 4124 | static bool isShiftAssignOp(Opcode Opc) { |
| 4125 | return Opc == BO_ShlAssign || Opc == BO_ShrAssign; |
| 4126 | } |
| 4127 | bool isShiftAssignOp() const { |
| 4128 | return isShiftAssignOp(Opc: getOpcode()); |
| 4129 | } |
| 4130 | |
| 4131 | /// Return true if a binary operator using the specified opcode and operands |
| 4132 | /// would match the 'p = (i8*)nullptr + n' idiom for casting a pointer-sized |
| 4133 | /// integer to a pointer. |
| 4134 | static bool isNullPointerArithmeticExtension(ASTContext &Ctx, Opcode Opc, |
| 4135 | const Expr *LHS, |
| 4136 | const Expr *RHS); |
| 4137 | |
| 4138 | static bool classof(const Stmt *S) { |
| 4139 | return S->getStmtClass() >= firstBinaryOperatorConstant && |
| 4140 | S->getStmtClass() <= lastBinaryOperatorConstant; |
| 4141 | } |
| 4142 | |
| 4143 | // Iterators |
| 4144 | child_range children() { |
| 4145 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); |
| 4146 | } |
| 4147 | const_child_range children() const { |
| 4148 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 4149 | } |
| 4150 | |
| 4151 | /// Set and fetch the bit that shows whether FPFeatures needs to be |
| 4152 | /// allocated in Trailing Storage |
| 4153 | void setHasStoredFPFeatures(bool B) { BinaryOperatorBits.HasFPFeatures = B; } |
| 4154 | bool hasStoredFPFeatures() const { return BinaryOperatorBits.HasFPFeatures; } |
| 4155 | |
| 4156 | /// Set and get the bit that informs arithmetic overflow sanitizers whether |
| 4157 | /// or not they should exclude certain BinaryOperators from instrumentation |
| 4158 | void setExcludedOverflowPattern(bool B) { |
| 4159 | BinaryOperatorBits.ExcludedOverflowPattern = B; |
| 4160 | } |
| 4161 | bool hasExcludedOverflowPattern() const { |
| 4162 | return BinaryOperatorBits.ExcludedOverflowPattern; |
| 4163 | } |
| 4164 | |
| 4165 | /// Get FPFeatures from trailing storage |
| 4166 | FPOptionsOverride getStoredFPFeatures() const { |
| 4167 | assert(hasStoredFPFeatures()); |
| 4168 | return *getTrailingFPFeatures(); |
| 4169 | } |
| 4170 | /// Set FPFeatures in trailing storage, used only by Serialization |
| 4171 | void setStoredFPFeatures(FPOptionsOverride F) { |
| 4172 | assert(BinaryOperatorBits.HasFPFeatures); |
| 4173 | *getTrailingFPFeatures() = F; |
| 4174 | } |
| 4175 | /// Get the store FPOptionsOverride or default if not stored. |
| 4176 | FPOptionsOverride getStoredFPFeaturesOrDefault() const { |
| 4177 | return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride(); |
| 4178 | } |
| 4179 | |
| 4180 | /// Get the FP features status of this operator. Only meaningful for |
| 4181 | /// operations on floating point types. |
| 4182 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { |
| 4183 | if (BinaryOperatorBits.HasFPFeatures) |
| 4184 | return getStoredFPFeatures().applyOverrides(LO); |
| 4185 | return FPOptions::defaultWithoutTrailingStorage(LO); |
| 4186 | } |
| 4187 | |
| 4188 | // This is used in ASTImporter |
| 4189 | FPOptionsOverride getFPFeatures() const { |
| 4190 | if (BinaryOperatorBits.HasFPFeatures) |
| 4191 | return getStoredFPFeatures(); |
| 4192 | return FPOptionsOverride(); |
| 4193 | } |
| 4194 | |
| 4195 | /// Get the FP contractibility status of this operator. Only meaningful for |
| 4196 | /// operations on floating point types. |
| 4197 | bool isFPContractableWithinStatement(const LangOptions &LO) const { |
| 4198 | return getFPFeaturesInEffect(LO).allowFPContractWithinStatement(); |
| 4199 | } |
| 4200 | |
| 4201 | /// Get the FENV_ACCESS status of this operator. Only meaningful for |
| 4202 | /// operations on floating point types. |
| 4203 | bool isFEnvAccessOn(const LangOptions &LO) const { |
| 4204 | return getFPFeaturesInEffect(LO).getAllowFEnvAccess(); |
| 4205 | } |
| 4206 | |
| 4207 | protected: |
| 4208 | BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, Opcode opc, |
| 4209 | QualType ResTy, ExprValueKind VK, ExprObjectKind OK, |
| 4210 | SourceLocation opLoc, FPOptionsOverride FPFeatures, |
| 4211 | bool dead2); |
| 4212 | |
| 4213 | /// Construct an empty BinaryOperator, SC is CompoundAssignOperator. |
| 4214 | BinaryOperator(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) { |
| 4215 | BinaryOperatorBits.Opc = BO_MulAssign; |
| 4216 | } |
| 4217 | |
| 4218 | /// Return the size in bytes needed for the trailing objects. |
| 4219 | /// Used to allocate the right amount of storage. |
| 4220 | static unsigned sizeOfTrailingObjects(bool HasFPFeatures) { |
| 4221 | return HasFPFeatures * sizeof(FPOptionsOverride); |
| 4222 | } |
| 4223 | }; |
| 4224 | |
| 4225 | /// CompoundAssignOperator - For compound assignments (e.g. +=), we keep |
| 4226 | /// track of the type the operation is performed in. Due to the semantics of |
| 4227 | /// these operators, the operands are promoted, the arithmetic performed, an |
| 4228 | /// implicit conversion back to the result type done, then the assignment takes |
| 4229 | /// place. This captures the intermediate type which the computation is done |
| 4230 | /// in. |
| 4231 | class CompoundAssignOperator : public BinaryOperator { |
| 4232 | QualType ComputationLHSType; |
| 4233 | QualType ComputationResultType; |
| 4234 | |
| 4235 | /// Construct an empty CompoundAssignOperator. |
| 4236 | explicit CompoundAssignOperator(const ASTContext &C, EmptyShell Empty, |
| 4237 | bool hasFPFeatures) |
| 4238 | : BinaryOperator(CompoundAssignOperatorClass, Empty) {} |
| 4239 | |
| 4240 | protected: |
| 4241 | CompoundAssignOperator(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, |
| 4242 | QualType ResType, ExprValueKind VK, ExprObjectKind OK, |
| 4243 | SourceLocation OpLoc, FPOptionsOverride FPFeatures, |
| 4244 | QualType CompLHSType, QualType CompResultType) |
| 4245 | : BinaryOperator(C, lhs, rhs, opc, ResType, VK, OK, OpLoc, FPFeatures, |
| 4246 | true), |
| 4247 | ComputationLHSType(CompLHSType), ComputationResultType(CompResultType) { |
| 4248 | assert(isCompoundAssignmentOp() && |
| 4249 | "Only should be used for compound assignments" ); |
| 4250 | } |
| 4251 | |
| 4252 | public: |
| 4253 | static CompoundAssignOperator *CreateEmpty(const ASTContext &C, |
| 4254 | bool hasFPFeatures); |
| 4255 | |
| 4256 | static CompoundAssignOperator * |
| 4257 | Create(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, |
| 4258 | ExprValueKind VK, ExprObjectKind OK, SourceLocation opLoc, |
| 4259 | FPOptionsOverride FPFeatures, QualType CompLHSType = QualType(), |
| 4260 | QualType CompResultType = QualType()); |
| 4261 | |
| 4262 | // The two computation types are the type the LHS is converted |
| 4263 | // to for the computation and the type of the result; the two are |
| 4264 | // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). |
| 4265 | QualType getComputationLHSType() const { return ComputationLHSType; } |
| 4266 | void setComputationLHSType(QualType T) { ComputationLHSType = T; } |
| 4267 | |
| 4268 | QualType getComputationResultType() const { return ComputationResultType; } |
| 4269 | void setComputationResultType(QualType T) { ComputationResultType = T; } |
| 4270 | |
| 4271 | static bool classof(const Stmt *S) { |
| 4272 | return S->getStmtClass() == CompoundAssignOperatorClass; |
| 4273 | } |
| 4274 | }; |
| 4275 | |
| 4276 | inline size_t BinaryOperator::offsetOfTrailingStorage() const { |
| 4277 | assert(BinaryOperatorBits.HasFPFeatures); |
| 4278 | return isa<CompoundAssignOperator>(Val: this) ? sizeof(CompoundAssignOperator) |
| 4279 | : sizeof(BinaryOperator); |
| 4280 | } |
| 4281 | |
| 4282 | /// AbstractConditionalOperator - An abstract base class for |
| 4283 | /// ConditionalOperator and BinaryConditionalOperator. |
| 4284 | class AbstractConditionalOperator : public Expr { |
| 4285 | SourceLocation QuestionLoc, ColonLoc; |
| 4286 | friend class ASTStmtReader; |
| 4287 | |
| 4288 | protected: |
| 4289 | AbstractConditionalOperator(StmtClass SC, QualType T, ExprValueKind VK, |
| 4290 | ExprObjectKind OK, SourceLocation qloc, |
| 4291 | SourceLocation cloc) |
| 4292 | : Expr(SC, T, VK, OK), QuestionLoc(qloc), ColonLoc(cloc) {} |
| 4293 | |
| 4294 | AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) |
| 4295 | : Expr(SC, Empty) { } |
| 4296 | |
| 4297 | public: |
| 4298 | /// getCond - Return the expression representing the condition for |
| 4299 | /// the ?: operator. |
| 4300 | Expr *getCond() const; |
| 4301 | |
| 4302 | /// getTrueExpr - Return the subexpression representing the value of |
| 4303 | /// the expression if the condition evaluates to true. |
| 4304 | Expr *getTrueExpr() const; |
| 4305 | |
| 4306 | /// getFalseExpr - Return the subexpression representing the value of |
| 4307 | /// the expression if the condition evaluates to false. This is |
| 4308 | /// the same as getRHS. |
| 4309 | Expr *getFalseExpr() const; |
| 4310 | |
| 4311 | SourceLocation getQuestionLoc() const { return QuestionLoc; } |
| 4312 | SourceLocation getColonLoc() const { return ColonLoc; } |
| 4313 | |
| 4314 | static bool classof(const Stmt *T) { |
| 4315 | return T->getStmtClass() == ConditionalOperatorClass || |
| 4316 | T->getStmtClass() == BinaryConditionalOperatorClass; |
| 4317 | } |
| 4318 | }; |
| 4319 | |
| 4320 | /// ConditionalOperator - The ?: ternary operator. The GNU "missing |
| 4321 | /// middle" extension is a BinaryConditionalOperator. |
| 4322 | class ConditionalOperator : public AbstractConditionalOperator { |
| 4323 | enum { COND, LHS, RHS, END_EXPR }; |
| 4324 | Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. |
| 4325 | |
| 4326 | friend class ASTStmtReader; |
| 4327 | public: |
| 4328 | ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, |
| 4329 | SourceLocation CLoc, Expr *rhs, QualType t, |
| 4330 | ExprValueKind VK, ExprObjectKind OK) |
| 4331 | : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, QLoc, |
| 4332 | CLoc) { |
| 4333 | SubExprs[COND] = cond; |
| 4334 | SubExprs[LHS] = lhs; |
| 4335 | SubExprs[RHS] = rhs; |
| 4336 | setDependence(computeDependence(E: this)); |
| 4337 | } |
| 4338 | |
| 4339 | /// Build an empty conditional operator. |
| 4340 | explicit ConditionalOperator(EmptyShell Empty) |
| 4341 | : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } |
| 4342 | |
| 4343 | /// getCond - Return the expression representing the condition for |
| 4344 | /// the ?: operator. |
| 4345 | Expr *getCond() const { return cast<Expr>(Val: SubExprs[COND]); } |
| 4346 | |
| 4347 | /// getTrueExpr - Return the subexpression representing the value of |
| 4348 | /// the expression if the condition evaluates to true. |
| 4349 | Expr *getTrueExpr() const { return cast<Expr>(Val: SubExprs[LHS]); } |
| 4350 | |
| 4351 | /// getFalseExpr - Return the subexpression representing the value of |
| 4352 | /// the expression if the condition evaluates to false. This is |
| 4353 | /// the same as getRHS. |
| 4354 | Expr *getFalseExpr() const { return cast<Expr>(Val: SubExprs[RHS]); } |
| 4355 | |
| 4356 | Expr *getLHS() const { return cast<Expr>(Val: SubExprs[LHS]); } |
| 4357 | Expr *getRHS() const { return cast<Expr>(Val: SubExprs[RHS]); } |
| 4358 | |
| 4359 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 4360 | return getCond()->getBeginLoc(); |
| 4361 | } |
| 4362 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 4363 | return getRHS()->getEndLoc(); |
| 4364 | } |
| 4365 | |
| 4366 | static bool classof(const Stmt *T) { |
| 4367 | return T->getStmtClass() == ConditionalOperatorClass; |
| 4368 | } |
| 4369 | |
| 4370 | // Iterators |
| 4371 | child_range children() { |
| 4372 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); |
| 4373 | } |
| 4374 | const_child_range children() const { |
| 4375 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 4376 | } |
| 4377 | }; |
| 4378 | |
| 4379 | /// BinaryConditionalOperator - The GNU extension to the conditional |
| 4380 | /// operator which allows the middle operand to be omitted. |
| 4381 | /// |
| 4382 | /// This is a different expression kind on the assumption that almost |
| 4383 | /// every client ends up needing to know that these are different. |
| 4384 | class BinaryConditionalOperator : public AbstractConditionalOperator { |
| 4385 | enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; |
| 4386 | |
| 4387 | /// - the common condition/left-hand-side expression, which will be |
| 4388 | /// evaluated as the opaque value |
| 4389 | /// - the condition, expressed in terms of the opaque value |
| 4390 | /// - the left-hand-side, expressed in terms of the opaque value |
| 4391 | /// - the right-hand-side |
| 4392 | Stmt *SubExprs[NUM_SUBEXPRS]; |
| 4393 | OpaqueValueExpr *OpaqueValue; |
| 4394 | |
| 4395 | friend class ASTStmtReader; |
| 4396 | public: |
| 4397 | BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, |
| 4398 | Expr *cond, Expr *lhs, Expr *rhs, |
| 4399 | SourceLocation qloc, SourceLocation cloc, |
| 4400 | QualType t, ExprValueKind VK, ExprObjectKind OK) |
| 4401 | : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, |
| 4402 | qloc, cloc), |
| 4403 | OpaqueValue(opaqueValue) { |
| 4404 | SubExprs[COMMON] = common; |
| 4405 | SubExprs[COND] = cond; |
| 4406 | SubExprs[LHS] = lhs; |
| 4407 | SubExprs[RHS] = rhs; |
| 4408 | assert(OpaqueValue->getSourceExpr() == common && "Wrong opaque value" ); |
| 4409 | setDependence(computeDependence(E: this)); |
| 4410 | } |
| 4411 | |
| 4412 | /// Build an empty conditional operator. |
| 4413 | explicit BinaryConditionalOperator(EmptyShell Empty) |
| 4414 | : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } |
| 4415 | |
| 4416 | /// getCommon - Return the common expression, written to the |
| 4417 | /// left of the condition. The opaque value will be bound to the |
| 4418 | /// result of this expression. |
| 4419 | Expr *getCommon() const { return cast<Expr>(Val: SubExprs[COMMON]); } |
| 4420 | |
| 4421 | /// getOpaqueValue - Return the opaque value placeholder. |
| 4422 | OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } |
| 4423 | |
| 4424 | /// getCond - Return the condition expression; this is defined |
| 4425 | /// in terms of the opaque value. |
| 4426 | Expr *getCond() const { return cast<Expr>(Val: SubExprs[COND]); } |
| 4427 | |
| 4428 | /// getTrueExpr - Return the subexpression which will be |
| 4429 | /// evaluated if the condition evaluates to true; this is defined |
| 4430 | /// in terms of the opaque value. |
| 4431 | Expr *getTrueExpr() const { |
| 4432 | return cast<Expr>(Val: SubExprs[LHS]); |
| 4433 | } |
| 4434 | |
| 4435 | /// getFalseExpr - Return the subexpression which will be |
| 4436 | /// evaluated if the condition evaluates to false; this is |
| 4437 | /// defined in terms of the opaque value. |
| 4438 | Expr *getFalseExpr() const { |
| 4439 | return cast<Expr>(Val: SubExprs[RHS]); |
| 4440 | } |
| 4441 | |
| 4442 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 4443 | return getCommon()->getBeginLoc(); |
| 4444 | } |
| 4445 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 4446 | return getFalseExpr()->getEndLoc(); |
| 4447 | } |
| 4448 | |
| 4449 | static bool classof(const Stmt *T) { |
| 4450 | return T->getStmtClass() == BinaryConditionalOperatorClass; |
| 4451 | } |
| 4452 | |
| 4453 | // Iterators |
| 4454 | child_range children() { |
| 4455 | return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); |
| 4456 | } |
| 4457 | const_child_range children() const { |
| 4458 | return const_child_range(SubExprs, SubExprs + NUM_SUBEXPRS); |
| 4459 | } |
| 4460 | }; |
| 4461 | |
| 4462 | inline Expr *AbstractConditionalOperator::getCond() const { |
| 4463 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(Val: this)) |
| 4464 | return co->getCond(); |
| 4465 | return cast<BinaryConditionalOperator>(Val: this)->getCond(); |
| 4466 | } |
| 4467 | |
| 4468 | inline Expr *AbstractConditionalOperator::getTrueExpr() const { |
| 4469 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(Val: this)) |
| 4470 | return co->getTrueExpr(); |
| 4471 | return cast<BinaryConditionalOperator>(Val: this)->getTrueExpr(); |
| 4472 | } |
| 4473 | |
| 4474 | inline Expr *AbstractConditionalOperator::getFalseExpr() const { |
| 4475 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(Val: this)) |
| 4476 | return co->getFalseExpr(); |
| 4477 | return cast<BinaryConditionalOperator>(Val: this)->getFalseExpr(); |
| 4478 | } |
| 4479 | |
| 4480 | /// AddrLabelExpr - The GNU address of label extension, representing &&label. |
| 4481 | class AddrLabelExpr : public Expr { |
| 4482 | SourceLocation AmpAmpLoc, LabelLoc; |
| 4483 | LabelDecl *Label; |
| 4484 | public: |
| 4485 | AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, |
| 4486 | QualType t) |
| 4487 | : Expr(AddrLabelExprClass, t, VK_PRValue, OK_Ordinary), AmpAmpLoc(AALoc), |
| 4488 | LabelLoc(LLoc), Label(L) { |
| 4489 | setDependence(ExprDependence::None); |
| 4490 | } |
| 4491 | |
| 4492 | /// Build an empty address of a label expression. |
| 4493 | explicit AddrLabelExpr(EmptyShell Empty) |
| 4494 | : Expr(AddrLabelExprClass, Empty) { } |
| 4495 | |
| 4496 | SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } |
| 4497 | void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } |
| 4498 | SourceLocation getLabelLoc() const { return LabelLoc; } |
| 4499 | void setLabelLoc(SourceLocation L) { LabelLoc = L; } |
| 4500 | |
| 4501 | SourceLocation getBeginLoc() const LLVM_READONLY { return AmpAmpLoc; } |
| 4502 | SourceLocation getEndLoc() const LLVM_READONLY { return LabelLoc; } |
| 4503 | |
| 4504 | LabelDecl *getLabel() const { return Label; } |
| 4505 | void setLabel(LabelDecl *L) { Label = L; } |
| 4506 | |
| 4507 | static bool classof(const Stmt *T) { |
| 4508 | return T->getStmtClass() == AddrLabelExprClass; |
| 4509 | } |
| 4510 | |
| 4511 | // Iterators |
| 4512 | child_range children() { |
| 4513 | return child_range(child_iterator(), child_iterator()); |
| 4514 | } |
| 4515 | const_child_range children() const { |
| 4516 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 4517 | } |
| 4518 | }; |
| 4519 | |
| 4520 | /// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). |
| 4521 | /// The StmtExpr contains a single CompoundStmt node, which it evaluates and |
| 4522 | /// takes the value of the last subexpression. |
| 4523 | /// |
| 4524 | /// A StmtExpr is always an r-value; values "returned" out of a |
| 4525 | /// StmtExpr will be copied. |
| 4526 | class StmtExpr : public Expr { |
| 4527 | Stmt *SubStmt; |
| 4528 | SourceLocation LParenLoc, RParenLoc; |
| 4529 | public: |
| 4530 | StmtExpr(CompoundStmt *SubStmt, QualType T, SourceLocation LParenLoc, |
| 4531 | SourceLocation RParenLoc, unsigned TemplateDepth) |
| 4532 | : Expr(StmtExprClass, T, VK_PRValue, OK_Ordinary), SubStmt(SubStmt), |
| 4533 | LParenLoc(LParenLoc), RParenLoc(RParenLoc) { |
| 4534 | setDependence(computeDependence(E: this, TemplateDepth)); |
| 4535 | // FIXME: A templated statement expression should have an associated |
| 4536 | // DeclContext so that nested declarations always have a dependent context. |
| 4537 | StmtExprBits.TemplateDepth = TemplateDepth; |
| 4538 | } |
| 4539 | |
| 4540 | /// Build an empty statement expression. |
| 4541 | explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } |
| 4542 | |
| 4543 | CompoundStmt *getSubStmt() { return cast<CompoundStmt>(Val: SubStmt); } |
| 4544 | const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(Val: SubStmt); } |
| 4545 | void setSubStmt(CompoundStmt *S) { SubStmt = S; } |
| 4546 | |
| 4547 | SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; } |
| 4548 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 4549 | |
| 4550 | SourceLocation getLParenLoc() const { return LParenLoc; } |
| 4551 | void setLParenLoc(SourceLocation L) { LParenLoc = L; } |
| 4552 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4553 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 4554 | |
| 4555 | unsigned getTemplateDepth() const { return StmtExprBits.TemplateDepth; } |
| 4556 | |
| 4557 | static bool classof(const Stmt *T) { |
| 4558 | return T->getStmtClass() == StmtExprClass; |
| 4559 | } |
| 4560 | |
| 4561 | // Iterators |
| 4562 | child_range children() { return child_range(&SubStmt, &SubStmt+1); } |
| 4563 | const_child_range children() const { |
| 4564 | return const_child_range(&SubStmt, &SubStmt + 1); |
| 4565 | } |
| 4566 | }; |
| 4567 | |
| 4568 | /// ShuffleVectorExpr - clang-specific builtin-in function |
| 4569 | /// __builtin_shufflevector. |
| 4570 | /// This AST node represents a operator that does a constant |
| 4571 | /// shuffle, similar to LLVM's shufflevector instruction. It takes |
| 4572 | /// two vectors and a variable number of constant indices, |
| 4573 | /// and returns the appropriately shuffled vector. |
| 4574 | class ShuffleVectorExpr : public Expr { |
| 4575 | SourceLocation BuiltinLoc, RParenLoc; |
| 4576 | |
| 4577 | // SubExprs - the list of values passed to the __builtin_shufflevector |
| 4578 | // function. The first two are vectors, and the rest are constant |
| 4579 | // indices. The number of values in this list is always |
| 4580 | // 2+the number of indices in the vector type. |
| 4581 | Stmt **SubExprs; |
| 4582 | |
| 4583 | public: |
| 4584 | ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args, QualType Type, |
| 4585 | SourceLocation BLoc, SourceLocation RP); |
| 4586 | |
| 4587 | /// Build an empty vector-shuffle expression. |
| 4588 | explicit ShuffleVectorExpr(EmptyShell Empty) |
| 4589 | : Expr(ShuffleVectorExprClass, Empty), SubExprs(nullptr) { } |
| 4590 | |
| 4591 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 4592 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } |
| 4593 | |
| 4594 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4595 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 4596 | |
| 4597 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 4598 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 4599 | |
| 4600 | static bool classof(const Stmt *T) { |
| 4601 | return T->getStmtClass() == ShuffleVectorExprClass; |
| 4602 | } |
| 4603 | |
| 4604 | /// getNumSubExprs - Return the size of the SubExprs array. This includes the |
| 4605 | /// constant expression, the actual arguments passed in, and the function |
| 4606 | /// pointers. |
| 4607 | unsigned getNumSubExprs() const { return ShuffleVectorExprBits.NumExprs; } |
| 4608 | |
| 4609 | /// Retrieve the array of expressions. |
| 4610 | Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } |
| 4611 | |
| 4612 | /// getExpr - Return the Expr at the specified index. |
| 4613 | Expr *getExpr(unsigned Index) { |
| 4614 | assert((Index < ShuffleVectorExprBits.NumExprs) && |
| 4615 | "Arg access out of range!" ); |
| 4616 | return cast<Expr>(Val: SubExprs[Index]); |
| 4617 | } |
| 4618 | const Expr *getExpr(unsigned Index) const { |
| 4619 | assert((Index < ShuffleVectorExprBits.NumExprs) && |
| 4620 | "Arg access out of range!" ); |
| 4621 | return cast<Expr>(Val: SubExprs[Index]); |
| 4622 | } |
| 4623 | |
| 4624 | void setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs); |
| 4625 | |
| 4626 | llvm::APSInt getShuffleMaskIdx(unsigned N) const { |
| 4627 | assert((N < ShuffleVectorExprBits.NumExprs - 2) && |
| 4628 | "Shuffle idx out of range!" ); |
| 4629 | assert(isa<ConstantExpr>(getExpr(N + 2)) && |
| 4630 | "Index expression must be a ConstantExpr" ); |
| 4631 | return cast<ConstantExpr>(Val: getExpr(Index: N + 2))->getAPValueResult().getInt(); |
| 4632 | } |
| 4633 | |
| 4634 | // Iterators |
| 4635 | child_range children() { |
| 4636 | return child_range(&SubExprs[0], |
| 4637 | &SubExprs[0] + ShuffleVectorExprBits.NumExprs); |
| 4638 | } |
| 4639 | const_child_range children() const { |
| 4640 | return const_child_range(&SubExprs[0], |
| 4641 | &SubExprs[0] + ShuffleVectorExprBits.NumExprs); |
| 4642 | } |
| 4643 | }; |
| 4644 | |
| 4645 | /// ConvertVectorExpr - Clang builtin function __builtin_convertvector |
| 4646 | /// This AST node provides support for converting a vector type to another |
| 4647 | /// vector type of the same arity. |
| 4648 | class ConvertVectorExpr final |
| 4649 | : public Expr, |
| 4650 | private llvm::TrailingObjects<ConvertVectorExpr, FPOptionsOverride> { |
| 4651 | private: |
| 4652 | Stmt *SrcExpr; |
| 4653 | TypeSourceInfo *TInfo; |
| 4654 | SourceLocation BuiltinLoc, RParenLoc; |
| 4655 | |
| 4656 | friend TrailingObjects; |
| 4657 | friend class ASTReader; |
| 4658 | friend class ASTStmtReader; |
| 4659 | explicit ConvertVectorExpr(bool HasFPFeatures, EmptyShell Empty) |
| 4660 | : Expr(ConvertVectorExprClass, Empty) { |
| 4661 | ConvertVectorExprBits.HasFPFeatures = HasFPFeatures; |
| 4662 | } |
| 4663 | |
| 4664 | ConvertVectorExpr(Expr *SrcExpr, TypeSourceInfo *TI, QualType DstType, |
| 4665 | ExprValueKind VK, ExprObjectKind OK, |
| 4666 | SourceLocation BuiltinLoc, SourceLocation RParenLoc, |
| 4667 | FPOptionsOverride FPFeatures) |
| 4668 | : Expr(ConvertVectorExprClass, DstType, VK, OK), SrcExpr(SrcExpr), |
| 4669 | TInfo(TI), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) { |
| 4670 | ConvertVectorExprBits.HasFPFeatures = FPFeatures.requiresTrailingStorage(); |
| 4671 | if (hasStoredFPFeatures()) |
| 4672 | setStoredFPFeatures(FPFeatures); |
| 4673 | setDependence(computeDependence(E: this)); |
| 4674 | } |
| 4675 | |
| 4676 | size_t numTrailingObjects(OverloadToken<FPOptionsOverride>) const { |
| 4677 | return ConvertVectorExprBits.HasFPFeatures ? 1 : 0; |
| 4678 | } |
| 4679 | |
| 4680 | FPOptionsOverride &getTrailingFPFeatures() { |
| 4681 | assert(ConvertVectorExprBits.HasFPFeatures); |
| 4682 | return *getTrailingObjects(); |
| 4683 | } |
| 4684 | |
| 4685 | const FPOptionsOverride &getTrailingFPFeatures() const { |
| 4686 | assert(ConvertVectorExprBits.HasFPFeatures); |
| 4687 | return *getTrailingObjects(); |
| 4688 | } |
| 4689 | |
| 4690 | public: |
| 4691 | static ConvertVectorExpr *CreateEmpty(const ASTContext &C, |
| 4692 | bool hasFPFeatures); |
| 4693 | |
| 4694 | static ConvertVectorExpr *Create(const ASTContext &C, Expr *SrcExpr, |
| 4695 | TypeSourceInfo *TI, QualType DstType, |
| 4696 | ExprValueKind VK, ExprObjectKind OK, |
| 4697 | SourceLocation BuiltinLoc, |
| 4698 | SourceLocation RParenLoc, |
| 4699 | FPOptionsOverride FPFeatures); |
| 4700 | |
| 4701 | /// Get the FP contractibility status of this operator. Only meaningful for |
| 4702 | /// operations on floating point types. |
| 4703 | bool isFPContractableWithinStatement(const LangOptions &LO) const { |
| 4704 | return getFPFeaturesInEffect(LO).allowFPContractWithinStatement(); |
| 4705 | } |
| 4706 | |
| 4707 | /// Is FPFeatures in Trailing Storage? |
| 4708 | bool hasStoredFPFeatures() const { |
| 4709 | return ConvertVectorExprBits.HasFPFeatures; |
| 4710 | } |
| 4711 | |
| 4712 | /// Get FPFeatures from trailing storage. |
| 4713 | FPOptionsOverride getStoredFPFeatures() const { |
| 4714 | return getTrailingFPFeatures(); |
| 4715 | } |
| 4716 | |
| 4717 | /// Get the store FPOptionsOverride or default if not stored. |
| 4718 | FPOptionsOverride getStoredFPFeaturesOrDefault() const { |
| 4719 | return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride(); |
| 4720 | } |
| 4721 | |
| 4722 | /// Set FPFeatures in trailing storage, used by Serialization & ASTImporter. |
| 4723 | void setStoredFPFeatures(FPOptionsOverride F) { getTrailingFPFeatures() = F; } |
| 4724 | |
| 4725 | /// Get the FP features status of this operator. Only meaningful for |
| 4726 | /// operations on floating point types. |
| 4727 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { |
| 4728 | if (ConvertVectorExprBits.HasFPFeatures) |
| 4729 | return getStoredFPFeatures().applyOverrides(LO); |
| 4730 | return FPOptions::defaultWithoutTrailingStorage(LO); |
| 4731 | } |
| 4732 | |
| 4733 | FPOptionsOverride getFPOptionsOverride() const { |
| 4734 | if (ConvertVectorExprBits.HasFPFeatures) |
| 4735 | return getStoredFPFeatures(); |
| 4736 | return FPOptionsOverride(); |
| 4737 | } |
| 4738 | |
| 4739 | /// getSrcExpr - Return the Expr to be converted. |
| 4740 | Expr *getSrcExpr() const { return cast<Expr>(Val: SrcExpr); } |
| 4741 | |
| 4742 | /// getTypeSourceInfo - Return the destination type. |
| 4743 | TypeSourceInfo *getTypeSourceInfo() const { |
| 4744 | return TInfo; |
| 4745 | } |
| 4746 | void setTypeSourceInfo(TypeSourceInfo *ti) { |
| 4747 | TInfo = ti; |
| 4748 | } |
| 4749 | |
| 4750 | /// getBuiltinLoc - Return the location of the __builtin_convertvector token. |
| 4751 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 4752 | |
| 4753 | /// getRParenLoc - Return the location of final right parenthesis. |
| 4754 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4755 | |
| 4756 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 4757 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 4758 | |
| 4759 | static bool classof(const Stmt *T) { |
| 4760 | return T->getStmtClass() == ConvertVectorExprClass; |
| 4761 | } |
| 4762 | |
| 4763 | // Iterators |
| 4764 | child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } |
| 4765 | const_child_range children() const { |
| 4766 | return const_child_range(&SrcExpr, &SrcExpr + 1); |
| 4767 | } |
| 4768 | }; |
| 4769 | |
| 4770 | /// ChooseExpr - GNU builtin-in function __builtin_choose_expr. |
| 4771 | /// This AST node is similar to the conditional operator (?:) in C, with |
| 4772 | /// the following exceptions: |
| 4773 | /// - the test expression must be a integer constant expression. |
| 4774 | /// - the expression returned acts like the chosen subexpression in every |
| 4775 | /// visible way: the type is the same as that of the chosen subexpression, |
| 4776 | /// and all predicates (whether it's an l-value, whether it's an integer |
| 4777 | /// constant expression, etc.) return the same result as for the chosen |
| 4778 | /// sub-expression. |
| 4779 | class ChooseExpr : public Expr { |
| 4780 | enum { COND, LHS, RHS, END_EXPR }; |
| 4781 | Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. |
| 4782 | SourceLocation BuiltinLoc, RParenLoc; |
| 4783 | |
| 4784 | public: |
| 4785 | ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, |
| 4786 | ExprValueKind VK, ExprObjectKind OK, SourceLocation RP, |
| 4787 | bool condIsTrue) |
| 4788 | : Expr(ChooseExprClass, t, VK, OK), BuiltinLoc(BLoc), RParenLoc(RP) { |
| 4789 | ChooseExprBits.CondIsTrue = condIsTrue; |
| 4790 | SubExprs[COND] = cond; |
| 4791 | SubExprs[LHS] = lhs; |
| 4792 | SubExprs[RHS] = rhs; |
| 4793 | |
| 4794 | setDependence(computeDependence(E: this)); |
| 4795 | } |
| 4796 | |
| 4797 | /// Build an empty __builtin_choose_expr. |
| 4798 | explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } |
| 4799 | |
| 4800 | /// isConditionTrue - Return whether the condition is true (i.e. not |
| 4801 | /// equal to zero). |
| 4802 | bool isConditionTrue() const { |
| 4803 | assert(!isConditionDependent() && |
| 4804 | "Dependent condition isn't true or false" ); |
| 4805 | return ChooseExprBits.CondIsTrue; |
| 4806 | } |
| 4807 | void setIsConditionTrue(bool isTrue) { ChooseExprBits.CondIsTrue = isTrue; } |
| 4808 | |
| 4809 | bool isConditionDependent() const { |
| 4810 | return getCond()->isTypeDependent() || getCond()->isValueDependent(); |
| 4811 | } |
| 4812 | |
| 4813 | /// getChosenSubExpr - Return the subexpression chosen according to the |
| 4814 | /// condition. |
| 4815 | Expr *getChosenSubExpr() const { |
| 4816 | return isConditionTrue() ? getLHS() : getRHS(); |
| 4817 | } |
| 4818 | |
| 4819 | Expr *getCond() const { return cast<Expr>(Val: SubExprs[COND]); } |
| 4820 | void setCond(Expr *E) { SubExprs[COND] = E; } |
| 4821 | Expr *getLHS() const { return cast<Expr>(Val: SubExprs[LHS]); } |
| 4822 | void setLHS(Expr *E) { SubExprs[LHS] = E; } |
| 4823 | Expr *getRHS() const { return cast<Expr>(Val: SubExprs[RHS]); } |
| 4824 | void setRHS(Expr *E) { SubExprs[RHS] = E; } |
| 4825 | |
| 4826 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 4827 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } |
| 4828 | |
| 4829 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4830 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 4831 | |
| 4832 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 4833 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 4834 | |
| 4835 | static bool classof(const Stmt *T) { |
| 4836 | return T->getStmtClass() == ChooseExprClass; |
| 4837 | } |
| 4838 | |
| 4839 | // Iterators |
| 4840 | child_range children() { |
| 4841 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); |
| 4842 | } |
| 4843 | const_child_range children() const { |
| 4844 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); |
| 4845 | } |
| 4846 | }; |
| 4847 | |
| 4848 | /// GNUNullExpr - Implements the GNU __null extension, which is a name |
| 4849 | /// for a null pointer constant that has integral type (e.g., int or |
| 4850 | /// long) and is the same size and alignment as a pointer. The __null |
| 4851 | /// extension is typically only used by system headers, which define |
| 4852 | /// NULL as __null in C++ rather than using 0 (which is an integer |
| 4853 | /// that may not match the size of a pointer). |
| 4854 | class GNUNullExpr : public Expr { |
| 4855 | /// TokenLoc - The location of the __null keyword. |
| 4856 | SourceLocation TokenLoc; |
| 4857 | |
| 4858 | public: |
| 4859 | GNUNullExpr(QualType Ty, SourceLocation Loc) |
| 4860 | : Expr(GNUNullExprClass, Ty, VK_PRValue, OK_Ordinary), TokenLoc(Loc) { |
| 4861 | setDependence(ExprDependence::None); |
| 4862 | } |
| 4863 | |
| 4864 | /// Build an empty GNU __null expression. |
| 4865 | explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } |
| 4866 | |
| 4867 | /// getTokenLocation - The location of the __null token. |
| 4868 | SourceLocation getTokenLocation() const { return TokenLoc; } |
| 4869 | void setTokenLocation(SourceLocation L) { TokenLoc = L; } |
| 4870 | |
| 4871 | SourceLocation getBeginLoc() const LLVM_READONLY { return TokenLoc; } |
| 4872 | SourceLocation getEndLoc() const LLVM_READONLY { return TokenLoc; } |
| 4873 | |
| 4874 | static bool classof(const Stmt *T) { |
| 4875 | return T->getStmtClass() == GNUNullExprClass; |
| 4876 | } |
| 4877 | |
| 4878 | // Iterators |
| 4879 | child_range children() { |
| 4880 | return child_range(child_iterator(), child_iterator()); |
| 4881 | } |
| 4882 | const_child_range children() const { |
| 4883 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 4884 | } |
| 4885 | }; |
| 4886 | |
| 4887 | /// Represents a call to the builtin function \c __builtin_va_arg. |
| 4888 | class VAArgExpr : public Expr { |
| 4889 | Stmt *Val; |
| 4890 | llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfo; |
| 4891 | SourceLocation BuiltinLoc, RParenLoc; |
| 4892 | public: |
| 4893 | VAArgExpr(SourceLocation BLoc, Expr *e, TypeSourceInfo *TInfo, |
| 4894 | SourceLocation RPLoc, QualType t, bool IsMS) |
| 4895 | : Expr(VAArgExprClass, t, VK_PRValue, OK_Ordinary), Val(e), |
| 4896 | TInfo(TInfo, IsMS), BuiltinLoc(BLoc), RParenLoc(RPLoc) { |
| 4897 | setDependence(computeDependence(E: this)); |
| 4898 | } |
| 4899 | |
| 4900 | /// Create an empty __builtin_va_arg expression. |
| 4901 | explicit VAArgExpr(EmptyShell Empty) |
| 4902 | : Expr(VAArgExprClass, Empty), Val(nullptr), TInfo(nullptr, false) {} |
| 4903 | |
| 4904 | const Expr *getSubExpr() const { return cast<Expr>(Val); } |
| 4905 | Expr *getSubExpr() { return cast<Expr>(Val); } |
| 4906 | void setSubExpr(Expr *E) { Val = E; } |
| 4907 | |
| 4908 | /// Returns whether this is really a Win64 ABI va_arg expression. |
| 4909 | bool isMicrosoftABI() const { return TInfo.getInt(); } |
| 4910 | void setIsMicrosoftABI(bool IsMS) { TInfo.setInt(IsMS); } |
| 4911 | |
| 4912 | TypeSourceInfo *getWrittenTypeInfo() const { return TInfo.getPointer(); } |
| 4913 | void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo.setPointer(TI); } |
| 4914 | |
| 4915 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 4916 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } |
| 4917 | |
| 4918 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4919 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
| 4920 | |
| 4921 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 4922 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 4923 | |
| 4924 | static bool classof(const Stmt *T) { |
| 4925 | return T->getStmtClass() == VAArgExprClass; |
| 4926 | } |
| 4927 | |
| 4928 | // Iterators |
| 4929 | child_range children() { return child_range(&Val, &Val+1); } |
| 4930 | const_child_range children() const { |
| 4931 | return const_child_range(&Val, &Val + 1); |
| 4932 | } |
| 4933 | }; |
| 4934 | |
| 4935 | enum class SourceLocIdentKind { |
| 4936 | Function, |
| 4937 | FuncSig, |
| 4938 | File, |
| 4939 | FileName, |
| 4940 | Line, |
| 4941 | Column, |
| 4942 | SourceLocStruct |
| 4943 | }; |
| 4944 | |
| 4945 | /// Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), |
| 4946 | /// __builtin_FUNCTION(), __builtin_FUNCSIG(), __builtin_FILE(), |
| 4947 | /// __builtin_FILE_NAME() or __builtin_source_location(). |
| 4948 | class SourceLocExpr final : public Expr { |
| 4949 | SourceLocation BuiltinLoc, RParenLoc; |
| 4950 | DeclContext *ParentContext; |
| 4951 | |
| 4952 | public: |
| 4953 | SourceLocExpr(const ASTContext &Ctx, SourceLocIdentKind Type, |
| 4954 | QualType ResultTy, SourceLocation BLoc, |
| 4955 | SourceLocation RParenLoc, DeclContext *Context); |
| 4956 | |
| 4957 | /// Build an empty call expression. |
| 4958 | explicit SourceLocExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {} |
| 4959 | |
| 4960 | /// Return the result of evaluating this SourceLocExpr in the specified |
| 4961 | /// (and possibly null) default argument or initialization context. |
| 4962 | APValue EvaluateInContext(const ASTContext &Ctx, |
| 4963 | const Expr *DefaultExpr) const; |
| 4964 | |
| 4965 | /// Return a string representing the name of the specific builtin function. |
| 4966 | StringRef getBuiltinStr() const; |
| 4967 | |
| 4968 | SourceLocIdentKind getIdentKind() const { |
| 4969 | return static_cast<SourceLocIdentKind>(SourceLocExprBits.Kind); |
| 4970 | } |
| 4971 | |
| 4972 | bool isIntType() const { |
| 4973 | switch (getIdentKind()) { |
| 4974 | case SourceLocIdentKind::File: |
| 4975 | case SourceLocIdentKind::FileName: |
| 4976 | case SourceLocIdentKind::Function: |
| 4977 | case SourceLocIdentKind::FuncSig: |
| 4978 | case SourceLocIdentKind::SourceLocStruct: |
| 4979 | return false; |
| 4980 | case SourceLocIdentKind::Line: |
| 4981 | case SourceLocIdentKind::Column: |
| 4982 | return true; |
| 4983 | } |
| 4984 | llvm_unreachable("unknown source location expression kind" ); |
| 4985 | } |
| 4986 | |
| 4987 | /// If the SourceLocExpr has been resolved return the subexpression |
| 4988 | /// representing the resolved value. Otherwise return null. |
| 4989 | const DeclContext *getParentContext() const { return ParentContext; } |
| 4990 | DeclContext *getParentContext() { return ParentContext; } |
| 4991 | |
| 4992 | SourceLocation getLocation() const { return BuiltinLoc; } |
| 4993 | SourceLocation getBeginLoc() const { return BuiltinLoc; } |
| 4994 | SourceLocation getEndLoc() const { return RParenLoc; } |
| 4995 | |
| 4996 | child_range children() { |
| 4997 | return child_range(child_iterator(), child_iterator()); |
| 4998 | } |
| 4999 | |
| 5000 | const_child_range children() const { |
| 5001 | return const_child_range(child_iterator(), child_iterator()); |
| 5002 | } |
| 5003 | |
| 5004 | static bool classof(const Stmt *T) { |
| 5005 | return T->getStmtClass() == SourceLocExprClass; |
| 5006 | } |
| 5007 | |
| 5008 | static bool MayBeDependent(SourceLocIdentKind Kind) { |
| 5009 | switch (Kind) { |
| 5010 | case SourceLocIdentKind::Function: |
| 5011 | case SourceLocIdentKind::FuncSig: |
| 5012 | case SourceLocIdentKind::SourceLocStruct: |
| 5013 | return true; |
| 5014 | default: |
| 5015 | return false; |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | private: |
| 5020 | friend class ASTStmtReader; |
| 5021 | }; |
| 5022 | |
| 5023 | /// Stores data related to a single #embed directive. |
| 5024 | struct EmbedDataStorage { |
| 5025 | StringLiteral *BinaryData; |
| 5026 | // FileName string already includes braces, i.e. it is <files/my_file> for a |
| 5027 | // directive #embed <files/my_file>. |
| 5028 | StringRef FileName; |
| 5029 | size_t getDataElementCount() const { return BinaryData->getByteLength(); } |
| 5030 | }; |
| 5031 | |
| 5032 | /// Represents a reference to #emded data. By default, this references the whole |
| 5033 | /// range. Otherwise it represents a subrange of data imported by #embed |
| 5034 | /// directive. Needed to handle nested initializer lists with #embed directives. |
| 5035 | /// Example: |
| 5036 | /// struct S { |
| 5037 | /// int x, y; |
| 5038 | /// }; |
| 5039 | /// |
| 5040 | /// struct T { |
| 5041 | /// int x[2]; |
| 5042 | /// struct S s |
| 5043 | /// }; |
| 5044 | /// |
| 5045 | /// struct T t[] = { |
| 5046 | /// #embed "data" // data contains 10 elements; |
| 5047 | /// }; |
| 5048 | /// |
| 5049 | /// The resulting semantic form of initializer list will contain (EE stands |
| 5050 | /// for EmbedExpr): |
| 5051 | /// { {EE(first two data elements), {EE(3rd element), EE(4th element) }}, |
| 5052 | /// { {EE(5th and 6th element), {EE(7th element), EE(8th element) }}, |
| 5053 | /// { {EE(9th and 10th element), { zeroinitializer }}} |
| 5054 | /// |
| 5055 | /// EmbedExpr inside of a semantic initializer list and referencing more than |
| 5056 | /// one element can only appear for arrays of scalars. |
| 5057 | class EmbedExpr final : public Expr { |
| 5058 | SourceLocation EmbedKeywordLoc; |
| 5059 | IntegerLiteral *FakeChildNode = nullptr; |
| 5060 | const ASTContext *Ctx = nullptr; |
| 5061 | EmbedDataStorage *Data; |
| 5062 | unsigned Begin = 0; |
| 5063 | unsigned NumOfElements; |
| 5064 | |
| 5065 | public: |
| 5066 | EmbedExpr(const ASTContext &Ctx, SourceLocation Loc, EmbedDataStorage *Data, |
| 5067 | unsigned Begin, unsigned NumOfElements); |
| 5068 | explicit EmbedExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {} |
| 5069 | |
| 5070 | SourceLocation getLocation() const { return EmbedKeywordLoc; } |
| 5071 | SourceLocation getBeginLoc() const { return EmbedKeywordLoc; } |
| 5072 | SourceLocation getEndLoc() const { return EmbedKeywordLoc; } |
| 5073 | |
| 5074 | StringLiteral *getDataStringLiteral() const { return Data->BinaryData; } |
| 5075 | StringRef getFileName() const { return Data->FileName; } |
| 5076 | EmbedDataStorage *getData() const { return Data; } |
| 5077 | |
| 5078 | unsigned getStartingElementPos() const { return Begin; } |
| 5079 | size_t getDataElementCount() const { return NumOfElements; } |
| 5080 | |
| 5081 | // Allows accessing every byte of EmbedExpr data and iterating over it. |
| 5082 | // An Iterator knows the EmbedExpr that it refers to, and an offset value |
| 5083 | // within the data. |
| 5084 | // Dereferencing an Iterator results in construction of IntegerLiteral AST |
| 5085 | // node filled with byte of data of the corresponding EmbedExpr within offset |
| 5086 | // that the Iterator currently has. |
| 5087 | template <bool Const> |
| 5088 | class ChildElementIter |
| 5089 | : public llvm::iterator_facade_base< |
| 5090 | ChildElementIter<Const>, std::random_access_iterator_tag, |
| 5091 | std::conditional_t<Const, const IntegerLiteral *, |
| 5092 | IntegerLiteral *>> { |
| 5093 | friend class EmbedExpr; |
| 5094 | |
| 5095 | EmbedExpr *EExpr = nullptr; |
| 5096 | unsigned long long CurOffset = ULLONG_MAX; |
| 5097 | using BaseTy = typename ChildElementIter::iterator_facade_base; |
| 5098 | |
| 5099 | ChildElementIter(EmbedExpr *E) : EExpr(E) { |
| 5100 | if (E) |
| 5101 | CurOffset = E->getStartingElementPos(); |
| 5102 | } |
| 5103 | |
| 5104 | public: |
| 5105 | ChildElementIter() : CurOffset(ULLONG_MAX) {} |
| 5106 | typename BaseTy::reference operator*() const { |
| 5107 | assert(EExpr && CurOffset != ULLONG_MAX && |
| 5108 | "trying to dereference an invalid iterator" ); |
| 5109 | IntegerLiteral *N = EExpr->FakeChildNode; |
| 5110 | N->setValue(*EExpr->Ctx, |
| 5111 | llvm::APInt(N->getValue().getBitWidth(), |
| 5112 | EExpr->Data->BinaryData->getCodeUnit(i: CurOffset), |
| 5113 | N->getType()->isSignedIntegerType())); |
| 5114 | // We want to return a reference to the fake child node in the |
| 5115 | // EmbedExpr, not the local variable N. |
| 5116 | return const_cast<typename BaseTy::reference>(EExpr->FakeChildNode); |
| 5117 | } |
| 5118 | typename BaseTy::pointer operator->() const { return **this; } |
| 5119 | using BaseTy::operator++; |
| 5120 | ChildElementIter &operator++() { |
| 5121 | assert(EExpr && "trying to increment an invalid iterator" ); |
| 5122 | assert(CurOffset != ULLONG_MAX && |
| 5123 | "Already at the end of what we can iterate over" ); |
| 5124 | if (++CurOffset >= |
| 5125 | EExpr->getDataElementCount() + EExpr->getStartingElementPos()) { |
| 5126 | CurOffset = ULLONG_MAX; |
| 5127 | EExpr = nullptr; |
| 5128 | } |
| 5129 | return *this; |
| 5130 | } |
| 5131 | bool operator==(ChildElementIter Other) const { |
| 5132 | return (EExpr == Other.EExpr && CurOffset == Other.CurOffset); |
| 5133 | } |
| 5134 | }; // class ChildElementIter |
| 5135 | |
| 5136 | public: |
| 5137 | using fake_child_range = llvm::iterator_range<ChildElementIter<false>>; |
| 5138 | using const_fake_child_range = llvm::iterator_range<ChildElementIter<true>>; |
| 5139 | |
| 5140 | fake_child_range underlying_data_elements() { |
| 5141 | return fake_child_range(ChildElementIter<false>(this), |
| 5142 | ChildElementIter<false>()); |
| 5143 | } |
| 5144 | |
| 5145 | const_fake_child_range underlying_data_elements() const { |
| 5146 | return const_fake_child_range( |
| 5147 | ChildElementIter<true>(const_cast<EmbedExpr *>(this)), |
| 5148 | ChildElementIter<true>()); |
| 5149 | } |
| 5150 | |
| 5151 | child_range children() { |
| 5152 | return child_range(child_iterator(), child_iterator()); |
| 5153 | } |
| 5154 | |
| 5155 | const_child_range children() const { |
| 5156 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 5157 | } |
| 5158 | |
| 5159 | static bool classof(const Stmt *T) { |
| 5160 | return T->getStmtClass() == EmbedExprClass; |
| 5161 | } |
| 5162 | |
| 5163 | ChildElementIter<false> begin() { return ChildElementIter<false>(this); } |
| 5164 | |
| 5165 | ChildElementIter<true> begin() const { |
| 5166 | return ChildElementIter<true>(const_cast<EmbedExpr *>(this)); |
| 5167 | } |
| 5168 | |
| 5169 | template <typename Call, typename... Targs> |
| 5170 | bool doForEachDataElement(Call &&C, unsigned &StartingIndexInArray, |
| 5171 | Targs &&...Fargs) const { |
| 5172 | for (auto It : underlying_data_elements()) { |
| 5173 | if (!std::invoke(std::forward<Call>(C), const_cast<IntegerLiteral *>(It), |
| 5174 | StartingIndexInArray, std::forward<Targs>(Fargs)...)) |
| 5175 | return false; |
| 5176 | StartingIndexInArray++; |
| 5177 | } |
| 5178 | return true; |
| 5179 | } |
| 5180 | |
| 5181 | private: |
| 5182 | friend class ASTStmtReader; |
| 5183 | }; |
| 5184 | |
| 5185 | /// Describes an C or C++ initializer list. |
| 5186 | /// |
| 5187 | /// InitListExpr describes an initializer list, which can be used to |
| 5188 | /// initialize objects of different types, including |
| 5189 | /// struct/class/union types, arrays, and vectors. For example: |
| 5190 | /// |
| 5191 | /// @code |
| 5192 | /// struct foo x = { 1, { 2, 3 } }; |
| 5193 | /// @endcode |
| 5194 | /// |
| 5195 | /// Prior to semantic analysis, an initializer list will represent the |
| 5196 | /// initializer list as written by the user, but will have the |
| 5197 | /// placeholder type "void". This initializer list is called the |
| 5198 | /// syntactic form of the initializer, and may contain C99 designated |
| 5199 | /// initializers (represented as DesignatedInitExprs), initializations |
| 5200 | /// of subobject members without explicit braces, and so on. Clients |
| 5201 | /// interested in the original syntax of the initializer list should |
| 5202 | /// use the syntactic form of the initializer list. |
| 5203 | /// |
| 5204 | /// After semantic analysis, the initializer list will represent the |
| 5205 | /// semantic form of the initializer, where the initializations of all |
| 5206 | /// subobjects are made explicit with nested InitListExpr nodes and |
| 5207 | /// C99 designators have been eliminated by placing the designated |
| 5208 | /// initializations into the subobject they initialize. Additionally, |
| 5209 | /// any "holes" in the initialization, where no initializer has been |
| 5210 | /// specified for a particular subobject, will be replaced with |
| 5211 | /// implicitly-generated ImplicitValueInitExpr expressions that |
| 5212 | /// value-initialize the subobjects. Note, however, that the |
| 5213 | /// initializer lists may still have fewer initializers than there are |
| 5214 | /// elements to initialize within the object. |
| 5215 | /// |
| 5216 | /// After semantic analysis has completed, given an initializer list, |
| 5217 | /// method isSemanticForm() returns true if and only if this is the |
| 5218 | /// semantic form of the initializer list (note: the same AST node |
| 5219 | /// may at the same time be the syntactic form). |
| 5220 | /// Given the semantic form of the initializer list, one can retrieve |
| 5221 | /// the syntactic form of that initializer list (when different) |
| 5222 | /// using method getSyntacticForm(); the method returns null if applied |
| 5223 | /// to a initializer list which is already in syntactic form. |
| 5224 | /// Similarly, given the syntactic form (i.e., an initializer list such |
| 5225 | /// that isSemanticForm() returns false), one can retrieve the semantic |
| 5226 | /// form using method getSemanticForm(). |
| 5227 | /// Since many initializer lists have the same syntactic and semantic forms, |
| 5228 | /// getSyntacticForm() may return NULL, indicating that the current |
| 5229 | /// semantic initializer list also serves as its syntactic form. |
| 5230 | class InitListExpr : public Expr { |
| 5231 | // FIXME: Eliminate this vector in favor of ASTContext allocation |
| 5232 | typedef ASTVector<Stmt *> InitExprsTy; |
| 5233 | InitExprsTy InitExprs; |
| 5234 | SourceLocation LBraceLoc, RBraceLoc; |
| 5235 | |
| 5236 | /// The alternative form of the initializer list (if it exists). |
| 5237 | /// The int part of the pair stores whether this initializer list is |
| 5238 | /// in semantic form. If not null, the pointer points to: |
| 5239 | /// - the syntactic form, if this is in semantic form; |
| 5240 | /// - the semantic form, if this is in syntactic form. |
| 5241 | llvm::PointerIntPair<InitListExpr *, 1, bool> AltForm; |
| 5242 | |
| 5243 | /// Either: |
| 5244 | /// If this initializer list initializes an array with more elements than |
| 5245 | /// there are initializers in the list, specifies an expression to be used |
| 5246 | /// for value initialization of the rest of the elements. |
| 5247 | /// Or |
| 5248 | /// If this initializer list initializes a union, specifies which |
| 5249 | /// field within the union will be initialized. |
| 5250 | llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; |
| 5251 | |
| 5252 | public: |
| 5253 | InitListExpr(const ASTContext &C, SourceLocation lbraceloc, |
| 5254 | ArrayRef<Expr*> initExprs, SourceLocation rbraceloc); |
| 5255 | |
| 5256 | /// Build an empty initializer list. |
| 5257 | explicit InitListExpr(EmptyShell Empty) |
| 5258 | : Expr(InitListExprClass, Empty), AltForm(nullptr, true) { } |
| 5259 | |
| 5260 | unsigned getNumInits() const { return InitExprs.size(); } |
| 5261 | |
| 5262 | /// getNumInits but if the list has an EmbedExpr inside includes full length |
| 5263 | /// of embedded data. |
| 5264 | unsigned getNumInitsWithEmbedExpanded() const { |
| 5265 | unsigned Sum = InitExprs.size(); |
| 5266 | for (auto *IE : InitExprs) |
| 5267 | if (auto *EE = dyn_cast<EmbedExpr>(Val: IE)) |
| 5268 | Sum += EE->getDataElementCount() - 1; |
| 5269 | return Sum; |
| 5270 | } |
| 5271 | |
| 5272 | /// Retrieve the set of initializers. |
| 5273 | Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } |
| 5274 | |
| 5275 | /// Retrieve the set of initializers. |
| 5276 | Expr * const *getInits() const { |
| 5277 | return reinterpret_cast<Expr * const *>(InitExprs.data()); |
| 5278 | } |
| 5279 | |
| 5280 | ArrayRef<Expr *> inits() { return llvm::ArrayRef(getInits(), getNumInits()); } |
| 5281 | |
| 5282 | ArrayRef<Expr *> inits() const { |
| 5283 | return llvm::ArrayRef(getInits(), getNumInits()); |
| 5284 | } |
| 5285 | |
| 5286 | const Expr *getInit(unsigned Init) const { |
| 5287 | assert(Init < getNumInits() && "Initializer access out of range!" ); |
| 5288 | return cast_or_null<Expr>(Val: InitExprs[Init]); |
| 5289 | } |
| 5290 | |
| 5291 | Expr *getInit(unsigned Init) { |
| 5292 | assert(Init < getNumInits() && "Initializer access out of range!" ); |
| 5293 | return cast_or_null<Expr>(Val: InitExprs[Init]); |
| 5294 | } |
| 5295 | |
| 5296 | void setInit(unsigned Init, Expr *expr) { |
| 5297 | assert(Init < getNumInits() && "Initializer access out of range!" ); |
| 5298 | InitExprs[Init] = expr; |
| 5299 | |
| 5300 | if (expr) |
| 5301 | setDependence(getDependence() | expr->getDependence()); |
| 5302 | } |
| 5303 | |
| 5304 | /// Mark the semantic form of the InitListExpr as error when the semantic |
| 5305 | /// analysis fails. |
| 5306 | void markError() { |
| 5307 | assert(isSemanticForm()); |
| 5308 | setDependence(getDependence() | ExprDependence::ErrorDependent); |
| 5309 | } |
| 5310 | |
| 5311 | /// Reserve space for some number of initializers. |
| 5312 | void reserveInits(const ASTContext &C, unsigned NumInits); |
| 5313 | |
| 5314 | /// Specify the number of initializers |
| 5315 | /// |
| 5316 | /// If there are more than @p NumInits initializers, the remaining |
| 5317 | /// initializers will be destroyed. If there are fewer than @p |
| 5318 | /// NumInits initializers, NULL expressions will be added for the |
| 5319 | /// unknown initializers. |
| 5320 | void resizeInits(const ASTContext &Context, unsigned NumInits); |
| 5321 | |
| 5322 | /// Updates the initializer at index @p Init with the new |
| 5323 | /// expression @p expr, and returns the old expression at that |
| 5324 | /// location. |
| 5325 | /// |
| 5326 | /// When @p Init is out of range for this initializer list, the |
| 5327 | /// initializer list will be extended with NULL expressions to |
| 5328 | /// accommodate the new entry. |
| 5329 | Expr *updateInit(const ASTContext &C, unsigned Init, Expr *expr); |
| 5330 | |
| 5331 | /// If this initializer list initializes an array with more elements |
| 5332 | /// than there are initializers in the list, specifies an expression to be |
| 5333 | /// used for value initialization of the rest of the elements. |
| 5334 | Expr *getArrayFiller() { |
| 5335 | return dyn_cast_if_present<Expr *>(Val&: ArrayFillerOrUnionFieldInit); |
| 5336 | } |
| 5337 | const Expr *getArrayFiller() const { |
| 5338 | return const_cast<InitListExpr *>(this)->getArrayFiller(); |
| 5339 | } |
| 5340 | void setArrayFiller(Expr *filler); |
| 5341 | |
| 5342 | /// Return true if this is an array initializer and its array "filler" |
| 5343 | /// has been set. |
| 5344 | bool hasArrayFiller() const { return getArrayFiller(); } |
| 5345 | |
| 5346 | /// Determine whether this initializer list contains a designated initializer. |
| 5347 | bool hasDesignatedInit() const { |
| 5348 | return llvm::any_of( |
| 5349 | Range: *this, P: [](const Stmt *S) { return isa<DesignatedInitExpr>(Val: S); }); |
| 5350 | } |
| 5351 | |
| 5352 | /// If this initializes a union, specifies which field in the |
| 5353 | /// union to initialize. |
| 5354 | /// |
| 5355 | /// Typically, this field is the first named field within the |
| 5356 | /// union. However, a designated initializer can specify the |
| 5357 | /// initialization of a different field within the union. |
| 5358 | FieldDecl *getInitializedFieldInUnion() { |
| 5359 | return dyn_cast_if_present<FieldDecl *>(Val&: ArrayFillerOrUnionFieldInit); |
| 5360 | } |
| 5361 | const FieldDecl *getInitializedFieldInUnion() const { |
| 5362 | return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion(); |
| 5363 | } |
| 5364 | void setInitializedFieldInUnion(FieldDecl *FD) { |
| 5365 | assert((FD == nullptr |
| 5366 | || getInitializedFieldInUnion() == nullptr |
| 5367 | || getInitializedFieldInUnion() == FD) |
| 5368 | && "Only one field of a union may be initialized at a time!" ); |
| 5369 | ArrayFillerOrUnionFieldInit = FD; |
| 5370 | } |
| 5371 | |
| 5372 | // Explicit InitListExpr's originate from source code (and have valid source |
| 5373 | // locations). Implicit InitListExpr's are created by the semantic analyzer. |
| 5374 | // FIXME: This is wrong; InitListExprs created by semantic analysis have |
| 5375 | // valid source locations too! |
| 5376 | bool isExplicit() const { |
| 5377 | return LBraceLoc.isValid() && RBraceLoc.isValid(); |
| 5378 | } |
| 5379 | |
| 5380 | /// Is this an initializer for an array of characters, initialized by a string |
| 5381 | /// literal or an @encode? |
| 5382 | bool isStringLiteralInit() const; |
| 5383 | |
| 5384 | /// Is this a transparent initializer list (that is, an InitListExpr that is |
| 5385 | /// purely syntactic, and whose semantics are that of the sole contained |
| 5386 | /// initializer)? |
| 5387 | bool isTransparent() const; |
| 5388 | |
| 5389 | /// Is this the zero initializer {0} in a language which considers it |
| 5390 | /// idiomatic? |
| 5391 | bool isIdiomaticZeroInitializer(const LangOptions &LangOpts) const; |
| 5392 | |
| 5393 | SourceLocation getLBraceLoc() const { return LBraceLoc; } |
| 5394 | void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } |
| 5395 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
| 5396 | void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } |
| 5397 | |
| 5398 | bool isSemanticForm() const { return AltForm.getInt(); } |
| 5399 | InitListExpr *getSemanticForm() const { |
| 5400 | return isSemanticForm() ? nullptr : AltForm.getPointer(); |
| 5401 | } |
| 5402 | bool isSyntacticForm() const { |
| 5403 | return !AltForm.getInt() || !AltForm.getPointer(); |
| 5404 | } |
| 5405 | InitListExpr *getSyntacticForm() const { |
| 5406 | return isSemanticForm() ? AltForm.getPointer() : nullptr; |
| 5407 | } |
| 5408 | |
| 5409 | void setSyntacticForm(InitListExpr *Init) { |
| 5410 | AltForm.setPointer(Init); |
| 5411 | AltForm.setInt(true); |
| 5412 | Init->AltForm.setPointer(this); |
| 5413 | Init->AltForm.setInt(false); |
| 5414 | } |
| 5415 | |
| 5416 | bool hadArrayRangeDesignator() const { |
| 5417 | return InitListExprBits.HadArrayRangeDesignator != 0; |
| 5418 | } |
| 5419 | void sawArrayRangeDesignator(bool ARD = true) { |
| 5420 | InitListExprBits.HadArrayRangeDesignator = ARD; |
| 5421 | } |
| 5422 | |
| 5423 | SourceLocation getBeginLoc() const LLVM_READONLY; |
| 5424 | SourceLocation getEndLoc() const LLVM_READONLY; |
| 5425 | |
| 5426 | static bool classof(const Stmt *T) { |
| 5427 | return T->getStmtClass() == InitListExprClass; |
| 5428 | } |
| 5429 | |
| 5430 | // Iterators |
| 5431 | child_range children() { |
| 5432 | const_child_range CCR = const_cast<const InitListExpr *>(this)->children(); |
| 5433 | return child_range(cast_away_const(RHS: CCR.begin()), |
| 5434 | cast_away_const(RHS: CCR.end())); |
| 5435 | } |
| 5436 | |
| 5437 | const_child_range children() const { |
| 5438 | // FIXME: This does not include the array filler expression. |
| 5439 | if (InitExprs.empty()) |
| 5440 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 5441 | return const_child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); |
| 5442 | } |
| 5443 | |
| 5444 | typedef InitExprsTy::iterator iterator; |
| 5445 | typedef InitExprsTy::const_iterator const_iterator; |
| 5446 | typedef InitExprsTy::reverse_iterator reverse_iterator; |
| 5447 | typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; |
| 5448 | |
| 5449 | iterator begin() { return InitExprs.begin(); } |
| 5450 | const_iterator begin() const { return InitExprs.begin(); } |
| 5451 | iterator end() { return InitExprs.end(); } |
| 5452 | const_iterator end() const { return InitExprs.end(); } |
| 5453 | reverse_iterator rbegin() { return InitExprs.rbegin(); } |
| 5454 | const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } |
| 5455 | reverse_iterator rend() { return InitExprs.rend(); } |
| 5456 | const_reverse_iterator rend() const { return InitExprs.rend(); } |
| 5457 | |
| 5458 | friend class ASTStmtReader; |
| 5459 | friend class ASTStmtWriter; |
| 5460 | }; |
| 5461 | |
| 5462 | /// Represents a C99 designated initializer expression. |
| 5463 | /// |
| 5464 | /// A designated initializer expression (C99 6.7.8) contains one or |
| 5465 | /// more designators (which can be field designators, array |
| 5466 | /// designators, or GNU array-range designators) followed by an |
| 5467 | /// expression that initializes the field or element(s) that the |
| 5468 | /// designators refer to. For example, given: |
| 5469 | /// |
| 5470 | /// @code |
| 5471 | /// struct point { |
| 5472 | /// double x; |
| 5473 | /// double y; |
| 5474 | /// }; |
| 5475 | /// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; |
| 5476 | /// @endcode |
| 5477 | /// |
| 5478 | /// The InitListExpr contains three DesignatedInitExprs, the first of |
| 5479 | /// which covers @c [2].y=1.0. This DesignatedInitExpr will have two |
| 5480 | /// designators, one array designator for @c [2] followed by one field |
| 5481 | /// designator for @c .y. The initialization expression will be 1.0. |
| 5482 | class DesignatedInitExpr final |
| 5483 | : public Expr, |
| 5484 | private llvm::TrailingObjects<DesignatedInitExpr, Stmt *> { |
| 5485 | public: |
| 5486 | /// Forward declaration of the Designator class. |
| 5487 | class Designator; |
| 5488 | |
| 5489 | private: |
| 5490 | /// The location of the '=' or ':' prior to the actual initializer |
| 5491 | /// expression. |
| 5492 | SourceLocation EqualOrColonLoc; |
| 5493 | |
| 5494 | /// Whether this designated initializer used the GNU deprecated |
| 5495 | /// syntax rather than the C99 '=' syntax. |
| 5496 | LLVM_PREFERRED_TYPE(bool) |
| 5497 | unsigned GNUSyntax : 1; |
| 5498 | |
| 5499 | /// The number of designators in this initializer expression. |
| 5500 | unsigned NumDesignators : 15; |
| 5501 | |
| 5502 | /// The number of subexpressions of this initializer expression, |
| 5503 | /// which contains both the initializer and any additional |
| 5504 | /// expressions used by array and array-range designators. |
| 5505 | unsigned NumSubExprs : 16; |
| 5506 | |
| 5507 | /// The designators in this designated initialization |
| 5508 | /// expression. |
| 5509 | Designator *Designators; |
| 5510 | |
| 5511 | DesignatedInitExpr(const ASTContext &C, QualType Ty, |
| 5512 | llvm::ArrayRef<Designator> Designators, |
| 5513 | SourceLocation EqualOrColonLoc, bool GNUSyntax, |
| 5514 | ArrayRef<Expr *> IndexExprs, Expr *Init); |
| 5515 | |
| 5516 | explicit DesignatedInitExpr(unsigned NumSubExprs) |
| 5517 | : Expr(DesignatedInitExprClass, EmptyShell()), |
| 5518 | NumDesignators(0), NumSubExprs(NumSubExprs), Designators(nullptr) { } |
| 5519 | |
| 5520 | public: |
| 5521 | /// Represents a single C99 designator. |
| 5522 | /// |
| 5523 | /// @todo This class is infuriatingly similar to clang::Designator, |
| 5524 | /// but minor differences (storing indices vs. storing pointers) |
| 5525 | /// keep us from reusing it. Try harder, later, to rectify these |
| 5526 | /// differences. |
| 5527 | class Designator { |
| 5528 | /// A field designator, e.g., ".x". |
| 5529 | struct FieldDesignatorInfo { |
| 5530 | /// Refers to the field that is being initialized. The low bit |
| 5531 | /// of this field determines whether this is actually a pointer |
| 5532 | /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When |
| 5533 | /// initially constructed, a field designator will store an |
| 5534 | /// IdentifierInfo*. After semantic analysis has resolved that |
| 5535 | /// name, the field designator will instead store a FieldDecl*. |
| 5536 | uintptr_t NameOrField; |
| 5537 | |
| 5538 | /// The location of the '.' in the designated initializer. |
| 5539 | SourceLocation DotLoc; |
| 5540 | |
| 5541 | /// The location of the field name in the designated initializer. |
| 5542 | SourceLocation FieldLoc; |
| 5543 | |
| 5544 | FieldDesignatorInfo(const IdentifierInfo *II, SourceLocation DotLoc, |
| 5545 | SourceLocation FieldLoc) |
| 5546 | : NameOrField(reinterpret_cast<uintptr_t>(II) | 0x1), DotLoc(DotLoc), |
| 5547 | FieldLoc(FieldLoc) {} |
| 5548 | }; |
| 5549 | |
| 5550 | /// An array or GNU array-range designator, e.g., "[9]" or "[10...15]". |
| 5551 | struct ArrayOrRangeDesignatorInfo { |
| 5552 | /// Location of the first index expression within the designated |
| 5553 | /// initializer expression's list of subexpressions. |
| 5554 | unsigned Index; |
| 5555 | |
| 5556 | /// The location of the '[' starting the array range designator. |
| 5557 | SourceLocation LBracketLoc; |
| 5558 | |
| 5559 | /// The location of the ellipsis separating the start and end |
| 5560 | /// indices. Only valid for GNU array-range designators. |
| 5561 | SourceLocation EllipsisLoc; |
| 5562 | |
| 5563 | /// The location of the ']' terminating the array range designator. |
| 5564 | SourceLocation RBracketLoc; |
| 5565 | |
| 5566 | ArrayOrRangeDesignatorInfo(unsigned Index, SourceLocation LBracketLoc, |
| 5567 | SourceLocation RBracketLoc) |
| 5568 | : Index(Index), LBracketLoc(LBracketLoc), RBracketLoc(RBracketLoc) {} |
| 5569 | |
| 5570 | ArrayOrRangeDesignatorInfo(unsigned Index, |
| 5571 | SourceLocation LBracketLoc, |
| 5572 | SourceLocation EllipsisLoc, |
| 5573 | SourceLocation RBracketLoc) |
| 5574 | : Index(Index), LBracketLoc(LBracketLoc), EllipsisLoc(EllipsisLoc), |
| 5575 | RBracketLoc(RBracketLoc) {} |
| 5576 | }; |
| 5577 | |
| 5578 | /// The kind of designator this describes. |
| 5579 | enum DesignatorKind { |
| 5580 | FieldDesignator, |
| 5581 | ArrayDesignator, |
| 5582 | ArrayRangeDesignator |
| 5583 | }; |
| 5584 | |
| 5585 | DesignatorKind Kind; |
| 5586 | |
| 5587 | union { |
| 5588 | /// A field designator, e.g., ".x". |
| 5589 | struct FieldDesignatorInfo FieldInfo; |
| 5590 | |
| 5591 | /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". |
| 5592 | struct ArrayOrRangeDesignatorInfo ArrayOrRangeInfo; |
| 5593 | }; |
| 5594 | |
| 5595 | Designator(DesignatorKind Kind) : Kind(Kind) {} |
| 5596 | |
| 5597 | public: |
| 5598 | Designator() {} |
| 5599 | |
| 5600 | bool isFieldDesignator() const { return Kind == FieldDesignator; } |
| 5601 | bool isArrayDesignator() const { return Kind == ArrayDesignator; } |
| 5602 | bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } |
| 5603 | |
| 5604 | //===------------------------------------------------------------------===// |
| 5605 | // FieldDesignatorInfo |
| 5606 | |
| 5607 | /// Creates a field designator. |
| 5608 | static Designator CreateFieldDesignator(const IdentifierInfo *FieldName, |
| 5609 | SourceLocation DotLoc, |
| 5610 | SourceLocation FieldLoc) { |
| 5611 | Designator D(FieldDesignator); |
| 5612 | new (&D.FieldInfo) FieldDesignatorInfo(FieldName, DotLoc, FieldLoc); |
| 5613 | return D; |
| 5614 | } |
| 5615 | |
| 5616 | const IdentifierInfo *getFieldName() const; |
| 5617 | |
| 5618 | FieldDecl *getFieldDecl() const { |
| 5619 | assert(isFieldDesignator() && "Only valid on a field designator" ); |
| 5620 | if (FieldInfo.NameOrField & 0x01) |
| 5621 | return nullptr; |
| 5622 | return reinterpret_cast<FieldDecl *>(FieldInfo.NameOrField); |
| 5623 | } |
| 5624 | |
| 5625 | void setFieldDecl(FieldDecl *FD) { |
| 5626 | assert(isFieldDesignator() && "Only valid on a field designator" ); |
| 5627 | FieldInfo.NameOrField = reinterpret_cast<uintptr_t>(FD); |
| 5628 | } |
| 5629 | |
| 5630 | SourceLocation getDotLoc() const { |
| 5631 | assert(isFieldDesignator() && "Only valid on a field designator" ); |
| 5632 | return FieldInfo.DotLoc; |
| 5633 | } |
| 5634 | |
| 5635 | SourceLocation getFieldLoc() const { |
| 5636 | assert(isFieldDesignator() && "Only valid on a field designator" ); |
| 5637 | return FieldInfo.FieldLoc; |
| 5638 | } |
| 5639 | |
| 5640 | //===------------------------------------------------------------------===// |
| 5641 | // ArrayOrRangeDesignator |
| 5642 | |
| 5643 | /// Creates an array designator. |
| 5644 | static Designator CreateArrayDesignator(unsigned Index, |
| 5645 | SourceLocation LBracketLoc, |
| 5646 | SourceLocation RBracketLoc) { |
| 5647 | Designator D(ArrayDesignator); |
| 5648 | new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, |
| 5649 | RBracketLoc); |
| 5650 | return D; |
| 5651 | } |
| 5652 | |
| 5653 | /// Creates a GNU array-range designator. |
| 5654 | static Designator CreateArrayRangeDesignator(unsigned Index, |
| 5655 | SourceLocation LBracketLoc, |
| 5656 | SourceLocation EllipsisLoc, |
| 5657 | SourceLocation RBracketLoc) { |
| 5658 | Designator D(ArrayRangeDesignator); |
| 5659 | new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, |
| 5660 | EllipsisLoc, |
| 5661 | RBracketLoc); |
| 5662 | return D; |
| 5663 | } |
| 5664 | |
| 5665 | unsigned getArrayIndex() const { |
| 5666 | assert((isArrayDesignator() || isArrayRangeDesignator()) && |
| 5667 | "Only valid on an array or array-range designator" ); |
| 5668 | return ArrayOrRangeInfo.Index; |
| 5669 | } |
| 5670 | |
| 5671 | SourceLocation getLBracketLoc() const { |
| 5672 | assert((isArrayDesignator() || isArrayRangeDesignator()) && |
| 5673 | "Only valid on an array or array-range designator" ); |
| 5674 | return ArrayOrRangeInfo.LBracketLoc; |
| 5675 | } |
| 5676 | |
| 5677 | SourceLocation getEllipsisLoc() const { |
| 5678 | assert(isArrayRangeDesignator() && |
| 5679 | "Only valid on an array-range designator" ); |
| 5680 | return ArrayOrRangeInfo.EllipsisLoc; |
| 5681 | } |
| 5682 | |
| 5683 | SourceLocation getRBracketLoc() const { |
| 5684 | assert((isArrayDesignator() || isArrayRangeDesignator()) && |
| 5685 | "Only valid on an array or array-range designator" ); |
| 5686 | return ArrayOrRangeInfo.RBracketLoc; |
| 5687 | } |
| 5688 | |
| 5689 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 5690 | if (isFieldDesignator()) |
| 5691 | return getDotLoc().isInvalid() ? getFieldLoc() : getDotLoc(); |
| 5692 | return getLBracketLoc(); |
| 5693 | } |
| 5694 | |
| 5695 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 5696 | return isFieldDesignator() ? getFieldLoc() : getRBracketLoc(); |
| 5697 | } |
| 5698 | |
| 5699 | SourceRange getSourceRange() const LLVM_READONLY { |
| 5700 | return SourceRange(getBeginLoc(), getEndLoc()); |
| 5701 | } |
| 5702 | }; |
| 5703 | |
| 5704 | static DesignatedInitExpr *Create(const ASTContext &C, |
| 5705 | llvm::ArrayRef<Designator> Designators, |
| 5706 | ArrayRef<Expr*> IndexExprs, |
| 5707 | SourceLocation EqualOrColonLoc, |
| 5708 | bool GNUSyntax, Expr *Init); |
| 5709 | |
| 5710 | static DesignatedInitExpr *CreateEmpty(const ASTContext &C, |
| 5711 | unsigned NumIndexExprs); |
| 5712 | |
| 5713 | /// Returns the number of designators in this initializer. |
| 5714 | unsigned size() const { return NumDesignators; } |
| 5715 | |
| 5716 | // Iterator access to the designators. |
| 5717 | llvm::MutableArrayRef<Designator> designators() { |
| 5718 | return {Designators, NumDesignators}; |
| 5719 | } |
| 5720 | |
| 5721 | llvm::ArrayRef<Designator> designators() const { |
| 5722 | return {Designators, NumDesignators}; |
| 5723 | } |
| 5724 | |
| 5725 | Designator *getDesignator(unsigned Idx) { return &designators()[Idx]; } |
| 5726 | const Designator *getDesignator(unsigned Idx) const { |
| 5727 | return &designators()[Idx]; |
| 5728 | } |
| 5729 | |
| 5730 | void setDesignators(const ASTContext &C, const Designator *Desigs, |
| 5731 | unsigned NumDesigs); |
| 5732 | |
| 5733 | Expr *getArrayIndex(const Designator &D) const; |
| 5734 | Expr *getArrayRangeStart(const Designator &D) const; |
| 5735 | Expr *getArrayRangeEnd(const Designator &D) const; |
| 5736 | |
| 5737 | /// Retrieve the location of the '=' that precedes the |
| 5738 | /// initializer value itself, if present. |
| 5739 | SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } |
| 5740 | void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } |
| 5741 | |
| 5742 | /// Whether this designated initializer should result in direct-initialization |
| 5743 | /// of the designated subobject (eg, '{.foo{1, 2, 3}}'). |
| 5744 | bool isDirectInit() const { return EqualOrColonLoc.isInvalid(); } |
| 5745 | |
| 5746 | /// Determines whether this designated initializer used the |
| 5747 | /// deprecated GNU syntax for designated initializers. |
| 5748 | bool usesGNUSyntax() const { return GNUSyntax; } |
| 5749 | void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } |
| 5750 | |
| 5751 | /// Retrieve the initializer value. |
| 5752 | Expr *getInit() const { |
| 5753 | return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); |
| 5754 | } |
| 5755 | |
| 5756 | void setInit(Expr *init) { |
| 5757 | *child_begin() = init; |
| 5758 | } |
| 5759 | |
| 5760 | /// Retrieve the total number of subexpressions in this |
| 5761 | /// designated initializer expression, including the actual |
| 5762 | /// initialized value and any expressions that occur within array |
| 5763 | /// and array-range designators. |
| 5764 | unsigned getNumSubExprs() const { return NumSubExprs; } |
| 5765 | |
| 5766 | Expr *getSubExpr(unsigned Idx) const { |
| 5767 | return cast<Expr>(getTrailingObjects(NumSubExprs)[Idx]); |
| 5768 | } |
| 5769 | |
| 5770 | void setSubExpr(unsigned Idx, Expr *E) { |
| 5771 | getTrailingObjects(NumSubExprs)[Idx] = E; |
| 5772 | } |
| 5773 | |
| 5774 | /// Replaces the designator at index @p Idx with the series |
| 5775 | /// of designators in [First, Last). |
| 5776 | void ExpandDesignator(const ASTContext &C, unsigned Idx, |
| 5777 | const Designator *First, const Designator *Last); |
| 5778 | |
| 5779 | SourceRange () const; |
| 5780 | |
| 5781 | SourceLocation getBeginLoc() const LLVM_READONLY; |
| 5782 | SourceLocation getEndLoc() const LLVM_READONLY; |
| 5783 | |
| 5784 | static bool classof(const Stmt *T) { |
| 5785 | return T->getStmtClass() == DesignatedInitExprClass; |
| 5786 | } |
| 5787 | |
| 5788 | // Iterators |
| 5789 | child_range children() { |
| 5790 | Stmt **begin = getTrailingObjects(); |
| 5791 | return child_range(begin, begin + NumSubExprs); |
| 5792 | } |
| 5793 | const_child_range children() const { |
| 5794 | Stmt *const *begin = getTrailingObjects(); |
| 5795 | return const_child_range(begin, begin + NumSubExprs); |
| 5796 | } |
| 5797 | |
| 5798 | friend TrailingObjects; |
| 5799 | }; |
| 5800 | |
| 5801 | /// Represents a place-holder for an object not to be initialized by |
| 5802 | /// anything. |
| 5803 | /// |
| 5804 | /// This only makes sense when it appears as part of an updater of a |
| 5805 | /// DesignatedInitUpdateExpr (see below). The base expression of a DIUE |
| 5806 | /// initializes a big object, and the NoInitExpr's mark the spots within the |
| 5807 | /// big object not to be overwritten by the updater. |
| 5808 | /// |
| 5809 | /// \see DesignatedInitUpdateExpr |
| 5810 | class NoInitExpr : public Expr { |
| 5811 | public: |
| 5812 | explicit NoInitExpr(QualType ty) |
| 5813 | : Expr(NoInitExprClass, ty, VK_PRValue, OK_Ordinary) { |
| 5814 | setDependence(computeDependence(E: this)); |
| 5815 | } |
| 5816 | |
| 5817 | explicit NoInitExpr(EmptyShell Empty) |
| 5818 | : Expr(NoInitExprClass, Empty) { } |
| 5819 | |
| 5820 | static bool classof(const Stmt *T) { |
| 5821 | return T->getStmtClass() == NoInitExprClass; |
| 5822 | } |
| 5823 | |
| 5824 | SourceLocation getBeginLoc() const LLVM_READONLY { return SourceLocation(); } |
| 5825 | SourceLocation getEndLoc() const LLVM_READONLY { return SourceLocation(); } |
| 5826 | |
| 5827 | // Iterators |
| 5828 | child_range children() { |
| 5829 | return child_range(child_iterator(), child_iterator()); |
| 5830 | } |
| 5831 | const_child_range children() const { |
| 5832 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 5833 | } |
| 5834 | }; |
| 5835 | |
| 5836 | // In cases like: |
| 5837 | // struct Q { int a, b, c; }; |
| 5838 | // Q *getQ(); |
| 5839 | // void foo() { |
| 5840 | // struct A { Q q; } a = { *getQ(), .q.b = 3 }; |
| 5841 | // } |
| 5842 | // |
| 5843 | // We will have an InitListExpr for a, with type A, and then a |
| 5844 | // DesignatedInitUpdateExpr for "a.q" with type Q. The "base" for this DIUE |
| 5845 | // is the call expression *getQ(); the "updater" for the DIUE is ".q.b = 3" |
| 5846 | // |
| 5847 | class DesignatedInitUpdateExpr : public Expr { |
| 5848 | // BaseAndUpdaterExprs[0] is the base expression; |
| 5849 | // BaseAndUpdaterExprs[1] is an InitListExpr overwriting part of the base. |
| 5850 | Stmt *BaseAndUpdaterExprs[2]; |
| 5851 | |
| 5852 | public: |
| 5853 | DesignatedInitUpdateExpr(const ASTContext &C, SourceLocation lBraceLoc, |
| 5854 | Expr *baseExprs, SourceLocation rBraceLoc); |
| 5855 | |
| 5856 | explicit DesignatedInitUpdateExpr(EmptyShell Empty) |
| 5857 | : Expr(DesignatedInitUpdateExprClass, Empty) { } |
| 5858 | |
| 5859 | SourceLocation getBeginLoc() const LLVM_READONLY; |
| 5860 | SourceLocation getEndLoc() const LLVM_READONLY; |
| 5861 | |
| 5862 | static bool classof(const Stmt *T) { |
| 5863 | return T->getStmtClass() == DesignatedInitUpdateExprClass; |
| 5864 | } |
| 5865 | |
| 5866 | Expr *getBase() const { return cast<Expr>(Val: BaseAndUpdaterExprs[0]); } |
| 5867 | void setBase(Expr *Base) { BaseAndUpdaterExprs[0] = Base; } |
| 5868 | |
| 5869 | InitListExpr *getUpdater() const { |
| 5870 | return cast<InitListExpr>(Val: BaseAndUpdaterExprs[1]); |
| 5871 | } |
| 5872 | void setUpdater(Expr *Updater) { BaseAndUpdaterExprs[1] = Updater; } |
| 5873 | |
| 5874 | // Iterators |
| 5875 | // children = the base and the updater |
| 5876 | child_range children() { |
| 5877 | return child_range(&BaseAndUpdaterExprs[0], &BaseAndUpdaterExprs[0] + 2); |
| 5878 | } |
| 5879 | const_child_range children() const { |
| 5880 | return const_child_range(&BaseAndUpdaterExprs[0], |
| 5881 | &BaseAndUpdaterExprs[0] + 2); |
| 5882 | } |
| 5883 | }; |
| 5884 | |
| 5885 | /// Represents a loop initializing the elements of an array. |
| 5886 | /// |
| 5887 | /// The need to initialize the elements of an array occurs in a number of |
| 5888 | /// contexts: |
| 5889 | /// |
| 5890 | /// * in the implicit copy/move constructor for a class with an array member |
| 5891 | /// * when a lambda-expression captures an array by value |
| 5892 | /// * when a decomposition declaration decomposes an array |
| 5893 | /// |
| 5894 | /// There are two subexpressions: a common expression (the source array) |
| 5895 | /// that is evaluated once up-front, and a per-element initializer that |
| 5896 | /// runs once for each array element. |
| 5897 | /// |
| 5898 | /// Within the per-element initializer, the common expression may be referenced |
| 5899 | /// via an OpaqueValueExpr, and the current index may be obtained via an |
| 5900 | /// ArrayInitIndexExpr. |
| 5901 | class ArrayInitLoopExpr : public Expr { |
| 5902 | Stmt *SubExprs[2]; |
| 5903 | |
| 5904 | explicit ArrayInitLoopExpr(EmptyShell Empty) |
| 5905 | : Expr(ArrayInitLoopExprClass, Empty), SubExprs{} {} |
| 5906 | |
| 5907 | public: |
| 5908 | explicit ArrayInitLoopExpr(QualType T, Expr *CommonInit, Expr *ElementInit) |
| 5909 | : Expr(ArrayInitLoopExprClass, T, VK_PRValue, OK_Ordinary), |
| 5910 | SubExprs{CommonInit, ElementInit} { |
| 5911 | setDependence(computeDependence(E: this)); |
| 5912 | } |
| 5913 | |
| 5914 | /// Get the common subexpression shared by all initializations (the source |
| 5915 | /// array). |
| 5916 | OpaqueValueExpr *getCommonExpr() const { |
| 5917 | return cast<OpaqueValueExpr>(Val: SubExprs[0]); |
| 5918 | } |
| 5919 | |
| 5920 | /// Get the initializer to use for each array element. |
| 5921 | Expr *getSubExpr() const { return cast<Expr>(Val: SubExprs[1]); } |
| 5922 | |
| 5923 | llvm::APInt getArraySize() const { |
| 5924 | return cast<ConstantArrayType>(getType()->castAsArrayTypeUnsafe()) |
| 5925 | ->getSize(); |
| 5926 | } |
| 5927 | |
| 5928 | static bool classof(const Stmt *S) { |
| 5929 | return S->getStmtClass() == ArrayInitLoopExprClass; |
| 5930 | } |
| 5931 | |
| 5932 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 5933 | return getCommonExpr()->getBeginLoc(); |
| 5934 | } |
| 5935 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 5936 | return getCommonExpr()->getEndLoc(); |
| 5937 | } |
| 5938 | |
| 5939 | child_range children() { |
| 5940 | return child_range(SubExprs, SubExprs + 2); |
| 5941 | } |
| 5942 | const_child_range children() const { |
| 5943 | return const_child_range(SubExprs, SubExprs + 2); |
| 5944 | } |
| 5945 | |
| 5946 | friend class ASTReader; |
| 5947 | friend class ASTStmtReader; |
| 5948 | friend class ASTStmtWriter; |
| 5949 | }; |
| 5950 | |
| 5951 | /// Represents the index of the current element of an array being |
| 5952 | /// initialized by an ArrayInitLoopExpr. This can only appear within the |
| 5953 | /// subexpression of an ArrayInitLoopExpr. |
| 5954 | class ArrayInitIndexExpr : public Expr { |
| 5955 | explicit ArrayInitIndexExpr(EmptyShell Empty) |
| 5956 | : Expr(ArrayInitIndexExprClass, Empty) {} |
| 5957 | |
| 5958 | public: |
| 5959 | explicit ArrayInitIndexExpr(QualType T) |
| 5960 | : Expr(ArrayInitIndexExprClass, T, VK_PRValue, OK_Ordinary) { |
| 5961 | setDependence(ExprDependence::None); |
| 5962 | } |
| 5963 | |
| 5964 | static bool classof(const Stmt *S) { |
| 5965 | return S->getStmtClass() == ArrayInitIndexExprClass; |
| 5966 | } |
| 5967 | |
| 5968 | SourceLocation getBeginLoc() const LLVM_READONLY { return SourceLocation(); } |
| 5969 | SourceLocation getEndLoc() const LLVM_READONLY { return SourceLocation(); } |
| 5970 | |
| 5971 | child_range children() { |
| 5972 | return child_range(child_iterator(), child_iterator()); |
| 5973 | } |
| 5974 | const_child_range children() const { |
| 5975 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 5976 | } |
| 5977 | |
| 5978 | friend class ASTReader; |
| 5979 | friend class ASTStmtReader; |
| 5980 | }; |
| 5981 | |
| 5982 | /// Represents an implicitly-generated value initialization of |
| 5983 | /// an object of a given type. |
| 5984 | /// |
| 5985 | /// Implicit value initializations occur within semantic initializer |
| 5986 | /// list expressions (InitListExpr) as placeholders for subobject |
| 5987 | /// initializations not explicitly specified by the user. |
| 5988 | /// |
| 5989 | /// \see InitListExpr |
| 5990 | class ImplicitValueInitExpr : public Expr { |
| 5991 | public: |
| 5992 | explicit ImplicitValueInitExpr(QualType ty) |
| 5993 | : Expr(ImplicitValueInitExprClass, ty, VK_PRValue, OK_Ordinary) { |
| 5994 | setDependence(computeDependence(E: this)); |
| 5995 | } |
| 5996 | |
| 5997 | /// Construct an empty implicit value initialization. |
| 5998 | explicit ImplicitValueInitExpr(EmptyShell Empty) |
| 5999 | : Expr(ImplicitValueInitExprClass, Empty) { } |
| 6000 | |
| 6001 | static bool classof(const Stmt *T) { |
| 6002 | return T->getStmtClass() == ImplicitValueInitExprClass; |
| 6003 | } |
| 6004 | |
| 6005 | SourceLocation getBeginLoc() const LLVM_READONLY { return SourceLocation(); } |
| 6006 | SourceLocation getEndLoc() const LLVM_READONLY { return SourceLocation(); } |
| 6007 | |
| 6008 | // Iterators |
| 6009 | child_range children() { |
| 6010 | return child_range(child_iterator(), child_iterator()); |
| 6011 | } |
| 6012 | const_child_range children() const { |
| 6013 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 6014 | } |
| 6015 | }; |
| 6016 | |
| 6017 | class ParenListExpr final |
| 6018 | : public Expr, |
| 6019 | private llvm::TrailingObjects<ParenListExpr, Stmt *> { |
| 6020 | friend class ASTStmtReader; |
| 6021 | friend TrailingObjects; |
| 6022 | |
| 6023 | /// The location of the left and right parentheses. |
| 6024 | SourceLocation LParenLoc, RParenLoc; |
| 6025 | |
| 6026 | /// Build a paren list. |
| 6027 | ParenListExpr(SourceLocation LParenLoc, ArrayRef<Expr *> Exprs, |
| 6028 | SourceLocation RParenLoc); |
| 6029 | |
| 6030 | /// Build an empty paren list. |
| 6031 | ParenListExpr(EmptyShell Empty, unsigned NumExprs); |
| 6032 | |
| 6033 | public: |
| 6034 | /// Create a paren list. |
| 6035 | static ParenListExpr *Create(const ASTContext &Ctx, SourceLocation LParenLoc, |
| 6036 | ArrayRef<Expr *> Exprs, |
| 6037 | SourceLocation RParenLoc); |
| 6038 | |
| 6039 | /// Create an empty paren list. |
| 6040 | static ParenListExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumExprs); |
| 6041 | |
| 6042 | /// Return the number of expressions in this paren list. |
| 6043 | unsigned getNumExprs() const { return ParenListExprBits.NumExprs; } |
| 6044 | |
| 6045 | Expr *getExpr(unsigned Init) { |
| 6046 | assert(Init < getNumExprs() && "Initializer access out of range!" ); |
| 6047 | return getExprs()[Init]; |
| 6048 | } |
| 6049 | |
| 6050 | const Expr *getExpr(unsigned Init) const { |
| 6051 | return const_cast<ParenListExpr *>(this)->getExpr(Init); |
| 6052 | } |
| 6053 | |
| 6054 | Expr **getExprs() { return reinterpret_cast<Expr **>(getTrailingObjects()); } |
| 6055 | |
| 6056 | ArrayRef<Expr *> exprs() { return llvm::ArrayRef(getExprs(), getNumExprs()); } |
| 6057 | |
| 6058 | SourceLocation getLParenLoc() const { return LParenLoc; } |
| 6059 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 6060 | SourceLocation getBeginLoc() const { return getLParenLoc(); } |
| 6061 | SourceLocation getEndLoc() const { return getRParenLoc(); } |
| 6062 | |
| 6063 | static bool classof(const Stmt *T) { |
| 6064 | return T->getStmtClass() == ParenListExprClass; |
| 6065 | } |
| 6066 | |
| 6067 | // Iterators |
| 6068 | child_range children() { |
| 6069 | return child_range(getTrailingObjects(getNumExprs())); |
| 6070 | } |
| 6071 | const_child_range children() const { |
| 6072 | return const_child_range(getTrailingObjects(getNumExprs())); |
| 6073 | } |
| 6074 | }; |
| 6075 | |
| 6076 | /// Represents a C11 generic selection. |
| 6077 | /// |
| 6078 | /// A generic selection (C11 6.5.1.1) contains an unevaluated controlling |
| 6079 | /// expression, followed by one or more generic associations. Each generic |
| 6080 | /// association specifies a type name and an expression, or "default" and an |
| 6081 | /// expression (in which case it is known as a default generic association). |
| 6082 | /// The type and value of the generic selection are identical to those of its |
| 6083 | /// result expression, which is defined as the expression in the generic |
| 6084 | /// association with a type name that is compatible with the type of the |
| 6085 | /// controlling expression, or the expression in the default generic association |
| 6086 | /// if no types are compatible. For example: |
| 6087 | /// |
| 6088 | /// @code |
| 6089 | /// _Generic(X, double: 1, float: 2, default: 3) |
| 6090 | /// @endcode |
| 6091 | /// |
| 6092 | /// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f |
| 6093 | /// or 3 if "hello". |
| 6094 | /// |
| 6095 | /// As an extension, generic selections are allowed in C++, where the following |
| 6096 | /// additional semantics apply: |
| 6097 | /// |
| 6098 | /// Any generic selection whose controlling expression is type-dependent or |
| 6099 | /// which names a dependent type in its association list is result-dependent, |
| 6100 | /// which means that the choice of result expression is dependent. |
| 6101 | /// Result-dependent generic associations are both type- and value-dependent. |
| 6102 | /// |
| 6103 | /// We also allow an extended form in both C and C++ where the controlling |
| 6104 | /// predicate for the selection expression is a type rather than an expression. |
| 6105 | /// This type argument form does not perform any conversions for the |
| 6106 | /// controlling type, which makes it suitable for use with qualified type |
| 6107 | /// associations, which is not possible with the expression form. |
| 6108 | class GenericSelectionExpr final |
| 6109 | : public Expr, |
| 6110 | private llvm::TrailingObjects<GenericSelectionExpr, Stmt *, |
| 6111 | TypeSourceInfo *> { |
| 6112 | friend class ASTStmtReader; |
| 6113 | friend class ASTStmtWriter; |
| 6114 | friend TrailingObjects; |
| 6115 | |
| 6116 | /// The number of association expressions and the index of the result |
| 6117 | /// expression in the case where the generic selection expression is not |
| 6118 | /// result-dependent. The result index is equal to ResultDependentIndex |
| 6119 | /// if and only if the generic selection expression is result-dependent. |
| 6120 | unsigned NumAssocs : 15; |
| 6121 | unsigned ResultIndex : 15; // NB: ResultDependentIndex is tied to this width. |
| 6122 | LLVM_PREFERRED_TYPE(bool) |
| 6123 | unsigned IsExprPredicate : 1; |
| 6124 | enum : unsigned { |
| 6125 | ResultDependentIndex = 0x7FFF |
| 6126 | }; |
| 6127 | |
| 6128 | unsigned getIndexOfControllingExpression() const { |
| 6129 | // If controlled by an expression, the first offset into the Stmt * |
| 6130 | // trailing array is the controlling expression, the associated expressions |
| 6131 | // follow this. |
| 6132 | assert(isExprPredicate() && "Asking for the controlling expression of a " |
| 6133 | "selection expr predicated by a type" ); |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
| 6137 | unsigned getIndexOfControllingType() const { |
| 6138 | // If controlled by a type, the first offset into the TypeSourceInfo * |
| 6139 | // trailing array is the controlling type, the associated types follow this. |
| 6140 | assert(isTypePredicate() && "Asking for the controlling type of a " |
| 6141 | "selection expr predicated by an expression" ); |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
| 6145 | unsigned getIndexOfStartOfAssociatedExprs() const { |
| 6146 | // If the predicate is a type, then the associated expressions are the only |
| 6147 | // Stmt * in the trailing array, otherwise we need to offset past the |
| 6148 | // predicate expression. |
| 6149 | return (int)isExprPredicate(); |
| 6150 | } |
| 6151 | |
| 6152 | unsigned getIndexOfStartOfAssociatedTypes() const { |
| 6153 | // If the predicate is a type, then the associated types follow it in the |
| 6154 | // trailing array. Otherwise, the associated types are the only |
| 6155 | // TypeSourceInfo * in the trailing array. |
| 6156 | return (int)isTypePredicate(); |
| 6157 | } |
| 6158 | |
| 6159 | |
| 6160 | /// The location of the "default" and of the right parenthesis. |
| 6161 | SourceLocation DefaultLoc, RParenLoc; |
| 6162 | |
| 6163 | // GenericSelectionExpr is followed by several trailing objects. |
| 6164 | // They are (in order): |
| 6165 | // |
| 6166 | // * A single Stmt * for the controlling expression or a TypeSourceInfo * for |
| 6167 | // the controlling type, depending on the result of isTypePredicate() or |
| 6168 | // isExprPredicate(). |
| 6169 | // * An array of getNumAssocs() Stmt * for the association expressions. |
| 6170 | // * An array of getNumAssocs() TypeSourceInfo *, one for each of the |
| 6171 | // association expressions. |
| 6172 | unsigned numTrailingObjects(OverloadToken<Stmt *>) const { |
| 6173 | // Add one to account for the controlling expression; the remainder |
| 6174 | // are the associated expressions. |
| 6175 | return getNumAssocs() + (int)isExprPredicate(); |
| 6176 | } |
| 6177 | |
| 6178 | unsigned numTrailingObjects(OverloadToken<TypeSourceInfo *>) const { |
| 6179 | // Add one to account for the controlling type predicate, the remainder |
| 6180 | // are the associated types. |
| 6181 | return getNumAssocs() + (int)isTypePredicate(); |
| 6182 | } |
| 6183 | |
| 6184 | template <bool Const> class AssociationIteratorTy; |
| 6185 | /// Bundle together an association expression and its TypeSourceInfo. |
| 6186 | /// The Const template parameter is for the const and non-const versions |
| 6187 | /// of AssociationTy. |
| 6188 | template <bool Const> class AssociationTy { |
| 6189 | friend class GenericSelectionExpr; |
| 6190 | template <bool OtherConst> friend class AssociationIteratorTy; |
| 6191 | using ExprPtrTy = std::conditional_t<Const, const Expr *, Expr *>; |
| 6192 | using TSIPtrTy = |
| 6193 | std::conditional_t<Const, const TypeSourceInfo *, TypeSourceInfo *>; |
| 6194 | ExprPtrTy E; |
| 6195 | TSIPtrTy TSI; |
| 6196 | bool Selected; |
| 6197 | AssociationTy(ExprPtrTy E, TSIPtrTy TSI, bool Selected) |
| 6198 | : E(E), TSI(TSI), Selected(Selected) {} |
| 6199 | |
| 6200 | public: |
| 6201 | ExprPtrTy getAssociationExpr() const { return E; } |
| 6202 | TSIPtrTy getTypeSourceInfo() const { return TSI; } |
| 6203 | QualType getType() const { return TSI ? TSI->getType() : QualType(); } |
| 6204 | bool isSelected() const { return Selected; } |
| 6205 | AssociationTy *operator->() { return this; } |
| 6206 | const AssociationTy *operator->() const { return this; } |
| 6207 | }; // class AssociationTy |
| 6208 | |
| 6209 | /// Iterator over const and non-const Association objects. The Association |
| 6210 | /// objects are created on the fly when the iterator is dereferenced. |
| 6211 | /// This abstract over how exactly the association expressions and the |
| 6212 | /// corresponding TypeSourceInfo * are stored. |
| 6213 | template <bool Const> |
| 6214 | class AssociationIteratorTy |
| 6215 | : public llvm::iterator_facade_base< |
| 6216 | AssociationIteratorTy<Const>, std::input_iterator_tag, |
| 6217 | AssociationTy<Const>, std::ptrdiff_t, AssociationTy<Const>, |
| 6218 | AssociationTy<Const>> { |
| 6219 | friend class GenericSelectionExpr; |
| 6220 | // FIXME: This iterator could conceptually be a random access iterator, and |
| 6221 | // it would be nice if we could strengthen the iterator category someday. |
| 6222 | // However this iterator does not satisfy two requirements of forward |
| 6223 | // iterators: |
| 6224 | // a) reference = T& or reference = const T& |
| 6225 | // b) If It1 and It2 are both dereferenceable, then It1 == It2 if and only |
| 6226 | // if *It1 and *It2 are bound to the same objects. |
| 6227 | // An alternative design approach was discussed during review; |
| 6228 | // store an Association object inside the iterator, and return a reference |
| 6229 | // to it when dereferenced. This idea was discarded because of nasty |
| 6230 | // lifetime issues: |
| 6231 | // AssociationIterator It = ...; |
| 6232 | // const Association &Assoc = *It++; // Oops, Assoc is dangling. |
| 6233 | using BaseTy = typename AssociationIteratorTy::iterator_facade_base; |
| 6234 | using StmtPtrPtrTy = |
| 6235 | std::conditional_t<Const, const Stmt *const *, Stmt **>; |
| 6236 | using TSIPtrPtrTy = std::conditional_t<Const, const TypeSourceInfo *const *, |
| 6237 | TypeSourceInfo **>; |
| 6238 | StmtPtrPtrTy E = nullptr; |
| 6239 | TSIPtrPtrTy TSI; // Kept in sync with E. |
| 6240 | unsigned Offset = 0, SelectedOffset = 0; |
| 6241 | AssociationIteratorTy(StmtPtrPtrTy E, TSIPtrPtrTy TSI, unsigned Offset, |
| 6242 | unsigned SelectedOffset) |
| 6243 | : E(E), TSI(TSI), Offset(Offset), SelectedOffset(SelectedOffset) {} |
| 6244 | |
| 6245 | public: |
| 6246 | AssociationIteratorTy() : E(nullptr), TSI(nullptr) {} |
| 6247 | typename BaseTy::reference operator*() const { |
| 6248 | return AssociationTy<Const>(cast<Expr>(*E), *TSI, |
| 6249 | Offset == SelectedOffset); |
| 6250 | } |
| 6251 | typename BaseTy::pointer operator->() const { return **this; } |
| 6252 | using BaseTy::operator++; |
| 6253 | AssociationIteratorTy &operator++() { |
| 6254 | ++E; |
| 6255 | ++TSI; |
| 6256 | ++Offset; |
| 6257 | return *this; |
| 6258 | } |
| 6259 | bool operator==(AssociationIteratorTy Other) const { return E == Other.E; } |
| 6260 | }; // class AssociationIterator |
| 6261 | |
| 6262 | /// Build a non-result-dependent generic selection expression accepting an |
| 6263 | /// expression predicate. |
| 6264 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, |
| 6265 | Expr *ControllingExpr, |
| 6266 | ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6267 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6268 | SourceLocation RParenLoc, |
| 6269 | bool ContainsUnexpandedParameterPack, |
| 6270 | unsigned ResultIndex); |
| 6271 | |
| 6272 | /// Build a result-dependent generic selection expression accepting an |
| 6273 | /// expression predicate. |
| 6274 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, |
| 6275 | Expr *ControllingExpr, |
| 6276 | ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6277 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6278 | SourceLocation RParenLoc, |
| 6279 | bool ContainsUnexpandedParameterPack); |
| 6280 | |
| 6281 | /// Build a non-result-dependent generic selection expression accepting a |
| 6282 | /// type predicate. |
| 6283 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, |
| 6284 | TypeSourceInfo *ControllingType, |
| 6285 | ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6286 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6287 | SourceLocation RParenLoc, |
| 6288 | bool ContainsUnexpandedParameterPack, |
| 6289 | unsigned ResultIndex); |
| 6290 | |
| 6291 | /// Build a result-dependent generic selection expression accepting a type |
| 6292 | /// predicate. |
| 6293 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, |
| 6294 | TypeSourceInfo *ControllingType, |
| 6295 | ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6296 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6297 | SourceLocation RParenLoc, |
| 6298 | bool ContainsUnexpandedParameterPack); |
| 6299 | |
| 6300 | /// Build an empty generic selection expression for deserialization. |
| 6301 | explicit GenericSelectionExpr(EmptyShell Empty, unsigned NumAssocs); |
| 6302 | |
| 6303 | public: |
| 6304 | /// Create a non-result-dependent generic selection expression accepting an |
| 6305 | /// expression predicate. |
| 6306 | static GenericSelectionExpr * |
| 6307 | Create(const ASTContext &Context, SourceLocation GenericLoc, |
| 6308 | Expr *ControllingExpr, ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6309 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6310 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack, |
| 6311 | unsigned ResultIndex); |
| 6312 | |
| 6313 | /// Create a result-dependent generic selection expression accepting an |
| 6314 | /// expression predicate. |
| 6315 | static GenericSelectionExpr * |
| 6316 | Create(const ASTContext &Context, SourceLocation GenericLoc, |
| 6317 | Expr *ControllingExpr, ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6318 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6319 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack); |
| 6320 | |
| 6321 | /// Create a non-result-dependent generic selection expression accepting a |
| 6322 | /// type predicate. |
| 6323 | static GenericSelectionExpr * |
| 6324 | Create(const ASTContext &Context, SourceLocation GenericLoc, |
| 6325 | TypeSourceInfo *ControllingType, ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6326 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6327 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack, |
| 6328 | unsigned ResultIndex); |
| 6329 | |
| 6330 | /// Create a result-dependent generic selection expression accepting a type |
| 6331 | /// predicate |
| 6332 | static GenericSelectionExpr * |
| 6333 | Create(const ASTContext &Context, SourceLocation GenericLoc, |
| 6334 | TypeSourceInfo *ControllingType, ArrayRef<TypeSourceInfo *> AssocTypes, |
| 6335 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, |
| 6336 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack); |
| 6337 | |
| 6338 | /// Create an empty generic selection expression for deserialization. |
| 6339 | static GenericSelectionExpr *CreateEmpty(const ASTContext &Context, |
| 6340 | unsigned NumAssocs); |
| 6341 | |
| 6342 | using Association = AssociationTy<false>; |
| 6343 | using ConstAssociation = AssociationTy<true>; |
| 6344 | using AssociationIterator = AssociationIteratorTy<false>; |
| 6345 | using ConstAssociationIterator = AssociationIteratorTy<true>; |
| 6346 | using association_range = llvm::iterator_range<AssociationIterator>; |
| 6347 | using const_association_range = |
| 6348 | llvm::iterator_range<ConstAssociationIterator>; |
| 6349 | |
| 6350 | /// The number of association expressions. |
| 6351 | unsigned getNumAssocs() const { return NumAssocs; } |
| 6352 | |
| 6353 | /// The zero-based index of the result expression's generic association in |
| 6354 | /// the generic selection's association list. Defined only if the |
| 6355 | /// generic selection is not result-dependent. |
| 6356 | unsigned getResultIndex() const { |
| 6357 | assert(!isResultDependent() && |
| 6358 | "Generic selection is result-dependent but getResultIndex called!" ); |
| 6359 | return ResultIndex; |
| 6360 | } |
| 6361 | |
| 6362 | /// Whether this generic selection is result-dependent. |
| 6363 | bool isResultDependent() const { return ResultIndex == ResultDependentIndex; } |
| 6364 | |
| 6365 | /// Whether this generic selection uses an expression as its controlling |
| 6366 | /// argument. |
| 6367 | bool isExprPredicate() const { return IsExprPredicate; } |
| 6368 | /// Whether this generic selection uses a type as its controlling argument. |
| 6369 | bool isTypePredicate() const { return !IsExprPredicate; } |
| 6370 | |
| 6371 | /// Return the controlling expression of this generic selection expression. |
| 6372 | /// Only valid to call if the selection expression used an expression as its |
| 6373 | /// controlling argument. |
| 6374 | Expr *getControllingExpr() { |
| 6375 | return cast<Expr>( |
| 6376 | getTrailingObjects<Stmt *>()[getIndexOfControllingExpression()]); |
| 6377 | } |
| 6378 | const Expr *getControllingExpr() const { |
| 6379 | return cast<Expr>( |
| 6380 | getTrailingObjects<Stmt *>()[getIndexOfControllingExpression()]); |
| 6381 | } |
| 6382 | |
| 6383 | /// Return the controlling type of this generic selection expression. Only |
| 6384 | /// valid to call if the selection expression used a type as its controlling |
| 6385 | /// argument. |
| 6386 | TypeSourceInfo *getControllingType() { |
| 6387 | return getTrailingObjects<TypeSourceInfo *>()[getIndexOfControllingType()]; |
| 6388 | } |
| 6389 | const TypeSourceInfo* getControllingType() const { |
| 6390 | return getTrailingObjects<TypeSourceInfo *>()[getIndexOfControllingType()]; |
| 6391 | } |
| 6392 | |
| 6393 | /// Return the result expression of this controlling expression. Defined if |
| 6394 | /// and only if the generic selection expression is not result-dependent. |
| 6395 | Expr *getResultExpr() { |
| 6396 | return cast<Expr>( |
| 6397 | getTrailingObjects<Stmt *>()[getIndexOfStartOfAssociatedExprs() + |
| 6398 | getResultIndex()]); |
| 6399 | } |
| 6400 | const Expr *getResultExpr() const { |
| 6401 | return cast<Expr>( |
| 6402 | getTrailingObjects<Stmt *>()[getIndexOfStartOfAssociatedExprs() + |
| 6403 | getResultIndex()]); |
| 6404 | } |
| 6405 | |
| 6406 | ArrayRef<Expr *> getAssocExprs() const { |
| 6407 | return {reinterpret_cast<Expr *const *>(getTrailingObjects<Stmt *>() + |
| 6408 | getIndexOfStartOfAssociatedExprs()), |
| 6409 | NumAssocs}; |
| 6410 | } |
| 6411 | ArrayRef<TypeSourceInfo *> getAssocTypeSourceInfos() const { |
| 6412 | return {getTrailingObjects<TypeSourceInfo *>() + |
| 6413 | getIndexOfStartOfAssociatedTypes(), |
| 6414 | NumAssocs}; |
| 6415 | } |
| 6416 | |
| 6417 | /// Return the Ith association expression with its TypeSourceInfo, |
| 6418 | /// bundled together in GenericSelectionExpr::(Const)Association. |
| 6419 | Association getAssociation(unsigned I) { |
| 6420 | assert(I < getNumAssocs() && |
| 6421 | "Out-of-range index in GenericSelectionExpr::getAssociation!" ); |
| 6422 | return Association( |
| 6423 | cast<Expr>( |
| 6424 | getTrailingObjects<Stmt *>()[getIndexOfStartOfAssociatedExprs() + |
| 6425 | I]), |
| 6426 | getTrailingObjects< |
| 6427 | TypeSourceInfo *>()[getIndexOfStartOfAssociatedTypes() + I], |
| 6428 | !isResultDependent() && (getResultIndex() == I)); |
| 6429 | } |
| 6430 | ConstAssociation getAssociation(unsigned I) const { |
| 6431 | assert(I < getNumAssocs() && |
| 6432 | "Out-of-range index in GenericSelectionExpr::getAssociation!" ); |
| 6433 | return ConstAssociation( |
| 6434 | cast<Expr>( |
| 6435 | getTrailingObjects<Stmt *>()[getIndexOfStartOfAssociatedExprs() + |
| 6436 | I]), |
| 6437 | getTrailingObjects< |
| 6438 | TypeSourceInfo *>()[getIndexOfStartOfAssociatedTypes() + I], |
| 6439 | !isResultDependent() && (getResultIndex() == I)); |
| 6440 | } |
| 6441 | |
| 6442 | association_range associations() { |
| 6443 | AssociationIterator Begin(getTrailingObjects<Stmt *>() + |
| 6444 | getIndexOfStartOfAssociatedExprs(), |
| 6445 | getTrailingObjects<TypeSourceInfo *>() + |
| 6446 | getIndexOfStartOfAssociatedTypes(), |
| 6447 | /*Offset=*/0, ResultIndex); |
| 6448 | AssociationIterator End(Begin.E + NumAssocs, Begin.TSI + NumAssocs, |
| 6449 | /*Offset=*/NumAssocs, ResultIndex); |
| 6450 | return llvm::make_range(x: Begin, y: End); |
| 6451 | } |
| 6452 | |
| 6453 | const_association_range associations() const { |
| 6454 | ConstAssociationIterator Begin(getTrailingObjects<Stmt *>() + |
| 6455 | getIndexOfStartOfAssociatedExprs(), |
| 6456 | getTrailingObjects<TypeSourceInfo *>() + |
| 6457 | getIndexOfStartOfAssociatedTypes(), |
| 6458 | /*Offset=*/0, ResultIndex); |
| 6459 | ConstAssociationIterator End(Begin.E + NumAssocs, Begin.TSI + NumAssocs, |
| 6460 | /*Offset=*/NumAssocs, ResultIndex); |
| 6461 | return llvm::make_range(x: Begin, y: End); |
| 6462 | } |
| 6463 | |
| 6464 | SourceLocation getGenericLoc() const { |
| 6465 | return GenericSelectionExprBits.GenericLoc; |
| 6466 | } |
| 6467 | SourceLocation getDefaultLoc() const { return DefaultLoc; } |
| 6468 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 6469 | SourceLocation getBeginLoc() const { return getGenericLoc(); } |
| 6470 | SourceLocation getEndLoc() const { return getRParenLoc(); } |
| 6471 | |
| 6472 | static bool classof(const Stmt *T) { |
| 6473 | return T->getStmtClass() == GenericSelectionExprClass; |
| 6474 | } |
| 6475 | |
| 6476 | child_range children() { |
| 6477 | return child_range(getTrailingObjects<Stmt *>( |
| 6478 | numTrailingObjects(OverloadToken<Stmt *>()))); |
| 6479 | } |
| 6480 | const_child_range children() const { |
| 6481 | return const_child_range(getTrailingObjects<Stmt *>( |
| 6482 | numTrailingObjects(OverloadToken<Stmt *>()))); |
| 6483 | } |
| 6484 | }; |
| 6485 | |
| 6486 | //===----------------------------------------------------------------------===// |
| 6487 | // Clang Extensions |
| 6488 | //===----------------------------------------------------------------------===// |
| 6489 | |
| 6490 | /// ExtVectorElementExpr - This represents access to specific elements of a |
| 6491 | /// vector, and may occur on the left hand side or right hand side. For example |
| 6492 | /// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. |
| 6493 | /// |
| 6494 | /// Note that the base may have either vector or pointer to vector type, just |
| 6495 | /// like a struct field reference. |
| 6496 | /// |
| 6497 | class ExtVectorElementExpr : public Expr { |
| 6498 | Stmt *Base; |
| 6499 | IdentifierInfo *Accessor; |
| 6500 | SourceLocation AccessorLoc; |
| 6501 | public: |
| 6502 | ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, |
| 6503 | IdentifierInfo &accessor, SourceLocation loc) |
| 6504 | : Expr(ExtVectorElementExprClass, ty, VK, |
| 6505 | (VK == VK_PRValue ? OK_Ordinary : OK_VectorComponent)), |
| 6506 | Base(base), Accessor(&accessor), AccessorLoc(loc) { |
| 6507 | setDependence(computeDependence(E: this)); |
| 6508 | } |
| 6509 | |
| 6510 | /// Build an empty vector element expression. |
| 6511 | explicit ExtVectorElementExpr(EmptyShell Empty) |
| 6512 | : Expr(ExtVectorElementExprClass, Empty) { } |
| 6513 | |
| 6514 | const Expr *getBase() const { return cast<Expr>(Val: Base); } |
| 6515 | Expr *getBase() { return cast<Expr>(Val: Base); } |
| 6516 | void setBase(Expr *E) { Base = E; } |
| 6517 | |
| 6518 | IdentifierInfo &getAccessor() const { return *Accessor; } |
| 6519 | void setAccessor(IdentifierInfo *II) { Accessor = II; } |
| 6520 | |
| 6521 | SourceLocation getAccessorLoc() const { return AccessorLoc; } |
| 6522 | void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } |
| 6523 | |
| 6524 | /// getNumElements - Get the number of components being selected. |
| 6525 | unsigned getNumElements() const; |
| 6526 | |
| 6527 | /// containsDuplicateElements - Return true if any element access is |
| 6528 | /// repeated. |
| 6529 | bool containsDuplicateElements() const; |
| 6530 | |
| 6531 | /// getEncodedElementAccess - Encode the elements accessed into an llvm |
| 6532 | /// aggregate Constant of ConstantInt(s). |
| 6533 | void getEncodedElementAccess(SmallVectorImpl<uint32_t> &Elts) const; |
| 6534 | |
| 6535 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 6536 | return getBase()->getBeginLoc(); |
| 6537 | } |
| 6538 | SourceLocation getEndLoc() const LLVM_READONLY { return AccessorLoc; } |
| 6539 | |
| 6540 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 6541 | /// return false if the base expression is a vector. |
| 6542 | bool isArrow() const; |
| 6543 | |
| 6544 | static bool classof(const Stmt *T) { |
| 6545 | return T->getStmtClass() == ExtVectorElementExprClass; |
| 6546 | } |
| 6547 | |
| 6548 | // Iterators |
| 6549 | child_range children() { return child_range(&Base, &Base+1); } |
| 6550 | const_child_range children() const { |
| 6551 | return const_child_range(&Base, &Base + 1); |
| 6552 | } |
| 6553 | }; |
| 6554 | |
| 6555 | /// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. |
| 6556 | /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } |
| 6557 | class BlockExpr : public Expr { |
| 6558 | protected: |
| 6559 | BlockDecl *TheBlock; |
| 6560 | public: |
| 6561 | BlockExpr(BlockDecl *BD, QualType ty, bool ContainsUnexpandedParameterPack) |
| 6562 | : Expr(BlockExprClass, ty, VK_PRValue, OK_Ordinary), TheBlock(BD) { |
| 6563 | setDependence(computeDependence(E: this, ContainsUnexpandedParameterPack)); |
| 6564 | } |
| 6565 | |
| 6566 | /// Build an empty block expression. |
| 6567 | explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } |
| 6568 | |
| 6569 | const BlockDecl *getBlockDecl() const { return TheBlock; } |
| 6570 | BlockDecl *getBlockDecl() { return TheBlock; } |
| 6571 | void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } |
| 6572 | |
| 6573 | // Convenience functions for probing the underlying BlockDecl. |
| 6574 | SourceLocation getCaretLocation() const; |
| 6575 | const Stmt *getBody() const; |
| 6576 | Stmt *getBody(); |
| 6577 | |
| 6578 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 6579 | return getCaretLocation(); |
| 6580 | } |
| 6581 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 6582 | return getBody()->getEndLoc(); |
| 6583 | } |
| 6584 | |
| 6585 | /// getFunctionType - Return the underlying function type for this block. |
| 6586 | const FunctionProtoType *getFunctionType() const; |
| 6587 | |
| 6588 | static bool classof(const Stmt *T) { |
| 6589 | return T->getStmtClass() == BlockExprClass; |
| 6590 | } |
| 6591 | |
| 6592 | // Iterators |
| 6593 | child_range children() { |
| 6594 | return child_range(child_iterator(), child_iterator()); |
| 6595 | } |
| 6596 | const_child_range children() const { |
| 6597 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 6598 | } |
| 6599 | }; |
| 6600 | |
| 6601 | /// Copy initialization expr of a __block variable and a boolean flag that |
| 6602 | /// indicates whether the expression can throw. |
| 6603 | struct BlockVarCopyInit { |
| 6604 | BlockVarCopyInit() = default; |
| 6605 | BlockVarCopyInit(Expr *CopyExpr, bool CanThrow) |
| 6606 | : ExprAndFlag(CopyExpr, CanThrow) {} |
| 6607 | void setExprAndFlag(Expr *CopyExpr, bool CanThrow) { |
| 6608 | ExprAndFlag.setPointerAndInt(PtrVal: CopyExpr, IntVal: CanThrow); |
| 6609 | } |
| 6610 | Expr *getCopyExpr() const { return ExprAndFlag.getPointer(); } |
| 6611 | bool canThrow() const { return ExprAndFlag.getInt(); } |
| 6612 | llvm::PointerIntPair<Expr *, 1, bool> ExprAndFlag; |
| 6613 | }; |
| 6614 | |
| 6615 | /// AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] |
| 6616 | /// This AST node provides support for reinterpreting a type to another |
| 6617 | /// type of the same size. |
| 6618 | class AsTypeExpr : public Expr { |
| 6619 | private: |
| 6620 | Stmt *SrcExpr; |
| 6621 | SourceLocation BuiltinLoc, RParenLoc; |
| 6622 | |
| 6623 | friend class ASTReader; |
| 6624 | friend class ASTStmtReader; |
| 6625 | explicit AsTypeExpr(EmptyShell Empty) : Expr(AsTypeExprClass, Empty) {} |
| 6626 | |
| 6627 | public: |
| 6628 | AsTypeExpr(Expr *SrcExpr, QualType DstType, ExprValueKind VK, |
| 6629 | ExprObjectKind OK, SourceLocation BuiltinLoc, |
| 6630 | SourceLocation RParenLoc) |
| 6631 | : Expr(AsTypeExprClass, DstType, VK, OK), SrcExpr(SrcExpr), |
| 6632 | BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) { |
| 6633 | setDependence(computeDependence(E: this)); |
| 6634 | } |
| 6635 | |
| 6636 | /// getSrcExpr - Return the Expr to be converted. |
| 6637 | Expr *getSrcExpr() const { return cast<Expr>(Val: SrcExpr); } |
| 6638 | |
| 6639 | /// getBuiltinLoc - Return the location of the __builtin_astype token. |
| 6640 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 6641 | |
| 6642 | /// getRParenLoc - Return the location of final right parenthesis. |
| 6643 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 6644 | |
| 6645 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 6646 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 6647 | |
| 6648 | static bool classof(const Stmt *T) { |
| 6649 | return T->getStmtClass() == AsTypeExprClass; |
| 6650 | } |
| 6651 | |
| 6652 | // Iterators |
| 6653 | child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } |
| 6654 | const_child_range children() const { |
| 6655 | return const_child_range(&SrcExpr, &SrcExpr + 1); |
| 6656 | } |
| 6657 | }; |
| 6658 | |
| 6659 | /// PseudoObjectExpr - An expression which accesses a pseudo-object |
| 6660 | /// l-value. A pseudo-object is an abstract object, accesses to which |
| 6661 | /// are translated to calls. The pseudo-object expression has a |
| 6662 | /// syntactic form, which shows how the expression was actually |
| 6663 | /// written in the source code, and a semantic form, which is a series |
| 6664 | /// of expressions to be executed in order which detail how the |
| 6665 | /// operation is actually evaluated. Optionally, one of the semantic |
| 6666 | /// forms may also provide a result value for the expression. |
| 6667 | /// |
| 6668 | /// If any of the semantic-form expressions is an OpaqueValueExpr, |
| 6669 | /// that OVE is required to have a source expression, and it is bound |
| 6670 | /// to the result of that source expression. Such OVEs may appear |
| 6671 | /// only in subsequent semantic-form expressions and as |
| 6672 | /// sub-expressions of the syntactic form. |
| 6673 | /// |
| 6674 | /// PseudoObjectExpr should be used only when an operation can be |
| 6675 | /// usefully described in terms of fairly simple rewrite rules on |
| 6676 | /// objects and functions that are meant to be used by end-developers. |
| 6677 | /// For example, under the Itanium ABI, dynamic casts are implemented |
| 6678 | /// as a call to a runtime function called __dynamic_cast; using this |
| 6679 | /// class to describe that would be inappropriate because that call is |
| 6680 | /// not really part of the user-visible semantics, and instead the |
| 6681 | /// cast is properly reflected in the AST and IR-generation has been |
| 6682 | /// taught to generate the call as necessary. In contrast, an |
| 6683 | /// Objective-C property access is semantically defined to be |
| 6684 | /// equivalent to a particular message send, and this is very much |
| 6685 | /// part of the user model. The name of this class encourages this |
| 6686 | /// modelling design. |
| 6687 | class PseudoObjectExpr final |
| 6688 | : public Expr, |
| 6689 | private llvm::TrailingObjects<PseudoObjectExpr, Expr *> { |
| 6690 | // PseudoObjectExprBits.NumSubExprs - The number of sub-expressions. |
| 6691 | // Always at least two, because the first sub-expression is the |
| 6692 | // syntactic form. |
| 6693 | |
| 6694 | // PseudoObjectExprBits.ResultIndex - The index of the |
| 6695 | // sub-expression holding the result. 0 means the result is void, |
| 6696 | // which is unambiguous because it's the index of the syntactic |
| 6697 | // form. Note that this is therefore 1 higher than the value passed |
| 6698 | // in to Create, which is an index within the semantic forms. |
| 6699 | // Note also that ASTStmtWriter assumes this encoding. |
| 6700 | |
| 6701 | PseudoObjectExpr(QualType type, ExprValueKind VK, |
| 6702 | Expr *syntactic, ArrayRef<Expr*> semantic, |
| 6703 | unsigned resultIndex); |
| 6704 | |
| 6705 | PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs); |
| 6706 | |
| 6707 | unsigned getNumSubExprs() const { |
| 6708 | return PseudoObjectExprBits.NumSubExprs; |
| 6709 | } |
| 6710 | |
| 6711 | public: |
| 6712 | /// NoResult - A value for the result index indicating that there is |
| 6713 | /// no semantic result. |
| 6714 | enum : unsigned { NoResult = ~0U }; |
| 6715 | |
| 6716 | static PseudoObjectExpr *Create(const ASTContext &Context, Expr *syntactic, |
| 6717 | ArrayRef<Expr*> semantic, |
| 6718 | unsigned resultIndex); |
| 6719 | |
| 6720 | static PseudoObjectExpr *Create(const ASTContext &Context, EmptyShell shell, |
| 6721 | unsigned numSemanticExprs); |
| 6722 | |
| 6723 | /// Return the syntactic form of this expression, i.e. the |
| 6724 | /// expression it actually looks like. Likely to be expressed in |
| 6725 | /// terms of OpaqueValueExprs bound in the semantic form. |
| 6726 | Expr *getSyntacticForm() { return getTrailingObjects()[0]; } |
| 6727 | const Expr *getSyntacticForm() const { return getTrailingObjects()[0]; } |
| 6728 | |
| 6729 | /// Return the index of the result-bearing expression into the semantics |
| 6730 | /// expressions, or PseudoObjectExpr::NoResult if there is none. |
| 6731 | unsigned getResultExprIndex() const { |
| 6732 | if (PseudoObjectExprBits.ResultIndex == 0) return NoResult; |
| 6733 | return PseudoObjectExprBits.ResultIndex - 1; |
| 6734 | } |
| 6735 | |
| 6736 | /// Return the result-bearing expression, or null if there is none. |
| 6737 | Expr *getResultExpr() { |
| 6738 | if (PseudoObjectExprBits.ResultIndex == 0) |
| 6739 | return nullptr; |
| 6740 | return getTrailingObjects()[PseudoObjectExprBits.ResultIndex]; |
| 6741 | } |
| 6742 | const Expr *getResultExpr() const { |
| 6743 | return const_cast<PseudoObjectExpr*>(this)->getResultExpr(); |
| 6744 | } |
| 6745 | |
| 6746 | unsigned getNumSemanticExprs() const { return getNumSubExprs() - 1; } |
| 6747 | |
| 6748 | typedef Expr * const *semantics_iterator; |
| 6749 | typedef const Expr * const *const_semantics_iterator; |
| 6750 | semantics_iterator semantics_begin() { return getTrailingObjects() + 1; } |
| 6751 | const_semantics_iterator semantics_begin() const { |
| 6752 | return getTrailingObjects() + 1; |
| 6753 | } |
| 6754 | semantics_iterator semantics_end() { |
| 6755 | return getTrailingObjects() + getNumSubExprs(); |
| 6756 | } |
| 6757 | const_semantics_iterator semantics_end() const { |
| 6758 | return getTrailingObjects() + getNumSubExprs(); |
| 6759 | } |
| 6760 | |
| 6761 | ArrayRef<Expr*> semantics() { |
| 6762 | return getTrailingObjects(getNumSubExprs()).drop_front(); |
| 6763 | } |
| 6764 | ArrayRef<const Expr*> semantics() const { |
| 6765 | return getTrailingObjects(getNumSubExprs()).drop_front(); |
| 6766 | } |
| 6767 | |
| 6768 | Expr *getSemanticExpr(unsigned index) { |
| 6769 | return getTrailingObjects(getNumSubExprs())[index + 1]; |
| 6770 | } |
| 6771 | const Expr *getSemanticExpr(unsigned index) const { |
| 6772 | return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index); |
| 6773 | } |
| 6774 | |
| 6775 | SourceLocation getExprLoc() const LLVM_READONLY { |
| 6776 | return getSyntacticForm()->getExprLoc(); |
| 6777 | } |
| 6778 | |
| 6779 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 6780 | return getSyntacticForm()->getBeginLoc(); |
| 6781 | } |
| 6782 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 6783 | return getSyntacticForm()->getEndLoc(); |
| 6784 | } |
| 6785 | |
| 6786 | child_range children() { |
| 6787 | const_child_range CCR = |
| 6788 | const_cast<const PseudoObjectExpr *>(this)->children(); |
| 6789 | return child_range(cast_away_const(RHS: CCR.begin()), |
| 6790 | cast_away_const(RHS: CCR.end())); |
| 6791 | } |
| 6792 | const_child_range children() const { |
| 6793 | Stmt *const *cs = const_cast<Stmt *const *>( |
| 6794 | reinterpret_cast<const Stmt *const *>(getTrailingObjects())); |
| 6795 | return const_child_range(cs, cs + getNumSubExprs()); |
| 6796 | } |
| 6797 | |
| 6798 | static bool classof(const Stmt *T) { |
| 6799 | return T->getStmtClass() == PseudoObjectExprClass; |
| 6800 | } |
| 6801 | |
| 6802 | friend TrailingObjects; |
| 6803 | friend class ASTStmtReader; |
| 6804 | }; |
| 6805 | |
| 6806 | /// AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, |
| 6807 | /// __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the |
| 6808 | /// similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>, |
| 6809 | /// and corresponding __opencl_atomic_* for OpenCL 2.0. |
| 6810 | /// All of these instructions take one primary pointer, at least one memory |
| 6811 | /// order. The instructions for which getScopeModel returns non-null value |
| 6812 | /// take one sync scope. |
| 6813 | class AtomicExpr : public Expr { |
| 6814 | public: |
| 6815 | enum AtomicOp { |
| 6816 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) AO ## ID, |
| 6817 | #include "clang/Basic/Builtins.inc" |
| 6818 | // Avoid trailing comma |
| 6819 | BI_First = 0 |
| 6820 | }; |
| 6821 | |
| 6822 | private: |
| 6823 | /// Location of sub-expressions. |
| 6824 | /// The location of Scope sub-expression is NumSubExprs - 1, which is |
| 6825 | /// not fixed, therefore is not defined in enum. |
| 6826 | enum { PTR, ORDER, VAL1, ORDER_FAIL, VAL2, WEAK, END_EXPR }; |
| 6827 | Stmt *SubExprs[END_EXPR + 1]; |
| 6828 | unsigned NumSubExprs; |
| 6829 | SourceLocation BuiltinLoc, RParenLoc; |
| 6830 | AtomicOp Op; |
| 6831 | |
| 6832 | friend class ASTStmtReader; |
| 6833 | public: |
| 6834 | AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args, QualType t, |
| 6835 | AtomicOp op, SourceLocation RP); |
| 6836 | |
| 6837 | /// Determine the number of arguments the specified atomic builtin |
| 6838 | /// should have. |
| 6839 | static unsigned getNumSubExprs(AtomicOp Op); |
| 6840 | |
| 6841 | /// Build an empty AtomicExpr. |
| 6842 | explicit AtomicExpr(EmptyShell Empty) : Expr(AtomicExprClass, Empty) { } |
| 6843 | |
| 6844 | Expr *getPtr() const { |
| 6845 | return cast<Expr>(SubExprs[PTR]); |
| 6846 | } |
| 6847 | Expr *getOrder() const { |
| 6848 | return cast<Expr>(SubExprs[ORDER]); |
| 6849 | } |
| 6850 | Expr *getScope() const { |
| 6851 | assert(getScopeModel() && "No scope" ); |
| 6852 | return cast<Expr>(SubExprs[NumSubExprs - 1]); |
| 6853 | } |
| 6854 | Expr *getVal1() const { |
| 6855 | if (Op == AO__c11_atomic_init || Op == AO__opencl_atomic_init) |
| 6856 | return cast<Expr>(SubExprs[ORDER]); |
| 6857 | assert(NumSubExprs > VAL1); |
| 6858 | return cast<Expr>(SubExprs[VAL1]); |
| 6859 | } |
| 6860 | Expr *getOrderFail() const { |
| 6861 | assert(NumSubExprs > ORDER_FAIL); |
| 6862 | return cast<Expr>(SubExprs[ORDER_FAIL]); |
| 6863 | } |
| 6864 | Expr *getVal2() const { |
| 6865 | if (Op == AO__atomic_exchange || Op == AO__scoped_atomic_exchange) |
| 6866 | return cast<Expr>(SubExprs[ORDER_FAIL]); |
| 6867 | assert(NumSubExprs > VAL2); |
| 6868 | return cast<Expr>(SubExprs[VAL2]); |
| 6869 | } |
| 6870 | Expr *getWeak() const { |
| 6871 | assert(NumSubExprs > WEAK); |
| 6872 | return cast<Expr>(SubExprs[WEAK]); |
| 6873 | } |
| 6874 | QualType getValueType() const; |
| 6875 | |
| 6876 | AtomicOp getOp() const { return Op; } |
| 6877 | StringRef getOpAsString() const { |
| 6878 | switch (Op) { |
| 6879 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 6880 | case AO##ID: \ |
| 6881 | return #ID; |
| 6882 | #include "clang/Basic/Builtins.inc" |
| 6883 | } |
| 6884 | llvm_unreachable("not an atomic operator?" ); |
| 6885 | } |
| 6886 | unsigned getNumSubExprs() const { return NumSubExprs; } |
| 6887 | |
| 6888 | Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } |
| 6889 | const Expr * const *getSubExprs() const { |
| 6890 | return reinterpret_cast<Expr * const *>(SubExprs); |
| 6891 | } |
| 6892 | |
| 6893 | bool isVolatile() const { |
| 6894 | return getPtr()->getType()->getPointeeType().isVolatileQualified(); |
| 6895 | } |
| 6896 | |
| 6897 | bool isCmpXChg() const { |
| 6898 | return getOp() == AO__c11_atomic_compare_exchange_strong || |
| 6899 | getOp() == AO__c11_atomic_compare_exchange_weak || |
| 6900 | getOp() == AO__hip_atomic_compare_exchange_strong || |
| 6901 | getOp() == AO__opencl_atomic_compare_exchange_strong || |
| 6902 | getOp() == AO__opencl_atomic_compare_exchange_weak || |
| 6903 | getOp() == AO__hip_atomic_compare_exchange_weak || |
| 6904 | getOp() == AO__atomic_compare_exchange || |
| 6905 | getOp() == AO__atomic_compare_exchange_n || |
| 6906 | getOp() == AO__scoped_atomic_compare_exchange || |
| 6907 | getOp() == AO__scoped_atomic_compare_exchange_n; |
| 6908 | } |
| 6909 | |
| 6910 | bool isOpenCL() const { |
| 6911 | return getOp() >= AO__opencl_atomic_compare_exchange_strong && |
| 6912 | getOp() <= AO__opencl_atomic_store; |
| 6913 | } |
| 6914 | |
| 6915 | bool isHIP() const { |
| 6916 | return Op >= AO__hip_atomic_compare_exchange_strong && |
| 6917 | Op <= AO__hip_atomic_store; |
| 6918 | } |
| 6919 | |
| 6920 | /// Return true if atomics operations targeting allocations in private memory |
| 6921 | /// are undefined. |
| 6922 | bool threadPrivateMemoryAtomicsAreUndefined() const { |
| 6923 | return isOpenCL() || isHIP(); |
| 6924 | } |
| 6925 | |
| 6926 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } |
| 6927 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 6928 | |
| 6929 | SourceLocation getBeginLoc() const LLVM_READONLY { return BuiltinLoc; } |
| 6930 | SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } |
| 6931 | |
| 6932 | static bool classof(const Stmt *T) { |
| 6933 | return T->getStmtClass() == AtomicExprClass; |
| 6934 | } |
| 6935 | |
| 6936 | // Iterators |
| 6937 | child_range children() { |
| 6938 | return child_range(SubExprs, SubExprs+NumSubExprs); |
| 6939 | } |
| 6940 | const_child_range children() const { |
| 6941 | return const_child_range(SubExprs, SubExprs + NumSubExprs); |
| 6942 | } |
| 6943 | |
| 6944 | /// Get atomic scope model for the atomic op code. |
| 6945 | /// \return empty atomic scope model if the atomic op code does not have |
| 6946 | /// scope operand. |
| 6947 | static std::unique_ptr<AtomicScopeModel> getScopeModel(AtomicOp Op) { |
| 6948 | // FIXME: Allow grouping of builtins to be able to only check >= and <= |
| 6949 | if (Op >= AO__opencl_atomic_compare_exchange_strong && |
| 6950 | Op <= AO__opencl_atomic_store && Op != AO__opencl_atomic_init) |
| 6951 | return AtomicScopeModel::create(K: AtomicScopeModelKind::OpenCL); |
| 6952 | if (Op >= AO__hip_atomic_compare_exchange_strong && |
| 6953 | Op <= AO__hip_atomic_store) |
| 6954 | return AtomicScopeModel::create(K: AtomicScopeModelKind::HIP); |
| 6955 | if (Op >= AO__scoped_atomic_add_fetch && Op <= AO__scoped_atomic_xor_fetch) |
| 6956 | return AtomicScopeModel::create(K: AtomicScopeModelKind::Generic); |
| 6957 | return AtomicScopeModel::create(K: AtomicScopeModelKind::None); |
| 6958 | } |
| 6959 | |
| 6960 | /// Get atomic scope model. |
| 6961 | /// \return empty atomic scope model if this atomic expression does not have |
| 6962 | /// scope operand. |
| 6963 | std::unique_ptr<AtomicScopeModel> getScopeModel() const { |
| 6964 | return getScopeModel(getOp()); |
| 6965 | } |
| 6966 | }; |
| 6967 | |
| 6968 | /// TypoExpr - Internal placeholder for expressions where typo correction |
| 6969 | /// still needs to be performed and/or an error diagnostic emitted. |
| 6970 | class TypoExpr : public Expr { |
| 6971 | // The location for the typo name. |
| 6972 | SourceLocation TypoLoc; |
| 6973 | |
| 6974 | public: |
| 6975 | TypoExpr(QualType T, SourceLocation TypoLoc) |
| 6976 | : Expr(TypoExprClass, T, VK_LValue, OK_Ordinary), TypoLoc(TypoLoc) { |
| 6977 | assert(T->isDependentType() && "TypoExpr given a non-dependent type" ); |
| 6978 | setDependence(ExprDependence::TypeValueInstantiation | |
| 6979 | ExprDependence::Error); |
| 6980 | } |
| 6981 | |
| 6982 | child_range children() { |
| 6983 | return child_range(child_iterator(), child_iterator()); |
| 6984 | } |
| 6985 | const_child_range children() const { |
| 6986 | return const_child_range(const_child_iterator(), const_child_iterator()); |
| 6987 | } |
| 6988 | |
| 6989 | SourceLocation getBeginLoc() const LLVM_READONLY { return TypoLoc; } |
| 6990 | SourceLocation getEndLoc() const LLVM_READONLY { return TypoLoc; } |
| 6991 | |
| 6992 | static bool classof(const Stmt *T) { |
| 6993 | return T->getStmtClass() == TypoExprClass; |
| 6994 | } |
| 6995 | |
| 6996 | }; |
| 6997 | |
| 6998 | /// This class represents BOTH the OpenMP Array Section and OpenACC 'subarray', |
| 6999 | /// with a boolean differentiator. |
| 7000 | /// OpenMP 5.0 [2.1.5, Array Sections]. |
| 7001 | /// To specify an array section in an OpenMP construct, array subscript |
| 7002 | /// expressions are extended with the following syntax: |
| 7003 | /// \code |
| 7004 | /// [ lower-bound : length : stride ] |
| 7005 | /// [ lower-bound : length : ] |
| 7006 | /// [ lower-bound : length ] |
| 7007 | /// [ lower-bound : : stride ] |
| 7008 | /// [ lower-bound : : ] |
| 7009 | /// [ lower-bound : ] |
| 7010 | /// [ : length : stride ] |
| 7011 | /// [ : length : ] |
| 7012 | /// [ : length ] |
| 7013 | /// [ : : stride ] |
| 7014 | /// [ : : ] |
| 7015 | /// [ : ] |
| 7016 | /// \endcode |
| 7017 | /// The array section must be a subset of the original array. |
| 7018 | /// Array sections are allowed on multidimensional arrays. Base language array |
| 7019 | /// subscript expressions can be used to specify length-one dimensions of |
| 7020 | /// multidimensional array sections. |
| 7021 | /// Each of the lower-bound, length, and stride expressions if specified must be |
| 7022 | /// an integral type expressions of the base language. When evaluated |
| 7023 | /// they represent a set of integer values as follows: |
| 7024 | /// \code |
| 7025 | /// { lower-bound, lower-bound + stride, lower-bound + 2 * stride,... , |
| 7026 | /// lower-bound + ((length - 1) * stride) } |
| 7027 | /// \endcode |
| 7028 | /// The lower-bound and length must evaluate to non-negative integers. |
| 7029 | /// The stride must evaluate to a positive integer. |
| 7030 | /// When the size of the array dimension is not known, the length must be |
| 7031 | /// specified explicitly. |
| 7032 | /// When the stride is absent it defaults to 1. |
| 7033 | /// When the length is absent it defaults to ⌈(size − lower-bound)/stride⌉, |
| 7034 | /// where size is the size of the array dimension. When the lower-bound is |
| 7035 | /// absent it defaults to 0. |
| 7036 | /// |
| 7037 | /// |
| 7038 | /// OpenACC 3.3 [2.7.1 Data Specification in Data Clauses] |
| 7039 | /// In C and C++, a subarray is an array name followed by an extended array |
| 7040 | /// range specification in brackets, with start and length, such as |
| 7041 | /// |
| 7042 | /// AA[2:n] |
| 7043 | /// |
| 7044 | /// If the lower bound is missing, zero is used. If the length is missing and |
| 7045 | /// the array has known size, the size of the array is used; otherwise the |
| 7046 | /// length is required. The subarray AA[2:n] means elements AA[2], AA[3], . . . |
| 7047 | /// , AA[2+n-1]. In C and C++, a two dimensional array may be declared in at |
| 7048 | /// least four ways: |
| 7049 | /// |
| 7050 | /// -Statically-sized array: float AA[100][200]; |
| 7051 | /// -Pointer to statically sized rows: typedef float row[200]; row* BB; |
| 7052 | /// -Statically-sized array of pointers: float* CC[200]; |
| 7053 | /// -Pointer to pointers: float** DD; |
| 7054 | /// |
| 7055 | /// Each dimension may be statically sized, or a pointer to dynamically |
| 7056 | /// allocated memory. Each of these may be included in a data clause using |
| 7057 | /// subarray notation to specify a rectangular array: |
| 7058 | /// |
| 7059 | /// -AA[2:n][0:200] |
| 7060 | /// -BB[2:n][0:m] |
| 7061 | /// -CC[2:n][0:m] |
| 7062 | /// -DD[2:n][0:m] |
| 7063 | /// |
| 7064 | /// Multidimensional rectangular subarrays in C and C++ may be specified for any |
| 7065 | /// array with any combination of statically-sized or dynamically-allocated |
| 7066 | /// dimensions. For statically sized dimensions, all dimensions except the first |
| 7067 | /// must specify the whole extent to preserve the contiguous data restriction, |
| 7068 | /// discussed below. For dynamically allocated dimensions, the implementation |
| 7069 | /// will allocate pointers in device memory corresponding to the pointers in |
| 7070 | /// local memory and will fill in those pointers as appropriate. |
| 7071 | /// |
| 7072 | /// In Fortran, a subarray is an array name followed by a comma-separated list |
| 7073 | /// of range specifications in parentheses, with lower and upper bound |
| 7074 | /// subscripts, such as |
| 7075 | /// |
| 7076 | /// arr(1:high,low:100) |
| 7077 | /// |
| 7078 | /// If either the lower or upper bounds are missing, the declared or allocated |
| 7079 | /// bounds of the array, if known, are used. All dimensions except the last must |
| 7080 | /// specify the whole extent, to preserve the contiguous data restriction, |
| 7081 | /// discussed below. |
| 7082 | /// |
| 7083 | /// Restrictions |
| 7084 | /// |
| 7085 | /// -In Fortran, the upper bound for the last dimension of an assumed-size dummy |
| 7086 | /// array must be specified. |
| 7087 | /// |
| 7088 | /// -In C and C++, the length for dynamically allocated dimensions of an array |
| 7089 | /// must be explicitly specified. |
| 7090 | /// |
| 7091 | /// -In C and C++, modifying pointers in pointer arrays during the data |
| 7092 | /// lifetime, either on the host or on the device, may result in undefined |
| 7093 | /// behavior. |
| 7094 | /// |
| 7095 | /// -If a subarray appears in a data clause, the implementation may choose to |
| 7096 | /// allocate memory for only that subarray on the accelerator. |
| 7097 | /// |
| 7098 | /// -In Fortran, array pointers may appear, but pointer association is not |
| 7099 | /// preserved in device memory. |
| 7100 | /// |
| 7101 | /// -Any array or subarray in a data clause, including Fortran array pointers, |
| 7102 | /// must be a contiguous section of memory, except for dynamic multidimensional |
| 7103 | /// C arrays. |
| 7104 | /// |
| 7105 | /// -In C and C++, if a variable or array of composite type appears, all the |
| 7106 | /// data members of the struct or class are allocated and copied, as |
| 7107 | /// appropriate. If a composite member is a pointer type, the data addressed by |
| 7108 | /// that pointer are not implicitly copied. |
| 7109 | /// |
| 7110 | /// -In Fortran, if a variable or array of composite type appears, all the |
| 7111 | /// members of that derived type are allocated and copied, as appropriate. If |
| 7112 | /// any member has the allocatable or pointer attribute, the data accessed |
| 7113 | /// through that member are not copied. |
| 7114 | /// |
| 7115 | /// -If an expression is used in a subscript or subarray expression in a clause |
| 7116 | /// on a data construct, the same value is used when copying data at the end of |
| 7117 | /// the data region, even if the values of variables in the expression change |
| 7118 | /// during the data region. |
| 7119 | class ArraySectionExpr : public Expr { |
| 7120 | friend class ASTStmtReader; |
| 7121 | friend class ASTStmtWriter; |
| 7122 | |
| 7123 | public: |
| 7124 | enum ArraySectionType { OMPArraySection, OpenACCArraySection }; |
| 7125 | |
| 7126 | private: |
| 7127 | enum { |
| 7128 | BASE, |
| 7129 | LOWER_BOUND, |
| 7130 | LENGTH, |
| 7131 | STRIDE, |
| 7132 | END_EXPR, |
| 7133 | OPENACC_END_EXPR = STRIDE |
| 7134 | }; |
| 7135 | |
| 7136 | ArraySectionType ASType = OMPArraySection; |
| 7137 | Stmt *SubExprs[END_EXPR] = {nullptr}; |
| 7138 | SourceLocation ColonLocFirst; |
| 7139 | SourceLocation ColonLocSecond; |
| 7140 | SourceLocation RBracketLoc; |
| 7141 | |
| 7142 | public: |
| 7143 | // Constructor for OMP array sections, which include a 'stride'. |
| 7144 | ArraySectionExpr(Expr *Base, Expr *LowerBound, Expr *Length, Expr *Stride, |
| 7145 | QualType Type, ExprValueKind VK, ExprObjectKind OK, |
| 7146 | SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, |
| 7147 | SourceLocation RBracketLoc) |
| 7148 | : Expr(ArraySectionExprClass, Type, VK, OK), ASType(OMPArraySection), |
| 7149 | ColonLocFirst(ColonLocFirst), ColonLocSecond(ColonLocSecond), |
| 7150 | RBracketLoc(RBracketLoc) { |
| 7151 | setBase(Base); |
| 7152 | setLowerBound(LowerBound); |
| 7153 | setLength(Length); |
| 7154 | setStride(Stride); |
| 7155 | setDependence(computeDependence(E: this)); |
| 7156 | } |
| 7157 | |
| 7158 | // Constructor for OpenACC sub-arrays, which do not permit a 'stride'. |
| 7159 | ArraySectionExpr(Expr *Base, Expr *LowerBound, Expr *Length, QualType Type, |
| 7160 | ExprValueKind VK, ExprObjectKind OK, SourceLocation ColonLoc, |
| 7161 | SourceLocation RBracketLoc) |
| 7162 | : Expr(ArraySectionExprClass, Type, VK, OK), ASType(OpenACCArraySection), |
| 7163 | ColonLocFirst(ColonLoc), RBracketLoc(RBracketLoc) { |
| 7164 | setBase(Base); |
| 7165 | setLowerBound(LowerBound); |
| 7166 | setLength(Length); |
| 7167 | setDependence(computeDependence(E: this)); |
| 7168 | } |
| 7169 | |
| 7170 | /// Create an empty array section expression. |
| 7171 | explicit ArraySectionExpr(EmptyShell Shell) |
| 7172 | : Expr(ArraySectionExprClass, Shell) {} |
| 7173 | |
| 7174 | /// Return original type of the base expression for array section. |
| 7175 | static QualType getBaseOriginalType(const Expr *Base); |
| 7176 | |
| 7177 | static bool classof(const Stmt *T) { |
| 7178 | return T->getStmtClass() == ArraySectionExprClass; |
| 7179 | } |
| 7180 | |
| 7181 | bool isOMPArraySection() const { return ASType == OMPArraySection; } |
| 7182 | bool isOpenACCArraySection() const { return ASType == OpenACCArraySection; } |
| 7183 | |
| 7184 | /// Get base of the array section. |
| 7185 | Expr *getBase() { return cast<Expr>(SubExprs[BASE]); } |
| 7186 | const Expr *getBase() const { return cast<Expr>(SubExprs[BASE]); } |
| 7187 | |
| 7188 | /// Get lower bound of array section. |
| 7189 | Expr *getLowerBound() { return cast_or_null<Expr>(SubExprs[LOWER_BOUND]); } |
| 7190 | const Expr *getLowerBound() const { |
| 7191 | return cast_or_null<Expr>(SubExprs[LOWER_BOUND]); |
| 7192 | } |
| 7193 | |
| 7194 | /// Get length of array section. |
| 7195 | Expr *getLength() { return cast_or_null<Expr>(SubExprs[LENGTH]); } |
| 7196 | const Expr *getLength() const { return cast_or_null<Expr>(SubExprs[LENGTH]); } |
| 7197 | |
| 7198 | /// Get stride of array section. |
| 7199 | Expr *getStride() { |
| 7200 | assert(ASType != OpenACCArraySection && |
| 7201 | "Stride not valid in OpenACC subarrays" ); |
| 7202 | return cast_or_null<Expr>(SubExprs[STRIDE]); |
| 7203 | } |
| 7204 | |
| 7205 | const Expr *getStride() const { |
| 7206 | assert(ASType != OpenACCArraySection && |
| 7207 | "Stride not valid in OpenACC subarrays" ); |
| 7208 | return cast_or_null<Expr>(SubExprs[STRIDE]); |
| 7209 | } |
| 7210 | |
| 7211 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 7212 | return getBase()->getBeginLoc(); |
| 7213 | } |
| 7214 | SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; } |
| 7215 | |
| 7216 | SourceLocation getColonLocFirst() const { return ColonLocFirst; } |
| 7217 | SourceLocation getColonLocSecond() const { |
| 7218 | assert(ASType != OpenACCArraySection && |
| 7219 | "second colon for stride not valid in OpenACC subarrays" ); |
| 7220 | return ColonLocSecond; |
| 7221 | } |
| 7222 | SourceLocation getRBracketLoc() const { return RBracketLoc; } |
| 7223 | |
| 7224 | SourceLocation getExprLoc() const LLVM_READONLY { |
| 7225 | return getBase()->getExprLoc(); |
| 7226 | } |
| 7227 | |
| 7228 | child_range children() { |
| 7229 | return child_range( |
| 7230 | &SubExprs[BASE], |
| 7231 | &SubExprs[ASType == OMPArraySection ? END_EXPR : OPENACC_END_EXPR]); |
| 7232 | } |
| 7233 | |
| 7234 | const_child_range children() const { |
| 7235 | return const_child_range( |
| 7236 | &SubExprs[BASE], |
| 7237 | &SubExprs[ASType == OMPArraySection ? END_EXPR : OPENACC_END_EXPR]); |
| 7238 | } |
| 7239 | |
| 7240 | private: |
| 7241 | /// Set base of the array section. |
| 7242 | void setBase(Expr *E) { SubExprs[BASE] = E; } |
| 7243 | |
| 7244 | /// Set lower bound of the array section. |
| 7245 | void setLowerBound(Expr *E) { SubExprs[LOWER_BOUND] = E; } |
| 7246 | |
| 7247 | /// Set length of the array section. |
| 7248 | void setLength(Expr *E) { SubExprs[LENGTH] = E; } |
| 7249 | |
| 7250 | /// Set length of the array section. |
| 7251 | void setStride(Expr *E) { |
| 7252 | assert(ASType != OpenACCArraySection && |
| 7253 | "Stride not valid in OpenACC subarrays" ); |
| 7254 | SubExprs[STRIDE] = E; |
| 7255 | } |
| 7256 | |
| 7257 | void setColonLocFirst(SourceLocation L) { ColonLocFirst = L; } |
| 7258 | |
| 7259 | void setColonLocSecond(SourceLocation L) { |
| 7260 | assert(ASType != OpenACCArraySection && |
| 7261 | "second colon for stride not valid in OpenACC subarrays" ); |
| 7262 | ColonLocSecond = L; |
| 7263 | } |
| 7264 | void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } |
| 7265 | }; |
| 7266 | |
| 7267 | /// This class represents temporary values used to represent inout and out |
| 7268 | /// arguments in HLSL. From the callee perspective these parameters are more or |
| 7269 | /// less __restrict__ T&. They are guaranteed to not alias any memory. inout |
| 7270 | /// parameters are initialized by the caller, and out parameters are references |
| 7271 | /// to uninitialized memory. |
| 7272 | /// |
| 7273 | /// In the caller, the argument expression creates a temporary in local memory |
| 7274 | /// and the address of the temporary is passed into the callee. There may be |
| 7275 | /// implicit conversion sequences to initialize the temporary, and on expiration |
| 7276 | /// of the temporary an inverse conversion sequence is applied as a write-back |
| 7277 | /// conversion to the source l-value. |
| 7278 | /// |
| 7279 | /// This AST node has three sub-expressions: |
| 7280 | /// - An OpaqueValueExpr with a source that is the argument lvalue expression. |
| 7281 | /// - An OpaqueValueExpr with a source that is an implicit conversion |
| 7282 | /// sequence from the source lvalue to the argument type. |
| 7283 | /// - An expression that assigns the second expression into the first, |
| 7284 | /// performing any necessary conversions. |
| 7285 | class HLSLOutArgExpr : public Expr { |
| 7286 | friend class ASTStmtReader; |
| 7287 | |
| 7288 | enum { |
| 7289 | BaseLValue, |
| 7290 | CastedTemporary, |
| 7291 | WritebackCast, |
| 7292 | NumSubExprs, |
| 7293 | }; |
| 7294 | |
| 7295 | Stmt *SubExprs[NumSubExprs]; |
| 7296 | bool IsInOut; |
| 7297 | |
| 7298 | HLSLOutArgExpr(QualType Ty, OpaqueValueExpr *B, OpaqueValueExpr *OpV, |
| 7299 | Expr *WB, bool IsInOut) |
| 7300 | : Expr(HLSLOutArgExprClass, Ty, VK_LValue, OK_Ordinary), |
| 7301 | IsInOut(IsInOut) { |
| 7302 | SubExprs[BaseLValue] = B; |
| 7303 | SubExprs[CastedTemporary] = OpV; |
| 7304 | SubExprs[WritebackCast] = WB; |
| 7305 | assert(!Ty->isDependentType() && "HLSLOutArgExpr given a dependent type!" ); |
| 7306 | } |
| 7307 | |
| 7308 | explicit HLSLOutArgExpr(EmptyShell Shell) |
| 7309 | : Expr(HLSLOutArgExprClass, Shell) {} |
| 7310 | |
| 7311 | public: |
| 7312 | static HLSLOutArgExpr *Create(const ASTContext &C, QualType Ty, |
| 7313 | OpaqueValueExpr *Base, OpaqueValueExpr *OpV, |
| 7314 | Expr *WB, bool IsInOut); |
| 7315 | static HLSLOutArgExpr *CreateEmpty(const ASTContext &Ctx); |
| 7316 | |
| 7317 | const OpaqueValueExpr *getOpaqueArgLValue() const { |
| 7318 | return cast<OpaqueValueExpr>(SubExprs[BaseLValue]); |
| 7319 | } |
| 7320 | OpaqueValueExpr *getOpaqueArgLValue() { |
| 7321 | return cast<OpaqueValueExpr>(SubExprs[BaseLValue]); |
| 7322 | } |
| 7323 | |
| 7324 | /// Return the l-value expression that was written as the argument |
| 7325 | /// in source. Everything else here is implicitly generated. |
| 7326 | const Expr *getArgLValue() const { |
| 7327 | return getOpaqueArgLValue()->getSourceExpr(); |
| 7328 | } |
| 7329 | Expr *getArgLValue() { return getOpaqueArgLValue()->getSourceExpr(); } |
| 7330 | |
| 7331 | const Expr *getWritebackCast() const { |
| 7332 | return cast<Expr>(SubExprs[WritebackCast]); |
| 7333 | } |
| 7334 | Expr *getWritebackCast() { return cast<Expr>(SubExprs[WritebackCast]); } |
| 7335 | |
| 7336 | const OpaqueValueExpr *getCastedTemporary() const { |
| 7337 | return cast<OpaqueValueExpr>(SubExprs[CastedTemporary]); |
| 7338 | } |
| 7339 | OpaqueValueExpr *getCastedTemporary() { |
| 7340 | return cast<OpaqueValueExpr>(SubExprs[CastedTemporary]); |
| 7341 | } |
| 7342 | |
| 7343 | /// returns true if the parameter is inout and false if the parameter is out. |
| 7344 | bool isInOut() const { return IsInOut; } |
| 7345 | |
| 7346 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 7347 | return SubExprs[BaseLValue]->getBeginLoc(); |
| 7348 | } |
| 7349 | |
| 7350 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 7351 | return SubExprs[BaseLValue]->getEndLoc(); |
| 7352 | } |
| 7353 | |
| 7354 | static bool classof(const Stmt *T) { |
| 7355 | return T->getStmtClass() == HLSLOutArgExprClass; |
| 7356 | } |
| 7357 | |
| 7358 | // Iterators |
| 7359 | child_range children() { |
| 7360 | return child_range(&SubExprs[BaseLValue], &SubExprs[NumSubExprs]); |
| 7361 | } |
| 7362 | }; |
| 7363 | |
| 7364 | /// Frontend produces RecoveryExprs on semantic errors that prevent creating |
| 7365 | /// other well-formed expressions. E.g. when type-checking of a binary operator |
| 7366 | /// fails, we cannot produce a BinaryOperator expression. Instead, we can choose |
| 7367 | /// to produce a recovery expression storing left and right operands. |
| 7368 | /// |
| 7369 | /// RecoveryExpr does not have any semantic meaning in C++, it is only useful to |
| 7370 | /// preserve expressions in AST that would otherwise be dropped. It captures |
| 7371 | /// subexpressions of some expression that we could not construct and source |
| 7372 | /// range covered by the expression. |
| 7373 | /// |
| 7374 | /// By default, RecoveryExpr uses dependence-bits to take advantage of existing |
| 7375 | /// machinery to deal with dependent code in C++, e.g. RecoveryExpr is preserved |
| 7376 | /// in `decltype(<broken-expr>)` as part of the `DependentDecltypeType`. In |
| 7377 | /// addition to that, clang does not report most errors on dependent |
| 7378 | /// expressions, so we get rid of bogus errors for free. However, note that |
| 7379 | /// unlike other dependent expressions, RecoveryExpr can be produced in |
| 7380 | /// non-template contexts. |
| 7381 | /// |
| 7382 | /// We will preserve the type in RecoveryExpr when the type is known, e.g. |
| 7383 | /// preserving the return type for a broken non-overloaded function call, a |
| 7384 | /// overloaded call where all candidates have the same return type. In this |
| 7385 | /// case, the expression is not type-dependent (unless the known type is itself |
| 7386 | /// dependent) |
| 7387 | /// |
| 7388 | /// One can also reliably suppress all bogus errors on expressions containing |
| 7389 | /// recovery expressions by examining results of Expr::containsErrors(). |
| 7390 | class RecoveryExpr final : public Expr, |
| 7391 | private llvm::TrailingObjects<RecoveryExpr, Expr *> { |
| 7392 | public: |
| 7393 | static RecoveryExpr *Create(ASTContext &Ctx, QualType T, |
| 7394 | SourceLocation BeginLoc, SourceLocation EndLoc, |
| 7395 | ArrayRef<Expr *> SubExprs); |
| 7396 | static RecoveryExpr *CreateEmpty(ASTContext &Ctx, unsigned NumSubExprs); |
| 7397 | |
| 7398 | ArrayRef<Expr *> subExpressions() { |
| 7399 | auto *B = getTrailingObjects<Expr *>(); |
| 7400 | return llvm::ArrayRef(B, B + NumExprs); |
| 7401 | } |
| 7402 | |
| 7403 | ArrayRef<const Expr *> subExpressions() const { |
| 7404 | return const_cast<RecoveryExpr *>(this)->subExpressions(); |
| 7405 | } |
| 7406 | |
| 7407 | child_range children() { |
| 7408 | Stmt **B = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>()); |
| 7409 | return child_range(B, B + NumExprs); |
| 7410 | } |
| 7411 | |
| 7412 | SourceLocation getBeginLoc() const { return BeginLoc; } |
| 7413 | SourceLocation getEndLoc() const { return EndLoc; } |
| 7414 | |
| 7415 | static bool classof(const Stmt *T) { |
| 7416 | return T->getStmtClass() == RecoveryExprClass; |
| 7417 | } |
| 7418 | |
| 7419 | private: |
| 7420 | RecoveryExpr(ASTContext &Ctx, QualType T, SourceLocation BeginLoc, |
| 7421 | SourceLocation EndLoc, ArrayRef<Expr *> SubExprs); |
| 7422 | RecoveryExpr(EmptyShell Empty, unsigned NumSubExprs) |
| 7423 | : Expr(RecoveryExprClass, Empty), NumExprs(NumSubExprs) {} |
| 7424 | |
| 7425 | size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumExprs; } |
| 7426 | |
| 7427 | SourceLocation BeginLoc, EndLoc; |
| 7428 | unsigned NumExprs; |
| 7429 | friend TrailingObjects; |
| 7430 | friend class ASTStmtReader; |
| 7431 | friend class ASTStmtWriter; |
| 7432 | }; |
| 7433 | |
| 7434 | /// Insertion operator for diagnostics. This allows sending |
| 7435 | /// Expr into a diagnostic with <<. |
| 7436 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
| 7437 | const Expr *E) { |
| 7438 | DB.AddTaggedVal(V: reinterpret_cast<uint64_t>(E), Kind: DiagnosticsEngine::ak_expr); |
| 7439 | return DB; |
| 7440 | } |
| 7441 | |
| 7442 | } // end namespace clang |
| 7443 | |
| 7444 | #endif // LLVM_CLANG_AST_EXPR_H |
| 7445 | |