clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaDecl.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Sema -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/Sema -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Sema -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/Sema/SemaDecl.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "TypeLocBuilder.h" |
14 | #include "clang/AST/ASTConsumer.h" |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/AST/ASTLambda.h" |
17 | #include "clang/AST/CXXInheritance.h" |
18 | #include "clang/AST/CharUnits.h" |
19 | #include "clang/AST/CommentDiagnostic.h" |
20 | #include "clang/AST/DeclCXX.h" |
21 | #include "clang/AST/DeclObjC.h" |
22 | #include "clang/AST/DeclTemplate.h" |
23 | #include "clang/AST/EvaluatedExprVisitor.h" |
24 | #include "clang/AST/Expr.h" |
25 | #include "clang/AST/ExprCXX.h" |
26 | #include "clang/AST/NonTrivialTypeVisitor.h" |
27 | #include "clang/AST/StmtCXX.h" |
28 | #include "clang/Basic/Builtins.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceManager.h" |
31 | #include "clang/Basic/TargetInfo.h" |
32 | #include "clang/Lex/HeaderSearch.h" // TODO: Sema shouldn't depend on Lex |
33 | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
34 | #include "clang/Lex/ModuleLoader.h" // TODO: Sema shouldn't depend on Lex |
35 | #include "clang/Lex/Preprocessor.h" // Included for isCodeCompletionEnabled() |
36 | #include "clang/Sema/CXXFieldCollector.h" |
37 | #include "clang/Sema/DeclSpec.h" |
38 | #include "clang/Sema/DelayedDiagnostic.h" |
39 | #include "clang/Sema/Initialization.h" |
40 | #include "clang/Sema/Lookup.h" |
41 | #include "clang/Sema/ParsedTemplate.h" |
42 | #include "clang/Sema/Scope.h" |
43 | #include "clang/Sema/ScopeInfo.h" |
44 | #include "clang/Sema/SemaInternal.h" |
45 | #include "clang/Sema/Template.h" |
46 | #include "llvm/ADT/SmallString.h" |
47 | #include "llvm/ADT/Triple.h" |
48 | #include <algorithm> |
49 | #include <cstring> |
50 | #include <functional> |
51 | #include <unordered_map> |
52 | |
53 | using namespace clang; |
54 | using namespace sema; |
55 | |
56 | Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { |
57 | if (OwnedType) { |
58 | Decl *Group[2] = { OwnedType, Ptr }; |
59 | return DeclGroupPtrTy::make(DeclGroupRef::Create(Context, Group, 2)); |
60 | } |
61 | |
62 | return DeclGroupPtrTy::make(DeclGroupRef(Ptr)); |
63 | } |
64 | |
65 | namespace { |
66 | |
67 | class TypeNameValidatorCCC final : public CorrectionCandidateCallback { |
68 | public: |
69 | TypeNameValidatorCCC(bool AllowInvalid, bool WantClass = false, |
70 | bool AllowTemplates = false, |
71 | bool AllowNonTemplates = true) |
72 | : AllowInvalidDecl(AllowInvalid), WantClassName(WantClass), |
73 | AllowTemplates(AllowTemplates), AllowNonTemplates(AllowNonTemplates) { |
74 | WantExpressionKeywords = false; |
75 | WantCXXNamedCasts = false; |
76 | WantRemainingKeywords = false; |
77 | } |
78 | |
79 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
80 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
81 | if (!AllowInvalidDecl && ND->isInvalidDecl()) |
82 | return false; |
83 | |
84 | if (getAsTypeTemplateDecl(ND)) |
85 | return AllowTemplates; |
86 | |
87 | bool IsType = isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
88 | if (!IsType) |
89 | return false; |
90 | |
91 | if (AllowNonTemplates) |
92 | return true; |
93 | |
94 | |
95 | |
96 | if (AllowTemplates) { |
97 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
98 | if (!RD || !RD->isInjectedClassName()) |
99 | return false; |
100 | RD = cast<CXXRecordDecl>(RD->getDeclContext()); |
101 | return RD->getDescribedClassTemplate() || |
102 | isa<ClassTemplateSpecializationDecl>(RD); |
103 | } |
104 | |
105 | return false; |
106 | } |
107 | |
108 | return !WantClassName && candidate.isKeyword(); |
109 | } |
110 | |
111 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
112 | return std::make_unique<TypeNameValidatorCCC>(*this); |
113 | } |
114 | |
115 | private: |
116 | bool AllowInvalidDecl; |
117 | bool WantClassName; |
118 | bool AllowTemplates; |
119 | bool AllowNonTemplates; |
120 | }; |
121 | |
122 | } |
123 | |
124 | |
125 | bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { |
126 | switch (Kind) { |
127 | |
128 | |
129 | case tok::kw_short: |
130 | case tok::kw_long: |
131 | case tok::kw___int64: |
132 | case tok::kw___int128: |
133 | case tok::kw_signed: |
134 | case tok::kw_unsigned: |
135 | case tok::kw_void: |
136 | case tok::kw_char: |
137 | case tok::kw_int: |
138 | case tok::kw_half: |
139 | case tok::kw_float: |
140 | case tok::kw_double: |
141 | case tok::kw___bf16: |
142 | case tok::kw__Float16: |
143 | case tok::kw___float128: |
144 | case tok::kw_wchar_t: |
145 | case tok::kw_bool: |
146 | case tok::kw___underlying_type: |
147 | case tok::kw___auto_type: |
148 | return true; |
149 | |
150 | case tok::annot_typename: |
151 | case tok::kw_char16_t: |
152 | case tok::kw_char32_t: |
153 | case tok::kw_typeof: |
154 | case tok::annot_decltype: |
155 | case tok::kw_decltype: |
156 | return getLangOpts().CPlusPlus; |
157 | |
158 | case tok::kw_char8_t: |
159 | return getLangOpts().Char8; |
160 | |
161 | default: |
162 | break; |
163 | } |
164 | |
165 | return false; |
166 | } |
167 | |
168 | namespace { |
169 | enum class UnqualifiedTypeNameLookupResult { |
170 | NotFound, |
171 | FoundNonType, |
172 | FoundType |
173 | }; |
174 | } |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | static UnqualifiedTypeNameLookupResult |
181 | lookupUnqualifiedTypeNameInBase(Sema &S, const IdentifierInfo &II, |
182 | SourceLocation NameLoc, |
183 | const CXXRecordDecl *RD) { |
184 | if (!RD->hasDefinition()) |
185 | return UnqualifiedTypeNameLookupResult::NotFound; |
186 | |
187 | UnqualifiedTypeNameLookupResult FoundTypeDecl = |
188 | UnqualifiedTypeNameLookupResult::NotFound; |
189 | for (const auto &Base : RD->bases()) { |
190 | const CXXRecordDecl *BaseRD = nullptr; |
191 | if (auto *BaseTT = Base.getType()->getAs<TagType>()) |
192 | BaseRD = BaseTT->getAsCXXRecordDecl(); |
193 | else if (auto *TST = Base.getType()->getAs<TemplateSpecializationType>()) { |
194 | |
195 | |
196 | if (!TST || !TST->isDependentType()) |
197 | continue; |
198 | auto *TD = TST->getTemplateName().getAsTemplateDecl(); |
199 | if (!TD) |
200 | continue; |
201 | if (auto *BasePrimaryTemplate = |
202 | dyn_cast_or_null<CXXRecordDecl>(TD->getTemplatedDecl())) { |
203 | if (BasePrimaryTemplate->getCanonicalDecl() != RD->getCanonicalDecl()) |
204 | BaseRD = BasePrimaryTemplate; |
205 | else if (auto *CTD = dyn_cast<ClassTemplateDecl>(TD)) { |
206 | if (const ClassTemplatePartialSpecializationDecl *PS = |
207 | CTD->findPartialSpecialization(Base.getType())) |
208 | if (PS->getCanonicalDecl() != RD->getCanonicalDecl()) |
209 | BaseRD = PS; |
210 | } |
211 | } |
212 | } |
213 | if (BaseRD) { |
214 | for (NamedDecl *ND : BaseRD->lookup(&II)) { |
215 | if (!isa<TypeDecl>(ND)) |
216 | return UnqualifiedTypeNameLookupResult::FoundNonType; |
217 | FoundTypeDecl = UnqualifiedTypeNameLookupResult::FoundType; |
218 | } |
219 | if (FoundTypeDecl == UnqualifiedTypeNameLookupResult::NotFound) { |
220 | switch (lookupUnqualifiedTypeNameInBase(S, II, NameLoc, BaseRD)) { |
221 | case UnqualifiedTypeNameLookupResult::FoundNonType: |
222 | return UnqualifiedTypeNameLookupResult::FoundNonType; |
223 | case UnqualifiedTypeNameLookupResult::FoundType: |
224 | FoundTypeDecl = UnqualifiedTypeNameLookupResult::FoundType; |
225 | break; |
226 | case UnqualifiedTypeNameLookupResult::NotFound: |
227 | break; |
228 | } |
229 | } |
230 | } |
231 | } |
232 | |
233 | return FoundTypeDecl; |
234 | } |
235 | |
236 | static ParsedType recoverFromTypeInKnownDependentBase(Sema &S, |
237 | const IdentifierInfo &II, |
238 | SourceLocation NameLoc) { |
239 | |
240 | const CXXRecordDecl *RD = nullptr; |
241 | UnqualifiedTypeNameLookupResult FoundTypeDecl = |
242 | UnqualifiedTypeNameLookupResult::NotFound; |
243 | for (DeclContext *DC = S.CurContext; |
244 | DC && FoundTypeDecl == UnqualifiedTypeNameLookupResult::NotFound; |
245 | DC = DC->getParent()) { |
246 | |
247 | |
248 | RD = dyn_cast<CXXRecordDecl>(DC); |
249 | if (RD && RD->getDescribedClassTemplate()) |
250 | FoundTypeDecl = lookupUnqualifiedTypeNameInBase(S, II, NameLoc, RD); |
251 | } |
252 | if (FoundTypeDecl != UnqualifiedTypeNameLookupResult::FoundType) |
253 | return nullptr; |
254 | |
255 | |
256 | |
257 | |
258 | S.Diag(NameLoc, diag::ext_found_in_dependent_base) << &II; |
259 | |
260 | ASTContext &Context = S.Context; |
261 | auto *NNS = NestedNameSpecifier::Create(Context, nullptr, false, |
262 | cast<Type>(Context.getRecordType(RD))); |
263 | QualType T = Context.getDependentNameType(ETK_Typename, NNS, &II); |
264 | |
265 | CXXScopeSpec SS; |
266 | SS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
267 | |
268 | TypeLocBuilder Builder; |
269 | DependentNameTypeLoc DepTL = Builder.push<DependentNameTypeLoc>(T); |
270 | DepTL.setNameLoc(NameLoc); |
271 | DepTL.setElaboratedKeywordLoc(SourceLocation()); |
272 | DepTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
273 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
274 | } |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
285 | Scope *S, CXXScopeSpec *SS, |
286 | bool isClassName, bool HasTrailingDot, |
287 | ParsedType ObjectTypePtr, |
288 | bool IsCtorOrDtorName, |
289 | bool WantNontrivialTypeSourceInfo, |
290 | bool IsClassTemplateDeductionContext, |
291 | IdentifierInfo **CorrectedII) { |
292 | |
293 | bool AllowDeducedTemplate = IsClassTemplateDeductionContext && |
294 | getLangOpts().CPlusPlus17 && !IsCtorOrDtorName && |
295 | !isClassName && !HasTrailingDot; |
296 | |
297 | |
298 | DeclContext *LookupCtx = nullptr; |
299 | if (ObjectTypePtr) { |
300 | QualType ObjectType = ObjectTypePtr.get(); |
301 | if (ObjectType->isRecordType()) |
302 | LookupCtx = computeDeclContext(ObjectType); |
303 | } else if (SS && SS->isNotEmpty()) { |
304 | LookupCtx = computeDeclContext(*SS, false); |
305 | |
306 | if (!LookupCtx) { |
307 | if (isDependentScopeSpecifier(*SS)) { |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | if (!isClassName && !IsCtorOrDtorName) |
318 | return nullptr; |
319 | |
320 | |
321 | |
322 | if (WantNontrivialTypeSourceInfo) |
323 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); |
324 | |
325 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); |
326 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
327 | II, NameLoc); |
328 | return ParsedType::make(T); |
329 | } |
330 | |
331 | return nullptr; |
332 | } |
333 | |
334 | if (!LookupCtx->isDependentContext() && |
335 | RequireCompleteDeclContext(*SS, LookupCtx)) |
336 | return nullptr; |
337 | } |
338 | |
339 | |
340 | |
341 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : |
342 | LookupOrdinaryName; |
343 | LookupResult Result(*this, &II, NameLoc, Kind); |
344 | if (LookupCtx) { |
345 | |
346 | |
347 | |
348 | |
349 | LookupQualifiedName(Result, LookupCtx); |
350 | |
351 | if (ObjectTypePtr && Result.empty()) { |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | LookupName(Result, S); |
359 | } |
360 | } else { |
361 | |
362 | LookupName(Result, S); |
363 | |
364 | |
365 | |
366 | if (Result.empty() && getLangOpts().MSVCCompat && (!SS || SS->isEmpty())) { |
367 | if (ParsedType TypeInBase = |
368 | recoverFromTypeInKnownDependentBase(*this, II, NameLoc)) |
369 | return TypeInBase; |
370 | } |
371 | } |
372 | |
373 | NamedDecl *IIDecl = nullptr; |
374 | switch (Result.getResultKind()) { |
375 | case LookupResult::NotFound: |
376 | case LookupResult::NotFoundInCurrentInstantiation: |
377 | if (CorrectedII) { |
378 | TypeNameValidatorCCC CCC(true, isClassName, |
379 | AllowDeducedTemplate); |
380 | TypoCorrection Correction = CorrectTypo(Result.getLookupNameInfo(), Kind, |
381 | S, SS, CCC, CTK_ErrorRecovery); |
382 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
383 | TemplateTy Template; |
384 | bool MemberOfUnknownSpecialization; |
385 | UnqualifiedId TemplateName; |
386 | TemplateName.setIdentifier(NewII, NameLoc); |
387 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); |
388 | CXXScopeSpec NewSS, *NewSSPtr = SS; |
389 | if (SS && NNS) { |
390 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
391 | NewSSPtr = &NewSS; |
392 | } |
393 | if (Correction && (NNS || NewII != &II) && |
394 | |
395 | |
396 | |
397 | !(getLangOpts().CPlusPlus && NewSSPtr && |
398 | isTemplateName(S, *NewSSPtr, false, TemplateName, nullptr, false, |
399 | Template, MemberOfUnknownSpecialization))) { |
400 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, |
401 | isClassName, HasTrailingDot, ObjectTypePtr, |
402 | IsCtorOrDtorName, |
403 | WantNontrivialTypeSourceInfo, |
404 | IsClassTemplateDeductionContext); |
405 | if (Ty) { |
406 | diagnoseTypo(Correction, |
407 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
408 | << Result.getLookupName() << isClassName); |
409 | if (SS && NNS) |
410 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
411 | *CorrectedII = NewII; |
412 | return Ty; |
413 | } |
414 | } |
415 | } |
416 | |
417 | LLVM_FALLTHROUGH; |
418 | case LookupResult::FoundOverloaded: |
419 | case LookupResult::FoundUnresolvedValue: |
420 | Result.suppressDiagnostics(); |
421 | return nullptr; |
422 | |
423 | case LookupResult::Ambiguous: |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
430 | Result.suppressDiagnostics(); |
431 | return nullptr; |
432 | } |
433 | |
434 | |
435 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); |
436 | Res != ResEnd; ++Res) { |
437 | NamedDecl *RealRes = (*Res)->getUnderlyingDecl(); |
438 | if (isa<TypeDecl, ObjCInterfaceDecl, UnresolvedUsingIfExistsDecl>( |
439 | RealRes) || |
440 | (AllowDeducedTemplate && getAsTypeTemplateDecl(RealRes))) { |
441 | if (!IIDecl || |
442 | |
443 | RealRes->getLocation() < IIDecl->getLocation()) |
444 | IIDecl = RealRes; |
445 | } |
446 | } |
447 | |
448 | if (!IIDecl) { |
449 | |
450 | |
451 | |
452 | |
453 | |
454 | |
455 | Result.suppressDiagnostics(); |
456 | return nullptr; |
457 | } |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | break; |
464 | |
465 | case LookupResult::Found: |
466 | IIDecl = Result.getFoundDecl(); |
467 | break; |
468 | } |
469 | |
470 | assert(IIDecl && "Didn't find decl"); |
471 | |
472 | QualType T; |
473 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { |
474 | |
475 | |
476 | |
477 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
478 | auto *FoundRD = dyn_cast<CXXRecordDecl>(TD); |
479 | if (!isClassName && !IsCtorOrDtorName && LookupRD && FoundRD && |
480 | FoundRD->isInjectedClassName() && |
481 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
482 | Diag(NameLoc, diag::err_out_of_line_qualified_id_type_names_constructor) |
483 | << &II << 1; |
484 | |
485 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
486 | |
487 | T = Context.getTypeDeclType(TD); |
488 | MarkAnyDeclReferenced(TD->getLocation(), TD, false); |
489 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
490 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
491 | if (!HasTrailingDot) |
492 | T = Context.getObjCInterfaceType(IDecl); |
493 | } else if (auto *UD = dyn_cast<UnresolvedUsingIfExistsDecl>(IIDecl)) { |
494 | (void)DiagnoseUseOfDecl(UD, NameLoc); |
495 | |
496 | T = Context.IntTy; |
497 | } else if (AllowDeducedTemplate) { |
498 | if (auto *TD = getAsTypeTemplateDecl(IIDecl)) |
499 | T = Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
500 | QualType(), false); |
501 | } |
502 | |
503 | if (T.isNull()) { |
504 | |
505 | Result.suppressDiagnostics(); |
506 | return nullptr; |
507 | } |
508 | |
509 | |
510 | |
511 | |
512 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName && |
513 | !isa<ObjCInterfaceDecl, UnresolvedUsingIfExistsDecl>(IIDecl)) { |
514 | if (WantNontrivialTypeSourceInfo) { |
515 | |
516 | TypeLocBuilder Builder; |
517 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
518 | |
519 | T = getElaboratedType(ETK_None, *SS, T); |
520 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
521 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
522 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
523 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
524 | } else { |
525 | T = getElaboratedType(ETK_None, *SS, T); |
526 | } |
527 | } |
528 | |
529 | return ParsedType::make(T); |
530 | } |
531 | |
532 | |
533 | static NestedNameSpecifier * |
534 | synthesizeCurrentNestedNameSpecifier(ASTContext &Context, DeclContext *DC) { |
535 | for (;; DC = DC->getLookupParent()) { |
536 | DC = DC->getPrimaryContext(); |
537 | auto *ND = dyn_cast<NamespaceDecl>(DC); |
538 | if (ND && !ND->isInline() && !ND->isAnonymousNamespace()) |
539 | return NestedNameSpecifier::Create(Context, nullptr, ND); |
540 | else if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) |
541 | return NestedNameSpecifier::Create(Context, nullptr, RD->isTemplateDecl(), |
542 | RD->getTypeForDecl()); |
543 | else if (isa<TranslationUnitDecl>(DC)) |
544 | return NestedNameSpecifier::GlobalSpecifier(Context); |
545 | } |
546 | llvm_unreachable("something isn't in TU scope?"); |
547 | } |
548 | |
549 | |
550 | |
551 | |
552 | |
553 | static const CXXRecordDecl * |
554 | findRecordWithDependentBasesOfEnclosingMethod(const DeclContext *DC) { |
555 | for (; DC && DC->isDependentContext(); DC = DC->getLookupParent()) { |
556 | DC = DC->getPrimaryContext(); |
557 | if (const auto *MD = dyn_cast<CXXMethodDecl>(DC)) |
558 | if (MD->getParent()->hasAnyDependentBases()) |
559 | return MD->getParent(); |
560 | } |
561 | return nullptr; |
562 | } |
563 | |
564 | ParsedType Sema::ActOnMSVCUnknownTypeName(const IdentifierInfo &II, |
565 | SourceLocation NameLoc, |
566 | bool IsTemplateTypeArg) { |
567 | assert(getLangOpts().MSVCCompat && "shouldn't be called in non-MSVC mode"); |
568 | |
569 | NestedNameSpecifier *NNS = nullptr; |
570 | if (IsTemplateTypeArg && getCurScope()->isTemplateParamScope()) { |
571 | |
572 | |
573 | |
574 | |
575 | |
576 | |
577 | |
578 | NNS = synthesizeCurrentNestedNameSpecifier(Context, CurContext); |
579 | Diag(NameLoc, diag::ext_ms_delayed_template_argument) << &II; |
580 | } else if (const CXXRecordDecl *RD = |
581 | findRecordWithDependentBasesOfEnclosingMethod(CurContext)) { |
582 | |
583 | |
584 | NNS = NestedNameSpecifier::Create(Context, nullptr, RD->isTemplateDecl(), |
585 | RD->getTypeForDecl()); |
586 | |
587 | |
588 | |
589 | Diag(NameLoc, diag::ext_undeclared_unqual_id_with_dependent_base) << &II |
590 | << RD; |
591 | } else { |
592 | |
593 | return ParsedType(); |
594 | } |
595 | |
596 | QualType T = Context.getDependentNameType(ETK_None, NNS, &II); |
597 | |
598 | |
599 | |
600 | NestedNameSpecifierLocBuilder NNSLocBuilder; |
601 | NNSLocBuilder.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
602 | NestedNameSpecifierLoc QualifierLoc = NNSLocBuilder.getWithLocInContext(Context); |
603 | |
604 | TypeLocBuilder Builder; |
605 | DependentNameTypeLoc DepTL = Builder.push<DependentNameTypeLoc>(T); |
606 | DepTL.setNameLoc(NameLoc); |
607 | DepTL.setElaboratedKeywordLoc(SourceLocation()); |
608 | DepTL.setQualifierLoc(QualifierLoc); |
609 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
610 | } |
611 | |
612 | |
613 | |
614 | |
615 | |
616 | |
617 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
618 | |
619 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
620 | LookupName(R, S, false); |
621 | R.suppressDiagnostics(); |
622 | if (R.getResultKind() == LookupResult::Found) |
623 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
624 | switch (TD->getTagKind()) { |
625 | case TTK_Struct: return DeclSpec::TST_struct; |
626 | case TTK_Interface: return DeclSpec::TST_interface; |
627 | case TTK_Union: return DeclSpec::TST_union; |
628 | case TTK_Class: return DeclSpec::TST_class; |
629 | case TTK_Enum: return DeclSpec::TST_enum; |
630 | } |
631 | } |
632 | |
633 | return DeclSpec::TST_unspecified; |
634 | } |
635 | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
651 | if (CurContext->isRecord()) { |
652 | if (SS->getScopeRep()->getKind() == NestedNameSpecifier::Super) |
653 | return true; |
654 | |
655 | const Type *Ty = SS->getScopeRep()->getAsType(); |
656 | |
657 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); |
658 | for (const auto &Base : RD->bases()) |
659 | if (Ty && Context.hasSameUnqualifiedType(QualType(Ty, 1), Base.getType())) |
660 | return true; |
661 | return S->isFunctionPrototypeScope(); |
662 | } |
663 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
664 | } |
665 | |
666 | void Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
667 | SourceLocation IILoc, |
668 | Scope *S, |
669 | CXXScopeSpec *SS, |
670 | ParsedType &SuggestedType, |
671 | bool IsTemplateName) { |
672 | |
673 | if (II->isEditorPlaceholder()) |
674 | return; |
675 | |
676 | SuggestedType = nullptr; |
677 | |
678 | |
679 | |
680 | TypeNameValidatorCCC CCC(false, false, |
681 | IsTemplateName, |
682 | !IsTemplateName); |
683 | if (TypoCorrection Corrected = |
684 | CorrectTypo(DeclarationNameInfo(II, IILoc), LookupOrdinaryName, S, SS, |
685 | CCC, CTK_ErrorRecovery)) { |
686 | |
687 | bool CanRecover = !IsTemplateName; |
688 | if (Corrected.isKeyword()) { |
689 | |
690 | diagnoseTypo(Corrected, |
691 | PDiag(IsTemplateName ? diag::err_no_template_suggest |
692 | : diag::err_unknown_typename_suggest) |
693 | << II); |
694 | II = Corrected.getCorrectionAsIdentifierInfo(); |
695 | } else { |
696 | |
697 | if (!SS || !SS->isSet()) { |
698 | diagnoseTypo(Corrected, |
699 | PDiag(IsTemplateName ? diag::err_no_template_suggest |
700 | : diag::err_unknown_typename_suggest) |
701 | << II, CanRecover); |
702 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
703 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
704 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
705 | II->getName().equals(CorrectedStr); |
706 | diagnoseTypo(Corrected, |
707 | PDiag(IsTemplateName |
708 | ? diag::err_no_member_template_suggest |
709 | : diag::err_unknown_nested_typename_suggest) |
710 | << II << DC << DroppedSpecifier << SS->getRange(), |
711 | CanRecover); |
712 | } else { |
713 | llvm_unreachable("could not have corrected a typo here"); |
714 | } |
715 | |
716 | if (!CanRecover) |
717 | return; |
718 | |
719 | CXXScopeSpec tmpSS; |
720 | if (Corrected.getCorrectionSpecifier()) |
721 | tmpSS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
722 | SourceRange(IILoc)); |
723 | |
724 | SuggestedType = |
725 | getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), IILoc, S, |
726 | tmpSS.isSet() ? &tmpSS : SS, false, false, nullptr, |
727 | false, |
728 | true); |
729 | } |
730 | return; |
731 | } |
732 | |
733 | if (getLangOpts().CPlusPlus && !IsTemplateName) { |
734 | |
735 | UnqualifiedId Name; |
736 | Name.setIdentifier(II, IILoc); |
737 | CXXScopeSpec EmptySS; |
738 | TemplateTy TemplateResult; |
739 | bool MemberOfUnknownSpecialization; |
740 | if (isTemplateName(S, SS ? *SS : EmptySS, false, |
741 | Name, nullptr, true, TemplateResult, |
742 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
743 | diagnoseMissingTemplateArguments(TemplateResult.get(), IILoc); |
744 | return; |
745 | } |
746 | } |
747 | |
748 | |
749 | |
750 | |
751 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
752 | Diag(IILoc, IsTemplateName ? diag::err_no_template |
753 | : diag::err_unknown_typename) |
754 | << II; |
755 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
756 | Diag(IILoc, IsTemplateName ? diag::err_no_member_template |
757 | : diag::err_typename_nested_not_found) |
758 | << II << DC << SS->getRange(); |
759 | else if (SS->isValid() && SS->getScopeRep()->containsErrors()) { |
760 | SuggestedType = |
761 | ActOnTypenameType(S, SourceLocation(), *SS, *II, IILoc).get(); |
762 | } else if (isDependentScopeSpecifier(*SS)) { |
763 | unsigned DiagID = diag::err_typename_missing; |
764 | if (getLangOpts().MSVCCompat && isMicrosoftMissingTypename(SS, S)) |
765 | DiagID = diag::ext_typename_missing; |
766 | |
767 | Diag(SS->getRange().getBegin(), DiagID) |
768 | << SS->getScopeRep() << II->getName() |
769 | << SourceRange(SS->getRange().getBegin(), IILoc) |
770 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
771 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
772 | *SS, *II, IILoc).get(); |
773 | } else { |
774 | assert(SS && SS->isInvalid() && |
775 | "Invalid scope specifier has already been diagnosed"); |
776 | } |
777 | } |
778 | |
779 | |
780 | |
781 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { |
782 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
783 | NextToken.is(tok::less); |
784 | |
785 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
786 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) |
787 | return true; |
788 | |
789 | if (CheckTemplate && isa<TemplateDecl>(*I)) |
790 | return true; |
791 | } |
792 | |
793 | return false; |
794 | } |
795 | |
796 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
797 | Scope *S, CXXScopeSpec &SS, |
798 | IdentifierInfo *&Name, |
799 | SourceLocation NameLoc) { |
800 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
801 | SemaRef.LookupParsedName(R, S, &SS); |
802 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { |
803 | StringRef FixItTagName; |
804 | switch (Tag->getTagKind()) { |
805 | case TTK_Class: |
806 | FixItTagName = "class "; |
807 | break; |
808 | |
809 | case TTK_Enum: |
810 | FixItTagName = "enum "; |
811 | break; |
812 | |
813 | case TTK_Struct: |
814 | FixItTagName = "struct "; |
815 | break; |
816 | |
817 | case TTK_Interface: |
818 | FixItTagName = "__interface "; |
819 | break; |
820 | |
821 | case TTK_Union: |
822 | FixItTagName = "union "; |
823 | break; |
824 | } |
825 | |
826 | StringRef TagName = FixItTagName.drop_back(); |
827 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) |
828 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus |
829 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); |
830 | |
831 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
832 | I != IEnd; ++I) |
833 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
834 | << Name << TagName; |
835 | |
836 | |
837 | Result.clear(Sema::LookupTagName); |
838 | SemaRef.LookupParsedName(Result, S, &SS); |
839 | return true; |
840 | } |
841 | |
842 | return false; |
843 | } |
844 | |
845 | |
846 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, |
847 | QualType T, SourceLocation NameLoc) { |
848 | ASTContext &Context = S.Context; |
849 | |
850 | TypeLocBuilder Builder; |
851 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
852 | |
853 | T = S.getElaboratedType(ETK_None, SS, T); |
854 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
855 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
856 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
857 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
858 | } |
859 | |
860 | Sema::NameClassification Sema::ClassifyName(Scope *S, CXXScopeSpec &SS, |
861 | IdentifierInfo *&Name, |
862 | SourceLocation NameLoc, |
863 | const Token &NextToken, |
864 | CorrectionCandidateCallback *CCC) { |
865 | DeclarationNameInfo NameInfo(Name, NameLoc); |
866 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
867 | |
868 | assert(NextToken.isNot(tok::coloncolon) && |
869 | "parse nested name specifiers before calling ClassifyName"); |
870 | if (getLangOpts().CPlusPlus && SS.isSet() && |
871 | isCurrentClassName(*Name, S, &SS)) { |
872 | |
873 | |
874 | |
875 | |
876 | return NameClassification::Unknown(); |
877 | } |
878 | |
879 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
880 | LookupParsedName(Result, S, &SS, !CurMethod); |
881 | |
882 | if (SS.isInvalid()) |
883 | return NameClassification::Error(); |
884 | |
885 | |
886 | |
887 | if (Result.empty() && SS.isEmpty() && getLangOpts().MSVCCompat) { |
888 | if (ParsedType TypeInBase = |
889 | recoverFromTypeInKnownDependentBase(*this, *Name, NameLoc)) |
890 | return TypeInBase; |
891 | } |
892 | |
893 | |
894 | |
895 | |
896 | |
897 | if (SS.isEmpty() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { |
898 | DeclResult Ivar = LookupIvarInObjCMethod(Result, S, Name); |
899 | if (Ivar.isInvalid()) |
900 | return NameClassification::Error(); |
901 | if (Ivar.isUsable()) |
902 | return NameClassification::NonType(cast<NamedDecl>(Ivar.get())); |
903 | |
904 | |
905 | if (Result.empty()) |
906 | LookupBuiltin(Result); |
907 | } |
908 | |
909 | bool SecondTry = false; |
910 | bool IsFilteredTemplateName = false; |
911 | |
912 | Corrected: |
913 | switch (Result.getResultKind()) { |
914 | case LookupResult::NotFound: |
915 | |
916 | |
917 | if (SS.isEmpty() && NextToken.is(tok::l_paren)) { |
918 | |
919 | |
920 | if (getLangOpts().CPlusPlus) |
921 | return NameClassification::UndeclaredNonType(); |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | |
934 | |
935 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) |
936 | return NameClassification::NonType(D); |
937 | } |
938 | |
939 | if (getLangOpts().CPlusPlus20 && SS.isEmpty() && NextToken.is(tok::less)) { |
940 | |
941 | |
942 | |
943 | |
944 | TemplateName Template = |
945 | Context.getAssumedTemplateName(NameInfo.getName()); |
946 | return NameClassification::UndeclaredTemplate(Template); |
947 | } |
948 | |
949 | |
950 | |
951 | |
952 | if (!getLangOpts().CPlusPlus && !SecondTry && |
953 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
954 | break; |
955 | } |
956 | |
957 | |
958 | |
959 | if (!SecondTry && CCC) { |
960 | SecondTry = true; |
961 | if (TypoCorrection Corrected = |
962 | CorrectTypo(Result.getLookupNameInfo(), Result.getLookupKind(), S, |
963 | &SS, *CCC, CTK_ErrorRecovery)) { |
964 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
965 | unsigned QualifiedDiag = diag::err_no_member_suggest; |
966 | |
967 | NamedDecl *FirstDecl = Corrected.getFoundDecl(); |
968 | NamedDecl *UnderlyingFirstDecl = Corrected.getCorrectionDecl(); |
969 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
970 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
971 | UnqualifiedDiag = diag::err_no_template_suggest; |
972 | QualifiedDiag = diag::err_no_member_template_suggest; |
973 | } else if (UnderlyingFirstDecl && |
974 | (isa<TypeDecl>(UnderlyingFirstDecl) || |
975 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || |
976 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { |
977 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
978 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; |
979 | } |
980 | |
981 | if (SS.isEmpty()) { |
982 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
983 | } else { |
984 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
985 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
986 | Name->getName().equals(CorrectedStr); |
987 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
988 | << Name << computeDeclContext(SS, false) |
989 | << DroppedSpecifier << SS.getRange()); |
990 | } |
991 | |
992 | |
993 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
994 | |
995 | |
996 | if (Corrected.isKeyword()) |
997 | return Name; |
998 | |
999 | |
1000 | |
1001 | Result.clear(); |
1002 | Result.setLookupName(Corrected.getCorrection()); |
1003 | if (FirstDecl) |
1004 | Result.addDecl(FirstDecl); |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | |
1010 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { |
1011 | DeclResult R = |
1012 | LookupIvarInObjCMethod(Result, S, Ivar->getIdentifier()); |
1013 | if (R.isInvalid()) |
1014 | return NameClassification::Error(); |
1015 | if (R.isUsable()) |
1016 | return NameClassification::NonType(Ivar); |
1017 | } |
1018 | |
1019 | goto Corrected; |
1020 | } |
1021 | } |
1022 | |
1023 | |
1024 | Result.suppressDiagnostics(); |
1025 | return NameClassification::Unknown(); |
1026 | |
1027 | case LookupResult::NotFoundInCurrentInstantiation: { |
1028 | |
1029 | |
1030 | |
1031 | |
1032 | |
1033 | |
1034 | |
1035 | |
1036 | |
1037 | |
1038 | |
1039 | |
1040 | |
1041 | |
1042 | return NameClassification::DependentNonType(); |
1043 | } |
1044 | |
1045 | case LookupResult::Found: |
1046 | case LookupResult::FoundOverloaded: |
1047 | case LookupResult::FoundUnresolvedValue: |
1048 | break; |
1049 | |
1050 | case LookupResult::Ambiguous: |
1051 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
1052 | hasAnyAcceptableTemplateNames(Result, true, |
1053 | false)) { |
1054 | |
1055 | |
1056 | |
1057 | |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | FilterAcceptableTemplateNames(Result); |
1066 | if (!Result.isAmbiguous()) { |
1067 | IsFilteredTemplateName = true; |
1068 | break; |
1069 | } |
1070 | } |
1071 | |
1072 | |
1073 | return NameClassification::Error(); |
1074 | } |
1075 | |
1076 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
1077 | (IsFilteredTemplateName || |
1078 | hasAnyAcceptableTemplateNames( |
1079 | Result, true, |
1080 | false, |
1081 | SS.isEmpty() && |
1082 | getLangOpts().CPlusPlus20))) { |
1083 | |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | if (!IsFilteredTemplateName) |
1094 | FilterAcceptableTemplateNames(Result); |
1095 | |
1096 | bool IsFunctionTemplate; |
1097 | bool IsVarTemplate; |
1098 | TemplateName Template; |
1099 | if (Result.end() - Result.begin() > 1) { |
1100 | IsFunctionTemplate = true; |
1101 | Template = Context.getOverloadedTemplateName(Result.begin(), |
1102 | Result.end()); |
1103 | } else if (!Result.empty()) { |
1104 | auto *TD = cast<TemplateDecl>(getAsTemplateNameDecl( |
1105 | *Result.begin(), true, |
1106 | false)); |
1107 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); |
1108 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
1109 | |
1110 | if (SS.isNotEmpty()) |
1111 | Template = |
1112 | Context.getQualifiedTemplateName(SS.getScopeRep(), |
1113 | false, TD); |
1114 | else |
1115 | Template = TemplateName(TD); |
1116 | } else { |
1117 | |
1118 | |
1119 | IsFunctionTemplate = true; |
1120 | Template = Context.getAssumedTemplateName(NameInfo.getName()); |
1121 | } |
1122 | |
1123 | if (IsFunctionTemplate) { |
1124 | |
1125 | |
1126 | |
1127 | Result.suppressDiagnostics(); |
1128 | |
1129 | return NameClassification::FunctionTemplate(Template); |
1130 | } |
1131 | |
1132 | return IsVarTemplate ? NameClassification::VarTemplate(Template) |
1133 | : NameClassification::TypeTemplate(Template); |
1134 | } |
1135 | |
1136 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
1137 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
1138 | DiagnoseUseOfDecl(Type, NameLoc); |
1139 | MarkAnyDeclReferenced(Type->getLocation(), Type, false); |
1140 | QualType T = Context.getTypeDeclType(Type); |
1141 | if (SS.isNotEmpty()) |
1142 | return buildNestedType(*this, SS, T, NameLoc); |
1143 | return ParsedType::make(T); |
1144 | } |
1145 | |
1146 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
1147 | if (!Class) { |
1148 | |
1149 | if (ObjCCompatibleAliasDecl *Alias = |
1150 | dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) |
1151 | Class = Alias->getClassInterface(); |
1152 | } |
1153 | |
1154 | if (Class) { |
1155 | DiagnoseUseOfDecl(Class, NameLoc); |
1156 | |
1157 | if (NextToken.is(tok::period)) { |
1158 | |
1159 | |
1160 | Result.suppressDiagnostics(); |
1161 | return NameClassification::Unknown(); |
1162 | } |
1163 | |
1164 | QualType T = Context.getObjCInterfaceType(Class); |
1165 | return ParsedType::make(T); |
1166 | } |
1167 | |
1168 | if (isa<ConceptDecl>(FirstDecl)) |
1169 | return NameClassification::Concept( |
1170 | TemplateName(cast<TemplateDecl>(FirstDecl))); |
1171 | |
1172 | if (auto *EmptyD = dyn_cast<UnresolvedUsingIfExistsDecl>(FirstDecl)) { |
1173 | (void)DiagnoseUseOfDecl(EmptyD, NameLoc); |
1174 | return NameClassification::Error(); |
1175 | } |
1176 | |
1177 | |
1178 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl) && |
1179 | !isa<VarTemplateDecl>(FirstDecl)) |
1180 | return NameClassification::TypeTemplate( |
1181 | TemplateName(cast<TemplateDecl>(FirstDecl))); |
1182 | |
1183 | |
1184 | |
1185 | bool NextIsOp = NextToken.isOneOf(tok::amp, tok::star); |
1186 | if ((NextToken.is(tok::identifier) || |
1187 | (NextIsOp && |
1188 | FirstDecl->getUnderlyingDecl()->isFunctionOrFunctionTemplate())) && |
1189 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
1190 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); |
1191 | DiagnoseUseOfDecl(Type, NameLoc); |
1192 | QualType T = Context.getTypeDeclType(Type); |
1193 | if (SS.isNotEmpty()) |
1194 | return buildNestedType(*this, SS, T, NameLoc); |
1195 | return ParsedType::make(T); |
1196 | } |
1197 | |
1198 | |
1199 | |
1200 | |
1201 | |
1202 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
1203 | if (Result.isSingleResult() && !ADL && !FirstDecl->isCXXClassMember()) |
1204 | return NameClassification::NonType(Result.getRepresentativeDecl()); |
1205 | |
1206 | |
1207 | Result.suppressDiagnostics(); |
1208 | return NameClassification::OverloadSet(UnresolvedLookupExpr::Create( |
1209 | Context, Result.getNamingClass(), SS.getWithLocInContext(Context), |
1210 | Result.getLookupNameInfo(), ADL, Result.isOverloadedResult(), |
1211 | Result.begin(), Result.end())); |
1212 | } |
1213 | |
1214 | ExprResult |
1215 | Sema::ActOnNameClassifiedAsUndeclaredNonType(IdentifierInfo *Name, |
1216 | SourceLocation NameLoc) { |
1217 | assert(getLangOpts().CPlusPlus && "ADL-only call in C?"); |
1218 | CXXScopeSpec SS; |
1219 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
1220 | return BuildDeclarationNameExpr(SS, Result, true); |
1221 | } |
1222 | |
1223 | ExprResult |
1224 | Sema::ActOnNameClassifiedAsDependentNonType(const CXXScopeSpec &SS, |
1225 | IdentifierInfo *Name, |
1226 | SourceLocation NameLoc, |
1227 | bool IsAddressOfOperand) { |
1228 | DeclarationNameInfo NameInfo(Name, NameLoc); |
1229 | return ActOnDependentIdExpression(SS, SourceLocation(), |
1230 | NameInfo, IsAddressOfOperand, |
1231 | nullptr); |
1232 | } |
1233 | |
1234 | ExprResult Sema::ActOnNameClassifiedAsNonType(Scope *S, const CXXScopeSpec &SS, |
1235 | NamedDecl *Found, |
1236 | SourceLocation NameLoc, |
1237 | const Token &NextToken) { |
1238 | if (getCurMethodDecl() && SS.isEmpty()) |
1239 | if (auto *Ivar = dyn_cast<ObjCIvarDecl>(Found->getUnderlyingDecl())) |
1240 | return BuildIvarRefExpr(S, NameLoc, Ivar); |
1241 | |
1242 | |
1243 | LookupResult Result(*this, Found->getDeclName(), NameLoc, LookupOrdinaryName); |
1244 | Result.addDecl(Found); |
1245 | Result.resolveKind(); |
1246 | |
1247 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
1248 | return BuildDeclarationNameExpr(SS, Result, ADL); |
1249 | } |
1250 | |
1251 | ExprResult Sema::ActOnNameClassifiedAsOverloadSet(Scope *S, Expr *E) { |
1252 | |
1253 | |
1254 | auto *ULE = cast<UnresolvedLookupExpr>(E); |
1255 | if ((*ULE->decls_begin())->isCXXClassMember()) { |
1256 | CXXScopeSpec SS; |
1257 | SS.Adopt(ULE->getQualifierLoc()); |
1258 | |
1259 | |
1260 | LookupResult Result(*this, ULE->getName(), ULE->getNameLoc(), |
1261 | LookupOrdinaryName); |
1262 | Result.setNamingClass(ULE->getNamingClass()); |
1263 | for (auto I = ULE->decls_begin(), E = ULE->decls_end(); I != E; ++I) |
1264 | Result.addDecl(*I, I.getAccess()); |
1265 | Result.resolveKind(); |
1266 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, |
1267 | nullptr, S); |
1268 | } |
1269 | |
1270 | |
1271 | |
1272 | return ULE; |
1273 | } |
1274 | |
1275 | Sema::TemplateNameKindForDiagnostics |
1276 | Sema::getTemplateNameKindForDiagnostics(TemplateName Name) { |
1277 | auto *TD = Name.getAsTemplateDecl(); |
1278 | if (!TD) |
1279 | return TemplateNameKindForDiagnostics::DependentTemplate; |
1280 | if (isa<ClassTemplateDecl>(TD)) |
1281 | return TemplateNameKindForDiagnostics::ClassTemplate; |
1282 | if (isa<FunctionTemplateDecl>(TD)) |
1283 | return TemplateNameKindForDiagnostics::FunctionTemplate; |
1284 | if (isa<VarTemplateDecl>(TD)) |
1285 | return TemplateNameKindForDiagnostics::VarTemplate; |
1286 | if (isa<TypeAliasTemplateDecl>(TD)) |
1287 | return TemplateNameKindForDiagnostics::AliasTemplate; |
1288 | if (isa<TemplateTemplateParmDecl>(TD)) |
1289 | return TemplateNameKindForDiagnostics::TemplateTemplateParam; |
1290 | if (isa<ConceptDecl>(TD)) |
1291 | return TemplateNameKindForDiagnostics::Concept; |
1292 | return TemplateNameKindForDiagnostics::DependentTemplate; |
1293 | } |
1294 | |
1295 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
1296 | assert(DC->getLexicalParent() == CurContext && |
1297 | "The next DeclContext should be lexically contained in the current one."); |
1298 | CurContext = DC; |
1299 | S->setEntity(DC); |
1300 | } |
1301 | |
1302 | void Sema::PopDeclContext() { |
1303 | assert(CurContext && "DeclContext imbalance!"); |
1304 | |
1305 | CurContext = CurContext->getLexicalParent(); |
1306 | assert(CurContext && "Popped translation unit!"); |
1307 | } |
1308 | |
1309 | Sema::SkippedDefinitionContext Sema::ActOnTagStartSkippedDefinition(Scope *S, |
1310 | Decl *D) { |
1311 | |
1312 | |
1313 | |
1314 | auto Result = static_cast<SkippedDefinitionContext>(CurContext); |
1315 | CurContext = cast<TagDecl>(D)->getDefinition(); |
1316 | assert(CurContext && "skipping definition of undefined tag"); |
1317 | |
1318 | |
1319 | S->setEntity(CurContext->getLookupParent()); |
1320 | return Result; |
1321 | } |
1322 | |
1323 | void Sema::ActOnTagFinishSkippedDefinition(SkippedDefinitionContext Context) { |
1324 | CurContext = static_cast<decltype(CurContext)>(Context); |
1325 | } |
1326 | |
1327 | |
1328 | |
1329 | |
1330 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | assert(!S->getEntity() && "scope already has entity"); |
1348 | |
1349 | #ifndef NDEBUG |
1350 | Scope *Ancestor = S->getParent(); |
1351 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
1352 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); |
1353 | #endif |
1354 | |
1355 | CurContext = DC; |
1356 | S->setEntity(DC); |
1357 | |
1358 | if (S->getParent()->isTemplateParamScope()) { |
1359 | |
1360 | |
1361 | EnterTemplatedContext(S->getParent(), DC); |
1362 | } |
1363 | } |
1364 | |
1365 | void Sema::ExitDeclaratorContext(Scope *S) { |
1366 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
1367 | |
1368 | |
1369 | |
1370 | Scope *Ancestor = S->getParent(); |
1371 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
1372 | CurContext = Ancestor->getEntity(); |
1373 | |
1374 | |
1375 | |
1376 | } |
1377 | |
1378 | void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { |
1379 | assert(S->isTemplateParamScope() && |
1380 | "expected to be initializing a template parameter scope"); |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | unsigned ScopeDepth = getTemplateDepth(S); |
1404 | for (; S && S->isTemplateParamScope(); S = S->getParent(), --ScopeDepth) { |
1405 | DeclContext *SearchDCAfterScope = DC; |
1406 | for (; DC; DC = DC->getLookupParent()) { |
1407 | if (const TemplateParameterList *TPL = |
1408 | cast<Decl>(DC)->getDescribedTemplateParams()) { |
1409 | unsigned DCDepth = TPL->getDepth() + 1; |
1410 | if (DCDepth > ScopeDepth) |
1411 | continue; |
1412 | if (ScopeDepth == DCDepth) |
1413 | SearchDCAfterScope = DC = DC->getLookupParent(); |
1414 | break; |
1415 | } |
1416 | } |
1417 | S->setLookupEntity(SearchDCAfterScope); |
1418 | } |
1419 | } |
1420 | |
1421 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { |
1422 | |
1423 | |
1424 | FunctionDecl *FD = D->getAsFunction(); |
1425 | if (!FD) |
1426 | return; |
1427 | |
1428 | |
1429 | |
1430 | assert(CurContext == FD->getLexicalParent() && |
1431 | "The next DeclContext should be lexically contained in the current one."); |
1432 | CurContext = FD; |
1433 | S->setEntity(CurContext); |
1434 | |
1435 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
1436 | ParmVarDecl *Param = FD->getParamDecl(P); |
1437 | |
1438 | if (Param->getIdentifier()) { |
1439 | S->AddDecl(Param); |
1440 | IdResolver.AddDecl(Param); |
1441 | } |
1442 | } |
1443 | } |
1444 | |
1445 | void Sema::ActOnExitFunctionContext() { |
1446 | |
1447 | |
1448 | assert(CurContext && "DeclContext imbalance!"); |
1449 | CurContext = CurContext->getLexicalParent(); |
1450 | assert(CurContext && "Popped translation unit!"); |
1451 | } |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
1463 | ASTContext &Context, |
1464 | const FunctionDecl *New) { |
1465 | if (Context.getLangOpts().CPlusPlus) |
1466 | return true; |
1467 | |
1468 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
1469 | return true; |
1470 | |
1471 | return Previous.getResultKind() == LookupResult::Found && |
1472 | (Previous.getFoundDecl()->hasAttr<OverloadableAttr>() || |
1473 | New->hasAttr<OverloadableAttr>()); |
1474 | } |
1475 | |
1476 | |
1477 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
1478 | |
1479 | |
1480 | |
1481 | while (S->getEntity() && S->getEntity()->isTransparentContext()) |
1482 | S = S->getParent(); |
1483 | |
1484 | |
1485 | |
1486 | |
1487 | if (AddToContext) |
1488 | CurContext->addDecl(D); |
1489 | |
1490 | |
1491 | |
1492 | if (getLangOpts().CPlusPlus && D->isOutOfLine() && !S->getFnParent()) |
1493 | return; |
1494 | |
1495 | |
1496 | if (isa<FunctionDecl>(D) && |
1497 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) |
1498 | return; |
1499 | |
1500 | |
1501 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), |
1502 | IEnd = IdResolver.end(); |
1503 | for (; I != IEnd; ++I) { |
1504 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
1505 | S->RemoveDecl(*I); |
1506 | IdResolver.RemoveDecl(*I); |
1507 | |
1508 | |
1509 | break; |
1510 | } |
1511 | } |
1512 | |
1513 | S->AddDecl(D); |
1514 | |
1515 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { |
1516 | |
1517 | |
1518 | |
1519 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { |
1520 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
1521 | if (IDC == CurContext) { |
1522 | if (!S->isDeclScope(*I)) |
1523 | continue; |
1524 | } else if (IDC->Encloses(CurContext)) |
1525 | break; |
1526 | } |
1527 | |
1528 | IdResolver.InsertDeclAfter(I, D); |
1529 | } else { |
1530 | IdResolver.AddDecl(D); |
1531 | } |
1532 | warnOnReservedIdentifier(D); |
1533 | } |
1534 | |
1535 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
1536 | bool AllowInlineNamespace) { |
1537 | return IdResolver.isDeclInScope(D, Ctx, S, AllowInlineNamespace); |
1538 | } |
1539 | |
1540 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
1541 | DeclContext *TargetDC = DC->getPrimaryContext(); |
1542 | do { |
1543 | if (DeclContext *ScopeDC = S->getEntity()) |
1544 | if (ScopeDC->getPrimaryContext() == TargetDC) |
1545 | return S; |
1546 | } while ((S = S->getParent())); |
1547 | |
1548 | return nullptr; |
1549 | } |
1550 | |
1551 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
1552 | DeclContext*, |
1553 | ASTContext&); |
1554 | |
1555 | |
1556 | |
1557 | void Sema::FilterLookupForScope(LookupResult &R, DeclContext *Ctx, Scope *S, |
1558 | bool ConsiderLinkage, |
1559 | bool AllowInlineNamespace) { |
1560 | LookupResult::Filter F = R.makeFilter(); |
1561 | while (F.hasNext()) { |
1562 | NamedDecl *D = F.next(); |
1563 | |
1564 | if (isDeclInScope(D, Ctx, S, AllowInlineNamespace)) |
1565 | continue; |
1566 | |
1567 | if (ConsiderLinkage && isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
1568 | continue; |
1569 | |
1570 | F.erase(); |
1571 | } |
1572 | |
1573 | F.done(); |
1574 | } |
1575 | |
1576 | |
1577 | |
1578 | bool Sema::CheckRedeclarationModuleOwnership(NamedDecl *New, NamedDecl *Old) { |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | if (New->getFriendObjectKind() && |
1584 | Old->getOwningModuleForLinkage() != New->getOwningModuleForLinkage()) { |
1585 | New->setLocalOwningModule(Old->getOwningModule()); |
1586 | makeMergedDefinitionVisible(New); |
1587 | return false; |
1588 | } |
1589 | |
1590 | Module *NewM = New->getOwningModule(); |
1591 | Module *OldM = Old->getOwningModule(); |
1592 | |
1593 | if (NewM && NewM->Kind == Module::PrivateModuleFragment) |
1594 | NewM = NewM->Parent; |
1595 | if (OldM && OldM->Kind == Module::PrivateModuleFragment) |
1596 | OldM = OldM->Parent; |
1597 | |
1598 | if (NewM == OldM) |
1599 | return false; |
1600 | |
1601 | bool NewIsModuleInterface = NewM && NewM->isModulePurview(); |
1602 | bool OldIsModuleInterface = OldM && OldM->isModulePurview(); |
1603 | if (NewIsModuleInterface || OldIsModuleInterface) { |
1604 | |
1605 | |
1606 | |
1607 | Diag(New->getLocation(), diag::err_mismatched_owning_module) |
1608 | << New |
1609 | << NewIsModuleInterface |
1610 | << (NewIsModuleInterface ? NewM->getFullModuleName() : "") |
1611 | << OldIsModuleInterface |
1612 | << (OldIsModuleInterface ? OldM->getFullModuleName() : ""); |
1613 | Diag(Old->getLocation(), diag::note_previous_declaration); |
1614 | New->setInvalidDecl(); |
1615 | return true; |
1616 | } |
1617 | |
1618 | return false; |
1619 | } |
1620 | |
1621 | static bool isUsingDecl(NamedDecl *D) { |
1622 | return isa<UsingShadowDecl>(D) || |
1623 | isa<UnresolvedUsingTypenameDecl>(D) || |
1624 | isa<UnresolvedUsingValueDecl>(D); |
1625 | } |
1626 | |
1627 | |
1628 | static void RemoveUsingDecls(LookupResult &R) { |
1629 | LookupResult::Filter F = R.makeFilter(); |
1630 | while (F.hasNext()) |
1631 | if (isUsingDecl(F.next())) |
1632 | F.erase(); |
1633 | |
1634 | F.done(); |
1635 | } |
1636 | |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | |
1643 | |
1644 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { |
1645 | |
1646 | |
1647 | if (D->doesThisDeclarationHaveABody()) |
1648 | return false; |
1649 | |
1650 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
1651 | return CD->isCopyConstructor(); |
1652 | return D->isCopyAssignmentOperator(); |
1653 | } |
1654 | |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | |
1665 | |
1666 | |
1667 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { |
1668 | const DeclContext *DC = D->getDeclContext(); |
1669 | while (!DC->isTranslationUnit()) { |
1670 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ |
1671 | if (!RD->hasNameForLinkage()) |
1672 | return true; |
1673 | } |
1674 | DC = DC->getParent(); |
1675 | } |
1676 | |
1677 | return !D->isExternallyVisible(); |
1678 | } |
1679 | |
1680 | |
1681 | |
1682 | static bool isMainFileLoc(const Sema &S, SourceLocation Loc) { |
1683 | if (S.TUKind != TU_Complete) |
1684 | return false; |
1685 | return S.SourceMgr.isInMainFile(Loc); |
1686 | } |
1687 | |
1688 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
1689 | assert(D); |
1690 | |
1691 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
1692 | return false; |
1693 | |
1694 | |
1695 | |
1696 | if (D->getDeclContext()->isDependentContext() || |
1697 | D->getLexicalDeclContext()->isDependentContext()) |
1698 | return false; |
1699 | |
1700 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
1701 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
1702 | return false; |
1703 | |
1704 | |
1705 | if (FD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
1706 | FD->getMemberSpecializationInfo() && !FD->isOutOfLine()) |
1707 | return false; |
1708 | |
1709 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
1710 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) |
1711 | return false; |
1712 | } else { |
1713 | |
1714 | if (FD->isInlined() && !isMainFileLoc(*this, FD->getLocation())) |
1715 | return false; |
1716 | } |
1717 | |
1718 | if (FD->doesThisDeclarationHaveABody() && |
1719 | Context.DeclMustBeEmitted(FD)) |
1720 | return false; |
1721 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
1722 | |
1723 | |
1724 | |
1725 | if (!isMainFileLoc(*this, VD->getLocation())) |
1726 | return false; |
1727 | |
1728 | if (Context.DeclMustBeEmitted(VD)) |
1729 | return false; |
1730 | |
1731 | if (VD->isStaticDataMember() && |
1732 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
1733 | return false; |
1734 | if (VD->isStaticDataMember() && |
1735 | VD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
1736 | VD->getMemberSpecializationInfo() && !VD->isOutOfLine()) |
1737 | return false; |
1738 | |
1739 | if (VD->isInline() && !isMainFileLoc(*this, VD->getLocation())) |
1740 | return false; |
1741 | } else { |
1742 | return false; |
1743 | } |
1744 | |
1745 | |
1746 | |
1747 | |
1748 | return mightHaveNonExternalLinkage(D); |
1749 | } |
1750 | |
1751 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { |
1752 | if (!D) |
1753 | return; |
1754 | |
1755 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
1756 | const FunctionDecl *First = FD->getFirstDecl(); |
1757 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
1758 | return; |
1759 | } |
1760 | |
1761 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
1762 | const VarDecl *First = VD->getFirstDecl(); |
1763 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
1764 | return; |
1765 | } |
1766 | |
1767 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
1768 | UnusedFileScopedDecls.push_back(D); |
1769 | } |
1770 | |
1771 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
1772 | if (D->isInvalidDecl()) |
1773 | return false; |
1774 | |
1775 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) { |
1776 | |
1777 | |
1778 | |
1779 | for (auto *BD : DD->bindings()) |
1780 | if (BD->isReferenced()) |
1781 | return false; |
1782 | } else if (!D->getDeclName()) { |
1783 | return false; |
1784 | } else if (D->isReferenced() || D->isUsed()) { |
1785 | return false; |
1786 | } |
1787 | |
1788 | if (D->hasAttr<UnusedAttr>() || D->hasAttr<ObjCPreciseLifetimeAttr>()) |
1789 | return false; |
1790 | |
1791 | if (isa<LabelDecl>(D)) |
1792 | return true; |
1793 | |
1794 | |
1795 | |
1796 | bool WithinFunction = D->getDeclContext()->isFunctionOrMethod(); |
1797 | if (const auto *R = dyn_cast<CXXRecordDecl>(D->getDeclContext())) |
1798 | |
1799 | WithinFunction = |
1800 | WithinFunction || (R->isLocalClass() && !R->isDependentType()); |
1801 | if (!WithinFunction) |
1802 | return false; |
1803 | |
1804 | if (isa<TypedefNameDecl>(D)) |
1805 | return true; |
1806 | |
1807 | |
1808 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) |
1809 | return false; |
1810 | |
1811 | |
1812 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
1813 | |
1814 | |
1815 | const auto *Ty = VD->getType().getTypePtr(); |
1816 | |
1817 | |
1818 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
1819 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
1820 | return false; |
1821 | } |
1822 | |
1823 | |
1824 | |
1825 | if (Ty->isIncompleteType() || Ty->isDependentType()) |
1826 | return false; |
1827 | |
1828 | |
1829 | |
1830 | Ty = Ty->getBaseElementTypeUnsafe(); |
1831 | |
1832 | if (const TagType *TT = Ty->getAs<TagType>()) { |
1833 | const TagDecl *Tag = TT->getDecl(); |
1834 | if (Tag->hasAttr<UnusedAttr>()) |
1835 | return false; |
1836 | |
1837 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { |
1838 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
1839 | return false; |
1840 | |
1841 | if (const Expr *Init = VD->getInit()) { |
1842 | if (const ExprWithCleanups *Cleanups = |
1843 | dyn_cast<ExprWithCleanups>(Init)) |
1844 | Init = Cleanups->getSubExpr(); |
1845 | const CXXConstructExpr *Construct = |
1846 | dyn_cast<CXXConstructExpr>(Init); |
1847 | if (Construct && !Construct->isElidable()) { |
1848 | CXXConstructorDecl *CD = Construct->getConstructor(); |
1849 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() && |
1850 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
1851 | return false; |
1852 | } |
1853 | |
1854 | |
1855 | |
1856 | if (Init->isTypeDependent()) |
1857 | for (const CXXConstructorDecl *Ctor : RD->ctors()) |
1858 | if (!Ctor->isTrivial()) |
1859 | return false; |
1860 | } |
1861 | } |
1862 | } |
1863 | |
1864 | |
1865 | } |
1866 | |
1867 | return true; |
1868 | } |
1869 | |
1870 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
1871 | FixItHint &Hint) { |
1872 | if (isa<LabelDecl>(D)) { |
1873 | SourceLocation AfterColon = Lexer::findLocationAfterToken( |
1874 | D->getEndLoc(), tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), |
1875 | true); |
1876 | if (AfterColon.isInvalid()) |
1877 | return; |
1878 | Hint = FixItHint::CreateRemoval( |
1879 | CharSourceRange::getCharRange(D->getBeginLoc(), AfterColon)); |
1880 | } |
1881 | } |
1882 | |
1883 | void Sema::DiagnoseUnusedNestedTypedefs(const RecordDecl *D) { |
1884 | if (D->getTypeForDecl()->isDependentType()) |
1885 | return; |
1886 | |
1887 | for (auto *TmpD : D->decls()) { |
1888 | if (const auto *T = dyn_cast<TypedefNameDecl>(TmpD)) |
1889 | DiagnoseUnusedDecl(T); |
1890 | else if(const auto *R = dyn_cast<RecordDecl>(TmpD)) |
1891 | DiagnoseUnusedNestedTypedefs(R); |
1892 | } |
1893 | } |
1894 | |
1895 | |
1896 | |
1897 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
1898 | if (!ShouldDiagnoseUnusedDecl(D)) |
1899 | return; |
1900 | |
1901 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
1902 | |
1903 | |
1904 | UnusedLocalTypedefNameCandidates.insert(TD); |
1905 | return; |
1906 | } |
1907 | |
1908 | FixItHint Hint; |
1909 | GenerateFixForUnusedDecl(D, Context, Hint); |
1910 | |
1911 | unsigned DiagID; |
1912 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
1913 | DiagID = diag::warn_unused_exception_param; |
1914 | else if (isa<LabelDecl>(D)) |
1915 | DiagID = diag::warn_unused_label; |
1916 | else |
1917 | DiagID = diag::warn_unused_variable; |
1918 | |
1919 | Diag(D->getLocation(), DiagID) << D << Hint; |
1920 | } |
1921 | |
1922 | void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD) { |
1923 | |
1924 | if (!VD->isReferenced() || !VD->getDeclName() || VD->hasAttr<UnusedAttr>()) |
1925 | return; |
1926 | |
1927 | const auto *Ty = VD->getType().getTypePtr()->getBaseElementTypeUnsafe(); |
1928 | |
1929 | if (Ty->isReferenceType() || Ty->isDependentType()) |
1930 | return; |
1931 | |
1932 | if (const TagType *TT = Ty->getAs<TagType>()) { |
1933 | const TagDecl *Tag = TT->getDecl(); |
1934 | if (Tag->hasAttr<UnusedAttr>()) |
1935 | return; |
1936 | |
1937 | |
1938 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { |
1939 | if (!RD->hasAttr<WarnUnusedAttr>()) |
1940 | return; |
1941 | } |
1942 | } |
1943 | |
1944 | auto iter = RefsMinusAssignments.find(VD); |
1945 | if (iter == RefsMinusAssignments.end()) |
1946 | return; |
1947 | |
1948 | assert(iter->getSecond() >= 0 && |
1949 | "Found a negative number of references to a VarDecl"); |
1950 | if (iter->getSecond() != 0) |
1951 | return; |
1952 | unsigned DiagID = isa<ParmVarDecl>(VD) ? diag::warn_unused_but_set_parameter |
1953 | : diag::warn_unused_but_set_variable; |
1954 | Diag(VD->getLocation(), DiagID) << VD; |
1955 | } |
1956 | |
1957 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | bool Diagnose = false; |
1963 | if (L->isMSAsmLabel()) |
1964 | Diagnose = !L->isResolvedMSAsmLabel(); |
1965 | else |
1966 | Diagnose = L->getStmt() == nullptr; |
1967 | if (Diagnose) |
1968 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) << L; |
1969 | } |
1970 | |
1971 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
1972 | S->mergeNRVOIntoParent(); |
1973 | |
1974 | if (S->decl_empty()) return; |
1975 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
1976 | "Scope shouldn't contain decls!"); |
1977 | |
1978 | for (auto *TmpD : S->decls()) { |
1979 | assert(TmpD && "This decl didn't get pushed??"); |
1980 | |
1981 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
1982 | NamedDecl *D = cast<NamedDecl>(TmpD); |
1983 | |
1984 | |
1985 | if (!S->hasUnrecoverableErrorOccurred()) { |
1986 | DiagnoseUnusedDecl(D); |
1987 | if (const auto *RD = dyn_cast<RecordDecl>(D)) |
1988 | DiagnoseUnusedNestedTypedefs(RD); |
1989 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
1990 | DiagnoseUnusedButSetDecl(VD); |
1991 | RefsMinusAssignments.erase(VD); |
1992 | } |
1993 | } |
1994 | |
1995 | if (!D->getDeclName()) continue; |
1996 | |
1997 | |
1998 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) |
1999 | CheckPoppedLabel(LD, *this); |
2000 | |
2001 | |
2002 | |
2003 | IdResolver.RemoveDecl(D); |
2004 | auto ShadowI = ShadowingDecls.find(D); |
2005 | if (ShadowI != ShadowingDecls.end()) { |
2006 | if (const auto *FD = dyn_cast<FieldDecl>(ShadowI->second)) { |
2007 | Diag(D->getLocation(), diag::warn_ctor_parm_shadows_field) |
2008 | << D << FD << FD->getParent(); |
2009 | Diag(FD->getLocation(), diag::note_previous_declaration); |
2010 | } |
2011 | ShadowingDecls.erase(ShadowI); |
2012 | } |
2013 | } |
2014 | } |
2015 | |
2016 | |
2017 | |
2018 | |
2019 | |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | |
2025 | |
2026 | |
2027 | |
2028 | |
2029 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, |
2030 | SourceLocation IdLoc, |
2031 | bool DoTypoCorrection) { |
2032 | |
2033 | |
2034 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); |
2035 | |
2036 | if (!IDecl && DoTypoCorrection) { |
2037 | |
2038 | |
2039 | DeclFilterCCC<ObjCInterfaceDecl> CCC{}; |
2040 | if (TypoCorrection C = |
2041 | CorrectTypo(DeclarationNameInfo(Id, IdLoc), LookupOrdinaryName, |
2042 | TUScope, nullptr, CCC, CTK_ErrorRecovery)) { |
2043 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
2044 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
2045 | Id = IDecl->getIdentifier(); |
2046 | } |
2047 | } |
2048 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
2049 | |
2050 | if (Def && Def->getDefinition()) |
2051 | Def = Def->getDefinition(); |
2052 | return Def; |
2053 | } |
2054 | |
2055 | |
2056 | |
2057 | |
2058 | |
2059 | |
2060 | |
2061 | |
2062 | |
2063 | |
2064 | |
2065 | |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | Scope *Sema::getNonFieldDeclScope(Scope *S) { |
2079 | while (((S->getFlags() & Scope::DeclScope) == 0) || |
2080 | (S->getEntity() && S->getEntity()->isTransparentContext()) || |
2081 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
2082 | S = S->getParent(); |
2083 | return S; |
2084 | } |
2085 | |
2086 | static StringRef getHeaderName(Builtin::Context &BuiltinInfo, unsigned ID, |
2087 | ASTContext::GetBuiltinTypeError Error) { |
2088 | switch (Error) { |
2089 | case ASTContext::GE_None: |
2090 | return ""; |
2091 | case ASTContext::GE_Missing_type: |
2092 | return BuiltinInfo.getHeaderName(ID); |
2093 | case ASTContext::GE_Missing_stdio: |
2094 | return "stdio.h"; |
2095 | case ASTContext::GE_Missing_setjmp: |
2096 | return "setjmp.h"; |
2097 | case ASTContext::GE_Missing_ucontext: |
2098 | return "ucontext.h"; |
2099 | } |
2100 | llvm_unreachable("unhandled error kind"); |
2101 | } |
2102 | |
2103 | FunctionDecl *Sema::CreateBuiltin(IdentifierInfo *II, QualType Type, |
2104 | unsigned ID, SourceLocation Loc) { |
2105 | DeclContext *Parent = Context.getTranslationUnitDecl(); |
2106 | |
2107 | if (getLangOpts().CPlusPlus) { |
2108 | LinkageSpecDecl *CLinkageDecl = LinkageSpecDecl::Create( |
2109 | Context, Parent, Loc, Loc, LinkageSpecDecl::lang_c, false); |
2110 | CLinkageDecl->setImplicit(); |
2111 | Parent->addDecl(CLinkageDecl); |
2112 | Parent = CLinkageDecl; |
2113 | } |
2114 | |
2115 | FunctionDecl *New = FunctionDecl::Create(Context, Parent, Loc, Loc, II, Type, |
2116 | nullptr, SC_Extern, |
2117 | getCurFPFeatures().isFPConstrained(), |
2118 | false, Type->isFunctionProtoType()); |
2119 | New->setImplicit(); |
2120 | New->addAttr(BuiltinAttr::CreateImplicit(Context, ID)); |
2121 | |
2122 | |
2123 | |
2124 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(Type)) { |
2125 | SmallVector<ParmVarDecl *, 16> Params; |
2126 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
2127 | ParmVarDecl *parm = ParmVarDecl::Create( |
2128 | Context, New, SourceLocation(), SourceLocation(), nullptr, |
2129 | FT->getParamType(i), nullptr, SC_None, nullptr); |
2130 | parm->setScopeInfo(0, i); |
2131 | Params.push_back(parm); |
2132 | } |
2133 | New->setParams(Params); |
2134 | } |
2135 | |
2136 | AddKnownFunctionAttributes(New); |
2137 | return New; |
2138 | } |
2139 | |
2140 | |
2141 | |
2142 | |
2143 | |
2144 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, |
2145 | Scope *S, bool ForRedeclaration, |
2146 | SourceLocation Loc) { |
2147 | LookupNecessaryTypesForBuiltin(S, ID); |
2148 | |
2149 | ASTContext::GetBuiltinTypeError Error; |
2150 | QualType R = Context.GetBuiltinType(ID, Error); |
2151 | if (Error) { |
2152 | if (!ForRedeclaration) |
2153 | return nullptr; |
2154 | |
2155 | |
2156 | |
2157 | if (Error == ASTContext::GE_Missing_type || |
2158 | Context.BuiltinInfo.allowTypeMismatch(ID)) |
2159 | return nullptr; |
2160 | |
2161 | |
2162 | |
2163 | if (Error == ASTContext::GE_Missing_setjmp) { |
2164 | Diag(Loc, diag::warn_implicit_decl_no_jmp_buf) |
2165 | << Context.BuiltinInfo.getName(ID); |
2166 | return nullptr; |
2167 | } |
2168 | |
2169 | |
2170 | |
2171 | Diag(Loc, diag::warn_implicit_decl_requires_sysheader) |
2172 | << getHeaderName(Context.BuiltinInfo, ID, Error) |
2173 | << Context.BuiltinInfo.getName(ID); |
2174 | return nullptr; |
2175 | } |
2176 | |
2177 | if (!ForRedeclaration && |
2178 | (Context.BuiltinInfo.isPredefinedLibFunction(ID) || |
2179 | Context.BuiltinInfo.isHeaderDependentFunction(ID))) { |
2180 | Diag(Loc, diag::ext_implicit_lib_function_decl) |
2181 | << Context.BuiltinInfo.getName(ID) << R; |
2182 | if (const char *Header = Context.BuiltinInfo.getHeaderName(ID)) |
2183 | Diag(Loc, diag::note_include_header_or_declare) |
2184 | << Header << Context.BuiltinInfo.getName(ID); |
2185 | } |
2186 | |
2187 | if (R.isNull()) |
2188 | return nullptr; |
2189 | |
2190 | FunctionDecl *New = CreateBuiltin(II, R, ID, Loc); |
2191 | RegisterLocallyScopedExternCDecl(New, S); |
2192 | |
2193 | |
2194 | |
2195 | |
2196 | |
2197 | DeclContext *SavedContext = CurContext; |
2198 | CurContext = New->getDeclContext(); |
2199 | PushOnScopeChains(New, TUScope); |
2200 | CurContext = SavedContext; |
2201 | return New; |
2202 | } |
2203 | |
2204 | |
2205 | |
2206 | |
2207 | |
2208 | static void filterNonConflictingPreviousTypedefDecls(Sema &S, |
2209 | TypedefNameDecl *Decl, |
2210 | LookupResult &Previous) { |
2211 | |
2212 | if (!S.getLangOpts().Modules && !S.getLangOpts().ModulesLocalVisibility) |
2213 | return; |
2214 | |
2215 | |
2216 | if (Previous.empty()) |
2217 | return; |
2218 | |
2219 | LookupResult::Filter Filter = Previous.makeFilter(); |
2220 | while (Filter.hasNext()) { |
2221 | NamedDecl *Old = Filter.next(); |
2222 | |
2223 | |
2224 | if (S.isVisible(Old)) |
2225 | continue; |
2226 | |
2227 | |
2228 | |
2229 | if (auto *OldTD = dyn_cast<TypedefNameDecl>(Old)) { |
2230 | if (S.Context.hasSameType(OldTD->getUnderlyingType(), |
2231 | Decl->getUnderlyingType())) |
2232 | continue; |
2233 | |
2234 | |
2235 | |
2236 | if (OldTD->getAnonDeclWithTypedefName(true) && |
2237 | Decl->getAnonDeclWithTypedefName()) |
2238 | continue; |
2239 | } |
2240 | |
2241 | Filter.erase(); |
2242 | } |
2243 | |
2244 | Filter.done(); |
2245 | } |
2246 | |
2247 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
2248 | QualType OldType; |
2249 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) |
2250 | OldType = OldTypedef->getUnderlyingType(); |
2251 | else |
2252 | OldType = Context.getTypeDeclType(Old); |
2253 | QualType NewType = New->getUnderlyingType(); |
2254 | |
2255 | if (NewType->isVariablyModifiedType()) { |
2256 | |
2257 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
2258 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) |
2259 | << Kind << NewType; |
2260 | if (Old->getLocation().isValid()) |
2261 | notePreviousDefinition(Old, New->getLocation()); |
2262 | New->setInvalidDecl(); |
2263 | return true; |
2264 | } |
2265 | |
2266 | if (OldType != NewType && |
2267 | !OldType->isDependentType() && |
2268 | !NewType->isDependentType() && |
2269 | !Context.hasSameType(OldType, NewType)) { |
2270 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
2271 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
2272 | << Kind << NewType << OldType; |
2273 | if (Old->getLocation().isValid()) |
2274 | notePreviousDefinition(Old, New->getLocation()); |
2275 | New->setInvalidDecl(); |
2276 | return true; |
2277 | } |
2278 | return false; |
2279 | } |
2280 | |
2281 | |
2282 | |
2283 | |
2284 | |
2285 | |
2286 | void Sema::MergeTypedefNameDecl(Scope *S, TypedefNameDecl *New, |
2287 | LookupResult &OldDecls) { |
2288 | |
2289 | |
2290 | if (New->isInvalidDecl()) return; |
2291 | |
2292 | |
2293 | |
2294 | if (getLangOpts().ObjC) { |
2295 | const IdentifierInfo *TypeID = New->getIdentifier(); |
2296 | switch (TypeID->getLength()) { |
2297 | default: break; |
2298 | case 2: |
2299 | { |
2300 | if (!TypeID->isStr("id")) |
2301 | break; |
2302 | QualType T = New->getUnderlyingType(); |
2303 | if (!T->isPointerType()) |
2304 | break; |
2305 | if (!T->isVoidPointerType()) { |
2306 | QualType PT = T->castAs<PointerType>()->getPointeeType(); |
2307 | if (!PT->isStructureType()) |
2308 | break; |
2309 | } |
2310 | Context.setObjCIdRedefinitionType(T); |
2311 | |
2312 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); |
2313 | return; |
2314 | } |
2315 | case 5: |
2316 | if (!TypeID->isStr("Class")) |
2317 | break; |
2318 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
2319 | |
2320 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); |
2321 | return; |
2322 | case 3: |
2323 | if (!TypeID->isStr("SEL")) |
2324 | break; |
2325 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
2326 | |
2327 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); |
2328 | return; |
2329 | } |
2330 | |
2331 | } |
2332 | |
2333 | |
2334 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
2335 | if (!Old) { |
2336 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
2337 | << New->getDeclName(); |
2338 | |
2339 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); |
2340 | if (OldD->getLocation().isValid()) |
2341 | notePreviousDefinition(OldD, New->getLocation()); |
2342 | |
2343 | return New->setInvalidDecl(); |
2344 | } |
2345 | |
2346 | |
2347 | if (Old->isInvalidDecl()) |
2348 | return New->setInvalidDecl(); |
2349 | |
2350 | if (auto *OldTD = dyn_cast<TypedefNameDecl>(Old)) { |
2351 | auto *OldTag = OldTD->getAnonDeclWithTypedefName(true); |
2352 | auto *NewTag = New->getAnonDeclWithTypedefName(); |
2353 | NamedDecl *Hidden = nullptr; |
2354 | if (OldTag && NewTag && |
2355 | OldTag->getCanonicalDecl() != NewTag->getCanonicalDecl() && |
2356 | !hasVisibleDefinition(OldTag, &Hidden)) { |
2357 | |
2358 | |
2359 | New->setTypeForDecl(OldTD->getTypeForDecl()); |
2360 | if (OldTD->isModed()) |
2361 | New->setModedTypeSourceInfo(OldTD->getTypeSourceInfo(), |
2362 | OldTD->getUnderlyingType()); |
2363 | else |
2364 | New->setTypeSourceInfo(OldTD->getTypeSourceInfo()); |
2365 | |
2366 | |
2367 | makeMergedDefinitionVisible(Hidden); |
2368 | |
2369 | |
2370 | |
2371 | if (isa<EnumDecl>(NewTag)) { |
2372 | Scope *EnumScope = getNonFieldDeclScope(S); |
2373 | for (auto *D : NewTag->decls()) { |
2374 | auto *ED = cast<EnumConstantDecl>(D); |
2375 | assert(EnumScope->isDeclScope(ED)); |
2376 | EnumScope->RemoveDecl(ED); |
2377 | IdResolver.RemoveDecl(ED); |
2378 | ED->getLexicalDeclContext()->removeDecl(ED); |
2379 | } |
2380 | } |
2381 | } |
2382 | } |
2383 | |
2384 | |
2385 | |
2386 | if (isIncompatibleTypedef(Old, New)) |
2387 | return; |
2388 | |
2389 | |
2390 | |
2391 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) { |
2392 | New->setPreviousDecl(Typedef); |
2393 | mergeDeclAttributes(New, Old); |
2394 | } |
2395 | |
2396 | if (getLangOpts().MicrosoftExt) |
2397 | return; |
2398 | |
2399 | if (getLangOpts().CPlusPlus) { |
2400 | |
2401 | |
2402 | |
2403 | |
2404 | if (!isa<CXXRecordDecl>(CurContext)) |
2405 | return; |
2406 | |
2407 | |
2408 | |
2409 | |
2410 | |
2411 | |
2412 | |
2413 | |
2414 | |
2415 | |
2416 | |
2417 | |
2418 | |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | if (!isa<TypedefNameDecl>(Old)) |
2429 | return; |
2430 | |
2431 | Diag(New->getLocation(), diag::err_redefinition) |
2432 | << New->getDeclName(); |
2433 | notePreviousDefinition(Old, New->getLocation()); |
2434 | return New->setInvalidDecl(); |
2435 | } |
2436 | |
2437 | |
2438 | if (getLangOpts().Modules || getLangOpts().C11) |
2439 | return; |
2440 | |
2441 | |
2442 | |
2443 | |
2444 | |
2445 | if (getDiagnostics().getSuppressSystemWarnings() && |
2446 | |
2447 | (Old->isImplicit() || |
2448 | Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
2449 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) |
2450 | return; |
2451 | |
2452 | Diag(New->getLocation(), diag::ext_redefinition_of_typedef) |
2453 | << New->getDeclName(); |
2454 | notePreviousDefinition(Old, New->getLocation()); |
2455 | } |
2456 | |
2457 | |
2458 | |
2459 | static bool DeclHasAttr(const Decl *D, const Attr *A) { |
2460 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
2461 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
2462 | for (const auto *i : D->attrs()) |
2463 | if (i->getKind() == A->getKind()) { |
2464 | if (Ann) { |
2465 | if (Ann->getAnnotation() == cast<AnnotateAttr>(i)->getAnnotation()) |
2466 | return true; |
2467 | continue; |
2468 | } |
2469 | |
2470 | if (OA && isa<OwnershipAttr>(i)) |
2471 | return OA->getOwnKind() == cast<OwnershipAttr>(i)->getOwnKind(); |
2472 | return true; |
2473 | } |
2474 | |
2475 | return false; |
2476 | } |
2477 | |
2478 | static bool isAttributeTargetADefinition(Decl *D) { |
2479 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
2480 | return VD->isThisDeclarationADefinition(); |
2481 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
2482 | return TD->isCompleteDefinition() || TD->isBeingDefined(); |
2483 | return true; |
2484 | } |
2485 | |
2486 | |
2487 | |
2488 | |
2489 | |
2490 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { |
2491 | |
2492 | |
2493 | AlignedAttr *OldAlignasAttr = nullptr; |
2494 | AlignedAttr *OldStrictestAlignAttr = nullptr; |
2495 | unsigned OldAlign = 0; |
2496 | for (auto *I : Old->specific_attrs<AlignedAttr>()) { |
2497 | |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | |
2503 | if (I->isAlignmentDependent()) |
2504 | return false; |
2505 | |
2506 | if (I->isAlignas()) |
2507 | OldAlignasAttr = I; |
2508 | |
2509 | unsigned Align = I->getAlignment(S.Context); |
2510 | if (Align > OldAlign) { |
2511 | OldAlign = Align; |
2512 | OldStrictestAlignAttr = I; |
2513 | } |
2514 | } |
2515 | |
2516 | |
2517 | AlignedAttr *NewAlignasAttr = nullptr; |
2518 | unsigned NewAlign = 0; |
2519 | for (auto *I : New->specific_attrs<AlignedAttr>()) { |
2520 | if (I->isAlignmentDependent()) |
2521 | return false; |
2522 | |
2523 | if (I->isAlignas()) |
2524 | NewAlignasAttr = I; |
2525 | |
2526 | unsigned Align = I->getAlignment(S.Context); |
2527 | if (Align > NewAlign) |
2528 | NewAlign = Align; |
2529 | } |
2530 | |
2531 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { |
2532 | |
2533 | |
2534 | |
2535 | |
2536 | |
2537 | |
2538 | |
2539 | if (OldAlign == 0 || NewAlign == 0) { |
2540 | QualType Ty; |
2541 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) |
2542 | Ty = VD->getType(); |
2543 | else |
2544 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); |
2545 | |
2546 | if (OldAlign == 0) |
2547 | OldAlign = S.Context.getTypeAlign(Ty); |
2548 | if (NewAlign == 0) |
2549 | NewAlign = S.Context.getTypeAlign(Ty); |
2550 | } |
2551 | |
2552 | if (OldAlign != NewAlign) { |
2553 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) |
2554 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() |
2555 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); |
2556 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); |
2557 | } |
2558 | } |
2559 | |
2560 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { |
2561 | |
2562 | |
2563 | |
2564 | |
2565 | |
2566 | |
2567 | |
2568 | |
2569 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) |
2570 | << OldAlignasAttr; |
2571 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) |
2572 | << OldAlignasAttr; |
2573 | } |
2574 | |
2575 | bool AnyAdded = false; |
2576 | |
2577 | |
2578 | if (OldAlign > NewAlign) { |
2579 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); |
2580 | Clone->setInherited(true); |
2581 | New->addAttr(Clone); |
2582 | AnyAdded = true; |
2583 | } |
2584 | |
2585 | |
2586 | if (OldAlignasAttr && !NewAlignasAttr && |
2587 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { |
2588 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); |
2589 | Clone->setInherited(true); |
2590 | New->addAttr(Clone); |
2591 | AnyAdded = true; |
2592 | } |
2593 | |
2594 | return AnyAdded; |
2595 | } |
2596 | |
2597 | #define WANT_DECL_MERGE_LOGIC |
2598 | #include "clang/Sema/AttrParsedAttrImpl.inc" |
2599 | #undef WANT_DECL_MERGE_LOGIC |
2600 | |
2601 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, |
2602 | const InheritableAttr *Attr, |
2603 | Sema::AvailabilityMergeKind AMK) { |
2604 | |
2605 | |
2606 | if (!DiagnoseMutualExclusions(S, D, Attr)) |
2607 | return false; |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | |
2614 | |
2615 | |
2616 | InheritableAttr *NewAttr = nullptr; |
2617 | if (const auto *AA = dyn_cast<AvailabilityAttr>(Attr)) |
2618 | NewAttr = S.mergeAvailabilityAttr( |
2619 | D, *AA, AA->getPlatform(), AA->isImplicit(), AA->getIntroduced(), |
2620 | AA->getDeprecated(), AA->getObsoleted(), AA->getUnavailable(), |
2621 | AA->getMessage(), AA->getStrict(), AA->getReplacement(), AMK, |
2622 | AA->getPriority()); |
2623 | else if (const auto *VA = dyn_cast<VisibilityAttr>(Attr)) |
2624 | NewAttr = S.mergeVisibilityAttr(D, *VA, VA->getVisibility()); |
2625 | else if (const auto *VA = dyn_cast<TypeVisibilityAttr>(Attr)) |
2626 | NewAttr = S.mergeTypeVisibilityAttr(D, *VA, VA->getVisibility()); |
2627 | else if (const auto *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
2628 | NewAttr = S.mergeDLLImportAttr(D, *ImportA); |
2629 | else if (const auto *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
2630 | NewAttr = S.mergeDLLExportAttr(D, *ExportA); |
2631 | else if (const auto *EA = dyn_cast<ErrorAttr>(Attr)) |
2632 | NewAttr = S.mergeErrorAttr(D, *EA, EA->getUserDiagnostic()); |
2633 | else if (const auto *FA = dyn_cast<FormatAttr>(Attr)) |
2634 | NewAttr = S.mergeFormatAttr(D, *FA, FA->getType(), FA->getFormatIdx(), |
2635 | FA->getFirstArg()); |
2636 | else if (const auto *SA = dyn_cast<SectionAttr>(Attr)) |
2637 | NewAttr = S.mergeSectionAttr(D, *SA, SA->getName()); |
2638 | else if (const auto *CSA = dyn_cast<CodeSegAttr>(Attr)) |
2639 | NewAttr = S.mergeCodeSegAttr(D, *CSA, CSA->getName()); |
2640 | else if (const auto *IA = dyn_cast<MSInheritanceAttr>(Attr)) |
2641 | NewAttr = S.mergeMSInheritanceAttr(D, *IA, IA->getBestCase(), |
2642 | IA->getInheritanceModel()); |
2643 | else if (const auto *AA = dyn_cast<AlwaysInlineAttr>(Attr)) |
2644 | NewAttr = S.mergeAlwaysInlineAttr(D, *AA, |
2645 | &S.Context.Idents.get(AA->getSpelling())); |
2646 | else if (S.getLangOpts().CUDA && isa<FunctionDecl>(D) && |
2647 | (isa<CUDAHostAttr>(Attr) || isa<CUDADeviceAttr>(Attr) || |
2648 | isa<CUDAGlobalAttr>(Attr))) { |
2649 | |
2650 | |
2651 | return false; |
2652 | } else if (const auto *MA = dyn_cast<MinSizeAttr>(Attr)) |
2653 | NewAttr = S.mergeMinSizeAttr(D, *MA); |
2654 | else if (const auto *SNA = dyn_cast<SwiftNameAttr>(Attr)) |
2655 | NewAttr = S.mergeSwiftNameAttr(D, *SNA, SNA->getName()); |
2656 | else if (const auto *OA = dyn_cast<OptimizeNoneAttr>(Attr)) |
2657 | NewAttr = S.mergeOptimizeNoneAttr(D, *OA); |
2658 | else if (const auto *InternalLinkageA = dyn_cast<InternalLinkageAttr>(Attr)) |
2659 | NewAttr = S.mergeInternalLinkageAttr(D, *InternalLinkageA); |
2660 | else if (isa<AlignedAttr>(Attr)) |
2661 | |
2662 | |
2663 | NewAttr = nullptr; |
2664 | else if ((isa<DeprecatedAttr>(Attr) || isa<UnavailableAttr>(Attr)) && |
2665 | (AMK == Sema::AMK_Override || |
2666 | AMK == Sema::AMK_ProtocolImplementation || |
2667 | AMK == Sema::AMK_OptionalProtocolImplementation)) |
2668 | NewAttr = nullptr; |
2669 | else if (const auto *UA = dyn_cast<UuidAttr>(Attr)) |
2670 | NewAttr = S.mergeUuidAttr(D, *UA, UA->getGuid(), UA->getGuidDecl()); |
2671 | else if (const auto *IMA = dyn_cast<WebAssemblyImportModuleAttr>(Attr)) |
2672 | NewAttr = S.mergeImportModuleAttr(D, *IMA); |
2673 | else if (const auto *INA = dyn_cast<WebAssemblyImportNameAttr>(Attr)) |
2674 | NewAttr = S.mergeImportNameAttr(D, *INA); |
2675 | else if (const auto *TCBA = dyn_cast<EnforceTCBAttr>(Attr)) |
2676 | NewAttr = S.mergeEnforceTCBAttr(D, *TCBA); |
2677 | else if (const auto *TCBLA = dyn_cast<EnforceTCBLeafAttr>(Attr)) |
2678 | NewAttr = S.mergeEnforceTCBLeafAttr(D, *TCBLA); |
2679 | else if (const auto *BTFA = dyn_cast<BTFTagAttr>(Attr)) |
2680 | NewAttr = S.mergeBTFTagAttr(D, *BTFA); |
2681 | else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr)) |
2682 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
2683 | |
2684 | if (NewAttr) { |
2685 | NewAttr->setInherited(true); |
2686 | D->addAttr(NewAttr); |
2687 | if (isa<MSInheritanceAttr>(NewAttr)) |
2688 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
2689 | return true; |
2690 | } |
2691 | |
2692 | return false; |
2693 | } |
2694 | |
2695 | static const NamedDecl *getDefinition(const Decl *D) { |
2696 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) |
2697 | return TD->getDefinition(); |
2698 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
2699 | const VarDecl *Def = VD->getDefinition(); |
2700 | if (Def) |
2701 | return Def; |
2702 | return VD->getActingDefinition(); |
2703 | } |
2704 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
2705 | const FunctionDecl *Def = nullptr; |
2706 | if (FD->isDefined(Def, true)) |
2707 | return Def; |
2708 | } |
2709 | return nullptr; |
2710 | } |
2711 | |
2712 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
2713 | for (const auto *Attribute : D->attrs()) |
2714 | if (Attribute->getKind() == Kind) |
2715 | return true; |
2716 | return false; |
2717 | } |
2718 | |
2719 | |
2720 | |
2721 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { |
2722 | if (!New->hasAttrs()) |
2723 | return; |
2724 | |
2725 | const NamedDecl *Def = getDefinition(Old); |
2726 | if (!Def || Def == New) |
2727 | return; |
2728 | |
2729 | AttrVec &NewAttributes = New->getAttrs(); |
2730 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { |
2731 | const Attr *NewAttribute = NewAttributes[I]; |
2732 | |
2733 | if (isa<AliasAttr>(NewAttribute) || isa<IFuncAttr>(NewAttribute)) { |
2734 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(New)) { |
2735 | Sema::SkipBodyInfo SkipBody; |
2736 | S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def), &SkipBody); |
2737 | |
2738 | |
2739 | if (SkipBody.ShouldSkip) { |
2740 | NewAttributes.erase(NewAttributes.begin() + I); |
2741 | --E; |
2742 | continue; |
2743 | } |
2744 | } else { |
2745 | VarDecl *VD = cast<VarDecl>(New); |
2746 | unsigned Diag = cast<VarDecl>(Def)->isThisDeclarationADefinition() == |
2747 | VarDecl::TentativeDefinition |
2748 | ? diag::err_alias_after_tentative |
2749 | : diag::err_redefinition; |
2750 | S.Diag(VD->getLocation(), Diag) << VD->getDeclName(); |
2751 | if (Diag == diag::err_redefinition) |
2752 | S.notePreviousDefinition(Def, VD->getLocation()); |
2753 | else |
2754 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
2755 | VD->setInvalidDecl(); |
2756 | } |
2757 | ++I; |
2758 | continue; |
2759 | } |
2760 | |
2761 | if (const VarDecl *VD = dyn_cast<VarDecl>(Def)) { |
2762 | |
2763 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) { |
2764 | ++I; |
2765 | continue; |
2766 | } |
2767 | } |
2768 | |
2769 | if (hasAttribute(Def, NewAttribute->getKind())) { |
2770 | ++I; |
2771 | continue; |
2772 | } |
2773 | |
2774 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
2775 | |
2776 | ++I; |
2777 | continue; |
2778 | } else if (isa<UuidAttr>(NewAttribute)) { |
2779 | |
2780 | ++I; |
2781 | continue; |
2782 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
2783 | if (AA->isAlignas()) { |
2784 | |
2785 | |
2786 | |
2787 | |
2788 | |
2789 | |
2790 | |
2791 | |
2792 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) |
2793 | << AA; |
2794 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) |
2795 | << AA; |
2796 | NewAttributes.erase(NewAttributes.begin() + I); |
2797 | --E; |
2798 | continue; |
2799 | } |
2800 | } else if (isa<LoaderUninitializedAttr>(NewAttribute)) { |
2801 | |
2802 | |
2803 | |
2804 | if (!S.getLangOpts().CPlusPlus) { |
2805 | S.Diag(NewAttribute->getLocation(), |
2806 | diag::err_loader_uninitialized_redeclaration); |
2807 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
2808 | NewAttributes.erase(NewAttributes.begin() + I); |
2809 | --E; |
2810 | continue; |
2811 | } |
2812 | } else if (isa<SelectAnyAttr>(NewAttribute) && |
2813 | cast<VarDecl>(New)->isInline() && |
2814 | !cast<VarDecl>(New)->isInlineSpecified()) { |
2815 | |
2816 | |
2817 | |
2818 | |
2819 | ++I; |
2820 | continue; |
2821 | } else if (isa<OMPDeclareVariantAttr>(NewAttribute)) { |
2822 | |
2823 | |
2824 | ++I; |
2825 | continue; |
2826 | } |
2827 | |
2828 | S.Diag(NewAttribute->getLocation(), |
2829 | diag::warn_attribute_precede_definition); |
2830 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
2831 | NewAttributes.erase(NewAttributes.begin() + I); |
2832 | --E; |
2833 | } |
2834 | } |
2835 | |
2836 | static void diagnoseMissingConstinit(Sema &S, const VarDecl *InitDecl, |
2837 | const ConstInitAttr *CIAttr, |
2838 | bool AttrBeforeInit) { |
2839 | SourceLocation InsertLoc = InitDecl->getInnerLocStart(); |
2840 | |
2841 | |
2842 | |
2843 | |
2844 | |
2845 | std::string SuitableSpelling; |
2846 | if (S.getLangOpts().CPlusPlus20) |
2847 | SuitableSpelling = std::string( |
2848 | S.PP.getLastMacroWithSpelling(InsertLoc, {tok::kw_constinit})); |
2849 | if (SuitableSpelling.empty() && S.getLangOpts().CPlusPlus11) |
2850 | SuitableSpelling = std::string(S.PP.getLastMacroWithSpelling( |
2851 | InsertLoc, {tok::l_square, tok::l_square, |
2852 | S.PP.getIdentifierInfo("clang"), tok::coloncolon, |
2853 | S.PP.getIdentifierInfo("require_constant_initialization"), |
2854 | tok::r_square, tok::r_square})); |
2855 | if (SuitableSpelling.empty()) |
2856 | SuitableSpelling = std::string(S.PP.getLastMacroWithSpelling( |
2857 | InsertLoc, {tok::kw___attribute, tok::l_paren, tok::r_paren, |
2858 | S.PP.getIdentifierInfo("require_constant_initialization"), |
2859 | tok::r_paren, tok::r_paren})); |
2860 | if (SuitableSpelling.empty() && S.getLangOpts().CPlusPlus20) |
2861 | SuitableSpelling = "constinit"; |
2862 | if (SuitableSpelling.empty() && S.getLangOpts().CPlusPlus11) |
2863 | SuitableSpelling = "[[clang::require_constant_initialization]]"; |
2864 | if (SuitableSpelling.empty()) |
2865 | SuitableSpelling = "__attribute__((require_constant_initialization))"; |
2866 | SuitableSpelling += " "; |
2867 | |
2868 | if (AttrBeforeInit) { |
2869 | |
2870 | |
2871 | assert(CIAttr->isConstinit() && "should not diagnose this for attribute"); |
2872 | S.Diag(InitDecl->getLocation(), diag::ext_constinit_missing) |
2873 | << InitDecl << FixItHint::CreateInsertion(InsertLoc, SuitableSpelling); |
2874 | S.Diag(CIAttr->getLocation(), diag::note_constinit_specified_here); |
2875 | } else { |
2876 | |
2877 | |
2878 | S.Diag(CIAttr->getLocation(), |
2879 | CIAttr->isConstinit() ? diag::err_constinit_added_too_late |
2880 | : diag::warn_require_const_init_added_too_late) |
2881 | << FixItHint::CreateRemoval(SourceRange(CIAttr->getLocation())); |
2882 | S.Diag(InitDecl->getLocation(), diag::note_constinit_missing_here) |
2883 | << CIAttr->isConstinit() |
2884 | << FixItHint::CreateInsertion(InsertLoc, SuitableSpelling); |
2885 | } |
2886 | } |
2887 | |
2888 | |
2889 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
2890 | AvailabilityMergeKind AMK) { |
2891 | if (UsedAttr *OldAttr = Old->getMostRecentDecl()->getAttr<UsedAttr>()) { |
2892 | UsedAttr *NewAttr = OldAttr->clone(Context); |
2893 | NewAttr->setInherited(true); |
2894 | New->addAttr(NewAttr); |
2895 | } |
2896 | if (RetainAttr *OldAttr = Old->getMostRecentDecl()->getAttr<RetainAttr>()) { |
2897 | RetainAttr *NewAttr = OldAttr->clone(Context); |
2898 | NewAttr->setInherited(true); |
2899 | New->addAttr(NewAttr); |
2900 | } |
2901 | |
2902 | if (!Old->hasAttrs() && !New->hasAttrs()) |
2903 | return; |
2904 | |
2905 | |
2906 | |
2907 | |
2908 | const auto *OldConstInit = Old->getAttr<ConstInitAttr>(); |
2909 | const auto *NewConstInit = New->getAttr<ConstInitAttr>(); |
2910 | if (bool(OldConstInit) != bool(NewConstInit)) { |
2911 | const auto *OldVD = cast<VarDecl>(Old); |
2912 | auto *NewVD = cast<VarDecl>(New); |
2913 | |
2914 | |
2915 | |
2916 | const VarDecl *InitDecl = OldVD->getInitializingDeclaration(); |
2917 | if (!InitDecl && |
2918 | (NewVD->hasInit() || NewVD->isThisDeclarationADefinition())) |
2919 | InitDecl = NewVD; |
2920 | |
2921 | if (InitDecl == NewVD) { |
2922 | |
2923 | |
2924 | |
2925 | if (OldConstInit && OldConstInit->isConstinit()) |
2926 | diagnoseMissingConstinit(*this, NewVD, OldConstInit, |
2927 | true); |
2928 | } else if (NewConstInit) { |
2929 | |
2930 | |
2931 | |
2932 | if (InitDecl && InitDecl != NewVD) { |
2933 | diagnoseMissingConstinit(*this, InitDecl, NewConstInit, |
2934 | false); |
2935 | NewVD->dropAttr<ConstInitAttr>(); |
2936 | } |
2937 | } |
2938 | } |
2939 | |
2940 | |
2941 | checkNewAttributesAfterDef(*this, New, Old); |
2942 | |
2943 | if (AsmLabelAttr *NewA = New->getAttr<AsmLabelAttr>()) { |
2944 | if (AsmLabelAttr *OldA = Old->getAttr<AsmLabelAttr>()) { |
2945 | if (!OldA->isEquivalent(NewA)) { |
2946 | |
2947 | Diag(New->getLocation(), diag::err_different_asm_label); |
2948 | Diag(OldA->getLocation(), diag::note_previous_declaration); |
2949 | } |
2950 | } else if (Old->isUsed()) { |
2951 | |
2952 | |
2953 | Diag(New->getLocation(), diag::err_late_asm_label_name) |
2954 | << isa<FunctionDecl>(Old) << New->getAttr<AsmLabelAttr>()->getRange(); |
2955 | } |
2956 | } |
2957 | |
2958 | |
2959 | if (const auto *NewAbiTagAttr = New->getAttr<AbiTagAttr>()) { |
2960 | if (const auto *OldAbiTagAttr = Old->getAttr<AbiTagAttr>()) { |
2961 | for (const auto &NewTag : NewAbiTagAttr->tags()) { |
2962 | if (std::find(OldAbiTagAttr->tags_begin(), OldAbiTagAttr->tags_end(), |
2963 | NewTag) == OldAbiTagAttr->tags_end()) { |
2964 | Diag(NewAbiTagAttr->getLocation(), |
2965 | diag::err_new_abi_tag_on_redeclaration) |
2966 | << NewTag; |
2967 | Diag(OldAbiTagAttr->getLocation(), diag::note_previous_declaration); |
2968 | } |
2969 | } |
2970 | } else { |
2971 | Diag(NewAbiTagAttr->getLocation(), diag::err_abi_tag_on_redeclaration); |
2972 | Diag(Old->getLocation(), diag::note_previous_declaration); |
2973 | } |
2974 | } |
2975 | |
2976 | |
2977 | if (New->hasAttr<SectionAttr>() && !Old->hasAttr<SectionAttr>()) { |
2978 | if (auto *VD = dyn_cast<VarDecl>(New)) { |
2979 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) { |
2980 | Diag(New->getLocation(), diag::warn_attribute_section_on_redeclaration); |
2981 | Diag(Old->getLocation(), diag::note_previous_declaration); |
2982 | } |
2983 | } |
2984 | } |
2985 | |
2986 | |
2987 | const auto *NewCSA = New->getAttr<CodeSegAttr>(); |
2988 | if (NewCSA && !Old->hasAttr<CodeSegAttr>() && |
2989 | !NewCSA->isImplicit() && isa<CXXMethodDecl>(New)) { |
2990 | Diag(New->getLocation(), diag::warn_mismatched_section) |
2991 | << 0 ; |
2992 | Diag(Old->getLocation(), diag::note_previous_declaration); |
2993 | } |
2994 | |
2995 | if (!Old->hasAttrs()) |
2996 | return; |
2997 | |
2998 | bool foundAny = New->hasAttrs(); |
2999 | |
3000 | |
3001 | |
3002 | if (!foundAny) New->setAttrs(AttrVec()); |
3003 | |
3004 | for (auto *I : Old->specific_attrs<InheritableAttr>()) { |
3005 | |
3006 | AvailabilityMergeKind LocalAMK = AMK_None; |
3007 | if (isa<DeprecatedAttr>(I) || |
3008 | isa<UnavailableAttr>(I) || |
3009 | isa<AvailabilityAttr>(I)) { |
3010 | switch (AMK) { |
3011 | case AMK_None: |
3012 | continue; |
3013 | |
3014 | case AMK_Redeclaration: |
3015 | case AMK_Override: |
3016 | case AMK_ProtocolImplementation: |
3017 | case AMK_OptionalProtocolImplementation: |
3018 | LocalAMK = AMK; |
3019 | break; |
3020 | } |
3021 | } |
3022 | |
3023 | |
3024 | if (isa<UsedAttr>(I) || isa<RetainAttr>(I)) |
3025 | continue; |
3026 | |
3027 | if (mergeDeclAttribute(*this, New, I, LocalAMK)) |
3028 | foundAny = true; |
3029 | } |
3030 | |
3031 | if (mergeAlignedAttrs(*this, New, Old)) |
3032 | foundAny = true; |
3033 | |
3034 | if (!foundAny) New->dropAttrs(); |
3035 | } |
3036 | |
3037 | |
3038 | |
3039 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, |
3040 | const ParmVarDecl *oldDecl, |
3041 | Sema &S) { |
3042 | |
3043 | |
3044 | |
3045 | |
3046 | const CarriesDependencyAttr *CDA = newDecl->getAttr<CarriesDependencyAttr>(); |
3047 | if (CDA && !oldDecl->hasAttr<CarriesDependencyAttr>()) { |
3048 | S.Diag(CDA->getLocation(), |
3049 | diag::err_carries_dependency_missing_on_first_decl) << 1; |
3050 | |
3051 | |
3052 | const FunctionDecl *FirstFD = |
3053 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDecl(); |
3054 | const ParmVarDecl *FirstVD = |
3055 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); |
3056 | S.Diag(FirstVD->getLocation(), |
3057 | diag::note_carries_dependency_missing_first_decl) << 1; |
3058 | } |
3059 | |
3060 | if (!oldDecl->hasAttrs()) |
3061 | return; |
3062 | |
3063 | bool foundAny = newDecl->hasAttrs(); |
3064 | |
3065 | |
3066 | |
3067 | if (!foundAny) newDecl->setAttrs(AttrVec()); |
3068 | |
3069 | for (const auto *I : oldDecl->specific_attrs<InheritableParamAttr>()) { |
3070 | if (!DeclHasAttr(newDecl, I)) { |
3071 | InheritableAttr *newAttr = |
3072 | cast<InheritableParamAttr>(I->clone(S.Context)); |
3073 | newAttr->setInherited(true); |
3074 | newDecl->addAttr(newAttr); |
3075 | foundAny = true; |
3076 | } |
3077 | } |
3078 | |
3079 | if (!foundAny) newDecl->dropAttrs(); |
3080 | } |
3081 | |
3082 | static void mergeParamDeclTypes(ParmVarDecl *NewParam, |
3083 | const ParmVarDecl *OldParam, |
3084 | Sema &S) { |
3085 | if (auto Oldnullability = OldParam->getType()->getNullability(S.Context)) { |
3086 | if (auto Newnullability = NewParam->getType()->getNullability(S.Context)) { |
3087 | if (*Oldnullability != *Newnullability) { |
3088 | S.Diag(NewParam->getLocation(), diag::warn_mismatched_nullability_attr) |
3089 | << DiagNullabilityKind( |
3090 | *Newnullability, |
3091 | ((NewParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
3092 | != 0)) |
3093 | << DiagNullabilityKind( |
3094 | *Oldnullability, |
3095 | ((OldParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
3096 | != 0)); |
3097 | S.Diag(OldParam->getLocation(), diag::note_previous_declaration); |
3098 | } |
3099 | } else { |
3100 | QualType NewT = NewParam->getType(); |
3101 | NewT = S.Context.getAttributedType( |
3102 | AttributedType::getNullabilityAttrKind(*Oldnullability), |
3103 | NewT, NewT); |
3104 | NewParam->setType(NewT); |
3105 | } |
3106 | } |
3107 | } |
3108 | |
3109 | namespace { |
3110 | |
3111 | |
3112 | |
3113 | struct GNUCompatibleParamWarning { |
3114 | ParmVarDecl *OldParm; |
3115 | ParmVarDecl *NewParm; |
3116 | QualType PromotedType; |
3117 | }; |
3118 | |
3119 | } |
3120 | |
3121 | |
3122 | |
3123 | template <typename T> |
3124 | static std::pair<diag::kind, SourceLocation> |
3125 | getNoteDiagForInvalidRedeclaration(const T *Old, const T *New) { |
3126 | diag::kind PrevDiag; |
3127 | SourceLocation OldLocation = Old->getLocation(); |
3128 | if (Old->isThisDeclarationADefinition()) |
3129 | PrevDiag = diag::note_previous_definition; |
3130 | else if (Old->isImplicit()) { |
3131 | PrevDiag = diag::note_previous_implicit_declaration; |
3132 | if (OldLocation.isInvalid()) |
3133 | OldLocation = New->getLocation(); |
3134 | } else |
3135 | PrevDiag = diag::note_previous_declaration; |
3136 | return std::make_pair(PrevDiag, OldLocation); |
3137 | } |
3138 | |
3139 | |
3140 | |
3141 | |
3142 | static bool canRedefineFunction(const FunctionDecl *FD, |
3143 | const LangOptions& LangOpts) { |
3144 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
3145 | !LangOpts.CPlusPlus && |
3146 | FD->isInlineSpecified() && |
3147 | FD->getStorageClass() == SC_Extern); |
3148 | } |
3149 | |
3150 | const AttributedType *Sema::getCallingConvAttributedType(QualType T) const { |
3151 | const AttributedType *AT = T->getAs<AttributedType>(); |
3152 | while (AT && !AT->isCallingConv()) |
3153 | AT = AT->getModifiedType()->getAs<AttributedType>(); |
3154 | return AT; |
3155 | } |
3156 | |
3157 | template <typename T> |
3158 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { |
3159 | const DeclContext *DC = Old->getDeclContext(); |
3160 | if (DC->isRecord()) |
3161 | return false; |
3162 | |
3163 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); |
3164 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
3165 | return true; |
3166 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
3167 | return true; |
3168 | return false; |
3169 | } |
3170 | |
3171 | template<typename T> static bool isExternC(T *D) { return D->isExternC(); } |
3172 | static bool isExternC(VarTemplateDecl *) { return false; } |
3173 | static bool isExternC(FunctionTemplateDecl *) { return false; } |
3174 | |
3175 | |
3176 | |
3177 | |
3178 | template<typename ExpectedDecl> |
3179 | static bool checkUsingShadowRedecl(Sema &S, UsingShadowDecl *OldS, |
3180 | ExpectedDecl *New) { |
3181 | |
3182 | |
3183 | |
3184 | |
3185 | |
3186 | |
3187 | |
3188 | |
3189 | |
3190 | |
3191 | |
3192 | |
3193 | |
3194 | |
3195 | |
3196 | |
3197 | |
3198 | auto *Old = dyn_cast<ExpectedDecl>(OldS->getTargetDecl()); |
3199 | if (Old && |
3200 | !Old->getDeclContext()->getRedeclContext()->Equals( |
3201 | New->getDeclContext()->getRedeclContext()) && |
3202 | !(isExternC(Old) && isExternC(New))) |
3203 | Old = nullptr; |
3204 | |
3205 | if (!Old) { |
3206 | S.Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
3207 | S.Diag(OldS->getTargetDecl()->getLocation(), diag::note_using_decl_target); |
3208 | S.Diag(OldS->getIntroducer()->getLocation(), diag::note_using_decl) << 0; |
3209 | return true; |
3210 | } |
3211 | return false; |
3212 | } |
3213 | |
3214 | static bool hasIdenticalPassObjectSizeAttrs(const FunctionDecl *A, |
3215 | const FunctionDecl *B) { |
3216 | assert(A->getNumParams() == B->getNumParams()); |
3217 | |
3218 | auto AttrEq = [](const ParmVarDecl *A, const ParmVarDecl *B) { |
3219 | const auto *AttrA = A->getAttr<PassObjectSizeAttr>(); |
3220 | const auto *AttrB = B->getAttr<PassObjectSizeAttr>(); |
3221 | if (AttrA == AttrB) |
3222 | return true; |
3223 | return AttrA && AttrB && AttrA->getType() == AttrB->getType() && |
3224 | AttrA->isDynamic() == AttrB->isDynamic(); |
3225 | }; |
3226 | |
3227 | return std::equal(A->param_begin(), A->param_end(), B->param_begin(), AttrEq); |
3228 | } |
3229 | |
3230 | |
3231 | |
3232 | static void adjustDeclContextForDeclaratorDecl(DeclaratorDecl *NewD, |
3233 | DeclaratorDecl *OldD) { |
3234 | |
3235 | |
3236 | |
3237 | |
3238 | |
3239 | |
3240 | |
3241 | |
3242 | |
3243 | |
3244 | if (!NewD->getQualifier()) |
3245 | return; |
3246 | |
3247 | |
3248 | auto *NamedDC = NewD->getDeclContext()->getRedeclContext(); |
3249 | auto *SemaDC = OldD->getDeclContext()->getRedeclContext(); |
3250 | if (NamedDC->Equals(SemaDC)) |
3251 | return; |
3252 | |
3253 | assert((NamedDC->InEnclosingNamespaceSetOf(SemaDC) || |
3254 | NewD->isInvalidDecl() || OldD->isInvalidDecl()) && |
3255 | "unexpected context for redeclaration"); |
3256 | |
3257 | auto *LexDC = NewD->getLexicalDeclContext(); |
3258 | auto FixSemaDC = [=](NamedDecl *D) { |
3259 | if (!D) |
3260 | return; |
3261 | D->setDeclContext(SemaDC); |
3262 | D->setLexicalDeclContext(LexDC); |
3263 | }; |
3264 | |
3265 | FixSemaDC(NewD); |
3266 | if (auto *FD = dyn_cast<FunctionDecl>(NewD)) |
3267 | FixSemaDC(FD->getDescribedFunctionTemplate()); |
3268 | else if (auto *VD = dyn_cast<VarDecl>(NewD)) |
3269 | FixSemaDC(VD->getDescribedVarTemplate()); |
3270 | } |
3271 | |
3272 | |
3273 | |
3274 | |
3275 | |
3276 | |
3277 | |
3278 | |
3279 | |
3280 | |
3281 | |
3282 | |
3283 | bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, |
3284 | Scope *S, bool MergeTypeWithOld) { |
3285 | |
3286 | FunctionDecl *Old = OldD->getAsFunction(); |
3287 | if (!Old) { |
3288 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
3289 | if (New->getFriendObjectKind()) { |
3290 | Diag(New->getLocation(), diag::err_using_decl_friend); |
3291 | Diag(Shadow->getTargetDecl()->getLocation(), |
3292 | diag::note_using_decl_target); |
3293 | Diag(Shadow->getIntroducer()->getLocation(), diag::note_using_decl) |
3294 | << 0; |
3295 | return true; |
3296 | } |
3297 | |
3298 | |
3299 | |
3300 | if (FunctionTemplateDecl *NewTemplate = |
3301 | New->getDescribedFunctionTemplate()) { |
3302 | if (checkUsingShadowRedecl<FunctionTemplateDecl>(*this, Shadow, |
3303 | NewTemplate)) |
3304 | return true; |
3305 | OldD = Old = cast<FunctionTemplateDecl>(Shadow->getTargetDecl()) |
3306 | ->getAsFunction(); |
3307 | } else { |
3308 | if (checkUsingShadowRedecl<FunctionDecl>(*this, Shadow, New)) |
3309 | return true; |
3310 | OldD = Old = cast<FunctionDecl>(Shadow->getTargetDecl()); |
3311 | } |
3312 | } else { |
3313 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
3314 | << New->getDeclName(); |
3315 | notePreviousDefinition(OldD, New->getLocation()); |
3316 | return true; |
3317 | } |
3318 | } |
3319 | |
3320 | |
3321 | |
3322 | adjustDeclContextForDeclaratorDecl(New, Old); |
3323 | |
3324 | |
3325 | if (Old->isInvalidDecl()) |
3326 | return true; |
3327 | |
3328 | |
3329 | if (!getASTContext().canBuiltinBeRedeclared(Old)) { |
3330 | Diag(New->getLocation(), diag::err_builtin_redeclare) << Old->getDeclName(); |
3331 | Diag(Old->getLocation(), diag::note_previous_builtin_declaration) |
3332 | << Old << Old->getType(); |
3333 | return true; |
3334 | } |
3335 | |
3336 | diag::kind PrevDiag; |
3337 | SourceLocation OldLocation; |
3338 | std::tie(PrevDiag, OldLocation) = |
3339 | getNoteDiagForInvalidRedeclaration(Old, New); |
3340 | |
3341 | |
3342 | |
3343 | |
3344 | |
3345 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
3346 | New->getStorageClass() == SC_Static && |
3347 | Old->hasExternalFormalLinkage() && |
3348 | !New->getTemplateSpecializationInfo() && |
3349 | !canRedefineFunction(Old, getLangOpts())) { |
3350 | if (getLangOpts().MicrosoftExt) { |
3351 | Diag(New->getLocation(), diag::ext_static_non_static) << New; |
3352 | Diag(OldLocation, PrevDiag); |
3353 | } else { |
3354 | Diag(New->getLocation(), diag::err_static_non_static) << New; |
3355 | Diag(OldLocation, PrevDiag); |
3356 | return true; |
3357 | } |
3358 | } |
3359 | |
3360 | if (const auto *ILA = New->getAttr<InternalLinkageAttr>()) |
3361 | if (!Old->hasAttr<InternalLinkageAttr>()) { |
3362 | Diag(New->getLocation(), diag::err_attribute_missing_on_first_decl) |
3363 | << ILA; |
3364 | Diag(Old->getLocation(), diag::note_previous_declaration); |
3365 | New->dropAttr<InternalLinkageAttr>(); |
3366 | } |
3367 | |
3368 | if (auto *EA = New->getAttr<ErrorAttr>()) { |
3369 | if (!Old->hasAttr<ErrorAttr>()) { |
3370 | Diag(EA->getLocation(), diag::err_attribute_missing_on_first_decl) << EA; |
3371 | Diag(Old->getLocation(), diag::note_previous_declaration); |
3372 | New->dropAttr<ErrorAttr>(); |
3373 | } |
3374 | } |
3375 | |
3376 | if (CheckRedeclarationModuleOwnership(New, Old)) |
3377 | return true; |
3378 | |
3379 | if (!getLangOpts().CPlusPlus) { |
3380 | bool OldOvl = Old->hasAttr<OverloadableAttr>(); |
3381 | if (OldOvl != New->hasAttr<OverloadableAttr>() && !Old->isImplicit()) { |
3382 | Diag(New->getLocation(), diag::err_attribute_overloadable_mismatch) |
3383 | << New << OldOvl; |
3384 | |
3385 | |
3386 | |
3387 | |
3388 | |
3389 | |
3390 | |
3391 | const Decl *DiagOld = Old; |
3392 | if (OldOvl) { |
3393 | auto OldIter = llvm::find_if(Old->redecls(), [](const Decl *D) { |
3394 | const auto *A = D->getAttr<OverloadableAttr>(); |
3395 | return A && !A->isImplicit(); |
3396 | }); |
3397 | |
3398 | |
3399 | DiagOld = OldIter == Old->redecls_end() ? nullptr : *OldIter; |
3400 | } |
3401 | |
3402 | if (DiagOld) |
3403 | Diag(DiagOld->getLocation(), |
3404 | diag::note_attribute_overloadable_prev_overload) |
3405 | << OldOvl; |
3406 | |
3407 | if (OldOvl) |
3408 | New->addAttr(OverloadableAttr::CreateImplicit(Context)); |
3409 | else |
3410 | New->dropAttr<OverloadableAttr>(); |
3411 | } |
3412 | } |
3413 | |
3414 | |
3415 | |
3416 | |
3417 | |
3418 | |
3419 | |
3420 | |
3421 | |
3422 | |
3423 | |
3424 | |
3425 | |
3426 | |
3427 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
3428 | QualType NewQType = Context.getCanonicalType(New->getType()); |
3429 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
3430 | const FunctionType *NewType = cast<FunctionType>(NewQType); |
3431 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
3432 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); |
3433 | bool RequiresAdjustment = false; |
3434 | |
3435 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) { |
3436 | FunctionDecl *First = Old->getFirstDecl(); |
3437 | const FunctionType *FT = |
3438 | First->getType().getCanonicalType()->castAs<FunctionType>(); |
3439 | FunctionType::ExtInfo FI = FT->getExtInfo(); |
3440 | bool NewCCExplicit = getCallingConvAttributedType(New->getType()); |
3441 | if (!NewCCExplicit) { |
3442 | |
3443 | |
3444 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
3445 | RequiresAdjustment = true; |
3446 | } else if (Old->getBuiltinID()) { |
3447 | |
3448 | |
3449 | |
3450 | |
3451 | |
3452 | |
3453 | Diag(New->getLocation(), diag::warn_cconv_unsupported) |
3454 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
3455 | << (int)CallingConventionIgnoredReason::BuiltinFunction; |
3456 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
3457 | RequiresAdjustment = true; |
3458 | } else { |
3459 | |
3460 | bool FirstCCExplicit = getCallingConvAttributedType(First->getType()); |
3461 | Diag(New->getLocation(), diag::err_cconv_change) |
3462 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
3463 | << !FirstCCExplicit |
3464 | << (!FirstCCExplicit ? "" : |
3465 | FunctionType::getNameForCallConv(FI.getCC())); |
3466 | |
3467 | |
3468 | Diag(First->getLocation(), diag::note_previous_declaration); |
3469 | return true; |
3470 | } |
3471 | } |
3472 | |
3473 | |
3474 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
3475 | NewTypeInfo = NewTypeInfo.withNoReturn(true); |
3476 | RequiresAdjustment = true; |
3477 | } |
3478 | |
3479 | |
3480 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
3481 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { |
3482 | if (NewTypeInfo.getHasRegParm()) { |
3483 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
3484 | << NewType->getRegParmType() |
3485 | << OldType->getRegParmType(); |
3486 | Diag(OldLocation, diag::note_previous_declaration); |
3487 | return true; |
3488 | } |
3489 | |
3490 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); |
3491 | RequiresAdjustment = true; |
3492 | } |
3493 | |
3494 | |
3495 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { |
3496 | if (NewTypeInfo.getProducesResult()) { |
3497 | Diag(New->getLocation(), diag::err_function_attribute_mismatch) |
3498 | << "'ns_returns_retained'"; |
3499 | Diag(OldLocation, diag::note_previous_declaration); |
3500 | return true; |
3501 | } |
3502 | |
3503 | NewTypeInfo = NewTypeInfo.withProducesResult(true); |
3504 | RequiresAdjustment = true; |
3505 | } |
3506 | |
3507 | if (OldTypeInfo.getNoCallerSavedRegs() != |
3508 | NewTypeInfo.getNoCallerSavedRegs()) { |
3509 | if (NewTypeInfo.getNoCallerSavedRegs()) { |
3510 | AnyX86NoCallerSavedRegistersAttr *Attr = |
3511 | New->getAttr<AnyX86NoCallerSavedRegistersAttr>(); |
3512 | Diag(New->getLocation(), diag::err_function_attribute_mismatch) << Attr; |
3513 | Diag(OldLocation, diag::note_previous_declaration); |
3514 | return true; |
3515 | } |
3516 | |
3517 | NewTypeInfo = NewTypeInfo.withNoCallerSavedRegs(true); |
3518 | RequiresAdjustment = true; |
3519 | } |
3520 | |
3521 | if (RequiresAdjustment) { |
3522 | const FunctionType *AdjustedType = New->getType()->getAs<FunctionType>(); |
3523 | AdjustedType = Context.adjustFunctionType(AdjustedType, NewTypeInfo); |
3524 | New->setType(QualType(AdjustedType, 0)); |
3525 | NewQType = Context.getCanonicalType(New->getType()); |
3526 | } |
3527 | |
3528 | |
3529 | |
3530 | if (!Old->isInlined() && New->isInlined() && |
3531 | !New->hasAttr<GNUInlineAttr>() && |
3532 | !getLangOpts().GNUInline && |
3533 | Old->isUsed(false) && |
3534 | !Old->isDefined() && !New->isThisDeclarationADefinition()) |
3535 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), |
3536 | SourceLocation())); |
3537 | |
3538 | |
3539 | |
3540 | if (New->hasAttr<GNUInlineAttr>() && |
3541 | Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) { |
3542 | UndefinedButUsed.erase(Old->getCanonicalDecl()); |
3543 | } |
3544 | |
3545 | |
3546 | |
3547 | if (Old->getNumParams() > 0 && Old->getNumParams() == New->getNumParams() && |
3548 | !hasIdenticalPassObjectSizeAttrs(Old, New)) { |
3549 | Diag(New->getLocation(), diag::err_different_pass_object_size_params) |
3550 | << New->getDeclName(); |
3551 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
3552 | return true; |
3553 | } |
3554 | |
3555 | if (getLangOpts().CPlusPlus) { |
3556 | |
3557 | |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | if (CheckEquivalentExceptionSpec(Old, New)) |
3567 | return true; |
3568 | OldQType = Context.getCanonicalType(Old->getType()); |
3569 | NewQType = Context.getCanonicalType(New->getType()); |
3570 | |
3571 | |
3572 | |
3573 | |
3574 | |
3575 | |
3576 | QualType OldDeclaredReturnType = Old->getDeclaredReturnType(); |
3577 | QualType NewDeclaredReturnType = New->getDeclaredReturnType(); |
3578 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) && |
3579 | canFullyTypeCheckRedeclaration(New, Old, NewDeclaredReturnType, |
3580 | OldDeclaredReturnType)) { |
3581 | QualType ResQT; |
3582 | if (NewDeclaredReturnType->isObjCObjectPointerType() && |
3583 | OldDeclaredReturnType->isObjCObjectPointerType()) |
3584 | |
3585 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
3586 | if (ResQT.isNull()) { |
3587 | if (New->isCXXClassMember() && New->isOutOfLine()) |
3588 | Diag(New->getLocation(), diag::err_member_def_does_not_match_ret_type) |
3589 | << New << New->getReturnTypeSourceRange(); |
3590 | else |
3591 | Diag(New->getLocation(), diag::err_ovl_diff_return_type) |
3592 | << New->getReturnTypeSourceRange(); |
3593 | Diag(OldLocation, PrevDiag) << Old << Old->getType() |
3594 | << Old->getReturnTypeSourceRange(); |
3595 | return true; |
3596 | } |
3597 | else |
3598 | NewQType = ResQT; |
3599 | } |
3600 | |
3601 | QualType OldReturnType = OldType->getReturnType(); |
3602 | QualType NewReturnType = cast<FunctionType>(NewQType)->getReturnType(); |
3603 | if (OldReturnType != NewReturnType) { |
3604 | |
3605 | |
3606 | AutoType *OldAT = Old->getReturnType()->getContainedAutoType(); |
3607 | if (OldAT && OldAT->isDeduced()) { |
3608 | New->setType( |
3609 | SubstAutoType(New->getType(), |
3610 | OldAT->isDependentType() ? Context.DependentTy |
3611 | : OldAT->getDeducedType())); |
3612 | NewQType = Context.getCanonicalType( |
3613 | SubstAutoType(NewQType, |
3614 | OldAT->isDependentType() ? Context.DependentTy |
3615 | : OldAT->getDeducedType())); |
3616 | } |
3617 | } |
3618 | |
3619 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
3620 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
3621 | if (OldMethod && NewMethod) { |
3622 | |
3623 | NewMethod->setTrivial(OldMethod->isTrivial()); |
3624 | |
3625 | |
3626 | |
3627 | |
3628 | bool IsClassScopeExplicitSpecialization = |
3629 | OldMethod->isFunctionTemplateSpecialization() && |
3630 | NewMethod->isFunctionTemplateSpecialization(); |
3631 | bool isFriend = NewMethod->getFriendObjectKind(); |
3632 | |
3633 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
3634 | !IsClassScopeExplicitSpecialization) { |
3635 | |
3636 | |
3637 | |
3638 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
3639 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
3640 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
3641 | return true; |
3642 | } |
3643 | |
3644 | |
3645 | |
3646 | |
3647 | |
3648 | if (!inTemplateInstantiation()) { |
3649 | unsigned NewDiag; |
3650 | if (isa<CXXConstructorDecl>(OldMethod)) |
3651 | NewDiag = diag::err_constructor_redeclared; |
3652 | else if (isa<CXXDestructorDecl>(NewMethod)) |
3653 | NewDiag = diag::err_destructor_redeclared; |
3654 | else if (isa<CXXConversionDecl>(NewMethod)) |
3655 | NewDiag = diag::err_conv_function_redeclared; |
3656 | else |
3657 | NewDiag = diag::err_member_redeclared; |
3658 | |
3659 | Diag(New->getLocation(), NewDiag); |
3660 | } else { |
3661 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) |
3662 | << New << New->getType(); |
3663 | } |
3664 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
3665 | return true; |
3666 | |
3667 | |
3668 | |
3669 | |
3670 | |
3671 | |
3672 | } else if (OldMethod->isImplicit()) { |
3673 | if (isFriend) { |
3674 | NewMethod->setImplicit(); |
3675 | } else { |
3676 | Diag(NewMethod->getLocation(), |
3677 | diag::err_definition_of_implicitly_declared_member) |
3678 | << New << getSpecialMember(OldMethod); |
3679 | return true; |
3680 | } |
3681 | } else if (OldMethod->getFirstDecl()->isExplicitlyDefaulted() && !isFriend) { |
3682 | Diag(NewMethod->getLocation(), |
3683 | diag::err_definition_of_explicitly_defaulted_member) |
3684 | << getSpecialMember(OldMethod); |
3685 | return true; |
3686 | } |
3687 | } |
3688 | |
3689 | |
3690 | |
3691 | |
3692 | |
3693 | if (const auto *NRA = New->getAttr<CXX11NoReturnAttr>()) |
3694 | if (!Old->hasAttr<CXX11NoReturnAttr>()) { |
3695 | Diag(NRA->getLocation(), diag::err_attribute_missing_on_first_decl) |
3696 | << NRA; |
3697 | Diag(Old->getLocation(), diag::note_previous_declaration); |
3698 | } |
3699 | |
3700 | |
3701 | |
3702 | |
3703 | |
3704 | const CarriesDependencyAttr *CDA = New->getAttr<CarriesDependencyAttr>(); |
3705 | if (CDA && !Old->hasAttr<CarriesDependencyAttr>()) { |
3706 | Diag(CDA->getLocation(), |
3707 | diag::err_carries_dependency_missing_on_first_decl) << 0; |
3708 | Diag(Old->getFirstDecl()->getLocation(), |
3709 | diag::note_carries_dependency_missing_first_decl) << 0; |
3710 | } |
3711 | |
3712 | |
3713 | |
3714 | |
3715 | |
3716 | |
3717 | |
3718 | QualType OldQTypeForComparison = OldQType; |
3719 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { |
3720 | auto *OldType = OldQType->castAs<FunctionProtoType>(); |
3721 | const FunctionType *OldTypeForComparison |
3722 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); |
3723 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); |
3724 | assert(OldQTypeForComparison.isCanonical()); |
3725 | } |
3726 | |
3727 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
3728 | |
3729 | |
3730 | |
3731 | |
3732 | |
3733 | |
3734 | |
3735 | |
3736 | if (New->getFriendObjectKind() != Decl::FOK_None) { |
3737 | Diag(New->getLocation(), diag::ext_retained_language_linkage) << New; |
3738 | Diag(OldLocation, PrevDiag); |
3739 | } else { |
3740 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
3741 | Diag(OldLocation, PrevDiag); |
3742 | return true; |
3743 | } |
3744 | } |
3745 | |
3746 | |
3747 | |
3748 | |
3749 | |
3750 | if (Context.hasSameFunctionTypeIgnoringExceptionSpec(OldQTypeForComparison, |
3751 | NewQType)) |
3752 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
3753 | |
3754 | |
3755 | |
3756 | |
3757 | if (!canFullyTypeCheckRedeclaration(New, Old, NewQType, OldQType)) |
3758 | return false; |
3759 | |
3760 | |
3761 | } |
3762 | |
3763 | |
3764 | |
3765 | if (!getLangOpts().CPlusPlus && |
3766 | Context.typesAreCompatible(OldQType, NewQType)) { |
3767 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
3768 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); |
3769 | const FunctionProtoType *OldProto = nullptr; |
3770 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
3771 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
3772 | |
3773 | |
3774 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
3775 | SmallVector<QualType, 16> ParamTypes(OldProto->param_types()); |
3776 | NewQType = |
3777 | Context.getFunctionType(NewFuncType->getReturnType(), ParamTypes, |
3778 | OldProto->getExtProtoInfo()); |
3779 | New->setType(NewQType); |
3780 | New->setHasInheritedPrototype(); |
3781 | |
3782 | |
3783 | SmallVector<ParmVarDecl*, 16> Params; |
3784 | for (const auto &ParamType : OldProto->param_types()) { |
3785 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, SourceLocation(), |
3786 | SourceLocation(), nullptr, |
3787 | ParamType, nullptr, |
3788 | SC_None, nullptr); |
3789 | Param->setScopeInfo(0, Params.size()); |
3790 | Param->setImplicit(); |
3791 | Params.push_back(Param); |
3792 | } |
3793 | |
3794 | New->setParams(Params); |
3795 | } |
3796 | |
3797 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
3798 | } |
3799 | |
3800 | |
3801 | |
3802 | if (Context.hasSameFunctionTypeIgnoringPtrSizes(OldQType, NewQType)) |
3803 | return false; |
3804 | |
3805 | |
3806 | |
3807 | |
3808 | |
3809 | |
3810 | |
3811 | |
3812 | |
3813 | |
3814 | |
3815 | |
3816 | if (!getLangOpts().CPlusPlus && |
3817 | Old->hasPrototype() && !New->hasPrototype() && |
3818 | New->getType()->getAs<FunctionProtoType>() && |
3819 | Old->getNumParams() == New->getNumParams()) { |
3820 | SmallVector<QualType, 16> ArgTypes; |
3821 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; |
3822 | const FunctionProtoType *OldProto |
3823 | = Old->getType()->getAs<FunctionProtoType>(); |
3824 | const FunctionProtoType *NewProto |
3825 | = New->getType()->getAs<FunctionProtoType>(); |
3826 | |
3827 | |
3828 | QualType MergedReturn = Context.mergeTypes(OldProto->getReturnType(), |
3829 | NewProto->getReturnType()); |
3830 | bool LooseCompatible = !MergedReturn.isNull(); |
3831 | for (unsigned Idx = 0, End = Old->getNumParams(); |
3832 | LooseCompatible && Idx != End; ++Idx) { |
3833 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
3834 | ParmVarDecl *NewParm = New->getParamDecl(Idx); |
3835 | if (Context.typesAreCompatible(OldParm->getType(), |
3836 | NewProto->getParamType(Idx))) { |
3837 | ArgTypes.push_back(NewParm->getType()); |
3838 | } else if (Context.typesAreCompatible(OldParm->getType(), |
3839 | NewParm->getType(), |
3840 | true)) { |
3841 | GNUCompatibleParamWarning Warn = { OldParm, NewParm, |
3842 | NewProto->getParamType(Idx) }; |
3843 | Warnings.push_back(Warn); |
3844 | ArgTypes.push_back(NewParm->getType()); |
3845 | } else |
3846 | LooseCompatible = false; |
3847 | } |
3848 | |
3849 | if (LooseCompatible) { |
3850 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
3851 | Diag(Warnings[Warn].NewParm->getLocation(), |
3852 | diag::ext_param_promoted_not_compatible_with_prototype) |
3853 | << Warnings[Warn].PromotedType |
3854 | << Warnings[Warn].OldParm->getType(); |
3855 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
3856 | Diag(Warnings[Warn].OldParm->getLocation(), |
3857 | diag::note_previous_declaration); |
3858 | } |
3859 | |
3860 | if (MergeTypeWithOld) |
3861 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, |
3862 | OldProto->getExtProtoInfo())); |
3863 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
3864 | } |
3865 | |
3866 | |
3867 | } |
3868 | |
3869 | |
3870 | |
3871 | |
3872 | |
3873 | |
3874 | unsigned BuiltinID; |
3875 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { |
3876 | |
3877 | |
3878 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
3879 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
3880 | Diag(OldLocation, diag::note_previous_builtin_declaration) |
3881 | << Old << Old->getType(); |
3882 | return false; |
3883 | } |
3884 | |
3885 | PrevDiag = diag::note_previous_builtin_declaration; |
3886 | } |
3887 | |
3888 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
3889 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
3890 | return true; |
3891 | } |
3892 | |
3893 | |
3894 | |
3895 | |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | |
3902 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
3903 | Scope *S, bool MergeTypeWithOld) { |
3904 | |
3905 | mergeDeclAttributes(New, Old); |
3906 | |
3907 | |
3908 | if (Old->isPure()) |
3909 | New->setPure(); |
3910 | |
3911 | |
3912 | if (Old->getMostRecentDecl()->isUsed(false)) |
3913 | New->setIsUsed(); |
3914 | |
3915 | |
3916 | |
3917 | if (New->getNumParams() == Old->getNumParams()) |
3918 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) { |
3919 | ParmVarDecl *NewParam = New->getParamDecl(i); |
3920 | ParmVarDecl *OldParam = Old->getParamDecl(i); |
3921 | mergeParamDeclAttributes(NewParam, OldParam, *this); |
3922 | mergeParamDeclTypes(NewParam, OldParam, *this); |
3923 | } |
3924 | |
3925 | if (getLangOpts().CPlusPlus) |
3926 | return MergeCXXFunctionDecl(New, Old, S); |
3927 | |
3928 | |
3929 | |
3930 | |
3931 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
3932 | if (!Merged.isNull() && MergeTypeWithOld) |
3933 | New->setType(Merged); |
3934 | |
3935 | return false; |
3936 | } |
3937 | |
3938 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
3939 | ObjCMethodDecl *oldMethod) { |
3940 | |
3941 | AvailabilityMergeKind MergeKind = |
3942 | isa<ObjCProtocolDecl>(oldMethod->getDeclContext()) |
3943 | ? (oldMethod->isOptional() ? AMK_OptionalProtocolImplementation |
3944 | : AMK_ProtocolImplementation) |
3945 | : isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration |
3946 | : AMK_Override; |
3947 | |
3948 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); |
3949 | |
3950 | |
3951 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
3952 | oe = oldMethod->param_end(); |
3953 | for (ObjCMethodDecl::param_iterator |
3954 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
3955 | ni != ne && oi != oe; ++ni, ++oi) |
3956 | mergeParamDeclAttributes(*ni, *oi, *this); |
3957 | |
3958 | CheckObjCMethodOverride(newMethod, oldMethod); |
3959 | } |
3960 | |
3961 | static void diagnoseVarDeclTypeMismatch(Sema &S, VarDecl *New, VarDecl* Old) { |
3962 | assert(!S.Context.hasSameType(New->getType(), Old->getType())); |
3963 | |
3964 | S.Diag(New->getLocation(), New->isThisDeclarationADefinition() |
3965 | ? diag::err_redefinition_different_type |
3966 | : diag::err_redeclaration_different_type) |
3967 | << New->getDeclName() << New->getType() << Old->getType(); |
3968 | |
3969 | diag::kind PrevDiag; |
3970 | SourceLocation OldLocation; |
3971 | std::tie(PrevDiag, OldLocation) |
3972 | = getNoteDiagForInvalidRedeclaration(Old, New); |
3973 | S.Diag(OldLocation, PrevDiag); |
3974 | New->setInvalidDecl(); |
3975 | } |
3976 | |
3977 | |
3978 | |
3979 | |
3980 | |
3981 | |
3982 | |
3983 | |
3984 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
3985 | bool MergeTypeWithOld) { |
3986 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
3987 | return; |
3988 | |
3989 | QualType MergedT; |
3990 | if (getLangOpts().CPlusPlus) { |
3991 | if (New->getType()->isUndeducedType()) { |
3992 | |
3993 | return; |
3994 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
3995 | |
3996 | return MergeVarDeclExceptionSpecs(New, Old); |
3997 | } |
3998 | |
3999 | |
4000 | |
4001 | |
4002 | |
4003 | else if (Old->getType()->isArrayType() && New->getType()->isArrayType()) { |
4004 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
4005 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
4006 | |
4007 | |
4008 | |
4009 | |
4010 | if (!NewArray->isIncompleteArrayType() && !NewArray->isDependentType()) { |
4011 | for (VarDecl *PrevVD = Old->getMostRecentDecl(); PrevVD; |
4012 | PrevVD = PrevVD->getPreviousDecl()) { |
4013 | QualType PrevVDTy = PrevVD->getType(); |
4014 | if (PrevVDTy->isIncompleteArrayType() || PrevVDTy->isDependentType()) |
4015 | continue; |
4016 | |
4017 | if (!Context.hasSameType(New->getType(), PrevVDTy)) |
4018 | return diagnoseVarDeclTypeMismatch(*this, New, PrevVD); |
4019 | } |
4020 | } |
4021 | |
4022 | if (OldArray->isIncompleteArrayType() && NewArray->isArrayType()) { |
4023 | if (Context.hasSameType(OldArray->getElementType(), |
4024 | NewArray->getElementType())) |
4025 | MergedT = New->getType(); |
4026 | } |
4027 | |
4028 | |
4029 | |
4030 | else if (OldArray->isArrayType() && NewArray->isIncompleteArrayType()) { |
4031 | if (Context.hasSameType(OldArray->getElementType(), |
4032 | NewArray->getElementType())) |
4033 | MergedT = Old->getType(); |
4034 | } |
4035 | } |
4036 | else if (New->getType()->isObjCObjectPointerType() && |
4037 | Old->getType()->isObjCObjectPointerType()) { |
4038 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), |
4039 | Old->getType()); |
4040 | } |
4041 | } else { |
4042 | |
4043 | |
4044 | |
4045 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
4046 | } |
4047 | if (MergedT.isNull()) { |
4048 | |
4049 | |
4050 | |
4051 | |
4052 | |
4053 | if ((New->getType()->isDependentType() || |
4054 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { |
4055 | |
4056 | |
4057 | |
4058 | if (!New->getType()->isDependentType() && MergeTypeWithOld) |
4059 | New->setType(Context.DependentTy); |
4060 | return; |
4061 | } |
4062 | return diagnoseVarDeclTypeMismatch(*this, New, Old); |
4063 | } |
4064 | |
4065 | |
4066 | |
4067 | if (MergeTypeWithOld) |
4068 | New->setType(MergedT); |
4069 | } |
4070 | |
4071 | static bool mergeTypeWithPrevious(Sema &S, VarDecl *NewVD, VarDecl *OldVD, |
4072 | LookupResult &Previous) { |
4073 | |
4074 | |
4075 | |
4076 | |
4077 | |
4078 | |
4079 | |
4080 | |
4081 | if (Previous.isShadowed()) |
4082 | return false; |
4083 | |
4084 | if (S.getLangOpts().CPlusPlus) { |
4085 | |
4086 | |
4087 | |
4088 | |
4089 | return NewVD->isPreviousDeclInSameBlockScope() || |
4090 | (!OldVD->getLexicalDeclContext()->isFunctionOrMethod() && |
4091 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); |
4092 | } else { |
4093 | |
4094 | |
4095 | return !OldVD->getLexicalDeclContext()->isFunctionOrMethod() || |
4096 | OldVD->getLexicalDeclContext() == NewVD->getLexicalDeclContext(); |
4097 | } |
4098 | } |
4099 | |
4100 | |
4101 | |
4102 | |
4103 | |
4104 | |
4105 | |
4106 | |
4107 | |
4108 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { |
4109 | |
4110 | if (New->isInvalidDecl()) |
4111 | return; |
4112 | |
4113 | if (!shouldLinkPossiblyHiddenDecl(Previous, New)) |
4114 | return; |
4115 | |
4116 | VarTemplateDecl *NewTemplate = New->getDescribedVarTemplate(); |
4117 | |
4118 | |
4119 | VarDecl *Old = nullptr; |
4120 | VarTemplateDecl *OldTemplate = nullptr; |
4121 | if (Previous.isSingleResult()) { |
4122 | if (NewTemplate) { |
4123 | OldTemplate = dyn_cast<VarTemplateDecl>(Previous.getFoundDecl()); |
4124 | Old = OldTemplate ? OldTemplate->getTemplatedDecl() : nullptr; |
4125 | |
4126 | if (auto *Shadow = |
4127 | dyn_cast<UsingShadowDecl>(Previous.getRepresentativeDecl())) |
4128 | if (checkUsingShadowRedecl<VarTemplateDecl>(*this, Shadow, NewTemplate)) |
4129 | return New->setInvalidDecl(); |
4130 | } else { |
4131 | Old = dyn_cast<VarDecl>(Previous.getFoundDecl()); |
4132 | |
4133 | if (auto *Shadow = |
4134 | dyn_cast<UsingShadowDecl>(Previous.getRepresentativeDecl())) |
4135 | if (checkUsingShadowRedecl<VarDecl>(*this, Shadow, New)) |
4136 | return New->setInvalidDecl(); |
4137 | } |
4138 | } |
4139 | if (!Old) { |
4140 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
4141 | << New->getDeclName(); |
4142 | notePreviousDefinition(Previous.getRepresentativeDecl(), |
4143 | New->getLocation()); |
4144 | return New->setInvalidDecl(); |
4145 | } |
4146 | |
4147 | |
4148 | |
4149 | adjustDeclContextForDeclaratorDecl(New, Old); |
4150 | |
4151 | |
4152 | if (NewTemplate && |
4153 | !TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
4154 | OldTemplate->getTemplateParameters(), |
4155 | true, TPL_TemplateMatch)) |
4156 | return New->setInvalidDecl(); |
4157 | |
4158 | |
4159 | |
4160 | |
4161 | |
4162 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { |
4163 | Diag(New->getLocation(), diag::err_duplicate_member) |
4164 | << New->getIdentifier(); |
4165 | Diag(Old->getLocation(), diag::note_previous_declaration); |
4166 | New->setInvalidDecl(); |
4167 | } |
4168 | |
4169 | mergeDeclAttributes(New, Old); |
4170 | |
4171 | |
4172 | if (New->hasAttr<WeakImportAttr>() && |
4173 | Old->getStorageClass() == SC_None && |
4174 | !Old->hasAttr<WeakImportAttr>()) { |
4175 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); |
4176 | Diag(Old->getLocation(), diag::note_previous_declaration); |
4177 | |
4178 | New->dropAttr<WeakImportAttr>(); |
4179 | } |
4180 | |
4181 | if (const auto *ILA = New->getAttr<InternalLinkageAttr>()) |
4182 | if (!Old->hasAttr<InternalLinkageAttr>()) { |
4183 | Diag(New->getLocation(), diag::err_attribute_missing_on_first_decl) |
4184 | << ILA; |
4185 | Diag(Old->getLocation(), diag::note_previous_declaration); |
4186 | New->dropAttr<InternalLinkageAttr>(); |
4187 | } |
4188 | |
4189 | |
4190 | VarDecl *MostRecent = Old->getMostRecentDecl(); |
4191 | if (MostRecent != Old) { |
4192 | MergeVarDeclTypes(New, MostRecent, |
4193 | mergeTypeWithPrevious(*this, New, MostRecent, Previous)); |
4194 | if (New->isInvalidDecl()) |
4195 | return; |
4196 | } |
4197 | |
4198 | MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); |
4199 | if (New->isInvalidDecl()) |
4200 | return; |
4201 | |
4202 | diag::kind PrevDiag; |
4203 | SourceLocation OldLocation; |
4204 | std::tie(PrevDiag, OldLocation) = |
4205 | getNoteDiagForInvalidRedeclaration(Old, New); |
4206 | |
4207 | |
4208 | if (New->getStorageClass() == SC_Static && |
4209 | !New->isStaticDataMember() && |
4210 | Old->hasExternalFormalLinkage()) { |
4211 | if (getLangOpts().MicrosoftExt) { |
4212 | Diag(New->getLocation(), diag::ext_static_non_static) |
4213 | << New->getDeclName(); |
4214 | Diag(OldLocation, PrevDiag); |
4215 | } else { |
4216 | Diag(New->getLocation(), diag::err_static_non_static) |
4217 | << New->getDeclName(); |
4218 | Diag(OldLocation, PrevDiag); |
4219 | return New->setInvalidDecl(); |
4220 | } |
4221 | } |
4222 | |
4223 | |
4224 | |
4225 | |
4226 | |
4227 | |
4228 | |
4229 | |
4230 | |
4231 | if (New->hasExternalStorage() && Old->hasLinkage()) |
4232 | ; |
4233 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
4234 | !New->isStaticDataMember() && |
4235 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
4236 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
4237 | Diag(OldLocation, PrevDiag); |
4238 | return New->setInvalidDecl(); |
4239 | } |
4240 | |
4241 | |
4242 | if (New->hasExternalStorage() && |
4243 | !Old->hasLinkage() && Old->isLocalVarDeclOrParm()) { |
4244 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); |
4245 | Diag(OldLocation, PrevDiag); |
4246 | return New->setInvalidDecl(); |
4247 | } |
4248 | if (Old->hasLinkage() && New->isLocalVarDeclOrParm() && |
4249 | !New->hasExternalStorage()) { |
4250 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
4251 | Diag(OldLocation, PrevDiag); |
4252 | return New->setInvalidDecl(); |
4253 | } |
4254 | |
4255 | if (CheckRedeclarationModuleOwnership(New, Old)) |
4256 | return; |
4257 | |
4258 | |
4259 | |
4260 | |
4261 | |
4262 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && |
4263 | |
4264 | !(Old->getLexicalDeclContext()->isRecord() && |
4265 | !New->getLexicalDeclContext()->isRecord())) { |
4266 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
4267 | Diag(OldLocation, PrevDiag); |
4268 | return New->setInvalidDecl(); |
4269 | } |
4270 | |
4271 | if (New->isInline() && !Old->getMostRecentDecl()->isInline()) { |
4272 | if (VarDecl *Def = Old->getDefinition()) { |
4273 | |
4274 | |
4275 | |
4276 | Diag(New->getLocation(), diag::err_inline_decl_follows_def) << New; |
4277 | Diag(Def->getLocation(), diag::note_previous_definition); |
4278 | } |
4279 | } |
4280 | |
4281 | |
4282 | |
4283 | if (!Old->isInline() && New->isInline() && Old->isUsed(false) && |
4284 | !Old->getDefinition() && !New->isThisDeclarationADefinition()) |
4285 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), |
4286 | SourceLocation())); |
4287 | |
4288 | if (New->getTLSKind() != Old->getTLSKind()) { |
4289 | if (!Old->getTLSKind()) { |
4290 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); |
4291 | Diag(OldLocation, PrevDiag); |
4292 | } else if (!New->getTLSKind()) { |
4293 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); |
4294 | Diag(OldLocation, PrevDiag); |
4295 | } else { |
4296 | |
4297 | |
4298 | |
4299 | |
4300 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) |
4301 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); |
4302 | Diag(OldLocation, PrevDiag); |
4303 | } |
4304 | } |
4305 | |
4306 | |
4307 | if (getLangOpts().CPlusPlus && |
4308 | New->isThisDeclarationADefinition() == VarDecl::Definition) { |
4309 | if (Old->isStaticDataMember() && Old->getCanonicalDecl()->isInline() && |
4310 | Old->getCanonicalDecl()->isConstexpr()) { |
4311 | |
4312 | Diag(New->getLocation(), |
4313 | diag::warn_deprecated_redundant_constexpr_static_def); |
4314 | } else if (VarDecl *Def = Old->getDefinition()) { |
4315 | if (checkVarDeclRedefinition(Def, New)) |
4316 | return; |
4317 | } |
4318 | } |
4319 | |
4320 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
4321 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
4322 | Diag(OldLocation, PrevDiag); |
4323 | New->setInvalidDecl(); |
4324 | return; |
4325 | } |
4326 | |
4327 | |
4328 | if (Old->getMostRecentDecl()->isUsed(false)) |
4329 | New->setIsUsed(); |
4330 | |
4331 | |
4332 | New->setPreviousDecl(Old); |
4333 | if (NewTemplate) |
4334 | NewTemplate->setPreviousDecl(OldTemplate); |
4335 | |
4336 | |
4337 | New->setAccess(Old->getAccess()); |
4338 | if (NewTemplate) |
4339 | NewTemplate->setAccess(New->getAccess()); |
4340 | |
4341 | if (Old->isInline()) |
4342 | New->setImplicitlyInline(); |
4343 | } |
4344 | |
4345 | void Sema::notePreviousDefinition(const NamedDecl *Old, SourceLocation New) { |
4346 | SourceManager &SrcMgr = getSourceManager(); |
4347 | auto FNewDecLoc = SrcMgr.getDecomposedLoc(New); |
4348 | auto FOldDecLoc = SrcMgr.getDecomposedLoc(Old->getLocation()); |
4349 | auto *FNew = SrcMgr.getFileEntryForID(FNewDecLoc.first); |
4350 | auto *FOld = SrcMgr.getFileEntryForID(FOldDecLoc.first); |
4351 | auto &HSI = PP.getHeaderSearchInfo(); |
4352 | StringRef HdrFilename = |
4353 | SrcMgr.getFilename(SrcMgr.getSpellingLoc(Old->getLocation())); |
4354 | |
4355 | auto noteFromModuleOrInclude = [&](Module *Mod, |
4356 | SourceLocation IncLoc) -> bool { |
4357 | |
4358 | |
4359 | |
4360 | |
4361 | if (IncLoc.isValid()) { |
4362 | if (Mod) { |
4363 | Diag(IncLoc, diag::note_redefinition_modules_same_file) |
4364 | << HdrFilename.str() << Mod->getFullModuleName(); |
4365 | if (!Mod->DefinitionLoc.isInvalid()) |
4366 | Diag(Mod->DefinitionLoc, diag::note_defined_here) |
4367 | << Mod->getFullModuleName(); |
4368 | } else { |
4369 | Diag(IncLoc, diag::note_redefinition_include_same_file) |
4370 | << HdrFilename.str(); |
4371 | } |
4372 | return true; |
4373 | } |
4374 | |
4375 | return false; |
4376 | }; |
4377 | |
4378 | |
4379 | |
4380 | if (FNew == FOld && FNewDecLoc.second == FOldDecLoc.second) { |
4381 | SourceLocation OldIncLoc = SrcMgr.getIncludeLoc(FOldDecLoc.first); |
4382 | SourceLocation NewIncLoc = SrcMgr.getIncludeLoc(FNewDecLoc.first); |
4383 | bool EmittedDiag = |
4384 | noteFromModuleOrInclude(Old->getOwningModule(), OldIncLoc); |
4385 | EmittedDiag |= noteFromModuleOrInclude(getCurrentModule(), NewIncLoc); |
4386 | |
4387 | |
4388 | if (FOld && !HSI.isFileMultipleIncludeGuarded(FOld)) |
4389 | Diag(Old->getLocation(), diag::note_use_ifdef_guards); |
4390 | |
4391 | if (EmittedDiag) |
4392 | return; |
4393 | } |
4394 | |
4395 | |
4396 | if (Old->getLocation().isValid()) |
4397 | Diag(Old->getLocation(), diag::note_previous_definition); |
4398 | } |
4399 | |
4400 | |
4401 | |
4402 | bool Sema::checkVarDeclRedefinition(VarDecl *Old, VarDecl *New) { |
4403 | if (!hasVisibleDefinition(Old) && |
4404 | (New->getFormalLinkage() == InternalLinkage || |
4405 | New->isInline() || |
4406 | New->getDescribedVarTemplate() || |
4407 | New->getNumTemplateParameterLists() || |
4408 | New->getDeclContext()->isDependentContext())) { |
4409 | |
4410 | |
4411 | New->demoteThisDefinitionToDeclaration(); |
4412 | |
4413 | |
4414 | if (auto *OldTD = Old->getDescribedVarTemplate()) |
4415 | makeMergedDefinitionVisible(OldTD); |
4416 | makeMergedDefinitionVisible(Old); |
4417 | return false; |
4418 | } else { |
4419 | Diag(New->getLocation(), diag::err_redefinition) << New; |
4420 | notePreviousDefinition(Old, New->getLocation()); |
4421 | New->setInvalidDecl(); |
4422 | return true; |
4423 | } |
4424 | } |
4425 | |
4426 | |
4427 | |
4428 | Decl * |
4429 | Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, DeclSpec &DS, |
4430 | RecordDecl *&AnonRecord) { |
4431 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg(), false, |
4432 | AnonRecord); |
4433 | } |
4434 | |
4435 | |
4436 | |
4437 | |
4438 | |
4439 | |
4440 | |
4441 | static unsigned getMSManglingNumber(const LangOptions &LO, Scope *S) { |
4442 | return LO.isCompatibleWithMSVC(LangOptions::MSVC2015) |
4443 | ? S->getMSCurManglingNumber() |
4444 | : S->getMSLastManglingNumber(); |
4445 | } |
4446 | |
4447 | void Sema::handleTagNumbering(const TagDecl *Tag, Scope *TagScope) { |
4448 | if (!Context.getLangOpts().CPlusPlus) |
4449 | return; |
4450 | |
4451 | if (isa<CXXRecordDecl>(Tag->getParent())) { |
4452 | |
4453 | |
4454 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) |
4455 | return; |
4456 | MangleNumberingContext &MCtx = |
4457 | Context.getManglingNumberContext(Tag->getParent()); |
4458 | Context.setManglingNumber( |
4459 | Tag, MCtx.getManglingNumber( |
4460 | Tag, getMSManglingNumber(getLangOpts(), TagScope))); |
4461 | return; |
4462 | } |
4463 | |
4464 | |
4465 | MangleNumberingContext *MCtx; |
4466 | Decl *ManglingContextDecl; |
4467 | std::tie(MCtx, ManglingContextDecl) = |
4468 | getCurrentMangleNumberContext(Tag->getDeclContext()); |
4469 | if (MCtx) { |
4470 | Context.setManglingNumber( |
4471 | Tag, MCtx->getManglingNumber( |
4472 | Tag, getMSManglingNumber(getLangOpts(), TagScope))); |
4473 | } |
4474 | } |
4475 | |
4476 | namespace { |
4477 | struct NonCLikeKind { |
4478 | enum { |
4479 | None, |
4480 | BaseClass, |
4481 | DefaultMemberInit, |
4482 | Lambda, |
4483 | Friend, |
4484 | OtherMember, |
4485 | Invalid, |
4486 | } Kind = None; |
4487 | SourceRange Range; |
4488 | |
4489 | explicit operator bool() { return Kind != None; } |
4490 | }; |
4491 | } |
4492 | |
4493 | |
4494 | |
4495 | static NonCLikeKind getNonCLikeKindForAnonymousStruct(const CXXRecordDecl *RD) { |
4496 | if (RD->isInvalidDecl()) |
4497 | return {NonCLikeKind::Invalid, {}}; |
4498 | |
4499 | |
4500 | |
4501 | |
4502 | |
4503 | if (RD->getNumBases()) |
4504 | return {NonCLikeKind::BaseClass, |
4505 | SourceRange(RD->bases_begin()->getBeginLoc(), |
4506 | RD->bases_end()[-1].getEndLoc())}; |
4507 | bool Invalid = false; |
4508 | for (Decl *D : RD->decls()) { |
4509 | |
4510 | if (D->isInvalidDecl()) { |
4511 | Invalid = true; |
4512 | continue; |
4513 | } |
4514 | |
4515 | |
4516 | if (auto *FD = dyn_cast<FieldDecl>(D)) { |
4517 | if (FD->hasInClassInitializer()) { |
4518 | auto *Init = FD->getInClassInitializer(); |
4519 | return {NonCLikeKind::DefaultMemberInit, |
4520 | Init ? Init->getSourceRange() : D->getSourceRange()}; |
4521 | } |
4522 | continue; |
4523 | } |
4524 | |
4525 | |
4526 | |
4527 | if (isa<FriendDecl>(D)) |
4528 | return {NonCLikeKind::Friend, D->getSourceRange()}; |
4529 | |
4530 | |
4531 | |
4532 | if (isa<StaticAssertDecl>(D) || isa<IndirectFieldDecl>(D) || |
4533 | isa<EnumDecl>(D)) |
4534 | continue; |
4535 | auto *MemberRD = dyn_cast<CXXRecordDecl>(D); |
4536 | if (!MemberRD) { |
4537 | if (D->isImplicit()) |
4538 | continue; |
4539 | return {NonCLikeKind::OtherMember, D->getSourceRange()}; |
4540 | } |
4541 | |
4542 | |
4543 | if (MemberRD->isLambda()) |
4544 | return {NonCLikeKind::Lambda, MemberRD->getSourceRange()}; |
4545 | |
4546 | |
4547 | |
4548 | if (MemberRD->isThisDeclarationADefinition()) { |
4549 | if (auto Kind = getNonCLikeKindForAnonymousStruct(MemberRD)) |
4550 | return Kind; |
4551 | } |
4552 | } |
4553 | |
4554 | return {Invalid ? NonCLikeKind::Invalid : NonCLikeKind::None, {}}; |
4555 | } |
4556 | |
4557 | void Sema::setTagNameForLinkagePurposes(TagDecl *TagFromDeclSpec, |
4558 | TypedefNameDecl *NewTD) { |
4559 | if (TagFromDeclSpec->isInvalidDecl()) |
4560 | return; |
4561 | |
4562 | |
4563 | if (TagFromDeclSpec->hasNameForLinkage()) |
4564 | return; |
4565 | |
4566 | |
4567 | assert(TagFromDeclSpec->isThisDeclarationADefinition()); |
4568 | |
4569 | |
4570 | if (!Context.hasSameType(NewTD->getUnderlyingType(), |
4571 | Context.getTagDeclType(TagFromDeclSpec))) { |
4572 | if (getLangOpts().CPlusPlus) |
4573 | Context.addTypedefNameForUnnamedTagDecl(TagFromDeclSpec, NewTD); |
4574 | return; |
4575 | } |
4576 | |
4577 | |
4578 | |
4579 | |
4580 | |
4581 | |
4582 | |
4583 | |
4584 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TagFromDeclSpec); |
4585 | NonCLikeKind NonCLike = RD ? getNonCLikeKindForAnonymousStruct(RD) |
4586 | : NonCLikeKind(); |
4587 | bool ChangesLinkage = TagFromDeclSpec->hasLinkageBeenComputed(); |
4588 | if (NonCLike || ChangesLinkage) { |
4589 | if (NonCLike.Kind == NonCLikeKind::Invalid) |
4590 | return; |
4591 | |
4592 | unsigned DiagID = diag::ext_non_c_like_anon_struct_in_typedef; |
4593 | if (ChangesLinkage) { |
4594 | |
4595 | if (NonCLike.Kind == NonCLikeKind::None) |
4596 | DiagID = diag::err_typedef_changes_linkage; |
4597 | else |
4598 | DiagID = diag::err_non_c_like_anon_struct_in_typedef; |
4599 | } |
4600 | |
4601 | SourceLocation FixitLoc = |
4602 | getLocForEndOfToken(TagFromDeclSpec->getInnerLocStart()); |
4603 | llvm::SmallString<40> TextToInsert; |
4604 | TextToInsert += ' '; |
4605 | TextToInsert += NewTD->getIdentifier()->getName(); |
4606 | |
4607 | Diag(FixitLoc, DiagID) |
4608 | << isa<TypeAliasDecl>(NewTD) |
4609 | << FixItHint::CreateInsertion(FixitLoc, TextToInsert); |
4610 | if (NonCLike.Kind != NonCLikeKind::None) { |
4611 | Diag(NonCLike.Range.getBegin(), diag::note_non_c_like_anon_struct) |
4612 | << NonCLike.Kind - 1 << NonCLike.Range; |
4613 | } |
4614 | Diag(NewTD->getLocation(), diag::note_typedef_for_linkage_here) |
4615 | << NewTD << isa<TypeAliasDecl>(NewTD); |
4616 | |
4617 | if (ChangesLinkage) |
4618 | return; |
4619 | } |
4620 | |
4621 | |
4622 | TagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
4623 | } |
4624 | |
4625 | static unsigned GetDiagnosticTypeSpecifierID(DeclSpec::TST T) { |
4626 | switch (T) { |
4627 | case DeclSpec::TST_class: |
4628 | return 0; |
4629 | case DeclSpec::TST_struct: |
4630 | return 1; |
4631 | case DeclSpec::TST_interface: |
4632 | return 2; |
4633 | case DeclSpec::TST_union: |
4634 | return 3; |
4635 | case DeclSpec::TST_enum: |
4636 | return 4; |
4637 | default: |
4638 | llvm_unreachable("unexpected type specifier"); |
4639 | } |
4640 | } |
4641 | |
4642 | |
4643 | |
4644 | |
4645 | Decl * |
4646 | Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, DeclSpec &DS, |
4647 | MultiTemplateParamsArg TemplateParams, |
4648 | bool IsExplicitInstantiation, |
4649 | RecordDecl *&AnonRecord) { |
4650 | Decl *TagD = nullptr; |
4651 | TagDecl *Tag = nullptr; |
4652 | if (DS.getTypeSpecType() == DeclSpec::TST_class || |
4653 | DS.getTypeSpecType() == DeclSpec::TST_struct || |
4654 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
4655 | DS.getTypeSpecType() == DeclSpec::TST_union || |
4656 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
4657 | TagD = DS.getRepAsDecl(); |
4658 | |
4659 | if (!TagD) |
4660 | return nullptr; |
4661 | |
4662 | |
4663 | |
4664 | |
4665 | if (isa<TagDecl>(TagD)) |
4666 | Tag = cast<TagDecl>(TagD); |
4667 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) |
4668 | Tag = CTD->getTemplatedDecl(); |
4669 | } |
4670 | |
4671 | if (Tag) { |
4672 | handleTagNumbering(Tag, S); |
4673 | Tag->setFreeStanding(); |
4674 | if (Tag->isInvalidDecl()) |
4675 | return Tag; |
4676 | } |
4677 | |
4678 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
4679 | |
4680 | |
4681 | if (TypeQuals & DeclSpec::TQ_restrict) |
4682 | Diag(DS.getRestrictSpecLoc(), |
4683 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) |
4684 | << DS.getSourceRange(); |
4685 | } |
4686 | |
4687 | if (DS.isInlineSpecified()) |
4688 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
4689 | << getLangOpts().CPlusPlus17; |
4690 | |
4691 | if (DS.hasConstexprSpecifier()) { |
4692 | |
4693 | |
4694 | |
4695 | |
4696 | if (Tag) |
4697 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) |
4698 | << GetDiagnosticTypeSpecifierID(DS.getTypeSpecType()) |
4699 | << static_cast<int>(DS.getConstexprSpecifier()); |
4700 | else |
4701 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_wrong_decl_kind) |
4702 | << static_cast<int>(DS.getConstexprSpecifier()); |
4703 | |
4704 | return TagD; |
4705 | } |
4706 | |
4707 | DiagnoseFunctionSpecifiers(DS); |
4708 | |
4709 | if (DS.isFriendSpecified()) { |
4710 | |
4711 | |
4712 | if (TagD && !Tag) |
4713 | return nullptr; |
4714 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
4715 | } |
4716 | |
4717 | const CXXScopeSpec &SS = DS.getTypeSpecScope(); |
4718 | bool IsExplicitSpecialization = |
4719 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; |
4720 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && |
4721 | !IsExplicitInstantiation && !IsExplicitSpecialization && |
4722 | !isa<ClassTemplatePartialSpecializationDecl>(Tag)) { |
4723 | |
4724 | |
4725 | |
4726 | |
4727 | |
4728 | |
4729 | |
4730 | |
4731 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) |
4732 | << GetDiagnosticTypeSpecifierID(DS.getTypeSpecType()) << SS.getRange(); |
4733 | return nullptr; |
4734 | } |
4735 | |
4736 | |
4737 | bool DeclaresAnything = true; |
4738 | |
4739 | |
4740 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
4741 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
4742 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
4743 | if (getLangOpts().CPlusPlus || |
4744 | Record->getDeclContext()->isRecord()) { |
4745 | |
4746 | |
4747 | |
4748 | |
4749 | |
4750 | |
4751 | |
4752 | if (CurContext->isFunctionOrMethod()) |
4753 | AnonRecord = Record; |
4754 | return BuildAnonymousStructOrUnion(S, DS, AS, Record, |
4755 | Context.getPrintingPolicy()); |
4756 | } |
4757 | |
4758 | DeclaresAnything = false; |
4759 | } |
4760 | } |
4761 | |
4762 | |
4763 | |
4764 | |
4765 | |
4766 | |
4767 | |
4768 | if (!getLangOpts().CPlusPlus && CurContext->isRecord() && |
4769 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { |
4770 | |
4771 | |
4772 | |
4773 | |
4774 | |
4775 | |
4776 | |
4777 | if ((Tag && Tag->getDeclName()) || |
4778 | DS.getTypeSpecType() == DeclSpec::TST_typename) { |
4779 | RecordDecl *Record = nullptr; |
4780 | if (Tag) |
4781 | Record = dyn_cast<RecordDecl>(Tag); |
4782 | else if (const RecordType *RT = |
4783 | DS.getRepAsType().get()->getAsStructureType()) |
4784 | Record = RT->getDecl(); |
4785 | else if (const RecordType *UT = DS.getRepAsType().get()->getAsUnionType()) |
4786 | Record = UT->getDecl(); |
4787 | |
4788 | if (Record && getLangOpts().MicrosoftExt) { |
4789 | Diag(DS.getBeginLoc(), diag::ext_ms_anonymous_record) |
4790 | << Record->isUnion() << DS.getSourceRange(); |
4791 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); |
4792 | } |
4793 | |
4794 | DeclaresAnything = false; |
4795 | } |
4796 | } |
4797 | |
4798 | |
4799 | if (DS.getTypeSpecType() == DeclSpec::TST_error || |
4800 | (TagD && TagD->isInvalidDecl())) |
4801 | return TagD; |
4802 | |
4803 | if (getLangOpts().CPlusPlus && |
4804 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
4805 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) |
4806 | if (Enum->enumerator_begin() == Enum->enumerator_end() && |
4807 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
4808 | DeclaresAnything = false; |
4809 | |
4810 | if (!DS.isMissingDeclaratorOk()) { |
4811 | |
4812 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) |
4813 | Diag(DS.getBeginLoc(), diag::ext_typedef_without_a_name) |
4814 | << DS.getSourceRange(); |
4815 | else |
4816 | DeclaresAnything = false; |
4817 | } |
4818 | |
4819 | if (DS.isModulePrivateSpecified() && |
4820 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
4821 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) |
4822 | << Tag->getTagKind() |
4823 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); |
4824 | |
4825 | ActOnDocumentableDecl(TagD); |
4826 | |
4827 | |
4828 | |
4829 | |
4830 | |
4831 | |
4832 | |
4833 | |
4834 | |
4835 | if (!DeclaresAnything) { |
4836 | |
4837 | |
4838 | Diag(DS.getBeginLoc(), (IsExplicitInstantiation || !TemplateParams.empty()) |
4839 | ? diag::err_no_declarators |
4840 | : diag::ext_no_declarators) |
4841 | << DS.getSourceRange(); |
4842 | return TagD; |
4843 | } |
4844 | |
4845 | |
4846 | |
4847 | |
4848 | |
4849 | |
4850 | |
4851 | |
4852 | |
4853 | unsigned DiagID = diag::warn_standalone_specifier; |
4854 | if (getLangOpts().CPlusPlus) |
4855 | DiagID = diag::ext_standalone_specifier; |
4856 | |
4857 | |
4858 | |
4859 | |
4860 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
4861 | if (SCS == DeclSpec::SCS_mutable) |
4862 | |
4863 | |
4864 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_nonmember); |
4865 | else if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) |
4866 | Diag(DS.getStorageClassSpecLoc(), DiagID) |
4867 | << DeclSpec::getSpecifierName(SCS); |
4868 | } |
4869 | |
4870 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
4871 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) |
4872 | << DeclSpec::getSpecifierName(TSCS); |
4873 | if (DS.getTypeQualifiers()) { |
4874 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
4875 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; |
4876 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
4877 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; |
4878 | |
4879 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
4880 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; |
4881 | if (DS.getTypeQualifiers() & DeclSpec::TQ_unaligned) |
4882 | Diag(DS.getUnalignedSpecLoc(), DiagID) << "__unaligned"; |
4883 | } |
4884 | |
4885 | |
4886 | |
4887 | |
4888 | if (!DS.getAttributes().empty()) { |
4889 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); |
4890 | if (TypeSpecType == DeclSpec::TST_class || |
4891 | TypeSpecType == DeclSpec::TST_struct || |
4892 | TypeSpecType == DeclSpec::TST_interface || |
4893 | TypeSpecType == DeclSpec::TST_union || |
4894 | TypeSpecType == DeclSpec::TST_enum) { |
4895 | for (const ParsedAttr &AL : DS.getAttributes()) |
4896 | Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored) |
4897 | << AL << GetDiagnosticTypeSpecifierID(TypeSpecType); |
4898 | } |
4899 | } |
4900 | |
4901 | return TagD; |
4902 | } |
4903 | |
4904 | |
4905 | |
4906 | |
4907 | |
4908 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
4909 | Scope *S, |
4910 | DeclContext *Owner, |
4911 | DeclarationName Name, |
4912 | SourceLocation NameLoc, |
4913 | bool IsUnion) { |
4914 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, |
4915 | Sema::ForVisibleRedeclaration); |
4916 | if (!SemaRef.LookupName(R, S)) return false; |
4917 | |
4918 | |
4919 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
4920 | assert(PrevDecl && "Expected a non-null Decl"); |
4921 | |
4922 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) |
4923 | return false; |
4924 | |
4925 | SemaRef.Diag(NameLoc, diag::err_anonymous_record_member_redecl) |
4926 | << IsUnion << Name; |
4927 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
4928 | |
4929 | return true; |
4930 | } |
4931 | |
4932 | |
4933 | |
4934 | |
4935 | |
4936 | |
4937 | |
4938 | |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | |
4946 | |
4947 | |
4948 | static bool |
4949 | InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, DeclContext *Owner, |
4950 | RecordDecl *AnonRecord, AccessSpecifier AS, |
4951 | SmallVectorImpl<NamedDecl *> &Chaining) { |
4952 | bool Invalid = false; |
4953 | |
4954 | |
4955 | for (auto *D : AnonRecord->decls()) { |
4956 | if ((isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) && |
4957 | cast<NamedDecl>(D)->getDeclName()) { |
4958 | ValueDecl *VD = cast<ValueDecl>(D); |
4959 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), |
4960 | VD->getLocation(), |
4961 | AnonRecord->isUnion())) { |
4962 | |
4963 | |
4964 | |
4965 | |
4966 | Invalid = true; |
4967 | } else { |
4968 | |
4969 | |
4970 | |
4971 | |
4972 | |
4973 | unsigned OldChainingSize = Chaining.size(); |
4974 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) |
4975 | Chaining.append(IF->chain_begin(), IF->chain_end()); |
4976 | else |
4977 | Chaining.push_back(VD); |
4978 | |
4979 | assert(Chaining.size() >= 2); |
4980 | NamedDecl **NamedChain = |
4981 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; |
4982 | for (unsigned i = 0; i < Chaining.size(); i++) |
4983 | NamedChain[i] = Chaining[i]; |
4984 | |
4985 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
4986 | SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(), |
4987 | VD->getType(), {NamedChain, Chaining.size()}); |
4988 | |
4989 | for (const auto *Attr : VD->attrs()) |
4990 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
4991 | |
4992 | IndirectField->setAccess(AS); |
4993 | IndirectField->setImplicit(); |
4994 | SemaRef.PushOnScopeChains(IndirectField, S); |
4995 | |
4996 | |
4997 | if (AS != AS_none) IndirectField->setAccess(AS); |
4998 | |
4999 | Chaining.resize(OldChainingSize); |
5000 | } |
5001 | } |
5002 | } |
5003 | |
5004 | return Invalid; |
5005 | } |
5006 | |
5007 | |
5008 | |
5009 | |
5010 | static StorageClass |
5011 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
5012 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); |
5013 | assert(StorageClassSpec != DeclSpec::SCS_typedef && |
5014 | "Parser allowed 'typedef' as storage class VarDecl."); |
5015 | switch (StorageClassSpec) { |
5016 | case DeclSpec::SCS_unspecified: return SC_None; |
5017 | case DeclSpec::SCS_extern: |
5018 | if (DS.isExternInLinkageSpec()) |
5019 | return SC_None; |
5020 | return SC_Extern; |
5021 | case DeclSpec::SCS_static: return SC_Static; |
5022 | case DeclSpec::SCS_auto: return SC_Auto; |
5023 | case DeclSpec::SCS_register: return SC_Register; |
5024 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
5025 | |
5026 | case DeclSpec::SCS_mutable: |
5027 | case DeclSpec::SCS_typedef: return SC_None; |
5028 | } |
5029 | llvm_unreachable("unknown storage class specifier"); |
5030 | } |
5031 | |
5032 | static SourceLocation findDefaultInitializer(const CXXRecordDecl *Record) { |
5033 | assert(Record->hasInClassInitializer()); |
5034 | |
5035 | for (const auto *I : Record->decls()) { |
5036 | const auto *FD = dyn_cast<FieldDecl>(I); |
5037 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
5038 | FD = IFD->getAnonField(); |
5039 | if (FD && FD->hasInClassInitializer()) |
5040 | return FD->getLocation(); |
5041 | } |
5042 | |
5043 | llvm_unreachable("couldn't find in-class initializer"); |
5044 | } |
5045 | |
5046 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
5047 | SourceLocation DefaultInitLoc) { |
5048 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
5049 | return; |
5050 | |
5051 | S.Diag(DefaultInitLoc, diag::err_multiple_mem_union_initialization); |
5052 | S.Diag(findDefaultInitializer(Parent), diag::note_previous_initializer) << 0; |
5053 | } |
5054 | |
5055 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
5056 | CXXRecordDecl *AnonUnion) { |
5057 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
5058 | return; |
5059 | |
5060 | checkDuplicateDefaultInit(S, Parent, findDefaultInitializer(AnonUnion)); |
5061 | } |
5062 | |
5063 | |
5064 | |
5065 | |
5066 | |
5067 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
5068 | AccessSpecifier AS, |
5069 | RecordDecl *Record, |
5070 | const PrintingPolicy &Policy) { |
5071 | DeclContext *Owner = Record->getDeclContext(); |
5072 | |
5073 | |
5074 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
5075 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
5076 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
5077 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
5078 | else if (!Record->isUnion() && !getLangOpts().C11) |
5079 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
5080 | |
5081 | |
5082 | |
5083 | bool Invalid = false; |
5084 | if (getLangOpts().CPlusPlus) { |
5085 | const char *PrevSpec = nullptr; |
5086 | if (Record->isUnion()) { |
5087 | |
5088 | |
5089 | |
5090 | |
5091 | unsigned DiagID; |
5092 | DeclContext *OwnerScope = Owner->getRedeclContext(); |
5093 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && |
5094 | (OwnerScope->isTranslationUnit() || |
5095 | (OwnerScope->isNamespace() && |
5096 | !cast<NamespaceDecl>(OwnerScope)->isAnonymousNamespace()))) { |
5097 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
5098 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); |
5099 | |
5100 | |
5101 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), |
5102 | PrevSpec, DiagID, Policy); |
5103 | } |
5104 | |
5105 | |
5106 | |
5107 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
5108 | isa<RecordDecl>(Owner)) { |
5109 | Diag(DS.getStorageClassSpecLoc(), |
5110 | diag::err_anonymous_union_with_storage_spec) |
5111 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
5112 | |
5113 | |
5114 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
5115 | SourceLocation(), |
5116 | PrevSpec, DiagID, Context.getPrintingPolicy()); |
5117 | } |
5118 | } |
5119 | |
5120 | |
5121 | if (DS.getTypeQualifiers()) { |
5122 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
5123 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) |
5124 | << Record->isUnion() << "const" |
5125 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
5126 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
5127 | Diag(DS.getVolatileSpecLoc(), |
5128 | diag::ext_anonymous_struct_union_qualified) |
5129 | << Record->isUnion() << "volatile" |
5130 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
5131 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
5132 | Diag(DS.getRestrictSpecLoc(), |
5133 | diag::ext_anonymous_struct_union_qualified) |
5134 | << Record->isUnion() << "restrict" |
5135 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
5136 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
5137 | Diag(DS.getAtomicSpecLoc(), |
5138 | diag::ext_anonymous_struct_union_qualified) |
5139 | << Record->isUnion() << "_Atomic" |
5140 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); |
5141 | if (DS.getTypeQualifiers() & DeclSpec::TQ_unaligned) |
5142 | Diag(DS.getUnalignedSpecLoc(), |
5143 | diag::ext_anonymous_struct_union_qualified) |
5144 | << Record->isUnion() << "__unaligned" |
5145 | << FixItHint::CreateRemoval(DS.getUnalignedSpecLoc()); |
5146 | |
5147 | DS.ClearTypeQualifiers(); |
5148 | } |
5149 | |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | for (auto *Mem : Record->decls()) { |
5155 | |
5156 | if (Mem->isInvalidDecl()) |
5157 | continue; |
5158 | |
5159 | if (auto *FD = dyn_cast<FieldDecl>(Mem)) { |
5160 | |
5161 | |
5162 | |
5163 | assert(FD->getAccess() != AS_none); |
5164 | if (FD->getAccess() != AS_public) { |
5165 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
5166 | << Record->isUnion() << (FD->getAccess() == AS_protected); |
5167 | Invalid = true; |
5168 | } |
5169 | |
5170 | |
5171 | |
5172 | |
5173 | |
5174 | |
5175 | if (CheckNontrivialField(FD)) |
5176 | Invalid = true; |
5177 | } else if (Mem->isImplicit()) { |
5178 | |
5179 | } else if (isa<TagDecl>(Mem) && Mem->getDeclContext() != Record) { |
5180 | |
5181 | |
5182 | |
5183 | |
5184 | } else if (auto *MemRecord = dyn_cast<RecordDecl>(Mem)) { |
5185 | if (!MemRecord->isAnonymousStructOrUnion() && |
5186 | MemRecord->getDeclName()) { |
5187 | |
5188 | if (getLangOpts().MicrosoftExt) |
5189 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
5190 | << Record->isUnion(); |
5191 | else { |
5192 | |
5193 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) |
5194 | << Record->isUnion(); |
5195 | Invalid = true; |
5196 | } |
5197 | } else { |
5198 | |
5199 | |
5200 | |
5201 | Diag(MemRecord->getLocation(), |
5202 | diag::ext_anonymous_record_with_anonymous_type) |
5203 | << Record->isUnion(); |
5204 | } |
5205 | } else if (isa<AccessSpecDecl>(Mem)) { |
5206 | |
5207 | } else if (isa<StaticAssertDecl>(Mem)) { |
5208 | |
5209 | } else { |
5210 | |
5211 | |
5212 | unsigned DK = diag::err_anonymous_record_bad_member; |
5213 | if (isa<TypeDecl>(Mem)) |
5214 | DK = diag::err_anonymous_record_with_type; |
5215 | else if (isa<FunctionDecl>(Mem)) |
5216 | DK = diag::err_anonymous_record_with_function; |
5217 | else if (isa<VarDecl>(Mem)) |
5218 | DK = diag::err_anonymous_record_with_static; |
5219 | |
5220 | |
5221 | if (getLangOpts().MicrosoftExt && |
5222 | DK == diag::err_anonymous_record_with_type) |
5223 | Diag(Mem->getLocation(), diag::ext_anonymous_record_with_type) |
5224 | << Record->isUnion(); |
5225 | else { |
5226 | Diag(Mem->getLocation(), DK) << Record->isUnion(); |
5227 | Invalid = true; |
5228 | } |
5229 | } |
5230 | } |
5231 | |
5232 | |
5233 | |
5234 | |
5235 | if (cast<CXXRecordDecl>(Record)->hasInClassInitializer() && |
5236 | Owner->isRecord()) |
5237 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Owner), |
5238 | cast<CXXRecordDecl>(Record)); |
5239 | } |
5240 | |
5241 | if (!Record->isUnion() && !Owner->isRecord()) { |
5242 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
5243 | << getLangOpts().CPlusPlus; |
5244 | Invalid = true; |
5245 | } |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | |
5253 | |
5254 | |
5255 | |
5256 | if (getLangOpts().CPlusPlus && Record->field_empty()) |
5257 | Diag(DS.getBeginLoc(), diag::ext_no_declarators) << DS.getSourceRange(); |
5258 | |
5259 | |
5260 | Declarator Dc(DS, DeclaratorContext::Member); |
5261 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
5262 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
5263 | |
5264 | |
5265 | NamedDecl *Anon = nullptr; |
5266 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
5267 | Anon = FieldDecl::Create( |
5268 | Context, OwningClass, DS.getBeginLoc(), Record->getLocation(), |
5269 | nullptr, Context.getTypeDeclType(Record), TInfo, |
5270 | nullptr, false, |
5271 | ICIS_NoInit); |
5272 | Anon->setAccess(AS); |
5273 | ProcessDeclAttributes(S, Anon, Dc); |
5274 | |
5275 | if (getLangOpts().CPlusPlus) |
5276 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
5277 | } else { |
5278 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
5279 | StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
5280 | if (SCSpec == DeclSpec::SCS_mutable) { |
5281 | |
5282 | |
5283 | Diag(Record->getLocation(), diag::err_mutable_nonmember); |
5284 | Invalid = true; |
5285 | SC = SC_None; |
5286 | } |
5287 | |
5288 | assert(DS.getAttributes().empty() && "No attribute expected"); |
5289 | Anon = VarDecl::Create(Context, Owner, DS.getBeginLoc(), |
5290 | Record->getLocation(), nullptr, |
5291 | Context.getTypeDeclType(Record), TInfo, SC); |
5292 | |
5293 | |
5294 | |
5295 | |
5296 | |
5297 | if (!Invalid) |
5298 | ActOnUninitializedDecl(Anon); |
5299 | } |
5300 | Anon->setImplicit(); |
5301 | |
5302 | |
5303 | Record->setAnonymousStructOrUnion(true); |
5304 | |
5305 | |
5306 | |
5307 | |
5308 | Owner->addDecl(Anon); |
5309 | |
5310 | |
5311 | |
5312 | |
5313 | SmallVector<NamedDecl*, 2> Chain; |
5314 | Chain.push_back(Anon); |
5315 | |
5316 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, Chain)) |
5317 | Invalid = true; |
5318 | |
5319 | if (VarDecl *NewVD = dyn_cast<VarDecl>(Anon)) { |
5320 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
5321 | MangleNumberingContext *MCtx; |
5322 | Decl *ManglingContextDecl; |
5323 | std::tie(MCtx, ManglingContextDecl) = |
5324 | getCurrentMangleNumberContext(NewVD->getDeclContext()); |
5325 | if (MCtx) { |
5326 | Context.setManglingNumber( |
5327 | NewVD, MCtx->getManglingNumber( |
5328 | NewVD, getMSManglingNumber(getLangOpts(), S))); |
5329 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
5330 | } |
5331 | } |
5332 | } |
5333 | |
5334 | if (Invalid) |
5335 | Anon->setInvalidDecl(); |
5336 | |
5337 | return Anon; |
5338 | } |
5339 | |
5340 | |
5341 | |
5342 | |
5343 | |
5344 | |
5345 | |
5346 | |
5347 | |
5348 | |
5349 | |
5350 | |
5351 | |
5352 | |
5353 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, |
5354 | RecordDecl *Record) { |
5355 | assert(Record && "expected a record!"); |
5356 | |
5357 | |
5358 | Declarator Dc(DS, DeclaratorContext::TypeName); |
5359 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
5360 | assert(TInfo && "couldn't build declarator info for anonymous struct"); |
5361 | |
5362 | auto *ParentDecl = cast<RecordDecl>(CurContext); |
5363 | QualType RecTy = Context.getTypeDeclType(Record); |
5364 | |
5365 | |
5366 | NamedDecl *Anon = |
5367 | FieldDecl::Create(Context, ParentDecl, DS.getBeginLoc(), DS.getBeginLoc(), |
5368 | nullptr, RecTy, TInfo, |
5369 | nullptr, false, |
5370 | ICIS_NoInit); |
5371 | Anon->setImplicit(); |
5372 | |
5373 | |
5374 | CurContext->addDecl(Anon); |
5375 | |
5376 | |
5377 | |
5378 | |
5379 | SmallVector<NamedDecl*, 2> Chain; |
5380 | Chain.push_back(Anon); |
5381 | |
5382 | RecordDecl *RecordDef = Record->getDefinition(); |
5383 | if (RequireCompleteSizedType(Anon->getLocation(), RecTy, |
5384 | diag::err_field_incomplete_or_sizeless) || |
5385 | InjectAnonymousStructOrUnionMembers(*this, S, CurContext, RecordDef, |
5386 | AS_none, Chain)) { |
5387 | Anon->setInvalidDecl(); |
5388 | ParentDecl->setInvalidDecl(); |
5389 | } |
5390 | |
5391 | return Anon; |
5392 | } |
5393 | |
5394 | |
5395 | |
5396 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
5397 | return GetNameFromUnqualifiedId(D.getName()); |
5398 | } |
5399 | |
5400 | |
5401 | DeclarationNameInfo |
5402 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { |
5403 | DeclarationNameInfo NameInfo; |
5404 | NameInfo.setLoc(Name.StartLocation); |
5405 | |
5406 | switch (Name.getKind()) { |
5407 | |
5408 | case UnqualifiedIdKind::IK_ImplicitSelfParam: |
5409 | case UnqualifiedIdKind::IK_Identifier: |
5410 | NameInfo.setName(Name.Identifier); |
5411 | return NameInfo; |
5412 | |
5413 | case UnqualifiedIdKind::IK_DeductionGuideName: { |
5414 | |
5415 | |
5416 | |
5417 | |
5418 | |
5419 | |
5420 | |
5421 | |
5422 | |
5423 | |
5424 | TemplateName TN = Name.TemplateName.get().get(); |
5425 | auto *Template = TN.getAsTemplateDecl(); |
5426 | if (!Template || !isa<ClassTemplateDecl>(Template)) { |
5427 | Diag(Name.StartLocation, |
5428 | diag::err_deduction_guide_name_not_class_template) |
5429 | << (int)getTemplateNameKindForDiagnostics(TN) << TN; |
5430 | if (Template) |
5431 | Diag(Template->getLocation(), diag::note_template_decl_here); |
5432 | return DeclarationNameInfo(); |
5433 | } |
5434 | |
5435 | NameInfo.setName( |
5436 | Context.DeclarationNames.getCXXDeductionGuideName(Template)); |
5437 | return NameInfo; |
5438 | } |
5439 | |
5440 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
5441 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( |
5442 | Name.OperatorFunctionId.Operator)); |
5443 | NameInfo.setCXXOperatorNameRange(SourceRange( |
5444 | Name.OperatorFunctionId.SymbolLocations[0], Name.EndLocation)); |
5445 | return NameInfo; |
5446 | |
5447 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
5448 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( |
5449 | Name.Identifier)); |
5450 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); |
5451 | return NameInfo; |
5452 | |
5453 | case UnqualifiedIdKind::IK_ConversionFunctionId: { |
5454 | TypeSourceInfo *TInfo; |
5455 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); |
5456 | if (Ty.isNull()) |
5457 | return DeclarationNameInfo(); |
5458 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( |
5459 | Context.getCanonicalType(Ty))); |
5460 | NameInfo.setNamedTypeInfo(TInfo); |
5461 | return NameInfo; |
5462 | } |
5463 | |
5464 | case UnqualifiedIdKind::IK_ConstructorName: { |
5465 | TypeSourceInfo *TInfo; |
5466 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); |
5467 | if (Ty.isNull()) |
5468 | return DeclarationNameInfo(); |
5469 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
5470 | Context.getCanonicalType(Ty))); |
5471 | NameInfo.setNamedTypeInfo(TInfo); |
5472 | return NameInfo; |
5473 | } |
5474 | |
5475 | case UnqualifiedIdKind::IK_ConstructorTemplateId: { |
5476 | |
5477 | |
5478 | |
5479 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); |
5480 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) |
5481 | return DeclarationNameInfo(); |
5482 | |
5483 | |
5484 | QualType CurClassType = Context.getTypeDeclType(CurClass); |
5485 | |
5486 | |
5487 | |
5488 | |
5489 | |
5490 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
5491 | Context.getCanonicalType(CurClassType))); |
5492 | |
5493 | NameInfo.setNamedTypeInfo(nullptr); |
5494 | return NameInfo; |
5495 | } |
5496 | |
5497 | case UnqualifiedIdKind::IK_DestructorName: { |
5498 | TypeSourceInfo *TInfo; |
5499 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); |
5500 | if (Ty.isNull()) |
5501 | return DeclarationNameInfo(); |
5502 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( |
5503 | Context.getCanonicalType(Ty))); |
5504 | NameInfo.setNamedTypeInfo(TInfo); |
5505 | return NameInfo; |
5506 | } |
5507 | |
5508 | case UnqualifiedIdKind::IK_TemplateId: { |
5509 | TemplateName TName = Name.TemplateId->Template.get(); |
5510 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
5511 | return Context.getNameForTemplate(TName, TNameLoc); |
5512 | } |
5513 | |
5514 | } |
5515 | |
5516 | llvm_unreachable("Unknown name kind"); |
5517 | } |
5518 | |
5519 | static QualType getCoreType(QualType Ty) { |
5520 | do { |
5521 | if (Ty->isPointerType() || Ty->isReferenceType()) |
5522 | Ty = Ty->getPointeeType(); |
5523 | else if (Ty->isArrayType()) |
5524 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); |
5525 | else |
5526 | return Ty.withoutLocalFastQualifiers(); |
5527 | } while (true); |
5528 | } |
5529 | |
5530 | |
5531 | |
5532 | |
5533 | |
5534 | |
5535 | |
5536 | |
5537 | static bool hasSimilarParameters(ASTContext &Context, |
5538 | FunctionDecl *Declaration, |
5539 | FunctionDecl *Definition, |
5540 | SmallVectorImpl<unsigned> &Params) { |
5541 | Params.clear(); |
5542 | if (Declaration->param_size() != Definition->param_size()) |
5543 | return false; |
5544 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
5545 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
5546 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
5547 | |
5548 | |
5549 | if (Context.hasSameUnqualifiedType(DefParamTy, DeclParamTy)) |
5550 | continue; |
5551 | |
5552 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); |
5553 | QualType DefParamBaseTy = getCoreType(DefParamTy); |
5554 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); |
5555 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); |
5556 | |
5557 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || |
5558 | (DeclTyName && DeclTyName == DefTyName)) |
5559 | Params.push_back(Idx); |
5560 | else |
5561 | return false; |
5562 | } |
5563 | |
5564 | return true; |
5565 | } |
5566 | |
5567 | |
5568 | |
5569 | |
5570 | |
5571 | |
5572 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, |
5573 | DeclarationName Name) { |
5574 | |
5575 | |
5576 | |
5577 | |
5578 | |
5579 | |
5580 | |
5581 | DeclSpec &DS = D.getMutableDeclSpec(); |
5582 | switch (DS.getTypeSpecType()) { |
5583 | case DeclSpec::TST_typename: |
5584 | case DeclSpec::TST_typeofType: |
5585 | case DeclSpec::TST_underlyingType: |
5586 | case DeclSpec::TST_atomic: { |
5587 | |
5588 | TypeSourceInfo *TSI = nullptr; |
5589 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
5590 | if (T.isNull() || !T->isInstantiationDependentType()) break; |
5591 | |
5592 | |
5593 | |
5594 | |
5595 | if (!TSI) |
5596 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); |
5597 | |
5598 | |
5599 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); |
5600 | if (!TSI) return true; |
5601 | |
5602 | |
5603 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
5604 | DS.UpdateTypeRep(LocType); |
5605 | break; |
5606 | } |
5607 | |
5608 | case DeclSpec::TST_decltype: |
5609 | case DeclSpec::TST_typeofExpr: { |
5610 | Expr *E = DS.getRepAsExpr(); |
5611 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
5612 | if (Result.isInvalid()) return true; |
5613 | DS.UpdateExprRep(Result.get()); |
5614 | break; |
5615 | } |
5616 | |
5617 | default: |
5618 | |
5619 | break; |
5620 | } |
5621 | |
5622 | |
5623 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { |
5624 | DeclaratorChunk &Chunk = D.getTypeObject(I); |
5625 | |
5626 | |
5627 | |
5628 | |
5629 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) |
5630 | continue; |
5631 | |
5632 | |
5633 | CXXScopeSpec &SS = Chunk.Mem.Scope(); |
5634 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) |
5635 | return true; |
5636 | } |
5637 | |
5638 | return false; |
5639 | } |
5640 | |
5641 | void Sema::warnOnReservedIdentifier(const NamedDecl *D) { |
5642 | |
5643 | |
5644 | if (D->getPreviousDecl() || D->isImplicit()) |
5645 | return; |
5646 | ReservedIdentifierStatus Status = D->isReserved(getLangOpts()); |
5647 | if (Status != ReservedIdentifierStatus::NotReserved && |
5648 | !Context.getSourceManager().isInSystemHeader(D->getLocation())) |
5649 | Diag(D->getLocation(), diag::warn_reserved_extern_symbol) |
5650 | << D << static_cast<int>(Status); |
5651 | } |
5652 | |
5653 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
5654 | D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); |
5655 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
5656 | |
5657 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && |
5658 | Dcl && Dcl->getDeclContext()->isFileContext()) |
5659 | Dcl->setTopLevelDeclInObjCContainer(); |
5660 | |
5661 | return Dcl; |
5662 | } |
5663 | |
5664 | |
5665 | |
5666 | |
5667 | |
5668 | |
5669 | |
5670 | |
5671 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, |
5672 | DeclarationNameInfo NameInfo) { |
5673 | DeclarationName Name = NameInfo.getName(); |
5674 | |
5675 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC); |
5676 | while (Record && Record->isAnonymousStructOrUnion()) |
5677 | Record = dyn_cast<CXXRecordDecl>(Record->getParent()); |
5678 | if (Record && Record->getIdentifier() && Record->getDeclName() == Name) { |
5679 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; |
5680 | return true; |
5681 | } |
5682 | |
5683 | return false; |
5684 | } |
5685 | |
5686 | |
5687 | |
5688 | |
5689 | |
5690 | |
5691 | |
5692 | |
5693 | |
5694 | |
5695 | |
5696 | |
5697 | |
5698 | |
5699 | |
5700 | |
5701 | |
5702 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
5703 | DeclarationName Name, |
5704 | SourceLocation Loc, bool IsTemplateId) { |
5705 | DeclContext *Cur = CurContext; |
5706 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
5707 | Cur = Cur->getParent(); |
5708 | |
5709 | |
5710 | |
5711 | |
5712 | |
5713 | |
5714 | |
5715 | |
5716 | |
5717 | |
5718 | if (Cur->Equals(DC)) { |
5719 | if (Cur->isRecord()) { |
5720 | Diag(Loc, LangOpts.MicrosoftExt ? diag::warn_member_extra_qualification |
5721 | : diag::err_member_extra_qualification) |
5722 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
5723 | SS.clear(); |
5724 | } else { |
5725 | Diag(Loc, diag::warn_namespace_member_extra_qualification) << Name; |
5726 | } |
5727 | return false; |
5728 | } |
5729 | |
5730 | |
5731 | |
5732 | |
5733 | if (!Cur->Encloses(DC) && !IsTemplateId) { |
5734 | if (Cur->isRecord()) |
5735 | Diag(Loc, diag::err_member_qualification) |
5736 | << Name << SS.getRange(); |
5737 | else if (isa<TranslationUnitDecl>(DC)) |
5738 | Diag(Loc, diag::err_invalid_declarator_global_scope) |
5739 | << Name << SS.getRange(); |
5740 | else if (isa<FunctionDecl>(Cur)) |
5741 | Diag(Loc, diag::err_invalid_declarator_in_function) |
5742 | << Name << SS.getRange(); |
5743 | else if (isa<BlockDecl>(Cur)) |
5744 | Diag(Loc, diag::err_invalid_declarator_in_block) |
5745 | << Name << SS.getRange(); |
5746 | else |
5747 | Diag(Loc, diag::err_invalid_declarator_scope) |
5748 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
5749 | |
5750 | return true; |
5751 | } |
5752 | |
5753 | if (Cur->isRecord()) { |
5754 | |
5755 | Diag(Loc, diag::err_member_qualification) |
5756 | << Name << SS.getRange(); |
5757 | SS.clear(); |
5758 | |
5759 | |
5760 | |
5761 | |
5762 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || |
5763 | Name.getNameKind() == DeclarationName::CXXDestructorName) && |
5764 | !Context.hasSameType(Name.getCXXNameType(), |
5765 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) |
5766 | return true; |
5767 | |
5768 | return false; |
5769 | } |
5770 | |
5771 | |
5772 | |
5773 | |
5774 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); |
5775 | while (SpecLoc.getPrefix()) |
5776 | SpecLoc = SpecLoc.getPrefix(); |
5777 | if (dyn_cast_or_null<DecltypeType>( |
5778 | SpecLoc.getNestedNameSpecifier()->getAsType())) |
5779 | Diag(Loc, diag::err_decltype_in_declarator) |
5780 | << SpecLoc.getTypeLoc().getSourceRange(); |
5781 | |
5782 | return false; |
5783 | } |
5784 | |
5785 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
5786 | MultiTemplateParamsArg TemplateParamLists) { |
5787 | |
5788 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
5789 | DeclarationName Name = NameInfo.getName(); |
5790 | |
5791 | |
5792 | |
5793 | if (D.isDecompositionDeclarator()) { |
5794 | return ActOnDecompositionDeclarator(S, D, TemplateParamLists); |
5795 | } else if (!Name) { |
5796 | if (!D.isInvalidType()) |
5797 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_declarator_need_ident) |
5798 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
5799 | return nullptr; |
5800 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
5801 | return nullptr; |
5802 | |
5803 | |
5804 | |
5805 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
5806 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
5807 | S = S->getParent(); |
5808 | |
5809 | DeclContext *DC = CurContext; |
5810 | if (D.getCXXScopeSpec().isInvalid()) |
5811 | D.setInvalidType(); |
5812 | else if (D.getCXXScopeSpec().isSet()) { |
5813 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
5814 | UPPC_DeclarationQualifier)) |
5815 | return nullptr; |
5816 | |
5817 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
5818 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); |
5819 | if (!DC || isa<EnumDecl>(DC)) { |
5820 | |
5821 | |
5822 | |
5823 | |
5824 | Diag(D.getIdentifierLoc(), |
5825 | diag::err_template_qualified_declarator_no_match) |
5826 | << D.getCXXScopeSpec().getScopeRep() |
5827 | << D.getCXXScopeSpec().getRange(); |
5828 | return nullptr; |
5829 | } |
5830 | bool IsDependentContext = DC->isDependentContext(); |
5831 | |
5832 | if (!IsDependentContext && |
5833 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
5834 | return nullptr; |
5835 | |
5836 | |
5837 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
5838 | Diag(D.getIdentifierLoc(), |
5839 | diag::err_member_def_undefined_record) |
5840 | << Name << DC << D.getCXXScopeSpec().getRange(); |
5841 | return nullptr; |
5842 | } |
5843 | if (!D.getDeclSpec().isFriendSpecified()) { |
5844 | if (diagnoseQualifiedDeclaration( |
5845 | D.getCXXScopeSpec(), DC, Name, D.getIdentifierLoc(), |
5846 | D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId)) { |
5847 | if (DC->isRecord()) |
5848 | return nullptr; |
5849 | |
5850 | D.setInvalidType(); |
5851 | } |
5852 | } |
5853 | |
5854 | |
5855 | |
5856 | if (EnteringContext && IsDependentContext && |
5857 | TemplateParamLists.size() != 0) { |
5858 | ContextRAII SavedContext(*this, DC); |
5859 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) |
5860 | D.setInvalidType(); |
5861 | } |
5862 | } |
5863 | |
5864 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
5865 | QualType R = TInfo->getType(); |
5866 | |
5867 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
5868 | UPPC_DeclarationType)) |
5869 | D.setInvalidType(); |
5870 | |
5871 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
5872 | forRedeclarationInCurContext()); |
5873 | |
5874 | |
5875 | if (!D.getCXXScopeSpec().isSet()) { |
5876 | bool IsLinkageLookup = false; |
5877 | bool CreateBuiltins = false; |
5878 | |
5879 | |
5880 | |
5881 | |
5882 | |
5883 | |
5884 | |
5885 | |
5886 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
5887 | ; |
5888 | else if (CurContext->isFunctionOrMethod() && |
5889 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || |
5890 | R->isFunctionType())) { |
5891 | IsLinkageLookup = true; |
5892 | CreateBuiltins = |
5893 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); |
5894 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && |
5895 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) |
5896 | CreateBuiltins = true; |
5897 | |
5898 | if (IsLinkageLookup) { |
5899 | Previous.clear(LookupRedeclarationWithLinkage); |
5900 | Previous.setRedeclarationKind(ForExternalRedeclaration); |
5901 | } |
5902 | |
5903 | LookupName(Previous, S, CreateBuiltins); |
5904 | } else { |
5905 | LookupQualifiedName(Previous, DC); |
5906 | |
5907 | |
5908 | |
5909 | |
5910 | |
5911 | |
5912 | |
5913 | |
5914 | |
5915 | |
5916 | |
5917 | |
5918 | |
5919 | |
5920 | |
5921 | |
5922 | |
5923 | |
5924 | |
5925 | |
5926 | |
5927 | |
5928 | |
5929 | |
5930 | |
5931 | |
5932 | |
5933 | RemoveUsingDecls(Previous); |
5934 | } |
5935 | |
5936 | if (Previous.isSingleResult() && |
5937 | Previous.getFoundDecl()->isTemplateParameter()) { |
5938 | |
5939 | if (!D.isInvalidType()) |
5940 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
5941 | Previous.getFoundDecl()); |
5942 | |
5943 | |
5944 | Previous.clear(); |
5945 | } |
5946 | |
5947 | if (!R->isFunctionType() && DiagnoseClassNameShadow(DC, NameInfo)) |
5948 | |
5949 | Previous.clear(); |
5950 | |
5951 | |
5952 | |
5953 | |
5954 | |
5955 | if (Previous.isSingleTagDecl() && |
5956 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
5957 | (TemplateParamLists.size() == 0 || R->isFunctionType())) |
5958 | Previous.clear(); |
5959 | |
5960 | |
5961 | |
5962 | if (getLangOpts().CPlusPlus) |
5963 | CheckExtraCXXDefaultArguments(D); |
5964 | |
5965 | NamedDecl *New; |
5966 | |
5967 | bool AddToScope = true; |
5968 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
5969 | if (TemplateParamLists.size()) { |
5970 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); |
5971 | return nullptr; |
5972 | } |
5973 | |
5974 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
5975 | } else if (R->isFunctionType()) { |
5976 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
5977 | TemplateParamLists, |
5978 | AddToScope); |
5979 | } else { |
5980 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
5981 | AddToScope); |
5982 | } |
5983 | |
5984 | if (!New) |
5985 | return nullptr; |
5986 | |
5987 | |
5988 | |
5989 | if (New->getDeclName() && AddToScope) |
5990 | PushOnScopeChains(New, S); |
5991 | |
5992 | if (isInOpenMPDeclareTargetContext()) |
5993 | checkDeclIsAllowedInOpenMPTarget(nullptr, New); |
5994 | |
5995 | return New; |
5996 | } |
5997 | |
5998 | |
5999 | |
6000 | |
6001 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
6002 | ASTContext &Context, |
6003 | bool &SizeIsNegative, |
6004 | llvm::APSInt &Oversized) { |
6005 | |
6006 | |
6007 | |
6008 | |
6009 | SizeIsNegative = false; |
6010 | Oversized = 0; |
6011 | |
6012 | if (T->isDependentType()) |
6013 | return QualType(); |
6014 | |
6015 | QualifierCollector Qs; |
6016 | const Type *Ty = Qs.strip(T); |
6017 | |
6018 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { |
6019 | QualType Pointee = PTy->getPointeeType(); |
6020 | QualType FixedType = |
6021 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
6022 | Oversized); |
6023 | if (FixedType.isNull()) return FixedType; |
6024 | FixedType = Context.getPointerType(FixedType); |
6025 | return Qs.apply(Context, FixedType); |
6026 | } |
6027 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
6028 | QualType Inner = PTy->getInnerType(); |
6029 | QualType FixedType = |
6030 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, |
6031 | Oversized); |
6032 | if (FixedType.isNull()) return FixedType; |
6033 | FixedType = Context.getParenType(FixedType); |
6034 | return Qs.apply(Context, FixedType); |
6035 | } |
6036 | |
6037 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
6038 | if (!VLATy) |
6039 | return QualType(); |
6040 | |
6041 | QualType ElemTy = VLATy->getElementType(); |
6042 | if (ElemTy->isVariablyModifiedType()) { |
6043 | ElemTy = TryToFixInvalidVariablyModifiedType(ElemTy, Context, |
6044 | SizeIsNegative, Oversized); |
6045 | if (ElemTy.isNull()) |
6046 | return QualType(); |
6047 | } |
6048 | |
6049 | Expr::EvalResult Result; |
6050 | if (!VLATy->getSizeExpr() || |
6051 | !VLATy->getSizeExpr()->EvaluateAsInt(Result, Context)) |
6052 | return QualType(); |
6053 | |
6054 | llvm::APSInt Res = Result.Val.getInt(); |
6055 | |
6056 | |
6057 | if (Res.isSigned() && Res.isNegative()) { |
6058 | SizeIsNegative = true; |
6059 | return QualType(); |
6060 | } |
6061 | |
6062 | |
6063 | unsigned ActiveSizeBits = |
6064 | (!ElemTy->isDependentType() && !ElemTy->isVariablyModifiedType() && |
6065 | !ElemTy->isIncompleteType() && !ElemTy->isUndeducedType()) |
6066 | ? ConstantArrayType::getNumAddressingBits(Context, ElemTy, Res) |
6067 | : Res.getActiveBits(); |
6068 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
6069 | Oversized = Res; |
6070 | return QualType(); |
6071 | } |
6072 | |
6073 | QualType FoldedArrayType = Context.getConstantArrayType( |
6074 | ElemTy, Res, VLATy->getSizeExpr(), ArrayType::Normal, 0); |
6075 | return Qs.apply(Context, FoldedArrayType); |
6076 | } |
6077 | |
6078 | static void |
6079 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { |
6080 | SrcTL = SrcTL.getUnqualifiedLoc(); |
6081 | DstTL = DstTL.getUnqualifiedLoc(); |
6082 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
6083 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); |
6084 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), |
6085 | DstPTL.getPointeeLoc()); |
6086 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); |
6087 | return; |
6088 | } |
6089 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
6090 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); |
6091 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), |
6092 | DstPTL.getInnerLoc()); |
6093 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); |
6094 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); |
6095 | return; |
6096 | } |
6097 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
6098 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); |
6099 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); |
6100 | TypeLoc DstElemTL = DstATL.getElementLoc(); |
6101 | if (VariableArrayTypeLoc SrcElemATL = |
6102 | SrcElemTL.getAs<VariableArrayTypeLoc>()) { |
6103 | ConstantArrayTypeLoc DstElemATL = DstElemTL.castAs<ConstantArrayTypeLoc>(); |
6104 | FixInvalidVariablyModifiedTypeLoc(SrcElemATL, DstElemATL); |
6105 | } else { |
6106 | DstElemTL.initializeFullCopy(SrcElemTL); |
6107 | } |
6108 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
6109 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); |
6110 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); |
6111 | } |
6112 | |
6113 | |
6114 | |
6115 | |
6116 | static TypeSourceInfo* |
6117 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, |
6118 | ASTContext &Context, |
6119 | bool &SizeIsNegative, |
6120 | llvm::APSInt &Oversized) { |
6121 | QualType FixedTy |
6122 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, |
6123 | SizeIsNegative, Oversized); |
6124 | if (FixedTy.isNull()) |
6125 | return nullptr; |
6126 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); |
6127 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), |
6128 | FixedTInfo->getTypeLoc()); |
6129 | return FixedTInfo; |
6130 | } |
6131 | |
6132 | |
6133 | |
6134 | bool Sema::tryToFixVariablyModifiedVarType(TypeSourceInfo *&TInfo, |
6135 | QualType &T, SourceLocation Loc, |
6136 | unsigned FailedFoldDiagID) { |
6137 | bool SizeIsNegative; |
6138 | llvm::APSInt Oversized; |
6139 | TypeSourceInfo *FixedTInfo = TryToFixInvalidVariablyModifiedTypeSourceInfo( |
6140 | TInfo, Context, SizeIsNegative, Oversized); |
6141 | if (FixedTInfo) { |
6142 | Diag(Loc, diag::ext_vla_folded_to_constant); |
6143 | TInfo = FixedTInfo; |
6144 | T = FixedTInfo->getType(); |
6145 | return true; |
6146 | } |
6147 | |
6148 | if (SizeIsNegative) |
6149 | Diag(Loc, diag::err_typecheck_negative_array_size); |
6150 | else if (Oversized.getBoolValue()) |
6151 | Diag(Loc, diag::err_array_too_large) << toString(Oversized, 10); |
6152 | else if (FailedFoldDiagID) |
6153 | Diag(Loc, FailedFoldDiagID); |
6154 | return false; |
6155 | } |
6156 | |
6157 | |
6158 | |
6159 | |
6160 | |
6161 | void |
6162 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
6163 | if (!getLangOpts().CPlusPlus && |
6164 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) |
6165 | |
6166 | return; |
6167 | |
6168 | |
6169 | Context.getExternCContextDecl()->makeDeclVisibleInContext(ND); |
6170 | } |
6171 | |
6172 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
6173 | |
6174 | auto Result = Context.getExternCContextDecl()->lookup(Name); |
6175 | return Result.empty() ? nullptr : *Result.begin(); |
6176 | } |
6177 | |
6178 | |
6179 | |
6180 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
6181 | |
6182 | |
6183 | if (DS.isVirtualSpecified()) |
6184 | Diag(DS.getVirtualSpecLoc(), |
6185 | diag::err_virtual_non_function); |
6186 | |
6187 | if (DS.hasExplicitSpecifier()) |
6188 | Diag(DS.getExplicitSpecLoc(), |
6189 | diag::err_explicit_non_function); |
6190 | |
6191 | if (DS.isNoreturnSpecified()) |
6192 | Diag(DS.getNoreturnSpecLoc(), |
6193 | diag::err_noreturn_non_function); |
6194 | } |
6195 | |
6196 | NamedDecl* |
6197 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
6198 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
6199 | |
6200 | if (D.getCXXScopeSpec().isSet()) { |
6201 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
6202 | << D.getCXXScopeSpec().getRange(); |
6203 | D.setInvalidType(); |
6204 | |
6205 | DC = CurContext; |
6206 | Previous.clear(); |
6207 | } |
6208 | |
6209 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
6210 | |
6211 | if (D.getDeclSpec().isInlineSpecified()) |
6212 | Diag(D.getDeclSpec().getInlineSpecLoc(), diag::err_inline_non_function) |
6213 | << getLangOpts().CPlusPlus17; |
6214 | if (D.getDeclSpec().hasConstexprSpecifier()) |
6215 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) |
6216 | << 1 << static_cast<int>(D.getDeclSpec().getConstexprSpecifier()); |
6217 | |
6218 | if (D.getName().Kind != UnqualifiedIdKind::IK_Identifier) { |
6219 | if (D.getName().Kind == UnqualifiedIdKind::IK_DeductionGuideName) |
6220 | Diag(D.getName().StartLocation, |
6221 | diag::err_deduction_guide_invalid_specifier) |
6222 | << "typedef"; |
6223 | else |
6224 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) |
6225 | << D.getName().getSourceRange(); |
6226 | return nullptr; |
6227 | } |
6228 | |
6229 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
6230 | if (!NewTD) return nullptr; |
6231 | |
6232 | |
6233 | ProcessDeclAttributes(S, NewTD, D); |
6234 | |
6235 | CheckTypedefForVariablyModifiedType(S, NewTD); |
6236 | |
6237 | bool Redeclaration = D.isRedeclaration(); |
6238 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); |
6239 | D.setRedeclaration(Redeclaration); |
6240 | return ND; |
6241 | } |
6242 | |
6243 | void |
6244 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { |
6245 | |
6246 | |
6247 | |
6248 | |
6249 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
6250 | QualType T = TInfo->getType(); |
6251 | if (T->isVariablyModifiedType()) { |
6252 | setFunctionHasBranchProtectedScope(); |
6253 | |
6254 | if (S->getFnParent() == nullptr) { |
6255 | bool SizeIsNegative; |
6256 | llvm::APSInt Oversized; |
6257 | TypeSourceInfo *FixedTInfo = |
6258 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
6259 | SizeIsNegative, |
6260 | Oversized); |
6261 | if (FixedTInfo) { |
6262 | Diag(NewTD->getLocation(), diag::ext_vla_folded_to_constant); |
6263 | NewTD->setTypeSourceInfo(FixedTInfo); |
6264 | } else { |
6265 | if (SizeIsNegative) |
6266 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
6267 | else if (T->isVariableArrayType()) |
6268 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
6269 | else if (Oversized.getBoolValue()) |
6270 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
6271 | << toString(Oversized, 10); |
6272 | else |
6273 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
6274 | NewTD->setInvalidDecl(); |
6275 | } |
6276 | } |
6277 | } |
6278 | } |
6279 | |
6280 | |
6281 | |
6282 | |
6283 | NamedDecl* |
6284 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, |
6285 | LookupResult &Previous, bool &Redeclaration) { |
6286 | |
6287 | |
6288 | NamedDecl *ShadowedDecl = getShadowedDeclaration(NewTD, Previous); |
6289 | |
6290 | |
6291 | |
6292 | FilterLookupForScope(Previous, DC, S, false, |
6293 | false); |
6294 | filterNonConflictingPreviousTypedefDecls(*this, NewTD, Previous); |
6295 | if (!Previous.empty()) { |
6296 | Redeclaration = true; |
6297 | MergeTypedefNameDecl(S, NewTD, Previous); |
6298 | } else { |
6299 | inferGslPointerAttribute(NewTD); |
6300 | } |
6301 | |
6302 | if (ShadowedDecl && !Redeclaration) |
6303 | CheckShadow(NewTD, ShadowedDecl, Previous); |
6304 | |
6305 | |
6306 | if (IdentifierInfo *II = NewTD->getIdentifier()) |
6307 | if (!NewTD->isInvalidDecl() && |
6308 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
6309 | if (II->isStr("FILE")) |
6310 | Context.setFILEDecl(NewTD); |
6311 | else if (II->isStr("jmp_buf")) |
6312 | Context.setjmp_bufDecl(NewTD); |
6313 | else if (II->isStr("sigjmp_buf")) |
6314 | Context.setsigjmp_bufDecl(NewTD); |
6315 | else if (II->isStr("ucontext_t")) |
6316 | Context.setucontext_tDecl(NewTD); |
6317 | } |
6318 | |
6319 | return NewTD; |
6320 | } |
6321 | |
6322 | |
6323 | |
6324 | |
6325 | |
6326 | |
6327 | |
6328 | |
6329 | |
6330 | |
6331 | |
6332 | |
6333 | |
6334 | |
6335 | |
6336 | |
6337 | |
6338 | |
6339 | |
6340 | static bool |
6341 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
6342 | ASTContext &Context) { |
6343 | if (!PrevDecl) |
6344 | return false; |
6345 | |
6346 | if (!PrevDecl->hasLinkage()) |
6347 | return false; |
6348 | |
6349 | if (Context.getLangOpts().CPlusPlus) { |
6350 | |
6351 | |
6352 | |
6353 | |
6354 | |
6355 | |
6356 | DeclContext *OuterContext = DC->getRedeclContext(); |
6357 | if (!OuterContext->isFunctionOrMethod()) |
6358 | |
6359 | return false; |
6360 | |
6361 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); |
6362 | if (PrevOuterContext->isRecord()) |
6363 | |
6364 | return false; |
6365 | |
6366 | |
6367 | |
6368 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
6369 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); |
6370 | |
6371 | |
6372 | |
6373 | if (!OuterContext->Equals(PrevOuterContext)) |
6374 | return false; |
6375 | } |
6376 | |
6377 | return true; |
6378 | } |
6379 | |
6380 | static void SetNestedNameSpecifier(Sema &S, DeclaratorDecl *DD, Declarator &D) { |
6381 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
6382 | if (!SS.isSet()) return; |
6383 | DD->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
6384 | } |
6385 | |
6386 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
6387 | QualType type = decl->getType(); |
6388 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
6389 | if (lifetime == Qualifiers::OCL_Autoreleasing) { |
6390 | |
6391 | unsigned kind = -1U; |
6392 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
6393 | if (var->hasAttr<BlocksAttr>()) |
6394 | kind = 0; |
6395 | else if (!var->hasLocalStorage()) |
6396 | kind = 1; |
6397 | } else if (isa<ObjCIvarDecl>(decl)) { |
6398 | kind = 3; |
6399 | } else if (isa<FieldDecl>(decl)) { |
6400 | kind = 2; |
6401 | } |
6402 | |
6403 | if (kind != -1U) { |
6404 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) |
6405 | << kind; |
6406 | } |
6407 | } else if (lifetime == Qualifiers::OCL_None) { |
6408 | |
6409 | if (!type->isObjCLifetimeType()) |
6410 | return false; |
6411 | |
6412 | lifetime = type->getObjCARCImplicitLifetime(); |
6413 | type = Context.getLifetimeQualifiedType(type, lifetime); |
6414 | decl->setType(type); |
6415 | } |
6416 | |
6417 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
6418 | |
6419 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && |
6420 | var->getTLSKind()) { |
6421 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
6422 | << var->getType(); |
6423 | return true; |
6424 | } |
6425 | } |
6426 | |
6427 | return false; |
6428 | } |
6429 | |
6430 | void Sema::deduceOpenCLAddressSpace(ValueDecl *Decl) { |
6431 | if (Decl->getType().hasAddressSpace()) |
6432 | return; |
6433 | if (Decl->getType()->isDependentType()) |
6434 | return; |
6435 | if (VarDecl *Var = dyn_cast<VarDecl>(Decl)) { |
6436 | QualType Type = Var->getType(); |
6437 | if (Type->isSamplerT() || Type->isVoidType()) |
6438 | return; |
6439 | LangAS ImplAS = LangAS::opencl_private; |
6440 | |
6441 | |
6442 | |
6443 | |
6444 | if (getOpenCLOptions().areProgramScopeVariablesSupported(getLangOpts()) && |
6445 | Var->hasGlobalStorage()) |
6446 | ImplAS = LangAS::opencl_global; |
6447 | |
6448 | |
6449 | if (auto DT = dyn_cast<DecayedType>(Type)) { |
6450 | auto OrigTy = DT->getOriginalType(); |
6451 | if (!OrigTy.hasAddressSpace() && OrigTy->isArrayType()) { |
6452 | |
6453 | |
6454 | OrigTy = Context.getAddrSpaceQualType(OrigTy, ImplAS); |
6455 | OrigTy = QualType(Context.getAsArrayType(OrigTy), 0); |
6456 | |
6457 | Type = Context.getDecayedType(OrigTy); |
6458 | } |
6459 | } |
6460 | Type = Context.getAddrSpaceQualType(Type, ImplAS); |
6461 | |
6462 | |
6463 | |
6464 | |
6465 | if (Type->isArrayType()) |
6466 | Type = QualType(Context.getAsArrayType(Type), 0); |
6467 | Decl->setType(Type); |
6468 | } |
6469 | } |
6470 | |
6471 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
6472 | |
6473 | |
6474 | assert(S.ParsingInitForAutoVars.count(&ND) == 0); |
6475 | |
6476 | |
6477 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
6478 | if (!ND.isExternallyVisible()) { |
6479 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
6480 | ND.dropAttr<WeakAttr>(); |
6481 | } |
6482 | } |
6483 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { |
6484 | if (ND.isExternallyVisible()) { |
6485 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
6486 | ND.dropAttr<WeakRefAttr>(); |
6487 | ND.dropAttr<AliasAttr>(); |
6488 | } |
6489 | } |
6490 | |
6491 | if (auto *VD = dyn_cast<VarDecl>(&ND)) { |
6492 | if (VD->hasInit()) { |
6493 | if (const auto *Attr = VD->getAttr<AliasAttr>()) { |
6494 | assert(VD->isThisDeclarationADefinition() && |
6495 | !VD->isExternallyVisible() && "Broken AliasAttr handled late!"); |
6496 | S.Diag(Attr->getLocation(), diag::err_alias_is_definition) << VD << 0; |
6497 | VD->dropAttr<AliasAttr>(); |
6498 | } |
6499 | } |
6500 | } |
6501 | |
6502 | |
6503 | |
6504 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { |
6505 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { |
6506 | S.Diag(Attr->getLocation(), |
6507 | diag::err_attribute_selectany_non_extern_data); |
6508 | ND.dropAttr<SelectAnyAttr>(); |
6509 | } |
6510 | } |
6511 | |
6512 | if (const InheritableAttr *Attr = getDLLAttr(&ND)) { |
6513 | auto *VD = dyn_cast<VarDecl>(&ND); |
6514 | bool IsAnonymousNS = false; |
6515 | bool IsMicrosoft = S.Context.getTargetInfo().getCXXABI().isMicrosoft(); |
6516 | if (VD) { |
6517 | const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(VD->getDeclContext()); |
6518 | while (NS && !IsAnonymousNS) { |
6519 | IsAnonymousNS = NS->isAnonymousNamespace(); |
6520 | NS = dyn_cast<NamespaceDecl>(NS->getParent()); |
6521 | } |
6522 | } |
6523 | |
6524 | |
6525 | |
6526 | |
6527 | bool AnonNSInMicrosoftMode = IsAnonymousNS && IsMicrosoft; |
6528 | if ((ND.isExternallyVisible() && AnonNSInMicrosoftMode) || |
6529 | (!AnonNSInMicrosoftMode && |
6530 | (!ND.isExternallyVisible() || (VD && VD->isStaticLocal())))) { |
6531 | S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) |
6532 | << &ND << Attr; |
6533 | ND.setInvalidDecl(); |
6534 | } |
6535 | } |
6536 | |
6537 | |
6538 | if (const auto *FD = dyn_cast<FunctionDecl>(&ND)) { |
6539 | |
6540 | |
6541 | |
6542 | AttributedTypeLoc ATL; |
6543 | for (TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc(); |
6544 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
6545 | TL = ATL.getModifiedLoc()) { |
6546 | |
6547 | |
6548 | |
6549 | if (const auto *A = ATL.getAttrAs<LifetimeBoundAttr>()) { |
6550 | const auto *MD = dyn_cast<CXXMethodDecl>(FD); |
6551 | if (!MD || MD->isStatic()) { |
6552 | S.Diag(A->getLocation(), diag::err_lifetimebound_no_object_param) |
6553 | << !MD << A->getRange(); |
6554 | } else if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) { |
6555 | S.Diag(A->getLocation(), diag::err_lifetimebound_ctor_dtor) |
6556 | << isa<CXXDestructorDecl>(MD) << A->getRange(); |
6557 | } |
6558 | } |
6559 | } |
6560 | } |
6561 | } |
6562 | |
6563 | static void checkDLLAttributeRedeclaration(Sema &S, NamedDecl *OldDecl, |
6564 | NamedDecl *NewDecl, |
6565 | bool IsSpecialization, |
6566 | bool IsDefinition) { |
6567 | if (OldDecl->isInvalidDecl() || NewDecl->isInvalidDecl()) |
6568 | return; |
6569 | |
6570 | bool IsTemplate = false; |
6571 | if (TemplateDecl *OldTD = dyn_cast<TemplateDecl>(OldDecl)) { |
6572 | OldDecl = OldTD->getTemplatedDecl(); |
6573 | IsTemplate = true; |
6574 | if (!IsSpecialization) |
6575 | IsDefinition = false; |
6576 | } |
6577 | if (TemplateDecl *NewTD = dyn_cast<TemplateDecl>(NewDecl)) { |
6578 | NewDecl = NewTD->getTemplatedDecl(); |
6579 | IsTemplate = true; |
6580 | } |
6581 | |
6582 | if (!OldDecl || !NewDecl) |
6583 | return; |
6584 | |
6585 | const DLLImportAttr *OldImportAttr = OldDecl->getAttr<DLLImportAttr>(); |
6586 | const DLLExportAttr *OldExportAttr = OldDecl->getAttr<DLLExportAttr>(); |
6587 | const DLLImportAttr *NewImportAttr = NewDecl->getAttr<DLLImportAttr>(); |
6588 | const DLLExportAttr *NewExportAttr = NewDecl->getAttr<DLLExportAttr>(); |
6589 | |
6590 | |
6591 | |
6592 | bool HasNewAttr = (NewImportAttr && !NewImportAttr->isInherited()) || |
6593 | (NewExportAttr && !NewExportAttr->isInherited()); |
6594 | |
6595 | |
6596 | |
6597 | |
6598 | |
6599 | bool AddsAttr = !(OldImportAttr || OldExportAttr) && HasNewAttr; |
6600 | |
6601 | if (AddsAttr && !IsSpecialization && !OldDecl->isImplicit()) { |
6602 | |
6603 | bool JustWarn = false; |
6604 | if (!OldDecl->isCXXClassMember()) { |
6605 | auto *VD = dyn_cast<VarDecl>(OldDecl); |
6606 | if (VD && !VD->getDescribedVarTemplate()) |
6607 | JustWarn = true; |
6608 | auto *FD = dyn_cast<FunctionDecl>(OldDecl); |
6609 | if (FD && FD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) |
6610 | JustWarn = true; |
6611 | } |
6612 | |
6613 | |
6614 | |
6615 | |
6616 | if (OldDecl->isUsed()) |
6617 | if (!isa<FunctionDecl>(OldDecl) || !NewImportAttr) |
6618 | JustWarn = false; |
6619 | |
6620 | unsigned DiagID = JustWarn ? diag::warn_attribute_dll_redeclaration |
6621 | : diag::err_attribute_dll_redeclaration; |
6622 | S.Diag(NewDecl->getLocation(), DiagID) |
6623 | << NewDecl |
6624 | << (NewImportAttr ? (const Attr *)NewImportAttr : NewExportAttr); |
6625 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
6626 | if (!JustWarn) { |
6627 | NewDecl->setInvalidDecl(); |
6628 | return; |
6629 | } |
6630 | } |
6631 | |
6632 | |
6633 | |
6634 | |
6635 | |
6636 | |
6637 | bool IsInline = false, IsStaticDataMember = false, IsQualifiedFriend = false; |
6638 | bool IsMicrosoftABI = S.Context.getTargetInfo().shouldDLLImportComdatSymbols(); |
6639 | if (const auto *VD = dyn_cast<VarDecl>(NewDecl)) { |
6640 | |
6641 | |
6642 | IsStaticDataMember = VD->isStaticDataMember(); |
6643 | IsDefinition = VD->isThisDeclarationADefinition(S.Context) != |
6644 | VarDecl::DeclarationOnly; |
6645 | } else if (const auto *FD = dyn_cast<FunctionDecl>(NewDecl)) { |
6646 | IsInline = FD->isInlined(); |
6647 | IsQualifiedFriend = FD->getQualifier() && |
6648 | FD->getFriendObjectKind() == Decl::FOK_Declared; |
6649 | } |
6650 | |
6651 | if (OldImportAttr && !HasNewAttr && |
6652 | (!IsInline || (IsMicrosoftABI && IsTemplate)) && !IsStaticDataMember && |
6653 | !NewDecl->isLocalExternDecl() && !IsQualifiedFriend) { |
6654 | if (IsMicrosoftABI && IsDefinition) { |
6655 | S.Diag(NewDecl->getLocation(), |
6656 | diag::warn_redeclaration_without_import_attribute) |
6657 | << NewDecl; |
6658 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
6659 | NewDecl->dropAttr<DLLImportAttr>(); |
6660 | NewDecl->addAttr( |
6661 | DLLExportAttr::CreateImplicit(S.Context, NewImportAttr->getRange())); |
6662 | } else { |
6663 | S.Diag(NewDecl->getLocation(), |
6664 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) |
6665 | << NewDecl << OldImportAttr; |
6666 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
6667 | S.Diag(OldImportAttr->getLocation(), diag::note_previous_attribute); |
6668 | OldDecl->dropAttr<DLLImportAttr>(); |
6669 | NewDecl->dropAttr<DLLImportAttr>(); |
6670 | } |
6671 | } else if (IsInline && OldImportAttr && !IsMicrosoftABI) { |
6672 | |
6673 | |
6674 | OldDecl->dropAttr<DLLImportAttr>(); |
6675 | NewDecl->dropAttr<DLLImportAttr>(); |
6676 | S.Diag(NewDecl->getLocation(), |
6677 | diag::warn_dllimport_dropped_from_inline_function) |
6678 | << NewDecl << OldImportAttr; |
6679 | } |
6680 | |
6681 | |
6682 | |
6683 | |
6684 | |
6685 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDecl)) { |
6686 | if (MD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization && |
6687 | !NewImportAttr && !NewExportAttr) { |
6688 | if (const DLLExportAttr *ParentExportAttr = |
6689 | MD->getParent()->getAttr<DLLExportAttr>()) { |
6690 | DLLExportAttr *NewAttr = ParentExportAttr->clone(S.Context); |
6691 | NewAttr->setInherited(true); |
6692 | NewDecl->addAttr(NewAttr); |
6693 | } |
6694 | } |
6695 | } |
6696 | } |
6697 | |
6698 | |
6699 | |
6700 | |
6701 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { |
6702 | |
6703 | |
6704 | |
6705 | if (!FD->isInlined()) return false; |
6706 | |
6707 | |
6708 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) |
6709 | return false; |
6710 | |
6711 | |
6712 | return S.Context.GetGVALinkageForFunction(FD) == GVA_AvailableExternally; |
6713 | } |
6714 | |
6715 | |
6716 | |
6717 | |
6718 | |
6719 | |
6720 | |
6721 | |
6722 | |
6723 | |
6724 | |
6725 | |
6726 | |
6727 | |
6728 | |
6729 | |
6730 | |
6731 | template<typename T> |
6732 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { |
6733 | if (S.getLangOpts().CPlusPlus) { |
6734 | |
6735 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) |
6736 | return false; |
6737 | |
6738 | |
6739 | if (S.getLangOpts().CUDA && (D->template hasAttr<CUDADeviceAttr>() || |
6740 | D->template hasAttr<CUDAHostAttr>())) |
6741 | return false; |
6742 | } |
6743 | return D->isExternC(); |
6744 | } |
6745 | |
6746 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
6747 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); |
6748 | if (DC->isFunctionOrMethod() || isa<OMPDeclareReductionDecl>(DC) || |
6749 | isa<OMPDeclareMapperDecl>(DC)) |
6750 | return VD->hasExternalStorage(); |
6751 | if (DC->isFileContext()) |
6752 | return true; |
6753 | if (DC->isRecord()) |
6754 | return false; |
6755 | if (isa<RequiresExprBodyDecl>(DC)) |
6756 | return false; |
6757 | llvm_unreachable("Unexpected context"); |
6758 | } |
6759 | |
6760 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { |
6761 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); |
6762 | if (DC->isFileContext() || DC->isFunctionOrMethod() || |
6763 | isa<OMPDeclareReductionDecl>(DC) || isa<OMPDeclareMapperDecl>(DC)) |
6764 | return true; |
6765 | if (DC->isRecord()) |
6766 | return false; |
6767 | llvm_unreachable("Unexpected context"); |
6768 | } |
6769 | |
6770 | static bool hasParsedAttr(Scope *S, const Declarator &PD, |
6771 | ParsedAttr::Kind Kind) { |
6772 | |
6773 | if (PD.getDeclSpec().getAttributes().hasAttribute(Kind)) |
6774 | return true; |
6775 | |
6776 | |
6777 | |
6778 | for (unsigned I = 0, E = PD.getNumTypeObjects(); I != E; ++I) { |
6779 | if (PD.getTypeObject(I).getAttrs().hasAttribute(Kind)) |
6780 | return true; |
6781 | } |
6782 | |
6783 | |
6784 | return PD.getAttributes().hasAttribute(Kind); |
6785 | } |
6786 | |
6787 | |
6788 | |
6789 | bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) { |
6790 | if (!DC->isFunctionOrMethod()) |
6791 | return false; |
6792 | |
6793 | |
6794 | |
6795 | |
6796 | if (DC->isDependentContext()) |
6797 | return true; |
6798 | |
6799 | |
6800 | |
6801 | |
6802 | |
6803 | |
6804 | |
6805 | |
6806 | while (!DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) |
6807 | DC = DC->getParent(); |
6808 | return true; |
6809 | } |
6810 | |
6811 | |
6812 | static bool isDeclExternC(const Decl *D) { |
6813 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
6814 | return FD->isExternC(); |
6815 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
6816 | return VD->isExternC(); |
6817 | |
6818 | llvm_unreachable("Unknown type of decl!"); |
6819 | } |
6820 | |
6821 | |
6822 | static bool diagnoseOpenCLTypes(Sema &Se, VarDecl *NewVD) { |
6823 | DeclContext *DC = NewVD->getDeclContext(); |
6824 | QualType R = NewVD->getType(); |
6825 | |
6826 | |
6827 | |
6828 | |
6829 | if (R->isImageType() || R->isPipeType()) { |
6830 | Se.Diag(NewVD->getLocation(), |
6831 | diag::err_opencl_type_can_only_be_used_as_function_parameter) |
6832 | << R; |
6833 | NewVD->setInvalidDecl(); |
6834 | return false; |
6835 | } |
6836 | |
6837 | |
6838 | |
6839 | |
6840 | |
6841 | |
6842 | if (NewVD->hasGlobalStorage() && !NewVD->isStaticLocal()) { |
6843 | if (R->isReserveIDT() || R->isClkEventT() || R->isEventT()) { |
6844 | Se.Diag(NewVD->getLocation(), |
6845 | diag::err_invalid_type_for_program_scope_var) |
6846 | << R; |
6847 | NewVD->setInvalidDecl(); |
6848 | return false; |
6849 | } |
6850 | } |
6851 | |
6852 | |
6853 | if (!Se.getOpenCLOptions().isAvailableOption("__cl_clang_function_pointers", |
6854 | Se.getLangOpts())) { |
6855 | QualType NR = R.getCanonicalType(); |
6856 | while (NR->isPointerType() || NR->isMemberFunctionPointerType() || |
6857 | NR->isReferenceType()) { |
6858 | if (NR->isFunctionPointerType() || NR->isMemberFunctionPointerType() || |
6859 | NR->isFunctionReferenceType()) { |
6860 | Se.Diag(NewVD->getLocation(), diag::err_opencl_function_pointer) |
6861 | << NR->isReferenceType(); |
6862 | NewVD->setInvalidDecl(); |
6863 | return false; |
6864 | } |
6865 | NR = NR->getPointeeType(); |
6866 | } |
6867 | } |
6868 | |
6869 | if (!Se.getOpenCLOptions().isAvailableOption("cl_khr_fp16", |
6870 | Se.getLangOpts())) { |
6871 | |
6872 | |
6873 | if (Se.Context.getBaseElementType(R)->isHalfType()) { |
6874 | Se.Diag(NewVD->getLocation(), diag::err_opencl_half_declaration) << R; |
6875 | NewVD->setInvalidDecl(); |
6876 | return false; |
6877 | } |
6878 | } |
6879 | |
6880 | |
6881 | |
6882 | |
6883 | if (R->isEventT()) { |
6884 | if (R.getAddressSpace() != LangAS::opencl_private) { |
6885 | Se.Diag(NewVD->getBeginLoc(), diag::err_event_t_addr_space_qual); |
6886 | NewVD->setInvalidDecl(); |
6887 | return false; |
6888 | } |
6889 | } |
6890 | |
6891 | if (R->isSamplerT()) { |
6892 | |
6893 | |
6894 | |
6895 | if (R.getAddressSpace() == LangAS::opencl_local || |
6896 | R.getAddressSpace() == LangAS::opencl_global) { |
6897 | Se.Diag(NewVD->getLocation(), diag::err_wrong_sampler_addressspace); |
6898 | NewVD->setInvalidDecl(); |
6899 | } |
6900 | |
6901 | |
6902 | |
6903 | |
6904 | if (DC->isTranslationUnit() && |
6905 | !(R.getAddressSpace() == LangAS::opencl_constant || |
6906 | R.isConstQualified())) { |
6907 | Se.Diag(NewVD->getLocation(), diag::err_opencl_nonconst_global_sampler); |
6908 | NewVD->setInvalidDecl(); |
6909 | } |
6910 | if (NewVD->isInvalidDecl()) |
6911 | return false; |
6912 | } |
6913 | |
6914 | return true; |
6915 | } |
6916 | |
6917 | template <typename AttrTy> |
6918 | static void copyAttrFromTypedefToDecl(Sema &S, Decl *D, const TypedefType *TT) { |
6919 | const TypedefNameDecl *TND = TT->getDecl(); |
6920 | if (const auto *Attribute = TND->getAttr<AttrTy>()) { |
6921 | AttrTy *Clone = Attribute->clone(S.Context); |
6922 | Clone->setInherited(true); |
6923 | D->addAttr(Clone); |
6924 | } |
6925 | } |
6926 | |
6927 | NamedDecl *Sema::ActOnVariableDeclarator( |
6928 | Scope *S, Declarator &D, DeclContext *DC, TypeSourceInfo *TInfo, |
6929 | LookupResult &Previous, MultiTemplateParamsArg TemplateParamLists, |
6930 | bool &AddToScope, ArrayRef<BindingDecl *> Bindings) { |
6931 | QualType R = TInfo->getType(); |
6932 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
6933 | |
6934 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
6935 | |
6936 | if (D.isDecompositionDeclarator()) { |
6937 | |
6938 | |
6939 | auto &Decomp = D.getDecompositionDeclarator(); |
6940 | if (!Decomp.bindings().empty()) { |
6941 | II = Decomp.bindings()[0].Name; |
6942 | Name = II; |
6943 | } |
6944 | } else if (!II) { |
6945 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) << Name; |
6946 | return nullptr; |
6947 | } |
6948 | |
6949 | |
6950 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
6951 | StorageClass SC = StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); |
6952 | |
6953 | |
6954 | |
6955 | if (SC == SC_None && !DC->isRecord() && |
6956 | hasParsedAttr(S, D, ParsedAttr::AT_DLLImport) && |
6957 | !hasParsedAttr(S, D, ParsedAttr::AT_DLLExport)) |
6958 | SC = SC_Extern; |
6959 | |
6960 | DeclContext *OriginalDC = DC; |
6961 | bool IsLocalExternDecl = SC == SC_Extern && |
6962 | adjustContextForLocalExternDecl(DC); |
6963 | |
6964 | if (SCSpec == DeclSpec::SCS_mutable) { |
6965 | |
6966 | |
6967 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
6968 | D.setInvalidType(); |
6969 | SC = SC_None; |
6970 | } |
6971 | |
6972 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
6973 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( |
6974 | D.getDeclSpec().getStorageClassSpecLoc())) { |
6975 | |
6976 | |
6977 | |
6978 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
6979 | getLangOpts().CPlusPlus17 ? diag::ext_register_storage_class |
6980 | : diag::warn_deprecated_register) |
6981 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
6982 | } |
6983 | |
6984 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
6985 | |
6986 | if (!DC->isRecord() && S->getFnParent() == nullptr) { |
6987 | |
6988 | |
6989 | |
6990 | if (SC == SC_Auto || (SC == SC_Register && !D.getAsmLabel())) { |
6991 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
6992 | D.setInvalidType(); |
6993 | } |
6994 | } |
6995 | |
6996 | |
6997 | |
6998 | if (D.hasInitializer() && R->isVariableArrayType()) |
6999 | tryToFixVariablyModifiedVarType(TInfo, R, D.getIdentifierLoc(), |
7000 | 0); |
7001 | |
7002 | bool IsMemberSpecialization = false; |
7003 | bool IsVariableTemplateSpecialization = false; |
7004 | bool IsPartialSpecialization = false; |
7005 | bool IsVariableTemplate = false; |
7006 | VarDecl *NewVD = nullptr; |
7007 | VarTemplateDecl *NewTemplate = nullptr; |
7008 | TemplateParameterList *TemplateParams = nullptr; |
7009 | if (!getLangOpts().CPlusPlus) { |
7010 | NewVD = VarDecl::Create(Context, DC, D.getBeginLoc(), D.getIdentifierLoc(), |
7011 | II, R, TInfo, SC); |
7012 | |
7013 | if (R->getContainedDeducedType()) |
7014 | ParsingInitForAutoVars.insert(NewVD); |
7015 | |
7016 | if (D.isInvalidType()) |
7017 | NewVD->setInvalidDecl(); |
7018 | |
7019 | if (NewVD->getType().hasNonTrivialToPrimitiveDestructCUnion() && |
7020 | NewVD->hasLocalStorage()) |
7021 | checkNonTrivialCUnion(NewVD->getType(), NewVD->getLocation(), |
7022 | NTCUC_AutoVar, NTCUK_Destruct); |
7023 | } else { |
7024 | bool Invalid = false; |
7025 | |
7026 | if (DC->isRecord() && !CurContext->isRecord()) { |
7027 | |
7028 | switch (SC) { |
7029 | case SC_None: |
7030 | break; |
7031 | case SC_Static: |
7032 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
7033 | diag::err_static_out_of_line) |
7034 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
7035 | break; |
7036 | case SC_Auto: |
7037 | case SC_Register: |
7038 | case SC_Extern: |
7039 | |
7040 | |
7041 | |
7042 | |
7043 | |
7044 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
7045 | diag::err_storage_class_for_static_member) |
7046 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
7047 | break; |
7048 | case SC_PrivateExtern: |
7049 | llvm_unreachable("C storage class in c++!"); |
7050 | } |
7051 | } |
7052 | |
7053 | if (SC == SC_Static && CurContext->isRecord()) { |
7054 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
7055 | |
7056 | |
7057 | const DeclContext *FunctionOrMethod = nullptr; |
7058 | const CXXRecordDecl *AnonStruct = nullptr; |
7059 | for (DeclContext *Ctxt = DC; Ctxt; Ctxt = Ctxt->getParent()) { |
7060 | if (Ctxt->isFunctionOrMethod()) { |
7061 | FunctionOrMethod = Ctxt; |
7062 | break; |
7063 | } |
7064 | const CXXRecordDecl *ParentDecl = dyn_cast<CXXRecordDecl>(Ctxt); |
7065 | if (ParentDecl && !ParentDecl->getDeclName()) { |
7066 | AnonStruct = ParentDecl; |
7067 | break; |
7068 | } |
7069 | } |
7070 | if (FunctionOrMethod) { |
7071 | |
7072 | |
7073 | Diag(D.getIdentifierLoc(), |
7074 | diag::err_static_data_member_not_allowed_in_local_class) |
7075 | << Name << RD->getDeclName() << RD->getTagKind(); |
7076 | } else if (AnonStruct) { |
7077 | |
7078 | |
7079 | |
7080 | Diag(D.getIdentifierLoc(), |
7081 | diag::err_static_data_member_not_allowed_in_anon_struct) |
7082 | << Name << AnonStruct->getTagKind(); |
7083 | Invalid = true; |
7084 | } else if (RD->isUnion()) { |
7085 | |
7086 | |
7087 | Diag(D.getIdentifierLoc(), |
7088 | getLangOpts().CPlusPlus11 |
7089 | ? diag::warn_cxx98_compat_static_data_member_in_union |
7090 | : diag::ext_static_data_member_in_union) << Name; |
7091 | } |
7092 | } |
7093 | } |
7094 | |
7095 | |
7096 | |
7097 | bool InvalidScope = false; |
7098 | TemplateParams = MatchTemplateParametersToScopeSpecifier( |
7099 | D.getDeclSpec().getBeginLoc(), D.getIdentifierLoc(), |
7100 | D.getCXXScopeSpec(), |
7101 | D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId |
7102 | ? D.getName().TemplateId |
7103 | : nullptr, |
7104 | TemplateParamLists, |
7105 | false, IsMemberSpecialization, InvalidScope); |
7106 | Invalid |= InvalidScope; |
7107 | |
7108 | if (TemplateParams) { |
7109 | if (!TemplateParams->size() && |
7110 | D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
7111 | |
7112 | |
7113 | Diag(TemplateParams->getTemplateLoc(), |
7114 | diag::err_template_variable_noparams) |
7115 | << II |
7116 | << SourceRange(TemplateParams->getTemplateLoc(), |
7117 | TemplateParams->getRAngleLoc()); |
7118 | TemplateParams = nullptr; |
7119 | } else { |
7120 | |
7121 | if (CheckTemplateDeclScope(S, TemplateParams)) |
7122 | return nullptr; |
7123 | |
7124 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
7125 | |
7126 | IsVariableTemplateSpecialization = true; |
7127 | IsPartialSpecialization = TemplateParams->size() > 0; |
7128 | } else { |
7129 | |
7130 | IsVariableTemplate = true; |
7131 | |
7132 | |
7133 | Diag(D.getIdentifierLoc(), |
7134 | getLangOpts().CPlusPlus14 |
7135 | ? diag::warn_cxx11_compat_variable_template |
7136 | : diag::ext_variable_template); |
7137 | } |
7138 | } |
7139 | } else { |
7140 | |
7141 | if (!TemplateParamLists.empty() && IsMemberSpecialization && |
7142 | CheckTemplateDeclScope(S, TemplateParamLists.back())) |
7143 | return nullptr; |
7144 | assert((Invalid || |
7145 | D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) && |
7146 | "should have a 'template<>' for this decl"); |
7147 | } |
7148 | |
7149 | if (IsVariableTemplateSpecialization) { |
7150 | SourceLocation TemplateKWLoc = |
7151 | TemplateParamLists.size() > 0 |
7152 | ? TemplateParamLists[0]->getTemplateLoc() |
7153 | : SourceLocation(); |
7154 | DeclResult Res = ActOnVarTemplateSpecialization( |
7155 | S, D, TInfo, TemplateKWLoc, TemplateParams, SC, |
7156 | IsPartialSpecialization); |
7157 | if (Res.isInvalid()) |
7158 | return nullptr; |
7159 | NewVD = cast<VarDecl>(Res.get()); |
7160 | AddToScope = false; |
7161 | } else if (D.isDecompositionDeclarator()) { |
7162 | NewVD = DecompositionDecl::Create(Context, DC, D.getBeginLoc(), |
7163 | D.getIdentifierLoc(), R, TInfo, SC, |
7164 | Bindings); |
7165 | } else |
7166 | NewVD = VarDecl::Create(Context, DC, D.getBeginLoc(), |
7167 | D.getIdentifierLoc(), II, R, TInfo, SC); |
7168 | |
7169 | |
7170 | if (IsVariableTemplate) { |
7171 | NewTemplate = |
7172 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, |
7173 | TemplateParams, NewVD); |
7174 | NewVD->setDescribedVarTemplate(NewTemplate); |
7175 | } |
7176 | |
7177 | |
7178 | |
7179 | if (R->getContainedDeducedType()) |
7180 | ParsingInitForAutoVars.insert(NewVD); |
7181 | |
7182 | if (D.isInvalidType() || Invalid) { |
7183 | NewVD->setInvalidDecl(); |
7184 | if (NewTemplate) |
7185 | NewTemplate->setInvalidDecl(); |
7186 | } |
7187 | |
7188 | SetNestedNameSpecifier(*this, NewVD, D); |
7189 | |
7190 | |
7191 | |
7192 | unsigned VDTemplateParamLists = TemplateParams ? 1 : 0; |
7193 | if (TemplateParamLists.size() > VDTemplateParamLists) |
7194 | NewVD->setTemplateParameterListsInfo( |
7195 | Context, TemplateParamLists.drop_back(VDTemplateParamLists)); |
7196 | } |
7197 | |
7198 | if (D.getDeclSpec().isInlineSpecified()) { |
7199 | if (!getLangOpts().CPlusPlus) { |
7200 | Diag(D.getDeclSpec().getInlineSpecLoc(), diag::err_inline_non_function) |
7201 | << 0; |
7202 | } else if (CurContext->isFunctionOrMethod()) { |
7203 | |
7204 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
7205 | diag::err_inline_declaration_block_scope) << Name |
7206 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
7207 | } else { |
7208 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
7209 | getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_inline_variable |
7210 | : diag::ext_inline_variable); |
7211 | NewVD->setInlineSpecified(); |
7212 | } |
7213 | } |
7214 | |
7215 | |
7216 | |
7217 | NewVD->setLexicalDeclContext(CurContext); |
7218 | if (NewTemplate) |
7219 | NewTemplate->setLexicalDeclContext(CurContext); |
7220 | |
7221 | if (IsLocalExternDecl) { |
7222 | if (D.isDecompositionDeclarator()) |
7223 | for (auto *B : Bindings) |
7224 | B->setLocalExternDecl(); |
7225 | else |
7226 | NewVD->setLocalExternDecl(); |
7227 | } |
7228 | |
7229 | bool EmitTLSUnsupportedError = false; |
7230 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
7231 | |
7232 | |
7233 | |
7234 | |
7235 | |
7236 | |
7237 | if (NewVD->hasLocalStorage() && |
7238 | (SCSpec != DeclSpec::SCS_unspecified || |
7239 | TSCS != DeclSpec::TSCS_thread_local || |
7240 | !DC->isFunctionOrMethod())) |
7241 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
7242 | diag::err_thread_non_global) |
7243 | << DeclSpec::getSpecifierName(TSCS); |
7244 | else if (!Context.getTargetInfo().isTLSSupported()) { |
7245 | if (getLangOpts().CUDA || getLangOpts().OpenMPIsDevice || |
7246 | getLangOpts().SYCLIsDevice) { |
7247 | |
7248 | |
7249 | |
7250 | EmitTLSUnsupportedError = true; |
7251 | |
7252 | |
7253 | |
7254 | NewVD->setTSCSpec(TSCS); |
7255 | } else |
7256 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
7257 | diag::err_thread_unsupported); |
7258 | } else |
7259 | NewVD->setTSCSpec(TSCS); |
7260 | } |
7261 | |
7262 | switch (D.getDeclSpec().getConstexprSpecifier()) { |
7263 | case ConstexprSpecKind::Unspecified: |
7264 | break; |
7265 | |
7266 | case ConstexprSpecKind::Consteval: |
7267 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
7268 | diag::err_constexpr_wrong_decl_kind) |
7269 | << static_cast<int>(D.getDeclSpec().getConstexprSpecifier()); |
7270 | LLVM_FALLTHROUGH; |
7271 | |
7272 | case ConstexprSpecKind::Constexpr: |
7273 | NewVD->setConstexpr(true); |
7274 | |
7275 | |
7276 | |
7277 | if (NewVD->isStaticDataMember() && |
7278 | (getLangOpts().CPlusPlus17 || |
7279 | Context.getTargetInfo().getCXXABI().isMicrosoft())) |
7280 | NewVD->setImplicitlyInline(); |
7281 | break; |
7282 | |
7283 | case ConstexprSpecKind::Constinit: |
7284 | if (!NewVD->hasGlobalStorage()) |
7285 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
7286 | diag::err_constinit_local_variable); |
7287 | else |
7288 | NewVD->addAttr(ConstInitAttr::Create( |
7289 | Context, D.getDeclSpec().getConstexprSpecLoc(), |
7290 | AttributeCommonInfo::AS_Keyword, ConstInitAttr::Keyword_constinit)); |
7291 | break; |
7292 | } |
7293 | |
7294 | |
7295 | |
7296 | |
7297 | |
7298 | |
7299 | |
7300 | |
7301 | |
7302 | |
7303 | if (SC == SC_Static && S->getFnParent() != nullptr && |
7304 | !NewVD->getType().isConstQualified()) { |
7305 | FunctionDecl *CurFD = getCurFunctionDecl(); |
7306 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { |
7307 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
7308 | diag::warn_static_local_in_extern_inline); |
7309 | MaybeSuggestAddingStaticToDecl(CurFD); |
7310 | } |
7311 | } |
7312 | |
7313 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
7314 | if (IsVariableTemplateSpecialization) |
7315 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
7316 | << (IsPartialSpecialization ? 1 : 0) |
7317 | << FixItHint::CreateRemoval( |
7318 | D.getDeclSpec().getModulePrivateSpecLoc()); |
7319 | else if (IsMemberSpecialization) |
7320 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
7321 | << 2 |
7322 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
7323 | else if (NewVD->hasLocalStorage()) |
7324 | Diag(NewVD->getLocation(), diag::err_module_private_local) |
7325 | << 0 << NewVD |
7326 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
7327 | << FixItHint::CreateRemoval( |
7328 | D.getDeclSpec().getModulePrivateSpecLoc()); |
7329 | else { |
7330 | NewVD->setModulePrivate(); |
7331 | if (NewTemplate) |
7332 | NewTemplate->setModulePrivate(); |
7333 | for (auto *B : Bindings) |
7334 | B->setModulePrivate(); |
7335 | } |
7336 | } |
7337 | |
7338 | if (getLangOpts().OpenCL) { |
7339 | deduceOpenCLAddressSpace(NewVD); |
7340 | |
7341 | DeclSpec::TSCS TSC = D.getDeclSpec().getThreadStorageClassSpec(); |
7342 | if (TSC != TSCS_unspecified) { |
7343 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
7344 | diag::err_opencl_unknown_type_specifier) |
7345 | << getLangOpts().getOpenCLVersionString() |
7346 | << DeclSpec::getSpecifierName(TSC) << 1; |
7347 | NewVD->setInvalidDecl(); |
7348 | } |
7349 | } |
7350 | |
7351 | |
7352 | ProcessDeclAttributes(S, NewVD, D); |
7353 | |
7354 | |
7355 | |
7356 | |
7357 | |
7358 | if (R->isFunctionPointerType()) |
7359 | if (const auto *TT = R->getAs<TypedefType>()) |
7360 | copyAttrFromTypedefToDecl<AllocSizeAttr>(*this, NewVD, TT); |
7361 | |
7362 | if (getLangOpts().CUDA || getLangOpts().OpenMPIsDevice || |
7363 | getLangOpts().SYCLIsDevice) { |
7364 | if (EmitTLSUnsupportedError && |
7365 | ((getLangOpts().CUDA && DeclAttrsMatchCUDAMode(getLangOpts(), NewVD)) || |
7366 | (getLangOpts().OpenMPIsDevice && |
7367 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(NewVD)))) |
7368 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
7369 | diag::err_thread_unsupported); |
7370 | |
7371 | if (EmitTLSUnsupportedError && |
7372 | (LangOpts.SYCLIsDevice || (LangOpts.OpenMP && LangOpts.OpenMPIsDevice))) |
7373 | targetDiag(D.getIdentifierLoc(), diag::err_thread_unsupported); |
7374 | |
7375 | |
7376 | if (SC == SC_None && S->getFnParent() != nullptr && |
7377 | (NewVD->hasAttr<CUDASharedAttr>() || |
7378 | NewVD->hasAttr<CUDAConstantAttr>())) { |
7379 | NewVD->setStorageClass(SC_Static); |
7380 | } |
7381 | } |
7382 | |
7383 | |
7384 | |
7385 | |
7386 | assert(!NewVD->hasAttr<DLLImportAttr>() || |
7387 | NewVD->getAttr<DLLImportAttr>()->isInherited() || |
7388 | NewVD->isStaticDataMember() || NewVD->getStorageClass() != SC_None); |
7389 | |
7390 | |
7391 | |
7392 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
7393 | NewVD->setInvalidDecl(); |
7394 | |
7395 | |
7396 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
7397 | |
7398 | StringLiteral *SE = cast<StringLiteral>(E); |
7399 | StringRef Label = SE->getString(); |
7400 | if (S->getFnParent() != nullptr) { |
7401 | switch (SC) { |
7402 | case SC_None: |
7403 | case SC_Auto: |
7404 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; |
7405 | break; |
7406 | case SC_Register: |
7407 | |
7408 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label) && |
7409 | DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) |
7410 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
7411 | break; |
7412 | case SC_Static: |
7413 | case SC_Extern: |
7414 | case SC_PrivateExtern: |
7415 | break; |
7416 | } |
7417 | } else if (SC == SC_Register) { |
7418 | |
7419 | if (DeclAttrsMatchCUDAMode(getLangOpts(), NewVD)) { |
7420 | const auto &TI = Context.getTargetInfo(); |
7421 | bool HasSizeMismatch; |
7422 | |
7423 | if (!TI.isValidGCCRegisterName(Label)) |
7424 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
7425 | else if (!TI.validateGlobalRegisterVariable(Label, |
7426 | Context.getTypeSize(R), |
7427 | HasSizeMismatch)) |
7428 | Diag(E->getExprLoc(), diag::err_asm_invalid_global_var_reg) << Label; |
7429 | else if (HasSizeMismatch) |
7430 | Diag(E->getExprLoc(), diag::err_asm_register_size_mismatch) << Label; |
7431 | } |
7432 | |
7433 | if (!R->isIntegralType(Context) && !R->isPointerType()) { |
7434 | Diag(D.getBeginLoc(), diag::err_asm_bad_register_type); |
7435 | NewVD->setInvalidDecl(true); |
7436 | } |
7437 | } |
7438 | |
7439 | NewVD->addAttr(AsmLabelAttr::Create(Context, Label, |
7440 | true, |
7441 | SE->getStrTokenLoc(0))); |
7442 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
7443 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
7444 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); |
7445 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
7446 | if (isDeclExternC(NewVD)) { |
7447 | NewVD->addAttr(I->second); |
7448 | ExtnameUndeclaredIdentifiers.erase(I); |
7449 | } else |
7450 | Diag(NewVD->getLocation(), diag::warn_redefine_extname_not_applied) |
7451 | << 1 << NewVD; |
7452 | } |
7453 | } |
7454 | |
7455 | |
7456 | NamedDecl *ShadowedDecl = D.getCXXScopeSpec().isEmpty() |
7457 | ? getShadowedDeclaration(NewVD, Previous) |
7458 | : nullptr; |
7459 | |
7460 | |
7461 | |
7462 | |
7463 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewVD), |
7464 | D.getCXXScopeSpec().isNotEmpty() || |
7465 | IsMemberSpecialization || |
7466 | IsVariableTemplateSpecialization); |
7467 | |
7468 | |
7469 | |
7470 | if (getLangOpts().CPlusPlus && |
7471 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) |
7472 | NewVD->setPreviousDeclInSameBlockScope( |
7473 | Previous.isSingleResult() && !Previous.isShadowed() && |
7474 | isDeclInScope(Previous.getFoundDecl(), OriginalDC, S, false)); |
7475 | |
7476 | if (!getLangOpts().CPlusPlus) { |
7477 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
7478 | } else { |
7479 | |
7480 | if (IsMemberSpecialization && !NewVD->isInvalidDecl() && |
7481 | CheckMemberSpecialization(NewVD, Previous)) |
7482 | NewVD->setInvalidDecl(); |
7483 | |
7484 | |
7485 | if (!Previous.empty()) { |
7486 | if (Previous.isSingleResult() && |
7487 | isa<FieldDecl>(Previous.getFoundDecl()) && |
7488 | D.getCXXScopeSpec().isSet()) { |
7489 | |
7490 | |
7491 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
7492 | << D.getCXXScopeSpec().getRange(); |
7493 | Previous.clear(); |
7494 | NewVD->setInvalidDecl(); |
7495 | } |
7496 | } else if (D.getCXXScopeSpec().isSet()) { |
7497 | |
7498 | Diag(D.getIdentifierLoc(), diag::err_no_member) |
7499 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) |
7500 | << D.getCXXScopeSpec().getRange(); |
7501 | NewVD->setInvalidDecl(); |
7502 | } |
7503 | |
7504 | if (!IsVariableTemplateSpecialization) |
7505 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
7506 | |
7507 | if (NewTemplate) { |
7508 | VarTemplateDecl *PrevVarTemplate = |
7509 | NewVD->getPreviousDecl() |
7510 | ? NewVD->getPreviousDecl()->getDescribedVarTemplate() |
7511 | : nullptr; |
7512 | |
7513 | |
7514 | |
7515 | |
7516 | if (CheckTemplateParameterList( |
7517 | TemplateParams, |
7518 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() |
7519 | : nullptr, |
7520 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && |
7521 | DC->isDependentContext()) |
7522 | ? TPC_ClassTemplateMember |
7523 | : TPC_VarTemplate)) |
7524 | NewVD->setInvalidDecl(); |
7525 | |
7526 | |
7527 | |
7528 | if (PrevVarTemplate && |
7529 | PrevVarTemplate->getInstantiatedFromMemberTemplate()) |
7530 | PrevVarTemplate->setMemberSpecialization(); |
7531 | } |
7532 | } |
7533 | |
7534 | |
7535 | if (ShadowedDecl && !D.isRedeclaration()) |
7536 | CheckShadow(NewVD, ShadowedDecl, Previous); |
7537 | |
7538 | ProcessPragmaWeak(S, NewVD); |
7539 | |
7540 | |
7541 | |
7542 | if (NewVD->isFirstDecl() && !NewVD->isInvalidDecl() && |
7543 | isIncompleteDeclExternC(*this, NewVD)) |
7544 | RegisterLocallyScopedExternCDecl(NewVD, S); |
7545 | |
7546 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
7547 | MangleNumberingContext *MCtx; |
7548 | Decl *ManglingContextDecl; |
7549 | std::tie(MCtx, ManglingContextDecl) = |
7550 | getCurrentMangleNumberContext(NewVD->getDeclContext()); |
7551 | if (MCtx) { |
7552 | Context.setManglingNumber( |
7553 | NewVD, MCtx->getManglingNumber( |
7554 | NewVD, getMSManglingNumber(getLangOpts(), S))); |
7555 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
7556 | } |
7557 | } |
7558 | |
7559 | |
7560 | if (Name.getAsIdentifierInfo() && Name.getAsIdentifierInfo()->isStr("main") && |
7561 | NewVD->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
7562 | !getLangOpts().Freestanding && !NewVD->getDescribedVarTemplate()) { |
7563 | |
7564 | |
7565 | |
7566 | if (getLangOpts().CPlusPlus) |
7567 | Diag(D.getBeginLoc(), diag::err_main_global_variable); |
7568 | |
7569 | |
7570 | |
7571 | else if (NewVD->hasExternalFormalLinkage()) |
7572 | Diag(D.getBeginLoc(), diag::warn_main_redefined); |
7573 | } |
7574 | |
7575 | if (D.isRedeclaration() && !Previous.empty()) { |
7576 | NamedDecl *Prev = Previous.getRepresentativeDecl(); |
7577 | checkDLLAttributeRedeclaration(*this, Prev, NewVD, IsMemberSpecialization, |
7578 | D.isFunctionDefinition()); |
7579 | } |
7580 | |
7581 | if (NewTemplate) { |
7582 | if (NewVD->isInvalidDecl()) |
7583 | NewTemplate->setInvalidDecl(); |
7584 | ActOnDocumentableDecl(NewTemplate); |
7585 | return NewTemplate; |
7586 | } |
7587 | |
7588 | if (IsMemberSpecialization && !NewVD->isInvalidDecl()) |
7589 | CompleteMemberSpecialization(NewVD, Previous); |
7590 | |
7591 | return NewVD; |
7592 | } |
7593 | |
7594 | |
7595 | enum ShadowedDeclKind { |
7596 | SDK_Local, |
7597 | SDK_Global, |
7598 | SDK_StaticMember, |
7599 | SDK_Field, |
7600 | SDK_Typedef, |
7601 | SDK_Using, |
7602 | SDK_StructuredBinding |
7603 | }; |
7604 | |
7605 | |
7606 | static ShadowedDeclKind computeShadowedDeclKind(const NamedDecl *ShadowedDecl, |
7607 | const DeclContext *OldDC) { |
7608 | if (isa<TypeAliasDecl>(ShadowedDecl)) |
7609 | return SDK_Using; |
7610 | else if (isa<TypedefDecl>(ShadowedDecl)) |
7611 | return SDK_Typedef; |
7612 | else if (isa<BindingDecl>(ShadowedDecl)) |
7613 | return SDK_StructuredBinding; |
7614 | else if (isa<RecordDecl>(OldDC)) |
7615 | return isa<FieldDecl>(ShadowedDecl) ? SDK_Field : SDK_StaticMember; |
7616 | |
7617 | return OldDC->isFileContext() ? SDK_Global : SDK_Local; |
7618 | } |
7619 | |
7620 | |
7621 | |
7622 | static SourceLocation getCaptureLocation(const LambdaScopeInfo *LSI, |
7623 | const VarDecl *VD) { |
7624 | for (const Capture &Capture : LSI->Captures) { |
7625 | if (Capture.isVariableCapture() && Capture.getVariable() == VD) |
7626 | return Capture.getLocation(); |
7627 | } |
7628 | return SourceLocation(); |
7629 | } |
7630 | |
7631 | static bool shouldWarnIfShadowedDecl(const DiagnosticsEngine &Diags, |
7632 | const LookupResult &R) { |
7633 | |
7634 | if (R.getResultKind() != LookupResult::Found) |
7635 | return false; |
7636 | |
7637 | |
7638 | return !Diags.isIgnored(diag::warn_decl_shadow, R.getNameLoc()); |
7639 | } |
7640 | |
7641 | |
7642 | |
7643 | NamedDecl *Sema::getShadowedDeclaration(const VarDecl *D, |
7644 | const LookupResult &R) { |
7645 | if (!shouldWarnIfShadowedDecl(Diags, R)) |
7646 | return nullptr; |
7647 | |
7648 | |
7649 | if (D->hasGlobalStorage()) |
7650 | return nullptr; |
7651 | |
7652 | NamedDecl *ShadowedDecl = R.getFoundDecl(); |
7653 | return isa<VarDecl, FieldDecl, BindingDecl>(ShadowedDecl) ? ShadowedDecl |
7654 | : nullptr; |
7655 | } |
7656 | |
7657 | |
7658 | |
7659 | NamedDecl *Sema::getShadowedDeclaration(const TypedefNameDecl *D, |
7660 | const LookupResult &R) { |
7661 | |
7662 | if (D->getDeclContext()->isRecord()) |
7663 | return nullptr; |
7664 | |
7665 | if (!shouldWarnIfShadowedDecl(Diags, R)) |
7666 | return nullptr; |
7667 | |
7668 | NamedDecl *ShadowedDecl = R.getFoundDecl(); |
7669 | return isa<TypedefNameDecl>(ShadowedDecl) ? ShadowedDecl : nullptr; |
7670 | } |
7671 | |
7672 | |
7673 | |
7674 | NamedDecl *Sema::getShadowedDeclaration(const BindingDecl *D, |
7675 | const LookupResult &R) { |
7676 | if (!shouldWarnIfShadowedDecl(Diags, R)) |
7677 | return nullptr; |
7678 | |
7679 | NamedDecl *ShadowedDecl = R.getFoundDecl(); |
7680 | return isa<VarDecl, FieldDecl, BindingDecl>(ShadowedDecl) ? ShadowedDecl |
7681 | : nullptr; |
7682 | } |
7683 | |
7684 | |
7685 | |
7686 | |
7687 | |
7688 | |
7689 | |
7690 | |
7691 | |
7692 | |
7693 | void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl, |
7694 | const LookupResult &R) { |
7695 | DeclContext *NewDC = D->getDeclContext(); |
7696 | |
7697 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) { |
7698 | |
7699 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) |
7700 | if (MD->isStatic()) |
7701 | return; |
7702 | |
7703 | |
7704 | |
7705 | if (isa<CXXConstructorDecl>(NewDC)) |
7706 | if (const auto PVD = dyn_cast<ParmVarDecl>(D)) { |
7707 | |
7708 | |
7709 | ShadowingDecls.insert({PVD->getCanonicalDecl(), FD}); |
7710 | return; |
7711 | } |
7712 | } |
7713 | |
7714 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
7715 | if (shadowedVar->isExternC()) { |
7716 | |
7717 | |
7718 | for (auto I : shadowedVar->redecls()) |
7719 | if (I->isFileVarDecl()) { |
7720 | ShadowedDecl = I; |
7721 | break; |
7722 | } |
7723 | } |
7724 | |
7725 | DeclContext *OldDC = ShadowedDecl->getDeclContext()->getRedeclContext(); |
7726 | |
7727 | unsigned WarningDiag = diag::warn_decl_shadow; |
7728 | SourceLocation CaptureLoc; |
7729 | if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC && |
7730 | isa<CXXMethodDecl>(NewDC)) { |
7731 | if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) { |
7732 | if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) { |
7733 | if (RD->getLambdaCaptureDefault() == LCD_None) { |
7734 | |
7735 | const auto *LSI = cast<LambdaScopeInfo>(getCurFunction()); |
7736 | |
7737 | CaptureLoc = getCaptureLocation(LSI, cast<VarDecl>(ShadowedDecl)); |
7738 | if (CaptureLoc.isInvalid()) |
7739 | WarningDiag = diag::warn_decl_shadow_uncaptured_local; |
7740 | } else { |
7741 | |
7742 | |
7743 | cast<LambdaScopeInfo>(getCurFunction()) |
7744 | ->ShadowingDecls.push_back( |
7745 | {cast<VarDecl>(D), cast<VarDecl>(ShadowedDecl)}); |
7746 | return; |
7747 | } |
7748 | } |
7749 | |
7750 | if (cast<VarDecl>(ShadowedDecl)->hasLocalStorage()) { |
7751 | |
7752 | |
7753 | for (DeclContext *ParentDC = NewDC; |
7754 | ParentDC && !ParentDC->Equals(OldDC); |
7755 | ParentDC = getLambdaAwareParentOfDeclContext(ParentDC)) { |
7756 | |
7757 | |
7758 | if (!isa<BlockDecl>(ParentDC) && !isa<CapturedDecl>(ParentDC) && |
7759 | !isLambdaCallOperator(ParentDC)) { |
7760 | return; |
7761 | } |
7762 | } |
7763 | } |
7764 | } |
7765 | } |
7766 | |
7767 | |
7768 | if (NewDC && NewDC->isRecord()) { |
7769 | |
7770 | if (!OldDC->isRecord()) |
7771 | return; |
7772 | |
7773 | |
7774 | |
7775 | |
7776 | |
7777 | |
7778 | |
7779 | } |
7780 | |
7781 | |
7782 | DeclarationName Name = R.getLookupName(); |
7783 | |
7784 | |
7785 | if (getSourceManager().isInSystemMacro(R.getNameLoc())) |
7786 | return; |
7787 | ShadowedDeclKind Kind = computeShadowedDeclKind(ShadowedDecl, OldDC); |
7788 | Diag(R.getNameLoc(), WarningDiag) << Name << Kind << OldDC; |
7789 | if (!CaptureLoc.isInvalid()) |
7790 | Diag(CaptureLoc, diag::note_var_explicitly_captured_here) |
7791 | << Name << 1; |
7792 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
7793 | } |
7794 | |
7795 | |
7796 | |
7797 | void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) { |
7798 | for (const auto &Shadow : LSI->ShadowingDecls) { |
7799 | const VarDecl *ShadowedDecl = Shadow.ShadowedDecl; |
7800 | |
7801 | SourceLocation CaptureLoc = getCaptureLocation(LSI, ShadowedDecl); |
7802 | const DeclContext *OldDC = ShadowedDecl->getDeclContext(); |
7803 | Diag(Shadow.VD->getLocation(), CaptureLoc.isInvalid() |
7804 | ? diag::warn_decl_shadow_uncaptured_local |
7805 | : diag::warn_decl_shadow) |
7806 | << Shadow.VD->getDeclName() |
7807 | << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC; |
7808 | if (!CaptureLoc.isInvalid()) |
7809 | Diag(CaptureLoc, diag::note_var_explicitly_captured_here) |
7810 | << Shadow.VD->getDeclName() << 0; |
7811 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
7812 | } |
7813 | } |
7814 | |
7815 | |
7816 | void Sema::CheckShadow(Scope *S, VarDecl *D) { |
7817 | if (Diags.isIgnored(diag::warn_decl_shadow, D->getLocation())) |
7818 | return; |
7819 | |
7820 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
7821 | Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
7822 | LookupName(R, S); |
7823 | if (NamedDecl *ShadowedDecl = getShadowedDeclaration(D, R)) |
7824 | CheckShadow(D, ShadowedDecl, R); |
7825 | } |
7826 | |
7827 | |
7828 | |
7829 | void Sema::CheckShadowingDeclModification(Expr *E, SourceLocation Loc) { |
7830 | |
7831 | if (!getLangOpts().CPlusPlus || ShadowingDecls.empty()) |
7832 | return; |
7833 | E = E->IgnoreParenImpCasts(); |
7834 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
7835 | if (!DRE) |
7836 | return; |
7837 | const NamedDecl *D = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); |
7838 | auto I = ShadowingDecls.find(D); |
7839 | if (I == ShadowingDecls.end()) |
7840 | return; |
7841 | const NamedDecl *ShadowedDecl = I->second; |
7842 | const DeclContext *OldDC = ShadowedDecl->getDeclContext(); |
7843 | Diag(Loc, diag::warn_modifying_shadowing_decl) << D << OldDC; |
7844 | Diag(D->getLocation(), diag::note_var_declared_here) << D; |
7845 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
7846 | |
7847 | |
7848 | ShadowingDecls.erase(I); |
7849 | } |
7850 | |
7851 | |
7852 | |
7853 | template<typename T> |
7854 | static bool checkGlobalOrExternCConflict( |
7855 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { |
7856 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); |
7857 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); |
7858 | |
7859 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
7860 | |
7861 | |
7862 | return false; |
7863 | } |
7864 | |
7865 | if (Prev) { |
7866 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { |
7867 | |
7868 | |
7869 | Previous.clear(); |
7870 | Previous.addDecl(Prev); |
7871 | return true; |
7872 | } |
7873 | |
7874 | |
7875 | |
7876 | |
7877 | if (!isa<VarDecl>(ND)) |
7878 | return false; |
7879 | } else { |
7880 | |
7881 | |
7882 | if (IsGlobal) { |
7883 | |
7884 | IsGlobal = false; |
7885 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
7886 | I != E; ++I) { |
7887 | if (isa<VarDecl>(*I)) { |
7888 | Prev = *I; |
7889 | break; |
7890 | } |
7891 | } |
7892 | } else { |
7893 | DeclContext::lookup_result R = |
7894 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); |
7895 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); |
7896 | I != E; ++I) { |
7897 | if (isa<VarDecl>(*I)) { |
7898 | Prev = *I; |
7899 | break; |
7900 | } |
7901 | |
7902 | |
7903 | |
7904 | |
7905 | |
7906 | } |
7907 | } |
7908 | |
7909 | if (!Prev) |
7910 | return false; |
7911 | } |
7912 | |
7913 | |
7914 | |
7915 | assert(Prev && "should have found a previous declaration to diagnose"); |
7916 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) |
7917 | Prev = FD->getFirstDecl(); |
7918 | else |
7919 | Prev = cast<VarDecl>(Prev)->getFirstDecl(); |
7920 | |
7921 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) |
7922 | << IsGlobal << ND; |
7923 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) |
7924 | << IsGlobal; |
7925 | return false; |
7926 | } |
7927 | |
7928 | |
7929 | |
7930 | |
7931 | |
7932 | |
7933 | |
7934 | |
7935 | |
7936 | template<typename T> |
7937 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, |
7938 | LookupResult &Previous) { |
7939 | if (!S.getLangOpts().CPlusPlus) { |
7940 | |
7941 | |
7942 | |
7943 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
7944 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { |
7945 | Previous.clear(); |
7946 | Previous.addDecl(Prev); |
7947 | return true; |
7948 | } |
7949 | } |
7950 | return false; |
7951 | } |
7952 | |
7953 | |
7954 | |
7955 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) |
7956 | return checkGlobalOrExternCConflict(S, ND, true, Previous); |
7957 | |
7958 | |
7959 | |
7960 | |
7961 | if (isIncompleteDeclExternC(S,ND)) |
7962 | return checkGlobalOrExternCConflict(S, ND, false, Previous); |
7963 | |
7964 | |
7965 | return false; |
7966 | } |
7967 | |
7968 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
7969 | |
7970 | if (NewVD->isInvalidDecl()) |
7971 | return; |
7972 | |
7973 | QualType T = NewVD->getType(); |
7974 | |
7975 | |
7976 | if (T->isUndeducedType()) |
7977 | return; |
7978 | |
7979 | if (NewVD->hasAttrs()) |
7980 | CheckAlignasUnderalignment(NewVD); |
7981 | |
7982 | if (T->isObjCObjectType()) { |
7983 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
7984 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); |
7985 | T = Context.getObjCObjectPointerType(T); |
7986 | NewVD->setType(T); |
7987 | } |
7988 | |
7989 | |
7990 | |
7991 | |
7992 | |
7993 | if (!getLangOpts().OpenCL && NewVD->hasLocalStorage() && |
7994 | T.getAddressSpace() != LangAS::Default) { |
7995 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl) << 0; |
7996 | NewVD->setInvalidDecl(); |
7997 | return; |
7998 | } |
7999 | |
8000 | |
8001 | |
8002 | if (getLangOpts().OpenCLVersion == 120 && |
8003 | !getOpenCLOptions().isAvailableOption("cl_clang_storage_class_specifiers", |
8004 | getLangOpts()) && |
8005 | NewVD->isStaticLocal()) { |
8006 | Diag(NewVD->getLocation(), diag::err_static_function_scope); |
8007 | NewVD->setInvalidDecl(); |
8008 | return; |
8009 | } |
8010 | |
8011 | if (getLangOpts().OpenCL) { |
8012 | if (!diagnoseOpenCLTypes(*this, NewVD)) |
8013 | return; |
8014 | |
8015 | |
8016 | if (NewVD->hasAttr<BlocksAttr>()) { |
8017 | Diag(NewVD->getLocation(), diag::err_opencl_block_storage_type); |
8018 | return; |
8019 | } |
8020 | |
8021 | if (T->isBlockPointerType()) { |
8022 | |
8023 | |
8024 | if (!T.isConstQualified()) { |
8025 | Diag(NewVD->getLocation(), diag::err_opencl_invalid_block_declaration) |
8026 | << 0 ; |
8027 | NewVD->setInvalidDecl(); |
8028 | return; |
8029 | } |
8030 | if (NewVD->hasExternalStorage()) { |
8031 | Diag(NewVD->getLocation(), diag::err_opencl_extern_block_declaration); |
8032 | NewVD->setInvalidDecl(); |
8033 | return; |
8034 | } |
8035 | } |
8036 | |
8037 | |
8038 | if (NewVD->isFileVarDecl() || NewVD->isStaticLocal() || |
8039 | NewVD->hasExternalStorage()) { |
8040 | if (!T->isSamplerT() && !T->isDependentType() && |
8041 | !(T.getAddressSpace() == LangAS::opencl_constant || |
8042 | (T.getAddressSpace() == LangAS::opencl_global && |
8043 | getOpenCLOptions().areProgramScopeVariablesSupported( |
8044 | getLangOpts())))) { |
8045 | int Scope = NewVD->isStaticLocal() | NewVD->hasExternalStorage() << 1; |
8046 | if (getOpenCLOptions().areProgramScopeVariablesSupported(getLangOpts())) |
8047 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) |
8048 | << Scope << "global or constant"; |
8049 | else |
8050 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) |
8051 | << Scope << "constant"; |
8052 | NewVD->setInvalidDecl(); |
8053 | return; |
8054 | } |
8055 | } else { |
8056 | if (T.getAddressSpace() == LangAS::opencl_global) { |
8057 | Diag(NewVD->getLocation(), diag::err_opencl_function_variable) |
8058 | << 1 << "global"; |
8059 | NewVD->setInvalidDecl(); |
8060 | return; |
8061 | } |
8062 | if (T.getAddressSpace() == LangAS::opencl_constant || |
8063 | T.getAddressSpace() == LangAS::opencl_local) { |
8064 | FunctionDecl *FD = getCurFunctionDecl(); |
8065 | |
8066 | |
8067 | if (FD && !FD->hasAttr<OpenCLKernelAttr>()) { |
8068 | if (T.getAddressSpace() == LangAS::opencl_constant) |
8069 | Diag(NewVD->getLocation(), diag::err_opencl_function_variable) |
8070 | << 0 << "constant"; |
8071 | else |
8072 | Diag(NewVD->getLocation(), diag::err_opencl_function_variable) |
8073 | << 0 << "local"; |
8074 | NewVD->setInvalidDecl(); |
8075 | return; |
8076 | } |
8077 | |
8078 | |
8079 | if (FD && FD->hasAttr<OpenCLKernelAttr>()) { |
8080 | if (!getCurScope()->isFunctionScope()) { |
8081 | if (T.getAddressSpace() == LangAS::opencl_constant) |
8082 | Diag(NewVD->getLocation(), diag::err_opencl_addrspace_scope) |
8083 | << "constant"; |
8084 | else |
8085 | Diag(NewVD->getLocation(), diag::err_opencl_addrspace_scope) |
8086 | << "local"; |
8087 | NewVD->setInvalidDecl(); |
8088 | return; |
8089 | } |
8090 | } |
8091 | } else if (T.getAddressSpace() != LangAS::opencl_private && |
8092 | |
8093 | |
8094 | T.getAddressSpace() != LangAS::Default) { |
8095 | |
8096 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl) << 1; |
8097 | NewVD->setInvalidDecl(); |
8098 | return; |
8099 | } |
8100 | } |
8101 | } |
8102 | |
8103 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
8104 | && !NewVD->hasAttr<BlocksAttr>()) { |
8105 | if (getLangOpts().getGC() != LangOptions::NonGC) |
8106 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
8107 | else { |
8108 | assert(!getLangOpts().ObjCAutoRefCount); |
8109 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
8110 | } |
8111 | } |
8112 | |
8113 | bool isVM = T->isVariablyModifiedType(); |
8114 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
8115 | NewVD->hasAttr<BlocksAttr>()) |
8116 | setFunctionHasBranchProtectedScope(); |
8117 | |
8118 | if ((isVM && NewVD->hasLinkage()) || |
8119 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { |
8120 | bool SizeIsNegative; |
8121 | llvm::APSInt Oversized; |
8122 | TypeSourceInfo *FixedTInfo = TryToFixInvalidVariablyModifiedTypeSourceInfo( |
8123 | NewVD->getTypeSourceInfo(), Context, SizeIsNegative, Oversized); |
8124 | QualType FixedT; |
8125 | if (FixedTInfo && T == NewVD->getTypeSourceInfo()->getType()) |
8126 | FixedT = FixedTInfo->getType(); |
8127 | else if (FixedTInfo) { |
8128 | |
8129 | |
8130 | FixedT = TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative, |
8131 | Oversized); |
8132 | } |
8133 | if ((!FixedTInfo || FixedT.isNull()) && T->isVariableArrayType()) { |
8134 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
8135 | |
8136 | |
8137 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
8138 | |
8139 | if (NewVD->isFileVarDecl()) |
8140 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) |
8141 | << SizeRange; |
8142 | else if (NewVD->isStaticLocal()) |
8143 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
8144 | << SizeRange; |
8145 | else |
8146 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) |
8147 | << SizeRange; |
8148 | NewVD->setInvalidDecl(); |
8149 | return; |
8150 | } |
8151 | |
8152 | if (!FixedTInfo) { |
8153 | if (NewVD->isFileVarDecl()) |
8154 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); |
8155 | else |
8156 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); |
8157 | NewVD->setInvalidDecl(); |
8158 | return; |
8159 | } |
8160 | |
8161 | Diag(NewVD->getLocation(), diag::ext_vla_folded_to_constant); |
8162 | NewVD->setType(FixedT); |
8163 | NewVD->setTypeSourceInfo(FixedTInfo); |
8164 | } |
8165 | |
8166 | if (T->isVoidType()) { |
8167 | |
8168 | |
8169 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { |
8170 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) |
8171 | << T; |
8172 | NewVD->setInvalidDecl(); |
8173 | return; |
8174 | } |
8175 | } |
8176 | |
8177 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { |
8178 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); |
8179 | NewVD->setInvalidDecl(); |
8180 | return; |
8181 | } |
8182 | |
8183 | if (!NewVD->hasLocalStorage() && T->isSizelessType()) { |
8184 | Diag(NewVD->getLocation(), diag::err_sizeless_nonlocal) << T; |
8185 | NewVD->setInvalidDecl(); |
8186 | return; |
8187 | } |
8188 | |
8189 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { |
8190 | Diag(NewVD->getLocation(), diag::err_block_on_vm); |
8191 | NewVD->setInvalidDecl(); |
8192 | return; |
8193 | } |
8194 | |
8195 | if (NewVD->isConstexpr() && !T->isDependentType() && |
8196 | RequireLiteralType(NewVD->getLocation(), T, |
8197 | diag::err_constexpr_var_non_literal)) { |
8198 | NewVD->setInvalidDecl(); |
8199 | return; |
8200 | } |
8201 | |
8202 | |
8203 | if (Context.getTargetInfo().getTriple().isPPC64() && |
8204 | !NewVD->isLocalVarDecl() && |
8205 | CheckPPCMMAType(T, NewVD->getLocation())) { |
8206 | NewVD->setInvalidDecl(); |
8207 | return; |
8208 | } |
8209 | } |
8210 | |
8211 | |
8212 | |
8213 | |
8214 | |
8215 | |
8216 | |
8217 | |
8218 | |
8219 | |
8220 | |
8221 | |
8222 | |
8223 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
8224 | CheckVariableDeclarationType(NewVD); |
8225 | |
8226 | |
8227 | if (NewVD->isInvalidDecl()) |
8228 | return false; |
8229 | |
8230 | |
8231 | |
8232 | if (Previous.empty() && |
8233 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) |
8234 | Previous.setShadowed(); |
8235 | |
8236 | if (!Previous.empty()) { |
8237 | MergeVarDecl(NewVD, Previous); |
8238 | return true; |
8239 | } |
8240 | return false; |
8241 | } |
8242 | |
8243 | |
8244 | |
8245 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
8246 | llvm::SmallPtrSet<const CXXMethodDecl*, 4> Overridden; |
8247 | |
8248 | |
8249 | CXXBasePaths Paths(true, false, |
8250 | false); |
8251 | auto VisitBase = [&] (const CXXBaseSpecifier *Specifier, CXXBasePath &Path) { |
8252 | CXXRecordDecl *BaseRecord = Specifier->getType()->getAsCXXRecordDecl(); |
8253 | DeclarationName Name = MD->getDeclName(); |
8254 | |
8255 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
8256 | |
8257 | QualType T = Context.getTypeDeclType(BaseRecord); |
8258 | CanQualType CT = Context.getCanonicalType(T); |
8259 | Name = Context.DeclarationNames.getCXXDestructorName(CT); |
8260 | } |
8261 | |
8262 | for (NamedDecl *BaseND : BaseRecord->lookup(Name)) { |
8263 | CXXMethodDecl *BaseMD = |
8264 | dyn_cast<CXXMethodDecl>(BaseND->getCanonicalDecl()); |
8265 | if (!BaseMD || !BaseMD->isVirtual() || |
8266 | IsOverload(MD, BaseMD, false, |
8267 | true, |
8268 | |
8269 | |
8270 | false)) |
8271 | continue; |
8272 | |
8273 | if (Overridden.insert(BaseMD).second) { |
8274 | MD->addOverriddenMethod(BaseMD); |
8275 | CheckOverridingFunctionReturnType(MD, BaseMD); |
8276 | CheckOverridingFunctionAttributes(MD, BaseMD); |
8277 | CheckOverridingFunctionExceptionSpec(MD, BaseMD); |
8278 | CheckIfOverriddenFunctionIsMarkedFinal(MD, BaseMD); |
8279 | } |
8280 | |
8281 | |
8282 | |
8283 | return true; |
8284 | } |
8285 | |
8286 | return false; |
8287 | }; |
8288 | |
8289 | DC->lookupInBases(VisitBase, Paths); |
8290 | return !Overridden.empty(); |
8291 | } |
8292 | |
8293 | namespace { |
8294 | |
8295 | |
8296 | struct ActOnFDArgs { |
8297 | Scope *S; |
8298 | Declarator &D; |
8299 | MultiTemplateParamsArg TemplateParamLists; |
8300 | bool AddToScope; |
8301 | }; |
8302 | } |
8303 | |
8304 | namespace { |
8305 | |
8306 | |
8307 | |
8308 | class DifferentNameValidatorCCC final : public CorrectionCandidateCallback { |
8309 | public: |
8310 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
8311 | CXXRecordDecl *Parent) |
8312 | : Context(Context), OriginalFD(TypoFD), |
8313 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : nullptr) {} |
8314 | |
8315 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
8316 | if (candidate.getEditDistance() == 0) |
8317 | return false; |
8318 | |
8319 | SmallVector<unsigned, 1> MismatchedParams; |
8320 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
8321 | CDeclEnd = candidate.end(); |
8322 | CDecl != CDeclEnd; ++CDecl) { |
8323 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
8324 | |
8325 | if (FD && !FD->hasBody() && |
8326 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { |
8327 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
8328 | CXXRecordDecl *Parent = MD->getParent(); |
8329 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) |
8330 | return true; |
8331 | } else if (!ExpectedParent) { |
8332 | return true; |
8333 | } |
8334 | } |
8335 | } |
8336 | |
8337 | return false; |
8338 | } |
8339 | |
8340 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
8341 | return std::make_unique<DifferentNameValidatorCCC>(*this); |
8342 | } |
8343 | |
8344 | private: |
8345 | ASTContext &Context; |
8346 | FunctionDecl *OriginalFD; |
8347 | CXXRecordDecl *ExpectedParent; |
8348 | }; |
8349 | |
8350 | } |
8351 | |
8352 | void Sema::MarkTypoCorrectedFunctionDefinition(const NamedDecl *F) { |
8353 | TypoCorrectedFunctionDefinitions.insert(F); |
8354 | } |
8355 | |
8356 | |
8357 | |
8358 | |
8359 | |
8360 | |
8361 | |
8362 | |
8363 | |
8364 | |
8365 | static NamedDecl *DiagnoseInvalidRedeclaration( |
8366 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
8367 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
8368 | DeclarationName Name = NewFD->getDeclName(); |
8369 | DeclContext *NewDC = NewFD->getDeclContext(); |
8370 | SmallVector<unsigned, 1> MismatchedParams; |
8371 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; |
8372 | TypoCorrection Correction; |
8373 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
8374 | unsigned DiagMsg = |
8375 | IsLocalFriend ? diag::err_no_matching_local_friend : |
8376 | NewFD->getFriendObjectKind() ? diag::err_qualified_friend_no_match : |
8377 | diag::err_member_decl_does_not_match; |
8378 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), |
8379 | IsLocalFriend ? Sema::LookupLocalFriendName |
8380 | : Sema::LookupOrdinaryName, |
8381 | Sema::ForVisibleRedeclaration); |
8382 | |
8383 | NewFD->setInvalidDecl(); |
8384 | if (IsLocalFriend) |
8385 | SemaRef.LookupName(Prev, S); |
8386 | else |
8387 | SemaRef.LookupQualifiedName(Prev, NewDC); |
8388 | assert(!Prev.isAmbiguous() && |
8389 | "Cannot have an ambiguity in previous-declaration lookup"); |
8390 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
8391 | DifferentNameValidatorCCC CCC(SemaRef.Context, NewFD, |
8392 | MD ? MD->getParent() : nullptr); |
8393 | if (!Prev.empty()) { |
8394 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); |
8395 | Func != FuncEnd; ++Func) { |
8396 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); |
8397 | if (FD && |
8398 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
8399 | |
8400 | |
8401 | unsigned ParamNum = |
8402 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; |
8403 | NearMatches.push_back(std::make_pair(FD, ParamNum)); |
8404 | } |
8405 | } |
8406 | |
8407 | } else if ((Correction = SemaRef.CorrectTypo( |
8408 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
8409 | &ExtraArgs.D.getCXXScopeSpec(), CCC, Sema::CTK_ErrorRecovery, |
8410 | IsLocalFriend ? nullptr : NewDC))) { |
8411 | |
8412 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), |
8413 | ExtraArgs.D.getIdentifierLoc()); |
8414 | Previous.clear(); |
8415 | Previous.setLookupName(Correction.getCorrection()); |
8416 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
8417 | CDeclEnd = Correction.end(); |
8418 | CDecl != CDeclEnd; ++CDecl) { |
8419 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
8420 | if (FD && !FD->hasBody() && |
8421 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
8422 | Previous.addDecl(FD); |
8423 | } |
8424 | } |
8425 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
8426 | |
8427 | NamedDecl *Result; |
8428 | |
8429 | |
8430 | { |
8431 | |
8432 | Sema::SFINAETrap Trap(SemaRef); |
8433 | |
8434 | |
8435 | |
8436 | |
8437 | Result = SemaRef.ActOnFunctionDeclarator( |
8438 | ExtraArgs.S, ExtraArgs.D, |
8439 | Correction.getCorrectionDecl()->getDeclContext(), |
8440 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, |
8441 | ExtraArgs.AddToScope); |
8442 | |
8443 | if (Trap.hasErrorOccurred()) |
8444 | Result = nullptr; |
8445 | } |
8446 | |
8447 | if (Result) { |
8448 | |
8449 | Decl *Canonical = Result->getCanonicalDecl(); |
8450 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
8451 | I != E; ++I) |
8452 | if ((*I)->getCanonicalDecl() == Canonical) |
8453 | Correction.setCorrectionDecl(*I); |
8454 | |
8455 | |
8456 | SemaRef.MarkTypoCorrectedFunctionDefinition(Result); |
8457 | SemaRef.diagnoseTypo( |
8458 | Correction, |
8459 | SemaRef.PDiag(IsLocalFriend |
8460 | ? diag::err_no_matching_local_friend_suggest |
8461 | : diag::err_member_decl_does_not_match_suggest) |
8462 | << Name << NewDC << IsDefinition); |
8463 | return Result; |
8464 | } |
8465 | |
8466 | |
8467 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), |
8468 | ExtraArgs.D.getIdentifierLoc()); |
8469 | ExtraArgs.D.setRedeclaration(wasRedeclaration); |
8470 | Previous.clear(); |
8471 | Previous.setLookupName(Name); |
8472 | } |
8473 | |
8474 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
8475 | << Name << NewDC << IsDefinition << NewFD->getLocation(); |
8476 | |
8477 | bool NewFDisConst = false; |
8478 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) |
8479 | NewFDisConst = NewMD->isConst(); |
8480 | |
8481 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
8482 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
8483 | NearMatch != NearMatchEnd; ++NearMatch) { |
8484 | FunctionDecl *FD = NearMatch->first; |
8485 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
8486 | bool FDisConst = MD && MD->isConst(); |
8487 | bool IsMember = MD || !IsLocalFriend; |
8488 | |
8489 | |
8490 | if (unsigned Idx = NearMatch->second) { |
8491 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); |
8492 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
8493 | if (Loc.isInvalid()) Loc = FD->getLocation(); |
8494 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
8495 | : diag::note_local_decl_close_param_match) |
8496 | << Idx << FDParam->getType() |
8497 | << NewFD->getParamDecl(Idx - 1)->getType(); |
8498 | } else if (FDisConst != NewFDisConst) { |
8499 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
8500 | << NewFDisConst << FD->getSourceRange().getEnd(); |
8501 | } else |
8502 | SemaRef.Diag(FD->getLocation(), |
8503 | IsMember ? diag::note_member_def_close_match |
8504 | : diag::note_local_decl_close_match); |
8505 | } |
8506 | return nullptr; |
8507 | } |
8508 | |
8509 | static StorageClass getFunctionStorageClass(Sema &SemaRef, Declarator &D) { |
8510 | switch (D.getDeclSpec().getStorageClassSpec()) { |
8511 | default: llvm_unreachable("Unknown storage class!"); |
8512 | case DeclSpec::SCS_auto: |
8513 | case DeclSpec::SCS_register: |
8514 | case DeclSpec::SCS_mutable: |
8515 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
8516 | diag::err_typecheck_sclass_func); |
8517 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
8518 | D.setInvalidType(); |
8519 | break; |
8520 | case DeclSpec::SCS_unspecified: break; |
8521 | case DeclSpec::SCS_extern: |
8522 | if (D.getDeclSpec().isExternInLinkageSpec()) |
8523 | return SC_None; |
8524 | return SC_Extern; |
8525 | case DeclSpec::SCS_static: { |
8526 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
8527 | |
8528 | |
8529 | |
8530 | |
8531 | |
8532 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
8533 | diag::err_static_block_func); |
8534 | break; |
8535 | } else |
8536 | return SC_Static; |
8537 | } |
8538 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
8539 | } |
8540 | |
8541 | |
8542 | return SC_None; |
8543 | } |
8544 | |
8545 | static FunctionDecl *CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, |
8546 | DeclContext *DC, QualType &R, |
8547 | TypeSourceInfo *TInfo, |
8548 | StorageClass SC, |
8549 | bool &IsVirtualOkay) { |
8550 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); |
8551 | DeclarationName Name = NameInfo.getName(); |
8552 | |
8553 | FunctionDecl *NewFD = nullptr; |
8554 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
8555 | |
8556 | if (!SemaRef.getLangOpts().CPlusPlus) { |
8557 | |
8558 | |
8559 | |
8560 | |
8561 | |
8562 | |
8563 | bool HasPrototype = |
8564 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || |
8565 | (!R->getAsAdjusted<FunctionType>() && R->isFunctionProtoType()); |
8566 | |
8567 | NewFD = FunctionDecl::Create( |
8568 | SemaRef.Context, DC, D.getBeginLoc(), NameInfo, R, TInfo, SC, |
8569 | SemaRef.getCurFPFeatures().isFPConstrained(), isInline, HasPrototype, |
8570 | ConstexprSpecKind::Unspecified, |
8571 | nullptr); |
8572 | if (D.isInvalidType()) |
8573 | NewFD->setInvalidDecl(); |
8574 | |
8575 | return NewFD; |
8576 | } |
8577 | |
8578 | ExplicitSpecifier ExplicitSpecifier = D.getDeclSpec().getExplicitSpecifier(); |
8579 | |
8580 | ConstexprSpecKind ConstexprKind = D.getDeclSpec().getConstexprSpecifier(); |
8581 | if (ConstexprKind == ConstexprSpecKind::Constinit) { |
8582 | SemaRef.Diag(D.getDeclSpec().getConstexprSpecLoc(), |
8583 | diag::err_constexpr_wrong_decl_kind) |
8584 | << static_cast<int>(ConstexprKind); |
8585 | ConstexprKind = ConstexprSpecKind::Unspecified; |
8586 | D.getMutableDeclSpec().ClearConstexprSpec(); |
8587 | } |
8588 | Expr *TrailingRequiresClause = D.getTrailingRequiresClause(); |
8589 | |
8590 | |
8591 | |
8592 | |
8593 | if (!DC->isRecord() && |
8594 | SemaRef.RequireNonAbstractType( |
8595 | D.getIdentifierLoc(), R->castAs<FunctionType>()->getReturnType(), |
8596 | diag::err_abstract_type_in_decl, SemaRef.AbstractReturnType)) |
8597 | D.setInvalidType(); |
8598 | |
8599 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { |
8600 | |
8601 | assert(DC->isRecord() && |
8602 | "Constructors can only be declared in a member context"); |
8603 | |
8604 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); |
8605 | return CXXConstructorDecl::Create( |
8606 | SemaRef.Context, cast<CXXRecordDecl>(DC), D.getBeginLoc(), NameInfo, R, |
8607 | TInfo, ExplicitSpecifier, SemaRef.getCurFPFeatures().isFPConstrained(), |
8608 | isInline, false, ConstexprKind, |
8609 | InheritedConstructor(), TrailingRequiresClause); |
8610 | |
8611 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
8612 | |
8613 | if (DC->isRecord()) { |
8614 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); |
8615 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
8616 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( |
8617 | SemaRef.Context, Record, D.getBeginLoc(), NameInfo, R, TInfo, |
8618 | SemaRef.getCurFPFeatures().isFPConstrained(), isInline, |
8619 | false, ConstexprKind, |
8620 | TrailingRequiresClause); |
8621 | |
8622 | |
8623 | |
8624 | |
8625 | if (SemaRef.getLangOpts().CPlusPlus11) |
8626 | SemaRef.AdjustDestructorExceptionSpec(NewDD); |
8627 | |
8628 | IsVirtualOkay = true; |
8629 | return NewDD; |
8630 | |
8631 | } else { |
8632 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
8633 | D.setInvalidType(); |
8634 | |
8635 | |
8636 | |
8637 | return FunctionDecl::Create( |
8638 | SemaRef.Context, DC, D.getBeginLoc(), D.getIdentifierLoc(), Name, R, |
8639 | TInfo, SC, SemaRef.getCurFPFeatures().isFPConstrained(), isInline, |
8640 | true, ConstexprKind, TrailingRequiresClause); |
8641 | } |
8642 | |
8643 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
8644 | if (!DC->isRecord()) { |
8645 | SemaRef.Diag(D.getIdentifierLoc(), |
8646 | diag::err_conv_function_not_member); |
8647 | return nullptr; |
8648 | } |
8649 | |
8650 | SemaRef.CheckConversionDeclarator(D, R, SC); |
8651 | if (D.isInvalidType()) |
8652 | return nullptr; |
8653 | |
8654 | IsVirtualOkay = true; |
8655 | return CXXConversionDecl::Create( |
8656 | SemaRef.Context, cast<CXXRecordDecl>(DC), D.getBeginLoc(), NameInfo, R, |
8657 | TInfo, SemaRef.getCurFPFeatures().isFPConstrained(), isInline, |
8658 | ExplicitSpecifier, ConstexprKind, SourceLocation(), |
8659 | TrailingRequiresClause); |
8660 | |
8661 | } else if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
8662 | if (TrailingRequiresClause) |
8663 | SemaRef.Diag(TrailingRequiresClause->getBeginLoc(), |
8664 | diag::err_trailing_requires_clause_on_deduction_guide) |
8665 | << TrailingRequiresClause->getSourceRange(); |
8666 | SemaRef.CheckDeductionGuideDeclarator(D, R, SC); |
8667 | |
8668 | return CXXDeductionGuideDecl::Create(SemaRef.Context, DC, D.getBeginLoc(), |
8669 | ExplicitSpecifier, NameInfo, R, TInfo, |
8670 | D.getEndLoc()); |
8671 | } else if (DC->isRecord()) { |
8672 | |
8673 | |
8674 | |
8675 | |
8676 | if (Name.getAsIdentifierInfo() && |
8677 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ |
8678 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) |
8679 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
8680 | << SourceRange(D.getIdentifierLoc()); |
8681 | return nullptr; |
8682 | } |
8683 | |
8684 | |
8685 | CXXMethodDecl *Ret = CXXMethodDecl::Create( |
8686 | SemaRef.Context, cast<CXXRecordDecl>(DC), D.getBeginLoc(), NameInfo, R, |
8687 | TInfo, SC, SemaRef.getCurFPFeatures().isFPConstrained(), isInline, |
8688 | ConstexprKind, SourceLocation(), TrailingRequiresClause); |
8689 | IsVirtualOkay = !Ret->isStatic(); |
8690 | return Ret; |
8691 | } else { |
8692 | bool isFriend = |
8693 | SemaRef.getLangOpts().CPlusPlus && D.getDeclSpec().isFriendSpecified(); |
8694 | if (!isFriend && SemaRef.CurContext->isRecord()) |
8695 | return nullptr; |
8696 | |
8697 | |
8698 | |
8699 | |
8700 | return FunctionDecl::Create( |
8701 | SemaRef.Context, DC, D.getBeginLoc(), NameInfo, R, TInfo, SC, |
8702 | SemaRef.getCurFPFeatures().isFPConstrained(), isInline, |
8703 | true , ConstexprKind, TrailingRequiresClause); |
8704 | } |
8705 | } |
8706 | |
8707 | enum OpenCLParamType { |
8708 | ValidKernelParam, |
8709 | PtrPtrKernelParam, |
8710 | PtrKernelParam, |
8711 | InvalidAddrSpacePtrKernelParam, |
8712 | InvalidKernelParam, |
8713 | RecordKernelParam |
8714 | }; |
8715 | |
8716 | static bool isOpenCLSizeDependentType(ASTContext &C, QualType Ty) { |
8717 | |
8718 | |
8719 | |
8720 | StringRef SizeTypeNames[] = {"size_t", "intptr_t", "uintptr_t", "ptrdiff_t"}; |
8721 | |
8722 | |
8723 | |
8724 | QualType DesugaredTy = Ty; |
8725 | do { |
8726 | ArrayRef<StringRef> Names(SizeTypeNames); |
8727 | auto Match = llvm::find(Names, DesugaredTy.getUnqualifiedType().getAsString()); |
8728 | if (Names.end() != Match) |
8729 | return true; |
8730 | |
8731 | Ty = DesugaredTy; |
8732 | DesugaredTy = Ty.getSingleStepDesugaredType(C); |
8733 | } while (DesugaredTy != Ty); |
8734 | |
8735 | return false; |
8736 | } |
8737 | |
8738 | static OpenCLParamType getOpenCLKernelParameterType(Sema &S, QualType PT) { |
8739 | if (PT->isDependentType()) |
8740 | return InvalidKernelParam; |
8741 | |
8742 | if (PT->isPointerType() || PT->isReferenceType()) { |
8743 | QualType PointeeType = PT->getPointeeType(); |
8744 | if (PointeeType.getAddressSpace() == LangAS::opencl_generic || |
8745 | PointeeType.getAddressSpace() == LangAS::opencl_private || |
8746 | PointeeType.getAddressSpace() == LangAS::Default) |
8747 | return InvalidAddrSpacePtrKernelParam; |
8748 | |
8749 | if (PointeeType->isPointerType()) { |
8750 | |
8751 | |
8752 | OpenCLParamType ParamKind = getOpenCLKernelParameterType(S, PointeeType); |
8753 | if (ParamKind == InvalidAddrSpacePtrKernelParam || |
8754 | ParamKind == InvalidKernelParam) |
8755 | return ParamKind; |
8756 | |
8757 | return PtrPtrKernelParam; |
8758 | } |
8759 | |
8760 | |
8761 | |
8762 | |
8763 | |
8764 | if (S.getLangOpts().OpenCLCPlusPlus && |
8765 | !S.getOpenCLOptions().isAvailableOption( |
8766 | "__cl_clang_non_portable_kernel_param_types", S.getLangOpts()) && |
8767 | !PointeeType->isAtomicType() && !PointeeType->isVoidType() && |
8768 | !PointeeType->isStandardLayoutType()) |
8769 | return InvalidKernelParam; |
8770 | |
8771 | return PtrKernelParam; |
8772 | } |
8773 | |
8774 | |
8775 | |
8776 | |
8777 | |
8778 | |
8779 | if (isOpenCLSizeDependentType(S.getASTContext(), PT)) |
8780 | return InvalidKernelParam; |
8781 | |
8782 | if (PT->isImageType()) |
8783 | return PtrKernelParam; |
8784 | |
8785 | if (PT->isBooleanType() || PT->isEventT() || PT->isReserveIDT()) |
8786 | return InvalidKernelParam; |
8787 | |
8788 | |
8789 | |
8790 | |
8791 | if (!S.getOpenCLOptions().isAvailableOption("cl_khr_fp16", S.getLangOpts()) && |
8792 | PT->isHalfType()) |
8793 | return InvalidKernelParam; |
8794 | |
8795 | |
8796 | if (PT->isArrayType()) { |
8797 | const Type *UnderlyingTy = PT->getPointeeOrArrayElementType(); |
8798 | |
8799 | |
8800 | |
8801 | return getOpenCLKernelParameterType(S, QualType(UnderlyingTy, 0)); |
8802 | } |
8803 | |
8804 | |
8805 | |
8806 | |
8807 | |
8808 | if (S.getLangOpts().OpenCLCPlusPlus && |
8809 | !S.getOpenCLOptions().isAvailableOption( |
8810 | "__cl_clang_non_portable_kernel_param_types", S.getLangOpts()) && |
8811 | !PT->isOpenCLSpecificType() && !PT.isPODType(S.Context)) |
8812 | return InvalidKernelParam; |
8813 | |
8814 | if (PT->isRecordType()) |
8815 | return RecordKernelParam; |
8816 | |
8817 | return ValidKernelParam; |
8818 | } |
8819 | |
8820 | static void checkIsValidOpenCLKernelParameter( |
8821 | Sema &S, |
8822 | Declarator &D, |
8823 | ParmVarDecl *Param, |
8824 | llvm::SmallPtrSetImpl<const Type *> &ValidTypes) { |
8825 | QualType PT = Param->getType(); |
8826 | |
8827 | |
8828 | |
8829 | if (ValidTypes.count(PT.getTypePtr())) |
8830 | return; |
8831 | |
8832 | switch (getOpenCLKernelParameterType(S, PT)) { |
8833 | case PtrPtrKernelParam: |
8834 | |
8835 | |
8836 | |
8837 | if (S.getLangOpts().getOpenCLCompatibleVersion() <= 120) { |
8838 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); |
8839 | D.setInvalidType(); |
8840 | return; |
8841 | } |
8842 | |
8843 | ValidTypes.insert(PT.getTypePtr()); |
8844 | return; |
8845 | |
8846 | case InvalidAddrSpacePtrKernelParam: |
8847 | |
8848 | |
8849 | |
8850 | |
8851 | S.Diag(Param->getLocation(), diag::err_kernel_arg_address_space); |
8852 | D.setInvalidType(); |
8853 | return; |
8854 | |
8855 | |
8856 | |
8857 | |
8858 | |
8859 | |
8860 | |
8861 | case InvalidKernelParam: |
8862 | |
8863 | |
8864 | |
8865 | |
8866 | |
8867 | if (!PT->isHalfType()) { |
8868 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
8869 | |
8870 | |
8871 | const TypedefType *Typedef = nullptr; |
8872 | while ((Typedef = PT->getAs<TypedefType>())) { |
8873 | SourceLocation Loc = Typedef->getDecl()->getLocation(); |
8874 | |
8875 | if (Loc.isValid()) |
8876 | S.Diag(Loc, diag::note_entity_declared_at) << PT; |
8877 | PT = Typedef->desugar(); |
8878 | } |
8879 | } |
8880 | |
8881 | D.setInvalidType(); |
8882 | return; |
8883 | |
8884 | case PtrKernelParam: |
8885 | case ValidKernelParam: |
8886 | ValidTypes.insert(PT.getTypePtr()); |
8887 | return; |
8888 | |
8889 | case RecordKernelParam: |
8890 | break; |
8891 | } |
8892 | |
8893 | |
8894 | SmallVector<const Decl *, 4> VisitStack; |
8895 | |
8896 | |
8897 | |
8898 | SmallVector<const FieldDecl *, 4> HistoryStack; |
8899 | HistoryStack.push_back(nullptr); |
8900 | |
8901 | |
8902 | |
8903 | assert((PT->isArrayType() || PT->isRecordType()) && "Unexpected type."); |
8904 | const RecordType *RecTy = |
8905 | PT->getPointeeOrArrayElementType()->getAs<RecordType>(); |
8906 | const RecordDecl *OrigRecDecl = RecTy->getDecl(); |
8907 | |
8908 | VisitStack.push_back(RecTy->getDecl()); |
8909 | assert(VisitStack.back() && "First decl null?"); |
8910 | |
8911 | do { |
8912 | const Decl *Next = VisitStack.pop_back_val(); |
8913 | if (!Next) { |
8914 | assert(!HistoryStack.empty()); |
8915 | |
8916 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) |
8917 | ValidTypes.insert(Hist->getType().getTypePtr()); |
8918 | |
8919 | continue; |
8920 | } |
8921 | |
8922 | |
8923 | |
8924 | const RecordDecl *RD; |
8925 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { |
8926 | HistoryStack.push_back(Field); |
8927 | |
8928 | QualType FieldTy = Field->getType(); |
8929 | |
8930 | |
8931 | assert((FieldTy->isArrayType() || FieldTy->isRecordType()) && |
8932 | "Unexpected type."); |
8933 | const Type *FieldRecTy = FieldTy->getPointeeOrArrayElementType(); |
8934 | |
8935 | RD = FieldRecTy->castAs<RecordType>()->getDecl(); |
8936 | } else { |
8937 | RD = cast<RecordDecl>(Next); |
8938 | } |
8939 | |
8940 | |
8941 | VisitStack.push_back(nullptr); |
8942 | |
8943 | for (const auto *FD : RD->fields()) { |
8944 | QualType QT = FD->getType(); |
8945 | |
8946 | if (ValidTypes.count(QT.getTypePtr())) |
8947 | continue; |
8948 | |
8949 | OpenCLParamType ParamType = getOpenCLKernelParameterType(S, QT); |
8950 | if (ParamType == ValidKernelParam) |
8951 | continue; |
8952 | |
8953 | if (ParamType == RecordKernelParam) { |
8954 | VisitStack.push_back(FD); |
8955 | continue; |
8956 | } |
8957 | |
8958 | |
8959 | |
8960 | |
8961 | |
8962 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam || |
8963 | ParamType == InvalidAddrSpacePtrKernelParam) { |
8964 | S.Diag(Param->getLocation(), |
8965 | diag::err_record_with_pointers_kernel_param) |
8966 | << PT->isUnionType() |
8967 | << PT; |
8968 | } else { |
8969 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
8970 | } |
8971 | |
8972 | S.Diag(OrigRecDecl->getLocation(), diag::note_within_field_of_type) |
8973 | << OrigRecDecl->getDeclName(); |
8974 | |
8975 | |
8976 | |
8977 | for (ArrayRef<const FieldDecl *>::const_iterator |
8978 | I = HistoryStack.begin() + 1, |
8979 | E = HistoryStack.end(); |
8980 | I != E; ++I) { |
8981 | const FieldDecl *OuterField = *I; |
8982 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) |
8983 | << OuterField->getType(); |
8984 | } |
8985 | |
8986 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) |
8987 | << QT->isPointerType() |
8988 | << QT; |
8989 | D.setInvalidType(); |
8990 | return; |
8991 | } |
8992 | } while (!VisitStack.empty()); |
8993 | } |
8994 | |
8995 | |
8996 | |
8997 | |
8998 | static DeclContext *getTagInjectionContext(DeclContext *DC) { |
8999 | while (!DC->isFileContext() && !DC->isFunctionOrMethod()) |
9000 | DC = DC->getParent(); |
9001 | return DC; |
9002 | } |
9003 | |
9004 | |
9005 | |
9006 | |
9007 | static Scope *getTagInjectionScope(Scope *S, const LangOptions &LangOpts) { |
9008 | while (S->isClassScope() || |
9009 | (LangOpts.CPlusPlus && |
9010 | S->isFunctionPrototypeScope()) || |
9011 | ((S->getFlags() & Scope::DeclScope) == 0) || |
9012 | (S->getEntity() && S->getEntity()->isTransparentContext())) |
9013 | S = S->getParent(); |
9014 | return S; |
9015 | } |
9016 | |
9017 | NamedDecl* |
9018 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
9019 | TypeSourceInfo *TInfo, LookupResult &Previous, |
9020 | MultiTemplateParamsArg TemplateParamListsRef, |
9021 | bool &AddToScope) { |
9022 | QualType R = TInfo->getType(); |
9023 | |
9024 | assert(R->isFunctionType()); |
9025 | if (R.getCanonicalType()->castAs<FunctionType>()->getCmseNSCallAttr()) |
9026 | Diag(D.getIdentifierLoc(), diag::err_function_decl_cmse_ns_call); |
9027 | |
9028 | SmallVector<TemplateParameterList *, 4> TemplateParamLists; |
9029 | for (TemplateParameterList *TPL : TemplateParamListsRef) |
9030 | TemplateParamLists.push_back(TPL); |
9031 | if (TemplateParameterList *Invented = D.getInventedTemplateParameterList()) { |
9032 | if (!TemplateParamLists.empty() && |
9033 | Invented->getDepth() == TemplateParamLists.back()->getDepth()) |
9034 | TemplateParamLists.back() = Invented; |
9035 | else |
9036 | TemplateParamLists.push_back(Invented); |
9037 | } |
9038 | |
9039 | |
9040 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
9041 | DeclarationName Name = NameInfo.getName(); |
9042 | StorageClass SC = getFunctionStorageClass(*this, D); |
9043 | |
9044 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
9045 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
9046 | diag::err_invalid_thread) |
9047 | << DeclSpec::getSpecifierName(TSCS); |
9048 | |
9049 | if (D.isFirstDeclarationOfMember()) |
9050 | adjustMemberFunctionCC(R, D.isStaticMember(), D.isCtorOrDtor(), |
9051 | D.getIdentifierLoc()); |
9052 | |
9053 | bool isFriend = false; |
9054 | FunctionTemplateDecl *FunctionTemplate = nullptr; |
9055 | bool isMemberSpecialization = false; |
9056 | bool isFunctionTemplateSpecialization = false; |
9057 | |
9058 | bool isDependentClassScopeExplicitSpecialization = false; |
9059 | bool HasExplicitTemplateArgs = false; |
9060 | TemplateArgumentListInfo TemplateArgs; |
9061 | |
9062 | bool isVirtualOkay = false; |
9063 | |
9064 | DeclContext *OriginalDC = DC; |
9065 | bool IsLocalExternDecl = adjustContextForLocalExternDecl(DC); |
9066 | |
9067 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
9068 | isVirtualOkay); |
9069 | if (!NewFD) return nullptr; |
9070 | |
9071 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
9072 | NewFD->setTopLevelDeclInObjCContainer(); |
9073 | |
9074 | |
9075 | |
9076 | |
9077 | NewFD->setLexicalDeclContext(CurContext); |
9078 | |
9079 | if (IsLocalExternDecl) |
9080 | NewFD->setLocalExternDecl(); |
9081 | |
9082 | if (getLangOpts().CPlusPlus) { |
9083 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
9084 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
9085 | bool hasExplicit = D.getDeclSpec().hasExplicitSpecifier(); |
9086 | isFriend = D.getDeclSpec().isFriendSpecified(); |
9087 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
9088 | |
9089 | |
9090 | |
9091 | NewFD->setImplicitlyInline(); |
9092 | } |
9093 | |
9094 | |
9095 | |
9096 | |
9097 | if (const CXXRecordDecl *Parent = |
9098 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { |
9099 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) |
9100 | NewFD->setPure(true); |
9101 | |
9102 | |
9103 | |
9104 | if (isVirtual && Parent->isUnion()) |
9105 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_in_union); |
9106 | } |
9107 | |
9108 | SetNestedNameSpecifier(*this, NewFD, D); |
9109 | isMemberSpecialization = false; |
9110 | isFunctionTemplateSpecialization = false; |
9111 | if (D.isInvalidType()) |
9112 | NewFD->setInvalidDecl(); |
9113 | |
9114 | |
9115 | |
9116 | bool Invalid = false; |
9117 | TemplateParameterList *TemplateParams = |
9118 | MatchTemplateParametersToScopeSpecifier( |
9119 | D.getDeclSpec().getBeginLoc(), D.getIdentifierLoc(), |
9120 | D.getCXXScopeSpec(), |
9121 | D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId |
9122 | ? D.getName().TemplateId |
9123 | : nullptr, |
9124 | TemplateParamLists, isFriend, isMemberSpecialization, |
9125 | Invalid); |
9126 | if (TemplateParams) { |
9127 | |
9128 | if (CheckTemplateDeclScope(S, TemplateParams)) |
9129 | NewFD->setInvalidDecl(); |
9130 | |
9131 | if (TemplateParams->size() > 0) { |
9132 | |
9133 | |
9134 | |
9135 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
9136 | Diag(NewFD->getLocation(), diag::err_destructor_template); |
9137 | NewFD->setInvalidDecl(); |
9138 | } |
9139 | |
9140 | |
9141 | |
9142 | |
9143 | if (DC->isDependentContext()) { |
9144 | ContextRAII SavedContext(*this, DC); |
9145 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
9146 | Invalid = true; |
9147 | } |
9148 | |
9149 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
9150 | NewFD->getLocation(), |
9151 | Name, TemplateParams, |
9152 | NewFD); |
9153 | FunctionTemplate->setLexicalDeclContext(CurContext); |
9154 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); |
9155 | |
9156 | |
9157 | if (TemplateParamLists.size() > 1) { |
9158 | NewFD->setTemplateParameterListsInfo(Context, |
9159 | ArrayRef<TemplateParameterList *>(TemplateParamLists) |
9160 | .drop_back(1)); |
9161 | } |
9162 | } else { |
9163 | |
9164 | isFunctionTemplateSpecialization = true; |
9165 | |
9166 | if (TemplateParamLists.size() > 0) |
9167 | NewFD->setTemplateParameterListsInfo(Context, TemplateParamLists); |
9168 | |
9169 | |
9170 | if (isFriend) { |
9171 | |
9172 | SourceRange RemoveRange = TemplateParams->getSourceRange(); |
9173 | |
9174 | |
9175 | |
9176 | |
9177 | |
9178 | |
9179 | SourceLocation InsertLoc; |
9180 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
9181 | InsertLoc = D.getName().getSourceRange().getEnd(); |
9182 | InsertLoc = getLocForEndOfToken(InsertLoc); |
9183 | } |
9184 | |
9185 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) |
9186 | << Name << RemoveRange |
9187 | << FixItHint::CreateRemoval(RemoveRange) |
9188 | << FixItHint::CreateInsertion(InsertLoc, "<>"); |
9189 | } |
9190 | } |
9191 | } else { |
9192 | |
9193 | if (!TemplateParamLists.empty() && isMemberSpecialization && |
9194 | CheckTemplateDeclScope(S, TemplateParamLists.back())) |
9195 | NewFD->setInvalidDecl(); |
9196 | |
9197 | |
9198 | |
9199 | if (TemplateParamLists.size() > 0) |
9200 | |
9201 | NewFD->setTemplateParameterListsInfo(Context, TemplateParamLists); |
9202 | } |
9203 | |
9204 | if (Invalid) { |
9205 | NewFD->setInvalidDecl(); |
9206 | if (FunctionTemplate) |
9207 | FunctionTemplate->setInvalidDecl(); |
9208 | } |
9209 | |
9210 | |
9211 | |
9212 | |
9213 | |
9214 | |
9215 | if (isVirtual && !NewFD->isInvalidDecl()) { |
9216 | if (!isVirtualOkay) { |
9217 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
9218 | diag::err_virtual_non_function); |
9219 | } else if (!CurContext->isRecord()) { |
9220 | |
9221 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
9222 | diag::err_virtual_out_of_class) |
9223 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
9224 | } else if (NewFD->getDescribedFunctionTemplate()) { |
9225 | |
9226 | |
9227 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
9228 | diag::err_virtual_member_function_template) |
9229 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
9230 | } else { |
9231 | |
9232 | NewFD->setVirtualAsWritten(true); |
9233 | } |
9234 | |
9235 | if (getLangOpts().CPlusPlus14 && |
9236 | NewFD->getReturnType()->isUndeducedType()) |
9237 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); |
9238 | } |
9239 | |
9240 | if (getLangOpts().CPlusPlus14 && |
9241 | (NewFD->isDependentContext() || |
9242 | (isFriend && CurContext->isDependentContext())) && |
9243 | NewFD->getReturnType()->isUndeducedType()) { |
9244 | |
9245 | |
9246 | |
9247 | |
9248 | |
9249 | |
9250 | const FunctionProtoType *FPT = |
9251 | NewFD->getType()->castAs<FunctionProtoType>(); |
9252 | QualType Result = |
9253 | SubstAutoType(FPT->getReturnType(), Context.DependentTy); |
9254 | NewFD->setType(Context.getFunctionType(Result, FPT->getParamTypes(), |
9255 | FPT->getExtProtoInfo())); |
9256 | } |
9257 | |
9258 | |
9259 | |
9260 | |
9261 | if (isInline && !NewFD->isInvalidDecl()) { |
9262 | if (CurContext->isFunctionOrMethod()) { |
9263 | |
9264 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
9265 | diag::err_inline_declaration_block_scope) << Name |
9266 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
9267 | } |
9268 | } |
9269 | |
9270 | |
9271 | |
9272 | |
9273 | |
9274 | if (hasExplicit && !NewFD->isInvalidDecl() && |
9275 | !isa<CXXDeductionGuideDecl>(NewFD)) { |
9276 | if (!CurContext->isRecord()) { |
9277 | |
9278 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
9279 | diag::err_explicit_out_of_class) |
9280 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecRange()); |
9281 | } else if (!isa<CXXConstructorDecl>(NewFD) && |
9282 | !isa<CXXConversionDecl>(NewFD)) { |
9283 | |
9284 | |
9285 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
9286 | diag::err_explicit_non_ctor_or_conv_function) |
9287 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecRange()); |
9288 | } |
9289 | } |
9290 | |
9291 | ConstexprSpecKind ConstexprKind = D.getDeclSpec().getConstexprSpecifier(); |
9292 | if (ConstexprKind != ConstexprSpecKind::Unspecified) { |
9293 | |
9294 | |
9295 | NewFD->setImplicitlyInline(); |
9296 | |
9297 | |
9298 | |
9299 | |
9300 | if (isa<CXXDestructorDecl>(NewFD) && |
9301 | (!getLangOpts().CPlusPlus20 || |
9302 | ConstexprKind == ConstexprSpecKind::Consteval)) { |
9303 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor) |
9304 | << static_cast<int>(ConstexprKind); |
9305 | NewFD->setConstexprKind(getLangOpts().CPlusPlus20 |
9306 | ? ConstexprSpecKind::Unspecified |
9307 | : ConstexprSpecKind::Constexpr); |
9308 | } |
9309 | |
9310 | |
9311 | |
9312 | if (ConstexprKind == ConstexprSpecKind::Consteval && |
9313 | (NewFD->getOverloadedOperator() == OO_New || |
9314 | NewFD->getOverloadedOperator() == OO_Array_New || |
9315 | NewFD->getOverloadedOperator() == OO_Delete || |
9316 | NewFD->getOverloadedOperator() == OO_Array_Delete)) { |
9317 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
9318 | diag::err_invalid_consteval_decl_kind) |
9319 | << NewFD; |
9320 | NewFD->setConstexprKind(ConstexprSpecKind::Constexpr); |
9321 | } |
9322 | } |
9323 | |
9324 | |
9325 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
9326 | if (isFunctionTemplateSpecialization) { |
9327 | SourceLocation ModulePrivateLoc |
9328 | = D.getDeclSpec().getModulePrivateSpecLoc(); |
9329 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) |
9330 | << 0 |
9331 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
9332 | } else { |
9333 | NewFD->setModulePrivate(); |
9334 | if (FunctionTemplate) |
9335 | FunctionTemplate->setModulePrivate(); |
9336 | } |
9337 | } |
9338 | |
9339 | if (isFriend) { |
9340 | if (FunctionTemplate) { |
9341 | FunctionTemplate->setObjectOfFriendDecl(); |
9342 | FunctionTemplate->setAccess(AS_public); |
9343 | } |
9344 | NewFD->setObjectOfFriendDecl(); |
9345 | NewFD->setAccess(AS_public); |
9346 | } |
9347 | |
9348 | |
9349 | |
9350 | switch (D.getFunctionDefinitionKind()) { |
9351 | case FunctionDefinitionKind::Declaration: |
9352 | case FunctionDefinitionKind::Definition: |
9353 | break; |
9354 | |
9355 | case FunctionDefinitionKind::Defaulted: |
9356 | NewFD->setDefaulted(); |
9357 | break; |
9358 | |
9359 | case FunctionDefinitionKind::Deleted: |
9360 | NewFD->setDeletedAsWritten(); |
9361 | break; |
9362 | } |
9363 | |
9364 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
9365 | D.isFunctionDefinition()) { |
9366 | |
9367 | |
9368 | |
9369 | NewFD->setImplicitlyInline(); |
9370 | } |
9371 | |
9372 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
9373 | !CurContext->isRecord()) { |
9374 | |
9375 | |
9376 | |
9377 | |
9378 | |
9379 | |
9380 | |
9381 | |
9382 | |
9383 | |
9384 | |
9385 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
9386 | ((!getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015) && |
9387 | cast<CXXRecordDecl>(DC)->getDescribedClassTemplate()) || |
9388 | (getLangOpts().MSVCCompat && NewFD->getDescribedFunctionTemplate())) |
9389 | ? diag::ext_static_out_of_line : diag::err_static_out_of_line) |
9390 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
9391 | } |
9392 | |
9393 | |
9394 | |
9395 | |
9396 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); |
9397 | if ((Name.getCXXOverloadedOperator() == OO_Delete || |
9398 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && |
9399 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) |
9400 | NewFD->setType(Context.getFunctionType( |
9401 | FPT->getReturnType(), FPT->getParamTypes(), |
9402 | FPT->getExtProtoInfo().withExceptionSpec(EST_BasicNoexcept))); |
9403 | } |
9404 | |
9405 | |
9406 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewFD), |
9407 | D.getCXXScopeSpec().isNotEmpty() || |
9408 | isMemberSpecialization || |
9409 | isFunctionTemplateSpecialization); |
9410 | |
9411 | |
9412 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
9413 | |
9414 | StringLiteral *SE = cast<StringLiteral>(E); |
9415 | NewFD->addAttr(AsmLabelAttr::Create(Context, SE->getString(), |
9416 | true, |
9417 | SE->getStrTokenLoc(0))); |
9418 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
9419 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
9420 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); |
9421 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
9422 | if (isDeclExternC(NewFD)) { |
9423 | NewFD->addAttr(I->second); |
9424 | ExtnameUndeclaredIdentifiers.erase(I); |
9425 | } else |
9426 | Diag(NewFD->getLocation(), diag::warn_redefine_extname_not_applied) |
9427 | << 0 << NewFD; |
9428 | } |
9429 | } |
9430 | |
9431 | |
9432 | |
9433 | SmallVector<ParmVarDecl*, 16> Params; |
9434 | unsigned FTIIdx; |
9435 | if (D.isFunctionDeclarator(FTIIdx)) { |
9436 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(FTIIdx).Fun; |
9437 | |
9438 | |
9439 | |
9440 | |
9441 | |
9442 | |
9443 | if (FTIHasNonVoidParameters(FTI) && FTI.Params[0].Param) { |
9444 | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) { |
9445 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
9446 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
9447 | Param->setDeclContext(NewFD); |
9448 | Params.push_back(Param); |
9449 | |
9450 | if (Param->isInvalidDecl()) |
9451 | NewFD->setInvalidDecl(); |
9452 | } |
9453 | } |
9454 | |
9455 | if (!getLangOpts().CPlusPlus) { |
9456 | |
9457 | |
9458 | |
9459 | |
9460 | DeclContext *PrototypeTagContext = |
9461 | getTagInjectionContext(NewFD->getLexicalDeclContext()); |
9462 | for (NamedDecl *NonParmDecl : FTI.getDeclsInPrototype()) { |
9463 | auto *TD = dyn_cast<TagDecl>(NonParmDecl); |
9464 | |
9465 | |
9466 | |
9467 | if (!TD) { |
9468 | if (auto *ECD = dyn_cast<EnumConstantDecl>(NonParmDecl)) |
9469 | TD = cast<EnumDecl>(ECD->getDeclContext()); |
9470 | } |
9471 | if (!TD) |
9472 | continue; |
9473 | DeclContext *TagDC = TD->getLexicalDeclContext(); |
9474 | if (!TagDC->containsDecl(TD)) |
9475 | continue; |
9476 | TagDC->removeDecl(TD); |
9477 | TD->setDeclContext(NewFD); |
9478 | NewFD->addDecl(TD); |
9479 | |
9480 | |
9481 | |
9482 | |
9483 | |
9484 | |
9485 | if (TagDC != PrototypeTagContext) |
9486 | TD->setLexicalDeclContext(TagDC); |
9487 | } |
9488 | } |
9489 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
9490 | |
9491 | |
9492 | |
9493 | |
9494 | |
9495 | |
9496 | |
9497 | |
9498 | |
9499 | |
9500 | for (const auto &AI : FT->param_types()) { |
9501 | ParmVarDecl *Param = |
9502 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), AI); |
9503 | Param->setScopeInfo(0, Params.size()); |
9504 | Params.push_back(Param); |
9505 | } |
9506 | } else { |
9507 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && |
9508 | "Should not need args for typedef of non-prototype fn"); |
9509 | } |
9510 | |
9511 | |
9512 | NewFD->setParams(Params); |
9513 | |
9514 | if (D.getDeclSpec().isNoreturnSpecified()) |
9515 | NewFD->addAttr(C11NoReturnAttr::Create(Context, |
9516 | D.getDeclSpec().getNoreturnSpecLoc(), |
9517 | AttributeCommonInfo::AS_Keyword)); |
9518 | |
9519 | |
9520 | |
9521 | if (!NewFD->isInvalidDecl() && |
9522 | NewFD->getReturnType()->isVariablyModifiedType()) { |
9523 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); |
9524 | NewFD->setInvalidDecl(); |
9525 | } |
9526 | |
9527 | |
9528 | if (PragmaClangTextSection.Valid && D.isFunctionDefinition() && |
9529 | !NewFD->hasAttr<SectionAttr>()) |
9530 | NewFD->addAttr(PragmaClangTextSectionAttr::CreateImplicit( |
9531 | Context, PragmaClangTextSection.SectionName, |
9532 | PragmaClangTextSection.PragmaLocation, AttributeCommonInfo::AS_Pragma)); |
9533 | |
9534 | |
9535 | if (CodeSegStack.CurrentValue && D.isFunctionDefinition() && |
9536 | !NewFD->hasAttr<SectionAttr>()) { |
9537 | NewFD->addAttr(SectionAttr::CreateImplicit( |
9538 | Context, CodeSegStack.CurrentValue->getString(), |
9539 | CodeSegStack.CurrentPragmaLocation, AttributeCommonInfo::AS_Pragma, |
9540 | SectionAttr::Declspec_allocate)); |
9541 | if (UnifySection(CodeSegStack.CurrentValue->getString(), |
9542 | ASTContext::PSF_Implicit | ASTContext::PSF_Execute | |
9543 | ASTContext::PSF_Read, |
9544 | NewFD)) |
9545 | NewFD->dropAttr<SectionAttr>(); |
9546 | } |
9547 | |
9548 | |
9549 | |
9550 | if (!NewFD->hasAttr<CodeSegAttr>()) { |
9551 | if (Attr *SAttr = getImplicitCodeSegOrSectionAttrForFunction(NewFD, |
9552 | D.isFunctionDefinition())) { |
9553 | NewFD->addAttr(SAttr); |
9554 | } |
9555 | } |
9556 | |
9557 | |
9558 | ProcessDeclAttributes(S, NewFD, D); |
9559 | |
9560 | if (getLangOpts().OpenCL) { |
9561 | |
9562 | |
9563 | LangAS AddressSpace = NewFD->getReturnType().getAddressSpace(); |
9564 | if (AddressSpace != LangAS::Default) { |
9565 | Diag(NewFD->getLocation(), |
9566 | diag::err_opencl_return_value_with_address_space); |
9567 | NewFD->setInvalidDecl(); |
9568 | } |
9569 | } |
9570 | |
9571 | if (LangOpts.SYCLIsDevice || (LangOpts.OpenMP && LangOpts.OpenMPIsDevice)) |
9572 | checkDeviceDecl(NewFD, D.getBeginLoc()); |
9573 | |
9574 | if (!getLangOpts().CPlusPlus) { |
9575 | |
9576 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
9577 | CheckMain(NewFD, D.getDeclSpec()); |
9578 | |
9579 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
9580 | CheckMSVCRTEntryPoint(NewFD); |
9581 | |
9582 | if (!NewFD->isInvalidDecl()) |
9583 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
9584 | isMemberSpecialization)); |
9585 | else if (!Previous.empty()) |
9586 | |
9587 | D.setRedeclaration(true); |
9588 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
9589 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
9590 | "previous declaration set still overloaded"); |
9591 | |
9592 | |
9593 | |
9594 | |
9595 | const FunctionType *FT = NewFD->getType()->castAs<FunctionType>(); |
9596 | if (isa<FunctionNoProtoType>(FT) && !D.isFunctionDefinition()) { |
9597 | CallingConv CC = FT->getExtInfo().getCC(); |
9598 | if (!supportsVariadicCall(CC)) { |
9599 | |
9600 | |
9601 | int DiagID = |
9602 | CC == CC_X86StdCall ? diag::warn_cconv_knr : diag::err_cconv_knr; |
9603 | Diag(NewFD->getLocation(), DiagID) |
9604 | << FunctionType::getNameForCallConv(CC); |
9605 | } |
9606 | } |
9607 | |
9608 | if (NewFD->getReturnType().hasNonTrivialToPrimitiveDestructCUnion() || |
9609 | NewFD->getReturnType().hasNonTrivialToPrimitiveCopyCUnion()) |
9610 | checkNonTrivialCUnion(NewFD->getReturnType(), |
9611 | NewFD->getReturnTypeSourceRange().getBegin(), |
9612 | NTCUC_FunctionReturn, NTCUK_Destruct|NTCUK_Copy); |
9613 | } else { |
9614 | |
9615 | |
9616 | |
9617 | |
9618 | |
9619 | |
9620 | |
9621 | if (D.getDeclSpec().isInlineSpecified() && |
9622 | NewFD->isReplaceableGlobalAllocationFunction() && |
9623 | !NewFD->hasAttr<UsedAttr>()) |
9624 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
9625 | diag::ext_operator_new_delete_declared_inline) |
9626 | << NewFD->getDeclName(); |
9627 | |
9628 | |
9629 | |
9630 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
9631 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
9632 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
9633 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
9634 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
9635 | TemplateId->NumArgs); |
9636 | translateTemplateArguments(TemplateArgsPtr, |
9637 | TemplateArgs); |
9638 | |
9639 | HasExplicitTemplateArgs = true; |
9640 | |
9641 | if (NewFD->isInvalidDecl()) { |
9642 | HasExplicitTemplateArgs = false; |
9643 | } else if (FunctionTemplate) { |
9644 | |
9645 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) |
9646 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); |
9647 | |
9648 | HasExplicitTemplateArgs = false; |
9649 | } else { |
9650 | assert((isFunctionTemplateSpecialization || |
9651 | D.getDeclSpec().isFriendSpecified()) && |
9652 | "should have a 'template<>' for this decl"); |
9653 | |
9654 | isFunctionTemplateSpecialization = true; |
9655 | } |
9656 | } else if (isFriend && isFunctionTemplateSpecialization) { |
9657 | |
9658 | |
9659 | |
9660 | |
9661 | |
9662 | |
9663 | HasExplicitTemplateArgs = true; |
9664 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); |
9665 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); |
9666 | } |
9667 | |
9668 | |
9669 | |
9670 | |
9671 | |
9672 | |
9673 | |
9674 | if (getLangOpts().CUDA && !isFunctionTemplateSpecialization) |
9675 | maybeAddCUDAHostDeviceAttrs(NewFD, Previous); |
9676 | |
9677 | |
9678 | |
9679 | |
9680 | |
9681 | if (isFunctionTemplateSpecialization && isFriend && |
9682 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
9683 | TemplateSpecializationType::anyInstantiationDependentTemplateArguments( |
9684 | TemplateArgs.arguments()))) { |
9685 | assert(HasExplicitTemplateArgs && |
9686 | "friend function specialization without template args"); |
9687 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, |
9688 | Previous)) |
9689 | NewFD->setInvalidDecl(); |
9690 | } else if (isFunctionTemplateSpecialization) { |
9691 | if (CurContext->isDependentContext() && CurContext->isRecord() |
9692 | && !isFriend) { |
9693 | isDependentClassScopeExplicitSpecialization = true; |
9694 | } else if (!NewFD->isInvalidDecl() && |
9695 | CheckFunctionTemplateSpecialization( |
9696 | NewFD, (HasExplicitTemplateArgs ? &TemplateArgs : nullptr), |
9697 | Previous)) |
9698 | NewFD->setInvalidDecl(); |
9699 | |
9700 | |
9701 | |
9702 | |
9703 | FunctionTemplateSpecializationInfo *Info = |
9704 | NewFD->getTemplateSpecializationInfo(); |
9705 | if (Info && SC != SC_None) { |
9706 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) |
9707 | Diag(NewFD->getLocation(), |
9708 | diag::err_explicit_specialization_inconsistent_storage_class) |
9709 | << SC |
9710 | << FixItHint::CreateRemoval( |
9711 | D.getDeclSpec().getStorageClassSpecLoc()); |
9712 | |
9713 | else |
9714 | Diag(NewFD->getLocation(), |
9715 | diag::ext_explicit_specialization_storage_class) |
9716 | << FixItHint::CreateRemoval( |
9717 | D.getDeclSpec().getStorageClassSpecLoc()); |
9718 | } |
9719 | } else if (isMemberSpecialization && isa<CXXMethodDecl>(NewFD)) { |
9720 | if (CheckMemberSpecialization(NewFD, Previous)) |
9721 | NewFD->setInvalidDecl(); |
9722 | } |
9723 | |
9724 | |
9725 | if (!isDependentClassScopeExplicitSpecialization) { |
9726 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
9727 | CheckMain(NewFD, D.getDeclSpec()); |
9728 | |
9729 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
9730 | CheckMSVCRTEntryPoint(NewFD); |
9731 | |
9732 | if (!NewFD->isInvalidDecl()) |
9733 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
9734 | isMemberSpecialization)); |
9735 | else if (!Previous.empty()) |
9736 | |
9737 | D.setRedeclaration(true); |
9738 | } |
9739 | |
9740 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
9741 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
9742 | "previous declaration set still overloaded"); |
9743 | |
9744 | NamedDecl *PrincipalDecl = (FunctionTemplate |
9745 | ? cast<NamedDecl>(FunctionTemplate) |
9746 | : NewFD); |
9747 | |
9748 | if (isFriend && NewFD->getPreviousDecl()) { |
9749 | AccessSpecifier Access = AS_public; |
9750 | if (!NewFD->isInvalidDecl()) |
9751 | Access = NewFD->getPreviousDecl()->getAccess(); |
9752 | |
9753 | NewFD->setAccess(Access); |
9754 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); |
9755 | } |
9756 | |
9757 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && |
9758 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
9759 | PrincipalDecl->setNonMemberOperator(); |
9760 | |
9761 | |
9762 | |
9763 | if (FunctionTemplate) { |
9764 | FunctionTemplateDecl *PrevTemplate = |
9765 | FunctionTemplate->getPreviousDecl(); |
9766 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
9767 | PrevTemplate ? PrevTemplate->getTemplateParameters() |
9768 | : nullptr, |
9769 | D.getDeclSpec().isFriendSpecified() |
9770 | ? (D.isFunctionDefinition() |
9771 | ? TPC_FriendFunctionTemplateDefinition |
9772 | : TPC_FriendFunctionTemplate) |
9773 | : (D.getCXXScopeSpec().isSet() && |
9774 | DC && DC->isRecord() && |
9775 | DC->isDependentContext()) |
9776 | ? TPC_ClassTemplateMember |
9777 | : TPC_FunctionTemplate); |
9778 | } |
9779 | |
9780 | if (NewFD->isInvalidDecl()) { |
9781 | |
9782 | } else if (!D.isRedeclaration()) { |
9783 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
9784 | AddToScope }; |
9785 | |
9786 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) |
9787 | NewFD->setAccess(AS_public); |
9788 | |
9789 | |
9790 | if (D.getCXXScopeSpec().isSet()) { |
9791 | |
9792 | |
9793 | |
9794 | |
9795 | |
9796 | |
9797 | |
9798 | |
9799 | |
9800 | |
9801 | |
9802 | |
9803 | |
9804 | if (isFriend && |
9805 | (D.getCXXScopeSpec().getScopeRep()->isDependent() || |
9806 | (!Previous.empty() && CurContext->isDependentContext()))) { |
9807 | |
9808 | } else if (NewFD->isCPUDispatchMultiVersion() || |
9809 | NewFD->isCPUSpecificMultiVersion()) { |
9810 | |
9811 | |
9812 | } else { |
9813 | |
9814 | |
9815 | |
9816 | |
9817 | |
9818 | |
9819 | |
9820 | |
9821 | |
9822 | |
9823 | |
9824 | |
9825 | |
9826 | |
9827 | |
9828 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
9829 | *this, Previous, NewFD, ExtraArgs, false, nullptr)) { |
9830 | AddToScope = ExtraArgs.AddToScope; |
9831 | return Result; |
9832 | } |
9833 | } |
9834 | |
9835 | |
9836 | |
9837 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
9838 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
9839 | *this, Previous, NewFD, ExtraArgs, true, S)) { |
9840 | AddToScope = ExtraArgs.AddToScope; |
9841 | return Result; |
9842 | } |
9843 | } |
9844 | } else if (!D.isFunctionDefinition() && |
9845 | isa<CXXMethodDecl>(NewFD) && NewFD->isOutOfLine() && |
9846 | !isFriend && !isFunctionTemplateSpecialization && |
9847 | !isMemberSpecialization) { |
9848 | |
9849 | |
9850 | |
9851 | |
9852 | |
9853 | |
9854 | |
9855 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
9856 | << D.getCXXScopeSpec().getRange(); |
9857 | } |
9858 | } |
9859 | |
9860 | |
9861 | |
9862 | if (!D.isRedeclaration() && |
9863 | NewFD->getDeclContext()->getRedeclContext()->isFileContext()) { |
9864 | if (IdentifierInfo *II = Previous.getLookupName().getAsIdentifierInfo()) { |
9865 | if (unsigned BuiltinID = II->getBuiltinID()) { |
9866 | if (NewFD->getLanguageLinkage() == CLanguageLinkage) { |
9867 | |
9868 | |
9869 | if (Context.BuiltinInfo.allowTypeMismatch(BuiltinID)) { |
9870 | NewFD->addAttr(BuiltinAttr::CreateImplicit(Context, BuiltinID)); |
9871 | } else { |
9872 | ASTContext::GetBuiltinTypeError Error; |
9873 | LookupNecessaryTypesForBuiltin(S, BuiltinID); |
9874 | QualType BuiltinType = Context.GetBuiltinType(BuiltinID, Error); |
9875 | |
9876 | if (!Error && !BuiltinType.isNull() && |
9877 | Context.hasSameFunctionTypeIgnoringExceptionSpec( |
9878 | NewFD->getType(), BuiltinType)) |
9879 | NewFD->addAttr(BuiltinAttr::CreateImplicit(Context, BuiltinID)); |
9880 | } |
9881 | } else if (BuiltinID == Builtin::BI__GetExceptionInfo && |
9882 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
9883 | |
9884 | NewFD->addAttr(BuiltinAttr::CreateImplicit(Context, BuiltinID)); |
9885 | } |
9886 | } |
9887 | } |
9888 | } |
9889 | |
9890 | ProcessPragmaWeak(S, NewFD); |
9891 | checkAttributesAfterMerging(*this, *NewFD); |
9892 | |
9893 | AddKnownFunctionAttributes(NewFD); |
9894 | |
9895 | if (NewFD->hasAttr<OverloadableAttr>() && |
9896 | !NewFD->getType()->getAs<FunctionProtoType>()) { |
9897 | Diag(NewFD->getLocation(), |
9898 | diag::err_attribute_overloadable_no_prototype) |
9899 | << NewFD; |
9900 | |
9901 | |
9902 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); |
9903 | FunctionProtoType::ExtProtoInfo EPI( |
9904 | Context.getDefaultCallingConvention(true, false)); |
9905 | EPI.Variadic = true; |
9906 | EPI.ExtInfo = FT->getExtInfo(); |
9907 | |
9908 | QualType R = Context.getFunctionType(FT->getReturnType(), None, EPI); |
9909 | NewFD->setType(R); |
9910 | } |
9911 | |
9912 | |
9913 | |
9914 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
9915 | AddPushedVisibilityAttribute(NewFD); |
9916 | |
9917 | |
9918 | |
9919 | AddCFAuditedAttribute(NewFD); |
9920 | |
9921 | |
9922 | |
9923 | if(D.isFunctionDefinition()) |
9924 | AddRangeBasedOptnone(NewFD); |
9925 | |
9926 | |
9927 | |
9928 | if (NewFD->isFirstDecl() && !NewFD->isInvalidDecl() && |
9929 | isIncompleteDeclExternC(*this, NewFD)) |
9930 | RegisterLocallyScopedExternCDecl(NewFD, S); |
9931 | |
9932 | |
9933 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
9934 | |
9935 | if (D.isRedeclaration() && !Previous.empty()) { |
9936 | NamedDecl *Prev = Previous.getRepresentativeDecl(); |
9937 | checkDLLAttributeRedeclaration(*this, Prev, NewFD, |
9938 | isMemberSpecialization || |
9939 | isFunctionTemplateSpecialization, |
9940 | D.isFunctionDefinition()); |
9941 | } |
9942 | |
9943 | if (getLangOpts().CUDA) { |
9944 | IdentifierInfo *II = NewFD->getIdentifier(); |
9945 | if (II && II->isStr(getCudaConfigureFuncName()) && |
9946 | !NewFD->isInvalidDecl() && |
9947 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
9948 | if (!R->castAs<FunctionType>()->getReturnType()->isScalarType()) |
9949 | Diag(NewFD->getLocation(), diag::err_config_scalar_return) |
9950 | << getCudaConfigureFuncName(); |
9951 | Context.setcudaConfigureCallDecl(NewFD); |
9952 | } |
9953 | |
9954 | |
9955 | |
9956 | |
9957 | if (!getLangOpts().CUDAAllowVariadicFunctions && NewFD->isVariadic() && |
9958 | (NewFD->hasAttr<CUDADeviceAttr>() || |
9959 | NewFD->hasAttr<CUDAGlobalAttr>()) && |
9960 | !(II && II->isStr("printf") && NewFD->isExternC() && |
9961 | !D.isFunctionDefinition())) { |
9962 | Diag(NewFD->getLocation(), diag::err_variadic_device_fn); |
9963 | } |
9964 | } |
9965 | |
9966 | MarkUnusedFileScopedDecl(NewFD); |
9967 | |
9968 | |
9969 | |
9970 | if (getLangOpts().OpenCL && NewFD->hasAttr<OpenCLKernelAttr>()) { |
9971 | |
9972 | if ((getLangOpts().OpenCLVersion >= 120) |
9973 | && (SC == SC_Static)) { |
9974 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); |
9975 | D.setInvalidType(); |
9976 | } |
9977 | |
9978 | |
9979 | if (!NewFD->getReturnType()->isVoidType()) { |
9980 | SourceRange RTRange = NewFD->getReturnTypeSourceRange(); |
9981 | Diag(D.getIdentifierLoc(), diag::err_expected_kernel_void_return_type) |
9982 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
9983 | : FixItHint()); |
9984 | D.setInvalidType(); |
9985 | } |
9986 | |
9987 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; |
9988 | for (auto Param : NewFD->parameters()) |
9989 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
9990 | |
9991 | if (getLangOpts().OpenCLCPlusPlus) { |
9992 | if (DC->isRecord()) { |
9993 | Diag(D.getIdentifierLoc(), diag::err_method_kernel); |
9994 | D.setInvalidType(); |
9995 | } |
9996 | if (FunctionTemplate) { |
9997 | Diag(D.getIdentifierLoc(), diag::err_template_kernel); |
9998 | D.setInvalidType(); |
9999 | } |
10000 | } |
10001 | } |
10002 | |
10003 | if (getLangOpts().CPlusPlus) { |
10004 | if (FunctionTemplate) { |
10005 | if (NewFD->isInvalidDecl()) |
10006 | FunctionTemplate->setInvalidDecl(); |
10007 | return FunctionTemplate; |
10008 | } |
10009 | |
10010 | if (isMemberSpecialization && !NewFD->isInvalidDecl()) |
10011 | CompleteMemberSpecialization(NewFD, Previous); |
10012 | } |
10013 | |
10014 | for (const ParmVarDecl *Param : NewFD->parameters()) { |
10015 | QualType PT = Param->getType(); |
10016 | |
10017 | |
10018 | |
10019 | if (getLangOpts().getOpenCLCompatibleVersion() >= 200) { |
10020 | if(const PipeType *PipeTy = PT->getAs<PipeType>()) { |
10021 | QualType ElemTy = PipeTy->getElementType(); |
10022 | if (ElemTy->isReferenceType() || ElemTy->isPointerType()) { |
10023 | Diag(Param->getTypeSpecStartLoc(), diag::err_reference_pipe_type ); |
10024 | D.setInvalidType(); |
10025 | } |
10026 | } |
10027 | } |
10028 | } |
10029 | |
10030 | |
10031 | |
10032 | |
10033 | if (isDependentClassScopeExplicitSpecialization) { |
10034 | ClassScopeFunctionSpecializationDecl *NewSpec = |
10035 | ClassScopeFunctionSpecializationDecl::Create( |
10036 | Context, CurContext, NewFD->getLocation(), |
10037 | cast<CXXMethodDecl>(NewFD), |
10038 | HasExplicitTemplateArgs, TemplateArgs); |
10039 | CurContext->addDecl(NewSpec); |
10040 | AddToScope = false; |
10041 | } |
10042 | |
10043 | |
10044 | |
10045 | if (const auto *attr = NewFD->getAttr<AvailabilityAttr>()) { |
10046 | if (NewFD->hasAttr<ConstructorAttr>()) { |
10047 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
10048 | << 1; |
10049 | NewFD->dropAttr<AvailabilityAttr>(); |
10050 | } |
10051 | if (NewFD->hasAttr<DestructorAttr>()) { |
10052 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
10053 | << 2; |
10054 | NewFD->dropAttr<AvailabilityAttr>(); |
10055 | } |
10056 | } |
10057 | |
10058 | |
10059 | |
10060 | |
10061 | |
10062 | |
10063 | |
10064 | |
10065 | if (const auto *NBA = NewFD->getAttr<NoBuiltinAttr>()) |
10066 | switch (D.getFunctionDefinitionKind()) { |
10067 | case FunctionDefinitionKind::Defaulted: |
10068 | case FunctionDefinitionKind::Deleted: |
10069 | Diag(NBA->getLocation(), |
10070 | diag::err_attribute_no_builtin_on_defaulted_deleted_function) |
10071 | << NBA->getSpelling(); |
10072 | break; |
10073 | case FunctionDefinitionKind::Declaration: |
10074 | Diag(NBA->getLocation(), diag::err_attribute_no_builtin_on_non_definition) |
10075 | << NBA->getSpelling(); |
10076 | break; |
10077 | case FunctionDefinitionKind::Definition: |
10078 | break; |
10079 | } |
10080 | |
10081 | return NewFD; |
10082 | } |
10083 | |
10084 | |
10085 | |
10086 | |
10087 | |
10088 | |
10089 | |
10090 | |
10091 | |
10092 | |
10093 | |
10094 | static Attr *getImplicitCodeSegAttrFromClass(Sema &S, const FunctionDecl *FD) { |
10095 | const auto *Method = dyn_cast<CXXMethodDecl>(FD); |
10096 | if (!Method) |
10097 | return nullptr; |
10098 | const CXXRecordDecl *Parent = Method->getParent(); |
10099 | if (const auto *SAttr = Parent->getAttr<CodeSegAttr>()) { |
10100 | Attr *NewAttr = SAttr->clone(S.getASTContext()); |
10101 | NewAttr->setImplicit(true); |
10102 | return NewAttr; |
10103 | } |
10104 | |
10105 | |
10106 | |
10107 | if (S.CodeSegStack.CurrentValue) |
10108 | return nullptr; |
10109 | |
10110 | while ((Parent = dyn_cast<CXXRecordDecl>(Parent->getParent()))) { |
10111 | if (const auto *SAttr = Parent->getAttr<CodeSegAttr>()) { |
10112 | Attr *NewAttr = SAttr->clone(S.getASTContext()); |
10113 | NewAttr->setImplicit(true); |
10114 | return NewAttr; |
10115 | } |
10116 | } |
10117 | return nullptr; |
10118 | } |
10119 | |
10120 | |
10121 | |
10122 | |
10123 | |
10124 | |
10125 | |
10126 | |
10127 | |
10128 | |
10129 | Attr *Sema::getImplicitCodeSegOrSectionAttrForFunction(const FunctionDecl *FD, |
10130 | bool IsDefinition) { |
10131 | if (Attr *A = getImplicitCodeSegAttrFromClass(*this, FD)) |
10132 | return A; |
10133 | if (!FD->hasAttr<SectionAttr>() && IsDefinition && |
10134 | CodeSegStack.CurrentValue) |
10135 | return SectionAttr::CreateImplicit( |
10136 | getASTContext(), CodeSegStack.CurrentValue->getString(), |
10137 | CodeSegStack.CurrentPragmaLocation, AttributeCommonInfo::AS_Pragma, |
10138 | SectionAttr::Declspec_allocate); |
10139 | return nullptr; |
10140 | } |
10141 | |
10142 | |
10143 | |
10144 | |
10145 | |
10146 | |
10147 | |
10148 | |
10149 | |
10150 | bool Sema::canFullyTypeCheckRedeclaration(ValueDecl *NewD, ValueDecl *OldD, |
10151 | QualType NewT, QualType OldT) { |
10152 | if (!NewD->getLexicalDeclContext()->isDependentContext()) |
10153 | return true; |
10154 | |
10155 | |
10156 | |
10157 | |
10158 | |
10159 | |
10160 | |
10161 | |
10162 | if (NewT->isDependentType() && |
10163 | (NewD->isLocalExternDecl() || NewD->getFriendObjectKind())) |
10164 | return false; |
10165 | |
10166 | |
10167 | |
10168 | if (OldT->isDependentType() && OldD->isLocalExternDecl()) |
10169 | return false; |
10170 | |
10171 | return true; |
10172 | } |
10173 | |
10174 | |
10175 | |
10176 | |
10177 | |
10178 | |
10179 | |
10180 | |
10181 | |
10182 | |
10183 | bool Sema::shouldLinkDependentDeclWithPrevious(Decl *D, Decl *PrevDecl) { |
10184 | if (!D->getLexicalDeclContext()->isDependentContext()) |
10185 | return true; |
10186 | |
10187 | |
10188 | |
10189 | |
10190 | |
10191 | |
10192 | |
10193 | |
10194 | |
10195 | |
10196 | |
10197 | |
10198 | |
10199 | |
10200 | if (D->getFriendObjectKind() && D->getDeclContext()->isFileContext()) |
10201 | return false; |
10202 | |
10203 | auto *VD = dyn_cast<ValueDecl>(D); |
10204 | auto *PrevVD = dyn_cast<ValueDecl>(PrevDecl); |
10205 | return !VD || !PrevVD || |
10206 | canFullyTypeCheckRedeclaration(VD, PrevVD, VD->getType(), |
10207 | PrevVD->getType()); |
10208 | } |
10209 | |
10210 | |
10211 | |
10212 | |
10213 | |
10214 | static bool CheckMultiVersionValue(Sema &S, const FunctionDecl *FD) { |
10215 | const auto *TA = FD->getAttr<TargetAttr>(); |
10216 | assert(TA && "MultiVersion Candidate requires a target attribute"); |
10217 | ParsedTargetAttr ParseInfo = TA->parse(); |
10218 | const TargetInfo &TargetInfo = S.Context.getTargetInfo(); |
10219 | enum ErrType { Feature = 0, Architecture = 1 }; |
10220 | |
10221 | if (!ParseInfo.Architecture.empty() && |
10222 | !TargetInfo.validateCpuIs(ParseInfo.Architecture)) { |
10223 | S.Diag(FD->getLocation(), diag::err_bad_multiversion_option) |
10224 | << Architecture << ParseInfo.Architecture; |
10225 | return true; |
10226 | } |
10227 | |
10228 | for (const auto &Feat : ParseInfo.Features) { |
10229 | auto BareFeat = StringRef{Feat}.substr(1); |
10230 | if (Feat[0] == '-') { |
10231 | S.Diag(FD->getLocation(), diag::err_bad_multiversion_option) |
10232 | << Feature << ("no-" + BareFeat).str(); |
10233 | return true; |
10234 | } |
10235 | |
10236 | if (!TargetInfo.validateCpuSupports(BareFeat) || |
10237 | !TargetInfo.isValidFeatureName(BareFeat)) { |
10238 | S.Diag(FD->getLocation(), diag::err_bad_multiversion_option) |
10239 | << Feature << BareFeat; |
10240 | return true; |
10241 | } |
10242 | } |
10243 | return false; |
10244 | } |
10245 | |
10246 | |
10247 | |
10248 | static bool AttrCompatibleWithMultiVersion(attr::Kind Kind, |
10249 | MultiVersionKind MVType) { |
10250 | |
10251 | |
10252 | switch (Kind) { |
10253 | default: |
10254 | return false; |
10255 | case attr::Used: |
10256 | return MVType == MultiVersionKind::Target; |
10257 | case attr::NonNull: |
10258 | case attr::NoThrow: |
10259 | return true; |
10260 | } |
10261 | } |
10262 | |
10263 | static bool checkNonMultiVersionCompatAttributes(Sema &S, |
10264 | const FunctionDecl *FD, |
10265 | const FunctionDecl *CausedFD, |
10266 | MultiVersionKind MVType) { |
10267 | bool IsCPUSpecificCPUDispatchMVType = |
10268 | MVType == MultiVersionKind::CPUDispatch || |
10269 | MVType == MultiVersionKind::CPUSpecific; |
10270 | const auto Diagnose = [FD, CausedFD, IsCPUSpecificCPUDispatchMVType]( |
10271 | Sema &S, const Attr *A) { |
10272 | S.Diag(FD->getLocation(), diag::err_multiversion_disallowed_other_attr) |
10273 | << IsCPUSpecificCPUDispatchMVType << A; |
10274 | if (CausedFD) |
10275 | S.Diag(CausedFD->getLocation(), diag::note_multiversioning_caused_here); |
10276 | return true; |
10277 | }; |
10278 | |
10279 | for (const Attr *A : FD->attrs()) { |
10280 | switch (A->getKind()) { |
10281 | case attr::CPUDispatch: |
10282 | case attr::CPUSpecific: |
10283 | if (MVType != MultiVersionKind::CPUDispatch && |
10284 | MVType != MultiVersionKind::CPUSpecific) |
10285 | return Diagnose(S, A); |
10286 | break; |
10287 | case attr::Target: |
10288 | if (MVType != MultiVersionKind::Target) |
10289 | return Diagnose(S, A); |
10290 | break; |
10291 | default: |
10292 | if (!AttrCompatibleWithMultiVersion(A->getKind(), MVType)) |
10293 | return Diagnose(S, A); |
10294 | break; |
10295 | } |
10296 | } |
10297 | return false; |
10298 | } |
10299 | |
10300 | bool Sema::areMultiversionVariantFunctionsCompatible( |
10301 | const FunctionDecl *OldFD, const FunctionDecl *NewFD, |
10302 | const PartialDiagnostic &NoProtoDiagID, |
10303 | const PartialDiagnosticAt &NoteCausedDiagIDAt, |
10304 | const PartialDiagnosticAt &NoSupportDiagIDAt, |
10305 | const PartialDiagnosticAt &DiffDiagIDAt, bool TemplatesSupported, |
10306 | bool ConstexprSupported, bool CLinkageMayDiffer) { |
10307 | enum DoesntSupport { |
10308 | FuncTemplates = 0, |
10309 | VirtFuncs = 1, |
10310 | DeducedReturn = 2, |
10311 | Constructors = 3, |
10312 | Destructors = 4, |
10313 | DeletedFuncs = 5, |
10314 | DefaultedFuncs = 6, |
10315 | ConstexprFuncs = 7, |
10316 | ConstevalFuncs = 8, |
10317 | }; |
10318 | enum Different { |
10319 | CallingConv = 0, |
10320 | ReturnType = 1, |
10321 | ConstexprSpec = 2, |
10322 | InlineSpec = 3, |
10323 | StorageClass = 4, |
10324 | Linkage = 5, |
10325 | }; |
10326 | |
10327 | if (NoProtoDiagID.getDiagID() != 0 && OldFD && |
10328 | !OldFD->getType()->getAs<FunctionProtoType>()) { |
10329 | Diag(OldFD->getLocation(), NoProtoDiagID); |
10330 | Diag(NoteCausedDiagIDAt.first, NoteCausedDiagIDAt.second); |
10331 | return true; |
10332 | } |
10333 | |
10334 | if (NoProtoDiagID.getDiagID() != 0 && |
10335 | !NewFD->getType()->getAs<FunctionProtoType>()) |
10336 | return Diag(NewFD->getLocation(), NoProtoDiagID); |
10337 | |
10338 | if (!TemplatesSupported && |
10339 | NewFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
10340 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10341 | << FuncTemplates; |
10342 | |
10343 | if (const auto *NewCXXFD = dyn_cast<CXXMethodDecl>(NewFD)) { |
10344 | if (NewCXXFD->isVirtual()) |
10345 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10346 | << VirtFuncs; |
10347 | |
10348 | if (isa<CXXConstructorDecl>(NewCXXFD)) |
10349 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10350 | << Constructors; |
10351 | |
10352 | if (isa<CXXDestructorDecl>(NewCXXFD)) |
10353 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10354 | << Destructors; |
10355 | } |
10356 | |
10357 | if (NewFD->isDeleted()) |
10358 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10359 | << DeletedFuncs; |
10360 | |
10361 | if (NewFD->isDefaulted()) |
10362 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10363 | << DefaultedFuncs; |
10364 | |
10365 | if (!ConstexprSupported && NewFD->isConstexpr()) |
10366 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10367 | << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); |
10368 | |
10369 | QualType NewQType = Context.getCanonicalType(NewFD->getType()); |
10370 | const auto *NewType = cast<FunctionType>(NewQType); |
10371 | QualType NewReturnType = NewType->getReturnType(); |
10372 | |
10373 | if (NewReturnType->isUndeducedType()) |
10374 | return Diag(NoSupportDiagIDAt.first, NoSupportDiagIDAt.second) |
10375 | << DeducedReturn; |
10376 | |
10377 | |
10378 | if (OldFD) { |
10379 | QualType OldQType = Context.getCanonicalType(OldFD->getType()); |
10380 | const auto *OldType = cast<FunctionType>(OldQType); |
10381 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
10382 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); |
10383 | |
10384 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) |
10385 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << CallingConv; |
10386 | |
10387 | QualType OldReturnType = OldType->getReturnType(); |
10388 | |
10389 | if (OldReturnType != NewReturnType) |
10390 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << ReturnType; |
10391 | |
10392 | if (OldFD->getConstexprKind() != NewFD->getConstexprKind()) |
10393 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << ConstexprSpec; |
10394 | |
10395 | if (OldFD->isInlineSpecified() != NewFD->isInlineSpecified()) |
10396 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << InlineSpec; |
10397 | |
10398 | if (OldFD->getStorageClass() != NewFD->getStorageClass()) |
10399 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << StorageClass; |
10400 | |
10401 | if (!CLinkageMayDiffer && OldFD->isExternC() != NewFD->isExternC()) |
10402 | return Diag(DiffDiagIDAt.first, DiffDiagIDAt.second) << Linkage; |
10403 | |
10404 | if (CheckEquivalentExceptionSpec( |
10405 | OldFD->getType()->getAs<FunctionProtoType>(), OldFD->getLocation(), |
10406 | NewFD->getType()->getAs<FunctionProtoType>(), NewFD->getLocation())) |
10407 | return true; |
10408 | } |
10409 | return false; |
10410 | } |
10411 | |
10412 | static bool CheckMultiVersionAdditionalRules(Sema &S, const FunctionDecl *OldFD, |
10413 | const FunctionDecl *NewFD, |
10414 | bool CausesMV, |
10415 | MultiVersionKind MVType) { |
10416 | if (!S.getASTContext().getTargetInfo().supportsMultiVersioning()) { |
10417 | S.Diag(NewFD->getLocation(), diag::err_multiversion_not_supported); |
10418 | if (OldFD) |
10419 | S.Diag(OldFD->getLocation(), diag::note_previous_declaration); |
10420 | return true; |
10421 | } |
10422 | |
10423 | bool IsCPUSpecificCPUDispatchMVType = |
10424 | MVType == MultiVersionKind::CPUDispatch || |
10425 | MVType == MultiVersionKind::CPUSpecific; |
10426 | |
10427 | if (CausesMV && OldFD && |
10428 | checkNonMultiVersionCompatAttributes(S, OldFD, NewFD, MVType)) |
10429 | return true; |
10430 | |
10431 | if (checkNonMultiVersionCompatAttributes(S, NewFD, nullptr, MVType)) |
10432 | return true; |
10433 | |
10434 | |
10435 | if (OldFD && CausesMV && OldFD->isUsed(false)) |
10436 | return S.Diag(NewFD->getLocation(), diag::err_multiversion_after_used); |
10437 | |
10438 | return S.areMultiversionVariantFunctionsCompatible( |
10439 | OldFD, NewFD, S.PDiag(diag::err_multiversion_noproto), |
10440 | PartialDiagnosticAt(NewFD->getLocation(), |
10441 | S.PDiag(diag::note_multiversioning_caused_here)), |
10442 | PartialDiagnosticAt(NewFD->getLocation(), |
10443 | S.PDiag(diag::err_multiversion_doesnt_support) |
10444 | << IsCPUSpecificCPUDispatchMVType), |
10445 | PartialDiagnosticAt(NewFD->getLocation(), |
10446 | S.PDiag(diag::err_multiversion_diff)), |
10447 | false, |
10448 | !IsCPUSpecificCPUDispatchMVType, |
10449 | false); |
10450 | } |
10451 | |
10452 | |
10453 | |
10454 | |
10455 | |
10456 | |
10457 | |
10458 | static bool CheckMultiVersionFirstFunction(Sema &S, FunctionDecl *FD, |
10459 | MultiVersionKind MVType, |
10460 | const TargetAttr *TA) { |
10461 | assert(MVType != MultiVersionKind::None && |
10462 | "Function lacks multiversion attribute"); |
10463 | |
10464 | |
10465 | |
10466 | if (MVType == MultiVersionKind::Target && !TA->isDefaultVersion()) |
10467 | return false; |
10468 | |
10469 | if (MVType == MultiVersionKind::Target && CheckMultiVersionValue(S, FD)) { |
10470 | FD->setInvalidDecl(); |
10471 | return true; |
10472 | } |
10473 | |
10474 | if (CheckMultiVersionAdditionalRules(S, nullptr, FD, true, MVType)) { |
10475 | FD->setInvalidDecl(); |
10476 | return true; |
10477 | } |
10478 | |
10479 | FD->setIsMultiVersion(); |
10480 | return false; |
10481 | } |
10482 | |
10483 | static bool PreviousDeclsHaveMultiVersionAttribute(const FunctionDecl *FD) { |
10484 | for (const Decl *D = FD->getPreviousDecl(); D; D = D->getPreviousDecl()) { |
10485 | if (D->getAsFunction()->getMultiVersionKind() != MultiVersionKind::None) |
10486 | return true; |
10487 | } |
10488 | |
10489 | return false; |
10490 | } |
10491 | |
10492 | static bool CheckTargetCausesMultiVersioning( |
10493 | Sema &S, FunctionDecl *OldFD, FunctionDecl *NewFD, const TargetAttr *NewTA, |
10494 | bool &Redeclaration, NamedDecl *&OldDecl, bool &MergeTypeWithPrevious, |
10495 | LookupResult &Previous) { |
10496 | const auto *OldTA = OldFD->getAttr<TargetAttr>(); |
10497 | ParsedTargetAttr NewParsed = NewTA->parse(); |
10498 | |
10499 | llvm::sort(NewParsed.Features); |
10500 | |
10501 | |
10502 | |
10503 | if (!NewTA->isDefaultVersion() && |
10504 | (!OldTA || OldTA->getFeaturesStr() == NewTA->getFeaturesStr())) |
10505 | return false; |
10506 | |
10507 | |
10508 | if (!S.getASTContext().getTargetInfo().supportsMultiVersioning()) { |
10509 | S.Diag(NewFD->getLocation(), diag::err_multiversion_not_supported); |
10510 | S.Diag(OldFD->getLocation(), diag::note_previous_declaration); |
10511 | NewFD->setInvalidDecl(); |
10512 | return true; |
10513 | } |
10514 | |
10515 | if (CheckMultiVersionAdditionalRules(S, OldFD, NewFD, true, |
10516 | MultiVersionKind::Target)) { |
10517 | NewFD->setInvalidDecl(); |
10518 | return true; |
10519 | } |
10520 | |
10521 | if (CheckMultiVersionValue(S, NewFD)) { |
10522 | NewFD->setInvalidDecl(); |
10523 | return true; |
10524 | } |
10525 | |
10526 | |
10527 | if (!OldFD->isMultiVersion() && !OldTA && NewTA->isDefaultVersion()) { |
10528 | Redeclaration = true; |
10529 | OldDecl = OldFD; |
10530 | OldFD->setIsMultiVersion(); |
10531 | NewFD->setIsMultiVersion(); |
10532 | return false; |
10533 | } |
10534 | |
10535 | if (CheckMultiVersionValue(S, OldFD)) { |
10536 | S.Diag(NewFD->getLocation(), diag::note_multiversioning_caused_here); |
10537 | NewFD->setInvalidDecl(); |
10538 | return true; |
10539 | } |
10540 | |
10541 | ParsedTargetAttr OldParsed = OldTA->parse(std::less<std::string>()); |
10542 | |
10543 | if (OldParsed == NewParsed) { |
10544 | S.Diag(NewFD->getLocation(), diag::err_multiversion_duplicate); |
10545 | S.Diag(OldFD->getLocation(), diag::note_previous_declaration); |
10546 | NewFD->setInvalidDecl(); |
10547 | return true; |
10548 | } |
10549 | |
10550 | for (const auto *FD : OldFD->redecls()) { |
10551 | const auto *CurTA = FD->getAttr<TargetAttr>(); |
10552 | |
10553 | |
10554 | if (PreviousDeclsHaveMultiVersionAttribute(FD) && |
10555 | (!CurTA || CurTA->isInherited())) { |
10556 | S.Diag(FD->getLocation(), diag::err_multiversion_required_in_redecl) |
10557 | << 0; |
10558 | S.Diag(NewFD->getLocation(), diag::note_multiversioning_caused_here); |
10559 | NewFD->setInvalidDecl(); |
10560 | return true; |
10561 | } |
10562 | } |
10563 | |
10564 | OldFD->setIsMultiVersion(); |
10565 | NewFD->setIsMultiVersion(); |
10566 | Redeclaration = false; |
10567 | MergeTypeWithPrevious = false; |
10568 | OldDecl = nullptr; |
10569 | Previous.clear(); |
10570 | return false; |
10571 | } |
10572 | |
10573 | |
10574 | |
10575 | static bool CheckMultiVersionAdditionalDecl( |
10576 | Sema &S, FunctionDecl *OldFD, FunctionDecl *NewFD, |
10577 | MultiVersionKind NewMVType, const TargetAttr *NewTA, |
10578 | const CPUDispatchAttr *NewCPUDisp, const CPUSpecificAttr *NewCPUSpec, |
10579 | bool &Redeclaration, NamedDecl *&OldDecl, bool &MergeTypeWithPrevious, |
10580 | LookupResult &Previous) { |
10581 | |
10582 | MultiVersionKind OldMVType = OldFD->getMultiVersionKind(); |
10583 | |
10584 | if ((OldMVType == MultiVersionKind::Target && |
10585 | NewMVType != MultiVersionKind::Target) || |
10586 | (NewMVType == MultiVersionKind::Target && |
10587 | OldMVType != MultiVersionKind::Target)) { |
10588 | S.Diag(NewFD->getLocation(), diag::err_multiversion_types_mixed); |
10589 | S.Diag(OldFD->getLocation(), diag::note_previous_declaration); |
10590 | NewFD->setInvalidDecl(); |
10591 | return true; |
10592 | } |
10593 | |
10594 | ParsedTargetAttr NewParsed; |
10595 | if (NewTA) { |
10596 | NewParsed = NewTA->parse(); |
10597 | llvm::sort(NewParsed.Features); |
10598 | } |
10599 | |
10600 | bool UseMemberUsingDeclRules = |
10601 | S.CurContext->isRecord() && !NewFD->getFriendObjectKind(); |
10602 | |
10603 | |
10604 | |
10605 | for (NamedDecl *ND : Previous) { |
10606 | FunctionDecl *CurFD = ND->getAsFunction(); |
10607 | if (!CurFD) |
10608 | continue; |
10609 | if (S.IsOverload(NewFD, CurFD, UseMemberUsingDeclRules)) |
10610 | continue; |
10611 | |
10612 | if (NewMVType == MultiVersionKind::Target) { |
10613 | const auto *CurTA = CurFD->getAttr<TargetAttr>(); |
10614 | if (CurTA->getFeaturesStr() == NewTA->getFeaturesStr()) { |
10615 | NewFD->setIsMultiVersion(); |
10616 | Redeclaration = true; |
10617 | OldDecl = ND; |
10618 | return false; |
10619 | } |
10620 | |
10621 | ParsedTargetAttr CurParsed = CurTA->parse(std::less<std::string>()); |
10622 | if (CurParsed == NewParsed) { |
10623 | S.Diag(NewFD->getLocation(), diag::err_multiversion_duplicate); |
10624 | S.Diag(CurFD->getLocation(), diag::note_previous_declaration); |
10625 | NewFD->setInvalidDecl(); |
10626 | return true; |
10627 | } |
10628 | } else { |
10629 | const auto *CurCPUSpec = CurFD->getAttr<CPUSpecificAttr>(); |
10630 | const auto *CurCPUDisp = CurFD->getAttr<CPUDispatchAttr>(); |
10631 | |
10632 | |
10633 | |
10634 | if (NewMVType == MultiVersionKind::CPUDispatch && |
10635 | CurFD->hasAttr<CPUDispatchAttr>()) { |
10636 | if (CurCPUDisp->cpus_size() == NewCPUDisp->cpus_size() && |
10637 | std::equal( |
10638 | CurCPUDisp->cpus_begin(), CurCPUDisp->cpus_end(), |
10639 | NewCPUDisp->cpus_begin(), |
10640 | [](const IdentifierInfo *Cur, const IdentifierInfo *New) { |
10641 | return Cur->getName() == New->getName(); |
10642 | })) { |
10643 | NewFD->setIsMultiVersion(); |
10644 | Redeclaration = true; |
10645 | OldDecl = ND; |
10646 | return false; |
10647 | } |
10648 | |
10649 | |
10650 | S.Diag(NewFD->getLocation(), diag::err_cpu_dispatch_mismatch); |
10651 | S.Diag(CurFD->getLocation(), diag::note_previous_declaration); |
10652 | NewFD->setInvalidDecl(); |
10653 | return true; |
10654 | } |
10655 | if (NewMVType == MultiVersionKind::CPUSpecific && CurCPUSpec) { |
10656 | |
10657 | if (CurCPUSpec->cpus_size() == NewCPUSpec->cpus_size() && |
10658 | std::equal( |
10659 | CurCPUSpec->cpus_begin(), CurCPUSpec->cpus_end(), |
10660 | NewCPUSpec->cpus_begin(), |
10661 | [](const IdentifierInfo *Cur, const IdentifierInfo *New) { |
10662 | return Cur->getName() == New->getName(); |
10663 | })) { |
10664 | NewFD->setIsMultiVersion(); |
10665 | Redeclaration = true; |
10666 | OldDecl = ND; |
10667 | return false; |
10668 | } |
10669 | |
10670 | |
10671 | for (const IdentifierInfo *CurII : CurCPUSpec->cpus()) { |
10672 | for (const IdentifierInfo *NewII : NewCPUSpec->cpus()) { |
10673 | if (CurII == NewII) { |
10674 | S.Diag(NewFD->getLocation(), diag::err_cpu_specific_multiple_defs) |
10675 | << NewII; |
10676 | S.Diag(CurFD->getLocation(), diag::note_previous_declaration); |
10677 | NewFD->setInvalidDecl(); |
10678 | return true; |
10679 | } |
10680 | } |
10681 | } |
10682 | } |
10683 | |
10684 | |
10685 | } |
10686 | } |
10687 | |
10688 | |
10689 | |
10690 | |
10691 | if (NewMVType == MultiVersionKind::Target && |
10692 | CheckMultiVersionValue(S, NewFD)) { |
10693 | NewFD->setInvalidDecl(); |
10694 | return true; |
10695 | } |
10696 | |
10697 | if (CheckMultiVersionAdditionalRules(S, OldFD, NewFD, |
10698 | !OldFD->isMultiVersion(), NewMVType)) { |
10699 | NewFD->setInvalidDecl(); |
10700 | return true; |
10701 | } |
10702 | |
10703 | |
10704 | if (!OldFD->isMultiVersion()) { |
10705 | OldFD->setIsMultiVersion(); |
10706 | NewFD->setIsMultiVersion(); |
10707 | Redeclaration = true; |
10708 | OldDecl = OldFD; |
10709 | return false; |
10710 | } |
10711 | |
10712 | NewFD->setIsMultiVersion(); |
10713 | Redeclaration = false; |
10714 | MergeTypeWithPrevious = false; |
10715 | OldDecl = nullptr; |
10716 | Previous.clear(); |
10717 | return false; |
10718 | } |
10719 | |
10720 | |
10721 | |
10722 | |
10723 | |
10724 | |
10725 | |
10726 | |
10727 | static bool CheckMultiVersionFunction(Sema &S, FunctionDecl *NewFD, |
10728 | bool &Redeclaration, NamedDecl *&OldDecl, |
10729 | bool &MergeTypeWithPrevious, |
10730 | LookupResult &Previous) { |
10731 | const auto *NewTA = NewFD->getAttr<TargetAttr>(); |
10732 | const auto *NewCPUDisp = NewFD->getAttr<CPUDispatchAttr>(); |
10733 | const auto *NewCPUSpec = NewFD->getAttr<CPUSpecificAttr>(); |
10734 | |
10735 | |
10736 | if ((NewTA && NewCPUDisp) || (NewTA && NewCPUSpec) || |
10737 | (NewCPUDisp && NewCPUSpec)) { |
10738 | S.Diag(NewFD->getLocation(), diag::err_multiversion_types_mixed); |
10739 | NewFD->setInvalidDecl(); |
10740 | return true; |
10741 | } |
10742 | |
10743 | MultiVersionKind MVType = NewFD->getMultiVersionKind(); |
10744 | |
10745 | |
10746 | |
10747 | if (NewFD->isMain()) { |
10748 | if ((MVType == MultiVersionKind::Target && NewTA->isDefaultVersion()) || |
10749 | MVType == MultiVersionKind::CPUDispatch || |
10750 | MVType == MultiVersionKind::CPUSpecific) { |
10751 | S.Diag(NewFD->getLocation(), diag::err_multiversion_not_allowed_on_main); |
10752 | NewFD->setInvalidDecl(); |
10753 | return true; |
10754 | } |
10755 | return false; |
10756 | } |
10757 | |
10758 | if (!OldDecl || !OldDecl->getAsFunction() || |
10759 | OldDecl->getDeclContext()->getRedeclContext() != |
10760 | NewFD->getDeclContext()->getRedeclContext()) { |
10761 | |
10762 | |
10763 | if (MVType == MultiVersionKind::None) |
10764 | return false; |
10765 | return CheckMultiVersionFirstFunction(S, NewFD, MVType, NewTA); |
10766 | } |
10767 | |
10768 | FunctionDecl *OldFD = OldDecl->getAsFunction(); |
10769 | |
10770 | if (!OldFD->isMultiVersion() && MVType == MultiVersionKind::None) |
10771 | return false; |
10772 | |
10773 | if (OldFD->isMultiVersion() && MVType == MultiVersionKind::None) { |
10774 | S.Diag(NewFD->getLocation(), diag::err_multiversion_required_in_redecl) |
10775 | << (OldFD->getMultiVersionKind() != MultiVersionKind::Target); |
10776 | NewFD->setInvalidDecl(); |
10777 | return true; |
10778 | } |
10779 | |
10780 | |
10781 | if (!OldFD->isMultiVersion() && MVType == MultiVersionKind::Target) |
10782 | return CheckTargetCausesMultiVersioning(S, OldFD, NewFD, NewTA, |
10783 | Redeclaration, OldDecl, |
10784 | MergeTypeWithPrevious, Previous); |
10785 | |
10786 | |
10787 | |
10788 | |
10789 | return CheckMultiVersionAdditionalDecl( |
10790 | S, OldFD, NewFD, MVType, NewTA, NewCPUDisp, NewCPUSpec, Redeclaration, |
10791 | OldDecl, MergeTypeWithPrevious, Previous); |
10792 | } |
10793 | |
10794 | |
10795 | |
10796 | |
10797 | |
10798 | |
10799 | |
10800 | |
10801 | |
10802 | |
10803 | |
10804 | |
10805 | |
10806 | |
10807 | |
10808 | |
10809 | |
10810 | |
10811 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
10812 | LookupResult &Previous, |
10813 | bool IsMemberSpecialization) { |
10814 | assert(!NewFD->getReturnType()->isVariablyModifiedType() && |
10815 | "Variably modified return types are not handled here"); |
10816 | |
10817 | |
10818 | |
10819 | |
10820 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && |
10821 | !Previous.isShadowed(); |
10822 | |
10823 | bool Redeclaration = false; |
10824 | NamedDecl *OldDecl = nullptr; |
10825 | bool MayNeedOverloadableChecks = false; |
10826 | |
10827 | |
10828 | |
10829 | if (!Previous.empty()) { |
10830 | |
10831 | |
10832 | |
10833 | |
10834 | if (!AllowOverloadingOfFunction(Previous, Context, NewFD)) { |
10835 | NamedDecl *Candidate = Previous.getRepresentativeDecl(); |
10836 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { |
10837 | Redeclaration = true; |
10838 | OldDecl = Candidate; |
10839 | } |
10840 | } else { |
10841 | MayNeedOverloadableChecks = true; |
10842 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
10843 | false)) { |
10844 | case Ovl_Match: |
10845 | Redeclaration = true; |
10846 | break; |
10847 | |
10848 | case Ovl_NonFunction: |
10849 | Redeclaration = true; |
10850 | break; |
10851 | |
10852 | case Ovl_Overload: |
10853 | Redeclaration = false; |
10854 | break; |
10855 | } |
10856 | } |
10857 | } |
10858 | |
10859 | |
10860 | if (!Redeclaration && |
10861 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { |
10862 | if (!Previous.empty()) { |
10863 | |
10864 | |
10865 | Redeclaration = true; |
10866 | OldDecl = Previous.getFoundDecl(); |
10867 | MergeTypeWithPrevious = false; |
10868 | |
10869 | |
10870 | if (OldDecl->hasAttr<OverloadableAttr>() || |
10871 | NewFD->hasAttr<OverloadableAttr>()) { |
10872 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { |
10873 | MayNeedOverloadableChecks = true; |
10874 | Redeclaration = false; |
10875 | OldDecl = nullptr; |
10876 | } |
10877 | } |
10878 | } |
10879 | } |
10880 | |
10881 | if (CheckMultiVersionFunction(*this, NewFD, Redeclaration, OldDecl, |
10882 | MergeTypeWithPrevious, Previous)) |
10883 | return Redeclaration; |
10884 | |
10885 | |
10886 | if (Context.getTargetInfo().getTriple().isPPC64() && |
10887 | CheckPPCMMAType(NewFD->getReturnType(), NewFD->getLocation())) { |
10888 | NewFD->setInvalidDecl(); |
10889 | } |
10890 | |
10891 | |
10892 | |
10893 | |
10894 | |
10895 | |
10896 | |
10897 | |
10898 | |
10899 | |
10900 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
10901 | if (!getLangOpts().CPlusPlus14 && MD && MD->isConstexpr() && |
10902 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && |
10903 | !isa<CXXDestructorDecl>(MD) && !MD->getMethodQualifiers().hasConst()) { |
10904 | CXXMethodDecl *OldMD = nullptr; |
10905 | if (OldDecl) |
10906 | OldMD = dyn_cast_or_null<CXXMethodDecl>(OldDecl->getAsFunction()); |
10907 | if (!OldMD || !OldMD->isStatic()) { |
10908 | const FunctionProtoType *FPT = |
10909 | MD->getType()->castAs<FunctionProtoType>(); |
10910 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
10911 | EPI.TypeQuals.addConst(); |
10912 | MD->setType(Context.getFunctionType(FPT->getReturnType(), |
10913 | FPT->getParamTypes(), EPI)); |
10914 | |
10915 | |
10916 | |
10917 | if (!inTemplateInstantiation()) { |
10918 | SourceLocation AddConstLoc; |
10919 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() |
10920 | .IgnoreParens().getAs<FunctionTypeLoc>()) |
10921 | AddConstLoc = getLocForEndOfToken(FTL.getRParenLoc()); |
10922 | |
10923 | Diag(MD->getLocation(), diag::warn_cxx14_compat_constexpr_not_const) |
10924 | << FixItHint::CreateInsertion(AddConstLoc, " const"); |
10925 | } |
10926 | } |
10927 | } |
10928 | |
10929 | if (Redeclaration) { |
10930 | |
10931 | |
10932 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
10933 | NewFD->setInvalidDecl(); |
10934 | return Redeclaration; |
10935 | } |
10936 | |
10937 | Previous.clear(); |
10938 | Previous.addDecl(OldDecl); |
10939 | |
10940 | if (FunctionTemplateDecl *OldTemplateDecl = |
10941 | dyn_cast<FunctionTemplateDecl>(OldDecl)) { |
10942 | auto *OldFD = OldTemplateDecl->getTemplatedDecl(); |
10943 | FunctionTemplateDecl *NewTemplateDecl |
10944 | = NewFD->getDescribedFunctionTemplate(); |
10945 | assert(NewTemplateDecl && "Template/non-template mismatch"); |
10946 | |
10947 | |
10948 | |
10949 | |
10950 | NewTemplateDecl->mergePrevDecl(OldTemplateDecl); |
10951 | |
10952 | NewFD->setPreviousDeclaration(OldFD); |
10953 | if (NewFD->isCXXClassMember()) { |
10954 | NewFD->setAccess(OldTemplateDecl->getAccess()); |
10955 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); |
10956 | } |
10957 | |
10958 | |
10959 | |
10960 | if (IsMemberSpecialization && |
10961 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { |
10962 | NewTemplateDecl->setMemberSpecialization(); |
10963 | assert(OldTemplateDecl->isMemberSpecialization()); |
10964 | |
10965 | |
10966 | if (OldFD->isDeleted()) { |
10967 | |
10968 | assert(OldFD->getCanonicalDecl() == OldFD); |
10969 | |
10970 | OldFD->setDeletedAsWritten(false); |
10971 | } |
10972 | } |
10973 | |
10974 | } else { |
10975 | if (shouldLinkDependentDeclWithPrevious(NewFD, OldDecl)) { |
10976 | auto *OldFD = cast<FunctionDecl>(OldDecl); |
10977 | |
10978 | NewFD->setPreviousDeclaration(OldFD); |
10979 | if (NewFD->isCXXClassMember()) |
10980 | NewFD->setAccess(OldFD->getAccess()); |
10981 | } |
10982 | } |
10983 | } else if (!getLangOpts().CPlusPlus && MayNeedOverloadableChecks && |
10984 | !NewFD->getAttr<OverloadableAttr>()) { |
10985 | assert((Previous.empty() || |
10986 | llvm::any_of(Previous, |
10987 | [](const NamedDecl *ND) { |
10988 | return ND->hasAttr<OverloadableAttr>(); |
10989 | })) && |
10990 | "Non-redecls shouldn't happen without overloadable present"); |
10991 | |
10992 | auto OtherUnmarkedIter = llvm::find_if(Previous, [](const NamedDecl *ND) { |
10993 | const auto *FD = dyn_cast<FunctionDecl>(ND); |
10994 | return FD && !FD->hasAttr<OverloadableAttr>(); |
10995 | }); |
10996 | |
10997 | if (OtherUnmarkedIter != Previous.end()) { |
10998 | Diag(NewFD->getLocation(), |
10999 | diag::err_attribute_overloadable_multiple_unmarked_overloads); |
11000 | Diag((*OtherUnmarkedIter)->getLocation(), |
11001 | diag::note_attribute_overloadable_prev_overload) |
11002 | << false; |
11003 | |
11004 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
11005 | } |
11006 | } |
11007 | |
11008 | if (LangOpts.OpenMP) |
11009 | ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(NewFD); |
11010 | |
11011 | |
11012 | |
11013 | if (getLangOpts().CPlusPlus) { |
11014 | |
11015 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { |
11016 | CheckConstructor(Constructor); |
11017 | } else if (CXXDestructorDecl *Destructor = |
11018 | dyn_cast<CXXDestructorDecl>(NewFD)) { |
11019 | CXXRecordDecl *Record = Destructor->getParent(); |
11020 | QualType ClassType = Context.getTypeDeclType(Record); |
11021 | |
11022 | |
11023 | |
11024 | if (!ClassType->isDependentType()) { |
11025 | DeclarationName Name |
11026 | = Context.DeclarationNames.getCXXDestructorName( |
11027 | Context.getCanonicalType(ClassType)); |
11028 | if (NewFD->getDeclName() != Name) { |
11029 | Diag(NewFD->getLocation(), diag::err_destructor_name); |
11030 | NewFD->setInvalidDecl(); |
11031 | return Redeclaration; |
11032 | } |
11033 | } |
11034 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(NewFD)) { |
11035 | if (auto *TD = Guide->getDescribedFunctionTemplate()) |
11036 | CheckDeductionGuideTemplate(TD); |
11037 | |
11038 | |
11039 | |
11040 | if (Guide->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
11041 | Diag(Guide->getBeginLoc(), diag::err_deduction_guide_specialized) |
11042 | << 1; |
11043 | } |
11044 | |
11045 | |
11046 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
11047 | if (!Method->isFunctionTemplateSpecialization() && |
11048 | !Method->getDescribedFunctionTemplate() && |
11049 | Method->isCanonicalDecl()) { |
11050 | AddOverriddenMethods(Method->getParent(), Method); |
11051 | } |
11052 | if (Method->isVirtual() && NewFD->getTrailingRequiresClause()) |
11053 | |
11054 | |
11055 | Diag(NewFD->getTrailingRequiresClause()->getBeginLoc(), |
11056 | diag::err_constrained_virtual_method); |
11057 | |
11058 | if (Method->isStatic()) |
11059 | checkThisInStaticMemberFunctionType(Method); |
11060 | } |
11061 | |
11062 | if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(NewFD)) |
11063 | ActOnConversionDeclarator(Conversion); |
11064 | |
11065 | |
11066 | if (NewFD->isOverloadedOperator() && |
11067 | CheckOverloadedOperatorDeclaration(NewFD)) { |
11068 | NewFD->setInvalidDecl(); |
11069 | return Redeclaration; |
11070 | } |
11071 | |
11072 | |
11073 | if (NewFD->getLiteralIdentifier() && |
11074 | CheckLiteralOperatorDeclaration(NewFD)) { |
11075 | NewFD->setInvalidDecl(); |
11076 | return Redeclaration; |
11077 | } |
11078 | |
11079 | |
11080 | |
11081 | |
11082 | if (!CurContext->isRecord()) |
11083 | CheckCXXDefaultArguments(NewFD); |
11084 | |
11085 | |
11086 | |
11087 | |
11088 | |
11089 | if (Previous.empty() && NewFD->isExternC()) { |
11090 | QualType R = NewFD->getReturnType(); |
11091 | if (R->isIncompleteType() && !R->isVoidType()) |
11092 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) |
11093 | << NewFD << R; |
11094 | else if (!R.isPODType(Context) && !R->isVoidType() && |
11095 | !R->isObjCObjectPointerType()) |
11096 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
11097 | } |
11098 | |
11099 | |
11100 | |
11101 | |
11102 | |
11103 | |
11104 | |
11105 | |
11106 | |
11107 | |
11108 | if (!getLangOpts().CPlusPlus17 && !NewFD->getPrimaryTemplate()) { |
11109 | auto HasNoexcept = [&](QualType T) -> bool { |
11110 | |
11111 | |
11112 | |
11113 | if (auto *RT = T->getAs<ReferenceType>()) |
11114 | T = RT->getPointeeType(); |
11115 | else if (T->isAnyPointerType()) |
11116 | T = T->getPointeeType(); |
11117 | else if (auto *MPT = T->getAs<MemberPointerType>()) |
11118 | T = MPT->getPointeeType(); |
11119 | if (auto *FPT = T->getAs<FunctionProtoType>()) |
11120 | if (FPT->isNothrow()) |
11121 | return true; |
11122 | return false; |
11123 | }; |
11124 | |
11125 | auto *FPT = NewFD->getType()->castAs<FunctionProtoType>(); |
11126 | bool AnyNoexcept = HasNoexcept(FPT->getReturnType()); |
11127 | for (QualType T : FPT->param_types()) |
11128 | AnyNoexcept |= HasNoexcept(T); |
11129 | if (AnyNoexcept) |
11130 | Diag(NewFD->getLocation(), |
11131 | diag::warn_cxx17_compat_exception_spec_in_signature) |
11132 | << NewFD; |
11133 | } |
11134 | |
11135 | if (!Redeclaration && LangOpts.CUDA) |
11136 | checkCUDATargetOverload(NewFD, Previous); |
11137 | } |
11138 | return Redeclaration; |
11139 | } |
11140 | |
11141 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
11142 | |
11143 | |
11144 | |
11145 | |
11146 | |
11147 | |
11148 | |
11149 | if (FD->getStorageClass() == SC_Static) |
11150 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
11151 | ? diag::err_static_main : diag::warn_static_main) |
11152 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
11153 | if (FD->isInlineSpecified()) |
11154 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) |
11155 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); |
11156 | if (DS.isNoreturnSpecified()) { |
11157 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); |
11158 | SourceRange NoreturnRange(NoreturnLoc, getLocForEndOfToken(NoreturnLoc)); |
11159 | Diag(NoreturnLoc, diag::ext_noreturn_main); |
11160 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) |
11161 | << FixItHint::CreateRemoval(NoreturnRange); |
11162 | } |
11163 | if (FD->isConstexpr()) { |
11164 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) |
11165 | << FD->isConsteval() |
11166 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); |
11167 | FD->setConstexprKind(ConstexprSpecKind::Unspecified); |
11168 | } |
11169 | |
11170 | if (getLangOpts().OpenCL) { |
11171 | Diag(FD->getLocation(), diag::err_opencl_no_main) |
11172 | << FD->hasAttr<OpenCLKernelAttr>(); |
11173 | FD->setInvalidDecl(); |
11174 | return; |
11175 | } |
11176 | |
11177 | QualType T = FD->getType(); |
11178 | assert(T->isFunctionType() && "function decl is not of function type"); |
11179 | const FunctionType* FT = T->castAs<FunctionType>(); |
11180 | |
11181 | |
11182 | if (FT->getCallConv() != CC_C) { |
11183 | FT = Context.adjustFunctionType(FT, FT->getExtInfo().withCallingConv(CC_C)); |
11184 | FD->setType(QualType(FT, 0)); |
11185 | T = Context.getCanonicalType(FD->getType()); |
11186 | } |
11187 | |
11188 | if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
11189 | |
11190 | |
11191 | |
11192 | |
11193 | |
11194 | if (Context.hasSameUnqualifiedType(FT->getReturnType(), Context.IntTy)) |
11195 | FD->setHasImplicitReturnZero(true); |
11196 | else { |
11197 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
11198 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
11199 | if (RTRange.isValid()) |
11200 | Diag(RTRange.getBegin(), diag::note_main_change_return_type) |
11201 | << FixItHint::CreateReplacement(RTRange, "int"); |
11202 | } |
11203 | } else { |
11204 | |
11205 | |
11206 | |
11207 | |
11208 | |
11209 | if (Context.hasSameType(FT->getReturnType(), Context.IntTy)) |
11210 | FD->setHasImplicitReturnZero(true); |
11211 | else { |
11212 | |
11213 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
11214 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) |
11215 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "int") |
11216 | : FixItHint()); |
11217 | FD->setInvalidDecl(true); |
11218 | } |
11219 | } |
11220 | |
11221 | |
11222 | if (isa<FunctionNoProtoType>(FT)) return; |
11223 | |
11224 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); |
11225 | unsigned nparams = FTP->getNumParams(); |
11226 | assert(FD->getNumParams() == nparams); |
11227 | |
11228 | bool HasExtraParameters = (nparams > 3); |
11229 | |
11230 | if (FTP->isVariadic()) { |
11231 | Diag(FD->getLocation(), diag::ext_variadic_main); |
11232 | |
11233 | |
11234 | } |
11235 | |
11236 | |
11237 | |
11238 | |
11239 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
11240 | HasExtraParameters = false; |
11241 | |
11242 | if (HasExtraParameters) { |
11243 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
11244 | FD->setInvalidDecl(true); |
11245 | nparams = 3; |
11246 | } |
11247 | |
11248 | |
11249 | |
11250 | |
11251 | QualType CharPP = |
11252 | Context.getPointerType(Context.getPointerType(Context.CharTy)); |
11253 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
11254 | |
11255 | for (unsigned i = 0; i < nparams; ++i) { |
11256 | QualType AT = FTP->getParamType(i); |
11257 | |
11258 | bool mismatch = true; |
11259 | |
11260 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) |
11261 | mismatch = false; |
11262 | else if (Expected[i] == CharPP) { |
11263 | |
11264 | |
11265 | |
11266 | |
11267 | |
11268 | QualifierCollector qs; |
11269 | const PointerType* PT; |
11270 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
11271 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && |
11272 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
11273 | Context.CharTy)) { |
11274 | qs.removeConst(); |
11275 | mismatch = !qs.empty(); |
11276 | } |
11277 | } |
11278 | |
11279 | if (mismatch) { |
11280 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; |
11281 | |
11282 | FD->setInvalidDecl(true); |
11283 | } |
11284 | } |
11285 | |
11286 | if (nparams == 1 && !FD->isInvalidDecl()) { |
11287 | Diag(FD->getLocation(), diag::warn_main_one_arg); |
11288 | } |
11289 | |
11290 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
11291 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
11292 | FD->setInvalidDecl(); |
11293 | } |
11294 | } |
11295 | |
11296 | static bool isDefaultStdCall(FunctionDecl *FD, Sema &S) { |
11297 | |
11298 | |
11299 | if (FD->getName() == "main" || FD->getName() == "wmain") |
11300 | return false; |
11301 | |
11302 | |
11303 | const llvm::Triple &T = S.Context.getTargetInfo().getTriple(); |
11304 | if (T.isWindowsGNUEnvironment()) |
11305 | return false; |
11306 | |
11307 | |
11308 | |
11309 | if (T.isOSWindows() && T.getArch() == llvm::Triple::x86) |
11310 | return true; |
11311 | |
11312 | return false; |
11313 | } |
11314 | |
11315 | void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) { |
11316 | QualType T = FD->getType(); |
11317 | assert(T->isFunctionType() && "function decl is not of function type"); |
11318 | const FunctionType *FT = T->castAs<FunctionType>(); |
11319 | |
11320 | |
11321 | |
11322 | if (FT->getReturnType()->isIntegralOrEnumerationType() || |
11323 | FT->getReturnType()->isAnyPointerType() || |
11324 | FT->getReturnType()->isNullPtrType()) |
11325 | |
11326 | if (FD->getName() != "DllMain") |
11327 | FD->setHasImplicitReturnZero(true); |
11328 | |
11329 | |
11330 | if (!hasExplicitCallingConv(T)) { |
11331 | if (isDefaultStdCall(FD, *this)) { |
11332 | if (FT->getCallConv() != CC_X86StdCall) { |
11333 | FT = Context.adjustFunctionType( |
11334 | FT, FT->getExtInfo().withCallingConv(CC_X86StdCall)); |
11335 | FD->setType(QualType(FT, 0)); |
11336 | } |
11337 | } else if (FT->getCallConv() != CC_C) { |
11338 | FT = Context.adjustFunctionType(FT, |
11339 | FT->getExtInfo().withCallingConv(CC_C)); |
11340 | FD->setType(QualType(FT, 0)); |
11341 | } |
11342 | } |
11343 | |
11344 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
11345 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
11346 | FD->setInvalidDecl(); |
11347 | } |
11348 | } |
11349 | |
11350 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
11351 | |
11352 | |
11353 | |
11354 | |
11355 | |
11356 | |
11357 | |
11358 | |
11359 | |
11360 | if (Init->isValueDependent()) { |
11361 | assert(Init->containsErrors() && |
11362 | "Dependent code should only occur in error-recovery path."); |
11363 | return true; |
11364 | } |
11365 | const Expr *Culprit; |
11366 | if (Init->isConstantInitializer(Context, false, &Culprit)) |
11367 | return false; |
11368 | Diag(Culprit->getExprLoc(), diag::err_init_element_not_constant) |
11369 | << Culprit->getSourceRange(); |
11370 | return true; |
11371 | } |
11372 | |
11373 | namespace { |
11374 | |
11375 | |
11376 | class SelfReferenceChecker |
11377 | : public EvaluatedExprVisitor<SelfReferenceChecker> { |
11378 | Sema &S; |
11379 | Decl *OrigDecl; |
11380 | bool isRecordType; |
11381 | bool isPODType; |
11382 | bool isReferenceType; |
11383 | |
11384 | bool isInitList; |
11385 | llvm::SmallVector<unsigned, 4> InitFieldIndex; |
11386 | |
11387 | public: |
11388 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; |
11389 | |
11390 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), |
11391 | S(S), OrigDecl(OrigDecl) { |
11392 | isPODType = false; |
11393 | isRecordType = false; |
11394 | isReferenceType = false; |
11395 | isInitList = false; |
11396 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
11397 | isPODType = VD->getType().isPODType(S.Context); |
11398 | isRecordType = VD->getType()->isRecordType(); |
11399 | isReferenceType = VD->getType()->isReferenceType(); |
11400 | } |
11401 | } |
11402 | |
11403 | |
11404 | |
11405 | |
11406 | void CheckExpr(Expr *E) { |
11407 | InitListExpr *InitList = dyn_cast<InitListExpr>(E); |
11408 | if (!InitList) { |
11409 | Visit(E); |
11410 | return; |
11411 | } |
11412 | |
11413 | |
11414 | isInitList = true; |
11415 | InitFieldIndex.push_back(0); |
11416 | for (auto Child : InitList->children()) { |
11417 | CheckExpr(cast<Expr>(Child)); |
11418 | ++InitFieldIndex.back(); |
11419 | } |
11420 | InitFieldIndex.pop_back(); |
11421 | } |
11422 | |
11423 | |
11424 | |
11425 | bool CheckInitListMemberExpr(MemberExpr *E, bool CheckReference) { |
11426 | llvm::SmallVector<FieldDecl*, 4> Fields; |
11427 | Expr *Base = E; |
11428 | bool ReferenceField = false; |
11429 | |
11430 | |
11431 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
11432 | FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
11433 | if (!FD) |
11434 | return false; |
11435 | Fields.push_back(FD); |
11436 | if (FD->getType()->isReferenceType()) |
11437 | ReferenceField = true; |
11438 | Base = ME->getBase()->IgnoreParenImpCasts(); |
11439 | } |
11440 | |
11441 | |
11442 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base); |
11443 | if (!DRE || DRE->getDecl() != OrigDecl) |
11444 | return false; |
11445 | |
11446 | |
11447 | if (CheckReference && !ReferenceField) |
11448 | return true; |
11449 | |
11450 | |
11451 | llvm::SmallVector<unsigned, 4> UsedFieldIndex; |
11452 | for (const FieldDecl *I : llvm::reverse(Fields)) |
11453 | UsedFieldIndex.push_back(I->getFieldIndex()); |
11454 | |
11455 | |
11456 | |
11457 | |
11458 | for (auto UsedIter = UsedFieldIndex.begin(), |
11459 | UsedEnd = UsedFieldIndex.end(), |
11460 | OrigIter = InitFieldIndex.begin(), |
11461 | OrigEnd = InitFieldIndex.end(); |
11462 | UsedIter != UsedEnd && OrigIter != OrigEnd; ++UsedIter, ++OrigIter) { |
11463 | if (*UsedIter < *OrigIter) |
11464 | return true; |
11465 | if (*UsedIter > *OrigIter) |
11466 | break; |
11467 | } |
11468 | |
11469 | |
11470 | HandleDeclRefExpr(DRE); |
11471 | return true; |
11472 | } |
11473 | |
11474 | |
11475 | |
11476 | |
11477 | void HandleValue(Expr *E) { |
11478 | E = E->IgnoreParens(); |
11479 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { |
11480 | HandleDeclRefExpr(DRE); |
11481 | return; |
11482 | } |
11483 | |
11484 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
11485 | Visit(CO->getCond()); |
11486 | HandleValue(CO->getTrueExpr()); |
11487 | HandleValue(CO->getFalseExpr()); |
11488 | return; |
11489 | } |
11490 | |
11491 | if (BinaryConditionalOperator *BCO = |
11492 | dyn_cast<BinaryConditionalOperator>(E)) { |
11493 | Visit(BCO->getCond()); |
11494 | HandleValue(BCO->getFalseExpr()); |
11495 | return; |
11496 | } |
11497 | |
11498 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
11499 | HandleValue(OVE->getSourceExpr()); |
11500 | return; |
11501 | } |
11502 | |
11503 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
11504 | if (BO->getOpcode() == BO_Comma) { |
11505 | Visit(BO->getLHS()); |
11506 | HandleValue(BO->getRHS()); |
11507 | return; |
11508 | } |
11509 | } |
11510 | |
11511 | if (isa<MemberExpr>(E)) { |
11512 | if (isInitList) { |
11513 | if (CheckInitListMemberExpr(cast<MemberExpr>(E), |
11514 | false )) |
11515 | return; |
11516 | } |
11517 | |
11518 | Expr *Base = E->IgnoreParenImpCasts(); |
11519 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
11520 | |
11521 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
11522 | return; |
11523 | Base = ME->getBase()->IgnoreParenImpCasts(); |
11524 | } |
11525 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) |
11526 | HandleDeclRefExpr(DRE); |
11527 | return; |
11528 | } |
11529 | |
11530 | Visit(E); |
11531 | } |
11532 | |
11533 | |
11534 | |
11535 | void VisitDeclRefExpr(DeclRefExpr *E) { |
11536 | if (isReferenceType) |
11537 | HandleDeclRefExpr(E); |
11538 | } |
11539 | |
11540 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
11541 | if (E->getCastKind() == CK_LValueToRValue) { |
11542 | HandleValue(E->getSubExpr()); |
11543 | return; |
11544 | } |
11545 | |
11546 | Inherited::VisitImplicitCastExpr(E); |
11547 | } |
11548 | |
11549 | void VisitMemberExpr(MemberExpr *E) { |
11550 | if (isInitList) { |
11551 | if (CheckInitListMemberExpr(E, true )) |
11552 | return; |
11553 | } |
11554 | |
11555 | |
11556 | if (E->getType()->canDecayToPointerType()) return; |
11557 | |
11558 | |
11559 | |
11560 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); |
11561 | bool Warn = (MD && !MD->isStatic()); |
11562 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
11563 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
11564 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
11565 | Warn = false; |
11566 | Base = ME->getBase()->IgnoreParenImpCasts(); |
11567 | } |
11568 | |
11569 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
11570 | if (Warn) |
11571 | HandleDeclRefExpr(DRE); |
11572 | return; |
11573 | } |
11574 | |
11575 | |
11576 | |
11577 | Visit(Base); |
11578 | } |
11579 | |
11580 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
11581 | Expr *Callee = E->getCallee(); |
11582 | |
11583 | if (isa<UnresolvedLookupExpr>(Callee)) |
11584 | return Inherited::VisitCXXOperatorCallExpr(E); |
11585 | |
11586 | Visit(Callee); |
11587 | for (auto Arg: E->arguments()) |
11588 | HandleValue(Arg->IgnoreParenImpCasts()); |
11589 | } |
11590 | |
11591 | void VisitUnaryOperator(UnaryOperator *E) { |
11592 | |
11593 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
11594 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { |
11595 | if (!isPODType) |
11596 | HandleValue(E->getSubExpr()); |
11597 | return; |
11598 | } |
11599 | |
11600 | if (E->isIncrementDecrementOp()) { |
11601 | HandleValue(E->getSubExpr()); |
11602 | return; |
11603 | } |
11604 | |
11605 | Inherited::VisitUnaryOperator(E); |
11606 | } |
11607 | |
11608 | void VisitObjCMessageExpr(ObjCMessageExpr *E) {} |
11609 | |
11610 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
11611 | if (E->getConstructor()->isCopyConstructor()) { |
11612 | Expr *ArgExpr = E->getArg(0); |
11613 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(ArgExpr)) |
11614 | if (ILE->getNumInits() == 1) |
11615 | ArgExpr = ILE->getInit(0); |
11616 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
11617 | if (ICE->getCastKind() == CK_NoOp) |
11618 | ArgExpr = ICE->getSubExpr(); |
11619 | HandleValue(ArgExpr); |
11620 | return; |
11621 | } |
11622 | Inherited::VisitCXXConstructExpr(E); |
11623 | } |
11624 | |
11625 | void VisitCallExpr(CallExpr *E) { |
11626 | |
11627 | if (E->isCallToStdMove()) { |
11628 | HandleValue(E->getArg(0)); |
11629 | return; |
11630 | } |
11631 | |
11632 | Inherited::VisitCallExpr(E); |
11633 | } |
11634 | |
11635 | void VisitBinaryOperator(BinaryOperator *E) { |
11636 | if (E->isCompoundAssignmentOp()) { |
11637 | HandleValue(E->getLHS()); |
11638 | Visit(E->getRHS()); |
11639 | return; |
11640 | } |
11641 | |
11642 | Inherited::VisitBinaryOperator(E); |
11643 | } |
11644 | |
11645 | |
11646 | |
11647 | |
11648 | void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
11649 | Visit(E->getCond()); |
11650 | Visit(E->getFalseExpr()); |
11651 | } |
11652 | |
11653 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
11654 | Decl* ReferenceDecl = DRE->getDecl(); |
11655 | if (OrigDecl != ReferenceDecl) return; |
11656 | unsigned diag; |
11657 | if (isReferenceType) { |
11658 | diag = diag::warn_uninit_self_reference_in_reference_init; |
11659 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { |
11660 | diag = diag::warn_static_self_reference_in_init; |
11661 | } else if (isa<TranslationUnitDecl>(OrigDecl->getDeclContext()) || |
11662 | isa<NamespaceDecl>(OrigDecl->getDeclContext()) || |
11663 | DRE->getDecl()->getType()->isRecordType()) { |
11664 | diag = diag::warn_uninit_self_reference_in_init; |
11665 | } else { |
11666 | |
11667 | return; |
11668 | } |
11669 | |
11670 | S.DiagRuntimeBehavior(DRE->getBeginLoc(), DRE, |
11671 | S.PDiag(diag) |
11672 | << DRE->getDecl() << OrigDecl->getLocation() |
11673 | << DRE->getSourceRange()); |
11674 | } |
11675 | }; |
11676 | |
11677 | |
11678 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, |
11679 | bool DirectInit) { |
11680 | |
11681 | |
11682 | if (isa<ParmVarDecl>(OrigDecl)) |
11683 | return; |
11684 | |
11685 | E = E->IgnoreParens(); |
11686 | |
11687 | |
11688 | |
11689 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) |
11690 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
11691 | if (ICE->getCastKind() == CK_LValueToRValue) |
11692 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) |
11693 | if (DRE->getDecl() == OrigDecl) |
11694 | return; |
11695 | |
11696 | SelfReferenceChecker(S, OrigDecl).CheckExpr(E); |
11697 | } |
11698 | } |
11699 | |
11700 | namespace { |
11701 | |
11702 | |
11703 | struct VarDeclOrName { |
11704 | VarDecl *VDecl; |
11705 | DeclarationName Name; |
11706 | |
11707 | friend const Sema::SemaDiagnosticBuilder & |
11708 | operator<<(const Sema::SemaDiagnosticBuilder &Diag, VarDeclOrName VN) { |
11709 | return VN.VDecl ? Diag << VN.VDecl : Diag << VN.Name; |
11710 | } |
11711 | }; |
11712 | } |
11713 | |
11714 | QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl, |
11715 | DeclarationName Name, QualType Type, |
11716 | TypeSourceInfo *TSI, |
11717 | SourceRange Range, bool DirectInit, |
11718 | Expr *Init) { |
11719 | bool IsInitCapture = !VDecl; |
11720 | assert((!VDecl || !VDecl->isInitCapture()) && |
11721 | "init captures are expected to be deduced prior to initialization"); |
11722 | |
11723 | VarDeclOrName VN{VDecl, Name}; |
11724 | |
11725 | DeducedType *Deduced = Type->getContainedDeducedType(); |
11726 | assert(Deduced && "deduceVarTypeFromInitializer for non-deduced type"); |
11727 | |
11728 | |
11729 | if (!Init) { |
| 23 | | Assuming 'Init' is non-null | |
|
| |
11730 | assert(VDecl && "no init for init capture deduction?"); |
11731 | |
11732 | |
11733 | |
11734 | if (!isa<DeducedTemplateSpecializationType>(Deduced) || |
11735 | VDecl->hasExternalStorage() || |
11736 | VDecl->isStaticDataMember()) { |
11737 | Diag(VDecl->getLocation(), diag::err_auto_var_requires_init) |
11738 | << VDecl->getDeclName() << Type; |
11739 | return QualType(); |
11740 | } |
11741 | } |
11742 | |
11743 | ArrayRef<Expr*> DeduceInits; |
11744 | if (Init) |
| |
11745 | DeduceInits = Init; |
| 26 | | Value assigned to 'Init' | |
|
11746 | |
11747 | if (DirectInit) { |
| 27 | | Assuming 'DirectInit' is true | |
|
| |
11748 | if (auto *PL = dyn_cast_or_null<ParenListExpr>(Init)) |
| 29 | | Assuming null pointer is passed into cast | |
|
| 30 | | Assuming pointer value is null | |
|
| |
11749 | DeduceInits = PL->exprs(); |
11750 | } |
11751 | |
11752 | if (isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 32 | | Assuming 'Deduced' is not a 'DeducedTemplateSpecializationType' | |
|
| |
11753 | assert(VDecl && "non-auto type for init capture deduction?"); |
11754 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
11755 | InitializationKind Kind = InitializationKind::CreateForInit( |
11756 | VDecl->getLocation(), DirectInit, Init); |
11757 | |
11758 | SmallVector<Expr*, 8> InitsCopy(DeduceInits.begin(), DeduceInits.end()); |
11759 | return DeduceTemplateSpecializationFromInitializer(TSI, Entity, Kind, |
11760 | InitsCopy); |
11761 | } |
11762 | |
11763 | if (DirectInit) { |
| |
11764 | if (auto *IL = dyn_cast<InitListExpr>(Init)) |
| |
| |
11765 | DeduceInits = IL->inits(); |
11766 | } |
11767 | |
11768 | |
11769 | if (DeduceInits.empty()) { |
| 37 | | Assuming the condition is true | |
|
| |
11770 | |
11771 | |
11772 | Diag(Init->getBeginLoc(), IsInitCapture |
| 39 | | Called C++ object pointer is null |
|
11773 | ? diag::err_init_capture_no_expression |
11774 | : diag::err_auto_var_init_no_expression) |
11775 | << VN << Type << Range; |
11776 | return QualType(); |
11777 | } |
11778 | |
11779 | if (DeduceInits.size() > 1) { |
11780 | Diag(DeduceInits[1]->getBeginLoc(), |
11781 | IsInitCapture ? diag::err_init_capture_multiple_expressions |
11782 | : diag::err_auto_var_init_multiple_expressions) |
11783 | << VN << Type << Range; |
11784 | return QualType(); |
11785 | } |
11786 | |
11787 | Expr *DeduceInit = DeduceInits[0]; |
11788 | if (DirectInit && isa<InitListExpr>(DeduceInit)) { |
11789 | Diag(Init->getBeginLoc(), IsInitCapture |
11790 | ? diag::err_init_capture_paren_braces |
11791 | : diag::err_auto_var_init_paren_braces) |
11792 | << isa<InitListExpr>(Init) << VN << Type << Range; |
11793 | return QualType(); |
11794 | } |
11795 | |
11796 | |
11797 | bool DefaultedAnyToId = false; |
11798 | if (getLangOpts().DebuggerCastResultToId && |
11799 | Init->getType() == Context.UnknownAnyTy && !IsInitCapture) { |
11800 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
11801 | if (Result.isInvalid()) { |
11802 | return QualType(); |
11803 | } |
11804 | Init = Result.get(); |
11805 | DefaultedAnyToId = true; |
11806 | } |
11807 | |
11808 | |
11809 | |
11810 | |
11811 | if (VDecl && isa<DecompositionDecl>(VDecl) && |
11812 | Context.hasSameUnqualifiedType(Type, Context.getAutoDeductType()) && |
11813 | DeduceInit->getType()->isConstantArrayType()) |
11814 | return Context.getQualifiedType(DeduceInit->getType(), |
11815 | Type.getQualifiers()); |
11816 | |
11817 | QualType DeducedType; |
11818 | if (DeduceAutoType(TSI, DeduceInit, DeducedType) == DAR_Failed) { |
11819 | if (!IsInitCapture) |
11820 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
11821 | else if (isa<InitListExpr>(Init)) |
11822 | Diag(Range.getBegin(), |
11823 | diag::err_init_capture_deduction_failure_from_init_list) |
11824 | << VN |
11825 | << (DeduceInit->getType().isNull() ? TSI->getType() |
11826 | : DeduceInit->getType()) |
11827 | << DeduceInit->getSourceRange(); |
11828 | else |
11829 | Diag(Range.getBegin(), diag::err_init_capture_deduction_failure) |
11830 | << VN << TSI->getType() |
11831 | << (DeduceInit->getType().isNull() ? TSI->getType() |
11832 | : DeduceInit->getType()) |
11833 | << DeduceInit->getSourceRange(); |
11834 | } |
11835 | |
11836 | |
11837 | |
11838 | |
11839 | |
11840 | |
11841 | if (!inTemplateInstantiation() && !DefaultedAnyToId && !IsInitCapture && |
11842 | !DeducedType.isNull() && DeducedType->isObjCIdType()) { |
11843 | SourceLocation Loc = TSI->getTypeLoc().getBeginLoc(); |
11844 | Diag(Loc, diag::warn_auto_var_is_id) << VN << Range; |
11845 | } |
11846 | |
11847 | return DeducedType; |
11848 | } |
11849 | |
11850 | bool Sema::DeduceVariableDeclarationType(VarDecl *VDecl, bool DirectInit, |
11851 | Expr *Init) { |
11852 | assert(!Init || !Init->containsErrors()); |
11853 | QualType DeducedType = deduceVarTypeFromInitializer( |
| 22 | | Calling 'Sema::deduceVarTypeFromInitializer' | |
|
11854 | VDecl, VDecl->getDeclName(), VDecl->getType(), VDecl->getTypeSourceInfo(), |
11855 | VDecl->getSourceRange(), DirectInit, Init); |
11856 | if (DeducedType.isNull()) { |
11857 | VDecl->setInvalidDecl(); |
11858 | return true; |
11859 | } |
11860 | |
11861 | VDecl->setType(DeducedType); |
11862 | assert(VDecl->isLinkageValid()); |
11863 | |
11864 | |
11865 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
11866 | VDecl->setInvalidDecl(); |
11867 | |
11868 | if (getLangOpts().OpenCL) |
11869 | deduceOpenCLAddressSpace(VDecl); |
11870 | |
11871 | |
11872 | |
11873 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
11874 | |
11875 | |
11876 | MergeVarDeclTypes(VDecl, Old, false); |
11877 | } |
11878 | |
11879 | |
11880 | CheckVariableDeclarationType(VDecl); |
11881 | return VDecl->isInvalidDecl(); |
11882 | } |
11883 | |
11884 | void Sema::checkNonTrivialCUnionInInitializer(const Expr *Init, |
11885 | SourceLocation Loc) { |
11886 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
11887 | Init = EWC->getSubExpr(); |
11888 | |
11889 | if (auto *CE = dyn_cast<ConstantExpr>(Init)) |
11890 | Init = CE->getSubExpr(); |
11891 | |
11892 | QualType InitType = Init->getType(); |
11893 | assert((InitType.hasNonTrivialToPrimitiveDefaultInitializeCUnion() || |
11894 | InitType.hasNonTrivialToPrimitiveCopyCUnion()) && |
11895 | "shouldn't be called if type doesn't have a non-trivial C struct"); |
11896 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) { |
11897 | for (auto I : ILE->inits()) { |
11898 | if (!I->getType().hasNonTrivialToPrimitiveDefaultInitializeCUnion() && |
11899 | !I->getType().hasNonTrivialToPrimitiveCopyCUnion()) |
11900 | continue; |
11901 | SourceLocation SL = I->getExprLoc(); |
11902 | checkNonTrivialCUnionInInitializer(I, SL.isValid() ? SL : Loc); |
11903 | } |
11904 | return; |
11905 | } |
11906 | |
11907 | if (isa<ImplicitValueInitExpr>(Init)) { |
11908 | if (InitType.hasNonTrivialToPrimitiveDefaultInitializeCUnion()) |
11909 | checkNonTrivialCUnion(InitType, Loc, NTCUC_DefaultInitializedObject, |
11910 | NTCUK_Init); |
11911 | } else { |
11912 | |
11913 | |
11914 | |
11915 | |
11916 | if (InitType.hasNonTrivialToPrimitiveCopyCUnion()) |
11917 | checkNonTrivialCUnion(InitType, Loc, NTCUC_CopyInit, NTCUK_Copy); |
11918 | } |
11919 | } |
11920 | |
11921 | namespace { |
11922 | |
11923 | bool shouldIgnoreForRecordTriviality(const FieldDecl *FD) { |
11924 | |
11925 | |
11926 | |
11927 | |
11928 | |
11929 | return FD->hasAttr<UnavailableAttr>(); |
11930 | } |
11931 | |
11932 | struct DiagNonTrivalCUnionDefaultInitializeVisitor |
11933 | : DefaultInitializedTypeVisitor<DiagNonTrivalCUnionDefaultInitializeVisitor, |
11934 | void> { |
11935 | using Super = |
11936 | DefaultInitializedTypeVisitor<DiagNonTrivalCUnionDefaultInitializeVisitor, |
11937 | void>; |
11938 | |
11939 | DiagNonTrivalCUnionDefaultInitializeVisitor( |
11940 | QualType OrigTy, SourceLocation OrigLoc, |
11941 | Sema::NonTrivialCUnionContext UseContext, Sema &S) |
11942 | : OrigTy(OrigTy), OrigLoc(OrigLoc), UseContext(UseContext), S(S) {} |
11943 | |
11944 | void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType QT, |
11945 | const FieldDecl *FD, bool InNonTrivialUnion) { |
11946 | if (const auto *AT = S.Context.getAsArrayType(QT)) |
11947 | return this->asDerived().visit(S.Context.getBaseElementType(AT), FD, |
11948 | InNonTrivialUnion); |
11949 | return Super::visitWithKind(PDIK, QT, FD, InNonTrivialUnion); |
11950 | } |
11951 | |
11952 | void visitARCStrong(QualType QT, const FieldDecl *FD, |
11953 | bool InNonTrivialUnion) { |
11954 | if (InNonTrivialUnion) |
11955 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
11956 | << 1 << 0 << QT << FD->getName(); |
11957 | } |
11958 | |
11959 | void visitARCWeak(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
11960 | if (InNonTrivialUnion) |
11961 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
11962 | << 1 << 0 << QT << FD->getName(); |
11963 | } |
11964 | |
11965 | void visitStruct(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
11966 | const RecordDecl *RD = QT->castAs<RecordType>()->getDecl(); |
11967 | if (RD->isUnion()) { |
11968 | if (OrigLoc.isValid()) { |
11969 | bool IsUnion = false; |
11970 | if (auto *OrigRD = OrigTy->getAsRecordDecl()) |
11971 | IsUnion = OrigRD->isUnion(); |
11972 | S.Diag(OrigLoc, diag::err_non_trivial_c_union_in_invalid_context) |
11973 | << 0 << OrigTy << IsUnion << UseContext; |
11974 | |
11975 | OrigLoc = SourceLocation(); |
11976 | } |
11977 | InNonTrivialUnion = true; |
11978 | } |
11979 | |
11980 | if (InNonTrivialUnion) |
11981 | S.Diag(RD->getLocation(), diag::note_non_trivial_c_union) |
11982 | << 0 << 0 << QT.getUnqualifiedType() << ""; |
11983 | |
11984 | for (const FieldDecl *FD : RD->fields()) |
11985 | if (!shouldIgnoreForRecordTriviality(FD)) |
11986 | asDerived().visit(FD->getType(), FD, InNonTrivialUnion); |
11987 | } |
11988 | |
11989 | void visitTrivial(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) {} |
11990 | |
11991 | |
11992 | |
11993 | QualType OrigTy; |
11994 | SourceLocation OrigLoc; |
11995 | Sema::NonTrivialCUnionContext UseContext; |
11996 | Sema &S; |
11997 | }; |
11998 | |
11999 | struct DiagNonTrivalCUnionDestructedTypeVisitor |
12000 | : DestructedTypeVisitor<DiagNonTrivalCUnionDestructedTypeVisitor, void> { |
12001 | using Super = |
12002 | DestructedTypeVisitor<DiagNonTrivalCUnionDestructedTypeVisitor, void>; |
12003 | |
12004 | DiagNonTrivalCUnionDestructedTypeVisitor( |
12005 | QualType OrigTy, SourceLocation OrigLoc, |
12006 | Sema::NonTrivialCUnionContext UseContext, Sema &S) |
12007 | : OrigTy(OrigTy), OrigLoc(OrigLoc), UseContext(UseContext), S(S) {} |
12008 | |
12009 | void visitWithKind(QualType::DestructionKind DK, QualType QT, |
12010 | const FieldDecl *FD, bool InNonTrivialUnion) { |
12011 | if (const auto *AT = S.Context.getAsArrayType(QT)) |
12012 | return this->asDerived().visit(S.Context.getBaseElementType(AT), FD, |
12013 | InNonTrivialUnion); |
12014 | return Super::visitWithKind(DK, QT, FD, InNonTrivialUnion); |
12015 | } |
12016 | |
12017 | void visitARCStrong(QualType QT, const FieldDecl *FD, |
12018 | bool InNonTrivialUnion) { |
12019 | if (InNonTrivialUnion) |
12020 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
12021 | << 1 << 1 << QT << FD->getName(); |
12022 | } |
12023 | |
12024 | void visitARCWeak(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
12025 | if (InNonTrivialUnion) |
12026 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
12027 | << 1 << 1 << QT << FD->getName(); |
12028 | } |
12029 | |
12030 | void visitStruct(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
12031 | const RecordDecl *RD = QT->castAs<RecordType>()->getDecl(); |
12032 | if (RD->isUnion()) { |
12033 | if (OrigLoc.isValid()) { |
12034 | bool IsUnion = false; |
12035 | if (auto *OrigRD = OrigTy->getAsRecordDecl()) |
12036 | IsUnion = OrigRD->isUnion(); |
12037 | S.Diag(OrigLoc, diag::err_non_trivial_c_union_in_invalid_context) |
12038 | << 1 << OrigTy << IsUnion << UseContext; |
12039 | |
12040 | OrigLoc = SourceLocation(); |
12041 | } |
12042 | InNonTrivialUnion = true; |
12043 | } |
12044 | |
12045 | if (InNonTrivialUnion) |
12046 | S.Diag(RD->getLocation(), diag::note_non_trivial_c_union) |
12047 | << 0 << 1 << QT.getUnqualifiedType() << ""; |
12048 | |
12049 | for (const FieldDecl *FD : RD->fields()) |
12050 | if (!shouldIgnoreForRecordTriviality(FD)) |
12051 | asDerived().visit(FD->getType(), FD, InNonTrivialUnion); |
12052 | } |
12053 | |
12054 | void visitTrivial(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) {} |
12055 | void visitCXXDestructor(QualType QT, const FieldDecl *FD, |
12056 | bool InNonTrivialUnion) {} |
12057 | |
12058 | |
12059 | |
12060 | QualType OrigTy; |
12061 | SourceLocation OrigLoc; |
12062 | Sema::NonTrivialCUnionContext UseContext; |
12063 | Sema &S; |
12064 | }; |
12065 | |
12066 | struct DiagNonTrivalCUnionCopyVisitor |
12067 | : CopiedTypeVisitor<DiagNonTrivalCUnionCopyVisitor, false, void> { |
12068 | using Super = CopiedTypeVisitor<DiagNonTrivalCUnionCopyVisitor, false, void>; |
12069 | |
12070 | DiagNonTrivalCUnionCopyVisitor(QualType OrigTy, SourceLocation OrigLoc, |
12071 | Sema::NonTrivialCUnionContext UseContext, |
12072 | Sema &S) |
12073 | : OrigTy(OrigTy), OrigLoc(OrigLoc), UseContext(UseContext), S(S) {} |
12074 | |
12075 | void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType QT, |
12076 | const FieldDecl *FD, bool InNonTrivialUnion) { |
12077 | if (const auto *AT = S.Context.getAsArrayType(QT)) |
12078 | return this->asDerived().visit(S.Context.getBaseElementType(AT), FD, |
12079 | InNonTrivialUnion); |
12080 | return Super::visitWithKind(PCK, QT, FD, InNonTrivialUnion); |
12081 | } |
12082 | |
12083 | void visitARCStrong(QualType QT, const FieldDecl *FD, |
12084 | bool InNonTrivialUnion) { |
12085 | if (InNonTrivialUnion) |
12086 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
12087 | << 1 << 2 << QT << FD->getName(); |
12088 | } |
12089 | |
12090 | void visitARCWeak(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
12091 | if (InNonTrivialUnion) |
12092 | S.Diag(FD->getLocation(), diag::note_non_trivial_c_union) |
12093 | << 1 << 2 << QT << FD->getName(); |
12094 | } |
12095 | |
12096 | void visitStruct(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) { |
12097 | const RecordDecl *RD = QT->castAs<RecordType>()->getDecl(); |
12098 | if (RD->isUnion()) { |
12099 | if (OrigLoc.isValid()) { |
12100 | bool IsUnion = false; |
12101 | if (auto *OrigRD = OrigTy->getAsRecordDecl()) |
12102 | IsUnion = OrigRD->isUnion(); |
12103 | S.Diag(OrigLoc, diag::err_non_trivial_c_union_in_invalid_context) |
12104 | << 2 << OrigTy << IsUnion << UseContext; |
12105 | |
12106 | OrigLoc = SourceLocation(); |
12107 | } |
12108 | InNonTrivialUnion = true; |
12109 | } |
12110 | |
12111 | if (InNonTrivialUnion) |
12112 | S.Diag(RD->getLocation(), diag::note_non_trivial_c_union) |
12113 | << 0 << 2 << QT.getUnqualifiedType() << ""; |
12114 | |
12115 | for (const FieldDecl *FD : RD->fields()) |
12116 | if (!shouldIgnoreForRecordTriviality(FD)) |
12117 | asDerived().visit(FD->getType(), FD, InNonTrivialUnion); |
12118 | } |
12119 | |
12120 | void preVisit(QualType::PrimitiveCopyKind PCK, QualType QT, |
12121 | const FieldDecl *FD, bool InNonTrivialUnion) {} |
12122 | void visitTrivial(QualType QT, const FieldDecl *FD, bool InNonTrivialUnion) {} |
12123 | void visitVolatileTrivial(QualType QT, const FieldDecl *FD, |
12124 | bool InNonTrivialUnion) {} |
12125 | |
12126 | |
12127 | |
12128 | QualType OrigTy; |
12129 | SourceLocation OrigLoc; |
12130 | Sema::NonTrivialCUnionContext UseContext; |
12131 | Sema &S; |
12132 | }; |
12133 | |
12134 | } |
12135 | |
12136 | void Sema::checkNonTrivialCUnion(QualType QT, SourceLocation Loc, |
12137 | NonTrivialCUnionContext UseContext, |
12138 | unsigned NonTrivialKind) { |
12139 | assert((QT.hasNonTrivialToPrimitiveDefaultInitializeCUnion() || |
12140 | QT.hasNonTrivialToPrimitiveDestructCUnion() || |
12141 | QT.hasNonTrivialToPrimitiveCopyCUnion()) && |
12142 | "shouldn't be called if type doesn't have a non-trivial C union"); |
12143 | |
12144 | if ((NonTrivialKind & NTCUK_Init) && |
12145 | QT.hasNonTrivialToPrimitiveDefaultInitializeCUnion()) |
12146 | DiagNonTrivalCUnionDefaultInitializeVisitor(QT, Loc, UseContext, *this) |
12147 | .visit(QT, nullptr, false); |
12148 | if ((NonTrivialKind & NTCUK_Destruct) && |
12149 | QT.hasNonTrivialToPrimitiveDestructCUnion()) |
12150 | DiagNonTrivalCUnionDestructedTypeVisitor(QT, Loc, UseContext, *this) |
12151 | .visit(QT, nullptr, false); |
12152 | if ((NonTrivialKind & NTCUK_Copy) && QT.hasNonTrivialToPrimitiveCopyCUnion()) |
12153 | DiagNonTrivalCUnionCopyVisitor(QT, Loc, UseContext, *this) |
12154 | .visit(QT, nullptr, false); |
12155 | } |
12156 | |
12157 | |
12158 | |
12159 | |
12160 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { |
12161 | |
12162 | |
12163 | if (!RealDecl || RealDecl->isInvalidDecl()) { |
| 1 | Assuming 'RealDecl' is non-null | |
|
| 2 | | Assuming the condition is false | |
|
| |
12164 | CorrectDelayedTyposInExpr(Init, dyn_cast_or_null<VarDecl>(RealDecl)); |
12165 | return; |
12166 | } |
12167 | |
12168 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
| 4 | | Assuming 'RealDecl' is not a 'CXXMethodDecl' | |
|
| |
12169 | |
12170 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
12171 | << Method->getDeclName() << Init->getSourceRange(); |
12172 | Method->setInvalidDecl(); |
12173 | return; |
12174 | } |
12175 | |
12176 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 6 | | Assuming 'RealDecl' is a 'VarDecl' | |
|
12177 | if (!VDecl) { |
| |
12178 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
12179 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
12180 | RealDecl->setInvalidDecl(); |
12181 | return; |
12182 | } |
12183 | |
12184 | |
12185 | if (VDecl->getType()->isUndeducedType()) { |
| 8 | | Calling 'Type::isUndeducedType' | |
|
| 12 | | Returning from 'Type::isUndeducedType' | |
|
| |
12186 | |
12187 | |
12188 | |
12189 | ExprResult Res = CorrectDelayedTyposInExpr(Init, VDecl); |
12190 | if (!Res.isUsable()) { |
| 14 | | Calling 'ActionResult::isUsable' | |
|
| 17 | | Returning from 'ActionResult::isUsable' | |
|
| |
12191 | |
12192 | |
12193 | RealDecl->setInvalidDecl(); |
12194 | return; |
12195 | } |
12196 | if (Res.get()->containsErrors()) { |
| 19 | | Assuming the condition is false | |
|
| |
12197 | |
12198 | RealDecl->setInvalidDecl(); |
12199 | VDecl->setInit(Res.get()); |
12200 | return; |
12201 | } |
12202 | Init = Res.get(); |
12203 | |
12204 | if (DeduceVariableDeclarationType(VDecl, DirectInit, Init)) |
| 21 | | Calling 'Sema::DeduceVariableDeclarationType' | |
|
12205 | return; |
12206 | } |
12207 | |
12208 | |
12209 | if (VDecl->hasAttr<DLLImportAttr>() && !VDecl->isStaticDataMember()) { |
12210 | Diag(VDecl->getLocation(), diag::err_attribute_dllimport_data_definition); |
12211 | VDecl->setInvalidDecl(); |
12212 | return; |
12213 | } |
12214 | |
12215 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { |
12216 | |
12217 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
12218 | VDecl->setInvalidDecl(); |
12219 | return; |
12220 | } |
12221 | |
12222 | if (!VDecl->getType()->isDependentType()) { |
12223 | |
12224 | |
12225 | |
12226 | QualType BaseDeclType = VDecl->getType(); |
12227 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) |
12228 | BaseDeclType = Array->getElementType(); |
12229 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, |
12230 | diag::err_typecheck_decl_incomplete_type)) { |
12231 | RealDecl->setInvalidDecl(); |
12232 | return; |
12233 | } |
12234 | |
12235 | |
12236 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
12237 | diag::err_abstract_type_in_decl, |
12238 | AbstractVariableType)) |
12239 | VDecl->setInvalidDecl(); |
12240 | } |
12241 | |
12242 | |
12243 | |
12244 | |
12245 | if (VarDecl *Def = VDecl->getDefinition()) |
12246 | if (Def != VDecl && |
12247 | (!VDecl->isStaticDataMember() || VDecl->isOutOfLine()) && |
12248 | !VDecl->isThisDeclarationADemotedDefinition() && |
12249 | checkVarDeclRedefinition(Def, VDecl)) |
12250 | return; |
12251 | |
12252 | if (getLangOpts().CPlusPlus) { |
12253 | |
12254 | |
12255 | |
12256 | |
12257 | |
12258 | |
12259 | |
12260 | |
12261 | |
12262 | |
12263 | |
12264 | |
12265 | if (VDecl->isStaticDataMember() && VDecl->getCanonicalDecl()->hasInit()) { |
12266 | Diag(Init->getExprLoc(), diag::err_static_data_member_reinitialization) |
12267 | << VDecl->getDeclName(); |
12268 | Diag(VDecl->getCanonicalDecl()->getInit()->getExprLoc(), |
12269 | diag::note_previous_initializer) |
12270 | << 0; |
12271 | return; |
12272 | } |
12273 | |
12274 | if (VDecl->hasLocalStorage()) |
12275 | setFunctionHasBranchProtectedScope(); |
12276 | |
12277 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { |
12278 | VDecl->setInvalidDecl(); |
12279 | return; |
12280 | } |
12281 | } |
12282 | |
12283 | |
12284 | |
12285 | if (VDecl->getType().getAddressSpace() == LangAS::opencl_local) { |
12286 | Diag(VDecl->getLocation(), diag::err_local_cant_init); |
12287 | VDecl->setInvalidDecl(); |
12288 | return; |
12289 | } |
12290 | |
12291 | |
12292 | if (VDecl->hasAttr<LoaderUninitializedAttr>()) { |
12293 | Diag(VDecl->getLocation(), diag::err_loader_uninitialized_cant_init); |
12294 | VDecl->setInvalidDecl(); |
12295 | return; |
12296 | } |
12297 | |
12298 | |
12299 | |
12300 | QualType DclT = VDecl->getType(), SavT = DclT; |
12301 | |
12302 | |
12303 | |
12304 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && |
12305 | Init->getType() == Context.UnknownAnyTy) { |
12306 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
12307 | if (Result.isInvalid()) { |
12308 | VDecl->setInvalidDecl(); |
12309 | return; |
12310 | } |
12311 | Init = Result.get(); |
12312 | } |
12313 | |
12314 | |
12315 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
12316 | if (!VDecl->isInvalidDecl()) { |
12317 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
12318 | InitializationKind Kind = InitializationKind::CreateForInit( |
12319 | VDecl->getLocation(), DirectInit, Init); |
12320 | |
12321 | MultiExprArg Args = Init; |
12322 | if (CXXDirectInit) |
12323 | Args = MultiExprArg(CXXDirectInit->getExprs(), |
12324 | CXXDirectInit->getNumExprs()); |
12325 | |
12326 | |
12327 | for (size_t Idx = 0; Idx < Args.size(); ++Idx) { |
12328 | ExprResult Res = CorrectDelayedTyposInExpr( |
12329 | Args[Idx], VDecl, true, |
12330 | [this, Entity, Kind](Expr *E) { |
12331 | InitializationSequence Init(*this, Entity, Kind, MultiExprArg(E)); |
12332 | return Init.Failed() ? ExprError() : E; |
12333 | }); |
12334 | if (Res.isInvalid()) { |
12335 | VDecl->setInvalidDecl(); |
12336 | } else if (Res.get() != Args[Idx]) { |
12337 | Args[Idx] = Res.get(); |
12338 | } |
12339 | } |
12340 | if (VDecl->isInvalidDecl()) |
12341 | return; |
12342 | |
12343 | InitializationSequence InitSeq(*this, Entity, Kind, Args, |
12344 | false, |
12345 | false); |
12346 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); |
12347 | if (Result.isInvalid()) { |
12348 | |
12349 | |
12350 | auto RecoveryExpr = |
12351 | CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), Args); |
12352 | if (RecoveryExpr.get()) |
12353 | VDecl->setInit(RecoveryExpr.get()); |
12354 | return; |
12355 | } |
12356 | |
12357 | Init = Result.getAs<Expr>(); |
12358 | } |
12359 | |
12360 | |
12361 | |
12362 | |
12363 | |
12364 | if (getLangOpts().CPlusPlus) |
12365 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || |
12366 | VDecl->getType()->isReferenceType()) |
12367 | CheckSelfReference(*this, RealDecl, Init, DirectInit); |
12368 | |
12369 | |
12370 | |
12371 | |
12372 | |
12373 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
12374 | VDecl->setType(DclT); |
12375 | |
12376 | if (!VDecl->isInvalidDecl()) { |
12377 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
12378 | |
12379 | if (VDecl->hasAttr<BlocksAttr>()) |
12380 | checkRetainCycles(VDecl, Init); |
12381 | |
12382 | |
12383 | |
12384 | |
12385 | |
12386 | |
12387 | |
12388 | |
12389 | if (FunctionScopeInfo *FSI = getCurFunction()) |
12390 | if ((VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong || |
12391 | VDecl->getType().isNonWeakInMRRWithObjCWeak(Context)) && |
12392 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, |
12393 | Init->getBeginLoc())) |
12394 | FSI->markSafeWeakUse(Init); |
12395 | } |
12396 | |
12397 | |
12398 | |
12399 | |
12400 | |
12401 | |
12402 | |
12403 | |
12404 | |
12405 | |
12406 | |
12407 | |
12408 | ExprResult Result = |
12409 | ActOnFinishFullExpr(Init, VDecl->getLocation(), |
12410 | false, VDecl->isConstexpr()); |
12411 | if (Result.isInvalid()) { |
12412 | VDecl->setInvalidDecl(); |
12413 | return; |
12414 | } |
12415 | Init = Result.get(); |
12416 | |
12417 | |
12418 | VDecl->setInit(Init); |
12419 | |
12420 | if (VDecl->isLocalVarDecl()) { |
12421 | |
12422 | if (VDecl->isInvalidDecl()) { |
12423 | |
12424 | |
12425 | |
12426 | |
12427 | } else if (VDecl->getType().getAddressSpace() == LangAS::opencl_constant) { |
12428 | CheckForConstantInitializer(Init, DclT); |
12429 | |
12430 | |
12431 | } else if (getLangOpts().CPlusPlus) { |
12432 | |
12433 | |
12434 | |
12435 | |
12436 | } else if (VDecl->getStorageClass() == SC_Static) { |
12437 | CheckForConstantInitializer(Init, DclT); |
12438 | |
12439 | |
12440 | |
12441 | |
12442 | |
12443 | } else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && |
12444 | isa<InitListExpr>(Init)) { |
12445 | const Expr *Culprit; |
12446 | if (!Init->isConstantInitializer(Context, false, &Culprit)) { |
12447 | Diag(Culprit->getExprLoc(), |
12448 | diag::ext_aggregate_init_not_constant) |
12449 | << Culprit->getSourceRange(); |
12450 | } |
12451 | } |
12452 | |
12453 | if (auto *E = dyn_cast<ExprWithCleanups>(Init)) |
12454 | if (auto *BE = dyn_cast<BlockExpr>(E->getSubExpr()->IgnoreParens())) |
12455 | if (VDecl->hasLocalStorage()) |
12456 | BE->getBlockDecl()->setCanAvoidCopyToHeap(); |
12457 | } else if (VDecl->isStaticDataMember() && !VDecl->isInline() && |
12458 | VDecl->getLexicalDeclContext()->isRecord()) { |
12459 | |
12460 | |
12461 | |
12462 | |
12463 | |
12464 | |
12465 | |
12466 | |
12467 | |
12468 | |
12469 | |
12470 | |
12471 | |
12472 | |
12473 | |
12474 | |
12475 | |
12476 | |
12477 | |
12478 | |
12479 | |
12480 | |
12481 | if (DclT->isDependentType()) { |
12482 | |
12483 | |
12484 | |
12485 | |
12486 | } else if (VDecl->isConstexpr()) { |
12487 | |
12488 | |
12489 | } else if (!DclT.isConstQualified()) { |
12490 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
12491 | << Init->getSourceRange(); |
12492 | VDecl->setInvalidDecl(); |
12493 | |
12494 | |
12495 | } else if (DclT->isIntegralOrEnumerationType()) { |
12496 | |
12497 | SourceLocation Loc; |
12498 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
12499 | |
12500 | |
12501 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); |
12502 | else if (Init->isValueDependent()) |
12503 | ; |
12504 | else if (Init->isIntegerConstantExpr(Context, &Loc)) |
12505 | ; |
12506 | else if (Init->getType()->isScopedEnumeralType() && |
12507 | Init->isCXX11ConstantExpr(Context)) |
12508 | ; |
12509 | else if (Init->isEvaluatable(Context)) { |
12510 | |
12511 | |
12512 | Diag(Loc, diag::ext_in_class_initializer_non_constant) |
12513 | << Init->getSourceRange(); |
12514 | } else { |
12515 | |
12516 | |
12517 | Diag(Loc, diag::err_in_class_initializer_non_constant) |
12518 | << Init->getSourceRange(); |
12519 | VDecl->setInvalidDecl(); |
12520 | } |
12521 | |
12522 | |
12523 | } else if (DclT->isFloatingType()) { |
12524 | |
12525 | |
12526 | if (getLangOpts().CPlusPlus11) { |
12527 | Diag(VDecl->getLocation(), |
12528 | diag::ext_in_class_initializer_float_type_cxx11) |
12529 | << DclT << Init->getSourceRange(); |
12530 | Diag(VDecl->getBeginLoc(), |
12531 | diag::note_in_class_initializer_float_type_cxx11) |
12532 | << FixItHint::CreateInsertion(VDecl->getBeginLoc(), "constexpr "); |
12533 | } else { |
12534 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) |
12535 | << DclT << Init->getSourceRange(); |
12536 | |
12537 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
12538 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) |
12539 | << Init->getSourceRange(); |
12540 | VDecl->setInvalidDecl(); |
12541 | } |
12542 | } |
12543 | |
12544 | |
12545 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
12546 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
12547 | << DclT << Init->getSourceRange() |
12548 | << FixItHint::CreateInsertion(VDecl->getBeginLoc(), "constexpr "); |
12549 | VDecl->setConstexpr(true); |
12550 | |
12551 | } else { |
12552 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) |
12553 | << DclT << Init->getSourceRange(); |
12554 | VDecl->setInvalidDecl(); |
12555 | } |
12556 | } else if (VDecl->isFileVarDecl()) { |
12557 | |
12558 | |
12559 | |
12560 | |
12561 | |
12562 | |
12563 | |
12564 | if (VDecl->getStorageClass() == SC_Extern && |
12565 | ((!getLangOpts().CPlusPlus && !VDecl->hasAttr<SelectAnyAttr>()) || |
12566 | !Context.getBaseElementType(VDecl->getType()).isConstQualified()) && |
12567 | !(getLangOpts().CPlusPlus && VDecl->isExternC()) && |
12568 | !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) |
12569 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
12570 | |
12571 | |
12572 | |
12573 | |
12574 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
12575 | getLangOpts().CPlusPlus && VDecl->getType().isConstQualified() && |
12576 | VDecl->hasAttr<DLLExportAttr>() && VDecl->getDefinition()) |
12577 | VDecl->setStorageClass(SC_Extern); |
12578 | |
12579 | |
12580 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
12581 | CheckForConstantInitializer(Init, DclT); |
12582 | } |
12583 | |
12584 | QualType InitType = Init->getType(); |
12585 | if (!InitType.isNull() && |
12586 | (InitType.hasNonTrivialToPrimitiveDefaultInitializeCUnion() || |
12587 | InitType.hasNonTrivialToPrimitiveCopyCUnion())) |
12588 | checkNonTrivialCUnionInInitializer(Init, Init->getExprLoc()); |
12589 | |
12590 | |
12591 | |
12592 | |
12593 | |
12594 | |
12595 | |
12596 | |
12597 | |
12598 | |
12599 | |
12600 | |
12601 | |
12602 | |
12603 | |
12604 | if (CXXDirectInit) { |
12605 | assert(DirectInit && "Call-style initializer must be direct init."); |
12606 | VDecl->setInitStyle(VarDecl::CallInit); |
12607 | } else if (DirectInit) { |
12608 | |
12609 | VDecl->setInitStyle(VarDecl::ListInit); |
12610 | } |
12611 | |
12612 | if (LangOpts.OpenMP && VDecl->isFileVarDecl()) |
12613 | DeclsToCheckForDeferredDiags.insert(VDecl); |
12614 | CheckCompleteVariableDeclaration(VDecl); |
12615 | } |
12616 | |
12617 | |
12618 | |
12619 | |
12620 | void Sema::ActOnInitializerError(Decl *D) { |
12621 | |
12622 | |
12623 | if (!D || D->isInvalidDecl()) return; |
12624 | |
12625 | VarDecl *VD = dyn_cast<VarDecl>(D); |
12626 | if (!VD) return; |
12627 | |
12628 | |
12629 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) |
12630 | for (auto *BD : DD->bindings()) |
12631 | BD->setInvalidDecl(); |
12632 | |
12633 | |
12634 | if (VD->getType()->isUndeducedType()) { |
12635 | D->setInvalidDecl(); |
12636 | return; |
12637 | } |
12638 | |
12639 | QualType Ty = VD->getType(); |
12640 | if (Ty->isDependentType()) return; |
12641 | |
12642 | |
12643 | if (RequireCompleteType(VD->getLocation(), |
12644 | Context.getBaseElementType(Ty), |
12645 | diag::err_typecheck_decl_incomplete_type)) { |
12646 | VD->setInvalidDecl(); |
12647 | return; |
12648 | } |
12649 | |
12650 | |
12651 | if (RequireNonAbstractType(VD->getLocation(), Ty, |
12652 | diag::err_abstract_type_in_decl, |
12653 | AbstractVariableType)) { |
12654 | VD->setInvalidDecl(); |
12655 | return; |
12656 | } |
12657 | |
12658 | |
12659 | |
12660 | } |
12661 | |
12662 | void Sema::ActOnUninitializedDecl(Decl *RealDecl) { |
12663 | |
12664 | if (!RealDecl) |
12665 | return; |
12666 | |
12667 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
12668 | QualType Type = Var->getType(); |
12669 | |
12670 | |
12671 | if (isa<DecompositionDecl>(RealDecl)) { |
12672 | Diag(Var->getLocation(), diag::err_decomp_decl_requires_init) << Var; |
12673 | Var->setInvalidDecl(); |
12674 | return; |
12675 | } |
12676 | |
12677 | if (Type->isUndeducedType() && |
12678 | DeduceVariableDeclarationType(Var, false, nullptr)) |
12679 | return; |
12680 | |
12681 | |
12682 | |
12683 | |
12684 | |
12685 | |
12686 | |
12687 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition() && |
12688 | !Var->isThisDeclarationADemotedDefinition()) { |
12689 | if (Var->isStaticDataMember()) { |
12690 | |
12691 | |
12692 | if (!getLangOpts().CPlusPlus17 && |
12693 | !Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
12694 | Diag(Var->getLocation(), |
12695 | diag::err_constexpr_static_mem_var_requires_init) |
12696 | << Var; |
12697 | Var->setInvalidDecl(); |
12698 | return; |
12699 | } |
12700 | } else { |
12701 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); |
12702 | Var->setInvalidDecl(); |
12703 | return; |
12704 | } |
12705 | } |
12706 | |
12707 | |
12708 | |
12709 | if (!Var->isInvalidDecl() && |
12710 | Var->getType().getAddressSpace() == LangAS::opencl_constant && |
12711 | Var->getStorageClass() != SC_Extern && !Var->getInit()) { |
12712 | bool HasConstExprDefaultConstructor = false; |
12713 | if (CXXRecordDecl *RD = Var->getType()->getAsCXXRecordDecl()) { |
12714 | for (auto *Ctor : RD->ctors()) { |
12715 | if (Ctor->isConstexpr() && Ctor->getNumParams() == 0 && |
12716 | Ctor->getMethodQualifiers().getAddressSpace() == |
12717 | LangAS::opencl_constant) { |
12718 | HasConstExprDefaultConstructor = true; |
12719 | } |
12720 | } |
12721 | } |
12722 | if (!HasConstExprDefaultConstructor) { |
12723 | Diag(Var->getLocation(), diag::err_opencl_constant_no_init); |
12724 | Var->setInvalidDecl(); |
12725 | return; |
12726 | } |
12727 | } |
12728 | |
12729 | if (!Var->isInvalidDecl() && RealDecl->hasAttr<LoaderUninitializedAttr>()) { |
12730 | if (Var->getStorageClass() == SC_Extern) { |
12731 | Diag(Var->getLocation(), diag::err_loader_uninitialized_extern_decl) |
12732 | << Var; |
12733 | Var->setInvalidDecl(); |
12734 | return; |
12735 | } |
12736 | if (RequireCompleteType(Var->getLocation(), Var->getType(), |
12737 | diag::err_typecheck_decl_incomplete_type)) { |
12738 | Var->setInvalidDecl(); |
12739 | return; |
12740 | } |
12741 | if (CXXRecordDecl *RD = Var->getType()->getAsCXXRecordDecl()) { |
12742 | if (!RD->hasTrivialDefaultConstructor()) { |
12743 | Diag(Var->getLocation(), diag::err_loader_uninitialized_trivial_ctor); |
12744 | Var->setInvalidDecl(); |
12745 | return; |
12746 | } |
12747 | } |
12748 | |
12749 | return; |
12750 | } |
12751 | |
12752 | VarDecl::DefinitionKind DefKind = Var->isThisDeclarationADefinition(); |
12753 | if (!Var->isInvalidDecl() && DefKind != VarDecl::DeclarationOnly && |
12754 | Var->getType().hasNonTrivialToPrimitiveDefaultInitializeCUnion()) |
12755 | checkNonTrivialCUnion(Var->getType(), Var->getLocation(), |
12756 | NTCUC_DefaultInitializedObject, NTCUK_Init); |
12757 | |
12758 | |
12759 | switch (DefKind) { |
12760 | case VarDecl::Definition: |
12761 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) |
12762 | break; |
12763 | |
12764 | |
12765 | |
12766 | |
12767 | |
12768 | LLVM_FALLTHROUGH; |
12769 | |
12770 | case VarDecl::DeclarationOnly: |
12771 | |
12772 | |
12773 | |
12774 | |
12775 | |
12776 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
12777 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
12778 | RequireCompleteType(Var->getLocation(), Type, |
12779 | diag::err_typecheck_decl_incomplete_type)) |
12780 | Var->setInvalidDecl(); |
12781 | |
12782 | |
12783 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
12784 | RequireNonAbstractType(Var->getLocation(), Type, |
12785 | diag::err_abstract_type_in_decl, |
12786 | AbstractVariableType)) |
12787 | Var->setInvalidDecl(); |
12788 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
12789 | Var->getStorageClass() == SC_PrivateExtern) { |
12790 | Diag(Var->getLocation(), diag::warn_private_extern); |
12791 | Diag(Var->getLocation(), diag::note_private_extern); |
12792 | } |
12793 | |
12794 | if (Context.getTargetInfo().allowDebugInfoForExternalRef() && |
12795 | !Var->isInvalidDecl() && !getLangOpts().CPlusPlus) |
12796 | ExternalDeclarations.push_back(Var); |
12797 | |
12798 | return; |
12799 | |
12800 | case VarDecl::TentativeDefinition: |
12801 | |
12802 | |
12803 | |
12804 | |
12805 | |
12806 | if (!Var->isInvalidDecl()) { |
12807 | if (const IncompleteArrayType *ArrayT |
12808 | = Context.getAsIncompleteArrayType(Type)) { |
12809 | if (RequireCompleteSizedType( |
12810 | Var->getLocation(), ArrayT->getElementType(), |
12811 | diag::err_array_incomplete_or_sizeless_type)) |
12812 | Var->setInvalidDecl(); |
12813 | } else if (Var->getStorageClass() == SC_Static) { |
12814 | |
12815 | |
12816 | |
12817 | |
12818 | |
12819 | |
12820 | |
12821 | |
12822 | |
12823 | if (Var->isFirstDecl()) |
12824 | RequireCompleteType(Var->getLocation(), Type, |
12825 | diag::ext_typecheck_decl_incomplete_type); |
12826 | } |
12827 | } |
12828 | |
12829 | |
12830 | if (!Var->isInvalidDecl()) |
12831 | TentativeDefinitions.push_back(Var); |
12832 | return; |
12833 | } |
12834 | |
12835 | |
12836 | |
12837 | if (Type->isIncompleteArrayType()) { |
12838 | Diag(Var->getLocation(), |
12839 | diag::err_typecheck_incomplete_array_needs_initializer); |
12840 | Var->setInvalidDecl(); |
12841 | return; |
12842 | } |
12843 | |
12844 | |
12845 | |
12846 | if (Type->isReferenceType()) { |
12847 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
12848 | << Var << SourceRange(Var->getLocation(), Var->getLocation()); |
12849 | Var->setInvalidDecl(); |
12850 | return; |
12851 | } |
12852 | |
12853 | |
12854 | |
12855 | if (Type->isDependentType()) |
12856 | return; |
12857 | |
12858 | if (Var->isInvalidDecl()) |
12859 | return; |
12860 | |
12861 | if (!Var->hasAttr<AliasAttr>()) { |
12862 | if (RequireCompleteType(Var->getLocation(), |
12863 | Context.getBaseElementType(Type), |
12864 | diag::err_typecheck_decl_incomplete_type)) { |
12865 | Var->setInvalidDecl(); |
12866 | return; |
12867 | } |
12868 | } else { |
12869 | return; |
12870 | } |
12871 | |
12872 | |
12873 | if (RequireNonAbstractType(Var->getLocation(), Type, |
12874 | diag::err_abstract_type_in_decl, |
12875 | AbstractVariableType)) { |
12876 | Var->setInvalidDecl(); |
12877 | return; |
12878 | } |
12879 | |
12880 | |
12881 | |
12882 | |
12883 | |
12884 | |
12885 | |
12886 | |
12887 | |
12888 | |
12889 | |
12890 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
12891 | if (const RecordType *Record |
12892 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { |
12893 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
12894 | |
12895 | |
12896 | |
12897 | if (!CXXRecord->isPOD()) |
12898 | setFunctionHasBranchProtectedScope(); |
12899 | } |
12900 | } |
12901 | |
12902 | |
12903 | if (getLangOpts().OpenCL && |
12904 | Var->getType().getAddressSpace() == LangAS::opencl_local) |
12905 | return; |
12906 | |
12907 | |
12908 | |
12909 | |
12910 | |
12911 | |
12912 | |
12913 | |
12914 | |
12915 | |
12916 | |
12917 | |
12918 | |
12919 | |
12920 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); |
12921 | InitializationKind Kind |
12922 | = InitializationKind::CreateDefault(Var->getLocation()); |
12923 | |
12924 | InitializationSequence InitSeq(*this, Entity, Kind, None); |
12925 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); |
12926 | |
12927 | if (Init.get()) { |
12928 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
12929 | |
12930 | Var->setInitStyle(VarDecl::CallInit); |
12931 | } else if (Init.isInvalid()) { |
12932 | |
12933 | |
12934 | auto RecoveryExpr = |
12935 | CreateRecoveryExpr(Var->getLocation(), Var->getLocation(), {}); |
12936 | if (RecoveryExpr.get()) |
12937 | Var->setInit(RecoveryExpr.get()); |
12938 | } |
12939 | |
12940 | CheckCompleteVariableDeclaration(Var); |
12941 | } |
12942 | } |
12943 | |
12944 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
12945 | |
12946 | if (!D) |
12947 | return; |
12948 | |
12949 | VarDecl *VD = dyn_cast<VarDecl>(D); |
12950 | if (!VD) { |
12951 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); |
12952 | D->setInvalidDecl(); |
12953 | return; |
12954 | } |
12955 | |
12956 | VD->setCXXForRangeDecl(true); |
12957 | |
12958 | |
12959 | int Error = -1; |
12960 | switch (VD->getStorageClass()) { |
12961 | case SC_None: |
12962 | break; |
12963 | case SC_Extern: |
12964 | Error = 0; |
12965 | break; |
12966 | case SC_Static: |
12967 | Error = 1; |
12968 | break; |
12969 | case SC_PrivateExtern: |
12970 | Error = 2; |
12971 | break; |
12972 | case SC_Auto: |
12973 | Error = 3; |
12974 | break; |
12975 | case SC_Register: |
12976 | Error = 4; |
12977 | break; |
12978 | } |
12979 | |
12980 | |
12981 | switch (VD->getTSCSpec()) { |
12982 | case TSCS_thread_local: |
12983 | Error = 6; |
12984 | break; |
12985 | case TSCS___thread: |
12986 | case TSCS__Thread_local: |
12987 | case TSCS_unspecified: |
12988 | break; |
12989 | } |
12990 | |
12991 | if (Error != -1) { |
12992 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) |
12993 | << VD << Error; |
12994 | D->setInvalidDecl(); |
12995 | } |
12996 | } |
12997 | |
12998 | StmtResult |
12999 | Sema::ActOnCXXForRangeIdentifier(Scope *S, SourceLocation IdentLoc, |
13000 | IdentifierInfo *Ident, |
13001 | ParsedAttributes &Attrs, |
13002 | SourceLocation AttrEnd) { |
13003 | |
13004 | |
13005 | |
13006 | |
13007 | |
13008 | DeclSpec DS(Attrs.getPool().getFactory()); |
13009 | |
13010 | const char *PrevSpec; |
13011 | unsigned DiagID; |
13012 | DS.SetTypeSpecType(DeclSpec::TST_auto, IdentLoc, PrevSpec, DiagID, |
13013 | getPrintingPolicy()); |
13014 | |
13015 | Declarator D(DS, DeclaratorContext::ForInit); |
13016 | D.SetIdentifier(Ident, IdentLoc); |
13017 | D.takeAttributes(Attrs, AttrEnd); |
13018 | |
13019 | D.AddTypeInfo(DeclaratorChunk::getReference(0, IdentLoc, false), |
13020 | IdentLoc); |
13021 | Decl *Var = ActOnDeclarator(S, D); |
13022 | cast<VarDecl>(Var)->setCXXForRangeDecl(true); |
13023 | FinalizeDeclaration(Var); |
13024 | return ActOnDeclStmt(FinalizeDeclaratorGroup(S, DS, Var), IdentLoc, |
13025 | AttrEnd.isValid() ? AttrEnd : IdentLoc); |
13026 | } |
13027 | |
13028 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
13029 | if (var->isInvalidDecl()) return; |
13030 | |
13031 | MaybeAddCUDAConstantAttr(var); |
13032 | |
13033 | if (getLangOpts().OpenCL) { |
13034 | |
13035 | |
13036 | if (var->getTypeSourceInfo()->getType()->isBlockPointerType() && |
13037 | !var->hasInit()) { |
13038 | Diag(var->getLocation(), diag::err_opencl_invalid_block_declaration) |
13039 | << 1 ; |
13040 | var->setInvalidDecl(); |
13041 | return; |
13042 | } |
13043 | } |
13044 | |
13045 | |
13046 | |
13047 | if (getLangOpts().ObjC && |
13048 | var->hasLocalStorage()) { |
13049 | switch (var->getType().getObjCLifetime()) { |
13050 | case Qualifiers::OCL_None: |
13051 | case Qualifiers::OCL_ExplicitNone: |
13052 | case Qualifiers::OCL_Autoreleasing: |
13053 | break; |
13054 | |
13055 | case Qualifiers::OCL_Weak: |
13056 | case Qualifiers::OCL_Strong: |
13057 | setFunctionHasBranchProtectedScope(); |
13058 | break; |
13059 | } |
13060 | } |
13061 | |
13062 | if (var->hasLocalStorage() && |
13063 | var->getType().isDestructedType() == QualType::DK_nontrivial_c_struct) |
13064 | setFunctionHasBranchProtectedScope(); |
13065 | |
13066 | |
13067 | |
13068 | |
13069 | |
13070 | |
13071 | if (var->isThisDeclarationADefinition() && |
13072 | var->getDeclContext()->getRedeclContext()->isFileContext() && |
13073 | var->isExternallyVisible() && var->hasLinkage() && |
13074 | !var->isInline() && !var->getDescribedVarTemplate() && |
13075 | !isa<VarTemplatePartialSpecializationDecl>(var) && |
13076 | !isTemplateInstantiation(var->getTemplateSpecializationKind()) && |
13077 | !getDiagnostics().isIgnored(diag::warn_missing_variable_declarations, |
13078 | var->getLocation())) { |
13079 | |
13080 | VarDecl *prev = var->getPreviousDecl(); |
13081 | while (prev && prev->isThisDeclarationADefinition()) |
13082 | prev = prev->getPreviousDecl(); |
13083 | |
13084 | if (!prev) { |
13085 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; |
13086 | Diag(var->getTypeSpecStartLoc(), diag::note_static_for_internal_linkage) |
13087 | << 0; |
13088 | } |
13089 | } |
13090 | |
13091 | |
13092 | Optional<bool> CacheHasConstInit; |
13093 | const Expr *CacheCulprit = nullptr; |
13094 | auto checkConstInit = [&]() mutable { |
13095 | if (!CacheHasConstInit) |
13096 | CacheHasConstInit = var->getInit()->isConstantInitializer( |
13097 | Context, var->getType()->isReferenceType(), &CacheCulprit); |
13098 | return *CacheHasConstInit; |
13099 | }; |
13100 | |
13101 | if (var->getTLSKind() == VarDecl::TLS_Static) { |
13102 | if (var->getType().isDestructedType()) { |
13103 | |
13104 | |
13105 | |
13106 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); |
13107 | if (getLangOpts().CPlusPlus11) |
13108 | Diag(var->getLocation(), diag::note_use_thread_local); |
13109 | } else if (getLangOpts().CPlusPlus && var->hasInit()) { |
13110 | if (!checkConstInit()) { |
13111 | |
13112 | |
13113 | |
13114 | |
13115 | Diag(CacheCulprit->getExprLoc(), diag::err_thread_dynamic_init) |
13116 | << CacheCulprit->getSourceRange(); |
13117 | if (getLangOpts().CPlusPlus11) |
13118 | Diag(var->getLocation(), diag::note_use_thread_local); |
13119 | } |
13120 | } |
13121 | } |
13122 | |
13123 | |
13124 | if (!var->getType()->isStructureType() && var->hasInit() && |
13125 | isa<InitListExpr>(var->getInit())) { |
13126 | const auto *ILE = cast<InitListExpr>(var->getInit()); |
13127 | unsigned NumInits = ILE->getNumInits(); |
13128 | if (NumInits > 2) |
13129 | for (unsigned I = 0; I < NumInits; ++I) { |
13130 | const auto *Init = ILE->getInit(I); |
13131 | if (!Init) |
13132 | break; |
13133 | const auto *SL = dyn_cast<StringLiteral>(Init->IgnoreImpCasts()); |
13134 | if (!SL) |
13135 | break; |
13136 | |
13137 | unsigned NumConcat = SL->getNumConcatenated(); |
13138 | |
13139 | |
13140 | |
13141 | if (NumConcat == 2 && !SL->getBeginLoc().isMacroID()) { |
13142 | bool OnlyOneMissingComma = true; |
13143 | for (unsigned J = I + 1; J < NumInits; ++J) { |
13144 | const auto *Init = ILE->getInit(J); |
13145 | if (!Init) |
13146 | break; |
13147 | const auto *SLJ = dyn_cast<StringLiteral>(Init->IgnoreImpCasts()); |
13148 | if (!SLJ || SLJ->getNumConcatenated() > 1) { |
13149 | OnlyOneMissingComma = false; |
13150 | break; |
13151 | } |
13152 | } |
13153 | |
13154 | if (OnlyOneMissingComma) { |
13155 | SmallVector<FixItHint, 1> Hints; |
13156 | for (unsigned i = 0; i < NumConcat - 1; ++i) |
13157 | Hints.push_back(FixItHint::CreateInsertion( |
13158 | PP.getLocForEndOfToken(SL->getStrTokenLoc(i)), ",")); |
13159 | |
13160 | Diag(SL->getStrTokenLoc(1), |
13161 | diag::warn_concatenated_literal_array_init) |
13162 | << Hints; |
13163 | Diag(SL->getBeginLoc(), |
13164 | diag::note_concatenated_string_literal_silence); |
13165 | } |
13166 | |
13167 | break; |
13168 | } |
13169 | } |
13170 | } |
13171 | |
13172 | |
13173 | QualType type = var->getType(); |
13174 | |
13175 | if (var->hasAttr<BlocksAttr>()) |
13176 | getCurFunction()->addByrefBlockVar(var); |
13177 | |
13178 | Expr *Init = var->getInit(); |
13179 | bool GlobalStorage = var->hasGlobalStorage(); |
13180 | bool IsGlobal = GlobalStorage && !var->isStaticLocal(); |
13181 | QualType baseType = Context.getBaseElementType(type); |
13182 | bool HasConstInit = true; |
13183 | |
13184 | |
13185 | if (getLangOpts().CPlusPlus && !type->isDependentType() && Init && |
13186 | !Init->isValueDependent() && |
13187 | (GlobalStorage || var->isConstexpr() || |
13188 | var->mightBeUsableInConstantExpressions(Context))) { |
13189 | |
13190 | |
13191 | |
13192 | |
13193 | SmallVector<PartialDiagnosticAt, 8> Notes; |
13194 | if (!getLangOpts().CPlusPlus11) { |
13195 | |
13196 | |
13197 | |
13198 | |
13199 | |
13200 | HasConstInit = checkConstInit(); |
13201 | |
13202 | |
13203 | |
13204 | if (HasConstInit) { |
13205 | (void)var->checkForConstantInitialization(Notes); |
13206 | Notes.clear(); |
13207 | } else if (CacheCulprit) { |
13208 | Notes.emplace_back(CacheCulprit->getExprLoc(), |
13209 | PDiag(diag::note_invalid_subexpr_in_const_expr)); |
13210 | Notes.back().second << CacheCulprit->getSourceRange(); |
13211 | } |
13212 | } else { |
13213 | |
13214 | HasConstInit = var->checkForConstantInitialization(Notes); |
13215 | } |
13216 | |
13217 | if (HasConstInit) { |
13218 | |
13219 | } else if (var->isConstexpr()) { |
13220 | SourceLocation DiagLoc = var->getLocation(); |
13221 | |
13222 | |
13223 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
13224 | diag::note_invalid_subexpr_in_const_expr) { |
13225 | DiagLoc = Notes[0].first; |
13226 | Notes.clear(); |
13227 | } |
13228 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) |
13229 | << var << Init->getSourceRange(); |
13230 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
13231 | Diag(Notes[I].first, Notes[I].second); |
13232 | } else if (GlobalStorage && var->hasAttr<ConstInitAttr>()) { |
13233 | auto *Attr = var->getAttr<ConstInitAttr>(); |
13234 | Diag(var->getLocation(), diag::err_require_constant_init_failed) |
13235 | << Init->getSourceRange(); |
13236 | Diag(Attr->getLocation(), diag::note_declared_required_constant_init_here) |
13237 | << Attr->getRange() << Attr->isConstinit(); |
13238 | for (auto &it : Notes) |
13239 | Diag(it.first, it.second); |
13240 | } else if (IsGlobal && |
13241 | !getDiagnostics().isIgnored(diag::warn_global_constructor, |
13242 | var->getLocation())) { |
13243 | |
13244 | |
13245 | |
13246 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); |
13247 | if (!(RD && !RD->hasTrivialDestructor())) { |
13248 | |
13249 | |
13250 | |
13251 | if (!checkConstInit()) |
13252 | Diag(var->getLocation(), diag::warn_global_constructor) |
13253 | << Init->getSourceRange(); |
13254 | } |
13255 | } |
13256 | } |
13257 | |
13258 | |
13259 | if (GlobalStorage && var->isThisDeclarationADefinition() && |
13260 | !inTemplateInstantiation()) { |
13261 | PragmaStack<StringLiteral *> *Stack = nullptr; |
13262 | int SectionFlags = ASTContext::PSF_Read; |
13263 | if (var->getType().isConstQualified()) { |
13264 | if (HasConstInit) |
13265 | Stack = &ConstSegStack; |
13266 | else { |
13267 | Stack = &BSSSegStack; |
13268 | SectionFlags |= ASTContext::PSF_Write; |
13269 | } |
13270 | } else if (var->hasInit() && HasConstInit) { |
13271 | Stack = &DataSegStack; |
13272 | SectionFlags |= ASTContext::PSF_Write; |
13273 | } else { |
13274 | Stack = &BSSSegStack; |
13275 | SectionFlags |= ASTContext::PSF_Write; |
13276 | } |
13277 | if (const SectionAttr *SA = var->getAttr<SectionAttr>()) { |
13278 | if (SA->getSyntax() == AttributeCommonInfo::AS_Declspec) |
13279 | SectionFlags |= ASTContext::PSF_Implicit; |
13280 | UnifySection(SA->getName(), SectionFlags, var); |
13281 | } else if (Stack->CurrentValue) { |
13282 | SectionFlags |= ASTContext::PSF_Implicit; |
13283 | auto SectionName = Stack->CurrentValue->getString(); |
13284 | var->addAttr(SectionAttr::CreateImplicit( |
13285 | Context, SectionName, Stack->CurrentPragmaLocation, |
13286 | AttributeCommonInfo::AS_Pragma, SectionAttr::Declspec_allocate)); |
13287 | if (UnifySection(SectionName, SectionFlags, var)) |
13288 | var->dropAttr<SectionAttr>(); |
13289 | } |
13290 | |
13291 | |
13292 | |
13293 | |
13294 | if (CurInitSeg && var->getInit()) |
13295 | var->addAttr(InitSegAttr::CreateImplicit(Context, CurInitSeg->getString(), |
13296 | CurInitSegLoc, |
13297 | AttributeCommonInfo::AS_Pragma)); |
13298 | } |
13299 | |
13300 | |
13301 | if (!getLangOpts().CPlusPlus) { |
13302 | |
13303 | |
13304 | if (Context.DeclMustBeEmitted(var) && !ModuleScopes.empty()) |
13305 | Context.addModuleInitializer(ModuleScopes.back().Module, var); |
13306 | return; |
13307 | } |
13308 | |
13309 | |
13310 | if (!type->isDependentType()) |
13311 | if (const RecordType *recordType = baseType->getAs<RecordType>()) |
13312 | FinalizeVarWithDestructor(var, recordType); |
13313 | |
13314 | |
13315 | |
13316 | if (Context.DeclMustBeEmitted(var) && !ModuleScopes.empty()) |
13317 | Context.addModuleInitializer(ModuleScopes.back().Module, var); |
13318 | |
13319 | |
13320 | if (auto *DD = dyn_cast<DecompositionDecl>(var)) |
13321 | CheckCompleteDecompositionDeclaration(DD); |
13322 | } |
13323 | |
13324 | |
13325 | |
13326 | void Sema::CheckStaticLocalForDllExport(VarDecl *VD) { |
13327 | assert(VD->isStaticLocal()); |
13328 | |
13329 | auto *FD = dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()); |
13330 | |
13331 | |
13332 | while (FD && !getDLLAttr(FD) && |
13333 | !FD->hasAttr<DLLExportStaticLocalAttr>() && |
13334 | !FD->hasAttr<DLLImportStaticLocalAttr>()) { |
13335 | FD = dyn_cast_or_null<FunctionDecl>(FD->getParentFunctionOrMethod()); |
13336 | } |
13337 | |
13338 | if (!FD) |
13339 | return; |
13340 | |
13341 | |
13342 | if (Attr *A = getDLLAttr(FD)) { |
13343 | auto *NewAttr = cast<InheritableAttr>(A->clone(getASTContext())); |
13344 | NewAttr->setInherited(true); |
13345 | VD->addAttr(NewAttr); |
13346 | } else if (Attr *A = FD->getAttr<DLLExportStaticLocalAttr>()) { |
13347 | auto *NewAttr = DLLExportAttr::CreateImplicit(getASTContext(), *A); |
13348 | NewAttr->setInherited(true); |
13349 | VD->addAttr(NewAttr); |
13350 | |
13351 | |
13352 | |
13353 | if (!FD->hasAttr<DLLExportAttr>()) |
13354 | FD->addAttr(NewAttr); |
13355 | |
13356 | } else if (Attr *A = FD->getAttr<DLLImportStaticLocalAttr>()) { |
13357 | auto *NewAttr = DLLImportAttr::CreateImplicit(getASTContext(), *A); |
13358 | NewAttr->setInherited(true); |
13359 | VD->addAttr(NewAttr); |
13360 | } |
13361 | } |
13362 | |
13363 | |
13364 | |
13365 | void Sema::FinalizeDeclaration(Decl *ThisDecl) { |
13366 | |
13367 | ParsingInitForAutoVars.erase(ThisDecl); |
13368 | |
13369 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
13370 | if (!VD) |
13371 | return; |
13372 | |
13373 | |
13374 | if (VD->hasGlobalStorage() && VD->isThisDeclarationADefinition() && |
13375 | !inTemplateInstantiation() && !VD->hasAttr<SectionAttr>()) { |
13376 | if (PragmaClangBSSSection.Valid) |
13377 | VD->addAttr(PragmaClangBSSSectionAttr::CreateImplicit( |
13378 | Context, PragmaClangBSSSection.SectionName, |
13379 | PragmaClangBSSSection.PragmaLocation, |
13380 | AttributeCommonInfo::AS_Pragma)); |
13381 | if (PragmaClangDataSection.Valid) |
13382 | VD->addAttr(PragmaClangDataSectionAttr::CreateImplicit( |
13383 | Context, PragmaClangDataSection.SectionName, |
13384 | PragmaClangDataSection.PragmaLocation, |
13385 | AttributeCommonInfo::AS_Pragma)); |
13386 | if (PragmaClangRodataSection.Valid) |
13387 | VD->addAttr(PragmaClangRodataSectionAttr::CreateImplicit( |
13388 | Context, PragmaClangRodataSection.SectionName, |
13389 | PragmaClangRodataSection.PragmaLocation, |
13390 | AttributeCommonInfo::AS_Pragma)); |
13391 | if (PragmaClangRelroSection.Valid) |
13392 | VD->addAttr(PragmaClangRelroSectionAttr::CreateImplicit( |
13393 | Context, PragmaClangRelroSection.SectionName, |
13394 | PragmaClangRelroSection.PragmaLocation, |
13395 | AttributeCommonInfo::AS_Pragma)); |
13396 | } |
13397 | |
13398 | if (auto *DD = dyn_cast<DecompositionDecl>(ThisDecl)) { |
13399 | for (auto *BD : DD->bindings()) { |
13400 | FinalizeDeclaration(BD); |
13401 | } |
13402 | } |
13403 | |
13404 | checkAttributesAfterMerging(*this, *VD); |
13405 | |
13406 | |
13407 | |
13408 | |
13409 | if (unsigned MaxAlign = Context.getTargetInfo().getMaxTLSAlign()) { |
13410 | |
13411 | |
13412 | if (VD->getTLSKind() && !VD->hasDependentAlignment()) { |
13413 | CharUnits MaxAlignChars = Context.toCharUnitsFromBits(MaxAlign); |
13414 | if (Context.getDeclAlign(VD) > MaxAlignChars) { |
13415 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
13416 | << (unsigned)Context.getDeclAlign(VD).getQuantity() << VD |
13417 | << (unsigned)MaxAlignChars.getQuantity(); |
13418 | } |
13419 | } |
13420 | } |
13421 | |
13422 | if (VD->isStaticLocal()) |
13423 | CheckStaticLocalForDllExport(VD); |
13424 | |
13425 | |
13426 | |
13427 | |
13428 | |
13429 | |
13430 | if (getLangOpts().CUDA) |
13431 | checkAllowedCUDAInitializer(VD); |
13432 | |
13433 | |
13434 | const InheritableAttr *DLLAttr = getDLLAttr(VD); |
13435 | |
13436 | |
13437 | if (const auto *IA = dyn_cast_or_null<DLLImportAttr>(DLLAttr)) { |
13438 | if (VD->isStaticDataMember() && VD->isOutOfLine() && |
13439 | VD->isThisDeclarationADefinition()) { |
13440 | |
13441 | |
13442 | CXXRecordDecl *Context = |
13443 | cast<CXXRecordDecl>(VD->getFirstDecl()->getDeclContext()); |
13444 | bool IsClassTemplateMember = |
13445 | isa<ClassTemplatePartialSpecializationDecl>(Context) || |
13446 | Context->getDescribedClassTemplate(); |
13447 | |
13448 | Diag(VD->getLocation(), |
13449 | IsClassTemplateMember |
13450 | ? diag::warn_attribute_dllimport_static_field_definition |
13451 | : diag::err_attribute_dllimport_static_field_definition); |
13452 | Diag(IA->getLocation(), diag::note_attribute); |
13453 | if (!IsClassTemplateMember) |
13454 | VD->setInvalidDecl(); |
13455 | } |
13456 | } |
13457 | |
13458 | |
13459 | |
13460 | if (DLLAttr && VD->getTLSKind()) { |
13461 | auto *F = dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()); |
13462 | if (F && getDLLAttr(F)) { |
13463 | assert(VD->isStaticLocal()); |
13464 | |
13465 | |
13466 | |
13467 | } else { |
13468 | Diag(VD->getLocation(), diag::err_attribute_dll_thread_local) << VD |
13469 | << DLLAttr; |
13470 | VD->setInvalidDecl(); |
13471 | } |
13472 | } |
13473 | |
13474 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
13475 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { |
13476 | Diag(Attr->getLocation(), diag::warn_attribute_ignored_on_non_definition) |
13477 | << Attr; |
13478 | VD->dropAttr<UsedAttr>(); |
13479 | } |
13480 | } |
13481 | if (RetainAttr *Attr = VD->getAttr<RetainAttr>()) { |
13482 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { |
13483 | Diag(Attr->getLocation(), diag::warn_attribute_ignored_on_non_definition) |
13484 | << Attr; |
13485 | VD->dropAttr<RetainAttr>(); |
13486 | } |
13487 | } |
13488 | |
13489 | const DeclContext *DC = VD->getDeclContext(); |
13490 | |
13491 | |
13492 | if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible()) |
13493 | AddPushedVisibilityAttribute(VD); |
13494 | |
13495 | |
13496 | if (VD->isFileVarDecl() && !isa<VarTemplatePartialSpecializationDecl>(VD)) |
13497 | MarkUnusedFileScopedDecl(VD); |
13498 | |
13499 | |
13500 | |
13501 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
13502 | !VD->getType()->isIntegralOrEnumerationType()) |
13503 | return; |
13504 | |
13505 | for (const auto *I : ThisDecl->specific_attrs<TypeTagForDatatypeAttr>()) { |
13506 | const Expr *MagicValueExpr = VD->getInit(); |
13507 | if (!MagicValueExpr) { |
13508 | continue; |
13509 | } |
13510 | Optional<llvm::APSInt> MagicValueInt; |
13511 | if (!(MagicValueInt = MagicValueExpr->getIntegerConstantExpr(Context))) { |
13512 | Diag(I->getRange().getBegin(), |
13513 | diag::err_type_tag_for_datatype_not_ice) |
13514 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
13515 | continue; |
13516 | } |
13517 | if (MagicValueInt->getActiveBits() > 64) { |
13518 | Diag(I->getRange().getBegin(), |
13519 | diag::err_type_tag_for_datatype_too_large) |
13520 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
13521 | continue; |
13522 | } |
13523 | uint64_t MagicValue = MagicValueInt->getZExtValue(); |
13524 | RegisterTypeTagForDatatype(I->getArgumentKind(), |
13525 | MagicValue, |
13526 | I->getMatchingCType(), |
13527 | I->getLayoutCompatible(), |
13528 | I->getMustBeNull()); |
13529 | } |
13530 | } |
13531 | |
13532 | static bool hasDeducedAuto(DeclaratorDecl *DD) { |
13533 | auto *VD = dyn_cast<VarDecl>(DD); |
13534 | return VD && !VD->getType()->hasAutoForTrailingReturnType(); |
13535 | } |
13536 | |
13537 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
13538 | ArrayRef<Decl *> Group) { |
13539 | SmallVector<Decl*, 8> Decls; |
13540 | |
13541 | if (DS.isTypeSpecOwned()) |
13542 | Decls.push_back(DS.getRepAsDecl()); |
13543 | |
13544 | DeclaratorDecl *FirstDeclaratorInGroup = nullptr; |
13545 | DecompositionDecl *FirstDecompDeclaratorInGroup = nullptr; |
13546 | bool DiagnosedMultipleDecomps = false; |
13547 | DeclaratorDecl *FirstNonDeducedAutoInGroup = nullptr; |
13548 | bool DiagnosedNonDeducedAuto = false; |
13549 | |
13550 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
13551 | if (Decl *D = Group[i]) { |
13552 | |
13553 | |
13554 | if (auto *DD = dyn_cast<DeclaratorDecl>(D)) { |
13555 | if (!FirstDeclaratorInGroup) |
13556 | FirstDeclaratorInGroup = DD; |
13557 | if (!FirstDecompDeclaratorInGroup) |
13558 | FirstDecompDeclaratorInGroup = dyn_cast<DecompositionDecl>(D); |
13559 | if (!FirstNonDeducedAutoInGroup && DS.hasAutoTypeSpec() && |
13560 | !hasDeducedAuto(DD)) |
13561 | FirstNonDeducedAutoInGroup = DD; |
13562 | |
13563 | if (FirstDeclaratorInGroup != DD) { |
13564 | |
13565 | |
13566 | if (FirstDecompDeclaratorInGroup && !DiagnosedMultipleDecomps) { |
13567 | Diag(FirstDecompDeclaratorInGroup->getLocation(), |
13568 | diag::err_decomp_decl_not_alone) |
13569 | << FirstDeclaratorInGroup->getSourceRange() |
13570 | << DD->getSourceRange(); |
13571 | DiagnosedMultipleDecomps = true; |
13572 | } |
13573 | |
13574 | |
13575 | |
13576 | |
13577 | if (FirstNonDeducedAutoInGroup && !DiagnosedNonDeducedAuto) { |
13578 | Diag(FirstNonDeducedAutoInGroup->getLocation(), |
13579 | diag::err_auto_non_deduced_not_alone) |
13580 | << FirstNonDeducedAutoInGroup->getType() |
13581 | ->hasAutoForTrailingReturnType() |
13582 | << FirstDeclaratorInGroup->getSourceRange() |
13583 | << DD->getSourceRange(); |
13584 | DiagnosedNonDeducedAuto = true; |
13585 | } |
13586 | } |
13587 | } |
13588 | |
13589 | Decls.push_back(D); |
13590 | } |
13591 | } |
13592 | |
13593 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
13594 | if (TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) { |
13595 | handleTagNumbering(Tag, S); |
13596 | if (FirstDeclaratorInGroup && !Tag->hasNameForLinkage() && |
13597 | getLangOpts().CPlusPlus) |
13598 | Context.addDeclaratorForUnnamedTagDecl(Tag, FirstDeclaratorInGroup); |
13599 | } |
13600 | } |
13601 | |
13602 | return BuildDeclaratorGroup(Decls); |
13603 | } |
13604 | |
13605 | |
13606 | |
13607 | Sema::DeclGroupPtrTy |
13608 | Sema::BuildDeclaratorGroup(MutableArrayRef<Decl *> Group) { |
13609 | |
13610 | |
13611 | |
13612 | if (Group.size() > 1) { |
13613 | QualType Deduced; |
13614 | VarDecl *DeducedDecl = nullptr; |
13615 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
13616 | VarDecl *D = dyn_cast<VarDecl>(Group[i]); |
13617 | if (!D || D->isInvalidDecl()) |
13618 | break; |
13619 | DeducedType *DT = D->getType()->getContainedDeducedType(); |
13620 | if (!DT || DT->getDeducedType().isNull()) |
13621 | continue; |
13622 | if (Deduced.isNull()) { |
13623 | Deduced = DT->getDeducedType(); |
13624 | DeducedDecl = D; |
13625 | } else if (!Context.hasSameType(DT->getDeducedType(), Deduced)) { |
13626 | auto *AT = dyn_cast<AutoType>(DT); |
13627 | auto Dia = Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
13628 | diag::err_auto_different_deductions) |
13629 | << (AT ? (unsigned)AT->getKeyword() : 3) << Deduced |
13630 | << DeducedDecl->getDeclName() << DT->getDeducedType() |
13631 | << D->getDeclName(); |
13632 | if (DeducedDecl->hasInit()) |
13633 | Dia << DeducedDecl->getInit()->getSourceRange(); |
13634 | if (D->getInit()) |
13635 | Dia << D->getInit()->getSourceRange(); |
13636 | D->setInvalidDecl(); |
13637 | break; |
13638 | } |
13639 | } |
13640 | } |
13641 | |
13642 | ActOnDocumentableDecls(Group); |
13643 | |
13644 | return DeclGroupPtrTy::make( |
13645 | DeclGroupRef::Create(Context, Group.data(), Group.size())); |
13646 | } |
13647 | |
13648 | void Sema::ActOnDocumentableDecl(Decl *D) { |
13649 | ActOnDocumentableDecls(D); |
13650 | } |
13651 | |
13652 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
13653 | |
13654 | if (Group.empty() || !Group[0]) |
13655 | return; |
13656 | |
13657 | if (Diags.isIgnored(diag::warn_doc_param_not_found, |
13658 | Group[0]->getLocation()) && |
13659 | Diags.isIgnored(diag::warn_unknown_comment_command_name, |
13660 | Group[0]->getLocation())) |
13661 | return; |
13662 | |
13663 | if (Group.size() >= 2) { |
13664 | |
13665 | |
13666 | |
13667 | |
13668 | |
13669 | |
13670 | |
13671 | Decl *MaybeTagDecl = Group[0]; |
13672 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { |
13673 | Group = Group.slice(1); |
13674 | } |
13675 | } |
13676 | |
13677 | |
13678 | |
13679 | |
13680 | Context.attachCommentsToJustParsedDecls(Group, &getPreprocessor()); |
13681 | } |
13682 | |
13683 | |
13684 | |
13685 | void Sema::CheckFunctionOrTemplateParamDeclarator(Scope *S, Declarator &D) { |
13686 | |
13687 | |
13688 | if (getLangOpts().CPlusPlus) |
13689 | CheckExtraCXXDefaultArguments(D); |
13690 | |
13691 | |
13692 | if (D.getCXXScopeSpec().isSet()) { |
13693 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
13694 | << D.getCXXScopeSpec().getRange(); |
13695 | } |
13696 | |
13697 | |
13698 | |
13699 | switch (D.getName().getKind()) { |
13700 | case UnqualifiedIdKind::IK_Identifier: |
13701 | break; |
13702 | |
13703 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
13704 | case UnqualifiedIdKind::IK_ConversionFunctionId: |
13705 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
13706 | case UnqualifiedIdKind::IK_ConstructorName: |
13707 | case UnqualifiedIdKind::IK_DestructorName: |
13708 | case UnqualifiedIdKind::IK_ImplicitSelfParam: |
13709 | case UnqualifiedIdKind::IK_DeductionGuideName: |
13710 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) |
13711 | << GetNameForDeclarator(D).getName(); |
13712 | break; |
13713 | |
13714 | case UnqualifiedIdKind::IK_TemplateId: |
13715 | case UnqualifiedIdKind::IK_ConstructorTemplateId: |
13716 | |
13717 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name_template_id); |
13718 | break; |
13719 | } |
13720 | } |
13721 | |
13722 | |
13723 | |
13724 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
13725 | const DeclSpec &DS = D.getDeclSpec(); |
13726 | |
13727 | |
13728 | |
13729 | |
13730 | StorageClass SC = SC_None; |
13731 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
13732 | SC = SC_Register; |
13733 | |
13734 | |
13735 | if (getLangOpts().CPlusPlus11) { |
13736 | Diag(DS.getStorageClassSpecLoc(), |
13737 | getLangOpts().CPlusPlus17 ? diag::ext_register_storage_class |
13738 | : diag::warn_deprecated_register) |
13739 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
13740 | } |
13741 | } else if (getLangOpts().CPlusPlus && |
13742 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
13743 | SC = SC_Auto; |
13744 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
13745 | Diag(DS.getStorageClassSpecLoc(), |
13746 | diag::err_invalid_storage_class_in_func_decl); |
13747 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
13748 | } |
13749 | |
13750 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
13751 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) |
13752 | << DeclSpec::getSpecifierName(TSCS); |
13753 | if (DS.isInlineSpecified()) |
13754 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
13755 | << getLangOpts().CPlusPlus17; |
13756 | if (DS.hasConstexprSpecifier()) |
13757 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) |
13758 | << 0 << static_cast<int>(D.getDeclSpec().getConstexprSpecifier()); |
13759 | |
13760 | DiagnoseFunctionSpecifiers(DS); |
13761 | |
13762 | CheckFunctionOrTemplateParamDeclarator(S, D); |
13763 | |
13764 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
13765 | QualType parmDeclType = TInfo->getType(); |
13766 | |
13767 | |
13768 | IdentifierInfo *II = D.getIdentifier(); |
13769 | if (II) { |
13770 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
13771 | ForVisibleRedeclaration); |
13772 | LookupName(R, S); |
13773 | if (R.isSingleResult()) { |
13774 | NamedDecl *PrevDecl = R.getFoundDecl(); |
13775 | if (PrevDecl->isTemplateParameter()) { |
13776 | |
13777 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
13778 | |
13779 | PrevDecl = nullptr; |
13780 | } else if (S->isDeclScope(PrevDecl)) { |
13781 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
13782 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
13783 | |
13784 | |
13785 | II = nullptr; |
13786 | D.SetIdentifier(nullptr, D.getIdentifierLoc()); |
13787 | D.setInvalidType(true); |
13788 | } |
13789 | } |
13790 | } |
13791 | |
13792 | |
13793 | |
13794 | |
13795 | ParmVarDecl *New = |
13796 | CheckParameter(Context.getTranslationUnitDecl(), D.getBeginLoc(), |
13797 | D.getIdentifierLoc(), II, parmDeclType, TInfo, SC); |
13798 | |
13799 | if (D.isInvalidType()) |
13800 | New->setInvalidDecl(); |
13801 | |
13802 | assert(S->isFunctionPrototypeScope()); |
13803 | assert(S->getFunctionPrototypeDepth() >= 1); |
13804 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, |
13805 | S->getNextFunctionPrototypeIndex()); |
13806 | |
13807 | |
13808 | S->AddDecl(New); |
13809 | if (II) |
13810 | IdResolver.AddDecl(New); |
13811 | |
13812 | ProcessDeclAttributes(S, New, D); |
13813 | |
13814 | if (D.getDeclSpec().isModulePrivateSpecified()) |
13815 | Diag(New->getLocation(), diag::err_module_private_local) |
13816 | << 1 << New << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
13817 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
13818 | |
13819 | if (New->hasAttr<BlocksAttr>()) { |
13820 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
13821 | } |
13822 | |
13823 | if (getLangOpts().OpenCL) |
13824 | deduceOpenCLAddressSpace(New); |
13825 | |
13826 | return New; |
13827 | } |
13828 | |
13829 | |
13830 | |
13831 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, |
13832 | SourceLocation Loc, |
13833 | QualType T) { |
13834 | |
13835 | |
13836 | |
13837 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, nullptr, |
13838 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
13839 | SC_None, nullptr); |
13840 | Param->setImplicit(); |
13841 | return Param; |
13842 | } |
13843 | |
13844 | void Sema::DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters) { |
13845 | |
13846 | |
13847 | if (inTemplateInstantiation()) |
13848 | return; |
13849 | |
13850 | for (const ParmVarDecl *Parameter : Parameters) { |
13851 | if (!Parameter->isReferenced() && Parameter->getDeclName() && |
13852 | !Parameter->hasAttr<UnusedAttr>()) { |
13853 | Diag(Parameter->getLocation(), diag::warn_unused_parameter) |
13854 | << Parameter->getDeclName(); |
13855 | } |
13856 | } |
13857 | } |
13858 | |
13859 | void Sema::DiagnoseSizeOfParametersAndReturnValue( |
13860 | ArrayRef<ParmVarDecl *> Parameters, QualType ReturnTy, NamedDecl *D) { |
13861 | if (LangOpts.NumLargeByValueCopy == 0) |
13862 | return; |
13863 | |
13864 | |
13865 | |
13866 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
13867 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
13868 | if (Size > LangOpts.NumLargeByValueCopy) |
13869 | Diag(D->getLocation(), diag::warn_return_value_size) << D << Size; |
13870 | } |
13871 | |
13872 | |
13873 | |
13874 | for (const ParmVarDecl *Parameter : Parameters) { |
13875 | QualType T = Parameter->getType(); |
13876 | if (T->isDependentType() || !T.isPODType(Context)) |
13877 | continue; |
13878 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
13879 | if (Size > LangOpts.NumLargeByValueCopy) |
13880 | Diag(Parameter->getLocation(), diag::warn_parameter_size) |
13881 | << Parameter << Size; |
13882 | } |
13883 | } |
13884 | |
13885 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
13886 | SourceLocation NameLoc, IdentifierInfo *Name, |
13887 | QualType T, TypeSourceInfo *TSInfo, |
13888 | StorageClass SC) { |
13889 | |
13890 | if (getLangOpts().ObjCAutoRefCount && |
13891 | T.getObjCLifetime() == Qualifiers::OCL_None && |
13892 | T->isObjCLifetimeType()) { |
13893 | |
13894 | Qualifiers::ObjCLifetime lifetime; |
13895 | |
13896 | |
13897 | |
13898 | |
13899 | if (T->isArrayType()) { |
13900 | if (!T.isConstQualified()) { |
13901 | if (DelayedDiagnostics.shouldDelayDiagnostics()) |
13902 | DelayedDiagnostics.add( |
13903 | sema::DelayedDiagnostic::makeForbiddenType( |
13904 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
13905 | else |
13906 | Diag(NameLoc, diag::err_arc_array_param_no_ownership) |
13907 | << TSInfo->getTypeLoc().getSourceRange(); |
13908 | } |
13909 | lifetime = Qualifiers::OCL_ExplicitNone; |
13910 | } else { |
13911 | lifetime = T->getObjCARCImplicitLifetime(); |
13912 | } |
13913 | T = Context.getLifetimeQualifiedType(T, lifetime); |
13914 | } |
13915 | |
13916 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
13917 | Context.getAdjustedParameterType(T), |
13918 | TSInfo, SC, nullptr); |
13919 | |
13920 | |
13921 | |
13922 | |
13923 | if (New->isParameterPack()) |
13924 | if (auto *LSI = getEnclosingLambda()) |
13925 | LSI->LocalPacks.push_back(New); |
13926 | |
13927 | if (New->getType().hasNonTrivialToPrimitiveDestructCUnion() || |
13928 | New->getType().hasNonTrivialToPrimitiveCopyCUnion()) |
13929 | checkNonTrivialCUnion(New->getType(), New->getLocation(), |
13930 | NTCUC_FunctionParam, NTCUK_Destruct|NTCUK_Copy); |
13931 | |
13932 | |
13933 | |
13934 | |
13935 | if (!CurContext->isRecord() && |
13936 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, |
13937 | AbstractParamType)) |
13938 | New->setInvalidDecl(); |
13939 | |
13940 | |
13941 | |
13942 | if (T->isObjCObjectType()) { |
13943 | SourceLocation TypeEndLoc = |
13944 | getLocForEndOfToken(TSInfo->getTypeLoc().getEndLoc()); |
13945 | Diag(NameLoc, |
13946 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
13947 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
13948 | T = Context.getObjCObjectPointerType(T); |
13949 | New->setType(T); |
13950 | } |
13951 | |
13952 | |
13953 | |
13954 | |
13955 | |
13956 | if (T.getAddressSpace() != LangAS::Default && |
13957 | |
13958 | |
13959 | !(getLangOpts().OpenCL && |
13960 | (T->isArrayType() || T.getAddressSpace() == LangAS::opencl_private))) { |
13961 | Diag(NameLoc, diag::err_arg_with_address_space); |
13962 | New->setInvalidDecl(); |
13963 | } |
13964 | |
13965 | |
13966 | if (Context.getTargetInfo().getTriple().isPPC64() && |
13967 | CheckPPCMMAType(New->getOriginalType(), New->getLocation())) { |
13968 | New->setInvalidDecl(); |
13969 | } |
13970 | |
13971 | return New; |
13972 | } |
13973 | |
13974 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
13975 | SourceLocation LocAfterDecls) { |
13976 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
13977 | |
13978 | |
13979 | |
13980 | if (!FTI.hasPrototype) { |
13981 | for (int i = FTI.NumParams; i != 0; ) { |
13982 | --i; |
13983 | if (FTI.Params[i].Param == nullptr) { |
13984 | SmallString<256> Code; |
13985 | llvm::raw_svector_ostream(Code) |
13986 | << " int " << FTI.Params[i].Ident->getName() << ";\n"; |
13987 | Diag(FTI.Params[i].IdentLoc, diag::ext_param_not_declared) |
13988 | << FTI.Params[i].Ident |
13989 | << FixItHint::CreateInsertion(LocAfterDecls, Code); |
13990 | |
13991 | |
13992 | |
13993 | AttributeFactory attrs; |
13994 | DeclSpec DS(attrs); |
13995 | const char* PrevSpec; |
13996 | unsigned DiagID; |
13997 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.Params[i].IdentLoc, PrevSpec, |
13998 | DiagID, Context.getPrintingPolicy()); |
13999 | |
14000 | DS.SetRangeStart(FTI.Params[i].IdentLoc); |
14001 | DS.SetRangeEnd(FTI.Params[i].IdentLoc); |
14002 | Declarator ParamD(DS, DeclaratorContext::KNRTypeList); |
14003 | ParamD.SetIdentifier(FTI.Params[i].Ident, FTI.Params[i].IdentLoc); |
14004 | FTI.Params[i].Param = ActOnParamDeclarator(S, ParamD); |
14005 | } |
14006 | } |
14007 | } |
14008 | } |
14009 | |
14010 | Decl * |
14011 | Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D, |
14012 | MultiTemplateParamsArg TemplateParameterLists, |
14013 | SkipBodyInfo *SkipBody) { |
14014 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
14015 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
14016 | Scope *ParentScope = FnBodyScope->getParent(); |
14017 | |
14018 | |
14019 | |
14020 | |
14021 | |
14022 | |
14023 | |
14024 | |
14025 | SmallVector<FunctionDecl *, 4> Bases; |
14026 | if (LangOpts.OpenMP && isInOpenMPDeclareVariantScope()) |
14027 | ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( |
14028 | ParentScope, D, TemplateParameterLists, Bases); |
14029 | |
14030 | D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition); |
14031 | Decl *DP = HandleDeclarator(ParentScope, D, TemplateParameterLists); |
14032 | Decl *Dcl = ActOnStartOfFunctionDef(FnBodyScope, DP, SkipBody); |
14033 | |
14034 | if (!Bases.empty()) |
14035 | ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(Dcl, Bases); |
14036 | |
14037 | return Dcl; |
14038 | } |
14039 | |
14040 | void Sema::ActOnFinishInlineFunctionDef(FunctionDecl *D) { |
14041 | Consumer.HandleInlineFunctionDefinition(D); |
14042 | } |
14043 | |
14044 | static bool |
14045 | ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
14046 | const FunctionDecl *&PossiblePrototype) { |
14047 | |
14048 | if (FD->isInvalidDecl()) |
14049 | return false; |
14050 | |
14051 | |
14052 | if (!FD->isGlobal()) |
14053 | return false; |
14054 | |
14055 | |
14056 | if (isa<CXXMethodDecl>(FD)) |
14057 | return false; |
14058 | |
14059 | |
14060 | if (isa<TranslationUnitDecl>(FD->getDeclContext()->getRedeclContext())) |
14061 | if (IdentifierInfo *II = FD->getIdentifier()) |
14062 | if (II->isStr("main") || II->isStr("efi_main")) |
14063 | return false; |
14064 | |
14065 | |
14066 | if (FD->isInlined()) |
14067 | return false; |
14068 | |
14069 | |
14070 | if (FD->getDescribedFunctionTemplate()) |
14071 | return false; |
14072 | |
14073 | |
14074 | if (FD->isFunctionTemplateSpecialization()) |
14075 | return false; |
14076 | |
14077 | |
14078 | if (FD->hasAttr<OpenCLKernelAttr>()) |
14079 | return false; |
14080 | |
14081 | |
14082 | if (FD->isDeleted()) |
14083 | return false; |
14084 | |
14085 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
14086 | Prev; Prev = Prev->getPreviousDecl()) { |
14087 | |
14088 | |
14089 | if (Prev->getLexicalDeclContext()->isFunctionOrMethod()) |
14090 | continue; |
14091 | |
14092 | PossiblePrototype = Prev; |
14093 | return Prev->getType()->isFunctionNoProtoType(); |
14094 | } |
14095 | |
14096 | return true; |
14097 | } |
14098 | |
14099 | void |
14100 | Sema::CheckForFunctionRedefinition(FunctionDecl *FD, |
14101 | const FunctionDecl *EffectiveDefinition, |
14102 | SkipBodyInfo *SkipBody) { |
14103 | const FunctionDecl *Definition = EffectiveDefinition; |
14104 | if (!Definition && |
14105 | !FD->isDefined(Definition, true)) |
14106 | return; |
14107 | |
14108 | if (Definition->getFriendObjectKind() != Decl::FOK_None) { |
14109 | if (FunctionDecl *OrigDef = Definition->getInstantiatedFromMemberFunction()) { |
14110 | if (FunctionDecl *OrigFD = FD->getInstantiatedFromMemberFunction()) { |
14111 | |
14112 | |
14113 | if (declaresSameEntity(OrigFD, OrigDef) && |
14114 | declaresSameEntity(cast<Decl>(Definition->getLexicalDeclContext()), |
14115 | cast<Decl>(FD->getLexicalDeclContext()))) |
14116 | return; |
14117 | } |
14118 | } |
14119 | } |
14120 | |
14121 | if (canRedefineFunction(Definition, getLangOpts())) |
14122 | return; |
14123 | |
14124 | |
14125 | |
14126 | if (TypoCorrectedFunctionDefinitions.count(Definition)) |
14127 | return; |
14128 | |
14129 | |
14130 | |
14131 | if (SkipBody && !hasVisibleDefinition(Definition) && |
14132 | (Definition->getFormalLinkage() == InternalLinkage || |
14133 | Definition->isInlined() || |
14134 | Definition->getDescribedFunctionTemplate() || |
14135 | Definition->getNumTemplateParameterLists())) { |
14136 | SkipBody->ShouldSkip = true; |
14137 | SkipBody->Previous = const_cast<FunctionDecl*>(Definition); |
14138 | if (auto *TD = Definition->getDescribedFunctionTemplate()) |
14139 | makeMergedDefinitionVisible(TD); |
14140 | makeMergedDefinitionVisible(const_cast<FunctionDecl*>(Definition)); |
14141 | return; |
14142 | } |
14143 | |
14144 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && |
14145 | Definition->getStorageClass() == SC_Extern) |
14146 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) |
14147 | << FD << getLangOpts().CPlusPlus; |
14148 | else |
14149 | Diag(FD->getLocation(), diag::err_redefinition) << FD; |
14150 | |
14151 | Diag(Definition->getLocation(), diag::note_previous_definition); |
14152 | FD->setInvalidDecl(); |
14153 | } |
14154 | |
14155 | static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator, |
14156 | Sema &S) { |
14157 | CXXRecordDecl *const LambdaClass = CallOperator->getParent(); |
14158 | |
14159 | LambdaScopeInfo *LSI = S.PushLambdaScope(); |
14160 | LSI->CallOperator = CallOperator; |
14161 | LSI->Lambda = LambdaClass; |
14162 | LSI->ReturnType = CallOperator->getReturnType(); |
14163 | const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault(); |
14164 | |
14165 | if (LCD == LCD_None) |
14166 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; |
14167 | else if (LCD == LCD_ByCopy) |
14168 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; |
14169 | else if (LCD == LCD_ByRef) |
14170 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; |
14171 | DeclarationNameInfo DNI = CallOperator->getNameInfo(); |
14172 | |
14173 | LSI->IntroducerRange = DNI.getCXXOperatorNameRange(); |
14174 | LSI->Mutable = !CallOperator->isConst(); |
14175 | |
14176 | |
14177 | |
14178 | auto I = LambdaClass->field_begin(); |
14179 | for (const auto &C : LambdaClass->captures()) { |
14180 | if (C.capturesVariable()) { |
14181 | VarDecl *VD = C.getCapturedVar(); |
14182 | if (VD->isInitCapture()) |
14183 | S.CurrentInstantiationScope->InstantiatedLocal(VD, VD); |
14184 | const bool ByRef = C.getCaptureKind() == LCK_ByRef; |
14185 | LSI->addCapture(VD, false, ByRef, |
14186 | true, C.getLocation(), |
14187 | C.isPackExpansion() |
14188 | ? C.getEllipsisLoc() : SourceLocation(), |
14189 | I->getType(), false); |
14190 | |
14191 | } else if (C.capturesThis()) { |
14192 | LSI->addThisCapture( false, C.getLocation(), I->getType(), |
14193 | C.getCaptureKind() == LCK_StarThis); |
14194 | } else { |
14195 | LSI->addVLATypeCapture(C.getLocation(), I->getCapturedVLAType(), |
14196 | I->getType()); |
14197 | } |
14198 | ++I; |
14199 | } |
14200 | } |
14201 | |
14202 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D, |
14203 | SkipBodyInfo *SkipBody) { |
14204 | if (!D) { |
14205 | |
14206 | |
14207 | PushFunctionScope(); |
14208 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
14209 | return D; |
14210 | } |
14211 | |
14212 | FunctionDecl *FD = nullptr; |
14213 | |
14214 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
14215 | FD = FunTmpl->getTemplatedDecl(); |
14216 | else |
14217 | FD = cast<FunctionDecl>(D); |
14218 | |
14219 | |
14220 | |
14221 | if (!isLambdaCallOperator(FD)) |
14222 | PushExpressionEvaluationContext( |
14223 | FD->isConsteval() ? ExpressionEvaluationContext::ConstantEvaluated |
14224 | : ExprEvalContexts.back().Context); |
14225 | |
14226 | |
14227 | if (const auto *Attr = FD->getAttr<AliasAttr>()) { |
14228 | Diag(Attr->getLocation(), diag::err_alias_is_definition) << FD << 0; |
14229 | FD->dropAttr<AliasAttr>(); |
14230 | FD->setInvalidDecl(); |
14231 | } |
14232 | if (const auto *Attr = FD->getAttr<IFuncAttr>()) { |
14233 | Diag(Attr->getLocation(), diag::err_alias_is_definition) << FD << 1; |
14234 | FD->dropAttr<IFuncAttr>(); |
14235 | FD->setInvalidDecl(); |
14236 | } |
14237 | |
14238 | if (auto *Ctor = dyn_cast<CXXConstructorDecl>(FD)) { |
14239 | if (Ctor->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
14240 | Ctor->isDefaultConstructor() && |
14241 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
14242 | |
14243 | |
14244 | InstantiateDefaultCtorDefaultArgs(Ctor); |
14245 | } |
14246 | } |
14247 | |
14248 | |
14249 | |
14250 | if (!FD->willHaveBody() && !FD->isLateTemplateParsed() && |
14251 | !FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { |
14252 | CheckForFunctionRedefinition(FD, nullptr, SkipBody); |
14253 | |
14254 | |
14255 | if (SkipBody && SkipBody->ShouldSkip) |
14256 | return D; |
14257 | } |
14258 | |
14259 | |
14260 | |
14261 | |
14262 | FD->setWillHaveBody(); |
14263 | |
14264 | |
14265 | |
14266 | |
14267 | |
14268 | |
14269 | |
14270 | |
14271 | |
14272 | |
14273 | if (isGenericLambdaCallOperatorSpecialization(FD)) { |
14274 | assert(inTemplateInstantiation() && |
14275 | "There should be an active template instantiation on the stack " |
14276 | "when instantiating a generic lambda!"); |
14277 | RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this); |
14278 | } else { |
14279 | |
14280 | PushFunctionScope(); |
14281 | } |
14282 | |
14283 | |
14284 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
14285 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
14286 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { |
14287 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
14288 | FD->setInvalidDecl(); |
14289 | } |
14290 | } |
14291 | |
14292 | |
14293 | |
14294 | QualType ResultType = FD->getReturnType(); |
14295 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
14296 | !FD->isInvalidDecl() && |
14297 | RequireCompleteType(FD->getLocation(), ResultType, |
14298 | diag::err_func_def_incomplete_result)) |
14299 | FD->setInvalidDecl(); |
14300 | |
14301 | if (FnBodyScope) |
14302 | PushDeclContext(FnBodyScope, FD); |
14303 | |
14304 | |
14305 | CheckParmsForFunctionDef(FD->parameters(), |
14306 | true); |
14307 | |
14308 | |
14309 | |
14310 | if (FnBodyScope) { |
14311 | for (Decl *NPD : FD->decls()) { |
14312 | auto *NonParmDecl = dyn_cast<NamedDecl>(NPD); |
14313 | if (!NonParmDecl) |
14314 | continue; |
14315 | assert(!isa<ParmVarDecl>(NonParmDecl) && |
14316 | "parameters should not be in newly created FD yet"); |
14317 | |
14318 | |
14319 | if (NonParmDecl->getDeclName()) |
14320 | PushOnScopeChains(NonParmDecl, FnBodyScope, false); |
14321 | |
14322 | |
14323 | |
14324 | if (auto *ED = dyn_cast<EnumDecl>(NonParmDecl)) { |
14325 | for (auto *EI : ED->enumerators()) |
14326 | PushOnScopeChains(EI, FnBodyScope, false); |
14327 | } |
14328 | } |
14329 | } |
14330 | |
14331 | |
14332 | for (auto Param : FD->parameters()) { |
14333 | Param->setOwningFunction(FD); |
14334 | |
14335 | |
14336 | if (Param->getIdentifier() && FnBodyScope) { |
14337 | CheckShadow(FnBodyScope, Param); |
14338 | |
14339 | PushOnScopeChains(Param, FnBodyScope); |
14340 | } |
14341 | } |
14342 | |
14343 | |
14344 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) |
14345 | ResolveExceptionSpec(D->getLocation(), FPT); |
14346 | |
14347 | |
14348 | if (FD->hasAttr<DLLImportAttr>() && !FD->isInlined() && |
14349 | !FD->isTemplateInstantiation()) { |
14350 | assert(!FD->hasAttr<DLLExportAttr>()); |
14351 | Diag(FD->getLocation(), diag::err_attribute_dllimport_function_definition); |
14352 | FD->setInvalidDecl(); |
14353 | return D; |
14354 | } |
14355 | |
14356 | |
14357 | ActOnDocumentableDecl(D); |
14358 | if (getCurLexicalContext()->isObjCContainer() && |
14359 | getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl && |
14360 | getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) |
14361 | Diag(FD->getLocation(), diag::warn_function_def_in_objc_container); |
14362 | |
14363 | return D; |
14364 | } |
14365 | |
14366 | |
14367 | |
14368 | |
14369 | |
14370 | |
14371 | |
14372 | |
14373 | |
14374 | |
14375 | |
14376 | |
14377 | |
14378 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
14379 | ReturnStmt **Returns = Scope->Returns.data(); |
14380 | |
14381 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
14382 | if (const VarDecl *NRVOCandidate = Returns[I]->getNRVOCandidate()) { |
14383 | if (!NRVOCandidate->isNRVOVariable()) |
14384 | Returns[I]->setNRVOCandidate(nullptr); |
14385 | } |
14386 | } |
14387 | } |
14388 | |
14389 | bool Sema::canDelayFunctionBody(const Declarator &D) { |
14390 | |
14391 | if (D.getDeclSpec().hasConstexprSpecifier()) |
14392 | return false; |
14393 | |
14394 | |
14395 | |
14396 | if (D.getDeclSpec().hasAutoTypeSpec()) { |
14397 | |
14398 | |
14399 | if (D.getNumTypeObjects()) { |
14400 | const auto &Outer = D.getTypeObject(D.getNumTypeObjects() - 1); |
14401 | if (Outer.Kind == DeclaratorChunk::Function && |
14402 | Outer.Fun.hasTrailingReturnType()) { |
14403 | QualType Ty = GetTypeFromParser(Outer.Fun.getTrailingReturnType()); |
14404 | return Ty.isNull() || !Ty->isUndeducedType(); |
14405 | } |
14406 | } |
14407 | return false; |
14408 | } |
14409 | |
14410 | return true; |
14411 | } |
14412 | |
14413 | bool Sema::canSkipFunctionBody(Decl *D) { |
14414 | |
14415 | |
14416 | |
14417 | |
14418 | |
14419 | if (const FunctionDecl *FD = D->getAsFunction()) { |
14420 | if (FD->isConstexpr()) |
14421 | return false; |
14422 | |
14423 | |
14424 | |
14425 | if (FD->getReturnType()->getContainedDeducedType()) |
14426 | return false; |
14427 | } |
14428 | return Consumer.shouldSkipFunctionBody(D); |
14429 | } |
14430 | |
14431 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
14432 | if (!Decl) |
14433 | return nullptr; |
14434 | if (FunctionDecl *FD = Decl->getAsFunction()) |
14435 | FD->setHasSkippedBody(); |
14436 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Decl)) |
14437 | MD->setHasSkippedBody(); |
14438 | return Decl; |
14439 | } |
14440 | |
14441 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
14442 | return ActOnFinishFunctionBody(D, BodyArg, false); |
14443 | } |
14444 | |
14445 | |
14446 | |
14447 | class ExitFunctionBodyRAII { |
14448 | public: |
14449 | ExitFunctionBodyRAII(Sema &S, bool IsLambda) : S(S), IsLambda(IsLambda) {} |
14450 | ~ExitFunctionBodyRAII() { |
14451 | if (!IsLambda) |
14452 | S.PopExpressionEvaluationContext(); |
14453 | } |
14454 | |
14455 | private: |
14456 | Sema &S; |
14457 | bool IsLambda = false; |
14458 | }; |
14459 | |
14460 | static void diagnoseImplicitlyRetainedSelf(Sema &S) { |
14461 | llvm::DenseMap<const BlockDecl *, bool> EscapeInfo; |
14462 | |
14463 | auto IsOrNestedInEscapingBlock = [&](const BlockDecl *BD) { |
14464 | if (EscapeInfo.count(BD)) |
14465 | return EscapeInfo[BD]; |
14466 | |
14467 | bool R = false; |
14468 | const BlockDecl *CurBD = BD; |
14469 | |
14470 | do { |
14471 | R = !CurBD->doesNotEscape(); |
14472 | if (R) |
14473 | break; |
14474 | CurBD = CurBD->getParent()->getInnermostBlockDecl(); |
14475 | } while (CurBD); |
14476 | |
14477 | return EscapeInfo[BD] = R; |
14478 | }; |
14479 | |
14480 | |
14481 | |
14482 | for (const std::pair<SourceLocation, const BlockDecl *> &P : |
14483 | S.ImplicitlyRetainedSelfLocs) |
14484 | if (IsOrNestedInEscapingBlock(P.second)) |
14485 | S.Diag(P.first, diag::warn_implicitly_retains_self) |
14486 | << FixItHint::CreateInsertion(P.first, "self->"); |
14487 | } |
14488 | |
14489 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
14490 | bool IsInstantiation) { |
14491 | FunctionScopeInfo *FSI = getCurFunction(); |
14492 | FunctionDecl *FD = dcl ? dcl->getAsFunction() : nullptr; |
14493 | |
14494 | if (FSI->UsesFPIntrin && !FD->hasAttr<StrictFPAttr>()) |
14495 | FD->addAttr(StrictFPAttr::CreateImplicit(Context)); |
14496 | |
14497 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
14498 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = nullptr; |
14499 | |
14500 | if (getLangOpts().Coroutines && FSI->isCoroutine()) |
14501 | CheckCompletedCoroutineBody(FD, Body); |
14502 | |
14503 | |
14504 | |
14505 | |
14506 | ExitFunctionBodyRAII ExitRAII(*this, isLambdaCallOperator(FD)); |
14507 | |
14508 | if (FD) { |
14509 | FD->setBody(Body); |
14510 | FD->setWillHaveBody(false); |
14511 | |
14512 | if (getLangOpts().CPlusPlus14) { |
14513 | if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() && |
14514 | FD->getReturnType()->isUndeducedType()) { |
14515 | |
14516 | |
14517 | |
14518 | if (!FD->getReturnType()->getAs<AutoType>()) { |
14519 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) |
14520 | << FD->getReturnType(); |
14521 | FD->setInvalidDecl(); |
14522 | } else { |
14523 | |
14524 | TypeLoc ResultType = getReturnTypeLoc(FD); |
14525 | Context.adjustDeducedFunctionResultType( |
14526 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); |
14527 | } |
14528 | } |
14529 | } else if (getLangOpts().CPlusPlus11 && isLambdaCallOperator(FD)) { |
14530 | |
14531 | |
14532 | auto *LSI = getCurLambda(); |
14533 | if (LSI->HasImplicitReturnType) { |
14534 | deduceClosureReturnType(*LSI); |
14535 | |
14536 | |
14537 | |
14538 | |
14539 | QualType RetType = |
14540 | LSI->ReturnType.isNull() ? Context.VoidTy : LSI->ReturnType; |
14541 | |
14542 | |
14543 | const auto *Proto = FD->getType()->castAs<FunctionProtoType>(); |
14544 | FD->setType(Context.getFunctionType(RetType, Proto->getParamTypes(), |
14545 | Proto->getExtProtoInfo())); |
14546 | } |
14547 | } |
14548 | |
14549 | |
14550 | |
14551 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) |
14552 | WP.disableCheckFallThrough(); |
14553 | |
14554 | |
14555 | |
14556 | if (getLangOpts().MicrosoftExt && FD->isPure() && !FD->isOutOfLine()) |
14557 | Diag(FD->getLocation(), diag::ext_pure_function_definition); |
14558 | |
14559 | if (!FD->isInvalidDecl()) { |
14560 | |
14561 | if (!FD->isDeleted() && !FD->isDefaulted() && !FD->hasSkippedBody()) |
14562 | DiagnoseUnusedParameters(FD->parameters()); |
14563 | DiagnoseSizeOfParametersAndReturnValue(FD->parameters(), |
14564 | FD->getReturnType(), FD); |
14565 | |
14566 | |
14567 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) |
14568 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); |
14569 | else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(FD)) |
14570 | MarkVTableUsed(FD->getLocation(), Destructor->getParent()); |
14571 | |
14572 | |
14573 | |
14574 | |
14575 | if (FD->getReturnType()->isRecordType() && |
14576 | (!getLangOpts().CPlusPlus || !FD->isDependentContext())) |
14577 | computeNRVO(Body, FSI); |
14578 | } |
14579 | |
14580 | |
14581 | |
14582 | |
14583 | |
14584 | |
14585 | const FunctionDecl *PossiblePrototype = nullptr; |
14586 | if (ShouldWarnAboutMissingPrototype(FD, PossiblePrototype)) { |
14587 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
14588 | |
14589 | if (PossiblePrototype) { |
14590 | |
14591 | |
14592 | if (TypeSourceInfo *TI = PossiblePrototype->getTypeSourceInfo()) { |
14593 | TypeLoc TL = TI->getTypeLoc(); |
14594 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) |
14595 | Diag(PossiblePrototype->getLocation(), |
14596 | diag::note_declaration_not_a_prototype) |
14597 | << (FD->getNumParams() != 0) |
14598 | << (FD->getNumParams() == 0 |
14599 | ? FixItHint::CreateInsertion(FTL.getRParenLoc(), "void") |
14600 | : FixItHint{}); |
14601 | } |
14602 | } else { |
14603 | |
14604 | auto isLocAtConst = [&](SourceLocation Loc, const SourceManager &SM, |
14605 | const LangOptions &LangOpts) { |
14606 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
14607 | if (LocInfo.first.isInvalid()) |
14608 | return false; |
14609 | |
14610 | bool Invalid = false; |
14611 | StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
14612 | if (Invalid) |
14613 | return false; |
14614 | |
14615 | if (LocInfo.second > Buffer.size()) |
14616 | return false; |
14617 | |
14618 | const char *LexStart = Buffer.data() + LocInfo.second; |
14619 | StringRef StartTok(LexStart, Buffer.size() - LocInfo.second); |
14620 | |
14621 | return StartTok.consume_front("const") && |
14622 | (StartTok.empty() || isWhitespace(StartTok[0]) || |
14623 | StartTok.startswith("/*") || StartTok.startswith("//")); |
14624 | }; |
14625 | |
14626 | auto findBeginLoc = [&]() { |
14627 | |
14628 | |
14629 | if ((FD->getReturnType()->isAnyPointerType() && |
14630 | FD->getReturnType()->getPointeeType().isConstQualified()) || |
14631 | FD->getReturnType().isConstQualified()) { |
14632 | |
14633 | |
14634 | if (isLocAtConst(FD->getBeginLoc(), getSourceManager(), |
14635 | getLangOpts())) |
14636 | |
14637 | return FD->getBeginLoc(); |
14638 | } |
14639 | return FD->getTypeSpecStartLoc(); |
14640 | }; |
14641 | Diag(FD->getTypeSpecStartLoc(), diag::note_static_for_internal_linkage) |
14642 | << 1 |
14643 | << (FD->getStorageClass() == SC_None |
14644 | ? FixItHint::CreateInsertion(findBeginLoc(), "static ") |
14645 | : FixItHint{}); |
14646 | } |
14647 | |
14648 | |
14649 | |
14650 | |
14651 | |
14652 | if (!FD->hasWrittenPrototype()) { |
14653 | TypeSourceInfo *TI = FD->getTypeSourceInfo(); |
14654 | TypeLoc TL = TI->getTypeLoc(); |
14655 | FunctionTypeLoc FTL = TL.getAsAdjusted<FunctionTypeLoc>(); |
14656 | Diag(FTL.getLParenLoc(), diag::warn_strict_prototypes) << 2; |
14657 | } |
14658 | } |
14659 | |
14660 | |
14661 | if (FD->isMultiVersion() && FD->hasAttr<CPUDispatchAttr>() && Body) |
14662 | if (const auto *CmpndBody = dyn_cast<CompoundStmt>(Body)) |
14663 | if (!CmpndBody->body_empty()) |
14664 | Diag(CmpndBody->body_front()->getBeginLoc(), |
14665 | diag::warn_dispatch_body_ignored); |
14666 | |
14667 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
14668 | const CXXMethodDecl *KeyFunction; |
14669 | if (MD->isOutOfLine() && (MD = MD->getCanonicalDecl()) && |
14670 | MD->isVirtual() && |
14671 | (KeyFunction = Context.getCurrentKeyFunction(MD->getParent())) && |
14672 | MD == KeyFunction->getCanonicalDecl()) { |
14673 | |
14674 | if (FD->isInlined() && |
14675 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
14676 | Context.setNonKeyFunction(MD); |
14677 | |
14678 | |
14679 | |
14680 | KeyFunction = Context.getCurrentKeyFunction(MD->getParent()); |
14681 | const FunctionDecl *Definition; |
14682 | if (KeyFunction && KeyFunction->isDefined(Definition)) |
14683 | MarkVTableUsed(Definition->getLocation(), MD->getParent(), true); |
14684 | } else { |
14685 | |
14686 | MarkVTableUsed(FD->getLocation(), MD->getParent(), true); |
14687 | } |
14688 | } |
14689 | } |
14690 | |
14691 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
14692 | "Function parsing confused"); |
14693 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
14694 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
14695 | MD->setBody(Body); |
14696 | if (!MD->isInvalidDecl()) { |
14697 | DiagnoseSizeOfParametersAndReturnValue(MD->parameters(), |
14698 | MD->getReturnType(), MD); |
14699 | |
14700 | if (Body) |
14701 | computeNRVO(Body, FSI); |
14702 | } |
14703 | if (FSI->ObjCShouldCallSuper) { |
14704 | Diag(MD->getEndLoc(), diag::warn_objc_missing_super_call) |
14705 | << MD->getSelector().getAsString(); |
14706 | FSI->ObjCShouldCallSuper = false; |
14707 | } |
14708 | if (FSI->ObjCWarnForNoDesignatedInitChain) { |
14709 | const ObjCMethodDecl *InitMethod = nullptr; |
14710 | bool isDesignated = |
14711 | MD->isDesignatedInitializerForTheInterface(&InitMethod); |
14712 | assert(isDesignated && InitMethod); |
14713 | (void)isDesignated; |
14714 | |
14715 | auto superIsNSObject = [&](const ObjCMethodDecl *MD) { |
14716 | auto IFace = MD->getClassInterface(); |
14717 | if (!IFace) |
14718 | return false; |
14719 | auto SuperD = IFace->getSuperClass(); |
14720 | if (!SuperD) |
14721 | return false; |
14722 | return SuperD->getIdentifier() == |
14723 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject); |
14724 | }; |
14725 | |
14726 | |
14727 | if (!MD->isUnavailable() && !superIsNSObject(MD)) { |
14728 | Diag(MD->getLocation(), |
14729 | diag::warn_objc_designated_init_missing_super_call); |
14730 | Diag(InitMethod->getLocation(), |
14731 | diag::note_objc_designated_init_marked_here); |
14732 | } |
14733 | FSI->ObjCWarnForNoDesignatedInitChain = false; |
14734 | } |
14735 | if (FSI->ObjCWarnForNoInitDelegation) { |
14736 | |
14737 | if (!MD->isUnavailable()) |
14738 | Diag(MD->getLocation(), |
14739 | diag::warn_objc_secondary_init_missing_init_call); |
14740 | FSI->ObjCWarnForNoInitDelegation = false; |
14741 | } |
14742 | |
14743 | diagnoseImplicitlyRetainedSelf(*this); |
14744 | } else { |
14745 | |
14746 | |
14747 | PopFunctionScopeInfo(ActivePolicy, dcl); |
14748 | return nullptr; |
14749 | } |
14750 | |
14751 | if (Body && FSI->HasPotentialAvailabilityViolations) |
14752 | DiagnoseUnguardedAvailabilityViolations(dcl); |
14753 | |
14754 | assert(!FSI->ObjCShouldCallSuper && |
14755 | "This should only be set for ObjC methods, which should have been " |
14756 | "handled in the block above."); |
14757 | |
14758 | |
14759 | if (Body && (!FD || !FD->isDefaulted())) { |
14760 | |
14761 | |
14762 | |
14763 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) |
14764 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); |
14765 | |
14766 | |
14767 | if (FSI->NeedsScopeChecking() && |
14768 | !PP.isCodeCompletionEnabled()) |
14769 | DiagnoseInvalidJumps(Body); |
14770 | |
14771 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
14772 | if (!Destructor->getParent()->isDependentType()) |
14773 | CheckDestructor(Destructor); |
14774 | |
14775 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
14776 | Destructor->getParent()); |
14777 | } |
14778 | |
14779 | |
14780 | |
14781 | |
14782 | if (hasUncompilableErrorOccurred() || |
14783 | getDiagnostics().getSuppressAllDiagnostics()) { |
14784 | DiscardCleanupsInEvaluationContext(); |
14785 | } |
14786 | if (!hasUncompilableErrorOccurred() && |
14787 | !isa<FunctionTemplateDecl>(dcl)) { |
14788 | |
14789 | |
14790 | ActivePolicy = &WP; |
14791 | } |
14792 | |
14793 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
14794 | !CheckConstexprFunctionDefinition(FD, CheckConstexprKind::Diagnose)) |
14795 | FD->setInvalidDecl(); |
14796 | |
14797 | if (FD && FD->hasAttr<NakedAttr>()) { |
14798 | for (const Stmt *S : Body->children()) { |
14799 | |
14800 | |
14801 | bool RegisterVariables = false; |
14802 | if (auto *DS = dyn_cast<DeclStmt>(S)) { |
14803 | for (const auto *Decl : DS->decls()) { |
14804 | if (const auto *Var = dyn_cast<VarDecl>(Decl)) { |
14805 | RegisterVariables = |
14806 | Var->hasAttr<AsmLabelAttr>() && !Var->hasInit(); |
14807 | if (!RegisterVariables) |
14808 | break; |
14809 | } |
14810 | } |
14811 | } |
14812 | if (RegisterVariables) |
14813 | continue; |
14814 | if (!isa<AsmStmt>(S) && !isa<NullStmt>(S)) { |
14815 | Diag(S->getBeginLoc(), diag::err_non_asm_stmt_in_naked_function); |
14816 | Diag(FD->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
14817 | FD->setInvalidDecl(); |
14818 | break; |
14819 | } |
14820 | } |
14821 | } |
14822 | |
14823 | assert(ExprCleanupObjects.size() == |
14824 | ExprEvalContexts.back().NumCleanupObjects && |
14825 | "Leftover temporaries in function"); |
14826 | assert(!Cleanup.exprNeedsCleanups() && "Unaccounted cleanups in function"); |
14827 | assert(MaybeODRUseExprs.empty() && |
14828 | "Leftover expressions for odr-use checking"); |
14829 | } |
14830 | |
14831 | if (!IsInstantiation) |
14832 | PopDeclContext(); |
14833 | |
14834 | PopFunctionScopeInfo(ActivePolicy, dcl); |
14835 | |
14836 | |
14837 | |
14838 | if (hasUncompilableErrorOccurred()) { |
14839 | DiscardCleanupsInEvaluationContext(); |
14840 | } |
14841 | |
14842 | if (FD && (LangOpts.OpenMP || LangOpts.CUDA || LangOpts.SYCLIsDevice)) { |
14843 | auto ES = getEmissionStatus(FD); |
14844 | if (ES == Sema::FunctionEmissionStatus::Emitted || |
14845 | ES == Sema::FunctionEmissionStatus::Unknown) |
14846 | DeclsToCheckForDeferredDiags.insert(FD); |
14847 | } |
14848 | |
14849 | return dcl; |
14850 | } |
14851 | |
14852 | |
14853 | |
14854 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, |
14855 | ParsedAttributes &Attrs) { |
14856 | |
14857 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
14858 | D = TD->getTemplatedDecl(); |
14859 | ProcessDeclAttributeList(S, D, Attrs); |
14860 | |
14861 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) |
14862 | if (Method->isStatic()) |
14863 | checkThisInStaticMemberFunctionAttributes(Method); |
14864 | } |
14865 | |
14866 | |
14867 | |
14868 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
14869 | IdentifierInfo &II, Scope *S) { |
14870 | |
14871 | |
14872 | |
14873 | |
14874 | |
14875 | Scope *BlockScope = S; |
14876 | while (!BlockScope->isCompoundStmtScope() && BlockScope->getParent()) |
14877 | BlockScope = BlockScope->getParent(); |
14878 | |
14879 | Scope *ContextScope = BlockScope; |
14880 | while (!ContextScope->getEntity()) |
14881 | ContextScope = ContextScope->getParent(); |
14882 | ContextRAII SavedContext(*this, ContextScope->getEntity()); |
14883 | |
14884 | |
14885 | |
14886 | |
14887 | |
14888 | NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II); |
14889 | if (ExternCPrev) { |
14890 | |
14891 | |
14892 | PushOnScopeChains(ExternCPrev, BlockScope, false); |
14893 | |
14894 | |
14895 | |
14896 | |
14897 | if (!isa<FunctionDecl>(ExternCPrev) || |
14898 | !Context.typesAreCompatible( |
14899 | cast<FunctionDecl>(ExternCPrev)->getType(), |
14900 | Context.getFunctionNoProtoType(Context.IntTy))) { |
14901 | Diag(Loc, diag::ext_use_out_of_scope_declaration) |
14902 | << ExternCPrev << !getLangOpts().C99; |
14903 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); |
14904 | return ExternCPrev; |
14905 | } |
14906 | } |
14907 | |
14908 | |
14909 | unsigned diag_id; |
14910 | if (II.getName().startswith("__builtin_")) |
14911 | diag_id = diag::warn_builtin_unknown; |
14912 | |
14913 | else if (getLangOpts().OpenCL) |
14914 | diag_id = diag::err_opencl_implicit_function_decl; |
14915 | else if (getLangOpts().C99) |
14916 | diag_id = diag::ext_implicit_function_decl; |
14917 | else |
14918 | diag_id = diag::warn_implicit_function_decl; |
14919 | Diag(Loc, diag_id) << &II; |
14920 | |
14921 | |
14922 | |
14923 | |
14924 | if (ExternCPrev) |
14925 | return ExternCPrev; |
14926 | |
14927 | |
14928 | |
14929 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { |
14930 | TypoCorrection Corrected; |
14931 | DeclFilterCCC<FunctionDecl> CCC{}; |
14932 | if (S && (Corrected = |
14933 | CorrectTypo(DeclarationNameInfo(&II, Loc), LookupOrdinaryName, |
14934 | S, nullptr, CCC, CTK_NonError))) |
14935 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), |
14936 | false); |
14937 | } |
14938 | |
14939 | |
14940 | const char *Dummy; |
14941 | AttributeFactory attrFactory; |
14942 | DeclSpec DS(attrFactory); |
14943 | unsigned DiagID; |
14944 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID, |
14945 | Context.getPrintingPolicy()); |
14946 | (void)Error; |
14947 | assert(!Error && "Error setting up implicit decl!"); |
14948 | SourceLocation NoLoc; |
14949 | Declarator D(DS, DeclaratorContext::Block); |
14950 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, |
14951 | false, |
14952 | NoLoc, |
14953 | nullptr, |
14954 | 0, |
14955 | NoLoc, |
14956 | NoLoc, |
14957 | true, |
14958 | NoLoc, |
14959 | NoLoc, EST_None, |
14960 | SourceRange(), |
14961 | nullptr, |
14962 | nullptr, |
14963 | 0, |
14964 | nullptr, |
14965 | nullptr, |
14966 | None, Loc, |
14967 | Loc, D), |
14968 | std::move(DS.getAttributes()), SourceLocation()); |
14969 | D.SetIdentifier(&II, Loc); |
14970 | |
14971 | |
14972 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(BlockScope, D)); |
14973 | FD->setImplicit(); |
14974 | |
14975 | AddKnownFunctionAttributes(FD); |
14976 | |
14977 | return FD; |
14978 | } |
14979 | |
14980 | |
14981 | |
14982 | |
14983 | |
14984 | |
14985 | |
14986 | void Sema::AddKnownFunctionAttributesForReplaceableGlobalAllocationFunction( |
14987 | FunctionDecl *FD) { |
14988 | if (FD->isInvalidDecl()) |
14989 | return; |
14990 | |
14991 | if (FD->getDeclName().getCXXOverloadedOperator() != OO_New && |
14992 | FD->getDeclName().getCXXOverloadedOperator() != OO_Array_New) |
14993 | return; |
14994 | |
14995 | Optional<unsigned> AlignmentParam; |
14996 | bool IsNothrow = false; |
14997 | if (!FD->isReplaceableGlobalAllocationFunction(&AlignmentParam, &IsNothrow)) |
14998 | return; |
14999 | |
15000 | |
15001 | |
15002 | |
15003 | |
15004 | |
15005 | if (!IsNothrow && !FD->hasAttr<ReturnsNonNullAttr>()) |
15006 | FD->addAttr(ReturnsNonNullAttr::CreateImplicit(Context, FD->getLocation())); |
15007 | |
15008 | |
15009 | |
15010 | |
15011 | |
15012 | |
15013 | |
15014 | |
15015 | |
15016 | |
15017 | |
15018 | |
15019 | |
15020 | |
15021 | |
15022 | if (!FD->hasAttr<AllocSizeAttr>()) { |
15023 | FD->addAttr(AllocSizeAttr::CreateImplicit( |
15024 | Context, ParamIdx(1, FD), |
15025 | ParamIdx(), FD->getLocation())); |
15026 | } |
15027 | |
15028 | |
15029 | |
15030 | |
15031 | |
15032 | |
15033 | |
15034 | if (AlignmentParam.hasValue() && !FD->hasAttr<AllocAlignAttr>()) { |
15035 | FD->addAttr(AllocAlignAttr::CreateImplicit( |
15036 | Context, ParamIdx(AlignmentParam.getValue(), FD), FD->getLocation())); |
15037 | } |
15038 | |
15039 | |
15040 | |
15041 | |
15042 | |
15043 | |
15044 | |
15045 | |
15046 | |
15047 | |
15048 | |
15049 | } |
15050 | |
15051 | |
15052 | |
15053 | |
15054 | |
15055 | |
15056 | |
15057 | |
15058 | |
15059 | |
15060 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
15061 | if (FD->isInvalidDecl()) |
15062 | return; |
15063 | |
15064 | |
15065 | |
15066 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
15067 | |
15068 | unsigned FormatIdx; |
15069 | bool HasVAListArg; |
15070 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { |
15071 | if (!FD->hasAttr<FormatAttr>()) { |
15072 | const char *fmt = "printf"; |
15073 | unsigned int NumParams = FD->getNumParams(); |
15074 | if (FormatIdx < NumParams && |
15075 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) |
15076 | fmt = "NSString"; |
15077 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
15078 | &Context.Idents.get(fmt), |
15079 | FormatIdx+1, |
15080 | HasVAListArg ? 0 : FormatIdx+2, |
15081 | FD->getLocation())); |
15082 | } |
15083 | } |
15084 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
15085 | HasVAListArg)) { |
15086 | if (!FD->hasAttr<FormatAttr>()) |
15087 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
15088 | &Context.Idents.get("scanf"), |
15089 | FormatIdx+1, |
15090 | HasVAListArg ? 0 : FormatIdx+2, |
15091 | FD->getLocation())); |
15092 | } |
15093 | |
15094 | |
15095 | SmallVector<int, 4> Encoding; |
15096 | if (!FD->hasAttr<CallbackAttr>() && |
15097 | Context.BuiltinInfo.performsCallback(BuiltinID, Encoding)) |
15098 | FD->addAttr(CallbackAttr::CreateImplicit( |
15099 | Context, Encoding.data(), Encoding.size(), FD->getLocation())); |
15100 | |
15101 | |
15102 | |
15103 | |
15104 | if (!getLangOpts().MathErrno && !FD->hasAttr<ConstAttr>() && |
15105 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) |
15106 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
15107 | |
15108 | |
15109 | |
15110 | |
15111 | const llvm::Triple &Trip = Context.getTargetInfo().getTriple(); |
15112 | if ((Trip.isGNUEnvironment() || Trip.isAndroid() || Trip.isOSMSVCRT()) && |
15113 | !FD->hasAttr<ConstAttr>()) { |
15114 | switch (BuiltinID) { |
15115 | case Builtin::BI__builtin_fma: |
15116 | case Builtin::BI__builtin_fmaf: |
15117 | case Builtin::BI__builtin_fmal: |
15118 | case Builtin::BIfma: |
15119 | case Builtin::BIfmaf: |
15120 | case Builtin::BIfmal: |
15121 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
15122 | break; |
15123 | default: |
15124 | break; |
15125 | } |
15126 | } |
15127 | |
15128 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
15129 | !FD->hasAttr<ReturnsTwiceAttr>()) |
15130 | FD->addAttr(ReturnsTwiceAttr::CreateImplicit(Context, |
15131 | FD->getLocation())); |
15132 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->hasAttr<NoThrowAttr>()) |
15133 | FD->addAttr(NoThrowAttr::CreateImplicit(Context, FD->getLocation())); |
15134 | if (Context.BuiltinInfo.isPure(BuiltinID) && !FD->hasAttr<PureAttr>()) |
15135 | FD->addAttr(PureAttr::CreateImplicit(Context, FD->getLocation())); |
15136 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->hasAttr<ConstAttr>()) |
15137 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
15138 | if (getLangOpts().CUDA && Context.BuiltinInfo.isTSBuiltin(BuiltinID) && |
15139 | !FD->hasAttr<CUDADeviceAttr>() && !FD->hasAttr<CUDAHostAttr>()) { |
15140 | |
15141 | |
15142 | |
15143 | if (getLangOpts().CUDAIsDevice != |
15144 | Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) |
15145 | FD->addAttr(CUDADeviceAttr::CreateImplicit(Context, FD->getLocation())); |
15146 | else |
15147 | FD->addAttr(CUDAHostAttr::CreateImplicit(Context, FD->getLocation())); |
15148 | } |
15149 | } |
15150 | |
15151 | AddKnownFunctionAttributesForReplaceableGlobalAllocationFunction(FD); |
15152 | |
15153 | |
15154 | |
15155 | |
15156 | if (getLangOpts().CXXExceptions && getLangOpts().ExternCNoUnwind && |
15157 | FD->isExternC() && !FD->hasAttr<NoThrowAttr>()) { |
15158 | const auto *FPT = FD->getType()->getAs<FunctionProtoType>(); |
15159 | if (!FPT || FPT->getExceptionSpecType() == EST_None) |
15160 | FD->addAttr(NoThrowAttr::CreateImplicit(Context, FD->getLocation())); |
15161 | } |
15162 | |
15163 | IdentifierInfo *Name = FD->getIdentifier(); |
15164 | if (!Name) |
15165 | return; |
15166 | if ((!getLangOpts().CPlusPlus && |
15167 | FD->getDeclContext()->isTranslationUnit()) || |
15168 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && |
15169 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
15170 | LinkageSpecDecl::lang_c)) { |
15171 | |
15172 | |
15173 | } else |
15174 | return; |
15175 | |
15176 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
15177 | |
15178 | |
15179 | if (!FD->hasAttr<FormatAttr>()) |
15180 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
15181 | &Context.Idents.get("printf"), 2, |
15182 | Name->isStr("vasprintf") ? 0 : 3, |
15183 | FD->getLocation())); |
15184 | } |
15185 | |
15186 | if (Name->isStr("__CFStringMakeConstantString")) { |
15187 | |
15188 | |
15189 | if (!FD->hasAttr<FormatArgAttr>()) |
15190 | FD->addAttr(FormatArgAttr::CreateImplicit(Context, ParamIdx(1, FD), |
15191 | FD->getLocation())); |
15192 | } |
15193 | } |
15194 | |
15195 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
15196 | TypeSourceInfo *TInfo) { |
15197 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
15198 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
15199 | |
15200 | if (!TInfo) { |
15201 | assert(D.isInvalidType() && "no declarator info for valid type"); |
15202 | TInfo = Context.getTrivialTypeSourceInfo(T); |
15203 | } |
15204 | |
15205 | |
15206 | TypedefDecl *NewTD = |
15207 | TypedefDecl::Create(Context, CurContext, D.getBeginLoc(), |
15208 | D.getIdentifierLoc(), D.getIdentifier(), TInfo); |
15209 | |
15210 | |
15211 | if (D.isInvalidType()) { |
15212 | NewTD->setInvalidDecl(); |
15213 | return NewTD; |
15214 | } |
15215 | |
15216 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
15217 | if (CurContext->isFunctionOrMethod()) |
15218 | Diag(NewTD->getLocation(), diag::err_module_private_local) |
15219 | << 2 << NewTD |
15220 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
15221 | << FixItHint::CreateRemoval( |
15222 | D.getDeclSpec().getModulePrivateSpecLoc()); |
15223 | else |
15224 | NewTD->setModulePrivate(); |
15225 | } |
15226 | |
15227 | |
15228 | |
15229 | |
15230 | |
15231 | |
15232 | |
15233 | switch (D.getDeclSpec().getTypeSpecType()) { |
15234 | case TST_enum: |
15235 | case TST_struct: |
15236 | case TST_interface: |
15237 | case TST_union: |
15238 | case TST_class: { |
15239 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
15240 | setTagNameForLinkagePurposes(tagFromDeclSpec, NewTD); |
15241 | break; |
15242 | } |
15243 | |
15244 | default: |
15245 | break; |
15246 | } |
15247 | |
15248 | return NewTD; |
15249 | } |
15250 | |
15251 | |
15252 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { |
15253 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
15254 | QualType T = TI->getType(); |
15255 | |
15256 | if (T->isDependentType()) |
15257 | return false; |
15258 | |
15259 | |
15260 | |
15261 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
15262 | if (BT->isInteger()) |
15263 | return false; |
15264 | |
15265 | if (T->isExtIntType()) |
15266 | return false; |
15267 | |
15268 | return Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
15269 | } |
15270 | |
15271 | |
15272 | |
15273 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, |
15274 | QualType EnumUnderlyingTy, bool IsFixed, |
15275 | const EnumDecl *Prev) { |
15276 | if (IsScoped != Prev->isScoped()) { |
15277 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) |
15278 | << Prev->isScoped(); |
15279 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
15280 | return true; |
15281 | } |
15282 | |
15283 | if (IsFixed && Prev->isFixed()) { |
15284 | if (!EnumUnderlyingTy->isDependentType() && |
15285 | !Prev->getIntegerType()->isDependentType() && |
15286 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, |
15287 | Prev->getIntegerType())) { |
15288 | |
15289 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) |
15290 | << EnumUnderlyingTy << Prev->getIntegerType(); |
15291 | Diag(Prev->getLocation(), diag::note_previous_declaration) |
15292 | << Prev->getIntegerTypeRange(); |
15293 | return true; |
15294 | } |
15295 | } else if (IsFixed != Prev->isFixed()) { |
15296 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) |
15297 | << Prev->isFixed(); |
15298 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
15299 | return true; |
15300 | } |
15301 | |
15302 | return false; |
15303 | } |
15304 | |
15305 | |
15306 | |
15307 | |
15308 | |
15309 | |
15310 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
15311 | switch (Tag) { |
15312 | case TTK_Struct: return 0; |
15313 | case TTK_Interface: return 1; |
15314 | case TTK_Class: return 2; |
15315 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); |
15316 | } |
15317 | } |
15318 | |
15319 | |
15320 | |
15321 | |
15322 | |
15323 | static bool isClassCompatTagKind(TagTypeKind Tag) |
15324 | { |
15325 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; |
15326 | } |
15327 | |
15328 | Sema::NonTagKind Sema::getNonTagTypeDeclKind(const Decl *PrevDecl, |
15329 | TagTypeKind TTK) { |
15330 | if (isa<TypedefDecl>(PrevDecl)) |
15331 | return NTK_Typedef; |
15332 | else if (isa<TypeAliasDecl>(PrevDecl)) |
15333 | return NTK_TypeAlias; |
15334 | else if (isa<ClassTemplateDecl>(PrevDecl)) |
15335 | return NTK_Template; |
15336 | else if (isa<TypeAliasTemplateDecl>(PrevDecl)) |
15337 | return NTK_TypeAliasTemplate; |
15338 | else if (isa<TemplateTemplateParmDecl>(PrevDecl)) |
15339 | return NTK_TemplateTemplateArgument; |
15340 | switch (TTK) { |
15341 | case TTK_Struct: |
15342 | case TTK_Interface: |
15343 | case TTK_Class: |
15344 | return getLangOpts().CPlusPlus ? NTK_NonClass : NTK_NonStruct; |
15345 | case TTK_Union: |
15346 | return NTK_NonUnion; |
15347 | case TTK_Enum: |
15348 | return NTK_NonEnum; |
15349 | } |
15350 | llvm_unreachable("invalid TTK"); |
15351 | } |
15352 | |
15353 | |
15354 | |
15355 | |
15356 | |
15357 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
15358 | TagTypeKind NewTag, bool isDefinition, |
15359 | SourceLocation NewTagLoc, |
15360 | const IdentifierInfo *Name) { |
15361 | |
15362 | |
15363 | |
15364 | |
15365 | |
15366 | |
15367 | |
15368 | |
15369 | |
15370 | |
15371 | |
15372 | |
15373 | |
15374 | TagTypeKind OldTag = Previous->getTagKind(); |
15375 | if (OldTag != NewTag && |
15376 | !(isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag))) |
15377 | return false; |
15378 | |
15379 | |
15380 | |
15381 | if (!isClassCompatTagKind(NewTag)) |
15382 | return true; |
15383 | |
15384 | |
15385 | |
15386 | |
15387 | |
15388 | |
15389 | auto IsIgnoredLoc = [&](SourceLocation Loc) { |
15390 | return getDiagnostics().isIgnored(diag::warn_struct_class_tag_mismatch, |
15391 | Loc); |
15392 | }; |
15393 | if (IsIgnoredLoc(NewTagLoc)) |
15394 | return true; |
15395 | |
15396 | auto IsIgnored = [&](const TagDecl *Tag) { |
15397 | return IsIgnoredLoc(Tag->getLocation()); |
15398 | }; |
15399 | while (IsIgnored(Previous)) { |
15400 | Previous = Previous->getPreviousDecl(); |
15401 | if (!Previous) |
15402 | return true; |
15403 | OldTag = Previous->getTagKind(); |
15404 | } |
15405 | |
15406 | bool isTemplate = false; |
15407 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) |
15408 | isTemplate = Record->getDescribedClassTemplate(); |
15409 | |
15410 | if (inTemplateInstantiation()) { |
15411 | if (OldTag != NewTag) { |
15412 | |
15413 | |
15414 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
15415 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << Name |
15416 | << getRedeclDiagFromTagKind(OldTag); |
15417 | |
15418 | } |
15419 | return true; |
15420 | } |
15421 | |
15422 | if (isDefinition) { |
15423 | |
15424 | |
15425 | if (Previous->getDefinition()) { |
15426 | |
15427 | return true; |
15428 | } |
15429 | |
15430 | bool previousMismatch = false; |
15431 | for (const TagDecl *I : Previous->redecls()) { |
15432 | if (I->getTagKind() != NewTag) { |
15433 | |
15434 | if (IsIgnored(I)) |
15435 | continue; |
15436 | |
15437 | if (!previousMismatch) { |
15438 | previousMismatch = true; |
15439 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) |
15440 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << Name |
15441 | << getRedeclDiagFromTagKind(I->getTagKind()); |
15442 | } |
15443 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) |
15444 | << getRedeclDiagFromTagKind(NewTag) |
15445 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
15446 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
15447 | } |
15448 | } |
15449 | return true; |
15450 | } |
15451 | |
15452 | |
15453 | |
15454 | |
15455 | const TagDecl *PrevDef = Previous->getDefinition(); |
15456 | if (PrevDef && IsIgnored(PrevDef)) |
15457 | PrevDef = nullptr; |
15458 | const TagDecl *Redecl = PrevDef ? PrevDef : Previous; |
15459 | if (Redecl->getTagKind() != NewTag) { |
15460 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
15461 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << Name |
15462 | << getRedeclDiagFromTagKind(OldTag); |
15463 | Diag(Redecl->getLocation(), diag::note_previous_use); |
15464 | |
15465 | |
15466 | if (PrevDef) { |
15467 | Diag(NewTagLoc, diag::note_struct_class_suggestion) |
15468 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
15469 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
15470 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
15471 | } |
15472 | } |
15473 | |
15474 | return true; |
15475 | } |
15476 | |
15477 | |
15478 | |
15479 | |
15480 | |
15481 | |
15482 | |
15483 | |
15484 | |
15485 | |
15486 | static FixItHint createFriendTagNNSFixIt(Sema &SemaRef, NamedDecl *ND, Scope *S, |
15487 | SourceLocation NameLoc) { |
15488 | |
15489 | |
15490 | |
15491 | SmallVector<IdentifierInfo *, 4> Namespaces; |
15492 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
15493 | for (; !DC->isTranslationUnit(); DC = DC->getParent()) { |
15494 | |
15495 | |
15496 | NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC); |
15497 | if (!Namespace || Namespace->isAnonymousNamespace()) |
15498 | return FixItHint(); |
15499 | IdentifierInfo *II = Namespace->getIdentifier(); |
15500 | Namespaces.push_back(II); |
15501 | NamedDecl *Lookup = SemaRef.LookupSingleName( |
15502 | S, II, NameLoc, Sema::LookupNestedNameSpecifierName); |
15503 | if (Lookup == Namespace) |
15504 | break; |
15505 | } |
15506 | |
15507 | |
15508 | |
15509 | SmallString<64> Insertion; |
15510 | llvm::raw_svector_ostream OS(Insertion); |
15511 | if (DC->isTranslationUnit()) |
15512 | OS << "::"; |
15513 | std::reverse(Namespaces.begin(), Namespaces.end()); |
15514 | for (auto *II : Namespaces) |
15515 | OS << II->getName() << "::"; |
15516 | return FixItHint::CreateInsertion(NameLoc, Insertion); |
15517 | } |
15518 | |
15519 | |
15520 | |
15521 | |
15522 | |
15523 | static bool isAcceptableTagRedeclContext(Sema &S, DeclContext *OldDC, |
15524 | DeclContext *NewDC) { |
15525 | OldDC = OldDC->getRedeclContext(); |
15526 | NewDC = NewDC->getRedeclContext(); |
15527 | |
15528 | if (OldDC->Equals(NewDC)) |
15529 | return true; |
15530 | |
15531 | |
15532 | |
15533 | if (S.getLangOpts().MSVCCompat && |
15534 | (OldDC->Encloses(NewDC) || NewDC->Encloses(OldDC))) |
15535 | return true; |
15536 | |
15537 | return false; |
15538 | } |
15539 | |
15540 | |
15541 | |
15542 | |
15543 | |
15544 | |
15545 | |
15546 | |
15547 | |
15548 | |
15549 | |
15550 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
15551 | SourceLocation KWLoc, CXXScopeSpec &SS, |
15552 | IdentifierInfo *Name, SourceLocation NameLoc, |
15553 | const ParsedAttributesView &Attrs, AccessSpecifier AS, |
15554 | SourceLocation ModulePrivateLoc, |
15555 | MultiTemplateParamsArg TemplateParameterLists, |
15556 | bool &OwnedDecl, bool &IsDependent, |
15557 | SourceLocation ScopedEnumKWLoc, |
15558 | bool ScopedEnumUsesClassTag, TypeResult UnderlyingType, |
15559 | bool IsTypeSpecifier, bool IsTemplateParamOrArg, |
15560 | SkipBodyInfo *SkipBody) { |
15561 | |
15562 | IdentifierInfo *OrigName = Name; |
15563 | assert((Name != nullptr || TUK == TUK_Definition) && |
15564 | "Nameless record must be a definition!"); |
15565 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
15566 | |
15567 | OwnedDecl = false; |
15568 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
15569 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
15570 | |
15571 | |
15572 | bool isMemberSpecialization = false; |
15573 | bool Invalid = false; |
15574 | |
15575 | |
15576 | |
15577 | |
15578 | if (TemplateParameterLists.size() > 0 || |
15579 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
15580 | if (TemplateParameterList *TemplateParams = |
15581 | MatchTemplateParametersToScopeSpecifier( |
15582 | KWLoc, NameLoc, SS, nullptr, TemplateParameterLists, |
15583 | TUK == TUK_Friend, isMemberSpecialization, Invalid)) { |
15584 | if (Kind == TTK_Enum) { |
15585 | Diag(KWLoc, diag::err_enum_template); |
15586 | return nullptr; |
15587 | } |
15588 | |
15589 | if (TemplateParams->size() > 0) { |
15590 | |
15591 | |
15592 | |
15593 | if (Invalid) |
15594 | return nullptr; |
15595 | |
15596 | OwnedDecl = false; |
15597 | DeclResult Result = CheckClassTemplate( |
15598 | S, TagSpec, TUK, KWLoc, SS, Name, NameLoc, Attrs, TemplateParams, |
15599 | AS, ModulePrivateLoc, |
15600 | SourceLocation(), TemplateParameterLists.size() - 1, |
15601 | TemplateParameterLists.data(), SkipBody); |
15602 | return Result.get(); |
15603 | } else { |
15604 | |
15605 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
15606 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
15607 | isMemberSpecialization = true; |
15608 | } |
15609 | } |
15610 | |
15611 | if (!TemplateParameterLists.empty() && isMemberSpecialization && |
15612 | CheckTemplateDeclScope(S, TemplateParameterLists.back())) |
15613 | return nullptr; |
15614 | } |
15615 | |
15616 | |
15617 | |
15618 | |
15619 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; |
15620 | bool IsFixed = !UnderlyingType.isUnset() || ScopedEnum; |
15621 | |
15622 | if (Kind == TTK_Enum) { |
15623 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) { |
15624 | |
15625 | |
15626 | EnumUnderlying = Context.IntTy.getTypePtr(); |
15627 | } else if (UnderlyingType.get()) { |
15628 | |
15629 | |
15630 | TypeSourceInfo *TI = nullptr; |
15631 | GetTypeFromParser(UnderlyingType.get(), &TI); |
15632 | EnumUnderlying = TI; |
15633 | |
15634 | if (CheckEnumUnderlyingType(TI)) |
15635 | |
15636 | EnumUnderlying = Context.IntTy.getTypePtr(); |
15637 | |
15638 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
15639 | UPPC_FixedUnderlyingType)) |
15640 | EnumUnderlying = Context.IntTy.getTypePtr(); |
15641 | |
15642 | } else if (Context.getTargetInfo().getTriple().isWindowsMSVCEnvironment()) { |
15643 | |
15644 | |
15645 | |
15646 | |
15647 | if (TUK == TUK_Definition || getLangOpts().MSVCCompat) |
15648 | EnumUnderlying = Context.IntTy.getTypePtr(); |
15649 | } |
15650 | } |
15651 | |
15652 | DeclContext *SearchDC = CurContext; |
15653 | DeclContext *DC = CurContext; |
15654 | bool isStdBadAlloc = false; |
15655 | bool isStdAlignValT = false; |
15656 | |
15657 | RedeclarationKind Redecl = forRedeclarationInCurContext(); |
15658 | if (TUK == TUK_Friend || TUK == TUK_Reference) |
15659 | Redecl = NotForRedeclaration; |
15660 | |
15661 | |
15662 | |
15663 | |
15664 | auto createTagFromNewDecl = [&]() -> TagDecl * { |
15665 | assert(!getLangOpts().CPlusPlus && "not meant for C++ usage"); |
15666 | |
15667 | |
15668 | |
15669 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
15670 | TagDecl *New = nullptr; |
15671 | |
15672 | if (Kind == TTK_Enum) { |
15673 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, nullptr, |
15674 | ScopedEnum, ScopedEnumUsesClassTag, IsFixed); |
15675 | |
15676 | if (TUK != TUK_Definition && !Invalid) |
15677 | return nullptr; |
15678 | if (EnumUnderlying) { |
15679 | EnumDecl *ED = cast<EnumDecl>(New); |
15680 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo *>()) |
15681 | ED->setIntegerTypeSourceInfo(TI); |
15682 | else |
15683 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type *>(), 0)); |
15684 | ED->setPromotionType(ED->getIntegerType()); |
15685 | } |
15686 | } else { |
15687 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
15688 | nullptr); |
15689 | } |
15690 | |
15691 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
15692 | |
15693 | |
15694 | |
15695 | |
15696 | |
15697 | |
15698 | |
15699 | |
15700 | |
15701 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
15702 | AddAlignmentAttributesForRecord(RD); |
15703 | AddMsStructLayoutForRecord(RD); |
15704 | } |
15705 | } |
15706 | New->setLexicalDeclContext(CurContext); |
15707 | return New; |
15708 | }; |
15709 | |
15710 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); |
15711 | if (Name && SS.isNotEmpty()) { |
15712 | |
15713 | |
15714 | |
15715 | if (SS.isInvalid()) { |
15716 | Name = nullptr; |
15717 | goto CreateNewDecl; |
15718 | } |
15719 | |
15720 | |
15721 | |
15722 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
15723 | DC = computeDeclContext(SS, false); |
15724 | if (!DC) { |
15725 | IsDependent = true; |
15726 | return nullptr; |
15727 | } |
15728 | } else { |
15729 | DC = computeDeclContext(SS, true); |
15730 | if (!DC) { |
15731 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) |
15732 | << SS.getRange(); |
15733 | return nullptr; |
15734 | } |
15735 | } |
15736 | |
15737 | if (RequireCompleteDeclContext(SS, DC)) |
15738 | return nullptr; |
15739 | |
15740 | SearchDC = DC; |
15741 | |
15742 | LookupQualifiedName(Previous, DC); |
15743 | |
15744 | if (Previous.isAmbiguous()) |
15745 | return nullptr; |
15746 | |
15747 | if (Previous.empty()) { |
15748 | |
15749 | |
15750 | |
15751 | |
15752 | |
15753 | |
15754 | if (Previous.wasNotFoundInCurrentInstantiation() && |
15755 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
15756 | IsDependent = true; |
15757 | return nullptr; |
15758 | } |
15759 | |
15760 | |
15761 | Diag(NameLoc, diag::err_not_tag_in_scope) |
15762 | << Kind << Name << DC << SS.getRange(); |
15763 | Name = nullptr; |
15764 | Invalid = true; |
15765 | goto CreateNewDecl; |
15766 | } |
15767 | } else if (Name) { |
15768 | |
15769 | |
15770 | |
15771 | |
15772 | if (TUK != TUK_Reference && TUK != TUK_Friend && |
15773 | DiagnoseClassNameShadow(SearchDC, DeclarationNameInfo(Name, NameLoc))) |
15774 | return nullptr; |
15775 | |
15776 | |
15777 | |
15778 | |
15779 | |
15780 | |
15781 | LookupName(Previous, S); |
15782 | |
15783 | |
15784 | |
15785 | if (Previous.isAmbiguous() && |
15786 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { |
15787 | LookupResult::Filter F = Previous.makeFilter(); |
15788 | while (F.hasNext()) { |
15789 | NamedDecl *ND = F.next(); |
15790 | if (!ND->getDeclContext()->getRedeclContext()->Equals( |
15791 | SearchDC->getRedeclContext())) |
15792 | F.erase(); |
15793 | } |
15794 | F.done(); |
15795 | } |
15796 | |
15797 | |
15798 | |
15799 | |
15800 | |
15801 | |
15802 | |
15803 | |
15804 | |
15805 | |
15806 | |
15807 | |
15808 | |
15809 | |
15810 | if (!Previous.empty() && TUK == TUK_Friend) { |
15811 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); |
15812 | LookupResult::Filter F = Previous.makeFilter(); |
15813 | bool FriendSawTagOutsideEnclosingNamespace = false; |
15814 | while (F.hasNext()) { |
15815 | NamedDecl *ND = F.next(); |
15816 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
15817 | if (DC->isFileContext() && |
15818 | !EnclosingNS->Encloses(ND->getDeclContext())) { |
15819 | if (getLangOpts().MSVCCompat) |
15820 | FriendSawTagOutsideEnclosingNamespace = true; |
15821 | else |
15822 | F.erase(); |
15823 | } |
15824 | } |
15825 | F.done(); |
15826 | |
15827 | |
15828 | |
15829 | if (Previous.isSingleResult() && FriendSawTagOutsideEnclosingNamespace) { |
15830 | NamedDecl *ND = Previous.getFoundDecl(); |
15831 | Diag(NameLoc, diag::ext_friend_tag_redecl_outside_namespace) |
15832 | << createFriendTagNNSFixIt(*this, ND, S, NameLoc); |
15833 | } |
15834 | } |
15835 | |
15836 | |
15837 | if (Previous.isAmbiguous()) |
15838 | return nullptr; |
15839 | |
15840 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
15841 | |
15842 | |
15843 | |
15844 | |
15845 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
15846 | SearchDC = SearchDC->getParent(); |
15847 | } |
15848 | } |
15849 | |
15850 | if (Previous.isSingleResult() && |
15851 | Previous.getFoundDecl()->isTemplateParameter()) { |
15852 | |
15853 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
15854 | |
15855 | Previous.clear(); |
15856 | } |
15857 | |
15858 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
15859 | DC->Equals(getStdNamespace())) { |
15860 | if (Name->isStr("bad_alloc")) { |
15861 | |
15862 | isStdBadAlloc = true; |
15863 | |
15864 | |
15865 | |
15866 | |
15867 | if (Previous.empty() && StdBadAlloc) |
15868 | Previous.addDecl(getStdBadAlloc()); |
15869 | } else if (Name->isStr("align_val_t")) { |
15870 | isStdAlignValT = true; |
15871 | if (Previous.empty() && StdAlignValT) |
15872 | Previous.addDecl(getStdAlignValT()); |
15873 | } |
15874 | } |
15875 | |
15876 | |
15877 | |
15878 | |
15879 | |
15880 | if (Name && Previous.empty() && |
15881 | (TUK == TUK_Reference || TUK == TUK_Friend || IsTemplateParamOrArg)) { |
15882 | if (Invalid) goto CreateNewDecl; |
15883 | assert(SS.isEmpty()); |
15884 | |
15885 | if (TUK == TUK_Reference || IsTemplateParamOrArg) { |
15886 | |
15887 | |
15888 | |
15889 | |
15890 | |
15891 | |
15892 | |
15893 | |
15894 | |
15895 | |
15896 | |
15897 | |
15898 | |
15899 | |
15900 | |
15901 | |
15902 | |
15903 | |
15904 | |
15905 | |
15906 | |
15907 | |
15908 | |
15909 | |
15910 | |
15911 | |
15912 | |
15913 | |
15914 | SearchDC = getTagInjectionContext(SearchDC); |
15915 | |
15916 | |
15917 | S = getTagInjectionScope(S, getLangOpts()); |
15918 | } else { |
15919 | assert(TUK == TUK_Friend); |
15920 | |
15921 | |
15922 | |
15923 | |
15924 | SearchDC = SearchDC->getEnclosingNamespaceContext(); |
15925 | } |
15926 | |
15927 | |
15928 | |
15929 | |
15930 | |
15931 | |
15932 | |
15933 | if (getLangOpts().CPlusPlus) { |
15934 | |
15935 | |
15936 | Previous.setRedeclarationKind(forRedeclarationInCurContext()); |
15937 | LookupQualifiedName(Previous, SearchDC); |
15938 | } else { |
15939 | Previous.setRedeclarationKind(forRedeclarationInCurContext()); |
15940 | LookupName(Previous, S); |
15941 | } |
15942 | } |
15943 | |
15944 | |
15945 | if (Previous.empty() && SkipBody && SkipBody->Previous) |
15946 | Previous.addDecl(SkipBody->Previous); |
15947 | |
15948 | if (!Previous.empty()) { |
15949 | NamedDecl *PrevDecl = Previous.getFoundDecl(); |
15950 | NamedDecl *DirectPrevDecl = Previous.getRepresentativeDecl(); |
15951 | |
15952 | |
15953 | |
15954 | |
15955 | |
15956 | |
15957 | |
15958 | |
15959 | |
15960 | |
15961 | if (getLangOpts().CPlusPlus) { |
15962 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
15963 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
15964 | TagDecl *Tag = TT->getDecl(); |
15965 | if (Tag->getDeclName() == Name && |
15966 | Tag->getDeclContext()->getRedeclContext() |
15967 | ->Equals(TD->getDeclContext()->getRedeclContext())) { |
15968 | PrevDecl = Tag; |
15969 | Previous.clear(); |
15970 | Previous.addDecl(Tag); |
15971 | Previous.resolveKind(); |
15972 | } |
15973 | } |
15974 | } |
15975 | } |
15976 | |
15977 | |
15978 | |
15979 | |
15980 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(DirectPrevDecl)) { |
15981 | auto *OldTag = dyn_cast<TagDecl>(PrevDecl); |
15982 | if (SS.isEmpty() && TUK != TUK_Reference && TUK != TUK_Friend && |
15983 | isDeclInScope(Shadow, SearchDC, S, isMemberSpecialization) && |
15984 | !(OldTag && isAcceptableTagRedeclContext( |
15985 | *this, OldTag->getDeclContext(), SearchDC))) { |
15986 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
15987 | Diag(Shadow->getTargetDecl()->getLocation(), |
15988 | diag::note_using_decl_target); |
15989 | Diag(Shadow->getIntroducer()->getLocation(), diag::note_using_decl) |
15990 | << 0; |
15991 | |
15992 | Previous.clear(); |
15993 | goto CreateNewDecl; |
15994 | } |
15995 | } |
15996 | |
15997 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
15998 | |
15999 | |
16000 | |
16001 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
16002 | isDeclInScope(DirectPrevDecl, SearchDC, S, |
16003 | SS.isNotEmpty() || isMemberSpecialization)) { |
16004 | |
16005 | |
16006 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
16007 | TUK == TUK_Definition, KWLoc, |
16008 | Name)) { |
16009 | bool SafeToContinue |
16010 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
16011 | Kind != TTK_Enum); |
16012 | if (SafeToContinue) |
16013 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
16014 | << Name |
16015 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
16016 | PrevTagDecl->getKindName()); |
16017 | else |
16018 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
16019 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
16020 | |
16021 | if (SafeToContinue) |
16022 | Kind = PrevTagDecl->getTagKind(); |
16023 | else { |
16024 | |
16025 | Name = nullptr; |
16026 | Previous.clear(); |
16027 | Invalid = true; |
16028 | } |
16029 | } |
16030 | |
16031 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
16032 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); |
16033 | if (TUK == TUK_Reference || TUK == TUK_Friend) |
16034 | return PrevTagDecl; |
16035 | |
16036 | QualType EnumUnderlyingTy; |
16037 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
16038 | EnumUnderlyingTy = TI->getType().getUnqualifiedType(); |
16039 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) |
16040 | EnumUnderlyingTy = QualType(T, 0); |
16041 | |
16042 | |
16043 | |
16044 | |
16045 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
16046 | ScopedEnum, EnumUnderlyingTy, |
16047 | IsFixed, PrevEnum)) |
16048 | return TUK == TUK_Declaration ? PrevTagDecl : nullptr; |
16049 | } |
16050 | |
16051 | |
16052 | |
16053 | |
16054 | |
16055 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && |
16056 | S->isDeclScope(PrevDecl)) { |
16057 | Diag(NameLoc, diag::ext_member_redeclared); |
16058 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); |
16059 | } |
16060 | |
16061 | if (!Invalid) { |
16062 | |
16063 | |
16064 | if (TUK == TUK_Reference || TUK == TUK_Friend) { |
16065 | if (!Attrs.empty()) { |
16066 | |
16067 | |
16068 | } else if (TUK == TUK_Reference && |
16069 | (PrevTagDecl->getFriendObjectKind() == |
16070 | Decl::FOK_Undeclared || |
16071 | PrevDecl->getOwningModule() != getCurrentModule()) && |
16072 | SS.isEmpty()) { |
16073 | |
16074 | |
16075 | |
16076 | |
16077 | |
16078 | |
16079 | |
16080 | if (!getTagInjectionContext(CurContext)->getRedeclContext() |
16081 | ->Equals(PrevDecl->getDeclContext()->getRedeclContext())) |
16082 | return PrevTagDecl; |
16083 | |
16084 | |
16085 | S = getTagInjectionScope(S, getLangOpts()); |
16086 | } else { |
16087 | return PrevTagDecl; |
16088 | } |
16089 | } |
16090 | |
16091 | |
16092 | if (TUK == TUK_Definition) { |
16093 | if (NamedDecl *Def = PrevTagDecl->getDefinition()) { |
16094 | |
16095 | |
16096 | |
16097 | bool IsExplicitSpecializationAfterInstantiation = false; |
16098 | if (isMemberSpecialization) { |
16099 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) |
16100 | IsExplicitSpecializationAfterInstantiation = |
16101 | RD->getTemplateSpecializationKind() != |
16102 | TSK_ExplicitSpecialization; |
16103 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) |
16104 | IsExplicitSpecializationAfterInstantiation = |
16105 | ED->getTemplateSpecializationKind() != |
16106 | TSK_ExplicitSpecialization; |
16107 | } |
16108 | |
16109 | |
16110 | |
16111 | |
16112 | |
16113 | NamedDecl *Hidden = nullptr; |
16114 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
16115 | |
16116 | |
16117 | |
16118 | |
16119 | |
16120 | if (!getLangOpts().CPlusPlus) { |
16121 | |
16122 | |
16123 | |
16124 | SkipBody->CheckSameAsPrevious = true; |
16125 | SkipBody->New = createTagFromNewDecl(); |
16126 | SkipBody->Previous = Def; |
16127 | return Def; |
16128 | } else { |
16129 | SkipBody->ShouldSkip = true; |
16130 | SkipBody->Previous = Def; |
16131 | makeMergedDefinitionVisible(Hidden); |
16132 | |
16133 | |
16134 | } |
16135 | } else if (!IsExplicitSpecializationAfterInstantiation) { |
16136 | |
16137 | |
16138 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
16139 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
16140 | else |
16141 | Diag(NameLoc, diag::err_redefinition) << Name; |
16142 | notePreviousDefinition(Def, |
16143 | NameLoc.isValid() ? NameLoc : KWLoc); |
16144 | |
16145 | |
16146 | |
16147 | Name = nullptr; |
16148 | Previous.clear(); |
16149 | Invalid = true; |
16150 | } |
16151 | } else { |
16152 | |
16153 | |
16154 | auto *TD = Context.getTagDeclType(PrevTagDecl)->getAsTagDecl(); |
16155 | if (TD->isBeingDefined()) { |
16156 | Diag(NameLoc, diag::err_nested_redefinition) << Name; |
16157 | Diag(PrevTagDecl->getLocation(), |
16158 | diag::note_previous_definition); |
16159 | Name = nullptr; |
16160 | Previous.clear(); |
16161 | Invalid = true; |
16162 | } |
16163 | } |
16164 | |
16165 | |
16166 | |
16167 | } |
16168 | |
16169 | |
16170 | |
16171 | |
16172 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
16173 | SearchDC = PrevTagDecl->getDeclContext(); |
16174 | AS = AS_none; |
16175 | } |
16176 | } |
16177 | |
16178 | |
16179 | |
16180 | } else { |
16181 | |
16182 | |
16183 | |
16184 | |
16185 | Previous.clear(); |
16186 | } |
16187 | |
16188 | |
16189 | |
16190 | |
16191 | |
16192 | |
16193 | |
16194 | } else { |
16195 | |
16196 | |
16197 | |
16198 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && |
16199 | !Previous.isForRedeclaration()) { |
16200 | NonTagKind NTK = getNonTagTypeDeclKind(PrevDecl, Kind); |
16201 | Diag(NameLoc, diag::err_tag_reference_non_tag) << PrevDecl << NTK |
16202 | << Kind; |
16203 | Diag(PrevDecl->getLocation(), diag::note_declared_at); |
16204 | Invalid = true; |
16205 | |
16206 | |
16207 | } else if (!isDeclInScope(DirectPrevDecl, SearchDC, S, |
16208 | SS.isNotEmpty() || isMemberSpecialization)) { |
16209 | |
16210 | |
16211 | |
16212 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { |
16213 | NonTagKind NTK = getNonTagTypeDeclKind(PrevDecl, Kind); |
16214 | Diag(NameLoc, diag::err_tag_reference_conflict) << NTK; |
16215 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
16216 | Invalid = true; |
16217 | |
16218 | |
16219 | |
16220 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
16221 | unsigned Kind = 0; |
16222 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; |
16223 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
16224 | << Name << Kind << TND->getUnderlyingType(); |
16225 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
16226 | Invalid = true; |
16227 | |
16228 | |
16229 | } else { |
16230 | |
16231 | |
16232 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
16233 | notePreviousDefinition(PrevDecl, NameLoc); |
16234 | Name = nullptr; |
16235 | Invalid = true; |
16236 | } |
16237 | |
16238 | |
16239 | |
16240 | Previous.clear(); |
16241 | } |
16242 | } |
16243 | |
16244 | CreateNewDecl: |
16245 | |
16246 | TagDecl *PrevDecl = nullptr; |
16247 | if (Previous.isSingleResult()) |
16248 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); |
16249 | |
16250 | |
16251 | |
16252 | |
16253 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
16254 | |
16255 | |
16256 | |
16257 | |
16258 | TagDecl *New; |
16259 | |
16260 | if (Kind == TTK_Enum) { |
16261 | |
16262 | |
16263 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
16264 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
16265 | ScopedEnumUsesClassTag, IsFixed); |
16266 | |
16267 | if (isStdAlignValT && (!StdAlignValT || getStdAlignValT()->isImplicit())) |
16268 | StdAlignValT = cast<EnumDecl>(New); |
16269 | |
16270 | |
16271 | if (TUK != TUK_Definition && !Invalid) { |
16272 | TagDecl *Def; |
16273 | if (IsFixed && cast<EnumDecl>(New)->isFixed()) { |
16274 | |
16275 | |
16276 | } |
16277 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { |
16278 | Diag(Loc, diag::ext_forward_ref_enum_def) |
16279 | << New; |
16280 | Diag(Def->getLocation(), diag::note_previous_definition); |
16281 | } else { |
16282 | unsigned DiagID = diag::ext_forward_ref_enum; |
16283 | if (getLangOpts().MSVCCompat) |
16284 | DiagID = diag::ext_ms_forward_ref_enum; |
16285 | else if (getLangOpts().CPlusPlus) |
16286 | DiagID = diag::err_forward_ref_enum; |
16287 | Diag(Loc, DiagID); |
16288 | } |
16289 | } |
16290 | |
16291 | if (EnumUnderlying) { |
16292 | EnumDecl *ED = cast<EnumDecl>(New); |
16293 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
16294 | ED->setIntegerTypeSourceInfo(TI); |
16295 | else |
16296 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); |
16297 | ED->setPromotionType(ED->getIntegerType()); |
16298 | assert(ED->isComplete() && "enum with type should be complete"); |
16299 | } |
16300 | } else { |
16301 | |
16302 | |
16303 | |
16304 | |
16305 | if (getLangOpts().CPlusPlus) { |
16306 | |
16307 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
16308 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
16309 | |
16310 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
16311 | StdBadAlloc = cast<CXXRecordDecl>(New); |
16312 | } else |
16313 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
16314 | cast_or_null<RecordDecl>(PrevDecl)); |
16315 | } |
16316 | |
16317 | |
16318 | |
16319 | if (getLangOpts().CPlusPlus && (IsTypeSpecifier || IsTemplateParamOrArg) && |
16320 | TUK == TUK_Definition) { |
16321 | Diag(New->getLocation(), diag::err_type_defined_in_type_specifier) |
16322 | << Context.getTagDeclType(New); |
16323 | Invalid = true; |
16324 | } |
16325 | |
16326 | if (!Invalid && getLangOpts().CPlusPlus && TUK == TUK_Definition && |
16327 | DC->getDeclKind() == Decl::Enum) { |
16328 | Diag(New->getLocation(), diag::err_type_defined_in_enum) |
16329 | << Context.getTagDeclType(New); |
16330 | Invalid = true; |
16331 | } |
16332 | |
16333 | |
16334 | if (SS.isNotEmpty()) { |
16335 | if (SS.isSet()) { |
16336 | |
16337 | |
16338 | if ((TUK == TUK_Definition || TUK == TUK_Declaration) && |
16339 | diagnoseQualifiedDeclaration(SS, DC, OrigName, Loc, |
16340 | isMemberSpecialization)) |
16341 | Invalid = true; |
16342 | |
16343 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
16344 | if (TemplateParameterLists.size() > 0) { |
16345 | New->setTemplateParameterListsInfo(Context, TemplateParameterLists); |
16346 | } |
16347 | } |
16348 | else |
16349 | Invalid = true; |
16350 | } |
16351 | |
16352 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
16353 | |
16354 | |
16355 | |
16356 | |
16357 | |
16358 | |
16359 | |
16360 | |
16361 | |
16362 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
16363 | AddAlignmentAttributesForRecord(RD); |
16364 | AddMsStructLayoutForRecord(RD); |
16365 | } |
16366 | } |
16367 | |
16368 | if (ModulePrivateLoc.isValid()) { |
16369 | if (isMemberSpecialization) |
16370 | Diag(New->getLocation(), diag::err_module_private_specialization) |
16371 | << 2 |
16372 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
16373 | |
16374 | |
16375 | |
16376 | else if (!SearchDC->isFunctionOrMethod()) |
16377 | New->setModulePrivate(); |
16378 | } |
16379 | |
16380 | |
16381 | |
16382 | if (isMemberSpecialization && CheckMemberSpecialization(New, Previous)) |
16383 | Invalid = true; |
16384 | |
16385 | |
16386 | |
16387 | |
16388 | if ((Name || Kind == TTK_Enum) && |
16389 | getNonFieldDeclScope(S)->isFunctionPrototypeScope()) { |
16390 | if (getLangOpts().CPlusPlus) { |
16391 | |
16392 | |
16393 | if (TUK == TUK_Definition && !IsTypeSpecifier) { |
16394 | Diag(Loc, diag::err_type_defined_in_param_type) |
16395 | << Name; |
16396 | Invalid = true; |
16397 | } |
16398 | } else if (!PrevDecl) { |
16399 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
16400 | } |
16401 | } |
16402 | |
16403 | if (Invalid) |
16404 | New->setInvalidDecl(); |
16405 | |
16406 | |
16407 | |
16408 | New->setLexicalDeclContext(CurContext); |
16409 | |
16410 | |
16411 | |
16412 | |
16413 | |
16414 | if (TUK == TUK_Friend) |
16415 | New->setObjectOfFriendDecl(getLangOpts().MSVCCompat); |
16416 | |
16417 | |
16418 | if (!Invalid && SearchDC->isRecord()) |
16419 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
16420 | |
16421 | if (PrevDecl) |
16422 | CheckRedeclarationModuleOwnership(New, PrevDecl); |
16423 | |
16424 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
16425 | New->startDefinition(); |
16426 | |
16427 | ProcessDeclAttributeList(S, New, Attrs); |
16428 | AddPragmaAttributes(S, New); |
16429 | |
16430 | |
16431 | if (TUK == TUK_Friend) { |
16432 | |
16433 | |
16434 | if (PrevDecl) |
16435 | New->setAccess(PrevDecl->getAccess()); |
16436 | |
16437 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
16438 | DC->makeDeclVisibleInContext(New); |
16439 | if (Name) |
16440 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
16441 | PushOnScopeChains(New, EnclosingScope, false); |
16442 | } else if (Name) { |
16443 | S = getNonFieldDeclScope(S); |
16444 | PushOnScopeChains(New, S, true); |
16445 | } else { |
16446 | CurContext->addDecl(New); |
16447 | } |
16448 | |
16449 | |
16450 | if (IdentifierInfo *II = New->getIdentifier()) |
16451 | if (!New->isInvalidDecl() && |
16452 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
16453 | II->isStr("FILE")) |
16454 | Context.setFILEDecl(New); |
16455 | |
16456 | if (PrevDecl) |
16457 | mergeDeclAttributes(New, PrevDecl); |
16458 | |
16459 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(New)) |
16460 | inferGslOwnerPointerAttribute(CXXRD); |
16461 | |
16462 | |
16463 | |
16464 | AddPushedVisibilityAttribute(New); |
16465 | |
16466 | if (isMemberSpecialization && !New->isInvalidDecl()) |
16467 | CompleteMemberSpecialization(New, Previous); |
16468 | |
16469 | OwnedDecl = true; |
16470 | |
16471 | |
16472 | if (Invalid && getLangOpts().CPlusPlus) { |
16473 | if (New->isBeingDefined()) |
16474 | if (auto RD = dyn_cast<RecordDecl>(New)) |
16475 | RD->completeDefinition(); |
16476 | return nullptr; |
16477 | } else if (SkipBody && SkipBody->ShouldSkip) { |
16478 | return SkipBody->Previous; |
16479 | } else { |
16480 | return New; |
16481 | } |
16482 | } |
16483 | |
16484 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
16485 | AdjustDeclIfTemplate(TagD); |
16486 | TagDecl *Tag = cast<TagDecl>(TagD); |
16487 | |
16488 | |
16489 | PushDeclContext(S, Tag); |
16490 | |
16491 | ActOnDocumentableDecl(TagD); |
16492 | |
16493 | |
16494 | |
16495 | AddPushedVisibilityAttribute(Tag); |
16496 | } |
16497 | |
16498 | bool Sema::ActOnDuplicateDefinition(DeclSpec &DS, Decl *Prev, |
16499 | SkipBodyInfo &SkipBody) { |
16500 | if (!hasStructuralCompatLayout(Prev, SkipBody.New)) |
16501 | return false; |
16502 | |
16503 | |
16504 | makeMergedDefinitionVisible(SkipBody.Previous); |
16505 | return true; |
16506 | } |
16507 | |
16508 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
16509 | assert(isa<ObjCContainerDecl>(IDecl) && |
16510 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); |
16511 | DeclContext *OCD = cast<DeclContext>(IDecl); |
16512 | assert(OCD->getLexicalParent() == CurContext && |
16513 | "The next DeclContext should be lexically contained in the current one."); |
16514 | CurContext = OCD; |
16515 | return IDecl; |
16516 | } |
16517 | |
16518 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
16519 | SourceLocation FinalLoc, |
16520 | bool IsFinalSpelledSealed, |
16521 | bool IsAbstract, |
16522 | SourceLocation LBraceLoc) { |
16523 | AdjustDeclIfTemplate(TagD); |
16524 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
16525 | |
16526 | FieldCollector->StartClass(); |
16527 | |
16528 | if (!Record->getIdentifier()) |
16529 | return; |
16530 | |
16531 | if (IsAbstract) |
16532 | Record->markAbstract(); |
16533 | |
16534 | if (FinalLoc.isValid()) { |
16535 | Record->addAttr(FinalAttr::Create( |
16536 | Context, FinalLoc, AttributeCommonInfo::AS_Keyword, |
16537 | static_cast<FinalAttr::Spelling>(IsFinalSpelledSealed))); |
16538 | } |
16539 | |
16540 | |
16541 | |
16542 | |
16543 | |
16544 | CXXRecordDecl *InjectedClassName = CXXRecordDecl::Create( |
16545 | Context, Record->getTagKind(), CurContext, Record->getBeginLoc(), |
16546 | Record->getLocation(), Record->getIdentifier(), |
16547 | nullptr, |
16548 | true); |
16549 | Context.getTypeDeclType(InjectedClassName, Record); |
16550 | InjectedClassName->setImplicit(); |
16551 | InjectedClassName->setAccess(AS_public); |
16552 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) |
16553 | InjectedClassName->setDescribedClassTemplate(Template); |
16554 | PushOnScopeChains(InjectedClassName, S); |
16555 | assert(InjectedClassName->isInjectedClassName() && |
16556 | "Broken injected-class-name"); |
16557 | } |
16558 | |
16559 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
16560 | SourceRange BraceRange) { |
16561 | AdjustDeclIfTemplate(TagD); |
16562 | TagDecl *Tag = cast<TagDecl>(TagD); |
16563 | Tag->setBraceRange(BraceRange); |
16564 | |
16565 | |
16566 | if (Tag->isBeingDefined()) { |
16567 | assert(Tag->isInvalidDecl() && "We should already have completed it"); |
16568 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
16569 | RD->completeDefinition(); |
16570 | } |
16571 | |
16572 | if (isa<CXXRecordDecl>(Tag)) { |
16573 | FieldCollector->FinishClass(); |
16574 | } |
16575 | |
16576 | |
16577 | PopDeclContext(); |
16578 | |
16579 | if (getCurLexicalContext()->isObjCContainer() && |
16580 | Tag->getDeclContext()->isFileContext()) |
16581 | Tag->setTopLevelDeclInObjCContainer(); |
16582 | |
16583 | |
16584 | if (!Tag->isInvalidDecl()) |
16585 | Consumer.HandleTagDeclDefinition(Tag); |
16586 | } |
16587 | |
16588 | void Sema::ActOnObjCContainerFinishDefinition() { |
16589 | |
16590 | PopDeclContext(); |
16591 | } |
16592 | |
16593 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
16594 | assert(DC == CurContext && "Mismatch of container contexts"); |
16595 | OriginalLexicalContext = DC; |
16596 | ActOnObjCContainerFinishDefinition(); |
16597 | } |
16598 | |
16599 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
16600 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); |
16601 | OriginalLexicalContext = nullptr; |
16602 | } |
16603 | |
16604 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
16605 | AdjustDeclIfTemplate(TagD); |
16606 | TagDecl *Tag = cast<TagDecl>(TagD); |
16607 | Tag->setInvalidDecl(); |
16608 | |
16609 | |
16610 | if (Tag->isBeingDefined()) { |
16611 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
16612 | RD->completeDefinition(); |
16613 | } |
16614 | |
16615 | |
16616 | |
16617 | |
16618 | |
16619 | PopDeclContext(); |
16620 | } |
16621 | |
16622 | |
16623 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
16624 | IdentifierInfo *FieldName, |
16625 | QualType FieldTy, bool IsMsStruct, |
16626 | Expr *BitWidth, bool *ZeroWidth) { |
16627 | assert(BitWidth); |
16628 | if (BitWidth->containsErrors()) |
16629 | return ExprError(); |
16630 | |
16631 | |
16632 | if (ZeroWidth) |
16633 | *ZeroWidth = true; |
16634 | |
16635 | |
16636 | |
16637 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
16638 | |
16639 | if (RequireCompleteSizedType(FieldLoc, FieldTy, |
16640 | diag::err_field_incomplete_or_sizeless)) |
16641 | return ExprError(); |
16642 | if (FieldName) |
16643 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) |
16644 | << FieldName << FieldTy << BitWidth->getSourceRange(); |
16645 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) |
16646 | << FieldTy << BitWidth->getSourceRange(); |
16647 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
16648 | UPPC_BitFieldWidth)) |
16649 | return ExprError(); |
16650 | |
16651 | |
16652 | |
16653 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) |
16654 | return BitWidth; |
16655 | |
16656 | llvm::APSInt Value; |
16657 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value, AllowFold); |
16658 | if (ICE.isInvalid()) |
16659 | return ICE; |
16660 | BitWidth = ICE.get(); |
16661 | |
16662 | if (Value != 0 && ZeroWidth) |
16663 | *ZeroWidth = false; |
16664 | |
16665 | |
16666 | if (Value == 0 && FieldName) |
16667 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
16668 | |
16669 | if (Value.isSigned() && Value.isNegative()) { |
16670 | if (FieldName) |
16671 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
16672 | << FieldName << toString(Value, 10); |
16673 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) |
16674 | << toString(Value, 10); |
16675 | } |
16676 | |
16677 | |
16678 | |
16679 | if (Value.getActiveBits() > ConstantArrayType::getMaxSizeBits(Context)) { |
16680 | return Diag(FieldLoc, diag::err_bitfield_too_wide) |
16681 | << !FieldName << FieldName << toString(Value, 10); |
16682 | } |
16683 | |
16684 | if (!FieldTy->isDependentType()) { |
16685 | uint64_t TypeStorageSize = Context.getTypeSize(FieldTy); |
16686 | uint64_t TypeWidth = Context.getIntWidth(FieldTy); |
16687 | bool BitfieldIsOverwide = Value.ugt(TypeWidth); |
16688 | |
16689 | |
16690 | |
16691 | bool CStdConstraintViolation = |
16692 | BitfieldIsOverwide && !getLangOpts().CPlusPlus; |
16693 | bool MSBitfieldViolation = |
16694 | Value.ugt(TypeStorageSize) && |
16695 | (IsMsStruct || Context.getTargetInfo().getCXXABI().isMicrosoft()); |
16696 | if (CStdConstraintViolation || MSBitfieldViolation) { |
16697 | unsigned DiagWidth = |
16698 | CStdConstraintViolation ? TypeWidth : TypeStorageSize; |
16699 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_width) |
16700 | << (bool)FieldName << FieldName << toString(Value, 10) |
16701 | << !CStdConstraintViolation << DiagWidth; |
16702 | } |
16703 | |
16704 | |
16705 | |
16706 | |
16707 | if (BitfieldIsOverwide && !FieldTy->isBooleanType() && FieldName) { |
16708 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_width) |
16709 | << FieldName << toString(Value, 10) |
16710 | << (unsigned)TypeWidth; |
16711 | } |
16712 | } |
16713 | |
16714 | return BitWidth; |
16715 | } |
16716 | |
16717 | |
16718 | |
16719 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
16720 | Declarator &D, Expr *BitfieldWidth) { |
16721 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
16722 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
16723 | ICIS_NoInit, AS_public); |
16724 | return Res; |
16725 | } |
16726 | |
16727 | |
16728 | |
16729 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, |
16730 | SourceLocation DeclStart, |
16731 | Declarator &D, Expr *BitWidth, |
16732 | InClassInitStyle InitStyle, |
16733 | AccessSpecifier AS) { |
16734 | if (D.isDecompositionDeclarator()) { |
16735 | const DecompositionDeclarator &Decomp = D.getDecompositionDeclarator(); |
16736 | Diag(Decomp.getLSquareLoc(), diag::err_decomp_decl_context) |
16737 | << Decomp.getSourceRange(); |
16738 | return nullptr; |
16739 | } |
16740 | |
16741 | IdentifierInfo *II = D.getIdentifier(); |
16742 | SourceLocation Loc = DeclStart; |
16743 | if (II) Loc = D.getIdentifierLoc(); |
16744 | |
16745 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
16746 | QualType T = TInfo->getType(); |
16747 | if (getLangOpts().CPlusPlus) { |
16748 | CheckExtraCXXDefaultArguments(D); |
16749 | |
16750 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
16751 | UPPC_DataMemberType)) { |
16752 | D.setInvalidType(); |
16753 | T = Context.IntTy; |
16754 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
16755 | } |
16756 | } |
16757 | |
16758 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
16759 | |
16760 | if (D.getDeclSpec().isInlineSpecified()) |
16761 | Diag(D.getDeclSpec().getInlineSpecLoc(), diag::err_inline_non_function) |
16762 | << getLangOpts().CPlusPlus17; |
16763 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
16764 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
16765 | diag::err_invalid_thread) |
16766 | << DeclSpec::getSpecifierName(TSCS); |
16767 | |
16768 | |
16769 | NamedDecl *PrevDecl = nullptr; |
16770 | LookupResult Previous(*this, II, Loc, LookupMemberName, |
16771 | ForVisibleRedeclaration); |
16772 | LookupName(Previous, S); |
16773 | switch (Previous.getResultKind()) { |
16774 | case LookupResult::Found: |
16775 | case LookupResult::FoundUnresolvedValue: |
16776 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
16777 | break; |
16778 | |
16779 | case LookupResult::FoundOverloaded: |
16780 | PrevDecl = Previous.getRepresentativeDecl(); |
16781 | break; |
16782 | |
16783 | case LookupResult::NotFound: |
16784 | case LookupResult::NotFoundInCurrentInstantiation: |
16785 | case LookupResult::Ambiguous: |
16786 | break; |
16787 | } |
16788 | Previous.suppressDiagnostics(); |
16789 | |
16790 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
16791 | |
16792 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
16793 | |
16794 | PrevDecl = nullptr; |
16795 | } |
16796 | |
16797 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
16798 | PrevDecl = nullptr; |
16799 | |
16800 | bool Mutable |
16801 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); |
16802 | SourceLocation TSSL = D.getBeginLoc(); |
16803 | FieldDecl *NewFD |
16804 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
16805 | TSSL, AS, PrevDecl, &D); |
16806 | |
16807 | if (NewFD->isInvalidDecl()) |
16808 | Record->setInvalidDecl(); |
16809 | |
16810 | if (D.getDeclSpec().isModulePrivateSpecified()) |
16811 | NewFD->setModulePrivate(); |
16812 | |
16813 | if (NewFD->isInvalidDecl() && PrevDecl) { |
16814 | |
16815 | |
16816 | } else if (II) { |
16817 | PushOnScopeChains(NewFD, S); |
16818 | } else |
16819 | Record->addDecl(NewFD); |
16820 | |
16821 | return NewFD; |
16822 | } |
16823 | |
16824 | |
16825 | |
16826 | |
16827 | |
16828 | |
16829 | |
16830 | |
16831 | |
16832 | |
16833 | |
16834 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
16835 | TypeSourceInfo *TInfo, |
16836 | RecordDecl *Record, SourceLocation Loc, |
16837 | bool Mutable, Expr *BitWidth, |
16838 | InClassInitStyle InitStyle, |
16839 | SourceLocation TSSL, |
16840 | AccessSpecifier AS, NamedDecl *PrevDecl, |
16841 | Declarator *D) { |
16842 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
16843 | bool InvalidDecl = false; |
16844 | if (D) InvalidDecl = D->isInvalidType(); |
16845 | |
16846 | |
16847 | |
16848 | if (T.isNull() || T->containsErrors()) { |
16849 | InvalidDecl = true; |
16850 | T = Context.IntTy; |
16851 | } |
16852 | |
16853 | QualType EltTy = Context.getBaseElementType(T); |
16854 | if (!EltTy->isDependentType() && !EltTy->containsErrors()) { |
16855 | if (RequireCompleteSizedType(Loc, EltTy, |
16856 | diag::err_field_incomplete_or_sizeless)) { |
16857 | |
16858 | Record->setInvalidDecl(); |
16859 | InvalidDecl = true; |
16860 | } else { |
16861 | NamedDecl *Def; |
16862 | EltTy->isIncompleteType(&Def); |
16863 | if (Def && Def->isInvalidDecl()) { |
16864 | Record->setInvalidDecl(); |
16865 | InvalidDecl = true; |
16866 | } |
16867 | } |
16868 | } |
16869 | |
16870 | |
16871 | if (T.hasAddressSpace() || T->isDependentAddressSpaceType() || |
16872 | T->getBaseElementTypeUnsafe()->isDependentAddressSpaceType()) { |
16873 | Diag(Loc, diag::err_field_with_address_space); |
16874 | Record->setInvalidDecl(); |
16875 | InvalidDecl = true; |
16876 | } |
16877 | |
16878 | if (LangOpts.OpenCL) { |
16879 | |
16880 | |
16881 | if (T->isEventT() || T->isImageType() || T->isSamplerT() || |
16882 | T->isBlockPointerType()) { |
16883 | Diag(Loc, diag::err_opencl_type_struct_or_union_field) << T; |
16884 | Record->setInvalidDecl(); |
16885 | InvalidDecl = true; |
16886 | } |
16887 | |
16888 | |
16889 | if (BitWidth && !getOpenCLOptions().isAvailableOption( |
16890 | "__cl_clang_bitfields", LangOpts)) { |
16891 | Diag(Loc, diag::err_opencl_bitfields); |
16892 | InvalidDecl = true; |
16893 | } |
16894 | } |
16895 | |
16896 | |
16897 | if (!InvalidDecl && getLangOpts().CPlusPlus && !II && BitWidth && |
16898 | T.hasQualifiers()) { |
16899 | InvalidDecl = true; |
16900 | Diag(Loc, diag::err_anon_bitfield_qualifiers); |
16901 | } |
16902 | |
16903 | |
16904 | |
16905 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
16906 | if (!tryToFixVariablyModifiedVarType( |
16907 | TInfo, T, Loc, diag::err_typecheck_field_variable_size)) |
16908 | InvalidDecl = true; |
16909 | } |
16910 | |
16911 | |
16912 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
16913 | diag::err_abstract_type_in_decl, |
16914 | AbstractFieldType)) |
16915 | InvalidDecl = true; |
16916 | |
16917 | bool ZeroWidth = false; |
16918 | if (InvalidDecl) |
16919 | BitWidth = nullptr; |
16920 | |
16921 | if (BitWidth) { |
16922 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
16923 | &ZeroWidth).get(); |
16924 | if (!BitWidth) { |
16925 | InvalidDecl = true; |
16926 | BitWidth = nullptr; |
16927 | ZeroWidth = false; |
16928 | } |
16929 | } |
16930 | |
16931 | |
16932 | if (!InvalidDecl && Mutable) { |
16933 | unsigned DiagID = 0; |
16934 | if (T->isReferenceType()) |
16935 | DiagID = getLangOpts().MSVCCompat ? diag::ext_mutable_reference |
16936 | : diag::err_mutable_reference; |
16937 | else if (T.isConstQualified()) |
16938 | DiagID = diag::err_mutable_const; |
16939 | |
16940 | if (DiagID) { |
16941 | SourceLocation ErrLoc = Loc; |
16942 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) |
16943 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); |
16944 | Diag(ErrLoc, DiagID); |
16945 | if (DiagID != diag::ext_mutable_reference) { |
16946 | Mutable = false; |
16947 | InvalidDecl = true; |
16948 | } |
16949 | } |
16950 | } |
16951 | |
16952 | |
16953 | |
16954 | |
16955 | if (InitStyle != ICIS_NoInit) |
16956 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Record), Loc); |
16957 | |
16958 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
16959 | BitWidth, Mutable, InitStyle); |
16960 | if (InvalidDecl) |
16961 | NewFD->setInvalidDecl(); |
16962 | |
16963 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
16964 | Diag(Loc, diag::err_duplicate_member) << II; |
16965 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
16966 | NewFD->setInvalidDecl(); |
16967 | } |
16968 | |
16969 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
16970 | if (Record->isUnion()) { |
16971 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
16972 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
16973 | if (RDecl->getDefinition()) { |
16974 | |
16975 | |
16976 | |
16977 | |
16978 | |
16979 | if (CheckNontrivialField(NewFD)) |
16980 | NewFD->setInvalidDecl(); |
16981 | } |
16982 | } |
16983 | |
16984 | |
16985 | |
16986 | |
16987 | if (EltTy->isReferenceType()) { |
16988 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
16989 | diag::ext_union_member_of_reference_type : |
16990 | diag::err_union_member_of_reference_type) |
16991 | << NewFD->getDeclName() << EltTy; |
16992 | if (!getLangOpts().MicrosoftExt) |
16993 | NewFD->setInvalidDecl(); |
16994 | } |
16995 | } |
16996 | } |
16997 | |
16998 | |
16999 | |
17000 | if (D) { |
17001 | |
17002 | ProcessDeclAttributes(getCurScope(), NewFD, *D); |
17003 | |
17004 | if (NewFD->hasAttrs()) |
17005 | CheckAlignasUnderalignment(NewFD); |
17006 | } |
17007 | |
17008 | |
17009 | |
17010 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
17011 | NewFD->setInvalidDecl(); |
17012 | |
17013 | if (T.isObjCGCWeak()) |
17014 | Diag(Loc, diag::warn_attribute_weak_on_field); |
17015 | |
17016 | |
17017 | if (Context.getTargetInfo().getTriple().isPPC64() && |
17018 | CheckPPCMMAType(T, NewFD->getLocation())) |
17019 | NewFD->setInvalidDecl(); |
17020 | |
17021 | NewFD->setAccess(AS); |
17022 | return NewFD; |
17023 | } |
17024 | |
17025 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
17026 | assert(FD); |
17027 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
17028 | |
17029 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
17030 | return false; |
17031 | |
17032 | QualType EltTy = Context.getBaseElementType(FD->getType()); |
17033 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
17034 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
17035 | if (RDecl->getDefinition()) { |
17036 | |
17037 | |
17038 | |
17039 | |
17040 | CXXSpecialMember member = CXXInvalid; |
17041 | |
17042 | |
17043 | |
17044 | |
17045 | |
17046 | if (RDecl->hasNonTrivialCopyConstructor()) |
17047 | member = CXXCopyConstructor; |
17048 | else if (!RDecl->hasTrivialDefaultConstructor()) |
17049 | member = CXXDefaultConstructor; |
17050 | else if (RDecl->hasNonTrivialCopyAssignment()) |
17051 | member = CXXCopyAssignment; |
17052 | else if (RDecl->hasNonTrivialDestructor()) |
17053 | member = CXXDestructor; |
17054 | |
17055 | if (member != CXXInvalid) { |
17056 | if (!getLangOpts().CPlusPlus11 && |
17057 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
17058 | |
17059 | |
17060 | |
17061 | |
17062 | |
17063 | SourceLocation Loc = FD->getLocation(); |
17064 | if (getSourceManager().isInSystemHeader(Loc)) { |
17065 | if (!FD->hasAttr<UnavailableAttr>()) |
17066 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
17067 | UnavailableAttr::IR_ARCFieldWithOwnership, Loc)); |
17068 | return false; |
17069 | } |
17070 | } |
17071 | |
17072 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
17073 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
17074 | diag::err_illegal_union_or_anon_struct_member) |
17075 | << FD->getParent()->isUnion() << FD->getDeclName() << member; |
17076 | DiagnoseNontrivial(RDecl, member); |
17077 | return !getLangOpts().CPlusPlus11; |
17078 | } |
17079 | } |
17080 | } |
17081 | |
17082 | return false; |
17083 | } |
17084 | |
17085 | |
17086 | |
17087 | static ObjCIvarDecl::AccessControl |
17088 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
17089 | switch (ivarVisibility) { |
17090 | default: llvm_unreachable("Unknown visitibility kind"); |
17091 | case tok::objc_private: return ObjCIvarDecl::Private; |
17092 | case tok::objc_public: return ObjCIvarDecl::Public; |
17093 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
17094 | case tok::objc_package: return ObjCIvarDecl::Package; |
17095 | } |
17096 | } |
17097 | |
17098 | |
17099 | |
17100 | Decl *Sema::ActOnIvar(Scope *S, |
17101 | SourceLocation DeclStart, |
17102 | Declarator &D, Expr *BitfieldWidth, |
17103 | tok::ObjCKeywordKind Visibility) { |
17104 | |
17105 | IdentifierInfo *II = D.getIdentifier(); |
17106 | Expr *BitWidth = (Expr*)BitfieldWidth; |
17107 | SourceLocation Loc = DeclStart; |
17108 | if (II) Loc = D.getIdentifierLoc(); |
17109 | |
17110 | |
17111 | |
17112 | |
17113 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
17114 | QualType T = TInfo->getType(); |
17115 | |
17116 | if (BitWidth) { |
17117 | |
17118 | BitWidth = VerifyBitField(Loc, II, T, false, BitWidth).get(); |
17119 | if (!BitWidth) |
17120 | D.setInvalidType(); |
17121 | } else { |
17122 | |
17123 | |
17124 | |
17125 | |
17126 | } |
17127 | if (T->isReferenceType()) { |
17128 | Diag(Loc, diag::err_ivar_reference_type); |
17129 | D.setInvalidType(); |
17130 | } |
17131 | |
17132 | |
17133 | else if (T->isVariablyModifiedType()) { |
17134 | if (!tryToFixVariablyModifiedVarType( |
17135 | TInfo, T, Loc, diag::err_typecheck_ivar_variable_size)) |
17136 | D.setInvalidType(); |
17137 | } |
17138 | |
17139 | |
17140 | ObjCIvarDecl::AccessControl ac = |
17141 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
17142 | : ObjCIvarDecl::None; |
17143 | |
17144 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
17145 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
17146 | return nullptr; |
17147 | ObjCContainerDecl *EnclosingContext; |
17148 | if (ObjCImplementationDecl *IMPDecl = |
17149 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
17150 | if (LangOpts.ObjCRuntime.isFragile()) { |
17151 | |
17152 | EnclosingContext = IMPDecl->getClassInterface(); |
17153 | assert(EnclosingContext && "Implementation has no class interface!"); |
17154 | } |
17155 | else |
17156 | EnclosingContext = EnclosingDecl; |
17157 | } else { |
17158 | if (ObjCCategoryDecl *CDecl = |
17159 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
17160 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
17161 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
17162 | return nullptr; |
17163 | } |
17164 | } |
17165 | EnclosingContext = EnclosingDecl; |
17166 | } |
17167 | |
17168 | |
17169 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
17170 | DeclStart, Loc, II, T, |
17171 | TInfo, ac, (Expr *)BitfieldWidth); |
17172 | |
17173 | if (II) { |
17174 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
17175 | ForVisibleRedeclaration); |
17176 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
17177 | && !isa<TagDecl>(PrevDecl)) { |
17178 | Diag(Loc, diag::err_duplicate_member) << II; |
17179 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
17180 | NewID->setInvalidDecl(); |
17181 | } |
17182 | } |
17183 | |
17184 | |
17185 | ProcessDeclAttributes(S, NewID, D); |
17186 | |
17187 | if (D.isInvalidType()) |
17188 | NewID->setInvalidDecl(); |
17189 | |
17190 | |
17191 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
17192 | NewID->setInvalidDecl(); |
17193 | |
17194 | if (D.getDeclSpec().isModulePrivateSpecified()) |
17195 | NewID->setModulePrivate(); |
17196 | |
17197 | if (II) { |
17198 | |
17199 | |
17200 | S->AddDecl(NewID); |
17201 | IdResolver.AddDecl(NewID); |
17202 | } |
17203 | |
17204 | if (LangOpts.ObjCRuntime.isNonFragile() && |
17205 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
17206 | Diag(Loc, diag::warn_ivars_in_interface); |
17207 | |
17208 | return NewID; |
17209 | } |
17210 | |
17211 | |
17212 | |
17213 | |
17214 | |
17215 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
17216 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
17217 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
17218 | return; |
17219 | |
17220 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; |
17221 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); |
17222 | |
17223 | if (!Ivar->isBitField() || Ivar->isZeroLengthBitField(Context)) |
17224 | return; |
17225 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
17226 | if (!ID) { |
17227 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
17228 | if (!CD->IsClassExtension()) |
17229 | return; |
17230 | } |
17231 | |
17232 | else |
17233 | return; |
17234 | } |
17235 | |
17236 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
17237 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); |
17238 | |
17239 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
17240 | DeclLoc, DeclLoc, nullptr, |
17241 | Context.CharTy, |
17242 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
17243 | DeclLoc), |
17244 | ObjCIvarDecl::Private, BW, |
17245 | true); |
17246 | AllIvarDecls.push_back(Ivar); |
17247 | } |
17248 | |
17249 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
17250 | ArrayRef<Decl *> Fields, SourceLocation LBrac, |
17251 | SourceLocation RBrac, |
17252 | const ParsedAttributesView &Attrs) { |
17253 | assert(EnclosingDecl && "missing record or interface decl"); |
17254 | |
17255 | |
17256 | |
17257 | |
17258 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { |
17259 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); |
17260 | switch (DC->getKind()) { |
17261 | default: break; |
17262 | case Decl::ObjCCategory: |
17263 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); |
17264 | break; |
17265 | case Decl::ObjCImplementation: |
17266 | Context. |
17267 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); |
17268 | break; |
17269 | } |
17270 | } |
17271 | |
17272 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
17273 | CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(EnclosingDecl); |
17274 | |
17275 | |
17276 | |
17277 | unsigned NumNamedMembers = 0; |
17278 | if (Record) { |
17279 | for (const auto *I : Record->decls()) { |
17280 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
17281 | if (IFD->getDeclName()) |
17282 | ++NumNamedMembers; |
17283 | } |
17284 | } |
17285 | |
17286 | |
17287 | SmallVector<FieldDecl*, 32> RecFields; |
17288 | |
17289 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
17290 | i != end; ++i) { |
17291 | FieldDecl *FD = cast<FieldDecl>(*i); |
17292 | |
17293 | |
17294 | const Type *FDTy = FD->getType().getTypePtr(); |
17295 | |
17296 | if (!FD->isAnonymousStructOrUnion()) { |
17297 | |
17298 | RecFields.push_back(FD); |
17299 | } |
17300 | |
17301 | |
17302 | |
17303 | if (FD->isInvalidDecl()) { |
17304 | EnclosingDecl->setInvalidDecl(); |
17305 | continue; |
17306 | } |
17307 | |
17308 | |
17309 | |
17310 | |
17311 | |
17312 | |
17313 | |
17314 | |
17315 | |
17316 | |
17317 | |
17318 | bool IsLastField = (i + 1 == Fields.end()); |
17319 | if (FDTy->isFunctionType()) { |
17320 | |
17321 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
17322 | << FD->getDeclName(); |
17323 | FD->setInvalidDecl(); |
17324 | EnclosingDecl->setInvalidDecl(); |
17325 | continue; |
17326 | } else if (FDTy->isIncompleteArrayType() && |
17327 | (Record || isa<ObjCContainerDecl>(EnclosingDecl))) { |
17328 | if (Record) { |
17329 | |
17330 | |
17331 | |
17332 | |
17333 | unsigned DiagID = 0; |
17334 | if (!Record->isUnion() && !IsLastField) { |
17335 | Diag(FD->getLocation(), diag::err_flexible_array_not_at_end) |
17336 | << FD->getDeclName() << FD->getType() << Record->getTagKind(); |
17337 | Diag((*(i + 1))->getLocation(), diag::note_next_field_declaration); |
17338 | FD->setInvalidDecl(); |
17339 | EnclosingDecl->setInvalidDecl(); |
17340 | continue; |
17341 | } else if (Record->isUnion()) |
17342 | DiagID = getLangOpts().MicrosoftExt |
17343 | ? diag::ext_flexible_array_union_ms |
17344 | : getLangOpts().CPlusPlus |
17345 | ? diag::ext_flexible_array_union_gnu |
17346 | : diag::err_flexible_array_union; |
17347 | else if (NumNamedMembers < 1) |
17348 | DiagID = getLangOpts().MicrosoftExt |
17349 | ? diag::ext_flexible_array_empty_aggregate_ms |
17350 | : getLangOpts().CPlusPlus |
17351 | ? diag::ext_flexible_array_empty_aggregate_gnu |
17352 | : diag::err_flexible_array_empty_aggregate; |
17353 | |
17354 | if (DiagID) |
17355 | Diag(FD->getLocation(), DiagID) << FD->getDeclName() |
17356 | << Record->getTagKind(); |
17357 | |
17358 | |
17359 | |
17360 | |
17361 | |
17362 | if (CXXRecord && CXXRecord->getNumVBases() != 0) |
17363 | Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) |
17364 | << FD->getDeclName() << Record->getTagKind(); |
17365 | if (!getLangOpts().C99) |
17366 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) |
17367 | << FD->getDeclName() << Record->getTagKind(); |
17368 | |
17369 | |
17370 | |
17371 | |
17372 | |
17373 | |
17374 | QualType BaseElem = Context.getBaseElementType(FD->getType()); |
17375 | if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { |
17376 | Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) |
17377 | << FD->getDeclName() << FD->getType(); |
17378 | FD->setInvalidDecl(); |
17379 | EnclosingDecl->setInvalidDecl(); |
17380 | continue; |
17381 | } |
17382 | |
17383 | Record->setHasFlexibleArrayMember(true); |
17384 | } else { |
17385 | |
17386 | |
17387 | |
17388 | } |
17389 | } else if (!FDTy->isDependentType() && |
17390 | RequireCompleteSizedType( |
17391 | FD->getLocation(), FD->getType(), |
17392 | diag::err_field_incomplete_or_sizeless)) { |
17393 | |
17394 | FD->setInvalidDecl(); |
17395 | EnclosingDecl->setInvalidDecl(); |
17396 | continue; |
17397 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
17398 | if (Record && FDTTy->getDecl()->hasFlexibleArrayMember()) { |
17399 | |
17400 | |
17401 | Record->setHasFlexibleArrayMember(true); |
17402 | if (!Record->isUnion()) { |
17403 | |
17404 | |
17405 | |
17406 | if (!IsLastField) |
17407 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
17408 | << FD->getDeclName() << FD->getType(); |
17409 | else { |
17410 | |
17411 | |
17412 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
17413 | << FD->getDeclName(); |
17414 | } |
17415 | } |
17416 | } |
17417 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
17418 | RequireNonAbstractType(FD->getLocation(), FD->getType(), |
17419 | diag::err_abstract_type_in_decl, |
17420 | AbstractIvarType)) { |
17421 | |
17422 | FD->setInvalidDecl(); |
17423 | } |
17424 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
17425 | Record->setHasObjectMember(true); |
17426 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
17427 | Record->setHasVolatileMember(true); |
17428 | } else if (FDTy->isObjCObjectType()) { |
17429 | |
17430 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
17431 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); |
17432 | QualType T = Context.getObjCObjectPointerType(FD->getType()); |
17433 | FD->setType(T); |
17434 | } else if (Record && Record->isUnion() && |
17435 | FD->getType().hasNonTrivialObjCLifetime() && |
17436 | getSourceManager().isInSystemHeader(FD->getLocation()) && |
17437 | !getLangOpts().CPlusPlus && !FD->hasAttr<UnavailableAttr>() && |
17438 | (FD->getType().getObjCLifetime() != Qualifiers::OCL_Strong || |
17439 | !Context.hasDirectOwnershipQualifier(FD->getType()))) { |
17440 | |
17441 | |
17442 | |
17443 | |
17444 | |
17445 | |
17446 | FD->addAttr(UnavailableAttr::CreateImplicit( |
17447 | Context, "", UnavailableAttr::IR_ARCFieldWithOwnership, |
17448 | FD->getLocation())); |
17449 | } else if (getLangOpts().ObjC && |
17450 | getLangOpts().getGC() != LangOptions::NonGC && Record && |
17451 | !Record->hasObjectMember()) { |
17452 | if (FD->getType()->isObjCObjectPointerType() || |
17453 | FD->getType().isObjCGCStrong()) |
17454 | Record->setHasObjectMember(true); |
17455 | else if (Context.getAsArrayType(FD->getType())) { |
17456 | QualType BaseType = Context.getBaseElementType(FD->getType()); |
17457 | if (BaseType->isRecordType() && |
17458 | BaseType->castAs<RecordType>()->getDecl()->hasObjectMember()) |
17459 | Record->setHasObjectMember(true); |
17460 | else if (BaseType->isObjCObjectPointerType() || |
17461 | BaseType.isObjCGCStrong()) |
17462 | Record->setHasObjectMember(true); |
17463 | } |
17464 | } |
17465 | |
17466 | if (Record && !getLangOpts().CPlusPlus && |
17467 | !shouldIgnoreForRecordTriviality(FD)) { |
17468 | QualType FT = FD->getType(); |
17469 | if (FT.isNonTrivialToPrimitiveDefaultInitialize()) { |
17470 | Record->setNonTrivialToPrimitiveDefaultInitialize(true); |
17471 | if (FT.hasNonTrivialToPrimitiveDefaultInitializeCUnion() || |
17472 | Record->isUnion()) |
17473 | Record->setHasNonTrivialToPrimitiveDefaultInitializeCUnion(true); |
17474 | } |
17475 | QualType::PrimitiveCopyKind PCK = FT.isNonTrivialToPrimitiveCopy(); |
17476 | if (PCK != QualType::PCK_Trivial && PCK != QualType::PCK_VolatileTrivial) { |
17477 | Record->setNonTrivialToPrimitiveCopy(true); |
17478 | if (FT.hasNonTrivialToPrimitiveCopyCUnion() || Record->isUnion()) |
17479 | Record->setHasNonTrivialToPrimitiveCopyCUnion(true); |
17480 | } |
17481 | if (FT.isDestructedType()) { |
17482 | Record->setNonTrivialToPrimitiveDestroy(true); |
17483 | Record->setParamDestroyedInCallee(true); |
17484 | if (FT.hasNonTrivialToPrimitiveDestructCUnion() || Record->isUnion()) |
17485 | Record->setHasNonTrivialToPrimitiveDestructCUnion(true); |
17486 | } |
17487 | |
17488 | if (const auto *RT = FT->getAs<RecordType>()) { |
17489 | if (RT->getDecl()->getArgPassingRestrictions() == |
17490 | RecordDecl::APK_CanNeverPassInRegs) |
17491 | Record->setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs); |
17492 | } else if (FT.getQualifiers().getObjCLifetime() == Qualifiers::OCL_Weak) |
17493 | Record->setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs); |
17494 | } |
17495 | |
17496 | if (Record && FD->getType().isVolatileQualified()) |
17497 | Record->setHasVolatileMember(true); |
17498 | |
17499 | if (FD->getIdentifier()) |
17500 | ++NumNamedMembers; |
17501 | } |
17502 | |
17503 | |
17504 | if (Record) { |
17505 | bool Completed = false; |
17506 | if (CXXRecord) { |
17507 | if (!CXXRecord->isInvalidDecl()) { |
17508 | |
17509 | for (CXXRecordDecl::conversion_iterator |
17510 | I = CXXRecord->conversion_begin(), |
17511 | E = CXXRecord->conversion_end(); I != E; ++I) |
17512 | I.setAccess((*I)->getAccess()); |
17513 | } |
17514 | |
17515 | |
17516 | AddImplicitlyDeclaredMembersToClass(CXXRecord); |
17517 | |
17518 | if (!CXXRecord->isDependentType()) { |
17519 | if (!CXXRecord->isInvalidDecl()) { |
17520 | |
17521 | |
17522 | |
17523 | if (CXXRecord->getNumVBases()) { |
17524 | CXXFinalOverriderMap FinalOverriders; |
17525 | CXXRecord->getFinalOverriders(FinalOverriders); |
17526 | |
17527 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
17528 | MEnd = FinalOverriders.end(); |
17529 | M != MEnd; ++M) { |
17530 | for (OverridingMethods::iterator SO = M->second.begin(), |
17531 | SOEnd = M->second.end(); |
17532 | SO != SOEnd; ++SO) { |
17533 | assert(SO->second.size() > 0 && |
17534 | "Virtual function without overriding functions?"); |
17535 | if (SO->second.size() == 1) |
17536 | continue; |
17537 | |
17538 | |
17539 | |
17540 | |
17541 | |
17542 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
17543 | << (const NamedDecl *)M->first << Record; |
17544 | Diag(M->first->getLocation(), |
17545 | diag::note_overridden_virtual_function); |
17546 | for (OverridingMethods::overriding_iterator |
17547 | OM = SO->second.begin(), |
17548 | OMEnd = SO->second.end(); |
17549 | OM != OMEnd; ++OM) |
17550 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
17551 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
17552 | |
17553 | Record->setInvalidDecl(); |
17554 | } |
17555 | } |
17556 | CXXRecord->completeDefinition(&FinalOverriders); |
17557 | Completed = true; |
17558 | } |
17559 | } |
17560 | } |
17561 | } |
17562 | |
17563 | if (!Completed) |
17564 | Record->completeDefinition(); |
17565 | |
17566 | |
17567 | ProcessDeclAttributeList(S, Record, Attrs); |
17568 | |
17569 | |
17570 | if (CXXRecord) { |
17571 | auto *Dtor = CXXRecord->getDestructor(); |
17572 | if (Dtor && Dtor->isImplicit() && |
17573 | ShouldDeleteSpecialMember(Dtor, CXXDestructor)) { |
17574 | CXXRecord->setImplicitDestructorIsDeleted(); |
17575 | SetDeclDeleted(Dtor, CXXRecord->getLocation()); |
17576 | } |
17577 | } |
17578 | |
17579 | if (Record->hasAttrs()) { |
17580 | CheckAlignasUnderalignment(Record); |
17581 | |
17582 | if (const MSInheritanceAttr *IA = Record->getAttr<MSInheritanceAttr>()) |
17583 | checkMSInheritanceAttrOnDefinition(cast<CXXRecordDecl>(Record), |
17584 | IA->getRange(), IA->getBestCase(), |
17585 | IA->getInheritanceModel()); |
17586 | } |
17587 | |
17588 | |
17589 | |
17590 | |
17591 | bool CheckForZeroSize; |
17592 | if (!getLangOpts().CPlusPlus) { |
17593 | CheckForZeroSize = true; |
17594 | } else { |
17595 | |
17596 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record); |
17597 | CheckForZeroSize = |
17598 | CXXRecord->getLexicalDeclContext()->isExternCContext() && |
17599 | !CXXRecord->isDependentType() && !inTemplateInstantiation() && |
17600 | CXXRecord->isCLike(); |
17601 | } |
17602 | if (CheckForZeroSize) { |
17603 | bool ZeroSize = true; |
17604 | bool IsEmpty = true; |
17605 | unsigned NonBitFields = 0; |
17606 | for (RecordDecl::field_iterator I = Record->field_begin(), |
17607 | E = Record->field_end(); |
17608 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { |
17609 | IsEmpty = false; |
17610 | if (I->isUnnamedBitfield()) { |
17611 | if (!I->isZeroLengthBitField(Context)) |
17612 | ZeroSize = false; |
17613 | } else { |
17614 | ++NonBitFields; |
17615 | QualType FieldType = I->getType(); |
17616 | if (FieldType->isIncompleteType() || |
17617 | !Context.getTypeSizeInChars(FieldType).isZero()) |
17618 | ZeroSize = false; |
17619 | } |
17620 | } |
17621 | |
17622 | |
17623 | |
17624 | |
17625 | if (ZeroSize) { |
17626 | Diag(RecLoc, getLangOpts().CPlusPlus ? |
17627 | diag::warn_zero_size_struct_union_in_extern_c : |
17628 | diag::warn_zero_size_struct_union_compat) |
17629 | << IsEmpty << Record->isUnion() << (NonBitFields > 1); |
17630 | } |
17631 | |
17632 | |
17633 | |
17634 | if (NonBitFields == 0 && !getLangOpts().CPlusPlus) { |
17635 | Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union : |
17636 | diag::ext_no_named_members_in_struct_union) |
17637 | << Record->isUnion(); |
17638 | } |
17639 | } |
17640 | } else { |
17641 | ObjCIvarDecl **ClsFields = |
17642 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); |
17643 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
17644 | ID->setEndOfDefinitionLoc(RBrac); |
17645 | |
17646 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
17647 | ClsFields[i]->setLexicalDeclContext(ID); |
17648 | ID->addDecl(ClsFields[i]); |
17649 | } |
17650 | |
17651 | |
17652 | if (ID->getSuperClass()) |
17653 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); |
17654 | } else if (ObjCImplementationDecl *IMPDecl = |
17655 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
17656 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
17657 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
17658 | |
17659 | |
17660 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
17661 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
17662 | IMPDecl->setIvarLBraceLoc(LBrac); |
17663 | IMPDecl->setIvarRBraceLoc(RBrac); |
17664 | } else if (ObjCCategoryDecl *CDecl = |
17665 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
17666 | |
17667 | |
17668 | |
17669 | |
17670 | |
17671 | |
17672 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); |
17673 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
17674 | if (IDecl) { |
17675 | if (const ObjCIvarDecl *ClsIvar = |
17676 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { |
17677 | Diag(ClsFields[i]->getLocation(), |
17678 | diag::err_duplicate_ivar_declaration); |
17679 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
17680 | continue; |
17681 | } |
17682 | for (const auto *Ext : IDecl->known_extensions()) { |
17683 | if (const ObjCIvarDecl *ClsExtIvar |
17684 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { |
17685 | Diag(ClsFields[i]->getLocation(), |
17686 | diag::err_duplicate_ivar_declaration); |
17687 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
17688 | continue; |
17689 | } |
17690 | } |
17691 | } |
17692 | ClsFields[i]->setLexicalDeclContext(CDecl); |
17693 | CDecl->addDecl(ClsFields[i]); |
17694 | } |
17695 | CDecl->setIvarLBraceLoc(LBrac); |
17696 | CDecl->setIvarRBraceLoc(RBrac); |
17697 | } |
17698 | } |
17699 | } |
17700 | |
17701 | |
17702 | |
17703 | static bool isRepresentableIntegerValue(ASTContext &Context, |
17704 | llvm::APSInt &Value, |
17705 | QualType T) { |
17706 | assert((T->isIntegralType(Context) || T->isEnumeralType()) && |
17707 | "Integral type required!"); |
17708 | unsigned BitWidth = Context.getIntWidth(T); |
17709 | |
17710 | if (Value.isUnsigned() || Value.isNonNegative()) { |
17711 | if (T->isSignedIntegerOrEnumerationType()) |
17712 | --BitWidth; |
17713 | return Value.getActiveBits() <= BitWidth; |
17714 | } |
17715 | return Value.getMinSignedBits() <= BitWidth; |
17716 | } |
17717 | |
17718 | |
17719 | |
17720 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { |
17721 | |
17722 | |
17723 | assert((T->isIntegralType(Context) || |
17724 | T->isEnumeralType()) && "Integral type required!"); |
17725 | const unsigned NumTypes = 4; |
17726 | QualType SignedIntegralTypes[NumTypes] = { |
17727 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy |
17728 | }; |
17729 | QualType UnsignedIntegralTypes[NumTypes] = { |
17730 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, |
17731 | Context.UnsignedLongLongTy |
17732 | }; |
17733 | |
17734 | unsigned BitWidth = Context.getTypeSize(T); |
17735 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
17736 | : UnsignedIntegralTypes; |
17737 | for (unsigned I = 0; I != NumTypes; ++I) |
17738 | if (Context.getTypeSize(Types[I]) > BitWidth) |
17739 | return Types[I]; |
17740 | |
17741 | return QualType(); |
17742 | } |
17743 | |
17744 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
17745 | EnumConstantDecl *LastEnumConst, |
17746 | SourceLocation IdLoc, |
17747 | IdentifierInfo *Id, |
17748 | Expr *Val) { |
17749 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
17750 | llvm::APSInt EnumVal(IntWidth); |
17751 | QualType EltTy; |
17752 | |
17753 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) |
17754 | Val = nullptr; |
17755 | |
17756 | if (Val) |
17757 | Val = DefaultLvalueConversion(Val).get(); |
17758 | |
17759 | if (Val) { |
17760 | if (Enum->isDependentType() || Val->isTypeDependent()) |
17761 | EltTy = Context.DependentTy; |
17762 | else { |
17763 | |
17764 | |
17765 | if (getLangOpts().CPlusPlus11 && Enum->isFixed()) { |
17766 | |
17767 | |
17768 | |
17769 | EltTy = Enum->getIntegerType(); |
17770 | ExprResult Converted = |
17771 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, |
17772 | CCEK_Enumerator); |
17773 | if (Converted.isInvalid()) |
17774 | Val = nullptr; |
17775 | else |
17776 | Val = Converted.get(); |
17777 | } else if (!Val->isValueDependent() && |
17778 | !(Val = |
17779 | VerifyIntegerConstantExpression(Val, &EnumVal, AllowFold) |
17780 | .get())) { |
17781 | |
17782 | } else { |
17783 | if (Enum->isComplete()) { |
17784 | EltTy = Enum->getIntegerType(); |
17785 | |
17786 | |
17787 | |
17788 | |
17789 | |
17790 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
17791 | if (Context.getTargetInfo() |
17792 | .getTriple() |
17793 | .isWindowsMSVCEnvironment()) { |
17794 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
17795 | } else { |
17796 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; |
17797 | } |
17798 | } |
17799 | |
17800 | |
17801 | Val = ImpCastExprToType(Val, EltTy, |
17802 | EltTy->isBooleanType() ? CK_IntegralToBoolean |
17803 | : CK_IntegralCast) |
17804 | .get(); |
17805 | } else if (getLangOpts().CPlusPlus) { |
17806 | |
17807 | |
17808 | |
17809 | |
17810 | |
17811 | EltTy = Val->getType(); |
17812 | } else { |
17813 | |
17814 | |
17815 | |
17816 | |
17817 | |
17818 | |
17819 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) |
17820 | Diag(IdLoc, diag::ext_enum_value_not_int) |
17821 | << toString(EnumVal, 10) << Val->getSourceRange() |
17822 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); |
17823 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { |
17824 | |
17825 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).get(); |
17826 | } |
17827 | EltTy = Val->getType(); |
17828 | } |
17829 | } |
17830 | } |
17831 | } |
17832 | |
17833 | if (!Val) { |
17834 | if (Enum->isDependentType()) |
17835 | EltTy = Context.DependentTy; |
17836 | else if (!LastEnumConst) { |
17837 | |
17838 | |
17839 | |
17840 | |
17841 | |
17842 | |
17843 | |
17844 | |
17845 | if (Enum->isFixed()) { |
17846 | EltTy = Enum->getIntegerType(); |
17847 | } |
17848 | else { |
17849 | EltTy = Context.IntTy; |
17850 | } |
17851 | } else { |
17852 | |
17853 | EnumVal = LastEnumConst->getInitVal(); |
17854 | ++EnumVal; |
17855 | EltTy = LastEnumConst->getType(); |
17856 | |
17857 | |
17858 | if (EnumVal < LastEnumConst->getInitVal()) { |
17859 | |
17860 | |
17861 | |
17862 | |
17863 | |
17864 | |
17865 | |
17866 | |
17867 | |
17868 | |
17869 | QualType T = getNextLargerIntegralType(Context, EltTy); |
17870 | if (T.isNull() || Enum->isFixed()) { |
17871 | |
17872 | |
17873 | EnumVal = LastEnumConst->getInitVal(); |
17874 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
17875 | ++EnumVal; |
17876 | if (Enum->isFixed()) |
17877 | |
17878 | Diag(IdLoc, diag::err_enumerator_wrapped) |
17879 | << toString(EnumVal, 10) |
17880 | << EltTy; |
17881 | else |
17882 | Diag(IdLoc, diag::ext_enumerator_increment_too_large) |
17883 | << toString(EnumVal, 10); |
17884 | } else { |
17885 | EltTy = T; |
17886 | } |
17887 | |
17888 | |
17889 | |
17890 | |
17891 | EnumVal = LastEnumConst->getInitVal(); |
17892 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
17893 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
17894 | ++EnumVal; |
17895 | |
17896 | |
17897 | |
17898 | |
17899 | |
17900 | |
17901 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
17902 | Diag(IdLoc, diag::warn_enum_value_overflow); |
17903 | } else if (!getLangOpts().CPlusPlus && |
17904 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
17905 | |
17906 | Diag(IdLoc, diag::ext_enum_value_not_int) |
17907 | << toString(EnumVal, 10) << 1; |
17908 | } |
17909 | } |
17910 | } |
17911 | |
17912 | if (!EltTy->isDependentType()) { |
17913 | |
17914 | |
17915 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
17916 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
17917 | } |
17918 | |
17919 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
17920 | Val, EnumVal); |
17921 | } |
17922 | |
17923 | Sema::SkipBodyInfo Sema::shouldSkipAnonEnumBody(Scope *S, IdentifierInfo *II, |
17924 | SourceLocation IILoc) { |
17925 | if (!(getLangOpts().Modules || getLangOpts().ModulesLocalVisibility) || |
17926 | !getLangOpts().CPlusPlus) |
17927 | return SkipBodyInfo(); |
17928 | |
17929 | |
17930 | |
17931 | |
17932 | NamedDecl *PrevDecl = LookupSingleName(S, II, IILoc, LookupOrdinaryName, |
17933 | forRedeclarationInCurContext()); |
17934 | auto *PrevECD = dyn_cast_or_null<EnumConstantDecl>(PrevDecl); |
17935 | if (!PrevECD) |
17936 | return SkipBodyInfo(); |
17937 | |
17938 | EnumDecl *PrevED = cast<EnumDecl>(PrevECD->getDeclContext()); |
17939 | NamedDecl *Hidden; |
17940 | if (!PrevED->getDeclName() && !hasVisibleDefinition(PrevED, &Hidden)) { |
17941 | SkipBodyInfo Skip; |
17942 | Skip.Previous = Hidden; |
17943 | return Skip; |
17944 | } |
17945 | |
17946 | return SkipBodyInfo(); |
17947 | } |
17948 | |
17949 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
17950 | SourceLocation IdLoc, IdentifierInfo *Id, |
17951 | const ParsedAttributesView &Attrs, |
17952 | SourceLocation EqualLoc, Expr *Val) { |
17953 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
17954 | EnumConstantDecl *LastEnumConst = |
17955 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
17956 | |
17957 | |
17958 | |
17959 | S = getNonFieldDeclScope(S); |
17960 | |
17961 | |
17962 | |
17963 | LookupResult R(*this, Id, IdLoc, LookupOrdinaryName, ForVisibleRedeclaration); |
17964 | LookupName(R, S); |
17965 | NamedDecl *PrevDecl = R.getAsSingle<NamedDecl>(); |
17966 | |
17967 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
17968 | |
17969 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
17970 | |
17971 | PrevDecl = nullptr; |
17972 | } |
17973 | |
17974 | |
17975 | |
17976 | |
17977 | |
17978 | |
17979 | if (getLangOpts().CPlusPlus && !TheEnumDecl->isScoped()) |
17980 | DiagnoseClassNameShadow(TheEnumDecl->getDeclContext(), |
17981 | DeclarationNameInfo(Id, IdLoc)); |
17982 | |
17983 | EnumConstantDecl *New = |
17984 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); |
17985 | if (!New) |
17986 | return nullptr; |
17987 | |
17988 | if (PrevDecl) { |
17989 | if (!TheEnumDecl->isScoped() && isa<ValueDecl>(PrevDecl)) { |
17990 | |
17991 | CheckShadow(New, PrevDecl, R); |
17992 | } |
17993 | |
17994 | |
17995 | |
17996 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
17997 | "Received TagDecl when not in C++!"); |
17998 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
17999 | if (isa<EnumConstantDecl>(PrevDecl)) |
18000 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
18001 | else |
18002 | Diag(IdLoc, diag::err_redefinition) << Id; |
18003 | notePreviousDefinition(PrevDecl, IdLoc); |
18004 | return nullptr; |
18005 | } |
18006 | } |
18007 | |
18008 | |
18009 | ProcessDeclAttributeList(S, New, Attrs); |
18010 | AddPragmaAttributes(S, New); |
18011 | |
18012 | |
18013 | New->setAccess(TheEnumDecl->getAccess()); |
18014 | PushOnScopeChains(New, S); |
18015 | |
18016 | ActOnDocumentableDecl(New); |
18017 | |
18018 | return New; |
18019 | } |
18020 | |
18021 | |
18022 | |
18023 | |
18024 | |
18025 | |
18026 | |
18027 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { |
18028 | Expr *InitExpr = ECD->getInitExpr(); |
18029 | if (!InitExpr) |
18030 | return true; |
18031 | InitExpr = InitExpr->IgnoreImpCasts(); |
18032 | |
18033 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { |
18034 | if (!BO->isAdditiveOp()) |
18035 | return true; |
18036 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); |
18037 | if (!IL) |
18038 | return true; |
18039 | if (IL->getValue() != 1) |
18040 | return true; |
18041 | |
18042 | InitExpr = BO->getLHS(); |
18043 | } |
18044 | |
18045 | |
18046 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); |
18047 | if (!DRE) |
18048 | return true; |
18049 | |
18050 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); |
18051 | if (!EnumConstant) |
18052 | return true; |
18053 | |
18054 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != |
18055 | Enum) |
18056 | return true; |
18057 | |
18058 | return false; |
18059 | } |
18060 | |
18061 | |
18062 | |
18063 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
18064 | EnumDecl *Enum, QualType EnumType) { |
18065 | |
18066 | if (!Enum->getIdentifier()) |
18067 | return; |
18068 | |
18069 | |
18070 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) |
18071 | return; |
18072 | |
18073 | if (S.Diags.isIgnored(diag::warn_duplicate_enum_values, Enum->getLocation())) |
18074 | return; |
18075 | |
18076 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
18077 | typedef SmallVector<std::unique_ptr<ECDVector>, 3> DuplicatesVector; |
18078 | |
18079 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; |
18080 | |
18081 | |
18082 | typedef std::unordered_map<int64_t, DeclOrVector> ValueToVectorMap; |
18083 | |
18084 | |
18085 | auto EnumConstantToKey = [](const EnumConstantDecl *D) { |
18086 | llvm::APSInt Val = D->getInitVal(); |
18087 | return Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(); |
18088 | }; |
18089 | |
18090 | DuplicatesVector DupVector; |
18091 | ValueToVectorMap EnumMap; |
18092 | |
18093 | |
18094 | |
18095 | for (auto *Element : Elements) { |
18096 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Element); |
18097 | |
18098 | |
18099 | |
18100 | if (!ECD) { |
18101 | return; |
18102 | } |
18103 | |
18104 | |
18105 | if (ECD->getInitExpr()) |
18106 | continue; |
18107 | |
18108 | |
18109 | EnumMap.insert({EnumConstantToKey(ECD), ECD}); |
18110 | } |
18111 | |
18112 | if (EnumMap.size() == 0) |
18113 | return; |
18114 | |
18115 | |
18116 | for (auto *Element : Elements) { |
18117 | |
18118 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Element); |
18119 | if (!ValidDuplicateEnum(ECD, Enum)) |
18120 | continue; |
18121 | |
18122 | auto Iter = EnumMap.find(EnumConstantToKey(ECD)); |
18123 | if (Iter == EnumMap.end()) |
18124 | continue; |
18125 | |
18126 | DeclOrVector& Entry = Iter->second; |
18127 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { |
18128 | |
18129 | if (D == ECD) |
18130 | continue; |
18131 | |
18132 | |
18133 | auto Vec = std::make_unique<ECDVector>(); |
18134 | Vec->push_back(D); |
18135 | Vec->push_back(ECD); |
18136 | |
18137 | |
18138 | Entry = Vec.get(); |
18139 | |
18140 | |
18141 | |
18142 | DupVector.emplace_back(std::move(Vec)); |
18143 | continue; |
18144 | } |
18145 | |
18146 | ECDVector *Vec = Entry.get<ECDVector*>(); |
18147 | |
18148 | if (*Vec->begin() == ECD) |
18149 | continue; |
18150 | |
18151 | Vec->push_back(ECD); |
18152 | } |
18153 | |
18154 | |
18155 | for (const auto &Vec : DupVector) { |
18156 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); |
18157 | |
18158 | |
18159 | auto *FirstECD = Vec->front(); |
18160 | S.Diag(FirstECD->getLocation(), diag::warn_duplicate_enum_values) |
18161 | << FirstECD << toString(FirstECD->getInitVal(), 10) |
18162 | << FirstECD->getSourceRange(); |
18163 | |
18164 | |
18165 | |
18166 | for (auto *ECD : llvm::make_range(Vec->begin() + 1, Vec->end())) |
18167 | S.Diag(ECD->getLocation(), diag::note_duplicate_element) |
18168 | << ECD << toString(ECD->getInitVal(), 10) |
18169 | << ECD->getSourceRange(); |
18170 | } |
18171 | } |
18172 | |
18173 | bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val, |
18174 | bool AllowMask) const { |
18175 | assert(ED->isClosedFlag() && "looking for value in non-flag or open enum"); |
18176 | assert(ED->isCompleteDefinition() && "expected enum definition"); |
18177 | |
18178 | auto R = FlagBitsCache.insert(std::make_pair(ED, llvm::APInt())); |
18179 | llvm::APInt &FlagBits = R.first->second; |
18180 | |
18181 | if (R.second) { |
18182 | for (auto *E : ED->enumerators()) { |
18183 | const auto &EVal = E->getInitVal(); |
18184 | |
18185 | if (EVal.isPowerOf2()) |
18186 | FlagBits = FlagBits.zextOrSelf(EVal.getBitWidth()) | EVal; |
18187 | } |
18188 | } |
18189 | |
18190 | |
18191 | |
18192 | |
18193 | |
18194 | |
18195 | |
18196 | |
18197 | |
18198 | llvm::APInt FlagMask = ~FlagBits.zextOrTrunc(Val.getBitWidth()); |
18199 | return !(FlagMask & Val) || (AllowMask && !(FlagMask & ~Val)); |
18200 | } |
18201 | |
18202 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange, |
18203 | Decl *EnumDeclX, ArrayRef<Decl *> Elements, Scope *S, |
18204 | const ParsedAttributesView &Attrs) { |
18205 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
18206 | QualType EnumType = Context.getTypeDeclType(Enum); |
18207 | |
18208 | ProcessDeclAttributeList(S, Enum, Attrs); |
18209 | |
18210 | if (Enum->isDependentType()) { |
18211 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
18212 | EnumConstantDecl *ECD = |
18213 | cast_or_null<EnumConstantDecl>(Elements[i]); |
18214 | if (!ECD) continue; |
18215 | |
18216 | ECD->setType(EnumType); |
18217 | } |
18218 | |
18219 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
18220 | return; |
18221 | } |
18222 | |
18223 | |
18224 | |
18225 | |
18226 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
18227 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); |
18228 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); |
18229 | |
18230 | |
18231 | |
18232 | unsigned NumNegativeBits = 0; |
18233 | unsigned NumPositiveBits = 0; |
18234 | |
18235 | |
18236 | bool AllElementsInt = true; |
18237 | |
18238 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
18239 | EnumConstantDecl *ECD = |
18240 | cast_or_null<EnumConstantDecl>(Elements[i]); |
18241 | if (!ECD) continue; |
18242 | |
18243 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
18244 | |
18245 | |
18246 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
18247 | NumPositiveBits = std::max(NumPositiveBits, |
18248 | (unsigned)InitVal.getActiveBits()); |
18249 | else |
18250 | NumNegativeBits = std::max(NumNegativeBits, |
18251 | (unsigned)InitVal.getMinSignedBits()); |
18252 | |
18253 | |
18254 | if (AllElementsInt) |
18255 | AllElementsInt = ECD->getType() == Context.IntTy; |
18256 | } |
18257 | |
18258 | |
18259 | QualType BestType; |
18260 | unsigned BestWidth; |
18261 | |
18262 | |
18263 | |
18264 | |
18265 | |
18266 | |
18267 | |
18268 | |
18269 | |
18270 | |
18271 | QualType BestPromotionType; |
18272 | |
18273 | bool Packed = Enum->hasAttr<PackedAttr>(); |
18274 | |
18275 | |
18276 | if (LangOpts.ShortEnums) |
18277 | Packed = true; |
18278 | |
18279 | |
18280 | |
18281 | if (Enum->isComplete()) { |
18282 | BestType = Enum->getIntegerType(); |
18283 | if (BestType->isPromotableIntegerType()) |
18284 | BestPromotionType = Context.getPromotedIntegerType(BestType); |
18285 | else |
18286 | BestPromotionType = BestType; |
18287 | |
18288 | BestWidth = Context.getIntWidth(BestType); |
18289 | } |
18290 | else if (NumNegativeBits) { |
18291 | |
18292 | |
18293 | |
18294 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { |
18295 | BestType = Context.SignedCharTy; |
18296 | BestWidth = CharWidth; |
18297 | } else if (Packed && NumNegativeBits <= ShortWidth && |
18298 | NumPositiveBits < ShortWidth) { |
18299 | BestType = Context.ShortTy; |
18300 | BestWidth = ShortWidth; |
18301 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
18302 | BestType = Context.IntTy; |
18303 | BestWidth = IntWidth; |
18304 | } else { |
18305 | BestWidth = Context.getTargetInfo().getLongWidth(); |
18306 | |
18307 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
18308 | BestType = Context.LongTy; |
18309 | } else { |
18310 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
18311 | |
18312 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
18313 | Diag(Enum->getLocation(), diag::ext_enum_too_large); |
18314 | BestType = Context.LongLongTy; |
18315 | } |
18316 | } |
18317 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
18318 | } else { |
18319 | |
18320 | |
18321 | |
18322 | if (Packed && NumPositiveBits <= CharWidth) { |
18323 | BestType = Context.UnsignedCharTy; |
18324 | BestPromotionType = Context.IntTy; |
18325 | BestWidth = CharWidth; |
18326 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
18327 | BestType = Context.UnsignedShortTy; |
18328 | BestPromotionType = Context.IntTy; |
18329 | BestWidth = ShortWidth; |
18330 | } else if (NumPositiveBits <= IntWidth) { |
18331 | BestType = Context.UnsignedIntTy; |
18332 | BestWidth = IntWidth; |
18333 | BestPromotionType |
18334 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
18335 | ? Context.UnsignedIntTy : Context.IntTy; |
18336 | } else if (NumPositiveBits <= |
18337 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
18338 | BestType = Context.UnsignedLongTy; |
18339 | BestPromotionType |
18340 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
18341 | ? Context.UnsignedLongTy : Context.LongTy; |
18342 | } else { |
18343 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
18344 | assert(NumPositiveBits <= BestWidth && |
18345 | "How could an initializer get larger than ULL?"); |
18346 | BestType = Context.UnsignedLongLongTy; |
18347 | BestPromotionType |
18348 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
18349 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
18350 | } |
18351 | } |
18352 | |
18353 | |
18354 | |
18355 | for (auto *D : Elements) { |
18356 | auto *ECD = cast_or_null<EnumConstantDecl>(D); |
18357 | if (!ECD) continue; |
18358 | |
18359 | |
18360 | |
18361 | |
18362 | |
18363 | |
18364 | |
18365 | |
18366 | llvm::APSInt InitVal = ECD->getInitVal(); |
18367 | |
18368 | |
18369 | |
18370 | QualType NewTy; |
18371 | unsigned NewWidth; |
18372 | bool NewSign; |
18373 | if (!getLangOpts().CPlusPlus && |
18374 | !Enum->isFixed() && |
18375 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
18376 | NewTy = Context.IntTy; |
18377 | NewWidth = IntWidth; |
18378 | NewSign = true; |
18379 | } else if (ECD->getType() == BestType) { |
18380 | |
18381 | if (getLangOpts().CPlusPlus) |
18382 | |
18383 | |
18384 | |
18385 | ECD->setType(EnumType); |
18386 | continue; |
18387 | } else { |
18388 | NewTy = BestType; |
18389 | NewWidth = BestWidth; |
18390 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
18391 | } |
18392 | |
18393 | |
18394 | InitVal = InitVal.extOrTrunc(NewWidth); |
18395 | InitVal.setIsSigned(NewSign); |
18396 | ECD->setInitVal(InitVal); |
18397 | |
18398 | |
18399 | if (ECD->getInitExpr() && |
18400 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
18401 | ECD->setInitExpr(ImplicitCastExpr::Create( |
18402 | Context, NewTy, CK_IntegralCast, ECD->getInitExpr(), |
18403 | nullptr, VK_PRValue, FPOptionsOverride())); |
18404 | if (getLangOpts().CPlusPlus) |
18405 | |
18406 | |
18407 | |
18408 | ECD->setType(EnumType); |
18409 | else |
18410 | ECD->setType(NewTy); |
18411 | } |
18412 | |
18413 | Enum->completeDefinition(BestType, BestPromotionType, |
18414 | NumPositiveBits, NumNegativeBits); |
18415 | |
18416 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
18417 | |
18418 | if (Enum->isClosedFlag()) { |
18419 | for (Decl *D : Elements) { |
18420 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(D); |
18421 | if (!ECD) continue; |
18422 | |
18423 | llvm::APSInt InitVal = ECD->getInitVal(); |
18424 | if (InitVal != 0 && !InitVal.isPowerOf2() && |
18425 | !IsValueInFlagEnum(Enum, InitVal, true)) |
18426 | Diag(ECD->getLocation(), diag::warn_flag_enum_constant_out_of_range) |
18427 | << ECD << Enum; |
18428 | } |
18429 | } |
18430 | |
18431 | |
18432 | if (Enum->hasAttrs()) |
18433 | CheckAlignasUnderalignment(Enum); |
18434 | } |
18435 | |
18436 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
18437 | SourceLocation StartLoc, |
18438 | SourceLocation EndLoc) { |
18439 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
18440 | |
18441 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
18442 | AsmString, StartLoc, |
18443 | EndLoc); |
18444 | CurContext->addDecl(New); |
18445 | return New; |
18446 | } |
18447 | |
18448 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
18449 | IdentifierInfo* AliasName, |
18450 | SourceLocation PragmaLoc, |
18451 | SourceLocation NameLoc, |
18452 | SourceLocation AliasNameLoc) { |
18453 | NamedDecl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, |
18454 | LookupOrdinaryName); |
18455 | AttributeCommonInfo Info(AliasName, SourceRange(AliasNameLoc), |
18456 | AttributeCommonInfo::AS_Pragma); |
18457 | AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit( |
18458 | Context, AliasName->getName(), true, Info); |
18459 | |
18460 | |
18461 | |
18462 | |
18463 | |
18464 | if (PrevDecl && (isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) { |
18465 | if (isDeclExternC(PrevDecl)) |
18466 | PrevDecl->addAttr(Attr); |
18467 | else |
18468 | Diag(PrevDecl->getLocation(), diag::warn_redefine_extname_not_applied) |
18469 | << (isa<FunctionDecl>(PrevDecl) ? 0 : 1) << PrevDecl; |
18470 | |
18471 | } else |
18472 | (void)ExtnameUndeclaredIdentifiers.insert(std::make_pair(Name, Attr)); |
18473 | } |
18474 | |
18475 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
18476 | SourceLocation PragmaLoc, |
18477 | SourceLocation NameLoc) { |
18478 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
18479 | |
18480 | if (PrevDecl) { |
18481 | PrevDecl->addAttr(WeakAttr::CreateImplicit(Context, PragmaLoc, AttributeCommonInfo::AS_Pragma)); |
18482 | } else { |
18483 | (void)WeakUndeclaredIdentifiers.insert( |
18484 | std::pair<IdentifierInfo*,WeakInfo> |
18485 | (Name, WeakInfo((IdentifierInfo*)nullptr, NameLoc))); |
18486 | } |
18487 | } |
18488 | |
18489 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, |
18490 | IdentifierInfo* AliasName, |
18491 | SourceLocation PragmaLoc, |
18492 | SourceLocation NameLoc, |
18493 | SourceLocation AliasNameLoc) { |
18494 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
18495 | LookupOrdinaryName); |
18496 | WeakInfo W = WeakInfo(Name, NameLoc); |
18497 | |
18498 | if (PrevDecl && (isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) { |
18499 | if (!PrevDecl->hasAttr<AliasAttr>()) |
18500 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) |
18501 | DeclApplyPragmaWeak(TUScope, ND, W); |
18502 | } else { |
18503 | (void)WeakUndeclaredIdentifiers.insert( |
18504 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); |
18505 | } |
18506 | } |
18507 | |
18508 | Decl *Sema::getObjCDeclContext() const { |
18509 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); |
18510 | } |
18511 | |
18512 | Sema::FunctionEmissionStatus Sema::getEmissionStatus(FunctionDecl *FD, |
18513 | bool Final) { |
18514 | assert(FD && "Expected non-null FunctionDecl"); |
18515 | |
18516 | |
18517 | |
18518 | if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLKernelAttr>()) |
18519 | return FunctionEmissionStatus::Emitted; |
18520 | |
18521 | |
18522 | if (FD->isDependentContext()) |
18523 | return FunctionEmissionStatus::TemplateDiscarded; |
18524 | |
18525 | |
18526 | auto IsEmittedForExternalSymbol = [this, FD]() { |
18527 | |
18528 | |
18529 | |
18530 | FunctionDecl *Def = FD->getDefinition(); |
18531 | |
18532 | return Def && !isDiscardableGVALinkage( |
18533 | getASTContext().GetGVALinkageForFunction(Def)); |
18534 | }; |
18535 | |
18536 | if (LangOpts.OpenMPIsDevice) { |
18537 | |
18538 | |
18539 | Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = |
18540 | OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl()); |
18541 | |
18542 | |
18543 | |
18544 | |
18545 | if (DevTy.hasValue()) |
18546 | if (*DevTy == OMPDeclareTargetDeclAttr::DT_Host) |
18547 | return FunctionEmissionStatus::OMPDiscarded; |
18548 | |
18549 | |
18550 | if (isInOpenMPDeclareTargetContext() || DevTy.hasValue()) |
18551 | if (IsEmittedForExternalSymbol()) |
18552 | return FunctionEmissionStatus::Emitted; |
18553 | |
18554 | |
18555 | if (Final) |
18556 | return FunctionEmissionStatus::OMPDiscarded; |
18557 | } else if (LangOpts.OpenMP > 45) { |
18558 | |
18559 | |
18560 | |
18561 | Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = |
18562 | OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl()); |
18563 | if (DevTy.hasValue()) |
18564 | if (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) |
18565 | return FunctionEmissionStatus::OMPDiscarded; |
18566 | } |
18567 | |
18568 | if (Final && LangOpts.OpenMP && !LangOpts.CUDA) |
18569 | return FunctionEmissionStatus::Emitted; |
18570 | |
18571 | if (LangOpts.CUDA) { |
18572 | |
18573 | |
18574 | |
18575 | |
18576 | Sema::CUDAFunctionTarget T = IdentifyCUDATarget(FD); |
18577 | if (LangOpts.CUDAIsDevice && T == Sema::CFT_Host) |
18578 | return FunctionEmissionStatus::CUDADiscarded; |
18579 | if (!LangOpts.CUDAIsDevice && |
18580 | (T == Sema::CFT_Device || T == Sema::CFT_Global)) |
18581 | return FunctionEmissionStatus::CUDADiscarded; |
18582 | |
18583 | if (IsEmittedForExternalSymbol()) |
18584 | return FunctionEmissionStatus::Emitted; |
18585 | } |
18586 | |
18587 | |
18588 | |
18589 | return FunctionEmissionStatus::Unknown; |
18590 | } |
18591 | |
18592 | bool Sema::shouldIgnoreInHostDeviceCheck(FunctionDecl *Callee) { |
18593 | |
18594 | |
18595 | |
18596 | |
18597 | |
18598 | |
18599 | |
18600 | return LangOpts.CUDA && !LangOpts.CUDAIsDevice && |
18601 | IdentifyCUDATarget(Callee) == CFT_Global; |
18602 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_CLANG_AST_TYPE_H |
18 | #define LLVM_CLANG_AST_TYPE_H |
19 | |
20 | #include "clang/AST/DependenceFlags.h" |
21 | #include "clang/AST/NestedNameSpecifier.h" |
22 | #include "clang/AST/TemplateName.h" |
23 | #include "clang/Basic/AddressSpaces.h" |
24 | #include "clang/Basic/AttrKinds.h" |
25 | #include "clang/Basic/Diagnostic.h" |
26 | #include "clang/Basic/ExceptionSpecificationType.h" |
27 | #include "clang/Basic/LLVM.h" |
28 | #include "clang/Basic/Linkage.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceLocation.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Basic/Visibility.h" |
33 | #include "llvm/ADT/APInt.h" |
34 | #include "llvm/ADT/APSInt.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/FoldingSet.h" |
37 | #include "llvm/ADT/None.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/Twine.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/PointerLikeTypeTraits.h" |
48 | #include "llvm/Support/TrailingObjects.h" |
49 | #include "llvm/Support/type_traits.h" |
50 | #include <cassert> |
51 | #include <cstddef> |
52 | #include <cstdint> |
53 | #include <cstring> |
54 | #include <string> |
55 | #include <type_traits> |
56 | #include <utility> |
57 | |
58 | namespace clang { |
59 | |
60 | class ExtQuals; |
61 | class QualType; |
62 | class ConceptDecl; |
63 | class TagDecl; |
64 | class TemplateParameterList; |
65 | class Type; |
66 | |
67 | enum { |
68 | TypeAlignmentInBits = 4, |
69 | TypeAlignment = 1 << TypeAlignmentInBits |
70 | }; |
71 | |
72 | namespace serialization { |
73 | template <class T> class AbstractTypeReader; |
74 | template <class T> class AbstractTypeWriter; |
75 | } |
76 | |
77 | } |
78 | |
79 | namespace llvm { |
80 | |
81 | template <typename T> |
82 | struct PointerLikeTypeTraits; |
83 | template<> |
84 | struct PointerLikeTypeTraits< ::clang::Type*> { |
85 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } |
86 | |
87 | static inline ::clang::Type *getFromVoidPointer(void *P) { |
88 | return static_cast< ::clang::Type*>(P); |
89 | } |
90 | |
91 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
92 | }; |
93 | |
94 | template<> |
95 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { |
96 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } |
97 | |
98 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { |
99 | return static_cast< ::clang::ExtQuals*>(P); |
100 | } |
101 | |
102 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
103 | }; |
104 | |
105 | } |
106 | |
107 | namespace clang { |
108 | |
109 | class ASTContext; |
110 | template <typename> class CanQual; |
111 | class CXXRecordDecl; |
112 | class DeclContext; |
113 | class EnumDecl; |
114 | class Expr; |
115 | class ExtQualsTypeCommonBase; |
116 | class FunctionDecl; |
117 | class IdentifierInfo; |
118 | class NamedDecl; |
119 | class ObjCInterfaceDecl; |
120 | class ObjCProtocolDecl; |
121 | class ObjCTypeParamDecl; |
122 | struct PrintingPolicy; |
123 | class RecordDecl; |
124 | class Stmt; |
125 | class TagDecl; |
126 | class TemplateArgument; |
127 | class TemplateArgumentListInfo; |
128 | class TemplateArgumentLoc; |
129 | class TemplateTypeParmDecl; |
130 | class TypedefNameDecl; |
131 | class UnresolvedUsingTypenameDecl; |
132 | |
133 | using CanQualType = CanQual<Type>; |
134 | |
135 | |
136 | #define TYPE(Class, Base) class Class##Type; |
137 | #include "clang/AST/TypeNodes.inc" |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | class Qualifiers { |
146 | public: |
147 | enum TQ { |
148 | Const = 0x1, |
149 | Restrict = 0x2, |
150 | Volatile = 0x4, |
151 | CVRMask = Const | Volatile | Restrict |
152 | }; |
153 | |
154 | enum GC { |
155 | GCNone = 0, |
156 | Weak, |
157 | Strong |
158 | }; |
159 | |
160 | enum ObjCLifetime { |
161 | |
162 | OCL_None, |
163 | |
164 | |
165 | |
166 | OCL_ExplicitNone, |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | OCL_Strong, |
174 | |
175 | |
176 | OCL_Weak, |
177 | |
178 | |
179 | OCL_Autoreleasing |
180 | }; |
181 | |
182 | enum { |
183 | |
184 | |
185 | MaxAddressSpace = 0x7fffffu, |
186 | |
187 | |
188 | FastWidth = 3, |
189 | |
190 | |
191 | FastMask = (1 << FastWidth) - 1 |
192 | }; |
193 | |
194 | |
195 | |
196 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { |
197 | |
198 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { |
199 | Qualifiers Q; |
200 | Q.Mask = L.Mask & R.Mask; |
201 | L.Mask &= ~Q.Mask; |
202 | R.Mask &= ~Q.Mask; |
203 | return Q; |
204 | } |
205 | |
206 | Qualifiers Q; |
207 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); |
208 | Q.addCVRQualifiers(CommonCRV); |
209 | L.removeCVRQualifiers(CommonCRV); |
210 | R.removeCVRQualifiers(CommonCRV); |
211 | |
212 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { |
213 | Q.setObjCGCAttr(L.getObjCGCAttr()); |
214 | L.removeObjCGCAttr(); |
215 | R.removeObjCGCAttr(); |
216 | } |
217 | |
218 | if (L.getObjCLifetime() == R.getObjCLifetime()) { |
219 | Q.setObjCLifetime(L.getObjCLifetime()); |
220 | L.removeObjCLifetime(); |
221 | R.removeObjCLifetime(); |
222 | } |
223 | |
224 | if (L.getAddressSpace() == R.getAddressSpace()) { |
225 | Q.setAddressSpace(L.getAddressSpace()); |
226 | L.removeAddressSpace(); |
227 | R.removeAddressSpace(); |
228 | } |
229 | return Q; |
230 | } |
231 | |
232 | static Qualifiers fromFastMask(unsigned Mask) { |
233 | Qualifiers Qs; |
234 | Qs.addFastQualifiers(Mask); |
235 | return Qs; |
236 | } |
237 | |
238 | static Qualifiers fromCVRMask(unsigned CVR) { |
239 | Qualifiers Qs; |
240 | Qs.addCVRQualifiers(CVR); |
241 | return Qs; |
242 | } |
243 | |
244 | static Qualifiers fromCVRUMask(unsigned CVRU) { |
245 | Qualifiers Qs; |
246 | Qs.addCVRUQualifiers(CVRU); |
247 | return Qs; |
248 | } |
249 | |
250 | |
251 | static Qualifiers fromOpaqueValue(unsigned opaque) { |
252 | Qualifiers Qs; |
253 | Qs.Mask = opaque; |
254 | return Qs; |
255 | } |
256 | |
257 | |
258 | unsigned getAsOpaqueValue() const { |
259 | return Mask; |
260 | } |
261 | |
262 | bool hasConst() const { return Mask & Const; } |
263 | bool hasOnlyConst() const { return Mask == Const; } |
264 | void removeConst() { Mask &= ~Const; } |
265 | void addConst() { Mask |= Const; } |
266 | |
267 | bool hasVolatile() const { return Mask & Volatile; } |
268 | bool hasOnlyVolatile() const { return Mask == Volatile; } |
269 | void removeVolatile() { Mask &= ~Volatile; } |
270 | void addVolatile() { Mask |= Volatile; } |
271 | |
272 | bool hasRestrict() const { return Mask & Restrict; } |
273 | bool hasOnlyRestrict() const { return Mask == Restrict; } |
274 | void removeRestrict() { Mask &= ~Restrict; } |
275 | void addRestrict() { Mask |= Restrict; } |
276 | |
277 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } |
278 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } |
279 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } |
280 | |
281 | void setCVRQualifiers(unsigned mask) { |
282 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
283 | Mask = (Mask & ~CVRMask) | mask; |
284 | } |
285 | void removeCVRQualifiers(unsigned mask) { |
286 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
287 | Mask &= ~mask; |
288 | } |
289 | void removeCVRQualifiers() { |
290 | removeCVRQualifiers(CVRMask); |
291 | } |
292 | void addCVRQualifiers(unsigned mask) { |
293 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
294 | Mask |= mask; |
295 | } |
296 | void addCVRUQualifiers(unsigned mask) { |
297 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits"); |
298 | Mask |= mask; |
299 | } |
300 | |
301 | bool hasUnaligned() const { return Mask & UMask; } |
302 | void setUnaligned(bool flag) { |
303 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); |
304 | } |
305 | void removeUnaligned() { Mask &= ~UMask; } |
306 | void addUnaligned() { Mask |= UMask; } |
307 | |
308 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } |
309 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } |
310 | void setObjCGCAttr(GC type) { |
311 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); |
312 | } |
313 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } |
314 | void addObjCGCAttr(GC type) { |
315 | assert(type); |
316 | setObjCGCAttr(type); |
317 | } |
318 | Qualifiers withoutObjCGCAttr() const { |
319 | Qualifiers qs = *this; |
320 | qs.removeObjCGCAttr(); |
321 | return qs; |
322 | } |
323 | Qualifiers withoutObjCLifetime() const { |
324 | Qualifiers qs = *this; |
325 | qs.removeObjCLifetime(); |
326 | return qs; |
327 | } |
328 | Qualifiers withoutAddressSpace() const { |
329 | Qualifiers qs = *this; |
330 | qs.removeAddressSpace(); |
331 | return qs; |
332 | } |
333 | |
334 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } |
335 | ObjCLifetime getObjCLifetime() const { |
336 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); |
337 | } |
338 | void setObjCLifetime(ObjCLifetime type) { |
339 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); |
340 | } |
341 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } |
342 | void addObjCLifetime(ObjCLifetime type) { |
343 | assert(type); |
344 | assert(!hasObjCLifetime()); |
345 | Mask |= (type << LifetimeShift); |
346 | } |
347 | |
348 | |
349 | bool hasNonTrivialObjCLifetime() const { |
350 | ObjCLifetime lifetime = getObjCLifetime(); |
351 | return (lifetime > OCL_ExplicitNone); |
352 | } |
353 | |
354 | |
355 | bool hasStrongOrWeakObjCLifetime() const { |
356 | ObjCLifetime lifetime = getObjCLifetime(); |
357 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); |
358 | } |
359 | |
360 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } |
361 | LangAS getAddressSpace() const { |
362 | return static_cast<LangAS>(Mask >> AddressSpaceShift); |
363 | } |
364 | bool hasTargetSpecificAddressSpace() const { |
365 | return isTargetAddressSpace(getAddressSpace()); |
366 | } |
367 | |
368 | unsigned getAddressSpaceAttributePrintValue() const { |
369 | auto Addr = getAddressSpace(); |
370 | |
371 | |
372 | |
373 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace()); |
374 | if (Addr != LangAS::Default) |
375 | return toTargetAddressSpace(Addr); |
376 | |
377 | |
378 | |
379 | return 0; |
380 | } |
381 | void setAddressSpace(LangAS space) { |
382 | assert((unsigned)space <= MaxAddressSpace); |
383 | Mask = (Mask & ~AddressSpaceMask) |
384 | | (((uint32_t) space) << AddressSpaceShift); |
385 | } |
386 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } |
387 | void addAddressSpace(LangAS space) { |
388 | assert(space != LangAS::Default); |
389 | setAddressSpace(space); |
390 | } |
391 | |
392 | |
393 | |
394 | bool hasFastQualifiers() const { return getFastQualifiers(); } |
395 | unsigned getFastQualifiers() const { return Mask & FastMask; } |
396 | void setFastQualifiers(unsigned mask) { |
397 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
398 | Mask = (Mask & ~FastMask) | mask; |
399 | } |
400 | void removeFastQualifiers(unsigned mask) { |
401 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
402 | Mask &= ~mask; |
403 | } |
404 | void removeFastQualifiers() { |
405 | removeFastQualifiers(FastMask); |
406 | } |
407 | void addFastQualifiers(unsigned mask) { |
408 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
409 | Mask |= mask; |
410 | } |
411 | |
412 | |
413 | |
414 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } |
415 | Qualifiers getNonFastQualifiers() const { |
416 | Qualifiers Quals = *this; |
417 | Quals.setFastQualifiers(0); |
418 | return Quals; |
419 | } |
420 | |
421 | |
422 | bool hasQualifiers() const { return Mask; } |
423 | bool empty() const { return !Mask; } |
424 | |
425 | |
426 | void addQualifiers(Qualifiers Q) { |
427 | |
428 | |
429 | if (!(Q.Mask & ~CVRMask)) |
430 | Mask |= Q.Mask; |
431 | else { |
432 | Mask |= (Q.Mask & CVRMask); |
433 | if (Q.hasAddressSpace()) |
434 | addAddressSpace(Q.getAddressSpace()); |
435 | if (Q.hasObjCGCAttr()) |
436 | addObjCGCAttr(Q.getObjCGCAttr()); |
437 | if (Q.hasObjCLifetime()) |
438 | addObjCLifetime(Q.getObjCLifetime()); |
439 | } |
440 | } |
441 | |
442 | |
443 | void removeQualifiers(Qualifiers Q) { |
444 | |
445 | |
446 | if (!(Q.Mask & ~CVRMask)) |
447 | Mask &= ~Q.Mask; |
448 | else { |
449 | Mask &= ~(Q.Mask & CVRMask); |
450 | if (getObjCGCAttr() == Q.getObjCGCAttr()) |
451 | removeObjCGCAttr(); |
452 | if (getObjCLifetime() == Q.getObjCLifetime()) |
453 | removeObjCLifetime(); |
454 | if (getAddressSpace() == Q.getAddressSpace()) |
455 | removeAddressSpace(); |
456 | } |
457 | } |
458 | |
459 | |
460 | |
461 | void addConsistentQualifiers(Qualifiers qs) { |
462 | assert(getAddressSpace() == qs.getAddressSpace() || |
463 | !hasAddressSpace() || !qs.hasAddressSpace()); |
464 | assert(getObjCGCAttr() == qs.getObjCGCAttr() || |
465 | !hasObjCGCAttr() || !qs.hasObjCGCAttr()); |
466 | assert(getObjCLifetime() == qs.getObjCLifetime() || |
467 | !hasObjCLifetime() || !qs.hasObjCLifetime()); |
468 | Mask |= qs.Mask; |
469 | } |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | |
478 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { |
479 | |
480 | return A == B || |
481 | |
482 | |
483 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
484 | |
485 | |
486 | |
487 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || |
488 | B == LangAS::opencl_global_host)) || |
489 | (A == LangAS::sycl_global && (B == LangAS::sycl_global_device || |
490 | B == LangAS::sycl_global_host)) || |
491 | |
492 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && |
493 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)) || |
494 | |
495 | (A == LangAS::Default && |
496 | (B == LangAS::sycl_private || B == LangAS::sycl_local || |
497 | B == LangAS::sycl_global || B == LangAS::sycl_global_device || |
498 | B == LangAS::sycl_global_host)) || |
499 | |
500 | |
501 | (A == LangAS::Default && |
502 | (B == LangAS::cuda_constant || B == LangAS::cuda_device || |
503 | B == LangAS::cuda_shared)); |
504 | } |
505 | |
506 | |
507 | |
508 | bool isAddressSpaceSupersetOf(Qualifiers other) const { |
509 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); |
510 | } |
511 | |
512 | |
513 | |
514 | |
515 | bool compatiblyIncludes(Qualifiers other) const { |
516 | return isAddressSpaceSupersetOf(other) && |
517 | |
518 | |
519 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || |
520 | !other.hasObjCGCAttr()) && |
521 | |
522 | getObjCLifetime() == other.getObjCLifetime() && |
523 | |
524 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && |
525 | |
526 | (!other.hasUnaligned() || hasUnaligned()); |
527 | } |
528 | |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | |
536 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { |
537 | if (getObjCLifetime() == other.getObjCLifetime()) |
538 | return true; |
539 | |
540 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) |
541 | return false; |
542 | |
543 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) |
544 | return true; |
545 | |
546 | return hasConst(); |
547 | } |
548 | |
549 | |
550 | |
551 | bool isStrictSupersetOf(Qualifiers Other) const; |
552 | |
553 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } |
554 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } |
555 | |
556 | explicit operator bool() const { return hasQualifiers(); } |
557 | |
558 | Qualifiers &operator+=(Qualifiers R) { |
559 | addQualifiers(R); |
560 | return *this; |
561 | } |
562 | |
563 | |
564 | |
565 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { |
566 | L += R; |
567 | return L; |
568 | } |
569 | |
570 | Qualifiers &operator-=(Qualifiers R) { |
571 | removeQualifiers(R); |
572 | return *this; |
573 | } |
574 | |
575 | |
576 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { |
577 | L -= R; |
578 | return L; |
579 | } |
580 | |
581 | std::string getAsString() const; |
582 | std::string getAsString(const PrintingPolicy &Policy) const; |
583 | |
584 | static std::string getAddrSpaceAsString(LangAS AS); |
585 | |
586 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; |
587 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
588 | bool appendSpaceIfNonEmpty = false) const; |
589 | |
590 | void Profile(llvm::FoldingSetNodeID &ID) const { |
591 | ID.AddInteger(Mask); |
592 | } |
593 | |
594 | private: |
595 | |
596 | |
597 | uint32_t Mask = 0; |
598 | |
599 | static const uint32_t UMask = 0x8; |
600 | static const uint32_t UShift = 3; |
601 | static const uint32_t GCAttrMask = 0x30; |
602 | static const uint32_t GCAttrShift = 4; |
603 | static const uint32_t LifetimeMask = 0x1C0; |
604 | static const uint32_t LifetimeShift = 6; |
605 | static const uint32_t AddressSpaceMask = |
606 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); |
607 | static const uint32_t AddressSpaceShift = 9; |
608 | }; |
609 | |
610 | |
611 | |
612 | struct SplitQualType { |
613 | |
614 | const Type *Ty = nullptr; |
615 | |
616 | |
617 | Qualifiers Quals; |
618 | |
619 | SplitQualType() = default; |
620 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} |
621 | |
622 | SplitQualType getSingleStepDesugaredType() const; |
623 | |
624 | |
625 | std::pair<const Type *,Qualifiers> asPair() const { |
626 | return std::pair<const Type *, Qualifiers>(Ty, Quals); |
627 | } |
628 | |
629 | friend bool operator==(SplitQualType a, SplitQualType b) { |
630 | return a.Ty == b.Ty && a.Quals == b.Quals; |
631 | } |
632 | friend bool operator!=(SplitQualType a, SplitQualType b) { |
633 | return a.Ty != b.Ty || a.Quals != b.Quals; |
634 | } |
635 | }; |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | enum class ObjCSubstitutionContext { |
643 | |
644 | Ordinary, |
645 | |
646 | |
647 | Result, |
648 | |
649 | |
650 | Parameter, |
651 | |
652 | |
653 | Property, |
654 | |
655 | |
656 | Superclass, |
657 | }; |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | |
672 | |
673 | class QualType { |
674 | friend class QualifierCollector; |
675 | |
676 | |
677 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, |
678 | Qualifiers::FastWidth> Value; |
679 | |
680 | const ExtQuals *getExtQualsUnsafe() const { |
681 | return Value.getPointer().get<const ExtQuals*>(); |
682 | } |
683 | |
684 | const Type *getTypePtrUnsafe() const { |
685 | return Value.getPointer().get<const Type*>(); |
686 | } |
687 | |
688 | const ExtQualsTypeCommonBase *getCommonPtr() const { |
689 | assert(!isNull() && "Cannot retrieve a NULL type pointer"); |
690 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); |
691 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); |
692 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); |
693 | } |
694 | |
695 | public: |
696 | QualType() = default; |
697 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
698 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
699 | |
700 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } |
701 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | const Type *getTypePtr() const; |
708 | |
709 | const Type *getTypePtrOrNull() const; |
710 | |
711 | |
712 | const IdentifierInfo *getBaseTypeIdentifier() const; |
713 | |
714 | |
715 | |
716 | SplitQualType split() const; |
717 | |
718 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
719 | |
720 | static QualType getFromOpaquePtr(const void *Ptr) { |
721 | QualType T; |
722 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); |
723 | return T; |
724 | } |
725 | |
726 | const Type &operator*() const { |
727 | return *getTypePtr(); |
728 | } |
729 | |
730 | const Type *operator->() const { |
731 | return getTypePtr(); |
732 | } |
733 | |
734 | bool isCanonical() const; |
735 | bool isCanonicalAsParam() const; |
736 | |
737 | |
738 | bool isNull() const { |
739 | return Value.getPointer().isNull(); |
740 | } |
741 | |
742 | |
743 | |
744 | |
745 | bool isLocalConstQualified() const { |
746 | return (getLocalFastQualifiers() & Qualifiers::Const); |
747 | } |
748 | |
749 | |
750 | bool isConstQualified() const; |
751 | |
752 | |
753 | |
754 | |
755 | bool isLocalRestrictQualified() const { |
756 | return (getLocalFastQualifiers() & Qualifiers::Restrict); |
757 | } |
758 | |
759 | |
760 | bool isRestrictQualified() const; |
761 | |
762 | |
763 | |
764 | |
765 | bool isLocalVolatileQualified() const { |
766 | return (getLocalFastQualifiers() & Qualifiers::Volatile); |
767 | } |
768 | |
769 | |
770 | bool isVolatileQualified() const; |
771 | |
772 | |
773 | |
774 | |
775 | bool hasLocalQualifiers() const { |
776 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); |
777 | } |
778 | |
779 | |
780 | bool hasQualifiers() const; |
781 | |
782 | |
783 | |
784 | |
785 | bool hasLocalNonFastQualifiers() const { |
786 | return Value.getPointer().is<const ExtQuals*>(); |
787 | } |
788 | |
789 | |
790 | |
791 | |
792 | Qualifiers getLocalQualifiers() const; |
793 | |
794 | |
795 | Qualifiers getQualifiers() const; |
796 | |
797 | |
798 | |
799 | |
800 | unsigned getLocalCVRQualifiers() const { |
801 | return getLocalFastQualifiers(); |
802 | } |
803 | |
804 | |
805 | |
806 | unsigned getCVRQualifiers() const; |
807 | |
808 | bool isConstant(const ASTContext& Ctx) const { |
809 | return QualType::isConstant(*this, Ctx); |
810 | } |
811 | |
812 | |
813 | bool isPODType(const ASTContext &Context) const; |
814 | |
815 | |
816 | |
817 | bool isCXX98PODType(const ASTContext &Context) const; |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | bool isCXX11PODType(const ASTContext &Context) const; |
824 | |
825 | |
826 | bool isTrivialType(const ASTContext &Context) const; |
827 | |
828 | |
829 | bool isTriviallyCopyableType(const ASTContext &Context) const; |
830 | |
831 | |
832 | |
833 | bool mayBeDynamicClass() const; |
834 | |
835 | |
836 | bool mayBeNotDynamicClass() const; |
837 | |
838 | |
839 | |
840 | |
841 | |
842 | void addConst() { |
843 | addFastQualifiers(Qualifiers::Const); |
844 | } |
845 | QualType withConst() const { |
846 | return withFastQualifiers(Qualifiers::Const); |
847 | } |
848 | |
849 | |
850 | void addVolatile() { |
851 | addFastQualifiers(Qualifiers::Volatile); |
852 | } |
853 | QualType withVolatile() const { |
854 | return withFastQualifiers(Qualifiers::Volatile); |
855 | } |
856 | |
857 | |
858 | void addRestrict() { |
859 | addFastQualifiers(Qualifiers::Restrict); |
860 | } |
861 | QualType withRestrict() const { |
862 | return withFastQualifiers(Qualifiers::Restrict); |
863 | } |
864 | |
865 | QualType withCVRQualifiers(unsigned CVR) const { |
866 | return withFastQualifiers(CVR); |
867 | } |
868 | |
869 | void addFastQualifiers(unsigned TQs) { |
870 | assert(!(TQs & ~Qualifiers::FastMask) |
871 | && "non-fast qualifier bits set in mask!"); |
872 | Value.setInt(Value.getInt() | TQs); |
873 | } |
874 | |
875 | void removeLocalConst(); |
876 | void removeLocalVolatile(); |
877 | void removeLocalRestrict(); |
878 | void removeLocalCVRQualifiers(unsigned Mask); |
879 | |
880 | void removeLocalFastQualifiers() { Value.setInt(0); } |
881 | void removeLocalFastQualifiers(unsigned Mask) { |
882 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); |
883 | Value.setInt(Value.getInt() & ~Mask); |
884 | } |
885 | |
886 | |
887 | |
888 | QualType withFastQualifiers(unsigned TQs) const { |
889 | QualType T = *this; |
890 | T.addFastQualifiers(TQs); |
891 | return T; |
892 | } |
893 | |
894 | |
895 | |
896 | QualType withExactLocalFastQualifiers(unsigned TQs) const { |
897 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); |
898 | } |
899 | |
900 | |
901 | QualType withoutLocalFastQualifiers() const { |
902 | QualType T = *this; |
903 | T.removeLocalFastQualifiers(); |
904 | return T; |
905 | } |
906 | |
907 | QualType getCanonicalType() const; |
908 | |
909 | |
910 | |
911 | |
912 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } |
913 | |
914 | |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | inline QualType getUnqualifiedType() const; |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | |
940 | |
941 | |
942 | |
943 | inline SplitQualType getSplitUnqualifiedType() const; |
944 | |
945 | |
946 | |
947 | bool isMoreQualifiedThan(QualType Other) const; |
948 | |
949 | |
950 | |
951 | bool isAtLeastAsQualifiedAs(QualType Other) const; |
952 | |
953 | QualType getNonReferenceType() const; |
954 | |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | QualType getNonLValueExprType(const ASTContext &Context) const; |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | QualType getNonPackExpansionType() const; |
970 | |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | |
977 | |
978 | |
979 | QualType getDesugaredType(const ASTContext &Context) const { |
980 | return getDesugaredType(*this, Context); |
981 | } |
982 | |
983 | SplitQualType getSplitDesugaredType() const { |
984 | return getSplitDesugaredType(*this); |
985 | } |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | |
992 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { |
993 | return getSingleStepDesugaredTypeImpl(*this, Context); |
994 | } |
995 | |
996 | |
997 | |
998 | QualType IgnoreParens() const { |
999 | if (isa<ParenType>(*this)) |
1000 | return QualType::IgnoreParens(*this); |
1001 | return *this; |
1002 | } |
1003 | |
1004 | |
1005 | friend bool operator==(const QualType &LHS, const QualType &RHS) { |
1006 | return LHS.Value == RHS.Value; |
1007 | } |
1008 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { |
1009 | return LHS.Value != RHS.Value; |
1010 | } |
1011 | friend bool operator<(const QualType &LHS, const QualType &RHS) { |
1012 | return LHS.Value < RHS.Value; |
1013 | } |
1014 | |
1015 | static std::string getAsString(SplitQualType split, |
1016 | const PrintingPolicy &Policy) { |
1017 | return getAsString(split.Ty, split.Quals, Policy); |
1018 | } |
1019 | static std::string getAsString(const Type *ty, Qualifiers qs, |
1020 | const PrintingPolicy &Policy); |
1021 | |
1022 | std::string getAsString() const; |
1023 | std::string getAsString(const PrintingPolicy &Policy) const; |
1024 | |
1025 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
1026 | const Twine &PlaceHolder = Twine(), |
1027 | unsigned Indentation = 0) const; |
1028 | |
1029 | static void print(SplitQualType split, raw_ostream &OS, |
1030 | const PrintingPolicy &policy, const Twine &PlaceHolder, |
1031 | unsigned Indentation = 0) { |
1032 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); |
1033 | } |
1034 | |
1035 | static void print(const Type *ty, Qualifiers qs, |
1036 | raw_ostream &OS, const PrintingPolicy &policy, |
1037 | const Twine &PlaceHolder, |
1038 | unsigned Indentation = 0); |
1039 | |
1040 | void getAsStringInternal(std::string &Str, |
1041 | const PrintingPolicy &Policy) const; |
1042 | |
1043 | static void getAsStringInternal(SplitQualType split, std::string &out, |
1044 | const PrintingPolicy &policy) { |
1045 | return getAsStringInternal(split.Ty, split.Quals, out, policy); |
1046 | } |
1047 | |
1048 | static void getAsStringInternal(const Type *ty, Qualifiers qs, |
1049 | std::string &out, |
1050 | const PrintingPolicy &policy); |
1051 | |
1052 | class StreamedQualTypeHelper { |
1053 | const QualType &T; |
1054 | const PrintingPolicy &Policy; |
1055 | const Twine &PlaceHolder; |
1056 | unsigned Indentation; |
1057 | |
1058 | public: |
1059 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, |
1060 | const Twine &PlaceHolder, unsigned Indentation) |
1061 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), |
1062 | Indentation(Indentation) {} |
1063 | |
1064 | friend raw_ostream &operator<<(raw_ostream &OS, |
1065 | const StreamedQualTypeHelper &SQT) { |
1066 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); |
1067 | return OS; |
1068 | } |
1069 | }; |
1070 | |
1071 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, |
1072 | const Twine &PlaceHolder = Twine(), |
1073 | unsigned Indentation = 0) const { |
1074 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); |
1075 | } |
1076 | |
1077 | void dump(const char *s) const; |
1078 | void dump() const; |
1079 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
1080 | |
1081 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1082 | ID.AddPointer(getAsOpaquePtr()); |
1083 | } |
1084 | |
1085 | |
1086 | inline bool hasAddressSpace() const; |
1087 | |
1088 | |
1089 | inline LangAS getAddressSpace() const; |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | bool isAddressSpaceOverlapping(QualType T) const { |
1100 | Qualifiers Q = getQualifiers(); |
1101 | Qualifiers TQ = T.getQualifiers(); |
1102 | |
1103 | return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q); |
1104 | } |
1105 | |
1106 | |
1107 | inline Qualifiers::GC getObjCGCAttr() const; |
1108 | |
1109 | |
1110 | bool isObjCGCWeak() const { |
1111 | return getObjCGCAttr() == Qualifiers::Weak; |
1112 | } |
1113 | |
1114 | |
1115 | bool isObjCGCStrong() const { |
1116 | return getObjCGCAttr() == Qualifiers::Strong; |
1117 | } |
1118 | |
1119 | |
1120 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1121 | return getQualifiers().getObjCLifetime(); |
1122 | } |
1123 | |
1124 | bool hasNonTrivialObjCLifetime() const { |
1125 | return getQualifiers().hasNonTrivialObjCLifetime(); |
1126 | } |
1127 | |
1128 | bool hasStrongOrWeakObjCLifetime() const { |
1129 | return getQualifiers().hasStrongOrWeakObjCLifetime(); |
1130 | } |
1131 | |
1132 | |
1133 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; |
1134 | |
1135 | enum PrimitiveDefaultInitializeKind { |
1136 | |
1137 | |
1138 | |
1139 | PDIK_Trivial, |
1140 | |
1141 | |
1142 | |
1143 | PDIK_ARCStrong, |
1144 | |
1145 | |
1146 | |
1147 | PDIK_ARCWeak, |
1148 | |
1149 | |
1150 | PDIK_Struct |
1151 | }; |
1152 | |
1153 | |
1154 | |
1155 | |
1156 | |
1157 | |
1158 | PrimitiveDefaultInitializeKind |
1159 | isNonTrivialToPrimitiveDefaultInitialize() const; |
1160 | |
1161 | enum PrimitiveCopyKind { |
1162 | |
1163 | |
1164 | |
1165 | PCK_Trivial, |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | PCK_VolatileTrivial, |
1171 | |
1172 | |
1173 | |
1174 | PCK_ARCStrong, |
1175 | |
1176 | |
1177 | |
1178 | PCK_ARCWeak, |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | PCK_Struct |
1187 | }; |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | |
1199 | |
1200 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; |
1201 | |
1202 | enum DestructionKind { |
1203 | DK_none, |
1204 | DK_cxx_destructor, |
1205 | DK_objc_strong_lifetime, |
1206 | DK_objc_weak_lifetime, |
1207 | DK_nontrivial_c_struct |
1208 | }; |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | DestructionKind isDestructedType() const { |
1215 | return isDestructedTypeImpl(*this); |
1216 | } |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | bool hasNonTrivialToPrimitiveDestructCUnion() const; |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | bool hasNonTrivialToPrimitiveCopyCUnion() const; |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | bool isCForbiddenLValueType() const; |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | |
1260 | QualType substObjCTypeArgs(ASTContext &ctx, |
1261 | ArrayRef<QualType> typeArgs, |
1262 | ObjCSubstitutionContext context) const; |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | QualType substObjCMemberType(QualType objectType, |
1285 | const DeclContext *dc, |
1286 | ObjCSubstitutionContext context) const; |
1287 | |
1288 | |
1289 | QualType stripObjCKindOfType(const ASTContext &ctx) const; |
1290 | |
1291 | |
1292 | QualType getAtomicUnqualifiedType() const; |
1293 | |
1294 | private: |
1295 | |
1296 | |
1297 | |
1298 | static bool isConstant(QualType T, const ASTContext& Ctx); |
1299 | static QualType getDesugaredType(QualType T, const ASTContext &Context); |
1300 | static SplitQualType getSplitDesugaredType(QualType T); |
1301 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); |
1302 | static QualType getSingleStepDesugaredTypeImpl(QualType type, |
1303 | const ASTContext &C); |
1304 | static QualType IgnoreParens(QualType T); |
1305 | static DestructionKind isDestructedTypeImpl(QualType type); |
1306 | |
1307 | |
1308 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); |
1309 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); |
1310 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); |
1311 | }; |
1312 | |
1313 | } |
1314 | |
1315 | namespace llvm { |
1316 | |
1317 | |
1318 | |
1319 | template<> struct simplify_type< ::clang::QualType> { |
1320 | using SimpleType = const ::clang::Type *; |
1321 | |
1322 | static SimpleType getSimplifiedValue(::clang::QualType Val) { |
1323 | return Val.getTypePtr(); |
1324 | } |
1325 | }; |
1326 | |
1327 | |
1328 | template<> |
1329 | struct PointerLikeTypeTraits<clang::QualType> { |
1330 | static inline void *getAsVoidPointer(clang::QualType P) { |
1331 | return P.getAsOpaquePtr(); |
1332 | } |
1333 | |
1334 | static inline clang::QualType getFromVoidPointer(void *P) { |
1335 | return clang::QualType::getFromOpaquePtr(P); |
1336 | } |
1337 | |
1338 | |
1339 | static constexpr int NumLowBitsAvailable = 0; |
1340 | }; |
1341 | |
1342 | } |
1343 | |
1344 | namespace clang { |
1345 | |
1346 | |
1347 | |
1348 | |
1349 | class ExtQualsTypeCommonBase { |
1350 | friend class ExtQuals; |
1351 | friend class QualType; |
1352 | friend class Type; |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | const Type *const BaseType; |
1360 | |
1361 | |
1362 | QualType CanonicalType; |
1363 | |
1364 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) |
1365 | : BaseType(baseType), CanonicalType(canon) {} |
1366 | }; |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | Qualifiers Quals; |
1395 | |
1396 | ExtQuals *this_() { return this; } |
1397 | |
1398 | public: |
1399 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) |
1400 | : ExtQualsTypeCommonBase(baseType, |
1401 | canon.isNull() ? QualType(this_(), 0) : canon), |
1402 | Quals(quals) { |
1403 | assert(Quals.hasNonFastQualifiers() |
1404 | && "ExtQuals created with no fast qualifiers"); |
1405 | assert(!Quals.hasFastQualifiers() |
1406 | && "ExtQuals created with fast qualifiers"); |
1407 | } |
1408 | |
1409 | Qualifiers getQualifiers() const { return Quals; } |
1410 | |
1411 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } |
1412 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } |
1413 | |
1414 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } |
1415 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1416 | return Quals.getObjCLifetime(); |
1417 | } |
1418 | |
1419 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } |
1420 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
1421 | |
1422 | const Type *getBaseType() const { return BaseType; } |
1423 | |
1424 | public: |
1425 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1426 | Profile(ID, getBaseType(), Quals); |
1427 | } |
1428 | |
1429 | static void Profile(llvm::FoldingSetNodeID &ID, |
1430 | const Type *BaseType, |
1431 | Qualifiers Quals) { |
1432 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); |
1433 | ID.AddPointer(BaseType); |
1434 | Quals.Profile(ID); |
1435 | } |
1436 | }; |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | enum RefQualifierKind { |
1442 | |
1443 | RQ_None = 0, |
1444 | |
1445 | |
1446 | RQ_LValue, |
1447 | |
1448 | |
1449 | RQ_RValue |
1450 | }; |
1451 | |
1452 | |
1453 | enum class AutoTypeKeyword { |
1454 | |
1455 | Auto, |
1456 | |
1457 | |
1458 | DecltypeAuto, |
1459 | |
1460 | |
1461 | GNUAutoType |
1462 | }; |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | |
1483 | |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | class alignas(8) Type : public ExtQualsTypeCommonBase { |
1491 | public: |
1492 | enum TypeClass { |
1493 | #define TYPE(Class, Base) Class, |
1494 | #define LAST_TYPE(Class) TypeLast = Class |
1495 | #define ABSTRACT_TYPE(Class, Base) |
1496 | #include "clang/AST/TypeNodes.inc" |
1497 | }; |
1498 | |
1499 | private: |
1500 | |
1501 | class TypeBitfields { |
1502 | friend class Type; |
1503 | template <class T> friend class TypePropertyCache; |
1504 | |
1505 | |
1506 | unsigned TC : 8; |
1507 | |
1508 | |
1509 | unsigned Dependence : llvm::BitWidth<TypeDependence>; |
1510 | |
1511 | |
1512 | |
1513 | mutable unsigned CacheValid : 1; |
1514 | |
1515 | |
1516 | mutable unsigned CachedLinkage : 3; |
1517 | |
1518 | |
1519 | mutable unsigned CachedLocalOrUnnamed : 1; |
1520 | |
1521 | |
1522 | mutable unsigned FromAST : 1; |
1523 | |
1524 | bool isCacheValid() const { |
1525 | return CacheValid; |
1526 | } |
1527 | |
1528 | Linkage getLinkage() const { |
1529 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1530 | return static_cast<Linkage>(CachedLinkage); |
1531 | } |
1532 | |
1533 | bool hasLocalOrUnnamedType() const { |
1534 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1535 | return CachedLocalOrUnnamed; |
1536 | } |
1537 | }; |
1538 | enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; |
1539 | |
1540 | protected: |
1541 | |
1542 | |
1543 | |
1544 | class ArrayTypeBitfields { |
1545 | friend class ArrayType; |
1546 | |
1547 | unsigned : NumTypeBits; |
1548 | |
1549 | |
1550 | |
1551 | unsigned IndexTypeQuals : 3; |
1552 | |
1553 | |
1554 | |
1555 | |
1556 | unsigned SizeModifier : 3; |
1557 | }; |
1558 | |
1559 | class ConstantArrayTypeBitfields { |
1560 | friend class ConstantArrayType; |
1561 | |
1562 | unsigned : NumTypeBits + 3 + 3; |
1563 | |
1564 | |
1565 | unsigned HasStoredSizeExpr : 1; |
1566 | }; |
1567 | |
1568 | class BuiltinTypeBitfields { |
1569 | friend class BuiltinType; |
1570 | |
1571 | unsigned : NumTypeBits; |
1572 | |
1573 | |
1574 | unsigned Kind : 8; |
1575 | }; |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | class FunctionTypeBitfields { |
1581 | friend class FunctionProtoType; |
1582 | friend class FunctionType; |
1583 | |
1584 | unsigned : NumTypeBits; |
1585 | |
1586 | |
1587 | |
1588 | unsigned ExtInfo : 13; |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | unsigned RefQualifier : 2; |
1594 | |
1595 | |
1596 | |
1597 | |
1598 | |
1599 | |
1600 | |
1601 | unsigned FastTypeQuals : Qualifiers::FastWidth; |
1602 | |
1603 | unsigned HasExtQuals : 1; |
1604 | |
1605 | |
1606 | |
1607 | |
1608 | |
1609 | unsigned NumParams : 16; |
1610 | |
1611 | |
1612 | unsigned ExceptionSpecType : 4; |
1613 | |
1614 | |
1615 | unsigned HasExtParameterInfos : 1; |
1616 | |
1617 | |
1618 | unsigned Variadic : 1; |
1619 | |
1620 | |
1621 | unsigned HasTrailingReturn : 1; |
1622 | }; |
1623 | |
1624 | class ObjCObjectTypeBitfields { |
1625 | friend class ObjCObjectType; |
1626 | |
1627 | unsigned : NumTypeBits; |
1628 | |
1629 | |
1630 | unsigned NumTypeArgs : 7; |
1631 | |
1632 | |
1633 | unsigned NumProtocols : 6; |
1634 | |
1635 | |
1636 | unsigned IsKindOf : 1; |
1637 | }; |
1638 | |
1639 | class ReferenceTypeBitfields { |
1640 | friend class ReferenceType; |
1641 | |
1642 | unsigned : NumTypeBits; |
1643 | |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | unsigned SpelledAsLValue : 1; |
1656 | |
1657 | |
1658 | |
1659 | unsigned InnerRef : 1; |
1660 | }; |
1661 | |
1662 | class TypeWithKeywordBitfields { |
1663 | friend class TypeWithKeyword; |
1664 | |
1665 | unsigned : NumTypeBits; |
1666 | |
1667 | |
1668 | unsigned Keyword : 8; |
1669 | }; |
1670 | |
1671 | enum { NumTypeWithKeywordBits = 8 }; |
1672 | |
1673 | class ElaboratedTypeBitfields { |
1674 | friend class ElaboratedType; |
1675 | |
1676 | unsigned : NumTypeBits; |
1677 | unsigned : NumTypeWithKeywordBits; |
1678 | |
1679 | |
1680 | unsigned HasOwnedTagDecl : 1; |
1681 | }; |
1682 | |
1683 | class VectorTypeBitfields { |
1684 | friend class VectorType; |
1685 | friend class DependentVectorType; |
1686 | |
1687 | unsigned : NumTypeBits; |
1688 | |
1689 | |
1690 | |
1691 | unsigned VecKind : 3; |
1692 | |
1693 | uint32_t NumElements; |
1694 | }; |
1695 | |
1696 | class AttributedTypeBitfields { |
1697 | friend class AttributedType; |
1698 | |
1699 | unsigned : NumTypeBits; |
1700 | |
1701 | |
1702 | unsigned AttrKind : 32 - NumTypeBits; |
1703 | }; |
1704 | |
1705 | class AutoTypeBitfields { |
1706 | friend class AutoType; |
1707 | |
1708 | unsigned : NumTypeBits; |
1709 | |
1710 | |
1711 | |
1712 | unsigned Keyword : 2; |
1713 | |
1714 | |
1715 | |
1716 | |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | unsigned NumArgs; |
1722 | }; |
1723 | |
1724 | class SubstTemplateTypeParmPackTypeBitfields { |
1725 | friend class SubstTemplateTypeParmPackType; |
1726 | |
1727 | unsigned : NumTypeBits; |
1728 | |
1729 | |
1730 | |
1731 | |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | unsigned NumArgs; |
1737 | }; |
1738 | |
1739 | class TemplateSpecializationTypeBitfields { |
1740 | friend class TemplateSpecializationType; |
1741 | |
1742 | unsigned : NumTypeBits; |
1743 | |
1744 | |
1745 | unsigned TypeAlias : 1; |
1746 | |
1747 | |
1748 | |
1749 | |
1750 | |
1751 | |
1752 | |
1753 | |
1754 | unsigned NumArgs; |
1755 | }; |
1756 | |
1757 | class DependentTemplateSpecializationTypeBitfields { |
1758 | friend class DependentTemplateSpecializationType; |
1759 | |
1760 | unsigned : NumTypeBits; |
1761 | unsigned : NumTypeWithKeywordBits; |
1762 | |
1763 | |
1764 | |
1765 | |
1766 | |
1767 | |
1768 | |
1769 | |
1770 | unsigned NumArgs; |
1771 | }; |
1772 | |
1773 | class PackExpansionTypeBitfields { |
1774 | friend class PackExpansionType; |
1775 | |
1776 | unsigned : NumTypeBits; |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | |
1787 | |
1788 | |
1789 | unsigned NumExpansions; |
1790 | }; |
1791 | |
1792 | union { |
1793 | TypeBitfields TypeBits; |
1794 | ArrayTypeBitfields ArrayTypeBits; |
1795 | ConstantArrayTypeBitfields ConstantArrayTypeBits; |
1796 | AttributedTypeBitfields AttributedTypeBits; |
1797 | AutoTypeBitfields AutoTypeBits; |
1798 | BuiltinTypeBitfields BuiltinTypeBits; |
1799 | FunctionTypeBitfields FunctionTypeBits; |
1800 | ObjCObjectTypeBitfields ObjCObjectTypeBits; |
1801 | ReferenceTypeBitfields ReferenceTypeBits; |
1802 | TypeWithKeywordBitfields TypeWithKeywordBits; |
1803 | ElaboratedTypeBitfields ElaboratedTypeBits; |
1804 | VectorTypeBitfields VectorTypeBits; |
1805 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; |
1806 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; |
1807 | DependentTemplateSpecializationTypeBitfields |
1808 | DependentTemplateSpecializationTypeBits; |
1809 | PackExpansionTypeBitfields PackExpansionTypeBits; |
1810 | }; |
1811 | |
1812 | private: |
1813 | template <class T> friend class TypePropertyCache; |
1814 | |
1815 | |
1816 | void setFromAST(bool V = true) const { |
1817 | TypeBits.FromAST = V; |
1818 | } |
1819 | |
1820 | protected: |
1821 | friend class ASTContext; |
1822 | |
1823 | Type(TypeClass tc, QualType canon, TypeDependence Dependence) |
1824 | : ExtQualsTypeCommonBase(this, |
1825 | canon.isNull() ? QualType(this_(), 0) : canon) { |
1826 | static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase), |
1827 | "changing bitfields changed sizeof(Type)!"); |
1828 | static_assert(alignof(decltype(*this)) % sizeof(void *) == 0, |
1829 | "Insufficient alignment!"); |
1830 | TypeBits.TC = tc; |
1831 | TypeBits.Dependence = static_cast<unsigned>(Dependence); |
1832 | TypeBits.CacheValid = false; |
1833 | TypeBits.CachedLocalOrUnnamed = false; |
1834 | TypeBits.CachedLinkage = NoLinkage; |
1835 | TypeBits.FromAST = false; |
1836 | } |
1837 | |
1838 | |
1839 | Type *this_() { return this; } |
1840 | |
1841 | void setDependence(TypeDependence D) { |
1842 | TypeBits.Dependence = static_cast<unsigned>(D); |
1843 | } |
1844 | |
1845 | void addDependence(TypeDependence D) { setDependence(getDependence() | D); } |
1846 | |
1847 | public: |
1848 | friend class ASTReader; |
1849 | friend class ASTWriter; |
1850 | template <class T> friend class serialization::AbstractTypeReader; |
1851 | template <class T> friend class serialization::AbstractTypeWriter; |
1852 | |
1853 | Type(const Type &) = delete; |
1854 | Type(Type &&) = delete; |
1855 | Type &operator=(const Type &) = delete; |
1856 | Type &operator=(Type &&) = delete; |
1857 | |
1858 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } |
1859 | |
1860 | |
1861 | bool isFromAST() const { return TypeBits.FromAST; } |
1862 | |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | |
1878 | bool containsUnexpandedParameterPack() const { |
1879 | return getDependence() & TypeDependence::UnexpandedPack; |
1880 | } |
1881 | |
1882 | |
1883 | |
1884 | bool isCanonicalUnqualified() const { |
1885 | return CanonicalType == QualType(this, 0); |
1886 | } |
1887 | |
1888 | |
1889 | |
1890 | |
1891 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | |
1897 | |
1898 | |
1899 | bool isSizelessType() const; |
1900 | bool isSizelessBuiltinType() const; |
1901 | |
1902 | |
1903 | |
1904 | |
1905 | bool isVLSTBuiltinType() const; |
1906 | |
1907 | |
1908 | |
1909 | |
1910 | QualType getSveEltType(const ASTContext &Ctx) const; |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | |
1920 | |
1921 | |
1922 | |
1923 | bool isIncompleteType(NamedDecl **Def = nullptr) const; |
1924 | |
1925 | |
1926 | |
1927 | bool isIncompleteOrObjectType() const { |
1928 | return !isFunctionType(); |
1929 | } |
1930 | |
1931 | |
1932 | bool isObjectType() const { |
1933 | |
1934 | |
1935 | |
1936 | return !isReferenceType() && !isFunctionType() && !isVoidType(); |
1937 | } |
1938 | |
1939 | |
1940 | |
1941 | bool isLiteralType(const ASTContext &Ctx) const; |
1942 | |
1943 | |
1944 | bool isStructuralType() const; |
1945 | |
1946 | |
1947 | |
1948 | bool isStandardLayoutType() const; |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | bool isBuiltinType() const; |
1955 | |
1956 | |
1957 | bool isSpecificBuiltinType(unsigned K) const; |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | bool isPlaceholderType() const; |
1963 | const BuiltinType *getAsPlaceholderType() const; |
1964 | |
1965 | |
1966 | bool isSpecificPlaceholderType(unsigned K) const; |
1967 | |
1968 | |
1969 | |
1970 | bool isNonOverloadPlaceholderType() const; |
1971 | |
1972 | |
1973 | |
1974 | bool isIntegerType() const; |
1975 | bool isEnumeralType() const; |
1976 | |
1977 | |
1978 | bool isScopedEnumeralType() const; |
1979 | bool isBooleanType() const; |
1980 | bool isCharType() const; |
1981 | bool isWideCharType() const; |
1982 | bool isChar8Type() const; |
1983 | bool isChar16Type() const; |
1984 | bool isChar32Type() const; |
1985 | bool isAnyCharacterType() const; |
1986 | bool isIntegralType(const ASTContext &Ctx) const; |
1987 | |
1988 | |
1989 | bool isIntegralOrEnumerationType() const; |
1990 | |
1991 | |
1992 | bool isIntegralOrUnscopedEnumerationType() const; |
1993 | bool isUnscopedEnumerationType() const; |
1994 | |
1995 | |
1996 | bool isRealFloatingType() const; |
1997 | |
1998 | |
1999 | bool isComplexType() const; |
2000 | bool isAnyComplexType() const; |
2001 | bool isFloatingType() const; |
2002 | bool isHalfType() const; |
2003 | bool isFloat16Type() const; |
2004 | bool isBFloat16Type() const; |
2005 | bool isFloat128Type() const; |
2006 | bool isRealType() const; |
2007 | bool isArithmeticType() const; |
2008 | bool isVoidType() const; |
2009 | bool isScalarType() const; |
2010 | bool isAggregateType() const; |
2011 | bool isFundamentalType() const; |
2012 | bool isCompoundType() const; |
2013 | |
2014 | |
2015 | |
2016 | bool isFunctionType() const; |
2017 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } |
2018 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } |
2019 | bool isPointerType() const; |
2020 | bool isAnyPointerType() const; |
2021 | bool isBlockPointerType() const; |
2022 | bool isVoidPointerType() const; |
2023 | bool isReferenceType() const; |
2024 | bool isLValueReferenceType() const; |
2025 | bool isRValueReferenceType() const; |
2026 | bool isObjectPointerType() const; |
2027 | bool isFunctionPointerType() const; |
2028 | bool isFunctionReferenceType() const; |
2029 | bool isMemberPointerType() const; |
2030 | bool isMemberFunctionPointerType() const; |
2031 | bool isMemberDataPointerType() const; |
2032 | bool isArrayType() const; |
2033 | bool isConstantArrayType() const; |
2034 | bool isIncompleteArrayType() const; |
2035 | bool isVariableArrayType() const; |
2036 | bool isDependentSizedArrayType() const; |
2037 | bool isRecordType() const; |
2038 | bool isClassType() const; |
2039 | bool isStructureType() const; |
2040 | bool isObjCBoxableRecordType() const; |
2041 | bool isInterfaceType() const; |
2042 | bool isStructureOrClassType() const; |
2043 | bool isUnionType() const; |
2044 | bool isComplexIntegerType() const; |
2045 | bool isVectorType() const; |
2046 | bool isExtVectorType() const; |
2047 | bool isMatrixType() const; |
2048 | bool isConstantMatrixType() const; |
2049 | bool isDependentAddressSpaceType() const; |
2050 | bool isObjCObjectPointerType() const; |
2051 | bool isObjCRetainableType() const; |
2052 | bool isObjCLifetimeType() const; |
2053 | bool isObjCIndirectLifetimeType() const; |
2054 | bool isObjCNSObjectType() const; |
2055 | bool isObjCIndependentClassType() const; |
2056 | |
2057 | |
2058 | bool isObjCObjectType() const; |
2059 | bool isObjCQualifiedInterfaceType() const; |
2060 | bool isObjCQualifiedIdType() const; |
2061 | bool isObjCQualifiedClassType() const; |
2062 | bool isObjCObjectOrInterfaceType() const; |
2063 | bool isObjCIdType() const; |
2064 | bool isDecltypeType() const; |
2065 | |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | bool isObjCInertUnsafeUnretainedType() const { |
2072 | return hasAttr(attr::ObjCInertUnsafeUnretained); |
2073 | } |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
2083 | const ObjCObjectType *&bound) const; |
2084 | |
2085 | bool isObjCClassType() const; |
2086 | |
2087 | |
2088 | |
2089 | |
2090 | |
2091 | |
2092 | |
2093 | bool isObjCClassOrClassKindOfType() const; |
2094 | |
2095 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; |
2096 | bool isObjCSelType() const; |
2097 | bool isObjCBuiltinType() const; |
2098 | bool isObjCARCBridgableType() const; |
2099 | bool isCARCBridgableType() const; |
2100 | bool isTemplateTypeParmType() const; |
2101 | bool isNullPtrType() const; |
2102 | bool isNothrowT() const; |
2103 | bool isAlignValT() const; |
2104 | bool isStdByteType() const; |
2105 | bool isAtomicType() const; |
2106 | bool isUndeducedAutoType() const; |
2107 | |
2108 | bool isTypedefNameType() const; |
2109 | |
2110 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2111 | bool is##Id##Type() const; |
2112 | #include "clang/Basic/OpenCLImageTypes.def" |
2113 | |
2114 | bool isImageType() const; |
2115 | |
2116 | bool isSamplerT() const; |
2117 | bool isEventT() const; |
2118 | bool isClkEventT() const; |
2119 | bool isQueueT() const; |
2120 | bool isReserveIDT() const; |
2121 | |
2122 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2123 | bool is##Id##Type() const; |
2124 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2125 | |
2126 | bool isOCLIntelSubgroupAVCType() const; |
2127 | bool isOCLExtOpaqueType() const; |
2128 | |
2129 | bool isPipeType() const; |
2130 | bool isExtIntType() const; |
2131 | bool isOpenCLSpecificType() const; |
2132 | |
2133 | |
2134 | |
2135 | |
2136 | bool isObjCARCImplicitlyUnretainedType() const; |
2137 | |
2138 | |
2139 | bool isCUDADeviceBuiltinSurfaceType() const; |
2140 | |
2141 | bool isCUDADeviceBuiltinTextureType() const; |
2142 | |
2143 | |
2144 | Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; |
2145 | |
2146 | enum ScalarTypeKind { |
2147 | STK_CPointer, |
2148 | STK_BlockPointer, |
2149 | STK_ObjCObjectPointer, |
2150 | STK_MemberPointer, |
2151 | STK_Bool, |
2152 | STK_Integral, |
2153 | STK_Floating, |
2154 | STK_IntegralComplex, |
2155 | STK_FloatingComplex, |
2156 | STK_FixedPoint |
2157 | }; |
2158 | |
2159 | |
2160 | ScalarTypeKind getScalarTypeKind() const; |
2161 | |
2162 | TypeDependence getDependence() const { |
2163 | return static_cast<TypeDependence>(TypeBits.Dependence); |
2164 | } |
2165 | |
2166 | |
2167 | bool containsErrors() const { |
2168 | return getDependence() & TypeDependence::Error; |
2169 | } |
2170 | |
2171 | |
2172 | |
2173 | bool isDependentType() const { |
2174 | return getDependence() & TypeDependence::Dependent; |
2175 | } |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | bool isInstantiationDependentType() const { |
2182 | return getDependence() & TypeDependence::Instantiation; |
2183 | } |
2184 | |
2185 | |
2186 | |
2187 | |
2188 | bool isUndeducedType() const; |
2189 | |
2190 | |
2191 | bool isVariablyModifiedType() const { |
2192 | return getDependence() & TypeDependence::VariablyModified; |
2193 | } |
2194 | |
2195 | |
2196 | |
2197 | bool hasSizedVLAType() const; |
2198 | |
2199 | |
2200 | bool hasUnnamedOrLocalType() const; |
2201 | |
2202 | bool isOverloadableType() const; |
2203 | |
2204 | |
2205 | bool isElaboratedTypeSpecifier() const; |
2206 | |
2207 | bool canDecayToPointerType() const; |
2208 | |
2209 | |
2210 | |
2211 | |
2212 | bool hasPointerRepresentation() const; |
2213 | |
2214 | |
2215 | |
2216 | bool hasObjCPointerRepresentation() const; |
2217 | |
2218 | |
2219 | |
2220 | bool hasIntegerRepresentation() const; |
2221 | |
2222 | |
2223 | |
2224 | bool hasSignedIntegerRepresentation() const; |
2225 | |
2226 | |
2227 | |
2228 | bool hasUnsignedIntegerRepresentation() const; |
2229 | |
2230 | |
2231 | |
2232 | bool hasFloatingRepresentation() const; |
2233 | |
2234 | |
2235 | |
2236 | |
2237 | const RecordType *getAsStructureType() const; |
2238 | |
2239 | const RecordType *getAsUnionType() const; |
2240 | const ComplexType *getAsComplexIntegerType() const; |
2241 | const ObjCObjectType *getAsObjCInterfaceType() const; |
2242 | |
2243 | |
2244 | |
2245 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; |
2246 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; |
2247 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; |
2248 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | CXXRecordDecl *getAsCXXRecordDecl() const; |
2254 | |
2255 | |
2256 | RecordDecl *getAsRecordDecl() const; |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | TagDecl *getAsTagDecl() const; |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; |
2269 | |
2270 | |
2271 | |
2272 | |
2273 | DeducedType *getContainedDeducedType() const; |
2274 | |
2275 | |
2276 | |
2277 | |
2278 | AutoType *getContainedAutoType() const { |
2279 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); |
2280 | } |
2281 | |
2282 | |
2283 | |
2284 | |
2285 | bool hasAutoForTrailingReturnType() const; |
2286 | |
2287 | |
2288 | |
2289 | |
2290 | |
2291 | |
2292 | |
2293 | template <typename T> const T *getAs() const; |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | template <typename T> const T *getAsAdjusted() const; |
2301 | |
2302 | |
2303 | |
2304 | const ArrayType *getAsArrayTypeUnsafe() const; |
2305 | |
2306 | |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | template <typename T> const T *castAs() const; |
2313 | |
2314 | |
2315 | |
2316 | const ArrayType *castAsArrayTypeUnsafe() const; |
2317 | |
2318 | |
2319 | |
2320 | bool hasAttr(attr::Kind AK) const; |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | const Type *getBaseElementTypeUnsafe() const; |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | const Type *getArrayElementTypeNoTypeQual() const; |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | const Type *getPointeeOrArrayElementType() const; |
2336 | |
2337 | |
2338 | |
2339 | QualType getPointeeType() const; |
2340 | |
2341 | |
2342 | |
2343 | const Type *getUnqualifiedDesugaredType() const; |
2344 | |
2345 | |
2346 | bool isPromotableIntegerType() const; |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | bool isSignedIntegerType() const; |
2352 | |
2353 | |
2354 | |
2355 | |
2356 | bool isUnsignedIntegerType() const; |
2357 | |
2358 | |
2359 | |
2360 | bool isSignedIntegerOrEnumerationType() const; |
2361 | |
2362 | |
2363 | |
2364 | bool isUnsignedIntegerOrEnumerationType() const; |
2365 | |
2366 | |
2367 | |
2368 | bool isFixedPointType() const; |
2369 | |
2370 | |
2371 | bool isFixedPointOrIntegerType() const; |
2372 | |
2373 | |
2374 | |
2375 | bool isSaturatedFixedPointType() const; |
2376 | |
2377 | |
2378 | |
2379 | bool isUnsaturatedFixedPointType() const; |
2380 | |
2381 | |
2382 | |
2383 | bool isSignedFixedPointType() const; |
2384 | |
2385 | |
2386 | |
2387 | bool isUnsignedFixedPointType() const; |
2388 | |
2389 | |
2390 | |
2391 | |
2392 | bool isConstantSizeType() const; |
2393 | |
2394 | |
2395 | |
2396 | bool isSpecifierType() const; |
2397 | |
2398 | |
2399 | Linkage getLinkage() const; |
2400 | |
2401 | |
2402 | Visibility getVisibility() const { |
2403 | return getLinkageAndVisibility().getVisibility(); |
2404 | } |
2405 | |
2406 | |
2407 | bool isVisibilityExplicit() const { |
2408 | return getLinkageAndVisibility().isVisibilityExplicit(); |
2409 | } |
2410 | |
2411 | |
2412 | LinkageInfo getLinkageAndVisibility() const; |
2413 | |
2414 | |
2415 | |
2416 | bool isLinkageValid() const; |
2417 | |
2418 | |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | |
2430 | bool canHaveNullability(bool ResultIfUnknown = true) const; |
2431 | |
2432 | |
2433 | |
2434 | |
2435 | |
2436 | |
2437 | |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | |
2447 | Optional<ArrayRef<QualType>> |
2448 | getObjCSubstitutions(const DeclContext *dc) const; |
2449 | |
2450 | |
2451 | |
2452 | bool acceptsObjCTypeParams() const; |
2453 | |
2454 | const char *getTypeClassName() const; |
2455 | |
2456 | QualType getCanonicalTypeInternal() const { |
2457 | return CanonicalType; |
2458 | } |
2459 | |
2460 | CanQualType getCanonicalTypeUnqualified() const; |
2461 | void dump() const; |
2462 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
2463 | }; |
2464 | |
2465 | |
2466 | |
2467 | template <> const TypedefType *Type::getAs() const; |
2468 | |
2469 | |
2470 | |
2471 | |
2472 | template <> const TemplateSpecializationType *Type::getAs() const; |
2473 | |
2474 | |
2475 | |
2476 | template <> const AttributedType *Type::getAs() const; |
2477 | |
2478 | |
2479 | |
2480 | #define TYPE(Class, Base) |
2481 | #define LEAF_TYPE(Class) \ |
2482 | template <> inline const Class##Type *Type::getAs() const { \ |
2483 | return dyn_cast<Class##Type>(CanonicalType); \ |
2484 | } \ |
2485 | template <> inline const Class##Type *Type::castAs() const { \ |
2486 | return cast<Class##Type>(CanonicalType); \ |
2487 | } |
2488 | #include "clang/AST/TypeNodes.inc" |
2489 | |
2490 | |
2491 | |
2492 | class BuiltinType : public Type { |
2493 | public: |
2494 | enum Kind { |
2495 | |
2496 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, |
2497 | #include "clang/Basic/OpenCLImageTypes.def" |
2498 | |
2499 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, |
2500 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2501 | |
2502 | #define SVE_TYPE(Name, Id, SingletonId) Id, |
2503 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2504 | |
2505 | #define PPC_VECTOR_TYPE(Name, Id, Size) Id, |
2506 | #include "clang/Basic/PPCTypes.def" |
2507 | |
2508 | #define RVV_TYPE(Name, Id, SingletonId) Id, |
2509 | #include "clang/Basic/RISCVVTypes.def" |
2510 | |
2511 | #define BUILTIN_TYPE(Id, SingletonId) Id, |
2512 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id |
2513 | #include "clang/AST/BuiltinTypes.def" |
2514 | }; |
2515 | |
2516 | private: |
2517 | friend class ASTContext; |
2518 | |
2519 | BuiltinType(Kind K) |
2520 | : Type(Builtin, QualType(), |
2521 | K == Dependent ? TypeDependence::DependentInstantiation |
2522 | : TypeDependence::None) { |
2523 | BuiltinTypeBits.Kind = K; |
2524 | } |
2525 | |
2526 | public: |
2527 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } |
2528 | StringRef getName(const PrintingPolicy &Policy) const; |
2529 | |
2530 | const char *getNameAsCString(const PrintingPolicy &Policy) const { |
2531 | |
2532 | StringRef str = getName(Policy); |
2533 | assert(!str.empty() && str.data()[str.size()] == '\0'); |
2534 | return str.data(); |
2535 | } |
2536 | |
2537 | bool isSugared() const { return false; } |
2538 | QualType desugar() const { return QualType(this, 0); } |
2539 | |
2540 | bool isInteger() const { |
2541 | return getKind() >= Bool && getKind() <= Int128; |
2542 | } |
2543 | |
2544 | bool isSignedInteger() const { |
2545 | return getKind() >= Char_S && getKind() <= Int128; |
2546 | } |
2547 | |
2548 | bool isUnsignedInteger() const { |
2549 | return getKind() >= Bool && getKind() <= UInt128; |
2550 | } |
2551 | |
2552 | bool isFloatingPoint() const { |
2553 | return getKind() >= Half && getKind() <= Float128; |
2554 | } |
2555 | |
2556 | |
2557 | static bool isPlaceholderTypeKind(Kind K) { |
2558 | return K >= Overload; |
2559 | } |
2560 | |
2561 | |
2562 | |
2563 | |
2564 | bool isPlaceholderType() const { |
2565 | return isPlaceholderTypeKind(getKind()); |
2566 | } |
2567 | |
2568 | |
2569 | |
2570 | |
2571 | |
2572 | |
2573 | |
2574 | |
2575 | |
2576 | |
2577 | bool isNonOverloadPlaceholderType() const { |
2578 | return getKind() > Overload; |
2579 | } |
2580 | |
2581 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } |
2582 | }; |
2583 | |
2584 | |
2585 | |
2586 | class ComplexType : public Type, public llvm::FoldingSetNode { |
2587 | friend class ASTContext; |
2588 | |
2589 | QualType ElementType; |
2590 | |
2591 | ComplexType(QualType Element, QualType CanonicalPtr) |
2592 | : Type(Complex, CanonicalPtr, Element->getDependence()), |
2593 | ElementType(Element) {} |
2594 | |
2595 | public: |
2596 | QualType getElementType() const { return ElementType; } |
2597 | |
2598 | bool isSugared() const { return false; } |
2599 | QualType desugar() const { return QualType(this, 0); } |
2600 | |
2601 | void Profile(llvm::FoldingSetNodeID &ID) { |
2602 | Profile(ID, getElementType()); |
2603 | } |
2604 | |
2605 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { |
2606 | ID.AddPointer(Element.getAsOpaquePtr()); |
2607 | } |
2608 | |
2609 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } |
2610 | }; |
2611 | |
2612 | |
2613 | class ParenType : public Type, public llvm::FoldingSetNode { |
2614 | friend class ASTContext; |
2615 | |
2616 | QualType Inner; |
2617 | |
2618 | ParenType(QualType InnerType, QualType CanonType) |
2619 | : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} |
2620 | |
2621 | public: |
2622 | QualType getInnerType() const { return Inner; } |
2623 | |
2624 | bool isSugared() const { return true; } |
2625 | QualType desugar() const { return getInnerType(); } |
2626 | |
2627 | void Profile(llvm::FoldingSetNodeID &ID) { |
2628 | Profile(ID, getInnerType()); |
2629 | } |
2630 | |
2631 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { |
2632 | Inner.Profile(ID); |
2633 | } |
2634 | |
2635 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } |
2636 | }; |
2637 | |
2638 | |
2639 | class PointerType : public Type, public llvm::FoldingSetNode { |
2640 | friend class ASTContext; |
2641 | |
2642 | QualType PointeeType; |
2643 | |
2644 | PointerType(QualType Pointee, QualType CanonicalPtr) |
2645 | : Type(Pointer, CanonicalPtr, Pointee->getDependence()), |
2646 | PointeeType(Pointee) {} |
2647 | |
2648 | public: |
2649 | QualType getPointeeType() const { return PointeeType; } |
2650 | |
2651 | bool isSugared() const { return false; } |
2652 | QualType desugar() const { return QualType(this, 0); } |
2653 | |
2654 | void Profile(llvm::FoldingSetNodeID &ID) { |
2655 | Profile(ID, getPointeeType()); |
2656 | } |
2657 | |
2658 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2659 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2660 | } |
2661 | |
2662 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } |
2663 | }; |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | class AdjustedType : public Type, public llvm::FoldingSetNode { |
2669 | QualType OriginalTy; |
2670 | QualType AdjustedTy; |
2671 | |
2672 | protected: |
2673 | friend class ASTContext; |
2674 | |
2675 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, |
2676 | QualType CanonicalPtr) |
2677 | : Type(TC, CanonicalPtr, OriginalTy->getDependence()), |
2678 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} |
2679 | |
2680 | public: |
2681 | QualType getOriginalType() const { return OriginalTy; } |
2682 | QualType getAdjustedType() const { return AdjustedTy; } |
2683 | |
2684 | bool isSugared() const { return true; } |
2685 | QualType desugar() const { return AdjustedTy; } |
2686 | |
2687 | void Profile(llvm::FoldingSetNodeID &ID) { |
2688 | Profile(ID, OriginalTy, AdjustedTy); |
2689 | } |
2690 | |
2691 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { |
2692 | ID.AddPointer(Orig.getAsOpaquePtr()); |
2693 | ID.AddPointer(New.getAsOpaquePtr()); |
2694 | } |
2695 | |
2696 | static bool classof(const Type *T) { |
2697 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; |
2698 | } |
2699 | }; |
2700 | |
2701 | |
2702 | class DecayedType : public AdjustedType { |
2703 | friend class ASTContext; |
2704 | |
2705 | inline |
2706 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); |
2707 | |
2708 | public: |
2709 | QualType getDecayedType() const { return getAdjustedType(); } |
2710 | |
2711 | inline QualType getPointeeType() const; |
2712 | |
2713 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } |
2714 | }; |
2715 | |
2716 | |
2717 | |
2718 | |
2719 | class BlockPointerType : public Type, public llvm::FoldingSetNode { |
2720 | friend class ASTContext; |
2721 | |
2722 | |
2723 | QualType PointeeType; |
2724 | |
2725 | BlockPointerType(QualType Pointee, QualType CanonicalCls) |
2726 | : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), |
2727 | PointeeType(Pointee) {} |
2728 | |
2729 | public: |
2730 | |
2731 | QualType getPointeeType() const { return PointeeType; } |
2732 | |
2733 | bool isSugared() const { return false; } |
2734 | QualType desugar() const { return QualType(this, 0); } |
2735 | |
2736 | void Profile(llvm::FoldingSetNodeID &ID) { |
2737 | Profile(ID, getPointeeType()); |
2738 | } |
2739 | |
2740 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2741 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2742 | } |
2743 | |
2744 | static bool classof(const Type *T) { |
2745 | return T->getTypeClass() == BlockPointer; |
2746 | } |
2747 | }; |
2748 | |
2749 | |
2750 | class ReferenceType : public Type, public llvm::FoldingSetNode { |
2751 | QualType PointeeType; |
2752 | |
2753 | protected: |
2754 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, |
2755 | bool SpelledAsLValue) |
2756 | : Type(tc, CanonicalRef, Referencee->getDependence()), |
2757 | PointeeType(Referencee) { |
2758 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; |
2759 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); |
2760 | } |
2761 | |
2762 | public: |
2763 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } |
2764 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } |
2765 | |
2766 | QualType getPointeeTypeAsWritten() const { return PointeeType; } |
2767 | |
2768 | QualType getPointeeType() const { |
2769 | |
2770 | const ReferenceType *T = this; |
2771 | while (T->isInnerRef()) |
2772 | T = T->PointeeType->castAs<ReferenceType>(); |
2773 | return T->PointeeType; |
2774 | } |
2775 | |
2776 | void Profile(llvm::FoldingSetNodeID &ID) { |
2777 | Profile(ID, PointeeType, isSpelledAsLValue()); |
2778 | } |
2779 | |
2780 | static void Profile(llvm::FoldingSetNodeID &ID, |
2781 | QualType Referencee, |
2782 | bool SpelledAsLValue) { |
2783 | ID.AddPointer(Referencee.getAsOpaquePtr()); |
2784 | ID.AddBoolean(SpelledAsLValue); |
2785 | } |
2786 | |
2787 | static bool classof(const Type *T) { |
2788 | return T->getTypeClass() == LValueReference || |
2789 | T->getTypeClass() == RValueReference; |
2790 | } |
2791 | }; |
2792 | |
2793 | |
2794 | class LValueReferenceType : public ReferenceType { |
2795 | friend class ASTContext; |
2796 | |
2797 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, |
2798 | bool SpelledAsLValue) |
2799 | : ReferenceType(LValueReference, Referencee, CanonicalRef, |
2800 | SpelledAsLValue) {} |
2801 | |
2802 | public: |
2803 | bool isSugared() const { return false; } |
2804 | QualType desugar() const { return QualType(this, 0); } |
2805 | |
2806 | static bool classof(const Type *T) { |
2807 | return T->getTypeClass() == LValueReference; |
2808 | } |
2809 | }; |
2810 | |
2811 | |
2812 | class RValueReferenceType : public ReferenceType { |
2813 | friend class ASTContext; |
2814 | |
2815 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) |
2816 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} |
2817 | |
2818 | public: |
2819 | bool isSugared() const { return false; } |
2820 | QualType desugar() const { return QualType(this, 0); } |
2821 | |
2822 | static bool classof(const Type *T) { |
2823 | return T->getTypeClass() == RValueReference; |
2824 | } |
2825 | }; |
2826 | |
2827 | |
2828 | |
2829 | |
2830 | class MemberPointerType : public Type, public llvm::FoldingSetNode { |
2831 | friend class ASTContext; |
2832 | |
2833 | QualType PointeeType; |
2834 | |
2835 | |
2836 | |
2837 | const Type *Class; |
2838 | |
2839 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) |
2840 | : Type(MemberPointer, CanonicalPtr, |
2841 | (Cls->getDependence() & ~TypeDependence::VariablyModified) | |
2842 | Pointee->getDependence()), |
2843 | PointeeType(Pointee), Class(Cls) {} |
2844 | |
2845 | public: |
2846 | QualType getPointeeType() const { return PointeeType; } |
2847 | |
2848 | |
2849 | |
2850 | bool isMemberFunctionPointer() const { |
2851 | return PointeeType->isFunctionProtoType(); |
2852 | } |
2853 | |
2854 | |
2855 | |
2856 | bool isMemberDataPointer() const { |
2857 | return !PointeeType->isFunctionProtoType(); |
2858 | } |
2859 | |
2860 | const Type *getClass() const { return Class; } |
2861 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; |
2862 | |
2863 | bool isSugared() const { return false; } |
2864 | QualType desugar() const { return QualType(this, 0); } |
2865 | |
2866 | void Profile(llvm::FoldingSetNodeID &ID) { |
2867 | Profile(ID, getPointeeType(), getClass()); |
2868 | } |
2869 | |
2870 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
2871 | const Type *Class) { |
2872 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2873 | ID.AddPointer(Class); |
2874 | } |
2875 | |
2876 | static bool classof(const Type *T) { |
2877 | return T->getTypeClass() == MemberPointer; |
2878 | } |
2879 | }; |
2880 | |
2881 | |
2882 | class ArrayType : public Type, public llvm::FoldingSetNode { |
2883 | public: |
2884 | |
2885 | |
2886 | |
2887 | |
2888 | enum ArraySizeModifier { |
2889 | Normal, Static, Star |
2890 | }; |
2891 | |
2892 | private: |
2893 | |
2894 | QualType ElementType; |
2895 | |
2896 | protected: |
2897 | friend class ASTContext; |
2898 | |
2899 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, |
2900 | unsigned tq, const Expr *sz = nullptr); |
2901 | |
2902 | public: |
2903 | QualType getElementType() const { return ElementType; } |
2904 | |
2905 | ArraySizeModifier getSizeModifier() const { |
2906 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); |
2907 | } |
2908 | |
2909 | Qualifiers getIndexTypeQualifiers() const { |
2910 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); |
2911 | } |
2912 | |
2913 | unsigned getIndexTypeCVRQualifiers() const { |
2914 | return ArrayTypeBits.IndexTypeQuals; |
2915 | } |
2916 | |
2917 | static bool classof(const Type *T) { |
2918 | return T->getTypeClass() == ConstantArray || |
2919 | T->getTypeClass() == VariableArray || |
2920 | T->getTypeClass() == IncompleteArray || |
2921 | T->getTypeClass() == DependentSizedArray; |
2922 | } |
2923 | }; |
2924 | |
2925 | |
2926 | |
2927 | |
2928 | class ConstantArrayType final |
2929 | : public ArrayType, |
2930 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { |
2931 | friend class ASTContext; |
2932 | friend TrailingObjects; |
2933 | |
2934 | llvm::APInt Size; |
2935 | |
2936 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, |
2937 | const Expr *sz, ArraySizeModifier sm, unsigned tq) |
2938 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { |
2939 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; |
2940 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { |
2941 | assert(!can.isNull() && "canonical constant array should not have size"); |
2942 | *getTrailingObjects<const Expr*>() = sz; |
2943 | } |
2944 | } |
2945 | |
2946 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { |
2947 | return ConstantArrayTypeBits.HasStoredSizeExpr; |
2948 | } |
2949 | |
2950 | public: |
2951 | const llvm::APInt &getSize() const { return Size; } |
2952 | const Expr *getSizeExpr() const { |
2953 | return ConstantArrayTypeBits.HasStoredSizeExpr |
2954 | ? *getTrailingObjects<const Expr *>() |
2955 | : nullptr; |
2956 | } |
2957 | bool isSugared() const { return false; } |
2958 | QualType desugar() const { return QualType(this, 0); } |
2959 | |
2960 | |
2961 | |
2962 | static unsigned getNumAddressingBits(const ASTContext &Context, |
2963 | QualType ElementType, |
2964 | const llvm::APInt &NumElements); |
2965 | |
2966 | |
2967 | |
2968 | static unsigned getMaxSizeBits(const ASTContext &Context); |
2969 | |
2970 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
2971 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), |
2972 | getSizeModifier(), getIndexTypeCVRQualifiers()); |
2973 | } |
2974 | |
2975 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, |
2976 | QualType ET, const llvm::APInt &ArraySize, |
2977 | const Expr *SizeExpr, ArraySizeModifier SizeMod, |
2978 | unsigned TypeQuals); |
2979 | |
2980 | static bool classof(const Type *T) { |
2981 | return T->getTypeClass() == ConstantArray; |
2982 | } |
2983 | }; |
2984 | |
2985 | |
2986 | |
2987 | |
2988 | class IncompleteArrayType : public ArrayType { |
2989 | friend class ASTContext; |
2990 | |
2991 | IncompleteArrayType(QualType et, QualType can, |
2992 | ArraySizeModifier sm, unsigned tq) |
2993 | : ArrayType(IncompleteArray, et, can, sm, tq) {} |
2994 | |
2995 | public: |
2996 | friend class StmtIteratorBase; |
2997 | |
2998 | bool isSugared() const { return false; } |
2999 | QualType desugar() const { return QualType(this, 0); } |
3000 | |
3001 | static bool classof(const Type *T) { |
3002 | return T->getTypeClass() == IncompleteArray; |
3003 | } |
3004 | |
3005 | void Profile(llvm::FoldingSetNodeID &ID) { |
3006 | Profile(ID, getElementType(), getSizeModifier(), |
3007 | getIndexTypeCVRQualifiers()); |
3008 | } |
3009 | |
3010 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, |
3011 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
3012 | ID.AddPointer(ET.getAsOpaquePtr()); |
3013 | ID.AddInteger(SizeMod); |
3014 | ID.AddInteger(TypeQuals); |
3015 | } |
3016 | }; |
3017 | |
3018 | |
3019 | |
3020 | |
3021 | |
3022 | |
3023 | |
3024 | |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | class VariableArrayType : public ArrayType { |
3033 | friend class ASTContext; |
3034 | |
3035 | |
3036 | |
3037 | Stmt *SizeExpr; |
3038 | |
3039 | |
3040 | SourceRange Brackets; |
3041 | |
3042 | VariableArrayType(QualType et, QualType can, Expr *e, |
3043 | ArraySizeModifier sm, unsigned tq, |
3044 | SourceRange brackets) |
3045 | : ArrayType(VariableArray, et, can, sm, tq, e), |
3046 | SizeExpr((Stmt*) e), Brackets(brackets) {} |
3047 | |
3048 | public: |
3049 | friend class StmtIteratorBase; |
3050 | |
3051 | Expr *getSizeExpr() const { |
3052 | |
3053 | |
3054 | return (Expr*) SizeExpr; |
3055 | } |
3056 | |
3057 | SourceRange getBracketsRange() const { return Brackets; } |
3058 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3059 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3060 | |
3061 | bool isSugared() const { return false; } |
3062 | QualType desugar() const { return QualType(this, 0); } |
3063 | |
3064 | static bool classof(const Type *T) { |
3065 | return T->getTypeClass() == VariableArray; |
3066 | } |
3067 | |
3068 | void Profile(llvm::FoldingSetNodeID &ID) { |
3069 | llvm_unreachable("Cannot unique VariableArrayTypes."); |
3070 | } |
3071 | }; |
3072 | |
3073 | |
3074 | |
3075 | |
3076 | |
3077 | |
3078 | |
3079 | |
3080 | |
3081 | |
3082 | |
3083 | |
3084 | |
3085 | |
3086 | class DependentSizedArrayType : public ArrayType { |
3087 | friend class ASTContext; |
3088 | |
3089 | const ASTContext &Context; |
3090 | |
3091 | |
3092 | |
3093 | |
3094 | |
3095 | |
3096 | Stmt *SizeExpr; |
3097 | |
3098 | |
3099 | SourceRange Brackets; |
3100 | |
3101 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, |
3102 | Expr *e, ArraySizeModifier sm, unsigned tq, |
3103 | SourceRange brackets); |
3104 | |
3105 | public: |
3106 | friend class StmtIteratorBase; |
3107 | |
3108 | Expr *getSizeExpr() const { |
3109 | |
3110 | |
3111 | return (Expr*) SizeExpr; |
3112 | } |
3113 | |
3114 | SourceRange getBracketsRange() const { return Brackets; } |
3115 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3116 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3117 | |
3118 | bool isSugared() const { return false; } |
3119 | QualType desugar() const { return QualType(this, 0); } |
3120 | |
3121 | static bool classof(const Type *T) { |
3122 | return T->getTypeClass() == DependentSizedArray; |
3123 | } |
3124 | |
3125 | void Profile(llvm::FoldingSetNodeID &ID) { |
3126 | Profile(ID, Context, getElementType(), |
3127 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); |
3128 | } |
3129 | |
3130 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3131 | QualType ET, ArraySizeModifier SizeMod, |
3132 | unsigned TypeQuals, Expr *E); |
3133 | }; |
3134 | |
3135 | |
3136 | |
3137 | |
3138 | |
3139 | |
3140 | |
3141 | |
3142 | |
3143 | |
3144 | |
3145 | |
3146 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { |
3147 | friend class ASTContext; |
3148 | |
3149 | const ASTContext &Context; |
3150 | Expr *AddrSpaceExpr; |
3151 | QualType PointeeType; |
3152 | SourceLocation loc; |
3153 | |
3154 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, |
3155 | QualType can, Expr *AddrSpaceExpr, |
3156 | SourceLocation loc); |
3157 | |
3158 | public: |
3159 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } |
3160 | QualType getPointeeType() const { return PointeeType; } |
3161 | SourceLocation getAttributeLoc() const { return loc; } |
3162 | |
3163 | bool isSugared() const { return false; } |
3164 | QualType desugar() const { return QualType(this, 0); } |
3165 | |
3166 | static bool classof(const Type *T) { |
3167 | return T->getTypeClass() == DependentAddressSpace; |
3168 | } |
3169 | |
3170 | void Profile(llvm::FoldingSetNodeID &ID) { |
3171 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); |
3172 | } |
3173 | |
3174 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3175 | QualType PointeeType, Expr *AddrSpaceExpr); |
3176 | }; |
3177 | |
3178 | |
3179 | |
3180 | |
3181 | |
3182 | |
3183 | |
3184 | |
3185 | |
3186 | |
3187 | |
3188 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { |
3189 | friend class ASTContext; |
3190 | |
3191 | const ASTContext &Context; |
3192 | Expr *SizeExpr; |
3193 | |
3194 | |
3195 | QualType ElementType; |
3196 | |
3197 | SourceLocation loc; |
3198 | |
3199 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, |
3200 | QualType can, Expr *SizeExpr, SourceLocation loc); |
3201 | |
3202 | public: |
3203 | Expr *getSizeExpr() const { return SizeExpr; } |
3204 | QualType getElementType() const { return ElementType; } |
3205 | SourceLocation getAttributeLoc() const { return loc; } |
3206 | |
3207 | bool isSugared() const { return false; } |
3208 | QualType desugar() const { return QualType(this, 0); } |
3209 | |
3210 | static bool classof(const Type *T) { |
3211 | return T->getTypeClass() == DependentSizedExtVector; |
3212 | } |
3213 | |
3214 | void Profile(llvm::FoldingSetNodeID &ID) { |
3215 | Profile(ID, Context, getElementType(), getSizeExpr()); |
3216 | } |
3217 | |
3218 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3219 | QualType ElementType, Expr *SizeExpr); |
3220 | }; |
3221 | |
3222 | |
3223 | |
3224 | |
3225 | |
3226 | |
3227 | |
3228 | class VectorType : public Type, public llvm::FoldingSetNode { |
3229 | public: |
3230 | enum VectorKind { |
3231 | |
3232 | GenericVector, |
3233 | |
3234 | |
3235 | AltiVecVector, |
3236 | |
3237 | |
3238 | AltiVecPixel, |
3239 | |
3240 | |
3241 | AltiVecBool, |
3242 | |
3243 | |
3244 | NeonVector, |
3245 | |
3246 | |
3247 | NeonPolyVector, |
3248 | |
3249 | |
3250 | SveFixedLengthDataVector, |
3251 | |
3252 | |
3253 | SveFixedLengthPredicateVector |
3254 | }; |
3255 | |
3256 | protected: |
3257 | friend class ASTContext; |
3258 | |
3259 | |
3260 | QualType ElementType; |
3261 | |
3262 | VectorType(QualType vecType, unsigned nElements, QualType canonType, |
3263 | VectorKind vecKind); |
3264 | |
3265 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
3266 | QualType canonType, VectorKind vecKind); |
3267 | |
3268 | public: |
3269 | QualType getElementType() const { return ElementType; } |
3270 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } |
3271 | |
3272 | bool isSugared() const { return false; } |
3273 | QualType desugar() const { return QualType(this, 0); } |
3274 | |
3275 | VectorKind getVectorKind() const { |
3276 | return VectorKind(VectorTypeBits.VecKind); |
3277 | } |
3278 | |
3279 | void Profile(llvm::FoldingSetNodeID &ID) { |
3280 | Profile(ID, getElementType(), getNumElements(), |
3281 | getTypeClass(), getVectorKind()); |
3282 | } |
3283 | |
3284 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3285 | unsigned NumElements, TypeClass TypeClass, |
3286 | VectorKind VecKind) { |
3287 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3288 | ID.AddInteger(NumElements); |
3289 | ID.AddInteger(TypeClass); |
3290 | ID.AddInteger(VecKind); |
3291 | } |
3292 | |
3293 | static bool classof(const Type *T) { |
3294 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; |
3295 | } |
3296 | }; |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 | |
3303 | |
3304 | |
3305 | |
3306 | |
3307 | class DependentVectorType : public Type, public llvm::FoldingSetNode { |
3308 | friend class ASTContext; |
3309 | |
3310 | const ASTContext &Context; |
3311 | QualType ElementType; |
3312 | Expr *SizeExpr; |
3313 | SourceLocation Loc; |
3314 | |
3315 | DependentVectorType(const ASTContext &Context, QualType ElementType, |
3316 | QualType CanonType, Expr *SizeExpr, |
3317 | SourceLocation Loc, VectorType::VectorKind vecKind); |
3318 | |
3319 | public: |
3320 | Expr *getSizeExpr() const { return SizeExpr; } |
3321 | QualType getElementType() const { return ElementType; } |
3322 | SourceLocation getAttributeLoc() const { return Loc; } |
3323 | VectorType::VectorKind getVectorKind() const { |
3324 | return VectorType::VectorKind(VectorTypeBits.VecKind); |
3325 | } |
3326 | |
3327 | bool isSugared() const { return false; } |
3328 | QualType desugar() const { return QualType(this, 0); } |
3329 | |
3330 | static bool classof(const Type *T) { |
3331 | return T->getTypeClass() == DependentVector; |
3332 | } |
3333 | |
3334 | void Profile(llvm::FoldingSetNodeID &ID) { |
3335 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); |
3336 | } |
3337 | |
3338 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3339 | QualType ElementType, const Expr *SizeExpr, |
3340 | VectorType::VectorKind VecKind); |
3341 | }; |
3342 | |
3343 | |
3344 | |
3345 | |
3346 | |
3347 | |
3348 | |
3349 | class ExtVectorType : public VectorType { |
3350 | friend class ASTContext; |
3351 | |
3352 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) |
3353 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} |
3354 | |
3355 | public: |
3356 | static int getPointAccessorIdx(char c) { |
3357 | switch (c) { |
3358 | default: return -1; |
3359 | case 'x': case 'r': return 0; |
3360 | case 'y': case 'g': return 1; |
3361 | case 'z': case 'b': return 2; |
3362 | case 'w': case 'a': return 3; |
3363 | } |
3364 | } |
3365 | |
3366 | static int getNumericAccessorIdx(char c) { |
3367 | switch (c) { |
3368 | default: return -1; |
3369 | case '0': return 0; |
3370 | case '1': return 1; |
3371 | case '2': return 2; |
3372 | case '3': return 3; |
3373 | case '4': return 4; |
3374 | case '5': return 5; |
3375 | case '6': return 6; |
3376 | case '7': return 7; |
3377 | case '8': return 8; |
3378 | case '9': return 9; |
3379 | case 'A': |
3380 | case 'a': return 10; |
3381 | case 'B': |
3382 | case 'b': return 11; |
3383 | case 'C': |
3384 | case 'c': return 12; |
3385 | case 'D': |
3386 | case 'd': return 13; |
3387 | case 'E': |
3388 | case 'e': return 14; |
3389 | case 'F': |
3390 | case 'f': return 15; |
3391 | } |
3392 | } |
3393 | |
3394 | static int getAccessorIdx(char c, bool isNumericAccessor) { |
3395 | if (isNumericAccessor) |
3396 | return getNumericAccessorIdx(c); |
3397 | else |
3398 | return getPointAccessorIdx(c); |
3399 | } |
3400 | |
3401 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { |
3402 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) |
3403 | return unsigned(idx-1) < getNumElements(); |
3404 | return false; |
3405 | } |
3406 | |
3407 | bool isSugared() const { return false; } |
3408 | QualType desugar() const { return QualType(this, 0); } |
3409 | |
3410 | static bool classof(const Type *T) { |
3411 | return T->getTypeClass() == ExtVector; |
3412 | } |
3413 | }; |
3414 | |
3415 | |
3416 | |
3417 | |
3418 | class MatrixType : public Type, public llvm::FoldingSetNode { |
3419 | protected: |
3420 | friend class ASTContext; |
3421 | |
3422 | |
3423 | QualType ElementType; |
3424 | |
3425 | MatrixType(QualType ElementTy, QualType CanonElementTy); |
3426 | |
3427 | MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, |
3428 | const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); |
3429 | |
3430 | public: |
3431 | |
3432 | QualType getElementType() const { return ElementType; } |
3433 | |
3434 | |
3435 | |
3436 | |
3437 | |
3438 | |
3439 | static bool isValidElementType(QualType T) { |
3440 | return T->isDependentType() || |
3441 | (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); |
3442 | } |
3443 | |
3444 | bool isSugared() const { return false; } |
3445 | QualType desugar() const { return QualType(this, 0); } |
3446 | |
3447 | static bool classof(const Type *T) { |
3448 | return T->getTypeClass() == ConstantMatrix || |
3449 | T->getTypeClass() == DependentSizedMatrix; |
3450 | } |
3451 | }; |
3452 | |
3453 | |
3454 | class ConstantMatrixType final : public MatrixType { |
3455 | protected: |
3456 | friend class ASTContext; |
3457 | |
3458 | |
3459 | unsigned NumRows; |
3460 | unsigned NumColumns; |
3461 | |
3462 | static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; |
3463 | |
3464 | ConstantMatrixType(QualType MatrixElementType, unsigned NRows, |
3465 | unsigned NColumns, QualType CanonElementType); |
3466 | |
3467 | ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, |
3468 | unsigned NColumns, QualType CanonElementType); |
3469 | |
3470 | public: |
3471 | |
3472 | unsigned getNumRows() const { return NumRows; } |
3473 | |
3474 | |
3475 | unsigned getNumColumns() const { return NumColumns; } |
3476 | |
3477 | |
3478 | unsigned getNumElementsFlattened() const { |
3479 | return getNumRows() * getNumColumns(); |
3480 | } |
3481 | |
3482 | |
3483 | static constexpr bool isDimensionValid(size_t NumElements) { |
3484 | return NumElements > 0 && NumElements <= MaxElementsPerDimension; |
3485 | } |
3486 | |
3487 | |
3488 | static constexpr unsigned getMaxElementsPerDimension() { |
3489 | return MaxElementsPerDimension; |
3490 | } |
3491 | |
3492 | void Profile(llvm::FoldingSetNodeID &ID) { |
3493 | Profile(ID, getElementType(), getNumRows(), getNumColumns(), |
3494 | getTypeClass()); |
3495 | } |
3496 | |
3497 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3498 | unsigned NumRows, unsigned NumColumns, |
3499 | TypeClass TypeClass) { |
3500 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3501 | ID.AddInteger(NumRows); |
3502 | ID.AddInteger(NumColumns); |
3503 | ID.AddInteger(TypeClass); |
3504 | } |
3505 | |
3506 | static bool classof(const Type *T) { |
3507 | return T->getTypeClass() == ConstantMatrix; |
3508 | } |
3509 | }; |
3510 | |
3511 | |
3512 | |
3513 | class DependentSizedMatrixType final : public MatrixType { |
3514 | friend class ASTContext; |
3515 | |
3516 | const ASTContext &Context; |
3517 | Expr *RowExpr; |
3518 | Expr *ColumnExpr; |
3519 | |
3520 | SourceLocation loc; |
3521 | |
3522 | DependentSizedMatrixType(const ASTContext &Context, QualType ElementType, |
3523 | QualType CanonicalType, Expr *RowExpr, |
3524 | Expr *ColumnExpr, SourceLocation loc); |
3525 | |
3526 | public: |
3527 | Expr *getRowExpr() const { return RowExpr; } |
3528 | Expr *getColumnExpr() const { return ColumnExpr; } |
3529 | SourceLocation getAttributeLoc() const { return loc; } |
3530 | |
3531 | static bool classof(const Type *T) { |
3532 | return T->getTypeClass() == DependentSizedMatrix; |
3533 | } |
3534 | |
3535 | void Profile(llvm::FoldingSetNodeID &ID) { |
3536 | Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); |
3537 | } |
3538 | |
3539 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3540 | QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); |
3541 | }; |
3542 | |
3543 | |
3544 | |
3545 | class FunctionType : public Type { |
3546 | |
3547 | QualType ResultType; |
3548 | |
3549 | public: |
3550 | |
3551 | |
3552 | |
3553 | |
3554 | |
3555 | |
3556 | |
3557 | |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | |
3567 | |
3568 | |
3569 | |
3570 | |
3571 | class ExtParameterInfo { |
3572 | enum { |
3573 | ABIMask = 0x0F, |
3574 | IsConsumed = 0x10, |
3575 | HasPassObjSize = 0x20, |
3576 | IsNoEscape = 0x40, |
3577 | }; |
3578 | unsigned char Data = 0; |
3579 | |
3580 | public: |
3581 | ExtParameterInfo() = default; |
3582 | |
3583 | |
3584 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } |
3585 | ExtParameterInfo withABI(ParameterABI kind) const { |
3586 | ExtParameterInfo copy = *this; |
3587 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); |
3588 | return copy; |
3589 | } |
3590 | |
3591 | |
3592 | |
3593 | bool isConsumed() const { return (Data & IsConsumed); } |
3594 | ExtParameterInfo withIsConsumed(bool consumed) const { |
3595 | ExtParameterInfo copy = *this; |
3596 | if (consumed) |
3597 | copy.Data |= IsConsumed; |
3598 | else |
3599 | copy.Data &= ~IsConsumed; |
3600 | return copy; |
3601 | } |
3602 | |
3603 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } |
3604 | ExtParameterInfo withHasPassObjectSize() const { |
3605 | ExtParameterInfo Copy = *this; |
3606 | Copy.Data |= HasPassObjSize; |
3607 | return Copy; |
3608 | } |
3609 | |
3610 | bool isNoEscape() const { return Data & IsNoEscape; } |
3611 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { |
3612 | ExtParameterInfo Copy = *this; |
3613 | if (NoEscape) |
3614 | Copy.Data |= IsNoEscape; |
3615 | else |
3616 | Copy.Data &= ~IsNoEscape; |
3617 | return Copy; |
3618 | } |
3619 | |
3620 | unsigned char getOpaqueValue() const { return Data; } |
3621 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { |
3622 | ExtParameterInfo result; |
3623 | result.Data = data; |
3624 | return result; |
3625 | } |
3626 | |
3627 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3628 | return lhs.Data == rhs.Data; |
3629 | } |
3630 | |
3631 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3632 | return lhs.Data != rhs.Data; |
3633 | } |
3634 | }; |
3635 | |
3636 | |
3637 | |
3638 | |
3639 | |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | |
3646 | |
3647 | |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | |
3654 | |
3655 | |
3656 | class ExtInfo { |
3657 | friend class FunctionType; |
3658 | |
3659 | |
3660 | |
3661 | |
3662 | |
3663 | |
3664 | |
3665 | |
3666 | |
3667 | enum { CallConvMask = 0x1F }; |
3668 | enum { NoReturnMask = 0x20 }; |
3669 | enum { ProducesResultMask = 0x40 }; |
3670 | enum { NoCallerSavedRegsMask = 0x80 }; |
3671 | enum { |
3672 | RegParmMask = 0x700, |
3673 | RegParmOffset = 8 |
3674 | }; |
3675 | enum { NoCfCheckMask = 0x800 }; |
3676 | enum { CmseNSCallMask = 0x1000 }; |
3677 | uint16_t Bits = CC_C; |
3678 | |
3679 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} |
3680 | |
3681 | public: |
3682 | |
3683 | |
3684 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, |
3685 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, |
3686 | bool cmseNSCall) { |
3687 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); |
3688 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | |
3689 | (producesResult ? ProducesResultMask : 0) | |
3690 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | |
3691 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | |
3692 | (NoCfCheck ? NoCfCheckMask : 0) | |
3693 | (cmseNSCall ? CmseNSCallMask : 0); |
3694 | } |
3695 | |
3696 | |
3697 | |
3698 | ExtInfo() = default; |
3699 | |
3700 | |
3701 | |
3702 | ExtInfo(CallingConv CC) : Bits(CC) {} |
3703 | |
3704 | bool getNoReturn() const { return Bits & NoReturnMask; } |
3705 | bool getProducesResult() const { return Bits & ProducesResultMask; } |
3706 | bool getCmseNSCall() const { return Bits & CmseNSCallMask; } |
3707 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } |
3708 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } |
3709 | bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } |
3710 | |
3711 | unsigned getRegParm() const { |
3712 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; |
3713 | if (RegParm > 0) |
3714 | --RegParm; |
3715 | return RegParm; |
3716 | } |
3717 | |
3718 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } |
3719 | |
3720 | bool operator==(ExtInfo Other) const { |
3721 | return Bits == Other.Bits; |
3722 | } |
3723 | bool operator!=(ExtInfo Other) const { |
3724 | return Bits != Other.Bits; |
3725 | } |
3726 | |
3727 | |
3728 | |
3729 | |
3730 | ExtInfo withNoReturn(bool noReturn) const { |
3731 | if (noReturn) |
3732 | return ExtInfo(Bits | NoReturnMask); |
3733 | else |
3734 | return ExtInfo(Bits & ~NoReturnMask); |
3735 | } |
3736 | |
3737 | ExtInfo withProducesResult(bool producesResult) const { |
3738 | if (producesResult) |
3739 | return ExtInfo(Bits | ProducesResultMask); |
3740 | else |
3741 | return ExtInfo(Bits & ~ProducesResultMask); |
3742 | } |
3743 | |
3744 | ExtInfo withCmseNSCall(bool cmseNSCall) const { |
3745 | if (cmseNSCall) |
3746 | return ExtInfo(Bits | CmseNSCallMask); |
3747 | else |
3748 | return ExtInfo(Bits & ~CmseNSCallMask); |
3749 | } |
3750 | |
3751 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { |
3752 | if (noCallerSavedRegs) |
3753 | return ExtInfo(Bits | NoCallerSavedRegsMask); |
3754 | else |
3755 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); |
3756 | } |
3757 | |
3758 | ExtInfo withNoCfCheck(bool noCfCheck) const { |
3759 | if (noCfCheck) |
3760 | return ExtInfo(Bits | NoCfCheckMask); |
3761 | else |
3762 | return ExtInfo(Bits & ~NoCfCheckMask); |
3763 | } |
3764 | |
3765 | ExtInfo withRegParm(unsigned RegParm) const { |
3766 | assert(RegParm < 7 && "Invalid regparm value"); |
3767 | return ExtInfo((Bits & ~RegParmMask) | |
3768 | ((RegParm + 1) << RegParmOffset)); |
3769 | } |
3770 | |
3771 | ExtInfo withCallingConv(CallingConv cc) const { |
3772 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); |
3773 | } |
3774 | |
3775 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3776 | ID.AddInteger(Bits); |
3777 | } |
3778 | }; |
3779 | |
3780 | |
3781 | |
3782 | |
3783 | struct ExceptionType { QualType Type; }; |
3784 | |
3785 | |
3786 | |
3787 | |
3788 | |
3789 | struct alignas(void *) FunctionTypeExtraBitfields { |
3790 | |
3791 | |
3792 | |
3793 | unsigned NumExceptionType; |
3794 | }; |
3795 | |
3796 | protected: |
3797 | FunctionType(TypeClass tc, QualType res, QualType Canonical, |
3798 | TypeDependence Dependence, ExtInfo Info) |
3799 | : Type(tc, Canonical, Dependence), ResultType(res) { |
3800 | FunctionTypeBits.ExtInfo = Info.Bits; |
3801 | } |
3802 | |
3803 | Qualifiers getFastTypeQuals() const { |
3804 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); |
3805 | } |
3806 | |
3807 | public: |
3808 | QualType getReturnType() const { return ResultType; } |
3809 | |
3810 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } |
3811 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } |
3812 | |
3813 | |
3814 | |
3815 | |
3816 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } |
3817 | |
3818 | bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } |
3819 | CallingConv getCallConv() const { return getExtInfo().getCC(); } |
3820 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } |
3821 | |
3822 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, |
3823 | "Const, volatile and restrict are assumed to be a subset of " |
3824 | "the fast qualifiers."); |
3825 | |
3826 | bool isConst() const { return getFastTypeQuals().hasConst(); } |
3827 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } |
3828 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } |
3829 | |
3830 | |
3831 | |
3832 | QualType getCallResultType(const ASTContext &Context) const { |
3833 | return getReturnType().getNonLValueExprType(Context); |
3834 | } |
3835 | |
3836 | static StringRef getNameForCallConv(CallingConv CC); |
3837 | |
3838 | static bool classof(const Type *T) { |
3839 | return T->getTypeClass() == FunctionNoProto || |
3840 | T->getTypeClass() == FunctionProto; |
3841 | } |
3842 | }; |
3843 | |
3844 | |
3845 | |
3846 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { |
3847 | friend class ASTContext; |
3848 | |
3849 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) |
3850 | : FunctionType(FunctionNoProto, Result, Canonical, |
3851 | Result->getDependence() & |
3852 | ~(TypeDependence::DependentInstantiation | |
3853 | TypeDependence::UnexpandedPack), |
3854 | Info) {} |
3855 | |
3856 | public: |
3857 | |
3858 | |
3859 | bool isSugared() const { return false; } |
3860 | QualType desugar() const { return QualType(this, 0); } |
3861 | |
3862 | void Profile(llvm::FoldingSetNodeID &ID) { |
3863 | Profile(ID, getReturnType(), getExtInfo()); |
3864 | } |
3865 | |
3866 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, |
3867 | ExtInfo Info) { |
3868 | Info.Profile(ID); |
3869 | ID.AddPointer(ResultType.getAsOpaquePtr()); |
3870 | } |
3871 | |
3872 | static bool classof(const Type *T) { |
3873 | return T->getTypeClass() == FunctionNoProto; |
3874 | } |
3875 | }; |
3876 | |
3877 | |
3878 | |
3879 | |
3880 | |
3881 | |
3882 | |
3883 | |
3884 | class FunctionProtoType final |
3885 | : public FunctionType, |
3886 | public llvm::FoldingSetNode, |
3887 | private llvm::TrailingObjects< |
3888 | FunctionProtoType, QualType, SourceLocation, |
3889 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, |
3890 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { |
3891 | friend class ASTContext; |
3892 | friend TrailingObjects; |
3893 | |
3894 | |
3895 | |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | |
3902 | |
3903 | |
3904 | |
3905 | |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | |
3923 | |
3924 | |
3925 | |
3926 | |
3927 | |
3928 | |
3929 | |
3930 | |
3931 | |
3932 | |
3933 | |
3934 | |
3935 | |
3936 | public: |
3937 | |
3938 | |
3939 | |
3940 | |
3941 | struct ExceptionSpecInfo { |
3942 | |
3943 | ExceptionSpecificationType Type = EST_None; |
3944 | |
3945 | |
3946 | ArrayRef<QualType> Exceptions; |
3947 | |
3948 | |
3949 | Expr *NoexceptExpr = nullptr; |
3950 | |
3951 | |
3952 | |
3953 | FunctionDecl *SourceDecl = nullptr; |
3954 | |
3955 | |
3956 | |
3957 | FunctionDecl *SourceTemplate = nullptr; |
3958 | |
3959 | ExceptionSpecInfo() = default; |
3960 | |
3961 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} |
3962 | }; |
3963 | |
3964 | |
3965 | |
3966 | |
3967 | struct ExtProtoInfo { |
3968 | FunctionType::ExtInfo ExtInfo; |
3969 | bool Variadic : 1; |
3970 | bool HasTrailingReturn : 1; |
3971 | Qualifiers TypeQuals; |
3972 | RefQualifierKind RefQualifier = RQ_None; |
3973 | ExceptionSpecInfo ExceptionSpec; |
3974 | const ExtParameterInfo *ExtParameterInfos = nullptr; |
3975 | SourceLocation EllipsisLoc; |
3976 | |
3977 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} |
3978 | |
3979 | ExtProtoInfo(CallingConv CC) |
3980 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} |
3981 | |
3982 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { |
3983 | ExtProtoInfo Result(*this); |
3984 | Result.ExceptionSpec = ESI; |
3985 | return Result; |
3986 | } |
3987 | }; |
3988 | |
3989 | private: |
3990 | unsigned numTrailingObjects(OverloadToken<QualType>) const { |
3991 | return getNumParams(); |
3992 | } |
3993 | |
3994 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
3995 | return isVariadic(); |
3996 | } |
3997 | |
3998 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { |
3999 | return hasExtraBitfields(); |
4000 | } |
4001 | |
4002 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { |
4003 | return getExceptionSpecSize().NumExceptionType; |
4004 | } |
4005 | |
4006 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { |
4007 | return getExceptionSpecSize().NumExprPtr; |
4008 | } |
4009 | |
4010 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { |
4011 | return getExceptionSpecSize().NumFunctionDeclPtr; |
4012 | } |
4013 | |
4014 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
4015 | return hasExtParameterInfos() ? getNumParams() : 0; |
4016 | } |
4017 | |
4018 | |
4019 | |
4020 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, |
4021 | unsigned numArgs) { |
4022 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) |
4023 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) |
4024 | return true; |
4025 | |
4026 | return false; |
4027 | } |
4028 | |
4029 | FunctionProtoType(QualType result, ArrayRef<QualType> params, |
4030 | QualType canonical, const ExtProtoInfo &epi); |
4031 | |
4032 | |
4033 | |
4034 | |
4035 | struct ExceptionSpecSizeHolder { |
4036 | unsigned NumExceptionType; |
4037 | unsigned NumExprPtr; |
4038 | unsigned NumFunctionDeclPtr; |
4039 | }; |
4040 | |
4041 | |
4042 | |
4043 | static ExceptionSpecSizeHolder |
4044 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { |
4045 | switch (EST) { |
4046 | case EST_None: |
4047 | case EST_DynamicNone: |
4048 | case EST_MSAny: |
4049 | case EST_BasicNoexcept: |
4050 | case EST_Unparsed: |
4051 | case EST_NoThrow: |
4052 | return {0, 0, 0}; |
4053 | |
4054 | case EST_Dynamic: |
4055 | return {NumExceptions, 0, 0}; |
4056 | |
4057 | case EST_DependentNoexcept: |
4058 | case EST_NoexceptFalse: |
4059 | case EST_NoexceptTrue: |
4060 | return {0, 1, 0}; |
4061 | |
4062 | case EST_Uninstantiated: |
4063 | return {0, 0, 2}; |
4064 | |
4065 | case EST_Unevaluated: |
4066 | return {0, 0, 1}; |
4067 | } |
4068 | llvm_unreachable("bad exception specification kind"); |
4069 | } |
4070 | |
4071 | |
4072 | |
4073 | ExceptionSpecSizeHolder getExceptionSpecSize() const { |
4074 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); |
4075 | } |
4076 | |
4077 | |
4078 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { |
4079 | |
4080 | |
4081 | return EST == EST_Dynamic; |
4082 | } |
4083 | |
4084 | |
4085 | bool hasExtraBitfields() const { |
4086 | return hasExtraBitfields(getExceptionSpecType()); |
4087 | } |
4088 | |
4089 | bool hasExtQualifiers() const { |
4090 | return FunctionTypeBits.HasExtQuals; |
4091 | } |
4092 | |
4093 | public: |
4094 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } |
4095 | |
4096 | QualType getParamType(unsigned i) const { |
4097 | assert(i < getNumParams() && "invalid parameter index"); |
4098 | return param_type_begin()[i]; |
4099 | } |
4100 | |
4101 | ArrayRef<QualType> getParamTypes() const { |
4102 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); |
4103 | } |
4104 | |
4105 | ExtProtoInfo getExtProtoInfo() const { |
4106 | ExtProtoInfo EPI; |
4107 | EPI.ExtInfo = getExtInfo(); |
4108 | EPI.Variadic = isVariadic(); |
4109 | EPI.EllipsisLoc = getEllipsisLoc(); |
4110 | EPI.HasTrailingReturn = hasTrailingReturn(); |
4111 | EPI.ExceptionSpec = getExceptionSpecInfo(); |
4112 | EPI.TypeQuals = getMethodQuals(); |
4113 | EPI.RefQualifier = getRefQualifier(); |
4114 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); |
4115 | return EPI; |
4116 | } |
4117 | |
4118 | |
4119 | ExceptionSpecificationType getExceptionSpecType() const { |
4120 | return static_cast<ExceptionSpecificationType>( |
4121 | FunctionTypeBits.ExceptionSpecType); |
4122 | } |
4123 | |
4124 | |
4125 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } |
4126 | |
4127 | |
4128 | bool hasDynamicExceptionSpec() const { |
4129 | return isDynamicExceptionSpec(getExceptionSpecType()); |
4130 | } |
4131 | |
4132 | |
4133 | bool hasNoexceptExceptionSpec() const { |
4134 | return isNoexceptExceptionSpec(getExceptionSpecType()); |
4135 | } |
4136 | |
4137 | |
4138 | bool hasDependentExceptionSpec() const; |
4139 | |
4140 | |
4141 | |
4142 | bool hasInstantiationDependentExceptionSpec() const; |
4143 | |
4144 | |
4145 | ExceptionSpecInfo getExceptionSpecInfo() const { |
4146 | ExceptionSpecInfo Result; |
4147 | Result.Type = getExceptionSpecType(); |
4148 | if (Result.Type == EST_Dynamic) { |
4149 | Result.Exceptions = exceptions(); |
4150 | } else if (isComputedNoexcept(Result.Type)) { |
4151 | Result.NoexceptExpr = getNoexceptExpr(); |
4152 | } else if (Result.Type == EST_Uninstantiated) { |
4153 | Result.SourceDecl = getExceptionSpecDecl(); |
4154 | Result.SourceTemplate = getExceptionSpecTemplate(); |
4155 | } else if (Result.Type == EST_Unevaluated) { |
4156 | Result.SourceDecl = getExceptionSpecDecl(); |
4157 | } |
4158 | return Result; |
4159 | } |
4160 | |
4161 | |
4162 | unsigned getNumExceptions() const { |
4163 | return getExceptionSpecType() == EST_Dynamic |
4164 | ? getTrailingObjects<FunctionTypeExtraBitfields>() |
4165 | ->NumExceptionType |
4166 | : 0; |
4167 | } |
4168 | |
4169 | |
4170 | QualType getExceptionType(unsigned i) const { |
4171 | assert(i < getNumExceptions() && "Invalid exception number!"); |
4172 | return exception_begin()[i]; |
4173 | } |
4174 | |
4175 | |
4176 | |
4177 | Expr *getNoexceptExpr() const { |
4178 | if (!isComputedNoexcept(getExceptionSpecType())) |
4179 | return nullptr; |
4180 | return *getTrailingObjects<Expr *>(); |
4181 | } |
4182 | |
4183 | |
4184 | |
4185 | |
4186 | |
4187 | FunctionDecl *getExceptionSpecDecl() const { |
4188 | if (getExceptionSpecType() != EST_Uninstantiated && |
4189 | getExceptionSpecType() != EST_Unevaluated) |
4190 | return nullptr; |
4191 | return getTrailingObjects<FunctionDecl *>()[0]; |
4192 | } |
4193 | |
4194 | |
4195 | |
4196 | |
4197 | |
4198 | FunctionDecl *getExceptionSpecTemplate() const { |
4199 | if (getExceptionSpecType() != EST_Uninstantiated) |
4200 | return nullptr; |
4201 | return getTrailingObjects<FunctionDecl *>()[1]; |
4202 | } |
4203 | |
4204 | |
4205 | |
4206 | CanThrowResult canThrow() const; |
4207 | |
4208 | |
4209 | |
4210 | |
4211 | bool isNothrow(bool ResultIfDependent = false) const { |
4212 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; |
4213 | } |
4214 | |
4215 | |
4216 | bool isVariadic() const { return FunctionTypeBits.Variadic; } |
4217 | |
4218 | SourceLocation getEllipsisLoc() const { |
4219 | return isVariadic() ? *getTrailingObjects<SourceLocation>() |
4220 | : SourceLocation(); |
4221 | } |
4222 | |
4223 | |
4224 | |
4225 | |
4226 | |
4227 | |
4228 | |
4229 | bool isTemplateVariadic() const; |
4230 | |
4231 | |
4232 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } |
4233 | |
4234 | Qualifiers getMethodQuals() const { |
4235 | if (hasExtQualifiers()) |
4236 | return *getTrailingObjects<Qualifiers>(); |
4237 | else |
4238 | return getFastTypeQuals(); |
4239 | } |
4240 | |
4241 | |
4242 | RefQualifierKind getRefQualifier() const { |
4243 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); |
4244 | } |
4245 | |
4246 | using param_type_iterator = const QualType *; |
4247 | using param_type_range = llvm::iterator_range<param_type_iterator>; |
4248 | |
4249 | param_type_range param_types() const { |
4250 | return param_type_range(param_type_begin(), param_type_end()); |
4251 | } |
4252 | |
4253 | param_type_iterator param_type_begin() const { |
4254 | return getTrailingObjects<QualType>(); |
4255 | } |
4256 | |
4257 | param_type_iterator param_type_end() const { |
4258 | return param_type_begin() + getNumParams(); |
4259 | } |
4260 | |
4261 | using exception_iterator = const QualType *; |
4262 | |
4263 | ArrayRef<QualType> exceptions() const { |
4264 | return llvm::makeArrayRef(exception_begin(), exception_end()); |
4265 | } |
4266 | |
4267 | exception_iterator exception_begin() const { |
4268 | return reinterpret_cast<exception_iterator>( |
4269 | getTrailingObjects<ExceptionType>()); |
4270 | } |
4271 | |
4272 | exception_iterator exception_end() const { |
4273 | return exception_begin() + getNumExceptions(); |
4274 | } |
4275 | |
4276 | |
4277 | |
4278 | bool hasExtParameterInfos() const { |
4279 | return FunctionTypeBits.HasExtParameterInfos; |
4280 | } |
4281 | |
4282 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
4283 | assert(hasExtParameterInfos()); |
4284 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), |
4285 | getNumParams()); |
4286 | } |
4287 | |
4288 | |
4289 | |
4290 | |
4291 | const ExtParameterInfo *getExtParameterInfosOrNull() const { |
4292 | if (!hasExtParameterInfos()) |
4293 | return nullptr; |
4294 | return getTrailingObjects<ExtParameterInfo>(); |
4295 | } |
4296 | |
4297 | ExtParameterInfo getExtParameterInfo(unsigned I) const { |
4298 | assert(I < getNumParams() && "parameter index out of range"); |
4299 | if (hasExtParameterInfos()) |
4300 | return getTrailingObjects<ExtParameterInfo>()[I]; |
4301 | return ExtParameterInfo(); |
4302 | } |
4303 | |
4304 | ParameterABI getParameterABI(unsigned I) const { |
4305 | assert(I < getNumParams() && "parameter index out of range"); |
4306 | if (hasExtParameterInfos()) |
4307 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); |
4308 | return ParameterABI::Ordinary; |
4309 | } |
4310 | |
4311 | bool isParamConsumed(unsigned I) const { |
4312 | assert(I < getNumParams() && "parameter index out of range"); |
4313 | if (hasExtParameterInfos()) |
4314 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); |
4315 | return false; |
4316 | } |
4317 | |
4318 | bool isSugared() const { return false; } |
4319 | QualType desugar() const { return QualType(this, 0); } |
4320 | |
4321 | void printExceptionSpecification(raw_ostream &OS, |
4322 | const PrintingPolicy &Policy) const; |
4323 | |
4324 | static bool classof(const Type *T) { |
4325 | return T->getTypeClass() == FunctionProto; |
4326 | } |
4327 | |
4328 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); |
4329 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
4330 | param_type_iterator ArgTys, unsigned NumArgs, |
4331 | const ExtProtoInfo &EPI, const ASTContext &Context, |
4332 | bool Canonical); |
4333 | }; |
4334 | |
4335 | |
4336 | |
4337 | |
4338 | |
4339 | |
4340 | class UnresolvedUsingType : public Type { |
4341 | friend class ASTContext; |
4342 | |
4343 | UnresolvedUsingTypenameDecl *Decl; |
4344 | |
4345 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) |
4346 | : Type(UnresolvedUsing, QualType(), |
4347 | TypeDependence::DependentInstantiation), |
4348 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} |
4349 | |
4350 | public: |
4351 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } |
4352 | |
4353 | bool isSugared() const { return false; } |
4354 | QualType desugar() const { return QualType(this, 0); } |
4355 | |
4356 | static bool classof(const Type *T) { |
4357 | return T->getTypeClass() == UnresolvedUsing; |
4358 | } |
4359 | |
4360 | void Profile(llvm::FoldingSetNodeID &ID) { |
4361 | return Profile(ID, Decl); |
4362 | } |
4363 | |
4364 | static void Profile(llvm::FoldingSetNodeID &ID, |
4365 | UnresolvedUsingTypenameDecl *D) { |
4366 | ID.AddPointer(D); |
4367 | } |
4368 | }; |
4369 | |
4370 | class TypedefType : public Type { |
4371 | TypedefNameDecl *Decl; |
4372 | |
4373 | private: |
4374 | friend class ASTContext; |
4375 | |
4376 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, |
4377 | QualType can); |
4378 | |
4379 | public: |
4380 | TypedefNameDecl *getDecl() const { return Decl; } |
4381 | |
4382 | bool isSugared() const { return true; } |
4383 | QualType desugar() const; |
4384 | |
4385 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } |
4386 | }; |
4387 | |
4388 | |
4389 | |
4390 | class MacroQualifiedType : public Type { |
4391 | friend class ASTContext; |
4392 | |
4393 | QualType UnderlyingTy; |
4394 | const IdentifierInfo *MacroII; |
4395 | |
4396 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, |
4397 | const IdentifierInfo *MacroII) |
4398 | : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), |
4399 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { |
4400 | assert(isa<AttributedType>(UnderlyingTy) && |
4401 | "Expected a macro qualified type to only wrap attributed types."); |
4402 | } |
4403 | |
4404 | public: |
4405 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
4406 | QualType getUnderlyingType() const { return UnderlyingTy; } |
4407 | |
4408 | |
4409 | |
4410 | QualType getModifiedType() const; |
4411 | |
4412 | bool isSugared() const { return true; } |
4413 | QualType desugar() const; |
4414 | |
4415 | static bool classof(const Type *T) { |
4416 | return T->getTypeClass() == MacroQualified; |
4417 | } |
4418 | }; |
4419 | |
4420 | |
4421 | class TypeOfExprType : public Type { |
4422 | Expr *TOExpr; |
4423 | |
4424 | protected: |
4425 | friend class ASTContext; |
4426 | |
4427 | TypeOfExprType(Expr *E, QualType can = QualType()); |
4428 | |
4429 | public: |
4430 | Expr *getUnderlyingExpr() const { return TOExpr; } |
4431 | |
4432 | |
4433 | QualType desugar() const; |
4434 | |
4435 | |
4436 | bool isSugared() const; |
4437 | |
4438 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } |
4439 | }; |
4440 | |
4441 | |
4442 | |
4443 | |
4444 | |
4445 | |
4446 | |
4447 | class DependentTypeOfExprType |
4448 | : public TypeOfExprType, public llvm::FoldingSetNode { |
4449 | const ASTContext &Context; |
4450 | |
4451 | public: |
4452 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) |
4453 | : TypeOfExprType(E), Context(Context) {} |
4454 | |
4455 | void Profile(llvm::FoldingSetNodeID &ID) { |
4456 | Profile(ID, Context, getUnderlyingExpr()); |
4457 | } |
4458 | |
4459 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4460 | Expr *E); |
4461 | }; |
4462 | |
4463 | |
4464 | class TypeOfType : public Type { |
4465 | friend class ASTContext; |
4466 | |
4467 | QualType TOType; |
4468 | |
4469 | TypeOfType(QualType T, QualType can) |
4470 | : Type(TypeOf, can, T->getDependence()), TOType(T) { |
4471 | assert(!isa<TypedefType>(can) && "Invalid canonical type"); |
4472 | } |
4473 | |
4474 | public: |
4475 | QualType getUnderlyingType() const { return TOType; } |
4476 | |
4477 | |
4478 | QualType desugar() const { return getUnderlyingType(); } |
4479 | |
4480 | |
4481 | bool isSugared() const { return true; } |
4482 | |
4483 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } |
4484 | }; |
4485 | |
4486 | |
4487 | class DecltypeType : public Type { |
4488 | Expr *E; |
4489 | QualType UnderlyingType; |
4490 | |
4491 | protected: |
4492 | friend class ASTContext; |
4493 | |
4494 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); |
4495 | |
4496 | public: |
4497 | Expr *getUnderlyingExpr() const { return E; } |
4498 | QualType getUnderlyingType() const { return UnderlyingType; } |
4499 | |
4500 | |
4501 | QualType desugar() const; |
4502 | |
4503 | |
4504 | bool isSugared() const; |
4505 | |
4506 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } |
4507 | }; |
4508 | |
4509 | |
4510 | |
4511 | |
4512 | |
4513 | |
4514 | |
4515 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { |
4516 | const ASTContext &Context; |
4517 | |
4518 | public: |
4519 | DependentDecltypeType(const ASTContext &Context, Expr *E); |
4520 | |
4521 | void Profile(llvm::FoldingSetNodeID &ID) { |
4522 | Profile(ID, Context, getUnderlyingExpr()); |
4523 | } |
4524 | |
4525 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4526 | Expr *E); |
4527 | }; |
4528 | |
4529 | |
4530 | class UnaryTransformType : public Type { |
4531 | public: |
4532 | enum UTTKind { |
4533 | EnumUnderlyingType |
4534 | }; |
4535 | |
4536 | private: |
4537 | |
4538 | QualType BaseType; |
4539 | |
4540 | |
4541 | QualType UnderlyingType; |
4542 | |
4543 | UTTKind UKind; |
4544 | |
4545 | protected: |
4546 | friend class ASTContext; |
4547 | |
4548 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, |
4549 | QualType CanonicalTy); |
4550 | |
4551 | public: |
4552 | bool isSugared() const { return !isDependentType(); } |
4553 | QualType desugar() const { return UnderlyingType; } |
4554 | |
4555 | QualType getUnderlyingType() const { return UnderlyingType; } |
4556 | QualType getBaseType() const { return BaseType; } |
4557 | |
4558 | UTTKind getUTTKind() const { return UKind; } |
4559 | |
4560 | static bool classof(const Type *T) { |
4561 | return T->getTypeClass() == UnaryTransform; |
4562 | } |
4563 | }; |
4564 | |
4565 | |
4566 | |
4567 | |
4568 | |
4569 | |
4570 | |
4571 | class DependentUnaryTransformType : public UnaryTransformType, |
4572 | public llvm::FoldingSetNode { |
4573 | public: |
4574 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, |
4575 | UTTKind UKind); |
4576 | |
4577 | void Profile(llvm::FoldingSetNodeID &ID) { |
4578 | Profile(ID, getBaseType(), getUTTKind()); |
4579 | } |
4580 | |
4581 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
4582 | UTTKind UKind) { |
4583 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
4584 | ID.AddInteger((unsigned)UKind); |
4585 | } |
4586 | }; |
4587 | |
4588 | class TagType : public Type { |
4589 | friend class ASTReader; |
4590 | template <class T> friend class serialization::AbstractTypeReader; |
4591 | |
4592 | |
4593 | |
4594 | TagDecl *decl; |
4595 | |
4596 | protected: |
4597 | TagType(TypeClass TC, const TagDecl *D, QualType can); |
4598 | |
4599 | public: |
4600 | TagDecl *getDecl() const; |
4601 | |
4602 | |
4603 | bool isBeingDefined() const; |
4604 | |
4605 | static bool classof(const Type *T) { |
4606 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; |
4607 | } |
4608 | }; |
4609 | |
4610 | |
4611 | |
4612 | class RecordType : public TagType { |
4613 | protected: |
4614 | friend class ASTContext; |
4615 | |
4616 | explicit RecordType(const RecordDecl *D) |
4617 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4618 | explicit RecordType(TypeClass TC, RecordDecl *D) |
4619 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4620 | |
4621 | public: |
4622 | RecordDecl *getDecl() const { |
4623 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); |
4624 | } |
4625 | |
4626 | |
4627 | |
4628 | bool hasConstFields() const; |
4629 | |
4630 | bool isSugared() const { return false; } |
4631 | QualType desugar() const { return QualType(this, 0); } |
4632 | |
4633 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } |
4634 | }; |
4635 | |
4636 | |
4637 | |
4638 | class EnumType : public TagType { |
4639 | friend class ASTContext; |
4640 | |
4641 | explicit EnumType(const EnumDecl *D) |
4642 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4643 | |
4644 | public: |
4645 | EnumDecl *getDecl() const { |
4646 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); |
4647 | } |
4648 | |
4649 | bool isSugared() const { return false; } |
4650 | QualType desugar() const { return QualType(this, 0); } |
4651 | |
4652 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } |
4653 | }; |
4654 | |
4655 | |
4656 | |
4657 | |
4658 | |
4659 | |
4660 | |
4661 | |
4662 | |
4663 | |
4664 | |
4665 | |
4666 | |
4667 | class AttributedType : public Type, public llvm::FoldingSetNode { |
4668 | public: |
4669 | using Kind = attr::Kind; |
4670 | |
4671 | private: |
4672 | friend class ASTContext; |
4673 | |
4674 | QualType ModifiedType; |
4675 | QualType EquivalentType; |
4676 | |
4677 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, |
4678 | QualType equivalent) |
4679 | : Type(Attributed, canon, equivalent->getDependence()), |
4680 | ModifiedType(modified), EquivalentType(equivalent) { |
4681 | AttributedTypeBits.AttrKind = attrKind; |
4682 | } |
4683 | |
4684 | public: |
4685 | Kind getAttrKind() const { |
4686 | return static_cast<Kind>(AttributedTypeBits.AttrKind); |
4687 | } |
4688 | |
4689 | QualType getModifiedType() const { return ModifiedType; } |
4690 | QualType getEquivalentType() const { return EquivalentType; } |
4691 | |
4692 | bool isSugared() const { return true; } |
4693 | QualType desugar() const { return getEquivalentType(); } |
4694 | |
4695 | |
4696 | |
4697 | |
4698 | |
4699 | |
4700 | |
4701 | |
4702 | |
4703 | |
4704 | |
4705 | |
4706 | |
4707 | |
4708 | |
4709 | |
4710 | bool isQualifier() const; |
4711 | |
4712 | bool isMSTypeSpec() const; |
4713 | |
4714 | bool isCallingConv() const; |
4715 | |
4716 | llvm::Optional<NullabilityKind> getImmediateNullability() const; |
4717 | |
4718 | |
4719 | |
4720 | static Kind getNullabilityAttrKind(NullabilityKind kind) { |
4721 | switch (kind) { |
4722 | case NullabilityKind::NonNull: |
4723 | return attr::TypeNonNull; |
4724 | |
4725 | case NullabilityKind::Nullable: |
4726 | return attr::TypeNullable; |
4727 | |
4728 | case NullabilityKind::NullableResult: |
4729 | return attr::TypeNullableResult; |
4730 | |
4731 | case NullabilityKind::Unspecified: |
4732 | return attr::TypeNullUnspecified; |
4733 | } |
4734 | llvm_unreachable("Unknown nullability kind."); |
4735 | } |
4736 | |
4737 | |
4738 | |
4739 | |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); |
4747 | |
4748 | void Profile(llvm::FoldingSetNodeID &ID) { |
4749 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); |
4750 | } |
4751 | |
4752 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, |
4753 | QualType modified, QualType equivalent) { |
4754 | ID.AddInteger(attrKind); |
4755 | ID.AddPointer(modified.getAsOpaquePtr()); |
4756 | ID.AddPointer(equivalent.getAsOpaquePtr()); |
4757 | } |
4758 | |
4759 | static bool classof(const Type *T) { |
4760 | return T->getTypeClass() == Attributed; |
4761 | } |
4762 | }; |
4763 | |
4764 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4765 | friend class ASTContext; |
4766 | |
4767 | |
4768 | struct CanonicalTTPTInfo { |
4769 | unsigned Depth : 15; |
4770 | unsigned ParameterPack : 1; |
4771 | unsigned Index : 16; |
4772 | }; |
4773 | |
4774 | union { |
4775 | |
4776 | CanonicalTTPTInfo CanTTPTInfo; |
4777 | |
4778 | |
4779 | TemplateTypeParmDecl *TTPDecl; |
4780 | }; |
4781 | |
4782 | |
4783 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) |
4784 | : Type(TemplateTypeParm, Canon, |
4785 | TypeDependence::DependentInstantiation | |
4786 | (Canon->getDependence() & TypeDependence::UnexpandedPack)), |
4787 | TTPDecl(TTPDecl) {} |
4788 | |
4789 | |
4790 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) |
4791 | : Type(TemplateTypeParm, QualType(this, 0), |
4792 | TypeDependence::DependentInstantiation | |
4793 | (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { |
4794 | CanTTPTInfo.Depth = D; |
4795 | CanTTPTInfo.Index = I; |
4796 | CanTTPTInfo.ParameterPack = PP; |
4797 | } |
4798 | |
4799 | const CanonicalTTPTInfo& getCanTTPTInfo() const { |
4800 | QualType Can = getCanonicalTypeInternal(); |
4801 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; |
4802 | } |
4803 | |
4804 | public: |
4805 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } |
4806 | unsigned getIndex() const { return getCanTTPTInfo().Index; } |
4807 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } |
4808 | |
4809 | TemplateTypeParmDecl *getDecl() const { |
4810 | return isCanonicalUnqualified() ? nullptr : TTPDecl; |
4811 | } |
4812 | |
4813 | IdentifierInfo *getIdentifier() const; |
4814 | |
4815 | bool isSugared() const { return false; } |
4816 | QualType desugar() const { return QualType(this, 0); } |
4817 | |
4818 | void Profile(llvm::FoldingSetNodeID &ID) { |
4819 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); |
4820 | } |
4821 | |
4822 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, |
4823 | unsigned Index, bool ParameterPack, |
4824 | TemplateTypeParmDecl *TTPDecl) { |
4825 | ID.AddInteger(Depth); |
4826 | ID.AddInteger(Index); |
4827 | ID.AddBoolean(ParameterPack); |
4828 | ID.AddPointer(TTPDecl); |
4829 | } |
4830 | |
4831 | static bool classof(const Type *T) { |
4832 | return T->getTypeClass() == TemplateTypeParm; |
4833 | } |
4834 | }; |
4835 | |
4836 | |
4837 | |
4838 | |
4839 | |
4840 | |
4841 | |
4842 | |
4843 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4844 | friend class ASTContext; |
4845 | |
4846 | |
4847 | const TemplateTypeParmType *Replaced; |
4848 | |
4849 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) |
4850 | : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()), |
4851 | Replaced(Param) {} |
4852 | |
4853 | public: |
4854 | |
4855 | const TemplateTypeParmType *getReplacedParameter() const { |
4856 | return Replaced; |
4857 | } |
4858 | |
4859 | |
4860 | |
4861 | QualType getReplacementType() const { |
4862 | return getCanonicalTypeInternal(); |
4863 | } |
4864 | |
4865 | bool isSugared() const { return true; } |
4866 | QualType desugar() const { return getReplacementType(); } |
4867 | |
4868 | void Profile(llvm::FoldingSetNodeID &ID) { |
4869 | Profile(ID, getReplacedParameter(), getReplacementType()); |
4870 | } |
4871 | |
4872 | static void Profile(llvm::FoldingSetNodeID &ID, |
4873 | const TemplateTypeParmType *Replaced, |
4874 | QualType Replacement) { |
4875 | ID.AddPointer(Replaced); |
4876 | ID.AddPointer(Replacement.getAsOpaquePtr()); |
4877 | } |
4878 | |
4879 | static bool classof(const Type *T) { |
4880 | return T->getTypeClass() == SubstTemplateTypeParm; |
4881 | } |
4882 | }; |
4883 | |
4884 | |
4885 | |
4886 | |
4887 | |
4888 | |
4889 | |
4890 | |
4891 | |
4892 | |
4893 | |
4894 | |
4895 | |
4896 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { |
4897 | friend class ASTContext; |
4898 | |
4899 | |
4900 | const TemplateTypeParmType *Replaced; |
4901 | |
4902 | |
4903 | |
4904 | const TemplateArgument *Arguments; |
4905 | |
4906 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
4907 | QualType Canon, |
4908 | const TemplateArgument &ArgPack); |
4909 | |
4910 | public: |
4911 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } |
4912 | |
4913 | |
4914 | const TemplateTypeParmType *getReplacedParameter() const { |
4915 | return Replaced; |
4916 | } |
4917 | |
4918 | unsigned getNumArgs() const { |
4919 | return SubstTemplateTypeParmPackTypeBits.NumArgs; |
4920 | } |
4921 | |
4922 | bool isSugared() const { return false; } |
4923 | QualType desugar() const { return QualType(this, 0); } |
4924 | |
4925 | TemplateArgument getArgumentPack() const; |
4926 | |
4927 | void Profile(llvm::FoldingSetNodeID &ID); |
4928 | static void Profile(llvm::FoldingSetNodeID &ID, |
4929 | const TemplateTypeParmType *Replaced, |
4930 | const TemplateArgument &ArgPack); |
4931 | |
4932 | static bool classof(const Type *T) { |
4933 | return T->getTypeClass() == SubstTemplateTypeParmPack; |
4934 | } |
4935 | }; |
4936 | |
4937 | |
4938 | |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | class DeducedType : public Type { |
4946 | protected: |
4947 | DeducedType(TypeClass TC, QualType DeducedAsType, |
4948 | TypeDependence ExtraDependence) |
4949 | : Type(TC, |
4950 | |
4951 | DeducedAsType.isNull() ? QualType(this, 0) |
4952 | : DeducedAsType.getCanonicalType(), |
4953 | ExtraDependence | (DeducedAsType.isNull() |
4954 | ? TypeDependence::None |
4955 | : DeducedAsType->getDependence() & |
4956 | ~TypeDependence::VariablyModified)) {} |
4957 | |
4958 | public: |
4959 | bool isSugared() const { return !isCanonicalUnqualified(); } |
4960 | QualType desugar() const { return getCanonicalTypeInternal(); } |
4961 | |
4962 | |
4963 | |
4964 | QualType getDeducedType() const { |
4965 | return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType(); |
4966 | } |
4967 | bool isDeduced() const { |
4968 | return !isCanonicalUnqualified() || isDependentType(); |
4969 | } |
4970 | |
4971 | static bool classof(const Type *T) { |
4972 | return T->getTypeClass() == Auto || |
4973 | T->getTypeClass() == DeducedTemplateSpecialization; |
4974 | } |
4975 | }; |
4976 | |
4977 | |
4978 | |
4979 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { |
4980 | friend class ASTContext; |
4981 | |
4982 | ConceptDecl *TypeConstraintConcept; |
4983 | |
4984 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, |
4985 | TypeDependence ExtraDependence, ConceptDecl *CD, |
4986 | ArrayRef<TemplateArgument> TypeConstraintArgs); |
4987 | |
4988 | const TemplateArgument *getArgBuffer() const { |
4989 | return reinterpret_cast<const TemplateArgument*>(this+1); |
4990 | } |
4991 | |
4992 | TemplateArgument *getArgBuffer() { |
4993 | return reinterpret_cast<TemplateArgument*>(this+1); |
4994 | } |
4995 | |
4996 | public: |
4997 | |
4998 | const TemplateArgument *getArgs() const { |
4999 | return getArgBuffer(); |
5000 | } |
5001 | |
5002 | |
5003 | unsigned getNumArgs() const { |
5004 | return AutoTypeBits.NumArgs; |
5005 | } |
5006 | |
5007 | const TemplateArgument &getArg(unsigned Idx) const; |
5008 | |
5009 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { |
5010 | return {getArgs(), getNumArgs()}; |
5011 | } |
5012 | |
5013 | ConceptDecl *getTypeConstraintConcept() const { |
5014 | return TypeConstraintConcept; |
5015 | } |
5016 | |
5017 | bool isConstrained() const { |
5018 | return TypeConstraintConcept != nullptr; |
5019 | } |
5020 | |
5021 | bool isDecltypeAuto() const { |
5022 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; |
5023 | } |
5024 | |
5025 | AutoTypeKeyword getKeyword() const { |
5026 | return (AutoTypeKeyword)AutoTypeBits.Keyword; |
5027 | } |
5028 | |
5029 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5030 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), |
5031 | getTypeConstraintConcept(), getTypeConstraintArguments()); |
5032 | } |
5033 | |
5034 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
5035 | QualType Deduced, AutoTypeKeyword Keyword, |
5036 | bool IsDependent, ConceptDecl *CD, |
5037 | ArrayRef<TemplateArgument> Arguments); |
5038 | |
5039 | static bool classof(const Type *T) { |
5040 | return T->getTypeClass() == Auto; |
5041 | } |
5042 | }; |
5043 | |
5044 | |
5045 | class DeducedTemplateSpecializationType : public DeducedType, |
5046 | public llvm::FoldingSetNode { |
5047 | friend class ASTContext; |
5048 | |
5049 | |
5050 | TemplateName Template; |
5051 | |
5052 | DeducedTemplateSpecializationType(TemplateName Template, |
5053 | QualType DeducedAsType, |
5054 | bool IsDeducedAsDependent) |
5055 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, |
5056 | toTypeDependence(Template.getDependence()) | |
5057 | (IsDeducedAsDependent |
5058 | ? TypeDependence::DependentInstantiation |
5059 | : TypeDependence::None)), |
5060 | Template(Template) {} |
5061 | |
5062 | public: |
5063 | |
5064 | TemplateName getTemplateName() const { return Template;} |
5065 | |
5066 | void Profile(llvm::FoldingSetNodeID &ID) { |
5067 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); |
5068 | } |
5069 | |
5070 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, |
5071 | QualType Deduced, bool IsDependent) { |
5072 | Template.Profile(ID); |
5073 | ID.AddPointer(Deduced.getAsOpaquePtr()); |
5074 | ID.AddBoolean(IsDependent); |
5075 | } |
5076 | |
5077 | static bool classof(const Type *T) { |
5078 | return T->getTypeClass() == DeducedTemplateSpecialization; |
5079 | } |
5080 | }; |
5081 | |
5082 | |
5083 | |
5084 | |
5085 | |
5086 | |
5087 | |
5088 | |
5089 | |
5090 | |
5091 | |
5092 | |
5093 | |
5094 | |
5095 | |
5096 | |
5097 | |
5098 | |
5099 | |
5100 | |
5101 | |
5102 | class alignas(8) TemplateSpecializationType |
5103 | : public Type, |
5104 | public llvm::FoldingSetNode { |
5105 | friend class ASTContext; |
5106 | |
5107 | |
5108 | |
5109 | |
5110 | |
5111 | |
5112 | |
5113 | |
5114 | TemplateName Template; |
5115 | |
5116 | TemplateSpecializationType(TemplateName T, |
5117 | ArrayRef<TemplateArgument> Args, |
5118 | QualType Canon, |
5119 | QualType Aliased); |
5120 | |
5121 | public: |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | static bool |
5134 | anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, |
5135 | ArrayRef<TemplateArgument> Converted); |
5136 | static bool |
5137 | anyDependentTemplateArguments(const TemplateArgumentListInfo &, |
5138 | ArrayRef<TemplateArgument> Converted); |
5139 | static bool anyInstantiationDependentTemplateArguments( |
5140 | ArrayRef<TemplateArgumentLoc> Args); |
5141 | |
5142 | |
5143 | |
5144 | bool isCurrentInstantiation() const { |
5145 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); |
5146 | } |
5147 | |
5148 | |
5149 | |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | |
5155 | |
5156 | |
5157 | |
5158 | |
5159 | |
5160 | |
5161 | |
5162 | |
5163 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } |
5164 | |
5165 | |
5166 | |
5167 | QualType getAliasedType() const { |
5168 | assert(isTypeAlias() && "not a type alias template specialization"); |
5169 | return *reinterpret_cast<const QualType*>(end()); |
5170 | } |
5171 | |
5172 | using iterator = const TemplateArgument *; |
5173 | |
5174 | iterator begin() const { return getArgs(); } |
5175 | iterator end() const; |
5176 | |
5177 | |
5178 | TemplateName getTemplateName() const { return Template; } |
5179 | |
5180 | |
5181 | const TemplateArgument *getArgs() const { |
5182 | return reinterpret_cast<const TemplateArgument *>(this + 1); |
5183 | } |
5184 | |
5185 | |
5186 | unsigned getNumArgs() const { |
5187 | return TemplateSpecializationTypeBits.NumArgs; |
5188 | } |
5189 | |
5190 | |
5191 | |
5192 | const TemplateArgument &getArg(unsigned Idx) const; |
5193 | |
5194 | ArrayRef<TemplateArgument> template_arguments() const { |
5195 | return {getArgs(), getNumArgs()}; |
5196 | } |
5197 | |
5198 | bool isSugared() const { |
5199 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); |
5200 | } |
5201 | |
5202 | QualType desugar() const { |
5203 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); |
5204 | } |
5205 | |
5206 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
5207 | Profile(ID, Template, template_arguments(), Ctx); |
5208 | if (isTypeAlias()) |
5209 | getAliasedType().Profile(ID); |
5210 | } |
5211 | |
5212 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, |
5213 | ArrayRef<TemplateArgument> Args, |
5214 | const ASTContext &Context); |
5215 | |
5216 | static bool classof(const Type *T) { |
5217 | return T->getTypeClass() == TemplateSpecialization; |
5218 | } |
5219 | }; |
5220 | |
5221 | |
5222 | |
5223 | void printTemplateArgumentList(raw_ostream &OS, |
5224 | ArrayRef<TemplateArgument> Args, |
5225 | const PrintingPolicy &Policy, |
5226 | const TemplateParameterList *TPL = nullptr); |
5227 | |
5228 | void printTemplateArgumentList(raw_ostream &OS, |
5229 | ArrayRef<TemplateArgumentLoc> Args, |
5230 | const PrintingPolicy &Policy, |
5231 | const TemplateParameterList *TPL = nullptr); |
5232 | |
5233 | void printTemplateArgumentList(raw_ostream &OS, |
5234 | const TemplateArgumentListInfo &Args, |
5235 | const PrintingPolicy &Policy, |
5236 | const TemplateParameterList *TPL = nullptr); |
5237 | |
5238 | |
5239 | |
5240 | |
5241 | |
5242 | |
5243 | |
5244 | |
5245 | |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | |
5253 | |
5254 | |
5255 | class InjectedClassNameType : public Type { |
5256 | friend class ASTContext; |
5257 | friend class ASTNodeImporter; |
5258 | friend class ASTReader; |
5259 | |
5260 | |
5261 | template <class T> friend class serialization::AbstractTypeReader; |
5262 | |
5263 | CXXRecordDecl *Decl; |
5264 | |
5265 | |
5266 | |
5267 | |
5268 | |
5269 | |
5270 | |
5271 | |
5272 | |
5273 | |
5274 | QualType InjectedType; |
5275 | |
5276 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) |
5277 | : Type(InjectedClassName, QualType(), |
5278 | TypeDependence::DependentInstantiation), |
5279 | Decl(D), InjectedType(TST) { |
5280 | assert(isa<TemplateSpecializationType>(TST)); |
5281 | assert(!TST.hasQualifiers()); |
5282 | assert(TST->isDependentType()); |
5283 | } |
5284 | |
5285 | public: |
5286 | QualType getInjectedSpecializationType() const { return InjectedType; } |
5287 | |
5288 | const TemplateSpecializationType *getInjectedTST() const { |
5289 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); |
5290 | } |
5291 | |
5292 | TemplateName getTemplateName() const { |
5293 | return getInjectedTST()->getTemplateName(); |
5294 | } |
5295 | |
5296 | CXXRecordDecl *getDecl() const; |
5297 | |
5298 | bool isSugared() const { return false; } |
5299 | QualType desugar() const { return QualType(this, 0); } |
5300 | |
5301 | static bool classof(const Type *T) { |
5302 | return T->getTypeClass() == InjectedClassName; |
5303 | } |
5304 | }; |
5305 | |
5306 | |
5307 | enum TagTypeKind { |
5308 | |
5309 | TTK_Struct, |
5310 | |
5311 | |
5312 | TTK_Interface, |
5313 | |
5314 | |
5315 | TTK_Union, |
5316 | |
5317 | |
5318 | TTK_Class, |
5319 | |
5320 | |
5321 | TTK_Enum |
5322 | }; |
5323 | |
5324 | |
5325 | |
5326 | enum ElaboratedTypeKeyword { |
5327 | |
5328 | ETK_Struct, |
5329 | |
5330 | |
5331 | ETK_Interface, |
5332 | |
5333 | |
5334 | ETK_Union, |
5335 | |
5336 | |
5337 | ETK_Class, |
5338 | |
5339 | |
5340 | ETK_Enum, |
5341 | |
5342 | |
5343 | |
5344 | ETK_Typename, |
5345 | |
5346 | |
5347 | ETK_None |
5348 | }; |
5349 | |
5350 | |
5351 | |
5352 | |
5353 | |
5354 | class TypeWithKeyword : public Type { |
5355 | protected: |
5356 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, |
5357 | QualType Canonical, TypeDependence Dependence) |
5358 | : Type(tc, Canonical, Dependence) { |
5359 | TypeWithKeywordBits.Keyword = Keyword; |
5360 | } |
5361 | |
5362 | public: |
5363 | ElaboratedTypeKeyword getKeyword() const { |
5364 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); |
5365 | } |
5366 | |
5367 | |
5368 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); |
5369 | |
5370 | |
5371 | |
5372 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); |
5373 | |
5374 | |
5375 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); |
5376 | |
5377 | |
5378 | |
5379 | |
5380 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); |
5381 | |
5382 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); |
5383 | |
5384 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); |
5385 | |
5386 | static StringRef getTagTypeKindName(TagTypeKind Kind) { |
5387 | return getKeywordName(getKeywordForTagTypeKind(Kind)); |
5388 | } |
5389 | |
5390 | class CannotCastToThisType {}; |
5391 | static CannotCastToThisType classof(const Type *); |
5392 | }; |
5393 | |
5394 | |
5395 | |
5396 | |
5397 | |
5398 | |
5399 | |
5400 | |
5401 | |
5402 | class ElaboratedType final |
5403 | : public TypeWithKeyword, |
5404 | public llvm::FoldingSetNode, |
5405 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { |
5406 | friend class ASTContext; |
5407 | friend TrailingObjects; |
5408 | |
5409 | |
5410 | NestedNameSpecifier *NNS; |
5411 | |
5412 | |
5413 | QualType NamedType; |
5414 | |
5415 | |
5416 | |
5417 | |
5418 | |
5419 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5420 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) |
5421 | : TypeWithKeyword(Keyword, Elaborated, CanonType, |
5422 | |
5423 | |
5424 | |
5425 | |
5426 | NamedType->getDependence() | |
5427 | (NNS ? toSyntacticDependence( |
5428 | toTypeDependence(NNS->getDependence())) |
5429 | : TypeDependence::None)), |
5430 | NNS(NNS), NamedType(NamedType) { |
5431 | ElaboratedTypeBits.HasOwnedTagDecl = false; |
5432 | if (OwnedTagDecl) { |
5433 | ElaboratedTypeBits.HasOwnedTagDecl = true; |
5434 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; |
5435 | } |
5436 | assert(!(Keyword == ETK_None && NNS == nullptr) && |
5437 | "ElaboratedType cannot have elaborated type keyword " |
5438 | "and name qualifier both null."); |
5439 | } |
5440 | |
5441 | public: |
5442 | |
5443 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5444 | |
5445 | |
5446 | QualType getNamedType() const { return NamedType; } |
5447 | |
5448 | |
5449 | QualType desugar() const { return getNamedType(); } |
5450 | |
5451 | |
5452 | bool isSugared() const { return true; } |
5453 | |
5454 | |
5455 | |
5456 | TagDecl *getOwnedTagDecl() const { |
5457 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() |
5458 | : nullptr; |
5459 | } |
5460 | |
5461 | void Profile(llvm::FoldingSetNodeID &ID) { |
5462 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); |
5463 | } |
5464 | |
5465 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5466 | NestedNameSpecifier *NNS, QualType NamedType, |
5467 | TagDecl *OwnedTagDecl) { |
5468 | ID.AddInteger(Keyword); |
5469 | ID.AddPointer(NNS); |
5470 | NamedType.Profile(ID); |
5471 | ID.AddPointer(OwnedTagDecl); |
5472 | } |
5473 | |
5474 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } |
5475 | }; |
5476 | |
5477 | |
5478 | |
5479 | |
5480 | |
5481 | |
5482 | |
5483 | |
5484 | |
5485 | |
5486 | |
5487 | |
5488 | |
5489 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { |
5490 | friend class ASTContext; |
5491 | |
5492 | |
5493 | NestedNameSpecifier *NNS; |
5494 | |
5495 | |
5496 | const IdentifierInfo *Name; |
5497 | |
5498 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5499 | const IdentifierInfo *Name, QualType CanonType) |
5500 | : TypeWithKeyword(Keyword, DependentName, CanonType, |
5501 | TypeDependence::DependentInstantiation | |
5502 | toTypeDependence(NNS->getDependence())), |
5503 | NNS(NNS), Name(Name) {} |
5504 | |
5505 | public: |
5506 | |
5507 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5508 | |
5509 | |
5510 | |
5511 | |
5512 | |
5513 | |
5514 | const IdentifierInfo *getIdentifier() const { |
5515 | return Name; |
5516 | } |
5517 | |
5518 | bool isSugared() const { return false; } |
5519 | QualType desugar() const { return QualType(this, 0); } |
5520 | |
5521 | void Profile(llvm::FoldingSetNodeID &ID) { |
5522 | Profile(ID, getKeyword(), NNS, Name); |
5523 | } |
5524 | |
5525 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5526 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { |
5527 | ID.AddInteger(Keyword); |
5528 | ID.AddPointer(NNS); |
5529 | ID.AddPointer(Name); |
5530 | } |
5531 | |
5532 | static bool classof(const Type *T) { |
5533 | return T->getTypeClass() == DependentName; |
5534 | } |
5535 | }; |
5536 | |
5537 | |
5538 | |
5539 | |
5540 | class alignas(8) DependentTemplateSpecializationType |
5541 | : public TypeWithKeyword, |
5542 | public llvm::FoldingSetNode { |
5543 | friend class ASTContext; |
5544 | |
5545 | |
5546 | NestedNameSpecifier *NNS; |
5547 | |
5548 | |
5549 | const IdentifierInfo *Name; |
5550 | |
5551 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, |
5552 | NestedNameSpecifier *NNS, |
5553 | const IdentifierInfo *Name, |
5554 | ArrayRef<TemplateArgument> Args, |
5555 | QualType Canon); |
5556 | |
5557 | const TemplateArgument *getArgBuffer() const { |
5558 | return reinterpret_cast<const TemplateArgument*>(this+1); |
5559 | } |
5560 | |
5561 | TemplateArgument *getArgBuffer() { |
5562 | return reinterpret_cast<TemplateArgument*>(this+1); |
5563 | } |
5564 | |
5565 | public: |
5566 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5567 | const IdentifierInfo *getIdentifier() const { return Name; } |
5568 | |
5569 | |
5570 | const TemplateArgument *getArgs() const { |
5571 | return getArgBuffer(); |
5572 | } |
5573 | |
5574 | |
5575 | unsigned getNumArgs() const { |
5576 | return DependentTemplateSpecializationTypeBits.NumArgs; |
5577 | } |
5578 | |
5579 | const TemplateArgument &getArg(unsigned Idx) const; |
5580 | |
5581 | ArrayRef<TemplateArgument> template_arguments() const { |
5582 | return {getArgs(), getNumArgs()}; |
5583 | } |
5584 | |
5585 | using iterator = const TemplateArgument *; |
5586 | |
5587 | iterator begin() const { return getArgs(); } |
5588 | iterator end() const; |
5589 | |
5590 | bool isSugared() const { return false; } |
5591 | QualType desugar() const { return QualType(this, 0); } |
5592 | |
5593 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5594 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); |
5595 | } |
5596 | |
5597 | static void Profile(llvm::FoldingSetNodeID &ID, |
5598 | const ASTContext &Context, |
5599 | ElaboratedTypeKeyword Keyword, |
5600 | NestedNameSpecifier *Qualifier, |
5601 | const IdentifierInfo *Name, |
5602 | ArrayRef<TemplateArgument> Args); |
5603 | |
5604 | static bool classof(const Type *T) { |
5605 | return T->getTypeClass() == DependentTemplateSpecialization; |
5606 | } |
5607 | }; |
5608 | |
5609 | |
5610 | |
5611 | |
5612 | |
5613 | |
5614 | |
5615 | |
5616 | |
5617 | |
5618 | |
5619 | |
5620 | |
5621 | |
5622 | |
5623 | |
5624 | |
5625 | |
5626 | |
5627 | |
5628 | |
5629 | |
5630 | |
5631 | class PackExpansionType : public Type, public llvm::FoldingSetNode { |
5632 | friend class ASTContext; |
5633 | |
5634 | |
5635 | QualType Pattern; |
5636 | |
5637 | PackExpansionType(QualType Pattern, QualType Canon, |
5638 | Optional<unsigned> NumExpansions) |
5639 | : Type(PackExpansion, Canon, |
5640 | (Pattern->getDependence() | TypeDependence::Dependent | |
5641 | TypeDependence::Instantiation) & |
5642 | ~TypeDependence::UnexpandedPack), |
5643 | Pattern(Pattern) { |
5644 | PackExpansionTypeBits.NumExpansions = |
5645 | NumExpansions ? *NumExpansions + 1 : 0; |
5646 | } |
5647 | |
5648 | public: |
5649 | |
5650 | |
5651 | |
5652 | QualType getPattern() const { return Pattern; } |
5653 | |
5654 | |
5655 | |
5656 | Optional<unsigned> getNumExpansions() const { |
5657 | if (PackExpansionTypeBits.NumExpansions) |
5658 | return PackExpansionTypeBits.NumExpansions - 1; |
5659 | return None; |
5660 | } |
5661 | |
5662 | bool isSugared() const { return false; } |
5663 | QualType desugar() const { return QualType(this, 0); } |
5664 | |
5665 | void Profile(llvm::FoldingSetNodeID &ID) { |
5666 | Profile(ID, getPattern(), getNumExpansions()); |
5667 | } |
5668 | |
5669 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, |
5670 | Optional<unsigned> NumExpansions) { |
5671 | ID.AddPointer(Pattern.getAsOpaquePtr()); |
5672 | ID.AddBoolean(NumExpansions.hasValue()); |
5673 | if (NumExpansions) |
5674 | ID.AddInteger(*NumExpansions); |
5675 | } |
5676 | |
5677 | static bool classof(const Type *T) { |
5678 | return T->getTypeClass() == PackExpansion; |
5679 | } |
5680 | }; |
5681 | |
5682 | |
5683 | |
5684 | template <class T> |
5685 | class ObjCProtocolQualifiers { |
5686 | protected: |
5687 | ObjCProtocolQualifiers() = default; |
5688 | |
5689 | ObjCProtocolDecl * const *getProtocolStorage() const { |
5690 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); |
5691 | } |
5692 | |
5693 | ObjCProtocolDecl **getProtocolStorage() { |
5694 | return static_cast<T*>(this)->getProtocolStorageImpl(); |
5695 | } |
5696 | |
5697 | void setNumProtocols(unsigned N) { |
5698 | static_cast<T*>(this)->setNumProtocolsImpl(N); |
5699 | } |
5700 | |
5701 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { |
5702 | setNumProtocols(protocols.size()); |
5703 | assert(getNumProtocols() == protocols.size() && |
5704 | "bitfield overflow in protocol count"); |
5705 | if (!protocols.empty()) |
5706 | memcpy(getProtocolStorage(), protocols.data(), |
5707 | protocols.size() * sizeof(ObjCProtocolDecl*)); |
5708 | } |
5709 | |
5710 | public: |
5711 | using qual_iterator = ObjCProtocolDecl * const *; |
5712 | using qual_range = llvm::iterator_range<qual_iterator>; |
5713 | |
5714 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
5715 | qual_iterator qual_begin() const { return getProtocolStorage(); } |
5716 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } |
5717 | |
5718 | bool qual_empty() const { return getNumProtocols() == 0; } |
5719 | |
5720 | |
5721 | |
5722 | unsigned getNumProtocols() const { |
5723 | return static_cast<const T*>(this)->getNumProtocolsImpl(); |
5724 | } |
5725 | |
5726 | |
5727 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
5728 | assert(I < getNumProtocols() && "Out-of-range protocol access"); |
5729 | return qual_begin()[I]; |
5730 | } |
5731 | |
5732 | |
5733 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { |
5734 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); |
5735 | } |
5736 | }; |
5737 | |
5738 | |
5739 | |
5740 | class ObjCTypeParamType : public Type, |
5741 | public ObjCProtocolQualifiers<ObjCTypeParamType>, |
5742 | public llvm::FoldingSetNode { |
5743 | friend class ASTContext; |
5744 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; |
5745 | |
5746 | |
5747 | unsigned NumProtocols : 6; |
5748 | |
5749 | ObjCTypeParamDecl *OTPDecl; |
5750 | |
5751 | |
5752 | |
5753 | |
5754 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5755 | |
5756 | |
5757 | |
5758 | unsigned getNumProtocolsImpl() const { |
5759 | return NumProtocols; |
5760 | } |
5761 | |
5762 | void setNumProtocolsImpl(unsigned N) { |
5763 | NumProtocols = N; |
5764 | } |
5765 | |
5766 | ObjCTypeParamType(const ObjCTypeParamDecl *D, |
5767 | QualType can, |
5768 | ArrayRef<ObjCProtocolDecl *> protocols); |
5769 | |
5770 | public: |
5771 | bool isSugared() const { return true; } |
5772 | QualType desugar() const { return getCanonicalTypeInternal(); } |
5773 | |
5774 | static bool classof(const Type *T) { |
5775 | return T->getTypeClass() == ObjCTypeParam; |
5776 | } |
5777 | |
5778 | void Profile(llvm::FoldingSetNodeID &ID); |
5779 | static void Profile(llvm::FoldingSetNodeID &ID, |
5780 | const ObjCTypeParamDecl *OTPDecl, |
5781 | QualType CanonicalType, |
5782 | ArrayRef<ObjCProtocolDecl *> protocols); |
5783 | |
5784 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } |
5785 | }; |
5786 | |
5787 | |
5788 | |
5789 | |
5790 | |
5791 | |
5792 | |
5793 | |
5794 | |
5795 | |
5796 | |
5797 | |
5798 | |
5799 | |
5800 | |
5801 | |
5802 | |
5803 | |
5804 | |
5805 | |
5806 | |
5807 | |
5808 | |
5809 | |
5810 | |
5811 | |
5812 | |
5813 | |
5814 | |
5815 | class ObjCObjectType : public Type, |
5816 | public ObjCProtocolQualifiers<ObjCObjectType> { |
5817 | friend class ObjCProtocolQualifiers<ObjCObjectType>; |
5818 | |
5819 | |
5820 | |
5821 | |
5822 | |
5823 | |
5824 | |
5825 | |
5826 | |
5827 | |
5828 | |
5829 | |
5830 | |
5831 | |
5832 | QualType BaseType; |
5833 | |
5834 | |
5835 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> |
5836 | CachedSuperClassType; |
5837 | |
5838 | QualType *getTypeArgStorage(); |
5839 | const QualType *getTypeArgStorage() const { |
5840 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); |
5841 | } |
5842 | |
5843 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5844 | |
5845 | |
5846 | unsigned getNumProtocolsImpl() const { |
5847 | return ObjCObjectTypeBits.NumProtocols; |
5848 | } |
5849 | void setNumProtocolsImpl(unsigned N) { |
5850 | ObjCObjectTypeBits.NumProtocols = N; |
5851 | } |
5852 | |
5853 | protected: |
5854 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; |
5855 | |
5856 | ObjCObjectType(QualType Canonical, QualType Base, |
5857 | ArrayRef<QualType> typeArgs, |
5858 | ArrayRef<ObjCProtocolDecl *> protocols, |
5859 | bool isKindOf); |
5860 | |
5861 | ObjCObjectType(enum Nonce_ObjCInterface) |
5862 | : Type(ObjCInterface, QualType(), TypeDependence::None), |
5863 | BaseType(QualType(this_(), 0)) { |
5864 | ObjCObjectTypeBits.NumProtocols = 0; |
5865 | ObjCObjectTypeBits.NumTypeArgs = 0; |
5866 | ObjCObjectTypeBits.IsKindOf = 0; |
5867 | } |
5868 | |
5869 | void computeSuperClassTypeSlow() const; |
5870 | |
5871 | public: |
5872 | |
5873 | |
5874 | |
5875 | |
5876 | |
5877 | |
5878 | QualType getBaseType() const { return BaseType; } |
5879 | |
5880 | bool isObjCId() const { |
5881 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); |
5882 | } |
5883 | |
5884 | bool isObjCClass() const { |
5885 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); |
5886 | } |
5887 | |
5888 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } |
5889 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } |
5890 | bool isObjCUnqualifiedIdOrClass() const { |
5891 | if (!qual_empty()) return false; |
5892 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) |
5893 | return T->getKind() == BuiltinType::ObjCId || |
5894 | T->getKind() == BuiltinType::ObjCClass; |
5895 | return false; |
5896 | } |
5897 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } |
5898 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } |
5899 | |
5900 | |
5901 | |
5902 | ObjCInterfaceDecl *getInterface() const; |
5903 | |
5904 | |
5905 | |
5906 | bool isSpecialized() const; |
5907 | |
5908 | |
5909 | bool isSpecializedAsWritten() const { |
5910 | return ObjCObjectTypeBits.NumTypeArgs > 0; |
5911 | } |
5912 | |
5913 | |
5914 | |
5915 | bool isUnspecialized() const { return !isSpecialized(); } |
5916 | |
5917 | |
5918 | |
5919 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
5920 | |
5921 | |
5922 | ArrayRef<QualType> getTypeArgs() const; |
5923 | |
5924 | |
5925 | |
5926 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
5927 | return llvm::makeArrayRef(getTypeArgStorage(), |
5928 | ObjCObjectTypeBits.NumTypeArgs); |
5929 | } |
5930 | |
5931 | |
5932 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } |
5933 | |
5934 | |
5935 | bool isKindOfType() const; |
5936 | |
5937 | |
5938 | |
5939 | |
5940 | |
5941 | |
5942 | |
5943 | QualType getSuperClassType() const { |
5944 | if (!CachedSuperClassType.getInt()) |
5945 | computeSuperClassTypeSlow(); |
5946 | |
5947 | assert(CachedSuperClassType.getInt() && "Superclass not set?"); |
5948 | return QualType(CachedSuperClassType.getPointer(), 0); |
5949 | } |
5950 | |
5951 | |
5952 | |
5953 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; |
5954 | |
5955 | bool isSugared() const { return false; } |
5956 | QualType desugar() const { return QualType(this, 0); } |
5957 | |
5958 | static bool classof(const Type *T) { |
5959 | return T->getTypeClass() == ObjCObject || |
5960 | T->getTypeClass() == ObjCInterface; |
5961 | } |
5962 | }; |
5963 | |
5964 | |
5965 | |
5966 | |
5967 | |
5968 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { |
5969 | friend class ASTContext; |
5970 | |
5971 | |
5972 | |
5973 | |
5974 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, |
5975 | ArrayRef<QualType> typeArgs, |
5976 | ArrayRef<ObjCProtocolDecl *> protocols, |
5977 | bool isKindOf) |
5978 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} |
5979 | |
5980 | public: |
5981 | void Profile(llvm::FoldingSetNodeID &ID); |
5982 | static void Profile(llvm::FoldingSetNodeID &ID, |
5983 | QualType Base, |
5984 | ArrayRef<QualType> typeArgs, |
5985 | ArrayRef<ObjCProtocolDecl *> protocols, |
5986 | bool isKindOf); |
5987 | }; |
5988 | |
5989 | inline QualType *ObjCObjectType::getTypeArgStorage() { |
5990 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); |
5991 | } |
5992 | |
5993 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { |
5994 | return reinterpret_cast<ObjCProtocolDecl**>( |
5995 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); |
5996 | } |
5997 | |
5998 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { |
5999 | return reinterpret_cast<ObjCProtocolDecl**>( |
6000 | static_cast<ObjCTypeParamType*>(this)+1); |
6001 | } |
6002 | |
6003 | |
6004 | |
6005 | |
6006 | |
6007 | |
6008 | |
6009 | |
6010 | |
6011 | |
6012 | |
6013 | |
6014 | |
6015 | class ObjCInterfaceType : public ObjCObjectType { |
6016 | friend class ASTContext; |
6017 | friend class ASTReader; |
6018 | friend class ObjCInterfaceDecl; |
6019 | template <class T> friend class serialization::AbstractTypeReader; |
6020 | |
6021 | mutable ObjCInterfaceDecl *Decl; |
6022 | |
6023 | ObjCInterfaceType(const ObjCInterfaceDecl *D) |
6024 | : ObjCObjectType(Nonce_ObjCInterface), |
6025 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} |
6026 | |
6027 | public: |
6028 | |
6029 | ObjCInterfaceDecl *getDecl() const { return Decl; } |
6030 | |
6031 | bool isSugared() const { return false; } |
6032 | QualType desugar() const { return QualType(this, 0); } |
6033 | |
6034 | static bool classof(const Type *T) { |
6035 | return T->getTypeClass() == ObjCInterface; |
6036 | } |
6037 | |
6038 | |
6039 | |
6040 | |
6041 | |
6042 | enum { |
6043 | qual_iterator, |
6044 | qual_begin, |
6045 | qual_end, |
6046 | getNumProtocols, |
6047 | getProtocol |
6048 | }; |
6049 | }; |
6050 | |
6051 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { |
6052 | QualType baseType = getBaseType(); |
6053 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { |
6054 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) |
6055 | return T->getDecl(); |
6056 | |
6057 | baseType = ObjT->getBaseType(); |
6058 | } |
6059 | |
6060 | return nullptr; |
6061 | } |
6062 | |
6063 | |
6064 | |
6065 | |
6066 | |
6067 | |
6068 | |
6069 | |
6070 | |
6071 | |
6072 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { |
6073 | friend class ASTContext; |
6074 | |
6075 | QualType PointeeType; |
6076 | |
6077 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) |
6078 | : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), |
6079 | PointeeType(Pointee) {} |
6080 | |
6081 | public: |
6082 | |
6083 | |
6084 | QualType getPointeeType() const { return PointeeType; } |
6085 | |
6086 | |
6087 | |
6088 | |
6089 | |
6090 | |
6091 | |
6092 | |
6093 | |
6094 | |
6095 | |
6096 | |
6097 | |
6098 | |
6099 | |
6100 | |
6101 | |
6102 | |
6103 | |
6104 | |
6105 | |
6106 | |
6107 | |
6108 | |
6109 | const ObjCObjectType *getObjectType() const { |
6110 | return PointeeType->castAs<ObjCObjectType>(); |
6111 | } |
6112 | |
6113 | |
6114 | |
6115 | |
6116 | |
6117 | |
6118 | const ObjCInterfaceType *getInterfaceType() const; |
6119 | |
6120 | |
6121 | |
6122 | |
6123 | |
6124 | ObjCInterfaceDecl *getInterfaceDecl() const { |
6125 | return getObjectType()->getInterface(); |
6126 | } |
6127 | |
6128 | |
6129 | |
6130 | bool isObjCIdType() const { |
6131 | return getObjectType()->isObjCUnqualifiedId(); |
6132 | } |
6133 | |
6134 | |
6135 | |
6136 | bool isObjCClassType() const { |
6137 | return getObjectType()->isObjCUnqualifiedClass(); |
6138 | } |
6139 | |
6140 | |
6141 | bool isObjCIdOrClassType() const { |
6142 | return getObjectType()->isObjCUnqualifiedIdOrClass(); |
6143 | } |
6144 | |
6145 | |
6146 | |
6147 | bool isObjCQualifiedIdType() const { |
6148 | return getObjectType()->isObjCQualifiedId(); |
6149 | } |
6150 | |
6151 | |
6152 | |
6153 | bool isObjCQualifiedClassType() const { |
6154 | return getObjectType()->isObjCQualifiedClass(); |
6155 | } |
6156 | |
6157 | |
6158 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } |
6159 | |
6160 | |
6161 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } |
6162 | |
6163 | |
6164 | bool isSpecializedAsWritten() const { |
6165 | return getObjectType()->isSpecializedAsWritten(); |
6166 | } |
6167 | |
6168 | |
6169 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } |
6170 | |
6171 | |
6172 | |
6173 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
6174 | |
6175 | |
6176 | ArrayRef<QualType> getTypeArgs() const { |
6177 | return getObjectType()->getTypeArgs(); |
6178 | } |
6179 | |
6180 | |
6181 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
6182 | return getObjectType()->getTypeArgsAsWritten(); |
6183 | } |
6184 | |
6185 | |
6186 | |
6187 | |
6188 | using qual_iterator = ObjCObjectType::qual_iterator; |
6189 | using qual_range = llvm::iterator_range<qual_iterator>; |
6190 | |
6191 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
6192 | |
6193 | qual_iterator qual_begin() const { |
6194 | return getObjectType()->qual_begin(); |
6195 | } |
6196 | |
6197 | qual_iterator qual_end() const { |
6198 | return getObjectType()->qual_end(); |
6199 | } |
6200 | |
6201 | bool qual_empty() const { return getObjectType()->qual_empty(); } |
6202 | |
6203 | |
6204 | unsigned getNumProtocols() const { |
6205 | return getObjectType()->getNumProtocols(); |
6206 | } |
6207 | |
6208 | |
6209 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
6210 | return getObjectType()->getProtocol(I); |
6211 | } |
6212 | |
6213 | bool isSugared() const { return false; } |
6214 | QualType desugar() const { return QualType(this, 0); } |
6215 | |
6216 | |
6217 | |
6218 | |
6219 | |
6220 | |
6221 | |
6222 | QualType getSuperClassType() const; |
6223 | |
6224 | |
6225 | |
6226 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( |
6227 | const ASTContext &ctx) const; |
6228 | |
6229 | void Profile(llvm::FoldingSetNodeID &ID) { |
6230 | Profile(ID, getPointeeType()); |
6231 | } |
6232 | |
6233 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6234 | ID.AddPointer(T.getAsOpaquePtr()); |
6235 | } |
6236 | |
6237 | static bool classof(const Type *T) { |
6238 | return T->getTypeClass() == ObjCObjectPointer; |
6239 | } |
6240 | }; |
6241 | |
6242 | class AtomicType : public Type, public llvm::FoldingSetNode { |
6243 | friend class ASTContext; |
6244 | |
6245 | QualType ValueType; |
6246 | |
6247 | AtomicType(QualType ValTy, QualType Canonical) |
6248 | : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} |
6249 | |
6250 | public: |
6251 | |
6252 | |
6253 | QualType getValueType() const { return ValueType; } |
6254 | |
6255 | bool isSugared() const { return false; } |
6256 | QualType desugar() const { return QualType(this, 0); } |
6257 | |
6258 | void Profile(llvm::FoldingSetNodeID &ID) { |
6259 | Profile(ID, getValueType()); |
6260 | } |
6261 | |
6262 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6263 | ID.AddPointer(T.getAsOpaquePtr()); |
6264 | } |
6265 | |
6266 | static bool classof(const Type *T) { |
6267 | return T->getTypeClass() == Atomic; |
6268 | } |
6269 | }; |
6270 | |
6271 | |
6272 | class PipeType : public Type, public llvm::FoldingSetNode { |
6273 | friend class ASTContext; |
6274 | |
6275 | QualType ElementType; |
6276 | bool isRead; |
6277 | |
6278 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) |
6279 | : Type(Pipe, CanonicalPtr, elemType->getDependence()), |
6280 | ElementType(elemType), isRead(isRead) {} |
6281 | |
6282 | public: |
6283 | QualType getElementType() const { return ElementType; } |
6284 | |
6285 | bool isSugared() const { return false; } |
6286 | |
6287 | QualType desugar() const { return QualType(this, 0); } |
6288 | |
6289 | void Profile(llvm::FoldingSetNodeID &ID) { |
6290 | Profile(ID, getElementType(), isReadOnly()); |
6291 | } |
6292 | |
6293 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { |
6294 | ID.AddPointer(T.getAsOpaquePtr()); |
6295 | ID.AddBoolean(isRead); |
6296 | } |
6297 | |
6298 | static bool classof(const Type *T) { |
6299 | return T->getTypeClass() == Pipe; |
6300 | } |
6301 | |
6302 | bool isReadOnly() const { return isRead; } |
6303 | }; |
6304 | |
6305 | |
6306 | class ExtIntType final : public Type, public llvm::FoldingSetNode { |
6307 | friend class ASTContext; |
6308 | unsigned IsUnsigned : 1; |
6309 | unsigned NumBits : 24; |
6310 | |
6311 | protected: |
6312 | ExtIntType(bool isUnsigned, unsigned NumBits); |
6313 | |
6314 | public: |
6315 | bool isUnsigned() const { return IsUnsigned; } |
6316 | bool isSigned() const { return !IsUnsigned; } |
6317 | unsigned getNumBits() const { return NumBits; } |
6318 | |
6319 | bool isSugared() const { return false; } |
6320 | QualType desugar() const { return QualType(this, 0); } |
6321 | |
6322 | void Profile(llvm::FoldingSetNodeID &ID) { |
6323 | Profile(ID, isUnsigned(), getNumBits()); |
6324 | } |
6325 | |
6326 | static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, |
6327 | unsigned NumBits) { |
6328 | ID.AddBoolean(IsUnsigned); |
6329 | ID.AddInteger(NumBits); |
6330 | } |
6331 | |
6332 | static bool classof(const Type *T) { return T->getTypeClass() == ExtInt; } |
6333 | }; |
6334 | |
6335 | class DependentExtIntType final : public Type, public llvm::FoldingSetNode { |
6336 | friend class ASTContext; |
6337 | const ASTContext &Context; |
6338 | llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; |
6339 | |
6340 | protected: |
6341 | DependentExtIntType(const ASTContext &Context, bool IsUnsigned, |
6342 | Expr *NumBits); |
6343 | |
6344 | public: |
6345 | bool isUnsigned() const; |
6346 | bool isSigned() const { return !isUnsigned(); } |
6347 | Expr *getNumBitsExpr() const; |
6348 | |
6349 | bool isSugared() const { return false; } |
6350 | QualType desugar() const { return QualType(this, 0); } |
6351 | |
6352 | void Profile(llvm::FoldingSetNodeID &ID) { |
6353 | Profile(ID, Context, isUnsigned(), getNumBitsExpr()); |
6354 | } |
6355 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
6356 | bool IsUnsigned, Expr *NumBitsExpr); |
6357 | |
6358 | static bool classof(const Type *T) { |
6359 | return T->getTypeClass() == DependentExtInt; |
6360 | } |
6361 | }; |
6362 | |
6363 | |
6364 | class QualifierCollector : public Qualifiers { |
6365 | public: |
6366 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} |
6367 | |
6368 | |
6369 | |
6370 | |
6371 | const Type *strip(QualType type) { |
6372 | addFastQualifiers(type.getLocalFastQualifiers()); |
6373 | if (!type.hasLocalNonFastQualifiers()) |
6374 | return type.getTypePtrUnsafe(); |
6375 | |
6376 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); |
6377 | addConsistentQualifiers(extQuals->getQualifiers()); |
6378 | return extQuals->getBaseType(); |
6379 | } |
6380 | |
6381 | |
6382 | QualType apply(const ASTContext &Context, QualType QT) const; |
6383 | |
6384 | |
6385 | QualType apply(const ASTContext &Context, const Type* T) const; |
6386 | }; |
6387 | |
6388 | |
6389 | |
6390 | |
6391 | |
6392 | |
6393 | |
6394 | |
6395 | class alignas(8) TypeSourceInfo { |
6396 | |
6397 | |
6398 | friend class ASTContext; |
6399 | |
6400 | QualType Ty; |
6401 | |
6402 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
6403 | |
6404 | public: |
6405 | |
6406 | QualType getType() const { return Ty; } |
6407 | |
6408 | |
6409 | TypeLoc getTypeLoc() const; |
6410 | |
6411 | |
6412 | void overrideType(QualType T) { Ty = T; } |
6413 | }; |
6414 | |
6415 | |
6416 | |
6417 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { |
6418 | SplitQualType desugar = |
6419 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); |
6420 | desugar.Quals.addConsistentQualifiers(Quals); |
6421 | return desugar; |
6422 | } |
6423 | |
6424 | inline const Type *QualType::getTypePtr() const { |
6425 | return getCommonPtr()->BaseType; |
6426 | } |
6427 | |
6428 | inline const Type *QualType::getTypePtrOrNull() const { |
6429 | return (isNull() ? nullptr : getCommonPtr()->BaseType); |
6430 | } |
6431 | |
6432 | inline SplitQualType QualType::split() const { |
6433 | if (!hasLocalNonFastQualifiers()) |
6434 | return SplitQualType(getTypePtrUnsafe(), |
6435 | Qualifiers::fromFastMask(getLocalFastQualifiers())); |
6436 | |
6437 | const ExtQuals *eq = getExtQualsUnsafe(); |
6438 | Qualifiers qs = eq->getQualifiers(); |
6439 | qs.addFastQualifiers(getLocalFastQualifiers()); |
6440 | return SplitQualType(eq->getBaseType(), qs); |
6441 | } |
6442 | |
6443 | inline Qualifiers QualType::getLocalQualifiers() const { |
6444 | Qualifiers Quals; |
6445 | if (hasLocalNonFastQualifiers()) |
6446 | Quals = getExtQualsUnsafe()->getQualifiers(); |
6447 | Quals.addFastQualifiers(getLocalFastQualifiers()); |
6448 | return Quals; |
6449 | } |
6450 | |
6451 | inline Qualifiers QualType::getQualifiers() const { |
6452 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); |
6453 | quals.addFastQualifiers(getLocalFastQualifiers()); |
6454 | return quals; |
6455 | } |
6456 | |
6457 | inline unsigned QualType::getCVRQualifiers() const { |
6458 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); |
6459 | cvr |= getLocalCVRQualifiers(); |
6460 | return cvr; |
6461 | } |
6462 | |
6463 | inline QualType QualType::getCanonicalType() const { |
6464 | QualType canon = getCommonPtr()->CanonicalType; |
6465 | return canon.withFastQualifiers(getLocalFastQualifiers()); |
6466 | } |
6467 | |
6468 | inline bool QualType::isCanonical() const { |
6469 | return getTypePtr()->isCanonicalUnqualified(); |
6470 | } |
6471 | |
6472 | inline bool QualType::isCanonicalAsParam() const { |
6473 | if (!isCanonical()) return false; |
6474 | if (hasLocalQualifiers()) return false; |
6475 | |
6476 | const Type *T = getTypePtr(); |
6477 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) |
6478 | return false; |
6479 | |
6480 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); |
6481 | } |
6482 | |
6483 | inline bool QualType::isConstQualified() const { |
6484 | return isLocalConstQualified() || |
6485 | getCommonPtr()->CanonicalType.isLocalConstQualified(); |
6486 | } |
6487 | |
6488 | inline bool QualType::isRestrictQualified() const { |
6489 | return isLocalRestrictQualified() || |
6490 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); |
6491 | } |
6492 | |
6493 | |
6494 | inline bool QualType::isVolatileQualified() const { |
6495 | return isLocalVolatileQualified() || |
6496 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); |
6497 | } |
6498 | |
6499 | inline bool QualType::hasQualifiers() const { |
6500 | return hasLocalQualifiers() || |
6501 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); |
6502 | } |
6503 | |
6504 | inline QualType QualType::getUnqualifiedType() const { |
6505 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6506 | return QualType(getTypePtr(), 0); |
6507 | |
6508 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); |
6509 | } |
6510 | |
6511 | inline SplitQualType QualType::getSplitUnqualifiedType() const { |
6512 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6513 | return split(); |
6514 | |
6515 | return getSplitUnqualifiedTypeImpl(*this); |
6516 | } |
6517 | |
6518 | inline void QualType::removeLocalConst() { |
6519 | removeLocalFastQualifiers(Qualifiers::Const); |
6520 | } |
6521 | |
6522 | inline void QualType::removeLocalRestrict() { |
6523 | removeLocalFastQualifiers(Qualifiers::Restrict); |
6524 | } |
6525 | |
6526 | inline void QualType::removeLocalVolatile() { |
6527 | removeLocalFastQualifiers(Qualifiers::Volatile); |
6528 | } |
6529 | |
6530 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { |
6531 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); |
6532 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, |
6533 | "Fast bits differ from CVR bits!"); |
6534 | |
6535 | |
6536 | removeLocalFastQualifiers(Mask); |
6537 | } |
6538 | |
6539 | |
6540 | inline bool QualType::hasAddressSpace() const { |
6541 | return getQualifiers().hasAddressSpace(); |
6542 | } |
6543 | |
6544 | |
6545 | inline LangAS QualType::getAddressSpace() const { |
6546 | return getQualifiers().getAddressSpace(); |
6547 | } |
6548 | |
6549 | |
6550 | inline Qualifiers::GC QualType::getObjCGCAttr() const { |
6551 | return getQualifiers().getObjCGCAttr(); |
6552 | } |
6553 | |
6554 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
6555 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6556 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); |
6557 | return false; |
6558 | } |
6559 | |
6560 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { |
6561 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6562 | return hasNonTrivialToPrimitiveDestructCUnion(RD); |
6563 | return false; |
6564 | } |
6565 | |
6566 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { |
6567 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6568 | return hasNonTrivialToPrimitiveCopyCUnion(RD); |
6569 | return false; |
6570 | } |
6571 | |
6572 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { |
6573 | if (const auto *PT = t.getAs<PointerType>()) { |
6574 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) |
6575 | return FT->getExtInfo(); |
6576 | } else if (const auto *FT = t.getAs<FunctionType>()) |
6577 | return FT->getExtInfo(); |
6578 | |
6579 | return FunctionType::ExtInfo(); |
6580 | } |
6581 | |
6582 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { |
6583 | return getFunctionExtInfo(*t); |
6584 | } |
6585 | |
6586 | |
6587 | |
6588 | |
6589 | |
6590 | |
6591 | inline bool QualType::isMoreQualifiedThan(QualType other) const { |
6592 | Qualifiers MyQuals = getQualifiers(); |
6593 | Qualifiers OtherQuals = other.getQualifiers(); |
6594 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); |
6595 | } |
6596 | |
6597 | |
6598 | |
6599 | |
6600 | |
6601 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { |
6602 | Qualifiers OtherQuals = other.getQualifiers(); |
6603 | |
6604 | |
6605 | if (getUnqualifiedType()->isVoidType()) |
6606 | OtherQuals.removeUnaligned(); |
6607 | |
6608 | return getQualifiers().compatiblyIncludes(OtherQuals); |
6609 | } |
6610 | |
6611 | |
6612 | |
6613 | |
6614 | |
6615 | |
6616 | |
6617 | |
6618 | |
6619 | |
6620 | inline QualType QualType::getNonReferenceType() const { |
6621 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) |
6622 | return RefType->getPointeeType(); |
6623 | else |
6624 | return *this; |
6625 | } |
6626 | |
6627 | inline bool QualType::isCForbiddenLValueType() const { |
6628 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || |
6629 | getTypePtr()->isFunctionType()); |
6630 | } |
6631 | |
6632 | |
6633 | |
6634 | |
6635 | inline bool Type::isFundamentalType() const { |
6636 | return isVoidType() || |
6637 | isNullPtrType() || |
6638 | |
6639 | |
6640 | (isArithmeticType() && !isEnumeralType()); |
6641 | } |
6642 | |
6643 | |
6644 | |
6645 | |
6646 | inline bool Type::isCompoundType() const { |
6647 | |
6648 | |
6649 | |
6650 | return isArrayType() || |
6651 | |
6652 | isFunctionType() || |
6653 | |
6654 | isPointerType() || |
6655 | |
6656 | isReferenceType() || |
6657 | |
6658 | isRecordType() || |
6659 | |
6660 | |
6661 | isUnionType() || |
6662 | |
6663 | isEnumeralType() || |
6664 | |
6665 | isMemberPointerType(); |
6666 | } |
6667 | |
6668 | inline bool Type::isFunctionType() const { |
6669 | return isa<FunctionType>(CanonicalType); |
6670 | } |
6671 | |
6672 | inline bool Type::isPointerType() const { |
6673 | return isa<PointerType>(CanonicalType); |
6674 | } |
6675 | |
6676 | inline bool Type::isAnyPointerType() const { |
6677 | return isPointerType() || isObjCObjectPointerType(); |
6678 | } |
6679 | |
6680 | inline bool Type::isBlockPointerType() const { |
6681 | return isa<BlockPointerType>(CanonicalType); |
6682 | } |
6683 | |
6684 | inline bool Type::isReferenceType() const { |
6685 | return isa<ReferenceType>(CanonicalType); |
6686 | } |
6687 | |
6688 | inline bool Type::isLValueReferenceType() const { |
6689 | return isa<LValueReferenceType>(CanonicalType); |
6690 | } |
6691 | |
6692 | inline bool Type::isRValueReferenceType() const { |
6693 | return isa<RValueReferenceType>(CanonicalType); |
6694 | } |
6695 | |
6696 | inline bool Type::isObjectPointerType() const { |
6697 | |
6698 | |
6699 | |
6700 | if (const auto *T = getAs<PointerType>()) |
6701 | return !T->getPointeeType()->isFunctionType(); |
6702 | else |
6703 | return false; |
6704 | } |
6705 | |
6706 | inline bool Type::isFunctionPointerType() const { |
6707 | if (const auto *T = getAs<PointerType>()) |
6708 | return T->getPointeeType()->isFunctionType(); |
6709 | else |
6710 | return false; |
6711 | } |
6712 | |
6713 | inline bool Type::isFunctionReferenceType() const { |
6714 | if (const auto *T = getAs<ReferenceType>()) |
6715 | return T->getPointeeType()->isFunctionType(); |
6716 | else |
6717 | return false; |
6718 | } |
6719 | |
6720 | inline bool Type::isMemberPointerType() const { |
6721 | return isa<MemberPointerType>(CanonicalType); |
6722 | } |
6723 | |
6724 | inline bool Type::isMemberFunctionPointerType() const { |
6725 | if (const auto *T = getAs<MemberPointerType>()) |
6726 | return T->isMemberFunctionPointer(); |
6727 | else |
6728 | return false; |
6729 | } |
6730 | |
6731 | inline bool Type::isMemberDataPointerType() const { |
6732 | if (const auto *T = getAs<MemberPointerType>()) |
6733 | return T->isMemberDataPointer(); |
6734 | else |
6735 | return false; |
6736 | } |
6737 | |
6738 | inline bool Type::isArrayType() const { |
6739 | return isa<ArrayType>(CanonicalType); |
6740 | } |
6741 | |
6742 | inline bool Type::isConstantArrayType() const { |
6743 | return isa<ConstantArrayType>(CanonicalType); |
6744 | } |
6745 | |
6746 | inline bool Type::isIncompleteArrayType() const { |
6747 | return isa<IncompleteArrayType>(CanonicalType); |
6748 | } |
6749 | |
6750 | inline bool Type::isVariableArrayType() const { |
6751 | return isa<VariableArrayType>(CanonicalType); |
6752 | } |
6753 | |
6754 | inline bool Type::isDependentSizedArrayType() const { |
6755 | return isa<DependentSizedArrayType>(CanonicalType); |
6756 | } |
6757 | |
6758 | inline bool Type::isBuiltinType() const { |
6759 | return isa<BuiltinType>(CanonicalType); |
6760 | } |
6761 | |
6762 | inline bool Type::isRecordType() const { |
6763 | return isa<RecordType>(CanonicalType); |
6764 | } |
6765 | |
6766 | inline bool Type::isEnumeralType() const { |
6767 | return isa<EnumType>(CanonicalType); |
6768 | } |
6769 | |
6770 | inline bool Type::isAnyComplexType() const { |
6771 | return isa<ComplexType>(CanonicalType); |
6772 | } |
6773 | |
6774 | inline bool Type::isVectorType() const { |
6775 | return isa<VectorType>(CanonicalType); |
6776 | } |
6777 | |
6778 | inline bool Type::isExtVectorType() const { |
6779 | return isa<ExtVectorType>(CanonicalType); |
6780 | } |
6781 | |
6782 | inline bool Type::isMatrixType() const { |
6783 | return isa<MatrixType>(CanonicalType); |
6784 | } |
6785 | |
6786 | inline bool Type::isConstantMatrixType() const { |
6787 | return isa<ConstantMatrixType>(CanonicalType); |
6788 | } |
6789 | |
6790 | inline bool Type::isDependentAddressSpaceType() const { |
6791 | return isa<DependentAddressSpaceType>(CanonicalType); |
6792 | } |
6793 | |
6794 | inline bool Type::isObjCObjectPointerType() const { |
6795 | return isa<ObjCObjectPointerType>(CanonicalType); |
6796 | } |
6797 | |
6798 | inline bool Type::isObjCObjectType() const { |
6799 | return isa<ObjCObjectType>(CanonicalType); |
6800 | } |
6801 | |
6802 | inline bool Type::isObjCObjectOrInterfaceType() const { |
6803 | return isa<ObjCInterfaceType>(CanonicalType) || |
6804 | isa<ObjCObjectType>(CanonicalType); |
6805 | } |
6806 | |
6807 | inline bool Type::isAtomicType() const { |
6808 | return isa<AtomicType>(CanonicalType); |
6809 | } |
6810 | |
6811 | inline bool Type::isUndeducedAutoType() const { |
6812 | return isa<AutoType>(CanonicalType); |
6813 | } |
6814 | |
6815 | inline bool Type::isObjCQualifiedIdType() const { |
6816 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6817 | return OPT->isObjCQualifiedIdType(); |
6818 | return false; |
6819 | } |
6820 | |
6821 | inline bool Type::isObjCQualifiedClassType() const { |
6822 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6823 | return OPT->isObjCQualifiedClassType(); |
6824 | return false; |
6825 | } |
6826 | |
6827 | inline bool Type::isObjCIdType() const { |
6828 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6829 | return OPT->isObjCIdType(); |
6830 | return false; |
6831 | } |
6832 | |
6833 | inline bool Type::isObjCClassType() const { |
6834 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6835 | return OPT->isObjCClassType(); |
6836 | return false; |
6837 | } |
6838 | |
6839 | inline bool Type::isObjCSelType() const { |
6840 | if (const auto *OPT = getAs<PointerType>()) |
6841 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); |
6842 | return false; |
6843 | } |
6844 | |
6845 | inline bool Type::isObjCBuiltinType() const { |
6846 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); |
6847 | } |
6848 | |
6849 | inline bool Type::isDecltypeType() const { |
6850 | return isa<DecltypeType>(this); |
6851 | } |
6852 | |
6853 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
6854 | inline bool Type::is##Id##Type() const { \ |
6855 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6856 | } |
6857 | #include "clang/Basic/OpenCLImageTypes.def" |
6858 | |
6859 | inline bool Type::isSamplerT() const { |
6860 | return isSpecificBuiltinType(BuiltinType::OCLSampler); |
6861 | } |
6862 | |
6863 | inline bool Type::isEventT() const { |
6864 | return isSpecificBuiltinType(BuiltinType::OCLEvent); |
6865 | } |
6866 | |
6867 | inline bool Type::isClkEventT() const { |
6868 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); |
6869 | } |
6870 | |
6871 | inline bool Type::isQueueT() const { |
6872 | return isSpecificBuiltinType(BuiltinType::OCLQueue); |
6873 | } |
6874 | |
6875 | inline bool Type::isReserveIDT() const { |
6876 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); |
6877 | } |
6878 | |
6879 | inline bool Type::isImageType() const { |
6880 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || |
6881 | return |
6882 | #include "clang/Basic/OpenCLImageTypes.def" |
6883 | false; |
6884 | } |
6885 | |
6886 | inline bool Type::isPipeType() const { |
6887 | return isa<PipeType>(CanonicalType); |
6888 | } |
6889 | |
6890 | inline bool Type::isExtIntType() const { |
6891 | return isa<ExtIntType>(CanonicalType); |
6892 | } |
6893 | |
6894 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
6895 | inline bool Type::is##Id##Type() const { \ |
6896 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6897 | } |
6898 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6899 | |
6900 | inline bool Type::isOCLIntelSubgroupAVCType() const { |
6901 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ |
6902 | isOCLIntelSubgroupAVC##Id##Type() || |
6903 | return |
6904 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6905 | false; |
6906 | } |
6907 | |
6908 | inline bool Type::isOCLExtOpaqueType() const { |
6909 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || |
6910 | return |
6911 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6912 | false; |
6913 | } |
6914 | |
6915 | inline bool Type::isOpenCLSpecificType() const { |
6916 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || |
6917 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); |
6918 | } |
6919 | |
6920 | inline bool Type::isTemplateTypeParmType() const { |
6921 | return isa<TemplateTypeParmType>(CanonicalType); |
6922 | } |
6923 | |
6924 | inline bool Type::isSpecificBuiltinType(unsigned K) const { |
6925 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
6926 | return BT->getKind() == static_cast<BuiltinType::Kind>(K); |
6927 | } |
6928 | return false; |
6929 | } |
6930 | |
6931 | inline bool Type::isPlaceholderType() const { |
6932 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6933 | return BT->isPlaceholderType(); |
6934 | return false; |
6935 | } |
6936 | |
6937 | inline const BuiltinType *Type::getAsPlaceholderType() const { |
6938 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6939 | if (BT->isPlaceholderType()) |
6940 | return BT; |
6941 | return nullptr; |
6942 | } |
6943 | |
6944 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { |
6945 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); |
6946 | return isSpecificBuiltinType(K); |
6947 | } |
6948 | |
6949 | inline bool Type::isNonOverloadPlaceholderType() const { |
6950 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6951 | return BT->isNonOverloadPlaceholderType(); |
6952 | return false; |
6953 | } |
6954 | |
6955 | inline bool Type::isVoidType() const { |
6956 | return isSpecificBuiltinType(BuiltinType::Void); |
6957 | } |
6958 | |
6959 | inline bool Type::isHalfType() const { |
6960 | |
6961 | return isSpecificBuiltinType(BuiltinType::Half); |
6962 | } |
6963 | |
6964 | inline bool Type::isFloat16Type() const { |
6965 | return isSpecificBuiltinType(BuiltinType::Float16); |
6966 | } |
6967 | |
6968 | inline bool Type::isBFloat16Type() const { |
6969 | return isSpecificBuiltinType(BuiltinType::BFloat16); |
6970 | } |
6971 | |
6972 | inline bool Type::isFloat128Type() const { |
6973 | return isSpecificBuiltinType(BuiltinType::Float128); |
6974 | } |
6975 | |
6976 | inline bool Type::isNullPtrType() const { |
6977 | return isSpecificBuiltinType(BuiltinType::NullPtr); |
6978 | } |
6979 | |
6980 | bool IsEnumDeclComplete(EnumDecl *); |
6981 | bool IsEnumDeclScoped(EnumDecl *); |
6982 | |
6983 | inline bool Type::isIntegerType() const { |
6984 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6985 | return BT->getKind() >= BuiltinType::Bool && |
6986 | BT->getKind() <= BuiltinType::Int128; |
6987 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
6988 | |
6989 | |
6990 | return IsEnumDeclComplete(ET->getDecl()) && |
6991 | !IsEnumDeclScoped(ET->getDecl()); |
6992 | } |
6993 | return isExtIntType(); |
6994 | } |
6995 | |
6996 | inline bool Type::isFixedPointType() const { |
6997 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6998 | return BT->getKind() >= BuiltinType::ShortAccum && |
6999 | BT->getKind() <= BuiltinType::SatULongFract; |
7000 | } |
7001 | return false; |
7002 | } |
7003 | |
7004 | inline bool Type::isFixedPointOrIntegerType() const { |
7005 | return isFixedPointType() || isIntegerType(); |
7006 | } |
7007 | |
7008 | inline bool Type::isSaturatedFixedPointType() const { |
7009 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7010 | return BT->getKind() >= BuiltinType::SatShortAccum && |
7011 | BT->getKind() <= BuiltinType::SatULongFract; |
7012 | } |
7013 | return false; |
7014 | } |
7015 | |
7016 | inline bool Type::isUnsaturatedFixedPointType() const { |
7017 | return isFixedPointType() && !isSaturatedFixedPointType(); |
7018 | } |
7019 | |
7020 | inline bool Type::isSignedFixedPointType() const { |
7021 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7022 | return ((BT->getKind() >= BuiltinType::ShortAccum && |
7023 | BT->getKind() <= BuiltinType::LongAccum) || |
7024 | (BT->getKind() >= BuiltinType::ShortFract && |
7025 | BT->getKind() <= BuiltinType::LongFract) || |
7026 | (BT->getKind() >= BuiltinType::SatShortAccum && |
7027 | BT->getKind() <= BuiltinType::SatLongAccum) || |
7028 | (BT->getKind() >= BuiltinType::SatShortFract && |
7029 | BT->getKind() <= BuiltinType::SatLongFract)); |
7030 | } |
7031 | return false; |
7032 | } |
7033 | |
7034 | inline bool Type::isUnsignedFixedPointType() const { |
7035 | return isFixedPointType() && !isSignedFixedPointType(); |
7036 | } |
7037 | |
7038 | inline bool Type::isScalarType() const { |
7039 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7040 | return BT->getKind() > BuiltinType::Void && |
7041 | BT->getKind() <= BuiltinType::NullPtr; |
7042 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
7043 | |
7044 | |
7045 | return IsEnumDeclComplete(ET->getDecl()); |
7046 | return isa<PointerType>(CanonicalType) || |
7047 | isa<BlockPointerType>(CanonicalType) || |
7048 | isa<MemberPointerType>(CanonicalType) || |
7049 | isa<ComplexType>(CanonicalType) || |
7050 | isa<ObjCObjectPointerType>(CanonicalType) || |
7051 | isExtIntType(); |
7052 | } |
7053 | |
7054 | inline bool Type::isIntegralOrEnumerationType() const { |
7055 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7056 | return BT->getKind() >= BuiltinType::Bool && |
7057 | BT->getKind() <= BuiltinType::Int128; |
7058 | |
7059 | |
7060 | |
7061 | if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) |
7062 | return IsEnumDeclComplete(ET->getDecl()); |
7063 | |
7064 | return isExtIntType(); |
7065 | } |
7066 | |
7067 | inline bool Type::isBooleanType() const { |
7068 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7069 | return BT->getKind() == BuiltinType::Bool; |
7070 | return false; |
7071 | } |
7072 | |
7073 | inline bool Type::isUndeducedType() const { |
7074 | auto *DT = getContainedDeducedType(); |
7075 | return DT && !DT->isDeduced(); |
| 9 | | Assuming 'DT' is non-null | |
|
| 10 | | Assuming the condition is true | |
|
| 11 | | Returning the value 1, which participates in a condition later | |
|
7076 | } |
7077 | |
7078 | |
7079 | |
7080 | inline bool Type::isOverloadableType() const { |
7081 | return isDependentType() || isRecordType() || isEnumeralType(); |
7082 | } |
7083 | |
7084 | |
7085 | inline bool Type::isTypedefNameType() const { |
7086 | if (getAs<TypedefType>()) |
7087 | return true; |
7088 | if (auto *TST = getAs<TemplateSpecializationType>()) |
7089 | return TST->isTypeAlias(); |
7090 | return false; |
7091 | } |
7092 | |
7093 | |
7094 | inline bool Type::canDecayToPointerType() const { |
7095 | return isFunctionType() || isArrayType(); |
7096 | } |
7097 | |
7098 | inline bool Type::hasPointerRepresentation() const { |
7099 | return (isPointerType() || isReferenceType() || isBlockPointerType() || |
7100 | isObjCObjectPointerType() || isNullPtrType()); |
7101 | } |
7102 | |
7103 | inline bool Type::hasObjCPointerRepresentation() const { |
7104 | return isObjCObjectPointerType(); |
7105 | } |
7106 | |
7107 | inline const Type *Type::getBaseElementTypeUnsafe() const { |
7108 | const Type *type = this; |
7109 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) |
7110 | type = arrayType->getElementType().getTypePtr(); |
7111 | return type; |
7112 | } |
7113 | |
7114 | inline const Type *Type::getPointeeOrArrayElementType() const { |
7115 | const Type *type = this; |
7116 | if (type->isAnyPointerType()) |
7117 | return type->getPointeeType().getTypePtr(); |
7118 | else if (type->isArrayType()) |
7119 | return type->getBaseElementTypeUnsafe(); |
7120 | return type; |
7121 | } |
7122 | |
7123 | |
7124 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7125 | LangAS AS) { |
7126 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
7127 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
7128 | return PD; |
7129 | } |
7130 | |
7131 | |
7132 | |
7133 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7134 | Qualifiers Q) { |
7135 | PD.AddTaggedVal(Q.getAsOpaqueValue(), |
7136 | DiagnosticsEngine::ArgumentKind::ak_qual); |
7137 | return PD; |
7138 | } |
7139 | |
7140 | |
7141 | |
7142 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7143 | QualType T) { |
7144 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7145 | DiagnosticsEngine::ak_qualtype); |
7146 | return PD; |
7147 | } |
7148 | |
7149 | |
7150 | |
7151 | template <typename T> |
7152 | using TypeIsArrayType = |
7153 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || |
7154 | std::is_base_of<ArrayType, T>::value>; |
7155 | |
7156 | |
7157 | template <typename T> const T *Type::getAs() const { |
7158 | static_assert(!TypeIsArrayType<T>::value, |
7159 | "ArrayType cannot be used with getAs!"); |
7160 | |
7161 | |
7162 | if (const auto *Ty = dyn_cast<T>(this)) |
7163 | return Ty; |
7164 | |
7165 | |
7166 | if (!isa<T>(CanonicalType)) |
7167 | return nullptr; |
7168 | |
7169 | |
7170 | |
7171 | return cast<T>(getUnqualifiedDesugaredType()); |
7172 | } |
7173 | |
7174 | template <typename T> const T *Type::getAsAdjusted() const { |
7175 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); |
7176 | |
7177 | |
7178 | if (const auto *Ty = dyn_cast<T>(this)) |
7179 | return Ty; |
7180 | |
7181 | |
7182 | if (!isa<T>(CanonicalType)) |
7183 | return nullptr; |
7184 | |
7185 | |
7186 | |
7187 | const Type *Ty = this; |
7188 | while (Ty) { |
7189 | if (const auto *A = dyn_cast<AttributedType>(Ty)) |
7190 | Ty = A->getModifiedType().getTypePtr(); |
7191 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) |
7192 | Ty = E->desugar().getTypePtr(); |
7193 | else if (const auto *P = dyn_cast<ParenType>(Ty)) |
7194 | Ty = P->desugar().getTypePtr(); |
7195 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) |
7196 | Ty = A->desugar().getTypePtr(); |
7197 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) |
7198 | Ty = M->desugar().getTypePtr(); |
7199 | else |
7200 | break; |
7201 | } |
7202 | |
7203 | |
7204 | |
7205 | return dyn_cast<T>(Ty); |
7206 | } |
7207 | |
7208 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { |
7209 | |
7210 | if (const auto *arr = dyn_cast<ArrayType>(this)) |
7211 | return arr; |
7212 | |
7213 | |
7214 | if (!isa<ArrayType>(CanonicalType)) |
7215 | return nullptr; |
7216 | |
7217 | |
7218 | |
7219 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7220 | } |
7221 | |
7222 | template <typename T> const T *Type::castAs() const { |
7223 | static_assert(!TypeIsArrayType<T>::value, |
7224 | "ArrayType cannot be used with castAs!"); |
7225 | |
7226 | if (const auto *ty = dyn_cast<T>(this)) return ty; |
7227 | assert(isa<T>(CanonicalType)); |
7228 | return cast<T>(getUnqualifiedDesugaredType()); |
7229 | } |
7230 | |
7231 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { |
7232 | assert(isa<ArrayType>(CanonicalType)); |
7233 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; |
7234 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7235 | } |
7236 | |
7237 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, |
7238 | QualType CanonicalPtr) |
7239 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { |
7240 | #ifndef NDEBUG |
7241 | QualType Adjusted = getAdjustedType(); |
7242 | (void)AttributedType::stripOuterNullability(Adjusted); |
7243 | assert(isa<PointerType>(Adjusted)); |
7244 | #endif |
7245 | } |
7246 | |
7247 | QualType DecayedType::getPointeeType() const { |
7248 | QualType Decayed = getDecayedType(); |
7249 | (void)AttributedType::stripOuterNullability(Decayed); |
7250 | return cast<PointerType>(Decayed)->getPointeeType(); |
7251 | } |
7252 | |
7253 | |
7254 | |
7255 | |
7256 | |
7257 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, |
7258 | unsigned Scale); |
7259 | |
7260 | } |
7261 | |
7262 | #endif // LLVM_CLANG_AST_TYPE_H |