| 1 | //===- Type.cpp - Type representation and manipulation --------------------===// |
| 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 implements type-related functionality. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/Type.h" |
| 14 | #include "Linkage.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Attr.h" |
| 17 | #include "clang/AST/CharUnits.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclBase.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/DeclFriend.h" |
| 22 | #include "clang/AST/DeclObjC.h" |
| 23 | #include "clang/AST/DeclTemplate.h" |
| 24 | #include "clang/AST/DependenceFlags.h" |
| 25 | #include "clang/AST/Expr.h" |
| 26 | #include "clang/AST/NestedNameSpecifier.h" |
| 27 | #include "clang/AST/PrettyPrinter.h" |
| 28 | #include "clang/AST/TemplateBase.h" |
| 29 | #include "clang/AST/TemplateName.h" |
| 30 | #include "clang/AST/TypeVisitor.h" |
| 31 | #include "clang/Basic/AddressSpaces.h" |
| 32 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 33 | #include "clang/Basic/IdentifierTable.h" |
| 34 | #include "clang/Basic/LLVM.h" |
| 35 | #include "clang/Basic/LangOptions.h" |
| 36 | #include "clang/Basic/Linkage.h" |
| 37 | #include "clang/Basic/Specifiers.h" |
| 38 | #include "clang/Basic/TargetCXXABI.h" |
| 39 | #include "clang/Basic/TargetInfo.h" |
| 40 | #include "clang/Basic/Visibility.h" |
| 41 | #include "llvm/ADT/APInt.h" |
| 42 | #include "llvm/ADT/APSInt.h" |
| 43 | #include "llvm/ADT/ArrayRef.h" |
| 44 | #include "llvm/ADT/FoldingSet.h" |
| 45 | #include "llvm/ADT/STLExtras.h" |
| 46 | #include "llvm/ADT/SmallVector.h" |
| 47 | #include "llvm/Support/ErrorHandling.h" |
| 48 | #include "llvm/Support/MathExtras.h" |
| 49 | #include <algorithm> |
| 50 | #include <cassert> |
| 51 | #include <cstdint> |
| 52 | #include <cstring> |
| 53 | #include <optional> |
| 54 | |
| 55 | using namespace clang; |
| 56 | |
| 57 | bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const { |
| 58 | return (*this != Other) && |
| 59 | // CVR qualifiers superset |
| 60 | (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) && |
| 61 | // ObjC GC qualifiers superset |
| 62 | ((getObjCGCAttr() == Other.getObjCGCAttr()) || |
| 63 | (hasObjCGCAttr() && !Other.hasObjCGCAttr())) && |
| 64 | // Address space superset. |
| 65 | ((getAddressSpace() == Other.getAddressSpace()) || |
| 66 | (hasAddressSpace() && !Other.hasAddressSpace())) && |
| 67 | // Lifetime qualifier superset. |
| 68 | ((getObjCLifetime() == Other.getObjCLifetime()) || |
| 69 | (hasObjCLifetime() && !Other.hasObjCLifetime())); |
| 70 | } |
| 71 | |
| 72 | bool Qualifiers::isTargetAddressSpaceSupersetOf(LangAS A, LangAS B, |
| 73 | const ASTContext &Ctx) { |
| 74 | // In OpenCLC v2.0 s6.5.5: every address space except for __constant can be |
| 75 | // used as __generic. |
| 76 | return (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
| 77 | // We also define global_device and global_host address spaces, |
| 78 | // to distinguish global pointers allocated on host from pointers |
| 79 | // allocated on device, which are a subset of __global. |
| 80 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || |
| 81 | B == LangAS::opencl_global_host)) || |
| 82 | (A == LangAS::sycl_global && |
| 83 | (B == LangAS::sycl_global_device || B == LangAS::sycl_global_host)) || |
| 84 | // Consider pointer size address spaces to be equivalent to default. |
| 85 | ((isPtrSizeAddressSpace(AS: A) || A == LangAS::Default) && |
| 86 | (isPtrSizeAddressSpace(AS: B) || B == LangAS::Default)) || |
| 87 | // Default is a superset of SYCL address spaces. |
| 88 | (A == LangAS::Default && |
| 89 | (B == LangAS::sycl_private || B == LangAS::sycl_local || |
| 90 | B == LangAS::sycl_global || B == LangAS::sycl_global_device || |
| 91 | B == LangAS::sycl_global_host)) || |
| 92 | // In HIP device compilation, any cuda address space is allowed |
| 93 | // to implicitly cast into the default address space. |
| 94 | (A == LangAS::Default && |
| 95 | (B == LangAS::cuda_constant || B == LangAS::cuda_device || |
| 96 | B == LangAS::cuda_shared)) || |
| 97 | // In HLSL, the this pointer for member functions points to the default |
| 98 | // address space. This causes a problem if the structure is in |
| 99 | // a different address space. We want to allow casting from these |
| 100 | // address spaces to default to work around this problem. |
| 101 | (A == LangAS::Default && B == LangAS::hlsl_private) || |
| 102 | (A == LangAS::Default && B == LangAS::hlsl_device) || |
| 103 | (A == LangAS::Default && B == LangAS::hlsl_input) || |
| 104 | (A == LangAS::Default && B == LangAS::hlsl_output) || |
| 105 | (A == LangAS::Default && B == LangAS::hlsl_push_constant) || |
| 106 | // Conversions from target specific address spaces may be legal |
| 107 | // depending on the target information. |
| 108 | Ctx.getTargetInfo().isAddressSpaceSupersetOf(A, B); |
| 109 | } |
| 110 | |
| 111 | const IdentifierInfo *QualType::getBaseTypeIdentifier() const { |
| 112 | const Type *ty = getTypePtr(); |
| 113 | NamedDecl *ND = nullptr; |
| 114 | if (const auto *DNT = ty->getAs<DependentNameType>()) |
| 115 | return DNT->getIdentifier(); |
| 116 | if (ty->isPointerOrReferenceType()) |
| 117 | return ty->getPointeeType().getBaseTypeIdentifier(); |
| 118 | if (const auto *TT = ty->getAs<TagType>()) |
| 119 | ND = TT->getDecl(); |
| 120 | else if (ty->getTypeClass() == Type::Typedef) |
| 121 | ND = ty->castAs<TypedefType>()->getDecl(); |
| 122 | else if (ty->isArrayType()) |
| 123 | return ty->castAsArrayTypeUnsafe() |
| 124 | ->getElementType() |
| 125 | .getBaseTypeIdentifier(); |
| 126 | |
| 127 | if (ND) |
| 128 | return ND->getIdentifier(); |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | bool QualType::mayBeDynamicClass() const { |
| 133 | const auto *ClassDecl = getTypePtr()->getPointeeCXXRecordDecl(); |
| 134 | return ClassDecl && ClassDecl->mayBeDynamicClass(); |
| 135 | } |
| 136 | |
| 137 | bool QualType::mayBeNotDynamicClass() const { |
| 138 | const auto *ClassDecl = getTypePtr()->getPointeeCXXRecordDecl(); |
| 139 | return !ClassDecl || ClassDecl->mayBeNonDynamicClass(); |
| 140 | } |
| 141 | |
| 142 | bool QualType::isConstant(QualType T, const ASTContext &Ctx) { |
| 143 | if (T.isConstQualified()) |
| 144 | return true; |
| 145 | |
| 146 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) |
| 147 | return AT->getElementType().isConstant(Ctx); |
| 148 | |
| 149 | return T.getAddressSpace() == LangAS::opencl_constant; |
| 150 | } |
| 151 | |
| 152 | std::optional<QualType::NonConstantStorageReason> |
| 153 | QualType::isNonConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, |
| 154 | bool ExcludeDtor) { |
| 155 | if (!isConstant(Ctx) && !(*this)->isReferenceType()) |
| 156 | return NonConstantStorageReason::NonConstNonReferenceType; |
| 157 | if (!Ctx.getLangOpts().CPlusPlus) |
| 158 | return std::nullopt; |
| 159 | if (const CXXRecordDecl *Record = |
| 160 | Ctx.getBaseElementType(QT: *this)->getAsCXXRecordDecl()) { |
| 161 | if (!ExcludeCtor) |
| 162 | return NonConstantStorageReason::NonTrivialCtor; |
| 163 | if (Record->hasMutableFields()) |
| 164 | return NonConstantStorageReason::MutableField; |
| 165 | if (!Record->hasTrivialDestructor() && !ExcludeDtor) |
| 166 | return NonConstantStorageReason::NonTrivialDtor; |
| 167 | } |
| 168 | return std::nullopt; |
| 169 | } |
| 170 | |
| 171 | // C++ [temp.dep.type]p1: |
| 172 | // A type is dependent if it is... |
| 173 | // - an array type constructed from any dependent type or whose |
| 174 | // size is specified by a constant expression that is |
| 175 | // value-dependent, |
| 176 | ArrayType::ArrayType(TypeClass tc, QualType et, QualType can, |
| 177 | ArraySizeModifier sm, unsigned tq, const Expr *sz) |
| 178 | // Note, we need to check for DependentSizedArrayType explicitly here |
| 179 | // because we use a DependentSizedArrayType with no size expression as the |
| 180 | // type of a dependent array of unknown bound with a dependent braced |
| 181 | // initializer: |
| 182 | // |
| 183 | // template<int ...N> int arr[] = {N...}; |
| 184 | : Type(tc, can, |
| 185 | et->getDependence() | |
| 186 | (sz ? toTypeDependence( |
| 187 | D: turnValueToTypeDependence(D: sz->getDependence())) |
| 188 | : TypeDependence::None) | |
| 189 | (tc == VariableArray ? TypeDependence::VariablyModified |
| 190 | : TypeDependence::None) | |
| 191 | (tc == DependentSizedArray |
| 192 | ? TypeDependence::DependentInstantiation |
| 193 | : TypeDependence::None)), |
| 194 | ElementType(et) { |
| 195 | ArrayTypeBits.IndexTypeQuals = tq; |
| 196 | ArrayTypeBits.SizeModifier = llvm::to_underlying(E: sm); |
| 197 | } |
| 198 | |
| 199 | ConstantArrayType * |
| 200 | ConstantArrayType::Create(const ASTContext &Ctx, QualType ET, QualType Can, |
| 201 | const llvm::APInt &Sz, const Expr *SzExpr, |
| 202 | ArraySizeModifier SzMod, unsigned Qual) { |
| 203 | bool NeedsExternalSize = SzExpr != nullptr || Sz.ugt(RHS: 0x0FFFFFFFFFFFFFFF) || |
| 204 | Sz.getBitWidth() > 0xFF; |
| 205 | if (!NeedsExternalSize) |
| 206 | return new (Ctx, alignof(ConstantArrayType)) ConstantArrayType( |
| 207 | ET, Can, Sz.getBitWidth(), Sz.getZExtValue(), SzMod, Qual); |
| 208 | |
| 209 | auto *SzPtr = new (Ctx, alignof(ConstantArrayType::ExternalSize)) |
| 210 | ConstantArrayType::ExternalSize(Sz, SzExpr); |
| 211 | return new (Ctx, alignof(ConstantArrayType)) |
| 212 | ConstantArrayType(ET, Can, SzPtr, SzMod, Qual); |
| 213 | } |
| 214 | |
| 215 | unsigned |
| 216 | ConstantArrayType::getNumAddressingBits(const ASTContext &Context, |
| 217 | QualType ElementType, |
| 218 | const llvm::APInt &NumElements) { |
| 219 | uint64_t ElementSize = Context.getTypeSizeInChars(T: ElementType).getQuantity(); |
| 220 | |
| 221 | // Fast path the common cases so we can avoid the conservative computation |
| 222 | // below, which in common cases allocates "large" APSInt values, which are |
| 223 | // slow. |
| 224 | |
| 225 | // If the element size is a power of 2, we can directly compute the additional |
| 226 | // number of addressing bits beyond those required for the element count. |
| 227 | if (llvm::isPowerOf2_64(Value: ElementSize)) { |
| 228 | return NumElements.getActiveBits() + llvm::Log2_64(Value: ElementSize); |
| 229 | } |
| 230 | |
| 231 | // If both the element count and element size fit in 32-bits, we can do the |
| 232 | // computation directly in 64-bits. |
| 233 | if ((ElementSize >> 32) == 0 && NumElements.getBitWidth() <= 64 && |
| 234 | (NumElements.getZExtValue() >> 32) == 0) { |
| 235 | uint64_t TotalSize = NumElements.getZExtValue() * ElementSize; |
| 236 | return llvm::bit_width(Value: TotalSize); |
| 237 | } |
| 238 | |
| 239 | // Otherwise, use APSInt to handle arbitrary sized values. |
| 240 | llvm::APSInt SizeExtended(NumElements, true); |
| 241 | unsigned SizeTypeBits = Context.getTypeSize(T: Context.getSizeType()); |
| 242 | SizeExtended = SizeExtended.extend( |
| 243 | width: std::max(a: SizeTypeBits, b: SizeExtended.getBitWidth()) * 2); |
| 244 | |
| 245 | llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize)); |
| 246 | TotalSize *= SizeExtended; |
| 247 | |
| 248 | return TotalSize.getActiveBits(); |
| 249 | } |
| 250 | |
| 251 | unsigned |
| 252 | ConstantArrayType::getNumAddressingBits(const ASTContext &Context) const { |
| 253 | return getNumAddressingBits(Context, ElementType: getElementType(), NumElements: getSize()); |
| 254 | } |
| 255 | |
| 256 | unsigned ConstantArrayType::getMaxSizeBits(const ASTContext &Context) { |
| 257 | unsigned Bits = Context.getTypeSize(T: Context.getSizeType()); |
| 258 | |
| 259 | // Limit the number of bits in size_t so that maximal bit size fits 64 bit |
| 260 | // integer (see PR8256). We can do this as currently there is no hardware |
| 261 | // that supports full 64-bit virtual space. |
| 262 | if (Bits > 61) |
| 263 | Bits = 61; |
| 264 | |
| 265 | return Bits; |
| 266 | } |
| 267 | |
| 268 | void ConstantArrayType::Profile(llvm::FoldingSetNodeID &ID, |
| 269 | const ASTContext &Context, QualType ET, |
| 270 | uint64_t ArraySize, const Expr *SizeExpr, |
| 271 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
| 272 | ID.AddPointer(Ptr: ET.getAsOpaquePtr()); |
| 273 | ID.AddInteger(I: ArraySize); |
| 274 | ID.AddInteger(I: llvm::to_underlying(E: SizeMod)); |
| 275 | ID.AddInteger(I: TypeQuals); |
| 276 | ID.AddBoolean(B: SizeExpr != nullptr); |
| 277 | if (SizeExpr) |
| 278 | SizeExpr->Profile(ID, Context, Canonical: true); |
| 279 | } |
| 280 | |
| 281 | QualType ArrayParameterType::getConstantArrayType(const ASTContext &Ctx) const { |
| 282 | return Ctx.getConstantArrayType(EltTy: getElementType(), ArySize: getSize(), SizeExpr: getSizeExpr(), |
| 283 | ASM: getSizeModifier(), |
| 284 | IndexTypeQuals: getIndexTypeQualifiers().getAsOpaqueValue()); |
| 285 | } |
| 286 | |
| 287 | DependentSizedArrayType::DependentSizedArrayType(QualType et, QualType can, |
| 288 | Expr *e, ArraySizeModifier sm, |
| 289 | unsigned tq) |
| 290 | : ArrayType(DependentSizedArray, et, can, sm, tq, e), SizeExpr((Stmt *)e) {} |
| 291 | |
| 292 | void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, |
| 293 | const ASTContext &Context, QualType ET, |
| 294 | ArraySizeModifier SizeMod, |
| 295 | unsigned TypeQuals, Expr *E) { |
| 296 | ID.AddPointer(Ptr: ET.getAsOpaquePtr()); |
| 297 | ID.AddInteger(I: llvm::to_underlying(E: SizeMod)); |
| 298 | ID.AddInteger(I: TypeQuals); |
| 299 | if (E) |
| 300 | E->Profile(ID, Context, Canonical: true); |
| 301 | } |
| 302 | |
| 303 | DependentVectorType::DependentVectorType(QualType ElementType, |
| 304 | QualType CanonType, Expr *SizeExpr, |
| 305 | SourceLocation Loc, VectorKind VecKind) |
| 306 | : Type(DependentVector, CanonType, |
| 307 | TypeDependence::DependentInstantiation | |
| 308 | ElementType->getDependence() | |
| 309 | (SizeExpr ? toTypeDependence(D: SizeExpr->getDependence()) |
| 310 | : TypeDependence::None)), |
| 311 | ElementType(ElementType), SizeExpr(SizeExpr), Loc(Loc) { |
| 312 | VectorTypeBits.VecKind = llvm::to_underlying(E: VecKind); |
| 313 | } |
| 314 | |
| 315 | void DependentVectorType::Profile(llvm::FoldingSetNodeID &ID, |
| 316 | const ASTContext &Context, |
| 317 | QualType ElementType, const Expr *SizeExpr, |
| 318 | VectorKind VecKind) { |
| 319 | ID.AddPointer(Ptr: ElementType.getAsOpaquePtr()); |
| 320 | ID.AddInteger(I: llvm::to_underlying(E: VecKind)); |
| 321 | SizeExpr->Profile(ID, Context, Canonical: true); |
| 322 | } |
| 323 | |
| 324 | DependentSizedExtVectorType::DependentSizedExtVectorType(QualType ElementType, |
| 325 | QualType can, |
| 326 | Expr *SizeExpr, |
| 327 | SourceLocation loc) |
| 328 | : Type(DependentSizedExtVector, can, |
| 329 | TypeDependence::DependentInstantiation | |
| 330 | ElementType->getDependence() | |
| 331 | (SizeExpr ? toTypeDependence(D: SizeExpr->getDependence()) |
| 332 | : TypeDependence::None)), |
| 333 | SizeExpr(SizeExpr), ElementType(ElementType), loc(loc) {} |
| 334 | |
| 335 | void DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, |
| 336 | const ASTContext &Context, |
| 337 | QualType ElementType, |
| 338 | Expr *SizeExpr) { |
| 339 | ID.AddPointer(Ptr: ElementType.getAsOpaquePtr()); |
| 340 | SizeExpr->Profile(ID, Context, Canonical: true); |
| 341 | } |
| 342 | |
| 343 | DependentAddressSpaceType::DependentAddressSpaceType(QualType PointeeType, |
| 344 | QualType can, |
| 345 | Expr *AddrSpaceExpr, |
| 346 | SourceLocation loc) |
| 347 | : Type(DependentAddressSpace, can, |
| 348 | TypeDependence::DependentInstantiation | |
| 349 | PointeeType->getDependence() | |
| 350 | (AddrSpaceExpr ? toTypeDependence(D: AddrSpaceExpr->getDependence()) |
| 351 | : TypeDependence::None)), |
| 352 | AddrSpaceExpr(AddrSpaceExpr), PointeeType(PointeeType), loc(loc) {} |
| 353 | |
| 354 | void DependentAddressSpaceType::Profile(llvm::FoldingSetNodeID &ID, |
| 355 | const ASTContext &Context, |
| 356 | QualType PointeeType, |
| 357 | Expr *AddrSpaceExpr) { |
| 358 | ID.AddPointer(Ptr: PointeeType.getAsOpaquePtr()); |
| 359 | AddrSpaceExpr->Profile(ID, Context, Canonical: true); |
| 360 | } |
| 361 | |
| 362 | MatrixType::MatrixType(TypeClass tc, QualType matrixType, QualType canonType, |
| 363 | const Expr *RowExpr, const Expr *ColumnExpr) |
| 364 | : Type(tc, canonType, |
| 365 | (RowExpr ? (matrixType->getDependence() | TypeDependence::Dependent | |
| 366 | TypeDependence::Instantiation | |
| 367 | (matrixType->isVariablyModifiedType() |
| 368 | ? TypeDependence::VariablyModified |
| 369 | : TypeDependence::None) | |
| 370 | (matrixType->containsUnexpandedParameterPack() || |
| 371 | (RowExpr && |
| 372 | RowExpr->containsUnexpandedParameterPack()) || |
| 373 | (ColumnExpr && |
| 374 | ColumnExpr->containsUnexpandedParameterPack()) |
| 375 | ? TypeDependence::UnexpandedPack |
| 376 | : TypeDependence::None)) |
| 377 | : matrixType->getDependence())), |
| 378 | ElementType(matrixType) {} |
| 379 | |
| 380 | ConstantMatrixType::ConstantMatrixType(QualType matrixType, unsigned nRows, |
| 381 | unsigned nColumns, QualType canonType) |
| 382 | : ConstantMatrixType(ConstantMatrix, matrixType, nRows, nColumns, |
| 383 | canonType) {} |
| 384 | |
| 385 | ConstantMatrixType::ConstantMatrixType(TypeClass tc, QualType matrixType, |
| 386 | unsigned nRows, unsigned nColumns, |
| 387 | QualType canonType) |
| 388 | : MatrixType(tc, matrixType, canonType), NumRows(nRows), |
| 389 | NumColumns(nColumns) {} |
| 390 | |
| 391 | DependentSizedMatrixType::DependentSizedMatrixType(QualType ElementType, |
| 392 | QualType CanonicalType, |
| 393 | Expr *RowExpr, |
| 394 | Expr *ColumnExpr, |
| 395 | SourceLocation loc) |
| 396 | : MatrixType(DependentSizedMatrix, ElementType, CanonicalType, RowExpr, |
| 397 | ColumnExpr), |
| 398 | RowExpr(RowExpr), ColumnExpr(ColumnExpr), loc(loc) {} |
| 399 | |
| 400 | void DependentSizedMatrixType::Profile(llvm::FoldingSetNodeID &ID, |
| 401 | const ASTContext &CTX, |
| 402 | QualType ElementType, Expr *RowExpr, |
| 403 | Expr *ColumnExpr) { |
| 404 | ID.AddPointer(Ptr: ElementType.getAsOpaquePtr()); |
| 405 | RowExpr->Profile(ID, Context: CTX, Canonical: true); |
| 406 | ColumnExpr->Profile(ID, Context: CTX, Canonical: true); |
| 407 | } |
| 408 | |
| 409 | VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType, |
| 410 | VectorKind vecKind) |
| 411 | : VectorType(Vector, vecType, nElements, canonType, vecKind) {} |
| 412 | |
| 413 | VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
| 414 | QualType canonType, VectorKind vecKind) |
| 415 | : Type(tc, canonType, vecType->getDependence()), ElementType(vecType) { |
| 416 | VectorTypeBits.VecKind = llvm::to_underlying(E: vecKind); |
| 417 | VectorTypeBits.NumElements = nElements; |
| 418 | } |
| 419 | |
| 420 | bool Type::isPackedVectorBoolType(const ASTContext &ctx) const { |
| 421 | if (ctx.getLangOpts().HLSL) |
| 422 | return false; |
| 423 | return isExtVectorBoolType(); |
| 424 | } |
| 425 | |
| 426 | BitIntType::BitIntType(bool IsUnsigned, unsigned NumBits) |
| 427 | : Type(BitInt, QualType{}, TypeDependence::None), IsUnsigned(IsUnsigned), |
| 428 | NumBits(NumBits) {} |
| 429 | |
| 430 | DependentBitIntType::DependentBitIntType(bool IsUnsigned, Expr *NumBitsExpr) |
| 431 | : Type(DependentBitInt, QualType{}, |
| 432 | toTypeDependence(D: NumBitsExpr->getDependence())), |
| 433 | ExprAndUnsigned(NumBitsExpr, IsUnsigned) {} |
| 434 | |
| 435 | bool DependentBitIntType::isUnsigned() const { |
| 436 | return ExprAndUnsigned.getInt(); |
| 437 | } |
| 438 | |
| 439 | clang::Expr *DependentBitIntType::getNumBitsExpr() const { |
| 440 | return ExprAndUnsigned.getPointer(); |
| 441 | } |
| 442 | |
| 443 | void DependentBitIntType::Profile(llvm::FoldingSetNodeID &ID, |
| 444 | const ASTContext &Context, bool IsUnsigned, |
| 445 | Expr *NumBitsExpr) { |
| 446 | ID.AddBoolean(B: IsUnsigned); |
| 447 | NumBitsExpr->Profile(ID, Context, Canonical: true); |
| 448 | } |
| 449 | |
| 450 | bool BoundsAttributedType::referencesFieldDecls() const { |
| 451 | return llvm::any_of(Range: dependent_decls(), |
| 452 | P: [](const TypeCoupledDeclRefInfo &Info) { |
| 453 | return isa<FieldDecl>(Val: Info.getDecl()); |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | void CountAttributedType::Profile(llvm::FoldingSetNodeID &ID, |
| 458 | QualType WrappedTy, Expr *CountExpr, |
| 459 | bool CountInBytes, bool OrNull) { |
| 460 | ID.AddPointer(Ptr: WrappedTy.getAsOpaquePtr()); |
| 461 | ID.AddBoolean(B: CountInBytes); |
| 462 | ID.AddBoolean(B: OrNull); |
| 463 | // We profile it as a pointer as the StmtProfiler considers parameter |
| 464 | // expressions on function declaration and function definition as the |
| 465 | // same, resulting in count expression being evaluated with ParamDecl |
| 466 | // not in the function scope. |
| 467 | ID.AddPointer(Ptr: CountExpr); |
| 468 | } |
| 469 | |
| 470 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 471 | /// element type of the array, potentially with type qualifiers missing. |
| 472 | /// This method should never be used when type qualifiers are meaningful. |
| 473 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 474 | // If this is directly an array type, return it. |
| 475 | if (const auto *ATy = dyn_cast<ArrayType>(Val: this)) |
| 476 | return ATy->getElementType().getTypePtr(); |
| 477 | |
| 478 | // If the canonical form of this type isn't the right kind, reject it. |
| 479 | if (!isa<ArrayType>(Val: CanonicalType)) |
| 480 | return nullptr; |
| 481 | |
| 482 | // If this is a typedef for an array type, strip the typedef off without |
| 483 | // losing all typedef information. |
| 484 | return cast<ArrayType>(Val: getUnqualifiedDesugaredType()) |
| 485 | ->getElementType() |
| 486 | .getTypePtr(); |
| 487 | } |
| 488 | |
| 489 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 490 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
| 491 | /// the type is already concrete, it returns it unmodified. This is similar |
| 492 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 493 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
| 494 | /// concrete. |
| 495 | QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) { |
| 496 | SplitQualType split = getSplitDesugaredType(T); |
| 497 | return Context.getQualifiedType(T: split.Ty, Qs: split.Quals); |
| 498 | } |
| 499 | |
| 500 | QualType QualType::getSingleStepDesugaredTypeImpl(QualType type, |
| 501 | const ASTContext &Context) { |
| 502 | SplitQualType split = type.split(); |
| 503 | QualType desugar = split.Ty->getLocallyUnqualifiedSingleStepDesugaredType(); |
| 504 | return Context.getQualifiedType(T: desugar, Qs: split.Quals); |
| 505 | } |
| 506 | |
| 507 | // Check that no type class is polymorphic. LLVM style RTTI should be used |
| 508 | // instead. If absolutely needed an exception can still be added here by |
| 509 | // defining the appropriate macro (but please don't do this). |
| 510 | #define TYPE(CLASS, BASE) \ |
| 511 | static_assert(!std::is_polymorphic<CLASS##Type>::value, \ |
| 512 | #CLASS "Type should not be polymorphic!"); |
| 513 | #include "clang/AST/TypeNodes.inc" |
| 514 | |
| 515 | // Check that no type class has a non-trival destructor. Types are |
| 516 | // allocated with the BumpPtrAllocator from ASTContext and therefore |
| 517 | // their destructor is not executed. |
| 518 | #define TYPE(CLASS, BASE) \ |
| 519 | static_assert(std::is_trivially_destructible<CLASS##Type>::value, \ |
| 520 | #CLASS "Type should be trivially destructible!"); |
| 521 | #include "clang/AST/TypeNodes.inc" |
| 522 | |
| 523 | QualType Type::getLocallyUnqualifiedSingleStepDesugaredType() const { |
| 524 | switch (getTypeClass()) { |
| 525 | #define ABSTRACT_TYPE(Class, Parent) |
| 526 | #define TYPE(Class, Parent) \ |
| 527 | case Type::Class: { \ |
| 528 | const auto *ty = cast<Class##Type>(this); \ |
| 529 | if (!ty->isSugared()) \ |
| 530 | return QualType(ty, 0); \ |
| 531 | return ty->desugar(); \ |
| 532 | } |
| 533 | #include "clang/AST/TypeNodes.inc" |
| 534 | } |
| 535 | llvm_unreachable("bad type kind!" ); |
| 536 | } |
| 537 | |
| 538 | SplitQualType QualType::getSplitDesugaredType(QualType T) { |
| 539 | QualifierCollector Qs; |
| 540 | |
| 541 | QualType Cur = T; |
| 542 | while (true) { |
| 543 | const Type *CurTy = Qs.strip(type: Cur); |
| 544 | switch (CurTy->getTypeClass()) { |
| 545 | #define ABSTRACT_TYPE(Class, Parent) |
| 546 | #define TYPE(Class, Parent) \ |
| 547 | case Type::Class: { \ |
| 548 | const auto *Ty = cast<Class##Type>(CurTy); \ |
| 549 | if (!Ty->isSugared()) \ |
| 550 | return SplitQualType(Ty, Qs); \ |
| 551 | Cur = Ty->desugar(); \ |
| 552 | break; \ |
| 553 | } |
| 554 | #include "clang/AST/TypeNodes.inc" |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) { |
| 560 | SplitQualType split = type.split(); |
| 561 | |
| 562 | // All the qualifiers we've seen so far. |
| 563 | Qualifiers quals = split.Quals; |
| 564 | |
| 565 | // The last type node we saw with any nodes inside it. |
| 566 | const Type *lastTypeWithQuals = split.Ty; |
| 567 | |
| 568 | while (true) { |
| 569 | QualType next; |
| 570 | |
| 571 | // Do a single-step desugar, aborting the loop if the type isn't |
| 572 | // sugared. |
| 573 | switch (split.Ty->getTypeClass()) { |
| 574 | #define ABSTRACT_TYPE(Class, Parent) |
| 575 | #define TYPE(Class, Parent) \ |
| 576 | case Type::Class: { \ |
| 577 | const auto *ty = cast<Class##Type>(split.Ty); \ |
| 578 | if (!ty->isSugared()) \ |
| 579 | goto done; \ |
| 580 | next = ty->desugar(); \ |
| 581 | break; \ |
| 582 | } |
| 583 | #include "clang/AST/TypeNodes.inc" |
| 584 | } |
| 585 | |
| 586 | // Otherwise, split the underlying type. If that yields qualifiers, |
| 587 | // update the information. |
| 588 | split = next.split(); |
| 589 | if (!split.Quals.empty()) { |
| 590 | lastTypeWithQuals = split.Ty; |
| 591 | quals.addConsistentQualifiers(qs: split.Quals); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | done: |
| 596 | return SplitQualType(lastTypeWithQuals, quals); |
| 597 | } |
| 598 | |
| 599 | QualType QualType::IgnoreParens(QualType T) { |
| 600 | // FIXME: this seems inherently un-qualifiers-safe. |
| 601 | while (const auto *PT = T->getAs<ParenType>()) |
| 602 | T = PT->getInnerType(); |
| 603 | return T; |
| 604 | } |
| 605 | |
| 606 | /// This will check for a T (which should be a Type which can act as |
| 607 | /// sugar, such as a TypedefType) by removing any existing sugar until it |
| 608 | /// reaches a T or a non-sugared type. |
| 609 | template <typename T> static const T *getAsSugar(const Type *Cur) { |
| 610 | while (true) { |
| 611 | if (const auto *Sugar = dyn_cast<T>(Cur)) |
| 612 | return Sugar; |
| 613 | switch (Cur->getTypeClass()) { |
| 614 | #define ABSTRACT_TYPE(Class, Parent) |
| 615 | #define TYPE(Class, Parent) \ |
| 616 | case Type::Class: { \ |
| 617 | const auto *Ty = cast<Class##Type>(Cur); \ |
| 618 | if (!Ty->isSugared()) \ |
| 619 | return 0; \ |
| 620 | Cur = Ty->desugar().getTypePtr(); \ |
| 621 | break; \ |
| 622 | } |
| 623 | #include "clang/AST/TypeNodes.inc" |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | template <> const TypedefType *Type::getAs() const { |
| 629 | return getAsSugar<TypedefType>(Cur: this); |
| 630 | } |
| 631 | |
| 632 | template <> const UsingType *Type::getAs() const { |
| 633 | return getAsSugar<UsingType>(Cur: this); |
| 634 | } |
| 635 | |
| 636 | template <> const TemplateSpecializationType *Type::getAs() const { |
| 637 | return getAsSugar<TemplateSpecializationType>(Cur: this); |
| 638 | } |
| 639 | |
| 640 | template <> const AttributedType *Type::getAs() const { |
| 641 | return getAsSugar<AttributedType>(Cur: this); |
| 642 | } |
| 643 | |
| 644 | template <> const BoundsAttributedType *Type::getAs() const { |
| 645 | return getAsSugar<BoundsAttributedType>(Cur: this); |
| 646 | } |
| 647 | |
| 648 | template <> const CountAttributedType *Type::getAs() const { |
| 649 | return getAsSugar<CountAttributedType>(Cur: this); |
| 650 | } |
| 651 | |
| 652 | /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic |
| 653 | /// sugar off the given type. This should produce an object of the |
| 654 | /// same dynamic type as the canonical type. |
| 655 | const Type *Type::getUnqualifiedDesugaredType() const { |
| 656 | const Type *Cur = this; |
| 657 | |
| 658 | while (true) { |
| 659 | switch (Cur->getTypeClass()) { |
| 660 | #define ABSTRACT_TYPE(Class, Parent) |
| 661 | #define TYPE(Class, Parent) \ |
| 662 | case Class: { \ |
| 663 | const auto *Ty = cast<Class##Type>(Cur); \ |
| 664 | if (!Ty->isSugared()) \ |
| 665 | return Cur; \ |
| 666 | Cur = Ty->desugar().getTypePtr(); \ |
| 667 | break; \ |
| 668 | } |
| 669 | #include "clang/AST/TypeNodes.inc" |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | bool Type::isClassType() const { |
| 675 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 676 | return RT->getDecl()->isClass(); |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | bool Type::isStructureType() const { |
| 681 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 682 | return RT->getDecl()->isStruct(); |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | bool Type::isStructureTypeWithFlexibleArrayMember() const { |
| 687 | const auto *RT = getAsCanonical<RecordType>(); |
| 688 | if (!RT) |
| 689 | return false; |
| 690 | const auto *Decl = RT->getDecl(); |
| 691 | if (!Decl->isStruct()) |
| 692 | return false; |
| 693 | return Decl->getDefinitionOrSelf()->hasFlexibleArrayMember(); |
| 694 | } |
| 695 | |
| 696 | bool Type::isObjCBoxableRecordType() const { |
| 697 | if (const auto *RD = getAsRecordDecl()) |
| 698 | return RD->hasAttr<ObjCBoxableAttr>(); |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | bool Type::isInterfaceType() const { |
| 703 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 704 | return RT->getDecl()->isInterface(); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | bool Type::isStructureOrClassType() const { |
| 709 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 710 | return RT->getDecl()->isStructureOrClass(); |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | bool Type::isVoidPointerType() const { |
| 715 | if (const auto *PT = getAsCanonical<PointerType>()) |
| 716 | return PT->getPointeeType()->isVoidType(); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | bool Type::isUnionType() const { |
| 721 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 722 | return RT->getDecl()->isUnion(); |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | bool Type::isComplexType() const { |
| 727 | if (const auto *CT = getAsCanonical<ComplexType>()) |
| 728 | return CT->getElementType()->isFloatingType(); |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | bool Type::isComplexIntegerType() const { |
| 733 | // Check for GCC complex integer extension. |
| 734 | return getAsComplexIntegerType(); |
| 735 | } |
| 736 | |
| 737 | bool Type::isScopedEnumeralType() const { |
| 738 | if (const auto *ET = getAsCanonical<EnumType>()) |
| 739 | return ET->getDecl()->isScoped(); |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | bool Type::isCountAttributedType() const { |
| 744 | return getAs<CountAttributedType>(); |
| 745 | } |
| 746 | |
| 747 | const ComplexType *Type::getAsComplexIntegerType() const { |
| 748 | if (const auto *Complex = getAs<ComplexType>()) |
| 749 | if (Complex->getElementType()->isIntegerType()) |
| 750 | return Complex; |
| 751 | return nullptr; |
| 752 | } |
| 753 | |
| 754 | QualType Type::getPointeeType() const { |
| 755 | if (const auto *PT = getAs<PointerType>()) |
| 756 | return PT->getPointeeType(); |
| 757 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
| 758 | return OPT->getPointeeType(); |
| 759 | if (const auto *BPT = getAs<BlockPointerType>()) |
| 760 | return BPT->getPointeeType(); |
| 761 | if (const auto *RT = getAs<ReferenceType>()) |
| 762 | return RT->getPointeeType(); |
| 763 | if (const auto *MPT = getAs<MemberPointerType>()) |
| 764 | return MPT->getPointeeType(); |
| 765 | if (const auto *DT = getAs<DecayedType>()) |
| 766 | return DT->getPointeeType(); |
| 767 | return {}; |
| 768 | } |
| 769 | |
| 770 | const RecordType *Type::getAsStructureType() const { |
| 771 | // If this is directly a structure type, return it. |
| 772 | if (const auto *RT = dyn_cast<RecordType>(Val: this)) { |
| 773 | if (RT->getDecl()->isStruct()) |
| 774 | return RT; |
| 775 | } |
| 776 | |
| 777 | // If the canonical form of this type isn't the right kind, reject it. |
| 778 | if (const auto *RT = dyn_cast<RecordType>(Val: CanonicalType)) { |
| 779 | if (!RT->getDecl()->isStruct()) |
| 780 | return nullptr; |
| 781 | |
| 782 | // If this is a typedef for a structure type, strip the typedef off without |
| 783 | // losing all typedef information. |
| 784 | return cast<RecordType>(Val: getUnqualifiedDesugaredType()); |
| 785 | } |
| 786 | return nullptr; |
| 787 | } |
| 788 | |
| 789 | const RecordType *Type::getAsUnionType() const { |
| 790 | // If this is directly a union type, return it. |
| 791 | if (const auto *RT = dyn_cast<RecordType>(Val: this)) { |
| 792 | if (RT->getDecl()->isUnion()) |
| 793 | return RT; |
| 794 | } |
| 795 | |
| 796 | // If the canonical form of this type isn't the right kind, reject it. |
| 797 | if (const auto *RT = dyn_cast<RecordType>(Val: CanonicalType)) { |
| 798 | if (!RT->getDecl()->isUnion()) |
| 799 | return nullptr; |
| 800 | |
| 801 | // If this is a typedef for a union type, strip the typedef off without |
| 802 | // losing all typedef information. |
| 803 | return cast<RecordType>(Val: getUnqualifiedDesugaredType()); |
| 804 | } |
| 805 | |
| 806 | return nullptr; |
| 807 | } |
| 808 | |
| 809 | bool Type::isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
| 810 | const ObjCObjectType *&bound) const { |
| 811 | bound = nullptr; |
| 812 | |
| 813 | const auto *OPT = getAs<ObjCObjectPointerType>(); |
| 814 | if (!OPT) |
| 815 | return false; |
| 816 | |
| 817 | // Easy case: id. |
| 818 | if (OPT->isObjCIdType()) |
| 819 | return true; |
| 820 | |
| 821 | // If it's not a __kindof type, reject it now. |
| 822 | if (!OPT->isKindOfType()) |
| 823 | return false; |
| 824 | |
| 825 | // If it's Class or qualified Class, it's not an object type. |
| 826 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) |
| 827 | return false; |
| 828 | |
| 829 | // Figure out the type bound for the __kindof type. |
| 830 | bound = OPT->getObjectType() |
| 831 | ->stripObjCKindOfTypeAndQuals(ctx) |
| 832 | ->getAs<ObjCObjectType>(); |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | bool Type::isObjCClassOrClassKindOfType() const { |
| 837 | const auto *OPT = getAs<ObjCObjectPointerType>(); |
| 838 | if (!OPT) |
| 839 | return false; |
| 840 | |
| 841 | // Easy case: Class. |
| 842 | if (OPT->isObjCClassType()) |
| 843 | return true; |
| 844 | |
| 845 | // If it's not a __kindof type, reject it now. |
| 846 | if (!OPT->isKindOfType()) |
| 847 | return false; |
| 848 | |
| 849 | // If it's Class or qualified Class, it's a class __kindof type. |
| 850 | return OPT->isObjCClassType() || OPT->isObjCQualifiedClassType(); |
| 851 | } |
| 852 | |
| 853 | ObjCTypeParamType::ObjCTypeParamType(const ObjCTypeParamDecl *D, QualType can, |
| 854 | ArrayRef<ObjCProtocolDecl *> protocols) |
| 855 | : Type(ObjCTypeParam, can, toSemanticDependence(D: can->getDependence())), |
| 856 | OTPDecl(const_cast<ObjCTypeParamDecl *>(D)) { |
| 857 | initialize(protocols); |
| 858 | } |
| 859 | |
| 860 | ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base, |
| 861 | ArrayRef<QualType> typeArgs, |
| 862 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 863 | bool isKindOf) |
| 864 | : Type(ObjCObject, Canonical, Base->getDependence()), BaseType(Base) { |
| 865 | ObjCObjectTypeBits.IsKindOf = isKindOf; |
| 866 | |
| 867 | ObjCObjectTypeBits.NumTypeArgs = typeArgs.size(); |
| 868 | assert(getTypeArgsAsWritten().size() == typeArgs.size() && |
| 869 | "bitfield overflow in type argument count" ); |
| 870 | if (!typeArgs.empty()) |
| 871 | memcpy(dest: getTypeArgStorage(), src: typeArgs.data(), |
| 872 | n: typeArgs.size() * sizeof(QualType)); |
| 873 | |
| 874 | for (auto typeArg : typeArgs) { |
| 875 | addDependence(D: typeArg->getDependence() & ~TypeDependence::VariablyModified); |
| 876 | } |
| 877 | // Initialize the protocol qualifiers. The protocol storage is known |
| 878 | // after we set number of type arguments. |
| 879 | initialize(protocols); |
| 880 | } |
| 881 | |
| 882 | bool ObjCObjectType::isSpecialized() const { |
| 883 | // If we have type arguments written here, the type is specialized. |
| 884 | if (ObjCObjectTypeBits.NumTypeArgs > 0) |
| 885 | return true; |
| 886 | |
| 887 | // Otherwise, check whether the base type is specialized. |
| 888 | if (const auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { |
| 889 | // Terminate when we reach an interface type. |
| 890 | if (isa<ObjCInterfaceType>(Val: objcObject)) |
| 891 | return false; |
| 892 | |
| 893 | return objcObject->isSpecialized(); |
| 894 | } |
| 895 | |
| 896 | // Not specialized. |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | ArrayRef<QualType> ObjCObjectType::getTypeArgs() const { |
| 901 | // We have type arguments written on this type. |
| 902 | if (isSpecializedAsWritten()) |
| 903 | return getTypeArgsAsWritten(); |
| 904 | |
| 905 | // Look at the base type, which might have type arguments. |
| 906 | if (const auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { |
| 907 | // Terminate when we reach an interface type. |
| 908 | if (isa<ObjCInterfaceType>(Val: objcObject)) |
| 909 | return {}; |
| 910 | |
| 911 | return objcObject->getTypeArgs(); |
| 912 | } |
| 913 | |
| 914 | // No type arguments. |
| 915 | return {}; |
| 916 | } |
| 917 | |
| 918 | bool ObjCObjectType::isKindOfType() const { |
| 919 | if (isKindOfTypeAsWritten()) |
| 920 | return true; |
| 921 | |
| 922 | // Look at the base type, which might have type arguments. |
| 923 | if (const auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { |
| 924 | // Terminate when we reach an interface type. |
| 925 | if (isa<ObjCInterfaceType>(Val: objcObject)) |
| 926 | return false; |
| 927 | |
| 928 | return objcObject->isKindOfType(); |
| 929 | } |
| 930 | |
| 931 | // Not a "__kindof" type. |
| 932 | return false; |
| 933 | } |
| 934 | |
| 935 | QualType |
| 936 | ObjCObjectType::stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const { |
| 937 | if (!isKindOfType() && qual_empty()) |
| 938 | return QualType(this, 0); |
| 939 | |
| 940 | // Recursively strip __kindof. |
| 941 | SplitQualType splitBaseType = getBaseType().split(); |
| 942 | QualType baseType(splitBaseType.Ty, 0); |
| 943 | if (const auto *baseObj = splitBaseType.Ty->getAs<ObjCObjectType>()) |
| 944 | baseType = baseObj->stripObjCKindOfTypeAndQuals(ctx); |
| 945 | |
| 946 | return ctx.getObjCObjectType( |
| 947 | Base: ctx.getQualifiedType(T: baseType, Qs: splitBaseType.Quals), |
| 948 | typeArgs: getTypeArgsAsWritten(), |
| 949 | /*protocols=*/{}, |
| 950 | /*isKindOf=*/false); |
| 951 | } |
| 952 | |
| 953 | ObjCInterfaceDecl *ObjCInterfaceType::getDecl() const { |
| 954 | ObjCInterfaceDecl *Canon = Decl->getCanonicalDecl(); |
| 955 | if (ObjCInterfaceDecl *Def = Canon->getDefinition()) |
| 956 | return Def; |
| 957 | return Canon; |
| 958 | } |
| 959 | |
| 960 | const ObjCObjectPointerType *ObjCObjectPointerType::stripObjCKindOfTypeAndQuals( |
| 961 | const ASTContext &ctx) const { |
| 962 | if (!isKindOfType() && qual_empty()) |
| 963 | return this; |
| 964 | |
| 965 | QualType obj = getObjectType()->stripObjCKindOfTypeAndQuals(ctx); |
| 966 | return ctx.getObjCObjectPointerType(OIT: obj)->castAs<ObjCObjectPointerType>(); |
| 967 | } |
| 968 | |
| 969 | namespace { |
| 970 | |
| 971 | /// Visitor used to perform a simple type transformation that does not change |
| 972 | /// the semantics of the type. |
| 973 | template <typename Derived> |
| 974 | struct SimpleTransformVisitor : public TypeVisitor<Derived, QualType> { |
| 975 | ASTContext &Ctx; |
| 976 | |
| 977 | QualType recurse(QualType type) { |
| 978 | // Split out the qualifiers from the type. |
| 979 | SplitQualType splitType = type.split(); |
| 980 | |
| 981 | // Visit the type itself. |
| 982 | QualType result = static_cast<Derived *>(this)->Visit(splitType.Ty); |
| 983 | if (result.isNull()) |
| 984 | return result; |
| 985 | |
| 986 | // Reconstruct the transformed type by applying the local qualifiers |
| 987 | // from the split type. |
| 988 | return Ctx.getQualifiedType(T: result, Qs: splitType.Quals); |
| 989 | } |
| 990 | |
| 991 | public: |
| 992 | explicit SimpleTransformVisitor(ASTContext &ctx) : Ctx(ctx) {} |
| 993 | |
| 994 | // None of the clients of this transformation can occur where |
| 995 | // there are dependent types, so skip dependent types. |
| 996 | #define TYPE(Class, Base) |
| 997 | #define DEPENDENT_TYPE(Class, Base) \ |
| 998 | QualType Visit##Class##Type(const Class##Type *T) { return QualType(T, 0); } |
| 999 | #include "clang/AST/TypeNodes.inc" |
| 1000 | |
| 1001 | #define TRIVIAL_TYPE_CLASS(Class) \ |
| 1002 | QualType Visit##Class##Type(const Class##Type *T) { return QualType(T, 0); } |
| 1003 | #define SUGARED_TYPE_CLASS(Class) \ |
| 1004 | QualType Visit##Class##Type(const Class##Type *T) { \ |
| 1005 | if (!T->isSugared()) \ |
| 1006 | return QualType(T, 0); \ |
| 1007 | QualType desugaredType = recurse(T->desugar()); \ |
| 1008 | if (desugaredType.isNull()) \ |
| 1009 | return {}; \ |
| 1010 | if (desugaredType.getAsOpaquePtr() == T->desugar().getAsOpaquePtr()) \ |
| 1011 | return QualType(T, 0); \ |
| 1012 | return desugaredType; \ |
| 1013 | } |
| 1014 | |
| 1015 | TRIVIAL_TYPE_CLASS(Builtin) |
| 1016 | |
| 1017 | QualType VisitComplexType(const ComplexType *T) { |
| 1018 | QualType elementType = recurse(type: T->getElementType()); |
| 1019 | if (elementType.isNull()) |
| 1020 | return {}; |
| 1021 | |
| 1022 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1023 | return QualType(T, 0); |
| 1024 | |
| 1025 | return Ctx.getComplexType(T: elementType); |
| 1026 | } |
| 1027 | |
| 1028 | QualType VisitPointerType(const PointerType *T) { |
| 1029 | QualType pointeeType = recurse(type: T->getPointeeType()); |
| 1030 | if (pointeeType.isNull()) |
| 1031 | return {}; |
| 1032 | |
| 1033 | if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) |
| 1034 | return QualType(T, 0); |
| 1035 | |
| 1036 | return Ctx.getPointerType(T: pointeeType); |
| 1037 | } |
| 1038 | |
| 1039 | QualType VisitBlockPointerType(const BlockPointerType *T) { |
| 1040 | QualType pointeeType = recurse(type: T->getPointeeType()); |
| 1041 | if (pointeeType.isNull()) |
| 1042 | return {}; |
| 1043 | |
| 1044 | if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) |
| 1045 | return QualType(T, 0); |
| 1046 | |
| 1047 | return Ctx.getBlockPointerType(T: pointeeType); |
| 1048 | } |
| 1049 | |
| 1050 | QualType VisitLValueReferenceType(const LValueReferenceType *T) { |
| 1051 | QualType pointeeType = recurse(type: T->getPointeeTypeAsWritten()); |
| 1052 | if (pointeeType.isNull()) |
| 1053 | return {}; |
| 1054 | |
| 1055 | if (pointeeType.getAsOpaquePtr() == |
| 1056 | T->getPointeeTypeAsWritten().getAsOpaquePtr()) |
| 1057 | return QualType(T, 0); |
| 1058 | |
| 1059 | return Ctx.getLValueReferenceType(T: pointeeType, SpelledAsLValue: T->isSpelledAsLValue()); |
| 1060 | } |
| 1061 | |
| 1062 | QualType VisitRValueReferenceType(const RValueReferenceType *T) { |
| 1063 | QualType pointeeType = recurse(type: T->getPointeeTypeAsWritten()); |
| 1064 | if (pointeeType.isNull()) |
| 1065 | return {}; |
| 1066 | |
| 1067 | if (pointeeType.getAsOpaquePtr() == |
| 1068 | T->getPointeeTypeAsWritten().getAsOpaquePtr()) |
| 1069 | return QualType(T, 0); |
| 1070 | |
| 1071 | return Ctx.getRValueReferenceType(T: pointeeType); |
| 1072 | } |
| 1073 | |
| 1074 | QualType VisitMemberPointerType(const MemberPointerType *T) { |
| 1075 | QualType pointeeType = recurse(type: T->getPointeeType()); |
| 1076 | if (pointeeType.isNull()) |
| 1077 | return {}; |
| 1078 | |
| 1079 | if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) |
| 1080 | return QualType(T, 0); |
| 1081 | |
| 1082 | return Ctx.getMemberPointerType(T: pointeeType, Qualifier: T->getQualifier(), |
| 1083 | Cls: T->getMostRecentCXXRecordDecl()); |
| 1084 | } |
| 1085 | |
| 1086 | QualType VisitConstantArrayType(const ConstantArrayType *T) { |
| 1087 | QualType elementType = recurse(type: T->getElementType()); |
| 1088 | if (elementType.isNull()) |
| 1089 | return {}; |
| 1090 | |
| 1091 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1092 | return QualType(T, 0); |
| 1093 | |
| 1094 | return Ctx.getConstantArrayType(EltTy: elementType, ArySize: T->getSize(), SizeExpr: T->getSizeExpr(), |
| 1095 | ASM: T->getSizeModifier(), |
| 1096 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1097 | } |
| 1098 | |
| 1099 | QualType VisitVariableArrayType(const VariableArrayType *T) { |
| 1100 | QualType elementType = recurse(type: T->getElementType()); |
| 1101 | if (elementType.isNull()) |
| 1102 | return {}; |
| 1103 | |
| 1104 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1105 | return QualType(T, 0); |
| 1106 | |
| 1107 | return Ctx.getVariableArrayType(EltTy: elementType, NumElts: T->getSizeExpr(), |
| 1108 | ASM: T->getSizeModifier(), |
| 1109 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1110 | } |
| 1111 | |
| 1112 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 1113 | QualType elementType = recurse(type: T->getElementType()); |
| 1114 | if (elementType.isNull()) |
| 1115 | return {}; |
| 1116 | |
| 1117 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1118 | return QualType(T, 0); |
| 1119 | |
| 1120 | return Ctx.getIncompleteArrayType(EltTy: elementType, ASM: T->getSizeModifier(), |
| 1121 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1122 | } |
| 1123 | |
| 1124 | QualType VisitVectorType(const VectorType *T) { |
| 1125 | QualType elementType = recurse(type: T->getElementType()); |
| 1126 | if (elementType.isNull()) |
| 1127 | return {}; |
| 1128 | |
| 1129 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1130 | return QualType(T, 0); |
| 1131 | |
| 1132 | return Ctx.getVectorType(VectorType: elementType, NumElts: T->getNumElements(), |
| 1133 | VecKind: T->getVectorKind()); |
| 1134 | } |
| 1135 | |
| 1136 | QualType VisitExtVectorType(const ExtVectorType *T) { |
| 1137 | QualType elementType = recurse(type: T->getElementType()); |
| 1138 | if (elementType.isNull()) |
| 1139 | return {}; |
| 1140 | |
| 1141 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1142 | return QualType(T, 0); |
| 1143 | |
| 1144 | return Ctx.getExtVectorType(VectorType: elementType, NumElts: T->getNumElements()); |
| 1145 | } |
| 1146 | |
| 1147 | QualType VisitConstantMatrixType(const ConstantMatrixType *T) { |
| 1148 | QualType elementType = recurse(type: T->getElementType()); |
| 1149 | if (elementType.isNull()) |
| 1150 | return {}; |
| 1151 | if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) |
| 1152 | return QualType(T, 0); |
| 1153 | |
| 1154 | return Ctx.getConstantMatrixType(ElementType: elementType, NumRows: T->getNumRows(), |
| 1155 | NumColumns: T->getNumColumns()); |
| 1156 | } |
| 1157 | |
| 1158 | QualType VisitOverflowBehaviorType(const OverflowBehaviorType *T) { |
| 1159 | QualType UnderlyingType = recurse(type: T->getUnderlyingType()); |
| 1160 | if (UnderlyingType.isNull()) |
| 1161 | return {}; |
| 1162 | |
| 1163 | if (UnderlyingType.getAsOpaquePtr() == |
| 1164 | T->getUnderlyingType().getAsOpaquePtr()) |
| 1165 | return QualType(T, 0); |
| 1166 | |
| 1167 | return Ctx.getOverflowBehaviorType(Kind: T->getBehaviorKind(), Wrapped: UnderlyingType); |
| 1168 | } |
| 1169 | |
| 1170 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 1171 | QualType returnType = recurse(type: T->getReturnType()); |
| 1172 | if (returnType.isNull()) |
| 1173 | return {}; |
| 1174 | |
| 1175 | if (returnType.getAsOpaquePtr() == T->getReturnType().getAsOpaquePtr()) |
| 1176 | return QualType(T, 0); |
| 1177 | |
| 1178 | return Ctx.getFunctionNoProtoType(ResultTy: returnType, Info: T->getExtInfo()); |
| 1179 | } |
| 1180 | |
| 1181 | QualType VisitFunctionProtoType(const FunctionProtoType *T) { |
| 1182 | QualType returnType = recurse(type: T->getReturnType()); |
| 1183 | if (returnType.isNull()) |
| 1184 | return {}; |
| 1185 | |
| 1186 | // Transform parameter types. |
| 1187 | SmallVector<QualType, 4> paramTypes; |
| 1188 | bool paramChanged = false; |
| 1189 | for (auto paramType : T->getParamTypes()) { |
| 1190 | QualType newParamType = recurse(type: paramType); |
| 1191 | if (newParamType.isNull()) |
| 1192 | return {}; |
| 1193 | |
| 1194 | if (newParamType.getAsOpaquePtr() != paramType.getAsOpaquePtr()) |
| 1195 | paramChanged = true; |
| 1196 | |
| 1197 | paramTypes.push_back(Elt: newParamType); |
| 1198 | } |
| 1199 | |
| 1200 | // Transform extended info. |
| 1201 | FunctionProtoType::ExtProtoInfo info = T->getExtProtoInfo(); |
| 1202 | bool exceptionChanged = false; |
| 1203 | if (info.ExceptionSpec.Type == EST_Dynamic) { |
| 1204 | SmallVector<QualType, 4> exceptionTypes; |
| 1205 | for (auto exceptionType : info.ExceptionSpec.Exceptions) { |
| 1206 | QualType newExceptionType = recurse(type: exceptionType); |
| 1207 | if (newExceptionType.isNull()) |
| 1208 | return {}; |
| 1209 | |
| 1210 | if (newExceptionType.getAsOpaquePtr() != exceptionType.getAsOpaquePtr()) |
| 1211 | exceptionChanged = true; |
| 1212 | |
| 1213 | exceptionTypes.push_back(Elt: newExceptionType); |
| 1214 | } |
| 1215 | |
| 1216 | if (exceptionChanged) { |
| 1217 | info.ExceptionSpec.Exceptions = |
| 1218 | llvm::ArrayRef(exceptionTypes).copy(A&: Ctx); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | if (returnType.getAsOpaquePtr() == T->getReturnType().getAsOpaquePtr() && |
| 1223 | !paramChanged && !exceptionChanged) |
| 1224 | return QualType(T, 0); |
| 1225 | |
| 1226 | return Ctx.getFunctionType(ResultTy: returnType, Args: paramTypes, EPI: info); |
| 1227 | } |
| 1228 | |
| 1229 | QualType VisitParenType(const ParenType *T) { |
| 1230 | QualType innerType = recurse(type: T->getInnerType()); |
| 1231 | if (innerType.isNull()) |
| 1232 | return {}; |
| 1233 | |
| 1234 | if (innerType.getAsOpaquePtr() == T->getInnerType().getAsOpaquePtr()) |
| 1235 | return QualType(T, 0); |
| 1236 | |
| 1237 | return Ctx.getParenType(NamedType: innerType); |
| 1238 | } |
| 1239 | |
| 1240 | SUGARED_TYPE_CLASS(Typedef) |
| 1241 | SUGARED_TYPE_CLASS(ObjCTypeParam) |
| 1242 | SUGARED_TYPE_CLASS(MacroQualified) |
| 1243 | |
| 1244 | QualType VisitAdjustedType(const AdjustedType *T) { |
| 1245 | QualType originalType = recurse(type: T->getOriginalType()); |
| 1246 | if (originalType.isNull()) |
| 1247 | return {}; |
| 1248 | |
| 1249 | QualType adjustedType = recurse(type: T->getAdjustedType()); |
| 1250 | if (adjustedType.isNull()) |
| 1251 | return {}; |
| 1252 | |
| 1253 | if (originalType.getAsOpaquePtr() == |
| 1254 | T->getOriginalType().getAsOpaquePtr() && |
| 1255 | adjustedType.getAsOpaquePtr() == T->getAdjustedType().getAsOpaquePtr()) |
| 1256 | return QualType(T, 0); |
| 1257 | |
| 1258 | return Ctx.getAdjustedType(Orig: originalType, New: adjustedType); |
| 1259 | } |
| 1260 | |
| 1261 | QualType VisitDecayedType(const DecayedType *T) { |
| 1262 | QualType originalType = recurse(type: T->getOriginalType()); |
| 1263 | if (originalType.isNull()) |
| 1264 | return {}; |
| 1265 | |
| 1266 | if (originalType.getAsOpaquePtr() == T->getOriginalType().getAsOpaquePtr()) |
| 1267 | return QualType(T, 0); |
| 1268 | |
| 1269 | return Ctx.getDecayedType(T: originalType); |
| 1270 | } |
| 1271 | |
| 1272 | QualType VisitArrayParameterType(const ArrayParameterType *T) { |
| 1273 | QualType ArrTy = VisitConstantArrayType(T); |
| 1274 | if (ArrTy.isNull()) |
| 1275 | return {}; |
| 1276 | |
| 1277 | return Ctx.getArrayParameterType(Ty: ArrTy); |
| 1278 | } |
| 1279 | |
| 1280 | SUGARED_TYPE_CLASS(TypeOfExpr) |
| 1281 | SUGARED_TYPE_CLASS(TypeOf) |
| 1282 | SUGARED_TYPE_CLASS(Decltype) |
| 1283 | SUGARED_TYPE_CLASS(UnaryTransform) |
| 1284 | TRIVIAL_TYPE_CLASS(Record) |
| 1285 | TRIVIAL_TYPE_CLASS(Enum) |
| 1286 | |
| 1287 | QualType VisitAttributedType(const AttributedType *T) { |
| 1288 | QualType modifiedType = recurse(type: T->getModifiedType()); |
| 1289 | if (modifiedType.isNull()) |
| 1290 | return {}; |
| 1291 | |
| 1292 | QualType equivalentType = recurse(type: T->getEquivalentType()); |
| 1293 | if (equivalentType.isNull()) |
| 1294 | return {}; |
| 1295 | |
| 1296 | if (modifiedType.getAsOpaquePtr() == |
| 1297 | T->getModifiedType().getAsOpaquePtr() && |
| 1298 | equivalentType.getAsOpaquePtr() == |
| 1299 | T->getEquivalentType().getAsOpaquePtr()) |
| 1300 | return QualType(T, 0); |
| 1301 | |
| 1302 | return Ctx.getAttributedType(attrKind: T->getAttrKind(), modifiedType, equivalentType, |
| 1303 | attr: T->getAttr()); |
| 1304 | } |
| 1305 | |
| 1306 | QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1307 | QualType replacementType = recurse(type: T->getReplacementType()); |
| 1308 | if (replacementType.isNull()) |
| 1309 | return {}; |
| 1310 | |
| 1311 | if (replacementType.getAsOpaquePtr() == |
| 1312 | T->getReplacementType().getAsOpaquePtr()) |
| 1313 | return QualType(T, 0); |
| 1314 | |
| 1315 | return Ctx.getSubstTemplateTypeParmType( |
| 1316 | Replacement: replacementType, AssociatedDecl: T->getAssociatedDecl(), Index: T->getIndex(), |
| 1317 | PackIndex: T->getPackIndex(), Final: T->getFinal()); |
| 1318 | } |
| 1319 | |
| 1320 | // FIXME: Non-trivial to implement, but important for C++ |
| 1321 | SUGARED_TYPE_CLASS(TemplateSpecialization) |
| 1322 | |
| 1323 | QualType VisitAutoType(const AutoType *T) { |
| 1324 | if (!T->isDeduced()) |
| 1325 | return QualType(T, 0); |
| 1326 | |
| 1327 | QualType deducedType = recurse(type: T->getDeducedType()); |
| 1328 | if (deducedType.isNull()) |
| 1329 | return {}; |
| 1330 | |
| 1331 | if (deducedType == T->getDeducedType()) |
| 1332 | return QualType(T, 0); |
| 1333 | |
| 1334 | return Ctx.getAutoType(DK: T->getDeducedKind(), DeducedAsType: deducedType, Keyword: T->getKeyword(), |
| 1335 | TypeConstraintConcept: T->getTypeConstraintConcept(), |
| 1336 | TypeConstraintArgs: T->getTypeConstraintArguments()); |
| 1337 | } |
| 1338 | |
| 1339 | QualType VisitObjCObjectType(const ObjCObjectType *T) { |
| 1340 | QualType baseType = recurse(type: T->getBaseType()); |
| 1341 | if (baseType.isNull()) |
| 1342 | return {}; |
| 1343 | |
| 1344 | // Transform type arguments. |
| 1345 | bool typeArgChanged = false; |
| 1346 | SmallVector<QualType, 4> typeArgs; |
| 1347 | for (auto typeArg : T->getTypeArgsAsWritten()) { |
| 1348 | QualType newTypeArg = recurse(type: typeArg); |
| 1349 | if (newTypeArg.isNull()) |
| 1350 | return {}; |
| 1351 | |
| 1352 | if (newTypeArg.getAsOpaquePtr() != typeArg.getAsOpaquePtr()) |
| 1353 | typeArgChanged = true; |
| 1354 | |
| 1355 | typeArgs.push_back(Elt: newTypeArg); |
| 1356 | } |
| 1357 | |
| 1358 | if (baseType.getAsOpaquePtr() == T->getBaseType().getAsOpaquePtr() && |
| 1359 | !typeArgChanged) |
| 1360 | return QualType(T, 0); |
| 1361 | |
| 1362 | return Ctx.getObjCObjectType( |
| 1363 | Base: baseType, typeArgs, |
| 1364 | protocols: llvm::ArrayRef(T->qual_begin(), T->getNumProtocols()), |
| 1365 | isKindOf: T->isKindOfTypeAsWritten()); |
| 1366 | } |
| 1367 | |
| 1368 | TRIVIAL_TYPE_CLASS(ObjCInterface) |
| 1369 | |
| 1370 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
| 1371 | QualType pointeeType = recurse(type: T->getPointeeType()); |
| 1372 | if (pointeeType.isNull()) |
| 1373 | return {}; |
| 1374 | |
| 1375 | if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) |
| 1376 | return QualType(T, 0); |
| 1377 | |
| 1378 | return Ctx.getObjCObjectPointerType(OIT: pointeeType); |
| 1379 | } |
| 1380 | |
| 1381 | QualType VisitAtomicType(const AtomicType *T) { |
| 1382 | QualType valueType = recurse(type: T->getValueType()); |
| 1383 | if (valueType.isNull()) |
| 1384 | return {}; |
| 1385 | |
| 1386 | if (valueType.getAsOpaquePtr() == T->getValueType().getAsOpaquePtr()) |
| 1387 | return QualType(T, 0); |
| 1388 | |
| 1389 | return Ctx.getAtomicType(T: valueType); |
| 1390 | } |
| 1391 | |
| 1392 | #undef TRIVIAL_TYPE_CLASS |
| 1393 | #undef SUGARED_TYPE_CLASS |
| 1394 | }; |
| 1395 | |
| 1396 | struct SubstObjCTypeArgsVisitor |
| 1397 | : public SimpleTransformVisitor<SubstObjCTypeArgsVisitor> { |
| 1398 | using BaseType = SimpleTransformVisitor<SubstObjCTypeArgsVisitor>; |
| 1399 | |
| 1400 | ArrayRef<QualType> TypeArgs; |
| 1401 | ObjCSubstitutionContext SubstContext; |
| 1402 | |
| 1403 | SubstObjCTypeArgsVisitor(ASTContext &ctx, ArrayRef<QualType> typeArgs, |
| 1404 | ObjCSubstitutionContext context) |
| 1405 | : BaseType(ctx), TypeArgs(typeArgs), SubstContext(context) {} |
| 1406 | |
| 1407 | QualType VisitObjCTypeParamType(const ObjCTypeParamType *OTPTy) { |
| 1408 | // Replace an Objective-C type parameter reference with the corresponding |
| 1409 | // type argument. |
| 1410 | ObjCTypeParamDecl *typeParam = OTPTy->getDecl(); |
| 1411 | // If we have type arguments, use them. |
| 1412 | if (!TypeArgs.empty()) { |
| 1413 | QualType argType = TypeArgs[typeParam->getIndex()]; |
| 1414 | if (OTPTy->qual_empty()) |
| 1415 | return argType; |
| 1416 | |
| 1417 | // Apply protocol lists if exists. |
| 1418 | bool hasError; |
| 1419 | SmallVector<ObjCProtocolDecl *, 8> protocolsVec; |
| 1420 | protocolsVec.append(in_start: OTPTy->qual_begin(), in_end: OTPTy->qual_end()); |
| 1421 | ArrayRef<ObjCProtocolDecl *> protocolsToApply = protocolsVec; |
| 1422 | return Ctx.applyObjCProtocolQualifiers( |
| 1423 | type: argType, protocols: protocolsToApply, hasError, allowOnPointerType: true /*allowOnPointerType*/); |
| 1424 | } |
| 1425 | |
| 1426 | switch (SubstContext) { |
| 1427 | case ObjCSubstitutionContext::Ordinary: |
| 1428 | case ObjCSubstitutionContext::Parameter: |
| 1429 | case ObjCSubstitutionContext::Superclass: |
| 1430 | // Substitute the bound. |
| 1431 | return typeParam->getUnderlyingType(); |
| 1432 | |
| 1433 | case ObjCSubstitutionContext::Result: |
| 1434 | case ObjCSubstitutionContext::Property: { |
| 1435 | // Substitute the __kindof form of the underlying type. |
| 1436 | const auto *objPtr = |
| 1437 | typeParam->getUnderlyingType()->castAs<ObjCObjectPointerType>(); |
| 1438 | |
| 1439 | // __kindof types, id, and Class don't need an additional |
| 1440 | // __kindof. |
| 1441 | if (objPtr->isKindOfType() || objPtr->isObjCIdOrClassType()) |
| 1442 | return typeParam->getUnderlyingType(); |
| 1443 | |
| 1444 | // Add __kindof. |
| 1445 | const auto *obj = objPtr->getObjectType(); |
| 1446 | QualType resultTy = Ctx.getObjCObjectType( |
| 1447 | Base: obj->getBaseType(), typeArgs: obj->getTypeArgsAsWritten(), protocols: obj->getProtocols(), |
| 1448 | /*isKindOf=*/true); |
| 1449 | |
| 1450 | // Rebuild object pointer type. |
| 1451 | return Ctx.getObjCObjectPointerType(OIT: resultTy); |
| 1452 | } |
| 1453 | } |
| 1454 | llvm_unreachable("Unexpected ObjCSubstitutionContext!" ); |
| 1455 | } |
| 1456 | |
| 1457 | QualType VisitFunctionType(const FunctionType *funcType) { |
| 1458 | // If we have a function type, update the substitution context |
| 1459 | // appropriately. |
| 1460 | |
| 1461 | // Substitute result type. |
| 1462 | QualType returnType = funcType->getReturnType().substObjCTypeArgs( |
| 1463 | ctx&: Ctx, typeArgs: TypeArgs, context: ObjCSubstitutionContext::Result); |
| 1464 | if (returnType.isNull()) |
| 1465 | return {}; |
| 1466 | |
| 1467 | // Handle non-prototyped functions, which only substitute into the result |
| 1468 | // type. |
| 1469 | if (isa<FunctionNoProtoType>(Val: funcType)) { |
| 1470 | // If the return type was unchanged, do nothing. |
| 1471 | if (returnType.getAsOpaquePtr() == |
| 1472 | funcType->getReturnType().getAsOpaquePtr()) |
| 1473 | return BaseType::VisitFunctionType(T: funcType); |
| 1474 | |
| 1475 | // Otherwise, build a new type. |
| 1476 | return Ctx.getFunctionNoProtoType(ResultTy: returnType, Info: funcType->getExtInfo()); |
| 1477 | } |
| 1478 | |
| 1479 | const auto *funcProtoType = cast<FunctionProtoType>(Val: funcType); |
| 1480 | |
| 1481 | // Transform parameter types. |
| 1482 | SmallVector<QualType, 4> paramTypes; |
| 1483 | bool paramChanged = false; |
| 1484 | for (auto paramType : funcProtoType->getParamTypes()) { |
| 1485 | QualType newParamType = paramType.substObjCTypeArgs( |
| 1486 | ctx&: Ctx, typeArgs: TypeArgs, context: ObjCSubstitutionContext::Parameter); |
| 1487 | if (newParamType.isNull()) |
| 1488 | return {}; |
| 1489 | |
| 1490 | if (newParamType.getAsOpaquePtr() != paramType.getAsOpaquePtr()) |
| 1491 | paramChanged = true; |
| 1492 | |
| 1493 | paramTypes.push_back(Elt: newParamType); |
| 1494 | } |
| 1495 | |
| 1496 | // Transform extended info. |
| 1497 | FunctionProtoType::ExtProtoInfo info = funcProtoType->getExtProtoInfo(); |
| 1498 | bool exceptionChanged = false; |
| 1499 | if (info.ExceptionSpec.Type == EST_Dynamic) { |
| 1500 | SmallVector<QualType, 4> exceptionTypes; |
| 1501 | for (auto exceptionType : info.ExceptionSpec.Exceptions) { |
| 1502 | QualType newExceptionType = exceptionType.substObjCTypeArgs( |
| 1503 | ctx&: Ctx, typeArgs: TypeArgs, context: ObjCSubstitutionContext::Ordinary); |
| 1504 | if (newExceptionType.isNull()) |
| 1505 | return {}; |
| 1506 | |
| 1507 | if (newExceptionType.getAsOpaquePtr() != exceptionType.getAsOpaquePtr()) |
| 1508 | exceptionChanged = true; |
| 1509 | |
| 1510 | exceptionTypes.push_back(Elt: newExceptionType); |
| 1511 | } |
| 1512 | |
| 1513 | if (exceptionChanged) { |
| 1514 | info.ExceptionSpec.Exceptions = |
| 1515 | llvm::ArrayRef(exceptionTypes).copy(A&: Ctx); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | if (returnType.getAsOpaquePtr() == |
| 1520 | funcProtoType->getReturnType().getAsOpaquePtr() && |
| 1521 | !paramChanged && !exceptionChanged) |
| 1522 | return BaseType::VisitFunctionType(T: funcType); |
| 1523 | |
| 1524 | return Ctx.getFunctionType(ResultTy: returnType, Args: paramTypes, EPI: info); |
| 1525 | } |
| 1526 | |
| 1527 | QualType VisitObjCObjectType(const ObjCObjectType *objcObjectType) { |
| 1528 | // Substitute into the type arguments of a specialized Objective-C object |
| 1529 | // type. |
| 1530 | if (objcObjectType->isSpecializedAsWritten()) { |
| 1531 | SmallVector<QualType, 4> newTypeArgs; |
| 1532 | bool anyChanged = false; |
| 1533 | for (auto typeArg : objcObjectType->getTypeArgsAsWritten()) { |
| 1534 | QualType newTypeArg = typeArg.substObjCTypeArgs( |
| 1535 | ctx&: Ctx, typeArgs: TypeArgs, context: ObjCSubstitutionContext::Ordinary); |
| 1536 | if (newTypeArg.isNull()) |
| 1537 | return {}; |
| 1538 | |
| 1539 | if (newTypeArg.getAsOpaquePtr() != typeArg.getAsOpaquePtr()) { |
| 1540 | // If we're substituting based on an unspecialized context type, |
| 1541 | // produce an unspecialized type. |
| 1542 | ArrayRef<ObjCProtocolDecl *> protocols( |
| 1543 | objcObjectType->qual_begin(), objcObjectType->getNumProtocols()); |
| 1544 | if (TypeArgs.empty() && |
| 1545 | SubstContext != ObjCSubstitutionContext::Superclass) { |
| 1546 | return Ctx.getObjCObjectType( |
| 1547 | Base: objcObjectType->getBaseType(), typeArgs: {}, protocols, |
| 1548 | isKindOf: objcObjectType->isKindOfTypeAsWritten()); |
| 1549 | } |
| 1550 | |
| 1551 | anyChanged = true; |
| 1552 | } |
| 1553 | |
| 1554 | newTypeArgs.push_back(Elt: newTypeArg); |
| 1555 | } |
| 1556 | |
| 1557 | if (anyChanged) { |
| 1558 | ArrayRef<ObjCProtocolDecl *> protocols( |
| 1559 | objcObjectType->qual_begin(), objcObjectType->getNumProtocols()); |
| 1560 | return Ctx.getObjCObjectType(Base: objcObjectType->getBaseType(), typeArgs: newTypeArgs, |
| 1561 | protocols, |
| 1562 | isKindOf: objcObjectType->isKindOfTypeAsWritten()); |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | return BaseType::VisitObjCObjectType(T: objcObjectType); |
| 1567 | } |
| 1568 | |
| 1569 | QualType VisitAttributedType(const AttributedType *attrType) { |
| 1570 | QualType newType = BaseType::VisitAttributedType(T: attrType); |
| 1571 | if (newType.isNull()) |
| 1572 | return {}; |
| 1573 | |
| 1574 | const auto *newAttrType = dyn_cast<AttributedType>(Val: newType.getTypePtr()); |
| 1575 | if (!newAttrType || newAttrType->getAttrKind() != attr::ObjCKindOf) |
| 1576 | return newType; |
| 1577 | |
| 1578 | // Find out if it's an Objective-C object or object pointer type; |
| 1579 | QualType newEquivType = newAttrType->getEquivalentType(); |
| 1580 | const ObjCObjectPointerType *ptrType = |
| 1581 | newEquivType->getAs<ObjCObjectPointerType>(); |
| 1582 | const ObjCObjectType *objType = ptrType |
| 1583 | ? ptrType->getObjectType() |
| 1584 | : newEquivType->getAs<ObjCObjectType>(); |
| 1585 | if (!objType) |
| 1586 | return newType; |
| 1587 | |
| 1588 | // Rebuild the "equivalent" type, which pushes __kindof down into |
| 1589 | // the object type. |
| 1590 | newEquivType = Ctx.getObjCObjectType( |
| 1591 | Base: objType->getBaseType(), typeArgs: objType->getTypeArgsAsWritten(), |
| 1592 | protocols: objType->getProtocols(), |
| 1593 | // There is no need to apply kindof on an unqualified id type. |
| 1594 | /*isKindOf=*/objType->isObjCUnqualifiedId() ? false : true); |
| 1595 | |
| 1596 | // If we started with an object pointer type, rebuild it. |
| 1597 | if (ptrType) |
| 1598 | newEquivType = Ctx.getObjCObjectPointerType(OIT: newEquivType); |
| 1599 | |
| 1600 | // Rebuild the attributed type. |
| 1601 | return Ctx.getAttributedType(attrKind: newAttrType->getAttrKind(), |
| 1602 | modifiedType: newAttrType->getModifiedType(), equivalentType: newEquivType, |
| 1603 | attr: newAttrType->getAttr()); |
| 1604 | } |
| 1605 | }; |
| 1606 | |
| 1607 | struct StripObjCKindOfTypeVisitor |
| 1608 | : public SimpleTransformVisitor<StripObjCKindOfTypeVisitor> { |
| 1609 | using BaseType = SimpleTransformVisitor<StripObjCKindOfTypeVisitor>; |
| 1610 | |
| 1611 | explicit StripObjCKindOfTypeVisitor(ASTContext &ctx) : BaseType(ctx) {} |
| 1612 | |
| 1613 | QualType VisitObjCObjectType(const ObjCObjectType *objType) { |
| 1614 | if (!objType->isKindOfType()) |
| 1615 | return BaseType::VisitObjCObjectType(T: objType); |
| 1616 | |
| 1617 | QualType baseType = objType->getBaseType().stripObjCKindOfType(ctx: Ctx); |
| 1618 | return Ctx.getObjCObjectType(Base: baseType, typeArgs: objType->getTypeArgsAsWritten(), |
| 1619 | protocols: objType->getProtocols(), |
| 1620 | /*isKindOf=*/false); |
| 1621 | } |
| 1622 | }; |
| 1623 | |
| 1624 | } // namespace |
| 1625 | |
| 1626 | bool QualType::UseExcessPrecision(const ASTContext &Ctx) { |
| 1627 | const BuiltinType *BT = getTypePtr()->getAs<BuiltinType>(); |
| 1628 | if (!BT) { |
| 1629 | const VectorType *VT = getTypePtr()->getAs<VectorType>(); |
| 1630 | if (VT) { |
| 1631 | QualType ElementType = VT->getElementType(); |
| 1632 | return ElementType.UseExcessPrecision(Ctx); |
| 1633 | } |
| 1634 | } else { |
| 1635 | switch (BT->getKind()) { |
| 1636 | case BuiltinType::Kind::Float16: { |
| 1637 | const TargetInfo &TI = Ctx.getTargetInfo(); |
| 1638 | if (TI.hasFloat16Type() && !TI.hasFastHalfType() && |
| 1639 | Ctx.getLangOpts().getFloat16ExcessPrecision() != |
| 1640 | Ctx.getLangOpts().ExcessPrecisionKind::FPP_None) |
| 1641 | return true; |
| 1642 | break; |
| 1643 | } |
| 1644 | case BuiltinType::Kind::BFloat16: { |
| 1645 | const TargetInfo &TI = Ctx.getTargetInfo(); |
| 1646 | if (TI.hasBFloat16Type() && !TI.hasFullBFloat16Type() && |
| 1647 | Ctx.getLangOpts().getBFloat16ExcessPrecision() != |
| 1648 | Ctx.getLangOpts().ExcessPrecisionKind::FPP_None) |
| 1649 | return true; |
| 1650 | break; |
| 1651 | } |
| 1652 | default: |
| 1653 | return false; |
| 1654 | } |
| 1655 | } |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
| 1659 | /// Substitute the given type arguments for Objective-C type |
| 1660 | /// parameters within the given type, recursively. |
| 1661 | QualType QualType::substObjCTypeArgs(ASTContext &ctx, |
| 1662 | ArrayRef<QualType> typeArgs, |
| 1663 | ObjCSubstitutionContext context) const { |
| 1664 | SubstObjCTypeArgsVisitor visitor(ctx, typeArgs, context); |
| 1665 | return visitor.recurse(type: *this); |
| 1666 | } |
| 1667 | |
| 1668 | QualType QualType::substObjCMemberType(QualType objectType, |
| 1669 | const DeclContext *dc, |
| 1670 | ObjCSubstitutionContext context) const { |
| 1671 | if (auto subs = objectType->getObjCSubstitutions(dc)) |
| 1672 | return substObjCTypeArgs(ctx&: dc->getParentASTContext(), typeArgs: *subs, context); |
| 1673 | |
| 1674 | return *this; |
| 1675 | } |
| 1676 | |
| 1677 | QualType QualType::stripObjCKindOfType(const ASTContext &constCtx) const { |
| 1678 | // FIXME: Because ASTContext::getAttributedType() is non-const. |
| 1679 | auto &ctx = const_cast<ASTContext &>(constCtx); |
| 1680 | StripObjCKindOfTypeVisitor visitor(ctx); |
| 1681 | return visitor.recurse(type: *this); |
| 1682 | } |
| 1683 | |
| 1684 | QualType QualType::getAtomicUnqualifiedType() const { |
| 1685 | QualType T = *this; |
| 1686 | if (const auto AT = T.getTypePtr()->getAs<AtomicType>()) |
| 1687 | T = AT->getValueType(); |
| 1688 | return T.getUnqualifiedType(); |
| 1689 | } |
| 1690 | |
| 1691 | std::optional<ArrayRef<QualType>> |
| 1692 | Type::getObjCSubstitutions(const DeclContext *dc) const { |
| 1693 | // Look through method scopes. |
| 1694 | if (const auto method = dyn_cast<ObjCMethodDecl>(Val: dc)) |
| 1695 | dc = method->getDeclContext(); |
| 1696 | |
| 1697 | // Find the class or category in which the type we're substituting |
| 1698 | // was declared. |
| 1699 | const auto *dcClassDecl = dyn_cast<ObjCInterfaceDecl>(Val: dc); |
| 1700 | const ObjCCategoryDecl *dcCategoryDecl = nullptr; |
| 1701 | ObjCTypeParamList *dcTypeParams = nullptr; |
| 1702 | if (dcClassDecl) { |
| 1703 | // If the class does not have any type parameters, there's no |
| 1704 | // substitution to do. |
| 1705 | dcTypeParams = dcClassDecl->getTypeParamList(); |
| 1706 | if (!dcTypeParams) |
| 1707 | return std::nullopt; |
| 1708 | } else { |
| 1709 | // If we are in neither a class nor a category, there's no |
| 1710 | // substitution to perform. |
| 1711 | dcCategoryDecl = dyn_cast<ObjCCategoryDecl>(Val: dc); |
| 1712 | if (!dcCategoryDecl) |
| 1713 | return std::nullopt; |
| 1714 | |
| 1715 | // If the category does not have any type parameters, there's no |
| 1716 | // substitution to do. |
| 1717 | dcTypeParams = dcCategoryDecl->getTypeParamList(); |
| 1718 | if (!dcTypeParams) |
| 1719 | return std::nullopt; |
| 1720 | |
| 1721 | dcClassDecl = dcCategoryDecl->getClassInterface(); |
| 1722 | if (!dcClassDecl) |
| 1723 | return std::nullopt; |
| 1724 | } |
| 1725 | assert(dcTypeParams && "No substitutions to perform" ); |
| 1726 | assert(dcClassDecl && "No class context" ); |
| 1727 | |
| 1728 | // Find the underlying object type. |
| 1729 | const ObjCObjectType *objectType; |
| 1730 | if (const auto *objectPointerType = getAs<ObjCObjectPointerType>()) { |
| 1731 | objectType = objectPointerType->getObjectType(); |
| 1732 | } else if (getAs<BlockPointerType>()) { |
| 1733 | ASTContext &ctx = dc->getParentASTContext(); |
| 1734 | objectType = ctx.getObjCObjectType(Base: ctx.ObjCBuiltinIdTy, Protocols: {}, NumProtocols: {}) |
| 1735 | ->castAs<ObjCObjectType>(); |
| 1736 | } else { |
| 1737 | objectType = getAs<ObjCObjectType>(); |
| 1738 | } |
| 1739 | |
| 1740 | /// Extract the class from the receiver object type. |
| 1741 | ObjCInterfaceDecl *curClassDecl = |
| 1742 | objectType ? objectType->getInterface() : nullptr; |
| 1743 | if (!curClassDecl) { |
| 1744 | // If we don't have a context type (e.g., this is "id" or some |
| 1745 | // variant thereof), substitute the bounds. |
| 1746 | return llvm::ArrayRef<QualType>(); |
| 1747 | } |
| 1748 | |
| 1749 | // Follow the superclass chain until we've mapped the receiver type |
| 1750 | // to the same class as the context. |
| 1751 | while (curClassDecl != dcClassDecl) { |
| 1752 | // Map to the superclass type. |
| 1753 | QualType superType = objectType->getSuperClassType(); |
| 1754 | if (superType.isNull()) { |
| 1755 | objectType = nullptr; |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | objectType = superType->castAs<ObjCObjectType>(); |
| 1760 | curClassDecl = objectType->getInterface(); |
| 1761 | } |
| 1762 | |
| 1763 | // If we don't have a receiver type, or the receiver type does not |
| 1764 | // have type arguments, substitute in the defaults. |
| 1765 | if (!objectType || objectType->isUnspecialized()) { |
| 1766 | return llvm::ArrayRef<QualType>(); |
| 1767 | } |
| 1768 | |
| 1769 | // The receiver type has the type arguments we want. |
| 1770 | return objectType->getTypeArgs(); |
| 1771 | } |
| 1772 | |
| 1773 | bool Type::acceptsObjCTypeParams() const { |
| 1774 | if (auto *IfaceT = getAsObjCInterfaceType()) { |
| 1775 | if (auto *ID = IfaceT->getInterface()) { |
| 1776 | if (ID->getTypeParamList()) |
| 1777 | return true; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
| 1784 | void ObjCObjectType::computeSuperClassTypeSlow() const { |
| 1785 | // Retrieve the class declaration for this type. If there isn't one |
| 1786 | // (e.g., this is some variant of "id" or "Class"), then there is no |
| 1787 | // superclass type. |
| 1788 | ObjCInterfaceDecl *classDecl = getInterface(); |
| 1789 | if (!classDecl) { |
| 1790 | CachedSuperClassType.setInt(true); |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | // Extract the superclass type. |
| 1795 | const ObjCObjectType *superClassObjTy = classDecl->getSuperClassType(); |
| 1796 | if (!superClassObjTy) { |
| 1797 | CachedSuperClassType.setInt(true); |
| 1798 | return; |
| 1799 | } |
| 1800 | |
| 1801 | ObjCInterfaceDecl *superClassDecl = superClassObjTy->getInterface(); |
| 1802 | if (!superClassDecl) { |
| 1803 | CachedSuperClassType.setInt(true); |
| 1804 | return; |
| 1805 | } |
| 1806 | |
| 1807 | // If the superclass doesn't have type parameters, then there is no |
| 1808 | // substitution to perform. |
| 1809 | QualType superClassType(superClassObjTy, 0); |
| 1810 | ObjCTypeParamList *superClassTypeParams = superClassDecl->getTypeParamList(); |
| 1811 | if (!superClassTypeParams) { |
| 1812 | CachedSuperClassType.setPointerAndInt( |
| 1813 | PtrVal: superClassType->castAs<ObjCObjectType>(), IntVal: true); |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | // If the superclass reference is unspecialized, return it. |
| 1818 | if (superClassObjTy->isUnspecialized()) { |
| 1819 | CachedSuperClassType.setPointerAndInt(PtrVal: superClassObjTy, IntVal: true); |
| 1820 | return; |
| 1821 | } |
| 1822 | |
| 1823 | // If the subclass is not parameterized, there aren't any type |
| 1824 | // parameters in the superclass reference to substitute. |
| 1825 | ObjCTypeParamList *typeParams = classDecl->getTypeParamList(); |
| 1826 | if (!typeParams) { |
| 1827 | CachedSuperClassType.setPointerAndInt( |
| 1828 | PtrVal: superClassType->castAs<ObjCObjectType>(), IntVal: true); |
| 1829 | return; |
| 1830 | } |
| 1831 | |
| 1832 | // If the subclass type isn't specialized, return the unspecialized |
| 1833 | // superclass. |
| 1834 | if (isUnspecialized()) { |
| 1835 | QualType unspecializedSuper = |
| 1836 | classDecl->getASTContext().getObjCInterfaceType( |
| 1837 | Decl: superClassObjTy->getInterface()); |
| 1838 | CachedSuperClassType.setPointerAndInt( |
| 1839 | PtrVal: unspecializedSuper->castAs<ObjCObjectType>(), IntVal: true); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | // Substitute the provided type arguments into the superclass type. |
| 1844 | ArrayRef<QualType> typeArgs = getTypeArgs(); |
| 1845 | assert(typeArgs.size() == typeParams->size()); |
| 1846 | CachedSuperClassType.setPointerAndInt( |
| 1847 | PtrVal: superClassType |
| 1848 | .substObjCTypeArgs(ctx&: classDecl->getASTContext(), typeArgs, |
| 1849 | context: ObjCSubstitutionContext::Superclass) |
| 1850 | ->castAs<ObjCObjectType>(), |
| 1851 | IntVal: true); |
| 1852 | } |
| 1853 | |
| 1854 | const ObjCInterfaceType *ObjCObjectPointerType::getInterfaceType() const { |
| 1855 | if (auto interfaceDecl = getObjectType()->getInterface()) { |
| 1856 | return interfaceDecl->getASTContext() |
| 1857 | .getObjCInterfaceType(Decl: interfaceDecl) |
| 1858 | ->castAs<ObjCInterfaceType>(); |
| 1859 | } |
| 1860 | |
| 1861 | return nullptr; |
| 1862 | } |
| 1863 | |
| 1864 | QualType ObjCObjectPointerType::getSuperClassType() const { |
| 1865 | QualType superObjectType = getObjectType()->getSuperClassType(); |
| 1866 | if (superObjectType.isNull()) |
| 1867 | return superObjectType; |
| 1868 | |
| 1869 | ASTContext &ctx = getInterfaceDecl()->getASTContext(); |
| 1870 | return ctx.getObjCObjectPointerType(OIT: superObjectType); |
| 1871 | } |
| 1872 | |
| 1873 | const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { |
| 1874 | // There is no sugar for ObjCObjectType's, just return the canonical |
| 1875 | // type pointer if it is the right class. There is no typedef information to |
| 1876 | // return and these cannot be Address-space qualified. |
| 1877 | if (const auto *T = getAs<ObjCObjectType>()) |
| 1878 | if (T->getNumProtocols() && T->getInterface()) |
| 1879 | return T; |
| 1880 | return nullptr; |
| 1881 | } |
| 1882 | |
| 1883 | bool Type::isObjCQualifiedInterfaceType() const { |
| 1884 | return getAsObjCQualifiedInterfaceType() != nullptr; |
| 1885 | } |
| 1886 | |
| 1887 | const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { |
| 1888 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 1889 | // type pointer if it is the right class. |
| 1890 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) { |
| 1891 | if (OPT->isObjCQualifiedIdType()) |
| 1892 | return OPT; |
| 1893 | } |
| 1894 | return nullptr; |
| 1895 | } |
| 1896 | |
| 1897 | const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const { |
| 1898 | // There is no sugar for ObjCQualifiedClassType's, just return the canonical |
| 1899 | // type pointer if it is the right class. |
| 1900 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) { |
| 1901 | if (OPT->isObjCQualifiedClassType()) |
| 1902 | return OPT; |
| 1903 | } |
| 1904 | return nullptr; |
| 1905 | } |
| 1906 | |
| 1907 | const ObjCObjectType *Type::getAsObjCInterfaceType() const { |
| 1908 | if (const auto *OT = getAs<ObjCObjectType>()) { |
| 1909 | if (OT->getInterface()) |
| 1910 | return OT; |
| 1911 | } |
| 1912 | return nullptr; |
| 1913 | } |
| 1914 | |
| 1915 | const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { |
| 1916 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) { |
| 1917 | if (OPT->getInterfaceType()) |
| 1918 | return OPT; |
| 1919 | } |
| 1920 | return nullptr; |
| 1921 | } |
| 1922 | |
| 1923 | const CXXRecordDecl *Type::getPointeeCXXRecordDecl() const { |
| 1924 | QualType PointeeType; |
| 1925 | if (const auto *PT = getAsCanonical<PointerType>()) |
| 1926 | PointeeType = PT->getPointeeType(); |
| 1927 | else if (const auto *RT = getAsCanonical<ReferenceType>()) |
| 1928 | PointeeType = RT->getPointeeType(); |
| 1929 | else |
| 1930 | return nullptr; |
| 1931 | return PointeeType->getAsCXXRecordDecl(); |
| 1932 | } |
| 1933 | |
| 1934 | const TemplateSpecializationType * |
| 1935 | Type::getAsNonAliasTemplateSpecializationType() const { |
| 1936 | const auto *TST = getAs<TemplateSpecializationType>(); |
| 1937 | while (TST && TST->isTypeAlias()) |
| 1938 | TST = TST->desugar()->getAs<TemplateSpecializationType>(); |
| 1939 | return TST; |
| 1940 | } |
| 1941 | |
| 1942 | NestedNameSpecifier Type::getPrefix() const { |
| 1943 | switch (getTypeClass()) { |
| 1944 | case Type::DependentName: |
| 1945 | return cast<DependentNameType>(Val: this)->getQualifier(); |
| 1946 | case Type::TemplateSpecialization: |
| 1947 | return cast<TemplateSpecializationType>(Val: this) |
| 1948 | ->getTemplateName() |
| 1949 | .getQualifier(); |
| 1950 | case Type::Enum: |
| 1951 | case Type::Record: |
| 1952 | case Type::InjectedClassName: |
| 1953 | return cast<TagType>(Val: this)->getQualifier(); |
| 1954 | case Type::Typedef: |
| 1955 | return cast<TypedefType>(Val: this)->getQualifier(); |
| 1956 | case Type::UnresolvedUsing: |
| 1957 | return cast<UnresolvedUsingType>(Val: this)->getQualifier(); |
| 1958 | case Type::Using: |
| 1959 | return cast<UsingType>(Val: this)->getQualifier(); |
| 1960 | default: |
| 1961 | return std::nullopt; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | bool Type::hasAttr(attr::Kind AK) const { |
| 1966 | const Type *Cur = this; |
| 1967 | while (const auto *AT = Cur->getAs<AttributedType>()) { |
| 1968 | if (AT->getAttrKind() == AK) |
| 1969 | return true; |
| 1970 | Cur = AT->getEquivalentType().getTypePtr(); |
| 1971 | } |
| 1972 | return false; |
| 1973 | } |
| 1974 | |
| 1975 | namespace { |
| 1976 | |
| 1977 | class GetContainedDeducedTypeVisitor |
| 1978 | : public TypeVisitor<GetContainedDeducedTypeVisitor, Type *> { |
| 1979 | bool Syntactic; |
| 1980 | |
| 1981 | public: |
| 1982 | GetContainedDeducedTypeVisitor(bool Syntactic = false) |
| 1983 | : Syntactic(Syntactic) {} |
| 1984 | |
| 1985 | using TypeVisitor<GetContainedDeducedTypeVisitor, Type *>::Visit; |
| 1986 | |
| 1987 | Type *Visit(QualType T) { |
| 1988 | if (T.isNull()) |
| 1989 | return nullptr; |
| 1990 | return Visit(T: T.getTypePtr()); |
| 1991 | } |
| 1992 | |
| 1993 | // The deduced type itself. |
| 1994 | Type *VisitDeducedType(const DeducedType *AT) { |
| 1995 | return const_cast<DeducedType *>(AT); |
| 1996 | } |
| 1997 | |
| 1998 | // Only these types can contain the desired 'auto' type. |
| 1999 | Type *VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2000 | return Visit(T: T->getReplacementType()); |
| 2001 | } |
| 2002 | |
| 2003 | Type *VisitPointerType(const PointerType *T) { |
| 2004 | return Visit(T: T->getPointeeType()); |
| 2005 | } |
| 2006 | |
| 2007 | Type *VisitBlockPointerType(const BlockPointerType *T) { |
| 2008 | return Visit(T: T->getPointeeType()); |
| 2009 | } |
| 2010 | |
| 2011 | Type *VisitReferenceType(const ReferenceType *T) { |
| 2012 | return Visit(T: T->getPointeeTypeAsWritten()); |
| 2013 | } |
| 2014 | |
| 2015 | Type *VisitMemberPointerType(const MemberPointerType *T) { |
| 2016 | return Visit(T: T->getPointeeType()); |
| 2017 | } |
| 2018 | |
| 2019 | Type *VisitArrayType(const ArrayType *T) { |
| 2020 | return Visit(T: T->getElementType()); |
| 2021 | } |
| 2022 | |
| 2023 | Type *VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) { |
| 2024 | return Visit(T: T->getElementType()); |
| 2025 | } |
| 2026 | |
| 2027 | Type *VisitVectorType(const VectorType *T) { |
| 2028 | return Visit(T: T->getElementType()); |
| 2029 | } |
| 2030 | |
| 2031 | Type *VisitDependentSizedMatrixType(const DependentSizedMatrixType *T) { |
| 2032 | return Visit(T: T->getElementType()); |
| 2033 | } |
| 2034 | |
| 2035 | Type *VisitConstantMatrixType(const ConstantMatrixType *T) { |
| 2036 | return Visit(T: T->getElementType()); |
| 2037 | } |
| 2038 | |
| 2039 | Type *VisitFunctionProtoType(const FunctionProtoType *T) { |
| 2040 | if (Syntactic && T->hasTrailingReturn()) |
| 2041 | return const_cast<FunctionProtoType *>(T); |
| 2042 | return VisitFunctionType(T); |
| 2043 | } |
| 2044 | |
| 2045 | Type *VisitFunctionType(const FunctionType *T) { |
| 2046 | return Visit(T: T->getReturnType()); |
| 2047 | } |
| 2048 | |
| 2049 | Type *VisitParenType(const ParenType *T) { return Visit(T: T->getInnerType()); } |
| 2050 | |
| 2051 | Type *VisitAttributedType(const AttributedType *T) { |
| 2052 | return Visit(T: T->getModifiedType()); |
| 2053 | } |
| 2054 | |
| 2055 | Type *VisitMacroQualifiedType(const MacroQualifiedType *T) { |
| 2056 | return Visit(T: T->getUnderlyingType()); |
| 2057 | } |
| 2058 | |
| 2059 | Type *VisitOverflowBehaviorType(const OverflowBehaviorType *T) { |
| 2060 | return Visit(T: T->getUnderlyingType()); |
| 2061 | } |
| 2062 | |
| 2063 | Type *VisitAdjustedType(const AdjustedType *T) { |
| 2064 | return Visit(T: T->getOriginalType()); |
| 2065 | } |
| 2066 | |
| 2067 | Type *VisitPackExpansionType(const PackExpansionType *T) { |
| 2068 | return Visit(T: T->getPattern()); |
| 2069 | } |
| 2070 | }; |
| 2071 | |
| 2072 | } // namespace |
| 2073 | |
| 2074 | DeducedType *Type::getContainedDeducedType() const { |
| 2075 | return cast_or_null<DeducedType>( |
| 2076 | Val: GetContainedDeducedTypeVisitor().Visit(T: this)); |
| 2077 | } |
| 2078 | |
| 2079 | bool Type::hasAutoForTrailingReturnType() const { |
| 2080 | return isa_and_nonnull<FunctionType>( |
| 2081 | Val: GetContainedDeducedTypeVisitor(true).Visit(T: this)); |
| 2082 | } |
| 2083 | |
| 2084 | bool Type::hasIntegerRepresentation() const { |
| 2085 | if (const auto *VT = dyn_cast<VectorType>(Val: CanonicalType)) |
| 2086 | return VT->getElementType()->isIntegerType(); |
| 2087 | if (CanonicalType->isSveVLSBuiltinType()) { |
| 2088 | const auto *VT = cast<BuiltinType>(Val: CanonicalType); |
| 2089 | return VT->getKind() == BuiltinType::SveBool || |
| 2090 | (VT->getKind() >= BuiltinType::SveInt8 && |
| 2091 | VT->getKind() <= BuiltinType::SveUint64); |
| 2092 | } |
| 2093 | if (CanonicalType->isRVVVLSBuiltinType()) { |
| 2094 | const auto *VT = cast<BuiltinType>(Val: CanonicalType); |
| 2095 | return (VT->getKind() >= BuiltinType::RvvInt8mf8 && |
| 2096 | VT->getKind() <= BuiltinType::RvvUint64m8); |
| 2097 | } |
| 2098 | |
| 2099 | return isIntegerType(); |
| 2100 | } |
| 2101 | |
| 2102 | /// Determine whether this type is an integral type. |
| 2103 | /// |
| 2104 | /// This routine determines whether the given type is an integral type per |
| 2105 | /// C++ [basic.fundamental]p7. Although the C standard does not define the |
| 2106 | /// term "integral type", it has a similar term "integer type", and in C++ |
| 2107 | /// the two terms are equivalent. However, C's "integer type" includes |
| 2108 | /// enumeration types, while C++'s "integer type" does not. The \c ASTContext |
| 2109 | /// parameter is used to determine whether we should be following the C or |
| 2110 | /// C++ rules when determining whether this type is an integral/integer type. |
| 2111 | /// |
| 2112 | /// For cases where C permits "an integer type" and C++ permits "an integral |
| 2113 | /// type", use this routine. |
| 2114 | /// |
| 2115 | /// For cases where C permits "an integer type" and C++ permits "an integral |
| 2116 | /// or enumeration type", use \c isIntegralOrEnumerationType() instead. |
| 2117 | /// |
| 2118 | /// \param Ctx The context in which this type occurs. |
| 2119 | /// |
| 2120 | /// \returns true if the type is considered an integral type, false otherwise. |
| 2121 | bool Type::isIntegralType(const ASTContext &Ctx) const { |
| 2122 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2123 | return BT->isInteger(); |
| 2124 | |
| 2125 | // Complete enum types are integral in C. |
| 2126 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 2127 | if (const auto *ET = dyn_cast<EnumType>(Val: CanonicalType)) |
| 2128 | return IsEnumDeclComplete(ED: ET->getDecl()); |
| 2129 | |
| 2130 | if (const OverflowBehaviorType *OBT = |
| 2131 | dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2132 | return OBT->getUnderlyingType()->isIntegralOrEnumerationType(); |
| 2133 | } |
| 2134 | |
| 2135 | return isBitIntType(); |
| 2136 | } |
| 2137 | |
| 2138 | bool Type::isIntegralOrUnscopedEnumerationType() const { |
| 2139 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2140 | return BT->isInteger(); |
| 2141 | |
| 2142 | if (const auto *OBT = dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2143 | return OBT->getUnderlyingType()->isIntegerType(); |
| 2144 | |
| 2145 | if (isBitIntType()) |
| 2146 | return true; |
| 2147 | |
| 2148 | return isUnscopedEnumerationType(); |
| 2149 | } |
| 2150 | |
| 2151 | bool Type::isUnscopedEnumerationType() const { |
| 2152 | if (const auto *ET = dyn_cast<EnumType>(Val: CanonicalType)) |
| 2153 | return !ET->getDecl()->isScoped(); |
| 2154 | |
| 2155 | return false; |
| 2156 | } |
| 2157 | |
| 2158 | bool Type::isCharType() const { |
| 2159 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2160 | return BT->getKind() == BuiltinType::Char_U || |
| 2161 | BT->getKind() == BuiltinType::UChar || |
| 2162 | BT->getKind() == BuiltinType::Char_S || |
| 2163 | BT->getKind() == BuiltinType::SChar; |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
| 2167 | bool Type::isWideCharType() const { |
| 2168 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2169 | return BT->getKind() == BuiltinType::WChar_S || |
| 2170 | BT->getKind() == BuiltinType::WChar_U; |
| 2171 | return false; |
| 2172 | } |
| 2173 | |
| 2174 | bool Type::isChar8Type() const { |
| 2175 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2176 | return BT->getKind() == BuiltinType::Char8; |
| 2177 | return false; |
| 2178 | } |
| 2179 | |
| 2180 | bool Type::isChar16Type() const { |
| 2181 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2182 | return BT->getKind() == BuiltinType::Char16; |
| 2183 | return false; |
| 2184 | } |
| 2185 | |
| 2186 | bool Type::isChar32Type() const { |
| 2187 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2188 | return BT->getKind() == BuiltinType::Char32; |
| 2189 | return false; |
| 2190 | } |
| 2191 | |
| 2192 | /// Determine whether this type is any of the built-in character |
| 2193 | /// types. |
| 2194 | bool Type::isAnyCharacterType() const { |
| 2195 | const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType); |
| 2196 | if (!BT) |
| 2197 | return false; |
| 2198 | switch (BT->getKind()) { |
| 2199 | default: |
| 2200 | return false; |
| 2201 | case BuiltinType::Char_U: |
| 2202 | case BuiltinType::UChar: |
| 2203 | case BuiltinType::WChar_U: |
| 2204 | case BuiltinType::Char8: |
| 2205 | case BuiltinType::Char16: |
| 2206 | case BuiltinType::Char32: |
| 2207 | case BuiltinType::Char_S: |
| 2208 | case BuiltinType::SChar: |
| 2209 | case BuiltinType::WChar_S: |
| 2210 | return true; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | bool Type::isUnicodeCharacterType() const { |
| 2215 | const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType); |
| 2216 | if (!BT) |
| 2217 | return false; |
| 2218 | switch (BT->getKind()) { |
| 2219 | default: |
| 2220 | return false; |
| 2221 | case BuiltinType::Char8: |
| 2222 | case BuiltinType::Char16: |
| 2223 | case BuiltinType::Char32: |
| 2224 | return true; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 2229 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 2230 | /// an enum decl which has a signed representation |
| 2231 | bool Type::isSignedIntegerType() const { |
| 2232 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2233 | return BT->isSignedInteger(); |
| 2234 | |
| 2235 | if (const auto *ED = getAsEnumDecl()) { |
| 2236 | // Incomplete enum types are not treated as integer types. |
| 2237 | // FIXME: In C++, enum types are never integer types. |
| 2238 | if (!ED->isComplete() || ED->isScoped()) |
| 2239 | return false; |
| 2240 | return ED->getIntegerType()->isSignedIntegerType(); |
| 2241 | } |
| 2242 | |
| 2243 | if (const auto *IT = dyn_cast<BitIntType>(Val: CanonicalType)) |
| 2244 | return IT->isSigned(); |
| 2245 | if (const auto *IT = dyn_cast<DependentBitIntType>(Val: CanonicalType)) |
| 2246 | return IT->isSigned(); |
| 2247 | |
| 2248 | if (const auto *OBT = dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2249 | return OBT->getUnderlyingType()->isSignedIntegerType(); |
| 2250 | |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
| 2254 | bool Type::isSignedIntegerOrEnumerationType() const { |
| 2255 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2256 | return BT->isSignedInteger(); |
| 2257 | |
| 2258 | if (const auto *ED = getAsEnumDecl()) { |
| 2259 | if (!ED->isComplete()) |
| 2260 | return false; |
| 2261 | return ED->getIntegerType()->isSignedIntegerType(); |
| 2262 | } |
| 2263 | |
| 2264 | if (const auto *IT = dyn_cast<BitIntType>(Val: CanonicalType)) |
| 2265 | return IT->isSigned(); |
| 2266 | if (const auto *IT = dyn_cast<DependentBitIntType>(Val: CanonicalType)) |
| 2267 | return IT->isSigned(); |
| 2268 | |
| 2269 | if (const auto *OBT = dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2270 | return OBT->getUnderlyingType()->isSignedIntegerOrEnumerationType(); |
| 2271 | |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | bool Type::hasSignedIntegerRepresentation() const { |
| 2276 | if (const auto *VT = dyn_cast<VectorType>(Val: CanonicalType)) |
| 2277 | return VT->getElementType()->isSignedIntegerOrEnumerationType(); |
| 2278 | else |
| 2279 | return isSignedIntegerOrEnumerationType(); |
| 2280 | } |
| 2281 | |
| 2282 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 2283 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 2284 | /// decl which has an unsigned representation |
| 2285 | bool Type::isUnsignedIntegerType() const { |
| 2286 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2287 | return BT->isUnsignedInteger(); |
| 2288 | |
| 2289 | if (const auto *ED = getAsEnumDecl()) { |
| 2290 | // Incomplete enum types are not treated as integer types. |
| 2291 | // FIXME: In C++, enum types are never integer types. |
| 2292 | if (!ED->isComplete() || ED->isScoped()) |
| 2293 | return false; |
| 2294 | return ED->getIntegerType()->isUnsignedIntegerType(); |
| 2295 | } |
| 2296 | |
| 2297 | if (const auto *IT = dyn_cast<BitIntType>(Val: CanonicalType)) |
| 2298 | return IT->isUnsigned(); |
| 2299 | if (const auto *IT = dyn_cast<DependentBitIntType>(Val: CanonicalType)) |
| 2300 | return IT->isUnsigned(); |
| 2301 | |
| 2302 | if (const auto *OBT = dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2303 | return OBT->getUnderlyingType()->isUnsignedIntegerType(); |
| 2304 | |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
| 2308 | bool Type::isUnsignedIntegerOrEnumerationType() const { |
| 2309 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2310 | return BT->isUnsignedInteger(); |
| 2311 | |
| 2312 | if (const auto *ED = getAsEnumDecl()) { |
| 2313 | if (!ED->isComplete()) |
| 2314 | return false; |
| 2315 | return ED->getIntegerType()->isUnsignedIntegerType(); |
| 2316 | } |
| 2317 | |
| 2318 | if (const auto *IT = dyn_cast<BitIntType>(Val: CanonicalType)) |
| 2319 | return IT->isUnsigned(); |
| 2320 | if (const auto *IT = dyn_cast<DependentBitIntType>(Val: CanonicalType)) |
| 2321 | return IT->isUnsigned(); |
| 2322 | |
| 2323 | if (const auto *OBT = dyn_cast<OverflowBehaviorType>(Val: CanonicalType)) |
| 2324 | return OBT->getUnderlyingType()->isUnsignedIntegerOrEnumerationType(); |
| 2325 | |
| 2326 | return false; |
| 2327 | } |
| 2328 | |
| 2329 | bool Type::hasUnsignedIntegerRepresentation() const { |
| 2330 | if (const auto *VT = dyn_cast<VectorType>(Val: CanonicalType)) |
| 2331 | return VT->getElementType()->isUnsignedIntegerOrEnumerationType(); |
| 2332 | if (const auto *VT = dyn_cast<MatrixType>(Val: CanonicalType)) |
| 2333 | return VT->getElementType()->isUnsignedIntegerOrEnumerationType(); |
| 2334 | if (CanonicalType->isSveVLSBuiltinType()) { |
| 2335 | const auto *VT = cast<BuiltinType>(Val: CanonicalType); |
| 2336 | return VT->getKind() >= BuiltinType::SveUint8 && |
| 2337 | VT->getKind() <= BuiltinType::SveUint64; |
| 2338 | } |
| 2339 | return isUnsignedIntegerOrEnumerationType(); |
| 2340 | } |
| 2341 | |
| 2342 | bool Type::isFloatingType() const { |
| 2343 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2344 | return BT->isFloatingPoint(); |
| 2345 | if (const auto *CT = dyn_cast<ComplexType>(Val: CanonicalType)) |
| 2346 | return CT->getElementType()->isFloatingType(); |
| 2347 | return false; |
| 2348 | } |
| 2349 | |
| 2350 | bool Type::hasFloatingRepresentation() const { |
| 2351 | if (const auto *VT = dyn_cast<VectorType>(Val: CanonicalType)) |
| 2352 | return VT->getElementType()->isFloatingType(); |
| 2353 | if (const auto *MT = dyn_cast<MatrixType>(Val: CanonicalType)) |
| 2354 | return MT->getElementType()->isFloatingType(); |
| 2355 | return isFloatingType(); |
| 2356 | } |
| 2357 | |
| 2358 | bool Type::isRealFloatingType() const { |
| 2359 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2360 | return BT->isFloatingPoint(); |
| 2361 | return false; |
| 2362 | } |
| 2363 | |
| 2364 | bool Type::isRealType() const { |
| 2365 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2366 | return BT->getKind() >= BuiltinType::Bool && |
| 2367 | BT->getKind() <= BuiltinType::Ibm128; |
| 2368 | if (const auto *ET = dyn_cast<EnumType>(Val: CanonicalType)) { |
| 2369 | const auto *ED = ET->getDecl(); |
| 2370 | return !ED->isScoped() && ED->getDefinitionOrSelf()->isComplete(); |
| 2371 | } |
| 2372 | return isBitIntType(); |
| 2373 | } |
| 2374 | |
| 2375 | bool Type::isArithmeticType() const { |
| 2376 | if (const auto *BT = dyn_cast<BuiltinType>(Val: CanonicalType)) |
| 2377 | return BT->getKind() >= BuiltinType::Bool && |
| 2378 | BT->getKind() <= BuiltinType::Ibm128; |
| 2379 | if (const auto *ET = dyn_cast<EnumType>(Val: CanonicalType)) { |
| 2380 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 2381 | // If a body isn't seen by the time we get here, return false. |
| 2382 | // |
| 2383 | // C++0x: Enumerations are not arithmetic types. For now, just return |
| 2384 | // false for scoped enumerations since that will disable any |
| 2385 | // unwanted implicit conversions. |
| 2386 | const auto *ED = ET->getDecl(); |
| 2387 | return !ED->isScoped() && ED->getDefinitionOrSelf()->isComplete(); |
| 2388 | } |
| 2389 | |
| 2390 | if (isOverflowBehaviorType() && |
| 2391 | getAs<OverflowBehaviorType>()->getUnderlyingType()->isArithmeticType()) |
| 2392 | return true; |
| 2393 | |
| 2394 | return isa<ComplexType>(Val: CanonicalType) || isBitIntType(); |
| 2395 | } |
| 2396 | |
| 2397 | bool Type::hasBooleanRepresentation() const { |
| 2398 | if (const auto *VT = dyn_cast<VectorType>(Val: CanonicalType)) |
| 2399 | return VT->getElementType()->isBooleanType(); |
| 2400 | if (const auto *ED = getAsEnumDecl()) |
| 2401 | return ED->isComplete() && ED->getIntegerType()->isBooleanType(); |
| 2402 | if (const auto *IT = dyn_cast<BitIntType>(Val: CanonicalType)) |
| 2403 | return IT->getNumBits() == 1; |
| 2404 | return isBooleanType(); |
| 2405 | } |
| 2406 | |
| 2407 | Type::ScalarTypeKind Type::getScalarTypeKind() const { |
| 2408 | assert(isScalarType()); |
| 2409 | |
| 2410 | const Type *T = CanonicalType.getTypePtr(); |
| 2411 | if (const auto *BT = dyn_cast<BuiltinType>(Val: T)) { |
| 2412 | if (BT->getKind() == BuiltinType::Bool) |
| 2413 | return STK_Bool; |
| 2414 | if (BT->getKind() == BuiltinType::NullPtr) |
| 2415 | return STK_CPointer; |
| 2416 | if (BT->isInteger()) |
| 2417 | return STK_Integral; |
| 2418 | if (BT->isFloatingPoint()) |
| 2419 | return STK_Floating; |
| 2420 | if (BT->isFixedPointType()) |
| 2421 | return STK_FixedPoint; |
| 2422 | llvm_unreachable("unknown scalar builtin type" ); |
| 2423 | } else if (isa<PointerType>(Val: T)) { |
| 2424 | return STK_CPointer; |
| 2425 | } else if (isa<BlockPointerType>(Val: T)) { |
| 2426 | return STK_BlockPointer; |
| 2427 | } else if (isa<ObjCObjectPointerType>(Val: T)) { |
| 2428 | return STK_ObjCObjectPointer; |
| 2429 | } else if (isa<MemberPointerType>(Val: T)) { |
| 2430 | return STK_MemberPointer; |
| 2431 | } else if (isa<EnumType>(Val: T)) { |
| 2432 | assert(T->castAsEnumDecl()->isComplete()); |
| 2433 | return STK_Integral; |
| 2434 | } else if (const auto *CT = dyn_cast<ComplexType>(Val: T)) { |
| 2435 | if (CT->getElementType()->isRealFloatingType()) |
| 2436 | return STK_FloatingComplex; |
| 2437 | return STK_IntegralComplex; |
| 2438 | } else if (isBitIntType()) { |
| 2439 | return STK_Integral; |
| 2440 | } else if (isa<OverflowBehaviorType>(Val: T)) { |
| 2441 | return STK_Integral; |
| 2442 | } |
| 2443 | |
| 2444 | llvm_unreachable("unknown scalar type" ); |
| 2445 | } |
| 2446 | |
| 2447 | /// Determines whether the type is a C++ aggregate type or C |
| 2448 | /// aggregate or union type. |
| 2449 | /// |
| 2450 | /// An aggregate type is an array or a class type (struct, union, or |
| 2451 | /// class) that has no user-declared constructors, no private or |
| 2452 | /// protected non-static data members, no base classes, and no virtual |
| 2453 | /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type |
| 2454 | /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also |
| 2455 | /// includes union types. |
| 2456 | bool Type::isAggregateType() const { |
| 2457 | if (const auto *Record = dyn_cast<RecordType>(Val: CanonicalType)) { |
| 2458 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: Record->getDecl())) |
| 2459 | return ClassDecl->isAggregate(); |
| 2460 | |
| 2461 | return true; |
| 2462 | } |
| 2463 | |
| 2464 | return isa<ArrayType>(Val: CanonicalType); |
| 2465 | } |
| 2466 | |
| 2467 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 2468 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
| 2469 | /// incomplete types or dependent types. |
| 2470 | bool Type::isConstantSizeType() const { |
| 2471 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types" ); |
| 2472 | assert(!isDependentType() && "This doesn't make sense for dependent types" ); |
| 2473 | // The VAT must have a size, as it is known to be complete. |
| 2474 | return !isa<VariableArrayType>(Val: CanonicalType); |
| 2475 | } |
| 2476 | |
| 2477 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 2478 | /// - a type that can describe objects, but which lacks information needed to |
| 2479 | /// determine its size. |
| 2480 | bool Type::isIncompleteType(NamedDecl **Def) const { |
| 2481 | if (Def) |
| 2482 | *Def = nullptr; |
| 2483 | |
| 2484 | switch (CanonicalType->getTypeClass()) { |
| 2485 | default: |
| 2486 | return false; |
| 2487 | case Builtin: |
| 2488 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 2489 | // be completed. |
| 2490 | return isVoidType(); |
| 2491 | case Enum: { |
| 2492 | auto *EnumD = castAsEnumDecl(); |
| 2493 | if (Def) |
| 2494 | *Def = EnumD; |
| 2495 | return !EnumD->isComplete(); |
| 2496 | } |
| 2497 | case Record: { |
| 2498 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 2499 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 2500 | auto *Rec = castAsRecordDecl(); |
| 2501 | if (Def) |
| 2502 | *Def = Rec; |
| 2503 | return !Rec->isCompleteDefinition(); |
| 2504 | } |
| 2505 | case InjectedClassName: { |
| 2506 | auto *Rec = castAsCXXRecordDecl(); |
| 2507 | if (!Rec->isBeingDefined()) |
| 2508 | return false; |
| 2509 | if (Def) |
| 2510 | *Def = Rec; |
| 2511 | return true; |
| 2512 | } |
| 2513 | case ConstantArray: |
| 2514 | case VariableArray: |
| 2515 | // An array is incomplete if its element type is incomplete |
| 2516 | // (C++ [dcl.array]p1). |
| 2517 | // We don't handle dependent-sized arrays (dependent types are never treated |
| 2518 | // as incomplete). |
| 2519 | return cast<ArrayType>(Val: CanonicalType) |
| 2520 | ->getElementType() |
| 2521 | ->isIncompleteType(Def); |
| 2522 | case IncompleteArray: |
| 2523 | // An array of unknown size is an incomplete type (C99 6.2.5p22). |
| 2524 | return true; |
| 2525 | case MemberPointer: { |
| 2526 | // Member pointers in the MS ABI have special behavior in |
| 2527 | // RequireCompleteType: they attach a MSInheritanceAttr to the CXXRecordDecl |
| 2528 | // to indicate which inheritance model to use. |
| 2529 | // The inheritance attribute might only be present on the most recent |
| 2530 | // CXXRecordDecl. |
| 2531 | const CXXRecordDecl *RD = |
| 2532 | cast<MemberPointerType>(Val: CanonicalType)->getMostRecentCXXRecordDecl(); |
| 2533 | // Member pointers with dependent class types don't get special treatment. |
| 2534 | if (!RD || RD->isDependentType()) |
| 2535 | return false; |
| 2536 | ASTContext &Context = RD->getASTContext(); |
| 2537 | // Member pointers not in the MS ABI don't get special treatment. |
| 2538 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 2539 | return false; |
| 2540 | // Nothing interesting to do if the inheritance attribute is already set. |
| 2541 | if (RD->hasAttr<MSInheritanceAttr>()) |
| 2542 | return false; |
| 2543 | return true; |
| 2544 | } |
| 2545 | case ObjCObject: |
| 2546 | return cast<ObjCObjectType>(Val: CanonicalType) |
| 2547 | ->getBaseType() |
| 2548 | ->isIncompleteType(Def); |
| 2549 | case ObjCInterface: { |
| 2550 | // ObjC interfaces are incomplete if they are @class, not @interface. |
| 2551 | ObjCInterfaceDecl *Interface = |
| 2552 | cast<ObjCInterfaceType>(Val: CanonicalType)->getDecl(); |
| 2553 | if (Def) |
| 2554 | *Def = Interface; |
| 2555 | return !Interface->hasDefinition(); |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | bool Type::isAlwaysIncompleteType() const { |
| 2561 | if (!isIncompleteType()) |
| 2562 | return false; |
| 2563 | |
| 2564 | // Forward declarations of structs, classes, enums, and unions could be later |
| 2565 | // completed in a compilation unit by providing a type definition. |
| 2566 | if (isa<TagType>(Val: CanonicalType)) |
| 2567 | return false; |
| 2568 | |
| 2569 | // Other types are incompletable. |
| 2570 | // |
| 2571 | // E.g. `char[]` and `void`. The type is incomplete and no future |
| 2572 | // type declarations can make the type complete. |
| 2573 | return true; |
| 2574 | } |
| 2575 | |
| 2576 | bool Type::isSizelessBuiltinType() const { |
| 2577 | if (isSizelessVectorType()) |
| 2578 | return true; |
| 2579 | |
| 2580 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 2581 | switch (BT->getKind()) { |
| 2582 | // WebAssembly reference types |
| 2583 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 2584 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 2585 | // HLSL intangible types |
| 2586 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 2587 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 2588 | // AMDGPU feature predicate type |
| 2589 | case BuiltinType::AMDGPUFeaturePredicate: |
| 2590 | return true; |
| 2591 | default: |
| 2592 | return false; |
| 2593 | } |
| 2594 | } |
| 2595 | return false; |
| 2596 | } |
| 2597 | |
| 2598 | bool Type::isWebAssemblyExternrefType() const { |
| 2599 | if (const auto *BT = getAs<BuiltinType>()) |
| 2600 | return BT->getKind() == BuiltinType::WasmExternRef; |
| 2601 | return false; |
| 2602 | } |
| 2603 | |
| 2604 | bool Type::isWebAssemblyTableType() const { |
| 2605 | if (const auto *ATy = dyn_cast<ArrayType>(Val: this)) |
| 2606 | return ATy->getElementType().isWebAssemblyReferenceType(); |
| 2607 | |
| 2608 | if (const auto *PTy = dyn_cast<PointerType>(Val: this)) |
| 2609 | return PTy->getPointeeType().isWebAssemblyReferenceType(); |
| 2610 | |
| 2611 | return false; |
| 2612 | } |
| 2613 | |
| 2614 | bool Type::isSizelessType() const { return isSizelessBuiltinType(); } |
| 2615 | |
| 2616 | bool Type::isSizelessVectorType() const { |
| 2617 | return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType(); |
| 2618 | } |
| 2619 | |
| 2620 | bool Type::isSVESizelessBuiltinType() const { |
| 2621 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 2622 | switch (BT->getKind()) { |
| 2623 | // SVE Types |
| 2624 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \ |
| 2625 | case BuiltinType::Id: \ |
| 2626 | return true; |
| 2627 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingletonId) \ |
| 2628 | case BuiltinType::Id: \ |
| 2629 | return true; |
| 2630 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \ |
| 2631 | case BuiltinType::Id: \ |
| 2632 | return true; |
| 2633 | #include "clang/Basic/AArch64ACLETypes.def" |
| 2634 | default: |
| 2635 | return false; |
| 2636 | } |
| 2637 | } |
| 2638 | return false; |
| 2639 | } |
| 2640 | |
| 2641 | bool Type::isRVVSizelessBuiltinType() const { |
| 2642 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 2643 | switch (BT->getKind()) { |
| 2644 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 2645 | #include "clang/Basic/RISCVVTypes.def" |
| 2646 | return true; |
| 2647 | default: |
| 2648 | return false; |
| 2649 | } |
| 2650 | } |
| 2651 | return false; |
| 2652 | } |
| 2653 | |
| 2654 | bool Type::isSveVLSBuiltinType() const { |
| 2655 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 2656 | switch (BT->getKind()) { |
| 2657 | case BuiltinType::SveInt8: |
| 2658 | case BuiltinType::SveInt16: |
| 2659 | case BuiltinType::SveInt32: |
| 2660 | case BuiltinType::SveInt64: |
| 2661 | case BuiltinType::SveUint8: |
| 2662 | case BuiltinType::SveUint16: |
| 2663 | case BuiltinType::SveUint32: |
| 2664 | case BuiltinType::SveUint64: |
| 2665 | case BuiltinType::SveFloat16: |
| 2666 | case BuiltinType::SveFloat32: |
| 2667 | case BuiltinType::SveFloat64: |
| 2668 | case BuiltinType::SveBFloat16: |
| 2669 | case BuiltinType::SveBool: |
| 2670 | case BuiltinType::SveBoolx2: |
| 2671 | case BuiltinType::SveBoolx4: |
| 2672 | case BuiltinType::SveMFloat8: |
| 2673 | return true; |
| 2674 | default: |
| 2675 | return false; |
| 2676 | } |
| 2677 | } |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
| 2681 | QualType Type::getSizelessVectorEltType(const ASTContext &Ctx) const { |
| 2682 | assert(isSizelessVectorType() && "Must be sizeless vector type" ); |
| 2683 | // Currently supports SVE and RVV |
| 2684 | if (isSVESizelessBuiltinType()) |
| 2685 | return getSveEltType(Ctx); |
| 2686 | |
| 2687 | if (isRVVSizelessBuiltinType()) |
| 2688 | return getRVVEltType(Ctx); |
| 2689 | |
| 2690 | llvm_unreachable("Unhandled type" ); |
| 2691 | } |
| 2692 | |
| 2693 | QualType Type::getSveEltType(const ASTContext &Ctx) const { |
| 2694 | assert(isSveVLSBuiltinType() && "unsupported type!" ); |
| 2695 | |
| 2696 | const BuiltinType *BTy = castAs<BuiltinType>(); |
| 2697 | if (BTy->getKind() == BuiltinType::SveBool) |
| 2698 | // Represent predicates as i8 rather than i1 to avoid any layout issues. |
| 2699 | // The type is bitcasted to a scalable predicate type when casting between |
| 2700 | // scalable and fixed-length vectors. |
| 2701 | return Ctx.UnsignedCharTy; |
| 2702 | else |
| 2703 | return Ctx.getBuiltinVectorTypeInfo(VecTy: BTy).ElementType; |
| 2704 | } |
| 2705 | |
| 2706 | bool Type::isRVVVLSBuiltinType() const { |
| 2707 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 2708 | switch (BT->getKind()) { |
| 2709 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, NumEls, ElBits, NF, IsSigned, \ |
| 2710 | IsFP, IsBF) \ |
| 2711 | case BuiltinType::Id: \ |
| 2712 | return NF == 1; |
| 2713 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
| 2714 | case BuiltinType::Id: \ |
| 2715 | return true; |
| 2716 | #include "clang/Basic/RISCVVTypes.def" |
| 2717 | default: |
| 2718 | return false; |
| 2719 | } |
| 2720 | } |
| 2721 | return false; |
| 2722 | } |
| 2723 | |
| 2724 | QualType Type::getRVVEltType(const ASTContext &Ctx) const { |
| 2725 | assert(isRVVVLSBuiltinType() && "unsupported type!" ); |
| 2726 | |
| 2727 | const BuiltinType *BTy = castAs<BuiltinType>(); |
| 2728 | |
| 2729 | switch (BTy->getKind()) { |
| 2730 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
| 2731 | case BuiltinType::Id: \ |
| 2732 | return Ctx.UnsignedCharTy; |
| 2733 | default: |
| 2734 | return Ctx.getBuiltinVectorTypeInfo(VecTy: BTy).ElementType; |
| 2735 | #include "clang/Basic/RISCVVTypes.def" |
| 2736 | } |
| 2737 | |
| 2738 | llvm_unreachable("Unhandled type" ); |
| 2739 | } |
| 2740 | |
| 2741 | bool QualType::isPODType(const ASTContext &Context) const { |
| 2742 | // C++11 has a more relaxed definition of POD. |
| 2743 | if (Context.getLangOpts().CPlusPlus11) |
| 2744 | return isCXX11PODType(Context); |
| 2745 | |
| 2746 | return isCXX98PODType(Context); |
| 2747 | } |
| 2748 | |
| 2749 | bool QualType::isCXX98PODType(const ASTContext &Context) const { |
| 2750 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 2751 | // We return false for that case. Except for incomplete arrays of PODs, which |
| 2752 | // are PODs according to the standard. |
| 2753 | if (isNull()) |
| 2754 | return false; |
| 2755 | |
| 2756 | if ((*this)->isIncompleteArrayType()) |
| 2757 | return Context.getBaseElementType(QT: *this).isCXX98PODType(Context); |
| 2758 | |
| 2759 | if ((*this)->isIncompleteType()) |
| 2760 | return false; |
| 2761 | |
| 2762 | if (hasNonTrivialObjCLifetime()) |
| 2763 | return false; |
| 2764 | |
| 2765 | QualType CanonicalType = getTypePtr()->CanonicalType; |
| 2766 | |
| 2767 | // Any type that is, or contains, address discriminated data is never POD. |
| 2768 | if (Context.containsAddressDiscriminatedPointerAuth(T: CanonicalType)) |
| 2769 | return false; |
| 2770 | |
| 2771 | switch (CanonicalType->getTypeClass()) { |
| 2772 | // Everything not explicitly mentioned is not POD. |
| 2773 | default: |
| 2774 | return false; |
| 2775 | case Type::VariableArray: |
| 2776 | case Type::ConstantArray: |
| 2777 | // IncompleteArray is handled above. |
| 2778 | return Context.getBaseElementType(QT: *this).isCXX98PODType(Context); |
| 2779 | |
| 2780 | case Type::ObjCObjectPointer: |
| 2781 | case Type::BlockPointer: |
| 2782 | case Type::Builtin: |
| 2783 | case Type::Complex: |
| 2784 | case Type::Pointer: |
| 2785 | case Type::MemberPointer: |
| 2786 | case Type::Vector: |
| 2787 | case Type::ExtVector: |
| 2788 | case Type::BitInt: |
| 2789 | case Type::OverflowBehavior: |
| 2790 | return true; |
| 2791 | |
| 2792 | case Type::Enum: |
| 2793 | return true; |
| 2794 | |
| 2795 | case Type::Record: |
| 2796 | if (const auto *ClassDecl = |
| 2797 | dyn_cast<CXXRecordDecl>(Val: cast<RecordType>(Val&: CanonicalType)->getDecl())) |
| 2798 | return ClassDecl->isPOD(); |
| 2799 | |
| 2800 | // C struct/union is POD. |
| 2801 | return true; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | bool QualType::isTrivialType(const ASTContext &Context) const { |
| 2806 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 2807 | // We return false for that case. Except for incomplete arrays of PODs, which |
| 2808 | // are PODs according to the standard. |
| 2809 | if (isNull()) |
| 2810 | return false; |
| 2811 | |
| 2812 | if ((*this)->isArrayType()) |
| 2813 | return Context.getBaseElementType(QT: *this).isTrivialType(Context); |
| 2814 | |
| 2815 | if ((*this)->isSizelessBuiltinType()) |
| 2816 | return true; |
| 2817 | |
| 2818 | // Return false for incomplete types after skipping any incomplete array |
| 2819 | // types which are expressly allowed by the standard and thus our API. |
| 2820 | if ((*this)->isIncompleteType()) |
| 2821 | return false; |
| 2822 | |
| 2823 | if (hasNonTrivialObjCLifetime()) |
| 2824 | return false; |
| 2825 | |
| 2826 | QualType CanonicalType = getTypePtr()->CanonicalType; |
| 2827 | if (CanonicalType->isDependentType()) |
| 2828 | return false; |
| 2829 | |
| 2830 | // Any type that is, or contains, address discriminated data is never a |
| 2831 | // trivial type. |
| 2832 | if (Context.containsAddressDiscriminatedPointerAuth(T: CanonicalType)) |
| 2833 | return false; |
| 2834 | |
| 2835 | // C++0x [basic.types]p9: |
| 2836 | // Scalar types, trivial class types, arrays of such types, and |
| 2837 | // cv-qualified versions of these types are collectively called trivial |
| 2838 | // types. |
| 2839 | |
| 2840 | // As an extension, Clang treats vector types as Scalar types. |
| 2841 | if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) |
| 2842 | return true; |
| 2843 | |
| 2844 | if (const auto *ClassDecl = CanonicalType->getAsCXXRecordDecl()) { |
| 2845 | // C++20 [class]p6: |
| 2846 | // A trivial class is a class that is trivially copyable, and |
| 2847 | // has one or more eligible default constructors such that each is |
| 2848 | // trivial. |
| 2849 | // FIXME: We should merge this definition of triviality into |
| 2850 | // CXXRecordDecl::isTrivial. Currently it computes the wrong thing. |
| 2851 | return ClassDecl->hasTrivialDefaultConstructor() && |
| 2852 | !ClassDecl->hasNonTrivialDefaultConstructor() && |
| 2853 | ClassDecl->isTriviallyCopyable(); |
| 2854 | } |
| 2855 | |
| 2856 | if (isa<RecordType>(Val: CanonicalType)) |
| 2857 | return true; |
| 2858 | |
| 2859 | // No other types can match. |
| 2860 | return false; |
| 2861 | } |
| 2862 | |
| 2863 | static bool isTriviallyCopyableTypeImpl(const QualType &type, |
| 2864 | const ASTContext &Context, |
| 2865 | bool IsCopyConstructible) { |
| 2866 | if (type->isArrayType()) |
| 2867 | return isTriviallyCopyableTypeImpl(type: Context.getBaseElementType(QT: type), |
| 2868 | Context, IsCopyConstructible); |
| 2869 | |
| 2870 | if (type.hasNonTrivialObjCLifetime()) |
| 2871 | return false; |
| 2872 | |
| 2873 | // C++11 [basic.types]p9 - See Core 2094 |
| 2874 | // Scalar types, trivially copyable class types, arrays of such types, and |
| 2875 | // cv-qualified versions of these types are collectively |
| 2876 | // called trivially copy constructible types. |
| 2877 | |
| 2878 | QualType CanonicalType = type.getCanonicalType(); |
| 2879 | if (CanonicalType->isDependentType()) |
| 2880 | return false; |
| 2881 | |
| 2882 | if (CanonicalType->isSizelessBuiltinType()) |
| 2883 | return true; |
| 2884 | |
| 2885 | // Return false for incomplete types after skipping any incomplete array types |
| 2886 | // which are expressly allowed by the standard and thus our API. |
| 2887 | if (CanonicalType->isIncompleteType()) |
| 2888 | return false; |
| 2889 | |
| 2890 | if (CanonicalType.hasAddressDiscriminatedPointerAuth()) |
| 2891 | return false; |
| 2892 | |
| 2893 | // As an extension, Clang treats vector types as Scalar types. |
| 2894 | if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) |
| 2895 | return true; |
| 2896 | |
| 2897 | // Mfloat8 type is a special case as it not scalar, but is still trivially |
| 2898 | // copyable. |
| 2899 | if (CanonicalType->isMFloat8Type()) |
| 2900 | return true; |
| 2901 | |
| 2902 | if (const auto *RD = CanonicalType->getAsRecordDecl()) { |
| 2903 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD)) { |
| 2904 | if (IsCopyConstructible) |
| 2905 | return ClassDecl->isTriviallyCopyConstructible(); |
| 2906 | return ClassDecl->isTriviallyCopyable(); |
| 2907 | } |
| 2908 | return !RD->isNonTrivialToPrimitiveCopy(); |
| 2909 | } |
| 2910 | // No other types can match. |
| 2911 | return false; |
| 2912 | } |
| 2913 | |
| 2914 | bool QualType::isTriviallyCopyableType(const ASTContext &Context) const { |
| 2915 | return isTriviallyCopyableTypeImpl(type: *this, Context, |
| 2916 | /*IsCopyConstructible=*/false); |
| 2917 | } |
| 2918 | |
| 2919 | // FIXME: each call will trigger a full computation, cache the result. |
| 2920 | bool QualType::isBitwiseCloneableType(const ASTContext &Context) const { |
| 2921 | auto CanonicalType = getCanonicalType(); |
| 2922 | if (CanonicalType.hasNonTrivialObjCLifetime()) |
| 2923 | return false; |
| 2924 | if (CanonicalType->isArrayType()) |
| 2925 | return Context.getBaseElementType(QT: CanonicalType) |
| 2926 | .isBitwiseCloneableType(Context); |
| 2927 | |
| 2928 | if (CanonicalType->isIncompleteType()) |
| 2929 | return false; |
| 2930 | |
| 2931 | // Any type that is, or contains, address discriminated data is never |
| 2932 | // bitwise clonable. |
| 2933 | if (Context.containsAddressDiscriminatedPointerAuth(T: CanonicalType)) |
| 2934 | return false; |
| 2935 | |
| 2936 | const auto *RD = CanonicalType->getAsRecordDecl(); // struct/union/class |
| 2937 | if (!RD) |
| 2938 | return true; |
| 2939 | |
| 2940 | if (RD->isInvalidDecl()) |
| 2941 | return false; |
| 2942 | |
| 2943 | // Never allow memcpy when we're adding poisoned padding bits to the struct. |
| 2944 | // Accessing these posioned bits will trigger false alarms on |
| 2945 | // SanitizeAddressFieldPadding etc. |
| 2946 | if (RD->mayInsertExtraPadding()) |
| 2947 | return false; |
| 2948 | |
| 2949 | for (auto *const Field : RD->fields()) { |
| 2950 | if (!Field->getType().isBitwiseCloneableType(Context)) |
| 2951 | return false; |
| 2952 | } |
| 2953 | |
| 2954 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
| 2955 | for (auto Base : CXXRD->bases()) |
| 2956 | if (!Base.getType().isBitwiseCloneableType(Context)) |
| 2957 | return false; |
| 2958 | for (auto VBase : CXXRD->vbases()) |
| 2959 | if (!VBase.getType().isBitwiseCloneableType(Context)) |
| 2960 | return false; |
| 2961 | } |
| 2962 | return true; |
| 2963 | } |
| 2964 | |
| 2965 | bool QualType::isTriviallyCopyConstructibleType( |
| 2966 | const ASTContext &Context) const { |
| 2967 | return isTriviallyCopyableTypeImpl(type: *this, Context, |
| 2968 | /*IsCopyConstructible=*/true); |
| 2969 | } |
| 2970 | |
| 2971 | bool QualType::isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const { |
| 2972 | return !Context.getLangOpts().ObjCAutoRefCount && |
| 2973 | Context.getLangOpts().ObjCWeak && |
| 2974 | getObjCLifetime() != Qualifiers::OCL_Weak; |
| 2975 | } |
| 2976 | |
| 2977 | bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion( |
| 2978 | const RecordDecl *RD) { |
| 2979 | return RD->hasNonTrivialToPrimitiveDefaultInitializeCUnion(); |
| 2980 | } |
| 2981 | |
| 2982 | bool QualType::hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD) { |
| 2983 | return RD->hasNonTrivialToPrimitiveDestructCUnion(); |
| 2984 | } |
| 2985 | |
| 2986 | bool QualType::hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD) { |
| 2987 | return RD->hasNonTrivialToPrimitiveCopyCUnion(); |
| 2988 | } |
| 2989 | |
| 2990 | bool QualType::isWebAssemblyReferenceType() const { |
| 2991 | return isWebAssemblyExternrefType() || isWebAssemblyFuncrefType(); |
| 2992 | } |
| 2993 | |
| 2994 | bool QualType::isWebAssemblyExternrefType() const { |
| 2995 | return getTypePtr()->isWebAssemblyExternrefType(); |
| 2996 | } |
| 2997 | |
| 2998 | bool QualType::isWebAssemblyFuncrefType() const { |
| 2999 | return getTypePtr()->isFunctionPointerType() && |
| 3000 | (getTypePtr()->getPointeeType().getAddressSpace() == |
| 3001 | LangAS::wasm_funcref); |
| 3002 | } |
| 3003 | |
| 3004 | bool QualType::isWrapType() const { |
| 3005 | if (const auto *OBT = getCanonicalType()->getAs<OverflowBehaviorType>()) |
| 3006 | return OBT->getBehaviorKind() == |
| 3007 | OverflowBehaviorType::OverflowBehaviorKind::Wrap; |
| 3008 | |
| 3009 | return false; |
| 3010 | } |
| 3011 | |
| 3012 | bool QualType::isTrapType() const { |
| 3013 | if (const auto *OBT = getCanonicalType()->getAs<OverflowBehaviorType>()) |
| 3014 | return OBT->getBehaviorKind() == |
| 3015 | OverflowBehaviorType::OverflowBehaviorKind::Trap; |
| 3016 | |
| 3017 | return false; |
| 3018 | } |
| 3019 | |
| 3020 | QualType::PrimitiveDefaultInitializeKind |
| 3021 | QualType::isNonTrivialToPrimitiveDefaultInitialize() const { |
| 3022 | if (const auto *RD = |
| 3023 | getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
| 3024 | if (RD->isNonTrivialToPrimitiveDefaultInitialize()) |
| 3025 | return PDIK_Struct; |
| 3026 | |
| 3027 | switch (getQualifiers().getObjCLifetime()) { |
| 3028 | case Qualifiers::OCL_Strong: |
| 3029 | return PDIK_ARCStrong; |
| 3030 | case Qualifiers::OCL_Weak: |
| 3031 | return PDIK_ARCWeak; |
| 3032 | default: |
| 3033 | return PDIK_Trivial; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | QualType::PrimitiveCopyKind QualType::isNonTrivialToPrimitiveCopy() const { |
| 3038 | if (const auto *RD = |
| 3039 | getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
| 3040 | if (RD->isNonTrivialToPrimitiveCopy()) |
| 3041 | return PCK_Struct; |
| 3042 | |
| 3043 | Qualifiers Qs = getQualifiers(); |
| 3044 | switch (Qs.getObjCLifetime()) { |
| 3045 | case Qualifiers::OCL_Strong: |
| 3046 | return PCK_ARCStrong; |
| 3047 | case Qualifiers::OCL_Weak: |
| 3048 | return PCK_ARCWeak; |
| 3049 | default: |
| 3050 | if (hasAddressDiscriminatedPointerAuth()) |
| 3051 | return PCK_PtrAuth; |
| 3052 | return Qs.hasVolatile() ? PCK_VolatileTrivial : PCK_Trivial; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | QualType::PrimitiveCopyKind |
| 3057 | QualType::isNonTrivialToPrimitiveDestructiveMove() const { |
| 3058 | return isNonTrivialToPrimitiveCopy(); |
| 3059 | } |
| 3060 | |
| 3061 | bool Type::isLiteralType(const ASTContext &Ctx) const { |
| 3062 | if (isDependentType()) |
| 3063 | return false; |
| 3064 | |
| 3065 | // C++1y [basic.types]p10: |
| 3066 | // A type is a literal type if it is: |
| 3067 | // -- cv void; or |
| 3068 | if (Ctx.getLangOpts().CPlusPlus14 && isVoidType()) |
| 3069 | return true; |
| 3070 | |
| 3071 | // C++11 [basic.types]p10: |
| 3072 | // A type is a literal type if it is: |
| 3073 | // [...] |
| 3074 | // -- an array of literal type other than an array of runtime bound; or |
| 3075 | if (isVariableArrayType()) |
| 3076 | return false; |
| 3077 | const Type *BaseTy = getBaseElementTypeUnsafe(); |
| 3078 | assert(BaseTy && "NULL element type" ); |
| 3079 | |
| 3080 | // Return false for incomplete types after skipping any incomplete array |
| 3081 | // types; those are expressly allowed by the standard and thus our API. |
| 3082 | if (BaseTy->isIncompleteType()) |
| 3083 | return false; |
| 3084 | |
| 3085 | // C++11 [basic.types]p10: |
| 3086 | // A type is a literal type if it is: |
| 3087 | // -- a scalar type; or |
| 3088 | // As an extension, Clang treats vector types and complex types as |
| 3089 | // literal types. |
| 3090 | if (BaseTy->isScalarType() || BaseTy->isVectorType() || |
| 3091 | BaseTy->isAnyComplexType()) |
| 3092 | return true; |
| 3093 | // Matrices with constant numbers of rows and columns are also literal types |
| 3094 | // in HLSL. |
| 3095 | if (Ctx.getLangOpts().HLSL && BaseTy->isConstantMatrixType()) |
| 3096 | return true; |
| 3097 | // -- a reference type; or |
| 3098 | if (BaseTy->isReferenceType()) |
| 3099 | return true; |
| 3100 | // -- a class type that has all of the following properties: |
| 3101 | if (const auto *RD = BaseTy->getAsRecordDecl()) { |
| 3102 | // -- a trivial destructor, |
| 3103 | // -- every constructor call and full-expression in the |
| 3104 | // brace-or-equal-initializers for non-static data members (if any) |
| 3105 | // is a constant expression, |
| 3106 | // -- it is an aggregate type or has at least one constexpr |
| 3107 | // constructor or constructor template that is not a copy or move |
| 3108 | // constructor, and |
| 3109 | // -- all non-static data members and base classes of literal types |
| 3110 | // |
| 3111 | // We resolve DR1361 by ignoring the second bullet. |
| 3112 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD)) |
| 3113 | return ClassDecl->isLiteral(); |
| 3114 | |
| 3115 | return true; |
| 3116 | } |
| 3117 | |
| 3118 | // We treat _Atomic T as a literal type if T is a literal type. |
| 3119 | if (const auto *AT = BaseTy->getAs<AtomicType>()) |
| 3120 | return AT->getValueType()->isLiteralType(Ctx); |
| 3121 | |
| 3122 | if (const auto *OBT = BaseTy->getAs<OverflowBehaviorType>()) |
| 3123 | return OBT->getUnderlyingType()->isLiteralType(Ctx); |
| 3124 | |
| 3125 | // If this type hasn't been deduced yet, then conservatively assume that |
| 3126 | // it'll work out to be a literal type. |
| 3127 | if (isa<AutoType>(Val: BaseTy->getCanonicalTypeInternal())) |
| 3128 | return true; |
| 3129 | |
| 3130 | return false; |
| 3131 | } |
| 3132 | |
| 3133 | bool Type::isStructuralType() const { |
| 3134 | // C++20 [temp.param]p6: |
| 3135 | // A structural type is one of the following: |
| 3136 | // -- a scalar type; or |
| 3137 | // -- a vector type [Clang extension]; or |
| 3138 | if (isScalarType() || isVectorType()) |
| 3139 | return true; |
| 3140 | // -- an lvalue reference type; or |
| 3141 | if (isLValueReferenceType()) |
| 3142 | return true; |
| 3143 | // -- a literal class type [...under some conditions] |
| 3144 | if (const CXXRecordDecl *RD = getAsCXXRecordDecl()) |
| 3145 | return RD->isStructural(); |
| 3146 | return false; |
| 3147 | } |
| 3148 | |
| 3149 | bool Type::isStandardLayoutType() const { |
| 3150 | if (isDependentType()) |
| 3151 | return false; |
| 3152 | |
| 3153 | // C++0x [basic.types]p9: |
| 3154 | // Scalar types, standard-layout class types, arrays of such types, and |
| 3155 | // cv-qualified versions of these types are collectively called |
| 3156 | // standard-layout types. |
| 3157 | const Type *BaseTy = getBaseElementTypeUnsafe(); |
| 3158 | assert(BaseTy && "NULL element type" ); |
| 3159 | |
| 3160 | // Return false for incomplete types after skipping any incomplete array |
| 3161 | // types which are expressly allowed by the standard and thus our API. |
| 3162 | if (BaseTy->isIncompleteType()) |
| 3163 | return false; |
| 3164 | |
| 3165 | // As an extension, Clang treats vector types as Scalar types. |
| 3166 | if (BaseTy->isScalarType() || BaseTy->isVectorType()) |
| 3167 | return true; |
| 3168 | if (const auto *RD = BaseTy->getAsRecordDecl()) { |
| 3169 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD); |
| 3170 | ClassDecl && !ClassDecl->isStandardLayout()) |
| 3171 | return false; |
| 3172 | |
| 3173 | // Default to 'true' for non-C++ class types. |
| 3174 | // FIXME: This is a bit dubious, but plain C structs should trivially meet |
| 3175 | // all the requirements of standard layout classes. |
| 3176 | return true; |
| 3177 | } |
| 3178 | |
| 3179 | // No other types can match. |
| 3180 | return false; |
| 3181 | } |
| 3182 | |
| 3183 | // This is effectively the intersection of isTrivialType and |
| 3184 | // isStandardLayoutType. We implement it directly to avoid redundant |
| 3185 | // conversions from a type to a CXXRecordDecl. |
| 3186 | bool QualType::isCXX11PODType(const ASTContext &Context) const { |
| 3187 | const Type *ty = getTypePtr(); |
| 3188 | if (ty->isDependentType()) |
| 3189 | return false; |
| 3190 | |
| 3191 | if (hasNonTrivialObjCLifetime()) |
| 3192 | return false; |
| 3193 | |
| 3194 | // C++11 [basic.types]p9: |
| 3195 | // Scalar types, POD classes, arrays of such types, and cv-qualified |
| 3196 | // versions of these types are collectively called trivial types. |
| 3197 | const Type *BaseTy = ty->getBaseElementTypeUnsafe(); |
| 3198 | assert(BaseTy && "NULL element type" ); |
| 3199 | |
| 3200 | if (BaseTy->isSizelessBuiltinType()) |
| 3201 | return true; |
| 3202 | |
| 3203 | // Return false for incomplete types after skipping any incomplete array |
| 3204 | // types which are expressly allowed by the standard and thus our API. |
| 3205 | if (BaseTy->isIncompleteType()) |
| 3206 | return false; |
| 3207 | |
| 3208 | // Any type that is, or contains, address discriminated data is non-POD. |
| 3209 | if (Context.containsAddressDiscriminatedPointerAuth(T: *this)) |
| 3210 | return false; |
| 3211 | |
| 3212 | // As an extension, Clang treats vector types as Scalar types. |
| 3213 | if (BaseTy->isScalarType() || BaseTy->isVectorType()) |
| 3214 | return true; |
| 3215 | if (const auto *RD = BaseTy->getAsRecordDecl()) { |
| 3216 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD)) { |
| 3217 | // C++11 [class]p10: |
| 3218 | // A POD struct is a non-union class that is both a trivial class [...] |
| 3219 | if (!ClassDecl->isTrivial()) |
| 3220 | return false; |
| 3221 | |
| 3222 | // C++11 [class]p10: |
| 3223 | // A POD struct is a non-union class that is both a trivial class and |
| 3224 | // a standard-layout class [...] |
| 3225 | if (!ClassDecl->isStandardLayout()) |
| 3226 | return false; |
| 3227 | |
| 3228 | // C++11 [class]p10: |
| 3229 | // A POD struct is a non-union class that is both a trivial class and |
| 3230 | // a standard-layout class, and has no non-static data members of type |
| 3231 | // non-POD struct, non-POD union (or array of such types). [...] |
| 3232 | // |
| 3233 | // We don't directly query the recursive aspect as the requirements for |
| 3234 | // both standard-layout classes and trivial classes apply recursively |
| 3235 | // already. |
| 3236 | } |
| 3237 | |
| 3238 | return true; |
| 3239 | } |
| 3240 | |
| 3241 | // No other types can match. |
| 3242 | return false; |
| 3243 | } |
| 3244 | |
| 3245 | bool Type::isNothrowT() const { |
| 3246 | if (const auto *RD = getAsCXXRecordDecl()) { |
| 3247 | IdentifierInfo *II = RD->getIdentifier(); |
| 3248 | if (II && II->isStr(Str: "nothrow_t" ) && RD->isInStdNamespace()) |
| 3249 | return true; |
| 3250 | } |
| 3251 | return false; |
| 3252 | } |
| 3253 | |
| 3254 | bool Type::isAlignValT() const { |
| 3255 | if (const auto *ET = getAsCanonical<EnumType>()) { |
| 3256 | const auto *ED = ET->getDecl(); |
| 3257 | IdentifierInfo *II = ED->getIdentifier(); |
| 3258 | if (II && II->isStr(Str: "align_val_t" ) && ED->isInStdNamespace()) |
| 3259 | return true; |
| 3260 | } |
| 3261 | return false; |
| 3262 | } |
| 3263 | |
| 3264 | bool Type::isStdByteType() const { |
| 3265 | if (const auto *ET = getAsCanonical<EnumType>()) { |
| 3266 | const auto *ED = ET->getDecl(); |
| 3267 | IdentifierInfo *II = ED->getIdentifier(); |
| 3268 | if (II && II->isStr(Str: "byte" ) && ED->isInStdNamespace()) |
| 3269 | return true; |
| 3270 | } |
| 3271 | return false; |
| 3272 | } |
| 3273 | |
| 3274 | bool Type::isSpecifierType() const { |
| 3275 | // Note that this intentionally does not use the canonical type. |
| 3276 | switch (getTypeClass()) { |
| 3277 | case Builtin: |
| 3278 | case Record: |
| 3279 | case Enum: |
| 3280 | case Typedef: |
| 3281 | case Complex: |
| 3282 | case TypeOfExpr: |
| 3283 | case TypeOf: |
| 3284 | case TemplateTypeParm: |
| 3285 | case SubstTemplateTypeParm: |
| 3286 | case TemplateSpecialization: |
| 3287 | case DependentName: |
| 3288 | case ObjCInterface: |
| 3289 | case ObjCObject: |
| 3290 | return true; |
| 3291 | default: |
| 3292 | return false; |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | ElaboratedTypeKeyword KeywordHelpers::getKeywordForTypeSpec(unsigned TypeSpec) { |
| 3297 | switch (TypeSpec) { |
| 3298 | default: |
| 3299 | return ElaboratedTypeKeyword::None; |
| 3300 | case TST_typename: |
| 3301 | return ElaboratedTypeKeyword::Typename; |
| 3302 | case TST_class: |
| 3303 | return ElaboratedTypeKeyword::Class; |
| 3304 | case TST_struct: |
| 3305 | return ElaboratedTypeKeyword::Struct; |
| 3306 | case TST_interface: |
| 3307 | return ElaboratedTypeKeyword::Interface; |
| 3308 | case TST_union: |
| 3309 | return ElaboratedTypeKeyword::Union; |
| 3310 | case TST_enum: |
| 3311 | return ElaboratedTypeKeyword::Enum; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | TagTypeKind KeywordHelpers::getTagTypeKindForTypeSpec(unsigned TypeSpec) { |
| 3316 | switch (TypeSpec) { |
| 3317 | case TST_class: |
| 3318 | return TagTypeKind::Class; |
| 3319 | case TST_struct: |
| 3320 | return TagTypeKind::Struct; |
| 3321 | case TST_interface: |
| 3322 | return TagTypeKind::Interface; |
| 3323 | case TST_union: |
| 3324 | return TagTypeKind::Union; |
| 3325 | case TST_enum: |
| 3326 | return TagTypeKind::Enum; |
| 3327 | } |
| 3328 | |
| 3329 | llvm_unreachable("Type specifier is not a tag type kind." ); |
| 3330 | } |
| 3331 | |
| 3332 | ElaboratedTypeKeyword |
| 3333 | KeywordHelpers::getKeywordForTagTypeKind(TagTypeKind Kind) { |
| 3334 | switch (Kind) { |
| 3335 | case TagTypeKind::Class: |
| 3336 | return ElaboratedTypeKeyword::Class; |
| 3337 | case TagTypeKind::Struct: |
| 3338 | return ElaboratedTypeKeyword::Struct; |
| 3339 | case TagTypeKind::Interface: |
| 3340 | return ElaboratedTypeKeyword::Interface; |
| 3341 | case TagTypeKind::Union: |
| 3342 | return ElaboratedTypeKeyword::Union; |
| 3343 | case TagTypeKind::Enum: |
| 3344 | return ElaboratedTypeKeyword::Enum; |
| 3345 | } |
| 3346 | llvm_unreachable("Unknown tag type kind." ); |
| 3347 | } |
| 3348 | |
| 3349 | TagTypeKind |
| 3350 | KeywordHelpers::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) { |
| 3351 | switch (Keyword) { |
| 3352 | case ElaboratedTypeKeyword::Class: |
| 3353 | return TagTypeKind::Class; |
| 3354 | case ElaboratedTypeKeyword::Struct: |
| 3355 | return TagTypeKind::Struct; |
| 3356 | case ElaboratedTypeKeyword::Interface: |
| 3357 | return TagTypeKind::Interface; |
| 3358 | case ElaboratedTypeKeyword::Union: |
| 3359 | return TagTypeKind::Union; |
| 3360 | case ElaboratedTypeKeyword::Enum: |
| 3361 | return TagTypeKind::Enum; |
| 3362 | case ElaboratedTypeKeyword::None: // Fall through. |
| 3363 | case ElaboratedTypeKeyword::Typename: |
| 3364 | llvm_unreachable("Elaborated type keyword is not a tag type kind." ); |
| 3365 | } |
| 3366 | llvm_unreachable("Unknown elaborated type keyword." ); |
| 3367 | } |
| 3368 | |
| 3369 | bool KeywordHelpers::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) { |
| 3370 | switch (Keyword) { |
| 3371 | case ElaboratedTypeKeyword::None: |
| 3372 | case ElaboratedTypeKeyword::Typename: |
| 3373 | return false; |
| 3374 | case ElaboratedTypeKeyword::Class: |
| 3375 | case ElaboratedTypeKeyword::Struct: |
| 3376 | case ElaboratedTypeKeyword::Interface: |
| 3377 | case ElaboratedTypeKeyword::Union: |
| 3378 | case ElaboratedTypeKeyword::Enum: |
| 3379 | return true; |
| 3380 | } |
| 3381 | llvm_unreachable("Unknown elaborated type keyword." ); |
| 3382 | } |
| 3383 | |
| 3384 | StringRef KeywordHelpers::getKeywordName(ElaboratedTypeKeyword Keyword) { |
| 3385 | switch (Keyword) { |
| 3386 | case ElaboratedTypeKeyword::None: |
| 3387 | return {}; |
| 3388 | case ElaboratedTypeKeyword::Typename: |
| 3389 | return "typename" ; |
| 3390 | case ElaboratedTypeKeyword::Class: |
| 3391 | return "class" ; |
| 3392 | case ElaboratedTypeKeyword::Struct: |
| 3393 | return "struct" ; |
| 3394 | case ElaboratedTypeKeyword::Interface: |
| 3395 | return "__interface" ; |
| 3396 | case ElaboratedTypeKeyword::Union: |
| 3397 | return "union" ; |
| 3398 | case ElaboratedTypeKeyword::Enum: |
| 3399 | return "enum" ; |
| 3400 | } |
| 3401 | |
| 3402 | llvm_unreachable("Unknown elaborated type keyword." ); |
| 3403 | } |
| 3404 | |
| 3405 | bool Type::isElaboratedTypeSpecifier() const { |
| 3406 | ElaboratedTypeKeyword Keyword; |
| 3407 | if (const auto *TST = dyn_cast<TemplateSpecializationType>(Val: this)) |
| 3408 | Keyword = TST->getKeyword(); |
| 3409 | else if (const auto *DepName = dyn_cast<DependentNameType>(Val: this)) |
| 3410 | Keyword = DepName->getKeyword(); |
| 3411 | else if (const auto *T = dyn_cast<TagType>(Val: this)) |
| 3412 | Keyword = T->getKeyword(); |
| 3413 | else if (const auto *T = dyn_cast<TypedefType>(Val: this)) |
| 3414 | Keyword = T->getKeyword(); |
| 3415 | else if (const auto *T = dyn_cast<UnresolvedUsingType>(Val: this)) |
| 3416 | Keyword = T->getKeyword(); |
| 3417 | else if (const auto *T = dyn_cast<UsingType>(Val: this)) |
| 3418 | Keyword = T->getKeyword(); |
| 3419 | else |
| 3420 | return false; |
| 3421 | |
| 3422 | return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); |
| 3423 | } |
| 3424 | |
| 3425 | const char *Type::getTypeClassName() const { |
| 3426 | switch (TypeBits.TC) { |
| 3427 | #define ABSTRACT_TYPE(Derived, Base) |
| 3428 | #define TYPE(Derived, Base) \ |
| 3429 | case Derived: \ |
| 3430 | return #Derived; |
| 3431 | #include "clang/AST/TypeNodes.inc" |
| 3432 | } |
| 3433 | |
| 3434 | llvm_unreachable("Invalid type class." ); |
| 3435 | } |
| 3436 | |
| 3437 | StringRef BuiltinType::getName(const PrintingPolicy &Policy) const { |
| 3438 | switch (getKind()) { |
| 3439 | case Void: |
| 3440 | return "void" ; |
| 3441 | case Bool: |
| 3442 | return Policy.Bool ? "bool" : "_Bool" ; |
| 3443 | case Char_S: |
| 3444 | return "char" ; |
| 3445 | case Char_U: |
| 3446 | return "char" ; |
| 3447 | case SChar: |
| 3448 | return "signed char" ; |
| 3449 | case Short: |
| 3450 | return "short" ; |
| 3451 | case Int: |
| 3452 | return "int" ; |
| 3453 | case Long: |
| 3454 | return "long" ; |
| 3455 | case LongLong: |
| 3456 | return "long long" ; |
| 3457 | case Int128: |
| 3458 | return "__int128" ; |
| 3459 | case UChar: |
| 3460 | return "unsigned char" ; |
| 3461 | case UShort: |
| 3462 | return "unsigned short" ; |
| 3463 | case UInt: |
| 3464 | return "unsigned int" ; |
| 3465 | case ULong: |
| 3466 | return "unsigned long" ; |
| 3467 | case ULongLong: |
| 3468 | return "unsigned long long" ; |
| 3469 | case UInt128: |
| 3470 | return "unsigned __int128" ; |
| 3471 | case Half: |
| 3472 | return Policy.Half ? "half" : "__fp16" ; |
| 3473 | case BFloat16: |
| 3474 | return "__bf16" ; |
| 3475 | case Float: |
| 3476 | return "float" ; |
| 3477 | case Double: |
| 3478 | return "double" ; |
| 3479 | case LongDouble: |
| 3480 | return "long double" ; |
| 3481 | case ShortAccum: |
| 3482 | return "short _Accum" ; |
| 3483 | case Accum: |
| 3484 | return "_Accum" ; |
| 3485 | case LongAccum: |
| 3486 | return "long _Accum" ; |
| 3487 | case UShortAccum: |
| 3488 | return "unsigned short _Accum" ; |
| 3489 | case UAccum: |
| 3490 | return "unsigned _Accum" ; |
| 3491 | case ULongAccum: |
| 3492 | return "unsigned long _Accum" ; |
| 3493 | case BuiltinType::ShortFract: |
| 3494 | return "short _Fract" ; |
| 3495 | case BuiltinType::Fract: |
| 3496 | return "_Fract" ; |
| 3497 | case BuiltinType::LongFract: |
| 3498 | return "long _Fract" ; |
| 3499 | case BuiltinType::UShortFract: |
| 3500 | return "unsigned short _Fract" ; |
| 3501 | case BuiltinType::UFract: |
| 3502 | return "unsigned _Fract" ; |
| 3503 | case BuiltinType::ULongFract: |
| 3504 | return "unsigned long _Fract" ; |
| 3505 | case BuiltinType::SatShortAccum: |
| 3506 | return "_Sat short _Accum" ; |
| 3507 | case BuiltinType::SatAccum: |
| 3508 | return "_Sat _Accum" ; |
| 3509 | case BuiltinType::SatLongAccum: |
| 3510 | return "_Sat long _Accum" ; |
| 3511 | case BuiltinType::SatUShortAccum: |
| 3512 | return "_Sat unsigned short _Accum" ; |
| 3513 | case BuiltinType::SatUAccum: |
| 3514 | return "_Sat unsigned _Accum" ; |
| 3515 | case BuiltinType::SatULongAccum: |
| 3516 | return "_Sat unsigned long _Accum" ; |
| 3517 | case BuiltinType::SatShortFract: |
| 3518 | return "_Sat short _Fract" ; |
| 3519 | case BuiltinType::SatFract: |
| 3520 | return "_Sat _Fract" ; |
| 3521 | case BuiltinType::SatLongFract: |
| 3522 | return "_Sat long _Fract" ; |
| 3523 | case BuiltinType::SatUShortFract: |
| 3524 | return "_Sat unsigned short _Fract" ; |
| 3525 | case BuiltinType::SatUFract: |
| 3526 | return "_Sat unsigned _Fract" ; |
| 3527 | case BuiltinType::SatULongFract: |
| 3528 | return "_Sat unsigned long _Fract" ; |
| 3529 | case Float16: |
| 3530 | return "_Float16" ; |
| 3531 | case Float128: |
| 3532 | return "__float128" ; |
| 3533 | case Ibm128: |
| 3534 | return "__ibm128" ; |
| 3535 | case WChar_S: |
| 3536 | case WChar_U: |
| 3537 | return Policy.MSWChar ? "__wchar_t" : "wchar_t" ; |
| 3538 | case Char8: |
| 3539 | return "char8_t" ; |
| 3540 | case Char16: |
| 3541 | return "char16_t" ; |
| 3542 | case Char32: |
| 3543 | return "char32_t" ; |
| 3544 | case NullPtr: |
| 3545 | return Policy.NullptrTypeInNamespace ? "std::nullptr_t" : "nullptr_t" ; |
| 3546 | case Overload: |
| 3547 | return "<overloaded function type>" ; |
| 3548 | case BoundMember: |
| 3549 | return "<bound member function type>" ; |
| 3550 | case UnresolvedTemplate: |
| 3551 | return "<unresolved template type>" ; |
| 3552 | case PseudoObject: |
| 3553 | return "<pseudo-object type>" ; |
| 3554 | case Dependent: |
| 3555 | return "<dependent type>" ; |
| 3556 | case UnknownAny: |
| 3557 | return "<unknown type>" ; |
| 3558 | case ARCUnbridgedCast: |
| 3559 | return "<ARC unbridged cast type>" ; |
| 3560 | case BuiltinFn: |
| 3561 | return "<builtin fn type>" ; |
| 3562 | case ObjCId: |
| 3563 | return "id" ; |
| 3564 | case ObjCClass: |
| 3565 | return "Class" ; |
| 3566 | case ObjCSel: |
| 3567 | return "SEL" ; |
| 3568 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 3569 | case Id: \ |
| 3570 | return "__" #Access " " #ImgType "_t"; |
| 3571 | #include "clang/Basic/OpenCLImageTypes.def" |
| 3572 | case OCLSampler: |
| 3573 | return "sampler_t" ; |
| 3574 | case OCLEvent: |
| 3575 | return "event_t" ; |
| 3576 | case OCLClkEvent: |
| 3577 | return "clk_event_t" ; |
| 3578 | case OCLQueue: |
| 3579 | return "queue_t" ; |
| 3580 | case OCLReserveID: |
| 3581 | return "reserve_id_t" ; |
| 3582 | case IncompleteMatrixIdx: |
| 3583 | return "<incomplete matrix index type>" ; |
| 3584 | case ArraySection: |
| 3585 | return "<array section type>" ; |
| 3586 | case OMPArrayShaping: |
| 3587 | return "<OpenMP array shaping type>" ; |
| 3588 | case OMPIterator: |
| 3589 | return "<OpenMP iterator type>" ; |
| 3590 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 3591 | case Id: \ |
| 3592 | return #ExtType; |
| 3593 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| 3594 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 3595 | case Id: \ |
| 3596 | return #Name; |
| 3597 | #include "clang/Basic/AArch64ACLETypes.def" |
| 3598 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
| 3599 | case Id: \ |
| 3600 | return #Name; |
| 3601 | #include "clang/Basic/PPCTypes.def" |
| 3602 | #define RVV_TYPE(Name, Id, SingletonId) \ |
| 3603 | case Id: \ |
| 3604 | return Name; |
| 3605 | #include "clang/Basic/RISCVVTypes.def" |
| 3606 | #define WASM_TYPE(Name, Id, SingletonId) \ |
| 3607 | case Id: \ |
| 3608 | return Name; |
| 3609 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 3610 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \ |
| 3611 | case Id: \ |
| 3612 | return Name; |
| 3613 | #include "clang/Basic/AMDGPUTypes.def" |
| 3614 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
| 3615 | case Id: \ |
| 3616 | return #Name; |
| 3617 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 3618 | } |
| 3619 | |
| 3620 | llvm_unreachable("Invalid builtin type." ); |
| 3621 | } |
| 3622 | |
| 3623 | QualType QualType::getNonPackExpansionType() const { |
| 3624 | // We never wrap type sugar around a PackExpansionType. |
| 3625 | if (auto *PET = dyn_cast<PackExpansionType>(Val: getTypePtr())) |
| 3626 | return PET->getPattern(); |
| 3627 | return *this; |
| 3628 | } |
| 3629 | |
| 3630 | QualType QualType::getNonLValueExprType(const ASTContext &Context) const { |
| 3631 | if (const auto *RefType = getTypePtr()->getAs<ReferenceType>()) |
| 3632 | return RefType->getPointeeType(); |
| 3633 | |
| 3634 | // C++0x [basic.lval]: |
| 3635 | // Class prvalues can have cv-qualified types; non-class prvalues always |
| 3636 | // have cv-unqualified types. |
| 3637 | // |
| 3638 | // See also C99 6.3.2.1p2. |
| 3639 | if (!Context.getLangOpts().CPlusPlus || |
| 3640 | (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType())) |
| 3641 | return getUnqualifiedType(); |
| 3642 | |
| 3643 | return *this; |
| 3644 | } |
| 3645 | |
| 3646 | bool FunctionType::getCFIUncheckedCalleeAttr() const { |
| 3647 | if (const auto *FPT = getAs<FunctionProtoType>()) |
| 3648 | return FPT->hasCFIUncheckedCallee(); |
| 3649 | return false; |
| 3650 | } |
| 3651 | |
| 3652 | StringRef FunctionType::getNameForCallConv(CallingConv CC) { |
| 3653 | switch (CC) { |
| 3654 | case CC_C: |
| 3655 | return "cdecl" ; |
| 3656 | case CC_X86StdCall: |
| 3657 | return "stdcall" ; |
| 3658 | case CC_X86FastCall: |
| 3659 | return "fastcall" ; |
| 3660 | case CC_X86ThisCall: |
| 3661 | return "thiscall" ; |
| 3662 | case CC_X86Pascal: |
| 3663 | return "pascal" ; |
| 3664 | case CC_X86VectorCall: |
| 3665 | return "vectorcall" ; |
| 3666 | case CC_Win64: |
| 3667 | return "ms_abi" ; |
| 3668 | case CC_X86_64SysV: |
| 3669 | return "sysv_abi" ; |
| 3670 | case CC_X86RegCall: |
| 3671 | return "regcall" ; |
| 3672 | case CC_AAPCS: |
| 3673 | return "aapcs" ; |
| 3674 | case CC_AAPCS_VFP: |
| 3675 | return "aapcs-vfp" ; |
| 3676 | case CC_AArch64VectorCall: |
| 3677 | return "aarch64_vector_pcs" ; |
| 3678 | case CC_AArch64SVEPCS: |
| 3679 | return "aarch64_sve_pcs" ; |
| 3680 | case CC_IntelOclBicc: |
| 3681 | return "intel_ocl_bicc" ; |
| 3682 | case CC_SpirFunction: |
| 3683 | return "spir_function" ; |
| 3684 | case CC_DeviceKernel: |
| 3685 | return "device_kernel" ; |
| 3686 | case CC_Swift: |
| 3687 | return "swiftcall" ; |
| 3688 | case CC_SwiftAsync: |
| 3689 | return "swiftasynccall" ; |
| 3690 | case CC_PreserveMost: |
| 3691 | return "preserve_most" ; |
| 3692 | case CC_PreserveAll: |
| 3693 | return "preserve_all" ; |
| 3694 | case CC_M68kRTD: |
| 3695 | return "m68k_rtd" ; |
| 3696 | case CC_PreserveNone: |
| 3697 | return "preserve_none" ; |
| 3698 | // clang-format off |
| 3699 | case CC_RISCVVectorCall: return "riscv_vector_cc" ; |
| 3700 | #define CC_VLS_CASE(ABI_VLEN) \ |
| 3701 | case CC_RISCVVLSCall_##ABI_VLEN: return "riscv_vls_cc(" #ABI_VLEN ")"; |
| 3702 | CC_VLS_CASE(32) |
| 3703 | CC_VLS_CASE(64) |
| 3704 | CC_VLS_CASE(128) |
| 3705 | CC_VLS_CASE(256) |
| 3706 | CC_VLS_CASE(512) |
| 3707 | CC_VLS_CASE(1024) |
| 3708 | CC_VLS_CASE(2048) |
| 3709 | CC_VLS_CASE(4096) |
| 3710 | CC_VLS_CASE(8192) |
| 3711 | CC_VLS_CASE(16384) |
| 3712 | CC_VLS_CASE(32768) |
| 3713 | CC_VLS_CASE(65536) |
| 3714 | #undef CC_VLS_CASE |
| 3715 | // clang-format on |
| 3716 | } |
| 3717 | |
| 3718 | llvm_unreachable("Invalid calling convention." ); |
| 3719 | } |
| 3720 | |
| 3721 | void FunctionProtoType::ExceptionSpecInfo::instantiate() { |
| 3722 | assert(Type == EST_Uninstantiated); |
| 3723 | NoexceptExpr = |
| 3724 | cast<FunctionProtoType>(Val: SourceTemplate->getType())->getNoexceptExpr(); |
| 3725 | Type = EST_DependentNoexcept; |
| 3726 | } |
| 3727 | |
| 3728 | FunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> params, |
| 3729 | QualType canonical, |
| 3730 | const ExtProtoInfo &epi) |
| 3731 | : FunctionType(FunctionProto, result, canonical, result->getDependence(), |
| 3732 | epi.ExtInfo) { |
| 3733 | FunctionTypeBits.FastTypeQuals = epi.TypeQuals.getFastQualifiers(); |
| 3734 | FunctionTypeBits.RefQualifier = epi.RefQualifier; |
| 3735 | FunctionTypeBits.NumParams = params.size(); |
| 3736 | assert(getNumParams() == params.size() && "NumParams overflow!" ); |
| 3737 | FunctionTypeBits.ExceptionSpecType = epi.ExceptionSpec.Type; |
| 3738 | FunctionTypeBits.HasExtParameterInfos = !!epi.ExtParameterInfos; |
| 3739 | FunctionTypeBits.Variadic = epi.Variadic; |
| 3740 | FunctionTypeBits.HasTrailingReturn = epi.HasTrailingReturn; |
| 3741 | FunctionTypeBits.CFIUncheckedCallee = epi.CFIUncheckedCallee; |
| 3742 | |
| 3743 | if (epi.requiresFunctionProtoTypeExtraBitfields()) { |
| 3744 | FunctionTypeBits.HasExtraBitfields = true; |
| 3745 | auto & = *getTrailingObjects<FunctionTypeExtraBitfields>(); |
| 3746 | ExtraBits = FunctionTypeExtraBitfields(); |
| 3747 | } else { |
| 3748 | FunctionTypeBits.HasExtraBitfields = false; |
| 3749 | } |
| 3750 | |
| 3751 | // Propagate any extra attribute information. |
| 3752 | if (epi.requiresFunctionProtoTypeExtraAttributeInfo()) { |
| 3753 | auto & = *getTrailingObjects<FunctionTypeExtraAttributeInfo>(); |
| 3754 | ExtraAttrInfo.CFISalt = epi.ExtraAttributeInfo.CFISalt; |
| 3755 | |
| 3756 | // Also set the bit in FunctionTypeExtraBitfields. |
| 3757 | auto & = *getTrailingObjects<FunctionTypeExtraBitfields>(); |
| 3758 | ExtraBits.HasExtraAttributeInfo = true; |
| 3759 | } |
| 3760 | |
| 3761 | if (epi.requiresFunctionProtoTypeArmAttributes()) { |
| 3762 | auto &ArmTypeAttrs = *getTrailingObjects<FunctionTypeArmAttributes>(); |
| 3763 | ArmTypeAttrs = FunctionTypeArmAttributes(); |
| 3764 | |
| 3765 | // Also set the bit in FunctionTypeExtraBitfields |
| 3766 | auto & = *getTrailingObjects<FunctionTypeExtraBitfields>(); |
| 3767 | ExtraBits.HasArmTypeAttributes = true; |
| 3768 | } |
| 3769 | |
| 3770 | // Fill in the trailing argument array. |
| 3771 | auto *argSlot = getTrailingObjects<QualType>(); |
| 3772 | for (unsigned i = 0; i != getNumParams(); ++i) { |
| 3773 | addDependence(D: params[i]->getDependence() & |
| 3774 | ~TypeDependence::VariablyModified); |
| 3775 | argSlot[i] = params[i]; |
| 3776 | } |
| 3777 | |
| 3778 | // Propagate the SME ACLE attributes. |
| 3779 | if (epi.AArch64SMEAttributes != SME_NormalFunction) { |
| 3780 | auto &ArmTypeAttrs = *getTrailingObjects<FunctionTypeArmAttributes>(); |
| 3781 | assert(epi.AArch64SMEAttributes <= SME_AttributeMask && |
| 3782 | "Not enough bits to encode SME attributes" ); |
| 3783 | ArmTypeAttrs.AArch64SMEAttributes = epi.AArch64SMEAttributes; |
| 3784 | } |
| 3785 | |
| 3786 | // Fill in the exception type array if present. |
| 3787 | if (getExceptionSpecType() == EST_Dynamic) { |
| 3788 | auto & = *getTrailingObjects<FunctionTypeExtraBitfields>(); |
| 3789 | size_t NumExceptions = epi.ExceptionSpec.Exceptions.size(); |
| 3790 | assert(NumExceptions <= 1023 && "Not enough bits to encode exceptions" ); |
| 3791 | ExtraBits.NumExceptionType = NumExceptions; |
| 3792 | |
| 3793 | assert(hasExtraBitfields() && "missing trailing extra bitfields!" ); |
| 3794 | auto *exnSlot = |
| 3795 | reinterpret_cast<QualType *>(getTrailingObjects<ExceptionType>()); |
| 3796 | unsigned I = 0; |
| 3797 | for (QualType ExceptionType : epi.ExceptionSpec.Exceptions) { |
| 3798 | // Note that, before C++17, a dependent exception specification does |
| 3799 | // *not* make a type dependent; it's not even part of the C++ type |
| 3800 | // system. |
| 3801 | addDependence( |
| 3802 | D: ExceptionType->getDependence() & |
| 3803 | (TypeDependence::Instantiation | TypeDependence::UnexpandedPack)); |
| 3804 | |
| 3805 | exnSlot[I++] = ExceptionType; |
| 3806 | } |
| 3807 | } |
| 3808 | // Fill in the Expr * in the exception specification if present. |
| 3809 | else if (isComputedNoexcept(ESpecType: getExceptionSpecType())) { |
| 3810 | assert(epi.ExceptionSpec.NoexceptExpr && "computed noexcept with no expr" ); |
| 3811 | assert((getExceptionSpecType() == EST_DependentNoexcept) == |
| 3812 | epi.ExceptionSpec.NoexceptExpr->isValueDependent()); |
| 3813 | |
| 3814 | // Store the noexcept expression and context. |
| 3815 | *getTrailingObjects<Expr *>() = epi.ExceptionSpec.NoexceptExpr; |
| 3816 | |
| 3817 | addDependence( |
| 3818 | D: toTypeDependence(D: epi.ExceptionSpec.NoexceptExpr->getDependence()) & |
| 3819 | (TypeDependence::Instantiation | TypeDependence::UnexpandedPack)); |
| 3820 | } |
| 3821 | // Fill in the FunctionDecl * in the exception specification if present. |
| 3822 | else if (getExceptionSpecType() == EST_Uninstantiated) { |
| 3823 | // Store the function decl from which we will resolve our |
| 3824 | // exception specification. |
| 3825 | auto **slot = getTrailingObjects<FunctionDecl *>(); |
| 3826 | slot[0] = epi.ExceptionSpec.SourceDecl; |
| 3827 | slot[1] = epi.ExceptionSpec.SourceTemplate; |
| 3828 | // This exception specification doesn't make the type dependent, because |
| 3829 | // it's not instantiated as part of instantiating the type. |
| 3830 | } else if (getExceptionSpecType() == EST_Unevaluated) { |
| 3831 | // Store the function decl from which we will resolve our |
| 3832 | // exception specification. |
| 3833 | auto **slot = getTrailingObjects<FunctionDecl *>(); |
| 3834 | slot[0] = epi.ExceptionSpec.SourceDecl; |
| 3835 | } |
| 3836 | |
| 3837 | // If this is a canonical type, and its exception specification is dependent, |
| 3838 | // then it's a dependent type. This only happens in C++17 onwards. |
| 3839 | if (isCanonicalUnqualified()) { |
| 3840 | if (getExceptionSpecType() == EST_Dynamic || |
| 3841 | getExceptionSpecType() == EST_DependentNoexcept) { |
| 3842 | assert(hasDependentExceptionSpec() && "type should not be canonical" ); |
| 3843 | addDependence(D: TypeDependence::DependentInstantiation); |
| 3844 | } |
| 3845 | } else if (getCanonicalTypeInternal()->isDependentType()) { |
| 3846 | // Ask our canonical type whether our exception specification was dependent. |
| 3847 | addDependence(D: TypeDependence::DependentInstantiation); |
| 3848 | } |
| 3849 | |
| 3850 | // Fill in the extra parameter info if present. |
| 3851 | if (epi.ExtParameterInfos) { |
| 3852 | auto *extParamInfos = getTrailingObjects<ExtParameterInfo>(); |
| 3853 | for (unsigned i = 0; i != getNumParams(); ++i) |
| 3854 | extParamInfos[i] = epi.ExtParameterInfos[i]; |
| 3855 | } |
| 3856 | |
| 3857 | if (epi.TypeQuals.hasNonFastQualifiers()) { |
| 3858 | FunctionTypeBits.HasExtQuals = 1; |
| 3859 | *getTrailingObjects<Qualifiers>() = epi.TypeQuals; |
| 3860 | } else { |
| 3861 | FunctionTypeBits.HasExtQuals = 0; |
| 3862 | } |
| 3863 | |
| 3864 | // Fill in the Ellipsis location info if present. |
| 3865 | if (epi.Variadic) { |
| 3866 | auto &EllipsisLoc = *getTrailingObjects<SourceLocation>(); |
| 3867 | EllipsisLoc = epi.EllipsisLoc; |
| 3868 | } |
| 3869 | |
| 3870 | if (!epi.FunctionEffects.empty()) { |
| 3871 | auto & = *getTrailingObjects<FunctionTypeExtraBitfields>(); |
| 3872 | size_t EffectsCount = epi.FunctionEffects.size(); |
| 3873 | ExtraBits.NumFunctionEffects = EffectsCount; |
| 3874 | assert(ExtraBits.NumFunctionEffects == EffectsCount && |
| 3875 | "effect bitfield overflow" ); |
| 3876 | |
| 3877 | ArrayRef<FunctionEffect> SrcFX = epi.FunctionEffects.effects(); |
| 3878 | auto *DestFX = getTrailingObjects<FunctionEffect>(); |
| 3879 | llvm::uninitialized_copy(Src&: SrcFX, Dst: DestFX); |
| 3880 | |
| 3881 | ArrayRef<EffectConditionExpr> SrcConds = epi.FunctionEffects.conditions(); |
| 3882 | if (!SrcConds.empty()) { |
| 3883 | ExtraBits.EffectsHaveConditions = true; |
| 3884 | auto *DestConds = getTrailingObjects<EffectConditionExpr>(); |
| 3885 | llvm::uninitialized_copy(Src&: SrcConds, Dst: DestConds); |
| 3886 | assert(llvm::any_of(SrcConds, |
| 3887 | [](const EffectConditionExpr &EC) { |
| 3888 | if (const Expr *E = EC.getCondition()) |
| 3889 | return E->isTypeDependent() || |
| 3890 | E->isValueDependent(); |
| 3891 | return false; |
| 3892 | }) && |
| 3893 | "expected a dependent expression among the conditions" ); |
| 3894 | addDependence(D: TypeDependence::DependentInstantiation); |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | bool FunctionProtoType::hasDependentExceptionSpec() const { |
| 3900 | if (Expr *NE = getNoexceptExpr()) |
| 3901 | return NE->isValueDependent(); |
| 3902 | for (QualType ET : exceptions()) |
| 3903 | // A pack expansion with a non-dependent pattern is still dependent, |
| 3904 | // because we don't know whether the pattern is in the exception spec |
| 3905 | // or not (that depends on whether the pack has 0 expansions). |
| 3906 | if (ET->isDependentType() || ET->getAs<PackExpansionType>()) |
| 3907 | return true; |
| 3908 | return false; |
| 3909 | } |
| 3910 | |
| 3911 | bool FunctionProtoType::hasInstantiationDependentExceptionSpec() const { |
| 3912 | if (Expr *NE = getNoexceptExpr()) |
| 3913 | return NE->isInstantiationDependent(); |
| 3914 | for (QualType ET : exceptions()) |
| 3915 | if (ET->isInstantiationDependentType()) |
| 3916 | return true; |
| 3917 | return false; |
| 3918 | } |
| 3919 | |
| 3920 | CanThrowResult FunctionProtoType::canThrow() const { |
| 3921 | switch (getExceptionSpecType()) { |
| 3922 | case EST_Unparsed: |
| 3923 | case EST_Unevaluated: |
| 3924 | llvm_unreachable("should not call this with unresolved exception specs" ); |
| 3925 | |
| 3926 | case EST_DynamicNone: |
| 3927 | case EST_BasicNoexcept: |
| 3928 | case EST_NoexceptTrue: |
| 3929 | case EST_NoThrow: |
| 3930 | return CT_Cannot; |
| 3931 | |
| 3932 | case EST_None: |
| 3933 | case EST_MSAny: |
| 3934 | case EST_NoexceptFalse: |
| 3935 | return CT_Can; |
| 3936 | |
| 3937 | case EST_Dynamic: |
| 3938 | // A dynamic exception specification is throwing unless every exception |
| 3939 | // type is an (unexpanded) pack expansion type. |
| 3940 | for (unsigned I = 0; I != getNumExceptions(); ++I) |
| 3941 | if (!getExceptionType(i: I)->getAs<PackExpansionType>()) |
| 3942 | return CT_Can; |
| 3943 | return CT_Dependent; |
| 3944 | |
| 3945 | case EST_Uninstantiated: |
| 3946 | case EST_DependentNoexcept: |
| 3947 | return CT_Dependent; |
| 3948 | } |
| 3949 | |
| 3950 | llvm_unreachable("unexpected exception specification kind" ); |
| 3951 | } |
| 3952 | |
| 3953 | bool FunctionProtoType::isTemplateVariadic() const { |
| 3954 | for (unsigned ArgIdx = getNumParams(); ArgIdx; --ArgIdx) |
| 3955 | if (isa<PackExpansionType>(Val: getParamType(i: ArgIdx - 1))) |
| 3956 | return true; |
| 3957 | |
| 3958 | return false; |
| 3959 | } |
| 3960 | |
| 3961 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
| 3962 | const QualType *ArgTys, unsigned NumParams, |
| 3963 | const ExtProtoInfo &epi, |
| 3964 | const ASTContext &Context, bool Canonical) { |
| 3965 | // We have to be careful not to get ambiguous profile encodings. |
| 3966 | // Note that valid type pointers are never ambiguous with anything else. |
| 3967 | // |
| 3968 | // The encoding grammar begins: |
| 3969 | // type type* bool int bool |
| 3970 | // If that final bool is true, then there is a section for the EH spec: |
| 3971 | // bool type* |
| 3972 | // This is followed by an optional "consumed argument" section of the |
| 3973 | // same length as the first type sequence: |
| 3974 | // bool* |
| 3975 | // This is followed by the ext info: |
| 3976 | // int |
| 3977 | // Finally we have a trailing return type flag (bool) |
| 3978 | // combined with AArch64 SME Attributes and extra attribute info, to save |
| 3979 | // space: |
| 3980 | // int |
| 3981 | // combined with any FunctionEffects |
| 3982 | // |
| 3983 | // There is no ambiguity between the consumed arguments and an empty EH |
| 3984 | // spec because of the leading 'bool' which unambiguously indicates |
| 3985 | // whether the following bool is the EH spec or part of the arguments. |
| 3986 | |
| 3987 | ID.AddPointer(Ptr: Result.getAsOpaquePtr()); |
| 3988 | for (unsigned i = 0; i != NumParams; ++i) |
| 3989 | ID.AddPointer(Ptr: ArgTys[i].getAsOpaquePtr()); |
| 3990 | // This method is relatively performance sensitive, so as a performance |
| 3991 | // shortcut, use one AddInteger call instead of four for the next four |
| 3992 | // fields. |
| 3993 | assert(!(unsigned(epi.Variadic) & ~1) && !(unsigned(epi.RefQualifier) & ~3) && |
| 3994 | !(unsigned(epi.ExceptionSpec.Type) & ~15) && |
| 3995 | "Values larger than expected." ); |
| 3996 | ID.AddInteger(I: unsigned(epi.Variadic) + (epi.RefQualifier << 1) + |
| 3997 | (epi.ExceptionSpec.Type << 3)); |
| 3998 | ID.Add(x: epi.TypeQuals); |
| 3999 | if (epi.ExceptionSpec.Type == EST_Dynamic) { |
| 4000 | for (QualType Ex : epi.ExceptionSpec.Exceptions) |
| 4001 | ID.AddPointer(Ptr: Ex.getAsOpaquePtr()); |
| 4002 | } else if (isComputedNoexcept(ESpecType: epi.ExceptionSpec.Type)) { |
| 4003 | epi.ExceptionSpec.NoexceptExpr->Profile(ID, Context, Canonical); |
| 4004 | } else if (epi.ExceptionSpec.Type == EST_Uninstantiated || |
| 4005 | epi.ExceptionSpec.Type == EST_Unevaluated) { |
| 4006 | ID.AddPointer(Ptr: epi.ExceptionSpec.SourceDecl->getCanonicalDecl()); |
| 4007 | } |
| 4008 | if (epi.ExtParameterInfos) { |
| 4009 | for (unsigned i = 0; i != NumParams; ++i) |
| 4010 | ID.AddInteger(I: epi.ExtParameterInfos[i].getOpaqueValue()); |
| 4011 | } |
| 4012 | |
| 4013 | epi.ExtInfo.Profile(ID); |
| 4014 | epi.ExtraAttributeInfo.Profile(ID); |
| 4015 | |
| 4016 | unsigned EffectCount = epi.FunctionEffects.size(); |
| 4017 | bool HasConds = !epi.FunctionEffects.Conditions.empty(); |
| 4018 | |
| 4019 | ID.AddInteger(I: (EffectCount << 3) | (HasConds << 2) | |
| 4020 | (epi.AArch64SMEAttributes << 1) | epi.HasTrailingReturn); |
| 4021 | ID.AddInteger(I: epi.CFIUncheckedCallee); |
| 4022 | |
| 4023 | for (unsigned Idx = 0; Idx != EffectCount; ++Idx) { |
| 4024 | ID.AddInteger(I: epi.FunctionEffects.Effects[Idx].toOpaqueInt32()); |
| 4025 | if (HasConds) |
| 4026 | ID.AddPointer(Ptr: epi.FunctionEffects.Conditions[Idx].getCondition()); |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, |
| 4031 | const ASTContext &Ctx) { |
| 4032 | Profile(ID, Result: getReturnType(), ArgTys: param_type_begin(), NumParams: getNumParams(), |
| 4033 | epi: getExtProtoInfo(), Context: Ctx, Canonical: isCanonicalUnqualified()); |
| 4034 | } |
| 4035 | |
| 4036 | TypeCoupledDeclRefInfo::TypeCoupledDeclRefInfo(ValueDecl *D, bool Deref) |
| 4037 | : Data(D, Deref << DerefShift) {} |
| 4038 | |
| 4039 | bool TypeCoupledDeclRefInfo::isDeref() const { |
| 4040 | return Data.getInt() & DerefMask; |
| 4041 | } |
| 4042 | ValueDecl *TypeCoupledDeclRefInfo::getDecl() const { return Data.getPointer(); } |
| 4043 | unsigned TypeCoupledDeclRefInfo::getInt() const { return Data.getInt(); } |
| 4044 | void *TypeCoupledDeclRefInfo::getOpaqueValue() const { |
| 4045 | return Data.getOpaqueValue(); |
| 4046 | } |
| 4047 | bool TypeCoupledDeclRefInfo::operator==( |
| 4048 | const TypeCoupledDeclRefInfo &Other) const { |
| 4049 | return getOpaqueValue() == Other.getOpaqueValue(); |
| 4050 | } |
| 4051 | void TypeCoupledDeclRefInfo::setFromOpaqueValue(void *V) { |
| 4052 | Data.setFromOpaqueValue(V); |
| 4053 | } |
| 4054 | |
| 4055 | OverflowBehaviorType::OverflowBehaviorType( |
| 4056 | QualType Canon, QualType Underlying, |
| 4057 | OverflowBehaviorType::OverflowBehaviorKind Kind) |
| 4058 | : Type(OverflowBehavior, Canon, Underlying->getDependence()), |
| 4059 | UnderlyingType(Underlying), BehaviorKind(Kind) {} |
| 4060 | |
| 4061 | BoundsAttributedType::BoundsAttributedType(TypeClass TC, QualType Wrapped, |
| 4062 | QualType Canon) |
| 4063 | : Type(TC, Canon, Wrapped->getDependence()), WrappedTy(Wrapped) {} |
| 4064 | |
| 4065 | CountAttributedType::CountAttributedType( |
| 4066 | QualType Wrapped, QualType Canon, Expr *CountExpr, bool CountInBytes, |
| 4067 | bool OrNull, ArrayRef<TypeCoupledDeclRefInfo> CoupledDecls) |
| 4068 | : BoundsAttributedType(CountAttributed, Wrapped, Canon), |
| 4069 | CountExpr(CountExpr) { |
| 4070 | CountAttributedTypeBits.NumCoupledDecls = CoupledDecls.size(); |
| 4071 | CountAttributedTypeBits.CountInBytes = CountInBytes; |
| 4072 | CountAttributedTypeBits.OrNull = OrNull; |
| 4073 | auto *DeclSlot = getTrailingObjects(); |
| 4074 | llvm::copy(Range&: CoupledDecls, Out: DeclSlot); |
| 4075 | Decls = llvm::ArrayRef(DeclSlot, CoupledDecls.size()); |
| 4076 | } |
| 4077 | |
| 4078 | StringRef CountAttributedType::getAttributeName(bool WithMacroPrefix) const { |
| 4079 | // TODO: This method isn't really ideal because it doesn't return the spelling |
| 4080 | // of the attribute that was used in the user's code. This method is used for |
| 4081 | // diagnostics so the fact it doesn't use the spelling of the attribute in |
| 4082 | // the user's code could be confusing (#113585). |
| 4083 | #define ENUMERATE_ATTRS(PREFIX) \ |
| 4084 | do { \ |
| 4085 | if (isCountInBytes()) { \ |
| 4086 | if (isOrNull()) \ |
| 4087 | return PREFIX "sized_by_or_null"; \ |
| 4088 | return PREFIX "sized_by"; \ |
| 4089 | } \ |
| 4090 | if (isOrNull()) \ |
| 4091 | return PREFIX "counted_by_or_null"; \ |
| 4092 | return PREFIX "counted_by"; \ |
| 4093 | } while (0) |
| 4094 | |
| 4095 | if (WithMacroPrefix) |
| 4096 | ENUMERATE_ATTRS("__" ); |
| 4097 | else |
| 4098 | ENUMERATE_ATTRS("" ); |
| 4099 | |
| 4100 | #undef ENUMERATE_ATTRS |
| 4101 | } |
| 4102 | |
| 4103 | TypedefType::TypedefType(TypeClass TC, ElaboratedTypeKeyword Keyword, |
| 4104 | NestedNameSpecifier Qualifier, |
| 4105 | const TypedefNameDecl *D, QualType UnderlyingType, |
| 4106 | bool HasTypeDifferentFromDecl) |
| 4107 | : TypeWithKeyword( |
| 4108 | Keyword, TC, UnderlyingType.getCanonicalType(), |
| 4109 | toSemanticDependence(D: UnderlyingType->getDependence()) | |
| 4110 | (Qualifier |
| 4111 | ? toTypeDependence(D: Qualifier.getDependence() & |
| 4112 | ~NestedNameSpecifierDependence::Dependent) |
| 4113 | : TypeDependence{})), |
| 4114 | Decl(const_cast<TypedefNameDecl *>(D)) { |
| 4115 | if ((TypedefBits.hasQualifier = !!Qualifier)) |
| 4116 | *getTrailingObjects<NestedNameSpecifier>() = Qualifier; |
| 4117 | if ((TypedefBits.hasTypeDifferentFromDecl = HasTypeDifferentFromDecl)) |
| 4118 | *getTrailingObjects<QualType>() = UnderlyingType; |
| 4119 | } |
| 4120 | |
| 4121 | QualType TypedefType::desugar() const { |
| 4122 | return typeMatchesDecl() ? Decl->getUnderlyingType() |
| 4123 | : *getTrailingObjects<QualType>(); |
| 4124 | } |
| 4125 | |
| 4126 | UnresolvedUsingType::UnresolvedUsingType(ElaboratedTypeKeyword Keyword, |
| 4127 | NestedNameSpecifier Qualifier, |
| 4128 | const UnresolvedUsingTypenameDecl *D, |
| 4129 | const Type *CanonicalType) |
| 4130 | : TypeWithKeyword( |
| 4131 | Keyword, UnresolvedUsing, QualType(CanonicalType, 0), |
| 4132 | TypeDependence::DependentInstantiation | |
| 4133 | (Qualifier |
| 4134 | ? toTypeDependence(D: Qualifier.getDependence() & |
| 4135 | ~NestedNameSpecifierDependence::Dependent) |
| 4136 | : TypeDependence{})), |
| 4137 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) { |
| 4138 | if ((UnresolvedUsingBits.hasQualifier = !!Qualifier)) |
| 4139 | *getTrailingObjects<NestedNameSpecifier>() = Qualifier; |
| 4140 | } |
| 4141 | |
| 4142 | UsingType::UsingType(ElaboratedTypeKeyword Keyword, |
| 4143 | NestedNameSpecifier Qualifier, const UsingShadowDecl *D, |
| 4144 | QualType UnderlyingType) |
| 4145 | : TypeWithKeyword(Keyword, Using, UnderlyingType.getCanonicalType(), |
| 4146 | toSemanticDependence(D: UnderlyingType->getDependence())), |
| 4147 | D(const_cast<UsingShadowDecl *>(D)), UnderlyingType(UnderlyingType) { |
| 4148 | if ((UsingBits.hasQualifier = !!Qualifier)) |
| 4149 | *getTrailingObjects() = Qualifier; |
| 4150 | } |
| 4151 | |
| 4152 | QualType MacroQualifiedType::desugar() const { return getUnderlyingType(); } |
| 4153 | |
| 4154 | QualType MacroQualifiedType::getModifiedType() const { |
| 4155 | // Step over MacroQualifiedTypes from the same macro to find the type |
| 4156 | // ultimately qualified by the macro qualifier. |
| 4157 | QualType Inner = cast<AttributedType>(Val: getUnderlyingType())->getModifiedType(); |
| 4158 | while (auto *InnerMQT = dyn_cast<MacroQualifiedType>(Val&: Inner)) { |
| 4159 | if (InnerMQT->getMacroIdentifier() != getMacroIdentifier()) |
| 4160 | break; |
| 4161 | Inner = InnerMQT->getModifiedType(); |
| 4162 | } |
| 4163 | return Inner; |
| 4164 | } |
| 4165 | |
| 4166 | TypeOfExprType::TypeOfExprType(const ASTContext &Context, Expr *E, |
| 4167 | TypeOfKind Kind, QualType Can) |
| 4168 | : Type(TypeOfExpr, |
| 4169 | // We have to protect against 'Can' being invalid through its |
| 4170 | // default argument. |
| 4171 | Kind == TypeOfKind::Unqualified && !Can.isNull() |
| 4172 | ? Context.getUnqualifiedArrayType(T: Can).getAtomicUnqualifiedType() |
| 4173 | : Can, |
| 4174 | toTypeDependence(D: E->getDependence()) | |
| 4175 | (E->getType()->getDependence() & |
| 4176 | TypeDependence::VariablyModified)), |
| 4177 | TOExpr(E), Context(Context) { |
| 4178 | TypeOfBits.Kind = static_cast<unsigned>(Kind); |
| 4179 | } |
| 4180 | |
| 4181 | bool TypeOfExprType::isSugared() const { return !TOExpr->isTypeDependent(); } |
| 4182 | |
| 4183 | QualType TypeOfExprType::desugar() const { |
| 4184 | if (isSugared()) { |
| 4185 | QualType QT = getUnderlyingExpr()->getType(); |
| 4186 | return getKind() == TypeOfKind::Unqualified |
| 4187 | ? Context.getUnqualifiedArrayType(T: QT).getAtomicUnqualifiedType() |
| 4188 | : QT; |
| 4189 | } |
| 4190 | return QualType(this, 0); |
| 4191 | } |
| 4192 | |
| 4193 | void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, |
| 4194 | const ASTContext &Context, Expr *E, |
| 4195 | bool IsUnqual) { |
| 4196 | E->Profile(ID, Context, Canonical: true); |
| 4197 | ID.AddBoolean(B: IsUnqual); |
| 4198 | } |
| 4199 | |
| 4200 | TypeOfType::TypeOfType(const ASTContext &Context, QualType T, QualType Can, |
| 4201 | TypeOfKind Kind) |
| 4202 | : Type(TypeOf, |
| 4203 | Kind == TypeOfKind::Unqualified |
| 4204 | ? Context.getUnqualifiedArrayType(T: Can).getAtomicUnqualifiedType() |
| 4205 | : Can, |
| 4206 | T->getDependence()), |
| 4207 | TOType(T), Context(Context) { |
| 4208 | TypeOfBits.Kind = static_cast<unsigned>(Kind); |
| 4209 | } |
| 4210 | |
| 4211 | QualType TypeOfType::desugar() const { |
| 4212 | QualType QT = getUnmodifiedType(); |
| 4213 | return getKind() == TypeOfKind::Unqualified |
| 4214 | ? Context.getUnqualifiedArrayType(T: QT).getAtomicUnqualifiedType() |
| 4215 | : QT; |
| 4216 | } |
| 4217 | |
| 4218 | DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) |
| 4219 | // C++11 [temp.type]p2: "If an expression e involves a template parameter, |
| 4220 | // decltype(e) denotes a unique dependent type." Hence a decltype type is |
| 4221 | // type-dependent even if its expression is only instantiation-dependent. |
| 4222 | : Type(Decltype, can, |
| 4223 | toTypeDependence(D: E->getDependence()) | |
| 4224 | (E->isInstantiationDependent() ? TypeDependence::Dependent |
| 4225 | : TypeDependence::None) | |
| 4226 | (E->getType()->getDependence() & |
| 4227 | TypeDependence::VariablyModified)), |
| 4228 | E(E), UnderlyingType(underlyingType) {} |
| 4229 | |
| 4230 | bool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); } |
| 4231 | |
| 4232 | QualType DecltypeType::desugar() const { |
| 4233 | if (isSugared()) |
| 4234 | return getUnderlyingType(); |
| 4235 | |
| 4236 | return QualType(this, 0); |
| 4237 | } |
| 4238 | |
| 4239 | DependentDecltypeType::DependentDecltypeType(Expr *E) |
| 4240 | : DecltypeType(E, QualType()) {} |
| 4241 | |
| 4242 | void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, |
| 4243 | const ASTContext &Context, Expr *E) { |
| 4244 | E->Profile(ID, Context, Canonical: true); |
| 4245 | } |
| 4246 | |
| 4247 | PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern, |
| 4248 | Expr *IndexExpr, bool FullySubstituted, |
| 4249 | ArrayRef<QualType> Expansions) |
| 4250 | : Type(PackIndexing, Canonical, |
| 4251 | computeDependence(Pattern, IndexExpr, Expansions)), |
| 4252 | Pattern(Pattern), IndexExpr(IndexExpr), Size(Expansions.size()), |
| 4253 | FullySubstituted(FullySubstituted) { |
| 4254 | |
| 4255 | llvm::uninitialized_copy(Src&: Expansions, Dst: getTrailingObjects()); |
| 4256 | } |
| 4257 | |
| 4258 | UnsignedOrNone PackIndexingType::getSelectedIndex() const { |
| 4259 | if (isInstantiationDependentType()) |
| 4260 | return std::nullopt; |
| 4261 | // Should only be not a constant for error recovery. |
| 4262 | ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: getIndexExpr()); |
| 4263 | if (!CE) |
| 4264 | return std::nullopt; |
| 4265 | auto Index = CE->getResultAsAPSInt(); |
| 4266 | assert(Index.isNonNegative() && "Invalid index" ); |
| 4267 | return static_cast<unsigned>(Index.getExtValue()); |
| 4268 | } |
| 4269 | |
| 4270 | TypeDependence |
| 4271 | PackIndexingType::computeDependence(QualType Pattern, Expr *IndexExpr, |
| 4272 | ArrayRef<QualType> Expansions) { |
| 4273 | TypeDependence IndexD = toTypeDependence(D: IndexExpr->getDependence()); |
| 4274 | |
| 4275 | TypeDependence TD = IndexD | (IndexExpr->isInstantiationDependent() |
| 4276 | ? TypeDependence::DependentInstantiation |
| 4277 | : TypeDependence::None); |
| 4278 | if (Expansions.empty()) |
| 4279 | TD |= Pattern->getDependence() & TypeDependence::DependentInstantiation; |
| 4280 | else |
| 4281 | for (const QualType &T : Expansions) |
| 4282 | TD |= T->getDependence(); |
| 4283 | |
| 4284 | if (!(IndexD & TypeDependence::UnexpandedPack)) |
| 4285 | TD &= ~TypeDependence::UnexpandedPack; |
| 4286 | |
| 4287 | // If the pattern does not contain an unexpended pack, |
| 4288 | // the type is still dependent, and invalid |
| 4289 | if (!Pattern->containsUnexpandedParameterPack()) |
| 4290 | TD |= TypeDependence::Error | TypeDependence::DependentInstantiation; |
| 4291 | |
| 4292 | return TD; |
| 4293 | } |
| 4294 | |
| 4295 | void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID, |
| 4296 | const ASTContext &Context) { |
| 4297 | Profile(ID, Context, Pattern: getPattern(), E: getIndexExpr(), FullySubstituted: isFullySubstituted(), |
| 4298 | Expansions: getExpansions()); |
| 4299 | } |
| 4300 | |
| 4301 | void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID, |
| 4302 | const ASTContext &Context, QualType Pattern, |
| 4303 | Expr *E, bool FullySubstituted, |
| 4304 | ArrayRef<QualType> Expansions) { |
| 4305 | |
| 4306 | E->Profile(ID, Context, Canonical: true); |
| 4307 | ID.AddBoolean(B: FullySubstituted); |
| 4308 | if (!Expansions.empty()) { |
| 4309 | ID.AddInteger(I: Expansions.size()); |
| 4310 | for (QualType T : Expansions) |
| 4311 | T.getCanonicalType().Profile(ID); |
| 4312 | } else { |
| 4313 | Pattern.Profile(ID); |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | UnaryTransformType::UnaryTransformType(QualType BaseType, |
| 4318 | QualType UnderlyingType, UTTKind UKind, |
| 4319 | QualType CanonicalType) |
| 4320 | : Type(UnaryTransform, CanonicalType, BaseType->getDependence()), |
| 4321 | BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind) {} |
| 4322 | |
| 4323 | TagType::TagType(TypeClass TC, ElaboratedTypeKeyword Keyword, |
| 4324 | NestedNameSpecifier Qualifier, const TagDecl *Tag, |
| 4325 | bool OwnsTag, bool ISInjected, const Type *CanonicalType) |
| 4326 | : TypeWithKeyword( |
| 4327 | Keyword, TC, QualType(CanonicalType, 0), |
| 4328 | (Tag->isDependentType() ? TypeDependence::DependentInstantiation |
| 4329 | : TypeDependence::None) | |
| 4330 | (Qualifier |
| 4331 | ? toTypeDependence(D: Qualifier.getDependence() & |
| 4332 | ~NestedNameSpecifierDependence::Dependent) |
| 4333 | : TypeDependence{})), |
| 4334 | decl(const_cast<TagDecl *>(Tag)) { |
| 4335 | if ((TagTypeBits.HasQualifier = !!Qualifier)) |
| 4336 | getTrailingQualifier() = Qualifier; |
| 4337 | TagTypeBits.OwnsTag = !!OwnsTag; |
| 4338 | TagTypeBits.IsInjected = ISInjected; |
| 4339 | } |
| 4340 | |
| 4341 | void *TagType::getTrailingPointer() const { |
| 4342 | switch (getTypeClass()) { |
| 4343 | case Type::Enum: |
| 4344 | return const_cast<EnumType *>(cast<EnumType>(Val: this) + 1); |
| 4345 | case Type::Record: |
| 4346 | return const_cast<RecordType *>(cast<RecordType>(Val: this) + 1); |
| 4347 | case Type::InjectedClassName: |
| 4348 | return const_cast<InjectedClassNameType *>( |
| 4349 | cast<InjectedClassNameType>(Val: this) + 1); |
| 4350 | default: |
| 4351 | llvm_unreachable("unexpected type class" ); |
| 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | NestedNameSpecifier &TagType::getTrailingQualifier() const { |
| 4356 | assert(TagTypeBits.HasQualifier); |
| 4357 | return *reinterpret_cast<NestedNameSpecifier *>(llvm::alignAddr( |
| 4358 | Addr: getTrailingPointer(), Alignment: llvm::Align::Of<NestedNameSpecifier *>())); |
| 4359 | } |
| 4360 | |
| 4361 | NestedNameSpecifier TagType::getQualifier() const { |
| 4362 | return TagTypeBits.HasQualifier ? getTrailingQualifier() : std::nullopt; |
| 4363 | } |
| 4364 | |
| 4365 | ClassTemplateDecl *TagType::getTemplateDecl() const { |
| 4366 | auto *Decl = dyn_cast<CXXRecordDecl>(Val: decl); |
| 4367 | if (!Decl) |
| 4368 | return nullptr; |
| 4369 | if (auto *RD = dyn_cast<ClassTemplateSpecializationDecl>(Val: Decl)) |
| 4370 | return RD->getSpecializedTemplate(); |
| 4371 | return Decl->getDescribedClassTemplate(); |
| 4372 | } |
| 4373 | |
| 4374 | TemplateName TagType::getTemplateName(const ASTContext &Ctx) const { |
| 4375 | auto *TD = getTemplateDecl(); |
| 4376 | if (!TD) |
| 4377 | return TemplateName(); |
| 4378 | if (isCanonicalUnqualified()) |
| 4379 | return TemplateName(TD); |
| 4380 | return Ctx.getQualifiedTemplateName(Qualifier: getQualifier(), /*TemplateKeyword=*/false, |
| 4381 | Template: TemplateName(TD)); |
| 4382 | } |
| 4383 | |
| 4384 | ArrayRef<TemplateArgument> |
| 4385 | TagType::getTemplateArgs(const ASTContext &Ctx) const { |
| 4386 | auto *Decl = dyn_cast<CXXRecordDecl>(Val: decl); |
| 4387 | if (!Decl) |
| 4388 | return {}; |
| 4389 | |
| 4390 | if (auto *RD = dyn_cast<ClassTemplateSpecializationDecl>(Val: Decl)) |
| 4391 | return RD->getTemplateArgs().asArray(); |
| 4392 | if (ClassTemplateDecl *TD = Decl->getDescribedClassTemplate()) |
| 4393 | return TD->getTemplateParameters()->getInjectedTemplateArgs(Context: Ctx); |
| 4394 | return {}; |
| 4395 | } |
| 4396 | |
| 4397 | bool RecordType::hasConstFields() const { |
| 4398 | std::vector<const RecordType *> RecordTypeList; |
| 4399 | RecordTypeList.push_back(x: this); |
| 4400 | unsigned NextToCheckIndex = 0; |
| 4401 | |
| 4402 | while (RecordTypeList.size() > NextToCheckIndex) { |
| 4403 | for (FieldDecl *FD : RecordTypeList[NextToCheckIndex] |
| 4404 | ->getDecl() |
| 4405 | ->getDefinitionOrSelf() |
| 4406 | ->fields()) { |
| 4407 | QualType FieldTy = FD->getType(); |
| 4408 | if (FieldTy.isConstQualified()) |
| 4409 | return true; |
| 4410 | FieldTy = FieldTy.getCanonicalType(); |
| 4411 | if (const auto *FieldRecTy = FieldTy->getAsCanonical<RecordType>()) { |
| 4412 | if (!llvm::is_contained(Range&: RecordTypeList, Element: FieldRecTy)) |
| 4413 | RecordTypeList.push_back(x: FieldRecTy); |
| 4414 | } |
| 4415 | } |
| 4416 | ++NextToCheckIndex; |
| 4417 | } |
| 4418 | return false; |
| 4419 | } |
| 4420 | |
| 4421 | InjectedClassNameType::InjectedClassNameType(ElaboratedTypeKeyword Keyword, |
| 4422 | NestedNameSpecifier Qualifier, |
| 4423 | const TagDecl *TD, bool IsInjected, |
| 4424 | const Type *CanonicalType) |
| 4425 | : TagType(TypeClass::InjectedClassName, Keyword, Qualifier, TD, |
| 4426 | /*OwnsTag=*/false, IsInjected, CanonicalType) {} |
| 4427 | |
| 4428 | AttributedType::AttributedType(QualType canon, const Attr *attr, |
| 4429 | QualType modified, QualType equivalent) |
| 4430 | : AttributedType(canon, attr->getKind(), attr, modified, equivalent) {} |
| 4431 | |
| 4432 | AttributedType::AttributedType(QualType canon, attr::Kind attrKind, |
| 4433 | const Attr *attr, QualType modified, |
| 4434 | QualType equivalent) |
| 4435 | : Type(Attributed, canon, equivalent->getDependence()), Attribute(attr), |
| 4436 | ModifiedType(modified), EquivalentType(equivalent) { |
| 4437 | AttributedTypeBits.AttrKind = attrKind; |
| 4438 | assert(!attr || attr->getKind() == attrKind); |
| 4439 | } |
| 4440 | |
| 4441 | bool AttributedType::isQualifier() const { |
| 4442 | // FIXME: Generate this with TableGen. |
| 4443 | switch (getAttrKind()) { |
| 4444 | // These are type qualifiers in the traditional C sense: they annotate |
| 4445 | // something about a specific value/variable of a type. (They aren't |
| 4446 | // always part of the canonical type, though.) |
| 4447 | case attr::ObjCGC: |
| 4448 | case attr::ObjCOwnership: |
| 4449 | case attr::ObjCInertUnsafeUnretained: |
| 4450 | case attr::TypeNonNull: |
| 4451 | case attr::TypeNullable: |
| 4452 | case attr::TypeNullableResult: |
| 4453 | case attr::TypeNullUnspecified: |
| 4454 | case attr::LifetimeBound: |
| 4455 | case attr::AddressSpace: |
| 4456 | return true; |
| 4457 | |
| 4458 | // All other type attributes aren't qualifiers; they rewrite the modified |
| 4459 | // type to be a semantically different type. |
| 4460 | default: |
| 4461 | return false; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | bool AttributedType::isMSTypeSpec() const { |
| 4466 | // FIXME: Generate this with TableGen? |
| 4467 | switch (getAttrKind()) { |
| 4468 | default: |
| 4469 | return false; |
| 4470 | case attr::Ptr32: |
| 4471 | case attr::Ptr64: |
| 4472 | case attr::SPtr: |
| 4473 | case attr::UPtr: |
| 4474 | return true; |
| 4475 | } |
| 4476 | llvm_unreachable("invalid attr kind" ); |
| 4477 | } |
| 4478 | |
| 4479 | bool AttributedType::isWebAssemblyFuncrefSpec() const { |
| 4480 | return getAttrKind() == attr::WebAssemblyFuncref; |
| 4481 | } |
| 4482 | |
| 4483 | bool AttributedType::isCallingConv() const { |
| 4484 | // FIXME: Generate this with TableGen. |
| 4485 | switch (getAttrKind()) { |
| 4486 | default: |
| 4487 | return false; |
| 4488 | case attr::Pcs: |
| 4489 | case attr::CDecl: |
| 4490 | case attr::FastCall: |
| 4491 | case attr::StdCall: |
| 4492 | case attr::ThisCall: |
| 4493 | case attr::RegCall: |
| 4494 | case attr::SwiftCall: |
| 4495 | case attr::SwiftAsyncCall: |
| 4496 | case attr::VectorCall: |
| 4497 | case attr::AArch64VectorPcs: |
| 4498 | case attr::AArch64SVEPcs: |
| 4499 | case attr::DeviceKernel: |
| 4500 | case attr::Pascal: |
| 4501 | case attr::MSABI: |
| 4502 | case attr::SysVABI: |
| 4503 | case attr::IntelOclBicc: |
| 4504 | case attr::PreserveMost: |
| 4505 | case attr::PreserveAll: |
| 4506 | case attr::M68kRTD: |
| 4507 | case attr::PreserveNone: |
| 4508 | case attr::RISCVVectorCC: |
| 4509 | case attr::RISCVVLSCC: |
| 4510 | return true; |
| 4511 | } |
| 4512 | llvm_unreachable("invalid attr kind" ); |
| 4513 | } |
| 4514 | |
| 4515 | IdentifierInfo *TemplateTypeParmType::getIdentifier() const { |
| 4516 | return isCanonicalUnqualified() ? nullptr : getDecl()->getIdentifier(); |
| 4517 | } |
| 4518 | |
| 4519 | SubstTemplateTypeParmType::SubstTemplateTypeParmType(QualType Replacement, |
| 4520 | Decl *AssociatedDecl, |
| 4521 | unsigned Index, |
| 4522 | UnsignedOrNone PackIndex, |
| 4523 | bool Final) |
| 4524 | : Type(SubstTemplateTypeParm, Replacement.getCanonicalType(), |
| 4525 | Replacement->getDependence()), |
| 4526 | AssociatedDecl(AssociatedDecl) { |
| 4527 | SubstTemplateTypeParmTypeBits.HasNonCanonicalUnderlyingType = |
| 4528 | Replacement != getCanonicalTypeInternal(); |
| 4529 | if (SubstTemplateTypeParmTypeBits.HasNonCanonicalUnderlyingType) |
| 4530 | *getTrailingObjects() = Replacement; |
| 4531 | |
| 4532 | SubstTemplateTypeParmTypeBits.Index = Index; |
| 4533 | SubstTemplateTypeParmTypeBits.Final = Final; |
| 4534 | SubstTemplateTypeParmTypeBits.PackIndex = |
| 4535 | PackIndex.toInternalRepresentation(); |
| 4536 | assert(AssociatedDecl != nullptr); |
| 4537 | } |
| 4538 | |
| 4539 | const TemplateTypeParmDecl * |
| 4540 | SubstTemplateTypeParmType::getReplacedParameter() const { |
| 4541 | return cast<TemplateTypeParmDecl>(Val: std::get<0>( |
| 4542 | t: getReplacedTemplateParameter(D: getAssociatedDecl(), Index: getIndex()))); |
| 4543 | } |
| 4544 | |
| 4545 | void SubstTemplateTypeParmType::Profile(llvm::FoldingSetNodeID &ID, |
| 4546 | QualType Replacement, |
| 4547 | const Decl *AssociatedDecl, |
| 4548 | unsigned Index, |
| 4549 | UnsignedOrNone PackIndex, bool Final) { |
| 4550 | Replacement.Profile(ID); |
| 4551 | ID.AddPointer(Ptr: AssociatedDecl); |
| 4552 | ID.AddInteger(I: Index); |
| 4553 | ID.AddInteger(I: PackIndex.toInternalRepresentation()); |
| 4554 | ID.AddBoolean(B: Final); |
| 4555 | } |
| 4556 | |
| 4557 | SubstPackType::SubstPackType(TypeClass Derived, QualType Canon, |
| 4558 | const TemplateArgument &ArgPack) |
| 4559 | : Type(Derived, Canon, |
| 4560 | TypeDependence::DependentInstantiation | |
| 4561 | TypeDependence::UnexpandedPack), |
| 4562 | Arguments(ArgPack.pack_begin()) { |
| 4563 | assert(llvm::all_of( |
| 4564 | ArgPack.pack_elements(), |
| 4565 | [](auto &P) { return P.getKind() == TemplateArgument::Type; }) && |
| 4566 | "non-type argument to SubstPackType?" ); |
| 4567 | SubstPackTypeBits.NumArgs = ArgPack.pack_size(); |
| 4568 | } |
| 4569 | |
| 4570 | TemplateArgument SubstPackType::getArgumentPack() const { |
| 4571 | return TemplateArgument(llvm::ArrayRef(Arguments, getNumArgs())); |
| 4572 | } |
| 4573 | |
| 4574 | void SubstPackType::Profile(llvm::FoldingSetNodeID &ID) { |
| 4575 | Profile(ID, ArgPack: getArgumentPack()); |
| 4576 | } |
| 4577 | |
| 4578 | void SubstPackType::Profile(llvm::FoldingSetNodeID &ID, |
| 4579 | const TemplateArgument &ArgPack) { |
| 4580 | ID.AddInteger(I: ArgPack.pack_size()); |
| 4581 | for (const auto &P : ArgPack.pack_elements()) |
| 4582 | ID.AddPointer(Ptr: P.getAsType().getAsOpaquePtr()); |
| 4583 | } |
| 4584 | |
| 4585 | SubstTemplateTypeParmPackType::SubstTemplateTypeParmPackType( |
| 4586 | QualType Canon, Decl *AssociatedDecl, unsigned Index, bool Final, |
| 4587 | const TemplateArgument &ArgPack) |
| 4588 | : SubstPackType(SubstTemplateTypeParmPack, Canon, ArgPack), |
| 4589 | AssociatedDeclAndFinal(AssociatedDecl, Final) { |
| 4590 | assert(AssociatedDecl != nullptr); |
| 4591 | |
| 4592 | SubstPackTypeBits.SubstTemplTypeParmPackIndex = Index; |
| 4593 | assert(getNumArgs() == ArgPack.pack_size() && |
| 4594 | "Parent bitfields in SubstPackType were overwritten." |
| 4595 | "Check NumSubstPackTypeBits." ); |
| 4596 | } |
| 4597 | |
| 4598 | Decl *SubstTemplateTypeParmPackType::getAssociatedDecl() const { |
| 4599 | return AssociatedDeclAndFinal.getPointer(); |
| 4600 | } |
| 4601 | |
| 4602 | bool SubstTemplateTypeParmPackType::getFinal() const { |
| 4603 | return AssociatedDeclAndFinal.getInt(); |
| 4604 | } |
| 4605 | |
| 4606 | const TemplateTypeParmDecl * |
| 4607 | SubstTemplateTypeParmPackType::getReplacedParameter() const { |
| 4608 | return cast<TemplateTypeParmDecl>(Val: std::get<0>( |
| 4609 | t: getReplacedTemplateParameter(D: getAssociatedDecl(), Index: getIndex()))); |
| 4610 | } |
| 4611 | |
| 4612 | IdentifierInfo *SubstTemplateTypeParmPackType::getIdentifier() const { |
| 4613 | return getReplacedParameter()->getIdentifier(); |
| 4614 | } |
| 4615 | |
| 4616 | void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) { |
| 4617 | Profile(ID, AssociatedDecl: getAssociatedDecl(), Index: getIndex(), Final: getFinal(), ArgPack: getArgumentPack()); |
| 4618 | } |
| 4619 | |
| 4620 | void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID, |
| 4621 | const Decl *AssociatedDecl, |
| 4622 | unsigned Index, bool Final, |
| 4623 | const TemplateArgument &ArgPack) { |
| 4624 | ID.AddPointer(Ptr: AssociatedDecl); |
| 4625 | ID.AddInteger(I: Index); |
| 4626 | ID.AddBoolean(B: Final); |
| 4627 | SubstPackType::Profile(ID, ArgPack); |
| 4628 | } |
| 4629 | |
| 4630 | SubstBuiltinTemplatePackType::SubstBuiltinTemplatePackType( |
| 4631 | QualType Canon, const TemplateArgument &ArgPack) |
| 4632 | : SubstPackType(SubstBuiltinTemplatePack, Canon, ArgPack) {} |
| 4633 | |
| 4634 | bool TemplateSpecializationType::anyDependentTemplateArguments( |
| 4635 | const TemplateArgumentListInfo &Args, |
| 4636 | ArrayRef<TemplateArgument> Converted) { |
| 4637 | return anyDependentTemplateArguments(Args: Args.arguments(), Converted); |
| 4638 | } |
| 4639 | |
| 4640 | bool TemplateSpecializationType::anyDependentTemplateArguments( |
| 4641 | ArrayRef<TemplateArgumentLoc> Args, ArrayRef<TemplateArgument> Converted) { |
| 4642 | for (const TemplateArgument &Arg : Converted) |
| 4643 | if (Arg.isDependent()) |
| 4644 | return true; |
| 4645 | return false; |
| 4646 | } |
| 4647 | |
| 4648 | bool TemplateSpecializationType::anyInstantiationDependentTemplateArguments( |
| 4649 | ArrayRef<TemplateArgumentLoc> Args) { |
| 4650 | for (const TemplateArgumentLoc &ArgLoc : Args) { |
| 4651 | if (ArgLoc.getArgument().isInstantiationDependent()) |
| 4652 | return true; |
| 4653 | } |
| 4654 | return false; |
| 4655 | } |
| 4656 | |
| 4657 | static TypeDependence |
| 4658 | getTemplateSpecializationTypeDependence(QualType Underlying, TemplateName T) { |
| 4659 | TypeDependence D = Underlying.isNull() |
| 4660 | ? TypeDependence::DependentInstantiation |
| 4661 | : toSemanticDependence(D: Underlying->getDependence()); |
| 4662 | D |= toTypeDependence(D: T.getDependence()) & TypeDependence::UnexpandedPack; |
| 4663 | if (isPackProducingBuiltinTemplateName(N: T)) { |
| 4664 | if (Underlying.isNull()) // Dependent, will produce a pack on substitution. |
| 4665 | D |= TypeDependence::UnexpandedPack; |
| 4666 | else |
| 4667 | D |= (Underlying->getDependence() & TypeDependence::UnexpandedPack); |
| 4668 | } |
| 4669 | return D; |
| 4670 | } |
| 4671 | |
| 4672 | TemplateSpecializationType::TemplateSpecializationType( |
| 4673 | ElaboratedTypeKeyword Keyword, TemplateName T, bool IsAlias, |
| 4674 | ArrayRef<TemplateArgument> Args, QualType Underlying) |
| 4675 | : TypeWithKeyword(Keyword, TemplateSpecialization, |
| 4676 | Underlying.isNull() ? QualType(this, 0) |
| 4677 | : Underlying.getCanonicalType(), |
| 4678 | getTemplateSpecializationTypeDependence(Underlying, T)), |
| 4679 | Template(T) { |
| 4680 | TemplateSpecializationTypeBits.NumArgs = Args.size(); |
| 4681 | TemplateSpecializationTypeBits.TypeAlias = IsAlias; |
| 4682 | |
| 4683 | auto *TemplateArgs = |
| 4684 | const_cast<TemplateArgument *>(template_arguments().data()); |
| 4685 | for (const TemplateArgument &Arg : Args) { |
| 4686 | // Update instantiation-dependent, variably-modified, and error bits. |
| 4687 | // If the canonical type exists and is non-dependent, the template |
| 4688 | // specialization type can be non-dependent even if one of the type |
| 4689 | // arguments is. Given: |
| 4690 | // template<typename T> using U = int; |
| 4691 | // U<T> is always non-dependent, irrespective of the type T. |
| 4692 | // However, U<Ts> contains an unexpanded parameter pack, even though |
| 4693 | // its expansion (and thus its desugared type) doesn't. |
| 4694 | addDependence(D: toTypeDependence(D: Arg.getDependence()) & |
| 4695 | ~TypeDependence::Dependent); |
| 4696 | if (Arg.getKind() == TemplateArgument::Type) |
| 4697 | addDependence(D: Arg.getAsType()->getDependence() & |
| 4698 | TypeDependence::VariablyModified); |
| 4699 | new (TemplateArgs++) TemplateArgument(Arg); |
| 4700 | } |
| 4701 | |
| 4702 | // Store the aliased type after the template arguments, if this is a type |
| 4703 | // alias template specialization. |
| 4704 | if (IsAlias) |
| 4705 | *reinterpret_cast<QualType *>(TemplateArgs) = Underlying; |
| 4706 | } |
| 4707 | |
| 4708 | QualType TemplateSpecializationType::getAliasedType() const { |
| 4709 | assert(isTypeAlias() && "not a type alias template specialization" ); |
| 4710 | return *reinterpret_cast<const QualType *>(template_arguments().end()); |
| 4711 | } |
| 4712 | |
| 4713 | bool clang::TemplateSpecializationType::isSugared() const { |
| 4714 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias() || |
| 4715 | (isPackProducingBuiltinTemplateName(N: Template) && |
| 4716 | isa<SubstBuiltinTemplatePackType>(Val: *getCanonicalTypeInternal())); |
| 4717 | } |
| 4718 | |
| 4719 | void TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 4720 | const ASTContext &Ctx) { |
| 4721 | Profile(ID, Keyword: getKeyword(), T: Template, Args: template_arguments(), |
| 4722 | Underlying: isSugared() ? desugar() : QualType(), Context: Ctx); |
| 4723 | } |
| 4724 | |
| 4725 | void TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 4726 | ElaboratedTypeKeyword Keyword, |
| 4727 | TemplateName T, |
| 4728 | ArrayRef<TemplateArgument> Args, |
| 4729 | QualType Underlying, |
| 4730 | const ASTContext &Context) { |
| 4731 | ID.AddInteger(I: llvm::to_underlying(E: Keyword)); |
| 4732 | T.Profile(ID); |
| 4733 | Underlying.Profile(ID); |
| 4734 | |
| 4735 | ID.AddInteger(I: Args.size()); |
| 4736 | for (const TemplateArgument &Arg : Args) |
| 4737 | Arg.Profile(ID, Context); |
| 4738 | } |
| 4739 | |
| 4740 | QualType QualifierCollector::apply(const ASTContext &Context, |
| 4741 | QualType QT) const { |
| 4742 | if (!hasNonFastQualifiers()) |
| 4743 | return QT.withFastQualifiers(TQs: getFastQualifiers()); |
| 4744 | |
| 4745 | return Context.getQualifiedType(T: QT, Qs: *this); |
| 4746 | } |
| 4747 | |
| 4748 | QualType QualifierCollector::apply(const ASTContext &Context, |
| 4749 | const Type *T) const { |
| 4750 | if (!hasNonFastQualifiers()) |
| 4751 | return QualType(T, getFastQualifiers()); |
| 4752 | |
| 4753 | return Context.getQualifiedType(T, Qs: *this); |
| 4754 | } |
| 4755 | |
| 4756 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
| 4757 | ArrayRef<QualType> typeArgs, |
| 4758 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4759 | bool isKindOf) { |
| 4760 | ID.AddPointer(Ptr: BaseType.getAsOpaquePtr()); |
| 4761 | ID.AddInteger(I: typeArgs.size()); |
| 4762 | for (auto typeArg : typeArgs) |
| 4763 | ID.AddPointer(Ptr: typeArg.getAsOpaquePtr()); |
| 4764 | ID.AddInteger(I: protocols.size()); |
| 4765 | for (auto *proto : protocols) |
| 4766 | ID.AddPointer(Ptr: proto); |
| 4767 | ID.AddBoolean(B: isKindOf); |
| 4768 | } |
| 4769 | |
| 4770 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { |
| 4771 | Profile(ID, BaseType: getBaseType(), typeArgs: getTypeArgsAsWritten(), |
| 4772 | protocols: llvm::ArrayRef(qual_begin(), getNumProtocols()), |
| 4773 | isKindOf: isKindOfTypeAsWritten()); |
| 4774 | } |
| 4775 | |
| 4776 | void ObjCTypeParamType::Profile(llvm::FoldingSetNodeID &ID, |
| 4777 | const ObjCTypeParamDecl *OTPDecl, |
| 4778 | QualType CanonicalType, |
| 4779 | ArrayRef<ObjCProtocolDecl *> protocols) { |
| 4780 | ID.AddPointer(Ptr: OTPDecl); |
| 4781 | ID.AddPointer(Ptr: CanonicalType.getAsOpaquePtr()); |
| 4782 | ID.AddInteger(I: protocols.size()); |
| 4783 | for (auto *proto : protocols) |
| 4784 | ID.AddPointer(Ptr: proto); |
| 4785 | } |
| 4786 | |
| 4787 | void ObjCTypeParamType::Profile(llvm::FoldingSetNodeID &ID) { |
| 4788 | Profile(ID, OTPDecl: getDecl(), CanonicalType: getCanonicalTypeInternal(), |
| 4789 | protocols: llvm::ArrayRef(qual_begin(), getNumProtocols())); |
| 4790 | } |
| 4791 | |
| 4792 | namespace { |
| 4793 | |
| 4794 | /// The cached properties of a type. |
| 4795 | class CachedProperties { |
| 4796 | Linkage L; |
| 4797 | bool local; |
| 4798 | |
| 4799 | public: |
| 4800 | CachedProperties(Linkage L, bool local) : L(L), local(local) {} |
| 4801 | |
| 4802 | Linkage getLinkage() const { return L; } |
| 4803 | bool hasLocalOrUnnamedType() const { return local; } |
| 4804 | |
| 4805 | friend CachedProperties merge(CachedProperties L, CachedProperties R) { |
| 4806 | Linkage MergedLinkage = minLinkage(L1: L.L, L2: R.L); |
| 4807 | return CachedProperties(MergedLinkage, L.hasLocalOrUnnamedType() || |
| 4808 | R.hasLocalOrUnnamedType()); |
| 4809 | } |
| 4810 | }; |
| 4811 | |
| 4812 | } // namespace |
| 4813 | |
| 4814 | static CachedProperties computeCachedProperties(const Type *T); |
| 4815 | |
| 4816 | namespace clang { |
| 4817 | |
| 4818 | /// The type-property cache. This is templated so as to be |
| 4819 | /// instantiated at an internal type to prevent unnecessary symbol |
| 4820 | /// leakage. |
| 4821 | template <class Private> class TypePropertyCache { |
| 4822 | public: |
| 4823 | static CachedProperties get(QualType T) { return get(T.getTypePtr()); } |
| 4824 | |
| 4825 | static CachedProperties get(const Type *T) { |
| 4826 | ensure(T); |
| 4827 | return CachedProperties(T->TypeBits.getLinkage(), |
| 4828 | T->TypeBits.hasLocalOrUnnamedType()); |
| 4829 | } |
| 4830 | |
| 4831 | static void ensure(const Type *T) { |
| 4832 | // If the cache is valid, we're okay. |
| 4833 | if (T->TypeBits.isCacheValid()) |
| 4834 | return; |
| 4835 | |
| 4836 | // If this type is non-canonical, ask its canonical type for the |
| 4837 | // relevant information. |
| 4838 | if (!T->isCanonicalUnqualified()) { |
| 4839 | const Type *CT = T->getCanonicalTypeInternal().getTypePtr(); |
| 4840 | ensure(T: CT); |
| 4841 | T->TypeBits.CacheValid = true; |
| 4842 | T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage; |
| 4843 | T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed; |
| 4844 | return; |
| 4845 | } |
| 4846 | |
| 4847 | // Compute the cached properties and then set the cache. |
| 4848 | CachedProperties Result = computeCachedProperties(T); |
| 4849 | T->TypeBits.CacheValid = true; |
| 4850 | T->TypeBits.CachedLinkage = llvm::to_underlying(E: Result.getLinkage()); |
| 4851 | T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType(); |
| 4852 | } |
| 4853 | }; |
| 4854 | |
| 4855 | } // namespace clang |
| 4856 | |
| 4857 | // Instantiate the friend template at a private class. In a |
| 4858 | // reasonable implementation, these symbols will be internal. |
| 4859 | // It is terrible that this is the best way to accomplish this. |
| 4860 | namespace { |
| 4861 | |
| 4862 | class Private {}; |
| 4863 | |
| 4864 | } // namespace |
| 4865 | |
| 4866 | using Cache = TypePropertyCache<Private>; |
| 4867 | |
| 4868 | static CachedProperties computeCachedProperties(const Type *T) { |
| 4869 | switch (T->getTypeClass()) { |
| 4870 | #define TYPE(Class, Base) |
| 4871 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4872 | #include "clang/AST/TypeNodes.inc" |
| 4873 | llvm_unreachable("didn't expect a non-canonical type here" ); |
| 4874 | |
| 4875 | #define TYPE(Class, Base) |
| 4876 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4877 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4878 | #include "clang/AST/TypeNodes.inc" |
| 4879 | // Treat instantiation-dependent types as external. |
| 4880 | assert(T->isInstantiationDependentType()); |
| 4881 | return CachedProperties(Linkage::External, false); |
| 4882 | |
| 4883 | case Type::Auto: |
| 4884 | case Type::DeducedTemplateSpecialization: |
| 4885 | // Give non-deduced 'auto' types external linkage. We should only see them |
| 4886 | // here in error recovery. |
| 4887 | return CachedProperties(Linkage::External, false); |
| 4888 | |
| 4889 | case Type::BitInt: |
| 4890 | case Type::Builtin: |
| 4891 | // C++ [basic.link]p8: |
| 4892 | // A type is said to have linkage if and only if: |
| 4893 | // - it is a fundamental type (3.9.1); or |
| 4894 | return CachedProperties(Linkage::External, false); |
| 4895 | |
| 4896 | case Type::Record: |
| 4897 | case Type::Enum: { |
| 4898 | const auto *Tag = cast<TagType>(Val: T)->getDecl()->getDefinitionOrSelf(); |
| 4899 | |
| 4900 | // C++ [basic.link]p8: |
| 4901 | // - it is a class or enumeration type that is named (or has a name |
| 4902 | // for linkage purposes (7.1.3)) and the name has linkage; or |
| 4903 | // - it is a specialization of a class template (14); or |
| 4904 | Linkage L = Tag->getLinkageInternal(); |
| 4905 | bool IsLocalOrUnnamed = Tag->getDeclContext()->isFunctionOrMethod() || |
| 4906 | !Tag->hasNameForLinkage(); |
| 4907 | return CachedProperties(L, IsLocalOrUnnamed); |
| 4908 | } |
| 4909 | |
| 4910 | // C++ [basic.link]p8: |
| 4911 | // - it is a compound type (3.9.2) other than a class or enumeration, |
| 4912 | // compounded exclusively from types that have linkage; or |
| 4913 | case Type::Complex: |
| 4914 | return Cache::get(T: cast<ComplexType>(Val: T)->getElementType()); |
| 4915 | case Type::Pointer: |
| 4916 | return Cache::get(T: cast<PointerType>(Val: T)->getPointeeType()); |
| 4917 | case Type::BlockPointer: |
| 4918 | return Cache::get(T: cast<BlockPointerType>(Val: T)->getPointeeType()); |
| 4919 | case Type::LValueReference: |
| 4920 | case Type::RValueReference: |
| 4921 | return Cache::get(T: cast<ReferenceType>(Val: T)->getPointeeType()); |
| 4922 | case Type::MemberPointer: { |
| 4923 | const auto *MPT = cast<MemberPointerType>(Val: T); |
| 4924 | CachedProperties Cls = [&] { |
| 4925 | if (MPT->isSugared()) |
| 4926 | MPT = cast<MemberPointerType>(Val: MPT->getCanonicalTypeInternal()); |
| 4927 | return Cache::get(T: MPT->getQualifier().getAsType()); |
| 4928 | }(); |
| 4929 | return merge(L: Cls, R: Cache::get(T: MPT->getPointeeType())); |
| 4930 | } |
| 4931 | case Type::ConstantArray: |
| 4932 | case Type::IncompleteArray: |
| 4933 | case Type::VariableArray: |
| 4934 | case Type::ArrayParameter: |
| 4935 | return Cache::get(T: cast<ArrayType>(Val: T)->getElementType()); |
| 4936 | case Type::Vector: |
| 4937 | case Type::ExtVector: |
| 4938 | return Cache::get(T: cast<VectorType>(Val: T)->getElementType()); |
| 4939 | case Type::ConstantMatrix: |
| 4940 | return Cache::get(T: cast<ConstantMatrixType>(Val: T)->getElementType()); |
| 4941 | case Type::FunctionNoProto: |
| 4942 | return Cache::get(T: cast<FunctionType>(Val: T)->getReturnType()); |
| 4943 | case Type::FunctionProto: { |
| 4944 | const auto *FPT = cast<FunctionProtoType>(Val: T); |
| 4945 | CachedProperties result = Cache::get(T: FPT->getReturnType()); |
| 4946 | for (const auto &ai : FPT->param_types()) |
| 4947 | result = merge(L: result, R: Cache::get(T: ai)); |
| 4948 | return result; |
| 4949 | } |
| 4950 | case Type::ObjCInterface: { |
| 4951 | Linkage L = cast<ObjCInterfaceType>(Val: T)->getDecl()->getLinkageInternal(); |
| 4952 | return CachedProperties(L, false); |
| 4953 | } |
| 4954 | case Type::ObjCObject: |
| 4955 | return Cache::get(T: cast<ObjCObjectType>(Val: T)->getBaseType()); |
| 4956 | case Type::ObjCObjectPointer: |
| 4957 | return Cache::get(T: cast<ObjCObjectPointerType>(Val: T)->getPointeeType()); |
| 4958 | case Type::Atomic: |
| 4959 | return Cache::get(T: cast<AtomicType>(Val: T)->getValueType()); |
| 4960 | case Type::Pipe: |
| 4961 | return Cache::get(T: cast<PipeType>(Val: T)->getElementType()); |
| 4962 | case Type::HLSLAttributedResource: |
| 4963 | return Cache::get(T: cast<HLSLAttributedResourceType>(Val: T)->getWrappedType()); |
| 4964 | case Type::HLSLInlineSpirv: |
| 4965 | return CachedProperties(Linkage::External, false); |
| 4966 | case Type::OverflowBehavior: |
| 4967 | return Cache::get(T: cast<OverflowBehaviorType>(Val: T)->getUnderlyingType()); |
| 4968 | } |
| 4969 | |
| 4970 | llvm_unreachable("unhandled type class" ); |
| 4971 | } |
| 4972 | |
| 4973 | /// Determine the linkage of this type. |
| 4974 | Linkage Type::getLinkage() const { |
| 4975 | Cache::ensure(T: this); |
| 4976 | return TypeBits.getLinkage(); |
| 4977 | } |
| 4978 | |
| 4979 | bool Type::hasUnnamedOrLocalType() const { |
| 4980 | Cache::ensure(T: this); |
| 4981 | return TypeBits.hasLocalOrUnnamedType(); |
| 4982 | } |
| 4983 | |
| 4984 | LinkageInfo LinkageComputer::computeTypeLinkageInfo(const Type *T) { |
| 4985 | switch (T->getTypeClass()) { |
| 4986 | #define TYPE(Class, Base) |
| 4987 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4988 | #include "clang/AST/TypeNodes.inc" |
| 4989 | llvm_unreachable("didn't expect a non-canonical type here" ); |
| 4990 | |
| 4991 | #define TYPE(Class, Base) |
| 4992 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4993 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4994 | #include "clang/AST/TypeNodes.inc" |
| 4995 | // Treat instantiation-dependent types as external. |
| 4996 | assert(T->isInstantiationDependentType()); |
| 4997 | return LinkageInfo::external(); |
| 4998 | |
| 4999 | case Type::BitInt: |
| 5000 | case Type::Builtin: |
| 5001 | return LinkageInfo::external(); |
| 5002 | |
| 5003 | case Type::Auto: |
| 5004 | case Type::DeducedTemplateSpecialization: |
| 5005 | return LinkageInfo::external(); |
| 5006 | |
| 5007 | case Type::Record: |
| 5008 | case Type::Enum: |
| 5009 | return getDeclLinkageAndVisibility( |
| 5010 | D: cast<TagType>(Val: T)->getDecl()->getDefinitionOrSelf()); |
| 5011 | |
| 5012 | case Type::Complex: |
| 5013 | return computeTypeLinkageInfo(T: cast<ComplexType>(Val: T)->getElementType()); |
| 5014 | case Type::Pointer: |
| 5015 | return computeTypeLinkageInfo(T: cast<PointerType>(Val: T)->getPointeeType()); |
| 5016 | case Type::BlockPointer: |
| 5017 | return computeTypeLinkageInfo(T: cast<BlockPointerType>(Val: T)->getPointeeType()); |
| 5018 | case Type::LValueReference: |
| 5019 | case Type::RValueReference: |
| 5020 | return computeTypeLinkageInfo(T: cast<ReferenceType>(Val: T)->getPointeeType()); |
| 5021 | case Type::MemberPointer: { |
| 5022 | const auto *MPT = cast<MemberPointerType>(Val: T); |
| 5023 | LinkageInfo LV; |
| 5024 | if (auto *D = MPT->getMostRecentCXXRecordDecl()) { |
| 5025 | LV.merge(other: getDeclLinkageAndVisibility(D)); |
| 5026 | } else { |
| 5027 | LV.merge(other: computeTypeLinkageInfo(T: MPT->getQualifier().getAsType())); |
| 5028 | } |
| 5029 | LV.merge(other: computeTypeLinkageInfo(T: MPT->getPointeeType())); |
| 5030 | return LV; |
| 5031 | } |
| 5032 | case Type::ConstantArray: |
| 5033 | case Type::IncompleteArray: |
| 5034 | case Type::VariableArray: |
| 5035 | case Type::ArrayParameter: |
| 5036 | return computeTypeLinkageInfo(T: cast<ArrayType>(Val: T)->getElementType()); |
| 5037 | case Type::Vector: |
| 5038 | case Type::ExtVector: |
| 5039 | return computeTypeLinkageInfo(T: cast<VectorType>(Val: T)->getElementType()); |
| 5040 | case Type::ConstantMatrix: |
| 5041 | return computeTypeLinkageInfo( |
| 5042 | T: cast<ConstantMatrixType>(Val: T)->getElementType()); |
| 5043 | case Type::FunctionNoProto: |
| 5044 | return computeTypeLinkageInfo(T: cast<FunctionType>(Val: T)->getReturnType()); |
| 5045 | case Type::FunctionProto: { |
| 5046 | const auto *FPT = cast<FunctionProtoType>(Val: T); |
| 5047 | LinkageInfo LV = computeTypeLinkageInfo(T: FPT->getReturnType()); |
| 5048 | for (const auto &ai : FPT->param_types()) |
| 5049 | LV.merge(other: computeTypeLinkageInfo(T: ai)); |
| 5050 | return LV; |
| 5051 | } |
| 5052 | case Type::ObjCInterface: |
| 5053 | return getDeclLinkageAndVisibility(D: cast<ObjCInterfaceType>(Val: T)->getDecl()); |
| 5054 | case Type::ObjCObject: |
| 5055 | return computeTypeLinkageInfo(T: cast<ObjCObjectType>(Val: T)->getBaseType()); |
| 5056 | case Type::ObjCObjectPointer: |
| 5057 | return computeTypeLinkageInfo( |
| 5058 | T: cast<ObjCObjectPointerType>(Val: T)->getPointeeType()); |
| 5059 | case Type::Atomic: |
| 5060 | return computeTypeLinkageInfo(T: cast<AtomicType>(Val: T)->getValueType()); |
| 5061 | case Type::Pipe: |
| 5062 | return computeTypeLinkageInfo(T: cast<PipeType>(Val: T)->getElementType()); |
| 5063 | case Type::OverflowBehavior: |
| 5064 | return computeTypeLinkageInfo( |
| 5065 | T: cast<OverflowBehaviorType>(Val: T)->getUnderlyingType()); |
| 5066 | case Type::HLSLAttributedResource: |
| 5067 | return computeTypeLinkageInfo(T: cast<HLSLAttributedResourceType>(Val: T) |
| 5068 | ->getContainedType() |
| 5069 | ->getCanonicalTypeInternal()); |
| 5070 | case Type::HLSLInlineSpirv: |
| 5071 | return LinkageInfo::external(); |
| 5072 | } |
| 5073 | |
| 5074 | llvm_unreachable("unhandled type class" ); |
| 5075 | } |
| 5076 | |
| 5077 | bool Type::isLinkageValid() const { |
| 5078 | if (!TypeBits.isCacheValid()) |
| 5079 | return true; |
| 5080 | |
| 5081 | Linkage L = LinkageComputer{} |
| 5082 | .computeTypeLinkageInfo(T: getCanonicalTypeInternal()) |
| 5083 | .getLinkage(); |
| 5084 | return L == TypeBits.getLinkage(); |
| 5085 | } |
| 5086 | |
| 5087 | LinkageInfo LinkageComputer::getTypeLinkageAndVisibility(const Type *T) { |
| 5088 | if (!T->isCanonicalUnqualified()) |
| 5089 | return computeTypeLinkageInfo(T: T->getCanonicalTypeInternal()); |
| 5090 | |
| 5091 | LinkageInfo LV = computeTypeLinkageInfo(T); |
| 5092 | assert(LV.getLinkage() == T->getLinkage()); |
| 5093 | return LV; |
| 5094 | } |
| 5095 | |
| 5096 | LinkageInfo Type::getLinkageAndVisibility() const { |
| 5097 | return LinkageComputer{}.getTypeLinkageAndVisibility(T: this); |
| 5098 | } |
| 5099 | |
| 5100 | std::optional<NullabilityKind> Type::getNullability() const { |
| 5101 | QualType Type(this, 0); |
| 5102 | while (const auto *AT = Type->getAs<AttributedType>()) { |
| 5103 | // Check whether this is an attributed type with nullability |
| 5104 | // information. |
| 5105 | if (auto Nullability = AT->getImmediateNullability()) |
| 5106 | return Nullability; |
| 5107 | |
| 5108 | Type = AT->getEquivalentType(); |
| 5109 | } |
| 5110 | return std::nullopt; |
| 5111 | } |
| 5112 | |
| 5113 | bool Type::canHaveNullability(bool ResultIfUnknown) const { |
| 5114 | QualType type = getCanonicalTypeInternal(); |
| 5115 | |
| 5116 | switch (type->getTypeClass()) { |
| 5117 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5118 | /* We'll only see canonical types here. */ \ |
| 5119 | case Type::Class: \ |
| 5120 | llvm_unreachable("non-canonical type"); |
| 5121 | #define TYPE(Class, Parent) |
| 5122 | #include "clang/AST/TypeNodes.inc" |
| 5123 | |
| 5124 | // Pointer types. |
| 5125 | case Type::Pointer: |
| 5126 | case Type::BlockPointer: |
| 5127 | case Type::MemberPointer: |
| 5128 | case Type::ObjCObjectPointer: |
| 5129 | return true; |
| 5130 | |
| 5131 | // Dependent types that could instantiate to pointer types. |
| 5132 | case Type::UnresolvedUsing: |
| 5133 | case Type::TypeOfExpr: |
| 5134 | case Type::TypeOf: |
| 5135 | case Type::Decltype: |
| 5136 | case Type::PackIndexing: |
| 5137 | case Type::UnaryTransform: |
| 5138 | case Type::TemplateTypeParm: |
| 5139 | case Type::SubstTemplateTypeParmPack: |
| 5140 | case Type::SubstBuiltinTemplatePack: |
| 5141 | case Type::DependentName: |
| 5142 | case Type::Auto: |
| 5143 | return ResultIfUnknown; |
| 5144 | |
| 5145 | // Dependent template specializations could instantiate to pointer types. |
| 5146 | case Type::TemplateSpecialization: |
| 5147 | // If it's a known class template, we can already check if it's nullable. |
| 5148 | if (TemplateDecl *templateDecl = |
| 5149 | cast<TemplateSpecializationType>(Val: type.getTypePtr()) |
| 5150 | ->getTemplateName() |
| 5151 | .getAsTemplateDecl()) |
| 5152 | if (auto *CTD = dyn_cast<ClassTemplateDecl>(Val: templateDecl)) |
| 5153 | return llvm::any_of( |
| 5154 | Range: CTD->redecls(), P: [](const RedeclarableTemplateDecl *RTD) { |
| 5155 | return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>(); |
| 5156 | }); |
| 5157 | return ResultIfUnknown; |
| 5158 | |
| 5159 | case Type::Builtin: |
| 5160 | switch (cast<BuiltinType>(Val: type.getTypePtr())->getKind()) { |
| 5161 | // Signed, unsigned, and floating-point types cannot have nullability. |
| 5162 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 5163 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 5164 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 5165 | #define BUILTIN_TYPE(Id, SingletonId) |
| 5166 | #include "clang/AST/BuiltinTypes.def" |
| 5167 | return false; |
| 5168 | |
| 5169 | case BuiltinType::UnresolvedTemplate: |
| 5170 | // Dependent types that could instantiate to a pointer type. |
| 5171 | case BuiltinType::Dependent: |
| 5172 | case BuiltinType::Overload: |
| 5173 | case BuiltinType::BoundMember: |
| 5174 | case BuiltinType::PseudoObject: |
| 5175 | case BuiltinType::UnknownAny: |
| 5176 | case BuiltinType::ARCUnbridgedCast: |
| 5177 | return ResultIfUnknown; |
| 5178 | |
| 5179 | case BuiltinType::Void: |
| 5180 | case BuiltinType::ObjCId: |
| 5181 | case BuiltinType::ObjCClass: |
| 5182 | case BuiltinType::ObjCSel: |
| 5183 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 5184 | case BuiltinType::Id: |
| 5185 | #include "clang/Basic/OpenCLImageTypes.def" |
| 5186 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) case BuiltinType::Id: |
| 5187 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| 5188 | case BuiltinType::OCLSampler: |
| 5189 | case BuiltinType::OCLEvent: |
| 5190 | case BuiltinType::OCLClkEvent: |
| 5191 | case BuiltinType::OCLQueue: |
| 5192 | case BuiltinType::OCLReserveID: |
| 5193 | #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 5194 | #include "clang/Basic/AArch64ACLETypes.def" |
| 5195 | #define PPC_VECTOR_TYPE(Name, Id, Size) case BuiltinType::Id: |
| 5196 | #include "clang/Basic/PPCTypes.def" |
| 5197 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 5198 | #include "clang/Basic/RISCVVTypes.def" |
| 5199 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 5200 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 5201 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: |
| 5202 | #include "clang/Basic/AMDGPUTypes.def" |
| 5203 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 5204 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 5205 | case BuiltinType::BuiltinFn: |
| 5206 | case BuiltinType::NullPtr: |
| 5207 | case BuiltinType::IncompleteMatrixIdx: |
| 5208 | case BuiltinType::ArraySection: |
| 5209 | case BuiltinType::OMPArrayShaping: |
| 5210 | case BuiltinType::OMPIterator: |
| 5211 | return false; |
| 5212 | } |
| 5213 | llvm_unreachable("unknown builtin type" ); |
| 5214 | |
| 5215 | case Type::Record: { |
| 5216 | const auto *RD = cast<RecordType>(Val&: type)->getDecl(); |
| 5217 | // For template specializations, look only at primary template attributes. |
| 5218 | // This is a consistent regardless of whether the instantiation is known. |
| 5219 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: RD)) |
| 5220 | return llvm::any_of( |
| 5221 | Range: CTSD->getSpecializedTemplate()->redecls(), |
| 5222 | P: [](const RedeclarableTemplateDecl *RTD) { |
| 5223 | return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>(); |
| 5224 | }); |
| 5225 | return llvm::any_of(Range: RD->redecls(), P: [](const TagDecl *RD) { |
| 5226 | return RD->hasAttr<TypeNullableAttr>(); |
| 5227 | }); |
| 5228 | } |
| 5229 | |
| 5230 | // Non-pointer types. |
| 5231 | case Type::Complex: |
| 5232 | case Type::LValueReference: |
| 5233 | case Type::RValueReference: |
| 5234 | case Type::ConstantArray: |
| 5235 | case Type::IncompleteArray: |
| 5236 | case Type::VariableArray: |
| 5237 | case Type::DependentSizedArray: |
| 5238 | case Type::DependentVector: |
| 5239 | case Type::DependentSizedExtVector: |
| 5240 | case Type::Vector: |
| 5241 | case Type::ExtVector: |
| 5242 | case Type::ConstantMatrix: |
| 5243 | case Type::DependentSizedMatrix: |
| 5244 | case Type::DependentAddressSpace: |
| 5245 | case Type::FunctionProto: |
| 5246 | case Type::FunctionNoProto: |
| 5247 | case Type::DeducedTemplateSpecialization: |
| 5248 | case Type::Enum: |
| 5249 | case Type::InjectedClassName: |
| 5250 | case Type::PackExpansion: |
| 5251 | case Type::ObjCObject: |
| 5252 | case Type::ObjCInterface: |
| 5253 | case Type::Atomic: |
| 5254 | case Type::Pipe: |
| 5255 | case Type::BitInt: |
| 5256 | case Type::DependentBitInt: |
| 5257 | case Type::ArrayParameter: |
| 5258 | case Type::HLSLAttributedResource: |
| 5259 | case Type::HLSLInlineSpirv: |
| 5260 | case Type::OverflowBehavior: |
| 5261 | return false; |
| 5262 | } |
| 5263 | llvm_unreachable("bad type kind!" ); |
| 5264 | } |
| 5265 | |
| 5266 | std::optional<NullabilityKind> AttributedType::getImmediateNullability() const { |
| 5267 | if (getAttrKind() == attr::TypeNonNull) |
| 5268 | return NullabilityKind::NonNull; |
| 5269 | if (getAttrKind() == attr::TypeNullable) |
| 5270 | return NullabilityKind::Nullable; |
| 5271 | if (getAttrKind() == attr::TypeNullUnspecified) |
| 5272 | return NullabilityKind::Unspecified; |
| 5273 | if (getAttrKind() == attr::TypeNullableResult) |
| 5274 | return NullabilityKind::NullableResult; |
| 5275 | return std::nullopt; |
| 5276 | } |
| 5277 | |
| 5278 | std::optional<NullabilityKind> |
| 5279 | AttributedType::stripOuterNullability(QualType &T) { |
| 5280 | QualType AttrTy = T; |
| 5281 | if (auto MacroTy = dyn_cast<MacroQualifiedType>(Val&: T)) |
| 5282 | AttrTy = MacroTy->getUnderlyingType(); |
| 5283 | |
| 5284 | if (auto attributed = dyn_cast<AttributedType>(Val&: AttrTy)) { |
| 5285 | if (auto nullability = attributed->getImmediateNullability()) { |
| 5286 | T = attributed->getModifiedType(); |
| 5287 | return nullability; |
| 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | return std::nullopt; |
| 5292 | } |
| 5293 | |
| 5294 | bool Type::isSignableIntegerType(const ASTContext &Ctx) const { |
| 5295 | if (!isIntegralType(Ctx) || isEnumeralType()) |
| 5296 | return false; |
| 5297 | return Ctx.getTypeSize(T: this) == Ctx.getTypeSize(T: Ctx.VoidPtrTy); |
| 5298 | } |
| 5299 | |
| 5300 | bool Type::isBlockCompatibleObjCPointerType(ASTContext &ctx) const { |
| 5301 | const auto *objcPtr = getAs<ObjCObjectPointerType>(); |
| 5302 | if (!objcPtr) |
| 5303 | return false; |
| 5304 | |
| 5305 | if (objcPtr->isObjCIdType()) { |
| 5306 | // id is always okay. |
| 5307 | return true; |
| 5308 | } |
| 5309 | |
| 5310 | // Blocks are NSObjects. |
| 5311 | if (ObjCInterfaceDecl *iface = objcPtr->getInterfaceDecl()) { |
| 5312 | if (iface->getIdentifier() != ctx.getNSObjectName()) |
| 5313 | return false; |
| 5314 | |
| 5315 | // Continue to check qualifiers, below. |
| 5316 | } else if (objcPtr->isObjCQualifiedIdType()) { |
| 5317 | // Continue to check qualifiers, below. |
| 5318 | } else { |
| 5319 | return false; |
| 5320 | } |
| 5321 | |
| 5322 | // Check protocol qualifiers. |
| 5323 | for (ObjCProtocolDecl *proto : objcPtr->quals()) { |
| 5324 | // Blocks conform to NSObject and NSCopying. |
| 5325 | if (proto->getIdentifier() != ctx.getNSObjectName() && |
| 5326 | proto->getIdentifier() != ctx.getNSCopyingName()) |
| 5327 | return false; |
| 5328 | } |
| 5329 | |
| 5330 | return true; |
| 5331 | } |
| 5332 | |
| 5333 | Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const { |
| 5334 | if (isObjCARCImplicitlyUnretainedType()) |
| 5335 | return Qualifiers::OCL_ExplicitNone; |
| 5336 | return Qualifiers::OCL_Strong; |
| 5337 | } |
| 5338 | |
| 5339 | bool Type::isObjCARCImplicitlyUnretainedType() const { |
| 5340 | assert(isObjCLifetimeType() && |
| 5341 | "cannot query implicit lifetime for non-inferrable type" ); |
| 5342 | |
| 5343 | const Type *canon = getCanonicalTypeInternal().getTypePtr(); |
| 5344 | |
| 5345 | // Walk down to the base type. We don't care about qualifiers for this. |
| 5346 | while (const auto *array = dyn_cast<ArrayType>(Val: canon)) |
| 5347 | canon = array->getElementType().getTypePtr(); |
| 5348 | |
| 5349 | if (const auto *opt = dyn_cast<ObjCObjectPointerType>(Val: canon)) { |
| 5350 | // Class and Class<Protocol> don't require retention. |
| 5351 | if (opt->getObjectType()->isObjCClass()) |
| 5352 | return true; |
| 5353 | } |
| 5354 | |
| 5355 | return false; |
| 5356 | } |
| 5357 | |
| 5358 | bool Type::isObjCNSObjectType() const { |
| 5359 | if (const auto *typedefType = getAs<TypedefType>()) |
| 5360 | return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>(); |
| 5361 | return false; |
| 5362 | } |
| 5363 | |
| 5364 | bool Type::isObjCIndependentClassType() const { |
| 5365 | if (const auto *typedefType = getAs<TypedefType>()) |
| 5366 | return typedefType->getDecl()->hasAttr<ObjCIndependentClassAttr>(); |
| 5367 | return false; |
| 5368 | } |
| 5369 | |
| 5370 | bool Type::isObjCRetainableType() const { |
| 5371 | return isObjCObjectPointerType() || isBlockPointerType() || |
| 5372 | isObjCNSObjectType(); |
| 5373 | } |
| 5374 | |
| 5375 | bool Type::isObjCIndirectLifetimeType() const { |
| 5376 | if (isObjCLifetimeType()) |
| 5377 | return true; |
| 5378 | if (const auto *OPT = getAs<PointerType>()) |
| 5379 | return OPT->getPointeeType()->isObjCIndirectLifetimeType(); |
| 5380 | if (const auto *Ref = getAs<ReferenceType>()) |
| 5381 | return Ref->getPointeeType()->isObjCIndirectLifetimeType(); |
| 5382 | if (const auto *MemPtr = getAs<MemberPointerType>()) |
| 5383 | return MemPtr->getPointeeType()->isObjCIndirectLifetimeType(); |
| 5384 | return false; |
| 5385 | } |
| 5386 | |
| 5387 | /// Returns true if objects of this type have lifetime semantics under |
| 5388 | /// ARC. |
| 5389 | bool Type::isObjCLifetimeType() const { |
| 5390 | const Type *type = this; |
| 5391 | while (const ArrayType *array = type->getAsArrayTypeUnsafe()) |
| 5392 | type = array->getElementType().getTypePtr(); |
| 5393 | return type->isObjCRetainableType(); |
| 5394 | } |
| 5395 | |
| 5396 | /// Determine whether the given type T is a "bridgable" Objective-C type, |
| 5397 | /// which is either an Objective-C object pointer type or an |
| 5398 | bool Type::isObjCARCBridgableType() const { |
| 5399 | return isObjCObjectPointerType() || isBlockPointerType(); |
| 5400 | } |
| 5401 | |
| 5402 | /// Determine whether the given type T is a "bridgeable" C type. |
| 5403 | bool Type::isCARCBridgableType() const { |
| 5404 | const auto *Pointer = getAsCanonical<PointerType>(); |
| 5405 | if (!Pointer) |
| 5406 | return false; |
| 5407 | |
| 5408 | QualType Pointee = Pointer->getPointeeType(); |
| 5409 | return Pointee->isVoidType() || Pointee->isRecordType(); |
| 5410 | } |
| 5411 | |
| 5412 | /// Check if the specified type is the CUDA device builtin surface type. |
| 5413 | bool Type::isCUDADeviceBuiltinSurfaceType() const { |
| 5414 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 5415 | return RT->getDecl() |
| 5416 | ->getMostRecentDecl() |
| 5417 | ->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>(); |
| 5418 | return false; |
| 5419 | } |
| 5420 | |
| 5421 | /// Check if the specified type is the CUDA device builtin texture type. |
| 5422 | bool Type::isCUDADeviceBuiltinTextureType() const { |
| 5423 | if (const auto *RT = getAsCanonical<RecordType>()) |
| 5424 | return RT->getDecl() |
| 5425 | ->getMostRecentDecl() |
| 5426 | ->hasAttr<CUDADeviceBuiltinTextureTypeAttr>(); |
| 5427 | return false; |
| 5428 | } |
| 5429 | |
| 5430 | bool Type::hasSizedVLAType() const { |
| 5431 | if (!isVariablyModifiedType()) |
| 5432 | return false; |
| 5433 | |
| 5434 | if (const auto *ptr = getAs<PointerType>()) |
| 5435 | return ptr->getPointeeType()->hasSizedVLAType(); |
| 5436 | if (const auto *ref = getAs<ReferenceType>()) |
| 5437 | return ref->getPointeeType()->hasSizedVLAType(); |
| 5438 | if (const ArrayType *arr = getAsArrayTypeUnsafe()) { |
| 5439 | if (isa<VariableArrayType>(Val: arr) && |
| 5440 | cast<VariableArrayType>(Val: arr)->getSizeExpr()) |
| 5441 | return true; |
| 5442 | |
| 5443 | return arr->getElementType()->hasSizedVLAType(); |
| 5444 | } |
| 5445 | |
| 5446 | return false; |
| 5447 | } |
| 5448 | |
| 5449 | bool Type::isHLSLResourceRecord() const { |
| 5450 | return HLSLAttributedResourceType::findHandleTypeOnResource(RT: this) != nullptr; |
| 5451 | } |
| 5452 | |
| 5453 | bool Type::isHLSLResourceRecordArray() const { |
| 5454 | const Type *Ty = getUnqualifiedDesugaredType(); |
| 5455 | if (!Ty->isArrayType()) |
| 5456 | return false; |
| 5457 | while (isa<ArrayType>(Val: Ty)) |
| 5458 | Ty = Ty->getArrayElementTypeNoTypeQual(); |
| 5459 | return Ty->isHLSLResourceRecord(); |
| 5460 | } |
| 5461 | |
| 5462 | bool Type::isHLSLIntangibleType() const { |
| 5463 | const Type *Ty = getUnqualifiedDesugaredType(); |
| 5464 | |
| 5465 | // check if it's a builtin type first |
| 5466 | if (Ty->isBuiltinType()) |
| 5467 | return Ty->isHLSLBuiltinIntangibleType(); |
| 5468 | |
| 5469 | // unwrap arrays |
| 5470 | while (isa<ArrayType>(Val: Ty)) |
| 5471 | Ty = Ty->getArrayElementTypeNoTypeQual(); |
| 5472 | |
| 5473 | const RecordType *RT = |
| 5474 | dyn_cast<RecordType>(Val: Ty->getUnqualifiedDesugaredType()); |
| 5475 | if (!RT) |
| 5476 | return false; |
| 5477 | |
| 5478 | CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
| 5479 | assert(RD != nullptr && |
| 5480 | "all HLSL structs and classes should be CXXRecordDecl" ); |
| 5481 | assert(RD->isCompleteDefinition() && "expecting complete type" ); |
| 5482 | return RD->isHLSLIntangible(); |
| 5483 | } |
| 5484 | |
| 5485 | QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) { |
| 5486 | switch (type.getObjCLifetime()) { |
| 5487 | case Qualifiers::OCL_None: |
| 5488 | case Qualifiers::OCL_ExplicitNone: |
| 5489 | case Qualifiers::OCL_Autoreleasing: |
| 5490 | break; |
| 5491 | |
| 5492 | case Qualifiers::OCL_Strong: |
| 5493 | return DK_objc_strong_lifetime; |
| 5494 | case Qualifiers::OCL_Weak: |
| 5495 | return DK_objc_weak_lifetime; |
| 5496 | } |
| 5497 | |
| 5498 | if (const auto *RD = type->getBaseElementTypeUnsafe()->getAsRecordDecl()) { |
| 5499 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
| 5500 | /// Check if this is a C++ object with a non-trivial destructor. |
| 5501 | if (CXXRD->hasDefinition() && !CXXRD->hasTrivialDestructor()) |
| 5502 | return DK_cxx_destructor; |
| 5503 | } else { |
| 5504 | /// Check if this is a C struct that is non-trivial to destroy or an array |
| 5505 | /// that contains such a struct. |
| 5506 | if (RD->isNonTrivialToPrimitiveDestroy()) |
| 5507 | return DK_nontrivial_c_struct; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | return DK_none; |
| 5512 | } |
| 5513 | |
| 5514 | bool MemberPointerType::isSugared() const { |
| 5515 | CXXRecordDecl *D1 = getMostRecentCXXRecordDecl(), |
| 5516 | *D2 = getQualifier().getAsRecordDecl(); |
| 5517 | assert(!D1 == !D2); |
| 5518 | return D1 != D2 && D1->getCanonicalDecl() != D2->getCanonicalDecl(); |
| 5519 | } |
| 5520 | |
| 5521 | void MemberPointerType::Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
| 5522 | const NestedNameSpecifier Qualifier, |
| 5523 | const CXXRecordDecl *Cls) { |
| 5524 | ID.AddPointer(Ptr: Pointee.getAsOpaquePtr()); |
| 5525 | Qualifier.Profile(ID); |
| 5526 | if (Cls) |
| 5527 | ID.AddPointer(Ptr: Cls->getCanonicalDecl()); |
| 5528 | } |
| 5529 | |
| 5530 | CXXRecordDecl *MemberPointerType::getCXXRecordDecl() const { |
| 5531 | return dyn_cast<MemberPointerType>(Val: getCanonicalTypeInternal()) |
| 5532 | ->getQualifier() |
| 5533 | .getAsRecordDecl(); |
| 5534 | } |
| 5535 | |
| 5536 | CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const { |
| 5537 | auto *RD = getCXXRecordDecl(); |
| 5538 | if (!RD) |
| 5539 | return nullptr; |
| 5540 | return RD->getMostRecentDecl(); |
| 5541 | } |
| 5542 | |
| 5543 | void clang::FixedPointValueToString(SmallVectorImpl<char> &Str, |
| 5544 | llvm::APSInt Val, unsigned Scale) { |
| 5545 | llvm::FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(), |
| 5546 | /*IsSaturated=*/false, |
| 5547 | /*HasUnsignedPadding=*/false); |
| 5548 | llvm::APFixedPoint(Val, FXSema).toString(Str); |
| 5549 | } |
| 5550 | |
| 5551 | DeducedType::DeducedType(TypeClass TC, DeducedKind DK, |
| 5552 | QualType DeducedAsTypeOrCanon) |
| 5553 | : Type(TC, /*canon=*/DK == DeducedKind::Deduced |
| 5554 | ? DeducedAsTypeOrCanon.getCanonicalType() |
| 5555 | : DeducedAsTypeOrCanon, |
| 5556 | TypeDependence::None) { |
| 5557 | DeducedTypeBits.Kind = llvm::to_underlying(E: DK); |
| 5558 | switch (DK) { |
| 5559 | case DeducedKind::Undeduced: |
| 5560 | break; |
| 5561 | case DeducedKind::Deduced: |
| 5562 | assert(!DeducedAsTypeOrCanon.isNull() && "Deduced type cannot be null" ); |
| 5563 | addDependence(D: DeducedAsTypeOrCanon->getDependence() & |
| 5564 | ~TypeDependence::VariablyModified); |
| 5565 | DeducedAsType = DeducedAsTypeOrCanon; |
| 5566 | break; |
| 5567 | case DeducedKind::DeducedAsPack: |
| 5568 | addDependence(D: TypeDependence::UnexpandedPack); |
| 5569 | [[fallthrough]]; |
| 5570 | case DeducedKind::DeducedAsDependent: |
| 5571 | addDependence(D: TypeDependence::DependentInstantiation); |
| 5572 | break; |
| 5573 | } |
| 5574 | assert(getDeducedKind() == DK && "DeducedKind does not match the type state" ); |
| 5575 | } |
| 5576 | |
| 5577 | AutoType::AutoType(DeducedKind DK, QualType DeducedAsTypeOrCanon, |
| 5578 | AutoTypeKeyword Keyword, TemplateDecl *TypeConstraintConcept, |
| 5579 | ArrayRef<TemplateArgument> TypeConstraintArgs) |
| 5580 | : DeducedType(Auto, DK, DeducedAsTypeOrCanon) { |
| 5581 | AutoTypeBits.Keyword = llvm::to_underlying(E: Keyword); |
| 5582 | AutoTypeBits.NumArgs = TypeConstraintArgs.size(); |
| 5583 | this->TypeConstraintConcept = TypeConstraintConcept; |
| 5584 | assert(TypeConstraintConcept || AutoTypeBits.NumArgs == 0); |
| 5585 | if (TypeConstraintConcept) { |
| 5586 | auto Dep = TypeDependence::None; |
| 5587 | if (const auto *TTP = |
| 5588 | dyn_cast<TemplateTemplateParmDecl>(Val: TypeConstraintConcept)) |
| 5589 | Dep = TypeDependence::DependentInstantiation | |
| 5590 | (TTP->isParameterPack() ? TypeDependence::UnexpandedPack |
| 5591 | : TypeDependence::None); |
| 5592 | |
| 5593 | auto *ArgBuffer = |
| 5594 | const_cast<TemplateArgument *>(getTypeConstraintArguments().data()); |
| 5595 | for (const TemplateArgument &Arg : TypeConstraintArgs) { |
| 5596 | Dep |= toTypeDependence(D: Arg.getDependence()); |
| 5597 | new (ArgBuffer++) TemplateArgument(Arg); |
| 5598 | } |
| 5599 | // A deduced AutoType only syntactically depends on its constraints. |
| 5600 | if (DK == DeducedKind::Deduced) |
| 5601 | Dep = toSyntacticDependence(D: Dep); |
| 5602 | addDependence(D: Dep); |
| 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | void AutoType::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
| 5607 | DeducedKind DK, QualType Deduced, |
| 5608 | AutoTypeKeyword Keyword, TemplateDecl *CD, |
| 5609 | ArrayRef<TemplateArgument> Arguments) { |
| 5610 | DeducedType::Profile(ID, DK, Deduced); |
| 5611 | ID.AddInteger(I: llvm::to_underlying(E: Keyword)); |
| 5612 | ID.AddPointer(Ptr: CD); |
| 5613 | for (const TemplateArgument &Arg : Arguments) |
| 5614 | Arg.Profile(ID, Context); |
| 5615 | } |
| 5616 | |
| 5617 | void AutoType::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
| 5618 | Profile(ID, Context, DK: getDeducedKind(), Deduced: getDeducedType(), Keyword: getKeyword(), |
| 5619 | CD: getTypeConstraintConcept(), Arguments: getTypeConstraintArguments()); |
| 5620 | } |
| 5621 | |
| 5622 | FunctionEffect::Kind FunctionEffect::oppositeKind() const { |
| 5623 | switch (kind()) { |
| 5624 | case Kind::NonBlocking: |
| 5625 | return Kind::Blocking; |
| 5626 | case Kind::Blocking: |
| 5627 | return Kind::NonBlocking; |
| 5628 | case Kind::NonAllocating: |
| 5629 | return Kind::Allocating; |
| 5630 | case Kind::Allocating: |
| 5631 | return Kind::NonAllocating; |
| 5632 | } |
| 5633 | llvm_unreachable("unknown effect kind" ); |
| 5634 | } |
| 5635 | |
| 5636 | StringRef FunctionEffect::name() const { |
| 5637 | switch (kind()) { |
| 5638 | case Kind::NonBlocking: |
| 5639 | return "nonblocking" ; |
| 5640 | case Kind::NonAllocating: |
| 5641 | return "nonallocating" ; |
| 5642 | case Kind::Blocking: |
| 5643 | return "blocking" ; |
| 5644 | case Kind::Allocating: |
| 5645 | return "allocating" ; |
| 5646 | } |
| 5647 | llvm_unreachable("unknown effect kind" ); |
| 5648 | } |
| 5649 | |
| 5650 | std::optional<FunctionEffect> FunctionEffect::effectProhibitingInference( |
| 5651 | const Decl &Callee, FunctionEffectKindSet CalleeFX) const { |
| 5652 | switch (kind()) { |
| 5653 | case Kind::NonAllocating: |
| 5654 | case Kind::NonBlocking: { |
| 5655 | for (FunctionEffect Effect : CalleeFX) { |
| 5656 | // nonblocking/nonallocating cannot call allocating. |
| 5657 | if (Effect.kind() == Kind::Allocating) |
| 5658 | return Effect; |
| 5659 | // nonblocking cannot call blocking. |
| 5660 | if (kind() == Kind::NonBlocking && Effect.kind() == Kind::Blocking) |
| 5661 | return Effect; |
| 5662 | } |
| 5663 | return std::nullopt; |
| 5664 | } |
| 5665 | |
| 5666 | case Kind::Allocating: |
| 5667 | case Kind::Blocking: |
| 5668 | assert(0 && "effectProhibitingInference with non-inferable effect kind" ); |
| 5669 | break; |
| 5670 | } |
| 5671 | llvm_unreachable("unknown effect kind" ); |
| 5672 | } |
| 5673 | |
| 5674 | bool FunctionEffect::shouldDiagnoseFunctionCall( |
| 5675 | bool Direct, FunctionEffectKindSet CalleeFX) const { |
| 5676 | switch (kind()) { |
| 5677 | case Kind::NonAllocating: |
| 5678 | case Kind::NonBlocking: { |
| 5679 | const Kind CallerKind = kind(); |
| 5680 | for (FunctionEffect Effect : CalleeFX) { |
| 5681 | const Kind EK = Effect.kind(); |
| 5682 | // Does callee have same or stronger constraint? |
| 5683 | if (EK == CallerKind || |
| 5684 | (CallerKind == Kind::NonAllocating && EK == Kind::NonBlocking)) { |
| 5685 | return false; // no diagnostic |
| 5686 | } |
| 5687 | } |
| 5688 | return true; // warning |
| 5689 | } |
| 5690 | case Kind::Allocating: |
| 5691 | case Kind::Blocking: |
| 5692 | return false; |
| 5693 | } |
| 5694 | llvm_unreachable("unknown effect kind" ); |
| 5695 | } |
| 5696 | |
| 5697 | // ===== |
| 5698 | |
| 5699 | bool FunctionEffectSet::insert(const FunctionEffectWithCondition &NewEC, |
| 5700 | Conflicts &Errs) { |
| 5701 | FunctionEffect::Kind NewOppositeKind = NewEC.Effect.oppositeKind(); |
| 5702 | Expr *NewCondition = NewEC.Cond.getCondition(); |
| 5703 | |
| 5704 | // The index at which insertion will take place; default is at end |
| 5705 | // but we might find an earlier insertion point. |
| 5706 | unsigned InsertIdx = Effects.size(); |
| 5707 | unsigned Idx = 0; |
| 5708 | for (const FunctionEffectWithCondition &EC : *this) { |
| 5709 | // Note about effects with conditions: They are considered distinct from |
| 5710 | // those without conditions; they are potentially unique, redundant, or |
| 5711 | // in conflict, but we can't tell which until the condition is evaluated. |
| 5712 | if (EC.Cond.getCondition() == nullptr && NewCondition == nullptr) { |
| 5713 | if (EC.Effect.kind() == NewEC.Effect.kind()) { |
| 5714 | // There is no condition, and the effect kind is already present, |
| 5715 | // so just fail to insert the new one (creating a duplicate), |
| 5716 | // and return success. |
| 5717 | return true; |
| 5718 | } |
| 5719 | |
| 5720 | if (EC.Effect.kind() == NewOppositeKind) { |
| 5721 | Errs.push_back(Elt: {.Kept: EC, .Rejected: NewEC}); |
| 5722 | return false; |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | if (NewEC.Effect.kind() < EC.Effect.kind() && InsertIdx > Idx) |
| 5727 | InsertIdx = Idx; |
| 5728 | |
| 5729 | ++Idx; |
| 5730 | } |
| 5731 | |
| 5732 | if (NewCondition || !Conditions.empty()) { |
| 5733 | if (Conditions.empty() && !Effects.empty()) |
| 5734 | Conditions.resize(N: Effects.size()); |
| 5735 | Conditions.insert(I: Conditions.begin() + InsertIdx, |
| 5736 | Elt: NewEC.Cond.getCondition()); |
| 5737 | } |
| 5738 | Effects.insert(I: Effects.begin() + InsertIdx, Elt: NewEC.Effect); |
| 5739 | return true; |
| 5740 | } |
| 5741 | |
| 5742 | bool FunctionEffectSet::insert(const FunctionEffectsRef &Set, Conflicts &Errs) { |
| 5743 | for (const auto &Item : Set) |
| 5744 | insert(NewEC: Item, Errs); |
| 5745 | return Errs.empty(); |
| 5746 | } |
| 5747 | |
| 5748 | FunctionEffectSet FunctionEffectSet::getIntersection(FunctionEffectsRef LHS, |
| 5749 | FunctionEffectsRef RHS) { |
| 5750 | FunctionEffectSet Result; |
| 5751 | FunctionEffectSet::Conflicts Errs; |
| 5752 | |
| 5753 | // We could use std::set_intersection but that would require expanding the |
| 5754 | // container interface to include push_back, making it available to clients |
| 5755 | // who might fail to maintain invariants. |
| 5756 | auto IterA = LHS.begin(), EndA = LHS.end(); |
| 5757 | auto IterB = RHS.begin(), EndB = RHS.end(); |
| 5758 | |
| 5759 | auto FEWCLess = [](const FunctionEffectWithCondition &LHS, |
| 5760 | const FunctionEffectWithCondition &RHS) { |
| 5761 | return std::tuple(LHS.Effect, uintptr_t(LHS.Cond.getCondition())) < |
| 5762 | std::tuple(RHS.Effect, uintptr_t(RHS.Cond.getCondition())); |
| 5763 | }; |
| 5764 | |
| 5765 | while (IterA != EndA && IterB != EndB) { |
| 5766 | FunctionEffectWithCondition A = *IterA; |
| 5767 | FunctionEffectWithCondition B = *IterB; |
| 5768 | if (FEWCLess(A, B)) |
| 5769 | ++IterA; |
| 5770 | else if (FEWCLess(B, A)) |
| 5771 | ++IterB; |
| 5772 | else { |
| 5773 | Result.insert(NewEC: A, Errs); |
| 5774 | ++IterA; |
| 5775 | ++IterB; |
| 5776 | } |
| 5777 | } |
| 5778 | |
| 5779 | // Insertion shouldn't be able to fail; that would mean both input |
| 5780 | // sets contained conflicts. |
| 5781 | assert(Errs.empty() && "conflict shouldn't be possible in getIntersection" ); |
| 5782 | |
| 5783 | return Result; |
| 5784 | } |
| 5785 | |
| 5786 | FunctionEffectSet FunctionEffectSet::getUnion(FunctionEffectsRef LHS, |
| 5787 | FunctionEffectsRef RHS, |
| 5788 | Conflicts &Errs) { |
| 5789 | // Optimize for either of the two sets being empty (very common). |
| 5790 | if (LHS.empty()) |
| 5791 | return FunctionEffectSet(RHS); |
| 5792 | |
| 5793 | FunctionEffectSet Combined(LHS); |
| 5794 | Combined.insert(Set: RHS, Errs); |
| 5795 | return Combined; |
| 5796 | } |
| 5797 | |
| 5798 | namespace clang { |
| 5799 | |
| 5800 | raw_ostream &operator<<(raw_ostream &OS, |
| 5801 | const FunctionEffectWithCondition &CFE) { |
| 5802 | OS << CFE.Effect.name(); |
| 5803 | if (Expr *E = CFE.Cond.getCondition()) { |
| 5804 | OS << '('; |
| 5805 | E->dump(); |
| 5806 | OS << ')'; |
| 5807 | } |
| 5808 | return OS; |
| 5809 | } |
| 5810 | |
| 5811 | } // namespace clang |
| 5812 | |
| 5813 | LLVM_DUMP_METHOD void FunctionEffectsRef::dump(llvm::raw_ostream &OS) const { |
| 5814 | OS << "Effects{" ; |
| 5815 | llvm::interleaveComma(c: *this, os&: OS); |
| 5816 | OS << "}" ; |
| 5817 | } |
| 5818 | |
| 5819 | LLVM_DUMP_METHOD void FunctionEffectSet::dump(llvm::raw_ostream &OS) const { |
| 5820 | FunctionEffectsRef(*this).dump(OS); |
| 5821 | } |
| 5822 | |
| 5823 | LLVM_DUMP_METHOD void FunctionEffectKindSet::dump(llvm::raw_ostream &OS) const { |
| 5824 | OS << "Effects{" ; |
| 5825 | llvm::interleaveComma(c: *this, os&: OS); |
| 5826 | OS << "}" ; |
| 5827 | } |
| 5828 | |
| 5829 | FunctionEffectsRef |
| 5830 | FunctionEffectsRef::create(ArrayRef<FunctionEffect> FX, |
| 5831 | ArrayRef<EffectConditionExpr> Conds) { |
| 5832 | assert(llvm::is_sorted(FX) && "effects should be sorted" ); |
| 5833 | assert((Conds.empty() || Conds.size() == FX.size()) && |
| 5834 | "effects size should match conditions size" ); |
| 5835 | return FunctionEffectsRef(FX, Conds); |
| 5836 | } |
| 5837 | |
| 5838 | std::string FunctionEffectWithCondition::description() const { |
| 5839 | std::string Result(Effect.name().str()); |
| 5840 | if (Cond.getCondition() != nullptr) |
| 5841 | Result += "(expr)" ; |
| 5842 | return Result; |
| 5843 | } |
| 5844 | |
| 5845 | const HLSLAttributedResourceType * |
| 5846 | HLSLAttributedResourceType::findHandleTypeOnResource(const Type *RT) { |
| 5847 | // If the type RT is an HLSL resource class, the first field must |
| 5848 | // be the resource handle of type HLSLAttributedResourceType |
| 5849 | const clang::Type *Ty = RT->getUnqualifiedDesugaredType(); |
| 5850 | if (const RecordDecl *RD = Ty->getAsCXXRecordDecl()) { |
| 5851 | if (!RD->fields().empty()) { |
| 5852 | const auto &FirstFD = RD->fields().begin(); |
| 5853 | return dyn_cast<HLSLAttributedResourceType>( |
| 5854 | Val: FirstFD->getType().getTypePtr()); |
| 5855 | } |
| 5856 | } |
| 5857 | return nullptr; |
| 5858 | } |
| 5859 | |
| 5860 | StringRef PredefinedSugarType::getName(Kind KD) { |
| 5861 | switch (KD) { |
| 5862 | case Kind::SizeT: |
| 5863 | return "__size_t" ; |
| 5864 | case Kind::SignedSizeT: |
| 5865 | return "__signed_size_t" ; |
| 5866 | case Kind::PtrdiffT: |
| 5867 | return "__ptrdiff_t" ; |
| 5868 | } |
| 5869 | llvm_unreachable("unexpected kind" ); |
| 5870 | } |
| 5871 | |