 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
79.3% |
1334 / 1682 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
96.2% |
1618 / 1682 |
| |
|
Line Coverage: |
89.0% |
1635 / 1838 |
| |
 |
|
 |
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 "Sema.h"
13 : #include "Lookup.h"
14 : #include "TreeTransform.h"
15 : #include "clang/AST/ASTContext.h"
16 : #include "clang/AST/Expr.h"
17 : #include "clang/AST/ExprCXX.h"
18 : #include "clang/AST/DeclTemplate.h"
19 : #include "clang/Parse/DeclSpec.h"
20 : #include "clang/Parse/Template.h"
21 : #include "clang/Basic/LangOptions.h"
22 : #include "clang/Basic/PartialDiagnostic.h"
23 : #include "llvm/ADT/StringExtras.h"
24 : using namespace clang;
25 :
26 : /// \brief Determine whether the declaration found is acceptable as the name
27 : /// of a template and, if so, return that template declaration. Otherwise,
28 : /// returns NULL.
29 3147: static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) {
0: branch 0 not taken
3147: branch 1 taken
30 3147: if (!D)
31 0: return 0;
32 :
2527: branch 1 taken
620: branch 2 taken
33 3147: if (isa<TemplateDecl>(D))
34 2527: return D;
35 :
92: branch 1 taken
528: branch 2 taken
36 620: if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
37 : // C++ [temp.local]p1:
38 : // Like normal (non-template) classes, class templates have an
39 : // injected-class-name (Clause 9). The injected-class-name
40 : // can be used with or without a template-argument-list. When
41 : // it is used without a template-argument-list, it is
42 : // equivalent to the injected-class-name followed by the
43 : // template-parameters of the class template enclosed in
44 : // <>. When it is used with a template-argument-list, it
45 : // refers to the specified class template specialization,
46 : // which could be the current specialization or another
47 : // specialization.
90: branch 1 taken
2: branch 2 taken
48 92: if (Record->isInjectedClassName()) {
49 90: Record = cast<CXXRecordDecl>(Record->getDeclContext());
63: branch 1 taken
27: branch 2 taken
50 90: if (Record->getDescribedClassTemplate())
51 63: return Record->getDescribedClassTemplate();
52 :
27: branch 0 taken
0: branch 1 not taken
53 27: if (ClassTemplateSpecializationDecl *Spec
54 27: = dyn_cast<ClassTemplateSpecializationDecl>(Record))
55 27: return Spec->getSpecializedTemplate();
56 : }
57 :
58 2: return 0;
59 : }
60 :
61 528: return 0;
62 : }
63 :
64 3118: static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
65 3118: LookupResult::Filter filter = R.makeFilter();
3147: branch 1 taken
3118: branch 2 taken
66 9383: while (filter.hasNext()) {
67 3147: NamedDecl *Orig = filter.next();
68 3147: NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl());
530: branch 0 taken
2617: branch 1 taken
69 3147: if (!Repl)
70 530: filter.erase();
107: branch 0 taken
2510: branch 1 taken
71 2617: else if (Repl != Orig)
72 107: filter.replace(Repl);
73 : }
74 3118: filter.done();
75 3118: }
76 :
77 : TemplateNameKind Sema::isTemplateName(Scope *S,
78 : const CXXScopeSpec &SS,
79 : UnqualifiedId &Name,
80 : TypeTy *ObjectTypePtr,
81 : bool EnteringContext,
82 3098: TemplateTy &TemplateResult) {
3098: branch 1 taken
0: branch 2 not taken
83 3098: assert(getLangOptions().CPlusPlus && "No template names in C!");
84 :
85 3098: DeclarationName TName;
86 :
3095: branch 1 taken
3: branch 2 taken
0: branch 3 not taken
0: branch 4 not taken
87 3098: switch (Name.getKind()) {
88 : case UnqualifiedId::IK_Identifier:
89 3095: TName = DeclarationName(Name.Identifier);
90 3095: break;
91 :
92 : case UnqualifiedId::IK_OperatorFunctionId:
93 : TName = Context.DeclarationNames.getCXXOperatorName(
94 3: Name.OperatorFunctionId.Operator);
95 3: break;
96 :
97 : case UnqualifiedId::IK_LiteralOperatorId:
98 0: TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
99 0: break;
100 :
101 : default:
102 0: return TNK_Non_template;
103 : }
104 :
105 3098: QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
106 :
107 : LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
108 3098: LookupOrdinaryName);
109 3098: R.suppressDiagnostics();
110 3098: LookupTemplateName(R, S, SS, ObjectType, EnteringContext);
526: branch 1 taken
2572: branch 2 taken
111 3098: if (R.empty())
112 526: return TNK_Non_template;
113 :
114 2572: TemplateName Template;
115 : TemplateNameKind TemplateKind;
116 :
117 2572: unsigned ResultCount = R.end() - R.begin();
28: branch 0 taken
2544: branch 1 taken
118 2572: if (ResultCount > 1) {
119 : // We assume that we'll preserve the qualifier from a function
120 : // template name in other ways.
121 28: Template = Context.getOverloadedTemplateName(R.begin(), R.end());
122 28: TemplateKind = TNK_Function_template;
123 : } else {
124 2544: TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
125 :
297: branch 1 taken
2247: branch 2 taken
297: branch 4 taken
0: branch 5 not taken
297: branch 6 taken
2247: branch 7 taken
126 2544: if (SS.isSet() && !SS.isInvalid()) {
127 : NestedNameSpecifier *Qualifier
128 297: = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
129 297: Template = Context.getQualifiedTemplateName(Qualifier, false, TD);
130 : } else {
131 2247: Template = TemplateName(TD);
132 : }
133 :
204: branch 1 taken
2340: branch 2 taken
134 2544: if (isa<FunctionTemplateDecl>(TD))
135 204: TemplateKind = TNK_Function_template;
136 : else {
17: branch 1 taken
2323: branch 2 taken
0: branch 4 not taken
17: branch 5 taken
137 2340: assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
138 2340: TemplateKind = TNK_Type_template;
139 : }
140 : }
141 :
142 2572: TemplateResult = TemplateTy::make(Template);
143 2572: return TemplateKind;
144 : }
145 :
146 : bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
147 : SourceLocation IILoc,
148 : Scope *S,
149 : const CXXScopeSpec *SS,
150 : TemplateTy &SuggestedTemplate,
151 2: TemplateNameKind &SuggestedKind) {
152 : // We can't recover unless there's a dependent scope specifier preceding the
153 : // template name.
2: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
1: branch 4 taken
1: branch 6 taken
0: branch 7 not taken
0: branch 9 not taken
1: branch 10 taken
1: branch 11 taken
1: branch 12 taken
154 2: if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
155 : computeDeclContext(*SS))
156 1: return false;
157 :
158 : // The code is missing a 'template' keyword prior to the dependent template
159 : // name.
160 1: NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
161 : Diag(IILoc, diag::err_template_kw_missing)
162 : << Qualifier << II.getName()
163 1: << CodeModificationHint::CreateInsertion(IILoc, "template ");
164 : SuggestedTemplate
165 1: = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
166 1: SuggestedKind = TNK_Dependent_template_name;
167 1: return true;
168 : }
169 :
170 : void Sema::LookupTemplateName(LookupResult &Found,
171 : Scope *S, const CXXScopeSpec &SS,
172 : QualType ObjectType,
173 3099: bool EnteringContext) {
174 : // Determine where to perform name lookup
175 3099: DeclContext *LookupCtx = 0;
176 3099: bool isDependent = false;
28: branch 1 taken
3071: branch 2 taken
177 3099: if (!ObjectType.isNull()) {
178 : // This nested-name-specifier occurs in a member access expression, e.g.,
179 : // x->B::f, and we are looking into the type of the object.
28: branch 1 taken
0: branch 2 not taken
180 28: assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
181 28: LookupCtx = computeDeclContext(ObjectType);
182 28: isDependent = ObjectType->isDependentType();
183 : assert((isDependent || !ObjectType->isIncompleteType()) &&
22: branch 0 taken
6: branch 1 taken
22: branch 4 taken
0: branch 5 not taken
184 50: "Caller should have completed object type");
317: branch 1 taken
2754: branch 2 taken
185 3071: } else if (SS.isSet()) {
186 : // This nested-name-specifier occurs after another nested-name-specifier,
187 : // so long into the context associated with the prior nested-name-specifier.
188 317: LookupCtx = computeDeclContext(SS, EnteringContext);
189 317: isDependent = isDependentScopeSpecifier(SS);
190 :
191 : // The declaration context must be complete.
313: branch 0 taken
4: branch 1 taken
4: branch 3 taken
309: branch 4 taken
4: branch 5 taken
313: branch 6 taken
192 317: if (LookupCtx && RequireCompleteDeclContext(SS))
193 4: return;
194 : }
195 :
196 3095: bool ObjectTypeSearchedInScope = false;
337: branch 0 taken
2758: branch 1 taken
197 3095: if (LookupCtx) {
198 : // Perform "qualified" name lookup into the declaration context we
199 : // computed, which is either the type of the base of a member access
200 : // expression or the declaration context associated with a prior
201 : // nested-name-specifier.
202 337: LookupQualifiedName(Found, LookupCtx);
203 :
28: branch 1 taken
309: branch 2 taken
4: branch 4 taken
24: branch 5 taken
4: branch 6 taken
333: branch 7 taken
204 337: if (!ObjectType.isNull() && Found.empty()) {
205 : // C++ [basic.lookup.classref]p1:
206 : // In a class member access expression (5.2.5), if the . or -> token is
207 : // immediately followed by an identifier followed by a <, the
208 : // identifier must be looked up to determine whether the < is the
209 : // beginning of a template argument list (14.2) or a less-than operator.
210 : // The identifier is first looked up in the class of the object
211 : // expression. If the identifier is not found, it is then looked up in
212 : // the context of the entire postfix-expression and shall name a class
213 : // or function template.
214 : //
215 : // FIXME: When we're instantiating a template, do we actually have to
216 : // look in the scope of the template? Seems fishy...
2: branch 0 taken
2: branch 1 taken
217 4: if (S) LookupName(Found, S);
218 4: ObjectTypeSearchedInScope = true;
219 : }
4: branch 0 taken
2754: branch 1 taken
220 2758: } else if (isDependent) {
221 : // We cannot look into a dependent object type or nested nme
222 : // specifier.
223 4: return;
224 : } else {
225 : // Perform unqualified name lookup in the current scope.
226 2754: LookupName(Found, S);
227 : }
228 :
229 : // FIXME: Cope with ambiguous name-lookup results.
230 : assert(!Found.isAmbiguous() &&
3091: branch 1 taken
0: branch 2 not taken
231 3091: "Cannot handle template name-lookup ambiguities");
232 :
16: branch 1 taken
3075: branch 2 taken
10: branch 3 taken
6: branch 4 taken
10: branch 5 taken
3081: branch 6 taken
233 3107: if (Found.empty() && !isDependent) {
234 : // If we did not find any names, attempt to correct any typos.
235 10: DeclarationName Name = Found.getLookupName();
4: branch 1 taken
6: branch 2 taken
236 10: if (CorrectTypo(Found, S, &SS, LookupCtx)) {
237 4: FilterAcceptableTemplateNames(Context, Found);
4: branch 1 taken
0: branch 2 not taken
4: branch 6 taken
0: branch 7 not taken
4: branch 8 taken
0: branch 9 not taken
238 4: if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) {
2: branch 0 taken
2: branch 1 taken
239 4: if (LookupCtx)
240 : Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
241 : << Name << LookupCtx << Found.getLookupName() << SS.getRange()
242 : << CodeModificationHint::CreateReplacement(Found.getNameLoc(),
243 2: Found.getLookupName().getAsString());
244 : else
245 : Diag(Found.getNameLoc(), diag::err_no_template_suggest)
246 : << Name << Found.getLookupName()
247 : << CodeModificationHint::CreateReplacement(Found.getNameLoc(),
248 2: Found.getLookupName().getAsString());
4: branch 1 taken
0: branch 2 not taken
249 4: if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
250 : Diag(Template->getLocation(), diag::note_previous_decl)
251 4: << Template->getDeclName();
252 : } else
253 0: Found.clear();
254 : } else {
255 6: Found.clear();
256 : }
257 : }
258 :
259 3091: FilterAcceptableTemplateNames(Context, Found);
518: branch 1 taken
2573: branch 2 taken
260 3091: if (Found.empty())
261 518: return;
262 :
2552: branch 0 taken
21: branch 1 taken
23: branch 3 taken
2529: branch 4 taken
23: branch 5 taken
0: branch 6 not taken
23: branch 7 taken
2550: branch 8 taken
263 2573: if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
264 : // C++ [basic.lookup.classref]p1:
265 : // [...] If the lookup in the class of the object expression finds a
266 : // template, the name is also looked up in the context of the entire
267 : // postfix-expression and [...]
268 : //
269 : LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
270 23: LookupOrdinaryName);
271 23: LookupName(FoundOuter, S);
272 23: FilterAcceptableTemplateNames(Context, FoundOuter);
273 : // FIXME: Handle ambiguities in this lookup better
274 :
4: branch 1 taken
19: branch 2 taken
275 23: if (FoundOuter.empty()) {
276 : // - if the name is not found, the name found in the class of the
277 : // object expression is used, otherwise
0: branch 1 not taken
4: branch 2 taken
278 4: } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
279 : // - if the name is found in the context of the entire
280 : // postfix-expression and does not name a class template, the name
281 : // found in the class of the object expression is used, otherwise
282 : } else {
283 : // - if the name found is a class template, it must refer to the same
284 : // entity as the one found in the class of the object expression,
285 : // otherwise the program is ill-formed.
0: branch 1 not taken
0: branch 2 not taken
0: branch 7 not taken
0: branch 8 not taken
0: branch 9 not taken
0: branch 10 not taken
286 0: if (!Found.isSingleResult() ||
287 : Found.getFoundDecl()->getCanonicalDecl()
288 : != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
289 : Diag(Found.getNameLoc(),
290 : diag::err_nested_name_member_ref_lookup_ambiguous)
291 0: << Found.getLookupName();
292 : Diag(Found.getRepresentativeDecl()->getLocation(),
293 : diag::note_ambig_member_ref_object_type)
294 0: << ObjectType;
295 : Diag(FoundOuter.getFoundDecl()->getLocation(),
296 0: diag::note_ambig_member_ref_scope);
297 :
298 : // Recover by taking the template that we found in the object
299 : // expression's type.
300 : }
301 23: }
302 : }
303 : }
304 :
305 : /// ActOnDependentIdExpression - Handle a dependent id-expression that
306 : /// was just parsed. This is only possible with an explicit scope
307 : /// specifier naming a dependent type.
308 : Sema::OwningExprResult
309 : Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
310 : DeclarationName Name,
311 : SourceLocation NameLoc,
312 : bool isAddressOfOperand,
313 53: const TemplateArgumentListInfo *TemplateArgs) {
314 : NestedNameSpecifier *Qualifier
315 53: = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
316 :
49: branch 0 taken
4: branch 1 taken
25: branch 3 taken
24: branch 4 taken
15: branch 7 taken
10: branch 8 taken
15: branch 9 taken
38: branch 10 taken
317 53: if (!isAddressOfOperand &&
318 : isa<CXXMethodDecl>(CurContext) &&
319 : cast<CXXMethodDecl>(CurContext)->isInstance()) {
320 15: QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context);
321 :
322 : // Since the 'this' expression is synthesized, we don't need to
323 : // perform the double-lookup check.
324 15: NamedDecl *FirstQualifierInScope = 0;
325 :
326 : return Owned(CXXDependentScopeMemberExpr::Create(Context,
327 : /*This*/ 0, ThisType,
328 : /*IsArrow*/ true,
329 : /*Op*/ SourceLocation(),
330 : Qualifier, SS.getRange(),
331 : FirstQualifierInScope,
332 : Name, NameLoc,
333 15: TemplateArgs));
334 : }
335 :
336 38: return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs);
337 : }
338 :
339 : Sema::OwningExprResult
340 : Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
341 : DeclarationName Name,
342 : SourceLocation NameLoc,
343 39: const TemplateArgumentListInfo *TemplateArgs) {
344 : return Owned(DependentScopeDeclRefExpr::Create(Context,
345 : static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
346 : SS.getRange(),
347 : Name, NameLoc,
348 39: TemplateArgs));
349 : }
350 :
351 : /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
352 : /// that the template parameter 'PrevDecl' is being shadowed by a new
353 : /// declaration at location Loc. Returns true to indicate that this is
354 : /// an error, and false otherwise.
355 6: bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
6: branch 1 taken
0: branch 2 not taken
356 6: assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
357 :
358 : // Microsoft Visual C++ permits template parameters to be shadowed.
0: branch 1 not taken
6: branch 2 taken
359 6: if (getLangOptions().Microsoft)
360 0: return false;
361 :
362 : // C++ [temp.local]p4:
363 : // A template-parameter shall not be redeclared within its
364 : // scope (including nested scopes).
365 : Diag(Loc, diag::err_template_param_shadow)
366 6: << cast<NamedDecl>(PrevDecl)->getDeclName();
367 6: Diag(PrevDecl->getLocation(), diag::note_template_param_here);
368 6: return true;
369 : }
370 :
371 : /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
372 : /// the parameter D to reference the templated declaration and return a pointer
373 : /// to the template declaration. Otherwise, do nothing to D and return null.
374 27276: TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) {
3307: branch 2 taken
23969: branch 3 taken
375 27276: if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) {
376 3307: D = DeclPtrTy::make(Temp->getTemplatedDecl());
377 3307: return Temp;
378 : }
379 23969: return 0;
380 : }
381 :
382 : static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
383 3135: const ParsedTemplateArgument &Arg) {
384 :
2573: branch 1 taken
516: branch 2 taken
46: branch 3 taken
0: branch 4 not taken
385 3135: switch (Arg.getKind()) {
386 : case ParsedTemplateArgument::Type: {
387 : TypeSourceInfo *DI;
388 2573: QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
0: branch 0 not taken
2573: branch 1 taken
389 2573: if (!DI)
390 0: DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
391 2573: return TemplateArgumentLoc(TemplateArgument(T), DI);
392 : }
393 :
394 : case ParsedTemplateArgument::NonType: {
395 516: Expr *E = static_cast<Expr *>(Arg.getAsExpr());
396 516: return TemplateArgumentLoc(TemplateArgument(E), E);
397 : }
398 :
399 : case ParsedTemplateArgument::Template: {
400 : TemplateName Template
401 46: = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get());
402 : return TemplateArgumentLoc(TemplateArgument(Template),
403 : Arg.getScopeSpec().getRange(),
404 46: Arg.getLocation());
405 : }
406 : }
407 :
408 0: llvm_unreachable("Unhandled parsed template argument");
409 : return TemplateArgumentLoc();
410 : }
411 :
412 : /// \brief Translates template arguments as provided by the parser
413 : /// into template arguments used by semantic analysis.
414 : void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
415 2527: TemplateArgumentListInfo &TemplateArgs) {
3124: branch 1 taken
2527: branch 2 taken
416 5651: for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
417 : TemplateArgs.addArgument(translateTemplateArgument(*this,
418 3124: TemplateArgsIn[I]));
419 2527: }
420 :
421 : /// ActOnTypeParameter - Called when a C++ template type parameter
422 : /// (e.g., "typename T") has been parsed. Typename specifies whether
423 : /// the keyword "typename" was used to declare the type parameter
424 : /// (otherwise, "class" was used), and KeyLoc is the location of the
425 : /// "class" or "typename" keyword. ParamName is the name of the
426 : /// parameter (NULL indicates an unnamed template parameter) and
427 : /// ParamName is the location of the parameter name (if any).
428 : /// If the type parameter has a default argument, it will be added
429 : /// later via ActOnTypeParameterDefault.
430 : Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
431 : SourceLocation EllipsisLoc,
432 : SourceLocation KeyLoc,
433 : IdentifierInfo *ParamName,
434 : SourceLocation ParamNameLoc,
435 1713: unsigned Depth, unsigned Position) {
436 : assert(S->isTemplateParamScope() &&
1713: branch 1 taken
0: branch 2 not taken
437 1713: "Template type parameter not in template parameter scope!");
438 1713: bool Invalid = false;
439 :
1620: branch 0 taken
93: branch 1 taken
440 1713: if (ParamName) {
441 1620: NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
19: branch 0 taken
1601: branch 1 taken
1: branch 3 taken
18: branch 4 taken
1: branch 5 taken
1619: branch 6 taken
442 1620: if (PrevDecl && PrevDecl->isTemplateParameter())
443 : Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
1: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
0: branch 4 not taken
444 1: PrevDecl);
445 : }
446 :
447 1713: SourceLocation Loc = ParamNameLoc;
93: branch 0 taken
1620: branch 1 taken
448 1713: if (!ParamName)
449 93: Loc = KeyLoc;
450 :
451 : TemplateTypeParmDecl *Param
452 : = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
453 : Loc, Depth, Position, ParamName, Typename,
1713: branch 1 taken
0: branch 2 not taken
454 1713: Ellipsis);
1: branch 0 taken
1712: branch 1 taken
455 1713: if (Invalid)
456 1: Param->setInvalidDecl();
457 :
1620: branch 0 taken
93: branch 1 taken
458 1713: if (ParamName) {
459 : // Add the template parameter into the current scope.
460 1620: S->AddDecl(DeclPtrTy::make(Param));
461 1620: IdResolver.AddDecl(Param);
462 : }
463 :
464 1713: return DeclPtrTy::make(Param);
465 : }
466 :
467 : /// ActOnTypeParameterDefault - Adds a default argument (the type
468 : /// Default) to the given template type parameter (TypeParam).
469 : void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
470 : SourceLocation EqualLoc,
471 : SourceLocation DefaultLoc,
472 49: TypeTy *DefaultT) {
473 : TemplateTypeParmDecl *Parm
474 49: = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
475 :
476 : TypeSourceInfo *DefaultTInfo;
477 49: GetTypeFromParser(DefaultT, &DefaultTInfo);
478 :
0: branch 0 not taken
49: branch 1 taken
479 49: assert(DefaultTInfo && "expected source information for type");
480 :
481 : // C++0x [temp.param]p9:
482 : // A default template-argument may be specified for any kind of
483 : // template-parameter that is not a template parameter pack.
1: branch 1 taken
48: branch 2 taken
484 49: if (Parm->isParameterPack()) {
485 1: Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
486 1: return;
487 : }
488 :
489 : // C++ [temp.param]p14:
490 : // A template-parameter shall not be used in its own default argument.
491 : // FIXME: Implement this check! Needs a recursive walk over the types.
492 :
493 : // Check the template argument itself.
0: branch 1 not taken
48: branch 2 taken
494 48: if (CheckTemplateArgument(Parm, DefaultTInfo)) {
495 0: Parm->setInvalidDecl();
496 0: return;
497 : }
498 :
499 48: Parm->setDefaultArgument(DefaultTInfo, false);
500 : }
501 :
502 : /// \brief Check that the type of a non-type template parameter is
503 : /// well-formed.
504 : ///
505 : /// \returns the (possibly-promoted) parameter type if valid;
506 : /// otherwise, produces a diagnostic and returns a NULL type.
507 : QualType
508 472: Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
509 : // C++ [temp.param]p4:
510 : //
511 : // A non-type template-parameter shall have one of the following
512 : // (optionally cv-qualified) types:
513 : //
514 : // -- integral or enumeration type,
154: branch 2 taken
318: branch 3 taken
154: branch 6 taken
0: branch 7 not taken
44: branch 10 taken
110: branch 11 taken
19: branch 17 taken
25: branch 18 taken
1: branch 24 taken
18: branch 25 taken
86: branch 28 taken
25: branch 29 taken
67: branch 32 taken
19: branch 33 taken
53: branch 36 taken
14: branch 37 taken
458: branch 38 taken
14: branch 39 taken
515 472: if (T->isIntegralType() || T->isEnumeralType() ||
516 : // -- pointer to object or pointer to function,
517 : (T->isPointerType() &&
518 : (T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
519 : T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
520 : // -- reference to object or reference to function,
521 : T->isReferenceType() ||
522 : // -- pointer to member.
523 : T->isMemberPointerType() ||
524 : // If T is a dependent type, we can't do the check now, so we
525 : // assume that it is well-formed.
526 : T->isDependentType())
527 458: return T;
528 : // C++ [temp.param]p8:
529 : //
530 : // A non-type template-parameter of type "array of T" or
531 : // "function returning T" is adjusted to be of type "pointer to
532 : // T" or "pointer to function returning T", respectively.
1: branch 2 taken
13: branch 3 taken
533 14: else if (T->isArrayType())
534 : // FIXME: Keep the type prior to promotion?
535 1: return Context.getArrayDecayedType(T);
4: branch 2 taken
9: branch 3 taken
536 13: else if (T->isFunctionType())
537 : // FIXME: Keep the type prior to promotion?
538 4: return Context.getPointerType(T);
539 :
540 : Diag(Loc, diag::err_template_nontype_parm_bad_type)
541 9: << T;
542 :
543 9: return QualType();
544 : }
545 :
546 : /// ActOnNonTypeTemplateParameter - Called when a C++ non-type
547 : /// template parameter (e.g., "int Size" in "template<int Size>
548 : /// class Array") has been parsed. S is the current scope and D is
549 : /// the parsed declarator.
550 : Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
551 : unsigned Depth,
552 392: unsigned Position) {
553 392: TypeSourceInfo *TInfo = 0;
554 392: QualType T = GetTypeForDeclarator(D, S, &TInfo);
555 :
556 : assert(S->isTemplateParamScope() &&
392: branch 1 taken
0: branch 2 not taken
557 392: "Non-type template parameter not in template parameter scope!");
558 392: bool Invalid = false;
559 :
560 392: IdentifierInfo *ParamName = D.getIdentifier();
335: branch 0 taken
57: branch 1 taken
561 392: if (ParamName) {
562 335: NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
8: branch 0 taken
327: branch 1 taken
0: branch 3 not taken
8: branch 4 taken
0: branch 5 not taken
335: branch 6 taken
563 335: if (PrevDecl && PrevDecl->isTemplateParameter())
564 : Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
0: branch 0 not taken
0: branch 1 not taken
0: branch 4 not taken
0: branch 5 not taken
565 0: PrevDecl);
566 : }
567 :
568 392: T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
5: branch 1 taken
387: branch 2 taken
569 392: if (T.isNull()) {
570 5: T = Context.IntTy; // Recover with an 'int' type.
571 5: Invalid = true;
572 : }
573 :
574 : NonTypeTemplateParmDecl *Param
575 : = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
576 : D.getIdentifierLoc(),
392: branch 2 taken
0: branch 3 not taken
577 392: Depth, Position, ParamName, T, TInfo);
5: branch 0 taken
387: branch 1 taken
578 392: if (Invalid)
579 5: Param->setInvalidDecl();
580 :
335: branch 1 taken
57: branch 2 taken
581 392: if (D.getIdentifier()) {
582 : // Add the template parameter into the current scope.
583 335: S->AddDecl(DeclPtrTy::make(Param));
584 335: IdResolver.AddDecl(Param);
585 : }
586 392: return DeclPtrTy::make(Param);
587 : }
588 :
589 : /// \brief Adds a default argument to the given non-type template
590 : /// parameter.
591 : void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
592 : SourceLocation EqualLoc,
593 23: ExprArg DefaultE) {
594 : NonTypeTemplateParmDecl *TemplateParm
595 23: = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
596 23: Expr *Default = static_cast<Expr *>(DefaultE.get());
597 :
598 : // C++ [temp.param]p14:
599 : // A template-parameter shall not be used in its own default argument.
600 : // FIXME: Implement this check! Needs a recursive walk over the types.
601 :
602 : // Check the well-formedness of the default template argument.
603 23: TemplateArgument Converted;
0: branch 2 not taken
23: branch 3 taken
604 23: if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
605 : Converted)) {
606 0: TemplateParm->setInvalidDecl();
607 0: return;
608 : }
609 :
23: branch 3 taken
0: branch 4 not taken
610 23: TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
611 : }
612 :
613 :
614 : /// ActOnTemplateTemplateParameter - Called when a C++ template template
615 : /// parameter (e.g. T in template <template <typename> class T> class array)
616 : /// has been parsed. S is the current scope.
617 : Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S,
618 : SourceLocation TmpLoc,
619 : TemplateParamsTy *Params,
620 : IdentifierInfo *Name,
621 : SourceLocation NameLoc,
622 : unsigned Depth,
623 56: unsigned Position) {
624 : assert(S->isTemplateParamScope() &&
56: branch 1 taken
0: branch 2 not taken
625 56: "Template template parameter not in template parameter scope!");
626 :
627 : // Construct the parameter object.
628 : TemplateTemplateParmDecl *Param =
629 : TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
630 : TmpLoc, Depth, Position, Name,
56: branch 1 taken
0: branch 2 not taken
631 112: (TemplateParameterList*)Params);
632 :
633 : // Make sure the parameter is valid.
634 : // FIXME: Decl object is not currently invalidated anywhere so this doesn't
635 : // do anything yet. However, if the template parameter list or (eventual)
636 : // default value is ever invalidated, that will propagate here.
637 56: bool Invalid = false;
0: branch 0 not taken
56: branch 1 taken
638 56: if (Invalid) {
639 0: Param->setInvalidDecl();
640 : }
641 :
642 : // If the tt-param has a name, then link the identifier into the scope
643 : // and lookup mechanisms.
36: branch 0 taken
20: branch 1 taken
644 56: if (Name) {
645 36: S->AddDecl(DeclPtrTy::make(Param));
646 36: IdResolver.AddDecl(Param);
647 : }
648 :
649 56: return DeclPtrTy::make(Param);
650 : }
651 :
652 : /// \brief Adds a default argument to the given template template
653 : /// parameter.
654 : void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
655 : SourceLocation EqualLoc,
656 11: const ParsedTemplateArgument &Default) {
657 : TemplateTemplateParmDecl *TemplateParm
658 11: = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
659 :
660 : // C++ [temp.param]p14:
661 : // A template-parameter shall not be used in its own default argument.
662 : // FIXME: Implement this check! Needs a recursive walk over the types.
663 :
664 : // Check only that we have a template template argument. We don't want to
665 : // try to check well-formedness now, because our template template parameter
666 : // might have dependent types in its template parameters, which we wouldn't
667 : // be able to match now.
668 : //
669 : // If none of the template template parameter's template arguments mention
670 : // other template parameters, we could actually perform more checking here.
671 : // However, it isn't worth doing.
672 11: TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
0: branch 3 not taken
11: branch 4 taken
673 11: if (DefaultArg.getArgument().getAsTemplate().isNull()) {
674 : Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
675 0: << DefaultArg.getSourceRange();
676 0: return;
677 : }
678 :
11: branch 2 taken
0: branch 3 not taken
679 11: TemplateParm->setDefaultArgument(DefaultArg);
680 : }
681 :
682 : /// ActOnTemplateParameterList - Builds a TemplateParameterList that
683 : /// contains the template parameters in Params/NumParams.
684 : Sema::TemplateParamsTy *
685 : Sema::ActOnTemplateParameterList(unsigned Depth,
686 : SourceLocation ExportLoc,
687 : SourceLocation TemplateLoc,
688 : SourceLocation LAngleLoc,
689 : DeclPtrTy *Params, unsigned NumParams,
690 1945: SourceLocation RAngleLoc) {
1: branch 1 taken
1944: branch 2 taken
691 1945: if (ExportLoc.isValid())
692 1: Diag(ExportLoc, diag::warn_template_export_unsupported);
693 :
694 : return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
695 : (NamedDecl**)Params, NumParams,
696 1945: RAngleLoc);
697 : }
698 :
699 : Sema::DeclResult
700 : Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
701 : SourceLocation KWLoc, const CXXScopeSpec &SS,
702 : IdentifierInfo *Name, SourceLocation NameLoc,
703 : AttributeList *Attr,
704 : TemplateParameterList *TemplateParams,
705 971: AccessSpecifier AS) {
706 : assert(TemplateParams && TemplateParams->size() > 0 &&
971: branch 0 taken
0: branch 1 not taken
971: branch 3 taken
0: branch 4 not taken
707 971: "No template parameters");
0: branch 0 not taken
971: branch 1 taken
708 971: assert(TUK != TUK_Reference && "Can only declare or define class templates");
709 971: bool Invalid = false;
710 :
711 : // Check that we can declare a template here.
1: branch 1 taken
970: branch 2 taken
712 971: if (CheckTemplateDeclScope(S, TemplateParams))
713 1: return true;
714 :
715 970: TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec);
0: branch 0 not taken
970: branch 1 taken
716 970: assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type");
717 :
718 : // There is no such thing as an unnamed class template.
0: branch 0 not taken
970: branch 1 taken
719 970: if (!Name) {
720 0: Diag(KWLoc, diag::err_template_unnamed_class);
721 0: return true;
722 : }
723 :
724 : // Find any previous declaration with this name.
725 : DeclContext *SemanticContext;
726 : LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
727 970: ForRedeclaration);
4: branch 1 taken
966: branch 2 taken
4: branch 4 taken
0: branch 5 not taken
4: branch 6 taken
966: branch 7 taken
728 970: if (SS.isNotEmpty() && !SS.isInvalid()) {
0: branch 1 not taken
4: branch 2 taken
729 4: if (RequireCompleteDeclContext(SS))
730 0: return true;
731 :
732 4: SemanticContext = computeDeclContext(SS, true);
0: branch 0 not taken
4: branch 1 taken
733 4: if (!SemanticContext) {
734 : // FIXME: Produce a reasonable diagnostic here
735 0: return true;
736 : }
737 :
738 4: LookupQualifiedName(Previous, SemanticContext);
739 : } else {
740 966: SemanticContext = CurContext;
741 966: LookupName(Previous, S);
742 : }
743 :
970: branch 1 taken
0: branch 2 not taken
744 970: assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
745 970: NamedDecl *PrevDecl = 0;
56: branch 3 taken
914: branch 4 taken
746 970: if (Previous.begin() != Previous.end())
747 56: PrevDecl = *Previous.begin();
748 :
749 : // If there is a previous declaration with the same name, check
750 : // whether this is a valid redeclaration.
751 : ClassTemplateDecl *PrevClassTemplate
752 970: = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
753 :
754 : // We may have found the injected-class-name of a class template,
755 : // class template partial specialization, or class template specialization.
756 : // In these cases, grab the template that is being defined or specialized.
920: branch 0 taken
50: branch 1 taken
6: branch 2 taken
914: branch 3 taken
5: branch 5 taken
1: branch 6 taken
4: branch 9 taken
1: branch 10 taken
4: branch 11 taken
966: branch 12 taken
757 970: if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
758 : cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
759 4: PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
760 : PrevClassTemplate
761 4: = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
2: branch 0 taken
2: branch 1 taken
2: branch 3 taken
0: branch 4 not taken
2: branch 5 taken
2: branch 6 taken
762 4: if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
763 : PrevClassTemplate
764 : = cast<ClassTemplateSpecializationDecl>(PrevDecl)
765 2: ->getSpecializedTemplate();
766 : }
767 : }
768 :
13: branch 0 taken
957: branch 1 taken
769 970: if (TUK == TUK_Friend) {
770 : // C++ [namespace.memdef]p3:
771 : // [...] When looking for a prior declaration of a class or a function
772 : // declared as a friend, and when the name of the friend class or
773 : // function is neither a qualified name nor a template-id, scopes outside
774 : // the innermost enclosing namespace scope are not considered.
775 13: DeclContext *OutermostContext = CurContext;
14: branch 1 taken
13: branch 2 taken
776 40: while (!OutermostContext->isFileContext())
777 14: OutermostContext = OutermostContext->getLookupParent();
778 :
10: branch 0 taken
3: branch 1 taken
2: branch 4 taken
8: branch 5 taken
2: branch 8 taken
0: branch 9 not taken
10: branch 10 taken
3: branch 11 taken
779 13: if (PrevDecl &&
780 : (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
781 : OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
782 10: SemanticContext = PrevDecl->getDeclContext();
783 : } else {
784 : // Declarations in outer scopes don't matter. However, the outermost
785 : // context we computed is the semantic context for our new
786 : // declaration.
787 3: PrevDecl = PrevClassTemplate = 0;
788 3: SemanticContext = OutermostContext;
789 : }
790 :
6: branch 1 taken
7: branch 2 taken
791 13: if (CurContext->isDependentContext()) {
792 : // If this is a dependent context, we don't want to link the friend
793 : // class template to the template in scope, because that would perform
794 : // checking of the template parameter lists that can't be performed
795 : // until the outer context is instantiated.
796 6: PrevDecl = PrevClassTemplate = 0;
797 : }
46: branch 0 taken
911: branch 1 taken
3: branch 3 taken
43: branch 4 taken
3: branch 5 taken
954: branch 6 taken
798 957: } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
799 3: PrevDecl = PrevClassTemplate = 0;
800 :
46: branch 0 taken
924: branch 1 taken
801 970: if (PrevClassTemplate) {
802 : // Ensure that the template parameter lists are compatible.
4: branch 3 taken
42: branch 4 taken
803 46: if (!TemplateParameterListsAreEqual(TemplateParams,
804 : PrevClassTemplate->getTemplateParameters(),
805 : /*Complain=*/true,
806 : TPL_TemplateMatch))
807 4: return true;
808 :
809 : // C++ [temp.class]p4:
810 : // In a redeclaration, partial specialization, explicit
811 : // specialization or explicit instantiation of a class template,
812 : // the class-key shall agree in kind with the original class
813 : // template declaration (7.1.5.3).
814 42: RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
0: branch 1 not taken
42: branch 2 taken
815 42: if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
816 : Diag(KWLoc, diag::err_use_with_wrong_tag)
817 : << Name
818 : << CodeModificationHint::CreateReplacement(KWLoc,
819 0: PrevRecordDecl->getKindName());
820 0: Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
821 0: Kind = PrevRecordDecl->getTagKind();
822 : }
823 :
824 : // Check for redefinition of this class template.
18: branch 0 taken
24: branch 1 taken
825 42: if (TUK == TUK_Definition) {
1: branch 1 taken
17: branch 2 taken
826 18: if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
827 1: Diag(NameLoc, diag::err_redefinition) << Name;
828 1: Diag(Def->getLocation(), diag::note_previous_definition);
829 : // FIXME: Would it make sense to try to "forget" the previous
830 : // definition, as part of error recovery?
831 1: return true;
832 : }
833 : }
1: branch 0 taken
923: branch 1 taken
1: branch 3 taken
0: branch 4 not taken
1: branch 5 taken
923: branch 6 taken
834 924: } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
835 : // Maybe we will complain about the shadowed template parameter.
836 1: DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
837 : // Just pretend that we didn't see the previous declaration.
838 1: PrevDecl = 0;
0: branch 0 not taken
923: branch 1 taken
839 923: } else if (PrevDecl) {
840 : // C++ [temp]p5:
841 : // A class template shall not have the same name as any other
842 : // template, class, function, object, enumeration, enumerator,
843 : // namespace, or type in the same scope (3.3), except as specified
844 : // in (14.5.4).
845 0: Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
846 0: Diag(PrevDecl->getLocation(), diag::note_previous_definition);
847 0: return true;
848 : }
849 :
850 : // Check the template parameter list of this declaration, possibly
851 : // merging in the template parameter list from the previous class
852 : // template declaration.
41: branch 0 taken
924: branch 1 taken
7: branch 4 taken
958: branch 5 taken
853 965: if (CheckTemplateParameterList(TemplateParams,
854 : PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
855 : TPC_ClassTemplate))
856 7: Invalid = true;
857 :
858 : // FIXME: If we had a scope specifier, we better have a previous template
859 : // declaration!
860 :
861 : CXXRecordDecl *NewClass =
862 : CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
863 : PrevClassTemplate?
864 : PrevClassTemplate->getTemplatedDecl() : 0,
41: branch 0 taken
924: branch 1 taken
865 965: /*DelayTypeCreation=*/true);
866 :
867 : ClassTemplateDecl *NewTemplate
868 : = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
869 : DeclarationName(Name), TemplateParams,
870 965: NewClass, PrevClassTemplate);
871 965: NewClass->setDescribedClassTemplate(NewTemplate);
872 :
873 : // Build the type for the class template declaration now.
874 : QualType T =
875 : Context.getTypeDeclType(NewClass,
876 : PrevClassTemplate?
41: branch 0 taken
924: branch 1 taken
877 965: PrevClassTemplate->getTemplatedDecl() : 0);
965: branch 2 taken
0: branch 3 not taken
878 965: assert(T->isDependentType() && "Class template type is not dependent?");
879 : (void)T;
880 :
881 : // If we are providing an explicit specialization of a member that is a
882 : // class template, make a note of that.
41: branch 0 taken
924: branch 1 taken
2: branch 3 taken
39: branch 4 taken
2: branch 5 taken
963: branch 6 taken
883 1006: if (PrevClassTemplate &&
884 : PrevClassTemplate->getInstantiatedFromMemberTemplate())
885 2: PrevClassTemplate->setMemberSpecialization();
886 :
887 : // Set the access specifier.
958: branch 0 taken
7: branch 1 taken
945: branch 2 taken
13: branch 3 taken
888 965: if (!Invalid && TUK != TUK_Friend)
889 945: SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
890 :
891 : // Set the lexical context of these templates
892 965: NewClass->setLexicalDeclContext(CurContext);
893 965: NewTemplate->setLexicalDeclContext(CurContext);
894 :
765: branch 0 taken
200: branch 1 taken
895 965: if (TUK == TUK_Definition)
896 765: NewClass->startDefinition();
897 :
0: branch 0 not taken
965: branch 1 taken
898 965: if (Attr)
899 0: ProcessDeclAttributeList(S, NewClass, Attr);
900 :
952: branch 0 taken
13: branch 1 taken
901 965: if (TUK != TUK_Friend)
902 952: PushOnScopeChains(NewTemplate, S);
903 : else {
4: branch 0 taken
9: branch 1 taken
1: branch 3 taken
3: branch 4 taken
1: branch 5 taken
12: branch 6 taken
904 13: if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
905 1: NewTemplate->setAccess(PrevClassTemplate->getAccess());
906 1: NewClass->setAccess(PrevClassTemplate->getAccess());
907 : }
908 :
909 : NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
910 13: PrevClassTemplate != NULL);
911 :
912 : // Friend templates are visible in fairly strange ways.
7: branch 1 taken
6: branch 2 taken
913 13: if (!CurContext->isDependentContext()) {
914 7: DeclContext *DC = SemanticContext->getLookupContext();
915 7: DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
7: branch 1 taken
0: branch 2 not taken
916 7: if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
917 : PushOnScopeChains(NewTemplate, EnclosingScope,
918 7: /* AddToContext = */ false);
919 : }
920 :
921 : FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
922 : NewClass->getLocation(),
923 : NewTemplate,
924 13: /*FIXME:*/NewClass->getLocation());
925 13: Friend->setAccess(AS_public);
926 13: CurContext->addDecl(Friend);
927 : }
928 :
7: branch 0 taken
958: branch 1 taken
929 965: if (Invalid) {
930 7: NewTemplate->setInvalidDecl();
931 7: NewClass->setInvalidDecl();
932 : }
933 965: return DeclPtrTy::make(NewTemplate);
934 : }
935 :
936 : /// \brief Diagnose the presence of a default template argument on a
937 : /// template parameter, which is ill-formed in certain contexts.
938 : ///
939 : /// \returns true if the default template argument should be dropped.
940 : static bool DiagnoseDefaultTemplateArgument(Sema &S,
941 : Sema::TemplateParamListContext TPC,
942 : SourceLocation ParamLoc,
943 79: SourceRange DefArgRange) {
73: branch 0 taken
4: branch 1 taken
1: branch 2 taken
1: branch 3 taken
0: branch 4 not taken
944 79: switch (TPC) {
945 : case Sema::TPC_ClassTemplate:
946 73: return false;
947 :
948 : case Sema::TPC_FunctionTemplate:
949 : // C++ [temp.param]p9:
950 : // A default template-argument shall not be specified in a
951 : // function template declaration or a function template
952 : // definition [...]
953 : // (This sentence is not in C++0x, per DR226).
2: branch 1 taken
2: branch 2 taken
954 4: if (!S.getLangOptions().CPlusPlus0x)
955 : S.Diag(ParamLoc,
956 : diag::err_template_parameter_default_in_function_template)
957 2: << DefArgRange;
958 4: return false;
959 :
960 : case Sema::TPC_ClassTemplateMember:
961 : // C++0x [temp.param]p9:
962 : // A default template-argument shall not be specified in the
963 : // template-parameter-lists of the definition of a member of a
964 : // class template that appears outside of the member's class.
965 : S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
966 1: << DefArgRange;
967 1: return true;
968 :
969 : case Sema::TPC_FriendFunctionTemplate:
970 : // C++ [temp.param]p9:
971 : // A default template-argument shall not be specified in a
972 : // friend template declaration.
973 : S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
974 1: << DefArgRange;
975 1: return true;
976 :
977 : // FIXME: C++0x [temp.param]p9 allows default template-arguments
978 : // for friend function templates if there is only a single
979 : // declaration (and it is a definition). Strange!
980 : }
981 :
982 0: return false;
983 : }
984 :
985 : /// \brief Checks the validity of a template parameter list, possibly
986 : /// considering the template parameter list from a previous
987 : /// declaration.
988 : ///
989 : /// If an "old" template parameter list is provided, it must be
990 : /// equivalent (per TemplateParameterListsAreEqual) to the "new"
991 : /// template parameter list.
992 : ///
993 : /// \param NewParams Template parameter list for a new template
994 : /// declaration. This template parameter list will be updated with any
995 : /// default arguments that are carried through from the previous
996 : /// template parameter list.
997 : ///
998 : /// \param OldParams If provided, template parameter list from a
999 : /// previous declaration of the same template. Default template
1000 : /// arguments will be merged from the old template parameter list to
1001 : /// the new template parameter list.
1002 : ///
1003 : /// \param TPC Describes the context in which we are checking the given
1004 : /// template parameter list.
1005 : ///
1006 : /// \returns true if an error occurred, false otherwise.
1007 : bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
1008 : TemplateParameterList *OldParams,
1009 1541: TemplateParamListContext TPC) {
1010 1541: bool Invalid = false;
1011 :
1012 : // C++ [temp.param]p10:
1013 : // The set of default template-arguments available for use with a
1014 : // template declaration or definition is obtained by merging the
1015 : // default arguments from the definition (if in scope) and all
1016 : // declarations in scope in the same way default function
1017 : // arguments are (8.3.6).
1018 1541: bool SawDefaultArgument = false;
1019 1541: SourceLocation PreviousDefaultArgLoc;
1020 :
1021 1541: bool SawParameterPack = false;
1022 1541: SourceLocation ParameterPackLoc;
1023 :
1024 : // Dummy initialization to avoid warnings.
1025 1541: TemplateParameterList::iterator OldParam = NewParams->end();
74: branch 0 taken
1467: branch 1 taken
1026 1541: if (OldParams)
1027 74: OldParam = OldParams->begin();
1028 :
1926: branch 1 taken
1541: branch 2 taken
1029 5008: for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1030 1541: NewParamEnd = NewParams->end();
1031 : NewParam != NewParamEnd; ++NewParam) {
1032 : // Variables used to diagnose redundant default arguments
1033 1926: bool RedundantDefaultArg = false;
1034 1926: SourceLocation OldDefaultLoc;
1035 1926: SourceLocation NewDefaultLoc;
1036 :
1037 : // Variables used to diagnose missing default arguments
1038 1926: bool MissingDefaultArg = false;
1039 :
1040 : // C++0x [temp.param]p11:
1041 : // If a template parameter of a class template is a template parameter pack,
1042 : // it must be the last template parameter.
1: branch 0 taken
1925: branch 1 taken
1043 1926: if (SawParameterPack) {
1044 : Diag(ParameterPackLoc,
1045 1: diag::err_template_param_pack_must_be_last_template_parameter);
1046 1: Invalid = true;
1047 : }
1048 :
1527: branch 0 taken
399: branch 1 taken
1049 1926: if (TemplateTypeParmDecl *NewTypeParm
1050 1926: = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
1051 : // Check the presence of a default argument here.
47: branch 1 taken
1480: branch 2 taken
0: branch 8 not taken
47: branch 9 taken
0: branch 10 not taken
1527: branch 11 taken
1052 1527: if (NewTypeParm->hasDefaultArgument() &&
1053 : DiagnoseDefaultTemplateArgument(*this, TPC,
1054 : NewTypeParm->getLocation(),
1055 : NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
1056 : .getFullSourceRange()))
1057 0: NewTypeParm->removeDefaultArgument();
1058 :
1059 : // Merge default arguments for template type parameters.
1060 : TemplateTypeParmDecl *OldTypeParm
65: branch 0 taken
1462: branch 1 taken
1061 1527: = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
1062 :
8: branch 1 taken
1519: branch 2 taken
1063 1527: if (NewTypeParm->isParameterPack()) {
1064 : assert(!NewTypeParm->hasDefaultArgument() &&
8: branch 1 taken
0: branch 2 not taken
1065 8: "Parameter packs can't have a default argument!");
1066 8: SawParameterPack = true;
1067 8: ParameterPackLoc = NewTypeParm->getLocation();
65: branch 0 taken
1454: branch 1 taken
5: branch 3 taken
60: branch 4 taken
1: branch 6 taken
4: branch 7 taken
1: branch 8 taken
1518: branch 9 taken
1068 1519: } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
1069 : NewTypeParm->hasDefaultArgument()) {
1070 1: OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1071 1: NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1072 1: SawDefaultArgument = true;
1073 1: RedundantDefaultArg = true;
1074 1: PreviousDefaultArgLoc = NewDefaultLoc;
64: branch 0 taken
1454: branch 1 taken
4: branch 3 taken
60: branch 4 taken
4: branch 5 taken
1514: branch 6 taken
1075 1518: } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1076 : // Merge the default argument from the old declaration to the
1077 : // new declaration.
1078 4: SawDefaultArgument = true;
1079 : NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
1080 4: true);
1081 4: PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
46: branch 1 taken
1468: branch 2 taken
1082 1514: } else if (NewTypeParm->hasDefaultArgument()) {
1083 46: SawDefaultArgument = true;
1084 46: PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1: branch 0 taken
1467: branch 1 taken
1085 1468: } else if (SawDefaultArgument)
1086 1: MissingDefaultArg = true;
350: branch 0 taken
49: branch 1 taken
1087 399: } else if (NonTypeTemplateParmDecl *NewNonTypeParm
1088 399: = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
1089 : // Check the presence of a default argument here.
22: branch 1 taken
328: branch 2 taken
1: branch 7 taken
21: branch 8 taken
1: branch 9 taken
349: branch 10 taken
1090 350: if (NewNonTypeParm->hasDefaultArgument() &&
1091 : DiagnoseDefaultTemplateArgument(*this, TPC,
1092 : NewNonTypeParm->getLocation(),
1093 : NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
1094 1: NewNonTypeParm->getDefaultArgument()->Destroy(Context);
1095 1: NewNonTypeParm->setDefaultArgument(0);
1096 : }
1097 :
1098 : // Merge default arguments for non-type template parameters
1099 : NonTypeTemplateParmDecl *OldNonTypeParm
24: branch 0 taken
326: branch 1 taken
1100 350: = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
24: branch 0 taken
326: branch 1 taken
4: branch 3 taken
20: branch 4 taken
1: branch 6 taken
3: branch 7 taken
1: branch 8 taken
349: branch 9 taken
1101 350: if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
1102 : NewNonTypeParm->hasDefaultArgument()) {
1103 1: OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1104 1: NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1105 1: SawDefaultArgument = true;
1106 1: RedundantDefaultArg = true;
1107 1: PreviousDefaultArgLoc = NewDefaultLoc;
23: branch 0 taken
326: branch 1 taken
3: branch 3 taken
20: branch 4 taken
3: branch 5 taken
346: branch 6 taken
1108 349: } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1109 : // Merge the default argument from the old declaration to the
1110 : // new declaration.
1111 3: SawDefaultArgument = true;
1112 : // FIXME: We need to create a new kind of "default argument"
1113 : // expression that points to a previous template template
1114 : // parameter.
1115 : NewNonTypeParm->setDefaultArgument(
1116 3: OldNonTypeParm->getDefaultArgument());
1117 3: PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
20: branch 1 taken
326: branch 2 taken
1118 346: } else if (NewNonTypeParm->hasDefaultArgument()) {
1119 20: SawDefaultArgument = true;
1120 20: PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1: branch 0 taken
325: branch 1 taken
1121 326: } else if (SawDefaultArgument)
1122 1: MissingDefaultArg = true;
1123 : } else {
1124 : // Check the presence of a default argument here.
1125 : TemplateTemplateParmDecl *NewTemplateParm
1126 49: = cast<TemplateTemplateParmDecl>(*NewParam);
10: branch 1 taken
39: branch 2 taken
1: branch 7 taken
9: branch 8 taken
1: branch 9 taken
48: branch 10 taken
1127 49: if (NewTemplateParm->hasDefaultArgument() &&
1128 : DiagnoseDefaultTemplateArgument(*this, TPC,
1129 : NewTemplateParm->getLocation(),
1130 : NewTemplateParm->getDefaultArgument().getSourceRange()))
1131 1: NewTemplateParm->setDefaultArgument(TemplateArgumentLoc());
1132 :
1133 : // Merge default arguments for template template parameters
1134 : TemplateTemplateParmDecl *OldTemplateParm
10: branch 0 taken
39: branch 1 taken
1135 49: = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
10: branch 0 taken
39: branch 1 taken
3: branch 3 taken
7: branch 4 taken
1: branch 6 taken
2: branch 7 taken
1: branch 8 taken
48: branch 9 taken
1136 49: if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
1137 : NewTemplateParm->hasDefaultArgument()) {
1138 1: OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1139 1: NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
1140 1: SawDefaultArgument = true;
1141 1: RedundantDefaultArg = true;
1142 1: PreviousDefaultArgLoc = NewDefaultLoc;
9: branch 0 taken
39: branch 1 taken
2: branch 3 taken
7: branch 4 taken
2: branch 5 taken
46: branch 6 taken
1143 48: } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1144 : // Merge the default argument from the old declaration to the
1145 : // new declaration.
1146 2: SawDefaultArgument = true;
1147 : // FIXME: We need to create a new kind of "default argument" expression
1148 : // that points to a previous template template parameter.
1149 : NewTemplateParm->setDefaultArgument(
1150 2: OldTemplateParm->getDefaultArgument());
1151 : PreviousDefaultArgLoc
1152 2: = OldTemplateParm->getDefaultArgument().getLocation();
8: branch 1 taken
38: branch 2 taken
1153 46: } else if (NewTemplateParm->hasDefaultArgument()) {
1154 8: SawDefaultArgument = true;
1155 : PreviousDefaultArgLoc
1156 8: = NewTemplateParm->getDefaultArgument().getLocation();
1: branch 0 taken
37: branch 1 taken
1157 38: } else if (SawDefaultArgument)
1158 1: MissingDefaultArg = true;
1159 : }
1160 :
3: branch 0 taken
1923: branch 1 taken
1161 1926: if (RedundantDefaultArg) {
1162 : // C++ [temp.param]p12:
1163 : // A template-parameter shall not be given default arguments
1164 : // by two different declarations in the same scope.
1165 3: Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1166 3: Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1167 3: Invalid = true;
3: branch 0 taken
1920: branch 1 taken
1168 1923: } else if (MissingDefaultArg) {
1169 : // C++ [temp.param]p11:
1170 : // If a template-parameter has a default template-argument,
1171 : // all subsequent template-parameters shall have a default
1172 : // template-argument supplied.
1173 : Diag((*NewParam)->getLocation(),
1174 3: diag::err_template_param_default_arg_missing);
1175 3: Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1176 3: Invalid = true;
1177 : }
1178 :
1179 : // If we have an old template parameter list that we're merging
1180 : // in, move on to the next parameter.
99: branch 0 taken
1827: branch 1 taken
1181 1926: if (OldParams)
1182 99: ++OldParam;
1183 : }
1184 :
1185 1541: return Invalid;
1186 : }
1187 :
1188 : /// \brief Match the given template parameter lists to the given scope
1189 : /// specifier, returning the template parameter list that applies to the
1190 : /// name.
1191 : ///
1192 : /// \param DeclStartLoc the start of the declaration that has a scope
1193 : /// specifier or a template parameter list.
1194 : ///
1195 : /// \param SS the scope specifier that will be matched to the given template
1196 : /// parameter lists. This scope specifier precedes a qualified name that is
1197 : /// being declared.
1198 : ///
1199 : /// \param ParamLists the template parameter lists, from the outermost to the
1200 : /// innermost template parameter lists.
1201 : ///
1202 : /// \param NumParamLists the number of template parameter lists in ParamLists.
1203 : ///
1204 : /// \param IsExplicitSpecialization will be set true if the entity being
1205 : /// declared is an explicit specialization, false otherwise.
1206 : ///
1207 : /// \returns the template parameter list, if any, that corresponds to the
1208 : /// name that is preceded by the scope specifier @p SS. This template
1209 : /// parameter list may be have template parameters (if we're declaring a
1210 : /// template) or may have no template parameters (if we're declaring a
1211 : /// template specialization), or may be NULL (if we were's declaring isn't
1212 : /// itself a template).
1213 : TemplateParameterList *
1214 : Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1215 : const CXXScopeSpec &SS,
1216 : TemplateParameterList **ParamLists,
1217 : unsigned NumParamLists,
1218 29306: bool &IsExplicitSpecialization) {
1219 29306: IsExplicitSpecialization = false;
1220 :
1221 : // Find the template-ids that occur within the nested-name-specifier. These
1222 : // template-ids will match up with the template parameter lists.
1223 : llvm::SmallVector<const TemplateSpecializationType *, 4>
1224 29306: TemplateIdsInSpecifier;
1225 : llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1226 29306: ExplicitSpecializationsInSpecifier;
516: branch 2 taken
29236: branch 3 taken
1227 29752: for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1228 : NNS; NNS = NNS->getPrefix()) {
1229 516: const Type *T = NNS->getAsType();
446: branch 0 taken
70: branch 1 taken
1230 516: if (!T) break;
1231 :
1232 : // C++0x [temp.expl.spec]p17:
1233 : // A member or a member template may be nested within many
1234 : // enclosing class templates. In an explicit specialization for
1235 : // such a member, the member declaration shall be preceded by a
1236 : // template<> for each enclosing class template that is
1237 : // explicitly specialized.
1238 : // We interpret this as forbidding typedefs of template
1239 : // specializations in the scope specifiers of out-of-line decls.
2: branch 1 taken
444: branch 2 taken
1240 446: if (const TypedefType *TT = dyn_cast<TypedefType>(T)) {
1241 2: const Type *UnderlyingT = TT->LookThroughTypedefs().getTypePtr();
2: branch 1 taken
0: branch 2 not taken
1242 2: if (isa<TemplateSpecializationType>(UnderlyingT))
1243 : // FIXME: better source location information.
1244 2: Diag(DeclStartLoc, diag::err_typedef_in_def_scope) << QualType(T,0);
1245 2: T = UnderlyingT;
1246 : }
1247 :
225: branch 0 taken
221: branch 1 taken
1248 446: if (const TemplateSpecializationType *SpecType
1249 446: = dyn_cast<TemplateSpecializationType>(T)) {
1250 225: TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
0: branch 0 not taken
225: branch 1 taken
1251 225: if (!Template)
1252 0: continue; // FIXME: should this be an error? probably...
1253 :
98: branch 1 taken
127: branch 2 taken
1254 225: if (const RecordType *Record = SpecType->getAs<RecordType>()) {
1255 : ClassTemplateSpecializationDecl *SpecDecl
1256 98: = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1257 : // If the nested name specifier refers to an explicit specialization,
1258 : // we don't need a template<> header.
9: branch 1 taken
89: branch 2 taken
1259 98: if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1260 9: ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
1261 9: continue;
1262 : }
1263 : }
1264 :
1265 216: TemplateIdsInSpecifier.push_back(SpecType);
1266 : }
1267 : }
1268 :
1269 : // Reverse the list of template-ids in the scope specifier, so that we can
1270 : // more easily match up the template-ids and the template parameter lists.
1271 29306: std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
1272 :
1273 29306: SourceLocation FirstTemplateLoc = DeclStartLoc;
1823: branch 0 taken
27483: branch 1 taken
1274 29306: if (NumParamLists)
1275 1823: FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
1276 :
1277 : // Match the template-ids found in the specifier to the template parameter
1278 : // lists.
1279 29306: unsigned Idx = 0;
216: branch 1 taken
29303: branch 2 taken
1280 29519: for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
1281 : Idx != NumTemplateIds; ++Idx) {
1282 216: QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
1283 216: bool DependentTemplateId = TemplateId->isDependentType();
3: branch 0 taken
213: branch 1 taken
1284 216: if (Idx >= NumParamLists) {
1285 : // We have a template-id without a corresponding template parameter
1286 : // list.
0: branch 0 not taken
3: branch 1 taken
1287 3: if (DependentTemplateId) {
1288 : // FIXME: the location information here isn't great.
1289 : Diag(SS.getRange().getBegin(),
1290 : diag::err_template_spec_needs_template_parameters)
1291 : << TemplateId
1292 0: << SS.getRange();
1293 : } else {
1294 : Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1295 : << SS.getRange()
1296 : << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
1297 3: "template<> ");
1298 3: IsExplicitSpecialization = true;
1299 : }
1300 3: return 0;
1301 : }
1302 :
1303 : // Check the template parameter list against its corresponding template-id.
127: branch 0 taken
86: branch 1 taken
1304 213: if (DependentTemplateId) {
1305 : TemplateDecl *Template
1306 127: = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
1307 :
127: branch 0 taken
0: branch 1 not taken
1308 127: if (ClassTemplateDecl *ClassTemplate
1309 127: = dyn_cast<ClassTemplateDecl>(Template)) {
1310 127: TemplateParameterList *ExpectedTemplateParams = 0;
1311 : // Is this template-id naming the primary template?
121: branch 2 taken
6: branch 3 taken
1312 127: if (Context.hasSameType(TemplateId,
1313 : ClassTemplate->getInjectedClassNameType(Context)))
1314 121: ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
1315 : // ... or a partial specialization?
6: branch 0 taken
0: branch 1 not taken
1316 6: else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1317 6: = ClassTemplate->findPartialSpecialization(TemplateId))
1318 6: ExpectedTemplateParams = PartialSpec->getTemplateParameters();
1319 :
127: branch 0 taken
0: branch 1 not taken
1320 127: if (ExpectedTemplateParams)
1321 : TemplateParameterListsAreEqual(ParamLists[Idx],
1322 : ExpectedTemplateParams,
1323 127: true, TPL_TemplateMatch);
1324 : }
1325 :
1326 127: CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember);
0: branch 1 not taken
86: branch 2 taken
1327 86: } else if (ParamLists[Idx]->size() > 0)
1328 : Diag(ParamLists[Idx]->getTemplateLoc(),
1329 : diag::err_template_param_list_matches_nontemplate)
1330 : << TemplateId
1331 0: << ParamLists[Idx]->getSourceRange();
1332 : else
1333 86: IsExplicitSpecialization = true;
1334 : }
1335 :
1336 : // If there were at least as many template-ids as there were template
1337 : // parameter lists, then there are no template parameter lists remaining for
1338 : // the declaration itself.
27638: branch 0 taken
1665: branch 1 taken
1339 29303: if (Idx >= NumParamLists)
1340 27638: return 0;
1341 :
1342 : // If there were too many template parameter lists, complain about that now.
1: branch 0 taken
1664: branch 1 taken
1343 1665: if (Idx != NumParamLists - 1) {
1: branch 0 taken
1: branch 1 taken
1344 3: while (Idx < NumParamLists - 1) {
1345 1: bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0;
1346 : Diag(ParamLists[Idx]->getTemplateLoc(),
1347 : isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1348 : : diag::err_template_spec_extra_headers)
1349 : << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1: branch 3 taken
0: branch 4 not taken
1350 1: ParamLists[Idx]->getRAngleLoc());
1351 :
1: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
0: branch 4 not taken
1: branch 5 taken
0: branch 6 not taken
1352 1: if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1353 : Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1354 : diag::note_explicit_template_spec_does_not_need_header)
1355 1: << ExplicitSpecializationsInSpecifier.back();
1356 1: ExplicitSpecializationsInSpecifier.pop_back();
1357 : }
1358 :
1359 1: ++Idx;
1360 : }
1361 : }
1362 :
1363 : // Return the last template parameter list, which corresponds to the
1364 : // entity being declared.
1365 1665: return ParamLists[NumParamLists - 1];
1366 : }
1367 :
1368 : QualType Sema::CheckTemplateIdType(TemplateName Name,
1369 : SourceLocation TemplateLoc,
1370 3188: const TemplateArgumentListInfo &TemplateArgs) {
1371 3188: TemplateDecl *Template = Name.getAsTemplateDecl();
34: branch 0 taken
3154: branch 1 taken
1372 3188: if (!Template) {
1373 : // The template name does not resolve to a template, so we just
1374 : // build a dependent template-id type.
1375 34: return Context.getTemplateSpecializationType(Name, TemplateArgs);
1376 : }
1377 :
1378 : // Check that the template argument list is well-formed for this
1379 : // template.
1380 : TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
1381 3154: TemplateArgs.size());
55: branch 1 taken
3099: branch 2 taken
1382 3154: if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
1383 : false, Converted))
1384 55: return QualType();
1385 :
1386 : assert((Converted.structuredSize() ==
1387 : Template->getTemplateParameters()->size()) &&
3099: branch 3 taken
0: branch 4 not taken
1388 3099: "Converted template argument list is too short!");
1389 :
1390 3099: QualType CanonType;
1391 :
3064: branch 1 taken
35: branch 2 taken
520: branch 4 taken
2544: branch 5 taken
555: branch 6 taken
2544: branch 7 taken
1392 6163: if (Name.isDependent() ||
1393 : TemplateSpecializationType::anyDependentTemplateArguments(
1394 : TemplateArgs)) {
1395 : // This class template specialization is a dependent
1396 : // type. Therefore, its canonical type is another class template
1397 : // specialization type that contains all of the converted
1398 : // arguments in canonical form. This ensures that, e.g., A<T> and
1399 : // A<T, T> have identical types when A is declared as:
1400 : //
1401 : // template<typename T, typename U = T> struct A;
1402 555: TemplateName CanonName = Context.getCanonicalTemplateName(Name);
1403 : CanonType = Context.getTemplateSpecializationType(CanonName,
1404 : Converted.getFlatArguments(),
1405 555: Converted.flatSize());
1406 :
1407 : // FIXME: CanonType is not actually the canonical type, and unfortunately
1408 : // it is a TemplateSpecializationType that we will never use again.
1409 : // In the future, we need to teach getTemplateSpecializationType to only
1410 : // build the canonical type and return that to us.
1411 555: CanonType = Context.getCanonicalType(CanonType);
2544: branch 0 taken
0: branch 1 not taken
1412 2544: } else if (ClassTemplateDecl *ClassTemplate
1413 2544: = dyn_cast<ClassTemplateDecl>(Template)) {
1414 : // Find the class template specialization declaration that
1415 : // corresponds to these arguments.
1416 2544: llvm::FoldingSetNodeID ID;
1417 : ClassTemplateSpecializationDecl::Profile(ID,
1418 : Converted.getFlatArguments(),
1419 : Converted.flatSize(),
1420 2544: Context);
1421 2544: void *InsertPos = 0;
1422 : ClassTemplateSpecializationDecl *Decl
1423 2544: = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1280: branch 0 taken
1264: branch 1 taken
1424 2544: if (!Decl) {
1425 : // This is the first time we have referenced this class template
1426 : // specialization. Create the canonical declaration and add it to
1427 : // the set of specializations.
1428 : Decl = ClassTemplateSpecializationDecl::Create(Context,
1429 : ClassTemplate->getDeclContext(),
1430 : ClassTemplate->getLocation(),
1431 : ClassTemplate,
1432 1280: Converted, 0);
1280: branch 0 taken
0: branch 1 not taken
1433 1280: ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1434 1280: Decl->setLexicalDeclContext(CurContext);
1435 : }
1436 :
1437 2544: CanonType = Context.getTypeDeclType(Decl);
1438 : }
1439 :
1440 : // Build the fully-sugared type for this class template
1441 : // specialization, which refers back to the class template
1442 : // specialization we created or found.
1443 3099: return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
1444 : }
1445 :
1446 : Action::TypeResult
1447 : Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
1448 : SourceLocation LAngleLoc,
1449 : ASTTemplateArgsPtr TemplateArgsIn,
1450 1869: SourceLocation RAngleLoc) {
1451 1869: TemplateName Template = TemplateD.getAsVal<TemplateName>();
1452 :
1453 : // Translate the parser's template argument list in our AST format.
1454 1869: TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1455 1869: translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1456 :
1457 1869: QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1458 1869: TemplateArgsIn.release();
1459 :
54: branch 1 taken
1815: branch 2 taken
1460 1869: if (Result.isNull())
1461 54: return true;
1462 :
1463 1815: TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
1464 : TemplateSpecializationTypeLoc TL
1465 1815: = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1466 1815: TL.setTemplateNameLoc(TemplateLoc);
1467 1815: TL.setLAngleLoc(LAngleLoc);
1468 1815: TL.setRAngleLoc(RAngleLoc);
2199: branch 1 taken
1815: branch 2 taken
1469 4014: for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1470 2199: TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1471 :
1472 1815: return CreateLocInfoType(Result, DI).getAsOpaquePtr();
1473 : }
1474 :
1475 : Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1476 : TagUseKind TUK,
1477 : DeclSpec::TST TagSpec,
1478 8: SourceLocation TagLoc) {
0: branch 1 not taken
8: branch 2 taken
1479 8: if (TypeResult.isInvalid())
1480 0: return Sema::TypeResult();
1481 :
1482 : // FIXME: preserve source info, ideally without copying the DI.
1483 : TypeSourceInfo *DI;
1484 8: QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
1485 :
1486 : // Verify the tag specifier.
1487 8: TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
1488 :
7: branch 2 taken
1: branch 3 taken
1489 8: if (const RecordType *RT = Type->getAs<RecordType>()) {
1490 7: RecordDecl *D = RT->getDecl();
1491 :
1492 7: IdentifierInfo *Id = D->getIdentifier();
0: branch 0 not taken
7: branch 1 taken
1493 7: assert(Id && "templated class must have an identifier");
1494 :
2: branch 1 taken
5: branch 2 taken
1495 7: if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1496 : Diag(TagLoc, diag::err_use_with_wrong_tag)
1497 : << Type
1498 : << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1499 2: D->getKindName());
1500 2: Diag(D->getLocation(), diag::note_previous_use);
1501 : }
1502 : }
1503 :
1504 8: QualType ElabType = Context.getElaboratedType(Type, TagKind);
1505 :
1506 8: return ElabType.getAsOpaquePtr();
1507 : }
1508 :
1509 : Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
1510 : LookupResult &R,
1511 : bool RequiresADL,
1512 128: const TemplateArgumentListInfo &TemplateArgs) {
1513 : // FIXME: Can we do any checking at this point? I guess we could check the
1514 : // template arguments that we have against the template name, if the template
1515 : // name refers to a single template. That's not a terribly common case,
1516 : // though.
1517 :
1518 : // These should be filtered out by our callers.
128: branch 1 taken
0: branch 2 not taken
1519 128: assert(!R.empty() && "empty lookup results when building templateid");
128: branch 1 taken
0: branch 2 not taken
1520 128: assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1521 :
1522 128: NestedNameSpecifier *Qualifier = 0;
1523 128: SourceRange QualifierRange;
11: branch 1 taken
117: branch 2 taken
1524 128: if (SS.isSet()) {
1525 11: Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1526 11: QualifierRange = SS.getRange();
1527 : }
1528 :
1529 : // We don't want lookup warnings at this point.
1530 128: R.suppressDiagnostics();
1531 :
1532 : bool Dependent
1533 : = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(),
1534 128: &TemplateArgs);
1535 : UnresolvedLookupExpr *ULE
1536 : = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(),
1537 : Qualifier, QualifierRange,
1538 : R.getLookupName(), R.getNameLoc(),
1539 128: RequiresADL, TemplateArgs);
1540 128: ULE->addDecls(R.begin(), R.end());
1541 :
1542 128: return Owned(ULE);
1543 : }
1544 :
1545 : // We actually only call this from template instantiation.
1546 : Sema::OwningExprResult
1547 : Sema::BuildQualifiedTemplateIdExpr(const CXXScopeSpec &SS,
1548 : DeclarationName Name,
1549 : SourceLocation NameLoc,
1550 1: const TemplateArgumentListInfo &TemplateArgs) {
1551 : DeclContext *DC;
1: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
0: branch 5 not taken
0: branch 7 not taken
1: branch 8 taken
0: branch 9 not taken
1: branch 10 taken
1552 1: if (!(DC = computeDeclContext(SS, false)) ||
1553 : DC->isDependentContext() ||
1554 : RequireCompleteDeclContext(SS))
1555 0: return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs);
1556 :
1557 1: LookupResult R(*this, Name, NameLoc, LookupOrdinaryName);
1558 1: LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false);
1559 :
0: branch 1 not taken
1: branch 2 taken
1560 1: if (R.isAmbiguous())
1561 0: return ExprError();
1562 :
0: branch 1 not taken
1: branch 2 taken
1563 1: if (R.empty()) {
1564 : Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1565 0: << Name << SS.getRange();
1566 0: return ExprError();
1567 : }
1568 :
0: branch 1 not taken
1: branch 2 taken
1569 1: if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
1570 : Diag(NameLoc, diag::err_template_kw_refers_to_class_template)
1571 0: << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange();
1572 0: Diag(Temp->getLocation(), diag::note_referenced_class_template);
1573 0: return ExprError();
1574 : }
1575 :
1576 1: return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
1577 : }
1578 :
1579 : /// \brief Form a dependent template name.
1580 : ///
1581 : /// This action forms a dependent template name given the template
1582 : /// name and its (presumably dependent) scope specifier. For
1583 : /// example, given "MetaFun::template apply", the scope specifier \p
1584 : /// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1585 : /// of the "template" keyword, and "apply" is the \p Name.
1586 : Sema::TemplateTy
1587 : Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
1588 : const CXXScopeSpec &SS,
1589 : UnqualifiedId &Name,
1590 : TypeTy *ObjectType,
1591 65: bool EnteringContext) {
1592 65: DeclContext *LookupCtx = 0;
59: branch 1 taken
6: branch 2 taken
1593 65: if (SS.isSet())
1594 59: LookupCtx = computeDeclContext(SS, EnteringContext);
41: branch 0 taken
24: branch 1 taken
6: branch 2 taken
35: branch 3 taken
1595 65: if (!LookupCtx && ObjectType)
1596 6: LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType));
27: branch 0 taken
38: branch 1 taken
1597 65: if (LookupCtx) {
1598 : // C++0x [temp.names]p5:
1599 : // If a name prefixed by the keyword template is not the name of
1600 : // a template, the program is ill-formed. [Note: the keyword
1601 : // template may not be applied to non-template members of class
1602 : // templates. -end note ] [ Note: as is the case with the
1603 : // typename prefix, the template prefix is allowed in cases
1604 : // where it is not strictly necessary; i.e., when the
1605 : // nested-name-specifier or the expression on the left of the ->
1606 : // or . is not dependent on a template-parameter, or the use
1607 : // does not appear in the scope of a template. -end note]
1608 : //
1609 : // Note: C++03 was more strict here, because it banned the use of
1610 : // the "template" keyword prior to a template-name that was not a
1611 : // dependent name. C++ DR468 relaxed this requirement (the
1612 : // "template" keyword is now permitted). We follow the C++0x
1613 : // rules, even in C++03 mode, retroactively applying the DR.
1614 27: TemplateTy Template;
1615 : TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType,
1616 27: EnteringContext, Template);
7: branch 0 taken
20: branch 1 taken
4: branch 3 taken
3: branch 4 taken
4: branch 6 taken
0: branch 7 not taken
2: branch 10 taken
2: branch 11 taken
25: branch 12 taken
2: branch 13 taken
1617 27: if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1618 : isa<CXXRecordDecl>(LookupCtx) &&
1619 : cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
1620 : // This is a dependent template.
5: branch 0 taken
20: branch 1 taken
1621 25: } else if (TNK == TNK_Non_template) {
1622 : Diag(Name.getSourceRange().getBegin(),
1623 : diag::err_template_kw_refers_to_non_template)
1624 : << GetNameFromUnqualifiedId(Name)
1625 5: << Name.getSourceRange();
1626 5: return TemplateTy();
1627 : } else {
1628 : // We found something; return it.
1629 20: return Template;
1630 : }
1631 : }
1632 :
1633 : NestedNameSpecifier *Qualifier
1634 40: = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1635 :
38: branch 1 taken
2: branch 2 taken
0: branch 3 not taken
0: branch 4 not taken
1636 40: switch (Name.getKind()) {
1637 : case UnqualifiedId::IK_Identifier:
1638 : return TemplateTy::make(Context.getDependentTemplateName(Qualifier,
1639 38: Name.Identifier));
1640 :
1641 : case UnqualifiedId::IK_OperatorFunctionId:
1642 : return TemplateTy::make(Context.getDependentTemplateName(Qualifier,
1643 2: Name.OperatorFunctionId.Operator));
1644 :
1645 : case UnqualifiedId::IK_LiteralOperatorId:
1646 0: assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1647 :
1648 : default:
1649 : break;
1650 : }
1651 :
1652 : Diag(Name.getSourceRange().getBegin(),
1653 : diag::err_template_kw_refers_to_non_template)
1654 : << GetNameFromUnqualifiedId(Name)
1655 0: << Name.getSourceRange();
1656 0: return TemplateTy();
1657 : }
1658 :
1659 : bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
1660 : const TemplateArgumentLoc &AL,
1661 3784: TemplateArgumentListBuilder &Converted) {
1662 3784: const TemplateArgument &Arg = AL.getArgument();
1663 :
1664 : // Check template type parameter.
15: branch 1 taken
3769: branch 2 taken
1665 3784: if (Arg.getKind() != TemplateArgument::Type) {
1666 : // C++ [temp.arg.type]p1:
1667 : // A template-argument for a template-parameter which is a
1668 : // type shall be a type-id.
1669 :
1670 : // We have a template type parameter but the template argument
1671 : // is not a type.
1672 15: SourceRange SR = AL.getSourceRange();
1673 15: Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
1674 15: Diag(Param->getLocation(), diag::note_template_param_here);
1675 :
1676 15: return true;
1677 : }
1678 :
2: branch 2 taken
3767: branch 3 taken
1679 3769: if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
1680 2: return true;
1681 :
1682 : // Add the converted template type argument.
1683 : Converted.Append(
1684 3767: TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
1685 3767: return false;
1686 : }
1687 :
1688 : /// \brief Substitute template arguments into the default template argument for
1689 : /// the given template type parameter.
1690 : ///
1691 : /// \param SemaRef the semantic analysis object for which we are performing
1692 : /// the substitution.
1693 : ///
1694 : /// \param Template the template that we are synthesizing template arguments
1695 : /// for.
1696 : ///
1697 : /// \param TemplateLoc the location of the template name that started the
1698 : /// template-id we are checking.
1699 : ///
1700 : /// \param RAngleLoc the location of the right angle bracket ('>') that
1701 : /// terminates the template-id.
1702 : ///
1703 : /// \param Param the template template parameter whose default we are
1704 : /// substituting into.
1705 : ///
1706 : /// \param Converted the list of template arguments provided for template
1707 : /// parameters that precede \p Param in the template parameter list.
1708 : ///
1709 : /// \returns the substituted template argument, or NULL if an error occurred.
1710 : static TypeSourceInfo *
1711 : SubstDefaultTemplateArgument(Sema &SemaRef,
1712 : TemplateDecl *Template,
1713 : SourceLocation TemplateLoc,
1714 : SourceLocation RAngleLoc,
1715 : TemplateTypeParmDecl *Param,
1716 89: TemplateArgumentListBuilder &Converted) {
1717 89: TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
1718 :
1719 : // If the argument type is dependent, instantiate it now based
1720 : // on the previously-computed template arguments.
47: branch 3 taken
42: branch 4 taken
1721 89: if (ArgType->getType()->isDependentType()) {
1722 : TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1723 47: /*TakeArgs=*/false);
1724 :
1725 : MultiLevelTemplateArgumentList AllTemplateArgs
1726 47: = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1727 :
1728 : Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1729 : Template, Converted.getFlatArguments(),
1730 : Converted.flatSize(),
1731 47: SourceRange(TemplateLoc, RAngleLoc));
1732 :
1733 : ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
1734 : Param->getDefaultArgumentLoc(),
1735 47: Param->getDeclName());
1736 : }
1737 :
1738 89: return ArgType;
1739 : }
1740 :
1741 : /// \brief Substitute template arguments into the default template argument for
1742 : /// the given non-type template parameter.
1743 : ///
1744 : /// \param SemaRef the semantic analysis object for which we are performing
1745 : /// the substitution.
1746 : ///
1747 : /// \param Template the template that we are synthesizing template arguments
1748 : /// for.
1749 : ///
1750 : /// \param TemplateLoc the location of the template name that started the
1751 : /// template-id we are checking.
1752 : ///
1753 : /// \param RAngleLoc the location of the right angle bracket ('>') that
1754 : /// terminates the template-id.
1755 : ///
1756 : /// \param Param the non-type template parameter whose default we are
1757 : /// substituting into.
1758 : ///
1759 : /// \param Converted the list of template arguments provided for template
1760 : /// parameters that precede \p Param in the template parameter list.
1761 : ///
1762 : /// \returns the substituted template argument, or NULL if an error occurred.
1763 : static Sema::OwningExprResult
1764 : SubstDefaultTemplateArgument(Sema &SemaRef,
1765 : TemplateDecl *Template,
1766 : SourceLocation TemplateLoc,
1767 : SourceLocation RAngleLoc,
1768 : NonTypeTemplateParmDecl *Param,
1769 13: TemplateArgumentListBuilder &Converted) {
1770 : TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1771 13: /*TakeArgs=*/false);
1772 :
1773 : MultiLevelTemplateArgumentList AllTemplateArgs
1774 13: = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1775 :
1776 : Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1777 : Template, Converted.getFlatArguments(),
1778 : Converted.flatSize(),
1779 13: SourceRange(TemplateLoc, RAngleLoc));
1780 :
1781 13: return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
1782 : }
1783 :
1784 : /// \brief Substitute template arguments into the default template argument for
1785 : /// the given template template parameter.
1786 : ///
1787 : /// \param SemaRef the semantic analysis object for which we are performing
1788 : /// the substitution.
1789 : ///
1790 : /// \param Template the template that we are synthesizing template arguments
1791 : /// for.
1792 : ///
1793 : /// \param TemplateLoc the location of the template name that started the
1794 : /// template-id we are checking.
1795 : ///
1796 : /// \param RAngleLoc the location of the right angle bracket ('>') that
1797 : /// terminates the template-id.
1798 : ///
1799 : /// \param Param the template template parameter whose default we are
1800 : /// substituting into.
1801 : ///
1802 : /// \param Converted the list of template arguments provided for template
1803 : /// parameters that precede \p Param in the template parameter list.
1804 : ///
1805 : /// \returns the substituted template argument, or NULL if an error occurred.
1806 : static TemplateName
1807 : SubstDefaultTemplateArgument(Sema &SemaRef,
1808 : TemplateDecl *Template,
1809 : SourceLocation TemplateLoc,
1810 : SourceLocation RAngleLoc,
1811 : TemplateTemplateParmDecl *Param,
1812 5: TemplateArgumentListBuilder &Converted) {
1813 : TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1814 5: /*TakeArgs=*/false);
1815 :
1816 : MultiLevelTemplateArgumentList AllTemplateArgs
1817 5: = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1818 :
1819 : Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1820 : Template, Converted.getFlatArguments(),
1821 : Converted.flatSize(),
1822 5: SourceRange(TemplateLoc, RAngleLoc));
1823 :
1824 : return SemaRef.SubstTemplateName(
1825 : Param->getDefaultArgument().getArgument().getAsTemplate(),
1826 : Param->getDefaultArgument().getTemplateNameLoc(),
1827 5: AllTemplateArgs);
1828 : }
1829 :
1830 : /// \brief If the given template parameter has a default template
1831 : /// argument, substitute into that default template argument and
1832 : /// return the corresponding template argument.
1833 : TemplateArgumentLoc
1834 : Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
1835 : SourceLocation TemplateLoc,
1836 : SourceLocation RAngleLoc,
1837 : Decl *Param,
1838 17: TemplateArgumentListBuilder &Converted) {
15: branch 1 taken
2: branch 2 taken
1839 17: if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
14: branch 1 taken
1: branch 2 taken
1840 15: if (!TypeParm->hasDefaultArgument())
1841 14: return TemplateArgumentLoc();
1842 :
1843 : TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
1844 : TemplateLoc,
1845 : RAngleLoc,
1846 : TypeParm,
1847 1: Converted);
1: branch 0 taken
0: branch 1 not taken
1848 1: if (DI)
1849 1: return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
1850 :
1851 0: return TemplateArgumentLoc();
1852 : }
1853 :
2: branch 0 taken
0: branch 1 not taken
1854 2: if (NonTypeTemplateParmDecl *NonTypeParm
1855 2: = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
0: branch 1 not taken
2: branch 2 taken
1856 2: if (!NonTypeParm->hasDefaultArgument())
1857 0: return TemplateArgumentLoc();
1858 :
1859 : OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
1860 : TemplateLoc,
1861 : RAngleLoc,
1862 : NonTypeParm,
1863 2: Converted);
1: branch 1 taken
1: branch 2 taken
1864 2: if (Arg.isInvalid())
1865 1: return TemplateArgumentLoc();
1866 :
1867 1: Expr *ArgE = Arg.takeAs<Expr>();
1868 1: return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
1869 : }
1870 :
1871 : TemplateTemplateParmDecl *TempTempParm
1872 0: = cast<TemplateTemplateParmDecl>(Param);
0: branch 1 not taken
0: branch 2 not taken
1873 0: if (!TempTempParm->hasDefaultArgument())
1874 0: return TemplateArgumentLoc();
1875 :
1876 : TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
1877 : TemplateLoc,
1878 : RAngleLoc,
1879 : TempTempParm,
1880 0: Converted);
0: branch 1 not taken
0: branch 2 not taken
1881 0: if (TName.isNull())
1882 0: return TemplateArgumentLoc();
1883 :
1884 : return TemplateArgumentLoc(TemplateArgument(TName),
1885 : TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
1886 0: TempTempParm->getDefaultArgument().getTemplateNameLoc());
1887 : }
1888 :
1889 : /// \brief Check that the given template argument corresponds to the given
1890 : /// template parameter.
1891 : bool Sema::CheckTemplateArgument(NamedDecl *Param,
1892 : const TemplateArgumentLoc &Arg,
1893 : TemplateDecl *Template,
1894 : SourceLocation TemplateLoc,
1895 : SourceLocation RAngleLoc,
1896 5550: TemplateArgumentListBuilder &Converted) {
1897 : // Check template type parameters.
3784: branch 1 taken
1766: branch 2 taken
1898 5550: if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1899 3784: return CheckTemplateTypeArgument(TTP, Arg, Converted);
1900 :
1901 : // Check non-type template parameters.
1725: branch 1 taken
41: branch 2 taken
1902 1766: if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1903 : // Do substitution on the type of the non-type template parameter
1904 : // with the template arguments we've seen thus far.
1905 1725: QualType NTTPType = NTTP->getType();
36: branch 2 taken
1689: branch 3 taken
1906 1725: if (NTTPType->isDependentType()) {
1907 : // Do substitution on the type of the non-type template parameter.
1908 : InstantiatingTemplate Inst(*this, TemplateLoc, Template,
1909 : NTTP, Converted.getFlatArguments(),
1910 : Converted.flatSize(),
1911 36: SourceRange(TemplateLoc, RAngleLoc));
1912 :
1913 : TemplateArgumentList TemplateArgs(Context, Converted,
1914 36: /*TakeArgs=*/false);
1915 : NTTPType = SubstType(NTTPType,
1916 : MultiLevelTemplateArgumentList(TemplateArgs),
1917 : NTTP->getLocation(),
1918 36: NTTP->getDeclName());
1919 : // If that worked, check the non-type template parameter type
1920 : // for validity.
36: branch 1 taken
0: branch 2 not taken
1921 36: if (!NTTPType.isNull())
1922 : NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
1923 36: NTTP->getLocation());
1: branch 1 taken
35: branch 2 taken
1924 36: if (NTTPType.isNull())
35: branch 1 taken
1: branch 2 taken
35: branch 4 taken
1: branch 5 taken
1925 1: return true;
1926 : }
1927 :
0: branch 2 not taken
1709: branch 3 taken
0: branch 4 not taken
1: branch 5 taken
14: branch 6 taken
0: branch 7 not taken
0: branch 8 not taken
1928 1724: switch (Arg.getArgument().getKind()) {
1929 : case TemplateArgument::Null:
1930 0: assert(false && "Should never see a NULL template argument here");
1931 : return true;
1932 :
1933 : case TemplateArgument::Expression: {
1934 1709: Expr *E = Arg.getArgument().getAsExpr();
1935 1709: TemplateArgument Result;
27: branch 1 taken
1682: branch 2 taken
1936 1709: if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
1937 54: return true;
1938 :
1939 1682: Converted.Append(Result);
27: branch 1 taken
1682: branch 2 taken
1940 3391: break;
1941 : }
1942 :
1943 : case TemplateArgument::Declaration:
1944 : case TemplateArgument::Integral:
1945 : // We've already checked this template argument, so just copy
1946 : // it to the list of converted arguments.
1947 0: Converted.Append(Arg.getArgument());
1948 0: break;
1949 :
1950 : case TemplateArgument::Template:
1951 : // We were given a template template argument. It may not be ill-formed;
1952 : // see below.
0: branch 0 not taken
1: branch 1 taken
1953 1: if (DependentTemplateName *DTN
1954 1: = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) {
1955 : // We have a template argument such as \c T::template X, which we
1956 : // parsed as a template template argument. However, since we now
1957 : // know that we need a non-type template argument, convert this
1958 : // template name into an expression.
1959 : Expr *E = DependentScopeDeclRefExpr::Create(Context,
1960 : DTN->getQualifier(),
1961 : Arg.getTemplateQualifierRange(),
1962 : DTN->getIdentifier(),
1963 0: Arg.getTemplateNameLoc());
1964 :
1965 0: TemplateArgument Result;
0: branch 1 not taken
0: branch 2 not taken
1966 0: if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
1967 0: return true;
1968 :
1969 0: Converted.Append(Result);
0: branch 1 not taken
0: branch 2 not taken
1970 0: break;
1971 : }
1972 :
1973 : // We have a template argument that actually does refer to a class
1974 : // template, template alias, or template template parameter, and
1975 : // therefore cannot be a non-type template argument.
1976 : Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
1977 1: << Arg.getSourceRange();
1978 :
1979 1: Diag(Param->getLocation(), diag::note_template_param_here);
1980 1: return true;
1981 :
1982 : case TemplateArgument::Type: {
1983 : // We have a non-type template parameter but the template
1984 : // argument is a type.
1985 :
1986 : // C++ [temp.arg]p2:
1987 : // In a template-argument, an ambiguity between a type-id and
1988 : // an expression is resolved to a type-id, regardless of the
1989 : // form of the corresponding template-parameter.
1990 : //
1991 : // We warn specifically about this case, since it can be rather
1992 : // confusing for users.
1993 14: QualType T = Arg.getArgument().getAsType();
1994 14: SourceRange SR = Arg.getSourceRange();
1: branch 2 taken
13: branch 3 taken
1995 14: if (T->isFunctionType())
1996 1: Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
1997 : else
1998 13: Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
1999 14: Diag(Param->getLocation(), diag::note_template_param_here);
2000 14: return true;
2001 : }
2002 :
2003 : case TemplateArgument::Pack:
2004 0: llvm_unreachable("Caller must expand template argument packs");
2005 : break;
2006 : }
2007 :
2008 1682: return false;
2009 : }
2010 :
2011 :
2012 : // Check template template parameters.
2013 41: TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
2014 :
2015 : // Substitute into the template parameter list of the template
2016 : // template parameter, since previously-supplied template arguments
2017 : // may appear within the template template parameter.
2018 : {
2019 : // Set up a template instantiation context.
2020 41: LocalInstantiationScope Scope(*this);
2021 : InstantiatingTemplate Inst(*this, TemplateLoc, Template,
2022 : TempParm, Converted.getFlatArguments(),
2023 : Converted.flatSize(),
2024 41: SourceRange(TemplateLoc, RAngleLoc));
2025 :
2026 : TemplateArgumentList TemplateArgs(Context, Converted,
2027 41: /*TakeArgs=*/false);
2028 : TempParm = cast_or_null<TemplateTemplateParmDecl>(
2029 : SubstDecl(TempParm, CurContext,
2030 41: MultiLevelTemplateArgumentList(TemplateArgs)));
1: branch 0 taken
40: branch 1 taken
2031 41: if (!TempParm)
40: branch 1 taken
1: branch 2 taken
40: branch 4 taken
1: branch 5 taken
40: branch 7 taken
1: branch 8 taken
2032 2: return true;
2033 :
2034 : // FIXME: TempParam is leaked.
2035 : }
2036 :
0: branch 2 not taken
39: branch 3 taken
1: branch 4 taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
0: branch 8 not taken
2037 40: switch (Arg.getArgument().getKind()) {
2038 : case TemplateArgument::Null:
2039 0: assert(false && "Should never see a NULL template argument here");
2040 : return true;
2041 :
2042 : case TemplateArgument::Template:
9: branch 1 taken
30: branch 2 taken
2043 39: if (CheckTemplateArgument(TempParm, Arg))
2044 9: return true;
2045 :
2046 30: Converted.Append(Arg.getArgument());
2047 30: break;
2048 :
2049 : case TemplateArgument::Expression:
2050 : case TemplateArgument::Type:
2051 : // We have a template template parameter but the template
2052 : // argument does not refer to a template.
2053 1: Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2054 1: return true;
2055 :
2056 : case TemplateArgument::Declaration:
2057 : llvm_unreachable(
2058 0: "Declaration argument with template template parameter");
2059 : break;
2060 : case TemplateArgument::Integral:
2061 : llvm_unreachable(
2062 0: "Integral argument with template template parameter");
2063 : break;
2064 :
2065 : case TemplateArgument::Pack:
2066 0: llvm_unreachable("Caller must expand template argument packs");
2067 : break;
2068 : }
2069 :
2070 30: return false;
2071 : }
2072 :
2073 : /// \brief Check that the given template argument list is well-formed
2074 : /// for specializing the given template.
2075 : bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2076 : SourceLocation TemplateLoc,
2077 : const TemplateArgumentListInfo &TemplateArgs,
2078 : bool PartialTemplateArgs,
2079 4112: TemplateArgumentListBuilder &Converted) {
2080 4112: TemplateParameterList *Params = Template->getTemplateParameters();
2081 4112: unsigned NumParams = Params->size();
2082 4112: unsigned NumArgs = TemplateArgs.size();
2083 4112: bool Invalid = false;
2084 :
2085 4112: SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2086 :
2087 : bool HasParameterPack =
4112: branch 0 taken
0: branch 1 not taken
11: branch 4 taken
4101: branch 5 taken
2088 4112: NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
2089 :
3: branch 0 taken
4109: branch 1 taken
2: branch 2 taken
1: branch 3 taken
33: branch 5 taken
4078: branch 6 taken
3: branch 7 taken
30: branch 8 taken
4: branch 9 taken
4108: branch 10 taken
2090 4112: if ((NumArgs > NumParams && !HasParameterPack) ||
2091 : (NumArgs < Params->getMinRequiredArguments() &&
2092 : !PartialTemplateArgs)) {
2093 : // FIXME: point at either the first arg beyond what we can handle,
2094 : // or the '>', depending on whether we have too many or too few
2095 : // arguments.
2096 4: SourceRange Range;
1: branch 0 taken
3: branch 1 taken
2097 4: if (NumArgs > NumParams)
2098 1: Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
2099 : Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2100 : << (NumArgs > NumParams)
2101 : << (isa<ClassTemplateDecl>(Template)? 0 :
2102 : isa<FunctionTemplateDecl>(Template)? 1 :
2103 : isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
4: branch 1 taken
0: branch 2 not taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 7 not taken
0: branch 8 not taken
2104 4: << Template << Range;
2105 : Diag(Template->getLocation(), diag::note_template_decl_here)
2106 4: << Params->getSourceRange();
2107 4: Invalid = true;
2108 : }
2109 :
2110 : // C++ [temp.arg]p1:
2111 : // [...] The type and form of each template-argument specified in
2112 : // a template-id shall match the type and form specified for the
2113 : // corresponding parameter declared by the template in its
2114 : // template-parameter-list.
2115 4112: unsigned ArgIdx = 0;
98: branch 2 taken
2: branch 3 taken
98: branch 5 taken
33: branch 6 taken
6: branch 7 taken
5587: branch 8 taken
4005: branch 9 taken
2116 13804: for (TemplateParameterList::iterator Param = Params->begin(),
2117 4112: ParamEnd = Params->end();
2118 : Param != ParamEnd; ++Param, ++ArgIdx) {
6: branch 0 taken
5581: branch 1 taken
0: branch 2 not taken
6: branch 3 taken
2119 5587: if (ArgIdx > NumArgs && PartialTemplateArgs)
2120 0: break;
2121 :
2122 : // If we have a template parameter pack, check every remaining template
2123 : // argument against that template parameter pack.
9: branch 1 taken
5578: branch 2 taken
2124 5587: if ((*Param)->isTemplateParameterPack()) {
2125 9: Converted.BeginPack();
7: branch 0 taken
8: branch 1 taken
2126 15: for (; ArgIdx < NumArgs; ++ArgIdx) {
1: branch 2 taken
6: branch 3 taken
2127 7: if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2128 : TemplateLoc, RAngleLoc, Converted)) {
2129 1: Invalid = true;
2130 1: break;
2131 : }
2132 : }
2133 9: Converted.EndPack();
2134 9: continue;
2135 : }
2136 :
5441: branch 0 taken
137: branch 1 taken
2137 5578: if (ArgIdx < NumArgs) {
2138 : // Check the template argument we were given.
68: branch 2 taken
5373: branch 3 taken
2139 5441: if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2140 : TemplateLoc, RAngleLoc, Converted))
2141 68: return true;
2142 :
2143 5373: continue;
2144 : }
2145 :
2146 : // We have a default template argument that we will use.
2147 137: TemplateArgumentLoc Arg;
2148 :
2149 : // Retrieve the default template argument from the template
2150 : // parameter. For each kind of template parameter, we substitute the
2151 : // template arguments provided thus far and any "outer" template arguments
2152 : // (when the template parameter was part of a nested template) into
2153 : // the default argument.
120: branch 1 taken
17: branch 2 taken
2154 137: if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
32: branch 1 taken
88: branch 2 taken
2155 120: if (!TTP->hasDefaultArgument()) {
30: branch 0 taken
2: branch 1 taken
0: branch 2 not taken
30: branch 3 taken
2156 32: assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2157 33: break;
2158 : }
2159 :
2160 : TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
2161 : Template,
2162 : TemplateLoc,
2163 : RAngleLoc,
2164 : TTP,
2165 88: Converted);
2: branch 0 taken
86: branch 1 taken
2166 88: if (!ArgType)
2167 8: return true;
2168 :
2169 : Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2170 86: ArgType);
11: branch 0 taken
6: branch 1 taken
2171 17: } else if (NonTypeTemplateParmDecl *NTTP
2172 17: = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
0: branch 1 not taken
11: branch 2 taken
2173 11: if (!NTTP->hasDefaultArgument()) {
0: branch 0 not taken
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
2174 0: assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2175 : break;
2176 : }
2177 :
2178 : Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template,
2179 : TemplateLoc,
2180 : RAngleLoc,
2181 : NTTP,
2182 11: Converted);
2: branch 1 taken
9: branch 2 taken
2183 11: if (E.isInvalid())
2184 2: return true;
2185 :
2186 9: Expr *Ex = E.takeAs<Expr>();
9: branch 6 taken
2: branch 7 taken
2187 9: Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2188 : } else {
2189 : TemplateTemplateParmDecl *TempParm
2190 6: = cast<TemplateTemplateParmDecl>(*Param);
2191 :
1: branch 1 taken
5: branch 2 taken
2192 6: if (!TempParm->hasDefaultArgument()) {
0: branch 0 not taken
1: branch 1 taken
1: branch 2 taken
1: branch 3 taken
2193 1: assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2194 : break;
2195 : }
2196 :
2197 : TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
2198 : TemplateLoc,
2199 : RAngleLoc,
2200 : TempParm,
2201 5: Converted);
0: branch 1 not taken
5: branch 2 taken
2202 5: if (Name.isNull())
2203 0: return true;
2204 :
2205 : Arg = TemplateArgumentLoc(TemplateArgument(Name),
2206 : TempParm->getDefaultArgument().getTemplateQualifierRange(),
2207 5: TempParm->getDefaultArgument().getTemplateNameLoc());
2208 : }
2209 :
2210 : // Introduce an instantiation record that describes where we are using
2211 : // the default template argument.
2212 : InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
2213 : Converted.getFlatArguments(),
2214 : Converted.flatSize(),
2215 100: SourceRange(TemplateLoc, RAngleLoc));
2216 :
2217 : // Check the default template argument.
2: branch 1 taken
98: branch 2 taken
2218 100: if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
2219 : RAngleLoc, Converted))
2220 2: return true;
2221 : }
2222 :
2223 4038: return Invalid;
2224 : }
2225 :
2226 : /// \brief Check a template argument against its corresponding
2227 : /// template type parameter.
2228 : ///
2229 : /// This routine implements the semantics of C++ [temp.arg.type]. It
2230 : /// returns true if an error occurred, and false otherwise.
2231 : bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
2232 3817: TypeSourceInfo *ArgInfo) {
0: branch 0 not taken
3817: branch 1 taken
2233 3817: assert(ArgInfo && "invalid TypeSourceInfo");
2234 3817: QualType Arg = ArgInfo->getType();
2235 :
2236 : // C++ [temp.arg.type]p2:
2237 : // A local type, a type with no linkage, an unnamed type or a type
2238 : // compounded from any of these types shall not be used as a
2239 : // template-argument for a template type-parameter.
2240 : //
2241 : // FIXME: Perform the recursive and no-linkage type checks.
2242 3817: const TagType *Tag = 0;
0: branch 2 not taken
3817: branch 3 taken
2243 3817: if (const EnumType *EnumT = Arg->getAs<EnumType>())
2244 0: Tag = EnumT;
619: branch 2 taken
3198: branch 3 taken
2245 3817: else if (const RecordType *RecordT = Arg->getAs<RecordType>())
2246 619: Tag = RecordT;
619: branch 0 taken
3198: branch 1 taken
1: branch 5 taken
618: branch 6 taken
1: branch 7 taken
3816: branch 8 taken
2247 3817: if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) {
2248 1: SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2249 : return Diag(SR.getBegin(), diag::err_template_arg_local_type)
2250 1: << QualType(Tag, 0) << SR;
618: branch 0 taken
3198: branch 1 taken
2: branch 5 taken
616: branch 6 taken
1: branch 9 taken
1: branch 10 taken
1: branch 11 taken
3815: branch 12 taken
2251 3816: } else if (Tag && !Tag->getDecl()->getDeclName() &&
2252 : !Tag->getDecl()->getTypedefForAnonDecl()) {
2253 1: SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2254 1: Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR;
2255 1: Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
2256 1: return true;
0: branch 2 not taken
3815: branch 3 taken
2257 3815: } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
2258 0: SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2259 0: return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
2260 : }
2261 :
2262 3815: return false;
2263 : }
2264 :
2265 : /// \brief Checks whether the given template argument is the address
2266 : /// of an object or function according to C++ [temp.arg.nontype]p1.
2267 : bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
2268 72: NamedDecl *&Entity) {
2269 72: bool Invalid = false;
2270 :
2271 : // See through any implicit casts we added to fix the type.
29: branch 1 taken
72: branch 2 taken
2272 130: while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
2273 29: Arg = Cast->getSubExpr();
2274 :
2275 : // C++0x allows nullptr, and there's no further checking to be done for that.
3: branch 3 taken
69: branch 4 taken
2276 72: if (Arg->getType()->isNullPtrType())
2277 3: return false;
2278 :
2279 : // C++ [temp.arg.nontype]p1:
2280 : //
2281 : // A template-argument for a non-type, non-template
2282 : // template-parameter shall be one of: [...]
2283 : //
2284 : // -- the address of an object or function with external
2285 : // linkage, including function templates and function
2286 : // template-ids but excluding non-static class members,
2287 : // expressed as & id-expression where the & is optional if
2288 : // the name refers to a function or array, or if the
2289 : // corresponding template-parameter is a reference; or
2290 69: DeclRefExpr *DRE = 0;
2291 :
2292 : // Ignore (and complain about) any excess parentheses.
1: branch 1 taken
69: branch 2 taken
2293 71: while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1: branch 0 taken
0: branch 1 not taken
2294 1: if (!Invalid) {
2295 : Diag(Arg->getSourceRange().getBegin(),
2296 : diag::err_template_arg_extra_parens)
2297 1: << Arg->getSourceRange();
2298 1: Invalid = true;
2299 : }
2300 :
2301 1: Arg = Parens->getSubExpr();
2302 : }
2303 :
16: branch 1 taken
53: branch 2 taken
2304 69: if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
16: branch 1 taken
0: branch 2 not taken
2305 16: if (UnOp->getOpcode() == UnaryOperator::AddrOf)
2306 16: DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
2307 : } else
2308 53: DRE = dyn_cast<DeclRefExpr>(Arg);
2309 :
1: branch 0 taken
68: branch 1 taken
2310 69: if (!DRE)
2311 : return Diag(Arg->getSourceRange().getBegin(),
2312 : diag::err_template_arg_not_decl_ref)
2313 1: << Arg->getSourceRange();
2314 :
2315 : // Stop checking the precise nature of the argument if it is value dependent,
2316 : // it should be checked when instantiated.
4: branch 1 taken
64: branch 2 taken
2317 68: if (Arg->isValueDependent())
2318 4: return false;
2319 :
0: branch 2 not taken
64: branch 3 taken
2320 64: if (!isa<ValueDecl>(DRE->getDecl()))
2321 : return Diag(Arg->getSourceRange().getBegin(),
2322 : diag::err_template_arg_not_object_or_func_form)
2323 0: << Arg->getSourceRange();
2324 :
2325 : // Cannot refer to non-static data members
0: branch 2 not taken
64: branch 3 taken
2326 64: if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
2327 : return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
2328 0: << Field << Arg->getSourceRange();
2329 :
2330 : // Cannot refer to non-static member functions
1: branch 2 taken
63: branch 3 taken
2331 64: if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
0: branch 1 not taken
1: branch 2 taken
2332 1: if (!Method->isStatic())
2333 : return Diag(Arg->getSourceRange().getBegin(),
2334 : diag::err_template_arg_method)
2335 0: << Method << Arg->getSourceRange();
2336 :
2337 : // Functions must have external linkage.
31: branch 2 taken
33: branch 3 taken
2338 64: if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
0: branch 2 not taken
31: branch 3 taken
2339 31: if (!isExternalLinkage(Func->getLinkage())) {
2340 : Diag(Arg->getSourceRange().getBegin(),
2341 : diag::err_template_arg_function_not_extern)
2342 0: << Func << Arg->getSourceRange();
2343 : Diag(Func->getLocation(), diag::note_template_arg_internal_object)
2344 0: << true;
2345 0: return true;
2346 : }
2347 :
2348 : // Okay: we've named a function with external linkage.
2349 31: Entity = Func;
2350 31: return Invalid;
2351 : }
2352 :
33: branch 2 taken
0: branch 3 not taken
2353 33: if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
0: branch 2 not taken
33: branch 3 taken
2354 33: if (!isExternalLinkage(Var->getLinkage())) {
2355 : Diag(Arg->getSourceRange().getBegin(),
2356 : diag::err_template_arg_object_not_extern)
2357 0: << Var << Arg->getSourceRange();
2358 : Diag(Var->getLocation(), diag::note_template_arg_internal_object)
2359 0: << true;
2360 0: return true;
2361 : }
2362 :
2363 : // Okay: we've named an object with external linkage
2364 33: Entity = Var;
2365 33: return Invalid;
2366 : }
2367 :
2368 : // We found something else, but we don't know specifically what it is.
2369 : Diag(Arg->getSourceRange().getBegin(),
2370 : diag::err_template_arg_not_object_or_func)
2371 0: << Arg->getSourceRange();
2372 : Diag(DRE->getDecl()->getLocation(),
2373 0: diag::note_template_arg_refers_here);
2374 0: return true;
2375 : }
2376 :
2377 : /// \brief Checks whether the given template argument is a pointer to
2378 : /// member constant according to C++ [temp.arg.nontype]p1.
2379 : bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
2380 16: TemplateArgument &Converted) {
2381 16: bool Invalid = false;
2382 :
2383 : // See through any implicit casts we added to fix the type.
2: branch 1 taken
16: branch 2 taken
2384 20: while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
2385 2: Arg = Cast->getSubExpr();
2386 :
2387 : // C++0x allows nullptr, and there's no further checking to be done for that.
2: branch 3 taken
14: branch 4 taken
2388 16: if (Arg->getType()->isNullPtrType())
2389 2: return false;
2390 :
2391 : // C++ [temp.arg.nontype]p1:
2392 : //
2393 : // A template-argument for a non-type, non-template
2394 : // template-parameter shall be one of: [...]
2395 : //
2396 : // -- a pointer to member expressed as described in 5.3.1.
2397 14: DeclRefExpr *DRE = 0;
2398 :
2399 : // Ignore (and complain about) any excess parentheses.
1: branch 1 taken
14: branch 2 taken
2400 16: while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1: branch 0 taken
0: branch 1 not taken
2401 1: if (!Invalid) {
2402 : Diag(Arg->getSourceRange().getBegin(),
2403 : diag::err_template_arg_extra_parens)
2404 1: << Arg->getSourceRange();
2405 1: Invalid = true;
2406 : }
2407 :
2408 1: Arg = Parens->getSubExpr();
2409 : }
2410 :
2411 : // A pointer-to-member constant written &Class::member.
12: branch 1 taken
2: branch 2 taken
2412 14: if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
12: branch 1 taken
0: branch 2 not taken
2413 12: if (UnOp->getOpcode() == UnaryOperator::AddrOf) {
2414 12: DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
12: branch 0 taken
0: branch 1 not taken
0: branch 3 not taken
12: branch 4 taken
0: branch 5 not taken
12: branch 6 taken
2415 12: if (DRE && !DRE->getQualifier())
2416 0: DRE = 0;
2417 : }
2418 : }
2419 : // A constant of pointer-to-member type.
2: branch 1 taken
0: branch 2 not taken
2420 2: else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
2: branch 2 taken
0: branch 3 not taken
2421 2: if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
2: branch 3 taken
0: branch 4 not taken
2422 2: if (VD->getType()->isMemberPointerType()) {
0: branch 1 not taken
2: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 9 not taken
0: branch 10 not taken
2: branch 11 taken
0: branch 12 not taken
2423 2: if (isa<NonTypeTemplateParmDecl>(VD) ||
2424 : (isa<VarDecl>(VD) &&
2425 : Context.getCanonicalType(VD->getType()).isConstQualified())) {
2: branch 1 taken
0: branch 2 not taken
2: branch 4 taken
0: branch 5 not taken
2: branch 6 taken
0: branch 7 not taken
2426 2: if (Arg->isTypeDependent() || Arg->isValueDependent())
2427 2: Converted = TemplateArgument(Arg->Retain());
2428 : else
2429 0: Converted = TemplateArgument(VD->getCanonicalDecl());
2430 2: return Invalid;
2431 : }
2432 : }
2433 : }
2434 :
2435 0: DRE = 0;
2436 : }
2437 :
0: branch 0 not taken
12: branch 1 taken
2438 12: if (!DRE)
2439 : return Diag(Arg->getSourceRange().getBegin(),
2440 : diag::err_template_arg_not_pointer_to_member_form)
2441 0: << Arg->getSourceRange();
2442 :
3: branch 2 taken
9: branch 3 taken
3: branch 6 taken
0: branch 7 not taken
12: branch 8 taken
0: branch 9 not taken
2443 12: if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
2444 : assert((isa<FieldDecl>(DRE->getDecl()) ||
2445 : !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3: branch 2 taken
9: branch 3 taken
3: branch 7 taken
0: branch 8 not taken
2446 12: "Only non-static member pointers can make it here");
2447 :
2448 : // Okay: this is the address of a non-static member, and therefore
2449 : // a member pointer constant.
12: branch 1 taken
0: branch 2 not taken
0: branch 4 not taken
12: branch 5 taken
0: branch 6 not taken
12: branch 7 taken
2450 24: if (Arg->isTypeDependent() || Arg->isValueDependent())
2451 0: Converted = TemplateArgument(Arg->Retain());
2452 : else
2453 12: Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
2454 12: return Invalid;
2455 : }
2456 :
2457 : // We found something else, but we don't know specifically what it is.
2458 : Diag(Arg->getSourceRange().getBegin(),
2459 : diag::err_template_arg_not_pointer_to_member_form)
2460 0: << Arg->getSourceRange();
2461 : Diag(DRE->getDecl()->getLocation(),
2462 0: diag::note_template_arg_refers_here);
2463 0: return true;
2464 : }
2465 :
2466 : /// \brief Check a template argument against its corresponding
2467 : /// non-type template parameter.
2468 : ///
2469 : /// This routine implements the semantics of C++ [temp.arg.nontype].
2470 : /// It returns true if an error occurred, and false otherwise. \p
2471 : /// InstantiatedParamType is the type of the non-type template
2472 : /// parameter after it has been instantiated.
2473 : ///
2474 : /// If no error was detected, Converted receives the converted template argument.
2475 : bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
2476 : QualType InstantiatedParamType, Expr *&Arg,
2477 1732: TemplateArgument &Converted) {
2478 1732: SourceLocation StartLoc = Arg->getSourceRange().getBegin();
2479 :
2480 : // If either the parameter has a dependent type or the argument is
2481 : // type-dependent, there's nothing we can check now.
2482 : // FIXME: Add template argument to Converted!
1717: branch 2 taken
15: branch 3 taken
9: branch 5 taken
1708: branch 6 taken
24: branch 7 taken
1708: branch 8 taken
2483 1732: if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
2484 : // FIXME: Produce a cloned, canonical expression?
2485 24: Converted = TemplateArgument(Arg);
2486 24: return false;
2487 : }
2488 :
2489 : // C++ [temp.arg.nontype]p5:
2490 : // The following conversions are performed on each expression used
2491 : // as a non-type template-argument. If a non-type
2492 : // template-argument cannot be converted to the type of the
2493 : // corresponding template-parameter then the program is
2494 : // ill-formed.
2495 : //
2496 : // -- for a non-type template-parameter of integral or
2497 : // enumeration type, integral promotions (4.5) and integral
2498 : // conversions (4.7) are applied.
2499 1708: QualType ParamType = InstantiatedParamType;
2500 1708: QualType ArgType = Arg->getType();
104: branch 2 taken
1604: branch 3 taken
0: branch 6 not taken
104: branch 7 taken
1604: branch 8 taken
104: branch 9 taken
2501 1708: if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
2502 : // C++ [temp.arg.nontype]p1:
2503 : // A template-argument for a non-type, non-template
2504 : // template-parameter shall be one of:
2505 : //
2506 : // -- an integral constant-expression of integral or enumeration
2507 : // type; or
2508 : // -- the name of a non-type template-parameter; or
2509 1604: SourceLocation NonConstantLoc;
2510 1604: llvm::APSInt Value;
1: branch 2 taken
1603: branch 3 taken
1: branch 6 taken
0: branch 7 not taken
1: branch 8 taken
1603: branch 9 taken
2511 1604: if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
2512 : Diag(Arg->getSourceRange().getBegin(),
2513 : diag::err_template_arg_not_integral_or_enumeral)
2514 1: << ArgType << Arg->getSourceRange();
2515 1: Diag(Param->getLocation(), diag::note_template_param_here);
2516 1: return true;
1523: branch 1 taken
80: branch 2 taken
1: branch 4 taken
1522: branch 5 taken
1: branch 6 taken
1602: branch 7 taken
2517 1603: } else if (!Arg->isValueDependent() &&
2518 : !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
2519 : Diag(NonConstantLoc, diag::err_template_arg_not_ice)
2520 1: << ArgType << Arg->getSourceRange();
2521 1: return true;
2522 : }
2523 :
2524 : // FIXME: We need some way to more easily get the unqualified form
2525 : // of the types without going all the way to the
2526 : // canonical type.
0: branch 2 not taken
1602: branch 3 taken
2527 1602: if (Context.getCanonicalType(ParamType).getCVRQualifiers())
2528 0: ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
10: branch 2 taken
1592: branch 3 taken
2529 1602: if (Context.getCanonicalType(ArgType).getCVRQualifiers())
2530 10: ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
2531 :
2532 : // Try to convert the argument to the parameter's type.
492: branch 1 taken
1110: branch 2 taken
2533 1602: if (Context.hasSameType(ParamType, ArgType)) {
2534 : // Okay: no conversion necessary
489: branch 1 taken
3: branch 2 taken
488: branch 5 taken
1: branch 6 taken
491: branch 7 taken
1: branch 8 taken
2535 492: } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
2536 : !ParamType->isEnumeralType()) {
2537 : // This is an integral promotion or conversion.
2538 491: ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast);
2539 : } else {
2540 : // We can't perform this conversion.
2541 : Diag(Arg->getSourceRange().getBegin(),
2542 : diag::err_template_arg_not_convertible)
2543 1: << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
2544 1: Diag(Param->getLocation(), diag::note_template_param_here);
2545 1: return true;
2546 : }
2547 :
2548 1601: QualType IntegerType = Context.getCanonicalType(ParamType);
12: branch 2 taken
1589: branch 3 taken
2549 1601: if (const EnumType *Enum = IntegerType->getAs<EnumType>())
2550 12: IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
2551 :
1521: branch 1 taken
80: branch 2 taken
2552 1601: if (!Arg->isValueDependent()) {
2553 : // Check that an unsigned parameter does not receive a negative
2554 : // value.
1279: branch 2 taken
242: branch 3 taken
251: branch 5 taken
1028: branch 6 taken
1: branch 8 taken
250: branch 9 taken
1: branch 10 taken
1520: branch 11 taken
2555 1521: if (IntegerType->isUnsignedIntegerType()
2556 : && (Value.isSigned() && Value.isNegative())) {
2557 : Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
2558 : << Value.toString(10) << Param->getType()
2559 1: << Arg->getSourceRange();
2560 1: Diag(Param->getLocation(), diag::note_template_param_here);
2561 1: return true;
2562 : }
2563 :
2564 : // Check that we don't overflow the template parameter type.
2565 1520: unsigned AllowedBits = Context.getTypeSize(IntegerType);
2566 : unsigned RequiredBits;
1278: branch 2 taken
242: branch 3 taken
2567 1520: if (IntegerType->isUnsignedIntegerType())
2568 1278: RequiredBits = Value.getActiveBits();
20: branch 1 taken
222: branch 2 taken
2569 242: else if (Value.isUnsigned())
2570 20: RequiredBits = Value.getActiveBits() + 1;
2571 : else
2572 222: RequiredBits = Value.getMinSignedBits();
4: branch 0 taken
1516: branch 1 taken
2573 1520: if (RequiredBits > AllowedBits) {
2574 : Diag(Arg->getSourceRange().getBegin(),
2575 : diag::err_template_arg_too_large)
2576 : << Value.toString(10) << Param->getType()
2577 4: << Arg->getSourceRange();
2578 4: Diag(Param->getLocation(), diag::note_template_param_here);
2579 4: return true;
2580 : }
2581 :
57: branch 1 taken
1459: branch 2 taken
2582 1516: if (Value.getBitWidth() != AllowedBits)
2583 57: Value.extOrTrunc(AllowedBits);
2584 1516: Value.setIsSigned(IntegerType->isSignedIntegerType());
2585 : }
2586 :
2587 : // Add the value of this argument to the list of converted
2588 : // arguments. We use the bitwidth and signedness of the template
2589 : // parameter.
80: branch 1 taken
1516: branch 2 taken
2590 1596: if (Arg->isValueDependent()) {
2591 : // The argument is value-dependent. Create a new
2592 : // TemplateArgument with the converted expression.
2593 80: Converted = TemplateArgument(Arg);
2594 80: return false;
2595 : }
2596 :
2597 : Converted = TemplateArgument(Value,
2598 : ParamType->isEnumeralType() ? ParamType
10: branch 2 taken
1506: branch 3 taken
2599 1516: : IntegerType);
2600 1516: return false;
2601 : }
2602 :
2603 : // Handle pointer-to-function, reference-to-function, and
2604 : // pointer-to-member-function all in (roughly) the same way.
49: branch 2 taken
55: branch 3 taken
21: branch 9 taken
28: branch 10 taken
37: branch 13 taken
39: branch 14 taken
27: branch 20 taken
10: branch 21 taken
18: branch 24 taken
48: branch 25 taken
5: branch 31 taken
13: branch 32 taken
43: branch 33 taken
61: branch 34 taken
2605 104: if (// -- For a non-type template-parameter of type pointer to
2606 : // function, only the function-to-pointer conversion (4.3) is
2607 : // applied. If the template-argument represents a set of
2608 : // overloaded functions (or a pointer to such), the matching
2609 : // function is selected from the set (13.4).
2610 : // In C++0x, any std::nullptr_t value can be converted.
2611 : (ParamType->isPointerType() &&
2612 : ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
2613 : // -- For a non-type template-parameter of type reference to
2614 : // function, no conversions apply. If the template-argument
2615 : // represents a set of overloaded functions, the matching
2616 : // function is selected from the set (13.4).
2617 : (ParamType->isReferenceType() &&
2618 : ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
2619 : // -- For a non-type template-parameter of type pointer to
2620 : // member function, no conversions apply. If the
2621 : // template-argument represents a set of overloaded member
2622 : // functions, the matching member function is selected from
2623 : // the set (13.4).
2624 : // Again, C++0x allows a std::nullptr_t value.
2625 : (ParamType->isMemberPointerType() &&
2626 : ParamType->getAs<MemberPointerType>()->getPointeeType()
2627 : ->isFunctionType())) {
30: branch 2 taken
13: branch 3 taken
2628 43: if (Context.hasSameUnqualifiedType(ArgType,
2629 : ParamType.getNonReferenceType())) {
2630 : // We don't have to do anything: the types already match.
2: branch 2 taken
28: branch 3 taken
1: branch 6 taken
1: branch 7 taken
1: branch 10 taken
0: branch 11 not taken
2: branch 12 taken
28: branch 13 taken
2631 30: } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
2632 : ParamType->isMemberPointerType())) {
2633 2: ArgType = ParamType;
1: branch 2 taken
1: branch 3 taken
2634 2: if (ParamType->isMemberPointerType())
2635 1: ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer);
2636 : else
2637 1: ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast);
10: branch 2 taken
18: branch 3 taken
9: branch 6 taken
1: branch 7 taken
9: branch 8 taken
19: branch 9 taken
2638 28: } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
2639 9: ArgType = Context.getPointerType(ArgType);
2640 9: ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay);
15: branch 0 taken
4: branch 1 taken
2641 19: } else if (FunctionDecl *Fn
2642 19: = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
0: branch 3 not taken
15: branch 4 taken
2643 15: if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
2644 0: return true;
2645 :
2646 15: Arg = FixOverloadedFunctionReference(Arg, Fn);
2647 15: ArgType = Arg->getType();
9: branch 2 taken
6: branch 3 taken
5: branch 6 taken
4: branch 7 taken
5: branch 8 taken
10: branch 9 taken
2648 15: if (ArgType->isFunctionType() && ParamType->isPointerType()) {
2649 5: ArgType = Context.getPointerType(Arg->getType());
2650 5: ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay);
2651 : }
2652 : }
2653 :
6: branch 2 taken
37: branch 3 taken
2654 43: if (!Context.hasSameUnqualifiedType(ArgType,
2655 : ParamType.getNonReferenceType())) {
2656 : // We can't perform this conversion.
2657 : Diag(Arg->getSourceRange().getBegin(),
2658 : diag::err_template_arg_not_convertible)
2659 6: << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
2660 6: Diag(Param->getLocation(), diag::note_template_param_here);
2661 6: return true;
2662 : }
2663 :
4: branch 2 taken
33: branch 3 taken
2664 37: if (ParamType->isMemberPointerType())
2665 4: return CheckTemplateArgumentPointerToMember(Arg, Converted);
2666 :
2667 33: NamedDecl *Entity = 0;
0: branch 1 not taken
33: branch 2 taken
2668 33: if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2669 0: return true;
2670 :
1: branch 1 taken
32: branch 2 taken
2671 33: if (Arg->isValueDependent()) {
2672 1: Converted = TemplateArgument(Arg);
2673 : } else {
31: branch 0 taken
1: branch 1 taken
2674 32: if (Entity)
2675 31: Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2676 32: Converted = TemplateArgument(Entity);
2677 : }
2678 33: return false;
2679 : }
2680 :
21: branch 2 taken
40: branch 3 taken
2681 61: if (ParamType->isPointerType()) {
2682 : // -- for a non-type template-parameter of type pointer to
2683 : // object, qualification conversions (4.4) and the
2684 : // array-to-pointer conversion (4.2) are applied.
2685 : // C++0x also allows a value of std::nullptr_t.
2686 : assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
21: branch 5 taken
0: branch 6 not taken
2687 21: "Only object pointers allowed here");
2688 :
2: branch 2 taken
19: branch 3 taken
2689 21: if (ArgType->isNullPtrType()) {
2690 2: ArgType = ParamType;
2691 2: ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast);
5: branch 2 taken
14: branch 3 taken
2692 19: } else if (ArgType->isArrayType()) {
2693 5: ArgType = Context.getArrayDecayedType(ArgType);
2694 5: ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay);
2695 : }
2696 :
6: branch 1 taken
15: branch 2 taken
2697 21: if (IsQualificationConversion(ArgType, ParamType)) {
2698 6: ArgType = ParamType;
2699 6: ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp);
2700 : }
2701 :
0: branch 1 not taken
21: branch 2 taken
2702 21: if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
2703 : // We can't perform this conversion.
2704 : Diag(Arg->getSourceRange().getBegin(),
2705 : diag::err_template_arg_not_convertible)
2706 0: << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
2707 0: Diag(Param->getLocation(), diag::note_template_param_here);
2708 0: return true;
2709 : }
2710 :
2711 21: NamedDecl *Entity = 0;
2: branch 1 taken
19: branch 2 taken
2712 21: if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2713 2: return true;
2714 :
2: branch 1 taken
17: branch 2 taken
2715 19: if (Arg->isValueDependent()) {
2716 2: Converted = TemplateArgument(Arg);
2717 : } else {
15: branch 0 taken
2: branch 1 taken
2718 17: if (Entity)
2719 15: Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2720 17: Converted = TemplateArgument(Entity);
2721 : }
2722 19: return false;
2723 : }
2724 :
27: branch 2 taken
13: branch 3 taken
2725 40: if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
2726 : // -- For a non-type template-parameter of type reference to
2727 : // object, no conversions apply. The type referred to by the
2728 : // reference may be more cv-qualified than the (otherwise
2729 : // identical) type of the template-argument. The
2730 : // template-parameter is bound directly to the
2731 : // template-argument, which must be an lvalue.
2732 : assert(ParamRefType->getPointeeType()->isObjectType() &&
27: branch 3 taken
0: branch 4 not taken
2733 27: "Only object references allowed here");
2734 :
2735 27: QualType ReferredType = ParamRefType->getPointeeType();
1: branch 1 taken
26: branch 2 taken
2736 27: if (!Context.hasSameUnqualifiedType(ReferredType, ArgType)) {
2737 : Diag(Arg->getSourceRange().getBegin(),
2738 : diag::err_template_arg_no_ref_bind)
2739 : << InstantiatedParamType << Arg->getType()
2740 1: << Arg->getSourceRange();
2741 1: Diag(Param->getLocation(), diag::note_template_param_here);
2742 1: return true;
2743 : }
2744 :
2745 : unsigned ParamQuals
2746 26: = Context.getCanonicalType(ReferredType).getCVRQualifiers();
2747 26: unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
2748 :
8: branch 0 taken
18: branch 1 taken
2749 26: if ((ParamQuals | ArgQuals) != ParamQuals) {
2750 : Diag(Arg->getSourceRange().getBegin(),
2751 : diag::err_template_arg_ref_bind_ignores_quals)
2752 : << InstantiatedParamType << Arg->getType()
2753 8: << Arg->getSourceRange();
2754 8: Diag(Param->getLocation(), diag::note_template_param_here);
2755 8: return true;
2756 : }
2757 :
2758 18: NamedDecl *Entity = 0;
0: branch 1 not taken
18: branch 2 taken
2759 18: if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2760 0: return true;
2761 :
1: branch 1 taken
17: branch 2 taken
2762 18: if (Arg->isValueDependent()) {
2763 1: Converted = TemplateArgument(Arg);
2764 : } else {
2765 17: Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2766 17: Converted = TemplateArgument(Entity);
2767 : }
2768 18: return false;
2769 : }
2770 :
2771 : // -- For a non-type template-parameter of type pointer to data
2772 : // member, qualification conversions (4.4) are applied.
2773 : // C++0x allows std::nullptr_t values.
13: branch 2 taken
0: branch 3 not taken
2774 13: assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2775 :
2: branch 1 taken
11: branch 2 taken
2776 13: if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
2777 : // Types match exactly: nothing more to do here.
1: branch 2 taken
1: branch 3 taken
2778 2: } else if (ArgType->isNullPtrType()) {
2779 1: ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer);
0: branch 1 not taken
1: branch 2 taken
2780 1: } else if (IsQualificationConversion(ArgType, ParamType)) {
2781 0: ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp);
2782 : } else {
2783 : // We can't perform this conversion.
2784 : Diag(Arg->getSourceRange().getBegin(),
2785 : diag::err_template_arg_not_convertible)
2786 1: << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
2787 1: Diag(Param->getLocation(), diag::note_template_param_here);
2788 1: return true;
2789 : }
2790 :
2791 12: return CheckTemplateArgumentPointerToMember(Arg, Converted);
2792 : }
2793 :
2794 : /// \brief Check a template argument against its corresponding
2795 : /// template template parameter.
2796 : ///
2797 : /// This routine implements the semantics of C++ [temp.arg.template].
2798 : /// It returns true if an error occurred, and false otherwise.
2799 : bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
2800 39: const TemplateArgumentLoc &Arg) {
2801 39: TemplateName Name = Arg.getArgument().getAsTemplate();
2802 39: TemplateDecl *Template = Name.getAsTemplateDecl();
0: branch 0 not taken
39: branch 1 taken
2803 39: if (!Template) {
2804 : // Any dependent template name is fine.
0: branch 1 not taken
0: branch 2 not taken
2805 0: assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
2806 0: return false;
2807 : }
2808 :
2809 : // C++ [temp.arg.template]p1:
2810 : // A template-argument for a template template-parameter shall be
2811 : // the name of a class template, expressed as id-expression. Only
2812 : // primary class templates are considered when matching the
2813 : // template template argument with the corresponding parameter;
2814 : // partial specializations are not considered even if their
2815 : // parameter lists match that of the template template parameter.
2816 : //
2817 : // Note that we also allow template template parameters here, which
2818 : // will happen when we are dealing with, e.g., class template
2819 : // partial specializations.
6: branch 1 taken
33: branch 2 taken
0: branch 4 not taken
6: branch 5 taken
0: branch 6 not taken
39: branch 7 taken
2820 39: if (!isa<ClassTemplateDecl>(Template) &&
2821 : !isa<TemplateTemplateParmDecl>(Template)) {
2822 : assert(isa<FunctionTemplateDecl>(Template) &&
0: branch 1 not taken
0: branch 2 not taken
2823 0: "Only function templates are possible here");
2824 0: Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
2825 : Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
2826 0: << Template;
2827 : }
2828 :
2829 : return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2830 : Param->getTemplateParameters(),
2831 : true,
2832 : TPL_TemplateTemplateArgumentMatch,
2833 39: Arg.getLocation());
2834 : }
2835 :
2836 : /// \brief Determine whether the given template parameter lists are
2837 : /// equivalent.
2838 : ///
2839 : /// \param New The new template parameter list, typically written in the
2840 : /// source code as part of a new template declaration.
2841 : ///
2842 : /// \param Old The old template parameter list, typically found via
2843 : /// name lookup of the template declared with this template parameter
2844 : /// list.
2845 : ///
2846 : /// \param Complain If true, this routine will produce a diagnostic if
2847 : /// the template parameter lists are not equivalent.
2848 : ///
2849 : /// \param Kind describes how we are to match the template parameter lists.
2850 : ///
2851 : /// \param TemplateArgLoc If this source location is valid, then we
2852 : /// are actually checking the template parameter list of a template
2853 : /// argument (New) against the template parameter list of its
2854 : /// corresponding template template parameter (Old). We produce
2855 : /// slightly different diagnostics in this scenario.
2856 : ///
2857 : /// \returns True if the template parameter lists are equal, false
2858 : /// otherwise.
2859 : bool
2860 : Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2861 : TemplateParameterList *Old,
2862 : bool Complain,
2863 : TemplateParameterListEqualKind Kind,
2864 279: SourceLocation TemplateArgLoc) {
9: branch 2 taken
270: branch 3 taken
2865 279: if (Old->size() != New->size()) {
6: branch 0 taken
3: branch 1 taken
2866 9: if (Complain) {
2867 6: unsigned NextDiag = diag::err_template_param_list_different_arity;
3: branch 1 taken
3: branch 2 taken
2868 6: if (TemplateArgLoc.isValid()) {
2869 3: Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2870 3: NextDiag = diag::note_template_param_list_different_arity;
2871 : }
2872 : Diag(New->getTemplateLoc(), NextDiag)
2873 : << (New->size() > Old->size())
2874 : << (Kind != TPL_TemplateMatch)
2875 6: << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
2876 : Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
2877 : << (Kind != TPL_TemplateMatch)
2878 6: << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2879 : }
2880 :
2881 9: return false;
2882 : }
2883 :
323: branch 1 taken
252: branch 2 taken
2884 845: for (TemplateParameterList::iterator OldParm = Old->begin(),
2885 270: OldParmEnd = Old->end(), NewParm = New->begin();
2886 : OldParm != OldParmEnd; ++OldParm, ++NewParm) {
10: branch 2 taken
313: branch 3 taken
2887 323: if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
3: branch 0 taken
7: branch 1 taken
2888 10: if (Complain) {
2889 3: unsigned NextDiag = diag::err_template_param_different_kind;
1: branch 1 taken
2: branch 2 taken
2890 3: if (TemplateArgLoc.isValid()) {
2891 1: Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2892 1: NextDiag = diag::note_template_param_different_kind;
2893 : }
2894 : Diag((*NewParm)->getLocation(), NextDiag)
2895 3: << (Kind != TPL_TemplateMatch);
2896 : Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
2897 3: << (Kind != TPL_TemplateMatch);
2898 : }
2899 10: return false;
2900 : }
2901 :
79: branch 1 taken
234: branch 2 taken
2902 313: if (isa<TemplateTypeParmDecl>(*OldParm)) {
2903 : // Okay; all template type parameters are equivalent (since we
2904 : // know we're at the same index).
66: branch 0 taken
13: branch 1 taken
2905 79: } else if (NonTypeTemplateParmDecl *OldNTTP
2906 79: = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2907 : // The types of non-type template parameters must agree.
2908 : NonTypeTemplateParmDecl *NewNTTP
2909 66: = cast<NonTypeTemplateParmDecl>(*NewParm);
2910 :
2911 : // If we are matching a template template argument to a template
2912 : // template parameter and one of the non-type template parameter types
2913 : // is dependent, then we must wait until template instantiation time
2914 : // to actually compare the arguments.
19: branch 0 taken
47: branch 1 taken
19: branch 5 taken
0: branch 6 not taken
1: branch 10 taken
18: branch 11 taken
65: branch 12 taken
1: branch 13 taken
2915 66: if (Kind == TPL_TemplateTemplateArgumentMatch &&
2916 : (OldNTTP->getType()->isDependentType() ||
2917 : NewNTTP->getType()->isDependentType()))
2918 1: continue;
2919 :
7: branch 5 taken
58: branch 6 taken
2920 65: if (Context.getCanonicalType(OldNTTP->getType()) !=
2921 : Context.getCanonicalType(NewNTTP->getType())) {
6: branch 0 taken
1: branch 1 taken
2922 7: if (Complain) {
2923 6: unsigned NextDiag = diag::err_template_nontype_parm_different_type;
5: branch 1 taken
1: branch 2 taken
2924 6: if (TemplateArgLoc.isValid()) {
2925 : Diag(TemplateArgLoc,
2926 5: diag::err_template_arg_template_params_mismatch);
2927 5: NextDiag = diag::note_template_nontype_parm_different_type;
2928 : }
2929 : Diag(NewNTTP->getLocation(), NextDiag)
2930 : << NewNTTP->getType()
2931 6: << (Kind != TPL_TemplateMatch);
2932 : Diag(OldNTTP->getLocation(),
2933 : diag::note_template_nontype_parm_prev_declaration)
2934 6: << OldNTTP->getType();
2935 : }
2936 7: return false;
2937 : }
2938 : } else {
2939 : // The template parameter lists of template template
2940 : // parameters must agree.
2941 : assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
13: branch 1 taken
0: branch 2 not taken
2942 13: "Only template template parameters handled here");
2943 : TemplateTemplateParmDecl *OldTTP
2944 13: = cast<TemplateTemplateParmDecl>(*OldParm);
2945 : TemplateTemplateParmDecl *NewTTP
2946 13: = cast<TemplateTemplateParmDecl>(*NewParm);
0: branch 0 not taken
13: branch 1 taken
1: branch 5 taken
12: branch 6 taken
2947 13: if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2948 : OldTTP->getTemplateParameters(),
2949 : Complain,
2950 : (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind),
2951 : TemplateArgLoc))
2952 1: return false;
2953 : }
2954 : }
2955 :
2956 252: return true;
2957 : }
2958 :
2959 : /// \brief Check whether a template can be declared within this scope.
2960 : ///
2961 : /// If the template declaration is valid in this scope, returns
2962 : /// false. Otherwise, issues a diagnostic and returns true.
2963 : bool
2964 1420: Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
2965 : // Find the nearest enclosing declaration scope.
1420: branch 1 taken
979: branch 2 taken
0: branch 4 not taken
1420: branch 5 taken
979: branch 6 taken
1420: branch 7 taken
2966 3819: while ((S->getFlags() & Scope::DeclScope) == 0 ||
2967 : (S->getFlags() & Scope::TemplateParamScope) != 0)
2968 979: S = S->getParent();
2969 :
2970 : // C++ [temp]p2:
2971 : // A template-declaration can appear only as a namespace scope or
2972 : // class scope declaration.
2973 1420: DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
1420: branch 0 taken
0: branch 1 not taken
3: branch 3 taken
1417: branch 4 taken
1: branch 7 taken
2: branch 8 taken
1: branch 9 taken
1419: branch 10 taken
2974 1420: if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2975 : cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
2976 : return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
2977 1: << TemplateParams->getSourceRange();
2978 :
1422: branch 0 taken
0: branch 1 not taken
3: branch 3 taken
1419: branch 4 taken
3: branch 5 taken
1419: branch 6 taken
2979 2841: while (Ctx && isa<LinkageSpecDecl>(Ctx))
2980 3: Ctx = Ctx->getParent();
2981 :
1419: branch 0 taken
0: branch 1 not taken
193: branch 3 taken
1226: branch 4 taken
193: branch 6 taken
0: branch 7 not taken
1419: branch 8 taken
0: branch 9 not taken
2982 1419: if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2983 1419: return false;
2984 :
2985 : return Diag(TemplateParams->getTemplateLoc(),
2986 : diag::err_template_outside_namespace_or_class_scope)
2987 0: << TemplateParams->getSourceRange();
2988 : }
2989 :
2990 : /// \brief Determine what kind of template specialization the given declaration
2991 : /// is.
2992 226: static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
0: branch 0 not taken
226: branch 1 taken
2993 226: if (!D)
2994 0: return TSK_Undeclared;
2995 :
62: branch 1 taken
164: branch 2 taken
2996 226: if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
2997 62: return Record->getTemplateSpecializationKind();
130: branch 1 taken
34: branch 2 taken
2998 164: if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
2999 130: return Function->getTemplateSpecializationKind();
34: branch 1 taken
0: branch 2 not taken
3000 34: if (VarDecl *Var = dyn_cast<VarDecl>(D))
3001 34: return Var->getTemplateSpecializationKind();
3002 :
3003 0: return TSK_Undeclared;
3004 : }
3005 :
3006 : /// \brief Check whether a specialization is well-formed in the current
3007 : /// context.
3008 : ///
3009 : /// This routine determines whether a template specialization can be declared
3010 : /// in the current context (C++ [temp.expl.spec]p2).
3011 : ///
3012 : /// \param S the semantic analysis object for which this check is being
3013 : /// performed.
3014 : ///
3015 : /// \param Specialized the entity being specialized or instantiated, which
3016 : /// may be a kind of template (class template, function template, etc.) or
3017 : /// a member of a class template (member function, static data member,
3018 : /// member class).
3019 : ///
3020 : /// \param PrevDecl the previous declaration of this entity, if any.
3021 : ///
3022 : /// \param Loc the location of the explicit specialization or instantiation of
3023 : /// this entity.
3024 : ///
3025 : /// \param IsPartialSpecialization whether this is a partial specialization of
3026 : /// a class template.
3027 : ///
3028 : /// \returns true if there was an error that we cannot recover from, false
3029 : /// otherwise.
3030 : static bool CheckTemplateSpecializationScope(Sema &S,
3031 : NamedDecl *Specialized,
3032 : NamedDecl *PrevDecl,
3033 : SourceLocation Loc,
3034 287: bool IsPartialSpecialization) {
3035 : // Keep these "kind" numbers in sync with the %select statements in the
3036 : // various diagnostics emitted by this routine.
3037 287: int EntityKind = 0;
3038 287: bool isTemplateSpecialization = false;
189: branch 1 taken
98: branch 2 taken
3039 287: if (isa<ClassTemplateDecl>(Specialized)) {
89: branch 0 taken
100: branch 1 taken
3040 189: EntityKind = IsPartialSpecialization? 1 : 0;
3041 189: isTemplateSpecialization = true;
47: branch 1 taken
51: branch 2 taken
3042 98: } else if (isa<FunctionTemplateDecl>(Specialized)) {
3043 47: EntityKind = 2;
3044 47: isTemplateSpecialization = true;
20: branch 1 taken
31: branch 2 taken
3045 51: } else if (isa<CXXMethodDecl>(Specialized))
3046 20: EntityKind = 3;
17: branch 1 taken
14: branch 2 taken
3047 31: else if (isa<VarDecl>(Specialized))
3048 17: EntityKind = 4;
14: branch 1 taken
0: branch 2 not taken
3049 14: else if (isa<RecordDecl>(Specialized))
3050 14: EntityKind = 5;
3051 : else {
3052 0: S.Diag(Loc, diag::err_template_spec_unknown_kind);
3053 0: S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
3054 0: return true;
3055 : }
3056 :
3057 : // C++ [temp.expl.spec]p2:
3058 : // An explicit specialization shall be declared in the namespace
3059 : // of which the template is a member, or, for member templates, in
3060 : // the namespace of which the enclosing class or enclosing class
3061 : // template is a member. An explicit specialization of a member
3062 : // function, member class or static data member of a class
3063 : // template shall be declared in the namespace of which the class
3064 : // template is a member. Such a declaration may also be a
3065 : // definition. If the declaration is not a definition, the
3066 : // specialization may be defined later in the name- space