blob: 09431650f26d9930036000caf6d51d5c2137bf31 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
252 } else if (SS.isSet()) {
253 // This nested-name-specifier occurs after another nested-name-specifier,
254 // so long into the context associated with the prior nested-name-specifier.
255 LookupCtx = computeDeclContext(SS, EnteringContext);
256 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
John McCallf7a1a742009-11-24 19:00:30 +0000258 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000259 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000260 return;
261 }
262
263 bool ObjectTypeSearchedInScope = false;
264 if (LookupCtx) {
265 // Perform "qualified" name lookup into the declaration context we
266 // computed, which is either the type of the base of a member access
267 // expression or the declaration context associated with a prior
268 // nested-name-specifier.
269 LookupQualifiedName(Found, LookupCtx);
270
271 if (!ObjectType.isNull() && Found.empty()) {
272 // C++ [basic.lookup.classref]p1:
273 // In a class member access expression (5.2.5), if the . or -> token is
274 // immediately followed by an identifier followed by a <, the
275 // identifier must be looked up to determine whether the < is the
276 // beginning of a template argument list (14.2) or a less-than operator.
277 // The identifier is first looked up in the class of the object
278 // expression. If the identifier is not found, it is then looked up in
279 // the context of the entire postfix-expression and shall name a class
280 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000281 if (S) LookupName(Found, S);
282 ObjectTypeSearchedInScope = true;
283 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000284 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000285 // We cannot look into a dependent object type or nested nme
286 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000287 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000288 return;
289 } else {
290 // Perform unqualified name lookup in the current scope.
291 LookupName(Found, S);
292 }
293
Douglas Gregor2e933882010-01-12 17:06:20 +0000294 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000295 // If we did not find any names, attempt to correct any typos.
296 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000297 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000298 false, CTC_CXXCasts)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000299 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000300 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000301 if (LookupCtx)
302 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
303 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000304 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000305 Found.getLookupName().getAsString());
306 else
307 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
308 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000309 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000310 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000311 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
312 Diag(Template->getLocation(), diag::note_previous_decl)
313 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000314 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000315 } else {
316 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000317 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000318 }
319 }
320
Douglas Gregor312eadb2011-04-24 05:37:28 +0000321 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000322 if (Found.empty()) {
323 if (isDependent)
324 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000325 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000326 }
John McCallf7a1a742009-11-24 19:00:30 +0000327
328 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
329 // C++ [basic.lookup.classref]p1:
330 // [...] If the lookup in the class of the object expression finds a
331 // template, the name is also looked up in the context of the entire
332 // postfix-expression and [...]
333 //
334 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
335 LookupOrdinaryName);
336 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000337 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000338
John McCallf7a1a742009-11-24 19:00:30 +0000339 if (FoundOuter.empty()) {
340 // - if the name is not found, the name found in the class of the
341 // object expression is used, otherwise
342 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
343 // - if the name is found in the context of the entire
344 // postfix-expression and does not name a class template, the name
345 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000346 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000347 // - if the name found is a class template, it must refer to the same
348 // entity as the one found in the class of the object expression,
349 // otherwise the program is ill-formed.
350 if (!Found.isSingleResult() ||
351 Found.getFoundDecl()->getCanonicalDecl()
352 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000353 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000354 diag::ext_nested_name_member_ref_lookup_ambiguous)
355 << Found.getLookupName()
356 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000357 Diag(Found.getRepresentativeDecl()->getLocation(),
358 diag::note_ambig_member_ref_object_type)
359 << ObjectType;
360 Diag(FoundOuter.getFoundDecl()->getLocation(),
361 diag::note_ambig_member_ref_scope);
362
363 // Recover by taking the template that we found in the object
364 // expression's type.
365 }
366 }
367 }
368}
369
John McCall2f841ba2009-12-02 03:53:29 +0000370/// ActOnDependentIdExpression - Handle a dependent id-expression that
371/// was just parsed. This is only possible with an explicit scope
372/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000373ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000374Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000375 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000376 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000377 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000378 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000379
John McCall2f841ba2009-12-02 03:53:29 +0000380 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000381 isa<CXXMethodDecl>(DC) &&
382 cast<CXXMethodDecl>(DC)->isInstance()) {
383 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000384
John McCallf7a1a742009-11-24 19:00:30 +0000385 // Since the 'this' expression is synthesized, we don't need to
386 // perform the double-lookup check.
387 NamedDecl *FirstQualifierInScope = 0;
388
John McCallaa81e162009-12-01 22:10:20 +0000389 return Owned(CXXDependentScopeMemberExpr::Create(Context,
390 /*This*/ 0, ThisType,
391 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000392 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000393 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000394 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000396 TemplateArgs));
397 }
398
Abramo Bagnara25777432010-08-11 22:01:17 +0000399 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000400}
401
John McCall60d7b3a2010-08-24 06:29:42 +0000402ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000403Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000405 const TemplateArgumentListInfo *TemplateArgs) {
406 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000407 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000408 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000409 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000410}
411
Douglas Gregor72c3f312008-12-05 18:15:24 +0000412/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
413/// that the template parameter 'PrevDecl' is being shadowed by a new
414/// declaration at location Loc. Returns true to indicate that this is
415/// an error, and false otherwise.
416bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000417 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000418
419 // Microsoft Visual C++ permits template parameters to be shadowed.
420 if (getLangOptions().Microsoft)
421 return false;
422
423 // C++ [temp.local]p4:
424 // A template-parameter shall not be redeclared within its
425 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000426 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000427 << cast<NamedDecl>(PrevDecl)->getDeclName();
428 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
429 return true;
430}
431
Douglas Gregor2943aed2009-03-03 04:44:36 +0000432/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000433/// the parameter D to reference the templated declaration and return a pointer
434/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000435TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
436 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
437 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000438 return Temp;
439 }
440 return 0;
441}
442
Douglas Gregorba68eca2011-01-05 17:40:24 +0000443ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
444 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000445 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000446 "Only template template arguments can be pack expansions here");
447 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
448 "Template template argument pack expansion without packs");
449 ParsedTemplateArgument Result(*this);
450 Result.EllipsisLoc = EllipsisLoc;
451 return Result;
452}
453
Douglas Gregor788cd062009-11-11 01:00:40 +0000454static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
455 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000456
Douglas Gregor788cd062009-11-11 01:00:40 +0000457 switch (Arg.getKind()) {
458 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000459 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000460 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000461 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000462 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000463 return TemplateArgumentLoc(TemplateArgument(T), DI);
464 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000465
Douglas Gregor788cd062009-11-11 01:00:40 +0000466 case ParsedTemplateArgument::NonType: {
467 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
468 return TemplateArgumentLoc(TemplateArgument(E), E);
469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000470
Douglas Gregor788cd062009-11-11 01:00:40 +0000471 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000472 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000473 TemplateArgument TArg;
474 if (Arg.getEllipsisLoc().isValid())
475 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
476 else
477 TArg = Template;
478 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000479 Arg.getScopeSpec().getWithLocInContext(
480 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000481 Arg.getLocation(),
482 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000483 }
484 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000486 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 return TemplateArgumentLoc();
488}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000489
Douglas Gregor788cd062009-11-11 01:00:40 +0000490/// \brief Translates template arguments as provided by the parser
491/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000492void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
493 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000495 TemplateArgs.addArgument(translateTemplateArgument(*this,
496 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000497}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000498
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// ActOnTypeParameter - Called when a C++ template type parameter
500/// (e.g., "typename T") has been parsed. Typename specifies whether
501/// the keyword "typename" was used to declare the type parameter
502/// (otherwise, "class" was used), and KeyLoc is the location of the
503/// "class" or "typename" keyword. ParamName is the name of the
504/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000505/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// If the type parameter has a default argument, it will be added
507/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000508Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
509 SourceLocation EllipsisLoc,
510 SourceLocation KeyLoc,
511 IdentifierInfo *ParamName,
512 SourceLocation ParamNameLoc,
513 unsigned Depth, unsigned Position,
514 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000515 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 assert(S->isTemplateParamScope() &&
517 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 bool Invalid = false;
519
520 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000521 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000522 LookupOrdinaryName,
523 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000524 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000526 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000527 }
528
Douglas Gregorddc29e12009-02-06 22:42:48 +0000529 SourceLocation Loc = ParamNameLoc;
530 if (!ParamName)
531 Loc = KeyLoc;
532
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000534 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000535 KeyLoc, Loc, Depth, Position, ParamName,
536 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000537 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000538 if (Invalid)
539 Param->setInvalidDecl();
540
541 if (ParamName) {
542 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000543 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000544 IdResolver.AddDecl(Param);
545 }
546
Douglas Gregor61c4d282011-01-05 15:48:55 +0000547 // C++0x [temp.param]p9:
548 // A default template-argument may be specified for any kind of
549 // template-parameter that is not a template parameter pack.
550 if (DefaultArg && Ellipsis) {
551 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
552 DefaultArg = ParsedType();
553 }
554
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000555 // Handle the default argument, if provided.
556 if (DefaultArg) {
557 TypeSourceInfo *DefaultTInfo;
558 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000561
Douglas Gregor6f526752010-12-16 08:48:57 +0000562 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000563 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000564 UPPC_DefaultArgument))
565 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 // Check the template argument itself.
568 if (CheckTemplateArgument(Param, DefaultTInfo)) {
569 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000570 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000571 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000572
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000573 Param->setDefaultArgument(DefaultTInfo, false);
574 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000575
John McCalld226f652010-08-21 09:40:31 +0000576 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000577}
578
Douglas Gregor2943aed2009-03-03 04:44:36 +0000579/// \brief Check that the type of a non-type template parameter is
580/// well-formed.
581///
582/// \returns the (possibly-promoted) parameter type if valid;
583/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000584QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000585Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000586 // We don't allow variably-modified types as the type of non-type template
587 // parameters.
588 if (T->isVariablyModifiedType()) {
589 Diag(Loc, diag::err_variably_modified_nontype_template_param)
590 << T;
591 return QualType();
592 }
593
Douglas Gregor2943aed2009-03-03 04:44:36 +0000594 // C++ [temp.param]p4:
595 //
596 // A non-type template-parameter shall have one of the following
597 // (optionally cv-qualified) types:
598 //
599 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000600 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000601 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000602 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000603 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000604 T->isReferenceType() ||
605 // -- pointer to member.
606 T->isMemberPointerType() ||
607 // If T is a dependent type, we can't do the check now, so we
608 // assume that it is well-formed.
609 T->isDependentType())
610 return T;
611 // C++ [temp.param]p8:
612 //
613 // A non-type template-parameter of type "array of T" or
614 // "function returning T" is adjusted to be of type "pointer to
615 // T" or "pointer to function returning T", respectively.
616 else if (T->isArrayType())
617 // FIXME: Keep the type prior to promotion?
618 return Context.getArrayDecayedType(T);
619 else if (T->isFunctionType())
620 // FIXME: Keep the type prior to promotion?
621 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000622
Douglas Gregor2943aed2009-03-03 04:44:36 +0000623 Diag(Loc, diag::err_template_nontype_parm_bad_type)
624 << T;
625
626 return QualType();
627}
628
John McCalld226f652010-08-21 09:40:31 +0000629Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
630 unsigned Depth,
631 unsigned Position,
632 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000633 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000634 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
635 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000636
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000637 assert(S->isTemplateParamScope() &&
638 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000639 bool Invalid = false;
640
641 IdentifierInfo *ParamName = D.getIdentifier();
642 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000643 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000644 LookupOrdinaryName,
645 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000646 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000647 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000648 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 }
650
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000651 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
652 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000653 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000654 Invalid = true;
655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656
Douglas Gregor10738d32010-12-23 23:51:58 +0000657 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000659 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000660 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000661 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000662 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000663 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000664 Param->setAccess(AS_public);
665
Douglas Gregor72c3f312008-12-05 18:15:24 +0000666 if (Invalid)
667 Param->setInvalidDecl();
668
669 if (D.getIdentifier()) {
670 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000671 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000672 IdResolver.AddDecl(Param);
673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000674
Douglas Gregor61c4d282011-01-05 15:48:55 +0000675 // C++0x [temp.param]p9:
676 // A default template-argument may be specified for any kind of
677 // template-parameter that is not a template parameter pack.
678 if (Default && IsParameterPack) {
679 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
680 Default = 0;
681 }
682
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000683 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000684 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000685 // Check for unexpanded parameter packs.
686 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
687 return Param;
688
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000689 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000690 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
691 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000692 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000693 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 }
John Wiegley429bb272011-04-08 18:41:53 +0000695 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000696
John McCall9ae2f072010-08-23 23:25:46 +0000697 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000699
John McCalld226f652010-08-21 09:40:31 +0000700 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000701}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000702
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000703/// ActOnTemplateTemplateParameter - Called when a C++ template template
704/// parameter (e.g. T in template <template <typename> class T> class array)
705/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000706Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
707 SourceLocation TmpLoc,
708 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000709 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000710 IdentifierInfo *Name,
711 SourceLocation NameLoc,
712 unsigned Depth,
713 unsigned Position,
714 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000715 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000716 assert(S->isTemplateParamScope() &&
717 "Template template parameter not in template parameter scope!");
718
719 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000720 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000722 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000723 NameLoc.isInvalid()? TmpLoc : NameLoc,
724 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000725 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000726 Param->setAccess(AS_public);
727
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000728 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000729 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000731 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 IdResolver.AddDecl(Param);
733 }
734
Douglas Gregor6f526752010-12-16 08:48:57 +0000735 if (Params->size() == 0) {
736 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
737 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
738 Param->setInvalidDecl();
739 }
740
Douglas Gregor61c4d282011-01-05 15:48:55 +0000741 // C++0x [temp.param]p9:
742 // A default template-argument may be specified for any kind of
743 // template-parameter that is not a template parameter pack.
744 if (IsParameterPack && !Default.isInvalid()) {
745 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
746 Default = ParsedTemplateArgument();
747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000748
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000749 if (!Default.isInvalid()) {
750 // Check only that we have a template template argument. We don't want to
751 // try to check well-formedness now, because our template template parameter
752 // might have dependent types in its template parameters, which we wouldn't
753 // be able to match now.
754 //
755 // If none of the template template parameter's template arguments mention
756 // other template parameters, we could actually perform more checking here.
757 // However, it isn't worth doing.
758 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
759 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
760 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
761 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000762 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000764
Douglas Gregor6f526752010-12-16 08:48:57 +0000765 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000767 DefaultArg.getArgument().getAsTemplate(),
768 UPPC_DefaultArgument))
769 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000770
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000771 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
John McCalld226f652010-08-21 09:40:31 +0000774 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000775}
776
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000777/// ActOnTemplateParameterList - Builds a TemplateParameterList that
778/// contains the template parameters in Params/NumParams.
779Sema::TemplateParamsTy *
780Sema::ActOnTemplateParameterList(unsigned Depth,
781 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000782 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000783 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000784 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000785 SourceLocation RAngleLoc) {
786 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000787 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000788
Douglas Gregorddc29e12009-02-06 22:42:48 +0000789 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000791 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000793
John McCallb6217662010-03-15 10:12:16 +0000794static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
795 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000796 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000797}
798
John McCallf312b1e2010-08-26 23:41:50 +0000799DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000800Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000801 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000802 IdentifierInfo *Name, SourceLocation NameLoc,
803 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000804 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000805 AccessSpecifier AS,
806 unsigned NumOuterTemplateParamLists,
807 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000808 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000809 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000810 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000811 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000812
813 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000814 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000815 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000816
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000817 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
818 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000819
820 // There is no such thing as an unnamed class template.
821 if (!Name) {
822 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000823 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000824 }
825
826 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000827 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000829 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 if (SS.isNotEmpty() && !SS.isInvalid()) {
831 SemanticContext = computeDeclContext(SS, true);
832 if (!SemanticContext) {
833 // FIXME: Produce a reasonable diagnostic here
834 return true;
835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
John McCall77bb1aa2010-05-01 00:40:08 +0000837 if (RequireCompleteDeclContext(SS, SemanticContext))
838 return true;
839
John McCalla24dc2e2009-11-17 02:14:36 +0000840 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 } else {
842 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000843 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregor57265e32010-04-12 16:00:01 +0000846 if (Previous.isAmbiguous())
847 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848
Douglas Gregorddc29e12009-02-06 22:42:48 +0000849 NamedDecl *PrevDecl = 0;
850 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000851 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000852
Douglas Gregorddc29e12009-02-06 22:42:48 +0000853 // If there is a previous declaration with the same name, check
854 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000855 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000856 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000857
858 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000859 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000860 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000861 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000862 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
863 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000864 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000865 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
866 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
867 PrevClassTemplate
868 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
869 ->getSpecializedTemplate();
870 }
871 }
872
John McCall65c49462009-12-18 11:25:59 +0000873 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000874 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875 // [...] When looking for a prior declaration of a class or a function
876 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000877 // function is neither a qualified name nor a template-id, scopes outside
878 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000879 if (!SS.isSet()) {
880 DeclContext *OutermostContext = CurContext;
881 while (!OutermostContext->isFileContext())
882 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000883
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000884 if (PrevDecl &&
885 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
886 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
887 SemanticContext = PrevDecl->getDeclContext();
888 } else {
889 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000890 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000891 // declaration.
892 PrevDecl = PrevClassTemplate = 0;
893 SemanticContext = OutermostContext;
894 }
John McCalle129d442009-12-17 23:21:11 +0000895 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000896
John McCalle129d442009-12-17 23:21:11 +0000897 if (CurContext->isDependentContext()) {
898 // If this is a dependent context, we don't want to link the friend
899 // class template to the template in scope, because that would perform
900 // checking of the template parameter lists that can't be performed
901 // until the outer context is instantiated.
902 PrevDecl = PrevClassTemplate = 0;
903 }
904 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
905 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000906
Douglas Gregorddc29e12009-02-06 22:42:48 +0000907 if (PrevClassTemplate) {
908 // Ensure that the template parameter lists are compatible.
909 if (!TemplateParameterListsAreEqual(TemplateParams,
910 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000911 /*Complain=*/true,
912 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000913 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914
915 // C++ [temp.class]p4:
916 // In a redeclaration, partial specialization, explicit
917 // specialization or explicit instantiation of a class template,
918 // the class-key shall agree in kind with the original class
919 // template declaration (7.1.5.3).
920 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000921 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000923 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000924 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000925 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000926 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 }
928
Douglas Gregorddc29e12009-02-06 22:42:48 +0000929 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000930 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000931 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000932 Diag(NameLoc, diag::err_redefinition) << Name;
933 Diag(Def->getLocation(), diag::note_previous_definition);
934 // FIXME: Would it make sense to try to "forget" the previous
935 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000936 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938 }
939 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
940 // Maybe we will complain about the shadowed template parameter.
941 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
942 // Just pretend that we didn't see the previous declaration.
943 PrevDecl = 0;
944 } else if (PrevDecl) {
945 // C++ [temp]p5:
946 // A class template shall not have the same name as any other
947 // template, class, function, object, enumeration, enumerator,
948 // namespace, or type in the same scope (3.3), except as specified
949 // in (14.5.4).
950 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
951 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000952 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000953 }
954
Douglas Gregord684b002009-02-10 19:49:53 +0000955 // Check the template parameter list of this declaration, possibly
956 // merging in the template parameter list from the previous class
957 // template declaration.
958 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000959 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000960 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000961 SemanticContext->isRecord() &&
962 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000963 ? TPC_ClassTemplateMember
964 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000965 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregor57265e32010-04-12 16:00:01 +0000967 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000968 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000969 // template out-of-line.
970 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
971 !(TUK == TUK_Friend && CurContext->isDependentContext()))
972 Diag(NameLoc, diag::err_member_def_does_not_match)
973 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000974 }
975
Mike Stump1eb44332009-09-09 15:08:12 +0000976 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000977 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000978 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000979 PrevClassTemplate->getTemplatedDecl() : 0,
980 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000981 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000982 if (NumOuterTemplateParamLists > 0)
983 NewClass->setTemplateParameterListsInfo(Context,
984 NumOuterTemplateParamLists,
985 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000986
987 ClassTemplateDecl *NewTemplate
988 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
989 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000990 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000991 NewClass->setDescribedClassTemplate(NewTemplate);
992
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000993 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000994 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000995 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000996 assert(T->isDependentType() && "Class template type is not dependent?");
997 (void)T;
998
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000999 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001000 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001001 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001002 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1003 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001004
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001005 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001006 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001007 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009 // Set the lexical context of these templates
1010 NewClass->setLexicalDeclContext(CurContext);
1011 NewTemplate->setLexicalDeclContext(CurContext);
1012
John McCall0f434ec2009-07-31 02:45:11 +00001013 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001014 NewClass->startDefinition();
1015
1016 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001017 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001018
John McCall05b23ea2009-09-14 21:59:20 +00001019 if (TUK != TUK_Friend)
1020 PushOnScopeChains(NewTemplate, S);
1021 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001022 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001023 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 NewClass->setAccess(PrevClassTemplate->getAccess());
1025 }
John McCall05b23ea2009-09-14 21:59:20 +00001026
Douglas Gregord85bea22009-09-26 06:47:28 +00001027 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1028 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029
John McCall05b23ea2009-09-14 21:59:20 +00001030 // Friend templates are visible in fairly strange ways.
1031 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001032 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001033 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1034 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1035 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001036 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001037 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038
Douglas Gregord85bea22009-09-26 06:47:28 +00001039 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1040 NewClass->getLocation(),
1041 NewTemplate,
1042 /*FIXME:*/NewClass->getLocation());
1043 Friend->setAccess(AS_public);
1044 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001045 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001046
Douglas Gregord684b002009-02-10 19:49:53 +00001047 if (Invalid) {
1048 NewTemplate->setInvalidDecl();
1049 NewClass->setInvalidDecl();
1050 }
John McCalld226f652010-08-21 09:40:31 +00001051 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001052}
1053
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001054/// \brief Diagnose the presence of a default template argument on a
1055/// template parameter, which is ill-formed in certain contexts.
1056///
1057/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001059 Sema::TemplateParamListContext TPC,
1060 SourceLocation ParamLoc,
1061 SourceRange DefArgRange) {
1062 switch (TPC) {
1063 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001064 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001065 return false;
1066
1067 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001068 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001070 // A default template-argument shall not be specified in a
1071 // function template declaration or a function template
1072 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001073 // If a friend function template declaration specifies a default
1074 // template-argument, that declaration shall be a definition and shall be
1075 // the only declaration of the function template in the translation unit.
1076 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001077 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001079 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 << DefArgRange;
1081 return false;
1082
1083 case Sema::TPC_ClassTemplateMember:
1084 // C++0x [temp.param]p9:
1085 // A default template-argument shall not be specified in the
1086 // template-parameter-lists of the definition of a member of a
1087 // class template that appears outside of the member's class.
1088 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1089 << DefArgRange;
1090 return true;
1091
1092 case Sema::TPC_FriendFunctionTemplate:
1093 // C++ [temp.param]p9:
1094 // A default template-argument shall not be specified in a
1095 // friend template declaration.
1096 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1097 << DefArgRange;
1098 return true;
1099
1100 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1101 // for friend function templates if there is only a single
1102 // declaration (and it is a definition). Strange!
1103 }
1104
1105 return false;
1106}
1107
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108/// \brief Check for unexpanded parameter packs within the template parameters
1109/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001110static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1111 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001112 TemplateParameterList *Params = TTP->getTemplateParameters();
1113 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1114 NamedDecl *P = Params->getParam(I);
1115 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001116 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001117 NTTP->getTypeSourceInfo(),
1118 Sema::UPPC_NonTypeTemplateParameterType))
1119 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001120
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001121 continue;
1122 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001123
1124 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001125 = dyn_cast<TemplateTemplateParmDecl>(P))
1126 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1127 return true;
1128 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001129
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001130 return false;
1131}
1132
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \brief Checks the validity of a template parameter list, possibly
1134/// considering the template parameter list from a previous
1135/// declaration.
1136///
1137/// If an "old" template parameter list is provided, it must be
1138/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1139/// template parameter list.
1140///
1141/// \param NewParams Template parameter list for a new template
1142/// declaration. This template parameter list will be updated with any
1143/// default arguments that are carried through from the previous
1144/// template parameter list.
1145///
1146/// \param OldParams If provided, template parameter list from a
1147/// previous declaration of the same template. Default template
1148/// arguments will be merged from the old template parameter list to
1149/// the new template parameter list.
1150///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001151/// \param TPC Describes the context in which we are checking the given
1152/// template parameter list.
1153///
Douglas Gregord684b002009-02-10 19:49:53 +00001154/// \returns true if an error occurred, false otherwise.
1155bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001156 TemplateParameterList *OldParams,
1157 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001158 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregord684b002009-02-10 19:49:53 +00001160 // C++ [temp.param]p10:
1161 // The set of default template-arguments available for use with a
1162 // template declaration or definition is obtained by merging the
1163 // default arguments from the definition (if in scope) and all
1164 // declarations in scope in the same way default function
1165 // arguments are (8.3.6).
1166 bool SawDefaultArgument = false;
1167 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001168
Anders Carlsson49d25572009-06-12 23:20:15 +00001169 bool SawParameterPack = false;
1170 SourceLocation ParameterPackLoc;
1171
Mike Stump1a35fde2009-02-11 23:03:27 +00001172 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001173 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001174 if (OldParams)
1175 OldParam = OldParams->begin();
1176
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001177 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001178 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1179 NewParamEnd = NewParams->end();
1180 NewParam != NewParamEnd; ++NewParam) {
1181 // Variables used to diagnose redundant default arguments
1182 bool RedundantDefaultArg = false;
1183 SourceLocation OldDefaultLoc;
1184 SourceLocation NewDefaultLoc;
1185
1186 // Variables used to diagnose missing default arguments
1187 bool MissingDefaultArg = false;
1188
Anders Carlsson49d25572009-06-12 23:20:15 +00001189 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001190 // If a template parameter of a primary class template or alias template
1191 // is a template parameter pack, it shall be the last template parameter.
1192 if (SawParameterPack &&
1193 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001194 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001195 diag::err_template_param_pack_must_be_last_template_parameter);
1196 Invalid = true;
1197 }
1198
Douglas Gregord684b002009-02-10 19:49:53 +00001199 if (TemplateTypeParmDecl *NewTypeParm
1200 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001201 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001202 if (NewTypeParm->hasDefaultArgument() &&
1203 DiagnoseDefaultTemplateArgument(*this, TPC,
1204 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001205 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001206 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001207 NewTypeParm->removeDefaultArgument();
1208
1209 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001210 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001211 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Anders Carlsson49d25572009-06-12 23:20:15 +00001213 if (NewTypeParm->isParameterPack()) {
1214 assert(!NewTypeParm->hasDefaultArgument() &&
1215 "Parameter packs can't have a default argument!");
1216 SawParameterPack = true;
1217 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001218 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001219 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001220 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1221 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1222 SawDefaultArgument = true;
1223 RedundantDefaultArg = true;
1224 PreviousDefaultArgLoc = NewDefaultLoc;
1225 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1226 // Merge the default argument from the old declaration to the
1227 // new declaration.
1228 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001229 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001230 true);
1231 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1232 } else if (NewTypeParm->hasDefaultArgument()) {
1233 SawDefaultArgument = true;
1234 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1235 } else if (SawDefaultArgument)
1236 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001237 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001238 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001239 // Check for unexpanded parameter packs.
1240 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001241 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001242 UPPC_NonTypeTemplateParameterType)) {
1243 Invalid = true;
1244 continue;
1245 }
1246
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001247 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001248 if (NewNonTypeParm->hasDefaultArgument() &&
1249 DiagnoseDefaultTemplateArgument(*this, TPC,
1250 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001251 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001252 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001253 }
1254
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001255 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001256 NonTypeTemplateParmDecl *OldNonTypeParm
1257 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001258 if (NewNonTypeParm->isParameterPack()) {
1259 assert(!NewNonTypeParm->hasDefaultArgument() &&
1260 "Parameter packs can't have a default argument!");
1261 SawParameterPack = true;
1262 ParameterPackLoc = NewNonTypeParm->getLocation();
1263 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001264 NewNonTypeParm->hasDefaultArgument()) {
1265 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1266 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1267 SawDefaultArgument = true;
1268 RedundantDefaultArg = true;
1269 PreviousDefaultArgLoc = NewDefaultLoc;
1270 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1271 // Merge the default argument from the old declaration to the
1272 // new declaration.
1273 SawDefaultArgument = true;
1274 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001275 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001276 // parameter.
1277 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001278 OldNonTypeParm->getDefaultArgument(),
1279 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001280 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1281 } else if (NewNonTypeParm->hasDefaultArgument()) {
1282 SawDefaultArgument = true;
1283 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1284 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001285 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001286 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001287 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001288 TemplateTemplateParmDecl *NewTemplateParm
1289 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001290
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001291 // Check for unexpanded parameter packs, recursively.
1292 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1293 Invalid = true;
1294 continue;
1295 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001296
1297 if (NewTemplateParm->hasDefaultArgument() &&
1298 DiagnoseDefaultTemplateArgument(*this, TPC,
1299 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001300 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001301 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001302
1303 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001304 TemplateTemplateParmDecl *OldTemplateParm
1305 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001306 if (NewTemplateParm->isParameterPack()) {
1307 assert(!NewTemplateParm->hasDefaultArgument() &&
1308 "Parameter packs can't have a default argument!");
1309 SawParameterPack = true;
1310 ParameterPackLoc = NewTemplateParm->getLocation();
1311 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001312 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001313 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1314 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001315 SawDefaultArgument = true;
1316 RedundantDefaultArg = true;
1317 PreviousDefaultArgLoc = NewDefaultLoc;
1318 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1319 // Merge the default argument from the old declaration to the
1320 // new declaration.
1321 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001322 // FIXME: We need to create a new kind of "default argument" expression
1323 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001324 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001325 OldTemplateParm->getDefaultArgument(),
1326 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001327 PreviousDefaultArgLoc
1328 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001329 } else if (NewTemplateParm->hasDefaultArgument()) {
1330 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001331 PreviousDefaultArgLoc
1332 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001333 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001334 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001335 }
1336
1337 if (RedundantDefaultArg) {
1338 // C++ [temp.param]p12:
1339 // A template-parameter shall not be given default arguments
1340 // by two different declarations in the same scope.
1341 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1342 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1343 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001344 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001345 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001346 // If a template-parameter of a class template has a default
1347 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001348 // have a default template-argument supplied or be a template parameter
1349 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001350 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001351 diag::err_template_param_default_arg_missing);
1352 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1353 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001354 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001355 }
1356
1357 // If we have an old template parameter list that we're merging
1358 // in, move on to the next parameter.
1359 if (OldParams)
1360 ++OldParam;
1361 }
1362
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001363 // We were missing some default arguments at the end of the list, so remove
1364 // all of the default arguments.
1365 if (RemoveDefaultArguments) {
1366 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1367 NewParamEnd = NewParams->end();
1368 NewParam != NewParamEnd; ++NewParam) {
1369 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1370 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001371 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001372 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1373 NTTP->removeDefaultArgument();
1374 else
1375 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1376 }
1377 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001378
Douglas Gregord684b002009-02-10 19:49:53 +00001379 return Invalid;
1380}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001381
John McCall4e2cbb22010-10-20 05:44:58 +00001382namespace {
1383
1384/// A class which looks for a use of a certain level of template
1385/// parameter.
1386struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1387 typedef RecursiveASTVisitor<DependencyChecker> super;
1388
1389 unsigned Depth;
1390 bool Match;
1391
1392 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1393 NamedDecl *ND = Params->getParam(0);
1394 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1395 Depth = PD->getDepth();
1396 } else if (NonTypeTemplateParmDecl *PD =
1397 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1398 Depth = PD->getDepth();
1399 } else {
1400 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1401 }
1402 }
1403
1404 bool Matches(unsigned ParmDepth) {
1405 if (ParmDepth >= Depth) {
1406 Match = true;
1407 return true;
1408 }
1409 return false;
1410 }
1411
1412 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1413 return !Matches(T->getDepth());
1414 }
1415
1416 bool TraverseTemplateName(TemplateName N) {
1417 if (TemplateTemplateParmDecl *PD =
1418 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1419 if (Matches(PD->getDepth())) return false;
1420 return super::TraverseTemplateName(N);
1421 }
1422
1423 bool VisitDeclRefExpr(DeclRefExpr *E) {
1424 if (NonTypeTemplateParmDecl *PD =
1425 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1426 if (PD->getDepth() == Depth) {
1427 Match = true;
1428 return false;
1429 }
1430 }
1431 return super::VisitDeclRefExpr(E);
1432 }
1433};
1434}
1435
1436/// Determines whether a template-id depends on the given parameter
1437/// list.
1438static bool
1439DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1440 TemplateParameterList *Params) {
1441 DependencyChecker Checker(Params);
1442 Checker.TraverseType(QualType(TemplateId, 0));
1443 return Checker.Match;
1444}
1445
Mike Stump1eb44332009-09-09 15:08:12 +00001446/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001447/// specifier, returning the template parameter list that applies to the
1448/// name.
1449///
1450/// \param DeclStartLoc the start of the declaration that has a scope
1451/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001452///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001453/// \param SS the scope specifier that will be matched to the given template
1454/// parameter lists. This scope specifier precedes a qualified name that is
1455/// being declared.
1456///
1457/// \param ParamLists the template parameter lists, from the outermost to the
1458/// innermost template parameter lists.
1459///
1460/// \param NumParamLists the number of template parameter lists in ParamLists.
1461///
John McCall77e8b112010-04-13 20:37:33 +00001462/// \param IsFriend Whether to apply the slightly different rules for
1463/// matching template parameters to scope specifiers in friend
1464/// declarations.
1465///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001466/// \param IsExplicitSpecialization will be set true if the entity being
1467/// declared is an explicit specialization, false otherwise.
1468///
Mike Stump1eb44332009-09-09 15:08:12 +00001469/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001470/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001471/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001472/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001473/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001474/// itself a template).
1475TemplateParameterList *
1476Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1477 const CXXScopeSpec &SS,
1478 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001479 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001480 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001481 bool &IsExplicitSpecialization,
1482 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001483 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001484
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001485 // Find the template-ids that occur within the nested-name-specifier. These
1486 // template-ids will match up with the template parameter lists.
1487 llvm::SmallVector<const TemplateSpecializationType *, 4>
1488 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001489 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1490 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001491 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1492 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001493 const Type *T = NNS->getAsType();
1494 if (!T) break;
1495
1496 // C++0x [temp.expl.spec]p17:
1497 // A member or a member template may be nested within many
1498 // enclosing class templates. In an explicit specialization for
1499 // such a member, the member declaration shall be preceded by a
1500 // template<> for each enclosing class template that is
1501 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001502 //
1503 // Following the existing practice of GNU and EDG, we allow a typedef of a
1504 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001505 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1506 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001507
Mike Stump1eb44332009-09-09 15:08:12 +00001508 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001509 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1511 if (!Template)
1512 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Ted Kremenek6217b802009-07-29 21:53:49 +00001514 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 ClassTemplateSpecializationDecl *SpecDecl
1516 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1517 // If the nested name specifier refers to an explicit specialization,
1518 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001519 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1520 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001522 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001525 TemplateIdsInSpecifier.push_back(SpecType);
1526 }
1527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001529 // Reverse the list of template-ids in the scope specifier, so that we can
1530 // more easily match up the template-ids and the template parameter lists.
1531 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001533 SourceLocation FirstTemplateLoc = DeclStartLoc;
1534 if (NumParamLists)
1535 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001537 // Match the template-ids found in the specifier to the template parameter
1538 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001539 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001540 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001541 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1542 const TemplateSpecializationType *TemplateId
1543 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001544 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001545
1546 // In friend declarations we can have template-ids which don't
1547 // depend on the corresponding template parameter lists. But
1548 // assume that empty parameter lists are supposed to match this
1549 // template-id.
1550 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1551 if (!DependentTemplateId ||
1552 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1553 continue;
1554 }
1555
1556 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001557 // We have a template-id without a corresponding template parameter
1558 // list.
John McCall77e8b112010-04-13 20:37:33 +00001559
1560 // ...which is fine if this is a friend declaration.
1561 if (IsFriend) {
1562 IsExplicitSpecialization = true;
1563 break;
1564 }
1565
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001566 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001567 // FIXME: the location information here isn't great.
1568 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001569 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001570 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001571 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001572 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001573 } else {
1574 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1575 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001576 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001577 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001578 }
1579 return 0;
1580 }
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001582 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001583 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001584 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001585
John McCall31f17ec2010-04-27 00:57:59 +00001586 // Are there cases in (e.g.) friends where this won't match?
1587 if (const InjectedClassNameType *Injected
1588 = TemplateId->getAs<InjectedClassNameType>()) {
1589 CXXRecordDecl *Record = Injected->getDecl();
1590 if (ClassTemplatePartialSpecializationDecl *Partial =
1591 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1592 ExpectedTemplateParams = Partial->getTemplateParameters();
1593 else
1594 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1595 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001596 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001597
John McCall31f17ec2010-04-27 00:57:59 +00001598 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001599 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001600 ExpectedTemplateParams,
1601 true, TPL_TemplateMatch);
1602
John McCall4e2cbb22010-10-20 05:44:58 +00001603 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1604 TPC_ClassTemplateMember);
1605 } else if (ParamLists[ParamIdx]->size() > 0)
1606 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001607 diag::err_template_param_list_matches_nontemplate)
1608 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001609 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001610 else
1611 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001612
1613 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001614 }
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001616 // If there were at least as many template-ids as there were template
1617 // parameter lists, then there are no template parameter lists remaining for
1618 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001619 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001620 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001622 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001623 if (ParamIdx != NumParamLists - 1) {
1624 while (ParamIdx < NumParamLists - 1) {
1625 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1626 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001627 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1628 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001629 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1630 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001631
1632 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1633 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1634 diag::note_explicit_template_spec_does_not_need_header)
1635 << ExplicitSpecializationsInSpecifier.back();
1636 ExplicitSpecializationsInSpecifier.pop_back();
1637 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001638
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001639 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001640 // means that the resulting template declaration can't be instantiated
1641 // properly (we'll end up with dependent nodes when we shouldn't).
1642 if (!isExplicitSpecHeader)
1643 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001644
John McCall4e2cbb22010-10-20 05:44:58 +00001645 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001646 }
1647 }
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001649 // Return the last template parameter list, which corresponds to the
1650 // entity being declared.
1651 return ParamLists[NumParamLists - 1];
1652}
1653
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001654void Sema::NoteAllFoundTemplates(TemplateName Name) {
1655 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1656 Diag(Template->getLocation(), diag::note_template_declared_here)
1657 << (isa<FunctionTemplateDecl>(Template)? 0
1658 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001659 : isa<TypeAliasTemplateDecl>(Template)? 2
1660 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001661 << Template->getDeclName();
1662 return;
1663 }
1664
1665 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1666 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1667 IEnd = OST->end();
1668 I != IEnd; ++I)
1669 Diag((*I)->getLocation(), diag::note_template_declared_here)
1670 << 0 << (*I)->getDeclName();
1671
1672 return;
1673 }
1674}
1675
1676
Douglas Gregor7532dc62009-03-30 22:58:21 +00001677QualType Sema::CheckTemplateIdType(TemplateName Name,
1678 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001679 TemplateArgumentListInfo &TemplateArgs) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001680 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1681 if (DTN && DTN->isIdentifier())
1682 // When building a template-id where the template-name is dependent,
1683 // assume the template is a type template. Either our assumption is
1684 // correct, or the code is ill-formed and will be diagnosed when the
1685 // dependent name is substituted.
1686 return Context.getDependentTemplateSpecializationType(ETK_None,
1687 DTN->getQualifier(),
1688 DTN->getIdentifier(),
1689 TemplateArgs);
1690
Douglas Gregor7532dc62009-03-30 22:58:21 +00001691 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001692 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1693 // We might have a substituted template template parameter pack. If so,
1694 // build a template specialization type for it.
1695 if (Name.getAsSubstTemplateTemplateParmPack())
1696 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001697
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001698 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1699 << Name;
1700 NoteAllFoundTemplates(Name);
1701 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001702 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001703
Douglas Gregor40808ce2009-03-09 23:48:35 +00001704 // Check that the template argument list is well-formed for this
1705 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001706 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001707 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001708 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001709 return QualType();
1710
Douglas Gregor910f8002010-11-07 23:05:16 +00001711 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001712 "Converted template argument list is too short!");
1713
1714 QualType CanonType;
1715
Richard Smith3e4c6c42011-05-05 21:57:07 +00001716 if (TypeAliasTemplateDecl *AliasTemplate
1717 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1718 // Find the canonical type for this type alias template specialization.
1719 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1720 if (Pattern->isInvalidDecl())
1721 return QualType();
1722
1723 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1724 Converted.data(), Converted.size());
1725
1726 // Only substitute for the innermost template argument list.
1727 MultiLevelTemplateArgumentList TemplateArgLists;
1728 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
1729
1730 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1731 CanonType = SubstType(Pattern->getUnderlyingType(),
1732 TemplateArgLists, AliasTemplate->getLocation(),
1733 AliasTemplate->getDeclName());
1734 if (CanonType.isNull())
1735 return QualType();
1736 } else if (Name.isDependent() ||
1737 TemplateSpecializationType::anyDependentTemplateArguments(
1738 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001739 // This class template specialization is a dependent
1740 // type. Therefore, its canonical type is another class template
1741 // specialization type that contains all of the converted
1742 // arguments in canonical form. This ensures that, e.g., A<T> and
1743 // A<T, T> have identical types when A is declared as:
1744 //
1745 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001746 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001747 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001748 Converted.data(),
1749 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Douglas Gregor1275ae02009-07-28 23:00:59 +00001751 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001752 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001753 // In the future, we need to teach getTemplateSpecializationType to only
1754 // build the canonical type and return that to us.
1755 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001756
1757 // This might work out to be a current instantiation, in which
1758 // case the canonical type needs to be the InjectedClassNameType.
1759 //
1760 // TODO: in theory this could be a simple hashtable lookup; most
1761 // changes to CurContext don't change the set of current
1762 // instantiations.
1763 if (isa<ClassTemplateDecl>(Template)) {
1764 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1765 // If we get out to a namespace, we're done.
1766 if (Ctx->isFileContext()) break;
1767
1768 // If this isn't a record, keep looking.
1769 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1770 if (!Record) continue;
1771
1772 // Look for one of the two cases with InjectedClassNameTypes
1773 // and check whether it's the same template.
1774 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1775 !Record->getDescribedClassTemplate())
1776 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001777
John McCall31f17ec2010-04-27 00:57:59 +00001778 // Fetch the injected class name type and check whether its
1779 // injected type is equal to the type we just built.
1780 QualType ICNT = Context.getTypeDeclType(Record);
1781 QualType Injected = cast<InjectedClassNameType>(ICNT)
1782 ->getInjectedSpecializationType();
1783
1784 if (CanonType != Injected->getCanonicalTypeInternal())
1785 continue;
1786
1787 // If so, the canonical type of this TST is the injected
1788 // class name type of the record we just found.
1789 assert(ICNT.isCanonical());
1790 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001791 break;
1792 }
1793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001795 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001796 // Find the class template specialization declaration that
1797 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001798 void *InsertPos = 0;
1799 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001800 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001801 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001802 if (!Decl) {
1803 // This is the first time we have referenced this class template
1804 // specialization. Create the canonical declaration and add it to
1805 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001806 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001807 ClassTemplate->getTemplatedDecl()->getTagKind(),
1808 ClassTemplate->getDeclContext(),
1809 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001810 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001811 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001812 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001813 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001814 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001815 Decl->setLexicalDeclContext(CurContext);
1816 }
1817
1818 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001819 assert(isa<RecordType>(CanonType) &&
1820 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Douglas Gregor40808ce2009-03-09 23:48:35 +00001823 // Build the fully-sugared type for this class template
1824 // specialization, which refers back to the class template
1825 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001826 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001827}
1828
John McCallf312b1e2010-08-26 23:41:50 +00001829TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001830Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1831 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001832 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001833 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001834 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001835 if (SS.isInvalid())
1836 return true;
1837
Douglas Gregor7532dc62009-03-30 22:58:21 +00001838 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001839
Douglas Gregor40808ce2009-03-09 23:48:35 +00001840 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001841 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001842 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001843
Douglas Gregora88f09f2011-02-28 17:23:35 +00001844 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1845 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1846 DTN->getQualifier(),
1847 DTN->getIdentifier(),
1848 TemplateArgs);
1849
1850 // Build type-source information.
1851 TypeLocBuilder TLB;
1852 DependentTemplateSpecializationTypeLoc SpecTL
1853 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001854 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001855 SpecTL.setNameLoc(TemplateLoc);
1856 SpecTL.setLAngleLoc(LAngleLoc);
1857 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001858 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001859 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1860 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1861 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1862 }
1863
John McCalld5532b62009-11-23 01:53:49 +00001864 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001865 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001866
1867 if (Result.isNull())
1868 return true;
1869
Douglas Gregor059101f2011-03-02 00:47:37 +00001870 // Build type-source information.
1871 TypeLocBuilder TLB;
1872 TemplateSpecializationTypeLoc SpecTL
1873 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1874 SpecTL.setTemplateNameLoc(TemplateLoc);
1875 SpecTL.setLAngleLoc(LAngleLoc);
1876 SpecTL.setRAngleLoc(RAngleLoc);
1877 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1878 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001879
Douglas Gregor059101f2011-03-02 00:47:37 +00001880 if (SS.isNotEmpty()) {
1881 // Create an elaborated-type-specifier containing the nested-name-specifier.
1882 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1883 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1884 ElabTL.setKeywordLoc(SourceLocation());
1885 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1886 }
1887
1888 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001889}
John McCallf1bbbb42009-09-04 01:14:41 +00001890
Douglas Gregor059101f2011-03-02 00:47:37 +00001891TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001892 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001893 SourceLocation TagLoc,
1894 CXXScopeSpec &SS,
1895 TemplateTy TemplateD,
1896 SourceLocation TemplateLoc,
1897 SourceLocation LAngleLoc,
1898 ASTTemplateArgsPtr TemplateArgsIn,
1899 SourceLocation RAngleLoc) {
1900 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1901
1902 // Translate the parser's template argument list in our AST format.
1903 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1904 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1905
1906 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001907 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001908 ElaboratedTypeKeyword Keyword
1909 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Douglas Gregor059101f2011-03-02 00:47:37 +00001911 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1912 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1913 DTN->getQualifier(),
1914 DTN->getIdentifier(),
1915 TemplateArgs);
1916
1917 // Build type-source information.
1918 TypeLocBuilder TLB;
1919 DependentTemplateSpecializationTypeLoc SpecTL
1920 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1921 SpecTL.setKeywordLoc(TagLoc);
1922 SpecTL.setNameLoc(TemplateLoc);
1923 SpecTL.setLAngleLoc(LAngleLoc);
1924 SpecTL.setRAngleLoc(RAngleLoc);
1925 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1926 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1927 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1928 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1929 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00001930
1931 if (TypeAliasTemplateDecl *TAT =
1932 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
1933 // C++0x [dcl.type.elab]p2:
1934 // If the identifier resolves to a typedef-name or the simple-template-id
1935 // resolves to an alias template specialization, the
1936 // elaborated-type-specifier is ill-formed.
1937 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
1938 Diag(TAT->getLocation(), diag::note_declared_at);
1939 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001940
1941 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1942 if (Result.isNull())
1943 return TypeResult();
1944
1945 // Check the tag kind
1946 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001947 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001948
John McCall6b2becf2009-09-08 17:47:29 +00001949 IdentifierInfo *Id = D->getIdentifier();
1950 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001951
John McCall6b2becf2009-09-08 17:47:29 +00001952 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1953 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001954 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001955 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001956 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001957 }
1958 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001959
1960 // Provide source-location information for the template specialization.
1961 TypeLocBuilder TLB;
1962 TemplateSpecializationTypeLoc SpecTL
1963 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1964 SpecTL.setTemplateNameLoc(TemplateLoc);
1965 SpecTL.setLAngleLoc(LAngleLoc);
1966 SpecTL.setRAngleLoc(RAngleLoc);
1967 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1968 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001969
Douglas Gregor059101f2011-03-02 00:47:37 +00001970 // Construct an elaborated type containing the nested-name-specifier (if any)
1971 // and keyword.
1972 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1973 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1974 ElabTL.setKeywordLoc(TagLoc);
1975 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1976 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001977}
1978
John McCall60d7b3a2010-08-24 06:29:42 +00001979ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001980 LookupResult &R,
1981 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001982 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001983 // FIXME: Can we do any checking at this point? I guess we could check the
1984 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001985 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001986 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001987 // foo<int> could identify a single function unambiguously
1988 // This approach does NOT work, since f<int>(1);
1989 // gets resolved prior to resorting to overload resolution
1990 // i.e., template<class T> void f(double);
1991 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001992
1993 // These should be filtered out by our callers.
1994 assert(!R.empty() && "empty lookup results when building templateid");
1995 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1996
John McCallc373d482010-01-27 01:50:18 +00001997 // We don't want lookup warnings at this point.
1998 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001999
John McCallf7a1a742009-11-24 19:00:30 +00002000 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002001 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002002 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002003 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002004 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002005 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002006
2007 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002008}
2009
John McCallf7a1a742009-11-24 19:00:30 +00002010// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002011ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002012Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002013 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002014 const TemplateArgumentListInfo &TemplateArgs) {
2015 DeclContext *DC;
2016 if (!(DC = computeDeclContext(SS, false)) ||
2017 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002018 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002019 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002021 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002022 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002023 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2024 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002025
John McCallf7a1a742009-11-24 19:00:30 +00002026 if (R.isAmbiguous())
2027 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002028
John McCallf7a1a742009-11-24 19:00:30 +00002029 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002030 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2031 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002032 return ExprError();
2033 }
2034
2035 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002036 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2037 << (NestedNameSpecifier*) SS.getScopeRep()
2038 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002039 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2040 return ExprError();
2041 }
2042
2043 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002044}
2045
Douglas Gregorc45c2322009-03-31 00:43:58 +00002046/// \brief Form a dependent template name.
2047///
2048/// This action forms a dependent template name given the template
2049/// name and its (presumably dependent) scope specifier. For
2050/// example, given "MetaFun::template apply", the scope specifier \p
2051/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2052/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002053TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002054 SourceLocation TemplateKWLoc,
2055 CXXScopeSpec &SS,
2056 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002057 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002058 bool EnteringContext,
2059 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002060 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2061 !getLangOptions().CPlusPlus0x)
2062 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002063 << FixItHint::CreateRemoval(TemplateKWLoc);
2064
Douglas Gregor0707bc52010-01-19 16:01:07 +00002065 DeclContext *LookupCtx = 0;
2066 if (SS.isSet())
2067 LookupCtx = computeDeclContext(SS, EnteringContext);
2068 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002069 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002070 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002071 // C++0x [temp.names]p5:
2072 // If a name prefixed by the keyword template is not the name of
2073 // a template, the program is ill-formed. [Note: the keyword
2074 // template may not be applied to non-template members of class
2075 // templates. -end note ] [ Note: as is the case with the
2076 // typename prefix, the template prefix is allowed in cases
2077 // where it is not strictly necessary; i.e., when the
2078 // nested-name-specifier or the expression on the left of the ->
2079 // or . is not dependent on a template-parameter, or the use
2080 // does not appear in the scope of a template. -end note]
2081 //
2082 // Note: C++03 was more strict here, because it banned the use of
2083 // the "template" keyword prior to a template-name that was not a
2084 // dependent name. C++ DR468 relaxed this requirement (the
2085 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002086 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002087 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002088 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2089 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002090 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002091 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2092 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002093 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2094 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002095 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002096 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002097 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002098 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002099 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002100 << Name.getSourceRange()
2101 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002102 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002103 } else {
2104 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002105 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002106 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002107 }
2108
Mike Stump1eb44332009-09-09 15:08:12 +00002109 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002110 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002111
Douglas Gregor014e88d2009-11-03 23:16:33 +00002112 switch (Name.getKind()) {
2113 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002114 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002115 Name.Identifier));
2116 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002117
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002118 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002119 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002120 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002121 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002122
2123 case UnqualifiedId::IK_LiteralOperatorId:
2124 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2125
Douglas Gregor014e88d2009-11-03 23:16:33 +00002126 default:
2127 break;
2128 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002129
2130 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002131 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002132 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002133 << Name.getSourceRange()
2134 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002135 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002136}
2137
Mike Stump1eb44332009-09-09 15:08:12 +00002138bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002139 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002140 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002141 const TemplateArgument &Arg = AL.getArgument();
2142
Anders Carlsson436b1562009-06-13 00:33:33 +00002143 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002144 switch(Arg.getKind()) {
2145 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002146 // C++ [temp.arg.type]p1:
2147 // A template-argument for a template-parameter which is a
2148 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002149 break;
2150 case TemplateArgument::Template: {
2151 // We have a template type parameter but the template argument
2152 // is a template without any arguments.
2153 SourceRange SR = AL.getSourceRange();
2154 TemplateName Name = Arg.getAsTemplate();
2155 Diag(SR.getBegin(), diag::err_template_missing_args)
2156 << Name << SR;
2157 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2158 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002159
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002160 return true;
2161 }
2162 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002163 // We have a template type parameter but the template argument
2164 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002165 SourceRange SR = AL.getSourceRange();
2166 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002167 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Anders Carlsson436b1562009-06-13 00:33:33 +00002169 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002170 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002171 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002172
John McCalla93c9342009-12-07 02:54:59 +00002173 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002174 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Anders Carlsson436b1562009-06-13 00:33:33 +00002176 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002177 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002178 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002179 return false;
2180}
2181
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002182/// \brief Substitute template arguments into the default template argument for
2183/// the given template type parameter.
2184///
2185/// \param SemaRef the semantic analysis object for which we are performing
2186/// the substitution.
2187///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002188/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002189/// for.
2190///
2191/// \param TemplateLoc the location of the template name that started the
2192/// template-id we are checking.
2193///
2194/// \param RAngleLoc the location of the right angle bracket ('>') that
2195/// terminates the template-id.
2196///
2197/// \param Param the template template parameter whose default we are
2198/// substituting into.
2199///
2200/// \param Converted the list of template arguments provided for template
2201/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002202/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002203static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002204SubstDefaultTemplateArgument(Sema &SemaRef,
2205 TemplateDecl *Template,
2206 SourceLocation TemplateLoc,
2207 SourceLocation RAngleLoc,
2208 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002209 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002210 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002211
2212 // If the argument type is dependent, instantiate it now based
2213 // on the previously-computed template arguments.
2214 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002215 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002216 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002217
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002218 MultiLevelTemplateArgumentList AllTemplateArgs
2219 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2220
2221 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002222 Template, Converted.data(),
2223 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002224 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002225
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002226 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2227 Param->getDefaultArgumentLoc(),
2228 Param->getDeclName());
2229 }
2230
2231 return ArgType;
2232}
2233
2234/// \brief Substitute template arguments into the default template argument for
2235/// the given non-type template parameter.
2236///
2237/// \param SemaRef the semantic analysis object for which we are performing
2238/// the substitution.
2239///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002240/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002241/// for.
2242///
2243/// \param TemplateLoc the location of the template name that started the
2244/// template-id we are checking.
2245///
2246/// \param RAngleLoc the location of the right angle bracket ('>') that
2247/// terminates the template-id.
2248///
Douglas Gregor788cd062009-11-11 01:00:40 +00002249/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002250/// substituting into.
2251///
2252/// \param Converted the list of template arguments provided for template
2253/// parameters that precede \p Param in the template parameter list.
2254///
2255/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002256static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002257SubstDefaultTemplateArgument(Sema &SemaRef,
2258 TemplateDecl *Template,
2259 SourceLocation TemplateLoc,
2260 SourceLocation RAngleLoc,
2261 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002262 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002263 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002264 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002266 MultiLevelTemplateArgumentList AllTemplateArgs
2267 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002268
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002269 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002270 Template, Converted.data(),
2271 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002272 SourceRange(TemplateLoc, RAngleLoc));
2273
2274 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2275}
2276
Douglas Gregor788cd062009-11-11 01:00:40 +00002277/// \brief Substitute template arguments into the default template argument for
2278/// the given template template parameter.
2279///
2280/// \param SemaRef the semantic analysis object for which we are performing
2281/// the substitution.
2282///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002283/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002284/// for.
2285///
2286/// \param TemplateLoc the location of the template name that started the
2287/// template-id we are checking.
2288///
2289/// \param RAngleLoc the location of the right angle bracket ('>') that
2290/// terminates the template-id.
2291///
2292/// \param Param the template template parameter whose default we are
2293/// substituting into.
2294///
2295/// \param Converted the list of template arguments provided for template
2296/// parameters that precede \p Param in the template parameter list.
2297///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002298/// \param QualifierLoc Will be set to the nested-name-specifier (with
2299/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002300///
Douglas Gregor788cd062009-11-11 01:00:40 +00002301/// \returns the substituted template argument, or NULL if an error occurred.
2302static TemplateName
2303SubstDefaultTemplateArgument(Sema &SemaRef,
2304 TemplateDecl *Template,
2305 SourceLocation TemplateLoc,
2306 SourceLocation RAngleLoc,
2307 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002308 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2309 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002310 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002311 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002312
Douglas Gregor788cd062009-11-11 01:00:40 +00002313 MultiLevelTemplateArgumentList AllTemplateArgs
2314 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002315
Douglas Gregor788cd062009-11-11 01:00:40 +00002316 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002317 Template, Converted.data(),
2318 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002319 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002320
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002321 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002322 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002323 if (QualifierLoc) {
2324 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2325 AllTemplateArgs);
2326 if (!QualifierLoc)
2327 return TemplateName();
2328 }
2329
Douglas Gregor1d752d72011-03-02 18:46:51 +00002330 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002331 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002332 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002333 AllTemplateArgs);
2334}
2335
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002336/// \brief If the given template parameter has a default template
2337/// argument, substitute into that default template argument and
2338/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002339TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002340Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2341 SourceLocation TemplateLoc,
2342 SourceLocation RAngleLoc,
2343 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002344 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2345 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002346 if (!TypeParm->hasDefaultArgument())
2347 return TemplateArgumentLoc();
2348
John McCalla93c9342009-12-07 02:54:59 +00002349 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002350 TemplateLoc,
2351 RAngleLoc,
2352 TypeParm,
2353 Converted);
2354 if (DI)
2355 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2356
2357 return TemplateArgumentLoc();
2358 }
2359
2360 if (NonTypeTemplateParmDecl *NonTypeParm
2361 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2362 if (!NonTypeParm->hasDefaultArgument())
2363 return TemplateArgumentLoc();
2364
John McCall60d7b3a2010-08-24 06:29:42 +00002365 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002366 TemplateLoc,
2367 RAngleLoc,
2368 NonTypeParm,
2369 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002370 if (Arg.isInvalid())
2371 return TemplateArgumentLoc();
2372
2373 Expr *ArgE = Arg.takeAs<Expr>();
2374 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2375 }
2376
2377 TemplateTemplateParmDecl *TempTempParm
2378 = cast<TemplateTemplateParmDecl>(Param);
2379 if (!TempTempParm->hasDefaultArgument())
2380 return TemplateArgumentLoc();
2381
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002382
Douglas Gregor1d752d72011-03-02 18:46:51 +00002383 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002384 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002385 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002386 RAngleLoc,
2387 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002388 Converted,
2389 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002390 if (TName.isNull())
2391 return TemplateArgumentLoc();
2392
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002393 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002394 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002395 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2396}
2397
Douglas Gregore7526412009-11-11 19:31:23 +00002398/// \brief Check that the given template argument corresponds to the given
2399/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002400///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002401/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002402/// checked.
2403///
2404/// \param Arg The template argument.
2405///
2406/// \param Template The template in which the template argument resides.
2407///
2408/// \param TemplateLoc The location of the template name for the template
2409/// whose argument list we're matching.
2410///
2411/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2412/// the template argument list.
2413///
2414/// \param ArgumentPackIndex The index into the argument pack where this
2415/// argument will be placed. Only valid if the parameter is a parameter pack.
2416///
2417/// \param Converted The checked, converted argument will be added to the
2418/// end of this small vector.
2419///
2420/// \param CTAK Describes how we arrived at this particular template argument:
2421/// explicitly written, deduced, etc.
2422///
2423/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002424bool Sema::CheckTemplateArgument(NamedDecl *Param,
2425 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002426 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002427 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002428 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002429 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002430 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002431 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002432 // Check template type parameters.
2433 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002434 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435
Douglas Gregord9e15302009-11-11 19:41:09 +00002436 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002437 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002438 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002439 // with the template arguments we've seen thus far. But if the
2440 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002441 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002442 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2443 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002445 if (NTTPType->isDependentType() &&
2446 !isa<TemplateTemplateParmDecl>(Template) &&
2447 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002448 // Do substitution on the type of the non-type template parameter.
2449 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002450 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002451 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452
2453 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002454 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002455 NTTPType = SubstType(NTTPType,
2456 MultiLevelTemplateArgumentList(TemplateArgs),
2457 NTTP->getLocation(),
2458 NTTP->getDeclName());
2459 // If that worked, check the non-type template parameter type
2460 // for validity.
2461 if (!NTTPType.isNull())
2462 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2463 NTTP->getLocation());
2464 if (NTTPType.isNull())
2465 return true;
2466 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467
Douglas Gregore7526412009-11-11 19:31:23 +00002468 switch (Arg.getArgument().getKind()) {
2469 case TemplateArgument::Null:
2470 assert(false && "Should never see a NULL template argument here");
2471 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregore7526412009-11-11 19:31:23 +00002473 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002474 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002475 ExprResult Res =
2476 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2477 Result, CTAK);
2478 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002479 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480
Douglas Gregor910f8002010-11-07 23:05:16 +00002481 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002482 break;
2483 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002484
Douglas Gregore7526412009-11-11 19:31:23 +00002485 case TemplateArgument::Declaration:
2486 case TemplateArgument::Integral:
2487 // We've already checked this template argument, so just copy
2488 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002490 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491
Douglas Gregore7526412009-11-11 19:31:23 +00002492 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002493 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002494 // We were given a template template argument. It may not be ill-formed;
2495 // see below.
2496 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002497 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2498 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002499 // We have a template argument such as \c T::template X, which we
2500 // parsed as a template template argument. However, since we now
2501 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002502 // template name into an expression.
2503
2504 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2505 Arg.getTemplateNameLoc());
2506
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002507 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002508 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002509 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002510 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002511 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002512
Douglas Gregora7fc9012011-01-05 18:58:31 +00002513 // If we parsed the template argument as a pack expansion, create a
2514 // pack expansion expression.
2515 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002516 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2517 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002518 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002520
Douglas Gregore7526412009-11-11 19:31:23 +00002521 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002522 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2523 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002524 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregor910f8002010-11-07 23:05:16 +00002526 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002527 break;
2528 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002529
Douglas Gregore7526412009-11-11 19:31:23 +00002530 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002531 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002532 // therefore cannot be a non-type template argument.
2533 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2534 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregore7526412009-11-11 19:31:23 +00002536 Diag(Param->getLocation(), diag::note_template_param_here);
2537 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002538
Douglas Gregore7526412009-11-11 19:31:23 +00002539 case TemplateArgument::Type: {
2540 // We have a non-type template parameter but the template
2541 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002542
Douglas Gregore7526412009-11-11 19:31:23 +00002543 // C++ [temp.arg]p2:
2544 // In a template-argument, an ambiguity between a type-id and
2545 // an expression is resolved to a type-id, regardless of the
2546 // form of the corresponding template-parameter.
2547 //
2548 // We warn specifically about this case, since it can be rather
2549 // confusing for users.
2550 QualType T = Arg.getArgument().getAsType();
2551 SourceRange SR = Arg.getSourceRange();
2552 if (T->isFunctionType())
2553 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2554 else
2555 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2556 Diag(Param->getLocation(), diag::note_template_param_here);
2557 return true;
2558 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559
Douglas Gregore7526412009-11-11 19:31:23 +00002560 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002561 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002562 break;
2563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564
Douglas Gregore7526412009-11-11 19:31:23 +00002565 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002566 }
2567
2568
Douglas Gregore7526412009-11-11 19:31:23 +00002569 // Check template template parameters.
2570 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002571
Douglas Gregore7526412009-11-11 19:31:23 +00002572 // Substitute into the template parameter list of the template
2573 // template parameter, since previously-supplied template arguments
2574 // may appear within the template template parameter.
2575 {
2576 // Set up a template instantiation context.
2577 LocalInstantiationScope Scope(*this);
2578 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002579 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002580 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002581
2582 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002583 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002584 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002585 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002586 MultiLevelTemplateArgumentList(TemplateArgs)));
2587 if (!TempParm)
2588 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002589 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002590
Douglas Gregore7526412009-11-11 19:31:23 +00002591 switch (Arg.getArgument().getKind()) {
2592 case TemplateArgument::Null:
2593 assert(false && "Should never see a NULL template argument here");
2594 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002595
Douglas Gregore7526412009-11-11 19:31:23 +00002596 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002597 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002598 if (CheckTemplateArgument(TempParm, Arg))
2599 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002600
Douglas Gregor910f8002010-11-07 23:05:16 +00002601 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002602 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002603
Douglas Gregore7526412009-11-11 19:31:23 +00002604 case TemplateArgument::Expression:
2605 case TemplateArgument::Type:
2606 // We have a template template parameter but the template
2607 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002608 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2609 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002610 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002611
Douglas Gregore7526412009-11-11 19:31:23 +00002612 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002613 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002614 "Declaration argument with template template parameter");
2615 break;
2616 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002617 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002618 "Integral argument with template template parameter");
2619 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002620
Douglas Gregore7526412009-11-11 19:31:23 +00002621 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002622 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002623 break;
2624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002625
Douglas Gregore7526412009-11-11 19:31:23 +00002626 return false;
2627}
2628
Douglas Gregorc15cb382009-02-09 23:23:08 +00002629/// \brief Check that the given template argument list is well-formed
2630/// for specializing the given template.
2631bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2632 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002633 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002634 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002635 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002636 TemplateParameterList *Params = Template->getTemplateParameters();
2637 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002638 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002639 bool Invalid = false;
2640
John McCalld5532b62009-11-23 01:53:49 +00002641 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2642
Mike Stump1eb44332009-09-09 15:08:12 +00002643 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002644 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002646 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002647 (NumArgs < Params->getMinRequiredArguments() &&
2648 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002649 // FIXME: point at either the first arg beyond what we can handle,
2650 // or the '>', depending on whether we have too many or too few
2651 // arguments.
2652 SourceRange Range;
2653 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002654 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002655 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2656 << (NumArgs > NumParams)
2657 << (isa<ClassTemplateDecl>(Template)? 0 :
2658 isa<FunctionTemplateDecl>(Template)? 1 :
2659 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2660 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002661 Diag(Template->getLocation(), diag::note_template_decl_here)
2662 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002663 Invalid = true;
2664 }
Mike Stump1eb44332009-09-09 15:08:12 +00002665
2666 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002667 // [...] The type and form of each template-argument specified in
2668 // a template-id shall match the type and form specified for the
2669 // corresponding parameter declared by the template in its
2670 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002671 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002672 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2673 TemplateParameterList::iterator Param = Params->begin(),
2674 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002675 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002676 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002677 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002678 if (ArgIdx > NumArgs && PartialTemplateArgs)
2679 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Douglas Gregorf35f8282009-11-11 21:54:23 +00002681 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002682 // If we have an expanded parameter pack, make sure we don't have too
2683 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002685 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002687 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2688 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2689 << true
2690 << (isa<ClassTemplateDecl>(Template)? 0 :
2691 isa<FunctionTemplateDecl>(Template)? 1 :
2692 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2693 << Template;
2694 Diag(Template->getLocation(), diag::note_template_decl_here)
2695 << Params->getSourceRange();
2696 return true;
2697 }
2698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699
Douglas Gregorf35f8282009-11-11 21:54:23 +00002700 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002701 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2702 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002703 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002704 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705
Douglas Gregor14be16b2010-12-20 16:57:52 +00002706 if ((*Param)->isTemplateParameterPack()) {
2707 // The template parameter was a template parameter pack, so take the
2708 // deduced argument and place it on the argument pack. Note that we
2709 // stay on the same template parameter so that we can deduce more
2710 // arguments.
2711 ArgumentPack.push_back(Converted.back());
2712 Converted.pop_back();
2713 } else {
2714 // Move to the next template parameter.
2715 ++Param;
2716 }
2717 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002718 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002720
2721 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002722 // arguments, just break out now and we'll fill in the argument pack below.
2723 if ((*Param)->isTemplateParameterPack())
2724 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725
Douglas Gregorf35f8282009-11-11 21:54:23 +00002726 // We have a default template argument that we will use.
2727 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002728
Douglas Gregorf35f8282009-11-11 21:54:23 +00002729 // Retrieve the default template argument from the template
2730 // parameter. For each kind of template parameter, we substitute the
2731 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002733 // the default argument.
2734 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2735 if (!TTP->hasDefaultArgument()) {
2736 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2737 break;
2738 }
2739
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002741 Template,
2742 TemplateLoc,
2743 RAngleLoc,
2744 TTP,
2745 Converted);
2746 if (!ArgType)
2747 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregorf35f8282009-11-11 21:54:23 +00002749 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2750 ArgType);
2751 } else if (NonTypeTemplateParmDecl *NTTP
2752 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2753 if (!NTTP->hasDefaultArgument()) {
2754 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2755 break;
2756 }
2757
John McCall60d7b3a2010-08-24 06:29:42 +00002758 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759 TemplateLoc,
2760 RAngleLoc,
2761 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002762 Converted);
2763 if (E.isInvalid())
2764 return true;
2765
2766 Expr *Ex = E.takeAs<Expr>();
2767 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2768 } else {
2769 TemplateTemplateParmDecl *TempParm
2770 = cast<TemplateTemplateParmDecl>(*Param);
2771
2772 if (!TempParm->hasDefaultArgument()) {
2773 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2774 break;
2775 }
2776
Douglas Gregor1d752d72011-03-02 18:46:51 +00002777 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002778 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779 TemplateLoc,
2780 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002781 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002782 Converted,
2783 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002784 if (Name.isNull())
2785 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002787 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2788 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790
Douglas Gregorf35f8282009-11-11 21:54:23 +00002791 // Introduce an instantiation record that describes where we are using
2792 // the default template argument.
2793 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002794 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795 SourceRange(TemplateLoc, RAngleLoc));
2796
Douglas Gregorf35f8282009-11-11 21:54:23 +00002797 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002798 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002799 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002800 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002801
Douglas Gregor67714232011-03-03 02:41:12 +00002802 // Core issue 150 (assumed resolution): if this is a template template
2803 // parameter, keep track of the default template arguments from the
2804 // template definition.
2805 if (isTemplateTemplateParameter)
2806 TemplateArgs.addArgument(Arg);
2807
Douglas Gregor14be16b2010-12-20 16:57:52 +00002808 // Move to the next template parameter and argument.
2809 ++Param;
2810 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002811 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812
Douglas Gregor14be16b2010-12-20 16:57:52 +00002813 // Form argument packs for each of the parameter packs remaining.
2814 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002815 // If we're checking a partial list of template arguments, don't fill
2816 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002817
2818 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002819 if (PartialTemplateArgs && ArgumentPack.empty()) {
2820 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002821 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002822 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002823 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002824 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2825 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002826 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002827 ArgumentPack.clear();
2828 }
2829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830
Douglas Gregor14be16b2010-12-20 16:57:52 +00002831 ++Param;
2832 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002833
Douglas Gregorc15cb382009-02-09 23:23:08 +00002834 return Invalid;
2835}
2836
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002837namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838 class UnnamedLocalNoLinkageFinder
2839 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002840 {
2841 Sema &S;
2842 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002844 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002846 public:
2847 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2848
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849 bool Visit(QualType T) {
2850 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002851 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002852
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002853#define TYPE(Class, Parent) \
2854 bool Visit##Class##Type(const Class##Type *);
2855#define ABSTRACT_TYPE(Class, Parent) \
2856 bool Visit##Class##Type(const Class##Type *) { return false; }
2857#define NON_CANONICAL_TYPE(Class, Parent) \
2858 bool Visit##Class##Type(const Class##Type *) { return false; }
2859#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002860
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002861 bool VisitTagDecl(const TagDecl *Tag);
2862 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2863 };
2864}
2865
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002866bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002867 return false;
2868}
2869
2870bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2871 return Visit(T->getElementType());
2872}
2873
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002875 return Visit(T->getPointeeType());
2876}
2877
2878bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002879 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002880 return Visit(T->getPointeeType());
2881}
2882
2883bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002884 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002885 return Visit(T->getPointeeType());
2886}
2887
2888bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002889 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002890 return Visit(T->getPointeeType());
2891}
2892
2893bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002894 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002895 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2896}
2897
2898bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002899 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002900 return Visit(T->getElementType());
2901}
2902
2903bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002904 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002905 return Visit(T->getElementType());
2906}
2907
2908bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002909 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002910 return Visit(T->getElementType());
2911}
2912
2913bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002914 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002915 return Visit(T->getElementType());
2916}
2917
2918bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002919 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002920 return Visit(T->getElementType());
2921}
2922
2923bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2924 return Visit(T->getElementType());
2925}
2926
2927bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2928 return Visit(T->getElementType());
2929}
2930
2931bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2932 const FunctionProtoType* T) {
2933 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002934 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002935 A != AEnd; ++A) {
2936 if (Visit(*A))
2937 return true;
2938 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002939
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002940 return Visit(T->getResultType());
2941}
2942
2943bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2944 const FunctionNoProtoType* T) {
2945 return Visit(T->getResultType());
2946}
2947
2948bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2949 const UnresolvedUsingType*) {
2950 return false;
2951}
2952
2953bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2954 return false;
2955}
2956
2957bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2958 return Visit(T->getUnderlyingType());
2959}
2960
2961bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2962 return false;
2963}
2964
Richard Smith34b41d92011-02-20 03:19:35 +00002965bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2966 return Visit(T->getDeducedType());
2967}
2968
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002969bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2970 return VisitTagDecl(T->getDecl());
2971}
2972
2973bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2974 return VisitTagDecl(T->getDecl());
2975}
2976
2977bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2978 const TemplateTypeParmType*) {
2979 return false;
2980}
2981
Douglas Gregorc3069d62011-01-14 02:55:32 +00002982bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2983 const SubstTemplateTypeParmPackType *) {
2984 return false;
2985}
2986
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002987bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2988 const TemplateSpecializationType*) {
2989 return false;
2990}
2991
2992bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2993 const InjectedClassNameType* T) {
2994 return VisitTagDecl(T->getDecl());
2995}
2996
2997bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2998 const DependentNameType* T) {
2999 return VisitNestedNameSpecifier(T->getQualifier());
3000}
3001
3002bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3003 const DependentTemplateSpecializationType* T) {
3004 return VisitNestedNameSpecifier(T->getQualifier());
3005}
3006
Douglas Gregor7536dd52010-12-20 02:24:11 +00003007bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3008 const PackExpansionType* T) {
3009 return Visit(T->getPattern());
3010}
3011
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003012bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3013 return false;
3014}
3015
3016bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3017 const ObjCInterfaceType *) {
3018 return false;
3019}
3020
3021bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3022 const ObjCObjectPointerType *) {
3023 return false;
3024}
3025
3026bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3027 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3028 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3029 << S.Context.getTypeDeclType(Tag) << SR;
3030 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 }
3032
Richard Smith162e1c12011-04-15 14:24:37 +00003033 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003034 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3035 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3036 return true;
3037 }
3038
3039 return false;
3040}
3041
3042bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3043 NestedNameSpecifier *NNS) {
3044 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3045 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003046
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003047 switch (NNS->getKind()) {
3048 case NestedNameSpecifier::Identifier:
3049 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003050 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003051 case NestedNameSpecifier::Global:
3052 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003053
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003054 case NestedNameSpecifier::TypeSpec:
3055 case NestedNameSpecifier::TypeSpecWithTemplate:
3056 return Visit(QualType(NNS->getAsType(), 0));
3057 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003058 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003059}
3060
3061
Douglas Gregorc15cb382009-02-09 23:23:08 +00003062/// \brief Check a template argument against its corresponding
3063/// template type parameter.
3064///
3065/// This routine implements the semantics of C++ [temp.arg.type]. It
3066/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003067bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003068 TypeSourceInfo *ArgInfo) {
3069 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003070 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003071 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003072
3073 if (Arg->isVariablyModifiedType()) {
3074 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003075 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003076 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003077 }
3078
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003079 // C++03 [temp.arg.type]p2:
3080 // A local type, a type with no linkage, an unnamed type or a type
3081 // compounded from any of these types shall not be used as a
3082 // template-argument for a template type-parameter.
3083 //
3084 // C++0x allows these, and even in C++03 we allow them as an extension with
3085 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003086 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003087 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3088 (void)Finder.Visit(Context.getCanonicalType(Arg));
3089 }
3090
Douglas Gregorc15cb382009-02-09 23:23:08 +00003091 return false;
3092}
3093
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003094/// \brief Checks whether the given template argument is the address
3095/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003097CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3098 NonTypeTemplateParmDecl *Param,
3099 QualType ParamType,
3100 Expr *ArgIn,
3101 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003102 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003103 Expr *Arg = ArgIn;
3104 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003105
3106 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003107 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003108 Arg = Cast->getSubExpr();
3109
3110 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003111 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003112 // A template-argument for a non-type, non-template
3113 // template-parameter shall be one of: [...]
3114 //
3115 // -- the address of an object or function with external
3116 // linkage, including function templates and function
3117 // template-ids but excluding non-static class members,
3118 // expressed as & id-expression where the & is optional if
3119 // the name refers to a function or array, or if the
3120 // corresponding template-parameter is a reference; or
3121 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003123 // In C++98/03 mode, give an extension warning on any extra parentheses.
3124 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3125 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003126 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003127 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003128 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003129 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003130 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003131 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003132 }
3133
3134 Arg = Parens->getSubExpr();
3135 }
3136
Douglas Gregorb7a09262010-04-01 18:32:35 +00003137 bool AddressTaken = false;
3138 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003139 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003140 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003141 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3142 // Very common in Microsoft COM headers.
3143 if (S.getLangOptions().Microsoft &&
3144 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3145 Converted = TemplateArgument(ArgIn);
3146 return false;
3147 }
3148
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003149 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003150 AddressTaken = true;
3151 AddrOpLoc = UnOp->getOperatorLoc();
3152 }
Francois Picheta343a412011-04-29 09:08:14 +00003153 } else {
3154 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3155 Converted = TemplateArgument(ArgIn);
3156 return false;
3157 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003158 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003159 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003160 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003161 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3162 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003163 S.Diag(Param->getLocation(), diag::note_template_param_here);
3164 return true;
3165 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003166
3167 // Stop checking the precise nature of the argument if it is value dependent,
3168 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003169 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003170 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003171 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003172 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003173
Douglas Gregorb7a09262010-04-01 18:32:35 +00003174 if (!isa<ValueDecl>(DRE->getDecl())) {
3175 S.Diag(Arg->getSourceRange().getBegin(),
3176 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003177 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003178 S.Diag(Param->getLocation(), diag::note_template_param_here);
3179 return true;
3180 }
3181
3182 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003183
3184 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003185 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3186 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003187 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003188 S.Diag(Param->getLocation(), diag::note_template_param_here);
3189 return true;
3190 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003191
3192 // Cannot refer to non-static member functions
3193 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003194 if (!Method->isStatic()) {
3195 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003196 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003197 S.Diag(Param->getLocation(), diag::note_template_param_here);
3198 return true;
3199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003201 // Functions must have external linkage.
3202 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003203 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003204 S.Diag(Arg->getSourceRange().getBegin(),
3205 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003206 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003207 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003208 << true;
3209 return true;
3210 }
3211
3212 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003213 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003214
Douglas Gregorb7a09262010-04-01 18:32:35 +00003215 // If the template parameter has pointer type, the function decays.
3216 if (ParamType->isPointerType() && !AddressTaken)
3217 ArgType = S.Context.getPointerType(Func->getType());
3218 else if (AddressTaken && ParamType->isReferenceType()) {
3219 // If we originally had an address-of operator, but the
3220 // parameter has reference type, complain and (if things look
3221 // like they will work) drop the address-of operator.
3222 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3223 ParamType.getNonReferenceType())) {
3224 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3225 << ParamType;
3226 S.Diag(Param->getLocation(), diag::note_template_param_here);
3227 return true;
3228 }
3229
3230 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3231 << ParamType
3232 << FixItHint::CreateRemoval(AddrOpLoc);
3233 S.Diag(Param->getLocation(), diag::note_template_param_here);
3234
3235 ArgType = Func->getType();
3236 }
3237 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003238 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003239 S.Diag(Arg->getSourceRange().getBegin(),
3240 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003241 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003242 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003243 << true;
3244 return true;
3245 }
3246
Douglas Gregorb7a09262010-04-01 18:32:35 +00003247 // A value of reference type is not an object.
3248 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003249 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003250 diag::err_template_arg_reference_var)
3251 << Var->getType() << Arg->getSourceRange();
3252 S.Diag(Param->getLocation(), diag::note_template_param_here);
3253 return true;
3254 }
3255
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003256 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003257 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003258
3259 // If the template parameter has pointer type, we must have taken
3260 // the address of this object.
3261 if (ParamType->isReferenceType()) {
3262 if (AddressTaken) {
3263 // If we originally had an address-of operator, but the
3264 // parameter has reference type, complain and (if things look
3265 // like they will work) drop the address-of operator.
3266 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3267 ParamType.getNonReferenceType())) {
3268 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3269 << ParamType;
3270 S.Diag(Param->getLocation(), diag::note_template_param_here);
3271 return true;
3272 }
3273
3274 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3275 << ParamType
3276 << FixItHint::CreateRemoval(AddrOpLoc);
3277 S.Diag(Param->getLocation(), diag::note_template_param_here);
3278
3279 ArgType = Var->getType();
3280 }
3281 } else if (!AddressTaken && ParamType->isPointerType()) {
3282 if (Var->getType()->isArrayType()) {
3283 // Array-to-pointer decay.
3284 ArgType = S.Context.getArrayDecayedType(Var->getType());
3285 } else {
3286 // If the template parameter has pointer type but the address of
3287 // this object was not taken, complain and (possibly) recover by
3288 // taking the address of the entity.
3289 ArgType = S.Context.getPointerType(Var->getType());
3290 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3291 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3292 << ParamType;
3293 S.Diag(Param->getLocation(), diag::note_template_param_here);
3294 return true;
3295 }
3296
3297 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3298 << ParamType
3299 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3300
3301 S.Diag(Param->getLocation(), diag::note_template_param_here);
3302 }
3303 }
3304 } else {
3305 // We found something else, but we don't know specifically what it is.
3306 S.Diag(Arg->getSourceRange().getBegin(),
3307 diag::err_template_arg_not_object_or_func)
3308 << Arg->getSourceRange();
3309 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3310 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003311 }
Mike Stump1eb44332009-09-09 15:08:12 +00003312
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003313 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003314 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003315 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003316 // For pointer-to-object types, qualification conversions are
3317 // permitted.
3318 } else {
3319 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3320 if (!ParamRef->getPointeeType()->isFunctionType()) {
3321 // C++ [temp.arg.nontype]p5b3:
3322 // For a non-type template-parameter of type reference to
3323 // object, no conversions apply. The type referred to by the
3324 // reference may be more cv-qualified than the (otherwise
3325 // identical) type of the template- argument. The
3326 // template-parameter is bound directly to the
3327 // template-argument, which shall be an lvalue.
3328
3329 // FIXME: Other qualifiers?
3330 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3331 unsigned ArgQuals = ArgType.getCVRQualifiers();
3332
3333 if ((ParamQuals | ArgQuals) != ParamQuals) {
3334 S.Diag(Arg->getSourceRange().getBegin(),
3335 diag::err_template_arg_ref_bind_ignores_quals)
3336 << ParamType << Arg->getType()
3337 << Arg->getSourceRange();
3338 S.Diag(Param->getLocation(), diag::note_template_param_here);
3339 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003340 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003341 }
3342 }
3343
3344 // At this point, the template argument refers to an object or
3345 // function with external linkage. We now need to check whether the
3346 // argument and parameter types are compatible.
3347 if (!S.Context.hasSameUnqualifiedType(ArgType,
3348 ParamType.getNonReferenceType())) {
3349 // We can't perform this conversion or binding.
3350 if (ParamType->isReferenceType())
3351 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3352 << ParamType << Arg->getType() << Arg->getSourceRange();
3353 else
3354 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3355 << Arg->getType() << ParamType << Arg->getSourceRange();
3356 S.Diag(Param->getLocation(), diag::note_template_param_here);
3357 return true;
3358 }
3359 }
3360
3361 // Create the template argument.
3362 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003363 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003364 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003365}
3366
3367/// \brief Checks whether the given template argument is a pointer to
3368/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003369bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003370 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003371 bool Invalid = false;
3372
3373 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003374 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003375 Arg = Cast->getSubExpr();
3376
3377 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003378 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003379 // A template-argument for a non-type, non-template
3380 // template-parameter shall be one of: [...]
3381 //
3382 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003383 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003384
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003385 // In C++98/03 mode, give an extension warning on any extra parentheses.
3386 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3387 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003389 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003390 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003391 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003392 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003393 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003394 }
3395
3396 Arg = Parens->getSubExpr();
3397 }
3398
Douglas Gregorcaddba02009-11-12 18:38:13 +00003399 // A pointer-to-member constant written &Class::member.
3400 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003401 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003402 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3403 if (DRE && !DRE->getQualifier())
3404 DRE = 0;
3405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003406 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003407 // A constant of pointer-to-member type.
3408 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3409 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3410 if (VD->getType()->isMemberPointerType()) {
3411 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003412 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003413 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3414 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003415 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003416 else
3417 Converted = TemplateArgument(VD->getCanonicalDecl());
3418 return Invalid;
3419 }
3420 }
3421 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003422
Douglas Gregorcaddba02009-11-12 18:38:13 +00003423 DRE = 0;
3424 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003425
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003426 if (!DRE)
3427 return Diag(Arg->getSourceRange().getBegin(),
3428 diag::err_template_arg_not_pointer_to_member_form)
3429 << Arg->getSourceRange();
3430
3431 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3432 assert((isa<FieldDecl>(DRE->getDecl()) ||
3433 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3434 "Only non-static member pointers can make it here");
3435
3436 // Okay: this is the address of a non-static member, and therefore
3437 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003438 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003439 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003440 else
3441 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003442 return Invalid;
3443 }
3444
3445 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003446 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003447 diag::err_template_arg_not_pointer_to_member_form)
3448 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003449 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003450 diag::note_template_arg_refers_here);
3451 return true;
3452}
3453
Douglas Gregorc15cb382009-02-09 23:23:08 +00003454/// \brief Check a template argument against its corresponding
3455/// non-type template parameter.
3456///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003457/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003458/// If an error occurred, it returns ExprError(); otherwise, it
3459/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003460/// InstantiatedParamType is the type of the non-type template
3461/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003462ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3463 QualType InstantiatedParamType, Expr *Arg,
3464 TemplateArgument &Converted,
3465 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003466 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3467
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003468 // If either the parameter has a dependent type or the argument is
3469 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003470 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3471 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003472 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003473 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003474 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003475
3476 // C++ [temp.arg.nontype]p5:
3477 // The following conversions are performed on each expression used
3478 // as a non-type template-argument. If a non-type
3479 // template-argument cannot be converted to the type of the
3480 // corresponding template-parameter then the program is
3481 // ill-formed.
3482 //
3483 // -- for a non-type template-parameter of integral or
3484 // enumeration type, integral promotions (4.5) and integral
3485 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003486 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003487 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003488 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003489 // C++ [temp.arg.nontype]p1:
3490 // A template-argument for a non-type, non-template
3491 // template-parameter shall be one of:
3492 //
3493 // -- an integral constant-expression of integral or enumeration
3494 // type; or
3495 // -- the name of a non-type template-parameter; or
3496 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003497 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003498 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003499 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003500 diag::err_template_arg_not_integral_or_enumeral)
3501 << ArgType << Arg->getSourceRange();
3502 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003503 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003504 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003505 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003506 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3507 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003508 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003509 }
3510
Douglas Gregor02024a92010-03-28 02:42:43 +00003511 // From here on out, all we care about are the unqualified forms
3512 // of the parameter and argument types.
3513 ParamType = ParamType.getUnqualifiedType();
3514 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003515
3516 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003517 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003518 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003519 } else if (CTAK == CTAK_Deduced) {
3520 // C++ [temp.deduct.type]p17:
3521 // If, in the declaration of a function template with a non-type
3522 // template-parameter, the non-type template- parameter is used
3523 // in an expression in the function parameter-list and, if the
3524 // corresponding template-argument is deduced, the
3525 // template-argument type shall match the type of the
3526 // template-parameter exactly, except that a template-argument
3527 // deduced from an array bound may be of any integral type.
3528 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3529 << ArgType << ParamType;
3530 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003531 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003532 } else if (ParamType->isBooleanType()) {
3533 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003534 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003535 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3536 !ParamType->isEnumeralType()) {
3537 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003538 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003539 } else {
3540 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003541 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003542 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003543 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003544 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003545 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003546 }
3547
Douglas Gregorc7469372011-05-04 21:55:00 +00003548 // Add the value of this argument to the list of converted
3549 // arguments. We use the bitwidth and signedness of the template
3550 // parameter.
3551 if (Arg->isValueDependent()) {
3552 // The argument is value-dependent. Create a new
3553 // TemplateArgument with the converted expression.
3554 Converted = TemplateArgument(Arg);
3555 return Owned(Arg);
3556 }
3557
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003558 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003559 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003560 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003561
Douglas Gregorc7469372011-05-04 21:55:00 +00003562 if (ParamType->isBooleanType()) {
3563 // Value must be zero or one.
3564 Value = Value != 0;
3565 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3566 if (Value.getBitWidth() != AllowedBits)
3567 Value = Value.extOrTrunc(AllowedBits);
3568 Value.setIsSigned(IntegerType->isSignedIntegerType());
3569 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003570 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003571
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003572 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003573 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003574 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003575 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003576 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003577 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003578
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003579 // Complain if an unsigned parameter received a negative value.
3580 if (IntegerType->isUnsignedIntegerType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003581 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003582 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3583 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3584 << Arg->getSourceRange();
3585 Diag(Param->getLocation(), diag::note_template_param_here);
3586 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003587
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003588 // Complain if we overflowed the template parameter's type.
3589 unsigned RequiredBits;
3590 if (IntegerType->isUnsignedIntegerType())
3591 RequiredBits = OldValue.getActiveBits();
3592 else if (OldValue.isUnsigned())
3593 RequiredBits = OldValue.getActiveBits() + 1;
3594 else
3595 RequiredBits = OldValue.getMinSignedBits();
3596 if (RequiredBits > AllowedBits) {
3597 Diag(Arg->getSourceRange().getBegin(),
3598 diag::warn_template_arg_too_large)
3599 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3600 << Arg->getSourceRange();
3601 Diag(Param->getLocation(), diag::note_template_param_here);
3602 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003603 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003604
John McCall833ca992009-10-29 08:12:44 +00003605 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003606 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003607 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003608 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003609 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003610
John McCall6bb80172010-03-30 21:47:33 +00003611 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3612
Douglas Gregorb7a09262010-04-01 18:32:35 +00003613 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3614 // from a template argument of type std::nullptr_t to a non-type
3615 // template parameter of type pointer to object, pointer to
3616 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003617 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003618 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3619 Converted = TemplateArgument((NamedDecl *)0);
John Wiegley429bb272011-04-08 18:41:53 +00003620 return Owned(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003621 }
3622
Douglas Gregorb86b0572009-02-11 01:18:59 +00003623 // Handle pointer-to-function, reference-to-function, and
3624 // pointer-to-member-function all in (roughly) the same way.
3625 if (// -- For a non-type template-parameter of type pointer to
3626 // function, only the function-to-pointer conversion (4.3) is
3627 // applied. If the template-argument represents a set of
3628 // overloaded functions (or a pointer to such), the matching
3629 // function is selected from the set (13.4).
3630 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003631 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003632 // -- For a non-type template-parameter of type reference to
3633 // function, no conversions apply. If the template-argument
3634 // represents a set of overloaded functions, the matching
3635 // function is selected from the set (13.4).
3636 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003637 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003638 // -- For a non-type template-parameter of type pointer to
3639 // member function, no conversions apply. If the
3640 // template-argument represents a set of overloaded member
3641 // functions, the matching member function is selected from
3642 // the set (13.4).
3643 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003644 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003645 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003646
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003647 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003648 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003649 true,
3650 FoundResult)) {
3651 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003652 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003653
3654 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3655 ArgType = Arg->getType();
3656 } else
John Wiegley429bb272011-04-08 18:41:53 +00003657 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003658 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003659
John Wiegley429bb272011-04-08 18:41:53 +00003660 if (!ParamType->isMemberPointerType()) {
3661 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3662 ParamType,
3663 Arg, Converted))
3664 return ExprError();
3665 return Owned(Arg);
3666 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003667
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003668 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3669 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003670 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003671 } else if (!Context.hasSameUnqualifiedType(ArgType,
3672 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003673 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003674 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003675 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003676 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003677 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003678 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003679 }
Mike Stump1eb44332009-09-09 15:08:12 +00003680
John Wiegley429bb272011-04-08 18:41:53 +00003681 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3682 return ExprError();
3683 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003684 }
3685
Chris Lattnerfe90de72009-02-20 21:37:53 +00003686 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003687 // -- for a non-type template-parameter of type pointer to
3688 // object, qualification conversions (4.4) and the
3689 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003690 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003691 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003692 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003693
John Wiegley429bb272011-04-08 18:41:53 +00003694 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3695 ParamType,
3696 Arg, Converted))
3697 return ExprError();
3698 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003699 }
Mike Stump1eb44332009-09-09 15:08:12 +00003700
Ted Kremenek6217b802009-07-29 21:53:49 +00003701 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003702 // -- For a non-type template-parameter of type reference to
3703 // object, no conversions apply. The type referred to by the
3704 // reference may be more cv-qualified than the (otherwise
3705 // identical) type of the template-argument. The
3706 // template-parameter is bound directly to the
3707 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003708 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003709 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003710
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003711 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003712 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3713 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003714 true,
3715 FoundResult)) {
3716 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003717 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003718
3719 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3720 ArgType = Arg->getType();
3721 } else
John Wiegley429bb272011-04-08 18:41:53 +00003722 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003724
John Wiegley429bb272011-04-08 18:41:53 +00003725 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3726 ParamType,
3727 Arg, Converted))
3728 return ExprError();
3729 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003730 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003731
3732 // -- For a non-type template-parameter of type pointer to data
3733 // member, qualification conversions (4.4) are applied.
3734 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3735
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003736 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003737 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003738 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003739 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003740 } else {
3741 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003742 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003743 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003744 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003745 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003746 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003747 }
3748
John Wiegley429bb272011-04-08 18:41:53 +00003749 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3750 return ExprError();
3751 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003752}
3753
3754/// \brief Check a template argument against its corresponding
3755/// template template parameter.
3756///
3757/// This routine implements the semantics of C++ [temp.arg.template].
3758/// It returns true if an error occurred, and false otherwise.
3759bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003760 const TemplateArgumentLoc &Arg) {
3761 TemplateName Name = Arg.getArgument().getAsTemplate();
3762 TemplateDecl *Template = Name.getAsTemplateDecl();
3763 if (!Template) {
3764 // Any dependent template name is fine.
3765 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3766 return false;
3767 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003768
Richard Smith3e4c6c42011-05-05 21:57:07 +00003769 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003770 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003771 // the name of a class template or an alias template, expressed as an
3772 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00003773 // primary class templates are considered when matching the
3774 // template template argument with the corresponding parameter;
3775 // partial specializations are not considered even if their
3776 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003777 //
3778 // Note that we also allow template template parameters here, which
3779 // will happen when we are dealing with, e.g., class template
3780 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003781 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00003782 !isa<TemplateTemplateParmDecl>(Template) &&
3783 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003784 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003785 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003786 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003787 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003788 << Template;
3789 }
3790
3791 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3792 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003793 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003794 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003795 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003796}
3797
Douglas Gregor02024a92010-03-28 02:42:43 +00003798/// \brief Given a non-type template argument that refers to a
3799/// declaration and the type of its corresponding non-type template
3800/// parameter, produce an expression that properly refers to that
3801/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003802ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003803Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3804 QualType ParamType,
3805 SourceLocation Loc) {
3806 assert(Arg.getKind() == TemplateArgument::Declaration &&
3807 "Only declaration template arguments permitted here");
3808 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3809
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003810 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003811 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3812 // If the value is a class member, we might have a pointer-to-member.
3813 // Determine whether the non-type template template parameter is of
3814 // pointer-to-member type. If so, we need to build an appropriate
3815 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3816 // would refer to the member itself.
3817 if (ParamType->isMemberPointerType()) {
3818 QualType ClassType
3819 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3820 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003821 = NestedNameSpecifier::Create(Context, 0, false,
3822 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003823 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003824 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003825
3826 // The actual value-ness of this is unimportant, but for
3827 // internal consistency's sake, references to instance methods
3828 // are r-values.
3829 ExprValueKind VK = VK_LValue;
3830 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3831 VK = VK_RValue;
3832
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003833 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003834 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003835 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003836 Loc,
3837 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003838 if (RefExpr.isInvalid())
3839 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003840
John McCall2de56d12010-08-25 11:45:40 +00003841 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003842
Douglas Gregorc0c83002010-04-30 21:46:38 +00003843 // We might need to perform a trailing qualification conversion, since
3844 // the element type on the parameter could be more qualified than the
3845 // element type in the expression we constructed.
3846 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00003847 ParamType.getUnqualifiedType(), false))
3848 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003849
Douglas Gregor02024a92010-03-28 02:42:43 +00003850 assert(!RefExpr.isInvalid() &&
3851 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003852 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003853 return move(RefExpr);
3854 }
3855 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003856
Douglas Gregor02024a92010-03-28 02:42:43 +00003857 QualType T = VD->getType().getNonReferenceType();
3858 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003859 // When the non-type template parameter is a pointer, take the
3860 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003861 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003862 if (RefExpr.isInvalid())
3863 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003864
3865 if (T->isFunctionType() || T->isArrayType()) {
3866 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00003867 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
3868 if (RefExpr.isInvalid())
3869 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003870
3871 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003873
Douglas Gregorb7a09262010-04-01 18:32:35 +00003874 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003875 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003876 }
3877
John McCallf89e55a2010-11-18 06:31:45 +00003878 ExprValueKind VK = VK_RValue;
3879
Douglas Gregor02024a92010-03-28 02:42:43 +00003880 // If the non-type template parameter has reference type, qualify the
3881 // resulting declaration reference with the extra qualifiers on the
3882 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003883 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3884 VK = VK_LValue;
3885 T = Context.getQualifiedType(T,
3886 TargetRef->getPointeeType().getQualifiers());
3887 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003888
John McCallf89e55a2010-11-18 06:31:45 +00003889 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003890}
3891
3892/// \brief Construct a new expression that refers to the given
3893/// integral template argument with the given source-location
3894/// information.
3895///
3896/// This routine takes care of the mapping from an integral template
3897/// argument (which may have any integral type) to the appropriate
3898/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003900Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3901 SourceLocation Loc) {
3902 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003903 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003904 QualType T = Arg.getIntegralType();
3905 if (T->isCharType() || T->isWideCharType())
3906 return Owned(new (Context) CharacterLiteral(
3907 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003908 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003909 if (T->isBooleanType())
3910 return Owned(new (Context) CXXBoolLiteralExpr(
3911 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003912 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003913
Chris Lattner223de242011-04-25 20:37:58 +00003914 // If this is an enum type that we're instantiating, we need to use an integer
3915 // type the same size as the enumerator. We don't want to build an
3916 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003917 QualType BT;
3918 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00003919 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003920 else
3921 BT = T;
3922
3923 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003924 if (T->isEnumeralType()) {
3925 // FIXME: This is a hack. We need a better way to handle substituted
3926 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00003927 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
3928 Context.getTrivialTypeSourceInfo(T, Loc),
3929 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003930 }
3931
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003932 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003933}
3934
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003935/// \brief Match two template parameters within template parameter lists.
3936static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3937 bool Complain,
3938 Sema::TemplateParameterListEqualKind Kind,
3939 SourceLocation TemplateArgLoc) {
3940 // Check the actual kind (type, non-type, template).
3941 if (Old->getKind() != New->getKind()) {
3942 if (Complain) {
3943 unsigned NextDiag = diag::err_template_param_different_kind;
3944 if (TemplateArgLoc.isValid()) {
3945 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3946 NextDiag = diag::note_template_param_different_kind;
3947 }
3948 S.Diag(New->getLocation(), NextDiag)
3949 << (Kind != Sema::TPL_TemplateMatch);
3950 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3951 << (Kind != Sema::TPL_TemplateMatch);
3952 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003953
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003954 return false;
3955 }
3956
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003957 // Check that both are parameter packs are neither are parameter packs.
3958 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003959 // template template parameter, the template template parameter can have
3960 // a parameter pack where the template template argument does not.
3961 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3962 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3963 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003964 if (Complain) {
3965 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3966 if (TemplateArgLoc.isValid()) {
3967 S.Diag(TemplateArgLoc,
3968 diag::err_template_arg_template_params_mismatch);
3969 NextDiag = diag::note_template_parameter_pack_non_pack;
3970 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003971
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003972 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3973 : isa<NonTypeTemplateParmDecl>(New)? 1
3974 : 2;
3975 S.Diag(New->getLocation(), NextDiag)
3976 << ParamKind << New->isParameterPack();
3977 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3978 << ParamKind << Old->isParameterPack();
3979 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003980
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003981 return false;
3982 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003983
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003984 // For non-type template parameters, check the type of the parameter.
3985 if (NonTypeTemplateParmDecl *OldNTTP
3986 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3987 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003988
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003989 // If we are matching a template template argument to a template
3990 // template parameter and one of the non-type template parameter types
3991 // is dependent, then we must wait until template instantiation time
3992 // to actually compare the arguments.
3993 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3994 (OldNTTP->getType()->isDependentType() ||
3995 NewNTTP->getType()->isDependentType()))
3996 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003997
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003998 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3999 if (Complain) {
4000 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4001 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004002 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004003 diag::err_template_arg_template_params_mismatch);
4004 NextDiag = diag::note_template_nontype_parm_different_type;
4005 }
4006 S.Diag(NewNTTP->getLocation(), NextDiag)
4007 << NewNTTP->getType()
4008 << (Kind != Sema::TPL_TemplateMatch);
4009 S.Diag(OldNTTP->getLocation(),
4010 diag::note_template_nontype_parm_prev_declaration)
4011 << OldNTTP->getType();
4012 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004013
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004014 return false;
4015 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004016
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004017 return true;
4018 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004019
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004020 // For template template parameters, check the template parameter types.
4021 // The template parameter lists of template template
4022 // parameters must agree.
4023 if (TemplateTemplateParmDecl *OldTTP
4024 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004025 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004026 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4027 OldTTP->getTemplateParameters(),
4028 Complain,
4029 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004030 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004031 : Kind),
4032 TemplateArgLoc);
4033 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004034
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004035 return true;
4036}
Douglas Gregor02024a92010-03-28 02:42:43 +00004037
Douglas Gregora0347822011-01-13 00:08:50 +00004038/// \brief Diagnose a known arity mismatch when comparing template argument
4039/// lists.
4040static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004041void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004042 TemplateParameterList *New,
4043 TemplateParameterList *Old,
4044 Sema::TemplateParameterListEqualKind Kind,
4045 SourceLocation TemplateArgLoc) {
4046 unsigned NextDiag = diag::err_template_param_list_different_arity;
4047 if (TemplateArgLoc.isValid()) {
4048 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4049 NextDiag = diag::note_template_param_list_different_arity;
4050 }
4051 S.Diag(New->getTemplateLoc(), NextDiag)
4052 << (New->size() > Old->size())
4053 << (Kind != Sema::TPL_TemplateMatch)
4054 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4055 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4056 << (Kind != Sema::TPL_TemplateMatch)
4057 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4058}
4059
Douglas Gregorddc29e12009-02-06 22:42:48 +00004060/// \brief Determine whether the given template parameter lists are
4061/// equivalent.
4062///
Mike Stump1eb44332009-09-09 15:08:12 +00004063/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004064/// source code as part of a new template declaration.
4065///
4066/// \param Old The old template parameter list, typically found via
4067/// name lookup of the template declared with this template parameter
4068/// list.
4069///
4070/// \param Complain If true, this routine will produce a diagnostic if
4071/// the template parameter lists are not equivalent.
4072///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004073/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004074///
4075/// \param TemplateArgLoc If this source location is valid, then we
4076/// are actually checking the template parameter list of a template
4077/// argument (New) against the template parameter list of its
4078/// corresponding template template parameter (Old). We produce
4079/// slightly different diagnostics in this scenario.
4080///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004081/// \returns True if the template parameter lists are equal, false
4082/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004083bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004084Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4085 TemplateParameterList *Old,
4086 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004087 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004088 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004089 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4090 if (Complain)
4091 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4092 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004093
4094 return false;
4095 }
4096
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004097 // C++0x [temp.arg.template]p3:
4098 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004099 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004100 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004101 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004102 // template-parameter-list of P. [...]
4103 TemplateParameterList::iterator NewParm = New->begin();
4104 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004105 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004106 OldParmEnd = Old->end();
4107 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004108 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4109 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004110 if (NewParm == NewParmEnd) {
4111 if (Complain)
4112 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4113 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004114
Douglas Gregora0347822011-01-13 00:08:50 +00004115 return false;
4116 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004117
Douglas Gregora0347822011-01-13 00:08:50 +00004118 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4119 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004120 return false;
4121
Douglas Gregora0347822011-01-13 00:08:50 +00004122 ++NewParm;
4123 continue;
4124 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004125
Douglas Gregora0347822011-01-13 00:08:50 +00004126 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004127 // [...] When P's template- parameter-list contains a template parameter
4128 // pack (14.5.3), the template parameter pack will match zero or more
4129 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004130 // template-parameter-list of A with the same type and form as the
4131 // template parameter pack in P (ignoring whether those template
4132 // parameters are template parameter packs).
4133 for (; NewParm != NewParmEnd; ++NewParm) {
4134 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4135 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004136 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004137 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
Douglas Gregora0347822011-01-13 00:08:50 +00004140 // Make sure we exhausted all of the arguments.
4141 if (NewParm != NewParmEnd) {
4142 if (Complain)
4143 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4144 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004145
Douglas Gregora0347822011-01-13 00:08:50 +00004146 return false;
4147 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004148
Douglas Gregorddc29e12009-02-06 22:42:48 +00004149 return true;
4150}
4151
4152/// \brief Check whether a template can be declared within this scope.
4153///
4154/// If the template declaration is valid in this scope, returns
4155/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004156bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004157Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004158 // Find the nearest enclosing declaration scope.
4159 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4160 (S->getFlags() & Scope::TemplateParamScope) != 0)
4161 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004162
Douglas Gregorddc29e12009-02-06 22:42:48 +00004163 // C++ [temp]p2:
4164 // A template-declaration can appear only as a namespace scope or
4165 // class scope declaration.
4166 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004167 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4168 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004169 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004170 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004171
Eli Friedman1503f772009-07-31 01:43:05 +00004172 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004173 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004174
4175 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4176 return false;
4177
Mike Stump1eb44332009-09-09 15:08:12 +00004178 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004179 diag::err_template_outside_namespace_or_class_scope)
4180 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004181}
Douglas Gregorcc636682009-02-17 23:15:12 +00004182
Douglas Gregord5cb8762009-10-07 00:13:32 +00004183/// \brief Determine what kind of template specialization the given declaration
4184/// is.
4185static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4186 if (!D)
4187 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004188
Douglas Gregorf6b11852009-10-08 15:14:33 +00004189 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4190 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004191 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4192 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004193 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4194 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004195
Douglas Gregord5cb8762009-10-07 00:13:32 +00004196 return TSK_Undeclared;
4197}
4198
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004199/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004200/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004201///
Douglas Gregor9302da62009-10-14 23:50:59 +00004202/// This routine determines whether a template specialization can be declared
4203/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004204///
4205/// \param S the semantic analysis object for which this check is being
4206/// performed.
4207///
4208/// \param Specialized the entity being specialized or instantiated, which
4209/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004210/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004211/// member class).
4212///
4213/// \param PrevDecl the previous declaration of this entity, if any.
4214///
4215/// \param Loc the location of the explicit specialization or instantiation of
4216/// this entity.
4217///
4218/// \param IsPartialSpecialization whether this is a partial specialization of
4219/// a class template.
4220///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004221/// \returns true if there was an error that we cannot recover from, false
4222/// otherwise.
4223static bool CheckTemplateSpecializationScope(Sema &S,
4224 NamedDecl *Specialized,
4225 NamedDecl *PrevDecl,
4226 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004227 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004228 // Keep these "kind" numbers in sync with the %select statements in the
4229 // various diagnostics emitted by this routine.
4230 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004231 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004232 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004233 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004234 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004235 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004236 EntityKind = 3;
4237 else if (isa<VarDecl>(Specialized))
4238 EntityKind = 4;
4239 else if (isa<RecordDecl>(Specialized))
4240 EntityKind = 5;
4241 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004242 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4243 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004244 return true;
4245 }
4246
Douglas Gregor88b70942009-02-25 22:02:03 +00004247 // C++ [temp.expl.spec]p2:
4248 // An explicit specialization shall be declared in the namespace
4249 // of which the template is a member, or, for member templates, in
4250 // the namespace of which the enclosing class or enclosing class
4251 // template is a member. An explicit specialization of a member
4252 // function, member class or static data member of a class
4253 // template shall be declared in the namespace of which the class
4254 // template is a member. Such a declaration may also be a
4255 // definition. If the declaration is not a definition, the
4256 // specialization may be defined later in the name- space in which
4257 // the explicit specialization was declared, or in a namespace
4258 // that encloses the one in which the explicit specialization was
4259 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004260 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004261 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004262 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004263 return true;
4264 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004265
Douglas Gregor0a407472009-10-07 17:30:37 +00004266 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4267 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004268 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004269 return true;
4270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004272 // C++ [temp.class.spec]p6:
4273 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004274 // in any namespace scope in which its definition may be defined (14.5.1
4275 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004276 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004277 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004278 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004279 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004280 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004281 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4282 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004283 // C++ [temp.exp.spec]p2:
4284 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004285 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004286 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287 // An explicit specialization of a member function, member class or
4288 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004289 // namespace of which the class template is a member.
4290 //
4291 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004292 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004293 // the specialized template.
4294 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4295 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004296 bool IsCPlusPlus0xExtension
4297 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004298 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004299 S.Diag(Loc, IsCPlusPlus0xExtension
4300 ? diag::ext_template_spec_decl_out_of_scope_global
4301 : diag::err_template_spec_decl_out_of_scope_global)
4302 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004303 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004304 S.Diag(Loc, IsCPlusPlus0xExtension
4305 ? diag::ext_template_spec_decl_out_of_scope
4306 : diag::err_template_spec_decl_out_of_scope)
4307 << EntityKind << Specialized
4308 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004309
Douglas Gregor9302da62009-10-14 23:50:59 +00004310 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4311 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004312 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004313 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004314
4315 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004316 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004317 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004318 // specializations of function templates, static data members, and member
4319 // functions, so we skip the check here for those kinds of entities.
4320 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004321 // Should we refactor that check, so that it occurs later?
4322 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004323 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4324 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004325 if (isa<TranslationUnitDecl>(SpecializedContext))
4326 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4327 << EntityKind << Specialized;
4328 else if (isa<NamespaceDecl>(SpecializedContext))
4329 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4330 << EntityKind << Specialized
4331 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004332
Douglas Gregor9302da62009-10-14 23:50:59 +00004333 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004334 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004335
Douglas Gregord5cb8762009-10-07 00:13:32 +00004336 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004337
Douglas Gregor88b70942009-02-25 22:02:03 +00004338 return false;
4339}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004340
Douglas Gregorbacb9492011-01-03 21:13:47 +00004341/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4342/// that checks non-type template partial specialization arguments.
4343static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4344 NonTypeTemplateParmDecl *Param,
4345 const TemplateArgument *Args,
4346 unsigned NumArgs) {
4347 for (unsigned I = 0; I != NumArgs; ++I) {
4348 if (Args[I].getKind() == TemplateArgument::Pack) {
4349 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004350 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004351 Args[I].pack_size()))
4352 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004353
Douglas Gregore94866f2009-06-12 21:21:02 +00004354 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004355 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004356
Douglas Gregorbacb9492011-01-03 21:13:47 +00004357 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004358 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004359 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004360 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004361
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004362 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004363 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4364 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004365
4366 // Strip off any implicit casts we added as part of type checking.
4367 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4368 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004369
Douglas Gregore94866f2009-06-12 21:21:02 +00004370 // C++ [temp.class.spec]p8:
4371 // A non-type argument is non-specialized if it is the name of a
4372 // non-type parameter. All other non-type arguments are
4373 // specialized.
4374 //
4375 // Below, we check the two conditions that only apply to
4376 // specialized non-type arguments, so skip any non-specialized
4377 // arguments.
4378 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004379 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004380 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004381
Douglas Gregore94866f2009-06-12 21:21:02 +00004382 // C++ [temp.class.spec]p9:
4383 // Within the argument list of a class template partial
4384 // specialization, the following restrictions apply:
4385 // -- A partially specialized non-type argument expression
4386 // shall not involve a template parameter of the partial
4387 // specialization except when the argument expression is a
4388 // simple identifier.
4389 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004390 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004391 diag::err_dependent_non_type_arg_in_partial_spec)
4392 << ArgExpr->getSourceRange();
4393 return true;
4394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004395
Douglas Gregore94866f2009-06-12 21:21:02 +00004396 // -- The type of a template parameter corresponding to a
4397 // specialized non-type argument shall not be dependent on a
4398 // parameter of the specialization.
4399 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004400 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004401 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4402 << Param->getType()
4403 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004404 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004405 return true;
4406 }
4407 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004408
Douglas Gregorbacb9492011-01-03 21:13:47 +00004409 return false;
4410}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004411
Douglas Gregorbacb9492011-01-03 21:13:47 +00004412/// \brief Check the non-type template arguments of a class template
4413/// partial specialization according to C++ [temp.class.spec]p9.
4414///
4415/// \param TemplateParams the template parameters of the primary class
4416/// template.
4417///
4418/// \param TemplateArg the template arguments of the class template
4419/// partial specialization.
4420///
4421/// \returns true if there was an error, false otherwise.
4422static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4423 TemplateParameterList *TemplateParams,
4424 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4425 const TemplateArgument *ArgList = TemplateArgs.data();
4426
4427 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4428 NonTypeTemplateParmDecl *Param
4429 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4430 if (!Param)
4431 continue;
4432
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004433 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004434 &ArgList[I], 1))
4435 return true;
4436 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004437
4438 return false;
4439}
4440
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004441/// \brief Retrieve the previous declaration of the given declaration.
4442static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4443 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4444 return VD->getPreviousDeclaration();
4445 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4446 return FD->getPreviousDeclaration();
4447 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4448 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004449 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004450 return TD->getPreviousDeclaration();
4451 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4452 return FTD->getPreviousDeclaration();
4453 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4454 return CTD->getPreviousDeclaration();
4455 return 0;
4456}
4457
John McCalld226f652010-08-21 09:40:31 +00004458DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004459Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4460 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004461 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004462 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004463 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004464 SourceLocation TemplateNameLoc,
4465 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004466 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004467 SourceLocation RAngleLoc,
4468 AttributeList *Attr,
4469 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004470 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004471
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004472 // NOTE: KWLoc is the location of the tag keyword. This will instead
4473 // store the location of the outermost template keyword in the declaration.
4474 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4475 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4476
Douglas Gregorcc636682009-02-17 23:15:12 +00004477 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004478 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004479 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004480 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4481
4482 if (!ClassTemplate) {
4483 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004484 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004485 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4486 return true;
4487 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004488
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004489 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004490 bool isPartialSpecialization = false;
4491
Douglas Gregor88b70942009-02-25 22:02:03 +00004492 // Check the validity of the template headers that introduce this
4493 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004494 // FIXME: We probably shouldn't complain about these headers for
4495 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004496 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004497 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004498 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4499 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004500 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004501 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004502 isExplicitSpecialization,
4503 Invalid);
4504 if (Invalid)
4505 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506
Douglas Gregor05396e22009-08-25 17:23:04 +00004507 if (TemplateParams && TemplateParams->size() > 0) {
4508 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004509
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004510 if (TUK == TUK_Friend) {
4511 Diag(KWLoc, diag::err_partial_specialization_friend)
4512 << SourceRange(LAngleLoc, RAngleLoc);
4513 return true;
4514 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515
Douglas Gregor05396e22009-08-25 17:23:04 +00004516 // C++ [temp.class.spec]p10:
4517 // The template parameter list of a specialization shall not
4518 // contain default template argument values.
4519 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4520 Decl *Param = TemplateParams->getParam(I);
4521 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4522 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004523 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004524 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004525 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004526 }
4527 } else if (NonTypeTemplateParmDecl *NTTP
4528 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4529 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004530 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004531 diag::err_default_arg_in_partial_spec)
4532 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004533 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004534 }
4535 } else {
4536 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004537 if (TTP->hasDefaultArgument()) {
4538 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004539 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004540 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004541 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004542 }
4543 }
4544 }
Douglas Gregora735b202009-10-13 14:39:41 +00004545 } else if (TemplateParams) {
4546 if (TUK == TUK_Friend)
4547 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004548 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004549 SourceRange(TemplateParams->getTemplateLoc(),
4550 TemplateParams->getRAngleLoc()))
4551 << SourceRange(LAngleLoc, RAngleLoc);
4552 else
4553 isExplicitSpecialization = true;
4554 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004555 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004556 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004557 isExplicitSpecialization = true;
4558 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004559
Douglas Gregorcc636682009-02-17 23:15:12 +00004560 // Check that the specialization uses the same tag kind as the
4561 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004562 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4563 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004564 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004565 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004566 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004567 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004568 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004569 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004570 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004571 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004572 diag::note_previous_use);
4573 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4574 }
4575
Douglas Gregor40808ce2009-03-09 23:48:35 +00004576 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004577 TemplateArgumentListInfo TemplateArgs;
4578 TemplateArgs.setLAngleLoc(LAngleLoc);
4579 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004580 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004581
Douglas Gregor925910d2011-01-03 20:35:03 +00004582 // Check for unexpanded parameter packs in any of the template arguments.
4583 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004584 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004585 UPPC_PartialSpecialization))
4586 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587
Douglas Gregorcc636682009-02-17 23:15:12 +00004588 // Check that the template argument list is well-formed for this
4589 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004590 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004591 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4592 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004593 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004594
Douglas Gregor910f8002010-11-07 23:05:16 +00004595 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004596 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004597
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004598 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004599 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004600 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004601 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004602 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004603 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004604 return true;
4605
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004607 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004608 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004609 TemplateArgs.size())) {
4610 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4611 << ClassTemplate->getDeclName();
4612 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004613 }
4614 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004615
Douglas Gregorcc636682009-02-17 23:15:12 +00004616 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004617 ClassTemplateSpecializationDecl *PrevDecl = 0;
4618
4619 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004620 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004621 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004622 = ClassTemplate->findPartialSpecialization(Converted.data(),
4623 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004624 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004625 else
4626 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004627 = ClassTemplate->findSpecialization(Converted.data(),
4628 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004629
4630 ClassTemplateSpecializationDecl *Specialization = 0;
4631
Douglas Gregor88b70942009-02-25 22:02:03 +00004632 // Check whether we can declare a class template specialization in
4633 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004634 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004635 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4636 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004637 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004638 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004639
Douglas Gregorb88e8882009-07-30 17:40:51 +00004640 // The canonical type
4641 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004643 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004644 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004645 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004646 // arguments was referenced but not declared, or we're only
4647 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004648 // declaration node as our own, updating its source location and
4649 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004650 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004651 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004652 if (TemplateParameterLists.size() > 0) {
4653 Specialization->setTemplateParameterListsInfo(Context,
4654 TemplateParameterLists.size(),
4655 (TemplateParameterList**) TemplateParameterLists.release());
4656 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004657 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004658 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004659 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004660 // Build the canonical type that describes the converted template
4661 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004662 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4663 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004664 Converted.data(),
4665 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004666
4667 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004668 ClassTemplate->getInjectedClassNameSpecialization())) {
4669 // C++ [temp.class.spec]p9b3:
4670 //
4671 // -- The argument list of the specialization shall not be identical
4672 // to the implicit argument list of the primary template.
4673 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4674 << (TUK == TUK_Definition)
4675 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4676 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4677 ClassTemplate->getIdentifier(),
4678 TemplateNameLoc,
4679 Attr,
4680 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004681 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004682 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004683 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004684 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004685
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004686 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004687 ClassTemplatePartialSpecializationDecl *PrevPartial
4688 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004689 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004690 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004691 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004692 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004693 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004694 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004695 TemplateParams,
4696 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004697 Converted.data(),
4698 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004699 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004700 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004701 PrevPartial,
4702 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004703 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004704 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004705 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004706 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004707 (TemplateParameterList**) TemplateParameterLists.release());
4708 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004709
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004710 if (!PrevPartial)
4711 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004712 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004713
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004714 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004715 // template specialization, make a note of that.
4716 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4717 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004718
Douglas Gregor031a5882009-06-13 00:26:55 +00004719 // Check that all of the template parameters of the class template
4720 // partial specialization are deducible from the template
4721 // arguments. If not, this class template partial specialization
4722 // will never be used.
4723 llvm::SmallVector<bool, 8> DeducibleParams;
4724 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004725 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004726 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004727 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004728 unsigned NumNonDeducible = 0;
4729 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4730 if (!DeducibleParams[I])
4731 ++NumNonDeducible;
4732
4733 if (NumNonDeducible) {
4734 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4735 << (NumNonDeducible > 1)
4736 << SourceRange(TemplateNameLoc, RAngleLoc);
4737 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4738 if (!DeducibleParams[I]) {
4739 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4740 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004741 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004742 diag::note_partial_spec_unused_parameter)
4743 << Param->getDeclName();
4744 else
Mike Stump1eb44332009-09-09 15:08:12 +00004745 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004746 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004747 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004748 }
4749 }
4750 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004751 } else {
4752 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004753 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004754 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004755 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004756 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004757 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004758 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004759 Converted.data(),
4760 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004761 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004762 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004763 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004764 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004765 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004766 (TemplateParameterList**) TemplateParameterLists.release());
4767 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004768
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004769 if (!PrevDecl)
4770 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004771
4772 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004773 }
4774
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004775 // C++ [temp.expl.spec]p6:
4776 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004777 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004778 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004779 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004780 // use occurs; no diagnostic is required.
4781 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004782 bool Okay = false;
4783 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4784 // Is there any previous explicit specialization declaration?
4785 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4786 Okay = true;
4787 break;
4788 }
4789 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004790
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004791 if (!Okay) {
4792 SourceRange Range(TemplateNameLoc, RAngleLoc);
4793 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4794 << Context.getTypeDeclType(Specialization) << Range;
4795
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004796 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004797 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004798 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004799 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004800 return true;
4801 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004802 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004803
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004804 // If this is not a friend, note that this is an explicit specialization.
4805 if (TUK != TUK_Friend)
4806 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004807
4808 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004809 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004810 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004811 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004812 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004813 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004814 Diag(Def->getLocation(), diag::note_previous_definition);
4815 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004816 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004817 }
4818 }
4819
John McCall7f1b9872010-12-18 03:30:47 +00004820 if (Attr)
4821 ProcessDeclAttributeList(S, Specialization, Attr);
4822
Douglas Gregorfc705b82009-02-26 22:19:44 +00004823 // Build the fully-sugared type for this class template
4824 // specialization as the user wrote in the specialization
4825 // itself. This means that we'll pretty-print the type retrieved
4826 // from the specialization's declaration the way that the user
4827 // actually wrote the specialization, rather than formatting the
4828 // name based on the "canonical" representation used to store the
4829 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004830 TypeSourceInfo *WrittenTy
4831 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4832 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004833 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004834 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004835 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004836 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004837 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004838
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004839 // C++ [temp.expl.spec]p9:
4840 // A template explicit specialization is in the scope of the
4841 // namespace in which the template was defined.
4842 //
4843 // We actually implement this paragraph where we set the semantic
4844 // context (in the creation of the ClassTemplateSpecializationDecl),
4845 // but we also maintain the lexical context where the actual
4846 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004847 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004848
Douglas Gregorcc636682009-02-17 23:15:12 +00004849 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004850 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004851 Specialization->startDefinition();
4852
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004853 if (TUK == TUK_Friend) {
4854 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4855 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004856 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004857 /*FIXME:*/KWLoc);
4858 Friend->setAccess(AS_public);
4859 CurContext->addDecl(Friend);
4860 } else {
4861 // Add the specialization into its lexical context, so that it can
4862 // be seen when iterating through the list of declarations in that
4863 // context. However, specializations are not found by name lookup.
4864 CurContext->addDecl(Specialization);
4865 }
John McCalld226f652010-08-21 09:40:31 +00004866 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004867}
Douglas Gregord57959a2009-03-27 23:10:48 +00004868
John McCalld226f652010-08-21 09:40:31 +00004869Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004870 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004871 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004872 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4873}
4874
John McCalld226f652010-08-21 09:40:31 +00004875Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004876 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004877 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004878 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004879 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004880
Douglas Gregor52591bf2009-06-24 00:54:41 +00004881 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004882 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004883 }
Mike Stump1eb44332009-09-09 15:08:12 +00004884
Douglas Gregor52591bf2009-06-24 00:54:41 +00004885 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004886
John McCalld226f652010-08-21 09:40:31 +00004887 Decl *DP = HandleDeclarator(ParentScope, D,
4888 move(TemplateParameterLists),
4889 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004890 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004891 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004892 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004893 FunctionTemplate->getTemplatedDecl());
4894 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4895 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4896 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004897}
4898
John McCall75042392010-02-11 01:33:53 +00004899/// \brief Strips various properties off an implicit instantiation
4900/// that has just been explicitly specialized.
4901static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004902 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004903
4904 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4905 FD->setInlineSpecified(false);
4906 }
4907}
4908
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004909/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004910/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004911/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004912/// new specialization/instantiation will have any effect.
4913///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004914/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004915/// instantiation.
4916///
4917/// \param NewTSK the kind of the new explicit specialization or instantiation.
4918///
4919/// \param PrevDecl the previous declaration of the entity.
4920///
4921/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4922///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004923/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004924/// declaration was instantiated (either implicitly or explicitly).
4925///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004926/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004927/// specialization or instantiation has no effect and should be ignored.
4928///
4929/// \returns true if there was an error that should prevent the introduction of
4930/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004931bool
4932Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4933 TemplateSpecializationKind NewTSK,
4934 NamedDecl *PrevDecl,
4935 TemplateSpecializationKind PrevTSK,
4936 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004937 bool &HasNoEffect) {
4938 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004939
Douglas Gregor454885e2009-10-15 15:54:05 +00004940 switch (NewTSK) {
4941 case TSK_Undeclared:
4942 case TSK_ImplicitInstantiation:
4943 assert(false && "Don't check implicit instantiations here");
4944 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004945
Douglas Gregor454885e2009-10-15 15:54:05 +00004946 case TSK_ExplicitSpecialization:
4947 switch (PrevTSK) {
4948 case TSK_Undeclared:
4949 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004950 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004951 // explicitly specialized or has merely been mentioned without any
4952 // instantiation.
4953 return false;
4954
4955 case TSK_ImplicitInstantiation:
4956 if (PrevPointOfInstantiation.isInvalid()) {
4957 // The declaration itself has not actually been instantiated, so it is
4958 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004959 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004960 return false;
4961 }
4962 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004963
Douglas Gregor454885e2009-10-15 15:54:05 +00004964 case TSK_ExplicitInstantiationDeclaration:
4965 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966 assert((PrevTSK == TSK_ImplicitInstantiation ||
4967 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004968 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004969
Douglas Gregor454885e2009-10-15 15:54:05 +00004970 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004971 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004972 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004973 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004974 // implicit instantiation to take place, in every translation unit in
4975 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004976 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4977 // Is there any previous explicit specialization declaration?
4978 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4979 return false;
4980 }
4981
Douglas Gregor0d035142009-10-27 18:42:08 +00004982 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004983 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004984 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004985 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004986
Douglas Gregor454885e2009-10-15 15:54:05 +00004987 return true;
4988 }
4989 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990
Douglas Gregor454885e2009-10-15 15:54:05 +00004991 case TSK_ExplicitInstantiationDeclaration:
4992 switch (PrevTSK) {
4993 case TSK_ExplicitInstantiationDeclaration:
4994 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004995 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004996 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004997
Douglas Gregor454885e2009-10-15 15:54:05 +00004998 case TSK_Undeclared:
4999 case TSK_ImplicitInstantiation:
5000 // We're explicitly instantiating something that may have already been
5001 // implicitly instantiated; that's fine.
5002 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005003
Douglas Gregor454885e2009-10-15 15:54:05 +00005004 case TSK_ExplicitSpecialization:
5005 // C++0x [temp.explicit]p4:
5006 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005007 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005008 // specialization for that template, the explicit instantiation has no
5009 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005010 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005011 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005012
Douglas Gregor454885e2009-10-15 15:54:05 +00005013 case TSK_ExplicitInstantiationDefinition:
5014 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005015 // If an entity is the subject of both an explicit instantiation
5016 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005017 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005018 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005019 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005020 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005021 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005022 assert(PrevPointOfInstantiation.isValid() &&
5023 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005024 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005025 return false;
5026 }
5027 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005028
Douglas Gregor454885e2009-10-15 15:54:05 +00005029 case TSK_ExplicitInstantiationDefinition:
5030 switch (PrevTSK) {
5031 case TSK_Undeclared:
5032 case TSK_ImplicitInstantiation:
5033 // We're explicitly instantiating something that may have already been
5034 // implicitly instantiated; that's fine.
5035 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005036
Douglas Gregor454885e2009-10-15 15:54:05 +00005037 case TSK_ExplicitSpecialization:
5038 // C++ DR 259, C++0x [temp.explicit]p4:
5039 // For a given set of template parameters, if an explicit
5040 // instantiation of a template appears after a declaration of
5041 // an explicit specialization for that template, the explicit
5042 // instantiation has no effect.
5043 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005044 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005045 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005046 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005047 if (!getLangOptions().CPlusPlus0x) {
5048 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005049 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005050 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005051 diag::note_previous_template_specialization);
5052 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005053 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005054 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055
Douglas Gregor454885e2009-10-15 15:54:05 +00005056 case TSK_ExplicitInstantiationDeclaration:
5057 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005059 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005060
Douglas Gregor454885e2009-10-15 15:54:05 +00005061 case TSK_ExplicitInstantiationDefinition:
5062 // C++0x [temp.spec]p5:
5063 // For a given template and a given set of template-arguments,
5064 // - an explicit instantiation definition shall appear at most once
5065 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005066 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005067 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005068 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005069 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005070 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005071 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005072 }
5073 break;
5074 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005075
Douglas Gregor454885e2009-10-15 15:54:05 +00005076 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005077
Douglas Gregor454885e2009-10-15 15:54:05 +00005078 return false;
5079}
5080
John McCallaf2094e2010-04-08 09:05:18 +00005081/// \brief Perform semantic analysis for the given dependent function
5082/// template specialization. The only possible way to get a dependent
5083/// function template specialization is with a friend declaration,
5084/// like so:
5085///
5086/// template <class T> void foo(T);
5087/// template <class T> class A {
5088/// friend void foo<>(T);
5089/// };
5090///
5091/// There really isn't any useful analysis we can do here, so we
5092/// just store the information.
5093bool
5094Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5095 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5096 LookupResult &Previous) {
5097 // Remove anything from Previous that isn't a function template in
5098 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005099 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005100 LookupResult::Filter F = Previous.makeFilter();
5101 while (F.hasNext()) {
5102 NamedDecl *D = F.next()->getUnderlyingDecl();
5103 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005104 !FDLookupContext->InEnclosingNamespaceSetOf(
5105 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005106 F.erase();
5107 }
5108 F.done();
5109
5110 // Should this be diagnosed here?
5111 if (Previous.empty()) return true;
5112
5113 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5114 ExplicitTemplateArgs);
5115 return false;
5116}
5117
Abramo Bagnarae03db982010-05-20 15:32:11 +00005118/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005119/// specialization.
5120///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005121/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005122/// explicit function template specialization. On successful completion,
5123/// the function declaration \p FD will become a function template
5124/// specialization.
5125///
5126/// \param FD the function declaration, which will be updated to become a
5127/// function template specialization.
5128///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005129/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5130/// if any. Note that this may be valid info even when 0 arguments are
5131/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5132/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005133///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005134/// \param PrevDecl the set of declarations that may be specialized by
5135/// this function specialization.
5136bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005137Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005138 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005139 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005140 // The set of function template specializations that could match this
5141 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005142 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005143
Sebastian Redl7a126a42010-08-31 00:36:30 +00005144 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005145 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5146 I != E; ++I) {
5147 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5148 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005149 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005150 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005151 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5152 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005153 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005154
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005155 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005156 // A trailing template-argument can be left unspecified in the
5157 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005158 // provided it can be deduced from the function argument type.
5159 // Perform template argument deduction to determine whether we may be
5160 // specializing this template.
5161 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005162 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005163 FunctionDecl *Specialization = 0;
5164 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005165 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005166 FD->getType(),
5167 Specialization,
5168 Info)) {
5169 // FIXME: Template argument deduction failed; record why it failed, so
5170 // that we can provide nifty diagnostics.
5171 (void)TDK;
5172 continue;
5173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005175 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005176 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005177 }
5178 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005180 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005181 UnresolvedSetIterator Result
5182 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005183 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005184 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005185 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005186 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005187 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005188 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005189 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005190 return true;
John McCallc373d482010-01-27 01:50:18 +00005191
5192 // Ignore access information; it doesn't figure into redeclaration checking.
5193 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005194
5195 FunctionTemplateSpecializationInfo *SpecInfo
5196 = Specialization->getTemplateSpecializationInfo();
5197 assert(SpecInfo && "Function template specialization info missing?");
5198 {
5199 // Note: do not overwrite location info if previous template
5200 // specialization kind was explicit.
5201 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5202 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5203 Specialization->setLocation(FD->getLocation());
5204 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005205
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005206 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005207 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005208
5209 // If this is a friend declaration, then we're not really declaring
5210 // an explicit specialization.
5211 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005212
Douglas Gregord5cb8762009-10-07 00:13:32 +00005213 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005214 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005215 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005216 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005217 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005218 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005219 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005220
5221 // C++ [temp.expl.spec]p6:
5222 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005223 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005224 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005226 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005227 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005228 if (!isFriend &&
5229 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005230 TSK_ExplicitSpecialization,
5231 Specialization,
5232 SpecInfo->getTemplateSpecializationKind(),
5233 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005234 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005235 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005237 // Mark the prior declaration as an explicit specialization, so that later
5238 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005239 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005240 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005241 MarkUnusedFileScopedDecl(Specialization);
5242 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005243
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005244 // Turn the given function declaration into a function template
5245 // specialization, with the template arguments from the previous
5246 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005247 // Take copies of (semantic and syntactic) template argument lists.
5248 const TemplateArgumentList* TemplArgs = new (Context)
5249 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5250 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5251 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005252 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005253 TemplArgs, /*InsertPos=*/0,
5254 SpecInfo->getTemplateSpecializationKind(),
5255 TemplArgsAsWritten);
5256
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005257 // The "previous declaration" for this function template specialization is
5258 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005259 Previous.clear();
5260 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005261 return false;
5262}
5263
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005264/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005265/// specialization.
5266///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005267/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005268/// explicit member function specialization. On successful completion,
5269/// the function declaration \p FD will become a member function
5270/// specialization.
5271///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005272/// \param Member the member declaration, which will be updated to become a
5273/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005274///
John McCall68263142009-11-18 22:49:29 +00005275/// \param Previous the set of declarations, one of which may be specialized
5276/// by this function specialization; the set will be modified to contain the
5277/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278bool
John McCall68263142009-11-18 22:49:29 +00005279Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005280 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005281
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005282 // Try to find the member we are instantiating.
5283 NamedDecl *Instantiation = 0;
5284 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005285 MemberSpecializationInfo *MSInfo = 0;
5286
John McCall68263142009-11-18 22:49:29 +00005287 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005288 // Nowhere to look anyway.
5289 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005290 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5291 I != E; ++I) {
5292 NamedDecl *D = (*I)->getUnderlyingDecl();
5293 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005294 if (Context.hasSameType(Function->getType(), Method->getType())) {
5295 Instantiation = Method;
5296 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005297 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005298 break;
5299 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005300 }
5301 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005302 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005303 VarDecl *PrevVar;
5304 if (Previous.isSingleResult() &&
5305 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005306 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005307 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005308 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005309 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005310 }
5311 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005312 CXXRecordDecl *PrevRecord;
5313 if (Previous.isSingleResult() &&
5314 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5315 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005316 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005317 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005318 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005319 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005320
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005321 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005322 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005323 // specializations are always out-of-line, the caller will complain about
5324 // this mismatch later.
5325 return false;
5326 }
John McCall77e8b112010-04-13 20:37:33 +00005327
5328 // If this is a friend, just bail out here before we start turning
5329 // things into explicit specializations.
5330 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5331 // Preserve instantiation information.
5332 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5333 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5334 cast<CXXMethodDecl>(InstantiatedFrom),
5335 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5336 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5337 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5338 cast<CXXRecordDecl>(InstantiatedFrom),
5339 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5340 }
5341
5342 Previous.clear();
5343 Previous.addDecl(Instantiation);
5344 return false;
5345 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005346
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005347 // Make sure that this is a specialization of a member.
5348 if (!InstantiatedFrom) {
5349 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5350 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005351 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5352 return true;
5353 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005355 // C++ [temp.expl.spec]p6:
5356 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005357 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005358 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005359 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005360 // use occurs; no diagnostic is required.
5361 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005362
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005363 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005364 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5365 TSK_ExplicitSpecialization,
5366 Instantiation,
5367 MSInfo->getTemplateSpecializationKind(),
5368 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005369 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005370 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005371
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005372 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005374 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005375 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005376 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005377 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005378
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005379 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005380 // the original declaration to note that it is an explicit specialization
5381 // (if it was previously an implicit instantiation). This latter step
5382 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005383 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005384 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5385 if (InstantiationFunction->getTemplateSpecializationKind() ==
5386 TSK_ImplicitInstantiation) {
5387 InstantiationFunction->setTemplateSpecializationKind(
5388 TSK_ExplicitSpecialization);
5389 InstantiationFunction->setLocation(Member->getLocation());
5390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005391
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005392 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5393 cast<CXXMethodDecl>(InstantiatedFrom),
5394 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005395 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005396 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005397 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5398 if (InstantiationVar->getTemplateSpecializationKind() ==
5399 TSK_ImplicitInstantiation) {
5400 InstantiationVar->setTemplateSpecializationKind(
5401 TSK_ExplicitSpecialization);
5402 InstantiationVar->setLocation(Member->getLocation());
5403 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005405 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5406 cast<VarDecl>(InstantiatedFrom),
5407 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005408 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005409 } else {
5410 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005411 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5412 if (InstantiationClass->getTemplateSpecializationKind() ==
5413 TSK_ImplicitInstantiation) {
5414 InstantiationClass->setTemplateSpecializationKind(
5415 TSK_ExplicitSpecialization);
5416 InstantiationClass->setLocation(Member->getLocation());
5417 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005418
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005419 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005420 cast<CXXRecordDecl>(InstantiatedFrom),
5421 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005422 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005423
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005424 // Save the caller the trouble of having to figure out which declaration
5425 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005426 Previous.clear();
5427 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005428 return false;
5429}
5430
Douglas Gregor558c0322009-10-14 23:41:34 +00005431/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005432///
5433/// \returns true if a serious error occurs, false otherwise.
5434static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005435 SourceLocation InstLoc,
5436 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005437 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5438 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005439
Douglas Gregor669eed82010-07-13 00:10:04 +00005440 if (CurContext->isRecord()) {
5441 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5442 << D;
5443 return true;
5444 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005445
Douglas Gregor558c0322009-10-14 23:41:34 +00005446 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005447 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005448 // template.
5449 //
5450 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005451 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005452 !CurContext->Encloses(OrigContext)) {
5453 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005454 S.Diag(InstLoc,
5455 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005456 diag::err_explicit_instantiation_out_of_scope
5457 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005458 << D << NS;
5459 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005460 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005461 S.getLangOptions().CPlusPlus0x?
5462 diag::err_explicit_instantiation_must_be_global
5463 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005464 << D;
5465 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005466 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005467 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005468
Douglas Gregor558c0322009-10-14 23:41:34 +00005469 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005470 // If the name declared in the explicit instantiation is an unqualified
5471 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005472 // its template is declared or, if that namespace is inline (7.3.1), any
5473 // namespace from its enclosing namespace set.
5474 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005475 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005476
5477 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005478 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005479
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005480 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005481 S.getLangOptions().CPlusPlus0x?
5482 diag::err_explicit_instantiation_unqualified_wrong_namespace
5483 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005484 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005485 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005486 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005487}
5488
5489/// \brief Determine whether the given scope specifier has a template-id in it.
5490static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5491 if (!SS.isSet())
5492 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005493
Douglas Gregor558c0322009-10-14 23:41:34 +00005494 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005495 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005496 // or a static data member of a class template specialization, the name of
5497 // the class template specialization in the qualified-id for the member
5498 // name shall be a simple-template-id.
5499 //
5500 // C++98 has the same restriction, just worded differently.
5501 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5502 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005503 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005504 if (isa<TemplateSpecializationType>(T))
5505 return true;
5506
5507 return false;
5508}
5509
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005510// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005511DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005512Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005513 SourceLocation ExternLoc,
5514 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005515 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005516 SourceLocation KWLoc,
5517 const CXXScopeSpec &SS,
5518 TemplateTy TemplateD,
5519 SourceLocation TemplateNameLoc,
5520 SourceLocation LAngleLoc,
5521 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005522 SourceLocation RAngleLoc,
5523 AttributeList *Attr) {
5524 // Find the class template we're specializing
5525 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005526 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005527 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5528
5529 // Check that the specialization uses the same tag kind as the
5530 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005531 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5532 assert(Kind != TTK_Enum &&
5533 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005534 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005535 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005536 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005537 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005538 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005539 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005540 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005541 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005542 diag::note_previous_use);
5543 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5544 }
5545
Douglas Gregor558c0322009-10-14 23:41:34 +00005546 // C++0x [temp.explicit]p2:
5547 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005548 // definition and an explicit instantiation declaration. An explicit
5549 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005550 TemplateSpecializationKind TSK
5551 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5552 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005553
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005554 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005555 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005556 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005557
5558 // Check that the template argument list is well-formed for this
5559 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005560 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005561 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5562 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005563 return true;
5564
Douglas Gregor910f8002010-11-07 23:05:16 +00005565 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005566 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005567
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005568 // Find the class template specialization declaration that
5569 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005570 void *InsertPos = 0;
5571 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005572 = ClassTemplate->findSpecialization(Converted.data(),
5573 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005574
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005575 TemplateSpecializationKind PrevDecl_TSK
5576 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5577
Douglas Gregord5cb8762009-10-07 00:13:32 +00005578 // C++0x [temp.explicit]p2:
5579 // [...] An explicit instantiation shall appear in an enclosing
5580 // namespace of its template. [...]
5581 //
5582 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005583 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5584 SS.isSet()))
5585 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005586
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005587 ClassTemplateSpecializationDecl *Specialization = 0;
5588
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005589 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005590 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005591 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005592 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005593 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005594 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005595 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005596
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005597 // Even though HasNoEffect == true means that this explicit instantiation
5598 // has no effect on semantics, we go on to put its syntax in the AST.
5599
5600 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5601 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005602 // Since the only prior class template specialization with these
5603 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005604 // declaration node as our own, updating the source location
5605 // for the template name to reflect our new declaration.
5606 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005607 Specialization = PrevDecl;
5608 Specialization->setLocation(TemplateNameLoc);
5609 PrevDecl = 0;
5610 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005611 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005612
Douglas Gregor52604ab2009-09-11 21:19:12 +00005613 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005614 // Create a new class template specialization declaration node for
5615 // this explicit specialization.
5616 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005617 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005618 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005619 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005620 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005621 Converted.data(),
5622 Converted.size(),
5623 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005624 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005625
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005626 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005627 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005628 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005629 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005630 }
5631
5632 // Build the fully-sugared type for this explicit instantiation as
5633 // the user wrote in the explicit instantiation itself. This means
5634 // that we'll pretty-print the type retrieved from the
5635 // specialization's declaration the way that the user actually wrote
5636 // the explicit instantiation, rather than formatting the name based
5637 // on the "canonical" representation used to store the template
5638 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005639 TypeSourceInfo *WrittenTy
5640 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5641 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005642 Context.getTypeDeclType(Specialization));
5643 Specialization->setTypeAsWritten(WrittenTy);
5644 TemplateArgsIn.release();
5645
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005646 // Set source locations for keywords.
5647 Specialization->setExternLoc(ExternLoc);
5648 Specialization->setTemplateKeywordLoc(TemplateLoc);
5649
5650 // Add the explicit instantiation into its lexical context. However,
5651 // since explicit instantiations are never found by name lookup, we
5652 // just put it into the declaration context directly.
5653 Specialization->setLexicalDeclContext(CurContext);
5654 CurContext->addDecl(Specialization);
5655
5656 // Syntax is now OK, so return if it has no other effect on semantics.
5657 if (HasNoEffect) {
5658 // Set the template specialization kind.
5659 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005660 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005661 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005662
5663 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005664 // A definition of a class template or class member template
5665 // shall be in scope at the point of the explicit instantiation of
5666 // the class template or class member template.
5667 //
5668 // This check comes when we actually try to perform the
5669 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005670 ClassTemplateSpecializationDecl *Def
5671 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005672 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005673 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005674 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005675 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005676 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005677 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5678 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005679
Douglas Gregor0d035142009-10-27 18:42:08 +00005680 // Instantiate the members of this class template specialization.
5681 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005682 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005683 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005684 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5685
5686 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5687 // TSK_ExplicitInstantiationDefinition
5688 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5689 TSK == TSK_ExplicitInstantiationDefinition)
5690 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005691
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005692 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005693 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005694
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005695 // Set the template specialization kind.
5696 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005697 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005698}
5699
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005700// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005701DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005702Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005703 SourceLocation ExternLoc,
5704 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005705 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005706 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005707 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005708 IdentifierInfo *Name,
5709 SourceLocation NameLoc,
5710 AttributeList *Attr) {
5711
Douglas Gregor402abb52009-05-28 23:31:59 +00005712 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005713 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005714 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005715 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5716 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005717 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005718 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005719 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5720
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005721 if (!TagD)
5722 return true;
5723
John McCalld226f652010-08-21 09:40:31 +00005724 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005725 if (Tag->isEnum()) {
5726 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5727 << Context.getTypeDeclType(Tag);
5728 return true;
5729 }
5730
Douglas Gregord0c87372009-05-27 17:30:49 +00005731 if (Tag->isInvalidDecl())
5732 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005734 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5735 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5736 if (!Pattern) {
5737 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5738 << Context.getTypeDeclType(Record);
5739 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5740 return true;
5741 }
5742
Douglas Gregor558c0322009-10-14 23:41:34 +00005743 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005744 // If the explicit instantiation is for a class or member class, the
5745 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005746 // simple-template-id.
5747 //
5748 // C++98 has the same restriction, just worded differently.
5749 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005750 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005751 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005752
Douglas Gregor558c0322009-10-14 23:41:34 +00005753 // C++0x [temp.explicit]p2:
5754 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005756 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005757 TemplateSpecializationKind TSK
5758 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5759 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005760
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005761 // C++0x [temp.explicit]p2:
5762 // [...] An explicit instantiation shall appear in an enclosing
5763 // namespace of its template. [...]
5764 //
5765 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005766 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005767
Douglas Gregor454885e2009-10-15 15:54:05 +00005768 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005770 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005771 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005772 PrevDecl = Record;
5773 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005774 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005775 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005776 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005777 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005778 PrevDecl,
5779 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005781 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005782 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005783 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005784 return TagD;
5785 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005786
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005787 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005788 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005789 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005790 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005792 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005793 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005794 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005795 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005796 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5797 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005798 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5799 << Pattern;
5800 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005801 } else {
5802 if (InstantiateClass(NameLoc, Record, Def,
5803 getTemplateInstantiationArgs(Record),
5804 TSK))
5805 return true;
5806
Douglas Gregor952b0172010-02-11 01:04:33 +00005807 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005808 if (!RecordDef)
5809 return true;
5810 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811 }
5812
Douglas Gregor0d035142009-10-27 18:42:08 +00005813 // Instantiate all of the members of the class.
5814 InstantiateClassMembers(NameLoc, RecordDef,
5815 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005816
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005817 if (TSK == TSK_ExplicitInstantiationDefinition)
5818 MarkVTableUsed(NameLoc, RecordDef, true);
5819
Mike Stump390b4cc2009-05-16 07:39:55 +00005820 // FIXME: We don't have any representation for explicit instantiations of
5821 // member classes. Such a representation is not needed for compilation, but it
5822 // should be available for clients that want to see all of the declarations in
5823 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005824 return TagD;
5825}
5826
John McCallf312b1e2010-08-26 23:41:50 +00005827DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5828 SourceLocation ExternLoc,
5829 SourceLocation TemplateLoc,
5830 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005831 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005832 // TODO: check if/when DNInfo should replace Name.
5833 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5834 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005835 if (!Name) {
5836 if (!D.isInvalidType())
5837 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5838 diag::err_explicit_instantiation_requires_name)
5839 << D.getDeclSpec().getSourceRange()
5840 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005841
Douglas Gregord5a423b2009-09-25 18:43:00 +00005842 return true;
5843 }
5844
5845 // The scope passed in may not be a decl scope. Zip up the scope tree until
5846 // we find one that is.
5847 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5848 (S->getFlags() & Scope::TemplateParamScope) != 0)
5849 S = S->getParent();
5850
5851 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005852 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5853 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005854 if (R.isNull())
5855 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005856
Douglas Gregord5a423b2009-09-25 18:43:00 +00005857 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5858 // Cannot explicitly instantiate a typedef.
5859 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5860 << Name;
5861 return true;
5862 }
5863
Douglas Gregor663b5a02009-10-14 20:14:33 +00005864 // C++0x [temp.explicit]p1:
5865 // [...] An explicit instantiation of a function template shall not use the
5866 // inline or constexpr specifiers.
5867 // Presumably, this also applies to member functions of class templates as
5868 // well.
5869 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005870 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005871 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005872 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873
Douglas Gregor663b5a02009-10-14 20:14:33 +00005874 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875
Douglas Gregor558c0322009-10-14 23:41:34 +00005876 // C++0x [temp.explicit]p2:
5877 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005878 // definition and an explicit instantiation declaration. An explicit
5879 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005880 TemplateSpecializationKind TSK
5881 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5882 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005883
Abramo Bagnara25777432010-08-11 22:01:17 +00005884 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005885 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005886
5887 if (!R->isFunctionType()) {
5888 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889 // A [...] static data member of a class template can be explicitly
5890 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005891 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005892 if (Previous.isAmbiguous())
5893 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
John McCall1bcee0a2009-12-02 08:25:40 +00005895 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005896 if (!Prev || !Prev->isStaticDataMember()) {
5897 // We expect to see a data data member here.
5898 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5899 << Name;
5900 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5901 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005902 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005903 return true;
5904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005905
Douglas Gregord5a423b2009-09-25 18:43:00 +00005906 if (!Prev->getInstantiatedFromStaticDataMember()) {
5907 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005908 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005909 diag::err_explicit_instantiation_data_member_not_instantiated)
5910 << Prev;
5911 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5912 // FIXME: Can we provide a note showing where this was declared?
5913 return true;
5914 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005915
Douglas Gregor558c0322009-10-14 23:41:34 +00005916 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005917 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005918 // or a static data member of a class template specialization, the name of
5919 // the class template specialization in the qualified-id for the member
5920 // name shall be a simple-template-id.
5921 //
5922 // C++98 has the same restriction, just worded differently.
5923 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005924 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005925 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005926 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005927
Douglas Gregor558c0322009-10-14 23:41:34 +00005928 // Check the scope of this explicit instantiation.
5929 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005930
Douglas Gregor454885e2009-10-15 15:54:05 +00005931 // Verify that it is okay to explicitly instantiate here.
5932 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5933 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005934 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005935 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005936 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005937 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005938 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005939 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005940 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005941 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005942
Douglas Gregord5a423b2009-09-25 18:43:00 +00005943 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005944 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005945 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005946 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005947
Douglas Gregord5a423b2009-09-25 18:43:00 +00005948 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005949 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005950 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005951
5952 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005953 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005954 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005955 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005956 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5957 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005958 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5959 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005960 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5961 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005962 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005963 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005964 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005965 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005966 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005967
Douglas Gregord5a423b2009-09-25 18:43:00 +00005968 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005969 // A [...] function [...] can be explicitly instantiated from its template.
5970 // A member function [...] of a class template can be explicitly
5971 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005972 // template.
John McCallc373d482010-01-27 01:50:18 +00005973 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005974 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5975 P != PEnd; ++P) {
5976 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005977 if (!HasExplicitTemplateArgs) {
5978 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5979 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5980 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005981
John McCallc373d482010-01-27 01:50:18 +00005982 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005983 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5984 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005985 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005986 }
5987 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005988
Douglas Gregord5a423b2009-09-25 18:43:00 +00005989 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5990 if (!FunTmpl)
5991 continue;
5992
John McCall5769d612010-02-08 23:07:23 +00005993 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005994 FunctionDecl *Specialization = 0;
5995 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005996 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005997 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005998 R, Specialization, Info)) {
5999 // FIXME: Keep track of almost-matches?
6000 (void)TDK;
6001 continue;
6002 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006003
John McCallc373d482010-01-27 01:50:18 +00006004 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006006
Douglas Gregord5a423b2009-09-25 18:43:00 +00006007 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006008 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006009 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006011 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6012 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6013 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006014
John McCallc373d482010-01-27 01:50:18 +00006015 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006016 return true;
John McCallc373d482010-01-27 01:50:18 +00006017
6018 // Ignore access control bits, we don't need them for redeclaration checking.
6019 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020
Douglas Gregor0a897e32009-10-15 17:21:20 +00006021 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006022 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006023 diag::err_explicit_instantiation_member_function_not_instantiated)
6024 << Specialization
6025 << (Specialization->getTemplateSpecializationKind() ==
6026 TSK_ExplicitSpecialization);
6027 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6028 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029 }
6030
Douglas Gregor0a897e32009-10-15 17:21:20 +00006031 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006032 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6033 PrevDecl = Specialization;
6034
Douglas Gregor0a897e32009-10-15 17:21:20 +00006035 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006036 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006037 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006038 PrevDecl,
6039 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006040 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006041 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006042 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006043
Douglas Gregor0a897e32009-10-15 17:21:20 +00006044 // FIXME: We may still want to build some representation of this
6045 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006046 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006047 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006048 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006049
6050 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006051
Douglas Gregor0a897e32009-10-15 17:21:20 +00006052 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006053 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006054
Douglas Gregor558c0322009-10-14 23:41:34 +00006055 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006056 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006057 // or a static data member of a class template specialization, the name of
6058 // the class template specialization in the qualified-id for the member
6059 // name shall be a simple-template-id.
6060 //
6061 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006062 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006063 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006064 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006065 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006066 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006067 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006068 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006069
Douglas Gregor558c0322009-10-14 23:41:34 +00006070 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006071 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006072 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006073 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006074 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006075
Douglas Gregord5a423b2009-09-25 18:43:00 +00006076 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006077 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006078}
6079
John McCallf312b1e2010-08-26 23:41:50 +00006080TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006081Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6082 const CXXScopeSpec &SS, IdentifierInfo *Name,
6083 SourceLocation TagLoc, SourceLocation NameLoc) {
6084 // This has to hold, because SS is expected to be defined.
6085 assert(Name && "Expected a name in a dependent tag");
6086
6087 NestedNameSpecifier *NNS
6088 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6089 if (!NNS)
6090 return true;
6091
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006092 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006093
Douglas Gregor48c89f42010-04-24 16:38:41 +00006094 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6095 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006096 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006097 return true;
6098 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006099
Douglas Gregor059101f2011-03-02 00:47:37 +00006100 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006101 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006102 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6103
6104 // Create type-source location information for this type.
6105 TypeLocBuilder TLB;
6106 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6107 TL.setKeywordLoc(TagLoc);
6108 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6109 TL.setNameLoc(NameLoc);
6110 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006111}
6112
John McCallf312b1e2010-08-26 23:41:50 +00006113TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006114Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6115 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006116 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006117 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006118 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006119
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006120 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6121 !getLangOptions().CPlusPlus0x)
6122 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006123 << FixItHint::CreateRemoval(TypenameLoc);
6124
Douglas Gregor2494dd02011-03-01 01:34:45 +00006125 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006126 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6127 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006128 if (T.isNull())
6129 return true;
John McCall63b43852010-04-29 23:50:39 +00006130
6131 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6132 if (isa<DependentNameType>(T)) {
6133 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006134 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006135 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006136 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006137 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006138 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006139 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006140 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006141 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006142 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006143
John McCallb3d87482010-08-24 05:47:05 +00006144 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006145}
6146
John McCallf312b1e2010-08-26 23:41:50 +00006147TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006148Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6149 const CXXScopeSpec &SS,
6150 SourceLocation TemplateLoc,
6151 TemplateTy TemplateIn,
6152 SourceLocation TemplateNameLoc,
6153 SourceLocation LAngleLoc,
6154 ASTTemplateArgsPtr TemplateArgsIn,
6155 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006156 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6157 !getLangOptions().CPlusPlus0x)
6158 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006159 << FixItHint::CreateRemoval(TypenameLoc);
6160
6161 // Translate the parser's template argument list in our AST format.
6162 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6163 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6164
6165 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006166 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6167 // Construct a dependent template specialization type.
6168 assert(DTN && "dependent template has non-dependent name?");
6169 assert(DTN->getQualifier()
6170 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6171 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6172 DTN->getQualifier(),
6173 DTN->getIdentifier(),
6174 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006175
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006176 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006177 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006178 DependentTemplateSpecializationTypeLoc SpecTL
6179 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006180 SpecTL.setLAngleLoc(LAngleLoc);
6181 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006182 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006183 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006184 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006185 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6186 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006187 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006188 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006189
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006190 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6191 if (T.isNull())
6192 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006193
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006194 // Provide source-location information for the template specialization
6195 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006196 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006197 TemplateSpecializationTypeLoc SpecTL
6198 = Builder.push<TemplateSpecializationTypeLoc>(T);
6199
6200 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006201 SpecTL.setLAngleLoc(LAngleLoc);
6202 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006203 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006204 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6205 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6206
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006207 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6208 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6209 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006210 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6211
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006212 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6213 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006214}
6215
Douglas Gregora02411e2011-02-27 22:46:49 +00006216
Douglas Gregord57959a2009-03-27 23:10:48 +00006217/// \brief Build the type that describes a C++ typename specifier,
6218/// e.g., "typename T::type".
6219QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006220Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6221 SourceLocation KeywordLoc,
6222 NestedNameSpecifierLoc QualifierLoc,
6223 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006224 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006225 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006226 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006227
John McCall77bb1aa2010-05-01 00:40:08 +00006228 DeclContext *Ctx = computeDeclContext(SS);
6229 if (!Ctx) {
6230 // If the nested-name-specifier is dependent and couldn't be
6231 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006232 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6233 return Context.getDependentNameType(Keyword,
6234 QualifierLoc.getNestedNameSpecifier(),
6235 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006236 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006237
John McCall77bb1aa2010-05-01 00:40:08 +00006238 // If the nested-name-specifier refers to the current instantiation,
6239 // the "typename" keyword itself is superfluous. In C++03, the
6240 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6241 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006242 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006243
John McCall77bb1aa2010-05-01 00:40:08 +00006244 if (RequireCompleteDeclContext(SS, Ctx))
6245 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006246
6247 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006248 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006249 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006250 unsigned DiagID = 0;
6251 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006252 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006253 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006254 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006255 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006256
6257 case LookupResult::FoundUnresolvedValue: {
6258 // We found a using declaration that is a value. Most likely, the using
6259 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006260 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006261 IILoc);
6262 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6263 << Name << Ctx << FullRange;
6264 if (UnresolvedUsingValueDecl *Using
6265 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006266 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006267 Diag(Loc, diag::note_using_value_decl_missing_typename)
6268 << FixItHint::CreateInsertion(Loc, "typename ");
6269 }
6270 }
6271 // Fall through to create a dependent typename type, from which we can recover
6272 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006273
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006274 case LookupResult::NotFoundInCurrentInstantiation:
6275 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006276 return Context.getDependentNameType(Keyword,
6277 QualifierLoc.getNestedNameSpecifier(),
6278 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006279
6280 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006281 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006282 // We found a type. Build an ElaboratedType, since the
6283 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006284 return Context.getElaboratedType(ETK_Typename,
6285 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006286 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006287 }
6288
6289 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006290 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006291 break;
6292
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006293
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006294 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006295 return QualType();
6296
Douglas Gregord57959a2009-03-27 23:10:48 +00006297 case LookupResult::FoundOverloaded:
6298 DiagID = diag::err_typename_nested_not_type;
6299 Referenced = *Result.begin();
6300 break;
6301
John McCall6e247262009-10-10 05:48:19 +00006302 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006303 return QualType();
6304 }
6305
6306 // If we get here, it's because name lookup did not find a
6307 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006308 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006309 IILoc);
6310 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006311 if (Referenced)
6312 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6313 << Name;
6314 return QualType();
6315}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006316
6317namespace {
6318 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006319 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006320 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006321 SourceLocation Loc;
6322 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006323
Douglas Gregor4a959d82009-08-06 16:20:37 +00006324 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006325 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006326
Mike Stump1eb44332009-09-09 15:08:12 +00006327 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006328 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006329 DeclarationName Entity)
6330 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006331 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006332
6333 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006334 /// transformed.
6335 ///
6336 /// For the purposes of type reconstruction, a type has already been
6337 /// transformed if it is NULL or if it is not dependent.
6338 bool AlreadyTransformed(QualType T) {
6339 return T.isNull() || !T->isDependentType();
6340 }
Mike Stump1eb44332009-09-09 15:08:12 +00006341
6342 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006343 /// rebuilt.
6344 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006345
Douglas Gregor4a959d82009-08-06 16:20:37 +00006346 /// \brief Returns the name of the entity whose type is being rebuilt.
6347 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006348
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006349 /// \brief Sets the "base" location and entity when that
6350 /// information is known based on another transformation.
6351 void setBase(SourceLocation Loc, DeclarationName Entity) {
6352 this->Loc = Loc;
6353 this->Entity = Entity;
6354 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006355 };
6356}
6357
Douglas Gregor4a959d82009-08-06 16:20:37 +00006358/// \brief Rebuilds a type within the context of the current instantiation.
6359///
Mike Stump1eb44332009-09-09 15:08:12 +00006360/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006361/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006362/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006363/// partial specialization thereof). This routine will rebuild that type now
6364/// that we have entered the declarator's scope, which may produce different
6365/// canonical types, e.g.,
6366///
6367/// \code
6368/// template<typename T>
6369/// struct X {
6370/// typedef T* pointer;
6371/// pointer data();
6372/// };
6373///
6374/// template<typename T>
6375/// typename X<T>::pointer X<T>::data() { ... }
6376/// \endcode
6377///
Douglas Gregor4714c122010-03-31 17:34:00 +00006378/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006379/// since we do not know that we can look into X<T> when we parsed the type.
6380/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006381/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006382/// as the canonical type of T*, allowing the return types of the out-of-line
6383/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006384TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6385 SourceLocation Loc,
6386 DeclarationName Name) {
6387 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006388 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006389
Douglas Gregor4a959d82009-08-06 16:20:37 +00006390 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6391 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006392}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006393
John McCall60d7b3a2010-08-24 06:29:42 +00006394ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006395 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6396 DeclarationName());
6397 return Rebuilder.TransformExpr(E);
6398}
6399
John McCall63b43852010-04-29 23:50:39 +00006400bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006401 if (SS.isInvalid())
6402 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006403
Douglas Gregor7e384942011-02-25 16:07:42 +00006404 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006405 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6406 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006407 NestedNameSpecifierLoc Rebuilt
6408 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6409 if (!Rebuilt)
6410 return true;
John McCall63b43852010-04-29 23:50:39 +00006411
Douglas Gregor7e384942011-02-25 16:07:42 +00006412 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006413 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006414}
6415
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006416/// \brief Produces a formatted string that describes the binding of
6417/// template parameters to template arguments.
6418std::string
6419Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6420 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006421 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006422}
6423
6424std::string
6425Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6426 const TemplateArgument *Args,
6427 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006428 llvm::SmallString<128> Str;
6429 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006430
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006431 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006432 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006433
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006434 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006435 if (I >= NumArgs)
6436 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006437
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006438 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006439 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006440 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006441 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006442
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006443 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006444 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006445 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006446 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006447 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006448
Douglas Gregor87dd6972010-12-20 16:52:59 +00006449 Out << " = ";
6450 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006451 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006452
6453 Out << ']';
6454 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006455}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006456
6457void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6458 if (!FD)
6459 return;
6460 FD->setLateTemplateParsed(Flag);
6461}
6462
6463bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6464 DeclContext *DC = CurContext;
6465
6466 while (DC) {
6467 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6468 const FunctionDecl *FD = RD->isLocalClass();
6469 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6470 } else if (DC->isTranslationUnit() || DC->isNamespace())
6471 return false;
6472
6473 DC = DC->getParent();
6474 }
6475 return false;
6476}