/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaType.cpp
Line | Count | Source (jump to first uncovered line) |
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/Specifiers.h" |
26 | | #include "clang/Basic/TargetInfo.h" |
27 | | #include "clang/Lex/Preprocessor.h" |
28 | | #include "clang/Sema/DeclSpec.h" |
29 | | #include "clang/Sema/DelayedDiagnostic.h" |
30 | | #include "clang/Sema/Lookup.h" |
31 | | #include "clang/Sema/ParsedTemplate.h" |
32 | | #include "clang/Sema/ScopeInfo.h" |
33 | | #include "clang/Sema/SemaInternal.h" |
34 | | #include "clang/Sema/Template.h" |
35 | | #include "clang/Sema/TemplateInstCallback.h" |
36 | | #include "llvm/ADT/SmallPtrSet.h" |
37 | | #include "llvm/ADT/SmallString.h" |
38 | | #include "llvm/ADT/StringSwitch.h" |
39 | | #include "llvm/IR/DerivedTypes.h" |
40 | | #include "llvm/Support/ErrorHandling.h" |
41 | | #include <bitset> |
42 | | |
43 | | using namespace clang; |
44 | | |
45 | | enum TypeDiagSelector { |
46 | | TDS_Function, |
47 | | TDS_Pointer, |
48 | | TDS_ObjCObjOrBlock |
49 | | }; |
50 | | |
51 | | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
52 | | /// return type because this is a omitted return type on a block literal. |
53 | 3.34k | static bool isOmittedBlockReturnType(const Declarator &D) { |
54 | 3.34k | if (D.getContext() != DeclaratorContext::BlockLiteral || |
55 | 3.34k | D.getDeclSpec().hasTypeSpecifier()3.00k ) |
56 | 343 | return false; |
57 | | |
58 | 3.00k | if (D.getNumTypeObjects() == 0) |
59 | 5 | return true; // ^{ ... } |
60 | | |
61 | 3.00k | if (D.getNumTypeObjects() == 1 && |
62 | 3.00k | D.getTypeObject(0).Kind == DeclaratorChunk::Function2.99k ) |
63 | 2.99k | return true; // ^(int X, float Y) { ... } |
64 | | |
65 | 1 | return false; |
66 | 3.00k | } |
67 | | |
68 | | /// diagnoseBadTypeAttribute - Diagnoses a type attribute which |
69 | | /// doesn't apply to the given type. |
70 | | static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr, |
71 | 34 | QualType type) { |
72 | 34 | TypeDiagSelector WhichType; |
73 | 34 | bool useExpansionLoc = true; |
74 | 34 | switch (attr.getKind()) { |
75 | 1 | case ParsedAttr::AT_ObjCGC: |
76 | 1 | WhichType = TDS_Pointer; |
77 | 1 | break; |
78 | 6 | case ParsedAttr::AT_ObjCOwnership: |
79 | 6 | WhichType = TDS_ObjCObjOrBlock; |
80 | 6 | break; |
81 | 27 | default: |
82 | | // Assume everything else was a function attribute. |
83 | 27 | WhichType = TDS_Function; |
84 | 27 | useExpansionLoc = false; |
85 | 27 | break; |
86 | 34 | } |
87 | | |
88 | 34 | SourceLocation loc = attr.getLoc(); |
89 | 34 | StringRef name = attr.getAttrName()->getName(); |
90 | | |
91 | | // The GC attributes are usually written with macros; special-case them. |
92 | 34 | IdentifierInfo *II = attr.isArgIdent(0) ? attr.getArgAsIdent(0)->Ident7 |
93 | 34 | : nullptr27 ; |
94 | 34 | if (useExpansionLoc && loc.isMacroID()7 && II5 ) { |
95 | 5 | if (II->isStr("strong")) { |
96 | 2 | if (S.findMacroSpelling(loc, "__strong")) name = "__strong"; |
97 | 3 | } else if (II->isStr("weak")) { |
98 | 3 | if (S.findMacroSpelling(loc, "__weak")) name = "__weak"2 ; |
99 | 3 | } |
100 | 5 | } |
101 | | |
102 | 34 | S.Diag(loc, diag::warn_type_attribute_wrong_type) << name << WhichType |
103 | 34 | << type; |
104 | 34 | } |
105 | | |
106 | | // objc_gc applies to Objective-C pointers or, otherwise, to the |
107 | | // smallest available pointer type (i.e. 'void*' in 'void**'). |
108 | | #define OBJC_POINTER_TYPE_ATTRS_CASELIST \ |
109 | 292 | case ParsedAttr::AT_ObjCGC: \ |
110 | 8.19k | case ParsedAttr::AT_ObjCOwnership |
111 | | |
112 | | // Calling convention attributes. |
113 | | #define CALLING_CONV_ATTRS_CASELIST \ |
114 | 106k | case ParsedAttr::AT_CDecl: \ |
115 | 107k | case ParsedAttr::AT_FastCall: \ |
116 | 108k | case ParsedAttr::AT_StdCall: \ |
117 | 108k | case ParsedAttr::AT_ThisCall: \ |
118 | 108k | case ParsedAttr::AT_RegCall: \ |
119 | 108k | case ParsedAttr::AT_Pascal: \ |
120 | 112k | case ParsedAttr::AT_SwiftCall: \ |
121 | 112k | case ParsedAttr::AT_SwiftAsyncCall: \ |
122 | 113k | case ParsedAttr::AT_VectorCall: \ |
123 | 113k | case ParsedAttr::AT_AArch64VectorPcs: \ |
124 | 113k | case ParsedAttr::AT_AArch64SVEPcs: \ |
125 | 113k | case ParsedAttr::AT_AMDGPUKernelCall: \ |
126 | 113k | case ParsedAttr::AT_MSABI: \ |
127 | 113k | case ParsedAttr::AT_SysVABI: \ |
128 | 113k | case ParsedAttr::AT_Pcs: \ |
129 | 113k | case ParsedAttr::AT_IntelOclBicc: \ |
130 | 113k | case ParsedAttr::AT_PreserveMost: \ |
131 | 114k | case ParsedAttr::AT_PreserveAll |
132 | | |
133 | | // Function type attributes. |
134 | | #define FUNCTION_TYPE_ATTRS_CASELIST \ |
135 | 91.4k | case ParsedAttr::AT_NSReturnsRetained: \ |
136 | 105k | case ParsedAttr::AT_NoReturn: \ |
137 | 105k | case ParsedAttr::AT_Regparm: \ |
138 | 105k | case ParsedAttr::AT_CmseNSCall: \ |
139 | 105k | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \ |
140 | 105k | case ParsedAttr::AT_AnyX86NoCfCheck: \ |
141 | 106k | CALLING_CONV_ATTRS_CASELIST |
142 | | |
143 | | // Microsoft-specific type qualifiers. |
144 | | #define MS_TYPE_ATTRS_CASELIST \ |
145 | 52 | case ParsedAttr::AT_Ptr32: \ |
146 | 82 | case ParsedAttr::AT_Ptr64: \ |
147 | 106 | case ParsedAttr::AT_SPtr: \ |
148 | 124 | case ParsedAttr::AT_UPtr |
149 | | |
150 | | // Nullability qualifiers. |
151 | | #define NULLABILITY_TYPE_ATTRS_CASELIST \ |
152 | 1.04M | case ParsedAttr::AT_TypeNonNull: \ |
153 | 1.98M | case ParsedAttr::AT_TypeNullable: \ |
154 | 1.98M | case ParsedAttr::AT_TypeNullableResult: \ |
155 | 2.02M | case ParsedAttr::AT_TypeNullUnspecified |
156 | | |
157 | | namespace { |
158 | | /// An object which stores processing state for the entire |
159 | | /// GetTypeForDeclarator process. |
160 | | class TypeProcessingState { |
161 | | Sema &sema; |
162 | | |
163 | | /// The declarator being processed. |
164 | | Declarator &declarator; |
165 | | |
166 | | /// The index of the declarator chunk we're currently processing. |
167 | | /// May be the total number of valid chunks, indicating the |
168 | | /// DeclSpec. |
169 | | unsigned chunkIndex; |
170 | | |
171 | | /// The original set of attributes on the DeclSpec. |
172 | | SmallVector<ParsedAttr *, 2> savedAttrs; |
173 | | |
174 | | /// A list of attributes to diagnose the uselessness of when the |
175 | | /// processing is complete. |
176 | | SmallVector<ParsedAttr *, 2> ignoredTypeAttrs; |
177 | | |
178 | | /// Attributes corresponding to AttributedTypeLocs that we have not yet |
179 | | /// populated. |
180 | | // FIXME: The two-phase mechanism by which we construct Types and fill |
181 | | // their TypeLocs makes it hard to correctly assign these. We keep the |
182 | | // attributes in creation order as an attempt to make them line up |
183 | | // properly. |
184 | | using TypeAttrPair = std::pair<const AttributedType*, const Attr*>; |
185 | | SmallVector<TypeAttrPair, 8> AttrsForTypes; |
186 | | bool AttrsForTypesSorted = true; |
187 | | |
188 | | /// MacroQualifiedTypes mapping to macro expansion locations that will be |
189 | | /// stored in a MacroQualifiedTypeLoc. |
190 | | llvm::DenseMap<const MacroQualifiedType *, SourceLocation> LocsForMacros; |
191 | | |
192 | | /// Flag to indicate we parsed a noderef attribute. This is used for |
193 | | /// validating that noderef was used on a pointer or array. |
194 | | bool parsedNoDeref; |
195 | | |
196 | | public: |
197 | | TypeProcessingState(Sema &sema, Declarator &declarator) |
198 | | : sema(sema), declarator(declarator), |
199 | 111M | chunkIndex(declarator.getNumTypeObjects()), parsedNoDeref(false) {} |
200 | | |
201 | 475M | Sema &getSema() const { |
202 | 475M | return sema; |
203 | 475M | } |
204 | | |
205 | 826M | Declarator &getDeclarator() const { |
206 | 826M | return declarator; |
207 | 826M | } |
208 | | |
209 | 112M | bool isProcessingDeclSpec() const { |
210 | 112M | return chunkIndex == declarator.getNumTypeObjects(); |
211 | 112M | } |
212 | | |
213 | 1.54M | unsigned getCurrentChunkIndex() const { |
214 | 1.54M | return chunkIndex; |
215 | 1.54M | } |
216 | | |
217 | 35.5M | void setCurrentChunkIndex(unsigned idx) { |
218 | 35.5M | assert(idx <= declarator.getNumTypeObjects()); |
219 | 0 | chunkIndex = idx; |
220 | 35.5M | } |
221 | | |
222 | 112M | ParsedAttributesView &getCurrentAttributes() const { |
223 | 112M | if (isProcessingDeclSpec()) |
224 | 112M | return getMutableDeclSpec().getAttributes(); |
225 | 30 | return declarator.getTypeObject(chunkIndex).getAttrs(); |
226 | 112M | } |
227 | | |
228 | | /// Save the current set of attributes on the DeclSpec. |
229 | 41.2k | void saveDeclSpecAttrs() { |
230 | | // Don't try to save them multiple times. |
231 | 41.2k | if (!savedAttrs.empty()) |
232 | 668 | return; |
233 | | |
234 | 40.6k | DeclSpec &spec = getMutableDeclSpec(); |
235 | 40.6k | llvm::append_range(savedAttrs, |
236 | 40.6k | llvm::make_pointer_range(spec.getAttributes())); |
237 | 40.6k | } |
238 | | |
239 | | /// Record that we had nowhere to put the given type attribute. |
240 | | /// We will diagnose such attributes later. |
241 | 26 | void addIgnoredTypeAttr(ParsedAttr &attr) { |
242 | 26 | ignoredTypeAttrs.push_back(&attr); |
243 | 26 | } |
244 | | |
245 | | /// Diagnose all the ignored type attributes, given that the |
246 | | /// declarator worked out to the given type. |
247 | 111M | void diagnoseIgnoredTypeAttrs(QualType type) const { |
248 | 111M | for (auto *Attr : ignoredTypeAttrs) |
249 | 26 | diagnoseBadTypeAttribute(getSema(), *Attr, type); |
250 | 111M | } |
251 | | |
252 | | /// Get an attributed type for the given attribute, and remember the Attr |
253 | | /// object so that we can attach it to the AttributedTypeLoc. |
254 | | QualType getAttributedType(Attr *A, QualType ModifiedType, |
255 | 3.25M | QualType EquivType) { |
256 | 3.25M | QualType T = |
257 | 3.25M | sema.Context.getAttributedType(A->getKind(), ModifiedType, EquivType); |
258 | 3.25M | AttrsForTypes.push_back({cast<AttributedType>(T.getTypePtr()), A}); |
259 | 3.25M | AttrsForTypesSorted = false; |
260 | 3.25M | return T; |
261 | 3.25M | } |
262 | | |
263 | | /// Get a BTFTagAttributed type for the btf_type_tag attribute. |
264 | | QualType getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr, |
265 | 62 | QualType WrappedType) { |
266 | 62 | return sema.Context.getBTFTagAttributedType(BTFAttr, WrappedType); |
267 | 62 | } |
268 | | |
269 | | /// Completely replace the \c auto in \p TypeWithAuto by |
270 | | /// \p Replacement. Also replace \p TypeWithAuto in \c TypeAttrPair if |
271 | | /// necessary. |
272 | 2.63k | QualType ReplaceAutoType(QualType TypeWithAuto, QualType Replacement) { |
273 | 2.63k | QualType T = sema.ReplaceAutoType(TypeWithAuto, Replacement); |
274 | 2.63k | if (auto *AttrTy = TypeWithAuto->getAs<AttributedType>()) { |
275 | | // Attributed type still should be an attributed type after replacement. |
276 | 1 | auto *NewAttrTy = cast<AttributedType>(T.getTypePtr()); |
277 | 1 | for (TypeAttrPair &A : AttrsForTypes) { |
278 | 1 | if (A.first == AttrTy) |
279 | 1 | A.first = NewAttrTy; |
280 | 1 | } |
281 | 1 | AttrsForTypesSorted = false; |
282 | 1 | } |
283 | 2.63k | return T; |
284 | 2.63k | } |
285 | | |
286 | | /// Extract and remove the Attr* for a given attributed type. |
287 | 3.25M | const Attr *takeAttrForAttributedType(const AttributedType *AT) { |
288 | 3.25M | if (!AttrsForTypesSorted) { |
289 | 3.10M | llvm::stable_sort(AttrsForTypes, llvm::less_first()); |
290 | 3.10M | AttrsForTypesSorted = true; |
291 | 3.10M | } |
292 | | |
293 | | // FIXME: This is quadratic if we have lots of reuses of the same |
294 | | // attributed type. |
295 | 3.25M | for (auto It = std::partition_point( |
296 | 3.25M | AttrsForTypes.begin(), AttrsForTypes.end(), |
297 | 3.53M | [=](const TypeAttrPair &A) { return A.first < AT; }); |
298 | 3.25M | It != AttrsForTypes.end() && It->first == AT; ++It0 ) { |
299 | 3.25M | if (It->second) { |
300 | 3.25M | const Attr *Result = It->second; |
301 | 3.25M | It->second = nullptr; |
302 | 3.25M | return Result; |
303 | 3.25M | } |
304 | 3.25M | } |
305 | | |
306 | 0 | llvm_unreachable("no Attr* for AttributedType*"); |
307 | 0 | } |
308 | | |
309 | | SourceLocation |
310 | 241k | getExpansionLocForMacroQualifiedType(const MacroQualifiedType *MQT) const { |
311 | 241k | auto FoundLoc = LocsForMacros.find(MQT); |
312 | 241k | assert(FoundLoc != LocsForMacros.end() && |
313 | 241k | "Unable to find macro expansion location for MacroQualifedType"); |
314 | 0 | return FoundLoc->second; |
315 | 241k | } |
316 | | |
317 | | void setExpansionLocForMacroQualifiedType(const MacroQualifiedType *MQT, |
318 | 242k | SourceLocation Loc) { |
319 | 242k | LocsForMacros[MQT] = Loc; |
320 | 242k | } |
321 | | |
322 | 483M | void setParsedNoDeref(bool parsed) { parsedNoDeref = parsed; } |
323 | | |
324 | 35.1M | bool didParseNoDeref() const { return parsedNoDeref; } |
325 | | |
326 | 111M | ~TypeProcessingState() { |
327 | 111M | if (savedAttrs.empty()) |
328 | 111M | return; |
329 | | |
330 | 40.5k | getMutableDeclSpec().getAttributes().clearListOnly(); |
331 | 40.5k | for (ParsedAttr *AL : savedAttrs) |
332 | 158k | getMutableDeclSpec().getAttributes().addAtEnd(AL); |
333 | 40.5k | } |
334 | | |
335 | | private: |
336 | 112M | DeclSpec &getMutableDeclSpec() const { |
337 | 112M | return const_cast<DeclSpec&>(declarator.getDeclSpec()); |
338 | 112M | } |
339 | | }; |
340 | | } // end anonymous namespace |
341 | | |
342 | | static void moveAttrFromListToList(ParsedAttr &attr, |
343 | | ParsedAttributesView &fromList, |
344 | 49.7k | ParsedAttributesView &toList) { |
345 | 49.7k | fromList.remove(&attr); |
346 | 49.7k | toList.addAtEnd(&attr); |
347 | 49.7k | } |
348 | | |
349 | | /// The location of a type attribute. |
350 | | enum TypeAttrLocation { |
351 | | /// The attribute is in the decl-specifier-seq. |
352 | | TAL_DeclSpec, |
353 | | /// The attribute is part of a DeclaratorChunk. |
354 | | TAL_DeclChunk, |
355 | | /// The attribute is immediately after the declaration's name. |
356 | | TAL_DeclName |
357 | | }; |
358 | | |
359 | | static void processTypeAttrs(TypeProcessingState &state, QualType &type, |
360 | | TypeAttrLocation TAL, |
361 | | const ParsedAttributesView &attrs); |
362 | | |
363 | | static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, |
364 | | QualType &type); |
365 | | |
366 | | static bool handleMSPointerTypeQualifierAttr(TypeProcessingState &state, |
367 | | ParsedAttr &attr, QualType &type); |
368 | | |
369 | | static bool handleObjCGCTypeAttr(TypeProcessingState &state, ParsedAttr &attr, |
370 | | QualType &type); |
371 | | |
372 | | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, |
373 | | ParsedAttr &attr, QualType &type); |
374 | | |
375 | | static bool handleObjCPointerTypeAttr(TypeProcessingState &state, |
376 | 8.18k | ParsedAttr &attr, QualType &type) { |
377 | 8.18k | if (attr.getKind() == ParsedAttr::AT_ObjCGC) |
378 | 292 | return handleObjCGCTypeAttr(state, attr, type); |
379 | 7.89k | assert(attr.getKind() == ParsedAttr::AT_ObjCOwnership); |
380 | 0 | return handleObjCOwnershipTypeAttr(state, attr, type); |
381 | 8.18k | } |
382 | | |
383 | | /// Given the index of a declarator chunk, check whether that chunk |
384 | | /// directly specifies the return type of a function and, if so, find |
385 | | /// an appropriate place for it. |
386 | | /// |
387 | | /// \param i - a notional index which the search will start |
388 | | /// immediately inside |
389 | | /// |
390 | | /// \param onlyBlockPointers Whether we should only look into block |
391 | | /// pointer types (vs. all pointer types). |
392 | | static DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator, |
393 | | unsigned i, |
394 | 5.38k | bool onlyBlockPointers) { |
395 | 5.38k | assert(i <= declarator.getNumTypeObjects()); |
396 | | |
397 | 0 | DeclaratorChunk *result = nullptr; |
398 | | |
399 | | // First, look inwards past parens for a function declarator. |
400 | 5.42k | for (; i != 0; --i37 ) { |
401 | 3.78k | DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1); |
402 | 3.78k | switch (fnChunk.Kind) { |
403 | 9 | case DeclaratorChunk::Paren: |
404 | 9 | continue; |
405 | | |
406 | | // If we find anything except a function, bail out. |
407 | 1.54k | case DeclaratorChunk::Pointer: |
408 | 1.54k | case DeclaratorChunk::BlockPointer: |
409 | 3.56k | case DeclaratorChunk::Array: |
410 | 3.73k | case DeclaratorChunk::Reference: |
411 | 3.73k | case DeclaratorChunk::MemberPointer: |
412 | 3.73k | case DeclaratorChunk::Pipe: |
413 | 3.73k | return result; |
414 | | |
415 | | // If we do find a function declarator, scan inwards from that, |
416 | | // looking for a (block-)pointer declarator. |
417 | 42 | case DeclaratorChunk::Function: |
418 | 78 | for (--i; i != 0; --i36 ) { |
419 | 64 | DeclaratorChunk &ptrChunk = declarator.getTypeObject(i-1); |
420 | 64 | switch (ptrChunk.Kind) { |
421 | 32 | case DeclaratorChunk::Paren: |
422 | 32 | case DeclaratorChunk::Array: |
423 | 32 | case DeclaratorChunk::Function: |
424 | 32 | case DeclaratorChunk::Reference: |
425 | 32 | case DeclaratorChunk::Pipe: |
426 | 32 | continue; |
427 | | |
428 | 2 | case DeclaratorChunk::MemberPointer: |
429 | 10 | case DeclaratorChunk::Pointer: |
430 | 10 | if (onlyBlockPointers) |
431 | 4 | continue; |
432 | | |
433 | 10 | LLVM_FALLTHROUGH6 ;6 |
434 | | |
435 | 28 | case DeclaratorChunk::BlockPointer: |
436 | 28 | result = &ptrChunk; |
437 | 28 | goto continue_outer; |
438 | 64 | } |
439 | 0 | llvm_unreachable("bad declarator chunk kind"); |
440 | 0 | } |
441 | | |
442 | | // If we run out of declarators doing that, we're done. |
443 | 14 | return result; |
444 | 3.78k | } |
445 | 0 | llvm_unreachable("bad declarator chunk kind"); |
446 | | |
447 | | // Okay, reconsider from our new point. |
448 | 28 | continue_outer: ; |
449 | 28 | } |
450 | | |
451 | | // Ran out of chunks, bail out. |
452 | 1.64k | return result; |
453 | 5.38k | } |
454 | | |
455 | | /// Given that an objc_gc attribute was written somewhere on a |
456 | | /// declaration *other* than on the declarator itself (for which, use |
457 | | /// distributeObjCPointerTypeAttrFromDeclarator), and given that it |
458 | | /// didn't apply in whatever position it was written in, try to move |
459 | | /// it to a more appropriate position. |
460 | | static void distributeObjCPointerTypeAttr(TypeProcessingState &state, |
461 | 1.61k | ParsedAttr &attr, QualType type) { |
462 | 1.61k | Declarator &declarator = state.getDeclarator(); |
463 | | |
464 | | // Move it to the outermost normal or block pointer declarator. |
465 | 1.61k | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i0 ) { |
466 | 1.61k | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
467 | 1.61k | switch (chunk.Kind) { |
468 | 1.60k | case DeclaratorChunk::Pointer: |
469 | 1.60k | case DeclaratorChunk::BlockPointer: { |
470 | | // But don't move an ARC ownership attribute to the return type |
471 | | // of a block. |
472 | 1.60k | DeclaratorChunk *destChunk = nullptr; |
473 | 1.60k | if (state.isProcessingDeclSpec() && |
474 | 1.60k | attr.getKind() == ParsedAttr::AT_ObjCOwnership) |
475 | 1.52k | destChunk = maybeMovePastReturnType(declarator, i - 1, |
476 | 1.52k | /*onlyBlockPointers=*/true); |
477 | 1.60k | if (!destChunk) destChunk = &chunk1.59k ; |
478 | | |
479 | 1.60k | moveAttrFromListToList(attr, state.getCurrentAttributes(), |
480 | 1.60k | destChunk->getAttrs()); |
481 | 1.60k | return; |
482 | 1.60k | } |
483 | | |
484 | 0 | case DeclaratorChunk::Paren: |
485 | 0 | case DeclaratorChunk::Array: |
486 | 0 | continue; |
487 | | |
488 | | // We may be starting at the return type of a block. |
489 | 8 | case DeclaratorChunk::Function: |
490 | 8 | if (state.isProcessingDeclSpec() && |
491 | 8 | attr.getKind() == ParsedAttr::AT_ObjCOwnership) { |
492 | 8 | if (DeclaratorChunk *dest = maybeMovePastReturnType( |
493 | 8 | declarator, i, |
494 | 8 | /*onlyBlockPointers=*/true)) { |
495 | 8 | moveAttrFromListToList(attr, state.getCurrentAttributes(), |
496 | 8 | dest->getAttrs()); |
497 | 8 | return; |
498 | 8 | } |
499 | 8 | } |
500 | 0 | goto error; |
501 | | |
502 | | // Don't walk through these. |
503 | 0 | case DeclaratorChunk::Reference: |
504 | 0 | case DeclaratorChunk::MemberPointer: |
505 | 0 | case DeclaratorChunk::Pipe: |
506 | 0 | goto error; |
507 | 1.61k | } |
508 | 1.61k | } |
509 | 6 | error: |
510 | | |
511 | 6 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
512 | 6 | } |
513 | | |
514 | | /// Distribute an objc_gc type attribute that was written on the |
515 | | /// declarator. |
516 | | static void distributeObjCPointerTypeAttrFromDeclarator( |
517 | 18 | TypeProcessingState &state, ParsedAttr &attr, QualType &declSpecType) { |
518 | 18 | Declarator &declarator = state.getDeclarator(); |
519 | | |
520 | | // objc_gc goes on the innermost pointer to something that's not a |
521 | | // pointer. |
522 | 18 | unsigned innermost = -1U; |
523 | 18 | bool considerDeclSpec = true; |
524 | 28 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i10 ) { |
525 | 15 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
526 | 15 | switch (chunk.Kind) { |
527 | 0 | case DeclaratorChunk::Pointer: |
528 | 4 | case DeclaratorChunk::BlockPointer: |
529 | 4 | innermost = i; |
530 | 4 | continue; |
531 | | |
532 | 0 | case DeclaratorChunk::Reference: |
533 | 0 | case DeclaratorChunk::MemberPointer: |
534 | 4 | case DeclaratorChunk::Paren: |
535 | 6 | case DeclaratorChunk::Array: |
536 | 6 | case DeclaratorChunk::Pipe: |
537 | 6 | continue; |
538 | | |
539 | 5 | case DeclaratorChunk::Function: |
540 | 5 | considerDeclSpec = false; |
541 | 5 | goto done; |
542 | 15 | } |
543 | 15 | } |
544 | 18 | done: |
545 | | |
546 | | // That might actually be the decl spec if we weren't blocked by |
547 | | // anything in the declarator. |
548 | 18 | if (considerDeclSpec) { |
549 | 13 | if (handleObjCPointerTypeAttr(state, attr, declSpecType)) { |
550 | | // Splice the attribute into the decl spec. Prevents the |
551 | | // attribute from being applied multiple times and gives |
552 | | // the source-location-filler something to work with. |
553 | 13 | state.saveDeclSpecAttrs(); |
554 | 13 | declarator.getMutableDeclSpec().getAttributes().takeOneFrom( |
555 | 13 | declarator.getAttributes(), &attr); |
556 | 13 | return; |
557 | 13 | } |
558 | 13 | } |
559 | | |
560 | | // Otherwise, if we found an appropriate chunk, splice the attribute |
561 | | // into it. |
562 | 5 | if (innermost != -1U) { |
563 | 4 | moveAttrFromListToList(attr, declarator.getAttributes(), |
564 | 4 | declarator.getTypeObject(innermost).getAttrs()); |
565 | 4 | return; |
566 | 4 | } |
567 | | |
568 | | // Otherwise, diagnose when we're done building the type. |
569 | 1 | declarator.getAttributes().remove(&attr); |
570 | 1 | state.addIgnoredTypeAttr(attr); |
571 | 1 | } |
572 | | |
573 | | /// A function type attribute was written somewhere in a declaration |
574 | | /// *other* than on the declarator itself or in the decl spec. Given |
575 | | /// that it didn't apply in whatever position it was written in, try |
576 | | /// to move it to a more appropriate position. |
577 | | static void distributeFunctionTypeAttr(TypeProcessingState &state, |
578 | 30 | ParsedAttr &attr, QualType type) { |
579 | 30 | Declarator &declarator = state.getDeclarator(); |
580 | | |
581 | | // Try to push the attribute from the return type of a function to |
582 | | // the function itself. |
583 | 32 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i2 ) { |
584 | 32 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
585 | 32 | switch (chunk.Kind) { |
586 | 30 | case DeclaratorChunk::Function: |
587 | 30 | moveAttrFromListToList(attr, state.getCurrentAttributes(), |
588 | 30 | chunk.getAttrs()); |
589 | 30 | return; |
590 | | |
591 | 2 | case DeclaratorChunk::Paren: |
592 | 2 | case DeclaratorChunk::Pointer: |
593 | 2 | case DeclaratorChunk::BlockPointer: |
594 | 2 | case DeclaratorChunk::Array: |
595 | 2 | case DeclaratorChunk::Reference: |
596 | 2 | case DeclaratorChunk::MemberPointer: |
597 | 2 | case DeclaratorChunk::Pipe: |
598 | 2 | continue; |
599 | 32 | } |
600 | 32 | } |
601 | | |
602 | 0 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
603 | 0 | } |
604 | | |
605 | | /// Try to distribute a function type attribute to the innermost |
606 | | /// function chunk or type. Returns true if the attribute was |
607 | | /// distributed, false if no location was found. |
608 | | static bool distributeFunctionTypeAttrToInnermost( |
609 | | TypeProcessingState &state, ParsedAttr &attr, |
610 | 48.1k | ParsedAttributesView &attrList, QualType &declSpecType) { |
611 | 48.1k | Declarator &declarator = state.getDeclarator(); |
612 | | |
613 | | // Put it on the innermost function chunk, if there is one. |
614 | 48.4k | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i306 ) { |
615 | 48.3k | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
616 | 48.3k | if (chunk.Kind != DeclaratorChunk::Function) continue306 ; |
617 | | |
618 | 48.0k | moveAttrFromListToList(attr, attrList, chunk.getAttrs()); |
619 | 48.0k | return true; |
620 | 48.3k | } |
621 | | |
622 | 59 | return handleFunctionTypeAttr(state, attr, declSpecType); |
623 | 48.1k | } |
624 | | |
625 | | /// A function type attribute was written in the decl spec. Try to |
626 | | /// apply it somewhere. |
627 | | static void distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state, |
628 | | ParsedAttr &attr, |
629 | 41.2k | QualType &declSpecType) { |
630 | 41.2k | state.saveDeclSpecAttrs(); |
631 | | |
632 | | // Try to distribute to the innermost. |
633 | 41.2k | if (distributeFunctionTypeAttrToInnermost( |
634 | 41.2k | state, attr, state.getCurrentAttributes(), declSpecType)) |
635 | 41.2k | return; |
636 | | |
637 | | // If that failed, diagnose the bad attribute when the declarator is |
638 | | // fully built. |
639 | 10 | state.addIgnoredTypeAttr(attr); |
640 | 10 | } |
641 | | |
642 | | /// A function type attribute was written on the declarator or declaration. |
643 | | /// Try to apply it somewhere. |
644 | | /// `Attrs` is the attribute list containing the declaration (either of the |
645 | | /// declarator or the declaration). |
646 | | static void distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state, |
647 | | ParsedAttr &attr, |
648 | 6.85k | QualType &declSpecType) { |
649 | 6.85k | Declarator &declarator = state.getDeclarator(); |
650 | | |
651 | | // Try to distribute to the innermost. |
652 | 6.85k | if (distributeFunctionTypeAttrToInnermost( |
653 | 6.85k | state, attr, declarator.getAttributes(), declSpecType)) |
654 | 6.84k | return; |
655 | | |
656 | | // If that failed, diagnose the bad attribute when the declarator is |
657 | | // fully built. |
658 | 15 | declarator.getAttributes().remove(&attr); |
659 | 15 | state.addIgnoredTypeAttr(attr); |
660 | 15 | } |
661 | | |
662 | | /// Given that there are attributes written on the declarator or declaration |
663 | | /// itself, try to distribute any type attributes to the appropriate |
664 | | /// declarator chunk. |
665 | | /// |
666 | | /// These are attributes like the following: |
667 | | /// int f ATTR; |
668 | | /// int (f ATTR)(); |
669 | | /// but not necessarily this: |
670 | | /// int f() ATTR; |
671 | | /// |
672 | | /// `Attrs` is the attribute list containing the declaration (either of the |
673 | | /// declarator or the declaration). |
674 | | static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state, |
675 | 111M | QualType &declSpecType) { |
676 | | // The called functions in this loop actually remove things from the current |
677 | | // list, so iterating over the existing list isn't possible. Instead, make a |
678 | | // non-owning copy and iterate over that. |
679 | 111M | ParsedAttributesView AttrsCopy{state.getDeclarator().getAttributes()}; |
680 | 111M | for (ParsedAttr &attr : AttrsCopy) { |
681 | | // Do not distribute [[]] attributes. They have strict rules for what |
682 | | // they appertain to. |
683 | 4.55M | if (attr.isStandardAttributeSyntax()) |
684 | 260 | continue; |
685 | | |
686 | 4.55M | switch (attr.getKind()) { |
687 | 18 | OBJC_POINTER_TYPE_ATTRS_CASELIST2 : |
688 | 18 | distributeObjCPointerTypeAttrFromDeclarator(state, attr, declSpecType); |
689 | 18 | break; |
690 | | |
691 | 6.85k | FUNCTION_TYPE_ATTRS_CASELIST513 : |
692 | 6.85k | distributeFunctionTypeAttrFromDeclarator(state, attr, declSpecType); |
693 | 6.85k | break; |
694 | | |
695 | 0 | MS_TYPE_ATTRS_CASELIST: |
696 | | // Microsoft type attributes cannot go after the declarator-id. |
697 | 0 | continue; |
698 | | |
699 | 0 | NULLABILITY_TYPE_ATTRS_CASELIST: |
700 | | // Nullability specifiers cannot go after the declarator-id. |
701 | | |
702 | | // Objective-C __kindof does not get distributed. |
703 | 0 | case ParsedAttr::AT_ObjCKindOf: |
704 | 0 | continue; |
705 | | |
706 | 4.55M | default: |
707 | 4.55M | break; |
708 | 4.55M | } |
709 | 4.55M | } |
710 | 111M | } |
711 | | |
712 | | /// Add a synthetic '()' to a block-literal declarator if it is |
713 | | /// required, given the return type. |
714 | | static void maybeSynthesizeBlockSignature(TypeProcessingState &state, |
715 | 3.33k | QualType declSpecType) { |
716 | 3.33k | Declarator &declarator = state.getDeclarator(); |
717 | | |
718 | | // First, check whether the declarator would produce a function, |
719 | | // i.e. whether the innermost semantic chunk is a function. |
720 | 3.33k | if (declarator.isFunctionDeclarator()) { |
721 | | // If so, make that declarator a prototyped declarator. |
722 | 3.30k | declarator.getFunctionTypeInfo().hasPrototype = true; |
723 | 3.30k | return; |
724 | 3.30k | } |
725 | | |
726 | | // If there are any type objects, the type as written won't name a |
727 | | // function, regardless of the decl spec type. This is because a |
728 | | // block signature declarator is always an abstract-declarator, and |
729 | | // abstract-declarators can't just be parentheses chunks. Therefore |
730 | | // we need to build a function chunk unless there are no type |
731 | | // objects and the decl spec type is a function. |
732 | 24 | if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType()21 ) |
733 | 4 | return; |
734 | | |
735 | | // Note that there *are* cases with invalid declarators where |
736 | | // declarators consist solely of parentheses. In general, these |
737 | | // occur only in failed efforts to make function declarators, so |
738 | | // faking up the function chunk is still the right thing to do. |
739 | | |
740 | | // Otherwise, we need to fake up a function declarator. |
741 | 20 | SourceLocation loc = declarator.getBeginLoc(); |
742 | | |
743 | | // ...and *prepend* it to the declarator. |
744 | 20 | SourceLocation NoLoc; |
745 | 20 | declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction( |
746 | 20 | /*HasProto=*/true, |
747 | 20 | /*IsAmbiguous=*/false, |
748 | 20 | /*LParenLoc=*/NoLoc, |
749 | 20 | /*ArgInfo=*/nullptr, |
750 | 20 | /*NumParams=*/0, |
751 | 20 | /*EllipsisLoc=*/NoLoc, |
752 | 20 | /*RParenLoc=*/NoLoc, |
753 | 20 | /*RefQualifierIsLvalueRef=*/true, |
754 | 20 | /*RefQualifierLoc=*/NoLoc, |
755 | 20 | /*MutableLoc=*/NoLoc, EST_None, |
756 | 20 | /*ESpecRange=*/SourceRange(), |
757 | 20 | /*Exceptions=*/nullptr, |
758 | 20 | /*ExceptionRanges=*/nullptr, |
759 | 20 | /*NumExceptions=*/0, |
760 | 20 | /*NoexceptExpr=*/nullptr, |
761 | 20 | /*ExceptionSpecTokens=*/nullptr, |
762 | 20 | /*DeclsInPrototype=*/None, loc, loc, declarator)); |
763 | | |
764 | | // For consistency, make sure the state still has us as processing |
765 | | // the decl spec. |
766 | 20 | assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1); |
767 | 0 | state.setCurrentChunkIndex(declarator.getNumTypeObjects()); |
768 | 20 | } |
769 | | |
770 | | static void diagnoseAndRemoveTypeQualifiers(Sema &S, const DeclSpec &DS, |
771 | | unsigned &TypeQuals, |
772 | | QualType TypeSoFar, |
773 | | unsigned RemoveTQs, |
774 | 3.08k | unsigned DiagID) { |
775 | | // If this occurs outside a template instantiation, warn the user about |
776 | | // it; they probably didn't mean to specify a redundant qualifier. |
777 | 3.08k | typedef std::pair<DeclSpec::TQ, SourceLocation> QualLoc; |
778 | 3.08k | for (QualLoc Qual : {QualLoc(DeclSpec::TQ_const, DS.getConstSpecLoc()), |
779 | 3.08k | QualLoc(DeclSpec::TQ_restrict, DS.getRestrictSpecLoc()), |
780 | 3.08k | QualLoc(DeclSpec::TQ_volatile, DS.getVolatileSpecLoc()), |
781 | 12.3k | QualLoc(DeclSpec::TQ_atomic, DS.getAtomicSpecLoc())}) { |
782 | 12.3k | if (!(RemoveTQs & Qual.first)) |
783 | 98 | continue; |
784 | | |
785 | 12.2k | if (!S.inTemplateInstantiation()) { |
786 | 12.2k | if (TypeQuals & Qual.first) |
787 | 79 | S.Diag(Qual.second, DiagID) |
788 | 79 | << DeclSpec::getSpecifierName(Qual.first) << TypeSoFar |
789 | 79 | << FixItHint::CreateRemoval(Qual.second); |
790 | 12.2k | } |
791 | | |
792 | 12.2k | TypeQuals &= ~Qual.first; |
793 | 12.2k | } |
794 | 3.08k | } |
795 | | |
796 | | /// Return true if this is omitted block return type. Also check type |
797 | | /// attributes and type qualifiers when returning true. |
798 | | static bool checkOmittedBlockReturnType(Sema &S, Declarator &declarator, |
799 | 3.34k | QualType Result) { |
800 | 3.34k | if (!isOmittedBlockReturnType(declarator)) |
801 | 344 | return false; |
802 | | |
803 | | // Warn if we see type attributes for omitted return type on a block literal. |
804 | 3.00k | SmallVector<ParsedAttr *, 2> ToBeRemoved; |
805 | 3.00k | for (ParsedAttr &AL : declarator.getMutableDeclSpec().getAttributes()) { |
806 | 22 | if (AL.isInvalid() || !AL.isTypeAttr()) |
807 | 17 | continue; |
808 | 5 | S.Diag(AL.getLoc(), |
809 | 5 | diag::warn_block_literal_attributes_on_omitted_return_type) |
810 | 5 | << AL; |
811 | 5 | ToBeRemoved.push_back(&AL); |
812 | 5 | } |
813 | | // Remove bad attributes from the list. |
814 | 3.00k | for (ParsedAttr *AL : ToBeRemoved) |
815 | 5 | declarator.getMutableDeclSpec().getAttributes().remove(AL); |
816 | | |
817 | | // Warn if we see type qualifiers for omitted return type on a block literal. |
818 | 3.00k | const DeclSpec &DS = declarator.getDeclSpec(); |
819 | 3.00k | unsigned TypeQuals = DS.getTypeQualifiers(); |
820 | 3.00k | diagnoseAndRemoveTypeQualifiers(S, DS, TypeQuals, Result, (unsigned)-1, |
821 | 3.00k | diag::warn_block_literal_qualifiers_on_omitted_return_type); |
822 | 3.00k | declarator.getMutableDeclSpec().ClearTypeQualifiers(); |
823 | | |
824 | 3.00k | return true; |
825 | 3.34k | } |
826 | | |
827 | | /// Apply Objective-C type arguments to the given type. |
828 | | static QualType applyObjCTypeArgs(Sema &S, SourceLocation loc, QualType type, |
829 | | ArrayRef<TypeSourceInfo *> typeArgs, |
830 | | SourceRange typeArgsRange, |
831 | 129k | bool failOnError = false) { |
832 | | // We can only apply type arguments to an Objective-C class type. |
833 | 129k | const auto *objcObjectType = type->getAs<ObjCObjectType>(); |
834 | 129k | if (!objcObjectType || !objcObjectType->getInterface()129k ) { |
835 | 1 | S.Diag(loc, diag::err_objc_type_args_non_class) |
836 | 1 | << type |
837 | 1 | << typeArgsRange; |
838 | | |
839 | 1 | if (failOnError) |
840 | 0 | return QualType(); |
841 | 1 | return type; |
842 | 1 | } |
843 | | |
844 | | // The class type must be parameterized. |
845 | 129k | ObjCInterfaceDecl *objcClass = objcObjectType->getInterface(); |
846 | 129k | ObjCTypeParamList *typeParams = objcClass->getTypeParamList(); |
847 | 129k | if (!typeParams) { |
848 | 1 | S.Diag(loc, diag::err_objc_type_args_non_parameterized_class) |
849 | 1 | << objcClass->getDeclName() |
850 | 1 | << FixItHint::CreateRemoval(typeArgsRange); |
851 | | |
852 | 1 | if (failOnError) |
853 | 0 | return QualType(); |
854 | | |
855 | 1 | return type; |
856 | 1 | } |
857 | | |
858 | | // The type must not already be specialized. |
859 | 129k | if (objcObjectType->isSpecialized()) { |
860 | 3 | S.Diag(loc, diag::err_objc_type_args_specialized_class) |
861 | 3 | << type |
862 | 3 | << FixItHint::CreateRemoval(typeArgsRange); |
863 | | |
864 | 3 | if (failOnError) |
865 | 0 | return QualType(); |
866 | | |
867 | 3 | return type; |
868 | 3 | } |
869 | | |
870 | | // Check the type arguments. |
871 | 129k | SmallVector<QualType, 4> finalTypeArgs; |
872 | 129k | unsigned numTypeParams = typeParams->size(); |
873 | 129k | bool anyPackExpansions = false; |
874 | 289k | for (unsigned i = 0, n = typeArgs.size(); i != n; ++i160k ) { |
875 | 160k | TypeSourceInfo *typeArgInfo = typeArgs[i]; |
876 | 160k | QualType typeArg = typeArgInfo->getType(); |
877 | | |
878 | | // Type arguments cannot have explicit qualifiers or nullability. |
879 | | // We ignore indirect sources of these, e.g. behind typedefs or |
880 | | // template arguments. |
881 | 160k | if (TypeLoc qual = typeArgInfo->getTypeLoc().findExplicitQualifierLoc()) { |
882 | 17 | bool diagnosed = false; |
883 | 17 | SourceRange rangeToRemove; |
884 | 17 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
885 | 12 | rangeToRemove = attr.getLocalSourceRange(); |
886 | 12 | if (attr.getTypePtr()->getImmediateNullability()) { |
887 | 1 | typeArg = attr.getTypePtr()->getModifiedType(); |
888 | 1 | S.Diag(attr.getBeginLoc(), |
889 | 1 | diag::err_objc_type_arg_explicit_nullability) |
890 | 1 | << typeArg << FixItHint::CreateRemoval(rangeToRemove); |
891 | 1 | diagnosed = true; |
892 | 1 | } |
893 | 12 | } |
894 | | |
895 | 17 | if (!diagnosed) { |
896 | 16 | S.Diag(qual.getBeginLoc(), diag::err_objc_type_arg_qualified) |
897 | 16 | << typeArg << typeArg.getQualifiers().getAsString() |
898 | 16 | << FixItHint::CreateRemoval(rangeToRemove); |
899 | 16 | } |
900 | 17 | } |
901 | | |
902 | | // Remove qualifiers even if they're non-local. |
903 | 160k | typeArg = typeArg.getUnqualifiedType(); |
904 | | |
905 | 160k | finalTypeArgs.push_back(typeArg); |
906 | | |
907 | 160k | if (typeArg->getAs<PackExpansionType>()) |
908 | 1 | anyPackExpansions = true; |
909 | | |
910 | | // Find the corresponding type parameter, if there is one. |
911 | 160k | ObjCTypeParamDecl *typeParam = nullptr; |
912 | 160k | if (!anyPackExpansions) { |
913 | 160k | if (i < numTypeParams) { |
914 | 160k | typeParam = typeParams->begin()[i]; |
915 | 160k | } else { |
916 | | // Too many arguments. |
917 | 2 | S.Diag(loc, diag::err_objc_type_args_wrong_arity) |
918 | 2 | << false |
919 | 2 | << objcClass->getDeclName() |
920 | 2 | << (unsigned)typeArgs.size() |
921 | 2 | << numTypeParams; |
922 | 2 | S.Diag(objcClass->getLocation(), diag::note_previous_decl) |
923 | 2 | << objcClass; |
924 | | |
925 | 2 | if (failOnError) |
926 | 1 | return QualType(); |
927 | | |
928 | 1 | return type; |
929 | 2 | } |
930 | 160k | } |
931 | | |
932 | | // Objective-C object pointer types must be substitutable for the bounds. |
933 | 160k | if (const auto *typeArgObjC = typeArg->getAs<ObjCObjectPointerType>()) { |
934 | | // If we don't have a type parameter to match against, assume |
935 | | // everything is fine. There was a prior pack expansion that |
936 | | // means we won't be able to match anything. |
937 | 159k | if (!typeParam) { |
938 | 0 | assert(anyPackExpansions && "Too many arguments?"); |
939 | 0 | continue; |
940 | 0 | } |
941 | | |
942 | | // Retrieve the bound. |
943 | 159k | QualType bound = typeParam->getUnderlyingType(); |
944 | 159k | const auto *boundObjC = bound->getAs<ObjCObjectPointerType>(); |
945 | | |
946 | | // Determine whether the type argument is substitutable for the bound. |
947 | 159k | if (typeArgObjC->isObjCIdType()) { |
948 | | // When the type argument is 'id', the only acceptable type |
949 | | // parameter bound is 'id'. |
950 | 63.8k | if (boundObjC->isObjCIdType()) |
951 | 63.8k | continue; |
952 | 96.0k | } else if (S.Context.canAssignObjCInterfaces(boundObjC, typeArgObjC)) { |
953 | | // Otherwise, we follow the assignability rules. |
954 | 96.0k | continue; |
955 | 96.0k | } |
956 | | |
957 | | // Diagnose the mismatch. |
958 | 6 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), |
959 | 6 | diag::err_objc_type_arg_does_not_match_bound) |
960 | 6 | << typeArg << bound << typeParam->getDeclName(); |
961 | 6 | S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here) |
962 | 6 | << typeParam->getDeclName(); |
963 | | |
964 | 6 | if (failOnError) |
965 | 2 | return QualType(); |
966 | | |
967 | 4 | return type; |
968 | 6 | } |
969 | | |
970 | | // Block pointer types are permitted for unqualified 'id' bounds. |
971 | 237 | if (typeArg->isBlockPointerType()) { |
972 | | // If we don't have a type parameter to match against, assume |
973 | | // everything is fine. There was a prior pack expansion that |
974 | | // means we won't be able to match anything. |
975 | 229 | if (!typeParam) { |
976 | 0 | assert(anyPackExpansions && "Too many arguments?"); |
977 | 0 | continue; |
978 | 0 | } |
979 | | |
980 | | // Retrieve the bound. |
981 | 229 | QualType bound = typeParam->getUnderlyingType(); |
982 | 229 | if (bound->isBlockCompatibleObjCPointerType(S.Context)) |
983 | 229 | continue; |
984 | | |
985 | | // Diagnose the mismatch. |
986 | 0 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), |
987 | 0 | diag::err_objc_type_arg_does_not_match_bound) |
988 | 0 | << typeArg << bound << typeParam->getDeclName(); |
989 | 0 | S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here) |
990 | 0 | << typeParam->getDeclName(); |
991 | |
|
992 | 0 | if (failOnError) |
993 | 0 | return QualType(); |
994 | | |
995 | 0 | return type; |
996 | 0 | } |
997 | | |
998 | | // Dependent types will be checked at instantiation time. |
999 | 8 | if (typeArg->isDependentType()) { |
1000 | 8 | continue; |
1001 | 8 | } |
1002 | | |
1003 | | // Diagnose non-id-compatible type arguments. |
1004 | 0 | S.Diag(typeArgInfo->getTypeLoc().getBeginLoc(), |
1005 | 0 | diag::err_objc_type_arg_not_id_compatible) |
1006 | 0 | << typeArg << typeArgInfo->getTypeLoc().getSourceRange(); |
1007 | |
|
1008 | 0 | if (failOnError) |
1009 | 0 | return QualType(); |
1010 | | |
1011 | 0 | return type; |
1012 | 0 | } |
1013 | | |
1014 | | // Make sure we didn't have the wrong number of arguments. |
1015 | 129k | if (!anyPackExpansions && finalTypeArgs.size() != numTypeParams129k ) { |
1016 | 3 | S.Diag(loc, diag::err_objc_type_args_wrong_arity) |
1017 | 3 | << (typeArgs.size() < typeParams->size()) |
1018 | 3 | << objcClass->getDeclName() |
1019 | 3 | << (unsigned)finalTypeArgs.size() |
1020 | 3 | << (unsigned)numTypeParams; |
1021 | 3 | S.Diag(objcClass->getLocation(), diag::note_previous_decl) |
1022 | 3 | << objcClass; |
1023 | | |
1024 | 3 | if (failOnError) |
1025 | 1 | return QualType(); |
1026 | | |
1027 | 2 | return type; |
1028 | 3 | } |
1029 | | |
1030 | | // Success. Form the specialized type. |
1031 | 129k | return S.Context.getObjCObjectType(type, finalTypeArgs, { }, false); |
1032 | 129k | } |
1033 | | |
1034 | | QualType Sema::BuildObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
1035 | | SourceLocation ProtocolLAngleLoc, |
1036 | | ArrayRef<ObjCProtocolDecl *> Protocols, |
1037 | | ArrayRef<SourceLocation> ProtocolLocs, |
1038 | | SourceLocation ProtocolRAngleLoc, |
1039 | 0 | bool FailOnError) { |
1040 | 0 | QualType Result = QualType(Decl->getTypeForDecl(), 0); |
1041 | 0 | if (!Protocols.empty()) { |
1042 | 0 | bool HasError; |
1043 | 0 | Result = Context.applyObjCProtocolQualifiers(Result, Protocols, |
1044 | 0 | HasError); |
1045 | 0 | if (HasError) { |
1046 | 0 | Diag(SourceLocation(), diag::err_invalid_protocol_qualifiers) |
1047 | 0 | << SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc); |
1048 | 0 | if (FailOnError) Result = QualType(); |
1049 | 0 | } |
1050 | 0 | if (FailOnError && Result.isNull()) |
1051 | 0 | return QualType(); |
1052 | 0 | } |
1053 | | |
1054 | 0 | return Result; |
1055 | 0 | } |
1056 | | |
1057 | | QualType Sema::BuildObjCObjectType(QualType BaseType, |
1058 | | SourceLocation Loc, |
1059 | | SourceLocation TypeArgsLAngleLoc, |
1060 | | ArrayRef<TypeSourceInfo *> TypeArgs, |
1061 | | SourceLocation TypeArgsRAngleLoc, |
1062 | | SourceLocation ProtocolLAngleLoc, |
1063 | | ArrayRef<ObjCProtocolDecl *> Protocols, |
1064 | | ArrayRef<SourceLocation> ProtocolLocs, |
1065 | | SourceLocation ProtocolRAngleLoc, |
1066 | 151k | bool FailOnError) { |
1067 | 151k | QualType Result = BaseType; |
1068 | 151k | if (!TypeArgs.empty()) { |
1069 | 129k | Result = applyObjCTypeArgs(*this, Loc, Result, TypeArgs, |
1070 | 129k | SourceRange(TypeArgsLAngleLoc, |
1071 | 129k | TypeArgsRAngleLoc), |
1072 | 129k | FailOnError); |
1073 | 129k | if (FailOnError && Result.isNull()17 ) |
1074 | 4 | return QualType(); |
1075 | 129k | } |
1076 | | |
1077 | 151k | if (!Protocols.empty()) { |
1078 | 22.4k | bool HasError; |
1079 | 22.4k | Result = Context.applyObjCProtocolQualifiers(Result, Protocols, |
1080 | 22.4k | HasError); |
1081 | 22.4k | if (HasError) { |
1082 | 1 | Diag(Loc, diag::err_invalid_protocol_qualifiers) |
1083 | 1 | << SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc); |
1084 | 1 | if (FailOnError) Result = QualType()0 ; |
1085 | 1 | } |
1086 | 22.4k | if (FailOnError && Result.isNull()0 ) |
1087 | 0 | return QualType(); |
1088 | 22.4k | } |
1089 | | |
1090 | 151k | return Result; |
1091 | 151k | } |
1092 | | |
1093 | | TypeResult Sema::actOnObjCProtocolQualifierType( |
1094 | | SourceLocation lAngleLoc, |
1095 | | ArrayRef<Decl *> protocols, |
1096 | | ArrayRef<SourceLocation> protocolLocs, |
1097 | 7 | SourceLocation rAngleLoc) { |
1098 | | // Form id<protocol-list>. |
1099 | 7 | QualType Result = Context.getObjCObjectType( |
1100 | 7 | Context.ObjCBuiltinIdTy, { }, |
1101 | 7 | llvm::makeArrayRef( |
1102 | 7 | (ObjCProtocolDecl * const *)protocols.data(), |
1103 | 7 | protocols.size()), |
1104 | 7 | false); |
1105 | 7 | Result = Context.getObjCObjectPointerType(Result); |
1106 | | |
1107 | 7 | TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result); |
1108 | 7 | TypeLoc ResultTL = ResultTInfo->getTypeLoc(); |
1109 | | |
1110 | 7 | auto ObjCObjectPointerTL = ResultTL.castAs<ObjCObjectPointerTypeLoc>(); |
1111 | 7 | ObjCObjectPointerTL.setStarLoc(SourceLocation()); // implicit |
1112 | | |
1113 | 7 | auto ObjCObjectTL = ObjCObjectPointerTL.getPointeeLoc() |
1114 | 7 | .castAs<ObjCObjectTypeLoc>(); |
1115 | 7 | ObjCObjectTL.setHasBaseTypeAsWritten(false); |
1116 | 7 | ObjCObjectTL.getBaseLoc().initialize(Context, SourceLocation()); |
1117 | | |
1118 | | // No type arguments. |
1119 | 7 | ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation()); |
1120 | 7 | ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation()); |
1121 | | |
1122 | | // Fill in protocol qualifiers. |
1123 | 7 | ObjCObjectTL.setProtocolLAngleLoc(lAngleLoc); |
1124 | 7 | ObjCObjectTL.setProtocolRAngleLoc(rAngleLoc); |
1125 | 14 | for (unsigned i = 0, n = protocols.size(); i != n; ++i7 ) |
1126 | 7 | ObjCObjectTL.setProtocolLoc(i, protocolLocs[i]); |
1127 | | |
1128 | | // We're done. Return the completed type to the parser. |
1129 | 7 | return CreateParsedType(Result, ResultTInfo); |
1130 | 7 | } |
1131 | | |
1132 | | TypeResult Sema::actOnObjCTypeArgsAndProtocolQualifiers( |
1133 | | Scope *S, |
1134 | | SourceLocation Loc, |
1135 | | ParsedType BaseType, |
1136 | | SourceLocation TypeArgsLAngleLoc, |
1137 | | ArrayRef<ParsedType> TypeArgs, |
1138 | | SourceLocation TypeArgsRAngleLoc, |
1139 | | SourceLocation ProtocolLAngleLoc, |
1140 | | ArrayRef<Decl *> Protocols, |
1141 | | ArrayRef<SourceLocation> ProtocolLocs, |
1142 | 151k | SourceLocation ProtocolRAngleLoc) { |
1143 | 151k | TypeSourceInfo *BaseTypeInfo = nullptr; |
1144 | 151k | QualType T = GetTypeFromParser(BaseType, &BaseTypeInfo); |
1145 | 151k | if (T.isNull()) |
1146 | 0 | return true; |
1147 | | |
1148 | | // Handle missing type-source info. |
1149 | 151k | if (!BaseTypeInfo) |
1150 | 151k | BaseTypeInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
1151 | | |
1152 | | // Extract type arguments. |
1153 | 151k | SmallVector<TypeSourceInfo *, 4> ActualTypeArgInfos; |
1154 | 311k | for (unsigned i = 0, n = TypeArgs.size(); i != n; ++i160k ) { |
1155 | 160k | TypeSourceInfo *TypeArgInfo = nullptr; |
1156 | 160k | QualType TypeArg = GetTypeFromParser(TypeArgs[i], &TypeArgInfo); |
1157 | 160k | if (TypeArg.isNull()) { |
1158 | 0 | ActualTypeArgInfos.clear(); |
1159 | 0 | break; |
1160 | 0 | } |
1161 | | |
1162 | 160k | assert(TypeArgInfo && "No type source info?"); |
1163 | 0 | ActualTypeArgInfos.push_back(TypeArgInfo); |
1164 | 160k | } |
1165 | | |
1166 | | // Build the object type. |
1167 | 151k | QualType Result = BuildObjCObjectType( |
1168 | 151k | T, BaseTypeInfo->getTypeLoc().getSourceRange().getBegin(), |
1169 | 151k | TypeArgsLAngleLoc, ActualTypeArgInfos, TypeArgsRAngleLoc, |
1170 | 151k | ProtocolLAngleLoc, |
1171 | 151k | llvm::makeArrayRef((ObjCProtocolDecl * const *)Protocols.data(), |
1172 | 151k | Protocols.size()), |
1173 | 151k | ProtocolLocs, ProtocolRAngleLoc, |
1174 | 151k | /*FailOnError=*/false); |
1175 | | |
1176 | 151k | if (Result == T) |
1177 | 57 | return BaseType; |
1178 | | |
1179 | | // Create source information for this type. |
1180 | 151k | TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result); |
1181 | 151k | TypeLoc ResultTL = ResultTInfo->getTypeLoc(); |
1182 | | |
1183 | | // For id<Proto1, Proto2> or Class<Proto1, Proto2>, we'll have an |
1184 | | // object pointer type. Fill in source information for it. |
1185 | 151k | if (auto ObjCObjectPointerTL = ResultTL.getAs<ObjCObjectPointerTypeLoc>()) { |
1186 | | // The '*' is implicit. |
1187 | 15.5k | ObjCObjectPointerTL.setStarLoc(SourceLocation()); |
1188 | 15.5k | ResultTL = ObjCObjectPointerTL.getPointeeLoc(); |
1189 | 15.5k | } |
1190 | | |
1191 | 151k | if (auto OTPTL = ResultTL.getAs<ObjCTypeParamTypeLoc>()) { |
1192 | | // Protocol qualifier information. |
1193 | 1.76k | if (OTPTL.getNumProtocols() > 0) { |
1194 | 1.76k | assert(OTPTL.getNumProtocols() == Protocols.size()); |
1195 | 0 | OTPTL.setProtocolLAngleLoc(ProtocolLAngleLoc); |
1196 | 1.76k | OTPTL.setProtocolRAngleLoc(ProtocolRAngleLoc); |
1197 | 3.53k | for (unsigned i = 0, n = Protocols.size(); i != n; ++i1.76k ) |
1198 | 1.76k | OTPTL.setProtocolLoc(i, ProtocolLocs[i]); |
1199 | 1.76k | } |
1200 | | |
1201 | | // We're done. Return the completed type to the parser. |
1202 | 0 | return CreateParsedType(Result, ResultTInfo); |
1203 | 1.76k | } |
1204 | | |
1205 | 149k | auto ObjCObjectTL = ResultTL.castAs<ObjCObjectTypeLoc>(); |
1206 | | |
1207 | | // Type argument information. |
1208 | 149k | if (ObjCObjectTL.getNumTypeArgs() > 0) { |
1209 | 129k | assert(ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size()); |
1210 | 0 | ObjCObjectTL.setTypeArgsLAngleLoc(TypeArgsLAngleLoc); |
1211 | 129k | ObjCObjectTL.setTypeArgsRAngleLoc(TypeArgsRAngleLoc); |
1212 | 289k | for (unsigned i = 0, n = ActualTypeArgInfos.size(); i != n; ++i160k ) |
1213 | 160k | ObjCObjectTL.setTypeArgTInfo(i, ActualTypeArgInfos[i]); |
1214 | 129k | } else { |
1215 | 20.6k | ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation()); |
1216 | 20.6k | ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation()); |
1217 | 20.6k | } |
1218 | | |
1219 | | // Protocol qualifier information. |
1220 | 149k | if (ObjCObjectTL.getNumProtocols() > 0) { |
1221 | 20.6k | assert(ObjCObjectTL.getNumProtocols() == Protocols.size()); |
1222 | 0 | ObjCObjectTL.setProtocolLAngleLoc(ProtocolLAngleLoc); |
1223 | 20.6k | ObjCObjectTL.setProtocolRAngleLoc(ProtocolRAngleLoc); |
1224 | 42.2k | for (unsigned i = 0, n = Protocols.size(); i != n; ++i21.5k ) |
1225 | 21.5k | ObjCObjectTL.setProtocolLoc(i, ProtocolLocs[i]); |
1226 | 129k | } else { |
1227 | 129k | ObjCObjectTL.setProtocolLAngleLoc(SourceLocation()); |
1228 | 129k | ObjCObjectTL.setProtocolRAngleLoc(SourceLocation()); |
1229 | 129k | } |
1230 | | |
1231 | | // Base type. |
1232 | 0 | ObjCObjectTL.setHasBaseTypeAsWritten(true); |
1233 | 149k | if (ObjCObjectTL.getType() == T) |
1234 | 0 | ObjCObjectTL.getBaseLoc().initializeFullCopy(BaseTypeInfo->getTypeLoc()); |
1235 | 149k | else |
1236 | 149k | ObjCObjectTL.getBaseLoc().initialize(Context, Loc); |
1237 | | |
1238 | | // We're done. Return the completed type to the parser. |
1239 | 149k | return CreateParsedType(Result, ResultTInfo); |
1240 | 151k | } |
1241 | | |
1242 | | static OpenCLAccessAttr::Spelling |
1243 | 8.05k | getImageAccess(const ParsedAttributesView &Attrs) { |
1244 | 8.05k | for (const ParsedAttr &AL : Attrs) |
1245 | 7.90k | if (AL.getKind() == ParsedAttr::AT_OpenCLAccess) |
1246 | 7.90k | return static_cast<OpenCLAccessAttr::Spelling>(AL.getSemanticSpelling()); |
1247 | 147 | return OpenCLAccessAttr::Keyword_read_only; |
1248 | 8.05k | } |
1249 | | |
1250 | | /// Convert the specified declspec to the appropriate type |
1251 | | /// object. |
1252 | | /// \param state Specifies the declarator containing the declaration specifier |
1253 | | /// to be converted, along with other associated processing state. |
1254 | | /// \returns The type described by the declaration specifiers. This function |
1255 | | /// never returns null. |
1256 | 111M | static QualType ConvertDeclSpecToType(TypeProcessingState &state) { |
1257 | | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
1258 | | // checking. |
1259 | | |
1260 | 111M | Sema &S = state.getSema(); |
1261 | 111M | Declarator &declarator = state.getDeclarator(); |
1262 | 111M | DeclSpec &DS = declarator.getMutableDeclSpec(); |
1263 | 111M | SourceLocation DeclLoc = declarator.getIdentifierLoc(); |
1264 | 111M | if (DeclLoc.isInvalid()) |
1265 | 895k | DeclLoc = DS.getBeginLoc(); |
1266 | | |
1267 | 111M | ASTContext &Context = S.Context; |
1268 | | |
1269 | 111M | QualType Result; |
1270 | 111M | switch (DS.getTypeSpecType()) { |
1271 | 3.62M | case DeclSpec::TST_void: |
1272 | 3.62M | Result = Context.VoidTy; |
1273 | 3.62M | break; |
1274 | 1.01M | case DeclSpec::TST_char: |
1275 | 1.01M | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified) |
1276 | 820k | Result = Context.CharTy; |
1277 | 190k | else if (DS.getTypeSpecSign() == TypeSpecifierSign::Signed) |
1278 | 52.5k | Result = Context.SignedCharTy; |
1279 | 138k | else { |
1280 | 138k | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && |
1281 | 138k | "Unknown TSS value"); |
1282 | 0 | Result = Context.UnsignedCharTy; |
1283 | 138k | } |
1284 | 0 | break; |
1285 | 141k | case DeclSpec::TST_wchar: |
1286 | 141k | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified) |
1287 | 141k | Result = Context.WCharTy; |
1288 | 6 | else if (DS.getTypeSpecSign() == TypeSpecifierSign::Signed) { |
1289 | 3 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_wchar_t_sign_spec) |
1290 | 3 | << DS.getSpecifierName(DS.getTypeSpecType(), |
1291 | 3 | Context.getPrintingPolicy()); |
1292 | 3 | Result = Context.getSignedWCharType(); |
1293 | 3 | } else { |
1294 | 3 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned && |
1295 | 3 | "Unknown TSS value"); |
1296 | 0 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_wchar_t_sign_spec) |
1297 | 3 | << DS.getSpecifierName(DS.getTypeSpecType(), |
1298 | 3 | Context.getPrintingPolicy()); |
1299 | 3 | Result = Context.getUnsignedWCharType(); |
1300 | 3 | } |
1301 | 0 | break; |
1302 | 183 | case DeclSpec::TST_char8: |
1303 | 183 | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && |
1304 | 183 | "Unknown TSS value"); |
1305 | 0 | Result = Context.Char8Ty; |
1306 | 183 | break; |
1307 | 8.00k | case DeclSpec::TST_char16: |
1308 | 8.00k | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && |
1309 | 8.00k | "Unknown TSS value"); |
1310 | 0 | Result = Context.Char16Ty; |
1311 | 8.00k | break; |
1312 | 8.01k | case DeclSpec::TST_char32: |
1313 | 8.01k | assert(DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && |
1314 | 8.01k | "Unknown TSS value"); |
1315 | 0 | Result = Context.Char32Ty; |
1316 | 8.01k | break; |
1317 | 10.1k | case DeclSpec::TST_unspecified: |
1318 | | // If this is a missing declspec in a block literal return context, then it |
1319 | | // is inferred from the return statements inside the block. |
1320 | | // The declspec is always missing in a lambda expr context; it is either |
1321 | | // specified with a trailing return type or inferred. |
1322 | 10.1k | if (S.getLangOpts().CPlusPlus14 && |
1323 | 10.1k | declarator.getContext() == DeclaratorContext::LambdaExpr5.62k ) { |
1324 | | // In C++1y, a lambda's implicit return type is 'auto'. |
1325 | 4.63k | Result = Context.getAutoDeductType(); |
1326 | 4.63k | break; |
1327 | 5.46k | } else if (declarator.getContext() == DeclaratorContext::LambdaExpr || |
1328 | 5.46k | checkOmittedBlockReturnType(S, declarator, |
1329 | 5.11k | Context.DependentTy)) { |
1330 | 5.11k | Result = Context.DependentTy; |
1331 | 5.11k | break; |
1332 | 5.11k | } |
1333 | | |
1334 | | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
1335 | | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
1336 | | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
1337 | | // Note that the one exception to this is function definitions, which are |
1338 | | // allowed to be completely missing a declspec. This is handled in the |
1339 | | // parser already though by it pretending to have seen an 'int' in this |
1340 | | // case. |
1341 | 344 | if (S.getLangOpts().isImplicitIntRequired()) { |
1342 | 14 | S.Diag(DeclLoc, diag::warn_missing_type_specifier) |
1343 | 14 | << DS.getSourceRange() |
1344 | 14 | << FixItHint::CreateInsertion(DS.getBeginLoc(), "int"); |
1345 | 330 | } else if (!DS.hasTypeSpecifier()) { |
1346 | | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
1347 | | // "At least one type specifier shall be given in the declaration |
1348 | | // specifiers in each declaration, and in the specifier-qualifier list in |
1349 | | // each struct declaration and type name." |
1350 | 330 | if (!S.getLangOpts().isImplicitIntAllowed() && !DS.isTypeSpecPipe()226 ) { |
1351 | 223 | S.Diag(DeclLoc, diag::err_missing_type_specifier) |
1352 | 223 | << DS.getSourceRange(); |
1353 | | |
1354 | | // When this occurs, often something is very broken with the value |
1355 | | // being declared, poison it as invalid so we don't get chains of |
1356 | | // errors. |
1357 | 223 | declarator.setInvalidType(true); |
1358 | 223 | } else if (107 S.getLangOpts().getOpenCLCompatibleVersion() >= 200107 && |
1359 | 107 | DS.isTypeSpecPipe()7 ) { |
1360 | 6 | S.Diag(DeclLoc, diag::err_missing_actual_pipe_type) |
1361 | 6 | << DS.getSourceRange(); |
1362 | 6 | declarator.setInvalidType(true); |
1363 | 101 | } else { |
1364 | 101 | assert(S.getLangOpts().isImplicitIntAllowed() && |
1365 | 101 | "implicit int is disabled?"); |
1366 | 0 | S.Diag(DeclLoc, diag::ext_missing_type_specifier) |
1367 | 101 | << DS.getSourceRange() |
1368 | 101 | << FixItHint::CreateInsertion(DS.getBeginLoc(), "int"); |
1369 | 101 | } |
1370 | 330 | } |
1371 | | |
1372 | 344 | LLVM_FALLTHROUGH; |
1373 | 3.07M | case DeclSpec::TST_int: { |
1374 | 3.07M | if (DS.getTypeSpecSign() != TypeSpecifierSign::Unsigned) { |
1375 | 2.43M | switch (DS.getTypeSpecWidth()) { |
1376 | 1.97M | case TypeSpecifierWidth::Unspecified: |
1377 | 1.97M | Result = Context.IntTy; |
1378 | 1.97M | break; |
1379 | 153k | case TypeSpecifierWidth::Short: |
1380 | 153k | Result = Context.ShortTy; |
1381 | 153k | break; |
1382 | 193k | case TypeSpecifierWidth::Long: |
1383 | 193k | Result = Context.LongTy; |
1384 | 193k | break; |
1385 | 107k | case TypeSpecifierWidth::LongLong: |
1386 | 107k | Result = Context.LongLongTy; |
1387 | | |
1388 | | // 'long long' is a C99 or C++11 feature. |
1389 | 107k | if (!S.getLangOpts().C99) { |
1390 | 61.5k | if (S.getLangOpts().CPlusPlus) |
1391 | 61.5k | S.Diag(DS.getTypeSpecWidthLoc(), |
1392 | 61.5k | S.getLangOpts().CPlusPlus11 ? |
1393 | 60.5k | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong935 ); |
1394 | 41 | else |
1395 | 41 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); |
1396 | 61.5k | } |
1397 | 107k | break; |
1398 | 2.43M | } |
1399 | 2.43M | } else { |
1400 | 641k | switch (DS.getTypeSpecWidth()) { |
1401 | 310k | case TypeSpecifierWidth::Unspecified: |
1402 | 310k | Result = Context.UnsignedIntTy; |
1403 | 310k | break; |
1404 | 121k | case TypeSpecifierWidth::Short: |
1405 | 121k | Result = Context.UnsignedShortTy; |
1406 | 121k | break; |
1407 | 114k | case TypeSpecifierWidth::Long: |
1408 | 114k | Result = Context.UnsignedLongTy; |
1409 | 114k | break; |
1410 | 95.0k | case TypeSpecifierWidth::LongLong: |
1411 | 95.0k | Result = Context.UnsignedLongLongTy; |
1412 | | |
1413 | | // 'long long' is a C99 or C++11 feature. |
1414 | 95.0k | if (!S.getLangOpts().C99) { |
1415 | 57.3k | if (S.getLangOpts().CPlusPlus) |
1416 | 57.3k | S.Diag(DS.getTypeSpecWidthLoc(), |
1417 | 57.3k | S.getLangOpts().CPlusPlus11 ? |
1418 | 56.5k | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong757 ); |
1419 | 11 | else |
1420 | 11 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); |
1421 | 57.3k | } |
1422 | 95.0k | break; |
1423 | 641k | } |
1424 | 641k | } |
1425 | 3.07M | break; |
1426 | 3.07M | } |
1427 | 3.07M | case DeclSpec::TST_bitint: { |
1428 | 1.08k | if (!S.Context.getTargetInfo().hasBitIntType()) |
1429 | 0 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) << "_BitInt"; |
1430 | 1.08k | Result = |
1431 | 1.08k | S.BuildBitIntType(DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned, |
1432 | 1.08k | DS.getRepAsExpr(), DS.getBeginLoc()); |
1433 | 1.08k | if (Result.isNull()) { |
1434 | 11 | Result = Context.IntTy; |
1435 | 11 | declarator.setInvalidType(true); |
1436 | 11 | } |
1437 | 1.08k | break; |
1438 | 3.07M | } |
1439 | 1.02k | case DeclSpec::TST_accum: { |
1440 | 1.02k | switch (DS.getTypeSpecWidth()) { |
1441 | 518 | case TypeSpecifierWidth::Short: |
1442 | 518 | Result = Context.ShortAccumTy; |
1443 | 518 | break; |
1444 | 315 | case TypeSpecifierWidth::Unspecified: |
1445 | 315 | Result = Context.AccumTy; |
1446 | 315 | break; |
1447 | 191 | case TypeSpecifierWidth::Long: |
1448 | 191 | Result = Context.LongAccumTy; |
1449 | 191 | break; |
1450 | 0 | case TypeSpecifierWidth::LongLong: |
1451 | 0 | llvm_unreachable("Unable to specify long long as _Accum width"); |
1452 | 1.02k | } |
1453 | | |
1454 | 1.02k | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) |
1455 | 327 | Result = Context.getCorrespondingUnsignedType(Result); |
1456 | | |
1457 | 1.02k | if (DS.isTypeSpecSat()) |
1458 | 369 | Result = Context.getCorrespondingSaturatedType(Result); |
1459 | | |
1460 | 1.02k | break; |
1461 | 1.02k | } |
1462 | 545 | case DeclSpec::TST_fract: { |
1463 | 545 | switch (DS.getTypeSpecWidth()) { |
1464 | 168 | case TypeSpecifierWidth::Short: |
1465 | 168 | Result = Context.ShortFractTy; |
1466 | 168 | break; |
1467 | 214 | case TypeSpecifierWidth::Unspecified: |
1468 | 214 | Result = Context.FractTy; |
1469 | 214 | break; |
1470 | 163 | case TypeSpecifierWidth::Long: |
1471 | 163 | Result = Context.LongFractTy; |
1472 | 163 | break; |
1473 | 0 | case TypeSpecifierWidth::LongLong: |
1474 | 0 | llvm_unreachable("Unable to specify long long as _Fract width"); |
1475 | 545 | } |
1476 | | |
1477 | 545 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) |
1478 | 208 | Result = Context.getCorrespondingUnsignedType(Result); |
1479 | | |
1480 | 545 | if (DS.isTypeSpecSat()) |
1481 | 176 | Result = Context.getCorrespondingSaturatedType(Result); |
1482 | | |
1483 | 545 | break; |
1484 | 545 | } |
1485 | 916 | case DeclSpec::TST_int128: |
1486 | 916 | if (!S.Context.getTargetInfo().hasInt128Type() && |
1487 | 916 | !(31 S.getLangOpts().SYCLIsDevice31 || S.getLangOpts().CUDAIsDevice13 || |
1488 | 31 | (9 S.getLangOpts().OpenMP9 && S.getLangOpts().OpenMPIsDevice0 ))) |
1489 | 9 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) |
1490 | 9 | << "__int128"; |
1491 | 916 | if (DS.getTypeSpecSign() == TypeSpecifierSign::Unsigned) |
1492 | 608 | Result = Context.UnsignedInt128Ty; |
1493 | 308 | else |
1494 | 308 | Result = Context.Int128Ty; |
1495 | 916 | break; |
1496 | 1.43k | case DeclSpec::TST_float16: |
1497 | | // CUDA host and device may have different _Float16 support, therefore |
1498 | | // do not diagnose _Float16 usage to avoid false alarm. |
1499 | | // ToDo: more precise diagnostics for CUDA. |
1500 | 1.43k | if (!S.Context.getTargetInfo().hasFloat16Type() && !S.getLangOpts().CUDA2 && |
1501 | 1.43k | !(2 S.getLangOpts().OpenMP2 && S.getLangOpts().OpenMPIsDevice0 )) |
1502 | 2 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) |
1503 | 2 | << "_Float16"; |
1504 | 1.43k | Result = Context.Float16Ty; |
1505 | 1.43k | break; |
1506 | 20.0k | case DeclSpec::TST_half: Result = Context.HalfTy; break; |
1507 | 724 | case DeclSpec::TST_BFloat16: |
1508 | 724 | if (!S.Context.getTargetInfo().hasBFloat16Type()) |
1509 | 5 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) |
1510 | 5 | << "__bf16"; |
1511 | 724 | Result = Context.BFloat16Ty; |
1512 | 724 | break; |
1513 | 349k | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
1514 | 527k | case DeclSpec::TST_double: |
1515 | 527k | if (DS.getTypeSpecWidth() == TypeSpecifierWidth::Long) |
1516 | 188k | Result = Context.LongDoubleTy; |
1517 | 338k | else |
1518 | 338k | Result = Context.DoubleTy; |
1519 | 527k | if (S.getLangOpts().OpenCL) { |
1520 | 13.0k | if (!S.getOpenCLOptions().isSupported("cl_khr_fp64", S.getLangOpts())) |
1521 | 38 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_opencl_requires_extension) |
1522 | 38 | << 0 << Result |
1523 | 38 | << (S.getLangOpts().getOpenCLCompatibleVersion() == 300 |
1524 | 38 | ? "cl_khr_fp64 and __opencl_c_fp64"16 |
1525 | 38 | : "cl_khr_fp64"22 ); |
1526 | 13.0k | else if (!S.getOpenCLOptions().isAvailableOption("cl_khr_fp64", S.getLangOpts())) |
1527 | 24 | S.Diag(DS.getTypeSpecTypeLoc(), diag::ext_opencl_double_without_pragma); |
1528 | 13.0k | } |
1529 | 527k | break; |
1530 | 455 | case DeclSpec::TST_float128: |
1531 | 455 | if (!S.Context.getTargetInfo().hasFloat128Type() && |
1532 | 455 | !S.getLangOpts().SYCLIsDevice40 && |
1533 | 455 | !(23 S.getLangOpts().OpenMP23 && S.getLangOpts().OpenMPIsDevice11 )) |
1534 | 12 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) |
1535 | 12 | << "__float128"; |
1536 | 455 | Result = Context.Float128Ty; |
1537 | 455 | break; |
1538 | 108 | case DeclSpec::TST_ibm128: |
1539 | 108 | if (!S.Context.getTargetInfo().hasIbm128Type() && |
1540 | 108 | !S.getLangOpts().SYCLIsDevice17 && |
1541 | 108 | !(17 S.getLangOpts().OpenMP17 && S.getLangOpts().OpenMPIsDevice2 )) |
1542 | 15 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) << "__ibm128"; |
1543 | 108 | Result = Context.Ibm128Ty; |
1544 | 108 | break; |
1545 | 826k | case DeclSpec::TST_bool: |
1546 | 826k | Result = Context.BoolTy; // _Bool or bool |
1547 | 826k | break; |
1548 | 6 | case DeclSpec::TST_decimal32: // _Decimal32 |
1549 | 6 | case DeclSpec::TST_decimal64: // _Decimal64 |
1550 | 8 | case DeclSpec::TST_decimal128: // _Decimal128 |
1551 | 8 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
1552 | 8 | Result = Context.IntTy; |
1553 | 8 | declarator.setInvalidType(true); |
1554 | 8 | break; |
1555 | 25.4k | case DeclSpec::TST_class: |
1556 | 440k | case DeclSpec::TST_enum: |
1557 | 470k | case DeclSpec::TST_union: |
1558 | 1.38M | case DeclSpec::TST_struct: |
1559 | 1.38M | case DeclSpec::TST_interface: { |
1560 | 1.38M | TagDecl *D = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl()); |
1561 | 1.38M | if (!D) { |
1562 | | // This can happen in C++ with ambiguous lookups. |
1563 | 18.9k | Result = Context.IntTy; |
1564 | 18.9k | declarator.setInvalidType(true); |
1565 | 18.9k | break; |
1566 | 18.9k | } |
1567 | | |
1568 | | // If the type is deprecated or unavailable, diagnose it. |
1569 | 1.36M | S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc()); |
1570 | | |
1571 | 1.36M | assert(DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && |
1572 | 1.36M | DS.getTypeSpecComplex() == 0 && |
1573 | 1.36M | DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && |
1574 | 1.36M | "No qualifiers on tag names!"); |
1575 | | |
1576 | | // TypeQuals handled by caller. |
1577 | 0 | Result = Context.getTypeDeclType(D); |
1578 | | |
1579 | | // In both C and C++, make an ElaboratedType. |
1580 | 1.36M | ElaboratedTypeKeyword Keyword |
1581 | 1.36M | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); |
1582 | 1.36M | Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result, |
1583 | 1.36M | DS.isTypeSpecOwned() ? D458k : nullptr906k ); |
1584 | 1.36M | break; |
1585 | 1.38M | } |
1586 | 100M | case DeclSpec::TST_typename: { |
1587 | 100M | assert(DS.getTypeSpecWidth() == TypeSpecifierWidth::Unspecified && |
1588 | 100M | DS.getTypeSpecComplex() == 0 && |
1589 | 100M | DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified && |
1590 | 100M | "Can't handle qualifiers on typedef names yet!"); |
1591 | 0 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
1592 | 100M | if (Result.isNull()) { |
1593 | 0 | declarator.setInvalidType(true); |
1594 | 0 | } |
1595 | | |
1596 | | // TypeQuals handled by caller. |
1597 | 100M | break; |
1598 | 1.38M | } |
1599 | 99 | case DeclSpec::TST_typeofType: |
1600 | | // FIXME: Preserve type source info. |
1601 | 99 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
1602 | 99 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
1603 | 99 | if (!Result->isDependentType()) |
1604 | 97 | if (const TagType *TT = Result->getAs<TagType>()) |
1605 | 8 | S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc()); |
1606 | | // TypeQuals handled by caller. |
1607 | 99 | Result = Context.getTypeOfType(Result); |
1608 | 99 | break; |
1609 | 2.85k | case DeclSpec::TST_typeofExpr: { |
1610 | 2.85k | Expr *E = DS.getRepAsExpr(); |
1611 | 2.85k | assert(E && "Didn't get an expression for typeof?"); |
1612 | | // TypeQuals handled by caller. |
1613 | 0 | Result = S.BuildTypeofExprType(E); |
1614 | 2.85k | if (Result.isNull()) { |
1615 | 0 | Result = Context.IntTy; |
1616 | 0 | declarator.setInvalidType(true); |
1617 | 0 | } |
1618 | 2.85k | break; |
1619 | 1.38M | } |
1620 | 47.4k | case DeclSpec::TST_decltype: { |
1621 | 47.4k | Expr *E = DS.getRepAsExpr(); |
1622 | 47.4k | assert(E && "Didn't get an expression for decltype?"); |
1623 | | // TypeQuals handled by caller. |
1624 | 0 | Result = S.BuildDecltypeType(E); |
1625 | 47.4k | if (Result.isNull()) { |
1626 | 0 | Result = Context.IntTy; |
1627 | 0 | declarator.setInvalidType(true); |
1628 | 0 | } |
1629 | 47.4k | break; |
1630 | 1.38M | } |
1631 | 551 | case DeclSpec::TST_underlyingType: |
1632 | 551 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
1633 | 551 | assert(!Result.isNull() && "Didn't get a type for __underlying_type?"); |
1634 | 0 | Result = S.BuildUnaryTransformType(Result, |
1635 | 551 | UnaryTransformType::EnumUnderlyingType, |
1636 | 551 | DS.getTypeSpecTypeLoc()); |
1637 | 551 | if (Result.isNull()) { |
1638 | 4 | Result = Context.IntTy; |
1639 | 4 | declarator.setInvalidType(true); |
1640 | 4 | } |
1641 | 551 | break; |
1642 | | |
1643 | 54.3k | case DeclSpec::TST_auto: |
1644 | 55.0k | case DeclSpec::TST_decltype_auto: { |
1645 | 55.0k | auto AutoKW = DS.getTypeSpecType() == DeclSpec::TST_decltype_auto |
1646 | 55.0k | ? AutoTypeKeyword::DecltypeAuto714 |
1647 | 55.0k | : AutoTypeKeyword::Auto54.3k ; |
1648 | | |
1649 | 55.0k | ConceptDecl *TypeConstraintConcept = nullptr; |
1650 | 55.0k | llvm::SmallVector<TemplateArgument, 8> TemplateArgs; |
1651 | 55.0k | if (DS.isConstrainedAuto()) { |
1652 | 202 | if (TemplateIdAnnotation *TemplateId = DS.getRepAsTemplateId()) { |
1653 | 200 | TypeConstraintConcept = |
1654 | 200 | cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl()); |
1655 | 200 | TemplateArgumentListInfo TemplateArgsInfo; |
1656 | 200 | TemplateArgsInfo.setLAngleLoc(TemplateId->LAngleLoc); |
1657 | 200 | TemplateArgsInfo.setRAngleLoc(TemplateId->RAngleLoc); |
1658 | 200 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
1659 | 200 | TemplateId->NumArgs); |
1660 | 200 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgsInfo); |
1661 | 200 | for (const auto &ArgLoc : TemplateArgsInfo.arguments()) |
1662 | 90 | TemplateArgs.push_back(ArgLoc.getArgument()); |
1663 | 200 | } else { |
1664 | 2 | declarator.setInvalidType(true); |
1665 | 2 | } |
1666 | 202 | } |
1667 | 55.0k | Result = S.Context.getAutoType(QualType(), AutoKW, |
1668 | 55.0k | /*IsDependent*/ false, /*IsPack=*/false, |
1669 | 55.0k | TypeConstraintConcept, TemplateArgs); |
1670 | 55.0k | break; |
1671 | 54.3k | } |
1672 | | |
1673 | 52 | case DeclSpec::TST_auto_type: |
1674 | 52 | Result = Context.getAutoType(QualType(), AutoTypeKeyword::GNUAutoType, false); |
1675 | 52 | break; |
1676 | | |
1677 | 62 | case DeclSpec::TST_unknown_anytype: |
1678 | 62 | Result = Context.UnknownAnyTy; |
1679 | 62 | break; |
1680 | | |
1681 | 2.96k | case DeclSpec::TST_atomic: |
1682 | 2.96k | Result = S.GetTypeFromParser(DS.getRepAsType()); |
1683 | 2.96k | assert(!Result.isNull() && "Didn't get a type for _Atomic?"); |
1684 | 0 | Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc()); |
1685 | 2.96k | if (Result.isNull()) { |
1686 | 11 | Result = Context.IntTy; |
1687 | 11 | declarator.setInvalidType(true); |
1688 | 11 | } |
1689 | 2.96k | break; |
1690 | | |
1691 | 0 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
1692 | 8.05k | case DeclSpec::TST_##ImgType##_t: \ |
1693 | 8.05k | switch (getImageAccess(DS.getAttributes())) { \ |
1694 | 2.13k | case OpenCLAccessAttr::Keyword_write_only: \ |
1695 | 2.13k | Result = Context.Id##WOTy; \ |
1696 | 2.13k | break; \ |
1697 | 2.21k | case OpenCLAccessAttr::Keyword_read_write: \ |
1698 | 2.21k | Result = Context.Id##RWTy; \ |
1699 | 2.21k | break; \ |
1700 | 3.70k | case OpenCLAccessAttr::Keyword_read_only: \ |
1701 | 3.70k | Result = Context.Id##ROTy; \ |
1702 | 3.70k | break; \ |
1703 | 0 | case OpenCLAccessAttr::SpellingNotCalculated: \ |
1704 | 0 | llvm_unreachable("Spelling not yet calculated"); \ |
1705 | 8.05k | } \ |
1706 | 8.05k | break; |
1707 | 2.96k | #include "clang/Basic/OpenCLImageTypes.def" |
1708 | | |
1709 | 1.94k | case DeclSpec::TST_error: |
1710 | 1.94k | Result = Context.IntTy; |
1711 | 1.94k | declarator.setInvalidType(true); |
1712 | 1.94k | break; |
1713 | 111M | } |
1714 | | |
1715 | | // FIXME: we want resulting declarations to be marked invalid, but claiming |
1716 | | // the type is invalid is too strong - e.g. it causes ActOnTypeName to return |
1717 | | // a null type. |
1718 | 111M | if (Result->containsErrors()) |
1719 | 101 | declarator.setInvalidType(); |
1720 | | |
1721 | 111M | if (S.getLangOpts().OpenCL) { |
1722 | 671k | const auto &OpenCLOptions = S.getOpenCLOptions(); |
1723 | 671k | bool IsOpenCLC30Compatible = |
1724 | 671k | S.getLangOpts().getOpenCLCompatibleVersion() == 300; |
1725 | | // OpenCL C v3.0 s6.3.3 - OpenCL image types require __opencl_c_images |
1726 | | // support. |
1727 | | // OpenCL C v3.0 s6.2.1 - OpenCL 3d image write types requires support |
1728 | | // for OpenCL C 2.0, or OpenCL C 3.0 or newer and the |
1729 | | // __opencl_c_3d_image_writes feature. OpenCL C v3.0 API s4.2 - For devices |
1730 | | // that support OpenCL 3.0, cl_khr_3d_image_writes must be returned when and |
1731 | | // only when the optional feature is supported |
1732 | 671k | if ((Result->isImageType() || Result->isSamplerT()663k ) && |
1733 | 671k | (10.1k IsOpenCLC30Compatible10.1k && |
1734 | 10.1k | !OpenCLOptions.isSupported("__opencl_c_images", S.getLangOpts())1.55k )) { |
1735 | 24 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_opencl_requires_extension) |
1736 | 24 | << 0 << Result << "__opencl_c_images"; |
1737 | 24 | declarator.setInvalidType(); |
1738 | 671k | } else if (Result->isOCLImage3dWOType() && |
1739 | 671k | !OpenCLOptions.isSupported("cl_khr_3d_image_writes", |
1740 | 287 | S.getLangOpts())) { |
1741 | 3 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_opencl_requires_extension) |
1742 | 3 | << 0 << Result |
1743 | 3 | << (IsOpenCLC30Compatible |
1744 | 3 | ? "cl_khr_3d_image_writes and __opencl_c_3d_image_writes"2 |
1745 | 3 | : "cl_khr_3d_image_writes"1 ); |
1746 | 3 | declarator.setInvalidType(); |
1747 | 3 | } |
1748 | 671k | } |
1749 | | |
1750 | 111M | bool IsFixedPointType = DS.getTypeSpecType() == DeclSpec::TST_accum || |
1751 | 111M | DS.getTypeSpecType() == DeclSpec::TST_fract111M ; |
1752 | | |
1753 | | // Only fixed point types can be saturated |
1754 | 111M | if (DS.isTypeSpecSat() && !IsFixedPointType556 ) |
1755 | 11 | S.Diag(DS.getTypeSpecSatLoc(), diag::err_invalid_saturation_spec) |
1756 | 11 | << DS.getSpecifierName(DS.getTypeSpecType(), |
1757 | 11 | Context.getPrintingPolicy()); |
1758 | | |
1759 | | // Handle complex types. |
1760 | 111M | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
1761 | 8.36k | if (S.getLangOpts().Freestanding) |
1762 | 54 | S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
1763 | 8.36k | Result = Context.getComplexType(Result); |
1764 | 111M | } else if (DS.isTypeAltiVecVector()) { |
1765 | 104k | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
1766 | 104k | assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); |
1767 | 0 | VectorType::VectorKind VecKind = VectorType::AltiVecVector; |
1768 | 104k | if (DS.isTypeAltiVecPixel()) |
1769 | 2.57k | VecKind = VectorType::AltiVecPixel; |
1770 | 101k | else if (DS.isTypeAltiVecBool()) |
1771 | 18.8k | VecKind = VectorType::AltiVecBool; |
1772 | 104k | Result = Context.getVectorType(Result, 128/typeSize, VecKind); |
1773 | 104k | } |
1774 | | |
1775 | | // FIXME: Imaginary. |
1776 | 111M | if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) |
1777 | 10 | S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); |
1778 | | |
1779 | | // Before we process any type attributes, synthesize a block literal |
1780 | | // function declarator if necessary. |
1781 | 111M | if (declarator.getContext() == DeclaratorContext::BlockLiteral) |
1782 | 3.33k | maybeSynthesizeBlockSignature(state, Result); |
1783 | | |
1784 | | // Apply any type attributes from the decl spec. This may cause the |
1785 | | // list of type attributes to be temporarily saved while the type |
1786 | | // attributes are pushed around. |
1787 | | // pipe attributes will be handled later ( at GetFullTypeForDeclarator ) |
1788 | 111M | if (!DS.isTypeSpecPipe()) { |
1789 | | // We also apply declaration attributes that "slide" to the decl spec. |
1790 | | // Ordering can be important for attributes. The decalaration attributes |
1791 | | // come syntactically before the decl spec attributes, so we process them |
1792 | | // in that order. |
1793 | 111M | ParsedAttributesView SlidingAttrs; |
1794 | 111M | for (ParsedAttr &AL : declarator.getDeclarationAttributes()) { |
1795 | 16.4k | if (AL.slidesFromDeclToDeclSpecLegacyBehavior()) { |
1796 | 31 | SlidingAttrs.addAtEnd(&AL); |
1797 | | |
1798 | | // For standard syntax attributes, which would normally appertain to the |
1799 | | // declaration here, suggest moving them to the type instead. But only |
1800 | | // do this for our own vendor attributes; moving other vendors' |
1801 | | // attributes might hurt portability. |
1802 | | // There's one special case that we need to deal with here: The |
1803 | | // `MatrixType` attribute may only be used in a typedef declaration. If |
1804 | | // it's being used anywhere else, don't output the warning as |
1805 | | // ProcessDeclAttributes() will output an error anyway. |
1806 | 31 | if (AL.isStandardAttributeSyntax() && AL.isClangScope() && |
1807 | 31 | !(AL.getKind() == ParsedAttr::AT_MatrixType && |
1808 | 31 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef3 )) { |
1809 | 30 | S.Diag(AL.getLoc(), diag::warn_type_attribute_deprecated_on_decl) |
1810 | 30 | << AL; |
1811 | 30 | } |
1812 | 31 | } |
1813 | 16.4k | } |
1814 | | // During this call to processTypeAttrs(), |
1815 | | // TypeProcessingState::getCurrentAttributes() will erroneously return a |
1816 | | // reference to the DeclSpec attributes, rather than the declaration |
1817 | | // attributes. However, this doesn't matter, as getCurrentAttributes() |
1818 | | // is only called when distributing attributes from one attribute list |
1819 | | // to another. Declaration attributes are always C++11 attributes, and these |
1820 | | // are never distributed. |
1821 | 111M | processTypeAttrs(state, Result, TAL_DeclSpec, SlidingAttrs); |
1822 | 111M | processTypeAttrs(state, Result, TAL_DeclSpec, DS.getAttributes()); |
1823 | 111M | } |
1824 | | |
1825 | | // Apply const/volatile/restrict qualifiers to T. |
1826 | 111M | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
1827 | | // Warn about CV qualifiers on function types. |
1828 | | // C99 6.7.3p8: |
1829 | | // If the specification of a function type includes any type qualifiers, |
1830 | | // the behavior is undefined. |
1831 | | // C++11 [dcl.fct]p7: |
1832 | | // The effect of a cv-qualifier-seq in a function declarator is not the |
1833 | | // same as adding cv-qualification on top of the function type. In the |
1834 | | // latter case, the cv-qualifiers are ignored. |
1835 | 5.11M | if (Result->isFunctionType()) { |
1836 | 19 | diagnoseAndRemoveTypeQualifiers( |
1837 | 19 | S, DS, TypeQuals, Result, DeclSpec::TQ_const | DeclSpec::TQ_volatile, |
1838 | 19 | S.getLangOpts().CPlusPlus |
1839 | 19 | ? diag::warn_typecheck_function_qualifiers_ignored15 |
1840 | 19 | : diag::warn_typecheck_function_qualifiers_unspecified4 ); |
1841 | | // No diagnostic for 'restrict' or '_Atomic' applied to a |
1842 | | // function type; we'll diagnose those later, in BuildQualifiedType. |
1843 | 19 | } |
1844 | | |
1845 | | // C++11 [dcl.ref]p1: |
1846 | | // Cv-qualified references are ill-formed except when the |
1847 | | // cv-qualifiers are introduced through the use of a typedef-name |
1848 | | // or decltype-specifier, in which case the cv-qualifiers are ignored. |
1849 | | // |
1850 | | // There don't appear to be any other contexts in which a cv-qualified |
1851 | | // reference type could be formed, so the 'ill-formed' clause here appears |
1852 | | // to never happen. |
1853 | 5.11M | if (TypeQuals && Result->isReferenceType()5.11M ) { |
1854 | 60 | diagnoseAndRemoveTypeQualifiers( |
1855 | 60 | S, DS, TypeQuals, Result, |
1856 | 60 | DeclSpec::TQ_const | DeclSpec::TQ_volatile | DeclSpec::TQ_atomic, |
1857 | 60 | diag::warn_typecheck_reference_qualifiers); |
1858 | 60 | } |
1859 | | |
1860 | | // C90 6.5.3 constraints: "The same type qualifier shall not appear more |
1861 | | // than once in the same specifier-list or qualifier-list, either directly |
1862 | | // or via one or more typedefs." |
1863 | 5.11M | if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus2.72M |
1864 | 5.11M | && TypeQuals & Result.getCVRQualifiers()571 ) { |
1865 | 8 | if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()6 ) { |
1866 | 6 | S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec) |
1867 | 6 | << "const"; |
1868 | 6 | } |
1869 | | |
1870 | 8 | if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()2 ) { |
1871 | 2 | S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec) |
1872 | 2 | << "volatile"; |
1873 | 2 | } |
1874 | | |
1875 | | // C90 doesn't have restrict nor _Atomic, so it doesn't force us to |
1876 | | // produce a warning in this case. |
1877 | 8 | } |
1878 | | |
1879 | 5.11M | QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS); |
1880 | | |
1881 | | // If adding qualifiers fails, just use the unqualified type. |
1882 | 5.11M | if (Qualified.isNull()) |
1883 | 16 | declarator.setInvalidType(true); |
1884 | 5.11M | else |
1885 | 5.11M | Result = Qualified; |
1886 | 5.11M | } |
1887 | | |
1888 | 111M | assert(!Result.isNull() && "This function should not return a null type"); |
1889 | 0 | return Result; |
1890 | 111M | } |
1891 | | |
1892 | 142 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
1893 | 142 | if (Entity) |
1894 | 110 | return Entity.getAsString(); |
1895 | | |
1896 | 32 | return "type name"; |
1897 | 142 | } |
1898 | | |
1899 | 3.77k | static bool isDependentOrGNUAutoType(QualType T) { |
1900 | 3.77k | if (T->isDependentType()) |
1901 | 462 | return true; |
1902 | | |
1903 | 3.30k | const auto *AT = dyn_cast<AutoType>(T); |
1904 | 3.30k | return AT && AT->isGNUAutoType()3 ; |
1905 | 3.77k | } |
1906 | | |
1907 | | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
1908 | 5.84M | Qualifiers Qs, const DeclSpec *DS) { |
1909 | 5.84M | if (T.isNull()) |
1910 | 0 | return QualType(); |
1911 | | |
1912 | | // Ignore any attempt to form a cv-qualified reference. |
1913 | 5.84M | if (T->isReferenceType()) { |
1914 | 75 | Qs.removeConst(); |
1915 | 75 | Qs.removeVolatile(); |
1916 | 75 | } |
1917 | | |
1918 | | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
1919 | | // object or incomplete types shall not be restrict-qualified." |
1920 | 5.84M | if (Qs.hasRestrict()) { |
1921 | 70.9k | unsigned DiagID = 0; |
1922 | 70.9k | QualType ProblemTy; |
1923 | | |
1924 | 70.9k | if (T->isAnyPointerType() || T->isReferenceType()40 || |
1925 | 70.9k | T->isMemberPointerType()29 ) { |
1926 | 70.8k | QualType EltTy; |
1927 | 70.8k | if (T->isObjCObjectPointerType()) |
1928 | 6 | EltTy = T; |
1929 | 70.8k | else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>()) |
1930 | 2 | EltTy = PTy->getPointeeType(); |
1931 | 70.8k | else |
1932 | 70.8k | EltTy = T->getPointeeType(); |
1933 | | |
1934 | | // If we have a pointer or reference, the pointee must have an object |
1935 | | // incomplete type. |
1936 | 70.8k | if (!EltTy->isIncompleteOrObjectType()) { |
1937 | 2 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
1938 | 2 | ProblemTy = EltTy; |
1939 | 2 | } |
1940 | 70.8k | } else if (27 !isDependentOrGNUAutoType(T)27 ) { |
1941 | | // For an __auto_type variable, we may not have seen the initializer yet |
1942 | | // and so have no idea whether the underlying type is a pointer type or |
1943 | | // not. |
1944 | 20 | DiagID = diag::err_typecheck_invalid_restrict_not_pointer; |
1945 | 20 | ProblemTy = T; |
1946 | 20 | } |
1947 | | |
1948 | 70.9k | if (DiagID) { |
1949 | 22 | Diag(DS ? DS->getRestrictSpecLoc()15 : Loc7 , DiagID) << ProblemTy; |
1950 | 22 | Qs.removeRestrict(); |
1951 | 22 | } |
1952 | 70.9k | } |
1953 | | |
1954 | 5.84M | return Context.getQualifiedType(T, Qs); |
1955 | 5.84M | } |
1956 | | |
1957 | | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
1958 | 5.26M | unsigned CVRAU, const DeclSpec *DS) { |
1959 | 5.26M | if (T.isNull()) |
1960 | 6 | return QualType(); |
1961 | | |
1962 | | // Ignore any attempt to form a cv-qualified reference. |
1963 | 5.26M | if (T->isReferenceType()) |
1964 | 67 | CVRAU &= |
1965 | 67 | ~(DeclSpec::TQ_const | DeclSpec::TQ_volatile | DeclSpec::TQ_atomic); |
1966 | | |
1967 | | // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic and |
1968 | | // TQ_unaligned; |
1969 | 5.26M | unsigned CVR = CVRAU & ~(DeclSpec::TQ_atomic | DeclSpec::TQ_unaligned); |
1970 | | |
1971 | | // C11 6.7.3/5: |
1972 | | // If the same qualifier appears more than once in the same |
1973 | | // specifier-qualifier-list, either directly or via one or more typedefs, |
1974 | | // the behavior is the same as if it appeared only once. |
1975 | | // |
1976 | | // It's not specified what happens when the _Atomic qualifier is applied to |
1977 | | // a type specified with the _Atomic specifier, but we assume that this |
1978 | | // should be treated as if the _Atomic qualifier appeared multiple times. |
1979 | 5.26M | if (CVRAU & DeclSpec::TQ_atomic && !T->isAtomicType()163 ) { |
1980 | | // C11 6.7.3/5: |
1981 | | // If other qualifiers appear along with the _Atomic qualifier in a |
1982 | | // specifier-qualifier-list, the resulting type is the so-qualified |
1983 | | // atomic type. |
1984 | | // |
1985 | | // Don't need to worry about array types here, since _Atomic can't be |
1986 | | // applied to such types. |
1987 | 161 | SplitQualType Split = T.getSplitUnqualifiedType(); |
1988 | 161 | T = BuildAtomicType(QualType(Split.Ty, 0), |
1989 | 161 | DS ? DS->getAtomicSpecLoc()145 : Loc16 ); |
1990 | 161 | if (T.isNull()) |
1991 | 16 | return T; |
1992 | 145 | Split.Quals.addCVRQualifiers(CVR); |
1993 | 145 | return BuildQualifiedType(T, Loc, Split.Quals); |
1994 | 161 | } |
1995 | | |
1996 | 5.26M | Qualifiers Q = Qualifiers::fromCVRMask(CVR); |
1997 | 5.26M | Q.setUnaligned(CVRAU & DeclSpec::TQ_unaligned); |
1998 | 5.26M | return BuildQualifiedType(T, Loc, Q, DS); |
1999 | 5.26M | } |
2000 | | |
2001 | | /// Build a paren type including \p T. |
2002 | 325k | QualType Sema::BuildParenType(QualType T) { |
2003 | 325k | return Context.getParenType(T); |
2004 | 325k | } |
2005 | | |
2006 | | /// Given that we're building a pointer or reference to the given |
2007 | | static QualType inferARCLifetimeForPointee(Sema &S, QualType type, |
2008 | | SourceLocation loc, |
2009 | 47.3k | bool isReference) { |
2010 | | // Bail out if retention is unrequired or already specified. |
2011 | 47.3k | if (!type->isObjCLifetimeType() || |
2012 | 47.3k | type.getObjCLifetime() != Qualifiers::OCL_None1.57k ) |
2013 | 47.2k | return type; |
2014 | | |
2015 | 24 | Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None; |
2016 | | |
2017 | | // If the object type is const-qualified, we can safely use |
2018 | | // __unsafe_unretained. This is safe (because there are no read |
2019 | | // barriers), and it'll be safe to coerce anything but __weak* to |
2020 | | // the resulting type. |
2021 | 24 | if (type.isConstQualified()) { |
2022 | 2 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
2023 | | |
2024 | | // Otherwise, check whether the static type does not require |
2025 | | // retaining. This currently only triggers for Class (possibly |
2026 | | // protocol-qualifed, and arrays thereof). |
2027 | 22 | } else if (type->isObjCARCImplicitlyUnretainedType()) { |
2028 | 3 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
2029 | | |
2030 | | // If we are in an unevaluated context, like sizeof, skip adding a |
2031 | | // qualification. |
2032 | 19 | } else if (S.isUnevaluatedContext()) { |
2033 | 4 | return type; |
2034 | | |
2035 | | // If that failed, give an error and recover using __strong. __strong |
2036 | | // is the option most likely to prevent spurious second-order diagnostics, |
2037 | | // like when binding a reference to a field. |
2038 | 15 | } else { |
2039 | | // These types can show up in private ivars in system headers, so |
2040 | | // we need this to not be an error in those cases. Instead we |
2041 | | // want to delay. |
2042 | 15 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
2043 | 14 | S.DelayedDiagnostics.add( |
2044 | 14 | sema::DelayedDiagnostic::makeForbiddenType(loc, |
2045 | 14 | diag::err_arc_indirect_no_ownership, type, isReference)); |
2046 | 14 | } else { |
2047 | 1 | S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference; |
2048 | 1 | } |
2049 | 15 | implicitLifetime = Qualifiers::OCL_Strong; |
2050 | 15 | } |
2051 | 20 | assert(implicitLifetime && "didn't infer any lifetime!"); |
2052 | | |
2053 | 0 | Qualifiers qs; |
2054 | 20 | qs.addObjCLifetime(implicitLifetime); |
2055 | 20 | return S.Context.getQualifiedType(type, qs); |
2056 | 24 | } |
2057 | | |
2058 | 80 | static std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){ |
2059 | 80 | std::string Quals = FnTy->getMethodQuals().getAsString(); |
2060 | | |
2061 | 80 | switch (FnTy->getRefQualifier()) { |
2062 | 43 | case RQ_None: |
2063 | 43 | break; |
2064 | | |
2065 | 17 | case RQ_LValue: |
2066 | 17 | if (!Quals.empty()) |
2067 | 0 | Quals += ' '; |
2068 | 17 | Quals += '&'; |
2069 | 17 | break; |
2070 | | |
2071 | 20 | case RQ_RValue: |
2072 | 20 | if (!Quals.empty()) |
2073 | 2 | Quals += ' '; |
2074 | 20 | Quals += "&&"; |
2075 | 20 | break; |
2076 | 80 | } |
2077 | | |
2078 | 80 | return Quals; |
2079 | 80 | } |
2080 | | |
2081 | | namespace { |
2082 | | /// Kinds of declarator that cannot contain a qualified function type. |
2083 | | /// |
2084 | | /// C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: |
2085 | | /// a function type with a cv-qualifier or a ref-qualifier can only appear |
2086 | | /// at the topmost level of a type. |
2087 | | /// |
2088 | | /// Parens and member pointers are permitted. We don't diagnose array and |
2089 | | /// function declarators, because they don't allow function types at all. |
2090 | | /// |
2091 | | /// The values of this enum are used in diagnostics. |
2092 | | enum QualifiedFunctionKind { QFK_BlockPointer, QFK_Pointer, QFK_Reference }; |
2093 | | } // end anonymous namespace |
2094 | | |
2095 | | /// Check whether the type T is a qualified function type, and if it is, |
2096 | | /// diagnose that it cannot be contained within the given kind of declarator. |
2097 | | static bool checkQualifiedFunction(Sema &S, QualType T, SourceLocation Loc, |
2098 | 9.88M | QualifiedFunctionKind QFK) { |
2099 | | // Does T refer to a function type with a cv-qualifier or a ref-qualifier? |
2100 | 9.88M | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); |
2101 | 9.88M | if (!FPT || |
2102 | 9.88M | (279k FPT->getMethodQuals().empty()279k && FPT->getRefQualifier() == RQ_None279k )) |
2103 | 9.88M | return false; |
2104 | | |
2105 | 24 | S.Diag(Loc, diag::err_compound_qualified_function_type) |
2106 | 24 | << QFK << isa<FunctionType>(T.IgnoreParens()) << T |
2107 | 24 | << getFunctionQualifiersAsString(FPT); |
2108 | 24 | return true; |
2109 | 9.88M | } |
2110 | | |
2111 | 5.01k | bool Sema::CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc) { |
2112 | 5.01k | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); |
2113 | 5.01k | if (!FPT || |
2114 | 5.01k | (6 FPT->getMethodQuals().empty()6 && FPT->getRefQualifier() == RQ_None3 )) |
2115 | 5.00k | return false; |
2116 | | |
2117 | 6 | Diag(Loc, diag::err_qualified_function_typeid) |
2118 | 6 | << T << getFunctionQualifiersAsString(FPT); |
2119 | 6 | return true; |
2120 | 5.01k | } |
2121 | | |
2122 | | // Helper to deduce addr space of a pointee type in OpenCL mode. |
2123 | 41.0k | static QualType deduceOpenCLPointeeAddrSpace(Sema &S, QualType PointeeType) { |
2124 | 41.0k | if (!PointeeType->isUndeducedAutoType() && !PointeeType->isDependentType()41.0k && |
2125 | 41.0k | !PointeeType->isSamplerT()41.0k && |
2126 | 41.0k | !PointeeType.hasAddressSpace()41.0k ) |
2127 | 7.57k | PointeeType = S.getASTContext().getAddrSpaceQualType( |
2128 | 7.57k | PointeeType, S.getASTContext().getDefaultOpenCLPointeeAddrSpace()); |
2129 | 41.0k | return PointeeType; |
2130 | 41.0k | } |
2131 | | |
2132 | | /// Build a pointer type. |
2133 | | /// |
2134 | | /// \param T The type to which we'll be building a pointer. |
2135 | | /// |
2136 | | /// \param Loc The location of the entity whose type involves this |
2137 | | /// pointer type or, if there is no such entity, the location of the |
2138 | | /// type that will have pointer type. |
2139 | | /// |
2140 | | /// \param Entity The name of the entity that involves the pointer |
2141 | | /// type, if known. |
2142 | | /// |
2143 | | /// \returns A suitable pointer type, if there are no |
2144 | | /// errors. Otherwise, returns a NULL type. |
2145 | | QualType Sema::BuildPointerType(QualType T, |
2146 | 7.12M | SourceLocation Loc, DeclarationName Entity) { |
2147 | 7.12M | if (T->isReferenceType()) { |
2148 | | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
2149 | 57 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
2150 | 57 | << getPrintableNameForEntity(Entity) << T; |
2151 | 57 | return QualType(); |
2152 | 57 | } |
2153 | | |
2154 | 7.12M | if (T->isFunctionType() && getLangOpts().OpenCL211k && |
2155 | 7.12M | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", |
2156 | 11 | getLangOpts())) { |
2157 | 7 | Diag(Loc, diag::err_opencl_function_pointer) << /*pointer*/ 0; |
2158 | 7 | return QualType(); |
2159 | 7 | } |
2160 | | |
2161 | 7.12M | if (getLangOpts().HLSL) { |
2162 | 6 | Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0; |
2163 | 6 | return QualType(); |
2164 | 6 | } |
2165 | | |
2166 | 7.12M | if (checkQualifiedFunction(*this, T, Loc, QFK_Pointer)) |
2167 | 12 | return QualType(); |
2168 | | |
2169 | 7.12M | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); |
2170 | | |
2171 | | // In ARC, it is forbidden to build pointers to unqualified pointers. |
2172 | 7.12M | if (getLangOpts().ObjCAutoRefCount) |
2173 | 45.5k | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false); |
2174 | | |
2175 | 7.12M | if (getLangOpts().OpenCL) |
2176 | 40.8k | T = deduceOpenCLPointeeAddrSpace(*this, T); |
2177 | | |
2178 | | // Build the pointer type. |
2179 | 7.12M | return Context.getPointerType(T); |
2180 | 7.12M | } |
2181 | | |
2182 | | /// Build a reference type. |
2183 | | /// |
2184 | | /// \param T The type to which we'll be building a reference. |
2185 | | /// |
2186 | | /// \param Loc The location of the entity whose type involves this |
2187 | | /// reference type or, if there is no such entity, the location of the |
2188 | | /// type that will have reference type. |
2189 | | /// |
2190 | | /// \param Entity The name of the entity that involves the reference |
2191 | | /// type, if known. |
2192 | | /// |
2193 | | /// \returns A suitable reference type, if there are no |
2194 | | /// errors. Otherwise, returns a NULL type. |
2195 | | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
2196 | | SourceLocation Loc, |
2197 | 2.69M | DeclarationName Entity) { |
2198 | 2.69M | assert(Context.getCanonicalType(T) != Context.OverloadTy && |
2199 | 2.69M | "Unresolved overloaded function type"); |
2200 | | |
2201 | | // C++0x [dcl.ref]p6: |
2202 | | // If a typedef (7.1.3), a type template-parameter (14.3.1), or a |
2203 | | // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a |
2204 | | // type T, an attempt to create the type "lvalue reference to cv TR" creates |
2205 | | // the type "lvalue reference to T", while an attempt to create the type |
2206 | | // "rvalue reference to cv TR" creates the type TR. |
2207 | 2.69M | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>()620k ; |
2208 | | |
2209 | | // C++ [dcl.ref]p4: There shall be no references to references. |
2210 | | // |
2211 | | // According to C++ DR 106, references to references are only |
2212 | | // diagnosed when they are written directly (e.g., "int & &"), |
2213 | | // but not when they happen via a typedef: |
2214 | | // |
2215 | | // typedef int& intref; |
2216 | | // typedef intref& intref2; |
2217 | | // |
2218 | | // Parser::ParseDeclaratorInternal diagnoses the case where |
2219 | | // references are written directly; here, we handle the |
2220 | | // collapsing of references-to-references as described in C++0x. |
2221 | | // DR 106 and 540 introduce reference-collapsing into C++98/03. |
2222 | | |
2223 | | // C++ [dcl.ref]p1: |
2224 | | // A declarator that specifies the type "reference to cv void" |
2225 | | // is ill-formed. |
2226 | 2.69M | if (T->isVoidType()) { |
2227 | 473 | Diag(Loc, diag::err_reference_to_void); |
2228 | 473 | return QualType(); |
2229 | 473 | } |
2230 | | |
2231 | 2.69M | if (getLangOpts().HLSL) { |
2232 | 5 | Diag(Loc, diag::err_hlsl_pointers_unsupported) << 1; |
2233 | 5 | return QualType(); |
2234 | 5 | } |
2235 | | |
2236 | 2.69M | if (checkQualifiedFunction(*this, T, Loc, QFK_Reference)) |
2237 | 12 | return QualType(); |
2238 | | |
2239 | 2.69M | if (T->isFunctionType() && getLangOpts().OpenCL6.52k && |
2240 | 2.69M | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", |
2241 | 10 | getLangOpts())) { |
2242 | 5 | Diag(Loc, diag::err_opencl_function_pointer) << /*reference*/ 1; |
2243 | 5 | return QualType(); |
2244 | 5 | } |
2245 | | |
2246 | | // In ARC, it is forbidden to build references to unqualified pointers. |
2247 | 2.69M | if (getLangOpts().ObjCAutoRefCount) |
2248 | 1.80k | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true); |
2249 | | |
2250 | 2.69M | if (getLangOpts().OpenCL) |
2251 | 108 | T = deduceOpenCLPointeeAddrSpace(*this, T); |
2252 | | |
2253 | | // Handle restrict on references. |
2254 | 2.69M | if (LValueRef) |
2255 | 2.12M | return Context.getLValueReferenceType(T, SpelledAsLValue); |
2256 | 572k | return Context.getRValueReferenceType(T); |
2257 | 2.69M | } |
2258 | | |
2259 | | /// Build a Read-only Pipe type. |
2260 | | /// |
2261 | | /// \param T The type to which we'll be building a Pipe. |
2262 | | /// |
2263 | | /// \param Loc We do not use it for now. |
2264 | | /// |
2265 | | /// \returns A suitable pipe type, if there are no errors. Otherwise, returns a |
2266 | | /// NULL type. |
2267 | 249 | QualType Sema::BuildReadPipeType(QualType T, SourceLocation Loc) { |
2268 | 249 | return Context.getReadPipeType(T); |
2269 | 249 | } |
2270 | | |
2271 | | /// Build a Write-only Pipe type. |
2272 | | /// |
2273 | | /// \param T The type to which we'll be building a Pipe. |
2274 | | /// |
2275 | | /// \param Loc We do not use it for now. |
2276 | | /// |
2277 | | /// \returns A suitable pipe type, if there are no errors. Otherwise, returns a |
2278 | | /// NULL type. |
2279 | 0 | QualType Sema::BuildWritePipeType(QualType T, SourceLocation Loc) { |
2280 | 0 | return Context.getWritePipeType(T); |
2281 | 0 | } |
2282 | | |
2283 | | /// Build a bit-precise integer type. |
2284 | | /// |
2285 | | /// \param IsUnsigned Boolean representing the signedness of the type. |
2286 | | /// |
2287 | | /// \param BitWidth Size of this int type in bits, or an expression representing |
2288 | | /// that. |
2289 | | /// |
2290 | | /// \param Loc Location of the keyword. |
2291 | | QualType Sema::BuildBitIntType(bool IsUnsigned, Expr *BitWidth, |
2292 | 1.23k | SourceLocation Loc) { |
2293 | 1.23k | if (BitWidth->isInstantiationDependent()) |
2294 | 21 | return Context.getDependentBitIntType(IsUnsigned, BitWidth); |
2295 | | |
2296 | 1.21k | llvm::APSInt Bits(32); |
2297 | 1.21k | ExprResult ICE = |
2298 | 1.21k | VerifyIntegerConstantExpression(BitWidth, &Bits, /*FIXME*/ AllowFold); |
2299 | | |
2300 | 1.21k | if (ICE.isInvalid()) |
2301 | 2 | return QualType(); |
2302 | | |
2303 | 1.21k | size_t NumBits = Bits.getZExtValue(); |
2304 | 1.21k | if (!IsUnsigned && NumBits < 21.02k ) { |
2305 | 5 | Diag(Loc, diag::err_bit_int_bad_size) << 0; |
2306 | 5 | return QualType(); |
2307 | 5 | } |
2308 | | |
2309 | 1.20k | if (IsUnsigned && NumBits < 1185 ) { |
2310 | 2 | Diag(Loc, diag::err_bit_int_bad_size) << 1; |
2311 | 2 | return QualType(); |
2312 | 2 | } |
2313 | | |
2314 | 1.20k | const TargetInfo &TI = getASTContext().getTargetInfo(); |
2315 | 1.20k | if (NumBits > TI.getMaxBitIntWidth()) { |
2316 | 7 | Diag(Loc, diag::err_bit_int_max_size) |
2317 | 7 | << IsUnsigned << static_cast<uint64_t>(TI.getMaxBitIntWidth()); |
2318 | 7 | return QualType(); |
2319 | 7 | } |
2320 | | |
2321 | 1.20k | return Context.getBitIntType(IsUnsigned, NumBits); |
2322 | 1.20k | } |
2323 | | |
2324 | | /// Check whether the specified array bound can be evaluated using the relevant |
2325 | | /// language rules. If so, returns the possibly-converted expression and sets |
2326 | | /// SizeVal to the size. If not, but the expression might be a VLA bound, |
2327 | | /// returns ExprResult(). Otherwise, produces a diagnostic and returns |
2328 | | /// ExprError(). |
2329 | | static ExprResult checkArraySize(Sema &S, Expr *&ArraySize, |
2330 | | llvm::APSInt &SizeVal, unsigned VLADiag, |
2331 | 266k | bool VLAIsError) { |
2332 | 266k | if (S.getLangOpts().CPlusPlus14 && |
2333 | 266k | (29.5k VLAIsError29.5k || |
2334 | 29.5k | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()28.8k )) { |
2335 | | // C++14 [dcl.array]p1: |
2336 | | // The constant-expression shall be a converted constant expression of |
2337 | | // type std::size_t. |
2338 | | // |
2339 | | // Don't apply this rule if we might be forming a VLA: in that case, we |
2340 | | // allow non-constant expressions and constant-folding. We only need to use |
2341 | | // the converted constant expression rules (to properly convert the source) |
2342 | | // when the source expression is of class type. |
2343 | 731 | return S.CheckConvertedConstantExpression( |
2344 | 731 | ArraySize, S.Context.getSizeType(), SizeVal, Sema::CCEK_ArrayBound); |
2345 | 731 | } |
2346 | | |
2347 | | // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode |
2348 | | // (like gnu99, but not c99) accept any evaluatable value as an extension. |
2349 | 265k | class VLADiagnoser : public Sema::VerifyICEDiagnoser { |
2350 | 265k | public: |
2351 | 265k | unsigned VLADiag; |
2352 | 265k | bool VLAIsError; |
2353 | 265k | bool IsVLA = false; |
2354 | | |
2355 | 265k | VLADiagnoser(unsigned VLADiag, bool VLAIsError) |
2356 | 265k | : VLADiag(VLADiag), VLAIsError(VLAIsError) {} |
2357 | | |
2358 | 265k | Sema::SemaDiagnosticBuilder diagnoseNotICEType(Sema &S, SourceLocation Loc, |
2359 | 265k | QualType T) override { |
2360 | 1 | return S.Diag(Loc, diag::err_array_size_non_int) << T; |
2361 | 1 | } |
2362 | | |
2363 | 265k | Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, |
2364 | 265k | SourceLocation Loc) override { |
2365 | 3.56k | IsVLA = !VLAIsError; |
2366 | 3.56k | return S.Diag(Loc, VLADiag); |
2367 | 3.56k | } |
2368 | | |
2369 | 265k | Sema::SemaDiagnosticBuilder diagnoseFold(Sema &S, |
2370 | 265k | SourceLocation Loc) override { |
2371 | 0 | return S.Diag(Loc, diag::ext_vla_folded_to_constant); |
2372 | 0 | } |
2373 | 265k | } Diagnoser(VLADiag, VLAIsError); |
2374 | | |
2375 | 265k | ExprResult R = |
2376 | 265k | S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser); |
2377 | 265k | if (Diagnoser.IsVLA) |
2378 | 3.55k | return ExprResult(); |
2379 | 262k | return R; |
2380 | 265k | } |
2381 | | |
2382 | | /// Build an array type. |
2383 | | /// |
2384 | | /// \param T The type of each element in the array. |
2385 | | /// |
2386 | | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
2387 | | /// |
2388 | | /// \param ArraySize Expression describing the size of the array. |
2389 | | /// |
2390 | | /// \param Brackets The range from the opening '[' to the closing ']'. |
2391 | | /// |
2392 | | /// \param Entity The name of the entity that involves the array |
2393 | | /// type, if known. |
2394 | | /// |
2395 | | /// \returns A suitable array type, if there are no errors. Otherwise, |
2396 | | /// returns a NULL type. |
2397 | | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
2398 | | Expr *ArraySize, unsigned Quals, |
2399 | 341k | SourceRange Brackets, DeclarationName Entity) { |
2400 | | |
2401 | 341k | SourceLocation Loc = Brackets.getBegin(); |
2402 | 341k | if (getLangOpts().CPlusPlus) { |
2403 | | // C++ [dcl.array]p1: |
2404 | | // T is called the array element type; this type shall not be a reference |
2405 | | // type, the (possibly cv-qualified) type void, a function type or an |
2406 | | // abstract class type. |
2407 | | // |
2408 | | // C++ [dcl.array]p3: |
2409 | | // When several "array of" specifications are adjacent, [...] only the |
2410 | | // first of the constant expressions that specify the bounds of the arrays |
2411 | | // may be omitted. |
2412 | | // |
2413 | | // Note: function types are handled in the common path with C. |
2414 | 166k | if (T->isReferenceType()) { |
2415 | 9 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
2416 | 9 | << getPrintableNameForEntity(Entity) << T; |
2417 | 9 | return QualType(); |
2418 | 9 | } |
2419 | | |
2420 | 166k | if (T->isVoidType() || T->isIncompleteArrayType()166k ) { |
2421 | 2 | Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 0 << T; |
2422 | 2 | return QualType(); |
2423 | 2 | } |
2424 | | |
2425 | 166k | if (RequireNonAbstractType(Brackets.getBegin(), T, |
2426 | 166k | diag::err_array_of_abstract_type)) |
2427 | 20 | return QualType(); |
2428 | | |
2429 | | // Mentioning a member pointer type for an array type causes us to lock in |
2430 | | // an inheritance model, even if it's inside an unused typedef. |
2431 | 166k | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) |
2432 | 881 | if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) |
2433 | 0 | if (!MPTy->getClass()->isDependentType()) |
2434 | 0 | (void)isCompleteType(Loc, T); |
2435 | | |
2436 | 174k | } else { |
2437 | | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
2438 | | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
2439 | 174k | if (RequireCompleteSizedType(Loc, T, |
2440 | 174k | diag::err_array_incomplete_or_sizeless_type)) |
2441 | 74 | return QualType(); |
2442 | 174k | } |
2443 | | |
2444 | 341k | if (T->isSizelessType()) { |
2445 | 19 | Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 1 << T; |
2446 | 19 | return QualType(); |
2447 | 19 | } |
2448 | | |
2449 | 341k | if (T->isFunctionType()) { |
2450 | 14 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
2451 | 14 | << getPrintableNameForEntity(Entity) << T; |
2452 | 14 | return QualType(); |
2453 | 14 | } |
2454 | | |
2455 | 341k | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
2456 | | // If the element type is a struct or union that contains a variadic |
2457 | | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
2458 | 31.2k | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
2459 | 6 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
2460 | 310k | } else if (T->isObjCObjectType()) { |
2461 | 1 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
2462 | 1 | return QualType(); |
2463 | 1 | } |
2464 | | |
2465 | | // Do placeholder conversions on the array size expression. |
2466 | 341k | if (ArraySize && ArraySize->hasPlaceholderType()287k ) { |
2467 | 2 | ExprResult Result = CheckPlaceholderExpr(ArraySize); |
2468 | 2 | if (Result.isInvalid()) return QualType()0 ; |
2469 | 2 | ArraySize = Result.get(); |
2470 | 2 | } |
2471 | | |
2472 | | // Do lvalue-to-rvalue conversions on the array size expression. |
2473 | 341k | if (ArraySize && !ArraySize->isPRValue()287k ) { |
2474 | 6.95k | ExprResult Result = DefaultLvalueConversion(ArraySize); |
2475 | 6.95k | if (Result.isInvalid()) |
2476 | 0 | return QualType(); |
2477 | | |
2478 | 6.95k | ArraySize = Result.get(); |
2479 | 6.95k | } |
2480 | | |
2481 | | // C99 6.7.5.2p1: The size expression shall have integer type. |
2482 | | // C++11 allows contextual conversions to such types. |
2483 | 341k | if (!getLangOpts().CPlusPlus11 && |
2484 | 341k | ArraySize178k && !ArraySize->isTypeDependent()151k && |
2485 | 341k | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()151k ) { |
2486 | 4 | Diag(ArraySize->getBeginLoc(), diag::err_array_size_non_int) |
2487 | 4 | << ArraySize->getType() << ArraySize->getSourceRange(); |
2488 | 4 | return QualType(); |
2489 | 4 | } |
2490 | | |
2491 | | // VLAs always produce at least a -Wvla diagnostic, sometimes an error. |
2492 | 341k | unsigned VLADiag; |
2493 | 341k | bool VLAIsError; |
2494 | 341k | if (getLangOpts().OpenCL) { |
2495 | | // OpenCL v1.2 s6.9.d: variable length arrays are not supported. |
2496 | 684 | VLADiag = diag::err_opencl_vla; |
2497 | 684 | VLAIsError = true; |
2498 | 340k | } else if (getLangOpts().C99) { |
2499 | 173k | VLADiag = diag::warn_vla_used; |
2500 | 173k | VLAIsError = false; |
2501 | 173k | } else if (166k isSFINAEContext()166k ) { |
2502 | 1.18k | VLADiag = diag::err_vla_in_sfinae; |
2503 | 1.18k | VLAIsError = true; |
2504 | 165k | } else if (getLangOpts().OpenMP && isInOpenMPTaskUntiedContext()28.6k ) { |
2505 | 10 | VLADiag = diag::err_openmp_vla_in_task_untied; |
2506 | 10 | VLAIsError = true; |
2507 | 165k | } else { |
2508 | 165k | VLADiag = diag::ext_vla; |
2509 | 165k | VLAIsError = false; |
2510 | 165k | } |
2511 | | |
2512 | 341k | llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); |
2513 | 341k | if (!ArraySize) { |
2514 | 54.2k | if (ASM == ArrayType::Star) { |
2515 | 27 | Diag(Loc, VLADiag); |
2516 | 27 | if (VLAIsError) |
2517 | 0 | return QualType(); |
2518 | | |
2519 | 27 | T = Context.getVariableArrayType(T, nullptr, ASM, Quals, Brackets); |
2520 | 54.1k | } else { |
2521 | 54.1k | T = Context.getIncompleteArrayType(T, ASM, Quals); |
2522 | 54.1k | } |
2523 | 287k | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()286k ) { |
2524 | 20.8k | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
2525 | 266k | } else { |
2526 | 266k | ExprResult R = |
2527 | 266k | checkArraySize(*this, ArraySize, ConstVal, VLADiag, VLAIsError); |
2528 | 266k | if (R.isInvalid()) |
2529 | 24 | return QualType(); |
2530 | | |
2531 | 266k | if (!R.isUsable()) { |
2532 | | // C99: an array with a non-ICE size is a VLA. We accept any expression |
2533 | | // that we can fold to a non-zero positive value as a non-VLA as an |
2534 | | // extension. |
2535 | 3.55k | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
2536 | 262k | } else if (!T->isDependentType() && !T->isIncompleteType()258k && |
2537 | 262k | !T->isConstantSizeType()258k ) { |
2538 | | // C99: an array with an element type that has a non-constant-size is a |
2539 | | // VLA. |
2540 | | // FIXME: Add a note to explain why this isn't a VLA. |
2541 | 1.15k | Diag(Loc, VLADiag); |
2542 | 1.15k | if (VLAIsError) |
2543 | 0 | return QualType(); |
2544 | 1.15k | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
2545 | 261k | } else { |
2546 | | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
2547 | | // have a value greater than zero. |
2548 | | // In C++, this follows from narrowing conversions being disallowed. |
2549 | 261k | if (ConstVal.isSigned() && ConstVal.isNegative()252k ) { |
2550 | 153 | if (Entity) |
2551 | 51 | Diag(ArraySize->getBeginLoc(), diag::err_decl_negative_array_size) |
2552 | 51 | << getPrintableNameForEntity(Entity) |
2553 | 51 | << ArraySize->getSourceRange(); |
2554 | 102 | else |
2555 | 102 | Diag(ArraySize->getBeginLoc(), |
2556 | 102 | diag::err_typecheck_negative_array_size) |
2557 | 102 | << ArraySize->getSourceRange(); |
2558 | 153 | return QualType(); |
2559 | 153 | } |
2560 | 261k | if (ConstVal == 0) { |
2561 | | // GCC accepts zero sized static arrays. We allow them when |
2562 | | // we're not in a SFINAE context. |
2563 | 3.51k | Diag(ArraySize->getBeginLoc(), |
2564 | 3.51k | isSFINAEContext() ? diag::err_typecheck_zero_array_size3 |
2565 | 3.51k | : diag::ext_typecheck_zero_array_size3.51k ) |
2566 | 3.51k | << 0 << ArraySize->getSourceRange(); |
2567 | 3.51k | } |
2568 | | |
2569 | | // Is the array too large? |
2570 | 261k | unsigned ActiveSizeBits = |
2571 | 261k | (!T->isDependentType() && !T->isVariablyModifiedType()256k && |
2572 | 261k | !T->isIncompleteType()256k && !T->isUndeducedType()256k ) |
2573 | 261k | ? ConstantArrayType::getNumAddressingBits(Context, T, ConstVal)256k |
2574 | 261k | : ConstVal.getActiveBits()4.83k ; |
2575 | 261k | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
2576 | 20 | Diag(ArraySize->getBeginLoc(), diag::err_array_too_large) |
2577 | 20 | << toString(ConstVal, 10) << ArraySize->getSourceRange(); |
2578 | 20 | return QualType(); |
2579 | 20 | } |
2580 | | |
2581 | 261k | T = Context.getConstantArrayType(T, ConstVal, ArraySize, ASM, Quals); |
2582 | 261k | } |
2583 | 266k | } |
2584 | | |
2585 | 341k | if (T->isVariableArrayType() && !Context.getTargetInfo().isVLASupported()4.73k ) { |
2586 | | // CUDA device code and some other targets don't support VLAs. |
2587 | 30 | targetDiag(Loc, (getLangOpts().CUDA && getLangOpts().CUDAIsDevice6 ) |
2588 | 30 | ? diag::err_cuda_vla6 |
2589 | 30 | : diag::err_vla_unsupported24 ) |
2590 | 30 | << ((getLangOpts().CUDA && getLangOpts().CUDAIsDevice6 ) |
2591 | 30 | ? CurrentCUDATarget()6 |
2592 | 30 | : CFT_InvalidTarget24 ); |
2593 | 30 | } |
2594 | | |
2595 | | // If this is not C99, diagnose array size modifiers on non-VLAs. |
2596 | 341k | if (!getLangOpts().C99 && !T->isVariableArrayType()166k && |
2597 | 341k | (162k ASM != ArrayType::Normal162k || Quals != 0162k )) { |
2598 | 6 | Diag(Loc, getLangOpts().CPlusPlus ? diag::err_c99_array_usage_cxx3 |
2599 | 6 | : diag::ext_c99_array_usage3 ) |
2600 | 6 | << ASM; |
2601 | 6 | } |
2602 | | |
2603 | | // OpenCL v2.0 s6.12.5 - Arrays of blocks are not supported. |
2604 | | // OpenCL v2.0 s6.16.13.1 - Arrays of pipe type are not supported. |
2605 | | // OpenCL v2.0 s6.9.b - Arrays of image/sampler type are not supported. |
2606 | 341k | if (getLangOpts().OpenCL) { |
2607 | 677 | const QualType ArrType = Context.getBaseElementType(T); |
2608 | 677 | if (ArrType->isBlockPointerType() || ArrType->isPipeType()675 || |
2609 | 677 | ArrType->isSamplerT()675 || ArrType->isImageType()669 ) { |
2610 | 12 | Diag(Loc, diag::err_opencl_invalid_type_array) << ArrType; |
2611 | 12 | return QualType(); |
2612 | 12 | } |
2613 | 677 | } |
2614 | | |
2615 | 341k | return T; |
2616 | 341k | } |
2617 | | |
2618 | | QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr, |
2619 | 30.4k | SourceLocation AttrLoc) { |
2620 | | // The base type must be integer (not Boolean or enumeration) or float, and |
2621 | | // can't already be a vector. |
2622 | 30.4k | if ((!CurType->isDependentType() && |
2623 | 30.4k | (30.4k !CurType->isBuiltinType()30.4k || CurType->isBooleanType()30.4k || |
2624 | 30.4k | (30.3k !CurType->isIntegerType()30.3k && !CurType->isRealFloatingType()10.2k ))) || |
2625 | 30.4k | CurType->isArrayType()30.4k ) { |
2626 | 41 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << CurType; |
2627 | 41 | return QualType(); |
2628 | 41 | } |
2629 | | |
2630 | 30.4k | if (SizeExpr->isTypeDependent() || SizeExpr->isValueDependent()) |
2631 | 30 | return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, |
2632 | 30 | VectorType::GenericVector); |
2633 | | |
2634 | 30.3k | Optional<llvm::APSInt> VecSize = SizeExpr->getIntegerConstantExpr(Context); |
2635 | 30.3k | if (!VecSize) { |
2636 | 0 | Diag(AttrLoc, diag::err_attribute_argument_type) |
2637 | 0 | << "vector_size" << AANT_ArgumentIntegerConstant |
2638 | 0 | << SizeExpr->getSourceRange(); |
2639 | 0 | return QualType(); |
2640 | 0 | } |
2641 | | |
2642 | 30.3k | if (CurType->isDependentType()) |
2643 | 15 | return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, |
2644 | 15 | VectorType::GenericVector); |
2645 | | |
2646 | | // vecSize is specified in bytes - convert to bits. |
2647 | 30.3k | if (!VecSize->isIntN(61)) { |
2648 | | // Bit size will overflow uint64. |
2649 | 12 | Diag(AttrLoc, diag::err_attribute_size_too_large) |
2650 | 12 | << SizeExpr->getSourceRange() << "vector"; |
2651 | 12 | return QualType(); |
2652 | 12 | } |
2653 | 30.3k | uint64_t VectorSizeBits = VecSize->getZExtValue() * 8; |
2654 | 30.3k | unsigned TypeSize = static_cast<unsigned>(Context.getTypeSize(CurType)); |
2655 | | |
2656 | 30.3k | if (VectorSizeBits == 0) { |
2657 | 11 | Diag(AttrLoc, diag::err_attribute_zero_size) |
2658 | 11 | << SizeExpr->getSourceRange() << "vector"; |
2659 | 11 | return QualType(); |
2660 | 11 | } |
2661 | | |
2662 | 30.3k | if (!TypeSize || VectorSizeBits % TypeSize30.3k ) { |
2663 | 11 | Diag(AttrLoc, diag::err_attribute_invalid_size) |
2664 | 11 | << SizeExpr->getSourceRange(); |
2665 | 11 | return QualType(); |
2666 | 11 | } |
2667 | | |
2668 | 30.3k | if (VectorSizeBits / TypeSize > std::numeric_limits<uint32_t>::max()) { |
2669 | 21 | Diag(AttrLoc, diag::err_attribute_size_too_large) |
2670 | 21 | << SizeExpr->getSourceRange() << "vector"; |
2671 | 21 | return QualType(); |
2672 | 21 | } |
2673 | | |
2674 | 30.3k | return Context.getVectorType(CurType, VectorSizeBits / TypeSize, |
2675 | 30.3k | VectorType::GenericVector); |
2676 | 30.3k | } |
2677 | | |
2678 | | /// Build an ext-vector type. |
2679 | | /// |
2680 | | /// Run the required checks for the extended vector type. |
2681 | | QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, |
2682 | 4.87k | SourceLocation AttrLoc) { |
2683 | | // Unlike gcc's vector_size attribute, we do not allow vectors to be defined |
2684 | | // in conjunction with complex types (pointers, arrays, functions, etc.). |
2685 | | // |
2686 | | // Additionally, OpenCL prohibits vectors of booleans (they're considered a |
2687 | | // reserved data type under OpenCL v2.0 s6.1.4), we don't support selects |
2688 | | // on bitvectors, and we have no well-defined ABI for bitvectors, so vectors |
2689 | | // of bool aren't allowed. |
2690 | | // |
2691 | | // We explictly allow bool elements in ext_vector_type for C/C++. |
2692 | 4.87k | bool IsNoBoolVecLang = getLangOpts().OpenCL || getLangOpts().OpenCLCPlusPlus784 ; |
2693 | 4.87k | if ((!T->isDependentType() && !T->isIntegerType()4.86k && |
2694 | 4.87k | !T->isRealFloatingType()1.48k ) || |
2695 | 4.87k | (4.86k IsNoBoolVecLang4.86k && T->isBooleanType()4.09k )) { |
2696 | 16 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
2697 | 16 | return QualType(); |
2698 | 16 | } |
2699 | | |
2700 | 4.86k | if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()4.86k ) { |
2701 | 4.83k | Optional<llvm::APSInt> vecSize = ArraySize->getIntegerConstantExpr(Context); |
2702 | 4.83k | if (!vecSize) { |
2703 | 0 | Diag(AttrLoc, diag::err_attribute_argument_type) |
2704 | 0 | << "ext_vector_type" << AANT_ArgumentIntegerConstant |
2705 | 0 | << ArraySize->getSourceRange(); |
2706 | 0 | return QualType(); |
2707 | 0 | } |
2708 | | |
2709 | 4.83k | if (!vecSize->isIntN(32)) { |
2710 | 6 | Diag(AttrLoc, diag::err_attribute_size_too_large) |
2711 | 6 | << ArraySize->getSourceRange() << "vector"; |
2712 | 6 | return QualType(); |
2713 | 6 | } |
2714 | | // Unlike gcc's vector_size attribute, the size is specified as the |
2715 | | // number of elements, not the number of bytes. |
2716 | 4.83k | unsigned vectorSize = static_cast<unsigned>(vecSize->getZExtValue()); |
2717 | | |
2718 | 4.83k | if (vectorSize == 0) { |
2719 | 9 | Diag(AttrLoc, diag::err_attribute_zero_size) |
2720 | 9 | << ArraySize->getSourceRange() << "vector"; |
2721 | 9 | return QualType(); |
2722 | 9 | } |
2723 | | |
2724 | 4.82k | return Context.getExtVectorType(T, vectorSize); |
2725 | 4.83k | } |
2726 | | |
2727 | 27 | return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); |
2728 | 4.86k | } |
2729 | | |
2730 | | QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols, |
2731 | 419 | SourceLocation AttrLoc) { |
2732 | 419 | assert(Context.getLangOpts().MatrixTypes && |
2733 | 419 | "Should never build a matrix type when it is disabled"); |
2734 | | |
2735 | | // Check element type, if it is not dependent. |
2736 | 419 | if (!ElementTy->isDependentType() && |
2737 | 419 | !MatrixType::isValidElementType(ElementTy)379 ) { |
2738 | 6 | Diag(AttrLoc, diag::err_attribute_invalid_matrix_type) << ElementTy; |
2739 | 6 | return QualType(); |
2740 | 6 | } |
2741 | | |
2742 | 413 | if (NumRows->isTypeDependent() || NumCols->isTypeDependent()412 || |
2743 | 413 | NumRows->isValueDependent()412 || NumCols->isValueDependent()362 ) |
2744 | 77 | return Context.getDependentSizedMatrixType(ElementTy, NumRows, NumCols, |
2745 | 77 | AttrLoc); |
2746 | | |
2747 | 336 | Optional<llvm::APSInt> ValueRows = NumRows->getIntegerConstantExpr(Context); |
2748 | 336 | Optional<llvm::APSInt> ValueColumns = |
2749 | 336 | NumCols->getIntegerConstantExpr(Context); |
2750 | | |
2751 | 336 | auto const RowRange = NumRows->getSourceRange(); |
2752 | 336 | auto const ColRange = NumCols->getSourceRange(); |
2753 | | |
2754 | | // Both are row and column expressions are invalid. |
2755 | 336 | if (!ValueRows && !ValueColumns2 ) { |
2756 | 1 | Diag(AttrLoc, diag::err_attribute_argument_type) |
2757 | 1 | << "matrix_type" << AANT_ArgumentIntegerConstant << RowRange |
2758 | 1 | << ColRange; |
2759 | 1 | return QualType(); |
2760 | 1 | } |
2761 | | |
2762 | | // Only the row expression is invalid. |
2763 | 335 | if (!ValueRows) { |
2764 | 1 | Diag(AttrLoc, diag::err_attribute_argument_type) |
2765 | 1 | << "matrix_type" << AANT_ArgumentIntegerConstant << RowRange; |
2766 | 1 | return QualType(); |
2767 | 1 | } |
2768 | | |
2769 | | // Only the column expression is invalid. |
2770 | 334 | if (!ValueColumns) { |
2771 | 1 | Diag(AttrLoc, diag::err_attribute_argument_type) |
2772 | 1 | << "matrix_type" << AANT_ArgumentIntegerConstant << ColRange; |
2773 | 1 | return QualType(); |
2774 | 1 | } |
2775 | | |
2776 | | // Check the matrix dimensions. |
2777 | 333 | unsigned MatrixRows = static_cast<unsigned>(ValueRows->getZExtValue()); |
2778 | 333 | unsigned MatrixColumns = static_cast<unsigned>(ValueColumns->getZExtValue()); |
2779 | 333 | if (MatrixRows == 0 && MatrixColumns == 03 ) { |
2780 | 0 | Diag(AttrLoc, diag::err_attribute_zero_size) |
2781 | 0 | << "matrix" << RowRange << ColRange; |
2782 | 0 | return QualType(); |
2783 | 0 | } |
2784 | 333 | if (MatrixRows == 0) { |
2785 | 3 | Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << RowRange; |
2786 | 3 | return QualType(); |
2787 | 3 | } |
2788 | 330 | if (MatrixColumns == 0) { |
2789 | 1 | Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << ColRange; |
2790 | 1 | return QualType(); |
2791 | 1 | } |
2792 | 329 | if (!ConstantMatrixType::isDimensionValid(MatrixRows)) { |
2793 | 3 | Diag(AttrLoc, diag::err_attribute_size_too_large) |
2794 | 3 | << RowRange << "matrix row"; |
2795 | 3 | return QualType(); |
2796 | 3 | } |
2797 | 326 | if (!ConstantMatrixType::isDimensionValid(MatrixColumns)) { |
2798 | 1 | Diag(AttrLoc, diag::err_attribute_size_too_large) |
2799 | 1 | << ColRange << "matrix column"; |
2800 | 1 | return QualType(); |
2801 | 1 | } |
2802 | 325 | return Context.getConstantMatrixType(ElementTy, MatrixRows, MatrixColumns); |
2803 | 326 | } |
2804 | | |
2805 | 1.73M | bool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) { |
2806 | 1.73M | if (T->isArrayType() || T->isFunctionType()1.73M ) { |
2807 | 18 | Diag(Loc, diag::err_func_returning_array_function) |
2808 | 18 | << T->isFunctionType() << T; |
2809 | 18 | return true; |
2810 | 18 | } |
2811 | | |
2812 | | // Functions cannot return half FP. |
2813 | 1.73M | if (T->isHalfType() && !getLangOpts().HalfArgsAndReturns0 ) { |
2814 | 0 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 << |
2815 | 0 | FixItHint::CreateInsertion(Loc, "*"); |
2816 | 0 | return true; |
2817 | 0 | } |
2818 | | |
2819 | | // Methods cannot return interface types. All ObjC objects are |
2820 | | // passed by reference. |
2821 | 1.73M | if (T->isObjCObjectType()) { |
2822 | 3 | Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) |
2823 | 3 | << 0 << T << FixItHint::CreateInsertion(Loc, "*"); |
2824 | 3 | return true; |
2825 | 3 | } |
2826 | | |
2827 | 1.73M | if (T.hasNonTrivialToPrimitiveDestructCUnion() || |
2828 | 1.73M | T.hasNonTrivialToPrimitiveCopyCUnion()1.73M ) |
2829 | 2 | checkNonTrivialCUnion(T, Loc, NTCUC_FunctionReturn, |
2830 | 2 | NTCUK_Destruct|NTCUK_Copy); |
2831 | | |
2832 | | // C++2a [dcl.fct]p12: |
2833 | | // A volatile-qualified return type is deprecated |
2834 | 1.73M | if (T.isVolatileQualified() && getLangOpts().CPlusPlus2031 ) |
2835 | 5 | Diag(Loc, diag::warn_deprecated_volatile_return) << T; |
2836 | | |
2837 | 1.73M | return false; |
2838 | 1.73M | } |
2839 | | |
2840 | | /// Check the extended parameter information. Most of the necessary |
2841 | | /// checking should occur when applying the parameter attribute; the |
2842 | | /// only other checks required are positional restrictions. |
2843 | | static void checkExtParameterInfos(Sema &S, ArrayRef<QualType> paramTypes, |
2844 | | const FunctionProtoType::ExtProtoInfo &EPI, |
2845 | 8.86k | llvm::function_ref<SourceLocation(unsigned)> getParamLoc) { |
2846 | 8.86k | assert(EPI.ExtParameterInfos && "shouldn't get here without param infos"); |
2847 | | |
2848 | 0 | bool emittedError = false; |
2849 | 8.86k | auto actualCC = EPI.ExtInfo.getCC(); |
2850 | 8.86k | enum class RequiredCC { OnlySwift, SwiftOrSwiftAsync }; |
2851 | 8.86k | auto checkCompatible = [&](unsigned paramIndex, RequiredCC required) { |
2852 | 181 | bool isCompatible = |
2853 | 181 | (required == RequiredCC::OnlySwift) |
2854 | 181 | ? (actualCC == CC_Swift)34 |
2855 | 181 | : (147 actualCC == CC_Swift147 || actualCC == CC_SwiftAsync21 ); |
2856 | 181 | if (isCompatible || emittedError9 ) |
2857 | 172 | return; |
2858 | 9 | S.Diag(getParamLoc(paramIndex), diag::err_swift_param_attr_not_swiftcall) |
2859 | 9 | << getParameterABISpelling(EPI.ExtParameterInfos[paramIndex].getABI()) |
2860 | 9 | << (required == RequiredCC::OnlySwift); |
2861 | 9 | emittedError = true; |
2862 | 9 | }; |
2863 | 8.86k | for (size_t paramIndex = 0, numParams = paramTypes.size(); |
2864 | 35.7k | paramIndex != numParams; ++paramIndex26.8k ) { |
2865 | 26.8k | switch (EPI.ExtParameterInfos[paramIndex].getABI()) { |
2866 | | // Nothing interesting to check for orindary-ABI parameters. |
2867 | 26.5k | case ParameterABI::Ordinary: |
2868 | 26.5k | continue; |
2869 | | |
2870 | | // swift_indirect_result parameters must be a prefix of the function |
2871 | | // arguments. |
2872 | 83 | case ParameterABI::SwiftIndirectResult: |
2873 | 83 | checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync); |
2874 | 83 | if (paramIndex != 0 && |
2875 | 83 | EPI.ExtParameterInfos[paramIndex - 1].getABI() |
2876 | 33 | != ParameterABI::SwiftIndirectResult) { |
2877 | 3 | S.Diag(getParamLoc(paramIndex), |
2878 | 3 | diag::err_swift_indirect_result_not_first); |
2879 | 3 | } |
2880 | 83 | continue; |
2881 | | |
2882 | 64 | case ParameterABI::SwiftContext: |
2883 | 64 | checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync); |
2884 | 64 | continue; |
2885 | | |
2886 | | // SwiftAsyncContext is not limited to swiftasynccall functions. |
2887 | 149 | case ParameterABI::SwiftAsyncContext: |
2888 | 149 | continue; |
2889 | | |
2890 | | // swift_error parameters must be preceded by a swift_context parameter. |
2891 | 34 | case ParameterABI::SwiftErrorResult: |
2892 | 34 | checkCompatible(paramIndex, RequiredCC::OnlySwift); |
2893 | 34 | if (paramIndex == 0 || |
2894 | 34 | EPI.ExtParameterInfos[paramIndex - 1].getABI() != |
2895 | 31 | ParameterABI::SwiftContext) { |
2896 | 6 | S.Diag(getParamLoc(paramIndex), |
2897 | 6 | diag::err_swift_error_result_not_after_swift_context); |
2898 | 6 | } |
2899 | 34 | continue; |
2900 | 26.8k | } |
2901 | 0 | llvm_unreachable("bad ABI kind"); |
2902 | 0 | } |
2903 | 8.86k | } |
2904 | | |
2905 | | QualType Sema::BuildFunctionType(QualType T, |
2906 | | MutableArrayRef<QualType> ParamTypes, |
2907 | | SourceLocation Loc, DeclarationName Entity, |
2908 | 1.08M | const FunctionProtoType::ExtProtoInfo &EPI) { |
2909 | 1.08M | bool Invalid = false; |
2910 | | |
2911 | 1.08M | Invalid |= CheckFunctionReturnType(T, Loc); |
2912 | | |
2913 | 2.58M | for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx1.50M ) { |
2914 | | // FIXME: Loc is too inprecise here, should use proper locations for args. |
2915 | 1.50M | QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]); |
2916 | 1.50M | if (ParamType->isVoidType()) { |
2917 | 0 | Diag(Loc, diag::err_param_with_void_type); |
2918 | 0 | Invalid = true; |
2919 | 1.50M | } else if (ParamType->isHalfType() && !getLangOpts().HalfArgsAndReturns0 ) { |
2920 | | // Disallow half FP arguments. |
2921 | 0 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 << |
2922 | 0 | FixItHint::CreateInsertion(Loc, "*"); |
2923 | 0 | Invalid = true; |
2924 | 0 | } |
2925 | | |
2926 | | // C++2a [dcl.fct]p4: |
2927 | | // A parameter with volatile-qualified type is deprecated |
2928 | 1.50M | if (ParamType.isVolatileQualified() && getLangOpts().CPlusPlus206 ) |
2929 | 1 | Diag(Loc, diag::warn_deprecated_volatile_param) << ParamType; |
2930 | | |
2931 | 1.50M | ParamTypes[Idx] = ParamType; |
2932 | 1.50M | } |
2933 | | |
2934 | 1.08M | if (EPI.ExtParameterInfos) { |
2935 | 86 | checkExtParameterInfos(*this, ParamTypes, EPI, |
2936 | 86 | [=](unsigned i) { return Loc; }0 ); |
2937 | 86 | } |
2938 | | |
2939 | 1.08M | if (EPI.ExtInfo.getProducesResult()) { |
2940 | | // This is just a warning, so we can't fail to build if we see it. |
2941 | 18 | checkNSReturnsRetainedReturnType(Loc, T); |
2942 | 18 | } |
2943 | | |
2944 | 1.08M | if (Invalid) |
2945 | 17 | return QualType(); |
2946 | | |
2947 | 1.08M | return Context.getFunctionType(T, ParamTypes, EPI); |
2948 | 1.08M | } |
2949 | | |
2950 | | /// Build a member pointer type \c T Class::*. |
2951 | | /// |
2952 | | /// \param T the type to which the member pointer refers. |
2953 | | /// \param Class the class type into which the member pointer points. |
2954 | | /// \param Loc the location where this type begins |
2955 | | /// \param Entity the name of the entity that will have this member pointer type |
2956 | | /// |
2957 | | /// \returns a member pointer type, if successful, or a NULL type if there was |
2958 | | /// an error. |
2959 | | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
2960 | | SourceLocation Loc, |
2961 | 33.5k | DeclarationName Entity) { |
2962 | | // Verify that we're not building a pointer to pointer to function with |
2963 | | // exception specification. |
2964 | 33.5k | if (CheckDistantExceptionSpec(T)) { |
2965 | 3 | Diag(Loc, diag::err_distant_exception_spec); |
2966 | 3 | return QualType(); |
2967 | 3 | } |
2968 | | |
2969 | | // C++ 8.3.3p3: A pointer to member shall not point to ... a member |
2970 | | // with reference type, or "cv void." |
2971 | 33.5k | if (T->isReferenceType()) { |
2972 | 4 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
2973 | 4 | << getPrintableNameForEntity(Entity) << T; |
2974 | 4 | return QualType(); |
2975 | 4 | } |
2976 | | |
2977 | 33.5k | if (T->isVoidType()) { |
2978 | 5 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
2979 | 5 | << getPrintableNameForEntity(Entity); |
2980 | 5 | return QualType(); |
2981 | 5 | } |
2982 | | |
2983 | 33.5k | if (!Class->isDependentType() && !Class->isRecordType()3.76k ) { |
2984 | 63 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
2985 | 63 | return QualType(); |
2986 | 63 | } |
2987 | | |
2988 | 33.5k | if (T->isFunctionType() && getLangOpts().OpenCL28.3k && |
2989 | 33.5k | !getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", |
2990 | 4 | getLangOpts())) { |
2991 | 2 | Diag(Loc, diag::err_opencl_function_pointer) << /*pointer*/ 0; |
2992 | 2 | return QualType(); |
2993 | 2 | } |
2994 | | |
2995 | 33.5k | if (getLangOpts().HLSL) { |
2996 | 2 | Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0; |
2997 | 2 | return QualType(); |
2998 | 2 | } |
2999 | | |
3000 | | // Adjust the default free function calling convention to the default method |
3001 | | // calling convention. |
3002 | 33.5k | bool IsCtorOrDtor = |
3003 | 33.5k | (Entity.getNameKind() == DeclarationName::CXXConstructorName) || |
3004 | 33.5k | (Entity.getNameKind() == DeclarationName::CXXDestructorName); |
3005 | 33.5k | if (T->isFunctionType()) |
3006 | 28.3k | adjustMemberFunctionCC(T, /*IsStatic=*/false, IsCtorOrDtor, Loc); |
3007 | | |
3008 | 33.5k | return Context.getMemberPointerType(T, Class.getTypePtr()); |
3009 | 33.5k | } |
3010 | | |
3011 | | /// Build a block pointer type. |
3012 | | /// |
3013 | | /// \param T The type to which we'll be building a block pointer. |
3014 | | /// |
3015 | | /// \param Loc The source location, used for diagnostics. |
3016 | | /// |
3017 | | /// \param Entity The name of the entity that involves the block pointer |
3018 | | /// type, if known. |
3019 | | /// |
3020 | | /// \returns A suitable block pointer type, if there are no |
3021 | | /// errors. Otherwise, returns a NULL type. |
3022 | | QualType Sema::BuildBlockPointerType(QualType T, |
3023 | | SourceLocation Loc, |
3024 | 61.6k | DeclarationName Entity) { |
3025 | 61.6k | if (!T->isFunctionType()) { |
3026 | 11 | Diag(Loc, diag::err_nonfunction_block_type); |
3027 | 11 | return QualType(); |
3028 | 11 | } |
3029 | | |
3030 | 61.6k | if (checkQualifiedFunction(*this, T, Loc, QFK_BlockPointer)) |
3031 | 0 | return QualType(); |
3032 | | |
3033 | 61.6k | if (getLangOpts().OpenCL) |
3034 | 147 | T = deduceOpenCLPointeeAddrSpace(*this, T); |
3035 | | |
3036 | 61.6k | return Context.getBlockPointerType(T); |
3037 | 61.6k | } |
3038 | | |
3039 | 119M | QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { |
3040 | 119M | QualType QT = Ty.get(); |
3041 | 119M | if (QT.isNull()) { |
3042 | 1.59M | if (TInfo) *TInfo = nullptr285 ; |
3043 | 1.59M | return QualType(); |
3044 | 1.59M | } |
3045 | | |
3046 | 118M | TypeSourceInfo *DI = nullptr; |
3047 | 118M | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
3048 | 18.6M | QT = LIT->getType(); |
3049 | 18.6M | DI = LIT->getTypeSourceInfo(); |
3050 | 18.6M | } |
3051 | | |
3052 | 118M | if (TInfo) *TInfo = DI17.5M ; |
3053 | 118M | return QT; |
3054 | 119M | } |
3055 | | |
3056 | | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, |
3057 | | Qualifiers::ObjCLifetime ownership, |
3058 | | unsigned chunkIndex); |
3059 | | |
3060 | | /// Given that this is the declaration of a parameter under ARC, |
3061 | | /// attempt to infer attributes and such for pointer-to-whatever |
3062 | | /// types. |
3063 | | static void inferARCWriteback(TypeProcessingState &state, |
3064 | 144k | QualType &declSpecType) { |
3065 | 144k | Sema &S = state.getSema(); |
3066 | 144k | Declarator &declarator = state.getDeclarator(); |
3067 | | |
3068 | | // TODO: should we care about decl qualifiers? |
3069 | | |
3070 | | // Check whether the declarator has the expected form. We walk |
3071 | | // from the inside out in order to make the block logic work. |
3072 | 144k | unsigned outermostPointerIndex = 0; |
3073 | 144k | bool isBlockPointer = false; |
3074 | 144k | unsigned numPointers = 0; |
3075 | 192k | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i47.8k ) { |
3076 | 49.4k | unsigned chunkIndex = i; |
3077 | 49.4k | DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex); |
3078 | 49.4k | switch (chunk.Kind) { |
3079 | 216 | case DeclaratorChunk::Paren: |
3080 | | // Ignore parens. |
3081 | 216 | break; |
3082 | | |
3083 | 931 | case DeclaratorChunk::Reference: |
3084 | 47.5k | case DeclaratorChunk::Pointer: |
3085 | | // Count the number of pointers. Treat references |
3086 | | // interchangeably as pointers; if they're mis-ordered, normal |
3087 | | // type building will discover that. |
3088 | 47.5k | outermostPointerIndex = chunkIndex; |
3089 | 47.5k | numPointers++; |
3090 | 47.5k | break; |
3091 | | |
3092 | 785 | case DeclaratorChunk::BlockPointer: |
3093 | | // If we have a pointer to block pointer, that's an acceptable |
3094 | | // indirect reference; anything else is not an application of |
3095 | | // the rules. |
3096 | 785 | if (numPointers != 1) return; |
3097 | 0 | numPointers++; |
3098 | 0 | outermostPointerIndex = chunkIndex; |
3099 | 0 | isBlockPointer = true; |
3100 | | |
3101 | | // We don't care about pointer structure in return values here. |
3102 | 0 | goto done; |
3103 | | |
3104 | 639 | case DeclaratorChunk::Array: // suppress if written (id[])? |
3105 | 855 | case DeclaratorChunk::Function: |
3106 | 855 | case DeclaratorChunk::MemberPointer: |
3107 | 855 | case DeclaratorChunk::Pipe: |
3108 | 855 | return; |
3109 | 49.4k | } |
3110 | 49.4k | } |
3111 | 142k | done: |
3112 | | |
3113 | | // If we have *one* pointer, then we want to throw the qualifier on |
3114 | | // the declaration-specifiers, which means that it needs to be a |
3115 | | // retainable object type. |
3116 | 142k | if (numPointers == 1) { |
3117 | | // If it's not a retainable object type, the rule doesn't apply. |
3118 | 44.1k | if (!declSpecType->isObjCRetainableType()) return43.8k ; |
3119 | | |
3120 | | // If it already has lifetime, don't do anything. |
3121 | 232 | if (declSpecType.getObjCLifetime()) return130 ; |
3122 | | |
3123 | | // Otherwise, modify the type in-place. |
3124 | 102 | Qualifiers qs; |
3125 | | |
3126 | 102 | if (declSpecType->isObjCARCImplicitlyUnretainedType()) |
3127 | 4 | qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone); |
3128 | 98 | else |
3129 | 98 | qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing); |
3130 | 102 | declSpecType = S.Context.getQualifiedType(declSpecType, qs); |
3131 | | |
3132 | | // If we have *two* pointers, then we want to throw the qualifier on |
3133 | | // the outermost pointer. |
3134 | 98.6k | } else if (numPointers == 2) { |
3135 | | // If we don't have a block pointer, we need to check whether the |
3136 | | // declaration-specifiers gave us something that will turn into a |
3137 | | // retainable object pointer after we slap the first pointer on it. |
3138 | 1.60k | if (!isBlockPointer && !declSpecType->isObjCObjectType()) |
3139 | 788 | return; |
3140 | | |
3141 | | // Look for an explicit lifetime attribute there. |
3142 | 821 | DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex); |
3143 | 821 | if (chunk.Kind != DeclaratorChunk::Pointer && |
3144 | 821 | chunk.Kind != DeclaratorChunk::BlockPointer1 ) |
3145 | 1 | return; |
3146 | 820 | for (const ParsedAttr &AL : chunk.getAttrs()) |
3147 | 259 | if (AL.getKind() == ParsedAttr::AT_ObjCOwnership) |
3148 | 87 | return; |
3149 | | |
3150 | 733 | transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing, |
3151 | 733 | outermostPointerIndex); |
3152 | | |
3153 | | // Any other number of pointers/references does not trigger the rule. |
3154 | 97.0k | } else return; |
3155 | | |
3156 | | // TODO: mark whether we did this inference? |
3157 | 142k | } |
3158 | | |
3159 | | void Sema::diagnoseIgnoredQualifiers(unsigned DiagID, unsigned Quals, |
3160 | | SourceLocation FallbackLoc, |
3161 | | SourceLocation ConstQualLoc, |
3162 | | SourceLocation VolatileQualLoc, |
3163 | | SourceLocation RestrictQualLoc, |
3164 | | SourceLocation AtomicQualLoc, |
3165 | 407 | SourceLocation UnalignedQualLoc) { |
3166 | 407 | if (!Quals) |
3167 | 18 | return; |
3168 | | |
3169 | 389 | struct Qual { |
3170 | 389 | const char *Name; |
3171 | 389 | unsigned Mask; |
3172 | 389 | SourceLocation Loc; |
3173 | 389 | } const QualKinds[5] = { |
3174 | 389 | { "const", DeclSpec::TQ_const, ConstQualLoc }, |
3175 | 389 | { "volatile", DeclSpec::TQ_volatile, VolatileQualLoc }, |
3176 | 389 | { "restrict", DeclSpec::TQ_restrict, RestrictQualLoc }, |
3177 | 389 | { "__unaligned", DeclSpec::TQ_unaligned, UnalignedQualLoc }, |
3178 | 389 | { "_Atomic", DeclSpec::TQ_atomic, AtomicQualLoc } |
3179 | 389 | }; |
3180 | | |
3181 | 389 | SmallString<32> QualStr; |
3182 | 389 | unsigned NumQuals = 0; |
3183 | 389 | SourceLocation Loc; |
3184 | 389 | FixItHint FixIts[5]; |
3185 | | |
3186 | | // Build a string naming the redundant qualifiers. |
3187 | 1.94k | for (auto &E : QualKinds) { |
3188 | 1.94k | if (Quals & E.Mask) { |
3189 | 419 | if (!QualStr.empty()) QualStr += ' '30 ; |
3190 | 419 | QualStr += E.Name; |
3191 | | |
3192 | | // If we have a location for the qualifier, offer a fixit. |
3193 | 419 | SourceLocation QualLoc = E.Loc; |
3194 | 419 | if (QualLoc.isValid()) { |
3195 | 398 | FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc); |
3196 | 398 | if (Loc.isInvalid() || |
3197 | 398 | getSourceManager().isBeforeInTranslationUnit(QualLoc, Loc)29 ) |
3198 | 370 | Loc = QualLoc; |
3199 | 398 | } |
3200 | | |
3201 | 419 | ++NumQuals; |
3202 | 419 | } |
3203 | 1.94k | } |
3204 | | |
3205 | 389 | Diag(Loc.isInvalid() ? FallbackLoc20 : Loc369 , DiagID) |
3206 | 389 | << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3]; |
3207 | 389 | } |
3208 | | |
3209 | | // Diagnose pointless type qualifiers on the return type of a function. |
3210 | | static void diagnoseRedundantReturnTypeQualifiers(Sema &S, QualType RetTy, |
3211 | | Declarator &D, |
3212 | 419 | unsigned FunctionChunkIndex) { |
3213 | 419 | const DeclaratorChunk::FunctionTypeInfo &FTI = |
3214 | 419 | D.getTypeObject(FunctionChunkIndex).Fun; |
3215 | 419 | if (FTI.hasTrailingReturnType()) { |
3216 | 10 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, |
3217 | 10 | RetTy.getLocalCVRQualifiers(), |
3218 | 10 | FTI.getTrailingReturnTypeLoc()); |
3219 | 10 | return; |
3220 | 10 | } |
3221 | | |
3222 | 409 | for (unsigned OuterChunkIndex = FunctionChunkIndex + 1, |
3223 | 409 | End = D.getNumTypeObjects(); |
3224 | 412 | OuterChunkIndex != End; ++OuterChunkIndex3 ) { |
3225 | 35 | DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex); |
3226 | 35 | switch (OuterChunk.Kind) { |
3227 | 3 | case DeclaratorChunk::Paren: |
3228 | 3 | continue; |
3229 | | |
3230 | 22 | case DeclaratorChunk::Pointer: { |
3231 | 22 | DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr; |
3232 | 22 | S.diagnoseIgnoredQualifiers( |
3233 | 22 | diag::warn_qual_return_type, |
3234 | 22 | PTI.TypeQuals, |
3235 | 22 | SourceLocation(), |
3236 | 22 | PTI.ConstQualLoc, |
3237 | 22 | PTI.VolatileQualLoc, |
3238 | 22 | PTI.RestrictQualLoc, |
3239 | 22 | PTI.AtomicQualLoc, |
3240 | 22 | PTI.UnalignedQualLoc); |
3241 | 22 | return; |
3242 | 0 | } |
3243 | | |
3244 | 0 | case DeclaratorChunk::Function: |
3245 | 0 | case DeclaratorChunk::BlockPointer: |
3246 | 1 | case DeclaratorChunk::Reference: |
3247 | 1 | case DeclaratorChunk::Array: |
3248 | 10 | case DeclaratorChunk::MemberPointer: |
3249 | 10 | case DeclaratorChunk::Pipe: |
3250 | | // FIXME: We can't currently provide an accurate source location and a |
3251 | | // fix-it hint for these. |
3252 | 10 | unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic0 : 0; |
3253 | 10 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, |
3254 | 10 | RetTy.getCVRQualifiers() | AtomicQual, |
3255 | 10 | D.getIdentifierLoc()); |
3256 | 10 | return; |
3257 | 35 | } |
3258 | | |
3259 | 0 | llvm_unreachable("unknown declarator chunk kind"); |
3260 | 0 | } |
3261 | | |
3262 | | // If the qualifiers come from a conversion function type, don't diagnose |
3263 | | // them -- they're not necessarily redundant, since such a conversion |
3264 | | // operator can be explicitly called as "x.operator const int()". |
3265 | 377 | if (D.getName().getKind() == UnqualifiedIdKind::IK_ConversionFunctionId) |
3266 | 15 | return; |
3267 | | |
3268 | | // Just parens all the way out to the decl specifiers. Diagnose any qualifiers |
3269 | | // which are present there. |
3270 | 362 | S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type, |
3271 | 362 | D.getDeclSpec().getTypeQualifiers(), |
3272 | 362 | D.getIdentifierLoc(), |
3273 | 362 | D.getDeclSpec().getConstSpecLoc(), |
3274 | 362 | D.getDeclSpec().getVolatileSpecLoc(), |
3275 | 362 | D.getDeclSpec().getRestrictSpecLoc(), |
3276 | 362 | D.getDeclSpec().getAtomicSpecLoc(), |
3277 | 362 | D.getDeclSpec().getUnalignedSpecLoc()); |
3278 | 362 | } |
3279 | | |
3280 | | static std::pair<QualType, TypeSourceInfo *> |
3281 | | InventTemplateParameter(TypeProcessingState &state, QualType T, |
3282 | | TypeSourceInfo *TrailingTSI, AutoType *Auto, |
3283 | 2.63k | InventedTemplateParameterInfo &Info) { |
3284 | 2.63k | Sema &S = state.getSema(); |
3285 | 2.63k | Declarator &D = state.getDeclarator(); |
3286 | | |
3287 | 2.63k | const unsigned TemplateParameterDepth = Info.AutoTemplateParameterDepth; |
3288 | 2.63k | const unsigned AutoParameterPosition = Info.TemplateParams.size(); |
3289 | 2.63k | const bool IsParameterPack = D.hasEllipsis(); |
3290 | | |
3291 | | // If auto is mentioned in a lambda parameter or abbreviated function |
3292 | | // template context, convert it to a template parameter type. |
3293 | | |
3294 | | // Create the TemplateTypeParmDecl here to retrieve the corresponding |
3295 | | // template parameter type. Template parameters are temporarily added |
3296 | | // to the TU until the associated TemplateDecl is created. |
3297 | 2.63k | TemplateTypeParmDecl *InventedTemplateParam = |
3298 | 2.63k | TemplateTypeParmDecl::Create( |
3299 | 2.63k | S.Context, S.Context.getTranslationUnitDecl(), |
3300 | 2.63k | /*KeyLoc=*/D.getDeclSpec().getTypeSpecTypeLoc(), |
3301 | 2.63k | /*NameLoc=*/D.getIdentifierLoc(), |
3302 | 2.63k | TemplateParameterDepth, AutoParameterPosition, |
3303 | 2.63k | S.InventAbbreviatedTemplateParameterTypeName( |
3304 | 2.63k | D.getIdentifier(), AutoParameterPosition), false, |
3305 | 2.63k | IsParameterPack, /*HasTypeConstraint=*/Auto->isConstrained()); |
3306 | 2.63k | InventedTemplateParam->setImplicit(); |
3307 | 2.63k | Info.TemplateParams.push_back(InventedTemplateParam); |
3308 | | |
3309 | | // Attach type constraints to the new parameter. |
3310 | 2.63k | if (Auto->isConstrained()) { |
3311 | 103 | if (TrailingTSI) { |
3312 | | // The 'auto' appears in a trailing return type we've already built; |
3313 | | // extract its type constraints to attach to the template parameter. |
3314 | 0 | AutoTypeLoc AutoLoc = TrailingTSI->getTypeLoc().getContainedAutoTypeLoc(); |
3315 | 0 | TemplateArgumentListInfo TAL(AutoLoc.getLAngleLoc(), AutoLoc.getRAngleLoc()); |
3316 | 0 | bool Invalid = false; |
3317 | 0 | for (unsigned Idx = 0; Idx < AutoLoc.getNumArgs(); ++Idx) { |
3318 | 0 | if (D.getEllipsisLoc().isInvalid() && !Invalid && |
3319 | 0 | S.DiagnoseUnexpandedParameterPack(AutoLoc.getArgLoc(Idx), |
3320 | 0 | Sema::UPPC_TypeConstraint)) |
3321 | 0 | Invalid = true; |
3322 | 0 | TAL.addArgument(AutoLoc.getArgLoc(Idx)); |
3323 | 0 | } |
3324 | |
|
3325 | 0 | if (!Invalid) { |
3326 | 0 | S.AttachTypeConstraint( |
3327 | 0 | AutoLoc.getNestedNameSpecifierLoc(), AutoLoc.getConceptNameInfo(), |
3328 | 0 | AutoLoc.getNamedConcept(), |
3329 | 0 | AutoLoc.hasExplicitTemplateArgs() ? &TAL : nullptr, |
3330 | 0 | InventedTemplateParam, D.getEllipsisLoc()); |
3331 | 0 | } |
3332 | 103 | } else { |
3333 | | // The 'auto' appears in the decl-specifiers; we've not finished forming |
3334 | | // TypeSourceInfo for it yet. |
3335 | 103 | TemplateIdAnnotation *TemplateId = D.getDeclSpec().getRepAsTemplateId(); |
3336 | 103 | TemplateArgumentListInfo TemplateArgsInfo; |
3337 | 103 | bool Invalid = false; |
3338 | 103 | if (TemplateId->LAngleLoc.isValid()) { |
3339 | 56 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
3340 | 56 | TemplateId->NumArgs); |
3341 | 56 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgsInfo); |
3342 | | |
3343 | 56 | if (D.getEllipsisLoc().isInvalid()) { |
3344 | 48 | for (TemplateArgumentLoc Arg : TemplateArgsInfo.arguments()) { |
3345 | 48 | if (S.DiagnoseUnexpandedParameterPack(Arg, |
3346 | 48 | Sema::UPPC_TypeConstraint)) { |
3347 | 2 | Invalid = true; |
3348 | 2 | break; |
3349 | 2 | } |
3350 | 48 | } |
3351 | 48 | } |
3352 | 56 | } |
3353 | 103 | if (!Invalid) { |
3354 | 101 | S.AttachTypeConstraint( |
3355 | 101 | D.getDeclSpec().getTypeSpecScope().getWithLocInContext(S.Context), |
3356 | 101 | DeclarationNameInfo(DeclarationName(TemplateId->Name), |
3357 | 101 | TemplateId->TemplateNameLoc), |
3358 | 101 | cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl()), |
3359 | 101 | TemplateId->LAngleLoc.isValid() ? &TemplateArgsInfo54 : nullptr47 , |
3360 | 101 | InventedTemplateParam, D.getEllipsisLoc()); |
3361 | 101 | } |
3362 | 103 | } |
3363 | 103 | } |
3364 | | |
3365 | | // Replace the 'auto' in the function parameter with this invented |
3366 | | // template type parameter. |
3367 | | // FIXME: Retain some type sugar to indicate that this was written |
3368 | | // as 'auto'? |
3369 | 2.63k | QualType Replacement(InventedTemplateParam->getTypeForDecl(), 0); |
3370 | 2.63k | QualType NewT = state.ReplaceAutoType(T, Replacement); |
3371 | 2.63k | TypeSourceInfo *NewTSI = |
3372 | 2.63k | TrailingTSI ? S.ReplaceAutoTypeSourceInfo(TrailingTSI, Replacement)8 |
3373 | 2.63k | : nullptr2.62k ; |
3374 | 2.63k | return {NewT, NewTSI}; |
3375 | 2.63k | } |
3376 | | |
3377 | | static TypeSourceInfo * |
3378 | | GetTypeSourceInfoForDeclarator(TypeProcessingState &State, |
3379 | | QualType T, TypeSourceInfo *ReturnTypeInfo); |
3380 | | |
3381 | | static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, |
3382 | 111M | TypeSourceInfo *&ReturnTypeInfo) { |
3383 | 111M | Sema &SemaRef = state.getSema(); |
3384 | 111M | Declarator &D = state.getDeclarator(); |
3385 | 111M | QualType T; |
3386 | 111M | ReturnTypeInfo = nullptr; |
3387 | | |
3388 | | // The TagDecl owned by the DeclSpec. |
3389 | 111M | TagDecl *OwnedTagDecl = nullptr; |
3390 | | |
3391 | 111M | switch (D.getName().getKind()) { |
3392 | 0 | case UnqualifiedIdKind::IK_ImplicitSelfParam: |
3393 | 408k | case UnqualifiedIdKind::IK_OperatorFunctionId: |
3394 | 111M | case UnqualifiedIdKind::IK_Identifier: |
3395 | 111M | case UnqualifiedIdKind::IK_LiteralOperatorId: |
3396 | 111M | case UnqualifiedIdKind::IK_TemplateId: |
3397 | 111M | T = ConvertDeclSpecToType(state); |
3398 | | |
3399 | 111M | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()111M ) { |
3400 | 458k | OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
3401 | | // Owned declaration is embedded in declarator. |
3402 | 458k | OwnedTagDecl->setEmbeddedInDeclarator(true); |
3403 | 458k | } |
3404 | 111M | break; |
3405 | | |
3406 | 358k | case UnqualifiedIdKind::IK_ConstructorName: |
3407 | 358k | case UnqualifiedIdKind::IK_ConstructorTemplateId: |
3408 | 407k | case UnqualifiedIdKind::IK_DestructorName: |
3409 | | // Constructors and destructors don't have return types. Use |
3410 | | // "void" instead. |
3411 | 407k | T = SemaRef.Context.VoidTy; |
3412 | 407k | processTypeAttrs(state, T, TAL_DeclSpec, |
3413 | 407k | D.getMutableDeclSpec().getAttributes()); |
3414 | 407k | break; |
3415 | | |
3416 | 900 | case UnqualifiedIdKind::IK_DeductionGuideName: |
3417 | | // Deduction guides have a trailing return type and no type in their |
3418 | | // decl-specifier sequence. Use a placeholder return type for now. |
3419 | 900 | T = SemaRef.Context.DependentTy; |
3420 | 900 | break; |
3421 | | |
3422 | 14.4k | case UnqualifiedIdKind::IK_ConversionFunctionId: |
3423 | | // The result type of a conversion function is the type that it |
3424 | | // converts to. |
3425 | 14.4k | T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId, |
3426 | 14.4k | &ReturnTypeInfo); |
3427 | 14.4k | break; |
3428 | 111M | } |
3429 | | |
3430 | | // Note: We don't need to distribute declaration attributes (i.e. |
3431 | | // D.getDeclarationAttributes()) because those are always C++11 attributes, |
3432 | | // and those don't get distributed. |
3433 | 111M | distributeTypeAttrsFromDeclarator(state, T); |
3434 | | |
3435 | | // Find the deduced type in this type. Look in the trailing return type if we |
3436 | | // have one, otherwise in the DeclSpec type. |
3437 | | // FIXME: The standard wording doesn't currently describe this. |
3438 | 111M | DeducedType *Deduced = T->getContainedDeducedType(); |
3439 | 111M | bool DeducedIsTrailingReturnType = false; |
3440 | 111M | if (Deduced && isa<AutoType>(Deduced)61.2k && D.hasTrailingReturnType()59.8k ) { |
3441 | 9.24k | QualType T = SemaRef.GetTypeFromParser(D.getTrailingReturnType()); |
3442 | 9.24k | Deduced = T.isNull() ? nullptr48 : T->getContainedDeducedType()9.20k ; |
3443 | 9.24k | DeducedIsTrailingReturnType = true; |
3444 | 9.24k | } |
3445 | | |
3446 | | // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context. |
3447 | 111M | if (Deduced) { |
3448 | 52.2k | AutoType *Auto = dyn_cast<AutoType>(Deduced); |
3449 | 52.2k | int Error = -1; |
3450 | | |
3451 | | // Is this a 'auto' or 'decltype(auto)' type (as opposed to __auto_type or |
3452 | | // class template argument deduction)? |
3453 | 52.2k | bool IsCXXAutoType = |
3454 | 52.2k | (Auto && Auto->getKeyword() != AutoTypeKeyword::GNUAutoType50.8k ); |
3455 | 52.2k | bool IsDeducedReturnType = false; |
3456 | | |
3457 | 52.2k | switch (D.getContext()) { |
3458 | 3.94k | case DeclaratorContext::LambdaExpr: |
3459 | | // Declared return type of a lambda-declarator is implicit and is always |
3460 | | // 'auto'. |
3461 | 3.94k | break; |
3462 | 0 | case DeclaratorContext::ObjCParameter: |
3463 | 0 | case DeclaratorContext::ObjCResult: |
3464 | 0 | Error = 0; |
3465 | 0 | break; |
3466 | 2 | case DeclaratorContext::RequiresExpr: |
3467 | 2 | Error = 22; |
3468 | 2 | break; |
3469 | 257 | case DeclaratorContext::Prototype: |
3470 | 2.65k | case DeclaratorContext::LambdaExprParameter: { |
3471 | 2.65k | InventedTemplateParameterInfo *Info = nullptr; |
3472 | 2.65k | if (D.getContext() == DeclaratorContext::Prototype) { |
3473 | | // With concepts we allow 'auto' in function parameters. |
3474 | 257 | if (!SemaRef.getLangOpts().CPlusPlus20 || !Auto244 || |
3475 | 257 | Auto->getKeyword() != AutoTypeKeyword::Auto243 ) { |
3476 | 18 | Error = 0; |
3477 | 18 | break; |
3478 | 239 | } else if (!SemaRef.getCurScope()->isFunctionDeclarationScope()) { |
3479 | 4 | Error = 21; |
3480 | 4 | break; |
3481 | 4 | } |
3482 | | |
3483 | 235 | Info = &SemaRef.InventedParameterInfos.back(); |
3484 | 2.40k | } else { |
3485 | | // In C++14, generic lambdas allow 'auto' in their parameters. |
3486 | 2.40k | if (!SemaRef.getLangOpts().CPlusPlus14 || !Auto2.39k || |
3487 | 2.40k | Auto->getKeyword() != AutoTypeKeyword::Auto2.39k ) { |
3488 | 4 | Error = 16; |
3489 | 4 | break; |
3490 | 4 | } |
3491 | 2.39k | Info = SemaRef.getCurLambda(); |
3492 | 2.39k | assert(Info && "No LambdaScopeInfo on the stack!"); |
3493 | 2.39k | } |
3494 | | |
3495 | | // We'll deal with inventing template parameters for 'auto' in trailing |
3496 | | // return types when we pick up the trailing return type when processing |
3497 | | // the function chunk. |
3498 | 2.63k | if (!DeducedIsTrailingReturnType) |
3499 | 2.62k | T = InventTemplateParameter(state, T, nullptr, Auto, *Info).first; |
3500 | 2.63k | break; |
3501 | 2.65k | } |
3502 | 1.40k | case DeclaratorContext::Member: { |
3503 | 1.40k | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static || |
3504 | 1.40k | D.isFunctionDeclarator()894 ) |
3505 | 1.38k | break; |
3506 | 21 | bool Cxx = SemaRef.getLangOpts().CPlusPlus; |
3507 | 21 | if (isa<ObjCContainerDecl>(SemaRef.CurContext)) { |
3508 | 4 | Error = 6; // Interface member. |
3509 | 17 | } else { |
3510 | 17 | switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) { |
3511 | 0 | case TTK_Enum: llvm_unreachable("unhandled tag kind"); |
3512 | 13 | case TTK_Struct: Error = Cxx ? 112 : 21 ; /* Struct member */ break; |
3513 | 0 | case TTK_Union: Error = Cxx ? 3 : 4; /* Union member */ break; |
3514 | 4 | case TTK_Class: Error = 5; /* Class member */ break; |
3515 | 0 | case TTK_Interface: Error = 6; /* Interface member */ break; |
3516 | 17 | } |
3517 | 17 | } |
3518 | 21 | if (D.getDeclSpec().isFriendSpecified()) |
3519 | 6 | Error = 20; // Friend type |
3520 | 21 | break; |
3521 | 21 | } |
3522 | 8 | case DeclaratorContext::CXXCatch: |
3523 | 9 | case DeclaratorContext::ObjCCatch: |
3524 | 9 | Error = 7; // Exception declaration |
3525 | 9 | break; |
3526 | 216 | case DeclaratorContext::TemplateParam: |
3527 | 216 | if (isa<DeducedTemplateSpecializationType>(Deduced) && |
3528 | 216 | !SemaRef.getLangOpts().CPlusPlus2019 ) |
3529 | 2 | Error = 19; // Template parameter (until C++20) |
3530 | 214 | else if (!SemaRef.getLangOpts().CPlusPlus17) |
3531 | 5 | Error = 8; // Template parameter (until C++17) |
3532 | 216 | break; |
3533 | 0 | case DeclaratorContext::BlockLiteral: |
3534 | 0 | Error = 9; // Block literal |
3535 | 0 | break; |
3536 | 786 | case DeclaratorContext::TemplateArg: |
3537 | | // Within a template argument list, a deduced template specialization |
3538 | | // type will be reinterpreted as a template template argument. |
3539 | 786 | if (isa<DeducedTemplateSpecializationType>(Deduced) && |
3540 | 786 | !D.getNumTypeObjects()774 && |
3541 | 786 | D.getDeclSpec().getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier773 ) |
3542 | 772 | break; |
3543 | 786 | LLVM_FALLTHROUGH14 ;14 |
3544 | 15 | case DeclaratorContext::TemplateTypeArg: |
3545 | 15 | Error = 10; // Template type argument |
3546 | 15 | break; |
3547 | 13 | case DeclaratorContext::AliasDecl: |
3548 | 13 | case DeclaratorContext::AliasTemplate: |
3549 | 13 | Error = 12; // Type alias |
3550 | 13 | break; |
3551 | 214 | case DeclaratorContext::TrailingReturn: |
3552 | 261 | case DeclaratorContext::TrailingReturnVar: |
3553 | 261 | if (!SemaRef.getLangOpts().CPlusPlus14 || !IsCXXAutoType253 ) |
3554 | 9 | Error = 13; // Function return type |
3555 | 261 | IsDeducedReturnType = true; |
3556 | 261 | break; |
3557 | 140 | case DeclaratorContext::ConversionId: |
3558 | 140 | if (!SemaRef.getLangOpts().CPlusPlus14 || !IsCXXAutoType138 ) |
3559 | 6 | Error = 14; // conversion-type-id |
3560 | 140 | IsDeducedReturnType = true; |
3561 | 140 | break; |
3562 | 221 | case DeclaratorContext::FunctionalCast: |
3563 | 221 | if (isa<DeducedTemplateSpecializationType>(Deduced)) |
3564 | 169 | break; |
3565 | 52 | if (SemaRef.getLangOpts().CPlusPlus2b && IsCXXAutoType48 && |
3566 | 52 | !Auto->isDecltypeAuto()48 ) |
3567 | 48 | break; // auto(x) |
3568 | 52 | LLVM_FALLTHROUGH4 ;4 |
3569 | 43 | case DeclaratorContext::TypeName: |
3570 | 43 | case DeclaratorContext::Association: |
3571 | 43 | Error = 15; // Generic |
3572 | 43 | break; |
3573 | 4.48k | case DeclaratorContext::File: |
3574 | 41.6k | case DeclaratorContext::Block: |
3575 | 42.3k | case DeclaratorContext::ForInit: |
3576 | 42.3k | case DeclaratorContext::SelectionInit: |
3577 | 42.4k | case DeclaratorContext::Condition: |
3578 | | // FIXME: P0091R3 (erroneously) does not permit class template argument |
3579 | | // deduction in conditions, for-init-statements, and other declarations |
3580 | | // that are not simple-declarations. |
3581 | 42.4k | break; |
3582 | 93 | case DeclaratorContext::CXXNew: |
3583 | | // FIXME: P0091R3 does not permit class template argument deduction here, |
3584 | | // but we follow GCC and allow it anyway. |
3585 | 93 | if (!IsCXXAutoType && !isa<DeducedTemplateSpecializationType>(Deduced)18 ) |
3586 | 1 | Error = 17; // 'new' type |
3587 | 93 | break; |
3588 | 1 | case DeclaratorContext::KNRTypeList: |
3589 | 1 | Error = 18; // K&R function parameter |
3590 | 1 | break; |
3591 | 52.2k | } |
3592 | | |
3593 | 52.2k | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
3594 | 10 | Error = 11; |
3595 | | |
3596 | | // In Objective-C it is an error to use 'auto' on a function declarator |
3597 | | // (and everywhere for '__auto_type'). |
3598 | 52.2k | if (D.isFunctionDeclarator() && |
3599 | 52.2k | (6.58k !SemaRef.getLangOpts().CPlusPlus116.58k || !IsCXXAutoType6.58k )) |
3600 | 7 | Error = 13; |
3601 | | |
3602 | 52.2k | SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc(); |
3603 | 52.2k | if (D.getName().getKind() == UnqualifiedIdKind::IK_ConversionFunctionId) |
3604 | 62 | AutoRange = D.getName().getSourceRange(); |
3605 | | |
3606 | 52.2k | if (Error != -1) { |
3607 | 169 | unsigned Kind; |
3608 | 169 | if (Auto) { |
3609 | 126 | switch (Auto->getKeyword()) { |
3610 | 102 | case AutoTypeKeyword::Auto: Kind = 0; break; |
3611 | 14 | case AutoTypeKeyword::DecltypeAuto: Kind = 1; break; |
3612 | 10 | case AutoTypeKeyword::GNUAutoType: Kind = 2; break; |
3613 | 126 | } |
3614 | 126 | } else { |
3615 | 43 | assert(isa<DeducedTemplateSpecializationType>(Deduced) && |
3616 | 43 | "unknown auto type"); |
3617 | 0 | Kind = 3; |
3618 | 43 | } |
3619 | | |
3620 | 169 | auto *DTST = dyn_cast<DeducedTemplateSpecializationType>(Deduced); |
3621 | 169 | TemplateName TN = DTST ? DTST->getTemplateName()43 : TemplateName()126 ; |
3622 | | |
3623 | 169 | SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed) |
3624 | 169 | << Kind << Error << (int)SemaRef.getTemplateNameKindForDiagnostics(TN) |
3625 | 169 | << QualType(Deduced, 0) << AutoRange; |
3626 | 169 | if (auto *TD = TN.getAsTemplateDecl()) |
3627 | 43 | SemaRef.Diag(TD->getLocation(), diag::note_template_decl_here); |
3628 | | |
3629 | 169 | T = SemaRef.Context.IntTy; |
3630 | 169 | D.setInvalidType(true); |
3631 | 52.0k | } else if (Auto && D.getContext() != DeclaratorContext::LambdaExpr50.7k ) { |
3632 | | // If there was a trailing return type, we already got |
3633 | | // warn_cxx98_compat_trailing_return_type in the parser. |
3634 | 46.7k | SemaRef.Diag(AutoRange.getBegin(), |
3635 | 46.7k | D.getContext() == DeclaratorContext::LambdaExprParameter |
3636 | 46.7k | ? diag::warn_cxx11_compat_generic_lambda2.39k |
3637 | 46.7k | : IsDeducedReturnType44.3k |
3638 | 44.3k | ? diag::warn_cxx11_compat_deduced_return_type386 |
3639 | 44.3k | : diag::warn_cxx98_compat_auto_type_specifier43.9k ) |
3640 | 46.7k | << AutoRange; |
3641 | 46.7k | } |
3642 | 52.2k | } |
3643 | | |
3644 | 111M | if (SemaRef.getLangOpts().CPlusPlus && |
3645 | 111M | OwnedTagDecl49.7M && OwnedTagDecl->isCompleteDefinition()91.9k ) { |
3646 | | // Check the contexts where C++ forbids the declaration of a new class |
3647 | | // or enumeration in a type-specifier-seq. |
3648 | 71.6k | unsigned DiagID = 0; |
3649 | 71.6k | switch (D.getContext()) { |
3650 | 0 | case DeclaratorContext::TrailingReturn: |
3651 | 0 | case DeclaratorContext::TrailingReturnVar: |
3652 | | // Class and enumeration definitions are syntactically not allowed in |
3653 | | // trailing return types. |
3654 | 0 | llvm_unreachable("parser should not have allowed this"); |
3655 | 0 | break; |
3656 | 57.5k | case DeclaratorContext::File: |
3657 | 66.1k | case DeclaratorContext::Member: |
3658 | 71.5k | case DeclaratorContext::Block: |
3659 | 71.5k | case DeclaratorContext::ForInit: |
3660 | 71.5k | case DeclaratorContext::SelectionInit: |
3661 | 71.5k | case DeclaratorContext::BlockLiteral: |
3662 | 71.5k | case DeclaratorContext::LambdaExpr: |
3663 | | // C++11 [dcl.type]p3: |
3664 | | // A type-specifier-seq shall not define a class or enumeration unless |
3665 | | // it appears in the type-id of an alias-declaration (7.1.3) that is not |
3666 | | // the declaration of a template-declaration. |
3667 | 71.6k | case DeclaratorContext::AliasDecl: |
3668 | 71.6k | break; |
3669 | 5 | case DeclaratorContext::AliasTemplate: |
3670 | 5 | DiagID = diag::err_type_defined_in_alias_template; |
3671 | 5 | break; |
3672 | 43 | case DeclaratorContext::TypeName: |
3673 | 43 | case DeclaratorContext::FunctionalCast: |
3674 | 43 | case DeclaratorContext::ConversionId: |
3675 | 43 | case DeclaratorContext::TemplateParam: |
3676 | 43 | case DeclaratorContext::CXXNew: |
3677 | 43 | case DeclaratorContext::CXXCatch: |
3678 | 43 | case DeclaratorContext::ObjCCatch: |
3679 | 43 | case DeclaratorContext::TemplateArg: |
3680 | 43 | case DeclaratorContext::TemplateTypeArg: |
3681 | 44 | case DeclaratorContext::Association: |
3682 | 44 | DiagID = diag::err_type_defined_in_type_specifier; |
3683 | 44 | break; |
3684 | 5 | case DeclaratorContext::Prototype: |
3685 | 5 | case DeclaratorContext::LambdaExprParameter: |
3686 | 5 | case DeclaratorContext::ObjCParameter: |
3687 | 6 | case DeclaratorContext::ObjCResult: |
3688 | 6 | case DeclaratorContext::KNRTypeList: |
3689 | 6 | case DeclaratorContext::RequiresExpr: |
3690 | | // C++ [dcl.fct]p6: |
3691 | | // Types shall not be defined in return or parameter types. |
3692 | 6 | DiagID = diag::err_type_defined_in_param_type; |
3693 | 6 | break; |
3694 | 15 | case DeclaratorContext::Condition: |
3695 | | // C++ 6.4p2: |
3696 | | // The type-specifier-seq shall not contain typedef and shall not declare |
3697 | | // a new class or enumeration. |
3698 | 15 | DiagID = diag::err_type_defined_in_condition; |
3699 | 15 | break; |
3700 | 71.6k | } |
3701 | | |
3702 | 71.6k | if (DiagID != 0) { |
3703 | 70 | SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID) |
3704 | 70 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
3705 | 70 | D.setInvalidType(true); |
3706 | 70 | } |
3707 | 71.6k | } |
3708 | | |
3709 | 111M | assert(!T.isNull() && "This function should not return a null type"); |
3710 | 0 | return T; |
3711 | 111M | } |
3712 | | |
3713 | | /// Produce an appropriate diagnostic for an ambiguity between a function |
3714 | | /// declarator and a C++ direct-initializer. |
3715 | | static void warnAboutAmbiguousFunction(Sema &S, Declarator &D, |
3716 | 17.9k | DeclaratorChunk &DeclType, QualType RT) { |
3717 | 17.9k | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
3718 | 17.9k | assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity"); |
3719 | | |
3720 | | // If the return type is void there is no ambiguity. |
3721 | 17.9k | if (RT->isVoidType()) |
3722 | 9.77k | return; |
3723 | | |
3724 | | // An initializer for a non-class type can have at most one argument. |
3725 | 8.18k | if (!RT->isRecordType() && FTI.NumParams > 16.81k ) |
3726 | 462 | return; |
3727 | | |
3728 | | // An initializer for a reference must have exactly one argument. |
3729 | 7.72k | if (RT->isReferenceType() && FTI.NumParams != 1502 ) |
3730 | 443 | return; |
3731 | | |
3732 | | // Only warn if this declarator is declaring a function at block scope, and |
3733 | | // doesn't have a storage class (such as 'extern') specified. |
3734 | 7.28k | if (!D.isFunctionDeclarator() || |
3735 | 7.28k | D.getFunctionDefinitionKind() != FunctionDefinitionKind::Declaration7.22k || |
3736 | 7.28k | !S.CurContext->isFunctionOrMethod()7.19k || |
3737 | 7.28k | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_unspecified142 ) |
3738 | 7.14k | return; |
3739 | | |
3740 | | // Inside a condition, a direct initializer is not permitted. We allow one to |
3741 | | // be parsed in order to give better diagnostics in condition parsing. |
3742 | 136 | if (D.getContext() == DeclaratorContext::Condition) |
3743 | 4 | return; |
3744 | | |
3745 | 132 | SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc); |
3746 | | |
3747 | 132 | S.Diag(DeclType.Loc, |
3748 | 132 | FTI.NumParams ? diag::warn_parens_disambiguated_as_function_declaration24 |
3749 | 132 | : diag::warn_empty_parens_are_function_decl108 ) |
3750 | 132 | << ParenRange; |
3751 | | |
3752 | | // If the declaration looks like: |
3753 | | // T var1, |
3754 | | // f(); |
3755 | | // and name lookup finds a function named 'f', then the ',' was |
3756 | | // probably intended to be a ';'. |
3757 | 132 | if (!D.isFirstDeclarator() && D.getIdentifier()19 ) { |
3758 | 19 | FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr); |
3759 | 19 | FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr); |
3760 | 19 | if (Comma.getFileID() != Name.getFileID() || |
3761 | 19 | Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) { |
3762 | 19 | LookupResult Result(S, D.getIdentifier(), SourceLocation(), |
3763 | 19 | Sema::LookupOrdinaryName); |
3764 | 19 | if (S.LookupName(Result, S.getCurScope())) |
3765 | 13 | S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call) |
3766 | 13 | << FixItHint::CreateReplacement(D.getCommaLoc(), ";") |
3767 | 13 | << D.getIdentifier(); |
3768 | 19 | Result.suppressDiagnostics(); |
3769 | 19 | } |
3770 | 19 | } |
3771 | | |
3772 | 132 | if (FTI.NumParams > 0) { |
3773 | | // For a declaration with parameters, eg. "T var(T());", suggest adding |
3774 | | // parens around the first parameter to turn the declaration into a |
3775 | | // variable declaration. |
3776 | 24 | SourceRange Range = FTI.Params[0].Param->getSourceRange(); |
3777 | 24 | SourceLocation B = Range.getBegin(); |
3778 | 24 | SourceLocation E = S.getLocForEndOfToken(Range.getEnd()); |
3779 | | // FIXME: Maybe we should suggest adding braces instead of parens |
3780 | | // in C++11 for classes that don't have an initializer_list constructor. |
3781 | 24 | S.Diag(B, diag::note_additional_parens_for_variable_declaration) |
3782 | 24 | << FixItHint::CreateInsertion(B, "(") |
3783 | 24 | << FixItHint::CreateInsertion(E, ")"); |
3784 | 108 | } else { |
3785 | | // For a declaration without parameters, eg. "T var();", suggest replacing |
3786 | | // the parens with an initializer to turn the declaration into a variable |
3787 | | // declaration. |
3788 | 108 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
3789 | | |
3790 | | // Empty parens mean value-initialization, and no parens mean |
3791 | | // default initialization. These are equivalent if the default |
3792 | | // constructor is user-provided or if zero-initialization is a |
3793 | | // no-op. |
3794 | 108 | if (RD && RD->hasDefinition()31 && |
3795 | 108 | (27 RD->isEmpty()27 || RD->hasUserProvidedDefaultConstructor()18 )) |
3796 | 13 | S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor) |
3797 | 13 | << FixItHint::CreateRemoval(ParenRange); |
3798 | 95 | else { |
3799 | 95 | std::string Init = |
3800 | 95 | S.getFixItZeroInitializerForType(RT, ParenRange.getBegin()); |
3801 | 95 | if (Init.empty() && S.LangOpts.CPlusPlus1112 ) |
3802 | 9 | Init = "{}"; |
3803 | 95 | if (!Init.empty()) |
3804 | 92 | S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize) |
3805 | 92 | << FixItHint::CreateReplacement(ParenRange, Init); |
3806 | 95 | } |
3807 | 108 | } |
3808 | 132 | } |
3809 | | |
3810 | | /// Produce an appropriate diagnostic for a declarator with top-level |
3811 | | /// parentheses. |
3812 | 452 | static void warnAboutRedundantParens(Sema &S, Declarator &D, QualType T) { |
3813 | 452 | DeclaratorChunk &Paren = D.getTypeObject(D.getNumTypeObjects() - 1); |
3814 | 452 | assert(Paren.Kind == DeclaratorChunk::Paren && |
3815 | 452 | "do not have redundant top-level parentheses"); |
3816 | | |
3817 | | // This is a syntactic check; we're not interested in cases that arise |
3818 | | // during template instantiation. |
3819 | 452 | if (S.inTemplateInstantiation()) |
3820 | 0 | return; |
3821 | | |
3822 | | // Check whether this could be intended to be a construction of a temporary |
3823 | | // object in C++ via a function-style cast. |
3824 | 452 | bool CouldBeTemporaryObject = |
3825 | 452 | S.getLangOpts().CPlusPlus && D.isExpressionContext()421 && |
3826 | 452 | !D.isInvalidType()142 && D.getIdentifier()126 && |
3827 | 452 | D.getDeclSpec().getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier120 && |
3828 | 452 | (109 T->isRecordType()109 || T->isDependentType()64 ) && |
3829 | 452 | D.getDeclSpec().getTypeQualifiers() == 046 && D.isFirstDeclarator()46 ; |
3830 | | |
3831 | 452 | bool StartsWithDeclaratorId = true; |
3832 | 742 | for (auto &C : D.type_objects()) { |
3833 | 742 | switch (C.Kind) { |
3834 | 511 | case DeclaratorChunk::Paren: |
3835 | 511 | if (&C == &Paren) |
3836 | 452 | continue; |
3837 | 511 | LLVM_FALLTHROUGH59 ;59 |
3838 | 94 | case DeclaratorChunk::Pointer: |
3839 | 94 | StartsWithDeclaratorId = false; |
3840 | 94 | continue; |
3841 | | |
3842 | 18 | case DeclaratorChunk::Array: |
3843 | 18 | if (!C.Arr.NumElts) |
3844 | 0 | CouldBeTemporaryObject = false; |
3845 | 18 | continue; |
3846 | | |
3847 | 9 | case DeclaratorChunk::Reference: |
3848 | | // FIXME: Suppress the warning here if there is no initializer; we're |
3849 | | // going to give an error anyway. |
3850 | | // We assume that something like 'T (&x) = y;' is highly likely to not |
3851 | | // be intended to be a temporary object. |
3852 | 9 | CouldBeTemporaryObject = false; |
3853 | 9 | StartsWithDeclaratorId = false; |
3854 | 9 | continue; |
3855 | | |
3856 | 55 | case DeclaratorChunk::Function: |
3857 | | // In a new-type-id, function chunks require parentheses. |
3858 | 55 | if (D.getContext() == DeclaratorContext::CXXNew) |
3859 | 0 | return; |
3860 | | // FIXME: "A(f())" deserves a vexing-parse warning, not just a |
3861 | | // redundant-parens warning, but we don't know whether the function |
3862 | | // chunk was syntactically valid as an expression here. |
3863 | 55 | CouldBeTemporaryObject = false; |
3864 | 55 | continue; |
3865 | | |
3866 | 0 | case DeclaratorChunk::BlockPointer: |
3867 | 114 | case DeclaratorChunk::MemberPointer: |
3868 | 114 | case DeclaratorChunk::Pipe: |
3869 | | // These cannot appear in expressions. |
3870 | 114 | CouldBeTemporaryObject = false; |
3871 | 114 | StartsWithDeclaratorId = false; |
3872 | 114 | continue; |
3873 | 742 | } |
3874 | 742 | } |
3875 | | |
3876 | | // FIXME: If there is an initializer, assume that this is not intended to be |
3877 | | // a construction of a temporary object. |
3878 | | |
3879 | | // Check whether the name has already been declared; if not, this is not a |
3880 | | // function-style cast. |
3881 | 452 | if (CouldBeTemporaryObject) { |
3882 | 33 | LookupResult Result(S, D.getIdentifier(), SourceLocation(), |
3883 | 33 | Sema::LookupOrdinaryName); |
3884 | 33 | if (!S.LookupName(Result, S.getCurScope())) |
3885 | 18 | CouldBeTemporaryObject = false; |
3886 | 33 | Result.suppressDiagnostics(); |
3887 | 33 | } |
3888 | | |
3889 | 452 | SourceRange ParenRange(Paren.Loc, Paren.EndLoc); |
3890 | | |
3891 | 452 | if (!CouldBeTemporaryObject) { |
3892 | | // If we have A (::B), the parentheses affect the meaning of the program. |
3893 | | // Suppress the warning in that case. Don't bother looking at the DeclSpec |
3894 | | // here: even (e.g.) "int ::x" is visually ambiguous even though it's |
3895 | | // formally unambiguous. |
3896 | 437 | if (StartsWithDeclaratorId && D.getCXXScopeSpec().isValid()263 ) { |
3897 | 63 | for (NestedNameSpecifier *NNS = D.getCXXScopeSpec().getScopeRep(); NNS; |
3898 | 51 | NNS = NNS->getPrefix()39 ) { |
3899 | 51 | if (NNS->getKind() == NestedNameSpecifier::Global) |
3900 | 12 | return; |
3901 | 51 | } |
3902 | 24 | } |
3903 | | |
3904 | 425 | S.Diag(Paren.Loc, diag::warn_redundant_parens_around_declarator) |
3905 | 425 | << ParenRange << FixItHint::CreateRemoval(Paren.Loc) |
3906 | 425 | << FixItHint::CreateRemoval(Paren.EndLoc); |
3907 | 425 | return; |
3908 | 437 | } |
3909 | | |
3910 | 15 | S.Diag(Paren.Loc, diag::warn_parens_disambiguated_as_variable_declaration) |
3911 | 15 | << ParenRange << D.getIdentifier(); |
3912 | 15 | auto *RD = T->getAsCXXRecordDecl(); |
3913 | 15 | if (!RD || !RD->hasDefinition()14 || RD->hasNonTrivialDestructor()14 ) |
3914 | 7 | S.Diag(Paren.Loc, diag::note_raii_guard_add_name) |
3915 | 7 | << FixItHint::CreateInsertion(Paren.Loc, " varname") << T |
3916 | 7 | << D.getIdentifier(); |
3917 | | // FIXME: A cast to void is probably a better suggestion in cases where it's |
3918 | | // valid (when there is no initializer and we're not in a condition). |
3919 | 15 | S.Diag(D.getBeginLoc(), diag::note_function_style_cast_add_parentheses) |
3920 | 15 | << FixItHint::CreateInsertion(D.getBeginLoc(), "(") |
3921 | 15 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(D.getEndLoc()), ")"); |
3922 | 15 | S.Diag(Paren.Loc, diag::note_remove_parens_for_variable_declaration) |
3923 | 15 | << FixItHint::CreateRemoval(Paren.Loc) |
3924 | 15 | << FixItHint::CreateRemoval(Paren.EndLoc); |
3925 | 15 | } |
3926 | | |
3927 | | /// Helper for figuring out the default CC for a function declarator type. If |
3928 | | /// this is the outermost chunk, then we can determine the CC from the |
3929 | | /// declarator context. If not, then this could be either a member function |
3930 | | /// type or normal function type. |
3931 | | static CallingConv getCCForDeclaratorChunk( |
3932 | | Sema &S, Declarator &D, const ParsedAttributesView &AttrList, |
3933 | 25.3M | const DeclaratorChunk::FunctionTypeInfo &FTI, unsigned ChunkIndex) { |
3934 | 25.3M | assert(D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function); |
3935 | | |
3936 | | // Check for an explicit CC attribute. |
3937 | 62.9k | for (const ParsedAttr &AL : AttrList) { |
3938 | 62.9k | switch (AL.getKind()) { |
3939 | 35.3k | CALLING_CONV_ATTRS_CASELIST375 : { |
3940 | | // Ignore attributes that don't validate or can't apply to the |
3941 | | // function type. We'll diagnose the failure to apply them in |
3942 | | // handleFunctionTypeAttr. |
3943 | 35.3k | CallingConv CC; |
3944 | 35.3k | if (!S.CheckCallingConvAttr(AL, CC)2.75k && |
3945 | 2.75k | (2.71k !FTI.isVariadic2.71k || supportsVariadicCall(CC)80 )) { |
3946 | 2.69k | return CC; |
3947 | 2.69k | } |
3948 | 59 | break; |
3949 | 35.3k | } |
3950 | | |
3951 | 60.2k | default: |
3952 | 60.2k | break; |
3953 | 62.9k | } |
3954 | 62.9k | } |
3955 | | |
3956 | 25.3M | bool IsCXXInstanceMethod = false; |
3957 | | |
3958 | 25.3M | if (S.getLangOpts().CPlusPlus) { |
3959 | | // Look inwards through parentheses to see if this chunk will form a |
3960 | | // member pointer type or if we're the declarator. Any type attributes |
3961 | | // between here and there will override the CC we choose here. |
3962 | 11.3M | unsigned I = ChunkIndex; |
3963 | 11.3M | bool FoundNonParen = false; |
3964 | 11.5M | while (I && !FoundNonParen246k ) { |
3965 | 244k | --I; |
3966 | 244k | if (D.getTypeObject(I).Kind != DeclaratorChunk::Paren) |
3967 | 116k | FoundNonParen = true; |
3968 | 244k | } |
3969 | | |
3970 | 11.3M | if (FoundNonParen) { |
3971 | | // If we're not the declarator, we're a regular function type unless we're |
3972 | | // in a member pointer. |
3973 | 116k | IsCXXInstanceMethod = |
3974 | 116k | D.getTypeObject(I).Kind == DeclaratorChunk::MemberPointer; |
3975 | 11.1M | } else if (D.getContext() == DeclaratorContext::LambdaExpr) { |
3976 | | // This can only be a call operator for a lambda, which is an instance |
3977 | | // method. |
3978 | 6.72k | IsCXXInstanceMethod = true; |
3979 | 11.1M | } else { |
3980 | | // We're the innermost decl chunk, so must be a function declarator. |
3981 | 11.1M | assert(D.isFunctionDeclarator()); |
3982 | | |
3983 | | // If we're inside a record, we're declaring a method, but it could be |
3984 | | // explicitly or implicitly static. |
3985 | 0 | IsCXXInstanceMethod = |
3986 | 11.1M | D.isFirstDeclarationOfMember() && |
3987 | 11.1M | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef1.33M && |
3988 | 11.1M | !D.isStaticMember()1.32M ; |
3989 | 11.1M | } |
3990 | 11.3M | } |
3991 | | |
3992 | 0 | CallingConv CC = S.Context.getDefaultCallingConvention(FTI.isVariadic, |
3993 | 25.3M | IsCXXInstanceMethod); |
3994 | | |
3995 | | // Attribute AT_OpenCLKernel affects the calling convention for SPIR |
3996 | | // and AMDGPU targets, hence it cannot be treated as a calling |
3997 | | // convention attribute. This is the simplest place to infer |
3998 | | // calling convention for OpenCL kernels. |
3999 | 25.3M | if (S.getLangOpts().OpenCL) { |
4000 | 431k | for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) { |
4001 | 431k | if (AL.getKind() == ParsedAttr::AT_OpenCLKernel) { |
4002 | 1.23k | CC = CC_OpenCLKernel; |
4003 | 1.23k | break; |
4004 | 1.23k | } |
4005 | 431k | } |
4006 | 25.1M | } else if (S.getLangOpts().CUDA) { |
4007 | | // If we're compiling CUDA/HIP code and targeting SPIR-V we need to make |
4008 | | // sure the kernels will be marked with the right calling convention so that |
4009 | | // they will be visible by the APIs that ingest SPIR-V. |
4010 | 8.00k | llvm::Triple Triple = S.Context.getTargetInfo().getTriple(); |
4011 | 8.00k | if (Triple.getArch() == llvm::Triple::spirv32 || |
4012 | 8.00k | Triple.getArch() == llvm::Triple::spirv647.99k ) { |
4013 | 11 | for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) { |
4014 | 11 | if (AL.getKind() == ParsedAttr::AT_CUDAGlobal) { |
4015 | 5 | CC = CC_OpenCLKernel; |
4016 | 5 | break; |
4017 | 5 | } |
4018 | 11 | } |
4019 | 11 | } |
4020 | 8.00k | } |
4021 | | |
4022 | 25.3M | return CC; |
4023 | 25.3M | } |
4024 | | |
4025 | | namespace { |
4026 | | /// A simple notion of pointer kinds, which matches up with the various |
4027 | | /// pointer declarators. |
4028 | | enum class SimplePointerKind { |
4029 | | Pointer, |
4030 | | BlockPointer, |
4031 | | MemberPointer, |
4032 | | Array, |
4033 | | }; |
4034 | | } // end anonymous namespace |
4035 | | |
4036 | 2.55M | IdentifierInfo *Sema::getNullabilityKeyword(NullabilityKind nullability) { |
4037 | 2.55M | switch (nullability) { |
4038 | 2.09M | case NullabilityKind::NonNull: |
4039 | 2.09M | if (!Ident__Nonnull) |
4040 | 1.04k | Ident__Nonnull = PP.getIdentifierInfo("_Nonnull"); |
4041 | 2.09M | return Ident__Nonnull; |
4042 | | |
4043 | 445k | case NullabilityKind::Nullable: |
4044 | 445k | if (!Ident__Nullable) |
4045 | 443 | Ident__Nullable = PP.getIdentifierInfo("_Nullable"); |
4046 | 445k | return Ident__Nullable; |
4047 | | |
4048 | 0 | case NullabilityKind::NullableResult: |
4049 | 0 | if (!Ident__Nullable_result) |
4050 | 0 | Ident__Nullable_result = PP.getIdentifierInfo("_Nullable_result"); |
4051 | 0 | return Ident__Nullable_result; |
4052 | | |
4053 | 14.9k | case NullabilityKind::Unspecified: |
4054 | 14.9k | if (!Ident__Null_unspecified) |
4055 | 233 | Ident__Null_unspecified = PP.getIdentifierInfo("_Null_unspecified"); |
4056 | 14.9k | return Ident__Null_unspecified; |
4057 | 2.55M | } |
4058 | 0 | llvm_unreachable("Unknown nullability kind."); |
4059 | 0 | } |
4060 | | |
4061 | | /// Retrieve the identifier "NSError". |
4062 | 1.11M | IdentifierInfo *Sema::getNSErrorIdent() { |
4063 | 1.11M | if (!Ident_NSError) |
4064 | 2.67k | Ident_NSError = PP.getIdentifierInfo("NSError"); |
4065 | | |
4066 | 1.11M | return Ident_NSError; |
4067 | 1.11M | } |
4068 | | |
4069 | | /// Check whether there is a nullability attribute of any kind in the given |
4070 | | /// attribute list. |
4071 | 10.7M | static bool hasNullabilityAttr(const ParsedAttributesView &attrs) { |
4072 | 10.7M | for (const ParsedAttr &AL : attrs) { |
4073 | 871k | if (AL.getKind() == ParsedAttr::AT_TypeNonNull || |
4074 | 871k | AL.getKind() == ParsedAttr::AT_TypeNullable802k || |
4075 | 871k | AL.getKind() == ParsedAttr::AT_TypeNullableResult356k || |
4076 | 871k | AL.getKind() == ParsedAttr::AT_TypeNullUnspecified356k ) |
4077 | 538k | return true; |
4078 | 871k | } |
4079 | | |
4080 | 10.2M | return false; |
4081 | 10.7M | } |
4082 | | |
4083 | | namespace { |
4084 | | /// Describes the kind of a pointer a declarator describes. |
4085 | | enum class PointerDeclaratorKind { |
4086 | | // Not a pointer. |
4087 | | NonPointer, |
4088 | | // Single-level pointer. |
4089 | | SingleLevelPointer, |
4090 | | // Multi-level pointer (of any pointer kind). |
4091 | | MultiLevelPointer, |
4092 | | // CFFooRef* |
4093 | | MaybePointerToCFRef, |
4094 | | // CFErrorRef* |
4095 | | CFErrorRefPointer, |
4096 | | // NSError** |
4097 | | NSErrorPointerPointer, |
4098 | | }; |
4099 | | |
4100 | | /// Describes a declarator chunk wrapping a pointer that marks inference as |
4101 | | /// unexpected. |
4102 | | // These values must be kept in sync with diagnostics. |
4103 | | enum class PointerWrappingDeclaratorKind { |
4104 | | /// Pointer is top-level. |
4105 | | None = -1, |
4106 | | /// Pointer is an array element. |
4107 | | Array = 0, |
4108 | | /// Pointer is the referent type of a C++ reference. |
4109 | | Reference = 1 |
4110 | | }; |
4111 | | } // end anonymous namespace |
4112 | | |
4113 | | /// Classify the given declarator, whose type-specified is \c type, based on |
4114 | | /// what kind of pointer it refers to. |
4115 | | /// |
4116 | | /// This is used to determine the default nullability. |
4117 | | static PointerDeclaratorKind |
4118 | | classifyPointerDeclarator(Sema &S, QualType type, Declarator &declarator, |
4119 | 96.5M | PointerWrappingDeclaratorKind &wrappingKind) { |
4120 | 96.5M | unsigned numNormalPointers = 0; |
4121 | | |
4122 | | // For any dependent type, we consider it a non-pointer. |
4123 | 96.5M | if (type->isDependentType()) |
4124 | 3.19M | return PointerDeclaratorKind::NonPointer; |
4125 | | |
4126 | | // Look through the declarator chunks to identify pointers. |
4127 | 125M | for (unsigned i = 0, n = declarator.getNumTypeObjects(); 93.3M i != n; ++i31.6M ) { |
4128 | 31.6M | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
4129 | 31.6M | switch (chunk.Kind) { |
4130 | 246k | case DeclaratorChunk::Array: |
4131 | 246k | if (numNormalPointers == 0) |
4132 | 245k | wrappingKind = PointerWrappingDeclaratorKind::Array; |
4133 | 246k | break; |
4134 | | |
4135 | 24.3M | case DeclaratorChunk::Function: |
4136 | 24.3M | case DeclaratorChunk::Pipe: |
4137 | 24.3M | break; |
4138 | | |
4139 | 48.8k | case DeclaratorChunk::BlockPointer: |
4140 | 50.8k | case DeclaratorChunk::MemberPointer: |
4141 | 50.8k | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer58 |
4142 | 50.8k | : PointerDeclaratorKind::SingleLevelPointer50.8k ; |
4143 | | |
4144 | 122k | case DeclaratorChunk::Paren: |
4145 | 122k | break; |
4146 | | |
4147 | 239k | case DeclaratorChunk::Reference: |
4148 | 239k | if (numNormalPointers == 0) |
4149 | 239k | wrappingKind = PointerWrappingDeclaratorKind::Reference; |
4150 | 239k | break; |
4151 | | |
4152 | 6.70M | case DeclaratorChunk::Pointer: |
4153 | 6.70M | ++numNormalPointers; |
4154 | 6.70M | if (numNormalPointers > 2) |
4155 | 565 | return PointerDeclaratorKind::MultiLevelPointer; |
4156 | 6.70M | break; |
4157 | 31.6M | } |
4158 | 31.6M | } |
4159 | | |
4160 | | // Then, dig into the type specifier itself. |
4161 | 93.3M | unsigned numTypeSpecifierPointers = 0; |
4162 | 95.7M | do { |
4163 | | // Decompose normal pointers. |
4164 | 95.7M | if (auto ptrType = type->getAs<PointerType>()) { |
4165 | 2.36M | ++numNormalPointers; |
4166 | | |
4167 | 2.36M | if (numNormalPointers > 2) |
4168 | 4.91k | return PointerDeclaratorKind::MultiLevelPointer; |
4169 | | |
4170 | 2.35M | type = ptrType->getPointeeType(); |
4171 | 2.35M | ++numTypeSpecifierPointers; |
4172 | 2.35M | continue; |
4173 | 2.36M | } |
4174 | | |
4175 | | // Decompose block pointers. |
4176 | 93.3M | if (type->getAs<BlockPointerType>()) { |
4177 | 22.7k | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer14 |
4178 | 22.7k | : PointerDeclaratorKind::SingleLevelPointer22.7k ; |
4179 | 22.7k | } |
4180 | | |
4181 | | // Decompose member pointers. |
4182 | 93.3M | if (type->getAs<MemberPointerType>()) { |
4183 | 797 | return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer9 |
4184 | 797 | : PointerDeclaratorKind::SingleLevelPointer788 ; |
4185 | 797 | } |
4186 | | |
4187 | | // Look at Objective-C object pointers. |
4188 | 93.3M | if (auto objcObjectPtr = type->getAs<ObjCObjectPointerType>()) { |
4189 | 498k | ++numNormalPointers; |
4190 | 498k | ++numTypeSpecifierPointers; |
4191 | | |
4192 | | // If this is NSError**, report that. |
4193 | 498k | if (auto objcClassDecl = objcObjectPtr->getInterfaceDecl()) { |
4194 | 209k | if (objcClassDecl->getIdentifier() == S.getNSErrorIdent() && |
4195 | 209k | numNormalPointers == 22 && numTypeSpecifierPointers < 22 ) { |
4196 | 1 | return PointerDeclaratorKind::NSErrorPointerPointer; |
4197 | 1 | } |
4198 | 209k | } |
4199 | | |
4200 | 498k | break; |
4201 | 498k | } |
4202 | | |
4203 | | // Look at Objective-C class types. |
4204 | 92.8M | if (auto objcClass = type->getAs<ObjCInterfaceType>()) { |
4205 | 896k | if (objcClass->getInterface()->getIdentifier() == S.getNSErrorIdent()) { |
4206 | 46.0k | if (numNormalPointers == 2 && numTypeSpecifierPointers < 230.6k ) |
4207 | 30.6k | return PointerDeclaratorKind::NSErrorPointerPointer; |
4208 | 46.0k | } |
4209 | | |
4210 | 865k | break; |
4211 | 896k | } |
4212 | | |
4213 | | // If at this point we haven't seen a pointer, we won't see one. |
4214 | 91.9M | if (numNormalPointers == 0) |
4215 | 84.0M | return PointerDeclaratorKind::NonPointer; |
4216 | | |
4217 | 7.85M | if (auto recordType = type->getAs<RecordType>()) { |
4218 | 2.59M | RecordDecl *recordDecl = recordType->getDecl(); |
4219 | | |
4220 | | // If this is CFErrorRef*, report it as such. |
4221 | 2.59M | if (numNormalPointers == 2 && numTypeSpecifierPointers < 2139k && |
4222 | 2.59M | S.isCFError(recordDecl)131k ) { |
4223 | 18.7k | return PointerDeclaratorKind::CFErrorRefPointer; |
4224 | 18.7k | } |
4225 | 2.57M | break; |
4226 | 2.59M | } |
4227 | | |
4228 | 5.25M | break; |
4229 | 7.85M | } while (true2.35M ); |
4230 | | |
4231 | 9.19M | switch (numNormalPointers) { |
4232 | 42 | case 0: |
4233 | 42 | return PointerDeclaratorKind::NonPointer; |
4234 | | |
4235 | 8.93M | case 1: |
4236 | 8.93M | return PointerDeclaratorKind::SingleLevelPointer; |
4237 | | |
4238 | 256k | case 2: |
4239 | 256k | return PointerDeclaratorKind::MaybePointerToCFRef; |
4240 | | |
4241 | 11 | default: |
4242 | 11 | return PointerDeclaratorKind::MultiLevelPointer; |
4243 | 9.19M | } |
4244 | 9.19M | } |
4245 | | |
4246 | 131k | bool Sema::isCFError(RecordDecl *RD) { |
4247 | | // If we already know about CFError, test it directly. |
4248 | 131k | if (CFError) |
4249 | 121k | return CFError == RD; |
4250 | | |
4251 | | // Check whether this is CFError, which we identify based on its bridge to |
4252 | | // NSError. CFErrorRef used to be declared with "objc_bridge" but is now |
4253 | | // declared with "objc_bridge_mutable", so look for either one of the two |
4254 | | // attributes. |
4255 | 10.0k | if (RD->getTagKind() == TTK_Struct) { |
4256 | 10.0k | IdentifierInfo *bridgedType = nullptr; |
4257 | 10.0k | if (auto bridgeAttr = RD->getAttr<ObjCBridgeAttr>()) |
4258 | 1.56k | bridgedType = bridgeAttr->getBridgedType(); |
4259 | 8.46k | else if (auto bridgeAttr = RD->getAttr<ObjCBridgeMutableAttr>()) |
4260 | 439 | bridgedType = bridgeAttr->getBridgedType(); |
4261 | | |
4262 | 10.0k | if (bridgedType == getNSErrorIdent()) { |
4263 | 265 | CFError = RD; |
4264 | 265 | return true; |
4265 | 265 | } |
4266 | 10.0k | } |
4267 | | |
4268 | 9.80k | return false; |
4269 | 10.0k | } |
4270 | | |
4271 | | static FileID getNullabilityCompletenessCheckFileID(Sema &S, |
4272 | 13.8M | SourceLocation loc) { |
4273 | | // If we're anywhere in a function, method, or closure context, don't perform |
4274 | | // completeness checks. |
4275 | 18.5M | for (DeclContext *ctx = S.CurContext; ctx; ctx = ctx->getParent()4.73M ) { |
4276 | 18.5M | if (ctx->isFunctionOrMethod()) |
4277 | 31.4k | return FileID(); |
4278 | | |
4279 | 18.5M | if (ctx->isFileContext()) |
4280 | 13.7M | break; |
4281 | 18.5M | } |
4282 | | |
4283 | | // We only care about the expansion location. |
4284 | 13.7M | loc = S.SourceMgr.getExpansionLoc(loc); |
4285 | 13.7M | FileID file = S.SourceMgr.getFileID(loc); |
4286 | 13.7M | if (file.isInvalid()) |
4287 | 507 | return FileID(); |
4288 | | |
4289 | | // Retrieve file information. |
4290 | 13.7M | bool invalid = false; |
4291 | 13.7M | const SrcMgr::SLocEntry &sloc = S.SourceMgr.getSLocEntry(file, &invalid); |
4292 | 13.7M | if (invalid || !sloc.isFile()) |
4293 | 0 | return FileID(); |
4294 | | |
4295 | | // We don't want to perform completeness checks on the main file or in |
4296 | | // system headers. |
4297 | 13.7M | const SrcMgr::FileInfo &fileInfo = sloc.getFile(); |
4298 | 13.7M | if (fileInfo.getIncludeLoc().isInvalid()) |
4299 | 193k | return FileID(); |
4300 | 13.5M | if (fileInfo.getFileCharacteristic() != SrcMgr::C_User && |
4301 | 13.5M | S.Diags.getSuppressSystemWarnings()13.5M ) { |
4302 | 13.5M | return FileID(); |
4303 | 13.5M | } |
4304 | | |
4305 | 40.6k | return file; |
4306 | 13.5M | } |
4307 | | |
4308 | | /// Creates a fix-it to insert a C-style nullability keyword at \p pointerLoc, |
4309 | | /// taking into account whitespace before and after. |
4310 | | template <typename DiagBuilderT> |
4311 | | static void fixItNullability(Sema &S, DiagBuilderT &Diag, |
4312 | | SourceLocation PointerLoc, |
4313 | 898 | NullabilityKind Nullability) { |
4314 | 898 | assert(PointerLoc.isValid()); |
4315 | 898 | if (PointerLoc.isMacroID()) |
4316 | 2 | return; |
4317 | | |
4318 | 896 | SourceLocation FixItLoc = S.getLocForEndOfToken(PointerLoc); |
4319 | 896 | if (!FixItLoc.isValid() || FixItLoc == PointerLoc) |
4320 | 0 | return; |
4321 | | |
4322 | 896 | const char *NextChar = S.SourceMgr.getCharacterData(FixItLoc); |
4323 | 896 | if (!NextChar) |
4324 | 0 | return; |
4325 | | |
4326 | 896 | SmallString<32> InsertionTextBuf{" "}; |
4327 | 896 | InsertionTextBuf += getNullabilitySpelling(Nullability); |
4328 | 896 | InsertionTextBuf += " "; |
4329 | 896 | StringRef InsertionText = InsertionTextBuf.str(); |
4330 | | |
4331 | 896 | if (isWhitespace(*NextChar)) { |
4332 | 152 | InsertionText = InsertionText.drop_back(); |
4333 | 744 | } else if (NextChar[-1] == '[') { |
4334 | 152 | if (NextChar[0] == ']') |
4335 | 120 | InsertionText = InsertionText.drop_back().drop_front(); |
4336 | 32 | else |
4337 | 32 | InsertionText = InsertionText.drop_front(); |
4338 | 592 | } else if (!isAsciiIdentifierContinue(NextChar[0], /*allow dollar*/ true) && |
4339 | 592 | !isAsciiIdentifierContinue(NextChar[-1], /*allow dollar*/ true)428 ) { |
4340 | 188 | InsertionText = InsertionText.drop_back().drop_front(); |
4341 | 188 | } |
4342 | | |
4343 | 896 | Diag << FixItHint::CreateInsertion(FixItLoc, InsertionText); |
4344 | 896 | } |
4345 | | |
4346 | | static void emitNullabilityConsistencyWarning(Sema &S, |
4347 | | SimplePointerKind PointerKind, |
4348 | | SourceLocation PointerLoc, |
4349 | 429 | SourceLocation PointerEndLoc) { |
4350 | 429 | assert(PointerLoc.isValid()); |
4351 | | |
4352 | 429 | if (PointerKind == SimplePointerKind::Array) { |
4353 | 82 | S.Diag(PointerLoc, diag::warn_nullability_missing_array); |
4354 | 347 | } else { |
4355 | 347 | S.Diag(PointerLoc, diag::warn_nullability_missing) |
4356 | 347 | << static_cast<unsigned>(PointerKind); |
4357 | 347 | } |
4358 | | |
4359 | 429 | auto FixItLoc = PointerEndLoc.isValid() ? PointerEndLoc194 : PointerLoc235 ; |
4360 | 429 | if (FixItLoc.isMacroID()) |
4361 | 6 | return; |
4362 | | |
4363 | 846 | auto addFixIt = [&](NullabilityKind Nullability) 423 { |
4364 | 846 | auto Diag = S.Diag(FixItLoc, diag::note_nullability_fix_it); |
4365 | 846 | Diag << static_cast<unsigned>(Nullability); |
4366 | 846 | Diag << static_cast<unsigned>(PointerKind); |
4367 | 846 | fixItNullability(S, Diag, FixItLoc, Nullability); |
4368 | 846 | }; |
4369 | 423 | addFixIt(NullabilityKind::Nullable); |
4370 | 423 | addFixIt(NullabilityKind::NonNull); |
4371 | 423 | } |
4372 | | |
4373 | | /// Complains about missing nullability if the file containing \p pointerLoc |
4374 | | /// has other uses of nullability (either the keywords or the \c assume_nonnull |
4375 | | /// pragma). |
4376 | | /// |
4377 | | /// If the file has \e not seen other uses of nullability, this particular |
4378 | | /// pointer is saved for possible later diagnosis. See recordNullabilitySeen(). |
4379 | | static void |
4380 | | checkNullabilityConsistency(Sema &S, SimplePointerKind pointerKind, |
4381 | | SourceLocation pointerLoc, |
4382 | 6.73M | SourceLocation pointerEndLoc = SourceLocation()) { |
4383 | | // Determine which file we're performing consistency checking for. |
4384 | 6.73M | FileID file = getNullabilityCompletenessCheckFileID(S, pointerLoc); |
4385 | 6.73M | if (file.isInvalid()) |
4386 | 6.69M | return; |
4387 | | |
4388 | | // If we haven't seen any type nullability in this file, we won't warn now |
4389 | | // about anything. |
4390 | 39.6k | FileNullability &fileNullability = S.NullabilityMap[file]; |
4391 | 39.6k | if (!fileNullability.SawTypeNullability) { |
4392 | | // If this is the first pointer declarator in the file, and the appropriate |
4393 | | // warning is on, record it in case we need to diagnose it retroactively. |
4394 | 39.2k | diag::kind diagKind; |
4395 | 39.2k | if (pointerKind == SimplePointerKind::Array) |
4396 | 485 | diagKind = diag::warn_nullability_missing_array; |
4397 | 38.7k | else |
4398 | 38.7k | diagKind = diag::warn_nullability_missing; |
4399 | | |
4400 | 39.2k | if (fileNullability.PointerLoc.isInvalid() && |
4401 | 39.2k | !S.Context.getDiagnostics().isIgnored(diagKind, pointerLoc)21.7k ) { |
4402 | 3.97k | fileNullability.PointerLoc = pointerLoc; |
4403 | 3.97k | fileNullability.PointerEndLoc = pointerEndLoc; |
4404 | 3.97k | fileNullability.PointerKind = static_cast<unsigned>(pointerKind); |
4405 | 3.97k | } |
4406 | | |
4407 | 39.2k | return; |
4408 | 39.2k | } |
4409 | | |
4410 | | // Complain about missing nullability. |
4411 | 411 | emitNullabilityConsistencyWarning(S, pointerKind, pointerLoc, pointerEndLoc); |
4412 | 411 | } |
4413 | | |
4414 | | /// Marks that a nullability feature has been used in the file containing |
4415 | | /// \p loc. |
4416 | | /// |
4417 | | /// If this file already had pointer types in it that were missing nullability, |
4418 | | /// the first such instance is retroactively diagnosed. |
4419 | | /// |
4420 | | /// \sa checkNullabilityConsistency |
4421 | 7.07M | static void recordNullabilitySeen(Sema &S, SourceLocation loc) { |
4422 | 7.07M | FileID file = getNullabilityCompletenessCheckFileID(S, loc); |
4423 | 7.07M | if (file.isInvalid()) |
4424 | 7.07M | return; |
4425 | | |
4426 | 961 | FileNullability &fileNullability = S.NullabilityMap[file]; |
4427 | 961 | if (fileNullability.SawTypeNullability) |
4428 | 910 | return; |
4429 | 51 | fileNullability.SawTypeNullability = true; |
4430 | | |
4431 | | // If we haven't seen any type nullability before, now we have. Retroactively |
4432 | | // diagnose the first unannotated pointer, if there was one. |
4433 | 51 | if (fileNullability.PointerLoc.isInvalid()) |
4434 | 33 | return; |
4435 | | |
4436 | 18 | auto kind = static_cast<SimplePointerKind>(fileNullability.PointerKind); |
4437 | 18 | emitNullabilityConsistencyWarning(S, kind, fileNullability.PointerLoc, |
4438 | 18 | fileNullability.PointerEndLoc); |
4439 | 18 | } |
4440 | | |
4441 | | /// Returns true if any of the declarator chunks before \p endIndex include a |
4442 | | /// level of indirection: array, pointer, reference, or pointer-to-member. |
4443 | | /// |
4444 | | /// Because declarator chunks are stored in outer-to-inner order, testing |
4445 | | /// every chunk before \p endIndex is testing all chunks that embed the current |
4446 | | /// chunk as part of their type. |
4447 | | /// |
4448 | | /// It is legal to pass the result of Declarator::getNumTypeObjects() as the |
4449 | | /// end index, in which case all chunks are tested. |
4450 | 1.62M | static bool hasOuterPointerLikeChunk(const Declarator &D, unsigned endIndex) { |
4451 | 1.62M | unsigned i = endIndex; |
4452 | 1.63M | while (i != 0) { |
4453 | | // Walk outwards along the declarator chunks. |
4454 | 103k | --i; |
4455 | 103k | const DeclaratorChunk &DC = D.getTypeObject(i); |
4456 | 103k | switch (DC.Kind) { |
4457 | 6.33k | case DeclaratorChunk::Paren: |
4458 | 6.33k | break; |
4459 | 5.12k | case DeclaratorChunk::Array: |
4460 | 89.6k | case DeclaratorChunk::Pointer: |
4461 | 93.3k | case DeclaratorChunk::Reference: |
4462 | 93.3k | case DeclaratorChunk::MemberPointer: |
4463 | 93.3k | return true; |
4464 | 2.32k | case DeclaratorChunk::Function: |
4465 | 3.64k | case DeclaratorChunk::BlockPointer: |
4466 | 3.64k | case DeclaratorChunk::Pipe: |
4467 | | // These are invalid anyway, so just ignore. |
4468 | 3.64k | break; |
4469 | 103k | } |
4470 | 103k | } |
4471 | 1.53M | return false; |
4472 | 1.62M | } |
4473 | | |
4474 | 85 | static bool IsNoDerefableChunk(DeclaratorChunk Chunk) { |
4475 | 85 | return (Chunk.Kind == DeclaratorChunk::Pointer || |
4476 | 85 | Chunk.Kind == DeclaratorChunk::Array14 ); |
4477 | 85 | } |
4478 | | |
4479 | | template<typename AttrT> |
4480 | 3.24M | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { |
4481 | 3.24M | AL.setUsedAsTypeAttr(); |
4482 | 3.24M | return ::new (Ctx) AttrT(Ctx, AL); |
4483 | 3.24M | } SemaType.cpp:clang::ObjCInertUnsafeUnretainedAttr* createSimpleAttr<clang::ObjCInertUnsafeUnretainedAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 2.25k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 2.25k | AL.setUsedAsTypeAttr(); | 4482 | 2.25k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 2.25k | } |
SemaType.cpp:clang::ArmMveStrictPolymorphismAttr* createSimpleAttr<clang::ArmMveStrictPolymorphismAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 1.58k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 1.58k | AL.setUsedAsTypeAttr(); | 4482 | 1.58k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 1.58k | } |
SemaType.cpp:clang::LifetimeBoundAttr* createSimpleAttr<clang::LifetimeBoundAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 7 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 7 | AL.setUsedAsTypeAttr(); | 4482 | 7 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 7 | } |
SemaType.cpp:clang::NoDerefAttr* createSimpleAttr<clang::NoDerefAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 98 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 98 | AL.setUsedAsTypeAttr(); | 4482 | 98 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 98 | } |
SemaType.cpp:clang::Ptr32Attr* createSimpleAttr<clang::Ptr32Attr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 52 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 52 | AL.setUsedAsTypeAttr(); | 4482 | 52 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 52 | } |
SemaType.cpp:clang::Ptr64Attr* createSimpleAttr<clang::Ptr64Attr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 30 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 30 | AL.setUsedAsTypeAttr(); | 4482 | 30 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 30 | } |
SemaType.cpp:clang::SPtrAttr* createSimpleAttr<clang::SPtrAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 24 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 24 | AL.setUsedAsTypeAttr(); | 4482 | 24 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 24 | } |
SemaType.cpp:clang::UPtrAttr* createSimpleAttr<clang::UPtrAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 18 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 18 | AL.setUsedAsTypeAttr(); | 4482 | 18 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 18 | } |
SemaType.cpp:clang::TypeNonNullAttr* createSimpleAttr<clang::TypeNonNullAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 2.20M | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 2.20M | AL.setUsedAsTypeAttr(); | 4482 | 2.20M | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 2.20M | } |
SemaType.cpp:clang::TypeNullableAttr* createSimpleAttr<clang::TypeNullableAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 980k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 980k | AL.setUsedAsTypeAttr(); | 4482 | 980k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 980k | } |
SemaType.cpp:clang::TypeNullableResultAttr* createSimpleAttr<clang::TypeNullableResultAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 18 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 18 | AL.setUsedAsTypeAttr(); | 4482 | 18 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 18 | } |
SemaType.cpp:clang::TypeNullUnspecifiedAttr* createSimpleAttr<clang::TypeNullUnspecifiedAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 44.0k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 44.0k | AL.setUsedAsTypeAttr(); | 4482 | 44.0k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 44.0k | } |
SemaType.cpp:clang::ObjCKindOfAttr* createSimpleAttr<clang::ObjCKindOfAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 1.18k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 1.18k | AL.setUsedAsTypeAttr(); | 4482 | 1.18k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 1.18k | } |
SemaType.cpp:clang::NSReturnsRetainedAttr* createSimpleAttr<clang::NSReturnsRetainedAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 12.4k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 12.4k | AL.setUsedAsTypeAttr(); | 4482 | 12.4k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 12.4k | } |
SemaType.cpp:clang::CDeclAttr* createSimpleAttr<clang::CDeclAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 424 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 424 | AL.setUsedAsTypeAttr(); | 4482 | 424 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 424 | } |
SemaType.cpp:clang::FastCallAttr* createSimpleAttr<clang::FastCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 230 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 230 | AL.setUsedAsTypeAttr(); | 4482 | 230 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 230 | } |
SemaType.cpp:clang::StdCallAttr* createSimpleAttr<clang::StdCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 279 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 279 | AL.setUsedAsTypeAttr(); | 4482 | 279 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 279 | } |
SemaType.cpp:clang::ThisCallAttr* createSimpleAttr<clang::ThisCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 78 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 78 | AL.setUsedAsTypeAttr(); | 4482 | 78 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 78 | } |
SemaType.cpp:clang::RegCallAttr* createSimpleAttr<clang::RegCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 129 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 129 | AL.setUsedAsTypeAttr(); | 4482 | 129 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 129 | } |
SemaType.cpp:clang::PascalAttr* createSimpleAttr<clang::PascalAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 23 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 23 | AL.setUsedAsTypeAttr(); | 4482 | 23 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 23 | } |
SemaType.cpp:clang::SwiftCallAttr* createSimpleAttr<clang::SwiftCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 1.21k | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 1.21k | AL.setUsedAsTypeAttr(); | 4482 | 1.21k | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 1.21k | } |
SemaType.cpp:clang::SwiftAsyncCallAttr* createSimpleAttr<clang::SwiftAsyncCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 173 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 173 | AL.setUsedAsTypeAttr(); | 4482 | 173 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 173 | } |
SemaType.cpp:clang::VectorCallAttr* createSimpleAttr<clang::VectorCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 201 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 201 | AL.setUsedAsTypeAttr(); | 4482 | 201 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 201 | } |
SemaType.cpp:clang::AArch64VectorPcsAttr* createSimpleAttr<clang::AArch64VectorPcsAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 12 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 12 | AL.setUsedAsTypeAttr(); | 4482 | 12 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 12 | } |
SemaType.cpp:clang::AArch64SVEPcsAttr* createSimpleAttr<clang::AArch64SVEPcsAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 12 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 12 | AL.setUsedAsTypeAttr(); | 4482 | 12 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 12 | } |
SemaType.cpp:clang::AMDGPUKernelCallAttr* createSimpleAttr<clang::AMDGPUKernelCallAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 11 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 11 | AL.setUsedAsTypeAttr(); | 4482 | 11 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 11 | } |
SemaType.cpp:clang::IntelOclBiccAttr* createSimpleAttr<clang::IntelOclBiccAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 8 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 8 | AL.setUsedAsTypeAttr(); | 4482 | 8 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 8 | } |
SemaType.cpp:clang::MSABIAttr* createSimpleAttr<clang::MSABIAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 48 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 48 | AL.setUsedAsTypeAttr(); | 4482 | 48 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 48 | } |
SemaType.cpp:clang::SysVABIAttr* createSimpleAttr<clang::SysVABIAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 37 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 37 | AL.setUsedAsTypeAttr(); | 4482 | 37 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 37 | } |
SemaType.cpp:clang::PreserveMostAttr* createSimpleAttr<clang::PreserveMostAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 36 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 36 | AL.setUsedAsTypeAttr(); | 4482 | 36 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 36 | } |
SemaType.cpp:clang::PreserveAllAttr* createSimpleAttr<clang::PreserveAllAttr>(clang::ASTContext&, clang::ParsedAttr&) Line | Count | Source | 4480 | 27 | static AttrT *createSimpleAttr(ASTContext &Ctx, ParsedAttr &AL) { | 4481 | 27 | AL.setUsedAsTypeAttr(); | 4482 | 27 | return ::new (Ctx) AttrT(Ctx, AL); | 4483 | 27 | } |
|
4484 | | |
4485 | | static Attr *createNullabilityAttr(ASTContext &Ctx, ParsedAttr &Attr, |
4486 | 3.22M | NullabilityKind NK) { |
4487 | 3.22M | switch (NK) { |
4488 | 2.20M | case NullabilityKind::NonNull: |
4489 | 2.20M | return createSimpleAttr<TypeNonNullAttr>(Ctx, Attr); |
4490 | | |
4491 | 980k | case NullabilityKind::Nullable: |
4492 | 980k | return createSimpleAttr<TypeNullableAttr>(Ctx, Attr); |
4493 | | |
4494 | 18 | case NullabilityKind::NullableResult: |
4495 | 18 | return createSimpleAttr<TypeNullableResultAttr>(Ctx, Attr); |
4496 | | |
4497 | 44.0k | case NullabilityKind::Unspecified: |
4498 | 44.0k | return createSimpleAttr<TypeNullUnspecifiedAttr>(Ctx, Attr); |
4499 | 3.22M | } |
4500 | 0 | llvm_unreachable("unknown NullabilityKind"); |
4501 | 0 | } |
4502 | | |
4503 | | // Diagnose whether this is a case with the multiple addr spaces. |
4504 | | // Returns true if this is an invalid case. |
4505 | | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified |
4506 | | // by qualifiers for two or more different address spaces." |
4507 | | static bool DiagnoseMultipleAddrSpaceAttributes(Sema &S, LangAS ASOld, |
4508 | | LangAS ASNew, |
4509 | 35.3k | SourceLocation AttrLoc) { |
4510 | 35.3k | if (ASOld != LangAS::Default) { |
4511 | 76 | if (ASOld != ASNew) { |
4512 | 50 | S.Diag(AttrLoc, diag::err_attribute_address_multiple_qualifiers); |
4513 | 50 | return true; |
4514 | 50 | } |
4515 | | // Emit a warning if they are identical; it's likely unintended. |
4516 | 26 | S.Diag(AttrLoc, |
4517 | 26 | diag::warn_attribute_address_multiple_identical_qualifiers); |
4518 | 26 | } |
4519 | 35.3k | return false; |
4520 | 35.3k | } |
4521 | | |
4522 | | static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, |
4523 | | QualType declSpecType, |
4524 | 111M | TypeSourceInfo *TInfo) { |
4525 | | // The TypeSourceInfo that this function returns will not be a null type. |
4526 | | // If there is an error, this function will fill in a dummy type as fallback. |
4527 | 111M | QualType T = declSpecType; |
4528 | 111M | Declarator &D = state.getDeclarator(); |
4529 | 111M | Sema &S = state.getSema(); |
4530 | 111M | ASTContext &Context = S.Context; |
4531 | 111M | const LangOptions &LangOpts = S.getLangOpts(); |
4532 | | |
4533 | | // The name we're declaring, if any. |
4534 | 111M | DeclarationName Name; |
4535 | 111M | if (D.getIdentifier()) |
4536 | 43.7M | Name = D.getIdentifier(); |
4537 | | |
4538 | | // Does this declaration declare a typedef-name? |
4539 | 111M | bool IsTypedefName = |
4540 | 111M | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef || |
4541 | 111M | D.getContext() == DeclaratorContext::AliasDecl109M || |
4542 | 111M | D.getContext() == DeclaratorContext::AliasTemplate109M ; |
4543 | | |
4544 | | // Does T refer to a function type with a cv-qualifier or a ref-qualifier? |
4545 | 111M | bool IsQualifiedFunction = T->isFunctionProtoType() && |
4546 | 111M | (1.97k !T->castAs<FunctionProtoType>()->getMethodQuals().empty()1.97k || |
4547 | 1.97k | T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None1.92k ); |
4548 | | |
4549 | | // If T is 'decltype(auto)', the only declarators we can have are parens |
4550 | | // and at most one function declarator if this is a function declaration. |
4551 | | // If T is a deduced class template specialization type, we can have no |
4552 | | // declarator chunks at all. |
4553 | 111M | if (auto *DT = T->getAs<DeducedType>()) { |
4554 | 58.4k | const AutoType *AT = T->getAs<AutoType>(); |
4555 | 58.4k | bool IsClassTemplateDeduction = isa<DeducedTemplateSpecializationType>(DT); |
4556 | 58.4k | if ((AT && AT->isDecltypeAuto()57.0k ) || IsClassTemplateDeduction57.7k ) { |
4557 | 2.53k | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I477 ) { |
4558 | 526 | unsigned Index = E - I - 1; |
4559 | 526 | DeclaratorChunk &DeclChunk = D.getTypeObject(Index); |
4560 | 526 | unsigned DiagId = IsClassTemplateDeduction |
4561 | 526 | ? diag::err_deduced_class_template_compound_type14 |
4562 | 526 | : diag::err_decltype_auto_compound_type512 ; |
4563 | 526 | unsigned DiagKind = 0; |
4564 | 526 | switch (DeclChunk.Kind) { |
4565 | 38 | case DeclaratorChunk::Paren: |
4566 | | // FIXME: Rejecting this is a little silly. |
4567 | 38 | if (IsClassTemplateDeduction) { |
4568 | 2 | DiagKind = 4; |
4569 | 2 | break; |
4570 | 2 | } |
4571 | 36 | continue; |
4572 | 452 | case DeclaratorChunk::Function: { |
4573 | 452 | if (IsClassTemplateDeduction) { |
4574 | 2 | DiagKind = 3; |
4575 | 2 | break; |
4576 | 2 | } |
4577 | 450 | unsigned FnIndex; |
4578 | 450 | if (D.isFunctionDeclarationContext() && |
4579 | 450 | D.isFunctionDeclarator(FnIndex) && FnIndex == Index444 ) |
4580 | 441 | continue; |
4581 | 9 | DiagId = diag::err_decltype_auto_function_declarator_not_declaration; |
4582 | 9 | break; |
4583 | 450 | } |
4584 | 16 | case DeclaratorChunk::Pointer: |
4585 | 16 | case DeclaratorChunk::BlockPointer: |
4586 | 18 | case DeclaratorChunk::MemberPointer: |
4587 | 18 | DiagKind = 0; |
4588 | 18 | break; |
4589 | 10 | case DeclaratorChunk::Reference: |
4590 | 10 | DiagKind = 1; |
4591 | 10 | break; |
4592 | 8 | case DeclaratorChunk::Array: |
4593 | 8 | DiagKind = 2; |
4594 | 8 | break; |
4595 | 0 | case DeclaratorChunk::Pipe: |
4596 | 0 | break; |
4597 | 526 | } |
4598 | | |
4599 | 49 | S.Diag(DeclChunk.Loc, DiagId) << DiagKind; |
4600 | 49 | D.setInvalidType(true); |
4601 | 49 | break; |
4602 | 526 | } |
4603 | 2.06k | } |
4604 | 58.4k | } |
4605 | | |
4606 | | // Determine whether we should infer _Nonnull on pointer types. |
4607 | 111M | Optional<NullabilityKind> inferNullability; |
4608 | 111M | bool inferNullabilityCS = false; |
4609 | 111M | bool inferNullabilityInnerOnly = false; |
4610 | 111M | bool inferNullabilityInnerOnlyComplete = false; |
4611 | | |
4612 | | // Are we in an assume-nonnull region? |
4613 | 111M | bool inAssumeNonNullRegion = false; |
4614 | 111M | SourceLocation assumeNonNullLoc = S.PP.getPragmaAssumeNonNullLoc(); |
4615 | 111M | if (assumeNonNullLoc.isValid()) { |
4616 | 5.04M | inAssumeNonNullRegion = true; |
4617 | 5.04M | recordNullabilitySeen(S, assumeNonNullLoc); |
4618 | 5.04M | } |
4619 | | |
4620 | | // Whether to complain about missing nullability specifiers or not. |
4621 | 111M | enum { |
4622 | | /// Never complain. |
4623 | 111M | CAMN_No, |
4624 | | /// Complain on the inner pointers (but not the outermost |
4625 | | /// pointer). |
4626 | 111M | CAMN_InnerPointers, |
4627 | | /// Complain about any pointers that don't have nullability |
4628 | | /// specified or inferred. |
4629 | 111M | CAMN_Yes |
4630 | 111M | } complainAboutMissingNullability = CAMN_No; |
4631 | 111M | unsigned NumPointersRemaining = 0; |
4632 | 111M | auto complainAboutInferringWithinChunk = PointerWrappingDeclaratorKind::None; |
4633 | | |
4634 | 111M | if (IsTypedefName) { |
4635 | | // For typedefs, we do not infer any nullability (the default), |
4636 | | // and we only complain about missing nullability specifiers on |
4637 | | // inner pointers. |
4638 | 2.34M | complainAboutMissingNullability = CAMN_InnerPointers; |
4639 | | |
4640 | 2.34M | if (T->canHaveNullability(/*ResultIfUnknown*/false) && |
4641 | 2.34M | !T->getNullability(S.Context)59.3k ) { |
4642 | | // Note that we allow but don't require nullability on dependent types. |
4643 | 57.5k | ++NumPointersRemaining; |
4644 | 57.5k | } |
4645 | | |
4646 | 2.91M | for (unsigned i = 0, n = D.getNumTypeObjects(); i != n; ++i567k ) { |
4647 | 567k | DeclaratorChunk &chunk = D.getTypeObject(i); |
4648 | 567k | switch (chunk.Kind) { |
4649 | 18.2k | case DeclaratorChunk::Array: |
4650 | 117k | case DeclaratorChunk::Function: |
4651 | 117k | case DeclaratorChunk::Pipe: |
4652 | 117k | break; |
4653 | | |
4654 | 10.5k | case DeclaratorChunk::BlockPointer: |
4655 | 10.8k | case DeclaratorChunk::MemberPointer: |
4656 | 10.8k | ++NumPointersRemaining; |
4657 | 10.8k | break; |
4658 | | |
4659 | 97.3k | case DeclaratorChunk::Paren: |
4660 | 131k | case DeclaratorChunk::Reference: |
4661 | 131k | continue; |
4662 | | |
4663 | 308k | case DeclaratorChunk::Pointer: |
4664 | 308k | ++NumPointersRemaining; |
4665 | 308k | continue; |
4666 | 567k | } |
4667 | 567k | } |
4668 | 109M | } else { |
4669 | 109M | bool isFunctionOrMethod = false; |
4670 | 109M | switch (auto context = state.getDeclarator().getContext()) { |
4671 | 969k | case DeclaratorContext::ObjCParameter: |
4672 | 1.62M | case DeclaratorContext::ObjCResult: |
4673 | 66.9M | case DeclaratorContext::Prototype: |
4674 | 66.9M | case DeclaratorContext::TrailingReturn: |
4675 | 66.9M | case DeclaratorContext::TrailingReturnVar: |
4676 | 66.9M | isFunctionOrMethod = true; |
4677 | 66.9M | LLVM_FALLTHROUGH; |
4678 | | |
4679 | 71.9M | case DeclaratorContext::Member: |
4680 | 71.9M | if (state.getDeclarator().isObjCIvar() && !isFunctionOrMethod135k ) { |
4681 | 135k | complainAboutMissingNullability = CAMN_No; |
4682 | 135k | break; |
4683 | 135k | } |
4684 | | |
4685 | | // Weak properties are inferred to be nullable. |
4686 | 71.8M | if (state.getDeclarator().isObjCWeakProperty() && inAssumeNonNullRegion1.04k ) { |
4687 | 907 | inferNullability = NullabilityKind::Nullable; |
4688 | 907 | break; |
4689 | 907 | } |
4690 | | |
4691 | 71.8M | LLVM_FALLTHROUGH71.8M ;71.8M |
4692 | | |
4693 | 96.5M | case DeclaratorContext::File: |
4694 | 96.5M | case DeclaratorContext::KNRTypeList: { |
4695 | 96.5M | complainAboutMissingNullability = CAMN_Yes; |
4696 | | |
4697 | | // Nullability inference depends on the type and declarator. |
4698 | 96.5M | auto wrappingKind = PointerWrappingDeclaratorKind::None; |
4699 | 96.5M | switch (classifyPointerDeclarator(S, T, D, wrappingKind)) { |
4700 | 87.2M | case PointerDeclaratorKind::NonPointer: |
4701 | 87.2M | case PointerDeclaratorKind::MultiLevelPointer: |
4702 | | // Cannot infer nullability. |
4703 | 87.2M | break; |
4704 | | |
4705 | 9.01M | case PointerDeclaratorKind::SingleLevelPointer: |
4706 | | // Infer _Nonnull if we are in an assumes-nonnull region. |
4707 | 9.01M | if (inAssumeNonNullRegion) { |
4708 | 2.86M | complainAboutInferringWithinChunk = wrappingKind; |
4709 | 2.86M | inferNullability = NullabilityKind::NonNull; |
4710 | 2.86M | inferNullabilityCS = (context == DeclaratorContext::ObjCParameter || |
4711 | 2.86M | context == DeclaratorContext::ObjCResult2.20M ); |
4712 | 2.86M | } |
4713 | 9.01M | break; |
4714 | | |
4715 | 18.7k | case PointerDeclaratorKind::CFErrorRefPointer: |
4716 | 49.3k | case PointerDeclaratorKind::NSErrorPointerPointer: |
4717 | | // Within a function or method signature, infer _Nullable at both |
4718 | | // levels. |
4719 | 49.3k | if (isFunctionOrMethod && inAssumeNonNullRegion49.3k ) |
4720 | 44.1k | inferNullability = NullabilityKind::Nullable; |
4721 | 49.3k | break; |
4722 | | |
4723 | 256k | case PointerDeclaratorKind::MaybePointerToCFRef: |
4724 | 256k | if (isFunctionOrMethod) { |
4725 | | // On pointer-to-pointer parameters marked cf_returns_retained or |
4726 | | // cf_returns_not_retained, if the outer pointer is explicit then |
4727 | | // infer the inner pointer as _Nullable. |
4728 | 231k | auto hasCFReturnsAttr = |
4729 | 807k | [](const ParsedAttributesView &AttrList) -> bool { |
4730 | 807k | return AttrList.hasAttribute(ParsedAttr::AT_CFReturnsRetained) || |
4731 | 807k | AttrList.hasAttribute(ParsedAttr::AT_CFReturnsNotRetained)779k ; |
4732 | 807k | }; |
4733 | 231k | if (const auto *InnermostChunk = D.getInnermostNonParenChunk()) { |
4734 | 209k | if (hasCFReturnsAttr(D.getDeclarationAttributes()) || |
4735 | 209k | hasCFReturnsAttr(D.getAttributes()) || |
4736 | 209k | hasCFReturnsAttr(InnermostChunk->getAttrs())207k || |
4737 | 209k | hasCFReturnsAttr(D.getDeclSpec().getAttributes())181k ) { |
4738 | 28.1k | inferNullability = NullabilityKind::Nullable; |
4739 | 28.1k | inferNullabilityInnerOnly = true; |
4740 | 28.1k | } |
4741 | 209k | } |
4742 | 231k | } |
4743 | 256k | break; |
4744 | 96.5M | } |
4745 | 96.5M | break; |
4746 | 96.5M | } |
4747 | | |
4748 | 96.5M | case DeclaratorContext::ConversionId: |
4749 | 14.7k | complainAboutMissingNullability = CAMN_Yes; |
4750 | 14.7k | break; |
4751 | | |
4752 | 0 | case DeclaratorContext::AliasDecl: |
4753 | 0 | case DeclaratorContext::AliasTemplate: |
4754 | 1.24M | case DeclaratorContext::Block: |
4755 | 1.24M | case DeclaratorContext::BlockLiteral: |
4756 | 1.25M | case DeclaratorContext::Condition: |
4757 | 1.25M | case DeclaratorContext::CXXCatch: |
4758 | 1.28M | case DeclaratorContext::CXXNew: |
4759 | 1.41M | case DeclaratorContext::ForInit: |
4760 | 1.41M | case DeclaratorContext::SelectionInit: |
4761 | 1.42M | case DeclaratorContext::LambdaExpr: |
4762 | 1.42M | case DeclaratorContext::LambdaExprParameter: |
4763 | 1.42M | case DeclaratorContext::ObjCCatch: |
4764 | 1.69M | case DeclaratorContext::TemplateParam: |
4765 | 6.61M | case DeclaratorContext::TemplateArg: |
4766 | 6.73M | case DeclaratorContext::TemplateTypeArg: |
4767 | 12.4M | case DeclaratorContext::TypeName: |
4768 | 12.9M | case DeclaratorContext::FunctionalCast: |
4769 | 12.9M | case DeclaratorContext::RequiresExpr: |
4770 | 12.9M | case DeclaratorContext::Association: |
4771 | | // Don't infer in these contexts. |
4772 | 12.9M | break; |
4773 | 109M | } |
4774 | 109M | } |
4775 | | |
4776 | | // Local function that returns true if its argument looks like a va_list. |
4777 | 111M | auto isVaList = [&S](QualType T) -> bool { |
4778 | 2.64M | auto *typedefTy = T->getAs<TypedefType>(); |
4779 | 2.64M | if (!typedefTy) |
4780 | 77.9k | return false; |
4781 | 2.56M | TypedefDecl *vaListTypedef = S.Context.getBuiltinVaListDecl(); |
4782 | 2.85M | do { |
4783 | 2.85M | if (typedefTy->getDecl() == vaListTypedef) |
4784 | 4.40k | return true; |
4785 | 2.84M | if (auto *name = typedefTy->getDecl()->getIdentifier()) |
4786 | 2.84M | if (name->isStr("va_list")) |
4787 | 21.3k | return true; |
4788 | 2.82M | typedefTy = typedefTy->desugar()->getAs<TypedefType>(); |
4789 | 2.82M | } while (typedefTy); |
4790 | 2.53M | return false; |
4791 | 2.56M | }; |
4792 | | |
4793 | | // Local function that checks the nullability for a given pointer declarator. |
4794 | | // Returns true if _Nonnull was inferred. |
4795 | 111M | auto inferPointerNullability = |
4796 | 111M | [&](SimplePointerKind pointerKind, SourceLocation pointerLoc, |
4797 | 111M | SourceLocation pointerEndLoc, |
4798 | 111M | ParsedAttributesView &attrs, AttributePool &Pool) -> ParsedAttr * { |
4799 | | // We've seen a pointer. |
4800 | 10.4M | if (NumPointersRemaining > 0) |
4801 | 376k | --NumPointersRemaining; |
4802 | | |
4803 | | // If a nullability attribute is present, there's nothing to do. |
4804 | 10.4M | if (hasNullabilityAttr(attrs)) |
4805 | 525k | return nullptr; |
4806 | | |
4807 | | // If we're supposed to infer nullability, do so now. |
4808 | 9.96M | if (inferNullability && !inferNullabilityInnerOnlyComplete2.20M ) { |
4809 | 2.20M | ParsedAttr::Syntax syntax = inferNullabilityCS |
4810 | 2.20M | ? ParsedAttr::AS_ContextSensitiveKeyword722k |
4811 | 2.20M | : ParsedAttr::AS_Keyword1.48M ; |
4812 | 2.20M | ParsedAttr *nullabilityAttr = Pool.create( |
4813 | 2.20M | S.getNullabilityKeyword(*inferNullability), SourceRange(pointerLoc), |
4814 | 2.20M | nullptr, SourceLocation(), nullptr, 0, syntax); |
4815 | | |
4816 | 2.20M | attrs.addAtEnd(nullabilityAttr); |
4817 | | |
4818 | 2.20M | if (inferNullabilityCS) { |
4819 | 722k | state.getDeclarator().getMutableDeclSpec().getObjCQualifiers() |
4820 | 722k | ->setObjCDeclQualifier(ObjCDeclSpec::DQ_CSNullability); |
4821 | 722k | } |
4822 | | |
4823 | 2.20M | if (pointerLoc.isValid() && |
4824 | 2.20M | complainAboutInferringWithinChunk != |
4825 | 2.20M | PointerWrappingDeclaratorKind::None) { |
4826 | 52 | auto Diag = |
4827 | 52 | S.Diag(pointerLoc, diag::warn_nullability_inferred_on_nested_type); |
4828 | 52 | Diag << static_cast<int>(complainAboutInferringWithinChunk); |
4829 | 52 | fixItNullability(S, Diag, pointerLoc, NullabilityKind::NonNull); |
4830 | 52 | } |
4831 | | |
4832 | 2.20M | if (inferNullabilityInnerOnly) |
4833 | 23.7k | inferNullabilityInnerOnlyComplete = true; |
4834 | 2.20M | return nullabilityAttr; |
4835 | 2.20M | } |
4836 | | |
4837 | | // If we're supposed to complain about missing nullability, do so |
4838 | | // now if it's truly missing. |
4839 | 7.76M | switch (complainAboutMissingNullability) { |
4840 | 724k | case CAMN_No: |
4841 | 724k | break; |
4842 | | |
4843 | 376k | case CAMN_InnerPointers: |
4844 | 376k | if (NumPointersRemaining == 0) |
4845 | 352k | break; |
4846 | 376k | LLVM_FALLTHROUGH23.3k ;23.3k |
4847 | | |
4848 | 6.68M | case CAMN_Yes: |
4849 | 6.68M | checkNullabilityConsistency(S, pointerKind, pointerLoc, pointerEndLoc); |
4850 | 7.76M | } |
4851 | 7.76M | return nullptr; |
4852 | 7.76M | }; |
4853 | | |
4854 | | // If the type itself could have nullability but does not, infer pointer |
4855 | | // nullability and perform consistency checking. |
4856 | 111M | if (S.CodeSynthesisContexts.empty()) { |
4857 | 111M | if (T->canHaveNullability(/*ResultIfUnknown*/false) && |
4858 | 111M | !T->getNullability(S.Context)3.06M ) { |
4859 | 2.58M | if (isVaList(T)) { |
4860 | | // Record that we've seen a pointer, but do nothing else. |
4861 | 535 | if (NumPointersRemaining > 0) |
4862 | 112 | --NumPointersRemaining; |
4863 | 2.58M | } else { |
4864 | 2.58M | SimplePointerKind pointerKind = SimplePointerKind::Pointer; |
4865 | 2.58M | if (T->isBlockPointerType()) |
4866 | 21.4k | pointerKind = SimplePointerKind::BlockPointer; |
4867 | 2.55M | else if (T->isMemberPointerType()) |
4868 | 1.01k | pointerKind = SimplePointerKind::MemberPointer; |
4869 | | |
4870 | 2.58M | if (auto *attr = inferPointerNullability( |
4871 | 2.58M | pointerKind, D.getDeclSpec().getTypeSpecTypeLoc(), |
4872 | 2.58M | D.getDeclSpec().getEndLoc(), |
4873 | 2.58M | D.getMutableDeclSpec().getAttributes(), |
4874 | 2.58M | D.getMutableDeclSpec().getAttributePool())) { |
4875 | 1.19M | T = state.getAttributedType( |
4876 | 1.19M | createNullabilityAttr(Context, *attr, *inferNullability), T, T); |
4877 | 1.19M | } |
4878 | 2.58M | } |
4879 | 2.58M | } |
4880 | | |
4881 | 111M | if (complainAboutMissingNullability == CAMN_Yes && |
4882 | 111M | T->isArrayType()96.6M && !T->getNullability(S.Context)60.3k && !isVaList(T)59.2k && |
4883 | 111M | D.isPrototypeContext()33.9k && |
4884 | 111M | !hasOuterPointerLikeChunk(D, D.getNumTypeObjects())21.3k ) { |
4885 | 19.4k | checkNullabilityConsistency(S, SimplePointerKind::Array, |
4886 | 19.4k | D.getDeclSpec().getTypeSpecTypeLoc()); |
4887 | 19.4k | } |
4888 | 111M | } |
4889 | | |
4890 | 111M | bool ExpectNoDerefChunk = |
4891 | 111M | state.getCurrentAttributes().hasAttribute(ParsedAttr::AT_NoDeref); |
4892 | | |
4893 | | // Walk the DeclTypeInfo, building the recursive type as we go. |
4894 | | // DeclTypeInfos are ordered from the identifier out, which is |
4895 | | // opposite of what we want :). |
4896 | 147M | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i35.5M ) { |
4897 | 35.5M | unsigned chunkIndex = e - i - 1; |
4898 | 35.5M | state.setCurrentChunkIndex(chunkIndex); |
4899 | 35.5M | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
4900 | 35.5M | IsQualifiedFunction &= DeclType.Kind == DeclaratorChunk::Paren; |
4901 | 35.5M | switch (DeclType.Kind) { |
4902 | 318k | case DeclaratorChunk::Paren: |
4903 | 318k | if (i == 0) |
4904 | 452 | warnAboutRedundantParens(S, D, T); |
4905 | 318k | T = S.BuildParenType(T); |
4906 | 318k | break; |
4907 | 61.6k | case DeclaratorChunk::BlockPointer: |
4908 | | // If blocks are disabled, emit an error. |
4909 | 61.6k | if (!LangOpts.Blocks) |
4910 | 7 | S.Diag(DeclType.Loc, diag::err_blocks_disable) << LangOpts.OpenCL; |
4911 | | |
4912 | | // Handle pointer nullability. |
4913 | 61.6k | inferPointerNullability(SimplePointerKind::BlockPointer, DeclType.Loc, |
4914 | 61.6k | DeclType.EndLoc, DeclType.getAttrs(), |
4915 | 61.6k | state.getDeclarator().getAttributePool()); |
4916 | | |
4917 | 61.6k | T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name); |
4918 | 61.6k | if (DeclType.Cls.TypeQuals || LangOpts.OpenCL61.6k ) { |
4919 | | // OpenCL v2.0, s6.12.5 - Block variable declarations are implicitly |
4920 | | // qualified with const. |
4921 | 159 | if (LangOpts.OpenCL) |
4922 | 147 | DeclType.Cls.TypeQuals |= DeclSpec::TQ_const; |
4923 | 159 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); |
4924 | 159 | } |
4925 | 61.6k | break; |
4926 | 7.81M | case DeclaratorChunk::Pointer: |
4927 | | // Verify that we're not building a pointer to pointer to function with |
4928 | | // exception specification. |
4929 | 7.81M | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)3.23M ) { |
4930 | 7 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
4931 | 7 | D.setInvalidType(true); |
4932 | | // Build the type anyway. |
4933 | 7 | } |
4934 | | |
4935 | | // Handle pointer nullability |
4936 | 7.81M | inferPointerNullability(SimplePointerKind::Pointer, DeclType.Loc, |
4937 | 7.81M | DeclType.EndLoc, DeclType.getAttrs(), |
4938 | 7.81M | state.getDeclarator().getAttributePool()); |
4939 | | |
4940 | 7.81M | if (LangOpts.ObjC && T->getAs<ObjCObjectType>()3.05M ) { |
4941 | 1.15M | T = Context.getObjCObjectPointerType(T); |
4942 | 1.15M | if (DeclType.Ptr.TypeQuals) |
4943 | 66.4k | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
4944 | 1.15M | break; |
4945 | 1.15M | } |
4946 | | |
4947 | | // OpenCL v2.0 s6.9b - Pointer to image/sampler cannot be used. |
4948 | | // OpenCL v2.0 s6.13.16.1 - Pointer to pipe cannot be used. |
4949 | | // OpenCL v2.0 s6.12.5 - Pointers to Blocks are not allowed. |
4950 | 6.65M | if (LangOpts.OpenCL) { |
4951 | 40.8k | if (T->isImageType() || T->isSamplerT()40.8k || T->isPipeType()40.8k || |
4952 | 40.8k | T->isBlockPointerType()40.8k ) { |
4953 | 14 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_pointer_to_type) << T; |
4954 | 14 | D.setInvalidType(true); |
4955 | 14 | } |
4956 | 40.8k | } |
4957 | | |
4958 | 6.65M | T = S.BuildPointerType(T, DeclType.Loc, Name); |
4959 | 6.65M | if (DeclType.Ptr.TypeQuals) |
4960 | 89.1k | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
4961 | 6.65M | break; |
4962 | 1.56M | case DeclaratorChunk::Reference: { |
4963 | | // Verify that we're not building a reference to pointer to function with |
4964 | | // exception specification. |
4965 | 1.56M | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
|