blob: 5eb2b2145c96dedbf9dea680eb300b2d5a3081bc [file] [log] [blame]
Douglas Gregordd861062008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
2
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//+//===----------------------------------------------------------------------===/
9
10//
11// This file implements semantic analysis for C++ templates.
12//+//===----------------------------------------------------------------------===/
13
14#include "Sema.h"
Douglas Gregord406b032009-02-06 22:42:48 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor1b21c7f2008-12-05 23:32:09 +000016#include "clang/AST/Expr.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregordd861062008-12-05 18:15:24 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
20
21using namespace clang;
22
Douglas Gregor2fa10442008-12-18 19:37:40 +000023/// isTemplateName - Determines whether the identifier II is a
24/// template name in the current scope, and returns the template
25/// declaration if II names a template. An optional CXXScope can be
26/// passed to indicate the C++ scope in which the identifier will be
27/// found.
Douglas Gregor8e458f42009-02-09 18:46:07 +000028Sema::TemplateNameKind Sema::isTemplateName(IdentifierInfo &II, Scope *S,
29 DeclTy *&Template,
30 const CXXScopeSpec *SS) {
Douglas Gregor09be81b2009-02-04 17:27:36 +000031 NamedDecl *IIDecl = LookupParsedName(S, SS, &II, LookupOrdinaryName);
Douglas Gregor2fa10442008-12-18 19:37:40 +000032
33 if (IIDecl) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000034 if (isa<TemplateDecl>(IIDecl)) {
35 Template = IIDecl;
36 if (isa<FunctionTemplateDecl>(IIDecl))
37 return TNK_Function_template;
38 else if (isa<ClassTemplateDecl>(IIDecl))
39 return TNK_Class_template;
40 else if (isa<TemplateTemplateParmDecl>(IIDecl))
41 return TNK_Template_template_parm;
42 else
43 assert(false && "Unknown TemplateDecl");
44 }
Douglas Gregor279272e2009-02-04 19:02:06 +000045
Douglas Gregor8e458f42009-02-09 18:46:07 +000046 // FIXME: What follows is a gross hack.
Douglas Gregor2fa10442008-12-18 19:37:40 +000047 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(IIDecl)) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000048 if (FD->getType()->isDependentType()) {
49 Template = FD;
50 return TNK_Function_template;
51 }
Douglas Gregor2fa10442008-12-18 19:37:40 +000052 } else if (OverloadedFunctionDecl *Ovl
53 = dyn_cast<OverloadedFunctionDecl>(IIDecl)) {
54 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
55 FEnd = Ovl->function_end();
56 F != FEnd; ++F) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000057 if ((*F)->getType()->isDependentType()) {
58 Template = Ovl;
59 return TNK_Function_template;
60 }
Douglas Gregor2fa10442008-12-18 19:37:40 +000061 }
62 }
Douglas Gregor2fa10442008-12-18 19:37:40 +000063 }
Douglas Gregor8e458f42009-02-09 18:46:07 +000064 return TNK_Non_template;
Douglas Gregor2fa10442008-12-18 19:37:40 +000065}
66
Douglas Gregordd861062008-12-05 18:15:24 +000067/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
68/// that the template parameter 'PrevDecl' is being shadowed by a new
69/// declaration at location Loc. Returns true to indicate that this is
70/// an error, and false otherwise.
71bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregor2715a1f2008-12-08 18:40:42 +000072 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregordd861062008-12-05 18:15:24 +000073
74 // Microsoft Visual C++ permits template parameters to be shadowed.
75 if (getLangOptions().Microsoft)
76 return false;
77
78 // C++ [temp.local]p4:
79 // A template-parameter shall not be redeclared within its
80 // scope (including nested scopes).
81 Diag(Loc, diag::err_template_param_shadow)
82 << cast<NamedDecl>(PrevDecl)->getDeclName();
83 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
84 return true;
85}
86
Douglas Gregor279272e2009-02-04 19:02:06 +000087/// AdjustDeclForTemplates - If the given decl happens to be a template, reset
88/// the parameter D to reference the templated declaration and return a pointer
89/// to the template declaration. Otherwise, do nothing to D and return null.
90TemplateDecl *Sema::AdjustDeclIfTemplate(DeclTy *&D)
91{
92 if(TemplateDecl *Temp = dyn_cast<TemplateDecl>(static_cast<Decl*>(D))) {
93 D = Temp->getTemplatedDecl();
94 return Temp;
95 }
96 return 0;
97}
98
Douglas Gregordd861062008-12-05 18:15:24 +000099/// ActOnTypeParameter - Called when a C++ template type parameter
100/// (e.g., "typename T") has been parsed. Typename specifies whether
101/// the keyword "typename" was used to declare the type parameter
102/// (otherwise, "class" was used), and KeyLoc is the location of the
103/// "class" or "typename" keyword. ParamName is the name of the
104/// parameter (NULL indicates an unnamed template parameter) and
105/// ParamName is the location of the parameter name (if any).
106/// If the type parameter has a default argument, it will be added
107/// later via ActOnTypeParameterDefault.
108Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename,
109 SourceLocation KeyLoc,
110 IdentifierInfo *ParamName,
Douglas Gregor52473432008-12-24 02:52:09 +0000111 SourceLocation ParamNameLoc,
112 unsigned Depth, unsigned Position) {
Douglas Gregordd861062008-12-05 18:15:24 +0000113 assert(S->isTemplateParamScope() &&
114 "Template type parameter not in template parameter scope!");
115 bool Invalid = false;
116
117 if (ParamName) {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000118 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000119 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000120 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
121 PrevDecl);
122 }
123
Douglas Gregord406b032009-02-06 22:42:48 +0000124 SourceLocation Loc = ParamNameLoc;
125 if (!ParamName)
126 Loc = KeyLoc;
127
Douglas Gregordd861062008-12-05 18:15:24 +0000128 TemplateTypeParmDecl *Param
Douglas Gregord406b032009-02-06 22:42:48 +0000129 = TemplateTypeParmDecl::Create(Context, CurContext, Loc,
Douglas Gregor279272e2009-02-04 19:02:06 +0000130 Depth, Position, ParamName, Typename);
Douglas Gregordd861062008-12-05 18:15:24 +0000131 if (Invalid)
132 Param->setInvalidDecl();
133
134 if (ParamName) {
135 // Add the template parameter into the current scope.
136 S->AddDecl(Param);
137 IdResolver.AddDecl(Param);
138 }
139
140 return Param;
141}
142
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000143/// ActOnTypeParameterDefault - Adds a default argument (the type
144/// Default) to the given template type parameter (TypeParam).
145void Sema::ActOnTypeParameterDefault(DeclTy *TypeParam,
146 SourceLocation EqualLoc,
147 SourceLocation DefaultLoc,
148 TypeTy *DefaultT) {
149 TemplateTypeParmDecl *Parm
150 = cast<TemplateTypeParmDecl>(static_cast<Decl *>(TypeParam));
151 QualType Default = QualType::getFromOpaquePtr(DefaultT);
152
153 // C++ [temp.param]p14:
154 // A template-parameter shall not be used in its own default argument.
155 // FIXME: Implement this check! Needs a recursive walk over the types.
156
157 // Check the template argument itself.
158 if (CheckTemplateArgument(Parm, Default, DefaultLoc)) {
159 Parm->setInvalidDecl();
160 return;
161 }
162
163 Parm->setDefaultArgument(Default, DefaultLoc, false);
164}
165
Douglas Gregordd861062008-12-05 18:15:24 +0000166/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
167/// template parameter (e.g., "int Size" in "template<int Size>
168/// class Array") has been parsed. S is the current scope and D is
169/// the parsed declarator.
Douglas Gregor52473432008-12-24 02:52:09 +0000170Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
171 unsigned Depth,
172 unsigned Position) {
Douglas Gregordd861062008-12-05 18:15:24 +0000173 QualType T = GetTypeForDeclarator(D, S);
174
Douglas Gregor279272e2009-02-04 19:02:06 +0000175 assert(S->isTemplateParamScope() &&
176 "Non-type template parameter not in template parameter scope!");
Douglas Gregordd861062008-12-05 18:15:24 +0000177 bool Invalid = false;
178
179 IdentifierInfo *ParamName = D.getIdentifier();
180 if (ParamName) {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000181 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000182 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000183 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregor279272e2009-02-04 19:02:06 +0000184 PrevDecl);
Douglas Gregordd861062008-12-05 18:15:24 +0000185 }
186
Douglas Gregor62cdc792009-02-10 17:43:50 +0000187 // C++ [temp.param]p4:
188 //
189 // A non-type template-parameter shall have one of the following
190 // (optionally cv-qualified) types:
191 //
192 // -- integral or enumeration type,
193 if (T->isIntegralType() || T->isEnumeralType() ||
194 // -- pointer to object or pointer to function,
195 T->isPointerType() ||
196 // -- reference to object or reference to function,
197 T->isReferenceType() ||
198 // -- pointer to member.
199 T->isMemberPointerType() ||
200 // If T is a dependent type, we can't do the check now, so we
201 // assume that it is well-formed.
202 T->isDependentType()) {
203 // Okay: The template parameter is well-formed.
204 }
205 // C++ [temp.param]p8:
206 //
207 // A non-type template-parameter of type "array of T" or
208 // "function returning T" is adjusted to be of type "pointer to
209 // T" or "pointer to function returning T", respectively.
210 else if (T->isArrayType())
211 // FIXME: Keep the type prior to promotion?
212 T = Context.getArrayDecayedType(T);
213 else if (T->isFunctionType())
214 // FIXME: Keep the type prior to promotion?
215 T = Context.getPointerType(T);
216 else {
217 Diag(D.getIdentifierLoc(), diag::err_template_nontype_parm_bad_type)
218 << T;
219 return 0;
220 }
221
Douglas Gregordd861062008-12-05 18:15:24 +0000222 NonTypeTemplateParmDecl *Param
223 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
Douglas Gregor279272e2009-02-04 19:02:06 +0000224 Depth, Position, ParamName, T);
Douglas Gregordd861062008-12-05 18:15:24 +0000225 if (Invalid)
226 Param->setInvalidDecl();
227
228 if (D.getIdentifier()) {
229 // Add the template parameter into the current scope.
230 S->AddDecl(Param);
231 IdResolver.AddDecl(Param);
232 }
233 return Param;
234}
Douglas Gregor52473432008-12-24 02:52:09 +0000235
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000236/// \brief Adds a default argument to the given non-type template
237/// parameter.
238void Sema::ActOnNonTypeTemplateParameterDefault(DeclTy *TemplateParamD,
239 SourceLocation EqualLoc,
240 ExprArg DefaultE) {
241 NonTypeTemplateParmDecl *TemplateParm
242 = cast<NonTypeTemplateParmDecl>(static_cast<Decl *>(TemplateParamD));
243 Expr *Default = static_cast<Expr *>(DefaultE.get());
244
245 // C++ [temp.param]p14:
246 // A template-parameter shall not be used in its own default argument.
247 // FIXME: Implement this check! Needs a recursive walk over the types.
248
249 // Check the well-formedness of the default template argument.
250 if (CheckTemplateArgument(TemplateParm, Default)) {
251 TemplateParm->setInvalidDecl();
252 return;
253 }
254
255 TemplateParm->setDefaultArgument(static_cast<Expr *>(DefaultE.release()));
256}
257
Douglas Gregor279272e2009-02-04 19:02:06 +0000258
259/// ActOnTemplateTemplateParameter - Called when a C++ template template
260/// parameter (e.g. T in template <template <typename> class T> class array)
261/// has been parsed. S is the current scope.
262Sema::DeclTy *Sema::ActOnTemplateTemplateParameter(Scope* S,
263 SourceLocation TmpLoc,
264 TemplateParamsTy *Params,
265 IdentifierInfo *Name,
266 SourceLocation NameLoc,
267 unsigned Depth,
268 unsigned Position)
269{
270 assert(S->isTemplateParamScope() &&
271 "Template template parameter not in template parameter scope!");
272
273 // Construct the parameter object.
274 TemplateTemplateParmDecl *Param =
275 TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth,
276 Position, Name,
277 (TemplateParameterList*)Params);
278
279 // Make sure the parameter is valid.
280 // FIXME: Decl object is not currently invalidated anywhere so this doesn't
281 // do anything yet. However, if the template parameter list or (eventual)
282 // default value is ever invalidated, that will propagate here.
283 bool Invalid = false;
284 if (Invalid) {
285 Param->setInvalidDecl();
286 }
287
288 // If the tt-param has a name, then link the identifier into the scope
289 // and lookup mechanisms.
290 if (Name) {
291 S->AddDecl(Param);
292 IdResolver.AddDecl(Param);
293 }
294
295 return Param;
296}
297
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000298/// \brief Adds a default argument to the given template template
299/// parameter.
300void Sema::ActOnTemplateTemplateParameterDefault(DeclTy *TemplateParamD,
301 SourceLocation EqualLoc,
302 ExprArg DefaultE) {
303 TemplateTemplateParmDecl *TemplateParm
304 = cast<TemplateTemplateParmDecl>(static_cast<Decl *>(TemplateParamD));
305
306 // Since a template-template parameter's default argument is an
307 // id-expression, it must be a DeclRefExpr.
308 DeclRefExpr *Default
309 = cast<DeclRefExpr>(static_cast<Expr *>(DefaultE.get()));
310
311 // C++ [temp.param]p14:
312 // A template-parameter shall not be used in its own default argument.
313 // FIXME: Implement this check! Needs a recursive walk over the types.
314
315 // Check the well-formedness of the template argument.
316 if (!isa<TemplateDecl>(Default->getDecl())) {
317 Diag(Default->getSourceRange().getBegin(),
318 diag::err_template_arg_must_be_template)
319 << Default->getSourceRange();
320 TemplateParm->setInvalidDecl();
321 return;
322 }
323 if (CheckTemplateArgument(TemplateParm, Default)) {
324 TemplateParm->setInvalidDecl();
325 return;
326 }
327
328 DefaultE.release();
329 TemplateParm->setDefaultArgument(Default);
330}
331
Douglas Gregor52473432008-12-24 02:52:09 +0000332/// ActOnTemplateParameterList - Builds a TemplateParameterList that
333/// contains the template parameters in Params/NumParams.
334Sema::TemplateParamsTy *
335Sema::ActOnTemplateParameterList(unsigned Depth,
336 SourceLocation ExportLoc,
337 SourceLocation TemplateLoc,
338 SourceLocation LAngleLoc,
339 DeclTy **Params, unsigned NumParams,
340 SourceLocation RAngleLoc) {
341 if (ExportLoc.isValid())
342 Diag(ExportLoc, diag::note_template_export_unsupported);
343
Douglas Gregord406b032009-02-06 22:42:48 +0000344 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
345 (Decl**)Params, NumParams, RAngleLoc);
Douglas Gregor52473432008-12-24 02:52:09 +0000346}
Douglas Gregor279272e2009-02-04 19:02:06 +0000347
Douglas Gregord406b032009-02-06 22:42:48 +0000348Sema::DeclTy *
349Sema::ActOnClassTemplate(Scope *S, unsigned TagSpec, TagKind TK,
350 SourceLocation KWLoc, const CXXScopeSpec &SS,
351 IdentifierInfo *Name, SourceLocation NameLoc,
352 AttributeList *Attr,
353 MultiTemplateParamsArg TemplateParameterLists) {
354 assert(TemplateParameterLists.size() > 0 && "No template parameter lists?");
355 assert(TK != TK_Reference && "Can only declare or define class templates");
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000356 bool Invalid = false;
Douglas Gregord406b032009-02-06 22:42:48 +0000357
358 // Check that we can declare a template here.
359 if (CheckTemplateDeclScope(S, TemplateParameterLists))
360 return 0;
361
362 TagDecl::TagKind Kind;
363 switch (TagSpec) {
364 default: assert(0 && "Unknown tag type!");
365 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
366 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
367 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
368 }
369
370 // There is no such thing as an unnamed class template.
371 if (!Name) {
372 Diag(KWLoc, diag::err_template_unnamed_class);
373 return 0;
374 }
375
376 // Find any previous declaration with this name.
377 LookupResult Previous = LookupParsedName(S, &SS, Name, LookupOrdinaryName,
378 true);
379 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
380 NamedDecl *PrevDecl = 0;
381 if (Previous.begin() != Previous.end())
382 PrevDecl = *Previous.begin();
383
384 DeclContext *SemanticContext = CurContext;
385 if (SS.isNotEmpty() && !SS.isInvalid()) {
386 SemanticContext = static_cast<DeclContext*>(SS.getScopeRep());
387
388 // FIXME: need to match up several levels of template parameter
389 // lists here.
390 }
391
392 // FIXME: member templates!
393 TemplateParameterList *TemplateParams
394 = static_cast<TemplateParameterList *>(*TemplateParameterLists.release());
395
396 // If there is a previous declaration with the same name, check
397 // whether this is a valid redeclaration.
398 ClassTemplateDecl *PrevClassTemplate
399 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
400 if (PrevClassTemplate) {
401 // Ensure that the template parameter lists are compatible.
402 if (!TemplateParameterListsAreEqual(TemplateParams,
403 PrevClassTemplate->getTemplateParameters(),
404 /*Complain=*/true))
405 return 0;
406
407 // C++ [temp.class]p4:
408 // In a redeclaration, partial specialization, explicit
409 // specialization or explicit instantiation of a class template,
410 // the class-key shall agree in kind with the original class
411 // template declaration (7.1.5.3).
412 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
413 if (PrevRecordDecl->getTagKind() != Kind) {
414 Diag(KWLoc, diag::err_use_with_wrong_tag) << Name;
415 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
416 return 0;
417 }
418
419
420 // Check for redefinition of this class template.
421 if (TK == TK_Definition) {
422 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
423 Diag(NameLoc, diag::err_redefinition) << Name;
424 Diag(Def->getLocation(), diag::note_previous_definition);
425 // FIXME: Would it make sense to try to "forget" the previous
426 // definition, as part of error recovery?
427 return 0;
428 }
429 }
430 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
431 // Maybe we will complain about the shadowed template parameter.
432 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
433 // Just pretend that we didn't see the previous declaration.
434 PrevDecl = 0;
435 } else if (PrevDecl) {
436 // C++ [temp]p5:
437 // A class template shall not have the same name as any other
438 // template, class, function, object, enumeration, enumerator,
439 // namespace, or type in the same scope (3.3), except as specified
440 // in (14.5.4).
441 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
442 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
443 return 0;
444 }
445
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000446 // Check the template parameter list of this declaration, possibly
447 // merging in the template parameter list from the previous class
448 // template declaration.
449 if (CheckTemplateParameterList(TemplateParams,
450 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
451 Invalid = true;
452
Douglas Gregord406b032009-02-06 22:42:48 +0000453 // If we had a scope specifier, we better have a previous template
454 // declaration!
455
456 TagDecl *NewClass =
457 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name,
458 PrevClassTemplate?
459 PrevClassTemplate->getTemplatedDecl() : 0);
460
461 ClassTemplateDecl *NewTemplate
462 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
463 DeclarationName(Name), TemplateParams,
464 NewClass);
465
466 // Set the lexical context of these templates
467 NewClass->setLexicalDeclContext(CurContext);
468 NewTemplate->setLexicalDeclContext(CurContext);
469
470 if (TK == TK_Definition)
471 NewClass->startDefinition();
472
473 if (Attr)
474 ProcessDeclAttributeList(NewClass, Attr);
475
476 PushOnScopeChains(NewTemplate, S);
477
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000478 if (Invalid) {
479 NewTemplate->setInvalidDecl();
480 NewClass->setInvalidDecl();
481 }
Douglas Gregord406b032009-02-06 22:42:48 +0000482 return NewTemplate;
483}
484
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000485/// \brief Checks the validity of a template parameter list, possibly
486/// considering the template parameter list from a previous
487/// declaration.
488///
489/// If an "old" template parameter list is provided, it must be
490/// equivalent (per TemplateParameterListsAreEqual) to the "new"
491/// template parameter list.
492///
493/// \param NewParams Template parameter list for a new template
494/// declaration. This template parameter list will be updated with any
495/// default arguments that are carried through from the previous
496/// template parameter list.
497///
498/// \param OldParams If provided, template parameter list from a
499/// previous declaration of the same template. Default template
500/// arguments will be merged from the old template parameter list to
501/// the new template parameter list.
502///
503/// \returns true if an error occurred, false otherwise.
504bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
505 TemplateParameterList *OldParams) {
506 bool Invalid = false;
507
508 // C++ [temp.param]p10:
509 // The set of default template-arguments available for use with a
510 // template declaration or definition is obtained by merging the
511 // default arguments from the definition (if in scope) and all
512 // declarations in scope in the same way default function
513 // arguments are (8.3.6).
514 bool SawDefaultArgument = false;
515 SourceLocation PreviousDefaultArgLoc;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000516
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000517 TemplateParameterList::iterator OldParam;
518 if (OldParams)
519 OldParam = OldParams->begin();
520
521 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
522 NewParamEnd = NewParams->end();
523 NewParam != NewParamEnd; ++NewParam) {
524 // Variables used to diagnose redundant default arguments
525 bool RedundantDefaultArg = false;
526 SourceLocation OldDefaultLoc;
527 SourceLocation NewDefaultLoc;
528
529 // Variables used to diagnose missing default arguments
530 bool MissingDefaultArg = false;
531
532 // Merge default arguments for template type parameters.
533 if (TemplateTypeParmDecl *NewTypeParm
534 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
535 TemplateTypeParmDecl *OldTypeParm
536 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
537
538 if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
539 NewTypeParm->hasDefaultArgument()) {
540 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
541 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
542 SawDefaultArgument = true;
543 RedundantDefaultArg = true;
544 PreviousDefaultArgLoc = NewDefaultLoc;
545 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
546 // Merge the default argument from the old declaration to the
547 // new declaration.
548 SawDefaultArgument = true;
549 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
550 OldTypeParm->getDefaultArgumentLoc(),
551 true);
552 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
553 } else if (NewTypeParm->hasDefaultArgument()) {
554 SawDefaultArgument = true;
555 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
556 } else if (SawDefaultArgument)
557 MissingDefaultArg = true;
558 }
559 // Merge default arguments for non-type template parameters
560 else if (NonTypeTemplateParmDecl *NewNonTypeParm
561 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
562 NonTypeTemplateParmDecl *OldNonTypeParm
563 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
564 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
565 NewNonTypeParm->hasDefaultArgument()) {
566 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
567 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
568 SawDefaultArgument = true;
569 RedundantDefaultArg = true;
570 PreviousDefaultArgLoc = NewDefaultLoc;
571 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
572 // Merge the default argument from the old declaration to the
573 // new declaration.
574 SawDefaultArgument = true;
575 // FIXME: We need to create a new kind of "default argument"
576 // expression that points to a previous template template
577 // parameter.
578 NewNonTypeParm->setDefaultArgument(
579 OldNonTypeParm->getDefaultArgument());
580 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
581 } else if (NewNonTypeParm->hasDefaultArgument()) {
582 SawDefaultArgument = true;
583 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
584 } else if (SawDefaultArgument)
585 MissingDefaultArg = true;
586 }
587 // Merge default arguments for template template parameters
588 else {
589 TemplateTemplateParmDecl *NewTemplateParm
590 = cast<TemplateTemplateParmDecl>(*NewParam);
591 TemplateTemplateParmDecl *OldTemplateParm
592 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
593 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
594 NewTemplateParm->hasDefaultArgument()) {
595 OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
596 NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
597 SawDefaultArgument = true;
598 RedundantDefaultArg = true;
599 PreviousDefaultArgLoc = NewDefaultLoc;
600 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
601 // Merge the default argument from the old declaration to the
602 // new declaration.
603 SawDefaultArgument = true;
604 // FIXME: We need to create a new kind of "default argument"
605 // expression that points to a previous template template
606 // parameter.
607 NewTemplateParm->setDefaultArgument(
608 OldTemplateParm->getDefaultArgument());
609 PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
610 } else if (NewTemplateParm->hasDefaultArgument()) {
611 SawDefaultArgument = true;
612 PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
613 } else if (SawDefaultArgument)
614 MissingDefaultArg = true;
615 }
616
617 if (RedundantDefaultArg) {
618 // C++ [temp.param]p12:
619 // A template-parameter shall not be given default arguments
620 // by two different declarations in the same scope.
621 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
622 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
623 Invalid = true;
624 } else if (MissingDefaultArg) {
625 // C++ [temp.param]p11:
626 // If a template-parameter has a default template-argument,
627 // all subsequent template-parameters shall have a default
628 // template-argument supplied.
629 Diag((*NewParam)->getLocation(),
630 diag::err_template_param_default_arg_missing);
631 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
632 Invalid = true;
633 }
634
635 // If we have an old template parameter list that we're merging
636 // in, move on to the next parameter.
637 if (OldParams)
638 ++OldParam;
639 }
640
641 return Invalid;
642}
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000643
Douglas Gregor8e458f42009-02-09 18:46:07 +0000644Action::TypeTy *
645Sema::ActOnClassTemplateSpecialization(DeclTy *TemplateD,
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000646 SourceLocation TemplateLoc,
Douglas Gregor8e458f42009-02-09 18:46:07 +0000647 SourceLocation LAngleLoc,
Douglas Gregor6f37b582009-02-09 19:34:22 +0000648 ASTTemplateArgsPtr TemplateArgs,
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000649 SourceLocation *TemplateArgLocs,
Douglas Gregor8e458f42009-02-09 18:46:07 +0000650 SourceLocation RAngleLoc,
651 const CXXScopeSpec *SS) {
652 TemplateDecl *Template = cast<TemplateDecl>(static_cast<Decl *>(TemplateD));
653
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000654 // Check that the template argument list is well-formed for this
655 // template.
656 if (!CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
657 TemplateArgs, TemplateArgLocs, RAngleLoc))
658 return 0;
659
Douglas Gregor8e458f42009-02-09 18:46:07 +0000660 // Yes, all class template specializations are just silly sugar for
661 // 'int'. Gotta problem wit dat?
Douglas Gregor6f37b582009-02-09 19:34:22 +0000662 QualType Result
663 = Context.getClassTemplateSpecializationType(Template,
664 TemplateArgs.size(),
665 reinterpret_cast<uintptr_t *>(TemplateArgs.getArgs()),
666 TemplateArgs.getArgIsType(),
667 Context.IntTy);
668 TemplateArgs.release();
669 return Result.getAsOpaquePtr();
Douglas Gregor8e458f42009-02-09 18:46:07 +0000670}
671
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000672/// \brief Check that the given template argument list is well-formed
673/// for specializing the given template.
674bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
675 SourceLocation TemplateLoc,
676 SourceLocation LAngleLoc,
677 ASTTemplateArgsPtr& Args,
678 SourceLocation *TemplateArgLocs,
679 SourceLocation RAngleLoc) {
680 TemplateParameterList *Params = Template->getTemplateParameters();
681 unsigned NumParams = Params->size();
682 unsigned NumArgs = Args.size();
683 bool Invalid = false;
684
685 if (NumArgs > NumParams ||
686 NumArgs < NumParams /*FIXME: default arguments! */) {
687 // FIXME: point at either the first arg beyond what we can handle,
688 // or the '>', depending on whether we have too many or too few
689 // arguments.
690 SourceRange Range;
691 if (NumArgs > NumParams)
692 Range = SourceRange(TemplateArgLocs[NumParams], RAngleLoc);
693 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
694 << (NumArgs > NumParams)
695 << (isa<ClassTemplateDecl>(Template)? 0 :
696 isa<FunctionTemplateDecl>(Template)? 1 :
697 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
698 << Template << Range;
699
700 Invalid = true;
701 }
702
703 // C++ [temp.arg]p1:
704 // [...] The type and form of each template-argument specified in
705 // a template-id shall match the type and form specified for the
706 // corresponding parameter declared by the template in its
707 // template-parameter-list.
708 unsigned ArgIdx = 0;
709 for (TemplateParameterList::iterator Param = Params->begin(),
710 ParamEnd = Params->end();
711 Param != ParamEnd; ++Param, ++ArgIdx) {
712 // Decode the template argument
713 QualType ArgType;
714 Expr *ArgExpr = 0;
715 SourceLocation ArgLoc;
716 if (ArgIdx >= NumArgs) {
717 // FIXME: Get the default argument here, which might
718 // (eventually) require instantiation.
719 break;
720 } else
721 ArgLoc = TemplateArgLocs[ArgIdx];
722
723 if (Args.getArgIsType()[ArgIdx])
724 ArgType = QualType::getFromOpaquePtr(Args.getArgs()[ArgIdx]);
725 else
726 ArgExpr = reinterpret_cast<Expr *>(Args.getArgs()[ArgIdx]);
727
728 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
729 // Check template type parameters.
730 if (!ArgType.isNull()) {
731 if (!CheckTemplateArgument(TTP, ArgType, ArgLoc))
732 Invalid = true;
733 continue;
734 }
735
736 // C++ [temp.arg.type]p1:
737 // A template-argument for a template-parameter which is a
738 // type shall be a type-id.
739
740 // We have a template type parameter but the template argument
741 // is an expression.
742 Diag(ArgExpr->getSourceRange().getBegin(),
743 diag::err_template_arg_must_be_type);
Douglas Gregor341ac792009-02-10 00:53:15 +0000744 Diag((*Param)->getLocation(), diag::note_template_param_here);
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000745 Invalid = true;
746 } else if (NonTypeTemplateParmDecl *NTTP
747 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
748 // Check non-type template parameters.
749 if (ArgExpr) {
750 if (!CheckTemplateArgument(NTTP, ArgExpr))
751 Invalid = true;
752 continue;
753 }
754
755 // We have a non-type template parameter but the template
756 // argument is a type.
757
758 // C++ [temp.arg]p2:
759 // In a template-argument, an ambiguity between a type-id and
760 // an expression is resolved to a type-id, regardless of the
761 // form of the corresponding template-parameter.
762 //
763 // We warn specifically about this case, since it can be rather
764 // confusing for users.
765 if (ArgType->isFunctionType())
766 Diag(ArgLoc, diag::err_template_arg_nontype_ambig)
767 << ArgType;
768 else
769 Diag(ArgLoc, diag::err_template_arg_must_be_expr);
Douglas Gregor341ac792009-02-10 00:53:15 +0000770 Diag((*Param)->getLocation(), diag::note_template_param_here);
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000771 Invalid = true;
772 } else {
773 // Check template template parameters.
774 TemplateTemplateParmDecl *TempParm
775 = cast<TemplateTemplateParmDecl>(*Param);
776
777 if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
778 isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
779 if (!CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
780 Invalid = true;
781 continue;
782 }
783
784 // We have a template template parameter but the template
785 // argument does not refer to a template.
786 Diag(ArgLoc, diag::err_template_arg_must_be_template);
787 Invalid = true;
788 }
789 }
790
791 return Invalid;
792}
793
794/// \brief Check a template argument against its corresponding
795/// template type parameter.
796///
797/// This routine implements the semantics of C++ [temp.arg.type]. It
798/// returns true if an error occurred, and false otherwise.
799bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
800 QualType Arg, SourceLocation ArgLoc) {
801 // C++ [temp.arg.type]p2:
802 // A local type, a type with no linkage, an unnamed type or a type
803 // compounded from any of these types shall not be used as a
804 // template-argument for a template type-parameter.
805 //
806 // FIXME: Perform the recursive and no-linkage type checks.
807 const TagType *Tag = 0;
808 if (const EnumType *EnumT = Arg->getAsEnumType())
809 Tag = EnumT;
810 else if (const RecordType *RecordT = Arg->getAsRecordType())
811 Tag = RecordT;
812 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
813 return Diag(ArgLoc, diag::err_template_arg_local_type)
814 << QualType(Tag, 0);
815 else if (Tag && !Tag->getDecl()->getDeclName()) {
816 Diag(ArgLoc, diag::err_template_arg_unnamed_type);
817 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
818 return true;
819 }
820
821 return false;
822}
823
824/// \brief Check a template argument against its corresponding
825/// non-type template parameter.
826///
827/// This routine implements the semantics of C++ [temp.arg.nontype].
828/// It returns true if an error occurred, and false otherwise.
829bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
830 Expr *Arg) {
831 return false;
832}
833
834/// \brief Check a template argument against its corresponding
835/// template template parameter.
836///
837/// This routine implements the semantics of C++ [temp.arg.template].
838/// It returns true if an error occurred, and false otherwise.
839bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
840 DeclRefExpr *Arg) {
Douglas Gregore8e367f2009-02-10 00:24:35 +0000841 assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
842 TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
843
844 // C++ [temp.arg.template]p1:
845 // A template-argument for a template template-parameter shall be
846 // the name of a class template, expressed as id-expression. Only
847 // primary class templates are considered when matching the
848 // template template argument with the corresponding parameter;
849 // partial specializations are not considered even if their
850 // parameter lists match that of the template template parameter.
851 if (!isa<ClassTemplateDecl>(Template)) {
852 assert(isa<FunctionTemplateDecl>(Template) &&
853 "Only function templates are possible here");
854 Diag(Arg->getSourceRange().getBegin(), diag::note_template_arg_refers_here)
855 << Template;
856 }
857
858 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
859 Param->getTemplateParameters(),
860 true, true,
861 Arg->getSourceRange().getBegin());
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000862}
863
Douglas Gregord406b032009-02-06 22:42:48 +0000864/// \brief Determine whether the given template parameter lists are
865/// equivalent.
866///
867/// \param New The new template parameter list, typically written in the
868/// source code as part of a new template declaration.
869///
870/// \param Old The old template parameter list, typically found via
871/// name lookup of the template declared with this template parameter
872/// list.
873///
874/// \param Complain If true, this routine will produce a diagnostic if
875/// the template parameter lists are not equivalent.
876///
Douglas Gregore8e367f2009-02-10 00:24:35 +0000877/// \param IsTemplateTemplateParm If true, this routine is being
878/// called to compare the template parameter lists of a template
879/// template parameter.
880///
881/// \param TemplateArgLoc If this source location is valid, then we
882/// are actually checking the template parameter list of a template
883/// argument (New) against the template parameter list of its
884/// corresponding template template parameter (Old). We produce
885/// slightly different diagnostics in this scenario.
886///
Douglas Gregord406b032009-02-06 22:42:48 +0000887/// \returns True if the template parameter lists are equal, false
888/// otherwise.
889bool
890Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
891 TemplateParameterList *Old,
892 bool Complain,
Douglas Gregore8e367f2009-02-10 00:24:35 +0000893 bool IsTemplateTemplateParm,
894 SourceLocation TemplateArgLoc) {
Douglas Gregord406b032009-02-06 22:42:48 +0000895 if (Old->size() != New->size()) {
896 if (Complain) {
Douglas Gregore8e367f2009-02-10 00:24:35 +0000897 unsigned NextDiag = diag::err_template_param_list_different_arity;
898 if (TemplateArgLoc.isValid()) {
899 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
900 NextDiag = diag::note_template_param_list_different_arity;
901 }
902 Diag(New->getTemplateLoc(), NextDiag)
903 << (New->size() > Old->size())
904 << IsTemplateTemplateParm
905 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregord406b032009-02-06 22:42:48 +0000906 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
907 << IsTemplateTemplateParm
908 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
909 }
910
911 return false;
912 }
913
914 for (TemplateParameterList::iterator OldParm = Old->begin(),
915 OldParmEnd = Old->end(), NewParm = New->begin();
916 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
917 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregore8e367f2009-02-10 00:24:35 +0000918 unsigned NextDiag = diag::err_template_param_different_kind;
919 if (TemplateArgLoc.isValid()) {
920 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
921 NextDiag = diag::note_template_param_different_kind;
922 }
923 Diag((*NewParm)->getLocation(), NextDiag)
Douglas Gregord406b032009-02-06 22:42:48 +0000924 << IsTemplateTemplateParm;
925 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
926 << IsTemplateTemplateParm;
927 return false;
928 }
929
930 if (isa<TemplateTypeParmDecl>(*OldParm)) {
931 // Okay; all template type parameters are equivalent (since we
Douglas Gregore8e367f2009-02-10 00:24:35 +0000932 // know we're at the same index).
933#if 0
934 // FIXME: Enable this code in debug mode *after* we properly go
935 // through and "instantiate" the template parameter lists of
936 // template template parameters. It's only after this
937 // instantiation that (1) any dependent types within the
938 // template parameter list of the template template parameter
939 // can be checked, and (2) the template type parameter depths
940 // will match up.
Douglas Gregord406b032009-02-06 22:42:48 +0000941 QualType OldParmType
942 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
943 QualType NewParmType
944 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
945 assert(Context.getCanonicalType(OldParmType) ==
946 Context.getCanonicalType(NewParmType) &&
947 "type parameter mismatch?");
948#endif
949 } else if (NonTypeTemplateParmDecl *OldNTTP
950 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
951 // The types of non-type template parameters must agree.
952 NonTypeTemplateParmDecl *NewNTTP
953 = cast<NonTypeTemplateParmDecl>(*NewParm);
954 if (Context.getCanonicalType(OldNTTP->getType()) !=
955 Context.getCanonicalType(NewNTTP->getType())) {
956 if (Complain) {
Douglas Gregore8e367f2009-02-10 00:24:35 +0000957 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
958 if (TemplateArgLoc.isValid()) {
959 Diag(TemplateArgLoc,
960 diag::err_template_arg_template_params_mismatch);
961 NextDiag = diag::note_template_nontype_parm_different_type;
962 }
963 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregord406b032009-02-06 22:42:48 +0000964 << NewNTTP->getType()
965 << IsTemplateTemplateParm;
966 Diag(OldNTTP->getLocation(),
967 diag::note_template_nontype_parm_prev_declaration)
968 << OldNTTP->getType();
969 }
970 return false;
971 }
972 } else {
973 // The template parameter lists of template template
974 // parameters must agree.
975 // FIXME: Could we perform a faster "type" comparison here?
976 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
977 "Only template template parameters handled here");
978 TemplateTemplateParmDecl *OldTTP
979 = cast<TemplateTemplateParmDecl>(*OldParm);
980 TemplateTemplateParmDecl *NewTTP
981 = cast<TemplateTemplateParmDecl>(*NewParm);
982 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
983 OldTTP->getTemplateParameters(),
984 Complain,
Douglas Gregore8e367f2009-02-10 00:24:35 +0000985 /*IsTemplateTemplateParm=*/true,
986 TemplateArgLoc))
Douglas Gregord406b032009-02-06 22:42:48 +0000987 return false;
988 }
989 }
990
991 return true;
992}
993
994/// \brief Check whether a template can be declared within this scope.
995///
996/// If the template declaration is valid in this scope, returns
997/// false. Otherwise, issues a diagnostic and returns true.
998bool
999Sema::CheckTemplateDeclScope(Scope *S,
1000 MultiTemplateParamsArg &TemplateParameterLists) {
1001 assert(TemplateParameterLists.size() > 0 && "Not a template");
1002
1003 // Find the nearest enclosing declaration scope.
1004 while ((S->getFlags() & Scope::DeclScope) == 0 ||
1005 (S->getFlags() & Scope::TemplateParamScope) != 0)
1006 S = S->getParent();
1007
1008 TemplateParameterList *TemplateParams =
1009 static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
1010 SourceLocation TemplateLoc = TemplateParams->getTemplateLoc();
1011 SourceRange TemplateRange
1012 = SourceRange(TemplateLoc, TemplateParams->getRAngleLoc());
1013
1014 // C++ [temp]p2:
1015 // A template-declaration can appear only as a namespace scope or
1016 // class scope declaration.
1017 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
1018 while (Ctx && isa<LinkageSpecDecl>(Ctx)) {
1019 if (cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
1020 return Diag(TemplateLoc, diag::err_template_linkage)
1021 << TemplateRange;
1022
1023 Ctx = Ctx->getParent();
1024 }
1025
1026 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
1027 return false;
1028
1029 return Diag(TemplateLoc, diag::err_template_outside_namespace_or_class_scope)
1030 << TemplateRange;
1031}