/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaTemplate.cpp
Line | Count | Source (jump to first uncovered line) |
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/Decl.h" |
15 | | #include "clang/AST/DeclFriend.h" |
16 | | #include "clang/AST/DeclTemplate.h" |
17 | | #include "clang/AST/Expr.h" |
18 | | #include "clang/AST/ExprCXX.h" |
19 | | #include "clang/AST/RecursiveASTVisitor.h" |
20 | | #include "clang/AST/TemplateName.h" |
21 | | #include "clang/AST/TypeVisitor.h" |
22 | | #include "clang/Basic/Builtins.h" |
23 | | #include "clang/Basic/LangOptions.h" |
24 | | #include "clang/Basic/PartialDiagnostic.h" |
25 | | #include "clang/Basic/Stack.h" |
26 | | #include "clang/Basic/TargetInfo.h" |
27 | | #include "clang/Sema/DeclSpec.h" |
28 | | #include "clang/Sema/Initialization.h" |
29 | | #include "clang/Sema/Lookup.h" |
30 | | #include "clang/Sema/Overload.h" |
31 | | #include "clang/Sema/ParsedTemplate.h" |
32 | | #include "clang/Sema/Scope.h" |
33 | | #include "clang/Sema/SemaInternal.h" |
34 | | #include "clang/Sema/Template.h" |
35 | | #include "clang/Sema/TemplateDeduction.h" |
36 | | #include "llvm/ADT/SmallBitVector.h" |
37 | | #include "llvm/ADT/SmallString.h" |
38 | | #include "llvm/ADT/StringExtras.h" |
39 | | |
40 | | #include <iterator> |
41 | | using namespace clang; |
42 | | using namespace sema; |
43 | | |
44 | | // Exported for use by Parser. |
45 | | SourceRange |
46 | | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
47 | 8 | unsigned N) { |
48 | 8 | if (!N) return SourceRange()0 ; |
49 | 8 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
50 | 8 | } |
51 | | |
52 | 470k | unsigned Sema::getTemplateDepth(Scope *S) const { |
53 | 470k | unsigned Depth = 0; |
54 | | |
55 | | // Each template parameter scope represents one level of template parameter |
56 | | // depth. |
57 | 1.13M | for (Scope *TempParamScope = S->getTemplateParamParent(); TempParamScope; |
58 | 660k | TempParamScope = TempParamScope->getParent()->getTemplateParamParent()) { |
59 | 660k | ++Depth; |
60 | 660k | } |
61 | | |
62 | | // Note that there are template parameters with the given depth. |
63 | 470k | auto ParamsAtDepth = [&](unsigned D) { Depth = std::max(Depth, D + 1); }0 ; |
64 | | |
65 | | // Look for parameters of an enclosing generic lambda. We don't create a |
66 | | // template parameter scope for these. |
67 | 470k | for (FunctionScopeInfo *FSI : getFunctionScopes()) { |
68 | 1 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(FSI)) { |
69 | 0 | if (!LSI->TemplateParams.empty()) { |
70 | 0 | ParamsAtDepth(LSI->AutoTemplateParameterDepth); |
71 | 0 | break; |
72 | 0 | } |
73 | 0 | if (LSI->GLTemplateParameterList) { |
74 | 0 | ParamsAtDepth(LSI->GLTemplateParameterList->getDepth()); |
75 | 0 | break; |
76 | 0 | } |
77 | 0 | } |
78 | 1 | } |
79 | | |
80 | | // Look for parameters of an enclosing terse function template. We don't |
81 | | // create a template parameter scope for these either. |
82 | 470k | for (const InventedTemplateParameterInfo &Info : |
83 | 470k | getInventedParameterInfos()) { |
84 | 0 | if (!Info.TemplateParams.empty()) { |
85 | 0 | ParamsAtDepth(Info.AutoTemplateParameterDepth); |
86 | 0 | break; |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | 470k | return Depth; |
91 | 470k | } |
92 | | |
93 | | /// \brief Determine whether the declaration found is acceptable as the name |
94 | | /// of a template and, if so, return that template declaration. Otherwise, |
95 | | /// returns null. |
96 | | /// |
97 | | /// Note that this may return an UnresolvedUsingValueDecl if AllowDependent |
98 | | /// is true. In all other cases it will return a TemplateDecl (or null). |
99 | | NamedDecl *Sema::getAsTemplateNameDecl(NamedDecl *D, |
100 | | bool AllowFunctionTemplates, |
101 | 9.14M | bool AllowDependent) { |
102 | 9.14M | D = D->getUnderlyingDecl(); |
103 | | |
104 | 9.14M | if (isa<TemplateDecl>(D)) { |
105 | 7.94M | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)2.12k ) |
106 | 1.52k | return nullptr; |
107 | | |
108 | 7.94M | return D; |
109 | 7.94M | } |
110 | | |
111 | 1.20M | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
112 | | // C++ [temp.local]p1: |
113 | | // Like normal (non-template) classes, class templates have an |
114 | | // injected-class-name (Clause 9). The injected-class-name |
115 | | // can be used with or without a template-argument-list. When |
116 | | // it is used without a template-argument-list, it is |
117 | | // equivalent to the injected-class-name followed by the |
118 | | // template-parameters of the class template enclosed in |
119 | | // <>. When it is used with a template-argument-list, it |
120 | | // refers to the specified class template specialization, |
121 | | // which could be the current specialization or another |
122 | | // specialization. |
123 | 246k | if (Record->isInjectedClassName()) { |
124 | 236k | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
125 | 236k | if (Record->getDescribedClassTemplate()) |
126 | 181k | return Record->getDescribedClassTemplate(); |
127 | | |
128 | 55.1k | if (ClassTemplateSpecializationDecl *Spec |
129 | 55.1k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
130 | 53.2k | return Spec->getSpecializedTemplate(); |
131 | 55.1k | } |
132 | | |
133 | 11.7k | return nullptr; |
134 | 246k | } |
135 | | |
136 | | // 'using Dependent::foo;' can resolve to a template name. |
137 | | // 'using typename Dependent::foo;' cannot (not even if 'foo' is an |
138 | | // injected-class-name). |
139 | 956k | if (AllowDependent && isa<UnresolvedUsingValueDecl>(D)956k ) |
140 | 35 | return D; |
141 | | |
142 | 956k | return nullptr; |
143 | 956k | } |
144 | | |
145 | | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
146 | | bool AllowFunctionTemplates, |
147 | 5.13M | bool AllowDependent) { |
148 | 5.13M | LookupResult::Filter filter = R.makeFilter(); |
149 | 10.7M | while (filter.hasNext()) { |
150 | 5.63M | NamedDecl *Orig = filter.next(); |
151 | 5.63M | if (!getAsTemplateNameDecl(Orig, AllowFunctionTemplates, AllowDependent)) |
152 | 969k | filter.erase(); |
153 | 5.63M | } |
154 | 5.13M | filter.done(); |
155 | 5.13M | } |
156 | | |
157 | | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
158 | | bool AllowFunctionTemplates, |
159 | | bool AllowDependent, |
160 | 141 | bool AllowNonTemplateFunctions) { |
161 | 283 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I142 ) { |
162 | 142 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) |
163 | 0 | return true; |
164 | 142 | if (AllowNonTemplateFunctions && |
165 | 142 | isa<FunctionDecl>((*I)->getUnderlyingDecl())3 ) |
166 | 0 | return true; |
167 | 142 | } |
168 | | |
169 | 141 | return false; |
170 | 141 | } |
171 | | |
172 | | TemplateNameKind Sema::isTemplateName(Scope *S, |
173 | | CXXScopeSpec &SS, |
174 | | bool hasTemplateKeyword, |
175 | | const UnqualifiedId &Name, |
176 | | ParsedType ObjectTypePtr, |
177 | | bool EnteringContext, |
178 | | TemplateTy &TemplateResult, |
179 | | bool &MemberOfUnknownSpecialization, |
180 | 4.68M | bool Disambiguation) { |
181 | 4.68M | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
182 | | |
183 | 0 | DeclarationName TName; |
184 | 4.68M | MemberOfUnknownSpecialization = false; |
185 | | |
186 | 4.68M | switch (Name.getKind()) { |
187 | 4.67M | case UnqualifiedIdKind::IK_Identifier: |
188 | 4.67M | TName = DeclarationName(Name.Identifier); |
189 | 4.67M | break; |
190 | | |
191 | 2.19k | case UnqualifiedIdKind::IK_OperatorFunctionId: |
192 | 2.19k | TName = Context.DeclarationNames.getCXXOperatorName( |
193 | 2.19k | Name.OperatorFunctionId.Operator); |
194 | 2.19k | break; |
195 | | |
196 | 4 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
197 | 4 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
198 | 4 | break; |
199 | | |
200 | 0 | default: |
201 | 0 | return TNK_Non_template; |
202 | 4.68M | } |
203 | | |
204 | 4.68M | QualType ObjectType = ObjectTypePtr.get(); |
205 | | |
206 | 4.68M | AssumedTemplateKind AssumedTemplate; |
207 | 4.68M | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
208 | 4.68M | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
209 | 4.68M | MemberOfUnknownSpecialization, SourceLocation(), |
210 | 4.68M | &AssumedTemplate, |
211 | 4.68M | /*AllowTypoCorrection=*/!Disambiguation)) |
212 | 232 | return TNK_Non_template; |
213 | | |
214 | 4.68M | if (AssumedTemplate != AssumedTemplateKind::None) { |
215 | 826 | TemplateResult = TemplateTy::make(Context.getAssumedTemplateName(TName)); |
216 | | // Let the parser know whether we found nothing or found functions; if we |
217 | | // found nothing, we want to more carefully check whether this is actually |
218 | | // a function template name versus some other kind of undeclared identifier. |
219 | 826 | return AssumedTemplate == AssumedTemplateKind::FoundNothing |
220 | 826 | ? TNK_Undeclared_template816 |
221 | 826 | : TNK_Function_template10 ; |
222 | 826 | } |
223 | | |
224 | 4.68M | if (R.empty()) |
225 | 1.02M | return TNK_Non_template; |
226 | | |
227 | 3.65M | NamedDecl *D = nullptr; |
228 | 3.65M | UsingShadowDecl *FoundUsingShadow = dyn_cast<UsingShadowDecl>(*R.begin()); |
229 | 3.65M | if (R.isAmbiguous()) { |
230 | | // If we got an ambiguity involving a non-function template, treat this |
231 | | // as a template name, and pick an arbitrary template for error recovery. |
232 | 12 | bool AnyFunctionTemplates = false; |
233 | 21 | for (NamedDecl *FoundD : R) { |
234 | 21 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { |
235 | 13 | if (isa<FunctionTemplateDecl>(FoundTemplate)) |
236 | 5 | AnyFunctionTemplates = true; |
237 | 8 | else { |
238 | 8 | D = FoundTemplate; |
239 | 8 | FoundUsingShadow = dyn_cast<UsingShadowDecl>(FoundD); |
240 | 8 | break; |
241 | 8 | } |
242 | 13 | } |
243 | 21 | } |
244 | | |
245 | | // If we didn't find any templates at all, this isn't a template name. |
246 | | // Leave the ambiguity for a later lookup to diagnose. |
247 | 12 | if (!D && !AnyFunctionTemplates4 ) { |
248 | 2 | R.suppressDiagnostics(); |
249 | 2 | return TNK_Non_template; |
250 | 2 | } |
251 | | |
252 | | // If the only templates were function templates, filter out the rest. |
253 | | // We'll diagnose the ambiguity later. |
254 | 10 | if (!D) |
255 | 2 | FilterAcceptableTemplateNames(R); |
256 | 10 | } |
257 | | |
258 | | // At this point, we have either picked a single template name declaration D |
259 | | // or we have a non-empty set of results R containing either one template name |
260 | | // declaration or a set of function templates. |
261 | | |
262 | 3.65M | TemplateName Template; |
263 | 3.65M | TemplateNameKind TemplateKind; |
264 | | |
265 | 3.65M | unsigned ResultCount = R.end() - R.begin(); |
266 | 3.65M | if (!D && ResultCount > 13.65M ) { |
267 | | // We assume that we'll preserve the qualifier from a function |
268 | | // template name in other ways. |
269 | 140k | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
270 | 140k | TemplateKind = TNK_Function_template; |
271 | | |
272 | | // We'll do this lookup again later. |
273 | 140k | R.suppressDiagnostics(); |
274 | 3.51M | } else { |
275 | 3.51M | if (!D) { |
276 | 3.51M | D = getAsTemplateNameDecl(*R.begin()); |
277 | 3.51M | assert(D && "unambiguous result is not a template name"); |
278 | 3.51M | } |
279 | | |
280 | 3.51M | if (isa<UnresolvedUsingValueDecl>(D)) { |
281 | | // We don't yet know whether this is a template-name or not. |
282 | 16 | MemberOfUnknownSpecialization = true; |
283 | 16 | return TNK_Non_template; |
284 | 16 | } |
285 | | |
286 | 3.51M | TemplateDecl *TD = cast<TemplateDecl>(D); |
287 | 3.51M | Template = |
288 | 3.51M | FoundUsingShadow ? TemplateName(FoundUsingShadow)204 : TemplateName(TD)3.51M ; |
289 | 3.51M | assert(!FoundUsingShadow || FoundUsingShadow->getTargetDecl() == TD); |
290 | 3.51M | if (SS.isSet() && !SS.isInvalid()296k ) { |
291 | 296k | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
292 | 296k | Template = Context.getQualifiedTemplateName(Qualifier, hasTemplateKeyword, |
293 | 296k | Template); |
294 | 296k | } |
295 | | |
296 | 3.51M | if (isa<FunctionTemplateDecl>(TD)) { |
297 | 152k | TemplateKind = TNK_Function_template; |
298 | | |
299 | | // We'll do this lookup again later. |
300 | 152k | R.suppressDiagnostics(); |
301 | 3.36M | } else { |
302 | 3.36M | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
303 | 3.36M | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
304 | 3.36M | isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD)); |
305 | 0 | TemplateKind = |
306 | 3.36M | isa<VarTemplateDecl>(TD) ? TNK_Var_template6.66k : |
307 | 3.36M | isa<ConceptDecl>(TD)3.35M ? TNK_Concept_template7.01k : |
308 | 3.35M | TNK_Type_template3.34M ; |
309 | 3.36M | } |
310 | 3.51M | } |
311 | | |
312 | 3.65M | TemplateResult = TemplateTy::make(Template); |
313 | 3.65M | return TemplateKind; |
314 | 3.65M | } |
315 | | |
316 | | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
317 | | SourceLocation NameLoc, |
318 | 141k | ParsedTemplateTy *Template) { |
319 | 141k | CXXScopeSpec SS; |
320 | 141k | bool MemberOfUnknownSpecialization = false; |
321 | | |
322 | | // We could use redeclaration lookup here, but we don't need to: the |
323 | | // syntactic form of a deduction guide is enough to identify it even |
324 | | // if we can't look up the template name at all. |
325 | 141k | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
326 | 141k | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), |
327 | 141k | /*EnteringContext*/ false, |
328 | 141k | MemberOfUnknownSpecialization)) |
329 | 0 | return false; |
330 | | |
331 | 141k | if (R.empty()) return false130k ; |
332 | 11.5k | if (R.isAmbiguous()) { |
333 | | // FIXME: Diagnose an ambiguity if we find at least one template. |
334 | 1 | R.suppressDiagnostics(); |
335 | 1 | return false; |
336 | 1 | } |
337 | | |
338 | | // We only treat template-names that name type templates as valid deduction |
339 | | // guide names. |
340 | 11.5k | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
341 | 11.5k | if (!TD || !getAsTypeTemplateDecl(TD)2.01k ) |
342 | 9.50k | return false; |
343 | | |
344 | 2.01k | if (Template) |
345 | 907 | *Template = TemplateTy::make(TemplateName(TD)); |
346 | 2.01k | return true; |
347 | 11.5k | } |
348 | | |
349 | | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
350 | | SourceLocation IILoc, |
351 | | Scope *S, |
352 | | const CXXScopeSpec *SS, |
353 | | TemplateTy &SuggestedTemplate, |
354 | 4 | TemplateNameKind &SuggestedKind) { |
355 | | // We can't recover unless there's a dependent scope specifier preceding the |
356 | | // template name. |
357 | | // FIXME: Typo correction? |
358 | 4 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS)0 || |
359 | 4 | computeDeclContext(*SS)0 ) |
360 | 4 | return false; |
361 | | |
362 | | // The code is missing a 'template' keyword prior to the dependent template |
363 | | // name. |
364 | 0 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
365 | 0 | Diag(IILoc, diag::err_template_kw_missing) |
366 | 0 | << Qualifier << II.getName() |
367 | 0 | << FixItHint::CreateInsertion(IILoc, "template "); |
368 | 0 | SuggestedTemplate |
369 | 0 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
370 | 0 | SuggestedKind = TNK_Dependent_template_name; |
371 | 0 | return true; |
372 | 4 | } |
373 | | |
374 | | bool Sema::LookupTemplateName(LookupResult &Found, |
375 | | Scope *S, CXXScopeSpec &SS, |
376 | | QualType ObjectType, |
377 | | bool EnteringContext, |
378 | | bool &MemberOfUnknownSpecialization, |
379 | | RequiredTemplateKind RequiredTemplate, |
380 | | AssumedTemplateKind *ATK, |
381 | 5.13M | bool AllowTypoCorrection) { |
382 | 5.13M | if (ATK) |
383 | 4.95M | *ATK = AssumedTemplateKind::None; |
384 | | |
385 | 5.13M | if (SS.isInvalid()) |
386 | 229 | return true; |
387 | | |
388 | 5.13M | Found.setTemplateNameLookup(true); |
389 | | |
390 | | // Determine where to perform name lookup |
391 | 5.13M | MemberOfUnknownSpecialization = false; |
392 | 5.13M | DeclContext *LookupCtx = nullptr; |
393 | 5.13M | bool IsDependent = false; |
394 | 5.13M | if (!ObjectType.isNull()) { |
395 | | // This nested-name-specifier occurs in a member access expression, e.g., |
396 | | // x->B::f, and we are looking into the type of the object. |
397 | 29.5k | assert(SS.isEmpty() && "ObjectType and scope specifier cannot coexist"); |
398 | 0 | LookupCtx = computeDeclContext(ObjectType); |
399 | 29.5k | IsDependent = !LookupCtx && ObjectType->isDependentType()5.37k ; |
400 | 29.5k | assert((IsDependent || !ObjectType->isIncompleteType() || |
401 | 29.5k | ObjectType->castAs<TagType>()->isBeingDefined()) && |
402 | 29.5k | "Caller should have completed object type"); |
403 | | |
404 | | // Template names cannot appear inside an Objective-C class or object type |
405 | | // or a vector type. |
406 | | // |
407 | | // FIXME: This is wrong. For example: |
408 | | // |
409 | | // template<typename T> using Vec = T __attribute__((ext_vector_type(4))); |
410 | | // Vec<int> vi; |
411 | | // vi.Vec<int>::~Vec<int>(); |
412 | | // |
413 | | // ... should be accepted but we will not treat 'Vec' as a template name |
414 | | // here. The right thing to do would be to check if the name is a valid |
415 | | // vector component name, and look up a template name if not. And similarly |
416 | | // for lookups into Objective-C class and object types, where the same |
417 | | // problem can arise. |
418 | 29.5k | if (ObjectType->isObjCObjectOrInterfaceType() || |
419 | 29.5k | ObjectType->isVectorType()29.5k ) { |
420 | 26 | Found.clear(); |
421 | 26 | return false; |
422 | 26 | } |
423 | 5.10M | } else if (SS.isNotEmpty()) { |
424 | | // This nested-name-specifier occurs after another nested-name-specifier, |
425 | | // so long into the context associated with the prior nested-name-specifier. |
426 | 812k | LookupCtx = computeDeclContext(SS, EnteringContext); |
427 | 812k | IsDependent = !LookupCtx && isDependentScopeSpecifier(SS)209k ; |
428 | | |
429 | | // The declaration context must be complete. |
430 | 812k | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)602k ) |
431 | 6 | return true; |
432 | 812k | } |
433 | | |
434 | 5.13M | bool ObjectTypeSearchedInScope = false; |
435 | 5.13M | bool AllowFunctionTemplatesInLookup = true; |
436 | 5.13M | if (LookupCtx) { |
437 | | // Perform "qualified" name lookup into the declaration context we |
438 | | // computed, which is either the type of the base of a member access |
439 | | // expression or the declaration context associated with a prior |
440 | | // nested-name-specifier. |
441 | 626k | LookupQualifiedName(Found, LookupCtx); |
442 | | |
443 | | // FIXME: The C++ standard does not clearly specify what happens in the |
444 | | // case where the object type is dependent, and implementations vary. In |
445 | | // Clang, we treat a name after a . or -> as a template-name if lookup |
446 | | // finds a non-dependent member or member of the current instantiation that |
447 | | // is a type template, or finds no such members and lookup in the context |
448 | | // of the postfix-expression finds a type template. In the latter case, the |
449 | | // name is nonetheless dependent, and we may resolve it to a member of an |
450 | | // unknown specialization when we come to instantiate the template. |
451 | 626k | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
452 | 626k | } |
453 | | |
454 | 5.13M | if (SS.isEmpty() && (4.32M ObjectType.isNull()4.32M || Found.empty()29.5k )) { |
455 | | // C++ [basic.lookup.classref]p1: |
456 | | // In a class member access expression (5.2.5), if the . or -> token is |
457 | | // immediately followed by an identifier followed by a <, the |
458 | | // identifier must be looked up to determine whether the < is the |
459 | | // beginning of a template argument list (14.2) or a less-than operator. |
460 | | // The identifier is first looked up in the class of the object |
461 | | // expression. If the identifier is not found, it is then looked up in |
462 | | // the context of the entire postfix-expression and shall name a class |
463 | | // template. |
464 | 4.29M | if (S) |
465 | 4.29M | LookupName(Found, S); |
466 | | |
467 | 4.29M | if (!ObjectType.isNull()) { |
468 | | // FIXME: We should filter out all non-type templates here, particularly |
469 | | // variable templates and concepts. But the exclusion of alias templates |
470 | | // and template template parameters is a wording defect. |
471 | 5.40k | AllowFunctionTemplatesInLookup = false; |
472 | 5.40k | ObjectTypeSearchedInScope = true; |
473 | 5.40k | } |
474 | | |
475 | 4.29M | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
476 | 4.29M | } |
477 | | |
478 | 5.13M | if (Found.isAmbiguous()) |
479 | 17 | return false; |
480 | | |
481 | 5.13M | if (ATK && SS.isEmpty()4.95M && ObjectType.isNull()4.17M && |
482 | 5.13M | !RequiredTemplate.hasTemplateKeyword()4.15M ) { |
483 | | // C++2a [temp.names]p2: |
484 | | // A name is also considered to refer to a template if it is an |
485 | | // unqualified-id followed by a < and name lookup finds either one or more |
486 | | // functions or finds nothing. |
487 | | // |
488 | | // To keep our behavior consistent, we apply the "finds nothing" part in |
489 | | // all language modes, and diagnose the empty lookup in ActOnCallExpr if we |
490 | | // successfully form a call to an undeclared template-id. |
491 | 4.15M | bool AllFunctions = |
492 | 4.15M | getLangOpts().CPlusPlus20 && llvm::all_of(Found, [](NamedDecl *ND) 87.6k { |
493 | 87.6k | return isa<FunctionDecl>(ND->getUnderlyingDecl()); |
494 | 87.6k | }); |
495 | 4.15M | if (AllFunctions || (4.15M Found.empty()4.15M && !IsDependent753 )) { |
496 | | // If lookup found any functions, or if this is a name that can only be |
497 | | // used for a function, then strongly assume this is a function |
498 | | // template-id. |
499 | 877 | *ATK = (Found.empty() && Found.getLookupName().isIdentifier()864 ) |
500 | 877 | ? AssumedTemplateKind::FoundNothing864 |
501 | 877 | : AssumedTemplateKind::FoundFunctions13 ; |
502 | 877 | Found.clear(); |
503 | 877 | return false; |
504 | 877 | } |
505 | 4.15M | } |
506 | | |
507 | 5.13M | if (Found.empty() && !IsDependent213k && AllowTypoCorrection263 ) { |
508 | | // If we did not find any names, and this is not a disambiguation, attempt |
509 | | // to correct any typos. |
510 | 227 | DeclarationName Name = Found.getLookupName(); |
511 | 227 | Found.clear(); |
512 | | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
513 | 227 | DefaultFilterCCC FilterCCC{}; |
514 | 227 | FilterCCC.WantTypeSpecifiers = false; |
515 | 227 | FilterCCC.WantExpressionKeywords = false; |
516 | 227 | FilterCCC.WantRemainingKeywords = false; |
517 | 227 | FilterCCC.WantCXXNamedCasts = true; |
518 | 227 | if (TypoCorrection Corrected = |
519 | 227 | CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, |
520 | 227 | &SS, FilterCCC, CTK_ErrorRecovery, LookupCtx)) { |
521 | 9 | if (auto *ND = Corrected.getFoundDecl()) |
522 | 9 | Found.addDecl(ND); |
523 | 9 | FilterAcceptableTemplateNames(Found); |
524 | 9 | if (Found.isAmbiguous()) { |
525 | 0 | Found.clear(); |
526 | 9 | } else if (!Found.empty()) { |
527 | 8 | Found.setLookupName(Corrected.getCorrection()); |
528 | 8 | if (LookupCtx) { |
529 | 8 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
530 | 8 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
531 | 8 | Name.getAsString() == CorrectedStr4 ; |
532 | 8 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
533 | 8 | << Name << LookupCtx << DroppedSpecifier |
534 | 8 | << SS.getRange()); |
535 | 8 | } else { |
536 | 0 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
537 | 0 | } |
538 | 8 | } |
539 | 9 | } |
540 | 227 | } |
541 | | |
542 | 5.13M | NamedDecl *ExampleLookupResult = |
543 | 5.13M | Found.empty() ? nullptr213k : Found.getRepresentativeDecl()4.92M ; |
544 | 5.13M | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
545 | 5.13M | if (Found.empty()) { |
546 | 1.15M | if (IsDependent) { |
547 | 214k | MemberOfUnknownSpecialization = true; |
548 | 214k | return false; |
549 | 214k | } |
550 | | |
551 | | // If a 'template' keyword was used, a lookup that finds only non-template |
552 | | // names is an error. |
553 | 940k | if (ExampleLookupResult && RequiredTemplate940k ) { |
554 | 40 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
555 | 40 | << Found.getLookupName() << SS.getRange() |
556 | 40 | << RequiredTemplate.hasTemplateKeyword() |
557 | 40 | << RequiredTemplate.getTemplateKeywordLoc(); |
558 | 40 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
559 | 40 | diag::note_template_kw_refers_to_non_template) |
560 | 40 | << Found.getLookupName(); |
561 | 40 | return true; |
562 | 40 | } |
563 | | |
564 | 940k | return false; |
565 | 940k | } |
566 | | |
567 | 3.97M | if (S && !ObjectType.isNull()3.90M && !ObjectTypeSearchedInScope8.93k && |
568 | 3.97M | !getLangOpts().CPlusPlus118.33k ) { |
569 | | // C++03 [basic.lookup.classref]p1: |
570 | | // [...] If the lookup in the class of the object expression finds a |
571 | | // template, the name is also looked up in the context of the entire |
572 | | // postfix-expression and [...] |
573 | | // |
574 | | // Note: C++11 does not perform this second lookup. |
575 | 87 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
576 | 87 | LookupOrdinaryName); |
577 | 87 | FoundOuter.setTemplateNameLookup(true); |
578 | 87 | LookupName(FoundOuter, S); |
579 | | // FIXME: We silently accept an ambiguous lookup here, in violation of |
580 | | // [basic.lookup]/1. |
581 | 87 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
582 | | |
583 | 87 | NamedDecl *OuterTemplate; |
584 | 87 | if (FoundOuter.empty()) { |
585 | | // - if the name is not found, the name found in the class of the |
586 | | // object expression is used, otherwise |
587 | 77 | } else if (10 FoundOuter.isAmbiguous()10 || !FoundOuter.isSingleResult()10 || |
588 | 10 | !(OuterTemplate = |
589 | 8 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { |
590 | | // - if the name is found in the context of the entire |
591 | | // postfix-expression and does not name a class template, the name |
592 | | // found in the class of the object expression is used, otherwise |
593 | 2 | FoundOuter.clear(); |
594 | 8 | } else if (!Found.isSuppressingDiagnostics()) { |
595 | | // - if the name found is a class template, it must refer to the same |
596 | | // entity as the one found in the class of the object expression, |
597 | | // otherwise the program is ill-formed. |
598 | 8 | if (!Found.isSingleResult() || |
599 | 8 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != |
600 | 7 | OuterTemplate->getCanonicalDecl()) { |
601 | 2 | Diag(Found.getNameLoc(), |
602 | 2 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
603 | 2 | << Found.getLookupName() |
604 | 2 | << ObjectType; |
605 | 2 | Diag(Found.getRepresentativeDecl()->getLocation(), |
606 | 2 | diag::note_ambig_member_ref_object_type) |
607 | 2 | << ObjectType; |
608 | 2 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
609 | 2 | diag::note_ambig_member_ref_scope); |
610 | | |
611 | | // Recover by taking the template that we found in the object |
612 | | // expression's type. |
613 | 2 | } |
614 | 8 | } |
615 | 87 | } |
616 | | |
617 | 3.97M | return false; |
618 | 5.13M | } |
619 | | |
620 | | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
621 | | SourceLocation Less, |
622 | 33 | SourceLocation Greater) { |
623 | 33 | if (TemplateName.isInvalid()) |
624 | 0 | return; |
625 | | |
626 | 33 | DeclarationNameInfo NameInfo; |
627 | 33 | CXXScopeSpec SS; |
628 | 33 | LookupNameKind LookupKind; |
629 | | |
630 | 33 | DeclContext *LookupCtx = nullptr; |
631 | 33 | NamedDecl *Found = nullptr; |
632 | 33 | bool MissingTemplateKeyword = false; |
633 | | |
634 | | // Figure out what name we looked up. |
635 | 33 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
636 | 13 | NameInfo = DRE->getNameInfo(); |
637 | 13 | SS.Adopt(DRE->getQualifierLoc()); |
638 | 13 | LookupKind = LookupOrdinaryName; |
639 | 13 | Found = DRE->getFoundDecl(); |
640 | 20 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
641 | 2 | NameInfo = ME->getMemberNameInfo(); |
642 | 2 | SS.Adopt(ME->getQualifierLoc()); |
643 | 2 | LookupKind = LookupMemberName; |
644 | 2 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
645 | 2 | Found = ME->getMemberDecl(); |
646 | 18 | } else if (auto *DSDRE = |
647 | 18 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
648 | 3 | NameInfo = DSDRE->getNameInfo(); |
649 | 3 | SS.Adopt(DSDRE->getQualifierLoc()); |
650 | 3 | MissingTemplateKeyword = true; |
651 | 15 | } else if (auto *DSME = |
652 | 15 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
653 | 15 | NameInfo = DSME->getMemberNameInfo(); |
654 | 15 | SS.Adopt(DSME->getQualifierLoc()); |
655 | 15 | MissingTemplateKeyword = true; |
656 | 15 | } else { |
657 | 0 | llvm_unreachable("unexpected kind of potential template name"); |
658 | 0 | } |
659 | | |
660 | | // If this is a dependent-scope lookup, diagnose that the 'template' keyword |
661 | | // was missing. |
662 | 33 | if (MissingTemplateKeyword) { |
663 | 18 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
664 | 18 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
665 | 18 | return; |
666 | 18 | } |
667 | | |
668 | | // Try to correct the name by looking for templates and C++ named casts. |
669 | 15 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
670 | 15 | Sema &S; |
671 | 15 | TemplateCandidateFilter(Sema &S) : S(S) { |
672 | 15 | WantTypeSpecifiers = false; |
673 | 15 | WantExpressionKeywords = false; |
674 | 15 | WantRemainingKeywords = false; |
675 | 15 | WantCXXNamedCasts = true; |
676 | 15 | }; |
677 | 43 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
678 | 43 | if (auto *ND = Candidate.getCorrectionDecl()) |
679 | 43 | return S.getAsTemplateNameDecl(ND); |
680 | 0 | return Candidate.isKeyword(); |
681 | 43 | } |
682 | | |
683 | 15 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
684 | 15 | return std::make_unique<TemplateCandidateFilter>(*this); |
685 | 15 | } |
686 | 15 | }; |
687 | | |
688 | 15 | DeclarationName Name = NameInfo.getName(); |
689 | 15 | TemplateCandidateFilter CCC(*this); |
690 | 15 | if (TypoCorrection Corrected = CorrectTypo(NameInfo, LookupKind, S, &SS, CCC, |
691 | 15 | CTK_ErrorRecovery, LookupCtx)) { |
692 | 14 | auto *ND = Corrected.getFoundDecl(); |
693 | 14 | if (ND) |
694 | 14 | ND = getAsTemplateNameDecl(ND); |
695 | 14 | if (ND || Corrected.isKeyword()0 ) { |
696 | 14 | if (LookupCtx) { |
697 | 1 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
698 | 1 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
699 | 1 | Name.getAsString() == CorrectedStr0 ; |
700 | 1 | diagnoseTypo(Corrected, |
701 | 1 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
702 | 1 | << Name << LookupCtx << DroppedSpecifier |
703 | 1 | << SS.getRange(), false); |
704 | 13 | } else { |
705 | 13 | diagnoseTypo(Corrected, |
706 | 13 | PDiag(diag::err_non_template_in_template_id_suggest) |
707 | 13 | << Name, false); |
708 | 13 | } |
709 | 14 | if (Found) |
710 | 14 | Diag(Found->getLocation(), |
711 | 14 | diag::note_non_template_in_template_id_found); |
712 | 14 | return; |
713 | 14 | } |
714 | 14 | } |
715 | | |
716 | 1 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
717 | 1 | << Name << SourceRange(Less, Greater); |
718 | 1 | if (Found) |
719 | 1 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
720 | 1 | } |
721 | | |
722 | | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
723 | | /// was just parsed. This is only possible with an explicit scope |
724 | | /// specifier naming a dependent type. |
725 | | ExprResult |
726 | | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
727 | | SourceLocation TemplateKWLoc, |
728 | | const DeclarationNameInfo &NameInfo, |
729 | | bool isAddressOfOperand, |
730 | 852k | const TemplateArgumentListInfo *TemplateArgs) { |
731 | 852k | DeclContext *DC = getFunctionLevelDeclContext(); |
732 | | |
733 | | // C++11 [expr.prim.general]p12: |
734 | | // An id-expression that denotes a non-static data member or non-static |
735 | | // member function of a class can only be used: |
736 | | // (...) |
737 | | // - if that id-expression denotes a non-static data member and it |
738 | | // appears in an unevaluated operand. |
739 | | // |
740 | | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
741 | | // CXXDependentScopeMemberExpr. The former can instantiate to either |
742 | | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
743 | | // always a MemberExpr. |
744 | 852k | bool MightBeCxx11UnevalField = |
745 | 852k | getLangOpts().CPlusPlus11 && isUnevaluatedContext()852k ; |
746 | | |
747 | | // Check if the nested name specifier is an enum type. |
748 | 852k | bool IsEnum = false; |
749 | 852k | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
750 | 852k | IsEnum = isa_and_nonnull<EnumType>(NNS->getAsType()); |
751 | | |
752 | 852k | if (!MightBeCxx11UnevalField && !isAddressOfOperand848k && !IsEnum848k && |
753 | 852k | isa<CXXMethodDecl>(DC)848k && cast<CXXMethodDecl>(DC)->isInstance()174k ) { |
754 | 141k | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
755 | | |
756 | | // Since the 'this' expression is synthesized, we don't need to |
757 | | // perform the double-lookup check. |
758 | 141k | NamedDecl *FirstQualifierInScope = nullptr; |
759 | | |
760 | 141k | return CXXDependentScopeMemberExpr::Create( |
761 | 141k | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
762 | 141k | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
763 | 141k | FirstQualifierInScope, NameInfo, TemplateArgs); |
764 | 141k | } |
765 | | |
766 | 711k | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
767 | 852k | } |
768 | | |
769 | | ExprResult |
770 | | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
771 | | SourceLocation TemplateKWLoc, |
772 | | const DeclarationNameInfo &NameInfo, |
773 | 1.18M | const TemplateArgumentListInfo *TemplateArgs) { |
774 | | // DependentScopeDeclRefExpr::Create requires a valid QualifierLoc |
775 | 1.18M | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
776 | 1.18M | if (!QualifierLoc) |
777 | 1 | return ExprError(); |
778 | | |
779 | 1.18M | return DependentScopeDeclRefExpr::Create( |
780 | 1.18M | Context, QualifierLoc, TemplateKWLoc, NameInfo, TemplateArgs); |
781 | 1.18M | } |
782 | | |
783 | | |
784 | | /// Determine whether we would be unable to instantiate this template (because |
785 | | /// it either has no definition, or is in the process of being instantiated). |
786 | | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
787 | | NamedDecl *Instantiation, |
788 | | bool InstantiatedFromMember, |
789 | | const NamedDecl *Pattern, |
790 | | const NamedDecl *PatternDef, |
791 | | TemplateSpecializationKind TSK, |
792 | 1.99M | bool Complain /*= true*/) { |
793 | 1.99M | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
794 | 1.99M | isa<VarDecl>(Instantiation)); |
795 | | |
796 | 0 | bool IsEntityBeingDefined = false; |
797 | 1.99M | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
798 | 931k | IsEntityBeingDefined = TD->isBeingDefined(); |
799 | | |
800 | 1.99M | if (PatternDef && !IsEntityBeingDefined1.94M ) { |
801 | 1.94M | NamedDecl *SuggestedDef = nullptr; |
802 | 1.94M | if (!hasReachableDefinition(const_cast<NamedDecl *>(PatternDef), |
803 | 1.94M | &SuggestedDef, |
804 | 1.94M | /*OnlyNeedComplete*/ false)) { |
805 | | // If we're allowed to diagnose this and recover, do so. |
806 | 23 | bool Recover = Complain && !isSFINAEContext()16 ; |
807 | 23 | if (Complain) |
808 | 16 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
809 | 16 | Sema::MissingImportKind::Definition, Recover); |
810 | 23 | return !Recover; |
811 | 23 | } |
812 | 1.94M | return false; |
813 | 1.94M | } |
814 | | |
815 | 51.3k | if (!Complain || (641 PatternDef641 && PatternDef->isInvalidDecl()3 )) |
816 | 50.6k | return true; |
817 | | |
818 | 641 | llvm::Optional<unsigned> Note; |
819 | 641 | QualType InstantiationTy; |
820 | 641 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
821 | 596 | InstantiationTy = Context.getTypeDeclType(TD); |
822 | 641 | if (PatternDef) { |
823 | 3 | Diag(PointOfInstantiation, |
824 | 3 | diag::err_template_instantiate_within_definition) |
825 | 3 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
826 | 3 | << InstantiationTy; |
827 | | // Not much point in noting the template declaration here, since |
828 | | // we're lexically inside it. |
829 | 3 | Instantiation->setInvalidDecl(); |
830 | 638 | } else if (InstantiatedFromMember) { |
831 | 16 | if (isa<FunctionDecl>(Instantiation)) { |
832 | 2 | Diag(PointOfInstantiation, |
833 | 2 | diag::err_explicit_instantiation_undefined_member) |
834 | 2 | << /*member function*/ 1 << Instantiation->getDeclName() |
835 | 2 | << Instantiation->getDeclContext(); |
836 | 2 | Note = diag::note_explicit_instantiation_here; |
837 | 14 | } else { |
838 | 14 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
839 | 0 | Diag(PointOfInstantiation, |
840 | 14 | diag::err_implicit_instantiate_member_undefined) |
841 | 14 | << InstantiationTy; |
842 | 14 | Note = diag::note_member_declared_at; |
843 | 14 | } |
844 | 622 | } else { |
845 | 622 | if (isa<FunctionDecl>(Instantiation)) { |
846 | 9 | Diag(PointOfInstantiation, |
847 | 9 | diag::err_explicit_instantiation_undefined_func_template) |
848 | 9 | << Pattern; |
849 | 9 | Note = diag::note_explicit_instantiation_here; |
850 | 613 | } else if (isa<TagDecl>(Instantiation)) { |
851 | 579 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
852 | 579 | << (TSK != TSK_ImplicitInstantiation) |
853 | 579 | << InstantiationTy; |
854 | 579 | Note = diag::note_template_decl_here; |
855 | 579 | } else { |
856 | 34 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
857 | 34 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
858 | 30 | Diag(PointOfInstantiation, |
859 | 30 | diag::err_explicit_instantiation_undefined_var_template) |
860 | 30 | << Instantiation; |
861 | 30 | Instantiation->setInvalidDecl(); |
862 | 30 | } else |
863 | 4 | Diag(PointOfInstantiation, |
864 | 4 | diag::err_explicit_instantiation_undefined_member) |
865 | 4 | << /*static data member*/ 2 << Instantiation->getDeclName() |
866 | 4 | << Instantiation->getDeclContext(); |
867 | 34 | Note = diag::note_explicit_instantiation_here; |
868 | 34 | } |
869 | 622 | } |
870 | 641 | if (Note) // Diagnostics were emitted. |
871 | 638 | Diag(Pattern->getLocation(), *Note); |
872 | | |
873 | | // In general, Instantiation isn't marked invalid to get more than one |
874 | | // error for multiple undefined instantiations. But the code that does |
875 | | // explicit declaration -> explicit definition conversion can't handle |
876 | | // invalid declarations, so mark as invalid in that case. |
877 | 641 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
878 | 3 | Instantiation->setInvalidDecl(); |
879 | 641 | return true; |
880 | 51.3k | } |
881 | | |
882 | | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
883 | | /// that the template parameter 'PrevDecl' is being shadowed by a new |
884 | | /// declaration at location Loc. Returns true to indicate that this is |
885 | | /// an error, and false otherwise. |
886 | 93 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
887 | 93 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
888 | | |
889 | | // C++ [temp.local]p4: |
890 | | // A template-parameter shall not be redeclared within its |
891 | | // scope (including nested scopes). |
892 | | // |
893 | | // Make this a warning when MSVC compatibility is requested. |
894 | 93 | unsigned DiagId = getLangOpts().MSVCCompat ? diag::ext_template_param_shadow11 |
895 | 93 | : diag::err_template_param_shadow82 ; |
896 | 93 | Diag(Loc, DiagId) << cast<NamedDecl>(PrevDecl)->getDeclName(); |
897 | 93 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
898 | 93 | } |
899 | | |
900 | | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
901 | | /// the parameter D to reference the templated declaration and return a pointer |
902 | | /// to the template declaration. Otherwise, do nothing to D and return null. |
903 | 7.19M | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
904 | 7.19M | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
905 | 1.61M | D = Temp->getTemplatedDecl(); |
906 | 1.61M | return Temp; |
907 | 1.61M | } |
908 | 5.58M | return nullptr; |
909 | 7.19M | } |
910 | | |
911 | | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
912 | 24 | SourceLocation EllipsisLoc) const { |
913 | 24 | assert(Kind == Template && |
914 | 24 | "Only template template arguments can be pack expansions here"); |
915 | 0 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
916 | 24 | "Template template argument pack expansion without packs"); |
917 | 0 | ParsedTemplateArgument Result(*this); |
918 | 24 | Result.EllipsisLoc = EllipsisLoc; |
919 | 24 | return Result; |
920 | 24 | } |
921 | | |
922 | | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
923 | 5.78M | const ParsedTemplateArgument &Arg) { |
924 | | |
925 | 5.78M | switch (Arg.getKind()) { |
926 | 4.91M | case ParsedTemplateArgument::Type: { |
927 | 4.91M | TypeSourceInfo *DI; |
928 | 4.91M | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
929 | 4.91M | if (!DI) |
930 | 0 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
931 | 4.91M | return TemplateArgumentLoc(TemplateArgument(T), DI); |
932 | 0 | } |
933 | | |
934 | 851k | case ParsedTemplateArgument::NonType: { |
935 | 851k | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
936 | 851k | return TemplateArgumentLoc(TemplateArgument(E), E); |
937 | 0 | } |
938 | | |
939 | 21.9k | case ParsedTemplateArgument::Template: { |
940 | 21.9k | TemplateName Template = Arg.getAsTemplate().get(); |
941 | 21.9k | TemplateArgument TArg; |
942 | 21.9k | if (Arg.getEllipsisLoc().isValid()) |
943 | 24 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
944 | 21.9k | else |
945 | 21.9k | TArg = Template; |
946 | 21.9k | return TemplateArgumentLoc( |
947 | 21.9k | SemaRef.Context, TArg, |
948 | 21.9k | Arg.getScopeSpec().getWithLocInContext(SemaRef.Context), |
949 | 21.9k | Arg.getLocation(), Arg.getEllipsisLoc()); |
950 | 0 | } |
951 | 5.78M | } |
952 | | |
953 | 0 | llvm_unreachable("Unhandled parsed template argument"); |
954 | 0 | } |
955 | | |
956 | | /// Translates template arguments as provided by the parser |
957 | | /// into template arguments used by semantic analysis. |
958 | | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
959 | 3.61M | TemplateArgumentListInfo &TemplateArgs) { |
960 | 9.39M | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I5.77M ) |
961 | 5.77M | TemplateArgs.addArgument(translateTemplateArgument(*this, |
962 | 5.77M | TemplateArgsIn[I])); |
963 | 3.61M | } |
964 | | |
965 | | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
966 | | SourceLocation Loc, |
967 | 2.71M | IdentifierInfo *Name) { |
968 | 2.71M | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
969 | 2.71M | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
970 | 2.71M | if (PrevDecl && PrevDecl->isTemplateParameter()9.43k ) |
971 | 24 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
972 | 2.71M | } |
973 | | |
974 | | /// Convert a parsed type into a parsed template argument. This is mostly |
975 | | /// trivial, except that we may have parsed a C++17 deduced class template |
976 | | /// specialization type, in which case we should form a template template |
977 | | /// argument instead of a type template argument. |
978 | 4.91M | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
979 | 4.91M | TypeSourceInfo *TInfo; |
980 | 4.91M | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
981 | 4.91M | if (T.isNull()) |
982 | 235 | return ParsedTemplateArgument(); |
983 | 4.91M | assert(TInfo && "template argument with no location"); |
984 | | |
985 | | // If we might have formed a deduced template specialization type, convert |
986 | | // it to a template template argument. |
987 | 4.91M | if (getLangOpts().CPlusPlus17) { |
988 | 398k | TypeLoc TL = TInfo->getTypeLoc(); |
989 | 398k | SourceLocation EllipsisLoc; |
990 | 398k | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
991 | 0 | EllipsisLoc = PET.getEllipsisLoc(); |
992 | 0 | TL = PET.getPatternLoc(); |
993 | 0 | } |
994 | | |
995 | 398k | CXXScopeSpec SS; |
996 | 398k | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
997 | 17.0k | SS.Adopt(ET.getQualifierLoc()); |
998 | 17.0k | TL = ET.getNamedTypeLoc(); |
999 | 17.0k | } |
1000 | | |
1001 | 398k | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
1002 | 772 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
1003 | 772 | if (SS.isSet()) |
1004 | 19 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
1005 | 19 | /*HasTemplateKeyword=*/false, |
1006 | 19 | Name); |
1007 | 772 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
1008 | 772 | DTST.getTemplateNameLoc()); |
1009 | 772 | if (EllipsisLoc.isValid()) |
1010 | 0 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
1011 | 772 | return Result; |
1012 | 772 | } |
1013 | 398k | } |
1014 | | |
1015 | | // This is a normal type template argument. Note, if the type template |
1016 | | // argument is an injected-class-name for a template, it has a dual nature |
1017 | | // and can be used as either a type or a template. We handle that in |
1018 | | // convertTypeTemplateArgumentToTemplate. |
1019 | 4.91M | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
1020 | 4.91M | ParsedType.get().getAsOpaquePtr(), |
1021 | 4.91M | TInfo->getTypeLoc().getBeginLoc()); |
1022 | 4.91M | } |
1023 | | |
1024 | | /// ActOnTypeParameter - Called when a C++ template type parameter |
1025 | | /// (e.g., "typename T") has been parsed. Typename specifies whether |
1026 | | /// the keyword "typename" was used to declare the type parameter |
1027 | | /// (otherwise, "class" was used), and KeyLoc is the location of the |
1028 | | /// "class" or "typename" keyword. ParamName is the name of the |
1029 | | /// parameter (NULL indicates an unnamed template parameter) and |
1030 | | /// ParamNameLoc is the location of the parameter name (if any). |
1031 | | /// If the type parameter has a default argument, it will be added |
1032 | | /// later via ActOnTypeParameterDefault. |
1033 | | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
1034 | | SourceLocation EllipsisLoc, |
1035 | | SourceLocation KeyLoc, |
1036 | | IdentifierInfo *ParamName, |
1037 | | SourceLocation ParamNameLoc, |
1038 | | unsigned Depth, unsigned Position, |
1039 | | SourceLocation EqualLoc, |
1040 | | ParsedType DefaultArg, |
1041 | 2.65M | bool HasTypeConstraint) { |
1042 | 2.65M | assert(S->isTemplateParamScope() && |
1043 | 2.65M | "Template type parameter not in template parameter scope!"); |
1044 | | |
1045 | 0 | bool IsParameterPack = EllipsisLoc.isValid(); |
1046 | 2.65M | TemplateTypeParmDecl *Param |
1047 | 2.65M | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
1048 | 2.65M | KeyLoc, ParamNameLoc, Depth, Position, |
1049 | 2.65M | ParamName, Typename, IsParameterPack, |
1050 | 2.65M | HasTypeConstraint); |
1051 | 2.65M | Param->setAccess(AS_public); |
1052 | | |
1053 | 2.65M | if (Param->isParameterPack()) |
1054 | 167k | if (auto *LSI = getEnclosingLambda()) |
1055 | 13 | LSI->LocalPacks.push_back(Param); |
1056 | | |
1057 | 2.65M | if (ParamName) { |
1058 | 2.50M | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
1059 | | |
1060 | | // Add the template parameter into the current scope. |
1061 | 2.50M | S->AddDecl(Param); |
1062 | 2.50M | IdResolver.AddDecl(Param); |
1063 | 2.50M | } |
1064 | | |
1065 | | // C++0x [temp.param]p9: |
1066 | | // A default template-argument may be specified for any kind of |
1067 | | // template-parameter that is not a template parameter pack. |
1068 | 2.65M | if (DefaultArg && IsParameterPack128k ) { |
1069 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1070 | 1 | DefaultArg = nullptr; |
1071 | 1 | } |
1072 | | |
1073 | | // Handle the default argument, if provided. |
1074 | 2.65M | if (DefaultArg) { |
1075 | 128k | TypeSourceInfo *DefaultTInfo; |
1076 | 128k | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
1077 | | |
1078 | 128k | assert(DefaultTInfo && "expected source information for type"); |
1079 | | |
1080 | | // Check for unexpanded parameter packs. |
1081 | 128k | if (DiagnoseUnexpandedParameterPack(ParamNameLoc, DefaultTInfo, |
1082 | 128k | UPPC_DefaultArgument)) |
1083 | 2 | return Param; |
1084 | | |
1085 | | // Check the template argument itself. |
1086 | 128k | if (CheckTemplateArgument(DefaultTInfo)) { |
1087 | 0 | Param->setInvalidDecl(); |
1088 | 0 | return Param; |
1089 | 0 | } |
1090 | | |
1091 | 128k | Param->setDefaultArgument(DefaultTInfo); |
1092 | 128k | } |
1093 | | |
1094 | 2.65M | return Param; |
1095 | 2.65M | } |
1096 | | |
1097 | | /// Convert the parser's template argument list representation into our form. |
1098 | | static TemplateArgumentListInfo |
1099 | 277k | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
1100 | 277k | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
1101 | 277k | TemplateId.RAngleLoc); |
1102 | 277k | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
1103 | 277k | TemplateId.NumArgs); |
1104 | 277k | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
1105 | 277k | return TemplateArgs; |
1106 | 277k | } |
1107 | | |
1108 | | bool Sema::ActOnTypeConstraint(const CXXScopeSpec &SS, |
1109 | | TemplateIdAnnotation *TypeConstr, |
1110 | | TemplateTypeParmDecl *ConstrainedParameter, |
1111 | 2.74k | SourceLocation EllipsisLoc) { |
1112 | 2.74k | return BuildTypeConstraint(SS, TypeConstr, ConstrainedParameter, EllipsisLoc, |
1113 | 2.74k | false); |
1114 | 2.74k | } |
1115 | | |
1116 | | bool Sema::BuildTypeConstraint(const CXXScopeSpec &SS, |
1117 | | TemplateIdAnnotation *TypeConstr, |
1118 | | TemplateTypeParmDecl *ConstrainedParameter, |
1119 | | SourceLocation EllipsisLoc, |
1120 | 3.19k | bool AllowUnexpandedPack) { |
1121 | 3.19k | TemplateName TN = TypeConstr->Template.get(); |
1122 | 3.19k | ConceptDecl *CD = cast<ConceptDecl>(TN.getAsTemplateDecl()); |
1123 | | |
1124 | | // C++2a [temp.param]p4: |
1125 | | // [...] The concept designated by a type-constraint shall be a type |
1126 | | // concept ([temp.concept]). |
1127 | 3.19k | if (!CD->isTypeConcept()) { |
1128 | 5 | Diag(TypeConstr->TemplateNameLoc, |
1129 | 5 | diag::err_type_constraint_non_type_concept); |
1130 | 5 | return true; |
1131 | 5 | } |
1132 | | |
1133 | 3.19k | bool WereArgsSpecified = TypeConstr->LAngleLoc.isValid(); |
1134 | | |
1135 | 3.19k | if (!WereArgsSpecified && |
1136 | 3.19k | CD->getTemplateParameters()->getMinRequiredArguments() > 11.91k ) { |
1137 | 4 | Diag(TypeConstr->TemplateNameLoc, |
1138 | 4 | diag::err_type_constraint_missing_arguments) << CD; |
1139 | 4 | return true; |
1140 | 4 | } |
1141 | | |
1142 | 3.18k | DeclarationNameInfo ConceptName(DeclarationName(TypeConstr->Name), |
1143 | 3.18k | TypeConstr->TemplateNameLoc); |
1144 | | |
1145 | 3.18k | TemplateArgumentListInfo TemplateArgs; |
1146 | 3.18k | if (TypeConstr->LAngleLoc.isValid()) { |
1147 | 1.27k | TemplateArgs = |
1148 | 1.27k | makeTemplateArgumentListInfo(*this, *TypeConstr); |
1149 | | |
1150 | 1.27k | if (EllipsisLoc.isInvalid() && !AllowUnexpandedPack1.26k ) { |
1151 | 1.05k | for (TemplateArgumentLoc Arg : TemplateArgs.arguments()) { |
1152 | 1.05k | if (DiagnoseUnexpandedParameterPack(Arg, UPPC_TypeConstraint)) |
1153 | 2 | return true; |
1154 | 1.05k | } |
1155 | 994 | } |
1156 | 1.27k | } |
1157 | 3.18k | return AttachTypeConstraint( |
1158 | 3.18k | SS.isSet() ? SS.getWithLocInContext(Context)76 : NestedNameSpecifierLoc()3.10k , |
1159 | 3.18k | ConceptName, CD, |
1160 | 3.18k | TypeConstr->LAngleLoc.isValid() ? &TemplateArgs1.26k : nullptr1.91k , |
1161 | 3.18k | ConstrainedParameter, EllipsisLoc); |
1162 | 3.18k | } |
1163 | | |
1164 | | template<typename ArgumentLocAppender> |
1165 | | static ExprResult formImmediatelyDeclaredConstraint( |
1166 | | Sema &S, NestedNameSpecifierLoc NS, DeclarationNameInfo NameInfo, |
1167 | | ConceptDecl *NamedConcept, SourceLocation LAngleLoc, |
1168 | | SourceLocation RAngleLoc, QualType ConstrainedType, |
1169 | | SourceLocation ParamNameLoc, ArgumentLocAppender Appender, |
1170 | 5.47k | SourceLocation EllipsisLoc) { |
1171 | | |
1172 | 5.47k | TemplateArgumentListInfo ConstraintArgs; |
1173 | 5.47k | ConstraintArgs.addArgument( |
1174 | 5.47k | S.getTrivialTemplateArgumentLoc(TemplateArgument(ConstrainedType), |
1175 | 5.47k | /*NTTPType=*/QualType(), ParamNameLoc)); |
1176 | | |
1177 | 5.47k | ConstraintArgs.setRAngleLoc(RAngleLoc); |
1178 | 5.47k | ConstraintArgs.setLAngleLoc(LAngleLoc); |
1179 | 5.47k | Appender(ConstraintArgs); |
1180 | | |
1181 | | // C++2a [temp.param]p4: |
1182 | | // [...] This constraint-expression E is called the immediately-declared |
1183 | | // constraint of T. [...] |
1184 | 5.47k | CXXScopeSpec SS; |
1185 | 5.47k | SS.Adopt(NS); |
1186 | 5.47k | ExprResult ImmediatelyDeclaredConstraint = S.CheckConceptTemplateId( |
1187 | 5.47k | SS, /*TemplateKWLoc=*/SourceLocation(), NameInfo, |
1188 | 5.47k | /*FoundDecl=*/NamedConcept, NamedConcept, &ConstraintArgs); |
1189 | 5.47k | if (ImmediatelyDeclaredConstraint.isInvalid() || !EllipsisLoc.isValid()) |
1190 | 5.44k | return ImmediatelyDeclaredConstraint; |
1191 | | |
1192 | | // C++2a [temp.param]p4: |
1193 | | // [...] If T is not a pack, then E is E', otherwise E is (E' && ...). |
1194 | | // |
1195 | | // We have the following case: |
1196 | | // |
1197 | | // template<typename T> concept C1 = true; |
1198 | | // template<C1... T> struct s1; |
1199 | | // |
1200 | | // The constraint: (C1<T> && ...) |
1201 | | // |
1202 | | // Note that the type of C1<T> is known to be 'bool', so we don't need to do |
1203 | | // any unqualified lookups for 'operator&&' here. |
1204 | 29 | return S.BuildCXXFoldExpr(/*UnqualifiedLookup=*/nullptr, |
1205 | 29 | /*LParenLoc=*/SourceLocation(), |
1206 | 29 | ImmediatelyDeclaredConstraint.get(), BO_LAnd, |
1207 | 29 | EllipsisLoc, /*RHS=*/nullptr, |
1208 | 29 | /*RParenLoc=*/SourceLocation(), |
1209 | 29 | /*NumExpansions=*/None); |
1210 | 5.47k | } SemaTemplate.cpp:clang::ActionResult<clang::Expr*, true> formImmediatelyDeclaredConstraint<clang::Sema::AttachTypeConstraint(clang::NestedNameSpecifierLoc, clang::DeclarationNameInfo, clang::ConceptDecl*, clang::TemplateArgumentListInfo const*, clang::TemplateTypeParmDecl*, clang::SourceLocation)::$_2>(clang::Sema&, clang::NestedNameSpecifierLoc, clang::DeclarationNameInfo, clang::ConceptDecl*, clang::SourceLocation, clang::SourceLocation, clang::QualType, clang::SourceLocation, clang::Sema::AttachTypeConstraint(clang::NestedNameSpecifierLoc, clang::DeclarationNameInfo, clang::ConceptDecl*, clang::TemplateArgumentListInfo const*, clang::TemplateTypeParmDecl*, clang::SourceLocation)::$_2, clang::SourceLocation) Line | Count | Source | 1170 | 5.47k | SourceLocation EllipsisLoc) { | 1171 | | | 1172 | 5.47k | TemplateArgumentListInfo ConstraintArgs; | 1173 | 5.47k | ConstraintArgs.addArgument( | 1174 | 5.47k | S.getTrivialTemplateArgumentLoc(TemplateArgument(ConstrainedType), | 1175 | 5.47k | /*NTTPType=*/QualType(), ParamNameLoc)); | 1176 | | | 1177 | 5.47k | ConstraintArgs.setRAngleLoc(RAngleLoc); | 1178 | 5.47k | ConstraintArgs.setLAngleLoc(LAngleLoc); | 1179 | 5.47k | Appender(ConstraintArgs); | 1180 | | | 1181 | | // C++2a [temp.param]p4: | 1182 | | // [...] This constraint-expression E is called the immediately-declared | 1183 | | // constraint of T. [...] | 1184 | 5.47k | CXXScopeSpec SS; | 1185 | 5.47k | SS.Adopt(NS); | 1186 | 5.47k | ExprResult ImmediatelyDeclaredConstraint = S.CheckConceptTemplateId( | 1187 | 5.47k | SS, /*TemplateKWLoc=*/SourceLocation(), NameInfo, | 1188 | 5.47k | /*FoundDecl=*/NamedConcept, NamedConcept, &ConstraintArgs); | 1189 | 5.47k | if (ImmediatelyDeclaredConstraint.isInvalid() || !EllipsisLoc.isValid()) | 1190 | 5.44k | return ImmediatelyDeclaredConstraint; | 1191 | | | 1192 | | // C++2a [temp.param]p4: | 1193 | | // [...] If T is not a pack, then E is E', otherwise E is (E' && ...). | 1194 | | // | 1195 | | // We have the following case: | 1196 | | // | 1197 | | // template<typename T> concept C1 = true; | 1198 | | // template<C1... T> struct s1; | 1199 | | // | 1200 | | // The constraint: (C1<T> && ...) | 1201 | | // | 1202 | | // Note that the type of C1<T> is known to be 'bool', so we don't need to do | 1203 | | // any unqualified lookups for 'operator&&' here. | 1204 | 28 | return S.BuildCXXFoldExpr(/*UnqualifiedLookup=*/nullptr, | 1205 | 28 | /*LParenLoc=*/SourceLocation(), | 1206 | 28 | ImmediatelyDeclaredConstraint.get(), BO_LAnd, | 1207 | 28 | EllipsisLoc, /*RHS=*/nullptr, | 1208 | 28 | /*RParenLoc=*/SourceLocation(), | 1209 | 28 | /*NumExpansions=*/None); | 1210 | 5.47k | } |
SemaTemplate.cpp:clang::ActionResult<clang::Expr*, true> formImmediatelyDeclaredConstraint<clang::Sema::AttachTypeConstraint(clang::AutoTypeLoc, clang::NonTypeTemplateParmDecl*, clang::SourceLocation)::$_3>(clang::Sema&, clang::NestedNameSpecifierLoc, clang::DeclarationNameInfo, clang::ConceptDecl*, clang::SourceLocation, clang::SourceLocation, clang::QualType, clang::SourceLocation, clang::Sema::AttachTypeConstraint(clang::AutoTypeLoc, clang::NonTypeTemplateParmDecl*, clang::SourceLocation)::$_3, clang::SourceLocation) Line | Count | Source | 1170 | 2 | SourceLocation EllipsisLoc) { | 1171 | | | 1172 | 2 | TemplateArgumentListInfo ConstraintArgs; | 1173 | 2 | ConstraintArgs.addArgument( | 1174 | 2 | S.getTrivialTemplateArgumentLoc(TemplateArgument(ConstrainedType), | 1175 | 2 | /*NTTPType=*/QualType(), ParamNameLoc)); | 1176 | | | 1177 | 2 | ConstraintArgs.setRAngleLoc(RAngleLoc); | 1178 | 2 | ConstraintArgs.setLAngleLoc(LAngleLoc); | 1179 | 2 | Appender(ConstraintArgs); | 1180 | | | 1181 | | // C++2a [temp.param]p4: | 1182 | | // [...] This constraint-expression E is called the immediately-declared | 1183 | | // constraint of T. [...] | 1184 | 2 | CXXScopeSpec SS; | 1185 | 2 | SS.Adopt(NS); | 1186 | 2 | ExprResult ImmediatelyDeclaredConstraint = S.CheckConceptTemplateId( | 1187 | 2 | SS, /*TemplateKWLoc=*/SourceLocation(), NameInfo, | 1188 | 2 | /*FoundDecl=*/NamedConcept, NamedConcept, &ConstraintArgs); | 1189 | 2 | if (ImmediatelyDeclaredConstraint.isInvalid() || !EllipsisLoc.isValid()) | 1190 | 1 | return ImmediatelyDeclaredConstraint; | 1191 | | | 1192 | | // C++2a [temp.param]p4: | 1193 | | // [...] If T is not a pack, then E is E', otherwise E is (E' && ...). | 1194 | | // | 1195 | | // We have the following case: | 1196 | | // | 1197 | | // template<typename T> concept C1 = true; | 1198 | | // template<C1... T> struct s1; | 1199 | | // | 1200 | | // The constraint: (C1<T> && ...) | 1201 | | // | 1202 | | // Note that the type of C1<T> is known to be 'bool', so we don't need to do | 1203 | | // any unqualified lookups for 'operator&&' here. | 1204 | 1 | return S.BuildCXXFoldExpr(/*UnqualifiedLookup=*/nullptr, | 1205 | 1 | /*LParenLoc=*/SourceLocation(), | 1206 | 1 | ImmediatelyDeclaredConstraint.get(), BO_LAnd, | 1207 | 1 | EllipsisLoc, /*RHS=*/nullptr, | 1208 | 1 | /*RParenLoc=*/SourceLocation(), | 1209 | 1 | /*NumExpansions=*/None); | 1210 | 2 | } |
|
1211 | | |
1212 | | /// Attach a type-constraint to a template parameter. |
1213 | | /// \returns true if an error occurred. This can happen if the |
1214 | | /// immediately-declared constraint could not be formed (e.g. incorrect number |
1215 | | /// of arguments for the named concept). |
1216 | | bool Sema::AttachTypeConstraint(NestedNameSpecifierLoc NS, |
1217 | | DeclarationNameInfo NameInfo, |
1218 | | ConceptDecl *NamedConcept, |
1219 | | const TemplateArgumentListInfo *TemplateArgs, |
1220 | | TemplateTypeParmDecl *ConstrainedParameter, |
1221 | 5.47k | SourceLocation EllipsisLoc) { |
1222 | | // C++2a [temp.param]p4: |
1223 | | // [...] If Q is of the form C<A1, ..., An>, then let E' be |
1224 | | // C<T, A1, ..., An>. Otherwise, let E' be C<T>. [...] |
1225 | 5.47k | const ASTTemplateArgumentListInfo *ArgsAsWritten = |
1226 | 5.47k | TemplateArgs ? ASTTemplateArgumentListInfo::Create(Context, |
1227 | 3.50k | *TemplateArgs) : nullptr1.96k ; |
1228 | | |
1229 | 5.47k | QualType ParamAsArgument(ConstrainedParameter->getTypeForDecl(), 0); |
1230 | | |
1231 | 5.47k | ExprResult ImmediatelyDeclaredConstraint = |
1232 | 5.47k | formImmediatelyDeclaredConstraint( |
1233 | 5.47k | *this, NS, NameInfo, NamedConcept, |
1234 | 5.47k | TemplateArgs ? TemplateArgs->getLAngleLoc()3.50k : SourceLocation()1.96k , |
1235 | 5.47k | TemplateArgs ? TemplateArgs->getRAngleLoc()3.50k : SourceLocation()1.96k , |
1236 | 5.47k | ParamAsArgument, ConstrainedParameter->getLocation(), |
1237 | 5.47k | [&] (TemplateArgumentListInfo &ConstraintArgs) { |
1238 | 5.47k | if (TemplateArgs) |
1239 | 3.50k | for (const auto &ArgLoc : TemplateArgs->arguments()) |
1240 | 2.68k | ConstraintArgs.addArgument(ArgLoc); |
1241 | 5.47k | }, EllipsisLoc); |
1242 | 5.47k | if (ImmediatelyDeclaredConstraint.isInvalid()) |
1243 | 0 | return true; |
1244 | | |
1245 | 5.47k | ConstrainedParameter->setTypeConstraint(NS, NameInfo, |
1246 | 5.47k | /*FoundDecl=*/NamedConcept, |
1247 | 5.47k | NamedConcept, ArgsAsWritten, |
1248 | 5.47k | ImmediatelyDeclaredConstraint.get()); |
1249 | 5.47k | return false; |
1250 | 5.47k | } |
1251 | | |
1252 | | bool Sema::AttachTypeConstraint(AutoTypeLoc TL, NonTypeTemplateParmDecl *NTTP, |
1253 | 3 | SourceLocation EllipsisLoc) { |
1254 | 3 | if (NTTP->getType() != TL.getType() || |
1255 | 3 | TL.getAutoKeyword() != AutoTypeKeyword::Auto2 ) { |
1256 | 1 | Diag(NTTP->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
1257 | 1 | diag::err_unsupported_placeholder_constraint) |
1258 | 1 | << NTTP->getTypeSourceInfo()->getTypeLoc().getSourceRange(); |
1259 | 1 | return true; |
1260 | 1 | } |
1261 | | // FIXME: Concepts: This should be the type of the placeholder, but this is |
1262 | | // unclear in the wording right now. |
1263 | 2 | DeclRefExpr *Ref = |
1264 | 2 | BuildDeclRefExpr(NTTP, NTTP->getType(), VK_PRValue, NTTP->getLocation()); |
1265 | 2 | if (!Ref) |
1266 | 0 | return true; |
1267 | 2 | ExprResult ImmediatelyDeclaredConstraint = formImmediatelyDeclaredConstraint( |
1268 | 2 | *this, TL.getNestedNameSpecifierLoc(), TL.getConceptNameInfo(), |
1269 | 2 | TL.getNamedConcept(), TL.getLAngleLoc(), TL.getRAngleLoc(), |
1270 | 2 | BuildDecltypeType(Ref), NTTP->getLocation(), |
1271 | 2 | [&](TemplateArgumentListInfo &ConstraintArgs) { |
1272 | 6 | for (unsigned I = 0, C = TL.getNumArgs(); I != C; ++I4 ) |
1273 | 4 | ConstraintArgs.addArgument(TL.getArgLoc(I)); |
1274 | 2 | }, |
1275 | 2 | EllipsisLoc); |
1276 | 2 | if (ImmediatelyDeclaredConstraint.isInvalid() || |
1277 | 2 | !ImmediatelyDeclaredConstraint.isUsable()) |
1278 | 0 | return true; |
1279 | | |
1280 | 2 | NTTP->setPlaceholderTypeConstraint(ImmediatelyDeclaredConstraint.get()); |
1281 | 2 | return false; |
1282 | 2 | } |
1283 | | |
1284 | | /// Check that the type of a non-type template parameter is |
1285 | | /// well-formed. |
1286 | | /// |
1287 | | /// \returns the (possibly-promoted) parameter type if valid; |
1288 | | /// otherwise, produces a diagnostic and returns a NULL type. |
1289 | | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
1290 | 414k | SourceLocation Loc) { |
1291 | 414k | if (TSI->getType()->isUndeducedType()) { |
1292 | | // C++17 [temp.dep.expr]p3: |
1293 | | // An id-expression is type-dependent if it contains |
1294 | | // - an identifier associated by name lookup with a non-type |
1295 | | // template-parameter declared with a type that contains a |
1296 | | // placeholder type (7.1.7.4), |
1297 | 228 | TSI = SubstAutoTypeSourceInfoDependent(TSI); |
1298 | 228 | } |
1299 | | |
1300 | 414k | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
1301 | 414k | } |
1302 | | |
1303 | | /// Require the given type to be a structural type, and diagnose if it is not. |
1304 | | /// |
1305 | | /// \return \c true if an error was produced. |
1306 | 373 | bool Sema::RequireStructuralType(QualType T, SourceLocation Loc) { |
1307 | 373 | if (T->isDependentType()) |
1308 | 0 | return false; |
1309 | | |
1310 | 373 | if (RequireCompleteType(Loc, T, diag::err_template_nontype_parm_incomplete)) |
1311 | 8 | return true; |
1312 | | |
1313 | 365 | if (T->isStructuralType()) |
1314 | 315 | return false; |
1315 | | |
1316 | | // Structural types are required to be object types or lvalue references. |
1317 | 50 | if (T->isRValueReferenceType()) { |
1318 | 4 | Diag(Loc, diag::err_template_nontype_parm_rvalue_ref) << T; |
1319 | 4 | return true; |
1320 | 4 | } |
1321 | | |
1322 | | // Don't mention structural types in our diagnostic prior to C++20. Also, |
1323 | | // there's not much more we can say about non-scalar non-class types -- |
1324 | | // because we can't see functions or arrays here, those can only be language |
1325 | | // extensions. |
1326 | 46 | if (!getLangOpts().CPlusPlus20 || |
1327 | 46 | (15 !T->isScalarType()15 && !T->isRecordType()15 )) { |
1328 | 33 | Diag(Loc, diag::err_template_nontype_parm_bad_type) << T; |
1329 | 33 | return true; |
1330 | 33 | } |
1331 | | |
1332 | | // Structural types are required to be literal types. |
1333 | 13 | if (RequireLiteralType(Loc, T, diag::err_template_nontype_parm_not_literal)) |
1334 | 0 | return true; |
1335 | | |
1336 | 13 | Diag(Loc, diag::err_template_nontype_parm_not_structural) << T; |
1337 | | |
1338 | | // Drill down into the reason why the class is non-structural. |
1339 | 18 | while (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
1340 | | // All members are required to be public and non-mutable, and can't be of |
1341 | | // rvalue reference type. Check these conditions first to prefer a "local" |
1342 | | // reason over a more distant one. |
1343 | 16 | for (const FieldDecl *FD : RD->fields()) { |
1344 | 12 | if (FD->getAccess() != AS_public) { |
1345 | 3 | Diag(FD->getLocation(), diag::note_not_structural_non_public) << T << 0; |
1346 | 3 | return true; |
1347 | 3 | } |
1348 | 9 | if (FD->isMutable()) { |
1349 | 1 | Diag(FD->getLocation(), diag::note_not_structural_mutable_field) << T; |
1350 | 1 | return true; |
1351 | 1 | } |
1352 | 8 | if (FD->getType()->isRValueReferenceType()) { |
1353 | 4 | Diag(FD->getLocation(), diag::note_not_structural_rvalue_ref_field) |
1354 | 4 | << T; |
1355 | 4 | return true; |
1356 | 4 | } |
1357 | 8 | } |
1358 | | |
1359 | | // All bases are required to be public. |
1360 | 8 | for (const auto &BaseSpec : RD->bases()) { |
1361 | 4 | if (BaseSpec.getAccessSpecifier() != AS_public) { |
1362 | 3 | Diag(BaseSpec.getBaseTypeLoc(), diag::note_not_structural_non_public) |
1363 | 3 | << T << 1; |
1364 | 3 | return true; |
1365 | 3 | } |
1366 | 4 | } |
1367 | | |
1368 | | // All subobjects are required to be of structural types. |
1369 | 5 | SourceLocation SubLoc; |
1370 | 5 | QualType SubType; |
1371 | 5 | int Kind = -1; |
1372 | | |
1373 | 5 | for (const FieldDecl *FD : RD->fields()) { |
1374 | 4 | QualType T = Context.getBaseElementType(FD->getType()); |
1375 | 4 | if (!T->isStructuralType()) { |
1376 | 4 | SubLoc = FD->getLocation(); |
1377 | 4 | SubType = T; |
1378 | 4 | Kind = 0; |
1379 | 4 | break; |
1380 | 4 | } |
1381 | 4 | } |
1382 | | |
1383 | 5 | if (Kind == -1) { |
1384 | 1 | for (const auto &BaseSpec : RD->bases()) { |
1385 | 1 | QualType T = BaseSpec.getType(); |
1386 | 1 | if (!T->isStructuralType()) { |
1387 | 1 | SubLoc = BaseSpec.getBaseTypeLoc(); |
1388 | 1 | SubType = T; |
1389 | 1 | Kind = 1; |
1390 | 1 | break; |
1391 | 1 | } |
1392 | 1 | } |
1393 | 1 | } |
1394 | | |
1395 | 5 | assert(Kind != -1 && "couldn't find reason why type is not structural"); |
1396 | 0 | Diag(SubLoc, diag::note_not_structural_subobject) |
1397 | 5 | << T << Kind << SubType; |
1398 | 5 | T = SubType; |
1399 | 5 | RD = T->getAsCXXRecordDecl(); |
1400 | 5 | } |
1401 | | |
1402 | 2 | return true; |
1403 | 13 | } |
1404 | | |
1405 | | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
1406 | 971k | SourceLocation Loc) { |
1407 | | // We don't allow variably-modified types as the type of non-type template |
1408 | | // parameters. |
1409 | 971k | if (T->isVariablyModifiedType()) { |
1410 | 1 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
1411 | 1 | << T; |
1412 | 1 | return QualType(); |
1413 | 1 | } |
1414 | | |
1415 | | // C++ [temp.param]p4: |
1416 | | // |
1417 | | // A non-type template-parameter shall have one of the following |
1418 | | // (optionally cv-qualified) types: |
1419 | | // |
1420 | | // -- integral or enumeration type, |
1421 | 971k | if (T->isIntegralOrEnumerationType() || |
1422 | | // -- pointer to object or pointer to function, |
1423 | 971k | T->isPointerType()226k || |
1424 | | // -- lvalue reference to object or lvalue reference to function, |
1425 | 971k | T->isLValueReferenceType()165k || |
1426 | | // -- pointer to member, |
1427 | 971k | T->isMemberPointerType()160k || |
1428 | | // -- std::nullptr_t, or |
1429 | 971k | T->isNullPtrType()160k || |
1430 | | // -- a type that contains a placeholder type. |
1431 | 971k | T->isUndeducedType()160k ) { |
1432 | | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
1433 | | // are ignored when determining its type. |
1434 | 811k | return T.getUnqualifiedType(); |
1435 | 811k | } |
1436 | | |
1437 | | // C++ [temp.param]p8: |
1438 | | // |
1439 | | // A non-type template-parameter of type "array of T" or |
1440 | | // "function returning T" is adjusted to be of type "pointer to |
1441 | | // T" or "pointer to function returning T", respectively. |
1442 | 159k | if (T->isArrayType() || T->isFunctionType()159k ) |
1443 | 99 | return Context.getDecayedType(T); |
1444 | | |
1445 | | // If T is a dependent type, we can't do the check now, so we |
1446 | | // assume that it is well-formed. Note that stripping off the |
1447 | | // qualifiers here is not really correct if T turns out to be |
1448 | | // an array type, but we'll recompute the type everywhere it's |
1449 | | // used during instantiation, so that should be OK. (Using the |
1450 | | // qualified type is equally wrong.) |
1451 | 159k | if (T->isDependentType()) |
1452 | 158k | return T.getUnqualifiedType(); |
1453 | | |
1454 | | // C++20 [temp.param]p6: |
1455 | | // -- a structural type |
1456 | 373 | if (RequireStructuralType(T, Loc)) |
1457 | 58 | return QualType(); |
1458 | | |
1459 | 315 | if (!getLangOpts().CPlusPlus20) { |
1460 | | // FIXME: Consider allowing structural types as an extension in C++17. (In |
1461 | | // earlier language modes, the template argument evaluation rules are too |
1462 | | // inflexible.) |
1463 | 30 | Diag(Loc, diag::err_template_nontype_parm_bad_structural_type) << T; |
1464 | 30 | return QualType(); |
1465 | 30 | } |
1466 | | |
1467 | 285 | Diag(Loc, diag::warn_cxx17_compat_template_nontype_parm_type) << T; |
1468 | 285 | return T.getUnqualifiedType(); |
1469 | 315 | } |
1470 | | |
1471 | | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
1472 | | unsigned Depth, |
1473 | | unsigned Position, |
1474 | | SourceLocation EqualLoc, |
1475 | 267k | Expr *Default) { |
1476 | 267k | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
1477 | | |
1478 | | // Check that we have valid decl-specifiers specified. |
1479 | 267k | auto CheckValidDeclSpecifiers = [this, &D] { |
1480 | | // C++ [temp.param] |
1481 | | // p1 |
1482 | | // template-parameter: |
1483 | | // ... |
1484 | | // parameter-declaration |
1485 | | // p2 |
1486 | | // ... A storage class shall not be specified in a template-parameter |
1487 | | // declaration. |
1488 | | // [dcl.typedef]p1: |
1489 | | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
1490 | | // of a parameter-declaration |
1491 | 267k | const DeclSpec &DS = D.getDeclSpec(); |
1492 | 267k | auto EmitDiag = [this](SourceLocation Loc) { |
1493 | 35 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
1494 | 35 | << FixItHint::CreateRemoval(Loc); |
1495 | 35 | }; |
1496 | 267k | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
1497 | 18 | EmitDiag(DS.getStorageClassSpecLoc()); |
1498 | | |
1499 | 267k | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
1500 | 1 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
1501 | | |
1502 | | // [dcl.inline]p1: |
1503 | | // The inline specifier can be applied only to the declaration or |
1504 | | // definition of a variable or function. |
1505 | | |
1506 | 267k | if (DS.isInlineSpecified()) |
1507 | 6 | EmitDiag(DS.getInlineSpecLoc()); |
1508 | | |
1509 | | // [dcl.constexpr]p1: |
1510 | | // The constexpr specifier shall be applied only to the definition of a |
1511 | | // variable or variable template or the declaration of a function or |
1512 | | // function template. |
1513 | | |
1514 | 267k | if (DS.hasConstexprSpecifier()) |
1515 | 1 | EmitDiag(DS.getConstexprSpecLoc()); |
1516 | | |
1517 | | // [dcl.fct.spec]p1: |
1518 | | // Function-specifiers can be used only in function declarations. |
1519 | | |
1520 | 267k | if (DS.isVirtualSpecified()) |
1521 | 3 | EmitDiag(DS.getVirtualSpecLoc()); |
1522 | | |
1523 | 267k | if (DS.hasExplicitSpecifier()) |
1524 | 6 | EmitDiag(DS.getExplicitSpecLoc()); |
1525 | | |
1526 | 267k | if (DS.isNoreturnSpecified()) |
1527 | 0 | EmitDiag(DS.getNoreturnSpecLoc()); |
1528 | 267k | }; |
1529 | | |
1530 | 267k | CheckValidDeclSpecifiers(); |
1531 | | |
1532 | 267k | if (TInfo->getType()->isUndeducedType()) { |
1533 | 208 | Diag(D.getIdentifierLoc(), |
1534 | 208 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
1535 | 208 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
1536 | 208 | } |
1537 | | |
1538 | 267k | assert(S->isTemplateParamScope() && |
1539 | 267k | "Non-type template parameter not in template parameter scope!"); |
1540 | 0 | bool Invalid = false; |
1541 | | |
1542 | 267k | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
1543 | 267k | if (T.isNull()) { |
1544 | 73 | T = Context.IntTy; // Recover with an 'int' type. |
1545 | 73 | Invalid = true; |
1546 | 73 | } |
1547 | | |
1548 | 267k | CheckFunctionOrTemplateParamDeclarator(S, D); |
1549 | | |
1550 | 267k | IdentifierInfo *ParamName = D.getIdentifier(); |
1551 | 267k | bool IsParameterPack = D.hasEllipsis(); |
1552 | 267k | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
1553 | 267k | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
1554 | 267k | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
1555 | 267k | TInfo); |
1556 | 267k | Param->setAccess(AS_public); |
1557 | | |
1558 | 267k | if (AutoTypeLoc TL = TInfo->getTypeLoc().getContainedAutoTypeLoc()) |
1559 | 192 | if (TL.isConstrained()) |
1560 | 3 | if (AttachTypeConstraint(TL, Param, D.getEllipsisLoc())) |
1561 | 1 | Invalid = true; |
1562 | | |
1563 | 267k | if (Invalid) |
1564 | 74 | Param->setInvalidDecl(); |
1565 | | |
1566 | 267k | if (Param->isParameterPack()) |
1567 | 14.8k | if (auto *LSI = getEnclosingLambda()) |
1568 | 12 | LSI->LocalPacks.push_back(Param); |
1569 | | |
1570 | 267k | if (ParamName) { |
1571 | 194k | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
1572 | 194k | ParamName); |
1573 | | |
1574 | | // Add the template parameter into the current scope. |
1575 | 194k | S->AddDecl(Param); |
1576 | 194k | IdResolver.AddDecl(Param); |
1577 | 194k | } |
1578 | | |
1579 | | // C++0x [temp.param]p9: |
1580 | | // A default template-argument may be specified for any kind of |
1581 | | // template-parameter that is not a template parameter pack. |
1582 | 267k | if (Default && IsParameterPack78.2k ) { |
1583 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1584 | 1 | Default = nullptr; |
1585 | 1 | } |
1586 | | |
1587 | | // Check the well-formedness of the default template argument, if provided. |
1588 | 267k | if (Default) { |
1589 | | // Check for unexpanded parameter packs. |
1590 | 78.2k | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
1591 | 1 | return Param; |
1592 | | |
1593 | 78.2k | TemplateArgument Converted; |
1594 | 78.2k | ExprResult DefaultRes = |
1595 | 78.2k | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
1596 | 78.2k | if (DefaultRes.isInvalid()) { |
1597 | 2 | Param->setInvalidDecl(); |
1598 | 2 | return Param; |
1599 | 2 | } |
1600 | 78.2k | Default = DefaultRes.get(); |
1601 | | |
1602 | 78.2k | Param->setDefaultArgument(Default); |
1603 | 78.2k | } |
1604 | | |
1605 | 267k | return Param; |
1606 | 267k | } |
1607 | | |
1608 | | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
1609 | | /// parameter (e.g. T in template <template \<typename> class T> class array) |
1610 | | /// has been parsed. S is the current scope. |
1611 | | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
1612 | | SourceLocation TmpLoc, |
1613 | | TemplateParameterList *Params, |
1614 | | SourceLocation EllipsisLoc, |
1615 | | IdentifierInfo *Name, |
1616 | | SourceLocation NameLoc, |
1617 | | unsigned Depth, |
1618 | | unsigned Position, |
1619 | | SourceLocation EqualLoc, |
1620 | 19.7k | ParsedTemplateArgument Default) { |
1621 | 19.7k | assert(S->isTemplateParamScope() && |
1622 | 19.7k | "Template template parameter not in template parameter scope!"); |
1623 | | |
1624 | | // Construct the parameter object. |
1625 | 0 | bool IsParameterPack = EllipsisLoc.isValid(); |
1626 | 19.7k | TemplateTemplateParmDecl *Param = |
1627 | 19.7k | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
1628 | 19.7k | NameLoc.isInvalid()? TmpLoc0 : NameLoc, |
1629 | 19.7k | Depth, Position, IsParameterPack, |
1630 | 19.7k | Name, Params); |
1631 | 19.7k | Param->setAccess(AS_public); |
1632 | | |
1633 | 19.7k | if (Param->isParameterPack()) |
1634 | 106 | if (auto *LSI = getEnclosingLambda()) |
1635 | 10 | LSI->LocalPacks.push_back(Param); |
1636 | | |
1637 | | // If the template template parameter has a name, then link the identifier |
1638 | | // into the scope and lookup mechanisms. |
1639 | 19.7k | if (Name) { |
1640 | 18.0k | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
1641 | | |
1642 | 18.0k | S->AddDecl(Param); |
1643 | 18.0k | IdResolver.AddDecl(Param); |
1644 | 18.0k | } |
1645 | | |
1646 | 19.7k | if (Params->size() == 0) { |
1647 | 1 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
1648 | 1 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
1649 | 1 | Param->setInvalidDecl(); |
1650 | 1 | } |
1651 | | |
1652 | | // C++0x [temp.param]p9: |
1653 | | // A default template-argument may be specified for any kind of |
1654 | | // template-parameter that is not a template parameter pack. |
1655 | 19.7k | if (IsParameterPack && !Default.isInvalid()106 ) { |
1656 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1657 | 1 | Default = ParsedTemplateArgument(); |
1658 | 1 | } |
1659 | | |
1660 | 19.7k | if (!Default.isInvalid()) { |
1661 | | // Check only that we have a template template argument. We don't want to |
1662 | | // try to check well-formedness now, because our template template parameter |
1663 | | // might have dependent types in its template parameters, which we wouldn't |
1664 | | // be able to match now. |
1665 | | // |
1666 | | // If none of the template template parameter's template arguments mention |
1667 | | // other template parameters, we could actually perform more checking here. |
1668 | | // However, it isn't worth doing. |
1669 | 9.34k | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
1670 | 9.34k | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
1671 | 0 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
1672 | 0 | << DefaultArg.getSourceRange(); |
1673 | 0 | return Param; |
1674 | 0 | } |
1675 | | |
1676 | | // Check for unexpanded parameter packs. |
1677 | 9.34k | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
1678 | 9.34k | DefaultArg.getArgument().getAsTemplate(), |
1679 | 9.34k | UPPC_DefaultArgument)) |
1680 | 1 | return Param; |
1681 | | |
1682 | 9.34k | Param->setDefaultArgument(Context, DefaultArg); |
1683 | 9.34k | } |
1684 | | |
1685 | 19.7k | return Param; |
1686 | 19.7k | } |
1687 | | |
1688 | | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
1689 | | /// constrained by RequiresClause, that contains the template parameters in |
1690 | | /// Params. |
1691 | | TemplateParameterList * |
1692 | | Sema::ActOnTemplateParameterList(unsigned Depth, |
1693 | | SourceLocation ExportLoc, |
1694 | | SourceLocation TemplateLoc, |
1695 | | SourceLocation LAngleLoc, |
1696 | | ArrayRef<NamedDecl *> Params, |
1697 | | SourceLocation RAngleLoc, |
1698 | 1.63M | Expr *RequiresClause) { |
1699 | 1.63M | if (ExportLoc.isValid()) |
1700 | 3 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
1701 | | |
1702 | 1.63M | for (NamedDecl *P : Params) |
1703 | 2.93M | warnOnReservedIdentifier(P); |
1704 | | |
1705 | 1.63M | return TemplateParameterList::Create( |
1706 | 1.63M | Context, TemplateLoc, LAngleLoc, |
1707 | 1.63M | llvm::makeArrayRef(Params.data(), Params.size()), |
1708 | 1.63M | RAngleLoc, RequiresClause); |
1709 | 1.63M | } |
1710 | | |
1711 | | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
1712 | 593k | const CXXScopeSpec &SS) { |
1713 | 593k | if (SS.isSet()) |
1714 | 626 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
1715 | 593k | } |
1716 | | |
1717 | | DeclResult Sema::CheckClassTemplate( |
1718 | | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
1719 | | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
1720 | | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
1721 | | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
1722 | | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
1723 | 328k | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
1724 | 328k | assert(TemplateParams && TemplateParams->size() > 0 && |
1725 | 328k | "No template parameters"); |
1726 | 0 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
1727 | 0 | bool Invalid = false; |
1728 | | |
1729 | | // Check that we can declare a template here. |
1730 | 328k | if (CheckTemplateDeclScope(S, TemplateParams)) |
1731 | 7 | return true; |
1732 | | |
1733 | 328k | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
1734 | 328k | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
1735 | | |
1736 | | // There is no such thing as an unnamed class template. |
1737 | 328k | if (!Name) { |
1738 | 0 | Diag(KWLoc, diag::err_template_unnamed_class); |
1739 | 0 | return true; |
1740 | 0 | } |
1741 | | |
1742 | | // Find any previous declaration with this name. For a friend with no |
1743 | | // scope explicitly specified, we only look for tag declarations (per |
1744 | | // C++11 [basic.lookup.elab]p2). |
1745 | 328k | DeclContext *SemanticContext; |
1746 | 328k | LookupResult Previous(*this, Name, NameLoc, |
1747 | 328k | (SS.isEmpty() && TUK == TUK_Friend328k ) |
1748 | 328k | ? LookupTagName18.8k : LookupOrdinaryName309k , |
1749 | 328k | forRedeclarationInCurContext()); |
1750 | 328k | if (SS.isNotEmpty() && !SS.isInvalid()149 ) { |
1751 | 149 | SemanticContext = computeDeclContext(SS, true); |
1752 | 149 | if (!SemanticContext) { |
1753 | | // FIXME: Horrible, horrible hack! We can't currently represent this |
1754 | | // in the AST, and historically we have just ignored such friend |
1755 | | // class templates, so don't complain here. |
1756 | 2 | Diag(NameLoc, TUK == TUK_Friend |
1757 | 2 | ? diag::warn_template_qualified_friend_ignored1 |
1758 | 2 | : diag::err_template_qualified_declarator_no_match1 ) |
1759 | 2 | << SS.getScopeRep() << SS.getRange(); |
1760 | 2 | return TUK != TUK_Friend; |
1761 | 2 | } |
1762 | | |
1763 | 147 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
1764 | 0 | return true; |
1765 | | |
1766 | | // If we're adding a template to a dependent context, we may need to |
1767 | | // rebuilding some of the types used within the template parameter list, |
1768 | | // now that we know what the current instantiation is. |
1769 | 147 | if (SemanticContext->isDependentContext()) { |
1770 | 41 | ContextRAII SavedContext(*this, SemanticContext); |
1771 | 41 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
1772 | 0 | Invalid = true; |
1773 | 106 | } else if (TUK != TUK_Friend && TUK != TUK_Reference87 ) |
1774 | 87 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
1775 | | |
1776 | 147 | LookupQualifiedName(Previous, SemanticContext); |
1777 | 328k | } else { |
1778 | 328k | SemanticContext = CurContext; |
1779 | | |
1780 | | // C++14 [class.mem]p14: |
1781 | | // If T is the name of a class, then each of the following shall have a |
1782 | | // name different from T: |
1783 | | // -- every member template of class T |
1784 | 328k | if (TUK != TUK_Friend && |
1785 | 328k | DiagnoseClassNameShadow(SemanticContext, |
1786 | 309k | DeclarationNameInfo(Name, NameLoc))) |
1787 | 6 | return true; |
1788 | | |
1789 | 328k | LookupName(Previous, S); |
1790 | 328k | } |
1791 | | |
1792 | 328k | if (Previous.isAmbiguous()) |
1793 | 0 | return true; |
1794 | | |
1795 | 328k | NamedDecl *PrevDecl = nullptr; |
1796 | 328k | if (Previous.begin() != Previous.end()) |
1797 | 42.0k | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1798 | | |
1799 | 328k | if (PrevDecl && PrevDecl->isTemplateParameter()42.0k ) { |
1800 | | // Maybe we will complain about the shadowed template parameter. |
1801 | 7 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
1802 | | // Just pretend that we didn't see the previous declaration. |
1803 | 7 | PrevDecl = nullptr; |
1804 | 7 | } |
1805 | | |
1806 | | // If there is a previous declaration with the same name, check |
1807 | | // whether this is a valid redeclaration. |
1808 | 328k | ClassTemplateDecl *PrevClassTemplate = |
1809 | 328k | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
1810 | | |
1811 | | // We may have found the injected-class-name of a class template, |
1812 | | // class template partial specialization, or class template specialization. |
1813 | | // In these cases, grab the template that is being defined or specialized. |
1814 | 328k | if (!PrevClassTemplate && PrevDecl289k && isa<CXXRecordDecl>(PrevDecl)3.03k && |
1815 | 328k | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()3.02k ) { |
1816 | 3.01k | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
1817 | 3.01k | PrevClassTemplate |
1818 | 3.01k | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
1819 | 3.01k | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)25 ) { |
1820 | 25 | PrevClassTemplate |
1821 | 25 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
1822 | 25 | ->getSpecializedTemplate(); |
1823 | 25 | } |
1824 | 3.01k | } |
1825 | | |
1826 | 328k | if (TUK == TUK_Friend) { |
1827 | | // C++ [namespace.memdef]p3: |
1828 | | // [...] When looking for a prior declaration of a class or a function |
1829 | | // declared as a friend, and when the name of the friend class or |
1830 | | // function is neither a qualified name nor a template-id, scopes outside |
1831 | | // the innermost enclosing namespace scope are not considered. |
1832 | 18.8k | if (!SS.isSet()) { |
1833 | 18.8k | DeclContext *OutermostContext = CurContext; |
1834 | 37.7k | while (!OutermostContext->isFileContext()) |
1835 | 18.9k | OutermostContext = OutermostContext->getLookupParent(); |
1836 | | |
1837 | 18.8k | if (PrevDecl && |
1838 | 18.8k | (14.5k OutermostContext->Equals(PrevDecl->getDeclContext())14.5k || |
1839 | 14.5k | OutermostContext->Encloses(PrevDecl->getDeclContext())15 )) { |
1840 | 14.5k | SemanticContext = PrevDecl->getDeclContext(); |
1841 | 14.5k | } else { |
1842 | | // Declarations in outer scopes don't matter. However, the outermost |
1843 | | // context we computed is the semantic context for our new |
1844 | | // declaration. |
1845 | 4.27k | PrevDecl = PrevClassTemplate = nullptr; |
1846 | 4.27k | SemanticContext = OutermostContext; |
1847 | | |
1848 | | // Check that the chosen semantic context doesn't already contain a |
1849 | | // declaration of this name as a non-tag type. |
1850 | 4.27k | Previous.clear(LookupOrdinaryName); |
1851 | 4.27k | DeclContext *LookupContext = SemanticContext; |
1852 | 4.27k | while (LookupContext->isTransparentContext()) |
1853 | 0 | LookupContext = LookupContext->getLookupParent(); |
1854 | 4.27k | LookupQualifiedName(Previous, LookupContext); |
1855 | | |
1856 | 4.27k | if (Previous.isAmbiguous()) |
1857 | 0 | return true; |
1858 | | |
1859 | 4.27k | if (Previous.begin() != Previous.end()) |
1860 | 1 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1861 | 4.27k | } |
1862 | 18.8k | } |
1863 | 309k | } else if (PrevDecl && |
1864 | 309k | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
1865 | 27.3k | S, SS.isValid())) |
1866 | 49 | PrevDecl = PrevClassTemplate = nullptr; |
1867 | | |
1868 | 328k | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
1869 | 328k | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
1870 | 3 | if (SS.isEmpty() && |
1871 | 3 | !(PrevClassTemplate && |
1872 | 3 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
1873 | 3 | SemanticContext->getRedeclContext()))) { |
1874 | 2 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
1875 | 2 | Diag(Shadow->getTargetDecl()->getLocation(), |
1876 | 2 | diag::note_using_decl_target); |
1877 | 2 | Diag(Shadow->getIntroducer()->getLocation(), diag::note_using_decl) << 0; |
1878 | | // Recover by ignoring the old declaration. |
1879 | 2 | PrevDecl = PrevClassTemplate = nullptr; |
1880 | 2 | } |
1881 | 3 | } |
1882 | | |
1883 | 328k | if (PrevClassTemplate) { |
1884 | | // Ensure that the template parameter lists are compatible. Skip this check |
1885 | | // for a friend in a dependent context: the template parameter list itself |
1886 | | // could be dependent. |
1887 | 41.9k | if (!(TUK == TUK_Friend && CurContext->isDependentContext()14.6k ) && |
1888 | 41.9k | !TemplateParameterListsAreEqual(TemplateParams, |
1889 | 27.6k | PrevClassTemplate->getTemplateParameters(), |
1890 | 27.6k | /*Complain=*/true, |
1891 | 27.6k | TPL_TemplateMatch)) |
1892 | 34 | return true; |
1893 | | |
1894 | | // C++ [temp.class]p4: |
1895 | | // In a redeclaration, partial specialization, explicit |
1896 | | // specialization or explicit instantiation of a class template, |
1897 | | // the class-key shall agree in kind with the original class |
1898 | | // template declaration (7.1.5.3). |
1899 | 41.9k | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
1900 | 41.9k | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
1901 | 41.9k | TUK == TUK_Definition, KWLoc, Name)) { |
1902 | 0 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
1903 | 0 | << Name |
1904 | 0 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
1905 | 0 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
1906 | 0 | Kind = PrevRecordDecl->getTagKind(); |
1907 | 0 | } |
1908 | | |
1909 | | // Check for redefinition of this class template. |
1910 | 41.9k | if (TUK == TUK_Definition) { |
1911 | 17.5k | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
1912 | | // If we have a prior definition that is not visible, treat this as |
1913 | | // simply making that previous definition visible. |
1914 | 114 | NamedDecl *Hidden = nullptr; |
1915 | 114 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)113 ) { |
1916 | 111 | SkipBody->ShouldSkip = true; |
1917 | 111 | SkipBody->Previous = Def; |
1918 | 111 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
1919 | 111 | assert(Tmpl && "original definition of a class template is not a " |
1920 | 111 | "class template?"); |
1921 | 0 | makeMergedDefinitionVisible(Hidden); |
1922 | 111 | makeMergedDefinitionVisible(Tmpl); |
1923 | 111 | } else { |
1924 | 3 | Diag(NameLoc, diag::err_redefinition) << Name; |
1925 | 3 | Diag(Def->getLocation(), diag::note_previous_definition); |
1926 | | // FIXME: Would it make sense to try to "forget" the previous |
1927 | | // definition, as part of error recovery? |
1928 | 3 | return true; |
1929 | 3 | } |
1930 | 114 | } |
1931 | 17.5k | } |
1932 | 286k | } else if (PrevDecl) { |
1933 | | // C++ [temp]p5: |
1934 | | // A class template shall not have the same name as any other |
1935 | | // template, class, function, object, enumeration, enumerator, |
1936 | | // namespace, or type in the same scope (3.3), except as specified |
1937 | | // in (14.5.4). |
1938 | 6 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
1939 | 6 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
1940 | 6 | return true; |
1941 | 6 | } |
1942 | | |
1943 | | // Check the template parameter list of this declaration, possibly |
1944 | | // merging in the template parameter list from the previous class |
1945 | | // template declaration. Skip this check for a friend in a dependent |
1946 | | // context, because the template parameter list might be dependent. |
1947 | 328k | if (!(TUK == TUK_Friend && CurContext->isDependentContext()18.8k ) && |
1948 | 328k | CheckTemplateParameterList( |
1949 | 309k | TemplateParams, |
1950 | 309k | PrevClassTemplate |
1951 | 309k | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters()27.5k |
1952 | 309k | : nullptr281k , |
1953 | 309k | (SS.isSet() && SemanticContext130 && SemanticContext->isRecord()130 && |
1954 | 309k | SemanticContext->isDependentContext()112 ) |
1955 | 309k | ? TPC_ClassTemplateMember33 |
1956 | 309k | : TUK == TUK_Friend309k ? TPC_FriendClassTemplate349 : TPC_ClassTemplate309k , |
1957 | 309k | SkipBody)) |
1958 | 22 | Invalid = true; |
1959 | | |
1960 | 328k | if (SS.isSet()) { |
1961 | | // If the name of the template was qualified, we must be defining the |
1962 | | // template out-of-line. |
1963 | 144 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
1964 | 3 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match1 |
1965 | 3 | : diag::err_member_decl_does_not_match2 ) |
1966 | 3 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
1967 | 3 | Invalid = true; |
1968 | 3 | } |
1969 | 144 | } |
1970 | | |
1971 | | // If this is a templated friend in a dependent context we should not put it |
1972 | | // on the redecl chain. In some cases, the templated friend can be the most |
1973 | | // recent declaration tricking the template instantiator to make substitutions |
1974 | | // there. |
1975 | | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
1976 | 328k | bool ShouldAddRedecl |
1977 | 328k | = !(TUK == TUK_Friend && CurContext->isDependentContext()18.8k ); |
1978 | | |
1979 | 328k | CXXRecordDecl *NewClass = |
1980 | 328k | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
1981 | 328k | PrevClassTemplate && ShouldAddRedecl41.9k ? |
1982 | 300k | PrevClassTemplate->getTemplatedDecl()27.5k : nullptr, |
1983 | 328k | /*DelayTypeCreation=*/true); |
1984 | 328k | SetNestedNameSpecifier(*this, NewClass, SS); |
1985 | 328k | if (NumOuterTemplateParamLists > 0) |
1986 | 76 | NewClass->setTemplateParameterListsInfo( |
1987 | 76 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
1988 | 76 | NumOuterTemplateParamLists)); |
1989 | | |
1990 | | // Add alignment attributes if necessary; these attributes are checked when |
1991 | | // the ASTContext lays out the structure. |
1992 | 328k | if (TUK == TUK_Definition && (250k !SkipBody250k || !SkipBody->ShouldSkip250k )) { |
1993 | 250k | AddAlignmentAttributesForRecord(NewClass); |
1994 | 250k | AddMsStructLayoutForRecord(NewClass); |
1995 | 250k | } |
1996 | | |
1997 | 328k | ClassTemplateDecl *NewTemplate |
1998 | 328k | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
1999 | 328k | DeclarationName(Name), TemplateParams, |
2000 | 328k | NewClass); |
2001 | | |
2002 | 328k | if (ShouldAddRedecl) |
2003 | 309k | NewTemplate->setPreviousDecl(PrevClassTemplate); |
2004 | | |
2005 | 328k | NewClass->setDescribedClassTemplate(NewTemplate); |
2006 | | |
2007 | 328k | if (ModulePrivateLoc.isValid()) |
2008 | 3 | NewTemplate->setModulePrivate(); |
2009 | | |
2010 | | // Build the type for the class template declaration now. |
2011 | 328k | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
2012 | 328k | T = Context.getInjectedClassNameType(NewClass, T); |
2013 | 328k | assert(T->isDependentType() && "Class template type is not dependent?"); |
2014 | 0 | (void)T; |
2015 | | |
2016 | | // If we are providing an explicit specialization of a member that is a |
2017 | | // class template, make a note of that. |
2018 | 328k | if (PrevClassTemplate && |
2019 | 328k | PrevClassTemplate->getInstantiatedFromMemberTemplate()41.9k ) |
2020 | 39 | PrevClassTemplate->setMemberSpecialization(); |
2021 | | |
2022 | | // Set the access specifier. |
2023 | 328k | if (!Invalid && TUK != TUK_Friend328k && NewTemplate->getDeclContext()->isRecord()309k ) |
2024 | 11.1k | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
2025 | | |
2026 | | // Set the lexical context of these templates |
2027 | 328k | NewClass->setLexicalDeclContext(CurContext); |
2028 | 328k | NewTemplate->setLexicalDeclContext(CurContext); |
2029 | | |
2030 | 328k | if (TUK == TUK_Definition && (250k !SkipBody250k || !SkipBody->ShouldSkip250k )) |
2031 | 250k | NewClass->startDefinition(); |
2032 | | |
2033 | 328k | ProcessDeclAttributeList(S, NewClass, Attr); |
2034 | | |
2035 | 328k | if (PrevClassTemplate) |
2036 | 41.9k | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
2037 | | |
2038 | 328k | AddPushedVisibilityAttribute(NewClass); |
2039 | 328k | inferGslOwnerPointerAttribute(NewClass); |
2040 | | |
2041 | 328k | if (TUK != TUK_Friend) { |
2042 | | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
2043 | 309k | Scope *Outer = S; |
2044 | 618k | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
2045 | 309k | Outer = Outer->getParent(); |
2046 | 309k | PushOnScopeChains(NewTemplate, Outer); |
2047 | 309k | } else { |
2048 | 18.8k | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none14.6k ) { |
2049 | 27 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
2050 | 27 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
2051 | 27 | } |
2052 | | |
2053 | 18.8k | NewTemplate->setObjectOfFriendDecl(); |
2054 | | |
2055 | | // Friend templates are visible in fairly strange ways. |
2056 | 18.8k | if (!CurContext->isDependentContext()) { |
2057 | 349 | DeclContext *DC = SemanticContext->getRedeclContext(); |
2058 | 349 | DC->makeDeclVisibleInContext(NewTemplate); |
2059 | 349 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
2060 | 338 | PushOnScopeChains(NewTemplate, EnclosingScope, |
2061 | 338 | /* AddToContext = */ false); |
2062 | 349 | } |
2063 | | |
2064 | 18.8k | FriendDecl *Friend = FriendDecl::Create( |
2065 | 18.8k | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
2066 | 18.8k | Friend->setAccess(AS_public); |
2067 | 18.8k | CurContext->addDecl(Friend); |
2068 | 18.8k | } |
2069 | | |
2070 | 328k | if (PrevClassTemplate) |
2071 | 41.9k | CheckRedeclarationInModule(NewTemplate, PrevClassTemplate); |
2072 | | |
2073 | 328k | if (Invalid) { |
2074 | 25 | NewTemplate->setInvalidDecl(); |
2075 | 25 | NewClass->setInvalidDecl(); |
2076 | 25 | } |
2077 | | |
2078 | 328k | ActOnDocumentableDecl(NewTemplate); |
2079 | | |
2080 | 328k | if (SkipBody && SkipBody->ShouldSkip309k ) |
2081 | 111 | return SkipBody->Previous; |
2082 | | |
2083 | 328k | return NewTemplate; |
2084 | 328k | } |
2085 | | |
2086 | | namespace { |
2087 | | /// Tree transform to "extract" a transformed type from a class template's |
2088 | | /// constructor to a deduction guide. |
2089 | | class ExtractTypeForDeductionGuide |
2090 | | : public TreeTransform<ExtractTypeForDeductionGuide> { |
2091 | | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs; |
2092 | | |
2093 | | public: |
2094 | | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
2095 | | ExtractTypeForDeductionGuide( |
2096 | | Sema &SemaRef, |
2097 | | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) |
2098 | 6.08k | : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs) {} |
2099 | | |
2100 | 6.08k | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
2101 | | |
2102 | 10.6k | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
2103 | 10.6k | ASTContext &Context = SemaRef.getASTContext(); |
2104 | 10.6k | TypedefNameDecl *OrigDecl = TL.getTypedefNameDecl(); |
2105 | 10.6k | TypedefNameDecl *Decl = OrigDecl; |
2106 | | // Transform the underlying type of the typedef and clone the Decl only if |
2107 | | // the typedef has a dependent context. |
2108 | 10.6k | if (OrigDecl->getDeclContext()->isDependentContext()) { |
2109 | 10.6k | TypeLocBuilder InnerTLB; |
2110 | 10.6k | QualType Transformed = |
2111 | 10.6k | TransformType(InnerTLB, OrigDecl->getTypeSourceInfo()->getTypeLoc()); |
2112 | 10.6k | TypeSourceInfo *TSI = InnerTLB.getTypeSourceInfo(Context, Transformed); |
2113 | 10.6k | if (isa<TypeAliasDecl>(OrigDecl)) |
2114 | 15 | Decl = TypeAliasDecl::Create( |
2115 | 15 | Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(), |
2116 | 15 | OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI); |
2117 | 10.5k | else { |
2118 | 10.5k | assert(isa<TypedefDecl>(OrigDecl) && "Not a Type alias or typedef"); |
2119 | 0 | Decl = TypedefDecl::Create( |
2120 | 10.5k | Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(), |
2121 | 10.5k | OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI); |
2122 | 10.5k | } |
2123 | 0 | MaterializedTypedefs.push_back(Decl); |
2124 | 10.6k | } |
2125 | | |
2126 | 0 | QualType TDTy = Context.getTypedefType(Decl); |
2127 | 10.6k | TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(TDTy); |
2128 | 10.6k | TypedefTL.setNameLoc(TL.getNameLoc()); |
2129 | | |
2130 | 10.6k | return TDTy; |
2131 | 10.6k | } |
2132 | | }; |
2133 | | |
2134 | | /// Transform to convert portions of a constructor declaration into the |
2135 | | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
2136 | | struct ConvertConstructorToDeductionGuideTransform { |
2137 | | ConvertConstructorToDeductionGuideTransform(Sema &S, |
2138 | | ClassTemplateDecl *Template) |
2139 | 2.00k | : SemaRef(S), Template(Template) {} |
2140 | | |
2141 | | Sema &SemaRef; |
2142 | | ClassTemplateDecl *Template; |
2143 | | |
2144 | | DeclContext *DC = Template->getDeclContext(); |
2145 | | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
2146 | | DeclarationName DeductionGuideName = |
2147 | | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
2148 | | |
2149 | | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
2150 | | |
2151 | | // Index adjustment to apply to convert depth-1 template parameters into |
2152 | | // depth-0 template parameters. |
2153 | | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
2154 | | |
2155 | | /// Transform a constructor declaration into a deduction guide. |
2156 | | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
2157 | 3.12k | CXXConstructorDecl *CD) { |
2158 | 3.12k | SmallVector<TemplateArgument, 16> SubstArgs; |
2159 | | |
2160 | 3.12k | LocalInstantiationScope Scope(SemaRef); |
2161 | | |
2162 | | // C++ [over.match.class.deduct]p1: |
2163 | | // -- For each constructor of the class template designated by the |
2164 | | // template-name, a function template with the following properties: |
2165 | | |
2166 | | // -- The template parameters are the template parameters of the class |
2167 | | // template followed by the template parameters (including default |
2168 | | // template arguments) of the constructor, if any. |
2169 | 3.12k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2170 | 3.12k | if (FTD) { |
2171 | 1.50k | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
2172 | 1.50k | SmallVector<NamedDecl *, 16> AllParams; |
2173 | 1.50k | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
2174 | 1.50k | AllParams.insert(AllParams.begin(), |
2175 | 1.50k | TemplateParams->begin(), TemplateParams->end()); |
2176 | 1.50k | SubstArgs.reserve(InnerParams->size()); |
2177 | | |
2178 | | // Later template parameters could refer to earlier ones, so build up |
2179 | | // a list of substituted template arguments as we go. |
2180 | 3.16k | for (NamedDecl *Param : *InnerParams) { |
2181 | 3.16k | MultiLevelTemplateArgumentList Args; |
2182 | 3.16k | Args.setKind(TemplateSubstitutionKind::Rewrite); |
2183 | 3.16k | Args.addOuterTemplateArguments(SubstArgs); |
2184 | 3.16k | Args.addOuterRetainedLevel(); |
2185 | 3.16k | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
2186 | 3.16k | if (!NewParam) |
2187 | 0 | return nullptr; |
2188 | 3.16k | AllParams.push_back(NewParam); |
2189 | 3.16k | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
2190 | 3.16k | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
2191 | 3.16k | } |
2192 | | |
2193 | | // Substitute new template parameters into requires-clause if present. |
2194 | 1.50k | Expr *RequiresClause = nullptr; |
2195 | 1.50k | if (Expr *InnerRC = InnerParams->getRequiresClause()) { |
2196 | 35 | MultiLevelTemplateArgumentList Args; |
2197 | 35 | Args.setKind(TemplateSubstitutionKind::Rewrite); |
2198 | 35 | Args.addOuterTemplateArguments(SubstArgs); |
2199 | 35 | Args.addOuterRetainedLevel(); |
2200 | 35 | ExprResult E = SemaRef.SubstExpr(InnerRC, Args); |
2201 | 35 | if (E.isInvalid()) |
2202 | 0 | return nullptr; |
2203 | 35 | RequiresClause = E.getAs<Expr>(); |
2204 | 35 | } |
2205 | | |
2206 | 1.50k | TemplateParams = TemplateParameterList::Create( |
2207 | 1.50k | SemaRef.Context, InnerParams->getTemplateLoc(), |
2208 | 1.50k | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
2209 | 1.50k | RequiresClause); |
2210 | 1.50k | } |
2211 | | |
2212 | | // If we built a new template-parameter-list, track that we need to |
2213 | | // substitute references to the old parameters into references to the |
2214 | | // new ones. |
2215 | 3.12k | MultiLevelTemplateArgumentList Args; |
2216 | 3.12k | Args.setKind(TemplateSubstitutionKind::Rewrite); |
2217 | 3.12k | if (FTD) { |
2218 | 1.50k | Args.addOuterTemplateArguments(SubstArgs); |
2219 | 1.50k | Args.addOuterRetainedLevel(); |
2220 | 1.50k | } |
2221 | | |
2222 | 3.12k | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
2223 | 3.12k | .getAsAdjusted<FunctionProtoTypeLoc>(); |
2224 | 3.12k | assert(FPTL && "no prototype for constructor declaration"); |
2225 | | |
2226 | | // Transform the type of the function, adjusting the return type and |
2227 | | // replacing references to the old parameters with references to the |
2228 | | // new ones. |
2229 | 0 | TypeLocBuilder TLB; |
2230 | 3.12k | SmallVector<ParmVarDecl*, 8> Params; |
2231 | 3.12k | SmallVector<TypedefNameDecl *, 4> MaterializedTypedefs; |
2232 | 3.12k | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args, |
2233 | 3.12k | MaterializedTypedefs); |
2234 | 3.12k | if (NewType.isNull()) |
2235 | 0 | return nullptr; |
2236 | 3.12k | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
2237 | | |
2238 | 3.12k | return buildDeductionGuide(TemplateParams, CD, CD->getExplicitSpecifier(), |
2239 | 3.12k | NewTInfo, CD->getBeginLoc(), CD->getLocation(), |
2240 | 3.12k | CD->getEndLoc(), MaterializedTypedefs); |
2241 | 3.12k | } |
2242 | | |
2243 | | /// Build a deduction guide with the specified parameter types. |
2244 | 547 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
2245 | 547 | SourceLocation Loc = Template->getLocation(); |
2246 | | |
2247 | | // Build the requested type. |
2248 | 547 | FunctionProtoType::ExtProtoInfo EPI; |
2249 | 547 | EPI.HasTrailingReturn = true; |
2250 | 547 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
2251 | 547 | DeductionGuideName, EPI); |
2252 | 547 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
2253 | | |
2254 | 547 | FunctionProtoTypeLoc FPTL = |
2255 | 547 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
2256 | | |
2257 | | // Build the parameters, needed during deduction / substitution. |
2258 | 547 | SmallVector<ParmVarDecl*, 4> Params; |
2259 | 547 | for (auto T : ParamTypes) { |
2260 | 468 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
2261 | 468 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
2262 | 468 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
2263 | 468 | NewParam->setScopeInfo(0, Params.size()); |
2264 | 468 | FPTL.setParam(Params.size(), NewParam); |
2265 | 468 | Params.push_back(NewParam); |
2266 | 468 | } |
2267 | | |
2268 | 547 | return buildDeductionGuide(Template->getTemplateParameters(), nullptr, |
2269 | 547 | ExplicitSpecifier(), TSI, Loc, Loc, Loc); |
2270 | 547 | } |
2271 | | |
2272 | | private: |
2273 | | /// Transform a constructor template parameter into a deduction guide template |
2274 | | /// parameter, rebuilding any internal references to earlier parameters and |
2275 | | /// renumbering as we go. |
2276 | | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
2277 | 3.16k | MultiLevelTemplateArgumentList &Args) { |
2278 | 3.16k | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
2279 | | // TemplateTypeParmDecl's index cannot be changed after creation, so |
2280 | | // substitute it directly. |
2281 | 2.11k | auto *NewTTP = TemplateTypeParmDecl::Create( |
2282 | 2.11k | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
2283 | 2.11k | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), |
2284 | 2.11k | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
2285 | 2.11k | TTP->isParameterPack(), TTP->hasTypeConstraint(), |
2286 | 2.11k | TTP->isExpandedParameterPack() ? |
2287 | 2.11k | llvm::Optional<unsigned>(TTP->getNumExpansionParameters())0 : None); |
2288 | 2.11k | if (const auto *TC = TTP->getTypeConstraint()) |
2289 | 73 | SemaRef.SubstTypeConstraint(NewTTP, TC, Args); |
2290 | 2.11k | if (TTP->hasDefaultArgument()) { |
2291 | 313 | TypeSourceInfo *InstantiatedDefaultArg = |
2292 | 313 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
2293 | 313 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
2294 | 313 | if (InstantiatedDefaultArg) |
2295 | 313 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
2296 | 313 | } |
2297 | 2.11k | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
2298 | 2.11k | NewTTP); |
2299 | 2.11k | return NewTTP; |
2300 | 2.11k | } |
2301 | | |
2302 | 1.04k | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
2303 | 284 | return transformTemplateParameterImpl(TTP, Args); |
2304 | | |
2305 | 764 | return transformTemplateParameterImpl( |
2306 | 764 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
2307 | 1.04k | } |
2308 | | template<typename TemplateParmDecl> |
2309 | | TemplateParmDecl * |
2310 | | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
2311 | 1.04k | MultiLevelTemplateArgumentList &Args) { |
2312 | | // Ask the template instantiator to do the heavy lifting for us, then adjust |
2313 | | // the index of the parameter once it's done. |
2314 | 1.04k | auto *NewParam = |
2315 | 1.04k | cast<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
2316 | 1.04k | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
2317 | 0 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
2318 | 1.04k | return NewParam; |
2319 | 1.04k | } SemaTemplate.cpp:clang::TemplateTemplateParmDecl* (anonymous namespace)::ConvertConstructorToDeductionGuideTransform::transformTemplateParameterImpl<clang::TemplateTemplateParmDecl>(clang::TemplateTemplateParmDecl*, clang::MultiLevelTemplateArgumentList&) Line | Count | Source | 2311 | 284 | MultiLevelTemplateArgumentList &Args) { | 2312 | | // Ask the template instantiator to do the heavy lifting for us, then adjust | 2313 | | // the index of the parameter once it's done. | 2314 | 284 | auto *NewParam = | 2315 | 284 | cast<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); | 2316 | 284 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); | 2317 | 0 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); | 2318 | 284 | return NewParam; | 2319 | 284 | } |
SemaTemplate.cpp:clang::NonTypeTemplateParmDecl* (anonymous namespace)::ConvertConstructorToDeductionGuideTransform::transformTemplateParameterImpl<clang::NonTypeTemplateParmDecl>(clang::NonTypeTemplateParmDecl*, clang::MultiLevelTemplateArgumentList&) Line | Count | Source | 2311 | 764 | MultiLevelTemplateArgumentList &Args) { | 2312 | | // Ask the template instantiator to do the heavy lifting for us, then adjust | 2313 | | // the index of the parameter once it's done. | 2314 | 764 | auto *NewParam = | 2315 | 764 | cast<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); | 2316 | 764 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); | 2317 | 0 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); | 2318 | 764 | return NewParam; | 2319 | 764 | } |
|
2320 | | |
2321 | | QualType transformFunctionProtoType( |
2322 | | TypeLocBuilder &TLB, FunctionProtoTypeLoc TL, |
2323 | | SmallVectorImpl<ParmVarDecl *> &Params, |
2324 | | MultiLevelTemplateArgumentList &Args, |
2325 | 3.12k | SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) { |
2326 | 3.12k | SmallVector<QualType, 4> ParamTypes; |
2327 | 3.12k | const FunctionProtoType *T = TL.getTypePtr(); |
2328 | | |
2329 | | // -- The types of the function parameters are those of the constructor. |
2330 | 6.08k | for (auto *OldParam : TL.getParams()) { |
2331 | 6.08k | ParmVarDecl *NewParam = |
2332 | 6.08k | transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs); |
2333 | 6.08k | if (!NewParam) |
2334 | 0 | return QualType(); |
2335 | 6.08k | ParamTypes.push_back(NewParam->getType()); |
2336 | 6.08k | Params.push_back(NewParam); |
2337 | 6.08k | } |
2338 | | |
2339 | | // -- The return type is the class template specialization designated by |
2340 | | // the template-name and template arguments corresponding to the |
2341 | | // template parameters obtained from the class template. |
2342 | | // |
2343 | | // We use the injected-class-name type of the primary template instead. |
2344 | | // This has the convenient property that it is different from any type that |
2345 | | // the user can write in a deduction-guide (because they cannot enter the |
2346 | | // context of the template), so implicit deduction guides can never collide |
2347 | | // with explicit ones. |
2348 | 3.12k | QualType ReturnType = DeducedType; |
2349 | 3.12k | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
2350 | | |
2351 | | // Resolving a wording defect, we also inherit the variadicness of the |
2352 | | // constructor. |
2353 | 3.12k | FunctionProtoType::ExtProtoInfo EPI; |
2354 | 3.12k | EPI.Variadic = T->isVariadic(); |
2355 | 3.12k | EPI.HasTrailingReturn = true; |
2356 | | |
2357 | 3.12k | QualType Result = SemaRef.BuildFunctionType( |
2358 | 3.12k | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
2359 | 3.12k | if (Result.isNull()) |
2360 | 0 | return QualType(); |
2361 | | |
2362 | 3.12k | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
2363 | 3.12k | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
2364 | 3.12k | NewTL.setLParenLoc(TL.getLParenLoc()); |
2365 | 3.12k | NewTL.setRParenLoc(TL.getRParenLoc()); |
2366 | 3.12k | NewTL.setExceptionSpecRange(SourceRange()); |
2367 | 3.12k | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
2368 | 9.21k | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I6.08k ) |
2369 | 6.08k | NewTL.setParam(I, Params[I]); |
2370 | | |
2371 | 3.12k | return Result; |
2372 | 3.12k | } |
2373 | | |
2374 | | ParmVarDecl *transformFunctionTypeParam( |
2375 | | ParmVarDecl *OldParam, MultiLevelTemplateArgumentList &Args, |
2376 | 6.08k | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) { |
2377 | 6.08k | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
2378 | 6.08k | TypeSourceInfo *NewDI; |
2379 | 6.08k | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
2380 | | // Expand out the one and only element in each inner pack. |
2381 | 150 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
2382 | 150 | NewDI = |
2383 | 150 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
2384 | 150 | OldParam->getLocation(), OldParam->getDeclName()); |
2385 | 150 | if (!NewDI) return nullptr0 ; |
2386 | 150 | NewDI = |
2387 | 150 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
2388 | 150 | PackTL.getTypePtr()->getNumExpansions()); |
2389 | 150 | } else |
2390 | 5.93k | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
2391 | 5.93k | OldParam->getDeclName()); |
2392 | 6.08k | if (!NewDI) |
2393 | 0 | return nullptr; |
2394 | | |
2395 | | // Extract the type. This (for instance) replaces references to typedef |
2396 | | // members of the current instantiations with the definitions of those |
2397 | | // typedefs, avoiding triggering instantiation of the deduced type during |
2398 | | // deduction. |
2399 | 6.08k | NewDI = ExtractTypeForDeductionGuide(SemaRef, MaterializedTypedefs) |
2400 | 6.08k | .transform(NewDI); |
2401 | | |
2402 | | // Resolving a wording defect, we also inherit default arguments from the |
2403 | | // constructor. |
2404 | 6.08k | ExprResult NewDefArg; |
2405 | 6.08k | if (OldParam->hasDefaultArg()) { |
2406 | | // We don't care what the value is (we won't use it); just create a |
2407 | | // placeholder to indicate there is a default argument. |
2408 | 463 | QualType ParamTy = NewDI->getType(); |
2409 | 463 | NewDefArg = new (SemaRef.Context) |
2410 | 463 | OpaqueValueExpr(OldParam->getDefaultArg()->getBeginLoc(), |
2411 | 463 | ParamTy.getNonLValueExprType(SemaRef.Context), |
2412 | 463 | ParamTy->isLValueReferenceType() ? VK_LValue264 |
2413 | 463 | : ParamTy->isRValueReferenceType()199 ? VK_XValue0 |
2414 | 199 | : VK_PRValue); |
2415 | 463 | } |
2416 | | |
2417 | 6.08k | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
2418 | 6.08k | OldParam->getInnerLocStart(), |
2419 | 6.08k | OldParam->getLocation(), |
2420 | 6.08k | OldParam->getIdentifier(), |
2421 | 6.08k | NewDI->getType(), |
2422 | 6.08k | NewDI, |
2423 | 6.08k | OldParam->getStorageClass(), |
2424 | 6.08k | NewDefArg.get()); |
2425 | 6.08k | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
2426 | 6.08k | OldParam->getFunctionScopeIndex()); |
2427 | 6.08k | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
2428 | 6.08k | return NewParam; |
2429 | 6.08k | } |
2430 | | |
2431 | | FunctionTemplateDecl *buildDeductionGuide( |
2432 | | TemplateParameterList *TemplateParams, CXXConstructorDecl *Ctor, |
2433 | | ExplicitSpecifier ES, TypeSourceInfo *TInfo, SourceLocation LocStart, |
2434 | | SourceLocation Loc, SourceLocation LocEnd, |
2435 | 3.67k | llvm::ArrayRef<TypedefNameDecl *> MaterializedTypedefs = {}) { |
2436 | 3.67k | DeclarationNameInfo Name(DeductionGuideName, Loc); |
2437 | 3.67k | ArrayRef<ParmVarDecl *> Params = |
2438 | 3.67k | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
2439 | | |
2440 | | // Build the implicit deduction guide template. |
2441 | 3.67k | auto *Guide = |
2442 | 3.67k | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, ES, Name, |
2443 | 3.67k | TInfo->getType(), TInfo, LocEnd, Ctor); |
2444 | 3.67k | Guide->setImplicit(); |
2445 | 3.67k | Guide->setParams(Params); |
2446 | | |
2447 | 3.67k | for (auto *Param : Params) |
2448 | 6.55k | Param->setDeclContext(Guide); |
2449 | 3.67k | for (auto *TD : MaterializedTypedefs) |
2450 | 10.6k | TD->setDeclContext(Guide); |
2451 | | |
2452 | 3.67k | auto *GuideTemplate = FunctionTemplateDecl::Create( |
2453 | 3.67k | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
2454 | 3.67k | GuideTemplate->setImplicit(); |
2455 | 3.67k | Guide->setDescribedFunctionTemplate(GuideTemplate); |
2456 | | |
2457 | 3.67k | if (isa<CXXRecordDecl>(DC)) { |
2458 | 30 | Guide->setAccess(AS_public); |
2459 | 30 | GuideTemplate->setAccess(AS_public); |
2460 | 30 | } |
2461 | | |
2462 | 3.67k | DC->addDecl(GuideTemplate); |
2463 | 3.67k | return GuideTemplate; |
2464 | 3.67k | } |
2465 | | }; |
2466 | | } |
2467 | | |
2468 | | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
2469 | 2.00k | SourceLocation Loc) { |
2470 | 2.00k | if (CXXRecordDecl *DefRecord = |
2471 | 2.00k | cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) { |
2472 | 1.93k | TemplateDecl *DescribedTemplate = DefRecord->getDescribedClassTemplate(); |
2473 | 1.93k | Template = DescribedTemplate ? DescribedTemplate : Template0 ; |
2474 | 1.93k | } |
2475 | | |
2476 | 2.00k | DeclContext *DC = Template->getDeclContext(); |
2477 | 2.00k | if (DC->isDependentContext()) |
2478 | 4 | return; |
2479 | | |
2480 | 2.00k | ConvertConstructorToDeductionGuideTransform Transform( |
2481 | 2.00k | *this, cast<ClassTemplateDecl>(Template)); |
2482 | 2.00k | if (!isCompleteType(Loc, Transform.DeducedType)) |
2483 | 0 | return; |
2484 | | |
2485 | | // Check whether we've already declared deduction guides for this template. |
2486 | | // FIXME: Consider storing a flag on the template to indicate this. |
2487 | 2.00k | auto Existing = DC->lookup(Transform.DeductionGuideName); |
2488 | 2.00k | for (auto *D : Existing) |
2489 | 1.53k | if (D->isImplicit()) |
2490 | 1.53k | return; |
2491 | | |
2492 | | // In case we were expanding a pack when we attempted to declare deduction |
2493 | | // guides, turn off pack expansion for everything we're about to do. |
2494 | 469 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
2495 | | // Create a template instantiation record to track the "instantiation" of |
2496 | | // constructors into deduction guides. |
2497 | | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
2498 | | // this substitution process actually fail? |
2499 | 469 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
2500 | 469 | if (BuildingDeductionGuides.isInvalid()) |
2501 | 1 | return; |
2502 | | |
2503 | | // Convert declared constructors into deduction guide templates. |
2504 | | // FIXME: Skip constructors for which deduction must necessarily fail (those |
2505 | | // for which some class template parameter without a default argument never |
2506 | | // appears in a deduced context). |
2507 | 468 | bool AddedAny = false; |
2508 | 3.13k | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
2509 | 3.13k | D = D->getUnderlyingDecl(); |
2510 | 3.13k | if (D->isInvalidDecl() || D->isImplicit()3.13k ) |
2511 | 1 | continue; |
2512 | 3.13k | D = cast<NamedDecl>(D->getCanonicalDecl()); |
2513 | | |
2514 | 3.13k | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
2515 | 3.13k | auto *CD = |
2516 | 3.13k | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl()1.50k : D1.62k ); |
2517 | | // Class-scope explicit specializations (MS extension) do not result in |
2518 | | // deduction guides. |
2519 | 3.13k | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization()1.62k )) |
2520 | 0 | continue; |
2521 | | |
2522 | | // Cannot make a deduction guide when unparsed arguments are present. |
2523 | 6.08k | if (3.13k std::any_of(CD->param_begin(), CD->param_end(), [](ParmVarDecl *P) 3.13k { |
2524 | 6.08k | return !P || P->hasUnparsedDefaultArg(); |
2525 | 6.08k | })) |
2526 | 1 | continue; |
2527 | | |
2528 | 3.12k | Transform.transformConstructor(FTD, CD); |
2529 | 3.12k | AddedAny = true; |
2530 | 3.12k | } |
2531 | | |
2532 | | // C++17 [over.match.class.deduct] |
2533 | | // -- If C is not defined or does not declare any constructors, an |
2534 | | // additional function template derived as above from a hypothetical |
2535 | | // constructor C(). |
2536 | 468 | if (!AddedAny) |
2537 | 79 | Transform.buildSimpleDeductionGuide(None); |
2538 | | |
2539 | | // -- An additional function template derived as above from a hypothetical |
2540 | | // constructor C(C), called the copy deduction candidate. |
2541 | 468 | cast<CXXDeductionGuideDecl>( |
2542 | 468 | cast<FunctionTemplateDecl>( |
2543 | 468 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
2544 | 468 | ->getTemplatedDecl()) |
2545 | 468 | ->setIsCopyDeductionCandidate(); |
2546 | 468 | } |
2547 | | |
2548 | | /// Diagnose the presence of a default template argument on a |
2549 | | /// template parameter, which is ill-formed in certain contexts. |
2550 | | /// |
2551 | | /// \returns true if the default template argument should be dropped. |
2552 | | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
2553 | | Sema::TemplateParamListContext TPC, |
2554 | | SourceLocation ParamLoc, |
2555 | 216k | SourceRange DefArgRange) { |
2556 | 216k | switch (TPC) { |
2557 | 85.5k | case Sema::TPC_ClassTemplate: |
2558 | 85.6k | case Sema::TPC_VarTemplate: |
2559 | 99.6k | case Sema::TPC_TypeAliasTemplate: |
2560 | 99.6k | return false; |
2561 | | |
2562 | 116k | case Sema::TPC_FunctionTemplate: |
2563 | 116k | case Sema::TPC_FriendFunctionTemplateDefinition: |
2564 | | // C++ [temp.param]p9: |
2565 | | // A default template-argument shall not be specified in a |
2566 | | // function template declaration or a function template |
2567 | | // definition [...] |
2568 | | // If a friend function template declaration specifies a default |
2569 | | // template-argument, that declaration shall be a definition and shall be |
2570 | | // the only declaration of the function template in the translation unit. |
2571 | | // (C++98/03 doesn't have this wording; see DR226). |
2572 | 116k | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
2573 | 116k | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
2574 | 116k | : diag::ext_template_parameter_default_in_function_template39 ) |
2575 | 116k | << DefArgRange; |
2576 | 116k | return false; |
2577 | | |
2578 | 14 | case Sema::TPC_ClassTemplateMember: |
2579 | | // C++0x [temp.param]p9: |
2580 | | // A default template-argument shall not be specified in the |
2581 | | // template-parameter-lists of the definition of a member of a |
2582 | | // class template that appears outside of the member's class. |
2583 | 14 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
2584 | 14 | << DefArgRange; |
2585 | 14 | return true; |
2586 | | |
2587 | 8 | case Sema::TPC_FriendClassTemplate: |
2588 | 10 | case Sema::TPC_FriendFunctionTemplate: |
2589 | | // C++ [temp.param]p9: |
2590 | | // A default template-argument shall not be specified in a |
2591 | | // friend template declaration. |
2592 | 10 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
2593 | 10 | << DefArgRange; |
2594 | 10 | return true; |
2595 | | |
2596 | | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
2597 | | // for friend function templates if there is only a single |
2598 | | // declaration (and it is a definition). Strange! |
2599 | 216k | } |
2600 | | |
2601 | 0 | llvm_unreachable("Invalid TemplateParamListContext!"); |
2602 | 0 | } |
2603 | | |
2604 | | /// Check for unexpanded parameter packs within the template parameters |
2605 | | /// of a template template parameter, recursively. |
2606 | | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
2607 | 15.6k | TemplateTemplateParmDecl *TTP) { |
2608 | | // A template template parameter which is a parameter pack is also a pack |
2609 | | // expansion. |
2610 | 15.6k | if (TTP->isParameterPack()) |
2611 | 89 | return false; |
2612 | | |
2613 | 15.5k | TemplateParameterList *Params = TTP->getTemplateParameters(); |
2614 | 32.2k | for (unsigned I = 0, N = Params->size(); I != N; ++I16.6k ) { |
2615 | 16.6k | NamedDecl *P = Params->getParam(I); |
2616 | 16.6k | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(P)) { |
2617 | 15.9k | if (!TTP->isParameterPack()) |
2618 | 1.84k | if (const TypeConstraint *TC = TTP->getTypeConstraint()) |
2619 | 1 | if (TC->hasExplicitTemplateArgs()) |
2620 | 0 | for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments()) |
2621 | 0 | if (S.DiagnoseUnexpandedParameterPack(ArgLoc, |
2622 | 0 | Sema::UPPC_TypeConstraint)) |
2623 | 0 | return true; |
2624 | 15.9k | continue; |
2625 | 15.9k | } |
2626 | | |
2627 | 742 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
2628 | 715 | if (!NTTP->isParameterPack() && |
2629 | 715 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
2630 | 243 | NTTP->getTypeSourceInfo(), |
2631 | 243 | Sema::UPPC_NonTypeTemplateParameterType)) |
2632 | 0 | return true; |
2633 | | |
2634 | 715 | continue; |
2635 | 715 | } |
2636 | | |
2637 | 27 | if (TemplateTemplateParmDecl *InnerTTP |
2638 | 27 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
2639 | 27 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
2640 | 0 | return true; |
2641 | 27 | } |
2642 | | |
2643 | 15.5k | return false; |
2644 | 15.5k | } |
2645 | | |
2646 | | /// Checks the validity of a template parameter list, possibly |
2647 | | /// considering the template parameter list from a previous |
2648 | | /// declaration. |
2649 | | /// |
2650 | | /// If an "old" template parameter list is provided, it must be |
2651 | | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
2652 | | /// template parameter list. |
2653 | | /// |
2654 | | /// \param NewParams Template parameter list for a new template |
2655 | | /// declaration. This template parameter list will be updated with any |
2656 | | /// default arguments that are carried through from the previous |
2657 | | /// template parameter list. |
2658 | | /// |
2659 | | /// \param OldParams If provided, template parameter list from a |
2660 | | /// previous declaration of the same template. Default template |
2661 | | /// arguments will be merged from the old template parameter list to |
2662 | | /// the new template parameter list. |
2663 | | /// |
2664 | | /// \param TPC Describes the context in which we are checking the given |
2665 | | /// template parameter list. |
2666 | | /// |
2667 | | /// \param SkipBody If we might have already made a prior merged definition |
2668 | | /// of this template visible, the corresponding body-skipping information. |
2669 | | /// Default argument redefinition is not an error when skipping such a body, |
2670 | | /// because (under the ODR) we can assume the default arguments are the same |
2671 | | /// as the prior merged definition. |
2672 | | /// |
2673 | | /// \returns true if an error occurred, false otherwise. |
2674 | | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
2675 | | TemplateParameterList *OldParams, |
2676 | | TemplateParamListContext TPC, |
2677 | 1.53M | SkipBodyInfo *SkipBody) { |
2678 | 1.53M | bool Invalid = false; |
2679 | | |
2680 | | // C++ [temp.param]p10: |
2681 | | // The set of default template-arguments available for use with a |
2682 | | // template declaration or definition is obtained by merging the |
2683 | | // default arguments from the definition (if in scope) and all |
2684 | | // declarations in scope in the same way default function |
2685 | | // arguments are (8.3.6). |
2686 | 1.53M | bool SawDefaultArgument = false; |
2687 | 1.53M | SourceLocation PreviousDefaultArgLoc; |
2688 | | |
2689 | | // Dummy initialization to avoid warnings. |
2690 | 1.53M | TemplateParameterList::iterator OldParam = NewParams->end(); |
2691 | 1.53M | if (OldParams) |
2692 | 99.4k | OldParam = OldParams->begin(); |
2693 | | |
2694 | 1.53M | bool RemoveDefaultArguments = false; |
2695 | 1.53M | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2696 | 1.53M | NewParamEnd = NewParams->end(); |
2697 | 4.56M | NewParam != NewParamEnd; ++NewParam3.02M ) { |
2698 | | // Whether we've seen a duplicate default argument in the same translation |
2699 | | // unit. |
2700 | 3.02M | bool RedundantDefaultArg = false; |
2701 | | // Whether we've found inconsis inconsitent default arguments in different |
2702 | | // translation unit. |
2703 | 3.02M | bool InconsistentDefaultArg = false; |
2704 | | // The name of the module which contains the inconsistent default argument. |
2705 | 3.02M | std::string PrevModuleName; |
2706 | | |
2707 | 3.02M | SourceLocation OldDefaultLoc; |
2708 | 3.02M | SourceLocation NewDefaultLoc; |
2709 | | |
2710 | | // Variable used to diagnose missing default arguments |
2711 | 3.02M | bool MissingDefaultArg = false; |
2712 | | |
2713 | | // Variable used to diagnose non-final parameter packs |
2714 | 3.02M | bool SawParameterPack = false; |
2715 | | |
2716 | 3.02M | if (TemplateTypeParmDecl *NewTypeParm |
2717 | 3.02M | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
2718 | | // Check the presence of a default argument here. |
2719 | 2.77M | if (NewTypeParm->hasDefaultArgument() && |
2720 | 2.77M | DiagnoseDefaultTemplateArgument(*this, TPC, |
2721 | 128k | NewTypeParm->getLocation(), |
2722 | 128k | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
2723 | 128k | .getSourceRange())) |
2724 | 22 | NewTypeParm->removeDefaultArgument(); |
2725 | | |
2726 | | // Merge default arguments for template type parameters. |
2727 | 2.77M | TemplateTypeParmDecl *OldTypeParm |
2728 | 2.77M | = OldParams? cast<TemplateTypeParmDecl>(*OldParam)146k : nullptr2.63M ; |
2729 | 2.77M | if (NewTypeParm->isParameterPack()) { |
2730 | 120k | assert(!NewTypeParm->hasDefaultArgument() && |
2731 | 120k | "Parameter packs can't have a default argument!"); |
2732 | 0 | SawParameterPack = true; |
2733 | 2.65M | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm)137k && |
2734 | 2.65M | NewTypeParm->hasDefaultArgument()19.4k && |
2735 | 2.65M | (5 !SkipBody5 || !SkipBody->ShouldSkip3 )) { |
2736 | 3 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
2737 | 3 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
2738 | 3 | SawDefaultArgument = true; |
2739 | | |
2740 | 3 | if (!OldTypeParm->getOwningModule() || |
2741 | 3 | isModuleUnitOfCurrentTU(OldTypeParm->getOwningModule())2 ) |
2742 | 1 | RedundantDefaultArg = true; |
2743 | 2 | else if (!getASTContext().isSameDefaultTemplateArgument(OldTypeParm, |
2744 | 2 | NewTypeParm)) { |
2745 | 1 | InconsistentDefaultArg = true; |
2746 | 1 | PrevModuleName = |
2747 | 1 | OldTypeParm->getImportedOwningModule()->getFullModuleName(); |
2748 | 1 | } |
2749 | 3 | PreviousDefaultArgLoc = NewDefaultLoc; |
2750 | 2.65M | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()137k ) { |
2751 | | // Merge the default argument from the old declaration to the |
2752 | | // new declaration. |
2753 | 19.6k | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
2754 | 19.6k | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
2755 | 2.63M | } else if (NewTypeParm->hasDefaultArgument()) { |
2756 | 128k | SawDefaultArgument = true; |
2757 | 128k | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
2758 | 2.51M | } else if (SawDefaultArgument) |
2759 | 213 | MissingDefaultArg = true; |
2760 | 2.77M | } else if (NonTypeTemplateParmDecl *249k NewNonTypeParm249k |
2761 | 249k | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
2762 | | // Check for unexpanded parameter packs. |
2763 | 233k | if (!NewNonTypeParm->isParameterPack() && |
2764 | 233k | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
2765 | 223k | NewNonTypeParm->getTypeSourceInfo(), |
2766 | 223k | UPPC_NonTypeTemplateParameterType)) { |
2767 | 1 | Invalid = true; |
2768 | 1 | continue; |
2769 | 1 | } |
2770 | | |
2771 | | // Check the presence of a default argument here. |
2772 | 233k | if (NewNonTypeParm->hasDefaultArgument() && |
2773 | 233k | DiagnoseDefaultTemplateArgument(*this, TPC, |
2774 | 78.2k | NewNonTypeParm->getLocation(), |
2775 | 78.2k | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
2776 | 1 | NewNonTypeParm->removeDefaultArgument(); |
2777 | 1 | } |
2778 | | |
2779 | | // Merge default arguments for non-type template parameters |
2780 | 233k | NonTypeTemplateParmDecl *OldNonTypeParm |
2781 | 233k | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam)21.0k : nullptr212k ; |
2782 | 233k | if (NewNonTypeParm->isParameterPack()) { |
2783 | 10.6k | assert(!NewNonTypeParm->hasDefaultArgument() && |
2784 | 10.6k | "Parameter packs can't have a default argument!"); |
2785 | 10.6k | if (!NewNonTypeParm->isPackExpansion()) |
2786 | 10.5k | SawParameterPack = true; |
2787 | 223k | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm)20.0k && |
2788 | 223k | NewNonTypeParm->hasDefaultArgument()1.02k && |
2789 | 223k | (15 !SkipBody15 || !SkipBody->ShouldSkip1 )) { |
2790 | 15 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2791 | 15 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2792 | 15 | SawDefaultArgument = true; |
2793 | 15 | if (!OldNonTypeParm->getOwningModule() || |
2794 | 15 | isModuleUnitOfCurrentTU(OldNonTypeParm->getOwningModule())14 ) |
2795 | 1 | RedundantDefaultArg = true; |
2796 | 14 | else if (!getASTContext().isSameDefaultTemplateArgument( |
2797 | 14 | OldNonTypeParm, NewNonTypeParm)) { |
2798 | 7 | InconsistentDefaultArg = true; |
2799 | 7 | PrevModuleName = |
2800 | 7 | OldNonTypeParm->getImportedOwningModule()->getFullModuleName(); |
2801 | 7 | } |
2802 | 15 | PreviousDefaultArgLoc = NewDefaultLoc; |
2803 | 223k | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()20.0k ) { |
2804 | | // Merge the default argument from the old declaration to the |
2805 | | // new declaration. |
2806 | 1.03k | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
2807 | 1.03k | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2808 | 222k | } else if (NewNonTypeParm->hasDefaultArgument()) { |
2809 | 78.2k | SawDefaultArgument = true; |
2810 | 78.2k | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2811 | 143k | } else if (SawDefaultArgument) |
2812 | 7 | MissingDefaultArg = true; |
2813 | 233k | } else { |
2814 | 15.6k | TemplateTemplateParmDecl *NewTemplateParm |
2815 | 15.6k | = cast<TemplateTemplateParmDecl>(*NewParam); |
2816 | | |
2817 | | // Check for unexpanded parameter packs, recursively. |
2818 | 15.6k | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
2819 | 0 | Invalid = true; |
2820 | 0 | continue; |
2821 | 0 | } |
2822 | | |
2823 | | // Check the presence of a default argument here. |
2824 | 15.6k | if (NewTemplateParm->hasDefaultArgument() && |
2825 | 15.6k | DiagnoseDefaultTemplateArgument(*this, TPC, |
2826 | 9.33k | NewTemplateParm->getLocation(), |
2827 | 9.33k | NewTemplateParm->getDefaultArgument().getSourceRange())) |
2828 | 1 | NewTemplateParm->removeDefaultArgument(); |
2829 | | |
2830 | | // Merge default arguments for template template parameters |
2831 | 15.6k | TemplateTemplateParmDecl *OldTemplateParm |
2832 | 15.6k | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam)78 : nullptr15.5k ; |
2833 | 15.6k | if (NewTemplateParm->isParameterPack()) { |
2834 | 89 | assert(!NewTemplateParm->hasDefaultArgument() && |
2835 | 89 | "Parameter packs can't have a default argument!"); |
2836 | 89 | if (!NewTemplateParm->isPackExpansion()) |
2837 | 80 | SawParameterPack = true; |
2838 | 15.5k | } else if (OldTemplateParm && |
2839 | 15.5k | hasVisibleDefaultArgument(OldTemplateParm)78 && |
2840 | 15.5k | NewTemplateParm->hasDefaultArgument()32 && |
2841 | 15.5k | (3 !SkipBody3 || !SkipBody->ShouldSkip1 )) { |
2842 | 3 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
2843 | 3 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
2844 | 3 | SawDefaultArgument = true; |
2845 | 3 | if (!OldTemplateParm->getOwningModule() || |
2846 | 3 | isModuleUnitOfCurrentTU(OldTemplateParm->getOwningModule())2 ) |
2847 | 1 | RedundantDefaultArg = true; |
2848 | 2 | else if (!getASTContext().isSameDefaultTemplateArgument( |
2849 | 2 | OldTemplateParm, NewTemplateParm)) { |
2850 | 1 | InconsistentDefaultArg = true; |
2851 | 1 | PrevModuleName = |
2852 | 1 | OldTemplateParm->getImportedOwningModule()->getFullModuleName(); |
2853 | 1 | } |
2854 | 3 | PreviousDefaultArgLoc = NewDefaultLoc; |
2855 | 15.5k | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()75 ) { |
2856 | | // Merge the default argument from the old declaration to the |
2857 | | // new declaration. |
2858 | 56 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
2859 | 56 | PreviousDefaultArgLoc |
2860 | 56 | = OldTemplateParm->getDefaultArgument().getLocation(); |
2861 | 15.5k | } else if (NewTemplateParm->hasDefaultArgument()) { |
2862 | 9.30k | SawDefaultArgument = true; |
2863 | 9.30k | PreviousDefaultArgLoc |
2864 | 9.30k | = NewTemplateParm->getDefaultArgument().getLocation(); |
2865 | 9.30k | } else if (6.19k SawDefaultArgument6.19k ) |
2866 | 5 | MissingDefaultArg = true; |
2867 | 15.6k | } |
2868 | | |
2869 | | // C++11 [temp.param]p11: |
2870 | | // If a template parameter of a primary class template or alias template |
2871 | | // is a template parameter pack, it shall be the last template parameter. |
2872 | 3.02M | if (SawParameterPack && (NewParam + 1) != NewParamEnd130k && |
2873 | 3.02M | (26.9k TPC == TPC_ClassTemplate26.9k || TPC == TPC_VarTemplate26.8k || |
2874 | 26.9k | TPC == TPC_TypeAliasTemplate26.8k )) { |
2875 | 7 | Diag((*NewParam)->getLocation(), |
2876 | 7 | diag::err_template_param_pack_must_be_last_template_parameter); |
2877 | 7 | Invalid = true; |
2878 | 7 | } |
2879 | | |
2880 | | // [basic.def.odr]/13: |
2881 | | // There can be more than one definition of a |
2882 | | // ... |
2883 | | // default template argument |
2884 | | // ... |
2885 | | // in a program provided that each definition appears in a different |
2886 | | // translation unit and the definitions satisfy the [same-meaning |
2887 | | // criteria of the ODR]. |
2888 | | // |
2889 | | // Simply, the design of modules allows the definition of template default |
2890 | | // argument to be repeated across translation unit. Note that the ODR is |
2891 | | // checked elsewhere. But it is still not allowed to repeat template default |
2892 | | // argument in the same translation unit. |
2893 | 3.02M | if (RedundantDefaultArg) { |
2894 | 3 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
2895 | 3 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
2896 | 3 | Invalid = true; |
2897 | 3.02M | } else if (InconsistentDefaultArg) { |
2898 | | // We could only diagnose about the case that the OldParam is imported. |
2899 | | // The case NewParam is imported should be handled in ASTReader. |
2900 | 9 | Diag(NewDefaultLoc, |
2901 | 9 | diag::err_template_param_default_arg_inconsistent_redefinition); |
2902 | 9 | Diag(OldDefaultLoc, |
2903 | 9 | diag::note_template_param_prev_default_arg_in_other_module) |
2904 | 9 | << PrevModuleName; |
2905 | 9 | Invalid = true; |
2906 | 3.02M | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate225 ) { |
2907 | | // C++ [temp.param]p11: |
2908 | | // If a template-parameter of a class template has a default |
2909 | | // template-argument, each subsequent template-parameter shall either |
2910 | | // have a default template-argument supplied or be a template parameter |
2911 | | // pack. |
2912 | 17 | Diag((*NewParam)->getLocation(), |
2913 | 17 | diag::err_template_param_default_arg_missing); |
2914 | 17 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
2915 | 17 | Invalid = true; |
2916 | 17 | RemoveDefaultArguments = true; |
2917 | 17 | } |
2918 | | |
2919 | | // If we have an old template parameter list that we're merging |
2920 | | // in, move on to the next parameter. |
2921 | 3.02M | if (OldParams) |
2922 | 167k | ++OldParam; |
2923 | 3.02M | } |
2924 | | |
2925 | | // We were missing some default arguments at the end of the list, so remove |
2926 | | // all of the default arguments. |
2927 | 1.53M | if (RemoveDefaultArguments) { |
2928 | 17 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2929 | 17 | NewParamEnd = NewParams->end(); |
2930 | 55 | NewParam != NewParamEnd; ++NewParam38 ) { |
2931 | 38 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
2932 | 24 | TTP->removeDefaultArgument(); |
2933 | 14 | else if (NonTypeTemplateParmDecl *NTTP |
2934 | 14 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
2935 | 8 | NTTP->removeDefaultArgument(); |
2936 | 6 | else |
2937 | 6 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
2938 | 38 | } |
2939 | 17 | } |
2940 | | |
2941 | 1.53M | return Invalid; |
2942 | 1.53M | } |
2943 | | |
2944 | | namespace { |
2945 | | |
2946 | | /// A class which looks for a use of a certain level of template |
2947 | | /// parameter. |
2948 | | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
2949 | | typedef RecursiveASTVisitor<DependencyChecker> super; |
2950 | | |
2951 | | unsigned Depth; |
2952 | | |
2953 | | // Whether we're looking for a use of a template parameter that makes the |
2954 | | // overall construct type-dependent / a dependent type. This is strictly |
2955 | | // best-effort for now; we may fail to match at all for a dependent type |
2956 | | // in some cases if this is set. |
2957 | | bool IgnoreNonTypeDependent; |
2958 | | |
2959 | | bool Match; |
2960 | | SourceLocation MatchLoc; |
2961 | | |
2962 | | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
2963 | | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
2964 | 18 | Match(false) {} |
2965 | | |
2966 | | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
2967 | 48 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
2968 | 48 | NamedDecl *ND = Params->getParam(0); |
2969 | 48 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
2970 | 41 | Depth = PD->getDepth(); |
2971 | 41 | } else if (NonTypeTemplateParmDecl *7 PD7 = |
2972 | 7 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
2973 | 5 | Depth = PD->getDepth(); |
2974 | 5 | } else { |
2975 | 2 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
2976 | 2 | } |
2977 | 48 | } |
2978 | | |
2979 | 64 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
2980 | 64 | if (ParmDepth >= Depth) { |
2981 | 52 | Match = true; |
2982 | 52 | MatchLoc = Loc; |
2983 | 52 | return true; |
2984 | 52 | } |
2985 | 12 | return false; |
2986 | 64 | } |
2987 | | |
2988 | 35 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
2989 | | // Prune out non-type-dependent expressions if requested. This can |
2990 | | // sometimes result in us failing to find a template parameter reference |
2991 | | // (if a value-dependent expression creates a dependent type), but this |
2992 | | // mode is best-effort only. |
2993 | 35 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
2994 | 35 | if (IgnoreNonTypeDependent && !E->isTypeDependent()34 ) |
2995 | 16 | return true; |
2996 | 19 | return super::TraverseStmt(S, Q); |
2997 | 35 | } |
2998 | | |
2999 | 36 | bool TraverseTypeLoc(TypeLoc TL) { |
3000 | 36 | if (IgnoreNonTypeDependent && !TL.isNull() && |
3001 | 36 | !TL.getType()->isDependentType()) |
3002 | 6 | return true; |
3003 | 30 | return super::TraverseTypeLoc(TL); |
3004 | 36 | } |
3005 | | |
3006 | 11 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
3007 | 11 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
3008 | 11 | } |
3009 | | |
3010 | 50 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
3011 | | // For a best-effort search, keep looking until we find a location. |
3012 | 50 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
3013 | 50 | } |
3014 | | |
3015 | 50 | bool TraverseTemplateName(TemplateName N) { |
3016 | 50 | if (TemplateTemplateParmDecl *PD = |
3017 | 50 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
3018 | 1 | if (Matches(PD->getDepth())) |
3019 | 1 | return false; |
3020 | 49 | return super::TraverseTemplateName(N); |
3021 | 50 | } |
3022 | | |
3023 | 2 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
3024 | 2 | if (NonTypeTemplateParmDecl *PD = |
3025 | 2 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
3026 | 2 | if (Matches(PD->getDepth(), E->getExprLoc())) |
3027 | 1 | return false; |
3028 | 1 | return super::VisitDeclRefExpr(E); |
3029 | 2 | } |
3030 | | |
3031 | 0 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
3032 | 0 | return TraverseType(T->getReplacementType()); |
3033 | 0 | } |
3034 | | |
3035 | | bool |
3036 | 0 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
3037 | 0 | return TraverseTemplateArgument(T->getArgumentPack()); |
3038 | 0 | } |
3039 | | |
3040 | 43 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
3041 | 43 | return TraverseType(T->getInjectedSpecializationType()); |
3042 | 43 | } |
3043 | | }; |
3044 | | } // end anonymous namespace |
3045 | | |
3046 | | /// Determines whether a given type depends on the given parameter |
3047 | | /// list. |
3048 | | static bool |
3049 | 48 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
3050 | 48 | if (!Params->size()) |
3051 | 0 | return false; |
3052 | | |
3053 | 48 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
3054 | 48 | Checker.TraverseType(T); |
3055 | 48 | return Checker.Match; |
3056 | 48 | } |
3057 | | |
3058 | | // Find the source range corresponding to the named type in the given |
3059 | | // nested-name-specifier, if any. |
3060 | | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
3061 | | QualType T, |
3062 | 56 | const CXXScopeSpec &SS) { |
3063 | 56 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
3064 | 66 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
3065 | 65 | if (const Type *CurType = NNS->getAsType()) { |
3066 | 65 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
3067 | 55 | return NNSLoc.getTypeLoc().getSourceRange(); |
3068 | 65 | } else |
3069 | 0 | break; |
3070 | | |
3071 | 10 | NNSLoc = NNSLoc.getPrefix(); |
3072 | 10 | } |
3073 | | |
3074 | 1 | return SourceRange(); |
3075 | 56 | } |
3076 | | |
3077 | | /// Match the given template parameter lists to the given scope |
3078 | | /// specifier, returning the template parameter list that applies to the |
3079 | | /// name. |
3080 | | /// |
3081 | | /// \param DeclStartLoc the start of the declaration that has a scope |
3082 | | /// specifier or a template parameter list. |
3083 | | /// |
3084 | | /// \param DeclLoc The location of the declaration itself. |
3085 | | /// |
3086 | | /// \param SS the scope specifier that will be matched to the given template |
3087 | | /// parameter lists. This scope specifier precedes a qualified name that is |
3088 | | /// being declared. |
3089 | | /// |
3090 | | /// \param TemplateId The template-id following the scope specifier, if there |
3091 | | /// is one. Used to check for a missing 'template<>'. |
3092 | | /// |
3093 | | /// \param ParamLists the template parameter lists, from the outermost to the |
3094 | | /// innermost template parameter lists. |
3095 | | /// |
3096 | | /// \param IsFriend Whether to apply the slightly different rules for |
3097 | | /// matching template parameters to scope specifiers in friend |
3098 | | /// declarations. |
3099 | | /// |
3100 | | /// \param IsMemberSpecialization will be set true if the scope specifier |
3101 | | /// denotes a fully-specialized type, and therefore this is a declaration of |
3102 | | /// a member specialization. |
3103 | | /// |
3104 | | /// \returns the template parameter list, if any, that corresponds to the |
3105 | | /// name that is preceded by the scope specifier @p SS. This template |
3106 | | /// parameter list may have template parameters (if we're declaring a |
3107 | | /// template) or may have no template parameters (if we're declaring a |
3108 | | /// template specialization), or may be NULL (if what we're declaring isn't |
3109 | | /// itself a template). |
3110 | | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
3111 | | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
3112 | | TemplateIdAnnotation *TemplateId, |
3113 | | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
3114 | 13.9M | bool &IsMemberSpecialization, bool &Invalid, bool SuppressDiagnostic) { |
3115 | 13.9M | IsMemberSpecialization = false; |
3116 | 13.9M | Invalid = false; |
3117 | | |
3118 | | // The sequence of nested types to which we will match up the template |
3119 | | // parameter lists. We first build this list by starting with the type named |
3120 | | // by the nested-name-specifier and walking out until we run out of types. |
3121 | 13.9M | SmallVector<QualType, 4> NestedTypes; |
3122 | 13.9M | QualType T; |
3123 | 13.9M | if (SS.getScopeRep()) { |
3124 | 470k | if (CXXRecordDecl *Record |
3125 | 470k | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
3126 | 469k | T = Context.getTypeDeclType(Record); |
3127 | 1.66k | else |
3128 | 1.66k | T = QualType(SS.getScopeRep()->getAsType(), 0); |
3129 | 470k | } |
3130 | | |
3131 | | // If we found an explicit specialization that prevents us from needing |
3132 | | // 'template<>' headers, this will be set to the location of that |
3133 | | // explicit specialization. |
3134 | 13.9M | SourceLocation ExplicitSpecLoc; |
3135 | | |
3136 | 14.4M | while (!T.isNull()) { |
3137 | 471k | NestedTypes.push_back(T); |
3138 | | |
3139 | | // Retrieve the parent of a record type. |
3140 | 471k | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
3141 | | // If this type is an explicit specialization, we're done. |
3142 | 471k | if (ClassTemplateSpecializationDecl *Spec |
3143 | 471k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
3144 | 108k | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
3145 | 108k | Spec->getSpecializationKind() == TSK_ExplicitSpecialization10.9k ) { |
3146 | 4.29k | ExplicitSpecLoc = Spec->getLocation(); |
3147 | 4.29k | break; |
3148 | 4.29k | } |
3149 | 362k | } else if (Record->getTemplateSpecializationKind() |
3150 | 362k | == TSK_ExplicitSpecialization) { |
3151 | 8 | ExplicitSpecLoc = Record->getLocation(); |
3152 | 8 | break; |
3153 | 8 | } |
3154 | | |
3155 | 466k | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
3156 | 2.00k | T = Context.getTypeDeclType(Parent); |
3157 | 464k | else |
3158 | 464k | T = QualType(); |
3159 | 466k | continue; |
3160 | 471k | } |
3161 | | |
3162 | 58 | if (const TemplateSpecializationType *TST |
3163 | 58 | = T->getAs<TemplateSpecializationType>()) { |
3164 | 29 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
3165 | 29 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
3166 | 16 | T = Context.getTypeDeclType(Parent); |
3167 | 13 | else |
3168 | 13 | T = QualType(); |
3169 | 29 | continue; |
3170 | 29 | } |
3171 | 29 | } |
3172 | | |
3173 | | // Look one step prior in a dependent template specialization type. |
3174 | 29 | if (const DependentTemplateSpecializationType *DependentTST |
3175 | 29 | = T->getAs<DependentTemplateSpecializationType>()) { |
3176 | 0 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
3177 | 0 | T = QualType(NNS->getAsType(), 0); |
3178 | 0 | else |
3179 | 0 | T = QualType(); |
3180 | 0 | continue; |
3181 | 0 | } |
3182 | | |
3183 | | // Look one step prior in a dependent name type. |
3184 | 29 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
3185 | 0 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
3186 | 0 | T = QualType(NNS->getAsType(), 0); |
3187 | 0 | else |
3188 | 0 | T = QualType(); |
3189 | 0 | continue; |
3190 | 0 | } |
3191 | | |
3192 | | // Retrieve the parent of an enumeration type. |
3193 | 29 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
3194 | | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
3195 | | // check here. |
3196 | 8 | EnumDecl *Enum = EnumT->getDecl(); |
3197 | | |
3198 | | // Get to the parent type. |
3199 | 8 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
3200 | 0 | T = Context.getTypeDeclType(Parent); |
3201 | 8 | else |
3202 | 8 | T = QualType(); |
3203 | 8 | continue; |
3204 | 8 | } |
3205 | | |
3206 | 21 | T = QualType(); |
3207 | 21 | } |
3208 | | // Reverse the nested types list, since we want to traverse from the outermost |
3209 | | // to the innermost while checking template-parameter-lists. |
3210 | 13.9M | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
3211 | | |
3212 | | // C++0x [temp.expl.spec]p17: |
3213 | | // A member or a member template may be nested within many |
3214 | | // enclosing class templates. In an explicit specialization for |
3215 | | // such a member, the member declaration shall be preceded by a |
3216 | | // template<> for each enclosing class template that is |
3217 | | // explicitly specialized. |
3218 | 13.9M | bool SawNonEmptyTemplateParameterList = false; |
3219 | | |
3220 | 13.9M | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
3221 | 86.7k | if (SawNonEmptyTemplateParameterList) { |
3222 | 19 | if (!SuppressDiagnostic) |
3223 | 4 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
3224 | 4 | << !Recovery << Range; |
3225 | 19 | Invalid = true; |
3226 | 19 | IsMemberSpecialization = false; |
3227 | 19 | return true; |
3228 | 19 | } |
3229 | | |
3230 | 86.7k | return false; |
3231 | 86.7k | }; |
3232 | | |
3233 | 13.9M | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
3234 | | // Check that we can have an explicit specialization here. |
3235 | 62 | if (CheckExplicitSpecialization(Range, true)) |
3236 | 3 | return true; |
3237 | | |
3238 | | // We don't have a template header, but we should. |
3239 | 59 | SourceLocation ExpectedTemplateLoc; |
3240 | 59 | if (!ParamLists.empty()) |
3241 | 10 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
3242 | 49 | else |
3243 | 49 | ExpectedTemplateLoc = DeclStartLoc; |
3244 | | |
3245 | 59 | if (!SuppressDiagnostic) |
3246 | 54 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
3247 | 54 | << Range |
3248 | 54 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
3249 | 59 | return false; |
3250 | 62 | }; |
3251 | | |
3252 | 13.9M | unsigned ParamIdx = 0; |
3253 | 14.4M | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
3254 | 13.9M | ++TypeIdx471k ) { |
3255 | 471k | T = NestedTypes[TypeIdx]; |
3256 | | |
3257 | | // Whether we expect a 'template<>' header. |
3258 | 471k | bool NeedEmptyTemplateHeader = false; |
3259 | | |
3260 | | // Whether we expect a template header with parameters. |
3261 | 471k | bool NeedNonemptyTemplateHeader = false; |
3262 | | |
3263 | | // For a dependent type, the set of template parameters that we |
3264 | | // expect to see. |
3265 | 471k | TemplateParameterList *ExpectedTemplateParams = nullptr; |
3266 | | |
3267 | | // C++0x [temp.expl.spec]p15: |
3268 | | // A member or a member template may be nested within many enclosing |
3269 | | // class templates. In an explicit specialization for such a member, the |
3270 | | // member declaration shall be preceded by a template<> for each |
3271 | | // enclosing class template that is explicitly specialized. |
3272 | 471k | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
3273 | 471k | if (ClassTemplatePartialSpecializationDecl *Partial |
3274 | 471k | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
3275 | 97.6k | ExpectedTemplateParams = Partial->getTemplateParameters(); |
3276 | 97.6k | NeedNonemptyTemplateHeader = true; |
3277 | 373k | } else if (Record->isDependentType()) { |
3278 | 344k | if (Record->getDescribedClassTemplate()) { |
3279 | 343k | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
3280 | 343k | ->getTemplateParameters(); |
3281 | 343k | NeedNonemptyTemplateHeader = true; |
3282 | 343k | } |
3283 | 344k | } else if (ClassTemplateSpecializationDecl *28.9k Spec28.9k |
3284 | 28.9k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
3285 | | // C++0x [temp.expl.spec]p4: |
3286 | | // Members of an explicitly specialized class template are defined |
3287 | | // in the same manner as members of normal classes, and not using |
3288 | | // the template<> syntax. |
3289 | 10.9k | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
3290 | 6.66k | NeedEmptyTemplateHeader = true; |
3291 | 4.29k | else |
3292 | 4.29k | continue; |
3293 | 18.0k | } else if (Record->getTemplateSpecializationKind()) { |
3294 | 9 | if (Record->getTemplateSpecializationKind() |
3295 | 9 | != TSK_ExplicitSpecialization && |
3296 | 9 | TypeIdx == NumTypes - 11 ) |
3297 | 1 | IsMemberSpecialization = true; |
3298 | | |
3299 | 9 | continue; |
3300 | 9 | } |
3301 | 471k | } else if (const TemplateSpecializationType *58 TST58 |
3302 | 58 | = T->getAs<TemplateSpecializationType>()) { |
3303 | 29 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
3304 | 29 | ExpectedTemplateParams = Template->getTemplateParameters(); |
3305 | 29 | NeedNonemptyTemplateHeader = true; |
3306 | 29 | } |
3307 | 29 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
3308 | | // FIXME: We actually could/should check the template arguments here |
3309 | | // against the corresponding template parameter list. |
3310 | 0 | NeedNonemptyTemplateHeader = false; |
3311 | 0 | } |
3312 | | |
3313 | | // C++ [temp.expl.spec]p16: |
3314 | | // In an explicit specialization declaration for a member of a class |
3315 | | // template or a member template that ap- pears in namespace scope, the |
3316 | | // member template and some of its enclosing class templates may remain |
3317 | | // unspecialized, except that the declaration shall not explicitly |
3318 | | // specialize a class member template if its en- closing class templates |
3319 | | // are not explicitly specialized as well. |
3320 | 467k | if (ParamIdx < ParamLists.size()) { |
3321 | 455k | if (ParamLists[ParamIdx]->size() == 0) { |
3322 | 7.66k | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
3323 | 7.66k | false)) |
3324 | 14 | return nullptr; |
3325 | 7.66k | } else |
3326 | 447k | SawNonEmptyTemplateParameterList = true; |
3327 | 455k | } |
3328 | | |
3329 | 467k | if (NeedEmptyTemplateHeader) { |
3330 | | // If we're on the last of the types, and we need a 'template<>' header |
3331 | | // here, then it's a member specialization. |
3332 | 6.66k | if (TypeIdx == NumTypes - 1) |
3333 | 6.54k | IsMemberSpecialization = true; |
3334 | | |
3335 | 6.66k | if (ParamIdx < ParamLists.size()) { |
3336 | 6.58k | if (ParamLists[ParamIdx]->size() > 0) { |
3337 | | // The header has template parameters when it shouldn't. Complain. |
3338 | 39 | if (!SuppressDiagnostic) |
3339 | 22 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
3340 | 22 | diag::err_template_param_list_matches_nontemplate) |
3341 | 22 | << T |
3342 | 22 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
3343 | 22 | ParamLists[ParamIdx]->getRAngleLoc()) |
3344 | 22 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
3345 | 39 | Invalid = true; |
3346 | 39 | return nullptr; |
3347 | 39 | } |
3348 | | |
3349 | | // Consume this template header. |
3350 | 6.55k | ++ParamIdx; |
3351 | 6.55k | continue; |
3352 | 6.58k | } |
3353 | | |
3354 | 75 | if (!IsFriend) |
3355 | 33 | if (DiagnoseMissingExplicitSpecialization( |
3356 | 33 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
3357 | 0 | return nullptr; |
3358 | | |
3359 | 75 | continue; |
3360 | 75 | } |
3361 | | |
3362 | 460k | if (NeedNonemptyTemplateHeader) { |
3363 | | // In friend declarations we can have template-ids which don't |
3364 | | // depend on the corresponding template parameter lists. But |
3365 | | // assume that empty parameter lists are supposed to match this |
3366 | | // template-id. |
3367 | 440k | if (IsFriend && T->isDependentType()60 ) { |
3368 | 60 | if (ParamIdx < ParamLists.size() && |
3369 | 60 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])48 ) |
3370 | 40 | ExpectedTemplateParams = nullptr; |
3371 | 20 | else |
3372 | 20 | continue; |
3373 | 60 | } |
3374 | | |
3375 | 440k | if (ParamIdx < ParamLists.size()) { |
3376 | | // Check the template parameter list, if we can. |
3377 | 440k | if (ExpectedTemplateParams && |
3378 | 440k | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
3379 | 440k | ExpectedTemplateParams, |
3380 | 440k | !SuppressDiagnostic, TPL_TemplateMatch)) |
3381 | 4 | Invalid = true; |
3382 | | |
3383 | 440k | if (!Invalid && |
3384 | 440k | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
3385 | 440k | TPC_ClassTemplateMember)) |
3386 | 0 | Invalid = true; |
3387 | | |
3388 | 440k | ++ParamIdx; |
3389 | 440k | continue; |
3390 | 440k | } |
3391 | | |
3392 | 3 | if (!SuppressDiagnostic) |
3393 | 1 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
3394 | 1 | << T |
3395 | 1 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
3396 | 3 | Invalid = true; |
3397 | 3 | continue; |
3398 | 440k | } |
3399 | 460k | } |
3400 | | |
3401 | | // If there were at least as many template-ids as there were template |
3402 | | // parameter lists, then there are no template parameter lists remaining for |
3403 | | // the declaration itself. |
3404 | 13.9M | if (ParamIdx >= ParamLists.size()) { |
3405 | 11.9M | if (TemplateId && !IsFriend1.72k ) { |
3406 | | // We don't have a template header for the declaration itself, but we |
3407 | | // should. |
3408 | 29 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
3409 | 29 | TemplateId->RAngleLoc)); |
3410 | | |
3411 | | // Fabricate an empty template parameter list for the invented header. |
3412 | 29 | return TemplateParameterList::Create(Context, SourceLocation(), |
3413 | 29 | SourceLocation(), None, |
3414 | 29 | SourceLocation(), nullptr); |
3415 | 29 | } |
3416 | | |
3417 | 11.9M | return nullptr; |
3418 | 11.9M | } |
3419 | | |
3420 | | // If there were too many template parameter lists, complain about that now. |
3421 | 2.01M | if (ParamIdx < ParamLists.size() - 1) { |
3422 | 20 | bool HasAnyExplicitSpecHeader = false; |
3423 | 20 | bool AllExplicitSpecHeaders = true; |
3424 | 40 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I20 ) { |
3425 | 20 | if (ParamLists[I]->size() == 0) |
3426 | 15 | HasAnyExplicitSpecHeader = true; |
3427 | 5 | else |
3428 | 5 | AllExplicitSpecHeaders = false; |
3429 | 20 | } |
3430 | | |
3431 | 20 | if (!SuppressDiagnostic) |
3432 | 14 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
3433 | 14 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers11 |
3434 | 14 | : diag::err_template_spec_extra_headers3 ) |
3435 | 14 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
3436 | 14 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
3437 | | |
3438 | | // If there was a specialization somewhere, such that 'template<>' is |
3439 | | // not required, and there were any 'template<>' headers, note where the |
3440 | | // specialization occurred. |
3441 | 20 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader15 && |
3442 | 20 | !SuppressDiagnostic15 ) |
3443 | 11 | Diag(ExplicitSpecLoc, |
3444 | 11 | diag::note_explicit_template_spec_does_not_need_header) |
3445 | 11 | << NestedTypes.back(); |
3446 | | |
3447 | | // We have a template parameter list with no corresponding scope, which |
3448 | | // means that the resulting template declaration can't be instantiated |
3449 | | // properly (we'll end up with dependent nodes when we shouldn't). |
3450 | 20 | if (!AllExplicitSpecHeaders) |
3451 | 5 | Invalid = true; |
3452 | 20 | } |
3453 | | |
3454 | | // C++ [temp.expl.spec]p16: |
3455 | | // In an explicit specialization declaration for a member of a class |
3456 | | // template or a member template that ap- pears in namespace scope, the |
3457 | | // member template and some of its enclosing class templates may remain |
3458 | | // unspecialized, except that the declaration shall not explicitly |
3459 | | // specialize a class member template if its en- closing class templates |
3460 | | // are not explicitly specialized as well. |
3461 | 2.01M | if (ParamLists.back()->size() == 0 && |
3462 | 2.01M | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
3463 | 79.0k | false)) |
3464 | 2 | return nullptr; |
3465 | | |
3466 | | // Return the last template parameter list, which corresponds to the |
3467 | | // entity being declared. |
3468 | 2.01M | return ParamLists.back(); |
3469 | 2.01M | } |
3470 | | |
3471 | 43 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
3472 | 43 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
3473 | 28 | Diag(Template->getLocation(), diag::note_template_declared_here) |
3474 | 28 | << (isa<FunctionTemplateDecl>(Template) |
3475 | 28 | ? 06 |
3476 | 28 | : isa<ClassTemplateDecl>(Template)22 |
3477 | 22 | ? 10 |
3478 | 22 | : isa<VarTemplateDecl>(Template) |
3479 | 22 | ? 220 |
3480 | 22 | : isa<TypeAliasTemplateDecl>(Template)2 ? 32 : 40 ) |
3481 | 28 | << Template->getDeclName(); |
3482 | 28 | return; |
3483 | 28 | } |
3484 | | |
3485 | 15 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
3486 | 3 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
3487 | 3 | IEnd = OST->end(); |
3488 | 9 | I != IEnd; ++I6 ) |
3489 | 6 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
3490 | 6 | << 0 << (*I)->getDeclName(); |
3491 | | |
3492 | 3 | return; |
3493 | 3 | } |
3494 | 15 | } |
3495 | | |
3496 | | static QualType |
3497 | | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
3498 | | const SmallVectorImpl<TemplateArgument> &Converted, |
3499 | | SourceLocation TemplateLoc, |
3500 | 1.89k | TemplateArgumentListInfo &TemplateArgs) { |
3501 | 1.89k | ASTContext &Context = SemaRef.getASTContext(); |
3502 | 1.89k | switch (BTD->getBuiltinTemplateKind()) { |
3503 | 1.17k | case BTK__make_integer_seq: { |
3504 | | // Specializations of __make_integer_seq<S, T, N> are treated like |
3505 | | // S<T, 0, ..., N-1>. |
3506 | | |
3507 | | // C++14 [inteseq.intseq]p1: |
3508 | | // T shall be an integer type. |
3509 | 1.17k | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
3510 | 1 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
3511 | 1 | diag::err_integer_sequence_integral_element_type); |
3512 | 1 | return QualType(); |
3513 | 1 | } |
3514 | | |
3515 | | // C++14 [inteseq.make]p1: |
3516 | | // If N is negative the program is ill-formed. |
3517 | 1.17k | TemplateArgument NumArgsArg = Converted[2]; |
3518 | 1.17k | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
3519 | 1.17k | if (NumArgs < 0) { |
3520 | 1 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
3521 | 1 | diag::err_integer_sequence_negative_length); |
3522 | 1 | return QualType(); |
3523 | 1 | } |
3524 | | |
3525 | 1.17k | QualType ArgTy = NumArgsArg.getIntegralType(); |
3526 | 1.17k | TemplateArgumentListInfo SyntheticTemplateArgs; |
3527 | | // The type argument gets reused as the first template argument in the |
3528 | | // synthetic template argument list. |
3529 | 1.17k | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
3530 | | // Expand N into 0 ... N-1. |
3531 | 1.17k | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
3532 | 19.2k | I < NumArgs; ++I18.0k ) { |
3533 | 18.0k | TemplateArgument TA(Context, I, ArgTy); |
3534 | 18.0k | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
3535 | 18.0k | TA, ArgTy, TemplateArgs[2].getLocation())); |
3536 | 18.0k | } |
3537 | | // The first template argument will be reused as the template decl that |
3538 | | // our synthetic template arguments will be applied to. |
3539 | 1.17k | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
3540 | 1.17k | TemplateLoc, SyntheticTemplateArgs); |
3541 | 1.17k | } |
3542 | | |
3543 | 716 | case BTK__type_pack_element: |
3544 | | // Specializations of |
3545 | | // __type_pack_element<Index, T_1, ..., T_N> |
3546 | | // are treated like T_Index. |
3547 | 716 | assert(Converted.size() == 2 && |
3548 | 716 | "__type_pack_element should be given an index and a parameter pack"); |
3549 | | |
3550 | | // If the Index is out of bounds, the program is ill-formed. |
3551 | 0 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
3552 | 716 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
3553 | 716 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
3554 | 716 | "type std::size_t, and hence be non-negative"); |
3555 | 716 | if (Index >= Ts.pack_size()) { |
3556 | 1 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
3557 | 1 | diag::err_type_pack_element_out_of_bounds); |
3558 | 1 | return QualType(); |
3559 | 1 | } |
3560 | | |
3561 | | // We simply return the type at index `Index`. |
3562 | 715 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
3563 | 715 | return Nth->getAsType(); |
3564 | 1.89k | } |
3565 | 0 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
3566 | 0 | } |
3567 | | |
3568 | | /// Determine whether this alias template is "enable_if_t". |
3569 | | /// libc++ >=14 uses "__enable_if_t" in C++11 mode. |
3570 | 56.7k | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
3571 | 56.7k | return AliasTemplate->getName().equals("enable_if_t") || |
3572 | 56.7k | AliasTemplate->getName().equals("__enable_if_t")56.0k ; |
3573 | 56.7k | } |
3574 | | |
3575 | | /// Collect all of the separable terms in the given condition, which |
3576 | | /// might be a conjunction. |
3577 | | /// |
3578 | | /// FIXME: The right answer is to convert the logical expression into |
3579 | | /// disjunctive normal form, so we can find the first failed term |
3580 | | /// within each possible clause. |
3581 | | static void collectConjunctionTerms(Expr *Clause, |
3582 | 205k | SmallVectorImpl<Expr *> &Terms) { |
3583 | 205k | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
3584 | 49.1k | if (BinOp->getOpcode() == BO_LAnd) { |
3585 | 41.8k | collectConjunctionTerms(BinOp->getLHS(), Terms); |
3586 | 41.8k | collectConjunctionTerms(BinOp->getRHS(), Terms); |
3587 | 41.8k | } |
3588 | | |
3589 | 49.1k | return; |
3590 | 49.1k | } |
3591 | | |
3592 | 155k | Terms.push_back(Clause); |
3593 | 155k | } |
3594 | | |
3595 | | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
3596 | | // a left-hand side that is value-dependent but never true. Identify |
3597 | | // the idiom and ignore that term. |
3598 | 121k | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
3599 | | // Top-level '||'. |
3600 | 121k | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
3601 | 121k | if (!BinOp) return Cond90.3k ; |
3602 | | |
3603 | 31.1k | if (BinOp->getOpcode() != BO_LOr) return Cond31.0k ; |
3604 | | |
3605 | | // With an inner '==' that has a literal on the right-hand side. |
3606 | 47 | Expr *LHS = BinOp->getLHS(); |
3607 | 47 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
3608 | 47 | if (!InnerBinOp) return Cond1 ; |
3609 | | |
3610 | 46 | if (InnerBinOp->getOpcode() != BO_EQ || |
3611 | 46 | !isa<IntegerLiteral>(InnerBinOp->getRHS())6 ) |
3612 | 40 | return Cond; |
3613 | | |
3614 | | // If the inner binary operation came from a macro expansion named |
3615 | | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
3616 | | // of the '||', which is the real, user-provided condition. |
3617 | 6 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
3618 | 6 | if (!Loc.isMacroID()) return Cond3 ; |
3619 | | |
3620 | 3 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
3621 | 3 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
3622 | 3 | return BinOp->getRHS(); |
3623 | | |
3624 | 0 | return Cond; |
3625 | 3 | } |
3626 | | |
3627 | | namespace { |
3628 | | |
3629 | | // A PrinterHelper that prints more helpful diagnostics for some sub-expressions |
3630 | | // within failing boolean expression, such as substituting template parameters |
3631 | | // for actual types. |
3632 | | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
3633 | | public: |
3634 | | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
3635 | 121k | : Policy(P) {} |
3636 | | |
3637 | 252k | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
3638 | 252k | const auto *DR = dyn_cast<DeclRefExpr>(E); |
3639 | 252k | if (DR && DR->getQualifier()125k ) { |
3640 | | // If this is a qualified name, expand the template arguments in nested |
3641 | | // qualifiers. |
3642 | 124k | DR->getQualifier()->print(OS, Policy, true); |
3643 | | // Then print the decl itself. |
3644 | 124k | const ValueDecl *VD = DR->getDecl(); |
3645 | 124k | OS << VD->getName(); |
3646 | 124k | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
3647 | | // This is a template variable, print the expanded template arguments. |
3648 | 6 | printTemplateArgumentList( |
3649 | 6 | OS, IV->getTemplateArgs().asArray(), Policy, |
3650 | 6 | IV->getSpecializedTemplate()->getTemplateParameters()); |
3651 | 6 | } |
3652 | 124k | return true; |
3653 | 124k | } |
3654 | 128k | return false; |
3655 | 252k | } |
3656 | | |
3657 | | private: |
3658 | | const PrintingPolicy Policy; |
3659 | | }; |
3660 | | |
3661 | | } // end anonymous namespace |
3662 | | |
3663 | | std::pair<Expr *, std::string> |
3664 | 121k | Sema::findFailedBooleanCondition(Expr *Cond) { |
3665 | 121k | Cond = lookThroughRangesV3Condition(PP, Cond); |
3666 | | |
3667 | | // Separate out all of the terms in a conjunction. |
3668 | 121k | SmallVector<Expr *, 4> Terms; |
3669 | 121k | collectConjunctionTerms(Cond, Terms); |
3670 | | |
3671 | | // Determine which term failed. |
3672 | 121k | Expr *FailedCond = nullptr; |
3673 | 130k | for (Expr *Term : Terms) { |
3674 | 130k | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
3675 | | |
3676 | | // Literals are uninteresting. |
3677 | 130k | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
3678 | 130k | isa<IntegerLiteral>(TermAsWritten)130k ) |
3679 | 42 | continue; |
3680 | | |
3681 | | // The initialization of the parameter from the argument is |
3682 | | // a constant-evaluated context. |
3683 | 130k | EnterExpressionEvaluationContext ConstantEvaluated( |
3684 | 130k | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
3685 | | |
3686 | 130k | bool Succeeded; |
3687 | 130k | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
3688 | 130k | !Succeeded) { |
3689 | 120k | FailedCond = TermAsWritten; |
3690 | 120k | break; |
3691 | 120k | } |
3692 | 130k | } |
3693 | 121k | if (!FailedCond) |
3694 | 512 | FailedCond = Cond->IgnoreParenImpCasts(); |
3695 | | |
3696 | 121k | std::string Description; |
3697 | 121k | { |
3698 | 121k | llvm::raw_string_ostream Out(Description); |
3699 | 121k | PrintingPolicy Policy = getPrintingPolicy(); |
3700 | 121k | Policy.PrintCanonicalTypes = true; |
3701 | 121k | FailedBooleanConditionPrinterHelper Helper(Policy); |
3702 | 121k | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
3703 | 121k | } |
3704 | 121k | return { FailedCond, Description }; |
3705 | 121k | } |
3706 | | |
3707 | | QualType Sema::CheckTemplateIdType(TemplateName Name, |
3708 | | SourceLocation TemplateLoc, |
3709 | 8.13M | TemplateArgumentListInfo &TemplateArgs) { |
3710 | 8.13M | DependentTemplateName *DTN |
3711 | 8.13M | = Name.getUnderlying().getAsDependentTemplateName(); |
3712 | 8.13M | if (DTN && DTN->isIdentifier()10 ) |
3713 | | // When building a template-id where the template-name is dependent, |
3714 | | // assume the template is a type template. Either our assumption is |
3715 | | // correct, or the code is ill-formed and will be diagnosed when the |
3716 | | // dependent name is substituted. |
3717 | 10 | return Context.getDependentTemplateSpecializationType(ETK_None, |
3718 | 10 | DTN->getQualifier(), |
3719 | 10 | DTN->getIdentifier(), |
3720 | 10 | TemplateArgs); |
3721 | | |
3722 | 8.13M | if (Name.getAsAssumedTemplateName() && |
3723 | 8.13M | resolveAssumedTemplateNameAsType(/*Scope*/nullptr, Name, TemplateLoc)0 ) |
3724 | 0 | return QualType(); |
3725 | | |
3726 | 8.13M | TemplateDecl *Template = Name.getAsTemplateDecl(); |
3727 | 8.13M | if (!Template || isa<FunctionTemplateDecl>(Template)8.13M || |
3728 | 8.13M | isa<VarTemplateDecl>(Template)8.13M || isa<ConceptDecl>(Template)8.13M ) { |
3729 | | // We might have a substituted template template parameter pack. If so, |
3730 | | // build a template specialization type for it. |
3731 | 22 | if (Name.getAsSubstTemplateTemplateParmPack()) |
3732 | 2 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
3733 | | |
3734 | 20 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
3735 | 20 | << Name; |
3736 | 20 | NoteAllFoundTemplates(Name); |
3737 | 20 | return QualType(); |
3738 | 22 | } |
3739 | | |
3740 | | // Check that the template argument list is well-formed for this |
3741 | | // template. |
3742 | 8.13M | SmallVector<TemplateArgument, 4> Converted; |
3743 | 8.13M | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
3744 | 8.13M | false, Converted, |
3745 | 8.13M | /*UpdateArgsWithConversions=*/true)) |
3746 | 2.96k | return QualType(); |
3747 | | |
3748 | 8.12M | QualType CanonType; |
3749 | | |
3750 | 8.12M | if (TypeAliasTemplateDecl *AliasTemplate = |
3751 | 8.12M | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
3752 | | |
3753 | | // Find the canonical type for this type alias template specialization. |
3754 | 1.10M | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
3755 | 1.10M | if (Pattern->isInvalidDecl()) |
3756 | 1 | return QualType(); |
3757 | | |
3758 | 1.10M | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
3759 | 1.10M | Converted); |
3760 | | |
3761 | | // Only substitute for the innermost template argument list. |
3762 | 1.10M | MultiLevelTemplateArgumentList TemplateArgLists; |
3763 | 1.10M | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
3764 | 1.10M | TemplateArgLists.addOuterRetainedLevels( |
3765 | 1.10M | AliasTemplate->getTemplateParameters()->getDepth()); |
3766 | | |
3767 | 1.10M | LocalInstantiationScope Scope(*this); |
3768 | 1.10M | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
3769 | 1.10M | if (Inst.isInvalid()) |
3770 | 212 | return QualType(); |
3771 | | |
3772 | 1.10M | CanonType = SubstType(Pattern->getUnderlyingType(), |
3773 | 1.10M | TemplateArgLists, AliasTemplate->getLocation(), |
3774 | 1.10M | AliasTemplate->getDeclName()); |
3775 | 1.10M | if (CanonType.isNull()) { |
3776 | | // If this was enable_if and we failed to find the nested type |
3777 | | // within enable_if in a SFINAE context, dig out the specific |
3778 | | // enable_if condition that failed and present that instead. |
3779 | 56.7k | if (isEnableIfAliasTemplate(AliasTemplate)) { |
3780 | 50.5k | if (auto DeductionInfo = isSFINAEContext()) { |
3781 | 50.5k | if (*DeductionInfo && |
3782 | 50.5k | (*DeductionInfo)->hasSFINAEDiagnostic() && |
3783 | 50.5k | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
3784 | 50.5k | diag::err_typename_nested_not_found_enable_if && |
3785 | 50.5k | TemplateArgs[0].getArgument().getKind() |
3786 | 50.5k | == TemplateArgument::Expression) { |
3787 | 50.5k | Expr *FailedCond; |
3788 | 50.5k | std::string FailedDescription; |
3789 | 50.5k | std::tie(FailedCond, FailedDescription) = |
3790 | 50.5k | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
3791 | | |
3792 | | // Remove the old SFINAE diagnostic. |
3793 | 50.5k | PartialDiagnosticAt OldDiag = |
3794 | 50.5k | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
3795 | 50.5k | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
3796 | | |
3797 | | // Add a new SFINAE diagnostic specifying which condition |
3798 | | // failed. |
3799 | 50.5k | (*DeductionInfo)->addSFINAEDiagnostic( |
3800 | 50.5k | OldDiag.first, |
3801 | 50.5k | PDiag(diag::err_typename_nested_not_found_requirement) |
3802 | 50.5k | << FailedDescription |
3803 | 50.5k | << FailedCond->getSourceRange()); |
3804 | 50.5k | } |
3805 | 50.5k | } |
3806 | 50.5k | } |
3807 | | |
3808 | 56.7k | return QualType(); |
3809 | 56.7k | } |
3810 | 7.02M | } else if (Name.isDependent() || |
3811 | 7.02M | TemplateSpecializationType::anyDependentTemplateArguments( |
3812 | 6.93M | TemplateArgs, Converted)) { |
3813 | | // This class template specialization is a dependent |
3814 | | // type. Therefore, its canonical type is another class template |
3815 | | // specialization type that contains all of the converted |
3816 | | // arguments in canonical form. This ensures that, e.g., A<T> and |
3817 | | // A<T, T> have identical types when A is declared as: |
3818 | | // |
3819 | | // template<typename T, typename U = T> struct A; |
3820 | 4.27M | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
3821 | | |
3822 | | // This might work out to be a current instantiation, in which |
3823 | | // case the canonical type needs to be the InjectedClassNameType. |
3824 | | // |
3825 | | // TODO: in theory this could be a simple hashtable lookup; most |
3826 | | // changes to CurContext don't change the set of current |
3827 | | // instantiations. |
3828 | 4.27M | if (isa<ClassTemplateDecl>(Template)) { |
3829 | 7.02M | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()2.79M ) { |
3830 | | // If we get out to a namespace, we're done. |
3831 | 7.02M | if (Ctx->isFileContext()) break3.86M ; |
3832 | | |
3833 | | // If this isn't a record, keep looking. |
3834 | 3.16M | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
3835 | 3.16M | if (!Record) continue580k ; |
3836 | | |
3837 | | // Look for one of the two cases with InjectedClassNameTypes |
3838 | | // and check whether it's the same template. |
3839 | 2.58M | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
3840 | 2.58M | !Record->getDescribedClassTemplate()2.16M ) |
3841 | 677k | continue; |
3842 | | |
3843 | | // Fetch the injected class name type and check whether its |
3844 | | // injected type is equal to the type we just built. |
3845 | 1.91M | QualType ICNT = Context.getTypeDeclType(Record); |
3846 | 1.91M | QualType Injected = cast<InjectedClassNameType>(ICNT) |
3847 | 1.91M | ->getInjectedSpecializationType(); |
3848 | | |
3849 | 1.91M | if (CanonType != Injected->getCanonicalTypeInternal()) |
3850 | 1.53M | continue; |
3851 | | |
3852 | | // If so, the canonical type of this TST is the injected |
3853 | | // class name type of the record we just found. |
3854 | 374k | assert(ICNT.isCanonical()); |
3855 | 0 | CanonType = ICNT; |
3856 | 374k | break; |
3857 | 1.91M | } |
3858 | 4.23M | } |
3859 | 4.27M | } else if (ClassTemplateDecl *2.75M ClassTemplate2.75M |
3860 | 2.75M | = dyn_cast<ClassTemplateDecl>(Template)) { |
3861 | | // Find the class template specialization declaration that |
3862 | | // corresponds to these arguments. |
3863 | 2.74M | void *InsertPos = nullptr; |
3864 | 2.74M | ClassTemplateSpecializationDecl *Decl |
3865 | 2.74M | = ClassTemplate->findSpecialization(Converted, InsertPos); |
3866 | 2.74M | if (!Decl) { |
3867 | | // This is the first time we have referenced this class template |
3868 | | // specialization. Create the canonical declaration and add it to |
3869 | | // the set of specializations. |
3870 | 1.00M | Decl = ClassTemplateSpecializationDecl::Create( |
3871 | 1.00M | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
3872 | 1.00M | ClassTemplate->getDeclContext(), |
3873 | 1.00M | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
3874 | 1.00M | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
3875 | 1.00M | ClassTemplate->AddSpecialization(Decl, InsertPos); |
3876 | 1.00M | if (ClassTemplate->isOutOfLine()) |
3877 | 52 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
3878 | 1.00M | } |
3879 | | |
3880 | 2.74M | if (Decl->getSpecializationKind() == TSK_Undeclared && |
3881 | 2.74M | ClassTemplate->getTemplatedDecl()->hasAttrs()1.10M ) { |
3882 | 637k | InstantiatingTemplate Inst(*this, TemplateLoc, Decl); |
3883 | 637k | if (!Inst.isInvalid()) { |
3884 | 637k | MultiLevelTemplateArgumentList TemplateArgLists; |
3885 | 637k | TemplateArgLists.addOuterTemplateArguments(Converted); |
3886 | 637k | InstantiateAttrsForDecl(TemplateArgLists, |
3887 | 637k | ClassTemplate->getTemplatedDecl(), Decl); |
3888 | 637k | } |
3889 | 637k | } |
3890 | | |
3891 | | // Diagnose uses of this specialization. |
3892 | 2.74M | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
3893 | | |
3894 | 2.74M | CanonType = Context.getTypeDeclType(Decl); |
3895 | 2.74M | assert(isa<RecordType>(CanonType) && |
3896 | 2.74M | "type of non-dependent specialization is not a RecordType"); |
3897 | 2.74M | } else if (auto *1.89k BTD1.89k = dyn_cast<BuiltinTemplateDecl>(Template)) { |
3898 | 1.89k | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
3899 | 1.89k | TemplateArgs); |
3900 | 1.89k | } |
3901 | | |
3902 | | // Build the fully-sugared type for this class template |
3903 | | // specialization, which refers back to the class template |
3904 | | // specialization we created or found. |
3905 | 8.07M | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
3906 | 8.12M | } |
3907 | | |
3908 | | void Sema::ActOnUndeclaredTypeTemplateName(Scope *S, TemplateTy &ParsedName, |
3909 | | TemplateNameKind &TNK, |
3910 | | SourceLocation NameLoc, |
3911 | 20 | IdentifierInfo *&II) { |
3912 | 20 | assert(TNK == TNK_Undeclared_template && "not an undeclared template name"); |
3913 | | |
3914 | 0 | TemplateName Name = ParsedName.get(); |
3915 | 20 | auto *ATN = Name.getAsAssumedTemplateName(); |
3916 | 20 | assert(ATN && "not an assumed template name"); |
3917 | 0 | II = ATN->getDeclName().getAsIdentifierInfo(); |
3918 | | |
3919 | 20 | if (!resolveAssumedTemplateNameAsType(S, Name, NameLoc, /*Diagnose*/false)) { |
3920 | | // Resolved to a type template name. |
3921 | 3 | ParsedName = TemplateTy::make(Name); |
3922 | 3 | TNK = TNK_Type_template; |
3923 | 3 | } |
3924 | 20 | } |
3925 | | |
3926 | | bool Sema::resolveAssumedTemplateNameAsType(Scope *S, TemplateName &Name, |
3927 | | SourceLocation NameLoc, |
3928 | 191 | bool Diagnose) { |
3929 | | // We assumed this undeclared identifier to be an (ADL-only) function |
3930 | | // template name, but it was used in a context where a type was required. |
3931 | | // Try to typo-correct it now. |
3932 | 191 | AssumedTemplateStorage *ATN = Name.getAsAssumedTemplateName(); |
3933 | 191 | assert(ATN && "not an assumed template name"); |
3934 | | |
3935 | 0 | LookupResult R(*this, ATN->getDeclName(), NameLoc, LookupOrdinaryName); |
3936 | 191 | struct CandidateCallback : CorrectionCandidateCallback { |
3937 | 191 | bool ValidateCandidate(const TypoCorrection &TC) override { |
3938 | 48 | return TC.getCorrectionDecl() && |
3939 | 48 | getAsTypeTemplateDecl(TC.getCorrectionDecl())47 ; |
3940 | 48 | } |
3941 | 191 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
3942 | 140 | return std::make_unique<CandidateCallback>(*this); |
3943 | 140 | } |
3944 | 191 | } FilterCCC; |
3945 | | |
3946 | 191 | TypoCorrection Corrected = |
3947 | 191 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr, |
3948 | 191 | FilterCCC, CTK_ErrorRecovery); |
3949 | 191 | if (Corrected && Corrected.getFoundDecl()36 ) { |
3950 | 36 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) |
3951 | 36 | << ATN->getDeclName()); |
3952 | 36 | Name = TemplateName(Corrected.getCorrectionDeclAs<TemplateDecl>()); |
3953 | 36 | return false; |
3954 | 36 | } |
3955 | | |
3956 | 155 | if (Diagnose) |
3957 | 138 | Diag(R.getNameLoc(), diag::err_no_template) << R.getLookupName(); |
3958 | 155 | return true; |
3959 | 191 | } |
3960 | | |
3961 | | TypeResult Sema::ActOnTemplateIdType( |
3962 | | Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
3963 | | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
3964 | | SourceLocation TemplateIILoc, SourceLocation LAngleLoc, |
3965 | | ASTTemplateArgsPtr TemplateArgsIn, SourceLocation RAngleLoc, |
3966 | 1.55M | bool IsCtorOrDtorName, bool IsClassName) { |
3967 | 1.55M | if (SS.isInvalid()) |
3968 | 0 | return true; |
3969 | | |
3970 | 1.55M | if (!IsCtorOrDtorName && !IsClassName1.55M && SS.isSet()1.40M ) { |
3971 | 57.3k | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
3972 | | |
3973 | | // C++ [temp.res]p3: |
3974 | | // A qualified-id that refers to a type and in which the |
3975 | | // nested-name-specifier depends on a template-parameter (14.6.2) |
3976 | | // shall be prefixed by the keyword typename to indicate that the |
3977 | | // qualified-id denotes a type, forming an |
3978 | | // elaborated-type-specifier (7.1.5.3). |
3979 | 57.3k | if (!LookupCtx && isDependentScopeSpecifier(SS)7 ) { |
3980 | 7 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
3981 | 7 | << SS.getScopeRep() << TemplateII->getName(); |
3982 | | // Recover as if 'typename' were specified. |
3983 | | // FIXME: This is not quite correct recovery as we don't transform SS |
3984 | | // into the corresponding dependent form (and we don't diagnose missing |
3985 | | // 'template' keywords within SS as a result). |
3986 | 7 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
3987 | 7 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
3988 | 7 | TemplateArgsIn, RAngleLoc); |
3989 | 7 | } |
3990 | | |
3991 | | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
3992 | | // it's not actually allowed to be used as a type in most cases. Because |
3993 | | // we annotate it before we know whether it's valid, we have to check for |
3994 | | // this case here. |
3995 | 57.3k | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
3996 | 57.3k | if (LookupRD && LookupRD->getIdentifier() == TemplateII500 ) { |
3997 | 16 | Diag(TemplateIILoc, |
3998 | 16 | TemplateKWLoc.isInvalid() |
3999 | 16 | ? diag::err_out_of_line_qualified_id_type_names_constructor12 |
4000 | 16 | : diag::ext_out_of_line_qualified_id_type_names_constructor4 ) |
4001 | 16 | << TemplateII << 0 /*injected-class-name used as template name*/ |
4002 | 16 | << 1 /*if any keyword was present, it was 'template'*/; |
4003 | 16 | } |
4004 | 57.3k | } |
4005 | | |
4006 | 1.55M | TemplateName Template = TemplateD.get(); |
4007 | 1.55M | if (Template.getAsAssumedTemplateName() && |
4008 | 1.55M | resolveAssumedTemplateNameAsType(S, Template, TemplateIILoc)148 ) |
4009 | 118 | return true; |
4010 | | |
4011 | | // Translate the parser's template argument list in our AST format. |
4012 | 1.55M | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
4013 | 1.55M | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
4014 | | |
4015 | 1.55M | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
4016 | 60 | QualType T |
4017 | 60 | = Context.getDependentTemplateSpecializationType(ETK_None, |
4018 | 60 | DTN->getQualifier(), |
4019 | 60 | DTN->getIdentifier(), |
4020 | 60 | TemplateArgs); |
4021 | | // Build type-source information. |
4022 | 60 | TypeLocBuilder TLB; |
4023 | 60 | DependentTemplateSpecializationTypeLoc SpecTL |
4024 | 60 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
4025 | 60 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
4026 | 60 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4027 | 60 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4028 | 60 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
4029 | 60 | SpecTL.setLAngleLoc(LAngleLoc); |
4030 | 60 | SpecTL.setRAngleLoc(RAngleLoc); |
4031 | 120 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I60 ) |
4032 | 60 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
4033 | 60 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
4034 | 60 | } |
4035 | | |
4036 | 1.55M | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
4037 | 1.55M | if (Result.isNull()) |
4038 | 1.27k | return true; |
4039 | | |
4040 | | // Build type-source information. |
4041 | 1.55M | TypeLocBuilder TLB; |
4042 | 1.55M | TemplateSpecializationTypeLoc SpecTL |
4043 | 1.55M | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
4044 | 1.55M | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4045 | 1.55M | SpecTL.setTemplateNameLoc(TemplateIILoc); |
4046 | 1.55M | SpecTL.setLAngleLoc(LAngleLoc); |
4047 | 1.55M | SpecTL.setRAngleLoc(RAngleLoc); |
4048 | 4.05M | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i2.49M ) |
4049 | 2.49M | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
4050 | | |
4051 | | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
4052 | | // constructor or destructor name (in such a case, the scope specifier |
4053 | | // will be attached to the enclosing Decl or Expr node). |
4054 | 1.55M | if (SS.isNotEmpty() && !IsCtorOrDtorName60.7k ) { |
4055 | | // Create an elaborated-type-specifier containing the nested-name-specifier. |
4056 | 60.7k | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
4057 | 60.7k | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
4058 | 60.7k | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
4059 | 60.7k | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4060 | 60.7k | } |
4061 | | |
4062 | 1.55M | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
4063 | 1.55M | } |
4064 | | |
4065 | | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
4066 | | TypeSpecifierType TagSpec, |
4067 | | SourceLocation TagLoc, |
4068 | | CXXScopeSpec &SS, |
4069 | | SourceLocation TemplateKWLoc, |
4070 | | TemplateTy TemplateD, |
4071 | | SourceLocation TemplateLoc, |
4072 | | SourceLocation LAngleLoc, |
4073 | | ASTTemplateArgsPtr TemplateArgsIn, |
4074 | 7.15k | SourceLocation RAngleLoc) { |
4075 | 7.15k | if (SS.isInvalid()) |
4076 | 0 | return TypeResult(true); |
4077 | | |
4078 | 7.15k | TemplateName Template = TemplateD.get(); |
4079 | | |
4080 | | // Translate the parser's template argument list in our AST format. |
4081 | 7.15k | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
4082 | 7.15k | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
4083 | | |
4084 | | // Determine the tag kind |
4085 | 7.15k | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
4086 | 7.15k | ElaboratedTypeKeyword Keyword |
4087 | 7.15k | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
4088 | | |
4089 | 7.15k | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
4090 | 5 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
4091 | 5 | DTN->getQualifier(), |
4092 | 5 | DTN->getIdentifier(), |
4093 | 5 | TemplateArgs); |
4094 | | |
4095 | | // Build type-source information. |
4096 | 5 | TypeLocBuilder TLB; |
4097 | 5 | DependentTemplateSpecializationTypeLoc SpecTL |
4098 | 5 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
4099 | 5 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
4100 | 5 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4101 | 5 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4102 | 5 | SpecTL.setTemplateNameLoc(TemplateLoc); |
4103 | 5 | SpecTL.setLAngleLoc(LAngleLoc); |
4104 | 5 | SpecTL.setRAngleLoc(RAngleLoc); |
4105 | 10 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I5 ) |
4106 | 5 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
4107 | 5 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
4108 | 5 | } |
4109 | | |
4110 | 7.15k | if (TypeAliasTemplateDecl *TAT = |
4111 | 7.15k | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
4112 | | // C++0x [dcl.type.elab]p2: |
4113 | | // If the identifier resolves to a typedef-name or the simple-template-id |
4114 | | // resolves to an alias template specialization, the |
4115 | | // elaborated-type-specifier is ill-formed. |
4116 | 2 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
4117 | 2 | << TAT << NTK_TypeAliasTemplate << TagKind; |
4118 | 2 | Diag(TAT->getLocation(), diag::note_declared_at); |
4119 | 2 | } |
4120 | | |
4121 | 7.15k | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
4122 | 7.15k | if (Result.isNull()) |
4123 | 15 | return TypeResult(true); |
4124 | | |
4125 | | // Check the tag kind |
4126 | 7.13k | if (const RecordType *RT = Result->getAs<RecordType>()) { |
4127 | 1.02k | RecordDecl *D = RT->getDecl(); |
4128 | | |
4129 | 1.02k | IdentifierInfo *Id = D->getIdentifier(); |
4130 | 1.02k | assert(Id && "templated class must have an identifier"); |
4131 | | |
4132 | 1.02k | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
4133 | 1.02k | TagLoc, Id)) { |
4134 | 4 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
4135 | 4 | << Result |
4136 | 4 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
4137 | 4 | Diag(D->getLocation(), diag::note_previous_use); |
4138 | 4 | } |
4139 | 1.02k | } |
4140 | | |
4141 | | // Provide source-location information for the template specialization. |
4142 | 0 | TypeLocBuilder TLB; |
4143 | 7.13k | TemplateSpecializationTypeLoc SpecTL |
4144 | 7.13k | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
4145 | 7.13k | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4146 | 7.13k | SpecTL.setTemplateNameLoc(TemplateLoc); |
4147 | 7.13k | SpecTL.setLAngleLoc(LAngleLoc); |
4148 | 7.13k | SpecTL.setRAngleLoc(RAngleLoc); |
4149 | 16.9k | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i9.81k ) |
4150 | 9.81k | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
4151 | | |
4152 | | // Construct an elaborated type containing the nested-name-specifier (if any) |
4153 | | // and tag keyword. |
4154 | 7.13k | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
4155 | 7.13k | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
4156 | 7.13k | ElabTL.setElaboratedKeywordLoc(TagLoc); |
4157 | 7.13k | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4158 | 7.13k | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
4159 | 7.15k | } |
4160 | | |
4161 | | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
4162 | | NamedDecl *PrevDecl, |
4163 | | SourceLocation Loc, |
4164 | | bool IsPartialSpecialization); |
4165 | | |
4166 | | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
4167 | | |
4168 | | static bool isTemplateArgumentTemplateParameter( |
4169 | 538 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
4170 | 538 | switch (Arg.getKind()) { |
4171 | 0 | case TemplateArgument::Null: |
4172 | 0 | case TemplateArgument::NullPtr: |
4173 | 10 | case TemplateArgument::Integral: |
4174 | 10 | case TemplateArgument::Declaration: |
4175 | 10 | case TemplateArgument::Pack: |
4176 | 10 | case TemplateArgument::TemplateExpansion: |
4177 | 10 | return false; |
4178 | | |
4179 | 518 | case TemplateArgument::Type: { |
4180 | 518 | QualType Type = Arg.getAsType(); |
4181 | 518 | const TemplateTypeParmType *TPT = |
4182 | 518 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
4183 | 518 | return TPT && !Type.hasQualifiers()162 && |
4184 | 518 | TPT->getDepth() == Depth162 && TPT->getIndex() == Index160 ; |
4185 | 10 | } |
4186 | | |
4187 | 8 | case TemplateArgument::Expression: { |
4188 | 8 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
4189 | 8 | if (!DRE || !DRE->getDecl()) |
4190 | 0 | return false; |
4191 | 8 | const NonTypeTemplateParmDecl *NTTP = |
4192 | 8 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
4193 | 8 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
4194 | 8 | } |
4195 | | |
4196 | 2 | case TemplateArgument::Template: |
4197 | 2 | const TemplateTemplateParmDecl *TTP = |
4198 | 2 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
4199 | 2 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
4200 | 2 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
4201 | 538 | } |
4202 | 0 | llvm_unreachable("unexpected kind of template argument"); |
4203 | 0 | } |
4204 | | |
4205 | | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
4206 | 441 | ArrayRef<TemplateArgument> Args) { |
4207 | 441 | if (Params->size() != Args.size()) |
4208 | 6 | return false; |
4209 | | |
4210 | 435 | unsigned Depth = Params->getDepth(); |
4211 | | |
4212 | 571 | for (unsigned I = 0, N = Args.size(); I != N; ++I136 ) { |
4213 | 540 | TemplateArgument Arg = Args[I]; |
4214 | | |
4215 | | // If the parameter is a pack expansion, the argument must be a pack |
4216 | | // whose only element is a pack expansion. |
4217 | 540 | if (Params->getParam(I)->isParameterPack()) { |
4218 | 5 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
4219 | 5 | !Arg.pack_begin()->isPackExpansion()) |
4220 | 2 | return false; |
4221 | 3 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
4222 | 3 | } |
4223 | | |
4224 | 538 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
4225 | 402 | return false; |
4226 | 538 | } |
4227 | | |
4228 | 31 | return true; |
4229 | 435 | } |
4230 | | |
4231 | | template<typename PartialSpecDecl> |
4232 | 193k | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
4233 | 193k | if (Partial->getDeclContext()->isDependentContext()) |
4234 | 5.41k | return; |
4235 | | |
4236 | | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
4237 | | // for non-substitution-failure issues? |
4238 | 187k | TemplateDeductionInfo Info(Partial->getLocation()); |
4239 | 187k | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
4240 | 187k | return; |
4241 | | |
4242 | 342 | auto *Template = Partial->getSpecializedTemplate(); |
4243 | 342 | S.Diag(Partial->getLocation(), |
4244 | 342 | diag::ext_partial_spec_not_more_specialized_than_primary) |
4245 | 342 | << isa<VarTemplateDecl>(Template); |
4246 | | |
4247 | 342 | if (Info.hasSFINAEDiagnostic()) { |
4248 | 3 | PartialDiagnosticAt Diag = {SourceLocation(), |
4249 | 3 | PartialDiagnostic::NullDiagnostic()}; |
4250 | 3 | Info.takeSFINAEDiagnostic(Diag); |
4251 | 3 | SmallString<128> SFINAEArgString; |
4252 | 3 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
4253 | 3 | S.Diag(Diag.first, |
4254 | 3 | diag::note_partial_spec_not_more_specialized_than_primary) |
4255 | 3 | << SFINAEArgString; |
4256 | 3 | } |
4257 | | |
4258 | 342 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
4259 | 342 | SmallVector<const Expr *, 3> PartialAC, TemplateAC; |
4260 | 342 | Template->getAssociatedConstraints(TemplateAC); |
4261 | 342 | Partial->getAssociatedConstraints(PartialAC); |
4262 | 342 | S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Partial, PartialAC, Template, |
4263 | 342 | TemplateAC); |
4264 | 342 | } SemaTemplate.cpp:void checkMoreSpecializedThanPrimary<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*) Line | Count | Source | 4232 | 192k | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { | 4233 | 192k | if (Partial->getDeclContext()->isDependentContext()) | 4234 | 5.34k | return; | 4235 | | | 4236 | | // FIXME: Get the TDK from deduction in order to provide better diagnostics | 4237 | | // for non-substitution-failure issues? | 4238 | 187k | TemplateDeductionInfo Info(Partial->getLocation()); | 4239 | 187k | if (S.isMoreSpecializedThanPrimary(Partial, Info)) | 4240 | 186k | return; | 4241 | | | 4242 | 332 | auto *Template = Partial->getSpecializedTemplate(); | 4243 | 332 | S.Diag(Partial->getLocation(), | 4244 | 332 | diag::ext_partial_spec_not_more_specialized_than_primary) | 4245 | 332 | << isa<VarTemplateDecl>(Template); | 4246 | | | 4247 | 332 | if (Info.hasSFINAEDiagnostic()) { | 4248 | 3 | PartialDiagnosticAt Diag = {SourceLocation(), | 4249 | 3 | PartialDiagnostic::NullDiagnostic()}; | 4250 | 3 | Info.takeSFINAEDiagnostic(Diag); | 4251 | 3 | SmallString<128> SFINAEArgString; | 4252 | 3 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); | 4253 | 3 | S.Diag(Diag.first, | 4254 | 3 | diag::note_partial_spec_not_more_specialized_than_primary) | 4255 | 3 | << SFINAEArgString; | 4256 | 3 | } | 4257 | | | 4258 | 332 | S.Diag(Template->getLocation(), diag::note_template_decl_here); | 4259 | 332 | SmallVector<const Expr *, 3> PartialAC, TemplateAC; | 4260 | 332 | Template->getAssociatedConstraints(TemplateAC); | 4261 | 332 | Partial->getAssociatedConstraints(PartialAC); | 4262 | 332 | S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Partial, PartialAC, Template, | 4263 | 332 | TemplateAC); | 4264 | 332 | } |
SemaTemplate.cpp:void checkMoreSpecializedThanPrimary<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*) Line | Count | Source | 4232 | 471 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { | 4233 | 471 | if (Partial->getDeclContext()->isDependentContext()) | 4234 | 69 | return; | 4235 | | | 4236 | | // FIXME: Get the TDK from deduction in order to provide better diagnostics | 4237 | | // for non-substitution-failure issues? | 4238 | 402 | TemplateDeductionInfo Info(Partial->getLocation()); | 4239 | 402 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) | 4240 | 392 | return; | 4241 | | | 4242 | 10 | auto *Template = Partial->getSpecializedTemplate(); | 4243 | 10 | S.Diag(Partial->getLocation(), | 4244 | 10 | diag::ext_partial_spec_not_more_specialized_than_primary) | 4245 | 10 | << isa<VarTemplateDecl>(Template); | 4246 | | | 4247 | 10 | if (Info.hasSFINAEDiagnostic()) { | 4248 | 0 | PartialDiagnosticAt Diag = {SourceLocation(), | 4249 | 0 | PartialDiagnostic::NullDiagnostic()}; | 4250 | 0 | Info.takeSFINAEDiagnostic(Diag); | 4251 | 0 | SmallString<128> SFINAEArgString; | 4252 | 0 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); | 4253 | 0 | S.Diag(Diag.first, | 4254 | 0 | diag::note_partial_spec_not_more_specialized_than_primary) | 4255 | 0 | << SFINAEArgString; | 4256 | 0 | } | 4257 | | | 4258 | 10 | S.Diag(Template->getLocation(), diag::note_template_decl_here); | 4259 | 10 | SmallVector<const Expr *, 3> PartialAC, TemplateAC; | 4260 | 10 | Template->getAssociatedConstraints(TemplateAC); | 4261 | 10 | Partial->getAssociatedConstraints(PartialAC); | 4262 | 10 | S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Partial, PartialAC, Template, | 4263 | 10 | TemplateAC); | 4264 | 10 | } |
|
4265 | | |
4266 | | static void |
4267 | | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
4268 | 26 | const llvm::SmallBitVector &DeducibleParams) { |
4269 | 60 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I34 ) { |
4270 | 34 | if (!DeducibleParams[I]) { |
4271 | 28 | NamedDecl *Param = TemplateParams->getParam(I); |
4272 | 28 | if (Param->getDeclName()) |
4273 | 27 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
4274 | 27 | << Param->getDeclName(); |
4275 | 1 | else |
4276 | 1 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
4277 | 1 | << "(anonymous)"; |
4278 | 28 | } |
4279 | 34 | } |
4280 | 26 | } |
4281 | | |
4282 | | |
4283 | | template<typename PartialSpecDecl> |
4284 | | static void checkTemplatePartialSpecialization(Sema &S, |
4285 | 193k | PartialSpecDecl *Partial) { |
4286 | | // C++1z [temp.class.spec]p8: (DR1495) |
4287 | | // - The specialization shall be more specialized than the primary |
4288 | | // template (14.5.5.2). |
4289 | 193k | checkMoreSpecializedThanPrimary(S, Partial); |
4290 | | |
4291 | | // C++ [temp.class.spec]p8: (DR1315) |
4292 | | // - Each template-parameter shall appear at least once in the |
4293 | | // template-id outside a non-deduced context. |
4294 | | // C++1z [temp.class.spec.match]p3 (P0127R2) |
4295 | | // If the template arguments of a partial specialization cannot be |
4296 | | // deduced because of the structure of its template-parameter-list |
4297 | | // and the template-id, the program is ill-formed. |
4298 | 193k | auto *TemplateParams = Partial->getTemplateParameters(); |
4299 | 193k | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
4300 | 193k | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
4301 | 193k | TemplateParams->getDepth(), DeducibleParams); |
4302 | | |
4303 | 193k | if (!DeducibleParams.all()) { |
4304 | 21 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
4305 | 21 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
4306 | 21 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
4307 | 21 | << (NumNonDeducible > 1) |
4308 | 21 | << SourceRange(Partial->getLocation(), |
4309 | 21 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
4310 | 21 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
4311 | 21 | } |
4312 | 193k | } SemaTemplate.cpp:void checkTemplatePartialSpecialization<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*) Line | Count | Source | 4285 | 192k | PartialSpecDecl *Partial) { | 4286 | | // C++1z [temp.class.spec]p8: (DR1495) | 4287 | | // - The specialization shall be more specialized than the primary | 4288 | | // template (14.5.5.2). | 4289 | 192k | checkMoreSpecializedThanPrimary(S, Partial); | 4290 | | | 4291 | | // C++ [temp.class.spec]p8: (DR1315) | 4292 | | // - Each template-parameter shall appear at least once in the | 4293 | | // template-id outside a non-deduced context. | 4294 | | // C++1z [temp.class.spec.match]p3 (P0127R2) | 4295 | | // If the template arguments of a partial specialization cannot be | 4296 | | // deduced because of the structure of its template-parameter-list | 4297 | | // and the template-id, the program is ill-formed. | 4298 | 192k | auto *TemplateParams = Partial->getTemplateParameters(); | 4299 | 192k | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | 4300 | 192k | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | 4301 | 192k | TemplateParams->getDepth(), DeducibleParams); | 4302 | | | 4303 | 192k | if (!DeducibleParams.all()) { | 4304 | 16 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | 4305 | 16 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) | 4306 | 16 | << isa<VarTemplatePartialSpecializationDecl>(Partial) | 4307 | 16 | << (NumNonDeducible > 1) | 4308 | 16 | << SourceRange(Partial->getLocation(), | 4309 | 16 | Partial->getTemplateArgsAsWritten()->RAngleLoc); | 4310 | 16 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); | 4311 | 16 | } | 4312 | 192k | } |
SemaTemplate.cpp:void checkTemplatePartialSpecialization<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*) Line | Count | Source | 4285 | 471 | PartialSpecDecl *Partial) { | 4286 | | // C++1z [temp.class.spec]p8: (DR1495) | 4287 | | // - The specialization shall be more specialized than the primary | 4288 | | // template (14.5.5.2). | 4289 | 471 | checkMoreSpecializedThanPrimary(S, Partial); | 4290 | | | 4291 | | // C++ [temp.class.spec]p8: (DR1315) | 4292 | | // - Each template-parameter shall appear at least once in the | 4293 | | // template-id outside a non-deduced context. | 4294 | | // C++1z [temp.class.spec.match]p3 (P0127R2) | 4295 | | // If the template arguments of a partial specialization cannot be | 4296 | | // deduced because of the structure of its template-parameter-list | 4297 | | // and the template-id, the program is ill-formed. | 4298 | 471 | auto *TemplateParams = Partial->getTemplateParameters(); | 4299 | 471 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | 4300 | 471 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | 4301 | 471 | TemplateParams->getDepth(), DeducibleParams); | 4302 | | | 4303 | 471 | if (!DeducibleParams.all()) { | 4304 | 5 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | 4305 | 5 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) | 4306 | 5 | << isa<VarTemplatePartialSpecializationDecl>(Partial) | 4307 | 5 | << (NumNonDeducible > 1) | 4308 | 5 | << SourceRange(Partial->getLocation(), | 4309 | 5 | Partial->getTemplateArgsAsWritten()->RAngleLoc); | 4310 | 5 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); | 4311 | 5 | } | 4312 | 471 | } |
|
4313 | | |
4314 | | void Sema::CheckTemplatePartialSpecialization( |
4315 | 192k | ClassTemplatePartialSpecializationDecl *Partial) { |
4316 | 192k | checkTemplatePartialSpecialization(*this, Partial); |
4317 | 192k | } |
4318 | | |
4319 | | void Sema::CheckTemplatePartialSpecialization( |
4320 | 471 | VarTemplatePartialSpecializationDecl *Partial) { |
4321 | 471 | checkTemplatePartialSpecialization(*this, Partial); |
4322 | 471 | } |
4323 | | |
4324 | 767 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
4325 | | // C++1z [temp.param]p11: |
4326 | | // A template parameter of a deduction guide template that does not have a |
4327 | | // default-argument shall be deducible from the parameter-type-list of the |
4328 | | // deduction guide template. |
4329 | 767 | auto *TemplateParams = TD->getTemplateParameters(); |
4330 | 767 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
4331 | 767 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
4332 | 3.61k | for (unsigned I = 0; I != TemplateParams->size(); ++I2.84k ) { |
4333 | | // A parameter pack is deducible (to an empty pack). |
4334 | 2.84k | auto *Param = TemplateParams->getParam(I); |
4335 | 2.84k | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)2.77k ) |
4336 | 1.50k | DeducibleParams[I] = true; |
4337 | 2.84k | } |
4338 | | |
4339 | 767 | if (!DeducibleParams.all()) { |
4340 | 5 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
4341 | 5 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
4342 | 5 | << (NumNonDeducible > 1); |
4343 | 5 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
4344 | 5 | } |
4345 | 767 | } |
4346 | | |
4347 | | DeclResult Sema::ActOnVarTemplateSpecialization( |
4348 | | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
4349 | | TemplateParameterList *TemplateParams, StorageClass SC, |
4350 | 1.02k | bool IsPartialSpecialization) { |
4351 | | // D must be variable template id. |
4352 | 1.02k | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
4353 | 1.02k | "Variable template specialization is declared with a template id."); |
4354 | | |
4355 | 0 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
4356 | 1.02k | TemplateArgumentListInfo TemplateArgs = |
4357 | 1.02k | makeTemplateArgumentListInfo(*this, *TemplateId); |
4358 | 1.02k | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
4359 | 1.02k | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
4360 | 1.02k | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
4361 | | |
4362 | 1.02k | TemplateName Name = TemplateId->Template.get(); |
4363 | | |
4364 | | // The template-id must name a variable template. |
4365 | 1.02k | VarTemplateDecl *VarTemplate = |
4366 | 1.02k | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
4367 | 1.02k | if (!VarTemplate) { |
4368 | 9 | NamedDecl *FnTemplate; |
4369 | 9 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
4370 | 3 | FnTemplate = *OTS->begin(); |
4371 | 6 | else |
4372 | 6 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
4373 | 9 | if (FnTemplate) |
4374 | 6 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
4375 | 6 | << FnTemplate->getDeclName(); |
4376 | 3 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
4377 | 3 | << IsPartialSpecialization; |
4378 | 9 | } |
4379 | | |
4380 | | // Check for unexpanded parameter packs in any of the template arguments. |
4381 | 2.19k | for (unsigned I = 0, N = TemplateArgs.size(); 1.01k I != N; ++I1.18k ) |
4382 | 1.18k | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
4383 | 1.18k | UPPC_PartialSpecialization)) |
4384 | 0 | return true; |
4385 | | |
4386 | | // Check that the template argument list is well-formed for this |
4387 | | // template. |
4388 | 1.01k | SmallVector<TemplateArgument, 4> Converted; |
4389 | 1.01k | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
4390 | 1.01k | false, Converted, |
4391 | 1.01k | /*UpdateArgsWithConversions=*/true)) |
4392 | 0 | return true; |
4393 | | |
4394 | | // Find the variable template (partial) specialization declaration that |
4395 | | // corresponds to these arguments. |
4396 | 1.01k | if (IsPartialSpecialization) { |
4397 | 441 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
4398 | 441 | TemplateArgs.size(), Converted)) |
4399 | 0 | return true; |
4400 | | |
4401 | | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
4402 | | // also do them during instantiation. |
4403 | 441 | if (!Name.isDependent() && |
4404 | 441 | !TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs, |
4405 | 370 | Converted)) { |
4406 | 0 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
4407 | 0 | << VarTemplate->getDeclName(); |
4408 | 0 | IsPartialSpecialization = false; |
4409 | 0 | } |
4410 | | |
4411 | 441 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
4412 | 441 | Converted) && |
4413 | 441 | (31 !Context.getLangOpts().CPlusPlus2031 || |
4414 | 31 | !TemplateParams->hasAssociatedConstraints()24 )) { |
4415 | | // C++ [temp.class.spec]p9b3: |
4416 | | // |
4417 | | // -- The argument list of the specialization shall not be identical |
4418 | | // to the implicit argument list of the primary template. |
4419 | 9 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
4420 | 9 | << /*variable template*/ 1 |
4421 | 9 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
4422 | 9 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
4423 | | // FIXME: Recover from this by treating the declaration as a redeclaration |
4424 | | // of the primary template. |
4425 | 9 | return true; |
4426 | 9 | } |
4427 | 441 | } |
4428 | | |
4429 | 1.00k | void *InsertPos = nullptr; |
4430 | 1.00k | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
4431 | | |
4432 | 1.00k | if (IsPartialSpecialization) |
4433 | 432 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, TemplateParams, |
4434 | 432 | InsertPos); |
4435 | 573 | else |
4436 | 573 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
4437 | | |
4438 | 1.00k | VarTemplateSpecializationDecl *Specialization = nullptr; |
4439 | | |
4440 | | // Check whether we can declare a variable template specialization in |
4441 | | // the current scope. |
4442 | 1.00k | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
4443 | 1.00k | TemplateNameLoc, |
4444 | 1.00k | IsPartialSpecialization)) |
4445 | 20 | return true; |
4446 | | |
4447 | 985 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared118 ) { |
4448 | | // Since the only prior variable template specialization with these |
4449 | | // arguments was referenced but not declared, reuse that |
4450 | | // declaration node as our own, updating its source location and |
4451 | | // the list of outer template parameters to reflect our new declaration. |
4452 | 0 | Specialization = PrevDecl; |
4453 | 0 | Specialization->setLocation(TemplateNameLoc); |
4454 | 0 | PrevDecl = nullptr; |
4455 | 985 | } else if (IsPartialSpecialization) { |
4456 | | // Create a new class template partial specialization declaration node. |
4457 | 422 | VarTemplatePartialSpecializationDecl *PrevPartial = |
4458 | 422 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
4459 | 422 | VarTemplatePartialSpecializationDecl *Partial = |
4460 | 422 | VarTemplatePartialSpecializationDecl::Create( |
4461 | 422 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
4462 | 422 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
4463 | 422 | Converted, TemplateArgs); |
4464 | | |
4465 | 422 | if (!PrevPartial) |
4466 | 357 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
4467 | 422 | Specialization = Partial; |
4468 | | |
4469 | | // If we are providing an explicit specialization of a member variable |
4470 | | // template specialization, make a note of that. |
4471 | 422 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()65 ) |
4472 | 21 | PrevPartial->setMemberSpecialization(); |
4473 | | |
4474 | 422 | CheckTemplatePartialSpecialization(Partial); |
4475 | 563 | } else { |
4476 | | // Create a new class template specialization declaration node for |
4477 | | // this explicit specialization or friend declaration. |
4478 | 563 | Specialization = VarTemplateSpecializationDecl::Create( |
4479 | 563 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
4480 | 563 | VarTemplate, DI->getType(), DI, SC, Converted); |
4481 | 563 | Specialization->setTemplateArgsInfo(TemplateArgs); |
4482 | | |
4483 | 563 | if (!PrevDecl) |
4484 | 510 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
4485 | 563 | } |
4486 | | |
4487 | | // C++ [temp.expl.spec]p6: |
4488 | | // If a template, a member template or the member of a class template is |
4489 | | // explicitly specialized then that specialization shall be declared |
4490 | | // before the first use of that specialization that would cause an implicit |
4491 | | // instantiation to take place, in every translation unit in which such a |
4492 | | // use occurs; no diagnostic is required. |
4493 | 985 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()118 ) { |
4494 | 6 | bool Okay = false; |
4495 | 12 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()6 ) { |
4496 | | // Is there any previous explicit specialization declaration? |
4497 | 6 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
4498 | 0 | Okay = true; |
4499 | 0 | break; |
4500 | 0 | } |
4501 | 6 | } |
4502 | | |
4503 | 6 | if (!Okay) { |
4504 | 6 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
4505 | 6 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
4506 | 6 | << Name << Range; |
4507 | | |
4508 | 6 | Diag(PrevDecl->getPointOfInstantiation(), |
4509 | 6 | diag::note_instantiation_required_here) |
4510 | 6 | << (PrevDecl->getTemplateSpecializationKind() != |
4511 | 6 | TSK_ImplicitInstantiation); |
4512 | 6 | return true; |
4513 | 6 | } |
4514 | 6 | } |
4515 | | |
4516 | 979 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
4517 | 979 | Specialization->setLexicalDeclContext(CurContext); |
4518 | | |
4519 | | // Add the specialization into its lexical context, so that it can |
4520 | | // be seen when iterating through the list of declarations in that |
4521 | | // context. However, specializations are not found by name lookup. |
4522 | 979 | CurContext->addDecl(Specialization); |
4523 | | |
4524 | | // Note that this is an explicit specialization. |
4525 | 979 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
4526 | | |
4527 | 979 | if (PrevDecl) { |
4528 | | // Check that this isn't a redefinition of this specialization, |
4529 | | // merging with previous declarations. |
4530 | 112 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
4531 | 112 | forRedeclarationInCurContext()); |
4532 | 112 | PrevSpec.addDecl(PrevDecl); |
4533 | 112 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
4534 | 867 | } else if (Specialization->isStaticDataMember() && |
4535 | 867 | Specialization->isOutOfLine()328 ) { |
4536 | 163 | Specialization->setAccess(VarTemplate->getAccess()); |
4537 | 163 | } |
4538 | | |
4539 | 979 | return Specialization; |
4540 | 985 | } |
4541 | | |
4542 | | namespace { |
4543 | | /// A partial specialization whose template arguments have matched |
4544 | | /// a given template-id. |
4545 | | struct PartialSpecMatchResult { |
4546 | | VarTemplatePartialSpecializationDecl *Partial; |
4547 | | TemplateArgumentList *Args; |
4548 | | }; |
4549 | | } // end anonymous namespace |
4550 | | |
4551 | | DeclResult |
4552 | | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
4553 | | SourceLocation TemplateNameLoc, |
4554 | 10.6k | const TemplateArgumentListInfo &TemplateArgs) { |
4555 | 10.6k | assert(Template && "A variable template id without template?"); |
4556 | | |
4557 | | // Check that the template argument list is well-formed for this template. |
4558 | 0 | SmallVector<TemplateArgument, 4> Converted; |
4559 | 10.6k | if (CheckTemplateArgumentList( |
4560 | 10.6k | Template, TemplateNameLoc, |
4561 | 10.6k | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
4562 | 10.6k | Converted, /*UpdateArgsWithConversions=*/true)) |
4563 | 14 | return true; |
4564 | | |
4565 | | // Produce a placeholder value if the specialization is dependent. |
4566 | 10.6k | if (Template->getDeclContext()->isDependentContext() || |
4567 | 10.6k | TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs, |
4568 | 10.6k | Converted)) |
4569 | 4.27k | return DeclResult(); |
4570 | | |
4571 | | // Find the variable template specialization declaration that |
4572 | | // corresponds to these arguments. |
4573 | 6.35k | void *InsertPos = nullptr; |
4574 | 6.35k | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
4575 | 6.35k | Converted, InsertPos)) { |
4576 | 2.13k | checkSpecializationVisibility(TemplateNameLoc, Spec); |
4577 | | // If we already have a variable template specialization, return it. |
4578 | 2.13k | return Spec; |
4579 | 2.13k | } |
4580 | | |
4581 | | // This is the first time we have referenced this variable template |
4582 | | // specialization. Create the canonical declaration and add it to |
4583 | | // the set of specializations, based on the closest partial specialization |
4584 | | // that it represents. That is, |
4585 | 4.21k | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
4586 | 4.21k | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
4587 | 4.21k | Converted); |
4588 | 4.21k | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
4589 | 4.21k | bool AmbiguousPartialSpec = false; |
4590 | 4.21k | typedef PartialSpecMatchResult MatchResult; |
4591 | 4.21k | SmallVector<MatchResult, 4> Matched; |
4592 | 4.21k | SourceLocation PointOfInstantiation = TemplateNameLoc; |
4593 | 4.21k | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
4594 | 4.21k | /*ForTakingAddress=*/false); |
4595 | | |
4596 | | // 1. Attempt to find the closest partial specialization that this |
4597 | | // specializes, if any. |
4598 | | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
4599 | | // Perhaps better after unification of DeduceTemplateArguments() and |
4600 | | // getMoreSpecializedPartialSpecialization(). |
4601 | 4.21k | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
4602 | 4.21k | Template->getPartialSpecializations(PartialSpecs); |
4603 | | |
4604 | 5.35k | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I1.13k ) { |
4605 | 1.13k | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
4606 | 1.13k | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
4607 | | |
4608 | 1.13k | if (TemplateDeductionResult Result = |
4609 | 1.13k | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
4610 | | // Store the failed-deduction information for use in diagnostics, later. |
4611 | | // TODO: Actually use the failed-deduction info? |
4612 | 287 | FailedCandidates.addCandidate().set( |
4613 | 287 | DeclAccessPair::make(Template, AS_public), Partial, |
4614 | 287 | MakeDeductionFailureInfo(Context, Result, Info)); |
4615 | 287 | (void)Result; |
4616 | 850 | } else { |
4617 | 850 | Matched.push_back(PartialSpecMatchResult()); |
4618 | 850 | Matched.back().Partial = Partial; |
4619 | 850 | Matched.back().Args = Info.take(); |
4620 | 850 | } |
4621 | 1.13k | } |
4622 | | |
4623 | 4.21k | if (Matched.size() >= 1) { |
4624 | 838 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
4625 | 838 | if (Matched.size() == 1) { |
4626 | | // -- If exactly one matching specialization is found, the |
4627 | | // instantiation is generated from that specialization. |
4628 | | // We don't need to do anything for this. |
4629 | 826 | } else { |
4630 | | // -- If more than one matching specialization is found, the |
4631 | | // partial order rules (14.5.4.2) are used to determine |
4632 | | // whether one of the specializations is more specialized |
4633 | | // than the others. If none of the specializations is more |
4634 | | // specialized than all of the other matching |
4635 | | // specializations, then the use of the variable template is |
4636 | | // ambiguous and the program is ill-formed. |
4637 | 12 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
4638 | 12 | PEnd = Matched.end(); |
4639 | 24 | P != PEnd; ++P12 ) { |
4640 | 12 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
4641 | 12 | PointOfInstantiation) == |
4642 | 12 | P->Partial) |
4643 | 8 | Best = P; |
4644 | 12 | } |
4645 | | |
4646 | | // Determine if the best partial specialization is more specialized than |
4647 | | // the others. |
4648 | 12 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
4649 | 12 | PEnd = Matched.end(); |
4650 | 33 | P != PEnd; ++P21 ) { |
4651 | 24 | if (P != Best && getMoreSpecializedPartialSpecialization( |
4652 | 12 | P->Partial, Best->Partial, |
4653 | 12 | PointOfInstantiation) != Best->Partial) { |
4654 | 3 | AmbiguousPartialSpec = true; |
4655 | 3 | break; |
4656 | 3 | } |
4657 | 24 | } |
4658 | 12 | } |
4659 | | |
4660 | | // Instantiate using the best variable template partial specialization. |
4661 | 838 | InstantiationPattern = Best->Partial; |
4662 | 838 | InstantiationArgs = Best->Args; |
4663 | 3.37k | } else { |
4664 | | // -- If no match is found, the instantiation is generated |
4665 | | // from the primary template. |
4666 | | // InstantiationPattern = Template->getTemplatedDecl(); |
4667 | 3.37k | } |
4668 | | |
4669 | | // 2. Create the canonical declaration. |
4670 | | // Note that we do not instantiate a definition until we see an odr-use |
4671 | | // in DoMarkVarDeclReferenced(). |
4672 | | // FIXME: LateAttrs et al.? |
4673 | 4.21k | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
4674 | 4.21k | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
4675 | 4.21k | Converted, TemplateNameLoc /*, LateAttrs, StartingScope*/); |
4676 | 4.21k | if (!Decl) |
4677 | 0 | return true; |
4678 | | |
4679 | 4.21k | if (AmbiguousPartialSpec) { |
4680 | | // Partial ordering did not produce a clear winner. Complain. |
4681 | 3 | Decl->setInvalidDecl(); |
4682 | 3 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
4683 | 3 | << Decl; |
4684 | | |
4685 | | // Print the matching partial specializations. |
4686 | 3 | for (MatchResult P : Matched) |
4687 | 6 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
4688 | 6 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
4689 | 6 | *P.Args); |
4690 | 3 | return true; |
4691 | 3 | } |
4692 | | |
4693 | 4.21k | if (VarTemplatePartialSpecializationDecl *D = |
4694 | 4.21k | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
4695 | 835 | Decl->setInstantiationOf(D, InstantiationArgs); |
4696 | | |
4697 | 4.21k | checkSpecializationVisibility(TemplateNameLoc, Decl); |
4698 | | |
4699 | 4.21k | assert(Decl && "No variable template specialization?"); |
4700 | 0 | return Decl; |
4701 | 4.21k | } |
4702 | | |
4703 | | ExprResult |
4704 | | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
4705 | | const DeclarationNameInfo &NameInfo, |
4706 | | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
4707 | 9.92k | const TemplateArgumentListInfo *TemplateArgs) { |
4708 | | |
4709 | 9.92k | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
4710 | 9.92k | *TemplateArgs); |
4711 | 9.92k | if (Decl.isInvalid()) |
4712 | 16 | return ExprError(); |
4713 | | |
4714 | 9.91k | if (!Decl.get()) |
4715 | 4.27k | return ExprResult(); |
4716 | | |
4717 | 5.64k | VarDecl *Var = cast<VarDecl>(Decl.get()); |
4718 | 5.64k | if (!Var->getTemplateSpecializationKind()) |
4719 | 3.61k | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
4720 | 3.61k | NameInfo.getLoc()); |
4721 | | |
4722 | | // Build an ordinary singleton decl ref. |
4723 | 5.64k | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
4724 | 5.64k | /*FoundD=*/nullptr, TemplateArgs); |
4725 | 9.91k | } |
4726 | | |
4727 | | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
4728 | 90 | SourceLocation Loc) { |
4729 | 90 | Diag(Loc, diag::err_template_missing_args) |
4730 | 90 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
4731 | 90 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
4732 | 90 | Diag(TD->getLocation(), diag::note_template_decl_here) |
4733 | 90 | << TD->getTemplateParameters()->getSourceRange(); |
4734 | 90 | } |
4735 | 90 | } |
4736 | | |
4737 | | ExprResult |
4738 | | Sema::CheckConceptTemplateId(const CXXScopeSpec &SS, |
4739 | | SourceLocation TemplateKWLoc, |
4740 | | const DeclarationNameInfo &ConceptNameInfo, |
4741 | | NamedDecl *FoundDecl, |
4742 | | ConceptDecl *NamedConcept, |
4743 | 19.3k | const TemplateArgumentListInfo *TemplateArgs) { |
4744 | 19.3k | assert(NamedConcept && "A concept template id without a template?"); |
4745 | | |
4746 | 0 | llvm::SmallVector<TemplateArgument, 4> Converted; |
4747 | 19.3k | if (CheckTemplateArgumentList(NamedConcept, ConceptNameInfo.getLoc(), |
4748 | 19.3k | const_cast<TemplateArgumentListInfo&>(*TemplateArgs), |
4749 | 19.3k | /*PartialTemplateArgs=*/false, Converted, |
4750 | 19.3k | /*UpdateArgsWithConversions=*/false)) |
4751 | 3 | return ExprError(); |
4752 | | |
4753 | 19.3k | ConstraintSatisfaction Satisfaction; |
4754 | 19.3k | bool AreArgsDependent = |
4755 | 19.3k | TemplateSpecializationType::anyDependentTemplateArguments(*TemplateArgs, |
4756 | 19.3k | Converted); |
4757 | 19.3k | if (!AreArgsDependent && |
4758 | 19.3k | CheckConstraintSatisfaction( |
4759 | 9.80k | NamedConcept, {NamedConcept->getConstraintExpr()}, Converted, |
4760 | 9.80k | SourceRange(SS.isSet() ? SS.getBeginLoc()177 : ConceptNameInfo.getLoc()9.62k , |
4761 | 9.80k | TemplateArgs->getRAngleLoc()), |
4762 | 9.80k | Satisfaction)) |
4763 | 6 | return ExprError(); |
4764 | | |
4765 | 19.3k | return ConceptSpecializationExpr::Create(Context, |
4766 | 19.3k | SS.isSet() ? SS.getWithLocInContext(Context)314 : NestedNameSpecifierLoc{}19.0k , |
4767 | 19.3k | TemplateKWLoc, ConceptNameInfo, FoundDecl, NamedConcept, |
4768 | 19.3k | ASTTemplateArgumentListInfo::Create(Context, *TemplateArgs), Converted, |
4769 | 19.3k | AreArgsDependent ? nullptr9.52k : &Satisfaction9.79k ); |
4770 | 19.3k | } |
4771 | | |
4772 | | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
4773 | | SourceLocation TemplateKWLoc, |
4774 | | LookupResult &R, |
4775 | | bool RequiresADL, |
4776 | 546k | const TemplateArgumentListInfo *TemplateArgs) { |
4777 | | // FIXME: Can we do any checking at this point? I guess we could check the |
4778 | | // template arguments that we have against the template name, if the template |
4779 | | // name refers to a single template. That's not a terribly common case, |
4780 | | // though. |
4781 | | // foo<int> could identify a single function unambiguously |
4782 | | // This approach does NOT work, since f<int>(1); |
4783 | | // gets resolved prior to resorting to overload resolution |
4784 | | // i.e., template<class T> void f(double); |
4785 | | // vs template<class T, class U> void f(U); |
4786 | | |
4787 | | // These should be filtered out by our callers. |
4788 | 546k | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
4789 | | |
4790 | | // Non-function templates require a template argument list. |
4791 | 546k | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
4792 | 13.5k | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)9 ) { |
4793 | 9 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
4794 | 9 | return ExprError(); |
4795 | 9 | } |
4796 | 13.5k | } |
4797 | | |
4798 | | // In C++1y, check variable template ids. |
4799 | 546k | if (R.getAsSingle<VarTemplateDecl>()) { |
4800 | 9.92k | ExprResult Res = CheckVarTemplateId(SS, R.getLookupNameInfo(), |
4801 | 9.92k | R.getAsSingle<VarTemplateDecl>(), |
4802 | 9.92k | TemplateKWLoc, TemplateArgs); |
4803 | 9.92k | if (Res.isInvalid() || Res.isUsable()9.91k ) |
4804 | 5.65k | return Res; |
4805 | | // Result is dependent. Carry on to build an UnresolvedLookupEpxr. |
4806 | 9.92k | } |
4807 | | |
4808 | 540k | if (R.getAsSingle<ConceptDecl>()) { |
4809 | 3.65k | return CheckConceptTemplateId(SS, TemplateKWLoc, R.getLookupNameInfo(), |
4810 | 3.65k | R.getFoundDecl(), |
4811 | 3.65k | R.getAsSingle<ConceptDecl>(), TemplateArgs); |
4812 | 3.65k | } |
4813 | | |
4814 | | // We don't want lookup warnings at this point. |
4815 | 537k | R.suppressDiagnostics(); |
4816 | | |
4817 | 537k | UnresolvedLookupExpr *ULE |
4818 | 537k | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
4819 | 537k | SS.getWithLocInContext(Context), |
4820 | 537k | TemplateKWLoc, |
4821 | 537k | R.getLookupNameInfo(), |
4822 | 537k | RequiresADL, TemplateArgs, |
4823 | 537k | R.begin(), R.end()); |
4824 | | |
4825 | 537k | return ULE; |
4826 | 540k | } |
4827 | | |
4828 | | // We actually only call this from template instantiation. |
4829 | | ExprResult |
4830 | | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
4831 | | SourceLocation TemplateKWLoc, |
4832 | | const DeclarationNameInfo &NameInfo, |
4833 | 47.3k | const TemplateArgumentListInfo *TemplateArgs) { |
4834 | | |
4835 | 47.3k | assert(TemplateArgs || TemplateKWLoc.isValid()); |
4836 | 0 | DeclContext *DC; |
4837 | 47.3k | if (!(DC = computeDeclContext(SS, false)) || |
4838 | 47.3k | DC->isDependentContext()29.4k || |
4839 | 47.3k | RequireCompleteDeclContext(SS, DC)29.4k ) |
4840 | 17.8k | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
4841 | | |
4842 | 29.4k | bool MemberOfUnknownSpecialization; |
4843 | 29.4k | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
4844 | 29.4k | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
4845 | 29.4k | /*Entering*/false, MemberOfUnknownSpecialization, |
4846 | 29.4k | TemplateKWLoc)) |
4847 | 3 | return ExprError(); |
4848 | | |
4849 | 29.4k | if (R.isAmbiguous()) |
4850 | 0 | return ExprError(); |
4851 | | |
4852 | 29.4k | if (R.empty()) { |
4853 | 42 | Diag(NameInfo.getLoc(), diag::err_no_member) |
4854 | 42 | << NameInfo.getName() << DC <<
|