xref: /freebsd/contrib/llvm-project/clang/include/clang/AST/Type.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- Type.h - C Language Family Type Representation -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// C Language Family Type Representation
11 ///
12 /// This file defines the clang::Type interface and subclasses, used to
13 /// represent types for languages in the C family.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CLANG_AST_TYPE_H
18 #define LLVM_CLANG_AST_TYPE_H
19 
20 #include "clang/AST/DependenceFlags.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/TemplateName.h"
23 #include "clang/Basic/AddressSpaces.h"
24 #include "clang/Basic/AttrKinds.h"
25 #include "clang/Basic/Diagnostic.h"
26 #include "clang/Basic/ExceptionSpecificationType.h"
27 #include "clang/Basic/LLVM.h"
28 #include "clang/Basic/LangOptions.h"
29 #include "clang/Basic/Linkage.h"
30 #include "clang/Basic/PartialDiagnostic.h"
31 #include "clang/Basic/PointerAuthOptions.h"
32 #include "clang/Basic/SourceLocation.h"
33 #include "clang/Basic/Specifiers.h"
34 #include "clang/Basic/Visibility.h"
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/ADT/APSInt.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/FoldingSet.h"
39 #include "llvm/ADT/PointerIntPair.h"
40 #include "llvm/ADT/PointerUnion.h"
41 #include "llvm/ADT/STLForwardCompat.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/ADT/Twine.h"
44 #include "llvm/ADT/iterator_range.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/DXILABI.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/PointerLikeTypeTraits.h"
50 #include "llvm/Support/TrailingObjects.h"
51 #include "llvm/Support/type_traits.h"
52 #include <bitset>
53 #include <cassert>
54 #include <cstddef>
55 #include <cstdint>
56 #include <cstring>
57 #include <optional>
58 #include <string>
59 #include <type_traits>
60 #include <utility>
61 
62 namespace clang {
63 
64 class BTFTypeTagAttr;
65 class ExtQuals;
66 class QualType;
67 class ConceptDecl;
68 class ValueDecl;
69 class TagDecl;
70 class TemplateParameterList;
71 class Type;
72 class Attr;
73 
74 enum {
75   TypeAlignmentInBits = 4,
76   TypeAlignment = 1 << TypeAlignmentInBits
77 };
78 
79 namespace serialization {
80   template <class T> class AbstractTypeReader;
81   template <class T> class AbstractTypeWriter;
82 }
83 
84 } // namespace clang
85 
86 namespace llvm {
87 
88   template <typename T>
89   struct PointerLikeTypeTraits;
90   template<>
91   struct PointerLikeTypeTraits< ::clang::Type*> {
92     static inline void *getAsVoidPointer(::clang::Type *P) { return P; }
93 
94     static inline ::clang::Type *getFromVoidPointer(void *P) {
95       return static_cast< ::clang::Type*>(P);
96     }
97 
98     static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits;
99   };
100 
101   template<>
102   struct PointerLikeTypeTraits< ::clang::ExtQuals*> {
103     static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; }
104 
105     static inline ::clang::ExtQuals *getFromVoidPointer(void *P) {
106       return static_cast< ::clang::ExtQuals*>(P);
107     }
108 
109     static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits;
110   };
111 
112 } // namespace llvm
113 
114 namespace clang {
115 
116 class ASTContext;
117 template <typename> class CanQual;
118 class CXXRecordDecl;
119 class DeclContext;
120 class EnumDecl;
121 class Expr;
122 class ExtQualsTypeCommonBase;
123 class FunctionDecl;
124 class FunctionEffectsRef;
125 class FunctionEffectKindSet;
126 class FunctionEffectSet;
127 class IdentifierInfo;
128 class NamedDecl;
129 class ObjCInterfaceDecl;
130 class ObjCProtocolDecl;
131 class ObjCTypeParamDecl;
132 struct PrintingPolicy;
133 class RecordDecl;
134 class Stmt;
135 class TagDecl;
136 class TemplateArgument;
137 class TemplateArgumentListInfo;
138 class TemplateArgumentLoc;
139 class TemplateTypeParmDecl;
140 class TypedefNameDecl;
141 class UnresolvedUsingTypenameDecl;
142 class UsingShadowDecl;
143 
144 using CanQualType = CanQual<Type>;
145 
146 // Provide forward declarations for all of the *Type classes.
147 #define TYPE(Class, Base) class Class##Type;
148 #include "clang/AST/TypeNodes.inc"
149 
150 /// Pointer-authentication qualifiers.
151 class PointerAuthQualifier {
152   enum : uint32_t {
153     EnabledShift = 0,
154     EnabledBits = 1,
155     EnabledMask = 1 << EnabledShift,
156     AddressDiscriminatedShift = EnabledShift + EnabledBits,
157     AddressDiscriminatedBits = 1,
158     AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
159     AuthenticationModeShift =
160         AddressDiscriminatedShift + AddressDiscriminatedBits,
161     AuthenticationModeBits = 2,
162     AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1)
163                              << AuthenticationModeShift,
164     IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits,
165     IsaPointerBits = 1,
166     IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift,
167     AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits,
168     AuthenticatesNullValuesBits = 1,
169     AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1)
170                                   << AuthenticatesNullValuesShift,
171     KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits,
172     KeyBits = 10,
173     KeyMask = ((1 << KeyBits) - 1) << KeyShift,
174     DiscriminatorShift = KeyShift + KeyBits,
175     DiscriminatorBits = 16,
176     DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift,
177   };
178 
179   // bits:     |0      |1      |2..3              |4          |
180   //           |Enabled|Address|AuthenticationMode|ISA pointer|
181   // bits:     |5                |6..15|   16...31   |
182   //           |AuthenticatesNull|Key  |Discriminator|
183   uint32_t Data = 0;
184 
185   // The following static assertions check that each of the 32 bits is present
186   // exactly in one of the constants.
187   static_assert((EnabledBits + AddressDiscriminatedBits +
188                  AuthenticationModeBits + IsaPointerBits +
189                  AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) ==
190                     32,
191                 "PointerAuthQualifier should be exactly 32 bits");
192   static_assert((EnabledMask + AddressDiscriminatedMask +
193                  AuthenticationModeMask + IsaPointerMask +
194                  AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) ==
195                     0xFFFFFFFF,
196                 "All masks should cover the entire bits");
197   static_assert((EnabledMask ^ AddressDiscriminatedMask ^
198                  AuthenticationModeMask ^ IsaPointerMask ^
199                  AuthenticatesNullValuesMask ^ KeyMask ^ DiscriminatorMask) ==
200                     0xFFFFFFFF,
201                 "All masks should cover the entire bits");
202 
203   PointerAuthQualifier(unsigned Key, bool IsAddressDiscriminated,
204                        unsigned ExtraDiscriminator,
205                        PointerAuthenticationMode AuthenticationMode,
206                        bool IsIsaPointer, bool AuthenticatesNullValues)
207       : Data(EnabledMask |
208              (IsAddressDiscriminated
209                   ? llvm::to_underlying(AddressDiscriminatedMask)
210                   : 0) |
211              (Key << KeyShift) |
212              (llvm::to_underlying(AuthenticationMode)
213               << AuthenticationModeShift) |
214              (ExtraDiscriminator << DiscriminatorShift) |
215              (IsIsaPointer << IsaPointerShift) |
216              (AuthenticatesNullValues << AuthenticatesNullValuesShift)) {
217     assert(Key <= KeyNoneInternal);
218     assert(ExtraDiscriminator <= MaxDiscriminator);
219     assert((Data == 0) ==
220            (getAuthenticationMode() == PointerAuthenticationMode::None));
221   }
222 
223 public:
224   enum {
225     KeyNoneInternal = (1u << KeyBits) - 1,
226 
227     /// The maximum supported pointer-authentication key.
228     MaxKey = KeyNoneInternal - 1,
229 
230     /// The maximum supported pointer-authentication discriminator.
231     MaxDiscriminator = (1u << DiscriminatorBits) - 1
232   };
233 
234 public:
235   PointerAuthQualifier() = default;
236 
237   static PointerAuthQualifier
238   Create(unsigned Key, bool IsAddressDiscriminated, unsigned ExtraDiscriminator,
239          PointerAuthenticationMode AuthenticationMode, bool IsIsaPointer,
240          bool AuthenticatesNullValues) {
241     if (Key == PointerAuthKeyNone)
242       Key = KeyNoneInternal;
243     assert(Key <= KeyNoneInternal && "out-of-range key value");
244     return PointerAuthQualifier(Key, IsAddressDiscriminated, ExtraDiscriminator,
245                                 AuthenticationMode, IsIsaPointer,
246                                 AuthenticatesNullValues);
247   }
248 
249   bool isPresent() const {
250     assert((Data == 0) ==
251            (getAuthenticationMode() == PointerAuthenticationMode::None));
252     return Data != 0;
253   }
254 
255   explicit operator bool() const { return isPresent(); }
256 
257   unsigned getKey() const {
258     assert(isPresent());
259     return (Data & KeyMask) >> KeyShift;
260   }
261 
262   bool hasKeyNone() const { return isPresent() && getKey() == KeyNoneInternal; }
263 
264   bool isAddressDiscriminated() const {
265     assert(isPresent());
266     return (Data & AddressDiscriminatedMask) >> AddressDiscriminatedShift;
267   }
268 
269   unsigned getExtraDiscriminator() const {
270     assert(isPresent());
271     return (Data >> DiscriminatorShift);
272   }
273 
274   PointerAuthenticationMode getAuthenticationMode() const {
275     return PointerAuthenticationMode((Data & AuthenticationModeMask) >>
276                                      AuthenticationModeShift);
277   }
278 
279   bool isIsaPointer() const {
280     assert(isPresent());
281     return (Data & IsaPointerMask) >> IsaPointerShift;
282   }
283 
284   bool authenticatesNullValues() const {
285     assert(isPresent());
286     return (Data & AuthenticatesNullValuesMask) >> AuthenticatesNullValuesShift;
287   }
288 
289   PointerAuthQualifier withoutKeyNone() const {
290     return hasKeyNone() ? PointerAuthQualifier() : *this;
291   }
292 
293   friend bool operator==(PointerAuthQualifier Lhs, PointerAuthQualifier Rhs) {
294     return Lhs.Data == Rhs.Data;
295   }
296   friend bool operator!=(PointerAuthQualifier Lhs, PointerAuthQualifier Rhs) {
297     return Lhs.Data != Rhs.Data;
298   }
299 
300   bool isEquivalent(PointerAuthQualifier Other) const {
301     return withoutKeyNone() == Other.withoutKeyNone();
302   }
303 
304   uint32_t getAsOpaqueValue() const { return Data; }
305 
306   // Deserialize pointer-auth qualifiers from an opaque representation.
307   static PointerAuthQualifier fromOpaqueValue(uint32_t Opaque) {
308     PointerAuthQualifier Result;
309     Result.Data = Opaque;
310     assert((Result.Data == 0) ==
311            (Result.getAuthenticationMode() == PointerAuthenticationMode::None));
312     return Result;
313   }
314 
315   std::string getAsString() const;
316   std::string getAsString(const PrintingPolicy &Policy) const;
317 
318   bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const;
319   void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
320 
321   void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(Data); }
322 };
323 
324 /// The collection of all-type qualifiers we support.
325 /// Clang supports five independent qualifiers:
326 /// * C99: const, volatile, and restrict
327 /// * MS: __unaligned
328 /// * Embedded C (TR18037): address spaces
329 /// * Objective C: the GC attributes (none, weak, or strong)
330 class Qualifiers {
331 public:
332   Qualifiers() = default;
333   enum TQ : uint64_t {
334     // NOTE: These flags must be kept in sync with DeclSpec::TQ.
335     Const = 0x1,
336     Restrict = 0x2,
337     Volatile = 0x4,
338     CVRMask = Const | Volatile | Restrict
339   };
340 
341   enum GC {
342     GCNone = 0,
343     Weak,
344     Strong
345   };
346 
347   enum ObjCLifetime {
348     /// There is no lifetime qualification on this type.
349     OCL_None,
350 
351     /// This object can be modified without requiring retains or
352     /// releases.
353     OCL_ExplicitNone,
354 
355     /// Assigning into this object requires the old value to be
356     /// released and the new value to be retained.  The timing of the
357     /// release of the old value is inexact: it may be moved to
358     /// immediately after the last known point where the value is
359     /// live.
360     OCL_Strong,
361 
362     /// Reading or writing from this object requires a barrier call.
363     OCL_Weak,
364 
365     /// Assigning into this object requires a lifetime extension.
366     OCL_Autoreleasing
367   };
368 
369   enum : uint64_t {
370     /// The maximum supported address space number.
371     /// 23 bits should be enough for anyone.
372     MaxAddressSpace = 0x7fffffu,
373 
374     /// The width of the "fast" qualifier mask.
375     FastWidth = 3,
376 
377     /// The fast qualifier mask.
378     FastMask = (1 << FastWidth) - 1
379   };
380 
381   /// Returns the common set of qualifiers while removing them from
382   /// the given sets.
383   static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) {
384     Qualifiers Q;
385     PointerAuthQualifier LPtrAuth = L.getPointerAuth();
386     if (LPtrAuth.isPresent() &&
387         LPtrAuth.getKey() != PointerAuthQualifier::KeyNoneInternal &&
388         LPtrAuth == R.getPointerAuth()) {
389       Q.setPointerAuth(LPtrAuth);
390       PointerAuthQualifier Empty;
391       L.setPointerAuth(Empty);
392       R.setPointerAuth(Empty);
393     }
394 
395     // If both are only CVR-qualified, bit operations are sufficient.
396     if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) {
397       Q.Mask = L.Mask & R.Mask;
398       L.Mask &= ~Q.Mask;
399       R.Mask &= ~Q.Mask;
400       return Q;
401     }
402 
403     unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers();
404     Q.addCVRQualifiers(CommonCRV);
405     L.removeCVRQualifiers(CommonCRV);
406     R.removeCVRQualifiers(CommonCRV);
407 
408     if (L.getObjCGCAttr() == R.getObjCGCAttr()) {
409       Q.setObjCGCAttr(L.getObjCGCAttr());
410       L.removeObjCGCAttr();
411       R.removeObjCGCAttr();
412     }
413 
414     if (L.getObjCLifetime() == R.getObjCLifetime()) {
415       Q.setObjCLifetime(L.getObjCLifetime());
416       L.removeObjCLifetime();
417       R.removeObjCLifetime();
418     }
419 
420     if (L.getAddressSpace() == R.getAddressSpace()) {
421       Q.setAddressSpace(L.getAddressSpace());
422       L.removeAddressSpace();
423       R.removeAddressSpace();
424     }
425     return Q;
426   }
427 
428   static Qualifiers fromFastMask(unsigned Mask) {
429     Qualifiers Qs;
430     Qs.addFastQualifiers(Mask);
431     return Qs;
432   }
433 
434   static Qualifiers fromCVRMask(unsigned CVR) {
435     Qualifiers Qs;
436     Qs.addCVRQualifiers(CVR);
437     return Qs;
438   }
439 
440   static Qualifiers fromCVRUMask(unsigned CVRU) {
441     Qualifiers Qs;
442     Qs.addCVRUQualifiers(CVRU);
443     return Qs;
444   }
445 
446   // Deserialize qualifiers from an opaque representation.
447   static Qualifiers fromOpaqueValue(uint64_t opaque) {
448     Qualifiers Qs;
449     Qs.Mask = opaque;
450     return Qs;
451   }
452 
453   // Serialize these qualifiers into an opaque representation.
454   uint64_t getAsOpaqueValue() const { return Mask; }
455 
456   bool hasConst() const { return Mask & Const; }
457   bool hasOnlyConst() const { return Mask == Const; }
458   void removeConst() { Mask &= ~Const; }
459   void addConst() { Mask |= Const; }
460   Qualifiers withConst() const {
461     Qualifiers Qs = *this;
462     Qs.addConst();
463     return Qs;
464   }
465 
466   bool hasVolatile() const { return Mask & Volatile; }
467   bool hasOnlyVolatile() const { return Mask == Volatile; }
468   void removeVolatile() { Mask &= ~Volatile; }
469   void addVolatile() { Mask |= Volatile; }
470   Qualifiers withVolatile() const {
471     Qualifiers Qs = *this;
472     Qs.addVolatile();
473     return Qs;
474   }
475 
476   bool hasRestrict() const { return Mask & Restrict; }
477   bool hasOnlyRestrict() const { return Mask == Restrict; }
478   void removeRestrict() { Mask &= ~Restrict; }
479   void addRestrict() { Mask |= Restrict; }
480   Qualifiers withRestrict() const {
481     Qualifiers Qs = *this;
482     Qs.addRestrict();
483     return Qs;
484   }
485 
486   bool hasCVRQualifiers() const { return getCVRQualifiers(); }
487   unsigned getCVRQualifiers() const { return Mask & CVRMask; }
488   unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); }
489 
490   void setCVRQualifiers(unsigned mask) {
491     assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
492     Mask = (Mask & ~CVRMask) | mask;
493   }
494   void removeCVRQualifiers(unsigned mask) {
495     assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
496     Mask &= ~static_cast<uint64_t>(mask);
497   }
498   void removeCVRQualifiers() {
499     removeCVRQualifiers(CVRMask);
500   }
501   void addCVRQualifiers(unsigned mask) {
502     assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
503     Mask |= mask;
504   }
505   void addCVRUQualifiers(unsigned mask) {
506     assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits");
507     Mask |= mask;
508   }
509 
510   bool hasUnaligned() const { return Mask & UMask; }
511   void setUnaligned(bool flag) {
512     Mask = (Mask & ~UMask) | (flag ? UMask : 0);
513   }
514   void removeUnaligned() { Mask &= ~UMask; }
515   void addUnaligned() { Mask |= UMask; }
516 
517   bool hasObjCGCAttr() const { return Mask & GCAttrMask; }
518   GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); }
519   void setObjCGCAttr(GC type) {
520     Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift);
521   }
522   void removeObjCGCAttr() { setObjCGCAttr(GCNone); }
523   void addObjCGCAttr(GC type) {
524     assert(type);
525     setObjCGCAttr(type);
526   }
527   Qualifiers withoutObjCGCAttr() const {
528     Qualifiers qs = *this;
529     qs.removeObjCGCAttr();
530     return qs;
531   }
532   Qualifiers withoutObjCLifetime() const {
533     Qualifiers qs = *this;
534     qs.removeObjCLifetime();
535     return qs;
536   }
537   Qualifiers withoutAddressSpace() const {
538     Qualifiers qs = *this;
539     qs.removeAddressSpace();
540     return qs;
541   }
542 
543   bool hasObjCLifetime() const { return Mask & LifetimeMask; }
544   ObjCLifetime getObjCLifetime() const {
545     return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift);
546   }
547   void setObjCLifetime(ObjCLifetime type) {
548     Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift);
549   }
550   void removeObjCLifetime() { setObjCLifetime(OCL_None); }
551   void addObjCLifetime(ObjCLifetime type) {
552     assert(type);
553     assert(!hasObjCLifetime());
554     Mask |= (type << LifetimeShift);
555   }
556 
557   /// True if the lifetime is neither None or ExplicitNone.
558   bool hasNonTrivialObjCLifetime() const {
559     ObjCLifetime lifetime = getObjCLifetime();
560     return (lifetime > OCL_ExplicitNone);
561   }
562 
563   /// True if the lifetime is either strong or weak.
564   bool hasStrongOrWeakObjCLifetime() const {
565     ObjCLifetime lifetime = getObjCLifetime();
566     return (lifetime == OCL_Strong || lifetime == OCL_Weak);
567   }
568 
569   bool hasAddressSpace() const { return Mask & AddressSpaceMask; }
570   LangAS getAddressSpace() const {
571     return static_cast<LangAS>((Mask & AddressSpaceMask) >> AddressSpaceShift);
572   }
573   bool hasTargetSpecificAddressSpace() const {
574     return isTargetAddressSpace(getAddressSpace());
575   }
576   /// Get the address space attribute value to be printed by diagnostics.
577   unsigned getAddressSpaceAttributePrintValue() const {
578     auto Addr = getAddressSpace();
579     // This function is not supposed to be used with language specific
580     // address spaces. If that happens, the diagnostic message should consider
581     // printing the QualType instead of the address space value.
582     assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace());
583     if (Addr != LangAS::Default)
584       return toTargetAddressSpace(Addr);
585     // TODO: The diagnostic messages where Addr may be 0 should be fixed
586     // since it cannot differentiate the situation where 0 denotes the default
587     // address space or user specified __attribute__((address_space(0))).
588     return 0;
589   }
590   void setAddressSpace(LangAS space) {
591     assert((unsigned)space <= MaxAddressSpace);
592     Mask = (Mask & ~AddressSpaceMask)
593          | (((uint32_t) space) << AddressSpaceShift);
594   }
595   void removeAddressSpace() { setAddressSpace(LangAS::Default); }
596   void addAddressSpace(LangAS space) {
597     assert(space != LangAS::Default);
598     setAddressSpace(space);
599   }
600 
601   bool hasPointerAuth() const { return Mask & PtrAuthMask; }
602   PointerAuthQualifier getPointerAuth() const {
603     return PointerAuthQualifier::fromOpaqueValue(Mask >> PtrAuthShift);
604   }
605   void setPointerAuth(PointerAuthQualifier Q) {
606     Mask = (Mask & ~PtrAuthMask) |
607            (uint64_t(Q.getAsOpaqueValue()) << PtrAuthShift);
608   }
609   void removePointerAuth() { Mask &= ~PtrAuthMask; }
610   void addPointerAuth(PointerAuthQualifier Q) {
611     assert(Q.isPresent());
612     setPointerAuth(Q);
613   }
614 
615   // Fast qualifiers are those that can be allocated directly
616   // on a QualType object.
617   bool hasFastQualifiers() const { return getFastQualifiers(); }
618   unsigned getFastQualifiers() const { return Mask & FastMask; }
619   void setFastQualifiers(unsigned mask) {
620     assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
621     Mask = (Mask & ~FastMask) | mask;
622   }
623   void removeFastQualifiers(unsigned mask) {
624     assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
625     Mask &= ~static_cast<uint64_t>(mask);
626   }
627   void removeFastQualifiers() {
628     removeFastQualifiers(FastMask);
629   }
630   void addFastQualifiers(unsigned mask) {
631     assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
632     Mask |= mask;
633   }
634 
635   /// Return true if the set contains any qualifiers which require an ExtQuals
636   /// node to be allocated.
637   bool hasNonFastQualifiers() const { return Mask & ~FastMask; }
638   Qualifiers getNonFastQualifiers() const {
639     Qualifiers Quals = *this;
640     Quals.setFastQualifiers(0);
641     return Quals;
642   }
643 
644   /// Return true if the set contains any qualifiers.
645   bool hasQualifiers() const { return Mask; }
646   bool empty() const { return !Mask; }
647 
648   /// Add the qualifiers from the given set to this set.
649   void addQualifiers(Qualifiers Q) {
650     // If the other set doesn't have any non-boolean qualifiers, just
651     // bit-or it in.
652     if (!(Q.Mask & ~CVRMask))
653       Mask |= Q.Mask;
654     else {
655       Mask |= (Q.Mask & CVRMask);
656       if (Q.hasAddressSpace())
657         addAddressSpace(Q.getAddressSpace());
658       if (Q.hasObjCGCAttr())
659         addObjCGCAttr(Q.getObjCGCAttr());
660       if (Q.hasObjCLifetime())
661         addObjCLifetime(Q.getObjCLifetime());
662       if (Q.hasPointerAuth())
663         addPointerAuth(Q.getPointerAuth());
664     }
665   }
666 
667   /// Remove the qualifiers from the given set from this set.
668   void removeQualifiers(Qualifiers Q) {
669     // If the other set doesn't have any non-boolean qualifiers, just
670     // bit-and the inverse in.
671     if (!(Q.Mask & ~CVRMask))
672       Mask &= ~Q.Mask;
673     else {
674       Mask &= ~(Q.Mask & CVRMask);
675       if (getObjCGCAttr() == Q.getObjCGCAttr())
676         removeObjCGCAttr();
677       if (getObjCLifetime() == Q.getObjCLifetime())
678         removeObjCLifetime();
679       if (getAddressSpace() == Q.getAddressSpace())
680         removeAddressSpace();
681       if (getPointerAuth() == Q.getPointerAuth())
682         removePointerAuth();
683     }
684   }
685 
686   /// Add the qualifiers from the given set to this set, given that
687   /// they don't conflict.
688   void addConsistentQualifiers(Qualifiers qs) {
689     assert(getAddressSpace() == qs.getAddressSpace() ||
690            !hasAddressSpace() || !qs.hasAddressSpace());
691     assert(getObjCGCAttr() == qs.getObjCGCAttr() ||
692            !hasObjCGCAttr() || !qs.hasObjCGCAttr());
693     assert(getObjCLifetime() == qs.getObjCLifetime() ||
694            !hasObjCLifetime() || !qs.hasObjCLifetime());
695     assert(!hasPointerAuth() || !qs.hasPointerAuth() ||
696            getPointerAuth() == qs.getPointerAuth());
697     Mask |= qs.Mask;
698   }
699 
700   /// Returns true if address space A is equal to or a superset of B.
701   /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of
702   /// overlapping address spaces.
703   /// CL1.1 or CL1.2:
704   ///   every address space is a superset of itself.
705   /// CL2.0 adds:
706   ///   __generic is a superset of any address space except for __constant.
707   static bool isAddressSpaceSupersetOf(LangAS A, LangAS B,
708                                        const ASTContext &Ctx) {
709     // Address spaces must match exactly.
710     return A == B || isTargetAddressSpaceSupersetOf(A, B, Ctx);
711   }
712 
713   static bool isTargetAddressSpaceSupersetOf(LangAS A, LangAS B,
714                                              const ASTContext &Ctx);
715 
716   /// Returns true if the address space in these qualifiers is equal to or
717   /// a superset of the address space in the argument qualifiers.
718   bool isAddressSpaceSupersetOf(Qualifiers other, const ASTContext &Ctx) const {
719     return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace(),
720                                     Ctx);
721   }
722 
723   /// Determines if these qualifiers compatibly include another set.
724   /// Generally this answers the question of whether an object with the other
725   /// qualifiers can be safely used as an object with these qualifiers.
726   bool compatiblyIncludes(Qualifiers other, const ASTContext &Ctx) const {
727     return isAddressSpaceSupersetOf(other, Ctx) &&
728            // ObjC GC qualifiers can match, be added, or be removed, but can't
729            // be changed.
730            (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() ||
731             !other.hasObjCGCAttr()) &&
732            // Pointer-auth qualifiers must match exactly.
733            getPointerAuth() == other.getPointerAuth() &&
734            // ObjC lifetime qualifiers must match exactly.
735            getObjCLifetime() == other.getObjCLifetime() &&
736            // CVR qualifiers may subset.
737            (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) &&
738            // U qualifier may superset.
739            (!other.hasUnaligned() || hasUnaligned());
740   }
741 
742   /// Determines if these qualifiers compatibly include another set of
743   /// qualifiers from the narrow perspective of Objective-C ARC lifetime.
744   ///
745   /// One set of Objective-C lifetime qualifiers compatibly includes the other
746   /// if the lifetime qualifiers match, or if both are non-__weak and the
747   /// including set also contains the 'const' qualifier, or both are non-__weak
748   /// and one is None (which can only happen in non-ARC modes).
749   bool compatiblyIncludesObjCLifetime(Qualifiers other) const {
750     if (getObjCLifetime() == other.getObjCLifetime())
751       return true;
752 
753     if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak)
754       return false;
755 
756     if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None)
757       return true;
758 
759     return hasConst();
760   }
761 
762   /// Determine whether this set of qualifiers is a strict superset of
763   /// another set of qualifiers, not considering qualifier compatibility.
764   bool isStrictSupersetOf(Qualifiers Other) const;
765 
766   bool operator==(Qualifiers Other) const { return Mask == Other.Mask; }
767   bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; }
768 
769   explicit operator bool() const { return hasQualifiers(); }
770 
771   Qualifiers &operator+=(Qualifiers R) {
772     addQualifiers(R);
773     return *this;
774   }
775 
776   // Union two qualifier sets.  If an enumerated qualifier appears
777   // in both sets, use the one from the right.
778   friend Qualifiers operator+(Qualifiers L, Qualifiers R) {
779     L += R;
780     return L;
781   }
782 
783   Qualifiers &operator-=(Qualifiers R) {
784     removeQualifiers(R);
785     return *this;
786   }
787 
788   /// Compute the difference between two qualifier sets.
789   friend Qualifiers operator-(Qualifiers L, Qualifiers R) {
790     L -= R;
791     return L;
792   }
793 
794   std::string getAsString() const;
795   std::string getAsString(const PrintingPolicy &Policy) const;
796 
797   static std::string getAddrSpaceAsString(LangAS AS);
798 
799   bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const;
800   void print(raw_ostream &OS, const PrintingPolicy &Policy,
801              bool appendSpaceIfNonEmpty = false) const;
802 
803   void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(Mask); }
804 
805 private:
806   // bits:     |0 1 2|3|4 .. 5|6  ..  8|9   ...   31|32 ... 63|
807   //           |C R V|U|GCAttr|Lifetime|AddressSpace| PtrAuth |
808   uint64_t Mask = 0;
809   static_assert(sizeof(PointerAuthQualifier) == sizeof(uint32_t),
810                 "PointerAuthQualifier must be 32 bits");
811 
812   static constexpr uint64_t PtrAuthShift = 32;
813   static constexpr uint64_t PtrAuthMask = UINT64_C(0xffffffff) << PtrAuthShift;
814 
815   static constexpr uint64_t UMask = 0x8;
816   static constexpr uint64_t UShift = 3;
817   static constexpr uint64_t GCAttrMask = 0x30;
818   static constexpr uint64_t GCAttrShift = 4;
819   static constexpr uint64_t LifetimeMask = 0x1C0;
820   static constexpr uint64_t LifetimeShift = 6;
821   static constexpr uint64_t AddressSpaceMask =
822       ~(CVRMask | UMask | GCAttrMask | LifetimeMask | PtrAuthMask);
823   static constexpr uint64_t AddressSpaceShift = 9;
824 };
825 
826 class QualifiersAndAtomic {
827   Qualifiers Quals;
828   bool HasAtomic;
829 
830 public:
831   QualifiersAndAtomic() : HasAtomic(false) {}
832   QualifiersAndAtomic(Qualifiers Quals, bool HasAtomic)
833       : Quals(Quals), HasAtomic(HasAtomic) {}
834 
835   operator Qualifiers() const { return Quals; }
836 
837   bool hasVolatile() const { return Quals.hasVolatile(); }
838   bool hasConst() const { return Quals.hasConst(); }
839   bool hasRestrict() const { return Quals.hasRestrict(); }
840   bool hasAtomic() const { return HasAtomic; }
841 
842   void addVolatile() { Quals.addVolatile(); }
843   void addConst() { Quals.addConst(); }
844   void addRestrict() { Quals.addRestrict(); }
845   void addAtomic() { HasAtomic = true; }
846 
847   void removeVolatile() { Quals.removeVolatile(); }
848   void removeConst() { Quals.removeConst(); }
849   void removeRestrict() { Quals.removeRestrict(); }
850   void removeAtomic() { HasAtomic = false; }
851 
852   QualifiersAndAtomic withVolatile() {
853     return {Quals.withVolatile(), HasAtomic};
854   }
855   QualifiersAndAtomic withConst() { return {Quals.withConst(), HasAtomic}; }
856   QualifiersAndAtomic withRestrict() {
857     return {Quals.withRestrict(), HasAtomic};
858   }
859   QualifiersAndAtomic withAtomic() { return {Quals, true}; }
860 
861   QualifiersAndAtomic &operator+=(Qualifiers RHS) {
862     Quals += RHS;
863     return *this;
864   }
865 };
866 
867 /// A std::pair-like structure for storing a qualified type split
868 /// into its local qualifiers and its locally-unqualified type.
869 struct SplitQualType {
870   /// The locally-unqualified type.
871   const Type *Ty = nullptr;
872 
873   /// The local qualifiers.
874   Qualifiers Quals;
875 
876   SplitQualType() = default;
877   SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {}
878 
879   SplitQualType getSingleStepDesugaredType() const; // end of this file
880 
881   // Make std::tie work.
882   std::pair<const Type *,Qualifiers> asPair() const {
883     return std::pair<const Type *, Qualifiers>(Ty, Quals);
884   }
885 
886   friend bool operator==(SplitQualType a, SplitQualType b) {
887     return a.Ty == b.Ty && a.Quals == b.Quals;
888   }
889   friend bool operator!=(SplitQualType a, SplitQualType b) {
890     return a.Ty != b.Ty || a.Quals != b.Quals;
891   }
892 };
893 
894 /// The kind of type we are substituting Objective-C type arguments into.
895 ///
896 /// The kind of substitution affects the replacement of type parameters when
897 /// no concrete type information is provided, e.g., when dealing with an
898 /// unspecialized type.
899 enum class ObjCSubstitutionContext {
900   /// An ordinary type.
901   Ordinary,
902 
903   /// The result type of a method or function.
904   Result,
905 
906   /// The parameter type of a method or function.
907   Parameter,
908 
909   /// The type of a property.
910   Property,
911 
912   /// The superclass of a type.
913   Superclass,
914 };
915 
916 /// The kind of 'typeof' expression we're after.
917 enum class TypeOfKind : uint8_t {
918   Qualified,
919   Unqualified,
920 };
921 
922 /// A (possibly-)qualified type.
923 ///
924 /// For efficiency, we don't store CV-qualified types as nodes on their
925 /// own: instead each reference to a type stores the qualifiers.  This
926 /// greatly reduces the number of nodes we need to allocate for types (for
927 /// example we only need one for 'int', 'const int', 'volatile int',
928 /// 'const volatile int', etc).
929 ///
930 /// As an added efficiency bonus, instead of making this a pair, we
931 /// just store the two bits we care about in the low bits of the
932 /// pointer.  To handle the packing/unpacking, we make QualType be a
933 /// simple wrapper class that acts like a smart pointer.  A third bit
934 /// indicates whether there are extended qualifiers present, in which
935 /// case the pointer points to a special structure.
936 class QualType {
937   friend class QualifierCollector;
938 
939   // Thankfully, these are efficiently composable.
940   llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>,
941                        Qualifiers::FastWidth> Value;
942 
943   const ExtQuals *getExtQualsUnsafe() const {
944     return cast<const ExtQuals *>(Value.getPointer());
945   }
946 
947   const Type *getTypePtrUnsafe() const {
948     return cast<const Type *>(Value.getPointer());
949   }
950 
951   const ExtQualsTypeCommonBase *getCommonPtr() const {
952     assert(!isNull() && "Cannot retrieve a NULL type pointer");
953     auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue());
954     CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1);
955     return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal);
956   }
957 
958 public:
959   QualType() = default;
960   QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
961   QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
962 
963   unsigned getLocalFastQualifiers() const { return Value.getInt(); }
964   void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); }
965 
966   bool UseExcessPrecision(const ASTContext &Ctx);
967 
968   /// Retrieves a pointer to the underlying (unqualified) type.
969   ///
970   /// This function requires that the type not be NULL. If the type might be
971   /// NULL, use the (slightly less efficient) \c getTypePtrOrNull().
972   const Type *getTypePtr() const;
973 
974   const Type *getTypePtrOrNull() const;
975 
976   /// Retrieves a pointer to the name of the base type.
977   const IdentifierInfo *getBaseTypeIdentifier() const;
978 
979   /// Divides a QualType into its unqualified type and a set of local
980   /// qualifiers.
981   SplitQualType split() const;
982 
983   void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
984 
985   static QualType getFromOpaquePtr(const void *Ptr) {
986     QualType T;
987     T.Value.setFromOpaqueValue(const_cast<void*>(Ptr));
988     return T;
989   }
990 
991   const Type &operator*() const {
992     return *getTypePtr();
993   }
994 
995   const Type *operator->() const {
996     return getTypePtr();
997   }
998 
999   bool isCanonical() const;
1000   bool isCanonicalAsParam() const;
1001 
1002   /// Return true if this QualType doesn't point to a type yet.
1003   bool isNull() const {
1004     return Value.getPointer().isNull();
1005   }
1006 
1007   // Determines if a type can form `T&`.
1008   bool isReferenceable() const;
1009 
1010   /// Determine whether this particular QualType instance has the
1011   /// "const" qualifier set, without looking through typedefs that may have
1012   /// added "const" at a different level.
1013   bool isLocalConstQualified() const {
1014     return (getLocalFastQualifiers() & Qualifiers::Const);
1015   }
1016 
1017   /// Determine whether this type is const-qualified.
1018   bool isConstQualified() const;
1019 
1020   enum class NonConstantStorageReason {
1021     MutableField,
1022     NonConstNonReferenceType,
1023     NonTrivialCtor,
1024     NonTrivialDtor,
1025   };
1026   /// Determine whether instances of this type can be placed in immutable
1027   /// storage.
1028   /// If ExcludeCtor is true, the duration when the object's constructor runs
1029   /// will not be considered. The caller will need to verify that the object is
1030   /// not written to during its construction. ExcludeDtor works similarly.
1031   std::optional<NonConstantStorageReason>
1032   isNonConstantStorage(const ASTContext &Ctx, bool ExcludeCtor,
1033                        bool ExcludeDtor);
1034 
1035   bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor,
1036                          bool ExcludeDtor) {
1037     return !isNonConstantStorage(Ctx, ExcludeCtor, ExcludeDtor);
1038   }
1039 
1040   /// Determine whether this particular QualType instance has the
1041   /// "restrict" qualifier set, without looking through typedefs that may have
1042   /// added "restrict" at a different level.
1043   bool isLocalRestrictQualified() const {
1044     return (getLocalFastQualifiers() & Qualifiers::Restrict);
1045   }
1046 
1047   /// Determine whether this type is restrict-qualified.
1048   bool isRestrictQualified() const;
1049 
1050   /// Determine whether this particular QualType instance has the
1051   /// "volatile" qualifier set, without looking through typedefs that may have
1052   /// added "volatile" at a different level.
1053   bool isLocalVolatileQualified() const {
1054     return (getLocalFastQualifiers() & Qualifiers::Volatile);
1055   }
1056 
1057   /// Determine whether this type is volatile-qualified.
1058   bool isVolatileQualified() const;
1059 
1060   /// Determine whether this particular QualType instance has any
1061   /// qualifiers, without looking through any typedefs that might add
1062   /// qualifiers at a different level.
1063   bool hasLocalQualifiers() const {
1064     return getLocalFastQualifiers() || hasLocalNonFastQualifiers();
1065   }
1066 
1067   /// Determine whether this type has any qualifiers.
1068   bool hasQualifiers() const;
1069 
1070   /// Determine whether this particular QualType instance has any
1071   /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType
1072   /// instance.
1073   bool hasLocalNonFastQualifiers() const {
1074     return isa<const ExtQuals *>(Value.getPointer());
1075   }
1076 
1077   /// Retrieve the set of qualifiers local to this particular QualType
1078   /// instance, not including any qualifiers acquired through typedefs or
1079   /// other sugar.
1080   Qualifiers getLocalQualifiers() const;
1081 
1082   /// Retrieve the set of qualifiers applied to this type.
1083   Qualifiers getQualifiers() const;
1084 
1085   /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
1086   /// local to this particular QualType instance, not including any qualifiers
1087   /// acquired through typedefs or other sugar.
1088   unsigned getLocalCVRQualifiers() const {
1089     return getLocalFastQualifiers();
1090   }
1091 
1092   /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
1093   /// applied to this type.
1094   unsigned getCVRQualifiers() const;
1095 
1096   bool isConstant(const ASTContext& Ctx) const {
1097     return QualType::isConstant(*this, Ctx);
1098   }
1099 
1100   /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
1101   bool isPODType(const ASTContext &Context) const;
1102 
1103   /// Return true if this is a POD type according to the rules of the C++98
1104   /// standard, regardless of the current compilation's language.
1105   bool isCXX98PODType(const ASTContext &Context) const;
1106 
1107   /// Return true if this is a POD type according to the more relaxed rules
1108   /// of the C++11 standard, regardless of the current compilation's language.
1109   /// (C++0x [basic.types]p9). Note that, unlike
1110   /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
1111   bool isCXX11PODType(const ASTContext &Context) const;
1112 
1113   /// Return true if this is a trivial type per (C++0x [basic.types]p9)
1114   bool isTrivialType(const ASTContext &Context) const;
1115 
1116   /// Return true if this is a trivially copyable type (C++0x [basic.types]p9)
1117   bool isTriviallyCopyableType(const ASTContext &Context) const;
1118 
1119   /// Return true if the type is safe to bitwise copy using memcpy/memmove.
1120   ///
1121   /// This is an extension in clang: bitwise cloneable types act as trivially
1122   /// copyable types, meaning their underlying bytes can be safely copied by
1123   /// memcpy or memmove. After the copy, the destination object has the same
1124   /// object representation.
1125   ///
1126   /// However, there are cases where it is not safe to copy:
1127   ///  - When sanitizers, such as AddressSanitizer, add padding with poison,
1128   ///    which can cause issues if those poisoned padding bits are accessed.
1129   ///  - Types with Objective-C lifetimes, where specific runtime
1130   ///    semantics may not be preserved during a bitwise copy.
1131   bool isBitwiseCloneableType(const ASTContext &Context) const;
1132 
1133   /// Return true if this is a trivially copyable type
1134   bool isTriviallyCopyConstructibleType(const ASTContext &Context) const;
1135 
1136   /// Returns true if it is a class and it might be dynamic.
1137   bool mayBeDynamicClass() const;
1138 
1139   /// Returns true if it is not a class or if the class might not be dynamic.
1140   bool mayBeNotDynamicClass() const;
1141 
1142   /// Returns true if it is a WebAssembly Reference Type.
1143   bool isWebAssemblyReferenceType() const;
1144 
1145   /// Returns true if it is a WebAssembly Externref Type.
1146   bool isWebAssemblyExternrefType() const;
1147 
1148   /// Returns true if it is a WebAssembly Funcref Type.
1149   bool isWebAssemblyFuncrefType() const;
1150 
1151   // Don't promise in the API that anything besides 'const' can be
1152   // easily added.
1153 
1154   /// Add the `const` type qualifier to this QualType.
1155   void addConst() {
1156     addFastQualifiers(Qualifiers::Const);
1157   }
1158   QualType withConst() const {
1159     return withFastQualifiers(Qualifiers::Const);
1160   }
1161 
1162   /// Add the `volatile` type qualifier to this QualType.
1163   void addVolatile() {
1164     addFastQualifiers(Qualifiers::Volatile);
1165   }
1166   QualType withVolatile() const {
1167     return withFastQualifiers(Qualifiers::Volatile);
1168   }
1169 
1170   /// Add the `restrict` qualifier to this QualType.
1171   void addRestrict() {
1172     addFastQualifiers(Qualifiers::Restrict);
1173   }
1174   QualType withRestrict() const {
1175     return withFastQualifiers(Qualifiers::Restrict);
1176   }
1177 
1178   QualType withCVRQualifiers(unsigned CVR) const {
1179     return withFastQualifiers(CVR);
1180   }
1181 
1182   void addFastQualifiers(unsigned TQs) {
1183     assert(!(TQs & ~Qualifiers::FastMask)
1184            && "non-fast qualifier bits set in mask!");
1185     Value.setInt(Value.getInt() | TQs);
1186   }
1187 
1188   void removeLocalConst();
1189   void removeLocalVolatile();
1190   void removeLocalRestrict();
1191 
1192   void removeLocalFastQualifiers() { Value.setInt(0); }
1193   void removeLocalFastQualifiers(unsigned Mask) {
1194     assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers");
1195     Value.setInt(Value.getInt() & ~Mask);
1196   }
1197 
1198   // Creates a type with the given qualifiers in addition to any
1199   // qualifiers already on this type.
1200   QualType withFastQualifiers(unsigned TQs) const {
1201     QualType T = *this;
1202     T.addFastQualifiers(TQs);
1203     return T;
1204   }
1205 
1206   // Creates a type with exactly the given fast qualifiers, removing
1207   // any existing fast qualifiers.
1208   QualType withExactLocalFastQualifiers(unsigned TQs) const {
1209     return withoutLocalFastQualifiers().withFastQualifiers(TQs);
1210   }
1211 
1212   // Removes fast qualifiers, but leaves any extended qualifiers in place.
1213   QualType withoutLocalFastQualifiers() const {
1214     QualType T = *this;
1215     T.removeLocalFastQualifiers();
1216     return T;
1217   }
1218 
1219   QualType getCanonicalType() const;
1220 
1221   /// Return this type with all of the instance-specific qualifiers
1222   /// removed, but without removing any qualifiers that may have been applied
1223   /// through typedefs.
1224   QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); }
1225 
1226   /// Retrieve the unqualified variant of the given type,
1227   /// removing as little sugar as possible.
1228   ///
1229   /// This routine looks through various kinds of sugar to find the
1230   /// least-desugared type that is unqualified. For example, given:
1231   ///
1232   /// \code
1233   /// typedef int Integer;
1234   /// typedef const Integer CInteger;
1235   /// typedef CInteger DifferenceType;
1236   /// \endcode
1237   ///
1238   /// Executing \c getUnqualifiedType() on the type \c DifferenceType will
1239   /// desugar until we hit the type \c Integer, which has no qualifiers on it.
1240   ///
1241   /// The resulting type might still be qualified if it's sugar for an array
1242   /// type.  To strip qualifiers even from within a sugared array type, use
1243   /// ASTContext::getUnqualifiedArrayType.
1244   ///
1245   /// Note: In C, the _Atomic qualifier is special (see C23 6.2.5p32 for
1246   /// details), and it is not stripped by this function. Use
1247   /// getAtomicUnqualifiedType() to strip qualifiers including _Atomic.
1248   inline QualType getUnqualifiedType() const;
1249 
1250   /// Retrieve the unqualified variant of the given type, removing as little
1251   /// sugar as possible.
1252   ///
1253   /// Like getUnqualifiedType(), but also returns the set of
1254   /// qualifiers that were built up.
1255   ///
1256   /// The resulting type might still be qualified if it's sugar for an array
1257   /// type.  To strip qualifiers even from within a sugared array type, use
1258   /// ASTContext::getUnqualifiedArrayType.
1259   inline SplitQualType getSplitUnqualifiedType() const;
1260 
1261   /// Determine whether this type is more qualified than the other
1262   /// given type, requiring exact equality for non-CVR qualifiers.
1263   bool isMoreQualifiedThan(QualType Other, const ASTContext &Ctx) const;
1264 
1265   /// Determine whether this type is at least as qualified as the other
1266   /// given type, requiring exact equality for non-CVR qualifiers.
1267   bool isAtLeastAsQualifiedAs(QualType Other, const ASTContext &Ctx) const;
1268 
1269   QualType getNonReferenceType() const;
1270 
1271   /// Determine the type of a (typically non-lvalue) expression with the
1272   /// specified result type.
1273   ///
1274   /// This routine should be used for expressions for which the return type is
1275   /// explicitly specified (e.g., in a cast or call) and isn't necessarily
1276   /// an lvalue. It removes a top-level reference (since there are no
1277   /// expressions of reference type) and deletes top-level cvr-qualifiers
1278   /// from non-class types (in C++) or all types (in C).
1279   QualType getNonLValueExprType(const ASTContext &Context) const;
1280 
1281   /// Remove an outer pack expansion type (if any) from this type. Used as part
1282   /// of converting the type of a declaration to the type of an expression that
1283   /// references that expression. It's meaningless for an expression to have a
1284   /// pack expansion type.
1285   QualType getNonPackExpansionType() const;
1286 
1287   /// Return the specified type with any "sugar" removed from
1288   /// the type.  This takes off typedefs, typeof's etc.  If the outer level of
1289   /// the type is already concrete, it returns it unmodified.  This is similar
1290   /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
1291   /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
1292   /// concrete.
1293   ///
1294   /// Qualifiers are left in place.
1295   QualType getDesugaredType(const ASTContext &Context) const {
1296     return getDesugaredType(*this, Context);
1297   }
1298 
1299   SplitQualType getSplitDesugaredType() const {
1300     return getSplitDesugaredType(*this);
1301   }
1302 
1303   /// Return the specified type with one level of "sugar" removed from
1304   /// the type.
1305   ///
1306   /// This routine takes off the first typedef, typeof, etc. If the outer level
1307   /// of the type is already concrete, it returns it unmodified.
1308   QualType getSingleStepDesugaredType(const ASTContext &Context) const {
1309     return getSingleStepDesugaredTypeImpl(*this, Context);
1310   }
1311 
1312   /// Returns the specified type after dropping any
1313   /// outer-level parentheses.
1314   QualType IgnoreParens() const {
1315     if (isa<ParenType>(*this))
1316       return QualType::IgnoreParens(*this);
1317     return *this;
1318   }
1319 
1320   /// Indicate whether the specified types and qualifiers are identical.
1321   friend bool operator==(const QualType &LHS, const QualType &RHS) {
1322     return LHS.Value == RHS.Value;
1323   }
1324   friend bool operator!=(const QualType &LHS, const QualType &RHS) {
1325     return LHS.Value != RHS.Value;
1326   }
1327   friend bool operator<(const QualType &LHS, const QualType &RHS) {
1328     return LHS.Value < RHS.Value;
1329   }
1330 
1331   static std::string getAsString(SplitQualType split,
1332                                  const PrintingPolicy &Policy) {
1333     return getAsString(split.Ty, split.Quals, Policy);
1334   }
1335   static std::string getAsString(const Type *ty, Qualifiers qs,
1336                                  const PrintingPolicy &Policy);
1337 
1338   std::string getAsString() const;
1339   std::string getAsString(const PrintingPolicy &Policy) const;
1340 
1341   void print(raw_ostream &OS, const PrintingPolicy &Policy,
1342              const Twine &PlaceHolder = Twine(),
1343              unsigned Indentation = 0) const;
1344 
1345   static void print(SplitQualType split, raw_ostream &OS,
1346                     const PrintingPolicy &policy, const Twine &PlaceHolder,
1347                     unsigned Indentation = 0) {
1348     return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation);
1349   }
1350 
1351   static void print(const Type *ty, Qualifiers qs,
1352                     raw_ostream &OS, const PrintingPolicy &policy,
1353                     const Twine &PlaceHolder,
1354                     unsigned Indentation = 0);
1355 
1356   void getAsStringInternal(std::string &Str,
1357                            const PrintingPolicy &Policy) const;
1358 
1359   static void getAsStringInternal(SplitQualType split, std::string &out,
1360                                   const PrintingPolicy &policy) {
1361     return getAsStringInternal(split.Ty, split.Quals, out, policy);
1362   }
1363 
1364   static void getAsStringInternal(const Type *ty, Qualifiers qs,
1365                                   std::string &out,
1366                                   const PrintingPolicy &policy);
1367 
1368   class StreamedQualTypeHelper {
1369     const QualType &T;
1370     const PrintingPolicy &Policy;
1371     const Twine &PlaceHolder;
1372     unsigned Indentation;
1373 
1374   public:
1375     StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy,
1376                            const Twine &PlaceHolder, unsigned Indentation)
1377         : T(T), Policy(Policy), PlaceHolder(PlaceHolder),
1378           Indentation(Indentation) {}
1379 
1380     friend raw_ostream &operator<<(raw_ostream &OS,
1381                                    const StreamedQualTypeHelper &SQT) {
1382       SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation);
1383       return OS;
1384     }
1385   };
1386 
1387   StreamedQualTypeHelper stream(const PrintingPolicy &Policy,
1388                                 const Twine &PlaceHolder = Twine(),
1389                                 unsigned Indentation = 0) const {
1390     return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation);
1391   }
1392 
1393   void dump(const char *s) const;
1394   void dump() const;
1395   void dump(llvm::raw_ostream &OS, const ASTContext &Context) const;
1396 
1397   void Profile(llvm::FoldingSetNodeID &ID) const {
1398     ID.AddPointer(getAsOpaquePtr());
1399   }
1400 
1401   /// Check if this type has any address space qualifier.
1402   inline bool hasAddressSpace() const;
1403 
1404   /// Return the address space of this type.
1405   inline LangAS getAddressSpace() const;
1406 
1407   /// Returns true if address space qualifiers overlap with T address space
1408   /// qualifiers.
1409   /// OpenCL C defines conversion rules for pointers to different address spaces
1410   /// and notion of overlapping address spaces.
1411   /// CL1.1 or CL1.2:
1412   ///   address spaces overlap iff they are they same.
1413   /// OpenCL C v2.0 s6.5.5 adds:
1414   ///   __generic overlaps with any address space except for __constant.
1415   bool isAddressSpaceOverlapping(QualType T, const ASTContext &Ctx) const {
1416     Qualifiers Q = getQualifiers();
1417     Qualifiers TQ = T.getQualifiers();
1418     // Address spaces overlap if at least one of them is a superset of another
1419     return Q.isAddressSpaceSupersetOf(TQ, Ctx) ||
1420            TQ.isAddressSpaceSupersetOf(Q, Ctx);
1421   }
1422 
1423   /// Returns gc attribute of this type.
1424   inline Qualifiers::GC getObjCGCAttr() const;
1425 
1426   /// true when Type is objc's weak.
1427   bool isObjCGCWeak() const {
1428     return getObjCGCAttr() == Qualifiers::Weak;
1429   }
1430 
1431   /// true when Type is objc's strong.
1432   bool isObjCGCStrong() const {
1433     return getObjCGCAttr() == Qualifiers::Strong;
1434   }
1435 
1436   /// Returns lifetime attribute of this type.
1437   Qualifiers::ObjCLifetime getObjCLifetime() const {
1438     return getQualifiers().getObjCLifetime();
1439   }
1440 
1441   bool hasNonTrivialObjCLifetime() const {
1442     return getQualifiers().hasNonTrivialObjCLifetime();
1443   }
1444 
1445   bool hasStrongOrWeakObjCLifetime() const {
1446     return getQualifiers().hasStrongOrWeakObjCLifetime();
1447   }
1448 
1449   // true when Type is objc's weak and weak is enabled but ARC isn't.
1450   bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const;
1451 
1452   PointerAuthQualifier getPointerAuth() const {
1453     return getQualifiers().getPointerAuth();
1454   }
1455 
1456   bool hasAddressDiscriminatedPointerAuth() const {
1457     if (PointerAuthQualifier PtrAuth = getPointerAuth())
1458       return PtrAuth.isAddressDiscriminated();
1459     return false;
1460   }
1461 
1462   enum PrimitiveDefaultInitializeKind {
1463     /// The type does not fall into any of the following categories. Note that
1464     /// this case is zero-valued so that values of this enum can be used as a
1465     /// boolean condition for non-triviality.
1466     PDIK_Trivial,
1467 
1468     /// The type is an Objective-C retainable pointer type that is qualified
1469     /// with the ARC __strong qualifier.
1470     PDIK_ARCStrong,
1471 
1472     /// The type is an Objective-C retainable pointer type that is qualified
1473     /// with the ARC __weak qualifier.
1474     PDIK_ARCWeak,
1475 
1476     /// The type is a struct containing a field whose type is not PCK_Trivial.
1477     PDIK_Struct
1478   };
1479 
1480   /// Functions to query basic properties of non-trivial C struct types.
1481 
1482   /// Check if this is a non-trivial type that would cause a C struct
1483   /// transitively containing this type to be non-trivial to default initialize
1484   /// and return the kind.
1485   PrimitiveDefaultInitializeKind
1486   isNonTrivialToPrimitiveDefaultInitialize() const;
1487 
1488   enum PrimitiveCopyKind {
1489     /// The type does not fall into any of the following categories. Note that
1490     /// this case is zero-valued so that values of this enum can be used as a
1491     /// boolean condition for non-triviality.
1492     PCK_Trivial,
1493 
1494     /// The type would be trivial except that it is volatile-qualified. Types
1495     /// that fall into one of the other non-trivial cases may additionally be
1496     /// volatile-qualified.
1497     PCK_VolatileTrivial,
1498 
1499     /// The type is an Objective-C retainable pointer type that is qualified
1500     /// with the ARC __strong qualifier.
1501     PCK_ARCStrong,
1502 
1503     /// The type is an Objective-C retainable pointer type that is qualified
1504     /// with the ARC __weak qualifier.
1505     PCK_ARCWeak,
1506 
1507     /// The type is an address-discriminated signed pointer type.
1508     PCK_PtrAuth,
1509 
1510     /// The type is a struct containing a field whose type is neither
1511     /// PCK_Trivial nor PCK_VolatileTrivial.
1512     /// Note that a C++ struct type does not necessarily match this; C++ copying
1513     /// semantics are too complex to express here, in part because they depend
1514     /// on the exact constructor or assignment operator that is chosen by
1515     /// overload resolution to do the copy.
1516     PCK_Struct
1517   };
1518 
1519   /// Check if this is a non-trivial type that would cause a C struct
1520   /// transitively containing this type to be non-trivial to copy and return the
1521   /// kind.
1522   PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const;
1523 
1524   /// Check if this is a non-trivial type that would cause a C struct
1525   /// transitively containing this type to be non-trivial to destructively
1526   /// move and return the kind. Destructive move in this context is a C++-style
1527   /// move in which the source object is placed in a valid but unspecified state
1528   /// after it is moved, as opposed to a truly destructive move in which the
1529   /// source object is placed in an uninitialized state.
1530   PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const;
1531 
1532   enum DestructionKind {
1533     DK_none,
1534     DK_cxx_destructor,
1535     DK_objc_strong_lifetime,
1536     DK_objc_weak_lifetime,
1537     DK_nontrivial_c_struct
1538   };
1539 
1540   /// Returns a nonzero value if objects of this type require
1541   /// non-trivial work to clean up after.  Non-zero because it's
1542   /// conceivable that qualifiers (objc_gc(weak)?) could make
1543   /// something require destruction.
1544   DestructionKind isDestructedType() const {
1545     return isDestructedTypeImpl(*this);
1546   }
1547 
1548   /// Check if this is or contains a C union that is non-trivial to
1549   /// default-initialize, which is a union that has a member that is non-trivial
1550   /// to default-initialize. If this returns true,
1551   /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct.
1552   bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const;
1553 
1554   /// Check if this is or contains a C union that is non-trivial to destruct,
1555   /// which is a union that has a member that is non-trivial to destruct. If
1556   /// this returns true, isDestructedType returns DK_nontrivial_c_struct.
1557   bool hasNonTrivialToPrimitiveDestructCUnion() const;
1558 
1559   /// Check if this is or contains a C union that is non-trivial to copy, which
1560   /// is a union that has a member that is non-trivial to copy. If this returns
1561   /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct.
1562   bool hasNonTrivialToPrimitiveCopyCUnion() const;
1563 
1564   /// Determine whether expressions of the given type are forbidden
1565   /// from being lvalues in C.
1566   ///
1567   /// The expression types that are forbidden to be lvalues are:
1568   ///   - 'void', but not qualified void
1569   ///   - function types
1570   ///
1571   /// The exact rule here is C99 6.3.2.1:
1572   ///   An lvalue is an expression with an object type or an incomplete
1573   ///   type other than void.
1574   bool isCForbiddenLValueType() const;
1575 
1576   /// Substitute type arguments for the Objective-C type parameters used in the
1577   /// subject type.
1578   ///
1579   /// \param ctx ASTContext in which the type exists.
1580   ///
1581   /// \param typeArgs The type arguments that will be substituted for the
1582   /// Objective-C type parameters in the subject type, which are generally
1583   /// computed via \c Type::getObjCSubstitutions. If empty, the type
1584   /// parameters will be replaced with their bounds or id/Class, as appropriate
1585   /// for the context.
1586   ///
1587   /// \param context The context in which the subject type was written.
1588   ///
1589   /// \returns the resulting type.
1590   QualType substObjCTypeArgs(ASTContext &ctx,
1591                              ArrayRef<QualType> typeArgs,
1592                              ObjCSubstitutionContext context) const;
1593 
1594   /// Substitute type arguments from an object type for the Objective-C type
1595   /// parameters used in the subject type.
1596   ///
1597   /// This operation combines the computation of type arguments for
1598   /// substitution (\c Type::getObjCSubstitutions) with the actual process of
1599   /// substitution (\c QualType::substObjCTypeArgs) for the convenience of
1600   /// callers that need to perform a single substitution in isolation.
1601   ///
1602   /// \param objectType The type of the object whose member type we're
1603   /// substituting into. For example, this might be the receiver of a message
1604   /// or the base of a property access.
1605   ///
1606   /// \param dc The declaration context from which the subject type was
1607   /// retrieved, which indicates (for example) which type parameters should
1608   /// be substituted.
1609   ///
1610   /// \param context The context in which the subject type was written.
1611   ///
1612   /// \returns the subject type after replacing all of the Objective-C type
1613   /// parameters with their corresponding arguments.
1614   QualType substObjCMemberType(QualType objectType,
1615                                const DeclContext *dc,
1616                                ObjCSubstitutionContext context) const;
1617 
1618   /// Strip Objective-C "__kindof" types from the given type.
1619   QualType stripObjCKindOfType(const ASTContext &ctx) const;
1620 
1621   /// Remove all qualifiers including _Atomic.
1622   ///
1623   /// Like getUnqualifiedType(), the type may still be qualified if it is a
1624   /// sugared array type.  To strip qualifiers even from within a sugared array
1625   /// type, use in conjunction with ASTContext::getUnqualifiedArrayType.
1626   QualType getAtomicUnqualifiedType() const;
1627 
1628 private:
1629   // These methods are implemented in a separate translation unit;
1630   // "static"-ize them to avoid creating temporary QualTypes in the
1631   // caller.
1632   static bool isConstant(QualType T, const ASTContext& Ctx);
1633   static QualType getDesugaredType(QualType T, const ASTContext &Context);
1634   static SplitQualType getSplitDesugaredType(QualType T);
1635   static SplitQualType getSplitUnqualifiedTypeImpl(QualType type);
1636   static QualType getSingleStepDesugaredTypeImpl(QualType type,
1637                                                  const ASTContext &C);
1638   static QualType IgnoreParens(QualType T);
1639   static DestructionKind isDestructedTypeImpl(QualType type);
1640 
1641   /// Check if \param RD is or contains a non-trivial C union.
1642   static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD);
1643   static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD);
1644   static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD);
1645 };
1646 
1647 raw_ostream &operator<<(raw_ostream &OS, QualType QT);
1648 
1649 } // namespace clang
1650 
1651 namespace llvm {
1652 
1653 /// Implement simplify_type for QualType, so that we can dyn_cast from QualType
1654 /// to a specific Type class.
1655 template<> struct simplify_type< ::clang::QualType> {
1656   using SimpleType = const ::clang::Type *;
1657 
1658   static SimpleType getSimplifiedValue(::clang::QualType Val) {
1659     return Val.getTypePtr();
1660   }
1661 };
1662 
1663 // Teach SmallPtrSet that QualType is "basically a pointer".
1664 template<>
1665 struct PointerLikeTypeTraits<clang::QualType> {
1666   static inline void *getAsVoidPointer(clang::QualType P) {
1667     return P.getAsOpaquePtr();
1668   }
1669 
1670   static inline clang::QualType getFromVoidPointer(void *P) {
1671     return clang::QualType::getFromOpaquePtr(P);
1672   }
1673 
1674   // Various qualifiers go in low bits.
1675   static constexpr int NumLowBitsAvailable = 0;
1676 };
1677 
1678 } // namespace llvm
1679 
1680 namespace clang {
1681 
1682 /// Base class that is common to both the \c ExtQuals and \c Type
1683 /// classes, which allows \c QualType to access the common fields between the
1684 /// two.
1685 class ExtQualsTypeCommonBase {
1686   friend class ExtQuals;
1687   friend class QualType;
1688   friend class Type;
1689   friend class ASTReader;
1690 
1691   /// The "base" type of an extended qualifiers type (\c ExtQuals) or
1692   /// a self-referential pointer (for \c Type).
1693   ///
1694   /// This pointer allows an efficient mapping from a QualType to its
1695   /// underlying type pointer.
1696   const Type *const BaseType;
1697 
1698   /// The canonical type of this type.  A QualType.
1699   QualType CanonicalType;
1700 
1701   ExtQualsTypeCommonBase(const Type *baseType, QualType canon)
1702       : BaseType(baseType), CanonicalType(canon) {}
1703 };
1704 
1705 /// We can encode up to four bits in the low bits of a
1706 /// type pointer, but there are many more type qualifiers that we want
1707 /// to be able to apply to an arbitrary type.  Therefore we have this
1708 /// struct, intended to be heap-allocated and used by QualType to
1709 /// store qualifiers.
1710 ///
1711 /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers
1712 /// in three low bits on the QualType pointer; a fourth bit records whether
1713 /// the pointer is an ExtQuals node. The extended qualifiers (address spaces,
1714 /// Objective-C GC attributes) are much more rare.
1715 class alignas(TypeAlignment) ExtQuals : public ExtQualsTypeCommonBase,
1716                                         public llvm::FoldingSetNode {
1717   // NOTE: changing the fast qualifiers should be straightforward as
1718   // long as you don't make 'const' non-fast.
1719   // 1. Qualifiers:
1720   //    a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ).
1721   //       Fast qualifiers must occupy the low-order bits.
1722   //    b) Update Qualifiers::FastWidth and FastMask.
1723   // 2. QualType:
1724   //    a) Update is{Volatile,Restrict}Qualified(), defined inline.
1725   //    b) Update remove{Volatile,Restrict}, defined near the end of
1726   //       this header.
1727   // 3. ASTContext:
1728   //    a) Update get{Volatile,Restrict}Type.
1729 
1730   /// The immutable set of qualifiers applied by this node. Always contains
1731   /// extended qualifiers.
1732   Qualifiers Quals;
1733 
1734   ExtQuals *this_() { return this; }
1735 
1736 public:
1737   ExtQuals(const Type *baseType, QualType canon, Qualifiers quals)
1738       : ExtQualsTypeCommonBase(baseType,
1739                                canon.isNull() ? QualType(this_(), 0) : canon),
1740         Quals(quals) {
1741     assert(Quals.hasNonFastQualifiers()
1742            && "ExtQuals created with no fast qualifiers");
1743     assert(!Quals.hasFastQualifiers()
1744            && "ExtQuals created with fast qualifiers");
1745   }
1746 
1747   Qualifiers getQualifiers() const { return Quals; }
1748 
1749   bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); }
1750   Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); }
1751 
1752   bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); }
1753   Qualifiers::ObjCLifetime getObjCLifetime() const {
1754     return Quals.getObjCLifetime();
1755   }
1756 
1757   bool hasAddressSpace() const { return Quals.hasAddressSpace(); }
1758   LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
1759 
1760   const Type *getBaseType() const { return BaseType; }
1761 
1762 public:
1763   void Profile(llvm::FoldingSetNodeID &ID) const {
1764     Profile(ID, getBaseType(), Quals);
1765   }
1766 
1767   static void Profile(llvm::FoldingSetNodeID &ID,
1768                       const Type *BaseType,
1769                       Qualifiers Quals) {
1770     assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!");
1771     ID.AddPointer(BaseType);
1772     Quals.Profile(ID);
1773   }
1774 };
1775 
1776 /// The kind of C++11 ref-qualifier associated with a function type.
1777 /// This determines whether a member function's "this" object can be an
1778 /// lvalue, rvalue, or neither.
1779 enum RefQualifierKind {
1780   /// No ref-qualifier was provided.
1781   RQ_None = 0,
1782 
1783   /// An lvalue ref-qualifier was provided (\c &).
1784   RQ_LValue,
1785 
1786   /// An rvalue ref-qualifier was provided (\c &&).
1787   RQ_RValue
1788 };
1789 
1790 /// Which keyword(s) were used to create an AutoType.
1791 enum class AutoTypeKeyword {
1792   /// auto
1793   Auto,
1794 
1795   /// decltype(auto)
1796   DecltypeAuto,
1797 
1798   /// __auto_type (GNU extension)
1799   GNUAutoType
1800 };
1801 
1802 enum class ArraySizeModifier;
1803 enum class ElaboratedTypeKeyword;
1804 enum class VectorKind;
1805 
1806 /// The base class of the type hierarchy.
1807 ///
1808 /// A central concept with types is that each type always has a canonical
1809 /// type.  A canonical type is the type with any typedef names stripped out
1810 /// of it or the types it references.  For example, consider:
1811 ///
1812 ///  typedef int  foo;
1813 ///  typedef foo* bar;
1814 ///    'int *'    'foo *'    'bar'
1815 ///
1816 /// There will be a Type object created for 'int'.  Since int is canonical, its
1817 /// CanonicalType pointer points to itself.  There is also a Type for 'foo' (a
1818 /// TypedefType).  Its CanonicalType pointer points to the 'int' Type.  Next
1819 /// there is a PointerType that represents 'int*', which, like 'int', is
1820 /// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
1821 /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type
1822 /// is also 'int*'.
1823 ///
1824 /// Non-canonical types are useful for emitting diagnostics, without losing
1825 /// information about typedefs being used.  Canonical types are useful for type
1826 /// comparisons (they allow by-pointer equality tests) and useful for reasoning
1827 /// about whether something has a particular form (e.g. is a function type),
1828 /// because they implicitly, recursively, strip all typedefs out of a type.
1829 ///
1830 /// Types, once created, are immutable.
1831 ///
1832 class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
1833 public:
1834   enum TypeClass {
1835 #define TYPE(Class, Base) Class,
1836 #define LAST_TYPE(Class) TypeLast = Class
1837 #define ABSTRACT_TYPE(Class, Base)
1838 #include "clang/AST/TypeNodes.inc"
1839   };
1840 
1841 private:
1842   /// Bitfields required by the Type class.
1843   class TypeBitfields {
1844     friend class Type;
1845     template <class T> friend class TypePropertyCache;
1846 
1847     /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
1848     LLVM_PREFERRED_TYPE(TypeClass)
1849     unsigned TC : 8;
1850 
1851     /// Store information on the type dependency.
1852     LLVM_PREFERRED_TYPE(TypeDependence)
1853     unsigned Dependence : llvm::BitWidth<TypeDependence>;
1854 
1855     /// True if the cache (i.e. the bitfields here starting with
1856     /// 'Cache') is valid.
1857     LLVM_PREFERRED_TYPE(bool)
1858     mutable unsigned CacheValid : 1;
1859 
1860     /// Linkage of this type.
1861     LLVM_PREFERRED_TYPE(Linkage)
1862     mutable unsigned CachedLinkage : 3;
1863 
1864     /// Whether this type involves and local or unnamed types.
1865     LLVM_PREFERRED_TYPE(bool)
1866     mutable unsigned CachedLocalOrUnnamed : 1;
1867 
1868     /// Whether this type comes from an AST file.
1869     LLVM_PREFERRED_TYPE(bool)
1870     mutable unsigned FromAST : 1;
1871 
1872     bool isCacheValid() const {
1873       return CacheValid;
1874     }
1875 
1876     Linkage getLinkage() const {
1877       assert(isCacheValid() && "getting linkage from invalid cache");
1878       return static_cast<Linkage>(CachedLinkage);
1879     }
1880 
1881     bool hasLocalOrUnnamedType() const {
1882       assert(isCacheValid() && "getting linkage from invalid cache");
1883       return CachedLocalOrUnnamed;
1884     }
1885   };
1886   enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 };
1887 
1888 protected:
1889   // These classes allow subclasses to somewhat cleanly pack bitfields
1890   // into Type.
1891 
1892   class ArrayTypeBitfields {
1893     friend class ArrayType;
1894 
1895     LLVM_PREFERRED_TYPE(TypeBitfields)
1896     unsigned : NumTypeBits;
1897 
1898     /// CVR qualifiers from declarations like
1899     /// 'int X[static restrict 4]'. For function parameters only.
1900     LLVM_PREFERRED_TYPE(Qualifiers)
1901     unsigned IndexTypeQuals : 3;
1902 
1903     /// Storage class qualifiers from declarations like
1904     /// 'int X[static restrict 4]'. For function parameters only.
1905     LLVM_PREFERRED_TYPE(ArraySizeModifier)
1906     unsigned SizeModifier : 3;
1907   };
1908   enum { NumArrayTypeBits = NumTypeBits + 6 };
1909 
1910   class ConstantArrayTypeBitfields {
1911     friend class ConstantArrayType;
1912 
1913     LLVM_PREFERRED_TYPE(ArrayTypeBitfields)
1914     unsigned : NumArrayTypeBits;
1915 
1916     /// Whether we have a stored size expression.
1917     LLVM_PREFERRED_TYPE(bool)
1918     unsigned HasExternalSize : 1;
1919 
1920     LLVM_PREFERRED_TYPE(unsigned)
1921     unsigned SizeWidth : 5;
1922   };
1923 
1924   class BuiltinTypeBitfields {
1925     friend class BuiltinType;
1926 
1927     LLVM_PREFERRED_TYPE(TypeBitfields)
1928     unsigned : NumTypeBits;
1929 
1930     /// The kind (BuiltinType::Kind) of builtin type this is.
1931     static constexpr unsigned NumOfBuiltinTypeBits = 9;
1932     unsigned Kind : NumOfBuiltinTypeBits;
1933   };
1934 
1935 public:
1936   static constexpr int FunctionTypeNumParamsWidth = 16;
1937   static constexpr int FunctionTypeNumParamsLimit = (1 << 16) - 1;
1938 
1939 protected:
1940   /// FunctionTypeBitfields store various bits belonging to FunctionProtoType.
1941   /// Only common bits are stored here. Additional uncommon bits are stored
1942   /// in a trailing object after FunctionProtoType.
1943   class FunctionTypeBitfields {
1944     friend class FunctionProtoType;
1945     friend class FunctionType;
1946 
1947     LLVM_PREFERRED_TYPE(TypeBitfields)
1948     unsigned : NumTypeBits;
1949 
1950     /// The ref-qualifier associated with a \c FunctionProtoType.
1951     ///
1952     /// This is a value of type \c RefQualifierKind.
1953     LLVM_PREFERRED_TYPE(RefQualifierKind)
1954     unsigned RefQualifier : 2;
1955 
1956     /// Used only by FunctionProtoType, put here to pack with the
1957     /// other bitfields.
1958     /// The qualifiers are part of FunctionProtoType because...
1959     ///
1960     /// C++ 8.3.5p4: The return type, the parameter type list and the
1961     /// cv-qualifier-seq, [...], are part of the function type.
1962     LLVM_PREFERRED_TYPE(Qualifiers)
1963     unsigned FastTypeQuals : Qualifiers::FastWidth;
1964     /// Whether this function has extended Qualifiers.
1965     LLVM_PREFERRED_TYPE(bool)
1966     unsigned HasExtQuals : 1;
1967 
1968     /// The type of exception specification this function has.
1969     LLVM_PREFERRED_TYPE(ExceptionSpecificationType)
1970     unsigned ExceptionSpecType : 4;
1971 
1972     /// Whether this function has extended parameter information.
1973     LLVM_PREFERRED_TYPE(bool)
1974     unsigned HasExtParameterInfos : 1;
1975 
1976     /// Whether this function has extra bitfields for the prototype.
1977     LLVM_PREFERRED_TYPE(bool)
1978     unsigned HasExtraBitfields : 1;
1979 
1980     /// Whether the function is variadic.
1981     LLVM_PREFERRED_TYPE(bool)
1982     unsigned Variadic : 1;
1983 
1984     /// Whether this function has a trailing return type.
1985     LLVM_PREFERRED_TYPE(bool)
1986     unsigned HasTrailingReturn : 1;
1987 
1988     /// Whether this function has is a cfi unchecked callee.
1989     LLVM_PREFERRED_TYPE(bool)
1990     unsigned CFIUncheckedCallee : 1;
1991 
1992     /// Extra information which affects how the function is called, like
1993     /// regparm and the calling convention.
1994     LLVM_PREFERRED_TYPE(CallingConv)
1995     unsigned ExtInfo : 14;
1996 
1997     /// The number of parameters this function has, not counting '...'.
1998     /// According to [implimits] 8 bits should be enough here but this is
1999     /// somewhat easy to exceed with metaprogramming and so we would like to
2000     /// keep NumParams as wide as reasonably possible.
2001     unsigned NumParams : FunctionTypeNumParamsWidth;
2002   };
2003 
2004   class ObjCObjectTypeBitfields {
2005     friend class ObjCObjectType;
2006 
2007     LLVM_PREFERRED_TYPE(TypeBitfields)
2008     unsigned : NumTypeBits;
2009 
2010     /// The number of type arguments stored directly on this object type.
2011     unsigned NumTypeArgs : 7;
2012 
2013     /// The number of protocols stored directly on this object type.
2014     unsigned NumProtocols : 6;
2015 
2016     /// Whether this is a "kindof" type.
2017     LLVM_PREFERRED_TYPE(bool)
2018     unsigned IsKindOf : 1;
2019   };
2020 
2021   class ReferenceTypeBitfields {
2022     friend class ReferenceType;
2023 
2024     LLVM_PREFERRED_TYPE(TypeBitfields)
2025     unsigned : NumTypeBits;
2026 
2027     /// True if the type was originally spelled with an lvalue sigil.
2028     /// This is never true of rvalue references but can also be false
2029     /// on lvalue references because of C++0x [dcl.typedef]p9,
2030     /// as follows:
2031     ///
2032     ///   typedef int &ref;    // lvalue, spelled lvalue
2033     ///   typedef int &&rvref; // rvalue
2034     ///   ref &a;              // lvalue, inner ref, spelled lvalue
2035     ///   ref &&a;             // lvalue, inner ref
2036     ///   rvref &a;            // lvalue, inner ref, spelled lvalue
2037     ///   rvref &&a;           // rvalue, inner ref
2038     LLVM_PREFERRED_TYPE(bool)
2039     unsigned SpelledAsLValue : 1;
2040 
2041     /// True if the inner type is a reference type.  This only happens
2042     /// in non-canonical forms.
2043     LLVM_PREFERRED_TYPE(bool)
2044     unsigned InnerRef : 1;
2045   };
2046 
2047   class TypeWithKeywordBitfields {
2048     friend class TypeWithKeyword;
2049 
2050     LLVM_PREFERRED_TYPE(TypeBitfields)
2051     unsigned : NumTypeBits;
2052 
2053     /// An ElaboratedTypeKeyword.  8 bits for efficient access.
2054     LLVM_PREFERRED_TYPE(ElaboratedTypeKeyword)
2055     unsigned Keyword : 8;
2056   };
2057 
2058   enum { NumTypeWithKeywordBits = NumTypeBits + 8 };
2059 
2060   class ElaboratedTypeBitfields {
2061     friend class ElaboratedType;
2062 
2063     LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
2064     unsigned : NumTypeWithKeywordBits;
2065 
2066     /// Whether the ElaboratedType has a trailing OwnedTagDecl.
2067     LLVM_PREFERRED_TYPE(bool)
2068     unsigned HasOwnedTagDecl : 1;
2069   };
2070 
2071   class VectorTypeBitfields {
2072     friend class VectorType;
2073     friend class DependentVectorType;
2074 
2075     LLVM_PREFERRED_TYPE(TypeBitfields)
2076     unsigned : NumTypeBits;
2077 
2078     /// The kind of vector, either a generic vector type or some
2079     /// target-specific vector type such as for AltiVec or Neon.
2080     LLVM_PREFERRED_TYPE(VectorKind)
2081     unsigned VecKind : 4;
2082     /// The number of elements in the vector.
2083     uint32_t NumElements;
2084   };
2085 
2086   class AttributedTypeBitfields {
2087     friend class AttributedType;
2088 
2089     LLVM_PREFERRED_TYPE(TypeBitfields)
2090     unsigned : NumTypeBits;
2091 
2092     LLVM_PREFERRED_TYPE(attr::Kind)
2093     unsigned AttrKind : 32 - NumTypeBits;
2094   };
2095 
2096   class AutoTypeBitfields {
2097     friend class AutoType;
2098 
2099     LLVM_PREFERRED_TYPE(TypeBitfields)
2100     unsigned : NumTypeBits;
2101 
2102     /// Was this placeholder type spelled as 'auto', 'decltype(auto)',
2103     /// or '__auto_type'?  AutoTypeKeyword value.
2104     LLVM_PREFERRED_TYPE(AutoTypeKeyword)
2105     unsigned Keyword : 2;
2106 
2107     /// The number of template arguments in the type-constraints, which is
2108     /// expected to be able to hold at least 1024 according to [implimits].
2109     /// However as this limit is somewhat easy to hit with template
2110     /// metaprogramming we'd prefer to keep it as large as possible.
2111     /// At the moment it has been left as a non-bitfield since this type
2112     /// safely fits in 64 bits as an unsigned, so there is no reason to
2113     /// introduce the performance impact of a bitfield.
2114     unsigned NumArgs;
2115   };
2116 
2117   class TypeOfBitfields {
2118     friend class TypeOfType;
2119     friend class TypeOfExprType;
2120 
2121     LLVM_PREFERRED_TYPE(TypeBitfields)
2122     unsigned : NumTypeBits;
2123     LLVM_PREFERRED_TYPE(TypeOfKind)
2124     unsigned Kind : 1;
2125   };
2126 
2127   class UsingBitfields {
2128     friend class UsingType;
2129 
2130     LLVM_PREFERRED_TYPE(TypeBitfields)
2131     unsigned : NumTypeBits;
2132 
2133     /// True if the underlying type is different from the declared one.
2134     LLVM_PREFERRED_TYPE(bool)
2135     unsigned hasTypeDifferentFromDecl : 1;
2136   };
2137 
2138   class TypedefBitfields {
2139     friend class TypedefType;
2140 
2141     LLVM_PREFERRED_TYPE(TypeBitfields)
2142     unsigned : NumTypeBits;
2143 
2144     /// True if the underlying type is different from the declared one.
2145     LLVM_PREFERRED_TYPE(bool)
2146     unsigned hasTypeDifferentFromDecl : 1;
2147   };
2148 
2149   class TemplateTypeParmTypeBitfields {
2150     friend class TemplateTypeParmType;
2151 
2152     LLVM_PREFERRED_TYPE(TypeBitfields)
2153     unsigned : NumTypeBits;
2154 
2155     /// The depth of the template parameter.
2156     unsigned Depth : 15;
2157 
2158     /// Whether this is a template parameter pack.
2159     LLVM_PREFERRED_TYPE(bool)
2160     unsigned ParameterPack : 1;
2161 
2162     /// The index of the template parameter.
2163     unsigned Index : 16;
2164   };
2165 
2166   class SubstTemplateTypeParmTypeBitfields {
2167     friend class SubstTemplateTypeParmType;
2168 
2169     LLVM_PREFERRED_TYPE(TypeBitfields)
2170     unsigned : NumTypeBits;
2171 
2172     LLVM_PREFERRED_TYPE(bool)
2173     unsigned HasNonCanonicalUnderlyingType : 1;
2174 
2175     // The index of the template parameter this substitution represents.
2176     unsigned Index : 15;
2177 
2178     LLVM_PREFERRED_TYPE(bool)
2179     unsigned Final : 1;
2180 
2181     /// Represents the index within a pack if this represents a substitution
2182     /// from a pack expansion. This index starts at the end of the pack and
2183     /// increments towards the beginning.
2184     /// Positive non-zero number represents the index + 1.
2185     /// Zero means this is not substituted from an expansion.
2186     unsigned PackIndex : 15;
2187   };
2188 
2189   class SubstTemplateTypeParmPackTypeBitfields {
2190     friend class SubstTemplateTypeParmPackType;
2191 
2192     LLVM_PREFERRED_TYPE(TypeBitfields)
2193     unsigned : NumTypeBits;
2194 
2195     // The index of the template parameter this substitution represents.
2196     unsigned Index : 16;
2197 
2198     /// The number of template arguments in \c Arguments, which is
2199     /// expected to be able to hold at least 1024 according to [implimits].
2200     /// However as this limit is somewhat easy to hit with template
2201     /// metaprogramming we'd prefer to keep it as large as possible.
2202     unsigned NumArgs : 16;
2203   };
2204 
2205   class TemplateSpecializationTypeBitfields {
2206     friend class TemplateSpecializationType;
2207 
2208     LLVM_PREFERRED_TYPE(TypeBitfields)
2209     unsigned : NumTypeBits;
2210 
2211     /// Whether this template specialization type is a substituted type alias.
2212     LLVM_PREFERRED_TYPE(bool)
2213     unsigned TypeAlias : 1;
2214 
2215     /// The number of template arguments named in this class template
2216     /// specialization, which is expected to be able to hold at least 1024
2217     /// according to [implimits]. However, as this limit is somewhat easy to
2218     /// hit with template metaprogramming we'd prefer to keep it as large
2219     /// as possible. At the moment it has been left as a non-bitfield since
2220     /// this type safely fits in 64 bits as an unsigned, so there is no reason
2221     /// to introduce the performance impact of a bitfield.
2222     unsigned NumArgs;
2223   };
2224 
2225   class DependentTemplateSpecializationTypeBitfields {
2226     friend class DependentTemplateSpecializationType;
2227 
2228     LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields)
2229     unsigned : NumTypeWithKeywordBits;
2230 
2231     /// The number of template arguments named in this class template
2232     /// specialization, which is expected to be able to hold at least 1024
2233     /// according to [implimits]. However, as this limit is somewhat easy to
2234     /// hit with template metaprogramming we'd prefer to keep it as large
2235     /// as possible. At the moment it has been left as a non-bitfield since
2236     /// this type safely fits in 64 bits as an unsigned, so there is no reason
2237     /// to introduce the performance impact of a bitfield.
2238     unsigned NumArgs;
2239   };
2240 
2241   class PackExpansionTypeBitfields {
2242     friend class PackExpansionType;
2243 
2244     LLVM_PREFERRED_TYPE(TypeBitfields)
2245     unsigned : NumTypeBits;
2246 
2247     /// The number of expansions that this pack expansion will
2248     /// generate when substituted (+1), which is expected to be able to
2249     /// hold at least 1024 according to [implimits]. However, as this limit
2250     /// is somewhat easy to hit with template metaprogramming we'd prefer to
2251     /// keep it as large as possible. At the moment it has been left as a
2252     /// non-bitfield since this type safely fits in 64 bits as an unsigned, so
2253     /// there is no reason to introduce the performance impact of a bitfield.
2254     ///
2255     /// This field will only have a non-zero value when some of the parameter
2256     /// packs that occur within the pattern have been substituted but others
2257     /// have not.
2258     unsigned NumExpansions;
2259   };
2260 
2261   class CountAttributedTypeBitfields {
2262     friend class CountAttributedType;
2263 
2264     LLVM_PREFERRED_TYPE(TypeBitfields)
2265     unsigned : NumTypeBits;
2266 
2267     static constexpr unsigned NumCoupledDeclsBits = 4;
2268     unsigned NumCoupledDecls : NumCoupledDeclsBits;
2269     LLVM_PREFERRED_TYPE(bool)
2270     unsigned CountInBytes : 1;
2271     LLVM_PREFERRED_TYPE(bool)
2272     unsigned OrNull : 1;
2273   };
2274   static_assert(sizeof(CountAttributedTypeBitfields) <= sizeof(unsigned));
2275 
2276   union {
2277     TypeBitfields TypeBits;
2278     ArrayTypeBitfields ArrayTypeBits;
2279     ConstantArrayTypeBitfields ConstantArrayTypeBits;
2280     AttributedTypeBitfields AttributedTypeBits;
2281     AutoTypeBitfields AutoTypeBits;
2282     TypeOfBitfields TypeOfBits;
2283     TypedefBitfields TypedefBits;
2284     UsingBitfields UsingBits;
2285     BuiltinTypeBitfields BuiltinTypeBits;
2286     FunctionTypeBitfields FunctionTypeBits;
2287     ObjCObjectTypeBitfields ObjCObjectTypeBits;
2288     ReferenceTypeBitfields ReferenceTypeBits;
2289     TypeWithKeywordBitfields TypeWithKeywordBits;
2290     ElaboratedTypeBitfields ElaboratedTypeBits;
2291     VectorTypeBitfields VectorTypeBits;
2292     TemplateTypeParmTypeBitfields TemplateTypeParmTypeBits;
2293     SubstTemplateTypeParmTypeBitfields SubstTemplateTypeParmTypeBits;
2294     SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits;
2295     TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits;
2296     DependentTemplateSpecializationTypeBitfields
2297       DependentTemplateSpecializationTypeBits;
2298     PackExpansionTypeBitfields PackExpansionTypeBits;
2299     CountAttributedTypeBitfields CountAttributedTypeBits;
2300   };
2301 
2302 private:
2303   template <class T> friend class TypePropertyCache;
2304 
2305   /// Set whether this type comes from an AST file.
2306   void setFromAST(bool V = true) const {
2307     TypeBits.FromAST = V;
2308   }
2309 
2310 protected:
2311   friend class ASTContext;
2312 
2313   Type(TypeClass tc, QualType canon, TypeDependence Dependence)
2314       : ExtQualsTypeCommonBase(this,
2315                                canon.isNull() ? QualType(this_(), 0) : canon) {
2316     static_assert(sizeof(*this) <=
2317                       alignof(decltype(*this)) + sizeof(ExtQualsTypeCommonBase),
2318                   "changing bitfields changed sizeof(Type)!");
2319     static_assert(alignof(decltype(*this)) % TypeAlignment == 0,
2320                   "Insufficient alignment!");
2321     TypeBits.TC = tc;
2322     TypeBits.Dependence = static_cast<unsigned>(Dependence);
2323     TypeBits.CacheValid = false;
2324     TypeBits.CachedLocalOrUnnamed = false;
2325     TypeBits.CachedLinkage = llvm::to_underlying(Linkage::Invalid);
2326     TypeBits.FromAST = false;
2327   }
2328 
2329   // silence VC++ warning C4355: 'this' : used in base member initializer list
2330   Type *this_() { return this; }
2331 
2332   void setDependence(TypeDependence D) {
2333     TypeBits.Dependence = static_cast<unsigned>(D);
2334   }
2335 
2336   void addDependence(TypeDependence D) { setDependence(getDependence() | D); }
2337 
2338 public:
2339   friend class ASTReader;
2340   friend class ASTWriter;
2341   template <class T> friend class serialization::AbstractTypeReader;
2342   template <class T> friend class serialization::AbstractTypeWriter;
2343 
2344   Type(const Type &) = delete;
2345   Type(Type &&) = delete;
2346   Type &operator=(const Type &) = delete;
2347   Type &operator=(Type &&) = delete;
2348 
2349   TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); }
2350 
2351   /// Whether this type comes from an AST file.
2352   bool isFromAST() const { return TypeBits.FromAST; }
2353 
2354   /// Whether this type is or contains an unexpanded parameter
2355   /// pack, used to support C++0x variadic templates.
2356   ///
2357   /// A type that contains a parameter pack shall be expanded by the
2358   /// ellipsis operator at some point. For example, the typedef in the
2359   /// following example contains an unexpanded parameter pack 'T':
2360   ///
2361   /// \code
2362   /// template<typename ...T>
2363   /// struct X {
2364   ///   typedef T* pointer_types; // ill-formed; T is a parameter pack.
2365   /// };
2366   /// \endcode
2367   ///
2368   /// Note that this routine does not specify which
2369   bool containsUnexpandedParameterPack() const {
2370     return getDependence() & TypeDependence::UnexpandedPack;
2371   }
2372 
2373   /// Determines if this type would be canonical if it had no further
2374   /// qualification.
2375   bool isCanonicalUnqualified() const {
2376     return CanonicalType == QualType(this, 0);
2377   }
2378 
2379   /// Pull a single level of sugar off of this locally-unqualified type.
2380   /// Users should generally prefer SplitQualType::getSingleStepDesugaredType()
2381   /// or QualType::getSingleStepDesugaredType(const ASTContext&).
2382   QualType getLocallyUnqualifiedSingleStepDesugaredType() const;
2383 
2384   /// As an extension, we classify types as one of "sized" or "sizeless";
2385   /// every type is one or the other.  Standard types are all sized;
2386   /// sizeless types are purely an extension.
2387   ///
2388   /// Sizeless types contain data with no specified size, alignment,
2389   /// or layout.
2390   bool isSizelessType() const;
2391   bool isSizelessBuiltinType() const;
2392 
2393   /// Returns true for all scalable vector types.
2394   bool isSizelessVectorType() const;
2395 
2396   /// Returns true for SVE scalable vector types.
2397   bool isSVESizelessBuiltinType() const;
2398 
2399   /// Returns true for RVV scalable vector types.
2400   bool isRVVSizelessBuiltinType() const;
2401 
2402   /// Check if this is a WebAssembly Externref Type.
2403   bool isWebAssemblyExternrefType() const;
2404 
2405   /// Returns true if this is a WebAssembly table type: either an array of
2406   /// reference types, or a pointer to a reference type (which can only be
2407   /// created by array to pointer decay).
2408   bool isWebAssemblyTableType() const;
2409 
2410   /// Determines if this is a sizeless type supported by the
2411   /// 'arm_sve_vector_bits' type attribute, which can be applied to a single
2412   /// SVE vector or predicate, excluding tuple types such as svint32x4_t.
2413   bool isSveVLSBuiltinType() const;
2414 
2415   /// Returns the representative type for the element of an SVE builtin type.
2416   /// This is used to represent fixed-length SVE vectors created with the
2417   /// 'arm_sve_vector_bits' type attribute as VectorType.
2418   QualType getSveEltType(const ASTContext &Ctx) const;
2419 
2420   /// Determines if this is a sizeless type supported by the
2421   /// 'riscv_rvv_vector_bits' type attribute, which can be applied to a single
2422   /// RVV vector or mask.
2423   bool isRVVVLSBuiltinType() const;
2424 
2425   /// Returns the representative type for the element of an RVV builtin type.
2426   /// This is used to represent fixed-length RVV vectors created with the
2427   /// 'riscv_rvv_vector_bits' type attribute as VectorType.
2428   QualType getRVVEltType(const ASTContext &Ctx) const;
2429 
2430   /// Returns the representative type for the element of a sizeless vector
2431   /// builtin type.
2432   QualType getSizelessVectorEltType(const ASTContext &Ctx) const;
2433 
2434   /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
2435   /// object types, function types, and incomplete types.
2436 
2437   /// Return true if this is an incomplete type.
2438   /// A type that can describe objects, but which lacks information needed to
2439   /// determine its size (e.g. void, or a fwd declared struct). Clients of this
2440   /// routine will need to determine if the size is actually required.
2441   ///
2442   /// Def If non-null, and the type refers to some kind of declaration
2443   /// that can be completed (such as a C struct, C++ class, or Objective-C
2444   /// class), will be set to the declaration.
2445   bool isIncompleteType(NamedDecl **Def = nullptr) const;
2446 
2447   /// Return true if this is an incomplete or object
2448   /// type, in other words, not a function type.
2449   bool isIncompleteOrObjectType() const {
2450     return !isFunctionType();
2451   }
2452 
2453   /// \returns True if the type is incomplete and it is also a type that
2454   /// cannot be completed by a later type definition.
2455   ///
2456   /// E.g. For `void` this is true but for `struct ForwardDecl;` this is false
2457   /// because a definition for `ForwardDecl` could be provided later on in the
2458   /// translation unit.
2459   ///
2460   /// Note even for types that this function returns true for it is still
2461   /// possible for the declarations that contain this type to later have a
2462   /// complete type in a translation unit. E.g.:
2463   ///
2464   /// \code{.c}
2465   /// // This decl has type 'char[]' which is incomplete and cannot be later
2466   /// // completed by another by another type declaration.
2467   /// extern char foo[];
2468   /// // This decl now has complete type 'char[5]'.
2469   /// char foo[5]; // foo has a complete type
2470   /// \endcode
2471   bool isAlwaysIncompleteType() const;
2472 
2473   /// Determine whether this type is an object type.
2474   bool isObjectType() const {
2475     // C++ [basic.types]p8:
2476     //   An object type is a (possibly cv-qualified) type that is not a
2477     //   function type, not a reference type, and not a void type.
2478     return !isReferenceType() && !isFunctionType() && !isVoidType();
2479   }
2480 
2481   /// Return true if this is a literal type
2482   /// (C++11 [basic.types]p10)
2483   bool isLiteralType(const ASTContext &Ctx) const;
2484 
2485   /// Determine if this type is a structural type, per C++20 [temp.param]p7.
2486   bool isStructuralType() const;
2487 
2488   /// Test if this type is a standard-layout type.
2489   /// (C++0x [basic.type]p9)
2490   bool isStandardLayoutType() const;
2491 
2492   /// Helper methods to distinguish type categories. All type predicates
2493   /// operate on the canonical type, ignoring typedefs and qualifiers.
2494 
2495   /// Returns true if the type is a builtin type.
2496   bool isBuiltinType() const;
2497 
2498   /// Test for a particular builtin type.
2499   bool isSpecificBuiltinType(unsigned K) const;
2500 
2501   /// Test for a type which does not represent an actual type-system type but
2502   /// is instead used as a placeholder for various convenient purposes within
2503   /// Clang.  All such types are BuiltinTypes.
2504   bool isPlaceholderType() const;
2505   const BuiltinType *getAsPlaceholderType() const;
2506 
2507   /// Test for a specific placeholder type.
2508   bool isSpecificPlaceholderType(unsigned K) const;
2509 
2510   /// Test for a placeholder type other than Overload; see
2511   /// BuiltinType::isNonOverloadPlaceholderType.
2512   bool isNonOverloadPlaceholderType() const;
2513 
2514   /// isIntegerType() does *not* include complex integers (a GCC extension).
2515   /// isComplexIntegerType() can be used to test for complex integers.
2516   bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
2517   bool isEnumeralType() const;
2518 
2519   /// Determine whether this type is a scoped enumeration type.
2520   bool isScopedEnumeralType() const;
2521   bool isBooleanType() const;
2522   bool isCharType() const;
2523   bool isWideCharType() const;
2524   bool isChar8Type() const;
2525   bool isChar16Type() const;
2526   bool isChar32Type() const;
2527   bool isAnyCharacterType() const;
2528   bool isUnicodeCharacterType() const;
2529   bool isIntegralType(const ASTContext &Ctx) const;
2530 
2531   /// Determine whether this type is an integral or enumeration type.
2532   bool isIntegralOrEnumerationType() const;
2533 
2534   /// Determine whether this type is an integral or unscoped enumeration type.
2535   bool isIntegralOrUnscopedEnumerationType() const;
2536   bool isUnscopedEnumerationType() const;
2537 
2538   /// Floating point categories.
2539   bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
2540   /// isComplexType() does *not* include complex integers (a GCC extension).
2541   /// isComplexIntegerType() can be used to test for complex integers.
2542   bool isComplexType() const;      // C99 6.2.5p11 (complex)
2543   bool isAnyComplexType() const;   // C99 6.2.5p11 (complex) + Complex Int.
2544   bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
2545   bool isHalfType() const;         // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half)
2546   bool isFloat16Type() const;      // C11 extension ISO/IEC TS 18661
2547   bool isFloat32Type() const;
2548   bool isDoubleType() const;
2549   bool isBFloat16Type() const;
2550   bool isMFloat8Type() const;
2551   bool isFloat128Type() const;
2552   bool isIbm128Type() const;
2553   bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
2554   bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
2555   bool isVoidType() const;         // C99 6.2.5p19
2556   bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
2557   bool isAggregateType() const;
2558   bool isFundamentalType() const;
2559   bool isCompoundType() const;
2560 
2561   // Type Predicates: Check to see if this type is structurally the specified
2562   // type, ignoring typedefs and qualifiers.
2563   bool isFunctionType() const;
2564   bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); }
2565   bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); }
2566   bool isPointerType() const;
2567   bool isPointerOrReferenceType() const;
2568   bool isSignableType(const ASTContext &Ctx) const;
2569   bool isSignablePointerType() const;
2570   bool isSignableIntegerType(const ASTContext &Ctx) const;
2571   bool isAnyPointerType() const;   // Any C pointer or ObjC object pointer
2572   bool isCountAttributedType() const;
2573   bool isCFIUncheckedCalleeFunctionType() const;
2574   bool hasPointeeToToCFIUncheckedCalleeFunctionType() const;
2575   bool isBlockPointerType() const;
2576   bool isVoidPointerType() const;
2577   bool isReferenceType() const;
2578   bool isLValueReferenceType() const;
2579   bool isRValueReferenceType() const;
2580   bool isObjectPointerType() const;
2581   bool isFunctionPointerType() const;
2582   bool isFunctionReferenceType() const;
2583   bool isMemberPointerType() const;
2584   bool isMemberFunctionPointerType() const;
2585   bool isMemberDataPointerType() const;
2586   bool isArrayType() const;
2587   bool isConstantArrayType() const;
2588   bool isIncompleteArrayType() const;
2589   bool isVariableArrayType() const;
2590   bool isArrayParameterType() const;
2591   bool isDependentSizedArrayType() const;
2592   bool isRecordType() const;
2593   bool isClassType() const;
2594   bool isStructureType() const;
2595   bool isStructureTypeWithFlexibleArrayMember() const;
2596   bool isObjCBoxableRecordType() const;
2597   bool isInterfaceType() const;
2598   bool isStructureOrClassType() const;
2599   bool isUnionType() const;
2600   bool isComplexIntegerType() const;            // GCC _Complex integer type.
2601   bool isVectorType() const;                    // GCC vector type.
2602   bool isExtVectorType() const;                 // Extended vector type.
2603   bool isExtVectorBoolType() const;             // Extended vector type with bool element.
2604   // Extended vector type with bool element that is packed. HLSL doesn't pack
2605   // its bool vectors.
2606   bool isPackedVectorBoolType(const ASTContext &ctx) const;
2607   bool isSubscriptableVectorType() const;
2608   bool isMatrixType() const;                    // Matrix type.
2609   bool isConstantMatrixType() const;            // Constant matrix type.
2610   bool isDependentAddressSpaceType() const;     // value-dependent address space qualifier
2611   bool isObjCObjectPointerType() const;         // pointer to ObjC object
2612   bool isObjCRetainableType() const;            // ObjC object or block pointer
2613   bool isObjCLifetimeType() const;              // (array of)* retainable type
2614   bool isObjCIndirectLifetimeType() const;      // (pointer to)* lifetime type
2615   bool isObjCNSObjectType() const;              // __attribute__((NSObject))
2616   bool isObjCIndependentClassType() const;      // __attribute__((objc_independent_class))
2617   // FIXME: change this to 'raw' interface type, so we can used 'interface' type
2618   // for the common case.
2619   bool isObjCObjectType() const;                // NSString or typeof(*(id)0)
2620   bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
2621   bool isObjCQualifiedIdType() const;           // id<foo>
2622   bool isObjCQualifiedClassType() const;        // Class<foo>
2623   bool isObjCObjectOrInterfaceType() const;
2624   bool isObjCIdType() const;                    // id
2625   bool isDecltypeType() const;
2626   /// Was this type written with the special inert-in-ARC __unsafe_unretained
2627   /// qualifier?
2628   ///
2629   /// This approximates the answer to the following question: if this
2630   /// translation unit were compiled in ARC, would this type be qualified
2631   /// with __unsafe_unretained?
2632   bool isObjCInertUnsafeUnretainedType() const {
2633     return hasAttr(attr::ObjCInertUnsafeUnretained);
2634   }
2635 
2636   /// Whether the type is Objective-C 'id' or a __kindof type of an
2637   /// object type, e.g., __kindof NSView * or __kindof id
2638   /// <NSCopying>.
2639   ///
2640   /// \param bound Will be set to the bound on non-id subtype types,
2641   /// which will be (possibly specialized) Objective-C class type, or
2642   /// null for 'id.
2643   bool isObjCIdOrObjectKindOfType(const ASTContext &ctx,
2644                                   const ObjCObjectType *&bound) const;
2645 
2646   bool isObjCClassType() const;                 // Class
2647 
2648   /// Whether the type is Objective-C 'Class' or a __kindof type of an
2649   /// Class type, e.g., __kindof Class <NSCopying>.
2650   ///
2651   /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound
2652   /// here because Objective-C's type system cannot express "a class
2653   /// object for a subclass of NSFoo".
2654   bool isObjCClassOrClassKindOfType() const;
2655 
2656   bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const;
2657   bool isObjCSelType() const;                 // Class
2658   bool isObjCBuiltinType() const;               // 'id' or 'Class'
2659   bool isObjCARCBridgableType() const;
2660   bool isCARCBridgableType() const;
2661   bool isTemplateTypeParmType() const;          // C++ template type parameter
2662   bool isNullPtrType() const;                   // C++11 std::nullptr_t or
2663                                                 // C23   nullptr_t
2664   bool isNothrowT() const;                      // C++   std::nothrow_t
2665   bool isAlignValT() const;                     // C++17 std::align_val_t
2666   bool isStdByteType() const;                   // C++17 std::byte
2667   bool isAtomicType() const;                    // C11 _Atomic()
2668   bool isUndeducedAutoType() const;             // C++11 auto or
2669                                                 // C++14 decltype(auto)
2670   bool isTypedefNameType() const;               // typedef or alias template
2671 
2672 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2673   bool is##Id##Type() const;
2674 #include "clang/Basic/OpenCLImageTypes.def"
2675 
2676   bool isImageType() const;                     // Any OpenCL image type
2677 
2678   bool isSamplerT() const;                      // OpenCL sampler_t
2679   bool isEventT() const;                        // OpenCL event_t
2680   bool isClkEventT() const;                     // OpenCL clk_event_t
2681   bool isQueueT() const;                        // OpenCL queue_t
2682   bool isReserveIDT() const;                    // OpenCL reserve_id_t
2683 
2684 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2685   bool is##Id##Type() const;
2686 #include "clang/Basic/OpenCLExtensionTypes.def"
2687   // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension
2688   bool isOCLIntelSubgroupAVCType() const;
2689   bool isOCLExtOpaqueType() const;              // Any OpenCL extension type
2690 
2691   bool isPipeType() const;                      // OpenCL pipe type
2692   bool isBitIntType() const;                    // Bit-precise integer type
2693   bool isOpenCLSpecificType() const;            // Any OpenCL specific type
2694 
2695 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) bool is##Id##Type() const;
2696 #include "clang/Basic/HLSLIntangibleTypes.def"
2697   bool isHLSLSpecificType() const; // Any HLSL specific type
2698   bool isHLSLBuiltinIntangibleType() const; // Any HLSL builtin intangible type
2699   bool isHLSLAttributedResourceType() const;
2700   bool isHLSLInlineSpirvType() const;
2701   bool isHLSLResourceRecord() const;
2702   bool isHLSLIntangibleType()
2703       const; // Any HLSL intangible type (builtin, array, class)
2704 
2705   /// Determines if this type, which must satisfy
2706   /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather
2707   /// than implicitly __strong.
2708   bool isObjCARCImplicitlyUnretainedType() const;
2709 
2710   /// Check if the type is the CUDA device builtin surface type.
2711   bool isCUDADeviceBuiltinSurfaceType() const;
2712   /// Check if the type is the CUDA device builtin texture type.
2713   bool isCUDADeviceBuiltinTextureType() const;
2714 
2715   /// Return the implicit lifetime for this type, which must not be dependent.
2716   Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const;
2717 
2718   enum ScalarTypeKind {
2719     STK_CPointer,
2720     STK_BlockPointer,
2721     STK_ObjCObjectPointer,
2722     STK_MemberPointer,
2723     STK_Bool,
2724     STK_Integral,
2725     STK_Floating,
2726     STK_IntegralComplex,
2727     STK_FloatingComplex,
2728     STK_FixedPoint
2729   };
2730 
2731   /// Given that this is a scalar type, classify it.
2732   ScalarTypeKind getScalarTypeKind() const;
2733 
2734   TypeDependence getDependence() const {
2735     return static_cast<TypeDependence>(TypeBits.Dependence);
2736   }
2737 
2738   /// Whether this type is an error type.
2739   bool containsErrors() const {
2740     return getDependence() & TypeDependence::Error;
2741   }
2742 
2743   /// Whether this type is a dependent type, meaning that its definition
2744   /// somehow depends on a template parameter (C++ [temp.dep.type]).
2745   bool isDependentType() const {
2746     return getDependence() & TypeDependence::Dependent;
2747   }
2748 
2749   /// Determine whether this type is an instantiation-dependent type,
2750   /// meaning that the type involves a template parameter (even if the
2751   /// definition does not actually depend on the type substituted for that
2752   /// template parameter).
2753   bool isInstantiationDependentType() const {
2754     return getDependence() & TypeDependence::Instantiation;
2755   }
2756 
2757   /// Determine whether this type is an undeduced type, meaning that
2758   /// it somehow involves a C++11 'auto' type or similar which has not yet been
2759   /// deduced.
2760   bool isUndeducedType() const;
2761 
2762   /// Whether this type is a variably-modified type (C99 6.7.5).
2763   bool isVariablyModifiedType() const {
2764     return getDependence() & TypeDependence::VariablyModified;
2765   }
2766 
2767   /// Whether this type involves a variable-length array type
2768   /// with a definite size.
2769   bool hasSizedVLAType() const;
2770 
2771   /// Whether this type is or contains a local or unnamed type.
2772   bool hasUnnamedOrLocalType() const;
2773 
2774   bool isOverloadableType() const;
2775 
2776   /// Determine wither this type is a C++ elaborated-type-specifier.
2777   bool isElaboratedTypeSpecifier() const;
2778 
2779   bool canDecayToPointerType() const;
2780 
2781   /// Whether this type is represented natively as a pointer.  This includes
2782   /// pointers, references, block pointers, and Objective-C interface,
2783   /// qualified id, and qualified interface types, as well as nullptr_t.
2784   bool hasPointerRepresentation() const;
2785 
2786   /// Whether this type can represent an objective pointer type for the
2787   /// purpose of GC'ability
2788   bool hasObjCPointerRepresentation() const;
2789 
2790   /// Determine whether this type has an integer representation
2791   /// of some sort, e.g., it is an integer type or a vector.
2792   bool hasIntegerRepresentation() const;
2793 
2794   /// Determine whether this type has an signed integer representation
2795   /// of some sort, e.g., it is an signed integer type or a vector.
2796   bool hasSignedIntegerRepresentation() const;
2797 
2798   /// Determine whether this type has an unsigned integer representation
2799   /// of some sort, e.g., it is an unsigned integer type or a vector.
2800   bool hasUnsignedIntegerRepresentation() const;
2801 
2802   /// Determine whether this type has a floating-point representation
2803   /// of some sort, e.g., it is a floating-point type or a vector thereof.
2804   bool hasFloatingRepresentation() const;
2805 
2806   /// Determine whether this type has a boolean representation -- i.e., it is a
2807   /// boolean type, an enum type whose underlying type is a boolean type, or a
2808   /// vector of booleans.
2809   bool hasBooleanRepresentation() const;
2810 
2811   // Type Checking Functions: Check to see if this type is structurally the
2812   // specified type, ignoring typedefs and qualifiers, and return a pointer to
2813   // the best type we can.
2814   const RecordType *getAsStructureType() const;
2815   /// NOTE: getAs*ArrayType are methods on ASTContext.
2816   const RecordType *getAsUnionType() const;
2817   const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
2818   const ObjCObjectType *getAsObjCInterfaceType() const;
2819 
2820   // The following is a convenience method that returns an ObjCObjectPointerType
2821   // for object declared using an interface.
2822   const ObjCObjectPointerType *getAsObjCInterfacePointerType() const;
2823   const ObjCObjectPointerType *getAsObjCQualifiedIdType() const;
2824   const ObjCObjectPointerType *getAsObjCQualifiedClassType() const;
2825   const ObjCObjectType *getAsObjCQualifiedInterfaceType() const;
2826 
2827   /// Retrieves the CXXRecordDecl that this type refers to, either
2828   /// because the type is a RecordType or because it is the injected-class-name
2829   /// type of a class template or class template partial specialization.
2830   CXXRecordDecl *getAsCXXRecordDecl() const;
2831 
2832   /// Retrieves the RecordDecl this type refers to.
2833   RecordDecl *getAsRecordDecl() const;
2834 
2835   /// Retrieves the TagDecl that this type refers to, either
2836   /// because the type is a TagType or because it is the injected-class-name
2837   /// type of a class template or class template partial specialization.
2838   TagDecl *getAsTagDecl() const;
2839 
2840   /// If this is a pointer or reference to a RecordType, return the
2841   /// CXXRecordDecl that the type refers to.
2842   ///
2843   /// If this is not a pointer or reference, or the type being pointed to does
2844   /// not refer to a CXXRecordDecl, returns NULL.
2845   const CXXRecordDecl *getPointeeCXXRecordDecl() const;
2846 
2847   /// Get the DeducedType whose type will be deduced for a variable with
2848   /// an initializer of this type. This looks through declarators like pointer
2849   /// types, but not through decltype or typedefs.
2850   DeducedType *getContainedDeducedType() const;
2851 
2852   /// Get the AutoType whose type will be deduced for a variable with
2853   /// an initializer of this type. This looks through declarators like pointer
2854   /// types, but not through decltype or typedefs.
2855   AutoType *getContainedAutoType() const {
2856     return dyn_cast_or_null<AutoType>(getContainedDeducedType());
2857   }
2858 
2859   /// Determine whether this type was written with a leading 'auto'
2860   /// corresponding to a trailing return type (possibly for a nested
2861   /// function type within a pointer to function type or similar).
2862   bool hasAutoForTrailingReturnType() const;
2863 
2864   /// Member-template getAs<specific type>'.  Look through sugar for
2865   /// an instance of \<specific type>.   This scheme will eventually
2866   /// replace the specific getAsXXXX methods above.
2867   ///
2868   /// There are some specializations of this member template listed
2869   /// immediately following this class.
2870   template <typename T> const T *getAs() const;
2871 
2872   /// Look through sugar for an instance of TemplateSpecializationType which
2873   /// is not a type alias, or null if there is no such type.
2874   /// This is used when you want as-written template arguments or the template
2875   /// name for a class template specialization.
2876   const TemplateSpecializationType *
2877   getAsNonAliasTemplateSpecializationType() const;
2878 
2879   const TemplateSpecializationType *
2880   castAsNonAliasTemplateSpecializationType() const {
2881     const auto *TST = getAsNonAliasTemplateSpecializationType();
2882     assert(TST && "not a TemplateSpecializationType");
2883     return TST;
2884   }
2885 
2886   /// Member-template getAsAdjusted<specific type>. Look through specific kinds
2887   /// of sugar (parens, attributes, etc) for an instance of \<specific type>.
2888   /// This is used when you need to walk over sugar nodes that represent some
2889   /// kind of type adjustment from a type that was written as a \<specific type>
2890   /// to another type that is still canonically a \<specific type>.
2891   template <typename T> const T *getAsAdjusted() const;
2892 
2893   /// A variant of getAs<> for array types which silently discards
2894   /// qualifiers from the outermost type.
2895   const ArrayType *getAsArrayTypeUnsafe() const;
2896 
2897   /// Member-template castAs<specific type>.  Look through sugar for
2898   /// the underlying instance of \<specific type>.
2899   ///
2900   /// This method has the same relationship to getAs<T> as cast<T> has
2901   /// to dyn_cast<T>; which is to say, the underlying type *must*
2902   /// have the intended type, and this method will never return null.
2903   template <typename T> const T *castAs() const;
2904 
2905   /// A variant of castAs<> for array type which silently discards
2906   /// qualifiers from the outermost type.
2907   const ArrayType *castAsArrayTypeUnsafe() const;
2908 
2909   /// Determine whether this type had the specified attribute applied to it
2910   /// (looking through top-level type sugar).
2911   bool hasAttr(attr::Kind AK) const;
2912 
2913   /// Get the base element type of this type, potentially discarding type
2914   /// qualifiers.  This should never be used when type qualifiers
2915   /// are meaningful.
2916   const Type *getBaseElementTypeUnsafe() const;
2917 
2918   /// If this is an array type, return the element type of the array,
2919   /// potentially with type qualifiers missing.
2920   /// This should never be used when type qualifiers are meaningful.
2921   const Type *getArrayElementTypeNoTypeQual() const;
2922 
2923   /// If this is a pointer type, return the pointee type.
2924   /// If this is an array type, return the array element type.
2925   /// This should never be used when type qualifiers are meaningful.
2926   const Type *getPointeeOrArrayElementType() const;
2927 
2928   /// If this is a pointer, ObjC object pointer, or block
2929   /// pointer, this returns the respective pointee.
2930   QualType getPointeeType() const;
2931 
2932   /// Return the specified type with any "sugar" removed from the type,
2933   /// removing any typedefs, typeofs, etc., as well as any qualifiers.
2934   const Type *getUnqualifiedDesugaredType() const;
2935 
2936   /// Return true if this is an integer type that is
2937   /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
2938   /// or an enum decl which has a signed representation.
2939   bool isSignedIntegerType() const;
2940 
2941   /// Return true if this is an integer type that is
2942   /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool],
2943   /// or an enum decl which has an unsigned representation.
2944   bool isUnsignedIntegerType() const;
2945 
2946   /// Determines whether this is an integer type that is signed or an
2947   /// enumeration types whose underlying type is a signed integer type.
2948   bool isSignedIntegerOrEnumerationType() const;
2949 
2950   /// Determines whether this is an integer type that is unsigned or an
2951   /// enumeration types whose underlying type is a unsigned integer type.
2952   bool isUnsignedIntegerOrEnumerationType() const;
2953 
2954   /// Return true if this is a fixed point type according to
2955   /// ISO/IEC JTC1 SC22 WG14 N1169.
2956   bool isFixedPointType() const;
2957 
2958   /// Return true if this is a fixed point or integer type.
2959   bool isFixedPointOrIntegerType() const;
2960 
2961   /// Return true if this can be converted to (or from) a fixed point type.
2962   bool isConvertibleToFixedPointType() const;
2963 
2964   /// Return true if this is a saturated fixed point type according to
2965   /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2966   bool isSaturatedFixedPointType() const;
2967 
2968   /// Return true if this is a saturated fixed point type according to
2969   /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2970   bool isUnsaturatedFixedPointType() const;
2971 
2972   /// Return true if this is a fixed point type that is signed according
2973   /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2974   bool isSignedFixedPointType() const;
2975 
2976   /// Return true if this is a fixed point type that is unsigned according
2977   /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2978   bool isUnsignedFixedPointType() const;
2979 
2980   /// Return true if this is not a variable sized type,
2981   /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
2982   /// incomplete types.
2983   bool isConstantSizeType() const;
2984 
2985   /// Returns true if this type can be represented by some
2986   /// set of type specifiers.
2987   bool isSpecifierType() const;
2988 
2989   /// Determine the linkage of this type.
2990   Linkage getLinkage() const;
2991 
2992   /// Determine the visibility of this type.
2993   Visibility getVisibility() const {
2994     return getLinkageAndVisibility().getVisibility();
2995   }
2996 
2997   /// Return true if the visibility was explicitly set is the code.
2998   bool isVisibilityExplicit() const {
2999     return getLinkageAndVisibility().isVisibilityExplicit();
3000   }
3001 
3002   /// Determine the linkage and visibility of this type.
3003   LinkageInfo getLinkageAndVisibility() const;
3004 
3005   /// True if the computed linkage is valid. Used for consistency
3006   /// checking. Should always return true.
3007   bool isLinkageValid() const;
3008 
3009   /// Determine the nullability of the given type.
3010   ///
3011   /// Note that nullability is only captured as sugar within the type
3012   /// system, not as part of the canonical type, so nullability will
3013   /// be lost by canonicalization and desugaring.
3014   std::optional<NullabilityKind> getNullability() const;
3015 
3016   /// Determine whether the given type can have a nullability
3017   /// specifier applied to it, i.e., if it is any kind of pointer type.
3018   ///
3019   /// \param ResultIfUnknown The value to return if we don't yet know whether
3020   ///        this type can have nullability because it is dependent.
3021   bool canHaveNullability(bool ResultIfUnknown = true) const;
3022 
3023   /// Retrieve the set of substitutions required when accessing a member
3024   /// of the Objective-C receiver type that is declared in the given context.
3025   ///
3026   /// \c *this is the type of the object we're operating on, e.g., the
3027   /// receiver for a message send or the base of a property access, and is
3028   /// expected to be of some object or object pointer type.
3029   ///
3030   /// \param dc The declaration context for which we are building up a
3031   /// substitution mapping, which should be an Objective-C class, extension,
3032   /// category, or method within.
3033   ///
3034   /// \returns an array of type arguments that can be substituted for
3035   /// the type parameters of the given declaration context in any type described
3036   /// within that context, or an empty optional to indicate that no
3037   /// substitution is required.
3038   std::optional<ArrayRef<QualType>>
3039   getObjCSubstitutions(const DeclContext *dc) const;
3040 
3041   /// Determines if this is an ObjC interface type that may accept type
3042   /// parameters.
3043   bool acceptsObjCTypeParams() const;
3044 
3045   const char *getTypeClassName() const;
3046 
3047   QualType getCanonicalTypeInternal() const {
3048     return CanonicalType;
3049   }
3050 
3051   CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h
3052   void dump() const;
3053   void dump(llvm::raw_ostream &OS, const ASTContext &Context) const;
3054 };
3055 
3056 /// This will check for a TypedefType by removing any existing sugar
3057 /// until it reaches a TypedefType or a non-sugared type.
3058 template <> const TypedefType *Type::getAs() const;
3059 template <> const UsingType *Type::getAs() const;
3060 
3061 /// This will check for a TemplateSpecializationType by removing any
3062 /// existing sugar until it reaches a TemplateSpecializationType or a
3063 /// non-sugared type.
3064 template <> const TemplateSpecializationType *Type::getAs() const;
3065 
3066 /// This will check for an AttributedType by removing any existing sugar
3067 /// until it reaches an AttributedType or a non-sugared type.
3068 template <> const AttributedType *Type::getAs() const;
3069 
3070 /// This will check for a BoundsAttributedType by removing any existing
3071 /// sugar until it reaches an BoundsAttributedType or a non-sugared type.
3072 template <> const BoundsAttributedType *Type::getAs() const;
3073 
3074 /// This will check for a CountAttributedType by removing any existing
3075 /// sugar until it reaches an CountAttributedType or a non-sugared type.
3076 template <> const CountAttributedType *Type::getAs() const;
3077 
3078 // We can do canonical leaf types faster, because we don't have to
3079 // worry about preserving child type decoration.
3080 #define TYPE(Class, Base)
3081 #define LEAF_TYPE(Class) \
3082 template <> inline const Class##Type *Type::getAs() const { \
3083   return dyn_cast<Class##Type>(CanonicalType); \
3084 } \
3085 template <> inline const Class##Type *Type::castAs() const { \
3086   return cast<Class##Type>(CanonicalType); \
3087 }
3088 #include "clang/AST/TypeNodes.inc"
3089 
3090 /// This class is used for builtin types like 'int'.  Builtin
3091 /// types are always canonical and have a literal name field.
3092 class BuiltinType : public Type {
3093 public:
3094   enum Kind {
3095 // OpenCL image types
3096 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id,
3097 #include "clang/Basic/OpenCLImageTypes.def"
3098 // OpenCL extension types
3099 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id,
3100 #include "clang/Basic/OpenCLExtensionTypes.def"
3101 // SVE Types
3102 #define SVE_TYPE(Name, Id, SingletonId) Id,
3103 #include "clang/Basic/AArch64ACLETypes.def"
3104 // PPC MMA Types
3105 #define PPC_VECTOR_TYPE(Name, Id, Size) Id,
3106 #include "clang/Basic/PPCTypes.def"
3107 // RVV Types
3108 #define RVV_TYPE(Name, Id, SingletonId) Id,
3109 #include "clang/Basic/RISCVVTypes.def"
3110 // WebAssembly reference types
3111 #define WASM_TYPE(Name, Id, SingletonId) Id,
3112 #include "clang/Basic/WebAssemblyReferenceTypes.def"
3113 // AMDGPU types
3114 #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) Id,
3115 #include "clang/Basic/AMDGPUTypes.def"
3116 // HLSL intangible Types
3117 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) Id,
3118 #include "clang/Basic/HLSLIntangibleTypes.def"
3119 // All other builtin types
3120 #define BUILTIN_TYPE(Id, SingletonId) Id,
3121 #define LAST_BUILTIN_TYPE(Id) LastKind = Id
3122 #include "clang/AST/BuiltinTypes.def"
3123   };
3124 
3125 private:
3126   friend class ASTContext; // ASTContext creates these.
3127 
3128   BuiltinType(Kind K)
3129       : Type(Builtin, QualType(),
3130              K == Dependent ? TypeDependence::DependentInstantiation
3131                             : TypeDependence::None) {
3132     static_assert(Kind::LastKind <
3133                       (1 << BuiltinTypeBitfields::NumOfBuiltinTypeBits) &&
3134                   "Defined builtin type exceeds the allocated space for serial "
3135                   "numbering");
3136     BuiltinTypeBits.Kind = K;
3137   }
3138 
3139 public:
3140   Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
3141   StringRef getName(const PrintingPolicy &Policy) const;
3142 
3143   const char *getNameAsCString(const PrintingPolicy &Policy) const {
3144     // The StringRef is null-terminated.
3145     StringRef str = getName(Policy);
3146     assert(!str.empty() && str.data()[str.size()] == '\0');
3147     return str.data();
3148   }
3149 
3150   bool isSugared() const { return false; }
3151   QualType desugar() const { return QualType(this, 0); }
3152 
3153   bool isInteger() const {
3154     return getKind() >= Bool && getKind() <= Int128;
3155   }
3156 
3157   bool isSignedInteger() const {
3158     return getKind() >= Char_S && getKind() <= Int128;
3159   }
3160 
3161   bool isUnsignedInteger() const {
3162     return getKind() >= Bool && getKind() <= UInt128;
3163   }
3164 
3165   bool isFloatingPoint() const {
3166     return getKind() >= Half && getKind() <= Ibm128;
3167   }
3168 
3169   bool isSVEBool() const { return getKind() == Kind::SveBool; }
3170 
3171   bool isSVECount() const { return getKind() == Kind::SveCount; }
3172 
3173   /// Determines whether the given kind corresponds to a placeholder type.
3174   static bool isPlaceholderTypeKind(Kind K) {
3175     return K >= Overload;
3176   }
3177 
3178   /// Determines whether this type is a placeholder type, i.e. a type
3179   /// which cannot appear in arbitrary positions in a fully-formed
3180   /// expression.
3181   bool isPlaceholderType() const {
3182     return isPlaceholderTypeKind(getKind());
3183   }
3184 
3185   /// Determines whether this type is a placeholder type other than
3186   /// Overload.  Most placeholder types require only syntactic
3187   /// information about their context in order to be resolved (e.g.
3188   /// whether it is a call expression), which means they can (and
3189   /// should) be resolved in an earlier "phase" of analysis.
3190   /// Overload expressions sometimes pick up further information
3191   /// from their context, like whether the context expects a
3192   /// specific function-pointer type, and so frequently need
3193   /// special treatment.
3194   bool isNonOverloadPlaceholderType() const {
3195     return getKind() > Overload;
3196   }
3197 
3198   static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
3199 };
3200 
3201 /// Complex values, per C99 6.2.5p11.  This supports the C99 complex
3202 /// types (_Complex float etc) as well as the GCC integer complex extensions.
3203 class ComplexType : public Type, public llvm::FoldingSetNode {
3204   friend class ASTContext; // ASTContext creates these.
3205 
3206   QualType ElementType;
3207 
3208   ComplexType(QualType Element, QualType CanonicalPtr)
3209       : Type(Complex, CanonicalPtr, Element->getDependence()),
3210         ElementType(Element) {}
3211 
3212 public:
3213   QualType getElementType() const { return ElementType; }
3214 
3215   bool isSugared() const { return false; }
3216   QualType desugar() const { return QualType(this, 0); }
3217 
3218   void Profile(llvm::FoldingSetNodeID &ID) {
3219     Profile(ID, getElementType());
3220   }
3221 
3222   static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
3223     ID.AddPointer(Element.getAsOpaquePtr());
3224   }
3225 
3226   static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
3227 };
3228 
3229 /// Sugar for parentheses used when specifying types.
3230 class ParenType : public Type, public llvm::FoldingSetNode {
3231   friend class ASTContext; // ASTContext creates these.
3232 
3233   QualType Inner;
3234 
3235   ParenType(QualType InnerType, QualType CanonType)
3236       : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {}
3237 
3238 public:
3239   QualType getInnerType() const { return Inner; }
3240 
3241   bool isSugared() const { return true; }
3242   QualType desugar() const { return getInnerType(); }
3243 
3244   void Profile(llvm::FoldingSetNodeID &ID) {
3245     Profile(ID, getInnerType());
3246   }
3247 
3248   static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) {
3249     Inner.Profile(ID);
3250   }
3251 
3252   static bool classof(const Type *T) { return T->getTypeClass() == Paren; }
3253 };
3254 
3255 /// PointerType - C99 6.7.5.1 - Pointer Declarators.
3256 class PointerType : public Type, public llvm::FoldingSetNode {
3257   friend class ASTContext; // ASTContext creates these.
3258 
3259   QualType PointeeType;
3260 
3261   PointerType(QualType Pointee, QualType CanonicalPtr)
3262       : Type(Pointer, CanonicalPtr, Pointee->getDependence()),
3263         PointeeType(Pointee) {}
3264 
3265 public:
3266   QualType getPointeeType() const { return PointeeType; }
3267 
3268   bool isSugared() const { return false; }
3269   QualType desugar() const { return QualType(this, 0); }
3270 
3271   void Profile(llvm::FoldingSetNodeID &ID) {
3272     Profile(ID, getPointeeType());
3273   }
3274 
3275   static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
3276     ID.AddPointer(Pointee.getAsOpaquePtr());
3277   }
3278 
3279   static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
3280 };
3281 
3282 /// [BoundsSafety] Represents information of declarations referenced by the
3283 /// arguments of the `counted_by` attribute and the likes.
3284 class TypeCoupledDeclRefInfo {
3285 public:
3286   using BaseTy = llvm::PointerIntPair<ValueDecl *, 1, unsigned>;
3287 
3288 private:
3289   enum {
3290     DerefShift = 0,
3291     DerefMask = 1,
3292   };
3293   BaseTy Data;
3294 
3295 public:
3296   /// \p D is to a declaration referenced by the argument of attribute. \p Deref
3297   /// indicates whether \p D is referenced as a dereferenced form, e.g., \p
3298   /// Deref is true for `*n` in `int *__counted_by(*n)`.
3299   TypeCoupledDeclRefInfo(ValueDecl *D = nullptr, bool Deref = false);
3300 
3301   bool isDeref() const;
3302   ValueDecl *getDecl() const;
3303   unsigned getInt() const;
3304   void *getOpaqueValue() const;
3305   bool operator==(const TypeCoupledDeclRefInfo &Other) const;
3306   void setFromOpaqueValue(void *V);
3307 };
3308 
3309 /// [BoundsSafety] Represents a parent type class for CountAttributedType and
3310 /// similar sugar types that will be introduced to represent a type with a
3311 /// bounds attribute.
3312 ///
3313 /// Provides a common interface to navigate declarations referred to by the
3314 /// bounds expression.
3315 
3316 class BoundsAttributedType : public Type, public llvm::FoldingSetNode {
3317   QualType WrappedTy;
3318 
3319 protected:
3320   ArrayRef<TypeCoupledDeclRefInfo> Decls; // stored in trailing objects
3321 
3322   BoundsAttributedType(TypeClass TC, QualType Wrapped, QualType Canon);
3323 
3324 public:
3325   bool isSugared() const { return true; }
3326   QualType desugar() const { return WrappedTy; }
3327 
3328   using decl_iterator = const TypeCoupledDeclRefInfo *;
3329   using decl_range = llvm::iterator_range<decl_iterator>;
3330 
3331   decl_iterator dependent_decl_begin() const { return Decls.begin(); }
3332   decl_iterator dependent_decl_end() const { return Decls.end(); }
3333 
3334   unsigned getNumCoupledDecls() const { return Decls.size(); }
3335 
3336   decl_range dependent_decls() const {
3337     return decl_range(dependent_decl_begin(), dependent_decl_end());
3338   }
3339 
3340   ArrayRef<TypeCoupledDeclRefInfo> getCoupledDecls() const {
3341     return {dependent_decl_begin(), dependent_decl_end()};
3342   }
3343 
3344   bool referencesFieldDecls() const;
3345 
3346   static bool classof(const Type *T) {
3347     // Currently, only `class CountAttributedType` inherits
3348     // `BoundsAttributedType` but the subclass will grow as we add more bounds
3349     // annotations.
3350     switch (T->getTypeClass()) {
3351     case CountAttributed:
3352       return true;
3353     default:
3354       return false;
3355     }
3356   }
3357 };
3358 
3359 /// Represents a sugar type with `__counted_by` or `__sized_by` annotations,
3360 /// including their `_or_null` variants.
3361 class CountAttributedType final
3362     : public BoundsAttributedType,
3363       public llvm::TrailingObjects<CountAttributedType,
3364                                    TypeCoupledDeclRefInfo> {
3365   friend class ASTContext;
3366 
3367   Expr *CountExpr;
3368   /// \p CountExpr represents the argument of __counted_by or the likes. \p
3369   /// CountInBytes indicates that \p CountExpr is a byte count (i.e.,
3370   /// __sized_by(_or_null)) \p OrNull means it's an or_null variant (i.e.,
3371   /// __counted_by_or_null or __sized_by_or_null) \p CoupledDecls contains the
3372   /// list of declarations referenced by \p CountExpr, which the type depends on
3373   /// for the bounds information.
3374   CountAttributedType(QualType Wrapped, QualType Canon, Expr *CountExpr,
3375                       bool CountInBytes, bool OrNull,
3376                       ArrayRef<TypeCoupledDeclRefInfo> CoupledDecls);
3377 
3378   unsigned numTrailingObjects(OverloadToken<TypeCoupledDeclRefInfo>) const {
3379     return CountAttributedTypeBits.NumCoupledDecls;
3380   }
3381 
3382 public:
3383   enum DynamicCountPointerKind {
3384     CountedBy = 0,
3385     SizedBy,
3386     CountedByOrNull,
3387     SizedByOrNull,
3388   };
3389 
3390   Expr *getCountExpr() const { return CountExpr; }
3391   bool isCountInBytes() const { return CountAttributedTypeBits.CountInBytes; }
3392   bool isOrNull() const { return CountAttributedTypeBits.OrNull; }
3393 
3394   DynamicCountPointerKind getKind() const {
3395     if (isOrNull())
3396       return isCountInBytes() ? SizedByOrNull : CountedByOrNull;
3397     return isCountInBytes() ? SizedBy : CountedBy;
3398   }
3399 
3400   void Profile(llvm::FoldingSetNodeID &ID) {
3401     Profile(ID, desugar(), CountExpr, isCountInBytes(), isOrNull());
3402   }
3403 
3404   static void Profile(llvm::FoldingSetNodeID &ID, QualType WrappedTy,
3405                       Expr *CountExpr, bool CountInBytes, bool Nullable);
3406 
3407   static bool classof(const Type *T) {
3408     return T->getTypeClass() == CountAttributed;
3409   }
3410 
3411   StringRef getAttributeName(bool WithMacroPrefix) const;
3412 };
3413 
3414 /// Represents a type which was implicitly adjusted by the semantic
3415 /// engine for arbitrary reasons.  For example, array and function types can
3416 /// decay, and function types can have their calling conventions adjusted.
3417 class AdjustedType : public Type, public llvm::FoldingSetNode {
3418   QualType OriginalTy;
3419   QualType AdjustedTy;
3420 
3421 protected:
3422   friend class ASTContext; // ASTContext creates these.
3423 
3424   AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy,
3425                QualType CanonicalPtr)
3426       : Type(TC, CanonicalPtr, OriginalTy->getDependence()),
3427         OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {}
3428 
3429 public:
3430   QualType getOriginalType() const { return OriginalTy; }
3431   QualType getAdjustedType() const { return AdjustedTy; }
3432 
3433   bool isSugared() const { return true; }
3434   QualType desugar() const { return AdjustedTy; }
3435 
3436   void Profile(llvm::FoldingSetNodeID &ID) {
3437     Profile(ID, OriginalTy, AdjustedTy);
3438   }
3439 
3440   static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) {
3441     ID.AddPointer(Orig.getAsOpaquePtr());
3442     ID.AddPointer(New.getAsOpaquePtr());
3443   }
3444 
3445   static bool classof(const Type *T) {
3446     return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed;
3447   }
3448 };
3449 
3450 /// Represents a pointer type decayed from an array or function type.
3451 class DecayedType : public AdjustedType {
3452   friend class ASTContext; // ASTContext creates these.
3453 
3454   inline
3455   DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical);
3456 
3457 public:
3458   QualType getDecayedType() const { return getAdjustedType(); }
3459 
3460   inline QualType getPointeeType() const;
3461 
3462   static bool classof(const Type *T) { return T->getTypeClass() == Decayed; }
3463 };
3464 
3465 /// Pointer to a block type.
3466 /// This type is to represent types syntactically represented as
3467 /// "void (^)(int)", etc. Pointee is required to always be a function type.
3468 class BlockPointerType : public Type, public llvm::FoldingSetNode {
3469   friend class ASTContext; // ASTContext creates these.
3470 
3471   // Block is some kind of pointer type
3472   QualType PointeeType;
3473 
3474   BlockPointerType(QualType Pointee, QualType CanonicalCls)
3475       : Type(BlockPointer, CanonicalCls, Pointee->getDependence()),
3476         PointeeType(Pointee) {}
3477 
3478 public:
3479   // Get the pointee type. Pointee is required to always be a function type.
3480   QualType getPointeeType() const { return PointeeType; }
3481 
3482   bool isSugared() const { return false; }
3483   QualType desugar() const { return QualType(this, 0); }
3484 
3485   void Profile(llvm::FoldingSetNodeID &ID) {
3486       Profile(ID, getPointeeType());
3487   }
3488 
3489   static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
3490       ID.AddPointer(Pointee.getAsOpaquePtr());
3491   }
3492 
3493   static bool classof(const Type *T) {
3494     return T->getTypeClass() == BlockPointer;
3495   }
3496 };
3497 
3498 /// Base for LValueReferenceType and RValueReferenceType
3499 class ReferenceType : public Type, public llvm::FoldingSetNode {
3500   QualType PointeeType;
3501 
3502 protected:
3503   ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef,
3504                 bool SpelledAsLValue)
3505       : Type(tc, CanonicalRef, Referencee->getDependence()),
3506         PointeeType(Referencee) {
3507     ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue;
3508     ReferenceTypeBits.InnerRef = Referencee->isReferenceType();
3509   }
3510 
3511 public:
3512   bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; }
3513   bool isInnerRef() const { return ReferenceTypeBits.InnerRef; }
3514 
3515   QualType getPointeeTypeAsWritten() const { return PointeeType; }
3516 
3517   QualType getPointeeType() const {
3518     // FIXME: this might strip inner qualifiers; okay?
3519     const ReferenceType *T = this;
3520     while (T->isInnerRef())
3521       T = T->PointeeType->castAs<ReferenceType>();
3522     return T->PointeeType;
3523   }
3524 
3525   void Profile(llvm::FoldingSetNodeID &ID) {
3526     Profile(ID, PointeeType, isSpelledAsLValue());
3527   }
3528 
3529   static void Profile(llvm::FoldingSetNodeID &ID,
3530                       QualType Referencee,
3531                       bool SpelledAsLValue) {
3532     ID.AddPointer(Referencee.getAsOpaquePtr());
3533     ID.AddBoolean(SpelledAsLValue);
3534   }
3535 
3536   static bool classof(const Type *T) {
3537     return T->getTypeClass() == LValueReference ||
3538            T->getTypeClass() == RValueReference;
3539   }
3540 };
3541 
3542 /// An lvalue reference type, per C++11 [dcl.ref].
3543 class LValueReferenceType : public ReferenceType {
3544   friend class ASTContext; // ASTContext creates these
3545 
3546   LValueReferenceType(QualType Referencee, QualType CanonicalRef,
3547                       bool SpelledAsLValue)
3548       : ReferenceType(LValueReference, Referencee, CanonicalRef,
3549                       SpelledAsLValue) {}
3550 
3551 public:
3552   bool isSugared() const { return false; }
3553   QualType desugar() const { return QualType(this, 0); }
3554 
3555   static bool classof(const Type *T) {
3556     return T->getTypeClass() == LValueReference;
3557   }
3558 };
3559 
3560 /// An rvalue reference type, per C++11 [dcl.ref].
3561 class RValueReferenceType : public ReferenceType {
3562   friend class ASTContext; // ASTContext creates these
3563 
3564   RValueReferenceType(QualType Referencee, QualType CanonicalRef)
3565        : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {}
3566 
3567 public:
3568   bool isSugared() const { return false; }
3569   QualType desugar() const { return QualType(this, 0); }
3570 
3571   static bool classof(const Type *T) {
3572     return T->getTypeClass() == RValueReference;
3573   }
3574 };
3575 
3576 /// A pointer to member type per C++ 8.3.3 - Pointers to members.
3577 ///
3578 /// This includes both pointers to data members and pointer to member functions.
3579 class MemberPointerType : public Type, public llvm::FoldingSetNode {
3580   friend class ASTContext; // ASTContext creates these.
3581 
3582   QualType PointeeType;
3583 
3584   /// The class of which the pointee is a member. Must ultimately be a
3585   /// CXXRecordType, but could be a typedef or a template parameter too.
3586   NestedNameSpecifier *Qualifier;
3587 
3588   MemberPointerType(QualType Pointee, NestedNameSpecifier *Qualifier,
3589                     QualType CanonicalPtr)
3590       : Type(MemberPointer, CanonicalPtr,
3591              (toTypeDependence(Qualifier->getDependence()) &
3592               ~TypeDependence::VariablyModified) |
3593                  Pointee->getDependence()),
3594         PointeeType(Pointee), Qualifier(Qualifier) {}
3595 
3596 public:
3597   QualType getPointeeType() const { return PointeeType; }
3598 
3599   /// Returns true if the member type (i.e. the pointee type) is a
3600   /// function type rather than a data-member type.
3601   bool isMemberFunctionPointer() const {
3602     return PointeeType->isFunctionProtoType();
3603   }
3604 
3605   /// Returns true if the member type (i.e. the pointee type) is a
3606   /// data type rather than a function type.
3607   bool isMemberDataPointer() const {
3608     return !PointeeType->isFunctionProtoType();
3609   }
3610 
3611   NestedNameSpecifier *getQualifier() const { return Qualifier; }
3612   /// Note: this can trigger extra deserialization when external AST sources are
3613   /// used. Prefer `getCXXRecordDecl()` unless you really need the most recent
3614   /// decl.
3615   CXXRecordDecl *getMostRecentCXXRecordDecl() const;
3616 
3617   bool isSugared() const;
3618   QualType desugar() const {
3619     return isSugared() ? getCanonicalTypeInternal() : QualType(this, 0);
3620   }
3621 
3622   void Profile(llvm::FoldingSetNodeID &ID) {
3623     // FIXME: `getMostRecentCXXRecordDecl()` should be possible to use here,
3624     // however when external AST sources are used it causes nondeterminism
3625     // issues (see https://github.com/llvm/llvm-project/pull/137910).
3626     Profile(ID, getPointeeType(), getQualifier(), getCXXRecordDecl());
3627   }
3628 
3629   static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
3630                       const NestedNameSpecifier *Qualifier,
3631                       const CXXRecordDecl *Cls);
3632 
3633   static bool classof(const Type *T) {
3634     return T->getTypeClass() == MemberPointer;
3635   }
3636 
3637 private:
3638   CXXRecordDecl *getCXXRecordDecl() const;
3639 };
3640 
3641 /// Capture whether this is a normal array (e.g. int X[4])
3642 /// an array with a static size (e.g. int X[static 4]), or an array
3643 /// with a star size (e.g. int X[*]).
3644 /// 'static' is only allowed on function parameters.
3645 enum class ArraySizeModifier { Normal, Static, Star };
3646 
3647 /// Represents an array type, per C99 6.7.5.2 - Array Declarators.
3648 class ArrayType : public Type, public llvm::FoldingSetNode {
3649 private:
3650   /// The element type of the array.
3651   QualType ElementType;
3652 
3653 protected:
3654   friend class ASTContext; // ASTContext creates these.
3655 
3656   ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm,
3657             unsigned tq, const Expr *sz = nullptr);
3658 
3659 public:
3660   QualType getElementType() const { return ElementType; }
3661 
3662   ArraySizeModifier getSizeModifier() const {
3663     return ArraySizeModifier(ArrayTypeBits.SizeModifier);
3664   }
3665 
3666   Qualifiers getIndexTypeQualifiers() const {
3667     return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers());
3668   }
3669 
3670   unsigned getIndexTypeCVRQualifiers() const {
3671     return ArrayTypeBits.IndexTypeQuals;
3672   }
3673 
3674   static bool classof(const Type *T) {
3675     return T->getTypeClass() == ConstantArray ||
3676            T->getTypeClass() == VariableArray ||
3677            T->getTypeClass() == IncompleteArray ||
3678            T->getTypeClass() == DependentSizedArray ||
3679            T->getTypeClass() == ArrayParameter;
3680   }
3681 };
3682 
3683 /// Represents the canonical version of C arrays with a specified constant size.
3684 /// For example, the canonical type for 'int A[4 + 4*100]' is a
3685 /// ConstantArrayType where the element type is 'int' and the size is 404.
3686 class ConstantArrayType : public ArrayType {
3687   friend class ASTContext; // ASTContext creates these.
3688 
3689   struct ExternalSize {
3690     ExternalSize(const llvm::APInt &Sz, const Expr *SE)
3691         : Size(Sz), SizeExpr(SE) {}
3692     llvm::APInt Size; // Allows us to unique the type.
3693     const Expr *SizeExpr;
3694   };
3695 
3696   union {
3697     uint64_t Size;
3698     ExternalSize *SizePtr;
3699   };
3700 
3701   ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
3702                     ArraySizeModifier SM, unsigned TQ)
3703       : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), Size(Sz) {
3704     ConstantArrayTypeBits.HasExternalSize = false;
3705     ConstantArrayTypeBits.SizeWidth = Width / 8;
3706     // The in-structure size stores the size in bytes rather than bits so we
3707     // drop the three least significant bits since they're always zero anyways.
3708     assert(Width < 0xFF && "Type width in bits must be less than 8 bits");
3709   }
3710 
3711   ConstantArrayType(QualType Et, QualType Can, ExternalSize *SzPtr,
3712                     ArraySizeModifier SM, unsigned TQ)
3713       : ArrayType(ConstantArray, Et, Can, SM, TQ, SzPtr->SizeExpr),
3714         SizePtr(SzPtr) {
3715     ConstantArrayTypeBits.HasExternalSize = true;
3716     ConstantArrayTypeBits.SizeWidth = 0;
3717 
3718     assert((SzPtr->SizeExpr == nullptr || !Can.isNull()) &&
3719            "canonical constant array should not have size expression");
3720   }
3721 
3722   static ConstantArrayType *Create(const ASTContext &Ctx, QualType ET,
3723                                    QualType Can, const llvm::APInt &Sz,
3724                                    const Expr *SzExpr, ArraySizeModifier SzMod,
3725                                    unsigned Qual);
3726 
3727 protected:
3728   ConstantArrayType(TypeClass Tc, const ConstantArrayType *ATy, QualType Can)
3729       : ArrayType(Tc, ATy->getElementType(), Can, ATy->getSizeModifier(),
3730                   ATy->getIndexTypeQualifiers().getAsOpaqueValue(), nullptr) {
3731     ConstantArrayTypeBits.HasExternalSize =
3732         ATy->ConstantArrayTypeBits.HasExternalSize;
3733     if (!ConstantArrayTypeBits.HasExternalSize) {
3734       ConstantArrayTypeBits.SizeWidth = ATy->ConstantArrayTypeBits.SizeWidth;
3735       Size = ATy->Size;
3736     } else
3737       SizePtr = ATy->SizePtr;
3738   }
3739 
3740 public:
3741   /// Return the constant array size as an APInt.
3742   llvm::APInt getSize() const {
3743     return ConstantArrayTypeBits.HasExternalSize
3744                ? SizePtr->Size
3745                : llvm::APInt(ConstantArrayTypeBits.SizeWidth * 8, Size);
3746   }
3747 
3748   /// Return the bit width of the size type.
3749   unsigned getSizeBitWidth() const {
3750     return ConstantArrayTypeBits.HasExternalSize
3751                ? SizePtr->Size.getBitWidth()
3752                : static_cast<unsigned>(ConstantArrayTypeBits.SizeWidth * 8);
3753   }
3754 
3755   /// Return true if the size is zero.
3756   bool isZeroSize() const {
3757     return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.isZero()
3758                                                  : 0 == Size;
3759   }
3760 
3761   /// Return the size zero-extended as a uint64_t.
3762   uint64_t getZExtSize() const {
3763     return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getZExtValue()
3764                                                  : Size;
3765   }
3766 
3767   /// Return the size sign-extended as a uint64_t.
3768   int64_t getSExtSize() const {
3769     return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getSExtValue()
3770                                                  : static_cast<int64_t>(Size);
3771   }
3772 
3773   /// Return the size zero-extended to uint64_t or UINT64_MAX if the value is
3774   /// larger than UINT64_MAX.
3775   uint64_t getLimitedSize() const {
3776     return ConstantArrayTypeBits.HasExternalSize
3777                ? SizePtr->Size.getLimitedValue()
3778                : Size;
3779   }
3780 
3781   /// Return a pointer to the size expression.
3782   const Expr *getSizeExpr() const {
3783     return ConstantArrayTypeBits.HasExternalSize ? SizePtr->SizeExpr : nullptr;
3784   }
3785 
3786   bool isSugared() const { return false; }
3787   QualType desugar() const { return QualType(this, 0); }
3788 
3789   /// Determine the number of bits required to address a member of
3790   // an array with the given element type and number of elements.
3791   static unsigned getNumAddressingBits(const ASTContext &Context,
3792                                        QualType ElementType,
3793                                        const llvm::APInt &NumElements);
3794 
3795   unsigned getNumAddressingBits(const ASTContext &Context) const;
3796 
3797   /// Determine the maximum number of active bits that an array's size
3798   /// can require, which limits the maximum size of the array.
3799   static unsigned getMaxSizeBits(const ASTContext &Context);
3800 
3801   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) {
3802     Profile(ID, Ctx, getElementType(), getZExtSize(), getSizeExpr(),
3803             getSizeModifier(), getIndexTypeCVRQualifiers());
3804   }
3805 
3806   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx,
3807                       QualType ET, uint64_t ArraySize, const Expr *SizeExpr,
3808                       ArraySizeModifier SizeMod, unsigned TypeQuals);
3809 
3810   static bool classof(const Type *T) {
3811     return T->getTypeClass() == ConstantArray ||
3812            T->getTypeClass() == ArrayParameter;
3813   }
3814 };
3815 
3816 /// Represents a constant array type that does not decay to a pointer when used
3817 /// as a function parameter.
3818 class ArrayParameterType : public ConstantArrayType {
3819   friend class ASTContext; // ASTContext creates these.
3820 
3821   ArrayParameterType(const ConstantArrayType *ATy, QualType CanTy)
3822       : ConstantArrayType(ArrayParameter, ATy, CanTy) {}
3823 
3824 public:
3825   static bool classof(const Type *T) {
3826     return T->getTypeClass() == ArrayParameter;
3827   }
3828 
3829   QualType getConstantArrayType(const ASTContext &Ctx) const;
3830 };
3831 
3832 /// Represents a C array with an unspecified size.  For example 'int A[]' has
3833 /// an IncompleteArrayType where the element type is 'int' and the size is
3834 /// unspecified.
3835 class IncompleteArrayType : public ArrayType {
3836   friend class ASTContext; // ASTContext creates these.
3837 
3838   IncompleteArrayType(QualType et, QualType can,
3839                       ArraySizeModifier sm, unsigned tq)
3840       : ArrayType(IncompleteArray, et, can, sm, tq) {}
3841 
3842 public:
3843   friend class StmtIteratorBase;
3844 
3845   bool isSugared() const { return false; }
3846   QualType desugar() const { return QualType(this, 0); }
3847 
3848   static bool classof(const Type *T) {
3849     return T->getTypeClass() == IncompleteArray;
3850   }
3851 
3852   void Profile(llvm::FoldingSetNodeID &ID) {
3853     Profile(ID, getElementType(), getSizeModifier(),
3854             getIndexTypeCVRQualifiers());
3855   }
3856 
3857   static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
3858                       ArraySizeModifier SizeMod, unsigned TypeQuals) {
3859     ID.AddPointer(ET.getAsOpaquePtr());
3860     ID.AddInteger(llvm::to_underlying(SizeMod));
3861     ID.AddInteger(TypeQuals);
3862   }
3863 };
3864 
3865 /// Represents a C array with a specified size that is not an
3866 /// integer-constant-expression.  For example, 'int s[x+foo()]'.
3867 /// Since the size expression is an arbitrary expression, we store it as such.
3868 ///
3869 /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
3870 /// should not be: two lexically equivalent variable array types could mean
3871 /// different things, for example, these variables do not have the same type
3872 /// dynamically:
3873 ///
3874 /// void foo(int x) {
3875 ///   int Y[x];
3876 ///   ++x;
3877 ///   int Z[x];
3878 /// }
3879 ///
3880 /// FIXME: Even constant array types might be represented by a
3881 /// VariableArrayType, as in:
3882 ///
3883 ///   void func(int n) {
3884 ///     int array[7][n];
3885 ///   }
3886 ///
3887 /// Even though 'array' is a constant-size array of seven elements of type
3888 /// variable-length array of size 'n', it will be represented as a
3889 /// VariableArrayType whose 'SizeExpr' is an IntegerLiteral whose value is 7.
3890 /// Instead, this should be a ConstantArrayType whose element is a
3891 /// VariableArrayType, which models the type better.
3892 class VariableArrayType : public ArrayType {
3893   friend class ASTContext; // ASTContext creates these.
3894 
3895   /// An assignment-expression. VLA's are only permitted within
3896   /// a function block.
3897   Stmt *SizeExpr;
3898 
3899   VariableArrayType(QualType et, QualType can, Expr *e, ArraySizeModifier sm,
3900                     unsigned tq)
3901       : ArrayType(VariableArray, et, can, sm, tq, e), SizeExpr((Stmt *)e) {}
3902 
3903 public:
3904   friend class StmtIteratorBase;
3905 
3906   Expr *getSizeExpr() const {
3907     // We use C-style casts instead of cast<> here because we do not wish
3908     // to have a dependency of Type.h on Stmt.h/Expr.h.
3909     return (Expr*) SizeExpr;
3910   }
3911 
3912   bool isSugared() const { return false; }
3913   QualType desugar() const { return QualType(this, 0); }
3914 
3915   static bool classof(const Type *T) {
3916     return T->getTypeClass() == VariableArray;
3917   }
3918 
3919   void Profile(llvm::FoldingSetNodeID &ID) {
3920     llvm_unreachable("Cannot unique VariableArrayTypes.");
3921   }
3922 };
3923 
3924 /// Represents an array type in C++ whose size is a value-dependent expression.
3925 ///
3926 /// For example:
3927 /// \code
3928 /// template<typename T, int Size>
3929 /// class array {
3930 ///   T data[Size];
3931 /// };
3932 /// \endcode
3933 ///
3934 /// For these types, we won't actually know what the array bound is
3935 /// until template instantiation occurs, at which point this will
3936 /// become either a ConstantArrayType or a VariableArrayType.
3937 class DependentSizedArrayType : public ArrayType {
3938   friend class ASTContext; // ASTContext creates these.
3939 
3940   /// An assignment expression that will instantiate to the
3941   /// size of the array.
3942   ///
3943   /// The expression itself might be null, in which case the array
3944   /// type will have its size deduced from an initializer.
3945   Stmt *SizeExpr;
3946 
3947   DependentSizedArrayType(QualType et, QualType can, Expr *e,
3948                           ArraySizeModifier sm, unsigned tq);
3949 
3950 public:
3951   friend class StmtIteratorBase;
3952 
3953   Expr *getSizeExpr() const {
3954     // We use C-style casts instead of cast<> here because we do not wish
3955     // to have a dependency of Type.h on Stmt.h/Expr.h.
3956     return (Expr*) SizeExpr;
3957   }
3958 
3959   bool isSugared() const { return false; }
3960   QualType desugar() const { return QualType(this, 0); }
3961 
3962   static bool classof(const Type *T) {
3963     return T->getTypeClass() == DependentSizedArray;
3964   }
3965 
3966   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
3967     Profile(ID, Context, getElementType(),
3968             getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr());
3969   }
3970 
3971   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3972                       QualType ET, ArraySizeModifier SizeMod,
3973                       unsigned TypeQuals, Expr *E);
3974 };
3975 
3976 /// Represents an extended address space qualifier where the input address space
3977 /// value is dependent. Non-dependent address spaces are not represented with a
3978 /// special Type subclass; they are stored on an ExtQuals node as part of a QualType.
3979 ///
3980 /// For example:
3981 /// \code
3982 /// template<typename T, int AddrSpace>
3983 /// class AddressSpace {
3984 ///   typedef T __attribute__((address_space(AddrSpace))) type;
3985 /// }
3986 /// \endcode
3987 class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
3988   friend class ASTContext;
3989 
3990   Expr *AddrSpaceExpr;
3991   QualType PointeeType;
3992   SourceLocation loc;
3993 
3994   DependentAddressSpaceType(QualType PointeeType, QualType can,
3995                             Expr *AddrSpaceExpr, SourceLocation loc);
3996 
3997 public:
3998   Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; }
3999   QualType getPointeeType() const { return PointeeType; }
4000   SourceLocation getAttributeLoc() const { return loc; }
4001 
4002   bool isSugared() const { return false; }
4003   QualType desugar() const { return QualType(this, 0); }
4004 
4005   static bool classof(const Type *T) {
4006     return T->getTypeClass() == DependentAddressSpace;
4007   }
4008 
4009   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
4010     Profile(ID, Context, getPointeeType(), getAddrSpaceExpr());
4011   }
4012 
4013   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4014                       QualType PointeeType, Expr *AddrSpaceExpr);
4015 };
4016 
4017 /// Represents an extended vector type where either the type or size is
4018 /// dependent.
4019 ///
4020 /// For example:
4021 /// \code
4022 /// template<typename T, int Size>
4023 /// class vector {
4024 ///   typedef T __attribute__((ext_vector_type(Size))) type;
4025 /// }
4026 /// \endcode
4027 class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
4028   friend class ASTContext;
4029 
4030   Expr *SizeExpr;
4031 
4032   /// The element type of the array.
4033   QualType ElementType;
4034 
4035   SourceLocation loc;
4036 
4037   DependentSizedExtVectorType(QualType ElementType, QualType can,
4038                               Expr *SizeExpr, SourceLocation loc);
4039 
4040 public:
4041   Expr *getSizeExpr() const { return SizeExpr; }
4042   QualType getElementType() const { return ElementType; }
4043   SourceLocation getAttributeLoc() const { return loc; }
4044 
4045   bool isSugared() const { return false; }
4046   QualType desugar() const { return QualType(this, 0); }
4047 
4048   static bool classof(const Type *T) {
4049     return T->getTypeClass() == DependentSizedExtVector;
4050   }
4051 
4052   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
4053     Profile(ID, Context, getElementType(), getSizeExpr());
4054   }
4055 
4056   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4057                       QualType ElementType, Expr *SizeExpr);
4058 };
4059 
4060 enum class VectorKind {
4061   /// not a target-specific vector type
4062   Generic,
4063 
4064   /// is AltiVec vector
4065   AltiVecVector,
4066 
4067   /// is AltiVec 'vector Pixel'
4068   AltiVecPixel,
4069 
4070   /// is AltiVec 'vector bool ...'
4071   AltiVecBool,
4072 
4073   /// is ARM Neon vector
4074   Neon,
4075 
4076   /// is ARM Neon polynomial vector
4077   NeonPoly,
4078 
4079   /// is AArch64 SVE fixed-length data vector
4080   SveFixedLengthData,
4081 
4082   /// is AArch64 SVE fixed-length predicate vector
4083   SveFixedLengthPredicate,
4084 
4085   /// is RISC-V RVV fixed-length data vector
4086   RVVFixedLengthData,
4087 
4088   /// is RISC-V RVV fixed-length mask vector
4089   RVVFixedLengthMask,
4090 
4091   RVVFixedLengthMask_1,
4092   RVVFixedLengthMask_2,
4093   RVVFixedLengthMask_4
4094 };
4095 
4096 /// Represents a GCC generic vector type. This type is created using
4097 /// __attribute__((vector_size(n)), where "n" specifies the vector size in
4098 /// bytes; or from an Altivec __vector or vector declaration.
4099 /// Since the constructor takes the number of vector elements, the
4100 /// client is responsible for converting the size into the number of elements.
4101 class VectorType : public Type, public llvm::FoldingSetNode {
4102 protected:
4103   friend class ASTContext; // ASTContext creates these.
4104 
4105   /// The element type of the vector.
4106   QualType ElementType;
4107 
4108   VectorType(QualType vecType, unsigned nElements, QualType canonType,
4109              VectorKind vecKind);
4110 
4111   VectorType(TypeClass tc, QualType vecType, unsigned nElements,
4112              QualType canonType, VectorKind vecKind);
4113 
4114 public:
4115   QualType getElementType() const { return ElementType; }
4116   unsigned getNumElements() const { return VectorTypeBits.NumElements; }
4117 
4118   bool isSugared() const { return false; }
4119   QualType desugar() const { return QualType(this, 0); }
4120 
4121   VectorKind getVectorKind() const {
4122     return VectorKind(VectorTypeBits.VecKind);
4123   }
4124 
4125   void Profile(llvm::FoldingSetNodeID &ID) {
4126     Profile(ID, getElementType(), getNumElements(),
4127             getTypeClass(), getVectorKind());
4128   }
4129 
4130   static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
4131                       unsigned NumElements, TypeClass TypeClass,
4132                       VectorKind VecKind) {
4133     ID.AddPointer(ElementType.getAsOpaquePtr());
4134     ID.AddInteger(NumElements);
4135     ID.AddInteger(TypeClass);
4136     ID.AddInteger(llvm::to_underlying(VecKind));
4137   }
4138 
4139   static bool classof(const Type *T) {
4140     return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
4141   }
4142 };
4143 
4144 /// Represents a vector type where either the type or size is dependent.
4145 ////
4146 /// For example:
4147 /// \code
4148 /// template<typename T, int Size>
4149 /// class vector {
4150 ///   typedef T __attribute__((vector_size(Size))) type;
4151 /// }
4152 /// \endcode
4153 class DependentVectorType : public Type, public llvm::FoldingSetNode {
4154   friend class ASTContext;
4155 
4156   QualType ElementType;
4157   Expr *SizeExpr;
4158   SourceLocation Loc;
4159 
4160   DependentVectorType(QualType ElementType, QualType CanonType, Expr *SizeExpr,
4161                       SourceLocation Loc, VectorKind vecKind);
4162 
4163 public:
4164   Expr *getSizeExpr() const { return SizeExpr; }
4165   QualType getElementType() const { return ElementType; }
4166   SourceLocation getAttributeLoc() const { return Loc; }
4167   VectorKind getVectorKind() const {
4168     return VectorKind(VectorTypeBits.VecKind);
4169   }
4170 
4171   bool isSugared() const { return false; }
4172   QualType desugar() const { return QualType(this, 0); }
4173 
4174   static bool classof(const Type *T) {
4175     return T->getTypeClass() == DependentVector;
4176   }
4177 
4178   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
4179     Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind());
4180   }
4181 
4182   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4183                       QualType ElementType, const Expr *SizeExpr,
4184                       VectorKind VecKind);
4185 };
4186 
4187 /// ExtVectorType - Extended vector type. This type is created using
4188 /// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
4189 /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
4190 /// class enables syntactic extensions, like Vector Components for accessing
4191 /// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL
4192 /// Shading Language).
4193 class ExtVectorType : public VectorType {
4194   friend class ASTContext; // ASTContext creates these.
4195 
4196   ExtVectorType(QualType vecType, unsigned nElements, QualType canonType)
4197       : VectorType(ExtVector, vecType, nElements, canonType,
4198                    VectorKind::Generic) {}
4199 
4200 public:
4201   static int getPointAccessorIdx(char c) {
4202     switch (c) {
4203     default: return -1;
4204     case 'x': case 'r': return 0;
4205     case 'y': case 'g': return 1;
4206     case 'z': case 'b': return 2;
4207     case 'w': case 'a': return 3;
4208     }
4209   }
4210 
4211   static int getNumericAccessorIdx(char c) {
4212     switch (c) {
4213       default: return -1;
4214       case '0': return 0;
4215       case '1': return 1;
4216       case '2': return 2;
4217       case '3': return 3;
4218       case '4': return 4;
4219       case '5': return 5;
4220       case '6': return 6;
4221       case '7': return 7;
4222       case '8': return 8;
4223       case '9': return 9;
4224       case 'A':
4225       case 'a': return 10;
4226       case 'B':
4227       case 'b': return 11;
4228       case 'C':
4229       case 'c': return 12;
4230       case 'D':
4231       case 'd': return 13;
4232       case 'E':
4233       case 'e': return 14;
4234       case 'F':
4235       case 'f': return 15;
4236     }
4237   }
4238 
4239   static int getAccessorIdx(char c, bool isNumericAccessor) {
4240     if (isNumericAccessor)
4241       return getNumericAccessorIdx(c);
4242     else
4243       return getPointAccessorIdx(c);
4244   }
4245 
4246   bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const {
4247     if (int idx = getAccessorIdx(c, isNumericAccessor)+1)
4248       return unsigned(idx-1) < getNumElements();
4249     return false;
4250   }
4251 
4252   bool isSugared() const { return false; }
4253   QualType desugar() const { return QualType(this, 0); }
4254 
4255   static bool classof(const Type *T) {
4256     return T->getTypeClass() == ExtVector;
4257   }
4258 };
4259 
4260 /// Represents a matrix type, as defined in the Matrix Types clang extensions.
4261 /// __attribute__((matrix_type(rows, columns))), where "rows" specifies
4262 /// number of rows and "columns" specifies the number of columns.
4263 class MatrixType : public Type, public llvm::FoldingSetNode {
4264 protected:
4265   friend class ASTContext;
4266 
4267   /// The element type of the matrix.
4268   QualType ElementType;
4269 
4270   MatrixType(QualType ElementTy, QualType CanonElementTy);
4271 
4272   MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy,
4273              const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr);
4274 
4275 public:
4276   /// Returns type of the elements being stored in the matrix
4277   QualType getElementType() const { return ElementType; }
4278 
4279   /// Valid elements types are the following:
4280   /// * an integer type (as in C23 6.2.5p22), but excluding enumerated types
4281   ///   and _Bool
4282   /// * the standard floating types float or double
4283   /// * a half-precision floating point type, if one is supported on the target
4284   static bool isValidElementType(QualType T) {
4285     return T->isDependentType() ||
4286            (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType());
4287   }
4288 
4289   bool isSugared() const { return false; }
4290   QualType desugar() const { return QualType(this, 0); }
4291 
4292   static bool classof(const Type *T) {
4293     return T->getTypeClass() == ConstantMatrix ||
4294            T->getTypeClass() == DependentSizedMatrix;
4295   }
4296 };
4297 
4298 /// Represents a concrete matrix type with constant number of rows and columns
4299 class ConstantMatrixType final : public MatrixType {
4300 protected:
4301   friend class ASTContext;
4302 
4303   /// Number of rows and columns.
4304   unsigned NumRows;
4305   unsigned NumColumns;
4306 
4307   static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1;
4308 
4309   ConstantMatrixType(QualType MatrixElementType, unsigned NRows,
4310                      unsigned NColumns, QualType CanonElementType);
4311 
4312   ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows,
4313                      unsigned NColumns, QualType CanonElementType);
4314 
4315 public:
4316   /// Returns the number of rows in the matrix.
4317   unsigned getNumRows() const { return NumRows; }
4318 
4319   /// Returns the number of columns in the matrix.
4320   unsigned getNumColumns() const { return NumColumns; }
4321 
4322   /// Returns the number of elements required to embed the matrix into a vector.
4323   unsigned getNumElementsFlattened() const {
4324     return getNumRows() * getNumColumns();
4325   }
4326 
4327   /// Returns true if \p NumElements is a valid matrix dimension.
4328   static constexpr bool isDimensionValid(size_t NumElements) {
4329     return NumElements > 0 && NumElements <= MaxElementsPerDimension;
4330   }
4331 
4332   /// Returns the maximum number of elements per dimension.
4333   static constexpr unsigned getMaxElementsPerDimension() {
4334     return MaxElementsPerDimension;
4335   }
4336 
4337   void Profile(llvm::FoldingSetNodeID &ID) {
4338     Profile(ID, getElementType(), getNumRows(), getNumColumns(),
4339             getTypeClass());
4340   }
4341 
4342   static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
4343                       unsigned NumRows, unsigned NumColumns,
4344                       TypeClass TypeClass) {
4345     ID.AddPointer(ElementType.getAsOpaquePtr());
4346     ID.AddInteger(NumRows);
4347     ID.AddInteger(NumColumns);
4348     ID.AddInteger(TypeClass);
4349   }
4350 
4351   static bool classof(const Type *T) {
4352     return T->getTypeClass() == ConstantMatrix;
4353   }
4354 };
4355 
4356 /// Represents a matrix type where the type and the number of rows and columns
4357 /// is dependent on a template.
4358 class DependentSizedMatrixType final : public MatrixType {
4359   friend class ASTContext;
4360 
4361   Expr *RowExpr;
4362   Expr *ColumnExpr;
4363 
4364   SourceLocation loc;
4365 
4366   DependentSizedMatrixType(QualType ElementType, QualType CanonicalType,
4367                            Expr *RowExpr, Expr *ColumnExpr, SourceLocation loc);
4368 
4369 public:
4370   Expr *getRowExpr() const { return RowExpr; }
4371   Expr *getColumnExpr() const { return ColumnExpr; }
4372   SourceLocation getAttributeLoc() const { return loc; }
4373 
4374   static bool classof(const Type *T) {
4375     return T->getTypeClass() == DependentSizedMatrix;
4376   }
4377 
4378   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
4379     Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr());
4380   }
4381 
4382   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4383                       QualType ElementType, Expr *RowExpr, Expr *ColumnExpr);
4384 };
4385 
4386 /// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
4387 /// class of FunctionNoProtoType and FunctionProtoType.
4388 class FunctionType : public Type {
4389   // The type returned by the function.
4390   QualType ResultType;
4391 
4392 public:
4393   /// Interesting information about a specific parameter that can't simply
4394   /// be reflected in parameter's type. This is only used by FunctionProtoType
4395   /// but is in FunctionType to make this class available during the
4396   /// specification of the bases of FunctionProtoType.
4397   ///
4398   /// It makes sense to model language features this way when there's some
4399   /// sort of parameter-specific override (such as an attribute) that
4400   /// affects how the function is called.  For example, the ARC ns_consumed
4401   /// attribute changes whether a parameter is passed at +0 (the default)
4402   /// or +1 (ns_consumed).  This must be reflected in the function type,
4403   /// but isn't really a change to the parameter type.
4404   ///
4405   /// One serious disadvantage of modelling language features this way is
4406   /// that they generally do not work with language features that attempt
4407   /// to destructure types.  For example, template argument deduction will
4408   /// not be able to match a parameter declared as
4409   ///   T (*)(U)
4410   /// against an argument of type
4411   ///   void (*)(__attribute__((ns_consumed)) id)
4412   /// because the substitution of T=void, U=id into the former will
4413   /// not produce the latter.
4414   class ExtParameterInfo {
4415     enum {
4416       ABIMask = 0x0F,
4417       IsConsumed = 0x10,
4418       HasPassObjSize = 0x20,
4419       IsNoEscape = 0x40,
4420     };
4421     unsigned char Data = 0;
4422 
4423   public:
4424     ExtParameterInfo() = default;
4425 
4426     /// Return the ABI treatment of this parameter.
4427     ParameterABI getABI() const { return ParameterABI(Data & ABIMask); }
4428     ExtParameterInfo withABI(ParameterABI kind) const {
4429       ExtParameterInfo copy = *this;
4430       copy.Data = (copy.Data & ~ABIMask) | unsigned(kind);
4431       return copy;
4432     }
4433 
4434     /// Is this parameter considered "consumed" by Objective-C ARC?
4435     /// Consumed parameters must have retainable object type.
4436     bool isConsumed() const { return (Data & IsConsumed); }
4437     ExtParameterInfo withIsConsumed(bool consumed) const {
4438       ExtParameterInfo copy = *this;
4439       if (consumed)
4440         copy.Data |= IsConsumed;
4441       else
4442         copy.Data &= ~IsConsumed;
4443       return copy;
4444     }
4445 
4446     bool hasPassObjectSize() const { return Data & HasPassObjSize; }
4447     ExtParameterInfo withHasPassObjectSize() const {
4448       ExtParameterInfo Copy = *this;
4449       Copy.Data |= HasPassObjSize;
4450       return Copy;
4451     }
4452 
4453     bool isNoEscape() const { return Data & IsNoEscape; }
4454     ExtParameterInfo withIsNoEscape(bool NoEscape) const {
4455       ExtParameterInfo Copy = *this;
4456       if (NoEscape)
4457         Copy.Data |= IsNoEscape;
4458       else
4459         Copy.Data &= ~IsNoEscape;
4460       return Copy;
4461     }
4462 
4463     unsigned char getOpaqueValue() const { return Data; }
4464     static ExtParameterInfo getFromOpaqueValue(unsigned char data) {
4465       ExtParameterInfo result;
4466       result.Data = data;
4467       return result;
4468     }
4469 
4470     friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) {
4471       return lhs.Data == rhs.Data;
4472     }
4473 
4474     friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) {
4475       return lhs.Data != rhs.Data;
4476     }
4477   };
4478 
4479   /// A class which abstracts out some details necessary for
4480   /// making a call.
4481   ///
4482   /// It is not actually used directly for storing this information in
4483   /// a FunctionType, although FunctionType does currently use the
4484   /// same bit-pattern.
4485   ///
4486   // If you add a field (say Foo), other than the obvious places (both,
4487   // constructors, compile failures), what you need to update is
4488   // * Operator==
4489   // * getFoo
4490   // * withFoo
4491   // * functionType. Add Foo, getFoo.
4492   // * ASTContext::getFooType
4493   // * ASTContext::mergeFunctionTypes
4494   // * FunctionNoProtoType::Profile
4495   // * FunctionProtoType::Profile
4496   // * TypePrinter::PrintFunctionProto
4497   // * AST read and write
4498   // * Codegen
4499   class ExtInfo {
4500     friend class FunctionType;
4501 
4502     // Feel free to rearrange or add bits, but if you go over 16, you'll need to
4503     // adjust the Bits field below, and if you add bits, you'll need to adjust
4504     // Type::FunctionTypeBitfields::ExtInfo as well.
4505 
4506     // |  CC  |noreturn|produces|nocallersavedregs|regparm|nocfcheck|cmsenscall|
4507     // |0 .. 5|   6    |    7   |       8         |9 .. 11|    12   |    13    |
4508     //
4509     // regparm is either 0 (no regparm attribute) or the regparm value+1.
4510     enum { CallConvMask = 0x3F };
4511     enum { NoReturnMask = 0x40 };
4512     enum { ProducesResultMask = 0x80 };
4513     enum { NoCallerSavedRegsMask = 0x100 };
4514     enum { RegParmMask = 0xe00, RegParmOffset = 9 };
4515     enum { NoCfCheckMask = 0x1000 };
4516     enum { CmseNSCallMask = 0x2000 };
4517     uint16_t Bits = CC_C;
4518 
4519     ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {}
4520 
4521   public:
4522     // Constructor with no defaults. Use this when you know that you
4523     // have all the elements (when reading an AST file for example).
4524     ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc,
4525             bool producesResult, bool noCallerSavedRegs, bool NoCfCheck,
4526             bool cmseNSCall) {
4527       assert((!hasRegParm || regParm < 7) && "Invalid regparm value");
4528       Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) |
4529              (producesResult ? ProducesResultMask : 0) |
4530              (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) |
4531              (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) |
4532              (NoCfCheck ? NoCfCheckMask : 0) |
4533              (cmseNSCall ? CmseNSCallMask : 0);
4534     }
4535 
4536     // Constructor with all defaults. Use when for example creating a
4537     // function known to use defaults.
4538     ExtInfo() = default;
4539 
4540     // Constructor with just the calling convention, which is an important part
4541     // of the canonical type.
4542     ExtInfo(CallingConv CC) : Bits(CC) {}
4543 
4544     bool getNoReturn() const { return Bits & NoReturnMask; }
4545     bool getProducesResult() const { return Bits & ProducesResultMask; }
4546     bool getCmseNSCall() const { return Bits & CmseNSCallMask; }
4547     bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; }
4548     bool getNoCfCheck() const { return Bits & NoCfCheckMask; }
4549     bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; }
4550 
4551     unsigned getRegParm() const {
4552       unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset;
4553       if (RegParm > 0)
4554         --RegParm;
4555       return RegParm;
4556     }
4557 
4558     CallingConv getCC() const { return CallingConv(Bits & CallConvMask); }
4559 
4560     bool operator==(ExtInfo Other) const {
4561       return Bits == Other.Bits;
4562     }
4563     bool operator!=(ExtInfo Other) const {
4564       return Bits != Other.Bits;
4565     }
4566 
4567     // Note that we don't have setters. That is by design, use
4568     // the following with methods instead of mutating these objects.
4569 
4570     ExtInfo withNoReturn(bool noReturn) const {
4571       if (noReturn)
4572         return ExtInfo(Bits | NoReturnMask);
4573       else
4574         return ExtInfo(Bits & ~NoReturnMask);
4575     }
4576 
4577     ExtInfo withProducesResult(bool producesResult) const {
4578       if (producesResult)
4579         return ExtInfo(Bits | ProducesResultMask);
4580       else
4581         return ExtInfo(Bits & ~ProducesResultMask);
4582     }
4583 
4584     ExtInfo withCmseNSCall(bool cmseNSCall) const {
4585       if (cmseNSCall)
4586         return ExtInfo(Bits | CmseNSCallMask);
4587       else
4588         return ExtInfo(Bits & ~CmseNSCallMask);
4589     }
4590 
4591     ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const {
4592       if (noCallerSavedRegs)
4593         return ExtInfo(Bits | NoCallerSavedRegsMask);
4594       else
4595         return ExtInfo(Bits & ~NoCallerSavedRegsMask);
4596     }
4597 
4598     ExtInfo withNoCfCheck(bool noCfCheck) const {
4599       if (noCfCheck)
4600         return ExtInfo(Bits | NoCfCheckMask);
4601       else
4602         return ExtInfo(Bits & ~NoCfCheckMask);
4603     }
4604 
4605     ExtInfo withRegParm(unsigned RegParm) const {
4606       assert(RegParm < 7 && "Invalid regparm value");
4607       return ExtInfo((Bits & ~RegParmMask) |
4608                      ((RegParm + 1) << RegParmOffset));
4609     }
4610 
4611     ExtInfo withCallingConv(CallingConv cc) const {
4612       return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc);
4613     }
4614 
4615     void Profile(llvm::FoldingSetNodeID &ID) const {
4616       ID.AddInteger(Bits);
4617     }
4618   };
4619 
4620   /// A simple holder for a QualType representing a type in an
4621   /// exception specification. Unfortunately needed by FunctionProtoType
4622   /// because TrailingObjects cannot handle repeated types.
4623   struct ExceptionType { QualType Type; };
4624 
4625   /// A simple holder for various uncommon bits which do not fit in
4626   /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the
4627   /// alignment of subsequent objects in TrailingObjects.
4628   struct alignas(void *) FunctionTypeExtraBitfields {
4629     /// The number of types in the exception specification.
4630     /// A whole unsigned is not needed here and according to
4631     /// [implimits] 8 bits would be enough here.
4632     unsigned NumExceptionType : 10;
4633 
4634     LLVM_PREFERRED_TYPE(bool)
4635     unsigned HasArmTypeAttributes : 1;
4636 
4637     LLVM_PREFERRED_TYPE(bool)
4638     unsigned EffectsHaveConditions : 1;
4639     unsigned NumFunctionEffects : 4;
4640 
4641     FunctionTypeExtraBitfields()
4642         : NumExceptionType(0), HasArmTypeAttributes(false),
4643           EffectsHaveConditions(false), NumFunctionEffects(0) {}
4644   };
4645 
4646   /// The AArch64 SME ACLE (Arm C/C++ Language Extensions) define a number
4647   /// of function type attributes that can be set on function types, including
4648   /// function pointers.
4649   enum AArch64SMETypeAttributes : unsigned {
4650     SME_NormalFunction = 0,
4651     SME_PStateSMEnabledMask = 1 << 0,
4652     SME_PStateSMCompatibleMask = 1 << 1,
4653 
4654     // Describes the value of the state using ArmStateValue.
4655     SME_ZAShift = 2,
4656     SME_ZAMask = 0b111 << SME_ZAShift,
4657     SME_ZT0Shift = 5,
4658     SME_ZT0Mask = 0b111 << SME_ZT0Shift,
4659 
4660     // A bit to tell whether a function is agnostic about sme ZA state.
4661     SME_AgnosticZAStateShift = 8,
4662     SME_AgnosticZAStateMask = 1 << SME_AgnosticZAStateShift,
4663 
4664     SME_AttributeMask =
4665         0b1'111'111'11 // We can't support more than 9 bits because of
4666                        // the bitmask in FunctionTypeArmAttributes
4667                        // and ExtProtoInfo.
4668   };
4669 
4670   enum ArmStateValue : unsigned {
4671     ARM_None = 0,
4672     ARM_Preserves = 1,
4673     ARM_In = 2,
4674     ARM_Out = 3,
4675     ARM_InOut = 4,
4676   };
4677 
4678   static ArmStateValue getArmZAState(unsigned AttrBits) {
4679     return (ArmStateValue)((AttrBits & SME_ZAMask) >> SME_ZAShift);
4680   }
4681 
4682   static ArmStateValue getArmZT0State(unsigned AttrBits) {
4683     return (ArmStateValue)((AttrBits & SME_ZT0Mask) >> SME_ZT0Shift);
4684   }
4685 
4686   /// A holder for Arm type attributes as described in the Arm C/C++
4687   /// Language extensions which are not particularly common to all
4688   /// types and therefore accounted separately from FunctionTypeBitfields.
4689   struct alignas(void *) FunctionTypeArmAttributes {
4690     /// Any AArch64 SME ACLE type attributes that need to be propagated
4691     /// on declarations and function pointers.
4692     unsigned AArch64SMEAttributes : 9;
4693 
4694     FunctionTypeArmAttributes() : AArch64SMEAttributes(SME_NormalFunction) {}
4695   };
4696 
4697 protected:
4698   FunctionType(TypeClass tc, QualType res, QualType Canonical,
4699                TypeDependence Dependence, ExtInfo Info)
4700       : Type(tc, Canonical, Dependence), ResultType(res) {
4701     FunctionTypeBits.ExtInfo = Info.Bits;
4702   }
4703 
4704   Qualifiers getFastTypeQuals() const {
4705     if (isFunctionProtoType())
4706       return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
4707 
4708     return Qualifiers();
4709   }
4710 
4711 public:
4712   QualType getReturnType() const { return ResultType; }
4713 
4714   bool getHasRegParm() const { return getExtInfo().getHasRegParm(); }
4715   unsigned getRegParmType() const { return getExtInfo().getRegParm(); }
4716 
4717   /// Determine whether this function type includes the GNU noreturn
4718   /// attribute. The C++11 [[noreturn]] attribute does not affect the function
4719   /// type.
4720   bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); }
4721 
4722   /// Determine whether this is a function prototype that includes the
4723   /// cfi_unchecked_callee attribute.
4724   bool getCFIUncheckedCalleeAttr() const;
4725 
4726   bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); }
4727   CallingConv getCallConv() const { return getExtInfo().getCC(); }
4728   ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); }
4729 
4730   static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0,
4731                 "Const, volatile and restrict are assumed to be a subset of "
4732                 "the fast qualifiers.");
4733 
4734   bool isConst() const { return getFastTypeQuals().hasConst(); }
4735   bool isVolatile() const { return getFastTypeQuals().hasVolatile(); }
4736   bool isRestrict() const { return getFastTypeQuals().hasRestrict(); }
4737 
4738   /// Determine the type of an expression that calls a function of
4739   /// this type.
4740   QualType getCallResultType(const ASTContext &Context) const {
4741     return getReturnType().getNonLValueExprType(Context);
4742   }
4743 
4744   static StringRef getNameForCallConv(CallingConv CC);
4745 
4746   static bool classof(const Type *T) {
4747     return T->getTypeClass() == FunctionNoProto ||
4748            T->getTypeClass() == FunctionProto;
4749   }
4750 };
4751 
4752 /// Represents a K&R-style 'int foo()' function, which has
4753 /// no information available about its arguments.
4754 class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
4755   friend class ASTContext; // ASTContext creates these.
4756 
4757   FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info)
4758       : FunctionType(FunctionNoProto, Result, Canonical,
4759                      Result->getDependence() &
4760                          ~(TypeDependence::DependentInstantiation |
4761                            TypeDependence::UnexpandedPack),
4762                      Info) {}
4763 
4764 public:
4765   // No additional state past what FunctionType provides.
4766 
4767   bool isSugared() const { return false; }
4768   QualType desugar() const { return QualType(this, 0); }
4769 
4770   void Profile(llvm::FoldingSetNodeID &ID) {
4771     Profile(ID, getReturnType(), getExtInfo());
4772   }
4773 
4774   static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType,
4775                       ExtInfo Info) {
4776     Info.Profile(ID);
4777     ID.AddPointer(ResultType.getAsOpaquePtr());
4778   }
4779 
4780   static bool classof(const Type *T) {
4781     return T->getTypeClass() == FunctionNoProto;
4782   }
4783 };
4784 
4785 // ------------------------------------------------------------------------------
4786 
4787 /// Represents an abstract function effect, using just an enumeration describing
4788 /// its kind.
4789 class FunctionEffect {
4790 public:
4791   /// Identifies the particular effect.
4792   enum class Kind : uint8_t {
4793     NonBlocking,
4794     NonAllocating,
4795     Blocking,
4796     Allocating,
4797     Last = Allocating
4798   };
4799   constexpr static size_t KindCount = static_cast<size_t>(Kind::Last) + 1;
4800 
4801   /// Flags describing some behaviors of the effect.
4802   using Flags = unsigned;
4803   enum FlagBit : Flags {
4804     // Can verification inspect callees' implementations? (e.g. nonblocking:
4805     // yes, tcb+types: no). This also implies the need for 2nd-pass
4806     // verification.
4807     FE_InferrableOnCallees = 0x1,
4808 
4809     // Language constructs which effects can diagnose as disallowed.
4810     FE_ExcludeThrow = 0x2,
4811     FE_ExcludeCatch = 0x4,
4812     FE_ExcludeObjCMessageSend = 0x8,
4813     FE_ExcludeStaticLocalVars = 0x10,
4814     FE_ExcludeThreadLocalVars = 0x20
4815   };
4816 
4817 private:
4818   Kind FKind;
4819 
4820   // Expansion: for hypothetical TCB+types, there could be one Kind for TCB,
4821   // then ~16(?) bits "SubKind" to map to a specific named TCB. SubKind would
4822   // be considered for uniqueness.
4823 
4824 public:
4825   explicit FunctionEffect(Kind K) : FKind(K) {}
4826 
4827   /// The kind of the effect.
4828   Kind kind() const { return FKind; }
4829 
4830   /// Return the opposite kind, for effects which have opposites.
4831   Kind oppositeKind() const;
4832 
4833   /// For serialization.
4834   uint32_t toOpaqueInt32() const { return uint32_t(FKind); }
4835   static FunctionEffect fromOpaqueInt32(uint32_t Value) {
4836     return FunctionEffect(Kind(Value));
4837   }
4838 
4839   /// Flags describing some behaviors of the effect.
4840   Flags flags() const {
4841     switch (kind()) {
4842     case Kind::NonBlocking:
4843       return FE_InferrableOnCallees | FE_ExcludeThrow | FE_ExcludeCatch |
4844              FE_ExcludeObjCMessageSend | FE_ExcludeStaticLocalVars |
4845              FE_ExcludeThreadLocalVars;
4846     case Kind::NonAllocating:
4847       // Same as NonBlocking, except without FE_ExcludeStaticLocalVars.
4848       return FE_InferrableOnCallees | FE_ExcludeThrow | FE_ExcludeCatch |
4849              FE_ExcludeObjCMessageSend | FE_ExcludeThreadLocalVars;
4850     case Kind::Blocking:
4851     case Kind::Allocating:
4852       return 0;
4853     }
4854     llvm_unreachable("unknown effect kind");
4855   }
4856 
4857   /// The description printed in diagnostics, e.g. 'nonblocking'.
4858   StringRef name() const;
4859 
4860   friend raw_ostream &operator<<(raw_ostream &OS,
4861                                  const FunctionEffect &Effect) {
4862     OS << Effect.name();
4863     return OS;
4864   }
4865 
4866   /// Determine whether the effect is allowed to be inferred on the callee,
4867   /// which is either a FunctionDecl or BlockDecl. If the returned optional
4868   /// is empty, inference is permitted; otherwise it holds the effect which
4869   /// blocked inference.
4870   /// Example: This allows nonblocking(false) to prevent inference for the
4871   /// function.
4872   std::optional<FunctionEffect>
4873   effectProhibitingInference(const Decl &Callee,
4874                              FunctionEffectKindSet CalleeFX) const;
4875 
4876   // Return false for success. When true is returned for a direct call, then the
4877   // FE_InferrableOnCallees flag may trigger inference rather than an immediate
4878   // diagnostic. Caller should be assumed to have the effect (it may not have it
4879   // explicitly when inferring).
4880   bool shouldDiagnoseFunctionCall(bool Direct,
4881                                   FunctionEffectKindSet CalleeFX) const;
4882 
4883   friend bool operator==(FunctionEffect LHS, FunctionEffect RHS) {
4884     return LHS.FKind == RHS.FKind;
4885   }
4886   friend bool operator!=(FunctionEffect LHS, FunctionEffect RHS) {
4887     return !(LHS == RHS);
4888   }
4889   friend bool operator<(FunctionEffect LHS, FunctionEffect RHS) {
4890     return LHS.FKind < RHS.FKind;
4891   }
4892 };
4893 
4894 /// Wrap a function effect's condition expression in another struct so
4895 /// that FunctionProtoType's TrailingObjects can treat it separately.
4896 class EffectConditionExpr {
4897   Expr *Cond = nullptr; // if null, unconditional.
4898 
4899 public:
4900   EffectConditionExpr() = default;
4901   EffectConditionExpr(Expr *E) : Cond(E) {}
4902 
4903   Expr *getCondition() const { return Cond; }
4904 
4905   bool operator==(const EffectConditionExpr &RHS) const {
4906     return Cond == RHS.Cond;
4907   }
4908 };
4909 
4910 /// A FunctionEffect plus a potential boolean expression determining whether
4911 /// the effect is declared (e.g. nonblocking(expr)). Generally the condition
4912 /// expression when present, is dependent.
4913 struct FunctionEffectWithCondition {
4914   FunctionEffect Effect;
4915   EffectConditionExpr Cond;
4916 
4917   FunctionEffectWithCondition(FunctionEffect E, const EffectConditionExpr &C)
4918       : Effect(E), Cond(C) {}
4919 
4920   /// Return a textual description of the effect, and its condition, if any.
4921   std::string description() const;
4922 
4923   friend raw_ostream &operator<<(raw_ostream &OS,
4924                                  const FunctionEffectWithCondition &CFE);
4925 };
4926 
4927 /// Support iteration in parallel through a pair of FunctionEffect and
4928 /// EffectConditionExpr containers.
4929 template <typename Container> class FunctionEffectIterator {
4930   friend Container;
4931 
4932   const Container *Outer = nullptr;
4933   size_t Idx = 0;
4934 
4935 public:
4936   FunctionEffectIterator();
4937   FunctionEffectIterator(const Container &O, size_t I) : Outer(&O), Idx(I) {}
4938   bool operator==(const FunctionEffectIterator &Other) const {
4939     return Idx == Other.Idx;
4940   }
4941   bool operator!=(const FunctionEffectIterator &Other) const {
4942     return Idx != Other.Idx;
4943   }
4944 
4945   FunctionEffectIterator operator++() {
4946     ++Idx;
4947     return *this;
4948   }
4949 
4950   FunctionEffectWithCondition operator*() const {
4951     assert(Outer != nullptr && "invalid FunctionEffectIterator");
4952     bool HasConds = !Outer->Conditions.empty();
4953     return FunctionEffectWithCondition{Outer->Effects[Idx],
4954                                        HasConds ? Outer->Conditions[Idx]
4955                                                 : EffectConditionExpr()};
4956   }
4957 };
4958 
4959 /// An immutable set of FunctionEffects and possibly conditions attached to
4960 /// them. The effects and conditions reside in memory not managed by this object
4961 /// (typically, trailing objects in FunctionProtoType, or borrowed references
4962 /// from a FunctionEffectSet).
4963 ///
4964 /// Invariants:
4965 /// - there is never more than one instance of any given effect.
4966 /// - the array of conditions is either empty or has the same size as the
4967 ///   array of effects.
4968 /// - some conditions may be null expressions; each condition pertains to
4969 ///   the effect at the same array index.
4970 ///
4971 /// Also, if there are any conditions, at least one of those expressions will be
4972 /// dependent, but this is only asserted in the constructor of
4973 /// FunctionProtoType.
4974 ///
4975 /// See also FunctionEffectSet, in Sema, which provides a mutable set.
4976 class FunctionEffectsRef {
4977   // Restrict classes which can call the private constructor -- these friends
4978   // all maintain the required invariants. FunctionEffectSet is generally the
4979   // only way in which the arrays are created; FunctionProtoType will not
4980   // reorder them.
4981   friend FunctionProtoType;
4982   friend FunctionEffectSet;
4983 
4984   ArrayRef<FunctionEffect> Effects;
4985   ArrayRef<EffectConditionExpr> Conditions;
4986 
4987   // The arrays are expected to have been sorted by the caller, with the
4988   // effects in order. The conditions array must be empty or the same size
4989   // as the effects array, since the conditions are associated with the effects
4990   // at the same array indices.
4991   FunctionEffectsRef(ArrayRef<FunctionEffect> FX,
4992                      ArrayRef<EffectConditionExpr> Conds)
4993       : Effects(FX), Conditions(Conds) {}
4994 
4995 public:
4996   /// Extract the effects from a Type if it is a function, block, or member
4997   /// function pointer, or a reference or pointer to one.
4998   static FunctionEffectsRef get(QualType QT);
4999 
5000   /// Asserts invariants.
5001   static FunctionEffectsRef create(ArrayRef<FunctionEffect> FX,
5002                                    ArrayRef<EffectConditionExpr> Conds);
5003 
5004   FunctionEffectsRef() = default;
5005 
5006   bool empty() const { return Effects.empty(); }
5007   size_t size() const { return Effects.size(); }
5008 
5009   ArrayRef<FunctionEffect> effects() const { return Effects; }
5010   ArrayRef<EffectConditionExpr> conditions() const { return Conditions; }
5011 
5012   using iterator = FunctionEffectIterator<FunctionEffectsRef>;
5013   friend iterator;
5014   iterator begin() const { return iterator(*this, 0); }
5015   iterator end() const { return iterator(*this, size()); }
5016 
5017   friend bool operator==(const FunctionEffectsRef &LHS,
5018                          const FunctionEffectsRef &RHS) {
5019     return LHS.Effects == RHS.Effects && LHS.Conditions == RHS.Conditions;
5020   }
5021   friend bool operator!=(const FunctionEffectsRef &LHS,
5022                          const FunctionEffectsRef &RHS) {
5023     return !(LHS == RHS);
5024   }
5025 
5026   void dump(llvm::raw_ostream &OS) const;
5027 };
5028 
5029 /// A mutable set of FunctionEffect::Kind.
5030 class FunctionEffectKindSet {
5031   // For now this only needs to be a bitmap.
5032   constexpr static size_t EndBitPos = FunctionEffect::KindCount;
5033   using KindBitsT = std::bitset<EndBitPos>;
5034 
5035   KindBitsT KindBits{};
5036 
5037   explicit FunctionEffectKindSet(KindBitsT KB) : KindBits(KB) {}
5038 
5039   // Functions to translate between an effect kind, starting at 1, and a
5040   // position in the bitset.
5041 
5042   constexpr static size_t kindToPos(FunctionEffect::Kind K) {
5043     return static_cast<size_t>(K);
5044   }
5045 
5046   constexpr static FunctionEffect::Kind posToKind(size_t Pos) {
5047     return static_cast<FunctionEffect::Kind>(Pos);
5048   }
5049 
5050   // Iterates through the bits which are set.
5051   class iterator {
5052     const FunctionEffectKindSet *Outer = nullptr;
5053     size_t Idx = 0;
5054 
5055     // If Idx does not reference a set bit, advance it until it does,
5056     // or until it reaches EndBitPos.
5057     void advanceToNextSetBit() {
5058       while (Idx < EndBitPos && !Outer->KindBits.test(Idx))
5059         ++Idx;
5060     }
5061 
5062   public:
5063     iterator();
5064     iterator(const FunctionEffectKindSet &O, size_t I) : Outer(&O), Idx(I) {
5065       advanceToNextSetBit();
5066     }
5067     bool operator==(const iterator &Other) const { return Idx == Other.Idx; }
5068     bool operator!=(const iterator &Other) const { return Idx != Other.Idx; }
5069 
5070     iterator operator++() {
5071       ++Idx;
5072       advanceToNextSetBit();
5073       return *this;
5074     }
5075 
5076     FunctionEffect operator*() const {
5077       assert(Idx < EndBitPos && "Dereference of end iterator");
5078       return FunctionEffect(posToKind(Idx));
5079     }
5080   };
5081 
5082 public:
5083   FunctionEffectKindSet() = default;
5084   explicit FunctionEffectKindSet(FunctionEffectsRef FX) { insert(FX); }
5085 
5086   iterator begin() const { return iterator(*this, 0); }
5087   iterator end() const { return iterator(*this, EndBitPos); }
5088 
5089   void insert(FunctionEffect Effect) { KindBits.set(kindToPos(Effect.kind())); }
5090   void insert(FunctionEffectsRef FX) {
5091     for (FunctionEffect Item : FX.effects())
5092       insert(Item);
5093   }
5094   void insert(FunctionEffectKindSet Set) { KindBits |= Set.KindBits; }
5095 
5096   bool empty() const { return KindBits.none(); }
5097   bool contains(const FunctionEffect::Kind EK) const {
5098     return KindBits.test(kindToPos(EK));
5099   }
5100   void dump(llvm::raw_ostream &OS) const;
5101 
5102   static FunctionEffectKindSet difference(FunctionEffectKindSet LHS,
5103                                           FunctionEffectKindSet RHS) {
5104     return FunctionEffectKindSet(LHS.KindBits & ~RHS.KindBits);
5105   }
5106 };
5107 
5108 /// A mutable set of FunctionEffects and possibly conditions attached to them.
5109 /// Used to compare and merge effects on declarations.
5110 ///
5111 /// Has the same invariants as FunctionEffectsRef.
5112 class FunctionEffectSet {
5113   SmallVector<FunctionEffect> Effects;
5114   SmallVector<EffectConditionExpr> Conditions;
5115 
5116 public:
5117   FunctionEffectSet() = default;
5118 
5119   explicit FunctionEffectSet(const FunctionEffectsRef &FX)
5120       : Effects(FX.effects()), Conditions(FX.conditions()) {}
5121 
5122   bool empty() const { return Effects.empty(); }
5123   size_t size() const { return Effects.size(); }
5124 
5125   using iterator = FunctionEffectIterator<FunctionEffectSet>;
5126   friend iterator;
5127   iterator begin() const { return iterator(*this, 0); }
5128   iterator end() const { return iterator(*this, size()); }
5129 
5130   operator FunctionEffectsRef() const { return {Effects, Conditions}; }
5131 
5132   void dump(llvm::raw_ostream &OS) const;
5133 
5134   // Mutators
5135 
5136   // On insertion, a conflict occurs when attempting to insert an
5137   // effect which is opposite an effect already in the set, or attempting
5138   // to insert an effect which is already in the set but with a condition
5139   // which is not identical.
5140   struct Conflict {
5141     FunctionEffectWithCondition Kept;
5142     FunctionEffectWithCondition Rejected;
5143   };
5144   using Conflicts = SmallVector<Conflict>;
5145 
5146   // Returns true for success (obviating a check of Errs.empty()).
5147   bool insert(const FunctionEffectWithCondition &NewEC, Conflicts &Errs);
5148 
5149   // Returns true for success (obviating a check of Errs.empty()).
5150   bool insert(const FunctionEffectsRef &Set, Conflicts &Errs);
5151 
5152   // Set operations
5153 
5154   static FunctionEffectSet getUnion(FunctionEffectsRef LHS,
5155                                     FunctionEffectsRef RHS, Conflicts &Errs);
5156   static FunctionEffectSet getIntersection(FunctionEffectsRef LHS,
5157                                            FunctionEffectsRef RHS);
5158 };
5159 
5160 /// Represents a prototype with parameter type info, e.g.
5161 /// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
5162 /// parameters, not as having a single void parameter. Such a type can have
5163 /// an exception specification, but this specification is not part of the
5164 /// canonical type. FunctionProtoType has several trailing objects, some of
5165 /// which optional. For more information about the trailing objects see
5166 /// the first comment inside FunctionProtoType.
5167 class FunctionProtoType final
5168     : public FunctionType,
5169       public llvm::FoldingSetNode,
5170       private llvm::TrailingObjects<
5171           FunctionProtoType, QualType, SourceLocation,
5172           FunctionType::FunctionTypeExtraBitfields,
5173           FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType,
5174           Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers,
5175           FunctionEffect, EffectConditionExpr> {
5176   friend class ASTContext; // ASTContext creates these.
5177   friend TrailingObjects;
5178 
5179   // FunctionProtoType is followed by several trailing objects, some of
5180   // which optional. They are in order:
5181   //
5182   // * An array of getNumParams() QualType holding the parameter types.
5183   //   Always present. Note that for the vast majority of FunctionProtoType,
5184   //   these will be the only trailing objects.
5185   //
5186   // * Optionally if the function is variadic, the SourceLocation of the
5187   //   ellipsis.
5188   //
5189   // * Optionally if some extra data is stored in FunctionTypeExtraBitfields
5190   //   (see FunctionTypeExtraBitfields and FunctionTypeBitfields):
5191   //   a single FunctionTypeExtraBitfields. Present if and only if
5192   //   hasExtraBitfields() is true.
5193   //
5194   // * Optionally exactly one of:
5195   //   * an array of getNumExceptions() ExceptionType,
5196   //   * a single Expr *,
5197   //   * a pair of FunctionDecl *,
5198   //   * a single FunctionDecl *
5199   //   used to store information about the various types of exception
5200   //   specification. See getExceptionSpecSize for the details.
5201   //
5202   // * Optionally an array of getNumParams() ExtParameterInfo holding
5203   //   an ExtParameterInfo for each of the parameters. Present if and
5204   //   only if hasExtParameterInfos() is true.
5205   //
5206   // * Optionally a Qualifiers object to represent extra qualifiers that can't
5207   //   be represented by FunctionTypeBitfields.FastTypeQuals. Present if and
5208   //   only if hasExtQualifiers() is true.
5209   //
5210   // * Optionally, an array of getNumFunctionEffects() FunctionEffect.
5211   //   Present only when getNumFunctionEffects() > 0
5212   //
5213   // * Optionally, an array of getNumFunctionEffects() EffectConditionExpr.
5214   //   Present only when getNumFunctionEffectConditions() > 0.
5215   //
5216   // The optional FunctionTypeExtraBitfields has to be before the data
5217   // related to the exception specification since it contains the number
5218   // of exception types.
5219   //
5220   // We put the ExtParameterInfos later.  If all were equal, it would make
5221   // more sense to put these before the exception specification, because
5222   // it's much easier to skip past them compared to the elaborate switch
5223   // required to skip the exception specification.  However, all is not
5224   // equal; ExtParameterInfos are used to model very uncommon features,
5225   // and it's better not to burden the more common paths.
5226 
5227 public:
5228   /// Holds information about the various types of exception specification.
5229   /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is
5230   /// used to group together the various bits of information about the
5231   /// exception specification.
5232   struct ExceptionSpecInfo {
5233     /// The kind of exception specification this is.
5234     ExceptionSpecificationType Type = EST_None;
5235 
5236     /// Explicitly-specified list of exception types.
5237     ArrayRef<QualType> Exceptions;
5238 
5239     /// Noexcept expression, if this is a computed noexcept specification.
5240     Expr *NoexceptExpr = nullptr;
5241 
5242     /// The function whose exception specification this is, for
5243     /// EST_Unevaluated and EST_Uninstantiated.
5244     FunctionDecl *SourceDecl = nullptr;
5245 
5246     /// The function template whose exception specification this is instantiated
5247     /// from, for EST_Uninstantiated.
5248     FunctionDecl *SourceTemplate = nullptr;
5249 
5250     ExceptionSpecInfo() = default;
5251 
5252     ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {}
5253 
5254     void instantiate();
5255   };
5256 
5257   /// Extra information about a function prototype. ExtProtoInfo is not
5258   /// stored as such in FunctionProtoType but is used to group together
5259   /// the various bits of extra information about a function prototype.
5260   struct ExtProtoInfo {
5261     FunctionType::ExtInfo ExtInfo;
5262     LLVM_PREFERRED_TYPE(bool)
5263     unsigned Variadic : 1;
5264     LLVM_PREFERRED_TYPE(bool)
5265     unsigned HasTrailingReturn : 1;
5266     LLVM_PREFERRED_TYPE(bool)
5267     unsigned CFIUncheckedCallee : 1;
5268     unsigned AArch64SMEAttributes : 9;
5269     Qualifiers TypeQuals;
5270     RefQualifierKind RefQualifier = RQ_None;
5271     ExceptionSpecInfo ExceptionSpec;
5272     const ExtParameterInfo *ExtParameterInfos = nullptr;
5273     SourceLocation EllipsisLoc;
5274     FunctionEffectsRef FunctionEffects;
5275 
5276     ExtProtoInfo()
5277         : Variadic(false), HasTrailingReturn(false), CFIUncheckedCallee(false),
5278           AArch64SMEAttributes(SME_NormalFunction) {}
5279 
5280     ExtProtoInfo(CallingConv CC)
5281         : ExtInfo(CC), Variadic(false), HasTrailingReturn(false),
5282           CFIUncheckedCallee(false), AArch64SMEAttributes(SME_NormalFunction) {}
5283 
5284     ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) {
5285       ExtProtoInfo Result(*this);
5286       Result.ExceptionSpec = ESI;
5287       return Result;
5288     }
5289 
5290     ExtProtoInfo withCFIUncheckedCallee(bool CFIUncheckedCallee) {
5291       ExtProtoInfo Result(*this);
5292       Result.CFIUncheckedCallee = CFIUncheckedCallee;
5293       return Result;
5294     }
5295 
5296     bool requiresFunctionProtoTypeExtraBitfields() const {
5297       return ExceptionSpec.Type == EST_Dynamic ||
5298              requiresFunctionProtoTypeArmAttributes() ||
5299              !FunctionEffects.empty();
5300     }
5301 
5302     bool requiresFunctionProtoTypeArmAttributes() const {
5303       return AArch64SMEAttributes != SME_NormalFunction;
5304     }
5305 
5306     void setArmSMEAttribute(AArch64SMETypeAttributes Kind, bool Enable = true) {
5307       if (Enable)
5308         AArch64SMEAttributes |= Kind;
5309       else
5310         AArch64SMEAttributes &= ~Kind;
5311     }
5312   };
5313 
5314 private:
5315   unsigned numTrailingObjects(OverloadToken<QualType>) const {
5316     return getNumParams();
5317   }
5318 
5319   unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
5320     return isVariadic();
5321   }
5322 
5323   unsigned numTrailingObjects(OverloadToken<FunctionTypeArmAttributes>) const {
5324     return hasArmTypeAttributes();
5325   }
5326 
5327   unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const {
5328     return hasExtraBitfields();
5329   }
5330 
5331   unsigned numTrailingObjects(OverloadToken<ExceptionType>) const {
5332     return getExceptionSpecSize().NumExceptionType;
5333   }
5334 
5335   unsigned numTrailingObjects(OverloadToken<Expr *>) const {
5336     return getExceptionSpecSize().NumExprPtr;
5337   }
5338 
5339   unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const {
5340     return getExceptionSpecSize().NumFunctionDeclPtr;
5341   }
5342 
5343   unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const {
5344     return hasExtParameterInfos() ? getNumParams() : 0;
5345   }
5346 
5347   unsigned numTrailingObjects(OverloadToken<Qualifiers>) const {
5348     return hasExtQualifiers() ? 1 : 0;
5349   }
5350 
5351   unsigned numTrailingObjects(OverloadToken<FunctionEffect>) const {
5352     return getNumFunctionEffects();
5353   }
5354 
5355   /// Determine whether there are any argument types that
5356   /// contain an unexpanded parameter pack.
5357   static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray,
5358                                                  unsigned numArgs) {
5359     for (unsigned Idx = 0; Idx < numArgs; ++Idx)
5360       if (ArgArray[Idx]->containsUnexpandedParameterPack())
5361         return true;
5362 
5363     return false;
5364   }
5365 
5366   FunctionProtoType(QualType result, ArrayRef<QualType> params,
5367                     QualType canonical, const ExtProtoInfo &epi);
5368 
5369   /// This struct is returned by getExceptionSpecSize and is used to
5370   /// translate an ExceptionSpecificationType to the number and kind
5371   /// of trailing objects related to the exception specification.
5372   struct ExceptionSpecSizeHolder {
5373     unsigned NumExceptionType;
5374     unsigned NumExprPtr;
5375     unsigned NumFunctionDeclPtr;
5376   };
5377 
5378   /// Return the number and kind of trailing objects
5379   /// related to the exception specification.
5380   static ExceptionSpecSizeHolder
5381   getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) {
5382     switch (EST) {
5383     case EST_None:
5384     case EST_DynamicNone:
5385     case EST_MSAny:
5386     case EST_BasicNoexcept:
5387     case EST_Unparsed:
5388     case EST_NoThrow:
5389       return {0, 0, 0};
5390 
5391     case EST_Dynamic:
5392       return {NumExceptions, 0, 0};
5393 
5394     case EST_DependentNoexcept:
5395     case EST_NoexceptFalse:
5396     case EST_NoexceptTrue:
5397       return {0, 1, 0};
5398 
5399     case EST_Uninstantiated:
5400       return {0, 0, 2};
5401 
5402     case EST_Unevaluated:
5403       return {0, 0, 1};
5404     }
5405     llvm_unreachable("bad exception specification kind");
5406   }
5407 
5408   /// Return the number and kind of trailing objects
5409   /// related to the exception specification.
5410   ExceptionSpecSizeHolder getExceptionSpecSize() const {
5411     return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions());
5412   }
5413 
5414   /// Whether the trailing FunctionTypeExtraBitfields is present.
5415   bool hasExtraBitfields() const {
5416     assert((getExceptionSpecType() != EST_Dynamic ||
5417             FunctionTypeBits.HasExtraBitfields) &&
5418            "ExtraBitfields are required for given ExceptionSpecType");
5419     return FunctionTypeBits.HasExtraBitfields;
5420 
5421   }
5422 
5423   bool hasArmTypeAttributes() const {
5424     return FunctionTypeBits.HasExtraBitfields &&
5425            getTrailingObjects<FunctionTypeExtraBitfields>()
5426                ->HasArmTypeAttributes;
5427   }
5428 
5429   bool hasExtQualifiers() const {
5430     return FunctionTypeBits.HasExtQuals;
5431   }
5432 
5433 public:
5434   unsigned getNumParams() const { return FunctionTypeBits.NumParams; }
5435 
5436   QualType getParamType(unsigned i) const {
5437     assert(i < getNumParams() && "invalid parameter index");
5438     return param_type_begin()[i];
5439   }
5440 
5441   ArrayRef<QualType> getParamTypes() const {
5442     return {param_type_begin(), param_type_end()};
5443   }
5444 
5445   ExtProtoInfo getExtProtoInfo() const {
5446     ExtProtoInfo EPI;
5447     EPI.ExtInfo = getExtInfo();
5448     EPI.Variadic = isVariadic();
5449     EPI.EllipsisLoc = getEllipsisLoc();
5450     EPI.HasTrailingReturn = hasTrailingReturn();
5451     EPI.CFIUncheckedCallee = hasCFIUncheckedCallee();
5452     EPI.ExceptionSpec = getExceptionSpecInfo();
5453     EPI.TypeQuals = getMethodQuals();
5454     EPI.RefQualifier = getRefQualifier();
5455     EPI.ExtParameterInfos = getExtParameterInfosOrNull();
5456     EPI.AArch64SMEAttributes = getAArch64SMEAttributes();
5457     EPI.FunctionEffects = getFunctionEffects();
5458     return EPI;
5459   }
5460 
5461   /// Get the kind of exception specification on this function.
5462   ExceptionSpecificationType getExceptionSpecType() const {
5463     return static_cast<ExceptionSpecificationType>(
5464         FunctionTypeBits.ExceptionSpecType);
5465   }
5466 
5467   /// Return whether this function has any kind of exception spec.
5468   bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; }
5469 
5470   /// Return whether this function has a dynamic (throw) exception spec.
5471   bool hasDynamicExceptionSpec() const {
5472     return isDynamicExceptionSpec(getExceptionSpecType());
5473   }
5474 
5475   /// Return whether this function has a noexcept exception spec.
5476   bool hasNoexceptExceptionSpec() const {
5477     return isNoexceptExceptionSpec(getExceptionSpecType());
5478   }
5479 
5480   /// Return whether this function has a dependent exception spec.
5481   bool hasDependentExceptionSpec() const;
5482 
5483   /// Return whether this function has an instantiation-dependent exception
5484   /// spec.
5485   bool hasInstantiationDependentExceptionSpec() const;
5486 
5487   /// Return all the available information about this type's exception spec.
5488   ExceptionSpecInfo getExceptionSpecInfo() const {
5489     ExceptionSpecInfo Result;
5490     Result.Type = getExceptionSpecType();
5491     if (Result.Type == EST_Dynamic) {
5492       Result.Exceptions = exceptions();
5493     } else if (isComputedNoexcept(Result.Type)) {
5494       Result.NoexceptExpr = getNoexceptExpr();
5495     } else if (Result.Type == EST_Uninstantiated) {
5496       Result.SourceDecl = getExceptionSpecDecl();
5497       Result.SourceTemplate = getExceptionSpecTemplate();
5498     } else if (Result.Type == EST_Unevaluated) {
5499       Result.SourceDecl = getExceptionSpecDecl();
5500     }
5501     return Result;
5502   }
5503 
5504   /// Return the number of types in the exception specification.
5505   unsigned getNumExceptions() const {
5506     return getExceptionSpecType() == EST_Dynamic
5507                ? getTrailingObjects<FunctionTypeExtraBitfields>()
5508                      ->NumExceptionType
5509                : 0;
5510   }
5511 
5512   /// Return the ith exception type, where 0 <= i < getNumExceptions().
5513   QualType getExceptionType(unsigned i) const {
5514     assert(i < getNumExceptions() && "Invalid exception number!");
5515     return exception_begin()[i];
5516   }
5517 
5518   /// Return the expression inside noexcept(expression), or a null pointer
5519   /// if there is none (because the exception spec is not of this form).
5520   Expr *getNoexceptExpr() const {
5521     if (!isComputedNoexcept(getExceptionSpecType()))
5522       return nullptr;
5523     return *getTrailingObjects<Expr *>();
5524   }
5525 
5526   /// If this function type has an exception specification which hasn't
5527   /// been determined yet (either because it has not been evaluated or because
5528   /// it has not been instantiated), this is the function whose exception
5529   /// specification is represented by this type.
5530   FunctionDecl *getExceptionSpecDecl() const {
5531     if (getExceptionSpecType() != EST_Uninstantiated &&
5532         getExceptionSpecType() != EST_Unevaluated)
5533       return nullptr;
5534     return getTrailingObjects<FunctionDecl *>()[0];
5535   }
5536 
5537   /// If this function type has an uninstantiated exception
5538   /// specification, this is the function whose exception specification
5539   /// should be instantiated to find the exception specification for
5540   /// this type.
5541   FunctionDecl *getExceptionSpecTemplate() const {
5542     if (getExceptionSpecType() != EST_Uninstantiated)
5543       return nullptr;
5544     return getTrailingObjects<FunctionDecl *>()[1];
5545   }
5546 
5547   /// Determine whether this function type has a non-throwing exception
5548   /// specification.
5549   CanThrowResult canThrow() const;
5550 
5551   /// Determine whether this function type has a non-throwing exception
5552   /// specification. If this depends on template arguments, returns
5553   /// \c ResultIfDependent.
5554   bool isNothrow(bool ResultIfDependent = false) const {
5555     return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot;
5556   }
5557 
5558   /// Whether this function prototype is variadic.
5559   bool isVariadic() const { return FunctionTypeBits.Variadic; }
5560 
5561   SourceLocation getEllipsisLoc() const {
5562     return isVariadic() ? *getTrailingObjects<SourceLocation>()
5563                         : SourceLocation();
5564   }
5565 
5566   /// Determines whether this function prototype contains a
5567   /// parameter pack at the end.
5568   ///
5569   /// A function template whose last parameter is a parameter pack can be
5570   /// called with an arbitrary number of arguments, much like a variadic
5571   /// function.
5572   bool isTemplateVariadic() const;
5573 
5574   /// Whether this function prototype has a trailing return type.
5575   bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; }
5576 
5577   bool hasCFIUncheckedCallee() const {
5578     return FunctionTypeBits.CFIUncheckedCallee;
5579   }
5580 
5581   Qualifiers getMethodQuals() const {
5582     if (hasExtQualifiers())
5583       return *getTrailingObjects<Qualifiers>();
5584     else
5585       return getFastTypeQuals();
5586   }
5587 
5588   /// Retrieve the ref-qualifier associated with this function type.
5589   RefQualifierKind getRefQualifier() const {
5590     return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier);
5591   }
5592 
5593   using param_type_iterator = const QualType *;
5594 
5595   ArrayRef<QualType> param_types() const {
5596     return {param_type_begin(), param_type_end()};
5597   }
5598 
5599   param_type_iterator param_type_begin() const {
5600     return getTrailingObjects<QualType>();
5601   }
5602 
5603   param_type_iterator param_type_end() const {
5604     return param_type_begin() + getNumParams();
5605   }
5606 
5607   using exception_iterator = const QualType *;
5608 
5609   ArrayRef<QualType> exceptions() const {
5610     return {exception_begin(), exception_end()};
5611   }
5612 
5613   exception_iterator exception_begin() const {
5614     return reinterpret_cast<exception_iterator>(
5615         getTrailingObjects<ExceptionType>());
5616   }
5617 
5618   exception_iterator exception_end() const {
5619     return exception_begin() + getNumExceptions();
5620   }
5621 
5622   /// Is there any interesting extra information for any of the parameters
5623   /// of this function type?
5624   bool hasExtParameterInfos() const {
5625     return FunctionTypeBits.HasExtParameterInfos;
5626   }
5627 
5628   ArrayRef<ExtParameterInfo> getExtParameterInfos() const {
5629     assert(hasExtParameterInfos());
5630     return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(),
5631                                       getNumParams());
5632   }
5633 
5634   /// Return a pointer to the beginning of the array of extra parameter
5635   /// information, if present, or else null if none of the parameters
5636   /// carry it.  This is equivalent to getExtProtoInfo().ExtParameterInfos.
5637   const ExtParameterInfo *getExtParameterInfosOrNull() const {
5638     if (!hasExtParameterInfos())
5639       return nullptr;
5640     return getTrailingObjects<ExtParameterInfo>();
5641   }
5642 
5643   /// Return a bitmask describing the SME attributes on the function type, see
5644   /// AArch64SMETypeAttributes for their values.
5645   unsigned getAArch64SMEAttributes() const {
5646     if (!hasArmTypeAttributes())
5647       return SME_NormalFunction;
5648     return getTrailingObjects<FunctionTypeArmAttributes>()
5649         ->AArch64SMEAttributes;
5650   }
5651 
5652   ExtParameterInfo getExtParameterInfo(unsigned I) const {
5653     assert(I < getNumParams() && "parameter index out of range");
5654     if (hasExtParameterInfos())
5655       return getTrailingObjects<ExtParameterInfo>()[I];
5656     return ExtParameterInfo();
5657   }
5658 
5659   ParameterABI getParameterABI(unsigned I) const {
5660     assert(I < getNumParams() && "parameter index out of range");
5661     if (hasExtParameterInfos())
5662       return getTrailingObjects<ExtParameterInfo>()[I].getABI();
5663     return ParameterABI::Ordinary;
5664   }
5665 
5666   bool isParamConsumed(unsigned I) const {
5667     assert(I < getNumParams() && "parameter index out of range");
5668     if (hasExtParameterInfos())
5669       return getTrailingObjects<ExtParameterInfo>()[I].isConsumed();
5670     return false;
5671   }
5672 
5673   unsigned getNumFunctionEffects() const {
5674     return hasExtraBitfields()
5675                ? getTrailingObjects<FunctionTypeExtraBitfields>()
5676                      ->NumFunctionEffects
5677                : 0;
5678   }
5679 
5680   // For serialization.
5681   ArrayRef<FunctionEffect> getFunctionEffectsWithoutConditions() const {
5682     if (hasExtraBitfields()) {
5683       const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>();
5684       if (Bitfields->NumFunctionEffects > 0)
5685         return getTrailingObjects<FunctionEffect>(
5686             Bitfields->NumFunctionEffects);
5687     }
5688     return {};
5689   }
5690 
5691   unsigned getNumFunctionEffectConditions() const {
5692     if (hasExtraBitfields()) {
5693       const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>();
5694       if (Bitfields->EffectsHaveConditions)
5695         return Bitfields->NumFunctionEffects;
5696     }
5697     return 0;
5698   }
5699 
5700   // For serialization.
5701   ArrayRef<EffectConditionExpr> getFunctionEffectConditions() const {
5702     if (hasExtraBitfields()) {
5703       const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>();
5704       if (Bitfields->EffectsHaveConditions)
5705         return getTrailingObjects<EffectConditionExpr>(
5706             Bitfields->NumFunctionEffects);
5707     }
5708     return {};
5709   }
5710 
5711   // Combines effects with their conditions.
5712   FunctionEffectsRef getFunctionEffects() const {
5713     if (hasExtraBitfields()) {
5714       const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>();
5715       if (Bitfields->NumFunctionEffects > 0) {
5716         const size_t NumConds = Bitfields->EffectsHaveConditions
5717                                     ? Bitfields->NumFunctionEffects
5718                                     : 0;
5719         return FunctionEffectsRef(
5720             getTrailingObjects<FunctionEffect>(Bitfields->NumFunctionEffects),
5721             {NumConds ? getTrailingObjects<EffectConditionExpr>() : nullptr,
5722              NumConds});
5723       }
5724     }
5725     return {};
5726   }
5727 
5728   bool isSugared() const { return false; }
5729   QualType desugar() const { return QualType(this, 0); }
5730 
5731   void printExceptionSpecification(raw_ostream &OS,
5732                                    const PrintingPolicy &Policy) const;
5733 
5734   static bool classof(const Type *T) {
5735     return T->getTypeClass() == FunctionProto;
5736   }
5737 
5738   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
5739   static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
5740                       param_type_iterator ArgTys, unsigned NumArgs,
5741                       const ExtProtoInfo &EPI, const ASTContext &Context,
5742                       bool Canonical);
5743 };
5744 
5745 /// Represents the dependent type named by a dependently-scoped
5746 /// typename using declaration, e.g.
5747 ///   using typename Base<T>::foo;
5748 ///
5749 /// Template instantiation turns these into the underlying type.
5750 class UnresolvedUsingType : public Type {
5751   friend class ASTContext; // ASTContext creates these.
5752 
5753   UnresolvedUsingTypenameDecl *Decl;
5754 
5755   UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D)
5756       : Type(UnresolvedUsing, QualType(),
5757              TypeDependence::DependentInstantiation),
5758         Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {}
5759 
5760 public:
5761   UnresolvedUsingTypenameDecl *getDecl() const { return Decl; }
5762 
5763   bool isSugared() const { return false; }
5764   QualType desugar() const { return QualType(this, 0); }
5765 
5766   static bool classof(const Type *T) {
5767     return T->getTypeClass() == UnresolvedUsing;
5768   }
5769 
5770   void Profile(llvm::FoldingSetNodeID &ID) {
5771     return Profile(ID, Decl);
5772   }
5773 
5774   static void Profile(llvm::FoldingSetNodeID &ID,
5775                       UnresolvedUsingTypenameDecl *D) {
5776     ID.AddPointer(D);
5777   }
5778 };
5779 
5780 class UsingType final : public Type,
5781                         public llvm::FoldingSetNode,
5782                         private llvm::TrailingObjects<UsingType, QualType> {
5783   UsingShadowDecl *Found;
5784   friend class ASTContext; // ASTContext creates these.
5785   friend TrailingObjects;
5786 
5787   UsingType(const UsingShadowDecl *Found, QualType Underlying, QualType Canon);
5788 
5789 public:
5790   UsingShadowDecl *getFoundDecl() const { return Found; }
5791   QualType getUnderlyingType() const;
5792 
5793   bool isSugared() const { return true; }
5794 
5795   // This always has the 'same' type as declared, but not necessarily identical.
5796   QualType desugar() const { return getUnderlyingType(); }
5797 
5798   // Internal helper, for debugging purposes.
5799   bool typeMatchesDecl() const { return !UsingBits.hasTypeDifferentFromDecl; }
5800 
5801   void Profile(llvm::FoldingSetNodeID &ID) {
5802     Profile(ID, Found, getUnderlyingType());
5803   }
5804   static void Profile(llvm::FoldingSetNodeID &ID, const UsingShadowDecl *Found,
5805                       QualType Underlying) {
5806     ID.AddPointer(Found);
5807     Underlying.Profile(ID);
5808   }
5809   static bool classof(const Type *T) { return T->getTypeClass() == Using; }
5810 };
5811 
5812 class TypedefType final : public Type,
5813                           public llvm::FoldingSetNode,
5814                           private llvm::TrailingObjects<TypedefType, QualType> {
5815   TypedefNameDecl *Decl;
5816   friend class ASTContext; // ASTContext creates these.
5817   friend TrailingObjects;
5818 
5819   TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType UnderlyingType,
5820               bool HasTypeDifferentFromDecl);
5821 
5822 public:
5823   TypedefNameDecl *getDecl() const { return Decl; }
5824 
5825   bool isSugared() const { return true; }
5826 
5827   // This always has the 'same' type as declared, but not necessarily identical.
5828   QualType desugar() const;
5829 
5830   // Internal helper, for debugging purposes.
5831   bool typeMatchesDecl() const { return !TypedefBits.hasTypeDifferentFromDecl; }
5832 
5833   void Profile(llvm::FoldingSetNodeID &ID) {
5834     Profile(ID, Decl, typeMatchesDecl() ? QualType() : desugar());
5835   }
5836   static void Profile(llvm::FoldingSetNodeID &ID, const TypedefNameDecl *Decl,
5837                       QualType Underlying) {
5838     ID.AddPointer(Decl);
5839     if (!Underlying.isNull())
5840       Underlying.Profile(ID);
5841   }
5842 
5843   static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
5844 };
5845 
5846 /// Sugar type that represents a type that was qualified by a qualifier written
5847 /// as a macro invocation.
5848 class MacroQualifiedType : public Type {
5849   friend class ASTContext; // ASTContext creates these.
5850 
5851   QualType UnderlyingTy;
5852   const IdentifierInfo *MacroII;
5853 
5854   MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy,
5855                      const IdentifierInfo *MacroII)
5856       : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()),
5857         UnderlyingTy(UnderlyingTy), MacroII(MacroII) {
5858     assert(isa<AttributedType>(UnderlyingTy) &&
5859            "Expected a macro qualified type to only wrap attributed types.");
5860   }
5861 
5862 public:
5863   const IdentifierInfo *getMacroIdentifier() const { return MacroII; }
5864   QualType getUnderlyingType() const { return UnderlyingTy; }
5865 
5866   /// Return this attributed type's modified type with no qualifiers attached to
5867   /// it.
5868   QualType getModifiedType() const;
5869 
5870   bool isSugared() const { return true; }
5871   QualType desugar() const;
5872 
5873   static bool classof(const Type *T) {
5874     return T->getTypeClass() == MacroQualified;
5875   }
5876 };
5877 
5878 /// Represents a `typeof` (or __typeof__) expression (a C23 feature and GCC
5879 /// extension) or a `typeof_unqual` expression (a C23 feature).
5880 class TypeOfExprType : public Type {
5881   Expr *TOExpr;
5882   const ASTContext &Context;
5883 
5884 protected:
5885   friend class ASTContext; // ASTContext creates these.
5886 
5887   TypeOfExprType(const ASTContext &Context, Expr *E, TypeOfKind Kind,
5888                  QualType Can = QualType());
5889 
5890 public:
5891   Expr *getUnderlyingExpr() const { return TOExpr; }
5892 
5893   /// Returns the kind of 'typeof' type this is.
5894   TypeOfKind getKind() const {
5895     return static_cast<TypeOfKind>(TypeOfBits.Kind);
5896   }
5897 
5898   /// Remove a single level of sugar.
5899   QualType desugar() const;
5900 
5901   /// Returns whether this type directly provides sugar.
5902   bool isSugared() const;
5903 
5904   static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
5905 };
5906 
5907 /// Internal representation of canonical, dependent
5908 /// `typeof(expr)` types.
5909 ///
5910 /// This class is used internally by the ASTContext to manage
5911 /// canonical, dependent types, only. Clients will only see instances
5912 /// of this class via TypeOfExprType nodes.
5913 class DependentTypeOfExprType : public TypeOfExprType,
5914                                 public llvm::FoldingSetNode {
5915 public:
5916   DependentTypeOfExprType(const ASTContext &Context, Expr *E, TypeOfKind Kind)
5917       : TypeOfExprType(Context, E, Kind) {}
5918 
5919   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
5920     Profile(ID, Context, getUnderlyingExpr(),
5921             getKind() == TypeOfKind::Unqualified);
5922   }
5923 
5924   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
5925                       Expr *E, bool IsUnqual);
5926 };
5927 
5928 /// Represents `typeof(type)`, a C23 feature and GCC extension, or
5929 /// `typeof_unqual(type), a C23 feature.
5930 class TypeOfType : public Type {
5931   friend class ASTContext; // ASTContext creates these.
5932 
5933   QualType TOType;
5934   const ASTContext &Context;
5935 
5936   TypeOfType(const ASTContext &Context, QualType T, QualType Can,
5937              TypeOfKind Kind);
5938 
5939 public:
5940   QualType getUnmodifiedType() const { return TOType; }
5941 
5942   /// Remove a single level of sugar.
5943   QualType desugar() const;
5944 
5945   /// Returns whether this type directly provides sugar.
5946   bool isSugared() const { return true; }
5947 
5948   /// Returns the kind of 'typeof' type this is.
5949   TypeOfKind getKind() const {
5950     return static_cast<TypeOfKind>(TypeOfBits.Kind);
5951   }
5952 
5953   static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
5954 };
5955 
5956 /// Represents the type `decltype(expr)` (C++11).
5957 class DecltypeType : public Type {
5958   Expr *E;
5959   QualType UnderlyingType;
5960 
5961 protected:
5962   friend class ASTContext; // ASTContext creates these.
5963 
5964   DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType());
5965 
5966 public:
5967   Expr *getUnderlyingExpr() const { return E; }
5968   QualType getUnderlyingType() const { return UnderlyingType; }
5969 
5970   /// Remove a single level of sugar.
5971   QualType desugar() const;
5972 
5973   /// Returns whether this type directly provides sugar.
5974   bool isSugared() const;
5975 
5976   static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
5977 };
5978 
5979 /// Internal representation of canonical, dependent
5980 /// decltype(expr) types.
5981 ///
5982 /// This class is used internally by the ASTContext to manage
5983 /// canonical, dependent types, only. Clients will only see instances
5984 /// of this class via DecltypeType nodes.
5985 class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode {
5986 public:
5987   DependentDecltypeType(Expr *E);
5988 
5989   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
5990     Profile(ID, Context, getUnderlyingExpr());
5991   }
5992 
5993   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
5994                       Expr *E);
5995 };
5996 
5997 class PackIndexingType final
5998     : public Type,
5999       public llvm::FoldingSetNode,
6000       private llvm::TrailingObjects<PackIndexingType, QualType> {
6001   friend TrailingObjects;
6002 
6003   QualType Pattern;
6004   Expr *IndexExpr;
6005 
6006   unsigned Size : 31;
6007 
6008   LLVM_PREFERRED_TYPE(bool)
6009   unsigned FullySubstituted : 1;
6010 
6011 protected:
6012   friend class ASTContext; // ASTContext creates these.
6013   PackIndexingType(QualType Canonical, QualType Pattern, Expr *IndexExpr,
6014                    bool FullySubstituted, ArrayRef<QualType> Expansions = {});
6015 
6016 public:
6017   Expr *getIndexExpr() const { return IndexExpr; }
6018   QualType getPattern() const { return Pattern; }
6019 
6020   bool isSugared() const { return hasSelectedType(); }
6021 
6022   QualType desugar() const {
6023     if (hasSelectedType())
6024       return getSelectedType();
6025     return QualType(this, 0);
6026   }
6027 
6028   QualType getSelectedType() const {
6029     assert(hasSelectedType() && "Type is dependant");
6030     return *(getExpansionsPtr() + *getSelectedIndex());
6031   }
6032 
6033   UnsignedOrNone getSelectedIndex() const;
6034 
6035   bool hasSelectedType() const { return getSelectedIndex() != std::nullopt; }
6036 
6037   bool isFullySubstituted() const { return FullySubstituted; }
6038 
6039   bool expandsToEmptyPack() const { return isFullySubstituted() && Size == 0; }
6040 
6041   ArrayRef<QualType> getExpansions() const {
6042     return {getExpansionsPtr(), Size};
6043   }
6044 
6045   static bool classof(const Type *T) {
6046     return T->getTypeClass() == PackIndexing;
6047   }
6048 
6049   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context);
6050   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
6051                       QualType Pattern, Expr *E, bool FullySubstituted,
6052                       ArrayRef<QualType> Expansions);
6053 
6054 private:
6055   const QualType *getExpansionsPtr() const { return getTrailingObjects(); }
6056 
6057   static TypeDependence computeDependence(QualType Pattern, Expr *IndexExpr,
6058                                           ArrayRef<QualType> Expansions = {});
6059 };
6060 
6061 /// A unary type transform, which is a type constructed from another.
6062 class UnaryTransformType : public Type, public llvm::FoldingSetNode {
6063 public:
6064   enum UTTKind {
6065 #define TRANSFORM_TYPE_TRAIT_DEF(Enum, _) Enum,
6066 #include "clang/Basic/TransformTypeTraits.def"
6067   };
6068 
6069 private:
6070   /// The untransformed type.
6071   QualType BaseType;
6072 
6073   /// The transformed type if not dependent, otherwise the same as BaseType.
6074   QualType UnderlyingType;
6075 
6076   UTTKind UKind;
6077 
6078 protected:
6079   friend class ASTContext;
6080 
6081   UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind,
6082                      QualType CanonicalTy);
6083 
6084 public:
6085   bool isSugared() const { return !isDependentType(); }
6086   QualType desugar() const { return UnderlyingType; }
6087 
6088   QualType getUnderlyingType() const { return UnderlyingType; }
6089   QualType getBaseType() const { return BaseType; }
6090 
6091   UTTKind getUTTKind() const { return UKind; }
6092 
6093   static bool classof(const Type *T) {
6094     return T->getTypeClass() == UnaryTransform;
6095   }
6096 
6097   void Profile(llvm::FoldingSetNodeID &ID) {
6098     Profile(ID, getBaseType(), getUnderlyingType(), getUTTKind());
6099   }
6100 
6101   static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType,
6102                       QualType UnderlyingType, UTTKind UKind) {
6103     BaseType.Profile(ID);
6104     UnderlyingType.Profile(ID);
6105     ID.AddInteger(UKind);
6106   }
6107 };
6108 
6109 class TagType : public Type {
6110   friend class ASTReader;
6111   template <class T> friend class serialization::AbstractTypeReader;
6112 
6113   /// Stores the TagDecl associated with this type. The decl may point to any
6114   /// TagDecl that declares the entity.
6115   TagDecl *decl;
6116 
6117 protected:
6118   TagType(TypeClass TC, const TagDecl *D, QualType can);
6119 
6120 public:
6121   TagDecl *getDecl() const;
6122 
6123   /// Determines whether this type is in the process of being defined.
6124   bool isBeingDefined() const;
6125 
6126   static bool classof(const Type *T) {
6127     return T->getTypeClass() == Enum || T->getTypeClass() == Record;
6128   }
6129 };
6130 
6131 /// A helper class that allows the use of isa/cast/dyncast
6132 /// to detect TagType objects of structs/unions/classes.
6133 class RecordType : public TagType {
6134 protected:
6135   friend class ASTContext; // ASTContext creates these.
6136 
6137   explicit RecordType(const RecordDecl *D)
6138       : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {}
6139   explicit RecordType(TypeClass TC, RecordDecl *D)
6140       : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {}
6141 
6142 public:
6143   RecordDecl *getDecl() const {
6144     return reinterpret_cast<RecordDecl*>(TagType::getDecl());
6145   }
6146 
6147   /// Recursively check all fields in the record for const-ness. If any field
6148   /// is declared const, return true. Otherwise, return false.
6149   bool hasConstFields() const;
6150 
6151   bool isSugared() const { return false; }
6152   QualType desugar() const { return QualType(this, 0); }
6153 
6154   static bool classof(const Type *T) { return T->getTypeClass() == Record; }
6155 };
6156 
6157 /// A helper class that allows the use of isa/cast/dyncast
6158 /// to detect TagType objects of enums.
6159 class EnumType : public TagType {
6160   friend class ASTContext; // ASTContext creates these.
6161 
6162   explicit EnumType(const EnumDecl *D)
6163       : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {}
6164 
6165 public:
6166   EnumDecl *getDecl() const {
6167     return reinterpret_cast<EnumDecl*>(TagType::getDecl());
6168   }
6169 
6170   bool isSugared() const { return false; }
6171   QualType desugar() const { return QualType(this, 0); }
6172 
6173   static bool classof(const Type *T) { return T->getTypeClass() == Enum; }
6174 };
6175 
6176 /// An attributed type is a type to which a type attribute has been applied.
6177 ///
6178 /// The "modified type" is the fully-sugared type to which the attributed
6179 /// type was applied; generally it is not canonically equivalent to the
6180 /// attributed type. The "equivalent type" is the minimally-desugared type
6181 /// which the type is canonically equivalent to.
6182 ///
6183 /// For example, in the following attributed type:
6184 ///     int32_t __attribute__((vector_size(16)))
6185 ///   - the modified type is the TypedefType for int32_t
6186 ///   - the equivalent type is VectorType(16, int32_t)
6187 ///   - the canonical type is VectorType(16, int)
6188 class AttributedType : public Type, public llvm::FoldingSetNode {
6189 public:
6190   using Kind = attr::Kind;
6191 
6192 private:
6193   friend class ASTContext; // ASTContext creates these
6194 
6195   const Attr *Attribute;
6196 
6197   QualType ModifiedType;
6198   QualType EquivalentType;
6199 
6200   AttributedType(QualType canon, attr::Kind attrKind, QualType modified,
6201                  QualType equivalent)
6202       : AttributedType(canon, attrKind, nullptr, modified, equivalent) {}
6203 
6204   AttributedType(QualType canon, const Attr *attr, QualType modified,
6205                  QualType equivalent);
6206 
6207 private:
6208   AttributedType(QualType canon, attr::Kind attrKind, const Attr *attr,
6209                  QualType modified, QualType equivalent);
6210 
6211 public:
6212   Kind getAttrKind() const {
6213     return static_cast<Kind>(AttributedTypeBits.AttrKind);
6214   }
6215 
6216   const Attr *getAttr() const { return Attribute; }
6217 
6218   QualType getModifiedType() const { return ModifiedType; }
6219   QualType getEquivalentType() const { return EquivalentType; }
6220 
6221   bool isSugared() const { return true; }
6222   QualType desugar() const { return getEquivalentType(); }
6223 
6224   /// Does this attribute behave like a type qualifier?
6225   ///
6226   /// A type qualifier adjusts a type to provide specialized rules for
6227   /// a specific object, like the standard const and volatile qualifiers.
6228   /// This includes attributes controlling things like nullability,
6229   /// address spaces, and ARC ownership.  The value of the object is still
6230   /// largely described by the modified type.
6231   ///
6232   /// In contrast, many type attributes "rewrite" their modified type to
6233   /// produce a fundamentally different type, not necessarily related in any
6234   /// formalizable way to the original type.  For example, calling convention
6235   /// and vector attributes are not simple type qualifiers.
6236   ///
6237   /// Type qualifiers are often, but not always, reflected in the canonical
6238   /// type.
6239   bool isQualifier() const;
6240 
6241   bool isMSTypeSpec() const;
6242 
6243   bool isWebAssemblyFuncrefSpec() const;
6244 
6245   bool isCallingConv() const;
6246 
6247   std::optional<NullabilityKind> getImmediateNullability() const;
6248 
6249   /// Strip off the top-level nullability annotation on the given
6250   /// type, if it's there.
6251   ///
6252   /// \param T The type to strip. If the type is exactly an
6253   /// AttributedType specifying nullability (without looking through
6254   /// type sugar), the nullability is returned and this type changed
6255   /// to the underlying modified type.
6256   ///
6257   /// \returns the top-level nullability, if present.
6258   static std::optional<NullabilityKind> stripOuterNullability(QualType &T);
6259 
6260   void Profile(llvm::FoldingSetNodeID &ID) {
6261     Profile(ID, getAttrKind(), ModifiedType, EquivalentType, Attribute);
6262   }
6263 
6264   static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind,
6265                       QualType modified, QualType equivalent,
6266                       const Attr *attr) {
6267     ID.AddInteger(attrKind);
6268     ID.AddPointer(modified.getAsOpaquePtr());
6269     ID.AddPointer(equivalent.getAsOpaquePtr());
6270     ID.AddPointer(attr);
6271   }
6272 
6273   static bool classof(const Type *T) {
6274     return T->getTypeClass() == Attributed;
6275   }
6276 };
6277 
6278 class BTFTagAttributedType : public Type, public llvm::FoldingSetNode {
6279 private:
6280   friend class ASTContext; // ASTContext creates these
6281 
6282   QualType WrappedType;
6283   const BTFTypeTagAttr *BTFAttr;
6284 
6285   BTFTagAttributedType(QualType Canon, QualType Wrapped,
6286                        const BTFTypeTagAttr *BTFAttr)
6287       : Type(BTFTagAttributed, Canon, Wrapped->getDependence()),
6288         WrappedType(Wrapped), BTFAttr(BTFAttr) {}
6289 
6290 public:
6291   QualType getWrappedType() const { return WrappedType; }
6292   const BTFTypeTagAttr *getAttr() const { return BTFAttr; }
6293 
6294   bool isSugared() const { return true; }
6295   QualType desugar() const { return getWrappedType(); }
6296 
6297   void Profile(llvm::FoldingSetNodeID &ID) {
6298     Profile(ID, WrappedType, BTFAttr);
6299   }
6300 
6301   static void Profile(llvm::FoldingSetNodeID &ID, QualType Wrapped,
6302                       const BTFTypeTagAttr *BTFAttr) {
6303     ID.AddPointer(Wrapped.getAsOpaquePtr());
6304     ID.AddPointer(BTFAttr);
6305   }
6306 
6307   static bool classof(const Type *T) {
6308     return T->getTypeClass() == BTFTagAttributed;
6309   }
6310 };
6311 
6312 class HLSLAttributedResourceType : public Type, public llvm::FoldingSetNode {
6313 public:
6314   struct Attributes {
6315     // Data gathered from HLSL resource attributes
6316     llvm::dxil::ResourceClass ResourceClass;
6317 
6318     LLVM_PREFERRED_TYPE(bool)
6319     uint8_t IsROV : 1;
6320 
6321     LLVM_PREFERRED_TYPE(bool)
6322     uint8_t RawBuffer : 1;
6323 
6324     Attributes(llvm::dxil::ResourceClass ResourceClass, bool IsROV = false,
6325                bool RawBuffer = false)
6326         : ResourceClass(ResourceClass), IsROV(IsROV), RawBuffer(RawBuffer) {}
6327 
6328     Attributes() : Attributes(llvm::dxil::ResourceClass::UAV, false, false) {}
6329 
6330     friend bool operator==(const Attributes &LHS, const Attributes &RHS) {
6331       return std::tie(LHS.ResourceClass, LHS.IsROV, LHS.RawBuffer) ==
6332              std::tie(RHS.ResourceClass, RHS.IsROV, RHS.RawBuffer);
6333     }
6334     friend bool operator!=(const Attributes &LHS, const Attributes &RHS) {
6335       return !(LHS == RHS);
6336     }
6337   };
6338 
6339 private:
6340   friend class ASTContext; // ASTContext creates these
6341 
6342   QualType WrappedType;
6343   QualType ContainedType;
6344   const Attributes Attrs;
6345 
6346   HLSLAttributedResourceType(QualType Wrapped, QualType Contained,
6347                              const Attributes &Attrs)
6348       : Type(HLSLAttributedResource, QualType(),
6349              Contained.isNull() ? TypeDependence::None
6350                                 : Contained->getDependence()),
6351         WrappedType(Wrapped), ContainedType(Contained), Attrs(Attrs) {}
6352 
6353 public:
6354   QualType getWrappedType() const { return WrappedType; }
6355   QualType getContainedType() const { return ContainedType; }
6356   bool hasContainedType() const { return !ContainedType.isNull(); }
6357   const Attributes &getAttrs() const { return Attrs; }
6358 
6359   bool isSugared() const { return false; }
6360   QualType desugar() const { return QualType(this, 0); }
6361 
6362   void Profile(llvm::FoldingSetNodeID &ID) {
6363     Profile(ID, WrappedType, ContainedType, Attrs);
6364   }
6365 
6366   static void Profile(llvm::FoldingSetNodeID &ID, QualType Wrapped,
6367                       QualType Contained, const Attributes &Attrs) {
6368     ID.AddPointer(Wrapped.getAsOpaquePtr());
6369     ID.AddPointer(Contained.getAsOpaquePtr());
6370     ID.AddInteger(static_cast<uint32_t>(Attrs.ResourceClass));
6371     ID.AddBoolean(Attrs.IsROV);
6372     ID.AddBoolean(Attrs.RawBuffer);
6373   }
6374 
6375   static bool classof(const Type *T) {
6376     return T->getTypeClass() == HLSLAttributedResource;
6377   }
6378 
6379   // Returns handle type from HLSL resource, if the type is a resource
6380   static const HLSLAttributedResourceType *
6381   findHandleTypeOnResource(const Type *RT);
6382 };
6383 
6384 /// Instances of this class represent operands to a SPIR-V type instruction.
6385 class SpirvOperand {
6386 public:
6387   enum SpirvOperandKind : unsigned char {
6388     Invalid,    ///< Uninitialized.
6389     ConstantId, ///< Integral value to represent as a SPIR-V OpConstant
6390                 ///< instruction ID.
6391     Literal,    ///< Integral value to represent as an immediate literal.
6392     TypeId,     ///< Type to represent as a SPIR-V type ID.
6393 
6394     Max,
6395   };
6396 
6397 private:
6398   SpirvOperandKind Kind = Invalid;
6399 
6400   QualType ResultType;
6401   llvm::APInt Value; // Signedness of constants is represented by ResultType.
6402 
6403 public:
6404   SpirvOperand() : Kind(Invalid), ResultType(), Value() {}
6405 
6406   SpirvOperand(SpirvOperandKind Kind, QualType ResultType, llvm::APInt Value)
6407       : Kind(Kind), ResultType(ResultType), Value(std::move(Value)) {}
6408 
6409   SpirvOperand(const SpirvOperand &Other) { *this = Other; }
6410   ~SpirvOperand() {}
6411 
6412   SpirvOperand &operator=(const SpirvOperand &Other) = default;
6413 
6414   bool operator==(const SpirvOperand &Other) const {
6415     return Kind == Other.Kind && ResultType == Other.ResultType &&
6416            Value == Other.Value;
6417   }
6418 
6419   bool operator!=(const SpirvOperand &Other) const { return !(*this == Other); }
6420 
6421   SpirvOperandKind getKind() const { return Kind; }
6422 
6423   bool isValid() const { return Kind != Invalid && Kind < Max; }
6424   bool isConstant() const { return Kind == ConstantId; }
6425   bool isLiteral() const { return Kind == Literal; }
6426   bool isType() const { return Kind == TypeId; }
6427 
6428   llvm::APInt getValue() const {
6429     assert((isConstant() || isLiteral()) &&
6430            "This is not an operand with a value!");
6431     return Value;
6432   }
6433 
6434   QualType getResultType() const {
6435     assert((isConstant() || isType()) &&
6436            "This is not an operand with a result type!");
6437     return ResultType;
6438   }
6439 
6440   static SpirvOperand createConstant(QualType ResultType, llvm::APInt Val) {
6441     return SpirvOperand(ConstantId, ResultType, std::move(Val));
6442   }
6443 
6444   static SpirvOperand createLiteral(llvm::APInt Val) {
6445     return SpirvOperand(Literal, QualType(), std::move(Val));
6446   }
6447 
6448   static SpirvOperand createType(QualType T) {
6449     return SpirvOperand(TypeId, T, llvm::APSInt());
6450   }
6451 
6452   void Profile(llvm::FoldingSetNodeID &ID) const {
6453     ID.AddInteger(Kind);
6454     ID.AddPointer(ResultType.getAsOpaquePtr());
6455     Value.Profile(ID);
6456   }
6457 };
6458 
6459 /// Represents an arbitrary, user-specified SPIR-V type instruction.
6460 class HLSLInlineSpirvType final
6461     : public Type,
6462       public llvm::FoldingSetNode,
6463       private llvm::TrailingObjects<HLSLInlineSpirvType, SpirvOperand> {
6464   friend class ASTContext; // ASTContext creates these
6465   friend TrailingObjects;
6466 
6467 private:
6468   uint32_t Opcode;
6469   uint32_t Size;
6470   uint32_t Alignment;
6471   size_t NumOperands;
6472 
6473   HLSLInlineSpirvType(uint32_t Opcode, uint32_t Size, uint32_t Alignment,
6474                       ArrayRef<SpirvOperand> Operands)
6475       : Type(HLSLInlineSpirv, QualType(), TypeDependence::None), Opcode(Opcode),
6476         Size(Size), Alignment(Alignment), NumOperands(Operands.size()) {
6477     for (size_t I = 0; I < NumOperands; I++) {
6478       // Since Operands are stored as a trailing object, they have not been
6479       // initialized yet. Call the constructor manually.
6480       auto *Operand = new (&getTrailingObjects()[I]) SpirvOperand();
6481       *Operand = Operands[I];
6482     }
6483   }
6484 
6485 public:
6486   uint32_t getOpcode() const { return Opcode; }
6487   uint32_t getSize() const { return Size; }
6488   uint32_t getAlignment() const { return Alignment; }
6489   ArrayRef<SpirvOperand> getOperands() const {
6490     return getTrailingObjects(NumOperands);
6491   }
6492 
6493   bool isSugared() const { return false; }
6494   QualType desugar() const { return QualType(this, 0); }
6495 
6496   void Profile(llvm::FoldingSetNodeID &ID) {
6497     Profile(ID, Opcode, Size, Alignment, getOperands());
6498   }
6499 
6500   static void Profile(llvm::FoldingSetNodeID &ID, uint32_t Opcode,
6501                       uint32_t Size, uint32_t Alignment,
6502                       ArrayRef<SpirvOperand> Operands) {
6503     ID.AddInteger(Opcode);
6504     ID.AddInteger(Size);
6505     ID.AddInteger(Alignment);
6506     for (auto &Operand : Operands)
6507       Operand.Profile(ID);
6508   }
6509 
6510   static bool classof(const Type *T) {
6511     return T->getTypeClass() == HLSLInlineSpirv;
6512   }
6513 };
6514 
6515 class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
6516   friend class ASTContext; // ASTContext creates these
6517 
6518   // The associated TemplateTypeParmDecl for the non-canonical type.
6519   TemplateTypeParmDecl *TTPDecl;
6520 
6521   TemplateTypeParmType(unsigned D, unsigned I, bool PP,
6522                        TemplateTypeParmDecl *TTPDecl, QualType Canon)
6523       : Type(TemplateTypeParm, Canon,
6524              TypeDependence::DependentInstantiation |
6525                  (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)),
6526         TTPDecl(TTPDecl) {
6527     assert(!TTPDecl == Canon.isNull());
6528     TemplateTypeParmTypeBits.Depth = D;
6529     TemplateTypeParmTypeBits.Index = I;
6530     TemplateTypeParmTypeBits.ParameterPack = PP;
6531   }
6532 
6533 public:
6534   unsigned getDepth() const { return TemplateTypeParmTypeBits.Depth; }
6535   unsigned getIndex() const { return TemplateTypeParmTypeBits.Index; }
6536   bool isParameterPack() const {
6537     return TemplateTypeParmTypeBits.ParameterPack;
6538   }
6539 
6540   TemplateTypeParmDecl *getDecl() const { return TTPDecl; }
6541 
6542   IdentifierInfo *getIdentifier() const;
6543 
6544   bool isSugared() const { return false; }
6545   QualType desugar() const { return QualType(this, 0); }
6546 
6547   void Profile(llvm::FoldingSetNodeID &ID) {
6548     Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl());
6549   }
6550 
6551   static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
6552                       unsigned Index, bool ParameterPack,
6553                       TemplateTypeParmDecl *TTPDecl) {
6554     ID.AddInteger(Depth);
6555     ID.AddInteger(Index);
6556     ID.AddBoolean(ParameterPack);
6557     ID.AddPointer(TTPDecl);
6558   }
6559 
6560   static bool classof(const Type *T) {
6561     return T->getTypeClass() == TemplateTypeParm;
6562   }
6563 };
6564 
6565 /// Represents the result of substituting a type for a template
6566 /// type parameter.
6567 ///
6568 /// Within an instantiated template, all template type parameters have
6569 /// been replaced with these.  They are used solely to record that a
6570 /// type was originally written as a template type parameter;
6571 /// therefore they are never canonical.
6572 class SubstTemplateTypeParmType final
6573     : public Type,
6574       public llvm::FoldingSetNode,
6575       private llvm::TrailingObjects<SubstTemplateTypeParmType, QualType> {
6576   friend class ASTContext;
6577   friend class llvm::TrailingObjects<SubstTemplateTypeParmType, QualType>;
6578 
6579   Decl *AssociatedDecl;
6580 
6581   SubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl,
6582                             unsigned Index, UnsignedOrNone PackIndex,
6583                             bool Final);
6584 
6585 public:
6586   /// Gets the type that was substituted for the template
6587   /// parameter.
6588   QualType getReplacementType() const {
6589     return SubstTemplateTypeParmTypeBits.HasNonCanonicalUnderlyingType
6590                ? *getTrailingObjects()
6591                : getCanonicalTypeInternal();
6592   }
6593 
6594   /// A template-like entity which owns the whole pattern being substituted.
6595   /// This will usually own a set of template parameters, or in some
6596   /// cases might even be a template parameter itself.
6597   Decl *getAssociatedDecl() const { return AssociatedDecl; }
6598 
6599   /// Gets the template parameter declaration that was substituted for.
6600   const TemplateTypeParmDecl *getReplacedParameter() const;
6601 
6602   /// Returns the index of the replaced parameter in the associated declaration.
6603   /// This should match the result of `getReplacedParameter()->getIndex()`.
6604   unsigned getIndex() const { return SubstTemplateTypeParmTypeBits.Index; }
6605 
6606   // This substitution is Final, which means the substitution is fully
6607   // sugared: it doesn't need to be resugared later.
6608   unsigned getFinal() const { return SubstTemplateTypeParmTypeBits.Final; }
6609 
6610   UnsignedOrNone getPackIndex() const {
6611     return UnsignedOrNone::fromInternalRepresentation(
6612         SubstTemplateTypeParmTypeBits.PackIndex);
6613   }
6614 
6615   bool isSugared() const { return true; }
6616   QualType desugar() const { return getReplacementType(); }
6617 
6618   void Profile(llvm::FoldingSetNodeID &ID) {
6619     Profile(ID, getReplacementType(), getAssociatedDecl(), getIndex(),
6620             getPackIndex(), getFinal());
6621   }
6622 
6623   static void Profile(llvm::FoldingSetNodeID &ID, QualType Replacement,
6624                       const Decl *AssociatedDecl, unsigned Index,
6625                       UnsignedOrNone PackIndex, bool Final);
6626 
6627   static bool classof(const Type *T) {
6628     return T->getTypeClass() == SubstTemplateTypeParm;
6629   }
6630 };
6631 
6632 /// Represents the result of substituting a set of types for a template
6633 /// type parameter pack.
6634 ///
6635 /// When a pack expansion in the source code contains multiple parameter packs
6636 /// and those parameter packs correspond to different levels of template
6637 /// parameter lists, this type node is used to represent a template type
6638 /// parameter pack from an outer level, which has already had its argument pack
6639 /// substituted but that still lives within a pack expansion that itself
6640 /// could not be instantiated. When actually performing a substitution into
6641 /// that pack expansion (e.g., when all template parameters have corresponding
6642 /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType
6643 /// at the current pack substitution index.
6644 class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode {
6645   friend class ASTContext;
6646 
6647   /// A pointer to the set of template arguments that this
6648   /// parameter pack is instantiated with.
6649   const TemplateArgument *Arguments;
6650 
6651   llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal;
6652 
6653   SubstTemplateTypeParmPackType(QualType Canon, Decl *AssociatedDecl,
6654                                 unsigned Index, bool Final,
6655                                 const TemplateArgument &ArgPack);
6656 
6657 public:
6658   IdentifierInfo *getIdentifier() const;
6659 
6660   /// A template-like entity which owns the whole pattern being substituted.
6661   /// This will usually own a set of template parameters, or in some
6662   /// cases might even be a template parameter itself.
6663   Decl *getAssociatedDecl() const;
6664 
6665   /// Gets the template parameter declaration that was substituted for.
6666   const TemplateTypeParmDecl *getReplacedParameter() const;
6667 
6668   /// Returns the index of the replaced parameter in the associated declaration.
6669   /// This should match the result of `getReplacedParameter()->getIndex()`.
6670   unsigned getIndex() const { return SubstTemplateTypeParmPackTypeBits.Index; }
6671 
6672   // This substitution will be Final, which means the substitution will be fully
6673   // sugared: it doesn't need to be resugared later.
6674   bool getFinal() const;
6675 
6676   unsigned getNumArgs() const {
6677     return SubstTemplateTypeParmPackTypeBits.NumArgs;
6678   }
6679 
6680   bool isSugared() const { return false; }
6681   QualType desugar() const { return QualType(this, 0); }
6682 
6683   TemplateArgument getArgumentPack() const;
6684 
6685   void Profile(llvm::FoldingSetNodeID &ID);
6686   static void Profile(llvm::FoldingSetNodeID &ID, const Decl *AssociatedDecl,
6687                       unsigned Index, bool Final,
6688                       const TemplateArgument &ArgPack);
6689 
6690   static bool classof(const Type *T) {
6691     return T->getTypeClass() == SubstTemplateTypeParmPack;
6692   }
6693 };
6694 
6695 /// Common base class for placeholders for types that get replaced by
6696 /// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced
6697 /// class template types, and constrained type names.
6698 ///
6699 /// These types are usually a placeholder for a deduced type. However, before
6700 /// the initializer is attached, or (usually) if the initializer is
6701 /// type-dependent, there is no deduced type and the type is canonical. In
6702 /// the latter case, it is also a dependent type.
6703 class DeducedType : public Type {
6704   QualType DeducedAsType;
6705 
6706 protected:
6707   DeducedType(TypeClass TC, QualType DeducedAsType,
6708               TypeDependence ExtraDependence, QualType Canon)
6709       : Type(TC, Canon,
6710              ExtraDependence | (DeducedAsType.isNull()
6711                                     ? TypeDependence::None
6712                                     : DeducedAsType->getDependence() &
6713                                           ~TypeDependence::VariablyModified)),
6714         DeducedAsType(DeducedAsType) {}
6715 
6716 public:
6717   bool isSugared() const { return !DeducedAsType.isNull(); }
6718   QualType desugar() const {
6719     return isSugared() ? DeducedAsType : QualType(this, 0);
6720   }
6721 
6722   /// Get the type deduced for this placeholder type, or null if it
6723   /// has not been deduced.
6724   QualType getDeducedType() const { return DeducedAsType; }
6725   bool isDeduced() const {
6726     return !DeducedAsType.isNull() || isDependentType();
6727   }
6728 
6729   static bool classof(const Type *T) {
6730     return T->getTypeClass() == Auto ||
6731            T->getTypeClass() == DeducedTemplateSpecialization;
6732   }
6733 };
6734 
6735 /// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained
6736 /// by a type-constraint.
6737 class AutoType : public DeducedType {
6738   friend class ASTContext; // ASTContext creates these
6739 
6740   ConceptDecl *TypeConstraintConcept;
6741 
6742   AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,
6743            TypeDependence ExtraDependence, QualType Canon, ConceptDecl *CD,
6744            ArrayRef<TemplateArgument> TypeConstraintArgs);
6745 
6746 public:
6747   ArrayRef<TemplateArgument> getTypeConstraintArguments() const {
6748     return {reinterpret_cast<const TemplateArgument *>(this + 1),
6749             AutoTypeBits.NumArgs};
6750   }
6751 
6752   ConceptDecl *getTypeConstraintConcept() const {
6753     return TypeConstraintConcept;
6754   }
6755 
6756   bool isConstrained() const {
6757     return TypeConstraintConcept != nullptr;
6758   }
6759 
6760   bool isDecltypeAuto() const {
6761     return getKeyword() == AutoTypeKeyword::DecltypeAuto;
6762   }
6763 
6764   bool isGNUAutoType() const {
6765     return getKeyword() == AutoTypeKeyword::GNUAutoType;
6766   }
6767 
6768   AutoTypeKeyword getKeyword() const {
6769     return (AutoTypeKeyword)AutoTypeBits.Keyword;
6770   }
6771 
6772   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context);
6773   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
6774                       QualType Deduced, AutoTypeKeyword Keyword,
6775                       bool IsDependent, ConceptDecl *CD,
6776                       ArrayRef<TemplateArgument> Arguments);
6777 
6778   static bool classof(const Type *T) {
6779     return T->getTypeClass() == Auto;
6780   }
6781 };
6782 
6783 /// Represents a C++17 deduced template specialization type.
6784 class DeducedTemplateSpecializationType : public DeducedType,
6785                                           public llvm::FoldingSetNode {
6786   friend class ASTContext; // ASTContext creates these
6787 
6788   /// The name of the template whose arguments will be deduced.
6789   TemplateName Template;
6790 
6791   DeducedTemplateSpecializationType(TemplateName Template,
6792                                     QualType DeducedAsType,
6793                                     bool IsDeducedAsDependent, QualType Canon)
6794       : DeducedType(DeducedTemplateSpecialization, DeducedAsType,
6795                     toTypeDependence(Template.getDependence()) |
6796                         (IsDeducedAsDependent
6797                              ? TypeDependence::DependentInstantiation
6798                              : TypeDependence::None),
6799                     Canon),
6800         Template(Template) {}
6801 
6802 public:
6803   /// Retrieve the name of the template that we are deducing.
6804   TemplateName getTemplateName() const { return Template;}
6805 
6806   void Profile(llvm::FoldingSetNodeID &ID) const {
6807     Profile(ID, getTemplateName(), getDeducedType(), isDependentType());
6808   }
6809 
6810   static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template,
6811                       QualType Deduced, bool IsDependent) {
6812     Template.Profile(ID);
6813     Deduced.Profile(ID);
6814     ID.AddBoolean(IsDependent || Template.isDependent());
6815   }
6816 
6817   static bool classof(const Type *T) {
6818     return T->getTypeClass() == DeducedTemplateSpecialization;
6819   }
6820 };
6821 
6822 /// Represents a type template specialization; the template
6823 /// must be a class template, a type alias template, or a template
6824 /// template parameter.  A template which cannot be resolved to one of
6825 /// these, e.g. because it is written with a dependent scope
6826 /// specifier, is instead represented as a
6827 /// @c DependentTemplateSpecializationType.
6828 ///
6829 /// A non-dependent template specialization type is always "sugar",
6830 /// typically for a \c RecordType.  For example, a class template
6831 /// specialization type of \c vector<int> will refer to a tag type for
6832 /// the instantiation \c std::vector<int, std::allocator<int>>
6833 ///
6834 /// Template specializations are dependent if either the template or
6835 /// any of the template arguments are dependent, in which case the
6836 /// type may also be canonical.
6837 ///
6838 /// Instances of this type are allocated with a trailing array of
6839 /// TemplateArguments, followed by a QualType representing the
6840 /// non-canonical aliased type when the template is a type alias
6841 /// template.
6842 class TemplateSpecializationType : public Type, public llvm::FoldingSetNode {
6843   friend class ASTContext; // ASTContext creates these
6844 
6845   /// The name of the template being specialized.  This is
6846   /// either a TemplateName::Template (in which case it is a
6847   /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a
6848   /// TypeAliasTemplateDecl*), a
6849   /// TemplateName::SubstTemplateTemplateParmPack, or a
6850   /// TemplateName::SubstTemplateTemplateParm (in which case the
6851   /// replacement must, recursively, be one of these).
6852   TemplateName Template;
6853 
6854   TemplateSpecializationType(TemplateName T, bool IsAlias,
6855                              ArrayRef<TemplateArgument> Args,
6856                              QualType Underlying);
6857 
6858 public:
6859   /// Determine whether any of the given template arguments are dependent.
6860   ///
6861   /// The converted arguments should be supplied when known; whether an
6862   /// argument is dependent can depend on the conversions performed on it
6863   /// (for example, a 'const int' passed as a template argument might be
6864   /// dependent if the parameter is a reference but non-dependent if the
6865   /// parameter is an int).
6866   ///
6867   /// Note that the \p Args parameter is unused: this is intentional, to remind
6868   /// the caller that they need to pass in the converted arguments, not the
6869   /// specified arguments.
6870   static bool
6871   anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
6872                                 ArrayRef<TemplateArgument> Converted);
6873   static bool
6874   anyDependentTemplateArguments(const TemplateArgumentListInfo &,
6875                                 ArrayRef<TemplateArgument> Converted);
6876   static bool anyInstantiationDependentTemplateArguments(
6877       ArrayRef<TemplateArgumentLoc> Args);
6878 
6879   /// True if this template specialization type matches a current
6880   /// instantiation in the context in which it is found.
6881   bool isCurrentInstantiation() const {
6882     return isa<InjectedClassNameType>(getCanonicalTypeInternal());
6883   }
6884 
6885   /// Determine if this template specialization type is for a type alias
6886   /// template that has been substituted.
6887   ///
6888   /// Nearly every template specialization type whose template is an alias
6889   /// template will be substituted. However, this is not the case when
6890   /// the specialization contains a pack expansion but the template alias
6891   /// does not have a corresponding parameter pack, e.g.,
6892   ///
6893   /// \code
6894   /// template<typename T, typename U, typename V> struct S;
6895   /// template<typename T, typename U> using A = S<T, int, U>;
6896   /// template<typename... Ts> struct X {
6897   ///   typedef A<Ts...> type; // not a type alias
6898   /// };
6899   /// \endcode
6900   bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; }
6901 
6902   /// Get the aliased type, if this is a specialization of a type alias
6903   /// template.
6904   QualType getAliasedType() const;
6905 
6906   /// Retrieve the name of the template that we are specializing.
6907   TemplateName getTemplateName() const { return Template; }
6908 
6909   ArrayRef<TemplateArgument> template_arguments() const {
6910     return {reinterpret_cast<const TemplateArgument *>(this + 1),
6911             TemplateSpecializationTypeBits.NumArgs};
6912   }
6913 
6914   bool isSugared() const {
6915     return !isDependentType() || isCurrentInstantiation() || isTypeAlias();
6916   }
6917 
6918   QualType desugar() const {
6919     return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal();
6920   }
6921 
6922   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
6923   static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
6924                       ArrayRef<TemplateArgument> Args, QualType Underlying,
6925                       const ASTContext &Context);
6926 
6927   static bool classof(const Type *T) {
6928     return T->getTypeClass() == TemplateSpecialization;
6929   }
6930 };
6931 
6932 /// Print a template argument list, including the '<' and '>'
6933 /// enclosing the template arguments.
6934 void printTemplateArgumentList(raw_ostream &OS,
6935                                ArrayRef<TemplateArgument> Args,
6936                                const PrintingPolicy &Policy,
6937                                const TemplateParameterList *TPL = nullptr);
6938 
6939 void printTemplateArgumentList(raw_ostream &OS,
6940                                ArrayRef<TemplateArgumentLoc> Args,
6941                                const PrintingPolicy &Policy,
6942                                const TemplateParameterList *TPL = nullptr);
6943 
6944 void printTemplateArgumentList(raw_ostream &OS,
6945                                const TemplateArgumentListInfo &Args,
6946                                const PrintingPolicy &Policy,
6947                                const TemplateParameterList *TPL = nullptr);
6948 
6949 /// Make a best-effort determination of whether the type T can be produced by
6950 /// substituting Args into the default argument of Param.
6951 bool isSubstitutedDefaultArgument(ASTContext &Ctx, TemplateArgument Arg,
6952                                   const NamedDecl *Param,
6953                                   ArrayRef<TemplateArgument> Args,
6954                                   unsigned Depth);
6955 
6956 /// The injected class name of a C++ class template or class
6957 /// template partial specialization.  Used to record that a type was
6958 /// spelled with a bare identifier rather than as a template-id; the
6959 /// equivalent for non-templated classes is just RecordType.
6960 ///
6961 /// Injected class name types are always dependent.  Template
6962 /// instantiation turns these into RecordTypes.
6963 ///
6964 /// Injected class name types are always canonical.  This works
6965 /// because it is impossible to compare an injected class name type
6966 /// with the corresponding non-injected template type, for the same
6967 /// reason that it is impossible to directly compare template
6968 /// parameters from different dependent contexts: injected class name
6969 /// types can only occur within the scope of a particular templated
6970 /// declaration, and within that scope every template specialization
6971 /// will canonicalize to the injected class name (when appropriate
6972 /// according to the rules of the language).
6973 class InjectedClassNameType : public Type {
6974   friend class ASTContext; // ASTContext creates these.
6975   friend class ASTNodeImporter;
6976   friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not
6977                           // currently suitable for AST reading, too much
6978                           // interdependencies.
6979   template <class T> friend class serialization::AbstractTypeReader;
6980 
6981   CXXRecordDecl *Decl;
6982 
6983   /// The template specialization which this type represents.
6984   /// For example, in
6985   ///   template <class T> class A { ... };
6986   /// this is A<T>, whereas in
6987   ///   template <class X, class Y> class A<B<X,Y> > { ... };
6988   /// this is A<B<X,Y> >.
6989   ///
6990   /// It is always unqualified, always a template specialization type,
6991   /// and always dependent.
6992   QualType InjectedType;
6993 
6994   InjectedClassNameType(CXXRecordDecl *D, QualType TST)
6995       : Type(InjectedClassName, QualType(),
6996              TypeDependence::DependentInstantiation),
6997         Decl(D), InjectedType(TST) {
6998     assert(isa<TemplateSpecializationType>(TST));
6999     assert(!TST.hasQualifiers());
7000     assert(TST->isDependentType());
7001   }
7002 
7003 public:
7004   QualType getInjectedSpecializationType() const { return InjectedType; }
7005 
7006   const TemplateSpecializationType *getInjectedTST() const {
7007     return cast<TemplateSpecializationType>(InjectedType.getTypePtr());
7008   }
7009 
7010   TemplateName getTemplateName() const {
7011     return getInjectedTST()->getTemplateName();
7012   }
7013 
7014   CXXRecordDecl *getDecl() const;
7015 
7016   bool isSugared() const { return false; }
7017   QualType desugar() const { return QualType(this, 0); }
7018 
7019   static bool classof(const Type *T) {
7020     return T->getTypeClass() == InjectedClassName;
7021   }
7022 };
7023 
7024 /// The elaboration keyword that precedes a qualified type name or
7025 /// introduces an elaborated-type-specifier.
7026 enum class ElaboratedTypeKeyword {
7027   /// The "struct" keyword introduces the elaborated-type-specifier.
7028   Struct,
7029 
7030   /// The "__interface" keyword introduces the elaborated-type-specifier.
7031   Interface,
7032 
7033   /// The "union" keyword introduces the elaborated-type-specifier.
7034   Union,
7035 
7036   /// The "class" keyword introduces the elaborated-type-specifier.
7037   Class,
7038 
7039   /// The "enum" keyword introduces the elaborated-type-specifier.
7040   Enum,
7041 
7042   /// The "typename" keyword precedes the qualified type name, e.g.,
7043   /// \c typename T::type.
7044   Typename,
7045 
7046   /// No keyword precedes the qualified type name.
7047   None
7048 };
7049 
7050 /// The kind of a tag type.
7051 enum class TagTypeKind {
7052   /// The "struct" keyword.
7053   Struct,
7054 
7055   /// The "__interface" keyword.
7056   Interface,
7057 
7058   /// The "union" keyword.
7059   Union,
7060 
7061   /// The "class" keyword.
7062   Class,
7063 
7064   /// The "enum" keyword.
7065   Enum
7066 };
7067 
7068 /// A helper class for Type nodes having an ElaboratedTypeKeyword.
7069 /// The keyword in stored in the free bits of the base class.
7070 /// Also provides a few static helpers for converting and printing
7071 /// elaborated type keyword and tag type kind enumerations.
7072 class TypeWithKeyword : public Type {
7073 protected:
7074   TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc,
7075                   QualType Canonical, TypeDependence Dependence)
7076       : Type(tc, Canonical, Dependence) {
7077     TypeWithKeywordBits.Keyword = llvm::to_underlying(Keyword);
7078   }
7079 
7080 public:
7081   ElaboratedTypeKeyword getKeyword() const {
7082     return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword);
7083   }
7084 
7085   /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword.
7086   static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec);
7087 
7088   /// Converts a type specifier (DeclSpec::TST) into a tag type kind.
7089   /// It is an error to provide a type specifier which *isn't* a tag kind here.
7090   static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec);
7091 
7092   /// Converts a TagTypeKind into an elaborated type keyword.
7093   static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag);
7094 
7095   /// Converts an elaborated type keyword into a TagTypeKind.
7096   /// It is an error to provide an elaborated type keyword
7097   /// which *isn't* a tag kind here.
7098   static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword);
7099 
7100   static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword);
7101 
7102   static StringRef getKeywordName(ElaboratedTypeKeyword Keyword);
7103 
7104   static StringRef getTagTypeKindName(TagTypeKind Kind) {
7105     return getKeywordName(getKeywordForTagTypeKind(Kind));
7106   }
7107 
7108   class CannotCastToThisType {};
7109   static CannotCastToThisType classof(const Type *);
7110 };
7111 
7112 /// Represents a type that was referred to using an elaborated type
7113 /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type,
7114 /// or both.
7115 ///
7116 /// This type is used to keep track of a type name as written in the
7117 /// source code, including tag keywords and any nested-name-specifiers.
7118 /// The type itself is always "sugar", used to express what was written
7119 /// in the source code but containing no additional semantic information.
7120 class ElaboratedType final
7121     : public TypeWithKeyword,
7122       public llvm::FoldingSetNode,
7123       private llvm::TrailingObjects<ElaboratedType, TagDecl *> {
7124   friend class ASTContext; // ASTContext creates these
7125   friend TrailingObjects;
7126 
7127   /// The nested name specifier containing the qualifier.
7128   NestedNameSpecifier *NNS;
7129 
7130   /// The type that this qualified name refers to.
7131   QualType NamedType;
7132 
7133   /// The (re)declaration of this tag type owned by this occurrence is stored
7134   /// as a trailing object if there is one. Use getOwnedTagDecl to obtain
7135   /// it, or obtain a null pointer if there is none.
7136 
7137   ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
7138                  QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl)
7139       : TypeWithKeyword(Keyword, Elaborated, CanonType,
7140                         // Any semantic dependence on the qualifier will have
7141                         // been incorporated into NamedType. We still need to
7142                         // track syntactic (instantiation / error / pack)
7143                         // dependence on the qualifier.
7144                         NamedType->getDependence() |
7145                             (NNS ? toSyntacticDependence(
7146                                        toTypeDependence(NNS->getDependence()))
7147                                  : TypeDependence::None)),
7148         NNS(NNS), NamedType(NamedType) {
7149     ElaboratedTypeBits.HasOwnedTagDecl = false;
7150     if (OwnedTagDecl) {
7151       ElaboratedTypeBits.HasOwnedTagDecl = true;
7152       *getTrailingObjects() = OwnedTagDecl;
7153     }
7154   }
7155 
7156 public:
7157   /// Retrieve the qualification on this type.
7158   NestedNameSpecifier *getQualifier() const { return NNS; }
7159 
7160   /// Retrieve the type named by the qualified-id.
7161   QualType getNamedType() const { return NamedType; }
7162 
7163   /// Remove a single level of sugar.
7164   QualType desugar() const { return getNamedType(); }
7165 
7166   /// Returns whether this type directly provides sugar.
7167   bool isSugared() const { return true; }
7168 
7169   /// Return the (re)declaration of this type owned by this occurrence of this
7170   /// type, or nullptr if there is none.
7171   TagDecl *getOwnedTagDecl() const {
7172     return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects() : nullptr;
7173   }
7174 
7175   void Profile(llvm::FoldingSetNodeID &ID) {
7176     Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl());
7177   }
7178 
7179   static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
7180                       NestedNameSpecifier *NNS, QualType NamedType,
7181                       TagDecl *OwnedTagDecl) {
7182     ID.AddInteger(llvm::to_underlying(Keyword));
7183     ID.AddPointer(NNS);
7184     NamedType.Profile(ID);
7185     ID.AddPointer(OwnedTagDecl);
7186   }
7187 
7188   static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; }
7189 };
7190 
7191 /// Represents a qualified type name for which the type name is
7192 /// dependent.
7193 ///
7194 /// DependentNameType represents a class of dependent types that involve a
7195 /// possibly dependent nested-name-specifier (e.g., "T::") followed by a
7196 /// name of a type. The DependentNameType may start with a "typename" (for a
7197 /// typename-specifier), "class", "struct", "union", or "enum" (for a
7198 /// dependent elaborated-type-specifier), or nothing (in contexts where we
7199 /// know that we must be referring to a type, e.g., in a base class specifier).
7200 /// Typically the nested-name-specifier is dependent, but in MSVC compatibility
7201 /// mode, this type is used with non-dependent names to delay name lookup until
7202 /// instantiation.
7203 class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode {
7204   friend class ASTContext; // ASTContext creates these
7205 
7206   /// The nested name specifier containing the qualifier.
7207   NestedNameSpecifier *NNS;
7208 
7209   /// The type that this typename specifier refers to.
7210   const IdentifierInfo *Name;
7211 
7212   DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
7213                     const IdentifierInfo *Name, QualType CanonType)
7214       : TypeWithKeyword(Keyword, DependentName, CanonType,
7215                         TypeDependence::DependentInstantiation |
7216                             toTypeDependence(NNS->getDependence())),
7217         NNS(NNS), Name(Name) {
7218     assert(NNS);
7219     assert(Name);
7220   }
7221 
7222 public:
7223   /// Retrieve the qualification on this type.
7224   NestedNameSpecifier *getQualifier() const { return NNS; }
7225 
7226   /// Retrieve the identifier that terminates this type name.
7227   /// For example, "type" in "typename T::type".
7228   const IdentifierInfo *getIdentifier() const {
7229     return Name;
7230   }
7231 
7232   bool isSugared() const { return false; }
7233   QualType desugar() const { return QualType(this, 0); }
7234 
7235   void Profile(llvm::FoldingSetNodeID &ID) {
7236     Profile(ID, getKeyword(), NNS, Name);
7237   }
7238 
7239   static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
7240                       NestedNameSpecifier *NNS, const IdentifierInfo *Name) {
7241     ID.AddInteger(llvm::to_underlying(Keyword));
7242     ID.AddPointer(NNS);
7243     ID.AddPointer(Name);
7244   }
7245 
7246   static bool classof(const Type *T) {
7247     return T->getTypeClass() == DependentName;
7248   }
7249 };
7250 
7251 /// Represents a template specialization type whose template cannot be
7252 /// resolved, e.g.
7253 ///   A<T>::template B<T>
7254 class DependentTemplateSpecializationType : public TypeWithKeyword,
7255                                             public llvm::FoldingSetNode {
7256   friend class ASTContext; // ASTContext creates these
7257 
7258   DependentTemplateStorage Name;
7259 
7260   DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
7261                                       const DependentTemplateStorage &Name,
7262                                       ArrayRef<TemplateArgument> Args,
7263                                       QualType Canon);
7264 
7265 public:
7266   const DependentTemplateStorage &getDependentTemplateName() const {
7267     return Name;
7268   }
7269 
7270   ArrayRef<TemplateArgument> template_arguments() const {
7271     return {reinterpret_cast<const TemplateArgument *>(this + 1),
7272             DependentTemplateSpecializationTypeBits.NumArgs};
7273   }
7274 
7275   bool isSugared() const { return false; }
7276   QualType desugar() const { return QualType(this, 0); }
7277 
7278   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
7279     Profile(ID, Context, getKeyword(), Name, template_arguments());
7280   }
7281 
7282   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
7283                       ElaboratedTypeKeyword Keyword,
7284                       const DependentTemplateStorage &Name,
7285                       ArrayRef<TemplateArgument> Args);
7286 
7287   static bool classof(const Type *T) {
7288     return T->getTypeClass() == DependentTemplateSpecialization;
7289   }
7290 };
7291 
7292 /// Represents a pack expansion of types.
7293 ///
7294 /// Pack expansions are part of C++11 variadic templates. A pack
7295 /// expansion contains a pattern, which itself contains one or more
7296 /// "unexpanded" parameter packs. When instantiated, a pack expansion
7297 /// produces a series of types, each instantiated from the pattern of
7298 /// the expansion, where the Ith instantiation of the pattern uses the
7299 /// Ith arguments bound to each of the unexpanded parameter packs. The
7300 /// pack expansion is considered to "expand" these unexpanded
7301 /// parameter packs.
7302 ///
7303 /// \code
7304 /// template<typename ...Types> struct tuple;
7305 ///
7306 /// template<typename ...Types>
7307 /// struct tuple_of_references {
7308 ///   typedef tuple<Types&...> type;
7309 /// };
7310 /// \endcode
7311 ///
7312 /// Here, the pack expansion \c Types&... is represented via a
7313 /// PackExpansionType whose pattern is Types&.
7314 class PackExpansionType : public Type, public llvm::FoldingSetNode {
7315   friend class ASTContext; // ASTContext creates these
7316 
7317   /// The pattern of the pack expansion.
7318   QualType Pattern;
7319 
7320   PackExpansionType(QualType Pattern, QualType Canon,
7321                     UnsignedOrNone NumExpansions)
7322       : Type(PackExpansion, Canon,
7323              (Pattern->getDependence() | TypeDependence::Dependent |
7324               TypeDependence::Instantiation) &
7325                  ~TypeDependence::UnexpandedPack),
7326         Pattern(Pattern) {
7327     PackExpansionTypeBits.NumExpansions =
7328         NumExpansions ? *NumExpansions + 1 : 0;
7329   }
7330 
7331 public:
7332   /// Retrieve the pattern of this pack expansion, which is the
7333   /// type that will be repeatedly instantiated when instantiating the
7334   /// pack expansion itself.
7335   QualType getPattern() const { return Pattern; }
7336 
7337   /// Retrieve the number of expansions that this pack expansion will
7338   /// generate, if known.
7339   UnsignedOrNone getNumExpansions() const {
7340     if (PackExpansionTypeBits.NumExpansions)
7341       return PackExpansionTypeBits.NumExpansions - 1;
7342     return std::nullopt;
7343   }
7344 
7345   bool isSugared() const { return false; }
7346   QualType desugar() const { return QualType(this, 0); }
7347 
7348   void Profile(llvm::FoldingSetNodeID &ID) {
7349     Profile(ID, getPattern(), getNumExpansions());
7350   }
7351 
7352   static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern,
7353                       UnsignedOrNone NumExpansions) {
7354     ID.AddPointer(Pattern.getAsOpaquePtr());
7355     ID.AddInteger(NumExpansions.toInternalRepresentation());
7356   }
7357 
7358   static bool classof(const Type *T) {
7359     return T->getTypeClass() == PackExpansion;
7360   }
7361 };
7362 
7363 /// This class wraps the list of protocol qualifiers. For types that can
7364 /// take ObjC protocol qualifers, they can subclass this class.
7365 template <class T>
7366 class ObjCProtocolQualifiers {
7367 protected:
7368   ObjCProtocolQualifiers() = default;
7369 
7370   ObjCProtocolDecl * const *getProtocolStorage() const {
7371     return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage();
7372   }
7373 
7374   ObjCProtocolDecl **getProtocolStorage() {
7375     return static_cast<T*>(this)->getProtocolStorageImpl();
7376   }
7377 
7378   void setNumProtocols(unsigned N) {
7379     static_cast<T*>(this)->setNumProtocolsImpl(N);
7380   }
7381 
7382   void initialize(ArrayRef<ObjCProtocolDecl *> protocols) {
7383     setNumProtocols(protocols.size());
7384     assert(getNumProtocols() == protocols.size() &&
7385            "bitfield overflow in protocol count");
7386     if (!protocols.empty())
7387       memcpy(getProtocolStorage(), protocols.data(),
7388              protocols.size() * sizeof(ObjCProtocolDecl*));
7389   }
7390 
7391 public:
7392   using qual_iterator = ObjCProtocolDecl * const *;
7393   using qual_range = llvm::iterator_range<qual_iterator>;
7394 
7395   qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
7396   qual_iterator qual_begin() const { return getProtocolStorage(); }
7397   qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); }
7398 
7399   bool qual_empty() const { return getNumProtocols() == 0; }
7400 
7401   /// Return the number of qualifying protocols in this type, or 0 if
7402   /// there are none.
7403   unsigned getNumProtocols() const {
7404     return static_cast<const T*>(this)->getNumProtocolsImpl();
7405   }
7406 
7407   /// Fetch a protocol by index.
7408   ObjCProtocolDecl *getProtocol(unsigned I) const {
7409     assert(I < getNumProtocols() && "Out-of-range protocol access");
7410     return qual_begin()[I];
7411   }
7412 
7413   /// Retrieve all of the protocol qualifiers.
7414   ArrayRef<ObjCProtocolDecl *> getProtocols() const {
7415     return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols());
7416   }
7417 };
7418 
7419 /// Represents a type parameter type in Objective C. It can take
7420 /// a list of protocols.
7421 class ObjCTypeParamType : public Type,
7422                           public ObjCProtocolQualifiers<ObjCTypeParamType>,
7423                           public llvm::FoldingSetNode {
7424   friend class ASTContext;
7425   friend class ObjCProtocolQualifiers<ObjCTypeParamType>;
7426 
7427   /// The number of protocols stored on this type.
7428   unsigned NumProtocols : 6;
7429 
7430   ObjCTypeParamDecl *OTPDecl;
7431 
7432   /// The protocols are stored after the ObjCTypeParamType node. In the
7433   /// canonical type, the list of protocols are sorted alphabetically
7434   /// and uniqued.
7435   ObjCProtocolDecl **getProtocolStorageImpl();
7436 
7437   /// Return the number of qualifying protocols in this interface type,
7438   /// or 0 if there are none.
7439   unsigned getNumProtocolsImpl() const {
7440     return NumProtocols;
7441   }
7442 
7443   void setNumProtocolsImpl(unsigned N) {
7444     NumProtocols = N;
7445   }
7446 
7447   ObjCTypeParamType(const ObjCTypeParamDecl *D,
7448                     QualType can,
7449                     ArrayRef<ObjCProtocolDecl *> protocols);
7450 
7451 public:
7452   bool isSugared() const { return true; }
7453   QualType desugar() const { return getCanonicalTypeInternal(); }
7454 
7455   static bool classof(const Type *T) {
7456     return T->getTypeClass() == ObjCTypeParam;
7457   }
7458 
7459   void Profile(llvm::FoldingSetNodeID &ID);
7460   static void Profile(llvm::FoldingSetNodeID &ID,
7461                       const ObjCTypeParamDecl *OTPDecl,
7462                       QualType CanonicalType,
7463                       ArrayRef<ObjCProtocolDecl *> protocols);
7464 
7465   ObjCTypeParamDecl *getDecl() const { return OTPDecl; }
7466 };
7467 
7468 /// Represents a class type in Objective C.
7469 ///
7470 /// Every Objective C type is a combination of a base type, a set of
7471 /// type arguments (optional, for parameterized classes) and a list of
7472 /// protocols.
7473 ///
7474 /// Given the following declarations:
7475 /// \code
7476 ///   \@class C<T>;
7477 ///   \@protocol P;
7478 /// \endcode
7479 ///
7480 /// 'C' is an ObjCInterfaceType C.  It is sugar for an ObjCObjectType
7481 /// with base C and no protocols.
7482 ///
7483 /// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P].
7484 /// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no
7485 /// protocol list.
7486 /// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*',
7487 /// and protocol list [P].
7488 ///
7489 /// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose
7490 /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType
7491 /// and no protocols.
7492 ///
7493 /// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType
7494 /// with base BuiltinType::ObjCIdType and protocol list [P].  Eventually
7495 /// this should get its own sugar class to better represent the source.
7496 class ObjCObjectType : public Type,
7497                        public ObjCProtocolQualifiers<ObjCObjectType> {
7498   friend class ObjCProtocolQualifiers<ObjCObjectType>;
7499 
7500   // ObjCObjectType.NumTypeArgs - the number of type arguments stored
7501   // after the ObjCObjectPointerType node.
7502   // ObjCObjectType.NumProtocols - the number of protocols stored
7503   // after the type arguments of ObjCObjectPointerType node.
7504   //
7505   // These protocols are those written directly on the type.  If
7506   // protocol qualifiers ever become additive, the iterators will need
7507   // to get kindof complicated.
7508   //
7509   // In the canonical object type, these are sorted alphabetically
7510   // and uniqued.
7511 
7512   /// Either a BuiltinType or an InterfaceType or sugar for either.
7513   QualType BaseType;
7514 
7515   /// Cached superclass type.
7516   mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool>
7517     CachedSuperClassType;
7518 
7519   QualType *getTypeArgStorage();
7520   const QualType *getTypeArgStorage() const {
7521     return const_cast<ObjCObjectType *>(this)->getTypeArgStorage();
7522   }
7523 
7524   ObjCProtocolDecl **getProtocolStorageImpl();
7525   /// Return the number of qualifying protocols in this interface type,
7526   /// or 0 if there are none.
7527   unsigned getNumProtocolsImpl() const {
7528     return ObjCObjectTypeBits.NumProtocols;
7529   }
7530   void setNumProtocolsImpl(unsigned N) {
7531     ObjCObjectTypeBits.NumProtocols = N;
7532   }
7533 
7534 protected:
7535   enum Nonce_ObjCInterface { Nonce_ObjCInterface };
7536 
7537   ObjCObjectType(QualType Canonical, QualType Base,
7538                  ArrayRef<QualType> typeArgs,
7539                  ArrayRef<ObjCProtocolDecl *> protocols,
7540                  bool isKindOf);
7541 
7542   ObjCObjectType(enum Nonce_ObjCInterface)
7543       : Type(ObjCInterface, QualType(), TypeDependence::None),
7544         BaseType(QualType(this_(), 0)) {
7545     ObjCObjectTypeBits.NumProtocols = 0;
7546     ObjCObjectTypeBits.NumTypeArgs = 0;
7547     ObjCObjectTypeBits.IsKindOf = 0;
7548   }
7549 
7550   void computeSuperClassTypeSlow() const;
7551 
7552 public:
7553   /// Gets the base type of this object type.  This is always (possibly
7554   /// sugar for) one of:
7555   ///  - the 'id' builtin type (as opposed to the 'id' type visible to the
7556   ///    user, which is a typedef for an ObjCObjectPointerType)
7557   ///  - the 'Class' builtin type (same caveat)
7558   ///  - an ObjCObjectType (currently always an ObjCInterfaceType)
7559   QualType getBaseType() const { return BaseType; }
7560 
7561   bool isObjCId() const {
7562     return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId);
7563   }
7564 
7565   bool isObjCClass() const {
7566     return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass);
7567   }
7568 
7569   bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); }
7570   bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); }
7571   bool isObjCUnqualifiedIdOrClass() const {
7572     if (!qual_empty()) return false;
7573     if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>())
7574       return T->getKind() == BuiltinType::ObjCId ||
7575              T->getKind() == BuiltinType::ObjCClass;
7576     return false;
7577   }
7578   bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); }
7579   bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); }
7580 
7581   /// Gets the interface declaration for this object type, if the base type
7582   /// really is an interface.
7583   ObjCInterfaceDecl *getInterface() const;
7584 
7585   /// Determine whether this object type is "specialized", meaning
7586   /// that it has type arguments.
7587   bool isSpecialized() const;
7588 
7589   /// Determine whether this object type was written with type arguments.
7590   bool isSpecializedAsWritten() const {
7591     return ObjCObjectTypeBits.NumTypeArgs > 0;
7592   }
7593 
7594   /// Determine whether this object type is "unspecialized", meaning
7595   /// that it has no type arguments.
7596   bool isUnspecialized() const { return !isSpecialized(); }
7597 
7598   /// Determine whether this object type is "unspecialized" as
7599   /// written, meaning that it has no type arguments.
7600   bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
7601 
7602   /// Retrieve the type arguments of this object type (semantically).
7603   ArrayRef<QualType> getTypeArgs() const;
7604 
7605   /// Retrieve the type arguments of this object type as they were
7606   /// written.
7607   ArrayRef<QualType> getTypeArgsAsWritten() const {
7608     return {getTypeArgStorage(), ObjCObjectTypeBits.NumTypeArgs};
7609   }
7610 
7611   /// Whether this is a "__kindof" type as written.
7612   bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; }
7613 
7614   /// Whether this ia a "__kindof" type (semantically).
7615   bool isKindOfType() const;
7616 
7617   /// Retrieve the type of the superclass of this object type.
7618   ///
7619   /// This operation substitutes any type arguments into the
7620   /// superclass of the current class type, potentially producing a
7621   /// specialization of the superclass type. Produces a null type if
7622   /// there is no superclass.
7623   QualType getSuperClassType() const {
7624     if (!CachedSuperClassType.getInt())
7625       computeSuperClassTypeSlow();
7626 
7627     assert(CachedSuperClassType.getInt() && "Superclass not set?");
7628     return QualType(CachedSuperClassType.getPointer(), 0);
7629   }
7630 
7631   /// Strip off the Objective-C "kindof" type and (with it) any
7632   /// protocol qualifiers.
7633   QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const;
7634 
7635   bool isSugared() const { return false; }
7636   QualType desugar() const { return QualType(this, 0); }
7637 
7638   static bool classof(const Type *T) {
7639     return T->getTypeClass() == ObjCObject ||
7640            T->getTypeClass() == ObjCInterface;
7641   }
7642 };
7643 
7644 /// A class providing a concrete implementation
7645 /// of ObjCObjectType, so as to not increase the footprint of
7646 /// ObjCInterfaceType.  Code outside of ASTContext and the core type
7647 /// system should not reference this type.
7648 class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode {
7649   friend class ASTContext;
7650 
7651   // If anyone adds fields here, ObjCObjectType::getProtocolStorage()
7652   // will need to be modified.
7653 
7654   ObjCObjectTypeImpl(QualType Canonical, QualType Base,
7655                      ArrayRef<QualType> typeArgs,
7656                      ArrayRef<ObjCProtocolDecl *> protocols,
7657                      bool isKindOf)
7658       : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {}
7659 
7660 public:
7661   void Profile(llvm::FoldingSetNodeID &ID);
7662   static void Profile(llvm::FoldingSetNodeID &ID,
7663                       QualType Base,
7664                       ArrayRef<QualType> typeArgs,
7665                       ArrayRef<ObjCProtocolDecl *> protocols,
7666                       bool isKindOf);
7667 };
7668 
7669 inline QualType *ObjCObjectType::getTypeArgStorage() {
7670   return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1);
7671 }
7672 
7673 inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() {
7674     return reinterpret_cast<ObjCProtocolDecl**>(
7675              getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs);
7676 }
7677 
7678 inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() {
7679     return reinterpret_cast<ObjCProtocolDecl**>(
7680              static_cast<ObjCTypeParamType*>(this)+1);
7681 }
7682 
7683 /// Interfaces are the core concept in Objective-C for object oriented design.
7684 /// They basically correspond to C++ classes.  There are two kinds of interface
7685 /// types: normal interfaces like `NSString`, and qualified interfaces, which
7686 /// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`.
7687 ///
7688 /// ObjCInterfaceType guarantees the following properties when considered
7689 /// as a subtype of its superclass, ObjCObjectType:
7690 ///   - There are no protocol qualifiers.  To reinforce this, code which
7691 ///     tries to invoke the protocol methods via an ObjCInterfaceType will
7692 ///     fail to compile.
7693 ///   - It is its own base type.  That is, if T is an ObjCInterfaceType*,
7694 ///     T->getBaseType() == QualType(T, 0).
7695 class ObjCInterfaceType : public ObjCObjectType {
7696   friend class ASTContext; // ASTContext creates these.
7697   friend class ASTReader;
7698   template <class T> friend class serialization::AbstractTypeReader;
7699 
7700   ObjCInterfaceDecl *Decl;
7701 
7702   ObjCInterfaceType(const ObjCInterfaceDecl *D)
7703       : ObjCObjectType(Nonce_ObjCInterface),
7704         Decl(const_cast<ObjCInterfaceDecl*>(D)) {}
7705 
7706 public:
7707   /// Get the declaration of this interface.
7708   ObjCInterfaceDecl *getDecl() const;
7709 
7710   bool isSugared() const { return false; }
7711   QualType desugar() const { return QualType(this, 0); }
7712 
7713   static bool classof(const Type *T) {
7714     return T->getTypeClass() == ObjCInterface;
7715   }
7716 
7717   // Nonsense to "hide" certain members of ObjCObjectType within this
7718   // class.  People asking for protocols on an ObjCInterfaceType are
7719   // not going to get what they want: ObjCInterfaceTypes are
7720   // guaranteed to have no protocols.
7721   enum {
7722     qual_iterator,
7723     qual_begin,
7724     qual_end,
7725     getNumProtocols,
7726     getProtocol
7727   };
7728 };
7729 
7730 inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const {
7731   QualType baseType = getBaseType();
7732   while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) {
7733     if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT))
7734       return T->getDecl();
7735 
7736     baseType = ObjT->getBaseType();
7737   }
7738 
7739   return nullptr;
7740 }
7741 
7742 /// Represents a pointer to an Objective C object.
7743 ///
7744 /// These are constructed from pointer declarators when the pointee type is
7745 /// an ObjCObjectType (or sugar for one).  In addition, the 'id' and 'Class'
7746 /// types are typedefs for these, and the protocol-qualified types 'id<P>'
7747 /// and 'Class<P>' are translated into these.
7748 ///
7749 /// Pointers to pointers to Objective C objects are still PointerTypes;
7750 /// only the first level of pointer gets it own type implementation.
7751 class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
7752   friend class ASTContext; // ASTContext creates these.
7753 
7754   QualType PointeeType;
7755 
7756   ObjCObjectPointerType(QualType Canonical, QualType Pointee)
7757       : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()),
7758         PointeeType(Pointee) {}
7759 
7760 public:
7761   /// Gets the type pointed to by this ObjC pointer.
7762   /// The result will always be an ObjCObjectType or sugar thereof.
7763   QualType getPointeeType() const { return PointeeType; }
7764 
7765   /// Gets the type pointed to by this ObjC pointer.  Always returns non-null.
7766   ///
7767   /// This method is equivalent to getPointeeType() except that
7768   /// it discards any typedefs (or other sugar) between this
7769   /// type and the "outermost" object type.  So for:
7770   /// \code
7771   ///   \@class A; \@protocol P; \@protocol Q;
7772   ///   typedef A<P> AP;
7773   ///   typedef A A1;
7774   ///   typedef A1<P> A1P;
7775   ///   typedef A1P<Q> A1PQ;
7776   /// \endcode
7777   /// For 'A*', getObjectType() will return 'A'.
7778   /// For 'A<P>*', getObjectType() will return 'A<P>'.
7779   /// For 'AP*', getObjectType() will return 'A<P>'.
7780   /// For 'A1*', getObjectType() will return 'A'.
7781   /// For 'A1<P>*', getObjectType() will return 'A1<P>'.
7782   /// For 'A1P*', getObjectType() will return 'A1<P>'.
7783   /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because
7784   ///   adding protocols to a protocol-qualified base discards the
7785   ///   old qualifiers (for now).  But if it didn't, getObjectType()
7786   ///   would return 'A1P<Q>' (and we'd have to make iterating over
7787   ///   qualifiers more complicated).
7788   const ObjCObjectType *getObjectType() const {
7789     return PointeeType->castAs<ObjCObjectType>();
7790   }
7791 
7792   /// If this pointer points to an Objective C
7793   /// \@interface type, gets the type for that interface.  Any protocol
7794   /// qualifiers on the interface are ignored.
7795   ///
7796   /// \return null if the base type for this pointer is 'id' or 'Class'
7797   const ObjCInterfaceType *getInterfaceType() const;
7798 
7799   /// If this pointer points to an Objective \@interface
7800   /// type, gets the declaration for that interface.
7801   ///
7802   /// \return null if the base type for this pointer is 'id' or 'Class'
7803   ObjCInterfaceDecl *getInterfaceDecl() const {
7804     return getObjectType()->getInterface();
7805   }
7806 
7807   /// True if this is equivalent to the 'id' type, i.e. if
7808   /// its object type is the primitive 'id' type with no protocols.
7809   bool isObjCIdType() const {
7810     return getObjectType()->isObjCUnqualifiedId();
7811   }
7812 
7813   /// True if this is equivalent to the 'Class' type,
7814   /// i.e. if its object tive is the primitive 'Class' type with no protocols.
7815   bool isObjCClassType() const {
7816     return getObjectType()->isObjCUnqualifiedClass();
7817   }
7818 
7819   /// True if this is equivalent to the 'id' or 'Class' type,
7820   bool isObjCIdOrClassType() const {
7821     return getObjectType()->isObjCUnqualifiedIdOrClass();
7822   }
7823 
7824   /// True if this is equivalent to 'id<P>' for some non-empty set of
7825   /// protocols.
7826   bool isObjCQualifiedIdType() const {
7827     return getObjectType()->isObjCQualifiedId();
7828   }
7829 
7830   /// True if this is equivalent to 'Class<P>' for some non-empty set of
7831   /// protocols.
7832   bool isObjCQualifiedClassType() const {
7833     return getObjectType()->isObjCQualifiedClass();
7834   }
7835 
7836   /// Whether this is a "__kindof" type.
7837   bool isKindOfType() const { return getObjectType()->isKindOfType(); }
7838 
7839   /// Whether this type is specialized, meaning that it has type arguments.
7840   bool isSpecialized() const { return getObjectType()->isSpecialized(); }
7841 
7842   /// Whether this type is specialized, meaning that it has type arguments.
7843   bool isSpecializedAsWritten() const {
7844     return getObjectType()->isSpecializedAsWritten();
7845   }
7846 
7847   /// Whether this type is unspecialized, meaning that is has no type arguments.
7848   bool isUnspecialized() const { return getObjectType()->isUnspecialized(); }
7849 
7850   /// Determine whether this object type is "unspecialized" as
7851   /// written, meaning that it has no type arguments.
7852   bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
7853 
7854   /// Retrieve the type arguments for this type.
7855   ArrayRef<QualType> getTypeArgs() const {
7856     return getObjectType()->getTypeArgs();
7857   }
7858 
7859   /// Retrieve the type arguments for this type.
7860   ArrayRef<QualType> getTypeArgsAsWritten() const {
7861     return getObjectType()->getTypeArgsAsWritten();
7862   }
7863 
7864   /// An iterator over the qualifiers on the object type.  Provided
7865   /// for convenience.  This will always iterate over the full set of
7866   /// protocols on a type, not just those provided directly.
7867   using qual_iterator = ObjCObjectType::qual_iterator;
7868   using qual_range = llvm::iterator_range<qual_iterator>;
7869 
7870   qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
7871 
7872   qual_iterator qual_begin() const {
7873     return getObjectType()->qual_begin();
7874   }
7875 
7876   qual_iterator qual_end() const {
7877     return getObjectType()->qual_end();
7878   }
7879 
7880   bool qual_empty() const { return getObjectType()->qual_empty(); }
7881 
7882   /// Return the number of qualifying protocols on the object type.
7883   unsigned getNumProtocols() const {
7884     return getObjectType()->getNumProtocols();
7885   }
7886 
7887   /// Retrieve a qualifying protocol by index on the object type.
7888   ObjCProtocolDecl *getProtocol(unsigned I) const {
7889     return getObjectType()->getProtocol(I);
7890   }
7891 
7892   bool isSugared() const { return false; }
7893   QualType desugar() const { return QualType(this, 0); }
7894 
7895   /// Retrieve the type of the superclass of this object pointer type.
7896   ///
7897   /// This operation substitutes any type arguments into the
7898   /// superclass of the current class type, potentially producing a
7899   /// pointer to a specialization of the superclass type. Produces a
7900   /// null type if there is no superclass.
7901   QualType getSuperClassType() const;
7902 
7903   /// Strip off the Objective-C "kindof" type and (with it) any
7904   /// protocol qualifiers.
7905   const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals(
7906                                  const ASTContext &ctx) const;
7907 
7908   void Profile(llvm::FoldingSetNodeID &ID) {
7909     Profile(ID, getPointeeType());
7910   }
7911 
7912   static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
7913     ID.AddPointer(T.getAsOpaquePtr());
7914   }
7915 
7916   static bool classof(const Type *T) {
7917     return T->getTypeClass() == ObjCObjectPointer;
7918   }
7919 };
7920 
7921 class AtomicType : public Type, public llvm::FoldingSetNode {
7922   friend class ASTContext; // ASTContext creates these.
7923 
7924   QualType ValueType;
7925 
7926   AtomicType(QualType ValTy, QualType Canonical)
7927       : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {}
7928 
7929 public:
7930   /// Gets the type contained by this atomic type, i.e.
7931   /// the type returned by performing an atomic load of this atomic type.
7932   QualType getValueType() const { return ValueType; }
7933 
7934   bool isSugared() const { return false; }
7935   QualType desugar() const { return QualType(this, 0); }
7936 
7937   void Profile(llvm::FoldingSetNodeID &ID) {
7938     Profile(ID, getValueType());
7939   }
7940 
7941   static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
7942     ID.AddPointer(T.getAsOpaquePtr());
7943   }
7944 
7945   static bool classof(const Type *T) {
7946     return T->getTypeClass() == Atomic;
7947   }
7948 };
7949 
7950 /// PipeType - OpenCL20.
7951 class PipeType : public Type, public llvm::FoldingSetNode {
7952   friend class ASTContext; // ASTContext creates these.
7953 
7954   QualType ElementType;
7955   bool isRead;
7956 
7957   PipeType(QualType elemType, QualType CanonicalPtr, bool isRead)
7958       : Type(Pipe, CanonicalPtr, elemType->getDependence()),
7959         ElementType(elemType), isRead(isRead) {}
7960 
7961 public:
7962   QualType getElementType() const { return ElementType; }
7963 
7964   bool isSugared() const { return false; }
7965 
7966   QualType desugar() const { return QualType(this, 0); }
7967 
7968   void Profile(llvm::FoldingSetNodeID &ID) {
7969     Profile(ID, getElementType(), isReadOnly());
7970   }
7971 
7972   static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) {
7973     ID.AddPointer(T.getAsOpaquePtr());
7974     ID.AddBoolean(isRead);
7975   }
7976 
7977   static bool classof(const Type *T) {
7978     return T->getTypeClass() == Pipe;
7979   }
7980 
7981   bool isReadOnly() const { return isRead; }
7982 };
7983 
7984 /// A fixed int type of a specified bitwidth.
7985 class BitIntType final : public Type, public llvm::FoldingSetNode {
7986   friend class ASTContext;
7987   LLVM_PREFERRED_TYPE(bool)
7988   unsigned IsUnsigned : 1;
7989   unsigned NumBits : 24;
7990 
7991 protected:
7992   BitIntType(bool isUnsigned, unsigned NumBits);
7993 
7994 public:
7995   bool isUnsigned() const { return IsUnsigned; }
7996   bool isSigned() const { return !IsUnsigned; }
7997   unsigned getNumBits() const { return NumBits; }
7998 
7999   bool isSugared() const { return false; }
8000   QualType desugar() const { return QualType(this, 0); }
8001 
8002   void Profile(llvm::FoldingSetNodeID &ID) const {
8003     Profile(ID, isUnsigned(), getNumBits());
8004   }
8005 
8006   static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned,
8007                       unsigned NumBits) {
8008     ID.AddBoolean(IsUnsigned);
8009     ID.AddInteger(NumBits);
8010   }
8011 
8012   static bool classof(const Type *T) { return T->getTypeClass() == BitInt; }
8013 };
8014 
8015 class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
8016   friend class ASTContext;
8017   llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned;
8018 
8019 protected:
8020   DependentBitIntType(bool IsUnsigned, Expr *NumBits);
8021 
8022 public:
8023   bool isUnsigned() const;
8024   bool isSigned() const { return !isUnsigned(); }
8025   Expr *getNumBitsExpr() const;
8026 
8027   bool isSugared() const { return false; }
8028   QualType desugar() const { return QualType(this, 0); }
8029 
8030   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
8031     Profile(ID, Context, isUnsigned(), getNumBitsExpr());
8032   }
8033   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
8034                       bool IsUnsigned, Expr *NumBitsExpr);
8035 
8036   static bool classof(const Type *T) {
8037     return T->getTypeClass() == DependentBitInt;
8038   }
8039 };
8040 
8041 /// A qualifier set is used to build a set of qualifiers.
8042 class QualifierCollector : public Qualifiers {
8043 public:
8044   QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {}
8045 
8046   /// Collect any qualifiers on the given type and return an
8047   /// unqualified type.  The qualifiers are assumed to be consistent
8048   /// with those already in the type.
8049   const Type *strip(QualType type) {
8050     addFastQualifiers(type.getLocalFastQualifiers());
8051     if (!type.hasLocalNonFastQualifiers())
8052       return type.getTypePtrUnsafe();
8053 
8054     const ExtQuals *extQuals = type.getExtQualsUnsafe();
8055     addConsistentQualifiers(extQuals->getQualifiers());
8056     return extQuals->getBaseType();
8057   }
8058 
8059   /// Apply the collected qualifiers to the given type.
8060   QualType apply(const ASTContext &Context, QualType QT) const;
8061 
8062   /// Apply the collected qualifiers to the given type.
8063   QualType apply(const ASTContext &Context, const Type* T) const;
8064 };
8065 
8066 /// A container of type source information.
8067 ///
8068 /// A client can read the relevant info using TypeLoc wrappers, e.g:
8069 /// @code
8070 /// TypeLoc TL = TypeSourceInfo->getTypeLoc();
8071 /// TL.getBeginLoc().print(OS, SrcMgr);
8072 /// @endcode
8073 class alignas(8) TypeSourceInfo {
8074   // Contains a memory block after the class, used for type source information,
8075   // allocated by ASTContext.
8076   friend class ASTContext;
8077 
8078   QualType Ty;
8079 
8080   TypeSourceInfo(QualType ty, size_t DataSize); // implemented in TypeLoc.h
8081 
8082 public:
8083   /// Return the type wrapped by this type source info.
8084   QualType getType() const { return Ty; }
8085 
8086   /// Return the TypeLoc wrapper for the type source info.
8087   TypeLoc getTypeLoc() const; // implemented in TypeLoc.h
8088 
8089   /// Override the type stored in this TypeSourceInfo. Use with caution!
8090   void overrideType(QualType T) { Ty = T; }
8091 };
8092 
8093 // Inline function definitions.
8094 
8095 inline SplitQualType SplitQualType::getSingleStepDesugaredType() const {
8096   SplitQualType desugar =
8097     Ty->getLocallyUnqualifiedSingleStepDesugaredType().split();
8098   desugar.Quals.addConsistentQualifiers(Quals);
8099   return desugar;
8100 }
8101 
8102 inline const Type *QualType::getTypePtr() const {
8103   return getCommonPtr()->BaseType;
8104 }
8105 
8106 inline const Type *QualType::getTypePtrOrNull() const {
8107   return (isNull() ? nullptr : getCommonPtr()->BaseType);
8108 }
8109 
8110 inline bool QualType::isReferenceable() const {
8111   // C++ [defns.referenceable]
8112   //   type that is either an object type, a function type that does not have
8113   //   cv-qualifiers or a ref-qualifier, or a reference type.
8114   const Type &Self = **this;
8115   if (Self.isObjectType() || Self.isReferenceType())
8116     return true;
8117   if (const auto *F = Self.getAs<FunctionProtoType>())
8118     return F->getMethodQuals().empty() && F->getRefQualifier() == RQ_None;
8119 
8120   return false;
8121 }
8122 
8123 inline SplitQualType QualType::split() const {
8124   if (!hasLocalNonFastQualifiers())
8125     return SplitQualType(getTypePtrUnsafe(),
8126                          Qualifiers::fromFastMask(getLocalFastQualifiers()));
8127 
8128   const ExtQuals *eq = getExtQualsUnsafe();
8129   Qualifiers qs = eq->getQualifiers();
8130   qs.addFastQualifiers(getLocalFastQualifiers());
8131   return SplitQualType(eq->getBaseType(), qs);
8132 }
8133 
8134 inline Qualifiers QualType::getLocalQualifiers() const {
8135   Qualifiers Quals;
8136   if (hasLocalNonFastQualifiers())
8137     Quals = getExtQualsUnsafe()->getQualifiers();
8138   Quals.addFastQualifiers(getLocalFastQualifiers());
8139   return Quals;
8140 }
8141 
8142 inline Qualifiers QualType::getQualifiers() const {
8143   Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers();
8144   quals.addFastQualifiers(getLocalFastQualifiers());
8145   return quals;
8146 }
8147 
8148 inline unsigned QualType::getCVRQualifiers() const {
8149   unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers();
8150   cvr |= getLocalCVRQualifiers();
8151   return cvr;
8152 }
8153 
8154 inline QualType QualType::getCanonicalType() const {
8155   QualType canon = getCommonPtr()->CanonicalType;
8156   return canon.withFastQualifiers(getLocalFastQualifiers());
8157 }
8158 
8159 inline bool QualType::isCanonical() const {
8160   return getTypePtr()->isCanonicalUnqualified();
8161 }
8162 
8163 inline bool QualType::isCanonicalAsParam() const {
8164   if (!isCanonical()) return false;
8165   if (hasLocalQualifiers()) return false;
8166 
8167   const Type *T = getTypePtr();
8168   if (T->isVariablyModifiedType() && T->hasSizedVLAType())
8169     return false;
8170 
8171   return !isa<FunctionType>(T) &&
8172          (!isa<ArrayType>(T) || isa<ArrayParameterType>(T));
8173 }
8174 
8175 inline bool QualType::isConstQualified() const {
8176   return isLocalConstQualified() ||
8177          getCommonPtr()->CanonicalType.isLocalConstQualified();
8178 }
8179 
8180 inline bool QualType::isRestrictQualified() const {
8181   return isLocalRestrictQualified() ||
8182          getCommonPtr()->CanonicalType.isLocalRestrictQualified();
8183 }
8184 
8185 
8186 inline bool QualType::isVolatileQualified() const {
8187   return isLocalVolatileQualified() ||
8188          getCommonPtr()->CanonicalType.isLocalVolatileQualified();
8189 }
8190 
8191 inline bool QualType::hasQualifiers() const {
8192   return hasLocalQualifiers() ||
8193          getCommonPtr()->CanonicalType.hasLocalQualifiers();
8194 }
8195 
8196 inline QualType QualType::getUnqualifiedType() const {
8197   if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
8198     return QualType(getTypePtr(), 0);
8199 
8200   return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0);
8201 }
8202 
8203 inline SplitQualType QualType::getSplitUnqualifiedType() const {
8204   if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
8205     return split();
8206 
8207   return getSplitUnqualifiedTypeImpl(*this);
8208 }
8209 
8210 inline void QualType::removeLocalConst() {
8211   removeLocalFastQualifiers(Qualifiers::Const);
8212 }
8213 
8214 inline void QualType::removeLocalRestrict() {
8215   removeLocalFastQualifiers(Qualifiers::Restrict);
8216 }
8217 
8218 inline void QualType::removeLocalVolatile() {
8219   removeLocalFastQualifiers(Qualifiers::Volatile);
8220 }
8221 
8222 /// Check if this type has any address space qualifier.
8223 inline bool QualType::hasAddressSpace() const {
8224   return getQualifiers().hasAddressSpace();
8225 }
8226 
8227 /// Return the address space of this type.
8228 inline LangAS QualType::getAddressSpace() const {
8229   return getQualifiers().getAddressSpace();
8230 }
8231 
8232 /// Return the gc attribute of this type.
8233 inline Qualifiers::GC QualType::getObjCGCAttr() const {
8234   return getQualifiers().getObjCGCAttr();
8235 }
8236 
8237 inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const {
8238   if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
8239     return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD);
8240   return false;
8241 }
8242 
8243 inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const {
8244   if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
8245     return hasNonTrivialToPrimitiveDestructCUnion(RD);
8246   return false;
8247 }
8248 
8249 inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const {
8250   if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
8251     return hasNonTrivialToPrimitiveCopyCUnion(RD);
8252   return false;
8253 }
8254 
8255 inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) {
8256   if (const auto *PT = t.getAs<PointerType>()) {
8257     if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>())
8258       return FT->getExtInfo();
8259   } else if (const auto *FT = t.getAs<FunctionType>())
8260     return FT->getExtInfo();
8261 
8262   return FunctionType::ExtInfo();
8263 }
8264 
8265 inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) {
8266   return getFunctionExtInfo(*t);
8267 }
8268 
8269 /// Determine whether this type is more
8270 /// qualified than the Other type. For example, "const volatile int"
8271 /// is more qualified than "const int", "volatile int", and
8272 /// "int". However, it is not more qualified than "const volatile
8273 /// int".
8274 inline bool QualType::isMoreQualifiedThan(QualType other,
8275                                           const ASTContext &Ctx) const {
8276   Qualifiers MyQuals = getQualifiers();
8277   Qualifiers OtherQuals = other.getQualifiers();
8278   return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals, Ctx));
8279 }
8280 
8281 /// Determine whether this type is at last
8282 /// as qualified as the Other type. For example, "const volatile
8283 /// int" is at least as qualified as "const int", "volatile int",
8284 /// "int", and "const volatile int".
8285 inline bool QualType::isAtLeastAsQualifiedAs(QualType other,
8286                                              const ASTContext &Ctx) const {
8287   Qualifiers OtherQuals = other.getQualifiers();
8288 
8289   // Ignore __unaligned qualifier if this type is a void.
8290   if (getUnqualifiedType()->isVoidType())
8291     OtherQuals.removeUnaligned();
8292 
8293   return getQualifiers().compatiblyIncludes(OtherQuals, Ctx);
8294 }
8295 
8296 /// If Type is a reference type (e.g., const
8297 /// int&), returns the type that the reference refers to ("const
8298 /// int"). Otherwise, returns the type itself. This routine is used
8299 /// throughout Sema to implement C++ 5p6:
8300 ///
8301 ///   If an expression initially has the type "reference to T" (8.3.2,
8302 ///   8.5.3), the type is adjusted to "T" prior to any further
8303 ///   analysis, the expression designates the object or function
8304 ///   denoted by the reference, and the expression is an lvalue.
8305 inline QualType QualType::getNonReferenceType() const {
8306   if (const auto *RefType = (*this)->getAs<ReferenceType>())
8307     return RefType->getPointeeType();
8308   else
8309     return *this;
8310 }
8311 
8312 inline bool QualType::isCForbiddenLValueType() const {
8313   return ((getTypePtr()->isVoidType() && !hasQualifiers()) ||
8314           getTypePtr()->isFunctionType());
8315 }
8316 
8317 /// Tests whether the type is categorized as a fundamental type.
8318 ///
8319 /// \returns True for types specified in C++0x [basic.fundamental].
8320 inline bool Type::isFundamentalType() const {
8321   return isVoidType() ||
8322          isNullPtrType() ||
8323          // FIXME: It's really annoying that we don't have an
8324          // 'isArithmeticType()' which agrees with the standard definition.
8325          (isArithmeticType() && !isEnumeralType());
8326 }
8327 
8328 /// Tests whether the type is categorized as a compound type.
8329 ///
8330 /// \returns True for types specified in C++0x [basic.compound].
8331 inline bool Type::isCompoundType() const {
8332   // C++0x [basic.compound]p1:
8333   //   Compound types can be constructed in the following ways:
8334   //    -- arrays of objects of a given type [...];
8335   return isArrayType() ||
8336   //    -- functions, which have parameters of given types [...];
8337          isFunctionType() ||
8338   //    -- pointers to void or objects or functions [...];
8339          isPointerType() ||
8340   //    -- references to objects or functions of a given type. [...]
8341          isReferenceType() ||
8342   //    -- classes containing a sequence of objects of various types, [...];
8343          isRecordType() ||
8344   //    -- unions, which are classes capable of containing objects of different
8345   //               types at different times;
8346          isUnionType() ||
8347   //    -- enumerations, which comprise a set of named constant values. [...];
8348          isEnumeralType() ||
8349   //    -- pointers to non-static class members, [...].
8350          isMemberPointerType();
8351 }
8352 
8353 inline bool Type::isFunctionType() const {
8354   return isa<FunctionType>(CanonicalType);
8355 }
8356 
8357 inline bool Type::isPointerType() const {
8358   return isa<PointerType>(CanonicalType);
8359 }
8360 
8361 inline bool Type::isPointerOrReferenceType() const {
8362   return isPointerType() || isReferenceType();
8363 }
8364 
8365 inline bool Type::isAnyPointerType() const {
8366   return isPointerType() || isObjCObjectPointerType();
8367 }
8368 
8369 inline bool Type::isSignableType(const ASTContext &Ctx) const {
8370   return isSignablePointerType() || isSignableIntegerType(Ctx);
8371 }
8372 
8373 inline bool Type::isSignablePointerType() const {
8374   return isPointerType() || isObjCClassType() || isObjCQualifiedClassType();
8375 }
8376 
8377 inline bool Type::isBlockPointerType() const {
8378   return isa<BlockPointerType>(CanonicalType);
8379 }
8380 
8381 inline bool Type::isReferenceType() const {
8382   return isa<ReferenceType>(CanonicalType);
8383 }
8384 
8385 inline bool Type::isLValueReferenceType() const {
8386   return isa<LValueReferenceType>(CanonicalType);
8387 }
8388 
8389 inline bool Type::isRValueReferenceType() const {
8390   return isa<RValueReferenceType>(CanonicalType);
8391 }
8392 
8393 inline bool Type::isObjectPointerType() const {
8394   // Note: an "object pointer type" is not the same thing as a pointer to an
8395   // object type; rather, it is a pointer to an object type or a pointer to cv
8396   // void.
8397   if (const auto *T = getAs<PointerType>())
8398     return !T->getPointeeType()->isFunctionType();
8399   else
8400     return false;
8401 }
8402 
8403 inline bool Type::isCFIUncheckedCalleeFunctionType() const {
8404   if (const auto *Fn = getAs<FunctionProtoType>())
8405     return Fn->hasCFIUncheckedCallee();
8406   return false;
8407 }
8408 
8409 inline bool Type::hasPointeeToToCFIUncheckedCalleeFunctionType() const {
8410   QualType Pointee;
8411   if (const auto *PT = getAs<PointerType>())
8412     Pointee = PT->getPointeeType();
8413   else if (const auto *RT = getAs<ReferenceType>())
8414     Pointee = RT->getPointeeType();
8415   else if (const auto *MPT = getAs<MemberPointerType>())
8416     Pointee = MPT->getPointeeType();
8417   else if (const auto *DT = getAs<DecayedType>())
8418     Pointee = DT->getPointeeType();
8419   else
8420     return false;
8421   return Pointee->isCFIUncheckedCalleeFunctionType();
8422 }
8423 
8424 inline bool Type::isFunctionPointerType() const {
8425   if (const auto *T = getAs<PointerType>())
8426     return T->getPointeeType()->isFunctionType();
8427   else
8428     return false;
8429 }
8430 
8431 inline bool Type::isFunctionReferenceType() const {
8432   if (const auto *T = getAs<ReferenceType>())
8433     return T->getPointeeType()->isFunctionType();
8434   else
8435     return false;
8436 }
8437 
8438 inline bool Type::isMemberPointerType() const {
8439   return isa<MemberPointerType>(CanonicalType);
8440 }
8441 
8442 inline bool Type::isMemberFunctionPointerType() const {
8443   if (const auto *T = getAs<MemberPointerType>())
8444     return T->isMemberFunctionPointer();
8445   else
8446     return false;
8447 }
8448 
8449 inline bool Type::isMemberDataPointerType() const {
8450   if (const auto *T = getAs<MemberPointerType>())
8451     return T->isMemberDataPointer();
8452   else
8453     return false;
8454 }
8455 
8456 inline bool Type::isArrayType() const {
8457   return isa<ArrayType>(CanonicalType);
8458 }
8459 
8460 inline bool Type::isConstantArrayType() const {
8461   return isa<ConstantArrayType>(CanonicalType);
8462 }
8463 
8464 inline bool Type::isIncompleteArrayType() const {
8465   return isa<IncompleteArrayType>(CanonicalType);
8466 }
8467 
8468 inline bool Type::isVariableArrayType() const {
8469   return isa<VariableArrayType>(CanonicalType);
8470 }
8471 
8472 inline bool Type::isArrayParameterType() const {
8473   return isa<ArrayParameterType>(CanonicalType);
8474 }
8475 
8476 inline bool Type::isDependentSizedArrayType() const {
8477   return isa<DependentSizedArrayType>(CanonicalType);
8478 }
8479 
8480 inline bool Type::isBuiltinType() const {
8481   return isa<BuiltinType>(CanonicalType);
8482 }
8483 
8484 inline bool Type::isRecordType() const {
8485   return isa<RecordType>(CanonicalType);
8486 }
8487 
8488 inline bool Type::isEnumeralType() const {
8489   return isa<EnumType>(CanonicalType);
8490 }
8491 
8492 inline bool Type::isAnyComplexType() const {
8493   return isa<ComplexType>(CanonicalType);
8494 }
8495 
8496 inline bool Type::isVectorType() const {
8497   return isa<VectorType>(CanonicalType);
8498 }
8499 
8500 inline bool Type::isExtVectorType() const {
8501   return isa<ExtVectorType>(CanonicalType);
8502 }
8503 
8504 inline bool Type::isExtVectorBoolType() const {
8505   if (!isExtVectorType())
8506     return false;
8507   return cast<ExtVectorType>(CanonicalType)->getElementType()->isBooleanType();
8508 }
8509 
8510 inline bool Type::isSubscriptableVectorType() const {
8511   return isVectorType() || isSveVLSBuiltinType();
8512 }
8513 
8514 inline bool Type::isMatrixType() const {
8515   return isa<MatrixType>(CanonicalType);
8516 }
8517 
8518 inline bool Type::isConstantMatrixType() const {
8519   return isa<ConstantMatrixType>(CanonicalType);
8520 }
8521 
8522 inline bool Type::isDependentAddressSpaceType() const {
8523   return isa<DependentAddressSpaceType>(CanonicalType);
8524 }
8525 
8526 inline bool Type::isObjCObjectPointerType() const {
8527   return isa<ObjCObjectPointerType>(CanonicalType);
8528 }
8529 
8530 inline bool Type::isObjCObjectType() const {
8531   return isa<ObjCObjectType>(CanonicalType);
8532 }
8533 
8534 inline bool Type::isObjCObjectOrInterfaceType() const {
8535   return isa<ObjCInterfaceType>(CanonicalType) ||
8536     isa<ObjCObjectType>(CanonicalType);
8537 }
8538 
8539 inline bool Type::isAtomicType() const {
8540   return isa<AtomicType>(CanonicalType);
8541 }
8542 
8543 inline bool Type::isUndeducedAutoType() const {
8544   return isa<AutoType>(CanonicalType);
8545 }
8546 
8547 inline bool Type::isObjCQualifiedIdType() const {
8548   if (const auto *OPT = getAs<ObjCObjectPointerType>())
8549     return OPT->isObjCQualifiedIdType();
8550   return false;
8551 }
8552 
8553 inline bool Type::isObjCQualifiedClassType() const {
8554   if (const auto *OPT = getAs<ObjCObjectPointerType>())
8555     return OPT->isObjCQualifiedClassType();
8556   return false;
8557 }
8558 
8559 inline bool Type::isObjCIdType() const {
8560   if (const auto *OPT = getAs<ObjCObjectPointerType>())
8561     return OPT->isObjCIdType();
8562   return false;
8563 }
8564 
8565 inline bool Type::isObjCClassType() const {
8566   if (const auto *OPT = getAs<ObjCObjectPointerType>())
8567     return OPT->isObjCClassType();
8568   return false;
8569 }
8570 
8571 inline bool Type::isObjCSelType() const {
8572   if (const auto *OPT = getAs<PointerType>())
8573     return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel);
8574   return false;
8575 }
8576 
8577 inline bool Type::isObjCBuiltinType() const {
8578   return isObjCIdType() || isObjCClassType() || isObjCSelType();
8579 }
8580 
8581 inline bool Type::isDecltypeType() const {
8582   return isa<DecltypeType>(this);
8583 }
8584 
8585 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8586   inline bool Type::is##Id##Type() const { \
8587     return isSpecificBuiltinType(BuiltinType::Id); \
8588   }
8589 #include "clang/Basic/OpenCLImageTypes.def"
8590 
8591 inline bool Type::isSamplerT() const {
8592   return isSpecificBuiltinType(BuiltinType::OCLSampler);
8593 }
8594 
8595 inline bool Type::isEventT() const {
8596   return isSpecificBuiltinType(BuiltinType::OCLEvent);
8597 }
8598 
8599 inline bool Type::isClkEventT() const {
8600   return isSpecificBuiltinType(BuiltinType::OCLClkEvent);
8601 }
8602 
8603 inline bool Type::isQueueT() const {
8604   return isSpecificBuiltinType(BuiltinType::OCLQueue);
8605 }
8606 
8607 inline bool Type::isReserveIDT() const {
8608   return isSpecificBuiltinType(BuiltinType::OCLReserveID);
8609 }
8610 
8611 inline bool Type::isImageType() const {
8612 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() ||
8613   return
8614 #include "clang/Basic/OpenCLImageTypes.def"
8615       false; // end boolean or operation
8616 }
8617 
8618 inline bool Type::isPipeType() const {
8619   return isa<PipeType>(CanonicalType);
8620 }
8621 
8622 inline bool Type::isBitIntType() const {
8623   return isa<BitIntType>(CanonicalType);
8624 }
8625 
8626 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8627   inline bool Type::is##Id##Type() const { \
8628     return isSpecificBuiltinType(BuiltinType::Id); \
8629   }
8630 #include "clang/Basic/OpenCLExtensionTypes.def"
8631 
8632 inline bool Type::isOCLIntelSubgroupAVCType() const {
8633 #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \
8634   isOCLIntelSubgroupAVC##Id##Type() ||
8635   return
8636 #include "clang/Basic/OpenCLExtensionTypes.def"
8637     false; // end of boolean or operation
8638 }
8639 
8640 inline bool Type::isOCLExtOpaqueType() const {
8641 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() ||
8642   return
8643 #include "clang/Basic/OpenCLExtensionTypes.def"
8644     false; // end of boolean or operation
8645 }
8646 
8647 inline bool Type::isOpenCLSpecificType() const {
8648   return isSamplerT() || isEventT() || isImageType() || isClkEventT() ||
8649          isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType();
8650 }
8651 
8652 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId)                            \
8653   inline bool Type::is##Id##Type() const {                                     \
8654     return isSpecificBuiltinType(BuiltinType::Id);                             \
8655   }
8656 #include "clang/Basic/HLSLIntangibleTypes.def"
8657 
8658 inline bool Type::isHLSLBuiltinIntangibleType() const {
8659 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) is##Id##Type() ||
8660   return
8661 #include "clang/Basic/HLSLIntangibleTypes.def"
8662       false;
8663 }
8664 
8665 inline bool Type::isHLSLSpecificType() const {
8666   return isHLSLBuiltinIntangibleType() || isHLSLAttributedResourceType() ||
8667          isHLSLInlineSpirvType();
8668 }
8669 
8670 inline bool Type::isHLSLAttributedResourceType() const {
8671   return isa<HLSLAttributedResourceType>(this);
8672 }
8673 
8674 inline bool Type::isHLSLInlineSpirvType() const {
8675   return isa<HLSLInlineSpirvType>(this);
8676 }
8677 
8678 inline bool Type::isTemplateTypeParmType() const {
8679   return isa<TemplateTypeParmType>(CanonicalType);
8680 }
8681 
8682 inline bool Type::isSpecificBuiltinType(unsigned K) const {
8683   if (const BuiltinType *BT = getAs<BuiltinType>()) {
8684     return BT->getKind() == static_cast<BuiltinType::Kind>(K);
8685   }
8686   return false;
8687 }
8688 
8689 inline bool Type::isPlaceholderType() const {
8690   if (const auto *BT = dyn_cast<BuiltinType>(this))
8691     return BT->isPlaceholderType();
8692   return false;
8693 }
8694 
8695 inline const BuiltinType *Type::getAsPlaceholderType() const {
8696   if (const auto *BT = dyn_cast<BuiltinType>(this))
8697     if (BT->isPlaceholderType())
8698       return BT;
8699   return nullptr;
8700 }
8701 
8702 inline bool Type::isSpecificPlaceholderType(unsigned K) const {
8703   assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K));
8704   return isSpecificBuiltinType(K);
8705 }
8706 
8707 inline bool Type::isNonOverloadPlaceholderType() const {
8708   if (const auto *BT = dyn_cast<BuiltinType>(this))
8709     return BT->isNonOverloadPlaceholderType();
8710   return false;
8711 }
8712 
8713 inline bool Type::isVoidType() const {
8714   return isSpecificBuiltinType(BuiltinType::Void);
8715 }
8716 
8717 inline bool Type::isHalfType() const {
8718   // FIXME: Should we allow complex __fp16? Probably not.
8719   return isSpecificBuiltinType(BuiltinType::Half);
8720 }
8721 
8722 inline bool Type::isFloat16Type() const {
8723   return isSpecificBuiltinType(BuiltinType::Float16);
8724 }
8725 
8726 inline bool Type::isFloat32Type() const {
8727   return isSpecificBuiltinType(BuiltinType::Float);
8728 }
8729 
8730 inline bool Type::isDoubleType() const {
8731   return isSpecificBuiltinType(BuiltinType::Double);
8732 }
8733 
8734 inline bool Type::isBFloat16Type() const {
8735   return isSpecificBuiltinType(BuiltinType::BFloat16);
8736 }
8737 
8738 inline bool Type::isMFloat8Type() const {
8739   return isSpecificBuiltinType(BuiltinType::MFloat8);
8740 }
8741 
8742 inline bool Type::isFloat128Type() const {
8743   return isSpecificBuiltinType(BuiltinType::Float128);
8744 }
8745 
8746 inline bool Type::isIbm128Type() const {
8747   return isSpecificBuiltinType(BuiltinType::Ibm128);
8748 }
8749 
8750 inline bool Type::isNullPtrType() const {
8751   return isSpecificBuiltinType(BuiltinType::NullPtr);
8752 }
8753 
8754 bool IsEnumDeclComplete(EnumDecl *);
8755 bool IsEnumDeclScoped(EnumDecl *);
8756 
8757 inline bool Type::isIntegerType() const {
8758   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
8759     return BT->isInteger();
8760   if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
8761     // Incomplete enum types are not treated as integer types.
8762     // FIXME: In C++, enum types are never integer types.
8763     return IsEnumDeclComplete(ET->getDecl()) &&
8764       !IsEnumDeclScoped(ET->getDecl());
8765   }
8766   return isBitIntType();
8767 }
8768 
8769 inline bool Type::isFixedPointType() const {
8770   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
8771     return BT->getKind() >= BuiltinType::ShortAccum &&
8772            BT->getKind() <= BuiltinType::SatULongFract;
8773   }
8774   return false;
8775 }
8776 
8777 inline bool Type::isFixedPointOrIntegerType() const {
8778   return isFixedPointType() || isIntegerType();
8779 }
8780 
8781 inline bool Type::isConvertibleToFixedPointType() const {
8782   return isRealFloatingType() || isFixedPointOrIntegerType();
8783 }
8784 
8785 inline bool Type::isSaturatedFixedPointType() const {
8786   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
8787     return BT->getKind() >= BuiltinType::SatShortAccum &&
8788            BT->getKind() <= BuiltinType::SatULongFract;
8789   }
8790   return false;
8791 }
8792 
8793 inline bool Type::isUnsaturatedFixedPointType() const {
8794   return isFixedPointType() && !isSaturatedFixedPointType();
8795 }
8796 
8797 inline bool Type::isSignedFixedPointType() const {
8798   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
8799     return ((BT->getKind() >= BuiltinType::ShortAccum &&
8800              BT->getKind() <= BuiltinType::LongAccum) ||
8801             (BT->getKind() >= BuiltinType::ShortFract &&
8802              BT->getKind() <= BuiltinType::LongFract) ||
8803             (BT->getKind() >= BuiltinType::SatShortAccum &&
8804              BT->getKind() <= BuiltinType::SatLongAccum) ||
8805             (BT->getKind() >= BuiltinType::SatShortFract &&
8806              BT->getKind() <= BuiltinType::SatLongFract));
8807   }
8808   return false;
8809 }
8810 
8811 inline bool Type::isUnsignedFixedPointType() const {
8812   return isFixedPointType() && !isSignedFixedPointType();
8813 }
8814 
8815 inline bool Type::isScalarType() const {
8816   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
8817     return BT->getKind() > BuiltinType::Void &&
8818            BT->getKind() <= BuiltinType::NullPtr;
8819   if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
8820     // Enums are scalar types, but only if they are defined.  Incomplete enums
8821     // are not treated as scalar types.
8822     return IsEnumDeclComplete(ET->getDecl());
8823   return isa<PointerType>(CanonicalType) ||
8824          isa<BlockPointerType>(CanonicalType) ||
8825          isa<MemberPointerType>(CanonicalType) ||
8826          isa<ComplexType>(CanonicalType) ||
8827          isa<ObjCObjectPointerType>(CanonicalType) ||
8828          isBitIntType();
8829 }
8830 
8831 inline bool Type::isIntegralOrEnumerationType() const {
8832   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
8833     return BT->isInteger();
8834 
8835   // Check for a complete enum type; incomplete enum types are not properly an
8836   // enumeration type in the sense required here.
8837   if (const auto *ET = dyn_cast<EnumType>(CanonicalType))
8838     return IsEnumDeclComplete(ET->getDecl());
8839 
8840   return isBitIntType();
8841 }
8842 
8843 inline bool Type::isBooleanType() const {
8844   if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
8845     return BT->getKind() == BuiltinType::Bool;
8846   return false;
8847 }
8848 
8849 inline bool Type::isUndeducedType() const {
8850   auto *DT = getContainedDeducedType();
8851   return DT && !DT->isDeduced();
8852 }
8853 
8854 /// Determines whether this is a type for which one can define
8855 /// an overloaded operator.
8856 inline bool Type::isOverloadableType() const {
8857   if (!isDependentType())
8858     return isRecordType() || isEnumeralType();
8859   return !isArrayType() && !isFunctionType() && !isAnyPointerType() &&
8860          !isMemberPointerType();
8861 }
8862 
8863 /// Determines whether this type is written as a typedef-name.
8864 inline bool Type::isTypedefNameType() const {
8865   if (getAs<TypedefType>())
8866     return true;
8867   if (auto *TST = getAs<TemplateSpecializationType>())
8868     return TST->isTypeAlias();
8869   return false;
8870 }
8871 
8872 /// Determines whether this type can decay to a pointer type.
8873 inline bool Type::canDecayToPointerType() const {
8874   return isFunctionType() || (isArrayType() && !isArrayParameterType());
8875 }
8876 
8877 inline bool Type::hasPointerRepresentation() const {
8878   return (isPointerType() || isReferenceType() || isBlockPointerType() ||
8879           isObjCObjectPointerType() || isNullPtrType());
8880 }
8881 
8882 inline bool Type::hasObjCPointerRepresentation() const {
8883   return isObjCObjectPointerType();
8884 }
8885 
8886 inline const Type *Type::getBaseElementTypeUnsafe() const {
8887   const Type *type = this;
8888   while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe())
8889     type = arrayType->getElementType().getTypePtr();
8890   return type;
8891 }
8892 
8893 inline const Type *Type::getPointeeOrArrayElementType() const {
8894   const Type *type = this;
8895   if (type->isAnyPointerType())
8896     return type->getPointeeType().getTypePtr();
8897   else if (type->isArrayType())
8898     return type->getBaseElementTypeUnsafe();
8899   return type;
8900 }
8901 /// Insertion operator for partial diagnostics. This allows sending adress
8902 /// spaces into a diagnostic with <<.
8903 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
8904                                              LangAS AS) {
8905   PD.AddTaggedVal(llvm::to_underlying(AS),
8906                   DiagnosticsEngine::ArgumentKind::ak_addrspace);
8907   return PD;
8908 }
8909 
8910 /// Insertion operator for partial diagnostics. This allows sending Qualifiers
8911 /// into a diagnostic with <<.
8912 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
8913                                              Qualifiers Q) {
8914   PD.AddTaggedVal(Q.getAsOpaqueValue(),
8915                   DiagnosticsEngine::ArgumentKind::ak_qual);
8916   return PD;
8917 }
8918 
8919 /// Insertion operator for partial diagnostics.  This allows sending QualType's
8920 /// into a diagnostic with <<.
8921 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
8922                                              QualType T) {
8923   PD.AddTaggedVal(reinterpret_cast<uint64_t>(T.getAsOpaquePtr()),
8924                   DiagnosticsEngine::ak_qualtype);
8925   return PD;
8926 }
8927 
8928 // Helper class template that is used by Type::getAs to ensure that one does
8929 // not try to look through a qualified type to get to an array type.
8930 template <typename T>
8931 using TypeIsArrayType =
8932     std::integral_constant<bool, std::is_same<T, ArrayType>::value ||
8933                                      std::is_base_of<ArrayType, T>::value>;
8934 
8935 // Member-template getAs<specific type>'.
8936 template <typename T> const T *Type::getAs() const {
8937   static_assert(!TypeIsArrayType<T>::value,
8938                 "ArrayType cannot be used with getAs!");
8939 
8940   // If this is directly a T type, return it.
8941   if (const auto *Ty = dyn_cast<T>(this))
8942     return Ty;
8943 
8944   // If the canonical form of this type isn't the right kind, reject it.
8945   if (!isa<T>(CanonicalType))
8946     return nullptr;
8947 
8948   // If this is a typedef for the type, strip the typedef off without
8949   // losing all typedef information.
8950   return cast<T>(getUnqualifiedDesugaredType());
8951 }
8952 
8953 template <typename T> const T *Type::getAsAdjusted() const {
8954   static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!");
8955 
8956   // If this is directly a T type, return it.
8957   if (const auto *Ty = dyn_cast<T>(this))
8958     return Ty;
8959 
8960   // If the canonical form of this type isn't the right kind, reject it.
8961   if (!isa<T>(CanonicalType))
8962     return nullptr;
8963 
8964   // Strip off type adjustments that do not modify the underlying nature of the
8965   // type.
8966   const Type *Ty = this;
8967   while (Ty) {
8968     if (const auto *A = dyn_cast<AttributedType>(Ty))
8969       Ty = A->getModifiedType().getTypePtr();
8970     else if (const auto *A = dyn_cast<BTFTagAttributedType>(Ty))
8971       Ty = A->getWrappedType().getTypePtr();
8972     else if (const auto *A = dyn_cast<HLSLAttributedResourceType>(Ty))
8973       Ty = A->getWrappedType().getTypePtr();
8974     else if (const auto *E = dyn_cast<ElaboratedType>(Ty))
8975       Ty = E->desugar().getTypePtr();
8976     else if (const auto *P = dyn_cast<ParenType>(Ty))
8977       Ty = P->desugar().getTypePtr();
8978     else if (const auto *A = dyn_cast<AdjustedType>(Ty))
8979       Ty = A->desugar().getTypePtr();
8980     else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty))
8981       Ty = M->desugar().getTypePtr();
8982     else
8983       break;
8984   }
8985 
8986   // Just because the canonical type is correct does not mean we can use cast<>,
8987   // since we may not have stripped off all the sugar down to the base type.
8988   return dyn_cast<T>(Ty);
8989 }
8990 
8991 inline const ArrayType *Type::getAsArrayTypeUnsafe() const {
8992   // If this is directly an array type, return it.
8993   if (const auto *arr = dyn_cast<ArrayType>(this))
8994     return arr;
8995 
8996   // If the canonical form of this type isn't the right kind, reject it.
8997   if (!isa<ArrayType>(CanonicalType))
8998     return nullptr;
8999 
9000   // If this is a typedef for the type, strip the typedef off without
9001   // losing all typedef information.
9002   return cast<ArrayType>(getUnqualifiedDesugaredType());
9003 }
9004 
9005 template <typename T> const T *Type::castAs() const {
9006   static_assert(!TypeIsArrayType<T>::value,
9007                 "ArrayType cannot be used with castAs!");
9008 
9009   if (const auto *ty = dyn_cast<T>(this)) return ty;
9010   assert(isa<T>(CanonicalType));
9011   return cast<T>(getUnqualifiedDesugaredType());
9012 }
9013 
9014 inline const ArrayType *Type::castAsArrayTypeUnsafe() const {
9015   assert(isa<ArrayType>(CanonicalType));
9016   if (const auto *arr = dyn_cast<ArrayType>(this)) return arr;
9017   return cast<ArrayType>(getUnqualifiedDesugaredType());
9018 }
9019 
9020 DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr,
9021                          QualType CanonicalPtr)
9022     : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) {
9023 #ifndef NDEBUG
9024   QualType Adjusted = getAdjustedType();
9025   (void)AttributedType::stripOuterNullability(Adjusted);
9026   assert(isa<PointerType>(Adjusted));
9027 #endif
9028 }
9029 
9030 QualType DecayedType::getPointeeType() const {
9031   QualType Decayed = getDecayedType();
9032   (void)AttributedType::stripOuterNullability(Decayed);
9033   return cast<PointerType>(Decayed)->getPointeeType();
9034 }
9035 
9036 // Get the decimal string representation of a fixed point type, represented
9037 // as a scaled integer.
9038 // TODO: At some point, we should change the arguments to instead just accept an
9039 // APFixedPoint instead of APSInt and scale.
9040 void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
9041                              unsigned Scale);
9042 
9043 inline FunctionEffectsRef FunctionEffectsRef::get(QualType QT) {
9044   const Type *TypePtr = QT.getTypePtr();
9045   while (true) {
9046     if (QualType Pointee = TypePtr->getPointeeType(); !Pointee.isNull())
9047       TypePtr = Pointee.getTypePtr();
9048     else if (TypePtr->isArrayType())
9049       TypePtr = TypePtr->getBaseElementTypeUnsafe();
9050     else
9051       break;
9052   }
9053   if (const auto *FPT = TypePtr->getAs<FunctionProtoType>())
9054     return FPT->getFunctionEffects();
9055   return {};
9056 }
9057 
9058 } // namespace clang
9059 
9060 #endif // LLVM_CLANG_AST_TYPE_H
9061