blob: 63c29de82b4c7d6ad4c0d1e8cc70fdf8d4a582fa [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
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.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
12#include "Sema.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000013#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000014#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000015#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000016#include "clang/AST/ExprCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000021
22using namespace clang;
23
Douglas Gregor2dd078a2009-09-02 22:59:36 +000024/// \brief Determine whether the declaration found is acceptable as the name
25/// of a template and, if so, return that template declaration. Otherwise,
26/// returns NULL.
27static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) {
28 if (!D)
29 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Douglas Gregor2dd078a2009-09-02 22:59:36 +000031 if (isa<TemplateDecl>(D))
32 return D;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
35 // C++ [temp.local]p1:
36 // Like normal (non-template) classes, class templates have an
37 // injected-class-name (Clause 9). The injected-class-name
38 // can be used with or without a template-argument-list. When
39 // it is used without a template-argument-list, it is
40 // equivalent to the injected-class-name followed by the
41 // template-parameters of the class template enclosed in
42 // <>. When it is used with a template-argument-list, it
43 // refers to the specified class template specialization,
44 // which could be the current specialization or another
45 // specialization.
46 if (Record->isInjectedClassName()) {
47 Record = cast<CXXRecordDecl>(Record->getCanonicalDecl());
48 if (Record->getDescribedClassTemplate())
49 return Record->getDescribedClassTemplate();
50
51 if (ClassTemplateSpecializationDecl *Spec
52 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
53 return Spec->getSpecializedTemplate();
54 }
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor2dd078a2009-09-02 22:59:36 +000056 return 0;
57 }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Douglas Gregor2dd078a2009-09-02 22:59:36 +000059 OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D);
60 if (!Ovl)
61 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregor2dd078a2009-09-02 22:59:36 +000063 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
64 FEnd = Ovl->function_end();
65 F != FEnd; ++F) {
66 if (FunctionTemplateDecl *FuncTmpl = dyn_cast<FunctionTemplateDecl>(*F)) {
67 // We've found a function template. Determine whether there are
68 // any other function templates we need to bundle together in an
69 // OverloadedFunctionDecl
70 for (++F; F != FEnd; ++F) {
71 if (isa<FunctionTemplateDecl>(*F))
72 break;
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor2dd078a2009-09-02 22:59:36 +000075 if (F != FEnd) {
76 // Build an overloaded function decl containing only the
77 // function templates in Ovl.
Mike Stump1eb44332009-09-09 15:08:12 +000078 OverloadedFunctionDecl *OvlTemplate
Douglas Gregor2dd078a2009-09-02 22:59:36 +000079 = OverloadedFunctionDecl::Create(Context,
80 Ovl->getDeclContext(),
81 Ovl->getDeclName());
82 OvlTemplate->addOverload(FuncTmpl);
83 OvlTemplate->addOverload(*F);
84 for (++F; F != FEnd; ++F) {
85 if (isa<FunctionTemplateDecl>(*F))
86 OvlTemplate->addOverload(*F);
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Douglas Gregor2dd078a2009-09-02 22:59:36 +000089 return OvlTemplate;
90 }
91
92 return FuncTmpl;
93 }
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Douglas Gregor2dd078a2009-09-02 22:59:36 +000096 return 0;
97}
98
99TemplateNameKind Sema::isTemplateName(Scope *S,
Mike Stump1eb44332009-09-09 15:08:12 +0000100 const IdentifierInfo &II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000101 SourceLocation IdLoc,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000102 const CXXScopeSpec *SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000103 TypeTy *ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000104 bool EnteringContext,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000105 TemplateTy &TemplateResult) {
106 // Determine where to perform name lookup
107 DeclContext *LookupCtx = 0;
108 bool isDependent = false;
109 if (ObjectTypePtr) {
110 // This nested-name-specifier occurs in a member access expression, e.g.,
111 // x->B::f, and we are looking into the type of the object.
Mike Stump1eb44332009-09-09 15:08:12 +0000112 assert((!SS || !SS->isSet()) &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000113 "ObjectType and scope specifier cannot coexist");
114 QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
115 LookupCtx = computeDeclContext(ObjectType);
116 isDependent = ObjectType->isDependentType();
117 } else if (SS && SS->isSet()) {
118 // This nested-name-specifier occurs after another nested-name-specifier,
119 // so long into the context associated with the prior nested-name-specifier.
120
121 LookupCtx = computeDeclContext(*SS, EnteringContext);
122 isDependent = isDependentScopeSpecifier(*SS);
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000125 LookupResult Found;
126 bool ObjectTypeSearchedInScope = false;
127 if (LookupCtx) {
128 // Perform "qualified" name lookup into the declaration context we
129 // computed, which is either the type of the base of a member access
Mike Stump1eb44332009-09-09 15:08:12 +0000130 // expression or the declaration context associated with a prior
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000131 // nested-name-specifier.
132
133 // The declaration context must be complete.
134 if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(*SS))
135 return TNK_Non_template;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000137 Found = LookupQualifiedName(LookupCtx, &II, LookupOrdinaryName);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000139 if (ObjectTypePtr && Found.getKind() == LookupResult::NotFound) {
140 // C++ [basic.lookup.classref]p1:
141 // In a class member access expression (5.2.5), if the . or -> token is
Mike Stump1eb44332009-09-09 15:08:12 +0000142 // immediately followed by an identifier followed by a <, the
143 // identifier must be looked up to determine whether the < is the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000144 // beginning of a template argument list (14.2) or a less-than operator.
Mike Stump1eb44332009-09-09 15:08:12 +0000145 // The identifier is first looked up in the class of the object
146 // expression. If the identifier is not found, it is then looked up in
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000147 // the context of the entire postfix-expression and shall name a class
148 // or function template.
149 //
150 // FIXME: When we're instantiating a template, do we actually have to
151 // look in the scope of the template? Seems fishy...
152 Found = LookupName(S, &II, LookupOrdinaryName);
153 ObjectTypeSearchedInScope = true;
154 }
155 } else if (isDependent) {
Mike Stump1eb44332009-09-09 15:08:12 +0000156 // We cannot look into a dependent object type or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000157 return TNK_Non_template;
158 } else {
159 // Perform unqualified name lookup in the current scope.
160 Found = LookupName(S, &II, LookupOrdinaryName);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Douglas Gregor495c35d2009-08-25 22:51:20 +0000163 // FIXME: Cope with ambiguous name-lookup results.
Mike Stump1eb44332009-09-09 15:08:12 +0000164 assert(!Found.isAmbiguous() &&
Douglas Gregor495c35d2009-08-25 22:51:20 +0000165 "Cannot handle template name-lookup ambiguities");
Douglas Gregor7532dc62009-03-30 22:58:21 +0000166
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 NamedDecl *Template = isAcceptableTemplateName(Context, Found);
168 if (!Template)
169 return TNK_Non_template;
170
171 if (ObjectTypePtr && !ObjectTypeSearchedInScope) {
172 // C++ [basic.lookup.classref]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000173 // [...] If the lookup in the class of the object expression finds a
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000174 // template, the name is also looked up in the context of the entire
175 // postfix-expression and [...]
176 //
177 LookupResult FoundOuter = LookupName(S, &II, LookupOrdinaryName);
178 // FIXME: Handle ambiguities in this lookup better
179 NamedDecl *OuterTemplate = isAcceptableTemplateName(Context, FoundOuter);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000181 if (!OuterTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182 // - if the name is not found, the name found in the class of the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000183 // object expression is used, otherwise
184 } else if (!isa<ClassTemplateDecl>(OuterTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185 // - if the name is found in the context of the entire
186 // postfix-expression and does not name a class template, the name
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000187 // found in the class of the object expression is used, otherwise
188 } else {
189 // - if the name found is a class template, it must refer to the same
Mike Stump1eb44332009-09-09 15:08:12 +0000190 // entity as the one found in the class of the object expression,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000191 // otherwise the program is ill-formed.
192 if (OuterTemplate->getCanonicalDecl() != Template->getCanonicalDecl()) {
193 Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous)
194 << &II;
195 Diag(Template->getLocation(), diag::note_ambig_member_ref_object_type)
196 << QualType::getFromOpaquePtr(ObjectTypePtr);
197 Diag(OuterTemplate->getLocation(), diag::note_ambig_member_ref_scope);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
199 // Recover by taking the template that we found in the object
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000200 // expression's type.
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202 }
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000205 if (SS && SS->isSet() && !SS->isInvalid()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000206 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000207 = static_cast<NestedNameSpecifier *>(SS->getScopeRep());
Mike Stump1eb44332009-09-09 15:08:12 +0000208 if (OverloadedFunctionDecl *Ovl
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000209 = dyn_cast<OverloadedFunctionDecl>(Template))
Mike Stump1eb44332009-09-09 15:08:12 +0000210 TemplateResult
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000211 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
212 Ovl));
213 else
Mike Stump1eb44332009-09-09 15:08:12 +0000214 TemplateResult
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000215 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
Mike Stump1eb44332009-09-09 15:08:12 +0000216 cast<TemplateDecl>(Template)));
217 } else if (OverloadedFunctionDecl *Ovl
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000218 = dyn_cast<OverloadedFunctionDecl>(Template)) {
219 TemplateResult = TemplateTy::make(TemplateName(Ovl));
220 } else {
221 TemplateResult = TemplateTy::make(
222 TemplateName(cast<TemplateDecl>(Template)));
223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
225 if (isa<ClassTemplateDecl>(Template) ||
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000226 isa<TemplateTemplateParmDecl>(Template))
227 return TNK_Type_template;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
229 assert((isa<FunctionTemplateDecl>(Template) ||
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000230 isa<OverloadedFunctionDecl>(Template)) &&
231 "Unhandled template kind in Sema::isTemplateName");
232 return TNK_Function_template;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000233}
234
Douglas Gregor72c3f312008-12-05 18:15:24 +0000235/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
236/// that the template parameter 'PrevDecl' is being shadowed by a new
237/// declaration at location Loc. Returns true to indicate that this is
238/// an error, and false otherwise.
239bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000240 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000241
242 // Microsoft Visual C++ permits template parameters to be shadowed.
243 if (getLangOptions().Microsoft)
244 return false;
245
246 // C++ [temp.local]p4:
247 // A template-parameter shall not be redeclared within its
248 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000249 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000250 << cast<NamedDecl>(PrevDecl)->getDeclName();
251 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
252 return true;
253}
254
Douglas Gregor2943aed2009-03-03 04:44:36 +0000255/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000256/// the parameter D to reference the templated declaration and return a pointer
257/// to the template declaration. Otherwise, do nothing to D and return null.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000258TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) {
259 if (TemplateDecl *Temp = dyn_cast<TemplateDecl>(D.getAs<Decl>())) {
260 D = DeclPtrTy::make(Temp->getTemplatedDecl());
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000261 return Temp;
262 }
263 return 0;
264}
265
Douglas Gregor72c3f312008-12-05 18:15:24 +0000266/// ActOnTypeParameter - Called when a C++ template type parameter
267/// (e.g., "typename T") has been parsed. Typename specifies whether
268/// the keyword "typename" was used to declare the type parameter
269/// (otherwise, "class" was used), and KeyLoc is the location of the
270/// "class" or "typename" keyword. ParamName is the name of the
271/// parameter (NULL indicates an unnamed template parameter) and
Mike Stump1eb44332009-09-09 15:08:12 +0000272/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000273/// If the type parameter has a default argument, it will be added
274/// later via ActOnTypeParameterDefault.
Mike Stump1eb44332009-09-09 15:08:12 +0000275Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000276 SourceLocation EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000277 SourceLocation KeyLoc,
278 IdentifierInfo *ParamName,
279 SourceLocation ParamNameLoc,
280 unsigned Depth, unsigned Position) {
Mike Stump1eb44332009-09-09 15:08:12 +0000281 assert(S->isTemplateParamScope() &&
282 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000283 bool Invalid = false;
284
285 if (ParamName) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000286 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000287 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000288 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000289 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000290 }
291
Douglas Gregorddc29e12009-02-06 22:42:48 +0000292 SourceLocation Loc = ParamNameLoc;
293 if (!ParamName)
294 Loc = KeyLoc;
295
Douglas Gregor72c3f312008-12-05 18:15:24 +0000296 TemplateTypeParmDecl *Param
Mike Stump1eb44332009-09-09 15:08:12 +0000297 = TemplateTypeParmDecl::Create(Context, CurContext, Loc,
298 Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000299 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000300 if (Invalid)
301 Param->setInvalidDecl();
302
303 if (ParamName) {
304 // Add the template parameter into the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000305 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor72c3f312008-12-05 18:15:24 +0000306 IdResolver.AddDecl(Param);
307 }
308
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 return DeclPtrTy::make(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000310}
311
Douglas Gregord684b002009-02-10 19:49:53 +0000312/// ActOnTypeParameterDefault - Adds a default argument (the type
Mike Stump1eb44332009-09-09 15:08:12 +0000313/// Default) to the given template type parameter (TypeParam).
314void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
Douglas Gregord684b002009-02-10 19:49:53 +0000315 SourceLocation EqualLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000316 SourceLocation DefaultLoc,
Douglas Gregord684b002009-02-10 19:49:53 +0000317 TypeTy *DefaultT) {
Mike Stump1eb44332009-09-09 15:08:12 +0000318 TemplateTypeParmDecl *Parm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319 = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000320 // FIXME: Preserve type source info.
321 QualType Default = GetTypeFromParser(DefaultT);
Douglas Gregord684b002009-02-10 19:49:53 +0000322
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000323 // C++0x [temp.param]p9:
324 // A default template-argument may be specified for any kind of
Mike Stump1eb44332009-09-09 15:08:12 +0000325 // template-parameter that is not a template parameter pack.
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000326 if (Parm->isParameterPack()) {
327 Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000328 return;
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregord684b002009-02-10 19:49:53 +0000331 // C++ [temp.param]p14:
332 // A template-parameter shall not be used in its own default argument.
333 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Douglas Gregord684b002009-02-10 19:49:53 +0000335 // Check the template argument itself.
336 if (CheckTemplateArgument(Parm, Default, DefaultLoc)) {
337 Parm->setInvalidDecl();
338 return;
339 }
340
341 Parm->setDefaultArgument(Default, DefaultLoc, false);
342}
343
Douglas Gregor2943aed2009-03-03 04:44:36 +0000344/// \brief Check that the type of a non-type template parameter is
345/// well-formed.
346///
347/// \returns the (possibly-promoted) parameter type if valid;
348/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000349QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000350Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
351 // C++ [temp.param]p4:
352 //
353 // A non-type template-parameter shall have one of the following
354 // (optionally cv-qualified) types:
355 //
356 // -- integral or enumeration type,
357 if (T->isIntegralType() || T->isEnumeralType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000358 // -- pointer to object or pointer to function,
359 (T->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +0000360 (T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
361 T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000362 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000363 T->isReferenceType() ||
364 // -- pointer to member.
365 T->isMemberPointerType() ||
366 // If T is a dependent type, we can't do the check now, so we
367 // assume that it is well-formed.
368 T->isDependentType())
369 return T;
370 // C++ [temp.param]p8:
371 //
372 // A non-type template-parameter of type "array of T" or
373 // "function returning T" is adjusted to be of type "pointer to
374 // T" or "pointer to function returning T", respectively.
375 else if (T->isArrayType())
376 // FIXME: Keep the type prior to promotion?
377 return Context.getArrayDecayedType(T);
378 else if (T->isFunctionType())
379 // FIXME: Keep the type prior to promotion?
380 return Context.getPointerType(T);
381
382 Diag(Loc, diag::err_template_nontype_parm_bad_type)
383 << T;
384
385 return QualType();
386}
387
Douglas Gregor72c3f312008-12-05 18:15:24 +0000388/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
389/// template parameter (e.g., "int Size" in "template<int Size>
390/// class Array") has been parsed. S is the current scope and D is
391/// the parsed declarator.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000392Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
Mike Stump1eb44332009-09-09 15:08:12 +0000393 unsigned Depth,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000394 unsigned Position) {
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000395 DeclaratorInfo *DInfo = 0;
396 QualType T = GetTypeForDeclarator(D, S, &DInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000397
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000398 assert(S->isTemplateParamScope() &&
399 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000400 bool Invalid = false;
401
402 IdentifierInfo *ParamName = D.getIdentifier();
403 if (ParamName) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000404 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000405 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000406 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000407 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000408 }
409
Douglas Gregor2943aed2009-03-03 04:44:36 +0000410 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
Douglas Gregorceef30c2009-03-09 16:46:39 +0000411 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000412 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000413 Invalid = true;
414 }
Douglas Gregor5d290d52009-02-10 17:43:50 +0000415
Douglas Gregor72c3f312008-12-05 18:15:24 +0000416 NonTypeTemplateParmDecl *Param
417 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000418 Depth, Position, ParamName, T, DInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419 if (Invalid)
420 Param->setInvalidDecl();
421
422 if (D.getIdentifier()) {
423 // Add the template parameter into the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000424 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor72c3f312008-12-05 18:15:24 +0000425 IdResolver.AddDecl(Param);
426 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000427 return DeclPtrTy::make(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000428}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000429
Douglas Gregord684b002009-02-10 19:49:53 +0000430/// \brief Adds a default argument to the given non-type template
431/// parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000432void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregord684b002009-02-10 19:49:53 +0000433 SourceLocation EqualLoc,
434 ExprArg DefaultE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000435 NonTypeTemplateParmDecl *TemplateParm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000436 = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregord684b002009-02-10 19:49:53 +0000437 Expr *Default = static_cast<Expr *>(DefaultE.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Douglas Gregord684b002009-02-10 19:49:53 +0000439 // C++ [temp.param]p14:
440 // A template-parameter shall not be used in its own default argument.
441 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregord684b002009-02-10 19:49:53 +0000443 // Check the well-formedness of the default template argument.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000444 TemplateArgument Converted;
445 if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
446 Converted)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000447 TemplateParm->setInvalidDecl();
448 return;
449 }
450
Anders Carlssone9146f22009-05-01 19:49:17 +0000451 TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
Douglas Gregord684b002009-02-10 19:49:53 +0000452}
453
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000454
455/// ActOnTemplateTemplateParameter - Called when a C++ template template
456/// parameter (e.g. T in template <template <typename> class T> class array)
457/// has been parsed. S is the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000458Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S,
459 SourceLocation TmpLoc,
460 TemplateParamsTy *Params,
461 IdentifierInfo *Name,
462 SourceLocation NameLoc,
463 unsigned Depth,
Mike Stump1eb44332009-09-09 15:08:12 +0000464 unsigned Position) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000465 assert(S->isTemplateParamScope() &&
466 "Template template parameter not in template parameter scope!");
467
468 // Construct the parameter object.
469 TemplateTemplateParmDecl *Param =
470 TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth,
471 Position, Name,
472 (TemplateParameterList*)Params);
473
474 // Make sure the parameter is valid.
475 // FIXME: Decl object is not currently invalidated anywhere so this doesn't
476 // do anything yet. However, if the template parameter list or (eventual)
477 // default value is ever invalidated, that will propagate here.
478 bool Invalid = false;
479 if (Invalid) {
480 Param->setInvalidDecl();
481 }
482
483 // If the tt-param has a name, then link the identifier into the scope
484 // and lookup mechanisms.
485 if (Name) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000486 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000487 IdResolver.AddDecl(Param);
488 }
489
Chris Lattnerb28317a2009-03-28 19:18:32 +0000490 return DeclPtrTy::make(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000491}
492
Douglas Gregord684b002009-02-10 19:49:53 +0000493/// \brief Adds a default argument to the given template template
494/// parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000495void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregord684b002009-02-10 19:49:53 +0000496 SourceLocation EqualLoc,
497 ExprArg DefaultE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000498 TemplateTemplateParmDecl *TemplateParm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000499 = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregord684b002009-02-10 19:49:53 +0000500
501 // Since a template-template parameter's default argument is an
502 // id-expression, it must be a DeclRefExpr.
Mike Stump1eb44332009-09-09 15:08:12 +0000503 DeclRefExpr *Default
Douglas Gregord684b002009-02-10 19:49:53 +0000504 = cast<DeclRefExpr>(static_cast<Expr *>(DefaultE.get()));
505
506 // C++ [temp.param]p14:
507 // A template-parameter shall not be used in its own default argument.
508 // FIXME: Implement this check! Needs a recursive walk over the types.
509
510 // Check the well-formedness of the template argument.
511 if (!isa<TemplateDecl>(Default->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000512 Diag(Default->getSourceRange().getBegin(),
Douglas Gregord684b002009-02-10 19:49:53 +0000513 diag::err_template_arg_must_be_template)
514 << Default->getSourceRange();
515 TemplateParm->setInvalidDecl();
516 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000517 }
Douglas Gregord684b002009-02-10 19:49:53 +0000518 if (CheckTemplateArgument(TemplateParm, Default)) {
519 TemplateParm->setInvalidDecl();
520 return;
521 }
522
523 DefaultE.release();
524 TemplateParm->setDefaultArgument(Default);
525}
526
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000527/// ActOnTemplateParameterList - Builds a TemplateParameterList that
528/// contains the template parameters in Params/NumParams.
529Sema::TemplateParamsTy *
530Sema::ActOnTemplateParameterList(unsigned Depth,
531 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000532 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000533 SourceLocation LAngleLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000534 DeclPtrTy *Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000535 SourceLocation RAngleLoc) {
536 if (ExportLoc.isValid())
537 Diag(ExportLoc, diag::note_template_export_unsupported);
538
Douglas Gregorddc29e12009-02-06 22:42:48 +0000539 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
540 (Decl**)Params, NumParams, RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000541}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000542
Douglas Gregor212e81c2009-03-25 00:13:59 +0000543Sema::DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000544Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000545 SourceLocation KWLoc, const CXXScopeSpec &SS,
546 IdentifierInfo *Name, SourceLocation NameLoc,
547 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000548 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000549 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000551 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000552 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000553 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000554
555 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000556 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000557 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000558
559 TagDecl::TagKind Kind;
560 switch (TagSpec) {
561 default: assert(0 && "Unknown tag type!");
562 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
563 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
564 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
565 }
566
567 // There is no such thing as an unnamed class template.
568 if (!Name) {
569 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000570 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000571 }
572
573 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000574 DeclContext *SemanticContext;
575 LookupResult Previous;
576 if (SS.isNotEmpty() && !SS.isInvalid()) {
577 SemanticContext = computeDeclContext(SS, true);
578 if (!SemanticContext) {
579 // FIXME: Produce a reasonable diagnostic here
580 return true;
581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 Previous = LookupQualifiedName(SemanticContext, Name, LookupOrdinaryName,
Douglas Gregor05396e22009-08-25 17:23:04 +0000584 true);
585 } else {
586 SemanticContext = CurContext;
587 Previous = LookupName(S, Name, LookupOrdinaryName, true);
588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Douglas Gregorddc29e12009-02-06 22:42:48 +0000590 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
591 NamedDecl *PrevDecl = 0;
592 if (Previous.begin() != Previous.end())
593 PrevDecl = *Previous.begin();
594
Douglas Gregor05396e22009-08-25 17:23:04 +0000595 if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
Douglas Gregorc19ee3e2009-06-17 23:37:01 +0000596 PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregorddc29e12009-02-06 22:42:48 +0000598 // If there is a previous declaration with the same name, check
599 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000600 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000601 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
602 if (PrevClassTemplate) {
603 // Ensure that the template parameter lists are compatible.
604 if (!TemplateParameterListsAreEqual(TemplateParams,
605 PrevClassTemplate->getTemplateParameters(),
606 /*Complain=*/true))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000607 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000608
609 // C++ [temp.class]p4:
610 // In a redeclaration, partial specialization, explicit
611 // specialization or explicit instantiation of a class template,
612 // the class-key shall agree in kind with the original class
613 // template declaration (7.1.5.3).
614 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000615 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000616 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000617 << Name
Mike Stump1eb44332009-09-09 15:08:12 +0000618 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +0000619 PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000620 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000621 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000622 }
623
Douglas Gregorddc29e12009-02-06 22:42:48 +0000624 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000625 if (TUK == TUK_Definition) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000626 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
627 Diag(NameLoc, diag::err_redefinition) << Name;
628 Diag(Def->getLocation(), diag::note_previous_definition);
629 // FIXME: Would it make sense to try to "forget" the previous
630 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000631 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000632 }
633 }
634 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
635 // Maybe we will complain about the shadowed template parameter.
636 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
637 // Just pretend that we didn't see the previous declaration.
638 PrevDecl = 0;
639 } else if (PrevDecl) {
640 // C++ [temp]p5:
641 // A class template shall not have the same name as any other
642 // template, class, function, object, enumeration, enumerator,
643 // namespace, or type in the same scope (3.3), except as specified
644 // in (14.5.4).
645 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
646 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000647 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000648 }
649
Douglas Gregord684b002009-02-10 19:49:53 +0000650 // Check the template parameter list of this declaration, possibly
651 // merging in the template parameter list from the previous class
652 // template declaration.
653 if (CheckTemplateParameterList(TemplateParams,
654 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
655 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregor7da97d02009-05-10 22:57:19 +0000657 // FIXME: If we had a scope specifier, we better have a previous template
Douglas Gregorddc29e12009-02-06 22:42:48 +0000658 // declaration!
659
Mike Stump1eb44332009-09-09 15:08:12 +0000660 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000661 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000662 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000663 PrevClassTemplate->getTemplatedDecl() : 0,
664 /*DelayTypeCreation=*/true);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000665
666 ClassTemplateDecl *NewTemplate
667 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
668 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000669 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000670 NewClass->setDescribedClassTemplate(NewTemplate);
671
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000672 // Build the type for the class template declaration now.
Mike Stump1eb44332009-09-09 15:08:12 +0000673 QualType T =
674 Context.getTypeDeclType(NewClass,
675 PrevClassTemplate?
676 PrevClassTemplate->getTemplatedDecl() : 0);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000677 assert(T->isDependentType() && "Class template type is not dependent?");
678 (void)T;
679
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000680 // Set the access specifier.
681 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Douglas Gregorddc29e12009-02-06 22:42:48 +0000683 // Set the lexical context of these templates
684 NewClass->setLexicalDeclContext(CurContext);
685 NewTemplate->setLexicalDeclContext(CurContext);
686
John McCall0f434ec2009-07-31 02:45:11 +0000687 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000688 NewClass->startDefinition();
689
690 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000691 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000692
693 PushOnScopeChains(NewTemplate, S);
694
Douglas Gregord684b002009-02-10 19:49:53 +0000695 if (Invalid) {
696 NewTemplate->setInvalidDecl();
697 NewClass->setInvalidDecl();
698 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000699 return DeclPtrTy::make(NewTemplate);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000700}
701
Douglas Gregord684b002009-02-10 19:49:53 +0000702/// \brief Checks the validity of a template parameter list, possibly
703/// considering the template parameter list from a previous
704/// declaration.
705///
706/// If an "old" template parameter list is provided, it must be
707/// equivalent (per TemplateParameterListsAreEqual) to the "new"
708/// template parameter list.
709///
710/// \param NewParams Template parameter list for a new template
711/// declaration. This template parameter list will be updated with any
712/// default arguments that are carried through from the previous
713/// template parameter list.
714///
715/// \param OldParams If provided, template parameter list from a
716/// previous declaration of the same template. Default template
717/// arguments will be merged from the old template parameter list to
718/// the new template parameter list.
719///
720/// \returns true if an error occurred, false otherwise.
721bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
722 TemplateParameterList *OldParams) {
723 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregord684b002009-02-10 19:49:53 +0000725 // C++ [temp.param]p10:
726 // The set of default template-arguments available for use with a
727 // template declaration or definition is obtained by merging the
728 // default arguments from the definition (if in scope) and all
729 // declarations in scope in the same way default function
730 // arguments are (8.3.6).
731 bool SawDefaultArgument = false;
732 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000733
Anders Carlsson49d25572009-06-12 23:20:15 +0000734 bool SawParameterPack = false;
735 SourceLocation ParameterPackLoc;
736
Mike Stump1a35fde2009-02-11 23:03:27 +0000737 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +0000738 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +0000739 if (OldParams)
740 OldParam = OldParams->begin();
741
742 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
743 NewParamEnd = NewParams->end();
744 NewParam != NewParamEnd; ++NewParam) {
745 // Variables used to diagnose redundant default arguments
746 bool RedundantDefaultArg = false;
747 SourceLocation OldDefaultLoc;
748 SourceLocation NewDefaultLoc;
749
750 // Variables used to diagnose missing default arguments
751 bool MissingDefaultArg = false;
752
Anders Carlsson49d25572009-06-12 23:20:15 +0000753 // C++0x [temp.param]p11:
754 // If a template parameter of a class template is a template parameter pack,
755 // it must be the last template parameter.
756 if (SawParameterPack) {
Mike Stump1eb44332009-09-09 15:08:12 +0000757 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +0000758 diag::err_template_param_pack_must_be_last_template_parameter);
759 Invalid = true;
760 }
761
Douglas Gregord684b002009-02-10 19:49:53 +0000762 // Merge default arguments for template type parameters.
763 if (TemplateTypeParmDecl *NewTypeParm
764 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000765 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +0000766 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Anders Carlsson49d25572009-06-12 23:20:15 +0000768 if (NewTypeParm->isParameterPack()) {
769 assert(!NewTypeParm->hasDefaultArgument() &&
770 "Parameter packs can't have a default argument!");
771 SawParameterPack = true;
772 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000773 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000774 NewTypeParm->hasDefaultArgument()) {
775 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
776 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
777 SawDefaultArgument = true;
778 RedundantDefaultArg = true;
779 PreviousDefaultArgLoc = NewDefaultLoc;
780 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
781 // Merge the default argument from the old declaration to the
782 // new declaration.
783 SawDefaultArgument = true;
784 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
785 OldTypeParm->getDefaultArgumentLoc(),
786 true);
787 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
788 } else if (NewTypeParm->hasDefaultArgument()) {
789 SawDefaultArgument = true;
790 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
791 } else if (SawDefaultArgument)
792 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000793 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +0000794 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000795 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +0000796 NonTypeTemplateParmDecl *OldNonTypeParm
797 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000798 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000799 NewNonTypeParm->hasDefaultArgument()) {
800 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
801 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
802 SawDefaultArgument = true;
803 RedundantDefaultArg = true;
804 PreviousDefaultArgLoc = NewDefaultLoc;
805 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
806 // Merge the default argument from the old declaration to the
807 // new declaration.
808 SawDefaultArgument = true;
809 // FIXME: We need to create a new kind of "default argument"
810 // expression that points to a previous template template
811 // parameter.
812 NewNonTypeParm->setDefaultArgument(
813 OldNonTypeParm->getDefaultArgument());
814 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
815 } else if (NewNonTypeParm->hasDefaultArgument()) {
816 SawDefaultArgument = true;
817 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
818 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +0000819 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000820 } else {
Douglas Gregord684b002009-02-10 19:49:53 +0000821 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +0000822 TemplateTemplateParmDecl *NewTemplateParm
823 = cast<TemplateTemplateParmDecl>(*NewParam);
824 TemplateTemplateParmDecl *OldTemplateParm
825 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000826 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000827 NewTemplateParm->hasDefaultArgument()) {
828 OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
829 NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
830 SawDefaultArgument = true;
831 RedundantDefaultArg = true;
832 PreviousDefaultArgLoc = NewDefaultLoc;
833 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
834 // Merge the default argument from the old declaration to the
835 // new declaration.
836 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000837 // FIXME: We need to create a new kind of "default argument" expression
838 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +0000839 NewTemplateParm->setDefaultArgument(
840 OldTemplateParm->getDefaultArgument());
841 PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
842 } else if (NewTemplateParm->hasDefaultArgument()) {
843 SawDefaultArgument = true;
844 PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
845 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +0000846 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +0000847 }
848
849 if (RedundantDefaultArg) {
850 // C++ [temp.param]p12:
851 // A template-parameter shall not be given default arguments
852 // by two different declarations in the same scope.
853 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
854 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
855 Invalid = true;
856 } else if (MissingDefaultArg) {
857 // C++ [temp.param]p11:
858 // If a template-parameter has a default template-argument,
859 // all subsequent template-parameters shall have a default
860 // template-argument supplied.
Mike Stump1eb44332009-09-09 15:08:12 +0000861 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +0000862 diag::err_template_param_default_arg_missing);
863 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
864 Invalid = true;
865 }
866
867 // If we have an old template parameter list that we're merging
868 // in, move on to the next parameter.
869 if (OldParams)
870 ++OldParam;
871 }
872
873 return Invalid;
874}
Douglas Gregorc15cb382009-02-09 23:23:08 +0000875
Mike Stump1eb44332009-09-09 15:08:12 +0000876/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000877/// specifier, returning the template parameter list that applies to the
878/// name.
879///
880/// \param DeclStartLoc the start of the declaration that has a scope
881/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +0000882///
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000883/// \param SS the scope specifier that will be matched to the given template
884/// parameter lists. This scope specifier precedes a qualified name that is
885/// being declared.
886///
887/// \param ParamLists the template parameter lists, from the outermost to the
888/// innermost template parameter lists.
889///
890/// \param NumParamLists the number of template parameter lists in ParamLists.
891///
Mike Stump1eb44332009-09-09 15:08:12 +0000892/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000893/// name that is preceded by the scope specifier @p SS. This template
894/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +0000895/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000896/// template specialization), or may be NULL (if we were's declaring isn't
897/// itself a template).
898TemplateParameterList *
899Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
900 const CXXScopeSpec &SS,
901 TemplateParameterList **ParamLists,
902 unsigned NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000903 // Find the template-ids that occur within the nested-name-specifier. These
904 // template-ids will match up with the template parameter lists.
905 llvm::SmallVector<const TemplateSpecializationType *, 4>
906 TemplateIdsInSpecifier;
907 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
908 NNS; NNS = NNS->getPrefix()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909 if (const TemplateSpecializationType *SpecType
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000910 = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) {
911 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
912 if (!Template)
913 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek6217b802009-07-29 21:53:49 +0000915 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000916 ClassTemplateSpecializationDecl *SpecDecl
917 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
918 // If the nested name specifier refers to an explicit specialization,
919 // we don't need a template<> header.
Mike Stump1eb44332009-09-09 15:08:12 +0000920 // FIXME: revisit this approach once we cope with specialization
Douglas Gregorb88e8882009-07-30 17:40:51 +0000921 // properly.
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000922 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization)
923 continue;
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000926 TemplateIdsInSpecifier.push_back(SpecType);
927 }
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000930 // Reverse the list of template-ids in the scope specifier, so that we can
931 // more easily match up the template-ids and the template parameter lists.
932 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000934 SourceLocation FirstTemplateLoc = DeclStartLoc;
935 if (NumParamLists)
936 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000938 // Match the template-ids found in the specifier to the template parameter
939 // lists.
940 unsigned Idx = 0;
941 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
942 Idx != NumTemplateIds; ++Idx) {
Douglas Gregorb88e8882009-07-30 17:40:51 +0000943 QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
944 bool DependentTemplateId = TemplateId->isDependentType();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000945 if (Idx >= NumParamLists) {
946 // We have a template-id without a corresponding template parameter
947 // list.
948 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +0000949 // FIXME: the location information here isn't great.
950 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000951 diag::err_template_spec_needs_template_parameters)
Douglas Gregorb88e8882009-07-30 17:40:51 +0000952 << TemplateId
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000953 << SS.getRange();
954 } else {
955 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
956 << SS.getRange()
957 << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
958 "template<> ");
959 }
960 return 0;
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000963 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +0000964 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +0000965 TemplateDecl *Template
Douglas Gregorb88e8882009-07-30 17:40:51 +0000966 = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
967
Mike Stump1eb44332009-09-09 15:08:12 +0000968 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorb88e8882009-07-30 17:40:51 +0000969 = dyn_cast<ClassTemplateDecl>(Template)) {
970 TemplateParameterList *ExpectedTemplateParams = 0;
971 // Is this template-id naming the primary template?
972 if (Context.hasSameType(TemplateId,
973 ClassTemplate->getInjectedClassNameType(Context)))
974 ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
975 // ... or a partial specialization?
976 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
977 = ClassTemplate->findPartialSpecialization(TemplateId))
978 ExpectedTemplateParams = PartialSpec->getTemplateParameters();
979
980 if (ExpectedTemplateParams)
Mike Stump1eb44332009-09-09 15:08:12 +0000981 TemplateParameterListsAreEqual(ParamLists[Idx],
Douglas Gregorb88e8882009-07-30 17:40:51 +0000982 ExpectedTemplateParams,
983 true);
Mike Stump1eb44332009-09-09 15:08:12 +0000984 }
Douglas Gregorb88e8882009-07-30 17:40:51 +0000985 } else if (ParamLists[Idx]->size() > 0)
Mike Stump1eb44332009-09-09 15:08:12 +0000986 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +0000987 diag::err_template_param_list_matches_nontemplate)
988 << TemplateId
989 << ParamLists[Idx]->getSourceRange();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000992 // If there were at least as many template-ids as there were template
993 // parameter lists, then there are no template parameter lists remaining for
994 // the declaration itself.
995 if (Idx >= NumParamLists)
996 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000998 // If there were too many template parameter lists, complain about that now.
999 if (Idx != NumParamLists - 1) {
1000 while (Idx < NumParamLists - 1) {
Mike Stump1eb44332009-09-09 15:08:12 +00001001 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001002 diag::err_template_spec_extra_headers)
1003 << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1004 ParamLists[Idx]->getRAngleLoc());
1005 ++Idx;
1006 }
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001009 // Return the last template parameter list, which corresponds to the
1010 // entity being declared.
1011 return ParamLists[NumParamLists - 1];
1012}
1013
Douglas Gregor40808ce2009-03-09 23:48:35 +00001014/// \brief Translates template arguments as provided by the parser
1015/// into template arguments used by semantic analysis.
Mike Stump1eb44332009-09-09 15:08:12 +00001016static void
1017translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001018 SourceLocation *TemplateArgLocs,
1019 llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) {
1020 TemplateArgs.reserve(TemplateArgsIn.size());
1021
1022 void **Args = TemplateArgsIn.getArgs();
1023 bool *ArgIsType = TemplateArgsIn.getArgIsType();
1024 for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) {
1025 TemplateArgs.push_back(
1026 ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg],
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001027 //FIXME: Preserve type source info.
1028 Sema::GetTypeFromParser(Args[Arg]))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001029 : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg])));
1030 }
1031}
1032
Douglas Gregor7532dc62009-03-30 22:58:21 +00001033QualType Sema::CheckTemplateIdType(TemplateName Name,
1034 SourceLocation TemplateLoc,
1035 SourceLocation LAngleLoc,
1036 const TemplateArgument *TemplateArgs,
1037 unsigned NumTemplateArgs,
1038 SourceLocation RAngleLoc) {
1039 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001040 if (!Template) {
1041 // The template name does not resolve to a template, so we just
1042 // build a dependent template-id type.
Douglas Gregorc45c2322009-03-31 00:43:58 +00001043 return Context.getTemplateSpecializationType(Name, TemplateArgs,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001044 NumTemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001045 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001046
Douglas Gregor40808ce2009-03-09 23:48:35 +00001047 // Check that the template argument list is well-formed for this
1048 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00001049 TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
1050 NumTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001051 if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001052 TemplateArgs, NumTemplateArgs, RAngleLoc,
Douglas Gregor16134c62009-07-01 00:28:38 +00001053 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001054 return QualType();
1055
Mike Stump1eb44332009-09-09 15:08:12 +00001056 assert((Converted.structuredSize() ==
Douglas Gregor7532dc62009-03-30 22:58:21 +00001057 Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058 "Converted template argument list is too short!");
1059
1060 QualType CanonType;
1061
Douglas Gregor7532dc62009-03-30 22:58:21 +00001062 if (TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor40808ce2009-03-09 23:48:35 +00001063 TemplateArgs,
1064 NumTemplateArgs)) {
1065 // This class template specialization is a dependent
1066 // type. Therefore, its canonical type is another class template
1067 // specialization type that contains all of the converted
1068 // arguments in canonical form. This ensures that, e.g., A<T> and
1069 // A<T, T> have identical types when A is declared as:
1070 //
1071 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001072 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001073 CanonType = Context.getTemplateSpecializationType(CanonName,
Anders Carlssonfb250522009-06-23 01:26:57 +00001074 Converted.getFlatArguments(),
1075 Converted.flatSize());
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregor1275ae02009-07-28 23:00:59 +00001077 // FIXME: CanonType is not actually the canonical type, and unfortunately
1078 // it is a TemplateTypeSpecializationType that we will never use again.
1079 // In the future, we need to teach getTemplateSpecializationType to only
1080 // build the canonical type and return that to us.
1081 CanonType = Context.getCanonicalType(CanonType);
Mike Stump1eb44332009-09-09 15:08:12 +00001082 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001083 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001084 // Find the class template specialization declaration that
1085 // corresponds to these arguments.
1086 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001087 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00001088 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00001089 Converted.flatSize(),
1090 Context);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001091 void *InsertPos = 0;
1092 ClassTemplateSpecializationDecl *Decl
1093 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1094 if (!Decl) {
1095 // This is the first time we have referenced this class template
1096 // specialization. Create the canonical declaration and add it to
1097 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001098 Decl = ClassTemplateSpecializationDecl::Create(Context,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001099 ClassTemplate->getDeclContext(),
1100 TemplateLoc,
1101 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00001102 Converted, 0);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001103 ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1104 Decl->setLexicalDeclContext(CurContext);
1105 }
1106
1107 CanonType = Context.getTypeDeclType(Decl);
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Douglas Gregor40808ce2009-03-09 23:48:35 +00001110 // Build the fully-sugared type for this class template
1111 // specialization, which refers back to the class template
1112 // specialization we created or found.
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001113 //FIXME: Preserve type source info.
Douglas Gregor7532dc62009-03-30 22:58:21 +00001114 return Context.getTemplateSpecializationType(Name, TemplateArgs,
1115 NumTemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001116}
1117
Douglas Gregorcc636682009-02-17 23:15:12 +00001118Action::TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001119Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001120 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001121 ASTTemplateArgsPtr TemplateArgsIn,
1122 SourceLocation *TemplateArgLocs,
John McCall6b2becf2009-09-08 17:47:29 +00001123 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001124 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001125
Douglas Gregor40808ce2009-03-09 23:48:35 +00001126 // Translate the parser's template argument list in our AST format.
1127 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1128 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001129
Douglas Gregor7532dc62009-03-30 22:58:21 +00001130 QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001131 TemplateArgs.data(),
1132 TemplateArgs.size(),
Douglas Gregor7532dc62009-03-30 22:58:21 +00001133 RAngleLoc);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001134 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001135
1136 if (Result.isNull())
1137 return true;
1138
John McCall6b2becf2009-09-08 17:47:29 +00001139 return Result.getAsOpaquePtr();
1140}
John McCallf1bbbb42009-09-04 01:14:41 +00001141
John McCall6b2becf2009-09-08 17:47:29 +00001142Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1143 TagUseKind TUK,
1144 DeclSpec::TST TagSpec,
1145 SourceLocation TagLoc) {
1146 if (TypeResult.isInvalid())
1147 return Sema::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001148
John McCall6b2becf2009-09-08 17:47:29 +00001149 QualType Type = QualType::getFromOpaquePtr(TypeResult.get());
John McCallf1bbbb42009-09-04 01:14:41 +00001150
John McCall6b2becf2009-09-08 17:47:29 +00001151 // Verify the tag specifier.
1152 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
John McCall6b2becf2009-09-08 17:47:29 +00001154 if (const RecordType *RT = Type->getAs<RecordType>()) {
1155 RecordDecl *D = RT->getDecl();
1156
1157 IdentifierInfo *Id = D->getIdentifier();
1158 assert(Id && "templated class must have an identifier");
1159
1160 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1161 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001162 << Type
John McCall6b2becf2009-09-08 17:47:29 +00001163 << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1164 D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001165 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001166 }
1167 }
1168
John McCall6b2becf2009-09-08 17:47:29 +00001169 QualType ElabType = Context.getElaboratedType(Type, TagKind);
1170
1171 return ElabType.getAsOpaquePtr();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001172}
1173
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001174Sema::OwningExprResult Sema::BuildTemplateIdExpr(TemplateName Template,
1175 SourceLocation TemplateNameLoc,
1176 SourceLocation LAngleLoc,
1177 const TemplateArgument *TemplateArgs,
1178 unsigned NumTemplateArgs,
1179 SourceLocation RAngleLoc) {
1180 // FIXME: Can we do any checking at this point? I guess we could check the
1181 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001182 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001183 // though.
Mike Stump1eb44332009-09-09 15:08:12 +00001184 return Owned(TemplateIdRefExpr::Create(Context,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001185 /*FIXME: New type?*/Context.OverloadTy,
1186 /*FIXME: Necessary?*/0,
1187 /*FIXME: Necessary?*/SourceRange(),
1188 Template, TemplateNameLoc, LAngleLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001189 TemplateArgs,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001190 NumTemplateArgs, RAngleLoc));
1191}
1192
1193Sema::OwningExprResult Sema::ActOnTemplateIdExpr(TemplateTy TemplateD,
1194 SourceLocation TemplateNameLoc,
1195 SourceLocation LAngleLoc,
1196 ASTTemplateArgsPtr TemplateArgsIn,
1197 SourceLocation *TemplateArgLocs,
1198 SourceLocation RAngleLoc) {
1199 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001201 // Translate the parser's template argument list in our AST format.
1202 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1203 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregor2aef06d2009-07-22 20:55:49 +00001204 TemplateArgsIn.release();
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001206 return BuildTemplateIdExpr(Template, TemplateNameLoc, LAngleLoc,
1207 TemplateArgs.data(), TemplateArgs.size(),
1208 RAngleLoc);
1209}
1210
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001211Sema::OwningExprResult
1212Sema::ActOnMemberTemplateIdReferenceExpr(Scope *S, ExprArg Base,
1213 SourceLocation OpLoc,
1214 tok::TokenKind OpKind,
1215 const CXXScopeSpec &SS,
1216 TemplateTy TemplateD,
1217 SourceLocation TemplateNameLoc,
1218 SourceLocation LAngleLoc,
1219 ASTTemplateArgsPtr TemplateArgsIn,
1220 SourceLocation *TemplateArgLocs,
1221 SourceLocation RAngleLoc) {
1222 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001224 // FIXME: We're going to end up looking up the template based on its name,
1225 // twice!
1226 DeclarationName Name;
1227 if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl())
1228 Name = ActualTemplate->getDeclName();
1229 else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl())
1230 Name = Ovl->getDeclName();
1231 else
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001232 Name = Template.getAsDependentTemplateName()->getName();
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001234 // Translate the parser's template argument list in our AST format.
1235 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1236 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
1237 TemplateArgsIn.release();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001239 // Do we have the save the actual template name? We might need it...
1240 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, TemplateNameLoc,
1241 Name, true, LAngleLoc,
1242 TemplateArgs.data(), TemplateArgs.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001243 RAngleLoc, DeclPtrTy(), &SS);
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001244}
1245
Douglas Gregorc45c2322009-03-31 00:43:58 +00001246/// \brief Form a dependent template name.
1247///
1248/// This action forms a dependent template name given the template
1249/// name and its (presumably dependent) scope specifier. For
1250/// example, given "MetaFun::template apply", the scope specifier \p
1251/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1252/// of the "template" keyword, and "apply" is the \p Name.
Mike Stump1eb44332009-09-09 15:08:12 +00001253Sema::TemplateTy
Douglas Gregorc45c2322009-03-31 00:43:58 +00001254Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
1255 const IdentifierInfo &Name,
1256 SourceLocation NameLoc,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001257 const CXXScopeSpec &SS,
1258 TypeTy *ObjectType) {
Mike Stump1eb44332009-09-09 15:08:12 +00001259 if ((ObjectType &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001260 computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) ||
1261 (SS.isSet() && computeDeclContext(SS, false))) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001262 // C++0x [temp.names]p5:
1263 // If a name prefixed by the keyword template is not the name of
1264 // a template, the program is ill-formed. [Note: the keyword
1265 // template may not be applied to non-template members of class
1266 // templates. -end note ] [ Note: as is the case with the
1267 // typename prefix, the template prefix is allowed in cases
1268 // where it is not strictly necessary; i.e., when the
1269 // nested-name-specifier or the expression on the left of the ->
1270 // or . is not dependent on a template-parameter, or the use
1271 // does not appear in the scope of a template. -end note]
1272 //
1273 // Note: C++03 was more strict here, because it banned the use of
1274 // the "template" keyword prior to a template-name that was not a
1275 // dependent name. C++ DR468 relaxed this requirement (the
1276 // "template" keyword is now permitted). We follow the C++0x
1277 // rules, even in C++03 mode, retroactively applying the DR.
1278 TemplateTy Template;
Mike Stump1eb44332009-09-09 15:08:12 +00001279 TemplateNameKind TNK = isTemplateName(0, Name, NameLoc, &SS, ObjectType,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001280 false, Template);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001281 if (TNK == TNK_Non_template) {
1282 Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1283 << &Name;
1284 return TemplateTy();
1285 }
1286
1287 return Template;
1288 }
1289
Mike Stump1eb44332009-09-09 15:08:12 +00001290 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001291 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001292 return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name));
1293}
1294
Mike Stump1eb44332009-09-09 15:08:12 +00001295bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
Anders Carlsson436b1562009-06-13 00:33:33 +00001296 const TemplateArgument &Arg,
1297 TemplateArgumentListBuilder &Converted) {
1298 // Check template type parameter.
1299 if (Arg.getKind() != TemplateArgument::Type) {
1300 // C++ [temp.arg.type]p1:
1301 // A template-argument for a template-parameter which is a
1302 // type shall be a type-id.
1303
1304 // We have a template type parameter but the template argument
1305 // is not a type.
1306 Diag(Arg.getLocation(), diag::err_template_arg_must_be_type);
1307 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Anders Carlsson436b1562009-06-13 00:33:33 +00001309 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001310 }
Anders Carlsson436b1562009-06-13 00:33:33 +00001311
1312 if (CheckTemplateArgument(Param, Arg.getAsType(), Arg.getLocation()))
1313 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Anders Carlsson436b1562009-06-13 00:33:33 +00001315 // Add the converted template type argument.
Anders Carlssonfb250522009-06-23 01:26:57 +00001316 Converted.Append(
Anders Carlsson436b1562009-06-13 00:33:33 +00001317 TemplateArgument(Arg.getLocation(),
1318 Context.getCanonicalType(Arg.getAsType())));
1319 return false;
1320}
1321
Douglas Gregorc15cb382009-02-09 23:23:08 +00001322/// \brief Check that the given template argument list is well-formed
1323/// for specializing the given template.
1324bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
1325 SourceLocation TemplateLoc,
1326 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001327 const TemplateArgument *TemplateArgs,
1328 unsigned NumTemplateArgs,
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001329 SourceLocation RAngleLoc,
Douglas Gregor16134c62009-07-01 00:28:38 +00001330 bool PartialTemplateArgs,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001331 TemplateArgumentListBuilder &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001332 TemplateParameterList *Params = Template->getTemplateParameters();
1333 unsigned NumParams = Params->size();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001334 unsigned NumArgs = NumTemplateArgs;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001335 bool Invalid = false;
1336
Mike Stump1eb44332009-09-09 15:08:12 +00001337 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001338 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001340 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00001341 (NumArgs < Params->getMinRequiredArguments() &&
1342 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001343 // FIXME: point at either the first arg beyond what we can handle,
1344 // or the '>', depending on whether we have too many or too few
1345 // arguments.
1346 SourceRange Range;
1347 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001348 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001349 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
1350 << (NumArgs > NumParams)
1351 << (isa<ClassTemplateDecl>(Template)? 0 :
1352 isa<FunctionTemplateDecl>(Template)? 1 :
1353 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
1354 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00001355 Diag(Template->getLocation(), diag::note_template_decl_here)
1356 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00001357 Invalid = true;
1358 }
Mike Stump1eb44332009-09-09 15:08:12 +00001359
1360 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00001361 // [...] The type and form of each template-argument specified in
1362 // a template-id shall match the type and form specified for the
1363 // corresponding parameter declared by the template in its
1364 // template-parameter-list.
1365 unsigned ArgIdx = 0;
1366 for (TemplateParameterList::iterator Param = Params->begin(),
1367 ParamEnd = Params->end();
1368 Param != ParamEnd; ++Param, ++ArgIdx) {
Douglas Gregor16134c62009-07-01 00:28:38 +00001369 if (ArgIdx > NumArgs && PartialTemplateArgs)
1370 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregorc15cb382009-02-09 23:23:08 +00001372 // Decode the template argument
Douglas Gregor40808ce2009-03-09 23:48:35 +00001373 TemplateArgument Arg;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001374 if (ArgIdx >= NumArgs) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001375 // Retrieve the default template argument from the template
1376 // parameter.
1377 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001378 if (TTP->isParameterPack()) {
Anders Carlssonfb250522009-06-23 01:26:57 +00001379 // We have an empty argument pack.
1380 Converted.BeginPack();
1381 Converted.EndPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001382 break;
1383 }
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001385 if (!TTP->hasDefaultArgument())
1386 break;
1387
Douglas Gregor40808ce2009-03-09 23:48:35 +00001388 QualType ArgType = TTP->getDefaultArgument();
Douglas Gregor99ebf652009-02-27 19:31:52 +00001389
1390 // If the argument type is dependent, instantiate it now based
1391 // on the previously-computed template arguments.
Douglas Gregordf667e72009-03-10 20:44:00 +00001392 if (ArgType->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001393 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001394 Template, Converted.getFlatArguments(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001395 Converted.flatSize(),
Douglas Gregordf667e72009-03-10 20:44:00 +00001396 SourceRange(TemplateLoc, RAngleLoc));
Douglas Gregor7e063902009-05-11 23:53:27 +00001397
Anders Carlssone9c904b2009-06-05 04:47:51 +00001398 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001399 /*TakeArgs=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001400 ArgType = SubstType(ArgType,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001401 MultiLevelTemplateArgumentList(TemplateArgs),
John McCallce3ff2b2009-08-25 22:02:44 +00001402 TTP->getDefaultArgumentLoc(),
1403 TTP->getDeclName());
Douglas Gregordf667e72009-03-10 20:44:00 +00001404 }
Douglas Gregor99ebf652009-02-27 19:31:52 +00001405
1406 if (ArgType.isNull())
Douglas Gregorcd281c32009-02-28 00:25:32 +00001407 return true;
Douglas Gregor99ebf652009-02-27 19:31:52 +00001408
Douglas Gregor40808ce2009-03-09 23:48:35 +00001409 Arg = TemplateArgument(TTP->getLocation(), ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001410 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001411 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1412 if (!NTTP->hasDefaultArgument())
1413 break;
1414
Mike Stump1eb44332009-09-09 15:08:12 +00001415 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001416 Template, Converted.getFlatArguments(),
Anders Carlsson3b56c002009-06-11 16:06:49 +00001417 Converted.flatSize(),
1418 SourceRange(TemplateLoc, RAngleLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Anders Carlsson3b56c002009-06-11 16:06:49 +00001420 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001421 /*TakeArgs=*/false);
Anders Carlsson3b56c002009-06-11 16:06:49 +00001422
Mike Stump1eb44332009-09-09 15:08:12 +00001423 Sema::OwningExprResult E
1424 = SubstExpr(NTTP->getDefaultArgument(),
Douglas Gregord6350ae2009-08-28 20:31:08 +00001425 MultiLevelTemplateArgumentList(TemplateArgs));
Anders Carlsson3b56c002009-06-11 16:06:49 +00001426 if (E.isInvalid())
1427 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Anders Carlsson3b56c002009-06-11 16:06:49 +00001429 Arg = TemplateArgument(E.takeAs<Expr>());
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001430 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001431 TemplateTemplateParmDecl *TempParm
1432 = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001433
1434 if (!TempParm->hasDefaultArgument())
1435 break;
1436
John McCallce3ff2b2009-08-25 22:02:44 +00001437 // FIXME: Subst default argument
Douglas Gregor40808ce2009-03-09 23:48:35 +00001438 Arg = TemplateArgument(TempParm->getDefaultArgument());
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001439 }
1440 } else {
1441 // Retrieve the template argument produced by the user.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001442 Arg = TemplateArgs[ArgIdx];
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001443 }
1444
Douglas Gregorc15cb382009-02-09 23:23:08 +00001445
1446 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001447 if (TTP->isParameterPack()) {
Anders Carlssonfb250522009-06-23 01:26:57 +00001448 Converted.BeginPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001449 // Check all the remaining arguments (if any).
1450 for (; ArgIdx < NumArgs; ++ArgIdx) {
1451 if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted))
1452 Invalid = true;
1453 }
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Anders Carlssonfb250522009-06-23 01:26:57 +00001455 Converted.EndPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001456 } else {
1457 if (CheckTemplateTypeArgument(TTP, Arg, Converted))
1458 Invalid = true;
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorc15cb382009-02-09 23:23:08 +00001461 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1462 // Check non-type template parameters.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001463
John McCallce3ff2b2009-08-25 22:02:44 +00001464 // Do substitution on the type of the non-type template parameter
1465 // with the template arguments we've seen thus far.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001466 QualType NTTPType = NTTP->getType();
1467 if (NTTPType->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +00001468 // Do substitution on the type of the non-type template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001469 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001470 Template, Converted.getFlatArguments(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001471 Converted.flatSize(),
Douglas Gregordf667e72009-03-10 20:44:00 +00001472 SourceRange(TemplateLoc, RAngleLoc));
1473
Anders Carlssone9c904b2009-06-05 04:47:51 +00001474 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001475 /*TakeArgs=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001476 NTTPType = SubstType(NTTPType,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001477 MultiLevelTemplateArgumentList(TemplateArgs),
John McCallce3ff2b2009-08-25 22:02:44 +00001478 NTTP->getLocation(),
1479 NTTP->getDeclName());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001480 // If that worked, check the non-type template parameter type
1481 // for validity.
1482 if (!NTTPType.isNull())
Mike Stump1eb44332009-09-09 15:08:12 +00001483 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001484 NTTP->getLocation());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001485 if (NTTPType.isNull()) {
1486 Invalid = true;
1487 break;
1488 }
1489 }
1490
Douglas Gregor40808ce2009-03-09 23:48:35 +00001491 switch (Arg.getKind()) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001492 case TemplateArgument::Null:
1493 assert(false && "Should never see a NULL template argument here");
1494 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Douglas Gregor40808ce2009-03-09 23:48:35 +00001496 case TemplateArgument::Expression: {
1497 Expr *E = Arg.getAsExpr();
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001498 TemplateArgument Result;
1499 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
Douglas Gregorc15cb382009-02-09 23:23:08 +00001500 Invalid = true;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001501 else
Anders Carlssonfb250522009-06-23 01:26:57 +00001502 Converted.Append(Result);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001503 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001504 }
1505
Douglas Gregor40808ce2009-03-09 23:48:35 +00001506 case TemplateArgument::Declaration:
1507 case TemplateArgument::Integral:
1508 // We've already checked this template argument, so just copy
1509 // it to the list of converted arguments.
Anders Carlssonfb250522009-06-23 01:26:57 +00001510 Converted.Append(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001511 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001512
Douglas Gregor40808ce2009-03-09 23:48:35 +00001513 case TemplateArgument::Type:
1514 // We have a non-type template parameter but the template
1515 // argument is a type.
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregor40808ce2009-03-09 23:48:35 +00001517 // C++ [temp.arg]p2:
1518 // In a template-argument, an ambiguity between a type-id and
1519 // an expression is resolved to a type-id, regardless of the
1520 // form of the corresponding template-parameter.
1521 //
1522 // We warn specifically about this case, since it can be rather
1523 // confusing for users.
1524 if (Arg.getAsType()->isFunctionType())
1525 Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig)
1526 << Arg.getAsType();
1527 else
1528 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr);
1529 Diag((*Param)->getLocation(), diag::note_template_param_here);
1530 Invalid = true;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001531 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Anders Carlssond01b1da2009-06-15 17:04:53 +00001533 case TemplateArgument::Pack:
1534 assert(0 && "FIXME: Implement!");
1535 break;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537 } else {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001538 // Check template template parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001539 TemplateTemplateParmDecl *TempParm
Douglas Gregorc15cb382009-02-09 23:23:08 +00001540 = cast<TemplateTemplateParmDecl>(*Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor40808ce2009-03-09 23:48:35 +00001542 switch (Arg.getKind()) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001543 case TemplateArgument::Null:
1544 assert(false && "Should never see a NULL template argument here");
1545 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor40808ce2009-03-09 23:48:35 +00001547 case TemplateArgument::Expression: {
1548 Expr *ArgExpr = Arg.getAsExpr();
1549 if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
1550 isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
1551 if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
1552 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Douglas Gregor40808ce2009-03-09 23:48:35 +00001554 // Add the converted template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001555 Decl *D
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001556 = cast<DeclRefExpr>(ArgExpr)->getDecl()->getCanonicalDecl();
Anders Carlssonfb250522009-06-23 01:26:57 +00001557 Converted.Append(TemplateArgument(Arg.getLocation(), D));
Douglas Gregor40808ce2009-03-09 23:48:35 +00001558 continue;
1559 }
1560 }
1561 // fall through
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregor40808ce2009-03-09 23:48:35 +00001563 case TemplateArgument::Type: {
1564 // We have a template template parameter but the template
1565 // argument does not refer to a template.
1566 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
1567 Invalid = true;
1568 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001569 }
1570
Douglas Gregor40808ce2009-03-09 23:48:35 +00001571 case TemplateArgument::Declaration:
1572 // We've already checked this template argument, so just copy
1573 // it to the list of converted arguments.
Anders Carlssonfb250522009-06-23 01:26:57 +00001574 Converted.Append(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001575 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Douglas Gregor40808ce2009-03-09 23:48:35 +00001577 case TemplateArgument::Integral:
1578 assert(false && "Integral argument with template template parameter");
1579 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Anders Carlssond01b1da2009-06-15 17:04:53 +00001581 case TemplateArgument::Pack:
1582 assert(0 && "FIXME: Implement!");
1583 break;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001584 }
Douglas Gregorc15cb382009-02-09 23:23:08 +00001585 }
1586 }
1587
1588 return Invalid;
1589}
1590
1591/// \brief Check a template argument against its corresponding
1592/// template type parameter.
1593///
1594/// This routine implements the semantics of C++ [temp.arg.type]. It
1595/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001596bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
Douglas Gregorc15cb382009-02-09 23:23:08 +00001597 QualType Arg, SourceLocation ArgLoc) {
1598 // C++ [temp.arg.type]p2:
1599 // A local type, a type with no linkage, an unnamed type or a type
1600 // compounded from any of these types shall not be used as a
1601 // template-argument for a template type-parameter.
1602 //
1603 // FIXME: Perform the recursive and no-linkage type checks.
1604 const TagType *Tag = 0;
1605 if (const EnumType *EnumT = Arg->getAsEnumType())
1606 Tag = EnumT;
Ted Kremenek6217b802009-07-29 21:53:49 +00001607 else if (const RecordType *RecordT = Arg->getAs<RecordType>())
Douglas Gregorc15cb382009-02-09 23:23:08 +00001608 Tag = RecordT;
1609 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
1610 return Diag(ArgLoc, diag::err_template_arg_local_type)
1611 << QualType(Tag, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001612 else if (Tag && !Tag->getDecl()->getDeclName() &&
Douglas Gregor98137532009-03-10 18:33:27 +00001613 !Tag->getDecl()->getTypedefForAnonDecl()) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001614 Diag(ArgLoc, diag::err_template_arg_unnamed_type);
1615 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
1616 return true;
1617 }
1618
1619 return false;
1620}
1621
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001622/// \brief Checks whether the given template argument is the address
1623/// of an object or function according to C++ [temp.arg.nontype]p1.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001624bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
1625 NamedDecl *&Entity) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001626 bool Invalid = false;
1627
1628 // See through any implicit casts we added to fix the type.
1629 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1630 Arg = Cast->getSubExpr();
1631
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001632 // C++0x allows nullptr, and there's no further checking to be done for that.
1633 if (Arg->getType()->isNullPtrType())
1634 return false;
1635
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001636 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001637 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001638 // A template-argument for a non-type, non-template
1639 // template-parameter shall be one of: [...]
1640 //
1641 // -- the address of an object or function with external
1642 // linkage, including function templates and function
1643 // template-ids but excluding non-static class members,
1644 // expressed as & id-expression where the & is optional if
1645 // the name refers to a function or array, or if the
1646 // corresponding template-parameter is a reference; or
1647 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001649 // Ignore (and complain about) any excess parentheses.
1650 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1651 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00001652 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001653 diag::err_template_arg_extra_parens)
1654 << Arg->getSourceRange();
1655 Invalid = true;
1656 }
1657
1658 Arg = Parens->getSubExpr();
1659 }
1660
1661 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
1662 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1663 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
1664 } else
1665 DRE = dyn_cast<DeclRefExpr>(Arg);
1666
1667 if (!DRE || !isa<ValueDecl>(DRE->getDecl()))
Mike Stump1eb44332009-09-09 15:08:12 +00001668 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001669 diag::err_template_arg_not_object_or_func_form)
1670 << Arg->getSourceRange();
1671
1672 // Cannot refer to non-static data members
1673 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
1674 return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
1675 << Field << Arg->getSourceRange();
1676
1677 // Cannot refer to non-static member functions
1678 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
1679 if (!Method->isStatic())
Mike Stump1eb44332009-09-09 15:08:12 +00001680 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001681 diag::err_template_arg_method)
1682 << Method << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001684 // Functions must have external linkage.
1685 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1686 if (Func->getStorageClass() == FunctionDecl::Static) {
Mike Stump1eb44332009-09-09 15:08:12 +00001687 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001688 diag::err_template_arg_function_not_extern)
1689 << Func << Arg->getSourceRange();
1690 Diag(Func->getLocation(), diag::note_template_arg_internal_object)
1691 << true;
1692 return true;
1693 }
1694
1695 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001696 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001697 return Invalid;
1698 }
1699
1700 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
1701 if (!Var->hasGlobalStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001702 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001703 diag::err_template_arg_object_not_extern)
1704 << Var << Arg->getSourceRange();
1705 Diag(Var->getLocation(), diag::note_template_arg_internal_object)
1706 << true;
1707 return true;
1708 }
1709
1710 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001711 Entity = Var;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001712 return Invalid;
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001715 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00001716 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001717 diag::err_template_arg_not_object_or_func)
1718 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001719 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001720 diag::note_template_arg_refers_here);
1721 return true;
1722}
1723
1724/// \brief Checks whether the given template argument is a pointer to
1725/// member constant according to C++ [temp.arg.nontype]p1.
Mike Stump1eb44332009-09-09 15:08:12 +00001726bool
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001727Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001728 bool Invalid = false;
1729
1730 // See through any implicit casts we added to fix the type.
1731 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1732 Arg = Cast->getSubExpr();
1733
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001734 // C++0x allows nullptr, and there's no further checking to be done for that.
1735 if (Arg->getType()->isNullPtrType())
1736 return false;
1737
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001738 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001739 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001740 // A template-argument for a non-type, non-template
1741 // template-parameter shall be one of: [...]
1742 //
1743 // -- a pointer to member expressed as described in 5.3.1.
1744 QualifiedDeclRefExpr *DRE = 0;
1745
1746 // Ignore (and complain about) any excess parentheses.
1747 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1748 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00001749 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001750 diag::err_template_arg_extra_parens)
1751 << Arg->getSourceRange();
1752 Invalid = true;
1753 }
1754
1755 Arg = Parens->getSubExpr();
1756 }
1757
1758 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg))
1759 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1760 DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr());
1761
1762 if (!DRE)
1763 return Diag(Arg->getSourceRange().getBegin(),
1764 diag::err_template_arg_not_pointer_to_member_form)
1765 << Arg->getSourceRange();
1766
1767 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
1768 assert((isa<FieldDecl>(DRE->getDecl()) ||
1769 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
1770 "Only non-static member pointers can make it here");
1771
1772 // Okay: this is the address of a non-static member, and therefore
1773 // a member pointer constant.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001774 Member = DRE->getDecl();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001775 return Invalid;
1776 }
1777
1778 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00001779 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001780 diag::err_template_arg_not_pointer_to_member_form)
1781 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001782 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001783 diag::note_template_arg_refers_here);
1784 return true;
1785}
1786
Douglas Gregorc15cb382009-02-09 23:23:08 +00001787/// \brief Check a template argument against its corresponding
1788/// non-type template parameter.
1789///
Douglas Gregor2943aed2009-03-03 04:44:36 +00001790/// This routine implements the semantics of C++ [temp.arg.nontype].
1791/// It returns true if an error occurred, and false otherwise. \p
1792/// InstantiatedParamType is the type of the non-type template
1793/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001794///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001795/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00001796bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00001797 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001798 TemplateArgument &Converted) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001799 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
1800
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001801 // If either the parameter has a dependent type or the argument is
1802 // type-dependent, there's nothing we can check now.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001803 // FIXME: Add template argument to Converted!
Douglas Gregor40808ce2009-03-09 23:48:35 +00001804 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
1805 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001806 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001807 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001808 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001809
1810 // C++ [temp.arg.nontype]p5:
1811 // The following conversions are performed on each expression used
1812 // as a non-type template-argument. If a non-type
1813 // template-argument cannot be converted to the type of the
1814 // corresponding template-parameter then the program is
1815 // ill-formed.
1816 //
1817 // -- for a non-type template-parameter of integral or
1818 // enumeration type, integral promotions (4.5) and integral
1819 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001820 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00001821 QualType ArgType = Arg->getType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001822 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001823 // C++ [temp.arg.nontype]p1:
1824 // A template-argument for a non-type, non-template
1825 // template-parameter shall be one of:
1826 //
1827 // -- an integral constant-expression of integral or enumeration
1828 // type; or
1829 // -- the name of a non-type template-parameter; or
1830 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001831 llvm::APSInt Value;
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001832 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001833 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001834 diag::err_template_arg_not_integral_or_enumeral)
1835 << ArgType << Arg->getSourceRange();
1836 Diag(Param->getLocation(), diag::note_template_param_here);
1837 return true;
1838 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001839 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001840 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
1841 << ArgType << Arg->getSourceRange();
1842 return true;
1843 }
1844
1845 // FIXME: We need some way to more easily get the unqualified form
1846 // of the types without going all the way to the
1847 // canonical type.
1848 if (Context.getCanonicalType(ParamType).getCVRQualifiers())
1849 ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
1850 if (Context.getCanonicalType(ArgType).getCVRQualifiers())
1851 ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
1852
1853 // Try to convert the argument to the parameter's type.
1854 if (ParamType == ArgType) {
1855 // Okay: no conversion necessary
1856 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
1857 !ParamType->isEnumeralType()) {
1858 // This is an integral promotion or conversion.
1859 ImpCastExprToType(Arg, ParamType);
1860 } else {
1861 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00001862 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001863 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00001864 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001865 Diag(Param->getLocation(), diag::note_template_param_here);
1866 return true;
1867 }
1868
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001869 QualType IntegerType = Context.getCanonicalType(ParamType);
1870 if (const EnumType *Enum = IntegerType->getAsEnumType())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001871 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001872
1873 if (!Arg->isValueDependent()) {
1874 // Check that an unsigned parameter does not receive a negative
1875 // value.
1876 if (IntegerType->isUnsignedIntegerType()
1877 && (Value.isSigned() && Value.isNegative())) {
1878 Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
1879 << Value.toString(10) << Param->getType()
1880 << Arg->getSourceRange();
1881 Diag(Param->getLocation(), diag::note_template_param_here);
1882 return true;
1883 }
1884
1885 // Check that we don't overflow the template parameter type.
1886 unsigned AllowedBits = Context.getTypeSize(IntegerType);
1887 if (Value.getActiveBits() > AllowedBits) {
Mike Stump1eb44332009-09-09 15:08:12 +00001888 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001889 diag::err_template_arg_too_large)
1890 << Value.toString(10) << Param->getType()
1891 << Arg->getSourceRange();
1892 Diag(Param->getLocation(), diag::note_template_param_here);
1893 return true;
1894 }
1895
1896 if (Value.getBitWidth() != AllowedBits)
1897 Value.extOrTrunc(AllowedBits);
1898 Value.setIsSigned(IntegerType->isSignedIntegerType());
1899 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001900
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001901 // Add the value of this argument to the list of converted
1902 // arguments. We use the bitwidth and signedness of the template
1903 // parameter.
1904 if (Arg->isValueDependent()) {
1905 // The argument is value-dependent. Create a new
1906 // TemplateArgument with the converted expression.
1907 Converted = TemplateArgument(Arg);
1908 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001909 }
1910
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001911 Converted = TemplateArgument(StartLoc, Value,
Mike Stump1eb44332009-09-09 15:08:12 +00001912 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001913 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001914 return false;
1915 }
Douglas Gregora35284b2009-02-11 00:19:33 +00001916
Douglas Gregorb86b0572009-02-11 01:18:59 +00001917 // Handle pointer-to-function, reference-to-function, and
1918 // pointer-to-member-function all in (roughly) the same way.
1919 if (// -- For a non-type template-parameter of type pointer to
1920 // function, only the function-to-pointer conversion (4.3) is
1921 // applied. If the template-argument represents a set of
1922 // overloaded functions (or a pointer to such), the matching
1923 // function is selected from the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001924 // In C++0x, any std::nullptr_t value can be converted.
Douglas Gregorb86b0572009-02-11 01:18:59 +00001925 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001926 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00001927 // -- For a non-type template-parameter of type reference to
1928 // function, no conversions apply. If the template-argument
1929 // represents a set of overloaded functions, the matching
1930 // function is selected from the set (13.4).
1931 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001932 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00001933 // -- For a non-type template-parameter of type pointer to
1934 // member function, no conversions apply. If the
1935 // template-argument represents a set of overloaded member
1936 // functions, the matching member function is selected from
1937 // the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001938 // Again, C++0x allows a std::nullptr_t value.
Douglas Gregorb86b0572009-02-11 01:18:59 +00001939 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001940 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00001941 ->isFunctionType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001942 if (Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001943 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001944 // We don't have to do anything: the types already match.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001945 } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
1946 ParamType->isMemberPointerType())) {
1947 ArgType = ParamType;
1948 ImpCastExprToType(Arg, ParamType);
Douglas Gregorb86b0572009-02-11 01:18:59 +00001949 } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001950 ArgType = Context.getPointerType(ArgType);
1951 ImpCastExprToType(Arg, ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001952 } else if (FunctionDecl *Fn
Douglas Gregora35284b2009-02-11 00:19:33 +00001953 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001954 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
1955 return true;
1956
Douglas Gregora35284b2009-02-11 00:19:33 +00001957 FixOverloadedFunctionReference(Arg, Fn);
1958 ArgType = Arg->getType();
Douglas Gregorb86b0572009-02-11 01:18:59 +00001959 if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001960 ArgType = Context.getPointerType(Arg->getType());
1961 ImpCastExprToType(Arg, ArgType);
1962 }
1963 }
1964
Mike Stump1eb44332009-09-09 15:08:12 +00001965 if (!Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001966 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001967 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00001968 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00001969 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00001970 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00001971 Diag(Param->getLocation(), diag::note_template_param_here);
1972 return true;
1973 }
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001975 if (ParamType->isMemberPointerType()) {
1976 NamedDecl *Member = 0;
1977 if (CheckTemplateArgumentPointerToMember(Arg, Member))
1978 return true;
1979
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001980 if (Member)
1981 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001982 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001983 return false;
1984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001986 NamedDecl *Entity = 0;
1987 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
1988 return true;
1989
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001990 if (Entity)
1991 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001992 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001993 return false;
Douglas Gregora35284b2009-02-11 00:19:33 +00001994 }
1995
Chris Lattnerfe90de72009-02-20 21:37:53 +00001996 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00001997 // -- for a non-type template-parameter of type pointer to
1998 // object, qualification conversions (4.4) and the
1999 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002000 // C++0x also allows a value of std::nullptr_t.
Ted Kremenek6217b802009-07-29 21:53:49 +00002001 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002002 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002003
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002004 if (ArgType->isNullPtrType()) {
2005 ArgType = ParamType;
2006 ImpCastExprToType(Arg, ParamType);
2007 } else if (ArgType->isArrayType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002008 ArgType = Context.getArrayDecayedType(ArgType);
2009 ImpCastExprToType(Arg, ArgType);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002010 }
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002011
Douglas Gregorb86b0572009-02-11 01:18:59 +00002012 if (IsQualificationConversion(ArgType, ParamType)) {
2013 ArgType = ParamType;
2014 ImpCastExprToType(Arg, ParamType);
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002017 if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002018 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002019 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002020 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002021 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregorb86b0572009-02-11 01:18:59 +00002022 Diag(Param->getLocation(), diag::note_template_param_here);
2023 return true;
2024 }
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002026 NamedDecl *Entity = 0;
2027 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2028 return true;
2029
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002030 if (Entity)
2031 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002032 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002033 return false;
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002034 }
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Ted Kremenek6217b802009-07-29 21:53:49 +00002036 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002037 // -- For a non-type template-parameter of type reference to
2038 // object, no conversions apply. The type referred to by the
2039 // reference may be more cv-qualified than the (otherwise
2040 // identical) type of the template-argument. The
2041 // template-parameter is bound directly to the
2042 // template-argument, which must be an lvalue.
Douglas Gregorbad0e652009-03-24 20:32:41 +00002043 assert(ParamRefType->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002044 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002045
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002046 if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002047 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002048 diag::err_template_arg_no_ref_bind)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002049 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002050 << Arg->getSourceRange();
2051 Diag(Param->getLocation(), diag::note_template_param_here);
2052 return true;
2053 }
2054
Mike Stump1eb44332009-09-09 15:08:12 +00002055 unsigned ParamQuals
Douglas Gregorb86b0572009-02-11 01:18:59 +00002056 = Context.getCanonicalType(ParamType).getCVRQualifiers();
2057 unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Douglas Gregorb86b0572009-02-11 01:18:59 +00002059 if ((ParamQuals | ArgQuals) != ParamQuals) {
2060 Diag(Arg->getSourceRange().getBegin(),
2061 diag::err_template_arg_ref_bind_ignores_quals)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002062 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002063 << Arg->getSourceRange();
2064 Diag(Param->getLocation(), diag::note_template_param_here);
2065 return true;
2066 }
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002068 NamedDecl *Entity = 0;
2069 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2070 return true;
2071
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002072 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002073 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002074 return false;
Douglas Gregorb86b0572009-02-11 01:18:59 +00002075 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00002076
2077 // -- For a non-type template-parameter of type pointer to data
2078 // member, qualification conversions (4.4) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002079 // C++0x allows std::nullptr_t values.
Douglas Gregor658bbb52009-02-11 16:16:59 +00002080 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2081
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002082 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00002083 // Types match exactly: nothing more to do here.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002084 } else if (ArgType->isNullPtrType()) {
2085 ImpCastExprToType(Arg, ParamType);
Douglas Gregor658bbb52009-02-11 16:16:59 +00002086 } else if (IsQualificationConversion(ArgType, ParamType)) {
2087 ImpCastExprToType(Arg, ParamType);
2088 } else {
2089 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002090 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00002091 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002092 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00002093 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002094 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00002095 }
2096
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002097 NamedDecl *Member = 0;
2098 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2099 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002101 if (Member)
2102 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002103 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002104 return false;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002105}
2106
2107/// \brief Check a template argument against its corresponding
2108/// template template parameter.
2109///
2110/// This routine implements the semantics of C++ [temp.arg.template].
2111/// It returns true if an error occurred, and false otherwise.
2112bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
2113 DeclRefExpr *Arg) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002114 assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
2115 TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
2116
2117 // C++ [temp.arg.template]p1:
2118 // A template-argument for a template template-parameter shall be
2119 // the name of a class template, expressed as id-expression. Only
2120 // primary class templates are considered when matching the
2121 // template template argument with the corresponding parameter;
2122 // partial specializations are not considered even if their
2123 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002124 //
2125 // Note that we also allow template template parameters here, which
2126 // will happen when we are dealing with, e.g., class template
2127 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002128 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002129 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002130 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00002131 "Only function templates are possible here");
Douglas Gregore53060f2009-06-25 22:08:12 +00002132 Diag(Arg->getLocStart(), diag::err_template_arg_not_class_template);
2133 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00002134 << Template;
2135 }
2136
2137 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2138 Param->getTemplateParameters(),
2139 true, true,
2140 Arg->getSourceRange().getBegin());
Douglas Gregorc15cb382009-02-09 23:23:08 +00002141}
2142
Douglas Gregorddc29e12009-02-06 22:42:48 +00002143/// \brief Determine whether the given template parameter lists are
2144/// equivalent.
2145///
Mike Stump1eb44332009-09-09 15:08:12 +00002146/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00002147/// source code as part of a new template declaration.
2148///
2149/// \param Old The old template parameter list, typically found via
2150/// name lookup of the template declared with this template parameter
2151/// list.
2152///
2153/// \param Complain If true, this routine will produce a diagnostic if
2154/// the template parameter lists are not equivalent.
2155///
Douglas Gregordd0574e2009-02-10 00:24:35 +00002156/// \param IsTemplateTemplateParm If true, this routine is being
2157/// called to compare the template parameter lists of a template
2158/// template parameter.
2159///
2160/// \param TemplateArgLoc If this source location is valid, then we
2161/// are actually checking the template parameter list of a template
2162/// argument (New) against the template parameter list of its
2163/// corresponding template template parameter (Old). We produce
2164/// slightly different diagnostics in this scenario.
2165///
Douglas Gregorddc29e12009-02-06 22:42:48 +00002166/// \returns True if the template parameter lists are equal, false
2167/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002168bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00002169Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2170 TemplateParameterList *Old,
2171 bool Complain,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002172 bool IsTemplateTemplateParm,
2173 SourceLocation TemplateArgLoc) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002174 if (Old->size() != New->size()) {
2175 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002176 unsigned NextDiag = diag::err_template_param_list_different_arity;
2177 if (TemplateArgLoc.isValid()) {
2178 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2179 NextDiag = diag::note_template_param_list_different_arity;
Mike Stump1eb44332009-09-09 15:08:12 +00002180 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00002181 Diag(New->getTemplateLoc(), NextDiag)
2182 << (New->size() > Old->size())
2183 << IsTemplateTemplateParm
2184 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregorddc29e12009-02-06 22:42:48 +00002185 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
2186 << IsTemplateTemplateParm
2187 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2188 }
2189
2190 return false;
2191 }
2192
2193 for (TemplateParameterList::iterator OldParm = Old->begin(),
2194 OldParmEnd = Old->end(), NewParm = New->begin();
2195 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
2196 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregor34d1dc92009-06-24 16:50:40 +00002197 if (Complain) {
2198 unsigned NextDiag = diag::err_template_param_different_kind;
2199 if (TemplateArgLoc.isValid()) {
2200 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2201 NextDiag = diag::note_template_param_different_kind;
2202 }
2203 Diag((*NewParm)->getLocation(), NextDiag)
2204 << IsTemplateTemplateParm;
2205 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
2206 << IsTemplateTemplateParm;
Douglas Gregordd0574e2009-02-10 00:24:35 +00002207 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00002208 return false;
2209 }
2210
2211 if (isa<TemplateTypeParmDecl>(*OldParm)) {
2212 // Okay; all template type parameters are equivalent (since we
Douglas Gregordd0574e2009-02-10 00:24:35 +00002213 // know we're at the same index).
2214#if 0
Mike Stump390b4cc2009-05-16 07:39:55 +00002215 // FIXME: Enable this code in debug mode *after* we properly go through
2216 // and "instantiate" the template parameter lists of template template
2217 // parameters. It's only after this instantiation that (1) any dependent
2218 // types within the template parameter list of the template template
2219 // parameter can be checked, and (2) the template type parameter depths
Douglas Gregordd0574e2009-02-10 00:24:35 +00002220 // will match up.
Mike Stump1eb44332009-09-09 15:08:12 +00002221 QualType OldParmType
Douglas Gregorddc29e12009-02-06 22:42:48 +00002222 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
Mike Stump1eb44332009-09-09 15:08:12 +00002223 QualType NewParmType
Douglas Gregorddc29e12009-02-06 22:42:48 +00002224 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
Mike Stump1eb44332009-09-09 15:08:12 +00002225 assert(Context.getCanonicalType(OldParmType) ==
2226 Context.getCanonicalType(NewParmType) &&
Douglas Gregorddc29e12009-02-06 22:42:48 +00002227 "type parameter mismatch?");
2228#endif
Mike Stump1eb44332009-09-09 15:08:12 +00002229 } else if (NonTypeTemplateParmDecl *OldNTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002230 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2231 // The types of non-type template parameters must agree.
2232 NonTypeTemplateParmDecl *NewNTTP
2233 = cast<NonTypeTemplateParmDecl>(*NewParm);
2234 if (Context.getCanonicalType(OldNTTP->getType()) !=
2235 Context.getCanonicalType(NewNTTP->getType())) {
2236 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002237 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
2238 if (TemplateArgLoc.isValid()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002239 Diag(TemplateArgLoc,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002240 diag::err_template_arg_template_params_mismatch);
2241 NextDiag = diag::note_template_nontype_parm_different_type;
2242 }
2243 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregorddc29e12009-02-06 22:42:48 +00002244 << NewNTTP->getType()
2245 << IsTemplateTemplateParm;
Mike Stump1eb44332009-09-09 15:08:12 +00002246 Diag(OldNTTP->getLocation(),
Douglas Gregorddc29e12009-02-06 22:42:48 +00002247 diag::note_template_nontype_parm_prev_declaration)
2248 << OldNTTP->getType();
2249 }
2250 return false;
2251 }
2252 } else {
2253 // The template parameter lists of template template
2254 // parameters must agree.
2255 // FIXME: Could we perform a faster "type" comparison here?
Mike Stump1eb44332009-09-09 15:08:12 +00002256 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
Douglas Gregorddc29e12009-02-06 22:42:48 +00002257 "Only template template parameters handled here");
Mike Stump1eb44332009-09-09 15:08:12 +00002258 TemplateTemplateParmDecl *OldTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002259 = cast<TemplateTemplateParmDecl>(*OldParm);
2260 TemplateTemplateParmDecl *NewTTP
2261 = cast<TemplateTemplateParmDecl>(*NewParm);
2262 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2263 OldTTP->getTemplateParameters(),
2264 Complain,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002265 /*IsTemplateTemplateParm=*/true,
2266 TemplateArgLoc))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002267 return false;
2268 }
2269 }
2270
2271 return true;
2272}
2273
2274/// \brief Check whether a template can be declared within this scope.
2275///
2276/// If the template declaration is valid in this scope, returns
2277/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00002278bool
Douglas Gregor05396e22009-08-25 17:23:04 +00002279Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002280 // Find the nearest enclosing declaration scope.
2281 while ((S->getFlags() & Scope::DeclScope) == 0 ||
2282 (S->getFlags() & Scope::TemplateParamScope) != 0)
2283 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Douglas Gregorddc29e12009-02-06 22:42:48 +00002285 // C++ [temp]p2:
2286 // A template-declaration can appear only as a namespace scope or
2287 // class scope declaration.
2288 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00002289 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2290 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00002291 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00002292 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Eli Friedman1503f772009-07-31 01:43:05 +00002294 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002295 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002296
2297 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2298 return false;
2299
Mike Stump1eb44332009-09-09 15:08:12 +00002300 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002301 diag::err_template_outside_namespace_or_class_scope)
2302 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002303}
Douglas Gregorcc636682009-02-17 23:15:12 +00002304
Douglas Gregorff668032009-05-13 18:28:20 +00002305/// \brief Check whether a class template specialization or explicit
2306/// instantiation in the current context is well-formed.
Douglas Gregor88b70942009-02-25 22:02:03 +00002307///
Douglas Gregorff668032009-05-13 18:28:20 +00002308/// This routine determines whether a class template specialization or
Mike Stump1eb44332009-09-09 15:08:12 +00002309/// explicit instantiation can be declared in the current context
2310/// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2) and emits
2311/// appropriate diagnostics if there was an error. It returns true if
Douglas Gregorff668032009-05-13 18:28:20 +00002312// there was an error that we cannot recover from, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002313bool
Douglas Gregor88b70942009-02-25 22:02:03 +00002314Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate,
2315 ClassTemplateSpecializationDecl *PrevDecl,
2316 SourceLocation TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002317 SourceRange ScopeSpecifierRange,
Douglas Gregor16df8502009-06-12 22:21:45 +00002318 bool PartialSpecialization,
Douglas Gregorff668032009-05-13 18:28:20 +00002319 bool ExplicitInstantiation) {
Douglas Gregor88b70942009-02-25 22:02:03 +00002320 // C++ [temp.expl.spec]p2:
2321 // An explicit specialization shall be declared in the namespace
2322 // of which the template is a member, or, for member templates, in
2323 // the namespace of which the enclosing class or enclosing class
2324 // template is a member. An explicit specialization of a member
2325 // function, member class or static data member of a class
2326 // template shall be declared in the namespace of which the class
2327 // template is a member. Such a declaration may also be a
2328 // definition. If the declaration is not a definition, the
2329 // specialization may be defined later in the name- space in which
2330 // the explicit specialization was declared, or in a namespace
2331 // that encloses the one in which the explicit specialization was
2332 // declared.
2333 if (CurContext->getLookupContext()->isFunctionOrMethod()) {
Douglas Gregor16df8502009-06-12 22:21:45 +00002334 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregor88b70942009-02-25 22:02:03 +00002335 Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002336 << Kind << ClassTemplate;
Douglas Gregor88b70942009-02-25 22:02:03 +00002337 return true;
2338 }
2339
2340 DeclContext *DC = CurContext->getEnclosingNamespaceContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002341 DeclContext *TemplateContext
Douglas Gregor88b70942009-02-25 22:02:03 +00002342 = ClassTemplate->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregorff668032009-05-13 18:28:20 +00002343 if ((!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) &&
2344 !ExplicitInstantiation) {
Douglas Gregor88b70942009-02-25 22:02:03 +00002345 // There is no prior declaration of this entity, so this
2346 // specialization must be in the same context as the template
2347 // itself.
2348 if (DC != TemplateContext) {
2349 if (isa<TranslationUnitDecl>(TemplateContext))
2350 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregor16df8502009-06-12 22:21:45 +00002351 << PartialSpecialization
Douglas Gregor88b70942009-02-25 22:02:03 +00002352 << ClassTemplate << ScopeSpecifierRange;
2353 else if (isa<NamespaceDecl>(TemplateContext))
2354 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope)
Mike Stump1eb44332009-09-09 15:08:12 +00002355 << PartialSpecialization << ClassTemplate
Douglas Gregor16df8502009-06-12 22:21:45 +00002356 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Douglas Gregor88b70942009-02-25 22:02:03 +00002357
2358 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
2359 }
2360
2361 return false;
2362 }
2363
2364 // We have a previous declaration of this entity. Make sure that
2365 // this redeclaration (or definition) occurs in an enclosing namespace.
2366 if (!CurContext->Encloses(TemplateContext)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002367 // FIXME: In C++98, we would like to turn these errors into warnings,
2368 // dependent on a -Wc++0x flag.
Douglas Gregorff668032009-05-13 18:28:20 +00002369 bool SuppressedDiag = false;
Douglas Gregor16df8502009-06-12 22:21:45 +00002370 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregorff668032009-05-13 18:28:20 +00002371 if (isa<TranslationUnitDecl>(TemplateContext)) {
2372 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2373 Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002374 << Kind << ClassTemplate << ScopeSpecifierRange;
Douglas Gregorff668032009-05-13 18:28:20 +00002375 else
2376 SuppressedDiag = true;
2377 } else if (isa<NamespaceDecl>(TemplateContext)) {
2378 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2379 Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002380 << Kind << ClassTemplate
Douglas Gregorff668032009-05-13 18:28:20 +00002381 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Mike Stump1eb44332009-09-09 15:08:12 +00002382 else
Douglas Gregorff668032009-05-13 18:28:20 +00002383 SuppressedDiag = true;
2384 }
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Douglas Gregorff668032009-05-13 18:28:20 +00002386 if (!SuppressedDiag)
2387 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
Douglas Gregor88b70942009-02-25 22:02:03 +00002388 }
2389
2390 return false;
2391}
2392
Douglas Gregore94866f2009-06-12 21:21:02 +00002393/// \brief Check the non-type template arguments of a class template
2394/// partial specialization according to C++ [temp.class.spec]p9.
2395///
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002396/// \param TemplateParams the template parameters of the primary class
2397/// template.
2398///
2399/// \param TemplateArg the template arguments of the class template
2400/// partial specialization.
2401///
2402/// \param MirrorsPrimaryTemplate will be set true if the class
2403/// template partial specialization arguments are identical to the
2404/// implicit template arguments of the primary template. This is not
2405/// necessarily an error (C++0x), and it is left to the caller to diagnose
2406/// this condition when it is an error.
2407///
Douglas Gregore94866f2009-06-12 21:21:02 +00002408/// \returns true if there was an error, false otherwise.
2409bool Sema::CheckClassTemplatePartialSpecializationArgs(
2410 TemplateParameterList *TemplateParams,
Anders Carlsson6360be72009-06-13 18:20:51 +00002411 const TemplateArgumentListBuilder &TemplateArgs,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002412 bool &MirrorsPrimaryTemplate) {
Douglas Gregore94866f2009-06-12 21:21:02 +00002413 // FIXME: the interface to this function will have to change to
2414 // accommodate variadic templates.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002415 MirrorsPrimaryTemplate = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Anders Carlssonfb250522009-06-23 01:26:57 +00002417 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Douglas Gregore94866f2009-06-12 21:21:02 +00002419 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002420 // Determine whether the template argument list of the partial
2421 // specialization is identical to the implicit argument list of
2422 // the primary template. The caller may need to diagnostic this as
2423 // an error per C++ [temp.class.spec]p9b3.
2424 if (MirrorsPrimaryTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00002425 if (TemplateTypeParmDecl *TTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002426 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
2427 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
Anders Carlsson6360be72009-06-13 18:20:51 +00002428 Context.getCanonicalType(ArgList[I].getAsType()))
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002429 MirrorsPrimaryTemplate = false;
2430 } else if (TemplateTemplateParmDecl *TTP
2431 = dyn_cast<TemplateTemplateParmDecl>(
2432 TemplateParams->getParam(I))) {
2433 // FIXME: We should settle on either Declaration storage or
2434 // Expression storage for template template parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002435 TemplateTemplateParmDecl *ArgDecl
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002436 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Anders Carlsson6360be72009-06-13 18:20:51 +00002437 ArgList[I].getAsDecl());
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002438 if (!ArgDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002439 if (DeclRefExpr *DRE
Anders Carlsson6360be72009-06-13 18:20:51 +00002440 = dyn_cast_or_null<DeclRefExpr>(ArgList[I].getAsExpr()))
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002441 ArgDecl = dyn_cast<TemplateTemplateParmDecl>(DRE->getDecl());
2442
2443 if (!ArgDecl ||
2444 ArgDecl->getIndex() != TTP->getIndex() ||
2445 ArgDecl->getDepth() != TTP->getDepth())
2446 MirrorsPrimaryTemplate = false;
2447 }
2448 }
2449
Mike Stump1eb44332009-09-09 15:08:12 +00002450 NonTypeTemplateParmDecl *Param
Douglas Gregore94866f2009-06-12 21:21:02 +00002451 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002452 if (!Param) {
Douglas Gregore94866f2009-06-12 21:21:02 +00002453 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002454 }
2455
Anders Carlsson6360be72009-06-13 18:20:51 +00002456 Expr *ArgExpr = ArgList[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002457 if (!ArgExpr) {
2458 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00002459 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002460 }
Douglas Gregore94866f2009-06-12 21:21:02 +00002461
2462 // C++ [temp.class.spec]p8:
2463 // A non-type argument is non-specialized if it is the name of a
2464 // non-type parameter. All other non-type arguments are
2465 // specialized.
2466 //
2467 // Below, we check the two conditions that only apply to
2468 // specialized non-type arguments, so skip any non-specialized
2469 // arguments.
2470 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Mike Stump1eb44332009-09-09 15:08:12 +00002471 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002472 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002473 if (MirrorsPrimaryTemplate &&
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002474 (Param->getIndex() != NTTP->getIndex() ||
2475 Param->getDepth() != NTTP->getDepth()))
2476 MirrorsPrimaryTemplate = false;
2477
Douglas Gregore94866f2009-06-12 21:21:02 +00002478 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002479 }
Douglas Gregore94866f2009-06-12 21:21:02 +00002480
2481 // C++ [temp.class.spec]p9:
2482 // Within the argument list of a class template partial
2483 // specialization, the following restrictions apply:
2484 // -- A partially specialized non-type argument expression
2485 // shall not involve a template parameter of the partial
2486 // specialization except when the argument expression is a
2487 // simple identifier.
2488 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002489 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00002490 diag::err_dependent_non_type_arg_in_partial_spec)
2491 << ArgExpr->getSourceRange();
2492 return true;
2493 }
2494
2495 // -- The type of a template parameter corresponding to a
2496 // specialized non-type argument shall not be dependent on a
2497 // parameter of the specialization.
2498 if (Param->getType()->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002499 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00002500 diag::err_dependent_typed_non_type_arg_in_partial_spec)
2501 << Param->getType()
2502 << ArgExpr->getSourceRange();
2503 Diag(Param->getLocation(), diag::note_template_param_here);
2504 return true;
2505 }
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002506
2507 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00002508 }
2509
2510 return false;
2511}
2512
Douglas Gregor212e81c2009-03-25 00:13:59 +00002513Sema::DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00002514Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
2515 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00002516 SourceLocation KWLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +00002517 const CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002518 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00002519 SourceLocation TemplateNameLoc,
2520 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00002521 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00002522 SourceLocation *TemplateArgLocs,
2523 SourceLocation RAngleLoc,
2524 AttributeList *Attr,
2525 MultiTemplateParamsArg TemplateParameterLists) {
John McCallf1bbbb42009-09-04 01:14:41 +00002526 assert(TUK == TUK_Declaration || TUK == TUK_Definition);
2527
Douglas Gregorcc636682009-02-17 23:15:12 +00002528 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00002529 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00002530 ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002531 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
Douglas Gregorcc636682009-02-17 23:15:12 +00002532
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002533 bool isPartialSpecialization = false;
2534
Douglas Gregor88b70942009-02-25 22:02:03 +00002535 // Check the validity of the template headers that introduce this
2536 // template.
Douglas Gregor05396e22009-08-25 17:23:04 +00002537 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00002538 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
2539 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002540 TemplateParameterLists.size());
2541 if (TemplateParams && TemplateParams->size() > 0) {
2542 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00002543
Douglas Gregor05396e22009-08-25 17:23:04 +00002544 // C++ [temp.class.spec]p10:
2545 // The template parameter list of a specialization shall not
2546 // contain default template argument values.
2547 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2548 Decl *Param = TemplateParams->getParam(I);
2549 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
2550 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002551 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002552 diag::err_default_arg_in_partial_spec);
2553 TTP->setDefaultArgument(QualType(), SourceLocation(), false);
2554 }
2555 } else if (NonTypeTemplateParmDecl *NTTP
2556 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2557 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002558 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002559 diag::err_default_arg_in_partial_spec)
2560 << DefArg->getSourceRange();
2561 NTTP->setDefaultArgument(0);
2562 DefArg->Destroy(Context);
2563 }
2564 } else {
2565 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
2566 if (Expr *DefArg = TTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002567 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002568 diag::err_default_arg_in_partial_spec)
2569 << DefArg->getSourceRange();
2570 TTP->setDefaultArgument(0);
2571 DefArg->Destroy(Context);
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002572 }
2573 }
2574 }
Douglas Gregor05396e22009-08-25 17:23:04 +00002575 } else if (!TemplateParams)
2576 Diag(KWLoc, diag::err_template_spec_needs_header)
2577 << CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor88b70942009-02-25 22:02:03 +00002578
Douglas Gregorcc636682009-02-17 23:15:12 +00002579 // Check that the specialization uses the same tag kind as the
2580 // original template.
2581 TagDecl::TagKind Kind;
2582 switch (TagSpec) {
2583 default: assert(0 && "Unknown tag type!");
2584 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2585 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2586 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2587 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002588 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00002589 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002590 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002591 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00002592 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00002593 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00002594 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00002595 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00002596 diag::note_previous_use);
2597 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2598 }
2599
Douglas Gregor40808ce2009-03-09 23:48:35 +00002600 // Translate the parser's template argument list in our AST format.
2601 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2602 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2603
Douglas Gregorcc636682009-02-17 23:15:12 +00002604 // Check that the template argument list is well-formed for this
2605 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00002606 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2607 TemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00002608 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson6360be72009-06-13 18:20:51 +00002609 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregor16134c62009-07-01 00:28:38 +00002610 RAngleLoc, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00002611 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00002612
Mike Stump1eb44332009-09-09 15:08:12 +00002613 assert((Converted.structuredSize() ==
Douglas Gregorcc636682009-02-17 23:15:12 +00002614 ClassTemplate->getTemplateParameters()->size()) &&
2615 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002617 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00002618 // corresponds to these arguments.
2619 llvm::FoldingSetNodeID ID;
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002620 if (isPartialSpecialization) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002621 bool MirrorsPrimaryTemplate;
Douglas Gregore94866f2009-06-12 21:21:02 +00002622 if (CheckClassTemplatePartialSpecializationArgs(
2623 ClassTemplate->getTemplateParameters(),
Anders Carlssonfb250522009-06-23 01:26:57 +00002624 Converted, MirrorsPrimaryTemplate))
Douglas Gregore94866f2009-06-12 21:21:02 +00002625 return true;
2626
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002627 if (MirrorsPrimaryTemplate) {
2628 // C++ [temp.class.spec]p9b3:
2629 //
Mike Stump1eb44332009-09-09 15:08:12 +00002630 // -- The argument list of the specialization shall not be identical
2631 // to the implicit argument list of the primary template.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002632 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
John McCall0f434ec2009-07-31 02:45:11 +00002633 << (TUK == TUK_Definition)
Mike Stump1eb44332009-09-09 15:08:12 +00002634 << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002635 RAngleLoc));
John McCall0f434ec2009-07-31 02:45:11 +00002636 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002637 ClassTemplate->getIdentifier(),
2638 TemplateNameLoc,
2639 Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +00002640 TemplateParams,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002641 AS_none);
2642 }
2643
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002644 // FIXME: Template parameter list matters, too
Mike Stump1eb44332009-09-09 15:08:12 +00002645 ClassTemplatePartialSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002646 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002647 Converted.flatSize(),
2648 Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002649 } else
Anders Carlsson1c5976e2009-06-05 03:43:12 +00002650 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002651 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002652 Converted.flatSize(),
2653 Context);
Douglas Gregorcc636682009-02-17 23:15:12 +00002654 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002655 ClassTemplateSpecializationDecl *PrevDecl = 0;
2656
2657 if (isPartialSpecialization)
2658 PrevDecl
Mike Stump1eb44332009-09-09 15:08:12 +00002659 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002660 InsertPos);
2661 else
2662 PrevDecl
2663 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00002664
2665 ClassTemplateSpecializationDecl *Specialization = 0;
2666
Douglas Gregor88b70942009-02-25 22:02:03 +00002667 // Check whether we can declare a class template specialization in
2668 // the current scope.
2669 if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00002670 TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002671 SS.getRange(),
Douglas Gregor16df8502009-06-12 22:21:45 +00002672 isPartialSpecialization,
Douglas Gregorff668032009-05-13 18:28:20 +00002673 /*ExplicitInstantiation=*/false))
Douglas Gregor212e81c2009-03-25 00:13:59 +00002674 return true;
Douglas Gregor88b70942009-02-25 22:02:03 +00002675
Douglas Gregorb88e8882009-07-30 17:40:51 +00002676 // The canonical type
2677 QualType CanonType;
Douglas Gregorcc636682009-02-17 23:15:12 +00002678 if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
2679 // Since the only prior class template specialization with these
2680 // arguments was referenced but not declared, reuse that
2681 // declaration node as our own, updating its source location to
2682 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00002683 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00002684 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00002685 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00002686 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002687 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00002688 // Build the canonical type that describes the converted template
2689 // arguments of the class template partial specialization.
2690 CanonType = Context.getTemplateSpecializationType(
2691 TemplateName(ClassTemplate),
2692 Converted.getFlatArguments(),
2693 Converted.flatSize());
2694
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002695 // Create a new class template partial specialization declaration node.
Mike Stump1eb44332009-09-09 15:08:12 +00002696 TemplateParameterList *TemplateParams
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002697 = static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
2698 ClassTemplatePartialSpecializationDecl *PrevPartial
2699 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002700 ClassTemplatePartialSpecializationDecl *Partial
2701 = ClassTemplatePartialSpecializationDecl::Create(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002702 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00002703 TemplateNameLoc,
2704 TemplateParams,
2705 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00002706 Converted,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00002707 PrevPartial);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002708
2709 if (PrevPartial) {
2710 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
2711 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
2712 } else {
2713 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
2714 }
2715 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00002716
2717 // Check that all of the template parameters of the class template
2718 // partial specialization are deducible from the template
2719 // arguments. If not, this class template partial specialization
2720 // will never be used.
2721 llvm::SmallVector<bool, 8> DeducibleParams;
2722 DeducibleParams.resize(TemplateParams->size());
2723 MarkDeducedTemplateParameters(Partial->getTemplateArgs(), DeducibleParams);
2724 unsigned NumNonDeducible = 0;
2725 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
2726 if (!DeducibleParams[I])
2727 ++NumNonDeducible;
2728
2729 if (NumNonDeducible) {
2730 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
2731 << (NumNonDeducible > 1)
2732 << SourceRange(TemplateNameLoc, RAngleLoc);
2733 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
2734 if (!DeducibleParams[I]) {
2735 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
2736 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00002737 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00002738 diag::note_partial_spec_unused_parameter)
2739 << Param->getDeclName();
2740 else
Mike Stump1eb44332009-09-09 15:08:12 +00002741 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00002742 diag::note_partial_spec_unused_parameter)
2743 << std::string("<anonymous>");
2744 }
2745 }
2746 }
Douglas Gregorcc636682009-02-17 23:15:12 +00002747 } else {
2748 // Create a new class template specialization declaration node for
2749 // this explicit specialization.
2750 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00002751 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorcc636682009-02-17 23:15:12 +00002752 ClassTemplate->getDeclContext(),
2753 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002754 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00002755 Converted,
Douglas Gregorcc636682009-02-17 23:15:12 +00002756 PrevDecl);
2757
2758 if (PrevDecl) {
2759 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
2760 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
2761 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002762 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregorcc636682009-02-17 23:15:12 +00002763 InsertPos);
2764 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00002765
2766 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00002767 }
2768
2769 // Note that this is an explicit specialization.
2770 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
2771
2772 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00002773 if (TUK == TUK_Definition) {
Douglas Gregorcc636682009-02-17 23:15:12 +00002774 if (RecordDecl *Def = Specialization->getDefinition(Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002775 // FIXME: Should also handle explicit specialization after implicit
2776 // instantiation with a special diagnostic.
Douglas Gregorcc636682009-02-17 23:15:12 +00002777 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002778 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002779 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00002780 Diag(Def->getLocation(), diag::note_previous_definition);
2781 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00002782 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00002783 }
2784 }
2785
Douglas Gregorfc705b82009-02-26 22:19:44 +00002786 // Build the fully-sugared type for this class template
2787 // specialization as the user wrote in the specialization
2788 // itself. This means that we'll pretty-print the type retrieved
2789 // from the specialization's declaration the way that the user
2790 // actually wrote the specialization, rather than formatting the
2791 // name based on the "canonical" representation used to store the
2792 // template arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002793 QualType WrittenTy
2794 = Context.getTemplateSpecializationType(Name,
Anders Carlsson6360be72009-06-13 18:20:51 +00002795 TemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +00002796 TemplateArgs.size(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00002797 CanonType);
Douglas Gregor7532dc62009-03-30 22:58:21 +00002798 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002799 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00002800
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00002801 // C++ [temp.expl.spec]p9:
2802 // A template explicit specialization is in the scope of the
2803 // namespace in which the template was defined.
2804 //
2805 // We actually implement this paragraph where we set the semantic
2806 // context (in the creation of the ClassTemplateSpecializationDecl),
2807 // but we also maintain the lexical context where the actual
2808 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00002809 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00002810
Douglas Gregorcc636682009-02-17 23:15:12 +00002811 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00002812 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00002813 Specialization->startDefinition();
2814
2815 // Add the specialization into its lexical context, so that it can
2816 // be seen when iterating through the list of declarations in that
2817 // context. However, specializations are not found by name lookup.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002818 CurContext->addDecl(Specialization);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002819 return DeclPtrTy::make(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00002820}
Douglas Gregord57959a2009-03-27 23:10:48 +00002821
Mike Stump1eb44332009-09-09 15:08:12 +00002822Sema::DeclPtrTy
2823Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00002824 MultiTemplateParamsArg TemplateParameterLists,
2825 Declarator &D) {
2826 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
2827}
2828
Mike Stump1eb44332009-09-09 15:08:12 +00002829Sema::DeclPtrTy
2830Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00002831 MultiTemplateParamsArg TemplateParameterLists,
2832 Declarator &D) {
2833 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
2834 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
2835 "Not a function declarator!");
2836 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Douglas Gregor52591bf2009-06-24 00:54:41 +00002838 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00002839 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00002840 }
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Douglas Gregor52591bf2009-06-24 00:54:41 +00002842 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00002843
2844 DeclPtrTy DP = HandleDeclarator(ParentScope, D,
Douglas Gregor52591bf2009-06-24 00:54:41 +00002845 move(TemplateParameterLists),
2846 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00002847 if (FunctionTemplateDecl *FunctionTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +00002848 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
Mike Stump1eb44332009-09-09 15:08:12 +00002849 return ActOnStartOfFunctionDef(FnBodyScope,
Douglas Gregore53060f2009-06-25 22:08:12 +00002850 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
Douglas Gregorf59a56e2009-07-21 23:53:31 +00002851 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
2852 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
Douglas Gregore53060f2009-06-25 22:08:12 +00002853 return DeclPtrTy();
Douglas Gregor52591bf2009-06-24 00:54:41 +00002854}
2855
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00002856// Explicit instantiation of a class template specialization
Douglas Gregor45f96552009-09-04 06:33:52 +00002857// FIXME: Implement extern template semantics
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002858Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00002859Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00002860 SourceLocation ExternLoc,
2861 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002862 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002863 SourceLocation KWLoc,
2864 const CXXScopeSpec &SS,
2865 TemplateTy TemplateD,
2866 SourceLocation TemplateNameLoc,
2867 SourceLocation LAngleLoc,
2868 ASTTemplateArgsPtr TemplateArgsIn,
2869 SourceLocation *TemplateArgLocs,
2870 SourceLocation RAngleLoc,
2871 AttributeList *Attr) {
2872 // Find the class template we're specializing
2873 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00002874 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002875 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
2876
2877 // Check that the specialization uses the same tag kind as the
2878 // original template.
2879 TagDecl::TagKind Kind;
2880 switch (TagSpec) {
2881 default: assert(0 && "Unknown tag type!");
2882 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2883 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2884 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2885 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002886 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00002887 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002888 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002889 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002890 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00002891 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002892 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00002893 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002894 diag::note_previous_use);
2895 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2896 }
2897
Douglas Gregorff668032009-05-13 18:28:20 +00002898 // C++0x [temp.explicit]p2:
2899 // [...] An explicit instantiation shall appear in an enclosing
2900 // namespace of its template. [...]
2901 //
2902 // This is C++ DR 275.
2903 if (CheckClassTemplateSpecializationScope(ClassTemplate, 0,
Mike Stump1eb44332009-09-09 15:08:12 +00002904 TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002905 SS.getRange(),
Douglas Gregor16df8502009-06-12 22:21:45 +00002906 /*PartialSpecialization=*/false,
Douglas Gregorff668032009-05-13 18:28:20 +00002907 /*ExplicitInstantiation=*/true))
2908 return true;
2909
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002910 // Translate the parser's template argument list in our AST format.
2911 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2912 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2913
2914 // Check that the template argument list is well-formed for this
2915 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00002916 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2917 TemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00002918 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson9bff9a92009-06-05 02:12:32 +00002919 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregor16134c62009-07-01 00:28:38 +00002920 RAngleLoc, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002921 return true;
2922
Mike Stump1eb44332009-09-09 15:08:12 +00002923 assert((Converted.structuredSize() ==
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002924 ClassTemplate->getTemplateParameters()->size()) &&
2925 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00002926
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002927 // Find the class template specialization declaration that
2928 // corresponds to these arguments.
2929 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00002930 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002931 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002932 Converted.flatSize(),
2933 Context);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002934 void *InsertPos = 0;
2935 ClassTemplateSpecializationDecl *PrevDecl
2936 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
2937
2938 ClassTemplateSpecializationDecl *Specialization = 0;
2939
Douglas Gregorff668032009-05-13 18:28:20 +00002940 bool SpecializationRequiresInstantiation = true;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002941 if (PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002942 if (PrevDecl->getSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002943 == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002944 // This particular specialization has already been declared or
2945 // instantiated. We cannot explicitly instantiate it.
Douglas Gregorff668032009-05-13 18:28:20 +00002946 Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate)
2947 << Context.getTypeDeclType(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002948 Diag(PrevDecl->getLocation(),
Douglas Gregorff668032009-05-13 18:28:20 +00002949 diag::note_previous_explicit_instantiation);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002950 return DeclPtrTy::make(PrevDecl);
2951 }
2952
Douglas Gregorff668032009-05-13 18:28:20 +00002953 if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00002954 // C++ DR 259, C++0x [temp.explicit]p4:
Douglas Gregorff668032009-05-13 18:28:20 +00002955 // For a given set of template parameters, if an explicit
2956 // instantiation of a template appears after a declaration of
2957 // an explicit specialization for that template, the explicit
2958 // instantiation has no effect.
2959 if (!getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00002960 Diag(TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002961 diag::ext_explicit_instantiation_after_specialization)
2962 << Context.getTypeDeclType(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002963 Diag(PrevDecl->getLocation(),
Douglas Gregorff668032009-05-13 18:28:20 +00002964 diag::note_previous_template_specialization);
2965 }
2966
2967 // Create a new class template specialization declaration node
2968 // for this explicit specialization. This node is only used to
2969 // record the existence of this explicit instantiation for
2970 // accurate reproduction of the source code; we don't actually
2971 // use it for anything, since it is semantically irrelevant.
2972 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00002973 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorff668032009-05-13 18:28:20 +00002974 ClassTemplate->getDeclContext(),
2975 TemplateNameLoc,
2976 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00002977 Converted, 0);
Douglas Gregorff668032009-05-13 18:28:20 +00002978 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002979 CurContext->addDecl(Specialization);
Douglas Gregorff668032009-05-13 18:28:20 +00002980 return DeclPtrTy::make(Specialization);
2981 }
2982
2983 // If we have already (implicitly) instantiated this
2984 // specialization, there is less work to do.
2985 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
2986 SpecializationRequiresInstantiation = false;
2987
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002988 // Since the only prior class template specialization with these
2989 // arguments was referenced but not declared, reuse that
2990 // declaration node as our own, updating its source location to
2991 // reflect our new declaration.
2992 Specialization = PrevDecl;
2993 Specialization->setLocation(TemplateNameLoc);
2994 PrevDecl = 0;
2995 } else {
2996 // Create a new class template specialization declaration node for
2997 // this explicit specialization.
2998 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00002999 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003000 ClassTemplate->getDeclContext(),
3001 TemplateNameLoc,
3002 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00003003 Converted, 0);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003004
Mike Stump1eb44332009-09-09 15:08:12 +00003005 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003006 InsertPos);
3007 }
3008
3009 // Build the fully-sugared type for this explicit instantiation as
3010 // the user wrote in the explicit instantiation itself. This means
3011 // that we'll pretty-print the type retrieved from the
3012 // specialization's declaration the way that the user actually wrote
3013 // the explicit instantiation, rather than formatting the name based
3014 // on the "canonical" representation used to store the template
3015 // arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00003016 QualType WrittenTy
3017 = Context.getTemplateSpecializationType(Name,
Anders Carlssonf4e2a2c2009-06-05 02:45:24 +00003018 TemplateArgs.data(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003019 TemplateArgs.size(),
3020 Context.getTypeDeclType(Specialization));
3021 Specialization->setTypeAsWritten(WrittenTy);
3022 TemplateArgsIn.release();
3023
3024 // Add the explicit instantiation into its lexical context. However,
3025 // since explicit instantiations are never found by name lookup, we
3026 // just put it into the declaration context directly.
3027 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003028 CurContext->addDecl(Specialization);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003029
3030 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003031 // A definition of a class template or class member template
3032 // shall be in scope at the point of the explicit instantiation of
3033 // the class template or class member template.
3034 //
3035 // This check comes when we actually try to perform the
3036 // instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003037 TemplateSpecializationKind TSK
Mike Stump1eb44332009-09-09 15:08:12 +00003038 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003039 : TSK_ExplicitInstantiationDeclaration;
Douglas Gregore2c31ff2009-05-15 17:59:04 +00003040 if (SpecializationRequiresInstantiation)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003041 InstantiateClassTemplateSpecialization(Specialization, TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00003042 else // Instantiate the members of this class template specialization.
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003043 InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization,
3044 TSK);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003045
3046 return DeclPtrTy::make(Specialization);
3047}
3048
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003049// Explicit instantiation of a member class of a class template.
3050Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00003051Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00003052 SourceLocation ExternLoc,
3053 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00003054 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003055 SourceLocation KWLoc,
3056 const CXXScopeSpec &SS,
3057 IdentifierInfo *Name,
3058 SourceLocation NameLoc,
3059 AttributeList *Attr) {
3060
Douglas Gregor402abb52009-05-28 23:31:59 +00003061 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003062 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00003063 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
Douglas Gregor7cdbc582009-07-22 23:48:44 +00003064 KWLoc, SS, Name, NameLoc, Attr, AS_none,
John McCallc4e70192009-09-11 04:59:25 +00003065 MultiTemplateParamsArg(*this, 0, 0),
3066 Owned, IsDependent);
3067 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
3068
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003069 if (!TagD)
3070 return true;
3071
3072 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
3073 if (Tag->isEnum()) {
3074 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
3075 << Context.getTypeDeclType(Tag);
3076 return true;
3077 }
3078
Douglas Gregord0c87372009-05-27 17:30:49 +00003079 if (Tag->isInvalidDecl())
3080 return true;
3081
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003082 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
3083 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
3084 if (!Pattern) {
3085 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
3086 << Context.getTypeDeclType(Record);
3087 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
3088 return true;
3089 }
3090
3091 // C++0x [temp.explicit]p2:
3092 // [...] An explicit instantiation shall appear in an enclosing
3093 // namespace of its template. [...]
3094 //
3095 // This is C++ DR 275.
3096 if (getLangOptions().CPlusPlus0x) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003097 // FIXME: In C++98, we would like to turn these errors into warnings,
3098 // dependent on a -Wc++0x flag.
Mike Stump1eb44332009-09-09 15:08:12 +00003099 DeclContext *PatternContext
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003100 = Pattern->getDeclContext()->getEnclosingNamespaceContext();
3101 if (!CurContext->Encloses(PatternContext)) {
3102 Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope)
3103 << Record << cast<NamedDecl>(PatternContext) << SS.getRange();
3104 Diag(Pattern->getLocation(), diag::note_previous_declaration);
3105 }
3106 }
3107
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003108 TemplateSpecializationKind TSK
Mike Stump1eb44332009-09-09 15:08:12 +00003109 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003110 : TSK_ExplicitInstantiationDeclaration;
Mike Stump1eb44332009-09-09 15:08:12 +00003111
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003112 if (!Record->getDefinition(Context)) {
3113 // If the class has a definition, instantiate it (and all of its
3114 // members, recursively).
3115 Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
Mike Stump1eb44332009-09-09 15:08:12 +00003116 if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern,
Douglas Gregor54dabfc2009-05-14 23:26:13 +00003117 getTemplateInstantiationArgs(Record),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003118 TSK))
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003119 return true;
John McCallce3ff2b2009-08-25 22:02:44 +00003120 } else // Instantiate all of the members of the class.
Mike Stump1eb44332009-09-09 15:08:12 +00003121 InstantiateClassMembers(TemplateLoc, Record,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003122 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003123
Mike Stump390b4cc2009-05-16 07:39:55 +00003124 // FIXME: We don't have any representation for explicit instantiations of
3125 // member classes. Such a representation is not needed for compilation, but it
3126 // should be available for clients that want to see all of the declarations in
3127 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003128 return TagD;
3129}
3130
Douglas Gregord57959a2009-03-27 23:10:48 +00003131Sema::TypeResult
John McCallc4e70192009-09-11 04:59:25 +00003132Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
3133 const CXXScopeSpec &SS, IdentifierInfo *Name,
3134 SourceLocation TagLoc, SourceLocation NameLoc) {
3135 // This has to hold, because SS is expected to be defined.
3136 assert(Name && "Expected a name in a dependent tag");
3137
3138 NestedNameSpecifier *NNS
3139 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3140 if (!NNS)
3141 return true;
3142
3143 QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc));
3144 if (T.isNull())
3145 return true;
3146
3147 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
3148 QualType ElabType = Context.getElaboratedType(T, TagKind);
3149
3150 return ElabType.getAsOpaquePtr();
3151}
3152
3153Sema::TypeResult
Douglas Gregord57959a2009-03-27 23:10:48 +00003154Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3155 const IdentifierInfo &II, SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003156 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00003157 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3158 if (!NNS)
3159 return true;
3160
3161 QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +00003162 if (T.isNull())
3163 return true;
Douglas Gregord57959a2009-03-27 23:10:48 +00003164 return T.getAsOpaquePtr();
3165}
3166
Douglas Gregor17343172009-04-01 00:28:59 +00003167Sema::TypeResult
3168Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3169 SourceLocation TemplateLoc, TypeTy *Ty) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00003170 QualType T = GetTypeFromParser(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00003171 NestedNameSpecifier *NNS
Douglas Gregor17343172009-04-01 00:28:59 +00003172 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump1eb44332009-09-09 15:08:12 +00003173 const TemplateSpecializationType *TemplateId
Douglas Gregor17343172009-04-01 00:28:59 +00003174 = T->getAsTemplateSpecializationType();
3175 assert(TemplateId && "Expected a template specialization type");
3176
Douglas Gregor6946baf2009-09-02 13:05:45 +00003177 if (computeDeclContext(SS, false)) {
3178 // If we can compute a declaration context, then the "typename"
3179 // keyword was superfluous. Just build a QualifiedNameType to keep
3180 // track of the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +00003181
Douglas Gregor6946baf2009-09-02 13:05:45 +00003182 // FIXME: Note that the QualifiedNameType had the "typename" keyword!
3183 return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
3184 }
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Douglas Gregor6946baf2009-09-02 13:05:45 +00003186 return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
Douglas Gregor17343172009-04-01 00:28:59 +00003187}
3188
Douglas Gregord57959a2009-03-27 23:10:48 +00003189/// \brief Build the type that describes a C++ typename specifier,
3190/// e.g., "typename T::type".
3191QualType
3192Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
3193 SourceRange Range) {
Douglas Gregor42af25f2009-05-11 19:58:34 +00003194 CXXRecordDecl *CurrentInstantiation = 0;
3195 if (NNS->isDependent()) {
3196 CurrentInstantiation = getCurrentInstantiationOf(NNS);
Douglas Gregord57959a2009-03-27 23:10:48 +00003197
Douglas Gregor42af25f2009-05-11 19:58:34 +00003198 // If the nested-name-specifier does not refer to the current
3199 // instantiation, then build a typename type.
3200 if (!CurrentInstantiation)
3201 return Context.getTypenameType(NNS, &II);
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Douglas Gregorde18d122009-09-02 13:12:51 +00003203 // The nested-name-specifier refers to the current instantiation, so the
3204 // "typename" keyword itself is superfluous. In C++03, the program is
Mike Stump1eb44332009-09-09 15:08:12 +00003205 // actually ill-formed. However, DR 382 (in C++0x CD1) allows such
Douglas Gregorde18d122009-09-02 13:12:51 +00003206 // extraneous "typename" keywords, and we retroactively apply this DR to
3207 // C++03 code.
Douglas Gregor42af25f2009-05-11 19:58:34 +00003208 }
Douglas Gregord57959a2009-03-27 23:10:48 +00003209
Douglas Gregor42af25f2009-05-11 19:58:34 +00003210 DeclContext *Ctx = 0;
3211
3212 if (CurrentInstantiation)
3213 Ctx = CurrentInstantiation;
3214 else {
3215 CXXScopeSpec SS;
3216 SS.setScopeRep(NNS);
3217 SS.setRange(Range);
3218 if (RequireCompleteDeclContext(SS))
3219 return QualType();
3220
3221 Ctx = computeDeclContext(SS);
3222 }
Douglas Gregord57959a2009-03-27 23:10:48 +00003223 assert(Ctx && "No declaration context?");
3224
3225 DeclarationName Name(&II);
Mike Stump1eb44332009-09-09 15:08:12 +00003226 LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName,
Douglas Gregord57959a2009-03-27 23:10:48 +00003227 false);
3228 unsigned DiagID = 0;
3229 Decl *Referenced = 0;
3230 switch (Result.getKind()) {
3231 case LookupResult::NotFound:
3232 if (Ctx->isTranslationUnit())
3233 DiagID = diag::err_typename_nested_not_found_global;
3234 else
3235 DiagID = diag::err_typename_nested_not_found;
3236 break;
3237
3238 case LookupResult::Found:
3239 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) {
3240 // We found a type. Build a QualifiedNameType, since the
3241 // typename-specifier was just sugar. FIXME: Tell
3242 // QualifiedNameType that it has a "typename" prefix.
3243 return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
3244 }
3245
3246 DiagID = diag::err_typename_nested_not_type;
3247 Referenced = Result.getAsDecl();
3248 break;
3249
3250 case LookupResult::FoundOverloaded:
3251 DiagID = diag::err_typename_nested_not_type;
3252 Referenced = *Result.begin();
3253 break;
3254
3255 case LookupResult::AmbiguousBaseSubobjectTypes:
3256 case LookupResult::AmbiguousBaseSubobjects:
3257 case LookupResult::AmbiguousReference:
3258 DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range);
3259 return QualType();
3260 }
3261
3262 // If we get here, it's because name lookup did not find a
3263 // type. Emit an appropriate diagnostic and return an error.
3264 if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx))
3265 Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx;
3266 else
3267 Diag(Range.getEnd(), DiagID) << Range << Name;
3268 if (Referenced)
3269 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
3270 << Name;
3271 return QualType();
3272}
Douglas Gregor4a959d82009-08-06 16:20:37 +00003273
3274namespace {
3275 // See Sema::RebuildTypeInCurrentInstantiation
Mike Stump1eb44332009-09-09 15:08:12 +00003276 class VISIBILITY_HIDDEN CurrentInstantiationRebuilder
3277 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00003278 SourceLocation Loc;
3279 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Douglas Gregor4a959d82009-08-06 16:20:37 +00003281 public:
Mike Stump1eb44332009-09-09 15:08:12 +00003282 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00003283 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00003284 DeclarationName Entity)
3285 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00003286 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00003287
3288 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00003289 /// transformed.
3290 ///
3291 /// For the purposes of type reconstruction, a type has already been
3292 /// transformed if it is NULL or if it is not dependent.
3293 bool AlreadyTransformed(QualType T) {
3294 return T.isNull() || !T->isDependentType();
3295 }
Mike Stump1eb44332009-09-09 15:08:12 +00003296
3297 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00003298 /// rebuilt.
3299 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00003300
Douglas Gregor4a959d82009-08-06 16:20:37 +00003301 /// \brief Returns the name of the entity whose type is being rebuilt.
3302 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Douglas Gregor4a959d82009-08-06 16:20:37 +00003304 /// \brief Transforms an expression by returning the expression itself
3305 /// (an identity function).
3306 ///
3307 /// FIXME: This is completely unsafe; we will need to actually clone the
3308 /// expressions.
3309 Sema::OwningExprResult TransformExpr(Expr *E) {
3310 return getSema().Owned(E);
3311 }
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Douglas Gregor4a959d82009-08-06 16:20:37 +00003313 /// \brief Transforms a typename type by determining whether the type now
3314 /// refers to a member of the current instantiation, and then
3315 /// type-checking and building a QualifiedNameType (when possible).
3316 QualType TransformTypenameType(const TypenameType *T);
3317 };
3318}
3319
Mike Stump1eb44332009-09-09 15:08:12 +00003320QualType
Douglas Gregor4a959d82009-08-06 16:20:37 +00003321CurrentInstantiationRebuilder::TransformTypenameType(const TypenameType *T) {
3322 NestedNameSpecifier *NNS
3323 = TransformNestedNameSpecifier(T->getQualifier(),
3324 /*FIXME:*/SourceRange(getBaseLocation()));
3325 if (!NNS)
3326 return QualType();
3327
3328 // If the nested-name-specifier did not change, and we cannot compute the
3329 // context corresponding to the nested-name-specifier, then this
3330 // typename type will not change; exit early.
3331 CXXScopeSpec SS;
3332 SS.setRange(SourceRange(getBaseLocation()));
3333 SS.setScopeRep(NNS);
3334 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
3335 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003336
3337 // Rebuild the typename type, which will probably turn into a
Douglas Gregor4a959d82009-08-06 16:20:37 +00003338 // QualifiedNameType.
3339 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003340 QualType NewTemplateId
Douglas Gregor4a959d82009-08-06 16:20:37 +00003341 = TransformType(QualType(TemplateId, 0));
3342 if (NewTemplateId.isNull())
3343 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Douglas Gregor4a959d82009-08-06 16:20:37 +00003345 if (NNS == T->getQualifier() &&
3346 NewTemplateId == QualType(TemplateId, 0))
3347 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003348
Douglas Gregor4a959d82009-08-06 16:20:37 +00003349 return getDerived().RebuildTypenameType(NNS, NewTemplateId);
3350 }
Mike Stump1eb44332009-09-09 15:08:12 +00003351
Douglas Gregor4a959d82009-08-06 16:20:37 +00003352 return getDerived().RebuildTypenameType(NNS, T->getIdentifier());
3353}
3354
3355/// \brief Rebuilds a type within the context of the current instantiation.
3356///
Mike Stump1eb44332009-09-09 15:08:12 +00003357/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00003358/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00003359/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00003360/// partial specialization thereof). This routine will rebuild that type now
3361/// that we have entered the declarator's scope, which may produce different
3362/// canonical types, e.g.,
3363///
3364/// \code
3365/// template<typename T>
3366/// struct X {
3367/// typedef T* pointer;
3368/// pointer data();
3369/// };
3370///
3371/// template<typename T>
3372/// typename X<T>::pointer X<T>::data() { ... }
3373/// \endcode
3374///
3375/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
3376/// since we do not know that we can look into X<T> when we parsed the type.
3377/// This function will rebuild the type, performing the lookup of "pointer"
3378/// in X<T> and returning a QualifiedNameType whose canonical type is the same
3379/// as the canonical type of T*, allowing the return types of the out-of-line
3380/// definition and the declaration to match.
3381QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
3382 DeclarationName Name) {
3383 if (T.isNull() || !T->isDependentType())
3384 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Douglas Gregor4a959d82009-08-06 16:20:37 +00003386 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
3387 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00003388}