/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/clang/lib/Sema/SemaTemplate.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for C++ templates. |
10 | | //===----------------------------------------------------------------------===// |
11 | | |
12 | | #include "TreeTransform.h" |
13 | | #include "clang/AST/ASTConsumer.h" |
14 | | #include "clang/AST/ASTContext.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/TypeVisitor.h" |
21 | | #include "clang/Basic/Builtins.h" |
22 | | #include "clang/Basic/LangOptions.h" |
23 | | #include "clang/Basic/PartialDiagnostic.h" |
24 | | #include "clang/Basic/TargetInfo.h" |
25 | | #include "clang/Sema/DeclSpec.h" |
26 | | #include "clang/Sema/Lookup.h" |
27 | | #include "clang/Sema/ParsedTemplate.h" |
28 | | #include "clang/Sema/Scope.h" |
29 | | #include "clang/Sema/SemaInternal.h" |
30 | | #include "clang/Sema/Template.h" |
31 | | #include "clang/Sema/TemplateDeduction.h" |
32 | | #include "llvm/ADT/SmallBitVector.h" |
33 | | #include "llvm/ADT/SmallString.h" |
34 | | #include "llvm/ADT/StringExtras.h" |
35 | | |
36 | | #include <iterator> |
37 | | using namespace clang; |
38 | | using namespace sema; |
39 | | |
40 | | // Exported for use by Parser. |
41 | | SourceRange |
42 | | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
43 | 8 | unsigned N) { |
44 | 8 | if (!N8 ) return SourceRange()0 ; |
45 | 8 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
46 | 8 | } |
47 | | |
48 | | namespace clang { |
49 | | /// \brief [temp.constr.decl]p2: A template's associated constraints are |
50 | | /// defined as a single constraint-expression derived from the introduced |
51 | | /// constraint-expressions [ ... ]. |
52 | | /// |
53 | | /// \param Params The template parameter list and optional requires-clause. |
54 | | /// |
55 | | /// \param FD The underlying templated function declaration for a function |
56 | | /// template. |
57 | | static Expr *formAssociatedConstraints(TemplateParameterList *Params, |
58 | | FunctionDecl *FD); |
59 | | } |
60 | | |
61 | | static Expr *clang::formAssociatedConstraints(TemplateParameterList *Params, |
62 | 23.8k | FunctionDecl *FD) { |
63 | 23.8k | // FIXME: Concepts: collect additional introduced constraint-expressions |
64 | 23.8k | assert(!FD && "Cannot collect constraints from function declaration yet."); |
65 | 23.8k | return Params->getRequiresClause(); |
66 | 23.8k | } |
67 | | |
68 | | /// \brief Determine whether the declaration found is acceptable as the name |
69 | | /// of a template and, if so, return that template declaration. Otherwise, |
70 | | /// returns NULL. |
71 | | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
72 | | NamedDecl *Orig, |
73 | 436k | bool AllowFunctionTemplates) { |
74 | 436k | NamedDecl *D = Orig->getUnderlyingDecl(); |
75 | 436k | |
76 | 436k | if (isa<TemplateDecl>(D)436k ) { |
77 | 148k | if (!AllowFunctionTemplates && 148k isa<FunctionTemplateDecl>(D)798 ) |
78 | 764 | return nullptr; |
79 | 147k | |
80 | 147k | return Orig; |
81 | 147k | } |
82 | 287k | |
83 | 287k | if (CXXRecordDecl *287k Record287k = dyn_cast<CXXRecordDecl>(D)) { |
84 | 9.57k | // C++ [temp.local]p1: |
85 | 9.57k | // Like normal (non-template) classes, class templates have an |
86 | 9.57k | // injected-class-name (Clause 9). The injected-class-name |
87 | 9.57k | // can be used with or without a template-argument-list. When |
88 | 9.57k | // it is used without a template-argument-list, it is |
89 | 9.57k | // equivalent to the injected-class-name followed by the |
90 | 9.57k | // template-parameters of the class template enclosed in |
91 | 9.57k | // <>. When it is used with a template-argument-list, it |
92 | 9.57k | // refers to the specified class template specialization, |
93 | 9.57k | // which could be the current specialization or another |
94 | 9.57k | // specialization. |
95 | 9.57k | if (Record->isInjectedClassName()9.57k ) { |
96 | 8.64k | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
97 | 8.64k | if (Record->getDescribedClassTemplate()) |
98 | 5.92k | return Record->getDescribedClassTemplate(); |
99 | 2.72k | |
100 | 2.72k | if (ClassTemplateSpecializationDecl *2.72k Spec2.72k |
101 | 2.72k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
102 | 2.63k | return Spec->getSpecializedTemplate(); |
103 | 1.02k | } |
104 | 1.02k | |
105 | 1.02k | return nullptr; |
106 | 1.02k | } |
107 | 278k | |
108 | 278k | return nullptr; |
109 | 278k | } |
110 | | |
111 | | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
112 | 445k | bool AllowFunctionTemplates) { |
113 | 445k | // The set of class templates we've already seen. |
114 | 445k | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
115 | 445k | LookupResult::Filter filter = R.makeFilter(); |
116 | 881k | while (filter.hasNext()881k ) { |
117 | 436k | NamedDecl *Orig = filter.next(); |
118 | 436k | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
119 | 436k | AllowFunctionTemplates); |
120 | 436k | if (!Repl) |
121 | 279k | filter.erase(); |
122 | 156k | else if (156k Repl != Orig156k ) { |
123 | 8.55k | |
124 | 8.55k | // C++ [temp.local]p3: |
125 | 8.55k | // A lookup that finds an injected-class-name (10.2) can result in an |
126 | 8.55k | // ambiguity in certain cases (for example, if it is found in more than |
127 | 8.55k | // one base class). If all of the injected-class-names that are found |
128 | 8.55k | // refer to specializations of the same class template, and if the name |
129 | 8.55k | // is used as a template-name, the reference refers to the class |
130 | 8.55k | // template itself and not a specialization thereof, and is not |
131 | 8.55k | // ambiguous. |
132 | 8.55k | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
133 | 8.55k | if (8.55k !ClassTemplates.insert(ClassTmpl).second8.55k ) { |
134 | 8 | filter.erase(); |
135 | 8 | continue; |
136 | 8 | } |
137 | 8.54k | |
138 | 8.54k | // FIXME: we promote access to public here as a workaround to |
139 | 8.54k | // the fact that LookupResult doesn't let us remember that we |
140 | 8.54k | // found this template through a particular injected class name, |
141 | 8.54k | // which means we end up doing nasty things to the invariants. |
142 | 8.54k | // Pretending that access is public is *much* safer. |
143 | 8.54k | filter.replace(Repl, AS_public); |
144 | 8.54k | } |
145 | 436k | } |
146 | 445k | filter.done(); |
147 | 445k | } |
148 | | |
149 | | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
150 | 54 | bool AllowFunctionTemplates) { |
151 | 109 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd109 ; ++I55 ) |
152 | 55 | if (55 isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)55 ) |
153 | 0 | return true; |
154 | 54 | |
155 | 54 | return false; |
156 | 54 | } |
157 | | |
158 | | TemplateNameKind Sema::isTemplateName(Scope *S, |
159 | | CXXScopeSpec &SS, |
160 | | bool hasTemplateKeyword, |
161 | | UnqualifiedId &Name, |
162 | | ParsedType ObjectTypePtr, |
163 | | bool EnteringContext, |
164 | | TemplateTy &TemplateResult, |
165 | 430k | bool &MemberOfUnknownSpecialization) { |
166 | 430k | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
167 | 430k | |
168 | 430k | DeclarationName TName; |
169 | 430k | MemberOfUnknownSpecialization = false; |
170 | 430k | |
171 | 430k | switch (Name.getKind()) { |
172 | 430k | case UnqualifiedId::IK_Identifier: |
173 | 430k | TName = DeclarationName(Name.Identifier); |
174 | 430k | break; |
175 | 430k | |
176 | 91 | case UnqualifiedId::IK_OperatorFunctionId: |
177 | 91 | TName = Context.DeclarationNames.getCXXOperatorName( |
178 | 91 | Name.OperatorFunctionId.Operator); |
179 | 91 | break; |
180 | 430k | |
181 | 3 | case UnqualifiedId::IK_LiteralOperatorId: |
182 | 3 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
183 | 3 | break; |
184 | 430k | |
185 | 0 | default: |
186 | 0 | return TNK_Non_template; |
187 | 430k | } |
188 | 430k | |
189 | 430k | QualType ObjectType = ObjectTypePtr.get(); |
190 | 430k | |
191 | 430k | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
192 | 430k | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
193 | 430k | MemberOfUnknownSpecialization); |
194 | 430k | if (R.empty()430k ) return TNK_Non_template294k ; |
195 | 135k | if (135k R.isAmbiguous()135k ) { |
196 | 1 | // Suppress diagnostics; we'll redo this lookup later. |
197 | 1 | R.suppressDiagnostics(); |
198 | 1 | |
199 | 1 | // FIXME: we might have ambiguous templates, in which case we |
200 | 1 | // should at least parse them properly! |
201 | 1 | return TNK_Non_template; |
202 | 1 | } |
203 | 135k | |
204 | 135k | TemplateName Template; |
205 | 135k | TemplateNameKind TemplateKind; |
206 | 135k | |
207 | 135k | unsigned ResultCount = R.end() - R.begin(); |
208 | 135k | if (ResultCount > 1135k ) { |
209 | 3.70k | // We assume that we'll preserve the qualifier from a function |
210 | 3.70k | // template name in other ways. |
211 | 3.70k | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
212 | 3.70k | TemplateKind = TNK_Function_template; |
213 | 3.70k | |
214 | 3.70k | // We'll do this lookup again later. |
215 | 3.70k | R.suppressDiagnostics(); |
216 | 135k | } else { |
217 | 132k | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
218 | 132k | |
219 | 132k | if (SS.isSet() && 132k !SS.isInvalid()5.69k ) { |
220 | 5.69k | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
221 | 5.69k | Template = Context.getQualifiedTemplateName(Qualifier, |
222 | 5.69k | hasTemplateKeyword, TD); |
223 | 132k | } else { |
224 | 126k | Template = TemplateName(TD); |
225 | 126k | } |
226 | 132k | |
227 | 132k | if (isa<FunctionTemplateDecl>(TD)132k ) { |
228 | 10.0k | TemplateKind = TNK_Function_template; |
229 | 10.0k | |
230 | 10.0k | // We'll do this lookup again later. |
231 | 10.0k | R.suppressDiagnostics(); |
232 | 132k | } else { |
233 | 122k | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
234 | 122k | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
235 | 122k | isa<BuiltinTemplateDecl>(TD)); |
236 | 122k | TemplateKind = |
237 | 122k | isa<VarTemplateDecl>(TD) ? TNK_Var_template1.64k : TNK_Type_template120k ; |
238 | 122k | } |
239 | 132k | } |
240 | 430k | |
241 | 430k | TemplateResult = TemplateTy::make(Template); |
242 | 430k | return TemplateKind; |
243 | 430k | } |
244 | | |
245 | | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
246 | | SourceLocation NameLoc, |
247 | 1.96k | ParsedTemplateTy *Template) { |
248 | 1.96k | CXXScopeSpec SS; |
249 | 1.96k | bool MemberOfUnknownSpecialization = false; |
250 | 1.96k | |
251 | 1.96k | // We could use redeclaration lookup here, but we don't need to: the |
252 | 1.96k | // syntactic form of a deduction guide is enough to identify it even |
253 | 1.96k | // if we can't look up the template name at all. |
254 | 1.96k | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
255 | 1.96k | LookupTemplateName(R, S, SS, /*ObjectType*/QualType(), |
256 | 1.96k | /*EnteringContext*/false, MemberOfUnknownSpecialization); |
257 | 1.96k | |
258 | 1.96k | if (R.empty()1.96k ) return false1.59k ; |
259 | 370 | if (370 R.isAmbiguous()370 ) { |
260 | 0 | // FIXME: Diagnose an ambiguity if we find at least one template. |
261 | 0 | R.suppressDiagnostics(); |
262 | 0 | return false; |
263 | 0 | } |
264 | 370 | |
265 | 370 | // We only treat template-names that name type templates as valid deduction |
266 | 370 | // guide names. |
267 | 370 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
268 | 370 | if (!TD || 370 !getAsTypeTemplateDecl(TD)369 ) |
269 | 2 | return false; |
270 | 368 | |
271 | 368 | if (368 Template368 ) |
272 | 134 | *Template = TemplateTy::make(TemplateName(TD)); |
273 | 1.96k | return true; |
274 | 1.96k | } |
275 | | |
276 | | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
277 | | SourceLocation IILoc, |
278 | | Scope *S, |
279 | | const CXXScopeSpec *SS, |
280 | | TemplateTy &SuggestedTemplate, |
281 | 85 | TemplateNameKind &SuggestedKind) { |
282 | 85 | // We can't recover unless there's a dependent scope specifier preceding the |
283 | 85 | // template name. |
284 | 85 | // FIXME: Typo correction? |
285 | 85 | if (!SS || 85 !SS->isSet()85 || !isDependentScopeSpecifier(*SS)4 || |
286 | 0 | computeDeclContext(*SS)) |
287 | 85 | return false; |
288 | 0 |
|
289 | 0 | // The code is missing a 'template' keyword prior to the dependent template |
290 | 0 | // name. |
291 | 0 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
292 | 0 | Diag(IILoc, diag::err_template_kw_missing) |
293 | 0 | << Qualifier << II.getName() |
294 | 0 | << FixItHint::CreateInsertion(IILoc, "template "); |
295 | 0 | SuggestedTemplate |
296 | 0 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
297 | 0 | SuggestedKind = TNK_Dependent_template_name; |
298 | 0 | return true; |
299 | 0 | } |
300 | | |
301 | | void Sema::LookupTemplateName(LookupResult &Found, |
302 | | Scope *S, CXXScopeSpec &SS, |
303 | | QualType ObjectType, |
304 | | bool EnteringContext, |
305 | 445k | bool &MemberOfUnknownSpecialization) { |
306 | 445k | // Determine where to perform name lookup |
307 | 445k | MemberOfUnknownSpecialization = false; |
308 | 445k | DeclContext *LookupCtx = nullptr; |
309 | 445k | bool isDependent = false; |
310 | 445k | if (!ObjectType.isNull()445k ) { |
311 | 4.87k | // This nested-name-specifier occurs in a member access expression, e.g., |
312 | 4.87k | // x->B::f, and we are looking into the type of the object. |
313 | 4.87k | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
314 | 4.87k | LookupCtx = computeDeclContext(ObjectType); |
315 | 4.87k | isDependent = ObjectType->isDependentType(); |
316 | 4.87k | assert((isDependent || !ObjectType->isIncompleteType() || |
317 | 4.87k | ObjectType->castAs<TagType>()->isBeingDefined()) && |
318 | 4.87k | "Caller should have completed object type"); |
319 | 4.87k | |
320 | 4.87k | // Template names cannot appear inside an Objective-C class or object type. |
321 | 4.87k | if (ObjectType->isObjCObjectOrInterfaceType()4.87k ) { |
322 | 4 | Found.clear(); |
323 | 4 | return; |
324 | 4 | } |
325 | 440k | } else if (440k SS.isSet()440k ) { |
326 | 8.13k | // This nested-name-specifier occurs after another nested-name-specifier, |
327 | 8.13k | // so long into the context associated with the prior nested-name-specifier. |
328 | 8.13k | LookupCtx = computeDeclContext(SS, EnteringContext); |
329 | 8.13k | isDependent = isDependentScopeSpecifier(SS); |
330 | 8.13k | |
331 | 8.13k | // The declaration context must be complete. |
332 | 8.13k | if (LookupCtx && 8.13k RequireCompleteDeclContext(SS, LookupCtx)7.35k ) |
333 | 6 | return; |
334 | 445k | } |
335 | 445k | |
336 | 445k | bool ObjectTypeSearchedInScope = false; |
337 | 445k | bool AllowFunctionTemplatesInLookup = true; |
338 | 445k | if (LookupCtx445k ) { |
339 | 10.9k | // Perform "qualified" name lookup into the declaration context we |
340 | 10.9k | // computed, which is either the type of the base of a member access |
341 | 10.9k | // expression or the declaration context associated with a prior |
342 | 10.9k | // nested-name-specifier. |
343 | 10.9k | LookupQualifiedName(Found, LookupCtx); |
344 | 10.9k | if (!ObjectType.isNull() && 10.9k Found.empty()3.62k ) { |
345 | 63 | // C++ [basic.lookup.classref]p1: |
346 | 63 | // In a class member access expression (5.2.5), if the . or -> token is |
347 | 63 | // immediately followed by an identifier followed by a <, the |
348 | 63 | // identifier must be looked up to determine whether the < is the |
349 | 63 | // beginning of a template argument list (14.2) or a less-than operator. |
350 | 63 | // The identifier is first looked up in the class of the object |
351 | 63 | // expression. If the identifier is not found, it is then looked up in |
352 | 63 | // the context of the entire postfix-expression and shall name a class |
353 | 63 | // or function template. |
354 | 63 | if (S63 ) LookupName(Found, S)63 ; |
355 | 63 | ObjectTypeSearchedInScope = true; |
356 | 63 | AllowFunctionTemplatesInLookup = false; |
357 | 63 | } |
358 | 445k | } else if (434k isDependent && 434k (!S || 2.03k ObjectType.isNull()2.03k )) { |
359 | 780 | // We cannot look into a dependent object type or nested nme |
360 | 780 | // specifier. |
361 | 780 | MemberOfUnknownSpecialization = true; |
362 | 780 | return; |
363 | 0 | } else { |
364 | 433k | // Perform unqualified name lookup in the current scope. |
365 | 433k | LookupName(Found, S); |
366 | 433k | |
367 | 433k | if (!ObjectType.isNull()) |
368 | 1.25k | AllowFunctionTemplatesInLookup = false; |
369 | 434k | } |
370 | 445k | |
371 | 444k | if (444k Found.empty() && 444k !isDependent16.6k ) { |
372 | 15.4k | // If we did not find any names, attempt to correct any typos. |
373 | 15.4k | DeclarationName Name = Found.getLookupName(); |
374 | 15.4k | Found.clear(); |
375 | 15.4k | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
376 | 15.4k | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
377 | 15.4k | FilterCCC->WantTypeSpecifiers = false; |
378 | 15.4k | FilterCCC->WantExpressionKeywords = false; |
379 | 15.4k | FilterCCC->WantRemainingKeywords = false; |
380 | 15.4k | FilterCCC->WantCXXNamedCasts = true; |
381 | 15.4k | if (TypoCorrection Corrected = CorrectTypo( |
382 | 15.4k | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
383 | 60 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
384 | 60 | Found.setLookupName(Corrected.getCorrection()); |
385 | 60 | if (auto *ND = Corrected.getFoundDecl()) |
386 | 58 | Found.addDecl(ND); |
387 | 60 | FilterAcceptableTemplateNames(Found); |
388 | 60 | if (!Found.empty()60 ) { |
389 | 57 | if (LookupCtx57 ) { |
390 | 7 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
391 | 7 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
392 | 4 | Name.getAsString() == CorrectedStr; |
393 | 7 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
394 | 7 | << Name << LookupCtx << DroppedSpecifier |
395 | 7 | << SS.getRange()); |
396 | 57 | } else { |
397 | 50 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
398 | 50 | } |
399 | 57 | } |
400 | 15.4k | } else { |
401 | 15.3k | Found.setLookupName(Name); |
402 | 15.3k | } |
403 | 15.4k | } |
404 | 444k | |
405 | 444k | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
406 | 444k | if (Found.empty()444k ) { |
407 | 295k | if (isDependent) |
408 | 1.32k | MemberOfUnknownSpecialization = true; |
409 | 295k | return; |
410 | 295k | } |
411 | 148k | |
412 | 148k | if (148k S && 148k !ObjectType.isNull()146k && !ObjectTypeSearchedInScope1.26k && |
413 | 148k | !getLangOpts().CPlusPlus111.25k ) { |
414 | 822 | // C++03 [basic.lookup.classref]p1: |
415 | 822 | // [...] If the lookup in the class of the object expression finds a |
416 | 822 | // template, the name is also looked up in the context of the entire |
417 | 822 | // postfix-expression and [...] |
418 | 822 | // |
419 | 822 | // Note: C++11 does not perform this second lookup. |
420 | 822 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
421 | 822 | LookupOrdinaryName); |
422 | 822 | LookupName(FoundOuter, S); |
423 | 822 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
424 | 822 | |
425 | 822 | if (FoundOuter.empty()822 ) { |
426 | 810 | // - if the name is not found, the name found in the class of the |
427 | 810 | // object expression is used, otherwise |
428 | 822 | } else if (12 !FoundOuter.getAsSingle<ClassTemplateDecl>() || |
429 | 12 | FoundOuter.isAmbiguous()12 ) { |
430 | 0 | // - if the name is found in the context of the entire |
431 | 0 | // postfix-expression and does not name a class template, the name |
432 | 0 | // found in the class of the object expression is used, otherwise |
433 | 0 | FoundOuter.clear(); |
434 | 12 | } else if (12 !Found.isSuppressingDiagnostics()12 ) { |
435 | 12 | // - if the name found is a class template, it must refer to the same |
436 | 12 | // entity as the one found in the class of the object expression, |
437 | 12 | // otherwise the program is ill-formed. |
438 | 12 | if (!Found.isSingleResult() || |
439 | 10 | Found.getFoundDecl()->getCanonicalDecl() |
440 | 12 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
441 | 3 | Diag(Found.getNameLoc(), |
442 | 3 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
443 | 3 | << Found.getLookupName() |
444 | 3 | << ObjectType; |
445 | 3 | Diag(Found.getRepresentativeDecl()->getLocation(), |
446 | 3 | diag::note_ambig_member_ref_object_type) |
447 | 3 | << ObjectType; |
448 | 3 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
449 | 3 | diag::note_ambig_member_ref_scope); |
450 | 3 | |
451 | 3 | // Recover by taking the template that we found in the object |
452 | 3 | // expression's type. |
453 | 3 | } |
454 | 12 | } |
455 | 822 | } |
456 | 445k | } |
457 | | |
458 | | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
459 | | SourceLocation Less, |
460 | 10 | SourceLocation Greater) { |
461 | 10 | if (TemplateName.isInvalid()) |
462 | 0 | return; |
463 | 10 | |
464 | 10 | DeclarationNameInfo NameInfo; |
465 | 10 | CXXScopeSpec SS; |
466 | 10 | LookupNameKind LookupKind; |
467 | 10 | |
468 | 10 | DeclContext *LookupCtx = nullptr; |
469 | 10 | NamedDecl *Found = nullptr; |
470 | 10 | |
471 | 10 | // Figure out what name we looked up. |
472 | 10 | if (auto *ME10 = dyn_cast<MemberExpr>(TemplateName.get())) { |
473 | 2 | NameInfo = ME->getMemberNameInfo(); |
474 | 2 | SS.Adopt(ME->getQualifierLoc()); |
475 | 2 | LookupKind = LookupMemberName; |
476 | 2 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
477 | 2 | Found = ME->getMemberDecl(); |
478 | 10 | } else { |
479 | 8 | auto *DRE = cast<DeclRefExpr>(TemplateName.get()); |
480 | 8 | NameInfo = DRE->getNameInfo(); |
481 | 8 | SS.Adopt(DRE->getQualifierLoc()); |
482 | 8 | LookupKind = LookupOrdinaryName; |
483 | 8 | Found = DRE->getFoundDecl(); |
484 | 8 | } |
485 | 10 | |
486 | 10 | // Try to correct the name by looking for templates and C++ named casts. |
487 | 10 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
488 | 10 | TemplateCandidateFilter() { |
489 | 10 | WantTypeSpecifiers = false; |
490 | 10 | WantExpressionKeywords = false; |
491 | 10 | WantRemainingKeywords = false; |
492 | 10 | WantCXXNamedCasts = true; |
493 | 10 | }; |
494 | 28 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
495 | 28 | if (auto *ND = Candidate.getCorrectionDecl()) |
496 | 28 | return isAcceptableTemplateName(ND->getASTContext(), ND, true); |
497 | 0 | return Candidate.isKeyword(); |
498 | 0 | } |
499 | 10 | }; |
500 | 10 | |
501 | 10 | DeclarationName Name = NameInfo.getName(); |
502 | 10 | if (TypoCorrection Corrected = |
503 | 10 | CorrectTypo(NameInfo, LookupKind, S, &SS, |
504 | 10 | llvm::make_unique<TemplateCandidateFilter>(), |
505 | 9 | CTK_ErrorRecovery, LookupCtx)) { |
506 | 9 | auto *ND = Corrected.getFoundDecl(); |
507 | 9 | if (ND) |
508 | 9 | ND = isAcceptableTemplateName(Context, ND, |
509 | 9 | /*AllowFunctionTemplates*/ true); |
510 | 9 | if (ND || 9 Corrected.isKeyword()0 ) { |
511 | 9 | if (LookupCtx9 ) { |
512 | 1 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
513 | 1 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
514 | 0 | Name.getAsString() == CorrectedStr; |
515 | 1 | diagnoseTypo(Corrected, |
516 | 1 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
517 | 1 | << Name << LookupCtx << DroppedSpecifier |
518 | 1 | << SS.getRange(), false); |
519 | 9 | } else { |
520 | 8 | diagnoseTypo(Corrected, |
521 | 8 | PDiag(diag::err_non_template_in_template_id_suggest) |
522 | 8 | << Name, false); |
523 | 8 | } |
524 | 9 | if (Found) |
525 | 9 | Diag(Found->getLocation(), |
526 | 9 | diag::note_non_template_in_template_id_found); |
527 | 9 | return; |
528 | 9 | } |
529 | 1 | } |
530 | 1 | |
531 | 1 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
532 | 1 | << Name << SourceRange(Less, Greater); |
533 | 1 | if (Found) |
534 | 1 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
535 | 10 | } |
536 | | |
537 | | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
538 | | /// was just parsed. This is only possible with an explicit scope |
539 | | /// specifier naming a dependent type. |
540 | | ExprResult |
541 | | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
542 | | SourceLocation TemplateKWLoc, |
543 | | const DeclarationNameInfo &NameInfo, |
544 | | bool isAddressOfOperand, |
545 | 18.6k | const TemplateArgumentListInfo *TemplateArgs) { |
546 | 18.6k | DeclContext *DC = getFunctionLevelDeclContext(); |
547 | 18.6k | |
548 | 18.6k | // C++11 [expr.prim.general]p12: |
549 | 18.6k | // An id-expression that denotes a non-static data member or non-static |
550 | 18.6k | // member function of a class can only be used: |
551 | 18.6k | // (...) |
552 | 18.6k | // - if that id-expression denotes a non-static data member and it |
553 | 18.6k | // appears in an unevaluated operand. |
554 | 18.6k | // |
555 | 18.6k | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
556 | 18.6k | // CXXDependentScopeMemberExpr. The former can instantiate to either |
557 | 18.6k | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
558 | 18.6k | // always a MemberExpr. |
559 | 18.6k | bool MightBeCxx11UnevalField = |
560 | 10.2k | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
561 | 18.6k | |
562 | 18.6k | // Check if the nested name specifier is an enum type. |
563 | 18.6k | bool IsEnum = false; |
564 | 18.6k | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
565 | 18.6k | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
566 | 18.6k | |
567 | 18.6k | if (!MightBeCxx11UnevalField && 18.6k !isAddressOfOperand18.4k && !IsEnum18.4k && |
568 | 18.6k | isa<CXXMethodDecl>(DC)18.4k && cast<CXXMethodDecl>(DC)->isInstance()9.02k ) { |
569 | 5.70k | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
570 | 5.70k | |
571 | 5.70k | // Since the 'this' expression is synthesized, we don't need to |
572 | 5.70k | // perform the double-lookup check. |
573 | 5.70k | NamedDecl *FirstQualifierInScope = nullptr; |
574 | 5.70k | |
575 | 5.70k | return CXXDependentScopeMemberExpr::Create( |
576 | 5.70k | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
577 | 5.70k | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
578 | 5.70k | FirstQualifierInScope, NameInfo, TemplateArgs); |
579 | 5.70k | } |
580 | 12.9k | |
581 | 12.9k | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
582 | 12.9k | } |
583 | | |
584 | | ExprResult |
585 | | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
586 | | SourceLocation TemplateKWLoc, |
587 | | const DeclarationNameInfo &NameInfo, |
588 | 13.0k | const TemplateArgumentListInfo *TemplateArgs) { |
589 | 13.0k | return DependentScopeDeclRefExpr::Create( |
590 | 13.0k | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
591 | 13.0k | TemplateArgs); |
592 | 13.0k | } |
593 | | |
594 | | |
595 | | /// Determine whether we would be unable to instantiate this template (because |
596 | | /// it either has no definition, or is in the process of being instantiated). |
597 | | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
598 | | NamedDecl *Instantiation, |
599 | | bool InstantiatedFromMember, |
600 | | const NamedDecl *Pattern, |
601 | | const NamedDecl *PatternDef, |
602 | | TemplateSpecializationKind TSK, |
603 | 79.9k | bool Complain /*= true*/) { |
604 | 79.9k | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
605 | 79.9k | isa<VarDecl>(Instantiation)); |
606 | 79.9k | |
607 | 79.9k | bool IsEntityBeingDefined = false; |
608 | 79.9k | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
609 | 25.7k | IsEntityBeingDefined = TD->isBeingDefined(); |
610 | 79.9k | |
611 | 79.9k | if (PatternDef && 79.9k !IsEntityBeingDefined71.4k ) { |
612 | 71.4k | NamedDecl *SuggestedDef = nullptr; |
613 | 71.4k | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
614 | 71.4k | /*OnlyNeedComplete*/false)) { |
615 | 28 | // If we're allowed to diagnose this and recover, do so. |
616 | 22 | bool Recover = Complain && !isSFINAEContext(); |
617 | 28 | if (Complain) |
618 | 22 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
619 | 22 | Sema::MissingImportKind::Definition, Recover); |
620 | 28 | return !Recover; |
621 | 28 | } |
622 | 71.4k | return false; |
623 | 71.4k | } |
624 | 8.45k | |
625 | 8.45k | if (8.45k !Complain || 8.45k (PatternDef && 658 PatternDef->isInvalidDecl()3 )) |
626 | 7.79k | return true; |
627 | 658 | |
628 | 658 | llvm::Optional<unsigned> Note; |
629 | 658 | QualType InstantiationTy; |
630 | 658 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
631 | 617 | InstantiationTy = Context.getTypeDeclType(TD); |
632 | 658 | if (PatternDef658 ) { |
633 | 3 | Diag(PointOfInstantiation, |
634 | 3 | diag::err_template_instantiate_within_definition) |
635 | 3 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
636 | 3 | << InstantiationTy; |
637 | 3 | // Not much point in noting the template declaration here, since |
638 | 3 | // we're lexically inside it. |
639 | 3 | Instantiation->setInvalidDecl(); |
640 | 658 | } else if (655 InstantiatedFromMember655 ) { |
641 | 7 | if (isa<FunctionDecl>(Instantiation)7 ) { |
642 | 2 | Diag(PointOfInstantiation, |
643 | 2 | diag::err_explicit_instantiation_undefined_member) |
644 | 2 | << /*member function*/ 1 << Instantiation->getDeclName() |
645 | 2 | << Instantiation->getDeclContext(); |
646 | 2 | Note = diag::note_explicit_instantiation_here; |
647 | 7 | } else { |
648 | 5 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
649 | 5 | Diag(PointOfInstantiation, |
650 | 5 | diag::err_implicit_instantiate_member_undefined) |
651 | 5 | << InstantiationTy; |
652 | 5 | Note = diag::note_member_declared_at; |
653 | 5 | } |
654 | 655 | } else { |
655 | 648 | if (isa<FunctionDecl>(Instantiation)648 ) { |
656 | 5 | Diag(PointOfInstantiation, |
657 | 5 | diag::err_explicit_instantiation_undefined_func_template) |
658 | 5 | << Pattern; |
659 | 5 | Note = diag::note_explicit_instantiation_here; |
660 | 648 | } else if (643 isa<TagDecl>(Instantiation)643 ) { |
661 | 609 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
662 | 609 | << (TSK != TSK_ImplicitInstantiation) |
663 | 609 | << InstantiationTy; |
664 | 609 | Note = diag::note_template_decl_here; |
665 | 643 | } else { |
666 | 34 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
667 | 34 | if (isa<VarTemplateSpecializationDecl>(Instantiation)34 ) { |
668 | 30 | Diag(PointOfInstantiation, |
669 | 30 | diag::err_explicit_instantiation_undefined_var_template) |
670 | 30 | << Instantiation; |
671 | 30 | Instantiation->setInvalidDecl(); |
672 | 30 | } else |
673 | 4 | Diag(PointOfInstantiation, |
674 | 4 | diag::err_explicit_instantiation_undefined_member) |
675 | 4 | << /*static data member*/ 2 << Instantiation->getDeclName() |
676 | 4 | << Instantiation->getDeclContext(); |
677 | 643 | Note = diag::note_explicit_instantiation_here; |
678 | 643 | } |
679 | 655 | } |
680 | 658 | if (Note) // Diagnostics were emitted. |
681 | 655 | Diag(Pattern->getLocation(), Note.getValue()); |
682 | 658 | |
683 | 658 | // In general, Instantiation isn't marked invalid to get more than one |
684 | 658 | // error for multiple undefined instantiations. But the code that does |
685 | 658 | // explicit declaration -> explicit definition conversion can't handle |
686 | 658 | // invalid declarations, so mark as invalid in that case. |
687 | 658 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
688 | 3 | Instantiation->setInvalidDecl(); |
689 | 79.9k | return true; |
690 | 79.9k | } |
691 | | |
692 | | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
693 | | /// that the template parameter 'PrevDecl' is being shadowed by a new |
694 | | /// declaration at location Loc. Returns true to indicate that this is |
695 | | /// an error, and false otherwise. |
696 | 80 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
697 | 80 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
698 | 80 | |
699 | 80 | // Microsoft Visual C++ permits template parameters to be shadowed. |
700 | 80 | if (getLangOpts().MicrosoftExt) |
701 | 1 | return; |
702 | 79 | |
703 | 79 | // C++ [temp.local]p4: |
704 | 79 | // A template-parameter shall not be redeclared within its |
705 | 79 | // scope (including nested scopes). |
706 | 79 | Diag(Loc, diag::err_template_param_shadow) |
707 | 79 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
708 | 79 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
709 | 79 | } |
710 | | |
711 | | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
712 | | /// the parameter D to reference the templated declaration and return a pointer |
713 | | /// to the template declaration. Otherwise, do nothing to D and return null. |
714 | 1.72M | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
715 | 1.72M | if (TemplateDecl *Temp1.72M = dyn_cast_or_null<TemplateDecl>(D)) { |
716 | 95.3k | D = Temp->getTemplatedDecl(); |
717 | 95.3k | return Temp; |
718 | 95.3k | } |
719 | 1.63M | return nullptr; |
720 | 1.63M | } |
721 | | |
722 | | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
723 | 19 | SourceLocation EllipsisLoc) const { |
724 | 19 | assert(Kind == Template && |
725 | 19 | "Only template template arguments can be pack expansions here"); |
726 | 19 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
727 | 19 | "Template template argument pack expansion without packs"); |
728 | 19 | ParsedTemplateArgument Result(*this); |
729 | 19 | Result.EllipsisLoc = EllipsisLoc; |
730 | 19 | return Result; |
731 | 19 | } |
732 | | |
733 | | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
734 | 205k | const ParsedTemplateArgument &Arg) { |
735 | 205k | |
736 | 205k | switch (Arg.getKind()) { |
737 | 160k | case ParsedTemplateArgument::Type: { |
738 | 160k | TypeSourceInfo *DI; |
739 | 160k | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
740 | 160k | if (!DI) |
741 | 0 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
742 | 160k | return TemplateArgumentLoc(TemplateArgument(T), DI); |
743 | 205k | } |
744 | 205k | |
745 | 44.2k | case ParsedTemplateArgument::NonType: { |
746 | 44.2k | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
747 | 44.2k | return TemplateArgumentLoc(TemplateArgument(E), E); |
748 | 205k | } |
749 | 205k | |
750 | 745 | case ParsedTemplateArgument::Template: { |
751 | 745 | TemplateName Template = Arg.getAsTemplate().get(); |
752 | 745 | TemplateArgument TArg; |
753 | 745 | if (Arg.getEllipsisLoc().isValid()) |
754 | 19 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
755 | 745 | else |
756 | 726 | TArg = Template; |
757 | 745 | return TemplateArgumentLoc(TArg, |
758 | 745 | Arg.getScopeSpec().getWithLocInContext( |
759 | 745 | SemaRef.Context), |
760 | 745 | Arg.getLocation(), |
761 | 745 | Arg.getEllipsisLoc()); |
762 | 0 | } |
763 | 0 | } |
764 | 0 |
|
765 | 0 | llvm_unreachable0 ("Unhandled parsed template argument"); |
766 | 0 | } |
767 | | |
768 | | /// \brief Translates template arguments as provided by the parser |
769 | | /// into template arguments used by semantic analysis. |
770 | | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
771 | 134k | TemplateArgumentListInfo &TemplateArgs) { |
772 | 340k | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last340k ; ++I205k ) |
773 | 205k | TemplateArgs.addArgument(translateTemplateArgument(*this, |
774 | 205k | TemplateArgsIn[I])); |
775 | 134k | } |
776 | | |
777 | | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
778 | | SourceLocation Loc, |
779 | 98.7k | IdentifierInfo *Name) { |
780 | 98.7k | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
781 | 98.7k | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
782 | 98.7k | if (PrevDecl && 98.7k PrevDecl->isTemplateParameter()1.28k ) |
783 | 18 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
784 | 98.7k | } |
785 | | |
786 | | /// ActOnTypeParameter - Called when a C++ template type parameter |
787 | | /// (e.g., "typename T") has been parsed. Typename specifies whether |
788 | | /// the keyword "typename" was used to declare the type parameter |
789 | | /// (otherwise, "class" was used), and KeyLoc is the location of the |
790 | | /// "class" or "typename" keyword. ParamName is the name of the |
791 | | /// parameter (NULL indicates an unnamed template parameter) and |
792 | | /// ParamNameLoc is the location of the parameter name (if any). |
793 | | /// If the type parameter has a default argument, it will be added |
794 | | /// later via ActOnTypeParameterDefault. |
795 | | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
796 | | SourceLocation EllipsisLoc, |
797 | | SourceLocation KeyLoc, |
798 | | IdentifierInfo *ParamName, |
799 | | SourceLocation ParamNameLoc, |
800 | | unsigned Depth, unsigned Position, |
801 | | SourceLocation EqualLoc, |
802 | 86.8k | ParsedType DefaultArg) { |
803 | 86.8k | assert(S->isTemplateParamScope() && |
804 | 86.8k | "Template type parameter not in template parameter scope!"); |
805 | 86.8k | |
806 | 86.8k | SourceLocation Loc = ParamNameLoc; |
807 | 86.8k | if (!ParamName) |
808 | 1.69k | Loc = KeyLoc; |
809 | 86.8k | |
810 | 86.8k | bool IsParameterPack = EllipsisLoc.isValid(); |
811 | 86.8k | TemplateTypeParmDecl *Param |
812 | 86.8k | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
813 | 86.8k | KeyLoc, Loc, Depth, Position, ParamName, |
814 | 86.8k | Typename, IsParameterPack); |
815 | 86.8k | Param->setAccess(AS_public); |
816 | 86.8k | |
817 | 86.8k | if (ParamName86.8k ) { |
818 | 85.1k | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
819 | 85.1k | |
820 | 85.1k | // Add the template parameter into the current scope. |
821 | 85.1k | S->AddDecl(Param); |
822 | 85.1k | IdResolver.AddDecl(Param); |
823 | 85.1k | } |
824 | 86.8k | |
825 | 86.8k | // C++0x [temp.param]p9: |
826 | 86.8k | // A default template-argument may be specified for any kind of |
827 | 86.8k | // template-parameter that is not a template parameter pack. |
828 | 86.8k | if (DefaultArg && 86.8k IsParameterPack1.96k ) { |
829 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
830 | 1 | DefaultArg = nullptr; |
831 | 1 | } |
832 | 86.8k | |
833 | 86.8k | // Handle the default argument, if provided. |
834 | 86.8k | if (DefaultArg86.8k ) { |
835 | 1.96k | TypeSourceInfo *DefaultTInfo; |
836 | 1.96k | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
837 | 1.96k | |
838 | 1.96k | assert(DefaultTInfo && "expected source information for type"); |
839 | 1.96k | |
840 | 1.96k | // Check for unexpanded parameter packs. |
841 | 1.96k | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
842 | 1.96k | UPPC_DefaultArgument)) |
843 | 2 | return Param; |
844 | 1.95k | |
845 | 1.95k | // Check the template argument itself. |
846 | 1.95k | if (1.95k CheckTemplateArgument(Param, DefaultTInfo)1.95k ) { |
847 | 0 | Param->setInvalidDecl(); |
848 | 0 | return Param; |
849 | 0 | } |
850 | 1.95k | |
851 | 1.95k | Param->setDefaultArgument(DefaultTInfo); |
852 | 1.95k | } |
853 | 86.8k | |
854 | 86.8k | return Param; |
855 | 86.8k | } |
856 | | |
857 | | /// \brief Check that the type of a non-type template parameter is |
858 | | /// well-formed. |
859 | | /// |
860 | | /// \returns the (possibly-promoted) parameter type if valid; |
861 | | /// otherwise, produces a diagnostic and returns a NULL type. |
862 | | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
863 | 15.0k | SourceLocation Loc) { |
864 | 15.0k | if (TSI->getType()->isUndeducedType()15.0k ) { |
865 | 60 | // C++1z [temp.dep.expr]p3: |
866 | 60 | // An id-expression is type-dependent if it contains |
867 | 60 | // - an identifier associated by name lookup with a non-type |
868 | 60 | // template-parameter declared with a type that contains a |
869 | 60 | // placeholder type (7.1.7.4), |
870 | 60 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
871 | 60 | } |
872 | 15.0k | |
873 | 15.0k | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
874 | 15.0k | } |
875 | | |
876 | | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
877 | 17.6k | SourceLocation Loc) { |
878 | 17.6k | // We don't allow variably-modified types as the type of non-type template |
879 | 17.6k | // parameters. |
880 | 17.6k | if (T->isVariablyModifiedType()17.6k ) { |
881 | 1 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
882 | 1 | << T; |
883 | 1 | return QualType(); |
884 | 1 | } |
885 | 17.6k | |
886 | 17.6k | // C++ [temp.param]p4: |
887 | 17.6k | // |
888 | 17.6k | // A non-type template-parameter shall have one of the following |
889 | 17.6k | // (optionally cv-qualified) types: |
890 | 17.6k | // |
891 | 17.6k | // -- integral or enumeration type, |
892 | 17.6k | if (17.6k T->isIntegralOrEnumerationType() || |
893 | 17.6k | // -- pointer to object or pointer to function, |
894 | 2.24k | T->isPointerType() || |
895 | 17.6k | // -- reference to object or reference to function, |
896 | 1.56k | T->isReferenceType() || |
897 | 17.6k | // -- pointer to member, |
898 | 1.32k | T->isMemberPointerType() || |
899 | 17.6k | // -- std::nullptr_t. |
900 | 1.12k | T->isNullPtrType() || |
901 | 17.6k | // If T is a dependent type, we can't do the check now, so we |
902 | 17.6k | // assume that it is well-formed. |
903 | 1.09k | T->isDependentType() || |
904 | 17.6k | // Allow use of auto in template parameter declarations. |
905 | 17.6k | T->isUndeducedType()239 ) { |
906 | 17.5k | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
907 | 17.5k | // are ignored when determining its type. |
908 | 17.5k | return T.getUnqualifiedType(); |
909 | 17.5k | } |
910 | 17.6k | |
911 | 17.6k | // C++ [temp.param]p8: |
912 | 17.6k | // |
913 | 17.6k | // A non-type template-parameter of type "array of T" or |
914 | 17.6k | // "function returning T" is adjusted to be of type "pointer to |
915 | 17.6k | // T" or "pointer to function returning T", respectively. |
916 | 82 | else if (82 T->isArrayType() || 82 T->isFunctionType()79 ) |
917 | 67 | return Context.getDecayedType(T); |
918 | 15 | |
919 | 15 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
920 | 15 | << T; |
921 | 15 | |
922 | 15 | return QualType(); |
923 | 15 | } |
924 | | |
925 | | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
926 | | unsigned Depth, |
927 | | unsigned Position, |
928 | | SourceLocation EqualLoc, |
929 | 14.1k | Expr *Default) { |
930 | 14.1k | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
931 | 14.1k | |
932 | 14.1k | if (TInfo->getType()->isUndeducedType()14.1k ) { |
933 | 40 | Diag(D.getIdentifierLoc(), |
934 | 40 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
935 | 40 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
936 | 40 | } |
937 | 14.1k | |
938 | 14.1k | assert(S->isTemplateParamScope() && |
939 | 14.1k | "Non-type template parameter not in template parameter scope!"); |
940 | 14.1k | bool Invalid = false; |
941 | 14.1k | |
942 | 14.1k | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
943 | 14.1k | if (T.isNull()14.1k ) { |
944 | 5 | T = Context.IntTy; // Recover with an 'int' type. |
945 | 5 | Invalid = true; |
946 | 5 | } |
947 | 14.1k | |
948 | 14.1k | IdentifierInfo *ParamName = D.getIdentifier(); |
949 | 14.1k | bool IsParameterPack = D.hasEllipsis(); |
950 | 14.1k | NonTypeTemplateParmDecl *Param |
951 | 14.1k | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
952 | 14.1k | D.getLocStart(), |
953 | 14.1k | D.getIdentifierLoc(), |
954 | 14.1k | Depth, Position, ParamName, T, |
955 | 14.1k | IsParameterPack, TInfo); |
956 | 14.1k | Param->setAccess(AS_public); |
957 | 14.1k | |
958 | 14.1k | if (Invalid) |
959 | 5 | Param->setInvalidDecl(); |
960 | 14.1k | |
961 | 14.1k | if (ParamName14.1k ) { |
962 | 13.0k | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
963 | 13.0k | ParamName); |
964 | 13.0k | |
965 | 13.0k | // Add the template parameter into the current scope. |
966 | 13.0k | S->AddDecl(Param); |
967 | 13.0k | IdResolver.AddDecl(Param); |
968 | 13.0k | } |
969 | 14.1k | |
970 | 14.1k | // C++0x [temp.param]p9: |
971 | 14.1k | // A default template-argument may be specified for any kind of |
972 | 14.1k | // template-parameter that is not a template parameter pack. |
973 | 14.1k | if (Default && 14.1k IsParameterPack474 ) { |
974 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
975 | 1 | Default = nullptr; |
976 | 1 | } |
977 | 14.1k | |
978 | 14.1k | // Check the well-formedness of the default template argument, if provided. |
979 | 14.1k | if (Default14.1k ) { |
980 | 473 | // Check for unexpanded parameter packs. |
981 | 473 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
982 | 1 | return Param; |
983 | 472 | |
984 | 472 | TemplateArgument Converted; |
985 | 472 | ExprResult DefaultRes = |
986 | 472 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
987 | 472 | if (DefaultRes.isInvalid()472 ) { |
988 | 2 | Param->setInvalidDecl(); |
989 | 2 | return Param; |
990 | 2 | } |
991 | 470 | Default = DefaultRes.get(); |
992 | 470 | |
993 | 470 | Param->setDefaultArgument(Default); |
994 | 470 | } |
995 | 14.1k | |
996 | 14.1k | return Param; |
997 | 14.1k | } |
998 | | |
999 | | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
1000 | | /// parameter (e.g. T in template <template \<typename> class T> class array) |
1001 | | /// has been parsed. S is the current scope. |
1002 | | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
1003 | | SourceLocation TmpLoc, |
1004 | | TemplateParameterList *Params, |
1005 | | SourceLocation EllipsisLoc, |
1006 | | IdentifierInfo *Name, |
1007 | | SourceLocation NameLoc, |
1008 | | unsigned Depth, |
1009 | | unsigned Position, |
1010 | | SourceLocation EqualLoc, |
1011 | 831 | ParsedTemplateArgument Default) { |
1012 | 831 | assert(S->isTemplateParamScope() && |
1013 | 831 | "Template template parameter not in template parameter scope!"); |
1014 | 831 | |
1015 | 831 | // Construct the parameter object. |
1016 | 831 | bool IsParameterPack = EllipsisLoc.isValid(); |
1017 | 831 | TemplateTemplateParmDecl *Param = |
1018 | 831 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
1019 | 831 | NameLoc.isInvalid()? TmpLoc200 : NameLoc631 , |
1020 | 831 | Depth, Position, IsParameterPack, |
1021 | 831 | Name, Params); |
1022 | 831 | Param->setAccess(AS_public); |
1023 | 831 | |
1024 | 831 | // If the template template parameter has a name, then link the identifier |
1025 | 831 | // into the scope and lookup mechanisms. |
1026 | 831 | if (Name831 ) { |
1027 | 631 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
1028 | 631 | |
1029 | 631 | S->AddDecl(Param); |
1030 | 631 | IdResolver.AddDecl(Param); |
1031 | 631 | } |
1032 | 831 | |
1033 | 831 | if (Params->size() == 0831 ) { |
1034 | 1 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
1035 | 1 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
1036 | 1 | Param->setInvalidDecl(); |
1037 | 1 | } |
1038 | 831 | |
1039 | 831 | // C++0x [temp.param]p9: |
1040 | 831 | // A default template-argument may be specified for any kind of |
1041 | 831 | // template-parameter that is not a template parameter pack. |
1042 | 831 | if (IsParameterPack && 831 !Default.isInvalid()63 ) { |
1043 | 1 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1044 | 1 | Default = ParsedTemplateArgument(); |
1045 | 1 | } |
1046 | 831 | |
1047 | 831 | if (!Default.isInvalid()831 ) { |
1048 | 132 | // Check only that we have a template template argument. We don't want to |
1049 | 132 | // try to check well-formedness now, because our template template parameter |
1050 | 132 | // might have dependent types in its template parameters, which we wouldn't |
1051 | 132 | // be able to match now. |
1052 | 132 | // |
1053 | 132 | // If none of the template template parameter's template arguments mention |
1054 | 132 | // other template parameters, we could actually perform more checking here. |
1055 | 132 | // However, it isn't worth doing. |
1056 | 132 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
1057 | 132 | if (DefaultArg.getArgument().getAsTemplate().isNull()132 ) { |
1058 | 0 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
1059 | 0 | << DefaultArg.getSourceRange(); |
1060 | 0 | return Param; |
1061 | 0 | } |
1062 | 132 | |
1063 | 132 | // Check for unexpanded parameter packs. |
1064 | 132 | if (132 DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
1065 | 132 | DefaultArg.getArgument().getAsTemplate(), |
1066 | 132 | UPPC_DefaultArgument)) |
1067 | 1 | return Param; |
1068 | 131 | |
1069 | 131 | Param->setDefaultArgument(Context, DefaultArg); |
1070 | 131 | } |
1071 | 831 | |
1072 | 830 | return Param; |
1073 | 831 | } |
1074 | | |
1075 | | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
1076 | | /// constrained by RequiresClause, that contains the template parameters in |
1077 | | /// Params. |
1078 | | TemplateParameterList * |
1079 | | Sema::ActOnTemplateParameterList(unsigned Depth, |
1080 | | SourceLocation ExportLoc, |
1081 | | SourceLocation TemplateLoc, |
1082 | | SourceLocation LAngleLoc, |
1083 | | ArrayRef<NamedDecl *> Params, |
1084 | | SourceLocation RAngleLoc, |
1085 | 73.5k | Expr *RequiresClause) { |
1086 | 73.5k | if (ExportLoc.isValid()) |
1087 | 3 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
1088 | 73.5k | |
1089 | 73.5k | return TemplateParameterList::Create( |
1090 | 73.5k | Context, TemplateLoc, LAngleLoc, |
1091 | 73.5k | llvm::makeArrayRef(Params.data(), Params.size()), |
1092 | 73.5k | RAngleLoc, RequiresClause); |
1093 | 73.5k | } |
1094 | | |
1095 | 31.0k | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
1096 | 31.0k | if (SS.isSet()) |
1097 | 391 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
1098 | 31.0k | } |
1099 | | |
1100 | | DeclResult |
1101 | | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
1102 | | SourceLocation KWLoc, CXXScopeSpec &SS, |
1103 | | IdentifierInfo *Name, SourceLocation NameLoc, |
1104 | | AttributeList *Attr, |
1105 | | TemplateParameterList *TemplateParams, |
1106 | | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
1107 | | SourceLocation FriendLoc, |
1108 | | unsigned NumOuterTemplateParamLists, |
1109 | | TemplateParameterList** OuterTemplateParamLists, |
1110 | 23.8k | SkipBodyInfo *SkipBody) { |
1111 | 23.8k | assert(TemplateParams && TemplateParams->size() > 0 && |
1112 | 23.8k | "No template parameters"); |
1113 | 23.8k | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
1114 | 23.8k | bool Invalid = false; |
1115 | 23.8k | |
1116 | 23.8k | // Check that we can declare a template here. |
1117 | 23.8k | if (CheckTemplateDeclScope(S, TemplateParams)) |
1118 | 3 | return true; |
1119 | 23.8k | |
1120 | 23.8k | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
1121 | 23.8k | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
1122 | 23.8k | |
1123 | 23.8k | // There is no such thing as an unnamed class template. |
1124 | 23.8k | if (!Name23.8k ) { |
1125 | 0 | Diag(KWLoc, diag::err_template_unnamed_class); |
1126 | 0 | return true; |
1127 | 0 | } |
1128 | 23.8k | |
1129 | 23.8k | // Find any previous declaration with this name. For a friend with no |
1130 | 23.8k | // scope explicitly specified, we only look for tag declarations (per |
1131 | 23.8k | // C++11 [basic.lookup.elab]p2). |
1132 | 23.8k | DeclContext *SemanticContext; |
1133 | 23.8k | LookupResult Previous(*this, Name, NameLoc, |
1134 | 23.7k | (SS.isEmpty() && TUK == TUK_Friend) |
1135 | 23.8k | ? LookupTagName185 : LookupOrdinaryName23.6k , |
1136 | 23.8k | ForRedeclaration); |
1137 | 23.8k | if (SS.isNotEmpty() && 23.8k !SS.isInvalid()107 ) { |
1138 | 107 | SemanticContext = computeDeclContext(SS, true); |
1139 | 107 | if (!SemanticContext107 ) { |
1140 | 2 | // FIXME: Horrible, horrible hack! We can't currently represent this |
1141 | 2 | // in the AST, and historically we have just ignored such friend |
1142 | 2 | // class templates, so don't complain here. |
1143 | 2 | Diag(NameLoc, TUK == TUK_Friend |
1144 | 1 | ? diag::warn_template_qualified_friend_ignored |
1145 | 1 | : diag::err_template_qualified_declarator_no_match) |
1146 | 2 | << SS.getScopeRep() << SS.getRange(); |
1147 | 2 | return TUK != TUK_Friend; |
1148 | 2 | } |
1149 | 105 | |
1150 | 105 | if (105 RequireCompleteDeclContext(SS, SemanticContext)105 ) |
1151 | 0 | return true; |
1152 | 105 | |
1153 | 105 | // If we're adding a template to a dependent context, we may need to |
1154 | 105 | // rebuilding some of the types used within the template parameter list, |
1155 | 105 | // now that we know what the current instantiation is. |
1156 | 105 | if (105 SemanticContext->isDependentContext()105 ) { |
1157 | 20 | ContextRAII SavedContext(*this, SemanticContext); |
1158 | 20 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
1159 | 0 | Invalid = true; |
1160 | 105 | } else if (85 TUK != TUK_Friend && 85 TUK != TUK_Reference75 ) |
1161 | 75 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
1162 | 107 | |
1163 | 107 | LookupQualifiedName(Previous, SemanticContext); |
1164 | 23.8k | } else { |
1165 | 23.7k | SemanticContext = CurContext; |
1166 | 23.7k | |
1167 | 23.7k | // C++14 [class.mem]p14: |
1168 | 23.7k | // If T is the name of a class, then each of the following shall have a |
1169 | 23.7k | // name different from T: |
1170 | 23.7k | // -- every member template of class T |
1171 | 23.7k | if (TUK != TUK_Friend && |
1172 | 23.5k | DiagnoseClassNameShadow(SemanticContext, |
1173 | 23.5k | DeclarationNameInfo(Name, NameLoc))) |
1174 | 6 | return true; |
1175 | 23.6k | |
1176 | 23.6k | LookupName(Previous, S); |
1177 | 23.6k | } |
1178 | 23.8k | |
1179 | 23.8k | if (23.8k Previous.isAmbiguous()23.8k ) |
1180 | 0 | return true; |
1181 | 23.8k | |
1182 | 23.8k | NamedDecl *PrevDecl = nullptr; |
1183 | 23.8k | if (Previous.begin() != Previous.end()) |
1184 | 1.56k | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1185 | 23.8k | |
1186 | 23.8k | if (PrevDecl && 23.8k PrevDecl->isTemplateParameter()1.56k ) { |
1187 | 7 | // Maybe we will complain about the shadowed template parameter. |
1188 | 7 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
1189 | 7 | // Just pretend that we didn't see the previous declaration. |
1190 | 7 | PrevDecl = nullptr; |
1191 | 7 | } |
1192 | 23.8k | |
1193 | 23.8k | // If there is a previous declaration with the same name, check |
1194 | 23.8k | // whether this is a valid redeclaration. |
1195 | 23.8k | ClassTemplateDecl *PrevClassTemplate |
1196 | 23.8k | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
1197 | 23.8k | |
1198 | 23.8k | // We may have found the injected-class-name of a class template, |
1199 | 23.8k | // class template partial specialization, or class template specialization. |
1200 | 23.8k | // In these cases, grab the template that is being defined or specialized. |
1201 | 23.8k | if (!PrevClassTemplate && 23.8k PrevDecl22.3k && isa<CXXRecordDecl>(PrevDecl)63 && |
1202 | 23.8k | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()54 ) { |
1203 | 39 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
1204 | 39 | PrevClassTemplate |
1205 | 39 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
1206 | 39 | if (!PrevClassTemplate && 39 isa<ClassTemplateSpecializationDecl>(PrevDecl)2 ) { |
1207 | 2 | PrevClassTemplate |
1208 | 2 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
1209 | 2 | ->getSpecializedTemplate(); |
1210 | 2 | } |
1211 | 39 | } |
1212 | 23.8k | |
1213 | 23.8k | if (TUK == TUK_Friend23.8k ) { |
1214 | 196 | // C++ [namespace.memdef]p3: |
1215 | 196 | // [...] When looking for a prior declaration of a class or a function |
1216 | 196 | // declared as a friend, and when the name of the friend class or |
1217 | 196 | // function is neither a qualified name nor a template-id, scopes outside |
1218 | 196 | // the innermost enclosing namespace scope are not considered. |
1219 | 196 | if (!SS.isSet()196 ) { |
1220 | 185 | DeclContext *OutermostContext = CurContext; |
1221 | 380 | while (!OutermostContext->isFileContext()) |
1222 | 195 | OutermostContext = OutermostContext->getLookupParent(); |
1223 | 185 | |
1224 | 185 | if (PrevDecl && |
1225 | 139 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
1226 | 185 | OutermostContext->Encloses(PrevDecl->getDeclContext())4 )) { |
1227 | 139 | SemanticContext = PrevDecl->getDeclContext(); |
1228 | 185 | } else { |
1229 | 46 | // Declarations in outer scopes don't matter. However, the outermost |
1230 | 46 | // context we computed is the semantic context for our new |
1231 | 46 | // declaration. |
1232 | 46 | PrevDecl = PrevClassTemplate = nullptr; |
1233 | 46 | SemanticContext = OutermostContext; |
1234 | 46 | |
1235 | 46 | // Check that the chosen semantic context doesn't already contain a |
1236 | 46 | // declaration of this name as a non-tag type. |
1237 | 46 | Previous.clear(LookupOrdinaryName); |
1238 | 46 | DeclContext *LookupContext = SemanticContext; |
1239 | 46 | while (LookupContext->isTransparentContext()) |
1240 | 0 | LookupContext = LookupContext->getLookupParent(); |
1241 | 46 | LookupQualifiedName(Previous, LookupContext); |
1242 | 46 | |
1243 | 46 | if (Previous.isAmbiguous()) |
1244 | 0 | return true; |
1245 | 46 | |
1246 | 46 | if (46 Previous.begin() != Previous.end()46 ) |
1247 | 1 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1248 | 46 | } |
1249 | 185 | } |
1250 | 23.8k | } else if (23.6k PrevDecl && |
1251 | 1.40k | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
1252 | 1.40k | S, SS.isValid())) |
1253 | 42 | PrevDecl = PrevClassTemplate = nullptr; |
1254 | 23.8k | |
1255 | 23.8k | if (auto *23.8k Shadow23.8k = dyn_cast_or_null<UsingShadowDecl>( |
1256 | 2 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
1257 | 2 | if (SS.isEmpty() && |
1258 | 2 | !(PrevClassTemplate && |
1259 | 2 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
1260 | 2 | SemanticContext->getRedeclContext()))) { |
1261 | 2 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
1262 | 2 | Diag(Shadow->getTargetDecl()->getLocation(), |
1263 | 2 | diag::note_using_decl_target); |
1264 | 2 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
1265 | 2 | // Recover by ignoring the old declaration. |
1266 | 2 | PrevDecl = PrevClassTemplate = nullptr; |
1267 | 2 | } |
1268 | 2 | } |
1269 | 23.8k | |
1270 | 23.8k | // TODO Memory management; associated constraints are not always stored. |
1271 | 23.8k | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
1272 | 23.8k | |
1273 | 23.8k | if (PrevClassTemplate23.8k ) { |
1274 | 1.50k | // Ensure that the template parameter lists are compatible. Skip this check |
1275 | 1.50k | // for a friend in a dependent context: the template parameter list itself |
1276 | 1.50k | // could be dependent. |
1277 | 1.50k | if (!(TUK == TUK_Friend && 1.50k CurContext->isDependentContext()148 ) && |
1278 | 1.38k | !TemplateParameterListsAreEqual(TemplateParams, |
1279 | 1.38k | PrevClassTemplate->getTemplateParameters(), |
1280 | 1.38k | /*Complain=*/true, |
1281 | 1.38k | TPL_TemplateMatch)) |
1282 | 10 | return true; |
1283 | 1.49k | |
1284 | 1.49k | // Check for matching associated constraints on redeclarations. |
1285 | 1.49k | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
1286 | 1.49k | const bool RedeclACMismatch = [&] { |
1287 | 1.49k | if (!(CurAC || 1.49k PrevAC1.49k )) |
1288 | 1.48k | return false; // Nothing to check; no mismatch. |
1289 | 10 | if (10 CurAC && 10 PrevAC8 ) { |
1290 | 7 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
1291 | 7 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
1292 | 7 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
1293 | 7 | if (CurACInfo == PrevACInfo) |
1294 | 5 | return false; // All good; no mismatch. |
1295 | 5 | } |
1296 | 5 | return true; |
1297 | 5 | }(); |
1298 | 1.49k | |
1299 | 1.49k | if (RedeclACMismatch1.49k ) { |
1300 | 5 | Diag(CurAC ? CurAC->getLocStart()3 : NameLoc2 , |
1301 | 5 | diag::err_template_different_associated_constraints); |
1302 | 5 | Diag(PrevAC ? PrevAC->getLocStart()4 : PrevClassTemplate->getLocation()1 , |
1303 | 5 | diag::note_template_prev_declaration) << /*declaration*/0; |
1304 | 5 | return true; |
1305 | 5 | } |
1306 | 1.49k | |
1307 | 1.49k | // C++ [temp.class]p4: |
1308 | 1.49k | // In a redeclaration, partial specialization, explicit |
1309 | 1.49k | // specialization or explicit instantiation of a class template, |
1310 | 1.49k | // the class-key shall agree in kind with the original class |
1311 | 1.49k | // template declaration (7.1.5.3). |
1312 | 1.49k | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
1313 | 1.49k | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
1314 | 1.49k | TUK == TUK_Definition, KWLoc, Name)) { |
1315 | 0 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
1316 | 0 | << Name |
1317 | 0 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
1318 | 0 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
1319 | 0 | Kind = PrevRecordDecl->getTagKind(); |
1320 | 0 | } |
1321 | 1.49k | |
1322 | 1.49k | // Check for redefinition of this class template. |
1323 | 1.49k | if (TUK == TUK_Definition1.49k ) { |
1324 | 998 | if (TagDecl *Def998 = PrevRecordDecl->getDefinition()) { |
1325 | 109 | // If we have a prior definition that is not visible, treat this as |
1326 | 109 | // simply making that previous definition visible. |
1327 | 109 | NamedDecl *Hidden = nullptr; |
1328 | 109 | if (SkipBody && 109 !hasVisibleDefinition(Def, &Hidden)109 ) { |
1329 | 108 | SkipBody->ShouldSkip = true; |
1330 | 108 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
1331 | 108 | assert(Tmpl && "original definition of a class template is not a " |
1332 | 108 | "class template?"); |
1333 | 108 | makeMergedDefinitionVisible(Hidden); |
1334 | 108 | makeMergedDefinitionVisible(Tmpl); |
1335 | 108 | return Def; |
1336 | 108 | } |
1337 | 1 | |
1338 | 1 | Diag(NameLoc, diag::err_redefinition) << Name; |
1339 | 1 | Diag(Def->getLocation(), diag::note_previous_definition); |
1340 | 1 | // FIXME: Would it make sense to try to "forget" the previous |
1341 | 1 | // definition, as part of error recovery? |
1342 | 1 | return true; |
1343 | 1 | } |
1344 | 998 | } |
1345 | 22.2k | } else if (22.2k PrevDecl22.2k ) { |
1346 | 6 | // C++ [temp]p5: |
1347 | 6 | // A class template shall not have the same name as any other |
1348 | 6 | // template, class, function, object, enumeration, enumerator, |
1349 | 6 | // namespace, or type in the same scope (3.3), except as specified |
1350 | 6 | // in (14.5.4). |
1351 | 6 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
1352 | 6 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
1353 | 6 | return true; |
1354 | 6 | } |
1355 | 23.6k | |
1356 | 23.6k | // Check the template parameter list of this declaration, possibly |
1357 | 23.6k | // merging in the template parameter list from the previous class |
1358 | 23.6k | // template declaration. Skip this check for a friend in a dependent |
1359 | 23.6k | // context, because the template parameter list might be dependent. |
1360 | 23.6k | if (23.6k !(TUK == TUK_Friend && 23.6k CurContext->isDependentContext()193 ) && |
1361 | 23.5k | CheckTemplateParameterList( |
1362 | 23.5k | TemplateParams, |
1363 | 1.26k | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
1364 | 22.2k | : nullptr, |
1365 | 23.5k | (SS.isSet() && SemanticContext90 && SemanticContext->isRecord()90 && |
1366 | 80 | SemanticContext->isDependentContext()) |
1367 | 18 | ? TPC_ClassTemplateMember |
1368 | 23.5k | : TUK == TUK_Friend ? 23.5k TPC_FriendClassTemplate49 |
1369 | 23.4k | : TPC_ClassTemplate)) |
1370 | 23 | Invalid = true; |
1371 | 23.6k | |
1372 | 23.6k | if (SS.isSet()23.6k ) { |
1373 | 97 | // If the name of the template was qualified, we must be defining the |
1374 | 97 | // template out-of-line. |
1375 | 97 | if (!SS.isInvalid() && 97 !Invalid97 && !PrevClassTemplate97 ) { |
1376 | 1 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
1377 | 2 | : diag::err_member_decl_does_not_match) |
1378 | 3 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
1379 | 3 | Invalid = true; |
1380 | 3 | } |
1381 | 97 | } |
1382 | 23.6k | |
1383 | 23.6k | // If this is a templated friend in a dependent context we should not put it |
1384 | 23.6k | // on the redecl chain. In some cases, the templated friend can be the most |
1385 | 23.6k | // recent declaration tricking the template instantiator to make substitutions |
1386 | 23.6k | // there. |
1387 | 23.6k | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
1388 | 23.6k | bool ShouldAddRedecl |
1389 | 193 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
1390 | 23.6k | |
1391 | 23.6k | CXXRecordDecl *NewClass = |
1392 | 23.6k | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
1393 | 1.38k | PrevClassTemplate && ShouldAddRedecl ? |
1394 | 23.6k | PrevClassTemplate->getTemplatedDecl()1.26k : nullptr22.4k , |
1395 | 23.6k | /*DelayTypeCreation=*/true); |
1396 | 23.6k | SetNestedNameSpecifier(NewClass, SS); |
1397 | 23.6k | if (NumOuterTemplateParamLists > 0) |
1398 | 52 | NewClass->setTemplateParameterListsInfo( |
1399 | 52 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
1400 | 52 | NumOuterTemplateParamLists)); |
1401 | 23.6k | |
1402 | 23.6k | // Add alignment attributes if necessary; these attributes are checked when |
1403 | 23.6k | // the ASTContext lays out the structure. |
1404 | 23.6k | if (TUK == TUK_Definition23.6k ) { |
1405 | 21.1k | AddAlignmentAttributesForRecord(NewClass); |
1406 | 21.1k | AddMsStructLayoutForRecord(NewClass); |
1407 | 21.1k | } |
1408 | 23.6k | |
1409 | 23.6k | // Attach the associated constraints when the declaration will not be part of |
1410 | 23.6k | // a decl chain. |
1411 | 23.6k | Expr *const ACtoAttach = |
1412 | 23.6k | PrevClassTemplate && ShouldAddRedecl1.38k ? nullptr1.26k : CurAC22.4k ; |
1413 | 23.6k | |
1414 | 23.6k | ClassTemplateDecl *NewTemplate |
1415 | 23.6k | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
1416 | 23.6k | DeclarationName(Name), TemplateParams, |
1417 | 23.6k | NewClass, ACtoAttach); |
1418 | 23.6k | |
1419 | 23.6k | if (ShouldAddRedecl) |
1420 | 23.5k | NewTemplate->setPreviousDecl(PrevClassTemplate); |
1421 | 23.6k | |
1422 | 23.6k | NewClass->setDescribedClassTemplate(NewTemplate); |
1423 | 23.6k | |
1424 | 23.6k | if (ModulePrivateLoc.isValid()) |
1425 | 3 | NewTemplate->setModulePrivate(); |
1426 | 23.6k | |
1427 | 23.6k | // Build the type for the class template declaration now. |
1428 | 23.6k | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
1429 | 23.6k | T = Context.getInjectedClassNameType(NewClass, T); |
1430 | 23.6k | assert(T->isDependentType() && "Class template type is not dependent?"); |
1431 | 23.6k | (void)T; |
1432 | 23.6k | |
1433 | 23.6k | // If we are providing an explicit specialization of a member that is a |
1434 | 23.6k | // class template, make a note of that. |
1435 | 23.6k | if (PrevClassTemplate && |
1436 | 1.38k | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
1437 | 30 | PrevClassTemplate->setMemberSpecialization(); |
1438 | 23.6k | |
1439 | 23.6k | // Set the access specifier. |
1440 | 23.6k | if (!Invalid && 23.6k TUK != TUK_Friend23.6k && NewTemplate->getDeclContext()->isRecord()23.4k ) |
1441 | 1.95k | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
1442 | 23.6k | |
1443 | 23.6k | // Set the lexical context of these templates |
1444 | 23.6k | NewClass->setLexicalDeclContext(CurContext); |
1445 | 23.6k | NewTemplate->setLexicalDeclContext(CurContext); |
1446 | 23.6k | |
1447 | 23.6k | if (TUK == TUK_Definition) |
1448 | 21.1k | NewClass->startDefinition(); |
1449 | 23.6k | |
1450 | 23.6k | if (Attr) |
1451 | 277 | ProcessDeclAttributeList(S, NewClass, Attr); |
1452 | 23.6k | |
1453 | 23.6k | if (PrevClassTemplate) |
1454 | 1.38k | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
1455 | 23.6k | |
1456 | 23.6k | AddPushedVisibilityAttribute(NewClass); |
1457 | 23.6k | |
1458 | 23.6k | if (TUK != TUK_Friend23.6k ) { |
1459 | 23.4k | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
1460 | 23.4k | Scope *Outer = S; |
1461 | 46.9k | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
1462 | 23.4k | Outer = Outer->getParent(); |
1463 | 23.4k | PushOnScopeChains(NewTemplate, Outer); |
1464 | 23.6k | } else { |
1465 | 193 | if (PrevClassTemplate && 193 PrevClassTemplate->getAccess() != AS_none147 ) { |
1466 | 9 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
1467 | 9 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
1468 | 9 | } |
1469 | 193 | |
1470 | 193 | NewTemplate->setObjectOfFriendDecl(); |
1471 | 193 | |
1472 | 193 | // Friend templates are visible in fairly strange ways. |
1473 | 193 | if (!CurContext->isDependentContext()193 ) { |
1474 | 49 | DeclContext *DC = SemanticContext->getRedeclContext(); |
1475 | 49 | DC->makeDeclVisibleInContext(NewTemplate); |
1476 | 49 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
1477 | 46 | PushOnScopeChains(NewTemplate, EnclosingScope, |
1478 | 46 | /* AddToContext = */ false); |
1479 | 49 | } |
1480 | 193 | |
1481 | 193 | FriendDecl *Friend = FriendDecl::Create( |
1482 | 193 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
1483 | 193 | Friend->setAccess(AS_public); |
1484 | 193 | CurContext->addDecl(Friend); |
1485 | 193 | } |
1486 | 23.6k | |
1487 | 23.6k | if (Invalid23.6k ) { |
1488 | 26 | NewTemplate->setInvalidDecl(); |
1489 | 26 | NewClass->setInvalidDecl(); |
1490 | 26 | } |
1491 | 23.8k | |
1492 | 23.8k | ActOnDocumentableDecl(NewTemplate); |
1493 | 23.8k | |
1494 | 23.8k | return NewTemplate; |
1495 | 23.8k | } |
1496 | | |
1497 | | namespace { |
1498 | | /// Transform to convert portions of a constructor declaration into the |
1499 | | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
1500 | | struct ConvertConstructorToDeductionGuideTransform { |
1501 | | ConvertConstructorToDeductionGuideTransform(Sema &S, |
1502 | | ClassTemplateDecl *Template) |
1503 | 243 | : SemaRef(S), Template(Template) {} |
1504 | | |
1505 | | Sema &SemaRef; |
1506 | | ClassTemplateDecl *Template; |
1507 | | |
1508 | | DeclContext *DC = Template->getDeclContext(); |
1509 | | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
1510 | | DeclarationName DeductionGuideName = |
1511 | | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
1512 | | |
1513 | | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
1514 | | |
1515 | | // Index adjustment to apply to convert depth-1 template parameters into |
1516 | | // depth-0 template parameters. |
1517 | | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
1518 | | |
1519 | | /// Transform a constructor declaration into a deduction guide. |
1520 | | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
1521 | 48 | CXXConstructorDecl *CD) { |
1522 | 48 | SmallVector<TemplateArgument, 16> SubstArgs; |
1523 | 48 | |
1524 | 48 | LocalInstantiationScope Scope(SemaRef); |
1525 | 48 | |
1526 | 48 | // C++ [over.match.class.deduct]p1: |
1527 | 48 | // -- For each constructor of the class template designated by the |
1528 | 48 | // template-name, a function template with the following properties: |
1529 | 48 | |
1530 | 48 | // -- The template parameters are the template parameters of the class |
1531 | 48 | // template followed by the template parameters (including default |
1532 | 48 | // template arguments) of the constructor, if any. |
1533 | 48 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
1534 | 48 | if (FTD48 ) { |
1535 | 18 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
1536 | 18 | SmallVector<NamedDecl *, 16> AllParams; |
1537 | 18 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
1538 | 18 | AllParams.insert(AllParams.begin(), |
1539 | 18 | TemplateParams->begin(), TemplateParams->end()); |
1540 | 18 | SubstArgs.reserve(InnerParams->size()); |
1541 | 18 | |
1542 | 18 | // Later template parameters could refer to earlier ones, so build up |
1543 | 18 | // a list of substituted template arguments as we go. |
1544 | 24 | for (NamedDecl *Param : *InnerParams) { |
1545 | 24 | MultiLevelTemplateArgumentList Args; |
1546 | 24 | Args.addOuterTemplateArguments(SubstArgs); |
1547 | 24 | Args.addOuterRetainedLevel(); |
1548 | 24 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
1549 | 24 | if (!NewParam) |
1550 | 0 | return nullptr; |
1551 | 24 | AllParams.push_back(NewParam); |
1552 | 24 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
1553 | 24 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
1554 | 24 | } |
1555 | 18 | TemplateParams = TemplateParameterList::Create( |
1556 | 18 | SemaRef.Context, InnerParams->getTemplateLoc(), |
1557 | 18 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
1558 | 18 | /*FIXME: RequiresClause*/ nullptr); |
1559 | 18 | } |
1560 | 48 | |
1561 | 48 | // If we built a new template-parameter-list, track that we need to |
1562 | 48 | // substitute references to the old parameters into references to the |
1563 | 48 | // new ones. |
1564 | 48 | MultiLevelTemplateArgumentList Args; |
1565 | 48 | if (FTD48 ) { |
1566 | 18 | Args.addOuterTemplateArguments(SubstArgs); |
1567 | 18 | Args.addOuterRetainedLevel(); |
1568 | 18 | } |
1569 | 48 | |
1570 | 48 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
1571 | 48 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
1572 | 48 | assert(FPTL && "no prototype for constructor declaration"); |
1573 | 48 | |
1574 | 48 | // Transform the type of the function, adjusting the return type and |
1575 | 48 | // replacing references to the old parameters with references to the |
1576 | 48 | // new ones. |
1577 | 48 | TypeLocBuilder TLB; |
1578 | 48 | SmallVector<ParmVarDecl*, 8> Params; |
1579 | 48 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
1580 | 48 | if (NewType.isNull()) |
1581 | 0 | return nullptr; |
1582 | 48 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
1583 | 48 | |
1584 | 48 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
1585 | 48 | CD->getLocStart(), CD->getLocation(), |
1586 | 48 | CD->getLocEnd()); |
1587 | 48 | } |
1588 | | |
1589 | | /// Build a deduction guide with the specified parameter types. |
1590 | 81 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
1591 | 81 | SourceLocation Loc = Template->getLocation(); |
1592 | 81 | |
1593 | 81 | // Build the requested type. |
1594 | 81 | FunctionProtoType::ExtProtoInfo EPI; |
1595 | 81 | EPI.HasTrailingReturn = true; |
1596 | 81 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
1597 | 81 | DeductionGuideName, EPI); |
1598 | 81 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
1599 | 81 | |
1600 | 81 | FunctionProtoTypeLoc FPTL = |
1601 | 81 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
1602 | 81 | |
1603 | 81 | // Build the parameters, needed during deduction / substitution. |
1604 | 81 | SmallVector<ParmVarDecl*, 4> Params; |
1605 | 61 | for (auto T : ParamTypes) { |
1606 | 61 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
1607 | 61 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
1608 | 61 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
1609 | 61 | NewParam->setScopeInfo(0, Params.size()); |
1610 | 61 | FPTL.setParam(Params.size(), NewParam); |
1611 | 61 | Params.push_back(NewParam); |
1612 | 61 | } |
1613 | 81 | |
1614 | 81 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
1615 | 81 | Loc, Loc, Loc); |
1616 | 81 | } |
1617 | | |
1618 | | private: |
1619 | | /// Transform a constructor template parameter into a deduction guide template |
1620 | | /// parameter, rebuilding any internal references to earlier parameters and |
1621 | | /// renumbering as we go. |
1622 | | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
1623 | 24 | MultiLevelTemplateArgumentList &Args) { |
1624 | 24 | if (auto *TTP24 = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
1625 | 13 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
1626 | 13 | // substitute it directly. |
1627 | 13 | auto *NewTTP = TemplateTypeParmDecl::Create( |
1628 | 13 | SemaRef.Context, DC, TTP->getLocStart(), TTP->getLocation(), |
1629 | 13 | /*Depth*/0, Depth1IndexAdjustment + TTP->getIndex(), |
1630 | 13 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
1631 | 13 | TTP->isParameterPack()); |
1632 | 13 | if (TTP->hasDefaultArgument()13 ) { |
1633 | 1 | TypeSourceInfo *InstantiatedDefaultArg = |
1634 | 1 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
1635 | 1 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
1636 | 1 | if (InstantiatedDefaultArg) |
1637 | 1 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
1638 | 1 | } |
1639 | 13 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
1640 | 13 | NewTTP); |
1641 | 13 | return NewTTP; |
1642 | 13 | } |
1643 | 11 | |
1644 | 11 | if (auto *11 TTP11 = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
1645 | 3 | return transformTemplateParameterImpl(TTP, Args); |
1646 | 8 | |
1647 | 8 | return transformTemplateParameterImpl( |
1648 | 8 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
1649 | 8 | } |
1650 | | template<typename TemplateParmDecl> |
1651 | | TemplateParmDecl * |
1652 | | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
1653 | 11 | MultiLevelTemplateArgumentList &Args) { |
1654 | 11 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
1655 | 11 | // the index of the parameter once it's done. |
1656 | 11 | auto *NewParam = |
1657 | 11 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
1658 | 11 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
1659 | 11 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
1660 | 11 | return NewParam; |
1661 | 11 | } SemaTemplate.cpp:clang::TemplateTemplateParmDecl* (anonymous namespace)::ConvertConstructorToDeductionGuideTransform::transformTemplateParameterImpl<clang::TemplateTemplateParmDecl>(clang::TemplateTemplateParmDecl*, clang::MultiLevelTemplateArgumentList&) Line | Count | Source | 1653 | 3 | MultiLevelTemplateArgumentList &Args) { | 1654 | 3 | // Ask the template instantiator to do the heavy lifting for us, then adjust | 1655 | 3 | // the index of the parameter once it's done. | 1656 | 3 | auto *NewParam = | 1657 | 3 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); | 1658 | 3 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); | 1659 | 3 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); | 1660 | 3 | return NewParam; | 1661 | 3 | } |
SemaTemplate.cpp:clang::NonTypeTemplateParmDecl* (anonymous namespace)::ConvertConstructorToDeductionGuideTransform::transformTemplateParameterImpl<clang::NonTypeTemplateParmDecl>(clang::NonTypeTemplateParmDecl*, clang::MultiLevelTemplateArgumentList&) Line | Count | Source | 1653 | 8 | MultiLevelTemplateArgumentList &Args) { | 1654 | 8 | // Ask the template instantiator to do the heavy lifting for us, then adjust | 1655 | 8 | // the index of the parameter once it's done. | 1656 | 8 | auto *NewParam = | 1657 | 8 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); | 1658 | 8 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); | 1659 | 8 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); | 1660 | 8 | return NewParam; | 1661 | 8 | } |
|
1662 | | |
1663 | | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
1664 | | FunctionProtoTypeLoc TL, |
1665 | | SmallVectorImpl<ParmVarDecl*> &Params, |
1666 | 48 | MultiLevelTemplateArgumentList &Args) { |
1667 | 48 | SmallVector<QualType, 4> ParamTypes; |
1668 | 48 | const FunctionProtoType *T = TL.getTypePtr(); |
1669 | 48 | |
1670 | 48 | // -- The types of the function parameters are those of the constructor. |
1671 | 62 | for (auto *OldParam : TL.getParams()) { |
1672 | 62 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
1673 | 62 | if (!NewParam) |
1674 | 0 | return QualType(); |
1675 | 62 | ParamTypes.push_back(NewParam->getType()); |
1676 | 62 | Params.push_back(NewParam); |
1677 | 62 | } |
1678 | 48 | |
1679 | 48 | // -- The return type is the class template specialization designated by |
1680 | 48 | // the template-name and template arguments corresponding to the |
1681 | 48 | // template parameters obtained from the class template. |
1682 | 48 | // |
1683 | 48 | // We use the injected-class-name type of the primary template instead. |
1684 | 48 | // This has the convenient property that it is different from any type that |
1685 | 48 | // the user can write in a deduction-guide (because they cannot enter the |
1686 | 48 | // context of the template), so implicit deduction guides can never collide |
1687 | 48 | // with explicit ones. |
1688 | 48 | QualType ReturnType = DeducedType; |
1689 | 48 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
1690 | 48 | |
1691 | 48 | // Resolving a wording defect, we also inherit the variadicness of the |
1692 | 48 | // constructor. |
1693 | 48 | FunctionProtoType::ExtProtoInfo EPI; |
1694 | 48 | EPI.Variadic = T->isVariadic(); |
1695 | 48 | EPI.HasTrailingReturn = true; |
1696 | 48 | |
1697 | 48 | QualType Result = SemaRef.BuildFunctionType( |
1698 | 48 | ReturnType, ParamTypes, TL.getLocStart(), DeductionGuideName, EPI); |
1699 | 48 | if (Result.isNull()) |
1700 | 0 | return QualType(); |
1701 | 48 | |
1702 | 48 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
1703 | 48 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
1704 | 48 | NewTL.setLParenLoc(TL.getLParenLoc()); |
1705 | 48 | NewTL.setRParenLoc(TL.getRParenLoc()); |
1706 | 48 | NewTL.setExceptionSpecRange(SourceRange()); |
1707 | 48 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
1708 | 110 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E110 ; ++I62 ) |
1709 | 62 | NewTL.setParam(I, Params[I]); |
1710 | 48 | |
1711 | 48 | return Result; |
1712 | 48 | } |
1713 | | |
1714 | | ParmVarDecl * |
1715 | | transformFunctionTypeParam(ParmVarDecl *OldParam, |
1716 | 62 | MultiLevelTemplateArgumentList &Args) { |
1717 | 62 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
1718 | 62 | TypeSourceInfo *NewDI; |
1719 | 62 | if (!Args.getNumLevels()) |
1720 | 32 | NewDI = OldDI; |
1721 | 30 | else if (auto 30 PackTL30 = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
1722 | 9 | // Expand out the one and only element in each inner pack. |
1723 | 9 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
1724 | 9 | NewDI = |
1725 | 9 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
1726 | 9 | OldParam->getLocation(), OldParam->getDeclName()); |
1727 | 9 | if (!NewDI9 ) return nullptr0 ; |
1728 | 9 | NewDI = |
1729 | 9 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
1730 | 9 | PackTL.getTypePtr()->getNumExpansions()); |
1731 | 9 | } else |
1732 | 21 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
1733 | 21 | OldParam->getDeclName()); |
1734 | 62 | if (62 !NewDI62 ) |
1735 | 0 | return nullptr; |
1736 | 62 | |
1737 | 62 | // Canonicalize the type. This (for instance) replaces references to |
1738 | 62 | // typedef members of the current instantiations with the definitions of |
1739 | 62 | // those typedefs, avoiding triggering instantiation of the deduced type |
1740 | 62 | // during deduction. |
1741 | 62 | // FIXME: It would be preferable to retain type sugar and source |
1742 | 62 | // information here (and handle this in substitution instead). |
1743 | 62 | NewDI = SemaRef.Context.getTrivialTypeSourceInfo( |
1744 | 62 | SemaRef.Context.getCanonicalType(NewDI->getType()), |
1745 | 62 | OldParam->getLocation()); |
1746 | 62 | |
1747 | 62 | // Resolving a wording defect, we also inherit default arguments from the |
1748 | 62 | // constructor. |
1749 | 62 | ExprResult NewDefArg; |
1750 | 62 | if (OldParam->hasDefaultArg()62 ) { |
1751 | 5 | NewDefArg = Args.getNumLevels() |
1752 | 2 | ? SemaRef.SubstExpr(OldParam->getDefaultArg(), Args) |
1753 | 3 | : OldParam->getDefaultArg(); |
1754 | 5 | if (NewDefArg.isInvalid()) |
1755 | 0 | return nullptr; |
1756 | 62 | } |
1757 | 62 | |
1758 | 62 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
1759 | 62 | OldParam->getInnerLocStart(), |
1760 | 62 | OldParam->getLocation(), |
1761 | 62 | OldParam->getIdentifier(), |
1762 | 62 | NewDI->getType(), |
1763 | 62 | NewDI, |
1764 | 62 | OldParam->getStorageClass(), |
1765 | 62 | NewDefArg.get()); |
1766 | 62 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
1767 | 62 | OldParam->getFunctionScopeIndex()); |
1768 | 62 | return NewParam; |
1769 | 62 | } |
1770 | | |
1771 | | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
1772 | | bool Explicit, TypeSourceInfo *TInfo, |
1773 | | SourceLocation LocStart, SourceLocation Loc, |
1774 | 129 | SourceLocation LocEnd) { |
1775 | 129 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
1776 | 129 | ArrayRef<ParmVarDecl *> Params = |
1777 | 129 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
1778 | 129 | |
1779 | 129 | // Build the implicit deduction guide template. |
1780 | 129 | auto *Guide = |
1781 | 129 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
1782 | 129 | Name, TInfo->getType(), TInfo, LocEnd); |
1783 | 129 | Guide->setImplicit(); |
1784 | 129 | Guide->setParams(Params); |
1785 | 129 | |
1786 | 129 | for (auto *Param : Params) |
1787 | 123 | Param->setDeclContext(Guide); |
1788 | 129 | |
1789 | 129 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
1790 | 129 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
1791 | 129 | GuideTemplate->setImplicit(); |
1792 | 129 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
1793 | 129 | |
1794 | 129 | if (isa<CXXRecordDecl>(DC)129 ) { |
1795 | 12 | Guide->setAccess(AS_public); |
1796 | 12 | GuideTemplate->setAccess(AS_public); |
1797 | 12 | } |
1798 | 129 | |
1799 | 129 | DC->addDecl(GuideTemplate); |
1800 | 129 | return GuideTemplate; |
1801 | 129 | } |
1802 | | }; |
1803 | | } |
1804 | | |
1805 | | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
1806 | 243 | SourceLocation Loc) { |
1807 | 243 | DeclContext *DC = Template->getDeclContext(); |
1808 | 243 | if (DC->isDependentContext()) |
1809 | 0 | return; |
1810 | 243 | |
1811 | 243 | ConvertConstructorToDeductionGuideTransform Transform( |
1812 | 243 | *this, cast<ClassTemplateDecl>(Template)); |
1813 | 243 | if (!isCompleteType(Loc, Transform.DeducedType)) |
1814 | 0 | return; |
1815 | 243 | |
1816 | 243 | // Check whether we've already declared deduction guides for this template. |
1817 | 243 | // FIXME: Consider storing a flag on the template to indicate this. |
1818 | 243 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
1819 | 243 | for (auto *D : Existing) |
1820 | 182 | if (182 D->isImplicit()182 ) |
1821 | 182 | return; |
1822 | 61 | |
1823 | 61 | // In case we were expanding a pack when we attempted to declare deduction |
1824 | 61 | // guides, turn off pack expansion for everything we're about to do. |
1825 | 61 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
1826 | 61 | // Create a template instantiation record to track the "instantiation" of |
1827 | 61 | // constructors into deduction guides. |
1828 | 61 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
1829 | 61 | // this substitution process actually fail? |
1830 | 61 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
1831 | 61 | |
1832 | 61 | // Convert declared constructors into deduction guide templates. |
1833 | 61 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
1834 | 61 | // for which some class template parameter without a default argument never |
1835 | 61 | // appears in a deduced context). |
1836 | 61 | bool AddedAny = false; |
1837 | 61 | bool AddedCopyOrMove = false; |
1838 | 48 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
1839 | 48 | D = D->getUnderlyingDecl(); |
1840 | 48 | if (D->isInvalidDecl() || 48 D->isImplicit()48 ) |
1841 | 0 | continue; |
1842 | 48 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
1843 | 48 | |
1844 | 48 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
1845 | 48 | auto *CD = |
1846 | 48 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl()18 : D30 ); |
1847 | 48 | // Class-scope explicit specializations (MS extension) do not result in |
1848 | 48 | // deduction guides. |
1849 | 48 | if (!CD || 48 (!FTD && 48 CD->isFunctionTemplateSpecialization()30 )) |
1850 | 0 | continue; |
1851 | 48 | |
1852 | 48 | Transform.transformConstructor(FTD, CD); |
1853 | 48 | AddedAny = true; |
1854 | 48 | |
1855 | 48 | AddedCopyOrMove |= CD->isCopyOrMoveConstructor(); |
1856 | 48 | } |
1857 | 61 | |
1858 | 61 | // Synthesize an X() -> X<...> guide if there were no declared constructors. |
1859 | 61 | // FIXME: The standard doesn't say (how) to do this. |
1860 | 61 | if (!AddedAny) |
1861 | 20 | Transform.buildSimpleDeductionGuide(None); |
1862 | 61 | |
1863 | 61 | // Synthesize an X(X<...>) -> X<...> guide if there was no declared constructor |
1864 | 61 | // resembling a copy or move constructor. |
1865 | 61 | // FIXME: The standard doesn't say (how) to do this. |
1866 | 61 | if (!AddedCopyOrMove) |
1867 | 61 | Transform.buildSimpleDeductionGuide(Transform.DeducedType); |
1868 | 243 | } |
1869 | | |
1870 | | /// \brief Diagnose the presence of a default template argument on a |
1871 | | /// template parameter, which is ill-formed in certain contexts. |
1872 | | /// |
1873 | | /// \returns true if the default template argument should be dropped. |
1874 | | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
1875 | | Sema::TemplateParamListContext TPC, |
1876 | | SourceLocation ParamLoc, |
1877 | 2.48k | SourceRange DefArgRange) { |
1878 | 2.48k | switch (TPC) { |
1879 | 1.81k | case Sema::TPC_ClassTemplate: |
1880 | 1.81k | case Sema::TPC_VarTemplate: |
1881 | 1.81k | case Sema::TPC_TypeAliasTemplate: |
1882 | 1.81k | return false; |
1883 | 1.81k | |
1884 | 640 | case Sema::TPC_FunctionTemplate: |
1885 | 640 | case Sema::TPC_FriendFunctionTemplateDefinition: |
1886 | 640 | // C++ [temp.param]p9: |
1887 | 640 | // A default template-argument shall not be specified in a |
1888 | 640 | // function template declaration or a function template |
1889 | 640 | // definition [...] |
1890 | 640 | // If a friend function template declaration specifies a default |
1891 | 640 | // template-argument, that declaration shall be a definition and shall be |
1892 | 640 | // the only declaration of the function template in the translation unit. |
1893 | 640 | // (C++98/03 doesn't have this wording; see DR226). |
1894 | 640 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
1895 | 239 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
1896 | 401 | : diag::ext_template_parameter_default_in_function_template) |
1897 | 640 | << DefArgRange; |
1898 | 640 | return false; |
1899 | 640 | |
1900 | 12 | case Sema::TPC_ClassTemplateMember: |
1901 | 12 | // C++0x [temp.param]p9: |
1902 | 12 | // A default template-argument shall not be specified in the |
1903 | 12 | // template-parameter-lists of the definition of a member of a |
1904 | 12 | // class template that appears outside of the member's class. |
1905 | 12 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
1906 | 12 | << DefArgRange; |
1907 | 12 | return true; |
1908 | 640 | |
1909 | 10 | case Sema::TPC_FriendClassTemplate: |
1910 | 10 | case Sema::TPC_FriendFunctionTemplate: |
1911 | 10 | // C++ [temp.param]p9: |
1912 | 10 | // A default template-argument shall not be specified in a |
1913 | 10 | // friend template declaration. |
1914 | 10 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
1915 | 10 | << DefArgRange; |
1916 | 10 | return true; |
1917 | 0 |
|
1918 | 0 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
1919 | 0 | // for friend function templates if there is only a single |
1920 | 0 | // declaration (and it is a definition). Strange! |
1921 | 0 | } |
1922 | 0 |
|
1923 | 0 | llvm_unreachable0 ("Invalid TemplateParamListContext!"); |
1924 | 0 | } |
1925 | | |
1926 | | /// \brief Check for unexpanded parameter packs within the template parameters |
1927 | | /// of a template template parameter, recursively. |
1928 | | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
1929 | 740 | TemplateTemplateParmDecl *TTP) { |
1930 | 740 | // A template template parameter which is a parameter pack is also a pack |
1931 | 740 | // expansion. |
1932 | 740 | if (TTP->isParameterPack()) |
1933 | 58 | return false; |
1934 | 682 | |
1935 | 682 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
1936 | 1.47k | for (unsigned I = 0, N = Params->size(); I != N1.47k ; ++I792 ) { |
1937 | 792 | NamedDecl *P = Params->getParam(I); |
1938 | 792 | if (NonTypeTemplateParmDecl *NTTP792 = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
1939 | 156 | if (!NTTP->isParameterPack() && |
1940 | 144 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
1941 | 144 | NTTP->getTypeSourceInfo(), |
1942 | 144 | Sema::UPPC_NonTypeTemplateParameterType)) |
1943 | 0 | return true; |
1944 | 156 | |
1945 | 156 | continue; |
1946 | 156 | } |
1947 | 636 | |
1948 | 636 | if (TemplateTemplateParmDecl *636 InnerTTP636 |
1949 | 636 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
1950 | 26 | if (26 DiagnoseUnexpandedParameterPacks(S, InnerTTP)26 ) |
1951 | 0 | return true; |
1952 | 792 | } |
1953 | 682 | |
1954 | 682 | return false; |
1955 | 740 | } |
1956 | | |
1957 | | /// \brief Checks the validity of a template parameter list, possibly |
1958 | | /// considering the template parameter list from a previous |
1959 | | /// declaration. |
1960 | | /// |
1961 | | /// If an "old" template parameter list is provided, it must be |
1962 | | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
1963 | | /// template parameter list. |
1964 | | /// |
1965 | | /// \param NewParams Template parameter list for a new template |
1966 | | /// declaration. This template parameter list will be updated with any |
1967 | | /// default arguments that are carried through from the previous |
1968 | | /// template parameter list. |
1969 | | /// |
1970 | | /// \param OldParams If provided, template parameter list from a |
1971 | | /// previous declaration of the same template. Default template |
1972 | | /// arguments will be merged from the old template parameter list to |
1973 | | /// the new template parameter list. |
1974 | | /// |
1975 | | /// \param TPC Describes the context in which we are checking the given |
1976 | | /// template parameter list. |
1977 | | /// |
1978 | | /// \returns true if an error occurred, false otherwise. |
1979 | | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
1980 | | TemplateParameterList *OldParams, |
1981 | 63.5k | TemplateParamListContext TPC) { |
1982 | 63.5k | bool Invalid = false; |
1983 | 63.5k | |
1984 | 63.5k | // C++ [temp.param]p10: |
1985 | 63.5k | // The set of default template-arguments available for use with a |
1986 | 63.5k | // template declaration or definition is obtained by merging the |
1987 | 63.5k | // default arguments from the definition (if in scope) and all |
1988 | 63.5k | // declarations in scope in the same way default function |
1989 | 63.5k | // arguments are (8.3.6). |
1990 | 63.5k | bool SawDefaultArgument = false; |
1991 | 63.5k | SourceLocation PreviousDefaultArgLoc; |
1992 | 63.5k | |
1993 | 63.5k | // Dummy initialization to avoid warnings. |
1994 | 63.5k | TemplateParameterList::iterator OldParam = NewParams->end(); |
1995 | 63.5k | if (OldParams) |
1996 | 3.00k | OldParam = OldParams->begin(); |
1997 | 63.5k | |
1998 | 63.5k | bool RemoveDefaultArguments = false; |
1999 | 63.5k | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2000 | 63.5k | NewParamEnd = NewParams->end(); |
2001 | 151k | NewParam != NewParamEnd151k ; ++NewParam87.8k ) { |
2002 | 87.8k | // Variables used to diagnose redundant default arguments |
2003 | 87.8k | bool RedundantDefaultArg = false; |
2004 | 87.8k | SourceLocation OldDefaultLoc; |
2005 | 87.8k | SourceLocation NewDefaultLoc; |
2006 | 87.8k | |
2007 | 87.8k | // Variable used to diagnose missing default arguments |
2008 | 87.8k | bool MissingDefaultArg = false; |
2009 | 87.8k | |
2010 | 87.8k | // Variable used to diagnose non-final parameter packs |
2011 | 87.8k | bool SawParameterPack = false; |
2012 | 87.8k | |
2013 | 87.8k | if (TemplateTypeParmDecl *NewTypeParm |
2014 | 76.0k | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
2015 | 76.0k | // Check the presence of a default argument here. |
2016 | 76.0k | if (NewTypeParm->hasDefaultArgument() && |
2017 | 1.91k | DiagnoseDefaultTemplateArgument(*this, TPC, |
2018 | 1.91k | NewTypeParm->getLocation(), |
2019 | 1.91k | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
2020 | 1.91k | .getSourceRange())) |
2021 | 20 | NewTypeParm->removeDefaultArgument(); |
2022 | 76.0k | |
2023 | 76.0k | // Merge default arguments for template type parameters. |
2024 | 76.0k | TemplateTypeParmDecl *OldTypeParm |
2025 | 76.0k | = OldParams? cast<TemplateTypeParmDecl>(*OldParam)3.58k : nullptr72.5k ; |
2026 | 76.0k | if (NewTypeParm->isParameterPack()76.0k ) { |
2027 | 964 | assert(!NewTypeParm->hasDefaultArgument() && |
2028 | 964 | "Parameter packs can't have a default argument!"); |
2029 | 964 | SawParameterPack = true; |
2030 | 76.0k | } else if (75.1k OldTypeParm && 75.1k hasVisibleDefaultArgument(OldTypeParm)3.57k && |
2031 | 75.1k | NewTypeParm->hasDefaultArgument()166 ) { |
2032 | 1 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
2033 | 1 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
2034 | 1 | SawDefaultArgument = true; |
2035 | 1 | RedundantDefaultArg = true; |
2036 | 1 | PreviousDefaultArgLoc = NewDefaultLoc; |
2037 | 75.1k | } else if (75.1k OldTypeParm && 75.1k OldTypeParm->hasDefaultArgument()3.57k ) { |
2038 | 189 | // Merge the default argument from the old declaration to the |
2039 | 189 | // new declaration. |
2040 | 189 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
2041 | 189 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
2042 | 75.1k | } else if (74.9k NewTypeParm->hasDefaultArgument()74.9k ) { |
2043 | 1.87k | SawDefaultArgument = true; |
2044 | 1.87k | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
2045 | 74.9k | } else if (73.0k SawDefaultArgument73.0k ) |
2046 | 37 | MissingDefaultArg = true; |
2047 | 87.8k | } else if (NonTypeTemplateParmDecl *11.7k NewNonTypeParm11.7k |
2048 | 11.0k | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
2049 | 11.0k | // Check for unexpanded parameter packs. |
2050 | 11.0k | if (!NewNonTypeParm->isParameterPack() && |
2051 | 10.8k | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
2052 | 10.8k | NewNonTypeParm->getTypeSourceInfo(), |
2053 | 11.0k | UPPC_NonTypeTemplateParameterType)) { |
2054 | 1 | Invalid = true; |
2055 | 1 | continue; |
2056 | 1 | } |
2057 | 11.0k | |
2058 | 11.0k | // Check the presence of a default argument here. |
2059 | 11.0k | if (11.0k NewNonTypeParm->hasDefaultArgument() && |
2060 | 453 | DiagnoseDefaultTemplateArgument(*this, TPC, |
2061 | 453 | NewNonTypeParm->getLocation(), |
2062 | 11.0k | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
2063 | 1 | NewNonTypeParm->removeDefaultArgument(); |
2064 | 1 | } |
2065 | 11.0k | |
2066 | 11.0k | // Merge default arguments for non-type template parameters |
2067 | 11.0k | NonTypeTemplateParmDecl *OldNonTypeParm |
2068 | 11.0k | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam)466 : nullptr10.5k ; |
2069 | 11.0k | if (NewNonTypeParm->isParameterPack()11.0k ) { |
2070 | 236 | assert(!NewNonTypeParm->hasDefaultArgument() && |
2071 | 236 | "Parameter packs can't have a default argument!"); |
2072 | 236 | if (!NewNonTypeParm->isPackExpansion()) |
2073 | 217 | SawParameterPack = true; |
2074 | 11.0k | } else if (10.8k OldNonTypeParm && 10.8k hasVisibleDefaultArgument(OldNonTypeParm)463 && |
2075 | 10.8k | NewNonTypeParm->hasDefaultArgument()50 ) { |
2076 | 1 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2077 | 1 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2078 | 1 | SawDefaultArgument = true; |
2079 | 1 | RedundantDefaultArg = true; |
2080 | 1 | PreviousDefaultArgLoc = NewDefaultLoc; |
2081 | 10.8k | } else if (10.8k OldNonTypeParm && 10.8k OldNonTypeParm->hasDefaultArgument()462 ) { |
2082 | 64 | // Merge the default argument from the old declaration to the |
2083 | 64 | // new declaration. |
2084 | 64 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
2085 | 64 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2086 | 10.8k | } else if (10.7k NewNonTypeParm->hasDefaultArgument()10.7k ) { |
2087 | 436 | SawDefaultArgument = true; |
2088 | 436 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2089 | 10.7k | } else if (10.3k SawDefaultArgument10.3k ) |
2090 | 5 | MissingDefaultArg = true; |
2091 | 11.7k | } else { |
2092 | 714 | TemplateTemplateParmDecl *NewTemplateParm |
2093 | 714 | = cast<TemplateTemplateParmDecl>(*NewParam); |
2094 | 714 | |
2095 | 714 | // Check for unexpanded parameter packs, recursively. |
2096 | 714 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)714 ) { |
2097 | 0 | Invalid = true; |
2098 | 0 | continue; |
2099 | 0 | } |
2100 | 714 | |
2101 | 714 | // Check the presence of a default argument here. |
2102 | 714 | if (714 NewTemplateParm->hasDefaultArgument() && |
2103 | 111 | DiagnoseDefaultTemplateArgument(*this, TPC, |
2104 | 111 | NewTemplateParm->getLocation(), |
2105 | 111 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
2106 | 1 | NewTemplateParm->removeDefaultArgument(); |
2107 | 714 | |
2108 | 714 | // Merge default arguments for template template parameters |
2109 | 714 | TemplateTemplateParmDecl *OldTemplateParm |
2110 | 714 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam)55 : nullptr659 ; |
2111 | 714 | if (NewTemplateParm->isParameterPack()714 ) { |
2112 | 58 | assert(!NewTemplateParm->hasDefaultArgument() && |
2113 | 58 | "Parameter packs can't have a default argument!"); |
2114 | 58 | if (!NewTemplateParm->isPackExpansion()) |
2115 | 51 | SawParameterPack = true; |
2116 | 714 | } else if (656 OldTemplateParm && |
2117 | 55 | hasVisibleDefaultArgument(OldTemplateParm) && |
2118 | 656 | NewTemplateParm->hasDefaultArgument()23 ) { |
2119 | 1 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
2120 | 1 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
2121 | 1 | SawDefaultArgument = true; |
2122 | 1 | RedundantDefaultArg = true; |
2123 | 1 | PreviousDefaultArgLoc = NewDefaultLoc; |
2124 | 656 | } else if (655 OldTemplateParm && 655 OldTemplateParm->hasDefaultArgument()54 ) { |
2125 | 37 | // Merge the default argument from the old declaration to the |
2126 | 37 | // new declaration. |
2127 | 37 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
2128 | 37 | PreviousDefaultArgLoc |
2129 | 37 | = OldTemplateParm->getDefaultArgument().getLocation(); |
2130 | 655 | } else if (618 NewTemplateParm->hasDefaultArgument()618 ) { |
2131 | 94 | SawDefaultArgument = true; |
2132 | 94 | PreviousDefaultArgLoc |
2133 | 94 | = NewTemplateParm->getDefaultArgument().getLocation(); |
2134 | 618 | } else if (524 SawDefaultArgument524 ) |
2135 | 5 | MissingDefaultArg = true; |
2136 | 11.7k | } |
2137 | 87.8k | |
2138 | 87.8k | // C++11 [temp.param]p11: |
2139 | 87.8k | // If a template parameter of a primary class template or alias template |
2140 | 87.8k | // is a template parameter pack, it shall be the last template parameter. |
2141 | 87.8k | if (87.8k SawParameterPack && 87.8k (NewParam + 1) != NewParamEnd1.23k && |
2142 | 81 | (TPC == TPC_ClassTemplate || 81 TPC == TPC_VarTemplate77 || |
2143 | 87.8k | TPC == TPC_TypeAliasTemplate77 )) { |
2144 | 7 | Diag((*NewParam)->getLocation(), |
2145 | 7 | diag::err_template_param_pack_must_be_last_template_parameter); |
2146 | 7 | Invalid = true; |
2147 | 7 | } |
2148 | 87.8k | |
2149 | 87.8k | if (RedundantDefaultArg87.8k ) { |
2150 | 3 | // C++ [temp.param]p12: |
2151 | 3 | // A template-parameter shall not be given default arguments |
2152 | 3 | // by two different declarations in the same scope. |
2153 | 3 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
2154 | 3 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
2155 | 3 | Invalid = true; |
2156 | 87.8k | } else if (87.8k MissingDefaultArg && 87.8k TPC != TPC_FunctionTemplate47 ) { |
2157 | 18 | // C++ [temp.param]p11: |
2158 | 18 | // If a template-parameter of a class template has a default |
2159 | 18 | // template-argument, each subsequent template-parameter shall either |
2160 | 18 | // have a default template-argument supplied or be a template parameter |
2161 | 18 | // pack. |
2162 | 18 | Diag((*NewParam)->getLocation(), |
2163 | 18 | diag::err_template_param_default_arg_missing); |
2164 | 18 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
2165 | 18 | Invalid = true; |
2166 | 18 | RemoveDefaultArguments = true; |
2167 | 18 | } |
2168 | 87.8k | |
2169 | 87.8k | // If we have an old template parameter list that we're merging |
2170 | 87.8k | // in, move on to the next parameter. |
2171 | 87.8k | if (OldParams) |
2172 | 4.10k | ++OldParam; |
2173 | 87.8k | } |
2174 | 63.5k | |
2175 | 63.5k | // We were missing some default arguments at the end of the list, so remove |
2176 | 63.5k | // all of the default arguments. |
2177 | 63.5k | if (RemoveDefaultArguments63.5k ) { |
2178 | 18 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2179 | 18 | NewParamEnd = NewParams->end(); |
2180 | 61 | NewParam != NewParamEnd61 ; ++NewParam43 ) { |
2181 | 43 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
2182 | 31 | TTP->removeDefaultArgument(); |
2183 | 12 | else if (NonTypeTemplateParmDecl *12 NTTP12 |
2184 | 12 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
2185 | 6 | NTTP->removeDefaultArgument(); |
2186 | 12 | else |
2187 | 6 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
2188 | 43 | } |
2189 | 18 | } |
2190 | 63.5k | |
2191 | 63.5k | return Invalid; |
2192 | 63.5k | } |
2193 | | |
2194 | | namespace { |
2195 | | |
2196 | | /// A class which looks for a use of a certain level of template |
2197 | | /// parameter. |
2198 | | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
2199 | | typedef RecursiveASTVisitor<DependencyChecker> super; |
2200 | | |
2201 | | unsigned Depth; |
2202 | | |
2203 | | // Whether we're looking for a use of a template parameter that makes the |
2204 | | // overall construct type-dependent / a dependent type. This is strictly |
2205 | | // best-effort for now; we may fail to match at all for a dependent type |
2206 | | // in some cases if this is set. |
2207 | | bool IgnoreNonTypeDependent; |
2208 | | |
2209 | | bool Match; |
2210 | | SourceLocation MatchLoc; |
2211 | | |
2212 | | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
2213 | | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
2214 | 18 | Match(false) {} |
2215 | | |
2216 | | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
2217 | 38 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
2218 | 38 | NamedDecl *ND = Params->getParam(0); |
2219 | 38 | if (TemplateTypeParmDecl *PD38 = dyn_cast<TemplateTypeParmDecl>(ND)) { |
2220 | 36 | Depth = PD->getDepth(); |
2221 | 38 | } else if (NonTypeTemplateParmDecl *2 PD2 = |
2222 | 0 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
2223 | 0 | Depth = PD->getDepth(); |
2224 | 2 | } else { |
2225 | 2 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
2226 | 2 | } |
2227 | 38 | } |
2228 | | |
2229 | 50 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
2230 | 50 | if (ParmDepth >= Depth50 ) { |
2231 | 47 | Match = true; |
2232 | 47 | MatchLoc = Loc; |
2233 | 47 | return true; |
2234 | 47 | } |
2235 | 3 | return false; |
2236 | 3 | } |
2237 | | |
2238 | 35 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
2239 | 35 | // Prune out non-type-dependent expressions if requested. This can |
2240 | 35 | // sometimes result in us failing to find a template parameter reference |
2241 | 35 | // (if a value-dependent expression creates a dependent type), but this |
2242 | 35 | // mode is best-effort only. |
2243 | 35 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
2244 | 35 | if (35 IgnoreNonTypeDependent && 35 !E->isTypeDependent()34 ) |
2245 | 16 | return true; |
2246 | 19 | return super::TraverseStmt(S, Q); |
2247 | 19 | } |
2248 | | |
2249 | 36 | bool TraverseTypeLoc(TypeLoc TL) { |
2250 | 36 | if (IgnoreNonTypeDependent && 36 !TL.isNull()36 && |
2251 | 36 | !TL.getType()->isDependentType()) |
2252 | 6 | return true; |
2253 | 30 | return super::TraverseTypeLoc(TL); |
2254 | 30 | } |
2255 | | |
2256 | 11 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
2257 | 11 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
2258 | 11 | } |
2259 | | |
2260 | 47 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
2261 | 47 | // For a best-effort search, keep looking until we find a location. |
2262 | 36 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
2263 | 47 | } |
2264 | | |
2265 | 40 | bool TraverseTemplateName(TemplateName N) { |
2266 | 40 | if (TemplateTemplateParmDecl *PD = |
2267 | 40 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
2268 | 1 | if (1 Matches(PD->getDepth())1 ) |
2269 | 1 | return false; |
2270 | 39 | return super::TraverseTemplateName(N); |
2271 | 39 | } |
2272 | | |
2273 | 2 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
2274 | 2 | if (NonTypeTemplateParmDecl *PD = |
2275 | 2 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
2276 | 2 | if (2 Matches(PD->getDepth(), E->getExprLoc())2 ) |
2277 | 1 | return false; |
2278 | 1 | return super::VisitDeclRefExpr(E); |
2279 | 1 | } |
2280 | | |
2281 | 0 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
2282 | 0 | return TraverseType(T->getReplacementType()); |
2283 | 0 | } |
2284 | | |
2285 | | bool |
2286 | 0 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
2287 | 0 | return TraverseTemplateArgument(T->getArgumentPack()); |
2288 | 0 | } |
2289 | | |
2290 | 33 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
2291 | 33 | return TraverseType(T->getInjectedSpecializationType()); |
2292 | 33 | } |
2293 | | }; |
2294 | | } // end anonymous namespace |
2295 | | |
2296 | | /// Determines whether a given type depends on the given parameter |
2297 | | /// list. |
2298 | | static bool |
2299 | 38 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
2300 | 38 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
2301 | 38 | Checker.TraverseType(T); |
2302 | 38 | return Checker.Match; |
2303 | 38 | } |
2304 | | |
2305 | | // Find the source range corresponding to the named type in the given |
2306 | | // nested-name-specifier, if any. |
2307 | | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
2308 | | QualType T, |
2309 | 50 | const CXXScopeSpec &SS) { |
2310 | 50 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
2311 | 60 | while (NestedNameSpecifier *NNS60 = NNSLoc.getNestedNameSpecifier()) { |
2312 | 59 | if (const Type *CurType59 = NNS->getAsType()) { |
2313 | 59 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
2314 | 49 | return NNSLoc.getTypeLoc().getSourceRange(); |
2315 | 59 | } else |
2316 | 0 | break; |
2317 | 10 | |
2318 | 10 | NNSLoc = NNSLoc.getPrefix(); |
2319 | 10 | } |
2320 | 50 | |
2321 | 1 | return SourceRange(); |
2322 | 50 | } |
2323 | | |
2324 | | /// \brief Match the given template parameter lists to the given scope |
2325 | | /// specifier, returning the template parameter list that applies to the |
2326 | | /// name. |
2327 | | /// |
2328 | | /// \param DeclStartLoc the start of the declaration that has a scope |
2329 | | /// specifier or a template parameter list. |
2330 | | /// |
2331 | | /// \param DeclLoc The location of the declaration itself. |
2332 | | /// |
2333 | | /// \param SS the scope specifier that will be matched to the given template |
2334 | | /// parameter lists. This scope specifier precedes a qualified name that is |
2335 | | /// being declared. |
2336 | | /// |
2337 | | /// \param TemplateId The template-id following the scope specifier, if there |
2338 | | /// is one. Used to check for a missing 'template<>'. |
2339 | | /// |
2340 | | /// \param ParamLists the template parameter lists, from the outermost to the |
2341 | | /// innermost template parameter lists. |
2342 | | /// |
2343 | | /// \param IsFriend Whether to apply the slightly different rules for |
2344 | | /// matching template parameters to scope specifiers in friend |
2345 | | /// declarations. |
2346 | | /// |
2347 | | /// \param IsMemberSpecialization will be set true if the scope specifier |
2348 | | /// denotes a fully-specialized type, and therefore this is a declaration of |
2349 | | /// a member specialization. |
2350 | | /// |
2351 | | /// \returns the template parameter list, if any, that corresponds to the |
2352 | | /// name that is preceded by the scope specifier @p SS. This template |
2353 | | /// parameter list may have template parameters (if we're declaring a |
2354 | | /// template) or may have no template parameters (if we're declaring a |
2355 | | /// template specialization), or may be NULL (if what we're declaring isn't |
2356 | | /// itself a template). |
2357 | | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
2358 | | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
2359 | | TemplateIdAnnotation *TemplateId, |
2360 | | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
2361 | 1.88M | bool &IsMemberSpecialization, bool &Invalid) { |
2362 | 1.88M | IsMemberSpecialization = false; |
2363 | 1.88M | Invalid = false; |
2364 | 1.88M | |
2365 | 1.88M | // The sequence of nested types to which we will match up the template |
2366 | 1.88M | // parameter lists. We first build this list by starting with the type named |
2367 | 1.88M | // by the nested-name-specifier and walking out until we run out of types. |
2368 | 1.88M | SmallVector<QualType, 4> NestedTypes; |
2369 | 1.88M | QualType T; |
2370 | 1.88M | if (SS.getScopeRep()1.88M ) { |
2371 | 26.6k | if (CXXRecordDecl *Record |
2372 | 26.6k | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
2373 | 26.1k | T = Context.getTypeDeclType(Record); |
2374 | 26.6k | else |
2375 | 514 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
2376 | 26.6k | } |
2377 | 1.88M | |
2378 | 1.88M | // If we found an explicit specialization that prevents us from needing |
2379 | 1.88M | // 'template<>' headers, this will be set to the location of that |
2380 | 1.88M | // explicit specialization. |
2381 | 1.88M | SourceLocation ExplicitSpecLoc; |
2382 | 1.88M | |
2383 | 1.90M | while (!T.isNull()1.90M ) { |
2384 | 26.6k | NestedTypes.push_back(T); |
2385 | 26.6k | |
2386 | 26.6k | // Retrieve the parent of a record type. |
2387 | 26.6k | if (CXXRecordDecl *Record26.6k = T->getAsCXXRecordDecl()) { |
2388 | 26.6k | // If this type is an explicit specialization, we're done. |
2389 | 26.6k | if (ClassTemplateSpecializationDecl *Spec |
2390 | 1.69k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
2391 | 1.69k | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
2392 | 1.69k | Spec->getSpecializationKind() == TSK_ExplicitSpecialization1.26k ) { |
2393 | 198 | ExplicitSpecLoc = Spec->getLocation(); |
2394 | 198 | break; |
2395 | 198 | } |
2396 | 24.9k | } else if (24.9k Record->getTemplateSpecializationKind() |
2397 | 24.9k | == TSK_ExplicitSpecialization) { |
2398 | 7 | ExplicitSpecLoc = Record->getLocation(); |
2399 | 7 | break; |
2400 | 7 | } |
2401 | 26.4k | |
2402 | 26.4k | if (TypeDecl *26.4k Parent26.4k = dyn_cast<TypeDecl>(Record->getParent())) |
2403 | 460 | T = Context.getTypeDeclType(Parent); |
2404 | 26.4k | else |
2405 | 25.9k | T = QualType(); |
2406 | 26.6k | continue; |
2407 | 26.6k | } |
2408 | 27 | |
2409 | 27 | if (const TemplateSpecializationType *27 TST27 |
2410 | 7 | = T->getAs<TemplateSpecializationType>()) { |
2411 | 7 | if (TemplateDecl *Template7 = TST->getTemplateName().getAsTemplateDecl()) { |
2412 | 7 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
2413 | 0 | T = Context.getTypeDeclType(Parent); |
2414 | 7 | else |
2415 | 7 | T = QualType(); |
2416 | 7 | continue; |
2417 | 7 | } |
2418 | 20 | } |
2419 | 20 | |
2420 | 20 | // Look one step prior in a dependent template specialization type. |
2421 | 20 | if (const DependentTemplateSpecializationType *20 DependentTST20 |
2422 | 0 | = T->getAs<DependentTemplateSpecializationType>()) { |
2423 | 0 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
2424 | 0 | T = QualType(NNS->getAsType(), 0); |
2425 | 0 | else |
2426 | 0 | T = QualType(); |
2427 | 0 | continue; |
2428 | 0 | } |
2429 | 20 | |
2430 | 20 | // Look one step prior in a dependent name type. |
2431 | 20 | if (const DependentNameType *20 DependentName20 = T->getAs<DependentNameType>()){ |
2432 | 0 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
2433 | 0 | T = QualType(NNS->getAsType(), 0); |
2434 | 0 | else |
2435 | 0 | T = QualType(); |
2436 | 0 | continue; |
2437 | 0 | } |
2438 | 20 | |
2439 | 20 | // Retrieve the parent of an enumeration type. |
2440 | 20 | if (const EnumType *20 EnumT20 = T->getAs<EnumType>()) { |
2441 | 4 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
2442 | 4 | // check here. |
2443 | 4 | EnumDecl *Enum = EnumT->getDecl(); |
2444 | 4 | |
2445 | 4 | // Get to the parent type. |
2446 | 4 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
2447 | 0 | T = Context.getTypeDeclType(Parent); |
2448 | 4 | else |
2449 | 4 | T = QualType(); |
2450 | 4 | continue; |
2451 | 4 | } |
2452 | 16 | |
2453 | 16 | T = QualType(); |
2454 | 16 | } |
2455 | 1.88M | // Reverse the nested types list, since we want to traverse from the outermost |
2456 | 1.88M | // to the innermost while checking template-parameter-lists. |
2457 | 1.88M | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
2458 | 1.88M | |
2459 | 1.88M | // C++0x [temp.expl.spec]p17: |
2460 | 1.88M | // A member or a member template may be nested within many |
2461 | 1.88M | // enclosing class templates. In an explicit specialization for |
2462 | 1.88M | // such a member, the member declaration shall be preceded by a |
2463 | 1.88M | // template<> for each enclosing class template that is |
2464 | 1.88M | // explicitly specialized. |
2465 | 1.88M | bool SawNonEmptyTemplateParameterList = false; |
2466 | 1.88M | |
2467 | 5.03k | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
2468 | 5.03k | if (SawNonEmptyTemplateParameterList5.03k ) { |
2469 | 3 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
2470 | 3 | << !Recovery << Range; |
2471 | 3 | Invalid = true; |
2472 | 3 | IsMemberSpecialization = false; |
2473 | 3 | return true; |
2474 | 3 | } |
2475 | 5.03k | |
2476 | 5.03k | return false; |
2477 | 5.03k | }; |
2478 | 1.88M | |
2479 | 48 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
2480 | 48 | // Check that we can have an explicit specialization here. |
2481 | 48 | if (CheckExplicitSpecialization(Range, true)) |
2482 | 2 | return true; |
2483 | 46 | |
2484 | 46 | // We don't have a template header, but we should. |
2485 | 46 | SourceLocation ExpectedTemplateLoc; |
2486 | 46 | if (!ParamLists.empty()) |
2487 | 5 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
2488 | 46 | else |
2489 | 41 | ExpectedTemplateLoc = DeclStartLoc; |
2490 | 48 | |
2491 | 48 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
2492 | 48 | << Range |
2493 | 48 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
2494 | 48 | return false; |
2495 | 48 | }; |
2496 | 1.88M | |
2497 | 1.88M | unsigned ParamIdx = 0; |
2498 | 1.90M | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
2499 | 1.88M | ++TypeIdx26.6k ) { |
2500 | 26.6k | T = NestedTypes[TypeIdx]; |
2501 | 26.6k | |
2502 | 26.6k | // Whether we expect a 'template<>' header. |
2503 | 26.6k | bool NeedEmptyTemplateHeader = false; |
2504 | 26.6k | |
2505 | 26.6k | // Whether we expect a template header with parameters. |
2506 | 26.6k | bool NeedNonemptyTemplateHeader = false; |
2507 | 26.6k | |
2508 | 26.6k | // For a dependent type, the set of template parameters that we |
2509 | 26.6k | // expect to see. |
2510 | 26.6k | TemplateParameterList *ExpectedTemplateParams = nullptr; |
2511 | 26.6k | |
2512 | 26.6k | // C++0x [temp.expl.spec]p15: |
2513 | 26.6k | // A member or a member template may be nested within many enclosing |
2514 | 26.6k | // class templates. In an explicit specialization for such a member, the |
2515 | 26.6k | // member declaration shall be preceded by a template<> for each |
2516 | 26.6k | // enclosing class template that is explicitly specialized. |
2517 | 26.6k | if (CXXRecordDecl *Record26.6k = T->getAsCXXRecordDecl()) { |
2518 | 26.6k | if (ClassTemplatePartialSpecializationDecl *Partial |
2519 | 431 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
2520 | 431 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
2521 | 431 | NeedNonemptyTemplateHeader = true; |
2522 | 26.6k | } else if (26.1k Record->isDependentType()26.1k ) { |
2523 | 2.55k | if (Record->getDescribedClassTemplate()2.55k ) { |
2524 | 2.51k | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
2525 | 2.51k | ->getTemplateParameters(); |
2526 | 2.51k | NeedNonemptyTemplateHeader = true; |
2527 | 2.51k | } |
2528 | 26.1k | } else if (ClassTemplateSpecializationDecl *23.6k Spec23.6k |
2529 | 1.26k | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
2530 | 1.26k | // C++0x [temp.expl.spec]p4: |
2531 | 1.26k | // Members of an explicitly specialized class template are defined |
2532 | 1.26k | // in the same manner as members of normal classes, and not using |
2533 | 1.26k | // the template<> syntax. |
2534 | 1.26k | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
2535 | 1.06k | NeedEmptyTemplateHeader = true; |
2536 | 1.26k | else |
2537 | 198 | continue; |
2538 | 22.3k | } else if (22.3k Record->getTemplateSpecializationKind()22.3k ) { |
2539 | 8 | if (Record->getTemplateSpecializationKind() |
2540 | 8 | != TSK_ExplicitSpecialization && |
2541 | 1 | TypeIdx == NumTypes - 1) |
2542 | 1 | IsMemberSpecialization = true; |
2543 | 26.1k | |
2544 | 26.1k | continue; |
2545 | 26.1k | } |
2546 | 27 | } else if (const TemplateSpecializationType *27 TST27 |
2547 | 7 | = T->getAs<TemplateSpecializationType>()) { |
2548 | 7 | if (TemplateDecl *Template7 = TST->getTemplateName().getAsTemplateDecl()) { |
2549 | 7 | ExpectedTemplateParams = Template->getTemplateParameters(); |
2550 | 7 | NeedNonemptyTemplateHeader = true; |
2551 | 7 | } |
2552 | 27 | } else if (20 T->getAs<DependentTemplateSpecializationType>()20 ) { |
2553 | 0 | // FIXME: We actually could/should check the template arguments here |
2554 | 0 | // against the corresponding template parameter list. |
2555 | 0 | NeedNonemptyTemplateHeader = false; |
2556 | 0 | } |
2557 | 26.6k | |
2558 | 26.6k | // C++ [temp.expl.spec]p16: |
2559 | 26.6k | // In an explicit specialization declaration for a member of a class |
2560 | 26.6k | // template or a member template that ap- pears in namespace scope, the |
2561 | 26.6k | // member template and some of its enclosing class templates may remain |
2562 | 26.6k | // unspecialized, except that the declaration shall not explicitly |
2563 | 26.6k | // specialize a class member template if its en- closing class templates |
2564 | 26.6k | // are not explicitly specialized as well. |
2565 | 26.4k | if (26.4k ParamIdx < ParamLists.size()26.4k ) { |
2566 | 4.67k | if (ParamLists[ParamIdx]->size() == 04.67k ) { |
2567 | 1.35k | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
2568 | 1.35k | false)) |
2569 | 0 | return nullptr; |
2570 | 4.67k | } else |
2571 | 3.31k | SawNonEmptyTemplateParameterList = true; |
2572 | 4.67k | } |
2573 | 26.4k | |
2574 | 26.4k | if (26.4k NeedEmptyTemplateHeader26.4k ) { |
2575 | 1.06k | // If we're on the last of the types, and we need a 'template<>' header |
2576 | 1.06k | // here, then it's a member specialization. |
2577 | 1.06k | if (TypeIdx == NumTypes - 1) |
2578 | 1.01k | IsMemberSpecialization = true; |
2579 | 1.06k | |
2580 | 1.06k | if (ParamIdx < ParamLists.size()1.06k ) { |
2581 | 999 | if (ParamLists[ParamIdx]->size() > 0999 ) { |
2582 | 22 | // The header has template parameters when it shouldn't. Complain. |
2583 | 22 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
2584 | 22 | diag::err_template_param_list_matches_nontemplate) |
2585 | 22 | << T |
2586 | 22 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
2587 | 22 | ParamLists[ParamIdx]->getRAngleLoc()) |
2588 | 22 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
2589 | 22 | Invalid = true; |
2590 | 22 | return nullptr; |
2591 | 22 | } |
2592 | 977 | |
2593 | 977 | // Consume this template header. |
2594 | 977 | ++ParamIdx; |
2595 | 977 | continue; |
2596 | 977 | } |
2597 | 67 | |
2598 | 67 | if (67 !IsFriend67 ) |
2599 | 27 | if (27 DiagnoseMissingExplicitSpecialization( |
2600 | 27 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
2601 | 0 | return nullptr; |
2602 | 67 | |
2603 | 67 | continue; |
2604 | 67 | } |
2605 | 25.3k | |
2606 | 25.3k | if (25.3k NeedNonemptyTemplateHeader25.3k ) { |
2607 | 2.95k | // In friend declarations we can have template-ids which don't |
2608 | 2.95k | // depend on the corresponding template parameter lists. But |
2609 | 2.95k | // assume that empty parameter lists are supposed to match this |
2610 | 2.95k | // template-id. |
2611 | 2.95k | if (IsFriend && 2.95k T->isDependentType()50 ) { |
2612 | 50 | if (ParamIdx < ParamLists.size() && |
2613 | 38 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
2614 | 35 | ExpectedTemplateParams = nullptr; |
2615 | 50 | else |
2616 | 15 | continue; |
2617 | 2.94k | } |
2618 | 2.94k | |
2619 | 2.94k | if (2.94k ParamIdx < ParamLists.size()2.94k ) { |
2620 | 2.93k | // Check the template parameter list, if we can. |
2621 | 2.93k | if (ExpectedTemplateParams && |
2622 | 2.90k | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
2623 | 2.90k | ExpectedTemplateParams, |
2624 | 2.90k | true, TPL_TemplateMatch)) |
2625 | 2 | Invalid = true; |
2626 | 2.93k | |
2627 | 2.93k | if (!Invalid && |
2628 | 2.93k | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
2629 | 2.93k | TPC_ClassTemplateMember)) |
2630 | 0 | Invalid = true; |
2631 | 2.93k | |
2632 | 2.93k | ++ParamIdx; |
2633 | 2.93k | continue; |
2634 | 2.93k | } |
2635 | 1 | |
2636 | 1 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
2637 | 1 | << T |
2638 | 1 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
2639 | 1 | Invalid = true; |
2640 | 1 | continue; |
2641 | 1 | } |
2642 | 26.6k | } |
2643 | 1.88M | |
2644 | 1.88M | // If there were at least as many template-ids as there were template |
2645 | 1.88M | // parameter lists, then there are no template parameter lists remaining for |
2646 | 1.88M | // the declaration itself. |
2647 | 1.88M | if (1.88M ParamIdx >= ParamLists.size()1.88M ) { |
2648 | 1.81M | if (TemplateId && 1.81M !IsFriend65 ) { |
2649 | 21 | // We don't have a template header for the declaration itself, but we |
2650 | 21 | // should. |
2651 | 21 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
2652 | 21 | TemplateId->RAngleLoc)); |
2653 | 21 | |
2654 | 21 | // Fabricate an empty template parameter list for the invented header. |
2655 | 21 | return TemplateParameterList::Create(Context, SourceLocation(), |
2656 | 21 | SourceLocation(), None, |
2657 | 21 | SourceLocation(), nullptr); |
2658 | 21 | } |
2659 | 1.81M | |
2660 | 1.81M | return nullptr; |
2661 | 1.81M | } |
2662 | 68.0k | |
2663 | 68.0k | // If there were too many template parameter lists, complain about that now. |
2664 | 68.0k | if (68.0k ParamIdx < ParamLists.size() - 168.0k ) { |
2665 | 14 | bool HasAnyExplicitSpecHeader = false; |
2666 | 14 | bool AllExplicitSpecHeaders = true; |
2667 | 28 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E28 ; ++I14 ) { |
2668 | 14 | if (ParamLists[I]->size() == 0) |
2669 | 11 | HasAnyExplicitSpecHeader = true; |
2670 | 14 | else |
2671 | 3 | AllExplicitSpecHeaders = false; |
2672 | 14 | } |
2673 | 14 | |
2674 | 14 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
2675 | 11 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
2676 | 3 | : diag::err_template_spec_extra_headers) |
2677 | 14 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
2678 | 14 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
2679 | 14 | |
2680 | 14 | // If there was a specialization somewhere, such that 'template<>' is |
2681 | 14 | // not required, and there were any 'template<>' headers, note where the |
2682 | 14 | // specialization occurred. |
2683 | 14 | if (ExplicitSpecLoc.isValid() && 14 HasAnyExplicitSpecHeader11 ) |
2684 | 11 | Diag(ExplicitSpecLoc, |
2685 | 11 | diag::note_explicit_template_spec_does_not_need_header) |
2686 | 11 | << NestedTypes.back(); |
2687 | 14 | |
2688 | 14 | // We have a template parameter list with no corresponding scope, which |
2689 | 14 | // means that the resulting template declaration can't be instantiated |
2690 | 14 | // properly (we'll end up with dependent nodes when we shouldn't). |
2691 | 14 | if (!AllExplicitSpecHeaders) |
2692 | 3 | Invalid = true; |
2693 | 14 | } |
2694 | 68.0k | |
2695 | 68.0k | // C++ [temp.expl.spec]p16: |
2696 | 68.0k | // In an explicit specialization declaration for a member of a class |
2697 | 68.0k | // template or a member template that ap- pears in namespace scope, the |
2698 | 68.0k | // member template and some of its enclosing class templates may remain |
2699 | 68.0k | // unspecialized, except that the declaration shall not explicitly |
2700 | 68.0k | // specialize a class member template if its en- closing class templates |
2701 | 68.0k | // are not explicitly specialized as well. |
2702 | 68.0k | if (ParamLists.back()->size() == 0 && |
2703 | 3.63k | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
2704 | 3.63k | false)) |
2705 | 1 | return nullptr; |
2706 | 68.0k | |
2707 | 68.0k | // Return the last template parameter list, which corresponds to the |
2708 | 68.0k | // entity being declared. |
2709 | 68.0k | return ParamLists.back(); |
2710 | 68.0k | } |
2711 | | |
2712 | 35 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
2713 | 35 | if (TemplateDecl *Template35 = Name.getAsTemplateDecl()) { |
2714 | 24 | Diag(Template->getLocation(), diag::note_template_declared_here) |
2715 | 24 | << (isa<FunctionTemplateDecl>(Template) |
2716 | 6 | ? 0 |
2717 | 18 | : isa<ClassTemplateDecl>(Template) |
2718 | 0 | ? 1 |
2719 | 18 | : isa<VarTemplateDecl>(Template) |
2720 | 16 | ? 2 |
2721 | 2 | : isa<TypeAliasTemplateDecl>(Template) ? 2 32 : 40 ) |
2722 | 24 | << Template->getDeclName(); |
2723 | 24 | return; |
2724 | 24 | } |
2725 | 11 | |
2726 | 11 | if (OverloadedTemplateStorage *11 OST11 = Name.getAsOverloadedTemplate()) { |
2727 | 3 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
2728 | 3 | IEnd = OST->end(); |
2729 | 9 | I != IEnd9 ; ++I6 ) |
2730 | 6 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
2731 | 6 | << 0 << (*I)->getDeclName(); |
2732 | 3 | |
2733 | 3 | return; |
2734 | 3 | } |
2735 | 8 | } |
2736 | | |
2737 | | static QualType |
2738 | | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
2739 | | const SmallVectorImpl<TemplateArgument> &Converted, |
2740 | | SourceLocation TemplateLoc, |
2741 | 52 | TemplateArgumentListInfo &TemplateArgs) { |
2742 | 52 | ASTContext &Context = SemaRef.getASTContext(); |
2743 | 52 | switch (BTD->getBuiltinTemplateKind()) { |
2744 | 26 | case BTK__make_integer_seq: { |
2745 | 26 | // Specializations of __make_integer_seq<S, T, N> are treated like |
2746 | 26 | // S<T, 0, ..., N-1>. |
2747 | 26 | |
2748 | 26 | // C++14 [inteseq.intseq]p1: |
2749 | 26 | // T shall be an integer type. |
2750 | 26 | if (!Converted[1].getAsType()->isIntegralType(Context)26 ) { |
2751 | 1 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
2752 | 1 | diag::err_integer_sequence_integral_element_type); |
2753 | 1 | return QualType(); |
2754 | 1 | } |
2755 | 25 | |
2756 | 25 | // C++14 [inteseq.make]p1: |
2757 | 25 | // If N is negative the program is ill-formed. |
2758 | 25 | TemplateArgument NumArgsArg = Converted[2]; |
2759 | 25 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
2760 | 25 | if (NumArgs < 025 ) { |
2761 | 1 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
2762 | 1 | diag::err_integer_sequence_negative_length); |
2763 | 1 | return QualType(); |
2764 | 1 | } |
2765 | 24 | |
2766 | 24 | QualType ArgTy = NumArgsArg.getIntegralType(); |
2767 | 24 | TemplateArgumentListInfo SyntheticTemplateArgs; |
2768 | 24 | // The type argument gets reused as the first template argument in the |
2769 | 24 | // synthetic template argument list. |
2770 | 24 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
2771 | 24 | // Expand N into 0 ... N-1. |
2772 | 24 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
2773 | 82 | I < NumArgs82 ; ++I58 ) { |
2774 | 58 | TemplateArgument TA(Context, I, ArgTy); |
2775 | 58 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
2776 | 58 | TA, ArgTy, TemplateArgs[2].getLocation())); |
2777 | 58 | } |
2778 | 24 | // The first template argument will be reused as the template decl that |
2779 | 24 | // our synthetic template arguments will be applied to. |
2780 | 24 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
2781 | 24 | TemplateLoc, SyntheticTemplateArgs); |
2782 | 24 | } |
2783 | 24 | |
2784 | 26 | case BTK__type_pack_element: |
2785 | 26 | // Specializations of |
2786 | 26 | // __type_pack_element<Index, T_1, ..., T_N> |
2787 | 26 | // are treated like T_Index. |
2788 | 26 | assert(Converted.size() == 2 && |
2789 | 26 | "__type_pack_element should be given an index and a parameter pack"); |
2790 | 26 | |
2791 | 26 | // If the Index is out of bounds, the program is ill-formed. |
2792 | 26 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
2793 | 26 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
2794 | 26 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
2795 | 26 | "type std::size_t, and hence be non-negative"); |
2796 | 26 | if (Index >= Ts.pack_size()26 ) { |
2797 | 1 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
2798 | 1 | diag::err_type_pack_element_out_of_bounds); |
2799 | 1 | return QualType(); |
2800 | 1 | } |
2801 | 25 | |
2802 | 25 | // We simply return the type at index `Index`. |
2803 | 25 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
2804 | 25 | return Nth->getAsType(); |
2805 | 0 | } |
2806 | 0 | llvm_unreachable0 ("unexpected BuiltinTemplateDecl!"); |
2807 | 0 | } |
2808 | | |
2809 | | /// Determine whether this alias template is "enable_if_t". |
2810 | 11 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
2811 | 11 | return AliasTemplate->getName().equals("enable_if_t"); |
2812 | 11 | } |
2813 | | |
2814 | | /// Collect all of the separable terms in the given condition, which |
2815 | | /// might be a conjunction. |
2816 | | /// |
2817 | | /// FIXME: The right answer is to convert the logical expression into |
2818 | | /// disjunctive normal form, so we can find the first failed term |
2819 | | /// within each possible clause. |
2820 | | static void collectConjunctionTerms(Expr *Clause, |
2821 | 124 | SmallVectorImpl<Expr *> &Terms) { |
2822 | 124 | if (auto BinOp124 = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
2823 | 50 | if (BinOp->getOpcode() == BO_LAnd50 ) { |
2824 | 10 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
2825 | 10 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
2826 | 10 | } |
2827 | 50 | |
2828 | 50 | return; |
2829 | 50 | } |
2830 | 74 | |
2831 | 74 | Terms.push_back(Clause); |
2832 | 74 | } |
2833 | | |
2834 | | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
2835 | | // a left-hand side that is value-dependent but never true. Identify |
2836 | | // the idiom and ignore that term. |
2837 | 104 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
2838 | 104 | // Top-level '||'. |
2839 | 104 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
2840 | 104 | if (!BinOp104 ) return Cond57 ; |
2841 | 47 | |
2842 | 47 | if (47 BinOp->getOpcode() != BO_LOr47 ) return Cond41 ; |
2843 | 6 | |
2844 | 6 | // With an inner '==' that has a literal on the right-hand side. |
2845 | 6 | Expr *LHS = BinOp->getLHS(); |
2846 | 6 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
2847 | 6 | if (!InnerBinOp6 ) return Cond0 ; |
2848 | 6 | |
2849 | 6 | if (6 InnerBinOp->getOpcode() != BO_EQ || |
2850 | 6 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
2851 | 0 | return Cond; |
2852 | 6 | |
2853 | 6 | // If the inner binary operation came from a macro expansion named |
2854 | 6 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
2855 | 6 | // of the '||', which is the real, user-provided condition. |
2856 | 6 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
2857 | 6 | if (!Loc.isMacroID()6 ) return Cond3 ; |
2858 | 3 | |
2859 | 3 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
2860 | 3 | if (MacroName == "CONCEPT_REQUIRES" || 3 MacroName == "CONCEPT_REQUIRES_"3 ) |
2861 | 3 | return BinOp->getRHS(); |
2862 | 0 |
|
2863 | 0 | return Cond; |
2864 | 0 | } |
2865 | | |
2866 | | std::pair<Expr *, std::string> |
2867 | 104 | Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { |
2868 | 104 | Cond = lookThroughRangesV3Condition(PP, Cond); |
2869 | 104 | |
2870 | 104 | // Separate out all of the terms in a conjunction. |
2871 | 104 | SmallVector<Expr *, 4> Terms; |
2872 | 104 | collectConjunctionTerms(Cond, Terms); |
2873 | 104 | |
2874 | 104 | // Determine which term failed. |
2875 | 104 | Expr *FailedCond = nullptr; |
2876 | 73 | for (Expr *Term : Terms) { |
2877 | 73 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
2878 | 73 | |
2879 | 73 | // Literals are uninteresting. |
2880 | 73 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
2881 | 41 | isa<IntegerLiteral>(TermAsWritten)) |
2882 | 38 | continue; |
2883 | 35 | |
2884 | 35 | // The initialization of the parameter from the argument is |
2885 | 35 | // a constant-evaluated context. |
2886 | 35 | EnterExpressionEvaluationContext ConstantEvaluated( |
2887 | 35 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
2888 | 35 | |
2889 | 35 | bool Succeeded; |
2890 | 35 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
2891 | 35 | !Succeeded35 ) { |
2892 | 27 | FailedCond = TermAsWritten; |
2893 | 27 | break; |
2894 | 27 | } |
2895 | 104 | } |
2896 | 104 | |
2897 | 104 | if (!FailedCond104 ) { |
2898 | 77 | if (!AllowTopLevelCond) |
2899 | 65 | return { nullptr, "" }; |
2900 | 12 | |
2901 | 12 | FailedCond = Cond->IgnoreParenImpCasts(); |
2902 | 12 | } |
2903 | 104 | |
2904 | 39 | std::string Description; |
2905 | 39 | { |
2906 | 39 | llvm::raw_string_ostream Out(Description); |
2907 | 39 | FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); |
2908 | 39 | } |
2909 | 39 | return { FailedCond, Description }; |
2910 | 104 | } |
2911 | | |
2912 | | QualType Sema::CheckTemplateIdType(TemplateName Name, |
2913 | | SourceLocation TemplateLoc, |
2914 | 140k | TemplateArgumentListInfo &TemplateArgs) { |
2915 | 140k | DependentTemplateName *DTN |
2916 | 140k | = Name.getUnderlying().getAsDependentTemplateName(); |
2917 | 140k | if (DTN && 140k DTN->isIdentifier()10 ) |
2918 | 140k | // When building a template-id where the template-name is dependent, |
2919 | 140k | // assume the template is a type template. Either our assumption is |
2920 | 140k | // correct, or the code is ill-formed and will be diagnosed when the |
2921 | 140k | // dependent name is substituted. |
2922 | 10 | return Context.getDependentTemplateSpecializationType(ETK_None, |
2923 | 10 | DTN->getQualifier(), |
2924 | 10 | DTN->getIdentifier(), |
2925 | 10 | TemplateArgs); |
2926 | 140k | |
2927 | 140k | TemplateDecl *Template = Name.getAsTemplateDecl(); |
2928 | 140k | if (!Template || 140k isa<FunctionTemplateDecl>(Template)140k || |
2929 | 140k | isa<VarTemplateDecl>(Template)140k ) { |
2930 | 19 | // We might have a substituted template template parameter pack. If so, |
2931 | 19 | // build a template specialization type for it. |
2932 | 19 | if (Name.getAsSubstTemplateTemplateParmPack()) |
2933 | 4 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
2934 | 15 | |
2935 | 15 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
2936 | 15 | << Name; |
2937 | 15 | NoteAllFoundTemplates(Name); |
2938 | 15 | return QualType(); |
2939 | 15 | } |
2940 | 140k | |
2941 | 140k | // Check that the template argument list is well-formed for this |
2942 | 140k | // template. |
2943 | 140k | SmallVector<TemplateArgument, 4> Converted; |
2944 | 140k | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
2945 | 140k | false, Converted)) |
2946 | 1.09k | return QualType(); |
2947 | 139k | |
2948 | 139k | QualType CanonType; |
2949 | 139k | |
2950 | 139k | bool InstantiationDependent = false; |
2951 | 139k | if (TypeAliasTemplateDecl *AliasTemplate = |
2952 | 2.72k | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
2953 | 2.72k | // Find the canonical type for this type alias template specialization. |
2954 | 2.72k | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
2955 | 2.72k | if (Pattern->isInvalidDecl()) |
2956 | 1 | return QualType(); |
2957 | 2.71k | |
2958 | 2.71k | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
2959 | 2.71k | Converted); |
2960 | 2.71k | |
2961 | 2.71k | // Only substitute for the innermost template argument list. |
2962 | 2.71k | MultiLevelTemplateArgumentList TemplateArgLists; |
2963 | 2.71k | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
2964 | 2.71k | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
2965 | 2.75k | for (unsigned I = 0; I < Depth2.75k ; ++I37 ) |
2966 | 37 | TemplateArgLists.addOuterTemplateArguments(None); |
2967 | 2.71k | |
2968 | 2.71k | LocalInstantiationScope Scope(*this); |
2969 | 2.71k | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
2970 | 2.71k | if (Inst.isInvalid()) |
2971 | 0 | return QualType(); |
2972 | 2.71k | |
2973 | 2.71k | CanonType = SubstType(Pattern->getUnderlyingType(), |
2974 | 2.71k | TemplateArgLists, AliasTemplate->getLocation(), |
2975 | 2.71k | AliasTemplate->getDeclName()); |
2976 | 2.71k | if (CanonType.isNull()2.71k ) { |
2977 | 11 | // If this was enable_if and we failed to find the nested type |
2978 | 11 | // within enable_if in a SFINAE context, dig out the specific |
2979 | 11 | // enable_if condition that failed and present that instead. |
2980 | 11 | if (isEnableIfAliasTemplate(AliasTemplate)11 ) { |
2981 | 0 | if (auto DeductionInfo0 = isSFINAEContext()) { |
2982 | 0 | if (*DeductionInfo && |
2983 | 0 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
2984 | 0 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
2985 | 0 | diag::err_typename_nested_not_found_enable_if && |
2986 | 0 | TemplateArgs[0].getArgument().getKind() |
2987 | 0 | == TemplateArgument::Expression) { |
2988 | 0 | Expr *FailedCond; |
2989 | 0 | std::string FailedDescription; |
2990 | 0 | std::tie(FailedCond, FailedDescription) = |
2991 | 0 | findFailedBooleanCondition( |
2992 | 0 | TemplateArgs[0].getSourceExpression(), |
2993 | 0 | /*AllowTopLevelCond=*/true); |
2994 | 0 |
|
2995 | 0 | // Remove the old SFINAE diagnostic. |
2996 | 0 | PartialDiagnosticAt OldDiag = |
2997 | 0 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
2998 | 0 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
2999 | 0 |
|
3000 | 0 | // Add a new SFINAE diagnostic specifying which condition |
3001 | 0 | // failed. |
3002 | 0 | (*DeductionInfo)->addSFINAEDiagnostic( |
3003 | 0 | OldDiag.first, |
3004 | 0 | PDiag(diag::err_typename_nested_not_found_requirement) |
3005 | 0 | << FailedDescription |
3006 | 0 | << FailedCond->getSourceRange()); |
3007 | 0 | } |
3008 | 0 | } |
3009 | 0 | } |
3010 | 11 | |
3011 | 11 | return QualType(); |
3012 | 11 | } |
3013 | 136k | } else if (136k Name.isDependent() || |
3014 | 134k | TemplateSpecializationType::anyDependentTemplateArguments( |
3015 | 136k | TemplateArgs, InstantiationDependent)) { |
3016 | 76.1k | // This class template specialization is a dependent |
3017 | 76.1k | // type. Therefore, its canonical type is another class template |
3018 | 76.1k | // specialization type that contains all of the converted |
3019 | 76.1k | // arguments in canonical form. This ensures that, e.g., A<T> and |
3020 | 76.1k | // A<T, T> have identical types when A is declared as: |
3021 | 76.1k | // |
3022 | 76.1k | // template<typename T, typename U = T> struct A; |
3023 | 76.1k | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
3024 | 76.1k | |
3025 | 76.1k | // This might work out to be a current instantiation, in which |
3026 | 76.1k | // case the canonical type needs to be the InjectedClassNameType. |
3027 | 76.1k | // |
3028 | 76.1k | // TODO: in theory this could be a simple hashtable lookup; most |
3029 | 76.1k | // changes to CurContext don't change the set of current |
3030 | 76.1k | // instantiations. |
3031 | 76.1k | if (isa<ClassTemplateDecl>(Template)76.1k ) { |
3032 | 127k | for (DeclContext *Ctx = CurContext; Ctx127k ; Ctx = Ctx->getLookupParent()51.6k ) { |
3033 | 127k | // If we get out to a namespace, we're done. |
3034 | 127k | if (Ctx->isFileContext()127k ) break67.5k ; |
3035 | 59.7k | |
3036 | 59.7k | // If this isn't a record, keep looking. |
3037 | 59.7k | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
3038 | 59.7k | if (!Record59.7k ) continue23.6k ; |
3039 | 36.1k | |
3040 | 36.1k | // Look for one of the two cases with InjectedClassNameTypes |
3041 | 36.1k | // and check whether it's the same template. |
3042 | 36.1k | if (36.1k !isa<ClassTemplatePartialSpecializationDecl>(Record) && |
3043 | 26.8k | !Record->getDescribedClassTemplate()) |
3044 | 2.67k | continue; |
3045 | 33.4k | |
3046 | 33.4k | // Fetch the injected class name type and check whether its |
3047 | 33.4k | // injected type is equal to the type we just built. |
3048 | 33.4k | QualType ICNT = Context.getTypeDeclType(Record); |
3049 | 33.4k | QualType Injected = cast<InjectedClassNameType>(ICNT) |
3050 | 33.4k | ->getInjectedSpecializationType(); |
3051 | 33.4k | |
3052 | 33.4k | if (CanonType != Injected->getCanonicalTypeInternal()) |
3053 | 25.3k | continue; |
3054 | 8.11k | |
3055 | 8.11k | // If so, the canonical type of this TST is the injected |
3056 | 8.11k | // class name type of the record we just found. |
3057 | 33.4k | assert(ICNT.isCanonical()); |
3058 | 8.11k | CanonType = ICNT; |
3059 | 8.11k | break; |
3060 | 8.11k | } |
3061 | 75.6k | } |
3062 | 136k | } else if (ClassTemplateDecl *60.5k ClassTemplate60.5k |
3063 | 60.5k | = dyn_cast<ClassTemplateDecl>(Template)) { |
3064 | 60.5k | // Find the class template specialization declaration that |
3065 | 60.5k | // corresponds to these arguments. |
3066 | 60.5k | void *InsertPos = nullptr; |
3067 | 60.5k | ClassTemplateSpecializationDecl *Decl |
3068 | 60.5k | = ClassTemplate->findSpecialization(Converted, InsertPos); |
3069 | 60.5k | if (!Decl60.5k ) { |
3070 | 33.1k | // This is the first time we have referenced this class template |
3071 | 33.1k | // specialization. Create the canonical declaration and add it to |
3072 | 33.1k | // the set of specializations. |
3073 | 33.1k | Decl = ClassTemplateSpecializationDecl::Create(Context, |
3074 | 33.1k | ClassTemplate->getTemplatedDecl()->getTagKind(), |
3075 | 33.1k | ClassTemplate->getDeclContext(), |
3076 | 33.1k | ClassTemplate->getTemplatedDecl()->getLocStart(), |
3077 | 33.1k | ClassTemplate->getLocation(), |
3078 | 33.1k | ClassTemplate, |
3079 | 33.1k | Converted, nullptr); |
3080 | 33.1k | ClassTemplate->AddSpecialization(Decl, InsertPos); |
3081 | 33.1k | if (ClassTemplate->isOutOfLine()) |
3082 | 51 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
3083 | 33.1k | } |
3084 | 60.5k | |
3085 | 60.5k | if (Decl->getSpecializationKind() == TSK_Undeclared60.5k ) { |
3086 | 39.4k | MultiLevelTemplateArgumentList TemplateArgLists; |
3087 | 39.4k | TemplateArgLists.addOuterTemplateArguments(Converted); |
3088 | 39.4k | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
3089 | 39.4k | Decl); |
3090 | 39.4k | } |
3091 | 60.5k | |
3092 | 60.5k | // Diagnose uses of this specialization. |
3093 | 60.5k | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
3094 | 60.5k | |
3095 | 60.5k | CanonType = Context.getTypeDeclType(Decl); |
3096 | 60.5k | assert(isa<RecordType>(CanonType) && |
3097 | 60.5k | "type of non-dependent specialization is not a RecordType"); |
3098 | 60.5k | } else if (auto *52 BTD52 = dyn_cast<BuiltinTemplateDecl>(Template)) { |
3099 | 52 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
3100 | 52 | TemplateArgs); |
3101 | 52 | } |
3102 | 139k | |
3103 | 139k | // Build the fully-sugared type for this class template |
3104 | 139k | // specialization, which refers back to the class template |
3105 | 139k | // specialization we created or found. |
3106 | 139k | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
3107 | 140k | } |
3108 | | |
3109 | | TypeResult |
3110 | | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
3111 | | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
3112 | | SourceLocation TemplateIILoc, |
3113 | | SourceLocation LAngleLoc, |
3114 | | ASTTemplateArgsPtr TemplateArgsIn, |
3115 | | SourceLocation RAngleLoc, |
3116 | 84.3k | bool IsCtorOrDtorName, bool IsClassName) { |
3117 | 84.3k | if (SS.isInvalid()) |
3118 | 3 | return true; |
3119 | 84.3k | |
3120 | 84.3k | if (84.3k !IsCtorOrDtorName && 84.3k !IsClassName84.2k && SS.isSet()78.7k ) { |
3121 | 1.57k | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
3122 | 1.57k | |
3123 | 1.57k | // C++ [temp.res]p3: |
3124 | 1.57k | // A qualified-id that refers to a type and in which the |
3125 | 1.57k | // nested-name-specifier depends on a template-parameter (14.6.2) |
3126 | 1.57k | // shall be prefixed by the keyword typename to indicate that the |
3127 | 1.57k | // qualified-id denotes a type, forming an |
3128 | 1.57k | // elaborated-type-specifier (7.1.5.3). |
3129 | 1.57k | if (!LookupCtx && 1.57k isDependentScopeSpecifier(SS)6 ) { |
3130 | 6 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
3131 | 6 | << SS.getScopeRep() << TemplateII->getName(); |
3132 | 6 | // Recover as if 'typename' were specified. |
3133 | 6 | // FIXME: This is not quite correct recovery as we don't transform SS |
3134 | 6 | // into the corresponding dependent form (and we don't diagnose missing |
3135 | 6 | // 'template' keywords within SS as a result). |
3136 | 6 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
3137 | 6 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
3138 | 6 | TemplateArgsIn, RAngleLoc); |
3139 | 6 | } |
3140 | 1.56k | |
3141 | 1.56k | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
3142 | 1.56k | // it's not actually allowed to be used as a type in most cases. Because |
3143 | 1.56k | // we annotate it before we know whether it's valid, we have to check for |
3144 | 1.56k | // this case here. |
3145 | 1.56k | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
3146 | 1.56k | if (LookupRD && 1.56k LookupRD->getIdentifier() == TemplateII287 ) { |
3147 | 16 | Diag(TemplateIILoc, |
3148 | 16 | TemplateKWLoc.isInvalid() |
3149 | 12 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
3150 | 4 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
3151 | 16 | << TemplateII << 0 /*injected-class-name used as template name*/ |
3152 | 16 | << 1 /*if any keyword was present, it was 'template'*/; |
3153 | 16 | } |
3154 | 1.57k | } |
3155 | 84.3k | |
3156 | 84.3k | TemplateName Template = TemplateD.get(); |
3157 | 84.3k | |
3158 | 84.3k | // Translate the parser's template argument list in our AST format. |
3159 | 84.3k | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
3160 | 84.3k | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
3161 | 84.3k | |
3162 | 84.3k | if (DependentTemplateName *DTN84.3k = Template.getAsDependentTemplateName()) { |
3163 | 80 | QualType T |
3164 | 80 | = Context.getDependentTemplateSpecializationType(ETK_None, |
3165 | 80 | DTN->getQualifier(), |
3166 | 80 | DTN->getIdentifier(), |
3167 | 80 | TemplateArgs); |
3168 | 80 | // Build type-source information. |
3169 | 80 | TypeLocBuilder TLB; |
3170 | 80 | DependentTemplateSpecializationTypeLoc SpecTL |
3171 | 80 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
3172 | 80 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
3173 | 80 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
3174 | 80 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3175 | 80 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
3176 | 80 | SpecTL.setLAngleLoc(LAngleLoc); |
3177 | 80 | SpecTL.setRAngleLoc(RAngleLoc); |
3178 | 160 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N160 ; ++I80 ) |
3179 | 80 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
3180 | 80 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
3181 | 80 | } |
3182 | 84.2k | |
3183 | 84.2k | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
3184 | 84.2k | if (Result.isNull()) |
3185 | 1.01k | return true; |
3186 | 83.2k | |
3187 | 83.2k | // Build type-source information. |
3188 | 83.2k | TypeLocBuilder TLB; |
3189 | 83.2k | TemplateSpecializationTypeLoc SpecTL |
3190 | 83.2k | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
3191 | 83.2k | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3192 | 83.2k | SpecTL.setTemplateNameLoc(TemplateIILoc); |
3193 | 83.2k | SpecTL.setLAngleLoc(LAngleLoc); |
3194 | 83.2k | SpecTL.setRAngleLoc(RAngleLoc); |
3195 | 217k | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e217k ; ++i133k ) |
3196 | 133k | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
3197 | 83.2k | |
3198 | 83.2k | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
3199 | 83.2k | // constructor or destructor name (in such a case, the scope specifier |
3200 | 83.2k | // will be attached to the enclosing Decl or Expr node). |
3201 | 83.2k | if (SS.isNotEmpty() && 83.2k !IsCtorOrDtorName2.16k ) { |
3202 | 2.14k | // Create an elaborated-type-specifier containing the nested-name-specifier. |
3203 | 2.14k | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
3204 | 2.14k | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
3205 | 2.14k | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
3206 | 2.14k | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
3207 | 2.14k | } |
3208 | 84.3k | |
3209 | 84.3k | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
3210 | 84.3k | } |
3211 | | |
3212 | | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
3213 | | TypeSpecifierType TagSpec, |
3214 | | SourceLocation TagLoc, |
3215 | | CXXScopeSpec &SS, |
3216 | | SourceLocation TemplateKWLoc, |
3217 | | TemplateTy TemplateD, |
3218 | | SourceLocation TemplateLoc, |
3219 | | SourceLocation LAngleLoc, |
3220 | | ASTTemplateArgsPtr TemplateArgsIn, |
3221 | 112 | SourceLocation RAngleLoc) { |
3222 | 112 | TemplateName Template = TemplateD.get(); |
3223 | 112 | |
3224 | 112 | // Translate the parser's template argument list in our AST format. |
3225 | 112 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
3226 | 112 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
3227 | 112 | |
3228 | 112 | // Determine the tag kind |
3229 | 112 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
3230 | 112 | ElaboratedTypeKeyword Keyword |
3231 | 112 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
3232 | 112 | |
3233 | 112 | if (DependentTemplateName *DTN112 = Template.getAsDependentTemplateName()) { |
3234 | 5 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
3235 | 5 | DTN->getQualifier(), |
3236 | 5 | DTN->getIdentifier(), |
3237 | 5 | TemplateArgs); |
3238 | 5 | |
3239 | 5 | // Build type-source information. |
3240 | 5 | TypeLocBuilder TLB; |
3241 | 5 | DependentTemplateSpecializationTypeLoc SpecTL |
3242 | 5 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
3243 | 5 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
3244 | 5 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
3245 | 5 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3246 | 5 | SpecTL.setTemplateNameLoc(TemplateLoc); |
3247 | 5 | SpecTL.setLAngleLoc(LAngleLoc); |
3248 | 5 | SpecTL.setRAngleLoc(RAngleLoc); |
3249 | 10 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N10 ; ++I5 ) |
3250 | 5 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
3251 | 5 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
3252 | 5 | } |
3253 | 107 | |
3254 | 107 | if (TypeAliasTemplateDecl *107 TAT107 = |
3255 | 2 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
3256 | 2 | // C++0x [dcl.type.elab]p2: |
3257 | 2 | // If the identifier resolves to a typedef-name or the simple-template-id |
3258 | 2 | // resolves to an alias template specialization, the |
3259 | 2 | // elaborated-type-specifier is ill-formed. |
3260 | 2 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
3261 | 2 | << TAT << NTK_TypeAliasTemplate << TagKind; |
3262 | 2 | Diag(TAT->getLocation(), diag::note_declared_at); |
3263 | 2 | } |
3264 | 107 | |
3265 | 107 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
3266 | 107 | if (Result.isNull()) |
3267 | 8 | return TypeResult(true); |
3268 | 99 | |
3269 | 99 | // Check the tag kind |
3270 | 99 | if (const RecordType *99 RT99 = Result->getAs<RecordType>()) { |
3271 | 67 | RecordDecl *D = RT->getDecl(); |
3272 | 67 | |
3273 | 67 | IdentifierInfo *Id = D->getIdentifier(); |
3274 | 67 | assert(Id && "templated class must have an identifier"); |
3275 | 67 | |
3276 | 67 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
3277 | 67 | TagLoc, Id)) { |
3278 | 4 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
3279 | 4 | << Result |
3280 | 4 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
3281 | 4 | Diag(D->getLocation(), diag::note_previous_use); |
3282 | 4 | } |
3283 | 67 | } |
3284 | 99 | |
3285 | 99 | // Provide source-location information for the template specialization. |
3286 | 99 | TypeLocBuilder TLB; |
3287 | 99 | TemplateSpecializationTypeLoc SpecTL |
3288 | 99 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
3289 | 99 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3290 | 99 | SpecTL.setTemplateNameLoc(TemplateLoc); |
3291 | 99 | SpecTL.setLAngleLoc(LAngleLoc); |
3292 | 99 | SpecTL.setRAngleLoc(RAngleLoc); |
3293 | 217 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e217 ; ++i118 ) |
3294 | 118 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
3295 | 112 | |
3296 | 112 | // Construct an elaborated type containing the nested-name-specifier (if any) |
3297 | 112 | // and tag keyword. |
3298 | 112 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
3299 | 112 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
3300 | 112 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
3301 | 112 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
3302 | 112 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
3303 | 112 | } |
3304 | | |
3305 | | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
3306 | | NamedDecl *PrevDecl, |
3307 | | SourceLocation Loc, |
3308 | | bool IsPartialSpecialization); |
3309 | | |
3310 | | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
3311 | | |
3312 | | static bool isTemplateArgumentTemplateParameter( |
3313 | 283 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
3314 | 283 | switch (Arg.getKind()) { |
3315 | 0 | case TemplateArgument::Null: |
3316 | 0 | case TemplateArgument::NullPtr: |
3317 | 0 | case TemplateArgument::Integral: |
3318 | 0 | case TemplateArgument::Declaration: |
3319 | 0 | case TemplateArgument::Pack: |
3320 | 0 | case TemplateArgument::TemplateExpansion: |
3321 | 0 | return false; |
3322 | 0 |
|
3323 | 279 | case TemplateArgument::Type: { |
3324 | 279 | QualType Type = Arg.getAsType(); |
3325 | 279 | const TemplateTypeParmType *TPT = |
3326 | 279 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
3327 | 81 | return TPT && !Type.hasQualifiers() && |
3328 | 279 | TPT->getDepth() == Depth81 && TPT->getIndex() == Index79 ; |
3329 | 0 | } |
3330 | 0 |
|
3331 | 2 | case TemplateArgument::Expression: { |
3332 | 2 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
3333 | 2 | if (!DRE || 2 !DRE->getDecl()2 ) |
3334 | 0 | return false; |
3335 | 2 | const NonTypeTemplateParmDecl *NTTP = |
3336 | 2 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
3337 | 2 | return NTTP && NTTP->getDepth() == Depth2 && NTTP->getIndex() == Index2 ; |
3338 | 2 | } |
3339 | 2 | |
3340 | 2 | case TemplateArgument::Template: |
3341 | 2 | const TemplateTemplateParmDecl *TTP = |
3342 | 2 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
3343 | 2 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
3344 | 2 | return TTP && TTP->getDepth() == Depth2 && TTP->getIndex() == Index2 ; |
3345 | 0 | } |
3346 | 0 | llvm_unreachable0 ("unexpected kind of template argument"); |
3347 | 0 | } |
3348 | | |
3349 | | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
3350 | 221 | ArrayRef<TemplateArgument> Args) { |
3351 | 221 | if (Params->size() != Args.size()) |
3352 | 4 | return false; |
3353 | 217 | |
3354 | 217 | unsigned Depth = Params->getDepth(); |
3355 | 217 | |
3356 | 290 | for (unsigned I = 0, N = Args.size(); I != N290 ; ++I73 ) { |
3357 | 283 | TemplateArgument Arg = Args[I]; |
3358 | 283 | |
3359 | 283 | // If the parameter is a pack expansion, the argument must be a pack |
3360 | 283 | // whose only element is a pack expansion. |
3361 | 283 | if (Params->getParam(I)->isParameterPack()283 ) { |
3362 | 3 | if (Arg.getKind() != TemplateArgument::Pack || 3 Arg.pack_size() != 13 || |
3363 | 3 | !Arg.pack_begin()->isPackExpansion()) |
3364 | 0 | return false; |
3365 | 3 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
3366 | 3 | } |
3367 | 283 | |
3368 | 283 | if (283 !isTemplateArgumentTemplateParameter(Arg, Depth, I)283 ) |
3369 | 210 | return false; |
3370 | 283 | } |
3371 | 217 | |
3372 | 7 | return true; |
3373 | 221 | } |
3374 | | |
3375 | | /// Convert the parser's template argument list representation into our form. |
3376 | | static TemplateArgumentListInfo |
3377 | 7.99k | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
3378 | 7.99k | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
3379 | 7.99k | TemplateId.RAngleLoc); |
3380 | 7.99k | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
3381 | 7.99k | TemplateId.NumArgs); |
3382 | 7.99k | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
3383 | 7.99k | return TemplateArgs; |
3384 | 7.99k | } |
3385 | | |
3386 | | template<typename PartialSpecDecl> |
3387 | 4.17k | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
3388 | 4.17k | if (Partial->getDeclContext()->isDependentContext()) |
3389 | 110 | return; |
3390 | 4.06k | |
3391 | 4.06k | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
3392 | 4.06k | // for non-substitution-failure issues? |
3393 | 4.06k | TemplateDeductionInfo Info(Partial->getLocation()); |
3394 | 4.06k | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
3395 | 4.04k | return; |
3396 | 20 | |
3397 | 20 | auto *Template = Partial->getSpecializedTemplate(); |
3398 | 20 | S.Diag(Partial->getLocation(), |
3399 | 20 | diag::ext_partial_spec_not_more_specialized_than_primary) |
3400 | 20 | << isa<VarTemplateDecl>(Template); |
3401 | 20 | |
3402 | 20 | if (Info.hasSFINAEDiagnostic()20 ) { |
3403 | 3 | PartialDiagnosticAt Diag = {SourceLocation(), |
3404 | 3 | PartialDiagnostic::NullDiagnostic()}; |
3405 | 3 | Info.takeSFINAEDiagnostic(Diag); |
3406 | 3 | SmallString<128> SFINAEArgString; |
3407 | 3 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
3408 | 3 | S.Diag(Diag.first, |
3409 | 3 | diag::note_partial_spec_not_more_specialized_than_primary) |
3410 | 3 | << SFINAEArgString; |
3411 | 3 | } |
3412 | 4.17k | |
3413 | 4.17k | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
3414 | 4.17k | } SemaTemplate.cpp:void checkMoreSpecializedThanPrimary<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*) Line | Count | Source | 3387 | 3.92k | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { | 3388 | 3.92k | if (Partial->getDeclContext()->isDependentContext()) | 3389 | 67 | return; | 3390 | 3.86k | | 3391 | 3.86k | // FIXME: Get the TDK from deduction in order to provide better diagnostics | 3392 | 3.86k | // for non-substitution-failure issues? | 3393 | 3.86k | TemplateDeductionInfo Info(Partial->getLocation()); | 3394 | 3.86k | if (S.isMoreSpecializedThanPrimary(Partial, Info)) | 3395 | 3.84k | return; | 3396 | 14 | | 3397 | 14 | auto *Template = Partial->getSpecializedTemplate(); | 3398 | 14 | S.Diag(Partial->getLocation(), | 3399 | 14 | diag::ext_partial_spec_not_more_specialized_than_primary) | 3400 | 14 | << isa<VarTemplateDecl>(Template); | 3401 | 14 | | 3402 | 14 | if (Info.hasSFINAEDiagnostic()14 ) { | 3403 | 3 | PartialDiagnosticAt Diag = {SourceLocation(), | 3404 | 3 | PartialDiagnostic::NullDiagnostic()}; | 3405 | 3 | Info.takeSFINAEDiagnostic(Diag); | 3406 | 3 | SmallString<128> SFINAEArgString; | 3407 | 3 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); | 3408 | 3 | S.Diag(Diag.first, | 3409 | 3 | diag::note_partial_spec_not_more_specialized_than_primary) | 3410 | 3 | << SFINAEArgString; | 3411 | 3 | } | 3412 | 3.92k | | 3413 | 3.92k | S.Diag(Template->getLocation(), diag::note_template_decl_here); | 3414 | 3.92k | } |
SemaTemplate.cpp:void checkMoreSpecializedThanPrimary<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*) Line | Count | Source | 3387 | 243 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { | 3388 | 243 | if (Partial->getDeclContext()->isDependentContext()) | 3389 | 43 | return; | 3390 | 200 | | 3391 | 200 | // FIXME: Get the TDK from deduction in order to provide better diagnostics | 3392 | 200 | // for non-substitution-failure issues? | 3393 | 200 | TemplateDeductionInfo Info(Partial->getLocation()); | 3394 | 200 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) | 3395 | 194 | return; | 3396 | 6 | | 3397 | 6 | auto *Template = Partial->getSpecializedTemplate(); | 3398 | 6 | S.Diag(Partial->getLocation(), | 3399 | 6 | diag::ext_partial_spec_not_more_specialized_than_primary) | 3400 | 6 | << isa<VarTemplateDecl>(Template); | 3401 | 6 | | 3402 | 6 | if (Info.hasSFINAEDiagnostic()6 ) { | 3403 | 0 | PartialDiagnosticAt Diag = {SourceLocation(), | 3404 | 0 | PartialDiagnostic::NullDiagnostic()}; | 3405 | 0 | Info.takeSFINAEDiagnostic(Diag); | 3406 | 0 | SmallString<128> SFINAEArgString; | 3407 | 0 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); | 3408 | 0 | S.Diag(Diag.first, | 3409 | 0 | diag::note_partial_spec_not_more_specialized_than_primary) | 3410 | 0 | << SFINAEArgString; | 3411 | 0 | } | 3412 | 243 | | 3413 | 243 | S.Diag(Template->getLocation(), diag::note_template_decl_here); | 3414 | 243 | } |
|
3415 | | |
3416 | | static void |
3417 | | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
3418 | 21 | const llvm::SmallBitVector &DeducibleParams) { |
3419 | 48 | for (unsigned I = 0, N = DeducibleParams.size(); I != N48 ; ++I27 ) { |
3420 | 27 | if (!DeducibleParams[I]27 ) { |
3421 | 22 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
3422 | 22 | if (Param->getDeclName()) |
3423 | 22 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
3424 | 22 | << Param->getDeclName(); |
3425 | 22 | else |
3426 | 0 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
3427 | 0 | << "(anonymous)"; |
3428 | 22 | } |
3429 | 27 | } |
3430 | 21 | } |
3431 | | |
3432 | | |
3433 | | template<typename PartialSpecDecl> |
3434 | | static void checkTemplatePartialSpecialization(Sema &S, |
3435 | 4.17k | PartialSpecDecl *Partial) { |
3436 | 4.17k | // C++1z [temp.class.spec]p8: (DR1495) |
3437 | 4.17k | // - The specialization shall be more specialized than the primary |
3438 | 4.17k | // template (14.5.5.2). |
3439 | 4.17k | checkMoreSpecializedThanPrimary(S, Partial); |
3440 | 4.17k | |
3441 | 4.17k | // C++ [temp.class.spec]p8: (DR1315) |
3442 | 4.17k | // - Each template-parameter shall appear at least once in the |
3443 | 4.17k | // template-id outside a non-deduced context. |
3444 | 4.17k | // C++1z [temp.class.spec.match]p3 (P0127R2) |
3445 | 4.17k | // If the template arguments of a partial specialization cannot be |
3446 | 4.17k | // deduced because of the structure of its template-parameter-list |
3447 | 4.17k | // and the template-id, the program is ill-formed. |
3448 | 4.17k | auto *TemplateParams = Partial->getTemplateParameters(); |
3449 | 4.17k | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
3450 | 4.17k | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
3451 | 4.17k | TemplateParams->getDepth(), DeducibleParams); |
3452 | 4.17k | |
3453 | 4.17k | if (!DeducibleParams.all()4.17k ) { |
3454 | 18 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
3455 | 18 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
3456 | 18 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
3457 | 18 | << (NumNonDeducible > 1) |
3458 | 18 | << SourceRange(Partial->getLocation(), |
3459 | 18 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
3460 | 18 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
3461 | 18 | } |
3462 | 4.17k | } SemaTemplate.cpp:void checkTemplatePartialSpecialization<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*) Line | Count | Source | 3435 | 243 | PartialSpecDecl *Partial) { | 3436 | 243 | // C++1z [temp.class.spec]p8: (DR1495) | 3437 | 243 | // - The specialization shall be more specialized than the primary | 3438 | 243 | // template (14.5.5.2). | 3439 | 243 | checkMoreSpecializedThanPrimary(S, Partial); | 3440 | 243 | | 3441 | 243 | // C++ [temp.class.spec]p8: (DR1315) | 3442 | 243 | // - Each template-parameter shall appear at least once in the | 3443 | 243 | // template-id outside a non-deduced context. | 3444 | 243 | // C++1z [temp.class.spec.match]p3 (P0127R2) | 3445 | 243 | // If the template arguments of a partial specialization cannot be | 3446 | 243 | // deduced because of the structure of its template-parameter-list | 3447 | 243 | // and the template-id, the program is ill-formed. | 3448 | 243 | auto *TemplateParams = Partial->getTemplateParameters(); | 3449 | 243 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | 3450 | 243 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | 3451 | 243 | TemplateParams->getDepth(), DeducibleParams); | 3452 | 243 | | 3453 | 243 | if (!DeducibleParams.all()243 ) { | 3454 | 5 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | 3455 | 5 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) | 3456 | 5 | << isa<VarTemplatePartialSpecializationDecl>(Partial) | 3457 | 5 | << (NumNonDeducible > 1) | 3458 | 5 | << SourceRange(Partial->getLocation(), | 3459 | 5 | Partial->getTemplateArgsAsWritten()->RAngleLoc); | 3460 | 5 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); | 3461 | 5 | } | 3462 | 243 | } |
SemaTemplate.cpp:void checkTemplatePartialSpecialization<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*) Line | Count | Source | 3435 | 3.92k | PartialSpecDecl *Partial) { | 3436 | 3.92k | // C++1z [temp.class.spec]p8: (DR1495) | 3437 | 3.92k | // - The specialization shall be more specialized than the primary | 3438 | 3.92k | // template (14.5.5.2). | 3439 | 3.92k | checkMoreSpecializedThanPrimary(S, Partial); | 3440 | 3.92k | | 3441 | 3.92k | // C++ [temp.class.spec]p8: (DR1315) | 3442 | 3.92k | // - Each template-parameter shall appear at least once in the | 3443 | 3.92k | // template-id outside a non-deduced context. | 3444 | 3.92k | // C++1z [temp.class.spec.match]p3 (P0127R2) | 3445 | 3.92k | // If the template arguments of a partial specialization cannot be | 3446 | 3.92k | // deduced because of the structure of its template-parameter-list | 3447 | 3.92k | // and the template-id, the program is ill-formed. | 3448 | 3.92k | auto *TemplateParams = Partial->getTemplateParameters(); | 3449 | 3.92k | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | 3450 | 3.92k | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | 3451 | 3.92k | TemplateParams->getDepth(), DeducibleParams); | 3452 | 3.92k | | 3453 | 3.92k | if (!DeducibleParams.all()3.92k ) { | 3454 | 13 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); | 3455 | 13 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) | 3456 | 13 | << isa<VarTemplatePartialSpecializationDecl>(Partial) | 3457 | 13 | << (NumNonDeducible > 1) | 3458 | 13 | << SourceRange(Partial->getLocation(), | 3459 | 13 | Partial->getTemplateArgsAsWritten()->RAngleLoc); | 3460 | 13 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); | 3461 | 13 | } | 3462 | 3.92k | } |
|
3463 | | |
3464 | | void Sema::CheckTemplatePartialSpecialization( |
3465 | 3.92k | ClassTemplatePartialSpecializationDecl *Partial) { |
3466 | 3.92k | checkTemplatePartialSpecialization(*this, Partial); |
3467 | 3.92k | } |
3468 | | |
3469 | | void Sema::CheckTemplatePartialSpecialization( |
3470 | 243 | VarTemplatePartialSpecializationDecl *Partial) { |
3471 | 243 | checkTemplatePartialSpecialization(*this, Partial); |
3472 | 243 | } |
3473 | | |
3474 | 36 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
3475 | 36 | // C++1z [temp.param]p11: |
3476 | 36 | // A template parameter of a deduction guide template that does not have a |
3477 | 36 | // default-argument shall be deducible from the parameter-type-list of the |
3478 | 36 | // deduction guide template. |
3479 | 36 | auto *TemplateParams = TD->getTemplateParameters(); |
3480 | 36 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
3481 | 36 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
3482 | 75 | for (unsigned I = 0; I != TemplateParams->size()75 ; ++I39 ) { |
3483 | 39 | // A parameter pack is deducible (to an empty pack). |
3484 | 39 | auto *Param = TemplateParams->getParam(I); |
3485 | 39 | if (Param->isParameterPack() || 39 hasVisibleDefaultArgument(Param)36 ) |
3486 | 7 | DeducibleParams[I] = true; |
3487 | 39 | } |
3488 | 36 | |
3489 | 36 | if (!DeducibleParams.all()36 ) { |
3490 | 3 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
3491 | 3 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
3492 | 3 | << (NumNonDeducible > 1); |
3493 | 3 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
3494 | 3 | } |
3495 | 36 | } |
3496 | | |
3497 | | DeclResult Sema::ActOnVarTemplateSpecialization( |
3498 | | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
3499 | | TemplateParameterList *TemplateParams, StorageClass SC, |
3500 | 538 | bool IsPartialSpecialization) { |
3501 | 538 | // D must be variable template id. |
3502 | 538 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
3503 | 538 | "Variable template specialization is declared with a template it."); |
3504 | 538 | |
3505 | 538 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
3506 | 538 | TemplateArgumentListInfo TemplateArgs = |
3507 | 538 | makeTemplateArgumentListInfo(*this, *TemplateId); |
3508 | 538 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
3509 | 538 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
3510 | 538 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
3511 | 538 | |
3512 | 538 | TemplateName Name = TemplateId->Template.get(); |
3513 | 538 | |
3514 | 538 | // The template-id must name a variable template. |
3515 | 538 | VarTemplateDecl *VarTemplate = |
3516 | 538 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
3517 | 538 | if (!VarTemplate538 ) { |
3518 | 8 | NamedDecl *FnTemplate; |
3519 | 8 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
3520 | 3 | FnTemplate = *OTS->begin(); |
3521 | 8 | else |
3522 | 5 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
3523 | 8 | if (FnTemplate) |
3524 | 6 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
3525 | 6 | << FnTemplate->getDeclName(); |
3526 | 2 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
3527 | 2 | << IsPartialSpecialization; |
3528 | 2 | } |
3529 | 530 | |
3530 | 530 | // Check for unexpanded parameter packs in any of the template arguments. |
3531 | 1.16k | for (unsigned I = 0, N = TemplateArgs.size(); 530 I != N1.16k ; ++I631 ) |
3532 | 631 | if (631 DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
3533 | 631 | UPPC_PartialSpecialization)) |
3534 | 0 | return true; |
3535 | 530 | |
3536 | 530 | // Check that the template argument list is well-formed for this |
3537 | 530 | // template. |
3538 | 530 | SmallVector<TemplateArgument, 4> Converted; |
3539 | 530 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
3540 | 530 | false, Converted)) |
3541 | 0 | return true; |
3542 | 530 | |
3543 | 530 | // Find the variable template (partial) specialization declaration that |
3544 | 530 | // corresponds to these arguments. |
3545 | 530 | if (530 IsPartialSpecialization530 ) { |
3546 | 221 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
3547 | 221 | TemplateArgs.size(), Converted)) |
3548 | 0 | return true; |
3549 | 221 | |
3550 | 221 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
3551 | 221 | // also do them during instantiation. |
3552 | 221 | bool InstantiationDependent; |
3553 | 221 | if (!Name.isDependent() && |
3554 | 176 | !TemplateSpecializationType::anyDependentTemplateArguments( |
3555 | 176 | TemplateArgs.arguments(), |
3556 | 221 | InstantiationDependent)) { |
3557 | 0 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
3558 | 0 | << VarTemplate->getDeclName(); |
3559 | 0 | IsPartialSpecialization = false; |
3560 | 0 | } |
3561 | 221 | |
3562 | 221 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
3563 | 221 | Converted)) { |
3564 | 7 | // C++ [temp.class.spec]p9b3: |
3565 | 7 | // |
3566 | 7 | // -- The argument list of the specialization shall not be identical |
3567 | 7 | // to the implicit argument list of the primary template. |
3568 | 7 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
3569 | 7 | << /*variable template*/ 1 |
3570 | 7 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
3571 | 7 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
3572 | 7 | // FIXME: Recover from this by treating the declaration as a redeclaration |
3573 | 7 | // of the primary template. |
3574 | 7 | return true; |
3575 | 7 | } |
3576 | 523 | } |
3577 | 523 | |
3578 | 523 | void *InsertPos = nullptr; |
3579 | 523 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
3580 | 523 | |
3581 | 523 | if (IsPartialSpecialization) |
3582 | 523 | // FIXME: Template parameter list matters too |
3583 | 214 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
3584 | 523 | else |
3585 | 309 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
3586 | 523 | |
3587 | 523 | VarTemplateSpecializationDecl *Specialization = nullptr; |
3588 | 523 | |
3589 | 523 | // Check whether we can declare a variable template specialization in |
3590 | 523 | // the current scope. |
3591 | 523 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
3592 | 523 | TemplateNameLoc, |
3593 | 523 | IsPartialSpecialization)) |
3594 | 10 | return true; |
3595 | 513 | |
3596 | 513 | if (513 PrevDecl && 513 PrevDecl->getSpecializationKind() == TSK_Undeclared88 ) { |
3597 | 0 | // Since the only prior variable template specialization with these |
3598 | 0 | // arguments was referenced but not declared, reuse that |
3599 | 0 | // declaration node as our own, updating its source location and |
3600 | 0 | // the list of outer template parameters to reflect our new declaration. |
3601 | 0 | Specialization = PrevDecl; |
3602 | 0 | Specialization->setLocation(TemplateNameLoc); |
3603 | 0 | PrevDecl = nullptr; |
3604 | 513 | } else if (513 IsPartialSpecialization513 ) { |
3605 | 214 | // Create a new class template partial specialization declaration node. |
3606 | 214 | VarTemplatePartialSpecializationDecl *PrevPartial = |
3607 | 214 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
3608 | 214 | VarTemplatePartialSpecializationDecl *Partial = |
3609 | 214 | VarTemplatePartialSpecializationDecl::Create( |
3610 | 214 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
3611 | 214 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
3612 | 214 | Converted, TemplateArgs); |
3613 | 214 | |
3614 | 214 | if (!PrevPartial) |
3615 | 162 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
3616 | 214 | Specialization = Partial; |
3617 | 214 | |
3618 | 214 | // If we are providing an explicit specialization of a member variable |
3619 | 214 | // template specialization, make a note of that. |
3620 | 214 | if (PrevPartial && 214 PrevPartial->getInstantiatedFromMember()52 ) |
3621 | 9 | PrevPartial->setMemberSpecialization(); |
3622 | 214 | |
3623 | 214 | CheckTemplatePartialSpecialization(Partial); |
3624 | 513 | } else { |
3625 | 299 | // Create a new class template specialization declaration node for |
3626 | 299 | // this explicit specialization or friend declaration. |
3627 | 299 | Specialization = VarTemplateSpecializationDecl::Create( |
3628 | 299 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
3629 | 299 | VarTemplate, DI->getType(), DI, SC, Converted); |
3630 | 299 | Specialization->setTemplateArgsInfo(TemplateArgs); |
3631 | 299 | |
3632 | 299 | if (!PrevDecl) |
3633 | 263 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
3634 | 513 | } |
3635 | 513 | |
3636 | 513 | // C++ [temp.expl.spec]p6: |
3637 | 513 | // If a template, a member template or the member of a class template is |
3638 | 513 | // explicitly specialized then that specialization shall be declared |
3639 | 513 | // before the first use of that specialization that would cause an implicit |
3640 | 513 | // instantiation to take place, in every translation unit in which such a |
3641 | 513 | // use occurs; no diagnostic is required. |
3642 | 513 | if (PrevDecl && 513 PrevDecl->getPointOfInstantiation().isValid()88 ) { |
3643 | 6 | bool Okay = false; |
3644 | 12 | for (Decl *Prev = PrevDecl; Prev12 ; Prev = Prev->getPreviousDecl()6 ) { |
3645 | 6 | // Is there any previous explicit specialization declaration? |
3646 | 6 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization6 ) { |
3647 | 0 | Okay = true; |
3648 | 0 | break; |
3649 | 0 | } |
3650 | 6 | } |
3651 | 6 | |
3652 | 6 | if (!Okay6 ) { |
3653 | 6 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
3654 | 6 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
3655 | 6 | << Name << Range; |
3656 | 6 | |
3657 | 6 | Diag(PrevDecl->getPointOfInstantiation(), |
3658 | 6 | diag::note_instantiation_required_here) |
3659 | 6 | << (PrevDecl->getTemplateSpecializationKind() != |
3660 | 6 | TSK_ImplicitInstantiation); |
3661 | 6 | return true; |
3662 | 6 | } |
3663 | 507 | } |
3664 | 507 | |
3665 | 507 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
3666 | 507 | Specialization->setLexicalDeclContext(CurContext); |
3667 | 507 | |
3668 | 507 | // Add the specialization into its lexical context, so that it can |
3669 | 507 | // be seen when iterating through the list of declarations in that |
3670 | 507 | // context. However, specializations are not found by name lookup. |
3671 | 507 | CurContext->addDecl(Specialization); |
3672 | 507 | |
3673 | 507 | // Note that this is an explicit specialization. |
3674 | 507 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
3675 | 507 | |
3676 | 507 | if (PrevDecl507 ) { |
3677 | 82 | // Check that this isn't a redefinition of this specialization, |
3678 | 82 | // merging with previous declarations. |
3679 | 82 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
3680 | 82 | ForRedeclaration); |
3681 | 82 | PrevSpec.addDecl(PrevDecl); |
3682 | 82 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
3683 | 507 | } else if (425 Specialization->isStaticDataMember() && |
3684 | 425 | Specialization->isOutOfLine()153 ) { |
3685 | 78 | Specialization->setAccess(VarTemplate->getAccess()); |
3686 | 78 | } |
3687 | 507 | |
3688 | 507 | // Link instantiations of static data members back to the template from |
3689 | 507 | // which they were instantiated. |
3690 | 507 | if (Specialization->isStaticDataMember()) |
3691 | 200 | Specialization->setInstantiationOfStaticDataMember( |
3692 | 200 | VarTemplate->getTemplatedDecl(), |
3693 | 200 | Specialization->getSpecializationKind()); |
3694 | 538 | |
3695 | 538 | return Specialization; |
3696 | 538 | } |
3697 | | |
3698 | | namespace { |
3699 | | /// \brief A partial specialization whose template arguments have matched |
3700 | | /// a given template-id. |
3701 | | struct PartialSpecMatchResult { |
3702 | | VarTemplatePartialSpecializationDecl *Partial; |
3703 | | TemplateArgumentList *Args; |
3704 | | }; |
3705 | | } // end anonymous namespace |
3706 | | |
3707 | | DeclResult |
3708 | | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
3709 | | SourceLocation TemplateNameLoc, |
3710 | 1.11k | const TemplateArgumentListInfo &TemplateArgs) { |
3711 | 1.11k | assert(Template && "A variable template id without template?"); |
3712 | 1.11k | |
3713 | 1.11k | // Check that the template argument list is well-formed for this template. |
3714 | 1.11k | SmallVector<TemplateArgument, 4> Converted; |
3715 | 1.11k | if (CheckTemplateArgumentList( |
3716 | 1.11k | Template, TemplateNameLoc, |
3717 | 1.11k | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
3718 | 1.11k | Converted)) |
3719 | 8 | return true; |
3720 | 1.10k | |
3721 | 1.10k | // Find the variable template specialization declaration that |
3722 | 1.10k | // corresponds to these arguments. |
3723 | 1.10k | void *InsertPos = nullptr; |
3724 | 1.10k | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
3725 | 372 | Converted, InsertPos)) { |
3726 | 372 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
3727 | 372 | // If we already have a variable template specialization, return it. |
3728 | 372 | return Spec; |
3729 | 372 | } |
3730 | 730 | |
3731 | 730 | // This is the first time we have referenced this variable template |
3732 | 730 | // specialization. Create the canonical declaration and add it to |
3733 | 730 | // the set of specializations, based on the closest partial specialization |
3734 | 730 | // that it represents. That is, |
3735 | 730 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
3736 | 730 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
3737 | 730 | Converted); |
3738 | 730 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
3739 | 730 | bool AmbiguousPartialSpec = false; |
3740 | 730 | typedef PartialSpecMatchResult MatchResult; |
3741 | 730 | SmallVector<MatchResult, 4> Matched; |
3742 | 730 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
3743 | 730 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
3744 | 730 | /*ForTakingAddress=*/false); |
3745 | 730 | |
3746 | 730 | // 1. Attempt to find the closest partial specialization that this |
3747 | 730 | // specializes, if any. |
3748 | 730 | // If any of the template arguments is dependent, then this is probably |
3749 | 730 | // a placeholder for an incomplete declarative context; which must be |
3750 | 730 | // complete by instantiation time. Thus, do not search through the partial |
3751 | 730 | // specializations yet. |
3752 | 730 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
3753 | 730 | // Perhaps better after unification of DeduceTemplateArguments() and |
3754 | 730 | // getMoreSpecializedPartialSpecialization(). |
3755 | 730 | bool InstantiationDependent = false; |
3756 | 730 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
3757 | 730 | TemplateArgs, InstantiationDependent)) { |
3758 | 730 | |
3759 | 730 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
3760 | 730 | Template->getPartialSpecializations(PartialSpecs); |
3761 | 730 | |
3762 | 950 | for (unsigned I = 0, N = PartialSpecs.size(); I != N950 ; ++I220 ) { |
3763 | 220 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
3764 | 220 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
3765 | 220 | |
3766 | 220 | if (TemplateDeductionResult Result = |
3767 | 77 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
3768 | 77 | // Store the failed-deduction information for use in diagnostics, later. |
3769 | 77 | // TODO: Actually use the failed-deduction info? |
3770 | 77 | FailedCandidates.addCandidate().set( |
3771 | 77 | DeclAccessPair::make(Template, AS_public), Partial, |
3772 | 77 | MakeDeductionFailureInfo(Context, Result, Info)); |
3773 | 77 | (void)Result; |
3774 | 220 | } else { |
3775 | 143 | Matched.push_back(PartialSpecMatchResult()); |
3776 | 143 | Matched.back().Partial = Partial; |
3777 | 143 | Matched.back().Args = Info.take(); |
3778 | 143 | } |
3779 | 220 | } |
3780 | 730 | |
3781 | 730 | if (Matched.size() >= 1730 ) { |
3782 | 140 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
3783 | 140 | if (Matched.size() == 1140 ) { |
3784 | 137 | // -- If exactly one matching specialization is found, the |
3785 | 137 | // instantiation is generated from that specialization. |
3786 | 137 | // We don't need to do anything for this. |
3787 | 140 | } else { |
3788 | 3 | // -- If more than one matching specialization is found, the |
3789 | 3 | // partial order rules (14.5.4.2) are used to determine |
3790 | 3 | // whether one of the specializations is more specialized |
3791 | 3 | // than the others. If none of the specializations is more |
3792 | 3 | // specialized than all of the other matching |
3793 | 3 | // specializations, then the use of the variable template is |
3794 | 3 | // ambiguous and the program is ill-formed. |
3795 | 3 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
3796 | 3 | PEnd = Matched.end(); |
3797 | 6 | P != PEnd6 ; ++P3 ) { |
3798 | 3 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
3799 | 3 | PointOfInstantiation) == |
3800 | 3 | P->Partial) |
3801 | 0 | Best = P; |
3802 | 3 | } |
3803 | 3 | |
3804 | 3 | // Determine if the best partial specialization is more specialized than |
3805 | 3 | // the others. |
3806 | 3 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
3807 | 3 | PEnd = Matched.end(); |
3808 | 6 | P != PEnd6 ; ++P3 ) { |
3809 | 6 | if (P != Best && 6 getMoreSpecializedPartialSpecialization( |
3810 | 3 | P->Partial, Best->Partial, |
3811 | 6 | PointOfInstantiation) != Best->Partial) { |
3812 | 3 | AmbiguousPartialSpec = true; |
3813 | 3 | break; |
3814 | 3 | } |
3815 | 6 | } |
3816 | 3 | } |
3817 | 140 | |
3818 | 140 | // Instantiate using the best variable template partial specialization. |
3819 | 140 | InstantiationPattern = Best->Partial; |
3820 | 140 | InstantiationArgs = Best->Args; |
3821 | 730 | } else { |
3822 | 590 | // -- If no match is found, the instantiation is generated |
3823 | 590 | // from the primary template. |
3824 | 590 | // InstantiationPattern = Template->getTemplatedDecl(); |
3825 | 590 | } |
3826 | 730 | } |
3827 | 730 | |
3828 | 730 | // 2. Create the canonical declaration. |
3829 | 730 | // Note that we do not instantiate a definition until we see an odr-use |
3830 | 730 | // in DoMarkVarDeclReferenced(). |
3831 | 730 | // FIXME: LateAttrs et al.? |
3832 | 730 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
3833 | 730 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
3834 | 730 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
3835 | 730 | if (!Decl) |
3836 | 0 | return true; |
3837 | 730 | |
3838 | 730 | if (730 AmbiguousPartialSpec730 ) { |
3839 | 3 | // Partial ordering did not produce a clear winner. Complain. |
3840 | 3 | Decl->setInvalidDecl(); |
3841 | 3 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
3842 | 3 | << Decl; |
3843 | 3 | |
3844 | 3 | // Print the matching partial specializations. |
3845 | 3 | for (MatchResult P : Matched) |
3846 | 6 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
3847 | 6 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
3848 | 6 | *P.Args); |
3849 | 3 | return true; |
3850 | 3 | } |
3851 | 727 | |
3852 | 727 | if (VarTemplatePartialSpecializationDecl *727 D727 = |
3853 | 727 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
3854 | 137 | Decl->setInstantiationOf(D, InstantiationArgs); |
3855 | 1.11k | |
3856 | 1.11k | checkSpecializationVisibility(TemplateNameLoc, Decl); |
3857 | 1.11k | |
3858 | 1.11k | assert(Decl && "No variable template specialization?"); |
3859 | 1.11k | return Decl; |
3860 | 1.11k | } |
3861 | | |
3862 | | ExprResult |
3863 | | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
3864 | | const DeclarationNameInfo &NameInfo, |
3865 | | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
3866 | 669 | const TemplateArgumentListInfo *TemplateArgs) { |
3867 | 669 | |
3868 | 669 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
3869 | 669 | *TemplateArgs); |
3870 | 669 | if (Decl.isInvalid()) |
3871 | 10 | return ExprError(); |
3872 | 659 | |
3873 | 659 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
3874 | 659 | if (!Var->getTemplateSpecializationKind()) |
3875 | 356 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
3876 | 356 | NameInfo.getLoc()); |
3877 | 669 | |
3878 | 669 | // Build an ordinary singleton decl ref. |
3879 | 669 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
3880 | 669 | /*FoundD=*/nullptr, TemplateArgs); |
3881 | 669 | } |
3882 | | |
3883 | | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
3884 | | SourceLocation TemplateKWLoc, |
3885 | | LookupResult &R, |
3886 | | bool RequiresADL, |
3887 | 13.2k | const TemplateArgumentListInfo *TemplateArgs) { |
3888 | 13.2k | // FIXME: Can we do any checking at this point? I guess we could check the |
3889 | 13.2k | // template arguments that we have against the template name, if the template |
3890 | 13.2k | // name refers to a single template. That's not a terribly common case, |
3891 | 13.2k | // though. |
3892 | 13.2k | // foo<int> could identify a single function unambiguously |
3893 | 13.2k | // This approach does NOT work, since f<int>(1); |
3894 | 13.2k | // gets resolved prior to resorting to overload resolution |
3895 | 13.2k | // i.e., template<class T> void f(double); |
3896 | 13.2k | // vs template<class T, class U> void f(U); |
3897 | 13.2k | |
3898 | 13.2k | // These should be filtered out by our callers. |
3899 | 13.2k | assert(!R.empty() && "empty lookup results when building templateid"); |
3900 | 13.2k | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
3901 | 13.2k | |
3902 | 13.2k | // In C++1y, check variable template ids. |
3903 | 13.2k | bool InstantiationDependent; |
3904 | 13.2k | if (R.getAsSingle<VarTemplateDecl>() && |
3905 | 706 | !TemplateSpecializationType::anyDependentTemplateArguments( |
3906 | 13.2k | *TemplateArgs, InstantiationDependent)) { |
3907 | 669 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
3908 | 669 | R.getAsSingle<VarTemplateDecl>(), |
3909 | 669 | TemplateKWLoc, TemplateArgs); |
3910 | 669 | } |
3911 | 12.5k | |
3912 | 12.5k | // We don't want lookup warnings at this point. |
3913 | 12.5k | R.suppressDiagnostics(); |
3914 | 12.5k | |
3915 | 12.5k | UnresolvedLookupExpr *ULE |
3916 | 12.5k | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
3917 | 12.5k | SS.getWithLocInContext(Context), |
3918 | 12.5k | TemplateKWLoc, |
3919 | 12.5k | R.getLookupNameInfo(), |
3920 | 12.5k | RequiresADL, TemplateArgs, |
3921 | 12.5k | R.begin(), R.end()); |
3922 | 12.5k | |
3923 | 12.5k | return ULE; |
3924 | 12.5k | } |
3925 | | |
3926 | | // We actually only call this from template instantiation. |
3927 | | ExprResult |
3928 | | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
3929 | | SourceLocation TemplateKWLoc, |
3930 | | const DeclarationNameInfo &NameInfo, |
3931 | 27 | const TemplateArgumentListInfo *TemplateArgs) { |
3932 | 27 | |
3933 | 27 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
3934 | 27 | DeclContext *DC; |
3935 | 27 | if (!(DC = computeDeclContext(SS, false)) || |
3936 | 27 | DC->isDependentContext() || |
3937 | 27 | RequireCompleteDeclContext(SS, DC)) |
3938 | 0 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
3939 | 27 | |
3940 | 27 | bool MemberOfUnknownSpecialization; |
3941 | 27 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
3942 | 27 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
3943 | 27 | MemberOfUnknownSpecialization); |
3944 | 27 | |
3945 | 27 | if (R.isAmbiguous()) |
3946 | 0 | return ExprError(); |
3947 | 27 | |
3948 | 27 | if (27 R.empty()27 ) { |
3949 | 3 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
3950 | 3 | << NameInfo.getName() << SS.getRange(); |
3951 | 3 | return ExprError(); |
3952 | 3 | } |
3953 | 24 | |
3954 | 24 | if (ClassTemplateDecl *24 Temp24 = R.getAsSingle<ClassTemplateDecl>()) { |
3955 | 1 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
3956 | 1 | << SS.getScopeRep() |
3957 | 1 | << NameInfo.getName().getAsString() << SS.getRange(); |
3958 | 1 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
3959 | 1 | return ExprError(); |
3960 | 1 | } |
3961 | 23 | |
3962 | 23 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
3963 | 23 | } |
3964 | | |
3965 | | /// \brief Form a dependent template name. |
3966 | | /// |
3967 | | /// This action forms a dependent template name given the template |
3968 | | /// name and its (presumably dependent) scope specifier. For |
3969 | | /// example, given "MetaFun::template apply", the scope specifier \p |
3970 | | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
3971 | | /// of the "template" keyword, and "apply" is the \p Name. |
3972 | | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
3973 | | CXXScopeSpec &SS, |
3974 | | SourceLocation TemplateKWLoc, |
3975 | | UnqualifiedId &Name, |
3976 | | ParsedType ObjectType, |
3977 | | bool EnteringContext, |
3978 | | TemplateTy &Result, |
3979 | 1.12k | bool AllowInjectedClassName) { |
3980 | 1.12k | if (TemplateKWLoc.isValid() && 1.12k S618 && !S->getTemplateParamParent()618 ) |
3981 | 88 | Diag(TemplateKWLoc, |
3982 | 88 | getLangOpts().CPlusPlus11 ? |
3983 | 60 | diag::warn_cxx98_compat_template_outside_of_template : |
3984 | 28 | diag::ext_template_outside_of_template) |
3985 | 88 | << FixItHint::CreateRemoval(TemplateKWLoc); |
3986 | 1.12k | |
3987 | 1.12k | DeclContext *LookupCtx = nullptr; |
3988 | 1.12k | if (SS.isSet()) |
3989 | 885 | LookupCtx = computeDeclContext(SS, EnteringContext); |
3990 | 1.12k | if (!LookupCtx && 1.12k ObjectType672 ) |
3991 | 214 | LookupCtx = computeDeclContext(ObjectType.get()); |
3992 | 1.12k | if (LookupCtx1.12k ) { |
3993 | 562 | // C++0x [temp.names]p5: |
3994 | 562 | // If a name prefixed by the keyword template is not the name of |
3995 | 562 | // a template, the program is ill-formed. [Note: the keyword |
3996 | 562 | // template may not be applied to non-template members of class |
3997 | 562 | // templates. -end note ] [ Note: as is the case with the |
3998 | 562 | // typename prefix, the template prefix is allowed in cases |
3999 | 562 | // where it is not strictly necessary; i.e., when the |
4000 | 562 | // nested-name-specifier or the expression on the left of the -> |
4001 | 562 | // or . is not dependent on a template-parameter, or the use |
4002 | 562 | // does not appear in the scope of a template. -end note] |
4003 | 562 | // |
4004 | 562 | // Note: C++03 was more strict here, because it banned the use of |
4005 | 562 | // the "template" keyword prior to a template-name that was not a |
4006 | 562 | // dependent name. C++ DR468 relaxed this requirement (the |
4007 | 562 | // "template" keyword is now permitted). We follow the C++0x |
4008 | 562 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
4009 | 562 | bool MemberOfUnknownSpecialization; |
4010 | 562 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
4011 | 562 | ObjectType, EnteringContext, Result, |
4012 | 562 | MemberOfUnknownSpecialization); |
4013 | 562 | if (TNK == TNK_Non_template && 562 LookupCtx->isDependentContext()43 && |
4014 | 19 | isa<CXXRecordDecl>(LookupCtx) && |
4015 | 19 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
4016 | 562 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()19 )) { |
4017 | 17 | // This is a dependent template. Handle it below. |
4018 | 562 | } else if (545 TNK == TNK_Non_template545 ) { |
4019 | 26 | Diag(Name.getLocStart(), |
4020 | 26 | diag::err_template_kw_refers_to_non_template) |
4021 | 26 | << GetNameFromUnqualifiedId(Name).getName() |
4022 | 26 | << Name.getSourceRange() |
4023 | 26 | << TemplateKWLoc; |
4024 | 26 | return TNK_Non_template; |
4025 | 0 | } else { |
4026 | 519 | // We found something; return it. |
4027 | 519 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
4028 | 519 | if (!AllowInjectedClassName && 519 SS.isSet()186 && LookupRD186 && |
4029 | 519 | Name.getKind() == UnqualifiedId::IK_Identifier186 && Name.Identifier186 && |
4030 | 519 | LookupRD->getIdentifier() == Name.Identifier186 ) { |
4031 | 26 | // C++14 [class.qual]p2: |
4032 | 26 | // In a lookup in which function names are not ignored and the |
4033 | 26 | // nested-name-specifier nominates a class C, if the name specified |
4034 | 26 | // [...] is the injected-class-name of C, [...] the name is instead |
4035 | 26 | // considered to name the constructor |
4036 | 26 | // |
4037 | 26 | // We don't get here if naming the constructor would be valid, so we |
4038 | 26 | // just reject immediately and recover by treating the |
4039 | 26 | // injected-class-name as naming the template. |
4040 | 26 | Diag(Name.getLocStart(), |
4041 | 26 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
4042 | 26 | << Name.Identifier << 0 /*injected-class-name used as template name*/ |
4043 | 26 | << 1 /*'template' keyword was used*/; |
4044 | 26 | } |
4045 | 545 | return TNK; |
4046 | 545 | } |
4047 | 578 | } |
4048 | 578 | |
4049 | 578 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
4050 | 578 | |
4051 | 578 | switch (Name.getKind()) { |
4052 | 549 | case UnqualifiedId::IK_Identifier: |
4053 | 549 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
4054 | 549 | Name.Identifier)); |
4055 | 549 | return TNK_Dependent_template_name; |
4056 | 578 | |
4057 | 29 | case UnqualifiedId::IK_OperatorFunctionId: |
4058 | 29 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
4059 | 29 | Name.OperatorFunctionId.Operator)); |
4060 | 29 | return TNK_Function_template; |
4061 | 578 | |
4062 | 0 | case UnqualifiedId::IK_LiteralOperatorId: |
4063 | 0 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
4064 | 578 | |
4065 | 0 | default: |
4066 | 0 | break; |
4067 | 0 | } |
4068 | 0 |
|
4069 | 0 | Diag(Name.getLocStart(), |
4070 | 0 | diag::err_template_kw_refers_to_non_template) |
4071 | 0 | << GetNameFromUnqualifiedId(Name).getName() |
4072 | 0 | << Name.getSourceRange() |
4073 | 0 | << TemplateKWLoc; |
4074 | 0 | return TNK_Non_template; |
4075 | 0 | } |
4076 | | |
4077 | | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
4078 | | TemplateArgumentLoc &AL, |
4079 | 235k | SmallVectorImpl<TemplateArgument> &Converted) { |
4080 | 235k | const TemplateArgument &Arg = AL.getArgument(); |
4081 | 235k | QualType ArgType; |
4082 | 235k | TypeSourceInfo *TSI = nullptr; |
4083 | 235k | |
4084 | 235k | // Check template type parameter. |
4085 | 235k | switch(Arg.getKind()) { |
4086 | 234k | case TemplateArgument::Type: |
4087 | 234k | // C++ [temp.arg.type]p1: |
4088 | 234k | // A template-argument for a template-parameter which is a |
4089 | 234k | // type shall be a type-id. |
4090 | 234k | ArgType = Arg.getAsType(); |
4091 | 234k | TSI = AL.getTypeSourceInfo(); |
4092 | 234k | break; |
4093 | 22 | case TemplateArgument::Template: { |
4094 | 22 | // We have a template type parameter but the template argument |
4095 | 22 | // is a template without any arguments. |
4096 | 22 | SourceRange SR = AL.getSourceRange(); |
4097 | 22 | TemplateName Name = Arg.getAsTemplate(); |
4098 | 22 | Diag(SR.getBegin(), diag::err_template_missing_args) |
4099 | 22 | << (int)getTemplateNameKindForDiagnostics(Name) << Name << SR; |
4100 | 22 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
4101 | 22 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
4102 | 22 | |
4103 | 22 | return true; |
4104 | 235k | } |
4105 | 143 | case TemplateArgument::Expression: { |
4106 | 143 | // We have a template type parameter but the template argument is an |
4107 | 143 | // expression; see if maybe it is missing the "typename" keyword. |
4108 | 143 | CXXScopeSpec SS; |
4109 | 143 | DeclarationNameInfo NameInfo; |
4110 | 143 | |
4111 | 143 | if (DeclRefExpr *ArgExpr143 = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
4112 | 14 | SS.Adopt(ArgExpr->getQualifierLoc()); |
4113 | 14 | NameInfo = ArgExpr->getNameInfo(); |
4114 | 143 | } else if (DependentScopeDeclRefExpr *129 ArgExpr129 = |
4115 | 30 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
4116 | 30 | SS.Adopt(ArgExpr->getQualifierLoc()); |
4117 | 30 | NameInfo = ArgExpr->getNameInfo(); |
4118 | 129 | } else if (CXXDependentScopeMemberExpr *99 ArgExpr99 = |
4119 | 18 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
4120 | 18 | if (ArgExpr->isImplicitAccess()18 ) { |
4121 | 12 | SS.Adopt(ArgExpr->getQualifierLoc()); |
4122 | 12 | NameInfo = ArgExpr->getMemberNameInfo(); |
4123 | 12 | } |
4124 | 129 | } |
4125 | 143 | |
4126 | 143 | if (auto *II143 = NameInfo.getName().getAsIdentifierInfo()) { |
4127 | 48 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
4128 | 48 | LookupParsedName(Result, CurScope, &SS); |
4129 | 48 | |
4130 | 48 | if (Result.getAsSingle<TypeDecl>() || |
4131 | 48 | Result.getResultKind() == |
4132 | 48 | LookupResult::NotFoundInCurrentInstantiation) { |
4133 | 36 | // Suggest that the user add 'typename' before the NNS. |
4134 | 36 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
4135 | 36 | Diag(Loc, getLangOpts().MSVCCompat |
4136 | 18 | ? diag::ext_ms_template_type_arg_missing_typename |
4137 | 18 | : diag::err_template_arg_must_be_type_suggest) |
4138 | 36 | << FixItHint::CreateInsertion(Loc, "typename "); |
4139 | 36 | Diag(Param->getLocation(), diag::note_template_param_here); |
4140 | 36 | |
4141 | 36 | // Recover by synthesizing a type using the location information that we |
4142 | 36 | // already have. |
4143 | 36 | ArgType = |
4144 | 36 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
4145 | 36 | TypeLocBuilder TLB; |
4146 | 36 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
4147 | 36 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
4148 | 36 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4149 | 36 | TL.setNameLoc(NameInfo.getLoc()); |
4150 | 36 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
4151 | 36 | |
4152 | 36 | // Overwrite our input TemplateArgumentLoc so that we can recover |
4153 | 36 | // properly. |
4154 | 36 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
4155 | 36 | TemplateArgumentLocInfo(TSI)); |
4156 | 36 | |
4157 | 36 | break; |
4158 | 36 | } |
4159 | 107 | } |
4160 | 107 | // fallthrough |
4161 | 107 | LLVM_FALLTHROUGH107 ; |
4162 | 107 | } |
4163 | 107 | default: { |
4164 | 107 | // We have a template type parameter but the template argument |
4165 | 107 | // is not a type. |
4166 | 107 | SourceRange SR = AL.getSourceRange(); |
4167 | 107 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
4168 | 107 | Diag(Param->getLocation(), diag::note_template_param_here); |
4169 | 107 | |
4170 | 107 | return true; |
4171 | 234k | } |
4172 | 234k | } |
4173 | 234k | |
4174 | 234k | if (234k CheckTemplateArgument(Param, TSI)234k ) |
4175 | 4 | return true; |
4176 | 234k | |
4177 | 234k | // Add the converted template type argument. |
4178 | 234k | ArgType = Context.getCanonicalType(ArgType); |
4179 | 234k | |
4180 | 234k | // Objective-C ARC: |
4181 | 234k | // If an explicitly-specified template argument type is a lifetime type |
4182 | 234k | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
4183 | 234k | if (getLangOpts().ObjCAutoRefCount && |
4184 | 666 | ArgType->isObjCLifetimeType() && |
4185 | 234k | !ArgType.getObjCLifetime()305 ) { |
4186 | 70 | Qualifiers Qs; |
4187 | 70 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
4188 | 70 | ArgType = Context.getQualifiedType(ArgType, Qs); |
4189 | 70 | } |
4190 | 235k | |
4191 | 235k | Converted.push_back(TemplateArgument(ArgType)); |
4192 | 235k | return false; |
4193 | 235k | } |
4194 | | |
4195 | | /// \brief Substitute template arguments into the default template argument for |
4196 | | /// the given template type parameter. |
4197 | | /// |
4198 | | /// \param SemaRef the semantic analysis object for which we are performing |
4199 | | /// the substitution. |
4200 | | /// |
4201 | | /// \param Template the template that we are synthesizing template arguments |
4202 | | /// for. |
4203 | | /// |
4204 | | /// \param TemplateLoc the location of the template name that started the |
4205 | | /// template-id we are checking. |
4206 | | /// |
4207 | | /// \param RAngleLoc the location of the right angle bracket ('>') that |
4208 | | /// terminates the template-id. |
4209 | | /// |
4210 | | /// \param Param the template template parameter whose default we are |
4211 | | /// substituting into. |
4212 | | /// |
4213 | | /// \param Converted the list of template arguments provided for template |
4214 | | /// parameters that precede \p Param in the template parameter list. |
4215 | | /// \returns the substituted template argument, or NULL if an error occurred. |
4216 | | static TypeSourceInfo * |
4217 | | SubstDefaultTemplateArgument(Sema &SemaRef, |
4218 | | TemplateDecl *Template, |
4219 | | SourceLocation TemplateLoc, |
4220 | | SourceLocation RAngleLoc, |
4221 | | TemplateTypeParmDecl *Param, |
4222 | 2.19k | SmallVectorImpl<TemplateArgument> &Converted) { |
4223 | 2.19k | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
4224 | 2.19k | |
4225 | 2.19k | // If the argument type is dependent, instantiate it now based |
4226 | 2.19k | // on the previously-computed template arguments. |
4227 | 2.19k | if (ArgType->getType()->isDependentType()2.19k ) { |
4228 | 1.15k | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
4229 | 1.15k | Param, Template, Converted, |
4230 | 1.15k | SourceRange(TemplateLoc, RAngleLoc)); |
4231 | 1.15k | if (Inst.isInvalid()) |
4232 | 106 | return nullptr; |
4233 | 1.05k | |
4234 | 1.05k | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
4235 | 1.05k | |
4236 | 1.05k | // Only substitute for the innermost template argument list. |
4237 | 1.05k | MultiLevelTemplateArgumentList TemplateArgLists; |
4238 | 1.05k | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
4239 | 1.06k | for (unsigned i = 0, e = Param->getDepth(); i != e1.06k ; ++i17 ) |
4240 | 17 | TemplateArgLists.addOuterTemplateArguments(None); |
4241 | 1.15k | |
4242 | 1.15k | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
4243 | 1.15k | ArgType = |
4244 | 1.15k | SemaRef.SubstType(ArgType, TemplateArgLists, |
4245 | 1.15k | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
4246 | 1.15k | } |
4247 | 2.19k | |
4248 | 2.08k | return ArgType; |
4249 | 2.19k | } |
4250 | | |
4251 | | /// \brief Substitute template arguments into the default template argument for |
4252 | | /// the given non-type template parameter. |
4253 | | /// |
4254 | | /// \param SemaRef the semantic analysis object for which we are performing |
4255 | | /// the substitution. |
4256 | | /// |
4257 | | /// \param Template the template that we are synthesizing template arguments |
4258 | | /// for. |
4259 | | /// |
4260 | | /// \param TemplateLoc the location of the template name that started the |
4261 | | /// template-id we are checking. |
4262 | | /// |
4263 | | /// \param RAngleLoc the location of the right angle bracket ('>') that |
4264 | | /// terminates the template-id. |
4265 | | /// |
4266 | | /// \param Param the non-type template parameter whose default we are |
4267 | | /// substituting into. |
4268 | | /// |
4269 | | /// \param Converted the list of template arguments provided for template |
4270 | | /// parameters that precede \p Param in the template parameter list. |
4271 | | /// |
4272 | | /// \returns the substituted template argument, or NULL if an error occurred. |
4273 | | static ExprResult |
4274 | | SubstDefaultTemplateArgument(Sema &SemaRef, |
4275 | | TemplateDecl *Template, |
4276 | | SourceLocation TemplateLoc, |
4277 | | SourceLocation RAngleLoc, |
4278 | | NonTypeTemplateParmDecl *Param, |
4279 | 741 | SmallVectorImpl<TemplateArgument> &Converted) { |
4280 | 741 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
4281 | 741 | Param, Template, Converted, |
4282 | 741 | SourceRange(TemplateLoc, RAngleLoc)); |
4283 | 741 | if (Inst.isInvalid()) |
4284 | 180 | return ExprError(); |
4285 | 561 | |
4286 | 561 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
4287 | 561 | |
4288 | 561 | // Only substitute for the innermost template argument list. |
4289 | 561 | MultiLevelTemplateArgumentList TemplateArgLists; |
4290 | 561 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
4291 | 565 | for (unsigned i = 0, e = Param->getDepth(); i != e565 ; ++i4 ) |
4292 | 4 | TemplateArgLists.addOuterTemplateArguments(None); |
4293 | 741 | |
4294 | 741 | EnterExpressionEvaluationContext ConstantEvaluated( |
4295 | 741 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
4296 | 741 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
4297 | 741 | } |
4298 | | |
4299 | | /// \brief Substitute template arguments into the default template argument for |
4300 | | /// the given template template parameter. |
4301 | | /// |
4302 | | /// \param SemaRef the semantic analysis object for which we are performing |
4303 | | /// the substitution. |
4304 | | /// |
4305 | | /// \param Template the template that we are synthesizing template arguments |
4306 | | /// for. |
4307 | | /// |
4308 | | /// \param TemplateLoc the location of the template name that started the |
4309 | | /// template-id we are checking. |
4310 | | /// |
4311 | | /// \param RAngleLoc the location of the right angle bracket ('>') that |
4312 | | /// terminates the template-id. |
4313 | | /// |
4314 | | /// \param Param the template template parameter whose default we are |
4315 | | /// substituting into. |
4316 | | /// |
4317 | | /// \param Converted the list of template arguments provided for template |
4318 | | /// parameters that precede \p Param in the template parameter list. |
4319 | | /// |
4320 | | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
4321 | | /// source-location information) that precedes the template name. |
4322 | | /// |
4323 | | /// \returns the substituted template argument, or NULL if an error occurred. |
4324 | | static TemplateName |
4325 | | SubstDefaultTemplateArgument(Sema &SemaRef, |
4326 | | TemplateDecl *Template, |
4327 | | SourceLocation TemplateLoc, |
4328 | | SourceLocation RAngleLoc, |
4329 | | TemplateTemplateParmDecl *Param, |
4330 | | SmallVectorImpl<TemplateArgument> &Converted, |
4331 | 125 | NestedNameSpecifierLoc &QualifierLoc) { |
4332 | 125 | Sema::InstantiatingTemplate Inst( |
4333 | 125 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
4334 | 125 | SourceRange(TemplateLoc, RAngleLoc)); |
4335 | 125 | if (Inst.isInvalid()) |
4336 | 0 | return TemplateName(); |
4337 | 125 | |
4338 | 125 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
4339 | 125 | |
4340 | 125 | // Only substitute for the innermost template argument list. |
4341 | 125 | MultiLevelTemplateArgumentList TemplateArgLists; |
4342 | 125 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
4343 | 129 | for (unsigned i = 0, e = Param->getDepth(); i != e129 ; ++i4 ) |
4344 | 4 | TemplateArgLists.addOuterTemplateArguments(None); |
4345 | 125 | |
4346 | 125 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
4347 | 125 | // Substitute into the nested-name-specifier first, |
4348 | 125 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
4349 | 125 | if (QualifierLoc125 ) { |
4350 | 15 | QualifierLoc = |
4351 | 15 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
4352 | 15 | if (!QualifierLoc) |
4353 | 0 | return TemplateName(); |
4354 | 125 | } |
4355 | 125 | |
4356 | 125 | return SemaRef.SubstTemplateName( |
4357 | 125 | QualifierLoc, |
4358 | 125 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
4359 | 125 | Param->getDefaultArgument().getTemplateNameLoc(), |
4360 | 125 | TemplateArgLists); |
4361 | 125 | } |
4362 | | |
4363 | | /// \brief If the given template parameter has a default template |
4364 | | /// argument, substitute into that default template argument and |
4365 | | /// return the corresponding template argument. |
4366 | | TemplateArgumentLoc |
4367 | | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
4368 | | SourceLocation TemplateLoc, |
4369 | | SourceLocation RAngleLoc, |
4370 | | Decl *Param, |
4371 | | SmallVectorImpl<TemplateArgument> |
4372 | | &Converted, |
4373 | 1.60k | bool &HasDefaultArg) { |
4374 | 1.60k | HasDefaultArg = false; |
4375 | 1.60k | |
4376 | 1.60k | if (TemplateTypeParmDecl *TypeParm1.60k = dyn_cast<TemplateTypeParmDecl>(Param)) { |
4377 | 1.44k | if (!hasVisibleDefaultArgument(TypeParm)) |
4378 | 521 | return TemplateArgumentLoc(); |
4379 | 928 | |
4380 | 928 | HasDefaultArg = true; |
4381 | 928 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
4382 | 928 | TemplateLoc, |
4383 | 928 | RAngleLoc, |
4384 | 928 | TypeParm, |
4385 | 928 | Converted); |
4386 | 928 | if (DI) |
4387 | 349 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
4388 | 579 | |
4389 | 579 | return TemplateArgumentLoc(); |
4390 | 579 | } |
4391 | 151 | |
4392 | 151 | if (NonTypeTemplateParmDecl *151 NonTypeParm151 |
4393 | 138 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
4394 | 138 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
4395 | 37 | return TemplateArgumentLoc(); |
4396 | 101 | |
4397 | 101 | HasDefaultArg = true; |
4398 | 101 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
4399 | 101 | TemplateLoc, |
4400 | 101 | RAngleLoc, |
4401 | 101 | NonTypeParm, |
4402 | 101 | Converted); |
4403 | 101 | if (Arg.isInvalid()) |
4404 | 9 | return TemplateArgumentLoc(); |
4405 | 92 | |
4406 | 92 | Expr *ArgE = Arg.getAs<Expr>(); |
4407 | 92 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
4408 | 92 | } |
4409 | 13 | |
4410 | 13 | TemplateTemplateParmDecl *TempTempParm |
4411 | 13 | = cast<TemplateTemplateParmDecl>(Param); |
4412 | 13 | if (!hasVisibleDefaultArgument(TempTempParm)) |
4413 | 0 | return TemplateArgumentLoc(); |
4414 | 13 | |
4415 | 13 | HasDefaultArg = true; |
4416 | 13 | NestedNameSpecifierLoc QualifierLoc; |
4417 | 13 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
4418 | 13 | TemplateLoc, |
4419 | 13 | RAngleLoc, |
4420 | 13 | TempTempParm, |
4421 | 13 | Converted, |
4422 | 13 | QualifierLoc); |
4423 | 13 | if (TName.isNull()) |
4424 | 0 | return TemplateArgumentLoc(); |
4425 | 13 | |
4426 | 13 | return TemplateArgumentLoc(TemplateArgument(TName), |
4427 | 13 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
4428 | 13 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
4429 | 13 | } |
4430 | | |
4431 | | /// Convert a template-argument that we parsed as a type into a template, if |
4432 | | /// possible. C++ permits injected-class-names to perform dual service as |
4433 | | /// template template arguments and as template type arguments. |
4434 | 29 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
4435 | 29 | // Extract and step over any surrounding nested-name-specifier. |
4436 | 29 | NestedNameSpecifierLoc QualLoc; |
4437 | 29 | if (auto ETLoc29 = TLoc.getAs<ElaboratedTypeLoc>()) { |
4438 | 8 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
4439 | 0 | return TemplateArgumentLoc(); |
4440 | 8 | |
4441 | 8 | QualLoc = ETLoc.getQualifierLoc(); |
4442 | 8 | TLoc = ETLoc.getNamedTypeLoc(); |
4443 | 8 | } |
4444 | 29 | |
4445 | 29 | // If this type was written as an injected-class-name, it can be used as a |
4446 | 29 | // template template argument. |
4447 | 29 | if (auto 29 InjLoc29 = TLoc.getAs<InjectedClassNameTypeLoc>()) |
4448 | 1 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
4449 | 1 | QualLoc, InjLoc.getNameLoc()); |
4450 | 28 | |
4451 | 28 | // If this type was written as an injected-class-name, it may have been |
4452 | 28 | // converted to a RecordType during instantiation. If the RecordType is |
4453 | 28 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
4454 | 28 | // template specialization, it must have come from an injected-class-name. |
4455 | 28 | if (auto 28 RecLoc28 = TLoc.getAs<RecordTypeLoc>()) |
4456 | 24 | if (auto *24 CTSD24 = |
4457 | 24 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
4458 | 24 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
4459 | 24 | QualLoc, RecLoc.getNameLoc()); |
4460 | 4 | |
4461 | 4 | return TemplateArgumentLoc(); |
4462 | 4 | } |
4463 | | |
4464 | | /// \brief Check that the given template argument corresponds to the given |
4465 | | /// template parameter. |
4466 | | /// |
4467 | | /// \param Param The template parameter against which the argument will be |
4468 | | /// checked. |
4469 | | /// |
4470 | | /// \param Arg The template argument, which may be updated due to conversions. |
4471 | | /// |
4472 | | /// \param Template The template in which the template argument resides. |
4473 | | /// |
4474 | | /// \param TemplateLoc The location of the template name for the template |
4475 | | /// whose argument list we're matching. |
4476 | | /// |
4477 | | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
4478 | | /// the template argument list. |
4479 | | /// |
4480 | | /// \param ArgumentPackIndex The index into the argument pack where this |
4481 | | /// argument will be placed. Only valid if the parameter is a parameter pack. |
4482 | | /// |
4483 | | /// \param Converted The checked, converted argument will be added to the |
4484 | | /// end of this small vector. |
4485 | | /// |
4486 | | /// \param CTAK Describes how we arrived at this particular template argument: |
4487 | | /// explicitly written, deduced, etc. |
4488 | | /// |
4489 | | /// \returns true on error, false otherwise. |
4490 | | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
4491 | | TemplateArgumentLoc &Arg, |
4492 | | NamedDecl *Template, |
4493 | | SourceLocation TemplateLoc, |
4494 | | SourceLocation RAngleLoc, |
4495 | | unsigned ArgumentPackIndex, |
4496 | | SmallVectorImpl<TemplateArgument> &Converted, |
4497 | 297k | CheckTemplateArgumentKind CTAK) { |
4498 | 297k | // Check template type parameters. |
4499 | 297k | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
4500 | 235k | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
4501 | 62.4k | |
4502 | 62.4k | // Check non-type template parameters. |
4503 | 62.4k | if (NonTypeTemplateParmDecl *62.4k NTTP62.4k =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
4504 | 59.1k | // Do substitution on the type of the non-type template parameter |
4505 | 59.1k | // with the template arguments we've seen thus far. But if the |
4506 | 59.1k | // template has a dependent context then we cannot substitute yet. |
4507 | 59.1k | QualType NTTPType = NTTP->getType(); |
4508 | 59.1k | if (NTTP->isParameterPack() && 59.1k NTTP->isExpandedParameterPack()1.56k ) |
4509 | 45 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
4510 | 59.1k | |
4511 | 59.1k | if (NTTPType->isDependentType() && |
4512 | 2.54k | !isa<TemplateTemplateParmDecl>(Template) && |
4513 | 59.1k | !Template->getDeclContext()->isDependentContext()2.48k ) { |
4514 | 2.44k | // Do substitution on the type of the non-type template parameter. |
4515 | 2.44k | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
4516 | 2.44k | NTTP, Converted, |
4517 | 2.44k | SourceRange(TemplateLoc, RAngleLoc)); |
4518 | 2.44k | if (Inst.isInvalid()) |
4519 | 0 | return true; |
4520 | 2.44k | |
4521 | 2.44k | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
4522 | 2.44k | Converted); |
4523 | 2.44k | NTTPType = SubstType(NTTPType, |
4524 | 2.44k | MultiLevelTemplateArgumentList(TemplateArgs), |
4525 | 2.44k | NTTP->getLocation(), |
4526 | 2.44k | NTTP->getDeclName()); |
4527 | 2.44k | // If that worked, check the non-type template parameter type |
4528 | 2.44k | // for validity. |
4529 | 2.44k | if (!NTTPType.isNull()) |
4530 | 2.42k | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
4531 | 2.42k | NTTP->getLocation()); |
4532 | 2.44k | if (NTTPType.isNull()) |
4533 | 21 | return true; |
4534 | 59.0k | } |
4535 | 59.0k | |
4536 | 59.0k | switch (Arg.getArgument().getKind()) { |
4537 | 0 | case TemplateArgument::Null: |
4538 | 0 | llvm_unreachable("Should never see a NULL template argument here"); |
4539 | 59.0k | |
4540 | 58.8k | case TemplateArgument::Expression: { |
4541 | 58.8k | TemplateArgument Result; |
4542 | 58.8k | ExprResult Res = |
4543 | 58.8k | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
4544 | 58.8k | Result, CTAK); |
4545 | 58.8k | if (Res.isInvalid()) |
4546 | 298 | return true; |
4547 | 58.5k | |
4548 | 58.5k | // If the resulting expression is new, then use it in place of the |
4549 | 58.5k | // old expression in the template argument. |
4550 | 58.5k | if (58.5k Res.get() != Arg.getArgument().getAsExpr()58.5k ) { |
4551 | 6.98k | TemplateArgument TA(Res.get()); |
4552 | 6.98k | Arg = TemplateArgumentLoc(TA, Res.get()); |
4553 | 6.98k | } |
4554 | 58.5k | |
4555 | 58.5k | Converted.push_back(Result); |
4556 | 58.5k | break; |
4557 | 58.5k | } |
4558 | 58.5k | |
4559 | 179 | case TemplateArgument::Declaration: |
4560 | 179 | case TemplateArgument::Integral: |
4561 | 179 | case TemplateArgument::NullPtr: |
4562 | 179 | // We've already checked this template argument, so just copy |
4563 | 179 | // it to the list of converted arguments. |
4564 | 179 | Converted.push_back(Arg.getArgument()); |
4565 | 179 | break; |
4566 | 179 | |
4567 | 4 | case TemplateArgument::Template: |
4568 | 4 | case TemplateArgument::TemplateExpansion: |
4569 | 4 | // We were given a template template argument. It may not be ill-formed; |
4570 | 4 | // see below. |
4571 | 4 | if (DependentTemplateName *DTN |
4572 | 4 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
4573 | 0 | .getAsDependentTemplateName()) { |
4574 | 0 | // We have a template argument such as \c T::template X, which we |
4575 | 0 | // parsed as a template template argument. However, since we now |
4576 | 0 | // know that we need a non-type template argument, convert this |
4577 | 0 | // template name into an expression. |
4578 | 0 |
|
4579 | 0 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
4580 | 0 | Arg.getTemplateNameLoc()); |
4581 | 0 |
|
4582 | 0 | CXXScopeSpec SS; |
4583 | 0 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
4584 | 0 | // FIXME: the template-template arg was a DependentTemplateName, |
4585 | 0 | // so it was provided with a template keyword. However, its source |
4586 | 0 | // location is not stored in the template argument structure. |
4587 | 0 | SourceLocation TemplateKWLoc; |
4588 | 0 | ExprResult E = DependentScopeDeclRefExpr::Create( |
4589 | 0 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
4590 | 0 | nullptr); |
4591 | 0 |
|
4592 | 0 | // If we parsed the template argument as a pack expansion, create a |
4593 | 0 | // pack expansion expression. |
4594 | 0 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion0 ){ |
4595 | 0 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
4596 | 0 | if (E.isInvalid()) |
4597 | 0 | return true; |
4598 | 0 | } |
4599 | 0 |
|
4600 | 0 | TemplateArgument Result; |
4601 | 0 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
4602 | 0 | if (E.isInvalid()) |
4603 | 0 | return true; |
4604 | 0 |
|
4605 | 0 | Converted.push_back(Result); |
4606 | 0 | break; |
4607 | 0 | } |
4608 | 4 | |
4609 | 4 | // We have a template argument that actually does refer to a class |
4610 | 4 | // template, alias template, or template template parameter, and |
4611 | 4 | // therefore cannot be a non-type template argument. |
4612 | 4 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
4613 | 4 | << Arg.getSourceRange(); |
4614 | 4 | |
4615 | 4 | Diag(Param->getLocation(), diag::note_template_param_here); |
4616 | 4 | return true; |
4617 | 4 | |
4618 | 70 | case TemplateArgument::Type: { |
4619 | 70 | // We have a non-type template parameter but the template |
4620 | 70 | // argument is a type. |
4621 | 70 | |
4622 | 70 | // C++ [temp.arg]p2: |
4623 | 70 | // In a template-argument, an ambiguity between a type-id and |
4624 | 70 | // an expression is resolved to a type-id, regardless of the |
4625 | 70 | // form of the corresponding template-parameter. |
4626 | 70 | // |
4627 | 70 | // We warn specifically about this case, since it can be rather |
4628 | 70 | // confusing for users. |
4629 | 70 | QualType T = Arg.getArgument().getAsType(); |
4630 | 70 | SourceRange SR = Arg.getSourceRange(); |
4631 | 70 | if (T->isFunctionType()) |
4632 | 11 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
4633 | 70 | else |
4634 | 59 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
4635 | 70 | Diag(Param->getLocation(), diag::note_template_param_here); |
4636 | 70 | return true; |
4637 | 4 | } |
4638 | 4 | |
4639 | 0 | case TemplateArgument::Pack: |
4640 | 0 | llvm_unreachable("Caller must expand template argument packs"); |
4641 | 58.7k | } |
4642 | 58.7k | |
4643 | 58.7k | return false; |
4644 | 58.7k | } |
4645 | 3.29k | |
4646 | 3.29k | |
4647 | 3.29k | // Check template template parameters. |
4648 | 3.29k | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
4649 | 3.29k | |
4650 | 3.29k | // Substitute into the template parameter list of the template |
4651 | 3.29k | // template parameter, since previously-supplied template arguments |
4652 | 3.29k | // may appear within the template template parameter. |
4653 | 3.29k | { |
4654 | 3.29k | // Set up a template instantiation context. |
4655 | 3.29k | LocalInstantiationScope Scope(*this); |
4656 | 3.29k | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
4657 | 3.29k | TempParm, Converted, |
4658 | 3.29k | SourceRange(TemplateLoc, RAngleLoc)); |
4659 | 3.29k | if (Inst.isInvalid()) |
4660 | 2 | return true; |
4661 | 3.29k | |
4662 | 3.29k | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
4663 | 3.29k | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
4664 | 3.29k | SubstDecl(TempParm, CurContext, |
4665 | 3.29k | MultiLevelTemplateArgumentList(TemplateArgs))); |
4666 | 3.29k | if (!TempParm) |
4667 | 1 | return true; |
4668 | 3.29k | } |
4669 | 3.29k | |
4670 | 3.29k | // C++1z [temp.local]p1: (DR1004) |
4671 | 3.29k | // When [the injected-class-name] is used [...] as a template-argument for |
4672 | 3.29k | // a template template-parameter [...] it refers to the class template |
4673 | 3.29k | // itself. |
4674 | 3.29k | if (3.29k Arg.getArgument().getKind() == TemplateArgument::Type3.29k ) { |
4675 | 29 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
4676 | 29 | Arg.getTypeSourceInfo()->getTypeLoc()); |
4677 | 29 | if (!ConvertedArg.getArgument().isNull()) |
4678 | 25 | Arg = ConvertedArg; |
4679 | 29 | } |
4680 | 3.29k | |
4681 | 3.29k | switch (Arg.getArgument().getKind()) { |
4682 | 0 | case TemplateArgument::Null: |
4683 | 0 | llvm_unreachable("Should never see a NULL template argument here"); |
4684 | 3.29k | |
4685 | 3.28k | case TemplateArgument::Template: |
4686 | 3.28k | case TemplateArgument::TemplateExpansion: |
4687 | 3.28k | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
4688 | 55 | return true; |
4689 | 3.23k | |
4690 | 3.23k | Converted.push_back(Arg.getArgument()); |
4691 | 3.23k | break; |
4692 | 3.23k | |
4693 | 8 | case TemplateArgument::Expression: |
4694 | 8 | case TemplateArgument::Type: |
4695 | 8 | // We have a template template parameter but the template |
4696 | 8 | // argument does not refer to a template. |
4697 | 8 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
4698 | 8 | << getLangOpts().CPlusPlus11; |
4699 | 8 | return true; |
4700 | 8 | |
4701 | 0 | case TemplateArgument::Declaration: |
4702 | 0 | llvm_unreachable("Declaration argument with template template parameter"); |
4703 | 0 | case TemplateArgument::Integral: |
4704 | 0 | llvm_unreachable("Integral argument with template template parameter"); |
4705 | 0 | case TemplateArgument::NullPtr: |
4706 | 0 | llvm_unreachable("Null pointer argument with template template parameter"); |
4707 | 8 | |
4708 | 0 | case TemplateArgument::Pack: |
4709 | 0 | llvm_unreachable("Caller must expand template argument packs"); |
4710 | 3.23k | } |
4711 | 3.23k | |
4712 | 3.23k | return false; |
4713 | 3.23k | } |
4714 | | |
4715 | | /// \brief Diagnose an arity mismatch in the |
4716 | | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
4717 | | SourceLocation TemplateLoc, |
4718 | 80 | TemplateArgumentListInfo &TemplateArgs) { |
4719 | 80 | TemplateParameterList *Params = Template->getTemplateParameters(); |
4720 | 80 | unsigned NumParams = Params->size(); |
4721 | 80 | unsigned NumArgs = TemplateArgs.size(); |
4722 | 80 | |
4723 | 80 | SourceRange Range; |
4724 | 80 | if (NumArgs > NumParams) |
4725 | 10 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
4726 | 10 | TemplateArgs.getRAngleLoc()); |
4727 | 80 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
4728 | 80 | << (NumArgs > NumParams) |
4729 | 80 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(Template)) |
4730 | 80 | << Template << Range; |
4731 | 80 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
4732 | 80 | << Params->getSourceRange(); |
4733 | 80 | return true; |
4734 | 80 | } |
4735 | | |
4736 | | /// \brief Check whether the template parameter is a pack expansion, and if so, |
4737 | | /// determine the number of parameters produced by that expansion. For instance: |
4738 | | /// |
4739 | | /// \code |
4740 | | /// template<typename ...Ts> struct A { |
4741 | | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
4742 | | /// }; |
4743 | | /// \endcode |
4744 | | /// |
4745 | | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
4746 | | /// is not a pack expansion, so returns an empty Optional. |
4747 | 254k | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
4748 | 254k | if (NonTypeTemplateParmDecl *NTTP |
4749 | 57.1k | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
4750 | 57.1k | if (NTTP->isExpandedParameterPack()) |
4751 | 34 | return NTTP->getNumExpansionTypes(); |
4752 | 254k | } |
4753 | 254k | |
4754 | 254k | if (TemplateTemplateParmDecl *254k TTP254k |
4755 | 3.25k | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
4756 | 3.25k | if (TTP->isExpandedParameterPack()) |
4757 | 50 | return TTP->getNumExpansionTemplateParameters(); |
4758 | 254k | } |
4759 | 254k | |
4760 | 254k | return None; |
4761 | 254k | } |
4762 | | |
4763 | | /// Diagnose a missing template argument. |
4764 | | template<typename TemplateParmDecl> |
4765 | | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
4766 | | TemplateDecl *TD, |
4767 | | const TemplateParmDecl *D, |
4768 | 86 | TemplateArgumentListInfo &Args) { |
4769 | 86 | // Dig out the most recent declaration of the template parameter; there may be |
4770 | 86 | // declarations of the template that are more recent than TD. |
4771 | 86 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
4772 | 86 | ->getTemplateParameters() |
4773 | 86 | ->getParam(D->getIndex())); |
4774 | 86 | |
4775 | 86 | // If there's a default argument that's not visible, diagnose that we're |
4776 | 86 | // missing a module import. |
4777 | 86 | llvm::SmallVector<Module*, 8> Modules; |
4778 | 86 | if (D->hasDefaultArgument() && 86 !S.hasVisibleDefaultArgument(D, &Modules)16 ) { |
4779 | 16 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
4780 | 16 | D->getDefaultArgumentLoc(), Modules, |
4781 | 16 | Sema::MissingImportKind::DefaultArgument, |
4782 | 16 | /*Recover*/true); |
4783 | 16 | return true; |
4784 | 16 | } |
4785 | 70 | |
4786 | 70 | // FIXME: If there's a more recent default argument that *is* visible, |
4787 | 70 | // diagnose that it was declared too late. |
4788 | 70 | |
4789 | 70 | return diagnoseArityMismatch(S, TD, Loc, Args); |
4790 | 70 | } SemaTemplate.cpp:bool diagnoseMissingArgument<clang::NonTypeTemplateParmDecl>(clang::Sema&, clang::SourceLocation, clang::TemplateDecl*, clang::NonTypeTemplateParmDecl const*, clang::TemplateArgumentListInfo&) Line | Count | Source | 4768 | 10 | TemplateArgumentListInfo &Args) { | 4769 | 10 | // Dig out the most recent declaration of the template parameter; there may be | 4770 | 10 | // declarations of the template that are more recent than TD. | 4771 | 10 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) | 4772 | 10 | ->getTemplateParameters() | 4773 | 10 | ->getParam(D->getIndex())); | 4774 | 10 | | 4775 | 10 | // If there's a default argument that's not visible, diagnose that we're | 4776 | 10 | // missing a module import. | 4777 | 10 | llvm::SmallVector<Module*, 8> Modules; | 4778 | 10 | if (D->hasDefaultArgument() && 10 !S.hasVisibleDefaultArgument(D, &Modules)0 ) { | 4779 | 0 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), | 4780 | 0 | D->getDefaultArgumentLoc(), Modules, | 4781 | 0 | Sema::MissingImportKind::DefaultArgument, | 4782 | 0 | /*Recover*/true); | 4783 | 0 | return true; | 4784 | 0 | } | 4785 | 10 | | 4786 | 10 | // FIXME: If there's a more recent default argument that *is* visible, | 4787 | 10 | // diagnose that it was declared too late. | 4788 | 10 | | 4789 | 10 | return diagnoseArityMismatch(S, TD, Loc, Args); | 4790 | 10 | } |
SemaTemplate.cpp:bool diagnoseMissingArgument<clang::TemplateTypeParmDecl>(clang::Sema&, clang::SourceLocation, clang::TemplateDecl*, clang::TemplateTypeParmDecl const*, clang::TemplateArgumentListInfo&) Line | Count | Source | 4768 | 75 | TemplateArgumentListInfo &Args) { | 4769 | 75 | // Dig out the most recent declaration of the template parameter; there may be | 4770 | 75 | // declarations of the template that are more recent than TD. | 4771 | 75 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) | 4772 | 75 | ->getTemplateParameters() | 4773 | 75 | ->getParam(D->getIndex())); | 4774 | 75 | | 4775 | 75 | // If there's a default argument that's not visible, diagnose that we're | 4776 | 75 | // missing a module import. | 4777 | 75 | llvm::SmallVector<Module*, 8> Modules; | 4778 | 75 | if (D->hasDefaultArgument() && 75 !S.hasVisibleDefaultArgument(D, &Modules)16 ) { | 4779 | 16 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), | 4780 | 16 | D->getDefaultArgumentLoc(), Modules, | 4781 | 16 | Sema::MissingImportKind::DefaultArgument, | 4782 | 16 | /*Recover*/true); | 4783 | 16 | return true; | 4784 | 16 | } | 4785 | 59 | | 4786 | 59 | // FIXME: If there's a more recent default argument that *is* visible, | 4787 | 59 | // diagnose that it was declared too late. | 4788 | 59 | | 4789 | 59 | return diagnoseArityMismatch(S, TD, Loc, Args); | 4790 | 59 | } |
SemaTemplate.cpp:bool diagnoseMissingArgument<clang::TemplateTemplateParmDecl>(clang::Sema&, clang::SourceLocation, clang::TemplateDecl*, clang::TemplateTemplateParmDecl const*, clang::TemplateArgumentListInfo&) Line | Count | Source | 4768 | 1 | TemplateArgumentListInfo &Args) { | 4769 | 1 | // Dig out the most recent declaration of the template parameter; there may be | 4770 | 1 | // declarations of the template that are more recent than TD. | 4771 | 1 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) | 4772 | 1 | ->getTemplateParameters() | 4773 | 1 | ->getParam(D->getIndex())); | 4774 | 1 | | 4775 | 1 | // If there's a default argument that's not visible, diagnose that we're | 4776 | 1 | // missing a module import. | 4777 | 1 | llvm::SmallVector<Module*, 8> Modules; | 4778 | 1 | if (D->hasDefaultArgument() && 1 !S.hasVisibleDefaultArgument(D, &Modules)0 ) { | 4779 | 0 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), | 4780 | 0 | D->getDefaultArgumentLoc(), Modules, | 4781 | 0 | Sema::MissingImportKind::DefaultArgument, | 4782 | 0 | /*Recover*/true); | 4783 | 0 | return true; | 4784 | 0 | } | 4785 | 1 | | 4786 | 1 | // FIXME: If there's a more recent default argument that *is* visible, | 4787 | 1 | // diagnose that it was declared too late. | 4788 | 1 | | 4789 | 1 | return diagnoseArityMismatch(S, TD, Loc, Args); | 4790 | 1 | } |
|
4791 | | |
4792 | | /// \brief Check that the given template argument list is well-formed |
4793 | | /// for specializing the given template. |
4794 | | bool Sema::CheckTemplateArgumentList( |
4795 | | TemplateDecl *Template, SourceLocation TemplateLoc, |
4796 | | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
4797 | | SmallVectorImpl<TemplateArgument> &Converted, |
4798 | 163k | bool UpdateArgsWithConversions) { |
4799 | 163k | // Make a copy of the template arguments for processing. Only make the |
4800 | 163k | // changes at the end when successful in matching the arguments to the |
4801 | 163k | // template. |
4802 | 163k | TemplateArgumentListInfo NewArgs = TemplateArgs; |
4803 | 163k | |
4804 | 163k | TemplateParameterList *Params = Template->getTemplateParameters(); |
4805 | 163k | |
4806 | 163k | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
4807 | 163k | |
4808 | 163k | // C++ [temp.arg]p1: |
4809 | 163k | // [...] The type and form of each template-argument specified in |
4810 | 163k | // a template-id shall match the type and form specified for the |
4811 | 163k | // corresponding parameter declared by the template in its |
4812 | 163k | // template-parameter-list. |
4813 | 163k | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
4814 | 163k | SmallVector<TemplateArgument, 2> ArgumentPack; |
4815 | 163k | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
4816 | 163k | LocalInstantiationScope InstScope(*this, true); |
4817 | 163k | for (TemplateParameterList::iterator Param = Params->begin(), |
4818 | 163k | ParamEnd = Params->end(); |
4819 | 414k | Param != ParamEnd414k ; /* increment in loop */) { |
4820 | 253k | // If we have an expanded parameter pack, make sure we don't have too |
4821 | 253k | // many arguments. |
4822 | 253k | if (Optional<unsigned> Expansions253k = getExpandedPackSize(*Param)) { |
4823 | 83 | if (*Expansions == ArgumentPack.size()83 ) { |
4824 | 19 | // We're done with this parameter pack. Pack up its arguments and add |
4825 | 19 | // them to the list. |
4826 | 19 | Converted.push_back( |
4827 | 19 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
4828 | 19 | ArgumentPack.clear(); |
4829 | 19 | |
4830 | 19 | // This argument is assigned to the next parameter. |
4831 | 19 | ++Param; |
4832 | 19 | continue; |
4833 | 64 | } else if (64 ArgIdx == NumArgs && 64 !PartialTemplateArgs3 ) { |
4834 | 3 | // Not enough arguments for this parameter pack. |
4835 | 3 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
4836 | 3 | << false |
4837 | 3 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
4838 | 3 | << Template; |
4839 | 3 | Diag(Template->getLocation(), diag::note_template_decl_here) |
4840 | 3 | << Params->getSourceRange(); |
4841 | 3 | return true; |
4842 | 3 | } |
4843 | 253k | } |
4844 | 253k | |
4845 | 253k | if (253k ArgIdx < NumArgs253k ) { |
4846 | 246k | // Check the template argument we were given. |
4847 | 246k | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
4848 | 246k | TemplateLoc, RAngleLoc, |
4849 | 246k | ArgumentPack.size(), Converted)) |
4850 | 519 | return true; |
4851 | 246k | |
4852 | 246k | bool PackExpansionIntoNonPack = |
4853 | 246k | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
4854 | 788 | (!(*Param)->isTemplateParameterPack() || 788 getExpandedPackSize(*Param)717 ); |
4855 | 246k | if (PackExpansionIntoNonPack && 246k isa<TypeAliasTemplateDecl>(Template)72 ) { |
4856 | 4 | // Core issue 1430: we have a pack expansion as an argument to an |
4857 | 4 | // alias template, and it's not part of a parameter pack. This |
4858 | 4 | // can't be canonicalized, so reject it now. |
4859 | 4 | Diag(NewArgs[ArgIdx].getLocation(), |
4860 | 4 | diag::err_alias_template_expansion_into_fixed_list) |
4861 | 4 | << NewArgs[ArgIdx].getSourceRange(); |
4862 | 4 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
4863 | 4 | return true; |
4864 | 4 | } |
4865 | 246k | |
4866 | 246k | // We're now done with this argument. |
4867 | 246k | ++ArgIdx; |
4868 | 246k | |
4869 | 246k | if ((*Param)->isTemplateParameterPack()246k ) { |
4870 | 7.60k | // The template parameter was a template parameter pack, so take the |
4871 | 7.60k | // deduced argument and place it on the argument pack. Note that we |
4872 | 7.60k | // stay on the same template parameter so that we can deduce more |
4873 | 7.60k | // arguments. |
4874 | 7.60k | ArgumentPack.push_back(Converted.pop_back_val()); |
4875 | 246k | } else { |
4876 | 238k | // Move to the next template parameter. |
4877 | 238k | ++Param; |
4878 | 238k | } |
4879 | 246k | |
4880 | 246k | // If we just saw a pack expansion into a non-pack, then directly convert |
4881 | 246k | // the remaining arguments, because we don't know what parameters they'll |
4882 | 246k | // match up with. |
4883 | 246k | if (PackExpansionIntoNonPack246k ) { |
4884 | 68 | if (!ArgumentPack.empty()68 ) { |
4885 | 1 | // If we were part way through filling in an expanded parameter pack, |
4886 | 1 | // fall back to just producing individual arguments. |
4887 | 1 | Converted.insert(Converted.end(), |
4888 | 1 | ArgumentPack.begin(), ArgumentPack.end()); |
4889 | 1 | ArgumentPack.clear(); |
4890 | 1 | } |
4891 | 68 | |
4892 | 76 | while (ArgIdx < NumArgs76 ) { |
4893 | 8 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
4894 | 8 | ++ArgIdx; |
4895 | 8 | } |
4896 | 68 | |
4897 | 68 | return false; |
4898 | 68 | } |
4899 | 246k | |
4900 | 246k | continue; |
4901 | 246k | } |
4902 | 6.50k | |
4903 | 6.50k | // If we're checking a partial template argument list, we're done. |
4904 | 6.50k | if (6.50k PartialTemplateArgs6.50k ) { |
4905 | 1.46k | if ((*Param)->isTemplateParameterPack() && 1.46k !ArgumentPack.empty()329 ) |
4906 | 280 | Converted.push_back( |
4907 | 280 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
4908 | 1.46k | |
4909 | 1.46k | return false; |
4910 | 1.46k | } |
4911 | 5.04k | |
4912 | 5.04k | // If we have a template parameter pack with no more corresponding |
4913 | 5.04k | // arguments, just break out now and we'll fill in the argument pack below. |
4914 | 5.04k | if (5.04k (*Param)->isTemplateParameterPack()5.04k ) { |
4915 | 2.94k | assert(!getExpandedPackSize(*Param) && |
4916 | 2.94k | "Should have dealt with this already"); |
4917 | 2.94k | |
4918 | 2.94k | // A non-expanded parameter pack before the end of the parameter list |
4919 | 2.94k | // only occurs for an ill-formed template parameter list, unless we've |
4920 | 2.94k | // got a partial argument list for a function template, so just bail out. |
4921 | 2.94k | if (Param + 1 != ParamEnd) |
4922 | 1 | return true; |
4923 | 2.94k | |
4924 | 2.94k | Converted.push_back( |
4925 | 2.94k | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
4926 | 2.94k | ArgumentPack.clear(); |
4927 | 2.94k | |
4928 | 2.94k | ++Param; |
4929 | 2.94k | continue; |
4930 | 2.94k | } |
4931 | 2.10k | |
4932 | 2.10k | // Check whether we have a default argument. |
4933 | 2.10k | TemplateArgumentLoc Arg; |
4934 | 2.10k | |
4935 | 2.10k | // Retrieve the default template argument from the template |
4936 | 2.10k | // parameter. For each kind of template parameter, we substitute the |
4937 | 2.10k | // template arguments provided thus far and any "outer" template arguments |
4938 | 2.10k | // (when the template parameter was part of a nested template) into |
4939 | 2.10k | // the default argument. |
4940 | 2.10k | if (TemplateTypeParmDecl *TTP2.10k = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
4941 | 1.33k | if (!hasVisibleDefaultArgument(TTP)) |
4942 | 75 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
4943 | 75 | NewArgs); |
4944 | 1.26k | |
4945 | 1.26k | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
4946 | 1.26k | Template, |
4947 | 1.26k | TemplateLoc, |
4948 | 1.26k | RAngleLoc, |
4949 | 1.26k | TTP, |
4950 | 1.26k | Converted); |
4951 | 1.26k | if (!ArgType) |
4952 | 108 | return true; |
4953 | 1.15k | |
4954 | 1.15k | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
4955 | 1.15k | ArgType); |
4956 | 2.10k | } else if (NonTypeTemplateParmDecl *763 NTTP763 |
4957 | 650 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
4958 | 650 | if (!hasVisibleDefaultArgument(NTTP)) |
4959 | 10 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
4960 | 10 | NewArgs); |
4961 | 640 | |
4962 | 640 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
4963 | 640 | TemplateLoc, |
4964 | 640 | RAngleLoc, |
4965 | 640 | NTTP, |
4966 | 640 | Converted); |
4967 | 640 | if (E.isInvalid()) |
4968 | 184 | return true; |
4969 | 456 | |
4970 | 456 | Expr *Ex = E.getAs<Expr>(); |
4971 | 456 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
4972 | 763 | } else { |
4973 | 113 | TemplateTemplateParmDecl *TempParm |
4974 | 113 | = cast<TemplateTemplateParmDecl>(*Param); |
4975 | 113 | |
4976 | 113 | if (!hasVisibleDefaultArgument(TempParm)) |
4977 | 1 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
4978 | 1 | NewArgs); |
4979 | 112 | |
4980 | 112 | NestedNameSpecifierLoc QualifierLoc; |
4981 | 112 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
4982 | 112 | TemplateLoc, |
4983 | 112 | RAngleLoc, |
4984 | 112 | TempParm, |
4985 | 112 | Converted, |
4986 | 112 | QualifierLoc); |
4987 | 112 | if (Name.isNull()) |
4988 | 0 | return true; |
4989 | 112 | |
4990 | 112 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
4991 | 112 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
4992 | 112 | } |
4993 | 2.10k | |
4994 | 2.10k | // Introduce an instantiation record that describes where we are using |
4995 | 2.10k | // the default template argument. We're not actually instantiating a |
4996 | 2.10k | // template here, we just create this object to put a note into the |
4997 | 2.10k | // context stack. |
4998 | 1.72k | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
4999 | 1.72k | SourceRange(TemplateLoc, RAngleLoc)); |
5000 | 1.72k | if (Inst.isInvalid()) |
5001 | 368 | return true; |
5002 | 1.35k | |
5003 | 1.35k | // Check the default template argument. |
5004 | 1.35k | if (1.35k CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
5005 | 1.35k | RAngleLoc, 0, Converted)) |
5006 | 4 | return true; |
5007 | 1.35k | |
5008 | 1.35k | // Core issue 150 (assumed resolution): if this is a template template |
5009 | 1.35k | // parameter, keep track of the default template arguments from the |
5010 | 1.35k | // template definition. |
5011 | 1.35k | if (1.35k isTemplateTemplateParameter1.35k ) |
5012 | 29 | NewArgs.addArgument(Arg); |
5013 | 253k | |
5014 | 253k | // Move to the next template parameter and argument. |
5015 | 253k | ++Param; |
5016 | 253k | ++ArgIdx; |
5017 | 253k | } |
5018 | 163k | |
5019 | 163k | // If we're performing a partial argument substitution, allow any trailing |
5020 | 163k | // pack expansions; they might be empty. This can happen even if |
5021 | 163k | // PartialTemplateArgs is false (the list of arguments is complete but |
5022 | 163k | // still dependent). |
5023 | 160k | if (160k ArgIdx < NumArgs && 160k CurrentInstantiationScope11 && |
5024 | 160k | CurrentInstantiationScope->getPartiallySubstitutedPack()11 ) { |
5025 | 3 | while (ArgIdx < NumArgs && 3 NewArgs[ArgIdx].getArgument().isPackExpansion()2 ) |
5026 | 1 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
5027 | 2 | } |
5028 | 160k | |
5029 | 160k | // If we have any leftover arguments, then there were too many arguments. |
5030 | 160k | // Complain and fail. |
5031 | 160k | if (ArgIdx < NumArgs) |
5032 | 10 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
5033 | 160k | |
5034 | 160k | // No problems found with the new argument list, propagate changes back |
5035 | 160k | // to caller. |
5036 | 160k | if (160k UpdateArgsWithConversions160k ) |
5037 | 150k | TemplateArgs = std::move(NewArgs); |
5038 | 163k | |
5039 | 163k | return false; |
5040 | 163k | } |
5041 | | |
5042 | | namespace { |
5043 | | class UnnamedLocalNoLinkageFinder |
5044 | | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
5045 | | { |
5046 | | Sema &S; |
5047 | | SourceRange SR; |
5048 | | |
5049 | | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
5050 | | |
5051 | | public: |
5052 | 96.6k | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
5053 | | |
5054 | 110k | bool Visit(QualType T) { |
5055 | 110k | return T.isNull() ? false0 : inherited::Visit(T.getTypePtr())110k ; |
5056 | 110k | } |
5057 | | |
5058 | | #define TYPE(Class, Parent) \ |
5059 | | bool Visit##Class##Type(const Class##Type *); |
5060 | | #define ABSTRACT_TYPE(Class, Parent) \ |
5061 | 0 | bool Visit##Class##Type(const Class##Type *) { return false; } Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitReferenceType(clang::ReferenceType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitDeducedType(clang::DeducedType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitTagType(clang::TagType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitFunctionType(clang::FunctionType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitArrayType(clang::ArrayType const*) |
5062 | | #define NON_CANONICAL_TYPE(Class, Parent) \ |
5063 | 0 | bool Visit##Class##Type(const Class##Type *) { return false; } Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitAdjustedType(clang::AdjustedType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitParenType(clang::ParenType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitTypedefType(clang::TypedefType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitObjCTypeParamType(clang::ObjCTypeParamType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitDecayedType(clang::DecayedType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitElaboratedType(clang::ElaboratedType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitAttributedType(clang::AttributedType const*) Unexecuted instantiation: SemaTemplate.cpp:(anonymous namespace)::UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmType(clang::SubstTemplateTypeParmType const*) |
5064 | | #include "clang/AST/TypeNodes.def" |
5065 | | |
5066 | | bool VisitTagDecl(const TagDecl *Tag); |
5067 | | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
5068 | | }; |
5069 | | } // end anonymous namespace |
5070 | | |
5071 | 38.1k | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
5072 | 38.1k | return false; |
5073 | 38.1k | } |
5074 | | |
5075 | 4 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
5076 | 4 | return Visit(T->getElementType()); |
5077 | 4 | } |
5078 | | |
5079 | 6.64k | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
5080 | 6.64k | return Visit(T->getPointeeType()); |
5081 | 6.64k | } |
5082 | | |
5083 | | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
5084 | 10 | const BlockPointerType* T) { |
5085 | 10 | return Visit(T->getPointeeType()); |
5086 | 10 | } |
5087 | | |
5088 | | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
5089 | 2.46k | const LValueReferenceType* T) { |
5090 | 2.46k | return Visit(T->getPointeeType()); |
5091 | 2.46k | } |
5092 | | |
5093 | | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
5094 | 220 | const RValueReferenceType* T) { |
5095 | 220 | return Visit(T->getPointeeType()); |
5096 | 220 | } |
5097 | | |
5098 | | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
5099 | 593 | const MemberPointerType* T) { |
5100 | 589 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
5101 | 593 | } |
5102 | | |
5103 | | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
5104 | 274 | const ConstantArrayType* T) { |
5105 | 274 | return Visit(T->getElementType()); |
5106 | 274 | } |
5107 | | |
5108 | | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
5109 | 41 | const IncompleteArrayType* T) { |
5110 | 41 | return Visit(T->getElementType()); |
5111 | 41 | } |
5112 | | |
5113 | | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
5114 | 0 | const VariableArrayType* T) { |
5115 | 0 | return Visit(T->getElementType()); |
5116 | 0 | } |
5117 | | |
5118 | | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
5119 | 231 | const DependentSizedArrayType* T) { |
5120 | 231 | return Visit(T->getElementType()); |
5121 | 231 | } |
5122 | | |
5123 | | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
5124 | 0 | const DependentSizedExtVectorType* T) { |
5125 | 0 | return Visit(T->getElementType()); |
5126 | 0 | } |
5127 | | |
5128 | 4 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
5129 | 4 | return Visit(T->getElementType()); |
5130 | 4 | } |
5131 | | |
5132 | 4 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
5133 | 4 | return Visit(T->getElementType()); |
5134 | 4 | } |
5135 | | |
5136 | | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
5137 | 1.03k | const FunctionProtoType* T) { |
5138 | 565 | for (const auto &A : T->param_types()) { |
5139 | 565 | if (Visit(A)) |
5140 | 5 | return true; |
5141 | 1.02k | } |
5142 | 1.02k | |
5143 | 1.02k | return Visit(T->getReturnType()); |
5144 | 1.02k | } |
5145 | | |
5146 | | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
5147 | 0 | const FunctionNoProtoType* T) { |
5148 | 0 | return Visit(T->getReturnType()); |
5149 | 0 | } |
5150 | | |
5151 | | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
5152 | 0 | const UnresolvedUsingType*) { |
5153 | 0 | return false; |
5154 | 0 | } |
5155 | | |
5156 | 144 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
5157 | 144 | return false; |
5158 | 144 | } |
5159 | | |
5160 | 0 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
5161 | 0 | return Visit(T->getUnderlyingType()); |
5162 | 0 | } |
5163 | | |
5164 | 84 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
5165 | 84 | return false; |
5166 | 84 | } |
5167 | | |
5168 | | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
5169 | 0 | const UnaryTransformType*) { |
5170 | 0 | return false; |
5171 | 0 | } |
5172 | | |
5173 | 0 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
5174 | 0 | return Visit(T->getDeducedType()); |
5175 | 0 | } |
5176 | | |
5177 | | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
5178 | 0 | const DeducedTemplateSpecializationType *T) { |
5179 | 0 | return Visit(T->getDeducedType()); |
5180 | 0 | } |
5181 | | |
5182 | 40.4k | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
5183 | 40.4k | return VisitTagDecl(T->getDecl()); |
5184 | 40.4k | } |
5185 | | |
5186 | 385 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
5187 | 385 | return VisitTagDecl(T->getDecl()); |
5188 | 385 | } |
5189 | | |
5190 | | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
5191 | 16.3k | const TemplateTypeParmType*) { |
5192 | 16.3k | return false; |
5193 | 16.3k | } |
5194 | | |
5195 | | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
5196 | 24 | const SubstTemplateTypeParmPackType *) { |
5197 | 24 | return false; |
5198 | 24 | } |
5199 | | |
5200 | | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
5201 | 1.33k | const TemplateSpecializationType*) { |
5202 | 1.33k | return false; |
5203 | 1.33k | } |
5204 | | |
5205 | | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
5206 | 403 | const InjectedClassNameType* T) { |
5207 | 403 | return VisitTagDecl(T->getDecl()); |
5208 | 403 | } |
5209 | | |
5210 | | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
5211 | 742 | const DependentNameType* T) { |
5212 | 742 | return VisitNestedNameSpecifier(T->getQualifier()); |
5213 | 742 | } |
5214 | | |
5215 | | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
5216 | 26 | const DependentTemplateSpecializationType* T) { |
5217 | 26 | return VisitNestedNameSpecifier(T->getQualifier()); |
5218 | 26 | } |
5219 | | |
5220 | | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
5221 | 693 | const PackExpansionType* T) { |
5222 | 693 | return Visit(T->getPattern()); |
5223 | 693 | } |
5224 | | |
5225 | 1 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
5226 | 1 | return false; |
5227 | 1 | } |
5228 | | |
5229 | | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
5230 | 14 | const ObjCInterfaceType *) { |
5231 | 14 | return false; |
5232 | 14 | } |
5233 | | |
5234 | | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
5235 | 487 | const ObjCObjectPointerType *) { |
5236 | 487 | return false; |
5237 | 487 | } |
5238 | | |
5239 | 0 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
5240 | 0 | return Visit(T->getValueType()); |
5241 | 0 | } |
5242 | | |
5243 | 0 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
5244 | 0 | return false; |
5245 | 0 | } |
5246 | | |
5247 | 41.2k | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
5248 | 41.2k | if (Tag->getDeclContext()->isFunctionOrMethod()41.2k ) { |
5249 | 522 | S.Diag(SR.getBegin(), |
5250 | 522 | S.getLangOpts().CPlusPlus11 ? |
5251 | 494 | diag::warn_cxx98_compat_template_arg_local_type : |
5252 | 28 | diag::ext_template_arg_local_type) |
5253 | 522 | << S.Context.getTypeDeclType(Tag) << SR; |
5254 | 522 | return true; |
5255 | 522 | } |
5256 | 40.7k | |
5257 | 40.7k | if (40.7k !Tag->hasNameForLinkage()40.7k ) { |
5258 | 152 | S.Diag(SR.getBegin(), |
5259 | 152 | S.getLangOpts().CPlusPlus11 ? |
5260 | 96 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
5261 | 56 | diag::ext_template_arg_unnamed_type) << SR; |
5262 | 152 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
5263 | 152 | return true; |
5264 | 152 | } |
5265 | 40.5k | |
5266 | 40.5k | return false; |
5267 | 40.5k | } |
5268 | | |
5269 | | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
5270 | 770 | NestedNameSpecifier *NNS) { |
5271 | 770 | if (NNS->getPrefix() && 770 VisitNestedNameSpecifier(NNS->getPrefix())2 ) |
5272 | 0 | return true; |
5273 | 770 | |
5274 | 770 | switch (NNS->getKind()) { |
5275 | 4 | case NestedNameSpecifier::Identifier: |
5276 | 4 | case NestedNameSpecifier::Namespace: |
5277 | 4 | case NestedNameSpecifier::NamespaceAlias: |
5278 | 4 | case NestedNameSpecifier::Global: |
5279 | 4 | case NestedNameSpecifier::Super: |
5280 | 4 | return false; |
5281 | 4 | |
5282 | 766 | case NestedNameSpecifier::TypeSpec: |
5283 | 766 | case NestedNameSpecifier::TypeSpecWithTemplate: |
5284 | 766 | return Visit(QualType(NNS->getAsType(), 0)); |
5285 | 0 | } |
5286 | 0 | llvm_unreachable0 ("Invalid NestedNameSpecifier::Kind!"); |
5287 | 0 | } |
5288 | | |
5289 | | /// \brief Check a template argument against its corresponding |
5290 | | /// template type parameter. |
5291 | | /// |
5292 | | /// This routine implements the semantics of C++ [temp.arg.type]. It |
5293 | | /// returns true if an error occurred, and false otherwise. |
5294 | | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
5295 | 236k | TypeSourceInfo *ArgInfo) { |
5296 | 236k | assert(ArgInfo && "invalid TypeSourceInfo"); |
5297 | 236k | QualType Arg = ArgInfo->getType(); |
5298 | 236k | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
5299 | 236k | |
5300 | 236k | if (Arg->isVariablyModifiedType()236k ) { |
5301 | 4 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
5302 | 236k | } else if (236k Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)236k ) { |
5303 | 0 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
5304 | 0 | } |
5305 | 236k | |
5306 | 236k | // C++03 [temp.arg.type]p2: |
5307 | 236k | // A local type, a type with no linkage, an unnamed type or a type |
5308 | 236k | // compounded from any of these types shall not be used as a |
5309 | 236k | // template-argument for a template type-parameter. |
5310 | 236k | // |
5311 | 236k | // C++11 allows these, and even in C++03 we allow them as an extension with |
5312 | 236k | // a warning. |
5313 | 236k | if (236k LangOpts.CPlusPlus11 || 236k Arg->hasUnnamedOrLocalType()140k ) { |
5314 | 96.6k | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
5315 | 96.6k | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
5316 | 96.6k | } |
5317 | 236k | |
5318 | 236k | return false; |
5319 | 236k | } |
5320 | | |
5321 | | enum NullPointerValueKind { |
5322 | | NPV_NotNullPointer, |
5323 | | NPV_NullPointer, |
5324 | | NPV_Error |
5325 | | }; |
5326 | | |
5327 | | /// \brief Determine whether the given template argument is a null pointer |
5328 | | /// value of the appropriate type. |
5329 | | static NullPointerValueKind |
5330 | | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
5331 | | QualType ParamType, Expr *Arg, |
5332 | 1.17k | Decl *Entity = nullptr) { |
5333 | 1.17k | if (Arg->isValueDependent() || 1.17k Arg->isTypeDependent()1.09k ) |
5334 | 81 | return NPV_NotNullPointer; |
5335 | 1.09k | |
5336 | 1.09k | // dllimport'd entities aren't constant but are available inside of template |
5337 | 1.09k | // arguments. |
5338 | 1.09k | if (1.09k Entity && 1.09k Entity->hasAttr<DLLImportAttr>()872 ) |
5339 | 5 | return NPV_NotNullPointer; |
5340 | 1.09k | |
5341 | 1.09k | if (1.09k !S.isCompleteType(Arg->getExprLoc(), ParamType)1.09k ) |
5342 | 0 | llvm_unreachable( |
5343 | 1.09k | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
5344 | 1.09k | |
5345 | 1.09k | if (1.09k !S.getLangOpts().CPlusPlus111.09k ) |
5346 | 208 | return NPV_NotNullPointer; |
5347 | 885 | |
5348 | 885 | // Determine whether we have a constant expression. |
5349 | 885 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
5350 | 885 | if (ArgRV.isInvalid()) |
5351 | 0 | return NPV_Error; |
5352 | 885 | Arg = ArgRV.get(); |
5353 | 885 | |
5354 | 885 | Expr::EvalResult EvalResult; |
5355 | 885 | SmallVector<PartialDiagnosticAt, 8> Notes; |
5356 | 885 | EvalResult.Diag = &Notes; |
5357 | 885 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
5358 | 885 | EvalResult.HasSideEffects868 ) { |
5359 | 17 | SourceLocation DiagLoc = Arg->getExprLoc(); |
5360 | 17 | |
5361 | 17 | // If our only note is the usual "invalid subexpression" note, just point |
5362 | 17 | // the caret at its location rather than producing an essentially |
5363 | 17 | // redundant note. |
5364 | 17 | if (Notes.size() == 1 && 17 Notes[0].second.getDiagID() == |
5365 | 17 | diag::note_invalid_subexpr_in_const_expr) { |
5366 | 1 | DiagLoc = Notes[0].first; |
5367 | 1 | Notes.clear(); |
5368 | 1 | } |
5369 | 17 | |
5370 | 17 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
5371 | 17 | << Arg->getType() << Arg->getSourceRange(); |
5372 | 45 | for (unsigned I = 0, N = Notes.size(); I != N45 ; ++I28 ) |
5373 | 28 | S.Diag(Notes[I].first, Notes[I].second); |
5374 | 17 | |
5375 | 17 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5376 | 17 | return NPV_Error; |
5377 | 17 | } |
5378 | 868 | |
5379 | 868 | // C++11 [temp.arg.nontype]p1: |
5380 | 868 | // - an address constant expression of type std::nullptr_t |
5381 | 868 | if (868 Arg->getType()->isNullPtrType()868 ) |
5382 | 140 | return NPV_NullPointer; |
5383 | 728 | |
5384 | 728 | // - a constant expression that evaluates to a null pointer value (4.10); or |
5385 | 728 | // - a constant expression that evaluates to a null member pointer value |
5386 | 728 | // (4.11); or |
5387 | 728 | if (728 (EvalResult.Val.isLValue() && 728 !EvalResult.Val.getLValueBase()590 ) || |
5388 | 636 | (EvalResult.Val.isMemberPointer() && |
5389 | 728 | !EvalResult.Val.getMemberPointerDecl()120 )) { |
5390 | 104 | // If our expression has an appropriate type, we've succeeded. |
5391 | 104 | bool ObjCLifetimeConversion; |
5392 | 104 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
5393 | 9 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
5394 | 9 | ObjCLifetimeConversion)) |
5395 | 95 | return NPV_NullPointer; |
5396 | 9 | |
5397 | 9 | // The types didn't match, but we know we got a null pointer; complain, |
5398 | 9 | // then recover as if the types were correct. |
5399 | 9 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
5400 | 9 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
5401 | 9 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5402 | 9 | return NPV_NullPointer; |
5403 | 9 | } |
5404 | 624 | |
5405 | 624 | // If we don't have a null pointer value, but we do have a NULL pointer |
5406 | 624 | // constant, suggest a cast to the appropriate type. |
5407 | 624 | if (624 Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)624 ) { |
5408 | 15 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
5409 | 15 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
5410 | 15 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
5411 | 15 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
5412 | 15 | ")"); |
5413 | 15 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5414 | 15 | return NPV_NullPointer; |
5415 | 15 | } |
5416 | 609 | |
5417 | 609 | // FIXME: If we ever want to support general, address-constant expressions |
5418 | 609 | // as non-type template arguments, we should return the ExprResult here to |
5419 | 609 | // be interpreted by the caller. |
5420 | 609 | return NPV_NotNullPointer; |
5421 | 609 | } |
5422 | | |
5423 | | /// \brief Checks whether the given template argument is compatible with its |
5424 | | /// template parameter. |
5425 | | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
5426 | | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
5427 | 937 | Expr *Arg, QualType ArgType) { |
5428 | 937 | bool ObjCLifetimeConversion; |
5429 | 937 | if (ParamType->isPointerType() && |
5430 | 636 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
5431 | 465 | S.IsQualificationConversion(ArgType, ParamType, false, |
5432 | 937 | ObjCLifetimeConversion)) { |
5433 | 34 | // For pointer-to-object types, qualification conversions are |
5434 | 34 | // permitted. |
5435 | 937 | } else { |
5436 | 903 | if (const ReferenceType *ParamRef903 = ParamType->getAs<ReferenceType>()) { |
5437 | 301 | if (!ParamRef->getPointeeType()->isFunctionType()301 ) { |
5438 | 247 | // C++ [temp.arg.nontype]p5b3: |
5439 | 247 | // For a non-type template-parameter of type reference to |
5440 | 247 | // object, no conversions apply. The type referred to by the |
5441 | 247 | // reference may be more cv-qualified than the (otherwise |
5442 | 247 | // identical) type of the template- argument. The |
5443 | 247 | // template-parameter is bound directly to the |
5444 | 247 | // template-argument, which shall be an lvalue. |
5445 | 247 | |
5446 | 247 | // FIXME: Other qualifiers? |
5447 | 247 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
5448 | 247 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
5449 | 247 | |
5450 | 247 | if ((ParamQuals | ArgQuals) != ParamQuals247 ) { |
5451 | 33 | S.Diag(Arg->getLocStart(), |
5452 | 33 | diag::err_template_arg_ref_bind_ignores_quals) |
5453 | 33 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
5454 | 33 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5455 | 33 | return true; |
5456 | 33 | } |
5457 | 870 | } |
5458 | 301 | } |
5459 | 870 | |
5460 | 870 | // At this point, the template argument refers to an object or |
5461 | 870 | // function with external linkage. We now need to check whether the |
5462 | 870 | // argument and parameter types are compatible. |
5463 | 870 | if (870 !S.Context.hasSameUnqualifiedType(ArgType, |
5464 | 870 | ParamType.getNonReferenceType())) { |
5465 | 33 | // We can't perform this conversion or binding. |
5466 | 33 | if (ParamType->isReferenceType()) |
5467 | 14 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
5468 | 14 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
5469 | 33 | else |
5470 | 19 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
5471 | 19 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
5472 | 33 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5473 | 33 | return true; |
5474 | 33 | } |
5475 | 871 | } |
5476 | 871 | |
5477 | 871 | return false; |
5478 | 871 | } |
5479 | | |
5480 | | /// \brief Checks whether the given template argument is the address |
5481 | | /// of an object or function according to C++ [temp.arg.nontype]p1. |
5482 | | static bool |
5483 | | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
5484 | | NonTypeTemplateParmDecl *Param, |
5485 | | QualType ParamType, |
5486 | | Expr *ArgIn, |
5487 | 1.31k | TemplateArgument &Converted) { |
5488 | 1.31k | bool Invalid = false; |
5489 | 1.31k | Expr *Arg = ArgIn; |
5490 | 1.31k | QualType ArgType = Arg->getType(); |
5491 | 1.31k | |
5492 | 1.31k | bool AddressTaken = false; |
5493 | 1.31k | SourceLocation AddrOpLoc; |
5494 | 1.31k | if (S.getLangOpts().MicrosoftExt1.31k ) { |
5495 | 43 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
5496 | 43 | // dereference and address-of operators. |
5497 | 43 | Arg = Arg->IgnoreParenCasts(); |
5498 | 43 | |
5499 | 43 | bool ExtWarnMSTemplateArg = false; |
5500 | 43 | UnaryOperatorKind FirstOpKind; |
5501 | 43 | SourceLocation FirstOpLoc; |
5502 | 64 | while (UnaryOperator *UnOp64 = dyn_cast<UnaryOperator>(Arg)) { |
5503 | 21 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
5504 | 21 | if (UnOpKind == UO_Deref) |
5505 | 1 | ExtWarnMSTemplateArg = true; |
5506 | 21 | if (UnOpKind == UO_AddrOf || 21 UnOpKind == UO_Deref1 ) { |
5507 | 21 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
5508 | 21 | if (!AddrOpLoc.isValid()21 ) { |
5509 | 21 | FirstOpKind = UnOpKind; |
5510 | 21 | FirstOpLoc = UnOp->getOperatorLoc(); |
5511 | 21 | } |
5512 | 21 | } else |
5513 | 0 | break; |
5514 | 21 | } |
5515 | 43 | if (FirstOpLoc.isValid()43 ) { |
5516 | 19 | if (ExtWarnMSTemplateArg) |
5517 | 1 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
5518 | 1 | << ArgIn->getSourceRange(); |
5519 | 19 | |
5520 | 19 | if (FirstOpKind == UO_AddrOf) |
5521 | 19 | AddressTaken = true; |
5522 | 0 | else if (0 Arg->getType()->isPointerType()0 ) { |
5523 | 0 | // We cannot let pointers get dereferenced here, that is obviously not a |
5524 | 0 | // constant expression. |
5525 | 0 | assert(FirstOpKind == UO_Deref); |
5526 | 0 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
5527 | 0 | << Arg->getSourceRange(); |
5528 | 0 | } |
5529 | 19 | } |
5530 | 1.31k | } else { |
5531 | 1.26k | // See through any implicit casts we added to fix the type. |
5532 | 1.26k | Arg = Arg->IgnoreImpCasts(); |
5533 | 1.26k | |
5534 | 1.26k | // C++ [temp.arg.nontype]p1: |
5535 | 1.26k | // |
5536 | 1.26k | // A template-argument for a non-type, non-template |
5537 | 1.26k | // template-parameter shall be one of: [...] |
5538 | 1.26k | // |
5539 | 1.26k | // -- the address of an object or function with external |
5540 | 1.26k | // linkage, including function templates and function |
5541 | 1.26k | // template-ids but excluding non-static class members, |
5542 | 1.26k | // expressed as & id-expression where the & is optional if |
5543 | 1.26k | // the name refers to a function or array, or if the |
5544 | 1.26k | // corresponding template-parameter is a reference; or |
5545 | 1.26k | |
5546 | 1.26k | // In C++98/03 mode, give an extension warning on any extra parentheses. |
5547 | 1.26k | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
5548 | 1.26k | bool ExtraParens = false; |
5549 | 1.29k | while (ParenExpr *Parens1.29k = dyn_cast<ParenExpr>(Arg)) { |
5550 | 29 | if (!Invalid && 29 !ExtraParens29 ) { |
5551 | 25 | S.Diag(Arg->getLocStart(), |
5552 | 25 | S.getLangOpts().CPlusPlus11 |
5553 | 23 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
5554 | 2 | : diag::ext_template_arg_extra_parens) |
5555 | 25 | << Arg->getSourceRange(); |
5556 | 25 | ExtraParens = true; |
5557 | 25 | } |
5558 | 29 | |
5559 | 29 | Arg = Parens->getSubExpr(); |
5560 | 29 | } |
5561 | 1.26k | |
5562 | 1.29k | while (SubstNonTypeTemplateParmExpr *subst = |
5563 | 1.26k | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
5564 | 23 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
5565 | 1.26k | |
5566 | 1.26k | if (UnaryOperator *UnOp1.26k = dyn_cast<UnaryOperator>(Arg)) { |
5567 | 523 | if (UnOp->getOpcode() == UO_AddrOf523 ) { |
5568 | 522 | Arg = UnOp->getSubExpr(); |
5569 | 522 | AddressTaken = true; |
5570 | 522 | AddrOpLoc = UnOp->getOperatorLoc(); |
5571 | 522 | } |
5572 | 523 | } |
5573 | 1.26k | |
5574 | 1.26k | while (SubstNonTypeTemplateParmExpr *subst = |
5575 | 1.26k | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
5576 | 0 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
5577 | 1.26k | } |
5578 | 1.31k | |
5579 | 1.31k | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
5580 | 1.31k | ValueDecl *Entity = DRE ? DRE->getDecl()1.14k : nullptr170 ; |
5581 | 1.31k | |
5582 | 1.31k | // If our parameter has pointer type, check for a null template value. |
5583 | 1.31k | if (ParamType->isPointerType() || 1.31k ParamType->isNullPtrType()346 ) { |
5584 | 965 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
5585 | 965 | Entity)) { |
5586 | 188 | case NPV_NullPointer: |
5587 | 188 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
5588 | 188 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
5589 | 188 | /*isNullPtr=*/true); |
5590 | 188 | return false; |
5591 | 965 | |
5592 | 17 | case NPV_Error: |
5593 | 17 | return true; |
5594 | 965 | |
5595 | 760 | case NPV_NotNullPointer: |
5596 | 760 | break; |
5597 | 1.10k | } |
5598 | 1.10k | } |
5599 | 1.10k | |
5600 | 1.10k | // Stop checking the precise nature of the argument if it is value dependent, |
5601 | 1.10k | // it should be checked when instantiated. |
5602 | 1.10k | if (1.10k Arg->isValueDependent()1.10k ) { |
5603 | 103 | Converted = TemplateArgument(ArgIn); |
5604 | 103 | return false; |
5605 | 103 | } |
5606 | 1.00k | |
5607 | 1.00k | if (1.00k isa<CXXUuidofExpr>(Arg)1.00k ) { |
5608 | 17 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
5609 | 17 | ArgIn, Arg, ArgType)) |
5610 | 0 | return true; |
5611 | 17 | |
5612 | 17 | Converted = TemplateArgument(ArgIn); |
5613 | 17 | return false; |
5614 | 17 | } |
5615 | 986 | |
5616 | 986 | if (986 !DRE986 ) { |
5617 | 23 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
5618 | 23 | << Arg->getSourceRange(); |
5619 | 23 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5620 | 23 | return true; |
5621 | 23 | } |
5622 | 963 | |
5623 | 963 | // Cannot refer to non-static data members |
5624 | 963 | if (963 isa<FieldDecl>(Entity) || 963 isa<IndirectFieldDecl>(Entity)963 ) { |
5625 | 3 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
5626 | 3 | << Entity << Arg->getSourceRange(); |
5627 | 3 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5628 | 3 | return true; |
5629 | 3 | } |
5630 | 960 | |
5631 | 960 | // Cannot refer to non-static member functions |
5632 | 960 | if (CXXMethodDecl *960 Method960 = dyn_cast<CXXMethodDecl>(Entity)) { |
5633 | 22 | if (!Method->isStatic()22 ) { |
5634 | 0 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
5635 | 0 | << Method << Arg->getSourceRange(); |
5636 | 0 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5637 | 0 | return true; |
5638 | 0 | } |
5639 | 960 | } |
5640 | 960 | |
5641 | 960 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
5642 | 960 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
5643 | 960 | |
5644 | 960 | // A non-type template argument must refer to an object or function. |
5645 | 960 | if (!Func && 960 !Var729 ) { |
5646 | 0 | // We found something, but we don't know specifically what it is. |
5647 | 0 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
5648 | 0 | << Arg->getSourceRange(); |
5649 | 0 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
5650 | 0 | return true; |
5651 | 0 | } |
5652 | 960 | |
5653 | 960 | // Address / reference template args must have external linkage in C++98. |
5654 | 960 | if (960 Entity->getFormalLinkage() == InternalLinkage960 ) { |
5655 | 59 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
5656 | 29 | diag::warn_cxx98_compat_template_arg_object_internal : |
5657 | 30 | diag::ext_template_arg_object_internal) |
5658 | 59 | << !Func << Entity << Arg->getSourceRange(); |
5659 | 59 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
5660 | 59 | << !Func; |
5661 | 960 | } else if (901 !Entity->hasLinkage()901 ) { |
5662 | 8 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
5663 | 8 | << !Func << Entity << Arg->getSourceRange(); |
5664 | 8 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
5665 | 8 | << !Func; |
5666 | 8 | return true; |
5667 | 8 | } |
5668 | 952 | |
5669 | 952 | if (952 Func952 ) { |
5670 | 225 | // If the template parameter has pointer type, the function decays. |
5671 | 225 | if (ParamType->isPointerType() && 225 !AddressTaken171 ) |
5672 | 91 | ArgType = S.Context.getPointerType(Func->getType()); |
5673 | 134 | else if (134 AddressTaken && 134 ParamType->isReferenceType()100 ) { |
5674 | 20 | // If we originally had an address-of operator, but the |
5675 | 20 | // parameter has reference type, complain and (if things look |
5676 | 20 | // like they will work) drop the address-of operator. |
5677 | 20 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
5678 | 20 | ParamType.getNonReferenceType())) { |
5679 | 4 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
5680 | 4 | << ParamType; |
5681 | 4 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5682 | 4 | return true; |
5683 | 4 | } |
5684 | 16 | |
5685 | 16 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
5686 | 16 | << ParamType |
5687 | 16 | << FixItHint::CreateRemoval(AddrOpLoc); |
5688 | 16 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5689 | 16 | |
5690 | 16 | ArgType = Func->getType(); |
5691 | 16 | } |
5692 | 952 | } else { |
5693 | 727 | // A value of reference type is not an object. |
5694 | 727 | if (Var->getType()->isReferenceType()727 ) { |
5695 | 4 | S.Diag(Arg->getLocStart(), |
5696 | 4 | diag::err_template_arg_reference_var) |
5697 | 4 | << Var->getType() << Arg->getSourceRange(); |
5698 | 4 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5699 | 4 | return true; |
5700 | 4 | } |
5701 | 723 | |
5702 | 723 | // A template argument must have static storage duration. |
5703 | 723 | if (723 Var->getTLSKind()723 ) { |
5704 | 6 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
5705 | 6 | << Arg->getSourceRange(); |
5706 | 6 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
5707 | 6 | return true; |
5708 | 6 | } |
5709 | 717 | |
5710 | 717 | // If the template parameter has pointer type, we must have taken |
5711 | 717 | // the address of this object. |
5712 | 717 | if (717 ParamType->isReferenceType()717 ) { |
5713 | 247 | if (AddressTaken247 ) { |
5714 | 9 | // If we originally had an address-of operator, but the |
5715 | 9 | // parameter has reference type, complain and (if things look |
5716 | 9 | // like they will work) drop the address-of operator. |
5717 | 9 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
5718 | 9 | ParamType.getNonReferenceType())) { |
5719 | 3 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
5720 | 3 | << ParamType; |
5721 | 3 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5722 | 3 | return true; |
5723 | 3 | } |
5724 | 6 | |
5725 | 6 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
5726 | 6 | << ParamType |
5727 | 6 | << FixItHint::CreateRemoval(AddrOpLoc); |
5728 | 6 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5729 | 6 | |
5730 | 6 | ArgType = Var->getType(); |
5731 | 6 | } |
5732 | 717 | } else if (470 !AddressTaken && 470 ParamType->isPointerType()65 ) { |
5733 | 65 | if (Var->getType()->isArrayType()65 ) { |
5734 | 46 | // Array-to-pointer decay. |
5735 | 46 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
5736 | 65 | } else { |
5737 | 19 | // If the template parameter has pointer type but the address of |
5738 | 19 | // this object was not taken, complain and (possibly) recover by |
5739 | 19 | // taking the address of the entity. |
5740 | 19 | ArgType = S.Context.getPointerType(Var->getType()); |
5741 | 19 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)19 ) { |
5742 | 15 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
5743 | 15 | << ParamType; |
5744 | 15 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5745 | 15 | return true; |
5746 | 15 | } |
5747 | 4 | |
5748 | 4 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
5749 | 4 | << ParamType |
5750 | 4 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
5751 | 4 | |
5752 | 4 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5753 | 4 | } |
5754 | 470 | } |
5755 | 727 | } |
5756 | 952 | |
5757 | 920 | if (920 CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
5758 | 920 | Arg, ArgType)) |
5759 | 66 | return true; |
5760 | 854 | |
5761 | 854 | // Create the template argument. |
5762 | 854 | Converted = |
5763 | 854 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
5764 | 854 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
5765 | 854 | return false; |
5766 | 854 | } |
5767 | | |
5768 | | /// \brief Checks whether the given template argument is a pointer to |
5769 | | /// member constant according to C++ [temp.arg.nontype]p1. |
5770 | | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
5771 | | NonTypeTemplateParmDecl *Param, |
5772 | | QualType ParamType, |
5773 | | Expr *&ResultArg, |
5774 | 199 | TemplateArgument &Converted) { |
5775 | 199 | bool Invalid = false; |
5776 | 199 | |
5777 | 199 | Expr *Arg = ResultArg; |
5778 | 199 | bool ObjCLifetimeConversion; |
5779 | 199 | |
5780 | 199 | // C++ [temp.arg.nontype]p1: |
5781 | 199 | // |
5782 | 199 | // A template-argument for a non-type, non-template |
5783 | 199 | // template-parameter shall be one of: [...] |
5784 | 199 | // |
5785 | 199 | // -- a pointer to member expressed as described in 5.3.1. |
5786 | 199 | DeclRefExpr *DRE = nullptr; |
5787 | 199 | |
5788 | 199 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
5789 | 199 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
5790 | 199 | bool ExtraParens = false; |
5791 | 202 | while (ParenExpr *Parens202 = dyn_cast<ParenExpr>(Arg)) { |
5792 | 3 | if (!Invalid && 3 !ExtraParens3 ) { |
5793 | 3 | S.Diag(Arg->getLocStart(), |
5794 | 3 | S.getLangOpts().CPlusPlus11 ? |
5795 | 2 | diag::warn_cxx98_compat_template_arg_extra_parens : |
5796 | 1 | diag::ext_template_arg_extra_parens) |
5797 | 3 | << Arg->getSourceRange(); |
5798 | 3 | ExtraParens = true; |
5799 | 3 | } |
5800 | 3 | |
5801 | 3 | Arg = Parens->getSubExpr(); |
5802 | 3 | } |
5803 | 199 | |
5804 | 206 | while (SubstNonTypeTemplateParmExpr *subst = |
5805 | 199 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
5806 | 7 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
5807 | 199 | |
5808 | 199 | // A pointer-to-member constant written &Class::member. |
5809 | 199 | if (UnaryOperator *UnOp199 = dyn_cast<UnaryOperator>(Arg)) { |
5810 | 137 | if (UnOp->getOpcode() == UO_AddrOf137 ) { |
5811 | 137 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
5812 | 137 | if (DRE && 137 !DRE->getQualifier()137 ) |
5813 | 0 | DRE = nullptr; |
5814 | 137 | } |
5815 | 137 | } |
5816 | 199 | // A constant of pointer-to-member type. |
5817 | 62 | else if (62 (DRE = dyn_cast<DeclRefExpr>(Arg))62 ) { |
5818 | 7 | if (ValueDecl *VD7 = dyn_cast<ValueDecl>(DRE->getDecl())) { |
5819 | 7 | if (VD->getType()->isMemberPointerType()7 ) { |
5820 | 5 | if (isa<NonTypeTemplateParmDecl>(VD)5 ) { |
5821 | 4 | if (Arg->isTypeDependent() || 4 Arg->isValueDependent()4 ) { |
5822 | 4 | Converted = TemplateArgument(Arg); |
5823 | 4 | } else { |
5824 | 0 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
5825 | 0 | Converted = TemplateArgument(VD, ParamType); |
5826 | 0 | } |
5827 | 4 | return Invalid; |
5828 | 4 | } |
5829 | 3 | } |
5830 | 7 | } |
5831 | 3 | |
5832 | 3 | DRE = nullptr; |
5833 | 3 | } |
5834 | 199 | |
5835 | 195 | ValueDecl *Entity = DRE ? 195 DRE->getDecl()137 : nullptr58 ; |
5836 | 195 | |
5837 | 195 | // Check for a null pointer value. |
5838 | 195 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
5839 | 195 | Entity)) { |
5840 | 0 | case NPV_Error: |
5841 | 0 | return true; |
5842 | 53 | case NPV_NullPointer: |
5843 | 53 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
5844 | 53 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
5845 | 53 | /*isNullPtr*/true); |
5846 | 53 | return false; |
5847 | 142 | case NPV_NotNullPointer: |
5848 | 142 | break; |
5849 | 142 | } |
5850 | 142 | |
5851 | 142 | if (142 S.IsQualificationConversion(ResultArg->getType(), |
5852 | 142 | ParamType.getNonReferenceType(), false, |
5853 | 142 | ObjCLifetimeConversion)) { |
5854 | 5 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
5855 | 5 | ResultArg->getValueKind()) |
5856 | 5 | .get(); |
5857 | 142 | } else if (137 !S.Context.hasSameUnqualifiedType( |
5858 | 137 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
5859 | 13 | // We can't perform this conversion. |
5860 | 13 | S.Diag(ResultArg->getLocStart(), diag::err_template_arg_not_convertible) |
5861 | 13 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
5862 | 13 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
5863 | 13 | return true; |
5864 | 13 | } |
5865 | 129 | |
5866 | 129 | if (129 !DRE129 ) |
5867 | 2 | return S.Diag(Arg->getLocStart(), |
5868 | 2 | diag::err_template_arg_not_pointer_to_member_form) |
5869 | 2 | << Arg->getSourceRange(); |
5870 | 127 | |
5871 | 127 | if (127 isa<FieldDecl>(DRE->getDecl()) || |
5872 | 92 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
5873 | 127 | isa<CXXMethodDecl>(DRE->getDecl())86 ) { |
5874 | 127 | assert((isa<FieldDecl>(DRE->getDecl()) || |
5875 | 127 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
5876 | 127 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
5877 | 127 | "Only non-static member pointers can make it here"); |
5878 | 127 | |
5879 | 127 | // Okay: this is the address of a non-static member, and therefore |
5880 | 127 | // a member pointer constant. |
5881 | 127 | if (Arg->isTypeDependent() || 127 Arg->isValueDependent()127 ) { |
5882 | 0 | Converted = TemplateArgument(Arg); |
5883 | 127 | } else { |
5884 | 127 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
5885 | 127 | Converted = TemplateArgument(D, ParamType); |
5886 | 127 | } |
5887 | 127 | return Invalid; |
5888 | 127 | } |
5889 | 0 |
|
5890 | 0 | // We found something else, but we don't know specifically what it is. |
5891 | 0 | S.Diag(Arg->getLocStart(), |
5892 | 0 | diag::err_template_arg_not_pointer_to_member_form) |
5893 | 0 | << Arg->getSourceRange(); |
5894 | 0 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
5895 | 0 | return true; |
5896 | 0 | } |
5897 | | |
5898 | | /// \brief Check a template argument against its corresponding |
5899 | | /// non-type template parameter. |
5900 | | /// |
5901 | | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
5902 | | /// If an error occurred, it returns ExprError(); otherwise, it |
5903 | | /// returns the converted template argument. \p ParamType is the |
5904 | | /// type of the non-type template parameter after it has been instantiated. |
5905 | | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
5906 | | QualType ParamType, Expr *Arg, |
5907 | | TemplateArgument &Converted, |
5908 | 59.3k | CheckTemplateArgumentKind CTAK) { |
5909 | 59.3k | SourceLocation StartLoc = Arg->getLocStart(); |
5910 | 59.3k | |
5911 | 59.3k | // If the parameter type somehow involves auto, deduce the type now. |
5912 | 59.3k | if (getLangOpts().CPlusPlus1z && 59.3k ParamType->isUndeducedType()1.75k ) { |
5913 | 210 | // During template argument deduction, we allow 'decltype(auto)' to |
5914 | 210 | // match an arbitrary dependent argument. |
5915 | 210 | // FIXME: The language rules don't say what happens in this case. |
5916 | 210 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
5917 | 210 | // expression is merely instantiation-dependent; is this enough? |
5918 | 210 | if (CTAK == CTAK_Deduced && 210 Arg->isTypeDependent()53 ) { |
5919 | 34 | auto *AT = dyn_cast<AutoType>(ParamType); |
5920 | 34 | if (AT && 34 AT->isDecltypeAuto()15 ) { |
5921 | 0 | Converted = TemplateArgument(Arg); |
5922 | 0 | return Arg; |
5923 | 0 | } |
5924 | 210 | } |
5925 | 210 | |
5926 | 210 | // When checking a deduced template argument, deduce from its type even if |
5927 | 210 | // the type is dependent, in order to check the types of non-type template |
5928 | 210 | // arguments line up properly in partial ordering. |
5929 | 210 | Optional<unsigned> Depth; |
5930 | 210 | if (CTAK != CTAK_Specified) |
5931 | 53 | Depth = Param->getDepth() + 1; |
5932 | 210 | if (DeduceAutoType( |
5933 | 210 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
5934 | 210 | Arg, ParamType, Depth) == DAR_Failed) { |
5935 | 0 | Diag(Arg->getExprLoc(), |
5936 | 0 | diag::err_non_type_template_parm_type_deduction_failure) |
5937 | 0 | << Param->getDeclName() << Param->getType() << Arg->getType() |
5938 | 0 | << Arg->getSourceRange(); |
5939 | 0 | Diag(Param->getLocation(), diag::note_template_param_here); |
5940 | 0 | return ExprError(); |
5941 | 0 | } |
5942 | 210 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
5943 | 210 | // an error. The error message normally references the parameter |
5944 | 210 | // declaration, but here we'll pass the argument location because that's |
5945 | 210 | // where the parameter type is deduced. |
5946 | 210 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
5947 | 210 | if (ParamType.isNull()210 ) { |
5948 | 1 | Diag(Param->getLocation(), diag::note_template_param_here); |
5949 | 1 | return ExprError(); |
5950 | 1 | } |
5951 | 59.3k | } |
5952 | 59.3k | |
5953 | 59.3k | // We should have already dropped all cv-qualifiers by now. |
5954 | 59.3k | assert(!ParamType.hasQualifiers() && |
5955 | 59.3k | "non-type template parameter type cannot be qualified"); |
5956 | 59.3k | |
5957 | 59.3k | if (CTAK == CTAK_Deduced && |
5958 | 1.56k | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
5959 | 59.3k | Arg->getType())) { |
5960 | 71 | // FIXME: If either type is dependent, we skip the check. This isn't |
5961 | 71 | // correct, since during deduction we're supposed to have replaced each |
5962 | 71 | // template parameter with some unique (non-dependent) placeholder. |
5963 | 71 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
5964 | 71 | // type check in order to force specific types to be more specialized than |
5965 | 71 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
5966 | 71 | // work. |
5967 | 71 | if ((ParamType->isDependentType() || 71 Arg->isTypeDependent()43 ) && |
5968 | 71 | !Arg->getType()->getContainedAutoType()39 ) { |
5969 | 29 | Converted = TemplateArgument(Arg); |
5970 | 29 | return Arg; |
5971 | 29 | } |
5972 | 42 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
5973 | 42 | // we should actually be checking the type of the template argument in P, |
5974 | 42 | // not the type of the template argument deduced from A, against the |
5975 | 42 | // template parameter type. |
5976 | 42 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
5977 | 42 | << Arg->getType() |
5978 | 42 | << ParamType.getUnqualifiedType(); |
5979 | 42 | Diag(Param->getLocation(), diag::note_template_param_here); |
5980 | 42 | return ExprError(); |
5981 | 42 | } |
5982 | 59.2k | |
5983 | 59.2k | // If either the parameter has a dependent type or the argument is |
5984 | 59.2k | // type-dependent, there's nothing we can check now. |
5985 | 59.2k | if (59.2k ParamType->isDependentType() || 59.2k Arg->isTypeDependent()58.7k ) { |
5986 | 3.18k | // FIXME: Produce a cloned, canonical expression? |
5987 | 3.18k | Converted = TemplateArgument(Arg); |
5988 | 3.18k | return Arg; |
5989 | 3.18k | } |
5990 | 56.0k | |
5991 | 56.0k | // The initialization of the parameter from the argument is |
5992 | 56.0k | // a constant-evaluated context. |
5993 | 56.0k | EnterExpressionEvaluationContext ConstantEvaluated( |
5994 | 56.0k | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
5995 | 56.0k | |
5996 | 56.0k | if (getLangOpts().CPlusPlus1z56.0k ) { |
5997 | 1.50k | // C++1z [temp.arg.nontype]p1: |
5998 | 1.50k | // A template-argument for a non-type template parameter shall be |
5999 | 1.50k | // a converted constant expression of the type of the template-parameter. |
6000 | 1.50k | APValue Value; |
6001 | 1.50k | ExprResult ArgResult = CheckConvertedConstantExpression( |
6002 | 1.50k | Arg, ParamType, Value, CCEK_TemplateArg); |
6003 | 1.50k | if (ArgResult.isInvalid()) |
6004 | 50 | return ExprError(); |
6005 | 1.45k | |
6006 | 1.45k | // For a value-dependent argument, CheckConvertedConstantExpression is |
6007 | 1.45k | // permitted (and expected) to be unable to determine a value. |
6008 | 1.45k | if (1.45k ArgResult.get()->isValueDependent()1.45k ) { |
6009 | 193 | Converted = TemplateArgument(ArgResult.get()); |
6010 | 193 | return ArgResult; |
6011 | 193 | } |
6012 | 1.26k | |
6013 | 1.26k | QualType CanonParamType = Context.getCanonicalType(ParamType); |
6014 | 1.26k | |
6015 | 1.26k | // Convert the APValue to a TemplateArgument. |
6016 | 1.26k | switch (Value.getKind()) { |
6017 | 0 | case APValue::Uninitialized: |
6018 | 0 | assert(ParamType->isNullPtrType()); |
6019 | 0 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
6020 | 0 | break; |
6021 | 1.02k | case APValue::Int: |
6022 | 1.02k | assert(ParamType->isIntegralOrEnumerationType()); |
6023 | 1.02k | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
6024 | 1.02k | break; |
6025 | 37 | case APValue::MemberPointer: { |
6026 | 37 | assert(ParamType->isMemberPointerType()); |
6027 | 37 | |
6028 | 37 | // FIXME: We need TemplateArgument representation and mangling for these. |
6029 | 37 | if (!Value.getMemberPointerPath().empty()37 ) { |
6030 | 6 | Diag(Arg->getLocStart(), |
6031 | 6 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
6032 | 6 | << Value.getMemberPointerDecl() << ParamType |
6033 | 6 | << Arg->getSourceRange(); |
6034 | 6 | return ExprError(); |
6035 | 6 | } |
6036 | 31 | |
6037 | 31 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
6038 | 28 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
6039 | 3 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
6040 | 31 | break; |
6041 | 31 | } |
6042 | 196 | case APValue::LValue: { |
6043 | 196 | // For a non-type template-parameter of pointer or reference type, |
6044 | 196 | // the value of the constant expression shall not refer to |
6045 | 196 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
6046 | 196 | ParamType->isNullPtrType()); |
6047 | 196 | // -- a temporary object |
6048 | 196 | // -- a string literal |
6049 | 196 | // -- the result of a typeid expression, or |
6050 | 196 | // -- a predefined __func__ variable |
6051 | 196 | if (auto *E196 = Value.getLValueBase().dyn_cast<const Expr*>()) { |
6052 | 7 | if (isa<CXXUuidofExpr>(E)7 ) { |
6053 | 0 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
6054 | 0 | break; |
6055 | 0 | } |
6056 | 7 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
6057 | 7 | << Arg->getSourceRange(); |
6058 | 7 | return ExprError(); |
6059 | 7 | } |
6060 | 189 | auto *VD = const_cast<ValueDecl *>( |
6061 | 189 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
6062 | 189 | // -- a subobject |
6063 | 189 | if (Value.hasLValuePath() && 189 Value.getLValuePath().size() == 1189 && |
6064 | 189 | VD13 && VD->getType()->isArrayType()13 && |
6065 | 12 | Value.getLValuePath()[0].ArrayIndex == 0 && |
6066 | 189 | !Value.isLValueOnePastTheEnd()11 && ParamType->isPointerType()11 ) { |
6067 | 11 | // Per defect report (no number yet): |
6068 | 11 | // ... other than a pointer to the first element of a complete array |
6069 | 11 | // object. |
6070 | 189 | } else if (178 !Value.hasLValuePath() || 178 Value.getLValuePath().size()178 || |
6071 | 178 | Value.isLValueOnePastTheEnd()176 ) { |
6072 | 3 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
6073 | 3 | << Value.getAsString(Context, ParamType); |
6074 | 3 | return ExprError(); |
6075 | 3 | } |
6076 | 189 | assert((VD || !ParamType->isReferenceType()) && |
6077 | 186 | "null reference should not be a constant expression"); |
6078 | 186 | assert((!VD || !ParamType->isNullPtrType()) && |
6079 | 186 | "non-null value of type nullptr_t?"); |
6080 | 154 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
6081 | 32 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
6082 | 186 | break; |
6083 | 186 | } |
6084 | 1 | case APValue::AddrLabelDiff: |
6085 | 1 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
6086 | 0 | case APValue::Float: |
6087 | 0 | case APValue::ComplexInt: |
6088 | 0 | case APValue::ComplexFloat: |
6089 | 0 | case APValue::Vector: |
6090 | 0 | case APValue::Array: |
6091 | 0 | case APValue::Struct: |
6092 | 0 | case APValue::Union: |
6093 | 0 | llvm_unreachable("invalid kind for template argument"); |
6094 | 1.24k | } |
6095 | 1.24k | |
6096 | 1.24k | return ArgResult.get(); |
6097 | 1.24k | } |
6098 | 54.5k | |
6099 | 54.5k | // C++ [temp.arg.nontype]p5: |
6100 | 54.5k | // The following conversions are performed on each expression used |
6101 | 54.5k | // as a non-type template-argument. If a non-type |
6102 | 54.5k | // template-argument cannot be converted to the type of the |
6103 | 54.5k | // corresponding template-parameter then the program is |
6104 | 54.5k | // ill-formed. |
6105 | 54.5k | if (54.5k ParamType->isIntegralOrEnumerationType()54.5k ) { |
6106 | 53.0k | // C++11: |
6107 | 53.0k | // -- for a non-type template-parameter of integral or |
6108 | 53.0k | // enumeration type, conversions permitted in a converted |
6109 | 53.0k | // constant expression are applied. |
6110 | 53.0k | // |
6111 | 53.0k | // C++98: |
6112 | 53.0k | // -- for a non-type template-parameter of integral or |
6113 | 53.0k | // enumeration type, integral promotions (4.5) and integral |
6114 | 53.0k | // conversions (4.7) are applied. |
6115 | 53.0k | |
6116 | 53.0k | if (getLangOpts().CPlusPlus1153.0k ) { |
6117 | 12.5k | // C++ [temp.arg.nontype]p1: |
6118 | 12.5k | // A template-argument for a non-type, non-template template-parameter |
6119 | 12.5k | // shall be one of: |
6120 | 12.5k | // |
6121 | 12.5k | // -- for a non-type template-parameter of integral or enumeration |
6122 | 12.5k | // type, a converted constant expression of the type of the |
6123 | 12.5k | // template-parameter; or |
6124 | 12.5k | llvm::APSInt Value; |
6125 | 12.5k | ExprResult ArgResult = |
6126 | 12.5k | CheckConvertedConstantExpression(Arg, ParamType, Value, |
6127 | 12.5k | CCEK_TemplateArg); |
6128 | 12.5k | if (ArgResult.isInvalid()) |
6129 | 17 | return ExprError(); |
6130 | 12.5k | |
6131 | 12.5k | // We can't check arbitrary value-dependent arguments. |
6132 | 12.5k | if (12.5k ArgResult.get()->isValueDependent()12.5k ) { |
6133 | 1.42k | Converted = TemplateArgument(ArgResult.get()); |
6134 | 1.42k | return ArgResult; |
6135 | 1.42k | } |
6136 | 11.1k | |
6137 | 11.1k | // Widen the argument value to sizeof(parameter type). This is almost |
6138 | 11.1k | // always a no-op, except when the parameter type is bool. In |
6139 | 11.1k | // that case, this may extend the argument from 1 bit to 8 bits. |
6140 | 11.1k | QualType IntegerType = ParamType; |
6141 | 11.1k | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
6142 | 177 | IntegerType = Enum->getDecl()->getIntegerType(); |
6143 | 12.5k | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
6144 | 12.5k | |
6145 | 12.5k | Converted = TemplateArgument(Context, Value, |
6146 | 12.5k | Context.getCanonicalType(ParamType)); |
6147 | 12.5k | return ArgResult; |
6148 | 12.5k | } |
6149 | 40.4k | |
6150 | 40.4k | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
6151 | 40.4k | if (ArgResult.isInvalid()) |
6152 | 0 | return ExprError(); |
6153 | 40.4k | Arg = ArgResult.get(); |
6154 | 40.4k | |
6155 | 40.4k | QualType ArgType = Arg->getType(); |
6156 | 40.4k | |
6157 | 40.4k | // C++ [temp.arg.nontype]p1: |
6158 | 40.4k | // A template-argument for a non-type, non-template |
6159 | 40.4k | // template-parameter shall be one of: |
6160 | 40.4k | // |
6161 | 40.4k | // -- an integral constant-expression of integral or enumeration |
6162 | 40.4k | // type; or |
6163 | 40.4k | // -- the name of a non-type template-parameter; or |
6164 | 40.4k | SourceLocation NonConstantLoc; |
6165 | 40.4k | llvm::APSInt Value; |
6166 | 40.4k | if (!ArgType->isIntegralOrEnumerationType()40.4k ) { |
6167 | 2 | Diag(Arg->getLocStart(), |
6168 | 2 | diag::err_template_arg_not_integral_or_enumeral) |
6169 | 2 | << ArgType << Arg->getSourceRange(); |
6170 | 2 | Diag(Param->getLocation(), diag::note_template_param_here); |
6171 | 2 | return ExprError(); |
6172 | 40.4k | } else if (40.4k !Arg->isValueDependent()40.4k ) { |
6173 | 15.1k | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
6174 | 15.1k | QualType T; |
6175 | 15.1k | |
6176 | 15.1k | public: |
6177 | 15.1k | TmplArgICEDiagnoser(QualType T) : T(T) { } |
6178 | 15.1k | |
6179 | 15.1k | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
6180 | 3 | SourceRange SR) override { |
6181 | 3 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
6182 | 3 | } |
6183 | 15.1k | } Diagnoser(ArgType); |
6184 | 15.1k | |
6185 | 15.1k | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
6186 | 15.1k | false).get(); |
6187 | 15.1k | if (!Arg) |
6188 | 3 | return ExprError(); |
6189 | 40.4k | } |
6190 | 40.4k | |
6191 | 40.4k | // From here on out, all we care about is the unqualified form |
6192 | 40.4k | // of the argument type. |
6193 | 40.4k | ArgType = ArgType.getUnqualifiedType(); |
6194 | 40.4k | |
6195 | 40.4k | // Try to convert the argument to the parameter's type. |
6196 | 40.4k | if (Context.hasSameType(ParamType, ArgType)40.4k ) { |
6197 | 39.0k | // Okay: no conversion necessary |
6198 | 40.4k | } else if (1.34k ParamType->isBooleanType()1.34k ) { |
6199 | 8 | // This is an integral-to-boolean conversion. |
6200 | 8 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
6201 | 1.34k | } else if (1.33k IsIntegralPromotion(Arg, ArgType, ParamType) || |
6202 | 1.33k | !ParamType->isEnumeralType()1.20k ) { |
6203 | 1.33k | // This is an integral promotion or conversion. |
6204 | 1.33k | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
6205 | 1.33k | } else { |
6206 | 1 | // We can't perform this conversion. |
6207 | 1 | Diag(Arg->getLocStart(), |
6208 | 1 | diag::err_template_arg_not_convertible) |
6209 | 1 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
6210 | 1 | Diag(Param->getLocation(), diag::note_template_param_here); |
6211 | 1 | return ExprError(); |
6212 | 1 | } |
6213 | 40.4k | |
6214 | 40.4k | // Add the value of this argument to the list of converted |
6215 | 40.4k | // arguments. We use the bitwidth and signedness of the template |
6216 | 40.4k | // parameter. |
6217 | 40.4k | if (40.4k Arg->isValueDependent()40.4k ) { |
6218 | 25.2k | // The argument is value-dependent. Create a new |
6219 | 25.2k | // TemplateArgument with the converted expression. |
6220 | 25.2k | Converted = TemplateArgument(Arg); |
6221 | 25.2k | return Arg; |
6222 | 25.2k | } |
6223 | 15.1k | |
6224 | 15.1k | QualType IntegerType = Context.getCanonicalType(ParamType); |
6225 | 15.1k | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
6226 | 40 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
6227 | 15.1k | |
6228 | 15.1k | if (ParamType->isBooleanType()15.1k ) { |
6229 | 936 | // Value must be zero or one. |
6230 | 936 | Value = Value != 0; |
6231 | 936 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
6232 | 936 | if (Value.getBitWidth() != AllowedBits) |
6233 | 936 | Value = Value.extOrTrunc(AllowedBits); |
6234 | 936 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
6235 | 15.1k | } else { |
6236 | 14.2k | llvm::APSInt OldValue = Value; |
6237 | 14.2k | |
6238 | 14.2k | // Coerce the template argument's value to the value it will have |
6239 | 14.2k | // based on the template parameter's type. |
6240 | 14.2k | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
6241 | 14.2k | if (Value.getBitWidth() != AllowedBits) |
6242 | 379 | Value = Value.extOrTrunc(AllowedBits); |
6243 | 14.2k | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
6244 | 14.2k | |
6245 | 14.2k | // Complain if an unsigned parameter received a negative value. |
6246 | 14.2k | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
6247 | 14.2k | && (OldValue.isSigned() && 1.96k OldValue.isNegative()557 )) { |
6248 | 1 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
6249 | 1 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
6250 | 1 | << Arg->getSourceRange(); |
6251 | 1 | Diag(Param->getLocation(), diag::note_template_param_here); |
6252 | 1 | } |
6253 | 14.2k | |
6254 | 14.2k | // Complain if we overflowed the template parameter's type. |
6255 | 14.2k | unsigned RequiredBits; |
6256 | 14.2k | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
6257 | 1.96k | RequiredBits = OldValue.getActiveBits(); |
6258 | 12.2k | else if (12.2k OldValue.isUnsigned()12.2k ) |
6259 | 242 | RequiredBits = OldValue.getActiveBits() + 1; |
6260 | 12.2k | else |
6261 | 12.0k | RequiredBits = OldValue.getMinSignedBits(); |
6262 | 14.2k | if (RequiredBits > AllowedBits14.2k ) { |
6263 | 14 | Diag(Arg->getLocStart(), |
6264 | 14 | diag::warn_template_arg_too_large) |
6265 | 14 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
6266 | 14 | << Arg->getSourceRange(); |
6267 | 14 | Diag(Param->getLocation(), diag::note_template_param_here); |
6268 | 14 | } |
6269 | 14.2k | } |
6270 | 15.1k | |
6271 | 15.1k | Converted = TemplateArgument(Context, Value, |
6272 | 15.1k | ParamType->isEnumeralType() |
6273 | 40 | ? Context.getCanonicalType(ParamType) |
6274 | 15.1k | : IntegerType); |
6275 | 53.0k | return Arg; |
6276 | 53.0k | } |
6277 | 1.53k | |
6278 | 1.53k | QualType ArgType = Arg->getType(); |
6279 | 1.53k | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
6280 | 1.53k | |
6281 | 1.53k | // Handle pointer-to-function, reference-to-function, and |
6282 | 1.53k | // pointer-to-member-function all in (roughly) the same way. |
6283 | 1.53k | if (// -- For a non-type template-parameter of type pointer to |
6284 | 1.53k | // function, only the function-to-pointer conversion (4.3) is |
6285 | 1.53k | // applied. If the template-argument represents a set of |
6286 | 1.53k | // overloaded functions (or a pointer to such), the matching |
6287 | 1.53k | // function is selected from the set (13.4). |
6288 | 1.53k | (ParamType->isPointerType() && |
6289 | 966 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
6290 | 1.53k | // -- For a non-type template-parameter of type reference to |
6291 | 1.53k | // function, no conversions apply. If the template-argument |
6292 | 1.53k | // represents a set of overloaded functions, the matching |
6293 | 1.53k | // function is selected from the set (13.4). |
6294 | 1.28k | (ParamType->isReferenceType() && |
6295 | 1.28k | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
6296 | 1.53k | // -- For a non-type template-parameter of type pointer to |
6297 | 1.53k | // member function, no conversions apply. If the |
6298 | 1.53k | // template-argument represents a set of overloaded member |
6299 | 1.53k | // functions, the matching member function is selected from |
6300 | 1.53k | // the set (13.4). |
6301 | 1.22k | (ParamType->isMemberPointerType() && |
6302 | 199 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
6303 | 1.53k | ->isFunctionType())) { |
6304 | 419 | |
6305 | 419 | if (Arg->getType() == Context.OverloadTy419 ) { |
6306 | 103 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
6307 | 103 | true, |
6308 | 101 | FoundResult)) { |
6309 | 101 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
6310 | 0 | return ExprError(); |
6311 | 101 | |
6312 | 101 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
6313 | 101 | ArgType = Arg->getType(); |
6314 | 101 | } else |
6315 | 2 | return ExprError(); |
6316 | 417 | } |
6317 | 417 | |
6318 | 417 | if (417 !ParamType->isMemberPointerType()417 ) { |
6319 | 307 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
6320 | 307 | ParamType, |
6321 | 307 | Arg, Converted)) |
6322 | 35 | return ExprError(); |
6323 | 272 | return Arg; |
6324 | 272 | } |
6325 | 110 | |
6326 | 110 | if (110 CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
6327 | 110 | Converted)) |
6328 | 5 | return ExprError(); |
6329 | 105 | return Arg; |
6330 | 105 | } |
6331 | 1.11k | |
6332 | 1.11k | if (1.11k ParamType->isPointerType()1.11k ) { |
6333 | 716 | // -- for a non-type template-parameter of type pointer to |
6334 | 716 | // object, qualification conversions (4.4) and the |
6335 | 716 | // array-to-pointer conversion (4.2) are applied. |
6336 | 716 | // C++0x also allows a value of std::nullptr_t. |
6337 | 716 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
6338 | 716 | "Only object pointers allowed here"); |
6339 | 716 | |
6340 | 716 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
6341 | 716 | ParamType, |
6342 | 716 | Arg, Converted)) |
6343 | 57 | return ExprError(); |
6344 | 659 | return Arg; |
6345 | 659 | } |
6346 | 397 | |
6347 | 397 | if (const ReferenceType *397 ParamRefType397 = ParamType->getAs<ReferenceType>()) { |
6348 | 288 | // -- For a non-type template-parameter of type reference to |
6349 | 288 | // object, no conversions apply. The type referred to by the |
6350 | 288 | // reference may be more cv-qualified than the (otherwise |
6351 | 288 | // identical) type of the template-argument. The |
6352 | 288 | // template-parameter is bound directly to the |
6353 | 288 | // template-argument, which must be an lvalue. |
6354 | 288 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
6355 | 288 | "Only object references allowed here"); |
6356 | 288 | |
6357 | 288 | if (Arg->getType() == Context.OverloadTy288 ) { |
6358 | 0 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
6359 | 0 | ParamRefType->getPointeeType(), |
6360 | 0 | true, |
6361 | 0 | FoundResult)) { |
6362 | 0 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
6363 | 0 | return ExprError(); |
6364 | 0 |
|
6365 | 0 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
6366 | 0 | ArgType = Arg->getType(); |
6367 | 0 | } else |
6368 | 0 | return ExprError(); |
6369 | 288 | } |
6370 | 288 | |
6371 | 288 | if (288 CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
6372 | 288 | ParamType, |
6373 | 288 | Arg, Converted)) |
6374 | 57 | return ExprError(); |
6375 | 231 | return Arg; |
6376 | 231 | } |
6377 | 109 | |
6378 | 109 | // Deal with parameters of type std::nullptr_t. |
6379 | 109 | if (109 ParamType->isNullPtrType()109 ) { |
6380 | 20 | if (Arg->isTypeDependent() || 20 Arg->isValueDependent()20 ) { |
6381 | 1 | Converted = TemplateArgument(Arg); |
6382 | 1 | return Arg; |
6383 | 1 | } |
6384 | 19 | |
6385 | 19 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
6386 | 1 | case NPV_NotNullPointer: |
6387 | 1 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
6388 | 1 | << Arg->getType() << ParamType; |
6389 | 1 | Diag(Param->getLocation(), diag::note_template_param_here); |
6390 | 1 | return ExprError(); |
6391 | 19 | |
6392 | 0 | case NPV_Error: |
6393 | 0 | return ExprError(); |
6394 | 19 | |
6395 | 18 | case NPV_NullPointer: |
6396 | 18 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
6397 | 18 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
6398 | 18 | /*isNullPtr*/true); |
6399 | 18 | return Arg; |
6400 | 89 | } |
6401 | 89 | } |
6402 | 89 | |
6403 | 89 | // -- For a non-type template-parameter of type pointer to data |
6404 | 89 | // member, qualification conversions (4.4) are applied. |
6405 | 109 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
6406 | 89 | |
6407 | 89 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
6408 | 89 | Converted)) |
6409 | 10 | return ExprError(); |
6410 | 79 | return Arg; |
6411 | 79 | } |
6412 | | |
6413 | | static void DiagnoseTemplateParameterListArityMismatch( |
6414 | | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
6415 | | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
6416 | | |
6417 | | /// \brief Check a template argument against its corresponding |
6418 | | /// template template parameter. |
6419 | | /// |
6420 | | /// This routine implements the semantics of C++ [temp.arg.template]. |
6421 | | /// It returns true if an error occurred, and false otherwise. |
6422 | | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
6423 | | TemplateArgumentLoc &Arg, |
6424 | 3.28k | unsigned ArgumentPackIndex) { |
6425 | 3.28k | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
6426 | 3.28k | TemplateDecl *Template = Name.getAsTemplateDecl(); |
6427 | 3.28k | if (!Template3.28k ) { |
6428 | 51 | // Any dependent template name is fine. |
6429 | 51 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
6430 | 51 | return false; |
6431 | 51 | } |
6432 | 3.23k | |
6433 | 3.23k | if (3.23k Template->isInvalidDecl()3.23k ) |
6434 | 0 | return true; |
6435 | 3.23k | |
6436 | 3.23k | // C++0x [temp.arg.template]p1: |
6437 | 3.23k | // A template-argument for a template template-parameter shall be |
6438 | 3.23k | // the name of a class template or an alias template, expressed as an |
6439 | 3.23k | // id-expression. When the template-argument names a class template, only |
6440 | 3.23k | // primary class templates are considered when matching the |
6441 | 3.23k | // template template argument with the corresponding parameter; |
6442 | 3.23k | // partial specializations are not considered even if their |
6443 | 3.23k | // parameter lists match that of the template template parameter. |
6444 | 3.23k | // |
6445 | 3.23k | // Note that we also allow template template parameters here, which |
6446 | 3.23k | // will happen when we are dealing with, e.g., class template |
6447 | 3.23k | // partial specializations. |
6448 | 3.23k | if (3.23k !isa<ClassTemplateDecl>(Template) && |
6449 | 2.36k | !isa<TemplateTemplateParmDecl>(Template) && |
6450 | 2.20k | !isa<TypeAliasTemplateDecl>(Template) && |
6451 | 3.23k | !isa<BuiltinTemplateDecl>(Template)1 ) { |
6452 | 0 | assert(isa<FunctionTemplateDecl>(Template) && |
6453 | 0 | "Only function templates are possible here"); |
6454 | 0 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
6455 | 0 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
6456 | 0 | << Template; |
6457 | 0 | } |
6458 | 3.23k | |
6459 | 3.23k | TemplateParameterList *Params = Param->getTemplateParameters(); |
6460 | 3.23k | if (Param->isExpandedParameterPack()) |
6461 | 37 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
6462 | 3.23k | |
6463 | 3.23k | // C++1z [temp.arg.template]p3: (DR 150) |
6464 | 3.23k | // A template-argument matches a template template-parameter P when P |
6465 | 3.23k | // is at least as specialized as the template-argument A. |
6466 | 3.23k | if (getLangOpts().RelaxedTemplateTemplateArgs3.23k ) { |
6467 | 57 | // Quick check for the common case: |
6468 | 57 | // If P contains a parameter pack, then A [...] matches P if each of A's |
6469 | 57 | // template parameters matches the corresponding template parameter in |
6470 | 57 | // the template-parameter-list of P. |
6471 | 57 | if (TemplateParameterListsAreEqual( |
6472 | 57 | Template->getTemplateParameters(), Params, false, |
6473 | 57 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
6474 | 35 | return false; |
6475 | 22 | |
6476 | 22 | if (22 isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
6477 | 22 | Arg.getLocation())) |
6478 | 9 | return false; |
6479 | 3.19k | // FIXME: Produce better diagnostics for deduction failures. |
6480 | 3.19k | } |
6481 | 3.19k | |
6482 | 3.19k | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
6483 | 3.19k | Params, |
6484 | 3.19k | true, |
6485 | 3.19k | TPL_TemplateTemplateArgumentMatch, |
6486 | 3.19k | Arg.getLocation()); |
6487 | 3.19k | } |
6488 | | |
6489 | | /// \brief Given a non-type template argument that refers to a |
6490 | | /// declaration and the type of its corresponding non-type template |
6491 | | /// parameter, produce an expression that properly refers to that |
6492 | | /// declaration. |
6493 | | ExprResult |
6494 | | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
6495 | | QualType ParamType, |
6496 | 239 | SourceLocation Loc) { |
6497 | 239 | // C++ [temp.param]p8: |
6498 | 239 | // |
6499 | 239 | // A non-type template-parameter of type "array of T" or |
6500 | 239 | // "function returning T" is adjusted to be of type "pointer to |
6501 | 239 | // T" or "pointer to function returning T", respectively. |
6502 | 239 | if (ParamType->isArrayType()) |
6503 | 0 | ParamType = Context.getArrayDecayedType(ParamType); |
6504 | 239 | else if (239 ParamType->isFunctionType()239 ) |
6505 | 0 | ParamType = Context.getPointerType(ParamType); |
6506 | 239 | |
6507 | 239 | // For a NULL non-type template argument, return nullptr casted to the |
6508 | 239 | // parameter's type. |
6509 | 239 | if (Arg.getKind() == TemplateArgument::NullPtr239 ) { |
6510 | 29 | return ImpCastExprToType( |
6511 | 29 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
6512 | 29 | ParamType, |
6513 | 29 | ParamType->getAs<MemberPointerType>() |
6514 | 16 | ? CK_NullToMemberPointer |
6515 | 13 | : CK_NullToPointer); |
6516 | 29 | } |
6517 | 239 | assert(Arg.getKind() == TemplateArgument::Declaration && |
6518 | 210 | "Only declaration template arguments permitted here"); |
6519 | 210 | |
6520 | 210 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
6521 | 210 | |
6522 | 210 | if (VD->getDeclContext()->isRecord() && |
6523 | 63 | (isa<CXXMethodDecl>(VD) || 63 isa<FieldDecl>(VD)42 || |
6524 | 210 | isa<IndirectFieldDecl>(VD)6 )) { |
6525 | 62 | // If the value is a class member, we might have a pointer-to-member. |
6526 | 62 | // Determine whether the non-type template template parameter is of |
6527 | 62 | // pointer-to-member type. If so, we need to build an appropriate |
6528 | 62 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
6529 | 62 | // would refer to the member itself. |
6530 | 62 | if (ParamType->isMemberPointerType()62 ) { |
6531 | 60 | QualType ClassType |
6532 | 60 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
6533 | 60 | NestedNameSpecifier *Qualifier |
6534 | 60 | = NestedNameSpecifier::Create(Context, nullptr, false, |
6535 | 60 | ClassType.getTypePtr()); |
6536 | 60 | CXXScopeSpec SS; |
6537 | 60 | SS.MakeTrivial(Context, Qualifier, Loc); |
6538 | 60 | |
6539 | 60 | // The actual value-ness of this is unimportant, but for |
6540 | 60 | // internal consistency's sake, references to instance methods |
6541 | 60 | // are r-values. |
6542 | 60 | ExprValueKind VK = VK_LValue; |
6543 | 60 | if (isa<CXXMethodDecl>(VD) && 60 cast<CXXMethodDecl>(VD)->isInstance()19 ) |
6544 | 19 | VK = VK_RValue; |
6545 | 60 | |
6546 | 60 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
6547 | 60 | VD->getType().getNonReferenceType(), |
6548 | 60 | VK, |
6549 | 60 | Loc, |
6550 | 60 | &SS); |
6551 | 60 | if (RefExpr.isInvalid()) |
6552 | 0 | return ExprError(); |
6553 | 60 | |
6554 | 60 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
6555 | 60 | |
6556 | 60 | // We might need to perform a trailing qualification conversion, since |
6557 | 60 | // the element type on the parameter could be more qualified than the |
6558 | 60 | // element type in the expression we constructed. |
6559 | 60 | bool ObjCLifetimeConversion; |
6560 | 60 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
6561 | 60 | ParamType.getUnqualifiedType(), false, |
6562 | 60 | ObjCLifetimeConversion)) |
6563 | 1 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
6564 | 60 | |
6565 | 60 | assert(!RefExpr.isInvalid() && |
6566 | 60 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
6567 | 60 | ParamType.getUnqualifiedType())); |
6568 | 60 | return RefExpr; |
6569 | 60 | } |
6570 | 62 | } |
6571 | 150 | |
6572 | 150 | QualType T = VD->getType().getNonReferenceType(); |
6573 | 150 | |
6574 | 150 | if (ParamType->isPointerType()150 ) { |
6575 | 119 | // When the non-type template parameter is a pointer, take the |
6576 | 119 | // address of the declaration. |
6577 | 119 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
6578 | 119 | if (RefExpr.isInvalid()) |
6579 | 0 | return ExprError(); |
6580 | 119 | |
6581 | 119 | if (119 !Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
6582 | 119 | (T->isFunctionType() || 12 T->isArrayType()12 )) { |
6583 | 12 | // Decay functions and arrays unless we're forming a pointer to array. |
6584 | 12 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
6585 | 12 | if (RefExpr.isInvalid()) |
6586 | 0 | return ExprError(); |
6587 | 12 | |
6588 | 12 | return RefExpr; |
6589 | 12 | } |
6590 | 107 | |
6591 | 107 | // Take the address of everything else |
6592 | 107 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
6593 | 107 | } |
6594 | 31 | |
6595 | 31 | ExprValueKind VK = VK_RValue; |
6596 | 31 | |
6597 | 31 | // If the non-type template parameter has reference type, qualify the |
6598 | 31 | // resulting declaration reference with the extra qualifiers on the |
6599 | 31 | // type that the reference refers to. |
6600 | 31 | if (const ReferenceType *TargetRef31 = ParamType->getAs<ReferenceType>()) { |
6601 | 31 | VK = VK_LValue; |
6602 | 31 | T = Context.getQualifiedType(T, |
6603 | 31 | TargetRef->getPointeeType().getQualifiers()); |
6604 | 31 | } else if (0 isa<FunctionDecl>(VD)0 ) { |
6605 | 0 | // References to functions are always lvalues. |
6606 | 0 | VK = VK_LValue; |
6607 | 0 | } |
6608 | 239 | |
6609 | 239 | return BuildDeclRefExpr(VD, T, VK, Loc); |
6610 | 239 | } |
6611 | | |
6612 | | /// \brief Construct a new expression that refers to the given |
6613 | | /// integral template argument with the given source-location |
6614 | | /// information. |
6615 | | /// |
6616 | | /// This routine takes care of the mapping from an integral template |
6617 | | /// argument (which may have any integral type) to the appropriate |
6618 | | /// literal value. |
6619 | | ExprResult |
6620 | | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
6621 | 30.7k | SourceLocation Loc) { |
6622 | 30.7k | assert(Arg.getKind() == TemplateArgument::Integral && |
6623 | 30.7k | "Operation is only valid for integral template arguments"); |
6624 | 30.7k | QualType OrigT = Arg.getIntegralType(); |
6625 | 30.7k | |
6626 | 30.7k | // If this is an enum type that we're instantiating, we need to use an integer |
6627 | 30.7k | // type the same size as the enumerator. We don't want to build an |
6628 | 30.7k | // IntegerLiteral with enum type. The integer type of an enum type can be of |
6629 | 30.7k | // any integral type with C++11 enum classes, make sure we create the right |
6630 | 30.7k | // type of literal for it. |
6631 | 30.7k | QualType T = OrigT; |
6632 | 30.7k | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
6633 | 147 | T = ET->getDecl()->getIntegerType(); |
6634 | 30.7k | |
6635 | 30.7k | Expr *E; |
6636 | 30.7k | if (T->isAnyCharacterType()30.7k ) { |
6637 | 132 | // This does not need to handle u8 character literals because those are |
6638 | 132 | // of type char, and so can also be covered by an ASCII character literal. |
6639 | 132 | CharacterLiteral::CharacterKind Kind; |
6640 | 132 | if (T->isWideCharType()) |
6641 | 1 | Kind = CharacterLiteral::Wide; |
6642 | 131 | else if (131 T->isChar16Type()131 ) |
6643 | 10 | Kind = CharacterLiteral::UTF16; |
6644 | 121 | else if (121 T->isChar32Type()121 ) |
6645 | 14 | Kind = CharacterLiteral::UTF32; |
6646 | 121 | else |
6647 | 107 | Kind = CharacterLiteral::Ascii; |
6648 | 132 | |
6649 | 132 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
6650 | 132 | Kind, T, Loc); |
6651 | 30.7k | } else if (30.5k T->isBooleanType()30.5k ) { |
6652 | 742 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
6653 | 742 | T, Loc); |
6654 | 30.5k | } else if (29.8k T->isNullPtrType()29.8k ) { |
6655 | 0 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
6656 | 29.8k | } else { |
6657 | 29.8k | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
6658 | 29.8k | } |
6659 | 30.7k | |
6660 | 30.7k | if (OrigT->isEnumeralType()30.7k ) { |
6661 | 147 | // FIXME: This is a hack. We need a better way to handle substituted |
6662 | 147 | // non-type template parameters. |
6663 | 147 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
6664 | 147 | nullptr, |
6665 | 147 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
6666 | 147 | Loc, Loc); |
6667 | 147 | } |
6668 | 30.7k | |
6669 | 30.7k | return E; |
6670 | 30.7k | } |
6671 | | |
6672 | | /// \brief Match two template parameters within template parameter lists. |
6673 | | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
6674 | | bool Complain, |
6675 | | Sema::TemplateParameterListEqualKind Kind, |
6676 | 13.7k | SourceLocation TemplateArgLoc) { |
6677 | 13.7k | // Check the actual kind (type, non-type, template). |
6678 | 13.7k | if (Old->getKind() != New->getKind()13.7k ) { |
6679 | 49 | if (Complain49 ) { |
6680 | 15 | unsigned NextDiag = diag::err_template_param_different_kind; |
6681 | 15 | if (TemplateArgLoc.isValid()15 ) { |
6682 | 10 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
6683 | 10 | NextDiag = diag::note_template_param_different_kind; |
6684 | 10 | } |
6685 | 15 | S.Diag(New->getLocation(), NextDiag) |
6686 | 15 | << (Kind != Sema::TPL_TemplateMatch); |
6687 | 15 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
6688 | 15 | << (Kind != Sema::TPL_TemplateMatch); |
6689 | 15 | } |
6690 | 49 | |
6691 | 49 | return false; |
6692 | 49 | } |
6693 | 13.7k | |
6694 | 13.7k | // Check that both are parameter packs or neither are parameter packs. |
6695 | 13.7k | // However, if we are matching a template template argument to a |
6696 | 13.7k | // template template parameter, the template template parameter can have |
6697 | 13.7k | // a parameter pack where the template template argument does not. |
6698 | 13.7k | if (13.7k Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
6699 | 57 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
6700 | 13.7k | Old->isTemplateParameterPack()50 )) { |
6701 | 9 | if (Complain9 ) { |
6702 | 6 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
6703 | 6 | if (TemplateArgLoc.isValid()6 ) { |
6704 | 0 | S.Diag(TemplateArgLoc, |
6705 | 0 | diag::err_template_arg_template_params_mismatch); |
6706 | 0 | NextDiag = diag::note_template_parameter_pack_non_pack; |
6707 | 0 | } |
6708 | 6 | |
6709 | 4 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
6710 | 2 | : isa<NonTypeTemplateParmDecl>(New)? 2 11 |
6711 | 1 | : 2; |
6712 | 6 | S.Diag(New->getLocation(), NextDiag) |
6713 | 6 | << ParamKind << New->isParameterPack(); |
6714 | 6 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
6715 | 6 | << ParamKind << Old->isParameterPack(); |
6716 | 6 | } |
6717 | 9 | |
6718 | 9 | return false; |
6719 | 9 | } |
6720 | 13.7k | |
6721 | 13.7k | // For non-type template parameters, check the type of the parameter. |
6722 | 13.7k | if (NonTypeTemplateParmDecl *13.7k OldNTTP13.7k |
6723 | 2.18k | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
6724 | 2.18k | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
6725 | 2.18k | |
6726 | 2.18k | // If we are matching a template template argument to a template |
6727 | 2.18k | // template parameter and one of the non-type template parameter types |
6728 | 2.18k | // is dependent, then we must wait until template instantiation time |
6729 | 2.18k | // to actually compare the arguments. |
6730 | 2.18k | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
6731 | 365 | (OldNTTP->getType()->isDependentType() || |
6732 | 270 | NewNTTP->getType()->isDependentType())) |
6733 | 104 | return true; |
6734 | 2.08k | |
6735 | 2.08k | if (2.08k !S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())2.08k ) { |
6736 | 43 | if (Complain43 ) { |
6737 | 30 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
6738 | 30 | if (TemplateArgLoc.isValid()30 ) { |
6739 | 27 | S.Diag(TemplateArgLoc, |
6740 | 27 | diag::err_template_arg_template_params_mismatch); |
6741 | 27 | NextDiag = diag::note_template_nontype_parm_different_type; |
6742 | 27 | } |
6743 | 30 | S.Diag(NewNTTP->getLocation(), NextDiag) |
6744 | 30 | << NewNTTP->getType() |
6745 | 30 | << (Kind != Sema::TPL_TemplateMatch); |
6746 | 30 | S.Diag(OldNTTP->getLocation(), |
6747 | 30 | diag::note_template_nontype_parm_prev_declaration) |
6748 | 30 | << OldNTTP->getType(); |
6749 | 30 | } |
6750 | 43 | |
6751 | 43 | return false; |
6752 | 43 | } |
6753 | 2.04k | |
6754 | 2.04k | return true; |
6755 | 2.04k | } |
6756 | 11.5k | |
6757 | 11.5k | // For template template parameters, check the template parameter types. |
6758 | 11.5k | // The template parameter lists of template template |
6759 | 11.5k | // parameters must agree. |
6760 | 11.5k | if (TemplateTemplateParmDecl *11.5k OldTTP11.5k |
6761 | 918 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
6762 | 918 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
6763 | 918 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
6764 | 918 | OldTTP->getTemplateParameters(), |
6765 | 918 | Complain, |
6766 | 918 | (Kind == Sema::TPL_TemplateMatch |
6767 | 140 | ? Sema::TPL_TemplateTemplateParmMatch |
6768 | 778 | : Kind), |
6769 | 918 | TemplateArgLoc); |
6770 | 918 | } |
6771 | 10.6k | |
6772 | 10.6k | return true; |
6773 | 10.6k | } |
6774 | | |
6775 | | /// \brief Diagnose a known arity mismatch when comparing template argument |
6776 | | /// lists. |
6777 | | static |
6778 | | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
6779 | | TemplateParameterList *New, |
6780 | | TemplateParameterList *Old, |
6781 | | Sema::TemplateParameterListEqualKind Kind, |
6782 | 28 | SourceLocation TemplateArgLoc) { |
6783 | 28 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
6784 | 28 | if (TemplateArgLoc.isValid()28 ) { |
6785 | 18 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
6786 | 18 | NextDiag = diag::note_template_param_list_different_arity; |
6787 | 18 | } |
6788 | 28 | S.Diag(New->getTemplateLoc(), NextDiag) |
6789 | 28 | << (New->size() > Old->size()) |
6790 | 28 | << (Kind != Sema::TPL_TemplateMatch) |
6791 | 28 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
6792 | 28 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
6793 | 28 | << (Kind != Sema::TPL_TemplateMatch) |
6794 | 28 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
6795 | 28 | } |
6796 | | |
6797 | | /// \brief Determine whether the given template parameter lists are |
6798 | | /// equivalent. |
6799 | | /// |
6800 | | /// \param New The new template parameter list, typically written in the |
6801 | | /// source code as part of a new template declaration. |
6802 | | /// |
6803 | | /// \param Old The old template parameter list, typically found via |
6804 | | /// name lookup of the template declared with this template parameter |
6805 | | /// list. |
6806 | | /// |
6807 | | /// \param Complain If true, this routine will produce a diagnostic if |
6808 | | /// the template parameter lists are not equivalent. |
6809 | | /// |
6810 | | /// \param Kind describes how we are to match the template parameter lists. |
6811 | | /// |
6812 | | /// \param TemplateArgLoc If this source location is valid, then we |
6813 | | /// are actually checking the template parameter list of a template |
6814 | | /// argument (New) against the template parameter list of its |
6815 | | /// corresponding template template parameter (Old). We produce |
6816 | | /// slightly different diagnostics in this scenario. |
6817 | | /// |
6818 | | /// \returns True if the template parameter lists are equal, false |
6819 | | /// otherwise. |
6820 | | bool |
6821 | | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
6822 | | TemplateParameterList *Old, |
6823 | | bool Complain, |
6824 | | TemplateParameterListEqualKind Kind, |
6825 | 10.4k | SourceLocation TemplateArgLoc) { |
6826 | 10.4k | if (Old->size() != New->size() && 10.4k Kind != TPL_TemplateTemplateArgumentMatch118 ) { |
6827 | 50 | if (Complain) |
6828 | 10 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
6829 | 10 | TemplateArgLoc); |
6830 | 50 | |
6831 | 50 | return false; |
6832 | 50 | } |
6833 | 10.4k | |
6834 | 10.4k | // C++0x [temp.arg.template]p3: |
6835 | 10.4k | // A template-argument matches a template template-parameter (call it P) |
6836 | 10.4k | // when each of the template parameters in the template-parameter-list of |
6837 | 10.4k | // the template-argument's corresponding class template or alias template |
6838 | 10.4k | // (call it A) matches the corresponding template parameter in the |
6839 | 10.4k | // template-parameter-list of P. [...] |
6840 | 10.4k | TemplateParameterList::iterator NewParm = New->begin(); |
6841 | 10.4k | TemplateParameterList::iterator NewParmEnd = New->end(); |
6842 | 10.4k | for (TemplateParameterList::iterator OldParm = Old->begin(), |
6843 | 10.4k | OldParmEnd = Old->end(); |
6844 | 24.0k | OldParm != OldParmEnd24.0k ; ++OldParm13.6k ) { |
6845 | 13.7k | if (Kind != TPL_TemplateTemplateArgumentMatch || |
6846 | 13.7k | !(*OldParm)->isTemplateParameterPack()4.97k ) { |
6847 | 13.7k | if (NewParm == NewParmEnd13.7k ) { |
6848 | 4 | if (Complain) |
6849 | 4 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
6850 | 4 | TemplateArgLoc); |
6851 | 4 | |
6852 | 4 | return false; |
6853 | 4 | } |
6854 | 13.6k | |
6855 | 13.6k | if (13.6k !MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
6856 | 13.6k | Kind, TemplateArgLoc)) |
6857 | 96 | return false; |
6858 | 13.6k | |
6859 | 13.6k | ++NewParm; |
6860 | 13.6k | continue; |
6861 | 13.6k | } |
6862 | 82 | |
6863 | 82 | // C++0x [temp.arg.template]p3: |
6864 | 82 | // [...] When P's template- parameter-list contains a template parameter |
6865 | 82 | // pack (14.5.3), the template parameter pack will match zero or more |
6866 | 82 | // template parameters or template parameter packs in the |
6867 | 82 | // template-parameter-list of A with the same type and form as the |
6868 | 82 | // template parameter pack in P (ignoring whether those template |
6869 | 82 | // parameters are template parameter packs). |
6870 | 176 | for (; 82 NewParm != NewParmEnd176 ; ++NewParm94 ) { |
6871 | 102 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
6872 | 102 | Kind, TemplateArgLoc)) |
6873 | 8 | return false; |
6874 | 102 | } |
6875 | 13.7k | } |
6876 | 10.4k | |
6877 | 10.4k | // Make sure we exhausted all of the arguments. |
6878 | 10.3k | if (10.3k NewParm != NewParmEnd10.3k ) { |
6879 | 23 | if (Complain) |
6880 | 14 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
6881 | 14 | TemplateArgLoc); |
6882 | 23 | |
6883 | 23 | return false; |
6884 | 23 | } |
6885 | 10.2k | |
6886 | 10.2k | return true; |
6887 | 10.2k | } |
6888 | | |
6889 | | /// \brief Check whether a template can be declared within this scope. |
6890 | | /// |
6891 | | /// If the template declaration is valid in this scope, returns |
6892 | | /// false. Otherwise, issues a diagnostic and returns true. |
6893 | | bool |
6894 | 60.7k | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
6895 | 60.7k | if (!S) |
6896 | 0 | return false; |
6897 | 60.7k | |
6898 | 60.7k | // Find the nearest enclosing declaration scope. |
6899 | 60.7k | while (60.7k (S->getFlags() & Scope::DeclScope) == 0 || |
6900 | 60.7k | (S->getFlags() & Scope::TemplateParamScope) != 0) |
6901 | 23.8k | S = S->getParent(); |
6902 | 60.7k | |
6903 | 60.7k | // C++ [temp]p4: |
6904 | 60.7k | // A template [...] shall not have C linkage. |
6905 | 60.7k | DeclContext *Ctx = S->getEntity(); |
6906 | 60.7k | if (Ctx && 60.7k Ctx->isExternCContext()60.7k ) { |
6907 | 9 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
6908 | 9 | << TemplateParams->getSourceRange(); |
6909 | 9 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
6910 | 9 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
6911 | 9 | return true; |
6912 | 9 | } |
6913 | 60.7k | Ctx = Ctx->getRedeclContext(); |
6914 | 60.7k | |
6915 | 60.7k | // C++ [temp]p2: |
6916 | 60.7k | // A template-declaration can appear only as a namespace scope or |
6917 | 60.7k | // class scope declaration. |
6918 | 60.7k | if (Ctx60.7k ) { |
6919 | 60.7k | if (Ctx->isFileContext()) |
6920 | 49.9k | return false; |
6921 | 10.8k | if (CXXRecordDecl *10.8k RD10.8k = dyn_cast<CXXRecordDecl>(Ctx)) { |
6922 | 10.8k | // C++ [temp.mem]p2: |
6923 | 10.8k | // A local class shall not have member templates. |
6924 | 10.8k | if (RD->isLocalClass()) |
6925 | 5 | return Diag(TemplateParams->getTemplateLoc(), |
6926 | 5 | diag::err_template_inside_local_class) |
6927 | 5 | << TemplateParams->getSourceRange(); |
6928 | 10.8k | else |
6929 | 10.8k | return false; |
6930 | 0 | } |
6931 | 60.7k | } |
6932 | 0 |
|
6933 | 0 | return Diag(TemplateParams->getTemplateLoc(), |
6934 | 0 | diag::err_template_outside_namespace_or_class_scope) |
6935 | 0 | << TemplateParams->getSourceRange(); |
6936 | 0 | } |
6937 | | |
6938 | | /// \brief Determine what kind of template specialization the given declaration |
6939 | | /// is. |
6940 | 4.56k | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
6941 | 4.56k | if (!D) |
6942 | 0 | return TSK_Undeclared; |
6943 | 4.56k | |
6944 | 4.56k | if (CXXRecordDecl *4.56k Record4.56k = dyn_cast<CXXRecordDecl>(D)) |
6945 | 615 | return Record->getTemplateSpecializationKind(); |
6946 | 3.94k | if (FunctionDecl *3.94k Function3.94k = dyn_cast<FunctionDecl>(D)) |
6947 | 3.48k | return Function->getTemplateSpecializationKind(); |
6948 | 459 | if (VarDecl *459 Var459 = dyn_cast<VarDecl>(D)) |
6949 | 418 | return Var->getTemplateSpecializationKind(); |
6950 | 41 | |
6951 | 41 | return TSK_Undeclared; |
6952 | 41 | } |
6953 | | |
6954 | | /// \brief Check whether a specialization is well-formed in the current |
6955 | | /// context. |
6956 | | /// |
6957 | | /// This routine determines whether a template specialization can be declared |
6958 | | /// in the current context (C++ [temp.expl.spec]p2). |
6959 | | /// |
6960 | | /// \param S the semantic analysis object for which this check is being |
6961 | | /// performed. |
6962 | | /// |
6963 | | /// \param Specialized the entity being specialized or instantiated, which |
6964 | | /// may be a kind of template (class template, function template, etc.) or |
6965 | | /// a member of a class template (member function, static data member, |
6966 | | /// member class). |
6967 | | /// |
6968 | | /// \param PrevDecl the previous declaration of this entity, if any. |
6969 | | /// |
6970 | | /// \param Loc the location of the explicit specialization or instantiation of |
6971 | | /// this entity. |
6972 | | /// |
6973 | | /// \param IsPartialSpecialization whether this is a partial specialization of |
6974 | | /// a class template. |
6975 | | /// |
6976 | | /// \returns true if there was an error that we cannot recover from, false |
6977 | | /// otherwise. |
6978 | | static bool CheckTemplateSpecializationScope(Sema &S, |
6979 | | NamedDecl *Specialized, |
6980 | | NamedDecl *PrevDecl, |
6981 | | SourceLocation Loc, |
6982 | 8.33k | bool IsPartialSpecialization) { |
6983 | 8.33k | // Keep these "kind" numbers in sync with the %select statements in the |
6984 | 8.33k | // various diagnostics emitted by this routine. |
6985 | 8.33k | int EntityKind = 0; |
6986 | 8.33k | if (isa<ClassTemplateDecl>(Specialized)) |
6987 | 5.89k | EntityKind = IsPartialSpecialization? 5.89k 13.88k : 02.01k ; |
6988 | 2.43k | else if (2.43k isa<VarTemplateDecl>(Specialized)2.43k ) |
6989 | 523 | EntityKind = IsPartialSpecialization ? 523 3214 : 2309 ; |
6990 | 1.91k | else if (1.91k isa<FunctionTemplateDecl>(Specialized)1.91k ) |
6991 | 1.19k | EntityKind = 4; |
6992 | 725 | else if (725 isa<CXXMethodDecl>(Specialized)725 ) |
6993 | 514 | EntityKind = 5; |
6994 | 211 | else if (211 isa<VarDecl>(Specialized)211 ) |
6995 | 99 | EntityKind = 6; |
6996 | 112 | else if (112 isa<RecordDecl>(Specialized)112 ) |
6997 | 73 | EntityKind = 7; |
6998 | 39 | else if (39 isa<EnumDecl>(Specialized) && 39 S.getLangOpts().CPlusPlus1139 ) |
6999 | 39 | EntityKind = 8; |
7000 | 0 | else { |
7001 | 0 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
7002 | 0 | << S.getLangOpts().CPlusPlus11; |
7003 | 0 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
7004 | 0 | return true; |
7005 | 0 | } |
7006 | 8.33k | |
7007 | 8.33k | // C++ [temp.expl.spec]p2: |
7008 | 8.33k | // An explicit specialization shall be declared in the namespace |
7009 | 8.33k | // of which the template is a member, or, for member templates, in |
7010 | 8.33k | // the namespace of which the enclosing class or enclosing class |
7011 | 8.33k | // template is a member. An explicit specialization of a member |
7012 | 8.33k | // function, member class or static data member of a class |
7013 | 8.33k | // template shall be declared in the namespace of which the class |
7014 | 8.33k | // template is a member. Such a declaration may also be a |
7015 | 8.33k | // definition. If the declaration is not a definition, the |
7016 | 8.33k | // specialization may be defined later in the name- space in which |
7017 | 8.33k | // the explicit specialization was declared, or in a namespace |
7018 | 8.33k | // that encloses the one in which the explicit specialization was |
7019 | 8.33k | // declared. |
7020 | 8.33k | if (8.33k S.CurContext->getRedeclContext()->isFunctionOrMethod()8.33k ) { |
7021 | 0 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
7022 | 0 | << Specialized; |
7023 | 0 | return true; |
7024 | 0 | } |
7025 | 8.33k | |
7026 | 8.33k | if (8.33k S.CurContext->isRecord() && 8.33k !IsPartialSpecialization199 ) { |
7027 | 64 | if (S.getLangOpts().MicrosoftExt64 ) { |
7028 | 43 | // Do not warn for class scope explicit specialization during |
7029 | 43 | // instantiation, warning was already emitted during pattern |
7030 | 43 | // semantic analysis. |
7031 | 43 | if (!S.inTemplateInstantiation()) |
7032 | 31 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
7033 | 31 | << Specialized; |
7034 | 64 | } else { |
7035 | 21 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
7036 | 21 | << Specialized; |
7037 | 21 | return true; |
7038 | 21 | } |
7039 | 8.31k | } |
7040 | 8.31k | |
7041 | 8.31k | if (8.31k S.CurContext->isRecord() && |
7042 | 8.31k | !S.CurContext->Equals(Specialized->getDeclContext())178 ) { |
7043 | 3 | // Make sure that we're specializing in the right record context. |
7044 | 3 | // Otherwise, things can go horribly wrong. |
7045 | 3 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
7046 | 3 | << Specialized; |
7047 | 3 | return true; |
7048 | 3 | } |
7049 | 8.31k | |
7050 | 8.31k | // C++ [temp.class.spec]p6: |
7051 | 8.31k | // A class template partial specialization may be declared or redeclared |
7052 | 8.31k | // in any namespace scope in which its definition may be defined (14.5.1 |
7053 | 8.31k | // and 14.5.2). |
7054 | 8.31k | DeclContext *SpecializedContext |
7055 | 8.31k | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
7056 | 8.31k | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
7057 | 8.31k | |
7058 | 8.31k | // Make sure that this redeclaration (or definition) occurs in an enclosing |
7059 | 8.31k | // namespace. |
7060 | 8.31k | // Note that HandleDeclarator() performs this check for explicit |
7061 | 8.31k | // specializations of function templates, static data members, and member |
7062 | 8.31k | // functions, so we skip the check here for those kinds of entities. |
7063 | 8.31k | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
7064 | 8.31k | // Should we refactor that check, so that it occurs later? |
7065 | 8.31k | if (!DC->Encloses(SpecializedContext) && |
7066 | 56 | !(isa<FunctionTemplateDecl>(Specialized) || |
7067 | 48 | isa<FunctionDecl>(Specialized) || |
7068 | 48 | isa<VarTemplateDecl>(Specialized) || |
7069 | 8.31k | isa<VarDecl>(Specialized)32 )) { |
7070 | 32 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
7071 | 9 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
7072 | 9 | << EntityKind << Specialized; |
7073 | 23 | else if (23 isa<NamespaceDecl>(SpecializedContext)23 ) { |
7074 | 23 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
7075 | 23 | if (S.getLangOpts().MicrosoftExt) |
7076 | 4 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
7077 | 23 | S.Diag(Loc, Diag) << EntityKind << Specialized |
7078 | 23 | << cast<NamedDecl>(SpecializedContext); |
7079 | 23 | } else |
7080 | 0 | llvm_unreachable("unexpected namespace context for specialization"); |
7081 | 32 | |
7082 | 32 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
7083 | 8.31k | } else if (8.28k (!PrevDecl || |
7084 | 2.17k | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
7085 | 2.12k | getTemplateSpecializationKind(PrevDecl) == |
7086 | 8.28k | TSK_ImplicitInstantiation)) { |
7087 | 7.93k | // C++ [temp.exp.spec]p2: |
7088 | 7.93k | // An explicit specialization shall be declared in the namespace of which |
7089 | 7.93k | // the template is a member, or, for member templates, in the namespace |
7090 | 7.93k | // of which the enclosing class or enclosing class template is a member. |
7091 | 7.93k | // An explicit specialization of a member function, member class or |
7092 | 7.93k | // static data member of a class template shall be declared in the |
7093 | 7.93k | // namespace of which the class template is a member. |
7094 | 7.93k | // |
7095 | 7.93k | // C++11 [temp.expl.spec]p2: |
7096 | 7.93k | // An explicit specialization shall be declared in a namespace enclosing |
7097 | 7.93k | // the specialized template. |
7098 | 7.93k | // C++11 [temp.explicit]p3: |
7099 | 7.93k | // An explicit instantiation shall appear in an enclosing namespace of its |
7100 | 7.93k | // template. |
7101 | 7.93k | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)7.93k ) { |
7102 | 154 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
7103 | 154 | if (isa<TranslationUnitDecl>(SpecializedContext)154 ) { |
7104 | 4 | assert(!IsCPlusPlus11Extension && |
7105 | 4 | "DC encloses TU but isn't in enclosing namespace set"); |
7106 | 4 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
7107 | 4 | << EntityKind << Specialized; |
7108 | 154 | } else if (150 isa<NamespaceDecl>(SpecializedContext)150 ) { |
7109 | 150 | int Diag; |
7110 | 150 | if (!IsCPlusPlus11Extension) |
7111 | 8 | Diag = diag::err_template_spec_decl_out_of_scope; |
7112 | 142 | else if (142 !S.getLangOpts().CPlusPlus11142 ) |
7113 | 25 | Diag = diag::ext_template_spec_decl_out_of_scope; |
7114 | 142 | else |
7115 | 117 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
7116 | 150 | S.Diag(Loc, Diag) |
7117 | 150 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
7118 | 150 | } |
7119 | 154 | |
7120 | 154 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
7121 | 154 | } |
7122 | 8.28k | } |
7123 | 8.31k | |
7124 | 8.31k | return false; |
7125 | 8.33k | } |
7126 | | |
7127 | 54 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
7128 | 54 | if (!E->isTypeDependent()) |
7129 | 46 | return SourceLocation(); |
7130 | 8 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
7131 | 8 | Checker.TraverseStmt(E); |
7132 | 8 | if (Checker.MatchLoc.isInvalid()) |
7133 | 3 | return E->getSourceRange(); |
7134 | 5 | return Checker.MatchLoc; |
7135 | 5 | } |
7136 | | |
7137 | 46 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
7138 | 46 | if (!TL.getType()->isDependentType()) |
7139 | 36 | return SourceLocation(); |
7140 | 10 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
7141 | 10 | Checker.TraverseTypeLoc(TL); |
7142 | 10 | if (Checker.MatchLoc.isInvalid()) |
7143 | 3 | return TL.getSourceRange(); |
7144 | 7 | return Checker.MatchLoc; |
7145 | 7 | } |
7146 | | |
7147 | | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
7148 | | /// that checks non-type template partial specialization arguments. |
7149 | | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
7150 | | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
7151 | 1.10k | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
7152 | 2.19k | for (unsigned I = 0; I != NumArgs2.19k ; ++I1.08k ) { |
7153 | 1.10k | if (Args[I].getKind() == TemplateArgument::Pack1.10k ) { |
7154 | 13 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
7155 | 13 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
7156 | 13 | Args[I].pack_size(), IsDefaultArgument)) |
7157 | 0 | return true; |
7158 | 13 | |
7159 | 13 | continue; |
7160 | 13 | } |
7161 | 1.09k | |
7162 | 1.09k | if (1.09k Args[I].getKind() != TemplateArgument::Expression1.09k ) |
7163 | 442 | continue; |
7164 | 652 | |
7165 | 652 | Expr *ArgExpr = Args[I].getAsExpr(); |
7166 | 652 | |
7167 | 652 | // We can have a pack expansion of any of the bullets below. |
7168 | 652 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
7169 | 9 | ArgExpr = Expansion->getPattern(); |
7170 | 652 | |
7171 | 652 | // Strip off any implicit casts we added as part of type checking. |
7172 | 668 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
7173 | 16 | ArgExpr = ICE->getSubExpr(); |
7174 | 652 | |
7175 | 652 | // C++ [temp.class.spec]p8: |
7176 | 652 | // A non-type argument is non-specialized if it is the name of a |
7177 | 652 | // non-type parameter. All other non-type arguments are |
7178 | 652 | // specialized. |
7179 | 652 | // |
7180 | 652 | // Below, we check the two conditions that only apply to |
7181 | 652 | // specialized non-type arguments, so skip any non-specialized |
7182 | 652 | // arguments. |
7183 | 652 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
7184 | 598 | if (598 isa<NonTypeTemplateParmDecl>(DRE->getDecl())598 ) |
7185 | 598 | continue; |
7186 | 54 | |
7187 | 54 | // C++ [temp.class.spec]p9: |
7188 | 54 | // Within the argument list of a class template partial |
7189 | 54 | // specialization, the following restrictions apply: |
7190 | 54 | // -- A partially specialized non-type argument expression |
7191 | 54 | // shall not involve a template parameter of the partial |
7192 | 54 | // specialization except when the argument expression is a |
7193 | 54 | // simple identifier. |
7194 | 54 | // -- The type of a template parameter corresponding to a |
7195 | 54 | // specialized non-type argument shall not be dependent on a |
7196 | 54 | // parameter of the specialization. |
7197 | 54 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
7198 | 54 | // We implement a compromise between the original rules and DR1315: |
7199 | 54 | // -- A specialized non-type template argument shall not be |
7200 | 54 | // type-dependent and the corresponding template parameter |
7201 | 54 | // shall have a non-dependent type. |
7202 | 54 | SourceRange ParamUseRange = |
7203 | 54 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
7204 | 54 | if (ParamUseRange.isValid()54 ) { |
7205 | 8 | if (IsDefaultArgument8 ) { |
7206 | 0 | S.Diag(TemplateNameLoc, |
7207 | 0 | diag::err_dependent_non_type_arg_in_partial_spec); |
7208 | 0 | S.Diag(ParamUseRange.getBegin(), |
7209 | 0 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
7210 | 0 | << ParamUseRange; |
7211 | 8 | } else { |
7212 | 8 | S.Diag(ParamUseRange.getBegin(), |
7213 | 8 | diag::err_dependent_non_type_arg_in_partial_spec) |
7214 | 8 | << ParamUseRange; |
7215 | 8 | } |
7216 | 8 | return true; |
7217 | 8 | } |
7218 | 46 | |
7219 | 46 | ParamUseRange = findTemplateParameter( |
7220 | 46 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
7221 | 46 | if (ParamUseRange.isValid()46 ) { |
7222 | 10 | S.Diag(IsDefaultArgument ? TemplateNameLoc3 : ArgExpr->getLocStart()7 , |
7223 | 10 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
7224 | 10 | << Param->getType(); |
7225 | 10 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
7226 | 10 | << (IsDefaultArgument ? ParamUseRange3 : SourceRange()7 ) |
7227 | 10 | << ParamUseRange; |
7228 | 10 | return true; |
7229 | 10 | } |
7230 | 1.10k | } |
7231 | 1.10k | |
7232 | 1.08k | return false; |
7233 | 1.10k | } |
7234 | | |
7235 | | /// \brief Check the non-type template arguments of a class template |
7236 | | /// partial specialization according to C++ [temp.class.spec]p9. |
7237 | | /// |
7238 | | /// \param TemplateNameLoc the location of the template name. |
7239 | | /// \param PrimaryTemplate the template parameters of the primary class |
7240 | | /// template. |
7241 | | /// \param NumExplicit the number of explicitly-specified template arguments. |
7242 | | /// \param TemplateArgs the template arguments of the class template |
7243 | | /// partial specialization. |
7244 | | /// |
7245 | | /// \returns \c true if there was an error, \c false otherwise. |
7246 | | bool Sema::CheckTemplatePartialSpecializationArgs( |
7247 | | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
7248 | 4.20k | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
7249 | 4.20k | // We have to be conservative when checking a template in a dependent |
7250 | 4.20k | // context. |
7251 | 4.20k | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
7252 | 112 | return false; |
7253 | 4.09k | |
7254 | 4.09k | TemplateParameterList *TemplateParams = |
7255 | 4.09k | PrimaryTemplate->getTemplateParameters(); |
7256 | 12.4k | for (unsigned I = 0, N = TemplateParams->size(); I != N12.4k ; ++I8.31k ) { |
7257 | 8.33k | NonTypeTemplateParmDecl *Param |
7258 | 8.33k | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
7259 | 8.33k | if (!Param) |
7260 | 7.24k | continue; |
7261 | 1.08k | |
7262 | 1.08k | if (1.08k CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
7263 | 1.08k | Param, &TemplateArgs[I], |
7264 | 1.08k | 1, I >= NumExplicit)) |
7265 | 18 | return true; |
7266 | 8.33k | } |
7267 | 4.09k | |
7268 | 4.07k | return false; |
7269 | 4.20k | } |
7270 | | |
7271 | | DeclResult |
7272 | | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
7273 | | TagUseKind TUK, |
7274 | | SourceLocation KWLoc, |
7275 | | SourceLocation ModulePrivateLoc, |
7276 | | TemplateIdAnnotation &TemplateId, |
7277 | | AttributeList *Attr, |
7278 | | MultiTemplateParamsArg |
7279 | | TemplateParameterLists, |
7280 | 5.93k | SkipBodyInfo *SkipBody) { |
7281 | 5.93k | assert(TUK != TUK_Reference && "References are not specializations"); |
7282 | 5.93k | |
7283 | 5.93k | CXXScopeSpec &SS = TemplateId.SS; |
7284 | 5.93k | |
7285 | 5.93k | // NOTE: KWLoc is the location of the tag keyword. This will instead |
7286 | 5.93k | // store the location of the outermost template keyword in the declaration. |
7287 | 5.93k | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
7288 | 5.93k | ? TemplateParameterLists[0]->getTemplateLoc()5.91k : KWLoc14 ; |
7289 | 5.93k | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
7290 | 5.93k | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
7291 | 5.93k | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
7292 | 5.93k | |
7293 | 5.93k | // Find the class template we're specializing |
7294 | 5.93k | TemplateName Name = TemplateId.Template.get(); |
7295 | 5.93k | ClassTemplateDecl *ClassTemplate |
7296 | 5.93k | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
7297 | 5.93k | |
7298 | 5.93k | if (!ClassTemplate5.93k ) { |
7299 | 6 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
7300 | 6 | << (Name.getAsTemplateDecl() && |
7301 | 6 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
7302 | 6 | return true; |
7303 | 6 | } |
7304 | 5.92k | |
7305 | 5.92k | bool isMemberSpecialization = false; |
7306 | 5.92k | bool isPartialSpecialization = false; |
7307 | 5.92k | |
7308 | 5.92k | // Check the validity of the template headers that introduce this |
7309 | 5.92k | // template. |
7310 | 5.92k | // FIXME: We probably shouldn't complain about these headers for |
7311 | 5.92k | // friend declarations. |
7312 | 5.92k | bool Invalid = false; |
7313 | 5.92k | TemplateParameterList *TemplateParams = |
7314 | 5.92k | MatchTemplateParametersToScopeSpecifier( |
7315 | 5.92k | KWLoc, TemplateNameLoc, SS, &TemplateId, |
7316 | 5.92k | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
7317 | 5.92k | Invalid); |
7318 | 5.92k | if (Invalid) |
7319 | 0 | return true; |
7320 | 5.92k | |
7321 | 5.92k | if (5.92k TemplateParams && 5.92k TemplateParams->size() > 05.92k ) { |
7322 | 3.90k | isPartialSpecialization = true; |
7323 | 3.90k | |
7324 | 3.90k | if (TUK == TUK_Friend3.90k ) { |
7325 | 2 | Diag(KWLoc, diag::err_partial_specialization_friend) |
7326 | 2 | << SourceRange(LAngleLoc, RAngleLoc); |
7327 | 2 | return true; |
7328 | 2 | } |
7329 | 3.90k | |
7330 | 3.90k | // C++ [temp.class.spec]p10: |
7331 | 3.90k | // The template parameter list of a specialization shall not |
7332 | 3.90k | // contain default template argument values. |
7333 | 16.0k | for (unsigned I = 0, N = TemplateParams->size(); 3.90k I != N16.0k ; ++I12.1k ) { |
7334 | 12.1k | Decl *Param = TemplateParams->getParam(I); |
7335 | 12.1k | if (TemplateTypeParmDecl *TTP12.1k = dyn_cast<TemplateTypeParmDecl>(Param)) { |
7336 | 9.33k | if (TTP->hasDefaultArgument()9.33k ) { |
7337 | 3 | Diag(TTP->getDefaultArgumentLoc(), |
7338 | 3 | diag::err_default_arg_in_partial_spec); |
7339 | 3 | TTP->removeDefaultArgument(); |
7340 | 3 | } |
7341 | 12.1k | } else if (NonTypeTemplateParmDecl *2.77k NTTP2.77k |
7342 | 2.71k | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
7343 | 2.71k | if (Expr *DefArg2.71k = NTTP->getDefaultArgument()) { |
7344 | 3 | Diag(NTTP->getDefaultArgumentLoc(), |
7345 | 3 | diag::err_default_arg_in_partial_spec) |
7346 | 3 | << DefArg->getSourceRange(); |
7347 | 3 | NTTP->removeDefaultArgument(); |
7348 | 3 | } |
7349 | 2.77k | } else { |
7350 | 62 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
7351 | 62 | if (TTP->hasDefaultArgument()62 ) { |
7352 | 3 | Diag(TTP->getDefaultArgument().getLocation(), |
7353 | 3 | diag::err_default_arg_in_partial_spec) |
7354 | 3 | << TTP->getDefaultArgument().getSourceRange(); |
7355 | 3 | TTP->removeDefaultArgument(); |
7356 | 3 | } |
7357 | 2.77k | } |
7358 | 12.1k | } |
7359 | 5.92k | } else if (2.02k TemplateParams2.02k ) { |
7360 | 2.02k | if (TUK == TUK_Friend) |
7361 | 1 | Diag(KWLoc, diag::err_template_spec_friend) |
7362 | 1 | << FixItHint::CreateRemoval( |
7363 | 1 | SourceRange(TemplateParams->getTemplateLoc(), |
7364 | 1 | TemplateParams->getRAngleLoc())) |
7365 | 1 | << SourceRange(LAngleLoc, RAngleLoc); |
7366 | 0 | } else { |
7367 | 0 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
7368 | 0 | } |
7369 | 5.92k | |
7370 | 5.92k | // Check that the specialization uses the same tag kind as the |
7371 | 5.92k | // original template. |
7372 | 5.92k | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
7373 | 5.92k | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
7374 | 5.92k | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
7375 | 5.92k | Kind, TUK == TUK_Definition, KWLoc, |
7376 | 5.92k | ClassTemplate->getIdentifier())) { |
7377 | 5 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
7378 | 5 | << ClassTemplate |
7379 | 5 | << FixItHint::CreateReplacement(KWLoc, |
7380 | 5 | ClassTemplate->getTemplatedDecl()->getKindName()); |
7381 | 5 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
7382 | 5 | diag::note_previous_use); |
7383 | 5 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
7384 | 5 | } |
7385 | 5.92k | |
7386 | 5.92k | // Translate the parser's template argument list in our AST format. |
7387 | 5.92k | TemplateArgumentListInfo TemplateArgs = |
7388 | 5.92k | makeTemplateArgumentListInfo(*this, TemplateId); |
7389 | 5.92k | |
7390 | 5.92k | // Check for unexpanded parameter packs in any of the template arguments. |
7391 | 16.7k | for (unsigned I = 0, N = TemplateArgs.size(); I != N16.7k ; ++I10.8k ) |
7392 | 10.8k | if (10.8k DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
7393 | 10.8k | UPPC_PartialSpecialization)) |
7394 | 1 | return true; |
7395 | 5.92k | |
7396 | 5.92k | // Check that the template argument list is well-formed for this |
7397 | 5.92k | // template. |
7398 | 5.92k | SmallVector<TemplateArgument, 4> Converted; |
7399 | 5.92k | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
7400 | 5.92k | TemplateArgs, false, Converted)) |
7401 | 11 | return true; |
7402 | 5.91k | |
7403 | 5.91k | // Find the class template (partial) specialization declaration that |
7404 | 5.91k | // corresponds to these arguments. |
7405 | 5.91k | if (5.91k isPartialSpecialization5.91k ) { |
7406 | 3.89k | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
7407 | 3.89k | TemplateArgs.size(), Converted)) |
7408 | 13 | return true; |
7409 | 3.88k | |
7410 | 3.88k | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
7411 | 3.88k | // also do it during instantiation. |
7412 | 3.88k | bool InstantiationDependent; |
7413 | 3.88k | if (!Name.isDependent() && |
7414 | 3.81k | !TemplateSpecializationType::anyDependentTemplateArguments( |
7415 | 3.88k | TemplateArgs.arguments(), InstantiationDependent)) { |
7416 | 1 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
7417 | 1 | << ClassTemplate->getDeclName(); |
7418 | 1 | isPartialSpecialization = false; |
7419 | 1 | } |
7420 | 3.89k | } |
7421 | 5.91k | |
7422 | 5.89k | void *InsertPos = nullptr; |
7423 | 5.89k | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
7424 | 5.89k | |
7425 | 5.89k | if (isPartialSpecialization) |
7426 | 5.89k | // FIXME: Template parameter list matters, too |
7427 | 3.88k | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
7428 | 5.89k | else |
7429 | 2.01k | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
7430 | 5.89k | |
7431 | 5.89k | ClassTemplateSpecializationDecl *Specialization = nullptr; |
7432 | 5.89k | |
7433 | 5.89k | // Check whether we can declare a class template specialization in |
7434 | 5.89k | // the current scope. |
7435 | 5.89k | if (TUK != TUK_Friend && |
7436 | 5.89k | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
7437 | 5.89k | TemplateNameLoc, |
7438 | 5.89k | isPartialSpecialization)) |
7439 | 3 | return true; |
7440 | 5.89k | |
7441 | 5.89k | // The canonical type |
7442 | 5.89k | QualType CanonType; |
7443 | 5.89k | if (isPartialSpecialization5.89k ) { |
7444 | 3.87k | // Build the canonical type that describes the converted template |
7445 | 3.87k | // arguments of the class template partial specialization. |
7446 | 3.87k | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
7447 | 3.87k | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
7448 | 3.87k | Converted); |
7449 | 3.87k | |
7450 | 3.87k | if (Context.hasSameType(CanonType, |
7451 | 3.87k | ClassTemplate->getInjectedClassNameSpecialization())) { |
7452 | 4 | // C++ [temp.class.spec]p9b3: |
7453 | 4 | // |
7454 | 4 | // -- The argument list of the specialization shall not be identical |
7455 | 4 | // to the implicit argument list of the primary template. |
7456 | 4 | // |
7457 | 4 | // This rule has since been removed, because it's redundant given DR1495, |
7458 | 4 | // but we keep it because it produces better diagnostics and recovery. |
7459 | 4 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
7460 | 4 | << /*class template*/0 << (TUK == TUK_Definition) |
7461 | 4 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
7462 | 4 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
7463 | 4 | ClassTemplate->getIdentifier(), |
7464 | 4 | TemplateNameLoc, |
7465 | 4 | Attr, |
7466 | 4 | TemplateParams, |
7467 | 4 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
7468 | 4 | /*FriendLoc*/SourceLocation(), |
7469 | 4 | TemplateParameterLists.size() - 1, |
7470 | 4 | TemplateParameterLists.data()); |
7471 | 4 | } |
7472 | 3.87k | |
7473 | 3.87k | // Create a new class template partial specialization declaration node. |
7474 | 3.87k | ClassTemplatePartialSpecializationDecl *PrevPartial |
7475 | 3.87k | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
7476 | 3.87k | ClassTemplatePartialSpecializationDecl *Partial |
7477 | 3.87k | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
7478 | 3.87k | ClassTemplate->getDeclContext(), |
7479 | 3.87k | KWLoc, TemplateNameLoc, |
7480 | 3.87k | TemplateParams, |
7481 | 3.87k | ClassTemplate, |
7482 | 3.87k | Converted, |
7483 | 3.87k | TemplateArgs, |
7484 | 3.87k | CanonType, |
7485 | 3.87k | PrevPartial); |
7486 | 3.87k | SetNestedNameSpecifier(Partial, SS); |
7487 | 3.87k | if (TemplateParameterLists.size() > 1 && 3.87k SS.isSet()14 ) { |
7488 | 14 | Partial->setTemplateParameterListsInfo( |
7489 | 14 | Context, TemplateParameterLists.drop_back(1)); |
7490 | 14 | } |
7491 | 3.87k | |
7492 | 3.87k | if (!PrevPartial) |
7493 | 3.82k | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
7494 | 3.87k | Specialization = Partial; |
7495 | 3.87k | |
7496 | 3.87k | // If we are providing an explicit specialization of a member class |
7497 | 3.87k | // template specialization, make a note of that. |
7498 | 3.87k | if (PrevPartial && 3.87k PrevPartial->getInstantiatedFromMember()48 ) |
7499 | 1 | PrevPartial->setMemberSpecialization(); |
7500 | 3.87k | |
7501 | 3.87k | CheckTemplatePartialSpecialization(Partial); |
7502 | 5.89k | } else { |
7503 | 2.01k | // Create a new class template specialization declaration node for |
7504 | 2.01k | // this explicit specialization or friend declaration. |
7505 | 2.01k | Specialization |
7506 | 2.01k | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
7507 | 2.01k | ClassTemplate->getDeclContext(), |
7508 | 2.01k | KWLoc, TemplateNameLoc, |
7509 | 2.01k | ClassTemplate, |
7510 | 2.01k | Converted, |
7511 | 2.01k | PrevDecl); |
7512 | 2.01k | SetNestedNameSpecifier(Specialization, SS); |
7513 | 2.01k | if (TemplateParameterLists.size() > 02.01k ) { |
7514 | 2.00k | Specialization->setTemplateParameterListsInfo(Context, |
7515 | 2.00k | TemplateParameterLists); |
7516 | 2.00k | } |
7517 | 2.01k | |
7518 | 2.01k | if (!PrevDecl) |
7519 | 1.86k | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
7520 | 2.01k | |
7521 | 2.01k | if (CurContext->isDependentContext()2.01k ) { |
7522 | 13 | // -fms-extensions permits specialization of nested classes without |
7523 | 13 | // fully specializing the outer class(es). |
7524 | 13 | assert(getLangOpts().MicrosoftExt && |
7525 | 13 | "Only possible with -fms-extensions!"); |
7526 | 13 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
7527 | 13 | CanonType = Context.getTemplateSpecializationType( |
7528 | 13 | CanonTemplate, Converted); |
7529 | 2.01k | } else { |
7530 | 2.00k | CanonType = Context.getTypeDeclType(Specialization); |
7531 | 2.00k | } |
7532 | 2.01k | } |
7533 | 5.89k | |
7534 | 5.89k | // C++ [temp.expl.spec]p6: |
7535 | 5.89k | // If a template, a member template or the member of a class template is |
7536 | 5.89k | // explicitly specialized then that specialization shall be declared |
7537 | 5.89k | // before the first use of that specialization that would cause an implicit |
7538 | 5.89k | // instantiation to take place, in every translation unit in which such a |
7539 | 5.89k | // use occurs; no diagnostic is required. |
7540 | 5.89k | if (5.89k PrevDecl && 5.89k PrevDecl->getPointOfInstantiation().isValid()201 ) { |
7541 | 14 | bool Okay = false; |
7542 | 30 | for (Decl *Prev = PrevDecl; Prev30 ; Prev = Prev->getPreviousDecl()16 ) { |
7543 | 16 | // Is there any previous explicit specialization declaration? |
7544 | 16 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization16 ) { |
7545 | 0 | Okay = true; |
7546 | 0 | break; |
7547 | 0 | } |
7548 | 16 | } |
7549 | 14 | |
7550 | 14 | if (!Okay14 ) { |
7551 | 14 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
7552 | 14 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
7553 | 14 | << Context.getTypeDeclType(Specialization) << Range; |
7554 | 14 | |
7555 | 14 | Diag(PrevDecl->getPointOfInstantiation(), |
7556 | 14 | diag::note_instantiation_required_here) |
7557 | 14 | << (PrevDecl->getTemplateSpecializationKind() |
7558 | 14 | != TSK_ImplicitInstantiation); |
7559 | 14 | return true; |
7560 | 14 | } |
7561 | 5.87k | } |
7562 | 5.87k | |
7563 | 5.87k | // If this is not a friend, note that this is an explicit specialization. |
7564 | 5.87k | if (5.87k TUK != TUK_Friend5.87k ) |
7565 | 5.87k | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
7566 | 5.87k | |
7567 | 5.87k | // Check that this isn't a redefinition of this specialization. |
7568 | 5.87k | if (TUK == TUK_Definition5.87k ) { |
7569 | 5.67k | RecordDecl *Def = Specialization->getDefinition(); |
7570 | 5.67k | NamedDecl *Hidden = nullptr; |
7571 | 5.67k | if (Def && 5.67k SkipBody65 && !hasVisibleDefinition(Def, &Hidden)65 ) { |
7572 | 46 | SkipBody->ShouldSkip = true; |
7573 | 46 | makeMergedDefinitionVisible(Hidden); |
7574 | 46 | // From here on out, treat this as just a redeclaration. |
7575 | 46 | TUK = TUK_Declaration; |
7576 | 5.67k | } else if (5.63k Def5.63k ) { |
7577 | 19 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
7578 | 19 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
7579 | 19 | Diag(Def->getLocation(), diag::note_previous_definition); |
7580 | 19 | Specialization->setInvalidDecl(); |
7581 | 19 | return true; |
7582 | 19 | } |
7583 | 5.85k | } |
7584 | 5.85k | |
7585 | 5.85k | if (5.85k Attr5.85k ) |
7586 | 135 | ProcessDeclAttributeList(S, Specialization, Attr); |
7587 | 5.85k | |
7588 | 5.85k | // Add alignment attributes if necessary; these attributes are checked when |
7589 | 5.85k | // the ASTContext lays out the structure. |
7590 | 5.85k | if (TUK == TUK_Definition5.85k ) { |
7591 | 5.61k | AddAlignmentAttributesForRecord(Specialization); |
7592 | 5.61k | AddMsStructLayoutForRecord(Specialization); |
7593 | 5.61k | } |
7594 | 5.85k | |
7595 | 5.85k | if (ModulePrivateLoc.isValid()) |
7596 | 2 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
7597 | 2 | << (isPartialSpecialization? 11 : 01 ) |
7598 | 2 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
7599 | 5.85k | |
7600 | 5.85k | // Build the fully-sugared type for this class template |
7601 | 5.85k | // specialization as the user wrote in the specialization |
7602 | 5.85k | // itself. This means that we'll pretty-print the type retrieved |
7603 | 5.85k | // from the specialization's declaration the way that the user |
7604 | 5.85k | // actually wrote the specialization, rather than formatting the |
7605 | 5.85k | // name based on the "canonical" representation used to store the |
7606 | 5.85k | // template arguments in the specialization. |
7607 | 5.85k | TypeSourceInfo *WrittenTy |
7608 | 5.85k | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
7609 | 5.85k | TemplateArgs, CanonType); |
7610 | 5.85k | if (TUK != TUK_Friend5.85k ) { |
7611 | 5.85k | Specialization->setTypeAsWritten(WrittenTy); |
7612 | 5.85k | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
7613 | 5.85k | } |
7614 | 5.85k | |
7615 | 5.85k | // C++ [temp.expl.spec]p9: |
7616 | 5.85k | // A template explicit specialization is in the scope of the |
7617 | 5.85k | // namespace in which the template was defined. |
7618 | 5.85k | // |
7619 | 5.85k | // We actually implement this paragraph where we set the semantic |
7620 | 5.85k | // context (in the creation of the ClassTemplateSpecializationDecl), |
7621 | 5.85k | // but we also maintain the lexical context where the actual |
7622 | 5.85k | // definition occurs. |
7623 | 5.85k | Specialization->setLexicalDeclContext(CurContext); |
7624 | 5.85k | |
7625 | 5.85k | // We may be starting the definition of this specialization. |
7626 | 5.85k | if (TUK == TUK_Definition) |
7627 | 5.61k | Specialization->startDefinition(); |
7628 | 5.85k | |
7629 | 5.85k | if (TUK == TUK_Friend5.85k ) { |
7630 | 1 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
7631 | 1 | TemplateNameLoc, |
7632 | 1 | WrittenTy, |
7633 | 1 | /*FIXME:*/KWLoc); |
7634 | 1 | Friend->setAccess(AS_public); |
7635 | 1 | CurContext->addDecl(Friend); |
7636 | 5.85k | } else { |
7637 | 5.85k | // Add the specialization into its lexical context, so that it can |
7638 | 5.85k | // be seen when iterating through the list of declarations in that |
7639 | 5.85k | // context. However, specializations are not found by name lookup. |
7640 | 5.85k | CurContext->addDecl(Specialization); |
7641 | 5.85k | } |
7642 | 5.93k | return Specialization; |
7643 | 5.93k | } |
7644 | | |
7645 | | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
7646 | | MultiTemplateParamsArg TemplateParameterLists, |
7647 | 6.84k | Declarator &D) { |
7648 | 6.84k | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
7649 | 6.84k | ActOnDocumentableDecl(NewDecl); |
7650 | 6.84k | return NewDecl; |
7651 | 6.84k | } |
7652 | | |
7653 | | /// \brief Strips various properties off an implicit instantiation |
7654 | | /// that has just been explicitly specialized. |
7655 | 1.80k | static void StripImplicitInstantiation(NamedDecl *D) { |
7656 | 1.80k | D->dropAttr<DLLImportAttr>(); |
7657 | 1.80k | D->dropAttr<DLLExportAttr>(); |
7658 | 1.80k | |
7659 | 1.80k | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
7660 | 1.61k | FD->setInlineSpecified(false); |
7661 | 1.80k | } |
7662 | | |
7663 | | /// \brief Compute the diagnostic location for an explicit instantiation |
7664 | | // declaration or definition. |
7665 | | static SourceLocation DiagLocForExplicitInstantiation( |
7666 | 38 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
7667 | 38 | // Explicit instantiations following a specialization have no effect and |
7668 | 38 | // hence no PointOfInstantiation. In that case, walk decl backwards |
7669 | 38 | // until a valid name loc is found. |
7670 | 38 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
7671 | 42 | for (Decl *Prev = D; Prev && 42 !PrevDiagLoc.isValid()41 ; |
7672 | 38 | Prev = Prev->getPreviousDecl()4 ) { |
7673 | 4 | PrevDiagLoc = Prev->getLocation(); |
7674 | 4 | } |
7675 | 38 | assert(PrevDiagLoc.isValid() && |
7676 | 38 | "Explicit instantiation without point of instantiation?"); |
7677 | 38 | return PrevDiagLoc; |
7678 | 38 | } |
7679 | | |
7680 | | /// \brief Diagnose cases where we have an explicit template specialization |
7681 | | /// before/after an explicit template instantiation, producing diagnostics |
7682 | | /// for those cases where they are required and determining whether the |
7683 | | /// new specialization/instantiation will have any effect. |
7684 | | /// |
7685 | | /// \param NewLoc the location of the new explicit specialization or |
7686 | | /// instantiation. |
7687 | | /// |
7688 | | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
7689 | | /// |
7690 | | /// \param PrevDecl the previous declaration of the entity. |
7691 | | /// |
7692 | | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
7693 | | /// |
7694 | | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
7695 | | /// declaration was instantiated (either implicitly or explicitly). |
7696 | | /// |
7697 | | /// \param HasNoEffect will be set to true to indicate that the new |
7698 | | /// specialization or instantiation has no effect and should be ignored. |
7699 | | /// |
7700 | | /// \returns true if there was an error that should prevent the introduction of |
7701 | | /// the new declaration into the AST, false otherwise. |
7702 | | bool |
7703 | | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
7704 | | TemplateSpecializationKind NewTSK, |
7705 | | NamedDecl *PrevDecl, |
7706 | | TemplateSpecializationKind PrevTSK, |
7707 | | SourceLocation PrevPointOfInstantiation, |
7708 | 4.58k | bool &HasNoEffect) { |
7709 | 4.58k | HasNoEffect = false; |
7710 | 4.58k | |
7711 | 4.58k | switch (NewTSK) { |
7712 | 124 | case TSK_Undeclared: |
7713 | 124 | case TSK_ImplicitInstantiation: |
7714 | 124 | assert( |
7715 | 124 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
7716 | 124 | "previous declaration must be implicit!"); |
7717 | 124 | return false; |
7718 | 124 | |
7719 | 1.91k | case TSK_ExplicitSpecialization: |
7720 | 1.91k | switch (PrevTSK) { |
7721 | 104 | case TSK_Undeclared: |
7722 | 104 | case TSK_ExplicitSpecialization: |
7723 | 104 | // Okay, we're just specializing something that is either already |
7724 | 104 | // explicitly specialized or has merely been mentioned without any |
7725 | 104 | // instantiation. |
7726 | 104 | return false; |
7727 | 104 | |
7728 | 1.81k | case TSK_ImplicitInstantiation: |
7729 | 1.81k | if (PrevPointOfInstantiation.isInvalid()1.81k ) { |
7730 | 1.80k | // The declaration itself has not actually been instantiated, so it is |
7731 | 1.80k | // still okay to specialize it. |
7732 | 1.80k | StripImplicitInstantiation(PrevDecl); |
7733 | 1.80k | return false; |
7734 | 1.80k | } |
7735 | 10 | // Fall through |
7736 | 10 | LLVM_FALLTHROUGH10 ; |
7737 | 10 | |
7738 | 10 | case TSK_ExplicitInstantiationDeclaration: |
7739 | 10 | case TSK_ExplicitInstantiationDefinition: |
7740 | 10 | assert((PrevTSK == TSK_ImplicitInstantiation || |
7741 | 10 | PrevPointOfInstantiation.isValid()) && |
7742 | 10 | "Explicit instantiation without point of instantiation?"); |
7743 | 10 | |
7744 | 10 | // C++ [temp.expl.spec]p6: |
7745 | 10 | // If a template, a member template or the member of a class template |
7746 | 10 | // is explicitly specialized then that specialization shall be declared |
7747 | 10 | // before the first use of that specialization that would cause an |
7748 | 10 | // implicit instantiation to take place, in every translation unit in |
7749 | 10 | // which such a use occurs; no diagnostic is required. |
7750 | 20 | for (Decl *Prev = PrevDecl; Prev20 ; Prev = Prev->getPreviousDecl()10 ) { |
7751 | 10 | // Is there any previous explicit specialization declaration? |
7752 | 10 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
7753 | 0 | return false; |
7754 | 10 | } |
7755 | 10 | |
7756 | 10 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
7757 | 10 | << PrevDecl; |
7758 | 10 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
7759 | 10 | << (PrevTSK != TSK_ImplicitInstantiation); |
7760 | 10 | |
7761 | 10 | return true; |
7762 | 0 | } |
7763 | 0 | llvm_unreachable0 ("The switch over PrevTSK must be exhaustive."); |
7764 | 0 |
|
7765 | 497 | case TSK_ExplicitInstantiationDeclaration: |
7766 | 497 | switch (PrevTSK) { |
7767 | 23 | case TSK_ExplicitInstantiationDeclaration: |
7768 | 23 | // This explicit instantiation declaration is redundant (that's okay). |
7769 | 23 | HasNoEffect = true; |
7770 | 23 | return false; |
7771 | 497 | |
7772 | 460 | case TSK_Undeclared: |
7773 | 460 | case TSK_ImplicitInstantiation: |
7774 | 460 | // We're explicitly instantiating something that may have already been |
7775 | 460 | // implicitly instantiated; that's fine. |
7776 | 460 | return false; |
7777 | 460 | |
7778 | 6 | case TSK_ExplicitSpecialization: |
7779 | 6 | // C++0x [temp.explicit]p4: |
7780 | 6 | // For a given set of template parameters, if an explicit instantiation |
7781 | 6 | // of a template appears after a declaration of an explicit |
7782 | 6 | // specialization for that template, the explicit instantiation has no |
7783 | 6 | // effect. |
7784 | 6 | HasNoEffect = true; |
7785 | 6 | return false; |
7786 | 460 | |
7787 | 8 | case TSK_ExplicitInstantiationDefinition: |
7788 | 8 | // C++0x [temp.explicit]p10: |
7789 | 8 | // If an entity is the subject of both an explicit instantiation |
7790 | 8 | // declaration and an explicit instantiation definition in the same |
7791 | 8 | // translation unit, the definition shall follow the declaration. |
7792 | 8 | Diag(NewLoc, |
7793 | 8 | diag::err_explicit_instantiation_declaration_after_definition); |
7794 | 8 | |
7795 | 8 | // Explicit instantiations following a specialization have no effect and |
7796 | 8 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
7797 | 8 | // until a valid name loc is found. |
7798 | 8 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
7799 | 8 | diag::note_explicit_instantiation_definition_here); |
7800 | 8 | HasNoEffect = true; |
7801 | 8 | return false; |
7802 | 0 | } |
7803 | 0 |
|
7804 | 2.05k | case TSK_ExplicitInstantiationDefinition: |
7805 | 2.05k | switch (PrevTSK) { |
7806 | 1.75k | case TSK_Undeclared: |
7807 | 1.75k | case TSK_ImplicitInstantiation: |
7808 | 1.75k | // We're explicitly instantiating something that may have already been |
7809 | 1.75k | // implicitly instantiated; that's fine. |
7810 | 1.75k | return false; |
7811 | 1.75k | |
7812 | 40 | case TSK_ExplicitSpecialization: |
7813 | 40 | // C++ DR 259, C++0x [temp.explicit]p4: |
7814 | 40 | // For a given set of template parameters, if an explicit |
7815 | 40 | // instantiation of a template appears after a declaration of |
7816 | 40 | // an explicit specialization for that template, the explicit |
7817 | 40 | // instantiation has no effect. |
7818 | 40 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
7819 | 40 | << PrevDecl; |
7820 | 40 | Diag(PrevDecl->getLocation(), |
7821 | 40 | diag::note_previous_template_specialization); |
7822 | 40 | HasNoEffect = true; |
7823 | 40 | return false; |
7824 | 1.75k | |
7825 | 230 | case TSK_ExplicitInstantiationDeclaration: |
7826 | 230 | // We're explicity instantiating a definition for something for which we |
7827 | 230 | // were previously asked to suppress instantiations. That's fine. |
7828 | 230 | |
7829 | 230 | // C++0x [temp.explicit]p4: |
7830 | 230 | // For a given set of template parameters, if an explicit instantiation |
7831 | 230 | // of a template appears after a declaration of an explicit |
7832 | 230 | // specialization for that template, the explicit instantiation has no |
7833 | 230 | // effect. |
7834 | 460 | for (Decl *Prev = PrevDecl; Prev460 ; Prev = Prev->getPreviousDecl()230 ) { |
7835 | 233 | // Is there any previous explicit specialization declaration? |
7836 | 233 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization233 ) { |
7837 | 3 | HasNoEffect = true; |
7838 | 3 | break; |
7839 | 3 | } |
7840 | 233 | } |
7841 | 230 | |
7842 | 230 | return false; |
7843 | 1.75k | |
7844 | 30 | case TSK_ExplicitInstantiationDefinition: |
7845 | 30 | // C++0x [temp.spec]p5: |
7846 | 30 | // For a given template and a given set of template-arguments, |
7847 | 30 | // - an explicit instantiation definition shall appear at most once |
7848 | 30 | // in a program, |
7849 | 30 | |
7850 | 30 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
7851 | 30 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
7852 | 1 | ? diag::ext_explicit_instantiation_duplicate |
7853 | 29 | : diag::err_explicit_instantiation_duplicate) |
7854 | 1.75k | << PrevDecl; |
7855 | 1.75k | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
7856 | 1.75k | diag::note_previous_explicit_instantiation); |
7857 | 1.75k | HasNoEffect = true; |
7858 | 1.75k | return false; |
7859 | 0 | } |
7860 | 0 | } |
7861 | 0 |
|
7862 | 0 | llvm_unreachable0 ("Missing specialization/instantiation case?"); |
7863 | 0 | } |
7864 | | |
7865 | | /// \brief Perform semantic analysis for the given dependent function |
7866 | | /// template specialization. |
7867 | | /// |
7868 | | /// The only possible way to get a dependent function template specialization |
7869 | | /// is with a friend declaration, like so: |
7870 | | /// |
7871 | | /// \code |
7872 | | /// template \<class T> void foo(T); |
7873 | | /// template \<class T> class A { |
7874 | | /// friend void foo<>(T); |
7875 | | /// }; |
7876 | | /// \endcode |
7877 | | /// |
7878 | | /// There really isn't any useful analysis we can do here, so we |
7879 | | /// just store the information. |
7880 | | bool |
7881 | | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
7882 | | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
7883 | 12 | LookupResult &Previous) { |
7884 | 12 | // Remove anything from Previous that isn't a function template in |
7885 | 12 | // the correct context. |
7886 | 12 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
7887 | 12 | LookupResult::Filter F = Previous.makeFilter(); |
7888 | 24 | while (F.hasNext()24 ) { |
7889 | 12 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
7890 | 12 | if (!isa<FunctionTemplateDecl>(D) || |
7891 | 12 | !FDLookupContext->InEnclosingNamespaceSetOf( |
7892 | 12 | D->getDeclContext()->getRedeclContext())) |
7893 | 0 | F.erase(); |
7894 | 12 | } |
7895 | 12 | F.done(); |
7896 | 12 | |
7897 | 12 | // Should this be diagnosed here? |
7898 | 12 | if (Previous.empty()12 ) return true0 ; |
7899 | 12 | |
7900 | 12 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
7901 | 12 | ExplicitTemplateArgs); |
7902 | 12 | return false; |
7903 | 12 | } |
7904 | | |
7905 | | /// \brief Perform semantic analysis for the given function template |
7906 | | /// specialization. |
7907 | | /// |
7908 | | /// This routine performs all of the semantic analysis required for an |
7909 | | /// explicit function template specialization. On successful completion, |
7910 | | /// the function declaration \p FD will become a function template |
7911 | | /// specialization. |
7912 | | /// |
7913 | | /// \param FD the function declaration, which will be updated to become a |
7914 | | /// function template specialization. |
7915 | | /// |
7916 | | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
7917 | | /// if any. Note that this may be valid info even when 0 arguments are |
7918 | | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
7919 | | /// as it anyway contains info on the angle brackets locations. |
7920 | | /// |
7921 | | /// \param Previous the set of declarations that may be specialized by |
7922 | | /// this function specialization. |
7923 | | bool Sema::CheckFunctionTemplateSpecialization( |
7924 | | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
7925 | 1.34k | LookupResult &Previous) { |
7926 | 1.34k | // The set of function template specializations that could match this |
7927 | 1.34k | // explicit function template specialization. |
7928 | 1.34k | UnresolvedSet<8> Candidates; |
7929 | 1.34k | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
7930 | 1.34k | /*ForTakingAddress=*/false); |
7931 | 1.34k | |
7932 | 1.34k | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
7933 | 1.34k | ConvertedTemplateArgs; |
7934 | 1.34k | |
7935 | 1.34k | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
7936 | 1.34k | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
7937 | 2.73k | I != E2.73k ; ++I1.38k ) { |
7938 | 1.38k | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
7939 | 1.38k | if (FunctionTemplateDecl *FunTmpl1.38k = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
7940 | 1.35k | // Only consider templates found within the same semantic lookup scope as |
7941 | 1.35k | // FD. |
7942 | 1.35k | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
7943 | 1.35k | Ovl->getDeclContext()->getRedeclContext())) |
7944 | 0 | continue; |
7945 | 1.35k | |
7946 | 1.35k | // When matching a constexpr member function template specialization |
7947 | 1.35k | // against the primary template, we don't yet know whether the |
7948 | 1.35k | // specialization has an implicit 'const' (because we don't know whether |
7949 | 1.35k | // it will be a static member function until we know which template it |
7950 | 1.35k | // specializes), so adjust it now assuming it specializes this template. |
7951 | 1.35k | QualType FT = FD->getType(); |
7952 | 1.35k | if (FD->isConstexpr()1.35k ) { |
7953 | 18 | CXXMethodDecl *OldMD = |
7954 | 18 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
7955 | 18 | if (OldMD && 18 OldMD->isConst()3 ) { |
7956 | 3 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
7957 | 3 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
7958 | 3 | EPI.TypeQuals |= Qualifiers::Const; |
7959 | 3 | FT = Context.getFunctionType(FPT->getReturnType(), |
7960 | 3 | FPT->getParamTypes(), EPI); |
7961 | 3 | } |
7962 | 18 | } |
7963 | 1.35k | |
7964 | 1.35k | TemplateArgumentListInfo Args; |
7965 | 1.35k | if (ExplicitTemplateArgs) |
7966 | 980 | Args = *ExplicitTemplateArgs; |
7967 | 1.35k | |
7968 | 1.35k | // C++ [temp.expl.spec]p11: |
7969 | 1.35k | // A trailing template-argument can be left unspecified in the |
7970 | 1.35k | // template-id naming an explicit function template specialization |
7971 | 1.35k | // provided it can be deduced from the function argument type. |
7972 | 1.35k | // Perform template argument deduction to determine whether we may be |
7973 | 1.35k | // specializing this template. |
7974 | 1.35k | // FIXME: It is somewhat wasteful to build |
7975 | 1.35k | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
7976 | 1.35k | FunctionDecl *Specialization = nullptr; |
7977 | 1.35k | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
7978 | 1.35k | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
7979 | 1.35k | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
7980 | 88 | Info)) { |
7981 | 88 | // Template argument deduction failed; record why it failed, so |
7982 | 88 | // that we can provide nifty diagnostics. |
7983 | 88 | FailedCandidates.addCandidate().set( |
7984 | 88 | I.getPair(), FunTmpl->getTemplatedDecl(), |
7985 | 88 | MakeDeductionFailureInfo(Context, TDK, Info)); |
7986 | 88 | (void)TDK; |
7987 | 88 | continue; |
7988 | 88 | } |
7989 | 1.26k | |
7990 | 1.26k | // Target attributes are part of the cuda function signature, so |
7991 | 1.26k | // the deduced template's cuda target must match that of the |
7992 | 1.26k | // specialization. Given that C++ template deduction does not |
7993 | 1.26k | // take target attributes into account, we reject candidates |
7994 | 1.26k | // here that have a different target. |
7995 | 1.26k | if (1.26k LangOpts.CUDA && |
7996 | 19 | IdentifyCUDATarget(Specialization, |
7997 | 19 | /* IgnoreImplicitHDAttributes = */ true) != |
7998 | 1.26k | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
7999 | 8 | FailedCandidates.addCandidate().set( |
8000 | 8 | I.getPair(), FunTmpl->getTemplatedDecl(), |
8001 | 8 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
8002 | 8 | continue; |
8003 | 8 | } |
8004 | 1.25k | |
8005 | 1.25k | // Record this candidate. |
8006 | 1.25k | if (1.25k ExplicitTemplateArgs1.25k ) |
8007 | 956 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
8008 | 1.35k | Candidates.addDecl(Specialization, I.getAccess()); |
8009 | 1.35k | } |
8010 | 1.38k | } |
8011 | 1.34k | |
8012 | 1.34k | // Find the most specialized function template. |
8013 | 1.34k | UnresolvedSetIterator Result = getMostSpecialized( |
8014 | 1.34k | Candidates.begin(), Candidates.end(), FailedCandidates, |
8015 | 1.34k | FD->getLocation(), |
8016 | 1.34k | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
8017 | 1.34k | PDiag(diag::err_function_template_spec_ambiguous) |
8018 | 1.34k | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
8019 | 1.34k | PDiag(diag::note_function_template_spec_matched)); |
8020 | 1.34k | |
8021 | 1.34k | if (Result == Candidates.end()) |
8022 | 107 | return true; |
8023 | 1.24k | |
8024 | 1.24k | // Ignore access information; it doesn't figure into redeclaration checking. |
8025 | 1.24k | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
8026 | 1.24k | |
8027 | 1.24k | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
8028 | 1.24k | // an explicit specialization (14.8.3) [...] of a concept definition. |
8029 | 1.24k | if (Specialization->getPrimaryTemplate()->isConcept()1.24k ) { |
8030 | 1 | Diag(FD->getLocation(), diag::err_concept_specialized) |
8031 | 1 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
8032 | 1 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
8033 | 1 | return true; |
8034 | 1 | } |
8035 | 1.24k | |
8036 | 1.24k | FunctionTemplateSpecializationInfo *SpecInfo |
8037 | 1.24k | = Specialization->getTemplateSpecializationInfo(); |
8038 | 1.24k | assert(SpecInfo && "Function template specialization info missing?"); |
8039 | 1.24k | |
8040 | 1.24k | // Note: do not overwrite location info if previous template |
8041 | 1.24k | // specialization kind was explicit. |
8042 | 1.24k | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
8043 | 1.24k | if (TSK == TSK_Undeclared || 1.24k TSK == TSK_ImplicitInstantiation1.24k ) { |
8044 | 1.17k | Specialization->setLocation(FD->getLocation()); |
8045 | 1.17k | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
8046 | 1.17k | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
8047 | 1.17k | // function can differ from the template declaration with respect to |
8048 | 1.17k | // the constexpr specifier. |
8049 | 1.17k | // FIXME: We need an update record for this AST mutation. |
8050 | 1.17k | // FIXME: What if there are multiple such prior declarations (for instance, |
8051 | 1.17k | // from different modules)? |
8052 | 1.17k | Specialization->setConstexpr(FD->isConstexpr()); |
8053 | 1.17k | } |
8054 | 1.24k | |
8055 | 1.24k | // FIXME: Check if the prior specialization has a point of instantiation. |
8056 | 1.24k | // If so, we have run afoul of . |
8057 | 1.24k | |
8058 | 1.24k | // If this is a friend declaration, then we're not really declaring |
8059 | 1.24k | // an explicit specialization. |
8060 | 1.24k | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
8061 | 1.24k | |
8062 | 1.24k | // Check the scope of this explicit specialization. |
8063 | 1.24k | if (!isFriend && |
8064 | 1.19k | CheckTemplateSpecializationScope(*this, |
8065 | 1.19k | Specialization->getPrimaryTemplate(), |
8066 | 1.19k | Specialization, FD->getLocation(), |
8067 | 1.19k | false)) |
8068 | 11 | return true; |
8069 | 1.22k | |
8070 | 1.22k | // C++ [temp.expl.spec]p6: |
8071 | 1.22k | // If a template, a member template or the member of a class template is |
8072 | 1.22k | // explicitly specialized then that specialization shall be declared |
8073 | 1.22k | // before the first use of that specialization that would cause an implicit |
8074 | 1.22k | // instantiation to take place, in every translation unit in which such a |
8075 | 1.22k | // use occurs; no diagnostic is required. |
8076 | 1.22k | bool HasNoEffect = false; |
8077 | 1.22k | if (!isFriend && |
8078 | 1.18k | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
8079 | 1.18k | TSK_ExplicitSpecialization, |
8080 | 1.18k | Specialization, |
8081 | 1.18k | SpecInfo->getTemplateSpecializationKind(), |
8082 | 1.18k | SpecInfo->getPointOfInstantiation(), |
8083 | 1.18k | HasNoEffect)) |
8084 | 2 | return true; |
8085 | 1.22k | |
8086 | 1.22k | // Mark the prior declaration as an explicit specialization, so that later |
8087 | 1.22k | // clients know that this is an explicit specialization. |
8088 | 1.22k | if (1.22k !isFriend1.22k ) { |
8089 | 1.17k | // Since explicit specializations do not inherit '=delete' from their |
8090 | 1.17k | // primary function template - check if the 'specialization' that was |
8091 | 1.17k | // implicitly generated (during template argument deduction for partial |
8092 | 1.17k | // ordering) from the most specialized of all the function templates that |
8093 | 1.17k | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
8094 | 1.17k | // first check that it was implicitly generated during template argument |
8095 | 1.17k | // deduction by making sure it wasn't referenced, and then reset the deleted |
8096 | 1.17k | // flag to not-deleted, so that we can inherit that information from 'FD'. |
8097 | 1.17k | if (Specialization->isDeleted() && 1.17k !SpecInfo->isExplicitSpecialization()16 && |
8098 | 1.17k | !Specialization->getCanonicalDecl()->isReferenced()12 ) { |
8099 | 12 | // FIXME: This assert will not hold in the presence of modules. |
8100 | 12 | assert( |
8101 | 12 | Specialization->getCanonicalDecl() == Specialization && |
8102 | 12 | "This must be the only existing declaration of this specialization"); |
8103 | 12 | // FIXME: We need an update record for this AST mutation. |
8104 | 12 | Specialization->setDeletedAsWritten(false); |
8105 | 12 | } |
8106 | 1.17k | // FIXME: We need an update record for this AST mutation. |
8107 | 1.17k | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
8108 | 1.17k | MarkUnusedFileScopedDecl(Specialization); |
8109 | 1.17k | } |
8110 | 1.22k | |
8111 | 1.22k | // Turn the given function declaration into a function template |
8112 | 1.22k | // specialization, with the template arguments from the previous |
8113 | 1.22k | // specialization. |
8114 | 1.22k | // Take copies of (semantic and syntactic) template argument lists. |
8115 | 1.22k | const TemplateArgumentList* TemplArgs = new (Context) |
8116 | 1.22k | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
8117 | 1.22k | FD->setFunctionTemplateSpecialization( |
8118 | 1.22k | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
8119 | 1.22k | SpecInfo->getTemplateSpecializationKind(), |
8120 | 1.22k | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization]942 : nullptr285 ); |
8121 | 1.22k | |
8122 | 1.22k | // A function template specialization inherits the target attributes |
8123 | 1.22k | // of its template. (We require the attributes explicitly in the |
8124 | 1.22k | // code to match, but a template may have implicit attributes by |
8125 | 1.22k | // virtue e.g. of being constexpr, and it passes these implicit |
8126 | 1.22k | // attributes on to its specializations.) |
8127 | 1.22k | if (LangOpts.CUDA) |
8128 | 11 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
8129 | 1.34k | |
8130 | 1.34k | // The "previous declaration" for this function template specialization is |
8131 | 1.34k | // the prior function template specialization. |
8132 | 1.34k | Previous.clear(); |
8133 | 1.34k | Previous.addDecl(Specialization); |
8134 | 1.34k | return false; |
8135 | 1.34k | } |
8136 | | |
8137 | | /// \brief Perform semantic analysis for the given non-template member |
8138 | | /// specialization. |
8139 | | /// |
8140 | | /// This routine performs all of the semantic analysis required for an |
8141 | | /// explicit member function specialization. On successful completion, |
8142 | | /// the function declaration \p FD will become a member function |
8143 | | /// specialization. |
8144 | | /// |
8145 | | /// \param Member the member declaration, which will be updated to become a |
8146 | | /// specialization. |
8147 | | /// |
8148 | | /// \param Previous the set of declarations, one of which may be specialized |
8149 | | /// by this function specialization; the set will be modified to contain the |
8150 | | /// redeclared member. |
8151 | | bool |
8152 | 849 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
8153 | 849 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
8154 | 849 | |
8155 | 849 | // Try to find the member we are instantiating. |
8156 | 849 | NamedDecl *FoundInstantiation = nullptr; |
8157 | 849 | NamedDecl *Instantiation = nullptr; |
8158 | 849 | NamedDecl *InstantiatedFrom = nullptr; |
8159 | 849 | MemberSpecializationInfo *MSInfo = nullptr; |
8160 | 849 | |
8161 | 849 | if (Previous.empty()849 ) { |
8162 | 12 | // Nowhere to look anyway. |
8163 | 849 | } else if (FunctionDecl *837 Function837 = dyn_cast<FunctionDecl>(Member)) { |
8164 | 609 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
8165 | 696 | I != E696 ; ++I87 ) { |
8166 | 640 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
8167 | 640 | if (CXXMethodDecl *Method640 = dyn_cast<CXXMethodDecl>(D)) { |
8168 | 587 | QualType Adjusted = Function->getType(); |
8169 | 587 | if (!hasExplicitCallingConv(Adjusted)) |
8170 | 584 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
8171 | 587 | if (Context.hasSameType(Adjusted, Method->getType())587 ) { |
8172 | 553 | FoundInstantiation = *I; |
8173 | 553 | Instantiation = Method; |
8174 | 553 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
8175 | 553 | MSInfo = Method->getMemberSpecializationInfo(); |
8176 | 553 | break; |
8177 | 553 | } |
8178 | 587 | } |
8179 | 640 | } |
8180 | 837 | } else if (228 isa<VarDecl>(Member)228 ) { |
8181 | 108 | VarDecl *PrevVar; |
8182 | 108 | if (Previous.isSingleResult() && |
8183 | 108 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
8184 | 100 | if (100 PrevVar->isStaticDataMember()100 ) { |
8185 | 100 | FoundInstantiation = Previous.getRepresentativeDecl(); |
8186 | 100 | Instantiation = PrevVar; |
8187 | 100 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
8188 | 100 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
8189 | 100 | } |
8190 | 228 | } else if (120 isa<RecordDecl>(Member)120 ) { |
8191 | 79 | CXXRecordDecl *PrevRecord; |
8192 | 79 | if (Previous.isSingleResult() && |
8193 | 79 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))79 ) { |
8194 | 79 | FoundInstantiation = Previous.getRepresentativeDecl(); |
8195 | 79 | Instantiation = PrevRecord; |
8196 | 79 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
8197 | 79 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
8198 | 79 | } |
8199 | 120 | } else if (41 isa<EnumDecl>(Member)41 ) { |
8200 | 41 | EnumDecl *PrevEnum; |
8201 | 41 | if (Previous.isSingleResult() && |
8202 | 41 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))41 ) { |
8203 | 41 | FoundInstantiation = Previous.getRepresentativeDecl(); |
8204 | 41 | Instantiation = PrevEnum; |
8205 | 41 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
8206 | 41 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
8207 | 41 | } |
8208 | 837 | } |
8209 | 849 | |
8210 | 849 | if (!Instantiation849 ) { |
8211 | 76 | // There is no previous declaration that matches. Since member |
8212 | 76 | // specializations are always out-of-line, the caller will complain about |
8213 | 76 | // this mismatch later. |
8214 | 76 | return false; |
8215 | 76 | } |
8216 | 773 | |
8217 | 773 | // A member specialization in a friend declaration isn't really declaring |
8218 | 773 | // an explicit specialization, just identifying a specific (possibly implicit) |
8219 | 773 | // specialization. Don't change the template specialization kind. |
8220 | 773 | // |
8221 | 773 | // FIXME: Is this really valid? Other compilers reject. |
8222 | 773 | if (773 Member->getFriendObjectKind() != Decl::FOK_None773 ) { |
8223 | 35 | // Preserve instantiation information. |
8224 | 35 | if (InstantiatedFrom && 35 isa<CXXMethodDecl>(Member)24 ) { |
8225 | 24 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
8226 | 24 | cast<CXXMethodDecl>(InstantiatedFrom), |
8227 | 24 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
8228 | 35 | } else if (11 InstantiatedFrom && 11 isa<CXXRecordDecl>(Member)0 ) { |
8229 | 0 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
8230 | 0 | cast<CXXRecordDecl>(InstantiatedFrom), |
8231 | 0 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
8232 | 0 | } |
8233 | 35 | |
8234 | 35 | Previous.clear(); |
8235 | 35 | Previous.addDecl(FoundInstantiation); |
8236 | 35 | return false; |
8237 | 35 | } |
8238 | 738 | |
8239 | 738 | // Make sure that this is a specialization of a member. |
8240 | 738 | if (738 !InstantiatedFrom738 ) { |
8241 | 7 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
8242 | 7 | << Member; |
8243 | 7 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
8244 | 7 | return true; |
8245 | 7 | } |
8246 | 731 | |
8247 | 731 | // C++ [temp.expl.spec]p6: |
8248 | 731 | // If a template, a member template or the member of a class template is |
8249 | 731 | // explicitly specialized then that specialization shall be declared |
8250 | 731 | // before the first use of that specialization that would cause an implicit |
8251 | 731 | // instantiation to take place, in every translation unit in which such a |
8252 | 731 | // use occurs; no diagnostic is required. |
8253 | 738 | assert(MSInfo && "Member specialization info missing?"); |
8254 | 731 | |
8255 | 731 | bool HasNoEffect = false; |
8256 | 731 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
8257 | 731 | TSK_ExplicitSpecialization, |
8258 | 731 | Instantiation, |
8259 | 731 | MSInfo->getTemplateSpecializationKind(), |
8260 | 731 | MSInfo->getPointOfInstantiation(), |
8261 | 731 | HasNoEffect)) |
8262 | 6 | return true; |
8263 | 725 | |
8264 | 725 | // Check the scope of this explicit specialization. |
8265 | 725 | if (725 CheckTemplateSpecializationScope(*this, |
8266 | 725 | InstantiatedFrom, |
8267 | 725 | Instantiation, Member->getLocation(), |
8268 | 725 | false)) |
8269 | 0 | return true; |
8270 | 725 | |
8271 | 725 | // Note that this member specialization is an "instantiation of" the |
8272 | 725 | // corresponding member of the original template. |
8273 | 725 | if (auto *725 MemberFunction725 = dyn_cast<FunctionDecl>(Member)) { |
8274 | 514 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
8275 | 514 | if (InstantiationFunction->getTemplateSpecializationKind() == |
8276 | 514 | TSK_ImplicitInstantiation) { |
8277 | 497 | // Explicit specializations of member functions of class templates do not |
8278 | 497 | // inherit '=delete' from the member function they are specializing. |
8279 | 497 | if (InstantiationFunction->isDeleted()497 ) { |
8280 | 4 | // FIXME: This assert will not hold in the presence of modules. |
8281 | 4 | assert(InstantiationFunction->getCanonicalDecl() == |
8282 | 4 | InstantiationFunction); |
8283 | 4 | // FIXME: We need an update record for this AST mutation. |
8284 | 4 | InstantiationFunction->setDeletedAsWritten(false); |
8285 | 4 | } |
8286 | 497 | } |
8287 | 514 | |
8288 | 514 | MemberFunction->setInstantiationOfMemberFunction( |
8289 | 514 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
8290 | 725 | } else if (auto *211 MemberVar211 = dyn_cast<VarDecl>(Member)) { |
8291 | 99 | MemberVar->setInstantiationOfStaticDataMember( |
8292 | 99 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
8293 | 211 | } else if (auto *112 MemberClass112 = dyn_cast<CXXRecordDecl>(Member)) { |
8294 | 73 | MemberClass->setInstantiationOfMemberClass( |
8295 | 73 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
8296 | 112 | } else if (auto *39 MemberEnum39 = dyn_cast<EnumDecl>(Member)) { |
8297 | 39 | MemberEnum->setInstantiationOfMemberEnum( |
8298 | 39 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
8299 | 39 | } else { |
8300 | 0 | llvm_unreachable("unknown member specialization kind"); |
8301 | 211 | } |
8302 | 725 | |
8303 | 725 | // Save the caller the trouble of having to figure out which declaration |
8304 | 725 | // this specialization matches. |
8305 | 725 | Previous.clear(); |
8306 | 725 | Previous.addDecl(FoundInstantiation); |
8307 | 725 | return false; |
8308 | 725 | } |
8309 | | |
8310 | | /// Complete the explicit specialization of a member of a class template by |
8311 | | /// updating the instantiated member to be marked as an explicit specialization. |
8312 | | /// |
8313 | | /// \param OrigD The member declaration instantiated from the template. |
8314 | | /// \param Loc The location of the explicit specialization of the member. |
8315 | | template<typename DeclT> |
8316 | | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
8317 | 807 | SourceLocation Loc) { |
8318 | 807 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
8319 | 114 | return; |
8320 | 693 | |
8321 | 693 | // FIXME: Inform AST mutation listeners of this AST mutation. |
8322 | 693 | // FIXME: If there are multiple in-class declarations of the member (from |
8323 | 693 | // multiple modules, or a declaration and later definition of a member type), |
8324 | 693 | // should we update all of them? |
8325 | 693 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
8326 | 693 | OrigD->setLocation(Loc); |
8327 | 693 | } SemaTemplate.cpp:void completeMemberSpecializationImpl<clang::EnumDecl>(clang::Sema&, clang::EnumDecl*, clang::SourceLocation) Line | Count | Source | 8317 | 39 | SourceLocation Loc) { | 8318 | 39 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | 8319 | 11 | return; | 8320 | 28 | | 8321 | 28 | // FIXME: Inform AST mutation listeners of this AST mutation. | 8322 | 28 | // FIXME: If there are multiple in-class declarations of the member (from | 8323 | 28 | // multiple modules, or a declaration and later definition of a member type), | 8324 | 28 | // should we update all of them? | 8325 | 28 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | 8326 | 28 | OrigD->setLocation(Loc); | 8327 | 28 | } |
SemaTemplate.cpp:void completeMemberSpecializationImpl<clang::CXXRecordDecl>(clang::Sema&, clang::CXXRecordDecl*, clang::SourceLocation) Line | Count | Source | 8317 | 73 | SourceLocation Loc) { | 8318 | 73 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | 8319 | 8 | return; | 8320 | 65 | | 8321 | 65 | // FIXME: Inform AST mutation listeners of this AST mutation. | 8322 | 65 | // FIXME: If there are multiple in-class declarations of the member (from | 8323 | 65 | // multiple modules, or a declaration and later definition of a member type), | 8324 | 65 | // should we update all of them? | 8325 | 65 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | 8326 | 65 | OrigD->setLocation(Loc); | 8327 | 65 | } |
SemaTemplate.cpp:void completeMemberSpecializationImpl<clang::VarDecl>(clang::Sema&, clang::VarDecl*, clang::SourceLocation) Line | Count | Source | 8317 | 97 | SourceLocation Loc) { | 8318 | 97 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | 8319 | 7 | return; | 8320 | 90 | | 8321 | 90 | // FIXME: Inform AST mutation listeners of this AST mutation. | 8322 | 90 | // FIXME: If there are multiple in-class declarations of the member (from | 8323 | 90 | // multiple modules, or a declaration and later definition of a member type), | 8324 | 90 | // should we update all of them? | 8325 | 90 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | 8326 | 90 | OrigD->setLocation(Loc); | 8327 | 90 | } |
SemaTemplate.cpp:void completeMemberSpecializationImpl<clang::CXXMethodDecl>(clang::Sema&, clang::CXXMethodDecl*, clang::SourceLocation) Line | Count | Source | 8317 | 598 | SourceLocation Loc) { | 8318 | 598 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | 8319 | 88 | return; | 8320 | 510 | | 8321 | 510 | // FIXME: Inform AST mutation listeners of this AST mutation. | 8322 | 510 | // FIXME: If there are multiple in-class declarations of the member (from | 8323 | 510 | // multiple modules, or a declaration and later definition of a member type), | 8324 | 510 | // should we update all of them? | 8325 | 510 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | 8326 | 510 | OrigD->setLocation(Loc); | 8327 | 510 | } |
|
8328 | | |
8329 | | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
8330 | 810 | LookupResult &Previous) { |
8331 | 810 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
8332 | 810 | if (Instantiation == Member) |
8333 | 3 | return; |
8334 | 807 | |
8335 | 807 | if (auto *807 Function807 = dyn_cast<CXXMethodDecl>(Instantiation)) |
8336 | 598 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
8337 | 209 | else if (auto *209 Var209 = dyn_cast<VarDecl>(Instantiation)) |
8338 | 97 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
8339 | 112 | else if (auto *112 Record112 = dyn_cast<CXXRecordDecl>(Instantiation)) |
8340 | 73 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
8341 | 39 | else if (auto *39 Enum39 = dyn_cast<EnumDecl>(Instantiation)) |
8342 | 39 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
8343 | 39 | else |
8344 | 0 | llvm_unreachable("unknown member specialization kind"); |
8345 | 810 | } |
8346 | | |
8347 | | /// \brief Check the scope of an explicit instantiation. |
8348 | | /// |
8349 | | /// \returns true if a serious error occurs, false otherwise. |
8350 | | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
8351 | | SourceLocation InstLoc, |
8352 | 3.72k | bool WasQualifiedName) { |
8353 | 3.72k | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
8354 | 3.72k | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
8355 | 3.72k | |
8356 | 3.72k | if (CurContext->isRecord()3.72k ) { |
8357 | 0 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
8358 | 0 | << D; |
8359 | 0 | return true; |
8360 | 0 | } |
8361 | 3.72k | |
8362 | 3.72k | // C++11 [temp.explicit]p3: |
8363 | 3.72k | // An explicit instantiation shall appear in an enclosing namespace of its |
8364 | 3.72k | // template. If the name declared in the explicit instantiation is an |
8365 | 3.72k | // unqualified name, the explicit instantiation shall appear in the |
8366 | 3.72k | // namespace where its template is declared or, if that namespace is inline |
8367 | 3.72k | // (7.3.1), any namespace from its enclosing namespace set. |
8368 | 3.72k | // |
8369 | 3.72k | // This is DR275, which we do not retroactively apply to C++98/03. |
8370 | 3.72k | if (3.72k WasQualifiedName3.72k ) { |
8371 | 1.01k | if (CurContext->Encloses(OrigContext)) |
8372 | 1.00k | return false; |
8373 | 2.70k | } else { |
8374 | 2.70k | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
8375 | 2.67k | return false; |
8376 | 51 | } |
8377 | 51 | |
8378 | 51 | if (NamespaceDecl *51 NS51 = dyn_cast<NamespaceDecl>(OrigContext)) { |
8379 | 36 | if (WasQualifiedName) |
8380 | 14 | S.Diag(InstLoc, |
8381 | 14 | S.getLangOpts().CPlusPlus11? |
8382 | 8 | diag::err_explicit_instantiation_out_of_scope : |
8383 | 6 | diag::warn_explicit_instantiation_out_of_scope_0x) |
8384 | 14 | << D << NS; |
8385 | 36 | else |
8386 | 22 | S.Diag(InstLoc, |
8387 | 22 | S.getLangOpts().CPlusPlus11? |
8388 | 11 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
8389 | 11 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
8390 | 22 | << D << NS; |
8391 | 36 | } else |
8392 | 15 | S.Diag(InstLoc, |
8393 | 15 | S.getLangOpts().CPlusPlus11? |
8394 | 5 | diag::err_explicit_instantiation_must_be_global : |
8395 | 10 | diag::warn_explicit_instantiation_must_be_global_0x) |
8396 | 15 | << D; |
8397 | 3.72k | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
8398 | 3.72k | return false; |
8399 | 3.72k | } |
8400 | | |
8401 | | /// \brief Determine whether the given scope specifier has a template-id in it. |
8402 | 639 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
8403 | 639 | if (!SS.isSet()) |
8404 | 221 | return false; |
8405 | 418 | |
8406 | 418 | // C++11 [temp.explicit]p3: |
8407 | 418 | // If the explicit instantiation is for a member function, a member class |
8408 | 418 | // or a static data member of a class template specialization, the name of |
8409 | 418 | // the class template specialization in the qualified-id for the member |
8410 | 418 | // name shall be a simple-template-id. |
8411 | 418 | // |
8412 | 418 | // C++98 has the same restriction, just worded differently. |
8413 | 583 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); 418 NNS583 ; |
8414 | 165 | NNS = NNS->getPrefix()) |
8415 | 422 | if (const Type *422 T422 = NNS->getAsType()) |
8416 | 408 | if (408 isa<TemplateSpecializationType>(T)408 ) |
8417 | 257 | return true; |
8418 | 418 | |
8419 | 161 | return false; |
8420 | 639 | } |
8421 | | |
8422 | | /// Make a dllexport or dllimport attr on a class template specialization take |
8423 | | /// effect. |
8424 | | static void dllExportImportClassTemplateSpecialization( |
8425 | 29 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
8426 | 29 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
8427 | 29 | assert(A && "dllExportImportClassTemplateSpecialization called " |
8428 | 29 | "on Def without dllexport or dllimport"); |
8429 | 29 | |
8430 | 29 | // We reject explicit instantiations in class scope, so there should |
8431 | 29 | // never be any delayed exported classes to worry about. |
8432 | 29 | assert(S.DelayedDllExportClasses.empty() && |
8433 | 29 | "delayed exports present at explicit instantiation"); |
8434 | 29 | S.checkClassLevelDLLAttribute(Def); |
8435 | 29 | |
8436 | 29 | // Propagate attribute to base class templates. |
8437 | 11 | for (auto &B : Def->bases()) { |
8438 | 11 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
8439 | 11 | B.getType()->getAsCXXRecordDecl())) |
8440 | 11 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
8441 | 11 | } |
8442 | 29 | |
8443 | 29 | S.referenceDLLExportedClassMethods(); |
8444 | 29 | } |
8445 | | |
8446 | | // Explicit instantiation of a class template specialization |
8447 | | DeclResult |
8448 | | Sema::ActOnExplicitInstantiation(Scope *S, |
8449 | | SourceLocation ExternLoc, |
8450 | | SourceLocation TemplateLoc, |
8451 | | unsigned TagSpec, |
8452 | | SourceLocation KWLoc, |
8453 | | const CXXScopeSpec &SS, |
8454 | | TemplateTy TemplateD, |
8455 | | SourceLocation TemplateNameLoc, |
8456 | | SourceLocation LAngleLoc, |
8457 | | ASTTemplateArgsPtr TemplateArgsIn, |
8458 | | SourceLocation RAngleLoc, |
8459 | 1.60k | AttributeList *Attr) { |
8460 | 1.60k | // Find the class template we're specializing |
8461 | 1.60k | TemplateName Name = TemplateD.get(); |
8462 | 1.60k | TemplateDecl *TD = Name.getAsTemplateDecl(); |
8463 | 1.60k | // Check that the specialization uses the same tag kind as the |
8464 | 1.60k | // original template. |
8465 | 1.60k | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
8466 | 1.60k | assert(Kind != TTK_Enum && |
8467 | 1.60k | "Invalid enum tag in class template explicit instantiation!"); |
8468 | 1.60k | |
8469 | 1.60k | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
8470 | 1.60k | |
8471 | 1.60k | if (!ClassTemplate1.60k ) { |
8472 | 1 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
8473 | 1 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
8474 | 1 | Diag(TD->getLocation(), diag::note_previous_use); |
8475 | 1 | return true; |
8476 | 1 | } |
8477 | 1.60k | |
8478 | 1.60k | if (1.60k !isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
8479 | 1.60k | Kind, /*isDefinition*/false, KWLoc, |
8480 | 1.60k | ClassTemplate->getIdentifier())) { |
8481 | 0 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
8482 | 0 | << ClassTemplate |
8483 | 0 | << FixItHint::CreateReplacement(KWLoc, |
8484 | 0 | ClassTemplate->getTemplatedDecl()->getKindName()); |
8485 | 0 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
8486 | 0 | diag::note_previous_use); |
8487 | 0 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
8488 | 0 | } |
8489 | 1.60k | |
8490 | 1.60k | // C++0x [temp.explicit]p2: |
8491 | 1.60k | // There are two forms of explicit instantiation: an explicit instantiation |
8492 | 1.60k | // definition and an explicit instantiation declaration. An explicit |
8493 | 1.60k | // instantiation declaration begins with the extern keyword. [...] |
8494 | 1.60k | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
8495 | 1.31k | ? TSK_ExplicitInstantiationDefinition |
8496 | 294 | : TSK_ExplicitInstantiationDeclaration; |
8497 | 1.60k | |
8498 | 1.60k | if (TSK == TSK_ExplicitInstantiationDeclaration1.60k ) { |
8499 | 294 | // Check for dllexport class template instantiation declarations. |
8500 | 314 | for (AttributeList *A = Attr; A314 ; A = A->getNext()20 ) { |
8501 | 30 | if (A->getKind() == AttributeList::AT_DLLExport30 ) { |
8502 | 10 | Diag(ExternLoc, |
8503 | 10 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
8504 | 10 | Diag(A->getLoc(), diag::note_attribute); |
8505 | 10 | break; |
8506 | 10 | } |
8507 | 30 | } |
8508 | 294 | |
8509 | 294 | if (auto *A294 = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
8510 | 14 | Diag(ExternLoc, |
8511 | 14 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
8512 | 14 | Diag(A->getLocation(), diag::note_attribute); |
8513 | 14 | } |
8514 | 294 | } |
8515 | 1.60k | |
8516 | 1.60k | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
8517 | 1.60k | // instantiation declarations for most purposes. |
8518 | 1.60k | bool DLLImportExplicitInstantiationDef = false; |
8519 | 1.60k | if (TSK == TSK_ExplicitInstantiationDefinition && |
8520 | 1.60k | Context.getTargetInfo().getCXXABI().isMicrosoft()1.31k ) { |
8521 | 170 | // Check for dllimport class template instantiation definitions. |
8522 | 170 | bool DLLImport = |
8523 | 170 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
8524 | 233 | for (AttributeList *A = Attr; A233 ; A = A->getNext()63 ) { |
8525 | 122 | if (A->getKind() == AttributeList::AT_DLLImport) |
8526 | 63 | DLLImport = true; |
8527 | 122 | if (A->getKind() == AttributeList::AT_DLLExport122 ) { |
8528 | 59 | // dllexport trumps dllimport here. |
8529 | 59 | DLLImport = false; |
8530 | 59 | break; |
8531 | 59 | } |
8532 | 122 | } |
8533 | 170 | if (DLLImport170 ) { |
8534 | 59 | TSK = TSK_ExplicitInstantiationDeclaration; |
8535 | 59 | DLLImportExplicitInstantiationDef = true; |
8536 | 59 | } |
8537 | 170 | } |
8538 | 1.60k | |
8539 | 1.60k | // Translate the parser's template argument list in our AST format. |
8540 | 1.60k | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
8541 | 1.60k | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
8542 | 1.60k | |
8543 | 1.60k | // Check that the template argument list is well-formed for this |
8544 | 1.60k | // template. |
8545 | 1.60k | SmallVector<TemplateArgument, 4> Converted; |
8546 | 1.60k | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
8547 | 1.60k | TemplateArgs, false, Converted)) |
8548 | 4 | return true; |
8549 | 1.60k | |
8550 | 1.60k | // Find the class template specialization declaration that |
8551 | 1.60k | // corresponds to these arguments. |
8552 | 1.60k | void *InsertPos = nullptr; |
8553 | 1.60k | ClassTemplateSpecializationDecl *PrevDecl |
8554 | 1.60k | = ClassTemplate->findSpecialization(Converted, InsertPos); |
8555 | 1.60k | |
8556 | 1.60k | TemplateSpecializationKind PrevDecl_TSK |
8557 | 1.60k | = PrevDecl ? PrevDecl->getTemplateSpecializationKind()262 : TSK_Undeclared1.33k ; |
8558 | 1.60k | |
8559 | 1.60k | // C++0x [temp.explicit]p2: |
8560 | 1.60k | // [...] An explicit instantiation shall appear in an enclosing |
8561 | 1.60k | // namespace of its template. [...] |
8562 | 1.60k | // |
8563 | 1.60k | // This is C++ DR 275. |
8564 | 1.60k | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
8565 | 1.60k | SS.isSet())) |
8566 | 0 | return true; |
8567 | 1.60k | |
8568 | 1.60k | ClassTemplateSpecializationDecl *Specialization = nullptr; |
8569 | 1.60k | |
8570 | 1.60k | bool HasNoEffect = false; |
8571 | 1.60k | if (PrevDecl1.60k ) { |
8572 | 262 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
8573 | 262 | PrevDecl, PrevDecl_TSK, |
8574 | 262 | PrevDecl->getPointOfInstantiation(), |
8575 | 262 | HasNoEffect)) |
8576 | 0 | return PrevDecl; |
8577 | 262 | |
8578 | 262 | // Even though HasNoEffect == true means that this explicit instantiation |
8579 | 262 | // has no effect on semantics, we go on to put its syntax in the AST. |
8580 | 262 | |
8581 | 262 | if (262 PrevDecl_TSK == TSK_ImplicitInstantiation || |
8582 | 262 | PrevDecl_TSK == TSK_Undeclared161 ) { |
8583 | 121 | // Since the only prior class template specialization with these |
8584 | 121 | // arguments was referenced but not declared, reuse that |
8585 | 121 | // declaration node as our own, updating the source location |
8586 | 121 | // for the template name to reflect our new declaration. |
8587 | 121 | // (Other source locations will be updated later.) |
8588 | 121 | Specialization = PrevDecl; |
8589 | 121 | Specialization->setLocation(TemplateNameLoc); |
8590 | 121 | PrevDecl = nullptr; |
8591 | 121 | } |
8592 | 262 | |
8593 | 262 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
8594 | 262 | DLLImportExplicitInstantiationDef101 ) { |
8595 | 10 | // The new specialization might add a dllimport attribute. |
8596 | 10 | HasNoEffect = false; |
8597 | 10 | } |
8598 | 262 | } |
8599 | 1.60k | |
8600 | 1.60k | if (1.60k !Specialization1.60k ) { |
8601 | 1.48k | // Create a new class template specialization declaration node for |
8602 | 1.48k | // this explicit specialization. |
8603 | 1.48k | Specialization |
8604 | 1.48k | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
8605 | 1.48k | ClassTemplate->getDeclContext(), |
8606 | 1.48k | KWLoc, TemplateNameLoc, |
8607 | 1.48k | ClassTemplate, |
8608 | 1.48k | Converted, |
8609 | 1.48k | PrevDecl); |
8610 | 1.48k | SetNestedNameSpecifier(Specialization, SS); |
8611 | 1.48k | |
8612 | 1.48k | if (!HasNoEffect && 1.48k !PrevDecl1.43k ) { |
8613 | 1.33k | // Insert the new specialization. |
8614 | 1.33k | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
8615 | 1.33k | } |
8616 | 1.48k | } |
8617 | 1.60k | |
8618 | 1.60k | // Build the fully-sugared type for this explicit instantiation as |
8619 | 1.60k | // the user wrote in the explicit instantiation itself. This means |
8620 | 1.60k | // that we'll pretty-print the type retrieved from the |
8621 | 1.60k | // specialization's declaration the way that the user actually wrote |
8622 | 1.60k | // the explicit instantiation, rather than formatting the name based |
8623 | 1.60k | // on the "canonical" representation used to store the template |
8624 | 1.60k | // arguments in the specialization. |
8625 | 1.60k | TypeSourceInfo *WrittenTy |
8626 | 1.60k | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
8627 | 1.60k | TemplateArgs, |
8628 | 1.60k | Context.getTypeDeclType(Specialization)); |
8629 | 1.60k | Specialization->setTypeAsWritten(WrittenTy); |
8630 | 1.60k | |
8631 | 1.60k | // Set source locations for keywords. |
8632 | 1.60k | Specialization->setExternLoc(ExternLoc); |
8633 | 1.60k | Specialization->setTemplateKeywordLoc(TemplateLoc); |
8634 | 1.60k | Specialization->setBraceRange(SourceRange()); |
8635 | 1.60k | |
8636 | 1.60k | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
8637 | 1.60k | if (Attr) |
8638 | 255 | ProcessDeclAttributeList(S, Specialization, Attr); |
8639 | 1.60k | |
8640 | 1.60k | // Add the explicit instantiation into its lexical context. However, |
8641 | 1.60k | // since explicit instantiations are never found by name lookup, we |
8642 | 1.60k | // just put it into the declaration context directly. |
8643 | 1.60k | Specialization->setLexicalDeclContext(CurContext); |
8644 | 1.60k | CurContext->addDecl(Specialization); |
8645 | 1.60k | |
8646 | 1.60k | // Syntax is now OK, so return if it has no other effect on semantics. |
8647 | 1.60k | if (HasNoEffect1.60k ) { |
8648 | 43 | // Set the template specialization kind. |
8649 | 43 | Specialization->setTemplateSpecializationKind(TSK); |
8650 | 43 | return Specialization; |
8651 | 43 | } |
8652 | 1.55k | |
8653 | 1.55k | // C++ [temp.explicit]p3: |
8654 | 1.55k | // A definition of a class template or class member template |
8655 | 1.55k | // shall be in scope at the point of the explicit instantiation of |
8656 | 1.55k | // the class template or class member template. |
8657 | 1.55k | // |
8658 | 1.55k | // This check comes when we actually try to perform the |
8659 | 1.55k | // instantiation. |
8660 | 1.55k | ClassTemplateSpecializationDecl *Def |
8661 | 1.55k | = cast_or_null<ClassTemplateSpecializationDecl>( |
8662 | 1.55k | Specialization->getDefinition()); |
8663 | 1.55k | if (!Def) |
8664 | 1.36k | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
8665 | 198 | else if (198 TSK == TSK_ExplicitInstantiationDefinition198 ) { |
8666 | 150 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
8667 | 150 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
8668 | 150 | } |
8669 | 1.55k | |
8670 | 1.55k | // Instantiate the members of this class template specialization. |
8671 | 1.55k | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
8672 | 1.55k | Specialization->getDefinition()); |
8673 | 1.55k | if (Def1.55k ) { |
8674 | 1.52k | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
8675 | 1.52k | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
8676 | 1.52k | // TSK_ExplicitInstantiationDefinition |
8677 | 1.52k | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
8678 | 387 | (TSK == TSK_ExplicitInstantiationDefinition || |
8679 | 1.52k | DLLImportExplicitInstantiationDef301 )) { |
8680 | 145 | // FIXME: Need to notify the ASTMutationListener that we did this. |
8681 | 145 | Def->setTemplateSpecializationKind(TSK); |
8682 | 145 | |
8683 | 145 | if (!getDLLAttr(Def) && 145 getDLLAttr(Specialization)73 && |
8684 | 40 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
8685 | 145 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment()18 )) { |
8686 | 24 | // In the MS ABI, an explicit instantiation definition can add a dll |
8687 | 24 | // attribute to a template with a previous instantiation declaration. |
8688 | 24 | // MinGW doesn't allow this. |
8689 | 24 | auto *A = cast<InheritableAttr>( |
8690 | 24 | getDLLAttr(Specialization)->clone(getASTContext())); |
8691 | 24 | A->setInherited(true); |
8692 | 24 | Def->addAttr(A); |
8693 | 24 | dllExportImportClassTemplateSpecialization(*this, Def); |
8694 | 24 | } |
8695 | 145 | } |
8696 | 1.52k | |
8697 | 1.52k | // Fix a TSK_ImplicitInstantiation followed by a |
8698 | 1.52k | // TSK_ExplicitInstantiationDefinition |
8699 | 1.52k | bool NewlyDLLExported = |
8700 | 1.51k | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
8701 | 1.52k | if (Old_TSK == TSK_ImplicitInstantiation && 1.52k NewlyDLLExported101 && |
8702 | 7 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
8703 | 1.52k | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment()3 )) { |
8704 | 5 | // In the MS ABI, an explicit instantiation definition can add a dll |
8705 | 5 | // attribute to a template with a previous implicit instantiation. |
8706 | 5 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
8707 | 5 | // avoid potentially strange codegen behavior. For example, if we extend |
8708 | 5 | // this conditional to dllimport, and we have a source file calling a |
8709 | 5 | // method on an implicitly instantiated template class instance and then |
8710 | 5 | // declaring a dllimport explicit instantiation definition for the same |
8711 | 5 | // template class, the codegen for the method call will not respect the |
8712 | 5 | // dllimport, while it will with cl. The Def will already have the DLL |
8713 | 5 | // attribute, since the Def and Specialization will be the same in the |
8714 | 5 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
8715 | 5 | // attribute to the Specialization; we just need to make it take effect. |
8716 | 5 | assert(Def == Specialization && |
8717 | 5 | "Def and Specialization should match for implicit instantiation"); |
8718 | 5 | dllExportImportClassTemplateSpecialization(*this, Def); |
8719 | 5 | } |
8720 | 1.52k | |
8721 | 1.52k | // Set the template specialization kind. Make sure it is set before |
8722 | 1.52k | // instantiating the members which will trigger ASTConsumer callbacks. |
8723 | 1.52k | Specialization->setTemplateSpecializationKind(TSK); |
8724 | 1.52k | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
8725 | 1.55k | } else { |
8726 | 30 | |
8727 | 30 | // Set the template specialization kind. |
8728 | 30 | Specialization->setTemplateSpecializationKind(TSK); |
8729 | 30 | } |
8730 | 1.60k | |
8731 | 1.60k | return Specialization; |
8732 | 1.60k | } |
8733 | | |
8734 | | // Explicit instantiation of a member class of a class template. |
8735 | | DeclResult |
8736 | | Sema::ActOnExplicitInstantiation(Scope *S, |
8737 | | SourceLocation ExternLoc, |
8738 | | SourceLocation TemplateLoc, |
8739 | | unsigned TagSpec, |
8740 | | SourceLocation KWLoc, |
8741 | | CXXScopeSpec &SS, |
8742 | | IdentifierInfo *Name, |
8743 | | SourceLocation NameLoc, |
8744 | 41 | AttributeList *Attr) { |
8745 | 41 | |
8746 | 41 | bool Owned = false; |
8747 | 41 | bool IsDependent = false; |
8748 | 41 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
8749 | 41 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
8750 | 41 | /*ModulePrivateLoc=*/SourceLocation(), |
8751 | 41 | MultiTemplateParamsArg(), Owned, IsDependent, |
8752 | 41 | SourceLocation(), false, TypeResult(), |
8753 | 41 | /*IsTypeSpecifier*/false, |
8754 | 41 | /*IsTemplateParamOrArg*/false); |
8755 | 41 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
8756 | 41 | |
8757 | 41 | if (!TagD) |
8758 | 4 | return true; |
8759 | 37 | |
8760 | 37 | TagDecl *Tag = cast<TagDecl>(TagD); |
8761 | 37 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
8762 | 37 | |
8763 | 37 | if (Tag->isInvalidDecl()) |
8764 | 0 | return true; |
8765 | 37 | |
8766 | 37 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
8767 | 37 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
8768 | 37 | if (!Pattern37 ) { |
8769 | 3 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
8770 | 3 | << Context.getTypeDeclType(Record); |
8771 | 3 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
8772 | 3 | return true; |
8773 | 3 | } |
8774 | 34 | |
8775 | 34 | // C++0x [temp.explicit]p2: |
8776 | 34 | // If the explicit instantiation is for a class or member class, the |
8777 | 34 | // elaborated-type-specifier in the declaration shall include a |
8778 | 34 | // simple-template-id. |
8779 | 34 | // |
8780 | 34 | // C++98 has the same restriction, just worded differently. |
8781 | 34 | if (34 !ScopeSpecifierHasTemplateId(SS)34 ) |
8782 | 3 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
8783 | 3 | << Record << SS.getRange(); |
8784 | 34 | |
8785 | 34 | // C++0x [temp.explicit]p2: |
8786 | 34 | // There are two forms of explicit instantiation: an explicit instantiation |
8787 | 34 | // definition and an explicit instantiation declaration. An explicit |
8788 | 34 | // instantiation declaration begins with the extern keyword. [...] |
8789 | 34 | TemplateSpecializationKind TSK |
8790 | 33 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
8791 | 1 | : TSK_ExplicitInstantiationDeclaration; |
8792 | 34 | |
8793 | 34 | // C++0x [temp.explicit]p2: |
8794 | 34 | // [...] An explicit instantiation shall appear in an enclosing |
8795 | 34 | // namespace of its template. [...] |
8796 | 34 | // |
8797 | 34 | // This is C++ DR 275. |
8798 | 34 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
8799 | 34 | |
8800 | 34 | // Verify that it is okay to explicitly instantiate here. |
8801 | 34 | CXXRecordDecl *PrevDecl |
8802 | 34 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
8803 | 34 | if (!PrevDecl && 34 Record->getDefinition()32 ) |
8804 | 6 | PrevDecl = Record; |
8805 | 34 | if (PrevDecl34 ) { |
8806 | 8 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
8807 | 8 | bool HasNoEffect = false; |
8808 | 8 | assert(MSInfo && "No member specialization information?"); |
8809 | 8 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
8810 | 8 | PrevDecl, |
8811 | 8 | MSInfo->getTemplateSpecializationKind(), |
8812 | 8 | MSInfo->getPointOfInstantiation(), |
8813 | 8 | HasNoEffect)) |
8814 | 0 | return true; |
8815 | 8 | if (8 HasNoEffect8 ) |
8816 | 8 | return TagD; |
8817 | 26 | } |
8818 | 26 | |
8819 | 26 | CXXRecordDecl *RecordDef |
8820 | 26 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
8821 | 26 | if (!RecordDef26 ) { |
8822 | 26 | // C++ [temp.explicit]p3: |
8823 | 26 | // A definition of a member class of a class template shall be in scope |
8824 | 26 | // at the point of an explicit instantiation of the member class. |
8825 | 26 | CXXRecordDecl *Def |
8826 | 26 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
8827 | 26 | if (!Def26 ) { |
8828 | 1 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
8829 | 1 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
8830 | 1 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
8831 | 1 | << Pattern; |
8832 | 1 | return true; |
8833 | 0 | } else { |
8834 | 25 | if (InstantiateClass(NameLoc, Record, Def, |
8835 | 25 | getTemplateInstantiationArgs(Record), |
8836 | 25 | TSK)) |
8837 | 3 | return true; |
8838 | 22 | |
8839 | 22 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
8840 | 22 | if (!RecordDef) |
8841 | 0 | return true; |
8842 | 22 | } |
8843 | 26 | } |
8844 | 22 | |
8845 | 22 | // Instantiate all of the members of the class. |
8846 | 22 | InstantiateClassMembers(NameLoc, RecordDef, |
8847 | 22 | getTemplateInstantiationArgs(Record), TSK); |
8848 | 22 | |
8849 | 22 | if (TSK == TSK_ExplicitInstantiationDefinition) |
8850 | 22 | MarkVTableUsed(NameLoc, RecordDef, true); |
8851 | 41 | |
8852 | 41 | // FIXME: We don't have any representation for explicit instantiations of |
8853 | 41 | // member classes. Such a representation is not needed for compilation, but it |
8854 | 41 | // should be available for clients that want to see all of the declarations in |
8855 | 41 | // the source code. |
8856 | 41 | return TagD; |
8857 | 41 | } |
8858 | | |
8859 | | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
8860 | | SourceLocation ExternLoc, |
8861 | | SourceLocation TemplateLoc, |
8862 | 2.20k | Declarator &D) { |
8863 | 2.20k | // Explicit instantiations always require a name. |
8864 | 2.20k | // TODO: check if/when DNInfo should replace Name. |
8865 | 2.20k | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
8866 | 2.20k | DeclarationName Name = NameInfo.getName(); |
8867 | 2.20k | if (!Name2.20k ) { |
8868 | 0 | if (!D.isInvalidType()) |
8869 | 0 | Diag(D.getDeclSpec().getLocStart(), |
8870 | 0 | diag::err_explicit_instantiation_requires_name) |
8871 | 0 | << D.getDeclSpec().getSourceRange() |
8872 | 0 | << D.getSourceRange(); |
8873 | 0 |
|
8874 | 0 | return true; |
8875 | 0 | } |
8876 | 2.20k | |
8877 | 2.20k | // The scope passed in may not be a decl scope. Zip up the scope tree until |
8878 | 2.20k | // we find one that is. |
8879 | 2.20k | while (2.20k (S->getFlags() & Scope::DeclScope) == 0 || |
8880 | 2.20k | (S->getFlags() & Scope::TemplateParamScope) != 0) |
8881 | 0 | S = S->getParent(); |
8882 | 2.20k | |
8883 | 2.20k | // Determine the type of the declaration. |
8884 | 2.20k | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
8885 | 2.20k | QualType R = T->getType(); |
8886 | 2.20k | if (R.isNull()) |
8887 | 0 | return true; |
8888 | 2.20k | |
8889 | 2.20k | // C++ [dcl.stc]p1: |
8890 | 2.20k | // A storage-class-specifier shall not be specified in [...] an explicit |
8891 | 2.20k | // instantiation (14.7.2) directive. |
8892 | 2.20k | if (2.20k D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef2.20k ) { |
8893 | 2 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
8894 | 2 | << Name; |
8895 | 2 | return true; |
8896 | 2.19k | } else if (2.19k D.getDeclSpec().getStorageClassSpec() |
8897 | 2.19k | != DeclSpec::SCS_unspecified) { |
8898 | 3 | // Complain about then remove the storage class specifier. |
8899 | 3 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
8900 | 3 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
8901 | 3 | |
8902 | 3 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
8903 | 3 | } |
8904 | 2.20k | |
8905 | 2.20k | // C++0x [temp.explicit]p1: |
8906 | 2.20k | // [...] An explicit instantiation of a function template shall not use the |
8907 | 2.20k | // inline or constexpr specifiers. |
8908 | 2.20k | // Presumably, this also applies to member functions of class templates as |
8909 | 2.20k | // well. |
8910 | 2.19k | if (2.19k D.getDeclSpec().isInlineSpecified()2.19k ) |
8911 | 14 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
8912 | 14 | getLangOpts().CPlusPlus11 ? |
8913 | 10 | diag::err_explicit_instantiation_inline : |
8914 | 4 | diag::warn_explicit_instantiation_inline_0x) |
8915 | 14 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
8916 | 2.19k | if (D.getDeclSpec().isConstexprSpecified() && 2.19k R->isFunctionType()14 ) |
8917 | 2.19k | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
8918 | 2.19k | // not already specified. |
8919 | 1 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
8920 | 1 | diag::err_explicit_instantiation_constexpr); |
8921 | 2.19k | |
8922 | 2.19k | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
8923 | 2.19k | // applied only to the definition of a function template or variable template, |
8924 | 2.19k | // declared in namespace scope. |
8925 | 2.19k | if (D.getDeclSpec().isConceptSpecified()2.19k ) { |
8926 | 4 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
8927 | 4 | diag::err_concept_specified_specialization) << 0; |
8928 | 4 | return true; |
8929 | 4 | } |
8930 | 2.19k | |
8931 | 2.19k | // A deduction guide is not on the list of entities that can be explicitly |
8932 | 2.19k | // instantiated. |
8933 | 2.19k | if (2.19k Name.getNameKind() == DeclarationName::CXXDeductionGuideName2.19k ) { |
8934 | 4 | Diag(D.getDeclSpec().getLocStart(), diag::err_deduction_guide_specialized) |
8935 | 4 | << /*explicit instantiation*/ 0; |
8936 | 4 | return true; |
8937 | 4 | } |
8938 | 2.19k | |
8939 | 2.19k | // C++0x [temp.explicit]p2: |
8940 | 2.19k | // There are two forms of explicit instantiation: an explicit instantiation |
8941 | 2.19k | // definition and an explicit instantiation declaration. An explicit |
8942 | 2.19k | // instantiation declaration begins with the extern keyword. [...] |
8943 | 2.19k | TemplateSpecializationKind TSK |
8944 | 1.87k | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
8945 | 313 | : TSK_ExplicitInstantiationDeclaration; |
8946 | 2.19k | |
8947 | 2.19k | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
8948 | 2.19k | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
8949 | 2.19k | |
8950 | 2.19k | if (!R->isFunctionType()2.19k ) { |
8951 | 512 | // C++ [temp.explicit]p1: |
8952 | 512 | // A [...] static data member of a class template can be explicitly |
8953 | 512 | // instantiated from the member definition associated with its class |
8954 | 512 | // template. |
8955 | 512 | // C++1y [temp.explicit]p1: |
8956 | 512 | // A [...] variable [...] template specialization can be explicitly |
8957 | 512 | // instantiated from its template. |
8958 | 512 | if (Previous.isAmbiguous()) |
8959 | 0 | return true; |
8960 | 512 | |
8961 | 512 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
8962 | 512 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
8963 | 512 | |
8964 | 512 | if (!PrevTemplate512 ) { |
8965 | 104 | if (!Prev || 104 !Prev->isStaticDataMember()99 ) { |
8966 | 7 | // We expect to see a data data member here. |
8967 | 7 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
8968 | 7 | << Name; |
8969 | 7 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
8970 | 10 | P != PEnd10 ; ++P3 ) |
8971 | 3 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
8972 | 7 | return true; |
8973 | 7 | } |
8974 | 97 | |
8975 | 97 | if (97 !Prev->getInstantiatedFromStaticDataMember()97 ) { |
8976 | 0 | // FIXME: Check for explicit specialization? |
8977 | 0 | Diag(D.getIdentifierLoc(), |
8978 | 0 | diag::err_explicit_instantiation_data_member_not_instantiated) |
8979 | 0 | << Prev; |
8980 | 0 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
8981 | 0 | // FIXME: Can we provide a note showing where this was declared? |
8982 | 0 | return true; |
8983 | 0 | } |
8984 | 408 | } else { |
8985 | 408 | // Explicitly instantiate a variable template. |
8986 | 408 | |
8987 | 408 | // C++1y [dcl.spec.auto]p6: |
8988 | 408 | // ... A program that uses auto or decltype(auto) in a context not |
8989 | 408 | // explicitly allowed in this section is ill-formed. |
8990 | 408 | // |
8991 | 408 | // This includes auto-typed variable template instantiations. |
8992 | 408 | if (R->isUndeducedType()408 ) { |
8993 | 2 | Diag(T->getTypeLoc().getLocStart(), |
8994 | 2 | diag::err_auto_not_allowed_var_inst); |
8995 | 2 | return true; |
8996 | 2 | } |
8997 | 406 | |
8998 | 406 | if (406 D.getName().getKind() != UnqualifiedId::IK_TemplateId406 ) { |
8999 | 3 | // C++1y [temp.explicit]p3: |
9000 | 3 | // If the explicit instantiation is for a variable, the unqualified-id |
9001 | 3 | // in the declaration shall be a template-id. |
9002 | 3 | Diag(D.getIdentifierLoc(), |
9003 | 3 | diag::err_explicit_instantiation_without_template_id) |
9004 | 3 | << PrevTemplate; |
9005 | 3 | Diag(PrevTemplate->getLocation(), |
9006 | 3 | diag::note_explicit_instantiation_here); |
9007 | 3 | return true; |
9008 | 3 | } |
9009 | 403 | |
9010 | 403 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
9011 | 403 | // explicit instantiation (14.8.2) [...] of a concept definition. |
9012 | 403 | if (403 PrevTemplate->isConcept()403 ) { |
9013 | 2 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
9014 | 2 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
9015 | 2 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
9016 | 2 | return true; |
9017 | 2 | } |
9018 | 401 | |
9019 | 401 | // Translate the parser's template argument list into our AST format. |
9020 | 401 | TemplateArgumentListInfo TemplateArgs = |
9021 | 401 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
9022 | 401 | |
9023 | 401 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
9024 | 401 | D.getIdentifierLoc(), TemplateArgs); |
9025 | 401 | if (Res.isInvalid()) |
9026 | 1 | return true; |
9027 | 400 | |
9028 | 400 | // Ignore access control bits, we don't need them for redeclaration |
9029 | 400 | // checking. |
9030 | 400 | Prev = cast<VarDecl>(Res.get()); |
9031 | 400 | } |
9032 | 512 | |
9033 | 512 | // C++0x [temp.explicit]p2: |
9034 | 512 | // If the explicit instantiation is for a member function, a member class |
9035 | 512 | // or a static data member of a class template specialization, the name of |
9036 | 512 | // the class template specialization in the qualified-id for the member |
9037 | 512 | // name shall be a simple-template-id. |
9038 | 512 | // |
9039 | 512 | // C++98 has the same restriction, just worded differently. |
9040 | 512 | // |
9041 | 512 | // This does not apply to variable template specializations, where the |
9042 | 512 | // template-id is in the unqualified-id instead. |
9043 | 497 | if (497 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && 497 !PrevTemplate376 ) |
9044 | 3 | Diag(D.getIdentifierLoc(), |
9045 | 3 | diag::ext_explicit_instantiation_without_qualified_id) |
9046 | 3 | << Prev << D.getCXXScopeSpec().getRange(); |
9047 | 497 | |
9048 | 497 | // Check the scope of this explicit instantiation. |
9049 | 497 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
9050 | 497 | |
9051 | 497 | // Verify that it is okay to explicitly instantiate here. |
9052 | 497 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
9053 | 497 | SourceLocation POI = Prev->getPointOfInstantiation(); |
9054 | 497 | bool HasNoEffect = false; |
9055 | 497 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
9056 | 497 | PrevTSK, POI, HasNoEffect)) |
9057 | 0 | return true; |
9058 | 497 | |
9059 | 497 | if (497 !HasNoEffect497 ) { |
9060 | 466 | // Instantiate static data member or variable template. |
9061 | 466 | |
9062 | 466 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
9063 | 466 | if (PrevTemplate466 ) { |
9064 | 373 | // Merge attributes. |
9065 | 373 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
9066 | 86 | ProcessDeclAttributeList(S, Prev, Attr); |
9067 | 373 | } |
9068 | 466 | if (TSK == TSK_ExplicitInstantiationDefinition) |
9069 | 352 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
9070 | 466 | } |
9071 | 497 | |
9072 | 497 | // Check the new variable specialization against the parsed input. |
9073 | 497 | if (PrevTemplate && 497 Prev400 && !Context.hasSameType(Prev->getType(), R)400 ) { |
9074 | 18 | Diag(T->getTypeLoc().getLocStart(), |
9075 | 18 | diag::err_invalid_var_template_spec_type) |
9076 | 18 | << 0 << PrevTemplate << R << Prev->getType(); |
9077 | 18 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
9078 | 18 | << 2 << PrevTemplate->getDeclName(); |
9079 | 18 | return true; |
9080 | 18 | } |
9081 | 479 | |
9082 | 479 | // FIXME: Create an ExplicitInstantiation node? |
9083 | 479 | return (Decl*) nullptr; |
9084 | 479 | } |
9085 | 1.67k | |
9086 | 1.67k | // If the declarator is a template-id, translate the parser's template |
9087 | 1.67k | // argument list into our AST format. |
9088 | 1.67k | bool HasExplicitTemplateArgs = false; |
9089 | 1.67k | TemplateArgumentListInfo TemplateArgs; |
9090 | 1.67k | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId1.67k ) { |
9091 | 1.13k | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
9092 | 1.13k | HasExplicitTemplateArgs = true; |
9093 | 1.13k | } |
9094 | 1.67k | |
9095 | 1.67k | // C++ [temp.explicit]p1: |
9096 | 1.67k | // A [...] function [...] can be explicitly instantiated from its template. |
9097 | 1.67k | // A member function [...] of a class template can be explicitly |
9098 | 1.67k | // instantiated from the member definition associated with its class |
9099 | 1.67k | // template. |
9100 | 1.67k | UnresolvedSet<8> TemplateMatches; |
9101 | 1.67k | FunctionDecl *NonTemplateMatch = nullptr; |
9102 | 1.67k | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
9103 | 1.67k | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
9104 | 1.67k | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
9105 | 3.45k | P != PEnd3.45k ; ++P1.77k ) { |
9106 | 1.77k | NamedDecl *Prev = *P; |
9107 | 1.77k | if (!HasExplicitTemplateArgs1.77k ) { |
9108 | 608 | if (CXXMethodDecl *Method608 = dyn_cast<CXXMethodDecl>(Prev)) { |
9109 | 155 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
9110 | 155 | /*AdjustExceptionSpec*/true); |
9111 | 155 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)155 ) { |
9112 | 123 | if (Method->getPrimaryTemplate()123 ) { |
9113 | 5 | TemplateMatches.addDecl(Method, P.getAccess()); |
9114 | 123 | } else { |
9115 | 118 | // FIXME: Can this assert ever happen? Needs a test. |
9116 | 118 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
9117 | 118 | NonTemplateMatch = Method; |
9118 | 118 | } |
9119 | 123 | } |
9120 | 155 | } |
9121 | 608 | } |
9122 | 1.77k | |
9123 | 1.77k | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
9124 | 1.77k | if (!FunTmpl) |
9125 | 159 | continue; |
9126 | 1.61k | |
9127 | 1.61k | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
9128 | 1.61k | FunctionDecl *Specialization = nullptr; |
9129 | 1.61k | if (TemplateDeductionResult TDK |
9130 | 1.61k | = DeduceTemplateArguments(FunTmpl, |
9131 | 1.61k | (HasExplicitTemplateArgs ? &TemplateArgs |
9132 | 1.61k | : nullptr), |
9133 | 80 | R, Specialization, Info)) { |
9134 | 80 | // Keep track of almost-matches. |
9135 | 80 | FailedCandidates.addCandidate() |
9136 | 80 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
9137 | 80 | MakeDeductionFailureInfo(Context, TDK, Info)); |
9138 | 80 | (void)TDK; |
9139 | 80 | continue; |
9140 | 80 | } |
9141 | 1.53k | |
9142 | 1.53k | // Target attributes are part of the cuda function signature, so |
9143 | 1.53k | // the cuda target of the instantiated function must match that of its |
9144 | 1.53k | // template. Given that C++ template deduction does not take |
9145 | 1.53k | // target attributes into account, we reject candidates here that |
9146 | 1.53k | // have a different target. |
9147 | 1.53k | if (1.53k LangOpts.CUDA && |
9148 | 20 | IdentifyCUDATarget(Specialization, |
9149 | 20 | /* IgnoreImplicitHDAttributes = */ true) != |
9150 | 1.53k | IdentifyCUDATarget(Attr)) { |
9151 | 6 | FailedCandidates.addCandidate().set( |
9152 | 6 | P.getPair(), FunTmpl->getTemplatedDecl(), |
9153 | 6 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
9154 | 6 | continue; |
9155 | 6 | } |
9156 | 1.52k | |
9157 | 1.52k | TemplateMatches.addDecl(Specialization, P.getAccess()); |
9158 | 1.52k | } |
9159 | 1.67k | |
9160 | 1.67k | FunctionDecl *Specialization = NonTemplateMatch; |
9161 | 1.67k | if (!Specialization1.67k ) { |
9162 | 1.56k | // Find the most specialized function template specialization. |
9163 | 1.56k | UnresolvedSetIterator Result = getMostSpecialized( |
9164 | 1.56k | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
9165 | 1.56k | D.getIdentifierLoc(), |
9166 | 1.56k | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
9167 | 1.56k | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
9168 | 1.56k | PDiag(diag::note_explicit_instantiation_candidate)); |
9169 | 1.56k | |
9170 | 1.56k | if (Result == TemplateMatches.end()) |
9171 | 62 | return true; |
9172 | 1.49k | |
9173 | 1.49k | // Ignore access control bits, we don't need them for redeclaration checking. |
9174 | 1.49k | Specialization = cast<FunctionDecl>(*Result); |
9175 | 1.49k | } |
9176 | 1.67k | |
9177 | 1.67k | // C++11 [except.spec]p4 |
9178 | 1.67k | // In an explicit instantiation an exception-specification may be specified, |
9179 | 1.67k | // but is not required. |
9180 | 1.67k | // If an exception-specification is specified in an explicit instantiation |
9181 | 1.67k | // directive, it shall be compatible with the exception-specifications of |
9182 | 1.67k | // other declarations of that function. |
9183 | 1.61k | if (auto *1.61k FPT1.61k = R->getAs<FunctionProtoType>()) |
9184 | 1.61k | if (1.61k FPT->hasExceptionSpec()1.61k ) { |
9185 | 7 | unsigned DiagID = |
9186 | 7 | diag::err_mismatched_exception_spec_explicit_instantiation; |
9187 | 7 | if (getLangOpts().MicrosoftExt) |
9188 | 0 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
9189 | 7 | bool Result = CheckEquivalentExceptionSpec( |
9190 | 7 | PDiag(DiagID) << Specialization->getType(), |
9191 | 7 | PDiag(diag::note_explicit_instantiation_here), |
9192 | 7 | Specialization->getType()->getAs<FunctionProtoType>(), |
9193 | 7 | Specialization->getLocation(), FPT, D.getLocStart()); |
9194 | 7 | // In Microsoft mode, mismatching exception specifications just cause a |
9195 | 7 | // warning. |
9196 | 7 | if (!getLangOpts().MicrosoftExt && 7 Result7 ) |
9197 | 7 | return true; |
9198 | 1.61k | } |
9199 | 1.61k | |
9200 | 1.61k | if (1.61k Specialization->getTemplateSpecializationKind() == TSK_Undeclared1.61k ) { |
9201 | 6 | Diag(D.getIdentifierLoc(), |
9202 | 6 | diag::err_explicit_instantiation_member_function_not_instantiated) |
9203 | 6 | << Specialization |
9204 | 6 | << (Specialization->getTemplateSpecializationKind() == |
9205 | 6 | TSK_ExplicitSpecialization); |
9206 | 6 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
9207 | 6 | return true; |
9208 | 6 | } |
9209 | 1.60k | |
9210 | 1.60k | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
9211 | 1.60k | if (!PrevDecl && 1.60k Specialization->isThisDeclarationADefinition()1.59k ) |
9212 | 14 | PrevDecl = Specialization; |
9213 | 1.60k | |
9214 | 1.60k | if (PrevDecl1.60k ) { |
9215 | 26 | bool HasNoEffect = false; |
9216 | 26 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
9217 | 26 | PrevDecl, |
9218 | 26 | PrevDecl->getTemplateSpecializationKind(), |
9219 | 26 | PrevDecl->getPointOfInstantiation(), |
9220 | 26 | HasNoEffect)) |
9221 | 0 | return true; |
9222 | 26 | |
9223 | 26 | // FIXME: We may still want to build some representation of this |
9224 | 26 | // explicit specialization. |
9225 | 26 | if (26 HasNoEffect26 ) |
9226 | 8 | return (Decl*) nullptr; |
9227 | 1.59k | } |
9228 | 1.59k | |
9229 | 1.59k | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
9230 | 1.59k | if (Attr) |
9231 | 206 | ProcessDeclAttributeList(S, Specialization, Attr); |
9232 | 1.59k | |
9233 | 1.59k | if (Specialization->isDefined()1.59k ) { |
9234 | 11 | // Let the ASTConsumer know that this function has been explicitly |
9235 | 11 | // instantiated now, and its linkage might have changed. |
9236 | 11 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
9237 | 1.59k | } else if (1.58k TSK == TSK_ExplicitInstantiationDefinition1.58k ) |
9238 | 1.39k | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
9239 | 1.59k | |
9240 | 1.59k | // C++0x [temp.explicit]p2: |
9241 | 1.59k | // If the explicit instantiation is for a member function, a member class |
9242 | 1.59k | // or a static data member of a class template specialization, the name of |
9243 | 1.59k | // the class template specialization in the qualified-id for the member |
9244 | 1.59k | // name shall be a simple-template-id. |
9245 | 1.59k | // |
9246 | 1.59k | // C++98 has the same restriction, just worded differently. |
9247 | 1.59k | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
9248 | 1.59k | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && 1.59k !FunTmpl467 && |
9249 | 108 | D.getCXXScopeSpec().isSet() && |
9250 | 108 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
9251 | 3 | Diag(D.getIdentifierLoc(), |
9252 | 3 | diag::ext_explicit_instantiation_without_qualified_id) |
9253 | 3 | << Specialization << D.getCXXScopeSpec().getRange(); |
9254 | 1.59k | |
9255 | 1.59k | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
9256 | 1.59k | // explicit instantiation (14.8.2) [...] of a concept definition. |
9257 | 1.59k | if (FunTmpl && 1.59k FunTmpl->isConcept()1.48k && |
9258 | 1.59k | !D.getDeclSpec().isConceptSpecified()2 ) { |
9259 | 2 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
9260 | 2 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
9261 | 2 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
9262 | 2 | return true; |
9263 | 2 | } |
9264 | 1.59k | |
9265 | 1.59k | CheckExplicitInstantiationScope(*this, |
9266 | 1.48k | FunTmpl? (NamedDecl *)FunTmpl |
9267 | 108 | : Specialization->getInstantiatedFromMemberFunction(), |
9268 | 2.20k | D.getIdentifierLoc(), |
9269 | 2.20k | D.getCXXScopeSpec().isSet()); |
9270 | 2.20k | |
9271 | 2.20k | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
9272 | 2.20k | return (Decl*) nullptr; |
9273 | 2.20k | } |
9274 | | |
9275 | | TypeResult |
9276 | | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
9277 | | const CXXScopeSpec &SS, IdentifierInfo *Name, |
9278 | 40 | SourceLocation TagLoc, SourceLocation NameLoc) { |
9279 | 40 | // This has to hold, because SS is expected to be defined. |
9280 | 40 | assert(Name && "Expected a name in a dependent tag"); |
9281 | 40 | |
9282 | 40 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
9283 | 40 | if (!NNS) |
9284 | 0 | return true; |
9285 | 40 | |
9286 | 40 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
9287 | 40 | |
9288 | 40 | if (TUK == TUK_Declaration || 40 TUK == TUK_Definition40 ) { |
9289 | 0 | Diag(NameLoc, diag::err_dependent_tag_decl) |
9290 | 0 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
9291 | 0 | return true; |
9292 | 0 | } |
9293 | 40 | |
9294 | 40 | // Create the resulting type. |
9295 | 40 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
9296 | 40 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
9297 | 40 | |
9298 | 40 | // Create type-source location information for this type. |
9299 | 40 | TypeLocBuilder TLB; |
9300 | 40 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
9301 | 40 | TL.setElaboratedKeywordLoc(TagLoc); |
9302 | 40 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
9303 | 40 | TL.setNameLoc(NameLoc); |
9304 | 40 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
9305 | 40 | } |
9306 | | |
9307 | | TypeResult |
9308 | | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
9309 | | const CXXScopeSpec &SS, const IdentifierInfo &II, |
9310 | 25.0k | SourceLocation IdLoc) { |
9311 | 25.0k | if (SS.isInvalid()) |
9312 | 0 | return true; |
9313 | 25.0k | |
9314 | 25.0k | if (25.0k TypenameLoc.isValid() && 25.0k S24.6k && !S->getTemplateParamParent()24.6k ) |
9315 | 107 | Diag(TypenameLoc, |
9316 | 107 | getLangOpts().CPlusPlus11 ? |
9317 | 63 | diag::warn_cxx98_compat_typename_outside_of_template : |
9318 | 44 | diag::ext_typename_outside_of_template) |
9319 | 107 | << FixItHint::CreateRemoval(TypenameLoc); |
9320 | 25.0k | |
9321 | 25.0k | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
9322 | 25.0k | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename24.6k : ETK_None411 , |
9323 | 25.0k | TypenameLoc, QualifierLoc, II, IdLoc); |
9324 | 25.0k | if (T.isNull()) |
9325 | 66 | return true; |
9326 | 24.9k | |
9327 | 24.9k | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
9328 | 24.9k | if (isa<DependentNameType>(T)24.9k ) { |
9329 | 24.8k | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
9330 | 24.8k | TL.setElaboratedKeywordLoc(TypenameLoc); |
9331 | 24.8k | TL.setQualifierLoc(QualifierLoc); |
9332 | 24.8k | TL.setNameLoc(IdLoc); |
9333 | 24.9k | } else { |
9334 | 174 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
9335 | 174 | TL.setElaboratedKeywordLoc(TypenameLoc); |
9336 | 174 | TL.setQualifierLoc(QualifierLoc); |
9337 | 174 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
9338 | 174 | } |
9339 | 25.0k | |
9340 | 25.0k | return CreateParsedType(T, TSI); |
9341 | 25.0k | } |
9342 | | |
9343 | | TypeResult |
9344 | | Sema::ActOnTypenameType(Scope *S, |
9345 | | SourceLocation TypenameLoc, |
9346 | | const CXXScopeSpec &SS, |
9347 | | SourceLocation TemplateKWLoc, |
9348 | | TemplateTy TemplateIn, |
9349 | | IdentifierInfo *TemplateII, |
9350 | | SourceLocation TemplateIILoc, |
9351 | | SourceLocation LAngleLoc, |
9352 | | ASTTemplateArgsPtr TemplateArgsIn, |
9353 | 187 | SourceLocation RAngleLoc) { |
9354 | 187 | if (TypenameLoc.isValid() && 187 S181 && !S->getTemplateParamParent()181 ) |
9355 | 13 | Diag(TypenameLoc, |
9356 | 13 | getLangOpts().CPlusPlus11 ? |
9357 | 8 | diag::warn_cxx98_compat_typename_outside_of_template : |
9358 | 5 | diag::ext_typename_outside_of_template) |
9359 | 13 | << FixItHint::CreateRemoval(TypenameLoc); |
9360 | 187 | |
9361 | 187 | // Strangely, non-type results are not ignored by this lookup, so the |
9362 | 187 | // program is ill-formed if it finds an injected-class-name. |
9363 | 187 | if (TypenameLoc.isValid()187 ) { |
9364 | 181 | auto *LookupRD = |
9365 | 181 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
9366 | 181 | if (LookupRD && 181 LookupRD->getIdentifier() == TemplateII50 ) { |
9367 | 10 | Diag(TemplateIILoc, |
9368 | 10 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
9369 | 10 | << TemplateII << 0 /*injected-class-name used as template name*/ |
9370 | 10 | << (TemplateKWLoc.isValid() ? 14 : 06 /*'template'/'typename' keyword*/); |
9371 | 10 | } |
9372 | 181 | } |
9373 | 187 | |
9374 | 187 | // Translate the parser's template argument list in our AST format. |
9375 | 187 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
9376 | 187 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
9377 | 187 | |
9378 | 187 | TemplateName Template = TemplateIn.get(); |
9379 | 187 | if (DependentTemplateName *DTN187 = Template.getAsDependentTemplateName()) { |
9380 | 134 | // Construct a dependent template specialization type. |
9381 | 134 | assert(DTN && "dependent template has non-dependent name?"); |
9382 | 134 | assert(DTN->getQualifier() == SS.getScopeRep()); |
9383 | 134 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
9384 | 134 | DTN->getQualifier(), |
9385 | 134 | DTN->getIdentifier(), |
9386 | 134 | TemplateArgs); |
9387 | 134 | |
9388 | 134 | // Create source-location information for this type. |
9389 | 134 | TypeLocBuilder Builder; |
9390 | 134 | DependentTemplateSpecializationTypeLoc SpecTL |
9391 | 134 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
9392 | 134 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
9393 | 134 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
9394 | 134 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
9395 | 134 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
9396 | 134 | SpecTL.setLAngleLoc(LAngleLoc); |
9397 | 134 | SpecTL.setRAngleLoc(RAngleLoc); |
9398 | 273 | for (unsigned I = 0, N = TemplateArgs.size(); I != N273 ; ++I139 ) |
9399 | 139 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
9400 | 134 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
9401 | 134 | } |
9402 | 53 | |
9403 | 53 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
9404 | 53 | if (T.isNull()) |
9405 | 0 | return true; |
9406 | 53 | |
9407 | 53 | // Provide source-location information for the template specialization type. |
9408 | 53 | TypeLocBuilder Builder; |
9409 | 53 | TemplateSpecializationTypeLoc SpecTL |
9410 | 53 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
9411 | 53 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
9412 | 53 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
9413 | 53 | SpecTL.setLAngleLoc(LAngleLoc); |
9414 | 53 | SpecTL.setRAngleLoc(RAngleLoc); |
9415 | 119 | for (unsigned I = 0, N = TemplateArgs.size(); I != N119 ; ++I66 ) |
9416 | 66 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
9417 | 187 | |
9418 | 187 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
9419 | 187 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
9420 | 187 | TL.setElaboratedKeywordLoc(TypenameLoc); |
9421 | 187 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
9422 | 187 | |
9423 | 187 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
9424 | 187 | return CreateParsedType(T, TSI); |
9425 | 187 | } |
9426 | | |
9427 | | |
9428 | | /// Determine whether this failed name lookup should be treated as being |
9429 | | /// disabled by a usage of std::enable_if. |
9430 | | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
9431 | 180 | SourceRange &CondRange, Expr *&Cond) { |
9432 | 180 | // We must be looking for a ::type... |
9433 | 180 | if (!II.isStr("type")) |
9434 | 119 | return false; |
9435 | 61 | |
9436 | 61 | // ... within an explicitly-written template specialization... |
9437 | 61 | if (61 !NNS || 61 !NNS.getNestedNameSpecifier()->getAsType()61 ) |
9438 | 0 | return false; |
9439 | 61 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
9440 | 61 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
9441 | 61 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
9442 | 61 | if (!EnableIfTSTLoc || 61 EnableIfTSTLoc.getNumArgs() == 041 ) |
9443 | 20 | return false; |
9444 | 41 | const TemplateSpecializationType *EnableIfTST = |
9445 | 41 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
9446 | 41 | |
9447 | 41 | // ... which names a complete class template declaration... |
9448 | 41 | const TemplateDecl *EnableIfDecl = |
9449 | 41 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
9450 | 41 | if (!EnableIfDecl || 41 EnableIfTST->isIncompleteType()41 ) |
9451 | 1 | return false; |
9452 | 40 | |
9453 | 40 | // ... called "enable_if". |
9454 | 40 | const IdentifierInfo *EnableIfII = |
9455 | 40 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
9456 | 40 | if (!EnableIfII || 40 !EnableIfII->isStr("enable_if")40 ) |
9457 | 11 | return false; |
9458 | 29 | |
9459 | 29 | // Assume the first template argument is the condition. |
9460 | 29 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
9461 | 29 | |
9462 | 29 | // Dig out the condition. |
9463 | 29 | Cond = nullptr; |
9464 | 29 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
9465 | 29 | != TemplateArgument::Expression) |
9466 | 3 | return true; |
9467 | 26 | |
9468 | 26 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
9469 | 26 | |
9470 | 26 | // Ignore Boolean literals; they add no value. |
9471 | 26 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
9472 | 0 | Cond = nullptr; |
9473 | 180 | |
9474 | 180 | return true; |
9475 | 180 | } |
9476 | | |
9477 | | /// \brief Build the type that describes a C++ typename specifier, |
9478 | | /// e.g., "typename T::type". |
9479 | | QualType |
9480 | | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
9481 | | SourceLocation KeywordLoc, |
9482 | | NestedNameSpecifierLoc QualifierLoc, |
9483 | | const IdentifierInfo &II, |
9484 | 41.6k | SourceLocation IILoc) { |
9485 | 41.6k | CXXScopeSpec SS; |
9486 | 41.6k | SS.Adopt(QualifierLoc); |
9487 | 41.6k | |
9488 | 41.6k | DeclContext *Ctx = computeDeclContext(SS); |
9489 | 41.6k | if (!Ctx41.6k ) { |
9490 | 24.8k | // If the nested-name-specifier is dependent and couldn't be |
9491 | 24.8k | // resolved to a type, build a typename type. |
9492 | 24.8k | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
9493 | 24.8k | return Context.getDependentNameType(Keyword, |
9494 | 24.8k | QualifierLoc.getNestedNameSpecifier(), |
9495 | 24.8k | &II); |
9496 | 24.8k | } |
9497 | 16.8k | |
9498 | 16.8k | // If the nested-name-specifier refers to the current instantiation, |
9499 | 16.8k | // the "typename" keyword itself is superfluous. In C++03, the |
9500 | 16.8k | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
9501 | 16.8k | // allows such extraneous "typename" keywords, and we retroactively |
9502 | 16.8k | // apply this DR to C++03 code with only a warning. In any case we continue. |
9503 | 16.8k | |
9504 | 16.8k | if (16.8k RequireCompleteDeclContext(SS, Ctx)16.8k ) |
9505 | 599 | return QualType(); |
9506 | 16.2k | |
9507 | 16.2k | DeclarationName Name(&II); |
9508 | 16.2k | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
9509 | 16.2k | LookupQualifiedName(Result, Ctx, SS); |
9510 | 16.2k | unsigned DiagID = 0; |
9511 | 16.2k | Decl *Referenced = nullptr; |
9512 | 16.2k | switch (Result.getResultKind()) { |
9513 | 180 | case LookupResult::NotFound: { |
9514 | 180 | // If we're looking up 'type' within a template named 'enable_if', produce |
9515 | 180 | // a more specific diagnostic. |
9516 | 180 | SourceRange CondRange; |
9517 | 180 | Expr *Cond = nullptr; |
9518 | 180 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)180 ) { |
9519 | 29 | // If we have a condition, narrow it down to the specific failed |
9520 | 29 | // condition. |
9521 | 29 | if (Cond29 ) { |
9522 | 26 | Expr *FailedCond; |
9523 | 26 | std::string FailedDescription; |
9524 | 26 | std::tie(FailedCond, FailedDescription) = |
9525 | 26 | findFailedBooleanCondition(Cond, /*AllowTopLevelCond=*/true); |
9526 | 26 | |
9527 | 26 | Diag(FailedCond->getExprLoc(), |
9528 | 26 | diag::err_typename_nested_not_found_requirement) |
9529 | 26 | << FailedDescription |
9530 | 26 | << FailedCond->getSourceRange(); |
9531 | 26 | return QualType(); |
9532 | 26 | } |
9533 | 3 | |
9534 | 3 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
9535 | 3 | << Ctx << CondRange; |
9536 | 3 | return QualType(); |
9537 | 3 | } |
9538 | 151 | |
9539 | 151 | DiagID = diag::err_typename_nested_not_found; |
9540 | 151 | break; |
9541 | 151 | } |
9542 | 151 | |
9543 | 3 | case LookupResult::FoundUnresolvedValue: { |
9544 | 3 | // We found a using declaration that is a value. Most likely, the using |
9545 | 3 | // declaration itself is meant to have the 'typename' keyword. |
9546 | 3 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc3 : SS.getBeginLoc()0 , |
9547 | 3 | IILoc); |
9548 | 3 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
9549 | 3 | << Name << Ctx << FullRange; |
9550 | 3 | if (UnresolvedUsingValueDecl *Using |
9551 | 3 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
9552 | 3 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
9553 | 3 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
9554 | 3 | << FixItHint::CreateInsertion(Loc, "typename "); |
9555 | 3 | } |
9556 | 3 | } |
9557 | 3 | // Fall through to create a dependent typename type, from which we can recover |
9558 | 3 | // better. |
9559 | 3 | LLVM_FALLTHROUGH; |
9560 | 3 | |
9561 | 12 | case LookupResult::NotFoundInCurrentInstantiation: |
9562 | 12 | // Okay, it's a member of an unknown instantiation. |
9563 | 12 | return Context.getDependentNameType(Keyword, |
9564 | 12 | QualifierLoc.getNestedNameSpecifier(), |
9565 | 12 | &II); |
9566 | 3 | |
9567 | 16.0k | case LookupResult::Found: |
9568 | 16.0k | if (TypeDecl *Type16.0k = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
9569 | 15.9k | // C++ [class.qual]p2: |
9570 | 15.9k | // In a lookup in which function names are not ignored and the |
9571 | 15.9k | // nested-name-specifier nominates a class C, if the name specified |
9572 | 15.9k | // after the nested-name-specifier, when looked up in C, is the |
9573 | 15.9k | // injected-class-name of C [...] then the name is instead considered |
9574 | 15.9k | // to name the constructor of class C. |
9575 | 15.9k | // |
9576 | 15.9k | // Unlike in an elaborated-type-specifier, function names are not ignored |
9577 | 15.9k | // in typename-specifier lookup. However, they are ignored in all the |
9578 | 15.9k | // contexts where we form a typename type with no keyword (that is, in |
9579 | 15.9k | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
9580 | 15.9k | // |
9581 | 15.9k | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
9582 | 15.9k | // ignore functions, but that appears to be an oversight. |
9583 | 15.9k | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
9584 | 15.9k | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
9585 | 15.9k | if (Keyword == ETK_Typename && 15.9k LookupRD15.8k && FoundRD15.8k && |
9586 | 213 | FoundRD->isInjectedClassName() && |
9587 | 43 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
9588 | 24 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
9589 | 24 | << &II << 1 << 0 /*'typename' keyword used*/; |
9590 | 15.9k | |
9591 | 15.9k | // We found a type. Build an ElaboratedType, since the |
9592 | 15.9k | // typename-specifier was just sugar. |
9593 | 15.9k | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
9594 | 15.9k | return Context.getElaboratedType(Keyword, |
9595 | 15.9k | QualifierLoc.getNestedNameSpecifier(), |
9596 | 15.9k | Context.getTypeDeclType(Type)); |
9597 | 15.9k | } |
9598 | 75 | |
9599 | 75 | // C++ [dcl.type.simple]p2: |
9600 | 75 | // A type-specifier of the form |
9601 | 75 | // typename[opt] nested-name-specifier[opt] template-name |
9602 | 75 | // is a placeholder for a deduced class type [...]. |
9603 | 75 | if (75 getLangOpts().CPlusPlus1z75 ) { |
9604 | 43 | if (auto *TD43 = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
9605 | 41 | return Context.getElaboratedType( |
9606 | 41 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
9607 | 41 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
9608 | 41 | QualType(), false)); |
9609 | 41 | } |
9610 | 34 | } |
9611 | 34 | |
9612 | 34 | DiagID = diag::err_typename_nested_not_type; |
9613 | 34 | Referenced = Result.getFoundDecl(); |
9614 | 34 | break; |
9615 | 34 | |
9616 | 0 | case LookupResult::FoundOverloaded: |
9617 | 0 | DiagID = diag::err_typename_nested_not_type; |
9618 | 0 | Referenced = *Result.begin(); |
9619 | 0 | break; |
9620 | 34 | |
9621 | 11 | case LookupResult::Ambiguous: |
9622 | 11 | return QualType(); |
9623 | 185 | } |
9624 | 185 | |
9625 | 185 | // If we get here, it's because name lookup did not find a |
9626 | 185 | // type. Emit an appropriate diagnostic and return an error. |
9627 | 185 | SourceRange FullRange(KeywordLoc.isValid() ? 185 KeywordLoc179 : SS.getBeginLoc()6 , |
9628 | 185 | IILoc); |
9629 | 185 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
9630 | 185 | if (Referenced) |
9631 | 34 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
9632 | 34 | << Name; |
9633 | 41.6k | return QualType(); |
9634 | 41.6k | } |
9635 | | |
9636 | | namespace { |
9637 | | // See Sema::RebuildTypeInCurrentInstantiation |
9638 | | class CurrentInstantiationRebuilder |
9639 | | : public TreeTransform<CurrentInstantiationRebuilder> { |
9640 | | SourceLocation Loc; |
9641 | | DeclarationName Entity; |
9642 | | |
9643 | | public: |
9644 | | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
9645 | | |
9646 | | CurrentInstantiationRebuilder(Sema &SemaRef, |
9647 | | SourceLocation Loc, |
9648 | | DeclarationName Entity) |
9649 | | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
9650 | 3.77k | Loc(Loc), Entity(Entity) { } |
9651 | | |
9652 | | /// \brief Determine whether the given type \p T has already been |
9653 | | /// transformed. |
9654 | | /// |
9655 | | /// For the purposes of type reconstruction, a type has already been |
9656 | | /// transformed if it is NULL or if it is not dependent. |
9657 | 8.19k | bool AlreadyTransformed(QualType T) { |
9658 | 8.19k | return T.isNull() || !T->isDependentType(); |
9659 | 8.19k | } |
9660 | | |
9661 | | /// \brief Returns the location of the entity whose type is being |
9662 | | /// rebuilt. |
9663 | 4.80k | SourceLocation getBaseLocation() { return Loc; } |
9664 | | |
9665 | | /// \brief Returns the name of the entity whose type is being rebuilt. |
9666 | 9.68k | DeclarationName getBaseEntity() { return Entity; } |
9667 | | |
9668 | | /// \brief Sets the "base" location and entity when that |
9669 | | /// information is known based on another transformation. |
9670 | 9.60k | void setBase(SourceLocation Loc, DeclarationName Entity) { |
9671 | 9.60k | this->Loc = Loc; |
9672 | 9.60k | this->Entity = Entity; |
9673 | 9.60k | } |
9674 | | |
9675 | 0 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
9676 | 0 | // Lambdas never need to be transformed. |
9677 | 0 | return E; |
9678 | 0 | } |
9679 | | }; |
9680 | | } // end anonymous namespace |
9681 | | |
9682 | | /// \brief Rebuilds a type within the context of the current instantiation. |
9683 | | /// |
9684 | | /// The type \p T is part of the type of an out-of-line member definition of |
9685 | | /// a class template (or class template partial specialization) that was parsed |
9686 | | /// and constructed before we entered the scope of the class template (or |
9687 | | /// partial specialization thereof). This routine will rebuild that type now |
9688 | | /// that we have entered the declarator's scope, which may produce different |
9689 | | /// canonical types, e.g., |
9690 | | /// |
9691 | | /// \code |
9692 | | /// template<typename T> |
9693 | | /// struct X { |
9694 | | /// typedef T* pointer; |
9695 | | /// pointer data(); |
9696 | | /// }; |
9697 | | /// |
9698 | | /// template<typename T> |
9699 | | /// typename X<T>::pointer X<T>::data() { ... } |
9700 | | /// \endcode |
9701 | | /// |
9702 | | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, |
9703 | | /// since we do not know that we can look into X<T> when we parsed the type. |
9704 | | /// This function will rebuild the type, performing the lookup of "pointer" |
9705 | | /// in X<T> and returning an ElaboratedType whose canonical type is the same |
9706 | | /// as the canonical type of T*, allowing the return types of the out-of-line |
9707 | | /// definition and the declaration to match. |
9708 | | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
9709 | | SourceLocation Loc, |
9710 | 904 | DeclarationName Name) { |
9711 | 904 | if (!T || 904 !T->getType()->isDependentType()904 ) |
9712 | 262 | return T; |
9713 | 642 | |
9714 | 642 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
9715 | 642 | return Rebuilder.TransformType(T); |
9716 | 642 | } |
9717 | | |
9718 | 3 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
9719 | 3 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
9720 | 3 | DeclarationName()); |
9721 | 3 | return Rebuilder.TransformExpr(E); |
9722 | 3 | } |
9723 | | |
9724 | 3.13k | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
9725 | 3.13k | if (SS.isInvalid()) |
9726 | 0 | return true; |
9727 | 3.13k | |
9728 | 3.13k | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
9729 | 3.13k | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
9730 | 3.13k | DeclarationName()); |
9731 | 3.13k | NestedNameSpecifierLoc Rebuilt |
9732 | 3.13k | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
9733 | 3.13k | if (!Rebuilt) |
9734 | 0 | return true; |
9735 | 3.13k | |
9736 | 3.13k | SS.Adopt(Rebuilt); |
9737 | 3.13k | return false; |
9738 | 3.13k | } |
9739 | | |
9740 | | /// \brief Rebuild the template parameters now that we know we're in a current |
9741 | | /// instantiation. |
9742 | | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
9743 | 6.69k | TemplateParameterList *Params) { |
9744 | 16.1k | for (unsigned I = 0, N = Params->size(); I != N16.1k ; ++I9.50k ) { |
9745 | 9.50k | Decl *Param = Params->getParam(I); |
9746 | 9.50k | |
9747 | 9.50k | // There is nothing to rebuild in a type parameter. |
9748 | 9.50k | if (isa<TemplateTypeParmDecl>(Param)) |
9749 | 9.19k | continue; |
9750 | 306 | |
9751 | 306 | // Rebuild the template parameter list of a template template parameter. |
9752 | 306 | if (TemplateTemplateParmDecl *306 TTP306 |
9753 | 22 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
9754 | 22 | if (RebuildTemplateParamsInCurrentInstantiation( |
9755 | 22 | TTP->getTemplateParameters())) |
9756 | 0 | return true; |
9757 | 22 | |
9758 | 22 | continue; |
9759 | 22 | } |
9760 | 284 | |
9761 | 284 | // Rebuild the type of a non-type template parameter. |
9762 | 284 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
9763 | 284 | TypeSourceInfo *NewTSI |
9764 | 284 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
9765 | 284 | NTTP->getLocation(), |
9766 | 284 | NTTP->getDeclName()); |
9767 | 284 | if (!NewTSI) |
9768 | 0 | return true; |
9769 | 284 | |
9770 | 284 | if (284 NewTSI != NTTP->getTypeSourceInfo()284 ) { |
9771 | 22 | NTTP->setTypeSourceInfo(NewTSI); |
9772 | 22 | NTTP->setType(NewTSI->getType()); |
9773 | 22 | } |
9774 | 9.50k | } |
9775 | 6.69k | |
9776 | 6.69k | return false; |
9777 | 6.69k | } |
9778 | | |
9779 | | /// \brief Produces a formatted string that describes the binding of |
9780 | | /// template parameters to template arguments. |
9781 | | std::string |
9782 | | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
9783 | 633 | const TemplateArgumentList &Args) { |
9784 | 633 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
9785 | 633 | } |
9786 | | |
9787 | | std::string |
9788 | | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
9789 | | const TemplateArgument *Args, |
9790 | 701 | unsigned NumArgs) { |
9791 | 701 | SmallString<128> Str; |
9792 | 701 | llvm::raw_svector_ostream Out(Str); |
9793 | 701 | |
9794 | 701 | if (!Params || 701 Params->size() == 0701 || NumArgs == 0701 ) |
9795 | 17 | return std::string(); |
9796 | 684 | |
9797 | 1.55k | for (unsigned I = 0, N = Params->size(); 684 I != N1.55k ; ++I874 ) { |
9798 | 906 | if (I >= NumArgs) |
9799 | 32 | break; |
9800 | 874 | |
9801 | 874 | if (874 I == 0874 ) |
9802 | 684 | Out << "[with "; |
9803 | 874 | else |
9804 | 190 | Out << ", "; |
9805 | 874 | |
9806 | 874 | if (const IdentifierInfo *Id874 = Params->getParam(I)->getIdentifier()) { |
9807 | 823 | Out << Id->getName(); |
9808 | 874 | } else { |
9809 | 51 | Out << '$' << I; |
9810 | 51 | } |
9811 | 906 | |
9812 | 906 | Out << " = "; |
9813 | 906 | Args[I].print(getPrintingPolicy(), Out); |
9814 | 906 | } |
9815 | 701 | |
9816 | 701 | Out << ']'; |
9817 | 701 | return Out.str(); |
9818 | 701 | } |
9819 | | |
9820 | | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
9821 | 235 | CachedTokens &Toks) { |
9822 | 235 | if (!FD) |
9823 | 0 | return; |
9824 | 235 | |
9825 | 235 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
9826 | 235 | |
9827 | 235 | // Take tokens to avoid allocations |
9828 | 235 | LPT->Toks.swap(Toks); |
9829 | 235 | LPT->D = FnD; |
9830 | 235 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
9831 | 235 | |
9832 | 235 | FD->setLateTemplateParsed(true); |
9833 | 235 | } |
9834 | | |
9835 | 178 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
9836 | 178 | if (!FD) |
9837 | 0 | return; |
9838 | 178 | FD->setLateTemplateParsed(false); |
9839 | 178 | } |
9840 | | |
9841 | 128 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
9842 | 128 | DeclContext *DC = CurContext; |
9843 | 128 | |
9844 | 128 | while (DC128 ) { |
9845 | 128 | if (CXXRecordDecl *RD128 = dyn_cast<CXXRecordDecl>(CurContext)) { |
9846 | 128 | const FunctionDecl *FD = RD->isLocalClass(); |
9847 | 20 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
9848 | 0 | } else if (0 DC->isTranslationUnit() || 0 DC->isNamespace()0 ) |
9849 | 0 | return false; |
9850 | 0 |
|
9851 | 0 | DC = DC->getParent(); |
9852 | 0 | } |
9853 | 0 | return false; |
9854 | 128 | } |
9855 | | |
9856 | | namespace { |
9857 | | /// \brief Walk the path from which a declaration was instantiated, and check |
9858 | | /// that every explicit specialization along that path is visible. This enforces |
9859 | | /// C++ [temp.expl.spec]/6: |
9860 | | /// |
9861 | | /// If a template, a member template or a member of a class template is |
9862 | | /// explicitly specialized then that specialization shall be declared before |
9863 | | /// the first use of that specialization that would cause an implicit |
9864 | | /// instantiation to take place, in every translation unit in which such a |
9865 | | /// use occurs; no diagnostic is required. |
9866 | | /// |
9867 | | /// and also C++ [temp.class.spec]/1: |
9868 | | /// |
9869 | | /// A partial specialization shall be declared before the first use of a |
9870 | | /// class template specialization that would make use of the partial |
9871 | | /// specialization as the result of an implicit or explicit instantiation |
9872 | | /// in every translation unit in which such a use occurs; no diagnostic is |
9873 | | /// required. |
9874 | | class ExplicitSpecializationVisibilityChecker { |
9875 | | Sema &S; |
9876 | | SourceLocation Loc; |
9877 | | llvm::SmallVector<Module *, 8> Modules; |
9878 | | |
9879 | | public: |
9880 | | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
9881 | 22.5k | : S(S), Loc(Loc) {} |
9882 | | |
9883 | 22.5k | void check(NamedDecl *ND) { |
9884 | 22.5k | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
9885 | 548 | return checkImpl(FD); |
9886 | 22.0k | if (auto *22.0k RD22.0k = dyn_cast<CXXRecordDecl>(ND)) |
9887 | 21.0k | return checkImpl(RD); |
9888 | 948 | if (auto *948 VD948 = dyn_cast<VarDecl>(ND)) |
9889 | 72 | return checkImpl(VD); |
9890 | 876 | if (auto *876 ED876 = dyn_cast<EnumDecl>(ND)) |
9891 | 0 | return checkImpl(ED); |
9892 | 876 | } |
9893 | | |
9894 | | private: |
9895 | 180 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
9896 | 40 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
9897 | 140 | : Sema::MissingImportKind::ExplicitSpecialization; |
9898 | 180 | const bool Recover = true; |
9899 | 180 | |
9900 | 180 | // If we got a custom set of modules (because only a subset of the |
9901 | 180 | // declarations are interesting), use them, otherwise let |
9902 | 180 | // diagnoseMissingImport intelligently pick some. |
9903 | 180 | if (Modules.empty()) |
9904 | 40 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
9905 | 180 | else |
9906 | 140 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
9907 | 180 | } |
9908 | | |
9909 | | // Check a specific declaration. There are three problematic cases: |
9910 | | // |
9911 | | // 1) The declaration is an explicit specialization of a template |
9912 | | // specialization. |
9913 | | // 2) The declaration is an explicit specialization of a member of an |
9914 | | // templated class. |
9915 | | // 3) The declaration is an instantiation of a template, and that template |
9916 | | // is an explicit specialization of a member of a templated class. |
9917 | | // |
9918 | | // We don't need to go any deeper than that, as the instantiation of the |
9919 | | // surrounding class / etc is not triggered by whatever triggered this |
9920 | | // instantiation, and thus should be checked elsewhere. |
9921 | | template<typename SpecDecl> |
9922 | 21.6k | void checkImpl(SpecDecl *Spec) { |
9923 | 21.6k | bool IsHiddenExplicitSpecialization = false; |
9924 | 21.6k | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization21.6k ) { |
9925 | 183 | IsHiddenExplicitSpecialization = |
9926 | 183 | Spec->getMemberSpecializationInfo() |
9927 | 30 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
9928 | 153 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
9929 | 21.6k | } else { |
9930 | 21.5k | checkInstantiated(Spec); |
9931 | 21.5k | } |
9932 | 21.6k | |
9933 | 21.6k | if (IsHiddenExplicitSpecialization) |
9934 | 95 | diagnose(Spec->getMostRecentDecl(), false); |
9935 | 21.6k | } Unexecuted instantiation: SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkImpl<clang::EnumDecl>(clang::EnumDecl*) SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkImpl<clang::FunctionDecl>(clang::FunctionDecl*) Line | Count | Source | 9922 | 548 | void checkImpl(SpecDecl *Spec) { | 9923 | 548 | bool IsHiddenExplicitSpecialization = false; | 9924 | 548 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization548 ) { | 9925 | 21 | IsHiddenExplicitSpecialization = | 9926 | 21 | Spec->getMemberSpecializationInfo() | 9927 | 10 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) | 9928 | 11 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); | 9929 | 548 | } else { | 9930 | 527 | checkInstantiated(Spec); | 9931 | 527 | } | 9932 | 548 | | 9933 | 548 | if (IsHiddenExplicitSpecialization) | 9934 | 10 | diagnose(Spec->getMostRecentDecl(), false); | 9935 | 548 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkImpl<clang::VarDecl>(clang::VarDecl*) Line | Count | Source | 9922 | 72 | void checkImpl(SpecDecl *Spec) { | 9923 | 72 | bool IsHiddenExplicitSpecialization = false; | 9924 | 72 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization72 ) { | 9925 | 11 | IsHiddenExplicitSpecialization = | 9926 | 11 | Spec->getMemberSpecializationInfo() | 9927 | 5 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) | 9928 | 6 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); | 9929 | 72 | } else { | 9930 | 61 | checkInstantiated(Spec); | 9931 | 61 | } | 9932 | 72 | | 9933 | 72 | if (IsHiddenExplicitSpecialization) | 9934 | 10 | diagnose(Spec->getMostRecentDecl(), false); | 9935 | 72 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkImpl<clang::CXXRecordDecl>(clang::CXXRecordDecl*) Line | Count | Source | 9922 | 21.0k | void checkImpl(SpecDecl *Spec) { | 9923 | 21.0k | bool IsHiddenExplicitSpecialization = false; | 9924 | 21.0k | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization21.0k ) { | 9925 | 151 | IsHiddenExplicitSpecialization = | 9926 | 151 | Spec->getMemberSpecializationInfo() | 9927 | 15 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) | 9928 | 136 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); | 9929 | 21.0k | } else { | 9930 | 20.9k | checkInstantiated(Spec); | 9931 | 20.9k | } | 9932 | 21.0k | | 9933 | 21.0k | if (IsHiddenExplicitSpecialization) | 9934 | 75 | diagnose(Spec->getMostRecentDecl(), false); | 9935 | 21.0k | } |
|
9936 | | |
9937 | 527 | void checkInstantiated(FunctionDecl *FD) { |
9938 | 527 | if (auto *TD = FD->getPrimaryTemplate()) |
9939 | 364 | checkTemplate(TD); |
9940 | 527 | } |
9941 | | |
9942 | 20.9k | void checkInstantiated(CXXRecordDecl *RD) { |
9943 | 20.9k | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
9944 | 20.9k | if (!SD) |
9945 | 3.36k | return; |
9946 | 17.5k | |
9947 | 17.5k | auto From = SD->getSpecializedTemplateOrPartial(); |
9948 | 17.5k | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
9949 | 17.4k | checkTemplate(TD); |
9950 | 160 | else if (auto *160 TD160 = |
9951 | 160 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
9952 | 160 | if (!S.hasVisibleDeclaration(TD)) |
9953 | 30 | diagnose(TD, true); |
9954 | 160 | checkTemplate(TD); |
9955 | 160 | } |
9956 | 20.9k | } |
9957 | | |
9958 | 61 | void checkInstantiated(VarDecl *RD) { |
9959 | 61 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
9960 | 61 | if (!SD) |
9961 | 17 | return; |
9962 | 44 | |
9963 | 44 | auto From = SD->getSpecializedTemplateOrPartial(); |
9964 | 44 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
9965 | 32 | checkTemplate(TD); |
9966 | 12 | else if (auto *12 TD12 = |
9967 | 12 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
9968 | 12 | if (!S.hasVisibleDeclaration(TD)) |
9969 | 10 | diagnose(TD, true); |
9970 | 12 | checkTemplate(TD); |
9971 | 12 | } |
9972 | 61 | } |
9973 | | |
9974 | 0 | void checkInstantiated(EnumDecl *FD) {} |
9975 | | |
9976 | | template<typename TemplDecl> |
9977 | 17.9k | void checkTemplate(TemplDecl *TD) { |
9978 | 17.9k | if (TD->isMemberSpecialization()17.9k ) { |
9979 | 60 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
9980 | 45 | diagnose(TD->getMostRecentDecl(), false); |
9981 | 60 | } |
9982 | 17.9k | } SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkTemplate<clang::VarTemplatePartialSpecializationDecl>(clang::VarTemplatePartialSpecializationDecl*) Line | Count | Source | 9977 | 12 | void checkTemplate(TemplDecl *TD) { | 9978 | 12 | if (TD->isMemberSpecialization()12 ) { | 9979 | 0 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | 9980 | 0 | diagnose(TD->getMostRecentDecl(), false); | 9981 | 0 | } | 9982 | 12 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkTemplate<clang::FunctionTemplateDecl>(clang::FunctionTemplateDecl*) Line | Count | Source | 9977 | 364 | void checkTemplate(TemplDecl *TD) { | 9978 | 364 | if (TD->isMemberSpecialization()364 ) { | 9979 | 17 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | 9980 | 10 | diagnose(TD->getMostRecentDecl(), false); | 9981 | 17 | } | 9982 | 364 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkTemplate<clang::VarTemplateDecl>(clang::VarTemplateDecl*) Line | Count | Source | 9977 | 32 | void checkTemplate(TemplDecl *TD) { | 9978 | 32 | if (TD->isMemberSpecialization()32 ) { | 9979 | 12 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | 9980 | 10 | diagnose(TD->getMostRecentDecl(), false); | 9981 | 12 | } | 9982 | 32 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkTemplate<clang::ClassTemplatePartialSpecializationDecl>(clang::ClassTemplatePartialSpecializationDecl*) Line | Count | Source | 9977 | 160 | void checkTemplate(TemplDecl *TD) { | 9978 | 160 | if (TD->isMemberSpecialization()160 ) { | 9979 | 0 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | 9980 | 0 | diagnose(TD->getMostRecentDecl(), false); | 9981 | 0 | } | 9982 | 160 | } |
SemaTemplate.cpp:void (anonymous namespace)::ExplicitSpecializationVisibilityChecker::checkTemplate<clang::ClassTemplateDecl>(clang::ClassTemplateDecl*) Line | Count | Source | 9977 | 17.4k | void checkTemplate(TemplDecl *TD) { | 9978 | 17.4k | if (TD->isMemberSpecialization()17.4k ) { | 9979 | 31 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) | 9980 | 25 | diagnose(TD->getMostRecentDecl(), false); | 9981 | 31 | } | 9982 | 17.4k | } |
|
9983 | | }; |
9984 | | } // end anonymous namespace |
9985 | | |
9986 | 8.35M | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
9987 | 8.35M | if (!getLangOpts().Modules) |
9988 | 8.33M | return; |
9989 | 22.5k | |
9990 | 22.5k | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
9991 | 22.5k | } |
9992 | | |
9993 | | /// \brief Check whether a template partial specialization that we've discovered |
9994 | | /// is hidden, and produce suitable diagnostics if so. |
9995 | | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
9996 | 0 | NamedDecl *Spec) { |
9997 | 0 | llvm::SmallVector<Module *, 8> Modules; |
9998 | 0 | if (!hasVisibleDeclaration(Spec, &Modules)) |
9999 | 0 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
10000 | 0 | MissingImportKind::PartialSpecialization, |
10001 | 0 | /*Recover*/true); |
10002 | 0 | } |