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