blob: fceac854ee859b8e648fe6154a35e647afabf3cf [file] [log] [blame]
Douglas Gregordd861062008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregordd861062008-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 Gregor74296542009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregordd861062008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor74296542009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregordd861062008-12-05 18:15:24 +000011
12#include "Sema.h"
Douglas Gregor3f220e82009-08-06 16:20:37 +000013#include "TreeTransform.h"
Douglas Gregord406b032009-02-06 22:42:48 +000014#include "clang/AST/ASTContext.h"
Douglas Gregor1b21c7f2008-12-05 23:32:09 +000015#include "clang/AST/Expr.h"
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +000016#include "clang/AST/ExprCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregordd861062008-12-05 18:15:24 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregor3f220e82009-08-06 16:20:37 +000020#include "llvm/Support/Compiler.h"
Douglas Gregordd861062008-12-05 18:15:24 +000021
22using namespace clang;
23
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +000030
Douglas Gregor681d31d2009-09-02 22:59:36 +000031 if (isa<TemplateDecl>(D))
32 return D;
Mike Stump25cf7602009-09-09 15:08:12 +000033
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +000055
Douglas Gregor681d31d2009-09-02 22:59:36 +000056 return 0;
57 }
Mike Stump25cf7602009-09-09 15:08:12 +000058
Douglas Gregor681d31d2009-09-02 22:59:36 +000059 OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D);
60 if (!Ovl)
61 return 0;
Mike Stump25cf7602009-09-09 15:08:12 +000062
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +000074
Douglas Gregor681d31d2009-09-02 22:59:36 +000075 if (F != FEnd) {
76 // Build an overloaded function decl containing only the
77 // function templates in Ovl.
Mike Stump25cf7602009-09-09 15:08:12 +000078 OverloadedFunctionDecl *OvlTemplate
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +000088
Douglas Gregor681d31d2009-09-02 22:59:36 +000089 return OvlTemplate;
90 }
91
92 return FuncTmpl;
93 }
94 }
Mike Stump25cf7602009-09-09 15:08:12 +000095
Douglas Gregor681d31d2009-09-02 22:59:36 +000096 return 0;
97}
98
99TemplateNameKind Sema::isTemplateName(Scope *S,
Mike Stump25cf7602009-09-09 15:08:12 +0000100 const IdentifierInfo &II,
Douglas Gregor681d31d2009-09-02 22:59:36 +0000101 SourceLocation IdLoc,
Douglas Gregorc03d3302009-08-25 22:51:20 +0000102 const CXXScopeSpec *SS,
Douglas Gregor681d31d2009-09-02 22:59:36 +0000103 TypeTy *ObjectTypePtr,
Douglas Gregorc03d3302009-08-25 22:51:20 +0000104 bool EnteringContext,
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000112 assert((!SS || !SS->isSet()) &&
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000124
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000130 // expression or the declaration context associated with a prior
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000136
Douglas Gregor681d31d2009-09-02 22:59:36 +0000137 Found = LookupQualifiedName(LookupCtx, &II, LookupOrdinaryName);
Mike Stump25cf7602009-09-09 15:08:12 +0000138
Douglas Gregor681d31d2009-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 Stump25cf7602009-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 Gregor681d31d2009-09-02 22:59:36 +0000144 // beginning of a template argument list (14.2) or a less-than operator.
Mike Stump25cf7602009-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 Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000156 // We cannot look into a dependent object type or
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000162
Douglas Gregorc03d3302009-08-25 22:51:20 +0000163 // FIXME: Cope with ambiguous name-lookup results.
Mike Stump25cf7602009-09-09 15:08:12 +0000164 assert(!Found.isAmbiguous() &&
Douglas Gregorc03d3302009-08-25 22:51:20 +0000165 "Cannot handle template name-lookup ambiguities");
Douglas Gregordd13e842009-03-30 22:58:21 +0000166
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000173 // [...] If the lookup in the class of the object expression finds a
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000180
Douglas Gregor681d31d2009-09-02 22:59:36 +0000181 if (!OuterTemplate) {
Mike Stump25cf7602009-09-09 15:08:12 +0000182 // - if the name is not found, the name found in the class of the
Douglas Gregor681d31d2009-09-02 22:59:36 +0000183 // object expression is used, otherwise
184 } else if (!isa<ClassTemplateDecl>(OuterTemplate)) {
Mike Stump25cf7602009-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 Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000190 // entity as the one found in the class of the object expression,
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000198
199 // Recover by taking the template that we found in the object
Douglas Gregor681d31d2009-09-02 22:59:36 +0000200 // expression's type.
Douglas Gregor55216ac2009-03-26 00:10:35 +0000201 }
Mike Stump25cf7602009-09-09 15:08:12 +0000202 }
Douglas Gregor2fa10442008-12-18 19:37:40 +0000203 }
Mike Stump25cf7602009-09-09 15:08:12 +0000204
Douglas Gregor681d31d2009-09-02 22:59:36 +0000205 if (SS && SS->isSet() && !SS->isInvalid()) {
Mike Stump25cf7602009-09-09 15:08:12 +0000206 NestedNameSpecifier *Qualifier
Douglas Gregor681d31d2009-09-02 22:59:36 +0000207 = static_cast<NestedNameSpecifier *>(SS->getScopeRep());
Mike Stump25cf7602009-09-09 15:08:12 +0000208 if (OverloadedFunctionDecl *Ovl
Douglas Gregor681d31d2009-09-02 22:59:36 +0000209 = dyn_cast<OverloadedFunctionDecl>(Template))
Mike Stump25cf7602009-09-09 15:08:12 +0000210 TemplateResult
Douglas Gregor681d31d2009-09-02 22:59:36 +0000211 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
212 Ovl));
213 else
Mike Stump25cf7602009-09-09 15:08:12 +0000214 TemplateResult
Douglas Gregor681d31d2009-09-02 22:59:36 +0000215 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
Mike Stump25cf7602009-09-09 15:08:12 +0000216 cast<TemplateDecl>(Template)));
217 } else if (OverloadedFunctionDecl *Ovl
Douglas Gregor681d31d2009-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 Stump25cf7602009-09-09 15:08:12 +0000224
225 if (isa<ClassTemplateDecl>(Template) ||
Douglas Gregor681d31d2009-09-02 22:59:36 +0000226 isa<TemplateTemplateParmDecl>(Template))
227 return TNK_Type_template;
Mike Stump25cf7602009-09-09 15:08:12 +0000228
229 assert((isa<FunctionTemplateDecl>(Template) ||
Douglas Gregor681d31d2009-09-02 22:59:36 +0000230 isa<OverloadedFunctionDecl>(Template)) &&
231 "Unhandled template kind in Sema::isTemplateName");
232 return TNK_Function_template;
Douglas Gregor2fa10442008-12-18 19:37:40 +0000233}
234
Douglas Gregordd861062008-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 Gregor2715a1f2008-12-08 18:40:42 +0000240 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregordd861062008-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 Stump25cf7602009-09-09 15:08:12 +0000249 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregordd861062008-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 Gregored3a3982009-03-03 04:44:36 +0000255/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregor279272e2009-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 Lattner5261d0c2009-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 Gregor279272e2009-02-04 19:02:06 +0000261 return Temp;
262 }
263 return 0;
264}
265
Douglas Gregordd861062008-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 Stump25cf7602009-09-09 15:08:12 +0000272/// ParamName is the location of the parameter name (if any).
Douglas Gregordd861062008-12-05 18:15:24 +0000273/// If the type parameter has a default argument, it will be added
274/// later via ActOnTypeParameterDefault.
Mike Stump25cf7602009-09-09 15:08:12 +0000275Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
Anders Carlsson9c85fa02009-06-12 19:58:00 +0000276 SourceLocation EllipsisLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000277 SourceLocation KeyLoc,
278 IdentifierInfo *ParamName,
279 SourceLocation ParamNameLoc,
280 unsigned Depth, unsigned Position) {
Mike Stump25cf7602009-09-09 15:08:12 +0000281 assert(S->isTemplateParamScope() &&
282 "Template type parameter not in template parameter scope!");
Douglas Gregordd861062008-12-05 18:15:24 +0000283 bool Invalid = false;
284
285 if (ParamName) {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000286 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000287 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000288 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000289 PrevDecl);
Douglas Gregordd861062008-12-05 18:15:24 +0000290 }
291
Douglas Gregord406b032009-02-06 22:42:48 +0000292 SourceLocation Loc = ParamNameLoc;
293 if (!ParamName)
294 Loc = KeyLoc;
295
Douglas Gregordd861062008-12-05 18:15:24 +0000296 TemplateTypeParmDecl *Param
Mike Stump25cf7602009-09-09 15:08:12 +0000297 = TemplateTypeParmDecl::Create(Context, CurContext, Loc,
298 Depth, Position, ParamName, Typename,
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000299 Ellipsis);
Douglas Gregordd861062008-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 Lattner5261d0c2009-03-28 19:18:32 +0000305 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregordd861062008-12-05 18:15:24 +0000306 IdResolver.AddDecl(Param);
307 }
308
Chris Lattner5261d0c2009-03-28 19:18:32 +0000309 return DeclPtrTy::make(Param);
Douglas Gregordd861062008-12-05 18:15:24 +0000310}
311
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000312/// ActOnTypeParameterDefault - Adds a default argument (the type
Mike Stump25cf7602009-09-09 15:08:12 +0000313/// Default) to the given template type parameter (TypeParam).
314void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000315 SourceLocation EqualLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000316 SourceLocation DefaultLoc,
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000317 TypeTy *DefaultT) {
Mike Stump25cf7602009-09-09 15:08:12 +0000318 TemplateTypeParmDecl *Parm
Chris Lattner5261d0c2009-03-28 19:18:32 +0000319 = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
Argiris Kirtzidisd6802ba2009-08-19 01:28:28 +0000320 // FIXME: Preserve type source info.
321 QualType Default = GetTypeFromParser(DefaultT);
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000322
Anders Carlssona2333782009-06-12 22:30:13 +0000323 // C++0x [temp.param]p9:
324 // A default template-argument may be specified for any kind of
Mike Stump25cf7602009-09-09 15:08:12 +0000325 // template-parameter that is not a template parameter pack.
Anders Carlssona2333782009-06-12 22:30:13 +0000326 if (Parm->isParameterPack()) {
327 Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
Anders Carlssona2333782009-06-12 22:30:13 +0000328 return;
329 }
Mike Stump25cf7602009-09-09 15:08:12 +0000330
Douglas Gregor9225a7e2009-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 Stump25cf7602009-09-09 15:08:12 +0000334
Douglas Gregor9225a7e2009-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 Gregored3a3982009-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 Stump25cf7602009-09-09 15:08:12 +0000349QualType
Douglas Gregored3a3982009-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 Stump25cf7602009-09-09 15:08:12 +0000358 // -- pointer to object or pointer to function,
359 (T->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000360 (T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
361 T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
Mike Stump25cf7602009-09-09 15:08:12 +0000362 // -- reference to object or reference to function,
Douglas Gregored3a3982009-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 Gregordd861062008-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 Lattner5261d0c2009-03-28 19:18:32 +0000392Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
Mike Stump25cf7602009-09-09 15:08:12 +0000393 unsigned Depth,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000394 unsigned Position) {
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000395 DeclaratorInfo *DInfo = 0;
396 QualType T = GetTypeForDeclarator(D, S, &DInfo);
Douglas Gregordd861062008-12-05 18:15:24 +0000397
Douglas Gregor279272e2009-02-04 19:02:06 +0000398 assert(S->isTemplateParamScope() &&
399 "Non-type template parameter not in template parameter scope!");
Douglas Gregordd861062008-12-05 18:15:24 +0000400 bool Invalid = false;
401
402 IdentifierInfo *ParamName = D.getIdentifier();
403 if (ParamName) {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000404 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000405 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000406 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregor279272e2009-02-04 19:02:06 +0000407 PrevDecl);
Douglas Gregordd861062008-12-05 18:15:24 +0000408 }
409
Douglas Gregored3a3982009-03-03 04:44:36 +0000410 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
Douglas Gregor8b90e8e2009-03-09 16:46:39 +0000411 if (T.isNull()) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000412 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregor8b90e8e2009-03-09 16:46:39 +0000413 Invalid = true;
414 }
Douglas Gregor62cdc792009-02-10 17:43:50 +0000415
Douglas Gregordd861062008-12-05 18:15:24 +0000416 NonTypeTemplateParmDecl *Param
417 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000418 Depth, Position, ParamName, T, DInfo);
Douglas Gregordd861062008-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 Lattner5261d0c2009-03-28 19:18:32 +0000424 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregordd861062008-12-05 18:15:24 +0000425 IdResolver.AddDecl(Param);
426 }
Chris Lattner5261d0c2009-03-28 19:18:32 +0000427 return DeclPtrTy::make(Param);
Douglas Gregordd861062008-12-05 18:15:24 +0000428}
Douglas Gregor52473432008-12-24 02:52:09 +0000429
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000430/// \brief Adds a default argument to the given non-type template
431/// parameter.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000432void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000433 SourceLocation EqualLoc,
434 ExprArg DefaultE) {
Mike Stump25cf7602009-09-09 15:08:12 +0000435 NonTypeTemplateParmDecl *TemplateParm
Chris Lattner5261d0c2009-03-28 19:18:32 +0000436 = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000437 Expr *Default = static_cast<Expr *>(DefaultE.get());
Mike Stump25cf7602009-09-09 15:08:12 +0000438
Douglas Gregor9225a7e2009-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 Stump25cf7602009-09-09 15:08:12 +0000442
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000443 // Check the well-formedness of the default template argument.
Douglas Gregor8f378a92009-06-11 18:10:32 +0000444 TemplateArgument Converted;
445 if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
446 Converted)) {
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000447 TemplateParm->setInvalidDecl();
448 return;
449 }
450
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000451 TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000452}
453
Douglas Gregor279272e2009-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 Lattner5261d0c2009-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 Stump25cf7602009-09-09 15:08:12 +0000464 unsigned Position) {
Douglas Gregor279272e2009-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 Lattner5261d0c2009-03-28 19:18:32 +0000486 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor279272e2009-02-04 19:02:06 +0000487 IdResolver.AddDecl(Param);
488 }
489
Chris Lattner5261d0c2009-03-28 19:18:32 +0000490 return DeclPtrTy::make(Param);
Douglas Gregor279272e2009-02-04 19:02:06 +0000491}
492
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000493/// \brief Adds a default argument to the given template template
494/// parameter.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000495void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000496 SourceLocation EqualLoc,
497 ExprArg DefaultE) {
Mike Stump25cf7602009-09-09 15:08:12 +0000498 TemplateTemplateParmDecl *TemplateParm
Chris Lattner5261d0c2009-03-28 19:18:32 +0000499 = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregor9225a7e2009-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 Stump25cf7602009-09-09 15:08:12 +0000503 DeclRefExpr *Default
Douglas Gregor9225a7e2009-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 Stump25cf7602009-09-09 15:08:12 +0000512 Diag(Default->getSourceRange().getBegin(),
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000513 diag::err_template_arg_must_be_template)
514 << Default->getSourceRange();
515 TemplateParm->setInvalidDecl();
516 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000517 }
Douglas Gregor9225a7e2009-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 Gregor52473432008-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 Stump25cf7602009-09-09 15:08:12 +0000532 SourceLocation TemplateLoc,
Douglas Gregor52473432008-12-24 02:52:09 +0000533 SourceLocation LAngleLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000534 DeclPtrTy *Params, unsigned NumParams,
Douglas Gregor52473432008-12-24 02:52:09 +0000535 SourceLocation RAngleLoc) {
536 if (ExportLoc.isValid())
537 Diag(ExportLoc, diag::note_template_export_unsupported);
538
Douglas Gregord406b032009-02-06 22:42:48 +0000539 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
540 (Decl**)Params, NumParams, RAngleLoc);
Douglas Gregor52473432008-12-24 02:52:09 +0000541}
Douglas Gregor279272e2009-02-04 19:02:06 +0000542
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000543Sema::DeclResult
John McCall069c23a2009-07-31 02:45:11 +0000544Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Douglas Gregord406b032009-02-06 22:42:48 +0000545 SourceLocation KWLoc, const CXXScopeSpec &SS,
546 IdentifierInfo *Name, SourceLocation NameLoc,
547 AttributeList *Attr,
Douglas Gregore305ae32009-08-25 17:23:04 +0000548 TemplateParameterList *TemplateParams,
Anders Carlssoned20fb92009-03-26 00:52:18 +0000549 AccessSpecifier AS) {
Mike Stump25cf7602009-09-09 15:08:12 +0000550 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregore305ae32009-08-25 17:23:04 +0000551 "No template parameters");
John McCall069c23a2009-07-31 02:45:11 +0000552 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000553 bool Invalid = false;
Douglas Gregord406b032009-02-06 22:42:48 +0000554
555 // Check that we can declare a template here.
Douglas Gregore305ae32009-08-25 17:23:04 +0000556 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000557 return true;
Douglas Gregord406b032009-02-06 22:42:48 +0000558
559 TagDecl::TagKind Kind;
560 switch (TagSpec) {
561 default: assert(0 && "Unknown tag type!");
562 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
563 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
564 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
565 }
566
567 // There is no such thing as an unnamed class template.
568 if (!Name) {
569 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000570 return true;
Douglas Gregord406b032009-02-06 22:42:48 +0000571 }
572
573 // Find any previous declaration with this name.
Douglas Gregore305ae32009-08-25 17:23:04 +0000574 DeclContext *SemanticContext;
575 LookupResult Previous;
576 if (SS.isNotEmpty() && !SS.isInvalid()) {
577 SemanticContext = computeDeclContext(SS, true);
578 if (!SemanticContext) {
579 // FIXME: Produce a reasonable diagnostic here
580 return true;
581 }
Mike Stump25cf7602009-09-09 15:08:12 +0000582
583 Previous = LookupQualifiedName(SemanticContext, Name, LookupOrdinaryName,
Douglas Gregore305ae32009-08-25 17:23:04 +0000584 true);
585 } else {
586 SemanticContext = CurContext;
587 Previous = LookupName(S, Name, LookupOrdinaryName, true);
588 }
Mike Stump25cf7602009-09-09 15:08:12 +0000589
Douglas Gregord406b032009-02-06 22:42:48 +0000590 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
591 NamedDecl *PrevDecl = 0;
592 if (Previous.begin() != Previous.end())
593 PrevDecl = *Previous.begin();
594
Douglas Gregore305ae32009-08-25 17:23:04 +0000595 if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
Douglas Gregor23521802009-06-17 23:37:01 +0000596 PrevDecl = 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000597
Douglas Gregord406b032009-02-06 22:42:48 +0000598 // If there is a previous declaration with the same name, check
599 // whether this is a valid redeclaration.
Mike Stump25cf7602009-09-09 15:08:12 +0000600 ClassTemplateDecl *PrevClassTemplate
Douglas Gregord406b032009-02-06 22:42:48 +0000601 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
602 if (PrevClassTemplate) {
603 // Ensure that the template parameter lists are compatible.
604 if (!TemplateParameterListsAreEqual(TemplateParams,
605 PrevClassTemplate->getTemplateParameters(),
606 /*Complain=*/true))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000607 return true;
Douglas Gregord406b032009-02-06 22:42:48 +0000608
609 // C++ [temp.class]p4:
610 // In a redeclaration, partial specialization, explicit
611 // specialization or explicit instantiation of a class template,
612 // the class-key shall agree in kind with the original class
613 // template declaration (7.1.5.3).
614 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor625185c2009-05-14 16:41:31 +0000615 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000616 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor3faaa812009-04-01 23:51:29 +0000617 << Name
Mike Stump25cf7602009-09-09 15:08:12 +0000618 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor3faaa812009-04-01 23:51:29 +0000619 PrevRecordDecl->getKindName());
Douglas Gregord406b032009-02-06 22:42:48 +0000620 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregor3faaa812009-04-01 23:51:29 +0000621 Kind = PrevRecordDecl->getTagKind();
Douglas Gregord406b032009-02-06 22:42:48 +0000622 }
623
Douglas Gregord406b032009-02-06 22:42:48 +0000624 // Check for redefinition of this class template.
John McCall069c23a2009-07-31 02:45:11 +0000625 if (TUK == TUK_Definition) {
Douglas Gregord406b032009-02-06 22:42:48 +0000626 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
627 Diag(NameLoc, diag::err_redefinition) << Name;
628 Diag(Def->getLocation(), diag::note_previous_definition);
629 // FIXME: Would it make sense to try to "forget" the previous
630 // definition, as part of error recovery?
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000631 return true;
Douglas Gregord406b032009-02-06 22:42:48 +0000632 }
633 }
634 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
635 // Maybe we will complain about the shadowed template parameter.
636 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
637 // Just pretend that we didn't see the previous declaration.
638 PrevDecl = 0;
639 } else if (PrevDecl) {
640 // C++ [temp]p5:
641 // A class template shall not have the same name as any other
642 // template, class, function, object, enumeration, enumerator,
643 // namespace, or type in the same scope (3.3), except as specified
644 // in (14.5.4).
645 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
646 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000647 return true;
Douglas Gregord406b032009-02-06 22:42:48 +0000648 }
649
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000650 // Check the template parameter list of this declaration, possibly
651 // merging in the template parameter list from the previous class
652 // template declaration.
653 if (CheckTemplateParameterList(TemplateParams,
654 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
655 Invalid = true;
Mike Stump25cf7602009-09-09 15:08:12 +0000656
Douglas Gregor9054f982009-05-10 22:57:19 +0000657 // FIXME: If we had a scope specifier, we better have a previous template
Douglas Gregord406b032009-02-06 22:42:48 +0000658 // declaration!
659
Mike Stump25cf7602009-09-09 15:08:12 +0000660 CXXRecordDecl *NewClass =
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000661 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000662 PrevClassTemplate?
Douglas Gregor12aed0b2009-05-15 19:11:46 +0000663 PrevClassTemplate->getTemplatedDecl() : 0,
664 /*DelayTypeCreation=*/true);
Douglas Gregord406b032009-02-06 22:42:48 +0000665
666 ClassTemplateDecl *NewTemplate
667 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
668 DeclarationName(Name), TemplateParams,
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000669 NewClass, PrevClassTemplate);
Douglas Gregor55216ac2009-03-26 00:10:35 +0000670 NewClass->setDescribedClassTemplate(NewTemplate);
671
Douglas Gregor12aed0b2009-05-15 19:11:46 +0000672 // Build the type for the class template declaration now.
Mike Stump25cf7602009-09-09 15:08:12 +0000673 QualType T =
674 Context.getTypeDeclType(NewClass,
675 PrevClassTemplate?
676 PrevClassTemplate->getTemplatedDecl() : 0);
Douglas Gregor12aed0b2009-05-15 19:11:46 +0000677 assert(T->isDependentType() && "Class template type is not dependent?");
678 (void)T;
679
Anders Carlsson4ca43492009-03-26 01:24:28 +0000680 // Set the access specifier.
681 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump25cf7602009-09-09 15:08:12 +0000682
Douglas Gregord406b032009-02-06 22:42:48 +0000683 // Set the lexical context of these templates
684 NewClass->setLexicalDeclContext(CurContext);
685 NewTemplate->setLexicalDeclContext(CurContext);
686
John McCall069c23a2009-07-31 02:45:11 +0000687 if (TUK == TUK_Definition)
Douglas Gregord406b032009-02-06 22:42:48 +0000688 NewClass->startDefinition();
689
690 if (Attr)
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000691 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregord406b032009-02-06 22:42:48 +0000692
693 PushOnScopeChains(NewTemplate, S);
694
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000695 if (Invalid) {
696 NewTemplate->setInvalidDecl();
697 NewClass->setInvalidDecl();
698 }
Chris Lattner5261d0c2009-03-28 19:18:32 +0000699 return DeclPtrTy::make(NewTemplate);
Douglas Gregord406b032009-02-06 22:42:48 +0000700}
701
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000702/// \brief Checks the validity of a template parameter list, possibly
703/// considering the template parameter list from a previous
704/// declaration.
705///
706/// If an "old" template parameter list is provided, it must be
707/// equivalent (per TemplateParameterListsAreEqual) to the "new"
708/// template parameter list.
709///
710/// \param NewParams Template parameter list for a new template
711/// declaration. This template parameter list will be updated with any
712/// default arguments that are carried through from the previous
713/// template parameter list.
714///
715/// \param OldParams If provided, template parameter list from a
716/// previous declaration of the same template. Default template
717/// arguments will be merged from the old template parameter list to
718/// the new template parameter list.
719///
720/// \returns true if an error occurred, false otherwise.
721bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
722 TemplateParameterList *OldParams) {
723 bool Invalid = false;
Mike Stump25cf7602009-09-09 15:08:12 +0000724
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000725 // C++ [temp.param]p10:
726 // The set of default template-arguments available for use with a
727 // template declaration or definition is obtained by merging the
728 // default arguments from the definition (if in scope) and all
729 // declarations in scope in the same way default function
730 // arguments are (8.3.6).
731 bool SawDefaultArgument = false;
732 SourceLocation PreviousDefaultArgLoc;
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000733
Anders Carlssonb1929502009-06-12 23:20:15 +0000734 bool SawParameterPack = false;
735 SourceLocation ParameterPackLoc;
736
Mike Stumpe0b7e032009-02-11 23:03:27 +0000737 // Dummy initialization to avoid warnings.
Douglas Gregorc5363f42009-02-11 20:46:19 +0000738 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000739 if (OldParams)
740 OldParam = OldParams->begin();
741
742 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
743 NewParamEnd = NewParams->end();
744 NewParam != NewParamEnd; ++NewParam) {
745 // Variables used to diagnose redundant default arguments
746 bool RedundantDefaultArg = false;
747 SourceLocation OldDefaultLoc;
748 SourceLocation NewDefaultLoc;
749
750 // Variables used to diagnose missing default arguments
751 bool MissingDefaultArg = false;
752
Anders Carlssonb1929502009-06-12 23:20:15 +0000753 // C++0x [temp.param]p11:
754 // If a template parameter of a class template is a template parameter pack,
755 // it must be the last template parameter.
756 if (SawParameterPack) {
Mike Stump25cf7602009-09-09 15:08:12 +0000757 Diag(ParameterPackLoc,
Anders Carlssonb1929502009-06-12 23:20:15 +0000758 diag::err_template_param_pack_must_be_last_template_parameter);
759 Invalid = true;
760 }
761
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000762 // Merge default arguments for template type parameters.
763 if (TemplateTypeParmDecl *NewTypeParm
764 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000765 TemplateTypeParmDecl *OldTypeParm
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000766 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000767
Anders Carlssonb1929502009-06-12 23:20:15 +0000768 if (NewTypeParm->isParameterPack()) {
769 assert(!NewTypeParm->hasDefaultArgument() &&
770 "Parameter packs can't have a default argument!");
771 SawParameterPack = true;
772 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump25cf7602009-09-09 15:08:12 +0000773 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000774 NewTypeParm->hasDefaultArgument()) {
775 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
776 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
777 SawDefaultArgument = true;
778 RedundantDefaultArg = true;
779 PreviousDefaultArgLoc = NewDefaultLoc;
780 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
781 // Merge the default argument from the old declaration to the
782 // new declaration.
783 SawDefaultArgument = true;
784 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
785 OldTypeParm->getDefaultArgumentLoc(),
786 true);
787 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
788 } else if (NewTypeParm->hasDefaultArgument()) {
789 SawDefaultArgument = true;
790 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
791 } else if (SawDefaultArgument)
792 MissingDefaultArg = true;
Mike Stump90fc78e2009-08-04 21:02:39 +0000793 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000794 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Mike Stump90fc78e2009-08-04 21:02:39 +0000795 // Merge default arguments for non-type template parameters
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000796 NonTypeTemplateParmDecl *OldNonTypeParm
797 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000798 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000799 NewNonTypeParm->hasDefaultArgument()) {
800 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
801 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
802 SawDefaultArgument = true;
803 RedundantDefaultArg = true;
804 PreviousDefaultArgLoc = NewDefaultLoc;
805 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
806 // Merge the default argument from the old declaration to the
807 // new declaration.
808 SawDefaultArgument = true;
809 // FIXME: We need to create a new kind of "default argument"
810 // expression that points to a previous template template
811 // parameter.
812 NewNonTypeParm->setDefaultArgument(
813 OldNonTypeParm->getDefaultArgument());
814 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
815 } else if (NewNonTypeParm->hasDefaultArgument()) {
816 SawDefaultArgument = true;
817 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
818 } else if (SawDefaultArgument)
Mike Stump25cf7602009-09-09 15:08:12 +0000819 MissingDefaultArg = true;
Mike Stump90fc78e2009-08-04 21:02:39 +0000820 } else {
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000821 // Merge default arguments for template template parameters
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000822 TemplateTemplateParmDecl *NewTemplateParm
823 = cast<TemplateTemplateParmDecl>(*NewParam);
824 TemplateTemplateParmDecl *OldTemplateParm
825 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000826 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000827 NewTemplateParm->hasDefaultArgument()) {
828 OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
829 NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
830 SawDefaultArgument = true;
831 RedundantDefaultArg = true;
832 PreviousDefaultArgLoc = NewDefaultLoc;
833 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
834 // Merge the default argument from the old declaration to the
835 // new declaration.
836 SawDefaultArgument = true;
Mike Stumpe127ae32009-05-16 07:39:55 +0000837 // FIXME: We need to create a new kind of "default argument" expression
838 // that points to a previous template template parameter.
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000839 NewTemplateParm->setDefaultArgument(
840 OldTemplateParm->getDefaultArgument());
841 PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
842 } else if (NewTemplateParm->hasDefaultArgument()) {
843 SawDefaultArgument = true;
844 PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
845 } else if (SawDefaultArgument)
Mike Stump25cf7602009-09-09 15:08:12 +0000846 MissingDefaultArg = true;
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000847 }
848
849 if (RedundantDefaultArg) {
850 // C++ [temp.param]p12:
851 // A template-parameter shall not be given default arguments
852 // by two different declarations in the same scope.
853 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
854 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
855 Invalid = true;
856 } else if (MissingDefaultArg) {
857 // C++ [temp.param]p11:
858 // If a template-parameter has a default template-argument,
859 // all subsequent template-parameters shall have a default
860 // template-argument supplied.
Mike Stump25cf7602009-09-09 15:08:12 +0000861 Diag((*NewParam)->getLocation(),
Douglas Gregor9225a7e2009-02-10 19:49:53 +0000862 diag::err_template_param_default_arg_missing);
863 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
864 Invalid = true;
865 }
866
867 // If we have an old template parameter list that we're merging
868 // in, move on to the next parameter.
869 if (OldParams)
870 ++OldParam;
871 }
872
873 return Invalid;
874}
Douglas Gregor35d81bb2009-02-09 23:23:08 +0000875
Mike Stump25cf7602009-09-09 15:08:12 +0000876/// \brief Match the given template parameter lists to the given scope
Douglas Gregorb462c322009-07-21 23:53:31 +0000877/// specifier, returning the template parameter list that applies to the
878/// name.
879///
880/// \param DeclStartLoc the start of the declaration that has a scope
881/// specifier or a template parameter list.
Mike Stump25cf7602009-09-09 15:08:12 +0000882///
Douglas Gregorb462c322009-07-21 23:53:31 +0000883/// \param SS the scope specifier that will be matched to the given template
884/// parameter lists. This scope specifier precedes a qualified name that is
885/// being declared.
886///
887/// \param ParamLists the template parameter lists, from the outermost to the
888/// innermost template parameter lists.
889///
890/// \param NumParamLists the number of template parameter lists in ParamLists.
891///
Mike Stump25cf7602009-09-09 15:08:12 +0000892/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorb462c322009-07-21 23:53:31 +0000893/// name that is preceded by the scope specifier @p SS. This template
894/// parameter list may be have template parameters (if we're declaring a
Mike Stump25cf7602009-09-09 15:08:12 +0000895/// template) or may have no template parameters (if we're declaring a
Douglas Gregorb462c322009-07-21 23:53:31 +0000896/// template specialization), or may be NULL (if we were's declaring isn't
897/// itself a template).
898TemplateParameterList *
899Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
900 const CXXScopeSpec &SS,
901 TemplateParameterList **ParamLists,
902 unsigned NumParamLists) {
Douglas Gregorb462c322009-07-21 23:53:31 +0000903 // Find the template-ids that occur within the nested-name-specifier. These
904 // template-ids will match up with the template parameter lists.
905 llvm::SmallVector<const TemplateSpecializationType *, 4>
906 TemplateIdsInSpecifier;
907 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
908 NNS; NNS = NNS->getPrefix()) {
Mike Stump25cf7602009-09-09 15:08:12 +0000909 if (const TemplateSpecializationType *SpecType
Douglas Gregorb462c322009-07-21 23:53:31 +0000910 = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) {
911 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
912 if (!Template)
913 continue; // FIXME: should this be an error? probably...
Mike Stump25cf7602009-09-09 15:08:12 +0000914
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000915 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorb462c322009-07-21 23:53:31 +0000916 ClassTemplateSpecializationDecl *SpecDecl
917 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
918 // If the nested name specifier refers to an explicit specialization,
919 // we don't need a template<> header.
Mike Stump25cf7602009-09-09 15:08:12 +0000920 // FIXME: revisit this approach once we cope with specialization
Douglas Gregor1930d202009-07-30 17:40:51 +0000921 // properly.
Douglas Gregorb462c322009-07-21 23:53:31 +0000922 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization)
923 continue;
924 }
Mike Stump25cf7602009-09-09 15:08:12 +0000925
Douglas Gregorb462c322009-07-21 23:53:31 +0000926 TemplateIdsInSpecifier.push_back(SpecType);
927 }
928 }
Mike Stump25cf7602009-09-09 15:08:12 +0000929
Douglas Gregorb462c322009-07-21 23:53:31 +0000930 // Reverse the list of template-ids in the scope specifier, so that we can
931 // more easily match up the template-ids and the template parameter lists.
932 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump25cf7602009-09-09 15:08:12 +0000933
Douglas Gregorb462c322009-07-21 23:53:31 +0000934 SourceLocation FirstTemplateLoc = DeclStartLoc;
935 if (NumParamLists)
936 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump25cf7602009-09-09 15:08:12 +0000937
Douglas Gregorb462c322009-07-21 23:53:31 +0000938 // Match the template-ids found in the specifier to the template parameter
939 // lists.
940 unsigned Idx = 0;
941 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
942 Idx != NumTemplateIds; ++Idx) {
Douglas Gregor1930d202009-07-30 17:40:51 +0000943 QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
944 bool DependentTemplateId = TemplateId->isDependentType();
Douglas Gregorb462c322009-07-21 23:53:31 +0000945 if (Idx >= NumParamLists) {
946 // We have a template-id without a corresponding template parameter
947 // list.
948 if (DependentTemplateId) {
Mike Stump25cf7602009-09-09 15:08:12 +0000949 // FIXME: the location information here isn't great.
950 Diag(SS.getRange().getBegin(),
Douglas Gregorb462c322009-07-21 23:53:31 +0000951 diag::err_template_spec_needs_template_parameters)
Douglas Gregor1930d202009-07-30 17:40:51 +0000952 << TemplateId
Douglas Gregorb462c322009-07-21 23:53:31 +0000953 << SS.getRange();
954 } else {
955 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
956 << SS.getRange()
957 << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
958 "template<> ");
959 }
960 return 0;
961 }
Mike Stump25cf7602009-09-09 15:08:12 +0000962
Douglas Gregorb462c322009-07-21 23:53:31 +0000963 // Check the template parameter list against its corresponding template-id.
Douglas Gregor1930d202009-07-30 17:40:51 +0000964 if (DependentTemplateId) {
Mike Stump25cf7602009-09-09 15:08:12 +0000965 TemplateDecl *Template
Douglas Gregor1930d202009-07-30 17:40:51 +0000966 = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
967
Mike Stump25cf7602009-09-09 15:08:12 +0000968 if (ClassTemplateDecl *ClassTemplate
Douglas Gregor1930d202009-07-30 17:40:51 +0000969 = dyn_cast<ClassTemplateDecl>(Template)) {
970 TemplateParameterList *ExpectedTemplateParams = 0;
971 // Is this template-id naming the primary template?
972 if (Context.hasSameType(TemplateId,
973 ClassTemplate->getInjectedClassNameType(Context)))
974 ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
975 // ... or a partial specialization?
976 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
977 = ClassTemplate->findPartialSpecialization(TemplateId))
978 ExpectedTemplateParams = PartialSpec->getTemplateParameters();
979
980 if (ExpectedTemplateParams)
Mike Stump25cf7602009-09-09 15:08:12 +0000981 TemplateParameterListsAreEqual(ParamLists[Idx],
Douglas Gregor1930d202009-07-30 17:40:51 +0000982 ExpectedTemplateParams,
983 true);
Mike Stump25cf7602009-09-09 15:08:12 +0000984 }
Douglas Gregor1930d202009-07-30 17:40:51 +0000985 } else if (ParamLists[Idx]->size() > 0)
Mike Stump25cf7602009-09-09 15:08:12 +0000986 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregor1930d202009-07-30 17:40:51 +0000987 diag::err_template_param_list_matches_nontemplate)
988 << TemplateId
989 << ParamLists[Idx]->getSourceRange();
Douglas Gregorb462c322009-07-21 23:53:31 +0000990 }
Mike Stump25cf7602009-09-09 15:08:12 +0000991
Douglas Gregorb462c322009-07-21 23:53:31 +0000992 // If there were at least as many template-ids as there were template
993 // parameter lists, then there are no template parameter lists remaining for
994 // the declaration itself.
995 if (Idx >= NumParamLists)
996 return 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000997
Douglas Gregorb462c322009-07-21 23:53:31 +0000998 // If there were too many template parameter lists, complain about that now.
999 if (Idx != NumParamLists - 1) {
1000 while (Idx < NumParamLists - 1) {
Mike Stump25cf7602009-09-09 15:08:12 +00001001 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorb462c322009-07-21 23:53:31 +00001002 diag::err_template_spec_extra_headers)
1003 << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1004 ParamLists[Idx]->getRAngleLoc());
1005 ++Idx;
1006 }
1007 }
Mike Stump25cf7602009-09-09 15:08:12 +00001008
Douglas Gregorb462c322009-07-21 23:53:31 +00001009 // Return the last template parameter list, which corresponds to the
1010 // entity being declared.
1011 return ParamLists[NumParamLists - 1];
1012}
1013
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001014/// \brief Translates template arguments as provided by the parser
1015/// into template arguments used by semantic analysis.
Mike Stump25cf7602009-09-09 15:08:12 +00001016static void
1017translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001018 SourceLocation *TemplateArgLocs,
1019 llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) {
1020 TemplateArgs.reserve(TemplateArgsIn.size());
1021
1022 void **Args = TemplateArgsIn.getArgs();
1023 bool *ArgIsType = TemplateArgsIn.getArgIsType();
1024 for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) {
1025 TemplateArgs.push_back(
1026 ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg],
Argiris Kirtzidisd6802ba2009-08-19 01:28:28 +00001027 //FIXME: Preserve type source info.
1028 Sema::GetTypeFromParser(Args[Arg]))
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001029 : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg])));
1030 }
1031}
1032
Douglas Gregordd13e842009-03-30 22:58:21 +00001033QualType Sema::CheckTemplateIdType(TemplateName Name,
1034 SourceLocation TemplateLoc,
1035 SourceLocation LAngleLoc,
1036 const TemplateArgument *TemplateArgs,
1037 unsigned NumTemplateArgs,
1038 SourceLocation RAngleLoc) {
1039 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregoraabb8502009-03-31 00:43:58 +00001040 if (!Template) {
1041 // The template name does not resolve to a template, so we just
1042 // build a dependent template-id type.
Douglas Gregoraabb8502009-03-31 00:43:58 +00001043 return Context.getTemplateSpecializationType(Name, TemplateArgs,
Douglas Gregor4262bc92009-07-28 23:00:59 +00001044 NumTemplateArgs);
Douglas Gregoraabb8502009-03-31 00:43:58 +00001045 }
Douglas Gregordd13e842009-03-30 22:58:21 +00001046
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001047 // Check that the template argument list is well-formed for this
1048 // template.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001049 TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
1050 NumTemplateArgs);
Mike Stump25cf7602009-09-09 15:08:12 +00001051 if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001052 TemplateArgs, NumTemplateArgs, RAngleLoc,
Douglas Gregorecd63b82009-07-01 00:28:38 +00001053 false, Converted))
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001054 return QualType();
1055
Mike Stump25cf7602009-09-09 15:08:12 +00001056 assert((Converted.structuredSize() ==
Douglas Gregordd13e842009-03-30 22:58:21 +00001057 Template->getTemplateParameters()->size()) &&
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001058 "Converted template argument list is too short!");
1059
1060 QualType CanonType;
1061
Douglas Gregordd13e842009-03-30 22:58:21 +00001062 if (TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001063 TemplateArgs,
1064 NumTemplateArgs)) {
1065 // This class template specialization is a dependent
1066 // type. Therefore, its canonical type is another class template
1067 // specialization type that contains all of the converted
1068 // arguments in canonical form. This ensures that, e.g., A<T> and
1069 // A<T, T> have identical types when A is declared as:
1070 //
1071 // template<typename T, typename U = T> struct A;
Douglas Gregorb88ba412009-05-07 06:41:52 +00001072 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump25cf7602009-09-09 15:08:12 +00001073 CanonType = Context.getTemplateSpecializationType(CanonName,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001074 Converted.getFlatArguments(),
1075 Converted.flatSize());
Mike Stump25cf7602009-09-09 15:08:12 +00001076
Douglas Gregor4262bc92009-07-28 23:00:59 +00001077 // FIXME: CanonType is not actually the canonical type, and unfortunately
1078 // it is a TemplateTypeSpecializationType that we will never use again.
1079 // In the future, we need to teach getTemplateSpecializationType to only
1080 // build the canonical type and return that to us.
1081 CanonType = Context.getCanonicalType(CanonType);
Mike Stump25cf7602009-09-09 15:08:12 +00001082 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregordd13e842009-03-30 22:58:21 +00001083 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001084 // Find the class template specialization declaration that
1085 // corresponds to these arguments.
1086 llvm::FoldingSetNodeID ID;
Mike Stump25cf7602009-09-09 15:08:12 +00001087 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001088 Converted.getFlatArguments(),
Douglas Gregor99eef4a2009-07-29 16:09:57 +00001089 Converted.flatSize(),
1090 Context);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001091 void *InsertPos = 0;
1092 ClassTemplateSpecializationDecl *Decl
1093 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1094 if (!Decl) {
1095 // This is the first time we have referenced this class template
1096 // specialization. Create the canonical declaration and add it to
1097 // the set of specializations.
Mike Stump25cf7602009-09-09 15:08:12 +00001098 Decl = ClassTemplateSpecializationDecl::Create(Context,
Anders Carlssona35faf92009-06-05 03:43:12 +00001099 ClassTemplate->getDeclContext(),
1100 TemplateLoc,
1101 ClassTemplate,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001102 Converted, 0);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001103 ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1104 Decl->setLexicalDeclContext(CurContext);
1105 }
1106
1107 CanonType = Context.getTypeDeclType(Decl);
1108 }
Mike Stump25cf7602009-09-09 15:08:12 +00001109
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001110 // Build the fully-sugared type for this class template
1111 // specialization, which refers back to the class template
1112 // specialization we created or found.
Argiris Kirtzidisd6802ba2009-08-19 01:28:28 +00001113 //FIXME: Preserve type source info.
Douglas Gregordd13e842009-03-30 22:58:21 +00001114 return Context.getTemplateSpecializationType(Name, TemplateArgs,
1115 NumTemplateArgs, CanonType);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001116}
1117
Douglas Gregora08b6c72009-02-17 23:15:12 +00001118Action::TypeResult
Douglas Gregordd13e842009-03-30 22:58:21 +00001119Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump25cf7602009-09-09 15:08:12 +00001120 SourceLocation LAngleLoc,
Douglas Gregordd13e842009-03-30 22:58:21 +00001121 ASTTemplateArgsPtr TemplateArgsIn,
1122 SourceLocation *TemplateArgLocs,
John McCalld321d492009-09-08 17:47:29 +00001123 SourceLocation RAngleLoc) {
Douglas Gregordd13e842009-03-30 22:58:21 +00001124 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor8e458f42009-02-09 18:46:07 +00001125
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001126 // Translate the parser's template argument list in our AST format.
1127 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1128 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001129
Douglas Gregordd13e842009-03-30 22:58:21 +00001130 QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001131 TemplateArgs.data(),
1132 TemplateArgs.size(),
Douglas Gregordd13e842009-03-30 22:58:21 +00001133 RAngleLoc);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001134 TemplateArgsIn.release();
Douglas Gregord7cb0372009-04-01 21:51:26 +00001135
1136 if (Result.isNull())
1137 return true;
1138
John McCalld321d492009-09-08 17:47:29 +00001139 return Result.getAsOpaquePtr();
1140}
John McCallfd025182009-09-04 01:14:41 +00001141
John McCalld321d492009-09-08 17:47:29 +00001142Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1143 TagUseKind TUK,
1144 DeclSpec::TST TagSpec,
1145 SourceLocation TagLoc) {
1146 if (TypeResult.isInvalid())
1147 return Sema::TypeResult();
John McCallfd025182009-09-04 01:14:41 +00001148
John McCalld321d492009-09-08 17:47:29 +00001149 QualType Type = QualType::getFromOpaquePtr(TypeResult.get());
John McCallfd025182009-09-04 01:14:41 +00001150
John McCalld321d492009-09-08 17:47:29 +00001151 // Verify the tag specifier.
1152 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
Mike Stump25cf7602009-09-09 15:08:12 +00001153
John McCalld321d492009-09-08 17:47:29 +00001154 if (const RecordType *RT = Type->getAs<RecordType>()) {
1155 RecordDecl *D = RT->getDecl();
1156
1157 IdentifierInfo *Id = D->getIdentifier();
1158 assert(Id && "templated class must have an identifier");
1159
1160 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1161 Diag(TagLoc, diag::err_use_with_wrong_tag)
1162 << Id
1163 << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1164 D->getKindName());
John McCallfd025182009-09-04 01:14:41 +00001165 }
1166 }
1167
John McCalld321d492009-09-08 17:47:29 +00001168 QualType ElabType = Context.getElaboratedType(Type, TagKind);
1169
1170 return ElabType.getAsOpaquePtr();
Douglas Gregor8e458f42009-02-09 18:46:07 +00001171}
1172
Douglas Gregor28857752009-06-30 22:34:41 +00001173Sema::OwningExprResult Sema::BuildTemplateIdExpr(TemplateName Template,
1174 SourceLocation TemplateNameLoc,
1175 SourceLocation LAngleLoc,
1176 const TemplateArgument *TemplateArgs,
1177 unsigned NumTemplateArgs,
1178 SourceLocation RAngleLoc) {
1179 // FIXME: Can we do any checking at this point? I guess we could check the
1180 // template arguments that we have against the template name, if the template
Mike Stump25cf7602009-09-09 15:08:12 +00001181 // name refers to a single template. That's not a terribly common case,
Douglas Gregor28857752009-06-30 22:34:41 +00001182 // though.
Mike Stump25cf7602009-09-09 15:08:12 +00001183 return Owned(TemplateIdRefExpr::Create(Context,
Douglas Gregor28857752009-06-30 22:34:41 +00001184 /*FIXME: New type?*/Context.OverloadTy,
1185 /*FIXME: Necessary?*/0,
1186 /*FIXME: Necessary?*/SourceRange(),
1187 Template, TemplateNameLoc, LAngleLoc,
Mike Stump25cf7602009-09-09 15:08:12 +00001188 TemplateArgs,
Douglas Gregor28857752009-06-30 22:34:41 +00001189 NumTemplateArgs, RAngleLoc));
1190}
1191
1192Sema::OwningExprResult Sema::ActOnTemplateIdExpr(TemplateTy TemplateD,
1193 SourceLocation TemplateNameLoc,
1194 SourceLocation LAngleLoc,
1195 ASTTemplateArgsPtr TemplateArgsIn,
1196 SourceLocation *TemplateArgLocs,
1197 SourceLocation RAngleLoc) {
1198 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump25cf7602009-09-09 15:08:12 +00001199
Douglas Gregor28857752009-06-30 22:34:41 +00001200 // Translate the parser's template argument list in our AST format.
1201 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1202 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregorf2fedc62009-07-22 20:55:49 +00001203 TemplateArgsIn.release();
Mike Stump25cf7602009-09-09 15:08:12 +00001204
Douglas Gregor28857752009-06-30 22:34:41 +00001205 return BuildTemplateIdExpr(Template, TemplateNameLoc, LAngleLoc,
1206 TemplateArgs.data(), TemplateArgs.size(),
1207 RAngleLoc);
1208}
1209
Douglas Gregord33e3282009-09-01 00:37:14 +00001210Sema::OwningExprResult
1211Sema::ActOnMemberTemplateIdReferenceExpr(Scope *S, ExprArg Base,
1212 SourceLocation OpLoc,
1213 tok::TokenKind OpKind,
1214 const CXXScopeSpec &SS,
1215 TemplateTy TemplateD,
1216 SourceLocation TemplateNameLoc,
1217 SourceLocation LAngleLoc,
1218 ASTTemplateArgsPtr TemplateArgsIn,
1219 SourceLocation *TemplateArgLocs,
1220 SourceLocation RAngleLoc) {
1221 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump25cf7602009-09-09 15:08:12 +00001222
Douglas Gregord33e3282009-09-01 00:37:14 +00001223 // FIXME: We're going to end up looking up the template based on its name,
1224 // twice!
1225 DeclarationName Name;
1226 if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl())
1227 Name = ActualTemplate->getDeclName();
1228 else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl())
1229 Name = Ovl->getDeclName();
1230 else
Douglas Gregorcc00fc02009-09-09 00:23:06 +00001231 Name = Template.getAsDependentTemplateName()->getName();
Mike Stump25cf7602009-09-09 15:08:12 +00001232
Douglas Gregord33e3282009-09-01 00:37:14 +00001233 // Translate the parser's template argument list in our AST format.
1234 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1235 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
1236 TemplateArgsIn.release();
Mike Stump25cf7602009-09-09 15:08:12 +00001237
Douglas Gregord33e3282009-09-01 00:37:14 +00001238 // Do we have the save the actual template name? We might need it...
1239 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, TemplateNameLoc,
1240 Name, true, LAngleLoc,
1241 TemplateArgs.data(), TemplateArgs.size(),
Mike Stump25cf7602009-09-09 15:08:12 +00001242 RAngleLoc, DeclPtrTy(), &SS);
Douglas Gregord33e3282009-09-01 00:37:14 +00001243}
1244
Douglas Gregoraabb8502009-03-31 00:43:58 +00001245/// \brief Form a dependent template name.
1246///
1247/// This action forms a dependent template name given the template
1248/// name and its (presumably dependent) scope specifier. For
1249/// example, given "MetaFun::template apply", the scope specifier \p
1250/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1251/// of the "template" keyword, and "apply" is the \p Name.
Mike Stump25cf7602009-09-09 15:08:12 +00001252Sema::TemplateTy
Douglas Gregoraabb8502009-03-31 00:43:58 +00001253Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
1254 const IdentifierInfo &Name,
1255 SourceLocation NameLoc,
Douglas Gregor681d31d2009-09-02 22:59:36 +00001256 const CXXScopeSpec &SS,
1257 TypeTy *ObjectType) {
Mike Stump25cf7602009-09-09 15:08:12 +00001258 if ((ObjectType &&
Douglas Gregor681d31d2009-09-02 22:59:36 +00001259 computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) ||
1260 (SS.isSet() && computeDeclContext(SS, false))) {
Douglas Gregoraabb8502009-03-31 00:43:58 +00001261 // C++0x [temp.names]p5:
1262 // If a name prefixed by the keyword template is not the name of
1263 // a template, the program is ill-formed. [Note: the keyword
1264 // template may not be applied to non-template members of class
1265 // templates. -end note ] [ Note: as is the case with the
1266 // typename prefix, the template prefix is allowed in cases
1267 // where it is not strictly necessary; i.e., when the
1268 // nested-name-specifier or the expression on the left of the ->
1269 // or . is not dependent on a template-parameter, or the use
1270 // does not appear in the scope of a template. -end note]
1271 //
1272 // Note: C++03 was more strict here, because it banned the use of
1273 // the "template" keyword prior to a template-name that was not a
1274 // dependent name. C++ DR468 relaxed this requirement (the
1275 // "template" keyword is now permitted). We follow the C++0x
1276 // rules, even in C++03 mode, retroactively applying the DR.
1277 TemplateTy Template;
Mike Stump25cf7602009-09-09 15:08:12 +00001278 TemplateNameKind TNK = isTemplateName(0, Name, NameLoc, &SS, ObjectType,
Douglas Gregor681d31d2009-09-02 22:59:36 +00001279 false, Template);
Douglas Gregoraabb8502009-03-31 00:43:58 +00001280 if (TNK == TNK_Non_template) {
1281 Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1282 << &Name;
1283 return TemplateTy();
1284 }
1285
1286 return Template;
1287 }
1288
Mike Stump25cf7602009-09-09 15:08:12 +00001289 NestedNameSpecifier *Qualifier
Douglas Gregor681d31d2009-09-02 22:59:36 +00001290 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregoraabb8502009-03-31 00:43:58 +00001291 return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name));
1292}
1293
Mike Stump25cf7602009-09-09 15:08:12 +00001294bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
Anders Carlssonfdd33cc2009-06-13 00:33:33 +00001295 const TemplateArgument &Arg,
1296 TemplateArgumentListBuilder &Converted) {
1297 // Check template type parameter.
1298 if (Arg.getKind() != TemplateArgument::Type) {
1299 // C++ [temp.arg.type]p1:
1300 // A template-argument for a template-parameter which is a
1301 // type shall be a type-id.
1302
1303 // We have a template type parameter but the template argument
1304 // is not a type.
1305 Diag(Arg.getLocation(), diag::err_template_arg_must_be_type);
1306 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump25cf7602009-09-09 15:08:12 +00001307
Anders Carlssonfdd33cc2009-06-13 00:33:33 +00001308 return true;
Mike Stump25cf7602009-09-09 15:08:12 +00001309 }
Anders Carlssonfdd33cc2009-06-13 00:33:33 +00001310
1311 if (CheckTemplateArgument(Param, Arg.getAsType(), Arg.getLocation()))
1312 return true;
Mike Stump25cf7602009-09-09 15:08:12 +00001313
Anders Carlssonfdd33cc2009-06-13 00:33:33 +00001314 // Add the converted template type argument.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001315 Converted.Append(
Anders Carlssonfdd33cc2009-06-13 00:33:33 +00001316 TemplateArgument(Arg.getLocation(),
1317 Context.getCanonicalType(Arg.getAsType())));
1318 return false;
1319}
1320
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001321/// \brief Check that the given template argument list is well-formed
1322/// for specializing the given template.
1323bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
1324 SourceLocation TemplateLoc,
1325 SourceLocation LAngleLoc,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001326 const TemplateArgument *TemplateArgs,
1327 unsigned NumTemplateArgs,
Douglas Gregorad964b32009-02-17 01:05:43 +00001328 SourceLocation RAngleLoc,
Douglas Gregorecd63b82009-07-01 00:28:38 +00001329 bool PartialTemplateArgs,
Anders Carlssona35faf92009-06-05 03:43:12 +00001330 TemplateArgumentListBuilder &Converted) {
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001331 TemplateParameterList *Params = Template->getTemplateParameters();
1332 unsigned NumParams = Params->size();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001333 unsigned NumArgs = NumTemplateArgs;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001334 bool Invalid = false;
1335
Mike Stump25cf7602009-09-09 15:08:12 +00001336 bool HasParameterPack =
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001337 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump25cf7602009-09-09 15:08:12 +00001338
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001339 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregorecd63b82009-07-01 00:28:38 +00001340 (NumArgs < Params->getMinRequiredArguments() &&
1341 !PartialTemplateArgs)) {
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001342 // FIXME: point at either the first arg beyond what we can handle,
1343 // or the '>', depending on whether we have too many or too few
1344 // arguments.
1345 SourceRange Range;
1346 if (NumArgs > NumParams)
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001347 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001348 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
1349 << (NumArgs > NumParams)
1350 << (isa<ClassTemplateDecl>(Template)? 0 :
1351 isa<FunctionTemplateDecl>(Template)? 1 :
1352 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
1353 << Template << Range;
Douglas Gregorc347d8e2009-02-11 18:16:40 +00001354 Diag(Template->getLocation(), diag::note_template_decl_here)
1355 << Params->getSourceRange();
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001356 Invalid = true;
1357 }
Mike Stump25cf7602009-09-09 15:08:12 +00001358
1359 // C++ [temp.arg]p1:
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001360 // [...] The type and form of each template-argument specified in
1361 // a template-id shall match the type and form specified for the
1362 // corresponding parameter declared by the template in its
1363 // template-parameter-list.
1364 unsigned ArgIdx = 0;
1365 for (TemplateParameterList::iterator Param = Params->begin(),
1366 ParamEnd = Params->end();
1367 Param != ParamEnd; ++Param, ++ArgIdx) {
Douglas Gregorecd63b82009-07-01 00:28:38 +00001368 if (ArgIdx > NumArgs && PartialTemplateArgs)
1369 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001370
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001371 // Decode the template argument
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001372 TemplateArgument Arg;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001373 if (ArgIdx >= NumArgs) {
Douglas Gregorad964b32009-02-17 01:05:43 +00001374 // Retrieve the default template argument from the template
1375 // parameter.
1376 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001377 if (TTP->isParameterPack()) {
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001378 // We have an empty argument pack.
1379 Converted.BeginPack();
1380 Converted.EndPack();
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001381 break;
1382 }
Mike Stump25cf7602009-09-09 15:08:12 +00001383
Douglas Gregorad964b32009-02-17 01:05:43 +00001384 if (!TTP->hasDefaultArgument())
1385 break;
1386
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001387 QualType ArgType = TTP->getDefaultArgument();
Douglas Gregor74296542009-02-27 19:31:52 +00001388
1389 // If the argument type is dependent, instantiate it now based
1390 // on the previously-computed template arguments.
Douglas Gregor56d25a72009-03-10 20:44:00 +00001391 if (ArgType->isDependentType()) {
Mike Stump25cf7602009-09-09 15:08:12 +00001392 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001393 Template, Converted.getFlatArguments(),
Anders Carlssona35faf92009-06-05 03:43:12 +00001394 Converted.flatSize(),
Douglas Gregor56d25a72009-03-10 20:44:00 +00001395 SourceRange(TemplateLoc, RAngleLoc));
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001396
Anders Carlsson0233eb62009-06-05 04:47:51 +00001397 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001398 /*TakeArgs=*/false);
Mike Stump25cf7602009-09-09 15:08:12 +00001399 ArgType = SubstType(ArgType,
Douglas Gregor8dbd0382009-08-28 20:31:08 +00001400 MultiLevelTemplateArgumentList(TemplateArgs),
John McCall0ba26ee2009-08-25 22:02:44 +00001401 TTP->getDefaultArgumentLoc(),
1402 TTP->getDeclName());
Douglas Gregor56d25a72009-03-10 20:44:00 +00001403 }
Douglas Gregor74296542009-02-27 19:31:52 +00001404
1405 if (ArgType.isNull())
Douglas Gregorf57dcd02009-02-28 00:25:32 +00001406 return true;
Douglas Gregor74296542009-02-27 19:31:52 +00001407
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001408 Arg = TemplateArgument(TTP->getLocation(), ArgType);
Mike Stump25cf7602009-09-09 15:08:12 +00001409 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorad964b32009-02-17 01:05:43 +00001410 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1411 if (!NTTP->hasDefaultArgument())
1412 break;
1413
Mike Stump25cf7602009-09-09 15:08:12 +00001414 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001415 Template, Converted.getFlatArguments(),
Anders Carlsson0297caf2009-06-11 16:06:49 +00001416 Converted.flatSize(),
1417 SourceRange(TemplateLoc, RAngleLoc));
Mike Stump25cf7602009-09-09 15:08:12 +00001418
Anders Carlsson0297caf2009-06-11 16:06:49 +00001419 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001420 /*TakeArgs=*/false);
Anders Carlsson0297caf2009-06-11 16:06:49 +00001421
Mike Stump25cf7602009-09-09 15:08:12 +00001422 Sema::OwningExprResult E
1423 = SubstExpr(NTTP->getDefaultArgument(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +00001424 MultiLevelTemplateArgumentList(TemplateArgs));
Anders Carlsson0297caf2009-06-11 16:06:49 +00001425 if (E.isInvalid())
1426 return true;
Mike Stump25cf7602009-09-09 15:08:12 +00001427
Anders Carlsson0297caf2009-06-11 16:06:49 +00001428 Arg = TemplateArgument(E.takeAs<Expr>());
Douglas Gregorad964b32009-02-17 01:05:43 +00001429 } else {
Mike Stump25cf7602009-09-09 15:08:12 +00001430 TemplateTemplateParmDecl *TempParm
1431 = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregorad964b32009-02-17 01:05:43 +00001432
1433 if (!TempParm->hasDefaultArgument())
1434 break;
1435
John McCall0ba26ee2009-08-25 22:02:44 +00001436 // FIXME: Subst default argument
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001437 Arg = TemplateArgument(TempParm->getDefaultArgument());
Douglas Gregorad964b32009-02-17 01:05:43 +00001438 }
1439 } else {
1440 // Retrieve the template argument produced by the user.
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001441 Arg = TemplateArgs[ArgIdx];
Douglas Gregorad964b32009-02-17 01:05:43 +00001442 }
1443
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001444
1445 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001446 if (TTP->isParameterPack()) {
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001447 Converted.BeginPack();
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001448 // Check all the remaining arguments (if any).
1449 for (; ArgIdx < NumArgs; ++ArgIdx) {
1450 if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted))
1451 Invalid = true;
1452 }
Mike Stump25cf7602009-09-09 15:08:12 +00001453
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001454 Converted.EndPack();
Anders Carlsson4ffb5812009-06-13 02:08:00 +00001455 } else {
1456 if (CheckTemplateTypeArgument(TTP, Arg, Converted))
1457 Invalid = true;
1458 }
Mike Stump25cf7602009-09-09 15:08:12 +00001459 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001460 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1461 // Check non-type template parameters.
Douglas Gregored3a3982009-03-03 04:44:36 +00001462
John McCall0ba26ee2009-08-25 22:02:44 +00001463 // Do substitution on the type of the non-type template parameter
1464 // with the template arguments we've seen thus far.
Douglas Gregored3a3982009-03-03 04:44:36 +00001465 QualType NTTPType = NTTP->getType();
1466 if (NTTPType->isDependentType()) {
John McCall0ba26ee2009-08-25 22:02:44 +00001467 // Do substitution on the type of the non-type template parameter.
Mike Stump25cf7602009-09-09 15:08:12 +00001468 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001469 Template, Converted.getFlatArguments(),
Anders Carlssona35faf92009-06-05 03:43:12 +00001470 Converted.flatSize(),
Douglas Gregor56d25a72009-03-10 20:44:00 +00001471 SourceRange(TemplateLoc, RAngleLoc));
1472
Anders Carlsson0233eb62009-06-05 04:47:51 +00001473 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001474 /*TakeArgs=*/false);
Mike Stump25cf7602009-09-09 15:08:12 +00001475 NTTPType = SubstType(NTTPType,
Douglas Gregor01b7a1c2009-08-28 20:50:45 +00001476 MultiLevelTemplateArgumentList(TemplateArgs),
John McCall0ba26ee2009-08-25 22:02:44 +00001477 NTTP->getLocation(),
1478 NTTP->getDeclName());
Douglas Gregored3a3982009-03-03 04:44:36 +00001479 // If that worked, check the non-type template parameter type
1480 // for validity.
1481 if (!NTTPType.isNull())
Mike Stump25cf7602009-09-09 15:08:12 +00001482 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
Douglas Gregored3a3982009-03-03 04:44:36 +00001483 NTTP->getLocation());
Douglas Gregored3a3982009-03-03 04:44:36 +00001484 if (NTTPType.isNull()) {
1485 Invalid = true;
1486 break;
1487 }
1488 }
1489
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001490 switch (Arg.getKind()) {
Douglas Gregorbf23a8a2009-06-04 00:03:07 +00001491 case TemplateArgument::Null:
1492 assert(false && "Should never see a NULL template argument here");
1493 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001494
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001495 case TemplateArgument::Expression: {
1496 Expr *E = Arg.getAsExpr();
Douglas Gregor8f378a92009-06-11 18:10:32 +00001497 TemplateArgument Result;
1498 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001499 Invalid = true;
Douglas Gregor8f378a92009-06-11 18:10:32 +00001500 else
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001501 Converted.Append(Result);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001502 break;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001503 }
1504
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001505 case TemplateArgument::Declaration:
1506 case TemplateArgument::Integral:
1507 // We've already checked this template argument, so just copy
1508 // it to the list of converted arguments.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001509 Converted.Append(Arg);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001510 break;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001511
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001512 case TemplateArgument::Type:
1513 // We have a non-type template parameter but the template
1514 // argument is a type.
Mike Stump25cf7602009-09-09 15:08:12 +00001515
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001516 // C++ [temp.arg]p2:
1517 // In a template-argument, an ambiguity between a type-id and
1518 // an expression is resolved to a type-id, regardless of the
1519 // form of the corresponding template-parameter.
1520 //
1521 // We warn specifically about this case, since it can be rather
1522 // confusing for users.
1523 if (Arg.getAsType()->isFunctionType())
1524 Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig)
1525 << Arg.getAsType();
1526 else
1527 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr);
1528 Diag((*Param)->getLocation(), diag::note_template_param_here);
1529 Invalid = true;
Anders Carlsson584b5062009-06-15 17:04:53 +00001530 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001531
Anders Carlsson584b5062009-06-15 17:04:53 +00001532 case TemplateArgument::Pack:
1533 assert(0 && "FIXME: Implement!");
1534 break;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001535 }
Mike Stump25cf7602009-09-09 15:08:12 +00001536 } else {
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001537 // Check template template parameters.
Mike Stump25cf7602009-09-09 15:08:12 +00001538 TemplateTemplateParmDecl *TempParm
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001539 = cast<TemplateTemplateParmDecl>(*Param);
Mike Stump25cf7602009-09-09 15:08:12 +00001540
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001541 switch (Arg.getKind()) {
Douglas Gregorbf23a8a2009-06-04 00:03:07 +00001542 case TemplateArgument::Null:
1543 assert(false && "Should never see a NULL template argument here");
1544 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001545
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001546 case TemplateArgument::Expression: {
1547 Expr *ArgExpr = Arg.getAsExpr();
1548 if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
1549 isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
1550 if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
1551 Invalid = true;
Mike Stump25cf7602009-09-09 15:08:12 +00001552
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001553 // Add the converted template argument.
Mike Stump25cf7602009-09-09 15:08:12 +00001554 Decl *D
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00001555 = cast<DeclRefExpr>(ArgExpr)->getDecl()->getCanonicalDecl();
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001556 Converted.Append(TemplateArgument(Arg.getLocation(), D));
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001557 continue;
1558 }
1559 }
1560 // fall through
Mike Stump25cf7602009-09-09 15:08:12 +00001561
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001562 case TemplateArgument::Type: {
1563 // We have a template template parameter but the template
1564 // argument does not refer to a template.
1565 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
1566 Invalid = true;
1567 break;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001568 }
1569
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001570 case TemplateArgument::Declaration:
1571 // We've already checked this template argument, so just copy
1572 // it to the list of converted arguments.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00001573 Converted.Append(Arg);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001574 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001575
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001576 case TemplateArgument::Integral:
1577 assert(false && "Integral argument with template template parameter");
1578 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001579
Anders Carlsson584b5062009-06-15 17:04:53 +00001580 case TemplateArgument::Pack:
1581 assert(0 && "FIXME: Implement!");
1582 break;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001583 }
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001584 }
1585 }
1586
1587 return Invalid;
1588}
1589
1590/// \brief Check a template argument against its corresponding
1591/// template type parameter.
1592///
1593/// This routine implements the semantics of C++ [temp.arg.type]. It
1594/// returns true if an error occurred, and false otherwise.
Mike Stump25cf7602009-09-09 15:08:12 +00001595bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001596 QualType Arg, SourceLocation ArgLoc) {
1597 // C++ [temp.arg.type]p2:
1598 // A local type, a type with no linkage, an unnamed type or a type
1599 // compounded from any of these types shall not be used as a
1600 // template-argument for a template type-parameter.
1601 //
1602 // FIXME: Perform the recursive and no-linkage type checks.
1603 const TagType *Tag = 0;
1604 if (const EnumType *EnumT = Arg->getAsEnumType())
1605 Tag = EnumT;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001606 else if (const RecordType *RecordT = Arg->getAs<RecordType>())
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001607 Tag = RecordT;
1608 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
1609 return Diag(ArgLoc, diag::err_template_arg_local_type)
1610 << QualType(Tag, 0);
Mike Stump25cf7602009-09-09 15:08:12 +00001611 else if (Tag && !Tag->getDecl()->getDeclName() &&
Douglas Gregor04385782009-03-10 18:33:27 +00001612 !Tag->getDecl()->getTypedefForAnonDecl()) {
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001613 Diag(ArgLoc, diag::err_template_arg_unnamed_type);
1614 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
1615 return true;
1616 }
1617
1618 return false;
1619}
1620
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001621/// \brief Checks whether the given template argument is the address
1622/// of an object or function according to C++ [temp.arg.nontype]p1.
Douglas Gregorad964b32009-02-17 01:05:43 +00001623bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
1624 NamedDecl *&Entity) {
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001625 bool Invalid = false;
1626
1627 // See through any implicit casts we added to fix the type.
1628 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1629 Arg = Cast->getSubExpr();
1630
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001631 // C++0x allows nullptr, and there's no further checking to be done for that.
1632 if (Arg->getType()->isNullPtrType())
1633 return false;
1634
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001635 // C++ [temp.arg.nontype]p1:
Mike Stump25cf7602009-09-09 15:08:12 +00001636 //
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001637 // A template-argument for a non-type, non-template
1638 // template-parameter shall be one of: [...]
1639 //
1640 // -- the address of an object or function with external
1641 // linkage, including function templates and function
1642 // template-ids but excluding non-static class members,
1643 // expressed as & id-expression where the & is optional if
1644 // the name refers to a function or array, or if the
1645 // corresponding template-parameter is a reference; or
1646 DeclRefExpr *DRE = 0;
Mike Stump25cf7602009-09-09 15:08:12 +00001647
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001648 // Ignore (and complain about) any excess parentheses.
1649 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1650 if (!Invalid) {
Mike Stump25cf7602009-09-09 15:08:12 +00001651 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001652 diag::err_template_arg_extra_parens)
1653 << Arg->getSourceRange();
1654 Invalid = true;
1655 }
1656
1657 Arg = Parens->getSubExpr();
1658 }
1659
1660 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
1661 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1662 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
1663 } else
1664 DRE = dyn_cast<DeclRefExpr>(Arg);
1665
1666 if (!DRE || !isa<ValueDecl>(DRE->getDecl()))
Mike Stump25cf7602009-09-09 15:08:12 +00001667 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001668 diag::err_template_arg_not_object_or_func_form)
1669 << Arg->getSourceRange();
1670
1671 // Cannot refer to non-static data members
1672 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
1673 return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
1674 << Field << Arg->getSourceRange();
1675
1676 // Cannot refer to non-static member functions
1677 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
1678 if (!Method->isStatic())
Mike Stump25cf7602009-09-09 15:08:12 +00001679 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001680 diag::err_template_arg_method)
1681 << Method << Arg->getSourceRange();
Mike Stump25cf7602009-09-09 15:08:12 +00001682
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001683 // Functions must have external linkage.
1684 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1685 if (Func->getStorageClass() == FunctionDecl::Static) {
Mike Stump25cf7602009-09-09 15:08:12 +00001686 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001687 diag::err_template_arg_function_not_extern)
1688 << Func << Arg->getSourceRange();
1689 Diag(Func->getLocation(), diag::note_template_arg_internal_object)
1690 << true;
1691 return true;
1692 }
1693
1694 // Okay: we've named a function with external linkage.
Douglas Gregorad964b32009-02-17 01:05:43 +00001695 Entity = Func;
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001696 return Invalid;
1697 }
1698
1699 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
1700 if (!Var->hasGlobalStorage()) {
Mike Stump25cf7602009-09-09 15:08:12 +00001701 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001702 diag::err_template_arg_object_not_extern)
1703 << Var << Arg->getSourceRange();
1704 Diag(Var->getLocation(), diag::note_template_arg_internal_object)
1705 << true;
1706 return true;
1707 }
1708
1709 // Okay: we've named an object with external linkage
Douglas Gregorad964b32009-02-17 01:05:43 +00001710 Entity = Var;
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001711 return Invalid;
1712 }
Mike Stump25cf7602009-09-09 15:08:12 +00001713
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001714 // We found something else, but we don't know specifically what it is.
Mike Stump25cf7602009-09-09 15:08:12 +00001715 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001716 diag::err_template_arg_not_object_or_func)
1717 << Arg->getSourceRange();
Mike Stump25cf7602009-09-09 15:08:12 +00001718 Diag(DRE->getDecl()->getLocation(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001719 diag::note_template_arg_refers_here);
1720 return true;
1721}
1722
1723/// \brief Checks whether the given template argument is a pointer to
1724/// member constant according to C++ [temp.arg.nontype]p1.
Mike Stump25cf7602009-09-09 15:08:12 +00001725bool
Douglas Gregorad964b32009-02-17 01:05:43 +00001726Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) {
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001727 bool Invalid = false;
1728
1729 // See through any implicit casts we added to fix the type.
1730 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1731 Arg = Cast->getSubExpr();
1732
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001733 // C++0x allows nullptr, and there's no further checking to be done for that.
1734 if (Arg->getType()->isNullPtrType())
1735 return false;
1736
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001737 // C++ [temp.arg.nontype]p1:
Mike Stump25cf7602009-09-09 15:08:12 +00001738 //
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001739 // A template-argument for a non-type, non-template
1740 // template-parameter shall be one of: [...]
1741 //
1742 // -- a pointer to member expressed as described in 5.3.1.
1743 QualifiedDeclRefExpr *DRE = 0;
1744
1745 // Ignore (and complain about) any excess parentheses.
1746 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1747 if (!Invalid) {
Mike Stump25cf7602009-09-09 15:08:12 +00001748 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001749 diag::err_template_arg_extra_parens)
1750 << Arg->getSourceRange();
1751 Invalid = true;
1752 }
1753
1754 Arg = Parens->getSubExpr();
1755 }
1756
1757 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg))
1758 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1759 DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr());
1760
1761 if (!DRE)
1762 return Diag(Arg->getSourceRange().getBegin(),
1763 diag::err_template_arg_not_pointer_to_member_form)
1764 << Arg->getSourceRange();
1765
1766 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
1767 assert((isa<FieldDecl>(DRE->getDecl()) ||
1768 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
1769 "Only non-static member pointers can make it here");
1770
1771 // Okay: this is the address of a non-static member, and therefore
1772 // a member pointer constant.
Douglas Gregorad964b32009-02-17 01:05:43 +00001773 Member = DRE->getDecl();
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001774 return Invalid;
1775 }
1776
1777 // We found something else, but we don't know specifically what it is.
Mike Stump25cf7602009-09-09 15:08:12 +00001778 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001779 diag::err_template_arg_not_pointer_to_member_form)
1780 << Arg->getSourceRange();
Mike Stump25cf7602009-09-09 15:08:12 +00001781 Diag(DRE->getDecl()->getLocation(),
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001782 diag::note_template_arg_refers_here);
1783 return true;
1784}
1785
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001786/// \brief Check a template argument against its corresponding
1787/// non-type template parameter.
1788///
Douglas Gregored3a3982009-03-03 04:44:36 +00001789/// This routine implements the semantics of C++ [temp.arg.nontype].
1790/// It returns true if an error occurred, and false otherwise. \p
1791/// InstantiatedParamType is the type of the non-type template
1792/// parameter after it has been instantiated.
Douglas Gregorad964b32009-02-17 01:05:43 +00001793///
Douglas Gregor8f378a92009-06-11 18:10:32 +00001794/// If no error was detected, Converted receives the converted template argument.
Douglas Gregor35d81bb2009-02-09 23:23:08 +00001795bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump25cf7602009-09-09 15:08:12 +00001796 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor8f378a92009-06-11 18:10:32 +00001797 TemplateArgument &Converted) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001798 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
1799
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001800 // If either the parameter has a dependent type or the argument is
1801 // type-dependent, there's nothing we can check now.
Douglas Gregorad964b32009-02-17 01:05:43 +00001802 // FIXME: Add template argument to Converted!
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001803 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
1804 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor8f378a92009-06-11 18:10:32 +00001805 Converted = TemplateArgument(Arg);
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001806 return false;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001807 }
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001808
1809 // C++ [temp.arg.nontype]p5:
1810 // The following conversions are performed on each expression used
1811 // as a non-type template-argument. If a non-type
1812 // template-argument cannot be converted to the type of the
1813 // corresponding template-parameter then the program is
1814 // ill-formed.
1815 //
1816 // -- for a non-type template-parameter of integral or
1817 // enumeration type, integral promotions (4.5) and integral
1818 // conversions (4.7) are applied.
Douglas Gregored3a3982009-03-03 04:44:36 +00001819 QualType ParamType = InstantiatedParamType;
Douglas Gregor2eedd992009-02-11 00:19:33 +00001820 QualType ArgType = Arg->getType();
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001821 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001822 // C++ [temp.arg.nontype]p1:
1823 // A template-argument for a non-type, non-template
1824 // template-parameter shall be one of:
1825 //
1826 // -- an integral constant-expression of integral or enumeration
1827 // type; or
1828 // -- the name of a non-type template-parameter; or
1829 SourceLocation NonConstantLoc;
Douglas Gregorad964b32009-02-17 01:05:43 +00001830 llvm::APSInt Value;
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001831 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
Mike Stump25cf7602009-09-09 15:08:12 +00001832 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001833 diag::err_template_arg_not_integral_or_enumeral)
1834 << ArgType << Arg->getSourceRange();
1835 Diag(Param->getLocation(), diag::note_template_param_here);
1836 return true;
1837 } else if (!Arg->isValueDependent() &&
Douglas Gregorad964b32009-02-17 01:05:43 +00001838 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001839 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
1840 << ArgType << Arg->getSourceRange();
1841 return true;
1842 }
1843
1844 // FIXME: We need some way to more easily get the unqualified form
1845 // of the types without going all the way to the
1846 // canonical type.
1847 if (Context.getCanonicalType(ParamType).getCVRQualifiers())
1848 ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
1849 if (Context.getCanonicalType(ArgType).getCVRQualifiers())
1850 ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
1851
1852 // Try to convert the argument to the parameter's type.
1853 if (ParamType == ArgType) {
1854 // Okay: no conversion necessary
1855 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
1856 !ParamType->isEnumeralType()) {
1857 // This is an integral promotion or conversion.
1858 ImpCastExprToType(Arg, ParamType);
1859 } else {
1860 // We can't perform this conversion.
Mike Stump25cf7602009-09-09 15:08:12 +00001861 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001862 diag::err_template_arg_not_convertible)
Douglas Gregored3a3982009-03-03 04:44:36 +00001863 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001864 Diag(Param->getLocation(), diag::note_template_param_here);
1865 return true;
1866 }
1867
Douglas Gregorafc86942009-03-14 00:20:21 +00001868 QualType IntegerType = Context.getCanonicalType(ParamType);
1869 if (const EnumType *Enum = IntegerType->getAsEnumType())
Douglas Gregor8f378a92009-06-11 18:10:32 +00001870 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorafc86942009-03-14 00:20:21 +00001871
1872 if (!Arg->isValueDependent()) {
1873 // Check that an unsigned parameter does not receive a negative
1874 // value.
1875 if (IntegerType->isUnsignedIntegerType()
1876 && (Value.isSigned() && Value.isNegative())) {
1877 Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
1878 << Value.toString(10) << Param->getType()
1879 << Arg->getSourceRange();
1880 Diag(Param->getLocation(), diag::note_template_param_here);
1881 return true;
1882 }
1883
1884 // Check that we don't overflow the template parameter type.
1885 unsigned AllowedBits = Context.getTypeSize(IntegerType);
1886 if (Value.getActiveBits() > AllowedBits) {
Mike Stump25cf7602009-09-09 15:08:12 +00001887 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorafc86942009-03-14 00:20:21 +00001888 diag::err_template_arg_too_large)
1889 << Value.toString(10) << Param->getType()
1890 << Arg->getSourceRange();
1891 Diag(Param->getLocation(), diag::note_template_param_here);
1892 return true;
1893 }
1894
1895 if (Value.getBitWidth() != AllowedBits)
1896 Value.extOrTrunc(AllowedBits);
1897 Value.setIsSigned(IntegerType->isSignedIntegerType());
1898 }
Douglas Gregorad964b32009-02-17 01:05:43 +00001899
Douglas Gregor8f378a92009-06-11 18:10:32 +00001900 // Add the value of this argument to the list of converted
1901 // arguments. We use the bitwidth and signedness of the template
1902 // parameter.
1903 if (Arg->isValueDependent()) {
1904 // The argument is value-dependent. Create a new
1905 // TemplateArgument with the converted expression.
1906 Converted = TemplateArgument(Arg);
1907 return false;
Douglas Gregorad964b32009-02-17 01:05:43 +00001908 }
1909
Douglas Gregor8f378a92009-06-11 18:10:32 +00001910 Converted = TemplateArgument(StartLoc, Value,
Mike Stump25cf7602009-09-09 15:08:12 +00001911 ParamType->isEnumeralType() ? ParamType
Douglas Gregor8f378a92009-06-11 18:10:32 +00001912 : IntegerType);
Douglas Gregor79e5c9c2009-02-10 23:36:10 +00001913 return false;
1914 }
Douglas Gregor2eedd992009-02-11 00:19:33 +00001915
Douglas Gregor3f411962009-02-11 01:18:59 +00001916 // Handle pointer-to-function, reference-to-function, and
1917 // pointer-to-member-function all in (roughly) the same way.
1918 if (// -- For a non-type template-parameter of type pointer to
1919 // function, only the function-to-pointer conversion (4.3) is
1920 // applied. If the template-argument represents a set of
1921 // overloaded functions (or a pointer to such), the matching
1922 // function is selected from the set (13.4).
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001923 // In C++0x, any std::nullptr_t value can be converted.
Douglas Gregor3f411962009-02-11 01:18:59 +00001924 (ParamType->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001925 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregor3f411962009-02-11 01:18:59 +00001926 // -- For a non-type template-parameter of type reference to
1927 // function, no conversions apply. If the template-argument
1928 // represents a set of overloaded functions, the matching
1929 // function is selected from the set (13.4).
1930 (ParamType->isReferenceType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001931 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregor3f411962009-02-11 01:18:59 +00001932 // -- For a non-type template-parameter of type pointer to
1933 // member function, no conversions apply. If the
1934 // template-argument represents a set of overloaded member
1935 // functions, the matching member function is selected from
1936 // the set (13.4).
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001937 // Again, C++0x allows a std::nullptr_t value.
Douglas Gregor3f411962009-02-11 01:18:59 +00001938 (ParamType->isMemberPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001939 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregor3f411962009-02-11 01:18:59 +00001940 ->isFunctionType())) {
Mike Stump25cf7602009-09-09 15:08:12 +00001941 if (Context.hasSameUnqualifiedType(ArgType,
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001942 ParamType.getNonReferenceType())) {
Douglas Gregor2eedd992009-02-11 00:19:33 +00001943 // We don't have to do anything: the types already match.
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001944 } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
1945 ParamType->isMemberPointerType())) {
1946 ArgType = ParamType;
1947 ImpCastExprToType(Arg, ParamType);
Douglas Gregor3f411962009-02-11 01:18:59 +00001948 } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregor2eedd992009-02-11 00:19:33 +00001949 ArgType = Context.getPointerType(ArgType);
1950 ImpCastExprToType(Arg, ArgType);
Mike Stump25cf7602009-09-09 15:08:12 +00001951 } else if (FunctionDecl *Fn
Douglas Gregor2eedd992009-02-11 00:19:33 +00001952 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
Douglas Gregoraa57e862009-02-18 21:56:37 +00001953 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
1954 return true;
1955
Douglas Gregor2eedd992009-02-11 00:19:33 +00001956 FixOverloadedFunctionReference(Arg, Fn);
1957 ArgType = Arg->getType();
Douglas Gregor3f411962009-02-11 01:18:59 +00001958 if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregor2eedd992009-02-11 00:19:33 +00001959 ArgType = Context.getPointerType(Arg->getType());
1960 ImpCastExprToType(Arg, ArgType);
1961 }
1962 }
1963
Mike Stump25cf7602009-09-09 15:08:12 +00001964 if (!Context.hasSameUnqualifiedType(ArgType,
Douglas Gregor6b3a0ba2009-02-11 19:52:55 +00001965 ParamType.getNonReferenceType())) {
Douglas Gregor2eedd992009-02-11 00:19:33 +00001966 // We can't perform this conversion.
Mike Stump25cf7602009-09-09 15:08:12 +00001967 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor2eedd992009-02-11 00:19:33 +00001968 diag::err_template_arg_not_convertible)
Douglas Gregored3a3982009-03-03 04:44:36 +00001969 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor2eedd992009-02-11 00:19:33 +00001970 Diag(Param->getLocation(), diag::note_template_param_here);
1971 return true;
1972 }
Mike Stump25cf7602009-09-09 15:08:12 +00001973
Douglas Gregorad964b32009-02-17 01:05:43 +00001974 if (ParamType->isMemberPointerType()) {
1975 NamedDecl *Member = 0;
1976 if (CheckTemplateArgumentPointerToMember(Arg, Member))
1977 return true;
1978
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00001979 if (Member)
1980 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor8f378a92009-06-11 18:10:32 +00001981 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregorad964b32009-02-17 01:05:43 +00001982 return false;
1983 }
Mike Stump25cf7602009-09-09 15:08:12 +00001984
Douglas Gregorad964b32009-02-17 01:05:43 +00001985 NamedDecl *Entity = 0;
1986 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
1987 return true;
1988
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00001989 if (Entity)
1990 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor8f378a92009-06-11 18:10:32 +00001991 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregorad964b32009-02-17 01:05:43 +00001992 return false;
Douglas Gregor2eedd992009-02-11 00:19:33 +00001993 }
1994
Chris Lattner320dff22009-02-20 21:37:53 +00001995 if (ParamType->isPointerType()) {
Douglas Gregor3f411962009-02-11 01:18:59 +00001996 // -- for a non-type template-parameter of type pointer to
1997 // object, qualification conversions (4.4) and the
1998 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001999 // C++0x also allows a value of std::nullptr_t.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00002000 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
Douglas Gregor3f411962009-02-11 01:18:59 +00002001 "Only object pointers allowed here");
Douglas Gregord8c8c092009-02-11 00:44:29 +00002002
Sebastian Redl5d0ead72009-05-10 18:38:11 +00002003 if (ArgType->isNullPtrType()) {
2004 ArgType = ParamType;
2005 ImpCastExprToType(Arg, ParamType);
2006 } else if (ArgType->isArrayType()) {
Douglas Gregor3f411962009-02-11 01:18:59 +00002007 ArgType = Context.getArrayDecayedType(ArgType);
2008 ImpCastExprToType(Arg, ArgType);
Douglas Gregord8c8c092009-02-11 00:44:29 +00002009 }
Sebastian Redl5d0ead72009-05-10 18:38:11 +00002010
Douglas Gregor3f411962009-02-11 01:18:59 +00002011 if (IsQualificationConversion(ArgType, ParamType)) {
2012 ArgType = ParamType;
2013 ImpCastExprToType(Arg, ParamType);
2014 }
Mike Stump25cf7602009-09-09 15:08:12 +00002015
Douglas Gregor0ea4e302009-02-11 18:22:40 +00002016 if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
Douglas Gregor3f411962009-02-11 01:18:59 +00002017 // We can't perform this conversion.
Mike Stump25cf7602009-09-09 15:08:12 +00002018 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor3f411962009-02-11 01:18:59 +00002019 diag::err_template_arg_not_convertible)
Douglas Gregored3a3982009-03-03 04:44:36 +00002020 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor3f411962009-02-11 01:18:59 +00002021 Diag(Param->getLocation(), diag::note_template_param_here);
2022 return true;
2023 }
Mike Stump25cf7602009-09-09 15:08:12 +00002024
Douglas Gregorad964b32009-02-17 01:05:43 +00002025 NamedDecl *Entity = 0;
2026 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2027 return true;
2028
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00002029 if (Entity)
2030 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor8f378a92009-06-11 18:10:32 +00002031 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregorad964b32009-02-17 01:05:43 +00002032 return false;
Douglas Gregord8c8c092009-02-11 00:44:29 +00002033 }
Mike Stump25cf7602009-09-09 15:08:12 +00002034
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00002035 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregor3f411962009-02-11 01:18:59 +00002036 // -- For a non-type template-parameter of type reference to
2037 // object, no conversions apply. The type referred to by the
2038 // reference may be more cv-qualified than the (otherwise
2039 // identical) type of the template-argument. The
2040 // template-parameter is bound directly to the
2041 // template-argument, which must be an lvalue.
Douglas Gregor26ea1222009-03-24 20:32:41 +00002042 assert(ParamRefType->getPointeeType()->isObjectType() &&
Douglas Gregor3f411962009-02-11 01:18:59 +00002043 "Only object references allowed here");
Douglas Gregord8c8c092009-02-11 00:44:29 +00002044
Douglas Gregor0ea4e302009-02-11 18:22:40 +00002045 if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002046 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor3f411962009-02-11 01:18:59 +00002047 diag::err_template_arg_no_ref_bind)
Douglas Gregored3a3982009-03-03 04:44:36 +00002048 << InstantiatedParamType << Arg->getType()
Douglas Gregor3f411962009-02-11 01:18:59 +00002049 << Arg->getSourceRange();
2050 Diag(Param->getLocation(), diag::note_template_param_here);
2051 return true;
2052 }
2053
Mike Stump25cf7602009-09-09 15:08:12 +00002054 unsigned ParamQuals
Douglas Gregor3f411962009-02-11 01:18:59 +00002055 = Context.getCanonicalType(ParamType).getCVRQualifiers();
2056 unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
Mike Stump25cf7602009-09-09 15:08:12 +00002057
Douglas Gregor3f411962009-02-11 01:18:59 +00002058 if ((ParamQuals | ArgQuals) != ParamQuals) {
2059 Diag(Arg->getSourceRange().getBegin(),
2060 diag::err_template_arg_ref_bind_ignores_quals)
Douglas Gregored3a3982009-03-03 04:44:36 +00002061 << InstantiatedParamType << Arg->getType()
Douglas Gregor3f411962009-02-11 01:18:59 +00002062 << Arg->getSourceRange();
2063 Diag(Param->getLocation(), diag::note_template_param_here);
2064 return true;
2065 }
Mike Stump25cf7602009-09-09 15:08:12 +00002066
Douglas Gregorad964b32009-02-17 01:05:43 +00002067 NamedDecl *Entity = 0;
2068 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2069 return true;
2070
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00002071 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor8f378a92009-06-11 18:10:32 +00002072 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregorad964b32009-02-17 01:05:43 +00002073 return false;
Douglas Gregor3f411962009-02-11 01:18:59 +00002074 }
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002075
2076 // -- For a non-type template-parameter of type pointer to data
2077 // member, qualification conversions (4.4) are applied.
Sebastian Redl5d0ead72009-05-10 18:38:11 +00002078 // C++0x allows std::nullptr_t values.
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002079 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2080
Douglas Gregor0ea4e302009-02-11 18:22:40 +00002081 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002082 // Types match exactly: nothing more to do here.
Sebastian Redl5d0ead72009-05-10 18:38:11 +00002083 } else if (ArgType->isNullPtrType()) {
2084 ImpCastExprToType(Arg, ParamType);
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002085 } else if (IsQualificationConversion(ArgType, ParamType)) {
2086 ImpCastExprToType(Arg, ParamType);
2087 } else {
2088 // We can't perform this conversion.
Mike Stump25cf7602009-09-09 15:08:12 +00002089 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002090 diag::err_template_arg_not_convertible)
Douglas Gregored3a3982009-03-03 04:44:36 +00002091 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002092 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump25cf7602009-09-09 15:08:12 +00002093 return true;
Douglas Gregor3628e1b2009-02-11 16:16:59 +00002094 }
2095
Douglas Gregorad964b32009-02-17 01:05:43 +00002096 NamedDecl *Member = 0;
2097 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2098 return true;
Mike Stump25cf7602009-09-09 15:08:12 +00002099
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00002100 if (Member)
2101 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor8f378a92009-06-11 18:10:32 +00002102 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregorad964b32009-02-17 01:05:43 +00002103 return false;
Douglas Gregor35d81bb2009-02-09 23:23:08 +00002104}
2105
2106/// \brief Check a template argument against its corresponding
2107/// template template parameter.
2108///
2109/// This routine implements the semantics of C++ [temp.arg.template].
2110/// It returns true if an error occurred, and false otherwise.
2111bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
2112 DeclRefExpr *Arg) {
Douglas Gregore8e367f2009-02-10 00:24:35 +00002113 assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
2114 TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
2115
2116 // C++ [temp.arg.template]p1:
2117 // A template-argument for a template template-parameter shall be
2118 // the name of a class template, expressed as id-expression. Only
2119 // primary class templates are considered when matching the
2120 // template template argument with the corresponding parameter;
2121 // partial specializations are not considered even if their
2122 // parameter lists match that of the template template parameter.
Douglas Gregorfbcc9e92009-06-12 19:43:02 +00002123 //
2124 // Note that we also allow template template parameters here, which
2125 // will happen when we are dealing with, e.g., class template
2126 // partial specializations.
Mike Stump25cf7602009-09-09 15:08:12 +00002127 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorfbcc9e92009-06-12 19:43:02 +00002128 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump25cf7602009-09-09 15:08:12 +00002129 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregore8e367f2009-02-10 00:24:35 +00002130 "Only function templates are possible here");
Douglas Gregorb60eb752009-06-25 22:08:12 +00002131 Diag(Arg->getLocStart(), diag::err_template_arg_not_class_template);
2132 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregore8e367f2009-02-10 00:24:35 +00002133 << Template;
2134 }
2135
2136 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2137 Param->getTemplateParameters(),
2138 true, true,
2139 Arg->getSourceRange().getBegin());
Douglas Gregor35d81bb2009-02-09 23:23:08 +00002140}
2141
Douglas Gregord406b032009-02-06 22:42:48 +00002142/// \brief Determine whether the given template parameter lists are
2143/// equivalent.
2144///
Mike Stump25cf7602009-09-09 15:08:12 +00002145/// \param New The new template parameter list, typically written in the
Douglas Gregord406b032009-02-06 22:42:48 +00002146/// source code as part of a new template declaration.
2147///
2148/// \param Old The old template parameter list, typically found via
2149/// name lookup of the template declared with this template parameter
2150/// list.
2151///
2152/// \param Complain If true, this routine will produce a diagnostic if
2153/// the template parameter lists are not equivalent.
2154///
Douglas Gregore8e367f2009-02-10 00:24:35 +00002155/// \param IsTemplateTemplateParm If true, this routine is being
2156/// called to compare the template parameter lists of a template
2157/// template parameter.
2158///
2159/// \param TemplateArgLoc If this source location is valid, then we
2160/// are actually checking the template parameter list of a template
2161/// argument (New) against the template parameter list of its
2162/// corresponding template template parameter (Old). We produce
2163/// slightly different diagnostics in this scenario.
2164///
Douglas Gregord406b032009-02-06 22:42:48 +00002165/// \returns True if the template parameter lists are equal, false
2166/// otherwise.
Mike Stump25cf7602009-09-09 15:08:12 +00002167bool
Douglas Gregord406b032009-02-06 22:42:48 +00002168Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2169 TemplateParameterList *Old,
2170 bool Complain,
Douglas Gregore8e367f2009-02-10 00:24:35 +00002171 bool IsTemplateTemplateParm,
2172 SourceLocation TemplateArgLoc) {
Douglas Gregord406b032009-02-06 22:42:48 +00002173 if (Old->size() != New->size()) {
2174 if (Complain) {
Douglas Gregore8e367f2009-02-10 00:24:35 +00002175 unsigned NextDiag = diag::err_template_param_list_different_arity;
2176 if (TemplateArgLoc.isValid()) {
2177 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2178 NextDiag = diag::note_template_param_list_different_arity;
Mike Stump25cf7602009-09-09 15:08:12 +00002179 }
Douglas Gregore8e367f2009-02-10 00:24:35 +00002180 Diag(New->getTemplateLoc(), NextDiag)
2181 << (New->size() > Old->size())
2182 << IsTemplateTemplateParm
2183 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregord406b032009-02-06 22:42:48 +00002184 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
2185 << IsTemplateTemplateParm
2186 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2187 }
2188
2189 return false;
2190 }
2191
2192 for (TemplateParameterList::iterator OldParm = Old->begin(),
2193 OldParmEnd = Old->end(), NewParm = New->begin();
2194 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
2195 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregorbf6bc302009-06-24 16:50:40 +00002196 if (Complain) {
2197 unsigned NextDiag = diag::err_template_param_different_kind;
2198 if (TemplateArgLoc.isValid()) {
2199 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2200 NextDiag = diag::note_template_param_different_kind;
2201 }
2202 Diag((*NewParm)->getLocation(), NextDiag)
2203 << IsTemplateTemplateParm;
2204 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
2205 << IsTemplateTemplateParm;
Douglas Gregore8e367f2009-02-10 00:24:35 +00002206 }
Douglas Gregord406b032009-02-06 22:42:48 +00002207 return false;
2208 }
2209
2210 if (isa<TemplateTypeParmDecl>(*OldParm)) {
2211 // Okay; all template type parameters are equivalent (since we
Douglas Gregore8e367f2009-02-10 00:24:35 +00002212 // know we're at the same index).
2213#if 0
Mike Stumpe127ae32009-05-16 07:39:55 +00002214 // FIXME: Enable this code in debug mode *after* we properly go through
2215 // and "instantiate" the template parameter lists of template template
2216 // parameters. It's only after this instantiation that (1) any dependent
2217 // types within the template parameter list of the template template
2218 // parameter can be checked, and (2) the template type parameter depths
Douglas Gregore8e367f2009-02-10 00:24:35 +00002219 // will match up.
Mike Stump25cf7602009-09-09 15:08:12 +00002220 QualType OldParmType
Douglas Gregord406b032009-02-06 22:42:48 +00002221 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
Mike Stump25cf7602009-09-09 15:08:12 +00002222 QualType NewParmType
Douglas Gregord406b032009-02-06 22:42:48 +00002223 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
Mike Stump25cf7602009-09-09 15:08:12 +00002224 assert(Context.getCanonicalType(OldParmType) ==
2225 Context.getCanonicalType(NewParmType) &&
Douglas Gregord406b032009-02-06 22:42:48 +00002226 "type parameter mismatch?");
2227#endif
Mike Stump25cf7602009-09-09 15:08:12 +00002228 } else if (NonTypeTemplateParmDecl *OldNTTP
Douglas Gregord406b032009-02-06 22:42:48 +00002229 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2230 // The types of non-type template parameters must agree.
2231 NonTypeTemplateParmDecl *NewNTTP
2232 = cast<NonTypeTemplateParmDecl>(*NewParm);
2233 if (Context.getCanonicalType(OldNTTP->getType()) !=
2234 Context.getCanonicalType(NewNTTP->getType())) {
2235 if (Complain) {
Douglas Gregore8e367f2009-02-10 00:24:35 +00002236 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
2237 if (TemplateArgLoc.isValid()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002238 Diag(TemplateArgLoc,
Douglas Gregore8e367f2009-02-10 00:24:35 +00002239 diag::err_template_arg_template_params_mismatch);
2240 NextDiag = diag::note_template_nontype_parm_different_type;
2241 }
2242 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregord406b032009-02-06 22:42:48 +00002243 << NewNTTP->getType()
2244 << IsTemplateTemplateParm;
Mike Stump25cf7602009-09-09 15:08:12 +00002245 Diag(OldNTTP->getLocation(),
Douglas Gregord406b032009-02-06 22:42:48 +00002246 diag::note_template_nontype_parm_prev_declaration)
2247 << OldNTTP->getType();
2248 }
2249 return false;
2250 }
2251 } else {
2252 // The template parameter lists of template template
2253 // parameters must agree.
2254 // FIXME: Could we perform a faster "type" comparison here?
Mike Stump25cf7602009-09-09 15:08:12 +00002255 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
Douglas Gregord406b032009-02-06 22:42:48 +00002256 "Only template template parameters handled here");
Mike Stump25cf7602009-09-09 15:08:12 +00002257 TemplateTemplateParmDecl *OldTTP
Douglas Gregord406b032009-02-06 22:42:48 +00002258 = cast<TemplateTemplateParmDecl>(*OldParm);
2259 TemplateTemplateParmDecl *NewTTP
2260 = cast<TemplateTemplateParmDecl>(*NewParm);
2261 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2262 OldTTP->getTemplateParameters(),
2263 Complain,
Douglas Gregore8e367f2009-02-10 00:24:35 +00002264 /*IsTemplateTemplateParm=*/true,
2265 TemplateArgLoc))
Douglas Gregord406b032009-02-06 22:42:48 +00002266 return false;
2267 }
2268 }
2269
2270 return true;
2271}
2272
2273/// \brief Check whether a template can be declared within this scope.
2274///
2275/// If the template declaration is valid in this scope, returns
2276/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump25cf7602009-09-09 15:08:12 +00002277bool
Douglas Gregore305ae32009-08-25 17:23:04 +00002278Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregord406b032009-02-06 22:42:48 +00002279 // Find the nearest enclosing declaration scope.
2280 while ((S->getFlags() & Scope::DeclScope) == 0 ||
2281 (S->getFlags() & Scope::TemplateParamScope) != 0)
2282 S = S->getParent();
Mike Stump25cf7602009-09-09 15:08:12 +00002283
Douglas Gregord406b032009-02-06 22:42:48 +00002284 // C++ [temp]p2:
2285 // A template-declaration can appear only as a namespace scope or
2286 // class scope declaration.
2287 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedmandda81522009-07-31 01:43:05 +00002288 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2289 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump25cf7602009-09-09 15:08:12 +00002290 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregore305ae32009-08-25 17:23:04 +00002291 << TemplateParams->getSourceRange();
Mike Stump25cf7602009-09-09 15:08:12 +00002292
Eli Friedmandda81522009-07-31 01:43:05 +00002293 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregord406b032009-02-06 22:42:48 +00002294 Ctx = Ctx->getParent();
Douglas Gregord406b032009-02-06 22:42:48 +00002295
2296 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2297 return false;
2298
Mike Stump25cf7602009-09-09 15:08:12 +00002299 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregore305ae32009-08-25 17:23:04 +00002300 diag::err_template_outside_namespace_or_class_scope)
2301 << TemplateParams->getSourceRange();
Douglas Gregord406b032009-02-06 22:42:48 +00002302}
Douglas Gregora08b6c72009-02-17 23:15:12 +00002303
Douglas Gregor90177912009-05-13 18:28:20 +00002304/// \brief Check whether a class template specialization or explicit
2305/// instantiation in the current context is well-formed.
Douglas Gregor0d93f692009-02-25 22:02:03 +00002306///
Douglas Gregor90177912009-05-13 18:28:20 +00002307/// This routine determines whether a class template specialization or
Mike Stump25cf7602009-09-09 15:08:12 +00002308/// explicit instantiation can be declared in the current context
2309/// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2) and emits
2310/// appropriate diagnostics if there was an error. It returns true if
Douglas Gregor90177912009-05-13 18:28:20 +00002311// there was an error that we cannot recover from, and false otherwise.
Mike Stump25cf7602009-09-09 15:08:12 +00002312bool
Douglas Gregor0d93f692009-02-25 22:02:03 +00002313Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate,
2314 ClassTemplateSpecializationDecl *PrevDecl,
2315 SourceLocation TemplateNameLoc,
Douglas Gregor90177912009-05-13 18:28:20 +00002316 SourceRange ScopeSpecifierRange,
Douglas Gregor4faa4262009-06-12 22:21:45 +00002317 bool PartialSpecialization,
Douglas Gregor90177912009-05-13 18:28:20 +00002318 bool ExplicitInstantiation) {
Douglas Gregor0d93f692009-02-25 22:02:03 +00002319 // C++ [temp.expl.spec]p2:
2320 // An explicit specialization shall be declared in the namespace
2321 // of which the template is a member, or, for member templates, in
2322 // the namespace of which the enclosing class or enclosing class
2323 // template is a member. An explicit specialization of a member
2324 // function, member class or static data member of a class
2325 // template shall be declared in the namespace of which the class
2326 // template is a member. Such a declaration may also be a
2327 // definition. If the declaration is not a definition, the
2328 // specialization may be defined later in the name- space in which
2329 // the explicit specialization was declared, or in a namespace
2330 // that encloses the one in which the explicit specialization was
2331 // declared.
2332 if (CurContext->getLookupContext()->isFunctionOrMethod()) {
Douglas Gregor4faa4262009-06-12 22:21:45 +00002333 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregor0d93f692009-02-25 22:02:03 +00002334 Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope)
Douglas Gregor4faa4262009-06-12 22:21:45 +00002335 << Kind << ClassTemplate;
Douglas Gregor0d93f692009-02-25 22:02:03 +00002336 return true;
2337 }
2338
2339 DeclContext *DC = CurContext->getEnclosingNamespaceContext();
Mike Stump25cf7602009-09-09 15:08:12 +00002340 DeclContext *TemplateContext
Douglas Gregor0d93f692009-02-25 22:02:03 +00002341 = ClassTemplate->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor90177912009-05-13 18:28:20 +00002342 if ((!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) &&
2343 !ExplicitInstantiation) {
Douglas Gregor0d93f692009-02-25 22:02:03 +00002344 // There is no prior declaration of this entity, so this
2345 // specialization must be in the same context as the template
2346 // itself.
2347 if (DC != TemplateContext) {
2348 if (isa<TranslationUnitDecl>(TemplateContext))
2349 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregor4faa4262009-06-12 22:21:45 +00002350 << PartialSpecialization
Douglas Gregor0d93f692009-02-25 22:02:03 +00002351 << ClassTemplate << ScopeSpecifierRange;
2352 else if (isa<NamespaceDecl>(TemplateContext))
2353 Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope)
Mike Stump25cf7602009-09-09 15:08:12 +00002354 << PartialSpecialization << ClassTemplate
Douglas Gregor4faa4262009-06-12 22:21:45 +00002355 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Douglas Gregor0d93f692009-02-25 22:02:03 +00002356
2357 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
2358 }
2359
2360 return false;
2361 }
2362
2363 // We have a previous declaration of this entity. Make sure that
2364 // this redeclaration (or definition) occurs in an enclosing namespace.
2365 if (!CurContext->Encloses(TemplateContext)) {
Mike Stumpe127ae32009-05-16 07:39:55 +00002366 // FIXME: In C++98, we would like to turn these errors into warnings,
2367 // dependent on a -Wc++0x flag.
Douglas Gregor90177912009-05-13 18:28:20 +00002368 bool SuppressedDiag = false;
Douglas Gregor4faa4262009-06-12 22:21:45 +00002369 int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
Douglas Gregor90177912009-05-13 18:28:20 +00002370 if (isa<TranslationUnitDecl>(TemplateContext)) {
2371 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2372 Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope)
Douglas Gregor4faa4262009-06-12 22:21:45 +00002373 << Kind << ClassTemplate << ScopeSpecifierRange;
Douglas Gregor90177912009-05-13 18:28:20 +00002374 else
2375 SuppressedDiag = true;
2376 } else if (isa<NamespaceDecl>(TemplateContext)) {
2377 if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
2378 Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope)
Douglas Gregor4faa4262009-06-12 22:21:45 +00002379 << Kind << ClassTemplate
Douglas Gregor90177912009-05-13 18:28:20 +00002380 << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
Mike Stump25cf7602009-09-09 15:08:12 +00002381 else
Douglas Gregor90177912009-05-13 18:28:20 +00002382 SuppressedDiag = true;
2383 }
Mike Stump25cf7602009-09-09 15:08:12 +00002384
Douglas Gregor90177912009-05-13 18:28:20 +00002385 if (!SuppressedDiag)
2386 Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
Douglas Gregor0d93f692009-02-25 22:02:03 +00002387 }
2388
2389 return false;
2390}
2391
Douglas Gregor76e79952009-06-12 21:21:02 +00002392/// \brief Check the non-type template arguments of a class template
2393/// partial specialization according to C++ [temp.class.spec]p9.
2394///
Douglas Gregor42476442009-06-12 22:08:06 +00002395/// \param TemplateParams the template parameters of the primary class
2396/// template.
2397///
2398/// \param TemplateArg the template arguments of the class template
2399/// partial specialization.
2400///
2401/// \param MirrorsPrimaryTemplate will be set true if the class
2402/// template partial specialization arguments are identical to the
2403/// implicit template arguments of the primary template. This is not
2404/// necessarily an error (C++0x), and it is left to the caller to diagnose
2405/// this condition when it is an error.
2406///
Douglas Gregor76e79952009-06-12 21:21:02 +00002407/// \returns true if there was an error, false otherwise.
2408bool Sema::CheckClassTemplatePartialSpecializationArgs(
2409 TemplateParameterList *TemplateParams,
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002410 const TemplateArgumentListBuilder &TemplateArgs,
Douglas Gregor42476442009-06-12 22:08:06 +00002411 bool &MirrorsPrimaryTemplate) {
Douglas Gregor76e79952009-06-12 21:21:02 +00002412 // FIXME: the interface to this function will have to change to
2413 // accommodate variadic templates.
Douglas Gregor42476442009-06-12 22:08:06 +00002414 MirrorsPrimaryTemplate = true;
Mike Stump25cf7602009-09-09 15:08:12 +00002415
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002416 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
Mike Stump25cf7602009-09-09 15:08:12 +00002417
Douglas Gregor76e79952009-06-12 21:21:02 +00002418 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
Douglas Gregor42476442009-06-12 22:08:06 +00002419 // Determine whether the template argument list of the partial
2420 // specialization is identical to the implicit argument list of
2421 // the primary template. The caller may need to diagnostic this as
2422 // an error per C++ [temp.class.spec]p9b3.
2423 if (MirrorsPrimaryTemplate) {
Mike Stump25cf7602009-09-09 15:08:12 +00002424 if (TemplateTypeParmDecl *TTP
Douglas Gregor42476442009-06-12 22:08:06 +00002425 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
2426 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002427 Context.getCanonicalType(ArgList[I].getAsType()))
Douglas Gregor42476442009-06-12 22:08:06 +00002428 MirrorsPrimaryTemplate = false;
2429 } else if (TemplateTemplateParmDecl *TTP
2430 = dyn_cast<TemplateTemplateParmDecl>(
2431 TemplateParams->getParam(I))) {
2432 // FIXME: We should settle on either Declaration storage or
2433 // Expression storage for template template parameters.
Mike Stump25cf7602009-09-09 15:08:12 +00002434 TemplateTemplateParmDecl *ArgDecl
Douglas Gregor42476442009-06-12 22:08:06 +00002435 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002436 ArgList[I].getAsDecl());
Douglas Gregor42476442009-06-12 22:08:06 +00002437 if (!ArgDecl)
Mike Stump25cf7602009-09-09 15:08:12 +00002438 if (DeclRefExpr *DRE
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002439 = dyn_cast_or_null<DeclRefExpr>(ArgList[I].getAsExpr()))
Douglas Gregor42476442009-06-12 22:08:06 +00002440 ArgDecl = dyn_cast<TemplateTemplateParmDecl>(DRE->getDecl());
2441
2442 if (!ArgDecl ||
2443 ArgDecl->getIndex() != TTP->getIndex() ||
2444 ArgDecl->getDepth() != TTP->getDepth())
2445 MirrorsPrimaryTemplate = false;
2446 }
2447 }
2448
Mike Stump25cf7602009-09-09 15:08:12 +00002449 NonTypeTemplateParmDecl *Param
Douglas Gregor76e79952009-06-12 21:21:02 +00002450 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
Douglas Gregor42476442009-06-12 22:08:06 +00002451 if (!Param) {
Douglas Gregor76e79952009-06-12 21:21:02 +00002452 continue;
Douglas Gregor42476442009-06-12 22:08:06 +00002453 }
2454
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002455 Expr *ArgExpr = ArgList[I].getAsExpr();
Douglas Gregor42476442009-06-12 22:08:06 +00002456 if (!ArgExpr) {
2457 MirrorsPrimaryTemplate = false;
Douglas Gregor76e79952009-06-12 21:21:02 +00002458 continue;
Douglas Gregor42476442009-06-12 22:08:06 +00002459 }
Douglas Gregor76e79952009-06-12 21:21:02 +00002460
2461 // C++ [temp.class.spec]p8:
2462 // A non-type argument is non-specialized if it is the name of a
2463 // non-type parameter. All other non-type arguments are
2464 // specialized.
2465 //
2466 // Below, we check the two conditions that only apply to
2467 // specialized non-type arguments, so skip any non-specialized
2468 // arguments.
2469 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Mike Stump25cf7602009-09-09 15:08:12 +00002470 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor42476442009-06-12 22:08:06 +00002471 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
Mike Stump25cf7602009-09-09 15:08:12 +00002472 if (MirrorsPrimaryTemplate &&
Douglas Gregor42476442009-06-12 22:08:06 +00002473 (Param->getIndex() != NTTP->getIndex() ||
2474 Param->getDepth() != NTTP->getDepth()))
2475 MirrorsPrimaryTemplate = false;
2476
Douglas Gregor76e79952009-06-12 21:21:02 +00002477 continue;
Douglas Gregor42476442009-06-12 22:08:06 +00002478 }
Douglas Gregor76e79952009-06-12 21:21:02 +00002479
2480 // C++ [temp.class.spec]p9:
2481 // Within the argument list of a class template partial
2482 // specialization, the following restrictions apply:
2483 // -- A partially specialized non-type argument expression
2484 // shall not involve a template parameter of the partial
2485 // specialization except when the argument expression is a
2486 // simple identifier.
2487 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002488 Diag(ArgExpr->getLocStart(),
Douglas Gregor76e79952009-06-12 21:21:02 +00002489 diag::err_dependent_non_type_arg_in_partial_spec)
2490 << ArgExpr->getSourceRange();
2491 return true;
2492 }
2493
2494 // -- The type of a template parameter corresponding to a
2495 // specialized non-type argument shall not be dependent on a
2496 // parameter of the specialization.
2497 if (Param->getType()->isDependentType()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002498 Diag(ArgExpr->getLocStart(),
Douglas Gregor76e79952009-06-12 21:21:02 +00002499 diag::err_dependent_typed_non_type_arg_in_partial_spec)
2500 << Param->getType()
2501 << ArgExpr->getSourceRange();
2502 Diag(Param->getLocation(), diag::note_template_param_here);
2503 return true;
2504 }
Douglas Gregor42476442009-06-12 22:08:06 +00002505
2506 MirrorsPrimaryTemplate = false;
Douglas Gregor76e79952009-06-12 21:21:02 +00002507 }
2508
2509 return false;
2510}
2511
Douglas Gregorc5d6fa72009-03-25 00:13:59 +00002512Sema::DeclResult
John McCall069c23a2009-07-31 02:45:11 +00002513Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
2514 TagUseKind TUK,
Mike Stump25cf7602009-09-09 15:08:12 +00002515 SourceLocation KWLoc,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002516 const CXXScopeSpec &SS,
Douglas Gregordd13e842009-03-30 22:58:21 +00002517 TemplateTy TemplateD,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002518 SourceLocation TemplateNameLoc,
2519 SourceLocation LAngleLoc,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00002520 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002521 SourceLocation *TemplateArgLocs,
2522 SourceLocation RAngleLoc,
2523 AttributeList *Attr,
2524 MultiTemplateParamsArg TemplateParameterLists) {
John McCallfd025182009-09-04 01:14:41 +00002525 assert(TUK == TUK_Declaration || TUK == TUK_Definition);
2526
Douglas Gregora08b6c72009-02-17 23:15:12 +00002527 // Find the class template we're specializing
Douglas Gregordd13e842009-03-30 22:58:21 +00002528 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump25cf7602009-09-09 15:08:12 +00002529 ClassTemplateDecl *ClassTemplate
Douglas Gregordd13e842009-03-30 22:58:21 +00002530 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
Douglas Gregora08b6c72009-02-17 23:15:12 +00002531
Douglas Gregor58944ac2009-05-31 09:31:02 +00002532 bool isPartialSpecialization = false;
2533
Douglas Gregor0d93f692009-02-25 22:02:03 +00002534 // Check the validity of the template headers that introduce this
2535 // template.
Douglas Gregore305ae32009-08-25 17:23:04 +00002536 TemplateParameterList *TemplateParams
Mike Stump25cf7602009-09-09 15:08:12 +00002537 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
2538 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregore305ae32009-08-25 17:23:04 +00002539 TemplateParameterLists.size());
2540 if (TemplateParams && TemplateParams->size() > 0) {
2541 isPartialSpecialization = true;
Douglas Gregor0d93f692009-02-25 22:02:03 +00002542
Douglas Gregore305ae32009-08-25 17:23:04 +00002543 // C++ [temp.class.spec]p10:
2544 // The template parameter list of a specialization shall not
2545 // contain default template argument values.
2546 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2547 Decl *Param = TemplateParams->getParam(I);
2548 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
2549 if (TTP->hasDefaultArgument()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002550 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregore305ae32009-08-25 17:23:04 +00002551 diag::err_default_arg_in_partial_spec);
2552 TTP->setDefaultArgument(QualType(), SourceLocation(), false);
2553 }
2554 } else if (NonTypeTemplateParmDecl *NTTP
2555 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2556 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002557 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregore305ae32009-08-25 17:23:04 +00002558 diag::err_default_arg_in_partial_spec)
2559 << DefArg->getSourceRange();
2560 NTTP->setDefaultArgument(0);
2561 DefArg->Destroy(Context);
2562 }
2563 } else {
2564 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
2565 if (Expr *DefArg = TTP->getDefaultArgument()) {
Mike Stump25cf7602009-09-09 15:08:12 +00002566 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregore305ae32009-08-25 17:23:04 +00002567 diag::err_default_arg_in_partial_spec)
2568 << DefArg->getSourceRange();
2569 TTP->setDefaultArgument(0);
2570 DefArg->Destroy(Context);
Douglas Gregorfbcc9e92009-06-12 19:43:02 +00002571 }
2572 }
2573 }
Douglas Gregore305ae32009-08-25 17:23:04 +00002574 } else if (!TemplateParams)
2575 Diag(KWLoc, diag::err_template_spec_needs_header)
2576 << CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor0d93f692009-02-25 22:02:03 +00002577
Douglas Gregora08b6c72009-02-17 23:15:12 +00002578 // Check that the specialization uses the same tag kind as the
2579 // original template.
2580 TagDecl::TagKind Kind;
2581 switch (TagSpec) {
2582 default: assert(0 && "Unknown tag type!");
2583 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2584 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2585 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2586 }
Douglas Gregor625185c2009-05-14 16:41:31 +00002587 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump25cf7602009-09-09 15:08:12 +00002588 Kind, KWLoc,
Douglas Gregor625185c2009-05-14 16:41:31 +00002589 *ClassTemplate->getIdentifier())) {
Mike Stump25cf7602009-09-09 15:08:12 +00002590 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor3faaa812009-04-01 23:51:29 +00002591 << ClassTemplate
Mike Stump25cf7602009-09-09 15:08:12 +00002592 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor3faaa812009-04-01 23:51:29 +00002593 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump25cf7602009-09-09 15:08:12 +00002594 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregora08b6c72009-02-17 23:15:12 +00002595 diag::note_previous_use);
2596 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2597 }
2598
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00002599 // Translate the parser's template argument list in our AST format.
2600 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2601 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2602
Douglas Gregora08b6c72009-02-17 23:15:12 +00002603 // Check that the template argument list is well-formed for this
2604 // template.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002605 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2606 TemplateArgs.size());
Mike Stump25cf7602009-09-09 15:08:12 +00002607 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002608 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregorecd63b82009-07-01 00:28:38 +00002609 RAngleLoc, false, Converted))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +00002610 return true;
Douglas Gregora08b6c72009-02-17 23:15:12 +00002611
Mike Stump25cf7602009-09-09 15:08:12 +00002612 assert((Converted.structuredSize() ==
Douglas Gregora08b6c72009-02-17 23:15:12 +00002613 ClassTemplate->getTemplateParameters()->size()) &&
2614 "Converted template argument list is too short!");
Mike Stump25cf7602009-09-09 15:08:12 +00002615
Douglas Gregor58944ac2009-05-31 09:31:02 +00002616 // Find the class template (partial) specialization declaration that
Douglas Gregora08b6c72009-02-17 23:15:12 +00002617 // corresponds to these arguments.
2618 llvm::FoldingSetNodeID ID;
Douglas Gregorfbcc9e92009-06-12 19:43:02 +00002619 if (isPartialSpecialization) {
Douglas Gregor42476442009-06-12 22:08:06 +00002620 bool MirrorsPrimaryTemplate;
Douglas Gregor76e79952009-06-12 21:21:02 +00002621 if (CheckClassTemplatePartialSpecializationArgs(
2622 ClassTemplate->getTemplateParameters(),
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002623 Converted, MirrorsPrimaryTemplate))
Douglas Gregor76e79952009-06-12 21:21:02 +00002624 return true;
2625
Douglas Gregor42476442009-06-12 22:08:06 +00002626 if (MirrorsPrimaryTemplate) {
2627 // C++ [temp.class.spec]p9b3:
2628 //
Mike Stump25cf7602009-09-09 15:08:12 +00002629 // -- The argument list of the specialization shall not be identical
2630 // to the implicit argument list of the primary template.
Douglas Gregor42476442009-06-12 22:08:06 +00002631 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
John McCall069c23a2009-07-31 02:45:11 +00002632 << (TUK == TUK_Definition)
Mike Stump25cf7602009-09-09 15:08:12 +00002633 << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
Douglas Gregor42476442009-06-12 22:08:06 +00002634 RAngleLoc));
John McCall069c23a2009-07-31 02:45:11 +00002635 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
Douglas Gregor42476442009-06-12 22:08:06 +00002636 ClassTemplate->getIdentifier(),
2637 TemplateNameLoc,
2638 Attr,
Douglas Gregore305ae32009-08-25 17:23:04 +00002639 TemplateParams,
Douglas Gregor42476442009-06-12 22:08:06 +00002640 AS_none);
2641 }
2642
Douglas Gregor58944ac2009-05-31 09:31:02 +00002643 // FIXME: Template parameter list matters, too
Mike Stump25cf7602009-09-09 15:08:12 +00002644 ClassTemplatePartialSpecializationDecl::Profile(ID,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002645 Converted.getFlatArguments(),
Douglas Gregor99eef4a2009-07-29 16:09:57 +00002646 Converted.flatSize(),
2647 Context);
Mike Stump90fc78e2009-08-04 21:02:39 +00002648 } else
Anders Carlssona35faf92009-06-05 03:43:12 +00002649 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002650 Converted.getFlatArguments(),
Douglas Gregor99eef4a2009-07-29 16:09:57 +00002651 Converted.flatSize(),
2652 Context);
Douglas Gregora08b6c72009-02-17 23:15:12 +00002653 void *InsertPos = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +00002654 ClassTemplateSpecializationDecl *PrevDecl = 0;
2655
2656 if (isPartialSpecialization)
2657 PrevDecl
Mike Stump25cf7602009-09-09 15:08:12 +00002658 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor58944ac2009-05-31 09:31:02 +00002659 InsertPos);
2660 else
2661 PrevDecl
2662 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregora08b6c72009-02-17 23:15:12 +00002663
2664 ClassTemplateSpecializationDecl *Specialization = 0;
2665
Douglas Gregor0d93f692009-02-25 22:02:03 +00002666 // Check whether we can declare a class template specialization in
2667 // the current scope.
2668 if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl,
Mike Stump25cf7602009-09-09 15:08:12 +00002669 TemplateNameLoc,
Douglas Gregor90177912009-05-13 18:28:20 +00002670 SS.getRange(),
Douglas Gregor4faa4262009-06-12 22:21:45 +00002671 isPartialSpecialization,
Douglas Gregor90177912009-05-13 18:28:20 +00002672 /*ExplicitInstantiation=*/false))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +00002673 return true;
Douglas Gregor0d93f692009-02-25 22:02:03 +00002674
Douglas Gregor1930d202009-07-30 17:40:51 +00002675 // The canonical type
2676 QualType CanonType;
Douglas Gregora08b6c72009-02-17 23:15:12 +00002677 if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
2678 // Since the only prior class template specialization with these
2679 // arguments was referenced but not declared, reuse that
2680 // declaration node as our own, updating its source location to
2681 // reflect our new declaration.
Douglas Gregora08b6c72009-02-17 23:15:12 +00002682 Specialization = PrevDecl;
Douglas Gregor50113ca2009-02-25 22:18:32 +00002683 Specialization->setLocation(TemplateNameLoc);
Douglas Gregora08b6c72009-02-17 23:15:12 +00002684 PrevDecl = 0;
Douglas Gregor1930d202009-07-30 17:40:51 +00002685 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregor58944ac2009-05-31 09:31:02 +00002686 } else if (isPartialSpecialization) {
Douglas Gregor1930d202009-07-30 17:40:51 +00002687 // Build the canonical type that describes the converted template
2688 // arguments of the class template partial specialization.
2689 CanonType = Context.getTemplateSpecializationType(
2690 TemplateName(ClassTemplate),
2691 Converted.getFlatArguments(),
2692 Converted.flatSize());
2693
Douglas Gregor58944ac2009-05-31 09:31:02 +00002694 // Create a new class template partial specialization declaration node.
Mike Stump25cf7602009-09-09 15:08:12 +00002695 TemplateParameterList *TemplateParams
Douglas Gregor58944ac2009-05-31 09:31:02 +00002696 = static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
2697 ClassTemplatePartialSpecializationDecl *PrevPartial
2698 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Mike Stump25cf7602009-09-09 15:08:12 +00002699 ClassTemplatePartialSpecializationDecl *Partial
2700 = ClassTemplatePartialSpecializationDecl::Create(Context,
Douglas Gregor58944ac2009-05-31 09:31:02 +00002701 ClassTemplate->getDeclContext(),
Anders Carlsson6e9d02f2009-06-05 04:06:48 +00002702 TemplateNameLoc,
2703 TemplateParams,
2704 ClassTemplate,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002705 Converted,
Anders Carlsson6e9d02f2009-06-05 04:06:48 +00002706 PrevPartial);
Douglas Gregor58944ac2009-05-31 09:31:02 +00002707
2708 if (PrevPartial) {
2709 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
2710 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
2711 } else {
2712 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
2713 }
2714 Specialization = Partial;
Douglas Gregorf90c2132009-06-13 00:26:55 +00002715
2716 // Check that all of the template parameters of the class template
2717 // partial specialization are deducible from the template
2718 // arguments. If not, this class template partial specialization
2719 // will never be used.
2720 llvm::SmallVector<bool, 8> DeducibleParams;
2721 DeducibleParams.resize(TemplateParams->size());
2722 MarkDeducedTemplateParameters(Partial->getTemplateArgs(), DeducibleParams);
2723 unsigned NumNonDeducible = 0;
2724 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
2725 if (!DeducibleParams[I])
2726 ++NumNonDeducible;
2727
2728 if (NumNonDeducible) {
2729 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
2730 << (NumNonDeducible > 1)
2731 << SourceRange(TemplateNameLoc, RAngleLoc);
2732 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
2733 if (!DeducibleParams[I]) {
2734 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
2735 if (Param->getDeclName())
Mike Stump25cf7602009-09-09 15:08:12 +00002736 Diag(Param->getLocation(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00002737 diag::note_partial_spec_unused_parameter)
2738 << Param->getDeclName();
2739 else
Mike Stump25cf7602009-09-09 15:08:12 +00002740 Diag(Param->getLocation(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00002741 diag::note_partial_spec_unused_parameter)
2742 << std::string("<anonymous>");
2743 }
2744 }
2745 }
Douglas Gregora08b6c72009-02-17 23:15:12 +00002746 } else {
2747 // Create a new class template specialization declaration node for
2748 // this explicit specialization.
2749 Specialization
Mike Stump25cf7602009-09-09 15:08:12 +00002750 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002751 ClassTemplate->getDeclContext(),
2752 TemplateNameLoc,
Mike Stump25cf7602009-09-09 15:08:12 +00002753 ClassTemplate,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002754 Converted,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002755 PrevDecl);
2756
2757 if (PrevDecl) {
2758 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
2759 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
2760 } else {
Mike Stump25cf7602009-09-09 15:08:12 +00002761 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregora08b6c72009-02-17 23:15:12 +00002762 InsertPos);
2763 }
Douglas Gregor1930d202009-07-30 17:40:51 +00002764
2765 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregora08b6c72009-02-17 23:15:12 +00002766 }
2767
2768 // Note that this is an explicit specialization.
2769 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
2770
2771 // Check that this isn't a redefinition of this specialization.
John McCall069c23a2009-07-31 02:45:11 +00002772 if (TUK == TUK_Definition) {
Douglas Gregora08b6c72009-02-17 23:15:12 +00002773 if (RecordDecl *Def = Specialization->getDefinition(Context)) {
Mike Stumpe127ae32009-05-16 07:39:55 +00002774 // FIXME: Should also handle explicit specialization after implicit
2775 // instantiation with a special diagnostic.
Douglas Gregora08b6c72009-02-17 23:15:12 +00002776 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump25cf7602009-09-09 15:08:12 +00002777 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregor58944ac2009-05-31 09:31:02 +00002778 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +00002779 Diag(Def->getLocation(), diag::note_previous_definition);
2780 Specialization->setInvalidDecl();
Douglas Gregorc5d6fa72009-03-25 00:13:59 +00002781 return true;
Douglas Gregora08b6c72009-02-17 23:15:12 +00002782 }
2783 }
2784
Douglas Gregor9c7825b2009-02-26 22:19:44 +00002785 // Build the fully-sugared type for this class template
2786 // specialization as the user wrote in the specialization
2787 // itself. This means that we'll pretty-print the type retrieved
2788 // from the specialization's declaration the way that the user
2789 // actually wrote the specialization, rather than formatting the
2790 // name based on the "canonical" representation used to store the
2791 // template arguments in the specialization.
Mike Stump25cf7602009-09-09 15:08:12 +00002792 QualType WrittenTy
2793 = Context.getTemplateSpecializationType(Name,
Anders Carlsson13fac3f2009-06-13 18:20:51 +00002794 TemplateArgs.data(),
Douglas Gregordd13e842009-03-30 22:58:21 +00002795 TemplateArgs.size(),
Douglas Gregor1930d202009-07-30 17:40:51 +00002796 CanonType);
Douglas Gregordd13e842009-03-30 22:58:21 +00002797 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00002798 TemplateArgsIn.release();
Douglas Gregora08b6c72009-02-17 23:15:12 +00002799
Douglas Gregor50113ca2009-02-25 22:18:32 +00002800 // C++ [temp.expl.spec]p9:
2801 // A template explicit specialization is in the scope of the
2802 // namespace in which the template was defined.
2803 //
2804 // We actually implement this paragraph where we set the semantic
2805 // context (in the creation of the ClassTemplateSpecializationDecl),
2806 // but we also maintain the lexical context where the actual
2807 // definition occurs.
Douglas Gregora08b6c72009-02-17 23:15:12 +00002808 Specialization->setLexicalDeclContext(CurContext);
Mike Stump25cf7602009-09-09 15:08:12 +00002809
Douglas Gregora08b6c72009-02-17 23:15:12 +00002810 // We may be starting the definition of this specialization.
John McCall069c23a2009-07-31 02:45:11 +00002811 if (TUK == TUK_Definition)
Douglas Gregora08b6c72009-02-17 23:15:12 +00002812 Specialization->startDefinition();
2813
2814 // Add the specialization into its lexical context, so that it can
2815 // be seen when iterating through the list of declarations in that
2816 // context. However, specializations are not found by name lookup.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002817 CurContext->addDecl(Specialization);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002818 return DeclPtrTy::make(Specialization);
Douglas Gregora08b6c72009-02-17 23:15:12 +00002819}
Douglas Gregord3022602009-03-27 23:10:48 +00002820
Mike Stump25cf7602009-09-09 15:08:12 +00002821Sema::DeclPtrTy
2822Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregor2ae1d772009-06-23 23:11:28 +00002823 MultiTemplateParamsArg TemplateParameterLists,
2824 Declarator &D) {
2825 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
2826}
2827
Mike Stump25cf7602009-09-09 15:08:12 +00002828Sema::DeclPtrTy
2829Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor19d10652009-06-24 00:54:41 +00002830 MultiTemplateParamsArg TemplateParameterLists,
2831 Declarator &D) {
2832 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
2833 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
2834 "Not a function declarator!");
2835 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Mike Stump25cf7602009-09-09 15:08:12 +00002836
Douglas Gregor19d10652009-06-24 00:54:41 +00002837 if (FTI.hasPrototype) {
Mike Stump25cf7602009-09-09 15:08:12 +00002838 // FIXME: Diagnose arguments without names in C.
Douglas Gregor19d10652009-06-24 00:54:41 +00002839 }
Mike Stump25cf7602009-09-09 15:08:12 +00002840
Douglas Gregor19d10652009-06-24 00:54:41 +00002841 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump25cf7602009-09-09 15:08:12 +00002842
2843 DeclPtrTy DP = HandleDeclarator(ParentScope, D,
Douglas Gregor19d10652009-06-24 00:54:41 +00002844 move(TemplateParameterLists),
2845 /*IsFunctionDefinition=*/true);
Mike Stump25cf7602009-09-09 15:08:12 +00002846 if (FunctionTemplateDecl *FunctionTemplate
Douglas Gregorb462c322009-07-21 23:53:31 +00002847 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
Mike Stump25cf7602009-09-09 15:08:12 +00002848 return ActOnStartOfFunctionDef(FnBodyScope,
Douglas Gregorb60eb752009-06-25 22:08:12 +00002849 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
Douglas Gregorb462c322009-07-21 23:53:31 +00002850 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
2851 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
Douglas Gregorb60eb752009-06-25 22:08:12 +00002852 return DeclPtrTy();
Douglas Gregor19d10652009-06-24 00:54:41 +00002853}
2854
Douglas Gregor96b6df92009-05-14 00:28:11 +00002855// Explicit instantiation of a class template specialization
Douglas Gregor7a374722009-09-04 06:33:52 +00002856// FIXME: Implement extern template semantics
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002857Sema::DeclResult
Mike Stump25cf7602009-09-09 15:08:12 +00002858Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor7a374722009-09-04 06:33:52 +00002859 SourceLocation ExternLoc,
2860 SourceLocation TemplateLoc,
Mike Stump25cf7602009-09-09 15:08:12 +00002861 unsigned TagSpec,
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002862 SourceLocation KWLoc,
2863 const CXXScopeSpec &SS,
2864 TemplateTy TemplateD,
2865 SourceLocation TemplateNameLoc,
2866 SourceLocation LAngleLoc,
2867 ASTTemplateArgsPtr TemplateArgsIn,
2868 SourceLocation *TemplateArgLocs,
2869 SourceLocation RAngleLoc,
2870 AttributeList *Attr) {
2871 // Find the class template we're specializing
2872 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump25cf7602009-09-09 15:08:12 +00002873 ClassTemplateDecl *ClassTemplate
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002874 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
2875
2876 // Check that the specialization uses the same tag kind as the
2877 // original template.
2878 TagDecl::TagKind Kind;
2879 switch (TagSpec) {
2880 default: assert(0 && "Unknown tag type!");
2881 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2882 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2883 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2884 }
Douglas Gregor625185c2009-05-14 16:41:31 +00002885 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump25cf7602009-09-09 15:08:12 +00002886 Kind, KWLoc,
Douglas Gregor625185c2009-05-14 16:41:31 +00002887 *ClassTemplate->getIdentifier())) {
Mike Stump25cf7602009-09-09 15:08:12 +00002888 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002889 << ClassTemplate
Mike Stump25cf7602009-09-09 15:08:12 +00002890 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002891 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump25cf7602009-09-09 15:08:12 +00002892 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002893 diag::note_previous_use);
2894 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2895 }
2896
Douglas Gregor90177912009-05-13 18:28:20 +00002897 // C++0x [temp.explicit]p2:
2898 // [...] An explicit instantiation shall appear in an enclosing
2899 // namespace of its template. [...]
2900 //
2901 // This is C++ DR 275.
2902 if (CheckClassTemplateSpecializationScope(ClassTemplate, 0,
Mike Stump25cf7602009-09-09 15:08:12 +00002903 TemplateNameLoc,
Douglas Gregor90177912009-05-13 18:28:20 +00002904 SS.getRange(),
Douglas Gregor4faa4262009-06-12 22:21:45 +00002905 /*PartialSpecialization=*/false,
Douglas Gregor90177912009-05-13 18:28:20 +00002906 /*ExplicitInstantiation=*/true))
2907 return true;
2908
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002909 // Translate the parser's template argument list in our AST format.
2910 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2911 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2912
2913 // Check that the template argument list is well-formed for this
2914 // template.
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002915 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2916 TemplateArgs.size());
Mike Stump25cf7602009-09-09 15:08:12 +00002917 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlssonb912b392009-06-05 02:12:32 +00002918 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregorecd63b82009-07-01 00:28:38 +00002919 RAngleLoc, false, Converted))
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002920 return true;
2921
Mike Stump25cf7602009-09-09 15:08:12 +00002922 assert((Converted.structuredSize() ==
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002923 ClassTemplate->getTemplateParameters()->size()) &&
2924 "Converted template argument list is too short!");
Mike Stump25cf7602009-09-09 15:08:12 +00002925
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002926 // Find the class template specialization declaration that
2927 // corresponds to these arguments.
2928 llvm::FoldingSetNodeID ID;
Mike Stump25cf7602009-09-09 15:08:12 +00002929 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002930 Converted.getFlatArguments(),
Douglas Gregor99eef4a2009-07-29 16:09:57 +00002931 Converted.flatSize(),
2932 Context);
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002933 void *InsertPos = 0;
2934 ClassTemplateSpecializationDecl *PrevDecl
2935 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
2936
2937 ClassTemplateSpecializationDecl *Specialization = 0;
2938
Douglas Gregor90177912009-05-13 18:28:20 +00002939 bool SpecializationRequiresInstantiation = true;
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002940 if (PrevDecl) {
Mike Stump25cf7602009-09-09 15:08:12 +00002941 if (PrevDecl->getSpecializationKind()
Douglas Gregor8e7e4602009-09-04 22:48:11 +00002942 == TSK_ExplicitInstantiationDefinition) {
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002943 // This particular specialization has already been declared or
2944 // instantiated. We cannot explicitly instantiate it.
Douglas Gregor90177912009-05-13 18:28:20 +00002945 Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate)
2946 << Context.getTypeDeclType(PrevDecl);
Mike Stump25cf7602009-09-09 15:08:12 +00002947 Diag(PrevDecl->getLocation(),
Douglas Gregor90177912009-05-13 18:28:20 +00002948 diag::note_previous_explicit_instantiation);
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002949 return DeclPtrTy::make(PrevDecl);
2950 }
2951
Douglas Gregor90177912009-05-13 18:28:20 +00002952 if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
Douglas Gregor96b6df92009-05-14 00:28:11 +00002953 // C++ DR 259, C++0x [temp.explicit]p4:
Douglas Gregor90177912009-05-13 18:28:20 +00002954 // For a given set of template parameters, if an explicit
2955 // instantiation of a template appears after a declaration of
2956 // an explicit specialization for that template, the explicit
2957 // instantiation has no effect.
2958 if (!getLangOptions().CPlusPlus0x) {
Mike Stump25cf7602009-09-09 15:08:12 +00002959 Diag(TemplateNameLoc,
Douglas Gregor90177912009-05-13 18:28:20 +00002960 diag::ext_explicit_instantiation_after_specialization)
2961 << Context.getTypeDeclType(PrevDecl);
Mike Stump25cf7602009-09-09 15:08:12 +00002962 Diag(PrevDecl->getLocation(),
Douglas Gregor90177912009-05-13 18:28:20 +00002963 diag::note_previous_template_specialization);
2964 }
2965
2966 // Create a new class template specialization declaration node
2967 // for this explicit specialization. This node is only used to
2968 // record the existence of this explicit instantiation for
2969 // accurate reproduction of the source code; we don't actually
2970 // use it for anything, since it is semantically irrelevant.
2971 Specialization
Mike Stump25cf7602009-09-09 15:08:12 +00002972 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor90177912009-05-13 18:28:20 +00002973 ClassTemplate->getDeclContext(),
2974 TemplateNameLoc,
2975 ClassTemplate,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00002976 Converted, 0);
Douglas Gregor90177912009-05-13 18:28:20 +00002977 Specialization->setLexicalDeclContext(CurContext);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002978 CurContext->addDecl(Specialization);
Douglas Gregor90177912009-05-13 18:28:20 +00002979 return DeclPtrTy::make(Specialization);
2980 }
2981
2982 // If we have already (implicitly) instantiated this
2983 // specialization, there is less work to do.
2984 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
2985 SpecializationRequiresInstantiation = false;
2986
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002987 // Since the only prior class template specialization with these
2988 // arguments was referenced but not declared, reuse that
2989 // declaration node as our own, updating its source location to
2990 // reflect our new declaration.
2991 Specialization = PrevDecl;
2992 Specialization->setLocation(TemplateNameLoc);
2993 PrevDecl = 0;
2994 } else {
2995 // Create a new class template specialization declaration node for
2996 // this explicit specialization.
2997 Specialization
Mike Stump25cf7602009-09-09 15:08:12 +00002998 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorfd79ac62009-05-13 00:25:59 +00002999 ClassTemplate->getDeclContext(),
3000 TemplateNameLoc,
3001 ClassTemplate,
Anders Carlssonb0fc9992009-06-23 01:26:57 +00003002 Converted, 0);
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003003
Mike Stump25cf7602009-09-09 15:08:12 +00003004 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003005 InsertPos);
3006 }
3007
3008 // Build the fully-sugared type for this explicit instantiation as
3009 // the user wrote in the explicit instantiation itself. This means
3010 // that we'll pretty-print the type retrieved from the
3011 // specialization's declaration the way that the user actually wrote
3012 // the explicit instantiation, rather than formatting the name based
3013 // on the "canonical" representation used to store the template
3014 // arguments in the specialization.
Mike Stump25cf7602009-09-09 15:08:12 +00003015 QualType WrittenTy
3016 = Context.getTemplateSpecializationType(Name,
Anders Carlssonefbff322009-06-05 02:45:24 +00003017 TemplateArgs.data(),
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003018 TemplateArgs.size(),
3019 Context.getTypeDeclType(Specialization));
3020 Specialization->setTypeAsWritten(WrittenTy);
3021 TemplateArgsIn.release();
3022
3023 // Add the explicit instantiation into its lexical context. However,
3024 // since explicit instantiations are never found by name lookup, we
3025 // just put it into the declaration context directly.
3026 Specialization->setLexicalDeclContext(CurContext);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00003027 CurContext->addDecl(Specialization);
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003028
3029 // C++ [temp.explicit]p3:
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003030 // A definition of a class template or class member template
3031 // shall be in scope at the point of the explicit instantiation of
3032 // the class template or class member template.
3033 //
3034 // This check comes when we actually try to perform the
3035 // instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003036 TemplateSpecializationKind TSK
Mike Stump25cf7602009-09-09 15:08:12 +00003037 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003038 : TSK_ExplicitInstantiationDeclaration;
Douglas Gregor556d8c72009-05-15 17:59:04 +00003039 if (SpecializationRequiresInstantiation)
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003040 InstantiateClassTemplateSpecialization(Specialization, TSK);
Douglas Gregorb12249d2009-05-18 17:01:57 +00003041 else // Instantiate the members of this class template specialization.
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003042 InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization,
3043 TSK);
Douglas Gregorfd79ac62009-05-13 00:25:59 +00003044
3045 return DeclPtrTy::make(Specialization);
3046}
3047
Douglas Gregor96b6df92009-05-14 00:28:11 +00003048// Explicit instantiation of a member class of a class template.
3049Sema::DeclResult
Mike Stump25cf7602009-09-09 15:08:12 +00003050Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor7a374722009-09-04 06:33:52 +00003051 SourceLocation ExternLoc,
3052 SourceLocation TemplateLoc,
Mike Stump25cf7602009-09-09 15:08:12 +00003053 unsigned TagSpec,
Douglas Gregor96b6df92009-05-14 00:28:11 +00003054 SourceLocation KWLoc,
3055 const CXXScopeSpec &SS,
3056 IdentifierInfo *Name,
3057 SourceLocation NameLoc,
3058 AttributeList *Attr) {
3059
Douglas Gregor71f06032009-05-28 23:31:59 +00003060 bool Owned = false;
John McCall069c23a2009-07-31 02:45:11 +00003061 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
Douglas Gregor84a20812009-07-22 23:48:44 +00003062 KWLoc, SS, Name, NameLoc, Attr, AS_none,
3063 MultiTemplateParamsArg(*this, 0, 0), Owned);
Douglas Gregor96b6df92009-05-14 00:28:11 +00003064 if (!TagD)
3065 return true;
3066
3067 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
3068 if (Tag->isEnum()) {
3069 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
3070 << Context.getTypeDeclType(Tag);
3071 return true;
3072 }
3073
Douglas Gregord769bfa2009-05-27 17:30:49 +00003074 if (Tag->isInvalidDecl())
3075 return true;
3076
Douglas Gregor96b6df92009-05-14 00:28:11 +00003077 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
3078 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
3079 if (!Pattern) {
3080 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
3081 << Context.getTypeDeclType(Record);
3082 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
3083 return true;
3084 }
3085
3086 // C++0x [temp.explicit]p2:
3087 // [...] An explicit instantiation shall appear in an enclosing
3088 // namespace of its template. [...]
3089 //
3090 // This is C++ DR 275.
3091 if (getLangOptions().CPlusPlus0x) {
Mike Stumpe127ae32009-05-16 07:39:55 +00003092 // FIXME: In C++98, we would like to turn these errors into warnings,
3093 // dependent on a -Wc++0x flag.
Mike Stump25cf7602009-09-09 15:08:12 +00003094 DeclContext *PatternContext
Douglas Gregor96b6df92009-05-14 00:28:11 +00003095 = Pattern->getDeclContext()->getEnclosingNamespaceContext();
3096 if (!CurContext->Encloses(PatternContext)) {
3097 Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope)
3098 << Record << cast<NamedDecl>(PatternContext) << SS.getRange();
3099 Diag(Pattern->getLocation(), diag::note_previous_declaration);
3100 }
3101 }
3102
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003103 TemplateSpecializationKind TSK
Mike Stump25cf7602009-09-09 15:08:12 +00003104 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003105 : TSK_ExplicitInstantiationDeclaration;
Mike Stump25cf7602009-09-09 15:08:12 +00003106
Douglas Gregor96b6df92009-05-14 00:28:11 +00003107 if (!Record->getDefinition(Context)) {
3108 // If the class has a definition, instantiate it (and all of its
3109 // members, recursively).
3110 Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
Mike Stump25cf7602009-09-09 15:08:12 +00003111 if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern,
Douglas Gregor5f62c5e2009-05-14 23:26:13 +00003112 getTemplateInstantiationArgs(Record),
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003113 TSK))
Douglas Gregor96b6df92009-05-14 00:28:11 +00003114 return true;
John McCall0ba26ee2009-08-25 22:02:44 +00003115 } else // Instantiate all of the members of the class.
Mike Stump25cf7602009-09-09 15:08:12 +00003116 InstantiateClassMembers(TemplateLoc, Record,
Douglas Gregor8e7e4602009-09-04 22:48:11 +00003117 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor96b6df92009-05-14 00:28:11 +00003118
Mike Stumpe127ae32009-05-16 07:39:55 +00003119 // FIXME: We don't have any representation for explicit instantiations of
3120 // member classes. Such a representation is not needed for compilation, but it
3121 // should be available for clients that want to see all of the declarations in
3122 // the source code.
Douglas Gregor96b6df92009-05-14 00:28:11 +00003123 return TagD;
3124}
3125
Douglas Gregord3022602009-03-27 23:10:48 +00003126Sema::TypeResult
3127Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3128 const IdentifierInfo &II, SourceLocation IdLoc) {
Mike Stump25cf7602009-09-09 15:08:12 +00003129 NestedNameSpecifier *NNS
Douglas Gregord3022602009-03-27 23:10:48 +00003130 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3131 if (!NNS)
3132 return true;
3133
3134 QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
Douglas Gregord7cb0372009-04-01 21:51:26 +00003135 if (T.isNull())
3136 return true;
Douglas Gregord3022602009-03-27 23:10:48 +00003137 return T.getAsOpaquePtr();
3138}
3139
Douglas Gregor77da5802009-04-01 00:28:59 +00003140Sema::TypeResult
3141Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3142 SourceLocation TemplateLoc, TypeTy *Ty) {
Argiris Kirtzidisd6802ba2009-08-19 01:28:28 +00003143 QualType T = GetTypeFromParser(Ty);
Mike Stump25cf7602009-09-09 15:08:12 +00003144 NestedNameSpecifier *NNS
Douglas Gregor77da5802009-04-01 00:28:59 +00003145 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump25cf7602009-09-09 15:08:12 +00003146 const TemplateSpecializationType *TemplateId
Douglas Gregor77da5802009-04-01 00:28:59 +00003147 = T->getAsTemplateSpecializationType();
3148 assert(TemplateId && "Expected a template specialization type");
3149
Douglas Gregorfaa28fb2009-09-02 13:05:45 +00003150 if (computeDeclContext(SS, false)) {
3151 // If we can compute a declaration context, then the "typename"
3152 // keyword was superfluous. Just build a QualifiedNameType to keep
3153 // track of the nested-name-specifier.
Mike Stump25cf7602009-09-09 15:08:12 +00003154
Douglas Gregorfaa28fb2009-09-02 13:05:45 +00003155 // FIXME: Note that the QualifiedNameType had the "typename" keyword!
3156 return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
3157 }
Mike Stump25cf7602009-09-09 15:08:12 +00003158
Douglas Gregorfaa28fb2009-09-02 13:05:45 +00003159 return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
Douglas Gregor77da5802009-04-01 00:28:59 +00003160}
3161
Douglas Gregord3022602009-03-27 23:10:48 +00003162/// \brief Build the type that describes a C++ typename specifier,
3163/// e.g., "typename T::type".
3164QualType
3165Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
3166 SourceRange Range) {
Douglas Gregor3eb20702009-05-11 19:58:34 +00003167 CXXRecordDecl *CurrentInstantiation = 0;
3168 if (NNS->isDependent()) {
3169 CurrentInstantiation = getCurrentInstantiationOf(NNS);
Douglas Gregord3022602009-03-27 23:10:48 +00003170
Douglas Gregor3eb20702009-05-11 19:58:34 +00003171 // If the nested-name-specifier does not refer to the current
3172 // instantiation, then build a typename type.
3173 if (!CurrentInstantiation)
3174 return Context.getTypenameType(NNS, &II);
Mike Stump25cf7602009-09-09 15:08:12 +00003175
Douglas Gregorab241c32009-09-02 13:12:51 +00003176 // The nested-name-specifier refers to the current instantiation, so the
3177 // "typename" keyword itself is superfluous. In C++03, the program is
Mike Stump25cf7602009-09-09 15:08:12 +00003178 // actually ill-formed. However, DR 382 (in C++0x CD1) allows such
Douglas Gregorab241c32009-09-02 13:12:51 +00003179 // extraneous "typename" keywords, and we retroactively apply this DR to
3180 // C++03 code.
Douglas Gregor3eb20702009-05-11 19:58:34 +00003181 }
Douglas Gregord3022602009-03-27 23:10:48 +00003182
Douglas Gregor3eb20702009-05-11 19:58:34 +00003183 DeclContext *Ctx = 0;
3184
3185 if (CurrentInstantiation)
3186 Ctx = CurrentInstantiation;
3187 else {
3188 CXXScopeSpec SS;
3189 SS.setScopeRep(NNS);
3190 SS.setRange(Range);
3191 if (RequireCompleteDeclContext(SS))
3192 return QualType();
3193
3194 Ctx = computeDeclContext(SS);
3195 }
Douglas Gregord3022602009-03-27 23:10:48 +00003196 assert(Ctx && "No declaration context?");
3197
3198 DeclarationName Name(&II);
Mike Stump25cf7602009-09-09 15:08:12 +00003199 LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName,
Douglas Gregord3022602009-03-27 23:10:48 +00003200 false);
3201 unsigned DiagID = 0;
3202 Decl *Referenced = 0;
3203 switch (Result.getKind()) {
3204 case LookupResult::NotFound:
3205 if (Ctx->isTranslationUnit())
3206 DiagID = diag::err_typename_nested_not_found_global;
3207 else
3208 DiagID = diag::err_typename_nested_not_found;
3209 break;
3210
3211 case LookupResult::Found:
3212 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) {
3213 // We found a type. Build a QualifiedNameType, since the
3214 // typename-specifier was just sugar. FIXME: Tell
3215 // QualifiedNameType that it has a "typename" prefix.
3216 return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
3217 }
3218
3219 DiagID = diag::err_typename_nested_not_type;
3220 Referenced = Result.getAsDecl();
3221 break;
3222
3223 case LookupResult::FoundOverloaded:
3224 DiagID = diag::err_typename_nested_not_type;
3225 Referenced = *Result.begin();
3226 break;
3227
3228 case LookupResult::AmbiguousBaseSubobjectTypes:
3229 case LookupResult::AmbiguousBaseSubobjects:
3230 case LookupResult::AmbiguousReference:
3231 DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range);
3232 return QualType();
3233 }
3234
3235 // If we get here, it's because name lookup did not find a
3236 // type. Emit an appropriate diagnostic and return an error.
3237 if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx))
3238 Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx;
3239 else
3240 Diag(Range.getEnd(), DiagID) << Range << Name;
3241 if (Referenced)
3242 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
3243 << Name;
3244 return QualType();
3245}
Douglas Gregor3f220e82009-08-06 16:20:37 +00003246
3247namespace {
3248 // See Sema::RebuildTypeInCurrentInstantiation
Mike Stump25cf7602009-09-09 15:08:12 +00003249 class VISIBILITY_HIDDEN CurrentInstantiationRebuilder
3250 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor3f220e82009-08-06 16:20:37 +00003251 SourceLocation Loc;
3252 DeclarationName Entity;
Mike Stump25cf7602009-09-09 15:08:12 +00003253
Douglas Gregor3f220e82009-08-06 16:20:37 +00003254 public:
Mike Stump25cf7602009-09-09 15:08:12 +00003255 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor3f220e82009-08-06 16:20:37 +00003256 SourceLocation Loc,
Mike Stump25cf7602009-09-09 15:08:12 +00003257 DeclarationName Entity)
3258 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor3f220e82009-08-06 16:20:37 +00003259 Loc(Loc), Entity(Entity) { }
Mike Stump25cf7602009-09-09 15:08:12 +00003260
3261 /// \brief Determine whether the given type \p T has already been
Douglas Gregor3f220e82009-08-06 16:20:37 +00003262 /// transformed.
3263 ///
3264 /// For the purposes of type reconstruction, a type has already been
3265 /// transformed if it is NULL or if it is not dependent.
3266 bool AlreadyTransformed(QualType T) {
3267 return T.isNull() || !T->isDependentType();
3268 }
Mike Stump25cf7602009-09-09 15:08:12 +00003269
3270 /// \brief Returns the location of the entity whose type is being
Douglas Gregor3f220e82009-08-06 16:20:37 +00003271 /// rebuilt.
3272 SourceLocation getBaseLocation() { return Loc; }
Mike Stump25cf7602009-09-09 15:08:12 +00003273
Douglas Gregor3f220e82009-08-06 16:20:37 +00003274 /// \brief Returns the name of the entity whose type is being rebuilt.
3275 DeclarationName getBaseEntity() { return Entity; }
Mike Stump25cf7602009-09-09 15:08:12 +00003276
Douglas Gregor3f220e82009-08-06 16:20:37 +00003277 /// \brief Transforms an expression by returning the expression itself
3278 /// (an identity function).
3279 ///
3280 /// FIXME: This is completely unsafe; we will need to actually clone the
3281 /// expressions.
3282 Sema::OwningExprResult TransformExpr(Expr *E) {
3283 return getSema().Owned(E);
3284 }
Mike Stump25cf7602009-09-09 15:08:12 +00003285
Douglas Gregor3f220e82009-08-06 16:20:37 +00003286 /// \brief Transforms a typename type by determining whether the type now
3287 /// refers to a member of the current instantiation, and then
3288 /// type-checking and building a QualifiedNameType (when possible).
3289 QualType TransformTypenameType(const TypenameType *T);
3290 };
3291}
3292
Mike Stump25cf7602009-09-09 15:08:12 +00003293QualType
Douglas Gregor3f220e82009-08-06 16:20:37 +00003294CurrentInstantiationRebuilder::TransformTypenameType(const TypenameType *T) {
3295 NestedNameSpecifier *NNS
3296 = TransformNestedNameSpecifier(T->getQualifier(),
3297 /*FIXME:*/SourceRange(getBaseLocation()));
3298 if (!NNS)
3299 return QualType();
3300
3301 // If the nested-name-specifier did not change, and we cannot compute the
3302 // context corresponding to the nested-name-specifier, then this
3303 // typename type will not change; exit early.
3304 CXXScopeSpec SS;
3305 SS.setRange(SourceRange(getBaseLocation()));
3306 SS.setScopeRep(NNS);
3307 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
3308 return QualType(T, 0);
Mike Stump25cf7602009-09-09 15:08:12 +00003309
3310 // Rebuild the typename type, which will probably turn into a
Douglas Gregor3f220e82009-08-06 16:20:37 +00003311 // QualifiedNameType.
3312 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
Mike Stump25cf7602009-09-09 15:08:12 +00003313 QualType NewTemplateId
Douglas Gregor3f220e82009-08-06 16:20:37 +00003314 = TransformType(QualType(TemplateId, 0));
3315 if (NewTemplateId.isNull())
3316 return QualType();
Mike Stump25cf7602009-09-09 15:08:12 +00003317
Douglas Gregor3f220e82009-08-06 16:20:37 +00003318 if (NNS == T->getQualifier() &&
3319 NewTemplateId == QualType(TemplateId, 0))
3320 return QualType(T, 0);
Mike Stump25cf7602009-09-09 15:08:12 +00003321
Douglas Gregor3f220e82009-08-06 16:20:37 +00003322 return getDerived().RebuildTypenameType(NNS, NewTemplateId);
3323 }
Mike Stump25cf7602009-09-09 15:08:12 +00003324
Douglas Gregor3f220e82009-08-06 16:20:37 +00003325 return getDerived().RebuildTypenameType(NNS, T->getIdentifier());
3326}
3327
3328/// \brief Rebuilds a type within the context of the current instantiation.
3329///
Mike Stump25cf7602009-09-09 15:08:12 +00003330/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor3f220e82009-08-06 16:20:37 +00003331/// a class template (or class template partial specialization) that was parsed
Mike Stump25cf7602009-09-09 15:08:12 +00003332/// and constructed before we entered the scope of the class template (or
Douglas Gregor3f220e82009-08-06 16:20:37 +00003333/// partial specialization thereof). This routine will rebuild that type now
3334/// that we have entered the declarator's scope, which may produce different
3335/// canonical types, e.g.,
3336///
3337/// \code
3338/// template<typename T>
3339/// struct X {
3340/// typedef T* pointer;
3341/// pointer data();
3342/// };
3343///
3344/// template<typename T>
3345/// typename X<T>::pointer X<T>::data() { ... }
3346/// \endcode
3347///
3348/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
3349/// since we do not know that we can look into X<T> when we parsed the type.
3350/// This function will rebuild the type, performing the lookup of "pointer"
3351/// in X<T> and returning a QualifiedNameType whose canonical type is the same
3352/// as the canonical type of T*, allowing the return types of the out-of-line
3353/// definition and the declaration to match.
3354QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
3355 DeclarationName Name) {
3356 if (T.isNull() || !T->isDependentType())
3357 return T;
Mike Stump25cf7602009-09-09 15:08:12 +00003358
Douglas Gregor3f220e82009-08-06 16:20:37 +00003359 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
3360 return Rebuilder.TransformType(T);
Benjamin Kramer3e2ac772009-08-11 22:33:06 +00003361}