File: | clang/lib/Sema/SemaTemplate.cpp |
Warning: | line 7396, column 9 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | //===----------------------------------------------------------------------===// | |||
7 | // | |||
8 | // This file implements semantic analysis for C++ templates. | |||
9 | //===----------------------------------------------------------------------===// | |||
10 | ||||
11 | #include "TreeTransform.h" | |||
12 | #include "clang/AST/ASTConsumer.h" | |||
13 | #include "clang/AST/ASTContext.h" | |||
14 | #include "clang/AST/DeclFriend.h" | |||
15 | #include "clang/AST/DeclTemplate.h" | |||
16 | #include "clang/AST/Expr.h" | |||
17 | #include "clang/AST/ExprCXX.h" | |||
18 | #include "clang/AST/RecursiveASTVisitor.h" | |||
19 | #include "clang/AST/TypeVisitor.h" | |||
20 | #include "clang/Basic/Builtins.h" | |||
21 | #include "clang/Basic/LangOptions.h" | |||
22 | #include "clang/Basic/PartialDiagnostic.h" | |||
23 | #include "clang/Basic/Stack.h" | |||
24 | #include "clang/Basic/TargetInfo.h" | |||
25 | #include "clang/Sema/DeclSpec.h" | |||
26 | #include "clang/Sema/Lookup.h" | |||
27 | #include "clang/Sema/Overload.h" | |||
28 | #include "clang/Sema/ParsedTemplate.h" | |||
29 | #include "clang/Sema/Scope.h" | |||
30 | #include "clang/Sema/SemaInternal.h" | |||
31 | #include "clang/Sema/Template.h" | |||
32 | #include "clang/Sema/TemplateDeduction.h" | |||
33 | #include "llvm/ADT/SmallBitVector.h" | |||
34 | #include "llvm/ADT/SmallString.h" | |||
35 | #include "llvm/ADT/StringExtras.h" | |||
36 | ||||
37 | #include <iterator> | |||
38 | using namespace clang; | |||
39 | using namespace sema; | |||
40 | ||||
41 | // Exported for use by Parser. | |||
42 | SourceRange | |||
43 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, | |||
44 | unsigned N) { | |||
45 | if (!N) return SourceRange(); | |||
46 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); | |||
47 | } | |||
48 | ||||
49 | /// \brief Determine whether the declaration found is acceptable as the name | |||
50 | /// of a template and, if so, return that template declaration. Otherwise, | |||
51 | /// returns null. | |||
52 | /// | |||
53 | /// Note that this may return an UnresolvedUsingValueDecl if AllowDependent | |||
54 | /// is true. In all other cases it will return a TemplateDecl (or null). | |||
55 | NamedDecl *Sema::getAsTemplateNameDecl(NamedDecl *D, | |||
56 | bool AllowFunctionTemplates, | |||
57 | bool AllowDependent) { | |||
58 | D = D->getUnderlyingDecl(); | |||
59 | ||||
60 | if (isa<TemplateDecl>(D)) { | |||
61 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) | |||
62 | return nullptr; | |||
63 | ||||
64 | return D; | |||
65 | } | |||
66 | ||||
67 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { | |||
68 | // C++ [temp.local]p1: | |||
69 | // Like normal (non-template) classes, class templates have an | |||
70 | // injected-class-name (Clause 9). The injected-class-name | |||
71 | // can be used with or without a template-argument-list. When | |||
72 | // it is used without a template-argument-list, it is | |||
73 | // equivalent to the injected-class-name followed by the | |||
74 | // template-parameters of the class template enclosed in | |||
75 | // <>. When it is used with a template-argument-list, it | |||
76 | // refers to the specified class template specialization, | |||
77 | // which could be the current specialization or another | |||
78 | // specialization. | |||
79 | if (Record->isInjectedClassName()) { | |||
80 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); | |||
81 | if (Record->getDescribedClassTemplate()) | |||
82 | return Record->getDescribedClassTemplate(); | |||
83 | ||||
84 | if (ClassTemplateSpecializationDecl *Spec | |||
85 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) | |||
86 | return Spec->getSpecializedTemplate(); | |||
87 | } | |||
88 | ||||
89 | return nullptr; | |||
90 | } | |||
91 | ||||
92 | // 'using Dependent::foo;' can resolve to a template name. | |||
93 | // 'using typename Dependent::foo;' cannot (not even if 'foo' is an | |||
94 | // injected-class-name). | |||
95 | if (AllowDependent && isa<UnresolvedUsingValueDecl>(D)) | |||
96 | return D; | |||
97 | ||||
98 | return nullptr; | |||
99 | } | |||
100 | ||||
101 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, | |||
102 | bool AllowFunctionTemplates, | |||
103 | bool AllowDependent) { | |||
104 | LookupResult::Filter filter = R.makeFilter(); | |||
105 | while (filter.hasNext()) { | |||
106 | NamedDecl *Orig = filter.next(); | |||
107 | if (!getAsTemplateNameDecl(Orig, AllowFunctionTemplates, AllowDependent)) | |||
108 | filter.erase(); | |||
109 | } | |||
110 | filter.done(); | |||
111 | } | |||
112 | ||||
113 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, | |||
114 | bool AllowFunctionTemplates, | |||
115 | bool AllowDependent, | |||
116 | bool AllowNonTemplateFunctions) { | |||
117 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { | |||
118 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) | |||
119 | return true; | |||
120 | if (AllowNonTemplateFunctions && | |||
121 | isa<FunctionDecl>((*I)->getUnderlyingDecl())) | |||
122 | return true; | |||
123 | } | |||
124 | ||||
125 | return false; | |||
126 | } | |||
127 | ||||
128 | TemplateNameKind Sema::isTemplateName(Scope *S, | |||
129 | CXXScopeSpec &SS, | |||
130 | bool hasTemplateKeyword, | |||
131 | const UnqualifiedId &Name, | |||
132 | ParsedType ObjectTypePtr, | |||
133 | bool EnteringContext, | |||
134 | TemplateTy &TemplateResult, | |||
135 | bool &MemberOfUnknownSpecialization) { | |||
136 | assert(getLangOpts().CPlusPlus && "No template names in C!")((getLangOpts().CPlusPlus && "No template names in C!" ) ? static_cast<void> (0) : __assert_fail ("getLangOpts().CPlusPlus && \"No template names in C!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 136, __PRETTY_FUNCTION__)); | |||
137 | ||||
138 | DeclarationName TName; | |||
139 | MemberOfUnknownSpecialization = false; | |||
140 | ||||
141 | switch (Name.getKind()) { | |||
142 | case UnqualifiedIdKind::IK_Identifier: | |||
143 | TName = DeclarationName(Name.Identifier); | |||
144 | break; | |||
145 | ||||
146 | case UnqualifiedIdKind::IK_OperatorFunctionId: | |||
147 | TName = Context.DeclarationNames.getCXXOperatorName( | |||
148 | Name.OperatorFunctionId.Operator); | |||
149 | break; | |||
150 | ||||
151 | case UnqualifiedIdKind::IK_LiteralOperatorId: | |||
152 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); | |||
153 | break; | |||
154 | ||||
155 | default: | |||
156 | return TNK_Non_template; | |||
157 | } | |||
158 | ||||
159 | QualType ObjectType = ObjectTypePtr.get(); | |||
160 | ||||
161 | AssumedTemplateKind AssumedTemplate; | |||
162 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); | |||
163 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, | |||
164 | MemberOfUnknownSpecialization, SourceLocation(), | |||
165 | &AssumedTemplate)) | |||
166 | return TNK_Non_template; | |||
167 | ||||
168 | if (AssumedTemplate != AssumedTemplateKind::None) { | |||
169 | TemplateResult = TemplateTy::make(Context.getAssumedTemplateName(TName)); | |||
170 | // Let the parser know whether we found nothing or found functions; if we | |||
171 | // found nothing, we want to more carefully check whether this is actually | |||
172 | // a function template name versus some other kind of undeclared identifier. | |||
173 | return AssumedTemplate == AssumedTemplateKind::FoundNothing | |||
174 | ? TNK_Undeclared_template | |||
175 | : TNK_Function_template; | |||
176 | } | |||
177 | ||||
178 | if (R.empty()) | |||
179 | return TNK_Non_template; | |||
180 | ||||
181 | NamedDecl *D = nullptr; | |||
182 | if (R.isAmbiguous()) { | |||
183 | // If we got an ambiguity involving a non-function template, treat this | |||
184 | // as a template name, and pick an arbitrary template for error recovery. | |||
185 | bool AnyFunctionTemplates = false; | |||
186 | for (NamedDecl *FoundD : R) { | |||
187 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { | |||
188 | if (isa<FunctionTemplateDecl>(FoundTemplate)) | |||
189 | AnyFunctionTemplates = true; | |||
190 | else { | |||
191 | D = FoundTemplate; | |||
192 | break; | |||
193 | } | |||
194 | } | |||
195 | } | |||
196 | ||||
197 | // If we didn't find any templates at all, this isn't a template name. | |||
198 | // Leave the ambiguity for a later lookup to diagnose. | |||
199 | if (!D && !AnyFunctionTemplates) { | |||
200 | R.suppressDiagnostics(); | |||
201 | return TNK_Non_template; | |||
202 | } | |||
203 | ||||
204 | // If the only templates were function templates, filter out the rest. | |||
205 | // We'll diagnose the ambiguity later. | |||
206 | if (!D) | |||
207 | FilterAcceptableTemplateNames(R); | |||
208 | } | |||
209 | ||||
210 | // At this point, we have either picked a single template name declaration D | |||
211 | // or we have a non-empty set of results R containing either one template name | |||
212 | // declaration or a set of function templates. | |||
213 | ||||
214 | TemplateName Template; | |||
215 | TemplateNameKind TemplateKind; | |||
216 | ||||
217 | unsigned ResultCount = R.end() - R.begin(); | |||
218 | if (!D && ResultCount > 1) { | |||
219 | // We assume that we'll preserve the qualifier from a function | |||
220 | // template name in other ways. | |||
221 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); | |||
222 | TemplateKind = TNK_Function_template; | |||
223 | ||||
224 | // We'll do this lookup again later. | |||
225 | R.suppressDiagnostics(); | |||
226 | } else { | |||
227 | if (!D) { | |||
228 | D = getAsTemplateNameDecl(*R.begin()); | |||
229 | assert(D && "unambiguous result is not a template name")((D && "unambiguous result is not a template name") ? static_cast<void> (0) : __assert_fail ("D && \"unambiguous result is not a template name\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 229, __PRETTY_FUNCTION__)); | |||
230 | } | |||
231 | ||||
232 | if (isa<UnresolvedUsingValueDecl>(D)) { | |||
233 | // We don't yet know whether this is a template-name or not. | |||
234 | MemberOfUnknownSpecialization = true; | |||
235 | return TNK_Non_template; | |||
236 | } | |||
237 | ||||
238 | TemplateDecl *TD = cast<TemplateDecl>(D); | |||
239 | ||||
240 | if (SS.isSet() && !SS.isInvalid()) { | |||
241 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); | |||
242 | Template = Context.getQualifiedTemplateName(Qualifier, | |||
243 | hasTemplateKeyword, TD); | |||
244 | } else { | |||
245 | Template = TemplateName(TD); | |||
246 | } | |||
247 | ||||
248 | if (isa<FunctionTemplateDecl>(TD)) { | |||
249 | TemplateKind = TNK_Function_template; | |||
250 | ||||
251 | // We'll do this lookup again later. | |||
252 | R.suppressDiagnostics(); | |||
253 | } else { | |||
254 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||((isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl >(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl >(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl >(TD)) ? static_cast<void> (0) : __assert_fail ("isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD)" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 256, __PRETTY_FUNCTION__)) | |||
255 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) ||((isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl >(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl >(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl >(TD)) ? static_cast<void> (0) : __assert_fail ("isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD)" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 256, __PRETTY_FUNCTION__)) | |||
256 | isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD))((isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl >(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl >(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl >(TD)) ? static_cast<void> (0) : __assert_fail ("isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD)" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 256, __PRETTY_FUNCTION__)); | |||
257 | TemplateKind = | |||
258 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : | |||
259 | isa<ConceptDecl>(TD) ? TNK_Concept_template : | |||
260 | TNK_Type_template; | |||
261 | } | |||
262 | } | |||
263 | ||||
264 | TemplateResult = TemplateTy::make(Template); | |||
265 | return TemplateKind; | |||
266 | } | |||
267 | ||||
268 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, | |||
269 | SourceLocation NameLoc, | |||
270 | ParsedTemplateTy *Template) { | |||
271 | CXXScopeSpec SS; | |||
272 | bool MemberOfUnknownSpecialization = false; | |||
273 | ||||
274 | // We could use redeclaration lookup here, but we don't need to: the | |||
275 | // syntactic form of a deduction guide is enough to identify it even | |||
276 | // if we can't look up the template name at all. | |||
277 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); | |||
278 | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), | |||
279 | /*EnteringContext*/ false, | |||
280 | MemberOfUnknownSpecialization)) | |||
281 | return false; | |||
282 | ||||
283 | if (R.empty()) return false; | |||
284 | if (R.isAmbiguous()) { | |||
285 | // FIXME: Diagnose an ambiguity if we find at least one template. | |||
286 | R.suppressDiagnostics(); | |||
287 | return false; | |||
288 | } | |||
289 | ||||
290 | // We only treat template-names that name type templates as valid deduction | |||
291 | // guide names. | |||
292 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); | |||
293 | if (!TD || !getAsTypeTemplateDecl(TD)) | |||
294 | return false; | |||
295 | ||||
296 | if (Template) | |||
297 | *Template = TemplateTy::make(TemplateName(TD)); | |||
298 | return true; | |||
299 | } | |||
300 | ||||
301 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, | |||
302 | SourceLocation IILoc, | |||
303 | Scope *S, | |||
304 | const CXXScopeSpec *SS, | |||
305 | TemplateTy &SuggestedTemplate, | |||
306 | TemplateNameKind &SuggestedKind) { | |||
307 | // We can't recover unless there's a dependent scope specifier preceding the | |||
308 | // template name. | |||
309 | // FIXME: Typo correction? | |||
310 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || | |||
311 | computeDeclContext(*SS)) | |||
312 | return false; | |||
313 | ||||
314 | // The code is missing a 'template' keyword prior to the dependent template | |||
315 | // name. | |||
316 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); | |||
317 | Diag(IILoc, diag::err_template_kw_missing) | |||
318 | << Qualifier << II.getName() | |||
319 | << FixItHint::CreateInsertion(IILoc, "template "); | |||
320 | SuggestedTemplate | |||
321 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); | |||
322 | SuggestedKind = TNK_Dependent_template_name; | |||
323 | return true; | |||
324 | } | |||
325 | ||||
326 | bool Sema::LookupTemplateName(LookupResult &Found, | |||
327 | Scope *S, CXXScopeSpec &SS, | |||
328 | QualType ObjectType, | |||
329 | bool EnteringContext, | |||
330 | bool &MemberOfUnknownSpecialization, | |||
331 | SourceLocation TemplateKWLoc, | |||
332 | AssumedTemplateKind *ATK) { | |||
333 | if (ATK) | |||
334 | *ATK = AssumedTemplateKind::None; | |||
335 | ||||
336 | Found.setTemplateNameLookup(true); | |||
337 | ||||
338 | // Determine where to perform name lookup | |||
339 | MemberOfUnknownSpecialization = false; | |||
340 | DeclContext *LookupCtx = nullptr; | |||
341 | bool IsDependent = false; | |||
342 | if (!ObjectType.isNull()) { | |||
343 | // This nested-name-specifier occurs in a member access expression, e.g., | |||
344 | // x->B::f, and we are looking into the type of the object. | |||
345 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist")((!SS.isSet() && "ObjectType and scope specifier cannot coexist" ) ? static_cast<void> (0) : __assert_fail ("!SS.isSet() && \"ObjectType and scope specifier cannot coexist\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 345, __PRETTY_FUNCTION__)); | |||
346 | LookupCtx = computeDeclContext(ObjectType); | |||
347 | IsDependent = !LookupCtx && ObjectType->isDependentType(); | |||
348 | assert((IsDependent || !ObjectType->isIncompleteType() ||(((IsDependent || !ObjectType->isIncompleteType() || ObjectType ->castAs<TagType>()->isBeingDefined()) && "Caller should have completed object type") ? static_cast< void> (0) : __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs<TagType>()->isBeingDefined()) && \"Caller should have completed object type\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 350, __PRETTY_FUNCTION__)) | |||
349 | ObjectType->castAs<TagType>()->isBeingDefined()) &&(((IsDependent || !ObjectType->isIncompleteType() || ObjectType ->castAs<TagType>()->isBeingDefined()) && "Caller should have completed object type") ? static_cast< void> (0) : __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs<TagType>()->isBeingDefined()) && \"Caller should have completed object type\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 350, __PRETTY_FUNCTION__)) | |||
350 | "Caller should have completed object type")(((IsDependent || !ObjectType->isIncompleteType() || ObjectType ->castAs<TagType>()->isBeingDefined()) && "Caller should have completed object type") ? static_cast< void> (0) : __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs<TagType>()->isBeingDefined()) && \"Caller should have completed object type\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 350, __PRETTY_FUNCTION__)); | |||
351 | ||||
352 | // Template names cannot appear inside an Objective-C class or object type | |||
353 | // or a vector type. | |||
354 | // | |||
355 | // FIXME: This is wrong. For example: | |||
356 | // | |||
357 | // template<typename T> using Vec = T __attribute__((ext_vector_type(4))); | |||
358 | // Vec<int> vi; | |||
359 | // vi.Vec<int>::~Vec<int>(); | |||
360 | // | |||
361 | // ... should be accepted but we will not treat 'Vec' as a template name | |||
362 | // here. The right thing to do would be to check if the name is a valid | |||
363 | // vector component name, and look up a template name if not. And similarly | |||
364 | // for lookups into Objective-C class and object types, where the same | |||
365 | // problem can arise. | |||
366 | if (ObjectType->isObjCObjectOrInterfaceType() || | |||
367 | ObjectType->isVectorType()) { | |||
368 | Found.clear(); | |||
369 | return false; | |||
370 | } | |||
371 | } else if (SS.isSet()) { | |||
372 | // This nested-name-specifier occurs after another nested-name-specifier, | |||
373 | // so long into the context associated with the prior nested-name-specifier. | |||
374 | LookupCtx = computeDeclContext(SS, EnteringContext); | |||
375 | IsDependent = !LookupCtx; | |||
376 | ||||
377 | // The declaration context must be complete. | |||
378 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) | |||
379 | return true; | |||
380 | } | |||
381 | ||||
382 | bool ObjectTypeSearchedInScope = false; | |||
383 | bool AllowFunctionTemplatesInLookup = true; | |||
384 | if (LookupCtx) { | |||
385 | // Perform "qualified" name lookup into the declaration context we | |||
386 | // computed, which is either the type of the base of a member access | |||
387 | // expression or the declaration context associated with a prior | |||
388 | // nested-name-specifier. | |||
389 | LookupQualifiedName(Found, LookupCtx); | |||
390 | ||||
391 | // FIXME: The C++ standard does not clearly specify what happens in the | |||
392 | // case where the object type is dependent, and implementations vary. In | |||
393 | // Clang, we treat a name after a . or -> as a template-name if lookup | |||
394 | // finds a non-dependent member or member of the current instantiation that | |||
395 | // is a type template, or finds no such members and lookup in the context | |||
396 | // of the postfix-expression finds a type template. In the latter case, the | |||
397 | // name is nonetheless dependent, and we may resolve it to a member of an | |||
398 | // unknown specialization when we come to instantiate the template. | |||
399 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); | |||
400 | } | |||
401 | ||||
402 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { | |||
403 | // C++ [basic.lookup.classref]p1: | |||
404 | // In a class member access expression (5.2.5), if the . or -> token is | |||
405 | // immediately followed by an identifier followed by a <, the | |||
406 | // identifier must be looked up to determine whether the < is the | |||
407 | // beginning of a template argument list (14.2) or a less-than operator. | |||
408 | // The identifier is first looked up in the class of the object | |||
409 | // expression. If the identifier is not found, it is then looked up in | |||
410 | // the context of the entire postfix-expression and shall name a class | |||
411 | // template. | |||
412 | if (S) | |||
413 | LookupName(Found, S); | |||
414 | ||||
415 | if (!ObjectType.isNull()) { | |||
416 | // FIXME: We should filter out all non-type templates here, particularly | |||
417 | // variable templates and concepts. But the exclusion of alias templates | |||
418 | // and template template parameters is a wording defect. | |||
419 | AllowFunctionTemplatesInLookup = false; | |||
420 | ObjectTypeSearchedInScope = true; | |||
421 | } | |||
422 | ||||
423 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); | |||
424 | } | |||
425 | ||||
426 | if (Found.isAmbiguous()) | |||
427 | return false; | |||
428 | ||||
429 | if (ATK && !SS.isSet() && ObjectType.isNull() && TemplateKWLoc.isInvalid()) { | |||
430 | // C++2a [temp.names]p2: | |||
431 | // A name is also considered to refer to a template if it is an | |||
432 | // unqualified-id followed by a < and name lookup finds either one or more | |||
433 | // functions or finds nothing. | |||
434 | // | |||
435 | // To keep our behavior consistent, we apply the "finds nothing" part in | |||
436 | // all language modes, and diagnose the empty lookup in ActOnCallExpr if we | |||
437 | // successfully form a call to an undeclared template-id. | |||
438 | bool AllFunctions = | |||
439 | getLangOpts().CPlusPlus2a && | |||
440 | std::all_of(Found.begin(), Found.end(), [](NamedDecl *ND) { | |||
441 | return isa<FunctionDecl>(ND->getUnderlyingDecl()); | |||
442 | }); | |||
443 | if (AllFunctions || (Found.empty() && !IsDependent)) { | |||
444 | // If lookup found any functions, or if this is a name that can only be | |||
445 | // used for a function, then strongly assume this is a function | |||
446 | // template-id. | |||
447 | *ATK = (Found.empty() && Found.getLookupName().isIdentifier()) | |||
448 | ? AssumedTemplateKind::FoundNothing | |||
449 | : AssumedTemplateKind::FoundFunctions; | |||
450 | Found.clear(); | |||
451 | return false; | |||
452 | } | |||
453 | } | |||
454 | ||||
455 | if (Found.empty() && !IsDependent) { | |||
456 | // If we did not find any names, attempt to correct any typos. | |||
457 | DeclarationName Name = Found.getLookupName(); | |||
458 | Found.clear(); | |||
459 | // Simple filter callback that, for keywords, only accepts the C++ *_cast | |||
460 | DefaultFilterCCC FilterCCC{}; | |||
461 | FilterCCC.WantTypeSpecifiers = false; | |||
462 | FilterCCC.WantExpressionKeywords = false; | |||
463 | FilterCCC.WantRemainingKeywords = false; | |||
464 | FilterCCC.WantCXXNamedCasts = true; | |||
465 | if (TypoCorrection Corrected = | |||
466 | CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, | |||
467 | &SS, FilterCCC, CTK_ErrorRecovery, LookupCtx)) { | |||
468 | if (auto *ND = Corrected.getFoundDecl()) | |||
469 | Found.addDecl(ND); | |||
470 | FilterAcceptableTemplateNames(Found); | |||
471 | if (Found.isAmbiguous()) { | |||
472 | Found.clear(); | |||
473 | } else if (!Found.empty()) { | |||
474 | Found.setLookupName(Corrected.getCorrection()); | |||
475 | if (LookupCtx) { | |||
476 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); | |||
477 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | |||
478 | Name.getAsString() == CorrectedStr; | |||
479 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) | |||
480 | << Name << LookupCtx << DroppedSpecifier | |||
481 | << SS.getRange()); | |||
482 | } else { | |||
483 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); | |||
484 | } | |||
485 | } | |||
486 | } | |||
487 | } | |||
488 | ||||
489 | NamedDecl *ExampleLookupResult = | |||
490 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); | |||
491 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); | |||
492 | if (Found.empty()) { | |||
493 | if (IsDependent) { | |||
494 | MemberOfUnknownSpecialization = true; | |||
495 | return false; | |||
496 | } | |||
497 | ||||
498 | // If a 'template' keyword was used, a lookup that finds only non-template | |||
499 | // names is an error. | |||
500 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { | |||
501 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) | |||
502 | << Found.getLookupName() << SS.getRange(); | |||
503 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), | |||
504 | diag::note_template_kw_refers_to_non_template) | |||
505 | << Found.getLookupName(); | |||
506 | return true; | |||
507 | } | |||
508 | ||||
509 | return false; | |||
510 | } | |||
511 | ||||
512 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && | |||
513 | !getLangOpts().CPlusPlus11) { | |||
514 | // C++03 [basic.lookup.classref]p1: | |||
515 | // [...] If the lookup in the class of the object expression finds a | |||
516 | // template, the name is also looked up in the context of the entire | |||
517 | // postfix-expression and [...] | |||
518 | // | |||
519 | // Note: C++11 does not perform this second lookup. | |||
520 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), | |||
521 | LookupOrdinaryName); | |||
522 | FoundOuter.setTemplateNameLookup(true); | |||
523 | LookupName(FoundOuter, S); | |||
524 | // FIXME: We silently accept an ambiguous lookup here, in violation of | |||
525 | // [basic.lookup]/1. | |||
526 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); | |||
527 | ||||
528 | NamedDecl *OuterTemplate; | |||
529 | if (FoundOuter.empty()) { | |||
530 | // - if the name is not found, the name found in the class of the | |||
531 | // object expression is used, otherwise | |||
532 | } else if (FoundOuter.isAmbiguous() || !FoundOuter.isSingleResult() || | |||
533 | !(OuterTemplate = | |||
534 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { | |||
535 | // - if the name is found in the context of the entire | |||
536 | // postfix-expression and does not name a class template, the name | |||
537 | // found in the class of the object expression is used, otherwise | |||
538 | FoundOuter.clear(); | |||
539 | } else if (!Found.isSuppressingDiagnostics()) { | |||
540 | // - if the name found is a class template, it must refer to the same | |||
541 | // entity as the one found in the class of the object expression, | |||
542 | // otherwise the program is ill-formed. | |||
543 | if (!Found.isSingleResult() || | |||
544 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != | |||
545 | OuterTemplate->getCanonicalDecl()) { | |||
546 | Diag(Found.getNameLoc(), | |||
547 | diag::ext_nested_name_member_ref_lookup_ambiguous) | |||
548 | << Found.getLookupName() | |||
549 | << ObjectType; | |||
550 | Diag(Found.getRepresentativeDecl()->getLocation(), | |||
551 | diag::note_ambig_member_ref_object_type) | |||
552 | << ObjectType; | |||
553 | Diag(FoundOuter.getFoundDecl()->getLocation(), | |||
554 | diag::note_ambig_member_ref_scope); | |||
555 | ||||
556 | // Recover by taking the template that we found in the object | |||
557 | // expression's type. | |||
558 | } | |||
559 | } | |||
560 | } | |||
561 | ||||
562 | return false; | |||
563 | } | |||
564 | ||||
565 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, | |||
566 | SourceLocation Less, | |||
567 | SourceLocation Greater) { | |||
568 | if (TemplateName.isInvalid()) | |||
569 | return; | |||
570 | ||||
571 | DeclarationNameInfo NameInfo; | |||
572 | CXXScopeSpec SS; | |||
573 | LookupNameKind LookupKind; | |||
574 | ||||
575 | DeclContext *LookupCtx = nullptr; | |||
576 | NamedDecl *Found = nullptr; | |||
577 | bool MissingTemplateKeyword = false; | |||
578 | ||||
579 | // Figure out what name we looked up. | |||
580 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { | |||
581 | NameInfo = DRE->getNameInfo(); | |||
582 | SS.Adopt(DRE->getQualifierLoc()); | |||
583 | LookupKind = LookupOrdinaryName; | |||
584 | Found = DRE->getFoundDecl(); | |||
585 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { | |||
586 | NameInfo = ME->getMemberNameInfo(); | |||
587 | SS.Adopt(ME->getQualifierLoc()); | |||
588 | LookupKind = LookupMemberName; | |||
589 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); | |||
590 | Found = ME->getMemberDecl(); | |||
591 | } else if (auto *DSDRE = | |||
592 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { | |||
593 | NameInfo = DSDRE->getNameInfo(); | |||
594 | SS.Adopt(DSDRE->getQualifierLoc()); | |||
595 | MissingTemplateKeyword = true; | |||
596 | } else if (auto *DSME = | |||
597 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { | |||
598 | NameInfo = DSME->getMemberNameInfo(); | |||
599 | SS.Adopt(DSME->getQualifierLoc()); | |||
600 | MissingTemplateKeyword = true; | |||
601 | } else { | |||
602 | llvm_unreachable("unexpected kind of potential template name")::llvm::llvm_unreachable_internal("unexpected kind of potential template name" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 602); | |||
603 | } | |||
604 | ||||
605 | // If this is a dependent-scope lookup, diagnose that the 'template' keyword | |||
606 | // was missing. | |||
607 | if (MissingTemplateKeyword) { | |||
608 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) | |||
609 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); | |||
610 | return; | |||
611 | } | |||
612 | ||||
613 | // Try to correct the name by looking for templates and C++ named casts. | |||
614 | struct TemplateCandidateFilter : CorrectionCandidateCallback { | |||
615 | Sema &S; | |||
616 | TemplateCandidateFilter(Sema &S) : S(S) { | |||
617 | WantTypeSpecifiers = false; | |||
618 | WantExpressionKeywords = false; | |||
619 | WantRemainingKeywords = false; | |||
620 | WantCXXNamedCasts = true; | |||
621 | }; | |||
622 | bool ValidateCandidate(const TypoCorrection &Candidate) override { | |||
623 | if (auto *ND = Candidate.getCorrectionDecl()) | |||
624 | return S.getAsTemplateNameDecl(ND); | |||
625 | return Candidate.isKeyword(); | |||
626 | } | |||
627 | ||||
628 | std::unique_ptr<CorrectionCandidateCallback> clone() override { | |||
629 | return std::make_unique<TemplateCandidateFilter>(*this); | |||
630 | } | |||
631 | }; | |||
632 | ||||
633 | DeclarationName Name = NameInfo.getName(); | |||
634 | TemplateCandidateFilter CCC(*this); | |||
635 | if (TypoCorrection Corrected = CorrectTypo(NameInfo, LookupKind, S, &SS, CCC, | |||
636 | CTK_ErrorRecovery, LookupCtx)) { | |||
637 | auto *ND = Corrected.getFoundDecl(); | |||
638 | if (ND) | |||
639 | ND = getAsTemplateNameDecl(ND); | |||
640 | if (ND || Corrected.isKeyword()) { | |||
641 | if (LookupCtx) { | |||
642 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); | |||
643 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | |||
644 | Name.getAsString() == CorrectedStr; | |||
645 | diagnoseTypo(Corrected, | |||
646 | PDiag(diag::err_non_template_in_member_template_id_suggest) | |||
647 | << Name << LookupCtx << DroppedSpecifier | |||
648 | << SS.getRange(), false); | |||
649 | } else { | |||
650 | diagnoseTypo(Corrected, | |||
651 | PDiag(diag::err_non_template_in_template_id_suggest) | |||
652 | << Name, false); | |||
653 | } | |||
654 | if (Found) | |||
655 | Diag(Found->getLocation(), | |||
656 | diag::note_non_template_in_template_id_found); | |||
657 | return; | |||
658 | } | |||
659 | } | |||
660 | ||||
661 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) | |||
662 | << Name << SourceRange(Less, Greater); | |||
663 | if (Found) | |||
664 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); | |||
665 | } | |||
666 | ||||
667 | /// ActOnDependentIdExpression - Handle a dependent id-expression that | |||
668 | /// was just parsed. This is only possible with an explicit scope | |||
669 | /// specifier naming a dependent type. | |||
670 | ExprResult | |||
671 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, | |||
672 | SourceLocation TemplateKWLoc, | |||
673 | const DeclarationNameInfo &NameInfo, | |||
674 | bool isAddressOfOperand, | |||
675 | const TemplateArgumentListInfo *TemplateArgs) { | |||
676 | DeclContext *DC = getFunctionLevelDeclContext(); | |||
677 | ||||
678 | // C++11 [expr.prim.general]p12: | |||
679 | // An id-expression that denotes a non-static data member or non-static | |||
680 | // member function of a class can only be used: | |||
681 | // (...) | |||
682 | // - if that id-expression denotes a non-static data member and it | |||
683 | // appears in an unevaluated operand. | |||
684 | // | |||
685 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a | |||
686 | // CXXDependentScopeMemberExpr. The former can instantiate to either | |||
687 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is | |||
688 | // always a MemberExpr. | |||
689 | bool MightBeCxx11UnevalField = | |||
690 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); | |||
691 | ||||
692 | // Check if the nested name specifier is an enum type. | |||
693 | bool IsEnum = false; | |||
694 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) | |||
695 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); | |||
696 | ||||
697 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && | |||
698 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { | |||
699 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); | |||
700 | ||||
701 | // Since the 'this' expression is synthesized, we don't need to | |||
702 | // perform the double-lookup check. | |||
703 | NamedDecl *FirstQualifierInScope = nullptr; | |||
704 | ||||
705 | return CXXDependentScopeMemberExpr::Create( | |||
706 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, | |||
707 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, | |||
708 | FirstQualifierInScope, NameInfo, TemplateArgs); | |||
709 | } | |||
710 | ||||
711 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); | |||
712 | } | |||
713 | ||||
714 | ExprResult | |||
715 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, | |||
716 | SourceLocation TemplateKWLoc, | |||
717 | const DeclarationNameInfo &NameInfo, | |||
718 | const TemplateArgumentListInfo *TemplateArgs) { | |||
719 | // DependentScopeDeclRefExpr::Create requires a valid QualifierLoc | |||
720 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); | |||
721 | if (!QualifierLoc) | |||
722 | return ExprError(); | |||
723 | ||||
724 | return DependentScopeDeclRefExpr::Create( | |||
725 | Context, QualifierLoc, TemplateKWLoc, NameInfo, TemplateArgs); | |||
726 | } | |||
727 | ||||
728 | ||||
729 | /// Determine whether we would be unable to instantiate this template (because | |||
730 | /// it either has no definition, or is in the process of being instantiated). | |||
731 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, | |||
732 | NamedDecl *Instantiation, | |||
733 | bool InstantiatedFromMember, | |||
734 | const NamedDecl *Pattern, | |||
735 | const NamedDecl *PatternDef, | |||
736 | TemplateSpecializationKind TSK, | |||
737 | bool Complain /*= true*/) { | |||
738 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) ||((isa<TagDecl>(Instantiation) || isa<FunctionDecl> (Instantiation) || isa<VarDecl>(Instantiation)) ? static_cast <void> (0) : __assert_fail ("isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || isa<VarDecl>(Instantiation)" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 739, __PRETTY_FUNCTION__)) | |||
739 | isa<VarDecl>(Instantiation))((isa<TagDecl>(Instantiation) || isa<FunctionDecl> (Instantiation) || isa<VarDecl>(Instantiation)) ? static_cast <void> (0) : __assert_fail ("isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || isa<VarDecl>(Instantiation)" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 739, __PRETTY_FUNCTION__)); | |||
740 | ||||
741 | bool IsEntityBeingDefined = false; | |||
742 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) | |||
743 | IsEntityBeingDefined = TD->isBeingDefined(); | |||
744 | ||||
745 | if (PatternDef && !IsEntityBeingDefined) { | |||
746 | NamedDecl *SuggestedDef = nullptr; | |||
747 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, | |||
748 | /*OnlyNeedComplete*/false)) { | |||
749 | // If we're allowed to diagnose this and recover, do so. | |||
750 | bool Recover = Complain && !isSFINAEContext(); | |||
751 | if (Complain) | |||
752 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, | |||
753 | Sema::MissingImportKind::Definition, Recover); | |||
754 | return !Recover; | |||
755 | } | |||
756 | return false; | |||
757 | } | |||
758 | ||||
759 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) | |||
760 | return true; | |||
761 | ||||
762 | llvm::Optional<unsigned> Note; | |||
763 | QualType InstantiationTy; | |||
764 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) | |||
765 | InstantiationTy = Context.getTypeDeclType(TD); | |||
766 | if (PatternDef) { | |||
767 | Diag(PointOfInstantiation, | |||
768 | diag::err_template_instantiate_within_definition) | |||
769 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) | |||
770 | << InstantiationTy; | |||
771 | // Not much point in noting the template declaration here, since | |||
772 | // we're lexically inside it. | |||
773 | Instantiation->setInvalidDecl(); | |||
774 | } else if (InstantiatedFromMember) { | |||
775 | if (isa<FunctionDecl>(Instantiation)) { | |||
776 | Diag(PointOfInstantiation, | |||
777 | diag::err_explicit_instantiation_undefined_member) | |||
778 | << /*member function*/ 1 << Instantiation->getDeclName() | |||
779 | << Instantiation->getDeclContext(); | |||
780 | Note = diag::note_explicit_instantiation_here; | |||
781 | } else { | |||
782 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!")((isa<TagDecl>(Instantiation) && "Must be a TagDecl!" ) ? static_cast<void> (0) : __assert_fail ("isa<TagDecl>(Instantiation) && \"Must be a TagDecl!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 782, __PRETTY_FUNCTION__)); | |||
783 | Diag(PointOfInstantiation, | |||
784 | diag::err_implicit_instantiate_member_undefined) | |||
785 | << InstantiationTy; | |||
786 | Note = diag::note_member_declared_at; | |||
787 | } | |||
788 | } else { | |||
789 | if (isa<FunctionDecl>(Instantiation)) { | |||
790 | Diag(PointOfInstantiation, | |||
791 | diag::err_explicit_instantiation_undefined_func_template) | |||
792 | << Pattern; | |||
793 | Note = diag::note_explicit_instantiation_here; | |||
794 | } else if (isa<TagDecl>(Instantiation)) { | |||
795 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) | |||
796 | << (TSK != TSK_ImplicitInstantiation) | |||
797 | << InstantiationTy; | |||
798 | Note = diag::note_template_decl_here; | |||
799 | } else { | |||
800 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!")((isa<VarDecl>(Instantiation) && "Must be a VarDecl!" ) ? static_cast<void> (0) : __assert_fail ("isa<VarDecl>(Instantiation) && \"Must be a VarDecl!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 800, __PRETTY_FUNCTION__)); | |||
801 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { | |||
802 | Diag(PointOfInstantiation, | |||
803 | diag::err_explicit_instantiation_undefined_var_template) | |||
804 | << Instantiation; | |||
805 | Instantiation->setInvalidDecl(); | |||
806 | } else | |||
807 | Diag(PointOfInstantiation, | |||
808 | diag::err_explicit_instantiation_undefined_member) | |||
809 | << /*static data member*/ 2 << Instantiation->getDeclName() | |||
810 | << Instantiation->getDeclContext(); | |||
811 | Note = diag::note_explicit_instantiation_here; | |||
812 | } | |||
813 | } | |||
814 | if (Note) // Diagnostics were emitted. | |||
815 | Diag(Pattern->getLocation(), Note.getValue()); | |||
816 | ||||
817 | // In general, Instantiation isn't marked invalid to get more than one | |||
818 | // error for multiple undefined instantiations. But the code that does | |||
819 | // explicit declaration -> explicit definition conversion can't handle | |||
820 | // invalid declarations, so mark as invalid in that case. | |||
821 | if (TSK == TSK_ExplicitInstantiationDeclaration) | |||
822 | Instantiation->setInvalidDecl(); | |||
823 | return true; | |||
824 | } | |||
825 | ||||
826 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining | |||
827 | /// that the template parameter 'PrevDecl' is being shadowed by a new | |||
828 | /// declaration at location Loc. Returns true to indicate that this is | |||
829 | /// an error, and false otherwise. | |||
830 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { | |||
831 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter")((PrevDecl->isTemplateParameter() && "Not a template parameter" ) ? static_cast<void> (0) : __assert_fail ("PrevDecl->isTemplateParameter() && \"Not a template parameter\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 831, __PRETTY_FUNCTION__)); | |||
832 | ||||
833 | // C++ [temp.local]p4: | |||
834 | // A template-parameter shall not be redeclared within its | |||
835 | // scope (including nested scopes). | |||
836 | // | |||
837 | // Make this a warning when MSVC compatibility is requested. | |||
838 | unsigned DiagId = getLangOpts().MSVCCompat ? diag::ext_template_param_shadow | |||
839 | : diag::err_template_param_shadow; | |||
840 | Diag(Loc, DiagId) << cast<NamedDecl>(PrevDecl)->getDeclName(); | |||
841 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); | |||
842 | } | |||
843 | ||||
844 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset | |||
845 | /// the parameter D to reference the templated declaration and return a pointer | |||
846 | /// to the template declaration. Otherwise, do nothing to D and return null. | |||
847 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { | |||
848 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { | |||
849 | D = Temp->getTemplatedDecl(); | |||
850 | return Temp; | |||
851 | } | |||
852 | return nullptr; | |||
853 | } | |||
854 | ||||
855 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( | |||
856 | SourceLocation EllipsisLoc) const { | |||
857 | assert(Kind == Template &&((Kind == Template && "Only template template arguments can be pack expansions here" ) ? static_cast<void> (0) : __assert_fail ("Kind == Template && \"Only template template arguments can be pack expansions here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 858, __PRETTY_FUNCTION__)) | |||
858 | "Only template template arguments can be pack expansions here")((Kind == Template && "Only template template arguments can be pack expansions here" ) ? static_cast<void> (0) : __assert_fail ("Kind == Template && \"Only template template arguments can be pack expansions here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 858, __PRETTY_FUNCTION__)); | |||
859 | assert(getAsTemplate().get().containsUnexpandedParameterPack() &&((getAsTemplate().get().containsUnexpandedParameterPack() && "Template template argument pack expansion without packs") ? static_cast<void> (0) : __assert_fail ("getAsTemplate().get().containsUnexpandedParameterPack() && \"Template template argument pack expansion without packs\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 860, __PRETTY_FUNCTION__)) | |||
860 | "Template template argument pack expansion without packs")((getAsTemplate().get().containsUnexpandedParameterPack() && "Template template argument pack expansion without packs") ? static_cast<void> (0) : __assert_fail ("getAsTemplate().get().containsUnexpandedParameterPack() && \"Template template argument pack expansion without packs\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 860, __PRETTY_FUNCTION__)); | |||
861 | ParsedTemplateArgument Result(*this); | |||
862 | Result.EllipsisLoc = EllipsisLoc; | |||
863 | return Result; | |||
864 | } | |||
865 | ||||
866 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, | |||
867 | const ParsedTemplateArgument &Arg) { | |||
868 | ||||
869 | switch (Arg.getKind()) { | |||
870 | case ParsedTemplateArgument::Type: { | |||
871 | TypeSourceInfo *DI; | |||
872 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); | |||
873 | if (!DI) | |||
874 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); | |||
875 | return TemplateArgumentLoc(TemplateArgument(T), DI); | |||
876 | } | |||
877 | ||||
878 | case ParsedTemplateArgument::NonType: { | |||
879 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); | |||
880 | return TemplateArgumentLoc(TemplateArgument(E), E); | |||
881 | } | |||
882 | ||||
883 | case ParsedTemplateArgument::Template: { | |||
884 | TemplateName Template = Arg.getAsTemplate().get(); | |||
885 | TemplateArgument TArg; | |||
886 | if (Arg.getEllipsisLoc().isValid()) | |||
887 | TArg = TemplateArgument(Template, Optional<unsigned int>()); | |||
888 | else | |||
889 | TArg = Template; | |||
890 | return TemplateArgumentLoc(TArg, | |||
891 | Arg.getScopeSpec().getWithLocInContext( | |||
892 | SemaRef.Context), | |||
893 | Arg.getLocation(), | |||
894 | Arg.getEllipsisLoc()); | |||
895 | } | |||
896 | } | |||
897 | ||||
898 | llvm_unreachable("Unhandled parsed template argument")::llvm::llvm_unreachable_internal("Unhandled parsed template argument" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 898); | |||
899 | } | |||
900 | ||||
901 | /// Translates template arguments as provided by the parser | |||
902 | /// into template arguments used by semantic analysis. | |||
903 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, | |||
904 | TemplateArgumentListInfo &TemplateArgs) { | |||
905 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) | |||
906 | TemplateArgs.addArgument(translateTemplateArgument(*this, | |||
907 | TemplateArgsIn[I])); | |||
908 | } | |||
909 | ||||
910 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, | |||
911 | SourceLocation Loc, | |||
912 | IdentifierInfo *Name) { | |||
913 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( | |||
914 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); | |||
915 | if (PrevDecl && PrevDecl->isTemplateParameter()) | |||
916 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); | |||
917 | } | |||
918 | ||||
919 | /// Convert a parsed type into a parsed template argument. This is mostly | |||
920 | /// trivial, except that we may have parsed a C++17 deduced class template | |||
921 | /// specialization type, in which case we should form a template template | |||
922 | /// argument instead of a type template argument. | |||
923 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { | |||
924 | TypeSourceInfo *TInfo; | |||
925 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); | |||
926 | if (T.isNull()) | |||
927 | return ParsedTemplateArgument(); | |||
928 | assert(TInfo && "template argument with no location")((TInfo && "template argument with no location") ? static_cast <void> (0) : __assert_fail ("TInfo && \"template argument with no location\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 928, __PRETTY_FUNCTION__)); | |||
929 | ||||
930 | // If we might have formed a deduced template specialization type, convert | |||
931 | // it to a template template argument. | |||
932 | if (getLangOpts().CPlusPlus17) { | |||
933 | TypeLoc TL = TInfo->getTypeLoc(); | |||
934 | SourceLocation EllipsisLoc; | |||
935 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { | |||
936 | EllipsisLoc = PET.getEllipsisLoc(); | |||
937 | TL = PET.getPatternLoc(); | |||
938 | } | |||
939 | ||||
940 | CXXScopeSpec SS; | |||
941 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { | |||
942 | SS.Adopt(ET.getQualifierLoc()); | |||
943 | TL = ET.getNamedTypeLoc(); | |||
944 | } | |||
945 | ||||
946 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { | |||
947 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); | |||
948 | if (SS.isSet()) | |||
949 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), | |||
950 | /*HasTemplateKeyword*/ false, | |||
951 | Name.getAsTemplateDecl()); | |||
952 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), | |||
953 | DTST.getTemplateNameLoc()); | |||
954 | if (EllipsisLoc.isValid()) | |||
955 | Result = Result.getTemplatePackExpansion(EllipsisLoc); | |||
956 | return Result; | |||
957 | } | |||
958 | } | |||
959 | ||||
960 | // This is a normal type template argument. Note, if the type template | |||
961 | // argument is an injected-class-name for a template, it has a dual nature | |||
962 | // and can be used as either a type or a template. We handle that in | |||
963 | // convertTypeTemplateArgumentToTemplate. | |||
964 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, | |||
965 | ParsedType.get().getAsOpaquePtr(), | |||
966 | TInfo->getTypeLoc().getBeginLoc()); | |||
967 | } | |||
968 | ||||
969 | /// ActOnTypeParameter - Called when a C++ template type parameter | |||
970 | /// (e.g., "typename T") has been parsed. Typename specifies whether | |||
971 | /// the keyword "typename" was used to declare the type parameter | |||
972 | /// (otherwise, "class" was used), and KeyLoc is the location of the | |||
973 | /// "class" or "typename" keyword. ParamName is the name of the | |||
974 | /// parameter (NULL indicates an unnamed template parameter) and | |||
975 | /// ParamNameLoc is the location of the parameter name (if any). | |||
976 | /// If the type parameter has a default argument, it will be added | |||
977 | /// later via ActOnTypeParameterDefault. | |||
978 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, | |||
979 | SourceLocation EllipsisLoc, | |||
980 | SourceLocation KeyLoc, | |||
981 | IdentifierInfo *ParamName, | |||
982 | SourceLocation ParamNameLoc, | |||
983 | unsigned Depth, unsigned Position, | |||
984 | SourceLocation EqualLoc, | |||
985 | ParsedType DefaultArg) { | |||
986 | assert(S->isTemplateParamScope() &&((S->isTemplateParamScope() && "Template type parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Template type parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 987, __PRETTY_FUNCTION__)) | |||
987 | "Template type parameter not in template parameter scope!")((S->isTemplateParamScope() && "Template type parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Template type parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 987, __PRETTY_FUNCTION__)); | |||
988 | ||||
989 | bool IsParameterPack = EllipsisLoc.isValid(); | |||
990 | TemplateTypeParmDecl *Param = TemplateTypeParmDecl::Create( | |||
991 | Context, Context.getTranslationUnitDecl(), KeyLoc, ParamNameLoc, Depth, | |||
992 | Position, ParamName, Typename, IsParameterPack); | |||
993 | Param->setAccess(AS_public); | |||
994 | ||||
995 | if (Param->isParameterPack()) | |||
996 | if (auto *LSI = getEnclosingLambda()) | |||
997 | LSI->LocalPacks.push_back(Param); | |||
998 | ||||
999 | if (ParamName) { | |||
1000 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); | |||
1001 | ||||
1002 | // Add the template parameter into the current scope. | |||
1003 | S->AddDecl(Param); | |||
1004 | IdResolver.AddDecl(Param); | |||
1005 | } | |||
1006 | ||||
1007 | // C++0x [temp.param]p9: | |||
1008 | // A default template-argument may be specified for any kind of | |||
1009 | // template-parameter that is not a template parameter pack. | |||
1010 | if (DefaultArg && IsParameterPack) { | |||
1011 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | |||
1012 | DefaultArg = nullptr; | |||
1013 | } | |||
1014 | ||||
1015 | // Handle the default argument, if provided. | |||
1016 | if (DefaultArg) { | |||
1017 | TypeSourceInfo *DefaultTInfo; | |||
1018 | GetTypeFromParser(DefaultArg, &DefaultTInfo); | |||
1019 | ||||
1020 | assert(DefaultTInfo && "expected source information for type")((DefaultTInfo && "expected source information for type" ) ? static_cast<void> (0) : __assert_fail ("DefaultTInfo && \"expected source information for type\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1020, __PRETTY_FUNCTION__)); | |||
1021 | ||||
1022 | // Check for unexpanded parameter packs. | |||
1023 | if (DiagnoseUnexpandedParameterPack(ParamNameLoc, DefaultTInfo, | |||
1024 | UPPC_DefaultArgument)) | |||
1025 | return Param; | |||
1026 | ||||
1027 | // Check the template argument itself. | |||
1028 | if (CheckTemplateArgument(Param, DefaultTInfo)) { | |||
1029 | Param->setInvalidDecl(); | |||
1030 | return Param; | |||
1031 | } | |||
1032 | ||||
1033 | Param->setDefaultArgument(DefaultTInfo); | |||
1034 | } | |||
1035 | ||||
1036 | return Param; | |||
1037 | } | |||
1038 | ||||
1039 | /// Check that the type of a non-type template parameter is | |||
1040 | /// well-formed. | |||
1041 | /// | |||
1042 | /// \returns the (possibly-promoted) parameter type if valid; | |||
1043 | /// otherwise, produces a diagnostic and returns a NULL type. | |||
1044 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, | |||
1045 | SourceLocation Loc) { | |||
1046 | if (TSI->getType()->isUndeducedType()) { | |||
1047 | // C++17 [temp.dep.expr]p3: | |||
1048 | // An id-expression is type-dependent if it contains | |||
1049 | // - an identifier associated by name lookup with a non-type | |||
1050 | // template-parameter declared with a type that contains a | |||
1051 | // placeholder type (7.1.7.4), | |||
1052 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); | |||
1053 | } | |||
1054 | ||||
1055 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); | |||
1056 | } | |||
1057 | ||||
1058 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, | |||
1059 | SourceLocation Loc) { | |||
1060 | // We don't allow variably-modified types as the type of non-type template | |||
1061 | // parameters. | |||
1062 | if (T->isVariablyModifiedType()) { | |||
1063 | Diag(Loc, diag::err_variably_modified_nontype_template_param) | |||
1064 | << T; | |||
1065 | return QualType(); | |||
1066 | } | |||
1067 | ||||
1068 | // C++ [temp.param]p4: | |||
1069 | // | |||
1070 | // A non-type template-parameter shall have one of the following | |||
1071 | // (optionally cv-qualified) types: | |||
1072 | // | |||
1073 | // -- integral or enumeration type, | |||
1074 | if (T->isIntegralOrEnumerationType() || | |||
1075 | // -- pointer to object or pointer to function, | |||
1076 | T->isPointerType() || | |||
1077 | // -- reference to object or reference to function, | |||
1078 | T->isReferenceType() || | |||
1079 | // -- pointer to member, | |||
1080 | T->isMemberPointerType() || | |||
1081 | // -- std::nullptr_t. | |||
1082 | T->isNullPtrType() || | |||
1083 | // Allow use of auto in template parameter declarations. | |||
1084 | T->isUndeducedType()) { | |||
1085 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter | |||
1086 | // are ignored when determining its type. | |||
1087 | return T.getUnqualifiedType(); | |||
1088 | } | |||
1089 | ||||
1090 | // C++ [temp.param]p8: | |||
1091 | // | |||
1092 | // A non-type template-parameter of type "array of T" or | |||
1093 | // "function returning T" is adjusted to be of type "pointer to | |||
1094 | // T" or "pointer to function returning T", respectively. | |||
1095 | if (T->isArrayType() || T->isFunctionType()) | |||
1096 | return Context.getDecayedType(T); | |||
1097 | ||||
1098 | // If T is a dependent type, we can't do the check now, so we | |||
1099 | // assume that it is well-formed. Note that stripping off the | |||
1100 | // qualifiers here is not really correct if T turns out to be | |||
1101 | // an array type, but we'll recompute the type everywhere it's | |||
1102 | // used during instantiation, so that should be OK. (Using the | |||
1103 | // qualified type is equally wrong.) | |||
1104 | if (T->isDependentType()) | |||
1105 | return T.getUnqualifiedType(); | |||
1106 | ||||
1107 | Diag(Loc, diag::err_template_nontype_parm_bad_type) | |||
1108 | << T; | |||
1109 | ||||
1110 | return QualType(); | |||
1111 | } | |||
1112 | ||||
1113 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, | |||
1114 | unsigned Depth, | |||
1115 | unsigned Position, | |||
1116 | SourceLocation EqualLoc, | |||
1117 | Expr *Default) { | |||
1118 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); | |||
1119 | ||||
1120 | // Check that we have valid decl-specifiers specified. | |||
1121 | auto CheckValidDeclSpecifiers = [this, &D] { | |||
1122 | // C++ [temp.param] | |||
1123 | // p1 | |||
1124 | // template-parameter: | |||
1125 | // ... | |||
1126 | // parameter-declaration | |||
1127 | // p2 | |||
1128 | // ... A storage class shall not be specified in a template-parameter | |||
1129 | // declaration. | |||
1130 | // [dcl.typedef]p1: | |||
1131 | // The typedef specifier [...] shall not be used in the decl-specifier-seq | |||
1132 | // of a parameter-declaration | |||
1133 | const DeclSpec &DS = D.getDeclSpec(); | |||
1134 | auto EmitDiag = [this](SourceLocation Loc) { | |||
1135 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) | |||
1136 | << FixItHint::CreateRemoval(Loc); | |||
1137 | }; | |||
1138 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) | |||
1139 | EmitDiag(DS.getStorageClassSpecLoc()); | |||
1140 | ||||
1141 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) | |||
1142 | EmitDiag(DS.getThreadStorageClassSpecLoc()); | |||
1143 | ||||
1144 | // [dcl.inline]p1: | |||
1145 | // The inline specifier can be applied only to the declaration or | |||
1146 | // definition of a variable or function. | |||
1147 | ||||
1148 | if (DS.isInlineSpecified()) | |||
1149 | EmitDiag(DS.getInlineSpecLoc()); | |||
1150 | ||||
1151 | // [dcl.constexpr]p1: | |||
1152 | // The constexpr specifier shall be applied only to the definition of a | |||
1153 | // variable or variable template or the declaration of a function or | |||
1154 | // function template. | |||
1155 | ||||
1156 | if (DS.hasConstexprSpecifier()) | |||
1157 | EmitDiag(DS.getConstexprSpecLoc()); | |||
1158 | ||||
1159 | // [dcl.fct.spec]p1: | |||
1160 | // Function-specifiers can be used only in function declarations. | |||
1161 | ||||
1162 | if (DS.isVirtualSpecified()) | |||
1163 | EmitDiag(DS.getVirtualSpecLoc()); | |||
1164 | ||||
1165 | if (DS.hasExplicitSpecifier()) | |||
1166 | EmitDiag(DS.getExplicitSpecLoc()); | |||
1167 | ||||
1168 | if (DS.isNoreturnSpecified()) | |||
1169 | EmitDiag(DS.getNoreturnSpecLoc()); | |||
1170 | }; | |||
1171 | ||||
1172 | CheckValidDeclSpecifiers(); | |||
1173 | ||||
1174 | if (TInfo->getType()->isUndeducedType()) { | |||
1175 | Diag(D.getIdentifierLoc(), | |||
1176 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) | |||
1177 | << QualType(TInfo->getType()->getContainedAutoType(), 0); | |||
1178 | } | |||
1179 | ||||
1180 | assert(S->isTemplateParamScope() &&((S->isTemplateParamScope() && "Non-type template parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Non-type template parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1181, __PRETTY_FUNCTION__)) | |||
1181 | "Non-type template parameter not in template parameter scope!")((S->isTemplateParamScope() && "Non-type template parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Non-type template parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1181, __PRETTY_FUNCTION__)); | |||
1182 | bool Invalid = false; | |||
1183 | ||||
1184 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); | |||
1185 | if (T.isNull()) { | |||
1186 | T = Context.IntTy; // Recover with an 'int' type. | |||
1187 | Invalid = true; | |||
1188 | } | |||
1189 | ||||
1190 | CheckFunctionOrTemplateParamDeclarator(S, D); | |||
1191 | ||||
1192 | IdentifierInfo *ParamName = D.getIdentifier(); | |||
1193 | bool IsParameterPack = D.hasEllipsis(); | |||
1194 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( | |||
1195 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), | |||
1196 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, | |||
1197 | TInfo); | |||
1198 | Param->setAccess(AS_public); | |||
1199 | ||||
1200 | if (Invalid) | |||
1201 | Param->setInvalidDecl(); | |||
1202 | ||||
1203 | if (Param->isParameterPack()) | |||
1204 | if (auto *LSI = getEnclosingLambda()) | |||
1205 | LSI->LocalPacks.push_back(Param); | |||
1206 | ||||
1207 | if (ParamName) { | |||
1208 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), | |||
1209 | ParamName); | |||
1210 | ||||
1211 | // Add the template parameter into the current scope. | |||
1212 | S->AddDecl(Param); | |||
1213 | IdResolver.AddDecl(Param); | |||
1214 | } | |||
1215 | ||||
1216 | // C++0x [temp.param]p9: | |||
1217 | // A default template-argument may be specified for any kind of | |||
1218 | // template-parameter that is not a template parameter pack. | |||
1219 | if (Default && IsParameterPack) { | |||
1220 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | |||
1221 | Default = nullptr; | |||
1222 | } | |||
1223 | ||||
1224 | // Check the well-formedness of the default template argument, if provided. | |||
1225 | if (Default) { | |||
1226 | // Check for unexpanded parameter packs. | |||
1227 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) | |||
1228 | return Param; | |||
1229 | ||||
1230 | TemplateArgument Converted; | |||
1231 | ExprResult DefaultRes = | |||
1232 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); | |||
1233 | if (DefaultRes.isInvalid()) { | |||
1234 | Param->setInvalidDecl(); | |||
1235 | return Param; | |||
1236 | } | |||
1237 | Default = DefaultRes.get(); | |||
1238 | ||||
1239 | Param->setDefaultArgument(Default); | |||
1240 | } | |||
1241 | ||||
1242 | return Param; | |||
1243 | } | |||
1244 | ||||
1245 | /// ActOnTemplateTemplateParameter - Called when a C++ template template | |||
1246 | /// parameter (e.g. T in template <template \<typename> class T> class array) | |||
1247 | /// has been parsed. S is the current scope. | |||
1248 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, | |||
1249 | SourceLocation TmpLoc, | |||
1250 | TemplateParameterList *Params, | |||
1251 | SourceLocation EllipsisLoc, | |||
1252 | IdentifierInfo *Name, | |||
1253 | SourceLocation NameLoc, | |||
1254 | unsigned Depth, | |||
1255 | unsigned Position, | |||
1256 | SourceLocation EqualLoc, | |||
1257 | ParsedTemplateArgument Default) { | |||
1258 | assert(S->isTemplateParamScope() &&((S->isTemplateParamScope() && "Template template parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Template template parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1259, __PRETTY_FUNCTION__)) | |||
1259 | "Template template parameter not in template parameter scope!")((S->isTemplateParamScope() && "Template template parameter not in template parameter scope!" ) ? static_cast<void> (0) : __assert_fail ("S->isTemplateParamScope() && \"Template template parameter not in template parameter scope!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1259, __PRETTY_FUNCTION__)); | |||
1260 | ||||
1261 | // Construct the parameter object. | |||
1262 | bool IsParameterPack = EllipsisLoc.isValid(); | |||
1263 | TemplateTemplateParmDecl *Param = | |||
1264 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), | |||
1265 | NameLoc.isInvalid()? TmpLoc : NameLoc, | |||
1266 | Depth, Position, IsParameterPack, | |||
1267 | Name, Params); | |||
1268 | Param->setAccess(AS_public); | |||
1269 | ||||
1270 | if (Param->isParameterPack()) | |||
1271 | if (auto *LSI = getEnclosingLambda()) | |||
1272 | LSI->LocalPacks.push_back(Param); | |||
1273 | ||||
1274 | // If the template template parameter has a name, then link the identifier | |||
1275 | // into the scope and lookup mechanisms. | |||
1276 | if (Name) { | |||
1277 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); | |||
1278 | ||||
1279 | S->AddDecl(Param); | |||
1280 | IdResolver.AddDecl(Param); | |||
1281 | } | |||
1282 | ||||
1283 | if (Params->size() == 0) { | |||
1284 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) | |||
1285 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); | |||
1286 | Param->setInvalidDecl(); | |||
1287 | } | |||
1288 | ||||
1289 | // C++0x [temp.param]p9: | |||
1290 | // A default template-argument may be specified for any kind of | |||
1291 | // template-parameter that is not a template parameter pack. | |||
1292 | if (IsParameterPack && !Default.isInvalid()) { | |||
1293 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | |||
1294 | Default = ParsedTemplateArgument(); | |||
1295 | } | |||
1296 | ||||
1297 | if (!Default.isInvalid()) { | |||
1298 | // Check only that we have a template template argument. We don't want to | |||
1299 | // try to check well-formedness now, because our template template parameter | |||
1300 | // might have dependent types in its template parameters, which we wouldn't | |||
1301 | // be able to match now. | |||
1302 | // | |||
1303 | // If none of the template template parameter's template arguments mention | |||
1304 | // other template parameters, we could actually perform more checking here. | |||
1305 | // However, it isn't worth doing. | |||
1306 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); | |||
1307 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { | |||
1308 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) | |||
1309 | << DefaultArg.getSourceRange(); | |||
1310 | return Param; | |||
1311 | } | |||
1312 | ||||
1313 | // Check for unexpanded parameter packs. | |||
1314 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), | |||
1315 | DefaultArg.getArgument().getAsTemplate(), | |||
1316 | UPPC_DefaultArgument)) | |||
1317 | return Param; | |||
1318 | ||||
1319 | Param->setDefaultArgument(Context, DefaultArg); | |||
1320 | } | |||
1321 | ||||
1322 | return Param; | |||
1323 | } | |||
1324 | ||||
1325 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally | |||
1326 | /// constrained by RequiresClause, that contains the template parameters in | |||
1327 | /// Params. | |||
1328 | TemplateParameterList * | |||
1329 | Sema::ActOnTemplateParameterList(unsigned Depth, | |||
1330 | SourceLocation ExportLoc, | |||
1331 | SourceLocation TemplateLoc, | |||
1332 | SourceLocation LAngleLoc, | |||
1333 | ArrayRef<NamedDecl *> Params, | |||
1334 | SourceLocation RAngleLoc, | |||
1335 | Expr *RequiresClause) { | |||
1336 | if (ExportLoc.isValid()) | |||
1337 | Diag(ExportLoc, diag::warn_template_export_unsupported); | |||
1338 | ||||
1339 | return TemplateParameterList::Create( | |||
1340 | Context, TemplateLoc, LAngleLoc, | |||
1341 | llvm::makeArrayRef(Params.data(), Params.size()), | |||
1342 | RAngleLoc, RequiresClause); | |||
1343 | } | |||
1344 | ||||
1345 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, | |||
1346 | const CXXScopeSpec &SS) { | |||
1347 | if (SS.isSet()) | |||
1348 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); | |||
1349 | } | |||
1350 | ||||
1351 | DeclResult Sema::CheckClassTemplate( | |||
1352 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, | |||
1353 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, | |||
1354 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, | |||
1355 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, | |||
1356 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, | |||
1357 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { | |||
1358 | assert(TemplateParams && TemplateParams->size() > 0 &&((TemplateParams && TemplateParams->size() > 0 && "No template parameters") ? static_cast<void> (0) : __assert_fail ("TemplateParams && TemplateParams->size() > 0 && \"No template parameters\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1359, __PRETTY_FUNCTION__)) | |||
| ||||
1359 | "No template parameters")((TemplateParams && TemplateParams->size() > 0 && "No template parameters") ? static_cast<void> (0) : __assert_fail ("TemplateParams && TemplateParams->size() > 0 && \"No template parameters\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1359, __PRETTY_FUNCTION__)); | |||
1360 | assert(TUK != TUK_Reference && "Can only declare or define class templates")((TUK != TUK_Reference && "Can only declare or define class templates" ) ? static_cast<void> (0) : __assert_fail ("TUK != TUK_Reference && \"Can only declare or define class templates\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1360, __PRETTY_FUNCTION__)); | |||
1361 | bool Invalid = false; | |||
1362 | ||||
1363 | // Check that we can declare a template here. | |||
1364 | if (CheckTemplateDeclScope(S, TemplateParams)) | |||
1365 | return true; | |||
1366 | ||||
1367 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | |||
1368 | assert(Kind != TTK_Enum && "can't build template of enumerated type")((Kind != TTK_Enum && "can't build template of enumerated type" ) ? static_cast<void> (0) : __assert_fail ("Kind != TTK_Enum && \"can't build template of enumerated type\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1368, __PRETTY_FUNCTION__)); | |||
1369 | ||||
1370 | // There is no such thing as an unnamed class template. | |||
1371 | if (!Name) { | |||
1372 | Diag(KWLoc, diag::err_template_unnamed_class); | |||
1373 | return true; | |||
1374 | } | |||
1375 | ||||
1376 | // Find any previous declaration with this name. For a friend with no | |||
1377 | // scope explicitly specified, we only look for tag declarations (per | |||
1378 | // C++11 [basic.lookup.elab]p2). | |||
1379 | DeclContext *SemanticContext; | |||
1380 | LookupResult Previous(*this, Name, NameLoc, | |||
1381 | (SS.isEmpty() && TUK == TUK_Friend) | |||
1382 | ? LookupTagName : LookupOrdinaryName, | |||
1383 | forRedeclarationInCurContext()); | |||
1384 | if (SS.isNotEmpty() && !SS.isInvalid()) { | |||
1385 | SemanticContext = computeDeclContext(SS, true); | |||
1386 | if (!SemanticContext) { | |||
1387 | // FIXME: Horrible, horrible hack! We can't currently represent this | |||
1388 | // in the AST, and historically we have just ignored such friend | |||
1389 | // class templates, so don't complain here. | |||
1390 | Diag(NameLoc, TUK == TUK_Friend | |||
1391 | ? diag::warn_template_qualified_friend_ignored | |||
1392 | : diag::err_template_qualified_declarator_no_match) | |||
1393 | << SS.getScopeRep() << SS.getRange(); | |||
1394 | return TUK != TUK_Friend; | |||
1395 | } | |||
1396 | ||||
1397 | if (RequireCompleteDeclContext(SS, SemanticContext)) | |||
1398 | return true; | |||
1399 | ||||
1400 | // If we're adding a template to a dependent context, we may need to | |||
1401 | // rebuilding some of the types used within the template parameter list, | |||
1402 | // now that we know what the current instantiation is. | |||
1403 | if (SemanticContext->isDependentContext()) { | |||
1404 | ContextRAII SavedContext(*this, SemanticContext); | |||
1405 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) | |||
1406 | Invalid = true; | |||
1407 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) | |||
1408 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); | |||
1409 | ||||
1410 | LookupQualifiedName(Previous, SemanticContext); | |||
1411 | } else { | |||
1412 | SemanticContext = CurContext; | |||
1413 | ||||
1414 | // C++14 [class.mem]p14: | |||
1415 | // If T is the name of a class, then each of the following shall have a | |||
1416 | // name different from T: | |||
1417 | // -- every member template of class T | |||
1418 | if (TUK != TUK_Friend && | |||
1419 | DiagnoseClassNameShadow(SemanticContext, | |||
1420 | DeclarationNameInfo(Name, NameLoc))) | |||
1421 | return true; | |||
1422 | ||||
1423 | LookupName(Previous, S); | |||
1424 | } | |||
1425 | ||||
1426 | if (Previous.isAmbiguous()) | |||
1427 | return true; | |||
1428 | ||||
1429 | NamedDecl *PrevDecl = nullptr; | |||
1430 | if (Previous.begin() != Previous.end()) | |||
1431 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | |||
1432 | ||||
1433 | if (PrevDecl && PrevDecl->isTemplateParameter()) { | |||
1434 | // Maybe we will complain about the shadowed template parameter. | |||
1435 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); | |||
1436 | // Just pretend that we didn't see the previous declaration. | |||
1437 | PrevDecl = nullptr; | |||
1438 | } | |||
1439 | ||||
1440 | // If there is a previous declaration with the same name, check | |||
1441 | // whether this is a valid redeclaration. | |||
1442 | ClassTemplateDecl *PrevClassTemplate = | |||
1443 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); | |||
1444 | ||||
1445 | // We may have found the injected-class-name of a class template, | |||
1446 | // class template partial specialization, or class template specialization. | |||
1447 | // In these cases, grab the template that is being defined or specialized. | |||
1448 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && | |||
1449 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { | |||
1450 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); | |||
1451 | PrevClassTemplate | |||
1452 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); | |||
1453 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { | |||
1454 | PrevClassTemplate | |||
1455 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) | |||
1456 | ->getSpecializedTemplate(); | |||
1457 | } | |||
1458 | } | |||
1459 | ||||
1460 | if (TUK == TUK_Friend) { | |||
1461 | // C++ [namespace.memdef]p3: | |||
1462 | // [...] When looking for a prior declaration of a class or a function | |||
1463 | // declared as a friend, and when the name of the friend class or | |||
1464 | // function is neither a qualified name nor a template-id, scopes outside | |||
1465 | // the innermost enclosing namespace scope are not considered. | |||
1466 | if (!SS.isSet()) { | |||
1467 | DeclContext *OutermostContext = CurContext; | |||
1468 | while (!OutermostContext->isFileContext()) | |||
1469 | OutermostContext = OutermostContext->getLookupParent(); | |||
1470 | ||||
1471 | if (PrevDecl && | |||
1472 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || | |||
1473 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { | |||
1474 | SemanticContext = PrevDecl->getDeclContext(); | |||
1475 | } else { | |||
1476 | // Declarations in outer scopes don't matter. However, the outermost | |||
1477 | // context we computed is the semantic context for our new | |||
1478 | // declaration. | |||
1479 | PrevDecl = PrevClassTemplate = nullptr; | |||
1480 | SemanticContext = OutermostContext; | |||
1481 | ||||
1482 | // Check that the chosen semantic context doesn't already contain a | |||
1483 | // declaration of this name as a non-tag type. | |||
1484 | Previous.clear(LookupOrdinaryName); | |||
1485 | DeclContext *LookupContext = SemanticContext; | |||
1486 | while (LookupContext->isTransparentContext()) | |||
1487 | LookupContext = LookupContext->getLookupParent(); | |||
1488 | LookupQualifiedName(Previous, LookupContext); | |||
1489 | ||||
1490 | if (Previous.isAmbiguous()) | |||
1491 | return true; | |||
1492 | ||||
1493 | if (Previous.begin() != Previous.end()) | |||
1494 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | |||
1495 | } | |||
1496 | } | |||
1497 | } else if (PrevDecl && | |||
1498 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, | |||
1499 | S, SS.isValid())) | |||
1500 | PrevDecl = PrevClassTemplate = nullptr; | |||
1501 | ||||
1502 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( | |||
1503 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { | |||
1504 | if (SS.isEmpty() && | |||
1505 | !(PrevClassTemplate && | |||
1506 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( | |||
1507 | SemanticContext->getRedeclContext()))) { | |||
1508 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); | |||
1509 | Diag(Shadow->getTargetDecl()->getLocation(), | |||
1510 | diag::note_using_decl_target); | |||
1511 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; | |||
1512 | // Recover by ignoring the old declaration. | |||
1513 | PrevDecl = PrevClassTemplate = nullptr; | |||
1514 | } | |||
1515 | } | |||
1516 | ||||
1517 | if (PrevClassTemplate) { | |||
1518 | // Ensure that the template parameter lists are compatible. Skip this check | |||
1519 | // for a friend in a dependent context: the template parameter list itself | |||
1520 | // could be dependent. | |||
1521 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && | |||
1522 | !TemplateParameterListsAreEqual(TemplateParams, | |||
1523 | PrevClassTemplate->getTemplateParameters(), | |||
1524 | /*Complain=*/true, | |||
1525 | TPL_TemplateMatch)) | |||
1526 | return true; | |||
1527 | ||||
1528 | // C++ [temp.class]p4: | |||
1529 | // In a redeclaration, partial specialization, explicit | |||
1530 | // specialization or explicit instantiation of a class template, | |||
1531 | // the class-key shall agree in kind with the original class | |||
1532 | // template declaration (7.1.5.3). | |||
1533 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); | |||
1534 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, | |||
1535 | TUK == TUK_Definition, KWLoc, Name)) { | |||
1536 | Diag(KWLoc, diag::err_use_with_wrong_tag) | |||
1537 | << Name | |||
1538 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); | |||
1539 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); | |||
1540 | Kind = PrevRecordDecl->getTagKind(); | |||
1541 | } | |||
1542 | ||||
1543 | // Check for redefinition of this class template. | |||
1544 | if (TUK == TUK_Definition) { | |||
1545 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { | |||
1546 | // If we have a prior definition that is not visible, treat this as | |||
1547 | // simply making that previous definition visible. | |||
1548 | NamedDecl *Hidden = nullptr; | |||
1549 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { | |||
1550 | SkipBody->ShouldSkip = true; | |||
1551 | SkipBody->Previous = Def; | |||
1552 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); | |||
1553 | assert(Tmpl && "original definition of a class template is not a "((Tmpl && "original definition of a class template is not a " "class template?") ? static_cast<void> (0) : __assert_fail ("Tmpl && \"original definition of a class template is not a \" \"class template?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1554, __PRETTY_FUNCTION__)) | |||
1554 | "class template?")((Tmpl && "original definition of a class template is not a " "class template?") ? static_cast<void> (0) : __assert_fail ("Tmpl && \"original definition of a class template is not a \" \"class template?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1554, __PRETTY_FUNCTION__)); | |||
1555 | makeMergedDefinitionVisible(Hidden); | |||
1556 | makeMergedDefinitionVisible(Tmpl); | |||
1557 | } else { | |||
1558 | Diag(NameLoc, diag::err_redefinition) << Name; | |||
1559 | Diag(Def->getLocation(), diag::note_previous_definition); | |||
1560 | // FIXME: Would it make sense to try to "forget" the previous | |||
1561 | // definition, as part of error recovery? | |||
1562 | return true; | |||
1563 | } | |||
1564 | } | |||
1565 | } | |||
1566 | } else if (PrevDecl) { | |||
1567 | // C++ [temp]p5: | |||
1568 | // A class template shall not have the same name as any other | |||
1569 | // template, class, function, object, enumeration, enumerator, | |||
1570 | // namespace, or type in the same scope (3.3), except as specified | |||
1571 | // in (14.5.4). | |||
1572 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; | |||
1573 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); | |||
1574 | return true; | |||
1575 | } | |||
1576 | ||||
1577 | // Check the template parameter list of this declaration, possibly | |||
1578 | // merging in the template parameter list from the previous class | |||
1579 | // template declaration. Skip this check for a friend in a dependent | |||
1580 | // context, because the template parameter list might be dependent. | |||
1581 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && | |||
1582 | CheckTemplateParameterList( | |||
1583 | TemplateParams, | |||
1584 | PrevClassTemplate | |||
1585 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() | |||
1586 | : nullptr, | |||
1587 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && | |||
1588 | SemanticContext->isDependentContext()) | |||
1589 | ? TPC_ClassTemplateMember | |||
1590 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, | |||
1591 | SkipBody)) | |||
1592 | Invalid = true; | |||
1593 | ||||
1594 | if (SS.isSet()) { | |||
1595 | // If the name of the template was qualified, we must be defining the | |||
1596 | // template out-of-line. | |||
1597 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { | |||
1598 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match | |||
1599 | : diag::err_member_decl_does_not_match) | |||
1600 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); | |||
1601 | Invalid = true; | |||
1602 | } | |||
1603 | } | |||
1604 | ||||
1605 | // If this is a templated friend in a dependent context we should not put it | |||
1606 | // on the redecl chain. In some cases, the templated friend can be the most | |||
1607 | // recent declaration tricking the template instantiator to make substitutions | |||
1608 | // there. | |||
1609 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious | |||
1610 | bool ShouldAddRedecl | |||
1611 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); | |||
1612 | ||||
1613 | CXXRecordDecl *NewClass = | |||
1614 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, | |||
1615 | PrevClassTemplate && ShouldAddRedecl ? | |||
1616 | PrevClassTemplate->getTemplatedDecl() : nullptr, | |||
1617 | /*DelayTypeCreation=*/true); | |||
1618 | SetNestedNameSpecifier(*this, NewClass, SS); | |||
1619 | if (NumOuterTemplateParamLists > 0) | |||
1620 | NewClass->setTemplateParameterListsInfo( | |||
1621 | Context, llvm::makeArrayRef(OuterTemplateParamLists, | |||
1622 | NumOuterTemplateParamLists)); | |||
1623 | ||||
1624 | // Add alignment attributes if necessary; these attributes are checked when | |||
1625 | // the ASTContext lays out the structure. | |||
1626 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { | |||
1627 | AddAlignmentAttributesForRecord(NewClass); | |||
1628 | AddMsStructLayoutForRecord(NewClass); | |||
1629 | } | |||
1630 | ||||
1631 | ClassTemplateDecl *NewTemplate | |||
1632 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, | |||
1633 | DeclarationName(Name), TemplateParams, | |||
1634 | NewClass); | |||
1635 | ||||
1636 | if (ShouldAddRedecl) | |||
1637 | NewTemplate->setPreviousDecl(PrevClassTemplate); | |||
1638 | ||||
1639 | NewClass->setDescribedClassTemplate(NewTemplate); | |||
1640 | ||||
1641 | if (ModulePrivateLoc.isValid()) | |||
1642 | NewTemplate->setModulePrivate(); | |||
1643 | ||||
1644 | // Build the type for the class template declaration now. | |||
1645 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); | |||
1646 | T = Context.getInjectedClassNameType(NewClass, T); | |||
1647 | assert(T->isDependentType() && "Class template type is not dependent?")((T->isDependentType() && "Class template type is not dependent?" ) ? static_cast<void> (0) : __assert_fail ("T->isDependentType() && \"Class template type is not dependent?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1647, __PRETTY_FUNCTION__)); | |||
1648 | (void)T; | |||
1649 | ||||
1650 | // If we are providing an explicit specialization of a member that is a | |||
1651 | // class template, make a note of that. | |||
1652 | if (PrevClassTemplate && | |||
1653 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) | |||
1654 | PrevClassTemplate->setMemberSpecialization(); | |||
1655 | ||||
1656 | // Set the access specifier. | |||
1657 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) | |||
1658 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); | |||
1659 | ||||
1660 | // Set the lexical context of these templates | |||
1661 | NewClass->setLexicalDeclContext(CurContext); | |||
1662 | NewTemplate->setLexicalDeclContext(CurContext); | |||
1663 | ||||
1664 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) | |||
1665 | NewClass->startDefinition(); | |||
1666 | ||||
1667 | ProcessDeclAttributeList(S, NewClass, Attr); | |||
1668 | ||||
1669 | if (PrevClassTemplate) | |||
1670 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); | |||
1671 | ||||
1672 | AddPushedVisibilityAttribute(NewClass); | |||
1673 | inferGslOwnerPointerAttribute(NewClass); | |||
1674 | ||||
1675 | if (TUK != TUK_Friend) { | |||
1676 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. | |||
1677 | Scope *Outer = S; | |||
1678 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) | |||
1679 | Outer = Outer->getParent(); | |||
1680 | PushOnScopeChains(NewTemplate, Outer); | |||
1681 | } else { | |||
1682 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { | |||
1683 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); | |||
1684 | NewClass->setAccess(PrevClassTemplate->getAccess()); | |||
1685 | } | |||
1686 | ||||
1687 | NewTemplate->setObjectOfFriendDecl(); | |||
1688 | ||||
1689 | // Friend templates are visible in fairly strange ways. | |||
1690 | if (!CurContext->isDependentContext()) { | |||
1691 | DeclContext *DC = SemanticContext->getRedeclContext(); | |||
1692 | DC->makeDeclVisibleInContext(NewTemplate); | |||
1693 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) | |||
1694 | PushOnScopeChains(NewTemplate, EnclosingScope, | |||
1695 | /* AddToContext = */ false); | |||
1696 | } | |||
1697 | ||||
1698 | FriendDecl *Friend = FriendDecl::Create( | |||
1699 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); | |||
1700 | Friend->setAccess(AS_public); | |||
1701 | CurContext->addDecl(Friend); | |||
1702 | } | |||
1703 | ||||
1704 | if (PrevClassTemplate) | |||
1705 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); | |||
1706 | ||||
1707 | if (Invalid) { | |||
1708 | NewTemplate->setInvalidDecl(); | |||
1709 | NewClass->setInvalidDecl(); | |||
1710 | } | |||
1711 | ||||
1712 | ActOnDocumentableDecl(NewTemplate); | |||
1713 | ||||
1714 | if (SkipBody && SkipBody->ShouldSkip) | |||
1715 | return SkipBody->Previous; | |||
1716 | ||||
1717 | return NewTemplate; | |||
1718 | } | |||
1719 | ||||
1720 | namespace { | |||
1721 | /// Tree transform to "extract" a transformed type from a class template's | |||
1722 | /// constructor to a deduction guide. | |||
1723 | class ExtractTypeForDeductionGuide | |||
1724 | : public TreeTransform<ExtractTypeForDeductionGuide> { | |||
1725 | public: | |||
1726 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; | |||
1727 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} | |||
1728 | ||||
1729 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } | |||
1730 | ||||
1731 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { | |||
1732 | return TransformType( | |||
1733 | TLB, | |||
1734 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); | |||
1735 | } | |||
1736 | }; | |||
1737 | ||||
1738 | /// Transform to convert portions of a constructor declaration into the | |||
1739 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. | |||
1740 | struct ConvertConstructorToDeductionGuideTransform { | |||
1741 | ConvertConstructorToDeductionGuideTransform(Sema &S, | |||
1742 | ClassTemplateDecl *Template) | |||
1743 | : SemaRef(S), Template(Template) {} | |||
1744 | ||||
1745 | Sema &SemaRef; | |||
1746 | ClassTemplateDecl *Template; | |||
1747 | ||||
1748 | DeclContext *DC = Template->getDeclContext(); | |||
1749 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); | |||
1750 | DeclarationName DeductionGuideName = | |||
1751 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); | |||
1752 | ||||
1753 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); | |||
1754 | ||||
1755 | // Index adjustment to apply to convert depth-1 template parameters into | |||
1756 | // depth-0 template parameters. | |||
1757 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); | |||
1758 | ||||
1759 | /// Transform a constructor declaration into a deduction guide. | |||
1760 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, | |||
1761 | CXXConstructorDecl *CD) { | |||
1762 | SmallVector<TemplateArgument, 16> SubstArgs; | |||
1763 | ||||
1764 | LocalInstantiationScope Scope(SemaRef); | |||
1765 | ||||
1766 | // C++ [over.match.class.deduct]p1: | |||
1767 | // -- For each constructor of the class template designated by the | |||
1768 | // template-name, a function template with the following properties: | |||
1769 | ||||
1770 | // -- The template parameters are the template parameters of the class | |||
1771 | // template followed by the template parameters (including default | |||
1772 | // template arguments) of the constructor, if any. | |||
1773 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | |||
1774 | if (FTD) { | |||
1775 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); | |||
1776 | SmallVector<NamedDecl *, 16> AllParams; | |||
1777 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); | |||
1778 | AllParams.insert(AllParams.begin(), | |||
1779 | TemplateParams->begin(), TemplateParams->end()); | |||
1780 | SubstArgs.reserve(InnerParams->size()); | |||
1781 | ||||
1782 | // Later template parameters could refer to earlier ones, so build up | |||
1783 | // a list of substituted template arguments as we go. | |||
1784 | for (NamedDecl *Param : *InnerParams) { | |||
1785 | MultiLevelTemplateArgumentList Args; | |||
1786 | Args.addOuterTemplateArguments(SubstArgs); | |||
1787 | Args.addOuterRetainedLevel(); | |||
1788 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); | |||
1789 | if (!NewParam) | |||
1790 | return nullptr; | |||
1791 | AllParams.push_back(NewParam); | |||
1792 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( | |||
1793 | SemaRef.Context.getInjectedTemplateArg(NewParam))); | |||
1794 | } | |||
1795 | TemplateParams = TemplateParameterList::Create( | |||
1796 | SemaRef.Context, InnerParams->getTemplateLoc(), | |||
1797 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), | |||
1798 | /*FIXME: RequiresClause*/ nullptr); | |||
1799 | } | |||
1800 | ||||
1801 | // If we built a new template-parameter-list, track that we need to | |||
1802 | // substitute references to the old parameters into references to the | |||
1803 | // new ones. | |||
1804 | MultiLevelTemplateArgumentList Args; | |||
1805 | if (FTD) { | |||
1806 | Args.addOuterTemplateArguments(SubstArgs); | |||
1807 | Args.addOuterRetainedLevel(); | |||
1808 | } | |||
1809 | ||||
1810 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() | |||
1811 | .getAsAdjusted<FunctionProtoTypeLoc>(); | |||
1812 | assert(FPTL && "no prototype for constructor declaration")((FPTL && "no prototype for constructor declaration") ? static_cast<void> (0) : __assert_fail ("FPTL && \"no prototype for constructor declaration\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1812, __PRETTY_FUNCTION__)); | |||
1813 | ||||
1814 | // Transform the type of the function, adjusting the return type and | |||
1815 | // replacing references to the old parameters with references to the | |||
1816 | // new ones. | |||
1817 | TypeLocBuilder TLB; | |||
1818 | SmallVector<ParmVarDecl*, 8> Params; | |||
1819 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); | |||
1820 | if (NewType.isNull()) | |||
1821 | return nullptr; | |||
1822 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); | |||
1823 | ||||
1824 | return buildDeductionGuide(TemplateParams, CD->getExplicitSpecifier(), | |||
1825 | NewTInfo, CD->getBeginLoc(), CD->getLocation(), | |||
1826 | CD->getEndLoc()); | |||
1827 | } | |||
1828 | ||||
1829 | /// Build a deduction guide with the specified parameter types. | |||
1830 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { | |||
1831 | SourceLocation Loc = Template->getLocation(); | |||
1832 | ||||
1833 | // Build the requested type. | |||
1834 | FunctionProtoType::ExtProtoInfo EPI; | |||
1835 | EPI.HasTrailingReturn = true; | |||
1836 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, | |||
1837 | DeductionGuideName, EPI); | |||
1838 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); | |||
1839 | ||||
1840 | FunctionProtoTypeLoc FPTL = | |||
1841 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); | |||
1842 | ||||
1843 | // Build the parameters, needed during deduction / substitution. | |||
1844 | SmallVector<ParmVarDecl*, 4> Params; | |||
1845 | for (auto T : ParamTypes) { | |||
1846 | ParmVarDecl *NewParam = ParmVarDecl::Create( | |||
1847 | SemaRef.Context, DC, Loc, Loc, nullptr, T, | |||
1848 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); | |||
1849 | NewParam->setScopeInfo(0, Params.size()); | |||
1850 | FPTL.setParam(Params.size(), NewParam); | |||
1851 | Params.push_back(NewParam); | |||
1852 | } | |||
1853 | ||||
1854 | return buildDeductionGuide(Template->getTemplateParameters(), | |||
1855 | ExplicitSpecifier(), TSI, Loc, Loc, Loc); | |||
1856 | } | |||
1857 | ||||
1858 | private: | |||
1859 | /// Transform a constructor template parameter into a deduction guide template | |||
1860 | /// parameter, rebuilding any internal references to earlier parameters and | |||
1861 | /// renumbering as we go. | |||
1862 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, | |||
1863 | MultiLevelTemplateArgumentList &Args) { | |||
1864 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { | |||
1865 | // TemplateTypeParmDecl's index cannot be changed after creation, so | |||
1866 | // substitute it directly. | |||
1867 | auto *NewTTP = TemplateTypeParmDecl::Create( | |||
1868 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), | |||
1869 | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), | |||
1870 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), | |||
1871 | TTP->isParameterPack()); | |||
1872 | if (TTP->hasDefaultArgument()) { | |||
1873 | TypeSourceInfo *InstantiatedDefaultArg = | |||
1874 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, | |||
1875 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); | |||
1876 | if (InstantiatedDefaultArg) | |||
1877 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); | |||
1878 | } | |||
1879 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, | |||
1880 | NewTTP); | |||
1881 | return NewTTP; | |||
1882 | } | |||
1883 | ||||
1884 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) | |||
1885 | return transformTemplateParameterImpl(TTP, Args); | |||
1886 | ||||
1887 | return transformTemplateParameterImpl( | |||
1888 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); | |||
1889 | } | |||
1890 | template<typename TemplateParmDecl> | |||
1891 | TemplateParmDecl * | |||
1892 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, | |||
1893 | MultiLevelTemplateArgumentList &Args) { | |||
1894 | // Ask the template instantiator to do the heavy lifting for us, then adjust | |||
1895 | // the index of the parameter once it's done. | |||
1896 | auto *NewParam = | |||
1897 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); | |||
1898 | assert(NewParam->getDepth() == 0 && "unexpected template param depth")((NewParam->getDepth() == 0 && "unexpected template param depth" ) ? static_cast<void> (0) : __assert_fail ("NewParam->getDepth() == 0 && \"unexpected template param depth\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 1898, __PRETTY_FUNCTION__)); | |||
1899 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); | |||
1900 | return NewParam; | |||
1901 | } | |||
1902 | ||||
1903 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, | |||
1904 | FunctionProtoTypeLoc TL, | |||
1905 | SmallVectorImpl<ParmVarDecl*> &Params, | |||
1906 | MultiLevelTemplateArgumentList &Args) { | |||
1907 | SmallVector<QualType, 4> ParamTypes; | |||
1908 | const FunctionProtoType *T = TL.getTypePtr(); | |||
1909 | ||||
1910 | // -- The types of the function parameters are those of the constructor. | |||
1911 | for (auto *OldParam : TL.getParams()) { | |||
1912 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); | |||
1913 | if (!NewParam) | |||
1914 | return QualType(); | |||
1915 | ParamTypes.push_back(NewParam->getType()); | |||
1916 | Params.push_back(NewParam); | |||
1917 | } | |||
1918 | ||||
1919 | // -- The return type is the class template specialization designated by | |||
1920 | // the template-name and template arguments corresponding to the | |||
1921 | // template parameters obtained from the class template. | |||
1922 | // | |||
1923 | // We use the injected-class-name type of the primary template instead. | |||
1924 | // This has the convenient property that it is different from any type that | |||
1925 | // the user can write in a deduction-guide (because they cannot enter the | |||
1926 | // context of the template), so implicit deduction guides can never collide | |||
1927 | // with explicit ones. | |||
1928 | QualType ReturnType = DeducedType; | |||
1929 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); | |||
1930 | ||||
1931 | // Resolving a wording defect, we also inherit the variadicness of the | |||
1932 | // constructor. | |||
1933 | FunctionProtoType::ExtProtoInfo EPI; | |||
1934 | EPI.Variadic = T->isVariadic(); | |||
1935 | EPI.HasTrailingReturn = true; | |||
1936 | ||||
1937 | QualType Result = SemaRef.BuildFunctionType( | |||
1938 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); | |||
1939 | if (Result.isNull()) | |||
1940 | return QualType(); | |||
1941 | ||||
1942 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); | |||
1943 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); | |||
1944 | NewTL.setLParenLoc(TL.getLParenLoc()); | |||
1945 | NewTL.setRParenLoc(TL.getRParenLoc()); | |||
1946 | NewTL.setExceptionSpecRange(SourceRange()); | |||
1947 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); | |||
1948 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) | |||
1949 | NewTL.setParam(I, Params[I]); | |||
1950 | ||||
1951 | return Result; | |||
1952 | } | |||
1953 | ||||
1954 | ParmVarDecl * | |||
1955 | transformFunctionTypeParam(ParmVarDecl *OldParam, | |||
1956 | MultiLevelTemplateArgumentList &Args) { | |||
1957 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); | |||
1958 | TypeSourceInfo *NewDI; | |||
1959 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { | |||
1960 | // Expand out the one and only element in each inner pack. | |||
1961 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); | |||
1962 | NewDI = | |||
1963 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, | |||
1964 | OldParam->getLocation(), OldParam->getDeclName()); | |||
1965 | if (!NewDI) return nullptr; | |||
1966 | NewDI = | |||
1967 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), | |||
1968 | PackTL.getTypePtr()->getNumExpansions()); | |||
1969 | } else | |||
1970 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), | |||
1971 | OldParam->getDeclName()); | |||
1972 | if (!NewDI) | |||
1973 | return nullptr; | |||
1974 | ||||
1975 | // Extract the type. This (for instance) replaces references to typedef | |||
1976 | // members of the current instantiations with the definitions of those | |||
1977 | // typedefs, avoiding triggering instantiation of the deduced type during | |||
1978 | // deduction. | |||
1979 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); | |||
1980 | ||||
1981 | // Resolving a wording defect, we also inherit default arguments from the | |||
1982 | // constructor. | |||
1983 | ExprResult NewDefArg; | |||
1984 | if (OldParam->hasDefaultArg()) { | |||
1985 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); | |||
1986 | if (NewDefArg.isInvalid()) | |||
1987 | return nullptr; | |||
1988 | } | |||
1989 | ||||
1990 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, | |||
1991 | OldParam->getInnerLocStart(), | |||
1992 | OldParam->getLocation(), | |||
1993 | OldParam->getIdentifier(), | |||
1994 | NewDI->getType(), | |||
1995 | NewDI, | |||
1996 | OldParam->getStorageClass(), | |||
1997 | NewDefArg.get()); | |||
1998 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), | |||
1999 | OldParam->getFunctionScopeIndex()); | |||
2000 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); | |||
2001 | return NewParam; | |||
2002 | } | |||
2003 | ||||
2004 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, | |||
2005 | ExplicitSpecifier ES, TypeSourceInfo *TInfo, | |||
2006 | SourceLocation LocStart, SourceLocation Loc, | |||
2007 | SourceLocation LocEnd) { | |||
2008 | DeclarationNameInfo Name(DeductionGuideName, Loc); | |||
2009 | ArrayRef<ParmVarDecl *> Params = | |||
2010 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); | |||
2011 | ||||
2012 | // Build the implicit deduction guide template. | |||
2013 | auto *Guide = | |||
2014 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, ES, Name, | |||
2015 | TInfo->getType(), TInfo, LocEnd); | |||
2016 | Guide->setImplicit(); | |||
2017 | Guide->setParams(Params); | |||
2018 | ||||
2019 | for (auto *Param : Params) | |||
2020 | Param->setDeclContext(Guide); | |||
2021 | ||||
2022 | auto *GuideTemplate = FunctionTemplateDecl::Create( | |||
2023 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); | |||
2024 | GuideTemplate->setImplicit(); | |||
2025 | Guide->setDescribedFunctionTemplate(GuideTemplate); | |||
2026 | ||||
2027 | if (isa<CXXRecordDecl>(DC)) { | |||
2028 | Guide->setAccess(AS_public); | |||
2029 | GuideTemplate->setAccess(AS_public); | |||
2030 | } | |||
2031 | ||||
2032 | DC->addDecl(GuideTemplate); | |||
2033 | return GuideTemplate; | |||
2034 | } | |||
2035 | }; | |||
2036 | } | |||
2037 | ||||
2038 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, | |||
2039 | SourceLocation Loc) { | |||
2040 | if (CXXRecordDecl *DefRecord = | |||
2041 | cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) { | |||
2042 | TemplateDecl *DescribedTemplate = DefRecord->getDescribedClassTemplate(); | |||
2043 | Template = DescribedTemplate ? DescribedTemplate : Template; | |||
2044 | } | |||
2045 | ||||
2046 | DeclContext *DC = Template->getDeclContext(); | |||
2047 | if (DC->isDependentContext()) | |||
2048 | return; | |||
2049 | ||||
2050 | ConvertConstructorToDeductionGuideTransform Transform( | |||
2051 | *this, cast<ClassTemplateDecl>(Template)); | |||
2052 | if (!isCompleteType(Loc, Transform.DeducedType)) | |||
2053 | return; | |||
2054 | ||||
2055 | // Check whether we've already declared deduction guides for this template. | |||
2056 | // FIXME: Consider storing a flag on the template to indicate this. | |||
2057 | auto Existing = DC->lookup(Transform.DeductionGuideName); | |||
2058 | for (auto *D : Existing) | |||
2059 | if (D->isImplicit()) | |||
2060 | return; | |||
2061 | ||||
2062 | // In case we were expanding a pack when we attempted to declare deduction | |||
2063 | // guides, turn off pack expansion for everything we're about to do. | |||
2064 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); | |||
2065 | // Create a template instantiation record to track the "instantiation" of | |||
2066 | // constructors into deduction guides. | |||
2067 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can | |||
2068 | // this substitution process actually fail? | |||
2069 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); | |||
2070 | if (BuildingDeductionGuides.isInvalid()) | |||
2071 | return; | |||
2072 | ||||
2073 | // Convert declared constructors into deduction guide templates. | |||
2074 | // FIXME: Skip constructors for which deduction must necessarily fail (those | |||
2075 | // for which some class template parameter without a default argument never | |||
2076 | // appears in a deduced context). | |||
2077 | bool AddedAny = false; | |||
2078 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { | |||
2079 | D = D->getUnderlyingDecl(); | |||
2080 | if (D->isInvalidDecl() || D->isImplicit()) | |||
2081 | continue; | |||
2082 | D = cast<NamedDecl>(D->getCanonicalDecl()); | |||
2083 | ||||
2084 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); | |||
2085 | auto *CD = | |||
2086 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); | |||
2087 | // Class-scope explicit specializations (MS extension) do not result in | |||
2088 | // deduction guides. | |||
2089 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) | |||
2090 | continue; | |||
2091 | ||||
2092 | Transform.transformConstructor(FTD, CD); | |||
2093 | AddedAny = true; | |||
2094 | } | |||
2095 | ||||
2096 | // C++17 [over.match.class.deduct] | |||
2097 | // -- If C is not defined or does not declare any constructors, an | |||
2098 | // additional function template derived as above from a hypothetical | |||
2099 | // constructor C(). | |||
2100 | if (!AddedAny) | |||
2101 | Transform.buildSimpleDeductionGuide(None); | |||
2102 | ||||
2103 | // -- An additional function template derived as above from a hypothetical | |||
2104 | // constructor C(C), called the copy deduction candidate. | |||
2105 | cast<CXXDeductionGuideDecl>( | |||
2106 | cast<FunctionTemplateDecl>( | |||
2107 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) | |||
2108 | ->getTemplatedDecl()) | |||
2109 | ->setIsCopyDeductionCandidate(); | |||
2110 | } | |||
2111 | ||||
2112 | /// Diagnose the presence of a default template argument on a | |||
2113 | /// template parameter, which is ill-formed in certain contexts. | |||
2114 | /// | |||
2115 | /// \returns true if the default template argument should be dropped. | |||
2116 | static bool DiagnoseDefaultTemplateArgument(Sema &S, | |||
2117 | Sema::TemplateParamListContext TPC, | |||
2118 | SourceLocation ParamLoc, | |||
2119 | SourceRange DefArgRange) { | |||
2120 | switch (TPC) { | |||
2121 | case Sema::TPC_ClassTemplate: | |||
2122 | case Sema::TPC_VarTemplate: | |||
2123 | case Sema::TPC_TypeAliasTemplate: | |||
2124 | return false; | |||
2125 | ||||
2126 | case Sema::TPC_FunctionTemplate: | |||
2127 | case Sema::TPC_FriendFunctionTemplateDefinition: | |||
2128 | // C++ [temp.param]p9: | |||
2129 | // A default template-argument shall not be specified in a | |||
2130 | // function template declaration or a function template | |||
2131 | // definition [...] | |||
2132 | // If a friend function template declaration specifies a default | |||
2133 | // template-argument, that declaration shall be a definition and shall be | |||
2134 | // the only declaration of the function template in the translation unit. | |||
2135 | // (C++98/03 doesn't have this wording; see DR226). | |||
2136 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? | |||
2137 | diag::warn_cxx98_compat_template_parameter_default_in_function_template | |||
2138 | : diag::ext_template_parameter_default_in_function_template) | |||
2139 | << DefArgRange; | |||
2140 | return false; | |||
2141 | ||||
2142 | case Sema::TPC_ClassTemplateMember: | |||
2143 | // C++0x [temp.param]p9: | |||
2144 | // A default template-argument shall not be specified in the | |||
2145 | // template-parameter-lists of the definition of a member of a | |||
2146 | // class template that appears outside of the member's class. | |||
2147 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) | |||
2148 | << DefArgRange; | |||
2149 | return true; | |||
2150 | ||||
2151 | case Sema::TPC_FriendClassTemplate: | |||
2152 | case Sema::TPC_FriendFunctionTemplate: | |||
2153 | // C++ [temp.param]p9: | |||
2154 | // A default template-argument shall not be specified in a | |||
2155 | // friend template declaration. | |||
2156 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) | |||
2157 | << DefArgRange; | |||
2158 | return true; | |||
2159 | ||||
2160 | // FIXME: C++0x [temp.param]p9 allows default template-arguments | |||
2161 | // for friend function templates if there is only a single | |||
2162 | // declaration (and it is a definition). Strange! | |||
2163 | } | |||
2164 | ||||
2165 | llvm_unreachable("Invalid TemplateParamListContext!")::llvm::llvm_unreachable_internal("Invalid TemplateParamListContext!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2165); | |||
2166 | } | |||
2167 | ||||
2168 | /// Check for unexpanded parameter packs within the template parameters | |||
2169 | /// of a template template parameter, recursively. | |||
2170 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, | |||
2171 | TemplateTemplateParmDecl *TTP) { | |||
2172 | // A template template parameter which is a parameter pack is also a pack | |||
2173 | // expansion. | |||
2174 | if (TTP->isParameterPack()) | |||
2175 | return false; | |||
2176 | ||||
2177 | TemplateParameterList *Params = TTP->getTemplateParameters(); | |||
2178 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | |||
2179 | NamedDecl *P = Params->getParam(I); | |||
2180 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { | |||
2181 | if (!NTTP->isParameterPack() && | |||
2182 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), | |||
2183 | NTTP->getTypeSourceInfo(), | |||
2184 | Sema::UPPC_NonTypeTemplateParameterType)) | |||
2185 | return true; | |||
2186 | ||||
2187 | continue; | |||
2188 | } | |||
2189 | ||||
2190 | if (TemplateTemplateParmDecl *InnerTTP | |||
2191 | = dyn_cast<TemplateTemplateParmDecl>(P)) | |||
2192 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) | |||
2193 | return true; | |||
2194 | } | |||
2195 | ||||
2196 | return false; | |||
2197 | } | |||
2198 | ||||
2199 | /// Checks the validity of a template parameter list, possibly | |||
2200 | /// considering the template parameter list from a previous | |||
2201 | /// declaration. | |||
2202 | /// | |||
2203 | /// If an "old" template parameter list is provided, it must be | |||
2204 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" | |||
2205 | /// template parameter list. | |||
2206 | /// | |||
2207 | /// \param NewParams Template parameter list for a new template | |||
2208 | /// declaration. This template parameter list will be updated with any | |||
2209 | /// default arguments that are carried through from the previous | |||
2210 | /// template parameter list. | |||
2211 | /// | |||
2212 | /// \param OldParams If provided, template parameter list from a | |||
2213 | /// previous declaration of the same template. Default template | |||
2214 | /// arguments will be merged from the old template parameter list to | |||
2215 | /// the new template parameter list. | |||
2216 | /// | |||
2217 | /// \param TPC Describes the context in which we are checking the given | |||
2218 | /// template parameter list. | |||
2219 | /// | |||
2220 | /// \param SkipBody If we might have already made a prior merged definition | |||
2221 | /// of this template visible, the corresponding body-skipping information. | |||
2222 | /// Default argument redefinition is not an error when skipping such a body, | |||
2223 | /// because (under the ODR) we can assume the default arguments are the same | |||
2224 | /// as the prior merged definition. | |||
2225 | /// | |||
2226 | /// \returns true if an error occurred, false otherwise. | |||
2227 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, | |||
2228 | TemplateParameterList *OldParams, | |||
2229 | TemplateParamListContext TPC, | |||
2230 | SkipBodyInfo *SkipBody) { | |||
2231 | bool Invalid = false; | |||
2232 | ||||
2233 | // C++ [temp.param]p10: | |||
2234 | // The set of default template-arguments available for use with a | |||
2235 | // template declaration or definition is obtained by merging the | |||
2236 | // default arguments from the definition (if in scope) and all | |||
2237 | // declarations in scope in the same way default function | |||
2238 | // arguments are (8.3.6). | |||
2239 | bool SawDefaultArgument = false; | |||
2240 | SourceLocation PreviousDefaultArgLoc; | |||
2241 | ||||
2242 | // Dummy initialization to avoid warnings. | |||
2243 | TemplateParameterList::iterator OldParam = NewParams->end(); | |||
2244 | if (OldParams) | |||
2245 | OldParam = OldParams->begin(); | |||
2246 | ||||
2247 | bool RemoveDefaultArguments = false; | |||
2248 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), | |||
2249 | NewParamEnd = NewParams->end(); | |||
2250 | NewParam != NewParamEnd; ++NewParam) { | |||
2251 | // Variables used to diagnose redundant default arguments | |||
2252 | bool RedundantDefaultArg = false; | |||
2253 | SourceLocation OldDefaultLoc; | |||
2254 | SourceLocation NewDefaultLoc; | |||
2255 | ||||
2256 | // Variable used to diagnose missing default arguments | |||
2257 | bool MissingDefaultArg = false; | |||
2258 | ||||
2259 | // Variable used to diagnose non-final parameter packs | |||
2260 | bool SawParameterPack = false; | |||
2261 | ||||
2262 | if (TemplateTypeParmDecl *NewTypeParm | |||
2263 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { | |||
2264 | // Check the presence of a default argument here. | |||
2265 | if (NewTypeParm->hasDefaultArgument() && | |||
2266 | DiagnoseDefaultTemplateArgument(*this, TPC, | |||
2267 | NewTypeParm->getLocation(), | |||
2268 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() | |||
2269 | .getSourceRange())) | |||
2270 | NewTypeParm->removeDefaultArgument(); | |||
2271 | ||||
2272 | // Merge default arguments for template type parameters. | |||
2273 | TemplateTypeParmDecl *OldTypeParm | |||
2274 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; | |||
2275 | if (NewTypeParm->isParameterPack()) { | |||
2276 | assert(!NewTypeParm->hasDefaultArgument() &&((!NewTypeParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2277, __PRETTY_FUNCTION__)) | |||
2277 | "Parameter packs can't have a default argument!")((!NewTypeParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2277, __PRETTY_FUNCTION__)); | |||
2278 | SawParameterPack = true; | |||
2279 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && | |||
2280 | NewTypeParm->hasDefaultArgument() && | |||
2281 | (!SkipBody || !SkipBody->ShouldSkip)) { | |||
2282 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); | |||
2283 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); | |||
2284 | SawDefaultArgument = true; | |||
2285 | RedundantDefaultArg = true; | |||
2286 | PreviousDefaultArgLoc = NewDefaultLoc; | |||
2287 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { | |||
2288 | // Merge the default argument from the old declaration to the | |||
2289 | // new declaration. | |||
2290 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); | |||
2291 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); | |||
2292 | } else if (NewTypeParm->hasDefaultArgument()) { | |||
2293 | SawDefaultArgument = true; | |||
2294 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); | |||
2295 | } else if (SawDefaultArgument) | |||
2296 | MissingDefaultArg = true; | |||
2297 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm | |||
2298 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { | |||
2299 | // Check for unexpanded parameter packs. | |||
2300 | if (!NewNonTypeParm->isParameterPack() && | |||
2301 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), | |||
2302 | NewNonTypeParm->getTypeSourceInfo(), | |||
2303 | UPPC_NonTypeTemplateParameterType)) { | |||
2304 | Invalid = true; | |||
2305 | continue; | |||
2306 | } | |||
2307 | ||||
2308 | // Check the presence of a default argument here. | |||
2309 | if (NewNonTypeParm->hasDefaultArgument() && | |||
2310 | DiagnoseDefaultTemplateArgument(*this, TPC, | |||
2311 | NewNonTypeParm->getLocation(), | |||
2312 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { | |||
2313 | NewNonTypeParm->removeDefaultArgument(); | |||
2314 | } | |||
2315 | ||||
2316 | // Merge default arguments for non-type template parameters | |||
2317 | NonTypeTemplateParmDecl *OldNonTypeParm | |||
2318 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; | |||
2319 | if (NewNonTypeParm->isParameterPack()) { | |||
2320 | assert(!NewNonTypeParm->hasDefaultArgument() &&((!NewNonTypeParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewNonTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2321, __PRETTY_FUNCTION__)) | |||
2321 | "Parameter packs can't have a default argument!")((!NewNonTypeParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewNonTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2321, __PRETTY_FUNCTION__)); | |||
2322 | if (!NewNonTypeParm->isPackExpansion()) | |||
2323 | SawParameterPack = true; | |||
2324 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && | |||
2325 | NewNonTypeParm->hasDefaultArgument() && | |||
2326 | (!SkipBody || !SkipBody->ShouldSkip)) { | |||
2327 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); | |||
2328 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); | |||
2329 | SawDefaultArgument = true; | |||
2330 | RedundantDefaultArg = true; | |||
2331 | PreviousDefaultArgLoc = NewDefaultLoc; | |||
2332 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { | |||
2333 | // Merge the default argument from the old declaration to the | |||
2334 | // new declaration. | |||
2335 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); | |||
2336 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); | |||
2337 | } else if (NewNonTypeParm->hasDefaultArgument()) { | |||
2338 | SawDefaultArgument = true; | |||
2339 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); | |||
2340 | } else if (SawDefaultArgument) | |||
2341 | MissingDefaultArg = true; | |||
2342 | } else { | |||
2343 | TemplateTemplateParmDecl *NewTemplateParm | |||
2344 | = cast<TemplateTemplateParmDecl>(*NewParam); | |||
2345 | ||||
2346 | // Check for unexpanded parameter packs, recursively. | |||
2347 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { | |||
2348 | Invalid = true; | |||
2349 | continue; | |||
2350 | } | |||
2351 | ||||
2352 | // Check the presence of a default argument here. | |||
2353 | if (NewTemplateParm->hasDefaultArgument() && | |||
2354 | DiagnoseDefaultTemplateArgument(*this, TPC, | |||
2355 | NewTemplateParm->getLocation(), | |||
2356 | NewTemplateParm->getDefaultArgument().getSourceRange())) | |||
2357 | NewTemplateParm->removeDefaultArgument(); | |||
2358 | ||||
2359 | // Merge default arguments for template template parameters | |||
2360 | TemplateTemplateParmDecl *OldTemplateParm | |||
2361 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; | |||
2362 | if (NewTemplateParm->isParameterPack()) { | |||
2363 | assert(!NewTemplateParm->hasDefaultArgument() &&((!NewTemplateParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewTemplateParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2364, __PRETTY_FUNCTION__)) | |||
2364 | "Parameter packs can't have a default argument!")((!NewTemplateParm->hasDefaultArgument() && "Parameter packs can't have a default argument!" ) ? static_cast<void> (0) : __assert_fail ("!NewTemplateParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 2364, __PRETTY_FUNCTION__)); | |||
2365 | if (!NewTemplateParm->isPackExpansion()) | |||
2366 | SawParameterPack = true; | |||
2367 | } else if (OldTemplateParm && | |||
2368 | hasVisibleDefaultArgument(OldTemplateParm) && | |||
2369 | NewTemplateParm->hasDefaultArgument() && | |||
2370 | (!SkipBody || !SkipBody->ShouldSkip)) { | |||
2371 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); | |||
2372 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); | |||
2373 | SawDefaultArgument = true; | |||
2374 | RedundantDefaultArg = true; | |||
2375 | PreviousDefaultArgLoc = NewDefaultLoc; | |||
2376 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { | |||
2377 | // Merge the default argument from the old declaration to the | |||
2378 | // new declaration. | |||
2379 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); | |||
2380 | PreviousDefaultArgLoc | |||
2381 | = OldTemplateParm->getDefaultArgument().getLocation(); | |||
2382 | } else if (NewTemplateParm->hasDefaultArgument()) { | |||
2383 | SawDefaultArgument = true; | |||
2384 | PreviousDefaultArgLoc | |||
2385 | = NewTemplateParm->getDefaultArgument().getLocation(); | |||
2386 | } else if (SawDefaultArgument) | |||
2387 | MissingDefaultArg = true; | |||
2388 | } | |||
2389 | ||||
2390 | // C++11 [temp.param]p11: | |||
2391 | // If a template parameter of a primary class template or alias template | |||
2392 | // is a template parameter pack, it shall be the last template parameter. | |||
2393 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && | |||
2394 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || | |||
2395 | TPC == TPC_TypeAliasTemplate)) { | |||
2396 | Diag((*NewParam)->getLocation(), | |||
2397 | diag::err_template_param_pack_must_be_last_template_parameter); | |||
2398 | Invalid = true; | |||
2399 | } | |||
2400 | ||||
2401 | if (RedundantDefaultArg) { | |||
2402 | // C++ [temp.param]p12: | |||
2403 | // A template-parameter shall not be given default arguments | |||
2404 | // by two different declarations in the same scope. | |||
2405 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); | |||
2406 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); | |||
2407 | Invalid = true; | |||
2408 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { | |||
2409 | // C++ [temp.param]p11: | |||
2410 | // If a template-parameter of a class template has a default | |||
2411 | // template-argument, each subsequent template-parameter shall either | |||
2412 | // have a default template-argument supplied or be a template parameter | |||
2413 | // pack. | |||
2414 | Diag((*NewParam)->getLocation(), | |||
2415 | diag::err_template_param_default_arg_missing); | |||
2416 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); | |||
2417 | Invalid = true; | |||
2418 | RemoveDefaultArguments = true; | |||
2419 | } | |||
2420 | ||||
2421 | // If we have an old template parameter list that we're merging | |||
2422 | // in, move on to the next parameter. | |||
2423 | if (OldParams) | |||
2424 | ++OldParam; | |||
2425 | } | |||
2426 | ||||
2427 | // We were missing some default arguments at the end of the list, so remove | |||
2428 | // all of the default arguments. | |||
2429 | if (RemoveDefaultArguments) { | |||
2430 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), | |||
2431 | NewParamEnd = NewParams->end(); | |||
2432 | NewParam != NewParamEnd; ++NewParam) { | |||
2433 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) | |||
2434 | TTP->removeDefaultArgument(); | |||
2435 | else if (NonTypeTemplateParmDecl *NTTP | |||
2436 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) | |||
2437 | NTTP->removeDefaultArgument(); | |||
2438 | else | |||
2439 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); | |||
2440 | } | |||
2441 | } | |||
2442 | ||||
2443 | return Invalid; | |||
2444 | } | |||
2445 | ||||
2446 | namespace { | |||
2447 | ||||
2448 | /// A class which looks for a use of a certain level of template | |||
2449 | /// parameter. | |||
2450 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { | |||
2451 | typedef RecursiveASTVisitor<DependencyChecker> super; | |||
2452 | ||||
2453 | unsigned Depth; | |||
2454 | ||||
2455 | // Whether we're looking for a use of a template parameter that makes the | |||
2456 | // overall construct type-dependent / a dependent type. This is strictly | |||
2457 | // best-effort for now; we may fail to match at all for a dependent type | |||
2458 | // in some cases if this is set. | |||
2459 | bool IgnoreNonTypeDependent; | |||
2460 | ||||
2461 | bool Match; | |||
2462 | SourceLocation MatchLoc; | |||
2463 | ||||
2464 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) | |||
2465 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), | |||
2466 | Match(false) {} | |||
2467 | ||||
2468 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) | |||
2469 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { | |||
2470 | NamedDecl *ND = Params->getParam(0); | |||
2471 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { | |||
2472 | Depth = PD->getDepth(); | |||
2473 | } else if (NonTypeTemplateParmDecl *PD = | |||
2474 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { | |||
2475 | Depth = PD->getDepth(); | |||
2476 | } else { | |||
2477 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); | |||
2478 | } | |||
2479 | } | |||
2480 | ||||
2481 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { | |||
2482 | if (ParmDepth >= Depth) { | |||
2483 | Match = true; | |||
2484 | MatchLoc = Loc; | |||
2485 | return true; | |||
2486 | } | |||
2487 | return false; | |||
2488 | } | |||
2489 | ||||
2490 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { | |||
2491 | // Prune out non-type-dependent expressions if requested. This can | |||
2492 | // sometimes result in us failing to find a template parameter reference | |||
2493 | // (if a value-dependent expression creates a dependent type), but this | |||
2494 | // mode is best-effort only. | |||
2495 | if (auto *E = dyn_cast_or_null<Expr>(S)) | |||
2496 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) | |||
2497 | return true; | |||
2498 | return super::TraverseStmt(S, Q); | |||
2499 | } | |||
2500 | ||||
2501 | bool TraverseTypeLoc(TypeLoc TL) { | |||
2502 | if (IgnoreNonTypeDependent && !TL.isNull() && | |||
2503 | !TL.getType()->isDependentType()) | |||
2504 | return true; | |||
2505 | return super::TraverseTypeLoc(TL); | |||
2506 | } | |||
2507 | ||||
2508 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { | |||
2509 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); | |||
2510 | } | |||
2511 | ||||
2512 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { | |||
2513 | // For a best-effort search, keep looking until we find a location. | |||
2514 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); | |||
2515 | } | |||
2516 | ||||
2517 | bool TraverseTemplateName(TemplateName N) { | |||
2518 | if (TemplateTemplateParmDecl *PD = | |||
2519 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) | |||
2520 | if (Matches(PD->getDepth())) | |||
2521 | return false; | |||
2522 | return super::TraverseTemplateName(N); | |||
2523 | } | |||
2524 | ||||
2525 | bool VisitDeclRefExpr(DeclRefExpr *E) { | |||
2526 | if (NonTypeTemplateParmDecl *PD = | |||
2527 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) | |||
2528 | if (Matches(PD->getDepth(), E->getExprLoc())) | |||
2529 | return false; | |||
2530 | return super::VisitDeclRefExpr(E); | |||
2531 | } | |||
2532 | ||||
2533 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { | |||
2534 | return TraverseType(T->getReplacementType()); | |||
2535 | } | |||
2536 | ||||
2537 | bool | |||
2538 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { | |||
2539 | return TraverseTemplateArgument(T->getArgumentPack()); | |||
2540 | } | |||
2541 | ||||
2542 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { | |||
2543 | return TraverseType(T->getInjectedSpecializationType()); | |||
2544 | } | |||
2545 | }; | |||
2546 | } // end anonymous namespace | |||
2547 | ||||
2548 | /// Determines whether a given type depends on the given parameter | |||
2549 | /// list. | |||
2550 | static bool | |||
2551 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { | |||
2552 | if (!Params->size()) | |||
2553 | return false; | |||
2554 | ||||
2555 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); | |||
2556 | Checker.TraverseType(T); | |||
2557 | return Checker.Match; | |||
2558 | } | |||
2559 | ||||
2560 | // Find the source range corresponding to the named type in the given | |||
2561 | // nested-name-specifier, if any. | |||
2562 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, | |||
2563 | QualType T, | |||
2564 | const CXXScopeSpec &SS) { | |||
2565 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); | |||
2566 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { | |||
2567 | if (const Type *CurType = NNS->getAsType()) { | |||
2568 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) | |||
2569 | return NNSLoc.getTypeLoc().getSourceRange(); | |||
2570 | } else | |||
2571 | break; | |||
2572 | ||||
2573 | NNSLoc = NNSLoc.getPrefix(); | |||
2574 | } | |||
2575 | ||||
2576 | return SourceRange(); | |||
2577 | } | |||
2578 | ||||
2579 | /// Match the given template parameter lists to the given scope | |||
2580 | /// specifier, returning the template parameter list that applies to the | |||
2581 | /// name. | |||
2582 | /// | |||
2583 | /// \param DeclStartLoc the start of the declaration that has a scope | |||
2584 | /// specifier or a template parameter list. | |||
2585 | /// | |||
2586 | /// \param DeclLoc The location of the declaration itself. | |||
2587 | /// | |||
2588 | /// \param SS the scope specifier that will be matched to the given template | |||
2589 | /// parameter lists. This scope specifier precedes a qualified name that is | |||
2590 | /// being declared. | |||
2591 | /// | |||
2592 | /// \param TemplateId The template-id following the scope specifier, if there | |||
2593 | /// is one. Used to check for a missing 'template<>'. | |||
2594 | /// | |||
2595 | /// \param ParamLists the template parameter lists, from the outermost to the | |||
2596 | /// innermost template parameter lists. | |||
2597 | /// | |||
2598 | /// \param IsFriend Whether to apply the slightly different rules for | |||
2599 | /// matching template parameters to scope specifiers in friend | |||
2600 | /// declarations. | |||
2601 | /// | |||
2602 | /// \param IsMemberSpecialization will be set true if the scope specifier | |||
2603 | /// denotes a fully-specialized type, and therefore this is a declaration of | |||
2604 | /// a member specialization. | |||
2605 | /// | |||
2606 | /// \returns the template parameter list, if any, that corresponds to the | |||
2607 | /// name that is preceded by the scope specifier @p SS. This template | |||
2608 | /// parameter list may have template parameters (if we're declaring a | |||
2609 | /// template) or may have no template parameters (if we're declaring a | |||
2610 | /// template specialization), or may be NULL (if what we're declaring isn't | |||
2611 | /// itself a template). | |||
2612 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( | |||
2613 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, | |||
2614 | TemplateIdAnnotation *TemplateId, | |||
2615 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, | |||
2616 | bool &IsMemberSpecialization, bool &Invalid) { | |||
2617 | IsMemberSpecialization = false; | |||
2618 | Invalid = false; | |||
2619 | ||||
2620 | // The sequence of nested types to which we will match up the template | |||
2621 | // parameter lists. We first build this list by starting with the type named | |||
2622 | // by the nested-name-specifier and walking out until we run out of types. | |||
2623 | SmallVector<QualType, 4> NestedTypes; | |||
2624 | QualType T; | |||
2625 | if (SS.getScopeRep()) { | |||
2626 | if (CXXRecordDecl *Record | |||
2627 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) | |||
2628 | T = Context.getTypeDeclType(Record); | |||
2629 | else | |||
2630 | T = QualType(SS.getScopeRep()->getAsType(), 0); | |||
2631 | } | |||
2632 | ||||
2633 | // If we found an explicit specialization that prevents us from needing | |||
2634 | // 'template<>' headers, this will be set to the location of that | |||
2635 | // explicit specialization. | |||
2636 | SourceLocation ExplicitSpecLoc; | |||
2637 | ||||
2638 | while (!T.isNull()) { | |||
2639 | NestedTypes.push_back(T); | |||
2640 | ||||
2641 | // Retrieve the parent of a record type. | |||
2642 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { | |||
2643 | // If this type is an explicit specialization, we're done. | |||
2644 | if (ClassTemplateSpecializationDecl *Spec | |||
2645 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { | |||
2646 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && | |||
2647 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { | |||
2648 | ExplicitSpecLoc = Spec->getLocation(); | |||
2649 | break; | |||
2650 | } | |||
2651 | } else if (Record->getTemplateSpecializationKind() | |||
2652 | == TSK_ExplicitSpecialization) { | |||
2653 | ExplicitSpecLoc = Record->getLocation(); | |||
2654 | break; | |||
2655 | } | |||
2656 | ||||
2657 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) | |||
2658 | T = Context.getTypeDeclType(Parent); | |||
2659 | else | |||
2660 | T = QualType(); | |||
2661 | continue; | |||
2662 | } | |||
2663 | ||||
2664 | if (const TemplateSpecializationType *TST | |||
2665 | = T->getAs<TemplateSpecializationType>()) { | |||
2666 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { | |||
2667 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) | |||
2668 | T = Context.getTypeDeclType(Parent); | |||
2669 | else | |||
2670 | T = QualType(); | |||
2671 | continue; | |||
2672 | } | |||
2673 | } | |||
2674 | ||||
2675 | // Look one step prior in a dependent template specialization type. | |||
2676 | if (const DependentTemplateSpecializationType *DependentTST | |||
2677 | = T->getAs<DependentTemplateSpecializationType>()) { | |||
2678 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) | |||
2679 | T = QualType(NNS->getAsType(), 0); | |||
2680 | else | |||
2681 | T = QualType(); | |||
2682 | continue; | |||
2683 | } | |||
2684 | ||||
2685 | // Look one step prior in a dependent name type. | |||
2686 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ | |||
2687 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) | |||
2688 | T = QualType(NNS->getAsType(), 0); | |||
2689 | else | |||
2690 | T = QualType(); | |||
2691 | continue; | |||
2692 | } | |||
2693 | ||||
2694 | // Retrieve the parent of an enumeration type. | |||
2695 | if (const EnumType *EnumT = T->getAs<EnumType>()) { | |||
2696 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization | |||
2697 | // check here. | |||
2698 | EnumDecl *Enum = EnumT->getDecl(); | |||
2699 | ||||
2700 | // Get to the parent type. | |||
2701 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) | |||
2702 | T = Context.getTypeDeclType(Parent); | |||
2703 | else | |||
2704 | T = QualType(); | |||
2705 | continue; | |||
2706 | } | |||
2707 | ||||
2708 | T = QualType(); | |||
2709 | } | |||
2710 | // Reverse the nested types list, since we want to traverse from the outermost | |||
2711 | // to the innermost while checking template-parameter-lists. | |||
2712 | std::reverse(NestedTypes.begin(), NestedTypes.end()); | |||
2713 | ||||
2714 | // C++0x [temp.expl.spec]p17: | |||
2715 | // A member or a member template may be nested within many | |||
2716 | // enclosing class templates. In an explicit specialization for | |||
2717 | // such a member, the member declaration shall be preceded by a | |||
2718 | // template<> for each enclosing class template that is | |||
2719 | // explicitly specialized. | |||
2720 | bool SawNonEmptyTemplateParameterList = false; | |||
2721 | ||||
2722 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { | |||
2723 | if (SawNonEmptyTemplateParameterList) { | |||
2724 | Diag(DeclLoc, diag::err_specialize_member_of_template) | |||
2725 | << !Recovery << Range; | |||
2726 | Invalid = true; | |||
2727 | IsMemberSpecialization = false; | |||
2728 | return true; | |||
2729 | } | |||
2730 | ||||
2731 | return false; | |||
2732 | }; | |||
2733 | ||||
2734 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { | |||
2735 | // Check that we can have an explicit specialization here. | |||
2736 | if (CheckExplicitSpecialization(Range, true)) | |||
2737 | return true; | |||
2738 | ||||
2739 | // We don't have a template header, but we should. | |||
2740 | SourceLocation ExpectedTemplateLoc; | |||
2741 | if (!ParamLists.empty()) | |||
2742 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); | |||
2743 | else | |||
2744 | ExpectedTemplateLoc = DeclStartLoc; | |||
2745 | ||||
2746 | Diag(DeclLoc, diag::err_template_spec_needs_header) | |||
2747 | << Range | |||
2748 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); | |||
2749 | return false; | |||
2750 | }; | |||
2751 | ||||
2752 | unsigned ParamIdx = 0; | |||
2753 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; | |||
2754 | ++TypeIdx) { | |||
2755 | T = NestedTypes[TypeIdx]; | |||
2756 | ||||
2757 | // Whether we expect a 'template<>' header. | |||
2758 | bool NeedEmptyTemplateHeader = false; | |||
2759 | ||||
2760 | // Whether we expect a template header with parameters. | |||
2761 | bool NeedNonemptyTemplateHeader = false; | |||
2762 | ||||
2763 | // For a dependent type, the set of template parameters that we | |||
2764 | // expect to see. | |||
2765 | TemplateParameterList *ExpectedTemplateParams = nullptr; | |||
2766 | ||||
2767 | // C++0x [temp.expl.spec]p15: | |||
2768 | // A member or a member template may be nested within many enclosing | |||
2769 | // class templates. In an explicit specialization for such a member, the | |||
2770 | // member declaration shall be preceded by a template<> for each | |||
2771 | // enclosing class template that is explicitly specialized. | |||
2772 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { | |||
2773 | if (ClassTemplatePartialSpecializationDecl *Partial | |||
2774 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { | |||
2775 | ExpectedTemplateParams = Partial->getTemplateParameters(); | |||
2776 | NeedNonemptyTemplateHeader = true; | |||
2777 | } else if (Record->isDependentType()) { | |||
2778 | if (Record->getDescribedClassTemplate()) { | |||
2779 | ExpectedTemplateParams = Record->getDescribedClassTemplate() | |||
2780 | ->getTemplateParameters(); | |||
2781 | NeedNonemptyTemplateHeader = true; | |||
2782 | } | |||
2783 | } else if (ClassTemplateSpecializationDecl *Spec | |||
2784 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { | |||
2785 | // C++0x [temp.expl.spec]p4: | |||
2786 | // Members of an explicitly specialized class template are defined | |||
2787 | // in the same manner as members of normal classes, and not using | |||
2788 | // the template<> syntax. | |||
2789 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) | |||
2790 | NeedEmptyTemplateHeader = true; | |||
2791 | else | |||
2792 | continue; | |||
2793 | } else if (Record->getTemplateSpecializationKind()) { | |||
2794 | if (Record->getTemplateSpecializationKind() | |||
2795 | != TSK_ExplicitSpecialization && | |||
2796 | TypeIdx == NumTypes - 1) | |||
2797 | IsMemberSpecialization = true; | |||
2798 | ||||
2799 | continue; | |||
2800 | } | |||
2801 | } else if (const TemplateSpecializationType *TST | |||
2802 | = T->getAs<TemplateSpecializationType>()) { | |||
2803 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { | |||
2804 | ExpectedTemplateParams = Template->getTemplateParameters(); | |||
2805 | NeedNonemptyTemplateHeader = true; | |||
2806 | } | |||
2807 | } else if (T->getAs<DependentTemplateSpecializationType>()) { | |||
2808 | // FIXME: We actually could/should check the template arguments here | |||
2809 | // against the corresponding template parameter list. | |||
2810 | NeedNonemptyTemplateHeader = false; | |||
2811 | } | |||
2812 | ||||
2813 | // C++ [temp.expl.spec]p16: | |||
2814 | // In an explicit specialization declaration for a member of a class | |||
2815 | // template or a member template that ap- pears in namespace scope, the | |||
2816 | // member template and some of its enclosing class templates may remain | |||
2817 | // unspecialized, except that the declaration shall not explicitly | |||
2818 | // specialize a class member template if its en- closing class templates | |||
2819 | // are not explicitly specialized as well. | |||
2820 | if (ParamIdx < ParamLists.size()) { | |||
2821 | if (ParamLists[ParamIdx]->size() == 0) { | |||
2822 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), | |||
2823 | false)) | |||
2824 | return nullptr; | |||
2825 | } else | |||
2826 | SawNonEmptyTemplateParameterList = true; | |||
2827 | } | |||
2828 | ||||
2829 | if (NeedEmptyTemplateHeader) { | |||
2830 | // If we're on the last of the types, and we need a 'template<>' header | |||
2831 | // here, then it's a member specialization. | |||
2832 | if (TypeIdx == NumTypes - 1) | |||
2833 | IsMemberSpecialization = true; | |||
2834 | ||||
2835 | if (ParamIdx < ParamLists.size()) { | |||
2836 | if (ParamLists[ParamIdx]->size() > 0) { | |||
2837 | // The header has template parameters when it shouldn't. Complain. | |||
2838 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), | |||
2839 | diag::err_template_param_list_matches_nontemplate) | |||
2840 | << T | |||
2841 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), | |||
2842 | ParamLists[ParamIdx]->getRAngleLoc()) | |||
2843 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); | |||
2844 | Invalid = true; | |||
2845 | return nullptr; | |||
2846 | } | |||
2847 | ||||
2848 | // Consume this template header. | |||
2849 | ++ParamIdx; | |||
2850 | continue; | |||
2851 | } | |||
2852 | ||||
2853 | if (!IsFriend) | |||
2854 | if (DiagnoseMissingExplicitSpecialization( | |||
2855 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) | |||
2856 | return nullptr; | |||
2857 | ||||
2858 | continue; | |||
2859 | } | |||
2860 | ||||
2861 | if (NeedNonemptyTemplateHeader) { | |||
2862 | // In friend declarations we can have template-ids which don't | |||
2863 | // depend on the corresponding template parameter lists. But | |||
2864 | // assume that empty parameter lists are supposed to match this | |||
2865 | // template-id. | |||
2866 | if (IsFriend && T->isDependentType()) { | |||
2867 | if (ParamIdx < ParamLists.size() && | |||
2868 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) | |||
2869 | ExpectedTemplateParams = nullptr; | |||
2870 | else | |||
2871 | continue; | |||
2872 | } | |||
2873 | ||||
2874 | if (ParamIdx < ParamLists.size()) { | |||
2875 | // Check the template parameter list, if we can. | |||
2876 | if (ExpectedTemplateParams && | |||
2877 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], | |||
2878 | ExpectedTemplateParams, | |||
2879 | true, TPL_TemplateMatch)) | |||
2880 | Invalid = true; | |||
2881 | ||||
2882 | if (!Invalid && | |||
2883 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, | |||
2884 | TPC_ClassTemplateMember)) | |||
2885 | Invalid = true; | |||
2886 | ||||
2887 | ++ParamIdx; | |||
2888 | continue; | |||
2889 | } | |||
2890 | ||||
2891 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) | |||
2892 | << T | |||
2893 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); | |||
2894 | Invalid = true; | |||
2895 | continue; | |||
2896 | } | |||
2897 | } | |||
2898 | ||||
2899 | // If there were at least as many template-ids as there were template | |||
2900 | // parameter lists, then there are no template parameter lists remaining for | |||
2901 | // the declaration itself. | |||
2902 | if (ParamIdx >= ParamLists.size()) { | |||
2903 | if (TemplateId && !IsFriend) { | |||
2904 | // We don't have a template header for the declaration itself, but we | |||
2905 | // should. | |||
2906 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, | |||
2907 | TemplateId->RAngleLoc)); | |||
2908 | ||||
2909 | // Fabricate an empty template parameter list for the invented header. | |||
2910 | return TemplateParameterList::Create(Context, SourceLocation(), | |||
2911 | SourceLocation(), None, | |||
2912 | SourceLocation(), nullptr); | |||
2913 | } | |||
2914 | ||||
2915 | return nullptr; | |||
2916 | } | |||
2917 | ||||
2918 | // If there were too many template parameter lists, complain about that now. | |||
2919 | if (ParamIdx < ParamLists.size() - 1) { | |||
2920 | bool HasAnyExplicitSpecHeader = false; | |||
2921 | bool AllExplicitSpecHeaders = true; | |||
2922 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { | |||
2923 | if (ParamLists[I]->size() == 0) | |||
2924 | HasAnyExplicitSpecHeader = true; | |||
2925 | else | |||
2926 | AllExplicitSpecHeaders = false; | |||
2927 | } | |||
2928 | ||||
2929 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), | |||
2930 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers | |||
2931 | : diag::err_template_spec_extra_headers) | |||
2932 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), | |||
2933 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); | |||
2934 | ||||
2935 | // If there was a specialization somewhere, such that 'template<>' is | |||
2936 | // not required, and there were any 'template<>' headers, note where the | |||
2937 | // specialization occurred. | |||
2938 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) | |||
2939 | Diag(ExplicitSpecLoc, | |||
2940 | diag::note_explicit_template_spec_does_not_need_header) | |||
2941 | << NestedTypes.back(); | |||
2942 | ||||
2943 | // We have a template parameter list with no corresponding scope, which | |||
2944 | // means that the resulting template declaration can't be instantiated | |||
2945 | // properly (we'll end up with dependent nodes when we shouldn't). | |||
2946 | if (!AllExplicitSpecHeaders) | |||
2947 | Invalid = true; | |||
2948 | } | |||
2949 | ||||
2950 | // C++ [temp.expl.spec]p16: | |||
2951 | // In an explicit specialization declaration for a member of a class | |||
2952 | // template or a member template that ap- pears in namespace scope, the | |||
2953 | // member template and some of its enclosing class templates may remain | |||
2954 | // unspecialized, except that the declaration shall not explicitly | |||
2955 | // specialize a class member template if its en- closing class templates | |||
2956 | // are not explicitly specialized as well. | |||
2957 | if (ParamLists.back()->size() == 0 && | |||
2958 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), | |||
2959 | false)) | |||
2960 | return nullptr; | |||
2961 | ||||
2962 | // Return the last template parameter list, which corresponds to the | |||
2963 | // entity being declared. | |||
2964 | return ParamLists.back(); | |||
2965 | } | |||
2966 | ||||
2967 | void Sema::NoteAllFoundTemplates(TemplateName Name) { | |||
2968 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { | |||
2969 | Diag(Template->getLocation(), diag::note_template_declared_here) | |||
2970 | << (isa<FunctionTemplateDecl>(Template) | |||
2971 | ? 0 | |||
2972 | : isa<ClassTemplateDecl>(Template) | |||
2973 | ? 1 | |||
2974 | : isa<VarTemplateDecl>(Template) | |||
2975 | ? 2 | |||
2976 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) | |||
2977 | << Template->getDeclName(); | |||
2978 | return; | |||
2979 | } | |||
2980 | ||||
2981 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { | |||
2982 | for (OverloadedTemplateStorage::iterator I = OST->begin(), | |||
2983 | IEnd = OST->end(); | |||
2984 | I != IEnd; ++I) | |||
2985 | Diag((*I)->getLocation(), diag::note_template_declared_here) | |||
2986 | << 0 << (*I)->getDeclName(); | |||
2987 | ||||
2988 | return; | |||
2989 | } | |||
2990 | } | |||
2991 | ||||
2992 | static QualType | |||
2993 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, | |||
2994 | const SmallVectorImpl<TemplateArgument> &Converted, | |||
2995 | SourceLocation TemplateLoc, | |||
2996 | TemplateArgumentListInfo &TemplateArgs) { | |||
2997 | ASTContext &Context = SemaRef.getASTContext(); | |||
2998 | switch (BTD->getBuiltinTemplateKind()) { | |||
2999 | case BTK__make_integer_seq: { | |||
3000 | // Specializations of __make_integer_seq<S, T, N> are treated like | |||
3001 | // S<T, 0, ..., N-1>. | |||
3002 | ||||
3003 | // C++14 [inteseq.intseq]p1: | |||
3004 | // T shall be an integer type. | |||
3005 | if (!Converted[1].getAsType()->isIntegralType(Context)) { | |||
3006 | SemaRef.Diag(TemplateArgs[1].getLocation(), | |||
3007 | diag::err_integer_sequence_integral_element_type); | |||
3008 | return QualType(); | |||
3009 | } | |||
3010 | ||||
3011 | // C++14 [inteseq.make]p1: | |||
3012 | // If N is negative the program is ill-formed. | |||
3013 | TemplateArgument NumArgsArg = Converted[2]; | |||
3014 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); | |||
3015 | if (NumArgs < 0) { | |||
3016 | SemaRef.Diag(TemplateArgs[2].getLocation(), | |||
3017 | diag::err_integer_sequence_negative_length); | |||
3018 | return QualType(); | |||
3019 | } | |||
3020 | ||||
3021 | QualType ArgTy = NumArgsArg.getIntegralType(); | |||
3022 | TemplateArgumentListInfo SyntheticTemplateArgs; | |||
3023 | // The type argument gets reused as the first template argument in the | |||
3024 | // synthetic template argument list. | |||
3025 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); | |||
3026 | // Expand N into 0 ... N-1. | |||
3027 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); | |||
3028 | I < NumArgs; ++I) { | |||
3029 | TemplateArgument TA(Context, I, ArgTy); | |||
3030 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( | |||
3031 | TA, ArgTy, TemplateArgs[2].getLocation())); | |||
3032 | } | |||
3033 | // The first template argument will be reused as the template decl that | |||
3034 | // our synthetic template arguments will be applied to. | |||
3035 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), | |||
3036 | TemplateLoc, SyntheticTemplateArgs); | |||
3037 | } | |||
3038 | ||||
3039 | case BTK__type_pack_element: | |||
3040 | // Specializations of | |||
3041 | // __type_pack_element<Index, T_1, ..., T_N> | |||
3042 | // are treated like T_Index. | |||
3043 | assert(Converted.size() == 2 &&((Converted.size() == 2 && "__type_pack_element should be given an index and a parameter pack" ) ? static_cast<void> (0) : __assert_fail ("Converted.size() == 2 && \"__type_pack_element should be given an index and a parameter pack\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3044, __PRETTY_FUNCTION__)) | |||
3044 | "__type_pack_element should be given an index and a parameter pack")((Converted.size() == 2 && "__type_pack_element should be given an index and a parameter pack" ) ? static_cast<void> (0) : __assert_fail ("Converted.size() == 2 && \"__type_pack_element should be given an index and a parameter pack\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3044, __PRETTY_FUNCTION__)); | |||
3045 | ||||
3046 | // If the Index is out of bounds, the program is ill-formed. | |||
3047 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; | |||
3048 | llvm::APSInt Index = IndexArg.getAsIntegral(); | |||
3049 | assert(Index >= 0 && "the index used with __type_pack_element should be of "((Index >= 0 && "the index used with __type_pack_element should be of " "type std::size_t, and hence be non-negative") ? static_cast <void> (0) : __assert_fail ("Index >= 0 && \"the index used with __type_pack_element should be of \" \"type std::size_t, and hence be non-negative\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3050, __PRETTY_FUNCTION__)) | |||
3050 | "type std::size_t, and hence be non-negative")((Index >= 0 && "the index used with __type_pack_element should be of " "type std::size_t, and hence be non-negative") ? static_cast <void> (0) : __assert_fail ("Index >= 0 && \"the index used with __type_pack_element should be of \" \"type std::size_t, and hence be non-negative\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3050, __PRETTY_FUNCTION__)); | |||
3051 | if (Index >= Ts.pack_size()) { | |||
3052 | SemaRef.Diag(TemplateArgs[0].getLocation(), | |||
3053 | diag::err_type_pack_element_out_of_bounds); | |||
3054 | return QualType(); | |||
3055 | } | |||
3056 | ||||
3057 | // We simply return the type at index `Index`. | |||
3058 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); | |||
3059 | return Nth->getAsType(); | |||
3060 | } | |||
3061 | llvm_unreachable("unexpected BuiltinTemplateDecl!")::llvm::llvm_unreachable_internal("unexpected BuiltinTemplateDecl!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3061); | |||
3062 | } | |||
3063 | ||||
3064 | /// Determine whether this alias template is "enable_if_t". | |||
3065 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { | |||
3066 | return AliasTemplate->getName().equals("enable_if_t"); | |||
3067 | } | |||
3068 | ||||
3069 | /// Collect all of the separable terms in the given condition, which | |||
3070 | /// might be a conjunction. | |||
3071 | /// | |||
3072 | /// FIXME: The right answer is to convert the logical expression into | |||
3073 | /// disjunctive normal form, so we can find the first failed term | |||
3074 | /// within each possible clause. | |||
3075 | static void collectConjunctionTerms(Expr *Clause, | |||
3076 | SmallVectorImpl<Expr *> &Terms) { | |||
3077 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { | |||
3078 | if (BinOp->getOpcode() == BO_LAnd) { | |||
3079 | collectConjunctionTerms(BinOp->getLHS(), Terms); | |||
3080 | collectConjunctionTerms(BinOp->getRHS(), Terms); | |||
3081 | } | |||
3082 | ||||
3083 | return; | |||
3084 | } | |||
3085 | ||||
3086 | Terms.push_back(Clause); | |||
3087 | } | |||
3088 | ||||
3089 | // The ranges-v3 library uses an odd pattern of a top-level "||" with | |||
3090 | // a left-hand side that is value-dependent but never true. Identify | |||
3091 | // the idiom and ignore that term. | |||
3092 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { | |||
3093 | // Top-level '||'. | |||
3094 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); | |||
3095 | if (!BinOp) return Cond; | |||
3096 | ||||
3097 | if (BinOp->getOpcode() != BO_LOr) return Cond; | |||
3098 | ||||
3099 | // With an inner '==' that has a literal on the right-hand side. | |||
3100 | Expr *LHS = BinOp->getLHS(); | |||
3101 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); | |||
3102 | if (!InnerBinOp) return Cond; | |||
3103 | ||||
3104 | if (InnerBinOp->getOpcode() != BO_EQ || | |||
3105 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) | |||
3106 | return Cond; | |||
3107 | ||||
3108 | // If the inner binary operation came from a macro expansion named | |||
3109 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side | |||
3110 | // of the '||', which is the real, user-provided condition. | |||
3111 | SourceLocation Loc = InnerBinOp->getExprLoc(); | |||
3112 | if (!Loc.isMacroID()) return Cond; | |||
3113 | ||||
3114 | StringRef MacroName = PP.getImmediateMacroName(Loc); | |||
3115 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") | |||
3116 | return BinOp->getRHS(); | |||
3117 | ||||
3118 | return Cond; | |||
3119 | } | |||
3120 | ||||
3121 | namespace { | |||
3122 | ||||
3123 | // A PrinterHelper that prints more helpful diagnostics for some sub-expressions | |||
3124 | // within failing boolean expression, such as substituting template parameters | |||
3125 | // for actual types. | |||
3126 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { | |||
3127 | public: | |||
3128 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) | |||
3129 | : Policy(P) {} | |||
3130 | ||||
3131 | bool handledStmt(Stmt *E, raw_ostream &OS) override { | |||
3132 | const auto *DR = dyn_cast<DeclRefExpr>(E); | |||
3133 | if (DR && DR->getQualifier()) { | |||
3134 | // If this is a qualified name, expand the template arguments in nested | |||
3135 | // qualifiers. | |||
3136 | DR->getQualifier()->print(OS, Policy, true); | |||
3137 | // Then print the decl itself. | |||
3138 | const ValueDecl *VD = DR->getDecl(); | |||
3139 | OS << VD->getName(); | |||
3140 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { | |||
3141 | // This is a template variable, print the expanded template arguments. | |||
3142 | printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); | |||
3143 | } | |||
3144 | return true; | |||
3145 | } | |||
3146 | return false; | |||
3147 | } | |||
3148 | ||||
3149 | private: | |||
3150 | const PrintingPolicy Policy; | |||
3151 | }; | |||
3152 | ||||
3153 | } // end anonymous namespace | |||
3154 | ||||
3155 | std::pair<Expr *, std::string> | |||
3156 | Sema::findFailedBooleanCondition(Expr *Cond) { | |||
3157 | Cond = lookThroughRangesV3Condition(PP, Cond); | |||
3158 | ||||
3159 | // Separate out all of the terms in a conjunction. | |||
3160 | SmallVector<Expr *, 4> Terms; | |||
3161 | collectConjunctionTerms(Cond, Terms); | |||
3162 | ||||
3163 | // Determine which term failed. | |||
3164 | Expr *FailedCond = nullptr; | |||
3165 | for (Expr *Term : Terms) { | |||
3166 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); | |||
3167 | ||||
3168 | // Literals are uninteresting. | |||
3169 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || | |||
3170 | isa<IntegerLiteral>(TermAsWritten)) | |||
3171 | continue; | |||
3172 | ||||
3173 | // The initialization of the parameter from the argument is | |||
3174 | // a constant-evaluated context. | |||
3175 | EnterExpressionEvaluationContext ConstantEvaluated( | |||
3176 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); | |||
3177 | ||||
3178 | bool Succeeded; | |||
3179 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && | |||
3180 | !Succeeded) { | |||
3181 | FailedCond = TermAsWritten; | |||
3182 | break; | |||
3183 | } | |||
3184 | } | |||
3185 | if (!FailedCond) | |||
3186 | FailedCond = Cond->IgnoreParenImpCasts(); | |||
3187 | ||||
3188 | std::string Description; | |||
3189 | { | |||
3190 | llvm::raw_string_ostream Out(Description); | |||
3191 | PrintingPolicy Policy = getPrintingPolicy(); | |||
3192 | Policy.PrintCanonicalTypes = true; | |||
3193 | FailedBooleanConditionPrinterHelper Helper(Policy); | |||
3194 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); | |||
3195 | } | |||
3196 | return { FailedCond, Description }; | |||
3197 | } | |||
3198 | ||||
3199 | QualType Sema::CheckTemplateIdType(TemplateName Name, | |||
3200 | SourceLocation TemplateLoc, | |||
3201 | TemplateArgumentListInfo &TemplateArgs) { | |||
3202 | DependentTemplateName *DTN | |||
3203 | = Name.getUnderlying().getAsDependentTemplateName(); | |||
3204 | if (DTN && DTN->isIdentifier()) | |||
3205 | // When building a template-id where the template-name is dependent, | |||
3206 | // assume the template is a type template. Either our assumption is | |||
3207 | // correct, or the code is ill-formed and will be diagnosed when the | |||
3208 | // dependent name is substituted. | |||
3209 | return Context.getDependentTemplateSpecializationType(ETK_None, | |||
3210 | DTN->getQualifier(), | |||
3211 | DTN->getIdentifier(), | |||
3212 | TemplateArgs); | |||
3213 | ||||
3214 | TemplateDecl *Template = Name.getAsTemplateDecl(); | |||
3215 | if (!Template || isa<FunctionTemplateDecl>(Template) || | |||
3216 | isa<VarTemplateDecl>(Template) || isa<ConceptDecl>(Template)) { | |||
3217 | // We might have a substituted template template parameter pack. If so, | |||
3218 | // build a template specialization type for it. | |||
3219 | if (Name.getAsSubstTemplateTemplateParmPack()) | |||
3220 | return Context.getTemplateSpecializationType(Name, TemplateArgs); | |||
3221 | ||||
3222 | Diag(TemplateLoc, diag::err_template_id_not_a_type) | |||
3223 | << Name; | |||
3224 | NoteAllFoundTemplates(Name); | |||
3225 | return QualType(); | |||
3226 | } | |||
3227 | ||||
3228 | // Check that the template argument list is well-formed for this | |||
3229 | // template. | |||
3230 | SmallVector<TemplateArgument, 4> Converted; | |||
3231 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, | |||
3232 | false, Converted, | |||
3233 | /*UpdateArgsWithConversion=*/true)) | |||
3234 | return QualType(); | |||
3235 | ||||
3236 | QualType CanonType; | |||
3237 | ||||
3238 | bool InstantiationDependent = false; | |||
3239 | if (TypeAliasTemplateDecl *AliasTemplate = | |||
3240 | dyn_cast<TypeAliasTemplateDecl>(Template)) { | |||
3241 | ||||
3242 | // Find the canonical type for this type alias template specialization. | |||
3243 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); | |||
3244 | if (Pattern->isInvalidDecl()) | |||
3245 | return QualType(); | |||
3246 | ||||
3247 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, | |||
3248 | Converted); | |||
3249 | ||||
3250 | // Only substitute for the innermost template argument list. | |||
3251 | MultiLevelTemplateArgumentList TemplateArgLists; | |||
3252 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); | |||
3253 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); | |||
3254 | for (unsigned I = 0; I < Depth; ++I) | |||
3255 | TemplateArgLists.addOuterTemplateArguments(None); | |||
3256 | ||||
3257 | LocalInstantiationScope Scope(*this); | |||
3258 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); | |||
3259 | if (Inst.isInvalid()) | |||
3260 | return QualType(); | |||
3261 | ||||
3262 | CanonType = SubstType(Pattern->getUnderlyingType(), | |||
3263 | TemplateArgLists, AliasTemplate->getLocation(), | |||
3264 | AliasTemplate->getDeclName()); | |||
3265 | if (CanonType.isNull()) { | |||
3266 | // If this was enable_if and we failed to find the nested type | |||
3267 | // within enable_if in a SFINAE context, dig out the specific | |||
3268 | // enable_if condition that failed and present that instead. | |||
3269 | if (isEnableIfAliasTemplate(AliasTemplate)) { | |||
3270 | if (auto DeductionInfo = isSFINAEContext()) { | |||
3271 | if (*DeductionInfo && | |||
3272 | (*DeductionInfo)->hasSFINAEDiagnostic() && | |||
3273 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == | |||
3274 | diag::err_typename_nested_not_found_enable_if && | |||
3275 | TemplateArgs[0].getArgument().getKind() | |||
3276 | == TemplateArgument::Expression) { | |||
3277 | Expr *FailedCond; | |||
3278 | std::string FailedDescription; | |||
3279 | std::tie(FailedCond, FailedDescription) = | |||
3280 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); | |||
3281 | ||||
3282 | // Remove the old SFINAE diagnostic. | |||
3283 | PartialDiagnosticAt OldDiag = | |||
3284 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; | |||
3285 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); | |||
3286 | ||||
3287 | // Add a new SFINAE diagnostic specifying which condition | |||
3288 | // failed. | |||
3289 | (*DeductionInfo)->addSFINAEDiagnostic( | |||
3290 | OldDiag.first, | |||
3291 | PDiag(diag::err_typename_nested_not_found_requirement) | |||
3292 | << FailedDescription | |||
3293 | << FailedCond->getSourceRange()); | |||
3294 | } | |||
3295 | } | |||
3296 | } | |||
3297 | ||||
3298 | return QualType(); | |||
3299 | } | |||
3300 | } else if (Name.isDependent() || | |||
3301 | TemplateSpecializationType::anyDependentTemplateArguments( | |||
3302 | TemplateArgs, InstantiationDependent)) { | |||
3303 | // This class template specialization is a dependent | |||
3304 | // type. Therefore, its canonical type is another class template | |||
3305 | // specialization type that contains all of the converted | |||
3306 | // arguments in canonical form. This ensures that, e.g., A<T> and | |||
3307 | // A<T, T> have identical types when A is declared as: | |||
3308 | // | |||
3309 | // template<typename T, typename U = T> struct A; | |||
3310 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); | |||
3311 | ||||
3312 | // This might work out to be a current instantiation, in which | |||
3313 | // case the canonical type needs to be the InjectedClassNameType. | |||
3314 | // | |||
3315 | // TODO: in theory this could be a simple hashtable lookup; most | |||
3316 | // changes to CurContext don't change the set of current | |||
3317 | // instantiations. | |||
3318 | if (isa<ClassTemplateDecl>(Template)) { | |||
3319 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { | |||
3320 | // If we get out to a namespace, we're done. | |||
3321 | if (Ctx->isFileContext()) break; | |||
3322 | ||||
3323 | // If this isn't a record, keep looking. | |||
3324 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); | |||
3325 | if (!Record) continue; | |||
3326 | ||||
3327 | // Look for one of the two cases with InjectedClassNameTypes | |||
3328 | // and check whether it's the same template. | |||
3329 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && | |||
3330 | !Record->getDescribedClassTemplate()) | |||
3331 | continue; | |||
3332 | ||||
3333 | // Fetch the injected class name type and check whether its | |||
3334 | // injected type is equal to the type we just built. | |||
3335 | QualType ICNT = Context.getTypeDeclType(Record); | |||
3336 | QualType Injected = cast<InjectedClassNameType>(ICNT) | |||
3337 | ->getInjectedSpecializationType(); | |||
3338 | ||||
3339 | if (CanonType != Injected->getCanonicalTypeInternal()) | |||
3340 | continue; | |||
3341 | ||||
3342 | // If so, the canonical type of this TST is the injected | |||
3343 | // class name type of the record we just found. | |||
3344 | assert(ICNT.isCanonical())((ICNT.isCanonical()) ? static_cast<void> (0) : __assert_fail ("ICNT.isCanonical()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3344, __PRETTY_FUNCTION__)); | |||
3345 | CanonType = ICNT; | |||
3346 | break; | |||
3347 | } | |||
3348 | } | |||
3349 | } else if (ClassTemplateDecl *ClassTemplate | |||
3350 | = dyn_cast<ClassTemplateDecl>(Template)) { | |||
3351 | // Find the class template specialization declaration that | |||
3352 | // corresponds to these arguments. | |||
3353 | void *InsertPos = nullptr; | |||
3354 | ClassTemplateSpecializationDecl *Decl | |||
3355 | = ClassTemplate->findSpecialization(Converted, InsertPos); | |||
3356 | if (!Decl) { | |||
3357 | // This is the first time we have referenced this class template | |||
3358 | // specialization. Create the canonical declaration and add it to | |||
3359 | // the set of specializations. | |||
3360 | Decl = ClassTemplateSpecializationDecl::Create( | |||
3361 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), | |||
3362 | ClassTemplate->getDeclContext(), | |||
3363 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), | |||
3364 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); | |||
3365 | ClassTemplate->AddSpecialization(Decl, InsertPos); | |||
3366 | if (ClassTemplate->isOutOfLine()) | |||
3367 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); | |||
3368 | } | |||
3369 | ||||
3370 | if (Decl->getSpecializationKind() == TSK_Undeclared) { | |||
3371 | MultiLevelTemplateArgumentList TemplateArgLists; | |||
3372 | TemplateArgLists.addOuterTemplateArguments(Converted); | |||
3373 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), | |||
3374 | Decl); | |||
3375 | } | |||
3376 | ||||
3377 | // Diagnose uses of this specialization. | |||
3378 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); | |||
3379 | ||||
3380 | CanonType = Context.getTypeDeclType(Decl); | |||
3381 | assert(isa<RecordType>(CanonType) &&((isa<RecordType>(CanonType) && "type of non-dependent specialization is not a RecordType" ) ? static_cast<void> (0) : __assert_fail ("isa<RecordType>(CanonType) && \"type of non-dependent specialization is not a RecordType\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3382, __PRETTY_FUNCTION__)) | |||
3382 | "type of non-dependent specialization is not a RecordType")((isa<RecordType>(CanonType) && "type of non-dependent specialization is not a RecordType" ) ? static_cast<void> (0) : __assert_fail ("isa<RecordType>(CanonType) && \"type of non-dependent specialization is not a RecordType\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3382, __PRETTY_FUNCTION__)); | |||
3383 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { | |||
3384 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, | |||
3385 | TemplateArgs); | |||
3386 | } | |||
3387 | ||||
3388 | // Build the fully-sugared type for this class template | |||
3389 | // specialization, which refers back to the class template | |||
3390 | // specialization we created or found. | |||
3391 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); | |||
3392 | } | |||
3393 | ||||
3394 | void Sema::ActOnUndeclaredTypeTemplateName(Scope *S, TemplateTy &ParsedName, | |||
3395 | TemplateNameKind &TNK, | |||
3396 | SourceLocation NameLoc, | |||
3397 | IdentifierInfo *&II) { | |||
3398 | assert(TNK == TNK_Undeclared_template && "not an undeclared template name")((TNK == TNK_Undeclared_template && "not an undeclared template name" ) ? static_cast<void> (0) : __assert_fail ("TNK == TNK_Undeclared_template && \"not an undeclared template name\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3398, __PRETTY_FUNCTION__)); | |||
3399 | ||||
3400 | TemplateName Name = ParsedName.get(); | |||
3401 | auto *ATN = Name.getAsAssumedTemplateName(); | |||
3402 | assert(ATN && "not an assumed template name")((ATN && "not an assumed template name") ? static_cast <void> (0) : __assert_fail ("ATN && \"not an assumed template name\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3402, __PRETTY_FUNCTION__)); | |||
3403 | II = ATN->getDeclName().getAsIdentifierInfo(); | |||
3404 | ||||
3405 | if (!resolveAssumedTemplateNameAsType(S, Name, NameLoc, /*Diagnose*/false)) { | |||
3406 | // Resolved to a type template name. | |||
3407 | ParsedName = TemplateTy::make(Name); | |||
3408 | TNK = TNK_Type_template; | |||
3409 | } | |||
3410 | } | |||
3411 | ||||
3412 | bool Sema::resolveAssumedTemplateNameAsType(Scope *S, TemplateName &Name, | |||
3413 | SourceLocation NameLoc, | |||
3414 | bool Diagnose) { | |||
3415 | // We assumed this undeclared identifier to be an (ADL-only) function | |||
3416 | // template name, but it was used in a context where a type was required. | |||
3417 | // Try to typo-correct it now. | |||
3418 | AssumedTemplateStorage *ATN = Name.getAsAssumedTemplateName(); | |||
3419 | assert(ATN && "not an assumed template name")((ATN && "not an assumed template name") ? static_cast <void> (0) : __assert_fail ("ATN && \"not an assumed template name\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3419, __PRETTY_FUNCTION__)); | |||
3420 | ||||
3421 | LookupResult R(*this, ATN->getDeclName(), NameLoc, LookupOrdinaryName); | |||
3422 | struct CandidateCallback : CorrectionCandidateCallback { | |||
3423 | bool ValidateCandidate(const TypoCorrection &TC) override { | |||
3424 | return TC.getCorrectionDecl() && | |||
3425 | getAsTypeTemplateDecl(TC.getCorrectionDecl()); | |||
3426 | } | |||
3427 | std::unique_ptr<CorrectionCandidateCallback> clone() override { | |||
3428 | return std::make_unique<CandidateCallback>(*this); | |||
3429 | } | |||
3430 | } FilterCCC; | |||
3431 | ||||
3432 | TypoCorrection Corrected = | |||
3433 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr, | |||
3434 | FilterCCC, CTK_ErrorRecovery); | |||
3435 | if (Corrected && Corrected.getFoundDecl()) { | |||
3436 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) | |||
3437 | << ATN->getDeclName()); | |||
3438 | Name = TemplateName(Corrected.getCorrectionDeclAs<TemplateDecl>()); | |||
3439 | return false; | |||
3440 | } | |||
3441 | ||||
3442 | if (Diagnose) | |||
3443 | Diag(R.getNameLoc(), diag::err_no_template) << R.getLookupName(); | |||
3444 | return true; | |||
3445 | } | |||
3446 | ||||
3447 | TypeResult Sema::ActOnTemplateIdType( | |||
3448 | Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, | |||
3449 | TemplateTy TemplateD, IdentifierInfo *TemplateII, | |||
3450 | SourceLocation TemplateIILoc, SourceLocation LAngleLoc, | |||
3451 | ASTTemplateArgsPtr TemplateArgsIn, SourceLocation RAngleLoc, | |||
3452 | bool IsCtorOrDtorName, bool IsClassName) { | |||
3453 | if (SS.isInvalid()) | |||
3454 | return true; | |||
3455 | ||||
3456 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { | |||
3457 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); | |||
3458 | ||||
3459 | // C++ [temp.res]p3: | |||
3460 | // A qualified-id that refers to a type and in which the | |||
3461 | // nested-name-specifier depends on a template-parameter (14.6.2) | |||
3462 | // shall be prefixed by the keyword typename to indicate that the | |||
3463 | // qualified-id denotes a type, forming an | |||
3464 | // elaborated-type-specifier (7.1.5.3). | |||
3465 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { | |||
3466 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) | |||
3467 | << SS.getScopeRep() << TemplateII->getName(); | |||
3468 | // Recover as if 'typename' were specified. | |||
3469 | // FIXME: This is not quite correct recovery as we don't transform SS | |||
3470 | // into the corresponding dependent form (and we don't diagnose missing | |||
3471 | // 'template' keywords within SS as a result). | |||
3472 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, | |||
3473 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, | |||
3474 | TemplateArgsIn, RAngleLoc); | |||
3475 | } | |||
3476 | ||||
3477 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, | |||
3478 | // it's not actually allowed to be used as a type in most cases. Because | |||
3479 | // we annotate it before we know whether it's valid, we have to check for | |||
3480 | // this case here. | |||
3481 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); | |||
3482 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { | |||
3483 | Diag(TemplateIILoc, | |||
3484 | TemplateKWLoc.isInvalid() | |||
3485 | ? diag::err_out_of_line_qualified_id_type_names_constructor | |||
3486 | : diag::ext_out_of_line_qualified_id_type_names_constructor) | |||
3487 | << TemplateII << 0 /*injected-class-name used as template name*/ | |||
3488 | << 1 /*if any keyword was present, it was 'template'*/; | |||
3489 | } | |||
3490 | } | |||
3491 | ||||
3492 | TemplateName Template = TemplateD.get(); | |||
3493 | if (Template.getAsAssumedTemplateName() && | |||
3494 | resolveAssumedTemplateNameAsType(S, Template, TemplateIILoc)) | |||
3495 | return true; | |||
3496 | ||||
3497 | // Translate the parser's template argument list in our AST format. | |||
3498 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | |||
3499 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | |||
3500 | ||||
3501 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | |||
3502 | QualType T | |||
3503 | = Context.getDependentTemplateSpecializationType(ETK_None, | |||
3504 | DTN->getQualifier(), | |||
3505 | DTN->getIdentifier(), | |||
3506 | TemplateArgs); | |||
3507 | // Build type-source information. | |||
3508 | TypeLocBuilder TLB; | |||
3509 | DependentTemplateSpecializationTypeLoc SpecTL | |||
3510 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); | |||
3511 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); | |||
3512 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
3513 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
3514 | SpecTL.setTemplateNameLoc(TemplateIILoc); | |||
3515 | SpecTL.setLAngleLoc(LAngleLoc); | |||
3516 | SpecTL.setRAngleLoc(RAngleLoc); | |||
3517 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) | |||
3518 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | |||
3519 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); | |||
3520 | } | |||
3521 | ||||
3522 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); | |||
3523 | if (Result.isNull()) | |||
3524 | return true; | |||
3525 | ||||
3526 | // Build type-source information. | |||
3527 | TypeLocBuilder TLB; | |||
3528 | TemplateSpecializationTypeLoc SpecTL | |||
3529 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | |||
3530 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
3531 | SpecTL.setTemplateNameLoc(TemplateIILoc); | |||
3532 | SpecTL.setLAngleLoc(LAngleLoc); | |||
3533 | SpecTL.setRAngleLoc(RAngleLoc); | |||
3534 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) | |||
3535 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); | |||
3536 | ||||
3537 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a | |||
3538 | // constructor or destructor name (in such a case, the scope specifier | |||
3539 | // will be attached to the enclosing Decl or Expr node). | |||
3540 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { | |||
3541 | // Create an elaborated-type-specifier containing the nested-name-specifier. | |||
3542 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); | |||
3543 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); | |||
3544 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); | |||
3545 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
3546 | } | |||
3547 | ||||
3548 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | |||
3549 | } | |||
3550 | ||||
3551 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, | |||
3552 | TypeSpecifierType TagSpec, | |||
3553 | SourceLocation TagLoc, | |||
3554 | CXXScopeSpec &SS, | |||
3555 | SourceLocation TemplateKWLoc, | |||
3556 | TemplateTy TemplateD, | |||
3557 | SourceLocation TemplateLoc, | |||
3558 | SourceLocation LAngleLoc, | |||
3559 | ASTTemplateArgsPtr TemplateArgsIn, | |||
3560 | SourceLocation RAngleLoc) { | |||
3561 | TemplateName Template = TemplateD.get(); | |||
3562 | ||||
3563 | // Translate the parser's template argument list in our AST format. | |||
3564 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | |||
3565 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | |||
3566 | ||||
3567 | // Determine the tag kind | |||
3568 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | |||
3569 | ElaboratedTypeKeyword Keyword | |||
3570 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); | |||
3571 | ||||
3572 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | |||
3573 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, | |||
3574 | DTN->getQualifier(), | |||
3575 | DTN->getIdentifier(), | |||
3576 | TemplateArgs); | |||
3577 | ||||
3578 | // Build type-source information. | |||
3579 | TypeLocBuilder TLB; | |||
3580 | DependentTemplateSpecializationTypeLoc SpecTL | |||
3581 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); | |||
3582 | SpecTL.setElaboratedKeywordLoc(TagLoc); | |||
3583 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
3584 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
3585 | SpecTL.setTemplateNameLoc(TemplateLoc); | |||
3586 | SpecTL.setLAngleLoc(LAngleLoc); | |||
3587 | SpecTL.setRAngleLoc(RAngleLoc); | |||
3588 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) | |||
3589 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | |||
3590 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); | |||
3591 | } | |||
3592 | ||||
3593 | if (TypeAliasTemplateDecl *TAT = | |||
3594 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { | |||
3595 | // C++0x [dcl.type.elab]p2: | |||
3596 | // If the identifier resolves to a typedef-name or the simple-template-id | |||
3597 | // resolves to an alias template specialization, the | |||
3598 | // elaborated-type-specifier is ill-formed. | |||
3599 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) | |||
3600 | << TAT << NTK_TypeAliasTemplate << TagKind; | |||
3601 | Diag(TAT->getLocation(), diag::note_declared_at); | |||
3602 | } | |||
3603 | ||||
3604 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); | |||
3605 | if (Result.isNull()) | |||
3606 | return TypeResult(true); | |||
3607 | ||||
3608 | // Check the tag kind | |||
3609 | if (const RecordType *RT = Result->getAs<RecordType>()) { | |||
3610 | RecordDecl *D = RT->getDecl(); | |||
3611 | ||||
3612 | IdentifierInfo *Id = D->getIdentifier(); | |||
3613 | assert(Id && "templated class must have an identifier")((Id && "templated class must have an identifier") ? static_cast <void> (0) : __assert_fail ("Id && \"templated class must have an identifier\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3613, __PRETTY_FUNCTION__)); | |||
3614 | ||||
3615 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, | |||
3616 | TagLoc, Id)) { | |||
3617 | Diag(TagLoc, diag::err_use_with_wrong_tag) | |||
3618 | << Result | |||
3619 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); | |||
3620 | Diag(D->getLocation(), diag::note_previous_use); | |||
3621 | } | |||
3622 | } | |||
3623 | ||||
3624 | // Provide source-location information for the template specialization. | |||
3625 | TypeLocBuilder TLB; | |||
3626 | TemplateSpecializationTypeLoc SpecTL | |||
3627 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | |||
3628 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
3629 | SpecTL.setTemplateNameLoc(TemplateLoc); | |||
3630 | SpecTL.setLAngleLoc(LAngleLoc); | |||
3631 | SpecTL.setRAngleLoc(RAngleLoc); | |||
3632 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) | |||
3633 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); | |||
3634 | ||||
3635 | // Construct an elaborated type containing the nested-name-specifier (if any) | |||
3636 | // and tag keyword. | |||
3637 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); | |||
3638 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); | |||
3639 | ElabTL.setElaboratedKeywordLoc(TagLoc); | |||
3640 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
3641 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | |||
3642 | } | |||
3643 | ||||
3644 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, | |||
3645 | NamedDecl *PrevDecl, | |||
3646 | SourceLocation Loc, | |||
3647 | bool IsPartialSpecialization); | |||
3648 | ||||
3649 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); | |||
3650 | ||||
3651 | static bool isTemplateArgumentTemplateParameter( | |||
3652 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { | |||
3653 | switch (Arg.getKind()) { | |||
3654 | case TemplateArgument::Null: | |||
3655 | case TemplateArgument::NullPtr: | |||
3656 | case TemplateArgument::Integral: | |||
3657 | case TemplateArgument::Declaration: | |||
3658 | case TemplateArgument::Pack: | |||
3659 | case TemplateArgument::TemplateExpansion: | |||
3660 | return false; | |||
3661 | ||||
3662 | case TemplateArgument::Type: { | |||
3663 | QualType Type = Arg.getAsType(); | |||
3664 | const TemplateTypeParmType *TPT = | |||
3665 | Arg.getAsType()->getAs<TemplateTypeParmType>(); | |||
3666 | return TPT && !Type.hasQualifiers() && | |||
3667 | TPT->getDepth() == Depth && TPT->getIndex() == Index; | |||
3668 | } | |||
3669 | ||||
3670 | case TemplateArgument::Expression: { | |||
3671 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); | |||
3672 | if (!DRE || !DRE->getDecl()) | |||
3673 | return false; | |||
3674 | const NonTypeTemplateParmDecl *NTTP = | |||
3675 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); | |||
3676 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; | |||
3677 | } | |||
3678 | ||||
3679 | case TemplateArgument::Template: | |||
3680 | const TemplateTemplateParmDecl *TTP = | |||
3681 | dyn_cast_or_null<TemplateTemplateParmDecl>( | |||
3682 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); | |||
3683 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; | |||
3684 | } | |||
3685 | llvm_unreachable("unexpected kind of template argument")::llvm::llvm_unreachable_internal("unexpected kind of template argument" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3685); | |||
3686 | } | |||
3687 | ||||
3688 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, | |||
3689 | ArrayRef<TemplateArgument> Args) { | |||
3690 | if (Params->size() != Args.size()) | |||
3691 | return false; | |||
3692 | ||||
3693 | unsigned Depth = Params->getDepth(); | |||
3694 | ||||
3695 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { | |||
3696 | TemplateArgument Arg = Args[I]; | |||
3697 | ||||
3698 | // If the parameter is a pack expansion, the argument must be a pack | |||
3699 | // whose only element is a pack expansion. | |||
3700 | if (Params->getParam(I)->isParameterPack()) { | |||
3701 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || | |||
3702 | !Arg.pack_begin()->isPackExpansion()) | |||
3703 | return false; | |||
3704 | Arg = Arg.pack_begin()->getPackExpansionPattern(); | |||
3705 | } | |||
3706 | ||||
3707 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) | |||
3708 | return false; | |||
3709 | } | |||
3710 | ||||
3711 | return true; | |||
3712 | } | |||
3713 | ||||
3714 | /// Convert the parser's template argument list representation into our form. | |||
3715 | static TemplateArgumentListInfo | |||
3716 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { | |||
3717 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, | |||
3718 | TemplateId.RAngleLoc); | |||
3719 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), | |||
3720 | TemplateId.NumArgs); | |||
3721 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); | |||
3722 | return TemplateArgs; | |||
3723 | } | |||
3724 | ||||
3725 | template<typename PartialSpecDecl> | |||
3726 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { | |||
3727 | if (Partial->getDeclContext()->isDependentContext()) | |||
3728 | return; | |||
3729 | ||||
3730 | // FIXME: Get the TDK from deduction in order to provide better diagnostics | |||
3731 | // for non-substitution-failure issues? | |||
3732 | TemplateDeductionInfo Info(Partial->getLocation()); | |||
3733 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) | |||
3734 | return; | |||
3735 | ||||
3736 | auto *Template = Partial->getSpecializedTemplate(); | |||
3737 | S.Diag(Partial->getLocation(), | |||
3738 | diag::ext_partial_spec_not_more_specialized_than_primary) | |||
3739 | << isa<VarTemplateDecl>(Template); | |||
3740 | ||||
3741 | if (Info.hasSFINAEDiagnostic()) { | |||
3742 | PartialDiagnosticAt Diag = {SourceLocation(), | |||
3743 | PartialDiagnostic::NullDiagnostic()}; | |||
3744 | Info.takeSFINAEDiagnostic(Diag); | |||
3745 | SmallString<128> SFINAEArgString; | |||
3746 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); | |||
3747 | S.Diag(Diag.first, | |||
3748 | diag::note_partial_spec_not_more_specialized_than_primary) | |||
3749 | << SFINAEArgString; | |||
3750 | } | |||
3751 | ||||
3752 | S.Diag(Template->getLocation(), diag::note_template_decl_here); | |||
3753 | SmallVector<const Expr *, 3> PartialAC, TemplateAC; | |||
3754 | Template->getAssociatedConstraints(TemplateAC); | |||
3755 | Partial->getAssociatedConstraints(PartialAC); | |||
3756 | S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Partial, PartialAC, Template, | |||
3757 | TemplateAC); | |||
3758 | } | |||
3759 | ||||
3760 | static void | |||
3761 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, | |||
3762 | const llvm::SmallBitVector &DeducibleParams) { | |||
3763 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { | |||
3764 | if (!DeducibleParams[I]) { | |||
3765 | NamedDecl *Param = TemplateParams->getParam(I); | |||
3766 | if (Param->getDeclName()) | |||
3767 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) | |||
3768 | << Param->getDeclName(); | |||
3769 | else | |||
3770 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) | |||
3771 | << "(anonymous)"; | |||
3772 | } | |||
3773 | } | |||
3774 | } | |||
3775 | ||||
3776 | ||||
3777 | template<typename PartialSpecDecl> | |||
3778 | static void checkTemplatePartialSpecialization(Sema &S, | |||
3779 | PartialSpecDecl *Partial) { | |||
3780 | // C++1z [temp.class.spec]p8: (DR1495) | |||
3781 | // - The specialization shall be more specialized than the primary | |||
3782 | // template (14.5.5.2). | |||
3783 | checkMoreSpecializedThanPrimary(S, Partial); | |||
3784 | ||||
3785 | // C++ [temp.class.spec]p8: (DR1315) | |||
3786 | // - Each template-parameter shall appear at least once in the | |||
3787 | // template-id outside a non-deduced context. | |||
3788 | // C++1z [temp.class.spec.match]p3 (P0127R2) | |||
3789 | // If the template arguments of a partial specialization cannot be | |||
3790 | // deduced because of the structure of its template-parameter-list | |||
3791 | // and the template-id, the program is ill-formed. | |||
3792 | auto *TemplateParams = Partial->getTemplateParameters(); | |||
3793 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | |||
3794 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | |||
3795 | TemplateParams->getDepth(), DeducibleParams); | |||
3796 | ||||
3797 | if (!DeducibleParams.all()) { | |||
3798 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | |||
3799 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) | |||
3800 | << isa<VarTemplatePartialSpecializationDecl>(Partial) | |||
3801 | << (NumNonDeducible > 1) | |||
3802 | << SourceRange(Partial->getLocation(), | |||
3803 | Partial->getTemplateArgsAsWritten()->RAngleLoc); | |||
3804 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); | |||
3805 | } | |||
3806 | } | |||
3807 | ||||
3808 | void Sema::CheckTemplatePartialSpecialization( | |||
3809 | ClassTemplatePartialSpecializationDecl *Partial) { | |||
3810 | checkTemplatePartialSpecialization(*this, Partial); | |||
3811 | } | |||
3812 | ||||
3813 | void Sema::CheckTemplatePartialSpecialization( | |||
3814 | VarTemplatePartialSpecializationDecl *Partial) { | |||
3815 | checkTemplatePartialSpecialization(*this, Partial); | |||
3816 | } | |||
3817 | ||||
3818 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { | |||
3819 | // C++1z [temp.param]p11: | |||
3820 | // A template parameter of a deduction guide template that does not have a | |||
3821 | // default-argument shall be deducible from the parameter-type-list of the | |||
3822 | // deduction guide template. | |||
3823 | auto *TemplateParams = TD->getTemplateParameters(); | |||
3824 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | |||
3825 | MarkDeducedTemplateParameters(TD, DeducibleParams); | |||
3826 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { | |||
3827 | // A parameter pack is deducible (to an empty pack). | |||
3828 | auto *Param = TemplateParams->getParam(I); | |||
3829 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) | |||
3830 | DeducibleParams[I] = true; | |||
3831 | } | |||
3832 | ||||
3833 | if (!DeducibleParams.all()) { | |||
3834 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | |||
3835 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) | |||
3836 | << (NumNonDeducible > 1); | |||
3837 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); | |||
3838 | } | |||
3839 | } | |||
3840 | ||||
3841 | DeclResult Sema::ActOnVarTemplateSpecialization( | |||
3842 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, | |||
3843 | TemplateParameterList *TemplateParams, StorageClass SC, | |||
3844 | bool IsPartialSpecialization) { | |||
3845 | // D must be variable template id. | |||
3846 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId &&((D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && "Variable template specialization is declared with a template it." ) ? static_cast<void> (0) : __assert_fail ("D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && \"Variable template specialization is declared with a template it.\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3847, __PRETTY_FUNCTION__)) | |||
3847 | "Variable template specialization is declared with a template it.")((D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && "Variable template specialization is declared with a template it." ) ? static_cast<void> (0) : __assert_fail ("D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && \"Variable template specialization is declared with a template it.\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 3847, __PRETTY_FUNCTION__)); | |||
3848 | ||||
3849 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | |||
3850 | TemplateArgumentListInfo TemplateArgs = | |||
3851 | makeTemplateArgumentListInfo(*this, *TemplateId); | |||
3852 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); | |||
3853 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; | |||
3854 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; | |||
3855 | ||||
3856 | TemplateName Name = TemplateId->Template.get(); | |||
3857 | ||||
3858 | // The template-id must name a variable template. | |||
3859 | VarTemplateDecl *VarTemplate = | |||
3860 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); | |||
3861 | if (!VarTemplate) { | |||
3862 | NamedDecl *FnTemplate; | |||
3863 | if (auto *OTS = Name.getAsOverloadedTemplate()) | |||
3864 | FnTemplate = *OTS->begin(); | |||
3865 | else | |||
3866 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); | |||
3867 | if (FnTemplate) | |||
3868 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) | |||
3869 | << FnTemplate->getDeclName(); | |||
3870 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) | |||
3871 | << IsPartialSpecialization; | |||
3872 | } | |||
3873 | ||||
3874 | // Check for unexpanded parameter packs in any of the template arguments. | |||
3875 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | |||
3876 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], | |||
3877 | UPPC_PartialSpecialization)) | |||
3878 | return true; | |||
3879 | ||||
3880 | // Check that the template argument list is well-formed for this | |||
3881 | // template. | |||
3882 | SmallVector<TemplateArgument, 4> Converted; | |||
3883 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, | |||
3884 | false, Converted, | |||
3885 | /*UpdateArgsWithConversion=*/true)) | |||
3886 | return true; | |||
3887 | ||||
3888 | // Find the variable template (partial) specialization declaration that | |||
3889 | // corresponds to these arguments. | |||
3890 | if (IsPartialSpecialization) { | |||
3891 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, | |||
3892 | TemplateArgs.size(), Converted)) | |||
3893 | return true; | |||
3894 | ||||
3895 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we | |||
3896 | // also do them during instantiation. | |||
3897 | bool InstantiationDependent; | |||
3898 | if (!Name.isDependent() && | |||
3899 | !TemplateSpecializationType::anyDependentTemplateArguments( | |||
3900 | TemplateArgs.arguments(), | |||
3901 | InstantiationDependent)) { | |||
3902 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) | |||
3903 | << VarTemplate->getDeclName(); | |||
3904 | IsPartialSpecialization = false; | |||
3905 | } | |||
3906 | ||||
3907 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), | |||
3908 | Converted) && | |||
3909 | (!Context.getLangOpts().ConceptsTS || | |||
3910 | !TemplateParams->hasAssociatedConstraints())) { | |||
3911 | // C++ [temp.class.spec]p9b3: | |||
3912 | // | |||
3913 | // -- The argument list of the specialization shall not be identical | |||
3914 | // to the implicit argument list of the primary template. | |||
3915 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) | |||
3916 | << /*variable template*/ 1 | |||
3917 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) | |||
3918 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); | |||
3919 | // FIXME: Recover from this by treating the declaration as a redeclaration | |||
3920 | // of the primary template. | |||
3921 | return true; | |||
3922 | } | |||
3923 | } | |||
3924 | ||||
3925 | void *InsertPos = nullptr; | |||
3926 | VarTemplateSpecializationDecl *PrevDecl = nullptr; | |||
3927 | ||||
3928 | if (IsPartialSpecialization) | |||
3929 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, TemplateParams, | |||
3930 | InsertPos); | |||
3931 | else | |||
3932 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); | |||
3933 | ||||
3934 | VarTemplateSpecializationDecl *Specialization = nullptr; | |||
3935 | ||||
3936 | // Check whether we can declare a variable template specialization in | |||
3937 | // the current scope. | |||
3938 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, | |||
3939 | TemplateNameLoc, | |||
3940 | IsPartialSpecialization)) | |||
3941 | return true; | |||
3942 | ||||
3943 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { | |||
3944 | // Since the only prior variable template specialization with these | |||
3945 | // arguments was referenced but not declared, reuse that | |||
3946 | // declaration node as our own, updating its source location and | |||
3947 | // the list of outer template parameters to reflect our new declaration. | |||
3948 | Specialization = PrevDecl; | |||
3949 | Specialization->setLocation(TemplateNameLoc); | |||
3950 | PrevDecl = nullptr; | |||
3951 | } else if (IsPartialSpecialization) { | |||
3952 | // Create a new class template partial specialization declaration node. | |||
3953 | VarTemplatePartialSpecializationDecl *PrevPartial = | |||
3954 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); | |||
3955 | VarTemplatePartialSpecializationDecl *Partial = | |||
3956 | VarTemplatePartialSpecializationDecl::Create( | |||
3957 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, | |||
3958 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, | |||
3959 | Converted, TemplateArgs); | |||
3960 | ||||
3961 | if (!PrevPartial) | |||
3962 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); | |||
3963 | Specialization = Partial; | |||
3964 | ||||
3965 | // If we are providing an explicit specialization of a member variable | |||
3966 | // template specialization, make a note of that. | |||
3967 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) | |||
3968 | PrevPartial->setMemberSpecialization(); | |||
3969 | ||||
3970 | CheckTemplatePartialSpecialization(Partial); | |||
3971 | } else { | |||
3972 | // Create a new class template specialization declaration node for | |||
3973 | // this explicit specialization or friend declaration. | |||
3974 | Specialization = VarTemplateSpecializationDecl::Create( | |||
3975 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, | |||
3976 | VarTemplate, DI->getType(), DI, SC, Converted); | |||
3977 | Specialization->setTemplateArgsInfo(TemplateArgs); | |||
3978 | ||||
3979 | if (!PrevDecl) | |||
3980 | VarTemplate->AddSpecialization(Specialization, InsertPos); | |||
3981 | } | |||
3982 | ||||
3983 | // C++ [temp.expl.spec]p6: | |||
3984 | // If a template, a member template or the member of a class template is | |||
3985 | // explicitly specialized then that specialization shall be declared | |||
3986 | // before the first use of that specialization that would cause an implicit | |||
3987 | // instantiation to take place, in every translation unit in which such a | |||
3988 | // use occurs; no diagnostic is required. | |||
3989 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { | |||
3990 | bool Okay = false; | |||
3991 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | |||
3992 | // Is there any previous explicit specialization declaration? | |||
3993 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { | |||
3994 | Okay = true; | |||
3995 | break; | |||
3996 | } | |||
3997 | } | |||
3998 | ||||
3999 | if (!Okay) { | |||
4000 | SourceRange Range(TemplateNameLoc, RAngleLoc); | |||
4001 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) | |||
4002 | << Name << Range; | |||
4003 | ||||
4004 | Diag(PrevDecl->getPointOfInstantiation(), | |||
4005 | diag::note_instantiation_required_here) | |||
4006 | << (PrevDecl->getTemplateSpecializationKind() != | |||
4007 | TSK_ImplicitInstantiation); | |||
4008 | return true; | |||
4009 | } | |||
4010 | } | |||
4011 | ||||
4012 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); | |||
4013 | Specialization->setLexicalDeclContext(CurContext); | |||
4014 | ||||
4015 | // Add the specialization into its lexical context, so that it can | |||
4016 | // be seen when iterating through the list of declarations in that | |||
4017 | // context. However, specializations are not found by name lookup. | |||
4018 | CurContext->addDecl(Specialization); | |||
4019 | ||||
4020 | // Note that this is an explicit specialization. | |||
4021 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); | |||
4022 | ||||
4023 | if (PrevDecl) { | |||
4024 | // Check that this isn't a redefinition of this specialization, | |||
4025 | // merging with previous declarations. | |||
4026 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, | |||
4027 | forRedeclarationInCurContext()); | |||
4028 | PrevSpec.addDecl(PrevDecl); | |||
4029 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); | |||
4030 | } else if (Specialization->isStaticDataMember() && | |||
4031 | Specialization->isOutOfLine()) { | |||
4032 | Specialization->setAccess(VarTemplate->getAccess()); | |||
4033 | } | |||
4034 | ||||
4035 | return Specialization; | |||
4036 | } | |||
4037 | ||||
4038 | namespace { | |||
4039 | /// A partial specialization whose template arguments have matched | |||
4040 | /// a given template-id. | |||
4041 | struct PartialSpecMatchResult { | |||
4042 | VarTemplatePartialSpecializationDecl *Partial; | |||
4043 | TemplateArgumentList *Args; | |||
4044 | }; | |||
4045 | } // end anonymous namespace | |||
4046 | ||||
4047 | DeclResult | |||
4048 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, | |||
4049 | SourceLocation TemplateNameLoc, | |||
4050 | const TemplateArgumentListInfo &TemplateArgs) { | |||
4051 | assert(Template && "A variable template id without template?")((Template && "A variable template id without template?" ) ? static_cast<void> (0) : __assert_fail ("Template && \"A variable template id without template?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4051, __PRETTY_FUNCTION__)); | |||
4052 | ||||
4053 | // Check that the template argument list is well-formed for this template. | |||
4054 | SmallVector<TemplateArgument, 4> Converted; | |||
4055 | if (CheckTemplateArgumentList( | |||
4056 | Template, TemplateNameLoc, | |||
4057 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, | |||
4058 | Converted, /*UpdateArgsWithConversion=*/true)) | |||
4059 | return true; | |||
4060 | ||||
4061 | // Find the variable template specialization declaration that | |||
4062 | // corresponds to these arguments. | |||
4063 | void *InsertPos = nullptr; | |||
4064 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( | |||
4065 | Converted, InsertPos)) { | |||
4066 | checkSpecializationVisibility(TemplateNameLoc, Spec); | |||
4067 | // If we already have a variable template specialization, return it. | |||
4068 | return Spec; | |||
4069 | } | |||
4070 | ||||
4071 | // This is the first time we have referenced this variable template | |||
4072 | // specialization. Create the canonical declaration and add it to | |||
4073 | // the set of specializations, based on the closest partial specialization | |||
4074 | // that it represents. That is, | |||
4075 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); | |||
4076 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, | |||
4077 | Converted); | |||
4078 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; | |||
4079 | bool AmbiguousPartialSpec = false; | |||
4080 | typedef PartialSpecMatchResult MatchResult; | |||
4081 | SmallVector<MatchResult, 4> Matched; | |||
4082 | SourceLocation PointOfInstantiation = TemplateNameLoc; | |||
4083 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, | |||
4084 | /*ForTakingAddress=*/false); | |||
4085 | ||||
4086 | // 1. Attempt to find the closest partial specialization that this | |||
4087 | // specializes, if any. | |||
4088 | // If any of the template arguments is dependent, then this is probably | |||
4089 | // a placeholder for an incomplete declarative context; which must be | |||
4090 | // complete by instantiation time. Thus, do not search through the partial | |||
4091 | // specializations yet. | |||
4092 | // TODO: Unify with InstantiateClassTemplateSpecialization()? | |||
4093 | // Perhaps better after unification of DeduceTemplateArguments() and | |||
4094 | // getMoreSpecializedPartialSpecialization(). | |||
4095 | bool InstantiationDependent = false; | |||
4096 | if (!TemplateSpecializationType::anyDependentTemplateArguments( | |||
4097 | TemplateArgs, InstantiationDependent)) { | |||
4098 | ||||
4099 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; | |||
4100 | Template->getPartialSpecializations(PartialSpecs); | |||
4101 | ||||
4102 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { | |||
4103 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; | |||
4104 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); | |||
4105 | ||||
4106 | if (TemplateDeductionResult Result = | |||
4107 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { | |||
4108 | // Store the failed-deduction information for use in diagnostics, later. | |||
4109 | // TODO: Actually use the failed-deduction info? | |||
4110 | FailedCandidates.addCandidate().set( | |||
4111 | DeclAccessPair::make(Template, AS_public), Partial, | |||
4112 | MakeDeductionFailureInfo(Context, Result, Info)); | |||
4113 | (void)Result; | |||
4114 | } else { | |||
4115 | Matched.push_back(PartialSpecMatchResult()); | |||
4116 | Matched.back().Partial = Partial; | |||
4117 | Matched.back().Args = Info.take(); | |||
4118 | } | |||
4119 | } | |||
4120 | ||||
4121 | if (Matched.size() >= 1) { | |||
4122 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); | |||
4123 | if (Matched.size() == 1) { | |||
4124 | // -- If exactly one matching specialization is found, the | |||
4125 | // instantiation is generated from that specialization. | |||
4126 | // We don't need to do anything for this. | |||
4127 | } else { | |||
4128 | // -- If more than one matching specialization is found, the | |||
4129 | // partial order rules (14.5.4.2) are used to determine | |||
4130 | // whether one of the specializations is more specialized | |||
4131 | // than the others. If none of the specializations is more | |||
4132 | // specialized than all of the other matching | |||
4133 | // specializations, then the use of the variable template is | |||
4134 | // ambiguous and the program is ill-formed. | |||
4135 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, | |||
4136 | PEnd = Matched.end(); | |||
4137 | P != PEnd; ++P) { | |||
4138 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, | |||
4139 | PointOfInstantiation) == | |||
4140 | P->Partial) | |||
4141 | Best = P; | |||
4142 | } | |||
4143 | ||||
4144 | // Determine if the best partial specialization is more specialized than | |||
4145 | // the others. | |||
4146 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), | |||
4147 | PEnd = Matched.end(); | |||
4148 | P != PEnd; ++P) { | |||
4149 | if (P != Best && getMoreSpecializedPartialSpecialization( | |||
4150 | P->Partial, Best->Partial, | |||
4151 | PointOfInstantiation) != Best->Partial) { | |||
4152 | AmbiguousPartialSpec = true; | |||
4153 | break; | |||
4154 | } | |||
4155 | } | |||
4156 | } | |||
4157 | ||||
4158 | // Instantiate using the best variable template partial specialization. | |||
4159 | InstantiationPattern = Best->Partial; | |||
4160 | InstantiationArgs = Best->Args; | |||
4161 | } else { | |||
4162 | // -- If no match is found, the instantiation is generated | |||
4163 | // from the primary template. | |||
4164 | // InstantiationPattern = Template->getTemplatedDecl(); | |||
4165 | } | |||
4166 | } | |||
4167 | ||||
4168 | // 2. Create the canonical declaration. | |||
4169 | // Note that we do not instantiate a definition until we see an odr-use | |||
4170 | // in DoMarkVarDeclReferenced(). | |||
4171 | // FIXME: LateAttrs et al.? | |||
4172 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( | |||
4173 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, | |||
4174 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); | |||
4175 | if (!Decl) | |||
4176 | return true; | |||
4177 | ||||
4178 | if (AmbiguousPartialSpec) { | |||
4179 | // Partial ordering did not produce a clear winner. Complain. | |||
4180 | Decl->setInvalidDecl(); | |||
4181 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) | |||
4182 | << Decl; | |||
4183 | ||||
4184 | // Print the matching partial specializations. | |||
4185 | for (MatchResult P : Matched) | |||
4186 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) | |||
4187 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), | |||
4188 | *P.Args); | |||
4189 | return true; | |||
4190 | } | |||
4191 | ||||
4192 | if (VarTemplatePartialSpecializationDecl *D = | |||
4193 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) | |||
4194 | Decl->setInstantiationOf(D, InstantiationArgs); | |||
4195 | ||||
4196 | checkSpecializationVisibility(TemplateNameLoc, Decl); | |||
4197 | ||||
4198 | assert(Decl && "No variable template specialization?")((Decl && "No variable template specialization?") ? static_cast <void> (0) : __assert_fail ("Decl && \"No variable template specialization?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4198, __PRETTY_FUNCTION__)); | |||
4199 | return Decl; | |||
4200 | } | |||
4201 | ||||
4202 | ExprResult | |||
4203 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, | |||
4204 | const DeclarationNameInfo &NameInfo, | |||
4205 | VarTemplateDecl *Template, SourceLocation TemplateLoc, | |||
4206 | const TemplateArgumentListInfo *TemplateArgs) { | |||
4207 | ||||
4208 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), | |||
4209 | *TemplateArgs); | |||
4210 | if (Decl.isInvalid()) | |||
4211 | return ExprError(); | |||
4212 | ||||
4213 | VarDecl *Var = cast<VarDecl>(Decl.get()); | |||
4214 | if (!Var->getTemplateSpecializationKind()) | |||
4215 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, | |||
4216 | NameInfo.getLoc()); | |||
4217 | ||||
4218 | // Build an ordinary singleton decl ref. | |||
4219 | return BuildDeclarationNameExpr(SS, NameInfo, Var, | |||
4220 | /*FoundD=*/nullptr, TemplateArgs); | |||
4221 | } | |||
4222 | ||||
4223 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, | |||
4224 | SourceLocation Loc) { | |||
4225 | Diag(Loc, diag::err_template_missing_args) | |||
4226 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; | |||
4227 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { | |||
4228 | Diag(TD->getLocation(), diag::note_template_decl_here) | |||
4229 | << TD->getTemplateParameters()->getSourceRange(); | |||
4230 | } | |||
4231 | } | |||
4232 | ||||
4233 | ExprResult | |||
4234 | Sema::CheckConceptTemplateId(const CXXScopeSpec &SS, | |||
4235 | SourceLocation TemplateKWLoc, | |||
4236 | SourceLocation ConceptNameLoc, | |||
4237 | NamedDecl *FoundDecl, | |||
4238 | ConceptDecl *NamedConcept, | |||
4239 | const TemplateArgumentListInfo *TemplateArgs) { | |||
4240 | assert(NamedConcept && "A concept template id without a template?")((NamedConcept && "A concept template id without a template?" ) ? static_cast<void> (0) : __assert_fail ("NamedConcept && \"A concept template id without a template?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4240, __PRETTY_FUNCTION__)); | |||
4241 | ||||
4242 | llvm::SmallVector<TemplateArgument, 4> Converted; | |||
4243 | if (CheckTemplateArgumentList(NamedConcept, ConceptNameLoc, | |||
4244 | const_cast<TemplateArgumentListInfo&>(*TemplateArgs), | |||
4245 | /*PartialTemplateArgs=*/false, Converted, | |||
4246 | /*UpdateArgsWithConversion=*/false)) | |||
4247 | return ExprError(); | |||
4248 | ||||
4249 | ConstraintSatisfaction Satisfaction; | |||
4250 | bool AreArgsDependent = false; | |||
4251 | for (TemplateArgument &Arg : Converted) { | |||
4252 | if (Arg.isDependent()) { | |||
4253 | AreArgsDependent = true; | |||
4254 | break; | |||
4255 | } | |||
4256 | } | |||
4257 | if (!AreArgsDependent && | |||
4258 | CheckConstraintSatisfaction(NamedConcept, | |||
4259 | {NamedConcept->getConstraintExpr()}, | |||
4260 | Converted, | |||
4261 | SourceRange(SS.isSet() ? SS.getBeginLoc() : | |||
4262 | ConceptNameLoc, | |||
4263 | TemplateArgs->getRAngleLoc()), | |||
4264 | Satisfaction)) | |||
4265 | return ExprError(); | |||
4266 | ||||
4267 | return ConceptSpecializationExpr::Create(Context, | |||
4268 | SS.isSet() ? SS.getWithLocInContext(Context) : NestedNameSpecifierLoc{}, | |||
4269 | TemplateKWLoc, ConceptNameLoc, FoundDecl, NamedConcept, | |||
4270 | ASTTemplateArgumentListInfo::Create(Context, *TemplateArgs), Converted, | |||
4271 | AreArgsDependent ? nullptr : &Satisfaction); | |||
4272 | } | |||
4273 | ||||
4274 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, | |||
4275 | SourceLocation TemplateKWLoc, | |||
4276 | LookupResult &R, | |||
4277 | bool RequiresADL, | |||
4278 | const TemplateArgumentListInfo *TemplateArgs) { | |||
4279 | // FIXME: Can we do any checking at this point? I guess we could check the | |||
4280 | // template arguments that we have against the template name, if the template | |||
4281 | // name refers to a single template. That's not a terribly common case, | |||
4282 | // though. | |||
4283 | // foo<int> could identify a single function unambiguously | |||
4284 | // This approach does NOT work, since f<int>(1); | |||
4285 | // gets resolved prior to resorting to overload resolution | |||
4286 | // i.e., template<class T> void f(double); | |||
4287 | // vs template<class T, class U> void f(U); | |||
4288 | ||||
4289 | // These should be filtered out by our callers. | |||
4290 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid")((!R.isAmbiguous() && "ambiguous lookup when building templateid" ) ? static_cast<void> (0) : __assert_fail ("!R.isAmbiguous() && \"ambiguous lookup when building templateid\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4290, __PRETTY_FUNCTION__)); | |||
4291 | ||||
4292 | // Non-function templates require a template argument list. | |||
4293 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { | |||
4294 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { | |||
4295 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); | |||
4296 | return ExprError(); | |||
4297 | } | |||
4298 | } | |||
4299 | ||||
4300 | auto AnyDependentArguments = [&]() -> bool { | |||
4301 | bool InstantiationDependent; | |||
4302 | return TemplateArgs && | |||
4303 | TemplateSpecializationType::anyDependentTemplateArguments( | |||
4304 | *TemplateArgs, InstantiationDependent); | |||
4305 | }; | |||
4306 | ||||
4307 | // In C++1y, check variable template ids. | |||
4308 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { | |||
4309 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), | |||
4310 | R.getAsSingle<VarTemplateDecl>(), | |||
4311 | TemplateKWLoc, TemplateArgs); | |||
4312 | } | |||
4313 | ||||
4314 | if (R.getAsSingle<ConceptDecl>()) { | |||
4315 | return CheckConceptTemplateId(SS, TemplateKWLoc, | |||
4316 | R.getLookupNameInfo().getBeginLoc(), | |||
4317 | R.getFoundDecl(), | |||
4318 | R.getAsSingle<ConceptDecl>(), TemplateArgs); | |||
4319 | } | |||
4320 | ||||
4321 | // We don't want lookup warnings at this point. | |||
4322 | R.suppressDiagnostics(); | |||
4323 | ||||
4324 | UnresolvedLookupExpr *ULE | |||
4325 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), | |||
4326 | SS.getWithLocInContext(Context), | |||
4327 | TemplateKWLoc, | |||
4328 | R.getLookupNameInfo(), | |||
4329 | RequiresADL, TemplateArgs, | |||
4330 | R.begin(), R.end()); | |||
4331 | ||||
4332 | return ULE; | |||
4333 | } | |||
4334 | ||||
4335 | // We actually only call this from template instantiation. | |||
4336 | ExprResult | |||
4337 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, | |||
4338 | SourceLocation TemplateKWLoc, | |||
4339 | const DeclarationNameInfo &NameInfo, | |||
4340 | const TemplateArgumentListInfo *TemplateArgs) { | |||
4341 | ||||
4342 | assert(TemplateArgs || TemplateKWLoc.isValid())((TemplateArgs || TemplateKWLoc.isValid()) ? static_cast<void > (0) : __assert_fail ("TemplateArgs || TemplateKWLoc.isValid()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4342, __PRETTY_FUNCTION__)); | |||
4343 | DeclContext *DC; | |||
4344 | if (!(DC = computeDeclContext(SS, false)) || | |||
4345 | DC->isDependentContext() || | |||
4346 | RequireCompleteDeclContext(SS, DC)) | |||
4347 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); | |||
4348 | ||||
4349 | bool MemberOfUnknownSpecialization; | |||
4350 | LookupResult R(*this, NameInfo, LookupOrdinaryName); | |||
4351 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), | |||
4352 | /*Entering*/false, MemberOfUnknownSpecialization, | |||
4353 | TemplateKWLoc)) | |||
4354 | return ExprError(); | |||
4355 | ||||
4356 | if (R.isAmbiguous()) | |||
4357 | return ExprError(); | |||
4358 | ||||
4359 | if (R.empty()) { | |||
4360 | Diag(NameInfo.getLoc(), diag::err_no_member) | |||
4361 | << NameInfo.getName() << DC << SS.getRange(); | |||
4362 | return ExprError(); | |||
4363 | } | |||
4364 | ||||
4365 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { | |||
4366 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) | |||
4367 | << SS.getScopeRep() | |||
4368 | << NameInfo.getName().getAsString() << SS.getRange(); | |||
4369 | Diag(Temp->getLocation(), diag::note_referenced_class_template); | |||
4370 | return ExprError(); | |||
4371 | } | |||
4372 | ||||
4373 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); | |||
4374 | } | |||
4375 | ||||
4376 | /// Form a dependent template name. | |||
4377 | /// | |||
4378 | /// This action forms a dependent template name given the template | |||
4379 | /// name and its (presumably dependent) scope specifier. For | |||
4380 | /// example, given "MetaFun::template apply", the scope specifier \p | |||
4381 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location | |||
4382 | /// of the "template" keyword, and "apply" is the \p Name. | |||
4383 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, | |||
4384 | CXXScopeSpec &SS, | |||
4385 | SourceLocation TemplateKWLoc, | |||
4386 | const UnqualifiedId &Name, | |||
4387 | ParsedType ObjectType, | |||
4388 | bool EnteringContext, | |||
4389 | TemplateTy &Result, | |||
4390 | bool AllowInjectedClassName) { | |||
4391 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) | |||
4392 | Diag(TemplateKWLoc, | |||
4393 | getLangOpts().CPlusPlus11 ? | |||
4394 | diag::warn_cxx98_compat_template_outside_of_template : | |||
4395 | diag::ext_template_outside_of_template) | |||
4396 | << FixItHint::CreateRemoval(TemplateKWLoc); | |||
4397 | ||||
4398 | DeclContext *LookupCtx = nullptr; | |||
4399 | if (SS.isSet()) | |||
4400 | LookupCtx = computeDeclContext(SS, EnteringContext); | |||
4401 | if (!LookupCtx && ObjectType) | |||
4402 | LookupCtx = computeDeclContext(ObjectType.get()); | |||
4403 | if (LookupCtx) { | |||
4404 | // C++0x [temp.names]p5: | |||
4405 | // If a name prefixed by the keyword template is not the name of | |||
4406 | // a template, the program is ill-formed. [Note: the keyword | |||
4407 | // template may not be applied to non-template members of class | |||
4408 | // templates. -end note ] [ Note: as is the case with the | |||
4409 | // typename prefix, the template prefix is allowed in cases | |||
4410 | // where it is not strictly necessary; i.e., when the | |||
4411 | // nested-name-specifier or the expression on the left of the -> | |||
4412 | // or . is not dependent on a template-parameter, or the use | |||
4413 | // does not appear in the scope of a template. -end note] | |||
4414 | // | |||
4415 | // Note: C++03 was more strict here, because it banned the use of | |||
4416 | // the "template" keyword prior to a template-name that was not a | |||
4417 | // dependent name. C++ DR468 relaxed this requirement (the | |||
4418 | // "template" keyword is now permitted). We follow the C++0x | |||
4419 | // rules, even in C++03 mode with a warning, retroactively applying the DR. | |||
4420 | bool MemberOfUnknownSpecialization; | |||
4421 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, | |||
4422 | ObjectType, EnteringContext, Result, | |||
4423 | MemberOfUnknownSpecialization); | |||
4424 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { | |||
4425 | // This is a dependent template. Handle it below. | |||
4426 | } else if (TNK == TNK_Non_template) { | |||
4427 | // Do the lookup again to determine if this is a "nothing found" case or | |||
4428 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't | |||
4429 | // need to do this. | |||
4430 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); | |||
4431 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), | |||
4432 | LookupOrdinaryName); | |||
4433 | bool MOUS; | |||
4434 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, | |||
4435 | MOUS, TemplateKWLoc) && !R.isAmbiguous()) | |||
4436 | Diag(Name.getBeginLoc(), diag::err_no_member) | |||
4437 | << DNI.getName() << LookupCtx << SS.getRange(); | |||
4438 | return TNK_Non_template; | |||
4439 | } else { | |||
4440 | // We found something; return it. | |||
4441 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); | |||
4442 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && | |||
4443 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && | |||
4444 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { | |||
4445 | // C++14 [class.qual]p2: | |||
4446 | // In a lookup in which function names are not ignored and the | |||
4447 | // nested-name-specifier nominates a class C, if the name specified | |||
4448 | // [...] is the injected-class-name of C, [...] the name is instead | |||
4449 | // considered to name the constructor | |||
4450 | // | |||
4451 | // We don't get here if naming the constructor would be valid, so we | |||
4452 | // just reject immediately and recover by treating the | |||
4453 | // injected-class-name as naming the template. | |||
4454 | Diag(Name.getBeginLoc(), | |||
4455 | diag::ext_out_of_line_qualified_id_type_names_constructor) | |||
4456 | << Name.Identifier | |||
4457 | << 0 /*injected-class-name used as template name*/ | |||
4458 | << 1 /*'template' keyword was used*/; | |||
4459 | } | |||
4460 | return TNK; | |||
4461 | } | |||
4462 | } | |||
4463 | ||||
4464 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); | |||
4465 | ||||
4466 | switch (Name.getKind()) { | |||
4467 | case UnqualifiedIdKind::IK_Identifier: | |||
4468 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, | |||
4469 | Name.Identifier)); | |||
4470 | return TNK_Dependent_template_name; | |||
4471 | ||||
4472 | case UnqualifiedIdKind::IK_OperatorFunctionId: | |||
4473 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, | |||
4474 | Name.OperatorFunctionId.Operator)); | |||
4475 | return TNK_Function_template; | |||
4476 | ||||
4477 | case UnqualifiedIdKind::IK_LiteralOperatorId: | |||
4478 | llvm_unreachable("literal operator id cannot have a dependent scope")::llvm::llvm_unreachable_internal("literal operator id cannot have a dependent scope" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4478); | |||
4479 | ||||
4480 | default: | |||
4481 | break; | |||
4482 | } | |||
4483 | ||||
4484 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) | |||
4485 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() | |||
4486 | << TemplateKWLoc; | |||
4487 | return TNK_Non_template; | |||
4488 | } | |||
4489 | ||||
4490 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, | |||
4491 | TemplateArgumentLoc &AL, | |||
4492 | SmallVectorImpl<TemplateArgument> &Converted) { | |||
4493 | const TemplateArgument &Arg = AL.getArgument(); | |||
4494 | QualType ArgType; | |||
4495 | TypeSourceInfo *TSI = nullptr; | |||
4496 | ||||
4497 | // Check template type parameter. | |||
4498 | switch(Arg.getKind()) { | |||
4499 | case TemplateArgument::Type: | |||
4500 | // C++ [temp.arg.type]p1: | |||
4501 | // A template-argument for a template-parameter which is a | |||
4502 | // type shall be a type-id. | |||
4503 | ArgType = Arg.getAsType(); | |||
4504 | TSI = AL.getTypeSourceInfo(); | |||
4505 | break; | |||
4506 | case TemplateArgument::Template: | |||
4507 | case TemplateArgument::TemplateExpansion: { | |||
4508 | // We have a template type parameter but the template argument | |||
4509 | // is a template without any arguments. | |||
4510 | SourceRange SR = AL.getSourceRange(); | |||
4511 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); | |||
4512 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); | |||
4513 | return true; | |||
4514 | } | |||
4515 | case TemplateArgument::Expression: { | |||
4516 | // We have a template type parameter but the template argument is an | |||
4517 | // expression; see if maybe it is missing the "typename" keyword. | |||
4518 | CXXScopeSpec SS; | |||
4519 | DeclarationNameInfo NameInfo; | |||
4520 | ||||
4521 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { | |||
4522 | SS.Adopt(ArgExpr->getQualifierLoc()); | |||
4523 | NameInfo = ArgExpr->getNameInfo(); | |||
4524 | } else if (DependentScopeDeclRefExpr *ArgExpr = | |||
4525 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { | |||
4526 | SS.Adopt(ArgExpr->getQualifierLoc()); | |||
4527 | NameInfo = ArgExpr->getNameInfo(); | |||
4528 | } else if (CXXDependentScopeMemberExpr *ArgExpr = | |||
4529 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { | |||
4530 | if (ArgExpr->isImplicitAccess()) { | |||
4531 | SS.Adopt(ArgExpr->getQualifierLoc()); | |||
4532 | NameInfo = ArgExpr->getMemberNameInfo(); | |||
4533 | } | |||
4534 | } | |||
4535 | ||||
4536 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { | |||
4537 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); | |||
4538 | LookupParsedName(Result, CurScope, &SS); | |||
4539 | ||||
4540 | if (Result.getAsSingle<TypeDecl>() || | |||
4541 | Result.getResultKind() == | |||
4542 | LookupResult::NotFoundInCurrentInstantiation) { | |||
4543 | // Suggest that the user add 'typename' before the NNS. | |||
4544 | SourceLocation Loc = AL.getSourceRange().getBegin(); | |||
4545 | Diag(Loc, getLangOpts().MSVCCompat | |||
4546 | ? diag::ext_ms_template_type_arg_missing_typename | |||
4547 | : diag::err_template_arg_must_be_type_suggest) | |||
4548 | << FixItHint::CreateInsertion(Loc, "typename "); | |||
4549 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
4550 | ||||
4551 | // Recover by synthesizing a type using the location information that we | |||
4552 | // already have. | |||
4553 | ArgType = | |||
4554 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); | |||
4555 | TypeLocBuilder TLB; | |||
4556 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); | |||
4557 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); | |||
4558 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
4559 | TL.setNameLoc(NameInfo.getLoc()); | |||
4560 | TSI = TLB.getTypeSourceInfo(Context, ArgType); | |||
4561 | ||||
4562 | // Overwrite our input TemplateArgumentLoc so that we can recover | |||
4563 | // properly. | |||
4564 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), | |||
4565 | TemplateArgumentLocInfo(TSI)); | |||
4566 | ||||
4567 | break; | |||
4568 | } | |||
4569 | } | |||
4570 | // fallthrough | |||
4571 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
4572 | } | |||
4573 | default: { | |||
4574 | // We have a template type parameter but the template argument | |||
4575 | // is not a type. | |||
4576 | SourceRange SR = AL.getSourceRange(); | |||
4577 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; | |||
4578 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
4579 | ||||
4580 | return true; | |||
4581 | } | |||
4582 | } | |||
4583 | ||||
4584 | if (CheckTemplateArgument(Param, TSI)) | |||
4585 | return true; | |||
4586 | ||||
4587 | // Add the converted template type argument. | |||
4588 | ArgType = Context.getCanonicalType(ArgType); | |||
4589 | ||||
4590 | // Objective-C ARC: | |||
4591 | // If an explicitly-specified template argument type is a lifetime type | |||
4592 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. | |||
4593 | if (getLangOpts().ObjCAutoRefCount && | |||
4594 | ArgType->isObjCLifetimeType() && | |||
4595 | !ArgType.getObjCLifetime()) { | |||
4596 | Qualifiers Qs; | |||
4597 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); | |||
4598 | ArgType = Context.getQualifiedType(ArgType, Qs); | |||
4599 | } | |||
4600 | ||||
4601 | Converted.push_back(TemplateArgument(ArgType)); | |||
4602 | return false; | |||
4603 | } | |||
4604 | ||||
4605 | /// Substitute template arguments into the default template argument for | |||
4606 | /// the given template type parameter. | |||
4607 | /// | |||
4608 | /// \param SemaRef the semantic analysis object for which we are performing | |||
4609 | /// the substitution. | |||
4610 | /// | |||
4611 | /// \param Template the template that we are synthesizing template arguments | |||
4612 | /// for. | |||
4613 | /// | |||
4614 | /// \param TemplateLoc the location of the template name that started the | |||
4615 | /// template-id we are checking. | |||
4616 | /// | |||
4617 | /// \param RAngleLoc the location of the right angle bracket ('>') that | |||
4618 | /// terminates the template-id. | |||
4619 | /// | |||
4620 | /// \param Param the template template parameter whose default we are | |||
4621 | /// substituting into. | |||
4622 | /// | |||
4623 | /// \param Converted the list of template arguments provided for template | |||
4624 | /// parameters that precede \p Param in the template parameter list. | |||
4625 | /// \returns the substituted template argument, or NULL if an error occurred. | |||
4626 | static TypeSourceInfo * | |||
4627 | SubstDefaultTemplateArgument(Sema &SemaRef, | |||
4628 | TemplateDecl *Template, | |||
4629 | SourceLocation TemplateLoc, | |||
4630 | SourceLocation RAngleLoc, | |||
4631 | TemplateTypeParmDecl *Param, | |||
4632 | SmallVectorImpl<TemplateArgument> &Converted) { | |||
4633 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); | |||
4634 | ||||
4635 | // If the argument type is dependent, instantiate it now based | |||
4636 | // on the previously-computed template arguments. | |||
4637 | if (ArgType->getType()->isInstantiationDependentType()) { | |||
4638 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, | |||
4639 | Param, Template, Converted, | |||
4640 | SourceRange(TemplateLoc, RAngleLoc)); | |||
4641 | if (Inst.isInvalid()) | |||
4642 | return nullptr; | |||
4643 | ||||
4644 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); | |||
4645 | ||||
4646 | // Only substitute for the innermost template argument list. | |||
4647 | MultiLevelTemplateArgumentList TemplateArgLists; | |||
4648 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); | |||
4649 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) | |||
4650 | TemplateArgLists.addOuterTemplateArguments(None); | |||
4651 | ||||
4652 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | |||
4653 | ArgType = | |||
4654 | SemaRef.SubstType(ArgType, TemplateArgLists, | |||
4655 | Param->getDefaultArgumentLoc(), Param->getDeclName()); | |||
4656 | } | |||
4657 | ||||
4658 | return ArgType; | |||
4659 | } | |||
4660 | ||||
4661 | /// Substitute template arguments into the default template argument for | |||
4662 | /// the given non-type template parameter. | |||
4663 | /// | |||
4664 | /// \param SemaRef the semantic analysis object for which we are performing | |||
4665 | /// the substitution. | |||
4666 | /// | |||
4667 | /// \param Template the template that we are synthesizing template arguments | |||
4668 | /// for. | |||
4669 | /// | |||
4670 | /// \param TemplateLoc the location of the template name that started the | |||
4671 | /// template-id we are checking. | |||
4672 | /// | |||
4673 | /// \param RAngleLoc the location of the right angle bracket ('>') that | |||
4674 | /// terminates the template-id. | |||
4675 | /// | |||
4676 | /// \param Param the non-type template parameter whose default we are | |||
4677 | /// substituting into. | |||
4678 | /// | |||
4679 | /// \param Converted the list of template arguments provided for template | |||
4680 | /// parameters that precede \p Param in the template parameter list. | |||
4681 | /// | |||
4682 | /// \returns the substituted template argument, or NULL if an error occurred. | |||
4683 | static ExprResult | |||
4684 | SubstDefaultTemplateArgument(Sema &SemaRef, | |||
4685 | TemplateDecl *Template, | |||
4686 | SourceLocation TemplateLoc, | |||
4687 | SourceLocation RAngleLoc, | |||
4688 | NonTypeTemplateParmDecl *Param, | |||
4689 | SmallVectorImpl<TemplateArgument> &Converted) { | |||
4690 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, | |||
4691 | Param, Template, Converted, | |||
4692 | SourceRange(TemplateLoc, RAngleLoc)); | |||
4693 | if (Inst.isInvalid()) | |||
4694 | return ExprError(); | |||
4695 | ||||
4696 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); | |||
4697 | ||||
4698 | // Only substitute for the innermost template argument list. | |||
4699 | MultiLevelTemplateArgumentList TemplateArgLists; | |||
4700 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); | |||
4701 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) | |||
4702 | TemplateArgLists.addOuterTemplateArguments(None); | |||
4703 | ||||
4704 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | |||
4705 | EnterExpressionEvaluationContext ConstantEvaluated( | |||
4706 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | |||
4707 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); | |||
4708 | } | |||
4709 | ||||
4710 | /// Substitute template arguments into the default template argument for | |||
4711 | /// the given template template parameter. | |||
4712 | /// | |||
4713 | /// \param SemaRef the semantic analysis object for which we are performing | |||
4714 | /// the substitution. | |||
4715 | /// | |||
4716 | /// \param Template the template that we are synthesizing template arguments | |||
4717 | /// for. | |||
4718 | /// | |||
4719 | /// \param TemplateLoc the location of the template name that started the | |||
4720 | /// template-id we are checking. | |||
4721 | /// | |||
4722 | /// \param RAngleLoc the location of the right angle bracket ('>') that | |||
4723 | /// terminates the template-id. | |||
4724 | /// | |||
4725 | /// \param Param the template template parameter whose default we are | |||
4726 | /// substituting into. | |||
4727 | /// | |||
4728 | /// \param Converted the list of template arguments provided for template | |||
4729 | /// parameters that precede \p Param in the template parameter list. | |||
4730 | /// | |||
4731 | /// \param QualifierLoc Will be set to the nested-name-specifier (with | |||
4732 | /// source-location information) that precedes the template name. | |||
4733 | /// | |||
4734 | /// \returns the substituted template argument, or NULL if an error occurred. | |||
4735 | static TemplateName | |||
4736 | SubstDefaultTemplateArgument(Sema &SemaRef, | |||
4737 | TemplateDecl *Template, | |||
4738 | SourceLocation TemplateLoc, | |||
4739 | SourceLocation RAngleLoc, | |||
4740 | TemplateTemplateParmDecl *Param, | |||
4741 | SmallVectorImpl<TemplateArgument> &Converted, | |||
4742 | NestedNameSpecifierLoc &QualifierLoc) { | |||
4743 | Sema::InstantiatingTemplate Inst( | |||
4744 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, | |||
4745 | SourceRange(TemplateLoc, RAngleLoc)); | |||
4746 | if (Inst.isInvalid()) | |||
4747 | return TemplateName(); | |||
4748 | ||||
4749 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); | |||
4750 | ||||
4751 | // Only substitute for the innermost template argument list. | |||
4752 | MultiLevelTemplateArgumentList TemplateArgLists; | |||
4753 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); | |||
4754 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) | |||
4755 | TemplateArgLists.addOuterTemplateArguments(None); | |||
4756 | ||||
4757 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | |||
4758 | // Substitute into the nested-name-specifier first, | |||
4759 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); | |||
4760 | if (QualifierLoc) { | |||
4761 | QualifierLoc = | |||
4762 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); | |||
4763 | if (!QualifierLoc) | |||
4764 | return TemplateName(); | |||
4765 | } | |||
4766 | ||||
4767 | return SemaRef.SubstTemplateName( | |||
4768 | QualifierLoc, | |||
4769 | Param->getDefaultArgument().getArgument().getAsTemplate(), | |||
4770 | Param->getDefaultArgument().getTemplateNameLoc(), | |||
4771 | TemplateArgLists); | |||
4772 | } | |||
4773 | ||||
4774 | /// If the given template parameter has a default template | |||
4775 | /// argument, substitute into that default template argument and | |||
4776 | /// return the corresponding template argument. | |||
4777 | TemplateArgumentLoc | |||
4778 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, | |||
4779 | SourceLocation TemplateLoc, | |||
4780 | SourceLocation RAngleLoc, | |||
4781 | Decl *Param, | |||
4782 | SmallVectorImpl<TemplateArgument> | |||
4783 | &Converted, | |||
4784 | bool &HasDefaultArg) { | |||
4785 | HasDefaultArg = false; | |||
4786 | ||||
4787 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { | |||
4788 | if (!hasVisibleDefaultArgument(TypeParm)) | |||
4789 | return TemplateArgumentLoc(); | |||
4790 | ||||
4791 | HasDefaultArg = true; | |||
4792 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, | |||
4793 | TemplateLoc, | |||
4794 | RAngleLoc, | |||
4795 | TypeParm, | |||
4796 | Converted); | |||
4797 | if (DI) | |||
4798 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); | |||
4799 | ||||
4800 | return TemplateArgumentLoc(); | |||
4801 | } | |||
4802 | ||||
4803 | if (NonTypeTemplateParmDecl *NonTypeParm | |||
4804 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | |||
4805 | if (!hasVisibleDefaultArgument(NonTypeParm)) | |||
4806 | return TemplateArgumentLoc(); | |||
4807 | ||||
4808 | HasDefaultArg = true; | |||
4809 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, | |||
4810 | TemplateLoc, | |||
4811 | RAngleLoc, | |||
4812 | NonTypeParm, | |||
4813 | Converted); | |||
4814 | if (Arg.isInvalid()) | |||
4815 | return TemplateArgumentLoc(); | |||
4816 | ||||
4817 | Expr *ArgE = Arg.getAs<Expr>(); | |||
4818 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); | |||
4819 | } | |||
4820 | ||||
4821 | TemplateTemplateParmDecl *TempTempParm | |||
4822 | = cast<TemplateTemplateParmDecl>(Param); | |||
4823 | if (!hasVisibleDefaultArgument(TempTempParm)) | |||
4824 | return TemplateArgumentLoc(); | |||
4825 | ||||
4826 | HasDefaultArg = true; | |||
4827 | NestedNameSpecifierLoc QualifierLoc; | |||
4828 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, | |||
4829 | TemplateLoc, | |||
4830 | RAngleLoc, | |||
4831 | TempTempParm, | |||
4832 | Converted, | |||
4833 | QualifierLoc); | |||
4834 | if (TName.isNull()) | |||
4835 | return TemplateArgumentLoc(); | |||
4836 | ||||
4837 | return TemplateArgumentLoc(TemplateArgument(TName), | |||
4838 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), | |||
4839 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); | |||
4840 | } | |||
4841 | ||||
4842 | /// Convert a template-argument that we parsed as a type into a template, if | |||
4843 | /// possible. C++ permits injected-class-names to perform dual service as | |||
4844 | /// template template arguments and as template type arguments. | |||
4845 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { | |||
4846 | // Extract and step over any surrounding nested-name-specifier. | |||
4847 | NestedNameSpecifierLoc QualLoc; | |||
4848 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { | |||
4849 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) | |||
4850 | return TemplateArgumentLoc(); | |||
4851 | ||||
4852 | QualLoc = ETLoc.getQualifierLoc(); | |||
4853 | TLoc = ETLoc.getNamedTypeLoc(); | |||
4854 | } | |||
4855 | ||||
4856 | // If this type was written as an injected-class-name, it can be used as a | |||
4857 | // template template argument. | |||
4858 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) | |||
4859 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), | |||
4860 | QualLoc, InjLoc.getNameLoc()); | |||
4861 | ||||
4862 | // If this type was written as an injected-class-name, it may have been | |||
4863 | // converted to a RecordType during instantiation. If the RecordType is | |||
4864 | // *not* wrapped in a TemplateSpecializationType and denotes a class | |||
4865 | // template specialization, it must have come from an injected-class-name. | |||
4866 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) | |||
4867 | if (auto *CTSD = | |||
4868 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) | |||
4869 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), | |||
4870 | QualLoc, RecLoc.getNameLoc()); | |||
4871 | ||||
4872 | return TemplateArgumentLoc(); | |||
4873 | } | |||
4874 | ||||
4875 | /// Check that the given template argument corresponds to the given | |||
4876 | /// template parameter. | |||
4877 | /// | |||
4878 | /// \param Param The template parameter against which the argument will be | |||
4879 | /// checked. | |||
4880 | /// | |||
4881 | /// \param Arg The template argument, which may be updated due to conversions. | |||
4882 | /// | |||
4883 | /// \param Template The template in which the template argument resides. | |||
4884 | /// | |||
4885 | /// \param TemplateLoc The location of the template name for the template | |||
4886 | /// whose argument list we're matching. | |||
4887 | /// | |||
4888 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes | |||
4889 | /// the template argument list. | |||
4890 | /// | |||
4891 | /// \param ArgumentPackIndex The index into the argument pack where this | |||
4892 | /// argument will be placed. Only valid if the parameter is a parameter pack. | |||
4893 | /// | |||
4894 | /// \param Converted The checked, converted argument will be added to the | |||
4895 | /// end of this small vector. | |||
4896 | /// | |||
4897 | /// \param CTAK Describes how we arrived at this particular template argument: | |||
4898 | /// explicitly written, deduced, etc. | |||
4899 | /// | |||
4900 | /// \returns true on error, false otherwise. | |||
4901 | bool Sema::CheckTemplateArgument(NamedDecl *Param, | |||
4902 | TemplateArgumentLoc &Arg, | |||
4903 | NamedDecl *Template, | |||
4904 | SourceLocation TemplateLoc, | |||
4905 | SourceLocation RAngleLoc, | |||
4906 | unsigned ArgumentPackIndex, | |||
4907 | SmallVectorImpl<TemplateArgument> &Converted, | |||
4908 | CheckTemplateArgumentKind CTAK) { | |||
4909 | // Check template type parameters. | |||
4910 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) | |||
4911 | return CheckTemplateTypeArgument(TTP, Arg, Converted); | |||
4912 | ||||
4913 | // Check non-type template parameters. | |||
4914 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { | |||
4915 | // Do substitution on the type of the non-type template parameter | |||
4916 | // with the template arguments we've seen thus far. But if the | |||
4917 | // template has a dependent context then we cannot substitute yet. | |||
4918 | QualType NTTPType = NTTP->getType(); | |||
4919 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) | |||
4920 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); | |||
4921 | ||||
4922 | if (NTTPType->isInstantiationDependentType() && | |||
4923 | !isa<TemplateTemplateParmDecl>(Template) && | |||
4924 | !Template->getDeclContext()->isDependentContext()) { | |||
4925 | // Do substitution on the type of the non-type template parameter. | |||
4926 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, | |||
4927 | NTTP, Converted, | |||
4928 | SourceRange(TemplateLoc, RAngleLoc)); | |||
4929 | if (Inst.isInvalid()) | |||
4930 | return true; | |||
4931 | ||||
4932 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | |||
4933 | Converted); | |||
4934 | ||||
4935 | // If the parameter is a pack expansion, expand this slice of the pack. | |||
4936 | if (auto *PET = NTTPType->getAs<PackExpansionType>()) { | |||
4937 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, | |||
4938 | ArgumentPackIndex); | |||
4939 | NTTPType = SubstType(PET->getPattern(), | |||
4940 | MultiLevelTemplateArgumentList(TemplateArgs), | |||
4941 | NTTP->getLocation(), | |||
4942 | NTTP->getDeclName()); | |||
4943 | } else { | |||
4944 | NTTPType = SubstType(NTTPType, | |||
4945 | MultiLevelTemplateArgumentList(TemplateArgs), | |||
4946 | NTTP->getLocation(), | |||
4947 | NTTP->getDeclName()); | |||
4948 | } | |||
4949 | ||||
4950 | // If that worked, check the non-type template parameter type | |||
4951 | // for validity. | |||
4952 | if (!NTTPType.isNull()) | |||
4953 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, | |||
4954 | NTTP->getLocation()); | |||
4955 | if (NTTPType.isNull()) | |||
4956 | return true; | |||
4957 | } | |||
4958 | ||||
4959 | switch (Arg.getArgument().getKind()) { | |||
4960 | case TemplateArgument::Null: | |||
4961 | llvm_unreachable("Should never see a NULL template argument here")::llvm::llvm_unreachable_internal("Should never see a NULL template argument here" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 4961); | |||
4962 | ||||
4963 | case TemplateArgument::Expression: { | |||
4964 | TemplateArgument Result; | |||
4965 | unsigned CurSFINAEErrors = NumSFINAEErrors; | |||
4966 | ExprResult Res = | |||
4967 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), | |||
4968 | Result, CTAK); | |||
4969 | if (Res.isInvalid()) | |||
4970 | return true; | |||
4971 | // If the current template argument causes an error, give up now. | |||
4972 | if (CurSFINAEErrors < NumSFINAEErrors) | |||
4973 | return true; | |||
4974 | ||||
4975 | // If the resulting expression is new, then use it in place of the | |||
4976 | // old expression in the template argument. | |||
4977 | if (Res.get() != Arg.getArgument().getAsExpr()) { | |||
4978 | TemplateArgument TA(Res.get()); | |||
4979 | Arg = TemplateArgumentLoc(TA, Res.get()); | |||
4980 | } | |||
4981 | ||||
4982 | Converted.push_back(Result); | |||
4983 | break; | |||
4984 | } | |||
4985 | ||||
4986 | case TemplateArgument::Declaration: | |||
4987 | case TemplateArgument::Integral: | |||
4988 | case TemplateArgument::NullPtr: | |||
4989 | // We've already checked this template argument, so just copy | |||
4990 | // it to the list of converted arguments. | |||
4991 | Converted.push_back(Arg.getArgument()); | |||
4992 | break; | |||
4993 | ||||
4994 | case TemplateArgument::Template: | |||
4995 | case TemplateArgument::TemplateExpansion: | |||
4996 | // We were given a template template argument. It may not be ill-formed; | |||
4997 | // see below. | |||
4998 | if (DependentTemplateName *DTN | |||
4999 | = Arg.getArgument().getAsTemplateOrTemplatePattern() | |||
5000 | .getAsDependentTemplateName()) { | |||
5001 | // We have a template argument such as \c T::template X, which we | |||
5002 | // parsed as a template template argument. However, since we now | |||
5003 | // know that we need a non-type template argument, convert this | |||
5004 | // template name into an expression. | |||
5005 | ||||
5006 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), | |||
5007 | Arg.getTemplateNameLoc()); | |||
5008 | ||||
5009 | CXXScopeSpec SS; | |||
5010 | SS.Adopt(Arg.getTemplateQualifierLoc()); | |||
5011 | // FIXME: the template-template arg was a DependentTemplateName, | |||
5012 | // so it was provided with a template keyword. However, its source | |||
5013 | // location is not stored in the template argument structure. | |||
5014 | SourceLocation TemplateKWLoc; | |||
5015 | ExprResult E = DependentScopeDeclRefExpr::Create( | |||
5016 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, | |||
5017 | nullptr); | |||
5018 | ||||
5019 | // If we parsed the template argument as a pack expansion, create a | |||
5020 | // pack expansion expression. | |||
5021 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ | |||
5022 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); | |||
5023 | if (E.isInvalid()) | |||
5024 | return true; | |||
5025 | } | |||
5026 | ||||
5027 | TemplateArgument Result; | |||
5028 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); | |||
5029 | if (E.isInvalid()) | |||
5030 | return true; | |||
5031 | ||||
5032 | Converted.push_back(Result); | |||
5033 | break; | |||
5034 | } | |||
5035 | ||||
5036 | // We have a template argument that actually does refer to a class | |||
5037 | // template, alias template, or template template parameter, and | |||
5038 | // therefore cannot be a non-type template argument. | |||
5039 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) | |||
5040 | << Arg.getSourceRange(); | |||
5041 | ||||
5042 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
5043 | return true; | |||
5044 | ||||
5045 | case TemplateArgument::Type: { | |||
5046 | // We have a non-type template parameter but the template | |||
5047 | // argument is a type. | |||
5048 | ||||
5049 | // C++ [temp.arg]p2: | |||
5050 | // In a template-argument, an ambiguity between a type-id and | |||
5051 | // an expression is resolved to a type-id, regardless of the | |||
5052 | // form of the corresponding template-parameter. | |||
5053 | // | |||
5054 | // We warn specifically about this case, since it can be rather | |||
5055 | // confusing for users. | |||
5056 | QualType T = Arg.getArgument().getAsType(); | |||
5057 | SourceRange SR = Arg.getSourceRange(); | |||
5058 | if (T->isFunctionType()) | |||
5059 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; | |||
5060 | else | |||
5061 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; | |||
5062 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
5063 | return true; | |||
5064 | } | |||
5065 | ||||
5066 | case TemplateArgument::Pack: | |||
5067 | llvm_unreachable("Caller must expand template argument packs")::llvm::llvm_unreachable_internal("Caller must expand template argument packs" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5067); | |||
5068 | } | |||
5069 | ||||
5070 | return false; | |||
5071 | } | |||
5072 | ||||
5073 | ||||
5074 | // Check template template parameters. | |||
5075 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); | |||
5076 | ||||
5077 | TemplateParameterList *Params = TempParm->getTemplateParameters(); | |||
5078 | if (TempParm->isExpandedParameterPack()) | |||
5079 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); | |||
5080 | ||||
5081 | // Substitute into the template parameter list of the template | |||
5082 | // template parameter, since previously-supplied template arguments | |||
5083 | // may appear within the template template parameter. | |||
5084 | // | |||
5085 | // FIXME: Skip this if the parameters aren't instantiation-dependent. | |||
5086 | { | |||
5087 | // Set up a template instantiation context. | |||
5088 | LocalInstantiationScope Scope(*this); | |||
5089 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, | |||
5090 | TempParm, Converted, | |||
5091 | SourceRange(TemplateLoc, RAngleLoc)); | |||
5092 | if (Inst.isInvalid()) | |||
5093 | return true; | |||
5094 | ||||
5095 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); | |||
5096 | Params = SubstTemplateParams(Params, CurContext, | |||
5097 | MultiLevelTemplateArgumentList(TemplateArgs)); | |||
5098 | if (!Params) | |||
5099 | return true; | |||
5100 | } | |||
5101 | ||||
5102 | // C++1z [temp.local]p1: (DR1004) | |||
5103 | // When [the injected-class-name] is used [...] as a template-argument for | |||
5104 | // a template template-parameter [...] it refers to the class template | |||
5105 | // itself. | |||
5106 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { | |||
5107 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( | |||
5108 | Arg.getTypeSourceInfo()->getTypeLoc()); | |||
5109 | if (!ConvertedArg.getArgument().isNull()) | |||
5110 | Arg = ConvertedArg; | |||
5111 | } | |||
5112 | ||||
5113 | switch (Arg.getArgument().getKind()) { | |||
5114 | case TemplateArgument::Null: | |||
5115 | llvm_unreachable("Should never see a NULL template argument here")::llvm::llvm_unreachable_internal("Should never see a NULL template argument here" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5115); | |||
5116 | ||||
5117 | case TemplateArgument::Template: | |||
5118 | case TemplateArgument::TemplateExpansion: | |||
5119 | if (CheckTemplateTemplateArgument(Params, Arg)) | |||
5120 | return true; | |||
5121 | ||||
5122 | Converted.push_back(Arg.getArgument()); | |||
5123 | break; | |||
5124 | ||||
5125 | case TemplateArgument::Expression: | |||
5126 | case TemplateArgument::Type: | |||
5127 | // We have a template template parameter but the template | |||
5128 | // argument does not refer to a template. | |||
5129 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) | |||
5130 | << getLangOpts().CPlusPlus11; | |||
5131 | return true; | |||
5132 | ||||
5133 | case TemplateArgument::Declaration: | |||
5134 | llvm_unreachable("Declaration argument with template template parameter")::llvm::llvm_unreachable_internal("Declaration argument with template template parameter" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5134); | |||
5135 | case TemplateArgument::Integral: | |||
5136 | llvm_unreachable("Integral argument with template template parameter")::llvm::llvm_unreachable_internal("Integral argument with template template parameter" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5136); | |||
5137 | case TemplateArgument::NullPtr: | |||
5138 | llvm_unreachable("Null pointer argument with template template parameter")::llvm::llvm_unreachable_internal("Null pointer argument with template template parameter" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5138); | |||
5139 | ||||
5140 | case TemplateArgument::Pack: | |||
5141 | llvm_unreachable("Caller must expand template argument packs")::llvm::llvm_unreachable_internal("Caller must expand template argument packs" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5141); | |||
5142 | } | |||
5143 | ||||
5144 | return false; | |||
5145 | } | |||
5146 | ||||
5147 | /// Check whether the template parameter is a pack expansion, and if so, | |||
5148 | /// determine the number of parameters produced by that expansion. For instance: | |||
5149 | /// | |||
5150 | /// \code | |||
5151 | /// template<typename ...Ts> struct A { | |||
5152 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; | |||
5153 | /// }; | |||
5154 | /// \endcode | |||
5155 | /// | |||
5156 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us | |||
5157 | /// is not a pack expansion, so returns an empty Optional. | |||
5158 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { | |||
5159 | if (NonTypeTemplateParmDecl *NTTP | |||
5160 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | |||
5161 | if (NTTP->isExpandedParameterPack()) | |||
5162 | return NTTP->getNumExpansionTypes(); | |||
5163 | } | |||
5164 | ||||
5165 | if (TemplateTemplateParmDecl *TTP | |||
5166 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { | |||
5167 | if (TTP->isExpandedParameterPack()) | |||
5168 | return TTP->getNumExpansionTemplateParameters(); | |||
5169 | } | |||
5170 | ||||
5171 | return None; | |||
5172 | } | |||
5173 | ||||
5174 | /// Diagnose a missing template argument. | |||
5175 | template<typename TemplateParmDecl> | |||
5176 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, | |||
5177 | TemplateDecl *TD, | |||
5178 | const TemplateParmDecl *D, | |||
5179 | TemplateArgumentListInfo &Args) { | |||
5180 | // Dig out the most recent declaration of the template parameter; there may be | |||
5181 | // declarations of the template that are more recent than TD. | |||
5182 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) | |||
5183 | ->getTemplateParameters() | |||
5184 | ->getParam(D->getIndex())); | |||
5185 | ||||
5186 | // If there's a default argument that's not visible, diagnose that we're | |||
5187 | // missing a module import. | |||
5188 | llvm::SmallVector<Module*, 8> Modules; | |||
5189 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { | |||
5190 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), | |||
5191 | D->getDefaultArgumentLoc(), Modules, | |||
5192 | Sema::MissingImportKind::DefaultArgument, | |||
5193 | /*Recover*/true); | |||
5194 | return true; | |||
5195 | } | |||
5196 | ||||
5197 | // FIXME: If there's a more recent default argument that *is* visible, | |||
5198 | // diagnose that it was declared too late. | |||
5199 | ||||
5200 | TemplateParameterList *Params = TD->getTemplateParameters(); | |||
5201 | ||||
5202 | S.Diag(Loc, diag::err_template_arg_list_different_arity) | |||
5203 | << /*not enough args*/0 | |||
5204 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) | |||
5205 | << TD; | |||
5206 | S.Diag(TD->getLocation(), diag::note_template_decl_here) | |||
5207 | << Params->getSourceRange(); | |||
5208 | return true; | |||
5209 | } | |||
5210 | ||||
5211 | /// Check that the given template argument list is well-formed | |||
5212 | /// for specializing the given template. | |||
5213 | bool Sema::CheckTemplateArgumentList( | |||
5214 | TemplateDecl *Template, SourceLocation TemplateLoc, | |||
5215 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, | |||
5216 | SmallVectorImpl<TemplateArgument> &Converted, | |||
5217 | bool UpdateArgsWithConversions, bool *ConstraintsNotSatisfied) { | |||
5218 | ||||
5219 | if (ConstraintsNotSatisfied) | |||
5220 | *ConstraintsNotSatisfied = false; | |||
5221 | ||||
5222 | // Make a copy of the template arguments for processing. Only make the | |||
5223 | // changes at the end when successful in matching the arguments to the | |||
5224 | // template. | |||
5225 | TemplateArgumentListInfo NewArgs = TemplateArgs; | |||
5226 | ||||
5227 | // Make sure we get the template parameter list from the most | |||
5228 | // recentdeclaration, since that is the only one that has is guaranteed to | |||
5229 | // have all the default template argument information. | |||
5230 | TemplateParameterList *Params = | |||
5231 | cast<TemplateDecl>(Template->getMostRecentDecl()) | |||
5232 | ->getTemplateParameters(); | |||
5233 | ||||
5234 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); | |||
5235 | ||||
5236 | // C++ [temp.arg]p1: | |||
5237 | // [...] The type and form of each template-argument specified in | |||
5238 | // a template-id shall match the type and form specified for the | |||
5239 | // corresponding parameter declared by the template in its | |||
5240 | // template-parameter-list. | |||
5241 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); | |||
5242 | SmallVector<TemplateArgument, 2> ArgumentPack; | |||
5243 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); | |||
5244 | LocalInstantiationScope InstScope(*this, true); | |||
5245 | for (TemplateParameterList::iterator Param = Params->begin(), | |||
5246 | ParamEnd = Params->end(); | |||
5247 | Param != ParamEnd; /* increment in loop */) { | |||
5248 | // If we have an expanded parameter pack, make sure we don't have too | |||
5249 | // many arguments. | |||
5250 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { | |||
5251 | if (*Expansions == ArgumentPack.size()) { | |||
5252 | // We're done with this parameter pack. Pack up its arguments and add | |||
5253 | // them to the list. | |||
5254 | Converted.push_back( | |||
5255 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); | |||
5256 | ArgumentPack.clear(); | |||
5257 | ||||
5258 | // This argument is assigned to the next parameter. | |||
5259 | ++Param; | |||
5260 | continue; | |||
5261 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { | |||
5262 | // Not enough arguments for this parameter pack. | |||
5263 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) | |||
5264 | << /*not enough args*/0 | |||
5265 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) | |||
5266 | << Template; | |||
5267 | Diag(Template->getLocation(), diag::note_template_decl_here) | |||
5268 | << Params->getSourceRange(); | |||
5269 | return true; | |||
5270 | } | |||
5271 | } | |||
5272 | ||||
5273 | if (ArgIdx < NumArgs) { | |||
5274 | // Check the template argument we were given. | |||
5275 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, | |||
5276 | TemplateLoc, RAngleLoc, | |||
5277 | ArgumentPack.size(), Converted)) | |||
5278 | return true; | |||
5279 | ||||
5280 | bool PackExpansionIntoNonPack = | |||
5281 | NewArgs[ArgIdx].getArgument().isPackExpansion() && | |||
5282 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); | |||
5283 | if (PackExpansionIntoNonPack && (isa<TypeAliasTemplateDecl>(Template) || | |||
5284 | isa<ConceptDecl>(Template))) { | |||
5285 | // Core issue 1430: we have a pack expansion as an argument to an | |||
5286 | // alias template, and it's not part of a parameter pack. This | |||
5287 | // can't be canonicalized, so reject it now. | |||
5288 | // As for concepts - we cannot normalize constraints where this | |||
5289 | // situation exists. | |||
5290 | Diag(NewArgs[ArgIdx].getLocation(), | |||
5291 | diag::err_template_expansion_into_fixed_list) | |||
5292 | << (isa<ConceptDecl>(Template) ? 1 : 0) | |||
5293 | << NewArgs[ArgIdx].getSourceRange(); | |||
5294 | Diag((*Param)->getLocation(), diag::note_template_param_here); | |||
5295 | return true; | |||
5296 | } | |||
5297 | ||||
5298 | // We're now done with this argument. | |||
5299 | ++ArgIdx; | |||
5300 | ||||
5301 | if ((*Param)->isTemplateParameterPack()) { | |||
5302 | // The template parameter was a template parameter pack, so take the | |||
5303 | // deduced argument and place it on the argument pack. Note that we | |||
5304 | // stay on the same template parameter so that we can deduce more | |||
5305 | // arguments. | |||
5306 | ArgumentPack.push_back(Converted.pop_back_val()); | |||
5307 | } else { | |||
5308 | // Move to the next template parameter. | |||
5309 | ++Param; | |||
5310 | } | |||
5311 | ||||
5312 | // If we just saw a pack expansion into a non-pack, then directly convert | |||
5313 | // the remaining arguments, because we don't know what parameters they'll | |||
5314 | // match up with. | |||
5315 | if (PackExpansionIntoNonPack) { | |||
5316 | if (!ArgumentPack.empty()) { | |||
5317 | // If we were part way through filling in an expanded parameter pack, | |||
5318 | // fall back to just producing individual arguments. | |||
5319 | Converted.insert(Converted.end(), | |||
5320 | ArgumentPack.begin(), ArgumentPack.end()); | |||
5321 | ArgumentPack.clear(); | |||
5322 | } | |||
5323 | ||||
5324 | while (ArgIdx < NumArgs) { | |||
5325 | Converted.push_back(NewArgs[ArgIdx].getArgument()); | |||
5326 | ++ArgIdx; | |||
5327 | } | |||
5328 | ||||
5329 | return false; | |||
5330 | } | |||
5331 | ||||
5332 | continue; | |||
5333 | } | |||
5334 | ||||
5335 | // If we're checking a partial template argument list, we're done. | |||
5336 | if (PartialTemplateArgs) { | |||
5337 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) | |||
5338 | Converted.push_back( | |||
5339 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); | |||
5340 | return false; | |||
5341 | } | |||
5342 | ||||
5343 | // If we have a template parameter pack with no more corresponding | |||
5344 | // arguments, just break out now and we'll fill in the argument pack below. | |||
5345 | if ((*Param)->isTemplateParameterPack()) { | |||
5346 | assert(!getExpandedPackSize(*Param) &&((!getExpandedPackSize(*Param) && "Should have dealt with this already" ) ? static_cast<void> (0) : __assert_fail ("!getExpandedPackSize(*Param) && \"Should have dealt with this already\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5347, __PRETTY_FUNCTION__)) | |||
5347 | "Should have dealt with this already")((!getExpandedPackSize(*Param) && "Should have dealt with this already" ) ? static_cast<void> (0) : __assert_fail ("!getExpandedPackSize(*Param) && \"Should have dealt with this already\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5347, __PRETTY_FUNCTION__)); | |||
5348 | ||||
5349 | // A non-expanded parameter pack before the end of the parameter list | |||
5350 | // only occurs for an ill-formed template parameter list, unless we've | |||
5351 | // got a partial argument list for a function template, so just bail out. | |||
5352 | if (Param + 1 != ParamEnd) | |||
5353 | return true; | |||
5354 | ||||
5355 | Converted.push_back( | |||
5356 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); | |||
5357 | ArgumentPack.clear(); | |||
5358 | ||||
5359 | ++Param; | |||
5360 | continue; | |||
5361 | } | |||
5362 | ||||
5363 | // Check whether we have a default argument. | |||
5364 | TemplateArgumentLoc Arg; | |||
5365 | ||||
5366 | // Retrieve the default template argument from the template | |||
5367 | // parameter. For each kind of template parameter, we substitute the | |||
5368 | // template arguments provided thus far and any "outer" template arguments | |||
5369 | // (when the template parameter was part of a nested template) into | |||
5370 | // the default argument. | |||
5371 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { | |||
5372 | if (!hasVisibleDefaultArgument(TTP)) | |||
5373 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, | |||
5374 | NewArgs); | |||
5375 | ||||
5376 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, | |||
5377 | Template, | |||
5378 | TemplateLoc, | |||
5379 | RAngleLoc, | |||
5380 | TTP, | |||
5381 | Converted); | |||
5382 | if (!ArgType) | |||
5383 | return true; | |||
5384 | ||||
5385 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), | |||
5386 | ArgType); | |||
5387 | } else if (NonTypeTemplateParmDecl *NTTP | |||
5388 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { | |||
5389 | if (!hasVisibleDefaultArgument(NTTP)) | |||
5390 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, | |||
5391 | NewArgs); | |||
5392 | ||||
5393 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, | |||
5394 | TemplateLoc, | |||
5395 | RAngleLoc, | |||
5396 | NTTP, | |||
5397 | Converted); | |||
5398 | if (E.isInvalid()) | |||
5399 | return true; | |||
5400 | ||||
5401 | Expr *Ex = E.getAs<Expr>(); | |||
5402 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); | |||
5403 | } else { | |||
5404 | TemplateTemplateParmDecl *TempParm | |||
5405 | = cast<TemplateTemplateParmDecl>(*Param); | |||
5406 | ||||
5407 | if (!hasVisibleDefaultArgument(TempParm)) | |||
5408 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, | |||
5409 | NewArgs); | |||
5410 | ||||
5411 | NestedNameSpecifierLoc QualifierLoc; | |||
5412 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, | |||
5413 | TemplateLoc, | |||
5414 | RAngleLoc, | |||
5415 | TempParm, | |||
5416 | Converted, | |||
5417 | QualifierLoc); | |||
5418 | if (Name.isNull()) | |||
5419 | return true; | |||
5420 | ||||
5421 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, | |||
5422 | TempParm->getDefaultArgument().getTemplateNameLoc()); | |||
5423 | } | |||
5424 | ||||
5425 | // Introduce an instantiation record that describes where we are using | |||
5426 | // the default template argument. We're not actually instantiating a | |||
5427 | // template here, we just create this object to put a note into the | |||
5428 | // context stack. | |||
5429 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, | |||
5430 | SourceRange(TemplateLoc, RAngleLoc)); | |||
5431 | if (Inst.isInvalid()) | |||
5432 | return true; | |||
5433 | ||||
5434 | // Check the default template argument. | |||
5435 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, | |||
5436 | RAngleLoc, 0, Converted)) | |||
5437 | return true; | |||
5438 | ||||
5439 | // Core issue 150 (assumed resolution): if this is a template template | |||
5440 | // parameter, keep track of the default template arguments from the | |||
5441 | // template definition. | |||
5442 | if (isTemplateTemplateParameter) | |||
5443 | NewArgs.addArgument(Arg); | |||
5444 | ||||
5445 | // Move to the next template parameter and argument. | |||
5446 | ++Param; | |||
5447 | ++ArgIdx; | |||
5448 | } | |||
5449 | ||||
5450 | // If we're performing a partial argument substitution, allow any trailing | |||
5451 | // pack expansions; they might be empty. This can happen even if | |||
5452 | // PartialTemplateArgs is false (the list of arguments is complete but | |||
5453 | // still dependent). | |||
5454 | if (ArgIdx < NumArgs && CurrentInstantiationScope && | |||
5455 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { | |||
5456 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) | |||
5457 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); | |||
5458 | } | |||
5459 | ||||
5460 | // If we have any leftover arguments, then there were too many arguments. | |||
5461 | // Complain and fail. | |||
5462 | if (ArgIdx < NumArgs) { | |||
5463 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) | |||
5464 | << /*too many args*/1 | |||
5465 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) | |||
5466 | << Template | |||
5467 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); | |||
5468 | Diag(Template->getLocation(), diag::note_template_decl_here) | |||
5469 | << Params->getSourceRange(); | |||
5470 | return true; | |||
5471 | } | |||
5472 | ||||
5473 | // No problems found with the new argument list, propagate changes back | |||
5474 | // to caller. | |||
5475 | if (UpdateArgsWithConversions) | |||
5476 | TemplateArgs = std::move(NewArgs); | |||
5477 | ||||
5478 | if (!PartialTemplateArgs && | |||
5479 | EnsureTemplateArgumentListConstraints( | |||
5480 | Template, Converted, SourceRange(TemplateLoc, | |||
5481 | TemplateArgs.getRAngleLoc()))) { | |||
5482 | if (ConstraintsNotSatisfied) | |||
5483 | *ConstraintsNotSatisfied = true; | |||
5484 | return true; | |||
5485 | } | |||
5486 | ||||
5487 | return false; | |||
5488 | } | |||
5489 | ||||
5490 | namespace { | |||
5491 | class UnnamedLocalNoLinkageFinder | |||
5492 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> | |||
5493 | { | |||
5494 | Sema &S; | |||
5495 | SourceRange SR; | |||
5496 | ||||
5497 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; | |||
5498 | ||||
5499 | public: | |||
5500 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } | |||
5501 | ||||
5502 | bool Visit(QualType T) { | |||
5503 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); | |||
5504 | } | |||
5505 | ||||
5506 | #define TYPE(Class, Parent) \ | |||
5507 | bool Visit##Class##Type(const Class##Type *); | |||
5508 | #define ABSTRACT_TYPE(Class, Parent) \ | |||
5509 | bool Visit##Class##Type(const Class##Type *) { return false; } | |||
5510 | #define NON_CANONICAL_TYPE(Class, Parent) \ | |||
5511 | bool Visit##Class##Type(const Class##Type *) { return false; } | |||
5512 | #include "clang/AST/TypeNodes.inc" | |||
5513 | ||||
5514 | bool VisitTagDecl(const TagDecl *Tag); | |||
5515 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); | |||
5516 | }; | |||
5517 | } // end anonymous namespace | |||
5518 | ||||
5519 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { | |||
5520 | return false; | |||
5521 | } | |||
5522 | ||||
5523 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { | |||
5524 | return Visit(T->getElementType()); | |||
5525 | } | |||
5526 | ||||
5527 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { | |||
5528 | return Visit(T->getPointeeType()); | |||
5529 | } | |||
5530 | ||||
5531 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( | |||
5532 | const BlockPointerType* T) { | |||
5533 | return Visit(T->getPointeeType()); | |||
5534 | } | |||
5535 | ||||
5536 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( | |||
5537 | const LValueReferenceType* T) { | |||
5538 | return Visit(T->getPointeeType()); | |||
5539 | } | |||
5540 | ||||
5541 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( | |||
5542 | const RValueReferenceType* T) { | |||
5543 | return Visit(T->getPointeeType()); | |||
5544 | } | |||
5545 | ||||
5546 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( | |||
5547 | const MemberPointerType* T) { | |||
5548 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); | |||
5549 | } | |||
5550 | ||||
5551 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( | |||
5552 | const ConstantArrayType* T) { | |||
5553 | return Visit(T->getElementType()); | |||
5554 | } | |||
5555 | ||||
5556 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( | |||
5557 | const IncompleteArrayType* T) { | |||
5558 | return Visit(T->getElementType()); | |||
5559 | } | |||
5560 | ||||
5561 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( | |||
5562 | const VariableArrayType* T) { | |||
5563 | return Visit(T->getElementType()); | |||
5564 | } | |||
5565 | ||||
5566 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( | |||
5567 | const DependentSizedArrayType* T) { | |||
5568 | return Visit(T->getElementType()); | |||
5569 | } | |||
5570 | ||||
5571 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( | |||
5572 | const DependentSizedExtVectorType* T) { | |||
5573 | return Visit(T->getElementType()); | |||
5574 | } | |||
5575 | ||||
5576 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( | |||
5577 | const DependentAddressSpaceType *T) { | |||
5578 | return Visit(T->getPointeeType()); | |||
5579 | } | |||
5580 | ||||
5581 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { | |||
5582 | return Visit(T->getElementType()); | |||
5583 | } | |||
5584 | ||||
5585 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( | |||
5586 | const DependentVectorType *T) { | |||
5587 | return Visit(T->getElementType()); | |||
5588 | } | |||
5589 | ||||
5590 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { | |||
5591 | return Visit(T->getElementType()); | |||
5592 | } | |||
5593 | ||||
5594 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( | |||
5595 | const FunctionProtoType* T) { | |||
5596 | for (const auto &A : T->param_types()) { | |||
5597 | if (Visit(A)) | |||
5598 | return true; | |||
5599 | } | |||
5600 | ||||
5601 | return Visit(T->getReturnType()); | |||
5602 | } | |||
5603 | ||||
5604 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( | |||
5605 | const FunctionNoProtoType* T) { | |||
5606 | return Visit(T->getReturnType()); | |||
5607 | } | |||
5608 | ||||
5609 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( | |||
5610 | const UnresolvedUsingType*) { | |||
5611 | return false; | |||
5612 | } | |||
5613 | ||||
5614 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { | |||
5615 | return false; | |||
5616 | } | |||
5617 | ||||
5618 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { | |||
5619 | return Visit(T->getUnderlyingType()); | |||
5620 | } | |||
5621 | ||||
5622 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { | |||
5623 | return false; | |||
5624 | } | |||
5625 | ||||
5626 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( | |||
5627 | const UnaryTransformType*) { | |||
5628 | return false; | |||
5629 | } | |||
5630 | ||||
5631 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { | |||
5632 | return Visit(T->getDeducedType()); | |||
5633 | } | |||
5634 | ||||
5635 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( | |||
5636 | const DeducedTemplateSpecializationType *T) { | |||
5637 | return Visit(T->getDeducedType()); | |||
5638 | } | |||
5639 | ||||
5640 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { | |||
5641 | return VisitTagDecl(T->getDecl()); | |||
5642 | } | |||
5643 | ||||
5644 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { | |||
5645 | return VisitTagDecl(T->getDecl()); | |||
5646 | } | |||
5647 | ||||
5648 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( | |||
5649 | const TemplateTypeParmType*) { | |||
5650 | return false; | |||
5651 | } | |||
5652 | ||||
5653 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( | |||
5654 | const SubstTemplateTypeParmPackType *) { | |||
5655 | return false; | |||
5656 | } | |||
5657 | ||||
5658 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( | |||
5659 | const TemplateSpecializationType*) { | |||
5660 | return false; | |||
5661 | } | |||
5662 | ||||
5663 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( | |||
5664 | const InjectedClassNameType* T) { | |||
5665 | return VisitTagDecl(T->getDecl()); | |||
5666 | } | |||
5667 | ||||
5668 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( | |||
5669 | const DependentNameType* T) { | |||
5670 | return VisitNestedNameSpecifier(T->getQualifier()); | |||
5671 | } | |||
5672 | ||||
5673 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( | |||
5674 | const DependentTemplateSpecializationType* T) { | |||
5675 | return VisitNestedNameSpecifier(T->getQualifier()); | |||
5676 | } | |||
5677 | ||||
5678 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( | |||
5679 | const PackExpansionType* T) { | |||
5680 | return Visit(T->getPattern()); | |||
5681 | } | |||
5682 | ||||
5683 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { | |||
5684 | return false; | |||
5685 | } | |||
5686 | ||||
5687 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( | |||
5688 | const ObjCInterfaceType *) { | |||
5689 | return false; | |||
5690 | } | |||
5691 | ||||
5692 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( | |||
5693 | const ObjCObjectPointerType *) { | |||
5694 | return false; | |||
5695 | } | |||
5696 | ||||
5697 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { | |||
5698 | return Visit(T->getValueType()); | |||
5699 | } | |||
5700 | ||||
5701 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { | |||
5702 | return false; | |||
5703 | } | |||
5704 | ||||
5705 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { | |||
5706 | if (Tag->getDeclContext()->isFunctionOrMethod()) { | |||
5707 | S.Diag(SR.getBegin(), | |||
5708 | S.getLangOpts().CPlusPlus11 ? | |||
5709 | diag::warn_cxx98_compat_template_arg_local_type : | |||
5710 | diag::ext_template_arg_local_type) | |||
5711 | << S.Context.getTypeDeclType(Tag) << SR; | |||
5712 | return true; | |||
5713 | } | |||
5714 | ||||
5715 | if (!Tag->hasNameForLinkage()) { | |||
5716 | S.Diag(SR.getBegin(), | |||
5717 | S.getLangOpts().CPlusPlus11 ? | |||
5718 | diag::warn_cxx98_compat_template_arg_unnamed_type : | |||
5719 | diag::ext_template_arg_unnamed_type) << SR; | |||
5720 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); | |||
5721 | return true; | |||
5722 | } | |||
5723 | ||||
5724 | return false; | |||
5725 | } | |||
5726 | ||||
5727 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( | |||
5728 | NestedNameSpecifier *NNS) { | |||
5729 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) | |||
5730 | return true; | |||
5731 | ||||
5732 | switch (NNS->getKind()) { | |||
5733 | case NestedNameSpecifier::Identifier: | |||
5734 | case NestedNameSpecifier::Namespace: | |||
5735 | case NestedNameSpecifier::NamespaceAlias: | |||
5736 | case NestedNameSpecifier::Global: | |||
5737 | case NestedNameSpecifier::Super: | |||
5738 | return false; | |||
5739 | ||||
5740 | case NestedNameSpecifier::TypeSpec: | |||
5741 | case NestedNameSpecifier::TypeSpecWithTemplate: | |||
5742 | return Visit(QualType(NNS->getAsType(), 0)); | |||
5743 | } | |||
5744 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!")::llvm::llvm_unreachable_internal("Invalid NestedNameSpecifier::Kind!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5744); | |||
5745 | } | |||
5746 | ||||
5747 | /// Check a template argument against its corresponding | |||
5748 | /// template type parameter. | |||
5749 | /// | |||
5750 | /// This routine implements the semantics of C++ [temp.arg.type]. It | |||
5751 | /// returns true if an error occurred, and false otherwise. | |||
5752 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, | |||
5753 | TypeSourceInfo *ArgInfo) { | |||
5754 | assert(ArgInfo && "invalid TypeSourceInfo")((ArgInfo && "invalid TypeSourceInfo") ? static_cast< void> (0) : __assert_fail ("ArgInfo && \"invalid TypeSourceInfo\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5754, __PRETTY_FUNCTION__)); | |||
5755 | QualType Arg = ArgInfo->getType(); | |||
5756 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); | |||
5757 | ||||
5758 | if (Arg->isVariablyModifiedType()) { | |||
5759 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; | |||
5760 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { | |||
5761 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; | |||
5762 | } | |||
5763 | ||||
5764 | // C++03 [temp.arg.type]p2: | |||
5765 | // A local type, a type with no linkage, an unnamed type or a type | |||
5766 | // compounded from any of these types shall not be used as a | |||
5767 | // template-argument for a template type-parameter. | |||
5768 | // | |||
5769 | // C++11 allows these, and even in C++03 we allow them as an extension with | |||
5770 | // a warning. | |||
5771 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { | |||
5772 | UnnamedLocalNoLinkageFinder Finder(*this, SR); | |||
5773 | (void)Finder.Visit(Context.getCanonicalType(Arg)); | |||
5774 | } | |||
5775 | ||||
5776 | return false; | |||
5777 | } | |||
5778 | ||||
5779 | enum NullPointerValueKind { | |||
5780 | NPV_NotNullPointer, | |||
5781 | NPV_NullPointer, | |||
5782 | NPV_Error | |||
5783 | }; | |||
5784 | ||||
5785 | /// Determine whether the given template argument is a null pointer | |||
5786 | /// value of the appropriate type. | |||
5787 | static NullPointerValueKind | |||
5788 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, | |||
5789 | QualType ParamType, Expr *Arg, | |||
5790 | Decl *Entity = nullptr) { | |||
5791 | if (Arg->isValueDependent() || Arg->isTypeDependent()) | |||
5792 | return NPV_NotNullPointer; | |||
5793 | ||||
5794 | // dllimport'd entities aren't constant but are available inside of template | |||
5795 | // arguments. | |||
5796 | if (Entity && Entity->hasAttr<DLLImportAttr>()) | |||
5797 | return NPV_NotNullPointer; | |||
5798 | ||||
5799 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) | |||
5800 | llvm_unreachable(::llvm::llvm_unreachable_internal("Incomplete parameter type in isNullPointerValueTemplateArgument!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5801) | |||
5801 | "Incomplete parameter type in isNullPointerValueTemplateArgument!")::llvm::llvm_unreachable_internal("Incomplete parameter type in isNullPointerValueTemplateArgument!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5801); | |||
5802 | ||||
5803 | if (!S.getLangOpts().CPlusPlus11) | |||
5804 | return NPV_NotNullPointer; | |||
5805 | ||||
5806 | // Determine whether we have a constant expression. | |||
5807 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); | |||
5808 | if (ArgRV.isInvalid()) | |||
5809 | return NPV_Error; | |||
5810 | Arg = ArgRV.get(); | |||
5811 | ||||
5812 | Expr::EvalResult EvalResult; | |||
5813 | SmallVector<PartialDiagnosticAt, 8> Notes; | |||
5814 | EvalResult.Diag = &Notes; | |||
5815 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || | |||
5816 | EvalResult.HasSideEffects) { | |||
5817 | SourceLocation DiagLoc = Arg->getExprLoc(); | |||
5818 | ||||
5819 | // If our only note is the usual "invalid subexpression" note, just point | |||
5820 | // the caret at its location rather than producing an essentially | |||
5821 | // redundant note. | |||
5822 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | |||
5823 | diag::note_invalid_subexpr_in_const_expr) { | |||
5824 | DiagLoc = Notes[0].first; | |||
5825 | Notes.clear(); | |||
5826 | } | |||
5827 | ||||
5828 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) | |||
5829 | << Arg->getType() << Arg->getSourceRange(); | |||
5830 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) | |||
5831 | S.Diag(Notes[I].first, Notes[I].second); | |||
5832 | ||||
5833 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
5834 | return NPV_Error; | |||
5835 | } | |||
5836 | ||||
5837 | // C++11 [temp.arg.nontype]p1: | |||
5838 | // - an address constant expression of type std::nullptr_t | |||
5839 | if (Arg->getType()->isNullPtrType()) | |||
5840 | return NPV_NullPointer; | |||
5841 | ||||
5842 | // - a constant expression that evaluates to a null pointer value (4.10); or | |||
5843 | // - a constant expression that evaluates to a null member pointer value | |||
5844 | // (4.11); or | |||
5845 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || | |||
5846 | (EvalResult.Val.isMemberPointer() && | |||
5847 | !EvalResult.Val.getMemberPointerDecl())) { | |||
5848 | // If our expression has an appropriate type, we've succeeded. | |||
5849 | bool ObjCLifetimeConversion; | |||
5850 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || | |||
5851 | S.IsQualificationConversion(Arg->getType(), ParamType, false, | |||
5852 | ObjCLifetimeConversion)) | |||
5853 | return NPV_NullPointer; | |||
5854 | ||||
5855 | // The types didn't match, but we know we got a null pointer; complain, | |||
5856 | // then recover as if the types were correct. | |||
5857 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) | |||
5858 | << Arg->getType() << ParamType << Arg->getSourceRange(); | |||
5859 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
5860 | return NPV_NullPointer; | |||
5861 | } | |||
5862 | ||||
5863 | // If we don't have a null pointer value, but we do have a NULL pointer | |||
5864 | // constant, suggest a cast to the appropriate type. | |||
5865 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { | |||
5866 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; | |||
5867 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) | |||
5868 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) | |||
5869 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), | |||
5870 | ")"); | |||
5871 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
5872 | return NPV_NullPointer; | |||
5873 | } | |||
5874 | ||||
5875 | // FIXME: If we ever want to support general, address-constant expressions | |||
5876 | // as non-type template arguments, we should return the ExprResult here to | |||
5877 | // be interpreted by the caller. | |||
5878 | return NPV_NotNullPointer; | |||
5879 | } | |||
5880 | ||||
5881 | /// Checks whether the given template argument is compatible with its | |||
5882 | /// template parameter. | |||
5883 | static bool CheckTemplateArgumentIsCompatibleWithParameter( | |||
5884 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, | |||
5885 | Expr *Arg, QualType ArgType) { | |||
5886 | bool ObjCLifetimeConversion; | |||
5887 | if (ParamType->isPointerType() && | |||
5888 | !ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType() && | |||
5889 | S.IsQualificationConversion(ArgType, ParamType, false, | |||
5890 | ObjCLifetimeConversion)) { | |||
5891 | // For pointer-to-object types, qualification conversions are | |||
5892 | // permitted. | |||
5893 | } else { | |||
5894 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { | |||
5895 | if (!ParamRef->getPointeeType()->isFunctionType()) { | |||
5896 | // C++ [temp.arg.nontype]p5b3: | |||
5897 | // For a non-type template-parameter of type reference to | |||
5898 | // object, no conversions apply. The type referred to by the | |||
5899 | // reference may be more cv-qualified than the (otherwise | |||
5900 | // identical) type of the template- argument. The | |||
5901 | // template-parameter is bound directly to the | |||
5902 | // template-argument, which shall be an lvalue. | |||
5903 | ||||
5904 | // FIXME: Other qualifiers? | |||
5905 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); | |||
5906 | unsigned ArgQuals = ArgType.getCVRQualifiers(); | |||
5907 | ||||
5908 | if ((ParamQuals | ArgQuals) != ParamQuals) { | |||
5909 | S.Diag(Arg->getBeginLoc(), | |||
5910 | diag::err_template_arg_ref_bind_ignores_quals) | |||
5911 | << ParamType << Arg->getType() << Arg->getSourceRange(); | |||
5912 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
5913 | return true; | |||
5914 | } | |||
5915 | } | |||
5916 | } | |||
5917 | ||||
5918 | // At this point, the template argument refers to an object or | |||
5919 | // function with external linkage. We now need to check whether the | |||
5920 | // argument and parameter types are compatible. | |||
5921 | if (!S.Context.hasSameUnqualifiedType(ArgType, | |||
5922 | ParamType.getNonReferenceType())) { | |||
5923 | // We can't perform this conversion or binding. | |||
5924 | if (ParamType->isReferenceType()) | |||
5925 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) | |||
5926 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); | |||
5927 | else | |||
5928 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) | |||
5929 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); | |||
5930 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
5931 | return true; | |||
5932 | } | |||
5933 | } | |||
5934 | ||||
5935 | return false; | |||
5936 | } | |||
5937 | ||||
5938 | /// Checks whether the given template argument is the address | |||
5939 | /// of an object or function according to C++ [temp.arg.nontype]p1. | |||
5940 | static bool | |||
5941 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, | |||
5942 | NonTypeTemplateParmDecl *Param, | |||
5943 | QualType ParamType, | |||
5944 | Expr *ArgIn, | |||
5945 | TemplateArgument &Converted) { | |||
5946 | bool Invalid = false; | |||
5947 | Expr *Arg = ArgIn; | |||
5948 | QualType ArgType = Arg->getType(); | |||
5949 | ||||
5950 | bool AddressTaken = false; | |||
5951 | SourceLocation AddrOpLoc; | |||
5952 | if (S.getLangOpts().MicrosoftExt) { | |||
5953 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of | |||
5954 | // dereference and address-of operators. | |||
5955 | Arg = Arg->IgnoreParenCasts(); | |||
5956 | ||||
5957 | bool ExtWarnMSTemplateArg = false; | |||
5958 | UnaryOperatorKind FirstOpKind; | |||
5959 | SourceLocation FirstOpLoc; | |||
5960 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { | |||
5961 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); | |||
5962 | if (UnOpKind == UO_Deref) | |||
5963 | ExtWarnMSTemplateArg = true; | |||
5964 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { | |||
5965 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); | |||
5966 | if (!AddrOpLoc.isValid()) { | |||
5967 | FirstOpKind = UnOpKind; | |||
5968 | FirstOpLoc = UnOp->getOperatorLoc(); | |||
5969 | } | |||
5970 | } else | |||
5971 | break; | |||
5972 | } | |||
5973 | if (FirstOpLoc.isValid()) { | |||
5974 | if (ExtWarnMSTemplateArg) | |||
5975 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) | |||
5976 | << ArgIn->getSourceRange(); | |||
5977 | ||||
5978 | if (FirstOpKind == UO_AddrOf) | |||
5979 | AddressTaken = true; | |||
5980 | else if (Arg->getType()->isPointerType()) { | |||
5981 | // We cannot let pointers get dereferenced here, that is obviously not a | |||
5982 | // constant expression. | |||
5983 | assert(FirstOpKind == UO_Deref)((FirstOpKind == UO_Deref) ? static_cast<void> (0) : __assert_fail ("FirstOpKind == UO_Deref", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 5983, __PRETTY_FUNCTION__)); | |||
5984 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) | |||
5985 | << Arg->getSourceRange(); | |||
5986 | } | |||
5987 | } | |||
5988 | } else { | |||
5989 | // See through any implicit casts we added to fix the type. | |||
5990 | Arg = Arg->IgnoreImpCasts(); | |||
5991 | ||||
5992 | // C++ [temp.arg.nontype]p1: | |||
5993 | // | |||
5994 | // A template-argument for a non-type, non-template | |||
5995 | // template-parameter shall be one of: [...] | |||
5996 | // | |||
5997 | // -- the address of an object or function with external | |||
5998 | // linkage, including function templates and function | |||
5999 | // template-ids but excluding non-static class members, | |||
6000 | // expressed as & id-expression where the & is optional if | |||
6001 | // the name refers to a function or array, or if the | |||
6002 | // corresponding template-parameter is a reference; or | |||
6003 | ||||
6004 | // In C++98/03 mode, give an extension warning on any extra parentheses. | |||
6005 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 | |||
6006 | bool ExtraParens = false; | |||
6007 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { | |||
6008 | if (!Invalid && !ExtraParens) { | |||
6009 | S.Diag(Arg->getBeginLoc(), | |||
6010 | S.getLangOpts().CPlusPlus11 | |||
6011 | ? diag::warn_cxx98_compat_template_arg_extra_parens | |||
6012 | : diag::ext_template_arg_extra_parens) | |||
6013 | << Arg->getSourceRange(); | |||
6014 | ExtraParens = true; | |||
6015 | } | |||
6016 | ||||
6017 | Arg = Parens->getSubExpr(); | |||
6018 | } | |||
6019 | ||||
6020 | while (SubstNonTypeTemplateParmExpr *subst = | |||
6021 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | |||
6022 | Arg = subst->getReplacement()->IgnoreImpCasts(); | |||
6023 | ||||
6024 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { | |||
6025 | if (UnOp->getOpcode() == UO_AddrOf) { | |||
6026 | Arg = UnOp->getSubExpr(); | |||
6027 | AddressTaken = true; | |||
6028 | AddrOpLoc = UnOp->getOperatorLoc(); | |||
6029 | } | |||
6030 | } | |||
6031 | ||||
6032 | while (SubstNonTypeTemplateParmExpr *subst = | |||
6033 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | |||
6034 | Arg = subst->getReplacement()->IgnoreImpCasts(); | |||
6035 | } | |||
6036 | ||||
6037 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); | |||
6038 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; | |||
6039 | ||||
6040 | // If our parameter has pointer type, check for a null template value. | |||
6041 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { | |||
6042 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, | |||
6043 | Entity)) { | |||
6044 | case NPV_NullPointer: | |||
6045 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | |||
6046 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), | |||
6047 | /*isNullPtr=*/true); | |||
6048 | return false; | |||
6049 | ||||
6050 | case NPV_Error: | |||
6051 | return true; | |||
6052 | ||||
6053 | case NPV_NotNullPointer: | |||
6054 | break; | |||
6055 | } | |||
6056 | } | |||
6057 | ||||
6058 | // Stop checking the precise nature of the argument if it is value dependent, | |||
6059 | // it should be checked when instantiated. | |||
6060 | if (Arg->isValueDependent()) { | |||
6061 | Converted = TemplateArgument(ArgIn); | |||
6062 | return false; | |||
6063 | } | |||
6064 | ||||
6065 | if (isa<CXXUuidofExpr>(Arg)) { | |||
6066 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, | |||
6067 | ArgIn, Arg, ArgType)) | |||
6068 | return true; | |||
6069 | ||||
6070 | Converted = TemplateArgument(ArgIn); | |||
6071 | return false; | |||
6072 | } | |||
6073 | ||||
6074 | if (!DRE) { | |||
6075 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) | |||
6076 | << Arg->getSourceRange(); | |||
6077 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6078 | return true; | |||
6079 | } | |||
6080 | ||||
6081 | // Cannot refer to non-static data members | |||
6082 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { | |||
6083 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) | |||
6084 | << Entity << Arg->getSourceRange(); | |||
6085 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6086 | return true; | |||
6087 | } | |||
6088 | ||||
6089 | // Cannot refer to non-static member functions | |||
6090 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { | |||
6091 | if (!Method->isStatic()) { | |||
6092 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) | |||
6093 | << Method << Arg->getSourceRange(); | |||
6094 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6095 | return true; | |||
6096 | } | |||
6097 | } | |||
6098 | ||||
6099 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); | |||
6100 | VarDecl *Var = dyn_cast<VarDecl>(Entity); | |||
6101 | ||||
6102 | // A non-type template argument must refer to an object or function. | |||
6103 | if (!Func && !Var) { | |||
6104 | // We found something, but we don't know specifically what it is. | |||
6105 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) | |||
6106 | << Arg->getSourceRange(); | |||
6107 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); | |||
6108 | return true; | |||
6109 | } | |||
6110 | ||||
6111 | // Address / reference template args must have external linkage in C++98. | |||
6112 | if (Entity->getFormalLinkage() == InternalLinkage) { | |||
6113 | S.Diag(Arg->getBeginLoc(), | |||
6114 | S.getLangOpts().CPlusPlus11 | |||
6115 | ? diag::warn_cxx98_compat_template_arg_object_internal | |||
6116 | : diag::ext_template_arg_object_internal) | |||
6117 | << !Func << Entity << Arg->getSourceRange(); | |||
6118 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) | |||
6119 | << !Func; | |||
6120 | } else if (!Entity->hasLinkage()) { | |||
6121 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) | |||
6122 | << !Func << Entity << Arg->getSourceRange(); | |||
6123 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) | |||
6124 | << !Func; | |||
6125 | return true; | |||
6126 | } | |||
6127 | ||||
6128 | if (Func) { | |||
6129 | // If the template parameter has pointer type, the function decays. | |||
6130 | if (ParamType->isPointerType() && !AddressTaken) | |||
6131 | ArgType = S.Context.getPointerType(Func->getType()); | |||
6132 | else if (AddressTaken && ParamType->isReferenceType()) { | |||
6133 | // If we originally had an address-of operator, but the | |||
6134 | // parameter has reference type, complain and (if things look | |||
6135 | // like they will work) drop the address-of operator. | |||
6136 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), | |||
6137 | ParamType.getNonReferenceType())) { | |||
6138 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | |||
6139 | << ParamType; | |||
6140 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6141 | return true; | |||
6142 | } | |||
6143 | ||||
6144 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | |||
6145 | << ParamType | |||
6146 | << FixItHint::CreateRemoval(AddrOpLoc); | |||
6147 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6148 | ||||
6149 | ArgType = Func->getType(); | |||
6150 | } | |||
6151 | } else { | |||
6152 | // A value of reference type is not an object. | |||
6153 | if (Var->getType()->isReferenceType()) { | |||
6154 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) | |||
6155 | << Var->getType() << Arg->getSourceRange(); | |||
6156 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6157 | return true; | |||
6158 | } | |||
6159 | ||||
6160 | // A template argument must have static storage duration. | |||
6161 | if (Var->getTLSKind()) { | |||
6162 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) | |||
6163 | << Arg->getSourceRange(); | |||
6164 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); | |||
6165 | return true; | |||
6166 | } | |||
6167 | ||||
6168 | // If the template parameter has pointer type, we must have taken | |||
6169 | // the address of this object. | |||
6170 | if (ParamType->isReferenceType()) { | |||
6171 | if (AddressTaken) { | |||
6172 | // If we originally had an address-of operator, but the | |||
6173 | // parameter has reference type, complain and (if things look | |||
6174 | // like they will work) drop the address-of operator. | |||
6175 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), | |||
6176 | ParamType.getNonReferenceType())) { | |||
6177 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | |||
6178 | << ParamType; | |||
6179 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6180 | return true; | |||
6181 | } | |||
6182 | ||||
6183 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | |||
6184 | << ParamType | |||
6185 | << FixItHint::CreateRemoval(AddrOpLoc); | |||
6186 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6187 | ||||
6188 | ArgType = Var->getType(); | |||
6189 | } | |||
6190 | } else if (!AddressTaken && ParamType->isPointerType()) { | |||
6191 | if (Var->getType()->isArrayType()) { | |||
6192 | // Array-to-pointer decay. | |||
6193 | ArgType = S.Context.getArrayDecayedType(Var->getType()); | |||
6194 | } else { | |||
6195 | // If the template parameter has pointer type but the address of | |||
6196 | // this object was not taken, complain and (possibly) recover by | |||
6197 | // taking the address of the entity. | |||
6198 | ArgType = S.Context.getPointerType(Var->getType()); | |||
6199 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { | |||
6200 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) | |||
6201 | << ParamType; | |||
6202 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6203 | return true; | |||
6204 | } | |||
6205 | ||||
6206 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) | |||
6207 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); | |||
6208 | ||||
6209 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6210 | } | |||
6211 | } | |||
6212 | } | |||
6213 | ||||
6214 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, | |||
6215 | Arg, ArgType)) | |||
6216 | return true; | |||
6217 | ||||
6218 | // Create the template argument. | |||
6219 | Converted = | |||
6220 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); | |||
6221 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); | |||
6222 | return false; | |||
6223 | } | |||
6224 | ||||
6225 | /// Checks whether the given template argument is a pointer to | |||
6226 | /// member constant according to C++ [temp.arg.nontype]p1. | |||
6227 | static bool CheckTemplateArgumentPointerToMember(Sema &S, | |||
6228 | NonTypeTemplateParmDecl *Param, | |||
6229 | QualType ParamType, | |||
6230 | Expr *&ResultArg, | |||
6231 | TemplateArgument &Converted) { | |||
6232 | bool Invalid = false; | |||
6233 | ||||
6234 | Expr *Arg = ResultArg; | |||
6235 | bool ObjCLifetimeConversion; | |||
6236 | ||||
6237 | // C++ [temp.arg.nontype]p1: | |||
6238 | // | |||
6239 | // A template-argument for a non-type, non-template | |||
6240 | // template-parameter shall be one of: [...] | |||
6241 | // | |||
6242 | // -- a pointer to member expressed as described in 5.3.1. | |||
6243 | DeclRefExpr *DRE = nullptr; | |||
6244 | ||||
6245 | // In C++98/03 mode, give an extension warning on any extra parentheses. | |||
6246 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 | |||
6247 | bool ExtraParens = false; | |||
6248 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { | |||
6249 | if (!Invalid && !ExtraParens) { | |||
6250 | S.Diag(Arg->getBeginLoc(), | |||
6251 | S.getLangOpts().CPlusPlus11 | |||
6252 | ? diag::warn_cxx98_compat_template_arg_extra_parens | |||
6253 | : diag::ext_template_arg_extra_parens) | |||
6254 | << Arg->getSourceRange(); | |||
6255 | ExtraParens = true; | |||
6256 | } | |||
6257 | ||||
6258 | Arg = Parens->getSubExpr(); | |||
6259 | } | |||
6260 | ||||
6261 | while (SubstNonTypeTemplateParmExpr *subst = | |||
6262 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | |||
6263 | Arg = subst->getReplacement()->IgnoreImpCasts(); | |||
6264 | ||||
6265 | // A pointer-to-member constant written &Class::member. | |||
6266 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { | |||
6267 | if (UnOp->getOpcode() == UO_AddrOf) { | |||
6268 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); | |||
6269 | if (DRE && !DRE->getQualifier()) | |||
6270 | DRE = nullptr; | |||
6271 | } | |||
6272 | } | |||
6273 | // A constant of pointer-to-member type. | |||
6274 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { | |||
6275 | ValueDecl *VD = DRE->getDecl(); | |||
6276 | if (VD->getType()->isMemberPointerType()) { | |||
6277 | if (isa<NonTypeTemplateParmDecl>(VD)) { | |||
6278 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | |||
6279 | Converted = TemplateArgument(Arg); | |||
6280 | } else { | |||
6281 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); | |||
6282 | Converted = TemplateArgument(VD, ParamType); | |||
6283 | } | |||
6284 | return Invalid; | |||
6285 | } | |||
6286 | } | |||
6287 | ||||
6288 | DRE = nullptr; | |||
6289 | } | |||
6290 | ||||
6291 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; | |||
6292 | ||||
6293 | // Check for a null pointer value. | |||
6294 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, | |||
6295 | Entity)) { | |||
6296 | case NPV_Error: | |||
6297 | return true; | |||
6298 | case NPV_NullPointer: | |||
6299 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | |||
6300 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), | |||
6301 | /*isNullPtr*/true); | |||
6302 | return false; | |||
6303 | case NPV_NotNullPointer: | |||
6304 | break; | |||
6305 | } | |||
6306 | ||||
6307 | if (S.IsQualificationConversion(ResultArg->getType(), | |||
6308 | ParamType.getNonReferenceType(), false, | |||
6309 | ObjCLifetimeConversion)) { | |||
6310 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, | |||
6311 | ResultArg->getValueKind()) | |||
6312 | .get(); | |||
6313 | } else if (!S.Context.hasSameUnqualifiedType( | |||
6314 | ResultArg->getType(), ParamType.getNonReferenceType())) { | |||
6315 | // We can't perform this conversion. | |||
6316 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) | |||
6317 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); | |||
6318 | S.Diag(Param->getLocation(), diag::note_template_param_here); | |||
6319 | return true; | |||
6320 | } | |||
6321 | ||||
6322 | if (!DRE) | |||
6323 | return S.Diag(Arg->getBeginLoc(), | |||
6324 | diag::err_template_arg_not_pointer_to_member_form) | |||
6325 | << Arg->getSourceRange(); | |||
6326 | ||||
6327 | if (isa<FieldDecl>(DRE->getDecl()) || | |||
6328 | isa<IndirectFieldDecl>(DRE->getDecl()) || | |||
6329 | isa<CXXMethodDecl>(DRE->getDecl())) { | |||
6330 | assert((isa<FieldDecl>(DRE->getDecl()) ||(((isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl >(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE-> getDecl())->isStatic()) && "Only non-static member pointers can make it here" ) ? static_cast<void> (0) : __assert_fail ("(isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl>(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6333, __PRETTY_FUNCTION__)) | |||
6331 | isa<IndirectFieldDecl>(DRE->getDecl()) ||(((isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl >(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE-> getDecl())->isStatic()) && "Only non-static member pointers can make it here" ) ? static_cast<void> (0) : __assert_fail ("(isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl>(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6333, __PRETTY_FUNCTION__)) | |||
6332 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&(((isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl >(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE-> getDecl())->isStatic()) && "Only non-static member pointers can make it here" ) ? static_cast<void> (0) : __assert_fail ("(isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl>(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6333, __PRETTY_FUNCTION__)) | |||
6333 | "Only non-static member pointers can make it here")(((isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl >(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE-> getDecl())->isStatic()) && "Only non-static member pointers can make it here" ) ? static_cast<void> (0) : __assert_fail ("(isa<FieldDecl>(DRE->getDecl()) || isa<IndirectFieldDecl>(DRE->getDecl()) || !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6333, __PRETTY_FUNCTION__)); | |||
6334 | ||||
6335 | // Okay: this is the address of a non-static member, and therefore | |||
6336 | // a member pointer constant. | |||
6337 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | |||
6338 | Converted = TemplateArgument(Arg); | |||
6339 | } else { | |||
6340 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); | |||
6341 | Converted = TemplateArgument(D, ParamType); | |||
6342 | } | |||
6343 | return Invalid; | |||
6344 | } | |||
6345 | ||||
6346 | // We found something else, but we don't know specifically what it is. | |||
6347 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) | |||
6348 | << Arg->getSourceRange(); | |||
6349 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); | |||
6350 | return true; | |||
6351 | } | |||
6352 | ||||
6353 | /// Check a template argument against its corresponding | |||
6354 | /// non-type template parameter. | |||
6355 | /// | |||
6356 | /// This routine implements the semantics of C++ [temp.arg.nontype]. | |||
6357 | /// If an error occurred, it returns ExprError(); otherwise, it | |||
6358 | /// returns the converted template argument. \p ParamType is the | |||
6359 | /// type of the non-type template parameter after it has been instantiated. | |||
6360 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, | |||
6361 | QualType ParamType, Expr *Arg, | |||
6362 | TemplateArgument &Converted, | |||
6363 | CheckTemplateArgumentKind CTAK) { | |||
6364 | SourceLocation StartLoc = Arg->getBeginLoc(); | |||
6365 | ||||
6366 | // If the parameter type somehow involves auto, deduce the type now. | |||
6367 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { | |||
6368 | // During template argument deduction, we allow 'decltype(auto)' to | |||
6369 | // match an arbitrary dependent argument. | |||
6370 | // FIXME: The language rules don't say what happens in this case. | |||
6371 | // FIXME: We get an opaque dependent type out of decltype(auto) if the | |||
6372 | // expression is merely instantiation-dependent; is this enough? | |||
6373 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { | |||
6374 | auto *AT = dyn_cast<AutoType>(ParamType); | |||
6375 | if (AT && AT->isDecltypeAuto()) { | |||
6376 | Converted = TemplateArgument(Arg); | |||
6377 | return Arg; | |||
6378 | } | |||
6379 | } | |||
6380 | ||||
6381 | // When checking a deduced template argument, deduce from its type even if | |||
6382 | // the type is dependent, in order to check the types of non-type template | |||
6383 | // arguments line up properly in partial ordering. | |||
6384 | Optional<unsigned> Depth = Param->getDepth() + 1; | |||
6385 | Expr *DeductionArg = Arg; | |||
6386 | if (auto *PE = dyn_cast<PackExpansionExpr>(DeductionArg)) | |||
6387 | DeductionArg = PE->getPattern(); | |||
6388 | if (DeduceAutoType( | |||
6389 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), | |||
6390 | DeductionArg, ParamType, Depth) == DAR_Failed) { | |||
6391 | Diag(Arg->getExprLoc(), | |||
6392 | diag::err_non_type_template_parm_type_deduction_failure) | |||
6393 | << Param->getDeclName() << Param->getType() << Arg->getType() | |||
6394 | << Arg->getSourceRange(); | |||
6395 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6396 | return ExprError(); | |||
6397 | } | |||
6398 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's | |||
6399 | // an error. The error message normally references the parameter | |||
6400 | // declaration, but here we'll pass the argument location because that's | |||
6401 | // where the parameter type is deduced. | |||
6402 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); | |||
6403 | if (ParamType.isNull()) { | |||
6404 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6405 | return ExprError(); | |||
6406 | } | |||
6407 | } | |||
6408 | ||||
6409 | // We should have already dropped all cv-qualifiers by now. | |||
6410 | assert(!ParamType.hasQualifiers() &&((!ParamType.hasQualifiers() && "non-type template parameter type cannot be qualified" ) ? static_cast<void> (0) : __assert_fail ("!ParamType.hasQualifiers() && \"non-type template parameter type cannot be qualified\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6411, __PRETTY_FUNCTION__)) | |||
6411 | "non-type template parameter type cannot be qualified")((!ParamType.hasQualifiers() && "non-type template parameter type cannot be qualified" ) ? static_cast<void> (0) : __assert_fail ("!ParamType.hasQualifiers() && \"non-type template parameter type cannot be qualified\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6411, __PRETTY_FUNCTION__)); | |||
6412 | ||||
6413 | if (CTAK == CTAK_Deduced && | |||
6414 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), | |||
6415 | Arg->getType())) { | |||
6416 | // FIXME: If either type is dependent, we skip the check. This isn't | |||
6417 | // correct, since during deduction we're supposed to have replaced each | |||
6418 | // template parameter with some unique (non-dependent) placeholder. | |||
6419 | // FIXME: If the argument type contains 'auto', we carry on and fail the | |||
6420 | // type check in order to force specific types to be more specialized than | |||
6421 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to | |||
6422 | // work. | |||
6423 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && | |||
6424 | !Arg->getType()->getContainedAutoType()) { | |||
6425 | Converted = TemplateArgument(Arg); | |||
6426 | return Arg; | |||
6427 | } | |||
6428 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, | |||
6429 | // we should actually be checking the type of the template argument in P, | |||
6430 | // not the type of the template argument deduced from A, against the | |||
6431 | // template parameter type. | |||
6432 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) | |||
6433 | << Arg->getType() | |||
6434 | << ParamType.getUnqualifiedType(); | |||
6435 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6436 | return ExprError(); | |||
6437 | } | |||
6438 | ||||
6439 | // If either the parameter has a dependent type or the argument is | |||
6440 | // type-dependent, there's nothing we can check now. The argument only | |||
6441 | // contains an unexpanded pack during partial ordering, and there's | |||
6442 | // nothing more we can check in that case. | |||
6443 | if (ParamType->isDependentType() || Arg->isTypeDependent() || | |||
6444 | Arg->containsUnexpandedParameterPack()) { | |||
6445 | // Force the argument to the type of the parameter to maintain invariants. | |||
6446 | auto *PE = dyn_cast<PackExpansionExpr>(Arg); | |||
6447 | if (PE) | |||
6448 | Arg = PE->getPattern(); | |||
6449 | ExprResult E = ImpCastExprToType( | |||
6450 | Arg, ParamType.getNonLValueExprType(Context), CK_Dependent, | |||
6451 | ParamType->isLValueReferenceType() ? VK_LValue : | |||
6452 | ParamType->isRValueReferenceType() ? VK_XValue : VK_RValue); | |||
6453 | if (E.isInvalid()) | |||
6454 | return ExprError(); | |||
6455 | if (PE) { | |||
6456 | // Recreate a pack expansion if we unwrapped one. | |||
6457 | E = new (Context) | |||
6458 | PackExpansionExpr(E.get()->getType(), E.get(), PE->getEllipsisLoc(), | |||
6459 | PE->getNumExpansions()); | |||
6460 | } | |||
6461 | Converted = TemplateArgument(E.get()); | |||
6462 | return E; | |||
6463 | } | |||
6464 | ||||
6465 | // The initialization of the parameter from the argument is | |||
6466 | // a constant-evaluated context. | |||
6467 | EnterExpressionEvaluationContext ConstantEvaluated( | |||
6468 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); | |||
6469 | ||||
6470 | if (getLangOpts().CPlusPlus17) { | |||
6471 | // C++17 [temp.arg.nontype]p1: | |||
6472 | // A template-argument for a non-type template parameter shall be | |||
6473 | // a converted constant expression of the type of the template-parameter. | |||
6474 | APValue Value; | |||
6475 | ExprResult ArgResult = CheckConvertedConstantExpression( | |||
6476 | Arg, ParamType, Value, CCEK_TemplateArg); | |||
6477 | if (ArgResult.isInvalid()) | |||
6478 | return ExprError(); | |||
6479 | ||||
6480 | // For a value-dependent argument, CheckConvertedConstantExpression is | |||
6481 | // permitted (and expected) to be unable to determine a value. | |||
6482 | if (ArgResult.get()->isValueDependent()) { | |||
6483 | Converted = TemplateArgument(ArgResult.get()); | |||
6484 | return ArgResult; | |||
6485 | } | |||
6486 | ||||
6487 | QualType CanonParamType = Context.getCanonicalType(ParamType); | |||
6488 | ||||
6489 | // Convert the APValue to a TemplateArgument. | |||
6490 | switch (Value.getKind()) { | |||
6491 | case APValue::None: | |||
6492 | assert(ParamType->isNullPtrType())((ParamType->isNullPtrType()) ? static_cast<void> (0 ) : __assert_fail ("ParamType->isNullPtrType()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6492, __PRETTY_FUNCTION__)); | |||
6493 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); | |||
6494 | break; | |||
6495 | case APValue::Indeterminate: | |||
6496 | llvm_unreachable("result of constant evaluation should be initialized")::llvm::llvm_unreachable_internal("result of constant evaluation should be initialized" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6496); | |||
6497 | break; | |||
6498 | case APValue::Int: | |||
6499 | assert(ParamType->isIntegralOrEnumerationType())((ParamType->isIntegralOrEnumerationType()) ? static_cast< void> (0) : __assert_fail ("ParamType->isIntegralOrEnumerationType()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6499, __PRETTY_FUNCTION__)); | |||
6500 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); | |||
6501 | break; | |||
6502 | case APValue::MemberPointer: { | |||
6503 | assert(ParamType->isMemberPointerType())((ParamType->isMemberPointerType()) ? static_cast<void> (0) : __assert_fail ("ParamType->isMemberPointerType()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6503, __PRETTY_FUNCTION__)); | |||
6504 | ||||
6505 | // FIXME: We need TemplateArgument representation and mangling for these. | |||
6506 | if (!Value.getMemberPointerPath().empty()) { | |||
6507 | Diag(Arg->getBeginLoc(), | |||
6508 | diag::err_template_arg_member_ptr_base_derived_not_supported) | |||
6509 | << Value.getMemberPointerDecl() << ParamType | |||
6510 | << Arg->getSourceRange(); | |||
6511 | return ExprError(); | |||
6512 | } | |||
6513 | ||||
6514 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); | |||
6515 | Converted = VD ? TemplateArgument(VD, CanonParamType) | |||
6516 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); | |||
6517 | break; | |||
6518 | } | |||
6519 | case APValue::LValue: { | |||
6520 | // For a non-type template-parameter of pointer or reference type, | |||
6521 | // the value of the constant expression shall not refer to | |||
6522 | assert(ParamType->isPointerType() || ParamType->isReferenceType() ||((ParamType->isPointerType() || ParamType->isReferenceType () || ParamType->isNullPtrType()) ? static_cast<void> (0) : __assert_fail ("ParamType->isPointerType() || ParamType->isReferenceType() || ParamType->isNullPtrType()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6523, __PRETTY_FUNCTION__)) | |||
6523 | ParamType->isNullPtrType())((ParamType->isPointerType() || ParamType->isReferenceType () || ParamType->isNullPtrType()) ? static_cast<void> (0) : __assert_fail ("ParamType->isPointerType() || ParamType->isReferenceType() || ParamType->isNullPtrType()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6523, __PRETTY_FUNCTION__)); | |||
6524 | // -- a temporary object | |||
6525 | // -- a string literal | |||
6526 | // -- the result of a typeid expression, or | |||
6527 | // -- a predefined __func__ variable | |||
6528 | APValue::LValueBase Base = Value.getLValueBase(); | |||
6529 | auto *VD = const_cast<ValueDecl *>(Base.dyn_cast<const ValueDecl *>()); | |||
6530 | if (Base && !VD) { | |||
6531 | auto *E = Base.dyn_cast<const Expr *>(); | |||
6532 | if (E && isa<CXXUuidofExpr>(E)) { | |||
6533 | Converted = TemplateArgument(ArgResult.get()->IgnoreImpCasts()); | |||
6534 | break; | |||
6535 | } | |||
6536 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) | |||
6537 | << Arg->getSourceRange(); | |||
6538 | return ExprError(); | |||
6539 | } | |||
6540 | // -- a subobject | |||
6541 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && | |||
6542 | VD && VD->getType()->isArrayType() && | |||
6543 | Value.getLValuePath()[0].getAsArrayIndex() == 0 && | |||
6544 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { | |||
6545 | // Per defect report (no number yet): | |||
6546 | // ... other than a pointer to the first element of a complete array | |||
6547 | // object. | |||
6548 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || | |||
6549 | Value.isLValueOnePastTheEnd()) { | |||
6550 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) | |||
6551 | << Value.getAsString(Context, ParamType); | |||
6552 | return ExprError(); | |||
6553 | } | |||
6554 | assert((VD || !ParamType->isReferenceType()) &&(((VD || !ParamType->isReferenceType()) && "null reference should not be a constant expression" ) ? static_cast<void> (0) : __assert_fail ("(VD || !ParamType->isReferenceType()) && \"null reference should not be a constant expression\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6555, __PRETTY_FUNCTION__)) | |||
6555 | "null reference should not be a constant expression")(((VD || !ParamType->isReferenceType()) && "null reference should not be a constant expression" ) ? static_cast<void> (0) : __assert_fail ("(VD || !ParamType->isReferenceType()) && \"null reference should not be a constant expression\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6555, __PRETTY_FUNCTION__)); | |||
6556 | assert((!VD || !ParamType->isNullPtrType()) &&(((!VD || !ParamType->isNullPtrType()) && "non-null value of type nullptr_t?" ) ? static_cast<void> (0) : __assert_fail ("(!VD || !ParamType->isNullPtrType()) && \"non-null value of type nullptr_t?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6557, __PRETTY_FUNCTION__)) | |||
6557 | "non-null value of type nullptr_t?")(((!VD || !ParamType->isNullPtrType()) && "non-null value of type nullptr_t?" ) ? static_cast<void> (0) : __assert_fail ("(!VD || !ParamType->isNullPtrType()) && \"non-null value of type nullptr_t?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6557, __PRETTY_FUNCTION__)); | |||
6558 | Converted = VD ? TemplateArgument(VD, CanonParamType) | |||
6559 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); | |||
6560 | break; | |||
6561 | } | |||
6562 | case APValue::AddrLabelDiff: | |||
6563 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); | |||
6564 | case APValue::FixedPoint: | |||
6565 | case APValue::Float: | |||
6566 | case APValue::ComplexInt: | |||
6567 | case APValue::ComplexFloat: | |||
6568 | case APValue::Vector: | |||
6569 | case APValue::Array: | |||
6570 | case APValue::Struct: | |||
6571 | case APValue::Union: | |||
6572 | llvm_unreachable("invalid kind for template argument")::llvm::llvm_unreachable_internal("invalid kind for template argument" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6572); | |||
6573 | } | |||
6574 | ||||
6575 | return ArgResult.get(); | |||
6576 | } | |||
6577 | ||||
6578 | // C++ [temp.arg.nontype]p5: | |||
6579 | // The following conversions are performed on each expression used | |||
6580 | // as a non-type template-argument. If a non-type | |||
6581 | // template-argument cannot be converted to the type of the | |||
6582 | // corresponding template-parameter then the program is | |||
6583 | // ill-formed. | |||
6584 | if (ParamType->isIntegralOrEnumerationType()) { | |||
6585 | // C++11: | |||
6586 | // -- for a non-type template-parameter of integral or | |||
6587 | // enumeration type, conversions permitted in a converted | |||
6588 | // constant expression are applied. | |||
6589 | // | |||
6590 | // C++98: | |||
6591 | // -- for a non-type template-parameter of integral or | |||
6592 | // enumeration type, integral promotions (4.5) and integral | |||
6593 | // conversions (4.7) are applied. | |||
6594 | ||||
6595 | if (getLangOpts().CPlusPlus11) { | |||
6596 | // C++ [temp.arg.nontype]p1: | |||
6597 | // A template-argument for a non-type, non-template template-parameter | |||
6598 | // shall be one of: | |||
6599 | // | |||
6600 | // -- for a non-type template-parameter of integral or enumeration | |||
6601 | // type, a converted constant expression of the type of the | |||
6602 | // template-parameter; or | |||
6603 | llvm::APSInt Value; | |||
6604 | ExprResult ArgResult = | |||
6605 | CheckConvertedConstantExpression(Arg, ParamType, Value, | |||
6606 | CCEK_TemplateArg); | |||
6607 | if (ArgResult.isInvalid()) | |||
6608 | return ExprError(); | |||
6609 | ||||
6610 | // We can't check arbitrary value-dependent arguments. | |||
6611 | if (ArgResult.get()->isValueDependent()) { | |||
6612 | Converted = TemplateArgument(ArgResult.get()); | |||
6613 | return ArgResult; | |||
6614 | } | |||
6615 | ||||
6616 | // Widen the argument value to sizeof(parameter type). This is almost | |||
6617 | // always a no-op, except when the parameter type is bool. In | |||
6618 | // that case, this may extend the argument from 1 bit to 8 bits. | |||
6619 | QualType IntegerType = ParamType; | |||
6620 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) | |||
6621 | IntegerType = Enum->getDecl()->getIntegerType(); | |||
6622 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); | |||
6623 | ||||
6624 | Converted = TemplateArgument(Context, Value, | |||
6625 | Context.getCanonicalType(ParamType)); | |||
6626 | return ArgResult; | |||
6627 | } | |||
6628 | ||||
6629 | ExprResult ArgResult = DefaultLvalueConversion(Arg); | |||
6630 | if (ArgResult.isInvalid()) | |||
6631 | return ExprError(); | |||
6632 | Arg = ArgResult.get(); | |||
6633 | ||||
6634 | QualType ArgType = Arg->getType(); | |||
6635 | ||||
6636 | // C++ [temp.arg.nontype]p1: | |||
6637 | // A template-argument for a non-type, non-template | |||
6638 | // template-parameter shall be one of: | |||
6639 | // | |||
6640 | // -- an integral constant-expression of integral or enumeration | |||
6641 | // type; or | |||
6642 | // -- the name of a non-type template-parameter; or | |||
6643 | llvm::APSInt Value; | |||
6644 | if (!ArgType->isIntegralOrEnumerationType()) { | |||
6645 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) | |||
6646 | << ArgType << Arg->getSourceRange(); | |||
6647 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6648 | return ExprError(); | |||
6649 | } else if (!Arg->isValueDependent()) { | |||
6650 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { | |||
6651 | QualType T; | |||
6652 | ||||
6653 | public: | |||
6654 | TmplArgICEDiagnoser(QualType T) : T(T) { } | |||
6655 | ||||
6656 | void diagnoseNotICE(Sema &S, SourceLocation Loc, | |||
6657 | SourceRange SR) override { | |||
6658 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; | |||
6659 | } | |||
6660 | } Diagnoser(ArgType); | |||
6661 | ||||
6662 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, | |||
6663 | false).get(); | |||
6664 | if (!Arg) | |||
6665 | return ExprError(); | |||
6666 | } | |||
6667 | ||||
6668 | // From here on out, all we care about is the unqualified form | |||
6669 | // of the argument type. | |||
6670 | ArgType = ArgType.getUnqualifiedType(); | |||
6671 | ||||
6672 | // Try to convert the argument to the parameter's type. | |||
6673 | if (Context.hasSameType(ParamType, ArgType)) { | |||
6674 | // Okay: no conversion necessary | |||
6675 | } else if (ParamType->isBooleanType()) { | |||
6676 | // This is an integral-to-boolean conversion. | |||
6677 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); | |||
6678 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || | |||
6679 | !ParamType->isEnumeralType()) { | |||
6680 | // This is an integral promotion or conversion. | |||
6681 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); | |||
6682 | } else { | |||
6683 | // We can't perform this conversion. | |||
6684 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) | |||
6685 | << Arg->getType() << ParamType << Arg->getSourceRange(); | |||
6686 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6687 | return ExprError(); | |||
6688 | } | |||
6689 | ||||
6690 | // Add the value of this argument to the list of converted | |||
6691 | // arguments. We use the bitwidth and signedness of the template | |||
6692 | // parameter. | |||
6693 | if (Arg->isValueDependent()) { | |||
6694 | // The argument is value-dependent. Create a new | |||
6695 | // TemplateArgument with the converted expression. | |||
6696 | Converted = TemplateArgument(Arg); | |||
6697 | return Arg; | |||
6698 | } | |||
6699 | ||||
6700 | QualType IntegerType = Context.getCanonicalType(ParamType); | |||
6701 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) | |||
6702 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); | |||
6703 | ||||
6704 | if (ParamType->isBooleanType()) { | |||
6705 | // Value must be zero or one. | |||
6706 | Value = Value != 0; | |||
6707 | unsigned AllowedBits = Context.getTypeSize(IntegerType); | |||
6708 | if (Value.getBitWidth() != AllowedBits) | |||
6709 | Value = Value.extOrTrunc(AllowedBits); | |||
6710 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); | |||
6711 | } else { | |||
6712 | llvm::APSInt OldValue = Value; | |||
6713 | ||||
6714 | // Coerce the template argument's value to the value it will have | |||
6715 | // based on the template parameter's type. | |||
6716 | unsigned AllowedBits = Context.getTypeSize(IntegerType); | |||
6717 | if (Value.getBitWidth() != AllowedBits) | |||
6718 | Value = Value.extOrTrunc(AllowedBits); | |||
6719 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); | |||
6720 | ||||
6721 | // Complain if an unsigned parameter received a negative value. | |||
6722 | if (IntegerType->isUnsignedIntegerOrEnumerationType() | |||
6723 | && (OldValue.isSigned() && OldValue.isNegative())) { | |||
6724 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) | |||
6725 | << OldValue.toString(10) << Value.toString(10) << Param->getType() | |||
6726 | << Arg->getSourceRange(); | |||
6727 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6728 | } | |||
6729 | ||||
6730 | // Complain if we overflowed the template parameter's type. | |||
6731 | unsigned RequiredBits; | |||
6732 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) | |||
6733 | RequiredBits = OldValue.getActiveBits(); | |||
6734 | else if (OldValue.isUnsigned()) | |||
6735 | RequiredBits = OldValue.getActiveBits() + 1; | |||
6736 | else | |||
6737 | RequiredBits = OldValue.getMinSignedBits(); | |||
6738 | if (RequiredBits > AllowedBits) { | |||
6739 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) | |||
6740 | << OldValue.toString(10) << Value.toString(10) << Param->getType() | |||
6741 | << Arg->getSourceRange(); | |||
6742 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6743 | } | |||
6744 | } | |||
6745 | ||||
6746 | Converted = TemplateArgument(Context, Value, | |||
6747 | ParamType->isEnumeralType() | |||
6748 | ? Context.getCanonicalType(ParamType) | |||
6749 | : IntegerType); | |||
6750 | return Arg; | |||
6751 | } | |||
6752 | ||||
6753 | QualType ArgType = Arg->getType(); | |||
6754 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction | |||
6755 | ||||
6756 | // Handle pointer-to-function, reference-to-function, and | |||
6757 | // pointer-to-member-function all in (roughly) the same way. | |||
6758 | if (// -- For a non-type template-parameter of type pointer to | |||
6759 | // function, only the function-to-pointer conversion (4.3) is | |||
6760 | // applied. If the template-argument represents a set of | |||
6761 | // overloaded functions (or a pointer to such), the matching | |||
6762 | // function is selected from the set (13.4). | |||
6763 | (ParamType->isPointerType() && | |||
6764 | ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType()) || | |||
6765 | // -- For a non-type template-parameter of type reference to | |||
6766 | // function, no conversions apply. If the template-argument | |||
6767 | // represents a set of overloaded functions, the matching | |||
6768 | // function is selected from the set (13.4). | |||
6769 | (ParamType->isReferenceType() && | |||
6770 | ParamType->castAs<ReferenceType>()->getPointeeType()->isFunctionType()) || | |||
6771 | // -- For a non-type template-parameter of type pointer to | |||
6772 | // member function, no conversions apply. If the | |||
6773 | // template-argument represents a set of overloaded member | |||
6774 | // functions, the matching member function is selected from | |||
6775 | // the set (13.4). | |||
6776 | (ParamType->isMemberPointerType() && | |||
6777 | ParamType->castAs<MemberPointerType>()->getPointeeType() | |||
6778 | ->isFunctionType())) { | |||
6779 | ||||
6780 | if (Arg->getType() == Context.OverloadTy) { | |||
6781 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, | |||
6782 | true, | |||
6783 | FoundResult)) { | |||
6784 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) | |||
6785 | return ExprError(); | |||
6786 | ||||
6787 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); | |||
6788 | ArgType = Arg->getType(); | |||
6789 | } else | |||
6790 | return ExprError(); | |||
6791 | } | |||
6792 | ||||
6793 | if (!ParamType->isMemberPointerType()) { | |||
6794 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | |||
6795 | ParamType, | |||
6796 | Arg, Converted)) | |||
6797 | return ExprError(); | |||
6798 | return Arg; | |||
6799 | } | |||
6800 | ||||
6801 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, | |||
6802 | Converted)) | |||
6803 | return ExprError(); | |||
6804 | return Arg; | |||
6805 | } | |||
6806 | ||||
6807 | if (ParamType->isPointerType()) { | |||
6808 | // -- for a non-type template-parameter of type pointer to | |||
6809 | // object, qualification conversions (4.4) and the | |||
6810 | // array-to-pointer conversion (4.2) are applied. | |||
6811 | // C++0x also allows a value of std::nullptr_t. | |||
6812 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&((ParamType->getPointeeType()->isIncompleteOrObjectType () && "Only object pointers allowed here") ? static_cast <void> (0) : __assert_fail ("ParamType->getPointeeType()->isIncompleteOrObjectType() && \"Only object pointers allowed here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6813, __PRETTY_FUNCTION__)) | |||
6813 | "Only object pointers allowed here")((ParamType->getPointeeType()->isIncompleteOrObjectType () && "Only object pointers allowed here") ? static_cast <void> (0) : __assert_fail ("ParamType->getPointeeType()->isIncompleteOrObjectType() && \"Only object pointers allowed here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6813, __PRETTY_FUNCTION__)); | |||
6814 | ||||
6815 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | |||
6816 | ParamType, | |||
6817 | Arg, Converted)) | |||
6818 | return ExprError(); | |||
6819 | return Arg; | |||
6820 | } | |||
6821 | ||||
6822 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { | |||
6823 | // -- For a non-type template-parameter of type reference to | |||
6824 | // object, no conversions apply. The type referred to by the | |||
6825 | // reference may be more cv-qualified than the (otherwise | |||
6826 | // identical) type of the template-argument. The | |||
6827 | // template-parameter is bound directly to the | |||
6828 | // template-argument, which must be an lvalue. | |||
6829 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&((ParamRefType->getPointeeType()->isIncompleteOrObjectType () && "Only object references allowed here") ? static_cast <void> (0) : __assert_fail ("ParamRefType->getPointeeType()->isIncompleteOrObjectType() && \"Only object references allowed here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6830, __PRETTY_FUNCTION__)) | |||
6830 | "Only object references allowed here")((ParamRefType->getPointeeType()->isIncompleteOrObjectType () && "Only object references allowed here") ? static_cast <void> (0) : __assert_fail ("ParamRefType->getPointeeType()->isIncompleteOrObjectType() && \"Only object references allowed here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6830, __PRETTY_FUNCTION__)); | |||
6831 | ||||
6832 | if (Arg->getType() == Context.OverloadTy) { | |||
6833 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, | |||
6834 | ParamRefType->getPointeeType(), | |||
6835 | true, | |||
6836 | FoundResult)) { | |||
6837 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) | |||
6838 | return ExprError(); | |||
6839 | ||||
6840 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); | |||
6841 | ArgType = Arg->getType(); | |||
6842 | } else | |||
6843 | return ExprError(); | |||
6844 | } | |||
6845 | ||||
6846 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | |||
6847 | ParamType, | |||
6848 | Arg, Converted)) | |||
6849 | return ExprError(); | |||
6850 | return Arg; | |||
6851 | } | |||
6852 | ||||
6853 | // Deal with parameters of type std::nullptr_t. | |||
6854 | if (ParamType->isNullPtrType()) { | |||
6855 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | |||
6856 | Converted = TemplateArgument(Arg); | |||
6857 | return Arg; | |||
6858 | } | |||
6859 | ||||
6860 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { | |||
6861 | case NPV_NotNullPointer: | |||
6862 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) | |||
6863 | << Arg->getType() << ParamType; | |||
6864 | Diag(Param->getLocation(), diag::note_template_param_here); | |||
6865 | return ExprError(); | |||
6866 | ||||
6867 | case NPV_Error: | |||
6868 | return ExprError(); | |||
6869 | ||||
6870 | case NPV_NullPointer: | |||
6871 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | |||
6872 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), | |||
6873 | /*isNullPtr*/true); | |||
6874 | return Arg; | |||
6875 | } | |||
6876 | } | |||
6877 | ||||
6878 | // -- For a non-type template-parameter of type pointer to data | |||
6879 | // member, qualification conversions (4.4) are applied. | |||
6880 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain")((ParamType->isMemberPointerType() && "Only pointers to members remain" ) ? static_cast<void> (0) : __assert_fail ("ParamType->isMemberPointerType() && \"Only pointers to members remain\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6880, __PRETTY_FUNCTION__)); | |||
6881 | ||||
6882 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, | |||
6883 | Converted)) | |||
6884 | return ExprError(); | |||
6885 | return Arg; | |||
6886 | } | |||
6887 | ||||
6888 | static void DiagnoseTemplateParameterListArityMismatch( | |||
6889 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, | |||
6890 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); | |||
6891 | ||||
6892 | /// Check a template argument against its corresponding | |||
6893 | /// template template parameter. | |||
6894 | /// | |||
6895 | /// This routine implements the semantics of C++ [temp.arg.template]. | |||
6896 | /// It returns true if an error occurred, and false otherwise. | |||
6897 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, | |||
6898 | TemplateArgumentLoc &Arg) { | |||
6899 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); | |||
6900 | TemplateDecl *Template = Name.getAsTemplateDecl(); | |||
6901 | if (!Template) { | |||
6902 | // Any dependent template name is fine. | |||
6903 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?")((Name.isDependent() && "Non-dependent template isn't a declaration?" ) ? static_cast<void> (0) : __assert_fail ("Name.isDependent() && \"Non-dependent template isn't a declaration?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6903, __PRETTY_FUNCTION__)); | |||
6904 | return false; | |||
6905 | } | |||
6906 | ||||
6907 | if (Template->isInvalidDecl()) | |||
6908 | return true; | |||
6909 | ||||
6910 | // C++0x [temp.arg.template]p1: | |||
6911 | // A template-argument for a template template-parameter shall be | |||
6912 | // the name of a class template or an alias template, expressed as an | |||
6913 | // id-expression. When the template-argument names a class template, only | |||
6914 | // primary class templates are considered when matching the | |||
6915 | // template template argument with the corresponding parameter; | |||
6916 | // partial specializations are not considered even if their | |||
6917 | // parameter lists match that of the template template parameter. | |||
6918 | // | |||
6919 | // Note that we also allow template template parameters here, which | |||
6920 | // will happen when we are dealing with, e.g., class template | |||
6921 | // partial specializations. | |||
6922 | if (!isa<ClassTemplateDecl>(Template) && | |||
6923 | !isa<TemplateTemplateParmDecl>(Template) && | |||
6924 | !isa<TypeAliasTemplateDecl>(Template) && | |||
6925 | !isa<BuiltinTemplateDecl>(Template)) { | |||
6926 | assert(isa<FunctionTemplateDecl>(Template) &&((isa<FunctionTemplateDecl>(Template) && "Only function templates are possible here" ) ? static_cast<void> (0) : __assert_fail ("isa<FunctionTemplateDecl>(Template) && \"Only function templates are possible here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6927, __PRETTY_FUNCTION__)) | |||
6927 | "Only function templates are possible here")((isa<FunctionTemplateDecl>(Template) && "Only function templates are possible here" ) ? static_cast<void> (0) : __assert_fail ("isa<FunctionTemplateDecl>(Template) && \"Only function templates are possible here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6927, __PRETTY_FUNCTION__)); | |||
6928 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); | |||
6929 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) | |||
6930 | << Template; | |||
6931 | } | |||
6932 | ||||
6933 | // C++1z [temp.arg.template]p3: (DR 150) | |||
6934 | // A template-argument matches a template template-parameter P when P | |||
6935 | // is at least as specialized as the template-argument A. | |||
6936 | if (getLangOpts().RelaxedTemplateTemplateArgs) { | |||
6937 | // Quick check for the common case: | |||
6938 | // If P contains a parameter pack, then A [...] matches P if each of A's | |||
6939 | // template parameters matches the corresponding template parameter in | |||
6940 | // the template-parameter-list of P. | |||
6941 | if (TemplateParameterListsAreEqual( | |||
6942 | Template->getTemplateParameters(), Params, false, | |||
6943 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) | |||
6944 | return false; | |||
6945 | ||||
6946 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, | |||
6947 | Arg.getLocation())) | |||
6948 | return false; | |||
6949 | // FIXME: Produce better diagnostics for deduction failures. | |||
6950 | } | |||
6951 | ||||
6952 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), | |||
6953 | Params, | |||
6954 | true, | |||
6955 | TPL_TemplateTemplateArgumentMatch, | |||
6956 | Arg.getLocation()); | |||
6957 | } | |||
6958 | ||||
6959 | /// Given a non-type template argument that refers to a | |||
6960 | /// declaration and the type of its corresponding non-type template | |||
6961 | /// parameter, produce an expression that properly refers to that | |||
6962 | /// declaration. | |||
6963 | ExprResult | |||
6964 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, | |||
6965 | QualType ParamType, | |||
6966 | SourceLocation Loc) { | |||
6967 | // C++ [temp.param]p8: | |||
6968 | // | |||
6969 | // A non-type template-parameter of type "array of T" or | |||
6970 | // "function returning T" is adjusted to be of type "pointer to | |||
6971 | // T" or "pointer to function returning T", respectively. | |||
6972 | if (ParamType->isArrayType()) | |||
6973 | ParamType = Context.getArrayDecayedType(ParamType); | |||
6974 | else if (ParamType->isFunctionType()) | |||
6975 | ParamType = Context.getPointerType(ParamType); | |||
6976 | ||||
6977 | // For a NULL non-type template argument, return nullptr casted to the | |||
6978 | // parameter's type. | |||
6979 | if (Arg.getKind() == TemplateArgument::NullPtr) { | |||
6980 | return ImpCastExprToType( | |||
6981 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), | |||
6982 | ParamType, | |||
6983 | ParamType->getAs<MemberPointerType>() | |||
6984 | ? CK_NullToMemberPointer | |||
6985 | : CK_NullToPointer); | |||
6986 | } | |||
6987 | assert(Arg.getKind() == TemplateArgument::Declaration &&((Arg.getKind() == TemplateArgument::Declaration && "Only declaration template arguments permitted here" ) ? static_cast<void> (0) : __assert_fail ("Arg.getKind() == TemplateArgument::Declaration && \"Only declaration template arguments permitted here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6988, __PRETTY_FUNCTION__)) | |||
6988 | "Only declaration template arguments permitted here")((Arg.getKind() == TemplateArgument::Declaration && "Only declaration template arguments permitted here" ) ? static_cast<void> (0) : __assert_fail ("Arg.getKind() == TemplateArgument::Declaration && \"Only declaration template arguments permitted here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6988, __PRETTY_FUNCTION__)); | |||
6989 | ||||
6990 | ValueDecl *VD = Arg.getAsDecl(); | |||
6991 | ||||
6992 | CXXScopeSpec SS; | |||
6993 | if (ParamType->isMemberPointerType()) { | |||
6994 | // If this is a pointer to member, we need to use a qualified name to | |||
6995 | // form a suitable pointer-to-member constant. | |||
6996 | assert(VD->getDeclContext()->isRecord() &&((VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl >(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl >(VD))) ? static_cast<void> (0) : __assert_fail ("VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD))" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6998, __PRETTY_FUNCTION__)) | |||
6997 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) ||((VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl >(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl >(VD))) ? static_cast<void> (0) : __assert_fail ("VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD))" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6998, __PRETTY_FUNCTION__)) | |||
6998 | isa<IndirectFieldDecl>(VD)))((VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl >(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl >(VD))) ? static_cast<void> (0) : __assert_fail ("VD->getDeclContext()->isRecord() && (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD))" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 6998, __PRETTY_FUNCTION__)); | |||
6999 | QualType ClassType | |||
7000 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); | |||
7001 | NestedNameSpecifier *Qualifier | |||
7002 | = NestedNameSpecifier::Create(Context, nullptr, false, | |||
7003 | ClassType.getTypePtr()); | |||
7004 | SS.MakeTrivial(Context, Qualifier, Loc); | |||
7005 | } | |||
7006 | ||||
7007 | ExprResult RefExpr = BuildDeclarationNameExpr( | |||
7008 | SS, DeclarationNameInfo(VD->getDeclName(), Loc), VD); | |||
7009 | if (RefExpr.isInvalid()) | |||
7010 | return ExprError(); | |||
7011 | ||||
7012 | // For a pointer, the argument declaration is the pointee. Take its address. | |||
7013 | QualType ElemT(RefExpr.get()->getType()->getArrayElementTypeNoTypeQual(), 0); | |||
7014 | if (ParamType->isPointerType() && !ElemT.isNull() && | |||
7015 | Context.hasSimilarType(ElemT, ParamType->getPointeeType())) { | |||
7016 | // Decay an array argument if we want a pointer to its first element. | |||
7017 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); | |||
7018 | if (RefExpr.isInvalid()) | |||
7019 | return ExprError(); | |||
7020 | } else if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { | |||
7021 | // For any other pointer, take the address (or form a pointer-to-member). | |||
7022 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); | |||
7023 | if (RefExpr.isInvalid()) | |||
7024 | return ExprError(); | |||
7025 | } else { | |||
7026 | assert(ParamType->isReferenceType() &&((ParamType->isReferenceType() && "unexpected type for decl template argument" ) ? static_cast<void> (0) : __assert_fail ("ParamType->isReferenceType() && \"unexpected type for decl template argument\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7027, __PRETTY_FUNCTION__)) | |||
7027 | "unexpected type for decl template argument")((ParamType->isReferenceType() && "unexpected type for decl template argument" ) ? static_cast<void> (0) : __assert_fail ("ParamType->isReferenceType() && \"unexpected type for decl template argument\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7027, __PRETTY_FUNCTION__)); | |||
7028 | } | |||
7029 | ||||
7030 | // At this point we should have the right value category. | |||
7031 | assert(ParamType->isReferenceType() == RefExpr.get()->isLValue() &&((ParamType->isReferenceType() == RefExpr.get()->isLValue () && "value kind mismatch for non-type template argument" ) ? static_cast<void> (0) : __assert_fail ("ParamType->isReferenceType() == RefExpr.get()->isLValue() && \"value kind mismatch for non-type template argument\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7032, __PRETTY_FUNCTION__)) | |||
7032 | "value kind mismatch for non-type template argument")((ParamType->isReferenceType() == RefExpr.get()->isLValue () && "value kind mismatch for non-type template argument" ) ? static_cast<void> (0) : __assert_fail ("ParamType->isReferenceType() == RefExpr.get()->isLValue() && \"value kind mismatch for non-type template argument\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7032, __PRETTY_FUNCTION__)); | |||
7033 | ||||
7034 | // The type of the template parameter can differ from the type of the | |||
7035 | // argument in various ways; convert it now if necessary. | |||
7036 | QualType DestExprType = ParamType.getNonLValueExprType(Context); | |||
7037 | if (!Context.hasSameType(RefExpr.get()->getType(), DestExprType)) { | |||
7038 | CastKind CK; | |||
7039 | QualType Ignored; | |||
7040 | if (Context.hasSimilarType(RefExpr.get()->getType(), DestExprType) || | |||
7041 | IsFunctionConversion(RefExpr.get()->getType(), DestExprType, Ignored)) { | |||
7042 | CK = CK_NoOp; | |||
7043 | } else if (ParamType->isVoidPointerType() && | |||
7044 | RefExpr.get()->getType()->isPointerType()) { | |||
7045 | CK = CK_BitCast; | |||
7046 | } else { | |||
7047 | // FIXME: Pointers to members can need conversion derived-to-base or | |||
7048 | // base-to-derived conversions. We currently don't retain enough | |||
7049 | // information to convert properly (we need to track a cast path or | |||
7050 | // subobject number in the template argument). | |||
7051 | llvm_unreachable(::llvm::llvm_unreachable_internal("unexpected conversion required for non-type template argument" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7052) | |||
7052 | "unexpected conversion required for non-type template argument")::llvm::llvm_unreachable_internal("unexpected conversion required for non-type template argument" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7052); | |||
7053 | } | |||
7054 | RefExpr = ImpCastExprToType(RefExpr.get(), DestExprType, CK, | |||
7055 | RefExpr.get()->getValueKind()); | |||
7056 | } | |||
7057 | ||||
7058 | return RefExpr; | |||
7059 | } | |||
7060 | ||||
7061 | /// Construct a new expression that refers to the given | |||
7062 | /// integral template argument with the given source-location | |||
7063 | /// information. | |||
7064 | /// | |||
7065 | /// This routine takes care of the mapping from an integral template | |||
7066 | /// argument (which may have any integral type) to the appropriate | |||
7067 | /// literal value. | |||
7068 | ExprResult | |||
7069 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, | |||
7070 | SourceLocation Loc) { | |||
7071 | assert(Arg.getKind() == TemplateArgument::Integral &&((Arg.getKind() == TemplateArgument::Integral && "Operation is only valid for integral template arguments" ) ? static_cast<void> (0) : __assert_fail ("Arg.getKind() == TemplateArgument::Integral && \"Operation is only valid for integral template arguments\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7072, __PRETTY_FUNCTION__)) | |||
7072 | "Operation is only valid for integral template arguments")((Arg.getKind() == TemplateArgument::Integral && "Operation is only valid for integral template arguments" ) ? static_cast<void> (0) : __assert_fail ("Arg.getKind() == TemplateArgument::Integral && \"Operation is only valid for integral template arguments\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7072, __PRETTY_FUNCTION__)); | |||
7073 | QualType OrigT = Arg.getIntegralType(); | |||
7074 | ||||
7075 | // If this is an enum type that we're instantiating, we need to use an integer | |||
7076 | // type the same size as the enumerator. We don't want to build an | |||
7077 | // IntegerLiteral with enum type. The integer type of an enum type can be of | |||
7078 | // any integral type with C++11 enum classes, make sure we create the right | |||
7079 | // type of literal for it. | |||
7080 | QualType T = OrigT; | |||
7081 | if (const EnumType *ET = OrigT->getAs<EnumType>()) | |||
7082 | T = ET->getDecl()->getIntegerType(); | |||
7083 | ||||
7084 | Expr *E; | |||
7085 | if (T->isAnyCharacterType()) { | |||
7086 | CharacterLiteral::CharacterKind Kind; | |||
7087 | if (T->isWideCharType()) | |||
7088 | Kind = CharacterLiteral::Wide; | |||
7089 | else if (T->isChar8Type() && getLangOpts().Char8) | |||
7090 | Kind = CharacterLiteral::UTF8; | |||
7091 | else if (T->isChar16Type()) | |||
7092 | Kind = CharacterLiteral::UTF16; | |||
7093 | else if (T->isChar32Type()) | |||
7094 | Kind = CharacterLiteral::UTF32; | |||
7095 | else | |||
7096 | Kind = CharacterLiteral::Ascii; | |||
7097 | ||||
7098 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), | |||
7099 | Kind, T, Loc); | |||
7100 | } else if (T->isBooleanType()) { | |||
7101 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), | |||
7102 | T, Loc); | |||
7103 | } else if (T->isNullPtrType()) { | |||
7104 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); | |||
7105 | } else { | |||
7106 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); | |||
7107 | } | |||
7108 | ||||
7109 | if (OrigT->isEnumeralType()) { | |||
7110 | // FIXME: This is a hack. We need a better way to handle substituted | |||
7111 | // non-type template parameters. | |||
7112 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, | |||
7113 | nullptr, | |||
7114 | Context.getTrivialTypeSourceInfo(OrigT, Loc), | |||
7115 | Loc, Loc); | |||
7116 | } | |||
7117 | ||||
7118 | return E; | |||
7119 | } | |||
7120 | ||||
7121 | /// Match two template parameters within template parameter lists. | |||
7122 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, | |||
7123 | bool Complain, | |||
7124 | Sema::TemplateParameterListEqualKind Kind, | |||
7125 | SourceLocation TemplateArgLoc) { | |||
7126 | // TODO: Concepts: Check constrained-parameter constraints here. | |||
7127 | // Check the actual kind (type, non-type, template). | |||
7128 | if (Old->getKind() != New->getKind()) { | |||
7129 | if (Complain) { | |||
7130 | unsigned NextDiag = diag::err_template_param_different_kind; | |||
7131 | if (TemplateArgLoc.isValid()) { | |||
7132 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); | |||
7133 | NextDiag = diag::note_template_param_different_kind; | |||
7134 | } | |||
7135 | S.Diag(New->getLocation(), NextDiag) | |||
7136 | << (Kind != Sema::TPL_TemplateMatch); | |||
7137 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) | |||
7138 | << (Kind != Sema::TPL_TemplateMatch); | |||
7139 | } | |||
7140 | ||||
7141 | return false; | |||
7142 | } | |||
7143 | ||||
7144 | // Check that both are parameter packs or neither are parameter packs. | |||
7145 | // However, if we are matching a template template argument to a | |||
7146 | // template template parameter, the template template parameter can have | |||
7147 | // a parameter pack where the template template argument does not. | |||
7148 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && | |||
7149 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && | |||
7150 | Old->isTemplateParameterPack())) { | |||
7151 | if (Complain) { | |||
7152 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; | |||
7153 | if (TemplateArgLoc.isValid()) { | |||
7154 | S.Diag(TemplateArgLoc, | |||
7155 | diag::err_template_arg_template_params_mismatch); | |||
7156 | NextDiag = diag::note_template_parameter_pack_non_pack; | |||
7157 | } | |||
7158 | ||||
7159 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 | |||
7160 | : isa<NonTypeTemplateParmDecl>(New)? 1 | |||
7161 | : 2; | |||
7162 | S.Diag(New->getLocation(), NextDiag) | |||
7163 | << ParamKind << New->isParameterPack(); | |||
7164 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) | |||
7165 | << ParamKind << Old->isParameterPack(); | |||
7166 | } | |||
7167 | ||||
7168 | return false; | |||
7169 | } | |||
7170 | ||||
7171 | // For non-type template parameters, check the type of the parameter. | |||
7172 | if (NonTypeTemplateParmDecl *OldNTTP | |||
7173 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { | |||
7174 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); | |||
7175 | ||||
7176 | // If we are matching a template template argument to a template | |||
7177 | // template parameter and one of the non-type template parameter types | |||
7178 | // is dependent, then we must wait until template instantiation time | |||
7179 | // to actually compare the arguments. | |||
7180 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && | |||
7181 | (OldNTTP->getType()->isDependentType() || | |||
7182 | NewNTTP->getType()->isDependentType())) | |||
7183 | return true; | |||
7184 | ||||
7185 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { | |||
7186 | if (Complain) { | |||
7187 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; | |||
7188 | if (TemplateArgLoc.isValid()) { | |||
7189 | S.Diag(TemplateArgLoc, | |||
7190 | diag::err_template_arg_template_params_mismatch); | |||
7191 | NextDiag = diag::note_template_nontype_parm_different_type; | |||
7192 | } | |||
7193 | S.Diag(NewNTTP->getLocation(), NextDiag) | |||
7194 | << NewNTTP->getType() | |||
7195 | << (Kind != Sema::TPL_TemplateMatch); | |||
7196 | S.Diag(OldNTTP->getLocation(), | |||
7197 | diag::note_template_nontype_parm_prev_declaration) | |||
7198 | << OldNTTP->getType(); | |||
7199 | } | |||
7200 | ||||
7201 | return false; | |||
7202 | } | |||
7203 | ||||
7204 | return true; | |||
7205 | } | |||
7206 | ||||
7207 | // For template template parameters, check the template parameter types. | |||
7208 | // The template parameter lists of template template | |||
7209 | // parameters must agree. | |||
7210 | if (TemplateTemplateParmDecl *OldTTP | |||
7211 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { | |||
7212 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); | |||
7213 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), | |||
7214 | OldTTP->getTemplateParameters(), | |||
7215 | Complain, | |||
7216 | (Kind == Sema::TPL_TemplateMatch | |||
7217 | ? Sema::TPL_TemplateTemplateParmMatch | |||
7218 | : Kind), | |||
7219 | TemplateArgLoc); | |||
7220 | } | |||
7221 | ||||
7222 | // TODO: Concepts: Match immediately-introduced-constraint for type | |||
7223 | // constraints | |||
7224 | ||||
7225 | return true; | |||
7226 | } | |||
7227 | ||||
7228 | /// Diagnose a known arity mismatch when comparing template argument | |||
7229 | /// lists. | |||
7230 | static | |||
7231 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, | |||
7232 | TemplateParameterList *New, | |||
7233 | TemplateParameterList *Old, | |||
7234 | Sema::TemplateParameterListEqualKind Kind, | |||
7235 | SourceLocation TemplateArgLoc) { | |||
7236 | unsigned NextDiag = diag::err_template_param_list_different_arity; | |||
7237 | if (TemplateArgLoc.isValid()) { | |||
7238 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); | |||
7239 | NextDiag = diag::note_template_param_list_different_arity; | |||
7240 | } | |||
7241 | S.Diag(New->getTemplateLoc(), NextDiag) | |||
7242 | << (New->size() > Old->size()) | |||
7243 | << (Kind != Sema::TPL_TemplateMatch) | |||
7244 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); | |||
7245 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) | |||
7246 | << (Kind != Sema::TPL_TemplateMatch) | |||
7247 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); | |||
7248 | } | |||
7249 | ||||
7250 | static void | |||
7251 | DiagnoseTemplateParameterListRequiresClauseMismatch(Sema &S, | |||
7252 | TemplateParameterList *New, | |||
7253 | TemplateParameterList *Old){ | |||
7254 | S.Diag(New->getTemplateLoc(), diag::err_template_different_requires_clause); | |||
7255 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) | |||
7256 | << /*declaration*/0; | |||
7257 | } | |||
7258 | ||||
7259 | /// Determine whether the given template parameter lists are | |||
7260 | /// equivalent. | |||
7261 | /// | |||
7262 | /// \param New The new template parameter list, typically written in the | |||
7263 | /// source code as part of a new template declaration. | |||
7264 | /// | |||
7265 | /// \param Old The old template parameter list, typically found via | |||
7266 | /// name lookup of the template declared with this template parameter | |||
7267 | /// list. | |||
7268 | /// | |||
7269 | /// \param Complain If true, this routine will produce a diagnostic if | |||
7270 | /// the template parameter lists are not equivalent. | |||
7271 | /// | |||
7272 | /// \param Kind describes how we are to match the template parameter lists. | |||
7273 | /// | |||
7274 | /// \param TemplateArgLoc If this source location is valid, then we | |||
7275 | /// are actually checking the template parameter list of a template | |||
7276 | /// argument (New) against the template parameter list of its | |||
7277 | /// corresponding template template parameter (Old). We produce | |||
7278 | /// slightly different diagnostics in this scenario. | |||
7279 | /// | |||
7280 | /// \returns True if the template parameter lists are equal, false | |||
7281 | /// otherwise. | |||
7282 | bool | |||
7283 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, | |||
7284 | TemplateParameterList *Old, | |||
7285 | bool Complain, | |||
7286 | TemplateParameterListEqualKind Kind, | |||
7287 | SourceLocation TemplateArgLoc) { | |||
7288 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { | |||
7289 | if (Complain) | |||
7290 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | |||
7291 | TemplateArgLoc); | |||
7292 | ||||
7293 | return false; | |||
7294 | } | |||
7295 | ||||
7296 | // C++0x [temp.arg.template]p3: | |||
7297 | // A template-argument matches a template template-parameter (call it P) | |||
7298 | // when each of the template parameters in the template-parameter-list of | |||
7299 | // the template-argument's corresponding class template or alias template | |||
7300 | // (call it A) matches the corresponding template parameter in the | |||
7301 | // template-parameter-list of P. [...] | |||
7302 | TemplateParameterList::iterator NewParm = New->begin(); | |||
7303 | TemplateParameterList::iterator NewParmEnd = New->end(); | |||
7304 | for (TemplateParameterList::iterator OldParm = Old->begin(), | |||
7305 | OldParmEnd = Old->end(); | |||
7306 | OldParm != OldParmEnd; ++OldParm) { | |||
7307 | if (Kind != TPL_TemplateTemplateArgumentMatch || | |||
7308 | !(*OldParm)->isTemplateParameterPack()) { | |||
7309 | if (NewParm == NewParmEnd) { | |||
7310 | if (Complain) | |||
7311 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | |||
7312 | TemplateArgLoc); | |||
7313 | ||||
7314 | return false; | |||
7315 | } | |||
7316 | ||||
7317 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, | |||
7318 | Kind, TemplateArgLoc)) | |||
7319 | return false; | |||
7320 | ||||
7321 | ++NewParm; | |||
7322 | continue; | |||
7323 | } | |||
7324 | ||||
7325 | // C++0x [temp.arg.template]p3: | |||
7326 | // [...] When P's template- parameter-list contains a template parameter | |||
7327 | // pack (14.5.3), the template parameter pack will match zero or more | |||
7328 | // template parameters or template parameter packs in the | |||
7329 | // template-parameter-list of A with the same type and form as the | |||
7330 | // template parameter pack in P (ignoring whether those template | |||
7331 | // parameters are template parameter packs). | |||
7332 | for (; NewParm != NewParmEnd; ++NewParm) { | |||
7333 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, | |||
7334 | Kind, TemplateArgLoc)) | |||
7335 | return false; | |||
7336 | } | |||
7337 | } | |||
7338 | ||||
7339 | // Make sure we exhausted all of the arguments. | |||
7340 | if (NewParm != NewParmEnd) { | |||
7341 | if (Complain) | |||
7342 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | |||
7343 | TemplateArgLoc); | |||
7344 | ||||
7345 | return false; | |||
7346 | } | |||
7347 | ||||
7348 | if (Kind != TPL_TemplateTemplateArgumentMatch) { | |||
7349 | const Expr *NewRC = New->getRequiresClause(); | |||
7350 | const Expr *OldRC = Old->getRequiresClause(); | |||
7351 | if (!NewRC != !OldRC) { | |||
7352 | if (Complain) | |||
7353 | DiagnoseTemplateParameterListRequiresClauseMismatch(*this, New, Old); | |||
7354 | return false; | |||
7355 | } | |||
7356 | ||||
7357 | if (NewRC) { | |||
7358 | llvm::FoldingSetNodeID OldRCID, NewRCID; | |||
7359 | OldRC->Profile(OldRCID, Context, /*Canonical=*/true); | |||
7360 | NewRC->Profile(NewRCID, Context, /*Canonical=*/true); | |||
7361 | if (OldRCID != NewRCID) { | |||
7362 | if (Complain) | |||
7363 | DiagnoseTemplateParameterListRequiresClauseMismatch(*this, New, Old); | |||
7364 | return false; | |||
7365 | } | |||
7366 | } | |||
7367 | } | |||
7368 | ||||
7369 | return true; | |||
7370 | } | |||
7371 | ||||
7372 | /// Check whether a template can be declared within this scope. | |||
7373 | /// | |||
7374 | /// If the template declaration is valid in this scope, returns | |||
7375 | /// false. Otherwise, issues a diagnostic and returns true. | |||
7376 | bool | |||
7377 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { | |||
7378 | if (!S) | |||
7379 | return false; | |||
7380 | ||||
7381 | // Find the nearest enclosing declaration scope. | |||
7382 | while ((S->getFlags() & Scope::DeclScope) == 0 || | |||
7383 | (S->getFlags() & Scope::TemplateParamScope) != 0) | |||
7384 | S = S->getParent(); | |||
7385 | ||||
7386 | // C++ [temp]p4: | |||
7387 | // A template [...] shall not have C linkage. | |||
7388 | DeclContext *Ctx = S->getEntity(); | |||
7389 | if (Ctx && Ctx->isExternCContext()) { | |||
7390 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) | |||
7391 | << TemplateParams->getSourceRange(); | |||
7392 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) | |||
7393 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); | |||
7394 | return true; | |||
7395 | } | |||
7396 | Ctx = Ctx->getRedeclContext(); | |||
| ||||
7397 | ||||
7398 | // C++ [temp]p2: | |||
7399 | // A template-declaration can appear only as a namespace scope or | |||
7400 | // class scope declaration. | |||
7401 | if (Ctx) { | |||
7402 | if (Ctx->isFileContext()) | |||
7403 | return false; | |||
7404 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { | |||
7405 | // C++ [temp.mem]p2: | |||
7406 | // A local class shall not have member templates. | |||
7407 | if (RD->isLocalClass()) | |||
7408 | return Diag(TemplateParams->getTemplateLoc(), | |||
7409 | diag::err_template_inside_local_class) | |||
7410 | << TemplateParams->getSourceRange(); | |||
7411 | else | |||
7412 | return false; | |||
7413 | } | |||
7414 | } | |||
7415 | ||||
7416 | return Diag(TemplateParams->getTemplateLoc(), | |||
7417 | diag::err_template_outside_namespace_or_class_scope) | |||
7418 | << TemplateParams->getSourceRange(); | |||
7419 | } | |||
7420 | ||||
7421 | /// Determine what kind of template specialization the given declaration | |||
7422 | /// is. | |||
7423 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { | |||
7424 | if (!D) | |||
7425 | return TSK_Undeclared; | |||
7426 | ||||
7427 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) | |||
7428 | return Record->getTemplateSpecializationKind(); | |||
7429 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) | |||
7430 | return Function->getTemplateSpecializationKind(); | |||
7431 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) | |||
7432 | return Var->getTemplateSpecializationKind(); | |||
7433 | ||||
7434 | return TSK_Undeclared; | |||
7435 | } | |||
7436 | ||||
7437 | /// Check whether a specialization is well-formed in the current | |||
7438 | /// context. | |||
7439 | /// | |||
7440 | /// This routine determines whether a template specialization can be declared | |||
7441 | /// in the current context (C++ [temp.expl.spec]p2). | |||
7442 | /// | |||
7443 | /// \param S the semantic analysis object for which this check is being | |||
7444 | /// performed. | |||
7445 | /// | |||
7446 | /// \param Specialized the entity being specialized or instantiated, which | |||
7447 | /// may be a kind of template (class template, function template, etc.) or | |||
7448 | /// a member of a class template (member function, static data member, | |||
7449 | /// member class). | |||
7450 | /// | |||
7451 | /// \param PrevDecl the previous declaration of this entity, if any. | |||
7452 | /// | |||
7453 | /// \param Loc the location of the explicit specialization or instantiation of | |||
7454 | /// this entity. | |||
7455 | /// | |||
7456 | /// \param IsPartialSpecialization whether this is a partial specialization of | |||
7457 | /// a class template. | |||
7458 | /// | |||
7459 | /// \returns true if there was an error that we cannot recover from, false | |||
7460 | /// otherwise. | |||
7461 | static bool CheckTemplateSpecializationScope(Sema &S, | |||
7462 | NamedDecl *Specialized, | |||
7463 | NamedDecl *PrevDecl, | |||
7464 | SourceLocation Loc, | |||
7465 | bool IsPartialSpecialization) { | |||
7466 | // Keep these "kind" numbers in sync with the %select statements in the | |||
7467 | // various diagnostics emitted by this routine. | |||
7468 | int EntityKind = 0; | |||
7469 | if (isa<ClassTemplateDecl>(Specialized)) | |||
7470 | EntityKind = IsPartialSpecialization? 1 : 0; | |||
7471 | else if (isa<VarTemplateDecl>(Specialized)) | |||
7472 | EntityKind = IsPartialSpecialization ? 3 : 2; | |||
7473 | else if (isa<FunctionTemplateDecl>(Specialized)) | |||
7474 | EntityKind = 4; | |||
7475 | else if (isa<CXXMethodDecl>(Specialized)) | |||
7476 | EntityKind = 5; | |||
7477 | else if (isa<VarDecl>(Specialized)) | |||
7478 | EntityKind = 6; | |||
7479 | else if (isa<RecordDecl>(Specialized)) | |||
7480 | EntityKind = 7; | |||
7481 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) | |||
7482 | EntityKind = 8; | |||
7483 | else { | |||
7484 | S.Diag(Loc, diag::err_template_spec_unknown_kind) | |||
7485 | << S.getLangOpts().CPlusPlus11; | |||
7486 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); | |||
7487 | return true; | |||
7488 | } | |||
7489 | ||||
7490 | // C++ [temp.expl.spec]p2: | |||
7491 | // An explicit specialization may be declared in any scope in which | |||
7492 | // the corresponding primary template may be defined. | |||
7493 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { | |||
7494 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) | |||
7495 | << Specialized; | |||
7496 | return true; | |||
7497 | } | |||
7498 | ||||
7499 | // C++ [temp.class.spec]p6: | |||
7500 | // A class template partial specialization may be declared in any | |||
7501 | // scope in which the primary template may be defined. | |||
7502 | DeclContext *SpecializedContext = | |||
7503 | Specialized->getDeclContext()->getRedeclContext(); | |||
7504 | DeclContext *DC = S.CurContext->getRedeclContext(); | |||
7505 | ||||
7506 | // Make sure that this redeclaration (or definition) occurs in the same | |||
7507 | // scope or an enclosing namespace. | |||
7508 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) | |||
7509 | : DC->Equals(SpecializedContext))) { | |||
7510 | if (isa<TranslationUnitDecl>(SpecializedContext)) | |||
7511 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) | |||
7512 | << EntityKind << Specialized; | |||
7513 | else { | |||
7514 | auto *ND = cast<NamedDecl>(SpecializedContext); | |||
7515 | int Diag = diag::err_template_spec_redecl_out_of_scope; | |||
7516 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) | |||
7517 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; | |||
7518 | S.Diag(Loc, Diag) << EntityKind << Specialized | |||
7519 | << ND << isa<CXXRecordDecl>(ND); | |||
7520 | } | |||
7521 | ||||
7522 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); | |||
7523 | ||||
7524 | // Don't allow specializing in the wrong class during error recovery. | |||
7525 | // Otherwise, things can go horribly wrong. | |||
7526 | if (DC->isRecord()) | |||
7527 | return true; | |||
7528 | } | |||
7529 | ||||
7530 | return false; | |||
7531 | } | |||
7532 | ||||
7533 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { | |||
7534 | if (!E->isTypeDependent()) | |||
7535 | return SourceLocation(); | |||
7536 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); | |||
7537 | Checker.TraverseStmt(E); | |||
7538 | if (Checker.MatchLoc.isInvalid()) | |||
7539 | return E->getSourceRange(); | |||
7540 | return Checker.MatchLoc; | |||
7541 | } | |||
7542 | ||||
7543 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { | |||
7544 | if (!TL.getType()->isDependentType()) | |||
7545 | return SourceLocation(); | |||
7546 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); | |||
7547 | Checker.TraverseTypeLoc(TL); | |||
7548 | if (Checker.MatchLoc.isInvalid()) | |||
7549 | return TL.getSourceRange(); | |||
7550 | return Checker.MatchLoc; | |||
7551 | } | |||
7552 | ||||
7553 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs | |||
7554 | /// that checks non-type template partial specialization arguments. | |||
7555 | static bool CheckNonTypeTemplatePartialSpecializationArgs( | |||
7556 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, | |||
7557 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { | |||
7558 | for (unsigned I = 0; I != NumArgs; ++I) { | |||
7559 | if (Args[I].getKind() == TemplateArgument::Pack) { | |||
7560 | if (CheckNonTypeTemplatePartialSpecializationArgs( | |||
7561 | S, TemplateNameLoc, Param, Args[I].pack_begin(), | |||
7562 | Args[I].pack_size(), IsDefaultArgument)) | |||
7563 | return true; | |||
7564 | ||||
7565 | continue; | |||
7566 | } | |||
7567 | ||||
7568 | if (Args[I].getKind() != TemplateArgument::Expression) | |||
7569 | continue; | |||
7570 | ||||
7571 | Expr *ArgExpr = Args[I].getAsExpr(); | |||
7572 | ||||
7573 | // We can have a pack expansion of any of the bullets below. | |||
7574 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) | |||
7575 | ArgExpr = Expansion->getPattern(); | |||
7576 | ||||
7577 | // Strip off any implicit casts we added as part of type checking. | |||
7578 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) | |||
7579 | ArgExpr = ICE->getSubExpr(); | |||
7580 | ||||
7581 | // C++ [temp.class.spec]p8: | |||
7582 | // A non-type argument is non-specialized if it is the name of a | |||
7583 | // non-type parameter. All other non-type arguments are | |||
7584 | // specialized. | |||
7585 | // | |||
7586 | // Below, we check the two conditions that only apply to | |||
7587 | // specialized non-type arguments, so skip any non-specialized | |||
7588 | // arguments. | |||
7589 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) | |||
7590 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) | |||
7591 | continue; | |||
7592 | ||||
7593 | // C++ [temp.class.spec]p9: | |||
7594 | // Within the argument list of a class template partial | |||
7595 | // specialization, the following restrictions apply: | |||
7596 | // -- A partially specialized non-type argument expression | |||
7597 | // shall not involve a template parameter of the partial | |||
7598 | // specialization except when the argument expression is a | |||
7599 | // simple identifier. | |||
7600 | // -- The type of a template parameter corresponding to a | |||
7601 | // specialized non-type argument shall not be dependent on a | |||
7602 | // parameter of the specialization. | |||
7603 | // DR1315 removes the first bullet, leaving an incoherent set of rules. | |||
7604 | // We implement a compromise between the original rules and DR1315: | |||
7605 | // -- A specialized non-type template argument shall not be | |||
7606 | // type-dependent and the corresponding template parameter | |||
7607 | // shall have a non-dependent type. | |||
7608 | SourceRange ParamUseRange = | |||
7609 | findTemplateParameterInType(Param->getDepth(), ArgExpr); | |||
7610 | if (ParamUseRange.isValid()) { | |||
7611 | if (IsDefaultArgument) { | |||
7612 | S.Diag(TemplateNameLoc, | |||
7613 | diag::err_dependent_non_type_arg_in_partial_spec); | |||
7614 | S.Diag(ParamUseRange.getBegin(), | |||
7615 | diag::note_dependent_non_type_default_arg_in_partial_spec) | |||
7616 | << ParamUseRange; | |||
7617 | } else { | |||
7618 | S.Diag(ParamUseRange.getBegin(), | |||
7619 | diag::err_dependent_non_type_arg_in_partial_spec) | |||
7620 | << ParamUseRange; | |||
7621 | } | |||
7622 | return true; | |||
7623 | } | |||
7624 | ||||
7625 | ParamUseRange = findTemplateParameter( | |||
7626 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); | |||
7627 | if (ParamUseRange.isValid()) { | |||
7628 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), | |||
7629 | diag::err_dependent_typed_non_type_arg_in_partial_spec) | |||
7630 | << Param->getType(); | |||
7631 | S.Diag(Param->getLocation(), diag::note_template_param_here) | |||
7632 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) | |||
7633 | << ParamUseRange; | |||
7634 | return true; | |||
7635 | } | |||
7636 | } | |||
7637 | ||||
7638 | return false; | |||
7639 | } | |||
7640 | ||||
7641 | /// Check the non-type template arguments of a class template | |||
7642 | /// partial specialization according to C++ [temp.class.spec]p9. | |||
7643 | /// | |||
7644 | /// \param TemplateNameLoc the location of the template name. | |||
7645 | /// \param PrimaryTemplate the template parameters of the primary class | |||
7646 | /// template. | |||
7647 | /// \param NumExplicit the number of explicitly-specified template arguments. | |||
7648 | /// \param TemplateArgs the template arguments of the class template | |||
7649 | /// partial specialization. | |||
7650 | /// | |||
7651 | /// \returns \c true if there was an error, \c false otherwise. | |||
7652 | bool Sema::CheckTemplatePartialSpecializationArgs( | |||
7653 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, | |||
7654 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { | |||
7655 | // We have to be conservative when checking a template in a dependent | |||
7656 | // context. | |||
7657 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) | |||
7658 | return false; | |||
7659 | ||||
7660 | TemplateParameterList *TemplateParams = | |||
7661 | PrimaryTemplate->getTemplateParameters(); | |||
7662 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { | |||
7663 | NonTypeTemplateParmDecl *Param | |||
7664 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); | |||
7665 | if (!Param) | |||
7666 | continue; | |||
7667 | ||||
7668 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, | |||
7669 | Param, &TemplateArgs[I], | |||
7670 | 1, I >= NumExplicit)) | |||
7671 | return true; | |||
7672 | } | |||
7673 | ||||
7674 | return false; | |||
7675 | } | |||
7676 | ||||
7677 | DeclResult Sema::ActOnClassTemplateSpecialization( | |||
7678 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, | |||
7679 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, | |||
7680 | const ParsedAttributesView &Attr, | |||
7681 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { | |||
7682 | assert(TUK != TUK_Reference && "References are not specializations")((TUK != TUK_Reference && "References are not specializations" ) ? static_cast<void> (0) : __assert_fail ("TUK != TUK_Reference && \"References are not specializations\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7682, __PRETTY_FUNCTION__)); | |||
7683 | ||||
7684 | CXXScopeSpec &SS = TemplateId.SS; | |||
7685 | ||||
7686 | // NOTE: KWLoc is the location of the tag keyword. This will instead | |||
7687 | // store the location of the outermost template keyword in the declaration. | |||
7688 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 | |||
7689 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; | |||
7690 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; | |||
7691 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; | |||
7692 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; | |||
7693 | ||||
7694 | // Find the class template we're specializing | |||
7695 | TemplateName Name = TemplateId.Template.get(); | |||
7696 | ClassTemplateDecl *ClassTemplate | |||
7697 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); | |||
7698 | ||||
7699 | if (!ClassTemplate) { | |||
7700 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) | |||
7701 | << (Name.getAsTemplateDecl() && | |||
7702 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); | |||
7703 | return true; | |||
7704 | } | |||
7705 | ||||
7706 | bool isMemberSpecialization = false; | |||
7707 | bool isPartialSpecialization = false; | |||
7708 | ||||
7709 | // Check the validity of the template headers that introduce this | |||
7710 | // template. | |||
7711 | // FIXME: We probably shouldn't complain about these headers for | |||
7712 | // friend declarations. | |||
7713 | bool Invalid = false; | |||
7714 | TemplateParameterList *TemplateParams = | |||
7715 | MatchTemplateParametersToScopeSpecifier( | |||
7716 | KWLoc, TemplateNameLoc, SS, &TemplateId, | |||
7717 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, | |||
7718 | Invalid); | |||
7719 | if (Invalid) | |||
7720 | return true; | |||
7721 | ||||
7722 | if (TemplateParams && TemplateParams->size() > 0) { | |||
7723 | isPartialSpecialization = true; | |||
7724 | ||||
7725 | if (TUK == TUK_Friend) { | |||
7726 | Diag(KWLoc, diag::err_partial_specialization_friend) | |||
7727 | << SourceRange(LAngleLoc, RAngleLoc); | |||
7728 | return true; | |||
7729 | } | |||
7730 | ||||
7731 | // C++ [temp.class.spec]p10: | |||
7732 | // The template parameter list of a specialization shall not | |||
7733 | // contain default template argument values. | |||
7734 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { | |||
7735 | Decl *Param = TemplateParams->getParam(I); | |||
7736 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { | |||
7737 | if (TTP->hasDefaultArgument()) { | |||
7738 | Diag(TTP->getDefaultArgumentLoc(), | |||
7739 | diag::err_default_arg_in_partial_spec); | |||
7740 | TTP->removeDefaultArgument(); | |||
7741 | } | |||
7742 | } else if (NonTypeTemplateParmDecl *NTTP | |||
7743 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | |||
7744 | if (Expr *DefArg = NTTP->getDefaultArgument()) { | |||
7745 | Diag(NTTP->getDefaultArgumentLoc(), | |||
7746 | diag::err_default_arg_in_partial_spec) | |||
7747 | << DefArg->getSourceRange(); | |||
7748 | NTTP->removeDefaultArgument(); | |||
7749 | } | |||
7750 | } else { | |||
7751 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); | |||
7752 | if (TTP->hasDefaultArgument()) { | |||
7753 | Diag(TTP->getDefaultArgument().getLocation(), | |||
7754 | diag::err_default_arg_in_partial_spec) | |||
7755 | << TTP->getDefaultArgument().getSourceRange(); | |||
7756 | TTP->removeDefaultArgument(); | |||
7757 | } | |||
7758 | } | |||
7759 | } | |||
7760 | } else if (TemplateParams) { | |||
7761 | if (TUK == TUK_Friend) | |||
7762 | Diag(KWLoc, diag::err_template_spec_friend) | |||
7763 | << FixItHint::CreateRemoval( | |||
7764 | SourceRange(TemplateParams->getTemplateLoc(), | |||
7765 | TemplateParams->getRAngleLoc())) | |||
7766 | << SourceRange(LAngleLoc, RAngleLoc); | |||
7767 | } else { | |||
7768 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl")((TUK == TUK_Friend && "should have a 'template<>' for this decl" ) ? static_cast<void> (0) : __assert_fail ("TUK == TUK_Friend && \"should have a 'template<>' for this decl\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7768, __PRETTY_FUNCTION__)); | |||
7769 | } | |||
7770 | ||||
7771 | // Check that the specialization uses the same tag kind as the | |||
7772 | // original template. | |||
7773 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | |||
7774 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!")((Kind != TTK_Enum && "Invalid enum tag in class template spec!" ) ? static_cast<void> (0) : __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template spec!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 7774, __PRETTY_FUNCTION__)); | |||
7775 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), | |||
7776 | Kind, TUK == TUK_Definition, KWLoc, | |||
7777 | ClassTemplate->getIdentifier())) { | |||
7778 | Diag(KWLoc, diag::err_use_with_wrong_tag) | |||
7779 | << ClassTemplate | |||
7780 | << FixItHint::CreateReplacement(KWLoc, | |||
7781 | ClassTemplate->getTemplatedDecl()->getKindName()); | |||
7782 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), | |||
7783 | diag::note_previous_use); | |||
7784 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); | |||
7785 | } | |||
7786 | ||||
7787 | // Translate the parser's template argument list in our AST format. | |||
7788 | TemplateArgumentListInfo TemplateArgs = | |||
7789 | makeTemplateArgumentListInfo(*this, TemplateId); | |||
7790 | ||||
7791 | // Check for unexpanded parameter packs in any of the template arguments. | |||
7792 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | |||
7793 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], | |||
7794 | UPPC_PartialSpecialization)) | |||
7795 | return true; | |||
7796 | ||||
7797 | // Check that the template argument list is well-formed for this | |||
7798 | // template. | |||
7799 | SmallVector<TemplateArgument, 4> Converted; | |||
7800 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, | |||
7801 | TemplateArgs, false, Converted, | |||
7802 | /*UpdateArgsWithConversion=*/true)) | |||
7803 | return true; | |||
7804 | ||||
7805 | // Find the class template (partial) specialization declaration that | |||
7806 | // corresponds to these arguments. | |||
7807 | if (isPartialSpecialization) { | |||
7808 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, | |||
7809 | TemplateArgs.size(), Converted)) | |||
7810 | return true; | |||
7811 | ||||
7812 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we | |||
7813 | // also do it during instantiation. | |||
7814 | bool InstantiationDependent; | |||
7815 | if (!Name.isDependent() && | |||
7816 | !TemplateSpecializationType::anyDependentTemplateArguments( | |||
7817 | TemplateArgs.arguments(), InstantiationDependent)) { | |||
7818 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) | |||
7819 | << ClassTemplate->getDeclName(); | |||
7820 | isPartialSpecialization = false; | |||
7821 | } | |||
7822 | } | |||
7823 | ||||
7824 | void *InsertPos = nullptr; | |||
7825 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; | |||
7826 | ||||
7827 | if (isPartialSpecialization) | |||
7828 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, | |||
7829 | TemplateParams, | |||
7830 | InsertPos); | |||
7831 | else | |||
7832 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); | |||
7833 | ||||
7834 | ClassTemplateSpecializationDecl *Specialization = nullptr; | |||
7835 | ||||
7836 | // Check whether we can declare a class template specialization in | |||
7837 | // the current scope. | |||
7838 | if (TUK != TUK_Friend && | |||
7839 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, | |||
7840 | TemplateNameLoc, | |||
7841 | isPartialSpecialization)) | |||
7842 | return true; | |||
7843 | ||||
7844 | // The canonical type | |||
7845 | QualType CanonType; | |||
7846 | if (isPartialSpecialization) { | |||
7847 | // Build the canonical type that describes the converted template | |||
7848 | // arguments of the class template partial specialization. | |||
7849 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); | |||
7850 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, | |||
7851 | Converted); | |||
7852 | ||||
7853 | if (Context.hasSameType(CanonType, | |||
7854 | ClassTemplate->getInjectedClassNameSpecialization()) && | |||
7855 | (!Context.getLangOpts().ConceptsTS || | |||
7856 | !TemplateParams->hasAssociatedConstraints())) { | |||
7857 | // C++ [temp.class.spec]p9b3: | |||
7858 | // | |||
7859 | // -- The argument list of the specialization shall not be identical | |||
7860 | // to the implicit argument list of the primary template. | |||
7861 | // | |||
7862 | // This rule has since been removed, because it's redundant given DR1495, | |||
7863 | // but we keep it because it produces better diagnostics and recovery. | |||
7864 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) | |||
7865 | << /*class template*/0 << (TUK == TUK_Definition) | |||
7866 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); | |||
7867 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, | |||
7868 | ClassTemplate->getIdentifier(), | |||
7869 | TemplateNameLoc, | |||
7870 | Attr, | |||
7871 | TemplateParams, | |||
7872 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), | |||
7873 | /*FriendLoc*/SourceLocation(), | |||
7874 | TemplateParameterLists.size() - 1, | |||
7875 | TemplateParameterLists.data()); | |||
7876 | } | |||
7877 | ||||
7878 | // Create a new class template partial specialization declaration node. | |||
7879 | ClassTemplatePartialSpecializationDecl *PrevPartial | |||
7880 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); | |||
7881 | ClassTemplatePartialSpecializationDecl *Partial | |||
7882 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, | |||
7883 | ClassTemplate->getDeclContext(), | |||
7884 | KWLoc, TemplateNameLoc, | |||
7885 | TemplateParams, | |||
7886 | ClassTemplate, | |||
7887 | Converted, | |||
7888 | TemplateArgs, | |||
7889 | CanonType, | |||
7890 | PrevPartial); | |||
7891 | SetNestedNameSpecifier(*this, Partial, SS); | |||
7892 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { | |||
7893 | Partial->setTemplateParameterListsInfo( | |||
7894 | Context, TemplateParameterLists.drop_back(1)); | |||
7895 | } | |||
7896 | ||||
7897 | if (!PrevPartial) | |||
7898 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); | |||
7899 | Specialization = Partial; | |||
7900 | ||||
7901 | // If we are providing an explicit specialization of a member class | |||
7902 | // template specialization, make a note of that. | |||
7903 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) | |||
7904 | PrevPartial->setMemberSpecialization(); | |||
7905 | ||||
7906 | CheckTemplatePartialSpecialization(Partial); | |||
7907 | } else { | |||
7908 | // Create a new class template specialization declaration node for | |||
7909 | // this explicit specialization or friend declaration. | |||
7910 | Specialization | |||
7911 | = ClassTemplateSpecializationDecl::Create(Context, Kind, | |||
7912 | ClassTemplate->getDeclContext(), | |||
7913 | KWLoc, TemplateNameLoc, | |||
7914 | ClassTemplate, | |||
7915 | Converted, | |||
7916 | PrevDecl); | |||
7917 | SetNestedNameSpecifier(*this, Specialization, SS); | |||
7918 | if (TemplateParameterLists.size() > 0) { | |||
7919 | Specialization->setTemplateParameterListsInfo(Context, | |||
7920 | TemplateParameterLists); | |||
7921 | } | |||
7922 | ||||
7923 | if (!PrevDecl) | |||
7924 | ClassTemplate->AddSpecialization(Specialization, InsertPos); | |||
7925 | ||||
7926 | if (CurContext->isDependentContext()) { | |||
7927 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); | |||
7928 | CanonType = Context.getTemplateSpecializationType( | |||
7929 | CanonTemplate, Converted); | |||
7930 | } else { | |||
7931 | CanonType = Context.getTypeDeclType(Specialization); | |||
7932 | } | |||
7933 | } | |||
7934 | ||||
7935 | // C++ [temp.expl.spec]p6: | |||
7936 | // If a template, a member template or the member of a class template is | |||
7937 | // explicitly specialized then that specialization shall be declared | |||
7938 | // before the first use of that specialization that would cause an implicit | |||
7939 | // instantiation to take place, in every translation unit in which such a | |||
7940 | // use occurs; no diagnostic is required. | |||
7941 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { | |||
7942 | bool Okay = false; | |||
7943 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | |||
7944 | // Is there any previous explicit specialization declaration? | |||
7945 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { | |||
7946 | Okay = true; | |||
7947 | break; | |||
7948 | } | |||
7949 | } | |||
7950 | ||||
7951 | if (!Okay) { | |||
7952 | SourceRange Range(TemplateNameLoc, RAngleLoc); | |||
7953 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) | |||
7954 | << Context.getTypeDeclType(Specialization) << Range; | |||
7955 | ||||
7956 | Diag(PrevDecl->getPointOfInstantiation(), | |||
7957 | diag::note_instantiation_required_here) | |||
7958 | << (PrevDecl->getTemplateSpecializationKind() | |||
7959 | != TSK_ImplicitInstantiation); | |||
7960 | return true; | |||
7961 | } | |||
7962 | } | |||
7963 | ||||
7964 | // If this is not a friend, note that this is an explicit specialization. | |||
7965 | if (TUK != TUK_Friend) | |||
7966 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); | |||
7967 | ||||
7968 | // Check that this isn't a redefinition of this specialization. | |||
7969 | if (TUK == TUK_Definition) { | |||
7970 | RecordDecl *Def = Specialization->getDefinition(); | |||
7971 | NamedDecl *Hidden = nullptr; | |||
7972 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { | |||
7973 | SkipBody->ShouldSkip = true; | |||
7974 | SkipBody->Previous = Def; | |||
7975 | makeMergedDefinitionVisible(Hidden); | |||
7976 | } else if (Def) { | |||
7977 | SourceRange Range(TemplateNameLoc, RAngleLoc); | |||
7978 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; | |||
7979 | Diag(Def->getLocation(), diag::note_previous_definition); | |||
7980 | Specialization->setInvalidDecl(); | |||
7981 | return true; | |||
7982 | } | |||
7983 | } | |||
7984 | ||||
7985 | ProcessDeclAttributeList(S, Specialization, Attr); | |||
7986 | ||||
7987 | // Add alignment attributes if necessary; these attributes are checked when | |||
7988 | // the ASTContext lays out the structure. | |||
7989 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { | |||
7990 | AddAlignmentAttributesForRecord(Specialization); | |||
7991 | AddMsStructLayoutForRecord(Specialization); | |||
7992 | } | |||
7993 | ||||
7994 | if (ModulePrivateLoc.isValid()) | |||
7995 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) | |||
7996 | << (isPartialSpecialization? 1 : 0) | |||
7997 | << FixItHint::CreateRemoval(ModulePrivateLoc); | |||
7998 | ||||
7999 | // Build the fully-sugared type for this class template | |||
8000 | // specialization as the user wrote in the specialization | |||
8001 | // itself. This means that we'll pretty-print the type retrieved | |||
8002 | // from the specialization's declaration the way that the user | |||
8003 | // actually wrote the specialization, rather than formatting the | |||
8004 | // name based on the "canonical" representation used to store the | |||
8005 | // template arguments in the specialization. | |||
8006 | TypeSourceInfo *WrittenTy | |||
8007 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, | |||
8008 | TemplateArgs, CanonType); | |||
8009 | if (TUK != TUK_Friend) { | |||
8010 | Specialization->setTypeAsWritten(WrittenTy); | |||
8011 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); | |||
8012 | } | |||
8013 | ||||
8014 | // C++ [temp.expl.spec]p9: | |||
8015 | // A template explicit specialization is in the scope of the | |||
8016 | // namespace in which the template was defined. | |||
8017 | // | |||
8018 | // We actually implement this paragraph where we set the semantic | |||
8019 | // context (in the creation of the ClassTemplateSpecializationDecl), | |||
8020 | // but we also maintain the lexical context where the actual | |||
8021 | // definition occurs. | |||
8022 | Specialization->setLexicalDeclContext(CurContext); | |||
8023 | ||||
8024 | // We may be starting the definition of this specialization. | |||
8025 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) | |||
8026 | Specialization->startDefinition(); | |||
8027 | ||||
8028 | if (TUK == TUK_Friend) { | |||
8029 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, | |||
8030 | TemplateNameLoc, | |||
8031 | WrittenTy, | |||
8032 | /*FIXME:*/KWLoc); | |||
8033 | Friend->setAccess(AS_public); | |||
8034 | CurContext->addDecl(Friend); | |||
8035 | } else { | |||
8036 | // Add the specialization into its lexical context, so that it can | |||
8037 | // be seen when iterating through the list of declarations in that | |||
8038 | // context. However, specializations are not found by name lookup. | |||
8039 | CurContext->addDecl(Specialization); | |||
8040 | } | |||
8041 | ||||
8042 | if (SkipBody && SkipBody->ShouldSkip) | |||
8043 | return SkipBody->Previous; | |||
8044 | ||||
8045 | return Specialization; | |||
8046 | } | |||
8047 | ||||
8048 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, | |||
8049 | MultiTemplateParamsArg TemplateParameterLists, | |||
8050 | Declarator &D) { | |||
8051 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); | |||
8052 | ActOnDocumentableDecl(NewDecl); | |||
8053 | return NewDecl; | |||
8054 | } | |||
8055 | ||||
8056 | Decl *Sema::ActOnConceptDefinition(Scope *S, | |||
8057 | MultiTemplateParamsArg TemplateParameterLists, | |||
8058 | IdentifierInfo *Name, SourceLocation NameLoc, | |||
8059 | Expr *ConstraintExpr) { | |||
8060 | DeclContext *DC = CurContext; | |||
8061 | ||||
8062 | if (!DC->getRedeclContext()->isFileContext()) { | |||
8063 | Diag(NameLoc, | |||
8064 | diag::err_concept_decls_may_only_appear_in_global_namespace_scope); | |||
8065 | return nullptr; | |||
8066 | } | |||
8067 | ||||
8068 | if (TemplateParameterLists.size() > 1) { | |||
8069 | Diag(NameLoc, diag::err_concept_extra_headers); | |||
8070 | return nullptr; | |||
8071 | } | |||
8072 | ||||
8073 | if (TemplateParameterLists.front()->size() == 0) { | |||
8074 | Diag(NameLoc, diag::err_concept_no_parameters); | |||
8075 | return nullptr; | |||
8076 | } | |||
8077 | ||||
8078 | ConceptDecl *NewDecl = ConceptDecl::Create(Context, DC, NameLoc, Name, | |||
8079 | TemplateParameterLists.front(), | |||
8080 | ConstraintExpr); | |||
8081 | ||||
8082 | if (NewDecl->hasAssociatedConstraints()) { | |||
8083 | // C++2a [temp.concept]p4: | |||
8084 | // A concept shall not have associated constraints. | |||
8085 | Diag(NameLoc, diag::err_concept_no_associated_constraints); | |||
8086 | NewDecl->setInvalidDecl(); | |||
8087 | } | |||
8088 | ||||
8089 | // Check for conflicting previous declaration. | |||
8090 | DeclarationNameInfo NameInfo(NewDecl->getDeclName(), NameLoc); | |||
8091 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, | |||
8092 | ForVisibleRedeclaration); | |||
8093 | LookupName(Previous, S); | |||
8094 | ||||
8095 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage=*/false, | |||
8096 | /*AllowInlineNamespace*/false); | |||
8097 | if (!Previous.empty()) { | |||
8098 | auto *Old = Previous.getRepresentativeDecl(); | |||
8099 | Diag(NameLoc, isa<ConceptDecl>(Old) ? diag::err_redefinition : | |||
8100 | diag::err_redefinition_different_kind) << NewDecl->getDeclName(); | |||
8101 | Diag(Old->getLocation(), diag::note_previous_definition); | |||
8102 | } | |||
8103 | ||||
8104 | ActOnDocumentableDecl(NewDecl); | |||
8105 | PushOnScopeChains(NewDecl, S); | |||
8106 | return NewDecl; | |||
8107 | } | |||
8108 | ||||
8109 | /// \brief Strips various properties off an implicit instantiation | |||
8110 | /// that has just been explicitly specialized. | |||
8111 | static void StripImplicitInstantiation(NamedDecl *D) { | |||
8112 | D->dropAttr<DLLImportAttr>(); | |||
8113 | D->dropAttr<DLLExportAttr>(); | |||
8114 | ||||
8115 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) | |||
8116 | FD->setInlineSpecified(false); | |||
8117 | } | |||
8118 | ||||
8119 | /// Compute the diagnostic location for an explicit instantiation | |||
8120 | // declaration or definition. | |||
8121 | static SourceLocation DiagLocForExplicitInstantiation( | |||
8122 | NamedDecl* D, SourceLocation PointOfInstantiation) { | |||
8123 | // Explicit instantiations following a specialization have no effect and | |||
8124 | // hence no PointOfInstantiation. In that case, walk decl backwards | |||
8125 | // until a valid name loc is found. | |||
8126 | SourceLocation PrevDiagLoc = PointOfInstantiation; | |||
8127 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); | |||
8128 | Prev = Prev->getPreviousDecl()) { | |||
8129 | PrevDiagLoc = Prev->getLocation(); | |||
8130 | } | |||
8131 | assert(PrevDiagLoc.isValid() &&((PrevDiagLoc.isValid() && "Explicit instantiation without point of instantiation?" ) ? static_cast<void> (0) : __assert_fail ("PrevDiagLoc.isValid() && \"Explicit instantiation without point of instantiation?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8132, __PRETTY_FUNCTION__)) | |||
8132 | "Explicit instantiation without point of instantiation?")((PrevDiagLoc.isValid() && "Explicit instantiation without point of instantiation?" ) ? static_cast<void> (0) : __assert_fail ("PrevDiagLoc.isValid() && \"Explicit instantiation without point of instantiation?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8132, __PRETTY_FUNCTION__)); | |||
8133 | return PrevDiagLoc; | |||
8134 | } | |||
8135 | ||||
8136 | /// Diagnose cases where we have an explicit template specialization | |||
8137 | /// before/after an explicit template instantiation, producing diagnostics | |||
8138 | /// for those cases where they are required and determining whether the | |||
8139 | /// new specialization/instantiation will have any effect. | |||
8140 | /// | |||
8141 | /// \param NewLoc the location of the new explicit specialization or | |||
8142 | /// instantiation. | |||
8143 | /// | |||
8144 | /// \param NewTSK the kind of the new explicit specialization or instantiation. | |||
8145 | /// | |||
8146 | /// \param PrevDecl the previous declaration of the entity. | |||
8147 | /// | |||
8148 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. | |||
8149 | /// | |||
8150 | /// \param PrevPointOfInstantiation if valid, indicates where the previus | |||
8151 | /// declaration was instantiated (either implicitly or explicitly). | |||
8152 | /// | |||
8153 | /// \param HasNoEffect will be set to true to indicate that the new | |||
8154 | /// specialization or instantiation has no effect and should be ignored. | |||
8155 | /// | |||
8156 | /// \returns true if there was an error that should prevent the introduction of | |||
8157 | /// the new declaration into the AST, false otherwise. | |||
8158 | bool | |||
8159 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, | |||
8160 | TemplateSpecializationKind NewTSK, | |||
8161 | NamedDecl *PrevDecl, | |||
8162 | TemplateSpecializationKind PrevTSK, | |||
8163 | SourceLocation PrevPointOfInstantiation, | |||
8164 | bool &HasNoEffect) { | |||
8165 | HasNoEffect = false; | |||
8166 | ||||
8167 | switch (NewTSK) { | |||
8168 | case TSK_Undeclared: | |||
8169 | case TSK_ImplicitInstantiation: | |||
8170 | assert((((PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation ) && "previous declaration must be implicit!") ? static_cast <void> (0) : __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8172, __PRETTY_FUNCTION__)) | |||
8171 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) &&(((PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation ) && "previous declaration must be implicit!") ? static_cast <void> (0) : __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8172, __PRETTY_FUNCTION__)) | |||
8172 | "previous declaration must be implicit!")(((PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation ) && "previous declaration must be implicit!") ? static_cast <void> (0) : __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8172, __PRETTY_FUNCTION__)); | |||
8173 | return false; | |||
8174 | ||||
8175 | case TSK_ExplicitSpecialization: | |||
8176 | switch (PrevTSK) { | |||
8177 | case TSK_Undeclared: | |||
8178 | case TSK_ExplicitSpecialization: | |||
8179 | // Okay, we're just specializing something that is either already | |||
8180 | // explicitly specialized or has merely been mentioned without any | |||
8181 | // instantiation. | |||
8182 | return false; | |||
8183 | ||||
8184 | case TSK_ImplicitInstantiation: | |||
8185 | if (PrevPointOfInstantiation.isInvalid()) { | |||
8186 | // The declaration itself has not actually been instantiated, so it is | |||
8187 | // still okay to specialize it. | |||
8188 | StripImplicitInstantiation(PrevDecl); | |||
8189 | return false; | |||
8190 | } | |||
8191 | // Fall through | |||
8192 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
8193 | ||||
8194 | case TSK_ExplicitInstantiationDeclaration: | |||
8195 | case TSK_ExplicitInstantiationDefinition: | |||
8196 | assert((PrevTSK == TSK_ImplicitInstantiation ||(((PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation .isValid()) && "Explicit instantiation without point of instantiation?" ) ? static_cast<void> (0) : __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8198, __PRETTY_FUNCTION__)) | |||
8197 | PrevPointOfInstantiation.isValid()) &&(((PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation .isValid()) && "Explicit instantiation without point of instantiation?" ) ? static_cast<void> (0) : __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8198, __PRETTY_FUNCTION__)) | |||
8198 | "Explicit instantiation without point of instantiation?")(((PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation .isValid()) && "Explicit instantiation without point of instantiation?" ) ? static_cast<void> (0) : __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8198, __PRETTY_FUNCTION__)); | |||
8199 | ||||
8200 | // C++ [temp.expl.spec]p6: | |||
8201 | // If a template, a member template or the member of a class template | |||
8202 | // is explicitly specialized then that specialization shall be declared | |||
8203 | // before the first use of that specialization that would cause an | |||
8204 | // implicit instantiation to take place, in every translation unit in | |||
8205 | // which such a use occurs; no diagnostic is required. | |||
8206 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | |||
8207 | // Is there any previous explicit specialization declaration? | |||
8208 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) | |||
8209 | return false; | |||
8210 | } | |||
8211 | ||||
8212 | Diag(NewLoc, diag::err_specialization_after_instantiation) | |||
8213 | << PrevDecl; | |||
8214 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) | |||
8215 | << (PrevTSK != TSK_ImplicitInstantiation); | |||
8216 | ||||
8217 | return true; | |||
8218 | } | |||
8219 | llvm_unreachable("The switch over PrevTSK must be exhaustive.")::llvm::llvm_unreachable_internal("The switch over PrevTSK must be exhaustive." , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8219); | |||
8220 | ||||
8221 | case TSK_ExplicitInstantiationDeclaration: | |||
8222 | switch (PrevTSK) { | |||
8223 | case TSK_ExplicitInstantiationDeclaration: | |||
8224 | // This explicit instantiation declaration is redundant (that's okay). | |||
8225 | HasNoEffect = true; | |||
8226 | return false; | |||
8227 | ||||
8228 | case TSK_Undeclared: | |||
8229 | case TSK_ImplicitInstantiation: | |||
8230 | // We're explicitly instantiating something that may have already been | |||
8231 | // implicitly instantiated; that's fine. | |||
8232 | return false; | |||
8233 | ||||
8234 | case TSK_ExplicitSpecialization: | |||
8235 | // C++0x [temp.explicit]p4: | |||
8236 | // For a given set of template parameters, if an explicit instantiation | |||
8237 | // of a template appears after a declaration of an explicit | |||
8238 | // specialization for that template, the explicit instantiation has no | |||
8239 | // effect. | |||
8240 | HasNoEffect = true; | |||
8241 | return false; | |||
8242 | ||||
8243 | case TSK_ExplicitInstantiationDefinition: | |||
8244 | // C++0x [temp.explicit]p10: | |||
8245 | // If an entity is the subject of both an explicit instantiation | |||
8246 | // declaration and an explicit instantiation definition in the same | |||
8247 | // translation unit, the definition shall follow the declaration. | |||
8248 | Diag(NewLoc, | |||
8249 | diag::err_explicit_instantiation_declaration_after_definition); | |||
8250 | ||||
8251 | // Explicit instantiations following a specialization have no effect and | |||
8252 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards | |||
8253 | // until a valid name loc is found. | |||
8254 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), | |||
8255 | diag::note_explicit_instantiation_definition_here); | |||
8256 | HasNoEffect = true; | |||
8257 | return false; | |||
8258 | } | |||
8259 | llvm_unreachable("Unexpected TemplateSpecializationKind!")::llvm::llvm_unreachable_internal("Unexpected TemplateSpecializationKind!" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8259); | |||
8260 | ||||
8261 | case TSK_ExplicitInstantiationDefinition: | |||
8262 | switch (PrevTSK) { | |||
8263 | case TSK_Undeclared: | |||
8264 | case TSK_ImplicitInstantiation: | |||
8265 | // We're explicitly instantiating something that may have already been | |||
8266 | // implicitly instantiated; that's fine. | |||
8267 | return false; | |||
8268 | ||||
8269 | case TSK_ExplicitSpecialization: | |||
8270 | // C++ DR 259, C++0x [temp.explicit]p4: | |||
8271 | // For a given set of template parameters, if an explicit | |||
8272 | // instantiation of a template appears after a declaration of | |||
8273 | // an explicit specialization for that template, the explicit | |||
8274 | // instantiation has no effect. | |||
8275 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) | |||
8276 | << PrevDecl; | |||
8277 | Diag(PrevDecl->getLocation(), | |||
8278 | diag::note_previous_template_specialization); | |||
8279 | HasNoEffect = true; | |||
8280 | return false; | |||
8281 | ||||
8282 | case TSK_ExplicitInstantiationDeclaration: | |||
8283 | // We're explicitly instantiating a definition for something for which we | |||
8284 | // were previously asked to suppress instantiations. That's fine. | |||
8285 | ||||
8286 | // C++0x [temp.explicit]p4: | |||
8287 | // For a given set of template parameters, if an explicit instantiation | |||
8288 | // of a template appears after a declaration of an explicit | |||
8289 | // specialization for that template, the explicit instantiation has no | |||
8290 | // effect. | |||
8291 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | |||
8292 | // Is there any previous explicit specialization declaration? | |||
8293 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { | |||
8294 | HasNoEffect = true; | |||
8295 | break; | |||
8296 | } | |||
8297 | } | |||
8298 | ||||
8299 | return false; | |||
8300 | ||||
8301 | case TSK_ExplicitInstantiationDefinition: | |||
8302 | // C++0x [temp.spec]p5: | |||
8303 | // For a given template and a given set of template-arguments, | |||
8304 | // - an explicit instantiation definition shall appear at most once | |||
8305 | // in a program, | |||
8306 | ||||
8307 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. | |||
8308 | Diag(NewLoc, (getLangOpts().MSVCCompat) | |||
8309 | ? diag::ext_explicit_instantiation_duplicate | |||
8310 | : diag::err_explicit_instantiation_duplicate) | |||
8311 | << PrevDecl; | |||
8312 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), | |||
8313 | diag::note_previous_explicit_instantiation); | |||
8314 | HasNoEffect = true; | |||
8315 | return false; | |||
8316 | } | |||
8317 | } | |||
8318 | ||||
8319 | llvm_unreachable("Missing specialization/instantiation case?")::llvm::llvm_unreachable_internal("Missing specialization/instantiation case?" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8319); | |||
8320 | } | |||
8321 | ||||
8322 | /// Perform semantic analysis for the given dependent function | |||
8323 | /// template specialization. | |||
8324 | /// | |||
8325 | /// The only possible way to get a dependent function template specialization | |||
8326 | /// is with a friend declaration, like so: | |||
8327 | /// | |||
8328 | /// \code | |||
8329 | /// template \<class T> void foo(T); | |||
8330 | /// template \<class T> class A { | |||
8331 | /// friend void foo<>(T); | |||
8332 | /// }; | |||
8333 | /// \endcode | |||
8334 | /// | |||
8335 | /// There really isn't any useful analysis we can do here, so we | |||
8336 | /// just store the information. | |||
8337 | bool | |||
8338 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, | |||
8339 | const TemplateArgumentListInfo &ExplicitTemplateArgs, | |||
8340 | LookupResult &Previous) { | |||
8341 | // Remove anything from Previous that isn't a function template in | |||
8342 | // the correct context. | |||
8343 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); | |||
8344 | LookupResult::Filter F = Previous.makeFilter(); | |||
8345 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; | |||
8346 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; | |||
8347 | while (F.hasNext()) { | |||
8348 | NamedDecl *D = F.next()->getUnderlyingDecl(); | |||
8349 | if (!isa<FunctionTemplateDecl>(D)) { | |||
8350 | F.erase(); | |||
8351 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); | |||
8352 | continue; | |||
8353 | } | |||
8354 | ||||
8355 | if (!FDLookupContext->InEnclosingNamespaceSetOf( | |||
8356 | D->getDeclContext()->getRedeclContext())) { | |||
8357 | F.erase(); | |||
8358 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); | |||
8359 | continue; | |||
8360 | } | |||
8361 | } | |||
8362 | F.done(); | |||
8363 | ||||
8364 | if (Previous.empty()) { | |||
8365 | Diag(FD->getLocation(), | |||
8366 | diag::err_dependent_function_template_spec_no_match); | |||
8367 | for (auto &P : DiscardedCandidates) | |||
8368 | Diag(P.second->getLocation(), | |||
8369 | diag::note_dependent_function_template_spec_discard_reason) | |||
8370 | << P.first; | |||
8371 | return true; | |||
8372 | } | |||
8373 | ||||
8374 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), | |||
8375 | ExplicitTemplateArgs); | |||
8376 | return false; | |||
8377 | } | |||
8378 | ||||
8379 | /// Perform semantic analysis for the given function template | |||
8380 | /// specialization. | |||
8381 | /// | |||
8382 | /// This routine performs all of the semantic analysis required for an | |||
8383 | /// explicit function template specialization. On successful completion, | |||
8384 | /// the function declaration \p FD will become a function template | |||
8385 | /// specialization. | |||
8386 | /// | |||
8387 | /// \param FD the function declaration, which will be updated to become a | |||
8388 | /// function template specialization. | |||
8389 | /// | |||
8390 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, | |||
8391 | /// if any. Note that this may be valid info even when 0 arguments are | |||
8392 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); | |||
8393 | /// as it anyway contains info on the angle brackets locations. | |||
8394 | /// | |||
8395 | /// \param Previous the set of declarations that may be specialized by | |||
8396 | /// this function specialization. | |||
8397 | /// | |||
8398 | /// \param QualifiedFriend whether this is a lookup for a qualified friend | |||
8399 | /// declaration with no explicit template argument list that might be | |||
8400 | /// befriending a function template specialization. | |||
8401 | bool Sema::CheckFunctionTemplateSpecialization( | |||
8402 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, | |||
8403 | LookupResult &Previous, bool QualifiedFriend) { | |||
8404 | // The set of function template specializations that could match this | |||
8405 | // explicit function template specialization. | |||
8406 | UnresolvedSet<8> Candidates; | |||
8407 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), | |||
8408 | /*ForTakingAddress=*/false); | |||
8409 | ||||
8410 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> | |||
8411 | ConvertedTemplateArgs; | |||
8412 | ||||
8413 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); | |||
8414 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | |||
8415 | I != E; ++I) { | |||
8416 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); | |||
8417 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { | |||
8418 | // Only consider templates found within the same semantic lookup scope as | |||
8419 | // FD. | |||
8420 | if (!FDLookupContext->InEnclosingNamespaceSetOf( | |||
8421 | Ovl->getDeclContext()->getRedeclContext())) | |||
8422 | continue; | |||
8423 | ||||
8424 | // When matching a constexpr member function template specialization | |||
8425 | // against the primary template, we don't yet know whether the | |||
8426 | // specialization has an implicit 'const' (because we don't know whether | |||
8427 | // it will be a static member function until we know which template it | |||
8428 | // specializes), so adjust it now assuming it specializes this template. | |||
8429 | QualType FT = FD->getType(); | |||
8430 | if (FD->isConstexpr()) { | |||
8431 | CXXMethodDecl *OldMD = | |||
8432 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); | |||
8433 | if (OldMD && OldMD->isConst()) { | |||
8434 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); | |||
8435 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | |||
8436 | EPI.TypeQuals.addConst(); | |||
8437 | FT = Context.getFunctionType(FPT->getReturnType(), | |||
8438 | FPT->getParamTypes(), EPI); | |||
8439 | } | |||
8440 | } | |||
8441 | ||||
8442 | TemplateArgumentListInfo Args; | |||
8443 | if (ExplicitTemplateArgs) | |||
8444 | Args = *ExplicitTemplateArgs; | |||
8445 | ||||
8446 | // C++ [temp.expl.spec]p11: | |||
8447 | // A trailing template-argument can be left unspecified in the | |||
8448 | // template-id naming an explicit function template specialization | |||
8449 | // provided it can be deduced from the function argument type. | |||
8450 | // Perform template argument deduction to determine whether we may be | |||
8451 | // specializing this template. | |||
8452 | // FIXME: It is somewhat wasteful to build | |||
8453 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); | |||
8454 | FunctionDecl *Specialization = nullptr; | |||
8455 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( | |||
8456 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), | |||
8457 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, | |||
8458 | Info)) { | |||
8459 | // Template argument deduction failed; record why it failed, so | |||
8460 | // that we can provide nifty diagnostics. | |||
8461 | FailedCandidates.addCandidate().set( | |||
8462 | I.getPair(), FunTmpl->getTemplatedDecl(), | |||
8463 | MakeDeductionFailureInfo(Context, TDK, Info)); | |||
8464 | (void)TDK; | |||
8465 | continue; | |||
8466 | } | |||
8467 | ||||
8468 | // Target attributes are part of the cuda function signature, so | |||
8469 | // the deduced template's cuda target must match that of the | |||
8470 | // specialization. Given that C++ template deduction does not | |||
8471 | // take target attributes into account, we reject candidates | |||
8472 | // here that have a different target. | |||
8473 | if (LangOpts.CUDA && | |||
8474 | IdentifyCUDATarget(Specialization, | |||
8475 | /* IgnoreImplicitHDAttr = */ true) != | |||
8476 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttr = */ true)) { | |||
8477 | FailedCandidates.addCandidate().set( | |||
8478 | I.getPair(), FunTmpl->getTemplatedDecl(), | |||
8479 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); | |||
8480 | continue; | |||
8481 | } | |||
8482 | ||||
8483 | // Record this candidate. | |||
8484 | if (ExplicitTemplateArgs) | |||
8485 | ConvertedTemplateArgs[Specialization] = std::move(Args); | |||
8486 | Candidates.addDecl(Specialization, I.getAccess()); | |||
8487 | } | |||
8488 | } | |||
8489 | ||||
8490 | // For a qualified friend declaration (with no explicit marker to indicate | |||
8491 | // that a template specialization was intended), note all (template and | |||
8492 | // non-template) candidates. | |||
8493 | if (QualifiedFriend && Candidates.empty()) { | |||
8494 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) | |||
8495 | << FD->getDeclName() << FDLookupContext; | |||
8496 | // FIXME: We should form a single candidate list and diagnose all | |||
8497 | // candidates at once, to get proper sorting and limiting. | |||
8498 | for (auto *OldND : Previous) { | |||
8499 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) | |||
8500 | NoteOverloadCandidate(OldND, OldFD, CRK_None, FD->getType(), false); | |||
8501 | } | |||
8502 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); | |||
8503 | return true; | |||
8504 | } | |||
8505 | ||||
8506 | // Find the most specialized function template. | |||
8507 | UnresolvedSetIterator Result = getMostSpecialized( | |||
8508 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), | |||
8509 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), | |||
8510 | PDiag(diag::err_function_template_spec_ambiguous) | |||
8511 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), | |||
8512 | PDiag(diag::note_function_template_spec_matched)); | |||
8513 | ||||
8514 | if (Result == Candidates.end()) | |||
8515 | return true; | |||
8516 | ||||
8517 | // Ignore access information; it doesn't figure into redeclaration checking. | |||
8518 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); | |||
8519 | ||||
8520 | FunctionTemplateSpecializationInfo *SpecInfo | |||
8521 | = Specialization->getTemplateSpecializationInfo(); | |||
8522 | assert(SpecInfo && "Function template specialization info missing?")((SpecInfo && "Function template specialization info missing?" ) ? static_cast<void> (0) : __assert_fail ("SpecInfo && \"Function template specialization info missing?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8522, __PRETTY_FUNCTION__)); | |||
8523 | ||||
8524 | // Note: do not overwrite location info if previous template | |||
8525 | // specialization kind was explicit. | |||
8526 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); | |||
8527 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { | |||
8528 | Specialization->setLocation(FD->getLocation()); | |||
8529 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); | |||
8530 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr | |||
8531 | // function can differ from the template declaration with respect to | |||
8532 | // the constexpr specifier. | |||
8533 | // FIXME: We need an update record for this AST mutation. | |||
8534 | // FIXME: What if there are multiple such prior declarations (for instance, | |||
8535 | // from different modules)? | |||
8536 | Specialization->setConstexprKind(FD->getConstexprKind()); | |||
8537 | } | |||
8538 | ||||
8539 | // FIXME: Check if the prior specialization has a point of instantiation. | |||
8540 | // If so, we have run afoul of . | |||
8541 | ||||
8542 | // If this is a friend declaration, then we're not really declaring | |||
8543 | // an explicit specialization. | |||
8544 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); | |||
8545 | ||||
8546 | // Check the scope of this explicit specialization. | |||
8547 | if (!isFriend && | |||
8548 | CheckTemplateSpecializationScope(*this, | |||
8549 | Specialization->getPrimaryTemplate(), | |||
8550 | Specialization, FD->getLocation(), | |||
8551 | false)) | |||
8552 | return true; | |||
8553 | ||||
8554 | // C++ [temp.expl.spec]p6: | |||
8555 | // If a template, a member template or the member of a class template is | |||
8556 | // explicitly specialized then that specialization shall be declared | |||
8557 | // before the first use of that specialization that would cause an implicit | |||
8558 | // instantiation to take place, in every translation unit in which such a | |||
8559 | // use occurs; no diagnostic is required. | |||
8560 | bool HasNoEffect = false; | |||
8561 | if (!isFriend && | |||
8562 | CheckSpecializationInstantiationRedecl(FD->getLocation(), | |||
8563 | TSK_ExplicitSpecialization, | |||
8564 | Specialization, | |||
8565 | SpecInfo->getTemplateSpecializationKind(), | |||
8566 | SpecInfo->getPointOfInstantiation(), | |||
8567 | HasNoEffect)) | |||
8568 | return true; | |||
8569 | ||||
8570 | // Mark the prior declaration as an explicit specialization, so that later | |||
8571 | // clients know that this is an explicit specialization. | |||
8572 | if (!isFriend) { | |||
8573 | // Since explicit specializations do not inherit '=delete' from their | |||
8574 | // primary function template - check if the 'specialization' that was | |||
8575 | // implicitly generated (during template argument deduction for partial | |||
8576 | // ordering) from the most specialized of all the function templates that | |||
8577 | // 'FD' could have been specializing, has a 'deleted' definition. If so, | |||
8578 | // first check that it was implicitly generated during template argument | |||
8579 | // deduction by making sure it wasn't referenced, and then reset the deleted | |||
8580 | // flag to not-deleted, so that we can inherit that information from 'FD'. | |||
8581 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && | |||
8582 | !Specialization->getCanonicalDecl()->isReferenced()) { | |||
8583 | // FIXME: This assert will not hold in the presence of modules. | |||
8584 | assert(((Specialization->getCanonicalDecl() == Specialization && "This must be the only existing declaration of this specialization" ) ? static_cast<void> (0) : __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8586, __PRETTY_FUNCTION__)) | |||
8585 | Specialization->getCanonicalDecl() == Specialization &&((Specialization->getCanonicalDecl() == Specialization && "This must be the only existing declaration of this specialization" ) ? static_cast<void> (0) : __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8586, __PRETTY_FUNCTION__)) | |||
8586 | "This must be the only existing declaration of this specialization")((Specialization->getCanonicalDecl() == Specialization && "This must be the only existing declaration of this specialization" ) ? static_cast<void> (0) : __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8586, __PRETTY_FUNCTION__)); | |||
8587 | // FIXME: We need an update record for this AST mutation. | |||
8588 | Specialization->setDeletedAsWritten(false); | |||
8589 | } | |||
8590 | // FIXME: We need an update record for this AST mutation. | |||
8591 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | |||
8592 | MarkUnusedFileScopedDecl(Specialization); | |||
8593 | } | |||
8594 | ||||
8595 | // Turn the given function declaration into a function template | |||
8596 | // specialization, with the template arguments from the previous | |||
8597 | // specialization. | |||
8598 | // Take copies of (semantic and syntactic) template argument lists. | |||
8599 | const TemplateArgumentList* TemplArgs = new (Context) | |||
8600 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); | |||
8601 | FD->setFunctionTemplateSpecialization( | |||
8602 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, | |||
8603 | SpecInfo->getTemplateSpecializationKind(), | |||
8604 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); | |||
8605 | ||||
8606 | // A function template specialization inherits the target attributes | |||
8607 | // of its template. (We require the attributes explicitly in the | |||
8608 | // code to match, but a template may have implicit attributes by | |||
8609 | // virtue e.g. of being constexpr, and it passes these implicit | |||
8610 | // attributes on to its specializations.) | |||
8611 | if (LangOpts.CUDA) | |||
8612 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); | |||
8613 | ||||
8614 | // The "previous declaration" for this function template specialization is | |||
8615 | // the prior function template specialization. | |||
8616 | Previous.clear(); | |||
8617 | Previous.addDecl(Specialization); | |||
8618 | return false; | |||
8619 | } | |||
8620 | ||||
8621 | /// Perform semantic analysis for the given non-template member | |||
8622 | /// specialization. | |||
8623 | /// | |||
8624 | /// This routine performs all of the semantic analysis required for an | |||
8625 | /// explicit member function specialization. On successful completion, | |||
8626 | /// the function declaration \p FD will become a member function | |||
8627 | /// specialization. | |||
8628 | /// | |||
8629 | /// \param Member the member declaration, which will be updated to become a | |||
8630 | /// specialization. | |||
8631 | /// | |||
8632 | /// \param Previous the set of declarations, one of which may be specialized | |||
8633 | /// by this function specialization; the set will be modified to contain the | |||
8634 | /// redeclared member. | |||
8635 | bool | |||
8636 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { | |||
8637 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members")((!isa<TemplateDecl>(Member) && "Only for non-template members" ) ? static_cast<void> (0) : __assert_fail ("!isa<TemplateDecl>(Member) && \"Only for non-template members\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8637, __PRETTY_FUNCTION__)); | |||
8638 | ||||
8639 | // Try to find the member we are instantiating. | |||
8640 | NamedDecl *FoundInstantiation = nullptr; | |||
8641 | NamedDecl *Instantiation = nullptr; | |||
8642 | NamedDecl *InstantiatedFrom = nullptr; | |||
8643 | MemberSpecializationInfo *MSInfo = nullptr; | |||
8644 | ||||
8645 | if (Previous.empty()) { | |||
8646 | // Nowhere to look anyway. | |||
8647 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { | |||
8648 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | |||
8649 | I != E; ++I) { | |||
8650 | NamedDecl *D = (*I)->getUnderlyingDecl(); | |||
8651 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | |||
8652 | QualType Adjusted = Function->getType(); | |||
8653 | if (!hasExplicitCallingConv(Adjusted)) | |||
8654 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); | |||
8655 | // This doesn't handle deduced return types, but both function | |||
8656 | // declarations should be undeduced at this point. | |||
8657 | if (Context.hasSameType(Adjusted, Method->getType())) { | |||
8658 | FoundInstantiation = *I; | |||
8659 | Instantiation = Method; | |||
8660 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); | |||
8661 | MSInfo = Method->getMemberSpecializationInfo(); | |||
8662 | break; | |||
8663 | } | |||
8664 | } | |||
8665 | } | |||
8666 | } else if (isa<VarDecl>(Member)) { | |||
8667 | VarDecl *PrevVar; | |||
8668 | if (Previous.isSingleResult() && | |||
8669 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) | |||
8670 | if (PrevVar->isStaticDataMember()) { | |||
8671 | FoundInstantiation = Previous.getRepresentativeDecl(); | |||
8672 | Instantiation = PrevVar; | |||
8673 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); | |||
8674 | MSInfo = PrevVar->getMemberSpecializationInfo(); | |||
8675 | } | |||
8676 | } else if (isa<RecordDecl>(Member)) { | |||
8677 | CXXRecordDecl *PrevRecord; | |||
8678 | if (Previous.isSingleResult() && | |||
8679 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { | |||
8680 | FoundInstantiation = Previous.getRepresentativeDecl(); | |||
8681 | Instantiation = PrevRecord; | |||
8682 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); | |||
8683 | MSInfo = PrevRecord->getMemberSpecializationInfo(); | |||
8684 | } | |||
8685 | } else if (isa<EnumDecl>(Member)) { | |||
8686 | EnumDecl *PrevEnum; | |||
8687 | if (Previous.isSingleResult() && | |||
8688 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { | |||
8689 | FoundInstantiation = Previous.getRepresentativeDecl(); | |||
8690 | Instantiation = PrevEnum; | |||
8691 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); | |||
8692 | MSInfo = PrevEnum->getMemberSpecializationInfo(); | |||
8693 | } | |||
8694 | } | |||
8695 | ||||
8696 | if (!Instantiation) { | |||
8697 | // There is no previous declaration that matches. Since member | |||
8698 | // specializations are always out-of-line, the caller will complain about | |||
8699 | // this mismatch later. | |||
8700 | return false; | |||
8701 | } | |||
8702 | ||||
8703 | // A member specialization in a friend declaration isn't really declaring | |||
8704 | // an explicit specialization, just identifying a specific (possibly implicit) | |||
8705 | // specialization. Don't change the template specialization kind. | |||
8706 | // | |||
8707 | // FIXME: Is this really valid? Other compilers reject. | |||
8708 | if (Member->getFriendObjectKind() != Decl::FOK_None) { | |||
8709 | // Preserve instantiation information. | |||
8710 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { | |||
8711 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( | |||
8712 | cast<CXXMethodDecl>(InstantiatedFrom), | |||
8713 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); | |||
8714 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { | |||
8715 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( | |||
8716 | cast<CXXRecordDecl>(InstantiatedFrom), | |||
8717 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); | |||
8718 | } | |||
8719 | ||||
8720 | Previous.clear(); | |||
8721 | Previous.addDecl(FoundInstantiation); | |||
8722 | return false; | |||
8723 | } | |||
8724 | ||||
8725 | // Make sure that this is a specialization of a member. | |||
8726 | if (!InstantiatedFrom) { | |||
8727 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) | |||
8728 | << Member; | |||
8729 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); | |||
8730 | return true; | |||
8731 | } | |||
8732 | ||||
8733 | // C++ [temp.expl.spec]p6: | |||
8734 | // If a template, a member template or the member of a class template is | |||
8735 | // explicitly specialized then that specialization shall be declared | |||
8736 | // before the first use of that specialization that would cause an implicit | |||
8737 | // instantiation to take place, in every translation unit in which such a | |||
8738 | // use occurs; no diagnostic is required. | |||
8739 | assert(MSInfo && "Member specialization info missing?")((MSInfo && "Member specialization info missing?") ? static_cast <void> (0) : __assert_fail ("MSInfo && \"Member specialization info missing?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8739, __PRETTY_FUNCTION__)); | |||
8740 | ||||
8741 | bool HasNoEffect = false; | |||
8742 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), | |||
8743 | TSK_ExplicitSpecialization, | |||
8744 | Instantiation, | |||
8745 | MSInfo->getTemplateSpecializationKind(), | |||
8746 | MSInfo->getPointOfInstantiation(), | |||
8747 | HasNoEffect)) | |||
8748 | return true; | |||
8749 | ||||
8750 | // Check the scope of this explicit specialization. | |||
8751 | if (CheckTemplateSpecializationScope(*this, | |||
8752 | InstantiatedFrom, | |||
8753 | Instantiation, Member->getLocation(), | |||
8754 | false)) | |||
8755 | return true; | |||
8756 | ||||
8757 | // Note that this member specialization is an "instantiation of" the | |||
8758 | // corresponding member of the original template. | |||
8759 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { | |||
8760 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); | |||
8761 | if (InstantiationFunction->getTemplateSpecializationKind() == | |||
8762 | TSK_ImplicitInstantiation) { | |||
8763 | // Explicit specializations of member functions of class templates do not | |||
8764 | // inherit '=delete' from the member function they are specializing. | |||
8765 | if (InstantiationFunction->isDeleted()) { | |||
8766 | // FIXME: This assert will not hold in the presence of modules. | |||
8767 | assert(InstantiationFunction->getCanonicalDecl() ==((InstantiationFunction->getCanonicalDecl() == InstantiationFunction ) ? static_cast<void> (0) : __assert_fail ("InstantiationFunction->getCanonicalDecl() == InstantiationFunction" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8768, __PRETTY_FUNCTION__)) | |||
8768 | InstantiationFunction)((InstantiationFunction->getCanonicalDecl() == InstantiationFunction ) ? static_cast<void> (0) : __assert_fail ("InstantiationFunction->getCanonicalDecl() == InstantiationFunction" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8768, __PRETTY_FUNCTION__)); | |||
8769 | // FIXME: We need an update record for this AST mutation. | |||
8770 | InstantiationFunction->setDeletedAsWritten(false); | |||
8771 | } | |||
8772 | } | |||
8773 | ||||
8774 | MemberFunction->setInstantiationOfMemberFunction( | |||
8775 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); | |||
8776 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { | |||
8777 | MemberVar->setInstantiationOfStaticDataMember( | |||
8778 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); | |||
8779 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { | |||
8780 | MemberClass->setInstantiationOfMemberClass( | |||
8781 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); | |||
8782 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { | |||
8783 | MemberEnum->setInstantiationOfMemberEnum( | |||
8784 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); | |||
8785 | } else { | |||
8786 | llvm_unreachable("unknown member specialization kind")::llvm::llvm_unreachable_internal("unknown member specialization kind" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8786); | |||
8787 | } | |||
8788 | ||||
8789 | // Save the caller the trouble of having to figure out which declaration | |||
8790 | // this specialization matches. | |||
8791 | Previous.clear(); | |||
8792 | Previous.addDecl(FoundInstantiation); | |||
8793 | return false; | |||
8794 | } | |||
8795 | ||||
8796 | /// Complete the explicit specialization of a member of a class template by | |||
8797 | /// updating the instantiated member to be marked as an explicit specialization. | |||
8798 | /// | |||
8799 | /// \param OrigD The member declaration instantiated from the template. | |||
8800 | /// \param Loc The location of the explicit specialization of the member. | |||
8801 | template<typename DeclT> | |||
8802 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, | |||
8803 | SourceLocation Loc) { | |||
8804 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | |||
8805 | return; | |||
8806 | ||||
8807 | // FIXME: Inform AST mutation listeners of this AST mutation. | |||
8808 | // FIXME: If there are multiple in-class declarations of the member (from | |||
8809 | // multiple modules, or a declaration and later definition of a member type), | |||
8810 | // should we update all of them? | |||
8811 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | |||
8812 | OrigD->setLocation(Loc); | |||
8813 | } | |||
8814 | ||||
8815 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, | |||
8816 | LookupResult &Previous) { | |||
8817 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); | |||
8818 | if (Instantiation == Member) | |||
8819 | return; | |||
8820 | ||||
8821 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) | |||
8822 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); | |||
8823 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) | |||
8824 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); | |||
8825 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) | |||
8826 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); | |||
8827 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) | |||
8828 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); | |||
8829 | else | |||
8830 | llvm_unreachable("unknown member specialization kind")::llvm::llvm_unreachable_internal("unknown member specialization kind" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8830); | |||
8831 | } | |||
8832 | ||||
8833 | /// Check the scope of an explicit instantiation. | |||
8834 | /// | |||
8835 | /// \returns true if a serious error occurs, false otherwise. | |||
8836 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, | |||
8837 | SourceLocation InstLoc, | |||
8838 | bool WasQualifiedName) { | |||
8839 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); | |||
8840 | DeclContext *CurContext = S.CurContext->getRedeclContext(); | |||
8841 | ||||
8842 | if (CurContext->isRecord()) { | |||
8843 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) | |||
8844 | << D; | |||
8845 | return true; | |||
8846 | } | |||
8847 | ||||
8848 | // C++11 [temp.explicit]p3: | |||
8849 | // An explicit instantiation shall appear in an enclosing namespace of its | |||
8850 | // template. If the name declared in the explicit instantiation is an | |||
8851 | // unqualified name, the explicit instantiation shall appear in the | |||
8852 | // namespace where its template is declared or, if that namespace is inline | |||
8853 | // (7.3.1), any namespace from its enclosing namespace set. | |||
8854 | // | |||
8855 | // This is DR275, which we do not retroactively apply to C++98/03. | |||
8856 | if (WasQualifiedName) { | |||
8857 | if (CurContext->Encloses(OrigContext)) | |||
8858 | return false; | |||
8859 | } else { | |||
8860 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) | |||
8861 | return false; | |||
8862 | } | |||
8863 | ||||
8864 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { | |||
8865 | if (WasQualifiedName) | |||
8866 | S.Diag(InstLoc, | |||
8867 | S.getLangOpts().CPlusPlus11? | |||
8868 | diag::err_explicit_instantiation_out_of_scope : | |||
8869 | diag::warn_explicit_instantiation_out_of_scope_0x) | |||
8870 | << D << NS; | |||
8871 | else | |||
8872 | S.Diag(InstLoc, | |||
8873 | S.getLangOpts().CPlusPlus11? | |||
8874 | diag::err_explicit_instantiation_unqualified_wrong_namespace : | |||
8875 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) | |||
8876 | << D << NS; | |||
8877 | } else | |||
8878 | S.Diag(InstLoc, | |||
8879 | S.getLangOpts().CPlusPlus11? | |||
8880 | diag::err_explicit_instantiation_must_be_global : | |||
8881 | diag::warn_explicit_instantiation_must_be_global_0x) | |||
8882 | << D; | |||
8883 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); | |||
8884 | return false; | |||
8885 | } | |||
8886 | ||||
8887 | /// Common checks for whether an explicit instantiation of \p D is valid. | |||
8888 | static bool CheckExplicitInstantiation(Sema &S, NamedDecl *D, | |||
8889 | SourceLocation InstLoc, | |||
8890 | bool WasQualifiedName, | |||
8891 | TemplateSpecializationKind TSK) { | |||
8892 | // C++ [temp.explicit]p13: | |||
8893 | // An explicit instantiation declaration shall not name a specialization of | |||
8894 | // a template with internal linkage. | |||
8895 | if (TSK == TSK_ExplicitInstantiationDeclaration && | |||
8896 | D->getFormalLinkage() == InternalLinkage) { | |||
8897 | S.Diag(InstLoc, diag::err_explicit_instantiation_internal_linkage) << D; | |||
8898 | return true; | |||
8899 | } | |||
8900 | ||||
8901 | // C++11 [temp.explicit]p3: [DR 275] | |||
8902 | // An explicit instantiation shall appear in an enclosing namespace of its | |||
8903 | // template. | |||
8904 | if (CheckExplicitInstantiationScope(S, D, InstLoc, WasQualifiedName)) | |||
8905 | return true; | |||
8906 | ||||
8907 | return false; | |||
8908 | } | |||
8909 | ||||
8910 | /// Determine whether the given scope specifier has a template-id in it. | |||
8911 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { | |||
8912 | if (!SS.isSet()) | |||
8913 | return false; | |||
8914 | ||||
8915 | // C++11 [temp.explicit]p3: | |||
8916 | // If the explicit instantiation is for a member function, a member class | |||
8917 | // or a static data member of a class template specialization, the name of | |||
8918 | // the class template specialization in the qualified-id for the member | |||
8919 | // name shall be a simple-template-id. | |||
8920 | // | |||
8921 | // C++98 has the same restriction, just worded differently. | |||
8922 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; | |||
8923 | NNS = NNS->getPrefix()) | |||
8924 | if (const Type *T = NNS->getAsType()) | |||
8925 | if (isa<TemplateSpecializationType>(T)) | |||
8926 | return true; | |||
8927 | ||||
8928 | return false; | |||
8929 | } | |||
8930 | ||||
8931 | /// Make a dllexport or dllimport attr on a class template specialization take | |||
8932 | /// effect. | |||
8933 | static void dllExportImportClassTemplateSpecialization( | |||
8934 | Sema &S, ClassTemplateSpecializationDecl *Def) { | |||
8935 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); | |||
8936 | assert(A && "dllExportImportClassTemplateSpecialization called "((A && "dllExportImportClassTemplateSpecialization called " "on Def without dllexport or dllimport") ? static_cast<void > (0) : __assert_fail ("A && \"dllExportImportClassTemplateSpecialization called \" \"on Def without dllexport or dllimport\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8937, __PRETTY_FUNCTION__)) | |||
8937 | "on Def without dllexport or dllimport")((A && "dllExportImportClassTemplateSpecialization called " "on Def without dllexport or dllimport") ? static_cast<void > (0) : __assert_fail ("A && \"dllExportImportClassTemplateSpecialization called \" \"on Def without dllexport or dllimport\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8937, __PRETTY_FUNCTION__)); | |||
8938 | ||||
8939 | // We reject explicit instantiations in class scope, so there should | |||
8940 | // never be any delayed exported classes to worry about. | |||
8941 | assert(S.DelayedDllExportClasses.empty() &&((S.DelayedDllExportClasses.empty() && "delayed exports present at explicit instantiation" ) ? static_cast<void> (0) : __assert_fail ("S.DelayedDllExportClasses.empty() && \"delayed exports present at explicit instantiation\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8942, __PRETTY_FUNCTION__)) | |||
8942 | "delayed exports present at explicit instantiation")((S.DelayedDllExportClasses.empty() && "delayed exports present at explicit instantiation" ) ? static_cast<void> (0) : __assert_fail ("S.DelayedDllExportClasses.empty() && \"delayed exports present at explicit instantiation\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8942, __PRETTY_FUNCTION__)); | |||
8943 | S.checkClassLevelDLLAttribute(Def); | |||
8944 | ||||
8945 | // Propagate attribute to base class templates. | |||
8946 | for (auto &B : Def->bases()) { | |||
8947 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( | |||
8948 | B.getType()->getAsCXXRecordDecl())) | |||
8949 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); | |||
8950 | } | |||
8951 | ||||
8952 | S.referenceDLLExportedClassMethods(); | |||
8953 | } | |||
8954 | ||||
8955 | // Explicit instantiation of a class template specialization | |||
8956 | DeclResult Sema::ActOnExplicitInstantiation( | |||
8957 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, | |||
8958 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, | |||
8959 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, | |||
8960 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, | |||
8961 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { | |||
8962 | // Find the class template we're specializing | |||
8963 | TemplateName Name = TemplateD.get(); | |||
8964 | TemplateDecl *TD = Name.getAsTemplateDecl(); | |||
8965 | // Check that the specialization uses the same tag kind as the | |||
8966 | // original template. | |||
8967 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | |||
8968 | assert(Kind != TTK_Enum &&((Kind != TTK_Enum && "Invalid enum tag in class template explicit instantiation!" ) ? static_cast<void> (0) : __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template explicit instantiation!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8969, __PRETTY_FUNCTION__)) | |||
8969 | "Invalid enum tag in class template explicit instantiation!")((Kind != TTK_Enum && "Invalid enum tag in class template explicit instantiation!" ) ? static_cast<void> (0) : __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template explicit instantiation!\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 8969, __PRETTY_FUNCTION__)); | |||
8970 | ||||
8971 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); | |||
8972 | ||||
8973 | if (!ClassTemplate) { | |||
8974 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); | |||
8975 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; | |||
8976 | Diag(TD->getLocation(), diag::note_previous_use); | |||
8977 | return true; | |||
8978 | } | |||
8979 | ||||
8980 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), | |||
8981 | Kind, /*isDefinition*/false, KWLoc, | |||
8982 | ClassTemplate->getIdentifier())) { | |||
8983 | Diag(KWLoc, diag::err_use_with_wrong_tag) | |||
8984 | << ClassTemplate | |||
8985 | << FixItHint::CreateReplacement(KWLoc, | |||
8986 | ClassTemplate->getTemplatedDecl()->getKindName()); | |||
8987 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), | |||
8988 | diag::note_previous_use); | |||
8989 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); | |||
8990 | } | |||
8991 | ||||
8992 | // C++0x [temp.explicit]p2: | |||
8993 | // There are two forms of explicit instantiation: an explicit instantiation | |||
8994 | // definition and an explicit instantiation declaration. An explicit | |||
8995 | // instantiation declaration begins with the extern keyword. [...] | |||
8996 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() | |||
8997 | ? TSK_ExplicitInstantiationDefinition | |||
8998 | : TSK_ExplicitInstantiationDeclaration; | |||
8999 | ||||
9000 | if (TSK == TSK_ExplicitInstantiationDeclaration && | |||
9001 | !Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { | |||
9002 | // Check for dllexport class template instantiation declarations, | |||
9003 | // except for MinGW mode. | |||
9004 | for (const ParsedAttr &AL : Attr) { | |||
9005 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { | |||
9006 | Diag(ExternLoc, | |||
9007 | diag::warn_attribute_dllexport_explicit_instantiation_decl); | |||
9008 | Diag(AL.getLoc(), diag::note_attribute); | |||
9009 | break; | |||
9010 | } | |||
9011 | } | |||
9012 | ||||
9013 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { | |||
9014 | Diag(ExternLoc, | |||
9015 | diag::warn_attribute_dllexport_explicit_instantiation_decl); | |||
9016 | Diag(A->getLocation(), diag::note_attribute); | |||
9017 | } | |||
9018 | } | |||
9019 | ||||
9020 | // In MSVC mode, dllimported explicit instantiation definitions are treated as | |||
9021 | // instantiation declarations for most purposes. | |||
9022 | bool DLLImportExplicitInstantiationDef = false; | |||
9023 | if (TSK == TSK_ExplicitInstantiationDefinition && | |||
9024 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { | |||
9025 | // Check for dllimport class template instantiation definitions. | |||
9026 | bool DLLImport = | |||
9027 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); | |||
9028 | for (const ParsedAttr &AL : Attr) { | |||
9029 | if (AL.getKind() == ParsedAttr::AT_DLLImport) | |||
9030 | DLLImport = true; | |||
9031 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { | |||
9032 | // dllexport trumps dllimport here. | |||
9033 | DLLImport = false; | |||
9034 | break; | |||
9035 | } | |||
9036 | } | |||
9037 | if (DLLImport) { | |||
9038 | TSK = TSK_ExplicitInstantiationDeclaration; | |||
9039 | DLLImportExplicitInstantiationDef = true; | |||
9040 | } | |||
9041 | } | |||
9042 | ||||
9043 | // Translate the parser's template argument list in our AST format. | |||
9044 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | |||
9045 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | |||
9046 | ||||
9047 | // Check that the template argument list is well-formed for this | |||
9048 | // template. | |||
9049 | SmallVector<TemplateArgument, 4> Converted; | |||
9050 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, | |||
9051 | TemplateArgs, false, Converted, | |||
9052 | /*UpdateArgsWithConversion=*/true)) | |||
9053 | return true; | |||
9054 | ||||
9055 | // Find the class template specialization declaration that | |||
9056 | // corresponds to these arguments. | |||
9057 | void *InsertPos = nullptr; | |||
9058 | ClassTemplateSpecializationDecl *PrevDecl | |||
9059 | = ClassTemplate->findSpecialization(Converted, InsertPos); | |||
9060 | ||||
9061 | TemplateSpecializationKind PrevDecl_TSK | |||
9062 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; | |||
9063 | ||||
9064 | if (TSK == TSK_ExplicitInstantiationDefinition && PrevDecl != nullptr && | |||
9065 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { | |||
9066 | // Check for dllexport class template instantiation definitions in MinGW | |||
9067 | // mode, if a previous declaration of the instantiation was seen. | |||
9068 | for (const ParsedAttr &AL : Attr) { | |||
9069 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { | |||
9070 | Diag(AL.getLoc(), | |||
9071 | diag::warn_attribute_dllexport_explicit_instantiation_def); | |||
9072 | break; | |||
9073 | } | |||
9074 | } | |||
9075 | } | |||
9076 | ||||
9077 | if (CheckExplicitInstantiation(*this, ClassTemplate, TemplateNameLoc, | |||
9078 | SS.isSet(), TSK)) | |||
9079 | return true; | |||
9080 | ||||
9081 | ClassTemplateSpecializationDecl *Specialization = nullptr; | |||
9082 | ||||
9083 | bool HasNoEffect = false; | |||
9084 | if (PrevDecl) { | |||
9085 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, | |||
9086 | PrevDecl, PrevDecl_TSK, | |||
9087 | PrevDecl->getPointOfInstantiation(), | |||
9088 | HasNoEffect)) | |||
9089 | return PrevDecl; | |||
9090 | ||||
9091 | // Even though HasNoEffect == true means that this explicit instantiation | |||
9092 | // has no effect on semantics, we go on to put its syntax in the AST. | |||
9093 | ||||
9094 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || | |||
9095 | PrevDecl_TSK == TSK_Undeclared) { | |||
9096 | // Since the only prior class template specialization with these | |||
9097 | // arguments was referenced but not declared, reuse that | |||
9098 | // declaration node as our own, updating the source location | |||
9099 | // for the template name to reflect our new declaration. | |||
9100 | // (Other source locations will be updated later.) | |||
9101 | Specialization = PrevDecl; | |||
9102 | Specialization->setLocation(TemplateNameLoc); | |||
9103 | PrevDecl = nullptr; | |||
9104 | } | |||
9105 | ||||
9106 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && | |||
9107 | DLLImportExplicitInstantiationDef) { | |||
9108 | // The new specialization might add a dllimport attribute. | |||
9109 | HasNoEffect = false; | |||
9110 | } | |||
9111 | } | |||
9112 | ||||
9113 | if (!Specialization) { | |||
9114 | // Create a new class template specialization declaration node for | |||
9115 | // this explicit specialization. | |||
9116 | Specialization | |||
9117 | = ClassTemplateSpecializationDecl::Create(Context, Kind, | |||
9118 | ClassTemplate->getDeclContext(), | |||
9119 | KWLoc, TemplateNameLoc, | |||
9120 | ClassTemplate, | |||
9121 | Converted, | |||
9122 | PrevDecl); | |||
9123 | SetNestedNameSpecifier(*this, Specialization, SS); | |||
9124 | ||||
9125 | if (!HasNoEffect && !PrevDecl) { | |||
9126 | // Insert the new specialization. | |||
9127 | ClassTemplate->AddSpecialization(Specialization, InsertPos); | |||
9128 | } | |||
9129 | } | |||
9130 | ||||
9131 | // Build the fully-sugared type for this explicit instantiation as | |||
9132 | // the user wrote in the explicit instantiation itself. This means | |||
9133 | // that we'll pretty-print the type retrieved from the | |||
9134 | // specialization's declaration the way that the user actually wrote | |||
9135 | // the explicit instantiation, rather than formatting the name based | |||
9136 | // on the "canonical" representation used to store the template | |||
9137 | // arguments in the specialization. | |||
9138 | TypeSourceInfo *WrittenTy | |||
9139 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, | |||
9140 | TemplateArgs, | |||
9141 | Context.getTypeDeclType(Specialization)); | |||
9142 | Specialization->setTypeAsWritten(WrittenTy); | |||
9143 | ||||
9144 | // Set source locations for keywords. | |||
9145 | Specialization->setExternLoc(ExternLoc); | |||
9146 | Specialization->setTemplateKeywordLoc(TemplateLoc); | |||
9147 | Specialization->setBraceRange(SourceRange()); | |||
9148 | ||||
9149 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); | |||
9150 | ProcessDeclAttributeList(S, Specialization, Attr); | |||
9151 | ||||
9152 | // Add the explicit instantiation into its lexical context. However, | |||
9153 | // since explicit instantiations are never found by name lookup, we | |||
9154 | // just put it into the declaration context directly. | |||
9155 | Specialization->setLexicalDeclContext(CurContext); | |||
9156 | CurContext->addDecl(Specialization); | |||
9157 | ||||
9158 | // Syntax is now OK, so return if it has no other effect on semantics. | |||
9159 | if (HasNoEffect) { | |||
9160 | // Set the template specialization kind. | |||
9161 | Specialization->setTemplateSpecializationKind(TSK); | |||
9162 | return Specialization; | |||
9163 | } | |||
9164 | ||||
9165 | // C++ [temp.explicit]p3: | |||
9166 | // A definition of a class template or class member template | |||
9167 | // shall be in scope at the point of the explicit instantiation of | |||
9168 | // the class template or class member template. | |||
9169 | // | |||
9170 | // This check comes when we actually try to perform the | |||
9171 | // instantiation. | |||
9172 | ClassTemplateSpecializationDecl *Def | |||
9173 | = cast_or_null<ClassTemplateSpecializationDecl>( | |||
9174 | Specialization->getDefinition()); | |||
9175 | if (!Def) | |||
9176 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); | |||
9177 | else if (TSK == TSK_ExplicitInstantiationDefinition) { | |||
9178 | MarkVTableUsed(TemplateNameLoc, Specialization, true); | |||
9179 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); | |||
9180 | } | |||
9181 | ||||
9182 | // Instantiate the members of this class template specialization. | |||
9183 | Def = cast_or_null<ClassTemplateSpecializationDecl>( | |||
9184 | Specialization->getDefinition()); | |||
9185 | if (Def) { | |||
9186 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); | |||
9187 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a | |||
9188 | // TSK_ExplicitInstantiationDefinition | |||
9189 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && | |||
9190 | (TSK == TSK_ExplicitInstantiationDefinition || | |||
9191 | DLLImportExplicitInstantiationDef)) { | |||
9192 | // FIXME: Need to notify the ASTMutationListener that we did this. | |||
9193 | Def->setTemplateSpecializationKind(TSK); | |||
9194 | ||||
9195 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && | |||
9196 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || | |||
9197 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { | |||
9198 | // In the MS ABI, an explicit instantiation definition can add a dll | |||
9199 | // attribute to a template with a previous instantiation declaration. | |||
9200 | // MinGW doesn't allow this. | |||
9201 | auto *A = cast<InheritableAttr>( | |||
9202 | getDLLAttr(Specialization)->clone(getASTContext())); | |||
9203 | A->setInherited(true); | |||
9204 | Def->addAttr(A); | |||
9205 | dllExportImportClassTemplateSpecialization(*this, Def); | |||
9206 | } | |||
9207 | } | |||
9208 | ||||
9209 | // Fix a TSK_ImplicitInstantiation followed by a | |||
9210 | // TSK_ExplicitInstantiationDefinition | |||
9211 | bool NewlyDLLExported = | |||
9212 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); | |||
9213 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && | |||
9214 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || | |||
9215 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { | |||
9216 | // In the MS ABI, an explicit instantiation definition can add a dll | |||
9217 | // attribute to a template with a previous implicit instantiation. | |||
9218 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to | |||
9219 | // avoid potentially strange codegen behavior. For example, if we extend | |||
9220 | // this conditional to dllimport, and we have a source file calling a | |||
9221 | // method on an implicitly instantiated template class instance and then | |||
9222 | // declaring a dllimport explicit instantiation definition for the same | |||
9223 | // template class, the codegen for the method call will not respect the | |||
9224 | // dllimport, while it will with cl. The Def will already have the DLL | |||
9225 | // attribute, since the Def and Specialization will be the same in the | |||
9226 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the | |||
9227 | // attribute to the Specialization; we just need to make it take effect. | |||
9228 | assert(Def == Specialization &&((Def == Specialization && "Def and Specialization should match for implicit instantiation" ) ? static_cast<void> (0) : __assert_fail ("Def == Specialization && \"Def and Specialization should match for implicit instantiation\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9229, __PRETTY_FUNCTION__)) | |||
9229 | "Def and Specialization should match for implicit instantiation")((Def == Specialization && "Def and Specialization should match for implicit instantiation" ) ? static_cast<void> (0) : __assert_fail ("Def == Specialization && \"Def and Specialization should match for implicit instantiation\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9229, __PRETTY_FUNCTION__)); | |||
9230 | dllExportImportClassTemplateSpecialization(*this, Def); | |||
9231 | } | |||
9232 | ||||
9233 | // In MinGW mode, export the template instantiation if the declaration | |||
9234 | // was marked dllexport. | |||
9235 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && | |||
9236 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment() && | |||
9237 | PrevDecl->hasAttr<DLLExportAttr>()) { | |||
9238 | dllExportImportClassTemplateSpecialization(*this, Def); | |||
9239 | } | |||
9240 | ||||
9241 | // Set the template specialization kind. Make sure it is set before | |||
9242 | // instantiating the members which will trigger ASTConsumer callbacks. | |||
9243 | Specialization->setTemplateSpecializationKind(TSK); | |||
9244 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); | |||
9245 | } else { | |||
9246 | ||||
9247 | // Set the template specialization kind. | |||
9248 | Specialization->setTemplateSpecializationKind(TSK); | |||
9249 | } | |||
9250 | ||||
9251 | return Specialization; | |||
9252 | } | |||
9253 | ||||
9254 | // Explicit instantiation of a member class of a class template. | |||
9255 | DeclResult | |||
9256 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, | |||
9257 | SourceLocation TemplateLoc, unsigned TagSpec, | |||
9258 | SourceLocation KWLoc, CXXScopeSpec &SS, | |||
9259 | IdentifierInfo *Name, SourceLocation NameLoc, | |||
9260 | const ParsedAttributesView &Attr) { | |||
9261 | ||||
9262 | bool Owned = false; | |||
9263 | bool IsDependent = false; | |||
9264 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, | |||
9265 | KWLoc, SS, Name, NameLoc, Attr, AS_none, | |||
9266 | /*ModulePrivateLoc=*/SourceLocation(), | |||
9267 | MultiTemplateParamsArg(), Owned, IsDependent, | |||
9268 | SourceLocation(), false, TypeResult(), | |||
9269 | /*IsTypeSpecifier*/false, | |||
9270 | /*IsTemplateParamOrArg*/false); | |||
9271 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled")((!IsDependent && "explicit instantiation of dependent name not yet handled" ) ? static_cast<void> (0) : __assert_fail ("!IsDependent && \"explicit instantiation of dependent name not yet handled\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9271, __PRETTY_FUNCTION__)); | |||
9272 | ||||
9273 | if (!TagD) | |||
9274 | return true; | |||
9275 | ||||
9276 | TagDecl *Tag = cast<TagDecl>(TagD); | |||
9277 | assert(!Tag->isEnum() && "shouldn't see enumerations here")((!Tag->isEnum() && "shouldn't see enumerations here" ) ? static_cast<void> (0) : __assert_fail ("!Tag->isEnum() && \"shouldn't see enumerations here\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9277, __PRETTY_FUNCTION__)); | |||
9278 | ||||
9279 | if (Tag->isInvalidDecl()) | |||
9280 | return true; | |||
9281 | ||||
9282 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); | |||
9283 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); | |||
9284 | if (!Pattern) { | |||
9285 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) | |||
9286 | << Context.getTypeDeclType(Record); | |||
9287 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); | |||
9288 | return true; | |||
9289 | } | |||
9290 | ||||
9291 | // C++0x [temp.explicit]p2: | |||
9292 | // If the explicit instantiation is for a class or member class, the | |||
9293 | // elaborated-type-specifier in the declaration shall include a | |||
9294 | // simple-template-id. | |||
9295 | // | |||
9296 | // C++98 has the same restriction, just worded differently. | |||
9297 | if (!ScopeSpecifierHasTemplateId(SS)) | |||
9298 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) | |||
9299 | << Record << SS.getRange(); | |||
9300 | ||||
9301 | // C++0x [temp.explicit]p2: | |||
9302 | // There are two forms of explicit instantiation: an explicit instantiation | |||
9303 | // definition and an explicit instantiation declaration. An explicit | |||
9304 | // instantiation declaration begins with the extern keyword. [...] | |||
9305 | TemplateSpecializationKind TSK | |||
9306 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition | |||
9307 | : TSK_ExplicitInstantiationDeclaration; | |||
9308 | ||||
9309 | CheckExplicitInstantiation(*this, Record, NameLoc, true, TSK); | |||
9310 | ||||
9311 | // Verify that it is okay to explicitly instantiate here. | |||
9312 | CXXRecordDecl *PrevDecl | |||
9313 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); | |||
9314 | if (!PrevDecl && Record->getDefinition()) | |||
9315 | PrevDecl = Record; | |||
9316 | if (PrevDecl) { | |||
9317 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); | |||
9318 | bool HasNoEffect = false; | |||
9319 | assert(MSInfo && "No member specialization information?")((MSInfo && "No member specialization information?") ? static_cast<void> (0) : __assert_fail ("MSInfo && \"No member specialization information?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9319, __PRETTY_FUNCTION__)); | |||
9320 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, | |||
9321 | PrevDecl, | |||
9322 | MSInfo->getTemplateSpecializationKind(), | |||
9323 | MSInfo->getPointOfInstantiation(), | |||
9324 | HasNoEffect)) | |||
9325 | return true; | |||
9326 | if (HasNoEffect) | |||
9327 | return TagD; | |||
9328 | } | |||
9329 | ||||
9330 | CXXRecordDecl *RecordDef | |||
9331 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); | |||
9332 | if (!RecordDef) { | |||
9333 | // C++ [temp.explicit]p3: | |||
9334 | // A definition of a member class of a class template shall be in scope | |||
9335 | // at the point of an explicit instantiation of the member class. | |||
9336 | CXXRecordDecl *Def | |||
9337 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); | |||
9338 | if (!Def) { | |||
9339 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) | |||
9340 | << 0 << Record->getDeclName() << Record->getDeclContext(); | |||
9341 | Diag(Pattern->getLocation(), diag::note_forward_declaration) | |||
9342 | << Pattern; | |||
9343 | return true; | |||
9344 | } else { | |||
9345 | if (InstantiateClass(NameLoc, Record, Def, | |||
9346 | getTemplateInstantiationArgs(Record), | |||
9347 | TSK)) | |||
9348 | return true; | |||
9349 | ||||
9350 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); | |||
9351 | if (!RecordDef) | |||
9352 | return true; | |||
9353 | } | |||
9354 | } | |||
9355 | ||||
9356 | // Instantiate all of the members of the class. | |||
9357 | InstantiateClassMembers(NameLoc, RecordDef, | |||
9358 | getTemplateInstantiationArgs(Record), TSK); | |||
9359 | ||||
9360 | if (TSK == TSK_ExplicitInstantiationDefinition) | |||
9361 | MarkVTableUsed(NameLoc, RecordDef, true); | |||
9362 | ||||
9363 | // FIXME: We don't have any representation for explicit instantiations of | |||
9364 | // member classes. Such a representation is not needed for compilation, but it | |||
9365 | // should be available for clients that want to see all of the declarations in | |||
9366 | // the source code. | |||
9367 | return TagD; | |||
9368 | } | |||
9369 | ||||
9370 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, | |||
9371 | SourceLocation ExternLoc, | |||
9372 | SourceLocation TemplateLoc, | |||
9373 | Declarator &D) { | |||
9374 | // Explicit instantiations always require a name. | |||
9375 | // TODO: check if/when DNInfo should replace Name. | |||
9376 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | |||
9377 | DeclarationName Name = NameInfo.getName(); | |||
9378 | if (!Name) { | |||
9379 | if (!D.isInvalidType()) | |||
9380 | Diag(D.getDeclSpec().getBeginLoc(), | |||
9381 | diag::err_explicit_instantiation_requires_name) | |||
9382 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); | |||
9383 | ||||
9384 | return true; | |||
9385 | } | |||
9386 | ||||
9387 | // The scope passed in may not be a decl scope. Zip up the scope tree until | |||
9388 | // we find one that is. | |||
9389 | while ((S->getFlags() & Scope::DeclScope) == 0 || | |||
9390 | (S->getFlags() & Scope::TemplateParamScope) != 0) | |||
9391 | S = S->getParent(); | |||
9392 | ||||
9393 | // Determine the type of the declaration. | |||
9394 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); | |||
9395 | QualType R = T->getType(); | |||
9396 | if (R.isNull()) | |||
9397 | return true; | |||
9398 | ||||
9399 | // C++ [dcl.stc]p1: | |||
9400 | // A storage-class-specifier shall not be specified in [...] an explicit | |||
9401 | // instantiation (14.7.2) directive. | |||
9402 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { | |||
9403 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) | |||
9404 | << Name; | |||
9405 | return true; | |||
9406 | } else if (D.getDeclSpec().getStorageClassSpec() | |||
9407 | != DeclSpec::SCS_unspecified) { | |||
9408 | // Complain about then remove the storage class specifier. | |||
9409 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) | |||
9410 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | |||
9411 | ||||
9412 | D.getMutableDeclSpec().ClearStorageClassSpecs(); | |||
9413 | } | |||
9414 | ||||
9415 | // C++0x [temp.explicit]p1: | |||
9416 | // [...] An explicit instantiation of a function template shall not use the | |||
9417 | // inline or constexpr specifiers. | |||
9418 | // Presumably, this also applies to member functions of class templates as | |||
9419 | // well. | |||
9420 | if (D.getDeclSpec().isInlineSpecified()) | |||
9421 | Diag(D.getDeclSpec().getInlineSpecLoc(), | |||
9422 | getLangOpts().CPlusPlus11 ? | |||
9423 | diag::err_explicit_instantiation_inline : | |||
9424 | diag::warn_explicit_instantiation_inline_0x) | |||
9425 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); | |||
9426 | if (D.getDeclSpec().hasConstexprSpecifier() && R->isFunctionType()) | |||
9427 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is | |||
9428 | // not already specified. | |||
9429 | Diag(D.getDeclSpec().getConstexprSpecLoc(), | |||
9430 | diag::err_explicit_instantiation_constexpr); | |||
9431 | ||||
9432 | // A deduction guide is not on the list of entities that can be explicitly | |||
9433 | // instantiated. | |||
9434 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { | |||
9435 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) | |||
9436 | << /*explicit instantiation*/ 0; | |||
9437 | return true; | |||
9438 | } | |||
9439 | ||||
9440 | // C++0x [temp.explicit]p2: | |||
9441 | // There are two forms of explicit instantiation: an explicit instantiation | |||
9442 | // definition and an explicit instantiation declaration. An explicit | |||
9443 | // instantiation declaration begins with the extern keyword. [...] | |||
9444 | TemplateSpecializationKind TSK | |||
9445 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition | |||
9446 | : TSK_ExplicitInstantiationDeclaration; | |||
9447 | ||||
9448 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); | |||
9449 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); | |||
9450 | ||||
9451 | if (!R->isFunctionType()) { | |||
9452 | // C++ [temp.explicit]p1: | |||
9453 | // A [...] static data member of a class template can be explicitly | |||
9454 | // instantiated from the member definition associated with its class | |||
9455 | // template. | |||
9456 | // C++1y [temp.explicit]p1: | |||
9457 | // A [...] variable [...] template specialization can be explicitly | |||
9458 | // instantiated from its template. | |||
9459 | if (Previous.isAmbiguous()) | |||
9460 | return true; | |||
9461 | ||||
9462 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); | |||
9463 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); | |||
9464 | ||||
9465 | if (!PrevTemplate) { | |||
9466 | if (!Prev || !Prev->isStaticDataMember()) { | |||
9467 | // We expect to see a static data member here. | |||
9468 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) | |||
9469 | << Name; | |||
9470 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); | |||
9471 | P != PEnd; ++P) | |||
9472 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); | |||
9473 | return true; | |||
9474 | } | |||
9475 | ||||
9476 | if (!Prev->getInstantiatedFromStaticDataMember()) { | |||
9477 | // FIXME: Check for explicit specialization? | |||
9478 | Diag(D.getIdentifierLoc(), | |||
9479 | diag::err_explicit_instantiation_data_member_not_instantiated) | |||
9480 | << Prev; | |||
9481 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); | |||
9482 | // FIXME: Can we provide a note showing where this was declared? | |||
9483 | return true; | |||
9484 | } | |||
9485 | } else { | |||
9486 | // Explicitly instantiate a variable template. | |||
9487 | ||||
9488 | // C++1y [dcl.spec.auto]p6: | |||
9489 | // ... A program that uses auto or decltype(auto) in a context not | |||
9490 | // explicitly allowed in this section is ill-formed. | |||
9491 | // | |||
9492 | // This includes auto-typed variable template instantiations. | |||
9493 | if (R->isUndeducedType()) { | |||
9494 | Diag(T->getTypeLoc().getBeginLoc(), | |||
9495 | diag::err_auto_not_allowed_var_inst); | |||
9496 | return true; | |||
9497 | } | |||
9498 | ||||
9499 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { | |||
9500 | // C++1y [temp.explicit]p3: | |||
9501 | // If the explicit instantiation is for a variable, the unqualified-id | |||
9502 | // in the declaration shall be a template-id. | |||
9503 | Diag(D.getIdentifierLoc(), | |||
9504 | diag::err_explicit_instantiation_without_template_id) | |||
9505 | << PrevTemplate; | |||
9506 | Diag(PrevTemplate->getLocation(), | |||
9507 | diag::note_explicit_instantiation_here); | |||
9508 | return true; | |||
9509 | } | |||
9510 | ||||
9511 | // Translate the parser's template argument list into our AST format. | |||
9512 | TemplateArgumentListInfo TemplateArgs = | |||
9513 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); | |||
9514 | ||||
9515 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, | |||
9516 | D.getIdentifierLoc(), TemplateArgs); | |||
9517 | if (Res.isInvalid()) | |||
9518 | return true; | |||
9519 | ||||
9520 | // Ignore access control bits, we don't need them for redeclaration | |||
9521 | // checking. | |||
9522 | Prev = cast<VarDecl>(Res.get()); | |||
9523 | } | |||
9524 | ||||
9525 | // C++0x [temp.explicit]p2: | |||
9526 | // If the explicit instantiation is for a member function, a member class | |||
9527 | // or a static data member of a class template specialization, the name of | |||
9528 | // the class template specialization in the qualified-id for the member | |||
9529 | // name shall be a simple-template-id. | |||
9530 | // | |||
9531 | // C++98 has the same restriction, just worded differently. | |||
9532 | // | |||
9533 | // This does not apply to variable template specializations, where the | |||
9534 | // template-id is in the unqualified-id instead. | |||
9535 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) | |||
9536 | Diag(D.getIdentifierLoc(), | |||
9537 | diag::ext_explicit_instantiation_without_qualified_id) | |||
9538 | << Prev << D.getCXXScopeSpec().getRange(); | |||
9539 | ||||
9540 | CheckExplicitInstantiation(*this, Prev, D.getIdentifierLoc(), true, TSK); | |||
9541 | ||||
9542 | // Verify that it is okay to explicitly instantiate here. | |||
9543 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); | |||
9544 | SourceLocation POI = Prev->getPointOfInstantiation(); | |||
9545 | bool HasNoEffect = false; | |||
9546 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, | |||
9547 | PrevTSK, POI, HasNoEffect)) | |||
9548 | return true; | |||
9549 | ||||
9550 | if (!HasNoEffect) { | |||
9551 | // Instantiate static data member or variable template. | |||
9552 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); | |||
9553 | // Merge attributes. | |||
9554 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); | |||
9555 | if (TSK == TSK_ExplicitInstantiationDefinition) | |||
9556 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); | |||
9557 | } | |||
9558 | ||||
9559 | // Check the new variable specialization against the parsed input. | |||
9560 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { | |||
9561 | Diag(T->getTypeLoc().getBeginLoc(), | |||
9562 | diag::err_invalid_var_template_spec_type) | |||
9563 | << 0 << PrevTemplate << R << Prev->getType(); | |||
9564 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) | |||
9565 | << 2 << PrevTemplate->getDeclName(); | |||
9566 | return true; | |||
9567 | } | |||
9568 | ||||
9569 | // FIXME: Create an ExplicitInstantiation node? | |||
9570 | return (Decl*) nullptr; | |||
9571 | } | |||
9572 | ||||
9573 | // If the declarator is a template-id, translate the parser's template | |||
9574 | // argument list into our AST format. | |||
9575 | bool HasExplicitTemplateArgs = false; | |||
9576 | TemplateArgumentListInfo TemplateArgs; | |||
9577 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { | |||
9578 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); | |||
9579 | HasExplicitTemplateArgs = true; | |||
9580 | } | |||
9581 | ||||
9582 | // C++ [temp.explicit]p1: | |||
9583 | // A [...] function [...] can be explicitly instantiated from its template. | |||
9584 | // A member function [...] of a class template can be explicitly | |||
9585 | // instantiated from the member definition associated with its class | |||
9586 | // template. | |||
9587 | UnresolvedSet<8> TemplateMatches; | |||
9588 | FunctionDecl *NonTemplateMatch = nullptr; | |||
9589 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); | |||
9590 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); | |||
9591 | P != PEnd; ++P) { | |||
9592 | NamedDecl *Prev = *P; | |||
9593 | if (!HasExplicitTemplateArgs) { | |||
9594 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { | |||
9595 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), | |||
9596 | /*AdjustExceptionSpec*/true); | |||
9597 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { | |||
9598 | if (Method->getPrimaryTemplate()) { | |||
9599 | TemplateMatches.addDecl(Method, P.getAccess()); | |||
9600 | } else { | |||
9601 | // FIXME: Can this assert ever happen? Needs a test. | |||
9602 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches")((!NonTemplateMatch && "Multiple NonTemplateMatches") ? static_cast<void> (0) : __assert_fail ("!NonTemplateMatch && \"Multiple NonTemplateMatches\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9602, __PRETTY_FUNCTION__)); | |||
9603 | NonTemplateMatch = Method; | |||
9604 | } | |||
9605 | } | |||
9606 | } | |||
9607 | } | |||
9608 | ||||
9609 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); | |||
9610 | if (!FunTmpl) | |||
9611 | continue; | |||
9612 | ||||
9613 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); | |||
9614 | FunctionDecl *Specialization = nullptr; | |||
9615 | if (TemplateDeductionResult TDK | |||
9616 | = DeduceTemplateArguments(FunTmpl, | |||
9617 | (HasExplicitTemplateArgs ? &TemplateArgs | |||
9618 | : nullptr), | |||
9619 | R, Specialization, Info)) { | |||
9620 | // Keep track of almost-matches. | |||
9621 | FailedCandidates.addCandidate() | |||
9622 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), | |||
9623 | MakeDeductionFailureInfo(Context, TDK, Info)); | |||
9624 | (void)TDK; | |||
9625 | continue; | |||
9626 | } | |||
9627 | ||||
9628 | // Target attributes are part of the cuda function signature, so | |||
9629 | // the cuda target of the instantiated function must match that of its | |||
9630 | // template. Given that C++ template deduction does not take | |||
9631 | // target attributes into account, we reject candidates here that | |||
9632 | // have a different target. | |||
9633 | if (LangOpts.CUDA && | |||
9634 | IdentifyCUDATarget(Specialization, | |||
9635 | /* IgnoreImplicitHDAttr = */ true) != | |||
9636 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { | |||
9637 | FailedCandidates.addCandidate().set( | |||
9638 | P.getPair(), FunTmpl->getTemplatedDecl(), | |||
9639 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); | |||
9640 | continue; | |||
9641 | } | |||
9642 | ||||
9643 | TemplateMatches.addDecl(Specialization, P.getAccess()); | |||
9644 | } | |||
9645 | ||||
9646 | FunctionDecl *Specialization = NonTemplateMatch; | |||
9647 | if (!Specialization) { | |||
9648 | // Find the most specialized function template specialization. | |||
9649 | UnresolvedSetIterator Result = getMostSpecialized( | |||
9650 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, | |||
9651 | D.getIdentifierLoc(), | |||
9652 | PDiag(diag::err_explicit_instantiation_not_known) << Name, | |||
9653 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, | |||
9654 | PDiag(diag::note_explicit_instantiation_candidate)); | |||
9655 | ||||
9656 | if (Result == TemplateMatches.end()) | |||
9657 | return true; | |||
9658 | ||||
9659 | // Ignore access control bits, we don't need them for redeclaration checking. | |||
9660 | Specialization = cast<FunctionDecl>(*Result); | |||
9661 | } | |||
9662 | ||||
9663 | // C++11 [except.spec]p4 | |||
9664 | // In an explicit instantiation an exception-specification may be specified, | |||
9665 | // but is not required. | |||
9666 | // If an exception-specification is specified in an explicit instantiation | |||
9667 | // directive, it shall be compatible with the exception-specifications of | |||
9668 | // other declarations of that function. | |||
9669 | if (auto *FPT = R->getAs<FunctionProtoType>()) | |||
9670 | if (FPT->hasExceptionSpec()) { | |||
9671 | unsigned DiagID = | |||
9672 | diag::err_mismatched_exception_spec_explicit_instantiation; | |||
9673 | if (getLangOpts().MicrosoftExt) | |||
9674 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; | |||
9675 | bool Result = CheckEquivalentExceptionSpec( | |||
9676 | PDiag(DiagID) << Specialization->getType(), | |||
9677 | PDiag(diag::note_explicit_instantiation_here), | |||
9678 | Specialization->getType()->getAs<FunctionProtoType>(), | |||
9679 | Specialization->getLocation(), FPT, D.getBeginLoc()); | |||
9680 | // In Microsoft mode, mismatching exception specifications just cause a | |||
9681 | // warning. | |||
9682 | if (!getLangOpts().MicrosoftExt && Result) | |||
9683 | return true; | |||
9684 | } | |||
9685 | ||||
9686 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { | |||
9687 | Diag(D.getIdentifierLoc(), | |||
9688 | diag::err_explicit_instantiation_member_function_not_instantiated) | |||
9689 | << Specialization | |||
9690 | << (Specialization->getTemplateSpecializationKind() == | |||
9691 | TSK_ExplicitSpecialization); | |||
9692 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); | |||
9693 | return true; | |||
9694 | } | |||
9695 | ||||
9696 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); | |||
9697 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) | |||
9698 | PrevDecl = Specialization; | |||
9699 | ||||
9700 | if (PrevDecl) { | |||
9701 | bool HasNoEffect = false; | |||
9702 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, | |||
9703 | PrevDecl, | |||
9704 | PrevDecl->getTemplateSpecializationKind(), | |||
9705 | PrevDecl->getPointOfInstantiation(), | |||
9706 | HasNoEffect)) | |||
9707 | return true; | |||
9708 | ||||
9709 | // FIXME: We may still want to build some representation of this | |||
9710 | // explicit specialization. | |||
9711 | if (HasNoEffect) | |||
9712 | return (Decl*) nullptr; | |||
9713 | } | |||
9714 | ||||
9715 | // HACK: libc++ has a bug where it attempts to explicitly instantiate the | |||
9716 | // functions | |||
9717 | // valarray<size_t>::valarray(size_t) and | |||
9718 | // valarray<size_t>::~valarray() | |||
9719 | // that it declared to have internal linkage with the internal_linkage | |||
9720 | // attribute. Ignore the explicit instantiation declaration in this case. | |||
9721 | if (Specialization->hasAttr<InternalLinkageAttr>() && | |||
9722 | TSK == TSK_ExplicitInstantiationDeclaration) { | |||
9723 | if (auto *RD = dyn_cast<CXXRecordDecl>(Specialization->getDeclContext())) | |||
9724 | if (RD->getIdentifier() && RD->getIdentifier()->isStr("valarray") && | |||
9725 | RD->isInStdNamespace()) | |||
9726 | return (Decl*) nullptr; | |||
9727 | } | |||
9728 | ||||
9729 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); | |||
9730 | ||||
9731 | // In MSVC mode, dllimported explicit instantiation definitions are treated as | |||
9732 | // instantiation declarations. | |||
9733 | if (TSK == TSK_ExplicitInstantiationDefinition && | |||
9734 | Specialization->hasAttr<DLLImportAttr>() && | |||
9735 | Context.getTargetInfo().getCXXABI().isMicrosoft()) | |||
9736 | TSK = TSK_ExplicitInstantiationDeclaration; | |||
9737 | ||||
9738 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); | |||
9739 | ||||
9740 | if (Specialization->isDefined()) { | |||
9741 | // Let the ASTConsumer know that this function has been explicitly | |||
9742 | // instantiated now, and its linkage might have changed. | |||
9743 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); | |||
9744 | } else if (TSK == TSK_ExplicitInstantiationDefinition) | |||
9745 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); | |||
9746 | ||||
9747 | // C++0x [temp.explicit]p2: | |||
9748 | // If the explicit instantiation is for a member function, a member class | |||
9749 | // or a static data member of a class template specialization, the name of | |||
9750 | // the class template specialization in the qualified-id for the member | |||
9751 | // name shall be a simple-template-id. | |||
9752 | // | |||
9753 | // C++98 has the same restriction, just worded differently. | |||
9754 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); | |||
9755 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && | |||
9756 | D.getCXXScopeSpec().isSet() && | |||
9757 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) | |||
9758 | Diag(D.getIdentifierLoc(), | |||
9759 | diag::ext_explicit_instantiation_without_qualified_id) | |||
9760 | << Specialization << D.getCXXScopeSpec().getRange(); | |||
9761 | ||||
9762 | CheckExplicitInstantiation( | |||
9763 | *this, | |||
9764 | FunTmpl ? (NamedDecl *)FunTmpl | |||
9765 | : Specialization->getInstantiatedFromMemberFunction(), | |||
9766 | D.getIdentifierLoc(), D.getCXXScopeSpec().isSet(), TSK); | |||
9767 | ||||
9768 | // FIXME: Create some kind of ExplicitInstantiationDecl here. | |||
9769 | return (Decl*) nullptr; | |||
9770 | } | |||
9771 | ||||
9772 | TypeResult | |||
9773 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, | |||
9774 | const CXXScopeSpec &SS, IdentifierInfo *Name, | |||
9775 | SourceLocation TagLoc, SourceLocation NameLoc) { | |||
9776 | // This has to hold, because SS is expected to be defined. | |||
9777 | assert(Name && "Expected a name in a dependent tag")((Name && "Expected a name in a dependent tag") ? static_cast <void> (0) : __assert_fail ("Name && \"Expected a name in a dependent tag\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9777, __PRETTY_FUNCTION__)); | |||
9778 | ||||
9779 | NestedNameSpecifier *NNS = SS.getScopeRep(); | |||
9780 | if (!NNS) | |||
9781 | return true; | |||
9782 | ||||
9783 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | |||
9784 | ||||
9785 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { | |||
9786 | Diag(NameLoc, diag::err_dependent_tag_decl) | |||
9787 | << (TUK == TUK_Definition) << Kind << SS.getRange(); | |||
9788 | return true; | |||
9789 | } | |||
9790 | ||||
9791 | // Create the resulting type. | |||
9792 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); | |||
9793 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); | |||
9794 | ||||
9795 | // Create type-source location information for this type. | |||
9796 | TypeLocBuilder TLB; | |||
9797 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); | |||
9798 | TL.setElaboratedKeywordLoc(TagLoc); | |||
9799 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
9800 | TL.setNameLoc(NameLoc); | |||
9801 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | |||
9802 | } | |||
9803 | ||||
9804 | TypeResult | |||
9805 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, | |||
9806 | const CXXScopeSpec &SS, const IdentifierInfo &II, | |||
9807 | SourceLocation IdLoc) { | |||
9808 | if (SS.isInvalid()) | |||
9809 | return true; | |||
9810 | ||||
9811 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) | |||
9812 | Diag(TypenameLoc, | |||
9813 | getLangOpts().CPlusPlus11 ? | |||
9814 | diag::warn_cxx98_compat_typename_outside_of_template : | |||
9815 | diag::ext_typename_outside_of_template) | |||
9816 | << FixItHint::CreateRemoval(TypenameLoc); | |||
9817 | ||||
9818 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); | |||
9819 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, | |||
9820 | TypenameLoc, QualifierLoc, II, IdLoc); | |||
9821 | if (T.isNull()) | |||
9822 | return true; | |||
9823 | ||||
9824 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); | |||
9825 | if (isa<DependentNameType>(T)) { | |||
9826 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); | |||
9827 | TL.setElaboratedKeywordLoc(TypenameLoc); | |||
9828 | TL.setQualifierLoc(QualifierLoc); | |||
9829 | TL.setNameLoc(IdLoc); | |||
9830 | } else { | |||
9831 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); | |||
9832 | TL.setElaboratedKeywordLoc(TypenameLoc); | |||
9833 | TL.setQualifierLoc(QualifierLoc); | |||
9834 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); | |||
9835 | } | |||
9836 | ||||
9837 | return CreateParsedType(T, TSI); | |||
9838 | } | |||
9839 | ||||
9840 | TypeResult | |||
9841 | Sema::ActOnTypenameType(Scope *S, | |||
9842 | SourceLocation TypenameLoc, | |||
9843 | const CXXScopeSpec &SS, | |||
9844 | SourceLocation TemplateKWLoc, | |||
9845 | TemplateTy TemplateIn, | |||
9846 | IdentifierInfo *TemplateII, | |||
9847 | SourceLocation TemplateIILoc, | |||
9848 | SourceLocation LAngleLoc, | |||
9849 | ASTTemplateArgsPtr TemplateArgsIn, | |||
9850 | SourceLocation RAngleLoc) { | |||
9851 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) | |||
9852 | Diag(TypenameLoc, | |||
9853 | getLangOpts().CPlusPlus11 ? | |||
9854 | diag::warn_cxx98_compat_typename_outside_of_template : | |||
9855 | diag::ext_typename_outside_of_template) | |||
9856 | << FixItHint::CreateRemoval(TypenameLoc); | |||
9857 | ||||
9858 | // Strangely, non-type results are not ignored by this lookup, so the | |||
9859 | // program is ill-formed if it finds an injected-class-name. | |||
9860 | if (TypenameLoc.isValid()) { | |||
9861 | auto *LookupRD = | |||
9862 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); | |||
9863 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { | |||
9864 | Diag(TemplateIILoc, | |||
9865 | diag::ext_out_of_line_qualified_id_type_names_constructor) | |||
9866 | << TemplateII << 0 /*injected-class-name used as template name*/ | |||
9867 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); | |||
9868 | } | |||
9869 | } | |||
9870 | ||||
9871 | // Translate the parser's template argument list in our AST format. | |||
9872 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | |||
9873 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | |||
9874 | ||||
9875 | TemplateName Template = TemplateIn.get(); | |||
9876 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | |||
9877 | // Construct a dependent template specialization type. | |||
9878 | assert(DTN && "dependent template has non-dependent name?")((DTN && "dependent template has non-dependent name?" ) ? static_cast<void> (0) : __assert_fail ("DTN && \"dependent template has non-dependent name?\"" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9878, __PRETTY_FUNCTION__)); | |||
9879 | assert(DTN->getQualifier() == SS.getScopeRep())((DTN->getQualifier() == SS.getScopeRep()) ? static_cast< void> (0) : __assert_fail ("DTN->getQualifier() == SS.getScopeRep()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9879, __PRETTY_FUNCTION__)); | |||
9880 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, | |||
9881 | DTN->getQualifier(), | |||
9882 | DTN->getIdentifier(), | |||
9883 | TemplateArgs); | |||
9884 | ||||
9885 | // Create source-location information for this type. | |||
9886 | TypeLocBuilder Builder; | |||
9887 | DependentTemplateSpecializationTypeLoc SpecTL | |||
9888 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); | |||
9889 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); | |||
9890 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
9891 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
9892 | SpecTL.setTemplateNameLoc(TemplateIILoc); | |||
9893 | SpecTL.setLAngleLoc(LAngleLoc); | |||
9894 | SpecTL.setRAngleLoc(RAngleLoc); | |||
9895 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | |||
9896 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | |||
9897 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | |||
9898 | } | |||
9899 | ||||
9900 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); | |||
9901 | if (T.isNull()) | |||
9902 | return true; | |||
9903 | ||||
9904 | // Provide source-location information for the template specialization type. | |||
9905 | TypeLocBuilder Builder; | |||
9906 | TemplateSpecializationTypeLoc SpecTL | |||
9907 | = Builder.push<TemplateSpecializationTypeLoc>(T); | |||
9908 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | |||
9909 | SpecTL.setTemplateNameLoc(TemplateIILoc); | |||
9910 | SpecTL.setLAngleLoc(LAngleLoc); | |||
9911 | SpecTL.setRAngleLoc(RAngleLoc); | |||
9912 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | |||
9913 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | |||
9914 | ||||
9915 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); | |||
9916 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); | |||
9917 | TL.setElaboratedKeywordLoc(TypenameLoc); | |||
9918 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
9919 | ||||
9920 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); | |||
9921 | return CreateParsedType(T, TSI); | |||
9922 | } | |||
9923 | ||||
9924 | ||||
9925 | /// Determine whether this failed name lookup should be treated as being | |||
9926 | /// disabled by a usage of std::enable_if. | |||
9927 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, | |||
9928 | SourceRange &CondRange, Expr *&Cond) { | |||
9929 | // We must be looking for a ::type... | |||
9930 | if (!II.isStr("type")) | |||
9931 | return false; | |||
9932 | ||||
9933 | // ... within an explicitly-written template specialization... | |||
9934 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) | |||
9935 | return false; | |||
9936 | TypeLoc EnableIfTy = NNS.getTypeLoc(); | |||
9937 | TemplateSpecializationTypeLoc EnableIfTSTLoc = | |||
9938 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); | |||
9939 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) | |||
9940 | return false; | |||
9941 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); | |||
9942 | ||||
9943 | // ... which names a complete class template declaration... | |||
9944 | const TemplateDecl *EnableIfDecl = | |||
9945 | EnableIfTST->getTemplateName().getAsTemplateDecl(); | |||
9946 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) | |||
9947 | return false; | |||
9948 | ||||
9949 | // ... called "enable_if". | |||
9950 | const IdentifierInfo *EnableIfII = | |||
9951 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); | |||
9952 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) | |||
9953 | return false; | |||
9954 | ||||
9955 | // Assume the first template argument is the condition. | |||
9956 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); | |||
9957 | ||||
9958 | // Dig out the condition. | |||
9959 | Cond = nullptr; | |||
9960 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() | |||
9961 | != TemplateArgument::Expression) | |||
9962 | return true; | |||
9963 | ||||
9964 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); | |||
9965 | ||||
9966 | // Ignore Boolean literals; they add no value. | |||
9967 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) | |||
9968 | Cond = nullptr; | |||
9969 | ||||
9970 | return true; | |||
9971 | } | |||
9972 | ||||
9973 | /// Build the type that describes a C++ typename specifier, | |||
9974 | /// e.g., "typename T::type". | |||
9975 | QualType | |||
9976 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, | |||
9977 | SourceLocation KeywordLoc, | |||
9978 | NestedNameSpecifierLoc QualifierLoc, | |||
9979 | const IdentifierInfo &II, | |||
9980 | SourceLocation IILoc) { | |||
9981 | CXXScopeSpec SS; | |||
9982 | SS.Adopt(QualifierLoc); | |||
9983 | ||||
9984 | DeclContext *Ctx = computeDeclContext(SS); | |||
9985 | if (!Ctx) { | |||
9986 | // If the nested-name-specifier is dependent and couldn't be | |||
9987 | // resolved to a type, build a typename type. | |||
9988 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent())((QualifierLoc.getNestedNameSpecifier()->isDependent()) ? static_cast <void> (0) : __assert_fail ("QualifierLoc.getNestedNameSpecifier()->isDependent()" , "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaTemplate.cpp" , 9988, __PRETTY_FUNCTION__)); | |||
9989 | return Context.getDependentNameType(Keyword, | |||
9990 | QualifierLoc.getNestedNameSpecifier(), | |||
9991 | &II); | |||
9992 | } | |||
9993 | ||||
9994 | // If the nested-name-specifier refers to the current instantiation, | |||
9995 | // the "typename" keyword itself is superfluous. In C++03, the | |||
9996 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) | |||
9997 | // allows such extraneous "typename" keywords, and we retroactively | |||
9998 | // apply this DR to C++03 code with only a warning. In any case we continue. | |||
9999 | ||||
10000 | if (RequireCompleteDeclContext(SS, Ctx)) | |||
10001 | return QualType(); | |||
10002 | ||||
10003 | DeclarationName Name(&II); | |||
10004 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); | |||
10005 | LookupQualifiedName(Result, Ctx, SS); | |||
10006 | unsigned DiagID = 0; | |||
10007 | Decl *Referenced = nullptr; | |||
10008 | switch (Result.getResultKind()) { | |||
10009 | case LookupResult::NotFound: { | |||
10010 | // If we're looking up 'type' within a template named 'enable_if', produce | |||
10011 | // a more specific diagnostic. | |||
10012 | SourceRange CondRange; | |||
10013 | Expr *Cond = nullptr; | |||
10014 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { | |||
10015 | // If we have a condition, narrow it down to the specific failed | |||
10016 | // condition. | |||
10017 | if (Cond) { | |||
10018 | Expr *FailedCond; | |||
10019 | std::string FailedDescription; | |||
10020 | std::tie(FailedCond, FailedDescription) = | |||
10021 | findFailedBooleanCondition(Cond); | |||
10022 | ||||
10023 | Diag(FailedCond->getExprLoc(), | |||
10024 | diag::err_typename_nested_not_found_requirement) | |||
10025 | << FailedDescription | |||
10026 | << FailedCond->getSourceRange(); | |||
10027 | return QualType(); | |||
10028 | } | |||
10029 | ||||
10030 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) | |||
10031 | << Ctx << CondRange; | |||
10032 | return QualType(); | |||
10033 | } | |||
10034 | ||||
10035 | DiagID = diag::err_typename_nested_not_found; | |||
10036 | break; | |||
10037 | } | |||
10038 | ||||
10039 | case LookupResult::FoundUnresolvedValue: { | |||
10040 | // We found a using declaration that is a value. Most likely, the using | |||
10041 | // declaration itself is meant to have the 'typename' keyword. | |||
10042 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), | |||
10043 | IILoc); | |||
10044 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) | |||
10045 | << Name << Ctx << FullRange; | |||
10046 | if (UnresolvedUsingValueDecl *Using | |||
10047 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ | |||
10048 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); | |||
10049 | Diag(Loc, diag::note_using_value_decl_missing_typename) | |||
10050 | << FixItHint::CreateInsertion(Loc, "typename "); | |||
10051 | } | |||
10052 | } | |||
10053 | // Fall through to create a dependent typename type, from which we can recover | |||
10054 | // better. | |||
10055 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
10056 | ||||
10057 | case LookupResult::NotFoundInCurrentInstantiation: | |||
10058 | // Okay, it's a member of an unknown instantiation. | |||
10059 | return Context.getDependentNameType(Keyword, | |||
10060 | QualifierLoc.getNestedNameSpecifier(), | |||
10061 | &II); | |||
10062 | ||||
10063 | case LookupResult::Found: | |||
10064 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { | |||
10065 | // C++ [class.qual]p2: | |||
10066 | // In a lookup in which function names are not ignored and the | |||
10067 | // nested-name-specifier nominates a class C, if the name specified | |||
10068 | // after the nested-name-specifier, when looked up in C, is the | |||
10069 | // injected-class-name of C [...] then the name is instead considered | |||
10070 | // to name the constructor of class C. | |||
10071 | // | |||
10072 | // Unlike in an elaborated-type-specifier, function names are not ignored | |||
10073 | // in typename-specifier lookup. However, they are ignored in all the | |||
10074 | // contexts where we form a typename type with no keyword (that is, in | |||
10075 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). | |||
10076 | // | |||
10077 | // FIXME: That's not strictly true: mem-initializer-id lookup does not | |||
10078 | // ignore functions, but that appears to be an oversight. | |||
10079 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); | |||
10080 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); | |||
10081 | if (Keyword == ETK_Typename && LookupRD && FoundRD && | |||
10082 | FoundRD->isInjectedClassName() && | |||
10083 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) | |||
10084 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) | |||
10085 | << &II << 1 << 0 /*'typename' keyword used*/; | |||
10086 | ||||
10087 | // We found a type. Build an ElaboratedType, since the | |||
10088 | // typename-specifier was just sugar. | |||
10089 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); | |||
10090 | return Context.getElaboratedType(Keyword, | |||
10091 | QualifierLoc.getNestedNameSpecifier(), | |||
10092 | Context.getTypeDeclType(Type)); | |||
10093 | } | |||
10094 | ||||
10095 | // C++ [dcl.type.simple]p2: | |||
10096 | // A type-specifier of the form | |||
10097 | // typename[opt] nested-name-specifier[opt] template-name | |||
10098 | // is a placeholder for a deduced class type [...]. | |||
10099 | if (getLangOpts().CPlusPlus17) { | |||
10100 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { | |||
10101 | return Context.getElaboratedType( | |||
10102 | Keyword, QualifierLoc.getNestedNameSpecifier(), | |||
10103 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), | |||
10104 | QualType(), false)); | |||
10105 | } | |||
10106 | } | |||
10107 | ||||
10108 | DiagID = diag::err_typename_nested_not_type; | |||
10109 | Referenced = Result.getFoundDecl(); | |||
10110 | break; | |||
10111 | ||||
10112 | case LookupResult::FoundOverloaded: | |||
10113 | DiagID = diag::err_typename_nested_not_type; | |||
10114 | Referenced = *Result.begin(); | |||
10115 | break; | |||
10116 | ||||
10117 | case LookupResult::Ambiguous: | |||
10118 | return QualType(); | |||
10119 | } | |||
10120 | ||||
10121 | // If we get here, it's because name lookup did not find a | |||
10122 | // type. Emit an appropriate diagnostic and return an error. | |||
10123 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), | |||
10124 | IILoc); | |||
10125 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; | |||
10126 | if (Referenced) | |||
10127 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) | |||
10128 | << Name; | |||
10129 | return QualType(); | |||
10130 | } | |||
10131 | ||||
10132 | namespace { | |||
10133 | // See Sema::RebuildTypeInCurrentInstantiation | |||
10134 | class CurrentInstantiationRebuilder | |||
10135 | : public TreeTransform<CurrentInstantiationRebuilder> { | |||
10136 | SourceLocation Loc; | |||
10137 | DeclarationName Entity; | |||
10138 | ||||
10139 | public: | |||
10140 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; | |||
10141 | ||||
10142 | CurrentInstantiationRebuilder(Sema &SemaRef, | |||
10143 | SourceLocation Loc, | |||
10144 | DeclarationName Entity) | |||
10145 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), | |||
10146 | Loc(Loc), Entity(Entity) { } | |||
10147 | ||||
10148 | /// Determine whether the given type \p T has already been | |||
10149 | /// transformed. | |||
10150 | /// | |||
10151 | /// For the purposes of type reconstruction, a type has already been | |||
10152 | /// transformed if it is NULL or if it is not dependent. | |||
10153 | bool AlreadyTransformed(QualType T) { | |||
10154 | return T.isNull() || !T->isDependentType(); | |||
10155 | } | |||
10156 | ||||
10157 | /// Returns the location of the entity whose type is being | |||
10158 | /// rebuilt. | |||
10159 | SourceLocation getBaseLocation() { return Loc; } | |||
10160 | ||||
10161 | /// Returns the name of the entity whose type is being rebuilt. | |||
10162 | DeclarationName getBaseEntity() { return Entity; } | |||
10163 | ||||
10164 | /// Sets the "base" location and entity when that | |||
10165 | /// information is known based on another transformation. | |||
10166 | void setBase(SourceLocation Loc, DeclarationName Entity) { | |||
10167 | this->Loc = Loc; | |||
10168 | this->Entity = Entity; | |||
10169 | } | |||
10170 | ||||
10171 | ExprResult TransformLambdaExpr(LambdaExpr *E) { | |||
10172 | // Lambdas never need to be transformed. | |||
10173 | return E; | |||
10174 | } | |||
10175 | }; | |||
10176 | } // end anonymous namespace | |||
10177 | ||||
10178 | /// Rebuilds a type within the context of the current instantiation. | |||
10179 | /// | |||
10180 | /// The type \p T is part of the type of an out-of-line member definition of | |||
10181 | /// a class template (or class template partial specialization) that was parsed | |||
10182 | /// and constructed before we entered the scope of the class template (or | |||
10183 | /// partial specialization thereof). This routine will rebuild that type now | |||
10184 | /// that we have entered the declarator's scope, which may produce different | |||
10185 | /// canonical types, e.g., | |||
10186 | /// | |||
10187 | /// \code | |||
10188 | /// template<typename T> | |||
10189 | /// struct X { | |||
10190 | /// typedef T* pointer; | |||
10191 | /// pointer data(); | |||
10192 | /// }; | |||
10193 | /// | |||
10194 | /// template<typename T> | |||
10195 | /// typename X<T>::pointer X<T>::data() { ... } | |||
10196 | /// \endcode | |||
10197 | /// | |||
10198 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, | |||
10199 | /// since we do not know that we can look into X<T> when we parsed the type. | |||
10200 | /// This function will rebuild the type, performing the lookup of "pointer" | |||
10201 | /// in X<T> and returning an ElaboratedType whose canonical type is the same | |||
10202 | /// as the canonical type of T*, allowing the return types of the out-of-line | |||
10203 | /// definition and the declaration to match. | |||
10204 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, | |||
10205 | SourceLocation Loc, | |||
10206 | DeclarationName Name) { | |||
10207 | if (!T || !T->getType()->isDependentType()) | |||
10208 | return T; | |||
10209 | ||||
10210 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); | |||
10211 | return Rebuilder.TransformType(T); | |||
10212 | } | |||
10213 | ||||
10214 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { | |||
10215 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), | |||
10216 | DeclarationName()); | |||
10217 | return Rebuilder.TransformExpr(E); | |||
10218 | } | |||
10219 | ||||
10220 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { | |||
10221 | if (SS.isInvalid()) | |||
10222 | return true; | |||
10223 | ||||
10224 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); | |||
10225 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), | |||
10226 | DeclarationName()); | |||
10227 | NestedNameSpecifierLoc Rebuilt | |||
10228 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); | |||
10229 | if (!Rebuilt) | |||
10230 | return true; | |||
10231 | ||||
10232 | SS.Adopt(Rebuilt); | |||
10233 | return false; | |||
10234 | } | |||
10235 | ||||
10236 | /// Rebuild the template parameters now that we know we're in a current | |||
10237 | /// instantiation. | |||
10238 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( | |||
10239 | TemplateParameterList *Params) { | |||
10240 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | |||
10241 | Decl *Param = Params->getParam(I); | |||
10242 | ||||
10243 | // There is nothing to rebuild in a type parameter. | |||
10244 | if (isa<TemplateTypeParmDecl>(Param)) | |||
10245 | continue; | |||
10246 | ||||
10247 | // Rebuild the template parameter list of a template template parameter. | |||
10248 | if (TemplateTemplateParmDecl *TTP | |||
10249 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { | |||
10250 | if (RebuildTemplateParamsInCurrentInstantiation( | |||
10251 | TTP->getTemplateParameters())) | |||
10252 | return true; | |||
10253 | ||||
10254 | continue; | |||
10255 | } | |||
10256 | ||||
10257 | // Rebuild the type of a non-type template parameter. | |||
10258 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); | |||
10259 | TypeSourceInfo *NewTSI | |||
10260 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), | |||
10261 | NTTP->getLocation(), | |||
10262 | NTTP->getDeclName()); | |||
10263 | if (!NewTSI) | |||
10264 | return true; | |||
10265 | ||||
10266 | if (NewTSI->getType()->isUndeducedType()) { | |||
10267 | // C++17 [temp.dep.expr]p3: | |||
10268 | // An id-expression is type-dependent if it contains | |||
10269 | // - an identifier associated by name lookup with a non-type | |||
10270 | // template-parameter declared with a type that contains a | |||
10271 | // placeholder type (7.1.7.4), | |||
10272 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); | |||
10273 | } | |||
10274 | ||||
10275 | if (NewTSI != NTTP->getTypeSourceInfo()) { | |||
10276 | NTTP->setTypeSourceInfo(NewTSI); | |||
10277 | NTTP->setType(NewTSI->getType()); | |||
10278 | } | |||
10279 | } | |||
10280 | ||||
10281 | return false; | |||
10282 | } | |||
10283 | ||||
10284 | /// Produces a formatted string that describes the binding of | |||
10285 | /// template parameters to template arguments. | |||
10286 | std::string | |||
10287 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, | |||
10288 | const TemplateArgumentList &Args) { | |||
10289 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); | |||
10290 | } | |||
10291 | ||||
10292 | std::string | |||
10293 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, | |||
10294 | const TemplateArgument *Args, | |||
10295 | unsigned NumArgs) { | |||
10296 | SmallString<128> Str; | |||
10297 | llvm::raw_svector_ostream Out(Str); | |||
10298 | ||||
10299 | if (!Params || Params->size() == 0 || NumArgs == 0) | |||
10300 | return std::string(); | |||
10301 | ||||
10302 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | |||
10303 | if (I >= NumArgs) | |||
10304 | break; | |||
10305 | ||||
10306 | if (I == 0) | |||
10307 | Out << "[with "; | |||
10308 | else | |||
10309 | Out << ", "; | |||
10310 | ||||
10311 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { | |||
10312 | Out << Id->getName(); | |||
10313 | } else { | |||
10314 | Out << '$' << I; | |||
10315 | } | |||
10316 | ||||
10317 | Out << " = "; | |||
10318 | Args[I].print(getPrintingPolicy(), Out); | |||
10319 | } | |||
10320 | ||||
10321 | Out << ']'; | |||
10322 | return Out.str(); | |||
10323 | } | |||
10324 | ||||
10325 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, | |||
10326 | CachedTokens &Toks) { | |||
10327 | if (!FD) | |||
10328 | return; | |||
10329 | ||||
10330 | auto LPT = std::make_unique<LateParsedTemplate>(); | |||
10331 | ||||
10332 | // Take tokens to avoid allocations | |||
10333 | LPT->Toks.swap(Toks); | |||
10334 | LPT->D = FnD; | |||
10335 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); | |||
10336 | ||||
10337 | FD->setLateTemplateParsed(true); | |||
10338 | } | |||
10339 | ||||
10340 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { | |||
10341 | if (!FD) | |||
10342 | return; | |||
10343 | FD->setLateTemplateParsed(false); | |||
10344 | } | |||
10345 | ||||
10346 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { | |||
10347 | DeclContext *DC = CurContext; | |||
10348 | ||||
10349 | while (DC) { | |||
10350 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { | |||
10351 | const FunctionDecl *FD = RD->isLocalClass(); | |||
10352 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); | |||
10353 | } else if (DC->isTranslationUnit() || DC->isNamespace()) | |||
10354 | return false; | |||
10355 | ||||
10356 | DC = DC->getParent(); | |||
10357 | } | |||
10358 | return false; | |||
10359 | } | |||
10360 | ||||
10361 | namespace { | |||
10362 | /// Walk the path from which a declaration was instantiated, and check | |||
10363 | /// that every explicit specialization along that path is visible. This enforces | |||
10364 | /// C++ [temp.expl.spec]/6: | |||
10365 | /// | |||
10366 | /// If a template, a member template or a member of a class template is | |||
10367 | /// explicitly specialized then that specialization shall be declared before | |||
10368 | /// the first use of that specialization that would cause an implicit | |||
10369 | /// instantiation to take place, in every translation unit in which such a | |||
10370 | /// use occurs; no diagnostic is required. | |||
10371 | /// | |||
10372 | /// and also C++ [temp.class.spec]/1: | |||
10373 | /// | |||
10374 | /// A partial specialization shall be declared before the first use of a | |||
10375 | /// class template specialization that would make use of the partial | |||
10376 | /// specialization as the result of an implicit or explicit instantiation | |||
10377 | /// in every translation unit in which such a use occurs; no diagnostic is | |||
10378 | /// required. | |||
10379 | class ExplicitSpecializationVisibilityChecker { | |||
10380 | Sema &S; | |||
10381 | SourceLocation Loc; | |||
10382 | llvm::SmallVector<Module *, 8> Modules; | |||
10383 | ||||
10384 | public: | |||
10385 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) | |||
10386 | : S(S), Loc(Loc) {} | |||
10387 | ||||
10388 | void check(NamedDecl *ND) { | |||
10389 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) | |||
10390 | return checkImpl(FD); | |||
10391 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) | |||
10392 | return checkImpl(RD); | |||
10393 | if (auto *VD = dyn_cast<VarDecl>(ND)) | |||
10394 | return checkImpl(VD); | |||
10395 | if (auto *ED = dyn_cast<EnumDecl>(ND)) | |||
10396 | return checkImpl(ED); | |||
10397 | } | |||
10398 | ||||
10399 | private: | |||
10400 | void diagnose(NamedDecl *D, bool IsPartialSpec) { | |||
10401 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization | |||
10402 | : Sema::MissingImportKind::ExplicitSpecialization; | |||
10403 | const bool Recover = true; | |||
10404 | ||||
10405 | // If we got a custom set of modules (because only a subset of the | |||
10406 | // declarations are interesting), use them, otherwise let | |||
10407 | // diagnoseMissingImport intelligently pick some. | |||
10408 | if (Modules.empty()) | |||
10409 | S.diagnoseMissingImport(Loc, D, Kind, Recover); | |||
10410 | else | |||
10411 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); | |||
10412 | } | |||
10413 | ||||
10414 | // Check a specific declaration. There are three problematic cases: | |||
10415 | // | |||
10416 | // 1) The declaration is an explicit specialization of a template | |||
10417 | // specialization. | |||
10418 | // 2) The declaration is an explicit specialization of a member of an | |||
10419 | // templated class. | |||
10420 | // 3) The declaration is an instantiation of a template, and that template | |||
10421 | // is an explicit specialization of a member of a templated class. | |||
10422 | // | |||
10423 | // We don't need to go any deeper than that, as the instantiation of the | |||
10424 | // surrounding class / etc is not triggered by whatever triggered this | |||
10425 | // instantiation, and thus should be checked elsewhere. | |||
10426 | template<typename SpecDecl> | |||
10427 | void checkImpl(SpecDecl *Spec) { | |||
10428 | bool IsHiddenExplicitSpecialization = false; | |||
10429 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { | |||
10430 | IsHiddenExplicitSpecialization = | |||
10431 | Spec->getMemberSpecializationInfo() | |||
10432 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) | |||
10433 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); | |||
10434 | } else { | |||
10435 | checkInstantiated(Spec); | |||
10436 | } | |||
10437 | ||||
10438 | if (IsHiddenExplicitSpecialization) | |||
10439 | diagnose(Spec->getMostRecentDecl(), false); | |||
10440 | } | |||
10441 | ||||
10442 | void checkInstantiated(FunctionDecl *FD) { | |||
10443 | if (auto *TD = FD->getPrimaryTemplate()) | |||
10444 | checkTemplate(TD); | |||
10445 | } | |||
10446 | ||||
10447 | void checkInstantiated(CXXRecordDecl *RD) { | |||
10448 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); | |||
10449 | if (!SD) | |||
10450 | return; | |||
10451 | ||||
10452 | auto From = SD->getSpecializedTemplateOrPartial(); | |||
10453 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) | |||
10454 | checkTemplate(TD); | |||
10455 | else if (auto *TD = | |||
10456 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { | |||
10457 | if (!S.hasVisibleDeclaration(TD)) | |||
10458 | diagnose(TD, true); | |||
10459 | checkTemplate(TD); | |||
10460 | } | |||
10461 | } | |||
10462 | ||||
10463 | void checkInstantiated(VarDecl *RD) { | |||
10464 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); | |||
10465 | if (!SD) | |||
10466 | return; | |||
10467 | ||||
10468 | auto From = SD->getSpecializedTemplateOrPartial(); | |||
10469 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) | |||
10470 | checkTemplate(TD); | |||
10471 | else if (auto *TD = | |||
10472 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { | |||
10473 | if (!S.hasVisibleDeclaration(TD)) | |||
10474 | diagnose(TD, true); | |||
10475 | checkTemplate(TD); | |||
10476 | } | |||
10477 | } | |||
10478 | ||||
10479 | void checkInstantiated(EnumDecl *FD) {} | |||
10480 | ||||
10481 | template<typename TemplDecl> | |||
10482 | void checkTemplate(TemplDecl *TD) { | |||
10483 | if (TD->isMemberSpecialization()) { | |||
10484 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | |||
10485 | diagnose(TD->getMostRecentDecl(), false); | |||
10486 | } | |||
10487 | } | |||
10488 | }; | |||
10489 | } // end anonymous namespace | |||
10490 | ||||
10491 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { | |||
10492 | if (!getLangOpts().Modules) | |||
10493 | return; | |||
10494 | ||||
10495 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); | |||
10496 | } | |||
10497 | ||||
10498 | /// Check whether a template partial specialization that we've discovered | |||
10499 | /// is hidden, and produce suitable diagnostics if so. | |||
10500 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, | |||
10501 | NamedDecl *Spec) { | |||
10502 | llvm::SmallVector<Module *, 8> Modules; | |||
10503 | if (!hasVisibleDeclaration(Spec, &Modules)) | |||
10504 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, | |||
10505 | MissingImportKind::PartialSpecialization, | |||
10506 | /*Recover*/true); | |||
10507 | } |