File: | clang/lib/Sema/SemaType.cpp |
Warning: | line 5851, column 15 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// | ||||||||||||
2 | // | ||||||||||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||
6 | // | ||||||||||||
7 | //===----------------------------------------------------------------------===// | ||||||||||||
8 | // | ||||||||||||
9 | // This file implements type-related semantic analysis. | ||||||||||||
10 | // | ||||||||||||
11 | //===----------------------------------------------------------------------===// | ||||||||||||
12 | |||||||||||||
13 | #include "TypeLocBuilder.h" | ||||||||||||
14 | #include "clang/AST/ASTConsumer.h" | ||||||||||||
15 | #include "clang/AST/ASTContext.h" | ||||||||||||
16 | #include "clang/AST/ASTMutationListener.h" | ||||||||||||
17 | #include "clang/AST/ASTStructuralEquivalence.h" | ||||||||||||
18 | #include "clang/AST/CXXInheritance.h" | ||||||||||||
19 | #include "clang/AST/DeclObjC.h" | ||||||||||||
20 | #include "clang/AST/DeclTemplate.h" | ||||||||||||
21 | #include "clang/AST/Expr.h" | ||||||||||||
22 | #include "clang/AST/TypeLoc.h" | ||||||||||||
23 | #include "clang/AST/TypeLocVisitor.h" | ||||||||||||
24 | #include "clang/Basic/PartialDiagnostic.h" | ||||||||||||
25 | #include "clang/Basic/TargetInfo.h" | ||||||||||||
26 | #include "clang/Lex/Preprocessor.h" | ||||||||||||
27 | #include "clang/Sema/DeclSpec.h" | ||||||||||||
28 | #include "clang/Sema/DelayedDiagnostic.h" | ||||||||||||
29 | #include "clang/Sema/Lookup.h" | ||||||||||||
30 | #include "clang/Sema/ParsedTemplate.h" | ||||||||||||
31 | #include "clang/Sema/ScopeInfo.h" | ||||||||||||
32 | #include "clang/Sema/SemaInternal.h" | ||||||||||||
33 | #include "clang/Sema/Template.h" | ||||||||||||
34 | #include "clang/Sema/TemplateInstCallback.h" | ||||||||||||
35 | #include "llvm/ADT/SmallPtrSet.h" | ||||||||||||
36 | #include "llvm/ADT/SmallString.h" | ||||||||||||
37 | #include "llvm/ADT/StringSwitch.h" | ||||||||||||
38 | #include "llvm/IR/DerivedTypes.h" | ||||||||||||
39 | #include "llvm/Support/ErrorHandling.h" | ||||||||||||
40 | #include <bitset> | ||||||||||||
41 | |||||||||||||
42 | using namespace clang; | ||||||||||||
43 | |||||||||||||
44 | enum TypeDiagSelector { | ||||||||||||
45 | TDS_Function, | ||||||||||||
46 | TDS_Pointer, | ||||||||||||
47 | TDS_ObjCObjOrBlock | ||||||||||||
48 | }; | ||||||||||||
49 | |||||||||||||
50 | /// isOmittedBlockReturnType - Return true if this declarator is missing a | ||||||||||||
51 | /// return type because this is a omitted return type on a block literal. | ||||||||||||
52 | static bool isOmittedBlockReturnType(const Declarator &D) { | ||||||||||||
53 | if (D.getContext() != DeclaratorContext::BlockLiteral || | ||||||||||||
54 | D.getDeclSpec().hasTypeSpecifier()) | ||||||||||||
55 | return false; | ||||||||||||
56 | |||||||||||||
57 | if (D.getNumTypeObjects() == 0) | ||||||||||||
58 | return true; // ^{ ... } | ||||||||||||
59 | |||||||||||||
60 | if (D.getNumTypeObjects() == 1 && | ||||||||||||
61 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) | ||||||||||||
62 | return true; // ^(int X, float Y) { ... } | ||||||||||||
63 | |||||||||||||
64 | return false; | ||||||||||||
65 | } | ||||||||||||
66 | |||||||||||||
67 | /// diagnoseBadTypeAttribute - Diagnoses a type attribute which | ||||||||||||
68 | /// doesn't apply to the given type. | ||||||||||||
69 | static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr, | ||||||||||||
70 | QualType type) { | ||||||||||||
71 | TypeDiagSelector WhichType; | ||||||||||||
72 | bool useExpansionLoc = true; | ||||||||||||
73 | switch (attr.getKind()) { | ||||||||||||
74 | case ParsedAttr::AT_ObjCGC: | ||||||||||||
75 | WhichType = TDS_Pointer; | ||||||||||||
76 | break; | ||||||||||||
77 | case ParsedAttr::AT_ObjCOwnership: | ||||||||||||
78 | WhichType = TDS_ObjCObjOrBlock; | ||||||||||||
79 | break; | ||||||||||||
80 | default: | ||||||||||||
81 | // Assume everything else was a function attribute. | ||||||||||||
82 | WhichType = TDS_Function; | ||||||||||||
83 | useExpansionLoc = false; | ||||||||||||
84 | break; | ||||||||||||
85 | } | ||||||||||||
86 | |||||||||||||
87 | SourceLocation loc = attr.getLoc(); | ||||||||||||
88 | StringRef name = attr.getAttrName()->getName(); | ||||||||||||
89 | |||||||||||||
90 | // The GC attributes are usually written with macros; special-case them. | ||||||||||||
91 | IdentifierInfo *II = attr.isArgIdent(0) ? attr.getArgAsIdent(0)->Ident | ||||||||||||
92 | : nullptr; | ||||||||||||
93 | if (useExpansionLoc && loc.isMacroID() && II) { | ||||||||||||
94 | if (II->isStr("strong")) { | ||||||||||||
95 | if (S.findMacroSpelling(loc, "__strong")) name = "__strong"; | ||||||||||||
96 | } else if (II->isStr("weak")) { | ||||||||||||
97 | if (S.findMacroSpelling(loc, "__weak")) name = "__weak"; | ||||||||||||
98 | } | ||||||||||||
99 | } | ||||||||||||
100 | |||||||||||||
101 | S.Diag(loc, diag::warn_type_attribute_wrong_type) << name << WhichType | ||||||||||||
102 | << type; | ||||||||||||
103 | } | ||||||||||||
104 | |||||||||||||
105 | // objc_gc applies to Objective-C pointers or, otherwise, to the | ||||||||||||
106 | // smallest available pointer type (i.e. 'void*' in 'void**'). | ||||||||||||
107 | #define OBJC_POINTER_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_ObjCGC: case ParsedAttr::AT_ObjCOwnership \ | ||||||||||||
108 | case ParsedAttr::AT_ObjCGC: \ | ||||||||||||
109 | case ParsedAttr::AT_ObjCOwnership | ||||||||||||
110 | |||||||||||||
111 | // Calling convention attributes. | ||||||||||||
112 | #define CALLING_CONV_ATTRS_CASELISTcase ParsedAttr::AT_CDecl: case ParsedAttr::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr::AT_ThisCall: case ParsedAttr ::AT_RegCall: case ParsedAttr::AT_Pascal: case ParsedAttr::AT_SwiftCall : case ParsedAttr::AT_VectorCall: case ParsedAttr::AT_AArch64VectorPcs : case ParsedAttr::AT_MSABI: case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr::AT_IntelOclBicc: case ParsedAttr ::AT_PreserveMost: case ParsedAttr::AT_PreserveAll \ | ||||||||||||
113 | case ParsedAttr::AT_CDecl: \ | ||||||||||||
114 | case ParsedAttr::AT_FastCall: \ | ||||||||||||
115 | case ParsedAttr::AT_StdCall: \ | ||||||||||||
116 | case ParsedAttr::AT_ThisCall: \ | ||||||||||||
117 | case ParsedAttr::AT_RegCall: \ | ||||||||||||
118 | case ParsedAttr::AT_Pascal: \ | ||||||||||||
119 | case ParsedAttr::AT_SwiftCall: \ | ||||||||||||
120 | case ParsedAttr::AT_VectorCall: \ | ||||||||||||
121 | case ParsedAttr::AT_AArch64VectorPcs: \ | ||||||||||||
122 | case ParsedAttr::AT_MSABI: \ | ||||||||||||
123 | case ParsedAttr::AT_SysVABI: \ | ||||||||||||
124 | case ParsedAttr::AT_Pcs: \ | ||||||||||||
125 | case ParsedAttr::AT_IntelOclBicc: \ | ||||||||||||
126 | case ParsedAttr::AT_PreserveMost: \ | ||||||||||||
127 | case ParsedAttr::AT_PreserveAll | ||||||||||||
128 | |||||||||||||
129 | // Function type attributes. | ||||||||||||
130 | #define FUNCTION_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_NSReturnsRetained: case ParsedAttr::AT_NoReturn : case ParsedAttr::AT_Regparm: case ParsedAttr::AT_CmseNSCall : case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: case ParsedAttr ::AT_AnyX86NoCfCheck: case ParsedAttr::AT_CDecl: case ParsedAttr ::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr:: AT_ThisCall: case ParsedAttr::AT_RegCall: case ParsedAttr::AT_Pascal : case ParsedAttr::AT_SwiftCall: case ParsedAttr::AT_VectorCall : case ParsedAttr::AT_AArch64VectorPcs: case ParsedAttr::AT_MSABI : case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr ::AT_IntelOclBicc: case ParsedAttr::AT_PreserveMost: case ParsedAttr ::AT_PreserveAll \ | ||||||||||||
131 | case ParsedAttr::AT_NSReturnsRetained: \ | ||||||||||||
132 | case ParsedAttr::AT_NoReturn: \ | ||||||||||||
133 | case ParsedAttr::AT_Regparm: \ | ||||||||||||
134 | case ParsedAttr::AT_CmseNSCall: \ | ||||||||||||
135 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \ | ||||||||||||
136 | case ParsedAttr::AT_AnyX86NoCfCheck: \ | ||||||||||||
137 | CALLING_CONV_ATTRS_CASELISTcase ParsedAttr::AT_CDecl: case ParsedAttr::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr::AT_ThisCall: case ParsedAttr ::AT_RegCall: case ParsedAttr::AT_Pascal: case ParsedAttr::AT_SwiftCall : case ParsedAttr::AT_VectorCall: case ParsedAttr::AT_AArch64VectorPcs : case ParsedAttr::AT_MSABI: case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr::AT_IntelOclBicc: case ParsedAttr ::AT_PreserveMost: case ParsedAttr::AT_PreserveAll | ||||||||||||
138 | |||||||||||||
139 | // Microsoft-specific type qualifiers. | ||||||||||||
140 | #define MS_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_Ptr32: case ParsedAttr::AT_Ptr64: case ParsedAttr ::AT_SPtr: case ParsedAttr::AT_UPtr \ | ||||||||||||
141 | case ParsedAttr::AT_Ptr32: \ | ||||||||||||
142 | case ParsedAttr::AT_Ptr64: \ | ||||||||||||
143 | case ParsedAttr::AT_SPtr: \ | ||||||||||||
144 | case ParsedAttr::AT_UPtr | ||||||||||||
145 | |||||||||||||
146 | // Nullability qualifiers. | ||||||||||||
147 | #define NULLABILITY_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_TypeNonNull: case ParsedAttr::AT_TypeNullable : case ParsedAttr::AT_TypeNullableResult: case ParsedAttr::AT_TypeNullUnspecified \ | ||||||||||||
148 | case ParsedAttr::AT_TypeNonNull: \ | ||||||||||||
149 | case ParsedAttr::AT_TypeNullable: \ | ||||||||||||
150 | case ParsedAttr::AT_TypeNullableResult: \ | ||||||||||||
151 | case ParsedAttr::AT_TypeNullUnspecified | ||||||||||||
152 | |||||||||||||
153 | namespace { | ||||||||||||
154 | /// An object which stores processing state for the entire | ||||||||||||
155 | /// GetTypeForDeclarator process. | ||||||||||||
156 | class TypeProcessingState { | ||||||||||||
157 | Sema &sema; | ||||||||||||
158 | |||||||||||||
159 | /// The declarator being processed. | ||||||||||||
160 | Declarator &declarator; | ||||||||||||
161 | |||||||||||||
162 | /// The index of the declarator chunk we're currently processing. | ||||||||||||
163 | /// May be the total number of valid chunks, indicating the | ||||||||||||
164 | /// DeclSpec. | ||||||||||||
165 | unsigned chunkIndex; | ||||||||||||
166 | |||||||||||||
167 | /// Whether there are non-trivial modifications to the decl spec. | ||||||||||||
168 | bool trivial; | ||||||||||||
169 | |||||||||||||
170 | /// Whether we saved the attributes in the decl spec. | ||||||||||||
171 | bool hasSavedAttrs; | ||||||||||||
172 | |||||||||||||
173 | /// The original set of attributes on the DeclSpec. | ||||||||||||
174 | SmallVector<ParsedAttr *, 2> savedAttrs; | ||||||||||||
175 | |||||||||||||
176 | /// A list of attributes to diagnose the uselessness of when the | ||||||||||||
177 | /// processing is complete. | ||||||||||||
178 | SmallVector<ParsedAttr *, 2> ignoredTypeAttrs; | ||||||||||||
179 | |||||||||||||
180 | /// Attributes corresponding to AttributedTypeLocs that we have not yet | ||||||||||||
181 | /// populated. | ||||||||||||
182 | // FIXME: The two-phase mechanism by which we construct Types and fill | ||||||||||||
183 | // their TypeLocs makes it hard to correctly assign these. We keep the | ||||||||||||
184 | // attributes in creation order as an attempt to make them line up | ||||||||||||
185 | // properly. | ||||||||||||
186 | using TypeAttrPair = std::pair<const AttributedType*, const Attr*>; | ||||||||||||
187 | SmallVector<TypeAttrPair, 8> AttrsForTypes; | ||||||||||||
188 | bool AttrsForTypesSorted = true; | ||||||||||||
189 | |||||||||||||
190 | /// MacroQualifiedTypes mapping to macro expansion locations that will be | ||||||||||||
191 | /// stored in a MacroQualifiedTypeLoc. | ||||||||||||
192 | llvm::DenseMap<const MacroQualifiedType *, SourceLocation> LocsForMacros; | ||||||||||||
193 | |||||||||||||
194 | /// Flag to indicate we parsed a noderef attribute. This is used for | ||||||||||||
195 | /// validating that noderef was used on a pointer or array. | ||||||||||||
196 | bool parsedNoDeref; | ||||||||||||
197 | |||||||||||||
198 | public: | ||||||||||||
199 | TypeProcessingState(Sema &sema, Declarator &declarator) | ||||||||||||
200 | : sema(sema), declarator(declarator), | ||||||||||||
201 | chunkIndex(declarator.getNumTypeObjects()), trivial(true), | ||||||||||||
202 | hasSavedAttrs(false), parsedNoDeref(false) {} | ||||||||||||
203 | |||||||||||||
204 | Sema &getSema() const { | ||||||||||||
205 | return sema; | ||||||||||||
206 | } | ||||||||||||
207 | |||||||||||||
208 | Declarator &getDeclarator() const { | ||||||||||||
209 | return declarator; | ||||||||||||
210 | } | ||||||||||||
211 | |||||||||||||
212 | bool isProcessingDeclSpec() const { | ||||||||||||
213 | return chunkIndex == declarator.getNumTypeObjects(); | ||||||||||||
214 | } | ||||||||||||
215 | |||||||||||||
216 | unsigned getCurrentChunkIndex() const { | ||||||||||||
217 | return chunkIndex; | ||||||||||||
218 | } | ||||||||||||
219 | |||||||||||||
220 | void setCurrentChunkIndex(unsigned idx) { | ||||||||||||
221 | assert(idx <= declarator.getNumTypeObjects())((idx <= declarator.getNumTypeObjects()) ? static_cast< void> (0) : __assert_fail ("idx <= declarator.getNumTypeObjects()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 221, __PRETTY_FUNCTION__)); | ||||||||||||
222 | chunkIndex = idx; | ||||||||||||
223 | } | ||||||||||||
224 | |||||||||||||
225 | ParsedAttributesView &getCurrentAttributes() const { | ||||||||||||
226 | if (isProcessingDeclSpec()) | ||||||||||||
227 | return getMutableDeclSpec().getAttributes(); | ||||||||||||
228 | return declarator.getTypeObject(chunkIndex).getAttrs(); | ||||||||||||
229 | } | ||||||||||||
230 | |||||||||||||
231 | /// Save the current set of attributes on the DeclSpec. | ||||||||||||
232 | void saveDeclSpecAttrs() { | ||||||||||||
233 | // Don't try to save them multiple times. | ||||||||||||
234 | if (hasSavedAttrs) return; | ||||||||||||
235 | |||||||||||||
236 | DeclSpec &spec = getMutableDeclSpec(); | ||||||||||||
237 | for (ParsedAttr &AL : spec.getAttributes()) | ||||||||||||
238 | savedAttrs.push_back(&AL); | ||||||||||||
239 | trivial &= savedAttrs.empty(); | ||||||||||||
240 | hasSavedAttrs = true; | ||||||||||||
241 | } | ||||||||||||
242 | |||||||||||||
243 | /// Record that we had nowhere to put the given type attribute. | ||||||||||||
244 | /// We will diagnose such attributes later. | ||||||||||||
245 | void addIgnoredTypeAttr(ParsedAttr &attr) { | ||||||||||||
246 | ignoredTypeAttrs.push_back(&attr); | ||||||||||||
247 | } | ||||||||||||
248 | |||||||||||||
249 | /// Diagnose all the ignored type attributes, given that the | ||||||||||||
250 | /// declarator worked out to the given type. | ||||||||||||
251 | void diagnoseIgnoredTypeAttrs(QualType type) const { | ||||||||||||
252 | for (auto *Attr : ignoredTypeAttrs) | ||||||||||||
253 | diagnoseBadTypeAttribute(getSema(), *Attr, type); | ||||||||||||
254 | } | ||||||||||||
255 | |||||||||||||
256 | /// Get an attributed type for the given attribute, and remember the Attr | ||||||||||||
257 | /// object so that we can attach it to the AttributedTypeLoc. | ||||||||||||
258 | QualType getAttributedType(Attr *A, QualType ModifiedType, | ||||||||||||
259 | QualType EquivType) { | ||||||||||||
260 | QualType T = | ||||||||||||
261 | sema.Context.getAttributedType(A->getKind(), ModifiedType, EquivType); | ||||||||||||
262 | AttrsForTypes.push_back({cast<AttributedType>(T.getTypePtr()), A}); | ||||||||||||
263 | AttrsForTypesSorted = false; | ||||||||||||
264 | return T; | ||||||||||||
265 | } | ||||||||||||
266 | |||||||||||||
267 | /// Completely replace the \c auto in \p TypeWithAuto by | ||||||||||||
268 | /// \p Replacement. Also replace \p TypeWithAuto in \c TypeAttrPair if | ||||||||||||
269 | /// necessary. | ||||||||||||
270 | QualType ReplaceAutoType(QualType TypeWithAuto, QualType Replacement) { | ||||||||||||
271 | QualType T = sema.ReplaceAutoType(TypeWithAuto, Replacement); | ||||||||||||
272 | if (auto *AttrTy = TypeWithAuto->getAs<AttributedType>()) { | ||||||||||||
273 | // Attributed type still should be an attributed type after replacement. | ||||||||||||
274 | auto *NewAttrTy = cast<AttributedType>(T.getTypePtr()); | ||||||||||||
275 | for (TypeAttrPair &A : AttrsForTypes) { | ||||||||||||
276 | if (A.first == AttrTy) | ||||||||||||
277 | A.first = NewAttrTy; | ||||||||||||
278 | } | ||||||||||||
279 | AttrsForTypesSorted = false; | ||||||||||||
280 | } | ||||||||||||
281 | return T; | ||||||||||||
282 | } | ||||||||||||
283 | |||||||||||||
284 | /// Extract and remove the Attr* for a given attributed type. | ||||||||||||
285 | const Attr *takeAttrForAttributedType(const AttributedType *AT) { | ||||||||||||
286 | if (!AttrsForTypesSorted) { | ||||||||||||
287 | llvm::stable_sort(AttrsForTypes, llvm::less_first()); | ||||||||||||
288 | AttrsForTypesSorted = true; | ||||||||||||
289 | } | ||||||||||||
290 | |||||||||||||
291 | // FIXME: This is quadratic if we have lots of reuses of the same | ||||||||||||
292 | // attributed type. | ||||||||||||
293 | for (auto It = std::partition_point( | ||||||||||||
294 | AttrsForTypes.begin(), AttrsForTypes.end(), | ||||||||||||
295 | [=](const TypeAttrPair &A) { return A.first < AT; }); | ||||||||||||
296 | It != AttrsForTypes.end() && It->first == AT; ++It) { | ||||||||||||
297 | if (It->second) { | ||||||||||||
298 | const Attr *Result = It->second; | ||||||||||||
299 | It->second = nullptr; | ||||||||||||
300 | return Result; | ||||||||||||
301 | } | ||||||||||||
302 | } | ||||||||||||
303 | |||||||||||||
304 | llvm_unreachable("no Attr* for AttributedType*")::llvm::llvm_unreachable_internal("no Attr* for AttributedType*" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 304); | ||||||||||||
305 | } | ||||||||||||
306 | |||||||||||||
307 | SourceLocation | ||||||||||||
308 | getExpansionLocForMacroQualifiedType(const MacroQualifiedType *MQT) const { | ||||||||||||
309 | auto FoundLoc = LocsForMacros.find(MQT); | ||||||||||||
310 | assert(FoundLoc != LocsForMacros.end() &&((FoundLoc != LocsForMacros.end() && "Unable to find macro expansion location for MacroQualifedType" ) ? static_cast<void> (0) : __assert_fail ("FoundLoc != LocsForMacros.end() && \"Unable to find macro expansion location for MacroQualifedType\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 311, __PRETTY_FUNCTION__)) | ||||||||||||
311 | "Unable to find macro expansion location for MacroQualifedType")((FoundLoc != LocsForMacros.end() && "Unable to find macro expansion location for MacroQualifedType" ) ? static_cast<void> (0) : __assert_fail ("FoundLoc != LocsForMacros.end() && \"Unable to find macro expansion location for MacroQualifedType\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 311, __PRETTY_FUNCTION__)); | ||||||||||||
312 | return FoundLoc->second; | ||||||||||||
313 | } | ||||||||||||
314 | |||||||||||||
315 | void setExpansionLocForMacroQualifiedType(const MacroQualifiedType *MQT, | ||||||||||||
316 | SourceLocation Loc) { | ||||||||||||
317 | LocsForMacros[MQT] = Loc; | ||||||||||||
318 | } | ||||||||||||
319 | |||||||||||||
320 | void setParsedNoDeref(bool parsed) { parsedNoDeref = parsed; } | ||||||||||||
321 | |||||||||||||
322 | bool didParseNoDeref() const { return parsedNoDeref; } | ||||||||||||
323 | |||||||||||||
324 | ~TypeProcessingState() { | ||||||||||||
325 | if (trivial) return; | ||||||||||||
326 | |||||||||||||
327 | restoreDeclSpecAttrs(); | ||||||||||||
328 | } | ||||||||||||
329 | |||||||||||||
330 | private: | ||||||||||||
331 | DeclSpec &getMutableDeclSpec() const { | ||||||||||||
332 | return const_cast<DeclSpec&>(declarator.getDeclSpec()); | ||||||||||||
333 | } | ||||||||||||
334 | |||||||||||||
335 | void restoreDeclSpecAttrs() { | ||||||||||||
336 | assert(hasSavedAttrs)((hasSavedAttrs) ? static_cast<void> (0) : __assert_fail ("hasSavedAttrs", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 336, __PRETTY_FUNCTION__)); | ||||||||||||
337 | |||||||||||||
338 | getMutableDeclSpec().getAttributes().clearListOnly(); | ||||||||||||
339 | for (ParsedAttr *AL : savedAttrs) | ||||||||||||
340 | getMutableDeclSpec().getAttributes().addAtEnd(AL); | ||||||||||||
341 | } | ||||||||||||
342 | }; | ||||||||||||
343 | } // end anonymous namespace | ||||||||||||
344 | |||||||||||||
345 | static void moveAttrFromListToList(ParsedAttr &attr, | ||||||||||||
346 | ParsedAttributesView &fromList, | ||||||||||||
347 | ParsedAttributesView &toList) { | ||||||||||||
348 | fromList.remove(&attr); | ||||||||||||
349 | toList.addAtEnd(&attr); | ||||||||||||
350 | } | ||||||||||||
351 | |||||||||||||
352 | /// The location of a type attribute. | ||||||||||||
353 | enum TypeAttrLocation { | ||||||||||||
354 | /// The attribute is in the decl-specifier-seq. | ||||||||||||
355 | TAL_DeclSpec, | ||||||||||||
356 | /// The attribute is part of a DeclaratorChunk. | ||||||||||||
357 | TAL_DeclChunk, | ||||||||||||
358 | /// The attribute is immediately after the declaration's name. | ||||||||||||
359 | TAL_DeclName | ||||||||||||
360 | }; | ||||||||||||
361 | |||||||||||||
362 | static void processTypeAttrs(TypeProcessingState &state, QualType &type, | ||||||||||||
363 | TypeAttrLocation TAL, ParsedAttributesView &attrs); | ||||||||||||
364 | |||||||||||||
365 | static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, | ||||||||||||
366 | QualType &type); | ||||||||||||
367 | |||||||||||||
368 | static bool handleMSPointerTypeQualifierAttr(TypeProcessingState &state, | ||||||||||||
369 | ParsedAttr &attr, QualType &type); | ||||||||||||
370 | |||||||||||||
371 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, ParsedAttr &attr, | ||||||||||||
372 | QualType &type); | ||||||||||||
373 | |||||||||||||
374 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, | ||||||||||||
375 | ParsedAttr &attr, QualType &type); | ||||||||||||
376 | |||||||||||||
377 | static bool handleObjCPointerTypeAttr(TypeProcessingState &state, | ||||||||||||
378 | ParsedAttr &attr, QualType &type) { | ||||||||||||
379 | if (attr.getKind() == ParsedAttr::AT_ObjCGC) | ||||||||||||
380 | return handleObjCGCTypeAttr(state, attr, type); | ||||||||||||
381 | assert(attr.getKind() == ParsedAttr::AT_ObjCOwnership)((attr.getKind() == ParsedAttr::AT_ObjCOwnership) ? static_cast <void> (0) : __assert_fail ("attr.getKind() == ParsedAttr::AT_ObjCOwnership" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 381, __PRETTY_FUNCTION__)); | ||||||||||||
382 | return handleObjCOwnershipTypeAttr(state, attr, type); | ||||||||||||
383 | } | ||||||||||||
384 | |||||||||||||
385 | /// Given the index of a declarator chunk, check whether that chunk | ||||||||||||
386 | /// directly specifies the return type of a function and, if so, find | ||||||||||||
387 | /// an appropriate place for it. | ||||||||||||
388 | /// | ||||||||||||
389 | /// \param i - a notional index which the search will start | ||||||||||||
390 | /// immediately inside | ||||||||||||
391 | /// | ||||||||||||
392 | /// \param onlyBlockPointers Whether we should only look into block | ||||||||||||
393 | /// pointer types (vs. all pointer types). | ||||||||||||
394 | static DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator, | ||||||||||||
395 | unsigned i, | ||||||||||||
396 | bool onlyBlockPointers) { | ||||||||||||
397 | assert(i <= declarator.getNumTypeObjects())((i <= declarator.getNumTypeObjects()) ? static_cast<void > (0) : __assert_fail ("i <= declarator.getNumTypeObjects()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 397, __PRETTY_FUNCTION__)); | ||||||||||||
398 | |||||||||||||
399 | DeclaratorChunk *result = nullptr; | ||||||||||||
400 | |||||||||||||
401 | // First, look inwards past parens for a function declarator. | ||||||||||||
402 | for (; i != 0; --i) { | ||||||||||||
403 | DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1); | ||||||||||||
404 | switch (fnChunk.Kind) { | ||||||||||||
405 | case DeclaratorChunk::Paren: | ||||||||||||
406 | continue; | ||||||||||||
407 | |||||||||||||
408 | // If we find anything except a function, bail out. | ||||||||||||
409 | case DeclaratorChunk::Pointer: | ||||||||||||
410 | case DeclaratorChunk::BlockPointer: | ||||||||||||
411 | case DeclaratorChunk::Array: | ||||||||||||
412 | case DeclaratorChunk::Reference: | ||||||||||||
413 | case DeclaratorChunk::MemberPointer: | ||||||||||||
414 | case DeclaratorChunk::Pipe: | ||||||||||||
415 | return result; | ||||||||||||
416 | |||||||||||||
417 | // If we do find a function declarator, scan inwards from that, | ||||||||||||
418 | // looking for a (block-)pointer declarator. | ||||||||||||
419 | case DeclaratorChunk::Function: | ||||||||||||
420 | for (--i; i != 0; --i) { | ||||||||||||
421 | DeclaratorChunk &ptrChunk = declarator.getTypeObject(i-1); | ||||||||||||
422 | switch (ptrChunk.Kind) { | ||||||||||||
423 | case DeclaratorChunk::Paren: | ||||||||||||
424 | case DeclaratorChunk::Array: | ||||||||||||
425 | case DeclaratorChunk::Function: | ||||||||||||
426 | case DeclaratorChunk::Reference: | ||||||||||||
427 | case DeclaratorChunk::Pipe: | ||||||||||||
428 | continue; | ||||||||||||
429 | |||||||||||||
430 | case DeclaratorChunk::MemberPointer: | ||||||||||||
431 | case DeclaratorChunk::Pointer: | ||||||||||||
432 | if (onlyBlockPointers) | ||||||||||||
433 | continue; | ||||||||||||
434 | |||||||||||||
435 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
436 | |||||||||||||
437 | case DeclaratorChunk::BlockPointer: | ||||||||||||
438 | result = &ptrChunk; | ||||||||||||
439 | goto continue_outer; | ||||||||||||
440 | } | ||||||||||||
441 | llvm_unreachable("bad declarator chunk kind")::llvm::llvm_unreachable_internal("bad declarator chunk kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 441); | ||||||||||||
442 | } | ||||||||||||
443 | |||||||||||||
444 | // If we run out of declarators doing that, we're done. | ||||||||||||
445 | return result; | ||||||||||||
446 | } | ||||||||||||
447 | llvm_unreachable("bad declarator chunk kind")::llvm::llvm_unreachable_internal("bad declarator chunk kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 447); | ||||||||||||
448 | |||||||||||||
449 | // Okay, reconsider from our new point. | ||||||||||||
450 | continue_outer: ; | ||||||||||||
451 | } | ||||||||||||
452 | |||||||||||||
453 | // Ran out of chunks, bail out. | ||||||||||||
454 | return result; | ||||||||||||
455 | } | ||||||||||||
456 | |||||||||||||
457 | /// Given that an objc_gc attribute was written somewhere on a | ||||||||||||
458 | /// declaration *other* than on the declarator itself (for which, use | ||||||||||||
459 | /// distributeObjCPointerTypeAttrFromDeclarator), and given that it | ||||||||||||
460 | /// didn't apply in whatever position it was written in, try to move | ||||||||||||
461 | /// it to a more appropriate position. | ||||||||||||
462 | static void distributeObjCPointerTypeAttr(TypeProcessingState &state, | ||||||||||||
463 | ParsedAttr &attr, QualType type) { | ||||||||||||
464 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
465 | |||||||||||||
466 | // Move it to the outermost normal or block pointer declarator. | ||||||||||||
467 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { | ||||||||||||
468 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); | ||||||||||||
469 | switch (chunk.Kind) { | ||||||||||||
470 | case DeclaratorChunk::Pointer: | ||||||||||||
471 | case DeclaratorChunk::BlockPointer: { | ||||||||||||
472 | // But don't move an ARC ownership attribute to the return type | ||||||||||||
473 | // of a block. | ||||||||||||
474 | DeclaratorChunk *destChunk = nullptr; | ||||||||||||
475 | if (state.isProcessingDeclSpec() && | ||||||||||||
476 | attr.getKind() == ParsedAttr::AT_ObjCOwnership) | ||||||||||||
477 | destChunk = maybeMovePastReturnType(declarator, i - 1, | ||||||||||||
478 | /*onlyBlockPointers=*/true); | ||||||||||||
479 | if (!destChunk) destChunk = &chunk; | ||||||||||||
480 | |||||||||||||
481 | moveAttrFromListToList(attr, state.getCurrentAttributes(), | ||||||||||||
482 | destChunk->getAttrs()); | ||||||||||||
483 | return; | ||||||||||||
484 | } | ||||||||||||
485 | |||||||||||||
486 | case DeclaratorChunk::Paren: | ||||||||||||
487 | case DeclaratorChunk::Array: | ||||||||||||
488 | continue; | ||||||||||||
489 | |||||||||||||
490 | // We may be starting at the return type of a block. | ||||||||||||
491 | case DeclaratorChunk::Function: | ||||||||||||
492 | if (state.isProcessingDeclSpec() && | ||||||||||||
493 | attr.getKind() == ParsedAttr::AT_ObjCOwnership) { | ||||||||||||
494 | if (DeclaratorChunk *dest = maybeMovePastReturnType( | ||||||||||||
495 | declarator, i, | ||||||||||||
496 | /*onlyBlockPointers=*/true)) { | ||||||||||||
497 | moveAttrFromListToList(attr, state.getCurrentAttributes(), | ||||||||||||
498 | dest->getAttrs()); | ||||||||||||
499 | return; | ||||||||||||
500 | } | ||||||||||||
501 | } | ||||||||||||
502 | goto error; | ||||||||||||
503 | |||||||||||||
504 | // Don't walk through these. | ||||||||||||
505 | case DeclaratorChunk::Reference: | ||||||||||||
506 | case DeclaratorChunk::MemberPointer: | ||||||||||||
507 | case DeclaratorChunk::Pipe: | ||||||||||||
508 | goto error; | ||||||||||||
509 | } | ||||||||||||
510 | } | ||||||||||||
511 | error: | ||||||||||||
512 | |||||||||||||
513 | diagnoseBadTypeAttribute(state.getSema(), attr, type); | ||||||||||||
514 | } | ||||||||||||
515 | |||||||||||||
516 | /// Distribute an objc_gc type attribute that was written on the | ||||||||||||
517 | /// declarator. | ||||||||||||
518 | static void distributeObjCPointerTypeAttrFromDeclarator( | ||||||||||||
519 | TypeProcessingState &state, ParsedAttr &attr, QualType &declSpecType) { | ||||||||||||
520 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
521 | |||||||||||||
522 | // objc_gc goes on the innermost pointer to something that's not a | ||||||||||||
523 | // pointer. | ||||||||||||
524 | unsigned innermost = -1U; | ||||||||||||
525 | bool considerDeclSpec = true; | ||||||||||||
526 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
527 | DeclaratorChunk &chunk = declarator.getTypeObject(i); | ||||||||||||
528 | switch (chunk.Kind) { | ||||||||||||
529 | case DeclaratorChunk::Pointer: | ||||||||||||
530 | case DeclaratorChunk::BlockPointer: | ||||||||||||
531 | innermost = i; | ||||||||||||
532 | continue; | ||||||||||||
533 | |||||||||||||
534 | case DeclaratorChunk::Reference: | ||||||||||||
535 | case DeclaratorChunk::MemberPointer: | ||||||||||||
536 | case DeclaratorChunk::Paren: | ||||||||||||
537 | case DeclaratorChunk::Array: | ||||||||||||
538 | case DeclaratorChunk::Pipe: | ||||||||||||
539 | continue; | ||||||||||||
540 | |||||||||||||
541 | case DeclaratorChunk::Function: | ||||||||||||
542 | considerDeclSpec = false; | ||||||||||||
543 | goto done; | ||||||||||||
544 | } | ||||||||||||
545 | } | ||||||||||||
546 | done: | ||||||||||||
547 | |||||||||||||
548 | // That might actually be the decl spec if we weren't blocked by | ||||||||||||
549 | // anything in the declarator. | ||||||||||||
550 | if (considerDeclSpec) { | ||||||||||||
551 | if (handleObjCPointerTypeAttr(state, attr, declSpecType)) { | ||||||||||||
552 | // Splice the attribute into the decl spec. Prevents the | ||||||||||||
553 | // attribute from being applied multiple times and gives | ||||||||||||
554 | // the source-location-filler something to work with. | ||||||||||||
555 | state.saveDeclSpecAttrs(); | ||||||||||||
556 | declarator.getMutableDeclSpec().getAttributes().takeOneFrom( | ||||||||||||
557 | declarator.getAttributes(), &attr); | ||||||||||||
558 | return; | ||||||||||||
559 | } | ||||||||||||
560 | } | ||||||||||||
561 | |||||||||||||
562 | // Otherwise, if we found an appropriate chunk, splice the attribute | ||||||||||||
563 | // into it. | ||||||||||||
564 | if (innermost != -1U) { | ||||||||||||
565 | moveAttrFromListToList(attr, declarator.getAttributes(), | ||||||||||||
566 | declarator.getTypeObject(innermost).getAttrs()); | ||||||||||||
567 | return; | ||||||||||||
568 | } | ||||||||||||
569 | |||||||||||||
570 | // Otherwise, diagnose when we're done building the type. | ||||||||||||
571 | declarator.getAttributes().remove(&attr); | ||||||||||||
572 | state.addIgnoredTypeAttr(attr); | ||||||||||||
573 | } | ||||||||||||
574 | |||||||||||||
575 | /// A function type attribute was written somewhere in a declaration | ||||||||||||
576 | /// *other* than on the declarator itself or in the decl spec. Given | ||||||||||||
577 | /// that it didn't apply in whatever position it was written in, try | ||||||||||||
578 | /// to move it to a more appropriate position. | ||||||||||||
579 | static void distributeFunctionTypeAttr(TypeProcessingState &state, | ||||||||||||
580 | ParsedAttr &attr, QualType type) { | ||||||||||||
581 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
582 | |||||||||||||
583 | // Try to push the attribute from the return type of a function to | ||||||||||||
584 | // the function itself. | ||||||||||||
585 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { | ||||||||||||
586 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); | ||||||||||||
587 | switch (chunk.Kind) { | ||||||||||||
588 | case DeclaratorChunk::Function: | ||||||||||||
589 | moveAttrFromListToList(attr, state.getCurrentAttributes(), | ||||||||||||
590 | chunk.getAttrs()); | ||||||||||||
591 | return; | ||||||||||||
592 | |||||||||||||
593 | case DeclaratorChunk::Paren: | ||||||||||||
594 | case DeclaratorChunk::Pointer: | ||||||||||||
595 | case DeclaratorChunk::BlockPointer: | ||||||||||||
596 | case DeclaratorChunk::Array: | ||||||||||||
597 | case DeclaratorChunk::Reference: | ||||||||||||
598 | case DeclaratorChunk::MemberPointer: | ||||||||||||
599 | case DeclaratorChunk::Pipe: | ||||||||||||
600 | continue; | ||||||||||||
601 | } | ||||||||||||
602 | } | ||||||||||||
603 | |||||||||||||
604 | diagnoseBadTypeAttribute(state.getSema(), attr, type); | ||||||||||||
605 | } | ||||||||||||
606 | |||||||||||||
607 | /// Try to distribute a function type attribute to the innermost | ||||||||||||
608 | /// function chunk or type. Returns true if the attribute was | ||||||||||||
609 | /// distributed, false if no location was found. | ||||||||||||
610 | static bool distributeFunctionTypeAttrToInnermost( | ||||||||||||
611 | TypeProcessingState &state, ParsedAttr &attr, | ||||||||||||
612 | ParsedAttributesView &attrList, QualType &declSpecType) { | ||||||||||||
613 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
614 | |||||||||||||
615 | // Put it on the innermost function chunk, if there is one. | ||||||||||||
616 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
617 | DeclaratorChunk &chunk = declarator.getTypeObject(i); | ||||||||||||
618 | if (chunk.Kind != DeclaratorChunk::Function) continue; | ||||||||||||
619 | |||||||||||||
620 | moveAttrFromListToList(attr, attrList, chunk.getAttrs()); | ||||||||||||
621 | return true; | ||||||||||||
622 | } | ||||||||||||
623 | |||||||||||||
624 | return handleFunctionTypeAttr(state, attr, declSpecType); | ||||||||||||
625 | } | ||||||||||||
626 | |||||||||||||
627 | /// A function type attribute was written in the decl spec. Try to | ||||||||||||
628 | /// apply it somewhere. | ||||||||||||
629 | static void distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state, | ||||||||||||
630 | ParsedAttr &attr, | ||||||||||||
631 | QualType &declSpecType) { | ||||||||||||
632 | state.saveDeclSpecAttrs(); | ||||||||||||
633 | |||||||||||||
634 | // C++11 attributes before the decl specifiers actually appertain to | ||||||||||||
635 | // the declarators. Move them straight there. We don't support the | ||||||||||||
636 | // 'put them wherever you like' semantics we allow for GNU attributes. | ||||||||||||
637 | if (attr.isCXX11Attribute()) { | ||||||||||||
638 | moveAttrFromListToList(attr, state.getCurrentAttributes(), | ||||||||||||
639 | state.getDeclarator().getAttributes()); | ||||||||||||
640 | return; | ||||||||||||
641 | } | ||||||||||||
642 | |||||||||||||
643 | // Try to distribute to the innermost. | ||||||||||||
644 | if (distributeFunctionTypeAttrToInnermost( | ||||||||||||
645 | state, attr, state.getCurrentAttributes(), declSpecType)) | ||||||||||||
646 | return; | ||||||||||||
647 | |||||||||||||
648 | // If that failed, diagnose the bad attribute when the declarator is | ||||||||||||
649 | // fully built. | ||||||||||||
650 | state.addIgnoredTypeAttr(attr); | ||||||||||||
651 | } | ||||||||||||
652 | |||||||||||||
653 | /// A function type attribute was written on the declarator. Try to | ||||||||||||
654 | /// apply it somewhere. | ||||||||||||
655 | static void distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state, | ||||||||||||
656 | ParsedAttr &attr, | ||||||||||||
657 | QualType &declSpecType) { | ||||||||||||
658 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
659 | |||||||||||||
660 | // Try to distribute to the innermost. | ||||||||||||
661 | if (distributeFunctionTypeAttrToInnermost( | ||||||||||||
662 | state, attr, declarator.getAttributes(), declSpecType)) | ||||||||||||
663 | return; | ||||||||||||
664 | |||||||||||||
665 | // If that failed, diagnose the bad attribute when the declarator is | ||||||||||||
666 | // fully built. | ||||||||||||
667 | declarator.getAttributes().remove(&attr); | ||||||||||||
668 | state.addIgnoredTypeAttr(attr); | ||||||||||||
669 | } | ||||||||||||
670 | |||||||||||||
671 | /// Given that there are attributes written on the declarator | ||||||||||||
672 | /// itself, try to distribute any type attributes to the appropriate | ||||||||||||
673 | /// declarator chunk. | ||||||||||||
674 | /// | ||||||||||||
675 | /// These are attributes like the following: | ||||||||||||
676 | /// int f ATTR; | ||||||||||||
677 | /// int (f ATTR)(); | ||||||||||||
678 | /// but not necessarily this: | ||||||||||||
679 | /// int f() ATTR; | ||||||||||||
680 | static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state, | ||||||||||||
681 | QualType &declSpecType) { | ||||||||||||
682 | // Collect all the type attributes from the declarator itself. | ||||||||||||
683 | assert(!state.getDeclarator().getAttributes().empty() &&((!state.getDeclarator().getAttributes().empty() && "declarator has no attrs!" ) ? static_cast<void> (0) : __assert_fail ("!state.getDeclarator().getAttributes().empty() && \"declarator has no attrs!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 684, __PRETTY_FUNCTION__)) | ||||||||||||
684 | "declarator has no attrs!")((!state.getDeclarator().getAttributes().empty() && "declarator has no attrs!" ) ? static_cast<void> (0) : __assert_fail ("!state.getDeclarator().getAttributes().empty() && \"declarator has no attrs!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 684, __PRETTY_FUNCTION__)); | ||||||||||||
685 | // The called functions in this loop actually remove things from the current | ||||||||||||
686 | // list, so iterating over the existing list isn't possible. Instead, make a | ||||||||||||
687 | // non-owning copy and iterate over that. | ||||||||||||
688 | ParsedAttributesView AttrsCopy{state.getDeclarator().getAttributes()}; | ||||||||||||
689 | for (ParsedAttr &attr : AttrsCopy) { | ||||||||||||
690 | // Do not distribute C++11 attributes. They have strict rules for what | ||||||||||||
691 | // they appertain to. | ||||||||||||
692 | if (attr.isCXX11Attribute()) | ||||||||||||
693 | continue; | ||||||||||||
694 | |||||||||||||
695 | switch (attr.getKind()) { | ||||||||||||
696 | OBJC_POINTER_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_ObjCGC: case ParsedAttr::AT_ObjCOwnership: | ||||||||||||
697 | distributeObjCPointerTypeAttrFromDeclarator(state, attr, declSpecType); | ||||||||||||
698 | break; | ||||||||||||
699 | |||||||||||||
700 | FUNCTION_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_NSReturnsRetained: case ParsedAttr::AT_NoReturn : case ParsedAttr::AT_Regparm: case ParsedAttr::AT_CmseNSCall : case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: case ParsedAttr ::AT_AnyX86NoCfCheck: case ParsedAttr::AT_CDecl: case ParsedAttr ::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr:: AT_ThisCall: case ParsedAttr::AT_RegCall: case ParsedAttr::AT_Pascal : case ParsedAttr::AT_SwiftCall: case ParsedAttr::AT_VectorCall : case ParsedAttr::AT_AArch64VectorPcs: case ParsedAttr::AT_MSABI : case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr ::AT_IntelOclBicc: case ParsedAttr::AT_PreserveMost: case ParsedAttr ::AT_PreserveAll: | ||||||||||||
701 | distributeFunctionTypeAttrFromDeclarator(state, attr, declSpecType); | ||||||||||||
702 | break; | ||||||||||||
703 | |||||||||||||
704 | MS_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_Ptr32: case ParsedAttr::AT_Ptr64: case ParsedAttr ::AT_SPtr: case ParsedAttr::AT_UPtr: | ||||||||||||
705 | // Microsoft type attributes cannot go after the declarator-id. | ||||||||||||
706 | continue; | ||||||||||||
707 | |||||||||||||
708 | NULLABILITY_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_TypeNonNull: case ParsedAttr::AT_TypeNullable : case ParsedAttr::AT_TypeNullableResult: case ParsedAttr::AT_TypeNullUnspecified: | ||||||||||||
709 | // Nullability specifiers cannot go after the declarator-id. | ||||||||||||
710 | |||||||||||||
711 | // Objective-C __kindof does not get distributed. | ||||||||||||
712 | case ParsedAttr::AT_ObjCKindOf: | ||||||||||||
713 | continue; | ||||||||||||
714 | |||||||||||||
715 | default: | ||||||||||||
716 | break; | ||||||||||||
717 | } | ||||||||||||
718 | } | ||||||||||||
719 | } | ||||||||||||
720 | |||||||||||||
721 | /// Add a synthetic '()' to a block-literal declarator if it is | ||||||||||||
722 | /// required, given the return type. | ||||||||||||
723 | static void maybeSynthesizeBlockSignature(TypeProcessingState &state, | ||||||||||||
724 | QualType declSpecType) { | ||||||||||||
725 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
726 | |||||||||||||
727 | // First, check whether the declarator would produce a function, | ||||||||||||
728 | // i.e. whether the innermost semantic chunk is a function. | ||||||||||||
729 | if (declarator.isFunctionDeclarator()) { | ||||||||||||
730 | // If so, make that declarator a prototyped declarator. | ||||||||||||
731 | declarator.getFunctionTypeInfo().hasPrototype = true; | ||||||||||||
732 | return; | ||||||||||||
733 | } | ||||||||||||
734 | |||||||||||||
735 | // If there are any type objects, the type as written won't name a | ||||||||||||
736 | // function, regardless of the decl spec type. This is because a | ||||||||||||
737 | // block signature declarator is always an abstract-declarator, and | ||||||||||||
738 | // abstract-declarators can't just be parentheses chunks. Therefore | ||||||||||||
739 | // we need to build a function chunk unless there are no type | ||||||||||||
740 | // objects and the decl spec type is a function. | ||||||||||||
741 | if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType()) | ||||||||||||
742 | return; | ||||||||||||
743 | |||||||||||||
744 | // Note that there *are* cases with invalid declarators where | ||||||||||||
745 | // declarators consist solely of parentheses. In general, these | ||||||||||||
746 | // occur only in failed efforts to make function declarators, so | ||||||||||||
747 | // faking up the function chunk is still the right thing to do. | ||||||||||||
748 | |||||||||||||
749 | // Otherwise, we need to fake up a function declarator. | ||||||||||||
750 | SourceLocation loc = declarator.getBeginLoc(); | ||||||||||||
751 | |||||||||||||
752 | // ...and *prepend* it to the declarator. | ||||||||||||
753 | SourceLocation NoLoc; | ||||||||||||
754 | declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction( | ||||||||||||
755 | /*HasProto=*/true, | ||||||||||||
756 | /*IsAmbiguous=*/false, | ||||||||||||
757 | /*LParenLoc=*/NoLoc, | ||||||||||||
758 | /*ArgInfo=*/nullptr, | ||||||||||||
759 | /*NumParams=*/0, | ||||||||||||
760 | /*EllipsisLoc=*/NoLoc, | ||||||||||||
761 | /*RParenLoc=*/NoLoc, | ||||||||||||
762 | /*RefQualifierIsLvalueRef=*/true, | ||||||||||||
763 | /*RefQualifierLoc=*/NoLoc, | ||||||||||||
764 | /*MutableLoc=*/NoLoc, EST_None, | ||||||||||||
765 | /*ESpecRange=*/SourceRange(), | ||||||||||||
766 | /*Exceptions=*/nullptr, | ||||||||||||
767 | /*ExceptionRanges=*/nullptr, | ||||||||||||
768 | /*NumExceptions=*/0, | ||||||||||||
769 | /*NoexceptExpr=*/nullptr, | ||||||||||||
770 | /*ExceptionSpecTokens=*/nullptr, | ||||||||||||
771 | /*DeclsInPrototype=*/None, loc, loc, declarator)); | ||||||||||||
772 | |||||||||||||
773 | // For consistency, make sure the state still has us as processing | ||||||||||||
774 | // the decl spec. | ||||||||||||
775 | assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1)((state.getCurrentChunkIndex() == declarator.getNumTypeObjects () - 1) ? static_cast<void> (0) : __assert_fail ("state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 775, __PRETTY_FUNCTION__)); | ||||||||||||
776 | state.setCurrentChunkIndex(declarator.getNumTypeObjects()); | ||||||||||||
777 | } | ||||||||||||
778 | |||||||||||||
779 | static void diagnoseAndRemoveTypeQualifiers(Sema &S, const DeclSpec &DS, | ||||||||||||
780 | unsigned &TypeQuals, | ||||||||||||
781 | QualType TypeSoFar, | ||||||||||||
782 | unsigned RemoveTQs, | ||||||||||||
783 | unsigned DiagID) { | ||||||||||||
784 | // If this occurs outside a template instantiation, warn the user about | ||||||||||||
785 | // it; they probably didn't mean to specify a redundant qualifier. | ||||||||||||
786 | typedef std::pair<DeclSpec::TQ, SourceLocation> QualLoc; | ||||||||||||
787 | for (QualLoc Qual : {QualLoc(DeclSpec::TQ_const, DS.getConstSpecLoc()), | ||||||||||||
788 | QualLoc(DeclSpec::TQ_restrict, DS.getRestrictSpecLoc()), | ||||||||||||
789 | QualLoc(DeclSpec::TQ_volatile, DS.getVolatileSpecLoc()), | ||||||||||||
790 | QualLoc(DeclSpec::TQ_atomic, DS.getAtomicSpecLoc())}) { | ||||||||||||
791 | if (!(RemoveTQs & Qual.first)) | ||||||||||||
792 | continue; | ||||||||||||
793 | |||||||||||||
794 | if (!S.inTemplateInstantiation()) { | ||||||||||||
795 | if (TypeQuals & Qual.first) | ||||||||||||
796 | S.Diag(Qual.second, DiagID) | ||||||||||||
797 | << DeclSpec::getSpecifierName(Qual.first) << TypeSoFar | ||||||||||||
798 | << FixItHint::CreateRemoval(Qual.second); | ||||||||||||
799 | } | ||||||||||||
800 | |||||||||||||
801 | TypeQuals &= ~Qual.first; | ||||||||||||
802 | } | ||||||||||||
803 | } | ||||||||||||
804 | |||||||||||||
805 | /// Return true if this is omitted block return type. Also check type | ||||||||||||
806 | /// attributes and type qualifiers when returning true. | ||||||||||||
807 | static bool checkOmittedBlockReturnType(Sema &S, Declarator &declarator, | ||||||||||||
808 | QualType Result) { | ||||||||||||
809 | if (!isOmittedBlockReturnType(declarator)) | ||||||||||||
810 | return false; | ||||||||||||
811 | |||||||||||||
812 | // Warn if we see type attributes for omitted return type on a block literal. | ||||||||||||
813 | SmallVector<ParsedAttr *, 2> ToBeRemoved; | ||||||||||||
814 | for (ParsedAttr &AL : declarator.getMutableDeclSpec().getAttributes()) { | ||||||||||||
815 | if (AL.isInvalid() || !AL.isTypeAttr()) | ||||||||||||
816 | continue; | ||||||||||||
817 | S.Diag(AL.getLoc(), | ||||||||||||
818 | diag::warn_block_literal_attributes_on_omitted_return_type) | ||||||||||||
819 | << AL; | ||||||||||||
820 | ToBeRemoved.push_back(&AL); | ||||||||||||
821 | } | ||||||||||||
822 | // Remove bad attributes from the list. | ||||||||||||
823 | for (ParsedAttr *AL : ToBeRemoved) | ||||||||||||
824 | declarator.getMutableDeclSpec().getAttributes().remove(AL); | ||||||||||||
825 | |||||||||||||
826 | // Warn if we see type qualifiers for omitted return type on a block literal. | ||||||||||||
827 | const DeclSpec &DS = declarator.getDeclSpec(); | ||||||||||||
828 | unsigned TypeQuals = DS.getTypeQualifiers(); | ||||||||||||
829 | diagnoseAndRemoveTypeQualifiers(S, DS, TypeQuals, Result, (unsigned)-1, | ||||||||||||
830 | diag::warn_block_literal_qualifiers_on_omitted_return_type); | ||||||||||||
831 | declarator.getMutableDeclSpec().ClearTypeQualifiers(); | ||||||||||||
832 | |||||||||||||
833 | return true; | ||||||||||||
834 | } | ||||||||||||
835 | |||||||||||||
836 | /// Apply Objective-C type arguments to the given type. | ||||||||||||
837 | static QualType applyObjCTypeArgs(Sema &S, SourceLocation loc, QualType type, | ||||||||||||
838 | ArrayRef<TypeSourceInfo *> typeArgs, | ||||||||||||
839 | SourceRange typeArgsRange, | ||||||||||||
840 | bool failOnError = false) { | ||||||||||||
841 | // We can only apply type arguments to an Objective-C class type. | ||||||||||||
842 | const auto *objcObjectType = type->getAs<ObjCObjectType>(); | ||||||||||||
843 | if (!objcObjectType || !objcObjectType->getInterface()) { | ||||||||||||
844 | S.Diag(loc, diag::err_objc_type_args_non_class) | ||||||||||||
845 | << type | ||||||||||||
846 | << typeArgsRange; | ||||||||||||
847 | |||||||||||||
848 | if (failOnError) | ||||||||||||
849 | return QualType(); | ||||||||||||
850 | return type; | ||||||||||||
851 | } | ||||||||||||
852 | |||||||||||||
853 | // The class type must be parameterized. | ||||||||||||
854 | ObjCInterfaceDecl *objcClass = objcObjectType->getInterface(); | ||||||||||||
855 | ObjCTypeParamList *typeParams = objcClass->getTypeParamList(); | ||||||||||||
856 | if (!typeParams) { | ||||||||||||
857 | S.Diag(loc, diag::err_objc_type_args_non_parameterized_class) | ||||||||||||
858 | << objcClass->getDeclName() | ||||||||||||
859 | << FixItHint::CreateRemoval(typeArgsRange); | ||||||||||||
860 | |||||||||||||
861 | if (failOnError) | ||||||||||||
862 | return QualType(); | ||||||||||||
863 | |||||||||||||
864 | return type; | ||||||||||||
865 | } | ||||||||||||
866 | |||||||||||||
867 | // The type must not already be specialized. | ||||||||||||
868 | if (objcObjectType->isSpecialized()) { | ||||||||||||
869 | S.Diag(loc, diag::err_objc_type_args_specialized_class) | ||||||||||||
870 | << type | ||||||||||||
871 | << FixItHint::CreateRemoval(typeArgsRange); | ||||||||||||
872 | |||||||||||||
873 | if (failOnError) | ||||||||||||
874 | return QualType(); | ||||||||||||
875 | |||||||||||||
876 | return type; | ||||||||||||
877 | } | ||||||||||||
878 | |||||||||||||
879 | // Check the type arguments. | ||||||||||||
880 | SmallVector<QualType, 4> finalTypeArgs; | ||||||||||||
881 | unsigned numTypeParams = typeParams->size(); | ||||||||||||
882 | bool anyPackExpansions = false; | ||||||||||||
883 | for (unsigned i = 0, n = typeArgs.size(); i != n; ++i) { | ||||||||||||
884 | TypeSourceInfo *typeArgInfo = typeArgs[i]; | ||||||||||||
885 | QualType typeArg = typeArgInfo->getType(); | ||||||||||||
886 | |||||||||||||
887 | // Type arguments cannot have explicit qualifiers or nullability. | ||||||||||||
888 | // We ignore indirect sources of these, e.g. behind typedefs or | ||||||||||||
889 | // template arguments. | ||||||||||||
890 | if (TypeLoc qual = typeArgInfo->getTypeLoc().findExplicitQualifierLoc()) { | ||||||||||||
891 | bool diagnosed = false; | ||||||||||||
892 | SourceRange rangeToRemove; | ||||||||||||
893 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { | ||||||||||||
894 | rangeToRemove = attr.getLocalSourceRange(); | ||||||||||||
895 | if (attr.getTypePtr()->getImmediateNullability()) { | ||||||||||||
896 | typeArg = attr.getTypePtr()->getModifiedType(); | ||||||||||||
897 | S.Diag(attr.getBeginLoc(), | ||||||||||||
898 | diag::err_objc_type_arg_explicit_nullability) | ||||||||||||
899 | << typeArg << FixItHint::CreateRemoval(rangeToRemove); | ||||||||||||
900 | diagnosed = true; | ||||||||||||
901 | } | ||||||||||||
902 | } | ||||||||||||
903 | |||||||||||||
904 | if (!diagnosed) { | ||||||||||||
905 | S.Diag(qual.getBeginLoc(), diag::err_objc_type_arg_qualified) | ||||||||||||
906 | << typeArg << typeArg.getQualifiers().getAsString() | ||||||||||||
907 | << FixItHint::CreateRemoval(rangeToRemove); | ||||||||||||
908 | } | ||||||||||||
909 | } | ||||||||||||
910 | |||||||||||||
911 | // Remove qualifiers even if they're non-local. | ||||||||||||
912 | typeArg = typeArg.getUnqualifiedType(); | ||||||||||||
913 | |||||||||||||
914 | finalTypeArgs.push_back(typeArg); | ||||||||||||
915 | |||||||||||||
916 | if (typeArg->getAs<PackExpansionType>()) | ||||||||||||
917 | anyPackExpansions = true; | ||||||||||||
918 | |||||||||||||
919 | // Find the corresponding type parameter, if there is one. | ||||||||||||
920 | ObjCTypeParamDecl *typeParam = nullptr; | ||||||||||||
921 | if (!anyPackExpansions) { | ||||||||||||
922 | if (i < numTypeParams) { | ||||||||||||
923 | typeParam = typeParams->begin()[i]; | ||||||||||||
924 | } else { | ||||||||||||
925 | // Too many arguments. | ||||||||||||
926 | S.Diag(loc, diag::err_objc_type_args_wrong_arity) | ||||||||||||
927 | << false | ||||||||||||
928 | << objcClass->getDeclName() | ||||||||||||
929 | << (unsigned)typeArgs.size() | ||||||||||||
930 | << numTypeParams; | ||||||||||||
931 | S.Diag(objcClass->getLocation(), diag::note_previous_decl) | ||||||||||||
932 | << objcClass; | ||||||||||||
933 | |||||||||||||
934 | if (failOnError) | ||||||||||||
935 | return QualType(); | ||||||||||||
936 | |||||||||||||
937 | return type; | ||||||||||||
938 | } | ||||||||||||
939 | } | ||||||||||||
940 | |||||||||||||
941 | // Objective-C object pointer types must be substitutable for the bounds. | ||||||||||||
942 | if (const auto *typeArgObjC = typeArg->getAs<ObjCObjectPointerType>()) { | ||||||||||||
943 | // If we don't have a type parameter to match against, assume | ||||||||||||
944 | // everything is fine. There was a prior pack expansion that | ||||||||||||
945 | // means we won't be able to match anything. | ||||||||||||
946 | if (!typeParam) { | ||||||||||||
947 | assert(anyPackExpansions && "Too many arguments?")((anyPackExpansions && "Too many arguments?") ? static_cast <void> (0) : __assert_fail ("anyPackExpansions && \"Too many arguments?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 947, __PRETTY_FUNCTION__)); | ||||||||||||
948 | continue; | ||||||||||||
949 | } | ||||||||||||
950 | |||||||||||||
951 | // Retrieve the bound. | ||||||||||||
952 | QualType bound = typeParam->getUnderlyingType(); | ||||||||||||
953 | const auto *boundObjC = bound->getAs<ObjCObjectPointerType>(); | ||||||||||||
954 | |||||||||||||
955 | // Determine whether the type argument is substitutable for the bound. | ||||||||||||
956 | if (typeArgObjC->isObjCIdType()) { | ||||||||||||
957 | // When the type argument is 'id', the only acceptable type | ||||||||||||
958 | // parameter bound is 'id'. | ||||||||||||
959 | if (boundObjC->isObjCIdType()) | ||||||||||||
960 | continue; | ||||||||||||
961 | } else if (S.Context.canAssignObjCInterfaces(boundObjC, typeArgObjC)) { | ||||||||||||
962 | // Otherwise, we follow the assignability rules. | ||||||||||||
963 | continue; | ||||||||||||
964 | } | ||||||||||||
965 | |||||||||||||
966 | // Diagnose the mismatch. | ||||||||||||
967 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), | ||||||||||||
968 | diag::err_objc_type_arg_does_not_match_bound) | ||||||||||||
969 | << typeArg << bound << typeParam->getDeclName(); | ||||||||||||
970 | S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here) | ||||||||||||
971 | << typeParam->getDeclName(); | ||||||||||||
972 | |||||||||||||
973 | if (failOnError) | ||||||||||||
974 | return QualType(); | ||||||||||||
975 | |||||||||||||
976 | return type; | ||||||||||||
977 | } | ||||||||||||
978 | |||||||||||||
979 | // Block pointer types are permitted for unqualified 'id' bounds. | ||||||||||||
980 | if (typeArg->isBlockPointerType()) { | ||||||||||||
981 | // If we don't have a type parameter to match against, assume | ||||||||||||
982 | // everything is fine. There was a prior pack expansion that | ||||||||||||
983 | // means we won't be able to match anything. | ||||||||||||
984 | if (!typeParam) { | ||||||||||||
985 | assert(anyPackExpansions && "Too many arguments?")((anyPackExpansions && "Too many arguments?") ? static_cast <void> (0) : __assert_fail ("anyPackExpansions && \"Too many arguments?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 985, __PRETTY_FUNCTION__)); | ||||||||||||
986 | continue; | ||||||||||||
987 | } | ||||||||||||
988 | |||||||||||||
989 | // Retrieve the bound. | ||||||||||||
990 | QualType bound = typeParam->getUnderlyingType(); | ||||||||||||
991 | if (bound->isBlockCompatibleObjCPointerType(S.Context)) | ||||||||||||
992 | continue; | ||||||||||||
993 | |||||||||||||
994 | // Diagnose the mismatch. | ||||||||||||
995 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), | ||||||||||||
996 | diag::err_objc_type_arg_does_not_match_bound) | ||||||||||||
997 | << typeArg << bound << typeParam->getDeclName(); | ||||||||||||
998 | S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here) | ||||||||||||
999 | << typeParam->getDeclName(); | ||||||||||||
1000 | |||||||||||||
1001 | if (failOnError) | ||||||||||||
1002 | return QualType(); | ||||||||||||
1003 | |||||||||||||
1004 | return type; | ||||||||||||
1005 | } | ||||||||||||
1006 | |||||||||||||
1007 | // Dependent types will be checked at instantiation time. | ||||||||||||
1008 | if (typeArg->isDependentType()) { | ||||||||||||
1009 | continue; | ||||||||||||
1010 | } | ||||||||||||
1011 | |||||||||||||
1012 | // Diagnose non-id-compatible type arguments. | ||||||||||||
1013 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), | ||||||||||||
1014 | diag::err_objc_type_arg_not_id_compatible) | ||||||||||||
1015 | << typeArg << typeArgInfo->getTypeLoc().getSourceRange(); | ||||||||||||
1016 | |||||||||||||
1017 | if (failOnError) | ||||||||||||
1018 | return QualType(); | ||||||||||||
1019 | |||||||||||||
1020 | return type; | ||||||||||||
1021 | } | ||||||||||||
1022 | |||||||||||||
1023 | // Make sure we didn't have the wrong number of arguments. | ||||||||||||
1024 | if (!anyPackExpansions && finalTypeArgs.size() != numTypeParams) { | ||||||||||||
1025 | S.Diag(loc, diag::err_objc_type_args_wrong_arity) | ||||||||||||
1026 | << (typeArgs.size() < typeParams->size()) | ||||||||||||
1027 | << objcClass->getDeclName() | ||||||||||||
1028 | << (unsigned)finalTypeArgs.size() | ||||||||||||
1029 | << (unsigned)numTypeParams; | ||||||||||||
1030 | S.Diag(objcClass->getLocation(), diag::note_previous_decl) | ||||||||||||
1031 | << objcClass; | ||||||||||||
1032 | |||||||||||||
1033 | if (failOnError) | ||||||||||||
1034 | return QualType(); | ||||||||||||
1035 | |||||||||||||
1036 | return type; | ||||||||||||
1037 | } | ||||||||||||
1038 | |||||||||||||
1039 | // Success. Form the specialized type. | ||||||||||||
1040 | return S.Context.getObjCObjectType(type, finalTypeArgs, { }, false); | ||||||||||||
1041 | } | ||||||||||||
1042 | |||||||||||||
1043 | QualType Sema::BuildObjCTypeParamType(const ObjCTypeParamDecl *Decl, | ||||||||||||
1044 | SourceLocation ProtocolLAngleLoc, | ||||||||||||
1045 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||||||
1046 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||||||
1047 | SourceLocation ProtocolRAngleLoc, | ||||||||||||
1048 | bool FailOnError) { | ||||||||||||
1049 | QualType Result = QualType(Decl->getTypeForDecl(), 0); | ||||||||||||
1050 | if (!Protocols.empty()) { | ||||||||||||
1051 | bool HasError; | ||||||||||||
1052 | Result = Context.applyObjCProtocolQualifiers(Result, Protocols, | ||||||||||||
1053 | HasError); | ||||||||||||
1054 | if (HasError) { | ||||||||||||
1055 | Diag(SourceLocation(), diag::err_invalid_protocol_qualifiers) | ||||||||||||
1056 | << SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc); | ||||||||||||
1057 | if (FailOnError) Result = QualType(); | ||||||||||||
1058 | } | ||||||||||||
1059 | if (FailOnError && Result.isNull()) | ||||||||||||
1060 | return QualType(); | ||||||||||||
1061 | } | ||||||||||||
1062 | |||||||||||||
1063 | return Result; | ||||||||||||
1064 | } | ||||||||||||
1065 | |||||||||||||
1066 | QualType Sema::BuildObjCObjectType(QualType BaseType, | ||||||||||||
1067 | SourceLocation Loc, | ||||||||||||
1068 | SourceLocation TypeArgsLAngleLoc, | ||||||||||||
1069 | ArrayRef<TypeSourceInfo *> TypeArgs, | ||||||||||||
1070 | SourceLocation TypeArgsRAngleLoc, | ||||||||||||
1071 | SourceLocation ProtocolLAngleLoc, | ||||||||||||
1072 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||||||
1073 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||||||
1074 | SourceLocation ProtocolRAngleLoc, | ||||||||||||
1075 | bool FailOnError) { | ||||||||||||
1076 | QualType Result = BaseType; | ||||||||||||
1077 | if (!TypeArgs.empty()) { | ||||||||||||
1078 | Result = applyObjCTypeArgs(*this, Loc, Result, TypeArgs, | ||||||||||||
1079 | SourceRange(TypeArgsLAngleLoc, | ||||||||||||
1080 | TypeArgsRAngleLoc), | ||||||||||||
1081 | FailOnError); | ||||||||||||
1082 | if (FailOnError && Result.isNull()) | ||||||||||||
1083 | return QualType(); | ||||||||||||
1084 | } | ||||||||||||
1085 | |||||||||||||
1086 | if (!Protocols.empty()) { | ||||||||||||
1087 | bool HasError; | ||||||||||||
1088 | Result = Context.applyObjCProtocolQualifiers(Result, Protocols, | ||||||||||||
1089 | HasError); | ||||||||||||
1090 | if (HasError) { | ||||||||||||
1091 | Diag(Loc, diag::err_invalid_protocol_qualifiers) | ||||||||||||
1092 | << SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc); | ||||||||||||
1093 | if (FailOnError) Result = QualType(); | ||||||||||||
1094 | } | ||||||||||||
1095 | if (FailOnError && Result.isNull()) | ||||||||||||
1096 | return QualType(); | ||||||||||||
1097 | } | ||||||||||||
1098 | |||||||||||||
1099 | return Result; | ||||||||||||
1100 | } | ||||||||||||
1101 | |||||||||||||
1102 | TypeResult Sema::actOnObjCProtocolQualifierType( | ||||||||||||
1103 | SourceLocation lAngleLoc, | ||||||||||||
1104 | ArrayRef<Decl *> protocols, | ||||||||||||
1105 | ArrayRef<SourceLocation> protocolLocs, | ||||||||||||
1106 | SourceLocation rAngleLoc) { | ||||||||||||
1107 | // Form id<protocol-list>. | ||||||||||||
1108 | QualType Result = Context.getObjCObjectType( | ||||||||||||
1109 | Context.ObjCBuiltinIdTy, { }, | ||||||||||||
1110 | llvm::makeArrayRef( | ||||||||||||
1111 | (ObjCProtocolDecl * const *)protocols.data(), | ||||||||||||
1112 | protocols.size()), | ||||||||||||
1113 | false); | ||||||||||||
1114 | Result = Context.getObjCObjectPointerType(Result); | ||||||||||||
1115 | |||||||||||||
1116 | TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result); | ||||||||||||
1117 | TypeLoc ResultTL = ResultTInfo->getTypeLoc(); | ||||||||||||
1118 | |||||||||||||
1119 | auto ObjCObjectPointerTL = ResultTL.castAs<ObjCObjectPointerTypeLoc>(); | ||||||||||||
1120 | ObjCObjectPointerTL.setStarLoc(SourceLocation()); // implicit | ||||||||||||
1121 | |||||||||||||
1122 | auto ObjCObjectTL = ObjCObjectPointerTL.getPointeeLoc() | ||||||||||||
1123 | .castAs<ObjCObjectTypeLoc>(); | ||||||||||||
1124 | ObjCObjectTL.setHasBaseTypeAsWritten(false); | ||||||||||||
1125 | ObjCObjectTL.getBaseLoc().initialize(Context, SourceLocation()); | ||||||||||||
1126 | |||||||||||||
1127 | // No type arguments. | ||||||||||||
1128 | ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation()); | ||||||||||||
1129 | ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation()); | ||||||||||||
1130 | |||||||||||||
1131 | // Fill in protocol qualifiers. | ||||||||||||
1132 | ObjCObjectTL.setProtocolLAngleLoc(lAngleLoc); | ||||||||||||
1133 | ObjCObjectTL.setProtocolRAngleLoc(rAngleLoc); | ||||||||||||
1134 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) | ||||||||||||
1135 | ObjCObjectTL.setProtocolLoc(i, protocolLocs[i]); | ||||||||||||
1136 | |||||||||||||
1137 | // We're done. Return the completed type to the parser. | ||||||||||||
1138 | return CreateParsedType(Result, ResultTInfo); | ||||||||||||
1139 | } | ||||||||||||
1140 | |||||||||||||
1141 | TypeResult Sema::actOnObjCTypeArgsAndProtocolQualifiers( | ||||||||||||
1142 | Scope *S, | ||||||||||||
1143 | SourceLocation Loc, | ||||||||||||
1144 | ParsedType BaseType, | ||||||||||||
1145 | SourceLocation TypeArgsLAngleLoc, | ||||||||||||
1146 | ArrayRef<ParsedType> TypeArgs, | ||||||||||||
1147 | SourceLocation TypeArgsRAngleLoc, | ||||||||||||
1148 | SourceLocation ProtocolLAngleLoc, | ||||||||||||
1149 | ArrayRef<Decl *> Protocols, | ||||||||||||
1150 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||||||
1151 | SourceLocation ProtocolRAngleLoc) { | ||||||||||||
1152 | TypeSourceInfo *BaseTypeInfo = nullptr; | ||||||||||||
1153 | QualType T = GetTypeFromParser(BaseType, &BaseTypeInfo); | ||||||||||||
1154 | if (T.isNull()) | ||||||||||||
1155 | return true; | ||||||||||||
1156 | |||||||||||||
1157 | // Handle missing type-source info. | ||||||||||||
1158 | if (!BaseTypeInfo) | ||||||||||||
1159 | BaseTypeInfo = Context.getTrivialTypeSourceInfo(T, Loc); | ||||||||||||
1160 | |||||||||||||
1161 | // Extract type arguments. | ||||||||||||
1162 | SmallVector<TypeSourceInfo *, 4> ActualTypeArgInfos; | ||||||||||||
1163 | for (unsigned i = 0, n = TypeArgs.size(); i != n; ++i) { | ||||||||||||
1164 | TypeSourceInfo *TypeArgInfo = nullptr; | ||||||||||||
1165 | QualType TypeArg = GetTypeFromParser(TypeArgs[i], &TypeArgInfo); | ||||||||||||
1166 | if (TypeArg.isNull()) { | ||||||||||||
1167 | ActualTypeArgInfos.clear(); | ||||||||||||
1168 | break; | ||||||||||||
1169 | } | ||||||||||||
1170 | |||||||||||||
1171 | assert(TypeArgInfo && "No type source info?")((TypeArgInfo && "No type source info?") ? static_cast <void> (0) : __assert_fail ("TypeArgInfo && \"No type source info?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1171, __PRETTY_FUNCTION__)); | ||||||||||||
1172 | ActualTypeArgInfos.push_back(TypeArgInfo); | ||||||||||||
1173 | } | ||||||||||||
1174 | |||||||||||||
1175 | // Build the object type. | ||||||||||||
1176 | QualType Result = BuildObjCObjectType( | ||||||||||||
1177 | T, BaseTypeInfo->getTypeLoc().getSourceRange().getBegin(), | ||||||||||||
1178 | TypeArgsLAngleLoc, ActualTypeArgInfos, TypeArgsRAngleLoc, | ||||||||||||
1179 | ProtocolLAngleLoc, | ||||||||||||
1180 | llvm::makeArrayRef((ObjCProtocolDecl * const *)Protocols.data(), | ||||||||||||
1181 | Protocols.size()), | ||||||||||||
1182 | ProtocolLocs, ProtocolRAngleLoc, | ||||||||||||
1183 | /*FailOnError=*/false); | ||||||||||||
1184 | |||||||||||||
1185 | if (Result == T) | ||||||||||||
1186 | return BaseType; | ||||||||||||
1187 | |||||||||||||
1188 | // Create source information for this type. | ||||||||||||
1189 | TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result); | ||||||||||||
1190 | TypeLoc ResultTL = ResultTInfo->getTypeLoc(); | ||||||||||||
1191 | |||||||||||||
1192 | // For id<Proto1, Proto2> or Class<Proto1, Proto2>, we'll have an | ||||||||||||
1193 | // object pointer type. Fill in source information for it. | ||||||||||||
1194 | if (auto ObjCObjectPointerTL = ResultTL.getAs<ObjCObjectPointerTypeLoc>()) { | ||||||||||||
1195 | // The '*' is implicit. | ||||||||||||
1196 | ObjCObjectPointerTL.setStarLoc(SourceLocation()); | ||||||||||||
1197 | ResultTL = ObjCObjectPointerTL.getPointeeLoc(); | ||||||||||||
1198 | } | ||||||||||||
1199 | |||||||||||||
1200 | if (auto OTPTL = ResultTL.getAs<ObjCTypeParamTypeLoc>()) { | ||||||||||||
1201 | // Protocol qualifier information. | ||||||||||||
1202 | if (OTPTL.getNumProtocols() > 0) { | ||||||||||||
1203 | assert(OTPTL.getNumProtocols() == Protocols.size())((OTPTL.getNumProtocols() == Protocols.size()) ? static_cast< void> (0) : __assert_fail ("OTPTL.getNumProtocols() == Protocols.size()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1203, __PRETTY_FUNCTION__)); | ||||||||||||
1204 | OTPTL.setProtocolLAngleLoc(ProtocolLAngleLoc); | ||||||||||||
1205 | OTPTL.setProtocolRAngleLoc(ProtocolRAngleLoc); | ||||||||||||
1206 | for (unsigned i = 0, n = Protocols.size(); i != n; ++i) | ||||||||||||
1207 | OTPTL.setProtocolLoc(i, ProtocolLocs[i]); | ||||||||||||
1208 | } | ||||||||||||
1209 | |||||||||||||
1210 | // We're done. Return the completed type to the parser. | ||||||||||||
1211 | return CreateParsedType(Result, ResultTInfo); | ||||||||||||
1212 | } | ||||||||||||
1213 | |||||||||||||
1214 | auto ObjCObjectTL = ResultTL.castAs<ObjCObjectTypeLoc>(); | ||||||||||||
1215 | |||||||||||||
1216 | // Type argument information. | ||||||||||||
1217 | if (ObjCObjectTL.getNumTypeArgs() > 0) { | ||||||||||||
1218 | assert(ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size())((ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size()) ? static_cast<void> (0) : __assert_fail ("ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1218, __PRETTY_FUNCTION__)); | ||||||||||||
1219 | ObjCObjectTL.setTypeArgsLAngleLoc(TypeArgsLAngleLoc); | ||||||||||||
1220 | ObjCObjectTL.setTypeArgsRAngleLoc(TypeArgsRAngleLoc); | ||||||||||||
1221 | for (unsigned i = 0, n = ActualTypeArgInfos.size(); i != n; ++i) | ||||||||||||
1222 | ObjCObjectTL.setTypeArgTInfo(i, ActualTypeArgInfos[i]); | ||||||||||||
1223 | } else { | ||||||||||||
1224 | ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation()); | ||||||||||||
1225 | ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation()); | ||||||||||||
1226 | } | ||||||||||||
1227 | |||||||||||||
1228 | // Protocol qualifier information. | ||||||||||||
1229 | if (ObjCObjectTL.getNumProtocols() > 0) { | ||||||||||||
1230 | assert(ObjCObjectTL.getNumProtocols() == Protocols.size())((ObjCObjectTL.getNumProtocols() == Protocols.size()) ? static_cast <void> (0) : __assert_fail ("ObjCObjectTL.getNumProtocols() == Protocols.size()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1230, __PRETTY_FUNCTION__)); | ||||||||||||
1231 | ObjCObjectTL.setProtocolLAngleLoc(ProtocolLAngleLoc); | ||||||||||||
1232 | ObjCObjectTL.setProtocolRAngleLoc(ProtocolRAngleLoc); | ||||||||||||
1233 | for (unsigned i = 0, n = Protocols.size(); i != n; ++i) | ||||||||||||
1234 | ObjCObjectTL.setProtocolLoc(i, ProtocolLocs[i]); | ||||||||||||
1235 | } else { | ||||||||||||
1236 | ObjCObjectTL.setProtocolLAngleLoc(SourceLocation()); | ||||||||||||
1237 | ObjCObjectTL.setProtocolRAngleLoc(SourceLocation()); | ||||||||||||
1238 | } | ||||||||||||
1239 | |||||||||||||
1240 | // Base type. | ||||||||||||
1241 | ObjCObjectTL.setHasBaseTypeAsWritten(true); | ||||||||||||
1242 | if (ObjCObjectTL.getType() == T) | ||||||||||||
1243 | ObjCObjectTL.getBaseLoc().initializeFullCopy(BaseTypeInfo->getTypeLoc()); | ||||||||||||
1244 | else | ||||||||||||
1245 | ObjCObjectTL.getBaseLoc().initialize(Context, Loc); | ||||||||||||
1246 | |||||||||||||
1247 | // We're done. Return the completed type to the parser. | ||||||||||||
1248 | return CreateParsedType(Result, ResultTInfo); | ||||||||||||
1249 | } | ||||||||||||
1250 | |||||||||||||
1251 | static OpenCLAccessAttr::Spelling | ||||||||||||
1252 | getImageAccess(const ParsedAttributesView &Attrs) { | ||||||||||||
1253 | for (const ParsedAttr &AL : Attrs) | ||||||||||||
1254 | if (AL.getKind() == ParsedAttr::AT_OpenCLAccess) | ||||||||||||
1255 | return static_cast<OpenCLAccessAttr::Spelling>(AL.getSemanticSpelling()); | ||||||||||||
1256 | return OpenCLAccessAttr::Keyword_read_only; | ||||||||||||
1257 | } | ||||||||||||
1258 | |||||||||||||
1259 | /// Convert the specified declspec to the appropriate type | ||||||||||||
1260 | /// object. | ||||||||||||
1261 | /// \param state Specifies the declarator containing the declaration specifier | ||||||||||||
1262 | /// to be converted, along with other associated processing state. | ||||||||||||
1263 | /// \returns The type described by the declaration specifiers. This function | ||||||||||||
1264 | /// never returns null. | ||||||||||||
1265 | static QualType ConvertDeclSpecToType(TypeProcessingState &state) { | ||||||||||||
1266 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity | ||||||||||||
1267 | // checking. | ||||||||||||
1268 | |||||||||||||
1269 | Sema &S = state.getSema(); | ||||||||||||
1270 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
1271 | DeclSpec &DS = declarator.getMutableDeclSpec(); | ||||||||||||
1272 | SourceLocation DeclLoc = declarator.getIdentifierLoc(); | ||||||||||||
1273 | if (DeclLoc.isInvalid()) | ||||||||||||
1274 | DeclLoc = DS.getBeginLoc(); | ||||||||||||
1275 | |||||||||||||
1276 | ASTContext &Context = S.Context; | ||||||||||||
1277 | |||||||||||||
1278 | QualType Result; | ||||||||||||
1279 | switch (DS.getTypeSpecType()) { | ||||||||||||
1280 | case DeclSpec::TST_void: | ||||||||||||
1281 | Result = Context.VoidTy; | ||||||||||||
1282 | break; | ||||||||||||
1283 | case DeclSpec::TST_char: | ||||||||||||
1284 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified) | ||||||||||||
1285 | Result = Context.CharTy; | ||||||||||||
1286 | else if (DS.getTypeSpecSign() == TypeSpecifierSign::Signed) | ||||||||||||
1287 | Result = Context.SignedCharTy; | ||||||||||||
1288 | else { | ||||||||||||
1289 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned &&((DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1290, __PRETTY_FUNCTION__)) | ||||||||||||
1290 | "Unknown TSS value")((DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1290, __PRETTY_FUNCTION__)); | ||||||||||||
1291 | Result = Context.UnsignedCharTy; | ||||||||||||
1292 | } | ||||||||||||
1293 | break; | ||||||||||||
1294 | case DeclSpec::TST_wchar: | ||||||||||||
1295 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified) | ||||||||||||
1296 | Result = Context.WCharTy; | ||||||||||||
1297 | else if (DS.getTypeSpecSign() == TypeSpecifierSign::Signed) { | ||||||||||||
1298 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_wchar_t_sign_spec) | ||||||||||||
1299 | << DS.getSpecifierName(DS.getTypeSpecType(), | ||||||||||||
1300 | Context.getPrintingPolicy()); | ||||||||||||
1301 | Result = Context.getSignedWCharType(); | ||||||||||||
1302 | } else { | ||||||||||||
1303 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned &&((DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1304, __PRETTY_FUNCTION__)) | ||||||||||||
1304 | "Unknown TSS value")((DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1304, __PRETTY_FUNCTION__)); | ||||||||||||
1305 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_wchar_t_sign_spec) | ||||||||||||
1306 | << DS.getSpecifierName(DS.getTypeSpecType(), | ||||||||||||
1307 | Context.getPrintingPolicy()); | ||||||||||||
1308 | Result = Context.getUnsignedWCharType(); | ||||||||||||
1309 | } | ||||||||||||
1310 | break; | ||||||||||||
1311 | case DeclSpec::TST_char8: | ||||||||||||
1312 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified &&((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1313, __PRETTY_FUNCTION__)) | ||||||||||||
1313 | "Unknown TSS value")((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1313, __PRETTY_FUNCTION__)); | ||||||||||||
1314 | Result = Context.Char8Ty; | ||||||||||||
1315 | break; | ||||||||||||
1316 | case DeclSpec::TST_char16: | ||||||||||||
1317 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified &&((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1318, __PRETTY_FUNCTION__)) | ||||||||||||
1318 | "Unknown TSS value")((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1318, __PRETTY_FUNCTION__)); | ||||||||||||
1319 | Result = Context.Char16Ty; | ||||||||||||
1320 | break; | ||||||||||||
1321 | case DeclSpec::TST_char32: | ||||||||||||
1322 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified &&((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1323, __PRETTY_FUNCTION__)) | ||||||||||||
1323 | "Unknown TSS value")((DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Unknown TSS value") ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Unknown TSS value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1323, __PRETTY_FUNCTION__)); | ||||||||||||
1324 | Result = Context.Char32Ty; | ||||||||||||
1325 | break; | ||||||||||||
1326 | case DeclSpec::TST_unspecified: | ||||||||||||
1327 | // If this is a missing declspec in a block literal return context, then it | ||||||||||||
1328 | // is inferred from the return statements inside the block. | ||||||||||||
1329 | // The declspec is always missing in a lambda expr context; it is either | ||||||||||||
1330 | // specified with a trailing return type or inferred. | ||||||||||||
1331 | if (S.getLangOpts().CPlusPlus14 && | ||||||||||||
1332 | declarator.getContext() == DeclaratorContext::LambdaExpr) { | ||||||||||||
1333 | // In C++1y, a lambda's implicit return type is 'auto'. | ||||||||||||
1334 | Result = Context.getAutoDeductType(); | ||||||||||||
1335 | break; | ||||||||||||
1336 | } else if (declarator.getContext() == DeclaratorContext::LambdaExpr || | ||||||||||||
1337 | checkOmittedBlockReturnType(S, declarator, | ||||||||||||
1338 | Context.DependentTy)) { | ||||||||||||
1339 | Result = Context.DependentTy; | ||||||||||||
1340 | break; | ||||||||||||
1341 | } | ||||||||||||
1342 | |||||||||||||
1343 | // Unspecified typespec defaults to int in C90. However, the C90 grammar | ||||||||||||
1344 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, | ||||||||||||
1345 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. | ||||||||||||
1346 | // Note that the one exception to this is function definitions, which are | ||||||||||||
1347 | // allowed to be completely missing a declspec. This is handled in the | ||||||||||||
1348 | // parser already though by it pretending to have seen an 'int' in this | ||||||||||||
1349 | // case. | ||||||||||||
1350 | if (S.getLangOpts().ImplicitInt) { | ||||||||||||
1351 | // In C89 mode, we only warn if there is a completely missing declspec | ||||||||||||
1352 | // when one is not allowed. | ||||||||||||
1353 | if (DS.isEmpty()) { | ||||||||||||
1354 | S.Diag(DeclLoc, diag::ext_missing_declspec) | ||||||||||||
1355 | << DS.getSourceRange() | ||||||||||||
1356 | << FixItHint::CreateInsertion(DS.getBeginLoc(), "int"); | ||||||||||||
1357 | } | ||||||||||||
1358 | } else if (!DS.hasTypeSpecifier()) { | ||||||||||||
1359 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: | ||||||||||||
1360 | // "At least one type specifier shall be given in the declaration | ||||||||||||
1361 | // specifiers in each declaration, and in the specifier-qualifier list in | ||||||||||||
1362 | // each struct declaration and type name." | ||||||||||||
1363 | if (S.getLangOpts().CPlusPlus && !DS.isTypeSpecPipe()) { | ||||||||||||
1364 | S.Diag(DeclLoc, diag::err_missing_type_specifier) | ||||||||||||
1365 | << DS.getSourceRange(); | ||||||||||||
1366 | |||||||||||||
1367 | // When this occurs in C++ code, often something is very broken with the | ||||||||||||
1368 | // value being declared, poison it as invalid so we don't get chains of | ||||||||||||
1369 | // errors. | ||||||||||||
1370 | declarator.setInvalidType(true); | ||||||||||||
1371 | } else if ((S.getLangOpts().OpenCLVersion >= 200 || | ||||||||||||
1372 | S.getLangOpts().OpenCLCPlusPlus) && | ||||||||||||
1373 | DS.isTypeSpecPipe()) { | ||||||||||||
1374 | S.Diag(DeclLoc, diag::err_missing_actual_pipe_type) | ||||||||||||
1375 | << DS.getSourceRange(); | ||||||||||||
1376 | declarator.setInvalidType(true); | ||||||||||||
1377 | } else { | ||||||||||||
1378 | S.Diag(DeclLoc, diag::ext_missing_type_specifier) | ||||||||||||
1379 | << DS.getSourceRange(); | ||||||||||||
1380 | } | ||||||||||||
1381 | } | ||||||||||||
1382 | |||||||||||||
1383 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
1384 | case DeclSpec::TST_int: { | ||||||||||||
1385 | if (DS.getTypeSpecSign() != TypeSpecifierSign::Unsigned) { | ||||||||||||
1386 | switch (DS.getTypeSpecWidth()) { | ||||||||||||
1387 | case TypeSpecifierWidth::Unspecified: | ||||||||||||
1388 | Result = Context.IntTy; | ||||||||||||
1389 | break; | ||||||||||||
1390 | case TypeSpecifierWidth::Short: | ||||||||||||
1391 | Result = Context.ShortTy; | ||||||||||||
1392 | break; | ||||||||||||
1393 | case TypeSpecifierWidth::Long: | ||||||||||||
1394 | Result = Context.LongTy; | ||||||||||||
1395 | break; | ||||||||||||
1396 | case TypeSpecifierWidth::LongLong: | ||||||||||||
1397 | Result = Context.LongLongTy; | ||||||||||||
1398 | |||||||||||||
1399 | // 'long long' is a C99 or C++11 feature. | ||||||||||||
1400 | if (!S.getLangOpts().C99) { | ||||||||||||
1401 | if (S.getLangOpts().CPlusPlus) | ||||||||||||
1402 | S.Diag(DS.getTypeSpecWidthLoc(), | ||||||||||||
1403 | S.getLangOpts().CPlusPlus11 ? | ||||||||||||
1404 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); | ||||||||||||
1405 | else | ||||||||||||
1406 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); | ||||||||||||
1407 | } | ||||||||||||
1408 | break; | ||||||||||||
1409 | } | ||||||||||||
1410 | } else { | ||||||||||||
1411 | switch (DS.getTypeSpecWidth()) { | ||||||||||||
1412 | case TypeSpecifierWidth::Unspecified: | ||||||||||||
1413 | Result = Context.UnsignedIntTy; | ||||||||||||
1414 | break; | ||||||||||||
1415 | case TypeSpecifierWidth::Short: | ||||||||||||
1416 | Result = Context.UnsignedShortTy; | ||||||||||||
1417 | break; | ||||||||||||
1418 | case TypeSpecifierWidth::Long: | ||||||||||||
1419 | Result = Context.UnsignedLongTy; | ||||||||||||
1420 | break; | ||||||||||||
1421 | case TypeSpecifierWidth::LongLong: | ||||||||||||
1422 | Result = Context.UnsignedLongLongTy; | ||||||||||||
1423 | |||||||||||||
1424 | // 'long long' is a C99 or C++11 feature. | ||||||||||||
1425 | if (!S.getLangOpts().C99) { | ||||||||||||
1426 | if (S.getLangOpts().CPlusPlus) | ||||||||||||
1427 | S.Diag(DS.getTypeSpecWidthLoc(), | ||||||||||||
1428 | S.getLangOpts().CPlusPlus11 ? | ||||||||||||
1429 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); | ||||||||||||
1430 | else | ||||||||||||
1431 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); | ||||||||||||
1432 | } | ||||||||||||
1433 | break; | ||||||||||||
1434 | } | ||||||||||||
1435 | } | ||||||||||||
1436 | break; | ||||||||||||
1437 | } | ||||||||||||
1438 | case DeclSpec::TST_extint: { | ||||||||||||
1439 | if (!S.Context.getTargetInfo().hasExtIntType()) | ||||||||||||
1440 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) | ||||||||||||
1441 | << "_ExtInt"; | ||||||||||||
1442 | Result = | ||||||||||||
1443 | S.BuildExtIntType(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned, | ||||||||||||
1444 | DS.getRepAsExpr(), DS.getBeginLoc()); | ||||||||||||
1445 | if (Result.isNull()) { | ||||||||||||
1446 | Result = Context.IntTy; | ||||||||||||
1447 | declarator.setInvalidType(true); | ||||||||||||
1448 | } | ||||||||||||
1449 | break; | ||||||||||||
1450 | } | ||||||||||||
1451 | case DeclSpec::TST_accum: { | ||||||||||||
1452 | switch (DS.getTypeSpecWidth()) { | ||||||||||||
1453 | case TypeSpecifierWidth::Short: | ||||||||||||
1454 | Result = Context.ShortAccumTy; | ||||||||||||
1455 | break; | ||||||||||||
1456 | case TypeSpecifierWidth::Unspecified: | ||||||||||||
1457 | Result = Context.AccumTy; | ||||||||||||
1458 | break; | ||||||||||||
1459 | case TypeSpecifierWidth::Long: | ||||||||||||
1460 | Result = Context.LongAccumTy; | ||||||||||||
1461 | break; | ||||||||||||
1462 | case TypeSpecifierWidth::LongLong: | ||||||||||||
1463 | llvm_unreachable("Unable to specify long long as _Accum width")::llvm::llvm_unreachable_internal("Unable to specify long long as _Accum width" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1463); | ||||||||||||
1464 | } | ||||||||||||
1465 | |||||||||||||
1466 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) | ||||||||||||
1467 | Result = Context.getCorrespondingUnsignedType(Result); | ||||||||||||
1468 | |||||||||||||
1469 | if (DS.isTypeSpecSat()) | ||||||||||||
1470 | Result = Context.getCorrespondingSaturatedType(Result); | ||||||||||||
1471 | |||||||||||||
1472 | break; | ||||||||||||
1473 | } | ||||||||||||
1474 | case DeclSpec::TST_fract: { | ||||||||||||
1475 | switch (DS.getTypeSpecWidth()) { | ||||||||||||
1476 | case TypeSpecifierWidth::Short: | ||||||||||||
1477 | Result = Context.ShortFractTy; | ||||||||||||
1478 | break; | ||||||||||||
1479 | case TypeSpecifierWidth::Unspecified: | ||||||||||||
1480 | Result = Context.FractTy; | ||||||||||||
1481 | break; | ||||||||||||
1482 | case TypeSpecifierWidth::Long: | ||||||||||||
1483 | Result = Context.LongFractTy; | ||||||||||||
1484 | break; | ||||||||||||
1485 | case TypeSpecifierWidth::LongLong: | ||||||||||||
1486 | llvm_unreachable("Unable to specify long long as _Fract width")::llvm::llvm_unreachable_internal("Unable to specify long long as _Fract width" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1486); | ||||||||||||
1487 | } | ||||||||||||
1488 | |||||||||||||
1489 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) | ||||||||||||
1490 | Result = Context.getCorrespondingUnsignedType(Result); | ||||||||||||
1491 | |||||||||||||
1492 | if (DS.isTypeSpecSat()) | ||||||||||||
1493 | Result = Context.getCorrespondingSaturatedType(Result); | ||||||||||||
1494 | |||||||||||||
1495 | break; | ||||||||||||
1496 | } | ||||||||||||
1497 | case DeclSpec::TST_int128: | ||||||||||||
1498 | if (!S.Context.getTargetInfo().hasInt128Type() && | ||||||||||||
1499 | !S.getLangOpts().SYCLIsDevice && | ||||||||||||
1500 | !(S.getLangOpts().OpenMP && S.getLangOpts().OpenMPIsDevice)) | ||||||||||||
1501 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) | ||||||||||||
1502 | << "__int128"; | ||||||||||||
1503 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) | ||||||||||||
1504 | Result = Context.UnsignedInt128Ty; | ||||||||||||
1505 | else | ||||||||||||
1506 | Result = Context.Int128Ty; | ||||||||||||
1507 | break; | ||||||||||||
1508 | case DeclSpec::TST_float16: | ||||||||||||
1509 | // CUDA host and device may have different _Float16 support, therefore | ||||||||||||
1510 | // do not diagnose _Float16 usage to avoid false alarm. | ||||||||||||
1511 | // ToDo: more precise diagnostics for CUDA. | ||||||||||||
1512 | if (!S.Context.getTargetInfo().hasFloat16Type() && !S.getLangOpts().CUDA && | ||||||||||||
1513 | !(S.getLangOpts().OpenMP && S.getLangOpts().OpenMPIsDevice)) | ||||||||||||
1514 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) | ||||||||||||
1515 | << "_Float16"; | ||||||||||||
1516 | Result = Context.Float16Ty; | ||||||||||||
1517 | break; | ||||||||||||
1518 | case DeclSpec::TST_half: Result = Context.HalfTy; break; | ||||||||||||
1519 | case DeclSpec::TST_BFloat16: | ||||||||||||
1520 | if (!S.Context.getTargetInfo().hasBFloat16Type()) | ||||||||||||
1521 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) | ||||||||||||
1522 | << "__bf16"; | ||||||||||||
1523 | Result = Context.BFloat16Ty; | ||||||||||||
1524 | break; | ||||||||||||
1525 | case DeclSpec::TST_float: Result = Context.FloatTy; break; | ||||||||||||
1526 | case DeclSpec::TST_double: | ||||||||||||
1527 | if (DS.getTypeSpecWidth() == TypeSpecifierWidth::Long) | ||||||||||||
1528 | Result = Context.LongDoubleTy; | ||||||||||||
1529 | else | ||||||||||||
1530 | Result = Context.DoubleTy; | ||||||||||||
1531 | break; | ||||||||||||
1532 | case DeclSpec::TST_float128: | ||||||||||||
1533 | if (!S.Context.getTargetInfo().hasFloat128Type() && | ||||||||||||
1534 | !S.getLangOpts().SYCLIsDevice && | ||||||||||||
1535 | !(S.getLangOpts().OpenMP && S.getLangOpts().OpenMPIsDevice)) | ||||||||||||
1536 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) | ||||||||||||
1537 | << "__float128"; | ||||||||||||
1538 | Result = Context.Float128Ty; | ||||||||||||
1539 | break; | ||||||||||||
1540 | case DeclSpec::TST_bool: | ||||||||||||
1541 | Result = Context.BoolTy; // _Bool or bool | ||||||||||||
1542 | break; | ||||||||||||
1543 | case DeclSpec::TST_decimal32: // _Decimal32 | ||||||||||||
1544 | case DeclSpec::TST_decimal64: // _Decimal64 | ||||||||||||
1545 | case DeclSpec::TST_decimal128: // _Decimal128 | ||||||||||||
1546 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); | ||||||||||||
1547 | Result = Context.IntTy; | ||||||||||||
1548 | declarator.setInvalidType(true); | ||||||||||||
1549 | break; | ||||||||||||
1550 | case DeclSpec::TST_class: | ||||||||||||
1551 | case DeclSpec::TST_enum: | ||||||||||||
1552 | case DeclSpec::TST_union: | ||||||||||||
1553 | case DeclSpec::TST_struct: | ||||||||||||
1554 | case DeclSpec::TST_interface: { | ||||||||||||
1555 | TagDecl *D = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl()); | ||||||||||||
1556 | if (!D) { | ||||||||||||
1557 | // This can happen in C++ with ambiguous lookups. | ||||||||||||
1558 | Result = Context.IntTy; | ||||||||||||
1559 | declarator.setInvalidType(true); | ||||||||||||
1560 | break; | ||||||||||||
1561 | } | ||||||||||||
1562 | |||||||||||||
1563 | // If the type is deprecated or unavailable, diagnose it. | ||||||||||||
1564 | S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc()); | ||||||||||||
1565 | |||||||||||||
1566 | assert(DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "No qualifiers on tag names!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"No qualifiers on tag names!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1569, __PRETTY_FUNCTION__)) | ||||||||||||
1567 | DS.getTypeSpecComplex() == 0 &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "No qualifiers on tag names!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"No qualifiers on tag names!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1569, __PRETTY_FUNCTION__)) | ||||||||||||
1568 | DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "No qualifiers on tag names!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"No qualifiers on tag names!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1569, __PRETTY_FUNCTION__)) | ||||||||||||
1569 | "No qualifiers on tag names!")((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "No qualifiers on tag names!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"No qualifiers on tag names!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1569, __PRETTY_FUNCTION__)); | ||||||||||||
1570 | |||||||||||||
1571 | // TypeQuals handled by caller. | ||||||||||||
1572 | Result = Context.getTypeDeclType(D); | ||||||||||||
1573 | |||||||||||||
1574 | // In both C and C++, make an ElaboratedType. | ||||||||||||
1575 | ElaboratedTypeKeyword Keyword | ||||||||||||
1576 | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); | ||||||||||||
1577 | Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result, | ||||||||||||
1578 | DS.isTypeSpecOwned() ? D : nullptr); | ||||||||||||
1579 | break; | ||||||||||||
1580 | } | ||||||||||||
1581 | case DeclSpec::TST_typename: { | ||||||||||||
1582 | assert(DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Can't handle qualifiers on typedef names yet!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Can't handle qualifiers on typedef names yet!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1585, __PRETTY_FUNCTION__)) | ||||||||||||
1583 | DS.getTypeSpecComplex() == 0 &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Can't handle qualifiers on typedef names yet!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Can't handle qualifiers on typedef names yet!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1585, __PRETTY_FUNCTION__)) | ||||||||||||
1584 | DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified &&((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Can't handle qualifiers on typedef names yet!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Can't handle qualifiers on typedef names yet!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1585, __PRETTY_FUNCTION__)) | ||||||||||||
1585 | "Can't handle qualifiers on typedef names yet!")((DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && "Can't handle qualifiers on typedef names yet!" ) ? static_cast<void> (0) : __assert_fail ("DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && DS.getTypeSpecComplex() == 0 && DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && \"Can't handle qualifiers on typedef names yet!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1585, __PRETTY_FUNCTION__)); | ||||||||||||
1586 | Result = S.GetTypeFromParser(DS.getRepAsType()); | ||||||||||||
1587 | if (Result.isNull()) { | ||||||||||||
1588 | declarator.setInvalidType(true); | ||||||||||||
1589 | } | ||||||||||||
1590 | |||||||||||||
1591 | // TypeQuals handled by caller. | ||||||||||||
1592 | break; | ||||||||||||
1593 | } | ||||||||||||
1594 | case DeclSpec::TST_typeofType: | ||||||||||||
1595 | // FIXME: Preserve type source info. | ||||||||||||
1596 | Result = S.GetTypeFromParser(DS.getRepAsType()); | ||||||||||||
1597 | assert(!Result.isNull() && "Didn't get a type for typeof?")((!Result.isNull() && "Didn't get a type for typeof?" ) ? static_cast<void> (0) : __assert_fail ("!Result.isNull() && \"Didn't get a type for typeof?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1597, __PRETTY_FUNCTION__)); | ||||||||||||
1598 | if (!Result->isDependentType()) | ||||||||||||
1599 | if (const TagType *TT = Result->getAs<TagType>()) | ||||||||||||
1600 | S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc()); | ||||||||||||
1601 | // TypeQuals handled by caller. | ||||||||||||
1602 | Result = Context.getTypeOfType(Result); | ||||||||||||
1603 | break; | ||||||||||||
1604 | case DeclSpec::TST_typeofExpr: { | ||||||||||||
1605 | Expr *E = DS.getRepAsExpr(); | ||||||||||||
1606 | assert(E && "Didn't get an expression for typeof?")((E && "Didn't get an expression for typeof?") ? static_cast <void> (0) : __assert_fail ("E && \"Didn't get an expression for typeof?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1606, __PRETTY_FUNCTION__)); | ||||||||||||
1607 | // TypeQuals handled by caller. | ||||||||||||
1608 | Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc()); | ||||||||||||
1609 | if (Result.isNull()) { | ||||||||||||
1610 | Result = Context.IntTy; | ||||||||||||
1611 | declarator.setInvalidType(true); | ||||||||||||
1612 | } | ||||||||||||
1613 | break; | ||||||||||||
1614 | } | ||||||||||||
1615 | case DeclSpec::TST_decltype: { | ||||||||||||
1616 | Expr *E = DS.getRepAsExpr(); | ||||||||||||
1617 | assert(E && "Didn't get an expression for decltype?")((E && "Didn't get an expression for decltype?") ? static_cast <void> (0) : __assert_fail ("E && \"Didn't get an expression for decltype?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1617, __PRETTY_FUNCTION__)); | ||||||||||||
1618 | // TypeQuals handled by caller. | ||||||||||||
1619 | Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc()); | ||||||||||||
1620 | if (Result.isNull()) { | ||||||||||||
1621 | Result = Context.IntTy; | ||||||||||||
1622 | declarator.setInvalidType(true); | ||||||||||||
1623 | } | ||||||||||||
1624 | break; | ||||||||||||
1625 | } | ||||||||||||
1626 | case DeclSpec::TST_underlyingType: | ||||||||||||
1627 | Result = S.GetTypeFromParser(DS.getRepAsType()); | ||||||||||||
1628 | assert(!Result.isNull() && "Didn't get a type for __underlying_type?")((!Result.isNull() && "Didn't get a type for __underlying_type?" ) ? static_cast<void> (0) : __assert_fail ("!Result.isNull() && \"Didn't get a type for __underlying_type?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1628, __PRETTY_FUNCTION__)); | ||||||||||||
1629 | Result = S.BuildUnaryTransformType(Result, | ||||||||||||
1630 | UnaryTransformType::EnumUnderlyingType, | ||||||||||||
1631 | DS.getTypeSpecTypeLoc()); | ||||||||||||
1632 | if (Result.isNull()) { | ||||||||||||
1633 | Result = Context.IntTy; | ||||||||||||
1634 | declarator.setInvalidType(true); | ||||||||||||
1635 | } | ||||||||||||
1636 | break; | ||||||||||||
1637 | |||||||||||||
1638 | case DeclSpec::TST_auto: | ||||||||||||
1639 | case DeclSpec::TST_decltype_auto: { | ||||||||||||
1640 | auto AutoKW = DS.getTypeSpecType() == DeclSpec::TST_decltype_auto | ||||||||||||
1641 | ? AutoTypeKeyword::DecltypeAuto | ||||||||||||
1642 | : AutoTypeKeyword::Auto; | ||||||||||||
1643 | |||||||||||||
1644 | ConceptDecl *TypeConstraintConcept = nullptr; | ||||||||||||
1645 | llvm::SmallVector<TemplateArgument, 8> TemplateArgs; | ||||||||||||
1646 | if (DS.isConstrainedAuto()) { | ||||||||||||
1647 | if (TemplateIdAnnotation *TemplateId = DS.getRepAsTemplateId()) { | ||||||||||||
1648 | TypeConstraintConcept = | ||||||||||||
1649 | cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl()); | ||||||||||||
1650 | TemplateArgumentListInfo TemplateArgsInfo; | ||||||||||||
1651 | TemplateArgsInfo.setLAngleLoc(TemplateId->LAngleLoc); | ||||||||||||
1652 | TemplateArgsInfo.setRAngleLoc(TemplateId->RAngleLoc); | ||||||||||||
1653 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), | ||||||||||||
1654 | TemplateId->NumArgs); | ||||||||||||
1655 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgsInfo); | ||||||||||||
1656 | for (const auto &ArgLoc : TemplateArgsInfo.arguments()) | ||||||||||||
1657 | TemplateArgs.push_back(ArgLoc.getArgument()); | ||||||||||||
1658 | } else { | ||||||||||||
1659 | declarator.setInvalidType(true); | ||||||||||||
1660 | } | ||||||||||||
1661 | } | ||||||||||||
1662 | Result = S.Context.getAutoType(QualType(), AutoKW, | ||||||||||||
1663 | /*IsDependent*/ false, /*IsPack=*/false, | ||||||||||||
1664 | TypeConstraintConcept, TemplateArgs); | ||||||||||||
1665 | break; | ||||||||||||
1666 | } | ||||||||||||
1667 | |||||||||||||
1668 | case DeclSpec::TST_auto_type: | ||||||||||||
1669 | Result = Context.getAutoType(QualType(), AutoTypeKeyword::GNUAutoType, false); | ||||||||||||
1670 | break; | ||||||||||||
1671 | |||||||||||||
1672 | case DeclSpec::TST_unknown_anytype: | ||||||||||||
1673 | Result = Context.UnknownAnyTy; | ||||||||||||
1674 | break; | ||||||||||||
1675 | |||||||||||||
1676 | case DeclSpec::TST_atomic: | ||||||||||||
1677 | Result = S.GetTypeFromParser(DS.getRepAsType()); | ||||||||||||
1678 | assert(!Result.isNull() && "Didn't get a type for _Atomic?")((!Result.isNull() && "Didn't get a type for _Atomic?" ) ? static_cast<void> (0) : __assert_fail ("!Result.isNull() && \"Didn't get a type for _Atomic?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1678, __PRETTY_FUNCTION__)); | ||||||||||||
1679 | Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc()); | ||||||||||||
1680 | if (Result.isNull()) { | ||||||||||||
1681 | Result = Context.IntTy; | ||||||||||||
1682 | declarator.setInvalidType(true); | ||||||||||||
1683 | } | ||||||||||||
1684 | break; | ||||||||||||
1685 | |||||||||||||
1686 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ | ||||||||||||
1687 | case DeclSpec::TST_##ImgType##_t: \ | ||||||||||||
1688 | switch (getImageAccess(DS.getAttributes())) { \ | ||||||||||||
1689 | case OpenCLAccessAttr::Keyword_write_only: \ | ||||||||||||
1690 | Result = Context.Id##WOTy; \ | ||||||||||||
1691 | break; \ | ||||||||||||
1692 | case OpenCLAccessAttr::Keyword_read_write: \ | ||||||||||||
1693 | Result = Context.Id##RWTy; \ | ||||||||||||
1694 | break; \ | ||||||||||||
1695 | case OpenCLAccessAttr::Keyword_read_only: \ | ||||||||||||
1696 | Result = Context.Id##ROTy; \ | ||||||||||||
1697 | break; \ | ||||||||||||
1698 | case OpenCLAccessAttr::SpellingNotCalculated: \ | ||||||||||||
1699 | llvm_unreachable("Spelling not yet calculated")::llvm::llvm_unreachable_internal("Spelling not yet calculated" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1699); \ | ||||||||||||
1700 | } \ | ||||||||||||
1701 | break; | ||||||||||||
1702 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||||||||
1703 | |||||||||||||
1704 | case DeclSpec::TST_error: | ||||||||||||
1705 | Result = Context.IntTy; | ||||||||||||
1706 | declarator.setInvalidType(true); | ||||||||||||
1707 | break; | ||||||||||||
1708 | } | ||||||||||||
1709 | |||||||||||||
1710 | // FIXME: we want resulting declarations to be marked invalid, but claiming | ||||||||||||
1711 | // the type is invalid is too strong - e.g. it causes ActOnTypeName to return | ||||||||||||
1712 | // a null type. | ||||||||||||
1713 | if (Result->containsErrors()) | ||||||||||||
1714 | declarator.setInvalidType(); | ||||||||||||
1715 | |||||||||||||
1716 | if (S.getLangOpts().OpenCL && | ||||||||||||
1717 | S.checkOpenCLDisabledTypeDeclSpec(DS, Result)) | ||||||||||||
1718 | declarator.setInvalidType(true); | ||||||||||||
1719 | |||||||||||||
1720 | bool IsFixedPointType = DS.getTypeSpecType() == DeclSpec::TST_accum || | ||||||||||||
1721 | DS.getTypeSpecType() == DeclSpec::TST_fract; | ||||||||||||
1722 | |||||||||||||
1723 | // Only fixed point types can be saturated | ||||||||||||
1724 | if (DS.isTypeSpecSat() && !IsFixedPointType) | ||||||||||||
1725 | S.Diag(DS.getTypeSpecSatLoc(), diag::err_invalid_saturation_spec) | ||||||||||||
1726 | << DS.getSpecifierName(DS.getTypeSpecType(), | ||||||||||||
1727 | Context.getPrintingPolicy()); | ||||||||||||
1728 | |||||||||||||
1729 | // Handle complex types. | ||||||||||||
1730 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { | ||||||||||||
1731 | if (S.getLangOpts().Freestanding) | ||||||||||||
1732 | S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); | ||||||||||||
1733 | Result = Context.getComplexType(Result); | ||||||||||||
1734 | } else if (DS.isTypeAltiVecVector()) { | ||||||||||||
1735 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); | ||||||||||||
1736 | assert(typeSize > 0 && "type size for vector must be greater than 0 bits")((typeSize > 0 && "type size for vector must be greater than 0 bits" ) ? static_cast<void> (0) : __assert_fail ("typeSize > 0 && \"type size for vector must be greater than 0 bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1736, __PRETTY_FUNCTION__)); | ||||||||||||
1737 | VectorType::VectorKind VecKind = VectorType::AltiVecVector; | ||||||||||||
1738 | if (DS.isTypeAltiVecPixel()) | ||||||||||||
1739 | VecKind = VectorType::AltiVecPixel; | ||||||||||||
1740 | else if (DS.isTypeAltiVecBool()) | ||||||||||||
1741 | VecKind = VectorType::AltiVecBool; | ||||||||||||
1742 | Result = Context.getVectorType(Result, 128/typeSize, VecKind); | ||||||||||||
1743 | } | ||||||||||||
1744 | |||||||||||||
1745 | // FIXME: Imaginary. | ||||||||||||
1746 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) | ||||||||||||
1747 | S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); | ||||||||||||
1748 | |||||||||||||
1749 | // Before we process any type attributes, synthesize a block literal | ||||||||||||
1750 | // function declarator if necessary. | ||||||||||||
1751 | if (declarator.getContext() == DeclaratorContext::BlockLiteral) | ||||||||||||
1752 | maybeSynthesizeBlockSignature(state, Result); | ||||||||||||
1753 | |||||||||||||
1754 | // Apply any type attributes from the decl spec. This may cause the | ||||||||||||
1755 | // list of type attributes to be temporarily saved while the type | ||||||||||||
1756 | // attributes are pushed around. | ||||||||||||
1757 | // pipe attributes will be handled later ( at GetFullTypeForDeclarator ) | ||||||||||||
1758 | if (!DS.isTypeSpecPipe()) | ||||||||||||
1759 | processTypeAttrs(state, Result, TAL_DeclSpec, DS.getAttributes()); | ||||||||||||
1760 | |||||||||||||
1761 | // Apply const/volatile/restrict qualifiers to T. | ||||||||||||
1762 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { | ||||||||||||
1763 | // Warn about CV qualifiers on function types. | ||||||||||||
1764 | // C99 6.7.3p8: | ||||||||||||
1765 | // If the specification of a function type includes any type qualifiers, | ||||||||||||
1766 | // the behavior is undefined. | ||||||||||||
1767 | // C++11 [dcl.fct]p7: | ||||||||||||
1768 | // The effect of a cv-qualifier-seq in a function declarator is not the | ||||||||||||
1769 | // same as adding cv-qualification on top of the function type. In the | ||||||||||||
1770 | // latter case, the cv-qualifiers are ignored. | ||||||||||||
1771 | if (Result->isFunctionType()) { | ||||||||||||
1772 | diagnoseAndRemoveTypeQualifiers( | ||||||||||||
1773 | S, DS, TypeQuals, Result, DeclSpec::TQ_const | DeclSpec::TQ_volatile, | ||||||||||||
1774 | S.getLangOpts().CPlusPlus | ||||||||||||
1775 | ? diag::warn_typecheck_function_qualifiers_ignored | ||||||||||||
1776 | : diag::warn_typecheck_function_qualifiers_unspecified); | ||||||||||||
1777 | // No diagnostic for 'restrict' or '_Atomic' applied to a | ||||||||||||
1778 | // function type; we'll diagnose those later, in BuildQualifiedType. | ||||||||||||
1779 | } | ||||||||||||
1780 | |||||||||||||
1781 | // C++11 [dcl.ref]p1: | ||||||||||||
1782 | // Cv-qualified references are ill-formed except when the | ||||||||||||
1783 | // cv-qualifiers are introduced through the use of a typedef-name | ||||||||||||
1784 | // or decltype-specifier, in which case the cv-qualifiers are ignored. | ||||||||||||
1785 | // | ||||||||||||
1786 | // There don't appear to be any other contexts in which a cv-qualified | ||||||||||||
1787 | // reference type could be formed, so the 'ill-formed' clause here appears | ||||||||||||
1788 | // to never happen. | ||||||||||||
1789 | if (TypeQuals && Result->isReferenceType()) { | ||||||||||||
1790 | diagnoseAndRemoveTypeQualifiers( | ||||||||||||
1791 | S, DS, TypeQuals, Result, | ||||||||||||
1792 | DeclSpec::TQ_const | DeclSpec::TQ_volatile | DeclSpec::TQ_atomic, | ||||||||||||
1793 | diag::warn_typecheck_reference_qualifiers); | ||||||||||||
1794 | } | ||||||||||||
1795 | |||||||||||||
1796 | // C90 6.5.3 constraints: "The same type qualifier shall not appear more | ||||||||||||
1797 | // than once in the same specifier-list or qualifier-list, either directly | ||||||||||||
1798 | // or via one or more typedefs." | ||||||||||||
1799 | if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus | ||||||||||||
1800 | && TypeQuals & Result.getCVRQualifiers()) { | ||||||||||||
1801 | if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) { | ||||||||||||
1802 | S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec) | ||||||||||||
1803 | << "const"; | ||||||||||||
1804 | } | ||||||||||||
1805 | |||||||||||||
1806 | if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) { | ||||||||||||
1807 | S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec) | ||||||||||||
1808 | << "volatile"; | ||||||||||||
1809 | } | ||||||||||||
1810 | |||||||||||||
1811 | // C90 doesn't have restrict nor _Atomic, so it doesn't force us to | ||||||||||||
1812 | // produce a warning in this case. | ||||||||||||
1813 | } | ||||||||||||
1814 | |||||||||||||
1815 | QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS); | ||||||||||||
1816 | |||||||||||||
1817 | // If adding qualifiers fails, just use the unqualified type. | ||||||||||||
1818 | if (Qualified.isNull()) | ||||||||||||
1819 | declarator.setInvalidType(true); | ||||||||||||
1820 | else | ||||||||||||
1821 | Result = Qualified; | ||||||||||||
1822 | } | ||||||||||||
1823 | |||||||||||||
1824 | assert(!Result.isNull() && "This function should not return a null type")((!Result.isNull() && "This function should not return a null type" ) ? static_cast<void> (0) : __assert_fail ("!Result.isNull() && \"This function should not return a null type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1824, __PRETTY_FUNCTION__)); | ||||||||||||
1825 | return Result; | ||||||||||||
1826 | } | ||||||||||||
1827 | |||||||||||||
1828 | static std::string getPrintableNameForEntity(DeclarationName Entity) { | ||||||||||||
1829 | if (Entity) | ||||||||||||
1830 | return Entity.getAsString(); | ||||||||||||
1831 | |||||||||||||
1832 | return "type name"; | ||||||||||||
1833 | } | ||||||||||||
1834 | |||||||||||||
1835 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, | ||||||||||||
1836 | Qualifiers Qs, const DeclSpec *DS) { | ||||||||||||
1837 | if (T.isNull()) | ||||||||||||
1838 | return QualType(); | ||||||||||||
1839 | |||||||||||||
1840 | // Ignore any attempt to form a cv-qualified reference. | ||||||||||||
1841 | if (T->isReferenceType()) { | ||||||||||||
1842 | Qs.removeConst(); | ||||||||||||
1843 | Qs.removeVolatile(); | ||||||||||||
1844 | } | ||||||||||||
1845 | |||||||||||||
1846 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from | ||||||||||||
1847 | // object or incomplete types shall not be restrict-qualified." | ||||||||||||
1848 | if (Qs.hasRestrict()) { | ||||||||||||
1849 | unsigned DiagID = 0; | ||||||||||||
1850 | QualType ProblemTy; | ||||||||||||
1851 | |||||||||||||
1852 | if (T->isAnyPointerType() || T->isReferenceType() || | ||||||||||||
1853 | T->isMemberPointerType()) { | ||||||||||||
1854 | QualType EltTy; | ||||||||||||
1855 | if (T->isObjCObjectPointerType()) | ||||||||||||
1856 | EltTy = T; | ||||||||||||
1857 | else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>()) | ||||||||||||
1858 | EltTy = PTy->getPointeeType(); | ||||||||||||
1859 | else | ||||||||||||
1860 | EltTy = T->getPointeeType(); | ||||||||||||
1861 | |||||||||||||
1862 | // If we have a pointer or reference, the pointee must have an object | ||||||||||||
1863 | // incomplete type. | ||||||||||||
1864 | if (!EltTy->isIncompleteOrObjectType()) { | ||||||||||||
1865 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; | ||||||||||||
1866 | ProblemTy = EltTy; | ||||||||||||
1867 | } | ||||||||||||
1868 | } else if (!T->isDependentType()) { | ||||||||||||
1869 | DiagID = diag::err_typecheck_invalid_restrict_not_pointer; | ||||||||||||
1870 | ProblemTy = T; | ||||||||||||
1871 | } | ||||||||||||
1872 | |||||||||||||
1873 | if (DiagID) { | ||||||||||||
1874 | Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy; | ||||||||||||
1875 | Qs.removeRestrict(); | ||||||||||||
1876 | } | ||||||||||||
1877 | } | ||||||||||||
1878 | |||||||||||||
1879 | return Context.getQualifiedType(T, Qs); | ||||||||||||
1880 | } | ||||||||||||
1881 | |||||||||||||
1882 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, | ||||||||||||
1883 | unsigned CVRAU, const DeclSpec *DS) { | ||||||||||||
1884 | if (T.isNull()) | ||||||||||||
1885 | return QualType(); | ||||||||||||
1886 | |||||||||||||
1887 | // Ignore any attempt to form a cv-qualified reference. | ||||||||||||
1888 | if (T->isReferenceType()) | ||||||||||||
1889 | CVRAU &= | ||||||||||||
1890 | ~(DeclSpec::TQ_const | DeclSpec::TQ_volatile | DeclSpec::TQ_atomic); | ||||||||||||
1891 | |||||||||||||
1892 | // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic and | ||||||||||||
1893 | // TQ_unaligned; | ||||||||||||
1894 | unsigned CVR = CVRAU & ~(DeclSpec::TQ_atomic | DeclSpec::TQ_unaligned); | ||||||||||||
1895 | |||||||||||||
1896 | // C11 6.7.3/5: | ||||||||||||
1897 | // If the same qualifier appears more than once in the same | ||||||||||||
1898 | // specifier-qualifier-list, either directly or via one or more typedefs, | ||||||||||||
1899 | // the behavior is the same as if it appeared only once. | ||||||||||||
1900 | // | ||||||||||||
1901 | // It's not specified what happens when the _Atomic qualifier is applied to | ||||||||||||
1902 | // a type specified with the _Atomic specifier, but we assume that this | ||||||||||||
1903 | // should be treated as if the _Atomic qualifier appeared multiple times. | ||||||||||||
1904 | if (CVRAU & DeclSpec::TQ_atomic && !T->isAtomicType()) { | ||||||||||||
1905 | // C11 6.7.3/5: | ||||||||||||
1906 | // If other qualifiers appear along with the _Atomic qualifier in a | ||||||||||||
1907 | // specifier-qualifier-list, the resulting type is the so-qualified | ||||||||||||
1908 | // atomic type. | ||||||||||||
1909 | // | ||||||||||||
1910 | // Don't need to worry about array types here, since _Atomic can't be | ||||||||||||
1911 | // applied to such types. | ||||||||||||
1912 | SplitQualType Split = T.getSplitUnqualifiedType(); | ||||||||||||
1913 | T = BuildAtomicType(QualType(Split.Ty, 0), | ||||||||||||
1914 | DS ? DS->getAtomicSpecLoc() : Loc); | ||||||||||||
1915 | if (T.isNull()) | ||||||||||||
1916 | return T; | ||||||||||||
1917 | Split.Quals.addCVRQualifiers(CVR); | ||||||||||||
1918 | return BuildQualifiedType(T, Loc, Split.Quals); | ||||||||||||
1919 | } | ||||||||||||
1920 | |||||||||||||
1921 | Qualifiers Q = Qualifiers::fromCVRMask(CVR); | ||||||||||||
1922 | Q.setUnaligned(CVRAU & DeclSpec::TQ_unaligned); | ||||||||||||
1923 | return BuildQualifiedType(T, Loc, Q, DS); | ||||||||||||
1924 | } | ||||||||||||
1925 | |||||||||||||
1926 | /// Build a paren type including \p T. | ||||||||||||
1927 | QualType Sema::BuildParenType(QualType T) { | ||||||||||||
1928 | return Context.getParenType(T); | ||||||||||||
1929 | } | ||||||||||||
1930 | |||||||||||||
1931 | /// Given that we're building a pointer or reference to the given | ||||||||||||
1932 | static QualType inferARCLifetimeForPointee(Sema &S, QualType type, | ||||||||||||
1933 | SourceLocation loc, | ||||||||||||
1934 | bool isReference) { | ||||||||||||
1935 | // Bail out if retention is unrequired or already specified. | ||||||||||||
1936 | if (!type->isObjCLifetimeType() || | ||||||||||||
1937 | type.getObjCLifetime() != Qualifiers::OCL_None) | ||||||||||||
1938 | return type; | ||||||||||||
1939 | |||||||||||||
1940 | Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None; | ||||||||||||
1941 | |||||||||||||
1942 | // If the object type is const-qualified, we can safely use | ||||||||||||
1943 | // __unsafe_unretained. This is safe (because there are no read | ||||||||||||
1944 | // barriers), and it'll be safe to coerce anything but __weak* to | ||||||||||||
1945 | // the resulting type. | ||||||||||||
1946 | if (type.isConstQualified()) { | ||||||||||||
1947 | implicitLifetime = Qualifiers::OCL_ExplicitNone; | ||||||||||||
1948 | |||||||||||||
1949 | // Otherwise, check whether the static type does not require | ||||||||||||
1950 | // retaining. This currently only triggers for Class (possibly | ||||||||||||
1951 | // protocol-qualifed, and arrays thereof). | ||||||||||||
1952 | } else if (type->isObjCARCImplicitlyUnretainedType()) { | ||||||||||||
1953 | implicitLifetime = Qualifiers::OCL_ExplicitNone; | ||||||||||||
1954 | |||||||||||||
1955 | // If we are in an unevaluated context, like sizeof, skip adding a | ||||||||||||
1956 | // qualification. | ||||||||||||
1957 | } else if (S.isUnevaluatedContext()) { | ||||||||||||
1958 | return type; | ||||||||||||
1959 | |||||||||||||
1960 | // If that failed, give an error and recover using __strong. __strong | ||||||||||||
1961 | // is the option most likely to prevent spurious second-order diagnostics, | ||||||||||||
1962 | // like when binding a reference to a field. | ||||||||||||
1963 | } else { | ||||||||||||
1964 | // These types can show up in private ivars in system headers, so | ||||||||||||
1965 | // we need this to not be an error in those cases. Instead we | ||||||||||||
1966 | // want to delay. | ||||||||||||
1967 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { | ||||||||||||
1968 | S.DelayedDiagnostics.add( | ||||||||||||
1969 | sema::DelayedDiagnostic::makeForbiddenType(loc, | ||||||||||||
1970 | diag::err_arc_indirect_no_ownership, type, isReference)); | ||||||||||||
1971 | } else { | ||||||||||||
1972 | S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference; | ||||||||||||
1973 | } | ||||||||||||
1974 | implicitLifetime = Qualifiers::OCL_Strong; | ||||||||||||
1975 | } | ||||||||||||
1976 | assert(implicitLifetime && "didn't infer any lifetime!")((implicitLifetime && "didn't infer any lifetime!") ? static_cast<void> (0) : __assert_fail ("implicitLifetime && \"didn't infer any lifetime!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 1976, __PRETTY_FUNCTION__)); | ||||||||||||
1977 | |||||||||||||
1978 | Qualifiers qs; | ||||||||||||
1979 | qs.addObjCLifetime(implicitLifetime); | ||||||||||||
1980 | return S.Context.getQualifiedType(type, qs); | ||||||||||||
1981 | } | ||||||||||||
1982 | |||||||||||||
1983 | static std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){ | ||||||||||||
1984 | std::string Quals = FnTy->getMethodQuals().getAsString(); | ||||||||||||
1985 | |||||||||||||
1986 | switch (FnTy->getRefQualifier()) { | ||||||||||||
1987 | case RQ_None: | ||||||||||||
1988 | break; | ||||||||||||
1989 | |||||||||||||
1990 | case RQ_LValue: | ||||||||||||
1991 | if (!Quals.empty()) | ||||||||||||
1992 | Quals += ' '; | ||||||||||||
1993 | Quals += '&'; | ||||||||||||
1994 | break; | ||||||||||||
1995 | |||||||||||||
1996 | case RQ_RValue: | ||||||||||||
1997 | if (!Quals.empty()) | ||||||||||||
1998 | Quals += ' '; | ||||||||||||
1999 | Quals += "&&"; | ||||||||||||
2000 | break; | ||||||||||||
2001 | } | ||||||||||||
2002 | |||||||||||||
2003 | return Quals; | ||||||||||||
2004 | } | ||||||||||||
2005 | |||||||||||||
2006 | namespace { | ||||||||||||
2007 | /// Kinds of declarator that cannot contain a qualified function type. | ||||||||||||
2008 | /// | ||||||||||||
2009 | /// C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: | ||||||||||||
2010 | /// a function type with a cv-qualifier or a ref-qualifier can only appear | ||||||||||||
2011 | /// at the topmost level of a type. | ||||||||||||
2012 | /// | ||||||||||||
2013 | /// Parens and member pointers are permitted. We don't diagnose array and | ||||||||||||
2014 | /// function declarators, because they don't allow function types at all. | ||||||||||||
2015 | /// | ||||||||||||
2016 | /// The values of this enum are used in diagnostics. | ||||||||||||
2017 | enum QualifiedFunctionKind { QFK_BlockPointer, QFK_Pointer, QFK_Reference }; | ||||||||||||
2018 | } // end anonymous namespace | ||||||||||||
2019 | |||||||||||||
2020 | /// Check whether the type T is a qualified function type, and if it is, | ||||||||||||
2021 | /// diagnose that it cannot be contained within the given kind of declarator. | ||||||||||||
2022 | static bool checkQualifiedFunction(Sema &S, QualType T, SourceLocation Loc, | ||||||||||||
2023 | QualifiedFunctionKind QFK) { | ||||||||||||
2024 | // Does T refer to a function type with a cv-qualifier or a ref-qualifier? | ||||||||||||
2025 | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); | ||||||||||||
2026 | if (!FPT || | ||||||||||||
2027 | (FPT->getMethodQuals().empty() && FPT->getRefQualifier() == RQ_None)) | ||||||||||||
2028 | return false; | ||||||||||||
2029 | |||||||||||||
2030 | S.Diag(Loc, diag::err_compound_qualified_function_type) | ||||||||||||
2031 | << QFK << isa<FunctionType>(T.IgnoreParens()) << T | ||||||||||||
2032 | << getFunctionQualifiersAsString(FPT); | ||||||||||||
2033 | return true; | ||||||||||||
2034 | } | ||||||||||||
2035 | |||||||||||||
2036 | bool Sema::CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc) { | ||||||||||||
2037 | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); | ||||||||||||
2038 | if (!FPT || | ||||||||||||
2039 | (FPT->getMethodQuals().empty() && FPT->getRefQualifier() == RQ_None)) | ||||||||||||
2040 | return false; | ||||||||||||
2041 | |||||||||||||
2042 | Diag(Loc, diag::err_qualified_function_typeid) | ||||||||||||
2043 | << T << getFunctionQualifiersAsString(FPT); | ||||||||||||
2044 | return true; | ||||||||||||
2045 | } | ||||||||||||
2046 | |||||||||||||
2047 | // Helper to deduce addr space of a pointee type in OpenCL mode. | ||||||||||||
2048 | static QualType deduceOpenCLPointeeAddrSpace(Sema &S, QualType PointeeType) { | ||||||||||||
2049 | if (!PointeeType->isUndeducedAutoType() && !PointeeType->isDependentType() && | ||||||||||||
2050 | !PointeeType->isSamplerT() && | ||||||||||||
2051 | !PointeeType.hasAddressSpace()) | ||||||||||||
2052 | PointeeType = S.getASTContext().getAddrSpaceQualType( | ||||||||||||
2053 | PointeeType, S.getLangOpts().OpenCLGenericAddressSpace | ||||||||||||
2054 | ? LangAS::opencl_generic | ||||||||||||
2055 | : LangAS::opencl_private); | ||||||||||||
2056 | return PointeeType; | ||||||||||||
2057 | } | ||||||||||||
2058 | |||||||||||||
2059 | /// Build a pointer type. | ||||||||||||
2060 | /// | ||||||||||||
2061 | /// \param T The type to which we'll be building a pointer. | ||||||||||||
2062 | /// | ||||||||||||
2063 | /// \param Loc The location of the entity whose type involves this | ||||||||||||
2064 | /// pointer type or, if there is no such entity, the location of the | ||||||||||||
2065 | /// type that will have pointer type. | ||||||||||||
2066 | /// | ||||||||||||
2067 | /// \param Entity The name of the entity that involves the pointer | ||||||||||||
2068 | /// type, if known. | ||||||||||||
2069 | /// | ||||||||||||
2070 | /// \returns A suitable pointer type, if there are no | ||||||||||||
2071 | /// errors. Otherwise, returns a NULL type. | ||||||||||||
2072 | QualType Sema::BuildPointerType(QualType T, | ||||||||||||
2073 | SourceLocation Loc, DeclarationName Entity) { | ||||||||||||
2074 | if (T->isReferenceType()) { | ||||||||||||
2075 | // C++ 8.3.2p4: There shall be no ... pointers to references ... | ||||||||||||
2076 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) | ||||||||||||
2077 | << getPrintableNameForEntity(Entity) << T; | ||||||||||||
2078 | return QualType(); | ||||||||||||
2079 | } | ||||||||||||
2080 | |||||||||||||
2081 | if (T->isFunctionType() && getLangOpts().OpenCL && | ||||||||||||
2082 | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", | ||||||||||||
2083 | getLangOpts())) { | ||||||||||||
2084 | Diag(Loc, diag::err_opencl_function_pointer) << /*pointer*/ 0; | ||||||||||||
2085 | return QualType(); | ||||||||||||
2086 | } | ||||||||||||
2087 | |||||||||||||
2088 | if (checkQualifiedFunction(*this, T, Loc, QFK_Pointer)) | ||||||||||||
2089 | return QualType(); | ||||||||||||
2090 | |||||||||||||
2091 | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType")((!T->isObjCObjectType() && "Should build ObjCObjectPointerType" ) ? static_cast<void> (0) : __assert_fail ("!T->isObjCObjectType() && \"Should build ObjCObjectPointerType\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2091, __PRETTY_FUNCTION__)); | ||||||||||||
2092 | |||||||||||||
2093 | // In ARC, it is forbidden to build pointers to unqualified pointers. | ||||||||||||
2094 | if (getLangOpts().ObjCAutoRefCount) | ||||||||||||
2095 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false); | ||||||||||||
2096 | |||||||||||||
2097 | if (getLangOpts().OpenCL) | ||||||||||||
2098 | T = deduceOpenCLPointeeAddrSpace(*this, T); | ||||||||||||
2099 | |||||||||||||
2100 | // Build the pointer type. | ||||||||||||
2101 | return Context.getPointerType(T); | ||||||||||||
2102 | } | ||||||||||||
2103 | |||||||||||||
2104 | /// Build a reference type. | ||||||||||||
2105 | /// | ||||||||||||
2106 | /// \param T The type to which we'll be building a reference. | ||||||||||||
2107 | /// | ||||||||||||
2108 | /// \param Loc The location of the entity whose type involves this | ||||||||||||
2109 | /// reference type or, if there is no such entity, the location of the | ||||||||||||
2110 | /// type that will have reference type. | ||||||||||||
2111 | /// | ||||||||||||
2112 | /// \param Entity The name of the entity that involves the reference | ||||||||||||
2113 | /// type, if known. | ||||||||||||
2114 | /// | ||||||||||||
2115 | /// \returns A suitable reference type, if there are no | ||||||||||||
2116 | /// errors. Otherwise, returns a NULL type. | ||||||||||||
2117 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, | ||||||||||||
2118 | SourceLocation Loc, | ||||||||||||
2119 | DeclarationName Entity) { | ||||||||||||
2120 | assert(Context.getCanonicalType(T) != Context.OverloadTy &&((Context.getCanonicalType(T) != Context.OverloadTy && "Unresolved overloaded function type") ? static_cast<void > (0) : __assert_fail ("Context.getCanonicalType(T) != Context.OverloadTy && \"Unresolved overloaded function type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2121, __PRETTY_FUNCTION__)) | ||||||||||||
2121 | "Unresolved overloaded function type")((Context.getCanonicalType(T) != Context.OverloadTy && "Unresolved overloaded function type") ? static_cast<void > (0) : __assert_fail ("Context.getCanonicalType(T) != Context.OverloadTy && \"Unresolved overloaded function type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2121, __PRETTY_FUNCTION__)); | ||||||||||||
2122 | |||||||||||||
2123 | // C++0x [dcl.ref]p6: | ||||||||||||
2124 | // If a typedef (7.1.3), a type template-parameter (14.3.1), or a | ||||||||||||
2125 | // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a | ||||||||||||
2126 | // type T, an attempt to create the type "lvalue reference to cv TR" creates | ||||||||||||
2127 | // the type "lvalue reference to T", while an attempt to create the type | ||||||||||||
2128 | // "rvalue reference to cv TR" creates the type TR. | ||||||||||||
2129 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); | ||||||||||||
2130 | |||||||||||||
2131 | // C++ [dcl.ref]p4: There shall be no references to references. | ||||||||||||
2132 | // | ||||||||||||
2133 | // According to C++ DR 106, references to references are only | ||||||||||||
2134 | // diagnosed when they are written directly (e.g., "int & &"), | ||||||||||||
2135 | // but not when they happen via a typedef: | ||||||||||||
2136 | // | ||||||||||||
2137 | // typedef int& intref; | ||||||||||||
2138 | // typedef intref& intref2; | ||||||||||||
2139 | // | ||||||||||||
2140 | // Parser::ParseDeclaratorInternal diagnoses the case where | ||||||||||||
2141 | // references are written directly; here, we handle the | ||||||||||||
2142 | // collapsing of references-to-references as described in C++0x. | ||||||||||||
2143 | // DR 106 and 540 introduce reference-collapsing into C++98/03. | ||||||||||||
2144 | |||||||||||||
2145 | // C++ [dcl.ref]p1: | ||||||||||||
2146 | // A declarator that specifies the type "reference to cv void" | ||||||||||||
2147 | // is ill-formed. | ||||||||||||
2148 | if (T->isVoidType()) { | ||||||||||||
2149 | Diag(Loc, diag::err_reference_to_void); | ||||||||||||
2150 | return QualType(); | ||||||||||||
2151 | } | ||||||||||||
2152 | |||||||||||||
2153 | if (checkQualifiedFunction(*this, T, Loc, QFK_Reference)) | ||||||||||||
2154 | return QualType(); | ||||||||||||
2155 | |||||||||||||
2156 | if (T->isFunctionType() && getLangOpts().OpenCL && | ||||||||||||
2157 | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", | ||||||||||||
2158 | getLangOpts())) { | ||||||||||||
2159 | Diag(Loc, diag::err_opencl_function_pointer) << /*reference*/ 1; | ||||||||||||
2160 | return QualType(); | ||||||||||||
2161 | } | ||||||||||||
2162 | |||||||||||||
2163 | // In ARC, it is forbidden to build references to unqualified pointers. | ||||||||||||
2164 | if (getLangOpts().ObjCAutoRefCount) | ||||||||||||
2165 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true); | ||||||||||||
2166 | |||||||||||||
2167 | if (getLangOpts().OpenCL) | ||||||||||||
2168 | T = deduceOpenCLPointeeAddrSpace(*this, T); | ||||||||||||
2169 | |||||||||||||
2170 | // Handle restrict on references. | ||||||||||||
2171 | if (LValueRef) | ||||||||||||
2172 | return Context.getLValueReferenceType(T, SpelledAsLValue); | ||||||||||||
2173 | return Context.getRValueReferenceType(T); | ||||||||||||
2174 | } | ||||||||||||
2175 | |||||||||||||
2176 | /// Build a Read-only Pipe type. | ||||||||||||
2177 | /// | ||||||||||||
2178 | /// \param T The type to which we'll be building a Pipe. | ||||||||||||
2179 | /// | ||||||||||||
2180 | /// \param Loc We do not use it for now. | ||||||||||||
2181 | /// | ||||||||||||
2182 | /// \returns A suitable pipe type, if there are no errors. Otherwise, returns a | ||||||||||||
2183 | /// NULL type. | ||||||||||||
2184 | QualType Sema::BuildReadPipeType(QualType T, SourceLocation Loc) { | ||||||||||||
2185 | return Context.getReadPipeType(T); | ||||||||||||
2186 | } | ||||||||||||
2187 | |||||||||||||
2188 | /// Build a Write-only Pipe type. | ||||||||||||
2189 | /// | ||||||||||||
2190 | /// \param T The type to which we'll be building a Pipe. | ||||||||||||
2191 | /// | ||||||||||||
2192 | /// \param Loc We do not use it for now. | ||||||||||||
2193 | /// | ||||||||||||
2194 | /// \returns A suitable pipe type, if there are no errors. Otherwise, returns a | ||||||||||||
2195 | /// NULL type. | ||||||||||||
2196 | QualType Sema::BuildWritePipeType(QualType T, SourceLocation Loc) { | ||||||||||||
2197 | return Context.getWritePipeType(T); | ||||||||||||
2198 | } | ||||||||||||
2199 | |||||||||||||
2200 | /// Build a extended int type. | ||||||||||||
2201 | /// | ||||||||||||
2202 | /// \param IsUnsigned Boolean representing the signedness of the type. | ||||||||||||
2203 | /// | ||||||||||||
2204 | /// \param BitWidth Size of this int type in bits, or an expression representing | ||||||||||||
2205 | /// that. | ||||||||||||
2206 | /// | ||||||||||||
2207 | /// \param Loc Location of the keyword. | ||||||||||||
2208 | QualType Sema::BuildExtIntType(bool IsUnsigned, Expr *BitWidth, | ||||||||||||
2209 | SourceLocation Loc) { | ||||||||||||
2210 | if (BitWidth->isInstantiationDependent()) | ||||||||||||
2211 | return Context.getDependentExtIntType(IsUnsigned, BitWidth); | ||||||||||||
2212 | |||||||||||||
2213 | llvm::APSInt Bits(32); | ||||||||||||
2214 | ExprResult ICE = | ||||||||||||
2215 | VerifyIntegerConstantExpression(BitWidth, &Bits, /*FIXME*/ AllowFold); | ||||||||||||
2216 | |||||||||||||
2217 | if (ICE.isInvalid()) | ||||||||||||
2218 | return QualType(); | ||||||||||||
2219 | |||||||||||||
2220 | int64_t NumBits = Bits.getSExtValue(); | ||||||||||||
2221 | if (!IsUnsigned && NumBits < 2) { | ||||||||||||
2222 | Diag(Loc, diag::err_ext_int_bad_size) << 0; | ||||||||||||
2223 | return QualType(); | ||||||||||||
2224 | } | ||||||||||||
2225 | |||||||||||||
2226 | if (IsUnsigned && NumBits < 1) { | ||||||||||||
2227 | Diag(Loc, diag::err_ext_int_bad_size) << 1; | ||||||||||||
2228 | return QualType(); | ||||||||||||
2229 | } | ||||||||||||
2230 | |||||||||||||
2231 | if (NumBits > llvm::IntegerType::MAX_INT_BITS) { | ||||||||||||
2232 | Diag(Loc, diag::err_ext_int_max_size) << IsUnsigned | ||||||||||||
2233 | << llvm::IntegerType::MAX_INT_BITS; | ||||||||||||
2234 | return QualType(); | ||||||||||||
2235 | } | ||||||||||||
2236 | |||||||||||||
2237 | return Context.getExtIntType(IsUnsigned, NumBits); | ||||||||||||
2238 | } | ||||||||||||
2239 | |||||||||||||
2240 | /// Check whether the specified array bound can be evaluated using the relevant | ||||||||||||
2241 | /// language rules. If so, returns the possibly-converted expression and sets | ||||||||||||
2242 | /// SizeVal to the size. If not, but the expression might be a VLA bound, | ||||||||||||
2243 | /// returns ExprResult(). Otherwise, produces a diagnostic and returns | ||||||||||||
2244 | /// ExprError(). | ||||||||||||
2245 | static ExprResult checkArraySize(Sema &S, Expr *&ArraySize, | ||||||||||||
2246 | llvm::APSInt &SizeVal, unsigned VLADiag, | ||||||||||||
2247 | bool VLAIsError) { | ||||||||||||
2248 | if (S.getLangOpts().CPlusPlus14 && | ||||||||||||
2249 | (VLAIsError || | ||||||||||||
2250 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType())) { | ||||||||||||
2251 | // C++14 [dcl.array]p1: | ||||||||||||
2252 | // The constant-expression shall be a converted constant expression of | ||||||||||||
2253 | // type std::size_t. | ||||||||||||
2254 | // | ||||||||||||
2255 | // Don't apply this rule if we might be forming a VLA: in that case, we | ||||||||||||
2256 | // allow non-constant expressions and constant-folding. We only need to use | ||||||||||||
2257 | // the converted constant expression rules (to properly convert the source) | ||||||||||||
2258 | // when the source expression is of class type. | ||||||||||||
2259 | return S.CheckConvertedConstantExpression( | ||||||||||||
2260 | ArraySize, S.Context.getSizeType(), SizeVal, Sema::CCEK_ArrayBound); | ||||||||||||
2261 | } | ||||||||||||
2262 | |||||||||||||
2263 | // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode | ||||||||||||
2264 | // (like gnu99, but not c99) accept any evaluatable value as an extension. | ||||||||||||
2265 | class VLADiagnoser : public Sema::VerifyICEDiagnoser { | ||||||||||||
2266 | public: | ||||||||||||
2267 | unsigned VLADiag; | ||||||||||||
2268 | bool VLAIsError; | ||||||||||||
2269 | bool IsVLA = false; | ||||||||||||
2270 | |||||||||||||
2271 | VLADiagnoser(unsigned VLADiag, bool VLAIsError) | ||||||||||||
2272 | : VLADiag(VLADiag), VLAIsError(VLAIsError) {} | ||||||||||||
2273 | |||||||||||||
2274 | Sema::SemaDiagnosticBuilder diagnoseNotICEType(Sema &S, SourceLocation Loc, | ||||||||||||
2275 | QualType T) override { | ||||||||||||
2276 | return S.Diag(Loc, diag::err_array_size_non_int) << T; | ||||||||||||
2277 | } | ||||||||||||
2278 | |||||||||||||
2279 | Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, | ||||||||||||
2280 | SourceLocation Loc) override { | ||||||||||||
2281 | IsVLA = !VLAIsError; | ||||||||||||
2282 | return S.Diag(Loc, VLADiag); | ||||||||||||
2283 | } | ||||||||||||
2284 | |||||||||||||
2285 | Sema::SemaDiagnosticBuilder diagnoseFold(Sema &S, | ||||||||||||
2286 | SourceLocation Loc) override { | ||||||||||||
2287 | return S.Diag(Loc, diag::ext_vla_folded_to_constant); | ||||||||||||
2288 | } | ||||||||||||
2289 | } Diagnoser(VLADiag, VLAIsError); | ||||||||||||
2290 | |||||||||||||
2291 | ExprResult R = | ||||||||||||
2292 | S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser); | ||||||||||||
2293 | if (Diagnoser.IsVLA) | ||||||||||||
2294 | return ExprResult(); | ||||||||||||
2295 | return R; | ||||||||||||
2296 | } | ||||||||||||
2297 | |||||||||||||
2298 | /// Build an array type. | ||||||||||||
2299 | /// | ||||||||||||
2300 | /// \param T The type of each element in the array. | ||||||||||||
2301 | /// | ||||||||||||
2302 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). | ||||||||||||
2303 | /// | ||||||||||||
2304 | /// \param ArraySize Expression describing the size of the array. | ||||||||||||
2305 | /// | ||||||||||||
2306 | /// \param Brackets The range from the opening '[' to the closing ']'. | ||||||||||||
2307 | /// | ||||||||||||
2308 | /// \param Entity The name of the entity that involves the array | ||||||||||||
2309 | /// type, if known. | ||||||||||||
2310 | /// | ||||||||||||
2311 | /// \returns A suitable array type, if there are no errors. Otherwise, | ||||||||||||
2312 | /// returns a NULL type. | ||||||||||||
2313 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, | ||||||||||||
2314 | Expr *ArraySize, unsigned Quals, | ||||||||||||
2315 | SourceRange Brackets, DeclarationName Entity) { | ||||||||||||
2316 | |||||||||||||
2317 | SourceLocation Loc = Brackets.getBegin(); | ||||||||||||
2318 | if (getLangOpts().CPlusPlus) { | ||||||||||||
2319 | // C++ [dcl.array]p1: | ||||||||||||
2320 | // T is called the array element type; this type shall not be a reference | ||||||||||||
2321 | // type, the (possibly cv-qualified) type void, a function type or an | ||||||||||||
2322 | // abstract class type. | ||||||||||||
2323 | // | ||||||||||||
2324 | // C++ [dcl.array]p3: | ||||||||||||
2325 | // When several "array of" specifications are adjacent, [...] only the | ||||||||||||
2326 | // first of the constant expressions that specify the bounds of the arrays | ||||||||||||
2327 | // may be omitted. | ||||||||||||
2328 | // | ||||||||||||
2329 | // Note: function types are handled in the common path with C. | ||||||||||||
2330 | if (T->isReferenceType()) { | ||||||||||||
2331 | Diag(Loc, diag::err_illegal_decl_array_of_references) | ||||||||||||
2332 | << getPrintableNameForEntity(Entity) << T; | ||||||||||||
2333 | return QualType(); | ||||||||||||
2334 | } | ||||||||||||
2335 | |||||||||||||
2336 | if (T->isVoidType() || T->isIncompleteArrayType()) { | ||||||||||||
2337 | Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 0 << T; | ||||||||||||
2338 | return QualType(); | ||||||||||||
2339 | } | ||||||||||||
2340 | |||||||||||||
2341 | if (RequireNonAbstractType(Brackets.getBegin(), T, | ||||||||||||
2342 | diag::err_array_of_abstract_type)) | ||||||||||||
2343 | return QualType(); | ||||||||||||
2344 | |||||||||||||
2345 | // Mentioning a member pointer type for an array type causes us to lock in | ||||||||||||
2346 | // an inheritance model, even if it's inside an unused typedef. | ||||||||||||
2347 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) | ||||||||||||
2348 | if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) | ||||||||||||
2349 | if (!MPTy->getClass()->isDependentType()) | ||||||||||||
2350 | (void)isCompleteType(Loc, T); | ||||||||||||
2351 | |||||||||||||
2352 | } else { | ||||||||||||
2353 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, | ||||||||||||
2354 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) | ||||||||||||
2355 | if (RequireCompleteSizedType(Loc, T, | ||||||||||||
2356 | diag::err_array_incomplete_or_sizeless_type)) | ||||||||||||
2357 | return QualType(); | ||||||||||||
2358 | } | ||||||||||||
2359 | |||||||||||||
2360 | if (T->isSizelessType()) { | ||||||||||||
2361 | Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 1 << T; | ||||||||||||
2362 | return QualType(); | ||||||||||||
2363 | } | ||||||||||||
2364 | |||||||||||||
2365 | if (T->isFunctionType()) { | ||||||||||||
2366 | Diag(Loc, diag::err_illegal_decl_array_of_functions) | ||||||||||||
2367 | << getPrintableNameForEntity(Entity) << T; | ||||||||||||
2368 | return QualType(); | ||||||||||||
2369 | } | ||||||||||||
2370 | |||||||||||||
2371 | if (const RecordType *EltTy = T->getAs<RecordType>()) { | ||||||||||||
2372 | // If the element type is a struct or union that contains a variadic | ||||||||||||
2373 | // array, accept it as a GNU extension: C99 6.7.2.1p2. | ||||||||||||
2374 | if (EltTy->getDecl()->hasFlexibleArrayMember()) | ||||||||||||
2375 | Diag(Loc, diag::ext_flexible_array_in_array) << T; | ||||||||||||
2376 | } else if (T->isObjCObjectType()) { | ||||||||||||
2377 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; | ||||||||||||
2378 | return QualType(); | ||||||||||||
2379 | } | ||||||||||||
2380 | |||||||||||||
2381 | // Do placeholder conversions on the array size expression. | ||||||||||||
2382 | if (ArraySize && ArraySize->hasPlaceholderType()) { | ||||||||||||
2383 | ExprResult Result = CheckPlaceholderExpr(ArraySize); | ||||||||||||
2384 | if (Result.isInvalid()) return QualType(); | ||||||||||||
2385 | ArraySize = Result.get(); | ||||||||||||
2386 | } | ||||||||||||
2387 | |||||||||||||
2388 | // Do lvalue-to-rvalue conversions on the array size expression. | ||||||||||||
2389 | if (ArraySize && !ArraySize->isRValue()) { | ||||||||||||
2390 | ExprResult Result = DefaultLvalueConversion(ArraySize); | ||||||||||||
2391 | if (Result.isInvalid()) | ||||||||||||
2392 | return QualType(); | ||||||||||||
2393 | |||||||||||||
2394 | ArraySize = Result.get(); | ||||||||||||
2395 | } | ||||||||||||
2396 | |||||||||||||
2397 | // C99 6.7.5.2p1: The size expression shall have integer type. | ||||||||||||
2398 | // C++11 allows contextual conversions to such types. | ||||||||||||
2399 | if (!getLangOpts().CPlusPlus11 && | ||||||||||||
2400 | ArraySize && !ArraySize->isTypeDependent() && | ||||||||||||
2401 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { | ||||||||||||
2402 | Diag(ArraySize->getBeginLoc(), diag::err_array_size_non_int) | ||||||||||||
2403 | << ArraySize->getType() << ArraySize->getSourceRange(); | ||||||||||||
2404 | return QualType(); | ||||||||||||
2405 | } | ||||||||||||
2406 | |||||||||||||
2407 | // VLAs always produce at least a -Wvla diagnostic, sometimes an error. | ||||||||||||
2408 | unsigned VLADiag; | ||||||||||||
2409 | bool VLAIsError; | ||||||||||||
2410 | if (getLangOpts().OpenCL) { | ||||||||||||
2411 | // OpenCL v1.2 s6.9.d: variable length arrays are not supported. | ||||||||||||
2412 | VLADiag = diag::err_opencl_vla; | ||||||||||||
2413 | VLAIsError = true; | ||||||||||||
2414 | } else if (getLangOpts().C99) { | ||||||||||||
2415 | VLADiag = diag::warn_vla_used; | ||||||||||||
2416 | VLAIsError = false; | ||||||||||||
2417 | } else if (isSFINAEContext()) { | ||||||||||||
2418 | VLADiag = diag::err_vla_in_sfinae; | ||||||||||||
2419 | VLAIsError = true; | ||||||||||||
2420 | } else { | ||||||||||||
2421 | VLADiag = diag::ext_vla; | ||||||||||||
2422 | VLAIsError = false; | ||||||||||||
2423 | } | ||||||||||||
2424 | |||||||||||||
2425 | llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); | ||||||||||||
2426 | if (!ArraySize) { | ||||||||||||
2427 | if (ASM == ArrayType::Star) { | ||||||||||||
2428 | Diag(Loc, VLADiag); | ||||||||||||
2429 | if (VLAIsError) | ||||||||||||
2430 | return QualType(); | ||||||||||||
2431 | |||||||||||||
2432 | T = Context.getVariableArrayType(T, nullptr, ASM, Quals, Brackets); | ||||||||||||
2433 | } else { | ||||||||||||
2434 | T = Context.getIncompleteArrayType(T, ASM, Quals); | ||||||||||||
2435 | } | ||||||||||||
2436 | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { | ||||||||||||
2437 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); | ||||||||||||
2438 | } else { | ||||||||||||
2439 | ExprResult R = | ||||||||||||
2440 | checkArraySize(*this, ArraySize, ConstVal, VLADiag, VLAIsError); | ||||||||||||
2441 | if (R.isInvalid()) | ||||||||||||
2442 | return QualType(); | ||||||||||||
2443 | |||||||||||||
2444 | if (!R.isUsable()) { | ||||||||||||
2445 | // C99: an array with a non-ICE size is a VLA. We accept any expression | ||||||||||||
2446 | // that we can fold to a non-zero positive value as a non-VLA as an | ||||||||||||
2447 | // extension. | ||||||||||||
2448 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); | ||||||||||||
2449 | } else if (!T->isDependentType() && !T->isIncompleteType() && | ||||||||||||
2450 | !T->isConstantSizeType()) { | ||||||||||||
2451 | // C99: an array with an element type that has a non-constant-size is a | ||||||||||||
2452 | // VLA. | ||||||||||||
2453 | // FIXME: Add a note to explain why this isn't a VLA. | ||||||||||||
2454 | Diag(Loc, VLADiag); | ||||||||||||
2455 | if (VLAIsError) | ||||||||||||
2456 | return QualType(); | ||||||||||||
2457 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); | ||||||||||||
2458 | } else { | ||||||||||||
2459 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall | ||||||||||||
2460 | // have a value greater than zero. | ||||||||||||
2461 | // In C++, this follows from narrowing conversions being disallowed. | ||||||||||||
2462 | if (ConstVal.isSigned() && ConstVal.isNegative()) { | ||||||||||||
2463 | if (Entity) | ||||||||||||
2464 | Diag(ArraySize->getBeginLoc(), diag::err_decl_negative_array_size) | ||||||||||||
2465 | << getPrintableNameForEntity(Entity) | ||||||||||||
2466 | << ArraySize->getSourceRange(); | ||||||||||||
2467 | else | ||||||||||||
2468 | Diag(ArraySize->getBeginLoc(), | ||||||||||||
2469 | diag::err_typecheck_negative_array_size) | ||||||||||||
2470 | << ArraySize->getSourceRange(); | ||||||||||||
2471 | return QualType(); | ||||||||||||
2472 | } | ||||||||||||
2473 | if (ConstVal == 0) { | ||||||||||||
2474 | // GCC accepts zero sized static arrays. We allow them when | ||||||||||||
2475 | // we're not in a SFINAE context. | ||||||||||||
2476 | Diag(ArraySize->getBeginLoc(), | ||||||||||||
2477 | isSFINAEContext() ? diag::err_typecheck_zero_array_size | ||||||||||||
2478 | : diag::ext_typecheck_zero_array_size) | ||||||||||||
2479 | << ArraySize->getSourceRange(); | ||||||||||||
2480 | } | ||||||||||||
2481 | |||||||||||||
2482 | // Is the array too large? | ||||||||||||
2483 | unsigned ActiveSizeBits = | ||||||||||||
2484 | (!T->isDependentType() && !T->isVariablyModifiedType() && | ||||||||||||
2485 | !T->isIncompleteType() && !T->isUndeducedType()) | ||||||||||||
2486 | ? ConstantArrayType::getNumAddressingBits(Context, T, ConstVal) | ||||||||||||
2487 | : ConstVal.getActiveBits(); | ||||||||||||
2488 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { | ||||||||||||
2489 | Diag(ArraySize->getBeginLoc(), diag::err_array_too_large) | ||||||||||||
2490 | << ConstVal.toString(10) << ArraySize->getSourceRange(); | ||||||||||||
2491 | return QualType(); | ||||||||||||
2492 | } | ||||||||||||
2493 | |||||||||||||
2494 | T = Context.getConstantArrayType(T, ConstVal, ArraySize, ASM, Quals); | ||||||||||||
2495 | } | ||||||||||||
2496 | } | ||||||||||||
2497 | |||||||||||||
2498 | if (T->isVariableArrayType() && !Context.getTargetInfo().isVLASupported()) { | ||||||||||||
2499 | // CUDA device code and some other targets don't support VLAs. | ||||||||||||
2500 | targetDiag(Loc, (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) | ||||||||||||
2501 | ? diag::err_cuda_vla | ||||||||||||
2502 | : diag::err_vla_unsupported) | ||||||||||||
2503 | << ((getLangOpts().CUDA && getLangOpts().CUDAIsDevice) | ||||||||||||
2504 | ? CurrentCUDATarget() | ||||||||||||
2505 | : CFT_InvalidTarget); | ||||||||||||
2506 | } | ||||||||||||
2507 | |||||||||||||
2508 | // If this is not C99, diagnose array size modifiers on non-VLAs. | ||||||||||||
2509 | if (!getLangOpts().C99 && !T->isVariableArrayType() && | ||||||||||||
2510 | (ASM != ArrayType::Normal || Quals != 0)) { | ||||||||||||
2511 | Diag(Loc, getLangOpts().CPlusPlus ? diag::err_c99_array_usage_cxx | ||||||||||||
2512 | : diag::ext_c99_array_usage) | ||||||||||||
2513 | << ASM; | ||||||||||||
2514 | } | ||||||||||||
2515 | |||||||||||||
2516 | // OpenCL v2.0 s6.12.5 - Arrays of blocks are not supported. | ||||||||||||
2517 | // OpenCL v2.0 s6.16.13.1 - Arrays of pipe type are not supported. | ||||||||||||
2518 | // OpenCL v2.0 s6.9.b - Arrays of image/sampler type are not supported. | ||||||||||||
2519 | if (getLangOpts().OpenCL) { | ||||||||||||
2520 | const QualType ArrType = Context.getBaseElementType(T); | ||||||||||||
2521 | if (ArrType->isBlockPointerType() || ArrType->isPipeType() || | ||||||||||||
2522 | ArrType->isSamplerT() || ArrType->isImageType()) { | ||||||||||||
2523 | Diag(Loc, diag::err_opencl_invalid_type_array) << ArrType; | ||||||||||||
2524 | return QualType(); | ||||||||||||
2525 | } | ||||||||||||
2526 | } | ||||||||||||
2527 | |||||||||||||
2528 | return T; | ||||||||||||
2529 | } | ||||||||||||
2530 | |||||||||||||
2531 | QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr, | ||||||||||||
2532 | SourceLocation AttrLoc) { | ||||||||||||
2533 | // The base type must be integer (not Boolean or enumeration) or float, and | ||||||||||||
2534 | // can't already be a vector. | ||||||||||||
2535 | if ((!CurType->isDependentType() && | ||||||||||||
2536 | (!CurType->isBuiltinType() || CurType->isBooleanType() || | ||||||||||||
2537 | (!CurType->isIntegerType() && !CurType->isRealFloatingType()))) || | ||||||||||||
2538 | CurType->isArrayType()) { | ||||||||||||
2539 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << CurType; | ||||||||||||
2540 | return QualType(); | ||||||||||||
2541 | } | ||||||||||||
2542 | |||||||||||||
2543 | if (SizeExpr->isTypeDependent() || SizeExpr->isValueDependent()) | ||||||||||||
2544 | return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, | ||||||||||||
2545 | VectorType::GenericVector); | ||||||||||||
2546 | |||||||||||||
2547 | Optional<llvm::APSInt> VecSize = SizeExpr->getIntegerConstantExpr(Context); | ||||||||||||
2548 | if (!VecSize) { | ||||||||||||
2549 | Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
2550 | << "vector_size" << AANT_ArgumentIntegerConstant | ||||||||||||
2551 | << SizeExpr->getSourceRange(); | ||||||||||||
2552 | return QualType(); | ||||||||||||
2553 | } | ||||||||||||
2554 | |||||||||||||
2555 | if (CurType->isDependentType()) | ||||||||||||
2556 | return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, | ||||||||||||
2557 | VectorType::GenericVector); | ||||||||||||
2558 | |||||||||||||
2559 | // vecSize is specified in bytes - convert to bits. | ||||||||||||
2560 | if (!VecSize->isIntN(61)) { | ||||||||||||
2561 | // Bit size will overflow uint64. | ||||||||||||
2562 | Diag(AttrLoc, diag::err_attribute_size_too_large) | ||||||||||||
2563 | << SizeExpr->getSourceRange() << "vector"; | ||||||||||||
2564 | return QualType(); | ||||||||||||
2565 | } | ||||||||||||
2566 | uint64_t VectorSizeBits = VecSize->getZExtValue() * 8; | ||||||||||||
2567 | unsigned TypeSize = static_cast<unsigned>(Context.getTypeSize(CurType)); | ||||||||||||
2568 | |||||||||||||
2569 | if (VectorSizeBits == 0) { | ||||||||||||
2570 | Diag(AttrLoc, diag::err_attribute_zero_size) | ||||||||||||
2571 | << SizeExpr->getSourceRange() << "vector"; | ||||||||||||
2572 | return QualType(); | ||||||||||||
2573 | } | ||||||||||||
2574 | |||||||||||||
2575 | if (VectorSizeBits % TypeSize) { | ||||||||||||
2576 | Diag(AttrLoc, diag::err_attribute_invalid_size) | ||||||||||||
2577 | << SizeExpr->getSourceRange(); | ||||||||||||
2578 | return QualType(); | ||||||||||||
2579 | } | ||||||||||||
2580 | |||||||||||||
2581 | if (VectorSizeBits / TypeSize > std::numeric_limits<uint32_t>::max()) { | ||||||||||||
2582 | Diag(AttrLoc, diag::err_attribute_size_too_large) | ||||||||||||
2583 | << SizeExpr->getSourceRange() << "vector"; | ||||||||||||
2584 | return QualType(); | ||||||||||||
2585 | } | ||||||||||||
2586 | |||||||||||||
2587 | return Context.getVectorType(CurType, VectorSizeBits / TypeSize, | ||||||||||||
2588 | VectorType::GenericVector); | ||||||||||||
2589 | } | ||||||||||||
2590 | |||||||||||||
2591 | /// Build an ext-vector type. | ||||||||||||
2592 | /// | ||||||||||||
2593 | /// Run the required checks for the extended vector type. | ||||||||||||
2594 | QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, | ||||||||||||
2595 | SourceLocation AttrLoc) { | ||||||||||||
2596 | // Unlike gcc's vector_size attribute, we do not allow vectors to be defined | ||||||||||||
2597 | // in conjunction with complex types (pointers, arrays, functions, etc.). | ||||||||||||
2598 | // | ||||||||||||
2599 | // Additionally, OpenCL prohibits vectors of booleans (they're considered a | ||||||||||||
2600 | // reserved data type under OpenCL v2.0 s6.1.4), we don't support selects | ||||||||||||
2601 | // on bitvectors, and we have no well-defined ABI for bitvectors, so vectors | ||||||||||||
2602 | // of bool aren't allowed. | ||||||||||||
2603 | if ((!T->isDependentType() && !T->isIntegerType() && | ||||||||||||
2604 | !T->isRealFloatingType()) || | ||||||||||||
2605 | T->isBooleanType()) { | ||||||||||||
2606 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; | ||||||||||||
2607 | return QualType(); | ||||||||||||
2608 | } | ||||||||||||
2609 | |||||||||||||
2610 | if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { | ||||||||||||
2611 | Optional<llvm::APSInt> vecSize = ArraySize->getIntegerConstantExpr(Context); | ||||||||||||
2612 | if (!vecSize) { | ||||||||||||
2613 | Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
2614 | << "ext_vector_type" << AANT_ArgumentIntegerConstant | ||||||||||||
2615 | << ArraySize->getSourceRange(); | ||||||||||||
2616 | return QualType(); | ||||||||||||
2617 | } | ||||||||||||
2618 | |||||||||||||
2619 | if (!vecSize->isIntN(32)) { | ||||||||||||
2620 | Diag(AttrLoc, diag::err_attribute_size_too_large) | ||||||||||||
2621 | << ArraySize->getSourceRange() << "vector"; | ||||||||||||
2622 | return QualType(); | ||||||||||||
2623 | } | ||||||||||||
2624 | // Unlike gcc's vector_size attribute, the size is specified as the | ||||||||||||
2625 | // number of elements, not the number of bytes. | ||||||||||||
2626 | unsigned vectorSize = static_cast<unsigned>(vecSize->getZExtValue()); | ||||||||||||
2627 | |||||||||||||
2628 | if (vectorSize == 0) { | ||||||||||||
2629 | Diag(AttrLoc, diag::err_attribute_zero_size) | ||||||||||||
2630 | << ArraySize->getSourceRange() << "vector"; | ||||||||||||
2631 | return QualType(); | ||||||||||||
2632 | } | ||||||||||||
2633 | |||||||||||||
2634 | return Context.getExtVectorType(T, vectorSize); | ||||||||||||
2635 | } | ||||||||||||
2636 | |||||||||||||
2637 | return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); | ||||||||||||
2638 | } | ||||||||||||
2639 | |||||||||||||
2640 | QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols, | ||||||||||||
2641 | SourceLocation AttrLoc) { | ||||||||||||
2642 | assert(Context.getLangOpts().MatrixTypes &&((Context.getLangOpts().MatrixTypes && "Should never build a matrix type when it is disabled" ) ? static_cast<void> (0) : __assert_fail ("Context.getLangOpts().MatrixTypes && \"Should never build a matrix type when it is disabled\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2643, __PRETTY_FUNCTION__)) | ||||||||||||
2643 | "Should never build a matrix type when it is disabled")((Context.getLangOpts().MatrixTypes && "Should never build a matrix type when it is disabled" ) ? static_cast<void> (0) : __assert_fail ("Context.getLangOpts().MatrixTypes && \"Should never build a matrix type when it is disabled\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2643, __PRETTY_FUNCTION__)); | ||||||||||||
2644 | |||||||||||||
2645 | // Check element type, if it is not dependent. | ||||||||||||
2646 | if (!ElementTy->isDependentType() && | ||||||||||||
2647 | !MatrixType::isValidElementType(ElementTy)) { | ||||||||||||
2648 | Diag(AttrLoc, diag::err_attribute_invalid_matrix_type) << ElementTy; | ||||||||||||
2649 | return QualType(); | ||||||||||||
2650 | } | ||||||||||||
2651 | |||||||||||||
2652 | if (NumRows->isTypeDependent() || NumCols->isTypeDependent() || | ||||||||||||
2653 | NumRows->isValueDependent() || NumCols->isValueDependent()) | ||||||||||||
2654 | return Context.getDependentSizedMatrixType(ElementTy, NumRows, NumCols, | ||||||||||||
2655 | AttrLoc); | ||||||||||||
2656 | |||||||||||||
2657 | Optional<llvm::APSInt> ValueRows = NumRows->getIntegerConstantExpr(Context); | ||||||||||||
2658 | Optional<llvm::APSInt> ValueColumns = | ||||||||||||
2659 | NumCols->getIntegerConstantExpr(Context); | ||||||||||||
2660 | |||||||||||||
2661 | auto const RowRange = NumRows->getSourceRange(); | ||||||||||||
2662 | auto const ColRange = NumCols->getSourceRange(); | ||||||||||||
2663 | |||||||||||||
2664 | // Both are row and column expressions are invalid. | ||||||||||||
2665 | if (!ValueRows && !ValueColumns) { | ||||||||||||
2666 | Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
2667 | << "matrix_type" << AANT_ArgumentIntegerConstant << RowRange | ||||||||||||
2668 | << ColRange; | ||||||||||||
2669 | return QualType(); | ||||||||||||
2670 | } | ||||||||||||
2671 | |||||||||||||
2672 | // Only the row expression is invalid. | ||||||||||||
2673 | if (!ValueRows) { | ||||||||||||
2674 | Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
2675 | << "matrix_type" << AANT_ArgumentIntegerConstant << RowRange; | ||||||||||||
2676 | return QualType(); | ||||||||||||
2677 | } | ||||||||||||
2678 | |||||||||||||
2679 | // Only the column expression is invalid. | ||||||||||||
2680 | if (!ValueColumns) { | ||||||||||||
2681 | Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
2682 | << "matrix_type" << AANT_ArgumentIntegerConstant << ColRange; | ||||||||||||
2683 | return QualType(); | ||||||||||||
2684 | } | ||||||||||||
2685 | |||||||||||||
2686 | // Check the matrix dimensions. | ||||||||||||
2687 | unsigned MatrixRows = static_cast<unsigned>(ValueRows->getZExtValue()); | ||||||||||||
2688 | unsigned MatrixColumns = static_cast<unsigned>(ValueColumns->getZExtValue()); | ||||||||||||
2689 | if (MatrixRows == 0 && MatrixColumns == 0) { | ||||||||||||
2690 | Diag(AttrLoc, diag::err_attribute_zero_size) | ||||||||||||
2691 | << "matrix" << RowRange << ColRange; | ||||||||||||
2692 | return QualType(); | ||||||||||||
2693 | } | ||||||||||||
2694 | if (MatrixRows == 0) { | ||||||||||||
2695 | Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << RowRange; | ||||||||||||
2696 | return QualType(); | ||||||||||||
2697 | } | ||||||||||||
2698 | if (MatrixColumns == 0) { | ||||||||||||
2699 | Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << ColRange; | ||||||||||||
2700 | return QualType(); | ||||||||||||
2701 | } | ||||||||||||
2702 | if (!ConstantMatrixType::isDimensionValid(MatrixRows)) { | ||||||||||||
2703 | Diag(AttrLoc, diag::err_attribute_size_too_large) | ||||||||||||
2704 | << RowRange << "matrix row"; | ||||||||||||
2705 | return QualType(); | ||||||||||||
2706 | } | ||||||||||||
2707 | if (!ConstantMatrixType::isDimensionValid(MatrixColumns)) { | ||||||||||||
2708 | Diag(AttrLoc, diag::err_attribute_size_too_large) | ||||||||||||
2709 | << ColRange << "matrix column"; | ||||||||||||
2710 | return QualType(); | ||||||||||||
2711 | } | ||||||||||||
2712 | return Context.getConstantMatrixType(ElementTy, MatrixRows, MatrixColumns); | ||||||||||||
2713 | } | ||||||||||||
2714 | |||||||||||||
2715 | bool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) { | ||||||||||||
2716 | if (T->isArrayType() || T->isFunctionType()) { | ||||||||||||
2717 | Diag(Loc, diag::err_func_returning_array_function) | ||||||||||||
2718 | << T->isFunctionType() << T; | ||||||||||||
2719 | return true; | ||||||||||||
2720 | } | ||||||||||||
2721 | |||||||||||||
2722 | // Functions cannot return half FP. | ||||||||||||
2723 | if (T->isHalfType() && !getLangOpts().HalfArgsAndReturns) { | ||||||||||||
2724 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 << | ||||||||||||
2725 | FixItHint::CreateInsertion(Loc, "*"); | ||||||||||||
2726 | return true; | ||||||||||||
2727 | } | ||||||||||||
2728 | |||||||||||||
2729 | // Methods cannot return interface types. All ObjC objects are | ||||||||||||
2730 | // passed by reference. | ||||||||||||
2731 | if (T->isObjCObjectType()) { | ||||||||||||
2732 | Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) | ||||||||||||
2733 | << 0 << T << FixItHint::CreateInsertion(Loc, "*"); | ||||||||||||
2734 | return true; | ||||||||||||
2735 | } | ||||||||||||
2736 | |||||||||||||
2737 | if (T.hasNonTrivialToPrimitiveDestructCUnion() || | ||||||||||||
2738 | T.hasNonTrivialToPrimitiveCopyCUnion()) | ||||||||||||
2739 | checkNonTrivialCUnion(T, Loc, NTCUC_FunctionReturn, | ||||||||||||
2740 | NTCUK_Destruct|NTCUK_Copy); | ||||||||||||
2741 | |||||||||||||
2742 | // C++2a [dcl.fct]p12: | ||||||||||||
2743 | // A volatile-qualified return type is deprecated | ||||||||||||
2744 | if (T.isVolatileQualified() && getLangOpts().CPlusPlus20) | ||||||||||||
2745 | Diag(Loc, diag::warn_deprecated_volatile_return) << T; | ||||||||||||
2746 | |||||||||||||
2747 | return false; | ||||||||||||
2748 | } | ||||||||||||
2749 | |||||||||||||
2750 | /// Check the extended parameter information. Most of the necessary | ||||||||||||
2751 | /// checking should occur when applying the parameter attribute; the | ||||||||||||
2752 | /// only other checks required are positional restrictions. | ||||||||||||
2753 | static void checkExtParameterInfos(Sema &S, ArrayRef<QualType> paramTypes, | ||||||||||||
2754 | const FunctionProtoType::ExtProtoInfo &EPI, | ||||||||||||
2755 | llvm::function_ref<SourceLocation(unsigned)> getParamLoc) { | ||||||||||||
2756 | assert(EPI.ExtParameterInfos && "shouldn't get here without param infos")((EPI.ExtParameterInfos && "shouldn't get here without param infos" ) ? static_cast<void> (0) : __assert_fail ("EPI.ExtParameterInfos && \"shouldn't get here without param infos\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2756, __PRETTY_FUNCTION__)); | ||||||||||||
2757 | |||||||||||||
2758 | bool hasCheckedSwiftCall = false; | ||||||||||||
2759 | auto checkForSwiftCC = [&](unsigned paramIndex) { | ||||||||||||
2760 | // Only do this once. | ||||||||||||
2761 | if (hasCheckedSwiftCall) return; | ||||||||||||
2762 | hasCheckedSwiftCall = true; | ||||||||||||
2763 | if (EPI.ExtInfo.getCC() == CC_Swift) return; | ||||||||||||
2764 | S.Diag(getParamLoc(paramIndex), diag::err_swift_param_attr_not_swiftcall) | ||||||||||||
2765 | << getParameterABISpelling(EPI.ExtParameterInfos[paramIndex].getABI()); | ||||||||||||
2766 | }; | ||||||||||||
2767 | |||||||||||||
2768 | for (size_t paramIndex = 0, numParams = paramTypes.size(); | ||||||||||||
2769 | paramIndex != numParams; ++paramIndex) { | ||||||||||||
2770 | switch (EPI.ExtParameterInfos[paramIndex].getABI()) { | ||||||||||||
2771 | // Nothing interesting to check for orindary-ABI parameters. | ||||||||||||
2772 | case ParameterABI::Ordinary: | ||||||||||||
2773 | continue; | ||||||||||||
2774 | |||||||||||||
2775 | // swift_indirect_result parameters must be a prefix of the function | ||||||||||||
2776 | // arguments. | ||||||||||||
2777 | case ParameterABI::SwiftIndirectResult: | ||||||||||||
2778 | checkForSwiftCC(paramIndex); | ||||||||||||
2779 | if (paramIndex != 0 && | ||||||||||||
2780 | EPI.ExtParameterInfos[paramIndex - 1].getABI() | ||||||||||||
2781 | != ParameterABI::SwiftIndirectResult) { | ||||||||||||
2782 | S.Diag(getParamLoc(paramIndex), | ||||||||||||
2783 | diag::err_swift_indirect_result_not_first); | ||||||||||||
2784 | } | ||||||||||||
2785 | continue; | ||||||||||||
2786 | |||||||||||||
2787 | case ParameterABI::SwiftContext: | ||||||||||||
2788 | checkForSwiftCC(paramIndex); | ||||||||||||
2789 | continue; | ||||||||||||
2790 | |||||||||||||
2791 | // swift_error parameters must be preceded by a swift_context parameter. | ||||||||||||
2792 | case ParameterABI::SwiftErrorResult: | ||||||||||||
2793 | checkForSwiftCC(paramIndex); | ||||||||||||
2794 | if (paramIndex == 0 || | ||||||||||||
2795 | EPI.ExtParameterInfos[paramIndex - 1].getABI() != | ||||||||||||
2796 | ParameterABI::SwiftContext) { | ||||||||||||
2797 | S.Diag(getParamLoc(paramIndex), | ||||||||||||
2798 | diag::err_swift_error_result_not_after_swift_context); | ||||||||||||
2799 | } | ||||||||||||
2800 | continue; | ||||||||||||
2801 | } | ||||||||||||
2802 | llvm_unreachable("bad ABI kind")::llvm::llvm_unreachable_internal("bad ABI kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 2802); | ||||||||||||
2803 | } | ||||||||||||
2804 | } | ||||||||||||
2805 | |||||||||||||
2806 | QualType Sema::BuildFunctionType(QualType T, | ||||||||||||
2807 | MutableArrayRef<QualType> ParamTypes, | ||||||||||||
2808 | SourceLocation Loc, DeclarationName Entity, | ||||||||||||
2809 | const FunctionProtoType::ExtProtoInfo &EPI) { | ||||||||||||
2810 | bool Invalid = false; | ||||||||||||
2811 | |||||||||||||
2812 | Invalid |= CheckFunctionReturnType(T, Loc); | ||||||||||||
2813 | |||||||||||||
2814 | for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) { | ||||||||||||
2815 | // FIXME: Loc is too inprecise here, should use proper locations for args. | ||||||||||||
2816 | QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]); | ||||||||||||
2817 | if (ParamType->isVoidType()) { | ||||||||||||
2818 | Diag(Loc, diag::err_param_with_void_type); | ||||||||||||
2819 | Invalid = true; | ||||||||||||
2820 | } else if (ParamType->isHalfType() && !getLangOpts().HalfArgsAndReturns) { | ||||||||||||
2821 | // Disallow half FP arguments. | ||||||||||||
2822 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 << | ||||||||||||
2823 | FixItHint::CreateInsertion(Loc, "*"); | ||||||||||||
2824 | Invalid = true; | ||||||||||||
2825 | } | ||||||||||||
2826 | |||||||||||||
2827 | // C++2a [dcl.fct]p4: | ||||||||||||
2828 | // A parameter with volatile-qualified type is deprecated | ||||||||||||
2829 | if (ParamType.isVolatileQualified() && getLangOpts().CPlusPlus20) | ||||||||||||
2830 | Diag(Loc, diag::warn_deprecated_volatile_param) << ParamType; | ||||||||||||
2831 | |||||||||||||
2832 | ParamTypes[Idx] = ParamType; | ||||||||||||
2833 | } | ||||||||||||
2834 | |||||||||||||
2835 | if (EPI.ExtParameterInfos) { | ||||||||||||
2836 | checkExtParameterInfos(*this, ParamTypes, EPI, | ||||||||||||
2837 | [=](unsigned i) { return Loc; }); | ||||||||||||
2838 | } | ||||||||||||
2839 | |||||||||||||
2840 | if (EPI.ExtInfo.getProducesResult()) { | ||||||||||||
2841 | // This is just a warning, so we can't fail to build if we see it. | ||||||||||||
2842 | checkNSReturnsRetainedReturnType(Loc, T); | ||||||||||||
2843 | } | ||||||||||||
2844 | |||||||||||||
2845 | if (Invalid) | ||||||||||||
2846 | return QualType(); | ||||||||||||
2847 | |||||||||||||
2848 | return Context.getFunctionType(T, ParamTypes, EPI); | ||||||||||||
2849 | } | ||||||||||||
2850 | |||||||||||||
2851 | /// Build a member pointer type \c T Class::*. | ||||||||||||
2852 | /// | ||||||||||||
2853 | /// \param T the type to which the member pointer refers. | ||||||||||||
2854 | /// \param Class the class type into which the member pointer points. | ||||||||||||
2855 | /// \param Loc the location where this type begins | ||||||||||||
2856 | /// \param Entity the name of the entity that will have this member pointer type | ||||||||||||
2857 | /// | ||||||||||||
2858 | /// \returns a member pointer type, if successful, or a NULL type if there was | ||||||||||||
2859 | /// an error. | ||||||||||||
2860 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, | ||||||||||||
2861 | SourceLocation Loc, | ||||||||||||
2862 | DeclarationName Entity) { | ||||||||||||
2863 | // Verify that we're not building a pointer to pointer to function with | ||||||||||||
2864 | // exception specification. | ||||||||||||
2865 | if (CheckDistantExceptionSpec(T)) { | ||||||||||||
2866 | Diag(Loc, diag::err_distant_exception_spec); | ||||||||||||
2867 | return QualType(); | ||||||||||||
2868 | } | ||||||||||||
2869 | |||||||||||||
2870 | // C++ 8.3.3p3: A pointer to member shall not point to ... a member | ||||||||||||
2871 | // with reference type, or "cv void." | ||||||||||||
2872 | if (T->isReferenceType()) { | ||||||||||||
2873 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) | ||||||||||||
2874 | << getPrintableNameForEntity(Entity) << T; | ||||||||||||
2875 | return QualType(); | ||||||||||||
2876 | } | ||||||||||||
2877 | |||||||||||||
2878 | if (T->isVoidType()) { | ||||||||||||
2879 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) | ||||||||||||
2880 | << getPrintableNameForEntity(Entity); | ||||||||||||
2881 | return QualType(); | ||||||||||||
2882 | } | ||||||||||||
2883 | |||||||||||||
2884 | if (!Class->isDependentType() && !Class->isRecordType()) { | ||||||||||||
2885 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; | ||||||||||||
2886 | return QualType(); | ||||||||||||
2887 | } | ||||||||||||
2888 | |||||||||||||
2889 | if (T->isFunctionType() && getLangOpts().OpenCL && | ||||||||||||
2890 | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", | ||||||||||||
2891 | getLangOpts())) { | ||||||||||||
2892 | Diag(Loc, diag::err_opencl_function_pointer) << /*pointer*/ 0; | ||||||||||||
2893 | return QualType(); | ||||||||||||
2894 | } | ||||||||||||
2895 | |||||||||||||
2896 | // Adjust the default free function calling convention to the default method | ||||||||||||
2897 | // calling convention. | ||||||||||||
2898 | bool IsCtorOrDtor = | ||||||||||||
2899 | (Entity.getNameKind() == DeclarationName::CXXConstructorName) || | ||||||||||||
2900 | (Entity.getNameKind() == DeclarationName::CXXDestructorName); | ||||||||||||
2901 | if (T->isFunctionType()) | ||||||||||||
2902 | adjustMemberFunctionCC(T, /*IsStatic=*/false, IsCtorOrDtor, Loc); | ||||||||||||
2903 | |||||||||||||
2904 | return Context.getMemberPointerType(T, Class.getTypePtr()); | ||||||||||||
2905 | } | ||||||||||||
2906 | |||||||||||||
2907 | /// Build a block pointer type. | ||||||||||||
2908 | /// | ||||||||||||
2909 | /// \param T The type to which we'll be building a block pointer. | ||||||||||||
2910 | /// | ||||||||||||
2911 | /// \param Loc The source location, used for diagnostics. | ||||||||||||
2912 | /// | ||||||||||||
2913 | /// \param Entity The name of the entity that involves the block pointer | ||||||||||||
2914 | /// type, if known. | ||||||||||||
2915 | /// | ||||||||||||
2916 | /// \returns A suitable block pointer type, if there are no | ||||||||||||
2917 | /// errors. Otherwise, returns a NULL type. | ||||||||||||
2918 | QualType Sema::BuildBlockPointerType(QualType T, | ||||||||||||
2919 | SourceLocation Loc, | ||||||||||||
2920 | DeclarationName Entity) { | ||||||||||||
2921 | if (!T->isFunctionType()) { | ||||||||||||
2922 | Diag(Loc, diag::err_nonfunction_block_type); | ||||||||||||
2923 | return QualType(); | ||||||||||||
2924 | } | ||||||||||||
2925 | |||||||||||||
2926 | if (checkQualifiedFunction(*this, T, Loc, QFK_BlockPointer)) | ||||||||||||
2927 | return QualType(); | ||||||||||||
2928 | |||||||||||||
2929 | if (getLangOpts().OpenCL) | ||||||||||||
2930 | T = deduceOpenCLPointeeAddrSpace(*this, T); | ||||||||||||
2931 | |||||||||||||
2932 | return Context.getBlockPointerType(T); | ||||||||||||
2933 | } | ||||||||||||
2934 | |||||||||||||
2935 | QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { | ||||||||||||
2936 | QualType QT = Ty.get(); | ||||||||||||
2937 | if (QT.isNull()) { | ||||||||||||
2938 | if (TInfo) *TInfo = nullptr; | ||||||||||||
2939 | return QualType(); | ||||||||||||
2940 | } | ||||||||||||
2941 | |||||||||||||
2942 | TypeSourceInfo *DI = nullptr; | ||||||||||||
2943 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { | ||||||||||||
2944 | QT = LIT->getType(); | ||||||||||||
2945 | DI = LIT->getTypeSourceInfo(); | ||||||||||||
2946 | } | ||||||||||||
2947 | |||||||||||||
2948 | if (TInfo
| ||||||||||||
2949 | return QT; | ||||||||||||
2950 | } | ||||||||||||
2951 | |||||||||||||
2952 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, | ||||||||||||
2953 | Qualifiers::ObjCLifetime ownership, | ||||||||||||
2954 | unsigned chunkIndex); | ||||||||||||
2955 | |||||||||||||
2956 | /// Given that this is the declaration of a parameter under ARC, | ||||||||||||
2957 | /// attempt to infer attributes and such for pointer-to-whatever | ||||||||||||
2958 | /// types. | ||||||||||||
2959 | static void inferARCWriteback(TypeProcessingState &state, | ||||||||||||
2960 | QualType &declSpecType) { | ||||||||||||
2961 | Sema &S = state.getSema(); | ||||||||||||
2962 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
2963 | |||||||||||||
2964 | // TODO: should we care about decl qualifiers? | ||||||||||||
2965 | |||||||||||||
2966 | // Check whether the declarator has the expected form. We walk | ||||||||||||
2967 | // from the inside out in order to make the block logic work. | ||||||||||||
2968 | unsigned outermostPointerIndex = 0; | ||||||||||||
2969 | bool isBlockPointer = false; | ||||||||||||
2970 | unsigned numPointers = 0; | ||||||||||||
2971 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
2972 | unsigned chunkIndex = i; | ||||||||||||
2973 | DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex); | ||||||||||||
2974 | switch (chunk.Kind) { | ||||||||||||
2975 | case DeclaratorChunk::Paren: | ||||||||||||
2976 | // Ignore parens. | ||||||||||||
2977 | break; | ||||||||||||
2978 | |||||||||||||
2979 | case DeclaratorChunk::Reference: | ||||||||||||
2980 | case DeclaratorChunk::Pointer: | ||||||||||||
2981 | // Count the number of pointers. Treat references | ||||||||||||
2982 | // interchangeably as pointers; if they're mis-ordered, normal | ||||||||||||
2983 | // type building will discover that. | ||||||||||||
2984 | outermostPointerIndex = chunkIndex; | ||||||||||||
2985 | numPointers++; | ||||||||||||
2986 | break; | ||||||||||||
2987 | |||||||||||||
2988 | case DeclaratorChunk::BlockPointer: | ||||||||||||
2989 | // If we have a pointer to block pointer, that's an acceptable | ||||||||||||
2990 | // indirect reference; anything else is not an application of | ||||||||||||
2991 | // the rules. | ||||||||||||
2992 | if (numPointers != 1) return; | ||||||||||||
2993 | numPointers++; | ||||||||||||
2994 | outermostPointerIndex = chunkIndex; | ||||||||||||
2995 | isBlockPointer = true; | ||||||||||||
2996 | |||||||||||||
2997 | // We don't care about pointer structure in return values here. | ||||||||||||
2998 | goto done; | ||||||||||||
2999 | |||||||||||||
3000 | case DeclaratorChunk::Array: // suppress if written (id[])? | ||||||||||||
3001 | case DeclaratorChunk::Function: | ||||||||||||
3002 | case DeclaratorChunk::MemberPointer: | ||||||||||||
3003 | case DeclaratorChunk::Pipe: | ||||||||||||
3004 | return; | ||||||||||||
3005 | } | ||||||||||||
3006 | } | ||||||||||||
3007 | done: | ||||||||||||
3008 | |||||||||||||
3009 | // If we have *one* pointer, then we want to throw the qualifier on | ||||||||||||
3010 | // the declaration-specifiers, which means that it needs to be a | ||||||||||||
3011 | // retainable object type. | ||||||||||||
3012 | if (numPointers == 1) { | ||||||||||||
3013 | // If it's not a retainable object type, the rule doesn't apply. | ||||||||||||
3014 | if (!declSpecType->isObjCRetainableType()) return; | ||||||||||||
3015 | |||||||||||||
3016 | // If it already has lifetime, don't do anything. | ||||||||||||
3017 | if (declSpecType.getObjCLifetime()) return; | ||||||||||||
3018 | |||||||||||||
3019 | // Otherwise, modify the type in-place. | ||||||||||||
3020 | Qualifiers qs; | ||||||||||||
3021 | |||||||||||||
3022 | if (declSpecType->isObjCARCImplicitlyUnretainedType()) | ||||||||||||
3023 | qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone); | ||||||||||||
3024 | else | ||||||||||||
3025 | qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing); | ||||||||||||
3026 | declSpecType = S.Context.getQualifiedType(declSpecType, qs); | ||||||||||||
3027 | |||||||||||||
3028 | // If we have *two* pointers, then we want to throw the qualifier on | ||||||||||||
3029 | // the outermost pointer. | ||||||||||||
3030 | } else if (numPointers == 2) { | ||||||||||||
3031 | // If we don't have a block pointer, we need to check whether the | ||||||||||||
3032 | // declaration-specifiers gave us something that will turn into a | ||||||||||||
3033 | // retainable object pointer after we slap the first pointer on it. | ||||||||||||
3034 | if (!isBlockPointer && !declSpecType->isObjCObjectType()) | ||||||||||||
3035 | return; | ||||||||||||
3036 | |||||||||||||
3037 | // Look for an explicit lifetime attribute there. | ||||||||||||
3038 | DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex); | ||||||||||||
3039 | if (chunk.Kind != DeclaratorChunk::Pointer && | ||||||||||||
3040 | chunk.Kind != DeclaratorChunk::BlockPointer) | ||||||||||||
3041 | return; | ||||||||||||
3042 | for (const ParsedAttr &AL : chunk.getAttrs()) | ||||||||||||
3043 | if (AL.getKind() == ParsedAttr::AT_ObjCOwnership) | ||||||||||||
3044 | return; | ||||||||||||
3045 | |||||||||||||
3046 | transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing, | ||||||||||||
3047 | outermostPointerIndex); | ||||||||||||
3048 | |||||||||||||
3049 | // Any other number of pointers/references does not trigger the rule. | ||||||||||||
3050 | } else return; | ||||||||||||
3051 | |||||||||||||
3052 | // TODO: mark whether we did this inference? | ||||||||||||
3053 | } | ||||||||||||
3054 | |||||||||||||
3055 | void Sema::diagnoseIgnoredQualifiers(unsigned DiagID, unsigned Quals, | ||||||||||||
3056 | SourceLocation FallbackLoc, | ||||||||||||
3057 | SourceLocation ConstQualLoc, | ||||||||||||
3058 | SourceLocation VolatileQualLoc, | ||||||||||||
3059 | SourceLocation RestrictQualLoc, | ||||||||||||
3060 | SourceLocation AtomicQualLoc, | ||||||||||||
3061 | SourceLocation UnalignedQualLoc) { | ||||||||||||
3062 | if (!Quals) | ||||||||||||
3063 | return; | ||||||||||||
3064 | |||||||||||||
3065 | struct Qual { | ||||||||||||
3066 | const char *Name; | ||||||||||||
3067 | unsigned Mask; | ||||||||||||
3068 | SourceLocation Loc; | ||||||||||||
3069 | } const QualKinds[5] = { | ||||||||||||
3070 | { "const", DeclSpec::TQ_const, ConstQualLoc }, | ||||||||||||
3071 | { "volatile", DeclSpec::TQ_volatile, VolatileQualLoc }, | ||||||||||||
3072 | { "restrict", DeclSpec::TQ_restrict, RestrictQualLoc }, | ||||||||||||
3073 | { "__unaligned", DeclSpec::TQ_unaligned, UnalignedQualLoc }, | ||||||||||||
3074 | { "_Atomic", DeclSpec::TQ_atomic, AtomicQualLoc } | ||||||||||||
3075 | }; | ||||||||||||
3076 | |||||||||||||
3077 | SmallString<32> QualStr; | ||||||||||||
3078 | unsigned NumQuals = 0; | ||||||||||||
3079 | SourceLocation Loc; | ||||||||||||
3080 | FixItHint FixIts[5]; | ||||||||||||
3081 | |||||||||||||
3082 | // Build a string naming the redundant qualifiers. | ||||||||||||
3083 | for (auto &E : QualKinds) { | ||||||||||||
3084 | if (Quals & E.Mask) { | ||||||||||||
3085 | if (!QualStr.empty()) QualStr += ' '; | ||||||||||||
3086 | QualStr += E.Name; | ||||||||||||
3087 | |||||||||||||
3088 | // If we have a location for the qualifier, offer a fixit. | ||||||||||||
3089 | SourceLocation QualLoc = E.Loc; | ||||||||||||
3090 | if (QualLoc.isValid()) { | ||||||||||||
3091 | FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc); | ||||||||||||
3092 | if (Loc.isInvalid() || | ||||||||||||
3093 | getSourceManager().isBeforeInTranslationUnit(QualLoc, Loc)) | ||||||||||||
3094 | Loc = QualLoc; | ||||||||||||
3095 | } | ||||||||||||
3096 | |||||||||||||
3097 | ++NumQuals; | ||||||||||||
3098 | } | ||||||||||||
3099 | } | ||||||||||||
3100 | |||||||||||||
3101 | Diag(Loc.isInvalid() ? FallbackLoc : Loc, DiagID) | ||||||||||||
3102 | << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3]; | ||||||||||||
3103 | } | ||||||||||||
3104 | |||||||||||||
3105 | // Diagnose pointless type qualifiers on the return type of a function. | ||||||||||||
3106 | static void diagnoseRedundantReturnTypeQualifiers(Sema &S, QualType RetTy, | ||||||||||||
3107 | Declarator &D, | ||||||||||||
3108 | unsigned FunctionChunkIndex) { | ||||||||||||
3109 | const DeclaratorChunk::FunctionTypeInfo &FTI = | ||||||||||||
3110 | D.getTypeObject(FunctionChunkIndex).Fun; | ||||||||||||
3111 | if (FTI.hasTrailingReturnType()) { | ||||||||||||
3112 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, | ||||||||||||
3113 | RetTy.getLocalCVRQualifiers(), | ||||||||||||
3114 | FTI.getTrailingReturnTypeLoc()); | ||||||||||||
3115 | return; | ||||||||||||
3116 | } | ||||||||||||
3117 | |||||||||||||
3118 | for (unsigned OuterChunkIndex = FunctionChunkIndex + 1, | ||||||||||||
3119 | End = D.getNumTypeObjects(); | ||||||||||||
3120 | OuterChunkIndex != End; ++OuterChunkIndex) { | ||||||||||||
3121 | DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex); | ||||||||||||
3122 | switch (OuterChunk.Kind) { | ||||||||||||
3123 | case DeclaratorChunk::Paren: | ||||||||||||
3124 | continue; | ||||||||||||
3125 | |||||||||||||
3126 | case DeclaratorChunk::Pointer: { | ||||||||||||
3127 | DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr; | ||||||||||||
3128 | S.diagnoseIgnoredQualifiers( | ||||||||||||
3129 | diag::warn_qual_return_type, | ||||||||||||
3130 | PTI.TypeQuals, | ||||||||||||
3131 | SourceLocation(), | ||||||||||||
3132 | PTI.ConstQualLoc, | ||||||||||||
3133 | PTI.VolatileQualLoc, | ||||||||||||
3134 | PTI.RestrictQualLoc, | ||||||||||||
3135 | PTI.AtomicQualLoc, | ||||||||||||
3136 | PTI.UnalignedQualLoc); | ||||||||||||
3137 | return; | ||||||||||||
3138 | } | ||||||||||||
3139 | |||||||||||||
3140 | case DeclaratorChunk::Function: | ||||||||||||
3141 | case DeclaratorChunk::BlockPointer: | ||||||||||||
3142 | case DeclaratorChunk::Reference: | ||||||||||||
3143 | case DeclaratorChunk::Array: | ||||||||||||
3144 | case DeclaratorChunk::MemberPointer: | ||||||||||||
3145 | case DeclaratorChunk::Pipe: | ||||||||||||
3146 | // FIXME: We can't currently provide an accurate source location and a | ||||||||||||
3147 | // fix-it hint for these. | ||||||||||||
3148 | unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0; | ||||||||||||
3149 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, | ||||||||||||
3150 | RetTy.getCVRQualifiers() | AtomicQual, | ||||||||||||
3151 | D.getIdentifierLoc()); | ||||||||||||
3152 | return; | ||||||||||||
3153 | } | ||||||||||||
3154 | |||||||||||||
3155 | llvm_unreachable("unknown declarator chunk kind")::llvm::llvm_unreachable_internal("unknown declarator chunk kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3155); | ||||||||||||
3156 | } | ||||||||||||
3157 | |||||||||||||
3158 | // If the qualifiers come from a conversion function type, don't diagnose | ||||||||||||
3159 | // them -- they're not necessarily redundant, since such a conversion | ||||||||||||
3160 | // operator can be explicitly called as "x.operator const int()". | ||||||||||||
3161 | if (D.getName().getKind() == UnqualifiedIdKind::IK_ConversionFunctionId) | ||||||||||||
3162 | return; | ||||||||||||
3163 | |||||||||||||
3164 | // Just parens all the way out to the decl specifiers. Diagnose any qualifiers | ||||||||||||
3165 | // which are present there. | ||||||||||||
3166 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, | ||||||||||||
3167 | D.getDeclSpec().getTypeQualifiers(), | ||||||||||||
3168 | D.getIdentifierLoc(), | ||||||||||||
3169 | D.getDeclSpec().getConstSpecLoc(), | ||||||||||||
3170 | D.getDeclSpec().getVolatileSpecLoc(), | ||||||||||||
3171 | D.getDeclSpec().getRestrictSpecLoc(), | ||||||||||||
3172 | D.getDeclSpec().getAtomicSpecLoc(), | ||||||||||||
3173 | D.getDeclSpec().getUnalignedSpecLoc()); | ||||||||||||
3174 | } | ||||||||||||
3175 | |||||||||||||
3176 | static std::pair<QualType, TypeSourceInfo *> | ||||||||||||
3177 | InventTemplateParameter(TypeProcessingState &state, QualType T, | ||||||||||||
3178 | TypeSourceInfo *TrailingTSI, AutoType *Auto, | ||||||||||||
3179 | InventedTemplateParameterInfo &Info) { | ||||||||||||
3180 | Sema &S = state.getSema(); | ||||||||||||
3181 | Declarator &D = state.getDeclarator(); | ||||||||||||
3182 | |||||||||||||
3183 | const unsigned TemplateParameterDepth = Info.AutoTemplateParameterDepth; | ||||||||||||
3184 | const unsigned AutoParameterPosition = Info.TemplateParams.size(); | ||||||||||||
3185 | const bool IsParameterPack = D.hasEllipsis(); | ||||||||||||
3186 | |||||||||||||
3187 | // If auto is mentioned in a lambda parameter or abbreviated function | ||||||||||||
3188 | // template context, convert it to a template parameter type. | ||||||||||||
3189 | |||||||||||||
3190 | // Create the TemplateTypeParmDecl here to retrieve the corresponding | ||||||||||||
3191 | // template parameter type. Template parameters are temporarily added | ||||||||||||
3192 | // to the TU until the associated TemplateDecl is created. | ||||||||||||
3193 | TemplateTypeParmDecl *InventedTemplateParam = | ||||||||||||
3194 | TemplateTypeParmDecl::Create( | ||||||||||||
3195 | S.Context, S.Context.getTranslationUnitDecl(), | ||||||||||||
3196 | /*KeyLoc=*/D.getDeclSpec().getTypeSpecTypeLoc(), | ||||||||||||
3197 | /*NameLoc=*/D.getIdentifierLoc(), | ||||||||||||
3198 | TemplateParameterDepth, AutoParameterPosition, | ||||||||||||
3199 | S.InventAbbreviatedTemplateParameterTypeName( | ||||||||||||
3200 | D.getIdentifier(), AutoParameterPosition), false, | ||||||||||||
3201 | IsParameterPack, /*HasTypeConstraint=*/Auto->isConstrained()); | ||||||||||||
3202 | InventedTemplateParam->setImplicit(); | ||||||||||||
3203 | Info.TemplateParams.push_back(InventedTemplateParam); | ||||||||||||
3204 | |||||||||||||
3205 | // Attach type constraints to the new parameter. | ||||||||||||
3206 | if (Auto->isConstrained()) { | ||||||||||||
3207 | if (TrailingTSI) { | ||||||||||||
3208 | // The 'auto' appears in a trailing return type we've already built; | ||||||||||||
3209 | // extract its type constraints to attach to the template parameter. | ||||||||||||
3210 | AutoTypeLoc AutoLoc = TrailingTSI->getTypeLoc().getContainedAutoTypeLoc(); | ||||||||||||
3211 | TemplateArgumentListInfo TAL(AutoLoc.getLAngleLoc(), AutoLoc.getRAngleLoc()); | ||||||||||||
3212 | for (unsigned Idx = 0; Idx < AutoLoc.getNumArgs(); ++Idx) | ||||||||||||
3213 | TAL.addArgument(AutoLoc.getArgLoc(Idx)); | ||||||||||||
3214 | |||||||||||||
3215 | S.AttachTypeConstraint(AutoLoc.getNestedNameSpecifierLoc(), | ||||||||||||
3216 | AutoLoc.getConceptNameInfo(), | ||||||||||||
3217 | AutoLoc.getNamedConcept(), | ||||||||||||
3218 | AutoLoc.hasExplicitTemplateArgs() ? &TAL : nullptr, | ||||||||||||
3219 | InventedTemplateParam, D.getEllipsisLoc()); | ||||||||||||
3220 | } else { | ||||||||||||
3221 | // The 'auto' appears in the decl-specifiers; we've not finished forming | ||||||||||||
3222 | // TypeSourceInfo for it yet. | ||||||||||||
3223 | TemplateIdAnnotation *TemplateId = D.getDeclSpec().getRepAsTemplateId(); | ||||||||||||
3224 | TemplateArgumentListInfo TemplateArgsInfo; | ||||||||||||
3225 | if (TemplateId->LAngleLoc.isValid()) { | ||||||||||||
3226 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), | ||||||||||||
3227 | TemplateId->NumArgs); | ||||||||||||
3228 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgsInfo); | ||||||||||||
3229 | } | ||||||||||||
3230 | S.AttachTypeConstraint( | ||||||||||||
3231 | D.getDeclSpec().getTypeSpecScope().getWithLocInContext(S.Context), | ||||||||||||
3232 | DeclarationNameInfo(DeclarationName(TemplateId->Name), | ||||||||||||
3233 | TemplateId->TemplateNameLoc), | ||||||||||||
3234 | cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl()), | ||||||||||||
3235 | TemplateId->LAngleLoc.isValid() ? &TemplateArgsInfo : nullptr, | ||||||||||||
3236 | InventedTemplateParam, D.getEllipsisLoc()); | ||||||||||||
3237 | } | ||||||||||||
3238 | } | ||||||||||||
3239 | |||||||||||||
3240 | // Replace the 'auto' in the function parameter with this invented | ||||||||||||
3241 | // template type parameter. | ||||||||||||
3242 | // FIXME: Retain some type sugar to indicate that this was written | ||||||||||||
3243 | // as 'auto'? | ||||||||||||
3244 | QualType Replacement(InventedTemplateParam->getTypeForDecl(), 0); | ||||||||||||
3245 | QualType NewT = state.ReplaceAutoType(T, Replacement); | ||||||||||||
3246 | TypeSourceInfo *NewTSI = | ||||||||||||
3247 | TrailingTSI ? S.ReplaceAutoTypeSourceInfo(TrailingTSI, Replacement) | ||||||||||||
3248 | : nullptr; | ||||||||||||
3249 | return {NewT, NewTSI}; | ||||||||||||
3250 | } | ||||||||||||
3251 | |||||||||||||
3252 | static TypeSourceInfo * | ||||||||||||
3253 | GetTypeSourceInfoForDeclarator(TypeProcessingState &State, | ||||||||||||
3254 | QualType T, TypeSourceInfo *ReturnTypeInfo); | ||||||||||||
3255 | |||||||||||||
3256 | static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, | ||||||||||||
3257 | TypeSourceInfo *&ReturnTypeInfo) { | ||||||||||||
3258 | Sema &SemaRef = state.getSema(); | ||||||||||||
3259 | Declarator &D = state.getDeclarator(); | ||||||||||||
3260 | QualType T; | ||||||||||||
3261 | ReturnTypeInfo = nullptr; | ||||||||||||
3262 | |||||||||||||
3263 | // The TagDecl owned by the DeclSpec. | ||||||||||||
3264 | TagDecl *OwnedTagDecl = nullptr; | ||||||||||||
3265 | |||||||||||||
3266 | switch (D.getName().getKind()) { | ||||||||||||
3267 | case UnqualifiedIdKind::IK_ImplicitSelfParam: | ||||||||||||
3268 | case UnqualifiedIdKind::IK_OperatorFunctionId: | ||||||||||||
3269 | case UnqualifiedIdKind::IK_Identifier: | ||||||||||||
3270 | case UnqualifiedIdKind::IK_LiteralOperatorId: | ||||||||||||
3271 | case UnqualifiedIdKind::IK_TemplateId: | ||||||||||||
3272 | T = ConvertDeclSpecToType(state); | ||||||||||||
3273 | |||||||||||||
3274 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { | ||||||||||||
3275 | OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); | ||||||||||||
3276 | // Owned declaration is embedded in declarator. | ||||||||||||
3277 | OwnedTagDecl->setEmbeddedInDeclarator(true); | ||||||||||||
3278 | } | ||||||||||||
3279 | break; | ||||||||||||
3280 | |||||||||||||
3281 | case UnqualifiedIdKind::IK_ConstructorName: | ||||||||||||
3282 | case UnqualifiedIdKind::IK_ConstructorTemplateId: | ||||||||||||
3283 | case UnqualifiedIdKind::IK_DestructorName: | ||||||||||||
3284 | // Constructors and destructors don't have return types. Use | ||||||||||||
3285 | // "void" instead. | ||||||||||||
3286 | T = SemaRef.Context.VoidTy; | ||||||||||||
3287 | processTypeAttrs(state, T, TAL_DeclSpec, | ||||||||||||
3288 | D.getMutableDeclSpec().getAttributes()); | ||||||||||||
3289 | break; | ||||||||||||
3290 | |||||||||||||
3291 | case UnqualifiedIdKind::IK_DeductionGuideName: | ||||||||||||
3292 | // Deduction guides have a trailing return type and no type in their | ||||||||||||
3293 | // decl-specifier sequence. Use a placeholder return type for now. | ||||||||||||
3294 | T = SemaRef.Context.DependentTy; | ||||||||||||
3295 | break; | ||||||||||||
3296 | |||||||||||||
3297 | case UnqualifiedIdKind::IK_ConversionFunctionId: | ||||||||||||
3298 | // The result type of a conversion function is the type that it | ||||||||||||
3299 | // converts to. | ||||||||||||
3300 | T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId, | ||||||||||||
3301 | &ReturnTypeInfo); | ||||||||||||
3302 | break; | ||||||||||||
3303 | } | ||||||||||||
3304 | |||||||||||||
3305 | if (!D.getAttributes().empty()) | ||||||||||||
3306 | distributeTypeAttrsFromDeclarator(state, T); | ||||||||||||
3307 | |||||||||||||
3308 | // Find the deduced type in this type. Look in the trailing return type if we | ||||||||||||
3309 | // have one, otherwise in the DeclSpec type. | ||||||||||||
3310 | // FIXME: The standard wording doesn't currently describe this. | ||||||||||||
3311 | DeducedType *Deduced = T->getContainedDeducedType(); | ||||||||||||
3312 | bool DeducedIsTrailingReturnType = false; | ||||||||||||
3313 | if (Deduced && isa<AutoType>(Deduced) && D.hasTrailingReturnType()) { | ||||||||||||
3314 | QualType T = SemaRef.GetTypeFromParser(D.getTrailingReturnType()); | ||||||||||||
3315 | Deduced = T.isNull() ? nullptr : T->getContainedDeducedType(); | ||||||||||||
3316 | DeducedIsTrailingReturnType = true; | ||||||||||||
3317 | } | ||||||||||||
3318 | |||||||||||||
3319 | // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context. | ||||||||||||
3320 | if (Deduced) { | ||||||||||||
3321 | AutoType *Auto = dyn_cast<AutoType>(Deduced); | ||||||||||||
3322 | int Error = -1; | ||||||||||||
3323 | |||||||||||||
3324 | // Is this a 'auto' or 'decltype(auto)' type (as opposed to __auto_type or | ||||||||||||
3325 | // class template argument deduction)? | ||||||||||||
3326 | bool IsCXXAutoType = | ||||||||||||
3327 | (Auto && Auto->getKeyword() != AutoTypeKeyword::GNUAutoType); | ||||||||||||
3328 | bool IsDeducedReturnType = false; | ||||||||||||
3329 | |||||||||||||
3330 | switch (D.getContext()) { | ||||||||||||
3331 | case DeclaratorContext::LambdaExpr: | ||||||||||||
3332 | // Declared return type of a lambda-declarator is implicit and is always | ||||||||||||
3333 | // 'auto'. | ||||||||||||
3334 | break; | ||||||||||||
3335 | case DeclaratorContext::ObjCParameter: | ||||||||||||
3336 | case DeclaratorContext::ObjCResult: | ||||||||||||
3337 | Error = 0; | ||||||||||||
3338 | break; | ||||||||||||
3339 | case DeclaratorContext::RequiresExpr: | ||||||||||||
3340 | Error = 22; | ||||||||||||
3341 | break; | ||||||||||||
3342 | case DeclaratorContext::Prototype: | ||||||||||||
3343 | case DeclaratorContext::LambdaExprParameter: { | ||||||||||||
3344 | InventedTemplateParameterInfo *Info = nullptr; | ||||||||||||
3345 | if (D.getContext() == DeclaratorContext::Prototype) { | ||||||||||||
3346 | // With concepts we allow 'auto' in function parameters. | ||||||||||||
3347 | if (!SemaRef.getLangOpts().CPlusPlus20 || !Auto || | ||||||||||||
3348 | Auto->getKeyword() != AutoTypeKeyword::Auto) { | ||||||||||||
3349 | Error = 0; | ||||||||||||
3350 | break; | ||||||||||||
3351 | } else if (!SemaRef.getCurScope()->isFunctionDeclarationScope()) { | ||||||||||||
3352 | Error = 21; | ||||||||||||
3353 | break; | ||||||||||||
3354 | } | ||||||||||||
3355 | |||||||||||||
3356 | Info = &SemaRef.InventedParameterInfos.back(); | ||||||||||||
3357 | } else { | ||||||||||||
3358 | // In C++14, generic lambdas allow 'auto' in their parameters. | ||||||||||||
3359 | if (!SemaRef.getLangOpts().CPlusPlus14 || !Auto || | ||||||||||||
3360 | Auto->getKeyword() != AutoTypeKeyword::Auto) { | ||||||||||||
3361 | Error = 16; | ||||||||||||
3362 | break; | ||||||||||||
3363 | } | ||||||||||||
3364 | Info = SemaRef.getCurLambda(); | ||||||||||||
3365 | assert(Info && "No LambdaScopeInfo on the stack!")((Info && "No LambdaScopeInfo on the stack!") ? static_cast <void> (0) : __assert_fail ("Info && \"No LambdaScopeInfo on the stack!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3365, __PRETTY_FUNCTION__)); | ||||||||||||
3366 | } | ||||||||||||
3367 | |||||||||||||
3368 | // We'll deal with inventing template parameters for 'auto' in trailing | ||||||||||||
3369 | // return types when we pick up the trailing return type when processing | ||||||||||||
3370 | // the function chunk. | ||||||||||||
3371 | if (!DeducedIsTrailingReturnType) | ||||||||||||
3372 | T = InventTemplateParameter(state, T, nullptr, Auto, *Info).first; | ||||||||||||
3373 | break; | ||||||||||||
3374 | } | ||||||||||||
3375 | case DeclaratorContext::Member: { | ||||||||||||
3376 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static || | ||||||||||||
3377 | D.isFunctionDeclarator()) | ||||||||||||
3378 | break; | ||||||||||||
3379 | bool Cxx = SemaRef.getLangOpts().CPlusPlus; | ||||||||||||
3380 | if (isa<ObjCContainerDecl>(SemaRef.CurContext)) { | ||||||||||||
3381 | Error = 6; // Interface member. | ||||||||||||
3382 | } else { | ||||||||||||
3383 | switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) { | ||||||||||||
3384 | case TTK_Enum: llvm_unreachable("unhandled tag kind")::llvm::llvm_unreachable_internal("unhandled tag kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3384); | ||||||||||||
3385 | case TTK_Struct: Error = Cxx ? 1 : 2; /* Struct member */ break; | ||||||||||||
3386 | case TTK_Union: Error = Cxx ? 3 : 4; /* Union member */ break; | ||||||||||||
3387 | case TTK_Class: Error = 5; /* Class member */ break; | ||||||||||||
3388 | case TTK_Interface: Error = 6; /* Interface member */ break; | ||||||||||||
3389 | } | ||||||||||||
3390 | } | ||||||||||||
3391 | if (D.getDeclSpec().isFriendSpecified()) | ||||||||||||
3392 | Error = 20; // Friend type | ||||||||||||
3393 | break; | ||||||||||||
3394 | } | ||||||||||||
3395 | case DeclaratorContext::CXXCatch: | ||||||||||||
3396 | case DeclaratorContext::ObjCCatch: | ||||||||||||
3397 | Error = 7; // Exception declaration | ||||||||||||
3398 | break; | ||||||||||||
3399 | case DeclaratorContext::TemplateParam: | ||||||||||||
3400 | if (isa<DeducedTemplateSpecializationType>(Deduced) && | ||||||||||||
3401 | !SemaRef.getLangOpts().CPlusPlus20) | ||||||||||||
3402 | Error = 19; // Template parameter (until C++20) | ||||||||||||
3403 | else if (!SemaRef.getLangOpts().CPlusPlus17) | ||||||||||||
3404 | Error = 8; // Template parameter (until C++17) | ||||||||||||
3405 | break; | ||||||||||||
3406 | case DeclaratorContext::BlockLiteral: | ||||||||||||
3407 | Error = 9; // Block literal | ||||||||||||
3408 | break; | ||||||||||||
3409 | case DeclaratorContext::TemplateArg: | ||||||||||||
3410 | // Within a template argument list, a deduced template specialization | ||||||||||||
3411 | // type will be reinterpreted as a template template argument. | ||||||||||||
3412 | if (isa<DeducedTemplateSpecializationType>(Deduced) && | ||||||||||||
3413 | !D.getNumTypeObjects() && | ||||||||||||
3414 | D.getDeclSpec().getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier) | ||||||||||||
3415 | break; | ||||||||||||
3416 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
3417 | case DeclaratorContext::TemplateTypeArg: | ||||||||||||
3418 | Error = 10; // Template type argument | ||||||||||||
3419 | break; | ||||||||||||
3420 | case DeclaratorContext::AliasDecl: | ||||||||||||
3421 | case DeclaratorContext::AliasTemplate: | ||||||||||||
3422 | Error = 12; // Type alias | ||||||||||||
3423 | break; | ||||||||||||
3424 | case DeclaratorContext::TrailingReturn: | ||||||||||||
3425 | case DeclaratorContext::TrailingReturnVar: | ||||||||||||
3426 | if (!SemaRef.getLangOpts().CPlusPlus14 || !IsCXXAutoType) | ||||||||||||
3427 | Error = 13; // Function return type | ||||||||||||
3428 | IsDeducedReturnType = true; | ||||||||||||
3429 | break; | ||||||||||||
3430 | case DeclaratorContext::ConversionId: | ||||||||||||
3431 | if (!SemaRef.getLangOpts().CPlusPlus14 || !IsCXXAutoType) | ||||||||||||
3432 | Error = 14; // conversion-type-id | ||||||||||||
3433 | IsDeducedReturnType = true; | ||||||||||||
3434 | break; | ||||||||||||
3435 | case DeclaratorContext::FunctionalCast: | ||||||||||||
3436 | if (isa<DeducedTemplateSpecializationType>(Deduced)) | ||||||||||||
3437 | break; | ||||||||||||
3438 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
3439 | case DeclaratorContext::TypeName: | ||||||||||||
3440 | Error = 15; // Generic | ||||||||||||
3441 | break; | ||||||||||||
3442 | case DeclaratorContext::File: | ||||||||||||
3443 | case DeclaratorContext::Block: | ||||||||||||
3444 | case DeclaratorContext::ForInit: | ||||||||||||
3445 | case DeclaratorContext::SelectionInit: | ||||||||||||
3446 | case DeclaratorContext::Condition: | ||||||||||||
3447 | // FIXME: P0091R3 (erroneously) does not permit class template argument | ||||||||||||
3448 | // deduction in conditions, for-init-statements, and other declarations | ||||||||||||
3449 | // that are not simple-declarations. | ||||||||||||
3450 | break; | ||||||||||||
3451 | case DeclaratorContext::CXXNew: | ||||||||||||
3452 | // FIXME: P0091R3 does not permit class template argument deduction here, | ||||||||||||
3453 | // but we follow GCC and allow it anyway. | ||||||||||||
3454 | if (!IsCXXAutoType && !isa<DeducedTemplateSpecializationType>(Deduced)) | ||||||||||||
3455 | Error = 17; // 'new' type | ||||||||||||
3456 | break; | ||||||||||||
3457 | case DeclaratorContext::KNRTypeList: | ||||||||||||
3458 | Error = 18; // K&R function parameter | ||||||||||||
3459 | break; | ||||||||||||
3460 | } | ||||||||||||
3461 | |||||||||||||
3462 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) | ||||||||||||
3463 | Error = 11; | ||||||||||||
3464 | |||||||||||||
3465 | // In Objective-C it is an error to use 'auto' on a function declarator | ||||||||||||
3466 | // (and everywhere for '__auto_type'). | ||||||||||||
3467 | if (D.isFunctionDeclarator() && | ||||||||||||
3468 | (!SemaRef.getLangOpts().CPlusPlus11 || !IsCXXAutoType)) | ||||||||||||
3469 | Error = 13; | ||||||||||||
3470 | |||||||||||||
3471 | SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc(); | ||||||||||||
3472 | if (D.getName().getKind() == UnqualifiedIdKind::IK_ConversionFunctionId) | ||||||||||||
3473 | AutoRange = D.getName().getSourceRange(); | ||||||||||||
3474 | |||||||||||||
3475 | if (Error != -1) { | ||||||||||||
3476 | unsigned Kind; | ||||||||||||
3477 | if (Auto) { | ||||||||||||
3478 | switch (Auto->getKeyword()) { | ||||||||||||
3479 | case AutoTypeKeyword::Auto: Kind = 0; break; | ||||||||||||
3480 | case AutoTypeKeyword::DecltypeAuto: Kind = 1; break; | ||||||||||||
3481 | case AutoTypeKeyword::GNUAutoType: Kind = 2; break; | ||||||||||||
3482 | } | ||||||||||||
3483 | } else { | ||||||||||||
3484 | assert(isa<DeducedTemplateSpecializationType>(Deduced) &&((isa<DeducedTemplateSpecializationType>(Deduced) && "unknown auto type") ? static_cast<void> (0) : __assert_fail ("isa<DeducedTemplateSpecializationType>(Deduced) && \"unknown auto type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3485, __PRETTY_FUNCTION__)) | ||||||||||||
3485 | "unknown auto type")((isa<DeducedTemplateSpecializationType>(Deduced) && "unknown auto type") ? static_cast<void> (0) : __assert_fail ("isa<DeducedTemplateSpecializationType>(Deduced) && \"unknown auto type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3485, __PRETTY_FUNCTION__)); | ||||||||||||
3486 | Kind = 3; | ||||||||||||
3487 | } | ||||||||||||
3488 | |||||||||||||
3489 | auto *DTST = dyn_cast<DeducedTemplateSpecializationType>(Deduced); | ||||||||||||
3490 | TemplateName TN = DTST ? DTST->getTemplateName() : TemplateName(); | ||||||||||||
3491 | |||||||||||||
3492 | SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed) | ||||||||||||
3493 | << Kind << Error << (int)SemaRef.getTemplateNameKindForDiagnostics(TN) | ||||||||||||
3494 | << QualType(Deduced, 0) << AutoRange; | ||||||||||||
3495 | if (auto *TD = TN.getAsTemplateDecl()) | ||||||||||||
3496 | SemaRef.Diag(TD->getLocation(), diag::note_template_decl_here); | ||||||||||||
3497 | |||||||||||||
3498 | T = SemaRef.Context.IntTy; | ||||||||||||
3499 | D.setInvalidType(true); | ||||||||||||
3500 | } else if (Auto && D.getContext() != DeclaratorContext::LambdaExpr) { | ||||||||||||
3501 | // If there was a trailing return type, we already got | ||||||||||||
3502 | // warn_cxx98_compat_trailing_return_type in the parser. | ||||||||||||
3503 | SemaRef.Diag(AutoRange.getBegin(), | ||||||||||||
3504 | D.getContext() == DeclaratorContext::LambdaExprParameter | ||||||||||||
3505 | ? diag::warn_cxx11_compat_generic_lambda | ||||||||||||
3506 | : IsDeducedReturnType | ||||||||||||
3507 | ? diag::warn_cxx11_compat_deduced_return_type | ||||||||||||
3508 | : diag::warn_cxx98_compat_auto_type_specifier) | ||||||||||||
3509 | << AutoRange; | ||||||||||||
3510 | } | ||||||||||||
3511 | } | ||||||||||||
3512 | |||||||||||||
3513 | if (SemaRef.getLangOpts().CPlusPlus && | ||||||||||||
3514 | OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) { | ||||||||||||
3515 | // Check the contexts where C++ forbids the declaration of a new class | ||||||||||||
3516 | // or enumeration in a type-specifier-seq. | ||||||||||||
3517 | unsigned DiagID = 0; | ||||||||||||
3518 | switch (D.getContext()) { | ||||||||||||
3519 | case DeclaratorContext::TrailingReturn: | ||||||||||||
3520 | case DeclaratorContext::TrailingReturnVar: | ||||||||||||
3521 | // Class and enumeration definitions are syntactically not allowed in | ||||||||||||
3522 | // trailing return types. | ||||||||||||
3523 | llvm_unreachable("parser should not have allowed this")::llvm::llvm_unreachable_internal("parser should not have allowed this" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3523); | ||||||||||||
3524 | break; | ||||||||||||
3525 | case DeclaratorContext::File: | ||||||||||||
3526 | case DeclaratorContext::Member: | ||||||||||||
3527 | case DeclaratorContext::Block: | ||||||||||||
3528 | case DeclaratorContext::ForInit: | ||||||||||||
3529 | case DeclaratorContext::SelectionInit: | ||||||||||||
3530 | case DeclaratorContext::BlockLiteral: | ||||||||||||
3531 | case DeclaratorContext::LambdaExpr: | ||||||||||||
3532 | // C++11 [dcl.type]p3: | ||||||||||||
3533 | // A type-specifier-seq shall not define a class or enumeration unless | ||||||||||||
3534 | // it appears in the type-id of an alias-declaration (7.1.3) that is not | ||||||||||||
3535 | // the declaration of a template-declaration. | ||||||||||||
3536 | case DeclaratorContext::AliasDecl: | ||||||||||||
3537 | break; | ||||||||||||
3538 | case DeclaratorContext::AliasTemplate: | ||||||||||||
3539 | DiagID = diag::err_type_defined_in_alias_template; | ||||||||||||
3540 | break; | ||||||||||||
3541 | case DeclaratorContext::TypeName: | ||||||||||||
3542 | case DeclaratorContext::FunctionalCast: | ||||||||||||
3543 | case DeclaratorContext::ConversionId: | ||||||||||||
3544 | case DeclaratorContext::TemplateParam: | ||||||||||||
3545 | case DeclaratorContext::CXXNew: | ||||||||||||
3546 | case DeclaratorContext::CXXCatch: | ||||||||||||
3547 | case DeclaratorContext::ObjCCatch: | ||||||||||||
3548 | case DeclaratorContext::TemplateArg: | ||||||||||||
3549 | case DeclaratorContext::TemplateTypeArg: | ||||||||||||
3550 | DiagID = diag::err_type_defined_in_type_specifier; | ||||||||||||
3551 | break; | ||||||||||||
3552 | case DeclaratorContext::Prototype: | ||||||||||||
3553 | case DeclaratorContext::LambdaExprParameter: | ||||||||||||
3554 | case DeclaratorContext::ObjCParameter: | ||||||||||||
3555 | case DeclaratorContext::ObjCResult: | ||||||||||||
3556 | case DeclaratorContext::KNRTypeList: | ||||||||||||
3557 | case DeclaratorContext::RequiresExpr: | ||||||||||||
3558 | // C++ [dcl.fct]p6: | ||||||||||||
3559 | // Types shall not be defined in return or parameter types. | ||||||||||||
3560 | DiagID = diag::err_type_defined_in_param_type; | ||||||||||||
3561 | break; | ||||||||||||
3562 | case DeclaratorContext::Condition: | ||||||||||||
3563 | // C++ 6.4p2: | ||||||||||||
3564 | // The type-specifier-seq shall not contain typedef and shall not declare | ||||||||||||
3565 | // a new class or enumeration. | ||||||||||||
3566 | DiagID = diag::err_type_defined_in_condition; | ||||||||||||
3567 | break; | ||||||||||||
3568 | } | ||||||||||||
3569 | |||||||||||||
3570 | if (DiagID != 0) { | ||||||||||||
3571 | SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID) | ||||||||||||
3572 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); | ||||||||||||
3573 | D.setInvalidType(true); | ||||||||||||
3574 | } | ||||||||||||
3575 | } | ||||||||||||
3576 | |||||||||||||
3577 | assert(!T.isNull() && "This function should not return a null type")((!T.isNull() && "This function should not return a null type" ) ? static_cast<void> (0) : __assert_fail ("!T.isNull() && \"This function should not return a null type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3577, __PRETTY_FUNCTION__)); | ||||||||||||
3578 | return T; | ||||||||||||
3579 | } | ||||||||||||
3580 | |||||||||||||
3581 | /// Produce an appropriate diagnostic for an ambiguity between a function | ||||||||||||
3582 | /// declarator and a C++ direct-initializer. | ||||||||||||
3583 | static void warnAboutAmbiguousFunction(Sema &S, Declarator &D, | ||||||||||||
3584 | DeclaratorChunk &DeclType, QualType RT) { | ||||||||||||
3585 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; | ||||||||||||
3586 | assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity")((FTI.isAmbiguous && "no direct-initializer / function ambiguity" ) ? static_cast<void> (0) : __assert_fail ("FTI.isAmbiguous && \"no direct-initializer / function ambiguity\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3586, __PRETTY_FUNCTION__)); | ||||||||||||
3587 | |||||||||||||
3588 | // If the return type is void there is no ambiguity. | ||||||||||||
3589 | if (RT->isVoidType()) | ||||||||||||
3590 | return; | ||||||||||||
3591 | |||||||||||||
3592 | // An initializer for a non-class type can have at most one argument. | ||||||||||||
3593 | if (!RT->isRecordType() && FTI.NumParams > 1) | ||||||||||||
3594 | return; | ||||||||||||
3595 | |||||||||||||
3596 | // An initializer for a reference must have exactly one argument. | ||||||||||||
3597 | if (RT->isReferenceType() && FTI.NumParams != 1) | ||||||||||||
3598 | return; | ||||||||||||
3599 | |||||||||||||
3600 | // Only warn if this declarator is declaring a function at block scope, and | ||||||||||||
3601 | // doesn't have a storage class (such as 'extern') specified. | ||||||||||||
3602 | if (!D.isFunctionDeclarator() || | ||||||||||||
3603 | D.getFunctionDefinitionKind() != FunctionDefinitionKind::Declaration || | ||||||||||||
3604 | !S.CurContext->isFunctionOrMethod() || | ||||||||||||
3605 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_unspecified) | ||||||||||||
3606 | return; | ||||||||||||
3607 | |||||||||||||
3608 | // Inside a condition, a direct initializer is not permitted. We allow one to | ||||||||||||
3609 | // be parsed in order to give better diagnostics in condition parsing. | ||||||||||||
3610 | if (D.getContext() == DeclaratorContext::Condition) | ||||||||||||
3611 | return; | ||||||||||||
3612 | |||||||||||||
3613 | SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc); | ||||||||||||
3614 | |||||||||||||
3615 | S.Diag(DeclType.Loc, | ||||||||||||
3616 | FTI.NumParams ? diag::warn_parens_disambiguated_as_function_declaration | ||||||||||||
3617 | : diag::warn_empty_parens_are_function_decl) | ||||||||||||
3618 | << ParenRange; | ||||||||||||
3619 | |||||||||||||
3620 | // If the declaration looks like: | ||||||||||||
3621 | // T var1, | ||||||||||||
3622 | // f(); | ||||||||||||
3623 | // and name lookup finds a function named 'f', then the ',' was | ||||||||||||
3624 | // probably intended to be a ';'. | ||||||||||||
3625 | if (!D.isFirstDeclarator() && D.getIdentifier()) { | ||||||||||||
3626 | FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr); | ||||||||||||
3627 | FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr); | ||||||||||||
3628 | if (Comma.getFileID() != Name.getFileID() || | ||||||||||||
3629 | Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) { | ||||||||||||
3630 | LookupResult Result(S, D.getIdentifier(), SourceLocation(), | ||||||||||||
3631 | Sema::LookupOrdinaryName); | ||||||||||||
3632 | if (S.LookupName(Result, S.getCurScope())) | ||||||||||||
3633 | S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call) | ||||||||||||
3634 | << FixItHint::CreateReplacement(D.getCommaLoc(), ";") | ||||||||||||
3635 | << D.getIdentifier(); | ||||||||||||
3636 | Result.suppressDiagnostics(); | ||||||||||||
3637 | } | ||||||||||||
3638 | } | ||||||||||||
3639 | |||||||||||||
3640 | if (FTI.NumParams > 0) { | ||||||||||||
3641 | // For a declaration with parameters, eg. "T var(T());", suggest adding | ||||||||||||
3642 | // parens around the first parameter to turn the declaration into a | ||||||||||||
3643 | // variable declaration. | ||||||||||||
3644 | SourceRange Range = FTI.Params[0].Param->getSourceRange(); | ||||||||||||
3645 | SourceLocation B = Range.getBegin(); | ||||||||||||
3646 | SourceLocation E = S.getLocForEndOfToken(Range.getEnd()); | ||||||||||||
3647 | // FIXME: Maybe we should suggest adding braces instead of parens | ||||||||||||
3648 | // in C++11 for classes that don't have an initializer_list constructor. | ||||||||||||
3649 | S.Diag(B, diag::note_additional_parens_for_variable_declaration) | ||||||||||||
3650 | << FixItHint::CreateInsertion(B, "(") | ||||||||||||
3651 | << FixItHint::CreateInsertion(E, ")"); | ||||||||||||
3652 | } else { | ||||||||||||
3653 | // For a declaration without parameters, eg. "T var();", suggest replacing | ||||||||||||
3654 | // the parens with an initializer to turn the declaration into a variable | ||||||||||||
3655 | // declaration. | ||||||||||||
3656 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); | ||||||||||||
3657 | |||||||||||||
3658 | // Empty parens mean value-initialization, and no parens mean | ||||||||||||
3659 | // default initialization. These are equivalent if the default | ||||||||||||
3660 | // constructor is user-provided or if zero-initialization is a | ||||||||||||
3661 | // no-op. | ||||||||||||
3662 | if (RD && RD->hasDefinition() && | ||||||||||||
3663 | (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor())) | ||||||||||||
3664 | S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor) | ||||||||||||
3665 | << FixItHint::CreateRemoval(ParenRange); | ||||||||||||
3666 | else { | ||||||||||||
3667 | std::string Init = | ||||||||||||
3668 | S.getFixItZeroInitializerForType(RT, ParenRange.getBegin()); | ||||||||||||
3669 | if (Init.empty() && S.LangOpts.CPlusPlus11) | ||||||||||||
3670 | Init = "{}"; | ||||||||||||
3671 | if (!Init.empty()) | ||||||||||||
3672 | S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize) | ||||||||||||
3673 | << FixItHint::CreateReplacement(ParenRange, Init); | ||||||||||||
3674 | } | ||||||||||||
3675 | } | ||||||||||||
3676 | } | ||||||||||||
3677 | |||||||||||||
3678 | /// Produce an appropriate diagnostic for a declarator with top-level | ||||||||||||
3679 | /// parentheses. | ||||||||||||
3680 | static void warnAboutRedundantParens(Sema &S, Declarator &D, QualType T) { | ||||||||||||
3681 | DeclaratorChunk &Paren = D.getTypeObject(D.getNumTypeObjects() - 1); | ||||||||||||
3682 | assert(Paren.Kind == DeclaratorChunk::Paren &&((Paren.Kind == DeclaratorChunk::Paren && "do not have redundant top-level parentheses" ) ? static_cast<void> (0) : __assert_fail ("Paren.Kind == DeclaratorChunk::Paren && \"do not have redundant top-level parentheses\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3683, __PRETTY_FUNCTION__)) | ||||||||||||
3683 | "do not have redundant top-level parentheses")((Paren.Kind == DeclaratorChunk::Paren && "do not have redundant top-level parentheses" ) ? static_cast<void> (0) : __assert_fail ("Paren.Kind == DeclaratorChunk::Paren && \"do not have redundant top-level parentheses\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3683, __PRETTY_FUNCTION__)); | ||||||||||||
3684 | |||||||||||||
3685 | // This is a syntactic check; we're not interested in cases that arise | ||||||||||||
3686 | // during template instantiation. | ||||||||||||
3687 | if (S.inTemplateInstantiation()) | ||||||||||||
3688 | return; | ||||||||||||
3689 | |||||||||||||
3690 | // Check whether this could be intended to be a construction of a temporary | ||||||||||||
3691 | // object in C++ via a function-style cast. | ||||||||||||
3692 | bool CouldBeTemporaryObject = | ||||||||||||
3693 | S.getLangOpts().CPlusPlus && D.isExpressionContext() && | ||||||||||||
3694 | !D.isInvalidType() && D.getIdentifier() && | ||||||||||||
3695 | D.getDeclSpec().getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && | ||||||||||||
3696 | (T->isRecordType() || T->isDependentType()) && | ||||||||||||
3697 | D.getDeclSpec().getTypeQualifiers() == 0 && D.isFirstDeclarator(); | ||||||||||||
3698 | |||||||||||||
3699 | bool StartsWithDeclaratorId = true; | ||||||||||||
3700 | for (auto &C : D.type_objects()) { | ||||||||||||
3701 | switch (C.Kind) { | ||||||||||||
3702 | case DeclaratorChunk::Paren: | ||||||||||||
3703 | if (&C == &Paren) | ||||||||||||
3704 | continue; | ||||||||||||
3705 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
3706 | case DeclaratorChunk::Pointer: | ||||||||||||
3707 | StartsWithDeclaratorId = false; | ||||||||||||
3708 | continue; | ||||||||||||
3709 | |||||||||||||
3710 | case DeclaratorChunk::Array: | ||||||||||||
3711 | if (!C.Arr.NumElts) | ||||||||||||
3712 | CouldBeTemporaryObject = false; | ||||||||||||
3713 | continue; | ||||||||||||
3714 | |||||||||||||
3715 | case DeclaratorChunk::Reference: | ||||||||||||
3716 | // FIXME: Suppress the warning here if there is no initializer; we're | ||||||||||||
3717 | // going to give an error anyway. | ||||||||||||
3718 | // We assume that something like 'T (&x) = y;' is highly likely to not | ||||||||||||
3719 | // be intended to be a temporary object. | ||||||||||||
3720 | CouldBeTemporaryObject = false; | ||||||||||||
3721 | StartsWithDeclaratorId = false; | ||||||||||||
3722 | continue; | ||||||||||||
3723 | |||||||||||||
3724 | case DeclaratorChunk::Function: | ||||||||||||
3725 | // In a new-type-id, function chunks require parentheses. | ||||||||||||
3726 | if (D.getContext() == DeclaratorContext::CXXNew) | ||||||||||||
3727 | return; | ||||||||||||
3728 | // FIXME: "A(f())" deserves a vexing-parse warning, not just a | ||||||||||||
3729 | // redundant-parens warning, but we don't know whether the function | ||||||||||||
3730 | // chunk was syntactically valid as an expression here. | ||||||||||||
3731 | CouldBeTemporaryObject = false; | ||||||||||||
3732 | continue; | ||||||||||||
3733 | |||||||||||||
3734 | case DeclaratorChunk::BlockPointer: | ||||||||||||
3735 | case DeclaratorChunk::MemberPointer: | ||||||||||||
3736 | case DeclaratorChunk::Pipe: | ||||||||||||
3737 | // These cannot appear in expressions. | ||||||||||||
3738 | CouldBeTemporaryObject = false; | ||||||||||||
3739 | StartsWithDeclaratorId = false; | ||||||||||||
3740 | continue; | ||||||||||||
3741 | } | ||||||||||||
3742 | } | ||||||||||||
3743 | |||||||||||||
3744 | // FIXME: If there is an initializer, assume that this is not intended to be | ||||||||||||
3745 | // a construction of a temporary object. | ||||||||||||
3746 | |||||||||||||
3747 | // Check whether the name has already been declared; if not, this is not a | ||||||||||||
3748 | // function-style cast. | ||||||||||||
3749 | if (CouldBeTemporaryObject) { | ||||||||||||
3750 | LookupResult Result(S, D.getIdentifier(), SourceLocation(), | ||||||||||||
3751 | Sema::LookupOrdinaryName); | ||||||||||||
3752 | if (!S.LookupName(Result, S.getCurScope())) | ||||||||||||
3753 | CouldBeTemporaryObject = false; | ||||||||||||
3754 | Result.suppressDiagnostics(); | ||||||||||||
3755 | } | ||||||||||||
3756 | |||||||||||||
3757 | SourceRange ParenRange(Paren.Loc, Paren.EndLoc); | ||||||||||||
3758 | |||||||||||||
3759 | if (!CouldBeTemporaryObject) { | ||||||||||||
3760 | // If we have A (::B), the parentheses affect the meaning of the program. | ||||||||||||
3761 | // Suppress the warning in that case. Don't bother looking at the DeclSpec | ||||||||||||
3762 | // here: even (e.g.) "int ::x" is visually ambiguous even though it's | ||||||||||||
3763 | // formally unambiguous. | ||||||||||||
3764 | if (StartsWithDeclaratorId && D.getCXXScopeSpec().isValid()) { | ||||||||||||
3765 | for (NestedNameSpecifier *NNS = D.getCXXScopeSpec().getScopeRep(); NNS; | ||||||||||||
3766 | NNS = NNS->getPrefix()) { | ||||||||||||
3767 | if (NNS->getKind() == NestedNameSpecifier::Global) | ||||||||||||
3768 | return; | ||||||||||||
3769 | } | ||||||||||||
3770 | } | ||||||||||||
3771 | |||||||||||||
3772 | S.Diag(Paren.Loc, diag::warn_redundant_parens_around_declarator) | ||||||||||||
3773 | << ParenRange << FixItHint::CreateRemoval(Paren.Loc) | ||||||||||||
3774 | << FixItHint::CreateRemoval(Paren.EndLoc); | ||||||||||||
3775 | return; | ||||||||||||
3776 | } | ||||||||||||
3777 | |||||||||||||
3778 | S.Diag(Paren.Loc, diag::warn_parens_disambiguated_as_variable_declaration) | ||||||||||||
3779 | << ParenRange << D.getIdentifier(); | ||||||||||||
3780 | auto *RD = T->getAsCXXRecordDecl(); | ||||||||||||
3781 | if (!RD || !RD->hasDefinition() || RD->hasNonTrivialDestructor()) | ||||||||||||
3782 | S.Diag(Paren.Loc, diag::note_raii_guard_add_name) | ||||||||||||
3783 | << FixItHint::CreateInsertion(Paren.Loc, " varname") << T | ||||||||||||
3784 | << D.getIdentifier(); | ||||||||||||
3785 | // FIXME: A cast to void is probably a better suggestion in cases where it's | ||||||||||||
3786 | // valid (when there is no initializer and we're not in a condition). | ||||||||||||
3787 | S.Diag(D.getBeginLoc(), diag::note_function_style_cast_add_parentheses) | ||||||||||||
3788 | << FixItHint::CreateInsertion(D.getBeginLoc(), "(") | ||||||||||||
3789 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(D.getEndLoc()), ")"); | ||||||||||||
3790 | S.Diag(Paren.Loc, diag::note_remove_parens_for_variable_declaration) | ||||||||||||
3791 | << FixItHint::CreateRemoval(Paren.Loc) | ||||||||||||
3792 | << FixItHint::CreateRemoval(Paren.EndLoc); | ||||||||||||
3793 | } | ||||||||||||
3794 | |||||||||||||
3795 | /// Helper for figuring out the default CC for a function declarator type. If | ||||||||||||
3796 | /// this is the outermost chunk, then we can determine the CC from the | ||||||||||||
3797 | /// declarator context. If not, then this could be either a member function | ||||||||||||
3798 | /// type or normal function type. | ||||||||||||
3799 | static CallingConv getCCForDeclaratorChunk( | ||||||||||||
3800 | Sema &S, Declarator &D, const ParsedAttributesView &AttrList, | ||||||||||||
3801 | const DeclaratorChunk::FunctionTypeInfo &FTI, unsigned ChunkIndex) { | ||||||||||||
3802 | assert(D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function)((D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function ) ? static_cast<void> (0) : __assert_fail ("D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3802, __PRETTY_FUNCTION__)); | ||||||||||||
3803 | |||||||||||||
3804 | // Check for an explicit CC attribute. | ||||||||||||
3805 | for (const ParsedAttr &AL : AttrList) { | ||||||||||||
3806 | switch (AL.getKind()) { | ||||||||||||
3807 | CALLING_CONV_ATTRS_CASELISTcase ParsedAttr::AT_CDecl: case ParsedAttr::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr::AT_ThisCall: case ParsedAttr ::AT_RegCall: case ParsedAttr::AT_Pascal: case ParsedAttr::AT_SwiftCall : case ParsedAttr::AT_VectorCall: case ParsedAttr::AT_AArch64VectorPcs : case ParsedAttr::AT_MSABI: case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr::AT_IntelOclBicc: case ParsedAttr ::AT_PreserveMost: case ParsedAttr::AT_PreserveAll : { | ||||||||||||
3808 | // Ignore attributes that don't validate or can't apply to the | ||||||||||||
3809 | // function type. We'll diagnose the failure to apply them in | ||||||||||||
3810 | // handleFunctionTypeAttr. | ||||||||||||
3811 | CallingConv CC; | ||||||||||||
3812 | if (!S.CheckCallingConvAttr(AL, CC) && | ||||||||||||
3813 | (!FTI.isVariadic || supportsVariadicCall(CC))) { | ||||||||||||
3814 | return CC; | ||||||||||||
3815 | } | ||||||||||||
3816 | break; | ||||||||||||
3817 | } | ||||||||||||
3818 | |||||||||||||
3819 | default: | ||||||||||||
3820 | break; | ||||||||||||
3821 | } | ||||||||||||
3822 | } | ||||||||||||
3823 | |||||||||||||
3824 | bool IsCXXInstanceMethod = false; | ||||||||||||
3825 | |||||||||||||
3826 | if (S.getLangOpts().CPlusPlus) { | ||||||||||||
3827 | // Look inwards through parentheses to see if this chunk will form a | ||||||||||||
3828 | // member pointer type or if we're the declarator. Any type attributes | ||||||||||||
3829 | // between here and there will override the CC we choose here. | ||||||||||||
3830 | unsigned I = ChunkIndex; | ||||||||||||
3831 | bool FoundNonParen = false; | ||||||||||||
3832 | while (I && !FoundNonParen) { | ||||||||||||
3833 | --I; | ||||||||||||
3834 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Paren) | ||||||||||||
3835 | FoundNonParen = true; | ||||||||||||
3836 | } | ||||||||||||
3837 | |||||||||||||
3838 | if (FoundNonParen) { | ||||||||||||
3839 | // If we're not the declarator, we're a regular function type unless we're | ||||||||||||
3840 | // in a member pointer. | ||||||||||||
3841 | IsCXXInstanceMethod = | ||||||||||||
3842 | D.getTypeObject(I).Kind == DeclaratorChunk::MemberPointer; | ||||||||||||
3843 | } else if (D.getContext() == DeclaratorContext::LambdaExpr) { | ||||||||||||
3844 | // This can only be a call operator for a lambda, which is an instance | ||||||||||||
3845 | // method. | ||||||||||||
3846 | IsCXXInstanceMethod = true; | ||||||||||||
3847 | } else { | ||||||||||||
3848 | // We're the innermost decl chunk, so must be a function declarator. | ||||||||||||
3849 | assert(D.isFunctionDeclarator())((D.isFunctionDeclarator()) ? static_cast<void> (0) : __assert_fail ("D.isFunctionDeclarator()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3849, __PRETTY_FUNCTION__)); | ||||||||||||
3850 | |||||||||||||
3851 | // If we're inside a record, we're declaring a method, but it could be | ||||||||||||
3852 | // explicitly or implicitly static. | ||||||||||||
3853 | IsCXXInstanceMethod = | ||||||||||||
3854 | D.isFirstDeclarationOfMember() && | ||||||||||||
3855 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && | ||||||||||||
3856 | !D.isStaticMember(); | ||||||||||||
3857 | } | ||||||||||||
3858 | } | ||||||||||||
3859 | |||||||||||||
3860 | CallingConv CC = S.Context.getDefaultCallingConvention(FTI.isVariadic, | ||||||||||||
3861 | IsCXXInstanceMethod); | ||||||||||||
3862 | |||||||||||||
3863 | // Attribute AT_OpenCLKernel affects the calling convention for SPIR | ||||||||||||
3864 | // and AMDGPU targets, hence it cannot be treated as a calling | ||||||||||||
3865 | // convention attribute. This is the simplest place to infer | ||||||||||||
3866 | // calling convention for OpenCL kernels. | ||||||||||||
3867 | if (S.getLangOpts().OpenCL) { | ||||||||||||
3868 | for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) { | ||||||||||||
3869 | if (AL.getKind() == ParsedAttr::AT_OpenCLKernel) { | ||||||||||||
3870 | CC = CC_OpenCLKernel; | ||||||||||||
3871 | break; | ||||||||||||
3872 | } | ||||||||||||
3873 | } | ||||||||||||
3874 | } | ||||||||||||
3875 | |||||||||||||
3876 | return CC; | ||||||||||||
3877 | } | ||||||||||||
3878 | |||||||||||||
3879 | namespace { | ||||||||||||
3880 | /// A simple notion of pointer kinds, which matches up with the various | ||||||||||||
3881 | /// pointer declarators. | ||||||||||||
3882 | enum class SimplePointerKind { | ||||||||||||
3883 | Pointer, | ||||||||||||
3884 | BlockPointer, | ||||||||||||
3885 | MemberPointer, | ||||||||||||
3886 | Array, | ||||||||||||
3887 | }; | ||||||||||||
3888 | } // end anonymous namespace | ||||||||||||
3889 | |||||||||||||
3890 | IdentifierInfo *Sema::getNullabilityKeyword(NullabilityKind nullability) { | ||||||||||||
3891 | switch (nullability) { | ||||||||||||
3892 | case NullabilityKind::NonNull: | ||||||||||||
3893 | if (!Ident__Nonnull) | ||||||||||||
3894 | Ident__Nonnull = PP.getIdentifierInfo("_Nonnull"); | ||||||||||||
3895 | return Ident__Nonnull; | ||||||||||||
3896 | |||||||||||||
3897 | case NullabilityKind::Nullable: | ||||||||||||
3898 | if (!Ident__Nullable) | ||||||||||||
3899 | Ident__Nullable = PP.getIdentifierInfo("_Nullable"); | ||||||||||||
3900 | return Ident__Nullable; | ||||||||||||
3901 | |||||||||||||
3902 | case NullabilityKind::NullableResult: | ||||||||||||
3903 | if (!Ident__Nullable_result) | ||||||||||||
3904 | Ident__Nullable_result = PP.getIdentifierInfo("_Nullable_result"); | ||||||||||||
3905 | return Ident__Nullable_result; | ||||||||||||
3906 | |||||||||||||
3907 | case NullabilityKind::Unspecified: | ||||||||||||
3908 | if (!Ident__Null_unspecified) | ||||||||||||
3909 | Ident__Null_unspecified = PP.getIdentifierInfo("_Null_unspecified"); | ||||||||||||
3910 | return Ident__Null_unspecified; | ||||||||||||
3911 | } | ||||||||||||
3912 | llvm_unreachable("Unknown nullability kind.")::llvm::llvm_unreachable_internal("Unknown nullability kind." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 3912); | ||||||||||||
3913 | } | ||||||||||||
3914 | |||||||||||||
3915 | /// Retrieve the identifier "NSError". | ||||||||||||
3916 | IdentifierInfo *Sema::getNSErrorIdent() { | ||||||||||||
3917 | if (!Ident_NSError) | ||||||||||||
3918 | Ident_NSError = PP.getIdentifierInfo("NSError"); | ||||||||||||
3919 | |||||||||||||
3920 | return Ident_NSError; | ||||||||||||
3921 | } | ||||||||||||
3922 | |||||||||||||
3923 | /// Check whether there is a nullability attribute of any kind in the given | ||||||||||||
3924 | /// attribute list. | ||||||||||||
3925 | static bool hasNullabilityAttr(const ParsedAttributesView &attrs) { | ||||||||||||
3926 | for (const ParsedAttr &AL : attrs) { | ||||||||||||
3927 | if (AL.getKind() == ParsedAttr::AT_TypeNonNull || | ||||||||||||
3928 | AL.getKind() == ParsedAttr::AT_TypeNullable || | ||||||||||||
3929 | AL.getKind() == ParsedAttr::AT_TypeNullableResult || | ||||||||||||
3930 | AL.getKind() == ParsedAttr::AT_TypeNullUnspecified) | ||||||||||||
3931 | return true; | ||||||||||||
3932 | } | ||||||||||||
3933 | |||||||||||||
3934 | return false; | ||||||||||||
3935 | } | ||||||||||||
3936 | |||||||||||||
3937 | namespace { | ||||||||||||
3938 | /// Describes the kind of a pointer a declarator describes. | ||||||||||||
3939 | enum class PointerDeclaratorKind { | ||||||||||||
3940 | // Not a pointer. | ||||||||||||
3941 | NonPointer, | ||||||||||||
3942 | // Single-level pointer. | ||||||||||||
3943 | SingleLevelPointer, | ||||||||||||
3944 | // Multi-level pointer (of any pointer kind). | ||||||||||||
3945 | MultiLevelPointer, | ||||||||||||
3946 | // CFFooRef* | ||||||||||||
3947 | MaybePointerToCFRef, | ||||||||||||
3948 | // CFErrorRef* | ||||||||||||
3949 | CFErrorRefPointer, | ||||||||||||
3950 | // NSError** | ||||||||||||
3951 | NSErrorPointerPointer, | ||||||||||||
3952 | }; | ||||||||||||
3953 | |||||||||||||
3954 | /// Describes a declarator chunk wrapping a pointer that marks inference as | ||||||||||||
3955 | /// unexpected. | ||||||||||||
3956 | // These values must be kept in sync with diagnostics. | ||||||||||||
3957 | enum class PointerWrappingDeclaratorKind { | ||||||||||||
3958 | /// Pointer is top-level. | ||||||||||||
3959 | None = -1, | ||||||||||||
3960 | /// Pointer is an array element. | ||||||||||||
3961 | Array = 0, | ||||||||||||
3962 | /// Pointer is the referent type of a C++ reference. | ||||||||||||
3963 | Reference = 1 | ||||||||||||
3964 | }; | ||||||||||||
3965 | } // end anonymous namespace | ||||||||||||
3966 | |||||||||||||
3967 | /// Classify the given declarator, whose type-specified is \c type, based on | ||||||||||||
3968 | /// what kind of pointer it refers to. | ||||||||||||
3969 | /// | ||||||||||||
3970 | /// This is used to determine the default nullability. | ||||||||||||
3971 | static PointerDeclaratorKind | ||||||||||||
3972 | classifyPointerDeclarator(Sema &S, QualType type, Declarator &declarator, | ||||||||||||
3973 | PointerWrappingDeclaratorKind &wrappingKind) { | ||||||||||||
3974 | unsigned numNormalPointers = 0; | ||||||||||||
3975 | |||||||||||||
3976 | // For any dependent type, we consider it a non-pointer. | ||||||||||||
3977 | if (type->isDependentType()) | ||||||||||||
3978 | return PointerDeclaratorKind::NonPointer; | ||||||||||||
3979 | |||||||||||||
3980 | // Look through the declarator chunks to identify pointers. | ||||||||||||
3981 | for (unsigned i = 0, n = declarator.getNumTypeObjects(); i != n; ++i) { | ||||||||||||
3982 | DeclaratorChunk &chunk = declarator.getTypeObject(i); | ||||||||||||
3983 | switch (chunk.Kind) { | ||||||||||||
3984 | case DeclaratorChunk::Array: | ||||||||||||
3985 | if (numNormalPointers == 0) | ||||||||||||
3986 | wrappingKind = PointerWrappingDeclaratorKind::Array; | ||||||||||||
3987 | break; | ||||||||||||
3988 | |||||||||||||
3989 | case DeclaratorChunk::Function: | ||||||||||||
3990 | case DeclaratorChunk::Pipe: | ||||||||||||
3991 | break; | ||||||||||||
3992 | |||||||||||||
3993 | case DeclaratorChunk::BlockPointer: | ||||||||||||
3994 | case DeclaratorChunk::MemberPointer: | ||||||||||||
3995 | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer | ||||||||||||
3996 | : PointerDeclaratorKind::SingleLevelPointer; | ||||||||||||
3997 | |||||||||||||
3998 | case DeclaratorChunk::Paren: | ||||||||||||
3999 | break; | ||||||||||||
4000 | |||||||||||||
4001 | case DeclaratorChunk::Reference: | ||||||||||||
4002 | if (numNormalPointers == 0) | ||||||||||||
4003 | wrappingKind = PointerWrappingDeclaratorKind::Reference; | ||||||||||||
4004 | break; | ||||||||||||
4005 | |||||||||||||
4006 | case DeclaratorChunk::Pointer: | ||||||||||||
4007 | ++numNormalPointers; | ||||||||||||
4008 | if (numNormalPointers > 2) | ||||||||||||
4009 | return PointerDeclaratorKind::MultiLevelPointer; | ||||||||||||
4010 | break; | ||||||||||||
4011 | } | ||||||||||||
4012 | } | ||||||||||||
4013 | |||||||||||||
4014 | // Then, dig into the type specifier itself. | ||||||||||||
4015 | unsigned numTypeSpecifierPointers = 0; | ||||||||||||
4016 | do { | ||||||||||||
4017 | // Decompose normal pointers. | ||||||||||||
4018 | if (auto ptrType = type->getAs<PointerType>()) { | ||||||||||||
4019 | ++numNormalPointers; | ||||||||||||
4020 | |||||||||||||
4021 | if (numNormalPointers > 2) | ||||||||||||
4022 | return PointerDeclaratorKind::MultiLevelPointer; | ||||||||||||
4023 | |||||||||||||
4024 | type = ptrType->getPointeeType(); | ||||||||||||
4025 | ++numTypeSpecifierPointers; | ||||||||||||
4026 | continue; | ||||||||||||
4027 | } | ||||||||||||
4028 | |||||||||||||
4029 | // Decompose block pointers. | ||||||||||||
4030 | if (type->getAs<BlockPointerType>()) { | ||||||||||||
4031 | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer | ||||||||||||
4032 | : PointerDeclaratorKind::SingleLevelPointer; | ||||||||||||
4033 | } | ||||||||||||
4034 | |||||||||||||
4035 | // Decompose member pointers. | ||||||||||||
4036 | if (type->getAs<MemberPointerType>()) { | ||||||||||||
4037 | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer | ||||||||||||
4038 | : PointerDeclaratorKind::SingleLevelPointer; | ||||||||||||
4039 | } | ||||||||||||
4040 | |||||||||||||
4041 | // Look at Objective-C object pointers. | ||||||||||||
4042 | if (auto objcObjectPtr = type->getAs<ObjCObjectPointerType>()) { | ||||||||||||
4043 | ++numNormalPointers; | ||||||||||||
4044 | ++numTypeSpecifierPointers; | ||||||||||||
4045 | |||||||||||||
4046 | // If this is NSError**, report that. | ||||||||||||
4047 | if (auto objcClassDecl = objcObjectPtr->getInterfaceDecl()) { | ||||||||||||
4048 | if (objcClassDecl->getIdentifier() == S.getNSErrorIdent() && | ||||||||||||
4049 | numNormalPointers == 2 && numTypeSpecifierPointers < 2) { | ||||||||||||
4050 | return PointerDeclaratorKind::NSErrorPointerPointer; | ||||||||||||
4051 | } | ||||||||||||
4052 | } | ||||||||||||
4053 | |||||||||||||
4054 | break; | ||||||||||||
4055 | } | ||||||||||||
4056 | |||||||||||||
4057 | // Look at Objective-C class types. | ||||||||||||
4058 | if (auto objcClass = type->getAs<ObjCInterfaceType>()) { | ||||||||||||
4059 | if (objcClass->getInterface()->getIdentifier() == S.getNSErrorIdent()) { | ||||||||||||
4060 | if (numNormalPointers == 2 && numTypeSpecifierPointers < 2) | ||||||||||||
4061 | return PointerDeclaratorKind::NSErrorPointerPointer; | ||||||||||||
4062 | } | ||||||||||||
4063 | |||||||||||||
4064 | break; | ||||||||||||
4065 | } | ||||||||||||
4066 | |||||||||||||
4067 | // If at this point we haven't seen a pointer, we won't see one. | ||||||||||||
4068 | if (numNormalPointers == 0) | ||||||||||||
4069 | return PointerDeclaratorKind::NonPointer; | ||||||||||||
4070 | |||||||||||||
4071 | if (auto recordType = type->getAs<RecordType>()) { | ||||||||||||
4072 | RecordDecl *recordDecl = recordType->getDecl(); | ||||||||||||
4073 | |||||||||||||
4074 | // If this is CFErrorRef*, report it as such. | ||||||||||||
4075 | if (numNormalPointers == 2 && numTypeSpecifierPointers < 2 && | ||||||||||||
4076 | S.isCFError(recordDecl)) { | ||||||||||||
4077 | return PointerDeclaratorKind::CFErrorRefPointer; | ||||||||||||
4078 | } | ||||||||||||
4079 | break; | ||||||||||||
4080 | } | ||||||||||||
4081 | |||||||||||||
4082 | break; | ||||||||||||
4083 | } while (true); | ||||||||||||
4084 | |||||||||||||
4085 | switch (numNormalPointers) { | ||||||||||||
4086 | case 0: | ||||||||||||
4087 | return PointerDeclaratorKind::NonPointer; | ||||||||||||
4088 | |||||||||||||
4089 | case 1: | ||||||||||||
4090 | return PointerDeclaratorKind::SingleLevelPointer; | ||||||||||||
4091 | |||||||||||||
4092 | case 2: | ||||||||||||
4093 | return PointerDeclaratorKind::MaybePointerToCFRef; | ||||||||||||
4094 | |||||||||||||
4095 | default: | ||||||||||||
4096 | return PointerDeclaratorKind::MultiLevelPointer; | ||||||||||||
4097 | } | ||||||||||||
4098 | } | ||||||||||||
4099 | |||||||||||||
4100 | bool Sema::isCFError(RecordDecl *RD) { | ||||||||||||
4101 | // If we already know about CFError, test it directly. | ||||||||||||
4102 | if (CFError) | ||||||||||||
4103 | return CFError == RD; | ||||||||||||
4104 | |||||||||||||
4105 | // Check whether this is CFError, which we identify based on its bridge to | ||||||||||||
4106 | // NSError. CFErrorRef used to be declared with "objc_bridge" but is now | ||||||||||||
4107 | // declared with "objc_bridge_mutable", so look for either one of the two | ||||||||||||
4108 | // attributes. | ||||||||||||
4109 | if (RD->getTagKind() == TTK_Struct) { | ||||||||||||
4110 | IdentifierInfo *bridgedType = nullptr; | ||||||||||||
4111 | if (auto bridgeAttr = RD->getAttr<ObjCBridgeAttr>()) | ||||||||||||
4112 | bridgedType = bridgeAttr->getBridgedType(); | ||||||||||||
4113 | else if (auto bridgeAttr = RD->getAttr<ObjCBridgeMutableAttr>()) | ||||||||||||
4114 | bridgedType = bridgeAttr->getBridgedType(); | ||||||||||||
4115 | |||||||||||||
4116 | if (bridgedType == getNSErrorIdent()) { | ||||||||||||
4117 | CFError = RD; | ||||||||||||
4118 | return true; | ||||||||||||
4119 | } | ||||||||||||
4120 | } | ||||||||||||
4121 | |||||||||||||
4122 | return false; | ||||||||||||
4123 | } | ||||||||||||
4124 | |||||||||||||
4125 | static FileID getNullabilityCompletenessCheckFileID(Sema &S, | ||||||||||||
4126 | SourceLocation loc) { | ||||||||||||
4127 | // If we're anywhere in a function, method, or closure context, don't perform | ||||||||||||
4128 | // completeness checks. | ||||||||||||
4129 | for (DeclContext *ctx = S.CurContext; ctx; ctx = ctx->getParent()) { | ||||||||||||
4130 | if (ctx->isFunctionOrMethod()) | ||||||||||||
4131 | return FileID(); | ||||||||||||
4132 | |||||||||||||
4133 | if (ctx->isFileContext()) | ||||||||||||
4134 | break; | ||||||||||||
4135 | } | ||||||||||||
4136 | |||||||||||||
4137 | // We only care about the expansion location. | ||||||||||||
4138 | loc = S.SourceMgr.getExpansionLoc(loc); | ||||||||||||
4139 | FileID file = S.SourceMgr.getFileID(loc); | ||||||||||||
4140 | if (file.isInvalid()) | ||||||||||||
4141 | return FileID(); | ||||||||||||
4142 | |||||||||||||
4143 | // Retrieve file information. | ||||||||||||
4144 | bool invalid = false; | ||||||||||||
4145 | const SrcMgr::SLocEntry &sloc = S.SourceMgr.getSLocEntry(file, &invalid); | ||||||||||||
4146 | if (invalid || !sloc.isFile()) | ||||||||||||
4147 | return FileID(); | ||||||||||||
4148 | |||||||||||||
4149 | // We don't want to perform completeness checks on the main file or in | ||||||||||||
4150 | // system headers. | ||||||||||||
4151 | const SrcMgr::FileInfo &fileInfo = sloc.getFile(); | ||||||||||||
4152 | if (fileInfo.getIncludeLoc().isInvalid()) | ||||||||||||
4153 | return FileID(); | ||||||||||||
4154 | if (fileInfo.getFileCharacteristic() != SrcMgr::C_User && | ||||||||||||
4155 | S.Diags.getSuppressSystemWarnings()) { | ||||||||||||
4156 | return FileID(); | ||||||||||||
4157 | } | ||||||||||||
4158 | |||||||||||||
4159 | return file; | ||||||||||||
4160 | } | ||||||||||||
4161 | |||||||||||||
4162 | /// Creates a fix-it to insert a C-style nullability keyword at \p pointerLoc, | ||||||||||||
4163 | /// taking into account whitespace before and after. | ||||||||||||
4164 | template <typename DiagBuilderT> | ||||||||||||
4165 | static void fixItNullability(Sema &S, DiagBuilderT &Diag, | ||||||||||||
4166 | SourceLocation PointerLoc, | ||||||||||||
4167 | NullabilityKind Nullability) { | ||||||||||||
4168 | assert(PointerLoc.isValid())((PointerLoc.isValid()) ? static_cast<void> (0) : __assert_fail ("PointerLoc.isValid()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 4168, __PRETTY_FUNCTION__)); | ||||||||||||
4169 | if (PointerLoc.isMacroID()) | ||||||||||||
4170 | return; | ||||||||||||
4171 | |||||||||||||
4172 | SourceLocation FixItLoc = S.getLocForEndOfToken(PointerLoc); | ||||||||||||
4173 | if (!FixItLoc.isValid() || FixItLoc == PointerLoc) | ||||||||||||
4174 | return; | ||||||||||||
4175 | |||||||||||||
4176 | const char *NextChar = S.SourceMgr.getCharacterData(FixItLoc); | ||||||||||||
4177 | if (!NextChar) | ||||||||||||
4178 | return; | ||||||||||||
4179 | |||||||||||||
4180 | SmallString<32> InsertionTextBuf{" "}; | ||||||||||||
4181 | InsertionTextBuf += getNullabilitySpelling(Nullability); | ||||||||||||
4182 | InsertionTextBuf += " "; | ||||||||||||
4183 | StringRef InsertionText = InsertionTextBuf.str(); | ||||||||||||
4184 | |||||||||||||
4185 | if (isWhitespace(*NextChar)) { | ||||||||||||
4186 | InsertionText = InsertionText.drop_back(); | ||||||||||||
4187 | } else if (NextChar[-1] == '[') { | ||||||||||||
4188 | if (NextChar[0] == ']') | ||||||||||||
4189 | InsertionText = InsertionText.drop_back().drop_front(); | ||||||||||||
4190 | else | ||||||||||||
4191 | InsertionText = InsertionText.drop_front(); | ||||||||||||
4192 | } else if (!isIdentifierBody(NextChar[0], /*allow dollar*/true) && | ||||||||||||
4193 | !isIdentifierBody(NextChar[-1], /*allow dollar*/true)) { | ||||||||||||
4194 | InsertionText = InsertionText.drop_back().drop_front(); | ||||||||||||
4195 | } | ||||||||||||
4196 | |||||||||||||
4197 | Diag << FixItHint::CreateInsertion(FixItLoc, InsertionText); | ||||||||||||
4198 | } | ||||||||||||
4199 | |||||||||||||
4200 | static void emitNullabilityConsistencyWarning(Sema &S, | ||||||||||||
4201 | SimplePointerKind PointerKind, | ||||||||||||
4202 | SourceLocation PointerLoc, | ||||||||||||
4203 | SourceLocation PointerEndLoc) { | ||||||||||||
4204 | assert(PointerLoc.isValid())((PointerLoc.isValid()) ? static_cast<void> (0) : __assert_fail ("PointerLoc.isValid()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 4204, __PRETTY_FUNCTION__)); | ||||||||||||
4205 | |||||||||||||
4206 | if (PointerKind == SimplePointerKind::Array) { | ||||||||||||
4207 | S.Diag(PointerLoc, diag::warn_nullability_missing_array); | ||||||||||||
4208 | } else { | ||||||||||||
4209 | S.Diag(PointerLoc, diag::warn_nullability_missing) | ||||||||||||
4210 | << static_cast<unsigned>(PointerKind); | ||||||||||||
4211 | } | ||||||||||||
4212 | |||||||||||||
4213 | auto FixItLoc = PointerEndLoc.isValid() ? PointerEndLoc : PointerLoc; | ||||||||||||
4214 | if (FixItLoc.isMacroID()) | ||||||||||||
4215 | return; | ||||||||||||
4216 | |||||||||||||
4217 | auto addFixIt = [&](NullabilityKind Nullability) { | ||||||||||||
4218 | auto Diag = S.Diag(FixItLoc, diag::note_nullability_fix_it); | ||||||||||||
4219 | Diag << static_cast<unsigned>(Nullability); | ||||||||||||
4220 | Diag << static_cast<unsigned>(PointerKind); | ||||||||||||
4221 | fixItNullability(S, Diag, FixItLoc, Nullability); | ||||||||||||
4222 | }; | ||||||||||||
4223 | addFixIt(NullabilityKind::Nullable); | ||||||||||||
4224 | addFixIt(NullabilityKind::NonNull); | ||||||||||||
4225 | } | ||||||||||||
4226 | |||||||||||||
4227 | /// Complains about missing nullability if the file containing \p pointerLoc | ||||||||||||
4228 | /// has other uses of nullability (either the keywords or the \c assume_nonnull | ||||||||||||
4229 | /// pragma). | ||||||||||||
4230 | /// | ||||||||||||
4231 | /// If the file has \e not seen other uses of nullability, this particular | ||||||||||||
4232 | /// pointer is saved for possible later diagnosis. See recordNullabilitySeen(). | ||||||||||||
4233 | static void | ||||||||||||
4234 | checkNullabilityConsistency(Sema &S, SimplePointerKind pointerKind, | ||||||||||||
4235 | SourceLocation pointerLoc, | ||||||||||||
4236 | SourceLocation pointerEndLoc = SourceLocation()) { | ||||||||||||
4237 | // Determine which file we're performing consistency checking for. | ||||||||||||
4238 | FileID file = getNullabilityCompletenessCheckFileID(S, pointerLoc); | ||||||||||||
4239 | if (file.isInvalid()) | ||||||||||||
4240 | return; | ||||||||||||
4241 | |||||||||||||
4242 | // If we haven't seen any type nullability in this file, we won't warn now | ||||||||||||
4243 | // about anything. | ||||||||||||
4244 | FileNullability &fileNullability = S.NullabilityMap[file]; | ||||||||||||
4245 | if (!fileNullability.SawTypeNullability) { | ||||||||||||
4246 | // If this is the first pointer declarator in the file, and the appropriate | ||||||||||||
4247 | // warning is on, record it in case we need to diagnose it retroactively. | ||||||||||||
4248 | diag::kind diagKind; | ||||||||||||
4249 | if (pointerKind == SimplePointerKind::Array) | ||||||||||||
4250 | diagKind = diag::warn_nullability_missing_array; | ||||||||||||
4251 | else | ||||||||||||
4252 | diagKind = diag::warn_nullability_missing; | ||||||||||||
4253 | |||||||||||||
4254 | if (fileNullability.PointerLoc.isInvalid() && | ||||||||||||
4255 | !S.Context.getDiagnostics().isIgnored(diagKind, pointerLoc)) { | ||||||||||||
4256 | fileNullability.PointerLoc = pointerLoc; | ||||||||||||
4257 | fileNullability.PointerEndLoc = pointerEndLoc; | ||||||||||||
4258 | fileNullability.PointerKind = static_cast<unsigned>(pointerKind); | ||||||||||||
4259 | } | ||||||||||||
4260 | |||||||||||||
4261 | return; | ||||||||||||
4262 | } | ||||||||||||
4263 | |||||||||||||
4264 | // Complain about missing nullability. | ||||||||||||
4265 | emitNullabilityConsistencyWarning(S, pointerKind, pointerLoc, pointerEndLoc); | ||||||||||||
4266 | } | ||||||||||||
4267 | |||||||||||||
4268 | /// Marks that a nullability feature has been used in the file containing | ||||||||||||
4269 | /// \p loc. | ||||||||||||
4270 | /// | ||||||||||||
4271 | /// If this file already had pointer types in it that were missing nullability, | ||||||||||||
4272 | /// the first such instance is retroactively diagnosed. | ||||||||||||
4273 | /// | ||||||||||||
4274 | /// \sa checkNullabilityConsistency | ||||||||||||
4275 | static void recordNullabilitySeen(Sema &S, SourceLocation loc) { | ||||||||||||
4276 | FileID file = getNullabilityCompletenessCheckFileID(S, loc); | ||||||||||||
4277 | if (file.isInvalid()) | ||||||||||||
4278 | return; | ||||||||||||
4279 | |||||||||||||
4280 | FileNullability &fileNullability = S.NullabilityMap[file]; | ||||||||||||
4281 | if (fileNullability.SawTypeNullability) | ||||||||||||
4282 | return; | ||||||||||||
4283 | fileNullability.SawTypeNullability = true; | ||||||||||||
4284 | |||||||||||||
4285 | // If we haven't seen any type nullability before, now we have. Retroactively | ||||||||||||
4286 | // diagnose the first unannotated pointer, if there was one. | ||||||||||||
4287 | if (fileNullability.PointerLoc.isInvalid()) | ||||||||||||
4288 | return; | ||||||||||||
4289 | |||||||||||||
4290 | auto kind = static_cast<SimplePointerKind>(fileNullability.PointerKind); | ||||||||||||
4291 | emitNullabilityConsistencyWarning(S, kind, fileNullability.PointerLoc, | ||||||||||||
4292 | fileNullability.PointerEndLoc); | ||||||||||||
4293 | } | ||||||||||||
4294 | |||||||||||||
4295 | /// Returns true if any of the declarator chunks before \p endIndex include a | ||||||||||||
4296 | /// level of indirection: array, pointer, reference, or pointer-to-member. | ||||||||||||
4297 | /// | ||||||||||||
4298 | /// Because declarator chunks are stored in outer-to-inner order, testing | ||||||||||||
4299 | /// every chunk before \p endIndex is testing all chunks that embed the current | ||||||||||||
4300 | /// chunk as part of their type. | ||||||||||||
4301 | /// | ||||||||||||
4302 | /// It is legal to pass the result of Declarator::getNumTypeObjects() as the | ||||||||||||
4303 | /// end index, in which case all chunks are tested. | ||||||||||||
4304 | static bool hasOuterPointerLikeChunk(const Declarator &D, unsigned endIndex) { | ||||||||||||
4305 | unsigned i = endIndex; | ||||||||||||
4306 | while (i != 0) { | ||||||||||||
4307 | // Walk outwards along the declarator chunks. | ||||||||||||
4308 | --i; | ||||||||||||
4309 | const DeclaratorChunk &DC = D.getTypeObject(i); | ||||||||||||
4310 | switch (DC.Kind) { | ||||||||||||
4311 | case DeclaratorChunk::Paren: | ||||||||||||
4312 | break; | ||||||||||||
4313 | case DeclaratorChunk::Array: | ||||||||||||
4314 | case DeclaratorChunk::Pointer: | ||||||||||||
4315 | case DeclaratorChunk::Reference: | ||||||||||||
4316 | case DeclaratorChunk::MemberPointer: | ||||||||||||
4317 | return true; | ||||||||||||
4318 | case DeclaratorChunk::Function: | ||||||||||||
4319 | case DeclaratorChunk::BlockPointer: | ||||||||||||
4320 | case DeclaratorChunk::Pipe: | ||||||||||||
4321 | // These are invalid anyway, so just ignore. | ||||||||||||
4322 | break; | ||||||||||||
4323 | } | ||||||||||||
4324 | } | ||||||||||||
4325 | return false; | ||||||||||||
4326 | } | ||||||||||||
4327 | |||||||||||||
4328 | static bool IsNoDerefableChunk(DeclaratorChunk Chunk) { | ||||||||||||
4329 | return (Chunk.Kind == DeclaratorChunk::Pointer || | ||||||||||||
4330 | Chunk.Kind == DeclaratorChunk::Array); | ||||||||||||
4331 | } | ||||||||||||
4332 | |||||||||||||
4333 | template<typename AttrT> | ||||||||||||
4334 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | ||||||||||||
4335 | AL.setUsedAsTypeAttr(); | ||||||||||||
4336 | return ::new (Ctx) AttrT(Ctx, AL); | ||||||||||||
4337 | } | ||||||||||||
4338 | |||||||||||||
4339 | static Attr *createNullabilityAttr(ASTContext &Ctx, ParsedAttr &Attr, | ||||||||||||
4340 | NullabilityKind NK) { | ||||||||||||
4341 | switch (NK) { | ||||||||||||
4342 | case NullabilityKind::NonNull: | ||||||||||||
4343 | return createSimpleAttr<TypeNonNullAttr>(Ctx, Attr); | ||||||||||||
4344 | |||||||||||||
4345 | case NullabilityKind::Nullable: | ||||||||||||
4346 | return createSimpleAttr<TypeNullableAttr>(Ctx, Attr); | ||||||||||||
4347 | |||||||||||||
4348 | case NullabilityKind::NullableResult: | ||||||||||||
4349 | return createSimpleAttr<TypeNullableResultAttr>(Ctx, Attr); | ||||||||||||
4350 | |||||||||||||
4351 | case NullabilityKind::Unspecified: | ||||||||||||
4352 | return createSimpleAttr<TypeNullUnspecifiedAttr>(Ctx, Attr); | ||||||||||||
4353 | } | ||||||||||||
4354 | llvm_unreachable("unknown NullabilityKind")::llvm::llvm_unreachable_internal("unknown NullabilityKind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 4354); | ||||||||||||
4355 | } | ||||||||||||
4356 | |||||||||||||
4357 | // Diagnose whether this is a case with the multiple addr spaces. | ||||||||||||
4358 | // Returns true if this is an invalid case. | ||||||||||||
4359 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified | ||||||||||||
4360 | // by qualifiers for two or more different address spaces." | ||||||||||||
4361 | static bool DiagnoseMultipleAddrSpaceAttributes(Sema &S, LangAS ASOld, | ||||||||||||
4362 | LangAS ASNew, | ||||||||||||
4363 | SourceLocation AttrLoc) { | ||||||||||||
4364 | if (ASOld != LangAS::Default) { | ||||||||||||
4365 | if (ASOld != ASNew) { | ||||||||||||
4366 | S.Diag(AttrLoc, diag::err_attribute_address_multiple_qualifiers); | ||||||||||||
4367 | return true; | ||||||||||||
4368 | } | ||||||||||||
4369 | // Emit a warning if they are identical; it's likely unintended. | ||||||||||||
4370 | S.Diag(AttrLoc, | ||||||||||||
4371 | diag::warn_attribute_address_multiple_identical_qualifiers); | ||||||||||||
4372 | } | ||||||||||||
4373 | return false; | ||||||||||||
4374 | } | ||||||||||||
4375 | |||||||||||||
4376 | static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, | ||||||||||||
4377 | QualType declSpecType, | ||||||||||||
4378 | TypeSourceInfo *TInfo) { | ||||||||||||
4379 | // The TypeSourceInfo that this function returns will not be a null type. | ||||||||||||
4380 | // If there is an error, this function will fill in a dummy type as fallback. | ||||||||||||
4381 | QualType T = declSpecType; | ||||||||||||
4382 | Declarator &D = state.getDeclarator(); | ||||||||||||
4383 | Sema &S = state.getSema(); | ||||||||||||
4384 | ASTContext &Context = S.Context; | ||||||||||||
4385 | const LangOptions &LangOpts = S.getLangOpts(); | ||||||||||||
4386 | |||||||||||||
4387 | // The name we're declaring, if any. | ||||||||||||
4388 | DeclarationName Name; | ||||||||||||
4389 | if (D.getIdentifier()) | ||||||||||||
| |||||||||||||
4390 | Name = D.getIdentifier(); | ||||||||||||
4391 | |||||||||||||
4392 | // Does this declaration declare a typedef-name? | ||||||||||||
4393 | bool IsTypedefName = | ||||||||||||
4394 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef || | ||||||||||||
4395 | D.getContext() == DeclaratorContext::AliasDecl || | ||||||||||||
4396 | D.getContext() == DeclaratorContext::AliasTemplate; | ||||||||||||
4397 | |||||||||||||
4398 | // Does T refer to a function type with a cv-qualifier or a ref-qualifier? | ||||||||||||
4399 | bool IsQualifiedFunction = T->isFunctionProtoType() && | ||||||||||||
4400 | (!T->castAs<FunctionProtoType>()->getMethodQuals().empty() || | ||||||||||||
4401 | T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None); | ||||||||||||
4402 | |||||||||||||
4403 | // If T is 'decltype(auto)', the only declarators we can have are parens | ||||||||||||
4404 | // and at most one function declarator if this is a function declaration. | ||||||||||||
4405 | // If T is a deduced class template specialization type, we can have no | ||||||||||||
4406 | // declarator chunks at all. | ||||||||||||
4407 | if (auto *DT
| ||||||||||||
4408 | const AutoType *AT = T->getAs<AutoType>(); | ||||||||||||
4409 | bool IsClassTemplateDeduction = isa<DeducedTemplateSpecializationType>(DT); | ||||||||||||
4410 | if ((AT && AT->isDecltypeAuto()) || IsClassTemplateDeduction) { | ||||||||||||
4411 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { | ||||||||||||
4412 | unsigned Index = E - I - 1; | ||||||||||||
4413 | DeclaratorChunk &DeclChunk = D.getTypeObject(Index); | ||||||||||||
4414 | unsigned DiagId = IsClassTemplateDeduction | ||||||||||||
4415 | ? diag::err_deduced_class_template_compound_type | ||||||||||||
4416 | : diag::err_decltype_auto_compound_type; | ||||||||||||
4417 | unsigned DiagKind = 0; | ||||||||||||
4418 | switch (DeclChunk.Kind) { | ||||||||||||
4419 | case DeclaratorChunk::Paren: | ||||||||||||
4420 | // FIXME: Rejecting this is a little silly. | ||||||||||||
4421 | if (IsClassTemplateDeduction) { | ||||||||||||
4422 | DiagKind = 4; | ||||||||||||
4423 | break; | ||||||||||||
4424 | } | ||||||||||||
4425 | continue; | ||||||||||||
4426 | case DeclaratorChunk::Function: { | ||||||||||||
4427 | if (IsClassTemplateDeduction) { | ||||||||||||
4428 | DiagKind = 3; | ||||||||||||
4429 | break; | ||||||||||||
4430 | } | ||||||||||||
4431 | unsigned FnIndex; | ||||||||||||
4432 | if (D.isFunctionDeclarationContext() && | ||||||||||||
4433 | D.isFunctionDeclarator(FnIndex) && FnIndex == Index) | ||||||||||||
4434 | continue; | ||||||||||||
4435 | DiagId = diag::err_decltype_auto_function_declarator_not_declaration; | ||||||||||||
4436 | break; | ||||||||||||
4437 | } | ||||||||||||
4438 | case DeclaratorChunk::Pointer: | ||||||||||||
4439 | case DeclaratorChunk::BlockPointer: | ||||||||||||
4440 | case DeclaratorChunk::MemberPointer: | ||||||||||||
4441 | DiagKind = 0; | ||||||||||||
4442 | break; | ||||||||||||
4443 | case DeclaratorChunk::Reference: | ||||||||||||
4444 | DiagKind = 1; | ||||||||||||
4445 | break; | ||||||||||||
4446 | case DeclaratorChunk::Array: | ||||||||||||
4447 | DiagKind = 2; | ||||||||||||
4448 | break; | ||||||||||||
4449 | case DeclaratorChunk::Pipe: | ||||||||||||
4450 | break; | ||||||||||||
4451 | } | ||||||||||||
4452 | |||||||||||||
4453 | S.Diag(DeclChunk.Loc, DiagId) << DiagKind; | ||||||||||||
4454 | D.setInvalidType(true); | ||||||||||||
4455 | break; | ||||||||||||
4456 | } | ||||||||||||
4457 | } | ||||||||||||
4458 | } | ||||||||||||
4459 | |||||||||||||
4460 | // Determine whether we should infer _Nonnull on pointer types. | ||||||||||||
4461 | Optional<NullabilityKind> inferNullability; | ||||||||||||
4462 | bool inferNullabilityCS = false; | ||||||||||||
4463 | bool inferNullabilityInnerOnly = false; | ||||||||||||
4464 | bool inferNullabilityInnerOnlyComplete = false; | ||||||||||||
4465 | |||||||||||||
4466 | // Are we in an assume-nonnull region? | ||||||||||||
4467 | bool inAssumeNonNullRegion = false; | ||||||||||||
4468 | SourceLocation assumeNonNullLoc = S.PP.getPragmaAssumeNonNullLoc(); | ||||||||||||
4469 | if (assumeNonNullLoc.isValid()) { | ||||||||||||
4470 | inAssumeNonNullRegion = true; | ||||||||||||
4471 | recordNullabilitySeen(S, assumeNonNullLoc); | ||||||||||||
4472 | } | ||||||||||||
4473 | |||||||||||||
4474 | // Whether to complain about missing nullability specifiers or not. | ||||||||||||
4475 | enum { | ||||||||||||
4476 | /// Never complain. | ||||||||||||
4477 | CAMN_No, | ||||||||||||
4478 | /// Complain on the inner pointers (but not the outermost | ||||||||||||
4479 | /// pointer). | ||||||||||||
4480 | CAMN_InnerPointers, | ||||||||||||
4481 | /// Complain about any pointers that don't have nullability | ||||||||||||
4482 | /// specified or inferred. | ||||||||||||
4483 | CAMN_Yes | ||||||||||||
4484 | } complainAboutMissingNullability = CAMN_No; | ||||||||||||
4485 | unsigned NumPointersRemaining = 0; | ||||||||||||
4486 | auto complainAboutInferringWithinChunk = PointerWrappingDeclaratorKind::None; | ||||||||||||
4487 | |||||||||||||
4488 | if (IsTypedefName
| ||||||||||||
4489 | // For typedefs, we do not infer any nullability (the default), | ||||||||||||
4490 | // and we only complain about missing nullability specifiers on | ||||||||||||
4491 | // inner pointers. | ||||||||||||
4492 | complainAboutMissingNullability = CAMN_InnerPointers; | ||||||||||||
4493 | |||||||||||||
4494 | if (T->canHaveNullability(/*ResultIfUnknown*/false) && | ||||||||||||
4495 | !T->getNullability(S.Context)) { | ||||||||||||
4496 | // Note that we allow but don't require nullability on dependent types. | ||||||||||||
4497 | ++NumPointersRemaining; | ||||||||||||
4498 | } | ||||||||||||
4499 | |||||||||||||
4500 | for (unsigned i = 0, n = D.getNumTypeObjects(); i != n; ++i) { | ||||||||||||
4501 | DeclaratorChunk &chunk = D.getTypeObject(i); | ||||||||||||
4502 | switch (chunk.Kind) { | ||||||||||||
4503 | case DeclaratorChunk::Array: | ||||||||||||
4504 | case DeclaratorChunk::Function: | ||||||||||||
4505 | case DeclaratorChunk::Pipe: | ||||||||||||
4506 | break; | ||||||||||||
4507 | |||||||||||||
4508 | case DeclaratorChunk::BlockPointer: | ||||||||||||
4509 | case DeclaratorChunk::MemberPointer: | ||||||||||||
4510 | ++NumPointersRemaining; | ||||||||||||
4511 | break; | ||||||||||||
4512 | |||||||||||||
4513 | case DeclaratorChunk::Paren: | ||||||||||||
4514 | case DeclaratorChunk::Reference: | ||||||||||||
4515 | continue; | ||||||||||||
4516 | |||||||||||||
4517 | case DeclaratorChunk::Pointer: | ||||||||||||
4518 | ++NumPointersRemaining; | ||||||||||||
4519 | continue; | ||||||||||||
4520 | } | ||||||||||||
4521 | } | ||||||||||||
4522 | } else { | ||||||||||||
4523 | bool isFunctionOrMethod = false; | ||||||||||||
4524 | switch (auto context = state.getDeclarator().getContext()) { | ||||||||||||
4525 | case DeclaratorContext::ObjCParameter: | ||||||||||||
4526 | case DeclaratorContext::ObjCResult: | ||||||||||||
4527 | case DeclaratorContext::Prototype: | ||||||||||||
4528 | case DeclaratorContext::TrailingReturn: | ||||||||||||
4529 | case DeclaratorContext::TrailingReturnVar: | ||||||||||||
4530 | isFunctionOrMethod = true; | ||||||||||||
4531 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
4532 | |||||||||||||
4533 | case DeclaratorContext::Member: | ||||||||||||
4534 | if (state.getDeclarator().isObjCIvar() && !isFunctionOrMethod) { | ||||||||||||
4535 | complainAboutMissingNullability = CAMN_No; | ||||||||||||
4536 | break; | ||||||||||||
4537 | } | ||||||||||||
4538 | |||||||||||||
4539 | // Weak properties are inferred to be nullable. | ||||||||||||
4540 | if (state.getDeclarator().isObjCWeakProperty() && inAssumeNonNullRegion) { | ||||||||||||
4541 | inferNullability = NullabilityKind::Nullable; | ||||||||||||
4542 | break; | ||||||||||||
4543 | } | ||||||||||||
4544 | |||||||||||||
4545 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
4546 | |||||||||||||
4547 | case DeclaratorContext::File: | ||||||||||||
4548 | case DeclaratorContext::KNRTypeList: { | ||||||||||||
4549 | complainAboutMissingNullability = CAMN_Yes; | ||||||||||||
4550 | |||||||||||||
4551 | // Nullability inference depends on the type and declarator. | ||||||||||||
4552 | auto wrappingKind = PointerWrappingDeclaratorKind::None; | ||||||||||||
4553 | switch (classifyPointerDeclarator(S, T, D, wrappingKind)) { | ||||||||||||
4554 | case PointerDeclaratorKind::NonPointer: | ||||||||||||
4555 | case PointerDeclaratorKind::MultiLevelPointer: | ||||||||||||
4556 | // Cannot infer nullability. | ||||||||||||
4557 | break; | ||||||||||||
4558 | |||||||||||||
4559 | case PointerDeclaratorKind::SingleLevelPointer: | ||||||||||||
4560 | // Infer _Nonnull if we are in an assumes-nonnull region. | ||||||||||||
4561 | if (inAssumeNonNullRegion) { | ||||||||||||
4562 | complainAboutInferringWithinChunk = wrappingKind; | ||||||||||||
4563 | inferNullability = NullabilityKind::NonNull; | ||||||||||||
4564 | inferNullabilityCS = (context == DeclaratorContext::ObjCParameter || | ||||||||||||
4565 | context == DeclaratorContext::ObjCResult); | ||||||||||||
4566 | } | ||||||||||||
4567 | break; | ||||||||||||
4568 | |||||||||||||
4569 | case PointerDeclaratorKind::CFErrorRefPointer: | ||||||||||||
4570 | case PointerDeclaratorKind::NSErrorPointerPointer: | ||||||||||||
4571 | // Within a function or method signature, infer _Nullable at both | ||||||||||||
4572 | // levels. | ||||||||||||
4573 | if (isFunctionOrMethod && inAssumeNonNullRegion) | ||||||||||||
4574 | inferNullability = NullabilityKind::Nullable; | ||||||||||||
4575 | break; | ||||||||||||
4576 | |||||||||||||
4577 | case PointerDeclaratorKind::MaybePointerToCFRef: | ||||||||||||
4578 | if (isFunctionOrMethod) { | ||||||||||||
4579 | // On pointer-to-pointer parameters marked cf_returns_retained or | ||||||||||||
4580 | // cf_returns_not_retained, if the outer pointer is explicit then | ||||||||||||
4581 | // infer the inner pointer as _Nullable. | ||||||||||||
4582 | auto hasCFReturnsAttr = | ||||||||||||
4583 | [](const ParsedAttributesView &AttrList) -> bool { | ||||||||||||
4584 | return AttrList.hasAttribute(ParsedAttr::AT_CFReturnsRetained) || | ||||||||||||
4585 | AttrList.hasAttribute(ParsedAttr::AT_CFReturnsNotRetained); | ||||||||||||
4586 | }; | ||||||||||||
4587 | if (const auto *InnermostChunk = D.getInnermostNonParenChunk()) { | ||||||||||||
4588 | if (hasCFReturnsAttr(D.getAttributes()) || | ||||||||||||
4589 | hasCFReturnsAttr(InnermostChunk->getAttrs()) || | ||||||||||||
4590 | hasCFReturnsAttr(D.getDeclSpec().getAttributes())) { | ||||||||||||
4591 | inferNullability = NullabilityKind::Nullable; | ||||||||||||
4592 | inferNullabilityInnerOnly = true; | ||||||||||||
4593 | } | ||||||||||||
4594 | } | ||||||||||||
4595 | } | ||||||||||||
4596 | break; | ||||||||||||
4597 | } | ||||||||||||
4598 | break; | ||||||||||||
4599 | } | ||||||||||||
4600 | |||||||||||||
4601 | case DeclaratorContext::ConversionId: | ||||||||||||
4602 | complainAboutMissingNullability = CAMN_Yes; | ||||||||||||
4603 | break; | ||||||||||||
4604 | |||||||||||||
4605 | case DeclaratorContext::AliasDecl: | ||||||||||||
4606 | case DeclaratorContext::AliasTemplate: | ||||||||||||
4607 | case DeclaratorContext::Block: | ||||||||||||
4608 | case DeclaratorContext::BlockLiteral: | ||||||||||||
4609 | case DeclaratorContext::Condition: | ||||||||||||
4610 | case DeclaratorContext::CXXCatch: | ||||||||||||
4611 | case DeclaratorContext::CXXNew: | ||||||||||||
4612 | case DeclaratorContext::ForInit: | ||||||||||||
4613 | case DeclaratorContext::SelectionInit: | ||||||||||||
4614 | case DeclaratorContext::LambdaExpr: | ||||||||||||
4615 | case DeclaratorContext::LambdaExprParameter: | ||||||||||||
4616 | case DeclaratorContext::ObjCCatch: | ||||||||||||
4617 | case DeclaratorContext::TemplateParam: | ||||||||||||
4618 | case DeclaratorContext::TemplateArg: | ||||||||||||
4619 | case DeclaratorContext::TemplateTypeArg: | ||||||||||||
4620 | case DeclaratorContext::TypeName: | ||||||||||||
4621 | case DeclaratorContext::FunctionalCast: | ||||||||||||
4622 | case DeclaratorContext::RequiresExpr: | ||||||||||||
4623 | // Don't infer in these contexts. | ||||||||||||
4624 | break; | ||||||||||||
4625 | } | ||||||||||||
4626 | } | ||||||||||||
4627 | |||||||||||||
4628 | // Local function that returns true if its argument looks like a va_list. | ||||||||||||
4629 | auto isVaList = [&S](QualType T) -> bool { | ||||||||||||
4630 | auto *typedefTy = T->getAs<TypedefType>(); | ||||||||||||
4631 | if (!typedefTy) | ||||||||||||
4632 | return false; | ||||||||||||
4633 | TypedefDecl *vaListTypedef = S.Context.getBuiltinVaListDecl(); | ||||||||||||
4634 | do { | ||||||||||||
4635 | if (typedefTy->getDecl() == vaListTypedef) | ||||||||||||
4636 | return true; | ||||||||||||
4637 | if (auto *name = typedefTy->getDecl()->getIdentifier()) | ||||||||||||
4638 | if (name->isStr("va_list")) | ||||||||||||
4639 | return true; | ||||||||||||
4640 | typedefTy = typedefTy->desugar()->getAs<TypedefType>(); | ||||||||||||
4641 | } while (typedefTy); | ||||||||||||
4642 | return false; | ||||||||||||
4643 | }; | ||||||||||||
4644 | |||||||||||||
4645 | // Local function that checks the nullability for a given pointer declarator. | ||||||||||||
4646 | // Returns true if _Nonnull was inferred. | ||||||||||||
4647 | auto inferPointerNullability = | ||||||||||||
4648 | [&](SimplePointerKind pointerKind, SourceLocation pointerLoc, | ||||||||||||
4649 | SourceLocation pointerEndLoc, | ||||||||||||
4650 | ParsedAttributesView &attrs, AttributePool &Pool) -> ParsedAttr * { | ||||||||||||
4651 | // We've seen a pointer. | ||||||||||||
4652 | if (NumPointersRemaining > 0) | ||||||||||||
4653 | --NumPointersRemaining; | ||||||||||||
4654 | |||||||||||||
4655 | // If a nullability attribute is present, there's nothing to do. | ||||||||||||
4656 | if (hasNullabilityAttr(attrs)) | ||||||||||||
4657 | return nullptr; | ||||||||||||
4658 | |||||||||||||
4659 | // If we're supposed to infer nullability, do so now. | ||||||||||||
4660 | if (inferNullability && !inferNullabilityInnerOnlyComplete) { | ||||||||||||
4661 | ParsedAttr::Syntax syntax = inferNullabilityCS | ||||||||||||
4662 | ? ParsedAttr::AS_ContextSensitiveKeyword | ||||||||||||
4663 | : ParsedAttr::AS_Keyword; | ||||||||||||
4664 | ParsedAttr *nullabilityAttr = Pool.create( | ||||||||||||
4665 | S.getNullabilityKeyword(*inferNullability), SourceRange(pointerLoc), | ||||||||||||
4666 | nullptr, SourceLocation(), nullptr, 0, syntax); | ||||||||||||
4667 | |||||||||||||
4668 | attrs.addAtEnd(nullabilityAttr); | ||||||||||||
4669 | |||||||||||||
4670 | if (inferNullabilityCS) { | ||||||||||||
4671 | state.getDeclarator().getMutableDeclSpec().getObjCQualifiers() | ||||||||||||
4672 | ->setObjCDeclQualifier(ObjCDeclSpec::DQ_CSNullability); | ||||||||||||
4673 | } | ||||||||||||
4674 | |||||||||||||
4675 | if (pointerLoc.isValid() && | ||||||||||||
4676 | complainAboutInferringWithinChunk != | ||||||||||||
4677 | PointerWrappingDeclaratorKind::None) { | ||||||||||||
4678 | auto Diag = | ||||||||||||
4679 | S.Diag(pointerLoc, diag::warn_nullability_inferred_on_nested_type); | ||||||||||||
4680 | Diag << static_cast<int>(complainAboutInferringWithinChunk); | ||||||||||||
4681 | fixItNullability(S, Diag, pointerLoc, NullabilityKind::NonNull); | ||||||||||||
4682 | } | ||||||||||||
4683 | |||||||||||||
4684 | if (inferNullabilityInnerOnly) | ||||||||||||
4685 | inferNullabilityInnerOnlyComplete = true; | ||||||||||||
4686 | return nullabilityAttr; | ||||||||||||
4687 | } | ||||||||||||
4688 | |||||||||||||
4689 | // If we're supposed to complain about missing nullability, do so | ||||||||||||
4690 | // now if it's truly missing. | ||||||||||||
4691 | switch (complainAboutMissingNullability) { | ||||||||||||
4692 | case CAMN_No: | ||||||||||||
4693 | break; | ||||||||||||
4694 | |||||||||||||
4695 | case CAMN_InnerPointers: | ||||||||||||
4696 | if (NumPointersRemaining == 0) | ||||||||||||
4697 | break; | ||||||||||||
4698 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
4699 | |||||||||||||
4700 | case CAMN_Yes: | ||||||||||||
4701 | checkNullabilityConsistency(S, pointerKind, pointerLoc, pointerEndLoc); | ||||||||||||
4702 | } | ||||||||||||
4703 | return nullptr; | ||||||||||||
4704 | }; | ||||||||||||
4705 | |||||||||||||
4706 | // If the type itself could have nullability but does not, infer pointer | ||||||||||||
4707 | // nullability and perform consistency checking. | ||||||||||||
4708 | if (S.CodeSynthesisContexts.empty()) { | ||||||||||||
4709 | if (T->canHaveNullability(/*ResultIfUnknown*/false) && | ||||||||||||
4710 | !T->getNullability(S.Context)) { | ||||||||||||
4711 | if (isVaList(T)) { | ||||||||||||
4712 | // Record that we've seen a pointer, but do nothing else. | ||||||||||||
4713 | if (NumPointersRemaining > 0) | ||||||||||||
4714 | --NumPointersRemaining; | ||||||||||||
4715 | } else { | ||||||||||||
4716 | SimplePointerKind pointerKind = SimplePointerKind::Pointer; | ||||||||||||
4717 | if (T->isBlockPointerType()) | ||||||||||||
4718 | pointerKind = SimplePointerKind::BlockPointer; | ||||||||||||
4719 | else if (T->isMemberPointerType()) | ||||||||||||
4720 | pointerKind = SimplePointerKind::MemberPointer; | ||||||||||||
4721 | |||||||||||||
4722 | if (auto *attr = inferPointerNullability( | ||||||||||||
4723 | pointerKind, D.getDeclSpec().getTypeSpecTypeLoc(), | ||||||||||||
4724 | D.getDeclSpec().getEndLoc(), | ||||||||||||
4725 | D.getMutableDeclSpec().getAttributes(), | ||||||||||||
4726 | D.getMutableDeclSpec().getAttributePool())) { | ||||||||||||
4727 | T = state.getAttributedType( | ||||||||||||
4728 | createNullabilityAttr(Context, *attr, *inferNullability), T, T); | ||||||||||||
4729 | } | ||||||||||||
4730 | } | ||||||||||||
4731 | } | ||||||||||||
4732 | |||||||||||||
4733 | if (complainAboutMissingNullability == CAMN_Yes && | ||||||||||||
4734 | T->isArrayType() && !T->getNullability(S.Context) && !isVaList(T) && | ||||||||||||
4735 | D.isPrototypeContext() && | ||||||||||||
4736 | !hasOuterPointerLikeChunk(D, D.getNumTypeObjects())) { | ||||||||||||
4737 | checkNullabilityConsistency(S, SimplePointerKind::Array, | ||||||||||||
4738 | D.getDeclSpec().getTypeSpecTypeLoc()); | ||||||||||||
4739 | } | ||||||||||||
4740 | } | ||||||||||||
4741 | |||||||||||||
4742 | bool ExpectNoDerefChunk = | ||||||||||||
4743 | state.getCurrentAttributes().hasAttribute(ParsedAttr::AT_NoDeref); | ||||||||||||
4744 | |||||||||||||
4745 | // Walk the DeclTypeInfo, building the recursive type as we go. | ||||||||||||
4746 | // DeclTypeInfos are ordered from the identifier out, which is | ||||||||||||
4747 | // opposite of what we want :). | ||||||||||||
4748 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
4749 | unsigned chunkIndex = e - i - 1; | ||||||||||||
4750 | state.setCurrentChunkIndex(chunkIndex); | ||||||||||||
4751 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); | ||||||||||||
4752 | IsQualifiedFunction &= DeclType.Kind == DeclaratorChunk::Paren; | ||||||||||||
4753 | switch (DeclType.Kind) { | ||||||||||||
4754 | case DeclaratorChunk::Paren: | ||||||||||||
4755 | if (i == 0) | ||||||||||||
4756 | warnAboutRedundantParens(S, D, T); | ||||||||||||
4757 | T = S.BuildParenType(T); | ||||||||||||
4758 | break; | ||||||||||||
4759 | case DeclaratorChunk::BlockPointer: | ||||||||||||
4760 | // If blocks are disabled, emit an error. | ||||||||||||
4761 | if (!LangOpts.Blocks) | ||||||||||||
4762 | S.Diag(DeclType.Loc, diag::err_blocks_disable) << LangOpts.OpenCL; | ||||||||||||
4763 | |||||||||||||
4764 | // Handle pointer nullability. | ||||||||||||
4765 | inferPointerNullability(SimplePointerKind::BlockPointer, DeclType.Loc, | ||||||||||||
4766 | DeclType.EndLoc, DeclType.getAttrs(), | ||||||||||||
4767 | state.getDeclarator().getAttributePool()); | ||||||||||||
4768 | |||||||||||||
4769 | T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name); | ||||||||||||
4770 | if (DeclType.Cls.TypeQuals || LangOpts.OpenCL) { | ||||||||||||
4771 | // OpenCL v2.0, s6.12.5 - Block variable declarations are implicitly | ||||||||||||
4772 | // qualified with const. | ||||||||||||
4773 | if (LangOpts.OpenCL) | ||||||||||||
4774 | DeclType.Cls.TypeQuals |= DeclSpec::TQ_const; | ||||||||||||
4775 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); | ||||||||||||
4776 | } | ||||||||||||
4777 | break; | ||||||||||||
4778 | case DeclaratorChunk::Pointer: | ||||||||||||
4779 | // Verify that we're not building a pointer to pointer to function with | ||||||||||||
4780 | // exception specification. | ||||||||||||
4781 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { | ||||||||||||
4782 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); | ||||||||||||
4783 | D.setInvalidType(true); | ||||||||||||
4784 | // Build the type anyway. | ||||||||||||
4785 | } | ||||||||||||
4786 | |||||||||||||
4787 | // Handle pointer nullability | ||||||||||||
4788 | inferPointerNullability(SimplePointerKind::Pointer, DeclType.Loc, | ||||||||||||
4789 | DeclType.EndLoc, DeclType.getAttrs(), | ||||||||||||
4790 | state.getDeclarator().getAttributePool()); | ||||||||||||
4791 | |||||||||||||
4792 | if (LangOpts.ObjC && T->getAs<ObjCObjectType>()) { | ||||||||||||
4793 | T = Context.getObjCObjectPointerType(T); | ||||||||||||
4794 | if (DeclType.Ptr.TypeQuals) | ||||||||||||
4795 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); | ||||||||||||
4796 | break; | ||||||||||||
4797 | } | ||||||||||||
4798 | |||||||||||||
4799 | // OpenCL v2.0 s6.9b - Pointer to image/sampler cannot be used. | ||||||||||||
4800 | // OpenCL v2.0 s6.13.16.1 - Pointer to pipe cannot be used. | ||||||||||||
4801 | // OpenCL v2.0 s6.12.5 - Pointers to Blocks are not allowed. | ||||||||||||
4802 | if (LangOpts.OpenCL) { | ||||||||||||
4803 | if (T->isImageType() || T->isSamplerT() || T->isPipeType() || | ||||||||||||
4804 | T->isBlockPointerType()) { | ||||||||||||
4805 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_pointer_to_type) << T; | ||||||||||||
4806 | D.setInvalidType(true); | ||||||||||||
4807 | } | ||||||||||||
4808 | } | ||||||||||||
4809 | |||||||||||||
4810 | T = S.BuildPointerType(T, DeclType.Loc, Name); | ||||||||||||
4811 | if (DeclType.Ptr.TypeQuals) | ||||||||||||
4812 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); | ||||||||||||
4813 | break; | ||||||||||||
4814 | case DeclaratorChunk::Reference: { | ||||||||||||
4815 | // Verify that we're not building a reference to pointer to function with | ||||||||||||
4816 | // exception specification. | ||||||||||||
4817 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { | ||||||||||||
4818 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); | ||||||||||||
4819 | D.setInvalidType(true); | ||||||||||||
4820 | // Build the type anyway. | ||||||||||||
4821 | } | ||||||||||||
4822 | T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); | ||||||||||||
4823 | |||||||||||||
4824 | if (DeclType.Ref.HasRestrict) | ||||||||||||
4825 | T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); | ||||||||||||
4826 | break; | ||||||||||||
4827 | } | ||||||||||||
4828 | case DeclaratorChunk::Array: { | ||||||||||||
4829 | // Verify that we're not building an array of pointers to function with | ||||||||||||
4830 | // exception specification. | ||||||||||||
4831 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { | ||||||||||||
4832 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); | ||||||||||||
4833 | D.setInvalidType(true); | ||||||||||||
4834 | // Build the type anyway. | ||||||||||||
4835 | } | ||||||||||||
4836 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; | ||||||||||||
4837 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); | ||||||||||||
4838 | ArrayType::ArraySizeModifier ASM; | ||||||||||||
4839 | if (ATI.isStar) | ||||||||||||
4840 | ASM = ArrayType::Star; | ||||||||||||
4841 | else if (ATI.hasStatic) | ||||||||||||
4842 | ASM = ArrayType::Static; | ||||||||||||
4843 | else | ||||||||||||
4844 | ASM = ArrayType::Normal; | ||||||||||||
4845 | if (ASM == ArrayType::Star && !D.isPrototypeContext()) { | ||||||||||||
4846 | // FIXME: This check isn't quite right: it allows star in prototypes | ||||||||||||
4847 | // for function definitions, and disallows some edge cases detailed | ||||||||||||
4848 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html | ||||||||||||
4849 | S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype); | ||||||||||||
4850 | ASM = ArrayType::Normal; | ||||||||||||
4851 | D.setInvalidType(true); | ||||||||||||
4852 | } | ||||||||||||
4853 | |||||||||||||
4854 | // C99 6.7.5.2p1: The optional type qualifiers and the keyword static | ||||||||||||
4855 | // shall appear only in a declaration of a function parameter with an | ||||||||||||
4856 | // array type, ... | ||||||||||||
4857 | if (ASM == ArrayType::Static || ATI.TypeQuals) { | ||||||||||||
4858 | if (!(D.isPrototypeContext() || | ||||||||||||
4859 | D.getContext() == DeclaratorContext::KNRTypeList)) { | ||||||||||||
4860 | S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) << | ||||||||||||
4861 | (ASM == ArrayType::Static ? "'static'" : "type qualifier"); | ||||||||||||
4862 | // Remove the 'static' and the type qualifiers. | ||||||||||||
4863 | if (ASM == ArrayType::Static) | ||||||||||||
4864 | ASM = ArrayType::Normal; | ||||||||||||
4865 | ATI.TypeQuals = 0; | ||||||||||||
4866 | D.setInvalidType(true); | ||||||||||||
4867 | } | ||||||||||||
4868 | |||||||||||||
4869 | // C99 6.7.5.2p1: ... and then only in the outermost array type | ||||||||||||
4870 | // derivation. | ||||||||||||
4871 | if (hasOuterPointerLikeChunk(D, chunkIndex)) { | ||||||||||||
4872 | S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) << | ||||||||||||
4873 | (ASM == ArrayType::Static ? "'static'" : "type qualifier"); | ||||||||||||
4874 | if (ASM == ArrayType::Static) | ||||||||||||
4875 | ASM = ArrayType::Normal; | ||||||||||||
4876 | ATI.TypeQuals = 0; | ||||||||||||
4877 | D.setInvalidType(true); | ||||||||||||
4878 | } | ||||||||||||
4879 | } | ||||||||||||
4880 | const AutoType *AT = T->getContainedAutoType(); | ||||||||||||
4881 | // Allow arrays of auto if we are a generic lambda parameter. | ||||||||||||
4882 | // i.e. [](auto (&array)[5]) { return array[0]; }; OK | ||||||||||||
4883 | if (AT && D.getContext() != DeclaratorContext::LambdaExprParameter) { | ||||||||||||
4884 | // We've already diagnosed this for decltype(auto). | ||||||||||||
4885 | if (!AT->isDecltypeAuto()) | ||||||||||||
4886 | S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto) | ||||||||||||
4887 | << getPrintableNameForEntity(Name) << T; | ||||||||||||
4888 | T = QualType(); | ||||||||||||
4889 | break; | ||||||||||||
4890 | } | ||||||||||||
4891 | |||||||||||||
4892 | // Array parameters can be marked nullable as well, although it's not | ||||||||||||
4893 | // necessary if they're marked 'static'. | ||||||||||||
4894 | if (complainAboutMissingNullability == CAMN_Yes && | ||||||||||||
4895 | !hasNullabilityAttr(DeclType.getAttrs()) && | ||||||||||||
4896 | ASM != ArrayType::Static && | ||||||||||||
4897 | D.isPrototypeContext() && | ||||||||||||
4898 | !hasOuterPointerLikeChunk(D, chunkIndex)) { | ||||||||||||
4899 | checkNullabilityConsistency(S, SimplePointerKind::Array, DeclType.Loc); | ||||||||||||
4900 | } | ||||||||||||
4901 | |||||||||||||
4902 | T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, | ||||||||||||
4903 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); | ||||||||||||
4904 | break; | ||||||||||||
4905 | } | ||||||||||||
4906 | case DeclaratorChunk::Function: { | ||||||||||||
4907 | // If the function declarator has a prototype (i.e. it is not () and | ||||||||||||
4908 | // does not have a K&R-style identifier list), then the arguments are part | ||||||||||||
4909 | // of the type, otherwise the argument list is (). | ||||||||||||
4910 | DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; | ||||||||||||
4911 | IsQualifiedFunction = | ||||||||||||
4912 | FTI.hasMethodTypeQualifiers() || FTI.hasRefQualifier(); | ||||||||||||
4913 | |||||||||||||
4914 | // Check for auto functions and trailing return type and adjust the | ||||||||||||
4915 | // return type accordingly. | ||||||||||||
4916 | if (!D.isInvalidType()) { | ||||||||||||
4917 | // trailing-return-type is only required if we're declaring a function, | ||||||||||||
4918 | // and not, for instance, a pointer to a function. | ||||||||||||
4919 | if (D.getDeclSpec().hasAutoTypeSpec() && | ||||||||||||
4920 | !FTI.hasTrailingReturnType() && chunkIndex == 0) { | ||||||||||||
4921 | if (!S.getLangOpts().CPlusPlus14) { | ||||||||||||
4922 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), | ||||||||||||
4923 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto | ||||||||||||
4924 | ? diag::err_auto_missing_trailing_return | ||||||||||||
4925 | : diag::err_deduced_return_type); | ||||||||||||
4926 | T = Context.IntTy; | ||||||||||||
4927 | D.setInvalidType(true); | ||||||||||||
4928 | } else { | ||||||||||||
4929 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), | ||||||||||||
4930 | diag::warn_cxx11_compat_deduced_return_type); | ||||||||||||
4931 | } | ||||||||||||
4932 | } else if (FTI.hasTrailingReturnType()) { | ||||||||||||
4933 | // T must be exactly 'auto' at this point. See CWG issue 681. | ||||||||||||
4934 | if (isa<ParenType>(T)) { | ||||||||||||
4935 | S.Diag(D.getBeginLoc(), diag::err_trailing_return_in_parens) | ||||||||||||
4936 | << T << D.getSourceRange(); | ||||||||||||
4937 | D.setInvalidType(true); | ||||||||||||
4938 | } else if (D.getName().getKind() == | ||||||||||||
4939 | UnqualifiedIdKind::IK_DeductionGuideName) { | ||||||||||||
4940 | if (T != Context.DependentTy) { | ||||||||||||
4941 | S.Diag(D.getDeclSpec().getBeginLoc(), | ||||||||||||
4942 | diag::err_deduction_guide_with_complex_decl) | ||||||||||||
4943 | << D.getSourceRange(); | ||||||||||||
4944 | D.setInvalidType(true); | ||||||||||||
4945 | } | ||||||||||||
4946 | } else if (D.getContext() != DeclaratorContext::LambdaExpr && | ||||||||||||
4947 | (T.hasQualifiers() || !isa<AutoType>(T) || | ||||||||||||
4948 | cast<AutoType>(T)->getKeyword() != | ||||||||||||
4949 | AutoTypeKeyword::Auto || | ||||||||||||
4950 | cast<AutoType>(T)->isConstrained())) { | ||||||||||||
4951 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), | ||||||||||||
4952 | diag::err_trailing_return_without_auto) | ||||||||||||
4953 | << T << D.getDeclSpec().getSourceRange(); | ||||||||||||
4954 | D.setInvalidType(true); | ||||||||||||
4955 | } | ||||||||||||
4956 | T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo); | ||||||||||||
4957 | if (T.isNull()) { | ||||||||||||
4958 | // An error occurred parsing the trailing return type. | ||||||||||||
4959 | T = Context.IntTy; | ||||||||||||
4960 | D.setInvalidType(true); | ||||||||||||
4961 | } else if (AutoType *Auto = T->getContainedAutoType()) { | ||||||||||||
4962 | // If the trailing return type contains an `auto`, we may need to | ||||||||||||
4963 | // invent a template parameter for it, for cases like | ||||||||||||
4964 | // `auto f() -> C auto` or `[](auto (*p) -> auto) {}`. | ||||||||||||
4965 | InventedTemplateParameterInfo *InventedParamInfo = nullptr; | ||||||||||||
4966 | if (D.getContext() == DeclaratorContext::Prototype) | ||||||||||||
4967 | InventedParamInfo = &S.InventedParameterInfos.back(); | ||||||||||||
4968 | else if (D.getContext() == DeclaratorContext::LambdaExprParameter) | ||||||||||||
4969 | InventedParamInfo = S.getCurLambda(); | ||||||||||||
4970 | if (InventedParamInfo) { | ||||||||||||
4971 | std::tie(T, TInfo) = InventTemplateParameter( | ||||||||||||
4972 | state, T, TInfo, Auto, *InventedParamInfo); | ||||||||||||
4973 | } | ||||||||||||
4974 | } | ||||||||||||
4975 | } else { | ||||||||||||
4976 | // This function type is not the type of the entity being declared, | ||||||||||||
4977 | // so checking the 'auto' is not the responsibility of this chunk. | ||||||||||||
4978 | } | ||||||||||||
4979 | } | ||||||||||||
4980 | |||||||||||||
4981 | // C99 6.7.5.3p1: The return type may not be a function or array type. | ||||||||||||
4982 | // For conversion functions, we'll diagnose this particular error later. | ||||||||||||
4983 | if (!D.isInvalidType() && (T->isArrayType() || T->isFunctionType()) && | ||||||||||||
4984 | (D.getName().getKind() != | ||||||||||||
4985 | UnqualifiedIdKind::IK_ConversionFunctionId)) { | ||||||||||||
4986 | unsigned diagID = diag::err_func_returning_array_function; | ||||||||||||
4987 | // Last processing chunk in block context means this function chunk | ||||||||||||
4988 | // represents the block. | ||||||||||||
4989 | if (chunkIndex == 0 && | ||||||||||||
4990 | D.getContext() == DeclaratorContext::BlockLiteral) | ||||||||||||
4991 | diagID = diag::err_block_returning_array_function; | ||||||||||||
4992 | S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T; | ||||||||||||
4993 | T = Context.IntTy; | ||||||||||||
4994 | D.setInvalidType(true); | ||||||||||||
4995 | } | ||||||||||||
4996 | |||||||||||||
4997 | // Do not allow returning half FP value. | ||||||||||||
4998 | // FIXME: This really should be in BuildFunctionType. | ||||||||||||
4999 | if (T->isHalfType()) { | ||||||||||||
5000 | if (S.getLangOpts().OpenCL) { | ||||||||||||
5001 | if (!S.getOpenCLOptions().isAvailableOption("cl_khr_fp16", | ||||||||||||
5002 | S.getLangOpts())) { | ||||||||||||
5003 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_invalid_return) | ||||||||||||
5004 | << T << 0 /*pointer hint*/; | ||||||||||||
5005 | D.setInvalidType(true); | ||||||||||||
5006 | } | ||||||||||||
5007 | } else if (!S.getLangOpts().HalfArgsAndReturns) { | ||||||||||||
5008 | S.Diag(D.getIdentifierLoc(), | ||||||||||||
5009 | diag::err_parameters_retval_cannot_have_fp16_type) << 1; | ||||||||||||
5010 | D.setInvalidType(true); | ||||||||||||
5011 | } | ||||||||||||
5012 | } | ||||||||||||
5013 | |||||||||||||
5014 | if (LangOpts.OpenCL) { | ||||||||||||
5015 | // OpenCL v2.0 s6.12.5 - A block cannot be the return value of a | ||||||||||||
5016 | // function. | ||||||||||||
5017 | if (T->isBlockPointerType() || T->isImageType() || T->isSamplerT() || | ||||||||||||
5018 | T->isPipeType()) { | ||||||||||||
5019 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_invalid_return) | ||||||||||||
5020 | << T << 1 /*hint off*/; | ||||||||||||
5021 | D.setInvalidType(true); | ||||||||||||
5022 | } | ||||||||||||
5023 | // OpenCL doesn't support variadic functions and blocks | ||||||||||||
5024 | // (s6.9.e and s6.12.5 OpenCL v2.0) except for printf. | ||||||||||||
5025 | // We also allow here any toolchain reserved identifiers. | ||||||||||||
5026 | if (FTI.isVariadic && | ||||||||||||
5027 | !S.getOpenCLOptions().isAvailableOption( | ||||||||||||
5028 | "__cl_clang_variadic_functions", S.getLangOpts()) && | ||||||||||||
5029 | !(D.getIdentifier() && | ||||||||||||
5030 | ((D.getIdentifier()->getName() == "printf" && | ||||||||||||
5031 | (LangOpts.OpenCLCPlusPlus || LangOpts.OpenCLVersion >= 120)) || | ||||||||||||
5032 | D.getIdentifier()->getName().startswith("__")))) { | ||||||||||||
5033 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_variadic_function); | ||||||||||||
5034 | D.setInvalidType(true); | ||||||||||||
5035 | } | ||||||||||||
5036 | } | ||||||||||||
5037 | |||||||||||||
5038 | // Methods cannot return interface types. All ObjC objects are | ||||||||||||
5039 | // passed by reference. | ||||||||||||
5040 | if (T->isObjCObjectType()) { | ||||||||||||
5041 | SourceLocation DiagLoc, FixitLoc; | ||||||||||||
5042 | if (TInfo) { | ||||||||||||
5043 | DiagLoc = TInfo->getTypeLoc().getBeginLoc(); | ||||||||||||
5044 | FixitLoc = S.getLocForEndOfToken(TInfo->getTypeLoc().getEndLoc()); | ||||||||||||
5045 | } else { | ||||||||||||
5046 | DiagLoc = D.getDeclSpec().getTypeSpecTypeLoc(); | ||||||||||||
5047 | FixitLoc = S.getLocForEndOfToken(D.getDeclSpec().getEndLoc()); | ||||||||||||
5048 | } | ||||||||||||
5049 | S.Diag(DiagLoc, diag::err_object_cannot_be_passed_returned_by_value) | ||||||||||||
5050 | << 0 << T | ||||||||||||
5051 | << FixItHint::CreateInsertion(FixitLoc, "*"); | ||||||||||||
5052 | |||||||||||||
5053 | T = Context.getObjCObjectPointerType(T); | ||||||||||||
5054 | if (TInfo) { | ||||||||||||
5055 | TypeLocBuilder TLB; | ||||||||||||
5056 | TLB.pushFullCopy(TInfo->getTypeLoc()); | ||||||||||||
5057 | ObjCObjectPointerTypeLoc TLoc = TLB.push<ObjCObjectPointerTypeLoc>(T); | ||||||||||||
5058 | TLoc.setStarLoc(FixitLoc); | ||||||||||||
5059 | TInfo = TLB.getTypeSourceInfo(Context, T); | ||||||||||||
5060 | } | ||||||||||||
5061 | |||||||||||||
5062 | D.setInvalidType(true); | ||||||||||||
5063 | } | ||||||||||||
5064 | |||||||||||||
5065 | // cv-qualifiers on return types are pointless except when the type is a | ||||||||||||
5066 | // class type in C++. | ||||||||||||
5067 | if ((T.getCVRQualifiers() || T->isAtomicType()) && | ||||||||||||
5068 | !(S.getLangOpts().CPlusPlus && | ||||||||||||
5069 | (T->isDependentType() || T->isRecordType()))) { | ||||||||||||
5070 | if (T->isVoidType() && !S.getLangOpts().CPlusPlus && | ||||||||||||
5071 | D.getFunctionDefinitionKind() == | ||||||||||||
5072 | FunctionDefinitionKind::Definition) { | ||||||||||||
5073 | // [6.9.1/3] qualified void return is invalid on a C | ||||||||||||
5074 | // function definition. Apparently ok on declarations and | ||||||||||||
5075 | // in C++ though (!) | ||||||||||||
5076 | S.Diag(DeclType.Loc, diag::err_func_returning_qualified_void) << T; | ||||||||||||
5077 | } else | ||||||||||||
5078 | diagnoseRedundantReturnTypeQualifiers(S, T, D, chunkIndex); | ||||||||||||
5079 | |||||||||||||
5080 | // C++2a [dcl.fct]p12: | ||||||||||||
5081 | // A volatile-qualified return type is deprecated | ||||||||||||
5082 | if (T.isVolatileQualified() && S.getLangOpts().CPlusPlus20) | ||||||||||||
5083 | S.Diag(DeclType.Loc, diag::warn_deprecated_volatile_return) << T; | ||||||||||||
5084 | } | ||||||||||||
5085 | |||||||||||||
5086 | // Objective-C ARC ownership qualifiers are ignored on the function | ||||||||||||
5087 | // return type (by type canonicalization). Complain if this attribute | ||||||||||||
5088 | // was written here. | ||||||||||||
5089 | if (T.getQualifiers().hasObjCLifetime()) { | ||||||||||||
5090 | SourceLocation AttrLoc; | ||||||||||||
5091 | if (chunkIndex + 1 < D.getNumTypeObjects()) { | ||||||||||||
5092 | DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); | ||||||||||||
5093 | for (const ParsedAttr &AL : ReturnTypeChunk.getAttrs()) { | ||||||||||||
5094 | if (AL.getKind() == ParsedAttr::AT_ObjCOwnership) { | ||||||||||||
5095 | AttrLoc = AL.getLoc(); | ||||||||||||
5096 | break; | ||||||||||||
5097 | } | ||||||||||||
5098 | } | ||||||||||||
5099 | } | ||||||||||||
5100 | if (AttrLoc.isInvalid()) { | ||||||||||||
5101 | for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) { | ||||||||||||
5102 | if (AL.getKind() == ParsedAttr::AT_ObjCOwnership) { | ||||||||||||
5103 | AttrLoc = AL.getLoc(); | ||||||||||||
5104 | break; | ||||||||||||
5105 | } | ||||||||||||
5106 | } | ||||||||||||
5107 | } | ||||||||||||
5108 | |||||||||||||
5109 | if (AttrLoc.isValid()) { | ||||||||||||
5110 | // The ownership attributes are almost always written via | ||||||||||||
5111 | // the predefined | ||||||||||||
5112 | // __strong/__weak/__autoreleasing/__unsafe_unretained. | ||||||||||||
5113 | if (AttrLoc.isMacroID()) | ||||||||||||
5114 | AttrLoc = | ||||||||||||
5115 | S.SourceMgr.getImmediateExpansionRange(AttrLoc).getBegin(); | ||||||||||||
5116 | |||||||||||||
5117 | S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type) | ||||||||||||
5118 | << T.getQualifiers().getObjCLifetime(); | ||||||||||||
5119 | } | ||||||||||||
5120 | } | ||||||||||||
5121 | |||||||||||||
5122 | if (LangOpts.CPlusPlus && D.getDeclSpec().hasTagDefinition()) { | ||||||||||||
5123 | // C++ [dcl.fct]p6: | ||||||||||||
5124 | // Types shall not be defined in return or parameter types. | ||||||||||||
5125 | TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); | ||||||||||||
5126 | S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) | ||||||||||||
5127 | << Context.getTypeDeclType(Tag); | ||||||||||||
5128 | } | ||||||||||||
5129 | |||||||||||||
5130 | // Exception specs are not allowed in typedefs. Complain, but add it | ||||||||||||
5131 | // anyway. | ||||||||||||
5132 | if (IsTypedefName && FTI.getExceptionSpecType() && !LangOpts.CPlusPlus17) | ||||||||||||
5133 | S.Diag(FTI.getExceptionSpecLocBeg(), | ||||||||||||
5134 | diag::err_exception_spec_in_typedef) | ||||||||||||
5135 | << (D.getContext() == DeclaratorContext::AliasDecl || | ||||||||||||
5136 | D.getContext() == DeclaratorContext::AliasTemplate); | ||||||||||||
5137 | |||||||||||||
5138 | // If we see "T var();" or "T var(T());" at block scope, it is probably | ||||||||||||
5139 | // an attempt to initialize a variable, not a function declaration. | ||||||||||||
5140 | if (FTI.isAmbiguous) | ||||||||||||
5141 | warnAboutAmbiguousFunction(S, D, DeclType, T); | ||||||||||||
5142 | |||||||||||||
5143 | FunctionType::ExtInfo EI( | ||||||||||||
5144 | getCCForDeclaratorChunk(S, D, DeclType.getAttrs(), FTI, chunkIndex)); | ||||||||||||
5145 | |||||||||||||
5146 | if (!FTI.NumParams && !FTI.isVariadic && !LangOpts.CPlusPlus | ||||||||||||
5147 | && !LangOpts.OpenCL) { | ||||||||||||
5148 | // Simple void foo(), where the incoming T is the result type. | ||||||||||||
5149 | T = Context.getFunctionNoProtoType(T, EI); | ||||||||||||
5150 | } else { | ||||||||||||
5151 | // We allow a zero-parameter variadic function in C if the | ||||||||||||
5152 | // function is marked with the "overloadable" attribute. Scan | ||||||||||||
5153 | // for this attribute now. | ||||||||||||
5154 | if (!FTI.NumParams && FTI.isVariadic && !LangOpts.CPlusPlus) | ||||||||||||
5155 | if (!D.getAttributes().hasAttribute(ParsedAttr::AT_Overloadable)) | ||||||||||||
5156 | S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_param); | ||||||||||||
5157 | |||||||||||||
5158 | if (FTI.NumParams && FTI.Params[0].Param == nullptr) { | ||||||||||||
5159 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function | ||||||||||||
5160 | // definition. | ||||||||||||
5161 | S.Diag(FTI.Params[0].IdentLoc, | ||||||||||||
5162 | diag::err_ident_list_in_fn_declaration); | ||||||||||||
5163 | D.setInvalidType(true); | ||||||||||||
5164 | // Recover by creating a K&R-style function type. | ||||||||||||
5165 | T = Context.getFunctionNoProtoType(T, EI); | ||||||||||||
5166 | break; | ||||||||||||
5167 | } | ||||||||||||
5168 | |||||||||||||
5169 | FunctionProtoType::ExtProtoInfo EPI; | ||||||||||||
5170 | EPI.ExtInfo = EI; | ||||||||||||
5171 | EPI.Variadic = FTI.isVariadic; | ||||||||||||
5172 | EPI.EllipsisLoc = FTI.getEllipsisLoc(); | ||||||||||||
5173 | EPI.HasTrailingReturn = FTI.hasTrailingReturnType(); | ||||||||||||
5174 | EPI.TypeQuals.addCVRUQualifiers( | ||||||||||||
5175 | FTI.MethodQualifiers ? FTI.MethodQualifiers->getTypeQualifiers() | ||||||||||||
5176 | : 0); | ||||||||||||
5177 | EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None | ||||||||||||
5178 | : FTI.RefQualifierIsLValueRef? RQ_LValue | ||||||||||||
5179 | : RQ_RValue; | ||||||||||||
5180 | |||||||||||||
5181 | // Otherwise, we have a function with a parameter list that is | ||||||||||||
5182 | // potentially variadic. | ||||||||||||
5183 | SmallVector<QualType, 16> ParamTys; | ||||||||||||
5184 | ParamTys.reserve(FTI.NumParams); | ||||||||||||
5185 | |||||||||||||
5186 | SmallVector<FunctionProtoType::ExtParameterInfo, 16> | ||||||||||||
5187 | ExtParameterInfos(FTI.NumParams); | ||||||||||||
5188 | bool HasAnyInterestingExtParameterInfos = false; | ||||||||||||
5189 | |||||||||||||
5190 | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) { | ||||||||||||
5191 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); | ||||||||||||
5192 | QualType ParamTy = Param->getType(); | ||||||||||||
5193 | assert(!ParamTy.isNull() && "Couldn't parse type?")((!ParamTy.isNull() && "Couldn't parse type?") ? static_cast <void> (0) : __assert_fail ("!ParamTy.isNull() && \"Couldn't parse type?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5193, __PRETTY_FUNCTION__)); | ||||||||||||
5194 | |||||||||||||
5195 | // Look for 'void'. void is allowed only as a single parameter to a | ||||||||||||
5196 | // function with no other parameters (C99 6.7.5.3p10). We record | ||||||||||||
5197 | // int(void) as a FunctionProtoType with an empty parameter list. | ||||||||||||
5198 | if (ParamTy->isVoidType()) { | ||||||||||||
5199 | // If this is something like 'float(int, void)', reject it. 'void' | ||||||||||||
5200 | // is an incomplete type (C99 6.2.5p19) and function decls cannot | ||||||||||||
5201 | // have parameters of incomplete type. | ||||||||||||
5202 | if (FTI.NumParams != 1 || FTI.isVariadic) { | ||||||||||||
5203 | S.Diag(FTI.Params[i].IdentLoc, diag::err_void_only_param); | ||||||||||||
5204 | ParamTy = Context.IntTy; | ||||||||||||
5205 | Param->setType(ParamTy); | ||||||||||||
5206 | } else if (FTI.Params[i].Ident) { | ||||||||||||
5207 | // Reject, but continue to parse 'int(void abc)'. | ||||||||||||
5208 | S.Diag(FTI.Params[i].IdentLoc, diag::err_param_with_void_type); | ||||||||||||
5209 | ParamTy = Context.IntTy; | ||||||||||||
5210 | Param->setType(ParamTy); | ||||||||||||
5211 | } else { | ||||||||||||
5212 | // Reject, but continue to parse 'float(const void)'. | ||||||||||||
5213 | if (ParamTy.hasQualifiers()) | ||||||||||||
5214 | S.Diag(DeclType.Loc, diag::err_void_param_qualified); | ||||||||||||
5215 | |||||||||||||
5216 | // Do not add 'void' to the list. | ||||||||||||
5217 | break; | ||||||||||||
5218 | } | ||||||||||||
5219 | } else if (ParamTy->isHalfType()) { | ||||||||||||
5220 | // Disallow half FP parameters. | ||||||||||||
5221 | // FIXME: This really should be in BuildFunctionType. | ||||||||||||
5222 | if (S.getLangOpts().OpenCL) { | ||||||||||||
5223 | if (!S.getOpenCLOptions().isAvailableOption("cl_khr_fp16", | ||||||||||||
5224 | S.getLangOpts())) { | ||||||||||||
5225 | S.Diag(Param->getLocation(), diag::err_opencl_invalid_param) | ||||||||||||
5226 | << ParamTy << 0; | ||||||||||||
5227 | D.setInvalidType(); | ||||||||||||
5228 | Param->setInvalidDecl(); | ||||||||||||
5229 | } | ||||||||||||
5230 | } else if (!S.getLangOpts().HalfArgsAndReturns) { | ||||||||||||
5231 | S.Diag(Param->getLocation(), | ||||||||||||
5232 | diag::err_parameters_retval_cannot_have_fp16_type) << 0; | ||||||||||||
5233 | D.setInvalidType(); | ||||||||||||
5234 | } | ||||||||||||
5235 | } else if (!FTI.hasPrototype) { | ||||||||||||
5236 | if (ParamTy->isPromotableIntegerType()) { | ||||||||||||
5237 | ParamTy = Context.getPromotedIntegerType(ParamTy); | ||||||||||||
5238 | Param->setKNRPromoted(true); | ||||||||||||
5239 | } else if (const BuiltinType* BTy = ParamTy->getAs<BuiltinType>()) { | ||||||||||||
5240 | if (BTy->getKind() == BuiltinType::Float) { | ||||||||||||
5241 | ParamTy = Context.DoubleTy; | ||||||||||||
5242 | Param->setKNRPromoted(true); | ||||||||||||
5243 | } | ||||||||||||
5244 | } | ||||||||||||
5245 | } else if (S.getLangOpts().OpenCL && ParamTy->isBlockPointerType()) { | ||||||||||||
5246 | // OpenCL 2.0 s6.12.5: A block cannot be a parameter of a function. | ||||||||||||
5247 | S.Diag(Param->getLocation(), diag::err_opencl_invalid_param) | ||||||||||||
5248 | << ParamTy << 1 /*hint off*/; | ||||||||||||
5249 | D.setInvalidType(); | ||||||||||||
5250 | } | ||||||||||||
5251 | |||||||||||||
5252 | if (LangOpts.ObjCAutoRefCount && Param->hasAttr<NSConsumedAttr>()) { | ||||||||||||
5253 | ExtParameterInfos[i] = ExtParameterInfos[i].withIsConsumed(true); | ||||||||||||
5254 | HasAnyInterestingExtParameterInfos = true; | ||||||||||||
5255 | } | ||||||||||||
5256 | |||||||||||||
5257 | if (auto attr = Param->getAttr<ParameterABIAttr>()) { | ||||||||||||
5258 | ExtParameterInfos[i] = | ||||||||||||
5259 | ExtParameterInfos[i].withABI(attr->getABI()); | ||||||||||||
5260 | HasAnyInterestingExtParameterInfos = true; | ||||||||||||
5261 | } | ||||||||||||
5262 | |||||||||||||
5263 | if (Param->hasAttr<PassObjectSizeAttr>()) { | ||||||||||||
5264 | ExtParameterInfos[i] = ExtParameterInfos[i].withHasPassObjectSize(); | ||||||||||||
5265 | HasAnyInterestingExtParameterInfos = true; | ||||||||||||
5266 | } | ||||||||||||
5267 | |||||||||||||
5268 | if (Param->hasAttr<NoEscapeAttr>()) { | ||||||||||||
5269 | ExtParameterInfos[i] = ExtParameterInfos[i].withIsNoEscape(true); | ||||||||||||
5270 | HasAnyInterestingExtParameterInfos = true; | ||||||||||||
5271 | } | ||||||||||||
5272 | |||||||||||||
5273 | ParamTys.push_back(ParamTy); | ||||||||||||
5274 | } | ||||||||||||
5275 | |||||||||||||
5276 | if (HasAnyInterestingExtParameterInfos) { | ||||||||||||
5277 | EPI.ExtParameterInfos = ExtParameterInfos.data(); | ||||||||||||
5278 | checkExtParameterInfos(S, ParamTys, EPI, | ||||||||||||
5279 | [&](unsigned i) { return FTI.Params[i].Param->getLocation(); }); | ||||||||||||
5280 | } | ||||||||||||
5281 | |||||||||||||
5282 | SmallVector<QualType, 4> Exceptions; | ||||||||||||
5283 | SmallVector<ParsedType, 2> DynamicExceptions; | ||||||||||||
5284 | SmallVector<SourceRange, 2> DynamicExceptionRanges; | ||||||||||||
5285 | Expr *NoexceptExpr = nullptr; | ||||||||||||
5286 | |||||||||||||
5287 | if (FTI.getExceptionSpecType() == EST_Dynamic) { | ||||||||||||
5288 | // FIXME: It's rather inefficient to have to split into two vectors | ||||||||||||
5289 | // here. | ||||||||||||
5290 | unsigned N = FTI.getNumExceptions(); | ||||||||||||
5291 | DynamicExceptions.reserve(N); | ||||||||||||
5292 | DynamicExceptionRanges.reserve(N); | ||||||||||||
5293 | for (unsigned I = 0; I != N; ++I) { | ||||||||||||
5294 | DynamicExceptions.push_back(FTI.Exceptions[I].Ty); | ||||||||||||
5295 | DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range); | ||||||||||||
5296 | } | ||||||||||||
5297 | } else if (isComputedNoexcept(FTI.getExceptionSpecType())) { | ||||||||||||
5298 | NoexceptExpr = FTI.NoexceptExpr; | ||||||||||||
5299 | } | ||||||||||||
5300 | |||||||||||||
5301 | S.checkExceptionSpecification(D.isFunctionDeclarationContext(), | ||||||||||||
5302 | FTI.getExceptionSpecType(), | ||||||||||||
5303 | DynamicExceptions, | ||||||||||||
5304 | DynamicExceptionRanges, | ||||||||||||
5305 | NoexceptExpr, | ||||||||||||
5306 | Exceptions, | ||||||||||||
5307 | EPI.ExceptionSpec); | ||||||||||||
5308 | |||||||||||||
5309 | // FIXME: Set address space from attrs for C++ mode here. | ||||||||||||
5310 | // OpenCLCPlusPlus: A class member function has an address space. | ||||||||||||
5311 | auto IsClassMember = [&]() { | ||||||||||||
5312 | return (!state.getDeclarator().getCXXScopeSpec().isEmpty() && | ||||||||||||
5313 | state.getDeclarator() | ||||||||||||
5314 | .getCXXScopeSpec() | ||||||||||||
5315 | .getScopeRep() | ||||||||||||
5316 | ->getKind() == NestedNameSpecifier::TypeSpec) || | ||||||||||||
5317 | state.getDeclarator().getContext() == | ||||||||||||
5318 | DeclaratorContext::Member || | ||||||||||||
5319 | state.getDeclarator().getContext() == | ||||||||||||
5320 | DeclaratorContext::LambdaExpr; | ||||||||||||
5321 | }; | ||||||||||||
5322 | |||||||||||||
5323 | if (state.getSema().getLangOpts().OpenCLCPlusPlus && IsClassMember()) { | ||||||||||||
5324 | LangAS ASIdx = LangAS::Default; | ||||||||||||
5325 | // Take address space attr if any and mark as invalid to avoid adding | ||||||||||||
5326 | // them later while creating QualType. | ||||||||||||
5327 | if (FTI.MethodQualifiers) | ||||||||||||
5328 | for (ParsedAttr &attr : FTI.MethodQualifiers->getAttributes()) { | ||||||||||||
5329 | LangAS ASIdxNew = attr.asOpenCLLangAS(); | ||||||||||||
5330 | if (DiagnoseMultipleAddrSpaceAttributes(S, ASIdx, ASIdxNew, | ||||||||||||
5331 | attr.getLoc())) | ||||||||||||
5332 | D.setInvalidType(true); | ||||||||||||
5333 | else | ||||||||||||
5334 | ASIdx = ASIdxNew; | ||||||||||||
5335 | } | ||||||||||||
5336 | // If a class member function's address space is not set, set it to | ||||||||||||
5337 | // __generic. | ||||||||||||
5338 | LangAS AS = | ||||||||||||
5339 | (ASIdx == LangAS::Default ? S.getDefaultCXXMethodAddrSpace() | ||||||||||||
5340 | : ASIdx); | ||||||||||||
5341 | EPI.TypeQuals.addAddressSpace(AS); | ||||||||||||
5342 | } | ||||||||||||
5343 | T = Context.getFunctionType(T, ParamTys, EPI); | ||||||||||||
5344 | } | ||||||||||||
5345 | break; | ||||||||||||
5346 | } | ||||||||||||
5347 | case DeclaratorChunk::MemberPointer: { | ||||||||||||
5348 | // The scope spec must refer to a class, or be dependent. | ||||||||||||
5349 | CXXScopeSpec &SS = DeclType.Mem.Scope(); | ||||||||||||
5350 | QualType ClsType; | ||||||||||||
5351 | |||||||||||||
5352 | // Handle pointer nullability. | ||||||||||||
5353 | inferPointerNullability(SimplePointerKind::MemberPointer, DeclType.Loc, | ||||||||||||
5354 | DeclType.EndLoc, DeclType.getAttrs(), | ||||||||||||
5355 | state.getDeclarator().getAttributePool()); | ||||||||||||
5356 | |||||||||||||
5357 | if (SS.isInvalid()) { | ||||||||||||
5358 | // Avoid emitting extra errors if we already errored on the scope. | ||||||||||||
5359 | D.setInvalidType(true); | ||||||||||||
5360 | } else if (S.isDependentScopeSpecifier(SS) || | ||||||||||||
5361 | dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) { | ||||||||||||
5362 | NestedNameSpecifier *NNS = SS.getScopeRep(); | ||||||||||||
5363 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); | ||||||||||||
5364 | switch (NNS->getKind()) { | ||||||||||||
5365 | case NestedNameSpecifier::Identifier: | ||||||||||||
5366 | ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, | ||||||||||||
5367 | NNS->getAsIdentifier()); | ||||||||||||
5368 | break; | ||||||||||||
5369 | |||||||||||||
5370 | case NestedNameSpecifier::Namespace: | ||||||||||||
5371 | case NestedNameSpecifier::NamespaceAlias: | ||||||||||||
5372 | case NestedNameSpecifier::Global: | ||||||||||||
5373 | case NestedNameSpecifier::Super: | ||||||||||||
5374 | llvm_unreachable("Nested-name-specifier must name a type")::llvm::llvm_unreachable_internal("Nested-name-specifier must name a type" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5374); | ||||||||||||
5375 | |||||||||||||
5376 | case NestedNameSpecifier::TypeSpec: | ||||||||||||
5377 | case NestedNameSpecifier::TypeSpecWithTemplate: | ||||||||||||
5378 | ClsType = QualType(NNS->getAsType(), 0); | ||||||||||||
5379 | // Note: if the NNS has a prefix and ClsType is a nondependent | ||||||||||||
5380 | // TemplateSpecializationType, then the NNS prefix is NOT included | ||||||||||||
5381 | // in ClsType; hence we wrap ClsType into an ElaboratedType. | ||||||||||||
5382 | // NOTE: in particular, no wrap occurs if ClsType already is an | ||||||||||||
5383 | // Elaborated, DependentName, or DependentTemplateSpecialization. | ||||||||||||
5384 | if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType())) | ||||||||||||
5385 | ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType); | ||||||||||||
5386 | break; | ||||||||||||
5387 | } | ||||||||||||
5388 | } else { | ||||||||||||
5389 | S.Diag(DeclType.Mem.Scope().getBeginLoc(), | ||||||||||||
5390 | diag::err_illegal_decl_mempointer_in_nonclass) | ||||||||||||
5391 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") | ||||||||||||
5392 | << DeclType.Mem.Scope().getRange(); | ||||||||||||
5393 | D.setInvalidType(true); | ||||||||||||
5394 | } | ||||||||||||
5395 | |||||||||||||
5396 | if (!ClsType.isNull()) | ||||||||||||
5397 | T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, | ||||||||||||
5398 | D.getIdentifier()); | ||||||||||||
5399 | if (T.isNull()) { | ||||||||||||
5400 | T = Context.IntTy; | ||||||||||||
5401 | D.setInvalidType(true); | ||||||||||||
5402 | } else if (DeclType.Mem.TypeQuals) { | ||||||||||||
5403 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); | ||||||||||||
5404 | } | ||||||||||||
5405 | break; | ||||||||||||
5406 | } | ||||||||||||
5407 | |||||||||||||
5408 | case DeclaratorChunk::Pipe: { | ||||||||||||
5409 | T = S.BuildReadPipeType(T, DeclType.Loc); | ||||||||||||
5410 | processTypeAttrs(state, T, TAL_DeclSpec, | ||||||||||||
5411 | D.getMutableDeclSpec().getAttributes()); | ||||||||||||
5412 | break; | ||||||||||||
5413 | } | ||||||||||||
5414 | } | ||||||||||||
5415 | |||||||||||||
5416 | if (T.isNull()) { | ||||||||||||
5417 | D.setInvalidType(true); | ||||||||||||
5418 | T = Context.IntTy; | ||||||||||||
5419 | } | ||||||||||||
5420 | |||||||||||||
5421 | // See if there are any attributes on this declarator chunk. | ||||||||||||
5422 | processTypeAttrs(state, T, TAL_DeclChunk, DeclType.getAttrs()); | ||||||||||||
5423 | |||||||||||||
5424 | if (DeclType.Kind != DeclaratorChunk::Paren) { | ||||||||||||
5425 | if (ExpectNoDerefChunk && !IsNoDerefableChunk(DeclType)) | ||||||||||||
5426 | S.Diag(DeclType.Loc, diag::warn_noderef_on_non_pointer_or_array); | ||||||||||||
5427 | |||||||||||||
5428 | ExpectNoDerefChunk = state.didParseNoDeref(); | ||||||||||||
5429 | } | ||||||||||||
5430 | } | ||||||||||||
5431 | |||||||||||||
5432 | if (ExpectNoDerefChunk) | ||||||||||||
5433 | S.Diag(state.getDeclarator().getBeginLoc(), | ||||||||||||
5434 | diag::warn_noderef_on_non_pointer_or_array); | ||||||||||||
5435 | |||||||||||||
5436 | // GNU warning -Wstrict-prototypes | ||||||||||||
5437 | // Warn if a function declaration is without a prototype. | ||||||||||||
5438 | // This warning is issued for all kinds of unprototyped function | ||||||||||||
5439 | // declarations (i.e. function type typedef, function pointer etc.) | ||||||||||||
5440 | // C99 6.7.5.3p14: | ||||||||||||
5441 | // The empty list in a function declarator that is not part of a definition | ||||||||||||
5442 | // of that function specifies that no information about the number or types | ||||||||||||
5443 | // of the parameters is supplied. | ||||||||||||
5444 | if (!LangOpts.CPlusPlus && | ||||||||||||
5445 | D.getFunctionDefinitionKind() == FunctionDefinitionKind::Declaration) { | ||||||||||||
5446 | bool IsBlock = false; | ||||||||||||
5447 | for (const DeclaratorChunk &DeclType : D.type_objects()) { | ||||||||||||
5448 | switch (DeclType.Kind) { | ||||||||||||
5449 | case DeclaratorChunk::BlockPointer: | ||||||||||||
5450 | IsBlock = true; | ||||||||||||
5451 | break; | ||||||||||||
5452 | case DeclaratorChunk::Function: { | ||||||||||||
5453 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; | ||||||||||||
5454 | // We supress the warning when there's no LParen location, as this | ||||||||||||
5455 | // indicates the declaration was an implicit declaration, which gets | ||||||||||||
5456 | // warned about separately via -Wimplicit-function-declaration. | ||||||||||||
5457 | if (FTI.NumParams == 0 && !FTI.isVariadic && FTI.getLParenLoc().isValid()) | ||||||||||||
5458 | S.Diag(DeclType.Loc, diag::warn_strict_prototypes) | ||||||||||||
5459 | << IsBlock | ||||||||||||
5460 | << FixItHint::CreateInsertion(FTI.getRParenLoc(), "void"); | ||||||||||||
5461 | IsBlock = false; | ||||||||||||
5462 | break; | ||||||||||||
5463 | } | ||||||||||||
5464 | default: | ||||||||||||
5465 | break; | ||||||||||||
5466 | } | ||||||||||||
5467 | } | ||||||||||||
5468 | } | ||||||||||||
5469 | |||||||||||||
5470 | assert(!T.isNull() && "T must not be null after this point")((!T.isNull() && "T must not be null after this point" ) ? static_cast<void> (0) : __assert_fail ("!T.isNull() && \"T must not be null after this point\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5470, __PRETTY_FUNCTION__)); | ||||||||||||
5471 | |||||||||||||
5472 | if (LangOpts.CPlusPlus
| ||||||||||||
5473 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); | ||||||||||||
5474 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?")((FnTy && "Why oh why is there not a FunctionProtoType here?" ) ? static_cast<void> (0) : __assert_fail ("FnTy && \"Why oh why is there not a FunctionProtoType here?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5474, __PRETTY_FUNCTION__)); | ||||||||||||
5475 | |||||||||||||
5476 | // C++ 8.3.5p4: | ||||||||||||
5477 | // A cv-qualifier-seq shall only be part of the function type | ||||||||||||
5478 | // for a nonstatic member function, the function type to which a pointer | ||||||||||||
5479 | // to member refers, or the top-level function type of a function typedef | ||||||||||||
5480 | // declaration. | ||||||||||||
5481 | // | ||||||||||||
5482 | // Core issue 547 also allows cv-qualifiers on function types that are | ||||||||||||
5483 | // top-level template type arguments. | ||||||||||||
5484 | enum { NonMember, Member, DeductionGuide } Kind = NonMember; | ||||||||||||
5485 | if (D.getName().getKind() == UnqualifiedIdKind::IK_DeductionGuideName) | ||||||||||||
5486 | Kind = DeductionGuide; | ||||||||||||
5487 | else if (!D.getCXXScopeSpec().isSet()) { | ||||||||||||
5488 | if ((D.getContext() == DeclaratorContext::Member || | ||||||||||||
5489 | D.getContext() == DeclaratorContext::LambdaExpr) && | ||||||||||||
5490 | !D.getDeclSpec().isFriendSpecified()) | ||||||||||||
5491 | Kind = Member; | ||||||||||||
5492 | } else { | ||||||||||||
5493 | DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec()); | ||||||||||||
5494 | if (!DC || DC->isRecord()) | ||||||||||||
5495 | Kind = Member; | ||||||||||||
5496 | } | ||||||||||||
5497 | |||||||||||||
5498 | // C++11 [dcl.fct]p6 (w/DR1417): | ||||||||||||
5499 | // An attempt to specify a function type with a cv-qualifier-seq or a | ||||||||||||
5500 | // ref-qualifier (including by typedef-name) is ill-formed unless it is: | ||||||||||||
5501 | // - the function type for a non-static member function, | ||||||||||||
5502 | // - the function type to which a pointer to member refers, | ||||||||||||
5503 | // - the top-level function type of a function typedef declaration or | ||||||||||||
5504 | // alias-declaration, | ||||||||||||
5505 | // - the type-id in the default argument of a type-parameter, or | ||||||||||||
5506 | // - the type-id of a template-argument for a type-parameter | ||||||||||||
5507 | // | ||||||||||||
5508 | // FIXME: Checking this here is insufficient. We accept-invalid on: | ||||||||||||
5509 | // | ||||||||||||
5510 | // template<typename T> struct S { void f(T); }; | ||||||||||||
5511 | // S<int() const> s; | ||||||||||||
5512 | // | ||||||||||||
5513 | // ... for instance. | ||||||||||||
5514 | if (IsQualifiedFunction && | ||||||||||||
5515 | !(Kind == Member && | ||||||||||||
5516 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) && | ||||||||||||
5517 | !IsTypedefName && D.getContext() != DeclaratorContext::TemplateArg && | ||||||||||||
5518 | D.getContext() != DeclaratorContext::TemplateTypeArg) { | ||||||||||||
5519 | SourceLocation Loc = D.getBeginLoc(); | ||||||||||||
5520 | SourceRange RemovalRange; | ||||||||||||
5521 | unsigned I; | ||||||||||||
5522 | if (D.isFunctionDeclarator(I)) { | ||||||||||||
5523 | SmallVector<SourceLocation, 4> RemovalLocs; | ||||||||||||
5524 | const DeclaratorChunk &Chunk = D.getTypeObject(I); | ||||||||||||
5525 | assert(Chunk.Kind == DeclaratorChunk::Function)((Chunk.Kind == DeclaratorChunk::Function) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Function" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5525, __PRETTY_FUNCTION__)); | ||||||||||||
5526 | |||||||||||||
5527 | if (Chunk.Fun.hasRefQualifier()) | ||||||||||||
5528 | RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc()); | ||||||||||||
5529 | |||||||||||||
5530 | if (Chunk.Fun.hasMethodTypeQualifiers()) | ||||||||||||
5531 | Chunk.Fun.MethodQualifiers->forEachQualifier( | ||||||||||||
5532 | [&](DeclSpec::TQ TypeQual, StringRef QualName, | ||||||||||||
5533 | SourceLocation SL) { RemovalLocs.push_back(SL); }); | ||||||||||||
5534 | |||||||||||||
5535 | if (!RemovalLocs.empty()) { | ||||||||||||
5536 | llvm::sort(RemovalLocs, | ||||||||||||
5537 | BeforeThanCompare<SourceLocation>(S.getSourceManager())); | ||||||||||||
5538 | RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back()); | ||||||||||||
5539 | Loc = RemovalLocs.front(); | ||||||||||||
5540 | } | ||||||||||||
5541 | } | ||||||||||||
5542 | |||||||||||||
5543 | S.Diag(Loc, diag::err_invalid_qualified_function_type) | ||||||||||||
5544 | << Kind << D.isFunctionDeclarator() << T | ||||||||||||
5545 | << getFunctionQualifiersAsString(FnTy) | ||||||||||||
5546 | << FixItHint::CreateRemoval(RemovalRange); | ||||||||||||
5547 | |||||||||||||
5548 | // Strip the cv-qualifiers and ref-qualifiers from the type. | ||||||||||||
5549 | FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo(); | ||||||||||||
5550 | EPI.TypeQuals.removeCVRQualifiers(); | ||||||||||||
5551 | EPI.RefQualifier = RQ_None; | ||||||||||||
5552 | |||||||||||||
5553 | T = Context.getFunctionType(FnTy->getReturnType(), FnTy->getParamTypes(), | ||||||||||||
5554 | EPI); | ||||||||||||
5555 | // Rebuild any parens around the identifier in the function type. | ||||||||||||
5556 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
5557 | if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren) | ||||||||||||
5558 | break; | ||||||||||||
5559 | T = S.BuildParenType(T); | ||||||||||||
5560 | } | ||||||||||||
5561 | } | ||||||||||||
5562 | } | ||||||||||||
5563 | |||||||||||||
5564 | // Apply any undistributed attributes from the declarator. | ||||||||||||
5565 | processTypeAttrs(state, T, TAL_DeclName, D.getAttributes()); | ||||||||||||
5566 | |||||||||||||
5567 | // Diagnose any ignored type attributes. | ||||||||||||
5568 | state.diagnoseIgnoredTypeAttrs(T); | ||||||||||||
5569 | |||||||||||||
5570 | // C++0x [dcl.constexpr]p9: | ||||||||||||
5571 | // A constexpr specifier used in an object declaration declares the object | ||||||||||||
5572 | // as const. | ||||||||||||
5573 | if (D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr && | ||||||||||||
5574 | T->isObjectType()) | ||||||||||||
5575 | T.addConst(); | ||||||||||||
5576 | |||||||||||||
5577 | // C++2a [dcl.fct]p4: | ||||||||||||
5578 | // A parameter with volatile-qualified type is deprecated | ||||||||||||
5579 | if (T.isVolatileQualified() && S.getLangOpts().CPlusPlus20 && | ||||||||||||
5580 | (D.getContext() == DeclaratorContext::Prototype || | ||||||||||||
5581 | D.getContext() == DeclaratorContext::LambdaExprParameter)) | ||||||||||||
5582 | S.Diag(D.getIdentifierLoc(), diag::warn_deprecated_volatile_param) << T; | ||||||||||||
5583 | |||||||||||||
5584 | // If there was an ellipsis in the declarator, the declaration declares a | ||||||||||||
5585 | // parameter pack whose type may be a pack expansion type. | ||||||||||||
5586 | if (D.hasEllipsis()) { | ||||||||||||
5587 | // C++0x [dcl.fct]p13: | ||||||||||||
5588 | // A declarator-id or abstract-declarator containing an ellipsis shall | ||||||||||||
5589 | // only be used in a parameter-declaration. Such a parameter-declaration | ||||||||||||
5590 | // is a parameter pack (14.5.3). [...] | ||||||||||||
5591 | switch (D.getContext()) { | ||||||||||||
5592 | case DeclaratorContext::Prototype: | ||||||||||||
5593 | case DeclaratorContext::LambdaExprParameter: | ||||||||||||
5594 | case DeclaratorContext::RequiresExpr: | ||||||||||||
5595 | // C++0x [dcl.fct]p13: | ||||||||||||
5596 | // [...] When it is part of a parameter-declaration-clause, the | ||||||||||||
5597 | // parameter pack is a function parameter pack (14.5.3). The type T | ||||||||||||
5598 | // of the declarator-id of the function parameter pack shall contain | ||||||||||||
5599 | // a template parameter pack; each template parameter pack in T is | ||||||||||||
5600 | // expanded by the function parameter pack. | ||||||||||||
5601 | // | ||||||||||||
5602 | // We represent function parameter packs as function parameters whose | ||||||||||||
5603 | // type is a pack expansion. | ||||||||||||
5604 | if (!T->containsUnexpandedParameterPack() && | ||||||||||||
5605 | (!LangOpts.CPlusPlus20 || !T->getContainedAutoType())) { | ||||||||||||
5606 | S.Diag(D.getEllipsisLoc(), | ||||||||||||
5607 | diag::err_function_parameter_pack_without_parameter_packs) | ||||||||||||
5608 | << T << D.getSourceRange(); | ||||||||||||
5609 | D.setEllipsisLoc(SourceLocation()); | ||||||||||||
5610 | } else { | ||||||||||||
5611 | T = Context.getPackExpansionType(T, None, /*ExpectPackInType=*/false); | ||||||||||||
5612 | } | ||||||||||||
5613 | break; | ||||||||||||
5614 | case DeclaratorContext::TemplateParam: | ||||||||||||
5615 | // C++0x [temp.param]p15: | ||||||||||||
5616 | // If a template-parameter is a [...] is a parameter-declaration that | ||||||||||||
5617 | // declares a parameter pack (8.3.5), then the template-parameter is a | ||||||||||||
5618 | // template parameter pack (14.5.3). | ||||||||||||
5619 | // | ||||||||||||
5620 | // Note: core issue 778 clarifies that, if there are any unexpanded | ||||||||||||
5621 | // parameter packs in the type of the non-type template parameter, then | ||||||||||||
5622 | // it expands those parameter packs. | ||||||||||||
5623 | if (T->containsUnexpandedParameterPack()) | ||||||||||||
5624 | T = Context.getPackExpansionType(T, None); | ||||||||||||
5625 | else | ||||||||||||
5626 | S.Diag(D.getEllipsisLoc(), | ||||||||||||
5627 | LangOpts.CPlusPlus11 | ||||||||||||
5628 | ? diag::warn_cxx98_compat_variadic_templates | ||||||||||||
5629 | : diag::ext_variadic_templates); | ||||||||||||
5630 | break; | ||||||||||||
5631 | |||||||||||||
5632 | case DeclaratorContext::File: | ||||||||||||
5633 | case DeclaratorContext::KNRTypeList: | ||||||||||||
5634 | case DeclaratorContext::ObjCParameter: // FIXME: special diagnostic here? | ||||||||||||
5635 | case DeclaratorContext::ObjCResult: // FIXME: special diagnostic here? | ||||||||||||
5636 | case DeclaratorContext::TypeName: | ||||||||||||
5637 | case DeclaratorContext::FunctionalCast: | ||||||||||||
5638 | case DeclaratorContext::CXXNew: | ||||||||||||
5639 | case DeclaratorContext::AliasDecl: | ||||||||||||
5640 | case DeclaratorContext::AliasTemplate: | ||||||||||||
5641 | case DeclaratorContext::Member: | ||||||||||||
5642 | case DeclaratorContext::Block: | ||||||||||||
5643 | case DeclaratorContext::ForInit: | ||||||||||||
5644 | case DeclaratorContext::SelectionInit: | ||||||||||||
5645 | case DeclaratorContext::Condition: | ||||||||||||
5646 | case DeclaratorContext::CXXCatch: | ||||||||||||
5647 | case DeclaratorContext::ObjCCatch: | ||||||||||||
5648 | case DeclaratorContext::BlockLiteral: | ||||||||||||
5649 | case DeclaratorContext::LambdaExpr: | ||||||||||||
5650 | case DeclaratorContext::ConversionId: | ||||||||||||
5651 | case DeclaratorContext::TrailingReturn: | ||||||||||||
5652 | case DeclaratorContext::TrailingReturnVar: | ||||||||||||
5653 | case DeclaratorContext::TemplateArg: | ||||||||||||
5654 | case DeclaratorContext::TemplateTypeArg: | ||||||||||||
5655 | // FIXME: We may want to allow parameter packs in block-literal contexts | ||||||||||||
5656 | // in the future. | ||||||||||||
5657 | S.Diag(D.getEllipsisLoc(), | ||||||||||||
5658 | diag::err_ellipsis_in_declarator_not_parameter); | ||||||||||||
5659 | D.setEllipsisLoc(SourceLocation()); | ||||||||||||
5660 | break; | ||||||||||||
5661 | } | ||||||||||||
5662 | } | ||||||||||||
5663 | |||||||||||||
5664 | assert(!T.isNull() && "T must not be null at the end of this function")((!T.isNull() && "T must not be null at the end of this function" ) ? static_cast<void> (0) : __assert_fail ("!T.isNull() && \"T must not be null at the end of this function\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5664, __PRETTY_FUNCTION__)); | ||||||||||||
5665 | if (D.isInvalidType()) | ||||||||||||
5666 | return Context.getTrivialTypeSourceInfo(T); | ||||||||||||
5667 | |||||||||||||
5668 | return GetTypeSourceInfoForDeclarator(state, T, TInfo); | ||||||||||||
5669 | } | ||||||||||||
5670 | |||||||||||||
5671 | /// GetTypeForDeclarator - Convert the type for the specified | ||||||||||||
5672 | /// declarator to Type instances. | ||||||||||||
5673 | /// | ||||||||||||
5674 | /// The result of this call will never be null, but the associated | ||||||||||||
5675 | /// type may be a null type if there's an unrecoverable error. | ||||||||||||
5676 | TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { | ||||||||||||
5677 | // Determine the type of the declarator. Not all forms of declarator | ||||||||||||
5678 | // have a type. | ||||||||||||
5679 | |||||||||||||
5680 | TypeProcessingState state(*this, D); | ||||||||||||
5681 | |||||||||||||
5682 | TypeSourceInfo *ReturnTypeInfo = nullptr; | ||||||||||||
5683 | QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); | ||||||||||||
5684 | if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount) | ||||||||||||
5685 | inferARCWriteback(state, T); | ||||||||||||
5686 | |||||||||||||
5687 | return GetFullTypeForDeclarator(state, T, ReturnTypeInfo); | ||||||||||||
5688 | } | ||||||||||||
5689 | |||||||||||||
5690 | static void transferARCOwnershipToDeclSpec(Sema &S, | ||||||||||||
5691 | QualType &declSpecTy, | ||||||||||||
5692 | Qualifiers::ObjCLifetime ownership) { | ||||||||||||
5693 | if (declSpecTy->isObjCRetainableType() && | ||||||||||||
5694 | declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) { | ||||||||||||
5695 | Qualifiers qs; | ||||||||||||
5696 | qs.addObjCLifetime(ownership); | ||||||||||||
5697 | declSpecTy = S.Context.getQualifiedType(declSpecTy, qs); | ||||||||||||
5698 | } | ||||||||||||
5699 | } | ||||||||||||
5700 | |||||||||||||
5701 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, | ||||||||||||
5702 | Qualifiers::ObjCLifetime ownership, | ||||||||||||
5703 | unsigned chunkIndex) { | ||||||||||||
5704 | Sema &S = state.getSema(); | ||||||||||||
5705 | Declarator &D = state.getDeclarator(); | ||||||||||||
5706 | |||||||||||||
5707 | // Look for an explicit lifetime attribute. | ||||||||||||
5708 | DeclaratorChunk &chunk = D.getTypeObject(chunkIndex); | ||||||||||||
5709 | if (chunk.getAttrs().hasAttribute(ParsedAttr::AT_ObjCOwnership)) | ||||||||||||
5710 | return; | ||||||||||||
5711 | |||||||||||||
5712 | const char *attrStr = nullptr; | ||||||||||||
5713 | switch (ownership) { | ||||||||||||
5714 | case Qualifiers::OCL_None: llvm_unreachable("no ownership!")::llvm::llvm_unreachable_internal("no ownership!", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5714); | ||||||||||||
5715 | case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break; | ||||||||||||
5716 | case Qualifiers::OCL_Strong: attrStr = "strong"; break; | ||||||||||||
5717 | case Qualifiers::OCL_Weak: attrStr = "weak"; break; | ||||||||||||
5718 | case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break; | ||||||||||||
5719 | } | ||||||||||||
5720 | |||||||||||||
5721 | IdentifierLoc *Arg = new (S.Context) IdentifierLoc; | ||||||||||||
5722 | Arg->Ident = &S.Context.Idents.get(attrStr); | ||||||||||||
5723 | Arg->Loc = SourceLocation(); | ||||||||||||
5724 | |||||||||||||
5725 | ArgsUnion Args(Arg); | ||||||||||||
5726 | |||||||||||||
5727 | // If there wasn't one, add one (with an invalid source location | ||||||||||||
5728 | // so that we don't make an AttributedType for it). | ||||||||||||
5729 | ParsedAttr *attr = D.getAttributePool().create( | ||||||||||||
5730 | &S.Context.Idents.get("objc_ownership"), SourceLocation(), | ||||||||||||
5731 | /*scope*/ nullptr, SourceLocation(), | ||||||||||||
5732 | /*args*/ &Args, 1, ParsedAttr::AS_GNU); | ||||||||||||
5733 | chunk.getAttrs().addAtEnd(attr); | ||||||||||||
5734 | // TODO: mark whether we did this inference? | ||||||||||||
5735 | } | ||||||||||||
5736 | |||||||||||||
5737 | /// Used for transferring ownership in casts resulting in l-values. | ||||||||||||
5738 | static void transferARCOwnership(TypeProcessingState &state, | ||||||||||||
5739 | QualType &declSpecTy, | ||||||||||||
5740 | Qualifiers::ObjCLifetime ownership) { | ||||||||||||
5741 | Sema &S = state.getSema(); | ||||||||||||
5742 | Declarator &D = state.getDeclarator(); | ||||||||||||
5743 | |||||||||||||
5744 | int inner = -1; | ||||||||||||
5745 | bool hasIndirection = false; | ||||||||||||
5746 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
5747 | DeclaratorChunk &chunk = D.getTypeObject(i); | ||||||||||||
5748 | switch (chunk.Kind) { | ||||||||||||
5749 | case DeclaratorChunk::Paren: | ||||||||||||
5750 | // Ignore parens. | ||||||||||||
5751 | break; | ||||||||||||
5752 | |||||||||||||
5753 | case DeclaratorChunk::Array: | ||||||||||||
5754 | case DeclaratorChunk::Reference: | ||||||||||||
5755 | case DeclaratorChunk::Pointer: | ||||||||||||
5756 | if (inner != -1) | ||||||||||||
5757 | hasIndirection = true; | ||||||||||||
5758 | inner = i; | ||||||||||||
5759 | break; | ||||||||||||
5760 | |||||||||||||
5761 | case DeclaratorChunk::BlockPointer: | ||||||||||||
5762 | if (inner != -1) | ||||||||||||
5763 | transferARCOwnershipToDeclaratorChunk(state, ownership, i); | ||||||||||||
5764 | return; | ||||||||||||
5765 | |||||||||||||
5766 | case DeclaratorChunk::Function: | ||||||||||||
5767 | case DeclaratorChunk::MemberPointer: | ||||||||||||
5768 | case DeclaratorChunk::Pipe: | ||||||||||||
5769 | return; | ||||||||||||
5770 | } | ||||||||||||
5771 | } | ||||||||||||
5772 | |||||||||||||
5773 | if (inner == -1) | ||||||||||||
5774 | return; | ||||||||||||
5775 | |||||||||||||
5776 | DeclaratorChunk &chunk = D.getTypeObject(inner); | ||||||||||||
5777 | if (chunk.Kind == DeclaratorChunk::Pointer) { | ||||||||||||
5778 | if (declSpecTy->isObjCRetainableType()) | ||||||||||||
5779 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); | ||||||||||||
5780 | if (declSpecTy->isObjCObjectType() && hasIndirection) | ||||||||||||
5781 | return transferARCOwnershipToDeclaratorChunk(state, ownership, inner); | ||||||||||||
5782 | } else { | ||||||||||||
5783 | assert(chunk.Kind == DeclaratorChunk::Array ||((chunk.Kind == DeclaratorChunk::Array || chunk.Kind == DeclaratorChunk ::Reference) ? static_cast<void> (0) : __assert_fail ("chunk.Kind == DeclaratorChunk::Array || chunk.Kind == DeclaratorChunk::Reference" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5784, __PRETTY_FUNCTION__)) | ||||||||||||
5784 | chunk.Kind == DeclaratorChunk::Reference)((chunk.Kind == DeclaratorChunk::Array || chunk.Kind == DeclaratorChunk ::Reference) ? static_cast<void> (0) : __assert_fail ("chunk.Kind == DeclaratorChunk::Array || chunk.Kind == DeclaratorChunk::Reference" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5784, __PRETTY_FUNCTION__)); | ||||||||||||
5785 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); | ||||||||||||
5786 | } | ||||||||||||
5787 | } | ||||||||||||
5788 | |||||||||||||
5789 | TypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) { | ||||||||||||
5790 | TypeProcessingState state(*this, D); | ||||||||||||
5791 | |||||||||||||
5792 | TypeSourceInfo *ReturnTypeInfo = nullptr; | ||||||||||||
5793 | QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); | ||||||||||||
5794 | |||||||||||||
5795 | if (getLangOpts().ObjC) { | ||||||||||||
5796 | Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy); | ||||||||||||
5797 | if (ownership != Qualifiers::OCL_None) | ||||||||||||
5798 | transferARCOwnership(state, declSpecTy, ownership); | ||||||||||||
5799 | } | ||||||||||||
5800 | |||||||||||||
5801 | return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo); | ||||||||||||
5802 | } | ||||||||||||
5803 | |||||||||||||
5804 | static void fillAttributedTypeLoc(AttributedTypeLoc TL, | ||||||||||||
5805 | TypeProcessingState &State) { | ||||||||||||
5806 | TL.setAttr(State.takeAttrForAttributedType(TL.getTypePtr())); | ||||||||||||
5807 | } | ||||||||||||
5808 | |||||||||||||
5809 | namespace { | ||||||||||||
5810 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { | ||||||||||||
5811 | Sema &SemaRef; | ||||||||||||
5812 | ASTContext &Context; | ||||||||||||
5813 | TypeProcessingState &State; | ||||||||||||
5814 | const DeclSpec &DS; | ||||||||||||
5815 | |||||||||||||
5816 | public: | ||||||||||||
5817 | TypeSpecLocFiller(Sema &S, ASTContext &Context, TypeProcessingState &State, | ||||||||||||
5818 | const DeclSpec &DS) | ||||||||||||
5819 | : SemaRef(S), Context(Context), State(State), DS(DS) {} | ||||||||||||
5820 | |||||||||||||
5821 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { | ||||||||||||
5822 | Visit(TL.getModifiedLoc()); | ||||||||||||
5823 | fillAttributedTypeLoc(TL, State); | ||||||||||||
5824 | } | ||||||||||||
5825 | void VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { | ||||||||||||
5826 | Visit(TL.getInnerLoc()); | ||||||||||||
5827 | TL.setExpansionLoc( | ||||||||||||
5828 | State.getExpansionLocForMacroQualifiedType(TL.getTypePtr())); | ||||||||||||
5829 | } | ||||||||||||
5830 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { | ||||||||||||
5831 | Visit(TL.getUnqualifiedLoc()); | ||||||||||||
5832 | } | ||||||||||||
5833 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { | ||||||||||||
5834 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5835 | } | ||||||||||||
5836 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { | ||||||||||||
5837 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5838 | // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires | ||||||||||||
5839 | // addition field. What we have is good enough for dispay of location | ||||||||||||
5840 | // of 'fixit' on interface name. | ||||||||||||
5841 | TL.setNameEndLoc(DS.getEndLoc()); | ||||||||||||
5842 | } | ||||||||||||
5843 | void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { | ||||||||||||
5844 | TypeSourceInfo *RepTInfo = nullptr; | ||||||||||||
5845 | Sema::GetTypeFromParser(DS.getRepAsType(), &RepTInfo); | ||||||||||||
5846 | TL.copy(RepTInfo->getTypeLoc()); | ||||||||||||
5847 | } | ||||||||||||
5848 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { | ||||||||||||
5849 | TypeSourceInfo *RepTInfo = nullptr; | ||||||||||||
5850 | Sema::GetTypeFromParser(DS.getRepAsType(), &RepTInfo); | ||||||||||||
5851 | TL.copy(RepTInfo->getTypeLoc()); | ||||||||||||
| |||||||||||||
5852 | } | ||||||||||||
5853 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { | ||||||||||||
5854 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5855 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5856 | |||||||||||||
5857 | // If we got no declarator info from previous Sema routines, | ||||||||||||
5858 | // just fill with the typespec loc. | ||||||||||||
5859 | if (!TInfo) { | ||||||||||||
5860 | TL.initialize(Context, DS.getTypeSpecTypeNameLoc()); | ||||||||||||
5861 | return; | ||||||||||||
5862 | } | ||||||||||||
5863 | |||||||||||||
5864 | TypeLoc OldTL = TInfo->getTypeLoc(); | ||||||||||||
5865 | if (TInfo->getType()->getAs<ElaboratedType>()) { | ||||||||||||
5866 | ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>(); | ||||||||||||
5867 | TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc() | ||||||||||||
5868 | .castAs<TemplateSpecializationTypeLoc>(); | ||||||||||||
5869 | TL.copy(NamedTL); | ||||||||||||
5870 | } else { | ||||||||||||
5871 | TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>()); | ||||||||||||
5872 | assert(TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc())((TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc >().getRAngleLoc()) ? static_cast<void> (0) : __assert_fail ("TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5872, __PRETTY_FUNCTION__)); | ||||||||||||
5873 | } | ||||||||||||
5874 | |||||||||||||
5875 | } | ||||||||||||
5876 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { | ||||||||||||
5877 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr)((DS.getTypeSpecType() == DeclSpec::TST_typeofExpr) ? static_cast <void> (0) : __assert_fail ("DS.getTypeSpecType() == DeclSpec::TST_typeofExpr" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5877, __PRETTY_FUNCTION__)); | ||||||||||||
5878 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5879 | TL.setParensRange(DS.getTypeofParensRange()); | ||||||||||||
5880 | } | ||||||||||||
5881 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { | ||||||||||||
5882 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType)((DS.getTypeSpecType() == DeclSpec::TST_typeofType) ? static_cast <void> (0) : __assert_fail ("DS.getTypeSpecType() == DeclSpec::TST_typeofType" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5882, __PRETTY_FUNCTION__)); | ||||||||||||
5883 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5884 | TL.setParensRange(DS.getTypeofParensRange()); | ||||||||||||
5885 | assert(DS.getRepAsType())((DS.getRepAsType()) ? static_cast<void> (0) : __assert_fail ("DS.getRepAsType()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5885, __PRETTY_FUNCTION__)); | ||||||||||||
5886 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5887 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5888 | TL.setUnderlyingTInfo(TInfo); | ||||||||||||
5889 | } | ||||||||||||
5890 | void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { | ||||||||||||
5891 | // FIXME: This holds only because we only have one unary transform. | ||||||||||||
5892 | assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType)((DS.getTypeSpecType() == DeclSpec::TST_underlyingType) ? static_cast <void> (0) : __assert_fail ("DS.getTypeSpecType() == DeclSpec::TST_underlyingType" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5892, __PRETTY_FUNCTION__)); | ||||||||||||
5893 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5894 | TL.setParensRange(DS.getTypeofParensRange()); | ||||||||||||
5895 | assert(DS.getRepAsType())((DS.getRepAsType()) ? static_cast<void> (0) : __assert_fail ("DS.getRepAsType()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5895, __PRETTY_FUNCTION__)); | ||||||||||||
5896 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5897 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5898 | TL.setUnderlyingTInfo(TInfo); | ||||||||||||
5899 | } | ||||||||||||
5900 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { | ||||||||||||
5901 | // By default, use the source location of the type specifier. | ||||||||||||
5902 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5903 | if (TL.needsExtraLocalData()) { | ||||||||||||
5904 | // Set info for the written builtin specifiers. | ||||||||||||
5905 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); | ||||||||||||
5906 | // Try to have a meaningful source location. | ||||||||||||
5907 | if (TL.getWrittenSignSpec() != TypeSpecifierSign::Unspecified) | ||||||||||||
5908 | TL.expandBuiltinRange(DS.getTypeSpecSignLoc()); | ||||||||||||
5909 | if (TL.getWrittenWidthSpec() != TypeSpecifierWidth::Unspecified) | ||||||||||||
5910 | TL.expandBuiltinRange(DS.getTypeSpecWidthRange()); | ||||||||||||
5911 | } | ||||||||||||
5912 | } | ||||||||||||
5913 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { | ||||||||||||
5914 | ElaboratedTypeKeyword Keyword | ||||||||||||
5915 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); | ||||||||||||
5916 | if (DS.getTypeSpecType() == TST_typename) { | ||||||||||||
5917 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5918 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5919 | if (TInfo) { | ||||||||||||
5920 | TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>()); | ||||||||||||
5921 | return; | ||||||||||||
5922 | } | ||||||||||||
5923 | } | ||||||||||||
5924 | TL.setElaboratedKeywordLoc(Keyword != ETK_None | ||||||||||||
5925 | ? DS.getTypeSpecTypeLoc() | ||||||||||||
5926 | : SourceLocation()); | ||||||||||||
5927 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); | ||||||||||||
5928 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | ||||||||||||
5929 | Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); | ||||||||||||
5930 | } | ||||||||||||
5931 | void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { | ||||||||||||
5932 | assert(DS.getTypeSpecType() == TST_typename)((DS.getTypeSpecType() == TST_typename) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_typename" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5932, __PRETTY_FUNCTION__)); | ||||||||||||
5933 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5934 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5935 | assert(TInfo)((TInfo) ? static_cast<void> (0) : __assert_fail ("TInfo" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5935, __PRETTY_FUNCTION__)); | ||||||||||||
5936 | TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>()); | ||||||||||||
5937 | } | ||||||||||||
5938 | void VisitDependentTemplateSpecializationTypeLoc( | ||||||||||||
5939 | DependentTemplateSpecializationTypeLoc TL) { | ||||||||||||
5940 | assert(DS.getTypeSpecType() == TST_typename)((DS.getTypeSpecType() == TST_typename) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_typename" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5940, __PRETTY_FUNCTION__)); | ||||||||||||
5941 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5942 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5943 | assert(TInfo)((TInfo) ? static_cast<void> (0) : __assert_fail ("TInfo" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5943, __PRETTY_FUNCTION__)); | ||||||||||||
5944 | TL.copy( | ||||||||||||
5945 | TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>()); | ||||||||||||
5946 | } | ||||||||||||
5947 | void VisitAutoTypeLoc(AutoTypeLoc TL) { | ||||||||||||
5948 | assert(DS.getTypeSpecType() == TST_auto ||((DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5951, __PRETTY_FUNCTION__)) | ||||||||||||
5949 | DS.getTypeSpecType() == TST_decltype_auto ||((DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5951, __PRETTY_FUNCTION__)) | ||||||||||||
5950 | DS.getTypeSpecType() == TST_auto_type ||((DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5951, __PRETTY_FUNCTION__)) | ||||||||||||
5951 | DS.getTypeSpecType() == TST_unspecified)((DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified) ? static_cast<void > (0) : __assert_fail ("DS.getTypeSpecType() == TST_auto || DS.getTypeSpecType() == TST_decltype_auto || DS.getTypeSpecType() == TST_auto_type || DS.getTypeSpecType() == TST_unspecified" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5951, __PRETTY_FUNCTION__)); | ||||||||||||
5952 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5953 | if (!DS.isConstrainedAuto()) | ||||||||||||
5954 | return; | ||||||||||||
5955 | TemplateIdAnnotation *TemplateId = DS.getRepAsTemplateId(); | ||||||||||||
5956 | if (!TemplateId) | ||||||||||||
5957 | return; | ||||||||||||
5958 | if (DS.getTypeSpecScope().isNotEmpty()) | ||||||||||||
5959 | TL.setNestedNameSpecifierLoc( | ||||||||||||
5960 | DS.getTypeSpecScope().getWithLocInContext(Context)); | ||||||||||||
5961 | else | ||||||||||||
5962 | TL.setNestedNameSpecifierLoc(NestedNameSpecifierLoc()); | ||||||||||||
5963 | TL.setTemplateKWLoc(TemplateId->TemplateKWLoc); | ||||||||||||
5964 | TL.setConceptNameLoc(TemplateId->TemplateNameLoc); | ||||||||||||
5965 | TL.setFoundDecl(nullptr); | ||||||||||||
5966 | TL.setLAngleLoc(TemplateId->LAngleLoc); | ||||||||||||
5967 | TL.setRAngleLoc(TemplateId->RAngleLoc); | ||||||||||||
5968 | if (TemplateId->NumArgs == 0) | ||||||||||||
5969 | return; | ||||||||||||
5970 | TemplateArgumentListInfo TemplateArgsInfo; | ||||||||||||
5971 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), | ||||||||||||
5972 | TemplateId->NumArgs); | ||||||||||||
5973 | SemaRef.translateTemplateArguments(TemplateArgsPtr, TemplateArgsInfo); | ||||||||||||
5974 | for (unsigned I = 0; I < TemplateId->NumArgs; ++I) | ||||||||||||
5975 | TL.setArgLocInfo(I, TemplateArgsInfo.arguments()[I].getLocInfo()); | ||||||||||||
5976 | } | ||||||||||||
5977 | void VisitTagTypeLoc(TagTypeLoc TL) { | ||||||||||||
5978 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); | ||||||||||||
5979 | } | ||||||||||||
5980 | void VisitAtomicTypeLoc(AtomicTypeLoc TL) { | ||||||||||||
5981 | // An AtomicTypeLoc can come from either an _Atomic(...) type specifier | ||||||||||||
5982 | // or an _Atomic qualifier. | ||||||||||||
5983 | if (DS.getTypeSpecType() == DeclSpec::TST_atomic) { | ||||||||||||
5984 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
5985 | TL.setParensRange(DS.getTypeofParensRange()); | ||||||||||||
5986 | |||||||||||||
5987 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
5988 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
5989 | assert(TInfo)((TInfo) ? static_cast<void> (0) : __assert_fail ("TInfo" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 5989, __PRETTY_FUNCTION__)); | ||||||||||||
5990 | TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc()); | ||||||||||||
5991 | } else { | ||||||||||||
5992 | TL.setKWLoc(DS.getAtomicSpecLoc()); | ||||||||||||
5993 | // No parens, to indicate this was spelled as an _Atomic qualifier. | ||||||||||||
5994 | TL.setParensRange(SourceRange()); | ||||||||||||
5995 | Visit(TL.getValueLoc()); | ||||||||||||
5996 | } | ||||||||||||
5997 | } | ||||||||||||
5998 | |||||||||||||
5999 | void VisitPipeTypeLoc(PipeTypeLoc TL) { | ||||||||||||
6000 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
6001 | |||||||||||||
6002 | TypeSourceInfo *TInfo = nullptr; | ||||||||||||
6003 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); | ||||||||||||
6004 | TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc()); | ||||||||||||
6005 | } | ||||||||||||
6006 | |||||||||||||
6007 | void VisitExtIntTypeLoc(ExtIntTypeLoc TL) { | ||||||||||||
6008 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
6009 | } | ||||||||||||
6010 | |||||||||||||
6011 | void VisitDependentExtIntTypeLoc(DependentExtIntTypeLoc TL) { | ||||||||||||
6012 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); | ||||||||||||
6013 | } | ||||||||||||
6014 | |||||||||||||
6015 | void VisitTypeLoc(TypeLoc TL) { | ||||||||||||
6016 | // FIXME: add other typespec types and change this to an assert. | ||||||||||||
6017 | TL.initialize(Context, DS.getTypeSpecTypeLoc()); | ||||||||||||
6018 | } | ||||||||||||
6019 | }; | ||||||||||||
6020 | |||||||||||||
6021 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { | ||||||||||||
6022 | ASTContext &Context; | ||||||||||||
6023 | TypeProcessingState &State; | ||||||||||||
6024 | const DeclaratorChunk &Chunk; | ||||||||||||
6025 | |||||||||||||
6026 | public: | ||||||||||||
6027 | DeclaratorLocFiller(ASTContext &Context, TypeProcessingState &State, | ||||||||||||
6028 | const DeclaratorChunk &Chunk) | ||||||||||||
6029 | : Context(Context), State(State), Chunk(Chunk) {} | ||||||||||||
6030 | |||||||||||||
6031 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { | ||||||||||||
6032 | llvm_unreachable("qualified type locs not expected here!")::llvm::llvm_unreachable_internal("qualified type locs not expected here!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6032); | ||||||||||||
6033 | } | ||||||||||||
6034 | void VisitDecayedTypeLoc(DecayedTypeLoc TL) { | ||||||||||||
6035 | llvm_unreachable("decayed type locs not expected here!")::llvm::llvm_unreachable_internal("decayed type locs not expected here!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6035); | ||||||||||||
6036 | } | ||||||||||||
6037 | |||||||||||||
6038 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { | ||||||||||||
6039 | fillAttributedTypeLoc(TL, State); | ||||||||||||
6040 | } | ||||||||||||
6041 | void VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { | ||||||||||||
6042 | // nothing | ||||||||||||
6043 | } | ||||||||||||
6044 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { | ||||||||||||
6045 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer)((Chunk.Kind == DeclaratorChunk::BlockPointer) ? static_cast< void> (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::BlockPointer" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6045, __PRETTY_FUNCTION__)); | ||||||||||||
6046 | TL.setCaretLoc(Chunk.Loc); | ||||||||||||
6047 | } | ||||||||||||
6048 | void VisitPointerTypeLoc(PointerTypeLoc TL) { | ||||||||||||
6049 | assert(Chunk.Kind == DeclaratorChunk::Pointer)((Chunk.Kind == DeclaratorChunk::Pointer) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Pointer" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6049, __PRETTY_FUNCTION__)); | ||||||||||||
6050 | TL.setStarLoc(Chunk.Loc); | ||||||||||||
6051 | } | ||||||||||||
6052 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { | ||||||||||||
6053 | assert(Chunk.Kind == DeclaratorChunk::Pointer)((Chunk.Kind == DeclaratorChunk::Pointer) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Pointer" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6053, __PRETTY_FUNCTION__)); | ||||||||||||
6054 | TL.setStarLoc(Chunk.Loc); | ||||||||||||
6055 | } | ||||||||||||
6056 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { | ||||||||||||
6057 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer)((Chunk.Kind == DeclaratorChunk::MemberPointer) ? static_cast <void> (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::MemberPointer" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6057, __PRETTY_FUNCTION__)); | ||||||||||||
6058 | const CXXScopeSpec& SS = Chunk.Mem.Scope(); | ||||||||||||
6059 | NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context); | ||||||||||||
6060 | |||||||||||||
6061 | const Type* ClsTy = TL.getClass(); | ||||||||||||
6062 | QualType ClsQT = QualType(ClsTy, 0); | ||||||||||||
6063 | TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0); | ||||||||||||
6064 | // Now copy source location info into the type loc component. | ||||||||||||
6065 | TypeLoc ClsTL = ClsTInfo->getTypeLoc(); | ||||||||||||
6066 | switch (NNSLoc.getNestedNameSpecifier()->getKind()) { | ||||||||||||
6067 | case NestedNameSpecifier::Identifier: | ||||||||||||
6068 | assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc")((isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc" ) ? static_cast<void> (0) : __assert_fail ("isa<DependentNameType>(ClsTy) && \"Unexpected TypeLoc\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6068, __PRETTY_FUNCTION__)); | ||||||||||||
6069 | { | ||||||||||||
6070 | DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>(); | ||||||||||||
6071 | DNTLoc.setElaboratedKeywordLoc(SourceLocation()); | ||||||||||||
6072 | DNTLoc.setQualifierLoc(NNSLoc.getPrefix()); | ||||||||||||
6073 | DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc()); | ||||||||||||
6074 | } | ||||||||||||
6075 | break; | ||||||||||||
6076 | |||||||||||||
6077 | case NestedNameSpecifier::TypeSpec: | ||||||||||||
6078 | case NestedNameSpecifier::TypeSpecWithTemplate: | ||||||||||||
6079 | if (isa<ElaboratedType>(ClsTy)) { | ||||||||||||
6080 | ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>(); | ||||||||||||
6081 | ETLoc.setElaboratedKeywordLoc(SourceLocation()); | ||||||||||||
6082 | ETLoc.setQualifierLoc(NNSLoc.getPrefix()); | ||||||||||||
6083 | TypeLoc NamedTL = ETLoc.getNamedTypeLoc(); | ||||||||||||
6084 | NamedTL.initializeFullCopy(NNSLoc.getTypeLoc()); | ||||||||||||
6085 | } else { | ||||||||||||
6086 | ClsTL.initializeFullCopy(NNSLoc.getTypeLoc()); | ||||||||||||
6087 | } | ||||||||||||
6088 | break; | ||||||||||||
6089 | |||||||||||||
6090 | case NestedNameSpecifier::Namespace: | ||||||||||||
6091 | case NestedNameSpecifier::NamespaceAlias: | ||||||||||||
6092 | case NestedNameSpecifier::Global: | ||||||||||||
6093 | case NestedNameSpecifier::Super: | ||||||||||||
6094 | llvm_unreachable("Nested-name-specifier must name a type")::llvm::llvm_unreachable_internal("Nested-name-specifier must name a type" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6094); | ||||||||||||
6095 | } | ||||||||||||
6096 | |||||||||||||
6097 | // Finally fill in MemberPointerLocInfo fields. | ||||||||||||
6098 | TL.setStarLoc(Chunk.Mem.StarLoc); | ||||||||||||
6099 | TL.setClassTInfo(ClsTInfo); | ||||||||||||
6100 | } | ||||||||||||
6101 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { | ||||||||||||
6102 | assert(Chunk.Kind == DeclaratorChunk::Reference)((Chunk.Kind == DeclaratorChunk::Reference) ? static_cast< void> (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Reference" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6102, __PRETTY_FUNCTION__)); | ||||||||||||
6103 | // 'Amp' is misleading: this might have been originally | ||||||||||||
6104 | /// spelled with AmpAmp. | ||||||||||||
6105 | TL.setAmpLoc(Chunk.Loc); | ||||||||||||
6106 | } | ||||||||||||
6107 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { | ||||||||||||
6108 | assert(Chunk.Kind == DeclaratorChunk::Reference)((Chunk.Kind == DeclaratorChunk::Reference) ? static_cast< void> (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Reference" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6108, __PRETTY_FUNCTION__)); | ||||||||||||
6109 | assert(!Chunk.Ref.LValueRef)((!Chunk.Ref.LValueRef) ? static_cast<void> (0) : __assert_fail ("!Chunk.Ref.LValueRef", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6109, __PRETTY_FUNCTION__)); | ||||||||||||
6110 | TL.setAmpAmpLoc(Chunk.Loc); | ||||||||||||
6111 | } | ||||||||||||
6112 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { | ||||||||||||
6113 | assert(Chunk.Kind == DeclaratorChunk::Array)((Chunk.Kind == DeclaratorChunk::Array) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Array" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6113, __PRETTY_FUNCTION__)); | ||||||||||||
6114 | TL.setLBracketLoc(Chunk.Loc); | ||||||||||||
6115 | TL.setRBracketLoc(Chunk.EndLoc); | ||||||||||||
6116 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); | ||||||||||||
6117 | } | ||||||||||||
6118 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { | ||||||||||||
6119 | assert(Chunk.Kind == DeclaratorChunk::Function)((Chunk.Kind == DeclaratorChunk::Function) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Function" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6119, __PRETTY_FUNCTION__)); | ||||||||||||
6120 | TL.setLocalRangeBegin(Chunk.Loc); | ||||||||||||
6121 | TL.setLocalRangeEnd(Chunk.EndLoc); | ||||||||||||
6122 | |||||||||||||
6123 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; | ||||||||||||
6124 | TL.setLParenLoc(FTI.getLParenLoc()); | ||||||||||||
6125 | TL.setRParenLoc(FTI.getRParenLoc()); | ||||||||||||
6126 | for (unsigned i = 0, e = TL.getNumParams(), tpi = 0; i != e; ++i) { | ||||||||||||
6127 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); | ||||||||||||
6128 | TL.setParam(tpi++, Param); | ||||||||||||
6129 | } | ||||||||||||
6130 | TL.setExceptionSpecRange(FTI.getExceptionSpecRange()); | ||||||||||||
6131 | } | ||||||||||||
6132 | void VisitParenTypeLoc(ParenTypeLoc TL) { | ||||||||||||
6133 | assert(Chunk.Kind == DeclaratorChunk::Paren)((Chunk.Kind == DeclaratorChunk::Paren) ? static_cast<void > (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Paren" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6133, __PRETTY_FUNCTION__)); | ||||||||||||
6134 | TL.setLParenLoc(Chunk.Loc); | ||||||||||||
6135 | TL.setRParenLoc(Chunk.EndLoc); | ||||||||||||
6136 | } | ||||||||||||
6137 | void VisitPipeTypeLoc(PipeTypeLoc TL) { | ||||||||||||
6138 | assert(Chunk.Kind == DeclaratorChunk::Pipe)((Chunk.Kind == DeclaratorChunk::Pipe) ? static_cast<void> (0) : __assert_fail ("Chunk.Kind == DeclaratorChunk::Pipe", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6138, __PRETTY_FUNCTION__)); | ||||||||||||
6139 | TL.setKWLoc(Chunk.Loc); | ||||||||||||
6140 | } | ||||||||||||
6141 | void VisitExtIntTypeLoc(ExtIntTypeLoc TL) { | ||||||||||||
6142 | TL.setNameLoc(Chunk.Loc); | ||||||||||||
6143 | } | ||||||||||||
6144 | void VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { | ||||||||||||
6145 | TL.setExpansionLoc(Chunk.Loc); | ||||||||||||
6146 | } | ||||||||||||
6147 | void VisitVectorTypeLoc(VectorTypeLoc TL) { TL.setNameLoc(Chunk.Loc); } | ||||||||||||
6148 | void VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL) { | ||||||||||||
6149 | TL.setNameLoc(Chunk.Loc); | ||||||||||||
6150 | } | ||||||||||||
6151 | void VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { | ||||||||||||
6152 | TL.setNameLoc(Chunk.Loc); | ||||||||||||
6153 | } | ||||||||||||
6154 | void | ||||||||||||
6155 | VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL) { | ||||||||||||
6156 | TL.setNameLoc(Chunk.Loc); | ||||||||||||
6157 | } | ||||||||||||
6158 | |||||||||||||
6159 | void VisitTypeLoc(TypeLoc TL) { | ||||||||||||
6160 | llvm_unreachable("unsupported TypeLoc kind in declarator!")::llvm::llvm_unreachable_internal("unsupported TypeLoc kind in declarator!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6160); | ||||||||||||
6161 | } | ||||||||||||
6162 | }; | ||||||||||||
6163 | } // end anonymous namespace | ||||||||||||
6164 | |||||||||||||
6165 | static void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) { | ||||||||||||
6166 | SourceLocation Loc; | ||||||||||||
6167 | switch (Chunk.Kind) { | ||||||||||||
6168 | case DeclaratorChunk::Function: | ||||||||||||
6169 | case DeclaratorChunk::Array: | ||||||||||||
6170 | case DeclaratorChunk::Paren: | ||||||||||||
6171 | case DeclaratorChunk::Pipe: | ||||||||||||
6172 | llvm_unreachable("cannot be _Atomic qualified")::llvm::llvm_unreachable_internal("cannot be _Atomic qualified" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6172); | ||||||||||||
6173 | |||||||||||||
6174 | case DeclaratorChunk::Pointer: | ||||||||||||
6175 | Loc = Chunk.Ptr.AtomicQualLoc; | ||||||||||||
6176 | break; | ||||||||||||
6177 | |||||||||||||
6178 | case DeclaratorChunk::BlockPointer: | ||||||||||||
6179 | case DeclaratorChunk::Reference: | ||||||||||||
6180 | case DeclaratorChunk::MemberPointer: | ||||||||||||
6181 | // FIXME: Provide a source location for the _Atomic keyword. | ||||||||||||
6182 | break; | ||||||||||||
6183 | } | ||||||||||||
6184 | |||||||||||||
6185 | ATL.setKWLoc(Loc); | ||||||||||||
6186 | ATL.setParensRange(SourceRange()); | ||||||||||||
6187 | } | ||||||||||||
6188 | |||||||||||||
6189 | static void | ||||||||||||
6190 | fillDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc DASTL, | ||||||||||||
6191 | const ParsedAttributesView &Attrs) { | ||||||||||||
6192 | for (const ParsedAttr &AL : Attrs) { | ||||||||||||
6193 | if (AL.getKind() == ParsedAttr::AT_AddressSpace) { | ||||||||||||
6194 | DASTL.setAttrNameLoc(AL.getLoc()); | ||||||||||||
6195 | DASTL.setAttrExprOperand(AL.getArgAsExpr(0)); | ||||||||||||
6196 | DASTL.setAttrOperandParensRange(SourceRange()); | ||||||||||||
6197 | return; | ||||||||||||
6198 | } | ||||||||||||
6199 | } | ||||||||||||
6200 | |||||||||||||
6201 | llvm_unreachable(::llvm::llvm_unreachable_internal("no address_space attribute found at the expected location!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6202) | ||||||||||||
6202 | "no address_space attribute found at the expected location!")::llvm::llvm_unreachable_internal("no address_space attribute found at the expected location!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6202); | ||||||||||||
6203 | } | ||||||||||||
6204 | |||||||||||||
6205 | static void fillMatrixTypeLoc(MatrixTypeLoc MTL, | ||||||||||||
6206 | const ParsedAttributesView &Attrs) { | ||||||||||||
6207 | for (const ParsedAttr &AL : Attrs) { | ||||||||||||
6208 | if (AL.getKind() == ParsedAttr::AT_MatrixType) { | ||||||||||||
6209 | MTL.setAttrNameLoc(AL.getLoc()); | ||||||||||||
6210 | MTL.setAttrRowOperand(AL.getArgAsExpr(0)); | ||||||||||||
6211 | MTL.setAttrColumnOperand(AL.getArgAsExpr(1)); | ||||||||||||
6212 | MTL.setAttrOperandParensRange(SourceRange()); | ||||||||||||
6213 | return; | ||||||||||||
6214 | } | ||||||||||||
6215 | } | ||||||||||||
6216 | |||||||||||||
6217 | llvm_unreachable("no matrix_type attribute found at the expected location!")::llvm::llvm_unreachable_internal("no matrix_type attribute found at the expected location!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6217); | ||||||||||||
6218 | } | ||||||||||||
6219 | |||||||||||||
6220 | /// Create and instantiate a TypeSourceInfo with type source information. | ||||||||||||
6221 | /// | ||||||||||||
6222 | /// \param T QualType referring to the type as written in source code. | ||||||||||||
6223 | /// | ||||||||||||
6224 | /// \param ReturnTypeInfo For declarators whose return type does not show | ||||||||||||
6225 | /// up in the normal place in the declaration specifiers (such as a C++ | ||||||||||||
6226 | /// conversion function), this pointer will refer to a type source information | ||||||||||||
6227 | /// for that return type. | ||||||||||||
6228 | static TypeSourceInfo * | ||||||||||||
6229 | GetTypeSourceInfoForDeclarator(TypeProcessingState &State, | ||||||||||||
6230 | QualType T, TypeSourceInfo *ReturnTypeInfo) { | ||||||||||||
6231 | Sema &S = State.getSema(); | ||||||||||||
6232 | Declarator &D = State.getDeclarator(); | ||||||||||||
6233 | |||||||||||||
6234 | TypeSourceInfo *TInfo = S.Context.CreateTypeSourceInfo(T); | ||||||||||||
6235 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6236 | |||||||||||||
6237 | // Handle parameter packs whose type is a pack expansion. | ||||||||||||
6238 | if (isa<PackExpansionType>(T)) { | ||||||||||||
6239 | CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc()); | ||||||||||||
6240 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6241 | } | ||||||||||||
6242 | |||||||||||||
6243 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { | ||||||||||||
6244 | // An AtomicTypeLoc might be produced by an atomic qualifier in this | ||||||||||||
6245 | // declarator chunk. | ||||||||||||
6246 | if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) { | ||||||||||||
6247 | fillAtomicQualLoc(ATL, D.getTypeObject(i)); | ||||||||||||
6248 | CurrTL = ATL.getValueLoc().getUnqualifiedLoc(); | ||||||||||||
6249 | } | ||||||||||||
6250 | |||||||||||||
6251 | while (MacroQualifiedTypeLoc TL = CurrTL.getAs<MacroQualifiedTypeLoc>()) { | ||||||||||||
6252 | TL.setExpansionLoc( | ||||||||||||
6253 | State.getExpansionLocForMacroQualifiedType(TL.getTypePtr())); | ||||||||||||
6254 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6255 | } | ||||||||||||
6256 | |||||||||||||
6257 | while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) { | ||||||||||||
6258 | fillAttributedTypeLoc(TL, State); | ||||||||||||
6259 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6260 | } | ||||||||||||
6261 | |||||||||||||
6262 | while (DependentAddressSpaceTypeLoc TL = | ||||||||||||
6263 | CurrTL.getAs<DependentAddressSpaceTypeLoc>()) { | ||||||||||||
6264 | fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs()); | ||||||||||||
6265 | CurrTL = TL.getPointeeTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6266 | } | ||||||||||||
6267 | |||||||||||||
6268 | if (MatrixTypeLoc TL = CurrTL.getAs<MatrixTypeLoc>()) | ||||||||||||
6269 | fillMatrixTypeLoc(TL, D.getTypeObject(i).getAttrs()); | ||||||||||||
6270 | |||||||||||||
6271 | // FIXME: Ordering here? | ||||||||||||
6272 | while (AdjustedTypeLoc TL = CurrTL.getAs<AdjustedTypeLoc>()) | ||||||||||||
6273 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6274 | |||||||||||||
6275 | DeclaratorLocFiller(S.Context, State, D.getTypeObject(i)).Visit(CurrTL); | ||||||||||||
6276 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); | ||||||||||||
6277 | } | ||||||||||||
6278 | |||||||||||||
6279 | // If we have different source information for the return type, use | ||||||||||||
6280 | // that. This really only applies to C++ conversion functions. | ||||||||||||
6281 | if (ReturnTypeInfo) { | ||||||||||||
6282 | TypeLoc TL = ReturnTypeInfo->getTypeLoc(); | ||||||||||||
6283 | assert(TL.getFullDataSize() == CurrTL.getFullDataSize())((TL.getFullDataSize() == CurrTL.getFullDataSize()) ? static_cast <void> (0) : __assert_fail ("TL.getFullDataSize() == CurrTL.getFullDataSize()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6283, __PRETTY_FUNCTION__)); | ||||||||||||
6284 | memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); | ||||||||||||
6285 | } else { | ||||||||||||
6286 | TypeSpecLocFiller(S, S.Context, State, D.getDeclSpec()).Visit(CurrTL); | ||||||||||||
6287 | } | ||||||||||||
6288 | |||||||||||||
6289 | return TInfo; | ||||||||||||
6290 | } | ||||||||||||
6291 | |||||||||||||
6292 | /// Create a LocInfoType to hold the given QualType and TypeSourceInfo. | ||||||||||||
6293 | ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) { | ||||||||||||
6294 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser | ||||||||||||
6295 | // and Sema during declaration parsing. Try deallocating/caching them when | ||||||||||||
6296 | // it's appropriate, instead of allocating them and keeping them around. | ||||||||||||
6297 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), | ||||||||||||
6298 | TypeAlignment); | ||||||||||||
6299 | new (LocT) LocInfoType(T, TInfo); | ||||||||||||
6300 | assert(LocT->getTypeClass() != T->getTypeClass() &&((LocT->getTypeClass() != T->getTypeClass() && "LocInfoType's TypeClass conflicts with an existing Type class" ) ? static_cast<void> (0) : __assert_fail ("LocT->getTypeClass() != T->getTypeClass() && \"LocInfoType's TypeClass conflicts with an existing Type class\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6301, __PRETTY_FUNCTION__)) | ||||||||||||
6301 | "LocInfoType's TypeClass conflicts with an existing Type class")((LocT->getTypeClass() != T->getTypeClass() && "LocInfoType's TypeClass conflicts with an existing Type class" ) ? static_cast<void> (0) : __assert_fail ("LocT->getTypeClass() != T->getTypeClass() && \"LocInfoType's TypeClass conflicts with an existing Type class\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6301, __PRETTY_FUNCTION__)); | ||||||||||||
6302 | return ParsedType::make(QualType(LocT, 0)); | ||||||||||||
6303 | } | ||||||||||||
6304 | |||||||||||||
6305 | void LocInfoType::getAsStringInternal(std::string &Str, | ||||||||||||
6306 | const PrintingPolicy &Policy) const { | ||||||||||||
6307 | llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"::llvm::llvm_unreachable_internal("LocInfoType leaked into the type system; an opaque TypeTy*" " was used directly instead of getting the QualType through" " GetTypeFromParser", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6309) | ||||||||||||
6308 | " was used directly instead of getting the QualType through"::llvm::llvm_unreachable_internal("LocInfoType leaked into the type system; an opaque TypeTy*" " was used directly instead of getting the QualType through" " GetTypeFromParser", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6309) | ||||||||||||
6309 | " GetTypeFromParser")::llvm::llvm_unreachable_internal("LocInfoType leaked into the type system; an opaque TypeTy*" " was used directly instead of getting the QualType through" " GetTypeFromParser", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6309); | ||||||||||||
6310 | } | ||||||||||||
6311 | |||||||||||||
6312 | TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { | ||||||||||||
6313 | // C99 6.7.6: Type names have no identifier. This is already validated by | ||||||||||||
6314 | // the parser. | ||||||||||||
6315 | assert(D.getIdentifier() == nullptr &&((D.getIdentifier() == nullptr && "Type name should have no identifier!" ) ? static_cast<void> (0) : __assert_fail ("D.getIdentifier() == nullptr && \"Type name should have no identifier!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6316, __PRETTY_FUNCTION__)) | ||||||||||||
6316 | "Type name should have no identifier!")((D.getIdentifier() == nullptr && "Type name should have no identifier!" ) ? static_cast<void> (0) : __assert_fail ("D.getIdentifier() == nullptr && \"Type name should have no identifier!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6316, __PRETTY_FUNCTION__)); | ||||||||||||
6317 | |||||||||||||
6318 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); | ||||||||||||
6319 | QualType T = TInfo->getType(); | ||||||||||||
6320 | if (D.isInvalidType()) | ||||||||||||
6321 | return true; | ||||||||||||
6322 | |||||||||||||
6323 | // Make sure there are no unused decl attributes on the declarator. | ||||||||||||
6324 | // We don't want to do this for ObjC parameters because we're going | ||||||||||||
6325 | // to apply them to the actual parameter declaration. | ||||||||||||
6326 | // Likewise, we don't want to do this for alias declarations, because | ||||||||||||
6327 | // we are actually going to build a declaration from this eventually. | ||||||||||||
6328 | if (D.getContext() != DeclaratorContext::ObjCParameter && | ||||||||||||
6329 | D.getContext() != DeclaratorContext::AliasDecl && | ||||||||||||
6330 | D.getContext() != DeclaratorContext::AliasTemplate) | ||||||||||||
6331 | checkUnusedDeclAttributes(D); | ||||||||||||
6332 | |||||||||||||
6333 | if (getLangOpts().CPlusPlus) { | ||||||||||||
6334 | // Check that there are no default arguments (C++ only). | ||||||||||||
6335 | CheckExtraCXXDefaultArguments(D); | ||||||||||||
6336 | } | ||||||||||||
6337 | |||||||||||||
6338 | return CreateParsedType(T, TInfo); | ||||||||||||
6339 | } | ||||||||||||
6340 | |||||||||||||
6341 | ParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) { | ||||||||||||
6342 | QualType T = Context.getObjCInstanceType(); | ||||||||||||
6343 | TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc); | ||||||||||||
6344 | return CreateParsedType(T, TInfo); | ||||||||||||
6345 | } | ||||||||||||
6346 | |||||||||||||
6347 | //===----------------------------------------------------------------------===// | ||||||||||||
6348 | // Type Attribute Processing | ||||||||||||
6349 | //===----------------------------------------------------------------------===// | ||||||||||||
6350 | |||||||||||||
6351 | /// Build an AddressSpace index from a constant expression and diagnose any | ||||||||||||
6352 | /// errors related to invalid address_spaces. Returns true on successfully | ||||||||||||
6353 | /// building an AddressSpace index. | ||||||||||||
6354 | static bool BuildAddressSpaceIndex(Sema &S, LangAS &ASIdx, | ||||||||||||
6355 | const Expr *AddrSpace, | ||||||||||||
6356 | SourceLocation AttrLoc) { | ||||||||||||
6357 | if (!AddrSpace->isValueDependent()) { | ||||||||||||
6358 | Optional<llvm::APSInt> OptAddrSpace = | ||||||||||||
6359 | AddrSpace->getIntegerConstantExpr(S.Context); | ||||||||||||
6360 | if (!OptAddrSpace) { | ||||||||||||
6361 | S.Diag(AttrLoc, diag::err_attribute_argument_type) | ||||||||||||
6362 | << "'address_space'" << AANT_ArgumentIntegerConstant | ||||||||||||
6363 | << AddrSpace->getSourceRange(); | ||||||||||||
6364 | return false; | ||||||||||||
6365 | } | ||||||||||||
6366 | llvm::APSInt &addrSpace = *OptAddrSpace; | ||||||||||||
6367 | |||||||||||||
6368 | // Bounds checking. | ||||||||||||
6369 | if (addrSpace.isSigned()) { | ||||||||||||
6370 | if (addrSpace.isNegative()) { | ||||||||||||
6371 | S.Diag(AttrLoc, diag::err_attribute_address_space_negative) | ||||||||||||
6372 | << AddrSpace->getSourceRange(); | ||||||||||||
6373 | return false; | ||||||||||||
6374 | } | ||||||||||||
6375 | addrSpace.setIsSigned(false); | ||||||||||||
6376 | } | ||||||||||||
6377 | |||||||||||||
6378 | llvm::APSInt max(addrSpace.getBitWidth()); | ||||||||||||
6379 | max = | ||||||||||||
6380 | Qualifiers::MaxAddressSpace - (unsigned)LangAS::FirstTargetAddressSpace; | ||||||||||||
6381 | if (addrSpace > max) { | ||||||||||||
6382 | S.Diag(AttrLoc, diag::err_attribute_address_space_too_high) | ||||||||||||
6383 | << (unsigned)max.getZExtValue() << AddrSpace->getSourceRange(); | ||||||||||||
6384 | return false; | ||||||||||||
6385 | } | ||||||||||||
6386 | |||||||||||||
6387 | ASIdx = | ||||||||||||
6388 | getLangASFromTargetAS(static_cast<unsigned>(addrSpace.getZExtValue())); | ||||||||||||
6389 | return true; | ||||||||||||
6390 | } | ||||||||||||
6391 | |||||||||||||
6392 | // Default value for DependentAddressSpaceTypes | ||||||||||||
6393 | ASIdx = LangAS::Default; | ||||||||||||
6394 | return true; | ||||||||||||
6395 | } | ||||||||||||
6396 | |||||||||||||
6397 | /// BuildAddressSpaceAttr - Builds a DependentAddressSpaceType if an expression | ||||||||||||
6398 | /// is uninstantiated. If instantiated it will apply the appropriate address | ||||||||||||
6399 | /// space to the type. This function allows dependent template variables to be | ||||||||||||
6400 | /// used in conjunction with the address_space attribute | ||||||||||||
6401 | QualType Sema::BuildAddressSpaceAttr(QualType &T, LangAS ASIdx, Expr *AddrSpace, | ||||||||||||
6402 | SourceLocation AttrLoc) { | ||||||||||||
6403 | if (!AddrSpace->isValueDependent()) { | ||||||||||||
6404 | if (DiagnoseMultipleAddrSpaceAttributes(*this, T.getAddressSpace(), ASIdx, | ||||||||||||
6405 | AttrLoc)) | ||||||||||||
6406 | return QualType(); | ||||||||||||
6407 | |||||||||||||
6408 | return Context.getAddrSpaceQualType(T, ASIdx); | ||||||||||||
6409 | } | ||||||||||||
6410 | |||||||||||||
6411 | // A check with similar intentions as checking if a type already has an | ||||||||||||
6412 | // address space except for on a dependent types, basically if the | ||||||||||||
6413 | // current type is already a DependentAddressSpaceType then its already | ||||||||||||
6414 | // lined up to have another address space on it and we can't have | ||||||||||||
6415 | // multiple address spaces on the one pointer indirection | ||||||||||||
6416 | if (T->getAs<DependentAddressSpaceType>()) { | ||||||||||||
6417 | Diag(AttrLoc, diag::err_attribute_address_multiple_qualifiers); | ||||||||||||
6418 | return QualType(); | ||||||||||||
6419 | } | ||||||||||||
6420 | |||||||||||||
6421 | return Context.getDependentAddressSpaceType(T, AddrSpace, AttrLoc); | ||||||||||||
6422 | } | ||||||||||||
6423 | |||||||||||||
6424 | QualType Sema::BuildAddressSpaceAttr(QualType &T, Expr *AddrSpace, | ||||||||||||
6425 | SourceLocation AttrLoc) { | ||||||||||||
6426 | LangAS ASIdx; | ||||||||||||
6427 | if (!BuildAddressSpaceIndex(*this, ASIdx, AddrSpace, AttrLoc)) | ||||||||||||
6428 | return QualType(); | ||||||||||||
6429 | return BuildAddressSpaceAttr(T, ASIdx, AddrSpace, AttrLoc); | ||||||||||||
6430 | } | ||||||||||||
6431 | |||||||||||||
6432 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the | ||||||||||||
6433 | /// specified type. The attribute contains 1 argument, the id of the address | ||||||||||||
6434 | /// space for the type. | ||||||||||||
6435 | static void HandleAddressSpaceTypeAttribute(QualType &Type, | ||||||||||||
6436 | const ParsedAttr &Attr, | ||||||||||||
6437 | TypeProcessingState &State) { | ||||||||||||
6438 | Sema &S = State.getSema(); | ||||||||||||
6439 | |||||||||||||
6440 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be | ||||||||||||
6441 | // qualified by an address-space qualifier." | ||||||||||||
6442 | if (Type->isFunctionType()) { | ||||||||||||
6443 | S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type); | ||||||||||||
6444 | Attr.setInvalid(); | ||||||||||||
6445 | return; | ||||||||||||
6446 | } | ||||||||||||
6447 | |||||||||||||
6448 | LangAS ASIdx; | ||||||||||||
6449 | if (Attr.getKind() == ParsedAttr::AT_AddressSpace) { | ||||||||||||
6450 | |||||||||||||
6451 | // Check the attribute arguments. | ||||||||||||
6452 | if (Attr.getNumArgs() != 1) { | ||||||||||||
6453 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Attr | ||||||||||||
6454 | << 1; | ||||||||||||
6455 | Attr.setInvalid(); | ||||||||||||
6456 | return; | ||||||||||||
6457 | } | ||||||||||||
6458 | |||||||||||||
6459 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); | ||||||||||||
6460 | LangAS ASIdx; | ||||||||||||
6461 | if (!BuildAddressSpaceIndex(S, ASIdx, ASArgExpr, Attr.getLoc())) { | ||||||||||||
6462 | Attr.setInvalid(); | ||||||||||||
6463 | return; | ||||||||||||
6464 | } | ||||||||||||
6465 | |||||||||||||
6466 | ASTContext &Ctx = S.Context; | ||||||||||||
6467 | auto *ASAttr = | ||||||||||||
6468 | ::new (Ctx) AddressSpaceAttr(Ctx, Attr, static_cast<unsigned>(ASIdx)); | ||||||||||||
6469 | |||||||||||||
6470 | // If the expression is not value dependent (not templated), then we can | ||||||||||||
6471 | // apply the address space qualifiers just to the equivalent type. | ||||||||||||
6472 | // Otherwise, we make an AttributedType with the modified and equivalent | ||||||||||||
6473 | // type the same, and wrap it in a DependentAddressSpaceType. When this | ||||||||||||
6474 | // dependent type is resolved, the qualifier is added to the equivalent type | ||||||||||||
6475 | // later. | ||||||||||||
6476 | QualType T; | ||||||||||||
6477 | if (!ASArgExpr->isValueDependent()) { | ||||||||||||
6478 | QualType EquivType = | ||||||||||||
6479 | S.BuildAddressSpaceAttr(Type, ASIdx, ASArgExpr, Attr.getLoc()); | ||||||||||||
6480 | if (EquivType.isNull()) { | ||||||||||||
6481 | Attr.setInvalid(); | ||||||||||||
6482 | return; | ||||||||||||
6483 | } | ||||||||||||
6484 | T = State.getAttributedType(ASAttr, Type, EquivType); | ||||||||||||
6485 | } else { | ||||||||||||
6486 | T = State.getAttributedType(ASAttr, Type, Type); | ||||||||||||
6487 | T = S.BuildAddressSpaceAttr(T, ASIdx, ASArgExpr, Attr.getLoc()); | ||||||||||||
6488 | } | ||||||||||||
6489 | |||||||||||||
6490 | if (!T.isNull()) | ||||||||||||
6491 | Type = T; | ||||||||||||
6492 | else | ||||||||||||
6493 | Attr.setInvalid(); | ||||||||||||
6494 | } else { | ||||||||||||
6495 | // The keyword-based type attributes imply which address space to use. | ||||||||||||
6496 | ASIdx = Attr.asOpenCLLangAS(); | ||||||||||||
6497 | if (ASIdx == LangAS::Default) | ||||||||||||
6498 | llvm_unreachable("Invalid address space")::llvm::llvm_unreachable_internal("Invalid address space", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6498); | ||||||||||||
6499 | |||||||||||||
6500 | if (DiagnoseMultipleAddrSpaceAttributes(S, Type.getAddressSpace(), ASIdx, | ||||||||||||
6501 | Attr.getLoc())) { | ||||||||||||
6502 | Attr.setInvalid(); | ||||||||||||
6503 | return; | ||||||||||||
6504 | } | ||||||||||||
6505 | |||||||||||||
6506 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); | ||||||||||||
6507 | } | ||||||||||||
6508 | } | ||||||||||||
6509 | |||||||||||||
6510 | /// handleObjCOwnershipTypeAttr - Process an objc_ownership | ||||||||||||
6511 | /// attribute on the specified type. | ||||||||||||
6512 | /// | ||||||||||||
6513 | /// Returns 'true' if the attribute was handled. | ||||||||||||
6514 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, | ||||||||||||
6515 | ParsedAttr &attr, QualType &type) { | ||||||||||||
6516 | bool NonObjCPointer = false; | ||||||||||||
6517 | |||||||||||||
6518 | if (!type->isDependentType() && !type->isUndeducedType()) { | ||||||||||||
6519 | if (const PointerType *ptr = type->getAs<PointerType>()) { | ||||||||||||
6520 | QualType pointee = ptr->getPointeeType(); | ||||||||||||
6521 | if (pointee->isObjCRetainableType() || pointee->isPointerType()) | ||||||||||||
6522 | return false; | ||||||||||||
6523 | // It is important not to lose the source info that there was an attribute | ||||||||||||
6524 | // applied to non-objc pointer. We will create an attributed type but | ||||||||||||
6525 | // its type will be the same as the original type. | ||||||||||||
6526 | NonObjCPointer = true; | ||||||||||||
6527 | } else if (!type->isObjCRetainableType()) { | ||||||||||||
6528 | return false; | ||||||||||||
6529 | } | ||||||||||||
6530 | |||||||||||||
6531 | // Don't accept an ownership attribute in the declspec if it would | ||||||||||||
6532 | // just be the return type of a block pointer. | ||||||||||||
6533 | if (state.isProcessingDeclSpec()) { | ||||||||||||
6534 | Declarator &D = state.getDeclarator(); | ||||||||||||
6535 | if (maybeMovePastReturnType(D, D.getNumTypeObjects(), | ||||||||||||
6536 | /*onlyBlockPointers=*/true)) | ||||||||||||
6537 | return false; | ||||||||||||
6538 | } | ||||||||||||
6539 | } | ||||||||||||
6540 | |||||||||||||
6541 | Sema &S = state.getSema(); | ||||||||||||
6542 | SourceLocation AttrLoc = attr.getLoc(); | ||||||||||||
6543 | if (AttrLoc.isMacroID()) | ||||||||||||
6544 | AttrLoc = | ||||||||||||
6545 | S.getSourceManager().getImmediateExpansionRange(AttrLoc).getBegin(); | ||||||||||||
6546 | |||||||||||||
6547 | if (!attr.isArgIdent(0)) { | ||||||||||||
6548 | S.Diag(AttrLoc, diag::err_attribute_argument_type) << attr | ||||||||||||
6549 | << AANT_ArgumentString; | ||||||||||||
6550 | attr.setInvalid(); | ||||||||||||
6551 | return true; | ||||||||||||
6552 | } | ||||||||||||
6553 | |||||||||||||
6554 | IdentifierInfo *II = attr.getArgAsIdent(0)->Ident; | ||||||||||||
6555 | Qualifiers::ObjCLifetime lifetime; | ||||||||||||
6556 | if (II->isStr("none")) | ||||||||||||
6557 | lifetime = Qualifiers::OCL_ExplicitNone; | ||||||||||||
6558 | else if (II->isStr("strong")) | ||||||||||||
6559 | lifetime = Qualifiers::OCL_Strong; | ||||||||||||
6560 | else if (II->isStr("weak")) | ||||||||||||
6561 | lifetime = Qualifiers::OCL_Weak; | ||||||||||||
6562 | else if (II->isStr("autoreleasing")) | ||||||||||||
6563 | lifetime = Qualifiers::OCL_Autoreleasing; | ||||||||||||
6564 | else { | ||||||||||||
6565 | S.Diag(AttrLoc, diag::warn_attribute_type_not_supported) << attr << II; | ||||||||||||
6566 | attr.setInvalid(); | ||||||||||||
6567 | return true; | ||||||||||||
6568 | } | ||||||||||||
6569 | |||||||||||||
6570 | // Just ignore lifetime attributes other than __weak and __unsafe_unretained | ||||||||||||
6571 | // outside of ARC mode. | ||||||||||||
6572 | if (!S.getLangOpts().ObjCAutoRefCount && | ||||||||||||
6573 | lifetime != Qualifiers::OCL_Weak && | ||||||||||||
6574 | lifetime != Qualifiers::OCL_ExplicitNone) { | ||||||||||||
6575 | return true; | ||||||||||||
6576 | } | ||||||||||||
6577 | |||||||||||||
6578 | SplitQualType underlyingType = type.split(); | ||||||||||||
6579 | |||||||||||||
6580 | // Check for redundant/conflicting ownership qualifiers. | ||||||||||||
6581 | if (Qualifiers::ObjCLifetime previousLifetime | ||||||||||||
6582 | = type.getQualifiers().getObjCLifetime()) { | ||||||||||||
6583 | // If it's written directly, that's an error. | ||||||||||||
6584 | if (S.Context.hasDirectOwnershipQualifier(type)) { | ||||||||||||
6585 | S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant) | ||||||||||||
6586 | << type; | ||||||||||||
6587 | return true; | ||||||||||||
6588 | } | ||||||||||||
6589 | |||||||||||||
6590 | // Otherwise, if the qualifiers actually conflict, pull sugar off | ||||||||||||
6591 | // and remove the ObjCLifetime qualifiers. | ||||||||||||
6592 | if (previousLifetime != lifetime) { | ||||||||||||
6593 | // It's possible to have multiple local ObjCLifetime qualifiers. We | ||||||||||||
6594 | // can't stop after we reach a type that is directly qualified. | ||||||||||||
6595 | const Type *prevTy = nullptr; | ||||||||||||
6596 | while (!prevTy || prevTy != underlyingType.Ty) { | ||||||||||||
6597 | prevTy = underlyingType.Ty; | ||||||||||||
6598 | underlyingType = underlyingType.getSingleStepDesugaredType(); | ||||||||||||
6599 | } | ||||||||||||
6600 | underlyingType.Quals.removeObjCLifetime(); | ||||||||||||
6601 | } | ||||||||||||
6602 | } | ||||||||||||
6603 | |||||||||||||
6604 | underlyingType.Quals.addObjCLifetime(lifetime); | ||||||||||||
6605 | |||||||||||||
6606 | if (NonObjCPointer) { | ||||||||||||
6607 | StringRef name = attr.getAttrName()->getName(); | ||||||||||||
6608 | switch (lifetime) { | ||||||||||||
6609 | case Qualifiers::OCL_None: | ||||||||||||
6610 | case Qualifiers::OCL_ExplicitNone: | ||||||||||||
6611 | break; | ||||||||||||
6612 | case Qualifiers::OCL_Strong: name = "__strong"; break; | ||||||||||||
6613 | case Qualifiers::OCL_Weak: name = "__weak"; break; | ||||||||||||
6614 | case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break; | ||||||||||||
6615 | } | ||||||||||||
6616 | S.Diag(AttrLoc, diag::warn_type_attribute_wrong_type) << name | ||||||||||||
6617 | << TDS_ObjCObjOrBlock << type; | ||||||||||||
6618 | } | ||||||||||||
6619 | |||||||||||||
6620 | // Don't actually add the __unsafe_unretained qualifier in non-ARC files, | ||||||||||||
6621 | // because having both 'T' and '__unsafe_unretained T' exist in the type | ||||||||||||
6622 | // system causes unfortunate widespread consistency problems. (For example, | ||||||||||||
6623 | // they're not considered compatible types, and we mangle them identicially | ||||||||||||
6624 | // as template arguments.) These problems are all individually fixable, | ||||||||||||
6625 | // but it's easier to just not add the qualifier and instead sniff it out | ||||||||||||
6626 | // in specific places using isObjCInertUnsafeUnretainedType(). | ||||||||||||
6627 | // | ||||||||||||
6628 | // Doing this does means we miss some trivial consistency checks that | ||||||||||||
6629 | // would've triggered in ARC, but that's better than trying to solve all | ||||||||||||
6630 | // the coexistence problems with __unsafe_unretained. | ||||||||||||
6631 | if (!S.getLangOpts().ObjCAutoRefCount && | ||||||||||||
6632 | lifetime == Qualifiers::OCL_ExplicitNone) { | ||||||||||||
6633 | type = state.getAttributedType( | ||||||||||||
6634 | createSimpleAttr<ObjCInertUnsafeUnretainedAttr>(S.Context, attr), | ||||||||||||
6635 | type, type); | ||||||||||||
6636 | return true; | ||||||||||||
6637 | } | ||||||||||||
6638 | |||||||||||||
6639 | QualType origType = type; | ||||||||||||
6640 | if (!NonObjCPointer) | ||||||||||||
6641 | type = S.Context.getQualifiedType(underlyingType); | ||||||||||||
6642 | |||||||||||||
6643 | // If we have a valid source location for the attribute, use an | ||||||||||||
6644 | // AttributedType instead. | ||||||||||||
6645 | if (AttrLoc.isValid()) { | ||||||||||||
6646 | type = state.getAttributedType(::new (S.Context) | ||||||||||||
6647 | ObjCOwnershipAttr(S.Context, attr, II), | ||||||||||||
6648 | origType, type); | ||||||||||||
6649 | } | ||||||||||||
6650 | |||||||||||||
6651 | auto diagnoseOrDelay = [](Sema &S, SourceLocation loc, | ||||||||||||
6652 | unsigned diagnostic, QualType type) { | ||||||||||||
6653 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { | ||||||||||||
6654 | S.DelayedDiagnostics.add( | ||||||||||||
6655 | sema::DelayedDiagnostic::makeForbiddenType( | ||||||||||||
6656 | S.getSourceManager().getExpansionLoc(loc), | ||||||||||||
6657 | diagnostic, type, /*ignored*/ 0)); | ||||||||||||
6658 | } else { | ||||||||||||
6659 | S.Diag(loc, diagnostic); | ||||||||||||
6660 | } | ||||||||||||
6661 | }; | ||||||||||||
6662 | |||||||||||||
6663 | // Sometimes, __weak isn't allowed. | ||||||||||||
6664 | if (lifetime == Qualifiers::OCL_Weak && | ||||||||||||
6665 | !S.getLangOpts().ObjCWeak && !NonObjCPointer) { | ||||||||||||
6666 | |||||||||||||
6667 | // Use a specialized diagnostic if the runtime just doesn't support them. | ||||||||||||
6668 | unsigned diagnostic = | ||||||||||||
6669 | (S.getLangOpts().ObjCWeakRuntime ? diag::err_arc_weak_disabled | ||||||||||||
6670 | : diag::err_arc_weak_no_runtime); | ||||||||||||
6671 | |||||||||||||
6672 | // In any case, delay the diagnostic until we know what we're parsing. | ||||||||||||
6673 | diagnoseOrDelay(S, AttrLoc, diagnostic, type); | ||||||||||||
6674 | |||||||||||||
6675 | attr.setInvalid(); | ||||||||||||
6676 | return true; | ||||||||||||
6677 | } | ||||||||||||
6678 | |||||||||||||
6679 | // Forbid __weak for class objects marked as | ||||||||||||
6680 | // objc_arc_weak_reference_unavailable | ||||||||||||
6681 | if (lifetime == Qualifiers::OCL_Weak) { | ||||||||||||
6682 | if (const ObjCObjectPointerType *ObjT = | ||||||||||||
6683 | type->getAs<ObjCObjectPointerType>()) { | ||||||||||||
6684 | if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) { | ||||||||||||
6685 | if (Class->isArcWeakrefUnavailable()) { | ||||||||||||
6686 | S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class); | ||||||||||||
6687 | S.Diag(ObjT->getInterfaceDecl()->getLocation(), | ||||||||||||
6688 | diag::note_class_declared); | ||||||||||||
6689 | } | ||||||||||||
6690 | } | ||||||||||||
6691 | } | ||||||||||||
6692 | } | ||||||||||||
6693 | |||||||||||||
6694 | return true; | ||||||||||||
6695 | } | ||||||||||||
6696 | |||||||||||||
6697 | /// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type | ||||||||||||
6698 | /// attribute on the specified type. Returns true to indicate that | ||||||||||||
6699 | /// the attribute was handled, false to indicate that the type does | ||||||||||||
6700 | /// not permit the attribute. | ||||||||||||
6701 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, ParsedAttr &attr, | ||||||||||||
6702 | QualType &type) { | ||||||||||||
6703 | Sema &S = state.getSema(); | ||||||||||||
6704 | |||||||||||||
6705 | // Delay if this isn't some kind of pointer. | ||||||||||||
6706 | if (!type->isPointerType() && | ||||||||||||
6707 | !type->isObjCObjectPointerType() && | ||||||||||||
6708 | !type->isBlockPointerType()) | ||||||||||||
6709 | return false; | ||||||||||||
6710 | |||||||||||||
6711 | if (type.getObjCGCAttr() != Qualifiers::GCNone) { | ||||||||||||
6712 | S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc); | ||||||||||||
6713 | attr.setInvalid(); | ||||||||||||
6714 | return true; | ||||||||||||
6715 | } | ||||||||||||
6716 | |||||||||||||
6717 | // Check the attribute arguments. | ||||||||||||
6718 | if (!attr.isArgIdent(0)) { | ||||||||||||
6719 | S.Diag(attr.getLoc(), diag::err_attribute_argument_type) | ||||||||||||
6720 | << attr << AANT_ArgumentString; | ||||||||||||
6721 | attr.setInvalid(); | ||||||||||||
6722 | return true; | ||||||||||||
6723 | } | ||||||||||||
6724 | Qualifiers::GC GCAttr; | ||||||||||||
6725 | if (attr.getNumArgs() > 1) { | ||||||||||||
6726 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << attr | ||||||||||||
6727 | << 1; | ||||||||||||
6728 | attr.setInvalid(); | ||||||||||||
6729 | return true; | ||||||||||||
6730 | } | ||||||||||||
6731 | |||||||||||||
6732 | IdentifierInfo *II = attr.getArgAsIdent(0)->Ident; | ||||||||||||
6733 | if (II->isStr("weak")) | ||||||||||||
6734 | GCAttr = Qualifiers::Weak; | ||||||||||||
6735 | else if (II->isStr("strong")) | ||||||||||||
6736 | GCAttr = Qualifiers::Strong; | ||||||||||||
6737 | else { | ||||||||||||
6738 | S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported) | ||||||||||||
6739 | << attr << II; | ||||||||||||
6740 | attr.setInvalid(); | ||||||||||||
6741 | return true; | ||||||||||||
6742 | } | ||||||||||||
6743 | |||||||||||||
6744 | QualType origType = type; | ||||||||||||
6745 | type = S.Context.getObjCGCQualType(origType, GCAttr); | ||||||||||||
6746 | |||||||||||||
6747 | // Make an attributed type to preserve the source information. | ||||||||||||
6748 | if (attr.getLoc().isValid()) | ||||||||||||
6749 | type = state.getAttributedType( | ||||||||||||
6750 | ::new (S.Context) ObjCGCAttr(S.Context, attr, II), origType, type); | ||||||||||||
6751 | |||||||||||||
6752 | return true; | ||||||||||||
6753 | } | ||||||||||||
6754 | |||||||||||||
6755 | namespace { | ||||||||||||
6756 | /// A helper class to unwrap a type down to a function for the | ||||||||||||
6757 | /// purposes of applying attributes there. | ||||||||||||
6758 | /// | ||||||||||||
6759 | /// Use: | ||||||||||||
6760 | /// FunctionTypeUnwrapper unwrapped(SemaRef, T); | ||||||||||||
6761 | /// if (unwrapped.isFunctionType()) { | ||||||||||||
6762 | /// const FunctionType *fn = unwrapped.get(); | ||||||||||||
6763 | /// // change fn somehow | ||||||||||||
6764 | /// T = unwrapped.wrap(fn); | ||||||||||||
6765 | /// } | ||||||||||||
6766 | struct FunctionTypeUnwrapper { | ||||||||||||
6767 | enum WrapKind { | ||||||||||||
6768 | Desugar, | ||||||||||||
6769 | Attributed, | ||||||||||||
6770 | Parens, | ||||||||||||
6771 | Array, | ||||||||||||
6772 | Pointer, | ||||||||||||
6773 | BlockPointer, | ||||||||||||
6774 | Reference, | ||||||||||||
6775 | MemberPointer, | ||||||||||||
6776 | MacroQualified, | ||||||||||||
6777 | }; | ||||||||||||
6778 | |||||||||||||
6779 | QualType Original; | ||||||||||||
6780 | const FunctionType *Fn; | ||||||||||||
6781 | SmallVector<unsigned char /*WrapKind*/, 8> Stack; | ||||||||||||
6782 | |||||||||||||
6783 | FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) { | ||||||||||||
6784 | while (true) { | ||||||||||||
6785 | const Type *Ty = T.getTypePtr(); | ||||||||||||
6786 | if (isa<FunctionType>(Ty)) { | ||||||||||||
6787 | Fn = cast<FunctionType>(Ty); | ||||||||||||
6788 | return; | ||||||||||||
6789 | } else if (isa<ParenType>(Ty)) { | ||||||||||||
6790 | T = cast<ParenType>(Ty)->getInnerType(); | ||||||||||||
6791 | Stack.push_back(Parens); | ||||||||||||
6792 | } else if (isa<ConstantArrayType>(Ty) || isa<VariableArrayType>(Ty) || | ||||||||||||
6793 | isa<IncompleteArrayType>(Ty)) { | ||||||||||||
6794 | T = cast<ArrayType>(Ty)->getElementType(); | ||||||||||||
6795 | Stack.push_back(Array); | ||||||||||||
6796 | } else if (isa<PointerType>(Ty)) { | ||||||||||||
6797 | T = cast<PointerType>(Ty)->getPointeeType(); | ||||||||||||
6798 | Stack.push_back(Pointer); | ||||||||||||
6799 | } else if (isa<BlockPointerType>(Ty)) { | ||||||||||||
6800 | T = cast<BlockPointerType>(Ty)->getPointeeType(); | ||||||||||||
6801 | Stack.push_back(BlockPointer); | ||||||||||||
6802 | } else if (isa<MemberPointerType>(Ty)) { | ||||||||||||
6803 | T = cast<MemberPointerType>(Ty)->getPointeeType(); | ||||||||||||
6804 | Stack.push_back(MemberPointer); | ||||||||||||
6805 | } else if (isa<ReferenceType>(Ty)) { | ||||||||||||
6806 | T = cast<ReferenceType>(Ty)->getPointeeType(); | ||||||||||||
6807 | Stack.push_back(Reference); | ||||||||||||
6808 | } else if (isa<AttributedType>(Ty)) { | ||||||||||||
6809 | T = cast<AttributedType>(Ty)->getEquivalentType(); | ||||||||||||
6810 | Stack.push_back(Attributed); | ||||||||||||
6811 | } else if (isa<MacroQualifiedType>(Ty)) { | ||||||||||||
6812 | T = cast<MacroQualifiedType>(Ty)->getUnderlyingType(); | ||||||||||||
6813 | Stack.push_back(MacroQualified); | ||||||||||||
6814 | } else { | ||||||||||||
6815 | const Type *DTy = Ty->getUnqualifiedDesugaredType(); | ||||||||||||
6816 | if (Ty == DTy) { | ||||||||||||
6817 | Fn = nullptr; | ||||||||||||
6818 | return; | ||||||||||||
6819 | } | ||||||||||||
6820 | |||||||||||||
6821 | T = QualType(DTy, 0); | ||||||||||||
6822 | Stack.push_back(Desugar); | ||||||||||||
6823 | } | ||||||||||||
6824 | } | ||||||||||||
6825 | } | ||||||||||||
6826 | |||||||||||||
6827 | bool isFunctionType() const { return (Fn != nullptr); } | ||||||||||||
6828 | const FunctionType *get() const { return Fn; } | ||||||||||||
6829 | |||||||||||||
6830 | QualType wrap(Sema &S, const FunctionType *New) { | ||||||||||||
6831 | // If T wasn't modified from the unwrapped type, do nothing. | ||||||||||||
6832 | if (New == get()) return Original; | ||||||||||||
6833 | |||||||||||||
6834 | Fn = New; | ||||||||||||
6835 | return wrap(S.Context, Original, 0); | ||||||||||||
6836 | } | ||||||||||||
6837 | |||||||||||||
6838 | private: | ||||||||||||
6839 | QualType wrap(ASTContext &C, QualType Old, unsigned I) { | ||||||||||||
6840 | if (I == Stack.size()) | ||||||||||||
6841 | return C.getQualifiedType(Fn, Old.getQualifiers()); | ||||||||||||
6842 | |||||||||||||
6843 | // Build up the inner type, applying the qualifiers from the old | ||||||||||||
6844 | // type to the new type. | ||||||||||||
6845 | SplitQualType SplitOld = Old.split(); | ||||||||||||
6846 | |||||||||||||
6847 | // As a special case, tail-recurse if there are no qualifiers. | ||||||||||||
6848 | if (SplitOld.Quals.empty()) | ||||||||||||
6849 | return wrap(C, SplitOld.Ty, I); | ||||||||||||
6850 | return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals); | ||||||||||||
6851 | } | ||||||||||||
6852 | |||||||||||||
6853 | QualType wrap(ASTContext &C, const Type *Old, unsigned I) { | ||||||||||||
6854 | if (I == Stack.size()) return QualType(Fn, 0); | ||||||||||||
6855 | |||||||||||||
6856 | switch (static_cast<WrapKind>(Stack[I++])) { | ||||||||||||
6857 | case Desugar: | ||||||||||||
6858 | // This is the point at which we potentially lose source | ||||||||||||
6859 | // information. | ||||||||||||
6860 | return wrap(C, Old->getUnqualifiedDesugaredType(), I); | ||||||||||||
6861 | |||||||||||||
6862 | case Attributed: | ||||||||||||
6863 | return wrap(C, cast<AttributedType>(Old)->getEquivalentType(), I); | ||||||||||||
6864 | |||||||||||||
6865 | case Parens: { | ||||||||||||
6866 | QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I); | ||||||||||||
6867 | return C.getParenType(New); | ||||||||||||
6868 | } | ||||||||||||
6869 | |||||||||||||
6870 | case MacroQualified: | ||||||||||||
6871 | return wrap(C, cast<MacroQualifiedType>(Old)->getUnderlyingType(), I); | ||||||||||||
6872 | |||||||||||||
6873 | case Array: { | ||||||||||||
6874 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Old)) { | ||||||||||||
6875 | QualType New = wrap(C, CAT->getElementType(), I); | ||||||||||||
6876 | return C.getConstantArrayType(New, CAT->getSize(), CAT->getSizeExpr(), | ||||||||||||
6877 | CAT->getSizeModifier(), | ||||||||||||
6878 | CAT->getIndexTypeCVRQualifiers()); | ||||||||||||
6879 | } | ||||||||||||
6880 | |||||||||||||
6881 | if (const auto *VAT = dyn_cast<VariableArrayType>(Old)) { | ||||||||||||
6882 | QualType New = wrap(C, VAT->getElementType(), I); | ||||||||||||
6883 | return C.getVariableArrayType( | ||||||||||||
6884 | New, VAT->getSizeExpr(), VAT->getSizeModifier(), | ||||||||||||
6885 | VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange()); | ||||||||||||
6886 | } | ||||||||||||
6887 | |||||||||||||
6888 | const auto *IAT = cast<IncompleteArrayType>(Old); | ||||||||||||
6889 | QualType New = wrap(C, IAT->getElementType(), I); | ||||||||||||
6890 | return C.getIncompleteArrayType(New, IAT->getSizeModifier(), | ||||||||||||
6891 | IAT->getIndexTypeCVRQualifiers()); | ||||||||||||
6892 | } | ||||||||||||
6893 | |||||||||||||
6894 | case Pointer: { | ||||||||||||
6895 | QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I); | ||||||||||||
6896 | return C.getPointerType(New); | ||||||||||||
6897 | } | ||||||||||||
6898 | |||||||||||||
6899 | case BlockPointer: { | ||||||||||||
6900 | QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I); | ||||||||||||
6901 | return C.getBlockPointerType(New); | ||||||||||||
6902 | } | ||||||||||||
6903 | |||||||||||||
6904 | case MemberPointer: { | ||||||||||||
6905 | const MemberPointerType *OldMPT = cast<MemberPointerType>(Old); | ||||||||||||
6906 | QualType New = wrap(C, OldMPT->getPointeeType(), I); | ||||||||||||
6907 | return C.getMemberPointerType(New, OldMPT->getClass()); | ||||||||||||
6908 | } | ||||||||||||
6909 | |||||||||||||
6910 | case Reference: { | ||||||||||||
6911 | const ReferenceType *OldRef = cast<ReferenceType>(Old); | ||||||||||||
6912 | QualType New = wrap(C, OldRef->getPointeeType(), I); | ||||||||||||
6913 | if (isa<LValueReferenceType>(OldRef)) | ||||||||||||
6914 | return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue()); | ||||||||||||
6915 | else | ||||||||||||
6916 | return C.getRValueReferenceType(New); | ||||||||||||
6917 | } | ||||||||||||
6918 | } | ||||||||||||
6919 | |||||||||||||
6920 | llvm_unreachable("unknown wrapping kind")::llvm::llvm_unreachable_internal("unknown wrapping kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6920); | ||||||||||||
6921 | } | ||||||||||||
6922 | }; | ||||||||||||
6923 | } // end anonymous namespace | ||||||||||||
6924 | |||||||||||||
6925 | static bool handleMSPointerTypeQualifierAttr(TypeProcessingState &State, | ||||||||||||
6926 | ParsedAttr &PAttr, QualType &Type) { | ||||||||||||
6927 | Sema &S = State.getSema(); | ||||||||||||
6928 | |||||||||||||
6929 | Attr *A; | ||||||||||||
6930 | switch (PAttr.getKind()) { | ||||||||||||
6931 | default: llvm_unreachable("Unknown attribute kind")::llvm::llvm_unreachable_internal("Unknown attribute kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 6931); | ||||||||||||
6932 | case ParsedAttr::AT_Ptr32: | ||||||||||||
6933 | A = createSimpleAttr<Ptr32Attr>(S.Context, PAttr); | ||||||||||||
6934 | break; | ||||||||||||
6935 | case ParsedAttr::AT_Ptr64: | ||||||||||||
6936 | A = createSimpleAttr<Ptr64Attr>(S.Context, PAttr); | ||||||||||||
6937 | break; | ||||||||||||
6938 | case ParsedAttr::AT_SPtr: | ||||||||||||
6939 | A = createSimpleAttr<SPtrAttr>(S.Context, PAttr); | ||||||||||||
6940 | break; | ||||||||||||
6941 | case ParsedAttr::AT_UPtr: | ||||||||||||
6942 | A = createSimpleAttr<UPtrAttr>(S.Context, PAttr); | ||||||||||||
6943 | break; | ||||||||||||
6944 | } | ||||||||||||
6945 | |||||||||||||
6946 | std::bitset<attr::LastAttr> Attrs; | ||||||||||||
6947 | attr::Kind NewAttrKind = A->getKind(); | ||||||||||||
6948 | QualType Desugared = Type; | ||||||||||||
6949 | const AttributedType *AT = dyn_cast<AttributedType>(Type); | ||||||||||||
6950 | while (AT) { | ||||||||||||
6951 | Attrs[AT->getAttrKind()] = true; | ||||||||||||
6952 | Desugared = AT->getModifiedType(); | ||||||||||||
6953 | AT = dyn_cast<AttributedType>(Desugared); | ||||||||||||
6954 | } | ||||||||||||
6955 | |||||||||||||
6956 | // You cannot specify duplicate type attributes, so if the attribute has | ||||||||||||
6957 | // already been applied, flag it. | ||||||||||||
6958 | if (Attrs[NewAttrKind]) { | ||||||||||||
6959 | S.Diag(PAttr.getLoc(), diag::warn_duplicate_attribute_exact) << PAttr; | ||||||||||||
6960 | return true; | ||||||||||||
6961 | } | ||||||||||||
6962 | Attrs[NewAttrKind] = true; | ||||||||||||
6963 | |||||||||||||
6964 | // You cannot have both __sptr and __uptr on the same type, nor can you | ||||||||||||
6965 | // have __ptr32 and __ptr64. | ||||||||||||
6966 | if (Attrs[attr::Ptr32] && Attrs[attr::Ptr64]) { | ||||||||||||
6967 | S.Diag(PAttr.getLoc(), diag::err_attributes_are_not_compatible) | ||||||||||||
6968 | << "'__ptr32'" | ||||||||||||
6969 | << "'__ptr64'"; | ||||||||||||
6970 | return true; | ||||||||||||
6971 | } else if (Attrs[attr::SPtr] && Attrs[attr::UPtr]) { | ||||||||||||
6972 | S.Diag(PAttr.getLoc(), diag::err_attributes_are_not_compatible) | ||||||||||||
6973 | << "'__sptr'" | ||||||||||||
6974 | << "'__uptr'"; | ||||||||||||
6975 | return true; | ||||||||||||
6976 | } | ||||||||||||
6977 | |||||||||||||
6978 | // Pointer type qualifiers can only operate on pointer types, but not | ||||||||||||
6979 | // pointer-to-member types. | ||||||||||||
6980 | // | ||||||||||||
6981 | // FIXME: Should we really be disallowing this attribute if there is any | ||||||||||||
6982 | // type sugar between it and the pointer (other than attributes)? Eg, this | ||||||||||||
6983 | // disallows the attribute on a parenthesized pointer. | ||||||||||||
6984 | // And if so, should we really allow *any* type attribute? | ||||||||||||
6985 | if (!isa<PointerType>(Desugared)) { | ||||||||||||
6986 | if (Type->isMemberPointerType()) | ||||||||||||
6987 | S.Diag(PAttr.getLoc(), diag::err_attribute_no_member_pointers) << PAttr; | ||||||||||||
6988 | else | ||||||||||||
6989 | S.Diag(PAttr.getLoc(), diag::err_attribute_pointers_only) << PAttr << 0; | ||||||||||||
6990 | return true; | ||||||||||||
6991 | } | ||||||||||||
6992 | |||||||||||||
6993 | // Add address space to type based on its attributes. | ||||||||||||
6994 | LangAS ASIdx = LangAS::Default; | ||||||||||||
6995 | uint64_t PtrWidth = S.Context.getTargetInfo().getPointerWidth(0); | ||||||||||||
6996 | if (PtrWidth == 32) { | ||||||||||||
6997 | if (Attrs[attr::Ptr64]) | ||||||||||||
6998 | ASIdx = LangAS::ptr64; | ||||||||||||
6999 | else if (Attrs[attr::UPtr]) | ||||||||||||
7000 | ASIdx = LangAS::ptr32_uptr; | ||||||||||||
7001 | } else if (PtrWidth == 64 && Attrs[attr::Ptr32]) { | ||||||||||||
7002 | if (Attrs[attr::UPtr]) | ||||||||||||
7003 | ASIdx = LangAS::ptr32_uptr; | ||||||||||||
7004 | else | ||||||||||||
7005 | ASIdx = LangAS::ptr32_sptr; | ||||||||||||
7006 | } | ||||||||||||
7007 | |||||||||||||
7008 | QualType Pointee = Type->getPointeeType(); | ||||||||||||
7009 | if (ASIdx != LangAS::Default) | ||||||||||||
7010 | Pointee = S.Context.getAddrSpaceQualType( | ||||||||||||
7011 | S.Context.removeAddrSpaceQualType(Pointee), ASIdx); | ||||||||||||
7012 | Type = State.getAttributedType(A, Type, S.Context.getPointerType(Pointee)); | ||||||||||||
7013 | return false; | ||||||||||||
7014 | } | ||||||||||||
7015 | |||||||||||||
7016 | /// Map a nullability attribute kind to a nullability kind. | ||||||||||||
7017 | static NullabilityKind mapNullabilityAttrKind(ParsedAttr::Kind kind) { | ||||||||||||
7018 | switch (kind) { | ||||||||||||
7019 | case ParsedAttr::AT_TypeNonNull: | ||||||||||||
7020 | return NullabilityKind::NonNull; | ||||||||||||
7021 | |||||||||||||
7022 | case ParsedAttr::AT_TypeNullable: | ||||||||||||
7023 | return NullabilityKind::Nullable; | ||||||||||||
7024 | |||||||||||||
7025 | case ParsedAttr::AT_TypeNullableResult: | ||||||||||||
7026 | return NullabilityKind::NullableResult; | ||||||||||||
7027 | |||||||||||||
7028 | case ParsedAttr::AT_TypeNullUnspecified: | ||||||||||||
7029 | return NullabilityKind::Unspecified; | ||||||||||||
7030 | |||||||||||||
7031 | default: | ||||||||||||
7032 | llvm_unreachable("not a nullability attribute kind")::llvm::llvm_unreachable_internal("not a nullability attribute kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7032); | ||||||||||||
7033 | } | ||||||||||||
7034 | } | ||||||||||||
7035 | |||||||||||||
7036 | /// Applies a nullability type specifier to the given type, if possible. | ||||||||||||
7037 | /// | ||||||||||||
7038 | /// \param state The type processing state. | ||||||||||||
7039 | /// | ||||||||||||
7040 | /// \param type The type to which the nullability specifier will be | ||||||||||||
7041 | /// added. On success, this type will be updated appropriately. | ||||||||||||
7042 | /// | ||||||||||||
7043 | /// \param attr The attribute as written on the type. | ||||||||||||
7044 | /// | ||||||||||||
7045 | /// \param allowOnArrayType Whether to accept nullability specifiers on an | ||||||||||||
7046 | /// array type (e.g., because it will decay to a pointer). | ||||||||||||
7047 | /// | ||||||||||||
7048 | /// \returns true if a problem has been diagnosed, false on success. | ||||||||||||
7049 | static bool checkNullabilityTypeSpecifier(TypeProcessingState &state, | ||||||||||||
7050 | QualType &type, | ||||||||||||
7051 | ParsedAttr &attr, | ||||||||||||
7052 | bool allowOnArrayType) { | ||||||||||||
7053 | Sema &S = state.getSema(); | ||||||||||||
7054 | |||||||||||||
7055 | NullabilityKind nullability = mapNullabilityAttrKind(attr.getKind()); | ||||||||||||
7056 | SourceLocation nullabilityLoc = attr.getLoc(); | ||||||||||||
7057 | bool isContextSensitive = attr.isContextSensitiveKeywordAttribute(); | ||||||||||||
7058 | |||||||||||||
7059 | recordNullabilitySeen(S, nullabilityLoc); | ||||||||||||
7060 | |||||||||||||
7061 | // Check for existing nullability attributes on the type. | ||||||||||||
7062 | QualType desugared = type; | ||||||||||||
7063 | while (auto attributed = dyn_cast<AttributedType>(desugared.getTypePtr())) { | ||||||||||||
7064 | // Check whether there is already a null | ||||||||||||
7065 | if (auto existingNullability = attributed->getImmediateNullability()) { | ||||||||||||
7066 | // Duplicated nullability. | ||||||||||||
7067 | if (nullability == *existingNullability) { | ||||||||||||
7068 | S.Diag(nullabilityLoc, diag::warn_nullability_duplicate) | ||||||||||||
7069 | << DiagNullabilityKind(nullability, isContextSensitive) | ||||||||||||
7070 | << FixItHint::CreateRemoval(nullabilityLoc); | ||||||||||||
7071 | |||||||||||||
7072 | break; | ||||||||||||
7073 | } | ||||||||||||
7074 | |||||||||||||
7075 | // Conflicting nullability. | ||||||||||||
7076 | S.Diag(nullabilityLoc, diag::err_nullability_conflicting) | ||||||||||||
7077 | << DiagNullabilityKind(nullability, isContextSensitive) | ||||||||||||
7078 | << DiagNullabilityKind(*existingNullability, false); | ||||||||||||
7079 | return true; | ||||||||||||
7080 | } | ||||||||||||
7081 | |||||||||||||
7082 | desugared = attributed->getModifiedType(); | ||||||||||||
7083 | } | ||||||||||||
7084 | |||||||||||||
7085 | // If there is already a different nullability specifier, complain. | ||||||||||||
7086 | // This (unlike the code above) looks through typedefs that might | ||||||||||||
7087 | // have nullability specifiers on them, which means we cannot | ||||||||||||
7088 | // provide a useful Fix-It. | ||||||||||||
7089 | if (auto existingNullability = desugared->getNullability(S.Context)) { | ||||||||||||
7090 | if (nullability != *existingNullability) { | ||||||||||||
7091 | S.Diag(nullabilityLoc, diag::err_nullability_conflicting) | ||||||||||||
7092 | << DiagNullabilityKind(nullability, isContextSensitive) | ||||||||||||
7093 | << DiagNullabilityKind(*existingNullability, false); | ||||||||||||
7094 | |||||||||||||
7095 | // Try to find the typedef with the existing nullability specifier. | ||||||||||||
7096 | if (auto typedefType = desugared->getAs<TypedefType>()) { | ||||||||||||
7097 | TypedefNameDecl *typedefDecl = typedefType->getDecl(); | ||||||||||||
7098 | QualType underlyingType = typedefDecl->getUnderlyingType(); | ||||||||||||
7099 | if (auto typedefNullability | ||||||||||||
7100 | = AttributedType::stripOuterNullability(underlyingType)) { | ||||||||||||
7101 | if (*typedefNullability == *existingNullability) { | ||||||||||||
7102 | S.Diag(typedefDecl->getLocation(), diag::note_nullability_here) | ||||||||||||
7103 | << DiagNullabilityKind(*existingNullability, false); | ||||||||||||
7104 | } | ||||||||||||
7105 | } | ||||||||||||
7106 | } | ||||||||||||
7107 | |||||||||||||
7108 | return true; | ||||||||||||
7109 | } | ||||||||||||
7110 | } | ||||||||||||
7111 | |||||||||||||
7112 | // If this definitely isn't a pointer type, reject the specifier. | ||||||||||||
7113 | if (!desugared->canHaveNullability() && | ||||||||||||
7114 | !(allowOnArrayType && desugared->isArrayType())) { | ||||||||||||
7115 | S.Diag(nullabilityLoc, diag::err_nullability_nonpointer) | ||||||||||||
7116 | << DiagNullabilityKind(nullability, isContextSensitive) << type; | ||||||||||||
7117 | return true; | ||||||||||||
7118 | } | ||||||||||||
7119 | |||||||||||||
7120 | // For the context-sensitive keywords/Objective-C property | ||||||||||||
7121 | // attributes, require that the type be a single-level pointer. | ||||||||||||
7122 | if (isContextSensitive) { | ||||||||||||
7123 | // Make sure that the pointee isn't itself a pointer type. | ||||||||||||
7124 | const Type *pointeeType = nullptr; | ||||||||||||
7125 | if (desugared->isArrayType()) | ||||||||||||
7126 | pointeeType = desugared->getArrayElementTypeNoTypeQual(); | ||||||||||||
7127 | else if (desugared->isAnyPointerType()) | ||||||||||||
7128 | pointeeType = desugared->getPointeeType().getTypePtr(); | ||||||||||||
7129 | |||||||||||||
7130 | if (pointeeType && (pointeeType->isAnyPointerType() || | ||||||||||||
7131 | pointeeType->isObjCObjectPointerType() || | ||||||||||||
7132 | pointeeType->isMemberPointerType())) { | ||||||||||||
7133 | S.Diag(nullabilityLoc, diag::err_nullability_cs_multilevel) | ||||||||||||
7134 | << DiagNullabilityKind(nullability, true) | ||||||||||||
7135 | << type; | ||||||||||||
7136 | S.Diag(nullabilityLoc, diag::note_nullability_type_specifier) | ||||||||||||
7137 | << DiagNullabilityKind(nullability, false) | ||||||||||||
7138 | << type | ||||||||||||
7139 | << FixItHint::CreateReplacement(nullabilityLoc, | ||||||||||||
7140 | getNullabilitySpelling(nullability)); | ||||||||||||
7141 | return true; | ||||||||||||
7142 | } | ||||||||||||
7143 | } | ||||||||||||
7144 | |||||||||||||
7145 | // Form the attributed type. | ||||||||||||
7146 | type = state.getAttributedType( | ||||||||||||
7147 | createNullabilityAttr(S.Context, attr, nullability), type, type); | ||||||||||||
7148 | return false; | ||||||||||||
7149 | } | ||||||||||||
7150 | |||||||||||||
7151 | /// Check the application of the Objective-C '__kindof' qualifier to | ||||||||||||
7152 | /// the given type. | ||||||||||||
7153 | static bool checkObjCKindOfType(TypeProcessingState &state, QualType &type, | ||||||||||||
7154 | ParsedAttr &attr) { | ||||||||||||
7155 | Sema &S = state.getSema(); | ||||||||||||
7156 | |||||||||||||
7157 | if (isa<ObjCTypeParamType>(type)) { | ||||||||||||
7158 | // Build the attributed type to record where __kindof occurred. | ||||||||||||
7159 | type = state.getAttributedType( | ||||||||||||
7160 | createSimpleAttr<ObjCKindOfAttr>(S.Context, attr), type, type); | ||||||||||||
7161 | return false; | ||||||||||||
7162 | } | ||||||||||||
7163 | |||||||||||||
7164 | // Find out if it's an Objective-C object or object pointer type; | ||||||||||||
7165 | const ObjCObjectPointerType *ptrType = type->getAs<ObjCObjectPointerType>(); | ||||||||||||
7166 | const ObjCObjectType *objType = ptrType ? ptrType->getObjectType() | ||||||||||||
7167 | : type->getAs<ObjCObjectType>(); | ||||||||||||
7168 | |||||||||||||
7169 | // If not, we can't apply __kindof. | ||||||||||||
7170 | if (!objType) { | ||||||||||||
7171 | // FIXME: Handle dependent types that aren't yet object types. | ||||||||||||
7172 | S.Diag(attr.getLoc(), diag::err_objc_kindof_nonobject) | ||||||||||||
7173 | << type; | ||||||||||||
7174 | return true; | ||||||||||||
7175 | } | ||||||||||||
7176 | |||||||||||||
7177 | // Rebuild the "equivalent" type, which pushes __kindof down into | ||||||||||||
7178 | // the object type. | ||||||||||||
7179 | // There is no need to apply kindof on an unqualified id type. | ||||||||||||
7180 | QualType equivType = S.Context.getObjCObjectType( | ||||||||||||
7181 | objType->getBaseType(), objType->getTypeArgsAsWritten(), | ||||||||||||
7182 | objType->getProtocols(), | ||||||||||||
7183 | /*isKindOf=*/objType->isObjCUnqualifiedId() ? false : true); | ||||||||||||
7184 | |||||||||||||
7185 | // If we started with an object pointer type, rebuild it. | ||||||||||||
7186 | if (ptrType) { | ||||||||||||
7187 | equivType = S.Context.getObjCObjectPointerType(equivType); | ||||||||||||
7188 | if (auto nullability = type->getNullability(S.Context)) { | ||||||||||||
7189 | // We create a nullability attribute from the __kindof attribute. | ||||||||||||
7190 | // Make sure that will make sense. | ||||||||||||
7191 | assert(attr.getAttributeSpellingListIndex() == 0 &&((attr.getAttributeSpellingListIndex() == 0 && "multiple spellings for __kindof?" ) ? static_cast<void> (0) : __assert_fail ("attr.getAttributeSpellingListIndex() == 0 && \"multiple spellings for __kindof?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7192, __PRETTY_FUNCTION__)) | ||||||||||||
7192 | "multiple spellings for __kindof?")((attr.getAttributeSpellingListIndex() == 0 && "multiple spellings for __kindof?" ) ? static_cast<void> (0) : __assert_fail ("attr.getAttributeSpellingListIndex() == 0 && \"multiple spellings for __kindof?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7192, __PRETTY_FUNCTION__)); | ||||||||||||
7193 | Attr *A = createNullabilityAttr(S.Context, attr, *nullability); | ||||||||||||
7194 | A->setImplicit(true); | ||||||||||||
7195 | equivType = state.getAttributedType(A, equivType, equivType); | ||||||||||||
7196 | } | ||||||||||||
7197 | } | ||||||||||||
7198 | |||||||||||||
7199 | // Build the attributed type to record where __kindof occurred. | ||||||||||||
7200 | type = state.getAttributedType( | ||||||||||||
7201 | createSimpleAttr<ObjCKindOfAttr>(S.Context, attr), type, equivType); | ||||||||||||
7202 | return false; | ||||||||||||
7203 | } | ||||||||||||
7204 | |||||||||||||
7205 | /// Distribute a nullability type attribute that cannot be applied to | ||||||||||||
7206 | /// the type specifier to a pointer, block pointer, or member pointer | ||||||||||||
7207 | /// declarator, complaining if necessary. | ||||||||||||
7208 | /// | ||||||||||||
7209 | /// \returns true if the nullability annotation was distributed, false | ||||||||||||
7210 | /// otherwise. | ||||||||||||
7211 | static bool distributeNullabilityTypeAttr(TypeProcessingState &state, | ||||||||||||
7212 | QualType type, ParsedAttr &attr) { | ||||||||||||
7213 | Declarator &declarator = state.getDeclarator(); | ||||||||||||
7214 | |||||||||||||
7215 | /// Attempt to move the attribute to the specified chunk. | ||||||||||||
7216 | auto moveToChunk = [&](DeclaratorChunk &chunk, bool inFunction) -> bool { | ||||||||||||
7217 | // If there is already a nullability attribute there, don't add | ||||||||||||
7218 | // one. | ||||||||||||
7219 | if (hasNullabilityAttr(chunk.getAttrs())) | ||||||||||||
7220 | return false; | ||||||||||||
7221 | |||||||||||||
7222 | // Complain about the nullability qualifier being in the wrong | ||||||||||||
7223 | // place. | ||||||||||||
7224 | enum { | ||||||||||||
7225 | PK_Pointer, | ||||||||||||
7226 | PK_BlockPointer, | ||||||||||||
7227 | PK_MemberPointer, | ||||||||||||
7228 | PK_FunctionPointer, | ||||||||||||
7229 | PK_MemberFunctionPointer, | ||||||||||||
7230 | } pointerKind | ||||||||||||
7231 | = chunk.Kind == DeclaratorChunk::Pointer ? (inFunction ? PK_FunctionPointer | ||||||||||||
7232 | : PK_Pointer) | ||||||||||||
7233 | : chunk.Kind == DeclaratorChunk::BlockPointer ? PK_BlockPointer | ||||||||||||
7234 | : inFunction? PK_MemberFunctionPointer : PK_MemberPointer; | ||||||||||||
7235 | |||||||||||||
7236 | auto diag = state.getSema().Diag(attr.getLoc(), | ||||||||||||
7237 | diag::warn_nullability_declspec) | ||||||||||||
7238 | << DiagNullabilityKind(mapNullabilityAttrKind(attr.getKind()), | ||||||||||||
7239 | attr.isContextSensitiveKeywordAttribute()) | ||||||||||||
7240 | << type | ||||||||||||
7241 | << static_cast<unsigned>(pointerKind); | ||||||||||||
7242 | |||||||||||||
7243 | // FIXME: MemberPointer chunks don't carry the location of the *. | ||||||||||||
7244 | if (chunk.Kind != DeclaratorChunk::MemberPointer) { | ||||||||||||
7245 | diag << FixItHint::CreateRemoval(attr.getLoc()) | ||||||||||||
7246 | << FixItHint::CreateInsertion( | ||||||||||||
7247 | state.getSema().getPreprocessor().getLocForEndOfToken( | ||||||||||||
7248 | chunk.Loc), | ||||||||||||
7249 | " " + attr.getAttrName()->getName().str() + " "); | ||||||||||||
7250 | } | ||||||||||||
7251 | |||||||||||||
7252 | moveAttrFromListToList(attr, state.getCurrentAttributes(), | ||||||||||||
7253 | chunk.getAttrs()); | ||||||||||||
7254 | return true; | ||||||||||||
7255 | }; | ||||||||||||
7256 | |||||||||||||
7257 | // Move it to the outermost pointer, member pointer, or block | ||||||||||||
7258 | // pointer declarator. | ||||||||||||
7259 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { | ||||||||||||
7260 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); | ||||||||||||
7261 | switch (chunk.Kind) { | ||||||||||||
7262 | case DeclaratorChunk::Pointer: | ||||||||||||
7263 | case DeclaratorChunk::BlockPointer: | ||||||||||||
7264 | case DeclaratorChunk::MemberPointer: | ||||||||||||
7265 | return moveToChunk(chunk, false); | ||||||||||||
7266 | |||||||||||||
7267 | case DeclaratorChunk::Paren: | ||||||||||||
7268 | case DeclaratorChunk::Array: | ||||||||||||
7269 | continue; | ||||||||||||
7270 | |||||||||||||
7271 | case DeclaratorChunk::Function: | ||||||||||||
7272 | // Try to move past the return type to a function/block/member | ||||||||||||
7273 | // function pointer. | ||||||||||||
7274 | if (DeclaratorChunk *dest = maybeMovePastReturnType( | ||||||||||||
7275 | declarator, i, | ||||||||||||
7276 | /*onlyBlockPointers=*/false)) { | ||||||||||||
7277 | return moveToChunk(*dest, true); | ||||||||||||
7278 | } | ||||||||||||
7279 | |||||||||||||
7280 | return false; | ||||||||||||
7281 | |||||||||||||
7282 | // Don't walk through these. | ||||||||||||
7283 | case DeclaratorChunk::Reference: | ||||||||||||
7284 | case DeclaratorChunk::Pipe: | ||||||||||||
7285 | return false; | ||||||||||||
7286 | } | ||||||||||||
7287 | } | ||||||||||||
7288 | |||||||||||||
7289 | return false; | ||||||||||||
7290 | } | ||||||||||||
7291 | |||||||||||||
7292 | static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) { | ||||||||||||
7293 | assert(!Attr.isInvalid())((!Attr.isInvalid()) ? static_cast<void> (0) : __assert_fail ("!Attr.isInvalid()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7293, __PRETTY_FUNCTION__)); | ||||||||||||
7294 | switch (Attr.getKind()) { | ||||||||||||
7295 | default: | ||||||||||||
7296 | llvm_unreachable("not a calling convention attribute")::llvm::llvm_unreachable_internal("not a calling convention attribute" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7296); | ||||||||||||
7297 | case ParsedAttr::AT_CDecl: | ||||||||||||
7298 | return createSimpleAttr<CDeclAttr>(Ctx, Attr); | ||||||||||||
7299 | case ParsedAttr::AT_FastCall: | ||||||||||||
7300 | return createSimpleAttr<FastCallAttr>(Ctx, Attr); | ||||||||||||
7301 | case ParsedAttr::AT_StdCall: | ||||||||||||
7302 | return createSimpleAttr<StdCallAttr>(Ctx, Attr); | ||||||||||||
7303 | case ParsedAttr::AT_ThisCall: | ||||||||||||
7304 | return createSimpleAttr<ThisCallAttr>(Ctx, Attr); | ||||||||||||
7305 | case ParsedAttr::AT_RegCall: | ||||||||||||
7306 | return createSimpleAttr<RegCallAttr>(Ctx, Attr); | ||||||||||||
7307 | case ParsedAttr::AT_Pascal: | ||||||||||||
7308 | return createSimpleAttr<PascalAttr>(Ctx, Attr); | ||||||||||||
7309 | case ParsedAttr::AT_SwiftCall: | ||||||||||||
7310 | return createSimpleAttr<SwiftCallAttr>(Ctx, Attr); | ||||||||||||
7311 | case ParsedAttr::AT_VectorCall: | ||||||||||||
7312 | return createSimpleAttr<VectorCallAttr>(Ctx, Attr); | ||||||||||||
7313 | case ParsedAttr::AT_AArch64VectorPcs: | ||||||||||||
7314 | return createSimpleAttr<AArch64VectorPcsAttr>(Ctx, Attr); | ||||||||||||
7315 | case ParsedAttr::AT_Pcs: { | ||||||||||||
7316 | // The attribute may have had a fixit applied where we treated an | ||||||||||||
7317 | // identifier as a string literal. The contents of the string are valid, | ||||||||||||
7318 | // but the form may not be. | ||||||||||||
7319 | StringRef Str; | ||||||||||||
7320 | if (Attr.isArgExpr(0)) | ||||||||||||
7321 | Str = cast<StringLiteral>(Attr.getArgAsExpr(0))->getString(); | ||||||||||||
7322 | else | ||||||||||||
7323 | Str = Attr.getArgAsIdent(0)->Ident->getName(); | ||||||||||||
7324 | PcsAttr::PCSType Type; | ||||||||||||
7325 | if (!PcsAttr::ConvertStrToPCSType(Str, Type)) | ||||||||||||
7326 | llvm_unreachable("already validated the attribute")::llvm::llvm_unreachable_internal("already validated the attribute" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7326); | ||||||||||||
7327 | return ::new (Ctx) PcsAttr(Ctx, Attr, Type); | ||||||||||||
7328 | } | ||||||||||||
7329 | case ParsedAttr::AT_IntelOclBicc: | ||||||||||||
7330 | return createSimpleAttr<IntelOclBiccAttr>(Ctx, Attr); | ||||||||||||
7331 | case ParsedAttr::AT_MSABI: | ||||||||||||
7332 | return createSimpleAttr<MSABIAttr>(Ctx, Attr); | ||||||||||||
7333 | case ParsedAttr::AT_SysVABI: | ||||||||||||
7334 | return createSimpleAttr<SysVABIAttr>(Ctx, Attr); | ||||||||||||
7335 | case ParsedAttr::AT_PreserveMost: | ||||||||||||
7336 | return createSimpleAttr<PreserveMostAttr>(Ctx, Attr); | ||||||||||||
7337 | case ParsedAttr::AT_PreserveAll: | ||||||||||||
7338 | return createSimpleAttr<PreserveAllAttr>(Ctx, Attr); | ||||||||||||
7339 | } | ||||||||||||
7340 | llvm_unreachable("unexpected attribute kind!")::llvm::llvm_unreachable_internal("unexpected attribute kind!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7340); | ||||||||||||
7341 | } | ||||||||||||
7342 | |||||||||||||
7343 | /// Process an individual function attribute. Returns true to | ||||||||||||
7344 | /// indicate that the attribute was handled, false if it wasn't. | ||||||||||||
7345 | static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, | ||||||||||||
7346 | QualType &type) { | ||||||||||||
7347 | Sema &S = state.getSema(); | ||||||||||||
7348 | |||||||||||||
7349 | FunctionTypeUnwrapper unwrapped(S, type); | ||||||||||||
7350 | |||||||||||||
7351 | if (attr.getKind() == ParsedAttr::AT_NoReturn) { | ||||||||||||
7352 | if (S.CheckAttrNoArgs(attr)) | ||||||||||||
7353 | return true; | ||||||||||||
7354 | |||||||||||||
7355 | // Delay if this is not a function type. | ||||||||||||
7356 | if (!unwrapped.isFunctionType()) | ||||||||||||
7357 | return false; | ||||||||||||
7358 | |||||||||||||
7359 | // Otherwise we can process right away. | ||||||||||||
7360 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true); | ||||||||||||
7361 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7362 | return true; | ||||||||||||
7363 | } | ||||||||||||
7364 | |||||||||||||
7365 | if (attr.getKind() == ParsedAttr::AT_CmseNSCall) { | ||||||||||||
7366 | // Delay if this is not a function type. | ||||||||||||
7367 | if (!unwrapped.isFunctionType()) | ||||||||||||
7368 | return false; | ||||||||||||
7369 | |||||||||||||
7370 | // Ignore if we don't have CMSE enabled. | ||||||||||||
7371 | if (!S.getLangOpts().Cmse) { | ||||||||||||
7372 | S.Diag(attr.getLoc(), diag::warn_attribute_ignored) << attr; | ||||||||||||
7373 | attr.setInvalid(); | ||||||||||||
7374 | return true; | ||||||||||||
7375 | } | ||||||||||||
7376 | |||||||||||||
7377 | // Otherwise we can process right away. | ||||||||||||
7378 | FunctionType::ExtInfo EI = | ||||||||||||
7379 | unwrapped.get()->getExtInfo().withCmseNSCall(true); | ||||||||||||
7380 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7381 | return true; | ||||||||||||
7382 | } | ||||||||||||
7383 | |||||||||||||
7384 | // ns_returns_retained is not always a type attribute, but if we got | ||||||||||||
7385 | // here, we're treating it as one right now. | ||||||||||||
7386 | if (attr.getKind() == ParsedAttr::AT_NSReturnsRetained) { | ||||||||||||
7387 | if (attr.getNumArgs()) return true; | ||||||||||||
7388 | |||||||||||||
7389 | // Delay if this is not a function type. | ||||||||||||
7390 | if (!unwrapped.isFunctionType()) | ||||||||||||
7391 | return false; | ||||||||||||
7392 | |||||||||||||
7393 | // Check whether the return type is reasonable. | ||||||||||||
7394 | if (S.checkNSReturnsRetainedReturnType(attr.getLoc(), | ||||||||||||
7395 | unwrapped.get()->getReturnType())) | ||||||||||||
7396 | return true; | ||||||||||||
7397 | |||||||||||||
7398 | // Only actually change the underlying type in ARC builds. | ||||||||||||
7399 | QualType origType = type; | ||||||||||||
7400 | if (state.getSema().getLangOpts().ObjCAutoRefCount) { | ||||||||||||
7401 | FunctionType::ExtInfo EI | ||||||||||||
7402 | = unwrapped.get()->getExtInfo().withProducesResult(true); | ||||||||||||
7403 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7404 | } | ||||||||||||
7405 | type = state.getAttributedType( | ||||||||||||
7406 | createSimpleAttr<NSReturnsRetainedAttr>(S.Context, attr), | ||||||||||||
7407 | origType, type); | ||||||||||||
7408 | return true; | ||||||||||||
7409 | } | ||||||||||||
7410 | |||||||||||||
7411 | if (attr.getKind() == ParsedAttr::AT_AnyX86NoCallerSavedRegisters) { | ||||||||||||
7412 | if (S.CheckAttrTarget(attr) || S.CheckAttrNoArgs(attr)) | ||||||||||||
7413 | return true; | ||||||||||||
7414 | |||||||||||||
7415 | // Delay if this is not a function type. | ||||||||||||
7416 | if (!unwrapped.isFunctionType()) | ||||||||||||
7417 | return false; | ||||||||||||
7418 | |||||||||||||
7419 | FunctionType::ExtInfo EI = | ||||||||||||
7420 | unwrapped.get()->getExtInfo().withNoCallerSavedRegs(true); | ||||||||||||
7421 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7422 | return true; | ||||||||||||
7423 | } | ||||||||||||
7424 | |||||||||||||
7425 | if (attr.getKind() == ParsedAttr::AT_AnyX86NoCfCheck) { | ||||||||||||
7426 | if (!S.getLangOpts().CFProtectionBranch) { | ||||||||||||
7427 | S.Diag(attr.getLoc(), diag::warn_nocf_check_attribute_ignored); | ||||||||||||
7428 | attr.setInvalid(); | ||||||||||||
7429 | return true; | ||||||||||||
7430 | } | ||||||||||||
7431 | |||||||||||||
7432 | if (S.CheckAttrTarget(attr) || S.CheckAttrNoArgs(attr)) | ||||||||||||
7433 | return true; | ||||||||||||
7434 | |||||||||||||
7435 | // If this is not a function type, warning will be asserted by subject | ||||||||||||
7436 | // check. | ||||||||||||
7437 | if (!unwrapped.isFunctionType()) | ||||||||||||
7438 | return true; | ||||||||||||
7439 | |||||||||||||
7440 | FunctionType::ExtInfo EI = | ||||||||||||
7441 | unwrapped.get()->getExtInfo().withNoCfCheck(true); | ||||||||||||
7442 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7443 | return true; | ||||||||||||
7444 | } | ||||||||||||
7445 | |||||||||||||
7446 | if (attr.getKind() == ParsedAttr::AT_Regparm) { | ||||||||||||
7447 | unsigned value; | ||||||||||||
7448 | if (S.CheckRegparmAttr(attr, value)) | ||||||||||||
7449 | return true; | ||||||||||||
7450 | |||||||||||||
7451 | // Delay if this is not a function type. | ||||||||||||
7452 | if (!unwrapped.isFunctionType()) | ||||||||||||
7453 | return false; | ||||||||||||
7454 | |||||||||||||
7455 | // Diagnose regparm with fastcall. | ||||||||||||
7456 | const FunctionType *fn = unwrapped.get(); | ||||||||||||
7457 | CallingConv CC = fn->getCallConv(); | ||||||||||||
7458 | if (CC == CC_X86FastCall) { | ||||||||||||
7459 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) | ||||||||||||
7460 | << FunctionType::getNameForCallConv(CC) | ||||||||||||
7461 | << "regparm"; | ||||||||||||
7462 | attr.setInvalid(); | ||||||||||||
7463 | return true; | ||||||||||||
7464 | } | ||||||||||||
7465 | |||||||||||||
7466 | FunctionType::ExtInfo EI = | ||||||||||||
7467 | unwrapped.get()->getExtInfo().withRegParm(value); | ||||||||||||
7468 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7469 | return true; | ||||||||||||
7470 | } | ||||||||||||
7471 | |||||||||||||
7472 | if (attr.getKind() == ParsedAttr::AT_NoThrow) { | ||||||||||||
7473 | // Delay if this is not a function type. | ||||||||||||
7474 | if (!unwrapped.isFunctionType()) | ||||||||||||
7475 | return false; | ||||||||||||
7476 | |||||||||||||
7477 | if (S.CheckAttrNoArgs(attr)) { | ||||||||||||
7478 | attr.setInvalid(); | ||||||||||||
7479 | return true; | ||||||||||||
7480 | } | ||||||||||||
7481 | |||||||||||||
7482 | // Otherwise we can process right away. | ||||||||||||
7483 | auto *Proto = unwrapped.get()->castAs<FunctionProtoType>(); | ||||||||||||
7484 | |||||||||||||
7485 | // MSVC ignores nothrow if it is in conflict with an explicit exception | ||||||||||||
7486 | // specification. | ||||||||||||
7487 | if (Proto->hasExceptionSpec()) { | ||||||||||||
7488 | switch (Proto->getExceptionSpecType()) { | ||||||||||||
7489 | case EST_None: | ||||||||||||
7490 | llvm_unreachable("This doesn't have an exception spec!")::llvm::llvm_unreachable_internal("This doesn't have an exception spec!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7490); | ||||||||||||
7491 | |||||||||||||
7492 | case EST_DynamicNone: | ||||||||||||
7493 | case EST_BasicNoexcept: | ||||||||||||
7494 | case EST_NoexceptTrue: | ||||||||||||
7495 | case EST_NoThrow: | ||||||||||||
7496 | // Exception spec doesn't conflict with nothrow, so don't warn. | ||||||||||||
7497 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
7498 | case EST_Unparsed: | ||||||||||||
7499 | case EST_Uninstantiated: | ||||||||||||
7500 | case EST_DependentNoexcept: | ||||||||||||
7501 | case EST_Unevaluated: | ||||||||||||
7502 | // We don't have enough information to properly determine if there is a | ||||||||||||
7503 | // conflict, so suppress the warning. | ||||||||||||
7504 | break; | ||||||||||||
7505 | case EST_Dynamic: | ||||||||||||
7506 | case EST_MSAny: | ||||||||||||
7507 | case EST_NoexceptFalse: | ||||||||||||
7508 | S.Diag(attr.getLoc(), diag::warn_nothrow_attribute_ignored); | ||||||||||||
7509 | break; | ||||||||||||
7510 | } | ||||||||||||
7511 | return true; | ||||||||||||
7512 | } | ||||||||||||
7513 | |||||||||||||
7514 | type = unwrapped.wrap( | ||||||||||||
7515 | S, S.Context | ||||||||||||
7516 | .getFunctionTypeWithExceptionSpec( | ||||||||||||
7517 | QualType{Proto, 0}, | ||||||||||||
7518 | FunctionProtoType::ExceptionSpecInfo{EST_NoThrow}) | ||||||||||||
7519 | ->getAs<FunctionType>()); | ||||||||||||
7520 | return true; | ||||||||||||
7521 | } | ||||||||||||
7522 | |||||||||||||
7523 | // Delay if the type didn't work out to a function. | ||||||||||||
7524 | if (!unwrapped.isFunctionType()) return false; | ||||||||||||
7525 | |||||||||||||
7526 | // Otherwise, a calling convention. | ||||||||||||
7527 | CallingConv CC; | ||||||||||||
7528 | if (S.CheckCallingConvAttr(attr, CC)) | ||||||||||||
7529 | return true; | ||||||||||||
7530 | |||||||||||||
7531 | const FunctionType *fn = unwrapped.get(); | ||||||||||||
7532 | CallingConv CCOld = fn->getCallConv(); | ||||||||||||
7533 | Attr *CCAttr = getCCTypeAttr(S.Context, attr); | ||||||||||||
7534 | |||||||||||||
7535 | if (CCOld != CC) { | ||||||||||||
7536 | // Error out on when there's already an attribute on the type | ||||||||||||
7537 | // and the CCs don't match. | ||||||||||||
7538 | if (S.getCallingConvAttributedType(type)) { | ||||||||||||
7539 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) | ||||||||||||
7540 | << FunctionType::getNameForCallConv(CC) | ||||||||||||
7541 | << FunctionType::getNameForCallConv(CCOld); | ||||||||||||
7542 | attr.setInvalid(); | ||||||||||||
7543 | return true; | ||||||||||||
7544 | } | ||||||||||||
7545 | } | ||||||||||||
7546 | |||||||||||||
7547 | // Diagnose use of variadic functions with calling conventions that | ||||||||||||
7548 | // don't support them (e.g. because they're callee-cleanup). | ||||||||||||
7549 | // We delay warning about this on unprototyped function declarations | ||||||||||||
7550 | // until after redeclaration checking, just in case we pick up a | ||||||||||||
7551 | // prototype that way. And apparently we also "delay" warning about | ||||||||||||
7552 | // unprototyped function types in general, despite not necessarily having | ||||||||||||
7553 | // much ability to diagnose it later. | ||||||||||||
7554 | if (!supportsVariadicCall(CC)) { | ||||||||||||
7555 | const FunctionProtoType *FnP = dyn_cast<FunctionProtoType>(fn); | ||||||||||||
7556 | if (FnP && FnP->isVariadic()) { | ||||||||||||
7557 | // stdcall and fastcall are ignored with a warning for GCC and MS | ||||||||||||
7558 | // compatibility. | ||||||||||||
7559 | if (CC == CC_X86StdCall || CC == CC_X86FastCall) | ||||||||||||
7560 | return S.Diag(attr.getLoc(), diag::warn_cconv_unsupported) | ||||||||||||
7561 | << FunctionType::getNameForCallConv(CC) | ||||||||||||
7562 | << (int)Sema::CallingConventionIgnoredReason::VariadicFunction; | ||||||||||||
7563 | |||||||||||||
7564 | attr.setInvalid(); | ||||||||||||
7565 | return S.Diag(attr.getLoc(), diag::err_cconv_varargs) | ||||||||||||
7566 | << FunctionType::getNameForCallConv(CC); | ||||||||||||
7567 | } | ||||||||||||
7568 | } | ||||||||||||
7569 | |||||||||||||
7570 | // Also diagnose fastcall with regparm. | ||||||||||||
7571 | if (CC == CC_X86FastCall && fn->getHasRegParm()) { | ||||||||||||
7572 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) | ||||||||||||
7573 | << "regparm" << FunctionType::getNameForCallConv(CC_X86FastCall); | ||||||||||||
7574 | attr.setInvalid(); | ||||||||||||
7575 | return true; | ||||||||||||
7576 | } | ||||||||||||
7577 | |||||||||||||
7578 | // Modify the CC from the wrapped function type, wrap it all back, and then | ||||||||||||
7579 | // wrap the whole thing in an AttributedType as written. The modified type | ||||||||||||
7580 | // might have a different CC if we ignored the attribute. | ||||||||||||
7581 | QualType Equivalent; | ||||||||||||
7582 | if (CCOld == CC) { | ||||||||||||
7583 | Equivalent = type; | ||||||||||||
7584 | } else { | ||||||||||||
7585 | auto EI = unwrapped.get()->getExtInfo().withCallingConv(CC); | ||||||||||||
7586 | Equivalent = | ||||||||||||
7587 | unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); | ||||||||||||
7588 | } | ||||||||||||
7589 | type = state.getAttributedType(CCAttr, type, Equivalent); | ||||||||||||
7590 | return true; | ||||||||||||
7591 | } | ||||||||||||
7592 | |||||||||||||
7593 | bool Sema::hasExplicitCallingConv(QualType T) { | ||||||||||||
7594 | const AttributedType *AT; | ||||||||||||
7595 | |||||||||||||
7596 | // Stop if we'd be stripping off a typedef sugar node to reach the | ||||||||||||
7597 | // AttributedType. | ||||||||||||
7598 | while ((AT = T->getAs<AttributedType>()) && | ||||||||||||
7599 | AT->getAs<TypedefType>() == T->getAs<TypedefType>()) { | ||||||||||||
7600 | if (AT->isCallingConv()) | ||||||||||||
7601 | return true; | ||||||||||||
7602 | T = AT->getModifiedType(); | ||||||||||||
7603 | } | ||||||||||||
7604 | return false; | ||||||||||||
7605 | } | ||||||||||||
7606 | |||||||||||||
7607 | void Sema::adjustMemberFunctionCC(QualType &T, bool IsStatic, bool IsCtorOrDtor, | ||||||||||||
7608 | SourceLocation Loc) { | ||||||||||||
7609 | FunctionTypeUnwrapper Unwrapped(*this, T); | ||||||||||||
7610 | const FunctionType *FT = Unwrapped.get(); | ||||||||||||
7611 | bool IsVariadic = (isa<FunctionProtoType>(FT) && | ||||||||||||
7612 | cast<FunctionProtoType>(FT)->isVariadic()); | ||||||||||||
7613 | CallingConv CurCC = FT->getCallConv(); | ||||||||||||
7614 | CallingConv ToCC = Context.getDefaultCallingConvention(IsVariadic, !IsStatic); | ||||||||||||
7615 | |||||||||||||
7616 | if (CurCC == ToCC) | ||||||||||||
7617 | return; | ||||||||||||
7618 | |||||||||||||
7619 | // MS compiler ignores explicit calling convention attributes on structors. We | ||||||||||||
7620 | // should do the same. | ||||||||||||
7621 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && IsCtorOrDtor) { | ||||||||||||
7622 | // Issue a warning on ignored calling convention -- except of __stdcall. | ||||||||||||
7623 | // Again, this is what MS compiler does. | ||||||||||||
7624 | if (CurCC != CC_X86StdCall) | ||||||||||||
7625 | Diag(Loc, diag::warn_cconv_unsupported) | ||||||||||||
7626 | << FunctionType::getNameForCallConv(CurCC) | ||||||||||||
7627 | << (int)Sema::CallingConventionIgnoredReason::ConstructorDestructor; | ||||||||||||
7628 | // Default adjustment. | ||||||||||||
7629 | } else { | ||||||||||||
7630 | // Only adjust types with the default convention. For example, on Windows | ||||||||||||
7631 | // we should adjust a __cdecl type to __thiscall for instance methods, and a | ||||||||||||
7632 | // __thiscall type to __cdecl for static methods. | ||||||||||||
7633 | CallingConv DefaultCC = | ||||||||||||
7634 | Context.getDefaultCallingConvention(IsVariadic, IsStatic); | ||||||||||||
7635 | |||||||||||||
7636 | if (CurCC != DefaultCC || DefaultCC == ToCC) | ||||||||||||
7637 | return; | ||||||||||||
7638 | |||||||||||||
7639 | if (hasExplicitCallingConv(T)) | ||||||||||||
7640 | return; | ||||||||||||
7641 | } | ||||||||||||
7642 | |||||||||||||
7643 | FT = Context.adjustFunctionType(FT, FT->getExtInfo().withCallingConv(ToCC)); | ||||||||||||
7644 | QualType Wrapped = Unwrapped.wrap(*this, FT); | ||||||||||||
7645 | T = Context.getAdjustedType(T, Wrapped); | ||||||||||||
7646 | } | ||||||||||||
7647 | |||||||||||||
7648 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral | ||||||||||||
7649 | /// and float scalars, although arrays, pointers, and function return values are | ||||||||||||
7650 | /// allowed in conjunction with this construct. Aggregates with this attribute | ||||||||||||
7651 | /// are invalid, even if they are of the same size as a corresponding scalar. | ||||||||||||
7652 | /// The raw attribute should contain precisely 1 argument, the vector size for | ||||||||||||
7653 | /// the variable, measured in bytes. If curType and rawAttr are well formed, | ||||||||||||
7654 | /// this routine will return a new vector type. | ||||||||||||
7655 | static void HandleVectorSizeAttr(QualType &CurType, const ParsedAttr &Attr, | ||||||||||||
7656 | Sema &S) { | ||||||||||||
7657 | // Check the attribute arguments. | ||||||||||||
7658 | if (Attr.getNumArgs() != 1) { | ||||||||||||
7659 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Attr | ||||||||||||
7660 | << 1; | ||||||||||||
7661 | Attr.setInvalid(); | ||||||||||||
7662 | return; | ||||||||||||
7663 | } | ||||||||||||
7664 | |||||||||||||
7665 | Expr *SizeExpr = Attr.getArgAsExpr(0); | ||||||||||||
7666 | QualType T = S.BuildVectorType(CurType, SizeExpr, Attr.getLoc()); | ||||||||||||
7667 | if (!T.isNull()) | ||||||||||||
7668 | CurType = T; | ||||||||||||
7669 | else | ||||||||||||
7670 | Attr.setInvalid(); | ||||||||||||
7671 | } | ||||||||||||
7672 | |||||||||||||
7673 | /// Process the OpenCL-like ext_vector_type attribute when it occurs on | ||||||||||||
7674 | /// a type. | ||||||||||||
7675 | static void HandleExtVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr, | ||||||||||||
7676 | Sema &S) { | ||||||||||||
7677 | // check the attribute arguments. | ||||||||||||
7678 | if (Attr.getNumArgs() != 1) { | ||||||||||||
7679 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Attr | ||||||||||||
7680 | << 1; | ||||||||||||
7681 | return; | ||||||||||||
7682 | } | ||||||||||||
7683 | |||||||||||||
7684 | Expr *SizeExpr = Attr.getArgAsExpr(0); | ||||||||||||
7685 | QualType T = S.BuildExtVectorType(CurType, SizeExpr, Attr.getLoc()); | ||||||||||||
7686 | if (!T.isNull()) | ||||||||||||
7687 | CurType = T; | ||||||||||||
7688 | } | ||||||||||||
7689 | |||||||||||||
7690 | static bool isPermittedNeonBaseType(QualType &Ty, | ||||||||||||
7691 | VectorType::VectorKind VecKind, Sema &S) { | ||||||||||||
7692 | const BuiltinType *BTy = Ty->getAs<BuiltinType>(); | ||||||||||||
7693 | if (!BTy) | ||||||||||||
7694 | return false; | ||||||||||||
7695 | |||||||||||||
7696 | llvm::Triple Triple = S.Context.getTargetInfo().getTriple(); | ||||||||||||
7697 | |||||||||||||
7698 | // Signed poly is mathematically wrong, but has been baked into some ABIs by | ||||||||||||
7699 | // now. | ||||||||||||
7700 | bool IsPolyUnsigned = Triple.getArch() == llvm::Triple::aarch64 || | ||||||||||||
7701 | Triple.getArch() == llvm::Triple::aarch64_32 || | ||||||||||||
7702 | Triple.getArch() == llvm::Triple::aarch64_be; | ||||||||||||
7703 | if (VecKind == VectorType::NeonPolyVector) { | ||||||||||||
7704 | if (IsPolyUnsigned) { | ||||||||||||
7705 | // AArch64 polynomial vectors are unsigned. | ||||||||||||
7706 | return BTy->getKind() == BuiltinType::UChar || | ||||||||||||
7707 | BTy->getKind() == BuiltinType::UShort || | ||||||||||||
7708 | BTy->getKind() == BuiltinType::ULong || | ||||||||||||
7709 | BTy->getKind() == BuiltinType::ULongLong; | ||||||||||||
7710 | } else { | ||||||||||||
7711 | // AArch32 polynomial vectors are signed. | ||||||||||||
7712 | return BTy->getKind() == BuiltinType::SChar || | ||||||||||||
7713 | BTy->getKind() == BuiltinType::Short || | ||||||||||||
7714 | BTy->getKind() == BuiltinType::LongLong; | ||||||||||||
7715 | } | ||||||||||||
7716 | } | ||||||||||||
7717 | |||||||||||||
7718 | // Non-polynomial vector types: the usual suspects are allowed, as well as | ||||||||||||
7719 | // float64_t on AArch64. | ||||||||||||
7720 | if ((Triple.isArch64Bit() || Triple.getArch() == llvm::Triple::aarch64_32) && | ||||||||||||
7721 | BTy->getKind() == BuiltinType::Double) | ||||||||||||
7722 | return true; | ||||||||||||
7723 | |||||||||||||
7724 | return BTy->getKind() == BuiltinType::SChar || | ||||||||||||
7725 | BTy->getKind() == BuiltinType::UChar || | ||||||||||||
7726 | BTy->getKind() == BuiltinType::Short || | ||||||||||||
7727 | BTy->getKind() == BuiltinType::UShort || | ||||||||||||
7728 | BTy->getKind() == BuiltinType::Int || | ||||||||||||
7729 | BTy->getKind() == BuiltinType::UInt || | ||||||||||||
7730 | BTy->getKind() == BuiltinType::Long || | ||||||||||||
7731 | BTy->getKind() == BuiltinType::ULong || | ||||||||||||
7732 | BTy->getKind() == BuiltinType::LongLong || | ||||||||||||
7733 | BTy->getKind() == BuiltinType::ULongLong || | ||||||||||||
7734 | BTy->getKind() == BuiltinType::Float || | ||||||||||||
7735 | BTy->getKind() == BuiltinType::Half || | ||||||||||||
7736 | BTy->getKind() == BuiltinType::BFloat16; | ||||||||||||
7737 | } | ||||||||||||
7738 | |||||||||||||
7739 | static bool verifyValidIntegerConstantExpr(Sema &S, const ParsedAttr &Attr, | ||||||||||||
7740 | llvm::APSInt &Result) { | ||||||||||||
7741 | const auto *AttrExpr = Attr.getArgAsExpr(0); | ||||||||||||
7742 | if (!AttrExpr->isTypeDependent() && !AttrExpr->isValueDependent()) { | ||||||||||||
7743 | if (Optional<llvm::APSInt> Res = | ||||||||||||
7744 | AttrExpr->getIntegerConstantExpr(S.Context)) { | ||||||||||||
7745 | Result = *Res; | ||||||||||||
7746 | return true; | ||||||||||||
7747 | } | ||||||||||||
7748 | } | ||||||||||||
7749 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) | ||||||||||||
7750 | << Attr << AANT_ArgumentIntegerConstant << AttrExpr->getSourceRange(); | ||||||||||||
7751 | Attr.setInvalid(); | ||||||||||||
7752 | return false; | ||||||||||||
7753 | } | ||||||||||||
7754 | |||||||||||||
7755 | /// HandleNeonVectorTypeAttr - The "neon_vector_type" and | ||||||||||||
7756 | /// "neon_polyvector_type" attributes are used to create vector types that | ||||||||||||
7757 | /// are mangled according to ARM's ABI. Otherwise, these types are identical | ||||||||||||
7758 | /// to those created with the "vector_size" attribute. Unlike "vector_size" | ||||||||||||
7759 | /// the argument to these Neon attributes is the number of vector elements, | ||||||||||||
7760 | /// not the vector size in bytes. The vector width and element type must | ||||||||||||
7761 | /// match one of the standard Neon vector types. | ||||||||||||
7762 | static void HandleNeonVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr, | ||||||||||||
7763 | Sema &S, VectorType::VectorKind VecKind) { | ||||||||||||
7764 | // Target must have NEON (or MVE, whose vectors are similar enough | ||||||||||||
7765 | // not to need a separate attribute) | ||||||||||||
7766 | if (!S.Context.getTargetInfo().hasFeature("neon") && | ||||||||||||
7767 | !S.Context.getTargetInfo().hasFeature("mve")) { | ||||||||||||
7768 | S.Diag(Attr.getLoc(), diag::err_attribute_unsupported) | ||||||||||||
7769 | << Attr << "'neon' or 'mve'"; | ||||||||||||
7770 | Attr.setInvalid(); | ||||||||||||
7771 | return; | ||||||||||||
7772 | } | ||||||||||||
7773 | // Check the attribute arguments. | ||||||||||||
7774 | if (Attr.getNumArgs() != 1) { | ||||||||||||
7775 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Attr | ||||||||||||
7776 | << 1; | ||||||||||||
7777 | Attr.setInvalid(); | ||||||||||||
7778 | return; | ||||||||||||
7779 | } | ||||||||||||
7780 | // The number of elements must be an ICE. | ||||||||||||
7781 | llvm::APSInt numEltsInt(32); | ||||||||||||
7782 | if (!verifyValidIntegerConstantExpr(S, Attr, numEltsInt)) | ||||||||||||
7783 | return; | ||||||||||||
7784 | |||||||||||||
7785 | // Only certain element types are supported for Neon vectors. | ||||||||||||
7786 | if (!isPermittedNeonBaseType(CurType, VecKind, S)) { | ||||||||||||
7787 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; | ||||||||||||
7788 | Attr.setInvalid(); | ||||||||||||
7789 | return; | ||||||||||||
7790 | } | ||||||||||||
7791 | |||||||||||||
7792 | // The total size of the vector must be 64 or 128 bits. | ||||||||||||
7793 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); | ||||||||||||
7794 | unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue()); | ||||||||||||
7795 | unsigned vecSize = typeSize * numElts; | ||||||||||||
7796 | if (vecSize != 64 && vecSize != 128) { | ||||||||||||
7797 | S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType; | ||||||||||||
7798 | Attr.setInvalid(); | ||||||||||||
7799 | return; | ||||||||||||
7800 | } | ||||||||||||
7801 | |||||||||||||
7802 | CurType = S.Context.getVectorType(CurType, numElts, VecKind); | ||||||||||||
7803 | } | ||||||||||||
7804 | |||||||||||||
7805 | /// HandleArmSveVectorBitsTypeAttr - The "arm_sve_vector_bits" attribute is | ||||||||||||
7806 | /// used to create fixed-length versions of sizeless SVE types defined by | ||||||||||||
7807 | /// the ACLE, such as svint32_t and svbool_t. | ||||||||||||
7808 | static void HandleArmSveVectorBitsTypeAttr(QualType &CurType, ParsedAttr &Attr, | ||||||||||||
7809 | Sema &S) { | ||||||||||||
7810 | // Target must have SVE. | ||||||||||||
7811 | if (!S.Context.getTargetInfo().hasFeature("sve")) { | ||||||||||||
7812 | S.Diag(Attr.getLoc(), diag::err_attribute_unsupported) << Attr << "'sve'"; | ||||||||||||
7813 | Attr.setInvalid(); | ||||||||||||
7814 | return; | ||||||||||||
7815 | } | ||||||||||||
7816 | |||||||||||||
7817 | // Attribute is unsupported if '-msve-vector-bits=<bits>' isn't specified. | ||||||||||||
7818 | if (!S.getLangOpts().ArmSveVectorBits) { | ||||||||||||
7819 | S.Diag(Attr.getLoc(), diag::err_attribute_arm_feature_sve_bits_unsupported) | ||||||||||||
7820 | << Attr; | ||||||||||||
7821 | Attr.setInvalid(); | ||||||||||||
7822 | return; | ||||||||||||
7823 | } | ||||||||||||
7824 | |||||||||||||
7825 | // Check the attribute arguments. | ||||||||||||
7826 | if (Attr.getNumArgs() != 1) { | ||||||||||||
7827 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) | ||||||||||||
7828 | << Attr << 1; | ||||||||||||
7829 | Attr.setInvalid(); | ||||||||||||
7830 | return; | ||||||||||||
7831 | } | ||||||||||||
7832 | |||||||||||||
7833 | // The vector size must be an integer constant expression. | ||||||||||||
7834 | llvm::APSInt SveVectorSizeInBits(32); | ||||||||||||
7835 | if (!verifyValidIntegerConstantExpr(S, Attr, SveVectorSizeInBits)) | ||||||||||||
7836 | return; | ||||||||||||
7837 | |||||||||||||
7838 | unsigned VecSize = static_cast<unsigned>(SveVectorSizeInBits.getZExtValue()); | ||||||||||||
7839 | |||||||||||||
7840 | // The attribute vector size must match -msve-vector-bits. | ||||||||||||
7841 | if (VecSize != S.getLangOpts().ArmSveVectorBits) { | ||||||||||||
7842 | S.Diag(Attr.getLoc(), diag::err_attribute_bad_sve_vector_size) | ||||||||||||
7843 | << VecSize << S.getLangOpts().ArmSveVectorBits; | ||||||||||||
7844 | Attr.setInvalid(); | ||||||||||||
7845 | return; | ||||||||||||
7846 | } | ||||||||||||
7847 | |||||||||||||
7848 | // Attribute can only be attached to a single SVE vector or predicate type. | ||||||||||||
7849 | if (!CurType->isVLSTBuiltinType()) { | ||||||||||||
7850 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_sve_type) | ||||||||||||
7851 | << Attr << CurType; | ||||||||||||
7852 | Attr.setInvalid(); | ||||||||||||
7853 | return; | ||||||||||||
7854 | } | ||||||||||||
7855 | |||||||||||||
7856 | const auto *BT = CurType->castAs<BuiltinType>(); | ||||||||||||
7857 | |||||||||||||
7858 | QualType EltType = CurType->getSveEltType(S.Context); | ||||||||||||
7859 | unsigned TypeSize = S.Context.getTypeSize(EltType); | ||||||||||||
7860 | VectorType::VectorKind VecKind = VectorType::SveFixedLengthDataVector; | ||||||||||||
7861 | if (BT->getKind() == BuiltinType::SveBool) { | ||||||||||||
7862 | // Predicates are represented as i8. | ||||||||||||
7863 | VecSize /= S.Context.getCharWidth() * S.Context.getCharWidth(); | ||||||||||||
7864 | VecKind = VectorType::SveFixedLengthPredicateVector; | ||||||||||||
7865 | } else | ||||||||||||
7866 | VecSize /= TypeSize; | ||||||||||||
7867 | CurType = S.Context.getVectorType(EltType, VecSize, VecKind); | ||||||||||||
7868 | } | ||||||||||||
7869 | |||||||||||||
7870 | static void HandleArmMveStrictPolymorphismAttr(TypeProcessingState &State, | ||||||||||||
7871 | QualType &CurType, | ||||||||||||
7872 | ParsedAttr &Attr) { | ||||||||||||
7873 | const VectorType *VT = dyn_cast<VectorType>(CurType); | ||||||||||||
7874 | if (!VT || VT->getVectorKind() != VectorType::NeonVector) { | ||||||||||||
7875 | State.getSema().Diag(Attr.getLoc(), | ||||||||||||
7876 | diag::err_attribute_arm_mve_polymorphism); | ||||||||||||
7877 | Attr.setInvalid(); | ||||||||||||
7878 | return; | ||||||||||||
7879 | } | ||||||||||||
7880 | |||||||||||||
7881 | CurType = | ||||||||||||
7882 | State.getAttributedType(createSimpleAttr<ArmMveStrictPolymorphismAttr>( | ||||||||||||
7883 | State.getSema().Context, Attr), | ||||||||||||
7884 | CurType, CurType); | ||||||||||||
7885 | } | ||||||||||||
7886 | |||||||||||||
7887 | /// Handle OpenCL Access Qualifier Attribute. | ||||||||||||
7888 | static void HandleOpenCLAccessAttr(QualType &CurType, const ParsedAttr &Attr, | ||||||||||||
7889 | Sema &S) { | ||||||||||||
7890 | // OpenCL v2.0 s6.6 - Access qualifier can be used only for image and pipe type. | ||||||||||||
7891 | if (!(CurType->isImageType() || CurType->isPipeType())) { | ||||||||||||
7892 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_access_qualifier); | ||||||||||||
7893 | Attr.setInvalid(); | ||||||||||||
7894 | return; | ||||||||||||
7895 | } | ||||||||||||
7896 | |||||||||||||
7897 | if (const TypedefType* TypedefTy = CurType->getAs<TypedefType>()) { | ||||||||||||
7898 | QualType BaseTy = TypedefTy->desugar(); | ||||||||||||
7899 | |||||||||||||
7900 | std::string PrevAccessQual; | ||||||||||||
7901 | if (BaseTy->isPipeType()) { | ||||||||||||
7902 | if (TypedefTy->getDecl()->hasAttr<OpenCLAccessAttr>()) { | ||||||||||||
7903 | OpenCLAccessAttr *Attr = | ||||||||||||
7904 | TypedefTy->getDecl()->getAttr<OpenCLAccessAttr>(); | ||||||||||||
7905 | PrevAccessQual = Attr->getSpelling(); | ||||||||||||
7906 | } else { | ||||||||||||
7907 | PrevAccessQual = "read_only"; | ||||||||||||
7908 | } | ||||||||||||
7909 | } else if (const BuiltinType* ImgType = BaseTy->getAs<BuiltinType>()) { | ||||||||||||
7910 | |||||||||||||
7911 | switch (ImgType->getKind()) { | ||||||||||||
7912 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | ||||||||||||
7913 | case BuiltinType::Id: \ | ||||||||||||
7914 | PrevAccessQual = #Access; \ | ||||||||||||
7915 | break; | ||||||||||||
7916 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||||||||
7917 | default: | ||||||||||||
7918 | llvm_unreachable("Unable to find corresponding image type.")::llvm::llvm_unreachable_internal("Unable to find corresponding image type." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7918); | ||||||||||||
7919 | } | ||||||||||||
7920 | } else { | ||||||||||||
7921 | llvm_unreachable("unexpected type")::llvm::llvm_unreachable_internal("unexpected type", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 7921); | ||||||||||||
7922 | } | ||||||||||||
7923 | StringRef AttrName = Attr.getAttrName()->getName(); | ||||||||||||
7924 | if (PrevAccessQual == AttrName.ltrim("_")) { | ||||||||||||
7925 | // Duplicated qualifiers | ||||||||||||
7926 | S.Diag(Attr.getLoc(), diag::warn_duplicate_declspec) | ||||||||||||
7927 | << AttrName << Attr.getRange(); | ||||||||||||
7928 | } else { | ||||||||||||
7929 | // Contradicting qualifiers | ||||||||||||
7930 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers); | ||||||||||||
7931 | } | ||||||||||||
7932 | |||||||||||||
7933 | S.Diag(TypedefTy->getDecl()->getBeginLoc(), | ||||||||||||
7934 | diag::note_opencl_typedef_access_qualifier) << PrevAccessQual; | ||||||||||||
7935 | } else if (CurType->isPipeType()) { | ||||||||||||
7936 | if (Attr.getSemanticSpelling() == OpenCLAccessAttr::Keyword_write_only) { | ||||||||||||
7937 | QualType ElemType = CurType->castAs<PipeType>()->getElementType(); | ||||||||||||
7938 | CurType = S.Context.getWritePipeType(ElemType); | ||||||||||||
7939 | } | ||||||||||||
7940 | } | ||||||||||||
7941 | } | ||||||||||||
7942 | |||||||||||||
7943 | /// HandleMatrixTypeAttr - "matrix_type" attribute, like ext_vector_type | ||||||||||||
7944 | static void HandleMatrixTypeAttr(QualType &CurType, const ParsedAttr &Attr, | ||||||||||||
7945 | Sema &S) { | ||||||||||||
7946 | if (!S.getLangOpts().MatrixTypes) { | ||||||||||||
7947 | S.Diag(Attr.getLoc(), diag::err_builtin_matrix_disabled); | ||||||||||||
7948 | return; | ||||||||||||
7949 | } | ||||||||||||
7950 | |||||||||||||
7951 | if (Attr.getNumArgs() != 2) { | ||||||||||||
7952 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) | ||||||||||||
7953 | << Attr << 2; | ||||||||||||
7954 | return; | ||||||||||||
7955 | } | ||||||||||||
7956 | |||||||||||||
7957 | Expr *RowsExpr = Attr.getArgAsExpr(0); | ||||||||||||
7958 | Expr *ColsExpr = Attr.getArgAsExpr(1); | ||||||||||||
7959 | QualType T = S.BuildMatrixType(CurType, RowsExpr, ColsExpr, Attr.getLoc()); | ||||||||||||
7960 | if (!T.isNull()) | ||||||||||||
7961 | CurType = T; | ||||||||||||
7962 | } | ||||||||||||
7963 | |||||||||||||
7964 | static void HandleLifetimeBoundAttr(TypeProcessingState &State, | ||||||||||||
7965 | QualType &CurType, | ||||||||||||
7966 | ParsedAttr &Attr) { | ||||||||||||
7967 | if (State.getDeclarator().isDeclarationOfFunction()) { | ||||||||||||
7968 | CurType = State.getAttributedType( | ||||||||||||
7969 | createSimpleAttr<LifetimeBoundAttr>(State.getSema().Context, Attr), | ||||||||||||
7970 | CurType, CurType); | ||||||||||||
7971 | } | ||||||||||||
7972 | } | ||||||||||||
7973 | |||||||||||||
7974 | static bool isAddressSpaceKind(const ParsedAttr &attr) { | ||||||||||||
7975 | auto attrKind = attr.getKind(); | ||||||||||||
7976 | |||||||||||||
7977 | return attrKind == ParsedAttr::AT_AddressSpace || | ||||||||||||
7978 | attrKind == ParsedAttr::AT_OpenCLPrivateAddressSpace || | ||||||||||||
7979 | attrKind == ParsedAttr::AT_OpenCLGlobalAddressSpace || | ||||||||||||
7980 | attrKind == ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace || | ||||||||||||
7981 | attrKind == ParsedAttr::AT_OpenCLGlobalHostAddressSpace || | ||||||||||||
7982 | attrKind == ParsedAttr::AT_OpenCLLocalAddressSpace || | ||||||||||||
7983 | attrKind == ParsedAttr::AT_OpenCLConstantAddressSpace || | ||||||||||||
7984 | attrKind == ParsedAttr::AT_OpenCLGenericAddressSpace; | ||||||||||||
7985 | } | ||||||||||||
7986 | |||||||||||||
7987 | static void processTypeAttrs(TypeProcessingState &state, QualType &type, | ||||||||||||
7988 | TypeAttrLocation TAL, | ||||||||||||
7989 | ParsedAttributesView &attrs) { | ||||||||||||
7990 | // Scan through and apply attributes to this type where it makes sense. Some | ||||||||||||
7991 | // attributes (such as __address_space__, __vector_size__, etc) apply to the | ||||||||||||
7992 | // type, but others can be present in the type specifiers even though they | ||||||||||||
7993 | // apply to the decl. Here we apply type attributes and ignore the rest. | ||||||||||||
7994 | |||||||||||||
7995 | // This loop modifies the list pretty frequently, but we still need to make | ||||||||||||
7996 | // sure we visit every element once. Copy the attributes list, and iterate | ||||||||||||
7997 | // over that. | ||||||||||||
7998 | ParsedAttributesView AttrsCopy{attrs}; | ||||||||||||
7999 | |||||||||||||
8000 | state.setParsedNoDeref(false); | ||||||||||||
8001 | |||||||||||||
8002 | for (ParsedAttr &attr : AttrsCopy) { | ||||||||||||
8003 | |||||||||||||
8004 | // Skip attributes that were marked to be invalid. | ||||||||||||
8005 | if (attr.isInvalid()) | ||||||||||||
8006 | continue; | ||||||||||||
8007 | |||||||||||||
8008 | if (attr.isCXX11Attribute()) { | ||||||||||||
8009 | // [[gnu::...]] attributes are treated as declaration attributes, so may | ||||||||||||
8010 | // not appertain to a DeclaratorChunk. If we handle them as type | ||||||||||||
8011 | // attributes, accept them in that position and diagnose the GCC | ||||||||||||
8012 | // incompatibility. | ||||||||||||
8013 | if (attr.isGNUScope()) { | ||||||||||||
8014 | bool IsTypeAttr = attr.isTypeAttr(); | ||||||||||||
8015 | if (TAL == TAL_DeclChunk) { | ||||||||||||
8016 | state.getSema().Diag(attr.getLoc(), | ||||||||||||
8017 | IsTypeAttr | ||||||||||||
8018 | ? diag::warn_gcc_ignores_type_attr | ||||||||||||
8019 | : diag::warn_cxx11_gnu_attribute_on_type) | ||||||||||||
8020 | << attr; | ||||||||||||
8021 | if (!IsTypeAttr) | ||||||||||||
8022 | continue; | ||||||||||||
8023 | } | ||||||||||||
8024 | } else if (TAL != TAL_DeclChunk && !isAddressSpaceKind(attr)) { | ||||||||||||
8025 | // Otherwise, only consider type processing for a C++11 attribute if | ||||||||||||
8026 | // it's actually been applied to a type. | ||||||||||||
8027 | // We also allow C++11 address_space and | ||||||||||||
8028 | // OpenCL language address space attributes to pass through. | ||||||||||||
8029 | continue; | ||||||||||||
8030 | } | ||||||||||||
8031 | } | ||||||||||||
8032 | |||||||||||||
8033 | // If this is an attribute we can handle, do so now, | ||||||||||||
8034 | // otherwise, add it to the FnAttrs list for rechaining. | ||||||||||||
8035 | switch (attr.getKind()) { | ||||||||||||
8036 | default: | ||||||||||||
8037 | // A C++11 attribute on a declarator chunk must appertain to a type. | ||||||||||||
8038 | if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) { | ||||||||||||
8039 | state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr) | ||||||||||||
8040 | << attr; | ||||||||||||
8041 | attr.setUsedAsTypeAttr(); | ||||||||||||
8042 | } | ||||||||||||
8043 | break; | ||||||||||||
8044 | |||||||||||||
8045 | case ParsedAttr::UnknownAttribute: | ||||||||||||
8046 | if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) | ||||||||||||
8047 | state.getSema().Diag(attr.getLoc(), | ||||||||||||
8048 | diag::warn_unknown_attribute_ignored) | ||||||||||||
8049 | << attr << attr.getRange(); | ||||||||||||
8050 | break; | ||||||||||||
8051 | |||||||||||||
8052 | case ParsedAttr::IgnoredAttribute: | ||||||||||||
8053 | break; | ||||||||||||
8054 | |||||||||||||
8055 | case ParsedAttr::AT_MayAlias: | ||||||||||||
8056 | // FIXME: This attribute needs to actually be handled, but if we ignore | ||||||||||||
8057 | // it it breaks large amounts of Linux software. | ||||||||||||
8058 | attr.setUsedAsTypeAttr(); | ||||||||||||
8059 | break; | ||||||||||||
8060 | case ParsedAttr::AT_OpenCLPrivateAddressSpace: | ||||||||||||
8061 | case ParsedAttr::AT_OpenCLGlobalAddressSpace: | ||||||||||||
8062 | case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace: | ||||||||||||
8063 | case ParsedAttr::AT_OpenCLGlobalHostAddressSpace: | ||||||||||||
8064 | case ParsedAttr::AT_OpenCLLocalAddressSpace: | ||||||||||||
8065 | case ParsedAttr::AT_OpenCLConstantAddressSpace: | ||||||||||||
8066 | case ParsedAttr::AT_OpenCLGenericAddressSpace: | ||||||||||||
8067 | case ParsedAttr::AT_AddressSpace: | ||||||||||||
8068 | HandleAddressSpaceTypeAttribute(type, attr, state); | ||||||||||||
8069 | attr.setUsedAsTypeAttr(); | ||||||||||||
8070 | break; | ||||||||||||
8071 | OBJC_POINTER_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_ObjCGC: case ParsedAttr::AT_ObjCOwnership: | ||||||||||||
8072 | if (!handleObjCPointerTypeAttr(state, attr, type)) | ||||||||||||
8073 | distributeObjCPointerTypeAttr(state, attr, type); | ||||||||||||
8074 | attr.setUsedAsTypeAttr(); | ||||||||||||
8075 | break; | ||||||||||||
8076 | case ParsedAttr::AT_VectorSize: | ||||||||||||
8077 | HandleVectorSizeAttr(type, attr, state.getSema()); | ||||||||||||
8078 | attr.setUsedAsTypeAttr(); | ||||||||||||
8079 | break; | ||||||||||||
8080 | case ParsedAttr::AT_ExtVectorType: | ||||||||||||
8081 | HandleExtVectorTypeAttr(type, attr, state.getSema()); | ||||||||||||
8082 | attr.setUsedAsTypeAttr(); | ||||||||||||
8083 | break; | ||||||||||||
8084 | case ParsedAttr::AT_NeonVectorType: | ||||||||||||
8085 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), | ||||||||||||
8086 | VectorType::NeonVector); | ||||||||||||
8087 | attr.setUsedAsTypeAttr(); | ||||||||||||
8088 | break; | ||||||||||||
8089 | case ParsedAttr::AT_NeonPolyVectorType: | ||||||||||||
8090 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), | ||||||||||||
8091 | VectorType::NeonPolyVector); | ||||||||||||
8092 | attr.setUsedAsTypeAttr(); | ||||||||||||
8093 | break; | ||||||||||||
8094 | case ParsedAttr::AT_ArmSveVectorBits: | ||||||||||||
8095 | HandleArmSveVectorBitsTypeAttr(type, attr, state.getSema()); | ||||||||||||
8096 | attr.setUsedAsTypeAttr(); | ||||||||||||
8097 | break; | ||||||||||||
8098 | case ParsedAttr::AT_ArmMveStrictPolymorphism: { | ||||||||||||
8099 | HandleArmMveStrictPolymorphismAttr(state, type, attr); | ||||||||||||
8100 | attr.setUsedAsTypeAttr(); | ||||||||||||
8101 | break; | ||||||||||||
8102 | } | ||||||||||||
8103 | case ParsedAttr::AT_OpenCLAccess: | ||||||||||||
8104 | HandleOpenCLAccessAttr(type, attr, state.getSema()); | ||||||||||||
8105 | attr.setUsedAsTypeAttr(); | ||||||||||||
8106 | break; | ||||||||||||
8107 | case ParsedAttr::AT_LifetimeBound: | ||||||||||||
8108 | if (TAL == TAL_DeclChunk) | ||||||||||||
8109 | HandleLifetimeBoundAttr(state, type, attr); | ||||||||||||
8110 | break; | ||||||||||||
8111 | |||||||||||||
8112 | case ParsedAttr::AT_NoDeref: { | ||||||||||||
8113 | ASTContext &Ctx = state.getSema().Context; | ||||||||||||
8114 | type = state.getAttributedType(createSimpleAttr<NoDerefAttr>(Ctx, attr), | ||||||||||||
8115 | type, type); | ||||||||||||
8116 | attr.setUsedAsTypeAttr(); | ||||||||||||
8117 | state.setParsedNoDeref(true); | ||||||||||||
8118 | break; | ||||||||||||
8119 | } | ||||||||||||
8120 | |||||||||||||
8121 | case ParsedAttr::AT_MatrixType: | ||||||||||||
8122 | HandleMatrixTypeAttr(type, attr, state.getSema()); | ||||||||||||
8123 | attr.setUsedAsTypeAttr(); | ||||||||||||
8124 | break; | ||||||||||||
8125 | |||||||||||||
8126 | MS_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_Ptr32: case ParsedAttr::AT_Ptr64: case ParsedAttr ::AT_SPtr: case ParsedAttr::AT_UPtr: | ||||||||||||
8127 | if (!handleMSPointerTypeQualifierAttr(state, attr, type)) | ||||||||||||
8128 | attr.setUsedAsTypeAttr(); | ||||||||||||
8129 | break; | ||||||||||||
8130 | |||||||||||||
8131 | |||||||||||||
8132 | NULLABILITY_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_TypeNonNull: case ParsedAttr::AT_TypeNullable : case ParsedAttr::AT_TypeNullableResult: case ParsedAttr::AT_TypeNullUnspecified: | ||||||||||||
8133 | // Either add nullability here or try to distribute it. We | ||||||||||||
8134 | // don't want to distribute the nullability specifier past any | ||||||||||||
8135 | // dependent type, because that complicates the user model. | ||||||||||||
8136 | if (type->canHaveNullability() || type->isDependentType() || | ||||||||||||
8137 | type->isArrayType() || | ||||||||||||
8138 | !distributeNullabilityTypeAttr(state, type, attr)) { | ||||||||||||
8139 | unsigned endIndex; | ||||||||||||
8140 | if (TAL == TAL_DeclChunk) | ||||||||||||
8141 | endIndex = state.getCurrentChunkIndex(); | ||||||||||||
8142 | else | ||||||||||||
8143 | endIndex = state.getDeclarator().getNumTypeObjects(); | ||||||||||||
8144 | bool allowOnArrayType = | ||||||||||||
8145 | state.getDeclarator().isPrototypeContext() && | ||||||||||||
8146 | !hasOuterPointerLikeChunk(state.getDeclarator(), endIndex); | ||||||||||||
8147 | if (checkNullabilityTypeSpecifier( | ||||||||||||
8148 | state, | ||||||||||||
8149 | type, | ||||||||||||
8150 | attr, | ||||||||||||
8151 | allowOnArrayType)) { | ||||||||||||
8152 | attr.setInvalid(); | ||||||||||||
8153 | } | ||||||||||||
8154 | |||||||||||||
8155 | attr.setUsedAsTypeAttr(); | ||||||||||||
8156 | } | ||||||||||||
8157 | break; | ||||||||||||
8158 | |||||||||||||
8159 | case ParsedAttr::AT_ObjCKindOf: | ||||||||||||
8160 | // '__kindof' must be part of the decl-specifiers. | ||||||||||||
8161 | switch (TAL) { | ||||||||||||
8162 | case TAL_DeclSpec: | ||||||||||||
8163 | break; | ||||||||||||
8164 | |||||||||||||
8165 | case TAL_DeclChunk: | ||||||||||||
8166 | case TAL_DeclName: | ||||||||||||
8167 | state.getSema().Diag(attr.getLoc(), | ||||||||||||
8168 | diag::err_objc_kindof_wrong_position) | ||||||||||||
8169 | << FixItHint::CreateRemoval(attr.getLoc()) | ||||||||||||
8170 | << FixItHint::CreateInsertion( | ||||||||||||
8171 | state.getDeclarator().getDeclSpec().getBeginLoc(), | ||||||||||||
8172 | "__kindof "); | ||||||||||||
8173 | break; | ||||||||||||
8174 | } | ||||||||||||
8175 | |||||||||||||
8176 | // Apply it regardless. | ||||||||||||
8177 | if (checkObjCKindOfType(state, type, attr)) | ||||||||||||
8178 | attr.setInvalid(); | ||||||||||||
8179 | break; | ||||||||||||
8180 | |||||||||||||
8181 | case ParsedAttr::AT_NoThrow: | ||||||||||||
8182 | // Exception Specifications aren't generally supported in C mode throughout | ||||||||||||
8183 | // clang, so revert to attribute-based handling for C. | ||||||||||||
8184 | if (!state.getSema().getLangOpts().CPlusPlus) | ||||||||||||
8185 | break; | ||||||||||||
8186 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||||
8187 | FUNCTION_TYPE_ATTRS_CASELISTcase ParsedAttr::AT_NSReturnsRetained: case ParsedAttr::AT_NoReturn : case ParsedAttr::AT_Regparm: case ParsedAttr::AT_CmseNSCall : case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: case ParsedAttr ::AT_AnyX86NoCfCheck: case ParsedAttr::AT_CDecl: case ParsedAttr ::AT_FastCall: case ParsedAttr::AT_StdCall: case ParsedAttr:: AT_ThisCall: case ParsedAttr::AT_RegCall: case ParsedAttr::AT_Pascal : case ParsedAttr::AT_SwiftCall: case ParsedAttr::AT_VectorCall : case ParsedAttr::AT_AArch64VectorPcs: case ParsedAttr::AT_MSABI : case ParsedAttr::AT_SysVABI: case ParsedAttr::AT_Pcs: case ParsedAttr ::AT_IntelOclBicc: case ParsedAttr::AT_PreserveMost: case ParsedAttr ::AT_PreserveAll: | ||||||||||||
8188 | attr.setUsedAsTypeAttr(); | ||||||||||||
8189 | |||||||||||||
8190 | // Never process function type attributes as part of the | ||||||||||||
8191 | // declaration-specifiers. | ||||||||||||
8192 | if (TAL == TAL_DeclSpec) | ||||||||||||
8193 | distributeFunctionTypeAttrFromDeclSpec(state, attr, type); | ||||||||||||
8194 | |||||||||||||
8195 | // Otherwise, handle the possible delays. | ||||||||||||
8196 | else if (!handleFunctionTypeAttr(state, attr, type)) | ||||||||||||
8197 | distributeFunctionTypeAttr(state, attr, type); | ||||||||||||
8198 | break; | ||||||||||||
8199 | case ParsedAttr::AT_AcquireHandle: { | ||||||||||||
8200 | if (!type->isFunctionType()) | ||||||||||||
8201 | return; | ||||||||||||
8202 | |||||||||||||
8203 | if (attr.getNumArgs() != 1) { | ||||||||||||
8204 | state.getSema().Diag(attr.getLoc(), | ||||||||||||
8205 | diag::err_attribute_wrong_number_arguments) | ||||||||||||
8206 | << attr << 1; | ||||||||||||
8207 | attr.setInvalid(); | ||||||||||||
8208 | return; | ||||||||||||
8209 | } | ||||||||||||
8210 | |||||||||||||
8211 | StringRef HandleType; | ||||||||||||
8212 | if (!state.getSema().checkStringLiteralArgumentAttr(attr, 0, HandleType)) | ||||||||||||
8213 | return; | ||||||||||||
8214 | type = state.getAttributedType( | ||||||||||||
8215 | AcquireHandleAttr::Create(state.getSema().Context, HandleType, attr), | ||||||||||||
8216 | type, type); | ||||||||||||
8217 | attr.setUsedAsTypeAttr(); | ||||||||||||
8218 | break; | ||||||||||||
8219 | } | ||||||||||||
8220 | } | ||||||||||||
8221 | |||||||||||||
8222 | // Handle attributes that are defined in a macro. We do not want this to be | ||||||||||||
8223 | // applied to ObjC builtin attributes. | ||||||||||||
8224 | if (isa<AttributedType>(type) && attr.hasMacroIdentifier() && | ||||||||||||
8225 | !type.getQualifiers().hasObjCLifetime() && | ||||||||||||
8226 | !type.getQualifiers().hasObjCGCAttr() && | ||||||||||||
8227 | attr.getKind() != ParsedAttr::AT_ObjCGC && | ||||||||||||
8228 | attr.getKind() != ParsedAttr::AT_ObjCOwnership) { | ||||||||||||
8229 | const IdentifierInfo *MacroII = attr.getMacroIdentifier(); | ||||||||||||
8230 | type = state.getSema().Context.getMacroQualifiedType(type, MacroII); | ||||||||||||
8231 | state.setExpansionLocForMacroQualifiedType( | ||||||||||||
8232 | cast<MacroQualifiedType>(type.getTypePtr()), | ||||||||||||
8233 | attr.getMacroExpansionLoc()); | ||||||||||||
8234 | } | ||||||||||||
8235 | } | ||||||||||||
8236 | |||||||||||||
8237 | if (!state.getSema().getLangOpts().OpenCL || | ||||||||||||
8238 | type.getAddressSpace() != LangAS::Default) | ||||||||||||
8239 | return; | ||||||||||||
8240 | } | ||||||||||||
8241 | |||||||||||||
8242 | void Sema::completeExprArrayBound(Expr *E) { | ||||||||||||
8243 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { | ||||||||||||
8244 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { | ||||||||||||
8245 | if (isTemplateInstantiation(Var->getTemplateSpecializationKind())) { | ||||||||||||
8246 | auto *Def = Var->getDefinition(); | ||||||||||||
8247 | if (!Def) { | ||||||||||||
8248 | SourceLocation PointOfInstantiation = E->getExprLoc(); | ||||||||||||
8249 | runWithSufficientStackSpace(PointOfInstantiation, [&] { | ||||||||||||
8250 | InstantiateVariableDefinition(PointOfInstantiation, Var); | ||||||||||||
8251 | }); | ||||||||||||
8252 | Def = Var->getDefinition(); | ||||||||||||
8253 | |||||||||||||
8254 | // If we don't already have a point of instantiation, and we managed | ||||||||||||
8255 | // to instantiate a definition, this is the point of instantiation. | ||||||||||||
8256 | // Otherwise, we don't request an end-of-TU instantiation, so this is | ||||||||||||
8257 | // not a point of instantiation. | ||||||||||||
8258 | // FIXME: Is this really the right behavior? | ||||||||||||
8259 | if (Var->getPointOfInstantiation().isInvalid() && Def) { | ||||||||||||
8260 | assert(Var->getTemplateSpecializationKind() ==((Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && "explicit instantiation with no point of instantiation" ) ? static_cast<void> (0) : __assert_fail ("Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && \"explicit instantiation with no point of instantiation\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8262, __PRETTY_FUNCTION__)) | ||||||||||||
8261 | TSK_ImplicitInstantiation &&((Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && "explicit instantiation with no point of instantiation" ) ? static_cast<void> (0) : __assert_fail ("Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && \"explicit instantiation with no point of instantiation\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8262, __PRETTY_FUNCTION__)) | ||||||||||||
8262 | "explicit instantiation with no point of instantiation")((Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && "explicit instantiation with no point of instantiation" ) ? static_cast<void> (0) : __assert_fail ("Var->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && \"explicit instantiation with no point of instantiation\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8262, __PRETTY_FUNCTION__)); | ||||||||||||
8263 | Var->setTemplateSpecializationKind( | ||||||||||||
8264 | Var->getTemplateSpecializationKind(), PointOfInstantiation); | ||||||||||||
8265 | } | ||||||||||||
8266 | } | ||||||||||||
8267 | |||||||||||||
8268 | // Update the type to the definition's type both here and within the | ||||||||||||
8269 | // expression. | ||||||||||||
8270 | if (Def) { | ||||||||||||
8271 | DRE->setDecl(Def); | ||||||||||||
8272 | QualType T = Def->getType(); | ||||||||||||
8273 | DRE->setType(T); | ||||||||||||
8274 | // FIXME: Update the type on all intervening expressions. | ||||||||||||
8275 | E->setType(T); | ||||||||||||
8276 | } | ||||||||||||
8277 | |||||||||||||
8278 | // We still go on to try to complete the type independently, as it | ||||||||||||
8279 | // may also require instantiations or diagnostics if it remains | ||||||||||||
8280 | // incomplete. | ||||||||||||
8281 | } | ||||||||||||
8282 | } | ||||||||||||
8283 | } | ||||||||||||
8284 | } | ||||||||||||
8285 | |||||||||||||
8286 | QualType Sema::getCompletedType(Expr *E) { | ||||||||||||
8287 | // Incomplete array types may be completed by the initializer attached to | ||||||||||||
8288 | // their definitions. For static data members of class templates and for | ||||||||||||
8289 | // variable templates, we need to instantiate the definition to get this | ||||||||||||
8290 | // initializer and complete the type. | ||||||||||||
8291 | if (E->getType()->isIncompleteArrayType()) | ||||||||||||
8292 | completeExprArrayBound(E); | ||||||||||||
8293 | |||||||||||||
8294 | // FIXME: Are there other cases which require instantiating something other | ||||||||||||
8295 | // than the type to complete the type of an expression? | ||||||||||||
8296 | |||||||||||||
8297 | return E->getType(); | ||||||||||||
8298 | } | ||||||||||||
8299 | |||||||||||||
8300 | /// Ensure that the type of the given expression is complete. | ||||||||||||
8301 | /// | ||||||||||||
8302 | /// This routine checks whether the expression \p E has a complete type. If the | ||||||||||||
8303 | /// expression refers to an instantiable construct, that instantiation is | ||||||||||||
8304 | /// performed as needed to complete its type. Furthermore | ||||||||||||
8305 | /// Sema::RequireCompleteType is called for the expression's type (or in the | ||||||||||||
8306 | /// case of a reference type, the referred-to type). | ||||||||||||
8307 | /// | ||||||||||||
8308 | /// \param E The expression whose type is required to be complete. | ||||||||||||
8309 | /// \param Kind Selects which completeness rules should be applied. | ||||||||||||
8310 | /// \param Diagnoser The object that will emit a diagnostic if the type is | ||||||||||||
8311 | /// incomplete. | ||||||||||||
8312 | /// | ||||||||||||
8313 | /// \returns \c true if the type of \p E is incomplete and diagnosed, \c false | ||||||||||||
8314 | /// otherwise. | ||||||||||||
8315 | bool Sema::RequireCompleteExprType(Expr *E, CompleteTypeKind Kind, | ||||||||||||
8316 | TypeDiagnoser &Diagnoser) { | ||||||||||||
8317 | return RequireCompleteType(E->getExprLoc(), getCompletedType(E), Kind, | ||||||||||||
8318 | Diagnoser); | ||||||||||||
8319 | } | ||||||||||||
8320 | |||||||||||||
8321 | bool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) { | ||||||||||||
8322 | BoundTypeDiagnoser<> Diagnoser(DiagID); | ||||||||||||
8323 | return RequireCompleteExprType(E, CompleteTypeKind::Default, Diagnoser); | ||||||||||||
8324 | } | ||||||||||||
8325 | |||||||||||||
8326 | /// Ensure that the type T is a complete type. | ||||||||||||
8327 | /// | ||||||||||||
8328 | /// This routine checks whether the type @p T is complete in any | ||||||||||||
8329 | /// context where a complete type is required. If @p T is a complete | ||||||||||||
8330 | /// type, returns false. If @p T is a class template specialization, | ||||||||||||
8331 | /// this routine then attempts to perform class template | ||||||||||||
8332 | /// instantiation. If instantiation fails, or if @p T is incomplete | ||||||||||||
8333 | /// and cannot be completed, issues the diagnostic @p diag (giving it | ||||||||||||
8334 | /// the type @p T) and returns true. | ||||||||||||
8335 | /// | ||||||||||||
8336 | /// @param Loc The location in the source that the incomplete type | ||||||||||||
8337 | /// diagnostic should refer to. | ||||||||||||
8338 | /// | ||||||||||||
8339 | /// @param T The type that this routine is examining for completeness. | ||||||||||||
8340 | /// | ||||||||||||
8341 | /// @param Kind Selects which completeness rules should be applied. | ||||||||||||
8342 | /// | ||||||||||||
8343 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, | ||||||||||||
8344 | /// @c false otherwise. | ||||||||||||
8345 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, | ||||||||||||
8346 | CompleteTypeKind Kind, | ||||||||||||
8347 | TypeDiagnoser &Diagnoser) { | ||||||||||||
8348 | if (RequireCompleteTypeImpl(Loc, T, Kind, &Diagnoser)) | ||||||||||||
8349 | return true; | ||||||||||||
8350 | if (const TagType *Tag = T->getAs<TagType>()) { | ||||||||||||
8351 | if (!Tag->getDecl()->isCompleteDefinitionRequired()) { | ||||||||||||
8352 | Tag->getDecl()->setCompleteDefinitionRequired(); | ||||||||||||
8353 | Consumer.HandleTagDeclRequiredDefinition(Tag->getDecl()); | ||||||||||||
8354 | } | ||||||||||||
8355 | } | ||||||||||||
8356 | return false; | ||||||||||||
8357 | } | ||||||||||||
8358 | |||||||||||||
8359 | bool Sema::hasStructuralCompatLayout(Decl *D, Decl *Suggested) { | ||||||||||||
8360 | llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls; | ||||||||||||
8361 | if (!Suggested) | ||||||||||||
8362 | return false; | ||||||||||||
8363 | |||||||||||||
8364 | // FIXME: Add a specific mode for C11 6.2.7/1 in StructuralEquivalenceContext | ||||||||||||
8365 | // and isolate from other C++ specific checks. | ||||||||||||
8366 | StructuralEquivalenceContext Ctx( | ||||||||||||
8367 | D->getASTContext(), Suggested->getASTContext(), NonEquivalentDecls, | ||||||||||||
8368 | StructuralEquivalenceKind::Default, | ||||||||||||
8369 | false /*StrictTypeSpelling*/, true /*Complain*/, | ||||||||||||
8370 | true /*ErrorOnTagTypeMismatch*/); | ||||||||||||
8371 | return Ctx.IsEquivalent(D, Suggested); | ||||||||||||
8372 | } | ||||||||||||
8373 | |||||||||||||
8374 | /// Determine whether there is any declaration of \p D that was ever a | ||||||||||||
8375 | /// definition (perhaps before module merging) and is currently visible. | ||||||||||||
8376 | /// \param D The definition of the entity. | ||||||||||||
8377 | /// \param Suggested Filled in with the declaration that should be made visible | ||||||||||||
8378 | /// in order to provide a definition of this entity. | ||||||||||||
8379 | /// \param OnlyNeedComplete If \c true, we only need the type to be complete, | ||||||||||||
8380 | /// not defined. This only matters for enums with a fixed underlying | ||||||||||||
8381 | /// type, since in all other cases, a type is complete if and only if it | ||||||||||||
8382 | /// is defined. | ||||||||||||
8383 | bool Sema::hasVisibleDefinition(NamedDecl *D, NamedDecl **Suggested, | ||||||||||||
8384 | bool OnlyNeedComplete) { | ||||||||||||
8385 | // Easy case: if we don't have modules, all declarations are visible. | ||||||||||||
8386 | if (!getLangOpts().Modules && !getLangOpts().ModulesLocalVisibility) | ||||||||||||
8387 | return true; | ||||||||||||
8388 | |||||||||||||
8389 | // If this definition was instantiated from a template, map back to the | ||||||||||||
8390 | // pattern from which it was instantiated. | ||||||||||||
8391 | if (isa<TagDecl>(D) && cast<TagDecl>(D)->isBeingDefined()) { | ||||||||||||
8392 | // We're in the middle of defining it; this definition should be treated | ||||||||||||
8393 | // as visible. | ||||||||||||
8394 | return true; | ||||||||||||
8395 | } else if (auto *RD = dyn_cast<CXXRecordDecl>(D)) { | ||||||||||||
8396 | if (auto *Pattern = RD->getTemplateInstantiationPattern()) | ||||||||||||
8397 | RD = Pattern; | ||||||||||||
8398 | D = RD->getDefinition(); | ||||||||||||
8399 | } else if (auto *ED = dyn_cast<EnumDecl>(D)) { | ||||||||||||
8400 | if (auto *Pattern = ED->getTemplateInstantiationPattern()) | ||||||||||||
8401 | ED = Pattern; | ||||||||||||
8402 | if (OnlyNeedComplete && (ED->isFixed() || getLangOpts().MSVCCompat)) { | ||||||||||||
8403 | // If the enum has a fixed underlying type, it may have been forward | ||||||||||||
8404 | // declared. In -fms-compatibility, `enum Foo;` will also forward declare | ||||||||||||
8405 | // the enum and assign it the underlying type of `int`. Since we're only | ||||||||||||
8406 | // looking for a complete type (not a definition), any visible declaration | ||||||||||||
8407 | // of it will do. | ||||||||||||
8408 | *Suggested = nullptr; | ||||||||||||
8409 | for (auto *Redecl : ED->redecls()) { | ||||||||||||
8410 | if (isVisible(Redecl)) | ||||||||||||
8411 | return true; | ||||||||||||
8412 | if (Redecl->isThisDeclarationADefinition() || | ||||||||||||
8413 | (Redecl->isCanonicalDecl() && !*Suggested)) | ||||||||||||
8414 | *Suggested = Redecl; | ||||||||||||
8415 | } | ||||||||||||
8416 | return false; | ||||||||||||
8417 | } | ||||||||||||
8418 | D = ED->getDefinition(); | ||||||||||||
8419 | } else if (auto *FD = dyn_cast<FunctionDecl>(D)) { | ||||||||||||
8420 | if (auto *Pattern = FD->getTemplateInstantiationPattern()) | ||||||||||||
8421 | FD = Pattern; | ||||||||||||
8422 | D = FD->getDefinition(); | ||||||||||||
8423 | } else if (auto *VD = dyn_cast<VarDecl>(D)) { | ||||||||||||
8424 | if (auto *Pattern = VD->getTemplateInstantiationPattern()) | ||||||||||||
8425 | VD = Pattern; | ||||||||||||
8426 | D = VD->getDefinition(); | ||||||||||||
8427 | } | ||||||||||||
8428 | assert(D && "missing definition for pattern of instantiated definition")((D && "missing definition for pattern of instantiated definition" ) ? static_cast<void> (0) : __assert_fail ("D && \"missing definition for pattern of instantiated definition\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8428, __PRETTY_FUNCTION__)); | ||||||||||||
8429 | |||||||||||||
8430 | *Suggested = D; | ||||||||||||
8431 | |||||||||||||
8432 | auto DefinitionIsVisible = [&] { | ||||||||||||
8433 | // The (primary) definition might be in a visible module. | ||||||||||||
8434 | if (isVisible(D)) | ||||||||||||
8435 | return true; | ||||||||||||
8436 | |||||||||||||
8437 | // A visible module might have a merged definition instead. | ||||||||||||
8438 | if (D->isModulePrivate() ? hasMergedDefinitionInCurrentModule(D) | ||||||||||||
8439 | : hasVisibleMergedDefinition(D)) { | ||||||||||||
8440 | if (CodeSynthesisContexts.empty() && | ||||||||||||
8441 | !getLangOpts().ModulesLocalVisibility) { | ||||||||||||
8442 | // Cache the fact that this definition is implicitly visible because | ||||||||||||
8443 | // there is a visible merged definition. | ||||||||||||
8444 | D->setVisibleDespiteOwningModule(); | ||||||||||||
8445 | } | ||||||||||||
8446 | return true; | ||||||||||||
8447 | } | ||||||||||||
8448 | |||||||||||||
8449 | return false; | ||||||||||||
8450 | }; | ||||||||||||
8451 | |||||||||||||
8452 | if (DefinitionIsVisible()) | ||||||||||||
8453 | return true; | ||||||||||||
8454 | |||||||||||||
8455 | // The external source may have additional definitions of this entity that are | ||||||||||||
8456 | // visible, so complete the redeclaration chain now and ask again. | ||||||||||||
8457 | if (auto *Source = Context.getExternalSource()) { | ||||||||||||
8458 | Source->CompleteRedeclChain(D); | ||||||||||||
8459 | return DefinitionIsVisible(); | ||||||||||||
8460 | } | ||||||||||||
8461 | |||||||||||||
8462 | return false; | ||||||||||||
8463 | } | ||||||||||||
8464 | |||||||||||||
8465 | /// Locks in the inheritance model for the given class and all of its bases. | ||||||||||||
8466 | static void assignInheritanceModel(Sema &S, CXXRecordDecl *RD) { | ||||||||||||
8467 | RD = RD->getMostRecentNonInjectedDecl(); | ||||||||||||
8468 | if (!RD->hasAttr<MSInheritanceAttr>()) { | ||||||||||||
8469 | MSInheritanceModel IM; | ||||||||||||
8470 | bool BestCase = false; | ||||||||||||
8471 | switch (S.MSPointerToMemberRepresentationMethod) { | ||||||||||||
8472 | case LangOptions::PPTMK_BestCase: | ||||||||||||
8473 | BestCase = true; | ||||||||||||
8474 | IM = RD->calculateInheritanceModel(); | ||||||||||||
8475 | break; | ||||||||||||
8476 | case LangOptions::PPTMK_FullGeneralitySingleInheritance: | ||||||||||||
8477 | IM = MSInheritanceModel::Single; | ||||||||||||
8478 | break; | ||||||||||||
8479 | case LangOptions::PPTMK_FullGeneralityMultipleInheritance: | ||||||||||||
8480 | IM = MSInheritanceModel::Multiple; | ||||||||||||
8481 | break; | ||||||||||||
8482 | case LangOptions::PPTMK_FullGeneralityVirtualInheritance: | ||||||||||||
8483 | IM = MSInheritanceModel::Unspecified; | ||||||||||||
8484 | break; | ||||||||||||
8485 | } | ||||||||||||
8486 | |||||||||||||
8487 | SourceRange Loc = S.ImplicitMSInheritanceAttrLoc.isValid() | ||||||||||||
8488 | ? S.ImplicitMSInheritanceAttrLoc | ||||||||||||
8489 | : RD->getSourceRange(); | ||||||||||||
8490 | RD->addAttr(MSInheritanceAttr::CreateImplicit( | ||||||||||||
8491 | S.getASTContext(), BestCase, Loc, AttributeCommonInfo::AS_Microsoft, | ||||||||||||
8492 | MSInheritanceAttr::Spelling(IM))); | ||||||||||||
8493 | S.Consumer.AssignInheritanceModel(RD); | ||||||||||||
8494 | } | ||||||||||||
8495 | } | ||||||||||||
8496 | |||||||||||||
8497 | /// The implementation of RequireCompleteType | ||||||||||||
8498 | bool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T, | ||||||||||||
8499 | CompleteTypeKind Kind, | ||||||||||||
8500 | TypeDiagnoser *Diagnoser) { | ||||||||||||
8501 | // FIXME: Add this assertion to make sure we always get instantiation points. | ||||||||||||
8502 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); | ||||||||||||
8503 | // FIXME: Add this assertion to help us flush out problems with | ||||||||||||
8504 | // checking for dependent types and type-dependent expressions. | ||||||||||||
8505 | // | ||||||||||||
8506 | // assert(!T->isDependentType() && | ||||||||||||
8507 | // "Can't ask whether a dependent type is complete"); | ||||||||||||
8508 | |||||||||||||
8509 | if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) { | ||||||||||||
8510 | if (!MPTy->getClass()->isDependentType()) { | ||||||||||||
8511 | if (getLangOpts().CompleteMemberPointers && | ||||||||||||
8512 | !MPTy->getClass()->getAsCXXRecordDecl()->isBeingDefined() && | ||||||||||||
8513 | RequireCompleteType(Loc, QualType(MPTy->getClass(), 0), Kind, | ||||||||||||
8514 | diag::err_memptr_incomplete)) | ||||||||||||
8515 | return true; | ||||||||||||
8516 | |||||||||||||
8517 | // We lock in the inheritance model once somebody has asked us to ensure | ||||||||||||
8518 | // that a pointer-to-member type is complete. | ||||||||||||
8519 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { | ||||||||||||
8520 | (void)isCompleteType(Loc, QualType(MPTy->getClass(), 0)); | ||||||||||||
8521 | assignInheritanceModel(*this, MPTy->getMostRecentCXXRecordDecl()); | ||||||||||||
8522 | } | ||||||||||||
8523 | } | ||||||||||||
8524 | } | ||||||||||||
8525 | |||||||||||||
8526 | NamedDecl *Def = nullptr; | ||||||||||||
8527 | bool AcceptSizeless = (Kind == CompleteTypeKind::AcceptSizeless); | ||||||||||||
8528 | bool Incomplete = (T->isIncompleteType(&Def) || | ||||||||||||
8529 | (!AcceptSizeless && T->isSizelessBuiltinType())); | ||||||||||||
8530 | |||||||||||||
8531 | // Check that any necessary explicit specializations are visible. For an | ||||||||||||
8532 | // enum, we just need the declaration, so don't check this. | ||||||||||||
8533 | if (Def && !isa<EnumDecl>(Def)) | ||||||||||||
8534 | checkSpecializationVisibility(Loc, Def); | ||||||||||||
8535 | |||||||||||||
8536 | // If we have a complete type, we're done. | ||||||||||||
8537 | if (!Incomplete) { | ||||||||||||
8538 | // If we know about the definition but it is not visible, complain. | ||||||||||||
8539 | NamedDecl *SuggestedDef = nullptr; | ||||||||||||
8540 | if (Def && | ||||||||||||
8541 | !hasVisibleDefinition(Def, &SuggestedDef, /*OnlyNeedComplete*/true)) { | ||||||||||||
8542 | // If the user is going to see an error here, recover by making the | ||||||||||||
8543 | // definition visible. | ||||||||||||
8544 | bool TreatAsComplete = Diagnoser && !isSFINAEContext(); | ||||||||||||
8545 | if (Diagnoser && SuggestedDef) | ||||||||||||
8546 | diagnoseMissingImport(Loc, SuggestedDef, MissingImportKind::Definition, | ||||||||||||
8547 | /*Recover*/TreatAsComplete); | ||||||||||||
8548 | return !TreatAsComplete; | ||||||||||||
8549 | } else if (Def && !TemplateInstCallbacks.empty()) { | ||||||||||||
8550 | CodeSynthesisContext TempInst; | ||||||||||||
8551 | TempInst.Kind = CodeSynthesisContext::Memoization; | ||||||||||||
8552 | TempInst.Template = Def; | ||||||||||||
8553 | TempInst.Entity = Def; | ||||||||||||
8554 | TempInst.PointOfInstantiation = Loc; | ||||||||||||
8555 | atTemplateBegin(TemplateInstCallbacks, *this, TempInst); | ||||||||||||
8556 | atTemplateEnd(TemplateInstCallbacks, *this, TempInst); | ||||||||||||
8557 | } | ||||||||||||
8558 | |||||||||||||
8559 | return false; | ||||||||||||
8560 | } | ||||||||||||
8561 | |||||||||||||
8562 | TagDecl *Tag = dyn_cast_or_null<TagDecl>(Def); | ||||||||||||
8563 | ObjCInterfaceDecl *IFace = dyn_cast_or_null<ObjCInterfaceDecl>(Def); | ||||||||||||
8564 | |||||||||||||
8565 | // Give the external source a chance to provide a definition of the type. | ||||||||||||
8566 | // This is kept separate from completing the redeclaration chain so that | ||||||||||||
8567 | // external sources such as LLDB can avoid synthesizing a type definition | ||||||||||||
8568 | // unless it's actually needed. | ||||||||||||
8569 | if (Tag || IFace) { | ||||||||||||
8570 | // Avoid diagnosing invalid decls as incomplete. | ||||||||||||
8571 | if (Def->isInvalidDecl()) | ||||||||||||
8572 | return true; | ||||||||||||
8573 | |||||||||||||
8574 | // Give the external AST source a chance to complete the type. | ||||||||||||
8575 | if (auto *Source = Context.getExternalSource()) { | ||||||||||||
8576 | if (Tag && Tag->hasExternalLexicalStorage()) | ||||||||||||
8577 | Source->CompleteType(Tag); | ||||||||||||
8578 | if (IFace && IFace->hasExternalLexicalStorage()) | ||||||||||||
8579 | Source->CompleteType(IFace); | ||||||||||||
8580 | // If the external source completed the type, go through the motions | ||||||||||||
8581 | // again to ensure we're allowed to use the completed type. | ||||||||||||
8582 | if (!T->isIncompleteType()) | ||||||||||||
8583 | return RequireCompleteTypeImpl(Loc, T, Kind, Diagnoser); | ||||||||||||
8584 | } | ||||||||||||
8585 | } | ||||||||||||
8586 | |||||||||||||
8587 | // If we have a class template specialization or a class member of a | ||||||||||||
8588 | // class template specialization, or an array with known size of such, | ||||||||||||
8589 | // try to instantiate it. | ||||||||||||
8590 | if (auto *RD = dyn_cast_or_null<CXXRecordDecl>(Tag)) { | ||||||||||||
8591 | bool Instantiated = false; | ||||||||||||
8592 | bool Diagnosed = false; | ||||||||||||
8593 | if (RD->isDependentContext()) { | ||||||||||||
8594 | // Don't try to instantiate a dependent class (eg, a member template of | ||||||||||||
8595 | // an instantiated class template specialization). | ||||||||||||
8596 | // FIXME: Can this ever happen? | ||||||||||||
8597 | } else if (auto *ClassTemplateSpec = | ||||||||||||
8598 | dyn_cast<ClassTemplateSpecializationDecl>(RD)) { | ||||||||||||
8599 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { | ||||||||||||
8600 | runWithSufficientStackSpace(Loc, [&] { | ||||||||||||
8601 | Diagnosed = InstantiateClassTemplateSpecialization( | ||||||||||||
8602 | Loc, ClassTemplateSpec, TSK_ImplicitInstantiation, | ||||||||||||
8603 | /*Complain=*/Diagnoser); | ||||||||||||
8604 | }); | ||||||||||||
8605 | Instantiated = true; | ||||||||||||
8606 | } | ||||||||||||
8607 | } else { | ||||||||||||
8608 | CXXRecordDecl *Pattern = RD->getInstantiatedFromMemberClass(); | ||||||||||||
8609 | if (!RD->isBeingDefined() && Pattern) { | ||||||||||||
8610 | MemberSpecializationInfo *MSI = RD->getMemberSpecializationInfo(); | ||||||||||||
8611 | assert(MSI && "Missing member specialization information?")((MSI && "Missing member specialization information?" ) ? static_cast<void> (0) : __assert_fail ("MSI && \"Missing member specialization information?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8611, __PRETTY_FUNCTION__)); | ||||||||||||
8612 | // This record was instantiated from a class within a template. | ||||||||||||
8613 | if (MSI->getTemplateSpecializationKind() != | ||||||||||||
8614 | TSK_ExplicitSpecialization) { | ||||||||||||
8615 | runWithSufficientStackSpace(Loc, [&] { | ||||||||||||
8616 | Diagnosed = InstantiateClass(Loc, RD, Pattern, | ||||||||||||
8617 | getTemplateInstantiationArgs(RD), | ||||||||||||
8618 | TSK_ImplicitInstantiation, | ||||||||||||
8619 | /*Complain=*/Diagnoser); | ||||||||||||
8620 | }); | ||||||||||||
8621 | Instantiated = true; | ||||||||||||
8622 | } | ||||||||||||
8623 | } | ||||||||||||
8624 | } | ||||||||||||
8625 | |||||||||||||
8626 | if (Instantiated) { | ||||||||||||
8627 | // Instantiate* might have already complained that the template is not | ||||||||||||
8628 | // defined, if we asked it to. | ||||||||||||
8629 | if (Diagnoser && Diagnosed) | ||||||||||||
8630 | return true; | ||||||||||||
8631 | // If we instantiated a definition, check that it's usable, even if | ||||||||||||
8632 | // instantiation produced an error, so that repeated calls to this | ||||||||||||
8633 | // function give consistent answers. | ||||||||||||
8634 | if (!T->isIncompleteType()) | ||||||||||||
8635 | return RequireCompleteTypeImpl(Loc, T, Kind, Diagnoser); | ||||||||||||
8636 | } | ||||||||||||
8637 | } | ||||||||||||
8638 | |||||||||||||
8639 | // FIXME: If we didn't instantiate a definition because of an explicit | ||||||||||||
8640 | // specialization declaration, check that it's visible. | ||||||||||||
8641 | |||||||||||||
8642 | if (!Diagnoser) | ||||||||||||
8643 | return true; | ||||||||||||
8644 | |||||||||||||
8645 | Diagnoser->diagnose(*this, Loc, T); | ||||||||||||
8646 | |||||||||||||
8647 | // If the type was a forward declaration of a class/struct/union | ||||||||||||
8648 | // type, produce a note. | ||||||||||||
8649 | if (Tag && !Tag->isInvalidDecl() && !Tag->getLocation().isInvalid()) | ||||||||||||
8650 | Diag(Tag->getLocation(), | ||||||||||||
8651 | Tag->isBeingDefined() ? diag::note_type_being_defined | ||||||||||||
8652 | : diag::note_forward_declaration) | ||||||||||||
8653 | << Context.getTagDeclType(Tag); | ||||||||||||
8654 | |||||||||||||
8655 | // If the Objective-C class was a forward declaration, produce a note. | ||||||||||||
8656 | if (IFace && !IFace->isInvalidDecl() && !IFace->getLocation().isInvalid()) | ||||||||||||
8657 | Diag(IFace->getLocation(), diag::note_forward_class); | ||||||||||||
8658 | |||||||||||||
8659 | // If we have external information that we can use to suggest a fix, | ||||||||||||
8660 | // produce a note. | ||||||||||||
8661 | if (ExternalSource) | ||||||||||||
8662 | ExternalSource->MaybeDiagnoseMissingCompleteType(Loc, T); | ||||||||||||
8663 | |||||||||||||
8664 | return true; | ||||||||||||
8665 | } | ||||||||||||
8666 | |||||||||||||
8667 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, | ||||||||||||
8668 | CompleteTypeKind Kind, unsigned DiagID) { | ||||||||||||
8669 | BoundTypeDiagnoser<> Diagnoser(DiagID); | ||||||||||||
8670 | return RequireCompleteType(Loc, T, Kind, Diagnoser); | ||||||||||||
8671 | } | ||||||||||||
8672 | |||||||||||||
8673 | /// Get diagnostic %select index for tag kind for | ||||||||||||
8674 | /// literal type diagnostic message. | ||||||||||||
8675 | /// WARNING: Indexes apply to particular diagnostics only! | ||||||||||||
8676 | /// | ||||||||||||
8677 | /// \returns diagnostic %select index. | ||||||||||||
8678 | static unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) { | ||||||||||||
8679 | switch (Tag) { | ||||||||||||
8680 | case TTK_Struct: return 0; | ||||||||||||
8681 | case TTK_Interface: return 1; | ||||||||||||
8682 | case TTK_Class: return 2; | ||||||||||||
8683 | default: llvm_unreachable("Invalid tag kind for literal type diagnostic!")::llvm::llvm_unreachable_internal("Invalid tag kind for literal type diagnostic!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8683); | ||||||||||||
8684 | } | ||||||||||||
8685 | } | ||||||||||||
8686 | |||||||||||||
8687 | /// Ensure that the type T is a literal type. | ||||||||||||
8688 | /// | ||||||||||||
8689 | /// This routine checks whether the type @p T is a literal type. If @p T is an | ||||||||||||
8690 | /// incomplete type, an attempt is made to complete it. If @p T is a literal | ||||||||||||
8691 | /// type, or @p AllowIncompleteType is true and @p T is an incomplete type, | ||||||||||||
8692 | /// returns false. Otherwise, this routine issues the diagnostic @p PD (giving | ||||||||||||
8693 | /// it the type @p T), along with notes explaining why the type is not a | ||||||||||||
8694 | /// literal type, and returns true. | ||||||||||||
8695 | /// | ||||||||||||
8696 | /// @param Loc The location in the source that the non-literal type | ||||||||||||
8697 | /// diagnostic should refer to. | ||||||||||||
8698 | /// | ||||||||||||
8699 | /// @param T The type that this routine is examining for literalness. | ||||||||||||
8700 | /// | ||||||||||||
8701 | /// @param Diagnoser Emits a diagnostic if T is not a literal type. | ||||||||||||
8702 | /// | ||||||||||||
8703 | /// @returns @c true if @p T is not a literal type and a diagnostic was emitted, | ||||||||||||
8704 | /// @c false otherwise. | ||||||||||||
8705 | bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, | ||||||||||||
8706 | TypeDiagnoser &Diagnoser) { | ||||||||||||
8707 | assert(!T->isDependentType() && "type should not be dependent")((!T->isDependentType() && "type should not be dependent" ) ? static_cast<void> (0) : __assert_fail ("!T->isDependentType() && \"type should not be dependent\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8707, __PRETTY_FUNCTION__)); | ||||||||||||
8708 | |||||||||||||
8709 | QualType ElemType = Context.getBaseElementType(T); | ||||||||||||
8710 | if ((isCompleteType(Loc, ElemType) || ElemType->isVoidType()) && | ||||||||||||
8711 | T->isLiteralType(Context)) | ||||||||||||
8712 | return false; | ||||||||||||
8713 | |||||||||||||
8714 | Diagnoser.diagnose(*this, Loc, T); | ||||||||||||
8715 | |||||||||||||
8716 | if (T->isVariableArrayType()) | ||||||||||||
8717 | return true; | ||||||||||||
8718 | |||||||||||||
8719 | const RecordType *RT = ElemType->getAs<RecordType>(); | ||||||||||||
8720 | if (!RT) | ||||||||||||
8721 | return true; | ||||||||||||
8722 | |||||||||||||
8723 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | ||||||||||||
8724 | |||||||||||||
8725 | // A partially-defined class type can't be a literal type, because a literal | ||||||||||||
8726 | // class type must have a trivial destructor (which can't be checked until | ||||||||||||
8727 | // the class definition is complete). | ||||||||||||
8728 | if (RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T)) | ||||||||||||
8729 | return true; | ||||||||||||
8730 | |||||||||||||
8731 | // [expr.prim.lambda]p3: | ||||||||||||
8732 | // This class type is [not] a literal type. | ||||||||||||
8733 | if (RD->isLambda() && !getLangOpts().CPlusPlus17) { | ||||||||||||
8734 | Diag(RD->getLocation(), diag::note_non_literal_lambda); | ||||||||||||
8735 | return true; | ||||||||||||
8736 | } | ||||||||||||
8737 | |||||||||||||
8738 | // If the class has virtual base classes, then it's not an aggregate, and | ||||||||||||
8739 | // cannot have any constexpr constructors or a trivial default constructor, | ||||||||||||
8740 | // so is non-literal. This is better to diagnose than the resulting absence | ||||||||||||
8741 | // of constexpr constructors. | ||||||||||||
8742 | if (RD->getNumVBases()) { | ||||||||||||
8743 | Diag(RD->getLocation(), diag::note_non_literal_virtual_base) | ||||||||||||
8744 | << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); | ||||||||||||
8745 | for (const auto &I : RD->vbases()) | ||||||||||||
8746 | Diag(I.getBeginLoc(), diag::note_constexpr_virtual_base_here) | ||||||||||||
8747 | << I.getSourceRange(); | ||||||||||||
8748 | } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() && | ||||||||||||
8749 | !RD->hasTrivialDefaultConstructor()) { | ||||||||||||
8750 | Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD; | ||||||||||||
8751 | } else if (RD->hasNonLiteralTypeFieldsOrBases()) { | ||||||||||||
8752 | for (const auto &I : RD->bases()) { | ||||||||||||
8753 | if (!I.getType()->isLiteralType(Context)) { | ||||||||||||
8754 | Diag(I.getBeginLoc(), diag::note_non_literal_base_class) | ||||||||||||
8755 | << RD << I.getType() << I.getSourceRange(); | ||||||||||||
8756 | return true; | ||||||||||||
8757 | } | ||||||||||||
8758 | } | ||||||||||||
8759 | for (const auto *I : RD->fields()) { | ||||||||||||
8760 | if (!I->getType()->isLiteralType(Context) || | ||||||||||||
8761 | I->getType().isVolatileQualified()) { | ||||||||||||
8762 | Diag(I->getLocation(), diag::note_non_literal_field) | ||||||||||||
8763 | << RD << I << I->getType() | ||||||||||||
8764 | << I->getType().isVolatileQualified(); | ||||||||||||
8765 | return true; | ||||||||||||
8766 | } | ||||||||||||
8767 | } | ||||||||||||
8768 | } else if (getLangOpts().CPlusPlus20 ? !RD->hasConstexprDestructor() | ||||||||||||
8769 | : !RD->hasTrivialDestructor()) { | ||||||||||||
8770 | // All fields and bases are of literal types, so have trivial or constexpr | ||||||||||||
8771 | // destructors. If this class's destructor is non-trivial / non-constexpr, | ||||||||||||
8772 | // it must be user-declared. | ||||||||||||
8773 | CXXDestructorDecl *Dtor = RD->getDestructor(); | ||||||||||||
8774 | assert(Dtor && "class has literal fields and bases but no dtor?")((Dtor && "class has literal fields and bases but no dtor?" ) ? static_cast<void> (0) : __assert_fail ("Dtor && \"class has literal fields and bases but no dtor?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8774, __PRETTY_FUNCTION__)); | ||||||||||||
8775 | if (!Dtor) | ||||||||||||
8776 | return true; | ||||||||||||
8777 | |||||||||||||
8778 | if (getLangOpts().CPlusPlus20) { | ||||||||||||
8779 | Diag(Dtor->getLocation(), diag::note_non_literal_non_constexpr_dtor) | ||||||||||||
8780 | << RD; | ||||||||||||
8781 | } else { | ||||||||||||
8782 | Diag(Dtor->getLocation(), Dtor->isUserProvided() | ||||||||||||
8783 | ? diag::note_non_literal_user_provided_dtor | ||||||||||||
8784 | : diag::note_non_literal_nontrivial_dtor) | ||||||||||||
8785 | << RD; | ||||||||||||
8786 | if (!Dtor->isUserProvided()) | ||||||||||||
8787 | SpecialMemberIsTrivial(Dtor, CXXDestructor, TAH_IgnoreTrivialABI, | ||||||||||||
8788 | /*Diagnose*/ true); | ||||||||||||
8789 | } | ||||||||||||
8790 | } | ||||||||||||
8791 | |||||||||||||
8792 | return true; | ||||||||||||
8793 | } | ||||||||||||
8794 | |||||||||||||
8795 | bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) { | ||||||||||||
8796 | BoundTypeDiagnoser<> Diagnoser(DiagID); | ||||||||||||
8797 | return RequireLiteralType(Loc, T, Diagnoser); | ||||||||||||
8798 | } | ||||||||||||
8799 | |||||||||||||
8800 | /// Retrieve a version of the type 'T' that is elaborated by Keyword, qualified | ||||||||||||
8801 | /// by the nested-name-specifier contained in SS, and that is (re)declared by | ||||||||||||
8802 | /// OwnedTagDecl, which is nullptr if this is not a (re)declaration. | ||||||||||||
8803 | QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, | ||||||||||||
8804 | const CXXScopeSpec &SS, QualType T, | ||||||||||||
8805 | TagDecl *OwnedTagDecl) { | ||||||||||||
8806 | if (T.isNull()) | ||||||||||||
8807 | return T; | ||||||||||||
8808 | NestedNameSpecifier *NNS; | ||||||||||||
8809 | if (SS.isValid()) | ||||||||||||
8810 | NNS = SS.getScopeRep(); | ||||||||||||
8811 | else { | ||||||||||||
8812 | if (Keyword == ETK_None) | ||||||||||||
8813 | return T; | ||||||||||||
8814 | NNS = nullptr; | ||||||||||||
8815 | } | ||||||||||||
8816 | return Context.getElaboratedType(Keyword, NNS, T, OwnedTagDecl); | ||||||||||||
8817 | } | ||||||||||||
8818 | |||||||||||||
8819 | QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) { | ||||||||||||
8820 | assert(!E->hasPlaceholderType() && "unexpected placeholder")((!E->hasPlaceholderType() && "unexpected placeholder" ) ? static_cast<void> (0) : __assert_fail ("!E->hasPlaceholderType() && \"unexpected placeholder\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8820, __PRETTY_FUNCTION__)); | ||||||||||||
8821 | |||||||||||||
8822 | if (!getLangOpts().CPlusPlus && E->refersToBitField()) | ||||||||||||
8823 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 2; | ||||||||||||
8824 | |||||||||||||
8825 | if (!E->isTypeDependent()) { | ||||||||||||
8826 | QualType T = E->getType(); | ||||||||||||
8827 | if (const TagType *TT = T->getAs<TagType>()) | ||||||||||||
8828 | DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc()); | ||||||||||||
8829 | } | ||||||||||||
8830 | return Context.getTypeOfExprType(E); | ||||||||||||
8831 | } | ||||||||||||
8832 | |||||||||||||
8833 | /// getDecltypeForParenthesizedExpr - Given an expr, will return the type for | ||||||||||||
8834 | /// that expression, as in [dcl.type.simple]p4 but without taking id-expressions | ||||||||||||
8835 | /// and class member access into account. | ||||||||||||
8836 | QualType Sema::getDecltypeForParenthesizedExpr(Expr *E) { | ||||||||||||
8837 | // C++11 [dcl.type.simple]p4: | ||||||||||||
8838 | // [...] | ||||||||||||
8839 | QualType T = E->getType(); | ||||||||||||
8840 | switch (E->getValueKind()) { | ||||||||||||
8841 | // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the | ||||||||||||
8842 | // type of e; | ||||||||||||
8843 | case VK_XValue: | ||||||||||||
8844 | return Context.getRValueReferenceType(T); | ||||||||||||
8845 | // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the | ||||||||||||
8846 | // type of e; | ||||||||||||
8847 | case VK_LValue: | ||||||||||||
8848 | return Context.getLValueReferenceType(T); | ||||||||||||
8849 | // - otherwise, decltype(e) is the type of e. | ||||||||||||
8850 | case VK_RValue: | ||||||||||||
8851 | return T; | ||||||||||||
8852 | } | ||||||||||||
8853 | llvm_unreachable("Unknown value kind")::llvm::llvm_unreachable_internal("Unknown value kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8853); | ||||||||||||
8854 | } | ||||||||||||
8855 | |||||||||||||
8856 | /// getDecltypeForExpr - Given an expr, will return the decltype for | ||||||||||||
8857 | /// that expression, according to the rules in C++11 | ||||||||||||
8858 | /// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18. | ||||||||||||
8859 | static QualType getDecltypeForExpr(Sema &S, Expr *E) { | ||||||||||||
8860 | if (E->isTypeDependent()) | ||||||||||||
8861 | return S.Context.DependentTy; | ||||||||||||
8862 | |||||||||||||
8863 | // C++11 [dcl.type.simple]p4: | ||||||||||||
8864 | // The type denoted by decltype(e) is defined as follows: | ||||||||||||
8865 | |||||||||||||
8866 | // C++20: | ||||||||||||
8867 | // - if E is an unparenthesized id-expression naming a non-type | ||||||||||||
8868 | // template-parameter (13.2), decltype(E) is the type of the | ||||||||||||
8869 | // template-parameter after performing any necessary type deduction | ||||||||||||
8870 | // Note that this does not pick up the implicit 'const' for a template | ||||||||||||
8871 | // parameter object. This rule makes no difference before C++20 so we apply | ||||||||||||
8872 | // it unconditionally. | ||||||||||||
8873 | if (const auto *SNTTPE = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) | ||||||||||||
8874 | return SNTTPE->getParameterType(S.Context); | ||||||||||||
8875 | |||||||||||||
8876 | // - if e is an unparenthesized id-expression or an unparenthesized class | ||||||||||||
8877 | // member access (5.2.5), decltype(e) is the type of the entity named | ||||||||||||
8878 | // by e. If there is no such entity, or if e names a set of overloaded | ||||||||||||
8879 | // functions, the program is ill-formed; | ||||||||||||
8880 | // | ||||||||||||
8881 | // We apply the same rules for Objective-C ivar and property references. | ||||||||||||
8882 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { | ||||||||||||
8883 | const ValueDecl *VD = DRE->getDecl(); | ||||||||||||
8884 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(VD)) | ||||||||||||
8885 | return TPO->getType().getUnqualifiedType(); | ||||||||||||
8886 | return VD->getType(); | ||||||||||||
8887 | } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { | ||||||||||||
8888 | if (const ValueDecl *VD = ME->getMemberDecl()) | ||||||||||||
8889 | if (isa<FieldDecl>(VD) || isa<VarDecl>(VD)) | ||||||||||||
8890 | return VD->getType(); | ||||||||||||
8891 | } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) { | ||||||||||||
8892 | return IR->getDecl()->getType(); | ||||||||||||
8893 | } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) { | ||||||||||||
8894 | if (PR->isExplicitProperty()) | ||||||||||||
8895 | return PR->getExplicitProperty()->getType(); | ||||||||||||
8896 | } else if (auto *PE = dyn_cast<PredefinedExpr>(E)) { | ||||||||||||
8897 | return PE->getType(); | ||||||||||||
8898 | } | ||||||||||||
8899 | |||||||||||||
8900 | // C++11 [expr.lambda.prim]p18: | ||||||||||||
8901 | // Every occurrence of decltype((x)) where x is a possibly | ||||||||||||
8902 | // parenthesized id-expression that names an entity of automatic | ||||||||||||
8903 | // storage duration is treated as if x were transformed into an | ||||||||||||
8904 | // access to a corresponding data member of the closure type that | ||||||||||||
8905 | // would have been declared if x were an odr-use of the denoted | ||||||||||||
8906 | // entity. | ||||||||||||
8907 | using namespace sema; | ||||||||||||
8908 | if (S.getCurLambda()) { | ||||||||||||
8909 | if (isa<ParenExpr>(E)) { | ||||||||||||
8910 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { | ||||||||||||
8911 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { | ||||||||||||
8912 | QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation()); | ||||||||||||
8913 | if (!T.isNull()) | ||||||||||||
8914 | return S.Context.getLValueReferenceType(T); | ||||||||||||
8915 | } | ||||||||||||
8916 | } | ||||||||||||
8917 | } | ||||||||||||
8918 | } | ||||||||||||
8919 | |||||||||||||
8920 | return S.getDecltypeForParenthesizedExpr(E); | ||||||||||||
8921 | } | ||||||||||||
8922 | |||||||||||||
8923 | QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc, | ||||||||||||
8924 | bool AsUnevaluated) { | ||||||||||||
8925 | assert(!E->hasPlaceholderType() && "unexpected placeholder")((!E->hasPlaceholderType() && "unexpected placeholder" ) ? static_cast<void> (0) : __assert_fail ("!E->hasPlaceholderType() && \"unexpected placeholder\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8925, __PRETTY_FUNCTION__)); | ||||||||||||
8926 | |||||||||||||
8927 | if (AsUnevaluated && CodeSynthesisContexts.empty() && | ||||||||||||
8928 | !E->isInstantiationDependent() && E->HasSideEffects(Context, false)) { | ||||||||||||
8929 | // The expression operand for decltype is in an unevaluated expression | ||||||||||||
8930 | // context, so side effects could result in unintended consequences. | ||||||||||||
8931 | // Exclude instantiation-dependent expressions, because 'decltype' is often | ||||||||||||
8932 | // used to build SFINAE gadgets. | ||||||||||||
8933 | Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context); | ||||||||||||
8934 | } | ||||||||||||
8935 | |||||||||||||
8936 | return Context.getDecltypeType(E, getDecltypeForExpr(*this, E)); | ||||||||||||
8937 | } | ||||||||||||
8938 | |||||||||||||
8939 | QualType Sema::BuildUnaryTransformType(QualType BaseType, | ||||||||||||
8940 | UnaryTransformType::UTTKind UKind, | ||||||||||||
8941 | SourceLocation Loc) { | ||||||||||||
8942 | switch (UKind) { | ||||||||||||
8943 | case UnaryTransformType::EnumUnderlyingType: | ||||||||||||
8944 | if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) { | ||||||||||||
8945 | Diag(Loc, diag::err_only_enums_have_underlying_types); | ||||||||||||
8946 | return QualType(); | ||||||||||||
8947 | } else { | ||||||||||||
8948 | QualType Underlying = BaseType; | ||||||||||||
8949 | if (!BaseType->isDependentType()) { | ||||||||||||
8950 | // The enum could be incomplete if we're parsing its definition or | ||||||||||||
8951 | // recovering from an error. | ||||||||||||
8952 | NamedDecl *FwdDecl = nullptr; | ||||||||||||
8953 | if (BaseType->isIncompleteType(&FwdDecl)) { | ||||||||||||
8954 | Diag(Loc, diag::err_underlying_type_of_incomplete_enum) << BaseType; | ||||||||||||
8955 | Diag(FwdDecl->getLocation(), diag::note_forward_declaration) << FwdDecl; | ||||||||||||
8956 | return QualType(); | ||||||||||||
8957 | } | ||||||||||||
8958 | |||||||||||||
8959 | EnumDecl *ED = BaseType->castAs<EnumType>()->getDecl(); | ||||||||||||
8960 | assert(ED && "EnumType has no EnumDecl")((ED && "EnumType has no EnumDecl") ? static_cast< void> (0) : __assert_fail ("ED && \"EnumType has no EnumDecl\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8960, __PRETTY_FUNCTION__)); | ||||||||||||
8961 | |||||||||||||
8962 | DiagnoseUseOfDecl(ED, Loc); | ||||||||||||
8963 | |||||||||||||
8964 | Underlying = ED->getIntegerType(); | ||||||||||||
8965 | assert(!Underlying.isNull())((!Underlying.isNull()) ? static_cast<void> (0) : __assert_fail ("!Underlying.isNull()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8965, __PRETTY_FUNCTION__)); | ||||||||||||
8966 | } | ||||||||||||
8967 | return Context.getUnaryTransformType(BaseType, Underlying, | ||||||||||||
8968 | UnaryTransformType::EnumUnderlyingType); | ||||||||||||
8969 | } | ||||||||||||
8970 | } | ||||||||||||
8971 | llvm_unreachable("unknown unary transform type")::llvm::llvm_unreachable_internal("unknown unary transform type" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/Sema/SemaType.cpp" , 8971); | ||||||||||||
8972 | } | ||||||||||||
8973 | |||||||||||||
8974 | QualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) { | ||||||||||||
8975 | if (!T->isDependentType()) { | ||||||||||||
8976 | // FIXME: It isn't entirely clear whether incomplete atomic types | ||||||||||||
8977 | // are allowed or not; for simplicity, ban them for the moment. | ||||||||||||
8978 | if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0)) | ||||||||||||
8979 | return QualType(); | ||||||||||||
8980 | |||||||||||||
8981 | int DisallowedKind = -1; | ||||||||||||
8982 | if (T->isArrayType()) | ||||||||||||
8983 | DisallowedKind = 1; | ||||||||||||
8984 | else if (T->isFunctionType()) | ||||||||||||
8985 | DisallowedKind = 2; | ||||||||||||
8986 | else if (T->isReferenceType()) | ||||||||||||
8987 | DisallowedKind = 3; | ||||||||||||
8988 | else if (T->isAtomicType()) | ||||||||||||
8989 | DisallowedKind = 4; | ||||||||||||
8990 | else if (T.hasQualifiers()) | ||||||||||||
8991 | DisallowedKind = 5; | ||||||||||||
8992 | else if (T->isSizelessType()) | ||||||||||||
8993 | DisallowedKind = 6; | ||||||||||||
8994 | else if (!T.isTriviallyCopyableType(Context)) | ||||||||||||
8995 | // Some other non-trivially-copyable type (probably a C++ class) | ||||||||||||
8996 | DisallowedKind = 7; | ||||||||||||
8997 | else if (T->isExtIntType()) { | ||||||||||||
8998 | DisallowedKind = 8; | ||||||||||||
8999 | } | ||||||||||||
9000 | |||||||||||||
9001 | if (DisallowedKind != -1) { | ||||||||||||
9002 | Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T; | ||||||||||||
9003 | return QualType(); | ||||||||||||
9004 | } | ||||||||||||
9005 | |||||||||||||
9006 | // FIXME: Do we need any handling for ARC here? | ||||||||||||
9007 | } | ||||||||||||
9008 | |||||||||||||
9009 | // Build the pointer type. | ||||||||||||
9010 | return Context.getAtomicType(T); | ||||||||||||
9011 | } |
1 | //===--- TypeLocVisitor.h - Visitor for TypeLoc subclasses ------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the TypeLocVisitor interface. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | #ifndef LLVM_CLANG_AST_TYPELOCVISITOR_H |
13 | #define LLVM_CLANG_AST_TYPELOCVISITOR_H |
14 | |
15 | #include "clang/AST/TypeLoc.h" |
16 | #include "llvm/Support/ErrorHandling.h" |
17 | |
18 | namespace clang { |
19 | |
20 | #define DISPATCH(CLASSNAME) \ |
21 | return static_cast<ImplClass*>(this)-> \ |
22 | Visit##CLASSNAME(TyLoc.castAs<CLASSNAME>()) |
23 | |
24 | template<typename ImplClass, typename RetTy=void> |
25 | class TypeLocVisitor { |
26 | public: |
27 | RetTy Visit(TypeLoc TyLoc) { |
28 | switch (TyLoc.getTypeLocClass()) { |
29 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
30 | #define TYPELOC(CLASS, PARENT) \ |
31 | case TypeLoc::CLASS: DISPATCH(CLASS##TypeLoc); |
32 | #include "clang/AST/TypeLocNodes.def" |
33 | } |
34 | llvm_unreachable("unexpected type loc class!")::llvm::llvm_unreachable_internal("unexpected type loc class!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/TypeLocVisitor.h" , 34); |
35 | } |
36 | |
37 | RetTy Visit(UnqualTypeLoc TyLoc) { |
38 | switch (TyLoc.getTypeLocClass()) { |
39 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
40 | #define TYPELOC(CLASS, PARENT) \ |
41 | case TypeLoc::CLASS: DISPATCH(CLASS##TypeLoc); |
42 | #include "clang/AST/TypeLocNodes.def" |
43 | } |
44 | llvm_unreachable("unexpected type loc class!")::llvm::llvm_unreachable_internal("unexpected type loc class!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/TypeLocVisitor.h" , 44); |
45 | } |
46 | |
47 | #define TYPELOC(CLASS, PARENT) \ |
48 | RetTy Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ |
49 | DISPATCH(PARENT); \ |
50 | } |
51 | #include "clang/AST/TypeLocNodes.def" |
52 | |
53 | RetTy VisitTypeLoc(TypeLoc TyLoc) { return RetTy(); } |
54 | }; |
55 | |
56 | #undef DISPATCH |
57 | |
58 | } // end namespace clang |
59 | |
60 | #endif // LLVM_CLANG_AST_TYPELOCVISITOR_H |
1 | /*===- TableGen'erated file -------------------------------------*- C++ -*-===*\ |
2 | |* *| |
3 | |* An x-macro database of Clang type nodes *| |
4 | |* *| |
5 | |* Automatically generated file, do not edit! *| |
6 | |* *| |
7 | \*===----------------------------------------------------------------------===*/ |
8 | |
9 | #ifndef ABSTRACT_TYPE |
10 | # define ABSTRACT_TYPE(Class, Base) TYPE(Class, Base) |
11 | #endif |
12 | #ifndef NON_CANONICAL_TYPE |
13 | # define NON_CANONICAL_TYPE(Class, Base) TYPE(Class, Base) |
14 | #endif |
15 | #ifndef DEPENDENT_TYPE |
16 | # define DEPENDENT_TYPE(Class, Base) TYPE(Class, Base) |
17 | #endif |
18 | #ifndef NON_CANONICAL_UNLESS_DEPENDENT_TYPE |
19 | # define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) TYPE(Class, Base) |
20 | #endif |
21 | NON_CANONICAL_TYPE(Adjusted, Type) |
22 | NON_CANONICAL_TYPE(Decayed, AdjustedType) |
23 | ABSTRACT_TYPE(Array, Type) |
24 | TYPE(ConstantArray, ArrayType) |
25 | DEPENDENT_TYPE(DependentSizedArray, ArrayType) |
26 | TYPE(IncompleteArray, ArrayType) |
27 | TYPE(VariableArray, ArrayType) |
28 | TYPE(Atomic, Type) |
29 | NON_CANONICAL_TYPE(Attributed, Type) |
30 | TYPE(BlockPointer, Type) |
31 | TYPE(Builtin, Type) |
32 | TYPE(Complex, Type) |
33 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Decltype, Type) |
34 | ABSTRACT_TYPE(Deduced, Type) |
35 | TYPE(Auto, DeducedType) |
36 | TYPE(DeducedTemplateSpecialization, DeducedType) |
37 | DEPENDENT_TYPE(DependentAddressSpace, Type) |
38 | DEPENDENT_TYPE(DependentExtInt, Type) |
39 | DEPENDENT_TYPE(DependentName, Type) |
40 | DEPENDENT_TYPE(DependentSizedExtVector, Type) |
41 | DEPENDENT_TYPE(DependentTemplateSpecialization, Type) |
42 | DEPENDENT_TYPE(DependentVector, Type) |
43 | NON_CANONICAL_TYPE(Elaborated, Type) |
44 | TYPE(ExtInt, Type) |
45 | ABSTRACT_TYPE(Function, Type) |
46 | TYPE(FunctionNoProto, FunctionType) |
47 | TYPE(FunctionProto, FunctionType) |
48 | DEPENDENT_TYPE(InjectedClassName, Type) |
49 | NON_CANONICAL_TYPE(MacroQualified, Type) |
50 | ABSTRACT_TYPE(Matrix, Type) |
51 | TYPE(ConstantMatrix, MatrixType) |
52 | DEPENDENT_TYPE(DependentSizedMatrix, MatrixType) |
53 | TYPE(MemberPointer, Type) |
54 | TYPE(ObjCObjectPointer, Type) |
55 | TYPE(ObjCObject, Type) |
56 | TYPE(ObjCInterface, ObjCObjectType) |
57 | NON_CANONICAL_TYPE(ObjCTypeParam, Type) |
58 | DEPENDENT_TYPE(PackExpansion, Type) |
59 | NON_CANONICAL_TYPE(Paren, Type) |
60 | TYPE(Pipe, Type) |
61 | TYPE(Pointer, Type) |
62 | ABSTRACT_TYPE(Reference, Type) |
63 | TYPE(LValueReference, ReferenceType) |
64 | TYPE(RValueReference, ReferenceType) |
65 | DEPENDENT_TYPE(SubstTemplateTypeParmPack, Type) |
66 | NON_CANONICAL_TYPE(SubstTemplateTypeParm, Type) |
67 | ABSTRACT_TYPE(Tag, Type) |
68 | TYPE(Enum, TagType) |
69 | TYPE(Record, TagType) |
70 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TemplateSpecialization, Type) |
71 | DEPENDENT_TYPE(TemplateTypeParm, Type) |
72 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TypeOfExpr, Type) |
73 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TypeOf, Type) |
74 | NON_CANONICAL_TYPE(Typedef, Type) |
75 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(UnaryTransform, Type) |
76 | DEPENDENT_TYPE(UnresolvedUsing, Type) |
77 | TYPE(Vector, Type) |
78 | TYPE(ExtVector, VectorType) |
79 | #ifdef LAST_TYPE |
80 | LAST_TYPE(ExtVector) |
81 | #undef LAST_TYPE |
82 | #endif |
83 | #ifdef LEAF_TYPE |
84 | LEAF_TYPE(Builtin) |
85 | LEAF_TYPE(Enum) |
86 | LEAF_TYPE(InjectedClassName) |
87 | LEAF_TYPE(ObjCInterface) |
88 | LEAF_TYPE(Record) |
89 | LEAF_TYPE(TemplateTypeParm) |
90 | #undef LEAF_TYPE |
91 | #endif |
92 | #undef TYPE |
93 | #undef ABSTRACT_TYPE |
94 | #undef ABSTRACT_TYPE |
95 | #undef NON_CANONICAL_TYPE |
96 | #undef DEPENDENT_TYPE |
97 | #undef NON_CANONICAL_UNLESS_DEPENDENT_TYPE |
1 | /*===- TableGen'erated file -------------------------------------*- C++ -*-===*\ |
2 | |* *| |
3 | |* An x-macro database of Clang type nodes *| |
4 | |* *| |
5 | |* Automatically generated file, do not edit! *| |
6 | |* *| |
7 | \*===----------------------------------------------------------------------===*/ |
8 | |
9 | #ifndef ABSTRACT_TYPE |
10 | # define ABSTRACT_TYPE(Class, Base) TYPE(Class, Base) |
11 | #endif |
12 | #ifndef NON_CANONICAL_TYPE |
13 | # define NON_CANONICAL_TYPE(Class, Base) TYPE(Class, Base) |
14 | #endif |
15 | #ifndef DEPENDENT_TYPE |
16 | # define DEPENDENT_TYPE(Class, Base) TYPE(Class, Base) |
17 | #endif |
18 | #ifndef NON_CANONICAL_UNLESS_DEPENDENT_TYPE |
19 | # define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) TYPE(Class, Base) |
20 | #endif |
21 | NON_CANONICAL_TYPE(Adjusted, Type) |
22 | NON_CANONICAL_TYPE(Decayed, AdjustedType) |
23 | ABSTRACT_TYPE(Array, Type) |
24 | TYPE(ConstantArray, ArrayType) |
25 | DEPENDENT_TYPE(DependentSizedArray, ArrayType) |
26 | TYPE(IncompleteArray, ArrayType) |
27 | TYPE(VariableArray, ArrayType) |
28 | TYPE(Atomic, Type) |
29 | NON_CANONICAL_TYPE(Attributed, Type) |
30 | TYPE(BlockPointer, Type) |
31 | TYPE(Builtin, Type) |
32 | TYPE(Complex, Type) |
33 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Decltype, Type) |
34 | ABSTRACT_TYPE(Deduced, Type) |
35 | TYPE(Auto, DeducedType) |
36 | TYPE(DeducedTemplateSpecialization, DeducedType) |
37 | DEPENDENT_TYPE(DependentAddressSpace, Type) |
38 | DEPENDENT_TYPE(DependentExtInt, Type) |
39 | DEPENDENT_TYPE(DependentName, Type) |
40 | DEPENDENT_TYPE(DependentSizedExtVector, Type) |
41 | DEPENDENT_TYPE(DependentTemplateSpecialization, Type) |
42 | DEPENDENT_TYPE(DependentVector, Type) |
43 | NON_CANONICAL_TYPE(Elaborated, Type) |
44 | TYPE(ExtInt, Type) |
45 | ABSTRACT_TYPE(Function, Type) |
46 | TYPE(FunctionNoProto, FunctionType) |
47 | TYPE(FunctionProto, FunctionType) |
48 | DEPENDENT_TYPE(InjectedClassName, Type) |
49 | NON_CANONICAL_TYPE(MacroQualified, Type) |
50 | ABSTRACT_TYPE(Matrix, Type) |
51 | TYPE(ConstantMatrix, MatrixType) |
52 | DEPENDENT_TYPE(DependentSizedMatrix, MatrixType) |
53 | TYPE(MemberPointer, Type) |
54 | TYPE(ObjCObjectPointer, Type) |
55 | TYPE(ObjCObject, Type) |
56 | TYPE(ObjCInterface, ObjCObjectType) |
57 | NON_CANONICAL_TYPE(ObjCTypeParam, Type) |
58 | DEPENDENT_TYPE(PackExpansion, Type) |
59 | NON_CANONICAL_TYPE(Paren, Type) |
60 | TYPE(Pipe, Type) |
61 | TYPE(Pointer, Type) |
62 | ABSTRACT_TYPE(Reference, Type) |
63 | TYPE(LValueReference, ReferenceType) |
64 | TYPE(RValueReference, ReferenceType) |
65 | DEPENDENT_TYPE(SubstTemplateTypeParmPack, Type) |
66 | NON_CANONICAL_TYPE(SubstTemplateTypeParm, Type) |
67 | ABSTRACT_TYPE(Tag, Type) |
68 | TYPE(Enum, TagType) |
69 | TYPE(Record, TagType) |
70 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TemplateSpecialization, Type) |
71 | DEPENDENT_TYPE(TemplateTypeParm, Type) |
72 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TypeOfExpr, Type) |
73 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TypeOf, Type) |
74 | NON_CANONICAL_TYPE(Typedef, Type) |
75 | NON_CANONICAL_UNLESS_DEPENDENT_TYPE(UnaryTransform, Type) |
76 | DEPENDENT_TYPE(UnresolvedUsing, Type) |
77 | TYPE(Vector, Type) |
78 | TYPE(ExtVector, VectorType) |
79 | #ifdef LAST_TYPE |
80 | LAST_TYPE(ExtVector) |
81 | #undef LAST_TYPE |
82 | #endif |
83 | #ifdef LEAF_TYPE |
84 | LEAF_TYPE(Builtin) |
85 | LEAF_TYPE(Enum) |
86 | LEAF_TYPE(InjectedClassName) |
87 | LEAF_TYPE(ObjCInterface) |
88 | LEAF_TYPE(Record) |
89 | LEAF_TYPE(TemplateTypeParm) |
90 | #undef LEAF_TYPE |
91 | #endif |
92 | #undef TYPE |
93 | #undef ABSTRACT_TYPE |
94 | #undef ABSTRACT_TYPE |
95 | #undef NON_CANONICAL_TYPE |
96 | #undef DEPENDENT_TYPE |
97 | #undef NON_CANONICAL_UNLESS_DEPENDENT_TYPE |
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/Linkage.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceLocation.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Basic/Visibility.h" |
33 | #include "llvm/ADT/APInt.h" |
34 | #include "llvm/ADT/APSInt.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/FoldingSet.h" |
37 | #include "llvm/ADT/None.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/Twine.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/PointerLikeTypeTraits.h" |
48 | #include "llvm/Support/TrailingObjects.h" |
49 | #include "llvm/Support/type_traits.h" |
50 | #include <cassert> |
51 | #include <cstddef> |
52 | #include <cstdint> |
53 | #include <cstring> |
54 | #include <string> |
55 | #include <type_traits> |
56 | #include <utility> |
57 | |
58 | namespace clang { |
59 | |
60 | class ExtQuals; |
61 | class QualType; |
62 | class ConceptDecl; |
63 | class TagDecl; |
64 | class TemplateParameterList; |
65 | class Type; |
66 | |
67 | enum { |
68 | TypeAlignmentInBits = 4, |
69 | TypeAlignment = 1 << TypeAlignmentInBits |
70 | }; |
71 | |
72 | namespace serialization { |
73 | template <class T> class AbstractTypeReader; |
74 | template <class T> class AbstractTypeWriter; |
75 | } |
76 | |
77 | } // namespace clang |
78 | |
79 | namespace llvm { |
80 | |
81 | template <typename T> |
82 | struct PointerLikeTypeTraits; |
83 | template<> |
84 | struct PointerLikeTypeTraits< ::clang::Type*> { |
85 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } |
86 | |
87 | static inline ::clang::Type *getFromVoidPointer(void *P) { |
88 | return static_cast< ::clang::Type*>(P); |
89 | } |
90 | |
91 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
92 | }; |
93 | |
94 | template<> |
95 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { |
96 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } |
97 | |
98 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { |
99 | return static_cast< ::clang::ExtQuals*>(P); |
100 | } |
101 | |
102 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
103 | }; |
104 | |
105 | } // namespace llvm |
106 | |
107 | namespace clang { |
108 | |
109 | class ASTContext; |
110 | template <typename> class CanQual; |
111 | class CXXRecordDecl; |
112 | class DeclContext; |
113 | class EnumDecl; |
114 | class Expr; |
115 | class ExtQualsTypeCommonBase; |
116 | class FunctionDecl; |
117 | class IdentifierInfo; |
118 | class NamedDecl; |
119 | class ObjCInterfaceDecl; |
120 | class ObjCProtocolDecl; |
121 | class ObjCTypeParamDecl; |
122 | struct PrintingPolicy; |
123 | class RecordDecl; |
124 | class Stmt; |
125 | class TagDecl; |
126 | class TemplateArgument; |
127 | class TemplateArgumentListInfo; |
128 | class TemplateArgumentLoc; |
129 | class TemplateTypeParmDecl; |
130 | class TypedefNameDecl; |
131 | class UnresolvedUsingTypenameDecl; |
132 | |
133 | using CanQualType = CanQual<Type>; |
134 | |
135 | // Provide forward declarations for all of the *Type classes. |
136 | #define TYPE(Class, Base) class Class##Type; |
137 | #include "clang/AST/TypeNodes.inc" |
138 | |
139 | /// The collection of all-type qualifiers we support. |
140 | /// Clang supports five independent qualifiers: |
141 | /// * C99: const, volatile, and restrict |
142 | /// * MS: __unaligned |
143 | /// * Embedded C (TR18037): address spaces |
144 | /// * Objective C: the GC attributes (none, weak, or strong) |
145 | class Qualifiers { |
146 | public: |
147 | enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. |
148 | Const = 0x1, |
149 | Restrict = 0x2, |
150 | Volatile = 0x4, |
151 | CVRMask = Const | Volatile | Restrict |
152 | }; |
153 | |
154 | enum GC { |
155 | GCNone = 0, |
156 | Weak, |
157 | Strong |
158 | }; |
159 | |
160 | enum ObjCLifetime { |
161 | /// There is no lifetime qualification on this type. |
162 | OCL_None, |
163 | |
164 | /// This object can be modified without requiring retains or |
165 | /// releases. |
166 | OCL_ExplicitNone, |
167 | |
168 | /// Assigning into this object requires the old value to be |
169 | /// released and the new value to be retained. The timing of the |
170 | /// release of the old value is inexact: it may be moved to |
171 | /// immediately after the last known point where the value is |
172 | /// live. |
173 | OCL_Strong, |
174 | |
175 | /// Reading or writing from this object requires a barrier call. |
176 | OCL_Weak, |
177 | |
178 | /// Assigning into this object requires a lifetime extension. |
179 | OCL_Autoreleasing |
180 | }; |
181 | |
182 | enum { |
183 | /// The maximum supported address space number. |
184 | /// 23 bits should be enough for anyone. |
185 | MaxAddressSpace = 0x7fffffu, |
186 | |
187 | /// The width of the "fast" qualifier mask. |
188 | FastWidth = 3, |
189 | |
190 | /// The fast qualifier mask. |
191 | FastMask = (1 << FastWidth) - 1 |
192 | }; |
193 | |
194 | /// Returns the common set of qualifiers while removing them from |
195 | /// the given sets. |
196 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { |
197 | // If both are only CVR-qualified, bit operations are sufficient. |
198 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { |
199 | Qualifiers Q; |
200 | Q.Mask = L.Mask & R.Mask; |
201 | L.Mask &= ~Q.Mask; |
202 | R.Mask &= ~Q.Mask; |
203 | return Q; |
204 | } |
205 | |
206 | Qualifiers Q; |
207 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); |
208 | Q.addCVRQualifiers(CommonCRV); |
209 | L.removeCVRQualifiers(CommonCRV); |
210 | R.removeCVRQualifiers(CommonCRV); |
211 | |
212 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { |
213 | Q.setObjCGCAttr(L.getObjCGCAttr()); |
214 | L.removeObjCGCAttr(); |
215 | R.removeObjCGCAttr(); |
216 | } |
217 | |
218 | if (L.getObjCLifetime() == R.getObjCLifetime()) { |
219 | Q.setObjCLifetime(L.getObjCLifetime()); |
220 | L.removeObjCLifetime(); |
221 | R.removeObjCLifetime(); |
222 | } |
223 | |
224 | if (L.getAddressSpace() == R.getAddressSpace()) { |
225 | Q.setAddressSpace(L.getAddressSpace()); |
226 | L.removeAddressSpace(); |
227 | R.removeAddressSpace(); |
228 | } |
229 | return Q; |
230 | } |
231 | |
232 | static Qualifiers fromFastMask(unsigned Mask) { |
233 | Qualifiers Qs; |
234 | Qs.addFastQualifiers(Mask); |
235 | return Qs; |
236 | } |
237 | |
238 | static Qualifiers fromCVRMask(unsigned CVR) { |
239 | Qualifiers Qs; |
240 | Qs.addCVRQualifiers(CVR); |
241 | return Qs; |
242 | } |
243 | |
244 | static Qualifiers fromCVRUMask(unsigned CVRU) { |
245 | Qualifiers Qs; |
246 | Qs.addCVRUQualifiers(CVRU); |
247 | return Qs; |
248 | } |
249 | |
250 | // Deserialize qualifiers from an opaque representation. |
251 | static Qualifiers fromOpaqueValue(unsigned opaque) { |
252 | Qualifiers Qs; |
253 | Qs.Mask = opaque; |
254 | return Qs; |
255 | } |
256 | |
257 | // Serialize these qualifiers into an opaque representation. |
258 | unsigned getAsOpaqueValue() const { |
259 | return Mask; |
260 | } |
261 | |
262 | bool hasConst() const { return Mask & Const; } |
263 | bool hasOnlyConst() const { return Mask == Const; } |
264 | void removeConst() { Mask &= ~Const; } |
265 | void addConst() { Mask |= Const; } |
266 | |
267 | bool hasVolatile() const { return Mask & Volatile; } |
268 | bool hasOnlyVolatile() const { return Mask == Volatile; } |
269 | void removeVolatile() { Mask &= ~Volatile; } |
270 | void addVolatile() { Mask |= Volatile; } |
271 | |
272 | bool hasRestrict() const { return Mask & Restrict; } |
273 | bool hasOnlyRestrict() const { return Mask == Restrict; } |
274 | void removeRestrict() { Mask &= ~Restrict; } |
275 | void addRestrict() { Mask |= Restrict; } |
276 | |
277 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } |
278 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } |
279 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } |
280 | |
281 | void setCVRQualifiers(unsigned mask) { |
282 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 282, __PRETTY_FUNCTION__)); |
283 | Mask = (Mask & ~CVRMask) | mask; |
284 | } |
285 | void removeCVRQualifiers(unsigned mask) { |
286 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 286, __PRETTY_FUNCTION__)); |
287 | Mask &= ~mask; |
288 | } |
289 | void removeCVRQualifiers() { |
290 | removeCVRQualifiers(CVRMask); |
291 | } |
292 | void addCVRQualifiers(unsigned mask) { |
293 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 293, __PRETTY_FUNCTION__)); |
294 | Mask |= mask; |
295 | } |
296 | void addCVRUQualifiers(unsigned mask) { |
297 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits")((!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask & ~UMask) && \"bitmask contains non-CVRU bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 297, __PRETTY_FUNCTION__)); |
298 | Mask |= mask; |
299 | } |
300 | |
301 | bool hasUnaligned() const { return Mask & UMask; } |
302 | void setUnaligned(bool flag) { |
303 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); |
304 | } |
305 | void removeUnaligned() { Mask &= ~UMask; } |
306 | void addUnaligned() { Mask |= UMask; } |
307 | |
308 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } |
309 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } |
310 | void setObjCGCAttr(GC type) { |
311 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); |
312 | } |
313 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } |
314 | void addObjCGCAttr(GC type) { |
315 | assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 315, __PRETTY_FUNCTION__)); |
316 | setObjCGCAttr(type); |
317 | } |
318 | Qualifiers withoutObjCGCAttr() const { |
319 | Qualifiers qs = *this; |
320 | qs.removeObjCGCAttr(); |
321 | return qs; |
322 | } |
323 | Qualifiers withoutObjCLifetime() const { |
324 | Qualifiers qs = *this; |
325 | qs.removeObjCLifetime(); |
326 | return qs; |
327 | } |
328 | Qualifiers withoutAddressSpace() const { |
329 | Qualifiers qs = *this; |
330 | qs.removeAddressSpace(); |
331 | return qs; |
332 | } |
333 | |
334 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } |
335 | ObjCLifetime getObjCLifetime() const { |
336 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); |
337 | } |
338 | void setObjCLifetime(ObjCLifetime type) { |
339 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); |
340 | } |
341 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } |
342 | void addObjCLifetime(ObjCLifetime type) { |
343 | assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 343, __PRETTY_FUNCTION__)); |
344 | assert(!hasObjCLifetime())((!hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("!hasObjCLifetime()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 344, __PRETTY_FUNCTION__)); |
345 | Mask |= (type << LifetimeShift); |
346 | } |
347 | |
348 | /// True if the lifetime is neither None or ExplicitNone. |
349 | bool hasNonTrivialObjCLifetime() const { |
350 | ObjCLifetime lifetime = getObjCLifetime(); |
351 | return (lifetime > OCL_ExplicitNone); |
352 | } |
353 | |
354 | /// True if the lifetime is either strong or weak. |
355 | bool hasStrongOrWeakObjCLifetime() const { |
356 | ObjCLifetime lifetime = getObjCLifetime(); |
357 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); |
358 | } |
359 | |
360 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } |
361 | LangAS getAddressSpace() const { |
362 | return static_cast<LangAS>(Mask >> AddressSpaceShift); |
363 | } |
364 | bool hasTargetSpecificAddressSpace() const { |
365 | return isTargetAddressSpace(getAddressSpace()); |
366 | } |
367 | /// Get the address space attribute value to be printed by diagnostics. |
368 | unsigned getAddressSpaceAttributePrintValue() const { |
369 | auto Addr = getAddressSpace(); |
370 | // This function is not supposed to be used with language specific |
371 | // address spaces. If that happens, the diagnostic message should consider |
372 | // printing the QualType instead of the address space value. |
373 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace())((Addr == LangAS::Default || hasTargetSpecificAddressSpace()) ? static_cast<void> (0) : __assert_fail ("Addr == LangAS::Default || hasTargetSpecificAddressSpace()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 373, __PRETTY_FUNCTION__)); |
374 | if (Addr != LangAS::Default) |
375 | return toTargetAddressSpace(Addr); |
376 | // TODO: The diagnostic messages where Addr may be 0 should be fixed |
377 | // since it cannot differentiate the situation where 0 denotes the default |
378 | // address space or user specified __attribute__((address_space(0))). |
379 | return 0; |
380 | } |
381 | void setAddressSpace(LangAS space) { |
382 | assert((unsigned)space <= MaxAddressSpace)(((unsigned)space <= MaxAddressSpace) ? static_cast<void > (0) : __assert_fail ("(unsigned)space <= MaxAddressSpace" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 382, __PRETTY_FUNCTION__)); |
383 | Mask = (Mask & ~AddressSpaceMask) |
384 | | (((uint32_t) space) << AddressSpaceShift); |
385 | } |
386 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } |
387 | void addAddressSpace(LangAS space) { |
388 | assert(space != LangAS::Default)((space != LangAS::Default) ? static_cast<void> (0) : __assert_fail ("space != LangAS::Default", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 388, __PRETTY_FUNCTION__)); |
389 | setAddressSpace(space); |
390 | } |
391 | |
392 | // Fast qualifiers are those that can be allocated directly |
393 | // on a QualType object. |
394 | bool hasFastQualifiers() const { return getFastQualifiers(); } |
395 | unsigned getFastQualifiers() const { return Mask & FastMask; } |
396 | void setFastQualifiers(unsigned mask) { |
397 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 397, __PRETTY_FUNCTION__)); |
398 | Mask = (Mask & ~FastMask) | mask; |
399 | } |
400 | void removeFastQualifiers(unsigned mask) { |
401 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 401, __PRETTY_FUNCTION__)); |
402 | Mask &= ~mask; |
403 | } |
404 | void removeFastQualifiers() { |
405 | removeFastQualifiers(FastMask); |
406 | } |
407 | void addFastQualifiers(unsigned mask) { |
408 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 408, __PRETTY_FUNCTION__)); |
409 | Mask |= mask; |
410 | } |
411 | |
412 | /// Return true if the set contains any qualifiers which require an ExtQuals |
413 | /// node to be allocated. |
414 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } |
415 | Qualifiers getNonFastQualifiers() const { |
416 | Qualifiers Quals = *this; |
417 | Quals.setFastQualifiers(0); |
418 | return Quals; |
419 | } |
420 | |
421 | /// Return true if the set contains any qualifiers. |
422 | bool hasQualifiers() const { return Mask; } |
423 | bool empty() const { return !Mask; } |
424 | |
425 | /// Add the qualifiers from the given set to this set. |
426 | void addQualifiers(Qualifiers Q) { |
427 | // If the other set doesn't have any non-boolean qualifiers, just |
428 | // bit-or it in. |
429 | if (!(Q.Mask & ~CVRMask)) |
430 | Mask |= Q.Mask; |
431 | else { |
432 | Mask |= (Q.Mask & CVRMask); |
433 | if (Q.hasAddressSpace()) |
434 | addAddressSpace(Q.getAddressSpace()); |
435 | if (Q.hasObjCGCAttr()) |
436 | addObjCGCAttr(Q.getObjCGCAttr()); |
437 | if (Q.hasObjCLifetime()) |
438 | addObjCLifetime(Q.getObjCLifetime()); |
439 | } |
440 | } |
441 | |
442 | /// Remove the qualifiers from the given set from this set. |
443 | void removeQualifiers(Qualifiers Q) { |
444 | // If the other set doesn't have any non-boolean qualifiers, just |
445 | // bit-and the inverse in. |
446 | if (!(Q.Mask & ~CVRMask)) |
447 | Mask &= ~Q.Mask; |
448 | else { |
449 | Mask &= ~(Q.Mask & CVRMask); |
450 | if (getObjCGCAttr() == Q.getObjCGCAttr()) |
451 | removeObjCGCAttr(); |
452 | if (getObjCLifetime() == Q.getObjCLifetime()) |
453 | removeObjCLifetime(); |
454 | if (getAddressSpace() == Q.getAddressSpace()) |
455 | removeAddressSpace(); |
456 | } |
457 | } |
458 | |
459 | /// Add the qualifiers from the given set to this set, given that |
460 | /// they don't conflict. |
461 | void addConsistentQualifiers(Qualifiers qs) { |
462 | assert(getAddressSpace() == qs.getAddressSpace() ||((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace () || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 463, __PRETTY_FUNCTION__)) |
463 | !hasAddressSpace() || !qs.hasAddressSpace())((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace () || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 463, __PRETTY_FUNCTION__)); |
464 | assert(getObjCGCAttr() == qs.getObjCGCAttr() ||((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 465, __PRETTY_FUNCTION__)) |
465 | !hasObjCGCAttr() || !qs.hasObjCGCAttr())((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 465, __PRETTY_FUNCTION__)); |
466 | assert(getObjCLifetime() == qs.getObjCLifetime() ||((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime () || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 467, __PRETTY_FUNCTION__)) |
467 | !hasObjCLifetime() || !qs.hasObjCLifetime())((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime () || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 467, __PRETTY_FUNCTION__)); |
468 | Mask |= qs.Mask; |
469 | } |
470 | |
471 | /// Returns true if address space A is equal to or a superset of B. |
472 | /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of |
473 | /// overlapping address spaces. |
474 | /// CL1.1 or CL1.2: |
475 | /// every address space is a superset of itself. |
476 | /// CL2.0 adds: |
477 | /// __generic is a superset of any address space except for __constant. |
478 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { |
479 | // Address spaces must match exactly. |
480 | return A == B || |
481 | // Otherwise in OpenCLC v2.0 s6.5.5: every address space except |
482 | // for __constant can be used as __generic. |
483 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
484 | // We also define global_device and global_host address spaces, |
485 | // to distinguish global pointers allocated on host from pointers |
486 | // allocated on device, which are a subset of __global. |
487 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || |
488 | B == LangAS::opencl_global_host)) || |
489 | // Consider pointer size address spaces to be equivalent to default. |
490 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && |
491 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)); |
492 | } |
493 | |
494 | /// Returns true if the address space in these qualifiers is equal to or |
495 | /// a superset of the address space in the argument qualifiers. |
496 | bool isAddressSpaceSupersetOf(Qualifiers other) const { |
497 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); |
498 | } |
499 | |
500 | /// Determines if these qualifiers compatibly include another set. |
501 | /// Generally this answers the question of whether an object with the other |
502 | /// qualifiers can be safely used as an object with these qualifiers. |
503 | bool compatiblyIncludes(Qualifiers other) const { |
504 | return isAddressSpaceSupersetOf(other) && |
505 | // ObjC GC qualifiers can match, be added, or be removed, but can't |
506 | // be changed. |
507 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || |
508 | !other.hasObjCGCAttr()) && |
509 | // ObjC lifetime qualifiers must match exactly. |
510 | getObjCLifetime() == other.getObjCLifetime() && |
511 | // CVR qualifiers may subset. |
512 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && |
513 | // U qualifier may superset. |
514 | (!other.hasUnaligned() || hasUnaligned()); |
515 | } |
516 | |
517 | /// Determines if these qualifiers compatibly include another set of |
518 | /// qualifiers from the narrow perspective of Objective-C ARC lifetime. |
519 | /// |
520 | /// One set of Objective-C lifetime qualifiers compatibly includes the other |
521 | /// if the lifetime qualifiers match, or if both are non-__weak and the |
522 | /// including set also contains the 'const' qualifier, or both are non-__weak |
523 | /// and one is None (which can only happen in non-ARC modes). |
524 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { |
525 | if (getObjCLifetime() == other.getObjCLifetime()) |
526 | return true; |
527 | |
528 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) |
529 | return false; |
530 | |
531 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) |
532 | return true; |
533 | |
534 | return hasConst(); |
535 | } |
536 | |
537 | /// Determine whether this set of qualifiers is a strict superset of |
538 | /// another set of qualifiers, not considering qualifier compatibility. |
539 | bool isStrictSupersetOf(Qualifiers Other) const; |
540 | |
541 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } |
542 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } |
543 | |
544 | explicit operator bool() const { return hasQualifiers(); } |
545 | |
546 | Qualifiers &operator+=(Qualifiers R) { |
547 | addQualifiers(R); |
548 | return *this; |
549 | } |
550 | |
551 | // Union two qualifier sets. If an enumerated qualifier appears |
552 | // in both sets, use the one from the right. |
553 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { |
554 | L += R; |
555 | return L; |
556 | } |
557 | |
558 | Qualifiers &operator-=(Qualifiers R) { |
559 | removeQualifiers(R); |
560 | return *this; |
561 | } |
562 | |
563 | /// Compute the difference between two qualifier sets. |
564 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { |
565 | L -= R; |
566 | return L; |
567 | } |
568 | |
569 | std::string getAsString() const; |
570 | std::string getAsString(const PrintingPolicy &Policy) const; |
571 | |
572 | static std::string getAddrSpaceAsString(LangAS AS); |
573 | |
574 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; |
575 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
576 | bool appendSpaceIfNonEmpty = false) const; |
577 | |
578 | void Profile(llvm::FoldingSetNodeID &ID) const { |
579 | ID.AddInteger(Mask); |
580 | } |
581 | |
582 | private: |
583 | // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31| |
584 | // |C R V|U|GCAttr|Lifetime|AddressSpace| |
585 | uint32_t Mask = 0; |
586 | |
587 | static const uint32_t UMask = 0x8; |
588 | static const uint32_t UShift = 3; |
589 | static const uint32_t GCAttrMask = 0x30; |
590 | static const uint32_t GCAttrShift = 4; |
591 | static const uint32_t LifetimeMask = 0x1C0; |
592 | static const uint32_t LifetimeShift = 6; |
593 | static const uint32_t AddressSpaceMask = |
594 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); |
595 | static const uint32_t AddressSpaceShift = 9; |
596 | }; |
597 | |
598 | /// A std::pair-like structure for storing a qualified type split |
599 | /// into its local qualifiers and its locally-unqualified type. |
600 | struct SplitQualType { |
601 | /// The locally-unqualified type. |
602 | const Type *Ty = nullptr; |
603 | |
604 | /// The local qualifiers. |
605 | Qualifiers Quals; |
606 | |
607 | SplitQualType() = default; |
608 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} |
609 | |
610 | SplitQualType getSingleStepDesugaredType() const; // end of this file |
611 | |
612 | // Make std::tie work. |
613 | std::pair<const Type *,Qualifiers> asPair() const { |
614 | return std::pair<const Type *, Qualifiers>(Ty, Quals); |
615 | } |
616 | |
617 | friend bool operator==(SplitQualType a, SplitQualType b) { |
618 | return a.Ty == b.Ty && a.Quals == b.Quals; |
619 | } |
620 | friend bool operator!=(SplitQualType a, SplitQualType b) { |
621 | return a.Ty != b.Ty || a.Quals != b.Quals; |
622 | } |
623 | }; |
624 | |
625 | /// The kind of type we are substituting Objective-C type arguments into. |
626 | /// |
627 | /// The kind of substitution affects the replacement of type parameters when |
628 | /// no concrete type information is provided, e.g., when dealing with an |
629 | /// unspecialized type. |
630 | enum class ObjCSubstitutionContext { |
631 | /// An ordinary type. |
632 | Ordinary, |
633 | |
634 | /// The result type of a method or function. |
635 | Result, |
636 | |
637 | /// The parameter type of a method or function. |
638 | Parameter, |
639 | |
640 | /// The type of a property. |
641 | Property, |
642 | |
643 | /// The superclass of a type. |
644 | Superclass, |
645 | }; |
646 | |
647 | /// A (possibly-)qualified type. |
648 | /// |
649 | /// For efficiency, we don't store CV-qualified types as nodes on their |
650 | /// own: instead each reference to a type stores the qualifiers. This |
651 | /// greatly reduces the number of nodes we need to allocate for types (for |
652 | /// example we only need one for 'int', 'const int', 'volatile int', |
653 | /// 'const volatile int', etc). |
654 | /// |
655 | /// As an added efficiency bonus, instead of making this a pair, we |
656 | /// just store the two bits we care about in the low bits of the |
657 | /// pointer. To handle the packing/unpacking, we make QualType be a |
658 | /// simple wrapper class that acts like a smart pointer. A third bit |
659 | /// indicates whether there are extended qualifiers present, in which |
660 | /// case the pointer points to a special structure. |
661 | class QualType { |
662 | friend class QualifierCollector; |
663 | |
664 | // Thankfully, these are efficiently composable. |
665 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, |
666 | Qualifiers::FastWidth> Value; |
667 | |
668 | const ExtQuals *getExtQualsUnsafe() const { |
669 | return Value.getPointer().get<const ExtQuals*>(); |
670 | } |
671 | |
672 | const Type *getTypePtrUnsafe() const { |
673 | return Value.getPointer().get<const Type*>(); |
674 | } |
675 | |
676 | const ExtQualsTypeCommonBase *getCommonPtr() const { |
677 | assert(!isNull() && "Cannot retrieve a NULL type pointer")((!isNull() && "Cannot retrieve a NULL type pointer") ? static_cast<void> (0) : __assert_fail ("!isNull() && \"Cannot retrieve a NULL type pointer\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 677, __PRETTY_FUNCTION__)); |
678 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); |
679 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); |
680 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); |
681 | } |
682 | |
683 | public: |
684 | QualType() = default; |
685 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
686 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
687 | |
688 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } |
689 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } |
690 | |
691 | /// Retrieves a pointer to the underlying (unqualified) type. |
692 | /// |
693 | /// This function requires that the type not be NULL. If the type might be |
694 | /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). |
695 | const Type *getTypePtr() const; |
696 | |
697 | const Type *getTypePtrOrNull() const; |
698 | |
699 | /// Retrieves a pointer to the name of the base type. |
700 | const IdentifierInfo *getBaseTypeIdentifier() const; |
701 | |
702 | /// Divides a QualType into its unqualified type and a set of local |
703 | /// qualifiers. |
704 | SplitQualType split() const; |
705 | |
706 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
707 | |
708 | static QualType getFromOpaquePtr(const void *Ptr) { |
709 | QualType T; |
710 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); |
711 | return T; |
712 | } |
713 | |
714 | const Type &operator*() const { |
715 | return *getTypePtr(); |
716 | } |
717 | |
718 | const Type *operator->() const { |
719 | return getTypePtr(); |
720 | } |
721 | |
722 | bool isCanonical() const; |
723 | bool isCanonicalAsParam() const; |
724 | |
725 | /// Return true if this QualType doesn't point to a type yet. |
726 | bool isNull() const { |
727 | return Value.getPointer().isNull(); |
728 | } |
729 | |
730 | /// Determine whether this particular QualType instance has the |
731 | /// "const" qualifier set, without looking through typedefs that may have |
732 | /// added "const" at a different level. |
733 | bool isLocalConstQualified() const { |
734 | return (getLocalFastQualifiers() & Qualifiers::Const); |
735 | } |
736 | |
737 | /// Determine whether this type is const-qualified. |
738 | bool isConstQualified() const; |
739 | |
740 | /// Determine whether this particular QualType instance has the |
741 | /// "restrict" qualifier set, without looking through typedefs that may have |
742 | /// added "restrict" at a different level. |
743 | bool isLocalRestrictQualified() const { |
744 | return (getLocalFastQualifiers() & Qualifiers::Restrict); |
745 | } |
746 | |
747 | /// Determine whether this type is restrict-qualified. |
748 | bool isRestrictQualified() const; |
749 | |
750 | /// Determine whether this particular QualType instance has the |
751 | /// "volatile" qualifier set, without looking through typedefs that may have |
752 | /// added "volatile" at a different level. |
753 | bool isLocalVolatileQualified() const { |
754 | return (getLocalFastQualifiers() & Qualifiers::Volatile); |
755 | } |
756 | |
757 | /// Determine whether this type is volatile-qualified. |
758 | bool isVolatileQualified() const; |
759 | |
760 | /// Determine whether this particular QualType instance has any |
761 | /// qualifiers, without looking through any typedefs that might add |
762 | /// qualifiers at a different level. |
763 | bool hasLocalQualifiers() const { |
764 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); |
765 | } |
766 | |
767 | /// Determine whether this type has any qualifiers. |
768 | bool hasQualifiers() const; |
769 | |
770 | /// Determine whether this particular QualType instance has any |
771 | /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType |
772 | /// instance. |
773 | bool hasLocalNonFastQualifiers() const { |
774 | return Value.getPointer().is<const ExtQuals*>(); |
775 | } |
776 | |
777 | /// Retrieve the set of qualifiers local to this particular QualType |
778 | /// instance, not including any qualifiers acquired through typedefs or |
779 | /// other sugar. |
780 | Qualifiers getLocalQualifiers() const; |
781 | |
782 | /// Retrieve the set of qualifiers applied to this type. |
783 | Qualifiers getQualifiers() const; |
784 | |
785 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers |
786 | /// local to this particular QualType instance, not including any qualifiers |
787 | /// acquired through typedefs or other sugar. |
788 | unsigned getLocalCVRQualifiers() const { |
789 | return getLocalFastQualifiers(); |
790 | } |
791 | |
792 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers |
793 | /// applied to this type. |
794 | unsigned getCVRQualifiers() const; |
795 | |
796 | bool isConstant(const ASTContext& Ctx) const { |
797 | return QualType::isConstant(*this, Ctx); |
798 | } |
799 | |
800 | /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). |
801 | bool isPODType(const ASTContext &Context) const; |
802 | |
803 | /// Return true if this is a POD type according to the rules of the C++98 |
804 | /// standard, regardless of the current compilation's language. |
805 | bool isCXX98PODType(const ASTContext &Context) const; |
806 | |
807 | /// Return true if this is a POD type according to the more relaxed rules |
808 | /// of the C++11 standard, regardless of the current compilation's language. |
809 | /// (C++0x [basic.types]p9). Note that, unlike |
810 | /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account. |
811 | bool isCXX11PODType(const ASTContext &Context) const; |
812 | |
813 | /// Return true if this is a trivial type per (C++0x [basic.types]p9) |
814 | bool isTrivialType(const ASTContext &Context) const; |
815 | |
816 | /// Return true if this is a trivially copyable type (C++0x [basic.types]p9) |
817 | bool isTriviallyCopyableType(const ASTContext &Context) const; |
818 | |
819 | |
820 | /// Returns true if it is a class and it might be dynamic. |
821 | bool mayBeDynamicClass() const; |
822 | |
823 | /// Returns true if it is not a class or if the class might not be dynamic. |
824 | bool mayBeNotDynamicClass() const; |
825 | |
826 | // Don't promise in the API that anything besides 'const' can be |
827 | // easily added. |
828 | |
829 | /// Add the `const` type qualifier to this QualType. |
830 | void addConst() { |
831 | addFastQualifiers(Qualifiers::Const); |
832 | } |
833 | QualType withConst() const { |
834 | return withFastQualifiers(Qualifiers::Const); |
835 | } |
836 | |
837 | /// Add the `volatile` type qualifier to this QualType. |
838 | void addVolatile() { |
839 | addFastQualifiers(Qualifiers::Volatile); |
840 | } |
841 | QualType withVolatile() const { |
842 | return withFastQualifiers(Qualifiers::Volatile); |
843 | } |
844 | |
845 | /// Add the `restrict` qualifier to this QualType. |
846 | void addRestrict() { |
847 | addFastQualifiers(Qualifiers::Restrict); |
848 | } |
849 | QualType withRestrict() const { |
850 | return withFastQualifiers(Qualifiers::Restrict); |
851 | } |
852 | |
853 | QualType withCVRQualifiers(unsigned CVR) const { |
854 | return withFastQualifiers(CVR); |
855 | } |
856 | |
857 | void addFastQualifiers(unsigned TQs) { |
858 | assert(!(TQs & ~Qualifiers::FastMask)((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!" ) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 859, __PRETTY_FUNCTION__)) |
859 | && "non-fast qualifier bits set in mask!")((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!" ) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 859, __PRETTY_FUNCTION__)); |
860 | Value.setInt(Value.getInt() | TQs); |
861 | } |
862 | |
863 | void removeLocalConst(); |
864 | void removeLocalVolatile(); |
865 | void removeLocalRestrict(); |
866 | void removeLocalCVRQualifiers(unsigned Mask); |
867 | |
868 | void removeLocalFastQualifiers() { Value.setInt(0); } |
869 | void removeLocalFastQualifiers(unsigned Mask) { |
870 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers")((!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::FastMask) && \"mask has non-fast qualifiers\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 870, __PRETTY_FUNCTION__)); |
871 | Value.setInt(Value.getInt() & ~Mask); |
872 | } |
873 | |
874 | // Creates a type with the given qualifiers in addition to any |
875 | // qualifiers already on this type. |
876 | QualType withFastQualifiers(unsigned TQs) const { |
877 | QualType T = *this; |
878 | T.addFastQualifiers(TQs); |
879 | return T; |
880 | } |
881 | |
882 | // Creates a type with exactly the given fast qualifiers, removing |
883 | // any existing fast qualifiers. |
884 | QualType withExactLocalFastQualifiers(unsigned TQs) const { |
885 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); |
886 | } |
887 | |
888 | // Removes fast qualifiers, but leaves any extended qualifiers in place. |
889 | QualType withoutLocalFastQualifiers() const { |
890 | QualType T = *this; |
891 | T.removeLocalFastQualifiers(); |
892 | return T; |
893 | } |
894 | |
895 | QualType getCanonicalType() const; |
896 | |
897 | /// Return this type with all of the instance-specific qualifiers |
898 | /// removed, but without removing any qualifiers that may have been applied |
899 | /// through typedefs. |
900 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } |
901 | |
902 | /// Retrieve the unqualified variant of the given type, |
903 | /// removing as little sugar as possible. |
904 | /// |
905 | /// This routine looks through various kinds of sugar to find the |
906 | /// least-desugared type that is unqualified. For example, given: |
907 | /// |
908 | /// \code |
909 | /// typedef int Integer; |
910 | /// typedef const Integer CInteger; |
911 | /// typedef CInteger DifferenceType; |
912 | /// \endcode |
913 | /// |
914 | /// Executing \c getUnqualifiedType() on the type \c DifferenceType will |
915 | /// desugar until we hit the type \c Integer, which has no qualifiers on it. |
916 | /// |
917 | /// The resulting type might still be qualified if it's sugar for an array |
918 | /// type. To strip qualifiers even from within a sugared array type, use |
919 | /// ASTContext::getUnqualifiedArrayType. |
920 | inline QualType getUnqualifiedType() const; |
921 | |
922 | /// Retrieve the unqualified variant of the given type, removing as little |
923 | /// sugar as possible. |
924 | /// |
925 | /// Like getUnqualifiedType(), but also returns the set of |
926 | /// qualifiers that were built up. |
927 | /// |
928 | /// The resulting type might still be qualified if it's sugar for an array |
929 | /// type. To strip qualifiers even from within a sugared array type, use |
930 | /// ASTContext::getUnqualifiedArrayType. |
931 | inline SplitQualType getSplitUnqualifiedType() const; |
932 | |
933 | /// Determine whether this type is more qualified than the other |
934 | /// given type, requiring exact equality for non-CVR qualifiers. |
935 | bool isMoreQualifiedThan(QualType Other) const; |
936 | |
937 | /// Determine whether this type is at least as qualified as the other |
938 | /// given type, requiring exact equality for non-CVR qualifiers. |
939 | bool isAtLeastAsQualifiedAs(QualType Other) const; |
940 | |
941 | QualType getNonReferenceType() const; |
942 | |
943 | /// Determine the type of a (typically non-lvalue) expression with the |
944 | /// specified result type. |
945 | /// |
946 | /// This routine should be used for expressions for which the return type is |
947 | /// explicitly specified (e.g., in a cast or call) and isn't necessarily |
948 | /// an lvalue. It removes a top-level reference (since there are no |
949 | /// expressions of reference type) and deletes top-level cvr-qualifiers |
950 | /// from non-class types (in C++) or all types (in C). |
951 | QualType getNonLValueExprType(const ASTContext &Context) const; |
952 | |
953 | /// Remove an outer pack expansion type (if any) from this type. Used as part |
954 | /// of converting the type of a declaration to the type of an expression that |
955 | /// references that expression. It's meaningless for an expression to have a |
956 | /// pack expansion type. |
957 | QualType getNonPackExpansionType() const; |
958 | |
959 | /// Return the specified type with any "sugar" removed from |
960 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
961 | /// the type is already concrete, it returns it unmodified. This is similar |
962 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
963 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
964 | /// concrete. |
965 | /// |
966 | /// Qualifiers are left in place. |
967 | QualType getDesugaredType(const ASTContext &Context) const { |
968 | return getDesugaredType(*this, Context); |
969 | } |
970 | |
971 | SplitQualType getSplitDesugaredType() const { |
972 | return getSplitDesugaredType(*this); |
973 | } |
974 | |
975 | /// Return the specified type with one level of "sugar" removed from |
976 | /// the type. |
977 | /// |
978 | /// This routine takes off the first typedef, typeof, etc. If the outer level |
979 | /// of the type is already concrete, it returns it unmodified. |
980 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { |
981 | return getSingleStepDesugaredTypeImpl(*this, Context); |
982 | } |
983 | |
984 | /// Returns the specified type after dropping any |
985 | /// outer-level parentheses. |
986 | QualType IgnoreParens() const { |
987 | if (isa<ParenType>(*this)) |
988 | return QualType::IgnoreParens(*this); |
989 | return *this; |
990 | } |
991 | |
992 | /// Indicate whether the specified types and qualifiers are identical. |
993 | friend bool operator==(const QualType &LHS, const QualType &RHS) { |
994 | return LHS.Value == RHS.Value; |
995 | } |
996 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { |
997 | return LHS.Value != RHS.Value; |
998 | } |
999 | friend bool operator<(const QualType &LHS, const QualType &RHS) { |
1000 | return LHS.Value < RHS.Value; |
1001 | } |
1002 | |
1003 | static std::string getAsString(SplitQualType split, |
1004 | const PrintingPolicy &Policy) { |
1005 | return getAsString(split.Ty, split.Quals, Policy); |
1006 | } |
1007 | static std::string getAsString(const Type *ty, Qualifiers qs, |
1008 | const PrintingPolicy &Policy); |
1009 | |
1010 | std::string getAsString() const; |
1011 | std::string getAsString(const PrintingPolicy &Policy) const; |
1012 | |
1013 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
1014 | const Twine &PlaceHolder = Twine(), |
1015 | unsigned Indentation = 0) const; |
1016 | |
1017 | static void print(SplitQualType split, raw_ostream &OS, |
1018 | const PrintingPolicy &policy, const Twine &PlaceHolder, |
1019 | unsigned Indentation = 0) { |
1020 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); |
1021 | } |
1022 | |
1023 | static void print(const Type *ty, Qualifiers qs, |
1024 | raw_ostream &OS, const PrintingPolicy &policy, |
1025 | const Twine &PlaceHolder, |
1026 | unsigned Indentation = 0); |
1027 | |
1028 | void getAsStringInternal(std::string &Str, |
1029 | const PrintingPolicy &Policy) const; |
1030 | |
1031 | static void getAsStringInternal(SplitQualType split, std::string &out, |
1032 | const PrintingPolicy &policy) { |
1033 | return getAsStringInternal(split.Ty, split.Quals, out, policy); |
1034 | } |
1035 | |
1036 | static void getAsStringInternal(const Type *ty, Qualifiers qs, |
1037 | std::string &out, |
1038 | const PrintingPolicy &policy); |
1039 | |
1040 | class StreamedQualTypeHelper { |
1041 | const QualType &T; |
1042 | const PrintingPolicy &Policy; |
1043 | const Twine &PlaceHolder; |
1044 | unsigned Indentation; |
1045 | |
1046 | public: |
1047 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, |
1048 | const Twine &PlaceHolder, unsigned Indentation) |
1049 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), |
1050 | Indentation(Indentation) {} |
1051 | |
1052 | friend raw_ostream &operator<<(raw_ostream &OS, |
1053 | const StreamedQualTypeHelper &SQT) { |
1054 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); |
1055 | return OS; |
1056 | } |
1057 | }; |
1058 | |
1059 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, |
1060 | const Twine &PlaceHolder = Twine(), |
1061 | unsigned Indentation = 0) const { |
1062 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); |
1063 | } |
1064 | |
1065 | void dump(const char *s) const; |
1066 | void dump() const; |
1067 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
1068 | |
1069 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1070 | ID.AddPointer(getAsOpaquePtr()); |
1071 | } |
1072 | |
1073 | /// Check if this type has any address space qualifier. |
1074 | inline bool hasAddressSpace() const; |
1075 | |
1076 | /// Return the address space of this type. |
1077 | inline LangAS getAddressSpace() const; |
1078 | |
1079 | /// Returns true if address space qualifiers overlap with T address space |
1080 | /// qualifiers. |
1081 | /// OpenCL C defines conversion rules for pointers to different address spaces |
1082 | /// and notion of overlapping address spaces. |
1083 | /// CL1.1 or CL1.2: |
1084 | /// address spaces overlap iff they are they same. |
1085 | /// OpenCL C v2.0 s6.5.5 adds: |
1086 | /// __generic overlaps with any address space except for __constant. |
1087 | bool isAddressSpaceOverlapping(QualType T) const { |
1088 | Qualifiers Q = getQualifiers(); |
1089 | Qualifiers TQ = T.getQualifiers(); |
1090 | // Address spaces overlap if at least one of them is a superset of another |
1091 | return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q); |
1092 | } |
1093 | |
1094 | /// Returns gc attribute of this type. |
1095 | inline Qualifiers::GC getObjCGCAttr() const; |
1096 | |
1097 | /// true when Type is objc's weak. |
1098 | bool isObjCGCWeak() const { |
1099 | return getObjCGCAttr() == Qualifiers::Weak; |
1100 | } |
1101 | |
1102 | /// true when Type is objc's strong. |
1103 | bool isObjCGCStrong() const { |
1104 | return getObjCGCAttr() == Qualifiers::Strong; |
1105 | } |
1106 | |
1107 | /// Returns lifetime attribute of this type. |
1108 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1109 | return getQualifiers().getObjCLifetime(); |
1110 | } |
1111 | |
1112 | bool hasNonTrivialObjCLifetime() const { |
1113 | return getQualifiers().hasNonTrivialObjCLifetime(); |
1114 | } |
1115 | |
1116 | bool hasStrongOrWeakObjCLifetime() const { |
1117 | return getQualifiers().hasStrongOrWeakObjCLifetime(); |
1118 | } |
1119 | |
1120 | // true when Type is objc's weak and weak is enabled but ARC isn't. |
1121 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; |
1122 | |
1123 | enum PrimitiveDefaultInitializeKind { |
1124 | /// The type does not fall into any of the following categories. Note that |
1125 | /// this case is zero-valued so that values of this enum can be used as a |
1126 | /// boolean condition for non-triviality. |
1127 | PDIK_Trivial, |
1128 | |
1129 | /// The type is an Objective-C retainable pointer type that is qualified |
1130 | /// with the ARC __strong qualifier. |
1131 | PDIK_ARCStrong, |
1132 | |
1133 | /// The type is an Objective-C retainable pointer type that is qualified |
1134 | /// with the ARC __weak qualifier. |
1135 | PDIK_ARCWeak, |
1136 | |
1137 | /// The type is a struct containing a field whose type is not PCK_Trivial. |
1138 | PDIK_Struct |
1139 | }; |
1140 | |
1141 | /// Functions to query basic properties of non-trivial C struct types. |
1142 | |
1143 | /// Check if this is a non-trivial type that would cause a C struct |
1144 | /// transitively containing this type to be non-trivial to default initialize |
1145 | /// and return the kind. |
1146 | PrimitiveDefaultInitializeKind |
1147 | isNonTrivialToPrimitiveDefaultInitialize() const; |
1148 | |
1149 | enum PrimitiveCopyKind { |
1150 | /// The type does not fall into any of the following categories. Note that |
1151 | /// this case is zero-valued so that values of this enum can be used as a |
1152 | /// boolean condition for non-triviality. |
1153 | PCK_Trivial, |
1154 | |
1155 | /// The type would be trivial except that it is volatile-qualified. Types |
1156 | /// that fall into one of the other non-trivial cases may additionally be |
1157 | /// volatile-qualified. |
1158 | PCK_VolatileTrivial, |
1159 | |
1160 | /// The type is an Objective-C retainable pointer type that is qualified |
1161 | /// with the ARC __strong qualifier. |
1162 | PCK_ARCStrong, |
1163 | |
1164 | /// The type is an Objective-C retainable pointer type that is qualified |
1165 | /// with the ARC __weak qualifier. |
1166 | PCK_ARCWeak, |
1167 | |
1168 | /// The type is a struct containing a field whose type is neither |
1169 | /// PCK_Trivial nor PCK_VolatileTrivial. |
1170 | /// Note that a C++ struct type does not necessarily match this; C++ copying |
1171 | /// semantics are too complex to express here, in part because they depend |
1172 | /// on the exact constructor or assignment operator that is chosen by |
1173 | /// overload resolution to do the copy. |
1174 | PCK_Struct |
1175 | }; |
1176 | |
1177 | /// Check if this is a non-trivial type that would cause a C struct |
1178 | /// transitively containing this type to be non-trivial to copy and return the |
1179 | /// kind. |
1180 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; |
1181 | |
1182 | /// Check if this is a non-trivial type that would cause a C struct |
1183 | /// transitively containing this type to be non-trivial to destructively |
1184 | /// move and return the kind. Destructive move in this context is a C++-style |
1185 | /// move in which the source object is placed in a valid but unspecified state |
1186 | /// after it is moved, as opposed to a truly destructive move in which the |
1187 | /// source object is placed in an uninitialized state. |
1188 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; |
1189 | |
1190 | enum DestructionKind { |
1191 | DK_none, |
1192 | DK_cxx_destructor, |
1193 | DK_objc_strong_lifetime, |
1194 | DK_objc_weak_lifetime, |
1195 | DK_nontrivial_c_struct |
1196 | }; |
1197 | |
1198 | /// Returns a nonzero value if objects of this type require |
1199 | /// non-trivial work to clean up after. Non-zero because it's |
1200 | /// conceivable that qualifiers (objc_gc(weak)?) could make |
1201 | /// something require destruction. |
1202 | DestructionKind isDestructedType() const { |
1203 | return isDestructedTypeImpl(*this); |
1204 | } |
1205 | |
1206 | /// Check if this is or contains a C union that is non-trivial to |
1207 | /// default-initialize, which is a union that has a member that is non-trivial |
1208 | /// to default-initialize. If this returns true, |
1209 | /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct. |
1210 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; |
1211 | |
1212 | /// Check if this is or contains a C union that is non-trivial to destruct, |
1213 | /// which is a union that has a member that is non-trivial to destruct. If |
1214 | /// this returns true, isDestructedType returns DK_nontrivial_c_struct. |
1215 | bool hasNonTrivialToPrimitiveDestructCUnion() const; |
1216 | |
1217 | /// Check if this is or contains a C union that is non-trivial to copy, which |
1218 | /// is a union that has a member that is non-trivial to copy. If this returns |
1219 | /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct. |
1220 | bool hasNonTrivialToPrimitiveCopyCUnion() const; |
1221 | |
1222 | /// Determine whether expressions of the given type are forbidden |
1223 | /// from being lvalues in C. |
1224 | /// |
1225 | /// The expression types that are forbidden to be lvalues are: |
1226 | /// - 'void', but not qualified void |
1227 | /// - function types |
1228 | /// |
1229 | /// The exact rule here is C99 6.3.2.1: |
1230 | /// An lvalue is an expression with an object type or an incomplete |
1231 | /// type other than void. |
1232 | bool isCForbiddenLValueType() const; |
1233 | |
1234 | /// Substitute type arguments for the Objective-C type parameters used in the |
1235 | /// subject type. |
1236 | /// |
1237 | /// \param ctx ASTContext in which the type exists. |
1238 | /// |
1239 | /// \param typeArgs The type arguments that will be substituted for the |
1240 | /// Objective-C type parameters in the subject type, which are generally |
1241 | /// computed via \c Type::getObjCSubstitutions. If empty, the type |
1242 | /// parameters will be replaced with their bounds or id/Class, as appropriate |
1243 | /// for the context. |
1244 | /// |
1245 | /// \param context The context in which the subject type was written. |
1246 | /// |
1247 | /// \returns the resulting type. |
1248 | QualType substObjCTypeArgs(ASTContext &ctx, |
1249 | ArrayRef<QualType> typeArgs, |
1250 | ObjCSubstitutionContext context) const; |
1251 | |
1252 | /// Substitute type arguments from an object type for the Objective-C type |
1253 | /// parameters used in the subject type. |
1254 | /// |
1255 | /// This operation combines the computation of type arguments for |
1256 | /// substitution (\c Type::getObjCSubstitutions) with the actual process of |
1257 | /// substitution (\c QualType::substObjCTypeArgs) for the convenience of |
1258 | /// callers that need to perform a single substitution in isolation. |
1259 | /// |
1260 | /// \param objectType The type of the object whose member type we're |
1261 | /// substituting into. For example, this might be the receiver of a message |
1262 | /// or the base of a property access. |
1263 | /// |
1264 | /// \param dc The declaration context from which the subject type was |
1265 | /// retrieved, which indicates (for example) which type parameters should |
1266 | /// be substituted. |
1267 | /// |
1268 | /// \param context The context in which the subject type was written. |
1269 | /// |
1270 | /// \returns the subject type after replacing all of the Objective-C type |
1271 | /// parameters with their corresponding arguments. |
1272 | QualType substObjCMemberType(QualType objectType, |
1273 | const DeclContext *dc, |
1274 | ObjCSubstitutionContext context) const; |
1275 | |
1276 | /// Strip Objective-C "__kindof" types from the given type. |
1277 | QualType stripObjCKindOfType(const ASTContext &ctx) const; |
1278 | |
1279 | /// Remove all qualifiers including _Atomic. |
1280 | QualType getAtomicUnqualifiedType() const; |
1281 | |
1282 | private: |
1283 | // These methods are implemented in a separate translation unit; |
1284 | // "static"-ize them to avoid creating temporary QualTypes in the |
1285 | // caller. |
1286 | static bool isConstant(QualType T, const ASTContext& Ctx); |
1287 | static QualType getDesugaredType(QualType T, const ASTContext &Context); |
1288 | static SplitQualType getSplitDesugaredType(QualType T); |
1289 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); |
1290 | static QualType getSingleStepDesugaredTypeImpl(QualType type, |
1291 | const ASTContext &C); |
1292 | static QualType IgnoreParens(QualType T); |
1293 | static DestructionKind isDestructedTypeImpl(QualType type); |
1294 | |
1295 | /// Check if \param RD is or contains a non-trivial C union. |
1296 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); |
1297 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); |
1298 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); |
1299 | }; |
1300 | |
1301 | } // namespace clang |
1302 | |
1303 | namespace llvm { |
1304 | |
1305 | /// Implement simplify_type for QualType, so that we can dyn_cast from QualType |
1306 | /// to a specific Type class. |
1307 | template<> struct simplify_type< ::clang::QualType> { |
1308 | using SimpleType = const ::clang::Type *; |
1309 | |
1310 | static SimpleType getSimplifiedValue(::clang::QualType Val) { |
1311 | return Val.getTypePtr(); |
1312 | } |
1313 | }; |
1314 | |
1315 | // Teach SmallPtrSet that QualType is "basically a pointer". |
1316 | template<> |
1317 | struct PointerLikeTypeTraits<clang::QualType> { |
1318 | static inline void *getAsVoidPointer(clang::QualType P) { |
1319 | return P.getAsOpaquePtr(); |
1320 | } |
1321 | |
1322 | static inline clang::QualType getFromVoidPointer(void *P) { |
1323 | return clang::QualType::getFromOpaquePtr(P); |
1324 | } |
1325 | |
1326 | // Various qualifiers go in low bits. |
1327 | static constexpr int NumLowBitsAvailable = 0; |
1328 | }; |
1329 | |
1330 | } // namespace llvm |
1331 | |
1332 | namespace clang { |
1333 | |
1334 | /// Base class that is common to both the \c ExtQuals and \c Type |
1335 | /// classes, which allows \c QualType to access the common fields between the |
1336 | /// two. |
1337 | class ExtQualsTypeCommonBase { |
1338 | friend class ExtQuals; |
1339 | friend class QualType; |
1340 | friend class Type; |
1341 | |
1342 | /// The "base" type of an extended qualifiers type (\c ExtQuals) or |
1343 | /// a self-referential pointer (for \c Type). |
1344 | /// |
1345 | /// This pointer allows an efficient mapping from a QualType to its |
1346 | /// underlying type pointer. |
1347 | const Type *const BaseType; |
1348 | |
1349 | /// The canonical type of this type. A QualType. |
1350 | QualType CanonicalType; |
1351 | |
1352 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) |
1353 | : BaseType(baseType), CanonicalType(canon) {} |
1354 | }; |
1355 | |
1356 | /// We can encode up to four bits in the low bits of a |
1357 | /// type pointer, but there are many more type qualifiers that we want |
1358 | /// to be able to apply to an arbitrary type. Therefore we have this |
1359 | /// struct, intended to be heap-allocated and used by QualType to |
1360 | /// store qualifiers. |
1361 | /// |
1362 | /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers |
1363 | /// in three low bits on the QualType pointer; a fourth bit records whether |
1364 | /// the pointer is an ExtQuals node. The extended qualifiers (address spaces, |
1365 | /// Objective-C GC attributes) are much more rare. |
1366 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { |
1367 | // NOTE: changing the fast qualifiers should be straightforward as |
1368 | // long as you don't make 'const' non-fast. |
1369 | // 1. Qualifiers: |
1370 | // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). |
1371 | // Fast qualifiers must occupy the low-order bits. |
1372 | // b) Update Qualifiers::FastWidth and FastMask. |
1373 | // 2. QualType: |
1374 | // a) Update is{Volatile,Restrict}Qualified(), defined inline. |
1375 | // b) Update remove{Volatile,Restrict}, defined near the end of |
1376 | // this header. |
1377 | // 3. ASTContext: |
1378 | // a) Update get{Volatile,Restrict}Type. |
1379 | |
1380 | /// The immutable set of qualifiers applied by this node. Always contains |
1381 | /// extended qualifiers. |
1382 | Qualifiers Quals; |
1383 | |
1384 | ExtQuals *this_() { return this; } |
1385 | |
1386 | public: |
1387 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) |
1388 | : ExtQualsTypeCommonBase(baseType, |
1389 | canon.isNull() ? QualType(this_(), 0) : canon), |
1390 | Quals(quals) { |
1391 | assert(Quals.hasNonFastQualifiers()((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1392, __PRETTY_FUNCTION__)) |
1392 | && "ExtQuals created with no fast qualifiers")((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1392, __PRETTY_FUNCTION__)); |
1393 | assert(!Quals.hasFastQualifiers()((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1394, __PRETTY_FUNCTION__)) |
1394 | && "ExtQuals created with fast qualifiers")((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1394, __PRETTY_FUNCTION__)); |
1395 | } |
1396 | |
1397 | Qualifiers getQualifiers() const { return Quals; } |
1398 | |
1399 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } |
1400 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } |
1401 | |
1402 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } |
1403 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1404 | return Quals.getObjCLifetime(); |
1405 | } |
1406 | |
1407 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } |
1408 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
1409 | |
1410 | const Type *getBaseType() const { return BaseType; } |
1411 | |
1412 | public: |
1413 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1414 | Profile(ID, getBaseType(), Quals); |
1415 | } |
1416 | |
1417 | static void Profile(llvm::FoldingSetNodeID &ID, |
1418 | const Type *BaseType, |
1419 | Qualifiers Quals) { |
1420 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!")((!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"fast qualifiers in ExtQuals hash!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1420, __PRETTY_FUNCTION__)); |
1421 | ID.AddPointer(BaseType); |
1422 | Quals.Profile(ID); |
1423 | } |
1424 | }; |
1425 | |
1426 | /// The kind of C++11 ref-qualifier associated with a function type. |
1427 | /// This determines whether a member function's "this" object can be an |
1428 | /// lvalue, rvalue, or neither. |
1429 | enum RefQualifierKind { |
1430 | /// No ref-qualifier was provided. |
1431 | RQ_None = 0, |
1432 | |
1433 | /// An lvalue ref-qualifier was provided (\c &). |
1434 | RQ_LValue, |
1435 | |
1436 | /// An rvalue ref-qualifier was provided (\c &&). |
1437 | RQ_RValue |
1438 | }; |
1439 | |
1440 | /// Which keyword(s) were used to create an AutoType. |
1441 | enum class AutoTypeKeyword { |
1442 | /// auto |
1443 | Auto, |
1444 | |
1445 | /// decltype(auto) |
1446 | DecltypeAuto, |
1447 | |
1448 | /// __auto_type (GNU extension) |
1449 | GNUAutoType |
1450 | }; |
1451 | |
1452 | /// The base class of the type hierarchy. |
1453 | /// |
1454 | /// A central concept with types is that each type always has a canonical |
1455 | /// type. A canonical type is the type with any typedef names stripped out |
1456 | /// of it or the types it references. For example, consider: |
1457 | /// |
1458 | /// typedef int foo; |
1459 | /// typedef foo* bar; |
1460 | /// 'int *' 'foo *' 'bar' |
1461 | /// |
1462 | /// There will be a Type object created for 'int'. Since int is canonical, its |
1463 | /// CanonicalType pointer points to itself. There is also a Type for 'foo' (a |
1464 | /// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next |
1465 | /// there is a PointerType that represents 'int*', which, like 'int', is |
1466 | /// canonical. Finally, there is a PointerType type for 'foo*' whose canonical |
1467 | /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type |
1468 | /// is also 'int*'. |
1469 | /// |
1470 | /// Non-canonical types are useful for emitting diagnostics, without losing |
1471 | /// information about typedefs being used. Canonical types are useful for type |
1472 | /// comparisons (they allow by-pointer equality tests) and useful for reasoning |
1473 | /// about whether something has a particular form (e.g. is a function type), |
1474 | /// because they implicitly, recursively, strip all typedefs out of a type. |
1475 | /// |
1476 | /// Types, once created, are immutable. |
1477 | /// |
1478 | class alignas(8) Type : public ExtQualsTypeCommonBase { |
1479 | public: |
1480 | enum TypeClass { |
1481 | #define TYPE(Class, Base) Class, |
1482 | #define LAST_TYPE(Class) TypeLast = Class |
1483 | #define ABSTRACT_TYPE(Class, Base) |
1484 | #include "clang/AST/TypeNodes.inc" |
1485 | }; |
1486 | |
1487 | private: |
1488 | /// Bitfields required by the Type class. |
1489 | class TypeBitfields { |
1490 | friend class Type; |
1491 | template <class T> friend class TypePropertyCache; |
1492 | |
1493 | /// TypeClass bitfield - Enum that specifies what subclass this belongs to. |
1494 | unsigned TC : 8; |
1495 | |
1496 | /// Store information on the type dependency. |
1497 | unsigned Dependence : llvm::BitWidth<TypeDependence>; |
1498 | |
1499 | /// True if the cache (i.e. the bitfields here starting with |
1500 | /// 'Cache') is valid. |
1501 | mutable unsigned CacheValid : 1; |
1502 | |
1503 | /// Linkage of this type. |
1504 | mutable unsigned CachedLinkage : 3; |
1505 | |
1506 | /// Whether this type involves and local or unnamed types. |
1507 | mutable unsigned CachedLocalOrUnnamed : 1; |
1508 | |
1509 | /// Whether this type comes from an AST file. |
1510 | mutable unsigned FromAST : 1; |
1511 | |
1512 | bool isCacheValid() const { |
1513 | return CacheValid; |
1514 | } |
1515 | |
1516 | Linkage getLinkage() const { |
1517 | assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache" ) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1517, __PRETTY_FUNCTION__)); |
1518 | return static_cast<Linkage>(CachedLinkage); |
1519 | } |
1520 | |
1521 | bool hasLocalOrUnnamedType() const { |
1522 | assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache" ) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 1522, __PRETTY_FUNCTION__)); |
1523 | return CachedLocalOrUnnamed; |
1524 | } |
1525 | }; |
1526 | enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; |
1527 | |
1528 | protected: |
1529 | // These classes allow subclasses to somewhat cleanly pack bitfields |
1530 | // into Type. |
1531 | |
1532 | class ArrayTypeBitfields { |
1533 | friend class ArrayType; |
1534 | |
1535 | unsigned : NumTypeBits; |
1536 | |
1537 | /// CVR qualifiers from declarations like |
1538 | /// 'int X[static restrict 4]'. For function parameters only. |
1539 | unsigned IndexTypeQuals : 3; |
1540 | |
1541 | /// Storage class qualifiers from declarations like |
1542 | /// 'int X[static restrict 4]'. For function parameters only. |
1543 | /// Actually an ArrayType::ArraySizeModifier. |
1544 | unsigned SizeModifier : 3; |
1545 | }; |
1546 | |
1547 | class ConstantArrayTypeBitfields { |
1548 | friend class ConstantArrayType; |
1549 | |
1550 | unsigned : NumTypeBits + 3 + 3; |
1551 | |
1552 | /// Whether we have a stored size expression. |
1553 | unsigned HasStoredSizeExpr : 1; |
1554 | }; |
1555 | |
1556 | class BuiltinTypeBitfields { |
1557 | friend class BuiltinType; |
1558 | |
1559 | unsigned : NumTypeBits; |
1560 | |
1561 | /// The kind (BuiltinType::Kind) of builtin type this is. |
1562 | unsigned Kind : 8; |
1563 | }; |
1564 | |
1565 | /// FunctionTypeBitfields store various bits belonging to FunctionProtoType. |
1566 | /// Only common bits are stored here. Additional uncommon bits are stored |
1567 | /// in a trailing object after FunctionProtoType. |
1568 | class FunctionTypeBitfields { |
1569 | friend class FunctionProtoType; |
1570 | friend class FunctionType; |
1571 | |
1572 | unsigned : NumTypeBits; |
1573 | |
1574 | /// Extra information which affects how the function is called, like |
1575 | /// regparm and the calling convention. |
1576 | unsigned ExtInfo : 13; |
1577 | |
1578 | /// The ref-qualifier associated with a \c FunctionProtoType. |
1579 | /// |
1580 | /// This is a value of type \c RefQualifierKind. |
1581 | unsigned RefQualifier : 2; |
1582 | |
1583 | /// Used only by FunctionProtoType, put here to pack with the |
1584 | /// other bitfields. |
1585 | /// The qualifiers are part of FunctionProtoType because... |
1586 | /// |
1587 | /// C++ 8.3.5p4: The return type, the parameter type list and the |
1588 | /// cv-qualifier-seq, [...], are part of the function type. |
1589 | unsigned FastTypeQuals : Qualifiers::FastWidth; |
1590 | /// Whether this function has extended Qualifiers. |
1591 | unsigned HasExtQuals : 1; |
1592 | |
1593 | /// The number of parameters this function has, not counting '...'. |
1594 | /// According to [implimits] 8 bits should be enough here but this is |
1595 | /// somewhat easy to exceed with metaprogramming and so we would like to |
1596 | /// keep NumParams as wide as reasonably possible. |
1597 | unsigned NumParams : 16; |
1598 | |
1599 | /// The type of exception specification this function has. |
1600 | unsigned ExceptionSpecType : 4; |
1601 | |
1602 | /// Whether this function has extended parameter information. |
1603 | unsigned HasExtParameterInfos : 1; |
1604 | |
1605 | /// Whether the function is variadic. |
1606 | unsigned Variadic : 1; |
1607 | |
1608 | /// Whether this function has a trailing return type. |
1609 | unsigned HasTrailingReturn : 1; |
1610 | }; |
1611 | |
1612 | class ObjCObjectTypeBitfields { |
1613 | friend class ObjCObjectType; |
1614 | |
1615 | unsigned : NumTypeBits; |
1616 | |
1617 | /// The number of type arguments stored directly on this object type. |
1618 | unsigned NumTypeArgs : 7; |
1619 | |
1620 | /// The number of protocols stored directly on this object type. |
1621 | unsigned NumProtocols : 6; |
1622 | |
1623 | /// Whether this is a "kindof" type. |
1624 | unsigned IsKindOf : 1; |
1625 | }; |
1626 | |
1627 | class ReferenceTypeBitfields { |
1628 | friend class ReferenceType; |
1629 | |
1630 | unsigned : NumTypeBits; |
1631 | |
1632 | /// True if the type was originally spelled with an lvalue sigil. |
1633 | /// This is never true of rvalue references but can also be false |
1634 | /// on lvalue references because of C++0x [dcl.typedef]p9, |
1635 | /// as follows: |
1636 | /// |
1637 | /// typedef int &ref; // lvalue, spelled lvalue |
1638 | /// typedef int &&rvref; // rvalue |
1639 | /// ref &a; // lvalue, inner ref, spelled lvalue |
1640 | /// ref &&a; // lvalue, inner ref |
1641 | /// rvref &a; // lvalue, inner ref, spelled lvalue |
1642 | /// rvref &&a; // rvalue, inner ref |
1643 | unsigned SpelledAsLValue : 1; |
1644 | |
1645 | /// True if the inner type is a reference type. This only happens |
1646 | /// in non-canonical forms. |
1647 | unsigned InnerRef : 1; |
1648 | }; |
1649 | |
1650 | class TypeWithKeywordBitfields { |
1651 | friend class TypeWithKeyword; |
1652 | |
1653 | unsigned : NumTypeBits; |
1654 | |
1655 | /// An ElaboratedTypeKeyword. 8 bits for efficient access. |
1656 | unsigned Keyword : 8; |
1657 | }; |
1658 | |
1659 | enum { NumTypeWithKeywordBits = 8 }; |
1660 | |
1661 | class ElaboratedTypeBitfields { |
1662 | friend class ElaboratedType; |
1663 | |
1664 | unsigned : NumTypeBits; |
1665 | unsigned : NumTypeWithKeywordBits; |
1666 | |
1667 | /// Whether the ElaboratedType has a trailing OwnedTagDecl. |
1668 | unsigned HasOwnedTagDecl : 1; |
1669 | }; |
1670 | |
1671 | class VectorTypeBitfields { |
1672 | friend class VectorType; |
1673 | friend class DependentVectorType; |
1674 | |
1675 | unsigned : NumTypeBits; |
1676 | |
1677 | /// The kind of vector, either a generic vector type or some |
1678 | /// target-specific vector type such as for AltiVec or Neon. |
1679 | unsigned VecKind : 3; |
1680 | /// The number of elements in the vector. |
1681 | uint32_t NumElements; |
1682 | }; |
1683 | |
1684 | class AttributedTypeBitfields { |
1685 | friend class AttributedType; |
1686 | |
1687 | unsigned : NumTypeBits; |
1688 | |
1689 | /// An AttributedType::Kind |
1690 | unsigned AttrKind : 32 - NumTypeBits; |
1691 | }; |
1692 | |
1693 | class AutoTypeBitfields { |
1694 | friend class AutoType; |
1695 | |
1696 | unsigned : NumTypeBits; |
1697 | |
1698 | /// Was this placeholder type spelled as 'auto', 'decltype(auto)', |
1699 | /// or '__auto_type'? AutoTypeKeyword value. |
1700 | unsigned Keyword : 2; |
1701 | |
1702 | /// The number of template arguments in the type-constraints, which is |
1703 | /// expected to be able to hold at least 1024 according to [implimits]. |
1704 | /// However as this limit is somewhat easy to hit with template |
1705 | /// metaprogramming we'd prefer to keep it as large as possible. |
1706 | /// At the moment it has been left as a non-bitfield since this type |
1707 | /// safely fits in 64 bits as an unsigned, so there is no reason to |
1708 | /// introduce the performance impact of a bitfield. |
1709 | unsigned NumArgs; |
1710 | }; |
1711 | |
1712 | class SubstTemplateTypeParmPackTypeBitfields { |
1713 | friend class SubstTemplateTypeParmPackType; |
1714 | |
1715 | unsigned : NumTypeBits; |
1716 | |
1717 | /// The number of template arguments in \c Arguments, which is |
1718 | /// expected to be able to hold at least 1024 according to [implimits]. |
1719 | /// However as this limit is somewhat easy to hit with template |
1720 | /// metaprogramming we'd prefer to keep it as large as possible. |
1721 | /// At the moment it has been left as a non-bitfield since this type |
1722 | /// safely fits in 64 bits as an unsigned, so there is no reason to |
1723 | /// introduce the performance impact of a bitfield. |
1724 | unsigned NumArgs; |
1725 | }; |
1726 | |
1727 | class TemplateSpecializationTypeBitfields { |
1728 | friend class TemplateSpecializationType; |
1729 | |
1730 | unsigned : NumTypeBits; |
1731 | |
1732 | /// Whether this template specialization type is a substituted type alias. |
1733 | unsigned TypeAlias : 1; |
1734 | |
1735 | /// The number of template arguments named in this class template |
1736 | /// specialization, which is expected to be able to hold at least 1024 |
1737 | /// according to [implimits]. However, as this limit is somewhat easy to |
1738 | /// hit with template metaprogramming we'd prefer to keep it as large |
1739 | /// as possible. At the moment it has been left as a non-bitfield since |
1740 | /// this type safely fits in 64 bits as an unsigned, so there is no reason |
1741 | /// to introduce the performance impact of a bitfield. |
1742 | unsigned NumArgs; |
1743 | }; |
1744 | |
1745 | class DependentTemplateSpecializationTypeBitfields { |
1746 | friend class DependentTemplateSpecializationType; |
1747 | |
1748 | unsigned : NumTypeBits; |
1749 | unsigned : NumTypeWithKeywordBits; |
1750 | |
1751 | /// The number of template arguments named in this class template |
1752 | /// specialization, which is expected to be able to hold at least 1024 |
1753 | /// according to [implimits]. However, as this limit is somewhat easy to |
1754 | /// hit with template metaprogramming we'd prefer to keep it as large |
1755 | /// as possible. At the moment it has been left as a non-bitfield since |
1756 | /// this type safely fits in 64 bits as an unsigned, so there is no reason |
1757 | /// to introduce the performance impact of a bitfield. |
1758 | unsigned NumArgs; |
1759 | }; |
1760 | |
1761 | class PackExpansionTypeBitfields { |
1762 | friend class PackExpansionType; |
1763 | |
1764 | unsigned : NumTypeBits; |
1765 | |
1766 | /// The number of expansions that this pack expansion will |
1767 | /// generate when substituted (+1), which is expected to be able to |
1768 | /// hold at least 1024 according to [implimits]. However, as this limit |
1769 | /// is somewhat easy to hit with template metaprogramming we'd prefer to |
1770 | /// keep it as large as possible. At the moment it has been left as a |
1771 | /// non-bitfield since this type safely fits in 64 bits as an unsigned, so |
1772 | /// there is no reason to introduce the performance impact of a bitfield. |
1773 | /// |
1774 | /// This field will only have a non-zero value when some of the parameter |
1775 | /// packs that occur within the pattern have been substituted but others |
1776 | /// have not. |
1777 | unsigned NumExpansions; |
1778 | }; |
1779 | |
1780 | union { |
1781 | TypeBitfields TypeBits; |
1782 | ArrayTypeBitfields ArrayTypeBits; |
1783 | ConstantArrayTypeBitfields ConstantArrayTypeBits; |
1784 | AttributedTypeBitfields AttributedTypeBits; |
1785 | AutoTypeBitfields AutoTypeBits; |
1786 | BuiltinTypeBitfields BuiltinTypeBits; |
1787 | FunctionTypeBitfields FunctionTypeBits; |
1788 | ObjCObjectTypeBitfields ObjCObjectTypeBits; |
1789 | ReferenceTypeBitfields ReferenceTypeBits; |
1790 | TypeWithKeywordBitfields TypeWithKeywordBits; |
1791 | ElaboratedTypeBitfields ElaboratedTypeBits; |
1792 | VectorTypeBitfields VectorTypeBits; |
1793 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; |
1794 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; |
1795 | DependentTemplateSpecializationTypeBitfields |
1796 | DependentTemplateSpecializationTypeBits; |
1797 | PackExpansionTypeBitfields PackExpansionTypeBits; |
1798 | }; |
1799 | |
1800 | private: |
1801 | template <class T> friend class TypePropertyCache; |
1802 | |
1803 | /// Set whether this type comes from an AST file. |
1804 | void setFromAST(bool V = true) const { |
1805 | TypeBits.FromAST = V; |
1806 | } |
1807 | |
1808 | protected: |
1809 | friend class ASTContext; |
1810 | |
1811 | Type(TypeClass tc, QualType canon, TypeDependence Dependence) |
1812 | : ExtQualsTypeCommonBase(this, |
1813 | canon.isNull() ? QualType(this_(), 0) : canon) { |
1814 | static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase), |
1815 | "changing bitfields changed sizeof(Type)!"); |
1816 | static_assert(alignof(decltype(*this)) % sizeof(void *) == 0, |
1817 | "Insufficient alignment!"); |
1818 | TypeBits.TC = tc; |
1819 | TypeBits.Dependence = static_cast<unsigned>(Dependence); |
1820 | TypeBits.CacheValid = false; |
1821 | TypeBits.CachedLocalOrUnnamed = false; |
1822 | TypeBits.CachedLinkage = NoLinkage; |
1823 | TypeBits.FromAST = false; |
1824 | } |
1825 | |
1826 | // silence VC++ warning C4355: 'this' : used in base member initializer list |
1827 | Type *this_() { return this; } |
1828 | |
1829 | void setDependence(TypeDependence D) { |
1830 | TypeBits.Dependence = static_cast<unsigned>(D); |
1831 | } |
1832 | |
1833 | void addDependence(TypeDependence D) { setDependence(getDependence() | D); } |
1834 | |
1835 | public: |
1836 | friend class ASTReader; |
1837 | friend class ASTWriter; |
1838 | template <class T> friend class serialization::AbstractTypeReader; |
1839 | template <class T> friend class serialization::AbstractTypeWriter; |
1840 | |
1841 | Type(const Type &) = delete; |
1842 | Type(Type &&) = delete; |
1843 | Type &operator=(const Type &) = delete; |
1844 | Type &operator=(Type &&) = delete; |
1845 | |
1846 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } |
1847 | |
1848 | /// Whether this type comes from an AST file. |
1849 | bool isFromAST() const { return TypeBits.FromAST; } |
1850 | |
1851 | /// Whether this type is or contains an unexpanded parameter |
1852 | /// pack, used to support C++0x variadic templates. |
1853 | /// |
1854 | /// A type that contains a parameter pack shall be expanded by the |
1855 | /// ellipsis operator at some point. For example, the typedef in the |
1856 | /// following example contains an unexpanded parameter pack 'T': |
1857 | /// |
1858 | /// \code |
1859 | /// template<typename ...T> |
1860 | /// struct X { |
1861 | /// typedef T* pointer_types; // ill-formed; T is a parameter pack. |
1862 | /// }; |
1863 | /// \endcode |
1864 | /// |
1865 | /// Note that this routine does not specify which |
1866 | bool containsUnexpandedParameterPack() const { |
1867 | return getDependence() & TypeDependence::UnexpandedPack; |
1868 | } |
1869 | |
1870 | /// Determines if this type would be canonical if it had no further |
1871 | /// qualification. |
1872 | bool isCanonicalUnqualified() const { |
1873 | return CanonicalType == QualType(this, 0); |
1874 | } |
1875 | |
1876 | /// Pull a single level of sugar off of this locally-unqualified type. |
1877 | /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() |
1878 | /// or QualType::getSingleStepDesugaredType(const ASTContext&). |
1879 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; |
1880 | |
1881 | /// As an extension, we classify types as one of "sized" or "sizeless"; |
1882 | /// every type is one or the other. Standard types are all sized; |
1883 | /// sizeless types are purely an extension. |
1884 | /// |
1885 | /// Sizeless types contain data with no specified size, alignment, |
1886 | /// or layout. |
1887 | bool isSizelessType() const; |
1888 | bool isSizelessBuiltinType() const; |
1889 | |
1890 | /// Determines if this is a sizeless type supported by the |
1891 | /// 'arm_sve_vector_bits' type attribute, which can be applied to a single |
1892 | /// SVE vector or predicate, excluding tuple types such as svint32x4_t. |
1893 | bool isVLSTBuiltinType() const; |
1894 | |
1895 | /// Returns the representative type for the element of an SVE builtin type. |
1896 | /// This is used to represent fixed-length SVE vectors created with the |
1897 | /// 'arm_sve_vector_bits' type attribute as VectorType. |
1898 | QualType getSveEltType(const ASTContext &Ctx) const; |
1899 | |
1900 | /// Types are partitioned into 3 broad categories (C99 6.2.5p1): |
1901 | /// object types, function types, and incomplete types. |
1902 | |
1903 | /// Return true if this is an incomplete type. |
1904 | /// A type that can describe objects, but which lacks information needed to |
1905 | /// determine its size (e.g. void, or a fwd declared struct). Clients of this |
1906 | /// routine will need to determine if the size is actually required. |
1907 | /// |
1908 | /// Def If non-null, and the type refers to some kind of declaration |
1909 | /// that can be completed (such as a C struct, C++ class, or Objective-C |
1910 | /// class), will be set to the declaration. |
1911 | bool isIncompleteType(NamedDecl **Def = nullptr) const; |
1912 | |
1913 | /// Return true if this is an incomplete or object |
1914 | /// type, in other words, not a function type. |
1915 | bool isIncompleteOrObjectType() const { |
1916 | return !isFunctionType(); |
1917 | } |
1918 | |
1919 | /// Determine whether this type is an object type. |
1920 | bool isObjectType() const { |
1921 | // C++ [basic.types]p8: |
1922 | // An object type is a (possibly cv-qualified) type that is not a |
1923 | // function type, not a reference type, and not a void type. |
1924 | return !isReferenceType() && !isFunctionType() && !isVoidType(); |
1925 | } |
1926 | |
1927 | /// Return true if this is a literal type |
1928 | /// (C++11 [basic.types]p10) |
1929 | bool isLiteralType(const ASTContext &Ctx) const; |
1930 | |
1931 | /// Determine if this type is a structural type, per C++20 [temp.param]p7. |
1932 | bool isStructuralType() const; |
1933 | |
1934 | /// Test if this type is a standard-layout type. |
1935 | /// (C++0x [basic.type]p9) |
1936 | bool isStandardLayoutType() const; |
1937 | |
1938 | /// Helper methods to distinguish type categories. All type predicates |
1939 | /// operate on the canonical type, ignoring typedefs and qualifiers. |
1940 | |
1941 | /// Returns true if the type is a builtin type. |
1942 | bool isBuiltinType() const; |
1943 | |
1944 | /// Test for a particular builtin type. |
1945 | bool isSpecificBuiltinType(unsigned K) const; |
1946 | |
1947 | /// Test for a type which does not represent an actual type-system type but |
1948 | /// is instead used as a placeholder for various convenient purposes within |
1949 | /// Clang. All such types are BuiltinTypes. |
1950 | bool isPlaceholderType() const; |
1951 | const BuiltinType *getAsPlaceholderType() const; |
1952 | |
1953 | /// Test for a specific placeholder type. |
1954 | bool isSpecificPlaceholderType(unsigned K) const; |
1955 | |
1956 | /// Test for a placeholder type other than Overload; see |
1957 | /// BuiltinType::isNonOverloadPlaceholderType. |
1958 | bool isNonOverloadPlaceholderType() const; |
1959 | |
1960 | /// isIntegerType() does *not* include complex integers (a GCC extension). |
1961 | /// isComplexIntegerType() can be used to test for complex integers. |
1962 | bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) |
1963 | bool isEnumeralType() const; |
1964 | |
1965 | /// Determine whether this type is a scoped enumeration type. |
1966 | bool isScopedEnumeralType() const; |
1967 | bool isBooleanType() const; |
1968 | bool isCharType() const; |
1969 | bool isWideCharType() const; |
1970 | bool isChar8Type() const; |
1971 | bool isChar16Type() const; |
1972 | bool isChar32Type() const; |
1973 | bool isAnyCharacterType() const; |
1974 | bool isIntegralType(const ASTContext &Ctx) const; |
1975 | |
1976 | /// Determine whether this type is an integral or enumeration type. |
1977 | bool isIntegralOrEnumerationType() const; |
1978 | |
1979 | /// Determine whether this type is an integral or unscoped enumeration type. |
1980 | bool isIntegralOrUnscopedEnumerationType() const; |
1981 | bool isUnscopedEnumerationType() const; |
1982 | |
1983 | /// Floating point categories. |
1984 | bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) |
1985 | /// isComplexType() does *not* include complex integers (a GCC extension). |
1986 | /// isComplexIntegerType() can be used to test for complex integers. |
1987 | bool isComplexType() const; // C99 6.2.5p11 (complex) |
1988 | bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. |
1989 | bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) |
1990 | bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) |
1991 | bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661 |
1992 | bool isBFloat16Type() const; |
1993 | bool isFloat128Type() const; |
1994 | bool isRealType() const; // C99 6.2.5p17 (real floating + integer) |
1995 | bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) |
1996 | bool isVoidType() const; // C99 6.2.5p19 |
1997 | bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) |
1998 | bool isAggregateType() const; |
1999 | bool isFundamentalType() const; |
2000 | bool isCompoundType() const; |
2001 | |
2002 | // Type Predicates: Check to see if this type is structurally the specified |
2003 | // type, ignoring typedefs and qualifiers. |
2004 | bool isFunctionType() const; |
2005 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } |
2006 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } |
2007 | bool isPointerType() const; |
2008 | bool isAnyPointerType() const; // Any C pointer or ObjC object pointer |
2009 | bool isBlockPointerType() const; |
2010 | bool isVoidPointerType() const; |
2011 | bool isReferenceType() const; |
2012 | bool isLValueReferenceType() const; |
2013 | bool isRValueReferenceType() const; |
2014 | bool isObjectPointerType() const; |
2015 | bool isFunctionPointerType() const; |
2016 | bool isFunctionReferenceType() const; |
2017 | bool isMemberPointerType() const; |
2018 | bool isMemberFunctionPointerType() const; |
2019 | bool isMemberDataPointerType() const; |
2020 | bool isArrayType() const; |
2021 | bool isConstantArrayType() const; |
2022 | bool isIncompleteArrayType() const; |
2023 | bool isVariableArrayType() const; |
2024 | bool isDependentSizedArrayType() const; |
2025 | bool isRecordType() const; |
2026 | bool isClassType() const; |
2027 | bool isStructureType() const; |
2028 | bool isObjCBoxableRecordType() const; |
2029 | bool isInterfaceType() const; |
2030 | bool isStructureOrClassType() const; |
2031 | bool isUnionType() const; |
2032 | bool isComplexIntegerType() const; // GCC _Complex integer type. |
2033 | bool isVectorType() const; // GCC vector type. |
2034 | bool isExtVectorType() const; // Extended vector type. |
2035 | bool isMatrixType() const; // Matrix type. |
2036 | bool isConstantMatrixType() const; // Constant matrix type. |
2037 | bool isDependentAddressSpaceType() const; // value-dependent address space qualifier |
2038 | bool isObjCObjectPointerType() const; // pointer to ObjC object |
2039 | bool isObjCRetainableType() const; // ObjC object or block pointer |
2040 | bool isObjCLifetimeType() const; // (array of)* retainable type |
2041 | bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type |
2042 | bool isObjCNSObjectType() const; // __attribute__((NSObject)) |
2043 | bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class)) |
2044 | // FIXME: change this to 'raw' interface type, so we can used 'interface' type |
2045 | // for the common case. |
2046 | bool isObjCObjectType() const; // NSString or typeof(*(id)0) |
2047 | bool isObjCQualifiedInterfaceType() const; // NSString<foo> |
2048 | bool isObjCQualifiedIdType() const; // id<foo> |
2049 | bool isObjCQualifiedClassType() const; // Class<foo> |
2050 | bool isObjCObjectOrInterfaceType() const; |
2051 | bool isObjCIdType() const; // id |
2052 | bool isDecltypeType() const; |
2053 | /// Was this type written with the special inert-in-ARC __unsafe_unretained |
2054 | /// qualifier? |
2055 | /// |
2056 | /// This approximates the answer to the following question: if this |
2057 | /// translation unit were compiled in ARC, would this type be qualified |
2058 | /// with __unsafe_unretained? |
2059 | bool isObjCInertUnsafeUnretainedType() const { |
2060 | return hasAttr(attr::ObjCInertUnsafeUnretained); |
2061 | } |
2062 | |
2063 | /// Whether the type is Objective-C 'id' or a __kindof type of an |
2064 | /// object type, e.g., __kindof NSView * or __kindof id |
2065 | /// <NSCopying>. |
2066 | /// |
2067 | /// \param bound Will be set to the bound on non-id subtype types, |
2068 | /// which will be (possibly specialized) Objective-C class type, or |
2069 | /// null for 'id. |
2070 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
2071 | const ObjCObjectType *&bound) const; |
2072 | |
2073 | bool isObjCClassType() const; // Class |
2074 | |
2075 | /// Whether the type is Objective-C 'Class' or a __kindof type of an |
2076 | /// Class type, e.g., __kindof Class <NSCopying>. |
2077 | /// |
2078 | /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound |
2079 | /// here because Objective-C's type system cannot express "a class |
2080 | /// object for a subclass of NSFoo". |
2081 | bool isObjCClassOrClassKindOfType() const; |
2082 | |
2083 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; |
2084 | bool isObjCSelType() const; // Class |
2085 | bool isObjCBuiltinType() const; // 'id' or 'Class' |
2086 | bool isObjCARCBridgableType() const; |
2087 | bool isCARCBridgableType() const; |
2088 | bool isTemplateTypeParmType() const; // C++ template type parameter |
2089 | bool isNullPtrType() const; // C++11 std::nullptr_t |
2090 | bool isNothrowT() const; // C++ std::nothrow_t |
2091 | bool isAlignValT() const; // C++17 std::align_val_t |
2092 | bool isStdByteType() const; // C++17 std::byte |
2093 | bool isAtomicType() const; // C11 _Atomic() |
2094 | bool isUndeducedAutoType() const; // C++11 auto or |
2095 | // C++14 decltype(auto) |
2096 | bool isTypedefNameType() const; // typedef or alias template |
2097 | |
2098 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2099 | bool is##Id##Type() const; |
2100 | #include "clang/Basic/OpenCLImageTypes.def" |
2101 | |
2102 | bool isImageType() const; // Any OpenCL image type |
2103 | |
2104 | bool isSamplerT() const; // OpenCL sampler_t |
2105 | bool isEventT() const; // OpenCL event_t |
2106 | bool isClkEventT() const; // OpenCL clk_event_t |
2107 | bool isQueueT() const; // OpenCL queue_t |
2108 | bool isReserveIDT() const; // OpenCL reserve_id_t |
2109 | |
2110 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2111 | bool is##Id##Type() const; |
2112 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2113 | // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension |
2114 | bool isOCLIntelSubgroupAVCType() const; |
2115 | bool isOCLExtOpaqueType() const; // Any OpenCL extension type |
2116 | |
2117 | bool isPipeType() const; // OpenCL pipe type |
2118 | bool isExtIntType() const; // Extended Int Type |
2119 | bool isOpenCLSpecificType() const; // Any OpenCL specific type |
2120 | |
2121 | /// Determines if this type, which must satisfy |
2122 | /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather |
2123 | /// than implicitly __strong. |
2124 | bool isObjCARCImplicitlyUnretainedType() const; |
2125 | |
2126 | /// Check if the type is the CUDA device builtin surface type. |
2127 | bool isCUDADeviceBuiltinSurfaceType() const; |
2128 | /// Check if the type is the CUDA device builtin texture type. |
2129 | bool isCUDADeviceBuiltinTextureType() const; |
2130 | |
2131 | /// Return the implicit lifetime for this type, which must not be dependent. |
2132 | Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; |
2133 | |
2134 | enum ScalarTypeKind { |
2135 | STK_CPointer, |
2136 | STK_BlockPointer, |
2137 | STK_ObjCObjectPointer, |
2138 | STK_MemberPointer, |
2139 | STK_Bool, |
2140 | STK_Integral, |
2141 | STK_Floating, |
2142 | STK_IntegralComplex, |
2143 | STK_FloatingComplex, |
2144 | STK_FixedPoint |
2145 | }; |
2146 | |
2147 | /// Given that this is a scalar type, classify it. |
2148 | ScalarTypeKind getScalarTypeKind() const; |
2149 | |
2150 | TypeDependence getDependence() const { |
2151 | return static_cast<TypeDependence>(TypeBits.Dependence); |
2152 | } |
2153 | |
2154 | /// Whether this type is an error type. |
2155 | bool containsErrors() const { |
2156 | return getDependence() & TypeDependence::Error; |
2157 | } |
2158 | |
2159 | /// Whether this type is a dependent type, meaning that its definition |
2160 | /// somehow depends on a template parameter (C++ [temp.dep.type]). |
2161 | bool isDependentType() const { |
2162 | return getDependence() & TypeDependence::Dependent; |
2163 | } |
2164 | |
2165 | /// Determine whether this type is an instantiation-dependent type, |
2166 | /// meaning that the type involves a template parameter (even if the |
2167 | /// definition does not actually depend on the type substituted for that |
2168 | /// template parameter). |
2169 | bool isInstantiationDependentType() const { |
2170 | return getDependence() & TypeDependence::Instantiation; |
2171 | } |
2172 | |
2173 | /// Determine whether this type is an undeduced type, meaning that |
2174 | /// it somehow involves a C++11 'auto' type or similar which has not yet been |
2175 | /// deduced. |
2176 | bool isUndeducedType() const; |
2177 | |
2178 | /// Whether this type is a variably-modified type (C99 6.7.5). |
2179 | bool isVariablyModifiedType() const { |
2180 | return getDependence() & TypeDependence::VariablyModified; |
2181 | } |
2182 | |
2183 | /// Whether this type involves a variable-length array type |
2184 | /// with a definite size. |
2185 | bool hasSizedVLAType() const; |
2186 | |
2187 | /// Whether this type is or contains a local or unnamed type. |
2188 | bool hasUnnamedOrLocalType() const; |
2189 | |
2190 | bool isOverloadableType() const; |
2191 | |
2192 | /// Determine wither this type is a C++ elaborated-type-specifier. |
2193 | bool isElaboratedTypeSpecifier() const; |
2194 | |
2195 | bool canDecayToPointerType() const; |
2196 | |
2197 | /// Whether this type is represented natively as a pointer. This includes |
2198 | /// pointers, references, block pointers, and Objective-C interface, |
2199 | /// qualified id, and qualified interface types, as well as nullptr_t. |
2200 | bool hasPointerRepresentation() const; |
2201 | |
2202 | /// Whether this type can represent an objective pointer type for the |
2203 | /// purpose of GC'ability |
2204 | bool hasObjCPointerRepresentation() const; |
2205 | |
2206 | /// Determine whether this type has an integer representation |
2207 | /// of some sort, e.g., it is an integer type or a vector. |
2208 | bool hasIntegerRepresentation() const; |
2209 | |
2210 | /// Determine whether this type has an signed integer representation |
2211 | /// of some sort, e.g., it is an signed integer type or a vector. |
2212 | bool hasSignedIntegerRepresentation() const; |
2213 | |
2214 | /// Determine whether this type has an unsigned integer representation |
2215 | /// of some sort, e.g., it is an unsigned integer type or a vector. |
2216 | bool hasUnsignedIntegerRepresentation() const; |
2217 | |
2218 | /// Determine whether this type has a floating-point representation |
2219 | /// of some sort, e.g., it is a floating-point type or a vector thereof. |
2220 | bool hasFloatingRepresentation() const; |
2221 | |
2222 | // Type Checking Functions: Check to see if this type is structurally the |
2223 | // specified type, ignoring typedefs and qualifiers, and return a pointer to |
2224 | // the best type we can. |
2225 | const RecordType *getAsStructureType() const; |
2226 | /// NOTE: getAs*ArrayType are methods on ASTContext. |
2227 | const RecordType *getAsUnionType() const; |
2228 | const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. |
2229 | const ObjCObjectType *getAsObjCInterfaceType() const; |
2230 | |
2231 | // The following is a convenience method that returns an ObjCObjectPointerType |
2232 | // for object declared using an interface. |
2233 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; |
2234 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; |
2235 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; |
2236 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; |
2237 | |
2238 | /// Retrieves the CXXRecordDecl that this type refers to, either |
2239 | /// because the type is a RecordType or because it is the injected-class-name |
2240 | /// type of a class template or class template partial specialization. |
2241 | CXXRecordDecl *getAsCXXRecordDecl() const; |
2242 | |
2243 | /// Retrieves the RecordDecl this type refers to. |
2244 | RecordDecl *getAsRecordDecl() const; |
2245 | |
2246 | /// Retrieves the TagDecl that this type refers to, either |
2247 | /// because the type is a TagType or because it is the injected-class-name |
2248 | /// type of a class template or class template partial specialization. |
2249 | TagDecl *getAsTagDecl() const; |
2250 | |
2251 | /// If this is a pointer or reference to a RecordType, return the |
2252 | /// CXXRecordDecl that the type refers to. |
2253 | /// |
2254 | /// If this is not a pointer or reference, or the type being pointed to does |
2255 | /// not refer to a CXXRecordDecl, returns NULL. |
2256 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; |
2257 | |
2258 | /// Get the DeducedType whose type will be deduced for a variable with |
2259 | /// an initializer of this type. This looks through declarators like pointer |
2260 | /// types, but not through decltype or typedefs. |
2261 | DeducedType *getContainedDeducedType() const; |
2262 | |
2263 | /// Get the AutoType whose type will be deduced for a variable with |
2264 | /// an initializer of this type. This looks through declarators like pointer |
2265 | /// types, but not through decltype or typedefs. |
2266 | AutoType *getContainedAutoType() const { |
2267 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); |
2268 | } |
2269 | |
2270 | /// Determine whether this type was written with a leading 'auto' |
2271 | /// corresponding to a trailing return type (possibly for a nested |
2272 | /// function type within a pointer to function type or similar). |
2273 | bool hasAutoForTrailingReturnType() const; |
2274 | |
2275 | /// Member-template getAs<specific type>'. Look through sugar for |
2276 | /// an instance of \<specific type>. This scheme will eventually |
2277 | /// replace the specific getAsXXXX methods above. |
2278 | /// |
2279 | /// There are some specializations of this member template listed |
2280 | /// immediately following this class. |
2281 | template <typename T> const T *getAs() const; |
2282 | |
2283 | /// Member-template getAsAdjusted<specific type>. Look through specific kinds |
2284 | /// of sugar (parens, attributes, etc) for an instance of \<specific type>. |
2285 | /// This is used when you need to walk over sugar nodes that represent some |
2286 | /// kind of type adjustment from a type that was written as a \<specific type> |
2287 | /// to another type that is still canonically a \<specific type>. |
2288 | template <typename T> const T *getAsAdjusted() const; |
2289 | |
2290 | /// A variant of getAs<> for array types which silently discards |
2291 | /// qualifiers from the outermost type. |
2292 | const ArrayType *getAsArrayTypeUnsafe() const; |
2293 | |
2294 | /// Member-template castAs<specific type>. Look through sugar for |
2295 | /// the underlying instance of \<specific type>. |
2296 | /// |
2297 | /// This method has the same relationship to getAs<T> as cast<T> has |
2298 | /// to dyn_cast<T>; which is to say, the underlying type *must* |
2299 | /// have the intended type, and this method will never return null. |
2300 | template <typename T> const T *castAs() const; |
2301 | |
2302 | /// A variant of castAs<> for array type which silently discards |
2303 | /// qualifiers from the outermost type. |
2304 | const ArrayType *castAsArrayTypeUnsafe() const; |
2305 | |
2306 | /// Determine whether this type had the specified attribute applied to it |
2307 | /// (looking through top-level type sugar). |
2308 | bool hasAttr(attr::Kind AK) const; |
2309 | |
2310 | /// Get the base element type of this type, potentially discarding type |
2311 | /// qualifiers. This should never be used when type qualifiers |
2312 | /// are meaningful. |
2313 | const Type *getBaseElementTypeUnsafe() const; |
2314 | |
2315 | /// If this is an array type, return the element type of the array, |
2316 | /// potentially with type qualifiers missing. |
2317 | /// This should never be used when type qualifiers are meaningful. |
2318 | const Type *getArrayElementTypeNoTypeQual() const; |
2319 | |
2320 | /// If this is a pointer type, return the pointee type. |
2321 | /// If this is an array type, return the array element type. |
2322 | /// This should never be used when type qualifiers are meaningful. |
2323 | const Type *getPointeeOrArrayElementType() const; |
2324 | |
2325 | /// If this is a pointer, ObjC object pointer, or block |
2326 | /// pointer, this returns the respective pointee. |
2327 | QualType getPointeeType() const; |
2328 | |
2329 | /// Return the specified type with any "sugar" removed from the type, |
2330 | /// removing any typedefs, typeofs, etc., as well as any qualifiers. |
2331 | const Type *getUnqualifiedDesugaredType() const; |
2332 | |
2333 | /// More type predicates useful for type checking/promotion |
2334 | bool isPromotableIntegerType() const; // C99 6.3.1.1p2 |
2335 | |
2336 | /// Return true if this is an integer type that is |
2337 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
2338 | /// or an enum decl which has a signed representation. |
2339 | bool isSignedIntegerType() const; |
2340 | |
2341 | /// Return true if this is an integer type that is |
2342 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], |
2343 | /// or an enum decl which has an unsigned representation. |
2344 | bool isUnsignedIntegerType() const; |
2345 | |
2346 | /// Determines whether this is an integer type that is signed or an |
2347 | /// enumeration types whose underlying type is a signed integer type. |
2348 | bool isSignedIntegerOrEnumerationType() const; |
2349 | |
2350 | /// Determines whether this is an integer type that is unsigned or an |
2351 | /// enumeration types whose underlying type is a unsigned integer type. |
2352 | bool isUnsignedIntegerOrEnumerationType() const; |
2353 | |
2354 | /// Return true if this is a fixed point type according to |
2355 | /// ISO/IEC JTC1 SC22 WG14 N1169. |
2356 | bool isFixedPointType() const; |
2357 | |
2358 | /// Return true if this is a fixed point or integer type. |
2359 | bool isFixedPointOrIntegerType() const; |
2360 | |
2361 | /// Return true if this is a saturated fixed point type according to |
2362 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. |
2363 | bool isSaturatedFixedPointType() const; |
2364 | |
2365 | /// Return true if this is a saturated fixed point type according to |
2366 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. |
2367 | bool isUnsaturatedFixedPointType() const; |
2368 | |
2369 | /// Return true if this is a fixed point type that is signed according |
2370 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. |
2371 | bool isSignedFixedPointType() const; |
2372 | |
2373 | /// Return true if this is a fixed point type that is unsigned according |
2374 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. |
2375 | bool isUnsignedFixedPointType() const; |
2376 | |
2377 | /// Return true if this is not a variable sized type, |
2378 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
2379 | /// incomplete types. |
2380 | bool isConstantSizeType() const; |
2381 | |
2382 | /// Returns true if this type can be represented by some |
2383 | /// set of type specifiers. |
2384 | bool isSpecifierType() const; |
2385 | |
2386 | /// Determine the linkage of this type. |
2387 | Linkage getLinkage() const; |
2388 | |
2389 | /// Determine the visibility of this type. |
2390 | Visibility getVisibility() const { |
2391 | return getLinkageAndVisibility().getVisibility(); |
2392 | } |
2393 | |
2394 | /// Return true if the visibility was explicitly set is the code. |
2395 | bool isVisibilityExplicit() const { |
2396 | return getLinkageAndVisibility().isVisibilityExplicit(); |
2397 | } |
2398 | |
2399 | /// Determine the linkage and visibility of this type. |
2400 | LinkageInfo getLinkageAndVisibility() const; |
2401 | |
2402 | /// True if the computed linkage is valid. Used for consistency |
2403 | /// checking. Should always return true. |
2404 | bool isLinkageValid() const; |
2405 | |
2406 | /// Determine the nullability of the given type. |
2407 | /// |
2408 | /// Note that nullability is only captured as sugar within the type |
2409 | /// system, not as part of the canonical type, so nullability will |
2410 | /// be lost by canonicalization and desugaring. |
2411 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; |
2412 | |
2413 | /// Determine whether the given type can have a nullability |
2414 | /// specifier applied to it, i.e., if it is any kind of pointer type. |
2415 | /// |
2416 | /// \param ResultIfUnknown The value to return if we don't yet know whether |
2417 | /// this type can have nullability because it is dependent. |
2418 | bool canHaveNullability(bool ResultIfUnknown = true) const; |
2419 | |
2420 | /// Retrieve the set of substitutions required when accessing a member |
2421 | /// of the Objective-C receiver type that is declared in the given context. |
2422 | /// |
2423 | /// \c *this is the type of the object we're operating on, e.g., the |
2424 | /// receiver for a message send or the base of a property access, and is |
2425 | /// expected to be of some object or object pointer type. |
2426 | /// |
2427 | /// \param dc The declaration context for which we are building up a |
2428 | /// substitution mapping, which should be an Objective-C class, extension, |
2429 | /// category, or method within. |
2430 | /// |
2431 | /// \returns an array of type arguments that can be substituted for |
2432 | /// the type parameters of the given declaration context in any type described |
2433 | /// within that context, or an empty optional to indicate that no |
2434 | /// substitution is required. |
2435 | Optional<ArrayRef<QualType>> |
2436 | getObjCSubstitutions(const DeclContext *dc) const; |
2437 | |
2438 | /// Determines if this is an ObjC interface type that may accept type |
2439 | /// parameters. |
2440 | bool acceptsObjCTypeParams() const; |
2441 | |
2442 | const char *getTypeClassName() const; |
2443 | |
2444 | QualType getCanonicalTypeInternal() const { |
2445 | return CanonicalType; |
2446 | } |
2447 | |
2448 | CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h |
2449 | void dump() const; |
2450 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
2451 | }; |
2452 | |
2453 | /// This will check for a TypedefType by removing any existing sugar |
2454 | /// until it reaches a TypedefType or a non-sugared type. |
2455 | template <> const TypedefType *Type::getAs() const; |
2456 | |
2457 | /// This will check for a TemplateSpecializationType by removing any |
2458 | /// existing sugar until it reaches a TemplateSpecializationType or a |
2459 | /// non-sugared type. |
2460 | template <> const TemplateSpecializationType *Type::getAs() const; |
2461 | |
2462 | /// This will check for an AttributedType by removing any existing sugar |
2463 | /// until it reaches an AttributedType or a non-sugared type. |
2464 | template <> const AttributedType *Type::getAs() const; |
2465 | |
2466 | // We can do canonical leaf types faster, because we don't have to |
2467 | // worry about preserving child type decoration. |
2468 | #define TYPE(Class, Base) |
2469 | #define LEAF_TYPE(Class) \ |
2470 | template <> inline const Class##Type *Type::getAs() const { \ |
2471 | return dyn_cast<Class##Type>(CanonicalType); \ |
2472 | } \ |
2473 | template <> inline const Class##Type *Type::castAs() const { \ |
2474 | return cast<Class##Type>(CanonicalType); \ |
2475 | } |
2476 | #include "clang/AST/TypeNodes.inc" |
2477 | |
2478 | /// This class is used for builtin types like 'int'. Builtin |
2479 | /// types are always canonical and have a literal name field. |
2480 | class BuiltinType : public Type { |
2481 | public: |
2482 | enum Kind { |
2483 | // OpenCL image types |
2484 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, |
2485 | #include "clang/Basic/OpenCLImageTypes.def" |
2486 | // OpenCL extension types |
2487 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, |
2488 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2489 | // SVE Types |
2490 | #define SVE_TYPE(Name, Id, SingletonId) Id, |
2491 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2492 | // PPC MMA Types |
2493 | #define PPC_VECTOR_TYPE(Name, Id, Size) Id, |
2494 | #include "clang/Basic/PPCTypes.def" |
2495 | // RVV Types |
2496 | #define RVV_TYPE(Name, Id, SingletonId) Id, |
2497 | #include "clang/Basic/RISCVVTypes.def" |
2498 | // All other builtin types |
2499 | #define BUILTIN_TYPE(Id, SingletonId) Id, |
2500 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id |
2501 | #include "clang/AST/BuiltinTypes.def" |
2502 | }; |
2503 | |
2504 | private: |
2505 | friend class ASTContext; // ASTContext creates these. |
2506 | |
2507 | BuiltinType(Kind K) |
2508 | : Type(Builtin, QualType(), |
2509 | K == Dependent ? TypeDependence::DependentInstantiation |
2510 | : TypeDependence::None) { |
2511 | BuiltinTypeBits.Kind = K; |
2512 | } |
2513 | |
2514 | public: |
2515 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } |
2516 | StringRef getName(const PrintingPolicy &Policy) const; |
2517 | |
2518 | const char *getNameAsCString(const PrintingPolicy &Policy) const { |
2519 | // The StringRef is null-terminated. |
2520 | StringRef str = getName(Policy); |
2521 | assert(!str.empty() && str.data()[str.size()] == '\0')((!str.empty() && str.data()[str.size()] == '\0') ? static_cast <void> (0) : __assert_fail ("!str.empty() && str.data()[str.size()] == '\\0'" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 2521, __PRETTY_FUNCTION__)); |
2522 | return str.data(); |
2523 | } |
2524 | |
2525 | bool isSugared() const { return false; } |
2526 | QualType desugar() const { return QualType(this, 0); } |
2527 | |
2528 | bool isInteger() const { |
2529 | return getKind() >= Bool && getKind() <= Int128; |
2530 | } |
2531 | |
2532 | bool isSignedInteger() const { |
2533 | return getKind() >= Char_S && getKind() <= Int128; |
2534 | } |
2535 | |
2536 | bool isUnsignedInteger() const { |
2537 | return getKind() >= Bool && getKind() <= UInt128; |
2538 | } |
2539 | |
2540 | bool isFloatingPoint() const { |
2541 | return getKind() >= Half && getKind() <= Float128; |
2542 | } |
2543 | |
2544 | /// Determines whether the given kind corresponds to a placeholder type. |
2545 | static bool isPlaceholderTypeKind(Kind K) { |
2546 | return K >= Overload; |
2547 | } |
2548 | |
2549 | /// Determines whether this type is a placeholder type, i.e. a type |
2550 | /// which cannot appear in arbitrary positions in a fully-formed |
2551 | /// expression. |
2552 | bool isPlaceholderType() const { |
2553 | return isPlaceholderTypeKind(getKind()); |
2554 | } |
2555 | |
2556 | /// Determines whether this type is a placeholder type other than |
2557 | /// Overload. Most placeholder types require only syntactic |
2558 | /// information about their context in order to be resolved (e.g. |
2559 | /// whether it is a call expression), which means they can (and |
2560 | /// should) be resolved in an earlier "phase" of analysis. |
2561 | /// Overload expressions sometimes pick up further information |
2562 | /// from their context, like whether the context expects a |
2563 | /// specific function-pointer type, and so frequently need |
2564 | /// special treatment. |
2565 | bool isNonOverloadPlaceholderType() const { |
2566 | return getKind() > Overload; |
2567 | } |
2568 | |
2569 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } |
2570 | }; |
2571 | |
2572 | /// Complex values, per C99 6.2.5p11. This supports the C99 complex |
2573 | /// types (_Complex float etc) as well as the GCC integer complex extensions. |
2574 | class ComplexType : public Type, public llvm::FoldingSetNode { |
2575 | friend class ASTContext; // ASTContext creates these. |
2576 | |
2577 | QualType ElementType; |
2578 | |
2579 | ComplexType(QualType Element, QualType CanonicalPtr) |
2580 | : Type(Complex, CanonicalPtr, Element->getDependence()), |
2581 | ElementType(Element) {} |
2582 | |
2583 | public: |
2584 | QualType getElementType() const { return ElementType; } |
2585 | |
2586 | bool isSugared() const { return false; } |
2587 | QualType desugar() const { return QualType(this, 0); } |
2588 | |
2589 | void Profile(llvm::FoldingSetNodeID &ID) { |
2590 | Profile(ID, getElementType()); |
2591 | } |
2592 | |
2593 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { |
2594 | ID.AddPointer(Element.getAsOpaquePtr()); |
2595 | } |
2596 | |
2597 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } |
2598 | }; |
2599 | |
2600 | /// Sugar for parentheses used when specifying types. |
2601 | class ParenType : public Type, public llvm::FoldingSetNode { |
2602 | friend class ASTContext; // ASTContext creates these. |
2603 | |
2604 | QualType Inner; |
2605 | |
2606 | ParenType(QualType InnerType, QualType CanonType) |
2607 | : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} |
2608 | |
2609 | public: |
2610 | QualType getInnerType() const { return Inner; } |
2611 | |
2612 | bool isSugared() const { return true; } |
2613 | QualType desugar() const { return getInnerType(); } |
2614 | |
2615 | void Profile(llvm::FoldingSetNodeID &ID) { |
2616 | Profile(ID, getInnerType()); |
2617 | } |
2618 | |
2619 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { |
2620 | Inner.Profile(ID); |
2621 | } |
2622 | |
2623 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } |
2624 | }; |
2625 | |
2626 | /// PointerType - C99 6.7.5.1 - Pointer Declarators. |
2627 | class PointerType : public Type, public llvm::FoldingSetNode { |
2628 | friend class ASTContext; // ASTContext creates these. |
2629 | |
2630 | QualType PointeeType; |
2631 | |
2632 | PointerType(QualType Pointee, QualType CanonicalPtr) |
2633 | : Type(Pointer, CanonicalPtr, Pointee->getDependence()), |
2634 | PointeeType(Pointee) {} |
2635 | |
2636 | public: |
2637 | QualType getPointeeType() const { return PointeeType; } |
2638 | |
2639 | bool isSugared() const { return false; } |
2640 | QualType desugar() const { return QualType(this, 0); } |
2641 | |
2642 | void Profile(llvm::FoldingSetNodeID &ID) { |
2643 | Profile(ID, getPointeeType()); |
2644 | } |
2645 | |
2646 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2647 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2648 | } |
2649 | |
2650 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } |
2651 | }; |
2652 | |
2653 | /// Represents a type which was implicitly adjusted by the semantic |
2654 | /// engine for arbitrary reasons. For example, array and function types can |
2655 | /// decay, and function types can have their calling conventions adjusted. |
2656 | class AdjustedType : public Type, public llvm::FoldingSetNode { |
2657 | QualType OriginalTy; |
2658 | QualType AdjustedTy; |
2659 | |
2660 | protected: |
2661 | friend class ASTContext; // ASTContext creates these. |
2662 | |
2663 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, |
2664 | QualType CanonicalPtr) |
2665 | : Type(TC, CanonicalPtr, OriginalTy->getDependence()), |
2666 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} |
2667 | |
2668 | public: |
2669 | QualType getOriginalType() const { return OriginalTy; } |
2670 | QualType getAdjustedType() const { return AdjustedTy; } |
2671 | |
2672 | bool isSugared() const { return true; } |
2673 | QualType desugar() const { return AdjustedTy; } |
2674 | |
2675 | void Profile(llvm::FoldingSetNodeID &ID) { |
2676 | Profile(ID, OriginalTy, AdjustedTy); |
2677 | } |
2678 | |
2679 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { |
2680 | ID.AddPointer(Orig.getAsOpaquePtr()); |
2681 | ID.AddPointer(New.getAsOpaquePtr()); |
2682 | } |
2683 | |
2684 | static bool classof(const Type *T) { |
2685 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; |
2686 | } |
2687 | }; |
2688 | |
2689 | /// Represents a pointer type decayed from an array or function type. |
2690 | class DecayedType : public AdjustedType { |
2691 | friend class ASTContext; // ASTContext creates these. |
2692 | |
2693 | inline |
2694 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); |
2695 | |
2696 | public: |
2697 | QualType getDecayedType() const { return getAdjustedType(); } |
2698 | |
2699 | inline QualType getPointeeType() const; |
2700 | |
2701 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } |
2702 | }; |
2703 | |
2704 | /// Pointer to a block type. |
2705 | /// This type is to represent types syntactically represented as |
2706 | /// "void (^)(int)", etc. Pointee is required to always be a function type. |
2707 | class BlockPointerType : public Type, public llvm::FoldingSetNode { |
2708 | friend class ASTContext; // ASTContext creates these. |
2709 | |
2710 | // Block is some kind of pointer type |
2711 | QualType PointeeType; |
2712 | |
2713 | BlockPointerType(QualType Pointee, QualType CanonicalCls) |
2714 | : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), |
2715 | PointeeType(Pointee) {} |
2716 | |
2717 | public: |
2718 | // Get the pointee type. Pointee is required to always be a function type. |
2719 | QualType getPointeeType() const { return PointeeType; } |
2720 | |
2721 | bool isSugared() const { return false; } |
2722 | QualType desugar() const { return QualType(this, 0); } |
2723 | |
2724 | void Profile(llvm::FoldingSetNodeID &ID) { |
2725 | Profile(ID, getPointeeType()); |
2726 | } |
2727 | |
2728 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2729 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2730 | } |
2731 | |
2732 | static bool classof(const Type *T) { |
2733 | return T->getTypeClass() == BlockPointer; |
2734 | } |
2735 | }; |
2736 | |
2737 | /// Base for LValueReferenceType and RValueReferenceType |
2738 | class ReferenceType : public Type, public llvm::FoldingSetNode { |
2739 | QualType PointeeType; |
2740 | |
2741 | protected: |
2742 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, |
2743 | bool SpelledAsLValue) |
2744 | : Type(tc, CanonicalRef, Referencee->getDependence()), |
2745 | PointeeType(Referencee) { |
2746 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; |
2747 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); |
2748 | } |
2749 | |
2750 | public: |
2751 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } |
2752 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } |
2753 | |
2754 | QualType getPointeeTypeAsWritten() const { return PointeeType; } |
2755 | |
2756 | QualType getPointeeType() const { |
2757 | // FIXME: this might strip inner qualifiers; okay? |
2758 | const ReferenceType *T = this; |
2759 | while (T->isInnerRef()) |
2760 | T = T->PointeeType->castAs<ReferenceType>(); |
2761 | return T->PointeeType; |
2762 | } |
2763 | |
2764 | void Profile(llvm::FoldingSetNodeID &ID) { |
2765 | Profile(ID, PointeeType, isSpelledAsLValue()); |
2766 | } |
2767 | |
2768 | static void Profile(llvm::FoldingSetNodeID &ID, |
2769 | QualType Referencee, |
2770 | bool SpelledAsLValue) { |
2771 | ID.AddPointer(Referencee.getAsOpaquePtr()); |
2772 | ID.AddBoolean(SpelledAsLValue); |
2773 | } |
2774 | |
2775 | static bool classof(const Type *T) { |
2776 | return T->getTypeClass() == LValueReference || |
2777 | T->getTypeClass() == RValueReference; |
2778 | } |
2779 | }; |
2780 | |
2781 | /// An lvalue reference type, per C++11 [dcl.ref]. |
2782 | class LValueReferenceType : public ReferenceType { |
2783 | friend class ASTContext; // ASTContext creates these |
2784 | |
2785 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, |
2786 | bool SpelledAsLValue) |
2787 | : ReferenceType(LValueReference, Referencee, CanonicalRef, |
2788 | SpelledAsLValue) {} |
2789 | |
2790 | public: |
2791 | bool isSugared() const { return false; } |
2792 | QualType desugar() const { return QualType(this, 0); } |
2793 | |
2794 | static bool classof(const Type *T) { |
2795 | return T->getTypeClass() == LValueReference; |
2796 | } |
2797 | }; |
2798 | |
2799 | /// An rvalue reference type, per C++11 [dcl.ref]. |
2800 | class RValueReferenceType : public ReferenceType { |
2801 | friend class ASTContext; // ASTContext creates these |
2802 | |
2803 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) |
2804 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} |
2805 | |
2806 | public: |
2807 | bool isSugared() const { return false; } |
2808 | QualType desugar() const { return QualType(this, 0); } |
2809 | |
2810 | static bool classof(const Type *T) { |
2811 | return T->getTypeClass() == RValueReference; |
2812 | } |
2813 | }; |
2814 | |
2815 | /// A pointer to member type per C++ 8.3.3 - Pointers to members. |
2816 | /// |
2817 | /// This includes both pointers to data members and pointer to member functions. |
2818 | class MemberPointerType : public Type, public llvm::FoldingSetNode { |
2819 | friend class ASTContext; // ASTContext creates these. |
2820 | |
2821 | QualType PointeeType; |
2822 | |
2823 | /// The class of which the pointee is a member. Must ultimately be a |
2824 | /// RecordType, but could be a typedef or a template parameter too. |
2825 | const Type *Class; |
2826 | |
2827 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) |
2828 | : Type(MemberPointer, CanonicalPtr, |
2829 | (Cls->getDependence() & ~TypeDependence::VariablyModified) | |
2830 | Pointee->getDependence()), |
2831 | PointeeType(Pointee), Class(Cls) {} |
2832 | |
2833 | public: |
2834 | QualType getPointeeType() const { return PointeeType; } |
2835 | |
2836 | /// Returns true if the member type (i.e. the pointee type) is a |
2837 | /// function type rather than a data-member type. |
2838 | bool isMemberFunctionPointer() const { |
2839 | return PointeeType->isFunctionProtoType(); |
2840 | } |
2841 | |
2842 | /// Returns true if the member type (i.e. the pointee type) is a |
2843 | /// data type rather than a function type. |
2844 | bool isMemberDataPointer() const { |
2845 | return !PointeeType->isFunctionProtoType(); |
2846 | } |
2847 | |
2848 | const Type *getClass() const { return Class; } |
2849 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; |
2850 | |
2851 | bool isSugared() const { return false; } |
2852 | QualType desugar() const { return QualType(this, 0); } |
2853 | |
2854 | void Profile(llvm::FoldingSetNodeID &ID) { |
2855 | Profile(ID, getPointeeType(), getClass()); |
2856 | } |
2857 | |
2858 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
2859 | const Type *Class) { |
2860 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2861 | ID.AddPointer(Class); |
2862 | } |
2863 | |
2864 | static bool classof(const Type *T) { |
2865 | return T->getTypeClass() == MemberPointer; |
2866 | } |
2867 | }; |
2868 | |
2869 | /// Represents an array type, per C99 6.7.5.2 - Array Declarators. |
2870 | class ArrayType : public Type, public llvm::FoldingSetNode { |
2871 | public: |
2872 | /// Capture whether this is a normal array (e.g. int X[4]) |
2873 | /// an array with a static size (e.g. int X[static 4]), or an array |
2874 | /// with a star size (e.g. int X[*]). |
2875 | /// 'static' is only allowed on function parameters. |
2876 | enum ArraySizeModifier { |
2877 | Normal, Static, Star |
2878 | }; |
2879 | |
2880 | private: |
2881 | /// The element type of the array. |
2882 | QualType ElementType; |
2883 | |
2884 | protected: |
2885 | friend class ASTContext; // ASTContext creates these. |
2886 | |
2887 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, |
2888 | unsigned tq, const Expr *sz = nullptr); |
2889 | |
2890 | public: |
2891 | QualType getElementType() const { return ElementType; } |
2892 | |
2893 | ArraySizeModifier getSizeModifier() const { |
2894 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); |
2895 | } |
2896 | |
2897 | Qualifiers getIndexTypeQualifiers() const { |
2898 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); |
2899 | } |
2900 | |
2901 | unsigned getIndexTypeCVRQualifiers() const { |
2902 | return ArrayTypeBits.IndexTypeQuals; |
2903 | } |
2904 | |
2905 | static bool classof(const Type *T) { |
2906 | return T->getTypeClass() == ConstantArray || |
2907 | T->getTypeClass() == VariableArray || |
2908 | T->getTypeClass() == IncompleteArray || |
2909 | T->getTypeClass() == DependentSizedArray; |
2910 | } |
2911 | }; |
2912 | |
2913 | /// Represents the canonical version of C arrays with a specified constant size. |
2914 | /// For example, the canonical type for 'int A[4 + 4*100]' is a |
2915 | /// ConstantArrayType where the element type is 'int' and the size is 404. |
2916 | class ConstantArrayType final |
2917 | : public ArrayType, |
2918 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { |
2919 | friend class ASTContext; // ASTContext creates these. |
2920 | friend TrailingObjects; |
2921 | |
2922 | llvm::APInt Size; // Allows us to unique the type. |
2923 | |
2924 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, |
2925 | const Expr *sz, ArraySizeModifier sm, unsigned tq) |
2926 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { |
2927 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; |
2928 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { |
2929 | assert(!can.isNull() && "canonical constant array should not have size")((!can.isNull() && "canonical constant array should not have size" ) ? static_cast<void> (0) : __assert_fail ("!can.isNull() && \"canonical constant array should not have size\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 2929, __PRETTY_FUNCTION__)); |
2930 | *getTrailingObjects<const Expr*>() = sz; |
2931 | } |
2932 | } |
2933 | |
2934 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { |
2935 | return ConstantArrayTypeBits.HasStoredSizeExpr; |
2936 | } |
2937 | |
2938 | public: |
2939 | const llvm::APInt &getSize() const { return Size; } |
2940 | const Expr *getSizeExpr() const { |
2941 | return ConstantArrayTypeBits.HasStoredSizeExpr |
2942 | ? *getTrailingObjects<const Expr *>() |
2943 | : nullptr; |
2944 | } |
2945 | bool isSugared() const { return false; } |
2946 | QualType desugar() const { return QualType(this, 0); } |
2947 | |
2948 | /// Determine the number of bits required to address a member of |
2949 | // an array with the given element type and number of elements. |
2950 | static unsigned getNumAddressingBits(const ASTContext &Context, |
2951 | QualType ElementType, |
2952 | const llvm::APInt &NumElements); |
2953 | |
2954 | /// Determine the maximum number of active bits that an array's size |
2955 | /// can require, which limits the maximum size of the array. |
2956 | static unsigned getMaxSizeBits(const ASTContext &Context); |
2957 | |
2958 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
2959 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), |
2960 | getSizeModifier(), getIndexTypeCVRQualifiers()); |
2961 | } |
2962 | |
2963 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, |
2964 | QualType ET, const llvm::APInt &ArraySize, |
2965 | const Expr *SizeExpr, ArraySizeModifier SizeMod, |
2966 | unsigned TypeQuals); |
2967 | |
2968 | static bool classof(const Type *T) { |
2969 | return T->getTypeClass() == ConstantArray; |
2970 | } |
2971 | }; |
2972 | |
2973 | /// Represents a C array with an unspecified size. For example 'int A[]' has |
2974 | /// an IncompleteArrayType where the element type is 'int' and the size is |
2975 | /// unspecified. |
2976 | class IncompleteArrayType : public ArrayType { |
2977 | friend class ASTContext; // ASTContext creates these. |
2978 | |
2979 | IncompleteArrayType(QualType et, QualType can, |
2980 | ArraySizeModifier sm, unsigned tq) |
2981 | : ArrayType(IncompleteArray, et, can, sm, tq) {} |
2982 | |
2983 | public: |
2984 | friend class StmtIteratorBase; |
2985 | |
2986 | bool isSugared() const { return false; } |
2987 | QualType desugar() const { return QualType(this, 0); } |
2988 | |
2989 | static bool classof(const Type *T) { |
2990 | return T->getTypeClass() == IncompleteArray; |
2991 | } |
2992 | |
2993 | void Profile(llvm::FoldingSetNodeID &ID) { |
2994 | Profile(ID, getElementType(), getSizeModifier(), |
2995 | getIndexTypeCVRQualifiers()); |
2996 | } |
2997 | |
2998 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, |
2999 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
3000 | ID.AddPointer(ET.getAsOpaquePtr()); |
3001 | ID.AddInteger(SizeMod); |
3002 | ID.AddInteger(TypeQuals); |
3003 | } |
3004 | }; |
3005 | |
3006 | /// Represents a C array with a specified size that is not an |
3007 | /// integer-constant-expression. For example, 'int s[x+foo()]'. |
3008 | /// Since the size expression is an arbitrary expression, we store it as such. |
3009 | /// |
3010 | /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and |
3011 | /// should not be: two lexically equivalent variable array types could mean |
3012 | /// different things, for example, these variables do not have the same type |
3013 | /// dynamically: |
3014 | /// |
3015 | /// void foo(int x) { |
3016 | /// int Y[x]; |
3017 | /// ++x; |
3018 | /// int Z[x]; |
3019 | /// } |
3020 | class VariableArrayType : public ArrayType { |
3021 | friend class ASTContext; // ASTContext creates these. |
3022 | |
3023 | /// An assignment-expression. VLA's are only permitted within |
3024 | /// a function block. |
3025 | Stmt *SizeExpr; |
3026 | |
3027 | /// The range spanned by the left and right array brackets. |
3028 | SourceRange Brackets; |
3029 | |
3030 | VariableArrayType(QualType et, QualType can, Expr *e, |
3031 | ArraySizeModifier sm, unsigned tq, |
3032 | SourceRange brackets) |
3033 | : ArrayType(VariableArray, et, can, sm, tq, e), |
3034 | SizeExpr((Stmt*) e), Brackets(brackets) {} |
3035 | |
3036 | public: |
3037 | friend class StmtIteratorBase; |
3038 | |
3039 | Expr *getSizeExpr() const { |
3040 | // We use C-style casts instead of cast<> here because we do not wish |
3041 | // to have a dependency of Type.h on Stmt.h/Expr.h. |
3042 | return (Expr*) SizeExpr; |
3043 | } |
3044 | |
3045 | SourceRange getBracketsRange() const { return Brackets; } |
3046 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3047 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3048 | |
3049 | bool isSugared() const { return false; } |
3050 | QualType desugar() const { return QualType(this, 0); } |
3051 | |
3052 | static bool classof(const Type *T) { |
3053 | return T->getTypeClass() == VariableArray; |
3054 | } |
3055 | |
3056 | void Profile(llvm::FoldingSetNodeID &ID) { |
3057 | llvm_unreachable("Cannot unique VariableArrayTypes.")::llvm::llvm_unreachable_internal("Cannot unique VariableArrayTypes." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 3057); |
3058 | } |
3059 | }; |
3060 | |
3061 | /// Represents an array type in C++ whose size is a value-dependent expression. |
3062 | /// |
3063 | /// For example: |
3064 | /// \code |
3065 | /// template<typename T, int Size> |
3066 | /// class array { |
3067 | /// T data[Size]; |
3068 | /// }; |
3069 | /// \endcode |
3070 | /// |
3071 | /// For these types, we won't actually know what the array bound is |
3072 | /// until template instantiation occurs, at which point this will |
3073 | /// become either a ConstantArrayType or a VariableArrayType. |
3074 | class DependentSizedArrayType : public ArrayType { |
3075 | friend class ASTContext; // ASTContext creates these. |
3076 | |
3077 | const ASTContext &Context; |
3078 | |
3079 | /// An assignment expression that will instantiate to the |
3080 | /// size of the array. |
3081 | /// |
3082 | /// The expression itself might be null, in which case the array |
3083 | /// type will have its size deduced from an initializer. |
3084 | Stmt *SizeExpr; |
3085 | |
3086 | /// The range spanned by the left and right array brackets. |
3087 | SourceRange Brackets; |
3088 | |
3089 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, |
3090 | Expr *e, ArraySizeModifier sm, unsigned tq, |
3091 | SourceRange brackets); |
3092 | |
3093 | public: |
3094 | friend class StmtIteratorBase; |
3095 | |
3096 | Expr *getSizeExpr() const { |
3097 | // We use C-style casts instead of cast<> here because we do not wish |
3098 | // to have a dependency of Type.h on Stmt.h/Expr.h. |
3099 | return (Expr*) SizeExpr; |
3100 | } |
3101 | |
3102 | SourceRange getBracketsRange() const { return Brackets; } |
3103 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3104 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3105 | |
3106 | bool isSugared() const { return false; } |
3107 | QualType desugar() const { return QualType(this, 0); } |
3108 | |
3109 | static bool classof(const Type *T) { |
3110 | return T->getTypeClass() == DependentSizedArray; |
3111 | } |
3112 | |
3113 | void Profile(llvm::FoldingSetNodeID &ID) { |
3114 | Profile(ID, Context, getElementType(), |
3115 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); |
3116 | } |
3117 | |
3118 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3119 | QualType ET, ArraySizeModifier SizeMod, |
3120 | unsigned TypeQuals, Expr *E); |
3121 | }; |
3122 | |
3123 | /// Represents an extended address space qualifier where the input address space |
3124 | /// value is dependent. Non-dependent address spaces are not represented with a |
3125 | /// special Type subclass; they are stored on an ExtQuals node as part of a QualType. |
3126 | /// |
3127 | /// For example: |
3128 | /// \code |
3129 | /// template<typename T, int AddrSpace> |
3130 | /// class AddressSpace { |
3131 | /// typedef T __attribute__((address_space(AddrSpace))) type; |
3132 | /// } |
3133 | /// \endcode |
3134 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { |
3135 | friend class ASTContext; |
3136 | |
3137 | const ASTContext &Context; |
3138 | Expr *AddrSpaceExpr; |
3139 | QualType PointeeType; |
3140 | SourceLocation loc; |
3141 | |
3142 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, |
3143 | QualType can, Expr *AddrSpaceExpr, |
3144 | SourceLocation loc); |
3145 | |
3146 | public: |
3147 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } |
3148 | QualType getPointeeType() const { return PointeeType; } |
3149 | SourceLocation getAttributeLoc() const { return loc; } |
3150 | |
3151 | bool isSugared() const { return false; } |
3152 | QualType desugar() const { return QualType(this, 0); } |
3153 | |
3154 | static bool classof(const Type *T) { |
3155 | return T->getTypeClass() == DependentAddressSpace; |
3156 | } |
3157 | |
3158 | void Profile(llvm::FoldingSetNodeID &ID) { |
3159 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); |
3160 | } |
3161 | |
3162 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3163 | QualType PointeeType, Expr *AddrSpaceExpr); |
3164 | }; |
3165 | |
3166 | /// Represents an extended vector type where either the type or size is |
3167 | /// dependent. |
3168 | /// |
3169 | /// For example: |
3170 | /// \code |
3171 | /// template<typename T, int Size> |
3172 | /// class vector { |
3173 | /// typedef T __attribute__((ext_vector_type(Size))) type; |
3174 | /// } |
3175 | /// \endcode |
3176 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { |
3177 | friend class ASTContext; |
3178 | |
3179 | const ASTContext &Context; |
3180 | Expr *SizeExpr; |
3181 | |
3182 | /// The element type of the array. |
3183 | QualType ElementType; |
3184 | |
3185 | SourceLocation loc; |
3186 | |
3187 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, |
3188 | QualType can, Expr *SizeExpr, SourceLocation loc); |
3189 | |
3190 | public: |
3191 | Expr *getSizeExpr() const { return SizeExpr; } |
3192 | QualType getElementType() const { return ElementType; } |
3193 | SourceLocation getAttributeLoc() const { return loc; } |
3194 | |
3195 | bool isSugared() const { return false; } |
3196 | QualType desugar() const { return QualType(this, 0); } |
3197 | |
3198 | static bool classof(const Type *T) { |
3199 | return T->getTypeClass() == DependentSizedExtVector; |
3200 | } |
3201 | |
3202 | void Profile(llvm::FoldingSetNodeID &ID) { |
3203 | Profile(ID, Context, getElementType(), getSizeExpr()); |
3204 | } |
3205 | |
3206 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3207 | QualType ElementType, Expr *SizeExpr); |
3208 | }; |
3209 | |
3210 | |
3211 | /// Represents a GCC generic vector type. This type is created using |
3212 | /// __attribute__((vector_size(n)), where "n" specifies the vector size in |
3213 | /// bytes; or from an Altivec __vector or vector declaration. |
3214 | /// Since the constructor takes the number of vector elements, the |
3215 | /// client is responsible for converting the size into the number of elements. |
3216 | class VectorType : public Type, public llvm::FoldingSetNode { |
3217 | public: |
3218 | enum VectorKind { |
3219 | /// not a target-specific vector type |
3220 | GenericVector, |
3221 | |
3222 | /// is AltiVec vector |
3223 | AltiVecVector, |
3224 | |
3225 | /// is AltiVec 'vector Pixel' |
3226 | AltiVecPixel, |
3227 | |
3228 | /// is AltiVec 'vector bool ...' |
3229 | AltiVecBool, |
3230 | |
3231 | /// is ARM Neon vector |
3232 | NeonVector, |
3233 | |
3234 | /// is ARM Neon polynomial vector |
3235 | NeonPolyVector, |
3236 | |
3237 | /// is AArch64 SVE fixed-length data vector |
3238 | SveFixedLengthDataVector, |
3239 | |
3240 | /// is AArch64 SVE fixed-length predicate vector |
3241 | SveFixedLengthPredicateVector |
3242 | }; |
3243 | |
3244 | protected: |
3245 | friend class ASTContext; // ASTContext creates these. |
3246 | |
3247 | /// The element type of the vector. |
3248 | QualType ElementType; |
3249 | |
3250 | VectorType(QualType vecType, unsigned nElements, QualType canonType, |
3251 | VectorKind vecKind); |
3252 | |
3253 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
3254 | QualType canonType, VectorKind vecKind); |
3255 | |
3256 | public: |
3257 | QualType getElementType() const { return ElementType; } |
3258 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } |
3259 | |
3260 | bool isSugared() const { return false; } |
3261 | QualType desugar() const { return QualType(this, 0); } |
3262 | |
3263 | VectorKind getVectorKind() const { |
3264 | return VectorKind(VectorTypeBits.VecKind); |
3265 | } |
3266 | |
3267 | void Profile(llvm::FoldingSetNodeID &ID) { |
3268 | Profile(ID, getElementType(), getNumElements(), |
3269 | getTypeClass(), getVectorKind()); |
3270 | } |
3271 | |
3272 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3273 | unsigned NumElements, TypeClass TypeClass, |
3274 | VectorKind VecKind) { |
3275 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3276 | ID.AddInteger(NumElements); |
3277 | ID.AddInteger(TypeClass); |
3278 | ID.AddInteger(VecKind); |
3279 | } |
3280 | |
3281 | static bool classof(const Type *T) { |
3282 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; |
3283 | } |
3284 | }; |
3285 | |
3286 | /// Represents a vector type where either the type or size is dependent. |
3287 | //// |
3288 | /// For example: |
3289 | /// \code |
3290 | /// template<typename T, int Size> |
3291 | /// class vector { |
3292 | /// typedef T __attribute__((vector_size(Size))) type; |
3293 | /// } |
3294 | /// \endcode |
3295 | class DependentVectorType : public Type, public llvm::FoldingSetNode { |
3296 | friend class ASTContext; |
3297 | |
3298 | const ASTContext &Context; |
3299 | QualType ElementType; |
3300 | Expr *SizeExpr; |
3301 | SourceLocation Loc; |
3302 | |
3303 | DependentVectorType(const ASTContext &Context, QualType ElementType, |
3304 | QualType CanonType, Expr *SizeExpr, |
3305 | SourceLocation Loc, VectorType::VectorKind vecKind); |
3306 | |
3307 | public: |
3308 | Expr *getSizeExpr() const { return SizeExpr; } |
3309 | QualType getElementType() const { return ElementType; } |
3310 | SourceLocation getAttributeLoc() const { return Loc; } |
3311 | VectorType::VectorKind getVectorKind() const { |
3312 | return VectorType::VectorKind(VectorTypeBits.VecKind); |
3313 | } |
3314 | |
3315 | bool isSugared() const { return false; } |
3316 | QualType desugar() const { return QualType(this, 0); } |
3317 | |
3318 | static bool classof(const Type *T) { |
3319 | return T->getTypeClass() == DependentVector; |
3320 | } |
3321 | |
3322 | void Profile(llvm::FoldingSetNodeID &ID) { |
3323 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); |
3324 | } |
3325 | |
3326 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3327 | QualType ElementType, const Expr *SizeExpr, |
3328 | VectorType::VectorKind VecKind); |
3329 | }; |
3330 | |
3331 | /// ExtVectorType - Extended vector type. This type is created using |
3332 | /// __attribute__((ext_vector_type(n)), where "n" is the number of elements. |
3333 | /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This |
3334 | /// class enables syntactic extensions, like Vector Components for accessing |
3335 | /// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL |
3336 | /// Shading Language). |
3337 | class ExtVectorType : public VectorType { |
3338 | friend class ASTContext; // ASTContext creates these. |
3339 | |
3340 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) |
3341 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} |
3342 | |
3343 | public: |
3344 | static int getPointAccessorIdx(char c) { |
3345 | switch (c) { |
3346 | default: return -1; |
3347 | case 'x': case 'r': return 0; |
3348 | case 'y': case 'g': return 1; |
3349 | case 'z': case 'b': return 2; |
3350 | case 'w': case 'a': return 3; |
3351 | } |
3352 | } |
3353 | |
3354 | static int getNumericAccessorIdx(char c) { |
3355 | switch (c) { |
3356 | default: return -1; |
3357 | case '0': return 0; |
3358 | case '1': return 1; |
3359 | case '2': return 2; |
3360 | case '3': return 3; |
3361 | case '4': return 4; |
3362 | case '5': return 5; |
3363 | case '6': return 6; |
3364 | case '7': return 7; |
3365 | case '8': return 8; |
3366 | case '9': return 9; |
3367 | case 'A': |
3368 | case 'a': return 10; |
3369 | case 'B': |
3370 | case 'b': return 11; |
3371 | case 'C': |
3372 | case 'c': return 12; |
3373 | case 'D': |
3374 | case 'd': return 13; |
3375 | case 'E': |
3376 | case 'e': return 14; |
3377 | case 'F': |
3378 | case 'f': return 15; |
3379 | } |
3380 | } |
3381 | |
3382 | static int getAccessorIdx(char c, bool isNumericAccessor) { |
3383 | if (isNumericAccessor) |
3384 | return getNumericAccessorIdx(c); |
3385 | else |
3386 | return getPointAccessorIdx(c); |
3387 | } |
3388 | |
3389 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { |
3390 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) |
3391 | return unsigned(idx-1) < getNumElements(); |
3392 | return false; |
3393 | } |
3394 | |
3395 | bool isSugared() const { return false; } |
3396 | QualType desugar() const { return QualType(this, 0); } |
3397 | |
3398 | static bool classof(const Type *T) { |
3399 | return T->getTypeClass() == ExtVector; |
3400 | } |
3401 | }; |
3402 | |
3403 | /// Represents a matrix type, as defined in the Matrix Types clang extensions. |
3404 | /// __attribute__((matrix_type(rows, columns))), where "rows" specifies |
3405 | /// number of rows and "columns" specifies the number of columns. |
3406 | class MatrixType : public Type, public llvm::FoldingSetNode { |
3407 | protected: |
3408 | friend class ASTContext; |
3409 | |
3410 | /// The element type of the matrix. |
3411 | QualType ElementType; |
3412 | |
3413 | MatrixType(QualType ElementTy, QualType CanonElementTy); |
3414 | |
3415 | MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, |
3416 | const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); |
3417 | |
3418 | public: |
3419 | /// Returns type of the elements being stored in the matrix |
3420 | QualType getElementType() const { return ElementType; } |
3421 | |
3422 | /// Valid elements types are the following: |
3423 | /// * an integer type (as in C2x 6.2.5p19), but excluding enumerated types |
3424 | /// and _Bool |
3425 | /// * the standard floating types float or double |
3426 | /// * a half-precision floating point type, if one is supported on the target |
3427 | static bool isValidElementType(QualType T) { |
3428 | return T->isDependentType() || |
3429 | (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); |
3430 | } |
3431 | |
3432 | bool isSugared() const { return false; } |
3433 | QualType desugar() const { return QualType(this, 0); } |
3434 | |
3435 | static bool classof(const Type *T) { |
3436 | return T->getTypeClass() == ConstantMatrix || |
3437 | T->getTypeClass() == DependentSizedMatrix; |
3438 | } |
3439 | }; |
3440 | |
3441 | /// Represents a concrete matrix type with constant number of rows and columns |
3442 | class ConstantMatrixType final : public MatrixType { |
3443 | protected: |
3444 | friend class ASTContext; |
3445 | |
3446 | /// The element type of the matrix. |
3447 | // FIXME: Appears to be unused? There is also MatrixType::ElementType... |
3448 | QualType ElementType; |
3449 | |
3450 | /// Number of rows and columns. |
3451 | unsigned NumRows; |
3452 | unsigned NumColumns; |
3453 | |
3454 | static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; |
3455 | |
3456 | ConstantMatrixType(QualType MatrixElementType, unsigned NRows, |
3457 | unsigned NColumns, QualType CanonElementType); |
3458 | |
3459 | ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, |
3460 | unsigned NColumns, QualType CanonElementType); |
3461 | |
3462 | public: |
3463 | /// Returns the number of rows in the matrix. |
3464 | unsigned getNumRows() const { return NumRows; } |
3465 | |
3466 | /// Returns the number of columns in the matrix. |
3467 | unsigned getNumColumns() const { return NumColumns; } |
3468 | |
3469 | /// Returns the number of elements required to embed the matrix into a vector. |
3470 | unsigned getNumElementsFlattened() const { |
3471 | return getNumRows() * getNumColumns(); |
3472 | } |
3473 | |
3474 | /// Returns true if \p NumElements is a valid matrix dimension. |
3475 | static constexpr bool isDimensionValid(size_t NumElements) { |
3476 | return NumElements > 0 && NumElements <= MaxElementsPerDimension; |
3477 | } |
3478 | |
3479 | /// Returns the maximum number of elements per dimension. |
3480 | static constexpr unsigned getMaxElementsPerDimension() { |
3481 | return MaxElementsPerDimension; |
3482 | } |
3483 | |
3484 | void Profile(llvm::FoldingSetNodeID &ID) { |
3485 | Profile(ID, getElementType(), getNumRows(), getNumColumns(), |
3486 | getTypeClass()); |
3487 | } |
3488 | |
3489 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3490 | unsigned NumRows, unsigned NumColumns, |
3491 | TypeClass TypeClass) { |
3492 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3493 | ID.AddInteger(NumRows); |
3494 | ID.AddInteger(NumColumns); |
3495 | ID.AddInteger(TypeClass); |
3496 | } |
3497 | |
3498 | static bool classof(const Type *T) { |
3499 | return T->getTypeClass() == ConstantMatrix; |
3500 | } |
3501 | }; |
3502 | |
3503 | /// Represents a matrix type where the type and the number of rows and columns |
3504 | /// is dependent on a template. |
3505 | class DependentSizedMatrixType final : public MatrixType { |
3506 | friend class ASTContext; |
3507 | |
3508 | const ASTContext &Context; |
3509 | Expr *RowExpr; |
3510 | Expr *ColumnExpr; |
3511 | |
3512 | SourceLocation loc; |
3513 | |
3514 | DependentSizedMatrixType(const ASTContext &Context, QualType ElementType, |
3515 | QualType CanonicalType, Expr *RowExpr, |
3516 | Expr *ColumnExpr, SourceLocation loc); |
3517 | |
3518 | public: |
3519 | QualType getElementType() const { return ElementType; } |
3520 | Expr *getRowExpr() const { return RowExpr; } |
3521 | Expr *getColumnExpr() const { return ColumnExpr; } |
3522 | SourceLocation getAttributeLoc() const { return loc; } |
3523 | |
3524 | bool isSugared() const { return false; } |
3525 | QualType desugar() const { return QualType(this, 0); } |
3526 | |
3527 | static bool classof(const Type *T) { |
3528 | return T->getTypeClass() == DependentSizedMatrix; |
3529 | } |
3530 | |
3531 | void Profile(llvm::FoldingSetNodeID &ID) { |
3532 | Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); |
3533 | } |
3534 | |
3535 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3536 | QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); |
3537 | }; |
3538 | |
3539 | /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base |
3540 | /// class of FunctionNoProtoType and FunctionProtoType. |
3541 | class FunctionType : public Type { |
3542 | // The type returned by the function. |
3543 | QualType ResultType; |
3544 | |
3545 | public: |
3546 | /// Interesting information about a specific parameter that can't simply |
3547 | /// be reflected in parameter's type. This is only used by FunctionProtoType |
3548 | /// but is in FunctionType to make this class available during the |
3549 | /// specification of the bases of FunctionProtoType. |
3550 | /// |
3551 | /// It makes sense to model language features this way when there's some |
3552 | /// sort of parameter-specific override (such as an attribute) that |
3553 | /// affects how the function is called. For example, the ARC ns_consumed |
3554 | /// attribute changes whether a parameter is passed at +0 (the default) |
3555 | /// or +1 (ns_consumed). This must be reflected in the function type, |
3556 | /// but isn't really a change to the parameter type. |
3557 | /// |
3558 | /// One serious disadvantage of modelling language features this way is |
3559 | /// that they generally do not work with language features that attempt |
3560 | /// to destructure types. For example, template argument deduction will |
3561 | /// not be able to match a parameter declared as |
3562 | /// T (*)(U) |
3563 | /// against an argument of type |
3564 | /// void (*)(__attribute__((ns_consumed)) id) |
3565 | /// because the substitution of T=void, U=id into the former will |
3566 | /// not produce the latter. |
3567 | class ExtParameterInfo { |
3568 | enum { |
3569 | ABIMask = 0x0F, |
3570 | IsConsumed = 0x10, |
3571 | HasPassObjSize = 0x20, |
3572 | IsNoEscape = 0x40, |
3573 | }; |
3574 | unsigned char Data = 0; |
3575 | |
3576 | public: |
3577 | ExtParameterInfo() = default; |
3578 | |
3579 | /// Return the ABI treatment of this parameter. |
3580 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } |
3581 | ExtParameterInfo withABI(ParameterABI kind) const { |
3582 | ExtParameterInfo copy = *this; |
3583 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); |
3584 | return copy; |
3585 | } |
3586 | |
3587 | /// Is this parameter considered "consumed" by Objective-C ARC? |
3588 | /// Consumed parameters must have retainable object type. |
3589 | bool isConsumed() const { return (Data & IsConsumed); } |
3590 | ExtParameterInfo withIsConsumed(bool consumed) const { |
3591 | ExtParameterInfo copy = *this; |
3592 | if (consumed) |
3593 | copy.Data |= IsConsumed; |
3594 | else |
3595 | copy.Data &= ~IsConsumed; |
3596 | return copy; |
3597 | } |
3598 | |
3599 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } |
3600 | ExtParameterInfo withHasPassObjectSize() const { |
3601 | ExtParameterInfo Copy = *this; |
3602 | Copy.Data |= HasPassObjSize; |
3603 | return Copy; |
3604 | } |
3605 | |
3606 | bool isNoEscape() const { return Data & IsNoEscape; } |
3607 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { |
3608 | ExtParameterInfo Copy = *this; |
3609 | if (NoEscape) |
3610 | Copy.Data |= IsNoEscape; |
3611 | else |
3612 | Copy.Data &= ~IsNoEscape; |
3613 | return Copy; |
3614 | } |
3615 | |
3616 | unsigned char getOpaqueValue() const { return Data; } |
3617 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { |
3618 | ExtParameterInfo result; |
3619 | result.Data = data; |
3620 | return result; |
3621 | } |
3622 | |
3623 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3624 | return lhs.Data == rhs.Data; |
3625 | } |
3626 | |
3627 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3628 | return lhs.Data != rhs.Data; |
3629 | } |
3630 | }; |
3631 | |
3632 | /// A class which abstracts out some details necessary for |
3633 | /// making a call. |
3634 | /// |
3635 | /// It is not actually used directly for storing this information in |
3636 | /// a FunctionType, although FunctionType does currently use the |
3637 | /// same bit-pattern. |
3638 | /// |
3639 | // If you add a field (say Foo), other than the obvious places (both, |
3640 | // constructors, compile failures), what you need to update is |
3641 | // * Operator== |
3642 | // * getFoo |
3643 | // * withFoo |
3644 | // * functionType. Add Foo, getFoo. |
3645 | // * ASTContext::getFooType |
3646 | // * ASTContext::mergeFunctionTypes |
3647 | // * FunctionNoProtoType::Profile |
3648 | // * FunctionProtoType::Profile |
3649 | // * TypePrinter::PrintFunctionProto |
3650 | // * AST read and write |
3651 | // * Codegen |
3652 | class ExtInfo { |
3653 | friend class FunctionType; |
3654 | |
3655 | // Feel free to rearrange or add bits, but if you go over 16, you'll need to |
3656 | // adjust the Bits field below, and if you add bits, you'll need to adjust |
3657 | // Type::FunctionTypeBitfields::ExtInfo as well. |
3658 | |
3659 | // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck|cmsenscall| |
3660 | // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 | 12 | |
3661 | // |
3662 | // regparm is either 0 (no regparm attribute) or the regparm value+1. |
3663 | enum { CallConvMask = 0x1F }; |
3664 | enum { NoReturnMask = 0x20 }; |
3665 | enum { ProducesResultMask = 0x40 }; |
3666 | enum { NoCallerSavedRegsMask = 0x80 }; |
3667 | enum { |
3668 | RegParmMask = 0x700, |
3669 | RegParmOffset = 8 |
3670 | }; |
3671 | enum { NoCfCheckMask = 0x800 }; |
3672 | enum { CmseNSCallMask = 0x1000 }; |
3673 | uint16_t Bits = CC_C; |
3674 | |
3675 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} |
3676 | |
3677 | public: |
3678 | // Constructor with no defaults. Use this when you know that you |
3679 | // have all the elements (when reading an AST file for example). |
3680 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, |
3681 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, |
3682 | bool cmseNSCall) { |
3683 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value")(((!hasRegParm || regParm < 7) && "Invalid regparm value" ) ? static_cast<void> (0) : __assert_fail ("(!hasRegParm || regParm < 7) && \"Invalid regparm value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 3683, __PRETTY_FUNCTION__)); |
3684 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | |
3685 | (producesResult ? ProducesResultMask : 0) | |
3686 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | |
3687 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | |
3688 | (NoCfCheck ? NoCfCheckMask : 0) | |
3689 | (cmseNSCall ? CmseNSCallMask : 0); |
3690 | } |
3691 | |
3692 | // Constructor with all defaults. Use when for example creating a |
3693 | // function known to use defaults. |
3694 | ExtInfo() = default; |
3695 | |
3696 | // Constructor with just the calling convention, which is an important part |
3697 | // of the canonical type. |
3698 | ExtInfo(CallingConv CC) : Bits(CC) {} |
3699 | |
3700 | bool getNoReturn() const { return Bits & NoReturnMask; } |
3701 | bool getProducesResult() const { return Bits & ProducesResultMask; } |
3702 | bool getCmseNSCall() const { return Bits & CmseNSCallMask; } |
3703 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } |
3704 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } |
3705 | bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } |
3706 | |
3707 | unsigned getRegParm() const { |
3708 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; |
3709 | if (RegParm > 0) |
3710 | --RegParm; |
3711 | return RegParm; |
3712 | } |
3713 | |
3714 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } |
3715 | |
3716 | bool operator==(ExtInfo Other) const { |
3717 | return Bits == Other.Bits; |
3718 | } |
3719 | bool operator!=(ExtInfo Other) const { |
3720 | return Bits != Other.Bits; |
3721 | } |
3722 | |
3723 | // Note that we don't have setters. That is by design, use |
3724 | // the following with methods instead of mutating these objects. |
3725 | |
3726 | ExtInfo withNoReturn(bool noReturn) const { |
3727 | if (noReturn) |
3728 | return ExtInfo(Bits | NoReturnMask); |
3729 | else |
3730 | return ExtInfo(Bits & ~NoReturnMask); |
3731 | } |
3732 | |
3733 | ExtInfo withProducesResult(bool producesResult) const { |
3734 | if (producesResult) |
3735 | return ExtInfo(Bits | ProducesResultMask); |
3736 | else |
3737 | return ExtInfo(Bits & ~ProducesResultMask); |
3738 | } |
3739 | |
3740 | ExtInfo withCmseNSCall(bool cmseNSCall) const { |
3741 | if (cmseNSCall) |
3742 | return ExtInfo(Bits | CmseNSCallMask); |
3743 | else |
3744 | return ExtInfo(Bits & ~CmseNSCallMask); |
3745 | } |
3746 | |
3747 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { |
3748 | if (noCallerSavedRegs) |
3749 | return ExtInfo(Bits | NoCallerSavedRegsMask); |
3750 | else |
3751 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); |
3752 | } |
3753 | |
3754 | ExtInfo withNoCfCheck(bool noCfCheck) const { |
3755 | if (noCfCheck) |
3756 | return ExtInfo(Bits | NoCfCheckMask); |
3757 | else |
3758 | return ExtInfo(Bits & ~NoCfCheckMask); |
3759 | } |
3760 | |
3761 | ExtInfo withRegParm(unsigned RegParm) const { |
3762 | assert(RegParm < 7 && "Invalid regparm value")((RegParm < 7 && "Invalid regparm value") ? static_cast <void> (0) : __assert_fail ("RegParm < 7 && \"Invalid regparm value\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 3762, __PRETTY_FUNCTION__)); |
3763 | return ExtInfo((Bits & ~RegParmMask) | |
3764 | ((RegParm + 1) << RegParmOffset)); |
3765 | } |
3766 | |
3767 | ExtInfo withCallingConv(CallingConv cc) const { |
3768 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); |
3769 | } |
3770 | |
3771 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3772 | ID.AddInteger(Bits); |
3773 | } |
3774 | }; |
3775 | |
3776 | /// A simple holder for a QualType representing a type in an |
3777 | /// exception specification. Unfortunately needed by FunctionProtoType |
3778 | /// because TrailingObjects cannot handle repeated types. |
3779 | struct ExceptionType { QualType Type; }; |
3780 | |
3781 | /// A simple holder for various uncommon bits which do not fit in |
3782 | /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the |
3783 | /// alignment of subsequent objects in TrailingObjects. You must update |
3784 | /// hasExtraBitfields in FunctionProtoType after adding extra data here. |
3785 | struct alignas(void *) FunctionTypeExtraBitfields { |
3786 | /// The number of types in the exception specification. |
3787 | /// A whole unsigned is not needed here and according to |
3788 | /// [implimits] 8 bits would be enough here. |
3789 | unsigned NumExceptionType; |
3790 | }; |
3791 | |
3792 | protected: |
3793 | FunctionType(TypeClass tc, QualType res, QualType Canonical, |
3794 | TypeDependence Dependence, ExtInfo Info) |
3795 | : Type(tc, Canonical, Dependence), ResultType(res) { |
3796 | FunctionTypeBits.ExtInfo = Info.Bits; |
3797 | } |
3798 | |
3799 | Qualifiers getFastTypeQuals() const { |
3800 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); |
3801 | } |
3802 | |
3803 | public: |
3804 | QualType getReturnType() const { return ResultType; } |
3805 | |
3806 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } |
3807 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } |
3808 | |
3809 | /// Determine whether this function type includes the GNU noreturn |
3810 | /// attribute. The C++11 [[noreturn]] attribute does not affect the function |
3811 | /// type. |
3812 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } |
3813 | |
3814 | bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } |
3815 | CallingConv getCallConv() const { return getExtInfo().getCC(); } |
3816 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } |
3817 | |
3818 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, |
3819 | "Const, volatile and restrict are assumed to be a subset of " |
3820 | "the fast qualifiers."); |
3821 | |
3822 | bool isConst() const { return getFastTypeQuals().hasConst(); } |
3823 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } |
3824 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } |
3825 | |
3826 | /// Determine the type of an expression that calls a function of |
3827 | /// this type. |
3828 | QualType getCallResultType(const ASTContext &Context) const { |
3829 | return getReturnType().getNonLValueExprType(Context); |
3830 | } |
3831 | |
3832 | static StringRef getNameForCallConv(CallingConv CC); |
3833 | |
3834 | static bool classof(const Type *T) { |
3835 | return T->getTypeClass() == FunctionNoProto || |
3836 | T->getTypeClass() == FunctionProto; |
3837 | } |
3838 | }; |
3839 | |
3840 | /// Represents a K&R-style 'int foo()' function, which has |
3841 | /// no information available about its arguments. |
3842 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { |
3843 | friend class ASTContext; // ASTContext creates these. |
3844 | |
3845 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) |
3846 | : FunctionType(FunctionNoProto, Result, Canonical, |
3847 | Result->getDependence() & |
3848 | ~(TypeDependence::DependentInstantiation | |
3849 | TypeDependence::UnexpandedPack), |
3850 | Info) {} |
3851 | |
3852 | public: |
3853 | // No additional state past what FunctionType provides. |
3854 | |
3855 | bool isSugared() const { return false; } |
3856 | QualType desugar() const { return QualType(this, 0); } |
3857 | |
3858 | void Profile(llvm::FoldingSetNodeID &ID) { |
3859 | Profile(ID, getReturnType(), getExtInfo()); |
3860 | } |
3861 | |
3862 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, |
3863 | ExtInfo Info) { |
3864 | Info.Profile(ID); |
3865 | ID.AddPointer(ResultType.getAsOpaquePtr()); |
3866 | } |
3867 | |
3868 | static bool classof(const Type *T) { |
3869 | return T->getTypeClass() == FunctionNoProto; |
3870 | } |
3871 | }; |
3872 | |
3873 | /// Represents a prototype with parameter type info, e.g. |
3874 | /// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no |
3875 | /// parameters, not as having a single void parameter. Such a type can have |
3876 | /// an exception specification, but this specification is not part of the |
3877 | /// canonical type. FunctionProtoType has several trailing objects, some of |
3878 | /// which optional. For more information about the trailing objects see |
3879 | /// the first comment inside FunctionProtoType. |
3880 | class FunctionProtoType final |
3881 | : public FunctionType, |
3882 | public llvm::FoldingSetNode, |
3883 | private llvm::TrailingObjects< |
3884 | FunctionProtoType, QualType, SourceLocation, |
3885 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, |
3886 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { |
3887 | friend class ASTContext; // ASTContext creates these. |
3888 | friend TrailingObjects; |
3889 | |
3890 | // FunctionProtoType is followed by several trailing objects, some of |
3891 | // which optional. They are in order: |
3892 | // |
3893 | // * An array of getNumParams() QualType holding the parameter types. |
3894 | // Always present. Note that for the vast majority of FunctionProtoType, |
3895 | // these will be the only trailing objects. |
3896 | // |
3897 | // * Optionally if the function is variadic, the SourceLocation of the |
3898 | // ellipsis. |
3899 | // |
3900 | // * Optionally if some extra data is stored in FunctionTypeExtraBitfields |
3901 | // (see FunctionTypeExtraBitfields and FunctionTypeBitfields): |
3902 | // a single FunctionTypeExtraBitfields. Present if and only if |
3903 | // hasExtraBitfields() is true. |
3904 | // |
3905 | // * Optionally exactly one of: |
3906 | // * an array of getNumExceptions() ExceptionType, |
3907 | // * a single Expr *, |
3908 | // * a pair of FunctionDecl *, |
3909 | // * a single FunctionDecl * |
3910 | // used to store information about the various types of exception |
3911 | // specification. See getExceptionSpecSize for the details. |
3912 | // |
3913 | // * Optionally an array of getNumParams() ExtParameterInfo holding |
3914 | // an ExtParameterInfo for each of the parameters. Present if and |
3915 | // only if hasExtParameterInfos() is true. |
3916 | // |
3917 | // * Optionally a Qualifiers object to represent extra qualifiers that can't |
3918 | // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and only |
3919 | // if hasExtQualifiers() is true. |
3920 | // |
3921 | // The optional FunctionTypeExtraBitfields has to be before the data |
3922 | // related to the exception specification since it contains the number |
3923 | // of exception types. |
3924 | // |
3925 | // We put the ExtParameterInfos last. If all were equal, it would make |
3926 | // more sense to put these before the exception specification, because |
3927 | // it's much easier to skip past them compared to the elaborate switch |
3928 | // required to skip the exception specification. However, all is not |
3929 | // equal; ExtParameterInfos are used to model very uncommon features, |
3930 | // and it's better not to burden the more common paths. |
3931 | |
3932 | public: |
3933 | /// Holds information about the various types of exception specification. |
3934 | /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is |
3935 | /// used to group together the various bits of information about the |
3936 | /// exception specification. |
3937 | struct ExceptionSpecInfo { |
3938 | /// The kind of exception specification this is. |
3939 | ExceptionSpecificationType Type = EST_None; |
3940 | |
3941 | /// Explicitly-specified list of exception types. |
3942 | ArrayRef<QualType> Exceptions; |
3943 | |
3944 | /// Noexcept expression, if this is a computed noexcept specification. |
3945 | Expr *NoexceptExpr = nullptr; |
3946 | |
3947 | /// The function whose exception specification this is, for |
3948 | /// EST_Unevaluated and EST_Uninstantiated. |
3949 | FunctionDecl *SourceDecl = nullptr; |
3950 | |
3951 | /// The function template whose exception specification this is instantiated |
3952 | /// from, for EST_Uninstantiated. |
3953 | FunctionDecl *SourceTemplate = nullptr; |
3954 | |
3955 | ExceptionSpecInfo() = default; |
3956 | |
3957 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} |
3958 | }; |
3959 | |
3960 | /// Extra information about a function prototype. ExtProtoInfo is not |
3961 | /// stored as such in FunctionProtoType but is used to group together |
3962 | /// the various bits of extra information about a function prototype. |
3963 | struct ExtProtoInfo { |
3964 | FunctionType::ExtInfo ExtInfo; |
3965 | bool Variadic : 1; |
3966 | bool HasTrailingReturn : 1; |
3967 | Qualifiers TypeQuals; |
3968 | RefQualifierKind RefQualifier = RQ_None; |
3969 | ExceptionSpecInfo ExceptionSpec; |
3970 | const ExtParameterInfo *ExtParameterInfos = nullptr; |
3971 | SourceLocation EllipsisLoc; |
3972 | |
3973 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} |
3974 | |
3975 | ExtProtoInfo(CallingConv CC) |
3976 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} |
3977 | |
3978 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { |
3979 | ExtProtoInfo Result(*this); |
3980 | Result.ExceptionSpec = ESI; |
3981 | return Result; |
3982 | } |
3983 | }; |
3984 | |
3985 | private: |
3986 | unsigned numTrailingObjects(OverloadToken<QualType>) const { |
3987 | return getNumParams(); |
3988 | } |
3989 | |
3990 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
3991 | return isVariadic(); |
3992 | } |
3993 | |
3994 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { |
3995 | return hasExtraBitfields(); |
3996 | } |
3997 | |
3998 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { |
3999 | return getExceptionSpecSize().NumExceptionType; |
4000 | } |
4001 | |
4002 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { |
4003 | return getExceptionSpecSize().NumExprPtr; |
4004 | } |
4005 | |
4006 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { |
4007 | return getExceptionSpecSize().NumFunctionDeclPtr; |
4008 | } |
4009 | |
4010 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
4011 | return hasExtParameterInfos() ? getNumParams() : 0; |
4012 | } |
4013 | |
4014 | /// Determine whether there are any argument types that |
4015 | /// contain an unexpanded parameter pack. |
4016 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, |
4017 | unsigned numArgs) { |
4018 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) |
4019 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) |
4020 | return true; |
4021 | |
4022 | return false; |
4023 | } |
4024 | |
4025 | FunctionProtoType(QualType result, ArrayRef<QualType> params, |
4026 | QualType canonical, const ExtProtoInfo &epi); |
4027 | |
4028 | /// This struct is returned by getExceptionSpecSize and is used to |
4029 | /// translate an ExceptionSpecificationType to the number and kind |
4030 | /// of trailing objects related to the exception specification. |
4031 | struct ExceptionSpecSizeHolder { |
4032 | unsigned NumExceptionType; |
4033 | unsigned NumExprPtr; |
4034 | unsigned NumFunctionDeclPtr; |
4035 | }; |
4036 | |
4037 | /// Return the number and kind of trailing objects |
4038 | /// related to the exception specification. |
4039 | static ExceptionSpecSizeHolder |
4040 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { |
4041 | switch (EST) { |
4042 | case EST_None: |
4043 | case EST_DynamicNone: |
4044 | case EST_MSAny: |
4045 | case EST_BasicNoexcept: |
4046 | case EST_Unparsed: |
4047 | case EST_NoThrow: |
4048 | return {0, 0, 0}; |
4049 | |
4050 | case EST_Dynamic: |
4051 | return {NumExceptions, 0, 0}; |
4052 | |
4053 | case EST_DependentNoexcept: |
4054 | case EST_NoexceptFalse: |
4055 | case EST_NoexceptTrue: |
4056 | return {0, 1, 0}; |
4057 | |
4058 | case EST_Uninstantiated: |
4059 | return {0, 0, 2}; |
4060 | |
4061 | case EST_Unevaluated: |
4062 | return {0, 0, 1}; |
4063 | } |
4064 | llvm_unreachable("bad exception specification kind")::llvm::llvm_unreachable_internal("bad exception specification kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4064); |
4065 | } |
4066 | |
4067 | /// Return the number and kind of trailing objects |
4068 | /// related to the exception specification. |
4069 | ExceptionSpecSizeHolder getExceptionSpecSize() const { |
4070 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); |
4071 | } |
4072 | |
4073 | /// Whether the trailing FunctionTypeExtraBitfields is present. |
4074 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { |
4075 | // If the exception spec type is EST_Dynamic then we have > 0 exception |
4076 | // types and the exact number is stored in FunctionTypeExtraBitfields. |
4077 | return EST == EST_Dynamic; |
4078 | } |
4079 | |
4080 | /// Whether the trailing FunctionTypeExtraBitfields is present. |
4081 | bool hasExtraBitfields() const { |
4082 | return hasExtraBitfields(getExceptionSpecType()); |
4083 | } |
4084 | |
4085 | bool hasExtQualifiers() const { |
4086 | return FunctionTypeBits.HasExtQuals; |
4087 | } |
4088 | |
4089 | public: |
4090 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } |
4091 | |
4092 | QualType getParamType(unsigned i) const { |
4093 | assert(i < getNumParams() && "invalid parameter index")((i < getNumParams() && "invalid parameter index") ? static_cast<void> (0) : __assert_fail ("i < getNumParams() && \"invalid parameter index\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4093, __PRETTY_FUNCTION__)); |
4094 | return param_type_begin()[i]; |
4095 | } |
4096 | |
4097 | ArrayRef<QualType> getParamTypes() const { |
4098 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); |
4099 | } |
4100 | |
4101 | ExtProtoInfo getExtProtoInfo() const { |
4102 | ExtProtoInfo EPI; |
4103 | EPI.ExtInfo = getExtInfo(); |
4104 | EPI.Variadic = isVariadic(); |
4105 | EPI.EllipsisLoc = getEllipsisLoc(); |
4106 | EPI.HasTrailingReturn = hasTrailingReturn(); |
4107 | EPI.ExceptionSpec = getExceptionSpecInfo(); |
4108 | EPI.TypeQuals = getMethodQuals(); |
4109 | EPI.RefQualifier = getRefQualifier(); |
4110 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); |
4111 | return EPI; |
4112 | } |
4113 | |
4114 | /// Get the kind of exception specification on this function. |
4115 | ExceptionSpecificationType getExceptionSpecType() const { |
4116 | return static_cast<ExceptionSpecificationType>( |
4117 | FunctionTypeBits.ExceptionSpecType); |
4118 | } |
4119 | |
4120 | /// Return whether this function has any kind of exception spec. |
4121 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } |
4122 | |
4123 | /// Return whether this function has a dynamic (throw) exception spec. |
4124 | bool hasDynamicExceptionSpec() const { |
4125 | return isDynamicExceptionSpec(getExceptionSpecType()); |
4126 | } |
4127 | |
4128 | /// Return whether this function has a noexcept exception spec. |
4129 | bool hasNoexceptExceptionSpec() const { |
4130 | return isNoexceptExceptionSpec(getExceptionSpecType()); |
4131 | } |
4132 | |
4133 | /// Return whether this function has a dependent exception spec. |
4134 | bool hasDependentExceptionSpec() const; |
4135 | |
4136 | /// Return whether this function has an instantiation-dependent exception |
4137 | /// spec. |
4138 | bool hasInstantiationDependentExceptionSpec() const; |
4139 | |
4140 | /// Return all the available information about this type's exception spec. |
4141 | ExceptionSpecInfo getExceptionSpecInfo() const { |
4142 | ExceptionSpecInfo Result; |
4143 | Result.Type = getExceptionSpecType(); |
4144 | if (Result.Type == EST_Dynamic) { |
4145 | Result.Exceptions = exceptions(); |
4146 | } else if (isComputedNoexcept(Result.Type)) { |
4147 | Result.NoexceptExpr = getNoexceptExpr(); |
4148 | } else if (Result.Type == EST_Uninstantiated) { |
4149 | Result.SourceDecl = getExceptionSpecDecl(); |
4150 | Result.SourceTemplate = getExceptionSpecTemplate(); |
4151 | } else if (Result.Type == EST_Unevaluated) { |
4152 | Result.SourceDecl = getExceptionSpecDecl(); |
4153 | } |
4154 | return Result; |
4155 | } |
4156 | |
4157 | /// Return the number of types in the exception specification. |
4158 | unsigned getNumExceptions() const { |
4159 | return getExceptionSpecType() == EST_Dynamic |
4160 | ? getTrailingObjects<FunctionTypeExtraBitfields>() |
4161 | ->NumExceptionType |
4162 | : 0; |
4163 | } |
4164 | |
4165 | /// Return the ith exception type, where 0 <= i < getNumExceptions(). |
4166 | QualType getExceptionType(unsigned i) const { |
4167 | assert(i < getNumExceptions() && "Invalid exception number!")((i < getNumExceptions() && "Invalid exception number!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumExceptions() && \"Invalid exception number!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4167, __PRETTY_FUNCTION__)); |
4168 | return exception_begin()[i]; |
4169 | } |
4170 | |
4171 | /// Return the expression inside noexcept(expression), or a null pointer |
4172 | /// if there is none (because the exception spec is not of this form). |
4173 | Expr *getNoexceptExpr() const { |
4174 | if (!isComputedNoexcept(getExceptionSpecType())) |
4175 | return nullptr; |
4176 | return *getTrailingObjects<Expr *>(); |
4177 | } |
4178 | |
4179 | /// If this function type has an exception specification which hasn't |
4180 | /// been determined yet (either because it has not been evaluated or because |
4181 | /// it has not been instantiated), this is the function whose exception |
4182 | /// specification is represented by this type. |
4183 | FunctionDecl *getExceptionSpecDecl() const { |
4184 | if (getExceptionSpecType() != EST_Uninstantiated && |
4185 | getExceptionSpecType() != EST_Unevaluated) |
4186 | return nullptr; |
4187 | return getTrailingObjects<FunctionDecl *>()[0]; |
4188 | } |
4189 | |
4190 | /// If this function type has an uninstantiated exception |
4191 | /// specification, this is the function whose exception specification |
4192 | /// should be instantiated to find the exception specification for |
4193 | /// this type. |
4194 | FunctionDecl *getExceptionSpecTemplate() const { |
4195 | if (getExceptionSpecType() != EST_Uninstantiated) |
4196 | return nullptr; |
4197 | return getTrailingObjects<FunctionDecl *>()[1]; |
4198 | } |
4199 | |
4200 | /// Determine whether this function type has a non-throwing exception |
4201 | /// specification. |
4202 | CanThrowResult canThrow() const; |
4203 | |
4204 | /// Determine whether this function type has a non-throwing exception |
4205 | /// specification. If this depends on template arguments, returns |
4206 | /// \c ResultIfDependent. |
4207 | bool isNothrow(bool ResultIfDependent = false) const { |
4208 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; |
4209 | } |
4210 | |
4211 | /// Whether this function prototype is variadic. |
4212 | bool isVariadic() const { return FunctionTypeBits.Variadic; } |
4213 | |
4214 | SourceLocation getEllipsisLoc() const { |
4215 | return isVariadic() ? *getTrailingObjects<SourceLocation>() |
4216 | : SourceLocation(); |
4217 | } |
4218 | |
4219 | /// Determines whether this function prototype contains a |
4220 | /// parameter pack at the end. |
4221 | /// |
4222 | /// A function template whose last parameter is a parameter pack can be |
4223 | /// called with an arbitrary number of arguments, much like a variadic |
4224 | /// function. |
4225 | bool isTemplateVariadic() const; |
4226 | |
4227 | /// Whether this function prototype has a trailing return type. |
4228 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } |
4229 | |
4230 | Qualifiers getMethodQuals() const { |
4231 | if (hasExtQualifiers()) |
4232 | return *getTrailingObjects<Qualifiers>(); |
4233 | else |
4234 | return getFastTypeQuals(); |
4235 | } |
4236 | |
4237 | /// Retrieve the ref-qualifier associated with this function type. |
4238 | RefQualifierKind getRefQualifier() const { |
4239 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); |
4240 | } |
4241 | |
4242 | using param_type_iterator = const QualType *; |
4243 | using param_type_range = llvm::iterator_range<param_type_iterator>; |
4244 | |
4245 | param_type_range param_types() const { |
4246 | return param_type_range(param_type_begin(), param_type_end()); |
4247 | } |
4248 | |
4249 | param_type_iterator param_type_begin() const { |
4250 | return getTrailingObjects<QualType>(); |
4251 | } |
4252 | |
4253 | param_type_iterator param_type_end() const { |
4254 | return param_type_begin() + getNumParams(); |
4255 | } |
4256 | |
4257 | using exception_iterator = const QualType *; |
4258 | |
4259 | ArrayRef<QualType> exceptions() const { |
4260 | return llvm::makeArrayRef(exception_begin(), exception_end()); |
4261 | } |
4262 | |
4263 | exception_iterator exception_begin() const { |
4264 | return reinterpret_cast<exception_iterator>( |
4265 | getTrailingObjects<ExceptionType>()); |
4266 | } |
4267 | |
4268 | exception_iterator exception_end() const { |
4269 | return exception_begin() + getNumExceptions(); |
4270 | } |
4271 | |
4272 | /// Is there any interesting extra information for any of the parameters |
4273 | /// of this function type? |
4274 | bool hasExtParameterInfos() const { |
4275 | return FunctionTypeBits.HasExtParameterInfos; |
4276 | } |
4277 | |
4278 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
4279 | assert(hasExtParameterInfos())((hasExtParameterInfos()) ? static_cast<void> (0) : __assert_fail ("hasExtParameterInfos()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4279, __PRETTY_FUNCTION__)); |
4280 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), |
4281 | getNumParams()); |
4282 | } |
4283 | |
4284 | /// Return a pointer to the beginning of the array of extra parameter |
4285 | /// information, if present, or else null if none of the parameters |
4286 | /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos. |
4287 | const ExtParameterInfo *getExtParameterInfosOrNull() const { |
4288 | if (!hasExtParameterInfos()) |
4289 | return nullptr; |
4290 | return getTrailingObjects<ExtParameterInfo>(); |
4291 | } |
4292 | |
4293 | ExtParameterInfo getExtParameterInfo(unsigned I) const { |
4294 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4294, __PRETTY_FUNCTION__)); |
4295 | if (hasExtParameterInfos()) |
4296 | return getTrailingObjects<ExtParameterInfo>()[I]; |
4297 | return ExtParameterInfo(); |
4298 | } |
4299 | |
4300 | ParameterABI getParameterABI(unsigned I) const { |
4301 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4301, __PRETTY_FUNCTION__)); |
4302 | if (hasExtParameterInfos()) |
4303 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); |
4304 | return ParameterABI::Ordinary; |
4305 | } |
4306 | |
4307 | bool isParamConsumed(unsigned I) const { |
4308 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4308, __PRETTY_FUNCTION__)); |
4309 | if (hasExtParameterInfos()) |
4310 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); |
4311 | return false; |
4312 | } |
4313 | |
4314 | bool isSugared() const { return false; } |
4315 | QualType desugar() const { return QualType(this, 0); } |
4316 | |
4317 | void printExceptionSpecification(raw_ostream &OS, |
4318 | const PrintingPolicy &Policy) const; |
4319 | |
4320 | static bool classof(const Type *T) { |
4321 | return T->getTypeClass() == FunctionProto; |
4322 | } |
4323 | |
4324 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); |
4325 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
4326 | param_type_iterator ArgTys, unsigned NumArgs, |
4327 | const ExtProtoInfo &EPI, const ASTContext &Context, |
4328 | bool Canonical); |
4329 | }; |
4330 | |
4331 | /// Represents the dependent type named by a dependently-scoped |
4332 | /// typename using declaration, e.g. |
4333 | /// using typename Base<T>::foo; |
4334 | /// |
4335 | /// Template instantiation turns these into the underlying type. |
4336 | class UnresolvedUsingType : public Type { |
4337 | friend class ASTContext; // ASTContext creates these. |
4338 | |
4339 | UnresolvedUsingTypenameDecl *Decl; |
4340 | |
4341 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) |
4342 | : Type(UnresolvedUsing, QualType(), |
4343 | TypeDependence::DependentInstantiation), |
4344 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} |
4345 | |
4346 | public: |
4347 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } |
4348 | |
4349 | bool isSugared() const { return false; } |
4350 | QualType desugar() const { return QualType(this, 0); } |
4351 | |
4352 | static bool classof(const Type *T) { |
4353 | return T->getTypeClass() == UnresolvedUsing; |
4354 | } |
4355 | |
4356 | void Profile(llvm::FoldingSetNodeID &ID) { |
4357 | return Profile(ID, Decl); |
4358 | } |
4359 | |
4360 | static void Profile(llvm::FoldingSetNodeID &ID, |
4361 | UnresolvedUsingTypenameDecl *D) { |
4362 | ID.AddPointer(D); |
4363 | } |
4364 | }; |
4365 | |
4366 | class TypedefType : public Type { |
4367 | TypedefNameDecl *Decl; |
4368 | |
4369 | private: |
4370 | friend class ASTContext; // ASTContext creates these. |
4371 | |
4372 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, |
4373 | QualType can); |
4374 | |
4375 | public: |
4376 | TypedefNameDecl *getDecl() const { return Decl; } |
4377 | |
4378 | bool isSugared() const { return true; } |
4379 | QualType desugar() const; |
4380 | |
4381 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } |
4382 | }; |
4383 | |
4384 | /// Sugar type that represents a type that was qualified by a qualifier written |
4385 | /// as a macro invocation. |
4386 | class MacroQualifiedType : public Type { |
4387 | friend class ASTContext; // ASTContext creates these. |
4388 | |
4389 | QualType UnderlyingTy; |
4390 | const IdentifierInfo *MacroII; |
4391 | |
4392 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, |
4393 | const IdentifierInfo *MacroII) |
4394 | : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), |
4395 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { |
4396 | assert(isa<AttributedType>(UnderlyingTy) &&((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types." ) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4397, __PRETTY_FUNCTION__)) |
4397 | "Expected a macro qualified type to only wrap attributed types.")((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types." ) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4397, __PRETTY_FUNCTION__)); |
4398 | } |
4399 | |
4400 | public: |
4401 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
4402 | QualType getUnderlyingType() const { return UnderlyingTy; } |
4403 | |
4404 | /// Return this attributed type's modified type with no qualifiers attached to |
4405 | /// it. |
4406 | QualType getModifiedType() const; |
4407 | |
4408 | bool isSugared() const { return true; } |
4409 | QualType desugar() const; |
4410 | |
4411 | static bool classof(const Type *T) { |
4412 | return T->getTypeClass() == MacroQualified; |
4413 | } |
4414 | }; |
4415 | |
4416 | /// Represents a `typeof` (or __typeof__) expression (a GCC extension). |
4417 | class TypeOfExprType : public Type { |
4418 | Expr *TOExpr; |
4419 | |
4420 | protected: |
4421 | friend class ASTContext; // ASTContext creates these. |
4422 | |
4423 | TypeOfExprType(Expr *E, QualType can = QualType()); |
4424 | |
4425 | public: |
4426 | Expr *getUnderlyingExpr() const { return TOExpr; } |
4427 | |
4428 | /// Remove a single level of sugar. |
4429 | QualType desugar() const; |
4430 | |
4431 | /// Returns whether this type directly provides sugar. |
4432 | bool isSugared() const; |
4433 | |
4434 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } |
4435 | }; |
4436 | |
4437 | /// Internal representation of canonical, dependent |
4438 | /// `typeof(expr)` types. |
4439 | /// |
4440 | /// This class is used internally by the ASTContext to manage |
4441 | /// canonical, dependent types, only. Clients will only see instances |
4442 | /// of this class via TypeOfExprType nodes. |
4443 | class DependentTypeOfExprType |
4444 | : public TypeOfExprType, public llvm::FoldingSetNode { |
4445 | const ASTContext &Context; |
4446 | |
4447 | public: |
4448 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) |
4449 | : TypeOfExprType(E), Context(Context) {} |
4450 | |
4451 | void Profile(llvm::FoldingSetNodeID &ID) { |
4452 | Profile(ID, Context, getUnderlyingExpr()); |
4453 | } |
4454 | |
4455 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4456 | Expr *E); |
4457 | }; |
4458 | |
4459 | /// Represents `typeof(type)`, a GCC extension. |
4460 | class TypeOfType : public Type { |
4461 | friend class ASTContext; // ASTContext creates these. |
4462 | |
4463 | QualType TOType; |
4464 | |
4465 | TypeOfType(QualType T, QualType can) |
4466 | : Type(TypeOf, can, T->getDependence()), TOType(T) { |
4467 | assert(!isa<TypedefType>(can) && "Invalid canonical type")((!isa<TypedefType>(can) && "Invalid canonical type" ) ? static_cast<void> (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4467, __PRETTY_FUNCTION__)); |
4468 | } |
4469 | |
4470 | public: |
4471 | QualType getUnderlyingType() const { return TOType; } |
4472 | |
4473 | /// Remove a single level of sugar. |
4474 | QualType desugar() const { return getUnderlyingType(); } |
4475 | |
4476 | /// Returns whether this type directly provides sugar. |
4477 | bool isSugared() const { return true; } |
4478 | |
4479 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } |
4480 | }; |
4481 | |
4482 | /// Represents the type `decltype(expr)` (C++11). |
4483 | class DecltypeType : public Type { |
4484 | Expr *E; |
4485 | QualType UnderlyingType; |
4486 | |
4487 | protected: |
4488 | friend class ASTContext; // ASTContext creates these. |
4489 | |
4490 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); |
4491 | |
4492 | public: |
4493 | Expr *getUnderlyingExpr() const { return E; } |
4494 | QualType getUnderlyingType() const { return UnderlyingType; } |
4495 | |
4496 | /// Remove a single level of sugar. |
4497 | QualType desugar() const; |
4498 | |
4499 | /// Returns whether this type directly provides sugar. |
4500 | bool isSugared() const; |
4501 | |
4502 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } |
4503 | }; |
4504 | |
4505 | /// Internal representation of canonical, dependent |
4506 | /// decltype(expr) types. |
4507 | /// |
4508 | /// This class is used internally by the ASTContext to manage |
4509 | /// canonical, dependent types, only. Clients will only see instances |
4510 | /// of this class via DecltypeType nodes. |
4511 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { |
4512 | const ASTContext &Context; |
4513 | |
4514 | public: |
4515 | DependentDecltypeType(const ASTContext &Context, Expr *E); |
4516 | |
4517 | void Profile(llvm::FoldingSetNodeID &ID) { |
4518 | Profile(ID, Context, getUnderlyingExpr()); |
4519 | } |
4520 | |
4521 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4522 | Expr *E); |
4523 | }; |
4524 | |
4525 | /// A unary type transform, which is a type constructed from another. |
4526 | class UnaryTransformType : public Type { |
4527 | public: |
4528 | enum UTTKind { |
4529 | EnumUnderlyingType |
4530 | }; |
4531 | |
4532 | private: |
4533 | /// The untransformed type. |
4534 | QualType BaseType; |
4535 | |
4536 | /// The transformed type if not dependent, otherwise the same as BaseType. |
4537 | QualType UnderlyingType; |
4538 | |
4539 | UTTKind UKind; |
4540 | |
4541 | protected: |
4542 | friend class ASTContext; |
4543 | |
4544 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, |
4545 | QualType CanonicalTy); |
4546 | |
4547 | public: |
4548 | bool isSugared() const { return !isDependentType(); } |
4549 | QualType desugar() const { return UnderlyingType; } |
4550 | |
4551 | QualType getUnderlyingType() const { return UnderlyingType; } |
4552 | QualType getBaseType() const { return BaseType; } |
4553 | |
4554 | UTTKind getUTTKind() const { return UKind; } |
4555 | |
4556 | static bool classof(const Type *T) { |
4557 | return T->getTypeClass() == UnaryTransform; |
4558 | } |
4559 | }; |
4560 | |
4561 | /// Internal representation of canonical, dependent |
4562 | /// __underlying_type(type) types. |
4563 | /// |
4564 | /// This class is used internally by the ASTContext to manage |
4565 | /// canonical, dependent types, only. Clients will only see instances |
4566 | /// of this class via UnaryTransformType nodes. |
4567 | class DependentUnaryTransformType : public UnaryTransformType, |
4568 | public llvm::FoldingSetNode { |
4569 | public: |
4570 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, |
4571 | UTTKind UKind); |
4572 | |
4573 | void Profile(llvm::FoldingSetNodeID &ID) { |
4574 | Profile(ID, getBaseType(), getUTTKind()); |
4575 | } |
4576 | |
4577 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
4578 | UTTKind UKind) { |
4579 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
4580 | ID.AddInteger((unsigned)UKind); |
4581 | } |
4582 | }; |
4583 | |
4584 | class TagType : public Type { |
4585 | friend class ASTReader; |
4586 | template <class T> friend class serialization::AbstractTypeReader; |
4587 | |
4588 | /// Stores the TagDecl associated with this type. The decl may point to any |
4589 | /// TagDecl that declares the entity. |
4590 | TagDecl *decl; |
4591 | |
4592 | protected: |
4593 | TagType(TypeClass TC, const TagDecl *D, QualType can); |
4594 | |
4595 | public: |
4596 | TagDecl *getDecl() const; |
4597 | |
4598 | /// Determines whether this type is in the process of being defined. |
4599 | bool isBeingDefined() const; |
4600 | |
4601 | static bool classof(const Type *T) { |
4602 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; |
4603 | } |
4604 | }; |
4605 | |
4606 | /// A helper class that allows the use of isa/cast/dyncast |
4607 | /// to detect TagType objects of structs/unions/classes. |
4608 | class RecordType : public TagType { |
4609 | protected: |
4610 | friend class ASTContext; // ASTContext creates these. |
4611 | |
4612 | explicit RecordType(const RecordDecl *D) |
4613 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4614 | explicit RecordType(TypeClass TC, RecordDecl *D) |
4615 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4616 | |
4617 | public: |
4618 | RecordDecl *getDecl() const { |
4619 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); |
4620 | } |
4621 | |
4622 | /// Recursively check all fields in the record for const-ness. If any field |
4623 | /// is declared const, return true. Otherwise, return false. |
4624 | bool hasConstFields() const; |
4625 | |
4626 | bool isSugared() const { return false; } |
4627 | QualType desugar() const { return QualType(this, 0); } |
4628 | |
4629 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } |
4630 | }; |
4631 | |
4632 | /// A helper class that allows the use of isa/cast/dyncast |
4633 | /// to detect TagType objects of enums. |
4634 | class EnumType : public TagType { |
4635 | friend class ASTContext; // ASTContext creates these. |
4636 | |
4637 | explicit EnumType(const EnumDecl *D) |
4638 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4639 | |
4640 | public: |
4641 | EnumDecl *getDecl() const { |
4642 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); |
4643 | } |
4644 | |
4645 | bool isSugared() const { return false; } |
4646 | QualType desugar() const { return QualType(this, 0); } |
4647 | |
4648 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } |
4649 | }; |
4650 | |
4651 | /// An attributed type is a type to which a type attribute has been applied. |
4652 | /// |
4653 | /// The "modified type" is the fully-sugared type to which the attributed |
4654 | /// type was applied; generally it is not canonically equivalent to the |
4655 | /// attributed type. The "equivalent type" is the minimally-desugared type |
4656 | /// which the type is canonically equivalent to. |
4657 | /// |
4658 | /// For example, in the following attributed type: |
4659 | /// int32_t __attribute__((vector_size(16))) |
4660 | /// - the modified type is the TypedefType for int32_t |
4661 | /// - the equivalent type is VectorType(16, int32_t) |
4662 | /// - the canonical type is VectorType(16, int) |
4663 | class AttributedType : public Type, public llvm::FoldingSetNode { |
4664 | public: |
4665 | using Kind = attr::Kind; |
4666 | |
4667 | private: |
4668 | friend class ASTContext; // ASTContext creates these |
4669 | |
4670 | QualType ModifiedType; |
4671 | QualType EquivalentType; |
4672 | |
4673 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, |
4674 | QualType equivalent) |
4675 | : Type(Attributed, canon, equivalent->getDependence()), |
4676 | ModifiedType(modified), EquivalentType(equivalent) { |
4677 | AttributedTypeBits.AttrKind = attrKind; |
4678 | } |
4679 | |
4680 | public: |
4681 | Kind getAttrKind() const { |
4682 | return static_cast<Kind>(AttributedTypeBits.AttrKind); |
4683 | } |
4684 | |
4685 | QualType getModifiedType() const { return ModifiedType; } |
4686 | QualType getEquivalentType() const { return EquivalentType; } |
4687 | |
4688 | bool isSugared() const { return true; } |
4689 | QualType desugar() const { return getEquivalentType(); } |
4690 | |
4691 | /// Does this attribute behave like a type qualifier? |
4692 | /// |
4693 | /// A type qualifier adjusts a type to provide specialized rules for |
4694 | /// a specific object, like the standard const and volatile qualifiers. |
4695 | /// This includes attributes controlling things like nullability, |
4696 | /// address spaces, and ARC ownership. The value of the object is still |
4697 | /// largely described by the modified type. |
4698 | /// |
4699 | /// In contrast, many type attributes "rewrite" their modified type to |
4700 | /// produce a fundamentally different type, not necessarily related in any |
4701 | /// formalizable way to the original type. For example, calling convention |
4702 | /// and vector attributes are not simple type qualifiers. |
4703 | /// |
4704 | /// Type qualifiers are often, but not always, reflected in the canonical |
4705 | /// type. |
4706 | bool isQualifier() const; |
4707 | |
4708 | bool isMSTypeSpec() const; |
4709 | |
4710 | bool isCallingConv() const; |
4711 | |
4712 | llvm::Optional<NullabilityKind> getImmediateNullability() const; |
4713 | |
4714 | /// Retrieve the attribute kind corresponding to the given |
4715 | /// nullability kind. |
4716 | static Kind getNullabilityAttrKind(NullabilityKind kind) { |
4717 | switch (kind) { |
4718 | case NullabilityKind::NonNull: |
4719 | return attr::TypeNonNull; |
4720 | |
4721 | case NullabilityKind::Nullable: |
4722 | return attr::TypeNullable; |
4723 | |
4724 | case NullabilityKind::NullableResult: |
4725 | return attr::TypeNullableResult; |
4726 | |
4727 | case NullabilityKind::Unspecified: |
4728 | return attr::TypeNullUnspecified; |
4729 | } |
4730 | llvm_unreachable("Unknown nullability kind.")::llvm::llvm_unreachable_internal("Unknown nullability kind." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 4730); |
4731 | } |
4732 | |
4733 | /// Strip off the top-level nullability annotation on the given |
4734 | /// type, if it's there. |
4735 | /// |
4736 | /// \param T The type to strip. If the type is exactly an |
4737 | /// AttributedType specifying nullability (without looking through |
4738 | /// type sugar), the nullability is returned and this type changed |
4739 | /// to the underlying modified type. |
4740 | /// |
4741 | /// \returns the top-level nullability, if present. |
4742 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); |
4743 | |
4744 | void Profile(llvm::FoldingSetNodeID &ID) { |
4745 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); |
4746 | } |
4747 | |
4748 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, |
4749 | QualType modified, QualType equivalent) { |
4750 | ID.AddInteger(attrKind); |
4751 | ID.AddPointer(modified.getAsOpaquePtr()); |
4752 | ID.AddPointer(equivalent.getAsOpaquePtr()); |
4753 | } |
4754 | |
4755 | static bool classof(const Type *T) { |
4756 | return T->getTypeClass() == Attributed; |
4757 | } |
4758 | }; |
4759 | |
4760 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4761 | friend class ASTContext; // ASTContext creates these |
4762 | |
4763 | // Helper data collector for canonical types. |
4764 | struct CanonicalTTPTInfo { |
4765 | unsigned Depth : 15; |
4766 | unsigned ParameterPack : 1; |
4767 | unsigned Index : 16; |
4768 | }; |
4769 | |
4770 | union { |
4771 | // Info for the canonical type. |
4772 | CanonicalTTPTInfo CanTTPTInfo; |
4773 | |
4774 | // Info for the non-canonical type. |
4775 | TemplateTypeParmDecl *TTPDecl; |
4776 | }; |
4777 | |
4778 | /// Build a non-canonical type. |
4779 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) |
4780 | : Type(TemplateTypeParm, Canon, |
4781 | TypeDependence::DependentInstantiation | |
4782 | (Canon->getDependence() & TypeDependence::UnexpandedPack)), |
4783 | TTPDecl(TTPDecl) {} |
4784 | |
4785 | /// Build the canonical type. |
4786 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) |
4787 | : Type(TemplateTypeParm, QualType(this, 0), |
4788 | TypeDependence::DependentInstantiation | |
4789 | (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { |
4790 | CanTTPTInfo.Depth = D; |
4791 | CanTTPTInfo.Index = I; |
4792 | CanTTPTInfo.ParameterPack = PP; |
4793 | } |
4794 | |
4795 | const CanonicalTTPTInfo& getCanTTPTInfo() const { |
4796 | QualType Can = getCanonicalTypeInternal(); |
4797 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; |
4798 | } |
4799 | |
4800 | public: |
4801 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } |
4802 | unsigned getIndex() const { return getCanTTPTInfo().Index; } |
4803 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } |
4804 | |
4805 | TemplateTypeParmDecl *getDecl() const { |
4806 | return isCanonicalUnqualified() ? nullptr : TTPDecl; |
4807 | } |
4808 | |
4809 | IdentifierInfo *getIdentifier() const; |
4810 | |
4811 | bool isSugared() const { return false; } |
4812 | QualType desugar() const { return QualType(this, 0); } |
4813 | |
4814 | void Profile(llvm::FoldingSetNodeID &ID) { |
4815 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); |
4816 | } |
4817 | |
4818 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, |
4819 | unsigned Index, bool ParameterPack, |
4820 | TemplateTypeParmDecl *TTPDecl) { |
4821 | ID.AddInteger(Depth); |
4822 | ID.AddInteger(Index); |
4823 | ID.AddBoolean(ParameterPack); |
4824 | ID.AddPointer(TTPDecl); |
4825 | } |
4826 | |
4827 | static bool classof(const Type *T) { |
4828 | return T->getTypeClass() == TemplateTypeParm; |
4829 | } |
4830 | }; |
4831 | |
4832 | /// Represents the result of substituting a type for a template |
4833 | /// type parameter. |
4834 | /// |
4835 | /// Within an instantiated template, all template type parameters have |
4836 | /// been replaced with these. They are used solely to record that a |
4837 | /// type was originally written as a template type parameter; |
4838 | /// therefore they are never canonical. |
4839 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4840 | friend class ASTContext; |
4841 | |
4842 | // The original type parameter. |
4843 | const TemplateTypeParmType *Replaced; |
4844 | |
4845 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) |
4846 | : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()), |
4847 | Replaced(Param) {} |
4848 | |
4849 | public: |
4850 | /// Gets the template parameter that was substituted for. |
4851 | const TemplateTypeParmType *getReplacedParameter() const { |
4852 | return Replaced; |
4853 | } |
4854 | |
4855 | /// Gets the type that was substituted for the template |
4856 | /// parameter. |
4857 | QualType getReplacementType() const { |
4858 | return getCanonicalTypeInternal(); |
4859 | } |
4860 | |
4861 | bool isSugared() const { return true; } |
4862 | QualType desugar() const { return getReplacementType(); } |
4863 | |
4864 | void Profile(llvm::FoldingSetNodeID &ID) { |
4865 | Profile(ID, getReplacedParameter(), getReplacementType()); |
4866 | } |
4867 | |
4868 | static void Profile(llvm::FoldingSetNodeID &ID, |
4869 | const TemplateTypeParmType *Replaced, |
4870 | QualType Replacement) { |
4871 | ID.AddPointer(Replaced); |
4872 | ID.AddPointer(Replacement.getAsOpaquePtr()); |
4873 | } |
4874 | |
4875 | static bool classof(const Type *T) { |
4876 | return T->getTypeClass() == SubstTemplateTypeParm; |
4877 | } |
4878 | }; |
4879 | |
4880 | /// Represents the result of substituting a set of types for a template |
4881 | /// type parameter pack. |
4882 | /// |
4883 | /// When a pack expansion in the source code contains multiple parameter packs |
4884 | /// and those parameter packs correspond to different levels of template |
4885 | /// parameter lists, this type node is used to represent a template type |
4886 | /// parameter pack from an outer level, which has already had its argument pack |
4887 | /// substituted but that still lives within a pack expansion that itself |
4888 | /// could not be instantiated. When actually performing a substitution into |
4889 | /// that pack expansion (e.g., when all template parameters have corresponding |
4890 | /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType |
4891 | /// at the current pack substitution index. |
4892 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { |
4893 | friend class ASTContext; |
4894 | |
4895 | /// The original type parameter. |
4896 | const TemplateTypeParmType *Replaced; |
4897 | |
4898 | /// A pointer to the set of template arguments that this |
4899 | /// parameter pack is instantiated with. |
4900 | const TemplateArgument *Arguments; |
4901 | |
4902 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
4903 | QualType Canon, |
4904 | const TemplateArgument &ArgPack); |
4905 | |
4906 | public: |
4907 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } |
4908 | |
4909 | /// Gets the template parameter that was substituted for. |
4910 | const TemplateTypeParmType *getReplacedParameter() const { |
4911 | return Replaced; |
4912 | } |
4913 | |
4914 | unsigned getNumArgs() const { |
4915 | return SubstTemplateTypeParmPackTypeBits.NumArgs; |
4916 | } |
4917 | |
4918 | bool isSugared() const { return false; } |
4919 | QualType desugar() const { return QualType(this, 0); } |
4920 | |
4921 | TemplateArgument getArgumentPack() const; |
4922 | |
4923 | void Profile(llvm::FoldingSetNodeID &ID); |
4924 | static void Profile(llvm::FoldingSetNodeID &ID, |
4925 | const TemplateTypeParmType *Replaced, |
4926 | const TemplateArgument &ArgPack); |
4927 | |
4928 | static bool classof(const Type *T) { |
4929 | return T->getTypeClass() == SubstTemplateTypeParmPack; |
4930 | } |
4931 | }; |
4932 | |
4933 | /// Common base class for placeholders for types that get replaced by |
4934 | /// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced |
4935 | /// class template types, and constrained type names. |
4936 | /// |
4937 | /// These types are usually a placeholder for a deduced type. However, before |
4938 | /// the initializer is attached, or (usually) if the initializer is |
4939 | /// type-dependent, there is no deduced type and the type is canonical. In |
4940 | /// the latter case, it is also a dependent type. |
4941 | class DeducedType : public Type { |
4942 | protected: |
4943 | DeducedType(TypeClass TC, QualType DeducedAsType, |
4944 | TypeDependence ExtraDependence) |
4945 | : Type(TC, |
4946 | // FIXME: Retain the sugared deduced type? |
4947 | DeducedAsType.isNull() ? QualType(this, 0) |
4948 | : DeducedAsType.getCanonicalType(), |
4949 | ExtraDependence | (DeducedAsType.isNull() |
4950 | ? TypeDependence::None |
4951 | : DeducedAsType->getDependence() & |
4952 | ~TypeDependence::VariablyModified)) {} |
4953 | |
4954 | public: |
4955 | bool isSugared() const { return !isCanonicalUnqualified(); } |
4956 | QualType desugar() const { return getCanonicalTypeInternal(); } |
4957 | |
4958 | /// Get the type deduced for this placeholder type, or null if it's |
4959 | /// either not been deduced or was deduced to a dependent type. |
4960 | QualType getDeducedType() const { |
4961 | return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType(); |
4962 | } |
4963 | bool isDeduced() const { |
4964 | return !isCanonicalUnqualified() || isDependentType(); |
4965 | } |
4966 | |
4967 | static bool classof(const Type *T) { |
4968 | return T->getTypeClass() == Auto || |
4969 | T->getTypeClass() == DeducedTemplateSpecialization; |
4970 | } |
4971 | }; |
4972 | |
4973 | /// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained |
4974 | /// by a type-constraint. |
4975 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { |
4976 | friend class ASTContext; // ASTContext creates these |
4977 | |
4978 | ConceptDecl *TypeConstraintConcept; |
4979 | |
4980 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, |
4981 | TypeDependence ExtraDependence, ConceptDecl *CD, |
4982 | ArrayRef<TemplateArgument> TypeConstraintArgs); |
4983 | |
4984 | const TemplateArgument *getArgBuffer() const { |
4985 | return reinterpret_cast<const TemplateArgument*>(this+1); |
4986 | } |
4987 | |
4988 | TemplateArgument *getArgBuffer() { |
4989 | return reinterpret_cast<TemplateArgument*>(this+1); |
4990 | } |
4991 | |
4992 | public: |
4993 | /// Retrieve the template arguments. |
4994 | const TemplateArgument *getArgs() const { |
4995 | return getArgBuffer(); |
4996 | } |
4997 | |
4998 | /// Retrieve the number of template arguments. |
4999 | unsigned getNumArgs() const { |
5000 | return AutoTypeBits.NumArgs; |
5001 | } |
5002 | |
5003 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
5004 | |
5005 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { |
5006 | return {getArgs(), getNumArgs()}; |
5007 | } |
5008 | |
5009 | ConceptDecl *getTypeConstraintConcept() const { |
5010 | return TypeConstraintConcept; |
5011 | } |
5012 | |
5013 | bool isConstrained() const { |
5014 | return TypeConstraintConcept != nullptr; |
5015 | } |
5016 | |
5017 | bool isDecltypeAuto() const { |
5018 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; |
5019 | } |
5020 | |
5021 | AutoTypeKeyword getKeyword() const { |
5022 | return (AutoTypeKeyword)AutoTypeBits.Keyword; |
5023 | } |
5024 | |
5025 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5026 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), |
5027 | getTypeConstraintConcept(), getTypeConstraintArguments()); |
5028 | } |
5029 | |
5030 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
5031 | QualType Deduced, AutoTypeKeyword Keyword, |
5032 | bool IsDependent, ConceptDecl *CD, |
5033 | ArrayRef<TemplateArgument> Arguments); |
5034 | |
5035 | static bool classof(const Type *T) { |
5036 | return T->getTypeClass() == Auto; |
5037 | } |
5038 | }; |
5039 | |
5040 | /// Represents a C++17 deduced template specialization type. |
5041 | class DeducedTemplateSpecializationType : public DeducedType, |
5042 | public llvm::FoldingSetNode { |
5043 | friend class ASTContext; // ASTContext creates these |
5044 | |
5045 | /// The name of the template whose arguments will be deduced. |
5046 | TemplateName Template; |
5047 | |
5048 | DeducedTemplateSpecializationType(TemplateName Template, |
5049 | QualType DeducedAsType, |
5050 | bool IsDeducedAsDependent) |
5051 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, |
5052 | toTypeDependence(Template.getDependence()) | |
5053 | (IsDeducedAsDependent |
5054 | ? TypeDependence::DependentInstantiation |
5055 | : TypeDependence::None)), |
5056 | Template(Template) {} |
5057 | |
5058 | public: |
5059 | /// Retrieve the name of the template that we are deducing. |
5060 | TemplateName getTemplateName() const { return Template;} |
5061 | |
5062 | void Profile(llvm::FoldingSetNodeID &ID) { |
5063 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); |
5064 | } |
5065 | |
5066 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, |
5067 | QualType Deduced, bool IsDependent) { |
5068 | Template.Profile(ID); |
5069 | ID.AddPointer(Deduced.getAsOpaquePtr()); |
5070 | ID.AddBoolean(IsDependent); |
5071 | } |
5072 | |
5073 | static bool classof(const Type *T) { |
5074 | return T->getTypeClass() == DeducedTemplateSpecialization; |
5075 | } |
5076 | }; |
5077 | |
5078 | /// Represents a type template specialization; the template |
5079 | /// must be a class template, a type alias template, or a template |
5080 | /// template parameter. A template which cannot be resolved to one of |
5081 | /// these, e.g. because it is written with a dependent scope |
5082 | /// specifier, is instead represented as a |
5083 | /// @c DependentTemplateSpecializationType. |
5084 | /// |
5085 | /// A non-dependent template specialization type is always "sugar", |
5086 | /// typically for a \c RecordType. For example, a class template |
5087 | /// specialization type of \c vector<int> will refer to a tag type for |
5088 | /// the instantiation \c std::vector<int, std::allocator<int>> |
5089 | /// |
5090 | /// Template specializations are dependent if either the template or |
5091 | /// any of the template arguments are dependent, in which case the |
5092 | /// type may also be canonical. |
5093 | /// |
5094 | /// Instances of this type are allocated with a trailing array of |
5095 | /// TemplateArguments, followed by a QualType representing the |
5096 | /// non-canonical aliased type when the template is a type alias |
5097 | /// template. |
5098 | class alignas(8) TemplateSpecializationType |
5099 | : public Type, |
5100 | public llvm::FoldingSetNode { |
5101 | friend class ASTContext; // ASTContext creates these |
5102 | |
5103 | /// The name of the template being specialized. This is |
5104 | /// either a TemplateName::Template (in which case it is a |
5105 | /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a |
5106 | /// TypeAliasTemplateDecl*), a |
5107 | /// TemplateName::SubstTemplateTemplateParmPack, or a |
5108 | /// TemplateName::SubstTemplateTemplateParm (in which case the |
5109 | /// replacement must, recursively, be one of these). |
5110 | TemplateName Template; |
5111 | |
5112 | TemplateSpecializationType(TemplateName T, |
5113 | ArrayRef<TemplateArgument> Args, |
5114 | QualType Canon, |
5115 | QualType Aliased); |
5116 | |
5117 | public: |
5118 | /// Determine whether any of the given template arguments are dependent. |
5119 | /// |
5120 | /// The converted arguments should be supplied when known; whether an |
5121 | /// argument is dependent can depend on the conversions performed on it |
5122 | /// (for example, a 'const int' passed as a template argument might be |
5123 | /// dependent if the parameter is a reference but non-dependent if the |
5124 | /// parameter is an int). |
5125 | /// |
5126 | /// Note that the \p Args parameter is unused: this is intentional, to remind |
5127 | /// the caller that they need to pass in the converted arguments, not the |
5128 | /// specified arguments. |
5129 | static bool |
5130 | anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, |
5131 | ArrayRef<TemplateArgument> Converted); |
5132 | static bool |
5133 | anyDependentTemplateArguments(const TemplateArgumentListInfo &, |
5134 | ArrayRef<TemplateArgument> Converted); |
5135 | static bool anyInstantiationDependentTemplateArguments( |
5136 | ArrayRef<TemplateArgumentLoc> Args); |
5137 | |
5138 | /// True if this template specialization type matches a current |
5139 | /// instantiation in the context in which it is found. |
5140 | bool isCurrentInstantiation() const { |
5141 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); |
5142 | } |
5143 | |
5144 | /// Determine if this template specialization type is for a type alias |
5145 | /// template that has been substituted. |
5146 | /// |
5147 | /// Nearly every template specialization type whose template is an alias |
5148 | /// template will be substituted. However, this is not the case when |
5149 | /// the specialization contains a pack expansion but the template alias |
5150 | /// does not have a corresponding parameter pack, e.g., |
5151 | /// |
5152 | /// \code |
5153 | /// template<typename T, typename U, typename V> struct S; |
5154 | /// template<typename T, typename U> using A = S<T, int, U>; |
5155 | /// template<typename... Ts> struct X { |
5156 | /// typedef A<Ts...> type; // not a type alias |
5157 | /// }; |
5158 | /// \endcode |
5159 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } |
5160 | |
5161 | /// Get the aliased type, if this is a specialization of a type alias |
5162 | /// template. |
5163 | QualType getAliasedType() const { |
5164 | assert(isTypeAlias() && "not a type alias template specialization")((isTypeAlias() && "not a type alias template specialization" ) ? static_cast<void> (0) : __assert_fail ("isTypeAlias() && \"not a type alias template specialization\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5164, __PRETTY_FUNCTION__)); |
5165 | return *reinterpret_cast<const QualType*>(end()); |
5166 | } |
5167 | |
5168 | using iterator = const TemplateArgument *; |
5169 | |
5170 | iterator begin() const { return getArgs(); } |
5171 | iterator end() const; // defined inline in TemplateBase.h |
5172 | |
5173 | /// Retrieve the name of the template that we are specializing. |
5174 | TemplateName getTemplateName() const { return Template; } |
5175 | |
5176 | /// Retrieve the template arguments. |
5177 | const TemplateArgument *getArgs() const { |
5178 | return reinterpret_cast<const TemplateArgument *>(this + 1); |
5179 | } |
5180 | |
5181 | /// Retrieve the number of template arguments. |
5182 | unsigned getNumArgs() const { |
5183 | return TemplateSpecializationTypeBits.NumArgs; |
5184 | } |
5185 | |
5186 | /// Retrieve a specific template argument as a type. |
5187 | /// \pre \c isArgType(Arg) |
5188 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
5189 | |
5190 | ArrayRef<TemplateArgument> template_arguments() const { |
5191 | return {getArgs(), getNumArgs()}; |
5192 | } |
5193 | |
5194 | bool isSugared() const { |
5195 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); |
5196 | } |
5197 | |
5198 | QualType desugar() const { |
5199 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); |
5200 | } |
5201 | |
5202 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
5203 | Profile(ID, Template, template_arguments(), Ctx); |
5204 | if (isTypeAlias()) |
5205 | getAliasedType().Profile(ID); |
5206 | } |
5207 | |
5208 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, |
5209 | ArrayRef<TemplateArgument> Args, |
5210 | const ASTContext &Context); |
5211 | |
5212 | static bool classof(const Type *T) { |
5213 | return T->getTypeClass() == TemplateSpecialization; |
5214 | } |
5215 | }; |
5216 | |
5217 | /// Print a template argument list, including the '<' and '>' |
5218 | /// enclosing the template arguments. |
5219 | void printTemplateArgumentList(raw_ostream &OS, |
5220 | ArrayRef<TemplateArgument> Args, |
5221 | const PrintingPolicy &Policy, |
5222 | const TemplateParameterList *TPL = nullptr); |
5223 | |
5224 | void printTemplateArgumentList(raw_ostream &OS, |
5225 | ArrayRef<TemplateArgumentLoc> Args, |
5226 | const PrintingPolicy &Policy, |
5227 | const TemplateParameterList *TPL = nullptr); |
5228 | |
5229 | void printTemplateArgumentList(raw_ostream &OS, |
5230 | const TemplateArgumentListInfo &Args, |
5231 | const PrintingPolicy &Policy, |
5232 | const TemplateParameterList *TPL = nullptr); |
5233 | |
5234 | /// The injected class name of a C++ class template or class |
5235 | /// template partial specialization. Used to record that a type was |
5236 | /// spelled with a bare identifier rather than as a template-id; the |
5237 | /// equivalent for non-templated classes is just RecordType. |
5238 | /// |
5239 | /// Injected class name types are always dependent. Template |
5240 | /// instantiation turns these into RecordTypes. |
5241 | /// |
5242 | /// Injected class name types are always canonical. This works |
5243 | /// because it is impossible to compare an injected class name type |
5244 | /// with the corresponding non-injected template type, for the same |
5245 | /// reason that it is impossible to directly compare template |
5246 | /// parameters from different dependent contexts: injected class name |
5247 | /// types can only occur within the scope of a particular templated |
5248 | /// declaration, and within that scope every template specialization |
5249 | /// will canonicalize to the injected class name (when appropriate |
5250 | /// according to the rules of the language). |
5251 | class InjectedClassNameType : public Type { |
5252 | friend class ASTContext; // ASTContext creates these. |
5253 | friend class ASTNodeImporter; |
5254 | friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not |
5255 | // currently suitable for AST reading, too much |
5256 | // interdependencies. |
5257 | template <class T> friend class serialization::AbstractTypeReader; |
5258 | |
5259 | CXXRecordDecl *Decl; |
5260 | |
5261 | /// The template specialization which this type represents. |
5262 | /// For example, in |
5263 | /// template <class T> class A { ... }; |
5264 | /// this is A<T>, whereas in |
5265 | /// template <class X, class Y> class A<B<X,Y> > { ... }; |
5266 | /// this is A<B<X,Y> >. |
5267 | /// |
5268 | /// It is always unqualified, always a template specialization type, |
5269 | /// and always dependent. |
5270 | QualType InjectedType; |
5271 | |
5272 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) |
5273 | : Type(InjectedClassName, QualType(), |
5274 | TypeDependence::DependentInstantiation), |
5275 | Decl(D), InjectedType(TST) { |
5276 | assert(isa<TemplateSpecializationType>(TST))((isa<TemplateSpecializationType>(TST)) ? static_cast< void> (0) : __assert_fail ("isa<TemplateSpecializationType>(TST)" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5276, __PRETTY_FUNCTION__)); |
5277 | assert(!TST.hasQualifiers())((!TST.hasQualifiers()) ? static_cast<void> (0) : __assert_fail ("!TST.hasQualifiers()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5277, __PRETTY_FUNCTION__)); |
5278 | assert(TST->isDependentType())((TST->isDependentType()) ? static_cast<void> (0) : __assert_fail ("TST->isDependentType()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5278, __PRETTY_FUNCTION__)); |
5279 | } |
5280 | |
5281 | public: |
5282 | QualType getInjectedSpecializationType() const { return InjectedType; } |
5283 | |
5284 | const TemplateSpecializationType *getInjectedTST() const { |
5285 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); |
5286 | } |
5287 | |
5288 | TemplateName getTemplateName() const { |
5289 | return getInjectedTST()->getTemplateName(); |
5290 | } |
5291 | |
5292 | CXXRecordDecl *getDecl() const; |
5293 | |
5294 | bool isSugared() const { return false; } |
5295 | QualType desugar() const { return QualType(this, 0); } |
5296 | |
5297 | static bool classof(const Type *T) { |
5298 | return T->getTypeClass() == InjectedClassName; |
5299 | } |
5300 | }; |
5301 | |
5302 | /// The kind of a tag type. |
5303 | enum TagTypeKind { |
5304 | /// The "struct" keyword. |
5305 | TTK_Struct, |
5306 | |
5307 | /// The "__interface" keyword. |
5308 | TTK_Interface, |
5309 | |
5310 | /// The "union" keyword. |
5311 | TTK_Union, |
5312 | |
5313 | /// The "class" keyword. |
5314 | TTK_Class, |
5315 | |
5316 | /// The "enum" keyword. |
5317 | TTK_Enum |
5318 | }; |
5319 | |
5320 | /// The elaboration keyword that precedes a qualified type name or |
5321 | /// introduces an elaborated-type-specifier. |
5322 | enum ElaboratedTypeKeyword { |
5323 | /// The "struct" keyword introduces the elaborated-type-specifier. |
5324 | ETK_Struct, |
5325 | |
5326 | /// The "__interface" keyword introduces the elaborated-type-specifier. |
5327 | ETK_Interface, |
5328 | |
5329 | /// The "union" keyword introduces the elaborated-type-specifier. |
5330 | ETK_Union, |
5331 | |
5332 | /// The "class" keyword introduces the elaborated-type-specifier. |
5333 | ETK_Class, |
5334 | |
5335 | /// The "enum" keyword introduces the elaborated-type-specifier. |
5336 | ETK_Enum, |
5337 | |
5338 | /// The "typename" keyword precedes the qualified type name, e.g., |
5339 | /// \c typename T::type. |
5340 | ETK_Typename, |
5341 | |
5342 | /// No keyword precedes the qualified type name. |
5343 | ETK_None |
5344 | }; |
5345 | |
5346 | /// A helper class for Type nodes having an ElaboratedTypeKeyword. |
5347 | /// The keyword in stored in the free bits of the base class. |
5348 | /// Also provides a few static helpers for converting and printing |
5349 | /// elaborated type keyword and tag type kind enumerations. |
5350 | class TypeWithKeyword : public Type { |
5351 | protected: |
5352 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, |
5353 | QualType Canonical, TypeDependence Dependence) |
5354 | : Type(tc, Canonical, Dependence) { |
5355 | TypeWithKeywordBits.Keyword = Keyword; |
5356 | } |
5357 | |
5358 | public: |
5359 | ElaboratedTypeKeyword getKeyword() const { |
5360 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); |
5361 | } |
5362 | |
5363 | /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword. |
5364 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); |
5365 | |
5366 | /// Converts a type specifier (DeclSpec::TST) into a tag type kind. |
5367 | /// It is an error to provide a type specifier which *isn't* a tag kind here. |
5368 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); |
5369 | |
5370 | /// Converts a TagTypeKind into an elaborated type keyword. |
5371 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); |
5372 | |
5373 | /// Converts an elaborated type keyword into a TagTypeKind. |
5374 | /// It is an error to provide an elaborated type keyword |
5375 | /// which *isn't* a tag kind here. |
5376 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); |
5377 | |
5378 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); |
5379 | |
5380 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); |
5381 | |
5382 | static StringRef getTagTypeKindName(TagTypeKind Kind) { |
5383 | return getKeywordName(getKeywordForTagTypeKind(Kind)); |
5384 | } |
5385 | |
5386 | class CannotCastToThisType {}; |
5387 | static CannotCastToThisType classof(const Type *); |
5388 | }; |
5389 | |
5390 | /// Represents a type that was referred to using an elaborated type |
5391 | /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, |
5392 | /// or both. |
5393 | /// |
5394 | /// This type is used to keep track of a type name as written in the |
5395 | /// source code, including tag keywords and any nested-name-specifiers. |
5396 | /// The type itself is always "sugar", used to express what was written |
5397 | /// in the source code but containing no additional semantic information. |
5398 | class ElaboratedType final |
5399 | : public TypeWithKeyword, |
5400 | public llvm::FoldingSetNode, |
5401 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { |
5402 | friend class ASTContext; // ASTContext creates these |
5403 | friend TrailingObjects; |
5404 | |
5405 | /// The nested name specifier containing the qualifier. |
5406 | NestedNameSpecifier *NNS; |
5407 | |
5408 | /// The type that this qualified name refers to. |
5409 | QualType NamedType; |
5410 | |
5411 | /// The (re)declaration of this tag type owned by this occurrence is stored |
5412 | /// as a trailing object if there is one. Use getOwnedTagDecl to obtain |
5413 | /// it, or obtain a null pointer if there is none. |
5414 | |
5415 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5416 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) |
5417 | : TypeWithKeyword(Keyword, Elaborated, CanonType, |
5418 | // Any semantic dependence on the qualifier will have |
5419 | // been incorporated into NamedType. We still need to |
5420 | // track syntactic (instantiation / error / pack) |
5421 | // dependence on the qualifier. |
5422 | NamedType->getDependence() | |
5423 | (NNS ? toSyntacticDependence( |
5424 | toTypeDependence(NNS->getDependence())) |
5425 | : TypeDependence::None)), |
5426 | NNS(NNS), NamedType(NamedType) { |
5427 | ElaboratedTypeBits.HasOwnedTagDecl = false; |
5428 | if (OwnedTagDecl) { |
5429 | ElaboratedTypeBits.HasOwnedTagDecl = true; |
5430 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; |
5431 | } |
5432 | assert(!(Keyword == ETK_None && NNS == nullptr) &&((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5434, __PRETTY_FUNCTION__)) |
5433 | "ElaboratedType cannot have elaborated type keyword "((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5434, __PRETTY_FUNCTION__)) |
5434 | "and name qualifier both null.")((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5434, __PRETTY_FUNCTION__)); |
5435 | } |
5436 | |
5437 | public: |
5438 | /// Retrieve the qualification on this type. |
5439 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5440 | |
5441 | /// Retrieve the type named by the qualified-id. |
5442 | QualType getNamedType() const { return NamedType; } |
5443 | |
5444 | /// Remove a single level of sugar. |
5445 | QualType desugar() const { return getNamedType(); } |
5446 | |
5447 | /// Returns whether this type directly provides sugar. |
5448 | bool isSugared() const { return true; } |
5449 | |
5450 | /// Return the (re)declaration of this type owned by this occurrence of this |
5451 | /// type, or nullptr if there is none. |
5452 | TagDecl *getOwnedTagDecl() const { |
5453 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() |
5454 | : nullptr; |
5455 | } |
5456 | |
5457 | void Profile(llvm::FoldingSetNodeID &ID) { |
5458 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); |
5459 | } |
5460 | |
5461 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5462 | NestedNameSpecifier *NNS, QualType NamedType, |
5463 | TagDecl *OwnedTagDecl) { |
5464 | ID.AddInteger(Keyword); |
5465 | ID.AddPointer(NNS); |
5466 | NamedType.Profile(ID); |
5467 | ID.AddPointer(OwnedTagDecl); |
5468 | } |
5469 | |
5470 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } |
5471 | }; |
5472 | |
5473 | /// Represents a qualified type name for which the type name is |
5474 | /// dependent. |
5475 | /// |
5476 | /// DependentNameType represents a class of dependent types that involve a |
5477 | /// possibly dependent nested-name-specifier (e.g., "T::") followed by a |
5478 | /// name of a type. The DependentNameType may start with a "typename" (for a |
5479 | /// typename-specifier), "class", "struct", "union", or "enum" (for a |
5480 | /// dependent elaborated-type-specifier), or nothing (in contexts where we |
5481 | /// know that we must be referring to a type, e.g., in a base class specifier). |
5482 | /// Typically the nested-name-specifier is dependent, but in MSVC compatibility |
5483 | /// mode, this type is used with non-dependent names to delay name lookup until |
5484 | /// instantiation. |
5485 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { |
5486 | friend class ASTContext; // ASTContext creates these |
5487 | |
5488 | /// The nested name specifier containing the qualifier. |
5489 | NestedNameSpecifier *NNS; |
5490 | |
5491 | /// The type that this typename specifier refers to. |
5492 | const IdentifierInfo *Name; |
5493 | |
5494 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5495 | const IdentifierInfo *Name, QualType CanonType) |
5496 | : TypeWithKeyword(Keyword, DependentName, CanonType, |
5497 | TypeDependence::DependentInstantiation | |
5498 | toTypeDependence(NNS->getDependence())), |
5499 | NNS(NNS), Name(Name) {} |
5500 | |
5501 | public: |
5502 | /// Retrieve the qualification on this type. |
5503 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5504 | |
5505 | /// Retrieve the type named by the typename specifier as an identifier. |
5506 | /// |
5507 | /// This routine will return a non-NULL identifier pointer when the |
5508 | /// form of the original typename was terminated by an identifier, |
5509 | /// e.g., "typename T::type". |
5510 | const IdentifierInfo *getIdentifier() const { |
5511 | return Name; |
5512 | } |
5513 | |
5514 | bool isSugared() const { return false; } |
5515 | QualType desugar() const { return QualType(this, 0); } |
5516 | |
5517 | void Profile(llvm::FoldingSetNodeID &ID) { |
5518 | Profile(ID, getKeyword(), NNS, Name); |
5519 | } |
5520 | |
5521 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5522 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { |
5523 | ID.AddInteger(Keyword); |
5524 | ID.AddPointer(NNS); |
5525 | ID.AddPointer(Name); |
5526 | } |
5527 | |
5528 | static bool classof(const Type *T) { |
5529 | return T->getTypeClass() == DependentName; |
5530 | } |
5531 | }; |
5532 | |
5533 | /// Represents a template specialization type whose template cannot be |
5534 | /// resolved, e.g. |
5535 | /// A<T>::template B<T> |
5536 | class alignas(8) DependentTemplateSpecializationType |
5537 | : public TypeWithKeyword, |
5538 | public llvm::FoldingSetNode { |
5539 | friend class ASTContext; // ASTContext creates these |
5540 | |
5541 | /// The nested name specifier containing the qualifier. |
5542 | NestedNameSpecifier *NNS; |
5543 | |
5544 | /// The identifier of the template. |
5545 | const IdentifierInfo *Name; |
5546 | |
5547 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, |
5548 | NestedNameSpecifier *NNS, |
5549 | const IdentifierInfo *Name, |
5550 | ArrayRef<TemplateArgument> Args, |
5551 | QualType Canon); |
5552 | |
5553 | const TemplateArgument *getArgBuffer() const { |
5554 | return reinterpret_cast<const TemplateArgument*>(this+1); |
5555 | } |
5556 | |
5557 | TemplateArgument *getArgBuffer() { |
5558 | return reinterpret_cast<TemplateArgument*>(this+1); |
5559 | } |
5560 | |
5561 | public: |
5562 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5563 | const IdentifierInfo *getIdentifier() const { return Name; } |
5564 | |
5565 | /// Retrieve the template arguments. |
5566 | const TemplateArgument *getArgs() const { |
5567 | return getArgBuffer(); |
5568 | } |
5569 | |
5570 | /// Retrieve the number of template arguments. |
5571 | unsigned getNumArgs() const { |
5572 | return DependentTemplateSpecializationTypeBits.NumArgs; |
5573 | } |
5574 | |
5575 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
5576 | |
5577 | ArrayRef<TemplateArgument> template_arguments() const { |
5578 | return {getArgs(), getNumArgs()}; |
5579 | } |
5580 | |
5581 | using iterator = const TemplateArgument *; |
5582 | |
5583 | iterator begin() const { return getArgs(); } |
5584 | iterator end() const; // inline in TemplateBase.h |
5585 | |
5586 | bool isSugared() const { return false; } |
5587 | QualType desugar() const { return QualType(this, 0); } |
5588 | |
5589 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5590 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); |
5591 | } |
5592 | |
5593 | static void Profile(llvm::FoldingSetNodeID &ID, |
5594 | const ASTContext &Context, |
5595 | ElaboratedTypeKeyword Keyword, |
5596 | NestedNameSpecifier *Qualifier, |
5597 | const IdentifierInfo *Name, |
5598 | ArrayRef<TemplateArgument> Args); |
5599 | |
5600 | static bool classof(const Type *T) { |
5601 | return T->getTypeClass() == DependentTemplateSpecialization; |
5602 | } |
5603 | }; |
5604 | |
5605 | /// Represents a pack expansion of types. |
5606 | /// |
5607 | /// Pack expansions are part of C++11 variadic templates. A pack |
5608 | /// expansion contains a pattern, which itself contains one or more |
5609 | /// "unexpanded" parameter packs. When instantiated, a pack expansion |
5610 | /// produces a series of types, each instantiated from the pattern of |
5611 | /// the expansion, where the Ith instantiation of the pattern uses the |
5612 | /// Ith arguments bound to each of the unexpanded parameter packs. The |
5613 | /// pack expansion is considered to "expand" these unexpanded |
5614 | /// parameter packs. |
5615 | /// |
5616 | /// \code |
5617 | /// template<typename ...Types> struct tuple; |
5618 | /// |
5619 | /// template<typename ...Types> |
5620 | /// struct tuple_of_references { |
5621 | /// typedef tuple<Types&...> type; |
5622 | /// }; |
5623 | /// \endcode |
5624 | /// |
5625 | /// Here, the pack expansion \c Types&... is represented via a |
5626 | /// PackExpansionType whose pattern is Types&. |
5627 | class PackExpansionType : public Type, public llvm::FoldingSetNode { |
5628 | friend class ASTContext; // ASTContext creates these |
5629 | |
5630 | /// The pattern of the pack expansion. |
5631 | QualType Pattern; |
5632 | |
5633 | PackExpansionType(QualType Pattern, QualType Canon, |
5634 | Optional<unsigned> NumExpansions) |
5635 | : Type(PackExpansion, Canon, |
5636 | (Pattern->getDependence() | TypeDependence::Dependent | |
5637 | TypeDependence::Instantiation) & |
5638 | ~TypeDependence::UnexpandedPack), |
5639 | Pattern(Pattern) { |
5640 | PackExpansionTypeBits.NumExpansions = |
5641 | NumExpansions ? *NumExpansions + 1 : 0; |
5642 | } |
5643 | |
5644 | public: |
5645 | /// Retrieve the pattern of this pack expansion, which is the |
5646 | /// type that will be repeatedly instantiated when instantiating the |
5647 | /// pack expansion itself. |
5648 | QualType getPattern() const { return Pattern; } |
5649 | |
5650 | /// Retrieve the number of expansions that this pack expansion will |
5651 | /// generate, if known. |
5652 | Optional<unsigned> getNumExpansions() const { |
5653 | if (PackExpansionTypeBits.NumExpansions) |
5654 | return PackExpansionTypeBits.NumExpansions - 1; |
5655 | return None; |
5656 | } |
5657 | |
5658 | bool isSugared() const { return false; } |
5659 | QualType desugar() const { return QualType(this, 0); } |
5660 | |
5661 | void Profile(llvm::FoldingSetNodeID &ID) { |
5662 | Profile(ID, getPattern(), getNumExpansions()); |
5663 | } |
5664 | |
5665 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, |
5666 | Optional<unsigned> NumExpansions) { |
5667 | ID.AddPointer(Pattern.getAsOpaquePtr()); |
5668 | ID.AddBoolean(NumExpansions.hasValue()); |
5669 | if (NumExpansions) |
5670 | ID.AddInteger(*NumExpansions); |
5671 | } |
5672 | |
5673 | static bool classof(const Type *T) { |
5674 | return T->getTypeClass() == PackExpansion; |
5675 | } |
5676 | }; |
5677 | |
5678 | /// This class wraps the list of protocol qualifiers. For types that can |
5679 | /// take ObjC protocol qualifers, they can subclass this class. |
5680 | template <class T> |
5681 | class ObjCProtocolQualifiers { |
5682 | protected: |
5683 | ObjCProtocolQualifiers() = default; |
5684 | |
5685 | ObjCProtocolDecl * const *getProtocolStorage() const { |
5686 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); |
5687 | } |
5688 | |
5689 | ObjCProtocolDecl **getProtocolStorage() { |
5690 | return static_cast<T*>(this)->getProtocolStorageImpl(); |
5691 | } |
5692 | |
5693 | void setNumProtocols(unsigned N) { |
5694 | static_cast<T*>(this)->setNumProtocolsImpl(N); |
5695 | } |
5696 | |
5697 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { |
5698 | setNumProtocols(protocols.size()); |
5699 | assert(getNumProtocols() == protocols.size() &&((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count" ) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5700, __PRETTY_FUNCTION__)) |
5700 | "bitfield overflow in protocol count")((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count" ) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5700, __PRETTY_FUNCTION__)); |
5701 | if (!protocols.empty()) |
5702 | memcpy(getProtocolStorage(), protocols.data(), |
5703 | protocols.size() * sizeof(ObjCProtocolDecl*)); |
5704 | } |
5705 | |
5706 | public: |
5707 | using qual_iterator = ObjCProtocolDecl * const *; |
5708 | using qual_range = llvm::iterator_range<qual_iterator>; |
5709 | |
5710 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
5711 | qual_iterator qual_begin() const { return getProtocolStorage(); } |
5712 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } |
5713 | |
5714 | bool qual_empty() const { return getNumProtocols() == 0; } |
5715 | |
5716 | /// Return the number of qualifying protocols in this type, or 0 if |
5717 | /// there are none. |
5718 | unsigned getNumProtocols() const { |
5719 | return static_cast<const T*>(this)->getNumProtocolsImpl(); |
5720 | } |
5721 | |
5722 | /// Fetch a protocol by index. |
5723 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
5724 | assert(I < getNumProtocols() && "Out-of-range protocol access")((I < getNumProtocols() && "Out-of-range protocol access" ) ? static_cast<void> (0) : __assert_fail ("I < getNumProtocols() && \"Out-of-range protocol access\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5724, __PRETTY_FUNCTION__)); |
5725 | return qual_begin()[I]; |
5726 | } |
5727 | |
5728 | /// Retrieve all of the protocol qualifiers. |
5729 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { |
5730 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); |
5731 | } |
5732 | }; |
5733 | |
5734 | /// Represents a type parameter type in Objective C. It can take |
5735 | /// a list of protocols. |
5736 | class ObjCTypeParamType : public Type, |
5737 | public ObjCProtocolQualifiers<ObjCTypeParamType>, |
5738 | public llvm::FoldingSetNode { |
5739 | friend class ASTContext; |
5740 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; |
5741 | |
5742 | /// The number of protocols stored on this type. |
5743 | unsigned NumProtocols : 6; |
5744 | |
5745 | ObjCTypeParamDecl *OTPDecl; |
5746 | |
5747 | /// The protocols are stored after the ObjCTypeParamType node. In the |
5748 | /// canonical type, the list of protocols are sorted alphabetically |
5749 | /// and uniqued. |
5750 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5751 | |
5752 | /// Return the number of qualifying protocols in this interface type, |
5753 | /// or 0 if there are none. |
5754 | unsigned getNumProtocolsImpl() const { |
5755 | return NumProtocols; |
5756 | } |
5757 | |
5758 | void setNumProtocolsImpl(unsigned N) { |
5759 | NumProtocols = N; |
5760 | } |
5761 | |
5762 | ObjCTypeParamType(const ObjCTypeParamDecl *D, |
5763 | QualType can, |
5764 | ArrayRef<ObjCProtocolDecl *> protocols); |
5765 | |
5766 | public: |
5767 | bool isSugared() const { return true; } |
5768 | QualType desugar() const { return getCanonicalTypeInternal(); } |
5769 | |
5770 | static bool classof(const Type *T) { |
5771 | return T->getTypeClass() == ObjCTypeParam; |
5772 | } |
5773 | |
5774 | void Profile(llvm::FoldingSetNodeID &ID); |
5775 | static void Profile(llvm::FoldingSetNodeID &ID, |
5776 | const ObjCTypeParamDecl *OTPDecl, |
5777 | QualType CanonicalType, |
5778 | ArrayRef<ObjCProtocolDecl *> protocols); |
5779 | |
5780 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } |
5781 | }; |
5782 | |
5783 | /// Represents a class type in Objective C. |
5784 | /// |
5785 | /// Every Objective C type is a combination of a base type, a set of |
5786 | /// type arguments (optional, for parameterized classes) and a list of |
5787 | /// protocols. |
5788 | /// |
5789 | /// Given the following declarations: |
5790 | /// \code |
5791 | /// \@class C<T>; |
5792 | /// \@protocol P; |
5793 | /// \endcode |
5794 | /// |
5795 | /// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType |
5796 | /// with base C and no protocols. |
5797 | /// |
5798 | /// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P]. |
5799 | /// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no |
5800 | /// protocol list. |
5801 | /// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*', |
5802 | /// and protocol list [P]. |
5803 | /// |
5804 | /// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose |
5805 | /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType |
5806 | /// and no protocols. |
5807 | /// |
5808 | /// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType |
5809 | /// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually |
5810 | /// this should get its own sugar class to better represent the source. |
5811 | class ObjCObjectType : public Type, |
5812 | public ObjCProtocolQualifiers<ObjCObjectType> { |
5813 | friend class ObjCProtocolQualifiers<ObjCObjectType>; |
5814 | |
5815 | // ObjCObjectType.NumTypeArgs - the number of type arguments stored |
5816 | // after the ObjCObjectPointerType node. |
5817 | // ObjCObjectType.NumProtocols - the number of protocols stored |
5818 | // after the type arguments of ObjCObjectPointerType node. |
5819 | // |
5820 | // These protocols are those written directly on the type. If |
5821 | // protocol qualifiers ever become additive, the iterators will need |
5822 | // to get kindof complicated. |
5823 | // |
5824 | // In the canonical object type, these are sorted alphabetically |
5825 | // and uniqued. |
5826 | |
5827 | /// Either a BuiltinType or an InterfaceType or sugar for either. |
5828 | QualType BaseType; |
5829 | |
5830 | /// Cached superclass type. |
5831 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> |
5832 | CachedSuperClassType; |
5833 | |
5834 | QualType *getTypeArgStorage(); |
5835 | const QualType *getTypeArgStorage() const { |
5836 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); |
5837 | } |
5838 | |
5839 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5840 | /// Return the number of qualifying protocols in this interface type, |
5841 | /// or 0 if there are none. |
5842 | unsigned getNumProtocolsImpl() const { |
5843 | return ObjCObjectTypeBits.NumProtocols; |
5844 | } |
5845 | void setNumProtocolsImpl(unsigned N) { |
5846 | ObjCObjectTypeBits.NumProtocols = N; |
5847 | } |
5848 | |
5849 | protected: |
5850 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; |
5851 | |
5852 | ObjCObjectType(QualType Canonical, QualType Base, |
5853 | ArrayRef<QualType> typeArgs, |
5854 | ArrayRef<ObjCProtocolDecl *> protocols, |
5855 | bool isKindOf); |
5856 | |
5857 | ObjCObjectType(enum Nonce_ObjCInterface) |
5858 | : Type(ObjCInterface, QualType(), TypeDependence::None), |
5859 | BaseType(QualType(this_(), 0)) { |
5860 | ObjCObjectTypeBits.NumProtocols = 0; |
5861 | ObjCObjectTypeBits.NumTypeArgs = 0; |
5862 | ObjCObjectTypeBits.IsKindOf = 0; |
5863 | } |
5864 | |
5865 | void computeSuperClassTypeSlow() const; |
5866 | |
5867 | public: |
5868 | /// Gets the base type of this object type. This is always (possibly |
5869 | /// sugar for) one of: |
5870 | /// - the 'id' builtin type (as opposed to the 'id' type visible to the |
5871 | /// user, which is a typedef for an ObjCObjectPointerType) |
5872 | /// - the 'Class' builtin type (same caveat) |
5873 | /// - an ObjCObjectType (currently always an ObjCInterfaceType) |
5874 | QualType getBaseType() const { return BaseType; } |
5875 | |
5876 | bool isObjCId() const { |
5877 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); |
5878 | } |
5879 | |
5880 | bool isObjCClass() const { |
5881 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); |
5882 | } |
5883 | |
5884 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } |
5885 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } |
5886 | bool isObjCUnqualifiedIdOrClass() const { |
5887 | if (!qual_empty()) return false; |
5888 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) |
5889 | return T->getKind() == BuiltinType::ObjCId || |
5890 | T->getKind() == BuiltinType::ObjCClass; |
5891 | return false; |
5892 | } |
5893 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } |
5894 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } |
5895 | |
5896 | /// Gets the interface declaration for this object type, if the base type |
5897 | /// really is an interface. |
5898 | ObjCInterfaceDecl *getInterface() const; |
5899 | |
5900 | /// Determine whether this object type is "specialized", meaning |
5901 | /// that it has type arguments. |
5902 | bool isSpecialized() const; |
5903 | |
5904 | /// Determine whether this object type was written with type arguments. |
5905 | bool isSpecializedAsWritten() const { |
5906 | return ObjCObjectTypeBits.NumTypeArgs > 0; |
5907 | } |
5908 | |
5909 | /// Determine whether this object type is "unspecialized", meaning |
5910 | /// that it has no type arguments. |
5911 | bool isUnspecialized() const { return !isSpecialized(); } |
5912 | |
5913 | /// Determine whether this object type is "unspecialized" as |
5914 | /// written, meaning that it has no type arguments. |
5915 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
5916 | |
5917 | /// Retrieve the type arguments of this object type (semantically). |
5918 | ArrayRef<QualType> getTypeArgs() const; |
5919 | |
5920 | /// Retrieve the type arguments of this object type as they were |
5921 | /// written. |
5922 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
5923 | return llvm::makeArrayRef(getTypeArgStorage(), |
5924 | ObjCObjectTypeBits.NumTypeArgs); |
5925 | } |
5926 | |
5927 | /// Whether this is a "__kindof" type as written. |
5928 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } |
5929 | |
5930 | /// Whether this ia a "__kindof" type (semantically). |
5931 | bool isKindOfType() const; |
5932 | |
5933 | /// Retrieve the type of the superclass of this object type. |
5934 | /// |
5935 | /// This operation substitutes any type arguments into the |
5936 | /// superclass of the current class type, potentially producing a |
5937 | /// specialization of the superclass type. Produces a null type if |
5938 | /// there is no superclass. |
5939 | QualType getSuperClassType() const { |
5940 | if (!CachedSuperClassType.getInt()) |
5941 | computeSuperClassTypeSlow(); |
5942 | |
5943 | assert(CachedSuperClassType.getInt() && "Superclass not set?")((CachedSuperClassType.getInt() && "Superclass not set?" ) ? static_cast<void> (0) : __assert_fail ("CachedSuperClassType.getInt() && \"Superclass not set?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 5943, __PRETTY_FUNCTION__)); |
5944 | return QualType(CachedSuperClassType.getPointer(), 0); |
5945 | } |
5946 | |
5947 | /// Strip off the Objective-C "kindof" type and (with it) any |
5948 | /// protocol qualifiers. |
5949 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; |
5950 | |
5951 | bool isSugared() const { return false; } |
5952 | QualType desugar() const { return QualType(this, 0); } |
5953 | |
5954 | static bool classof(const Type *T) { |
5955 | return T->getTypeClass() == ObjCObject || |
5956 | T->getTypeClass() == ObjCInterface; |
5957 | } |
5958 | }; |
5959 | |
5960 | /// A class providing a concrete implementation |
5961 | /// of ObjCObjectType, so as to not increase the footprint of |
5962 | /// ObjCInterfaceType. Code outside of ASTContext and the core type |
5963 | /// system should not reference this type. |
5964 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { |
5965 | friend class ASTContext; |
5966 | |
5967 | // If anyone adds fields here, ObjCObjectType::getProtocolStorage() |
5968 | // will need to be modified. |
5969 | |
5970 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, |
5971 | ArrayRef<QualType> typeArgs, |
5972 | ArrayRef<ObjCProtocolDecl *> protocols, |
5973 | bool isKindOf) |
5974 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} |
5975 | |
5976 | public: |
5977 | void Profile(llvm::FoldingSetNodeID &ID); |
5978 | static void Profile(llvm::FoldingSetNodeID &ID, |
5979 | QualType Base, |
5980 | ArrayRef<QualType> typeArgs, |
5981 | ArrayRef<ObjCProtocolDecl *> protocols, |
5982 | bool isKindOf); |
5983 | }; |
5984 | |
5985 | inline QualType *ObjCObjectType::getTypeArgStorage() { |
5986 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); |
5987 | } |
5988 | |
5989 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { |
5990 | return reinterpret_cast<ObjCProtocolDecl**>( |
5991 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); |
5992 | } |
5993 | |
5994 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { |
5995 | return reinterpret_cast<ObjCProtocolDecl**>( |
5996 | static_cast<ObjCTypeParamType*>(this)+1); |
5997 | } |
5998 | |
5999 | /// Interfaces are the core concept in Objective-C for object oriented design. |
6000 | /// They basically correspond to C++ classes. There are two kinds of interface |
6001 | /// types: normal interfaces like `NSString`, and qualified interfaces, which |
6002 | /// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`. |
6003 | /// |
6004 | /// ObjCInterfaceType guarantees the following properties when considered |
6005 | /// as a subtype of its superclass, ObjCObjectType: |
6006 | /// - There are no protocol qualifiers. To reinforce this, code which |
6007 | /// tries to invoke the protocol methods via an ObjCInterfaceType will |
6008 | /// fail to compile. |
6009 | /// - It is its own base type. That is, if T is an ObjCInterfaceType*, |
6010 | /// T->getBaseType() == QualType(T, 0). |
6011 | class ObjCInterfaceType : public ObjCObjectType { |
6012 | friend class ASTContext; // ASTContext creates these. |
6013 | friend class ASTReader; |
6014 | friend class ObjCInterfaceDecl; |
6015 | template <class T> friend class serialization::AbstractTypeReader; |
6016 | |
6017 | mutable ObjCInterfaceDecl *Decl; |
6018 | |
6019 | ObjCInterfaceType(const ObjCInterfaceDecl *D) |
6020 | : ObjCObjectType(Nonce_ObjCInterface), |
6021 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} |
6022 | |
6023 | public: |
6024 | /// Get the declaration of this interface. |
6025 | ObjCInterfaceDecl *getDecl() const { return Decl; } |
6026 | |
6027 | bool isSugared() const { return false; } |
6028 | QualType desugar() const { return QualType(this, 0); } |
6029 | |
6030 | static bool classof(const Type *T) { |
6031 | return T->getTypeClass() == ObjCInterface; |
6032 | } |
6033 | |
6034 | // Nonsense to "hide" certain members of ObjCObjectType within this |
6035 | // class. People asking for protocols on an ObjCInterfaceType are |
6036 | // not going to get what they want: ObjCInterfaceTypes are |
6037 | // guaranteed to have no protocols. |
6038 | enum { |
6039 | qual_iterator, |
6040 | qual_begin, |
6041 | qual_end, |
6042 | getNumProtocols, |
6043 | getProtocol |
6044 | }; |
6045 | }; |
6046 | |
6047 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { |
6048 | QualType baseType = getBaseType(); |
6049 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { |
6050 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) |
6051 | return T->getDecl(); |
6052 | |
6053 | baseType = ObjT->getBaseType(); |
6054 | } |
6055 | |
6056 | return nullptr; |
6057 | } |
6058 | |
6059 | /// Represents a pointer to an Objective C object. |
6060 | /// |
6061 | /// These are constructed from pointer declarators when the pointee type is |
6062 | /// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class' |
6063 | /// types are typedefs for these, and the protocol-qualified types 'id<P>' |
6064 | /// and 'Class<P>' are translated into these. |
6065 | /// |
6066 | /// Pointers to pointers to Objective C objects are still PointerTypes; |
6067 | /// only the first level of pointer gets it own type implementation. |
6068 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { |
6069 | friend class ASTContext; // ASTContext creates these. |
6070 | |
6071 | QualType PointeeType; |
6072 | |
6073 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) |
6074 | : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), |
6075 | PointeeType(Pointee) {} |
6076 | |
6077 | public: |
6078 | /// Gets the type pointed to by this ObjC pointer. |
6079 | /// The result will always be an ObjCObjectType or sugar thereof. |
6080 | QualType getPointeeType() const { return PointeeType; } |
6081 | |
6082 | /// Gets the type pointed to by this ObjC pointer. Always returns non-null. |
6083 | /// |
6084 | /// This method is equivalent to getPointeeType() except that |
6085 | /// it discards any typedefs (or other sugar) between this |
6086 | /// type and the "outermost" object type. So for: |
6087 | /// \code |
6088 | /// \@class A; \@protocol P; \@protocol Q; |
6089 | /// typedef A<P> AP; |
6090 | /// typedef A A1; |
6091 | /// typedef A1<P> A1P; |
6092 | /// typedef A1P<Q> A1PQ; |
6093 | /// \endcode |
6094 | /// For 'A*', getObjectType() will return 'A'. |
6095 | /// For 'A<P>*', getObjectType() will return 'A<P>'. |
6096 | /// For 'AP*', getObjectType() will return 'A<P>'. |
6097 | /// For 'A1*', getObjectType() will return 'A'. |
6098 | /// For 'A1<P>*', getObjectType() will return 'A1<P>'. |
6099 | /// For 'A1P*', getObjectType() will return 'A1<P>'. |
6100 | /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because |
6101 | /// adding protocols to a protocol-qualified base discards the |
6102 | /// old qualifiers (for now). But if it didn't, getObjectType() |
6103 | /// would return 'A1P<Q>' (and we'd have to make iterating over |
6104 | /// qualifiers more complicated). |
6105 | const ObjCObjectType *getObjectType() const { |
6106 | return PointeeType->castAs<ObjCObjectType>(); |
6107 | } |
6108 | |
6109 | /// If this pointer points to an Objective C |
6110 | /// \@interface type, gets the type for that interface. Any protocol |
6111 | /// qualifiers on the interface are ignored. |
6112 | /// |
6113 | /// \return null if the base type for this pointer is 'id' or 'Class' |
6114 | const ObjCInterfaceType *getInterfaceType() const; |
6115 | |
6116 | /// If this pointer points to an Objective \@interface |
6117 | /// type, gets the declaration for that interface. |
6118 | /// |
6119 | /// \return null if the base type for this pointer is 'id' or 'Class' |
6120 | ObjCInterfaceDecl *getInterfaceDecl() const { |
6121 | return getObjectType()->getInterface(); |
6122 | } |
6123 | |
6124 | /// True if this is equivalent to the 'id' type, i.e. if |
6125 | /// its object type is the primitive 'id' type with no protocols. |
6126 | bool isObjCIdType() const { |
6127 | return getObjectType()->isObjCUnqualifiedId(); |
6128 | } |
6129 | |
6130 | /// True if this is equivalent to the 'Class' type, |
6131 | /// i.e. if its object tive is the primitive 'Class' type with no protocols. |
6132 | bool isObjCClassType() const { |
6133 | return getObjectType()->isObjCUnqualifiedClass(); |
6134 | } |
6135 | |
6136 | /// True if this is equivalent to the 'id' or 'Class' type, |
6137 | bool isObjCIdOrClassType() const { |
6138 | return getObjectType()->isObjCUnqualifiedIdOrClass(); |
6139 | } |
6140 | |
6141 | /// True if this is equivalent to 'id<P>' for some non-empty set of |
6142 | /// protocols. |
6143 | bool isObjCQualifiedIdType() const { |
6144 | return getObjectType()->isObjCQualifiedId(); |
6145 | } |
6146 | |
6147 | /// True if this is equivalent to 'Class<P>' for some non-empty set of |
6148 | /// protocols. |
6149 | bool isObjCQualifiedClassType() const { |
6150 | return getObjectType()->isObjCQualifiedClass(); |
6151 | } |
6152 | |
6153 | /// Whether this is a "__kindof" type. |
6154 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } |
6155 | |
6156 | /// Whether this type is specialized, meaning that it has type arguments. |
6157 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } |
6158 | |
6159 | /// Whether this type is specialized, meaning that it has type arguments. |
6160 | bool isSpecializedAsWritten() const { |
6161 | return getObjectType()->isSpecializedAsWritten(); |
6162 | } |
6163 | |
6164 | /// Whether this type is unspecialized, meaning that is has no type arguments. |
6165 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } |
6166 | |
6167 | /// Determine whether this object type is "unspecialized" as |
6168 | /// written, meaning that it has no type arguments. |
6169 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
6170 | |
6171 | /// Retrieve the type arguments for this type. |
6172 | ArrayRef<QualType> getTypeArgs() const { |
6173 | return getObjectType()->getTypeArgs(); |
6174 | } |
6175 | |
6176 | /// Retrieve the type arguments for this type. |
6177 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
6178 | return getObjectType()->getTypeArgsAsWritten(); |
6179 | } |
6180 | |
6181 | /// An iterator over the qualifiers on the object type. Provided |
6182 | /// for convenience. This will always iterate over the full set of |
6183 | /// protocols on a type, not just those provided directly. |
6184 | using qual_iterator = ObjCObjectType::qual_iterator; |
6185 | using qual_range = llvm::iterator_range<qual_iterator>; |
6186 | |
6187 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
6188 | |
6189 | qual_iterator qual_begin() const { |
6190 | return getObjectType()->qual_begin(); |
6191 | } |
6192 | |
6193 | qual_iterator qual_end() const { |
6194 | return getObjectType()->qual_end(); |
6195 | } |
6196 | |
6197 | bool qual_empty() const { return getObjectType()->qual_empty(); } |
6198 | |
6199 | /// Return the number of qualifying protocols on the object type. |
6200 | unsigned getNumProtocols() const { |
6201 | return getObjectType()->getNumProtocols(); |
6202 | } |
6203 | |
6204 | /// Retrieve a qualifying protocol by index on the object type. |
6205 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
6206 | return getObjectType()->getProtocol(I); |
6207 | } |
6208 | |
6209 | bool isSugared() const { return false; } |
6210 | QualType desugar() const { return QualType(this, 0); } |
6211 | |
6212 | /// Retrieve the type of the superclass of this object pointer type. |
6213 | /// |
6214 | /// This operation substitutes any type arguments into the |
6215 | /// superclass of the current class type, potentially producing a |
6216 | /// pointer to a specialization of the superclass type. Produces a |
6217 | /// null type if there is no superclass. |
6218 | QualType getSuperClassType() const; |
6219 | |
6220 | /// Strip off the Objective-C "kindof" type and (with it) any |
6221 | /// protocol qualifiers. |
6222 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( |
6223 | const ASTContext &ctx) const; |
6224 | |
6225 | void Profile(llvm::FoldingSetNodeID &ID) { |
6226 | Profile(ID, getPointeeType()); |
6227 | } |
6228 | |
6229 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6230 | ID.AddPointer(T.getAsOpaquePtr()); |
6231 | } |
6232 | |
6233 | static bool classof(const Type *T) { |
6234 | return T->getTypeClass() == ObjCObjectPointer; |
6235 | } |
6236 | }; |
6237 | |
6238 | class AtomicType : public Type, public llvm::FoldingSetNode { |
6239 | friend class ASTContext; // ASTContext creates these. |
6240 | |
6241 | QualType ValueType; |
6242 | |
6243 | AtomicType(QualType ValTy, QualType Canonical) |
6244 | : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} |
6245 | |
6246 | public: |
6247 | /// Gets the type contained by this atomic type, i.e. |
6248 | /// the type returned by performing an atomic load of this atomic type. |
6249 | QualType getValueType() const { return ValueType; } |
6250 | |
6251 | bool isSugared() const { return false; } |
6252 | QualType desugar() const { return QualType(this, 0); } |
6253 | |
6254 | void Profile(llvm::FoldingSetNodeID &ID) { |
6255 | Profile(ID, getValueType()); |
6256 | } |
6257 | |
6258 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6259 | ID.AddPointer(T.getAsOpaquePtr()); |
6260 | } |
6261 | |
6262 | static bool classof(const Type *T) { |
6263 | return T->getTypeClass() == Atomic; |
6264 | } |
6265 | }; |
6266 | |
6267 | /// PipeType - OpenCL20. |
6268 | class PipeType : public Type, public llvm::FoldingSetNode { |
6269 | friend class ASTContext; // ASTContext creates these. |
6270 | |
6271 | QualType ElementType; |
6272 | bool isRead; |
6273 | |
6274 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) |
6275 | : Type(Pipe, CanonicalPtr, elemType->getDependence()), |
6276 | ElementType(elemType), isRead(isRead) {} |
6277 | |
6278 | public: |
6279 | QualType getElementType() const { return ElementType; } |
6280 | |
6281 | bool isSugared() const { return false; } |
6282 | |
6283 | QualType desugar() const { return QualType(this, 0); } |
6284 | |
6285 | void Profile(llvm::FoldingSetNodeID &ID) { |
6286 | Profile(ID, getElementType(), isReadOnly()); |
6287 | } |
6288 | |
6289 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { |
6290 | ID.AddPointer(T.getAsOpaquePtr()); |
6291 | ID.AddBoolean(isRead); |
6292 | } |
6293 | |
6294 | static bool classof(const Type *T) { |
6295 | return T->getTypeClass() == Pipe; |
6296 | } |
6297 | |
6298 | bool isReadOnly() const { return isRead; } |
6299 | }; |
6300 | |
6301 | /// A fixed int type of a specified bitwidth. |
6302 | class ExtIntType final : public Type, public llvm::FoldingSetNode { |
6303 | friend class ASTContext; |
6304 | unsigned IsUnsigned : 1; |
6305 | unsigned NumBits : 24; |
6306 | |
6307 | protected: |
6308 | ExtIntType(bool isUnsigned, unsigned NumBits); |
6309 | |
6310 | public: |
6311 | bool isUnsigned() const { return IsUnsigned; } |
6312 | bool isSigned() const { return !IsUnsigned; } |
6313 | unsigned getNumBits() const { return NumBits; } |
6314 | |
6315 | bool isSugared() const { return false; } |
6316 | QualType desugar() const { return QualType(this, 0); } |
6317 | |
6318 | void Profile(llvm::FoldingSetNodeID &ID) { |
6319 | Profile(ID, isUnsigned(), getNumBits()); |
6320 | } |
6321 | |
6322 | static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, |
6323 | unsigned NumBits) { |
6324 | ID.AddBoolean(IsUnsigned); |
6325 | ID.AddInteger(NumBits); |
6326 | } |
6327 | |
6328 | static bool classof(const Type *T) { return T->getTypeClass() == ExtInt; } |
6329 | }; |
6330 | |
6331 | class DependentExtIntType final : public Type, public llvm::FoldingSetNode { |
6332 | friend class ASTContext; |
6333 | const ASTContext &Context; |
6334 | llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; |
6335 | |
6336 | protected: |
6337 | DependentExtIntType(const ASTContext &Context, bool IsUnsigned, |
6338 | Expr *NumBits); |
6339 | |
6340 | public: |
6341 | bool isUnsigned() const; |
6342 | bool isSigned() const { return !isUnsigned(); } |
6343 | Expr *getNumBitsExpr() const; |
6344 | |
6345 | bool isSugared() const { return false; } |
6346 | QualType desugar() const { return QualType(this, 0); } |
6347 | |
6348 | void Profile(llvm::FoldingSetNodeID &ID) { |
6349 | Profile(ID, Context, isUnsigned(), getNumBitsExpr()); |
6350 | } |
6351 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
6352 | bool IsUnsigned, Expr *NumBitsExpr); |
6353 | |
6354 | static bool classof(const Type *T) { |
6355 | return T->getTypeClass() == DependentExtInt; |
6356 | } |
6357 | }; |
6358 | |
6359 | /// A qualifier set is used to build a set of qualifiers. |
6360 | class QualifierCollector : public Qualifiers { |
6361 | public: |
6362 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} |
6363 | |
6364 | /// Collect any qualifiers on the given type and return an |
6365 | /// unqualified type. The qualifiers are assumed to be consistent |
6366 | /// with those already in the type. |
6367 | const Type *strip(QualType type) { |
6368 | addFastQualifiers(type.getLocalFastQualifiers()); |
6369 | if (!type.hasLocalNonFastQualifiers()) |
6370 | return type.getTypePtrUnsafe(); |
6371 | |
6372 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); |
6373 | addConsistentQualifiers(extQuals->getQualifiers()); |
6374 | return extQuals->getBaseType(); |
6375 | } |
6376 | |
6377 | /// Apply the collected qualifiers to the given type. |
6378 | QualType apply(const ASTContext &Context, QualType QT) const; |
6379 | |
6380 | /// Apply the collected qualifiers to the given type. |
6381 | QualType apply(const ASTContext &Context, const Type* T) const; |
6382 | }; |
6383 | |
6384 | /// A container of type source information. |
6385 | /// |
6386 | /// A client can read the relevant info using TypeLoc wrappers, e.g: |
6387 | /// @code |
6388 | /// TypeLoc TL = TypeSourceInfo->getTypeLoc(); |
6389 | /// TL.getBeginLoc().print(OS, SrcMgr); |
6390 | /// @endcode |
6391 | class alignas(8) TypeSourceInfo { |
6392 | // Contains a memory block after the class, used for type source information, |
6393 | // allocated by ASTContext. |
6394 | friend class ASTContext; |
6395 | |
6396 | QualType Ty; |
6397 | |
6398 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
6399 | |
6400 | public: |
6401 | /// Return the type wrapped by this type source info. |
6402 | QualType getType() const { return Ty; } |
6403 | |
6404 | /// Return the TypeLoc wrapper for the type source info. |
6405 | TypeLoc getTypeLoc() const; // implemented in TypeLoc.h |
6406 | |
6407 | /// Override the type stored in this TypeSourceInfo. Use with caution! |
6408 | void overrideType(QualType T) { Ty = T; } |
6409 | }; |
6410 | |
6411 | // Inline function definitions. |
6412 | |
6413 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { |
6414 | SplitQualType desugar = |
6415 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); |
6416 | desugar.Quals.addConsistentQualifiers(Quals); |
6417 | return desugar; |
6418 | } |
6419 | |
6420 | inline const Type *QualType::getTypePtr() const { |
6421 | return getCommonPtr()->BaseType; |
6422 | } |
6423 | |
6424 | inline const Type *QualType::getTypePtrOrNull() const { |
6425 | return (isNull() ? nullptr : getCommonPtr()->BaseType); |
6426 | } |
6427 | |
6428 | inline SplitQualType QualType::split() const { |
6429 | if (!hasLocalNonFastQualifiers()) |
6430 | return SplitQualType(getTypePtrUnsafe(), |
6431 | Qualifiers::fromFastMask(getLocalFastQualifiers())); |
6432 | |
6433 | const ExtQuals *eq = getExtQualsUnsafe(); |
6434 | Qualifiers qs = eq->getQualifiers(); |
6435 | qs.addFastQualifiers(getLocalFastQualifiers()); |
6436 | return SplitQualType(eq->getBaseType(), qs); |
6437 | } |
6438 | |
6439 | inline Qualifiers QualType::getLocalQualifiers() const { |
6440 | Qualifiers Quals; |
6441 | if (hasLocalNonFastQualifiers()) |
6442 | Quals = getExtQualsUnsafe()->getQualifiers(); |
6443 | Quals.addFastQualifiers(getLocalFastQualifiers()); |
6444 | return Quals; |
6445 | } |
6446 | |
6447 | inline Qualifiers QualType::getQualifiers() const { |
6448 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); |
6449 | quals.addFastQualifiers(getLocalFastQualifiers()); |
6450 | return quals; |
6451 | } |
6452 | |
6453 | inline unsigned QualType::getCVRQualifiers() const { |
6454 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); |
6455 | cvr |= getLocalCVRQualifiers(); |
6456 | return cvr; |
6457 | } |
6458 | |
6459 | inline QualType QualType::getCanonicalType() const { |
6460 | QualType canon = getCommonPtr()->CanonicalType; |
6461 | return canon.withFastQualifiers(getLocalFastQualifiers()); |
6462 | } |
6463 | |
6464 | inline bool QualType::isCanonical() const { |
6465 | return getTypePtr()->isCanonicalUnqualified(); |
6466 | } |
6467 | |
6468 | inline bool QualType::isCanonicalAsParam() const { |
6469 | if (!isCanonical()) return false; |
6470 | if (hasLocalQualifiers()) return false; |
6471 | |
6472 | const Type *T = getTypePtr(); |
6473 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) |
6474 | return false; |
6475 | |
6476 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); |
6477 | } |
6478 | |
6479 | inline bool QualType::isConstQualified() const { |
6480 | return isLocalConstQualified() || |
6481 | getCommonPtr()->CanonicalType.isLocalConstQualified(); |
6482 | } |
6483 | |
6484 | inline bool QualType::isRestrictQualified() const { |
6485 | return isLocalRestrictQualified() || |
6486 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); |
6487 | } |
6488 | |
6489 | |
6490 | inline bool QualType::isVolatileQualified() const { |
6491 | return isLocalVolatileQualified() || |
6492 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); |
6493 | } |
6494 | |
6495 | inline bool QualType::hasQualifiers() const { |
6496 | return hasLocalQualifiers() || |
6497 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); |
6498 | } |
6499 | |
6500 | inline QualType QualType::getUnqualifiedType() const { |
6501 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6502 | return QualType(getTypePtr(), 0); |
6503 | |
6504 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); |
6505 | } |
6506 | |
6507 | inline SplitQualType QualType::getSplitUnqualifiedType() const { |
6508 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6509 | return split(); |
6510 | |
6511 | return getSplitUnqualifiedTypeImpl(*this); |
6512 | } |
6513 | |
6514 | inline void QualType::removeLocalConst() { |
6515 | removeLocalFastQualifiers(Qualifiers::Const); |
6516 | } |
6517 | |
6518 | inline void QualType::removeLocalRestrict() { |
6519 | removeLocalFastQualifiers(Qualifiers::Restrict); |
6520 | } |
6521 | |
6522 | inline void QualType::removeLocalVolatile() { |
6523 | removeLocalFastQualifiers(Qualifiers::Volatile); |
6524 | } |
6525 | |
6526 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { |
6527 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits")((!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::CVRMask) && \"mask has non-CVR bits\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 6527, __PRETTY_FUNCTION__)); |
6528 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, |
6529 | "Fast bits differ from CVR bits!"); |
6530 | |
6531 | // Fast path: we don't need to touch the slow qualifiers. |
6532 | removeLocalFastQualifiers(Mask); |
6533 | } |
6534 | |
6535 | /// Check if this type has any address space qualifier. |
6536 | inline bool QualType::hasAddressSpace() const { |
6537 | return getQualifiers().hasAddressSpace(); |
6538 | } |
6539 | |
6540 | /// Return the address space of this type. |
6541 | inline LangAS QualType::getAddressSpace() const { |
6542 | return getQualifiers().getAddressSpace(); |
6543 | } |
6544 | |
6545 | /// Return the gc attribute of this type. |
6546 | inline Qualifiers::GC QualType::getObjCGCAttr() const { |
6547 | return getQualifiers().getObjCGCAttr(); |
6548 | } |
6549 | |
6550 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
6551 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6552 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); |
6553 | return false; |
6554 | } |
6555 | |
6556 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { |
6557 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6558 | return hasNonTrivialToPrimitiveDestructCUnion(RD); |
6559 | return false; |
6560 | } |
6561 | |
6562 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { |
6563 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6564 | return hasNonTrivialToPrimitiveCopyCUnion(RD); |
6565 | return false; |
6566 | } |
6567 | |
6568 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { |
6569 | if (const auto *PT = t.getAs<PointerType>()) { |
6570 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) |
6571 | return FT->getExtInfo(); |
6572 | } else if (const auto *FT = t.getAs<FunctionType>()) |
6573 | return FT->getExtInfo(); |
6574 | |
6575 | return FunctionType::ExtInfo(); |
6576 | } |
6577 | |
6578 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { |
6579 | return getFunctionExtInfo(*t); |
6580 | } |
6581 | |
6582 | /// Determine whether this type is more |
6583 | /// qualified than the Other type. For example, "const volatile int" |
6584 | /// is more qualified than "const int", "volatile int", and |
6585 | /// "int". However, it is not more qualified than "const volatile |
6586 | /// int". |
6587 | inline bool QualType::isMoreQualifiedThan(QualType other) const { |
6588 | Qualifiers MyQuals = getQualifiers(); |
6589 | Qualifiers OtherQuals = other.getQualifiers(); |
6590 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); |
6591 | } |
6592 | |
6593 | /// Determine whether this type is at last |
6594 | /// as qualified as the Other type. For example, "const volatile |
6595 | /// int" is at least as qualified as "const int", "volatile int", |
6596 | /// "int", and "const volatile int". |
6597 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { |
6598 | Qualifiers OtherQuals = other.getQualifiers(); |
6599 | |
6600 | // Ignore __unaligned qualifier if this type is a void. |
6601 | if (getUnqualifiedType()->isVoidType()) |
6602 | OtherQuals.removeUnaligned(); |
6603 | |
6604 | return getQualifiers().compatiblyIncludes(OtherQuals); |
6605 | } |
6606 | |
6607 | /// If Type is a reference type (e.g., const |
6608 | /// int&), returns the type that the reference refers to ("const |
6609 | /// int"). Otherwise, returns the type itself. This routine is used |
6610 | /// throughout Sema to implement C++ 5p6: |
6611 | /// |
6612 | /// If an expression initially has the type "reference to T" (8.3.2, |
6613 | /// 8.5.3), the type is adjusted to "T" prior to any further |
6614 | /// analysis, the expression designates the object or function |
6615 | /// denoted by the reference, and the expression is an lvalue. |
6616 | inline QualType QualType::getNonReferenceType() const { |
6617 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) |
6618 | return RefType->getPointeeType(); |
6619 | else |
6620 | return *this; |
6621 | } |
6622 | |
6623 | inline bool QualType::isCForbiddenLValueType() const { |
6624 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || |
6625 | getTypePtr()->isFunctionType()); |
6626 | } |
6627 | |
6628 | /// Tests whether the type is categorized as a fundamental type. |
6629 | /// |
6630 | /// \returns True for types specified in C++0x [basic.fundamental]. |
6631 | inline bool Type::isFundamentalType() const { |
6632 | return isVoidType() || |
6633 | isNullPtrType() || |
6634 | // FIXME: It's really annoying that we don't have an |
6635 | // 'isArithmeticType()' which agrees with the standard definition. |
6636 | (isArithmeticType() && !isEnumeralType()); |
6637 | } |
6638 | |
6639 | /// Tests whether the type is categorized as a compound type. |
6640 | /// |
6641 | /// \returns True for types specified in C++0x [basic.compound]. |
6642 | inline bool Type::isCompoundType() const { |
6643 | // C++0x [basic.compound]p1: |
6644 | // Compound types can be constructed in the following ways: |
6645 | // -- arrays of objects of a given type [...]; |
6646 | return isArrayType() || |
6647 | // -- functions, which have parameters of given types [...]; |
6648 | isFunctionType() || |
6649 | // -- pointers to void or objects or functions [...]; |
6650 | isPointerType() || |
6651 | // -- references to objects or functions of a given type. [...] |
6652 | isReferenceType() || |
6653 | // -- classes containing a sequence of objects of various types, [...]; |
6654 | isRecordType() || |
6655 | // -- unions, which are classes capable of containing objects of different |
6656 | // types at different times; |
6657 | isUnionType() || |
6658 | // -- enumerations, which comprise a set of named constant values. [...]; |
6659 | isEnumeralType() || |
6660 | // -- pointers to non-static class members, [...]. |
6661 | isMemberPointerType(); |
6662 | } |
6663 | |
6664 | inline bool Type::isFunctionType() const { |
6665 | return isa<FunctionType>(CanonicalType); |
6666 | } |
6667 | |
6668 | inline bool Type::isPointerType() const { |
6669 | return isa<PointerType>(CanonicalType); |
6670 | } |
6671 | |
6672 | inline bool Type::isAnyPointerType() const { |
6673 | return isPointerType() || isObjCObjectPointerType(); |
6674 | } |
6675 | |
6676 | inline bool Type::isBlockPointerType() const { |
6677 | return isa<BlockPointerType>(CanonicalType); |
6678 | } |
6679 | |
6680 | inline bool Type::isReferenceType() const { |
6681 | return isa<ReferenceType>(CanonicalType); |
6682 | } |
6683 | |
6684 | inline bool Type::isLValueReferenceType() const { |
6685 | return isa<LValueReferenceType>(CanonicalType); |
6686 | } |
6687 | |
6688 | inline bool Type::isRValueReferenceType() const { |
6689 | return isa<RValueReferenceType>(CanonicalType); |
6690 | } |
6691 | |
6692 | inline bool Type::isObjectPointerType() const { |
6693 | // Note: an "object pointer type" is not the same thing as a pointer to an |
6694 | // object type; rather, it is a pointer to an object type or a pointer to cv |
6695 | // void. |
6696 | if (const auto *T = getAs<PointerType>()) |
6697 | return !T->getPointeeType()->isFunctionType(); |
6698 | else |
6699 | return false; |
6700 | } |
6701 | |
6702 | inline bool Type::isFunctionPointerType() const { |
6703 | if (const auto *T = getAs<PointerType>()) |
6704 | return T->getPointeeType()->isFunctionType(); |
6705 | else |
6706 | return false; |
6707 | } |
6708 | |
6709 | inline bool Type::isFunctionReferenceType() const { |
6710 | if (const auto *T = getAs<ReferenceType>()) |
6711 | return T->getPointeeType()->isFunctionType(); |
6712 | else |
6713 | return false; |
6714 | } |
6715 | |
6716 | inline bool Type::isMemberPointerType() const { |
6717 | return isa<MemberPointerType>(CanonicalType); |
6718 | } |
6719 | |
6720 | inline bool Type::isMemberFunctionPointerType() const { |
6721 | if (const auto *T = getAs<MemberPointerType>()) |
6722 | return T->isMemberFunctionPointer(); |
6723 | else |
6724 | return false; |
6725 | } |
6726 | |
6727 | inline bool Type::isMemberDataPointerType() const { |
6728 | if (const auto *T = getAs<MemberPointerType>()) |
6729 | return T->isMemberDataPointer(); |
6730 | else |
6731 | return false; |
6732 | } |
6733 | |
6734 | inline bool Type::isArrayType() const { |
6735 | return isa<ArrayType>(CanonicalType); |
6736 | } |
6737 | |
6738 | inline bool Type::isConstantArrayType() const { |
6739 | return isa<ConstantArrayType>(CanonicalType); |
6740 | } |
6741 | |
6742 | inline bool Type::isIncompleteArrayType() const { |
6743 | return isa<IncompleteArrayType>(CanonicalType); |
6744 | } |
6745 | |
6746 | inline bool Type::isVariableArrayType() const { |
6747 | return isa<VariableArrayType>(CanonicalType); |
6748 | } |
6749 | |
6750 | inline bool Type::isDependentSizedArrayType() const { |
6751 | return isa<DependentSizedArrayType>(CanonicalType); |
6752 | } |
6753 | |
6754 | inline bool Type::isBuiltinType() const { |
6755 | return isa<BuiltinType>(CanonicalType); |
6756 | } |
6757 | |
6758 | inline bool Type::isRecordType() const { |
6759 | return isa<RecordType>(CanonicalType); |
6760 | } |
6761 | |
6762 | inline bool Type::isEnumeralType() const { |
6763 | return isa<EnumType>(CanonicalType); |
6764 | } |
6765 | |
6766 | inline bool Type::isAnyComplexType() const { |
6767 | return isa<ComplexType>(CanonicalType); |
6768 | } |
6769 | |
6770 | inline bool Type::isVectorType() const { |
6771 | return isa<VectorType>(CanonicalType); |
6772 | } |
6773 | |
6774 | inline bool Type::isExtVectorType() const { |
6775 | return isa<ExtVectorType>(CanonicalType); |
6776 | } |
6777 | |
6778 | inline bool Type::isMatrixType() const { |
6779 | return isa<MatrixType>(CanonicalType); |
6780 | } |
6781 | |
6782 | inline bool Type::isConstantMatrixType() const { |
6783 | return isa<ConstantMatrixType>(CanonicalType); |
6784 | } |
6785 | |
6786 | inline bool Type::isDependentAddressSpaceType() const { |
6787 | return isa<DependentAddressSpaceType>(CanonicalType); |
6788 | } |
6789 | |
6790 | inline bool Type::isObjCObjectPointerType() const { |
6791 | return isa<ObjCObjectPointerType>(CanonicalType); |
6792 | } |
6793 | |
6794 | inline bool Type::isObjCObjectType() const { |
6795 | return isa<ObjCObjectType>(CanonicalType); |
6796 | } |
6797 | |
6798 | inline bool Type::isObjCObjectOrInterfaceType() const { |
6799 | return isa<ObjCInterfaceType>(CanonicalType) || |
6800 | isa<ObjCObjectType>(CanonicalType); |
6801 | } |
6802 | |
6803 | inline bool Type::isAtomicType() const { |
6804 | return isa<AtomicType>(CanonicalType); |
6805 | } |
6806 | |
6807 | inline bool Type::isUndeducedAutoType() const { |
6808 | return isa<AutoType>(CanonicalType); |
6809 | } |
6810 | |
6811 | inline bool Type::isObjCQualifiedIdType() const { |
6812 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6813 | return OPT->isObjCQualifiedIdType(); |
6814 | return false; |
6815 | } |
6816 | |
6817 | inline bool Type::isObjCQualifiedClassType() const { |
6818 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6819 | return OPT->isObjCQualifiedClassType(); |
6820 | return false; |
6821 | } |
6822 | |
6823 | inline bool Type::isObjCIdType() const { |
6824 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6825 | return OPT->isObjCIdType(); |
6826 | return false; |
6827 | } |
6828 | |
6829 | inline bool Type::isObjCClassType() const { |
6830 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6831 | return OPT->isObjCClassType(); |
6832 | return false; |
6833 | } |
6834 | |
6835 | inline bool Type::isObjCSelType() const { |
6836 | if (const auto *OPT = getAs<PointerType>()) |
6837 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); |
6838 | return false; |
6839 | } |
6840 | |
6841 | inline bool Type::isObjCBuiltinType() const { |
6842 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); |
6843 | } |
6844 | |
6845 | inline bool Type::isDecltypeType() const { |
6846 | return isa<DecltypeType>(this); |
6847 | } |
6848 | |
6849 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
6850 | inline bool Type::is##Id##Type() const { \ |
6851 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6852 | } |
6853 | #include "clang/Basic/OpenCLImageTypes.def" |
6854 | |
6855 | inline bool Type::isSamplerT() const { |
6856 | return isSpecificBuiltinType(BuiltinType::OCLSampler); |
6857 | } |
6858 | |
6859 | inline bool Type::isEventT() const { |
6860 | return isSpecificBuiltinType(BuiltinType::OCLEvent); |
6861 | } |
6862 | |
6863 | inline bool Type::isClkEventT() const { |
6864 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); |
6865 | } |
6866 | |
6867 | inline bool Type::isQueueT() const { |
6868 | return isSpecificBuiltinType(BuiltinType::OCLQueue); |
6869 | } |
6870 | |
6871 | inline bool Type::isReserveIDT() const { |
6872 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); |
6873 | } |
6874 | |
6875 | inline bool Type::isImageType() const { |
6876 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || |
6877 | return |
6878 | #include "clang/Basic/OpenCLImageTypes.def" |
6879 | false; // end boolean or operation |
6880 | } |
6881 | |
6882 | inline bool Type::isPipeType() const { |
6883 | return isa<PipeType>(CanonicalType); |
6884 | } |
6885 | |
6886 | inline bool Type::isExtIntType() const { |
6887 | return isa<ExtIntType>(CanonicalType); |
6888 | } |
6889 | |
6890 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
6891 | inline bool Type::is##Id##Type() const { \ |
6892 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6893 | } |
6894 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6895 | |
6896 | inline bool Type::isOCLIntelSubgroupAVCType() const { |
6897 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ |
6898 | isOCLIntelSubgroupAVC##Id##Type() || |
6899 | return |
6900 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6901 | false; // end of boolean or operation |
6902 | } |
6903 | |
6904 | inline bool Type::isOCLExtOpaqueType() const { |
6905 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || |
6906 | return |
6907 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6908 | false; // end of boolean or operation |
6909 | } |
6910 | |
6911 | inline bool Type::isOpenCLSpecificType() const { |
6912 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || |
6913 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); |
6914 | } |
6915 | |
6916 | inline bool Type::isTemplateTypeParmType() const { |
6917 | return isa<TemplateTypeParmType>(CanonicalType); |
6918 | } |
6919 | |
6920 | inline bool Type::isSpecificBuiltinType(unsigned K) const { |
6921 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
6922 | return BT->getKind() == static_cast<BuiltinType::Kind>(K); |
6923 | } |
6924 | return false; |
6925 | } |
6926 | |
6927 | inline bool Type::isPlaceholderType() const { |
6928 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6929 | return BT->isPlaceholderType(); |
6930 | return false; |
6931 | } |
6932 | |
6933 | inline const BuiltinType *Type::getAsPlaceholderType() const { |
6934 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6935 | if (BT->isPlaceholderType()) |
6936 | return BT; |
6937 | return nullptr; |
6938 | } |
6939 | |
6940 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { |
6941 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K))((BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)) ? static_cast<void> (0) : __assert_fail ("BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 6941, __PRETTY_FUNCTION__)); |
6942 | return isSpecificBuiltinType(K); |
6943 | } |
6944 | |
6945 | inline bool Type::isNonOverloadPlaceholderType() const { |
6946 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6947 | return BT->isNonOverloadPlaceholderType(); |
6948 | return false; |
6949 | } |
6950 | |
6951 | inline bool Type::isVoidType() const { |
6952 | return isSpecificBuiltinType(BuiltinType::Void); |
6953 | } |
6954 | |
6955 | inline bool Type::isHalfType() const { |
6956 | // FIXME: Should we allow complex __fp16? Probably not. |
6957 | return isSpecificBuiltinType(BuiltinType::Half); |
6958 | } |
6959 | |
6960 | inline bool Type::isFloat16Type() const { |
6961 | return isSpecificBuiltinType(BuiltinType::Float16); |
6962 | } |
6963 | |
6964 | inline bool Type::isBFloat16Type() const { |
6965 | return isSpecificBuiltinType(BuiltinType::BFloat16); |
6966 | } |
6967 | |
6968 | inline bool Type::isFloat128Type() const { |
6969 | return isSpecificBuiltinType(BuiltinType::Float128); |
6970 | } |
6971 | |
6972 | inline bool Type::isNullPtrType() const { |
6973 | return isSpecificBuiltinType(BuiltinType::NullPtr); |
6974 | } |
6975 | |
6976 | bool IsEnumDeclComplete(EnumDecl *); |
6977 | bool IsEnumDeclScoped(EnumDecl *); |
6978 | |
6979 | inline bool Type::isIntegerType() const { |
6980 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6981 | return BT->getKind() >= BuiltinType::Bool && |
6982 | BT->getKind() <= BuiltinType::Int128; |
6983 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
6984 | // Incomplete enum types are not treated as integer types. |
6985 | // FIXME: In C++, enum types are never integer types. |
6986 | return IsEnumDeclComplete(ET->getDecl()) && |
6987 | !IsEnumDeclScoped(ET->getDecl()); |
6988 | } |
6989 | return isExtIntType(); |
6990 | } |
6991 | |
6992 | inline bool Type::isFixedPointType() const { |
6993 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6994 | return BT->getKind() >= BuiltinType::ShortAccum && |
6995 | BT->getKind() <= BuiltinType::SatULongFract; |
6996 | } |
6997 | return false; |
6998 | } |
6999 | |
7000 | inline bool Type::isFixedPointOrIntegerType() const { |
7001 | return isFixedPointType() || isIntegerType(); |
7002 | } |
7003 | |
7004 | inline bool Type::isSaturatedFixedPointType() const { |
7005 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7006 | return BT->getKind() >= BuiltinType::SatShortAccum && |
7007 | BT->getKind() <= BuiltinType::SatULongFract; |
7008 | } |
7009 | return false; |
7010 | } |
7011 | |
7012 | inline bool Type::isUnsaturatedFixedPointType() const { |
7013 | return isFixedPointType() && !isSaturatedFixedPointType(); |
7014 | } |
7015 | |
7016 | inline bool Type::isSignedFixedPointType() const { |
7017 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7018 | return ((BT->getKind() >= BuiltinType::ShortAccum && |
7019 | BT->getKind() <= BuiltinType::LongAccum) || |
7020 | (BT->getKind() >= BuiltinType::ShortFract && |
7021 | BT->getKind() <= BuiltinType::LongFract) || |
7022 | (BT->getKind() >= BuiltinType::SatShortAccum && |
7023 | BT->getKind() <= BuiltinType::SatLongAccum) || |
7024 | (BT->getKind() >= BuiltinType::SatShortFract && |
7025 | BT->getKind() <= BuiltinType::SatLongFract)); |
7026 | } |
7027 | return false; |
7028 | } |
7029 | |
7030 | inline bool Type::isUnsignedFixedPointType() const { |
7031 | return isFixedPointType() && !isSignedFixedPointType(); |
7032 | } |
7033 | |
7034 | inline bool Type::isScalarType() const { |
7035 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7036 | return BT->getKind() > BuiltinType::Void && |
7037 | BT->getKind() <= BuiltinType::NullPtr; |
7038 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
7039 | // Enums are scalar types, but only if they are defined. Incomplete enums |
7040 | // are not treated as scalar types. |
7041 | return IsEnumDeclComplete(ET->getDecl()); |
7042 | return isa<PointerType>(CanonicalType) || |
7043 | isa<BlockPointerType>(CanonicalType) || |
7044 | isa<MemberPointerType>(CanonicalType) || |
7045 | isa<ComplexType>(CanonicalType) || |
7046 | isa<ObjCObjectPointerType>(CanonicalType) || |
7047 | isExtIntType(); |
7048 | } |
7049 | |
7050 | inline bool Type::isIntegralOrEnumerationType() const { |
7051 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7052 | return BT->getKind() >= BuiltinType::Bool && |
7053 | BT->getKind() <= BuiltinType::Int128; |
7054 | |
7055 | // Check for a complete enum type; incomplete enum types are not properly an |
7056 | // enumeration type in the sense required here. |
7057 | if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) |
7058 | return IsEnumDeclComplete(ET->getDecl()); |
7059 | |
7060 | return isExtIntType(); |
7061 | } |
7062 | |
7063 | inline bool Type::isBooleanType() const { |
7064 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7065 | return BT->getKind() == BuiltinType::Bool; |
7066 | return false; |
7067 | } |
7068 | |
7069 | inline bool Type::isUndeducedType() const { |
7070 | auto *DT = getContainedDeducedType(); |
7071 | return DT && !DT->isDeduced(); |
7072 | } |
7073 | |
7074 | /// Determines whether this is a type for which one can define |
7075 | /// an overloaded operator. |
7076 | inline bool Type::isOverloadableType() const { |
7077 | return isDependentType() || isRecordType() || isEnumeralType(); |
7078 | } |
7079 | |
7080 | /// Determines whether this type is written as a typedef-name. |
7081 | inline bool Type::isTypedefNameType() const { |
7082 | if (getAs<TypedefType>()) |
7083 | return true; |
7084 | if (auto *TST = getAs<TemplateSpecializationType>()) |
7085 | return TST->isTypeAlias(); |
7086 | return false; |
7087 | } |
7088 | |
7089 | /// Determines whether this type can decay to a pointer type. |
7090 | inline bool Type::canDecayToPointerType() const { |
7091 | return isFunctionType() || isArrayType(); |
7092 | } |
7093 | |
7094 | inline bool Type::hasPointerRepresentation() const { |
7095 | return (isPointerType() || isReferenceType() || isBlockPointerType() || |
7096 | isObjCObjectPointerType() || isNullPtrType()); |
7097 | } |
7098 | |
7099 | inline bool Type::hasObjCPointerRepresentation() const { |
7100 | return isObjCObjectPointerType(); |
7101 | } |
7102 | |
7103 | inline const Type *Type::getBaseElementTypeUnsafe() const { |
7104 | const Type *type = this; |
7105 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) |
7106 | type = arrayType->getElementType().getTypePtr(); |
7107 | return type; |
7108 | } |
7109 | |
7110 | inline const Type *Type::getPointeeOrArrayElementType() const { |
7111 | const Type *type = this; |
7112 | if (type->isAnyPointerType()) |
7113 | return type->getPointeeType().getTypePtr(); |
7114 | else if (type->isArrayType()) |
7115 | return type->getBaseElementTypeUnsafe(); |
7116 | return type; |
7117 | } |
7118 | /// Insertion operator for partial diagnostics. This allows sending adress |
7119 | /// spaces into a diagnostic with <<. |
7120 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7121 | LangAS AS) { |
7122 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
7123 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
7124 | return PD; |
7125 | } |
7126 | |
7127 | /// Insertion operator for partial diagnostics. This allows sending Qualifiers |
7128 | /// into a diagnostic with <<. |
7129 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7130 | Qualifiers Q) { |
7131 | PD.AddTaggedVal(Q.getAsOpaqueValue(), |
7132 | DiagnosticsEngine::ArgumentKind::ak_qual); |
7133 | return PD; |
7134 | } |
7135 | |
7136 | /// Insertion operator for partial diagnostics. This allows sending QualType's |
7137 | /// into a diagnostic with <<. |
7138 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7139 | QualType T) { |
7140 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7141 | DiagnosticsEngine::ak_qualtype); |
7142 | return PD; |
7143 | } |
7144 | |
7145 | // Helper class template that is used by Type::getAs to ensure that one does |
7146 | // not try to look through a qualified type to get to an array type. |
7147 | template <typename T> |
7148 | using TypeIsArrayType = |
7149 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || |
7150 | std::is_base_of<ArrayType, T>::value>; |
7151 | |
7152 | // Member-template getAs<specific type>'. |
7153 | template <typename T> const T *Type::getAs() const { |
7154 | static_assert(!TypeIsArrayType<T>::value, |
7155 | "ArrayType cannot be used with getAs!"); |
7156 | |
7157 | // If this is directly a T type, return it. |
7158 | if (const auto *Ty = dyn_cast<T>(this)) |
7159 | return Ty; |
7160 | |
7161 | // If the canonical form of this type isn't the right kind, reject it. |
7162 | if (!isa<T>(CanonicalType)) |
7163 | return nullptr; |
7164 | |
7165 | // If this is a typedef for the type, strip the typedef off without |
7166 | // losing all typedef information. |
7167 | return cast<T>(getUnqualifiedDesugaredType()); |
7168 | } |
7169 | |
7170 | template <typename T> const T *Type::getAsAdjusted() const { |
7171 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); |
7172 | |
7173 | // If this is directly a T type, return it. |
7174 | if (const auto *Ty = dyn_cast<T>(this)) |
7175 | return Ty; |
7176 | |
7177 | // If the canonical form of this type isn't the right kind, reject it. |
7178 | if (!isa<T>(CanonicalType)) |
7179 | return nullptr; |
7180 | |
7181 | // Strip off type adjustments that do not modify the underlying nature of the |
7182 | // type. |
7183 | const Type *Ty = this; |
7184 | while (Ty) { |
7185 | if (const auto *A = dyn_cast<AttributedType>(Ty)) |
7186 | Ty = A->getModifiedType().getTypePtr(); |
7187 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) |
7188 | Ty = E->desugar().getTypePtr(); |
7189 | else if (const auto *P = dyn_cast<ParenType>(Ty)) |
7190 | Ty = P->desugar().getTypePtr(); |
7191 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) |
7192 | Ty = A->desugar().getTypePtr(); |
7193 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) |
7194 | Ty = M->desugar().getTypePtr(); |
7195 | else |
7196 | break; |
7197 | } |
7198 | |
7199 | // Just because the canonical type is correct does not mean we can use cast<>, |
7200 | // since we may not have stripped off all the sugar down to the base type. |
7201 | return dyn_cast<T>(Ty); |
7202 | } |
7203 | |
7204 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { |
7205 | // If this is directly an array type, return it. |
7206 | if (const auto *arr = dyn_cast<ArrayType>(this)) |
7207 | return arr; |
7208 | |
7209 | // If the canonical form of this type isn't the right kind, reject it. |
7210 | if (!isa<ArrayType>(CanonicalType)) |
7211 | return nullptr; |
7212 | |
7213 | // If this is a typedef for the type, strip the typedef off without |
7214 | // losing all typedef information. |
7215 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7216 | } |
7217 | |
7218 | template <typename T> const T *Type::castAs() const { |
7219 | static_assert(!TypeIsArrayType<T>::value, |
7220 | "ArrayType cannot be used with castAs!"); |
7221 | |
7222 | if (const auto *ty = dyn_cast<T>(this)) return ty; |
7223 | assert(isa<T>(CanonicalType))((isa<T>(CanonicalType)) ? static_cast<void> (0) : __assert_fail ("isa<T>(CanonicalType)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 7223, __PRETTY_FUNCTION__)); |
7224 | return cast<T>(getUnqualifiedDesugaredType()); |
7225 | } |
7226 | |
7227 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { |
7228 | assert(isa<ArrayType>(CanonicalType))((isa<ArrayType>(CanonicalType)) ? static_cast<void> (0) : __assert_fail ("isa<ArrayType>(CanonicalType)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 7228, __PRETTY_FUNCTION__)); |
7229 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; |
7230 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7231 | } |
7232 | |
7233 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, |
7234 | QualType CanonicalPtr) |
7235 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { |
7236 | #ifndef NDEBUG |
7237 | QualType Adjusted = getAdjustedType(); |
7238 | (void)AttributedType::stripOuterNullability(Adjusted); |
7239 | assert(isa<PointerType>(Adjusted))((isa<PointerType>(Adjusted)) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Adjusted)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Type.h" , 7239, __PRETTY_FUNCTION__)); |
7240 | #endif |
7241 | } |
7242 | |
7243 | QualType DecayedType::getPointeeType() const { |
7244 | QualType Decayed = getDecayedType(); |
7245 | (void)AttributedType::stripOuterNullability(Decayed); |
7246 | return cast<PointerType>(Decayed)->getPointeeType(); |
7247 | } |
7248 | |
7249 | // Get the decimal string representation of a fixed point type, represented |
7250 | // as a scaled integer. |
7251 | // TODO: At some point, we should change the arguments to instead just accept an |
7252 | // APFixedPoint instead of APSInt and scale. |
7253 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, |
7254 | unsigned Scale); |
7255 | |
7256 | } // namespace clang |
7257 | |
7258 | #endif // LLVM_CLANG_AST_TYPE_H |
1 | //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the PointerUnion class, which is a discriminated union of |
10 | // pointer types. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_ADT_POINTERUNION_H |
15 | #define LLVM_ADT_POINTERUNION_H |
16 | |
17 | #include "llvm/ADT/DenseMapInfo.h" |
18 | #include "llvm/ADT/PointerIntPair.h" |
19 | #include "llvm/Support/PointerLikeTypeTraits.h" |
20 | #include <cassert> |
21 | #include <cstddef> |
22 | #include <cstdint> |
23 | |
24 | namespace llvm { |
25 | |
26 | template <typename T> struct PointerUnionTypeSelectorReturn { |
27 | using Return = T; |
28 | }; |
29 | |
30 | /// Get a type based on whether two types are the same or not. |
31 | /// |
32 | /// For: |
33 | /// |
34 | /// \code |
35 | /// using Ret = typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return; |
36 | /// \endcode |
37 | /// |
38 | /// Ret will be EQ type if T1 is same as T2 or NE type otherwise. |
39 | template <typename T1, typename T2, typename RET_EQ, typename RET_NE> |
40 | struct PointerUnionTypeSelector { |
41 | using Return = typename PointerUnionTypeSelectorReturn<RET_NE>::Return; |
42 | }; |
43 | |
44 | template <typename T, typename RET_EQ, typename RET_NE> |
45 | struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> { |
46 | using Return = typename PointerUnionTypeSelectorReturn<RET_EQ>::Return; |
47 | }; |
48 | |
49 | template <typename T1, typename T2, typename RET_EQ, typename RET_NE> |
50 | struct PointerUnionTypeSelectorReturn< |
51 | PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>> { |
52 | using Return = |
53 | typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return; |
54 | }; |
55 | |
56 | namespace pointer_union_detail { |
57 | /// Determine the number of bits required to store integers with values < n. |
58 | /// This is ceil(log2(n)). |
59 | constexpr int bitsRequired(unsigned n) { |
60 | return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0; |
61 | } |
62 | |
63 | template <typename... Ts> constexpr int lowBitsAvailable() { |
64 | return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...}); |
65 | } |
66 | |
67 | /// Find the index of a type in a list of types. TypeIndex<T, Us...>::Index |
68 | /// is the index of T in Us, or sizeof...(Us) if T does not appear in the |
69 | /// list. |
70 | template <typename T, typename ...Us> struct TypeIndex; |
71 | template <typename T, typename ...Us> struct TypeIndex<T, T, Us...> { |
72 | static constexpr int Index = 0; |
73 | }; |
74 | template <typename T, typename U, typename... Us> |
75 | struct TypeIndex<T, U, Us...> { |
76 | static constexpr int Index = 1 + TypeIndex<T, Us...>::Index; |
77 | }; |
78 | template <typename T> struct TypeIndex<T> { |
79 | static constexpr int Index = 0; |
80 | }; |
81 | |
82 | /// Find the first type in a list of types. |
83 | template <typename T, typename...> struct GetFirstType { |
84 | using type = T; |
85 | }; |
86 | |
87 | /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion |
88 | /// for the template arguments. |
89 | template <typename ...PTs> class PointerUnionUIntTraits { |
90 | public: |
91 | static inline void *getAsVoidPointer(void *P) { return P; } |
92 | static inline void *getFromVoidPointer(void *P) { return P; } |
93 | static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>(); |
94 | }; |
95 | |
96 | template <typename Derived, typename ValTy, int I, typename ...Types> |
97 | class PointerUnionMembers; |
98 | |
99 | template <typename Derived, typename ValTy, int I> |
100 | class PointerUnionMembers<Derived, ValTy, I> { |
101 | protected: |
102 | ValTy Val; |
103 | PointerUnionMembers() = default; |
104 | PointerUnionMembers(ValTy Val) : Val(Val) {} |
105 | |
106 | friend struct PointerLikeTypeTraits<Derived>; |
107 | }; |
108 | |
109 | template <typename Derived, typename ValTy, int I, typename Type, |
110 | typename ...Types> |
111 | class PointerUnionMembers<Derived, ValTy, I, Type, Types...> |
112 | : public PointerUnionMembers<Derived, ValTy, I + 1, Types...> { |
113 | using Base = PointerUnionMembers<Derived, ValTy, I + 1, Types...>; |
114 | public: |
115 | using Base::Base; |
116 | PointerUnionMembers() = default; |
117 | PointerUnionMembers(Type V) |
118 | : Base(ValTy(const_cast<void *>( |
119 | PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
120 | I)) {} |
121 | |
122 | using Base::operator=; |
123 | Derived &operator=(Type V) { |
124 | this->Val = ValTy( |
125 | const_cast<void *>(PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
126 | I); |
127 | return static_cast<Derived &>(*this); |
128 | }; |
129 | }; |
130 | } |
131 | |
132 | /// A discriminated union of two or more pointer types, with the discriminator |
133 | /// in the low bit of the pointer. |
134 | /// |
135 | /// This implementation is extremely efficient in space due to leveraging the |
136 | /// low bits of the pointer, while exposing a natural and type-safe API. |
137 | /// |
138 | /// Common use patterns would be something like this: |
139 | /// PointerUnion<int*, float*> P; |
140 | /// P = (int*)0; |
141 | /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0" |
142 | /// X = P.get<int*>(); // ok. |
143 | /// Y = P.get<float*>(); // runtime assertion failure. |
144 | /// Z = P.get<double*>(); // compile time failure. |
145 | /// P = (float*)0; |
146 | /// Y = P.get<float*>(); // ok. |
147 | /// X = P.get<int*>(); // runtime assertion failure. |
148 | template <typename... PTs> |
149 | class PointerUnion |
150 | : public pointer_union_detail::PointerUnionMembers< |
151 | PointerUnion<PTs...>, |
152 | PointerIntPair< |
153 | void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int, |
154 | pointer_union_detail::PointerUnionUIntTraits<PTs...>>, |
155 | 0, PTs...> { |
156 | // The first type is special because we want to directly cast a pointer to a |
157 | // default-initialized union to a pointer to the first type. But we don't |
158 | // want PointerUnion to be a 'template <typename First, typename ...Rest>' |
159 | // because it's much more convenient to have a name for the whole pack. So |
160 | // split off the first type here. |
161 | using First = typename pointer_union_detail::GetFirstType<PTs...>::type; |
162 | using Base = typename PointerUnion::PointerUnionMembers; |
163 | |
164 | public: |
165 | PointerUnion() = default; |
166 | |
167 | PointerUnion(std::nullptr_t) : PointerUnion() {} |
168 | using Base::Base; |
169 | |
170 | /// Test if the pointer held in the union is null, regardless of |
171 | /// which type it is. |
172 | bool isNull() const { return !this->Val.getPointer(); } |
173 | |
174 | explicit operator bool() const { return !isNull(); } |
175 | |
176 | /// Test if the Union currently holds the type matching T. |
177 | template <typename T> bool is() const { |
178 | constexpr int Index = pointer_union_detail::TypeIndex<T, PTs...>::Index; |
179 | static_assert(Index < sizeof...(PTs), |
180 | "PointerUnion::is<T> given type not in the union"); |
181 | return this->Val.getInt() == Index; |
182 | } |
183 | |
184 | /// Returns the value of the specified pointer type. |
185 | /// |
186 | /// If the specified pointer type is incorrect, assert. |
187 | template <typename T> T get() const { |
188 | assert(is<T>() && "Invalid accessor called")((is<T>() && "Invalid accessor called") ? static_cast <void> (0) : __assert_fail ("is<T>() && \"Invalid accessor called\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 188, __PRETTY_FUNCTION__)); |
189 | return PointerLikeTypeTraits<T>::getFromVoidPointer(this->Val.getPointer()); |
190 | } |
191 | |
192 | /// Returns the current pointer if it is of the specified pointer type, |
193 | /// otherwise returns null. |
194 | template <typename T> T dyn_cast() const { |
195 | if (is<T>()) |
196 | return get<T>(); |
197 | return T(); |
198 | } |
199 | |
200 | /// If the union is set to the first pointer type get an address pointing to |
201 | /// it. |
202 | First const *getAddrOfPtr1() const { |
203 | return const_cast<PointerUnion *>(this)->getAddrOfPtr1(); |
204 | } |
205 | |
206 | /// If the union is set to the first pointer type get an address pointing to |
207 | /// it. |
208 | First *getAddrOfPtr1() { |
209 | assert(is<First>() && "Val is not the first pointer")((is<First>() && "Val is not the first pointer" ) ? static_cast<void> (0) : __assert_fail ("is<First>() && \"Val is not the first pointer\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 209, __PRETTY_FUNCTION__)); |
210 | assert(((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
211 | PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
212 | this->Val.getPointer() &&((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
213 | "Can't get the address because PointerLikeTypeTraits changes the ptr")((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)); |
214 | return const_cast<First *>( |
215 | reinterpret_cast<const First *>(this->Val.getAddrOfPointer())); |
216 | } |
217 | |
218 | /// Assignment from nullptr which just clears the union. |
219 | const PointerUnion &operator=(std::nullptr_t) { |
220 | this->Val.initWithPointer(nullptr); |
221 | return *this; |
222 | } |
223 | |
224 | /// Assignment from elements of the union. |
225 | using Base::operator=; |
226 | |
227 | void *getOpaqueValue() const { return this->Val.getOpaqueValue(); } |
228 | static inline PointerUnion getFromOpaqueValue(void *VP) { |
229 | PointerUnion V; |
230 | V.Val = decltype(V.Val)::getFromOpaqueValue(VP); |
231 | return V; |
232 | } |
233 | }; |
234 | |
235 | template <typename ...PTs> |
236 | bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
237 | return lhs.getOpaqueValue() == rhs.getOpaqueValue(); |
238 | } |
239 | |
240 | template <typename ...PTs> |
241 | bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
242 | return lhs.getOpaqueValue() != rhs.getOpaqueValue(); |
243 | } |
244 | |
245 | template <typename ...PTs> |
246 | bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
247 | return lhs.getOpaqueValue() < rhs.getOpaqueValue(); |
248 | } |
249 | |
250 | // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has |
251 | // # low bits available = min(PT1bits,PT2bits)-1. |
252 | template <typename ...PTs> |
253 | struct PointerLikeTypeTraits<PointerUnion<PTs...>> { |
254 | static inline void *getAsVoidPointer(const PointerUnion<PTs...> &P) { |
255 | return P.getOpaqueValue(); |
256 | } |
257 | |
258 | static inline PointerUnion<PTs...> getFromVoidPointer(void *P) { |
259 | return PointerUnion<PTs...>::getFromOpaqueValue(P); |
260 | } |
261 | |
262 | // The number of bits available are the min of the pointer types minus the |
263 | // bits needed for the discriminator. |
264 | static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<decltype( |
265 | PointerUnion<PTs...>::Val)>::NumLowBitsAvailable; |
266 | }; |
267 | |
268 | // Teach DenseMap how to use PointerUnions as keys. |
269 | template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> { |
270 | using Union = PointerUnion<PTs...>; |
271 | using FirstInfo = |
272 | DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>; |
273 | |
274 | static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); } |
275 | |
276 | static inline Union getTombstoneKey() { |
277 | return Union(FirstInfo::getTombstoneKey()); |
278 | } |
279 | |
280 | static unsigned getHashValue(const Union &UnionVal) { |
281 | intptr_t key = (intptr_t)UnionVal.getOpaqueValue(); |
282 | return DenseMapInfo<intptr_t>::getHashValue(key); |
283 | } |
284 | |
285 | static bool isEqual(const Union &LHS, const Union &RHS) { |
286 | return LHS == RHS; |
287 | } |
288 | }; |
289 | |
290 | } // end namespace llvm |
291 | |
292 | #endif // LLVM_ADT_POINTERUNION_H |