blob: ad9328f2cac63dd327479ab95ed69550a07ef7dc [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
John McCall05b23ea2009-09-14 21:59:20 +0000559 TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec);
560 assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000561
562 // There is no such thing as an unnamed class template.
563 if (!Name) {
564 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000565 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000566 }
567
568 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000569 DeclContext *SemanticContext;
570 LookupResult Previous;
571 if (SS.isNotEmpty() && !SS.isInvalid()) {
572 SemanticContext = computeDeclContext(SS, true);
573 if (!SemanticContext) {
574 // FIXME: Produce a reasonable diagnostic here
575 return true;
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
578 Previous = LookupQualifiedName(SemanticContext, Name, LookupOrdinaryName,
Douglas Gregor05396e22009-08-25 17:23:04 +0000579 true);
580 } else {
581 SemanticContext = CurContext;
582 Previous = LookupName(S, Name, LookupOrdinaryName, true);
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Douglas Gregorddc29e12009-02-06 22:42:48 +0000585 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
586 NamedDecl *PrevDecl = 0;
587 if (Previous.begin() != Previous.end())
588 PrevDecl = *Previous.begin();
589
Douglas Gregor05396e22009-08-25 17:23:04 +0000590 if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
Douglas Gregorc19ee3e2009-06-17 23:37:01 +0000591 PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Douglas Gregorddc29e12009-02-06 22:42:48 +0000593 // If there is a previous declaration with the same name, check
594 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000595 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000596 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
597 if (PrevClassTemplate) {
598 // Ensure that the template parameter lists are compatible.
599 if (!TemplateParameterListsAreEqual(TemplateParams,
600 PrevClassTemplate->getTemplateParameters(),
601 /*Complain=*/true))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000602 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000603
604 // C++ [temp.class]p4:
605 // In a redeclaration, partial specialization, explicit
606 // specialization or explicit instantiation of a class template,
607 // the class-key shall agree in kind with the original class
608 // template declaration (7.1.5.3).
609 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000610 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000611 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000612 << Name
Mike Stump1eb44332009-09-09 15:08:12 +0000613 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +0000614 PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000615 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000616 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000617 }
618
Douglas Gregorddc29e12009-02-06 22:42:48 +0000619 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000620 if (TUK == TUK_Definition) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000621 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
622 Diag(NameLoc, diag::err_redefinition) << Name;
623 Diag(Def->getLocation(), diag::note_previous_definition);
624 // FIXME: Would it make sense to try to "forget" the previous
625 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000626 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000627 }
628 }
629 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
630 // Maybe we will complain about the shadowed template parameter.
631 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
632 // Just pretend that we didn't see the previous declaration.
633 PrevDecl = 0;
634 } else if (PrevDecl) {
635 // C++ [temp]p5:
636 // A class template shall not have the same name as any other
637 // template, class, function, object, enumeration, enumerator,
638 // namespace, or type in the same scope (3.3), except as specified
639 // in (14.5.4).
640 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
641 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000642 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000643 }
644
Douglas Gregord684b002009-02-10 19:49:53 +0000645 // Check the template parameter list of this declaration, possibly
646 // merging in the template parameter list from the previous class
647 // template declaration.
648 if (CheckTemplateParameterList(TemplateParams,
649 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
650 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor7da97d02009-05-10 22:57:19 +0000652 // FIXME: If we had a scope specifier, we better have a previous template
Douglas Gregorddc29e12009-02-06 22:42:48 +0000653 // declaration!
654
John McCall05b23ea2009-09-14 21:59:20 +0000655 // If this is a friend declaration of an undeclared template,
656 // create the template in the innermost namespace scope.
657 if (TUK == TUK_Friend && !PrevClassTemplate) {
658 while (!SemanticContext->isFileContext())
659 SemanticContext = SemanticContext->getParent();
660 }
661
Mike Stump1eb44332009-09-09 15:08:12 +0000662 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000663 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000664 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000665 PrevClassTemplate->getTemplatedDecl() : 0,
666 /*DelayTypeCreation=*/true);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000667
668 ClassTemplateDecl *NewTemplate
669 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
670 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000671 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000672 NewClass->setDescribedClassTemplate(NewTemplate);
673
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000674 // Build the type for the class template declaration now.
Mike Stump1eb44332009-09-09 15:08:12 +0000675 QualType T =
676 Context.getTypeDeclType(NewClass,
677 PrevClassTemplate?
678 PrevClassTemplate->getTemplatedDecl() : 0);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000679 assert(T->isDependentType() && "Class template type is not dependent?");
680 (void)T;
681
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000682 // Set the access specifier.
John McCall05b23ea2009-09-14 21:59:20 +0000683 if (TUK == TUK_Friend)
684 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
685 PrevClassTemplate != NULL);
686 else
687 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Douglas Gregorddc29e12009-02-06 22:42:48 +0000689 // Set the lexical context of these templates
690 NewClass->setLexicalDeclContext(CurContext);
691 NewTemplate->setLexicalDeclContext(CurContext);
692
John McCall0f434ec2009-07-31 02:45:11 +0000693 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000694 NewClass->startDefinition();
695
696 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000697 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000698
John McCall05b23ea2009-09-14 21:59:20 +0000699 if (TUK != TUK_Friend)
700 PushOnScopeChains(NewTemplate, S);
701 else {
702 // We might be replacing an existing declaration in the lookup tables;
703 // if so, borrow its access specifier.
704 if (PrevClassTemplate)
705 NewTemplate->setAccess(PrevClassTemplate->getAccess());
706
707 // Friend templates are visible in fairly strange ways.
708 if (!CurContext->isDependentContext()) {
709 DeclContext *DC = SemanticContext->getLookupContext();
710 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
711 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
712 PushOnScopeChains(NewTemplate, EnclosingScope,
713 /* AddToContext = */ false);
714 }
715 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000716
Douglas Gregord684b002009-02-10 19:49:53 +0000717 if (Invalid) {
718 NewTemplate->setInvalidDecl();
719 NewClass->setInvalidDecl();
720 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000721 return DeclPtrTy::make(NewTemplate);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000722}
723
Douglas Gregord684b002009-02-10 19:49:53 +0000724/// \brief Checks the validity of a template parameter list, possibly
725/// considering the template parameter list from a previous
726/// declaration.
727///
728/// If an "old" template parameter list is provided, it must be
729/// equivalent (per TemplateParameterListsAreEqual) to the "new"
730/// template parameter list.
731///
732/// \param NewParams Template parameter list for a new template
733/// declaration. This template parameter list will be updated with any
734/// default arguments that are carried through from the previous
735/// template parameter list.
736///
737/// \param OldParams If provided, template parameter list from a
738/// previous declaration of the same template. Default template
739/// arguments will be merged from the old template parameter list to
740/// the new template parameter list.
741///
742/// \returns true if an error occurred, false otherwise.
743bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
744 TemplateParameterList *OldParams) {
745 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregord684b002009-02-10 19:49:53 +0000747 // C++ [temp.param]p10:
748 // The set of default template-arguments available for use with a
749 // template declaration or definition is obtained by merging the
750 // default arguments from the definition (if in scope) and all
751 // declarations in scope in the same way default function
752 // arguments are (8.3.6).
753 bool SawDefaultArgument = false;
754 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000755
Anders Carlsson49d25572009-06-12 23:20:15 +0000756 bool SawParameterPack = false;
757 SourceLocation ParameterPackLoc;
758
Mike Stump1a35fde2009-02-11 23:03:27 +0000759 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +0000760 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +0000761 if (OldParams)
762 OldParam = OldParams->begin();
763
764 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
765 NewParamEnd = NewParams->end();
766 NewParam != NewParamEnd; ++NewParam) {
767 // Variables used to diagnose redundant default arguments
768 bool RedundantDefaultArg = false;
769 SourceLocation OldDefaultLoc;
770 SourceLocation NewDefaultLoc;
771
772 // Variables used to diagnose missing default arguments
773 bool MissingDefaultArg = false;
774
Anders Carlsson49d25572009-06-12 23:20:15 +0000775 // C++0x [temp.param]p11:
776 // If a template parameter of a class template is a template parameter pack,
777 // it must be the last template parameter.
778 if (SawParameterPack) {
Mike Stump1eb44332009-09-09 15:08:12 +0000779 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +0000780 diag::err_template_param_pack_must_be_last_template_parameter);
781 Invalid = true;
782 }
783
Douglas Gregord684b002009-02-10 19:49:53 +0000784 // Merge default arguments for template type parameters.
785 if (TemplateTypeParmDecl *NewTypeParm
786 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000787 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +0000788 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Anders Carlsson49d25572009-06-12 23:20:15 +0000790 if (NewTypeParm->isParameterPack()) {
791 assert(!NewTypeParm->hasDefaultArgument() &&
792 "Parameter packs can't have a default argument!");
793 SawParameterPack = true;
794 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000795 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000796 NewTypeParm->hasDefaultArgument()) {
797 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
798 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
799 SawDefaultArgument = true;
800 RedundantDefaultArg = true;
801 PreviousDefaultArgLoc = NewDefaultLoc;
802 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
803 // Merge the default argument from the old declaration to the
804 // new declaration.
805 SawDefaultArgument = true;
806 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
807 OldTypeParm->getDefaultArgumentLoc(),
808 true);
809 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
810 } else if (NewTypeParm->hasDefaultArgument()) {
811 SawDefaultArgument = true;
812 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
813 } else if (SawDefaultArgument)
814 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000815 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +0000816 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000817 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +0000818 NonTypeTemplateParmDecl *OldNonTypeParm
819 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000820 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000821 NewNonTypeParm->hasDefaultArgument()) {
822 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
823 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
824 SawDefaultArgument = true;
825 RedundantDefaultArg = true;
826 PreviousDefaultArgLoc = NewDefaultLoc;
827 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
828 // Merge the default argument from the old declaration to the
829 // new declaration.
830 SawDefaultArgument = true;
831 // FIXME: We need to create a new kind of "default argument"
832 // expression that points to a previous template template
833 // parameter.
834 NewNonTypeParm->setDefaultArgument(
835 OldNonTypeParm->getDefaultArgument());
836 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
837 } else if (NewNonTypeParm->hasDefaultArgument()) {
838 SawDefaultArgument = true;
839 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
840 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +0000841 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000842 } else {
Douglas Gregord684b002009-02-10 19:49:53 +0000843 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +0000844 TemplateTemplateParmDecl *NewTemplateParm
845 = cast<TemplateTemplateParmDecl>(*NewParam);
846 TemplateTemplateParmDecl *OldTemplateParm
847 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000848 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +0000849 NewTemplateParm->hasDefaultArgument()) {
850 OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
851 NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
852 SawDefaultArgument = true;
853 RedundantDefaultArg = true;
854 PreviousDefaultArgLoc = NewDefaultLoc;
855 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
856 // Merge the default argument from the old declaration to the
857 // new declaration.
858 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000859 // FIXME: We need to create a new kind of "default argument" expression
860 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +0000861 NewTemplateParm->setDefaultArgument(
862 OldTemplateParm->getDefaultArgument());
863 PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
864 } else if (NewTemplateParm->hasDefaultArgument()) {
865 SawDefaultArgument = true;
866 PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
867 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +0000868 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +0000869 }
870
871 if (RedundantDefaultArg) {
872 // C++ [temp.param]p12:
873 // A template-parameter shall not be given default arguments
874 // by two different declarations in the same scope.
875 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
876 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
877 Invalid = true;
878 } else if (MissingDefaultArg) {
879 // C++ [temp.param]p11:
880 // If a template-parameter has a default template-argument,
881 // all subsequent template-parameters shall have a default
882 // template-argument supplied.
Mike Stump1eb44332009-09-09 15:08:12 +0000883 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +0000884 diag::err_template_param_default_arg_missing);
885 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
886 Invalid = true;
887 }
888
889 // If we have an old template parameter list that we're merging
890 // in, move on to the next parameter.
891 if (OldParams)
892 ++OldParam;
893 }
894
895 return Invalid;
896}
Douglas Gregorc15cb382009-02-09 23:23:08 +0000897
Mike Stump1eb44332009-09-09 15:08:12 +0000898/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000899/// specifier, returning the template parameter list that applies to the
900/// name.
901///
902/// \param DeclStartLoc the start of the declaration that has a scope
903/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +0000904///
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000905/// \param SS the scope specifier that will be matched to the given template
906/// parameter lists. This scope specifier precedes a qualified name that is
907/// being declared.
908///
909/// \param ParamLists the template parameter lists, from the outermost to the
910/// innermost template parameter lists.
911///
912/// \param NumParamLists the number of template parameter lists in ParamLists.
913///
Mike Stump1eb44332009-09-09 15:08:12 +0000914/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000915/// name that is preceded by the scope specifier @p SS. This template
916/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +0000917/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000918/// template specialization), or may be NULL (if we were's declaring isn't
919/// itself a template).
920TemplateParameterList *
921Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
922 const CXXScopeSpec &SS,
923 TemplateParameterList **ParamLists,
924 unsigned NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000925 // Find the template-ids that occur within the nested-name-specifier. These
926 // template-ids will match up with the template parameter lists.
927 llvm::SmallVector<const TemplateSpecializationType *, 4>
928 TemplateIdsInSpecifier;
929 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
930 NNS; NNS = NNS->getPrefix()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000931 if (const TemplateSpecializationType *SpecType
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000932 = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) {
933 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
934 if (!Template)
935 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek6217b802009-07-29 21:53:49 +0000937 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000938 ClassTemplateSpecializationDecl *SpecDecl
939 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
940 // If the nested name specifier refers to an explicit specialization,
941 // we don't need a template<> header.
Mike Stump1eb44332009-09-09 15:08:12 +0000942 // FIXME: revisit this approach once we cope with specialization
Douglas Gregorb88e8882009-07-30 17:40:51 +0000943 // properly.
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000944 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization)
945 continue;
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000948 TemplateIdsInSpecifier.push_back(SpecType);
949 }
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000952 // Reverse the list of template-ids in the scope specifier, so that we can
953 // more easily match up the template-ids and the template parameter lists.
954 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000956 SourceLocation FirstTemplateLoc = DeclStartLoc;
957 if (NumParamLists)
958 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000960 // Match the template-ids found in the specifier to the template parameter
961 // lists.
962 unsigned Idx = 0;
963 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
964 Idx != NumTemplateIds; ++Idx) {
Douglas Gregorb88e8882009-07-30 17:40:51 +0000965 QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
966 bool DependentTemplateId = TemplateId->isDependentType();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000967 if (Idx >= NumParamLists) {
968 // We have a template-id without a corresponding template parameter
969 // list.
970 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +0000971 // FIXME: the location information here isn't great.
972 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000973 diag::err_template_spec_needs_template_parameters)
Douglas Gregorb88e8882009-07-30 17:40:51 +0000974 << TemplateId
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000975 << SS.getRange();
976 } else {
977 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
978 << SS.getRange()
979 << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
980 "template<> ");
981 }
982 return 0;
983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000985 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +0000986 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +0000987 TemplateDecl *Template
Douglas Gregorb88e8882009-07-30 17:40:51 +0000988 = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
989
Mike Stump1eb44332009-09-09 15:08:12 +0000990 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorb88e8882009-07-30 17:40:51 +0000991 = dyn_cast<ClassTemplateDecl>(Template)) {
992 TemplateParameterList *ExpectedTemplateParams = 0;
993 // Is this template-id naming the primary template?
994 if (Context.hasSameType(TemplateId,
995 ClassTemplate->getInjectedClassNameType(Context)))
996 ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
997 // ... or a partial specialization?
998 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
999 = ClassTemplate->findPartialSpecialization(TemplateId))
1000 ExpectedTemplateParams = PartialSpec->getTemplateParameters();
1001
1002 if (ExpectedTemplateParams)
Mike Stump1eb44332009-09-09 15:08:12 +00001003 TemplateParameterListsAreEqual(ParamLists[Idx],
Douglas Gregorb88e8882009-07-30 17:40:51 +00001004 ExpectedTemplateParams,
1005 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001006 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00001007 } else if (ParamLists[Idx]->size() > 0)
Mike Stump1eb44332009-09-09 15:08:12 +00001008 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001009 diag::err_template_param_list_matches_nontemplate)
1010 << TemplateId
1011 << ParamLists[Idx]->getSourceRange();
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001014 // If there were at least as many template-ids as there were template
1015 // parameter lists, then there are no template parameter lists remaining for
1016 // the declaration itself.
1017 if (Idx >= NumParamLists)
1018 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001020 // If there were too many template parameter lists, complain about that now.
1021 if (Idx != NumParamLists - 1) {
1022 while (Idx < NumParamLists - 1) {
Mike Stump1eb44332009-09-09 15:08:12 +00001023 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001024 diag::err_template_spec_extra_headers)
1025 << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1026 ParamLists[Idx]->getRAngleLoc());
1027 ++Idx;
1028 }
1029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001031 // Return the last template parameter list, which corresponds to the
1032 // entity being declared.
1033 return ParamLists[NumParamLists - 1];
1034}
1035
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036/// \brief Translates template arguments as provided by the parser
1037/// into template arguments used by semantic analysis.
Mike Stump1eb44332009-09-09 15:08:12 +00001038static void
1039translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001040 SourceLocation *TemplateArgLocs,
1041 llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) {
1042 TemplateArgs.reserve(TemplateArgsIn.size());
1043
1044 void **Args = TemplateArgsIn.getArgs();
1045 bool *ArgIsType = TemplateArgsIn.getArgIsType();
1046 for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) {
1047 TemplateArgs.push_back(
1048 ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg],
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001049 //FIXME: Preserve type source info.
1050 Sema::GetTypeFromParser(Args[Arg]))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001051 : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg])));
1052 }
1053}
1054
Douglas Gregor7532dc62009-03-30 22:58:21 +00001055QualType Sema::CheckTemplateIdType(TemplateName Name,
1056 SourceLocation TemplateLoc,
1057 SourceLocation LAngleLoc,
1058 const TemplateArgument *TemplateArgs,
1059 unsigned NumTemplateArgs,
1060 SourceLocation RAngleLoc) {
1061 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001062 if (!Template) {
1063 // The template name does not resolve to a template, so we just
1064 // build a dependent template-id type.
Douglas Gregorc45c2322009-03-31 00:43:58 +00001065 return Context.getTemplateSpecializationType(Name, TemplateArgs,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001066 NumTemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001067 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001068
Douglas Gregor40808ce2009-03-09 23:48:35 +00001069 // Check that the template argument list is well-formed for this
1070 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00001071 TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
1072 NumTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001073 if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001074 TemplateArgs, NumTemplateArgs, RAngleLoc,
Douglas Gregor16134c62009-07-01 00:28:38 +00001075 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001076 return QualType();
1077
Mike Stump1eb44332009-09-09 15:08:12 +00001078 assert((Converted.structuredSize() ==
Douglas Gregor7532dc62009-03-30 22:58:21 +00001079 Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001080 "Converted template argument list is too short!");
1081
1082 QualType CanonType;
1083
Douglas Gregor7532dc62009-03-30 22:58:21 +00001084 if (TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor40808ce2009-03-09 23:48:35 +00001085 TemplateArgs,
1086 NumTemplateArgs)) {
1087 // This class template specialization is a dependent
1088 // type. Therefore, its canonical type is another class template
1089 // specialization type that contains all of the converted
1090 // arguments in canonical form. This ensures that, e.g., A<T> and
1091 // A<T, T> have identical types when A is declared as:
1092 //
1093 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001094 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001095 CanonType = Context.getTemplateSpecializationType(CanonName,
Anders Carlssonfb250522009-06-23 01:26:57 +00001096 Converted.getFlatArguments(),
1097 Converted.flatSize());
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregor1275ae02009-07-28 23:00:59 +00001099 // FIXME: CanonType is not actually the canonical type, and unfortunately
1100 // it is a TemplateTypeSpecializationType that we will never use again.
1101 // In the future, we need to teach getTemplateSpecializationType to only
1102 // build the canonical type and return that to us.
1103 CanonType = Context.getCanonicalType(CanonType);
Mike Stump1eb44332009-09-09 15:08:12 +00001104 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001105 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001106 // Find the class template specialization declaration that
1107 // corresponds to these arguments.
1108 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001109 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00001110 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00001111 Converted.flatSize(),
1112 Context);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001113 void *InsertPos = 0;
1114 ClassTemplateSpecializationDecl *Decl
1115 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1116 if (!Decl) {
1117 // This is the first time we have referenced this class template
1118 // specialization. Create the canonical declaration and add it to
1119 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001120 Decl = ClassTemplateSpecializationDecl::Create(Context,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001121 ClassTemplate->getDeclContext(),
John McCall9cc78072009-09-11 07:25:08 +00001122 ClassTemplate->getLocation(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001123 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00001124 Converted, 0);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001125 ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1126 Decl->setLexicalDeclContext(CurContext);
1127 }
1128
1129 CanonType = Context.getTypeDeclType(Decl);
1130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor40808ce2009-03-09 23:48:35 +00001132 // Build the fully-sugared type for this class template
1133 // specialization, which refers back to the class template
1134 // specialization we created or found.
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001135 //FIXME: Preserve type source info.
Douglas Gregor7532dc62009-03-30 22:58:21 +00001136 return Context.getTemplateSpecializationType(Name, TemplateArgs,
1137 NumTemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001138}
1139
Douglas Gregorcc636682009-02-17 23:15:12 +00001140Action::TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001141Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001142 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001143 ASTTemplateArgsPtr TemplateArgsIn,
1144 SourceLocation *TemplateArgLocs,
John McCall6b2becf2009-09-08 17:47:29 +00001145 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001146 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001147
Douglas Gregor40808ce2009-03-09 23:48:35 +00001148 // Translate the parser's template argument list in our AST format.
1149 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1150 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001151
Douglas Gregor7532dc62009-03-30 22:58:21 +00001152 QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001153 TemplateArgs.data(),
1154 TemplateArgs.size(),
Douglas Gregor7532dc62009-03-30 22:58:21 +00001155 RAngleLoc);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001156 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001157
1158 if (Result.isNull())
1159 return true;
1160
John McCall6b2becf2009-09-08 17:47:29 +00001161 return Result.getAsOpaquePtr();
1162}
John McCallf1bbbb42009-09-04 01:14:41 +00001163
John McCall6b2becf2009-09-08 17:47:29 +00001164Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1165 TagUseKind TUK,
1166 DeclSpec::TST TagSpec,
1167 SourceLocation TagLoc) {
1168 if (TypeResult.isInvalid())
1169 return Sema::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001170
John McCall6b2becf2009-09-08 17:47:29 +00001171 QualType Type = QualType::getFromOpaquePtr(TypeResult.get());
John McCallf1bbbb42009-09-04 01:14:41 +00001172
John McCall6b2becf2009-09-08 17:47:29 +00001173 // Verify the tag specifier.
1174 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
John McCall6b2becf2009-09-08 17:47:29 +00001176 if (const RecordType *RT = Type->getAs<RecordType>()) {
1177 RecordDecl *D = RT->getDecl();
1178
1179 IdentifierInfo *Id = D->getIdentifier();
1180 assert(Id && "templated class must have an identifier");
1181
1182 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1183 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001184 << Type
John McCall6b2becf2009-09-08 17:47:29 +00001185 << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1186 D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001187 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001188 }
1189 }
1190
John McCall6b2becf2009-09-08 17:47:29 +00001191 QualType ElabType = Context.getElaboratedType(Type, TagKind);
1192
1193 return ElabType.getAsOpaquePtr();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001194}
1195
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001196Sema::OwningExprResult Sema::BuildTemplateIdExpr(TemplateName Template,
1197 SourceLocation TemplateNameLoc,
1198 SourceLocation LAngleLoc,
1199 const TemplateArgument *TemplateArgs,
1200 unsigned NumTemplateArgs,
1201 SourceLocation RAngleLoc) {
1202 // FIXME: Can we do any checking at this point? I guess we could check the
1203 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001204 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001205 // though.
Mike Stump1eb44332009-09-09 15:08:12 +00001206 return Owned(TemplateIdRefExpr::Create(Context,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001207 /*FIXME: New type?*/Context.OverloadTy,
1208 /*FIXME: Necessary?*/0,
1209 /*FIXME: Necessary?*/SourceRange(),
1210 Template, TemplateNameLoc, LAngleLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001211 TemplateArgs,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001212 NumTemplateArgs, RAngleLoc));
1213}
1214
1215Sema::OwningExprResult Sema::ActOnTemplateIdExpr(TemplateTy TemplateD,
1216 SourceLocation TemplateNameLoc,
1217 SourceLocation LAngleLoc,
1218 ASTTemplateArgsPtr TemplateArgsIn,
1219 SourceLocation *TemplateArgLocs,
1220 SourceLocation RAngleLoc) {
1221 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001223 // Translate the parser's template argument list in our AST format.
1224 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1225 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregor2aef06d2009-07-22 20:55:49 +00001226 TemplateArgsIn.release();
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001228 return BuildTemplateIdExpr(Template, TemplateNameLoc, LAngleLoc,
1229 TemplateArgs.data(), TemplateArgs.size(),
1230 RAngleLoc);
1231}
1232
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001233Sema::OwningExprResult
1234Sema::ActOnMemberTemplateIdReferenceExpr(Scope *S, ExprArg Base,
1235 SourceLocation OpLoc,
1236 tok::TokenKind OpKind,
1237 const CXXScopeSpec &SS,
1238 TemplateTy TemplateD,
1239 SourceLocation TemplateNameLoc,
1240 SourceLocation LAngleLoc,
1241 ASTTemplateArgsPtr TemplateArgsIn,
1242 SourceLocation *TemplateArgLocs,
1243 SourceLocation RAngleLoc) {
1244 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001246 // FIXME: We're going to end up looking up the template based on its name,
1247 // twice!
1248 DeclarationName Name;
1249 if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl())
1250 Name = ActualTemplate->getDeclName();
1251 else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl())
1252 Name = Ovl->getDeclName();
1253 else
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001254 Name = Template.getAsDependentTemplateName()->getName();
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001256 // Translate the parser's template argument list in our AST format.
1257 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1258 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
1259 TemplateArgsIn.release();
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001261 // Do we have the save the actual template name? We might need it...
1262 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, TemplateNameLoc,
1263 Name, true, LAngleLoc,
1264 TemplateArgs.data(), TemplateArgs.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001265 RAngleLoc, DeclPtrTy(), &SS);
Douglas Gregorc4bf26f2009-09-01 00:37:14 +00001266}
1267
Douglas Gregorc45c2322009-03-31 00:43:58 +00001268/// \brief Form a dependent template name.
1269///
1270/// This action forms a dependent template name given the template
1271/// name and its (presumably dependent) scope specifier. For
1272/// example, given "MetaFun::template apply", the scope specifier \p
1273/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1274/// of the "template" keyword, and "apply" is the \p Name.
Mike Stump1eb44332009-09-09 15:08:12 +00001275Sema::TemplateTy
Douglas Gregorc45c2322009-03-31 00:43:58 +00001276Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
1277 const IdentifierInfo &Name,
1278 SourceLocation NameLoc,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001279 const CXXScopeSpec &SS,
1280 TypeTy *ObjectType) {
Mike Stump1eb44332009-09-09 15:08:12 +00001281 if ((ObjectType &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001282 computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) ||
1283 (SS.isSet() && computeDeclContext(SS, false))) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001284 // C++0x [temp.names]p5:
1285 // If a name prefixed by the keyword template is not the name of
1286 // a template, the program is ill-formed. [Note: the keyword
1287 // template may not be applied to non-template members of class
1288 // templates. -end note ] [ Note: as is the case with the
1289 // typename prefix, the template prefix is allowed in cases
1290 // where it is not strictly necessary; i.e., when the
1291 // nested-name-specifier or the expression on the left of the ->
1292 // or . is not dependent on a template-parameter, or the use
1293 // does not appear in the scope of a template. -end note]
1294 //
1295 // Note: C++03 was more strict here, because it banned the use of
1296 // the "template" keyword prior to a template-name that was not a
1297 // dependent name. C++ DR468 relaxed this requirement (the
1298 // "template" keyword is now permitted). We follow the C++0x
1299 // rules, even in C++03 mode, retroactively applying the DR.
1300 TemplateTy Template;
Mike Stump1eb44332009-09-09 15:08:12 +00001301 TemplateNameKind TNK = isTemplateName(0, Name, NameLoc, &SS, ObjectType,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001302 false, Template);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001303 if (TNK == TNK_Non_template) {
1304 Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1305 << &Name;
1306 return TemplateTy();
1307 }
1308
1309 return Template;
1310 }
1311
Mike Stump1eb44332009-09-09 15:08:12 +00001312 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001313 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001314 return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name));
1315}
1316
Mike Stump1eb44332009-09-09 15:08:12 +00001317bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
Anders Carlsson436b1562009-06-13 00:33:33 +00001318 const TemplateArgument &Arg,
1319 TemplateArgumentListBuilder &Converted) {
1320 // Check template type parameter.
1321 if (Arg.getKind() != TemplateArgument::Type) {
1322 // C++ [temp.arg.type]p1:
1323 // A template-argument for a template-parameter which is a
1324 // type shall be a type-id.
1325
1326 // We have a template type parameter but the template argument
1327 // is not a type.
1328 Diag(Arg.getLocation(), diag::err_template_arg_must_be_type);
1329 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Anders Carlsson436b1562009-06-13 00:33:33 +00001331 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001332 }
Anders Carlsson436b1562009-06-13 00:33:33 +00001333
1334 if (CheckTemplateArgument(Param, Arg.getAsType(), Arg.getLocation()))
1335 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Anders Carlsson436b1562009-06-13 00:33:33 +00001337 // Add the converted template type argument.
Anders Carlssonfb250522009-06-23 01:26:57 +00001338 Converted.Append(
Anders Carlsson436b1562009-06-13 00:33:33 +00001339 TemplateArgument(Arg.getLocation(),
1340 Context.getCanonicalType(Arg.getAsType())));
1341 return false;
1342}
1343
Douglas Gregorc15cb382009-02-09 23:23:08 +00001344/// \brief Check that the given template argument list is well-formed
1345/// for specializing the given template.
1346bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
1347 SourceLocation TemplateLoc,
1348 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001349 const TemplateArgument *TemplateArgs,
1350 unsigned NumTemplateArgs,
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001351 SourceLocation RAngleLoc,
Douglas Gregor16134c62009-07-01 00:28:38 +00001352 bool PartialTemplateArgs,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001353 TemplateArgumentListBuilder &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001354 TemplateParameterList *Params = Template->getTemplateParameters();
1355 unsigned NumParams = Params->size();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001356 unsigned NumArgs = NumTemplateArgs;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001357 bool Invalid = false;
1358
Mike Stump1eb44332009-09-09 15:08:12 +00001359 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001360 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001362 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00001363 (NumArgs < Params->getMinRequiredArguments() &&
1364 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001365 // FIXME: point at either the first arg beyond what we can handle,
1366 // or the '>', depending on whether we have too many or too few
1367 // arguments.
1368 SourceRange Range;
1369 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001370 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001371 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
1372 << (NumArgs > NumParams)
1373 << (isa<ClassTemplateDecl>(Template)? 0 :
1374 isa<FunctionTemplateDecl>(Template)? 1 :
1375 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
1376 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00001377 Diag(Template->getLocation(), diag::note_template_decl_here)
1378 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00001379 Invalid = true;
1380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
1382 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00001383 // [...] The type and form of each template-argument specified in
1384 // a template-id shall match the type and form specified for the
1385 // corresponding parameter declared by the template in its
1386 // template-parameter-list.
1387 unsigned ArgIdx = 0;
1388 for (TemplateParameterList::iterator Param = Params->begin(),
1389 ParamEnd = Params->end();
1390 Param != ParamEnd; ++Param, ++ArgIdx) {
Douglas Gregor16134c62009-07-01 00:28:38 +00001391 if (ArgIdx > NumArgs && PartialTemplateArgs)
1392 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregorc15cb382009-02-09 23:23:08 +00001394 // Decode the template argument
Douglas Gregor40808ce2009-03-09 23:48:35 +00001395 TemplateArgument Arg;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001396 if (ArgIdx >= NumArgs) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001397 // Retrieve the default template argument from the template
1398 // parameter.
1399 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001400 if (TTP->isParameterPack()) {
Anders Carlssonfb250522009-06-23 01:26:57 +00001401 // We have an empty argument pack.
1402 Converted.BeginPack();
1403 Converted.EndPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001404 break;
1405 }
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001407 if (!TTP->hasDefaultArgument())
1408 break;
1409
Douglas Gregor40808ce2009-03-09 23:48:35 +00001410 QualType ArgType = TTP->getDefaultArgument();
Douglas Gregor99ebf652009-02-27 19:31:52 +00001411
1412 // If the argument type is dependent, instantiate it now based
1413 // on the previously-computed template arguments.
Douglas Gregordf667e72009-03-10 20:44:00 +00001414 if (ArgType->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001415 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001416 Template, Converted.getFlatArguments(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001417 Converted.flatSize(),
Douglas Gregordf667e72009-03-10 20:44:00 +00001418 SourceRange(TemplateLoc, RAngleLoc));
Douglas Gregor7e063902009-05-11 23:53:27 +00001419
Anders Carlssone9c904b2009-06-05 04:47:51 +00001420 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001421 /*TakeArgs=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001422 ArgType = SubstType(ArgType,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001423 MultiLevelTemplateArgumentList(TemplateArgs),
John McCallce3ff2b2009-08-25 22:02:44 +00001424 TTP->getDefaultArgumentLoc(),
1425 TTP->getDeclName());
Douglas Gregordf667e72009-03-10 20:44:00 +00001426 }
Douglas Gregor99ebf652009-02-27 19:31:52 +00001427
1428 if (ArgType.isNull())
Douglas Gregorcd281c32009-02-28 00:25:32 +00001429 return true;
Douglas Gregor99ebf652009-02-27 19:31:52 +00001430
Douglas Gregor40808ce2009-03-09 23:48:35 +00001431 Arg = TemplateArgument(TTP->getLocation(), ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001432 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001433 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1434 if (!NTTP->hasDefaultArgument())
1435 break;
1436
Mike Stump1eb44332009-09-09 15:08:12 +00001437 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001438 Template, Converted.getFlatArguments(),
Anders Carlsson3b56c002009-06-11 16:06:49 +00001439 Converted.flatSize(),
1440 SourceRange(TemplateLoc, RAngleLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Anders Carlsson3b56c002009-06-11 16:06:49 +00001442 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001443 /*TakeArgs=*/false);
Anders Carlsson3b56c002009-06-11 16:06:49 +00001444
Mike Stump1eb44332009-09-09 15:08:12 +00001445 Sema::OwningExprResult E
1446 = SubstExpr(NTTP->getDefaultArgument(),
Douglas Gregord6350ae2009-08-28 20:31:08 +00001447 MultiLevelTemplateArgumentList(TemplateArgs));
Anders Carlsson3b56c002009-06-11 16:06:49 +00001448 if (E.isInvalid())
1449 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Anders Carlsson3b56c002009-06-11 16:06:49 +00001451 Arg = TemplateArgument(E.takeAs<Expr>());
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001452 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001453 TemplateTemplateParmDecl *TempParm
1454 = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001455
1456 if (!TempParm->hasDefaultArgument())
1457 break;
1458
John McCallce3ff2b2009-08-25 22:02:44 +00001459 // FIXME: Subst default argument
Douglas Gregor40808ce2009-03-09 23:48:35 +00001460 Arg = TemplateArgument(TempParm->getDefaultArgument());
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001461 }
1462 } else {
1463 // Retrieve the template argument produced by the user.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001464 Arg = TemplateArgs[ArgIdx];
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001465 }
1466
Douglas Gregorc15cb382009-02-09 23:23:08 +00001467
1468 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001469 if (TTP->isParameterPack()) {
Anders Carlssonfb250522009-06-23 01:26:57 +00001470 Converted.BeginPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001471 // Check all the remaining arguments (if any).
1472 for (; ArgIdx < NumArgs; ++ArgIdx) {
1473 if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted))
1474 Invalid = true;
1475 }
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Anders Carlssonfb250522009-06-23 01:26:57 +00001477 Converted.EndPack();
Anders Carlsson0ceffb52009-06-13 02:08:00 +00001478 } else {
1479 if (CheckTemplateTypeArgument(TTP, Arg, Converted))
1480 Invalid = true;
1481 }
Mike Stump1eb44332009-09-09 15:08:12 +00001482 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorc15cb382009-02-09 23:23:08 +00001483 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1484 // Check non-type template parameters.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001485
John McCallce3ff2b2009-08-25 22:02:44 +00001486 // Do substitution on the type of the non-type template parameter
1487 // with the template arguments we've seen thus far.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001488 QualType NTTPType = NTTP->getType();
1489 if (NTTPType->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +00001490 // Do substitution on the type of the non-type template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001491 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonfb250522009-06-23 01:26:57 +00001492 Template, Converted.getFlatArguments(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001493 Converted.flatSize(),
Douglas Gregordf667e72009-03-10 20:44:00 +00001494 SourceRange(TemplateLoc, RAngleLoc));
1495
Anders Carlssone9c904b2009-06-05 04:47:51 +00001496 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonfb250522009-06-23 01:26:57 +00001497 /*TakeArgs=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001498 NTTPType = SubstType(NTTPType,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001499 MultiLevelTemplateArgumentList(TemplateArgs),
John McCallce3ff2b2009-08-25 22:02:44 +00001500 NTTP->getLocation(),
1501 NTTP->getDeclName());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001502 // If that worked, check the non-type template parameter type
1503 // for validity.
1504 if (!NTTPType.isNull())
Mike Stump1eb44332009-09-09 15:08:12 +00001505 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001506 NTTP->getLocation());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001507 if (NTTPType.isNull()) {
1508 Invalid = true;
1509 break;
1510 }
1511 }
1512
Douglas Gregor40808ce2009-03-09 23:48:35 +00001513 switch (Arg.getKind()) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001514 case TemplateArgument::Null:
1515 assert(false && "Should never see a NULL template argument here");
1516 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregor40808ce2009-03-09 23:48:35 +00001518 case TemplateArgument::Expression: {
1519 Expr *E = Arg.getAsExpr();
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001520 TemplateArgument Result;
1521 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
Douglas Gregorc15cb382009-02-09 23:23:08 +00001522 Invalid = true;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001523 else
Anders Carlssonfb250522009-06-23 01:26:57 +00001524 Converted.Append(Result);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001525 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001526 }
1527
Douglas Gregor40808ce2009-03-09 23:48:35 +00001528 case TemplateArgument::Declaration:
1529 case TemplateArgument::Integral:
1530 // We've already checked this template argument, so just copy
1531 // it to the list of converted arguments.
Anders Carlssonfb250522009-06-23 01:26:57 +00001532 Converted.Append(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001533 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001534
Douglas Gregor40808ce2009-03-09 23:48:35 +00001535 case TemplateArgument::Type:
1536 // We have a non-type template parameter but the template
1537 // argument is a type.
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregor40808ce2009-03-09 23:48:35 +00001539 // C++ [temp.arg]p2:
1540 // In a template-argument, an ambiguity between a type-id and
1541 // an expression is resolved to a type-id, regardless of the
1542 // form of the corresponding template-parameter.
1543 //
1544 // We warn specifically about this case, since it can be rather
1545 // confusing for users.
1546 if (Arg.getAsType()->isFunctionType())
1547 Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig)
1548 << Arg.getAsType();
1549 else
1550 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr);
1551 Diag((*Param)->getLocation(), diag::note_template_param_here);
1552 Invalid = true;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001553 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Anders Carlssond01b1da2009-06-15 17:04:53 +00001555 case TemplateArgument::Pack:
1556 assert(0 && "FIXME: Implement!");
1557 break;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559 } else {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001560 // Check template template parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001561 TemplateTemplateParmDecl *TempParm
Douglas Gregorc15cb382009-02-09 23:23:08 +00001562 = cast<TemplateTemplateParmDecl>(*Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Douglas Gregor40808ce2009-03-09 23:48:35 +00001564 switch (Arg.getKind()) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001565 case TemplateArgument::Null:
1566 assert(false && "Should never see a NULL template argument here");
1567 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Douglas Gregor40808ce2009-03-09 23:48:35 +00001569 case TemplateArgument::Expression: {
1570 Expr *ArgExpr = Arg.getAsExpr();
1571 if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
1572 isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
1573 if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
1574 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor40808ce2009-03-09 23:48:35 +00001576 // Add the converted template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001577 Decl *D
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001578 = cast<DeclRefExpr>(ArgExpr)->getDecl()->getCanonicalDecl();
Anders Carlssonfb250522009-06-23 01:26:57 +00001579 Converted.Append(TemplateArgument(Arg.getLocation(), D));
Douglas Gregor40808ce2009-03-09 23:48:35 +00001580 continue;
1581 }
1582 }
1583 // fall through
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Douglas Gregor40808ce2009-03-09 23:48:35 +00001585 case TemplateArgument::Type: {
1586 // We have a template template parameter but the template
1587 // argument does not refer to a template.
1588 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
1589 Invalid = true;
1590 break;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001591 }
1592
Douglas Gregor40808ce2009-03-09 23:48:35 +00001593 case TemplateArgument::Declaration:
1594 // We've already checked this template argument, so just copy
1595 // it to the list of converted arguments.
Anders Carlssonfb250522009-06-23 01:26:57 +00001596 Converted.Append(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001597 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregor40808ce2009-03-09 23:48:35 +00001599 case TemplateArgument::Integral:
1600 assert(false && "Integral argument with template template parameter");
1601 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Anders Carlssond01b1da2009-06-15 17:04:53 +00001603 case TemplateArgument::Pack:
1604 assert(0 && "FIXME: Implement!");
1605 break;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001606 }
Douglas Gregorc15cb382009-02-09 23:23:08 +00001607 }
1608 }
1609
1610 return Invalid;
1611}
1612
1613/// \brief Check a template argument against its corresponding
1614/// template type parameter.
1615///
1616/// This routine implements the semantics of C++ [temp.arg.type]. It
1617/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001618bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
Douglas Gregorc15cb382009-02-09 23:23:08 +00001619 QualType Arg, SourceLocation ArgLoc) {
1620 // C++ [temp.arg.type]p2:
1621 // A local type, a type with no linkage, an unnamed type or a type
1622 // compounded from any of these types shall not be used as a
1623 // template-argument for a template type-parameter.
1624 //
1625 // FIXME: Perform the recursive and no-linkage type checks.
1626 const TagType *Tag = 0;
1627 if (const EnumType *EnumT = Arg->getAsEnumType())
1628 Tag = EnumT;
Ted Kremenek6217b802009-07-29 21:53:49 +00001629 else if (const RecordType *RecordT = Arg->getAs<RecordType>())
Douglas Gregorc15cb382009-02-09 23:23:08 +00001630 Tag = RecordT;
1631 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
1632 return Diag(ArgLoc, diag::err_template_arg_local_type)
1633 << QualType(Tag, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001634 else if (Tag && !Tag->getDecl()->getDeclName() &&
Douglas Gregor98137532009-03-10 18:33:27 +00001635 !Tag->getDecl()->getTypedefForAnonDecl()) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00001636 Diag(ArgLoc, diag::err_template_arg_unnamed_type);
1637 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
1638 return true;
1639 }
1640
1641 return false;
1642}
1643
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001644/// \brief Checks whether the given template argument is the address
1645/// of an object or function according to C++ [temp.arg.nontype]p1.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001646bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
1647 NamedDecl *&Entity) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001648 bool Invalid = false;
1649
1650 // See through any implicit casts we added to fix the type.
1651 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1652 Arg = Cast->getSubExpr();
1653
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001654 // C++0x allows nullptr, and there's no further checking to be done for that.
1655 if (Arg->getType()->isNullPtrType())
1656 return false;
1657
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001658 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001659 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001660 // A template-argument for a non-type, non-template
1661 // template-parameter shall be one of: [...]
1662 //
1663 // -- the address of an object or function with external
1664 // linkage, including function templates and function
1665 // template-ids but excluding non-static class members,
1666 // expressed as & id-expression where the & is optional if
1667 // the name refers to a function or array, or if the
1668 // corresponding template-parameter is a reference; or
1669 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001671 // Ignore (and complain about) any excess parentheses.
1672 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1673 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00001674 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001675 diag::err_template_arg_extra_parens)
1676 << Arg->getSourceRange();
1677 Invalid = true;
1678 }
1679
1680 Arg = Parens->getSubExpr();
1681 }
1682
1683 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
1684 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1685 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
1686 } else
1687 DRE = dyn_cast<DeclRefExpr>(Arg);
1688
1689 if (!DRE || !isa<ValueDecl>(DRE->getDecl()))
Mike Stump1eb44332009-09-09 15:08:12 +00001690 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001691 diag::err_template_arg_not_object_or_func_form)
1692 << Arg->getSourceRange();
1693
1694 // Cannot refer to non-static data members
1695 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
1696 return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
1697 << Field << Arg->getSourceRange();
1698
1699 // Cannot refer to non-static member functions
1700 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
1701 if (!Method->isStatic())
Mike Stump1eb44332009-09-09 15:08:12 +00001702 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001703 diag::err_template_arg_method)
1704 << Method << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001706 // Functions must have external linkage.
1707 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1708 if (Func->getStorageClass() == FunctionDecl::Static) {
Mike Stump1eb44332009-09-09 15:08:12 +00001709 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001710 diag::err_template_arg_function_not_extern)
1711 << Func << Arg->getSourceRange();
1712 Diag(Func->getLocation(), diag::note_template_arg_internal_object)
1713 << true;
1714 return true;
1715 }
1716
1717 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001718 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001719 return Invalid;
1720 }
1721
1722 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
1723 if (!Var->hasGlobalStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001724 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001725 diag::err_template_arg_object_not_extern)
1726 << Var << Arg->getSourceRange();
1727 Diag(Var->getLocation(), diag::note_template_arg_internal_object)
1728 << true;
1729 return true;
1730 }
1731
1732 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001733 Entity = Var;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001734 return Invalid;
1735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001737 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00001738 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001739 diag::err_template_arg_not_object_or_func)
1740 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001741 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001742 diag::note_template_arg_refers_here);
1743 return true;
1744}
1745
1746/// \brief Checks whether the given template argument is a pointer to
1747/// member constant according to C++ [temp.arg.nontype]p1.
Mike Stump1eb44332009-09-09 15:08:12 +00001748bool
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001749Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001750 bool Invalid = false;
1751
1752 // See through any implicit casts we added to fix the type.
1753 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1754 Arg = Cast->getSubExpr();
1755
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001756 // C++0x allows nullptr, and there's no further checking to be done for that.
1757 if (Arg->getType()->isNullPtrType())
1758 return false;
1759
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001760 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001761 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001762 // A template-argument for a non-type, non-template
1763 // template-parameter shall be one of: [...]
1764 //
1765 // -- a pointer to member expressed as described in 5.3.1.
1766 QualifiedDeclRefExpr *DRE = 0;
1767
1768 // Ignore (and complain about) any excess parentheses.
1769 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1770 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00001771 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001772 diag::err_template_arg_extra_parens)
1773 << Arg->getSourceRange();
1774 Invalid = true;
1775 }
1776
1777 Arg = Parens->getSubExpr();
1778 }
1779
1780 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg))
1781 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1782 DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr());
1783
1784 if (!DRE)
1785 return Diag(Arg->getSourceRange().getBegin(),
1786 diag::err_template_arg_not_pointer_to_member_form)
1787 << Arg->getSourceRange();
1788
1789 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
1790 assert((isa<FieldDecl>(DRE->getDecl()) ||
1791 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
1792 "Only non-static member pointers can make it here");
1793
1794 // Okay: this is the address of a non-static member, and therefore
1795 // a member pointer constant.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001796 Member = DRE->getDecl();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001797 return Invalid;
1798 }
1799
1800 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00001801 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001802 diag::err_template_arg_not_pointer_to_member_form)
1803 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001804 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001805 diag::note_template_arg_refers_here);
1806 return true;
1807}
1808
Douglas Gregorc15cb382009-02-09 23:23:08 +00001809/// \brief Check a template argument against its corresponding
1810/// non-type template parameter.
1811///
Douglas Gregor2943aed2009-03-03 04:44:36 +00001812/// This routine implements the semantics of C++ [temp.arg.nontype].
1813/// It returns true if an error occurred, and false otherwise. \p
1814/// InstantiatedParamType is the type of the non-type template
1815/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001816///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001817/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00001818bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00001819 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001820 TemplateArgument &Converted) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001821 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
1822
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001823 // If either the parameter has a dependent type or the argument is
1824 // type-dependent, there's nothing we can check now.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001825 // FIXME: Add template argument to Converted!
Douglas Gregor40808ce2009-03-09 23:48:35 +00001826 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
1827 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001828 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001829 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001830 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001831
1832 // C++ [temp.arg.nontype]p5:
1833 // The following conversions are performed on each expression used
1834 // as a non-type template-argument. If a non-type
1835 // template-argument cannot be converted to the type of the
1836 // corresponding template-parameter then the program is
1837 // ill-formed.
1838 //
1839 // -- for a non-type template-parameter of integral or
1840 // enumeration type, integral promotions (4.5) and integral
1841 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001842 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00001843 QualType ArgType = Arg->getType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001844 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001845 // C++ [temp.arg.nontype]p1:
1846 // A template-argument for a non-type, non-template
1847 // template-parameter shall be one of:
1848 //
1849 // -- an integral constant-expression of integral or enumeration
1850 // type; or
1851 // -- the name of a non-type template-parameter; or
1852 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001853 llvm::APSInt Value;
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001854 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001855 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001856 diag::err_template_arg_not_integral_or_enumeral)
1857 << ArgType << Arg->getSourceRange();
1858 Diag(Param->getLocation(), diag::note_template_param_here);
1859 return true;
1860 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001861 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001862 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
1863 << ArgType << Arg->getSourceRange();
1864 return true;
1865 }
1866
1867 // FIXME: We need some way to more easily get the unqualified form
1868 // of the types without going all the way to the
1869 // canonical type.
1870 if (Context.getCanonicalType(ParamType).getCVRQualifiers())
1871 ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
1872 if (Context.getCanonicalType(ArgType).getCVRQualifiers())
1873 ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
1874
1875 // Try to convert the argument to the parameter's type.
1876 if (ParamType == ArgType) {
1877 // Okay: no conversion necessary
1878 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
1879 !ParamType->isEnumeralType()) {
1880 // This is an integral promotion or conversion.
1881 ImpCastExprToType(Arg, ParamType);
1882 } else {
1883 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00001884 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001885 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00001886 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001887 Diag(Param->getLocation(), diag::note_template_param_here);
1888 return true;
1889 }
1890
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001891 QualType IntegerType = Context.getCanonicalType(ParamType);
1892 if (const EnumType *Enum = IntegerType->getAsEnumType())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001893 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001894
1895 if (!Arg->isValueDependent()) {
1896 // Check that an unsigned parameter does not receive a negative
1897 // value.
1898 if (IntegerType->isUnsignedIntegerType()
1899 && (Value.isSigned() && Value.isNegative())) {
1900 Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
1901 << Value.toString(10) << Param->getType()
1902 << Arg->getSourceRange();
1903 Diag(Param->getLocation(), diag::note_template_param_here);
1904 return true;
1905 }
1906
1907 // Check that we don't overflow the template parameter type.
1908 unsigned AllowedBits = Context.getTypeSize(IntegerType);
1909 if (Value.getActiveBits() > AllowedBits) {
Mike Stump1eb44332009-09-09 15:08:12 +00001910 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorf80a9d52009-03-14 00:20:21 +00001911 diag::err_template_arg_too_large)
1912 << Value.toString(10) << Param->getType()
1913 << Arg->getSourceRange();
1914 Diag(Param->getLocation(), diag::note_template_param_here);
1915 return true;
1916 }
1917
1918 if (Value.getBitWidth() != AllowedBits)
1919 Value.extOrTrunc(AllowedBits);
1920 Value.setIsSigned(IntegerType->isSignedIntegerType());
1921 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001922
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001923 // Add the value of this argument to the list of converted
1924 // arguments. We use the bitwidth and signedness of the template
1925 // parameter.
1926 if (Arg->isValueDependent()) {
1927 // The argument is value-dependent. Create a new
1928 // TemplateArgument with the converted expression.
1929 Converted = TemplateArgument(Arg);
1930 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001931 }
1932
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001933 Converted = TemplateArgument(StartLoc, Value,
Mike Stump1eb44332009-09-09 15:08:12 +00001934 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001935 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00001936 return false;
1937 }
Douglas Gregora35284b2009-02-11 00:19:33 +00001938
Douglas Gregorb86b0572009-02-11 01:18:59 +00001939 // Handle pointer-to-function, reference-to-function, and
1940 // pointer-to-member-function all in (roughly) the same way.
1941 if (// -- For a non-type template-parameter of type pointer to
1942 // function, only the function-to-pointer conversion (4.3) is
1943 // applied. If the template-argument represents a set of
1944 // overloaded functions (or a pointer to such), the matching
1945 // function is selected from the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001946 // In C++0x, any std::nullptr_t value can be converted.
Douglas Gregorb86b0572009-02-11 01:18:59 +00001947 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001948 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00001949 // -- For a non-type template-parameter of type reference to
1950 // function, no conversions apply. If the template-argument
1951 // represents a set of overloaded functions, the matching
1952 // function is selected from the set (13.4).
1953 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001954 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00001955 // -- For a non-type template-parameter of type pointer to
1956 // member function, no conversions apply. If the
1957 // template-argument represents a set of overloaded member
1958 // functions, the matching member function is selected from
1959 // the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001960 // Again, C++0x allows a std::nullptr_t value.
Douglas Gregorb86b0572009-02-11 01:18:59 +00001961 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001962 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00001963 ->isFunctionType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001964 if (Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001965 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001966 // We don't have to do anything: the types already match.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001967 } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
1968 ParamType->isMemberPointerType())) {
1969 ArgType = ParamType;
1970 ImpCastExprToType(Arg, ParamType);
Douglas Gregorb86b0572009-02-11 01:18:59 +00001971 } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001972 ArgType = Context.getPointerType(ArgType);
1973 ImpCastExprToType(Arg, ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001974 } else if (FunctionDecl *Fn
Douglas Gregora35284b2009-02-11 00:19:33 +00001975 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001976 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
1977 return true;
1978
Douglas Gregora35284b2009-02-11 00:19:33 +00001979 FixOverloadedFunctionReference(Arg, Fn);
1980 ArgType = Arg->getType();
Douglas Gregorb86b0572009-02-11 01:18:59 +00001981 if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001982 ArgType = Context.getPointerType(Arg->getType());
1983 ImpCastExprToType(Arg, ArgType);
1984 }
1985 }
1986
Mike Stump1eb44332009-09-09 15:08:12 +00001987 if (!Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00001988 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00001989 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00001990 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00001991 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00001992 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00001993 Diag(Param->getLocation(), diag::note_template_param_here);
1994 return true;
1995 }
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Douglas Gregor3e00bad2009-02-17 01:05:43 +00001997 if (ParamType->isMemberPointerType()) {
1998 NamedDecl *Member = 0;
1999 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2000 return true;
2001
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002002 if (Member)
2003 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002004 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002005 return false;
2006 }
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002008 NamedDecl *Entity = 0;
2009 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2010 return true;
2011
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002012 if (Entity)
2013 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002014 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002015 return false;
Douglas Gregora35284b2009-02-11 00:19:33 +00002016 }
2017
Chris Lattnerfe90de72009-02-20 21:37:53 +00002018 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002019 // -- for a non-type template-parameter of type pointer to
2020 // object, qualification conversions (4.4) and the
2021 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002022 // C++0x also allows a value of std::nullptr_t.
Ted Kremenek6217b802009-07-29 21:53:49 +00002023 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002024 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002025
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002026 if (ArgType->isNullPtrType()) {
2027 ArgType = ParamType;
2028 ImpCastExprToType(Arg, ParamType);
2029 } else if (ArgType->isArrayType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002030 ArgType = Context.getArrayDecayedType(ArgType);
2031 ImpCastExprToType(Arg, ArgType);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002032 }
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002033
Douglas Gregorb86b0572009-02-11 01:18:59 +00002034 if (IsQualificationConversion(ArgType, ParamType)) {
2035 ArgType = ParamType;
2036 ImpCastExprToType(Arg, ParamType);
2037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002039 if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002040 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002041 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002042 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002043 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregorb86b0572009-02-11 01:18:59 +00002044 Diag(Param->getLocation(), diag::note_template_param_here);
2045 return true;
2046 }
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002048 NamedDecl *Entity = 0;
2049 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2050 return true;
2051
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002052 if (Entity)
2053 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002054 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002055 return false;
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002056 }
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Ted Kremenek6217b802009-07-29 21:53:49 +00002058 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002059 // -- For a non-type template-parameter of type reference to
2060 // object, no conversions apply. The type referred to by the
2061 // reference may be more cv-qualified than the (otherwise
2062 // identical) type of the template-argument. The
2063 // template-parameter is bound directly to the
2064 // template-argument, which must be an lvalue.
Douglas Gregorbad0e652009-03-24 20:32:41 +00002065 assert(ParamRefType->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002066 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002067
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002068 if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002069 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002070 diag::err_template_arg_no_ref_bind)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002071 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002072 << Arg->getSourceRange();
2073 Diag(Param->getLocation(), diag::note_template_param_here);
2074 return true;
2075 }
2076
Mike Stump1eb44332009-09-09 15:08:12 +00002077 unsigned ParamQuals
Douglas Gregorb86b0572009-02-11 01:18:59 +00002078 = Context.getCanonicalType(ParamType).getCVRQualifiers();
2079 unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Douglas Gregorb86b0572009-02-11 01:18:59 +00002081 if ((ParamQuals | ArgQuals) != ParamQuals) {
2082 Diag(Arg->getSourceRange().getBegin(),
2083 diag::err_template_arg_ref_bind_ignores_quals)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002084 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002085 << Arg->getSourceRange();
2086 Diag(Param->getLocation(), diag::note_template_param_here);
2087 return true;
2088 }
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002090 NamedDecl *Entity = 0;
2091 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2092 return true;
2093
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002094 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002095 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002096 return false;
Douglas Gregorb86b0572009-02-11 01:18:59 +00002097 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00002098
2099 // -- For a non-type template-parameter of type pointer to data
2100 // member, qualification conversions (4.4) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002101 // C++0x allows std::nullptr_t values.
Douglas Gregor658bbb52009-02-11 16:16:59 +00002102 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2103
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002104 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00002105 // Types match exactly: nothing more to do here.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002106 } else if (ArgType->isNullPtrType()) {
2107 ImpCastExprToType(Arg, ParamType);
Douglas Gregor658bbb52009-02-11 16:16:59 +00002108 } else if (IsQualificationConversion(ArgType, ParamType)) {
2109 ImpCastExprToType(Arg, ParamType);
2110 } else {
2111 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002112 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00002113 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002114 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00002115 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002116 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00002117 }
2118
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002119 NamedDecl *Member = 0;
2120 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2121 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002123 if (Member)
2124 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002125 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002126 return false;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002127}
2128
2129/// \brief Check a template argument against its corresponding
2130/// template template parameter.
2131///
2132/// This routine implements the semantics of C++ [temp.arg.template].
2133/// It returns true if an error occurred, and false otherwise.
2134bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
2135 DeclRefExpr *Arg) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002136 assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
2137 TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
2138
2139 // C++ [temp.arg.template]p1:
2140 // A template-argument for a template template-parameter shall be
2141 // the name of a class template, expressed as id-expression. Only
2142 // primary class templates are considered when matching the
2143 // template template argument with the corresponding parameter;
2144 // partial specializations are not considered even if their
2145 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002146 //
2147 // Note that we also allow template template parameters here, which
2148 // will happen when we are dealing with, e.g., class template
2149 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002150 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002151 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002152 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00002153 "Only function templates are possible here");
Douglas Gregore53060f2009-06-25 22:08:12 +00002154 Diag(Arg->getLocStart(), diag::err_template_arg_not_class_template);
2155 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00002156 << Template;
2157 }
2158
2159 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2160 Param->getTemplateParameters(),
2161 true, true,
2162 Arg->getSourceRange().getBegin());
Douglas Gregorc15cb382009-02-09 23:23:08 +00002163}
2164
Douglas Gregorddc29e12009-02-06 22:42:48 +00002165/// \brief Determine whether the given template parameter lists are
2166/// equivalent.
2167///
Mike Stump1eb44332009-09-09 15:08:12 +00002168/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00002169/// source code as part of a new template declaration.
2170///
2171/// \param Old The old template parameter list, typically found via
2172/// name lookup of the template declared with this template parameter
2173/// list.
2174///
2175/// \param Complain If true, this routine will produce a diagnostic if
2176/// the template parameter lists are not equivalent.
2177///
Douglas Gregordd0574e2009-02-10 00:24:35 +00002178/// \param IsTemplateTemplateParm If true, this routine is being
2179/// called to compare the template parameter lists of a template
2180/// template parameter.
2181///
2182/// \param TemplateArgLoc If this source location is valid, then we
2183/// are actually checking the template parameter list of a template
2184/// argument (New) against the template parameter list of its
2185/// corresponding template template parameter (Old). We produce
2186/// slightly different diagnostics in this scenario.
2187///
Douglas Gregorddc29e12009-02-06 22:42:48 +00002188/// \returns True if the template parameter lists are equal, false
2189/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002190bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00002191Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2192 TemplateParameterList *Old,
2193 bool Complain,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002194 bool IsTemplateTemplateParm,
2195 SourceLocation TemplateArgLoc) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002196 if (Old->size() != New->size()) {
2197 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002198 unsigned NextDiag = diag::err_template_param_list_different_arity;
2199 if (TemplateArgLoc.isValid()) {
2200 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2201 NextDiag = diag::note_template_param_list_different_arity;
Mike Stump1eb44332009-09-09 15:08:12 +00002202 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00002203 Diag(New->getTemplateLoc(), NextDiag)
2204 << (New->size() > Old->size())
2205 << IsTemplateTemplateParm
2206 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregorddc29e12009-02-06 22:42:48 +00002207 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
2208 << IsTemplateTemplateParm
2209 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2210 }
2211
2212 return false;
2213 }
2214
2215 for (TemplateParameterList::iterator OldParm = Old->begin(),
2216 OldParmEnd = Old->end(), NewParm = New->begin();
2217 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
2218 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregor34d1dc92009-06-24 16:50:40 +00002219 if (Complain) {
2220 unsigned NextDiag = diag::err_template_param_different_kind;
2221 if (TemplateArgLoc.isValid()) {
2222 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2223 NextDiag = diag::note_template_param_different_kind;
2224 }
2225 Diag((*NewParm)->getLocation(), NextDiag)
2226 << IsTemplateTemplateParm;
2227 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
2228 << IsTemplateTemplateParm;
Douglas Gregordd0574e2009-02-10 00:24:35 +00002229 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00002230 return false;
2231 }
2232
2233 if (isa<TemplateTypeParmDecl>(*OldParm)) {
2234 // Okay; all template type parameters are equivalent (since we
Douglas Gregordd0574e2009-02-10 00:24:35 +00002235 // know we're at the same index).
2236#if 0
Mike Stump390b4cc2009-05-16 07:39:55 +00002237 // FIXME: Enable this code in debug mode *after* we properly go through
2238 // and "instantiate" the template parameter lists of template template
2239 // parameters. It's only after this instantiation that (1) any dependent
2240 // types within the template parameter list of the template template
2241 // parameter can be checked, and (2) the template type parameter depths
Douglas Gregordd0574e2009-02-10 00:24:35 +00002242 // will match up.
Mike Stump1eb44332009-09-09 15:08:12 +00002243 QualType OldParmType
Douglas Gregorddc29e12009-02-06 22:42:48 +00002244 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
Mike Stump1eb44332009-09-09 15:08:12 +00002245 QualType NewParmType
Douglas Gregorddc29e12009-02-06 22:42:48 +00002246 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
Mike Stump1eb44332009-09-09 15:08:12 +00002247 assert(Context.getCanonicalType(OldParmType) ==
2248 Context.getCanonicalType(NewParmType) &&
Douglas Gregorddc29e12009-02-06 22:42:48 +00002249 "type parameter mismatch?");
2250#endif
Mike Stump1eb44332009-09-09 15:08:12 +00002251 } else if (NonTypeTemplateParmDecl *OldNTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002252 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2253 // The types of non-type template parameters must agree.
2254 NonTypeTemplateParmDecl *NewNTTP
2255 = cast<NonTypeTemplateParmDecl>(*NewParm);
2256 if (Context.getCanonicalType(OldNTTP->getType()) !=
2257 Context.getCanonicalType(NewNTTP->getType())) {
2258 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002259 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
2260 if (TemplateArgLoc.isValid()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002261 Diag(TemplateArgLoc,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002262 diag::err_template_arg_template_params_mismatch);
2263 NextDiag = diag::note_template_nontype_parm_different_type;
2264 }
2265 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregorddc29e12009-02-06 22:42:48 +00002266 << NewNTTP->getType()
2267 << IsTemplateTemplateParm;
Mike Stump1eb44332009-09-09 15:08:12 +00002268 Diag(OldNTTP->getLocation(),
Douglas Gregorddc29e12009-02-06 22:42:48 +00002269 diag::note_template_nontype_parm_prev_declaration)
2270 << OldNTTP->getType();
2271 }
2272 return false;
2273 }
2274 } else {
2275 // The template parameter lists of template template
2276 // parameters must agree.
2277 // FIXME: Could we perform a faster "type" comparison here?
Mike Stump1eb44332009-09-09 15:08:12 +00002278 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
Douglas Gregorddc29e12009-02-06 22:42:48 +00002279 "Only template template parameters handled here");
Mike Stump1eb44332009-09-09 15:08:12 +00002280 TemplateTemplateParmDecl *OldTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002281 = cast<TemplateTemplateParmDecl>(*OldParm);
2282 TemplateTemplateParmDecl *NewTTP
2283 = cast<TemplateTemplateParmDecl>(*NewParm);
2284 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2285 OldTTP->getTemplateParameters(),
2286 Complain,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002287 /*IsTemplateTemplateParm=*/true,
2288 TemplateArgLoc))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002289 return false;
2290 }
2291 }
2292
2293 return true;
2294}
2295
2296/// \brief Check whether a template can be declared within this scope.
2297///
2298/// If the template declaration is valid in this scope, returns
2299/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00002300bool
Douglas Gregor05396e22009-08-25 17:23:04 +00002301Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002302 // Find the nearest enclosing declaration scope.
2303 while ((S->getFlags() & Scope::DeclScope) == 0 ||
2304 (S->getFlags() & Scope::TemplateParamScope) != 0)
2305 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Douglas Gregorddc29e12009-02-06 22:42:48 +00002307 // C++ [temp]p2:
2308 // A template-declaration can appear only as a namespace scope or
2309 // class scope declaration.
2310 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00002311 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2312 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00002313 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00002314 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Eli Friedman1503f772009-07-31 01:43:05 +00002316 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002317 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002318
2319 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2320 return false;
2321
Mike Stump1eb44332009-09-09 15:08:12 +00002322 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002323 diag::err_template_outside_namespace_or_class_scope)
2324 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002325}
Douglas Gregorcc636682009-02-17 23:15:12 +00002326
Douglas Gregorff668032009-05-13 18:28:20 +00002327/// \brief Check whether a class template specialization or explicit
2328/// instantiation in the current context is well-formed.
Douglas Gregor88b70942009-02-25 22:02:03 +00002329///
Douglas Gregorff668032009-05-13 18:28:20 +00002330/// This routine determines whether a class template specialization or
Mike Stump1eb44332009-09-09 15:08:12 +00002331/// explicit instantiation can be declared in the current context
2332/// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2) and emits
2333/// appropriate diagnostics if there was an error. It returns true if
Douglas Gregorff668032009-05-13 18:28:20 +00002334// there was an error that we cannot recover from, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002335bool
Douglas Gregor88b70942009-02-25 22:02:03 +00002336Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate,
2337 ClassTemplateSpecializationDecl *PrevDecl,
2338 SourceLocation TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002339 SourceRange ScopeSpecifierRange,
Douglas Gregor16df8502009-06-12 22:21:45 +00002340 bool PartialSpecialization,
Douglas Gregorff668032009-05-13 18:28:20 +00002341 bool ExplicitInstantiation) {
Douglas Gregor88b70942009-02-25 22:02:03 +00002342 // C++ [temp.expl.spec]p2:
2343 // An explicit specialization shall be declared in the namespace
2344 // of which the template is a member, or, for member templates, in
2345 // the namespace of which the enclosing class or enclosing class
2346 // template is a member. An explicit specialization of a member
2347 // function, member class or static data member of a class
2348 // template shall be declared in the namespace of which the class
2349 // template is a member. Such a declaration may also be a
2350 // definition. If the declaration is not a definition, the
2351 // specialization may be defined later in the name- space in which
2352 // the explicit specialization was declared, or in a namespace
2353 // that encloses the one in which the explicit specialization was
2354 // declared.
2355 if (CurContext->getLookupContext()->isFunctionOrMethod()) {
Douglas Gregor16df8502009-06-12 22:21:45 +00002356 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregor88b70942009-02-25 22:02:03 +00002357 Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002358 << Kind << ClassTemplate;
Douglas Gregor88b70942009-02-25 22:02:03 +00002359 return true;
2360 }
2361
2362 DeclContext *DC = CurContext->getEnclosingNamespaceContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002363 DeclContext *TemplateContext
Douglas Gregor88b70942009-02-25 22:02:03 +00002364 = ClassTemplate->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregorff668032009-05-13 18:28:20 +00002365 if ((!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) &&
2366 !ExplicitInstantiation) {
Douglas Gregor88b70942009-02-25 22:02:03 +00002367 // There is no prior declaration of this entity, so this
2368 // specialization must be in the same context as the template
2369 // itself.
2370 if (DC != TemplateContext) {
2371 if (isa<TranslationUnitDecl>(TemplateContext))
2372 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregor16df8502009-06-12 22:21:45 +00002373 << PartialSpecialization
Douglas Gregor88b70942009-02-25 22:02:03 +00002374 << ClassTemplate << ScopeSpecifierRange;
2375 else if (isa<NamespaceDecl>(TemplateContext))
2376 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope)
Mike Stump1eb44332009-09-09 15:08:12 +00002377 << PartialSpecialization << ClassTemplate
Douglas Gregor16df8502009-06-12 22:21:45 +00002378 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Douglas Gregor88b70942009-02-25 22:02:03 +00002379
2380 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
2381 }
2382
2383 return false;
2384 }
2385
2386 // We have a previous declaration of this entity. Make sure that
2387 // this redeclaration (or definition) occurs in an enclosing namespace.
2388 if (!CurContext->Encloses(TemplateContext)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002389 // FIXME: In C++98, we would like to turn these errors into warnings,
2390 // dependent on a -Wc++0x flag.
Douglas Gregorff668032009-05-13 18:28:20 +00002391 bool SuppressedDiag = false;
Douglas Gregor16df8502009-06-12 22:21:45 +00002392 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregorff668032009-05-13 18:28:20 +00002393 if (isa<TranslationUnitDecl>(TemplateContext)) {
2394 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2395 Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002396 << Kind << ClassTemplate << ScopeSpecifierRange;
Douglas Gregorff668032009-05-13 18:28:20 +00002397 else
2398 SuppressedDiag = true;
2399 } else if (isa<NamespaceDecl>(TemplateContext)) {
2400 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2401 Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope)
Douglas Gregor16df8502009-06-12 22:21:45 +00002402 << Kind << ClassTemplate
Douglas Gregorff668032009-05-13 18:28:20 +00002403 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Mike Stump1eb44332009-09-09 15:08:12 +00002404 else
Douglas Gregorff668032009-05-13 18:28:20 +00002405 SuppressedDiag = true;
2406 }
Mike Stump1eb44332009-09-09 15:08:12 +00002407
Douglas Gregorff668032009-05-13 18:28:20 +00002408 if (!SuppressedDiag)
2409 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
Douglas Gregor88b70942009-02-25 22:02:03 +00002410 }
2411
2412 return false;
2413}
2414
Douglas Gregore94866f2009-06-12 21:21:02 +00002415/// \brief Check the non-type template arguments of a class template
2416/// partial specialization according to C++ [temp.class.spec]p9.
2417///
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002418/// \param TemplateParams the template parameters of the primary class
2419/// template.
2420///
2421/// \param TemplateArg the template arguments of the class template
2422/// partial specialization.
2423///
2424/// \param MirrorsPrimaryTemplate will be set true if the class
2425/// template partial specialization arguments are identical to the
2426/// implicit template arguments of the primary template. This is not
2427/// necessarily an error (C++0x), and it is left to the caller to diagnose
2428/// this condition when it is an error.
2429///
Douglas Gregore94866f2009-06-12 21:21:02 +00002430/// \returns true if there was an error, false otherwise.
2431bool Sema::CheckClassTemplatePartialSpecializationArgs(
2432 TemplateParameterList *TemplateParams,
Anders Carlsson6360be72009-06-13 18:20:51 +00002433 const TemplateArgumentListBuilder &TemplateArgs,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002434 bool &MirrorsPrimaryTemplate) {
Douglas Gregore94866f2009-06-12 21:21:02 +00002435 // FIXME: the interface to this function will have to change to
2436 // accommodate variadic templates.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002437 MirrorsPrimaryTemplate = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Anders Carlssonfb250522009-06-23 01:26:57 +00002439 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Douglas Gregore94866f2009-06-12 21:21:02 +00002441 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002442 // Determine whether the template argument list of the partial
2443 // specialization is identical to the implicit argument list of
2444 // the primary template. The caller may need to diagnostic this as
2445 // an error per C++ [temp.class.spec]p9b3.
2446 if (MirrorsPrimaryTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00002447 if (TemplateTypeParmDecl *TTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002448 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
2449 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
Anders Carlsson6360be72009-06-13 18:20:51 +00002450 Context.getCanonicalType(ArgList[I].getAsType()))
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002451 MirrorsPrimaryTemplate = false;
2452 } else if (TemplateTemplateParmDecl *TTP
2453 = dyn_cast<TemplateTemplateParmDecl>(
2454 TemplateParams->getParam(I))) {
2455 // FIXME: We should settle on either Declaration storage or
2456 // Expression storage for template template parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002457 TemplateTemplateParmDecl *ArgDecl
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002458 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Anders Carlsson6360be72009-06-13 18:20:51 +00002459 ArgList[I].getAsDecl());
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002460 if (!ArgDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002461 if (DeclRefExpr *DRE
Anders Carlsson6360be72009-06-13 18:20:51 +00002462 = dyn_cast_or_null<DeclRefExpr>(ArgList[I].getAsExpr()))
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002463 ArgDecl = dyn_cast<TemplateTemplateParmDecl>(DRE->getDecl());
2464
2465 if (!ArgDecl ||
2466 ArgDecl->getIndex() != TTP->getIndex() ||
2467 ArgDecl->getDepth() != TTP->getDepth())
2468 MirrorsPrimaryTemplate = false;
2469 }
2470 }
2471
Mike Stump1eb44332009-09-09 15:08:12 +00002472 NonTypeTemplateParmDecl *Param
Douglas Gregore94866f2009-06-12 21:21:02 +00002473 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002474 if (!Param) {
Douglas Gregore94866f2009-06-12 21:21:02 +00002475 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002476 }
2477
Anders Carlsson6360be72009-06-13 18:20:51 +00002478 Expr *ArgExpr = ArgList[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002479 if (!ArgExpr) {
2480 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00002481 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002482 }
Douglas Gregore94866f2009-06-12 21:21:02 +00002483
2484 // C++ [temp.class.spec]p8:
2485 // A non-type argument is non-specialized if it is the name of a
2486 // non-type parameter. All other non-type arguments are
2487 // specialized.
2488 //
2489 // Below, we check the two conditions that only apply to
2490 // specialized non-type arguments, so skip any non-specialized
2491 // arguments.
2492 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Mike Stump1eb44332009-09-09 15:08:12 +00002493 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002494 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002495 if (MirrorsPrimaryTemplate &&
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002496 (Param->getIndex() != NTTP->getIndex() ||
2497 Param->getDepth() != NTTP->getDepth()))
2498 MirrorsPrimaryTemplate = false;
2499
Douglas Gregore94866f2009-06-12 21:21:02 +00002500 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002501 }
Douglas Gregore94866f2009-06-12 21:21:02 +00002502
2503 // C++ [temp.class.spec]p9:
2504 // Within the argument list of a class template partial
2505 // specialization, the following restrictions apply:
2506 // -- A partially specialized non-type argument expression
2507 // shall not involve a template parameter of the partial
2508 // specialization except when the argument expression is a
2509 // simple identifier.
2510 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002511 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00002512 diag::err_dependent_non_type_arg_in_partial_spec)
2513 << ArgExpr->getSourceRange();
2514 return true;
2515 }
2516
2517 // -- The type of a template parameter corresponding to a
2518 // specialized non-type argument shall not be dependent on a
2519 // parameter of the specialization.
2520 if (Param->getType()->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002521 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00002522 diag::err_dependent_typed_non_type_arg_in_partial_spec)
2523 << Param->getType()
2524 << ArgExpr->getSourceRange();
2525 Diag(Param->getLocation(), diag::note_template_param_here);
2526 return true;
2527 }
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002528
2529 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00002530 }
2531
2532 return false;
2533}
2534
Douglas Gregor212e81c2009-03-25 00:13:59 +00002535Sema::DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00002536Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
2537 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00002538 SourceLocation KWLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +00002539 const CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002540 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00002541 SourceLocation TemplateNameLoc,
2542 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00002543 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00002544 SourceLocation *TemplateArgLocs,
2545 SourceLocation RAngleLoc,
2546 AttributeList *Attr,
2547 MultiTemplateParamsArg TemplateParameterLists) {
John McCallf1bbbb42009-09-04 01:14:41 +00002548 assert(TUK == TUK_Declaration || TUK == TUK_Definition);
2549
Douglas Gregorcc636682009-02-17 23:15:12 +00002550 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00002551 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00002552 ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002553 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
Douglas Gregorcc636682009-02-17 23:15:12 +00002554
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002555 bool isPartialSpecialization = false;
2556
Douglas Gregor88b70942009-02-25 22:02:03 +00002557 // Check the validity of the template headers that introduce this
2558 // template.
Douglas Gregor05396e22009-08-25 17:23:04 +00002559 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00002560 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
2561 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002562 TemplateParameterLists.size());
2563 if (TemplateParams && TemplateParams->size() > 0) {
2564 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00002565
Douglas Gregor05396e22009-08-25 17:23:04 +00002566 // C++ [temp.class.spec]p10:
2567 // The template parameter list of a specialization shall not
2568 // contain default template argument values.
2569 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2570 Decl *Param = TemplateParams->getParam(I);
2571 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
2572 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002573 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002574 diag::err_default_arg_in_partial_spec);
2575 TTP->setDefaultArgument(QualType(), SourceLocation(), false);
2576 }
2577 } else if (NonTypeTemplateParmDecl *NTTP
2578 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2579 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002580 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002581 diag::err_default_arg_in_partial_spec)
2582 << DefArg->getSourceRange();
2583 NTTP->setDefaultArgument(0);
2584 DefArg->Destroy(Context);
2585 }
2586 } else {
2587 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
2588 if (Expr *DefArg = TTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002589 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002590 diag::err_default_arg_in_partial_spec)
2591 << DefArg->getSourceRange();
2592 TTP->setDefaultArgument(0);
2593 DefArg->Destroy(Context);
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002594 }
2595 }
2596 }
Douglas Gregor05396e22009-08-25 17:23:04 +00002597 } else if (!TemplateParams)
2598 Diag(KWLoc, diag::err_template_spec_needs_header)
2599 << CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor88b70942009-02-25 22:02:03 +00002600
Douglas Gregorcc636682009-02-17 23:15:12 +00002601 // Check that the specialization uses the same tag kind as the
2602 // original template.
2603 TagDecl::TagKind Kind;
2604 switch (TagSpec) {
2605 default: assert(0 && "Unknown tag type!");
2606 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2607 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2608 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2609 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002610 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00002611 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002612 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002613 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00002614 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00002615 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00002616 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00002617 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00002618 diag::note_previous_use);
2619 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2620 }
2621
Douglas Gregor40808ce2009-03-09 23:48:35 +00002622 // Translate the parser's template argument list in our AST format.
2623 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2624 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2625
Douglas Gregorcc636682009-02-17 23:15:12 +00002626 // Check that the template argument list is well-formed for this
2627 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00002628 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2629 TemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00002630 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson6360be72009-06-13 18:20:51 +00002631 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregor16134c62009-07-01 00:28:38 +00002632 RAngleLoc, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00002633 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00002634
Mike Stump1eb44332009-09-09 15:08:12 +00002635 assert((Converted.structuredSize() ==
Douglas Gregorcc636682009-02-17 23:15:12 +00002636 ClassTemplate->getTemplateParameters()->size()) &&
2637 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002639 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00002640 // corresponds to these arguments.
2641 llvm::FoldingSetNodeID ID;
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002642 if (isPartialSpecialization) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002643 bool MirrorsPrimaryTemplate;
Douglas Gregore94866f2009-06-12 21:21:02 +00002644 if (CheckClassTemplatePartialSpecializationArgs(
2645 ClassTemplate->getTemplateParameters(),
Anders Carlssonfb250522009-06-23 01:26:57 +00002646 Converted, MirrorsPrimaryTemplate))
Douglas Gregore94866f2009-06-12 21:21:02 +00002647 return true;
2648
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002649 if (MirrorsPrimaryTemplate) {
2650 // C++ [temp.class.spec]p9b3:
2651 //
Mike Stump1eb44332009-09-09 15:08:12 +00002652 // -- The argument list of the specialization shall not be identical
2653 // to the implicit argument list of the primary template.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002654 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
John McCall0f434ec2009-07-31 02:45:11 +00002655 << (TUK == TUK_Definition)
Mike Stump1eb44332009-09-09 15:08:12 +00002656 << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002657 RAngleLoc));
John McCall0f434ec2009-07-31 02:45:11 +00002658 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002659 ClassTemplate->getIdentifier(),
2660 TemplateNameLoc,
2661 Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +00002662 TemplateParams,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00002663 AS_none);
2664 }
2665
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002666 // FIXME: Template parameter list matters, too
Mike Stump1eb44332009-09-09 15:08:12 +00002667 ClassTemplatePartialSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002668 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002669 Converted.flatSize(),
2670 Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002671 } else
Anders Carlsson1c5976e2009-06-05 03:43:12 +00002672 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002673 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002674 Converted.flatSize(),
2675 Context);
Douglas Gregorcc636682009-02-17 23:15:12 +00002676 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002677 ClassTemplateSpecializationDecl *PrevDecl = 0;
2678
2679 if (isPartialSpecialization)
2680 PrevDecl
Mike Stump1eb44332009-09-09 15:08:12 +00002681 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002682 InsertPos);
2683 else
2684 PrevDecl
2685 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00002686
2687 ClassTemplateSpecializationDecl *Specialization = 0;
2688
Douglas Gregor88b70942009-02-25 22:02:03 +00002689 // Check whether we can declare a class template specialization in
2690 // the current scope.
2691 if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00002692 TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002693 SS.getRange(),
Douglas Gregor16df8502009-06-12 22:21:45 +00002694 isPartialSpecialization,
Douglas Gregorff668032009-05-13 18:28:20 +00002695 /*ExplicitInstantiation=*/false))
Douglas Gregor212e81c2009-03-25 00:13:59 +00002696 return true;
Douglas Gregor88b70942009-02-25 22:02:03 +00002697
Douglas Gregorb88e8882009-07-30 17:40:51 +00002698 // The canonical type
2699 QualType CanonType;
Douglas Gregorcc636682009-02-17 23:15:12 +00002700 if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
2701 // Since the only prior class template specialization with these
2702 // arguments was referenced but not declared, reuse that
2703 // declaration node as our own, updating its source location to
2704 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00002705 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00002706 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00002707 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00002708 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002709 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00002710 // Build the canonical type that describes the converted template
2711 // arguments of the class template partial specialization.
2712 CanonType = Context.getTemplateSpecializationType(
2713 TemplateName(ClassTemplate),
2714 Converted.getFlatArguments(),
2715 Converted.flatSize());
2716
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002717 // Create a new class template partial specialization declaration node.
Mike Stump1eb44332009-09-09 15:08:12 +00002718 TemplateParameterList *TemplateParams
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002719 = static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
2720 ClassTemplatePartialSpecializationDecl *PrevPartial
2721 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002722 ClassTemplatePartialSpecializationDecl *Partial
2723 = ClassTemplatePartialSpecializationDecl::Create(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002724 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00002725 TemplateNameLoc,
2726 TemplateParams,
2727 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00002728 Converted,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00002729 PrevPartial);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002730
2731 if (PrevPartial) {
2732 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
2733 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
2734 } else {
2735 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
2736 }
2737 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00002738
2739 // Check that all of the template parameters of the class template
2740 // partial specialization are deducible from the template
2741 // arguments. If not, this class template partial specialization
2742 // will never be used.
2743 llvm::SmallVector<bool, 8> DeducibleParams;
2744 DeducibleParams.resize(TemplateParams->size());
Douglas Gregore73bb602009-09-14 21:25:05 +00002745 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
2746 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00002747 unsigned NumNonDeducible = 0;
2748 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
2749 if (!DeducibleParams[I])
2750 ++NumNonDeducible;
2751
2752 if (NumNonDeducible) {
2753 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
2754 << (NumNonDeducible > 1)
2755 << SourceRange(TemplateNameLoc, RAngleLoc);
2756 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
2757 if (!DeducibleParams[I]) {
2758 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
2759 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00002760 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00002761 diag::note_partial_spec_unused_parameter)
2762 << Param->getDeclName();
2763 else
Mike Stump1eb44332009-09-09 15:08:12 +00002764 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00002765 diag::note_partial_spec_unused_parameter)
2766 << std::string("<anonymous>");
2767 }
2768 }
2769 }
Douglas Gregorcc636682009-02-17 23:15:12 +00002770 } else {
2771 // Create a new class template specialization declaration node for
2772 // this explicit specialization.
2773 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00002774 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorcc636682009-02-17 23:15:12 +00002775 ClassTemplate->getDeclContext(),
2776 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002777 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00002778 Converted,
Douglas Gregorcc636682009-02-17 23:15:12 +00002779 PrevDecl);
2780
2781 if (PrevDecl) {
2782 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
2783 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
2784 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002785 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregorcc636682009-02-17 23:15:12 +00002786 InsertPos);
2787 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00002788
2789 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00002790 }
2791
2792 // Note that this is an explicit specialization.
2793 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
2794
2795 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00002796 if (TUK == TUK_Definition) {
Douglas Gregorcc636682009-02-17 23:15:12 +00002797 if (RecordDecl *Def = Specialization->getDefinition(Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002798 // FIXME: Should also handle explicit specialization after implicit
2799 // instantiation with a special diagnostic.
Douglas Gregorcc636682009-02-17 23:15:12 +00002800 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002801 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002802 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00002803 Diag(Def->getLocation(), diag::note_previous_definition);
2804 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00002805 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00002806 }
2807 }
2808
Douglas Gregorfc705b82009-02-26 22:19:44 +00002809 // Build the fully-sugared type for this class template
2810 // specialization as the user wrote in the specialization
2811 // itself. This means that we'll pretty-print the type retrieved
2812 // from the specialization's declaration the way that the user
2813 // actually wrote the specialization, rather than formatting the
2814 // name based on the "canonical" representation used to store the
2815 // template arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002816 QualType WrittenTy
2817 = Context.getTemplateSpecializationType(Name,
Anders Carlsson6360be72009-06-13 18:20:51 +00002818 TemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +00002819 TemplateArgs.size(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00002820 CanonType);
Douglas Gregor7532dc62009-03-30 22:58:21 +00002821 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002822 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00002823
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00002824 // C++ [temp.expl.spec]p9:
2825 // A template explicit specialization is in the scope of the
2826 // namespace in which the template was defined.
2827 //
2828 // We actually implement this paragraph where we set the semantic
2829 // context (in the creation of the ClassTemplateSpecializationDecl),
2830 // but we also maintain the lexical context where the actual
2831 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00002832 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Douglas Gregorcc636682009-02-17 23:15:12 +00002834 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00002835 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00002836 Specialization->startDefinition();
2837
2838 // Add the specialization into its lexical context, so that it can
2839 // be seen when iterating through the list of declarations in that
2840 // context. However, specializations are not found by name lookup.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002841 CurContext->addDecl(Specialization);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002842 return DeclPtrTy::make(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00002843}
Douglas Gregord57959a2009-03-27 23:10:48 +00002844
Mike Stump1eb44332009-09-09 15:08:12 +00002845Sema::DeclPtrTy
2846Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00002847 MultiTemplateParamsArg TemplateParameterLists,
2848 Declarator &D) {
2849 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
2850}
2851
Mike Stump1eb44332009-09-09 15:08:12 +00002852Sema::DeclPtrTy
2853Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00002854 MultiTemplateParamsArg TemplateParameterLists,
2855 Declarator &D) {
2856 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
2857 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
2858 "Not a function declarator!");
2859 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Mike Stump1eb44332009-09-09 15:08:12 +00002860
Douglas Gregor52591bf2009-06-24 00:54:41 +00002861 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00002862 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00002863 }
Mike Stump1eb44332009-09-09 15:08:12 +00002864
Douglas Gregor52591bf2009-06-24 00:54:41 +00002865 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00002866
2867 DeclPtrTy DP = HandleDeclarator(ParentScope, D,
Douglas Gregor52591bf2009-06-24 00:54:41 +00002868 move(TemplateParameterLists),
2869 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00002870 if (FunctionTemplateDecl *FunctionTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +00002871 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
Mike Stump1eb44332009-09-09 15:08:12 +00002872 return ActOnStartOfFunctionDef(FnBodyScope,
Douglas Gregore53060f2009-06-25 22:08:12 +00002873 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
Douglas Gregorf59a56e2009-07-21 23:53:31 +00002874 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
2875 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
Douglas Gregore53060f2009-06-25 22:08:12 +00002876 return DeclPtrTy();
Douglas Gregor52591bf2009-06-24 00:54:41 +00002877}
2878
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00002879// Explicit instantiation of a class template specialization
Douglas Gregor45f96552009-09-04 06:33:52 +00002880// FIXME: Implement extern template semantics
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002881Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00002882Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00002883 SourceLocation ExternLoc,
2884 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002885 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002886 SourceLocation KWLoc,
2887 const CXXScopeSpec &SS,
2888 TemplateTy TemplateD,
2889 SourceLocation TemplateNameLoc,
2890 SourceLocation LAngleLoc,
2891 ASTTemplateArgsPtr TemplateArgsIn,
2892 SourceLocation *TemplateArgLocs,
2893 SourceLocation RAngleLoc,
2894 AttributeList *Attr) {
2895 // Find the class template we're specializing
2896 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00002897 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002898 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
2899
2900 // Check that the specialization uses the same tag kind as the
2901 // original template.
2902 TagDecl::TagKind Kind;
2903 switch (TagSpec) {
2904 default: assert(0 && "Unknown tag type!");
2905 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2906 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2907 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2908 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002909 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00002910 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00002911 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002912 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002913 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00002914 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002915 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00002916 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002917 diag::note_previous_use);
2918 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2919 }
2920
Douglas Gregorff668032009-05-13 18:28:20 +00002921 // C++0x [temp.explicit]p2:
2922 // [...] An explicit instantiation shall appear in an enclosing
2923 // namespace of its template. [...]
2924 //
2925 // This is C++ DR 275.
2926 if (CheckClassTemplateSpecializationScope(ClassTemplate, 0,
Mike Stump1eb44332009-09-09 15:08:12 +00002927 TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002928 SS.getRange(),
Douglas Gregor16df8502009-06-12 22:21:45 +00002929 /*PartialSpecialization=*/false,
Douglas Gregorff668032009-05-13 18:28:20 +00002930 /*ExplicitInstantiation=*/true))
2931 return true;
2932
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002933 // Translate the parser's template argument list in our AST format.
2934 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2935 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2936
2937 // Check that the template argument list is well-formed for this
2938 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00002939 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2940 TemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00002941 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson9bff9a92009-06-05 02:12:32 +00002942 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregor16134c62009-07-01 00:28:38 +00002943 RAngleLoc, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002944 return true;
2945
Mike Stump1eb44332009-09-09 15:08:12 +00002946 assert((Converted.structuredSize() ==
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002947 ClassTemplate->getTemplateParameters()->size()) &&
2948 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002950 // Find the class template specialization declaration that
2951 // corresponds to these arguments.
2952 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00002953 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00002954 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00002955 Converted.flatSize(),
2956 Context);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002957 void *InsertPos = 0;
2958 ClassTemplateSpecializationDecl *PrevDecl
2959 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
2960
2961 ClassTemplateSpecializationDecl *Specialization = 0;
2962
Douglas Gregorff668032009-05-13 18:28:20 +00002963 bool SpecializationRequiresInstantiation = true;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002964 if (PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002965 if (PrevDecl->getSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002966 == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002967 // This particular specialization has already been declared or
2968 // instantiated. We cannot explicitly instantiate it.
Douglas Gregorff668032009-05-13 18:28:20 +00002969 Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate)
2970 << Context.getTypeDeclType(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002971 Diag(PrevDecl->getLocation(),
Douglas Gregorff668032009-05-13 18:28:20 +00002972 diag::note_previous_explicit_instantiation);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00002973 return DeclPtrTy::make(PrevDecl);
2974 }
2975
Douglas Gregorff668032009-05-13 18:28:20 +00002976 if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00002977 // C++ DR 259, C++0x [temp.explicit]p4:
Douglas Gregorff668032009-05-13 18:28:20 +00002978 // For a given set of template parameters, if an explicit
2979 // instantiation of a template appears after a declaration of
2980 // an explicit specialization for that template, the explicit
2981 // instantiation has no effect.
2982 if (!getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00002983 Diag(TemplateNameLoc,
Douglas Gregorff668032009-05-13 18:28:20 +00002984 diag::ext_explicit_instantiation_after_specialization)
2985 << Context.getTypeDeclType(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002986 Diag(PrevDecl->getLocation(),
Douglas Gregorff668032009-05-13 18:28:20 +00002987 diag::note_previous_template_specialization);
2988 }
2989
2990 // Create a new class template specialization declaration node
2991 // for this explicit specialization. This node is only used to
2992 // record the existence of this explicit instantiation for
2993 // accurate reproduction of the source code; we don't actually
2994 // use it for anything, since it is semantically irrelevant.
2995 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00002996 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorff668032009-05-13 18:28:20 +00002997 ClassTemplate->getDeclContext(),
2998 TemplateNameLoc,
2999 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00003000 Converted, 0);
Douglas Gregorff668032009-05-13 18:28:20 +00003001 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003002 CurContext->addDecl(Specialization);
Douglas Gregor52604ab2009-09-11 21:19:12 +00003003 return DeclPtrTy::make(PrevDecl);
Douglas Gregorff668032009-05-13 18:28:20 +00003004 }
3005
3006 // If we have already (implicitly) instantiated this
3007 // specialization, there is less work to do.
3008 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
3009 SpecializationRequiresInstantiation = false;
3010
Douglas Gregor52604ab2009-09-11 21:19:12 +00003011 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation ||
3012 PrevDecl->getSpecializationKind() == TSK_Undeclared) {
3013 // Since the only prior class template specialization with these
3014 // arguments was referenced but not declared, reuse that
3015 // declaration node as our own, updating its source location to
3016 // reflect our new declaration.
3017 Specialization = PrevDecl;
3018 Specialization->setLocation(TemplateNameLoc);
3019 PrevDecl = 0;
3020 }
3021 }
3022
3023 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003024 // Create a new class template specialization declaration node for
3025 // this explicit specialization.
3026 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00003027 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003028 ClassTemplate->getDeclContext(),
3029 TemplateNameLoc,
3030 ClassTemplate,
Douglas Gregor52604ab2009-09-11 21:19:12 +00003031 Converted, PrevDecl);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003032
Douglas Gregor52604ab2009-09-11 21:19:12 +00003033 if (PrevDecl) {
3034 // Remove the previous declaration from the folding set, since we want
3035 // to introduce a new declaration.
3036 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
3037 ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
3038 }
3039
3040 // Insert the new specialization.
3041 ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003042 }
3043
3044 // Build the fully-sugared type for this explicit instantiation as
3045 // the user wrote in the explicit instantiation itself. This means
3046 // that we'll pretty-print the type retrieved from the
3047 // specialization's declaration the way that the user actually wrote
3048 // the explicit instantiation, rather than formatting the name based
3049 // on the "canonical" representation used to store the template
3050 // arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00003051 QualType WrittenTy
3052 = Context.getTemplateSpecializationType(Name,
Anders Carlssonf4e2a2c2009-06-05 02:45:24 +00003053 TemplateArgs.data(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003054 TemplateArgs.size(),
3055 Context.getTypeDeclType(Specialization));
3056 Specialization->setTypeAsWritten(WrittenTy);
3057 TemplateArgsIn.release();
3058
3059 // Add the explicit instantiation into its lexical context. However,
3060 // since explicit instantiations are never found by name lookup, we
3061 // just put it into the declaration context directly.
3062 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003063 CurContext->addDecl(Specialization);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003064
John McCall9cc78072009-09-11 07:25:08 +00003065 Specialization->setPointOfInstantiation(TemplateNameLoc);
3066
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003067 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003068 // A definition of a class template or class member template
3069 // shall be in scope at the point of the explicit instantiation of
3070 // the class template or class member template.
3071 //
3072 // This check comes when we actually try to perform the
3073 // instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003074 TemplateSpecializationKind TSK
Mike Stump1eb44332009-09-09 15:08:12 +00003075 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003076 : TSK_ExplicitInstantiationDeclaration;
Douglas Gregore2c31ff2009-05-15 17:59:04 +00003077 if (SpecializationRequiresInstantiation)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003078 InstantiateClassTemplateSpecialization(Specialization, TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00003079 else // Instantiate the members of this class template specialization.
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003080 InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization,
3081 TSK);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00003082
3083 return DeclPtrTy::make(Specialization);
3084}
3085
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003086// Explicit instantiation of a member class of a class template.
3087Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00003088Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00003089 SourceLocation ExternLoc,
3090 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00003091 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003092 SourceLocation KWLoc,
3093 const CXXScopeSpec &SS,
3094 IdentifierInfo *Name,
3095 SourceLocation NameLoc,
3096 AttributeList *Attr) {
3097
Douglas Gregor402abb52009-05-28 23:31:59 +00003098 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003099 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00003100 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
Douglas Gregor7cdbc582009-07-22 23:48:44 +00003101 KWLoc, SS, Name, NameLoc, Attr, AS_none,
John McCallc4e70192009-09-11 04:59:25 +00003102 MultiTemplateParamsArg(*this, 0, 0),
3103 Owned, IsDependent);
3104 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
3105
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003106 if (!TagD)
3107 return true;
3108
3109 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
3110 if (Tag->isEnum()) {
3111 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
3112 << Context.getTypeDeclType(Tag);
3113 return true;
3114 }
3115
Douglas Gregord0c87372009-05-27 17:30:49 +00003116 if (Tag->isInvalidDecl())
3117 return true;
3118
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003119 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
3120 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
3121 if (!Pattern) {
3122 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
3123 << Context.getTypeDeclType(Record);
3124 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
3125 return true;
3126 }
3127
3128 // C++0x [temp.explicit]p2:
3129 // [...] An explicit instantiation shall appear in an enclosing
3130 // namespace of its template. [...]
3131 //
3132 // This is C++ DR 275.
3133 if (getLangOptions().CPlusPlus0x) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003134 // FIXME: In C++98, we would like to turn these errors into warnings,
3135 // dependent on a -Wc++0x flag.
Mike Stump1eb44332009-09-09 15:08:12 +00003136 DeclContext *PatternContext
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003137 = Pattern->getDeclContext()->getEnclosingNamespaceContext();
3138 if (!CurContext->Encloses(PatternContext)) {
3139 Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope)
3140 << Record << cast<NamedDecl>(PatternContext) << SS.getRange();
3141 Diag(Pattern->getLocation(), diag::note_previous_declaration);
3142 }
3143 }
3144
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003145 TemplateSpecializationKind TSK
Mike Stump1eb44332009-09-09 15:08:12 +00003146 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003147 : TSK_ExplicitInstantiationDeclaration;
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003149 if (!Record->getDefinition(Context)) {
3150 // If the class has a definition, instantiate it (and all of its
3151 // members, recursively).
3152 Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
Mike Stump1eb44332009-09-09 15:08:12 +00003153 if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern,
Douglas Gregor54dabfc2009-05-14 23:26:13 +00003154 getTemplateInstantiationArgs(Record),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003155 TSK))
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003156 return true;
John McCallce3ff2b2009-08-25 22:02:44 +00003157 } else // Instantiate all of the members of the class.
Mike Stump1eb44332009-09-09 15:08:12 +00003158 InstantiateClassMembers(TemplateLoc, Record,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003159 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003160
Mike Stump390b4cc2009-05-16 07:39:55 +00003161 // FIXME: We don't have any representation for explicit instantiations of
3162 // member classes. Such a representation is not needed for compilation, but it
3163 // should be available for clients that want to see all of the declarations in
3164 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00003165 return TagD;
3166}
3167
Douglas Gregord57959a2009-03-27 23:10:48 +00003168Sema::TypeResult
John McCallc4e70192009-09-11 04:59:25 +00003169Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
3170 const CXXScopeSpec &SS, IdentifierInfo *Name,
3171 SourceLocation TagLoc, SourceLocation NameLoc) {
3172 // This has to hold, because SS is expected to be defined.
3173 assert(Name && "Expected a name in a dependent tag");
3174
3175 NestedNameSpecifier *NNS
3176 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3177 if (!NNS)
3178 return true;
3179
3180 QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc));
3181 if (T.isNull())
3182 return true;
3183
3184 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
3185 QualType ElabType = Context.getElaboratedType(T, TagKind);
3186
3187 return ElabType.getAsOpaquePtr();
3188}
3189
3190Sema::TypeResult
Douglas Gregord57959a2009-03-27 23:10:48 +00003191Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3192 const IdentifierInfo &II, SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003193 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00003194 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3195 if (!NNS)
3196 return true;
3197
3198 QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +00003199 if (T.isNull())
3200 return true;
Douglas Gregord57959a2009-03-27 23:10:48 +00003201 return T.getAsOpaquePtr();
3202}
3203
Douglas Gregor17343172009-04-01 00:28:59 +00003204Sema::TypeResult
3205Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3206 SourceLocation TemplateLoc, TypeTy *Ty) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00003207 QualType T = GetTypeFromParser(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00003208 NestedNameSpecifier *NNS
Douglas Gregor17343172009-04-01 00:28:59 +00003209 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump1eb44332009-09-09 15:08:12 +00003210 const TemplateSpecializationType *TemplateId
Douglas Gregor17343172009-04-01 00:28:59 +00003211 = T->getAsTemplateSpecializationType();
3212 assert(TemplateId && "Expected a template specialization type");
3213
Douglas Gregor6946baf2009-09-02 13:05:45 +00003214 if (computeDeclContext(SS, false)) {
3215 // If we can compute a declaration context, then the "typename"
3216 // keyword was superfluous. Just build a QualifiedNameType to keep
3217 // track of the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Douglas Gregor6946baf2009-09-02 13:05:45 +00003219 // FIXME: Note that the QualifiedNameType had the "typename" keyword!
3220 return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
3221 }
Mike Stump1eb44332009-09-09 15:08:12 +00003222
Douglas Gregor6946baf2009-09-02 13:05:45 +00003223 return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
Douglas Gregor17343172009-04-01 00:28:59 +00003224}
3225
Douglas Gregord57959a2009-03-27 23:10:48 +00003226/// \brief Build the type that describes a C++ typename specifier,
3227/// e.g., "typename T::type".
3228QualType
3229Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
3230 SourceRange Range) {
Douglas Gregor42af25f2009-05-11 19:58:34 +00003231 CXXRecordDecl *CurrentInstantiation = 0;
3232 if (NNS->isDependent()) {
3233 CurrentInstantiation = getCurrentInstantiationOf(NNS);
Douglas Gregord57959a2009-03-27 23:10:48 +00003234
Douglas Gregor42af25f2009-05-11 19:58:34 +00003235 // If the nested-name-specifier does not refer to the current
3236 // instantiation, then build a typename type.
3237 if (!CurrentInstantiation)
3238 return Context.getTypenameType(NNS, &II);
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Douglas Gregorde18d122009-09-02 13:12:51 +00003240 // The nested-name-specifier refers to the current instantiation, so the
3241 // "typename" keyword itself is superfluous. In C++03, the program is
Mike Stump1eb44332009-09-09 15:08:12 +00003242 // actually ill-formed. However, DR 382 (in C++0x CD1) allows such
Douglas Gregorde18d122009-09-02 13:12:51 +00003243 // extraneous "typename" keywords, and we retroactively apply this DR to
3244 // C++03 code.
Douglas Gregor42af25f2009-05-11 19:58:34 +00003245 }
Douglas Gregord57959a2009-03-27 23:10:48 +00003246
Douglas Gregor42af25f2009-05-11 19:58:34 +00003247 DeclContext *Ctx = 0;
3248
3249 if (CurrentInstantiation)
3250 Ctx = CurrentInstantiation;
3251 else {
3252 CXXScopeSpec SS;
3253 SS.setScopeRep(NNS);
3254 SS.setRange(Range);
3255 if (RequireCompleteDeclContext(SS))
3256 return QualType();
3257
3258 Ctx = computeDeclContext(SS);
3259 }
Douglas Gregord57959a2009-03-27 23:10:48 +00003260 assert(Ctx && "No declaration context?");
3261
3262 DeclarationName Name(&II);
Mike Stump1eb44332009-09-09 15:08:12 +00003263 LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName,
Douglas Gregord57959a2009-03-27 23:10:48 +00003264 false);
3265 unsigned DiagID = 0;
3266 Decl *Referenced = 0;
3267 switch (Result.getKind()) {
3268 case LookupResult::NotFound:
3269 if (Ctx->isTranslationUnit())
3270 DiagID = diag::err_typename_nested_not_found_global;
3271 else
3272 DiagID = diag::err_typename_nested_not_found;
3273 break;
3274
3275 case LookupResult::Found:
3276 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) {
3277 // We found a type. Build a QualifiedNameType, since the
3278 // typename-specifier was just sugar. FIXME: Tell
3279 // QualifiedNameType that it has a "typename" prefix.
3280 return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
3281 }
3282
3283 DiagID = diag::err_typename_nested_not_type;
3284 Referenced = Result.getAsDecl();
3285 break;
3286
3287 case LookupResult::FoundOverloaded:
3288 DiagID = diag::err_typename_nested_not_type;
3289 Referenced = *Result.begin();
3290 break;
3291
3292 case LookupResult::AmbiguousBaseSubobjectTypes:
3293 case LookupResult::AmbiguousBaseSubobjects:
3294 case LookupResult::AmbiguousReference:
3295 DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range);
3296 return QualType();
3297 }
3298
3299 // If we get here, it's because name lookup did not find a
3300 // type. Emit an appropriate diagnostic and return an error.
3301 if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx))
3302 Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx;
3303 else
3304 Diag(Range.getEnd(), DiagID) << Range << Name;
3305 if (Referenced)
3306 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
3307 << Name;
3308 return QualType();
3309}
Douglas Gregor4a959d82009-08-06 16:20:37 +00003310
3311namespace {
3312 // See Sema::RebuildTypeInCurrentInstantiation
Mike Stump1eb44332009-09-09 15:08:12 +00003313 class VISIBILITY_HIDDEN CurrentInstantiationRebuilder
3314 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00003315 SourceLocation Loc;
3316 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Douglas Gregor4a959d82009-08-06 16:20:37 +00003318 public:
Mike Stump1eb44332009-09-09 15:08:12 +00003319 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00003320 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00003321 DeclarationName Entity)
3322 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00003323 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00003324
3325 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00003326 /// transformed.
3327 ///
3328 /// For the purposes of type reconstruction, a type has already been
3329 /// transformed if it is NULL or if it is not dependent.
3330 bool AlreadyTransformed(QualType T) {
3331 return T.isNull() || !T->isDependentType();
3332 }
Mike Stump1eb44332009-09-09 15:08:12 +00003333
3334 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00003335 /// rebuilt.
3336 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00003337
Douglas Gregor4a959d82009-08-06 16:20:37 +00003338 /// \brief Returns the name of the entity whose type is being rebuilt.
3339 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Douglas Gregor4a959d82009-08-06 16:20:37 +00003341 /// \brief Transforms an expression by returning the expression itself
3342 /// (an identity function).
3343 ///
3344 /// FIXME: This is completely unsafe; we will need to actually clone the
3345 /// expressions.
3346 Sema::OwningExprResult TransformExpr(Expr *E) {
3347 return getSema().Owned(E);
3348 }
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Douglas Gregor4a959d82009-08-06 16:20:37 +00003350 /// \brief Transforms a typename type by determining whether the type now
3351 /// refers to a member of the current instantiation, and then
3352 /// type-checking and building a QualifiedNameType (when possible).
3353 QualType TransformTypenameType(const TypenameType *T);
3354 };
3355}
3356
Mike Stump1eb44332009-09-09 15:08:12 +00003357QualType
Douglas Gregor4a959d82009-08-06 16:20:37 +00003358CurrentInstantiationRebuilder::TransformTypenameType(const TypenameType *T) {
3359 NestedNameSpecifier *NNS
3360 = TransformNestedNameSpecifier(T->getQualifier(),
3361 /*FIXME:*/SourceRange(getBaseLocation()));
3362 if (!NNS)
3363 return QualType();
3364
3365 // If the nested-name-specifier did not change, and we cannot compute the
3366 // context corresponding to the nested-name-specifier, then this
3367 // typename type will not change; exit early.
3368 CXXScopeSpec SS;
3369 SS.setRange(SourceRange(getBaseLocation()));
3370 SS.setScopeRep(NNS);
3371 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
3372 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003373
3374 // Rebuild the typename type, which will probably turn into a
Douglas Gregor4a959d82009-08-06 16:20:37 +00003375 // QualifiedNameType.
3376 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003377 QualType NewTemplateId
Douglas Gregor4a959d82009-08-06 16:20:37 +00003378 = TransformType(QualType(TemplateId, 0));
3379 if (NewTemplateId.isNull())
3380 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Douglas Gregor4a959d82009-08-06 16:20:37 +00003382 if (NNS == T->getQualifier() &&
3383 NewTemplateId == QualType(TemplateId, 0))
3384 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Douglas Gregor4a959d82009-08-06 16:20:37 +00003386 return getDerived().RebuildTypenameType(NNS, NewTemplateId);
3387 }
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Douglas Gregor4a959d82009-08-06 16:20:37 +00003389 return getDerived().RebuildTypenameType(NNS, T->getIdentifier());
3390}
3391
3392/// \brief Rebuilds a type within the context of the current instantiation.
3393///
Mike Stump1eb44332009-09-09 15:08:12 +00003394/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00003395/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00003396/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00003397/// partial specialization thereof). This routine will rebuild that type now
3398/// that we have entered the declarator's scope, which may produce different
3399/// canonical types, e.g.,
3400///
3401/// \code
3402/// template<typename T>
3403/// struct X {
3404/// typedef T* pointer;
3405/// pointer data();
3406/// };
3407///
3408/// template<typename T>
3409/// typename X<T>::pointer X<T>::data() { ... }
3410/// \endcode
3411///
3412/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
3413/// since we do not know that we can look into X<T> when we parsed the type.
3414/// This function will rebuild the type, performing the lookup of "pointer"
3415/// in X<T> and returning a QualifiedNameType whose canonical type is the same
3416/// as the canonical type of T*, allowing the return types of the out-of-line
3417/// definition and the declaration to match.
3418QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
3419 DeclarationName Name) {
3420 if (T.isNull() || !T->isDependentType())
3421 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00003422
Douglas Gregor4a959d82009-08-06 16:20:37 +00003423 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
3424 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00003425}