blob: fa2182a2aadc910c6d7e2982fff1fa0c4c91c418 [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() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000605 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000606 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000607 // -- std::nullptr_t.
608 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000609 // If T is a dependent type, we can't do the check now, so we
610 // assume that it is well-formed.
611 T->isDependentType())
612 return T;
613 // C++ [temp.param]p8:
614 //
615 // A non-type template-parameter of type "array of T" or
616 // "function returning T" is adjusted to be of type "pointer to
617 // T" or "pointer to function returning T", respectively.
618 else if (T->isArrayType())
619 // FIXME: Keep the type prior to promotion?
620 return Context.getArrayDecayedType(T);
621 else if (T->isFunctionType())
622 // FIXME: Keep the type prior to promotion?
623 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000624
Douglas Gregor2943aed2009-03-03 04:44:36 +0000625 Diag(Loc, diag::err_template_nontype_parm_bad_type)
626 << T;
627
628 return QualType();
629}
630
John McCalld226f652010-08-21 09:40:31 +0000631Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
632 unsigned Depth,
633 unsigned Position,
634 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000635 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000636 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
637 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000638
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000639 assert(S->isTemplateParamScope() &&
640 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 bool Invalid = false;
642
643 IdentifierInfo *ParamName = D.getIdentifier();
644 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000645 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000646 LookupOrdinaryName,
647 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000648 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000650 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 }
652
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000653 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
654 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000655 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000656 Invalid = true;
657 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000658
Douglas Gregor10738d32010-12-23 23:51:58 +0000659 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000661 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000662 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000663 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000664 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000665 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000666 Param->setAccess(AS_public);
667
Douglas Gregor72c3f312008-12-05 18:15:24 +0000668 if (Invalid)
669 Param->setInvalidDecl();
670
671 if (D.getIdentifier()) {
672 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000673 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000674 IdResolver.AddDecl(Param);
675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000676
Douglas Gregor61c4d282011-01-05 15:48:55 +0000677 // C++0x [temp.param]p9:
678 // A default template-argument may be specified for any kind of
679 // template-parameter that is not a template parameter pack.
680 if (Default && IsParameterPack) {
681 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
682 Default = 0;
683 }
684
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000686 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000687 // Check for unexpanded parameter packs.
688 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
689 return Param;
690
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000691 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000692 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
693 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000695 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000696 }
John Wiegley429bb272011-04-08 18:41:53 +0000697 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
John McCall9ae2f072010-08-23 23:25:46 +0000699 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000701
John McCalld226f652010-08-21 09:40:31 +0000702 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000703}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000704
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000705/// ActOnTemplateTemplateParameter - Called when a C++ template template
706/// parameter (e.g. T in template <template <typename> class T> class array)
707/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000708Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
709 SourceLocation TmpLoc,
710 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000711 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000712 IdentifierInfo *Name,
713 SourceLocation NameLoc,
714 unsigned Depth,
715 unsigned Position,
716 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000717 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000718 assert(S->isTemplateParamScope() &&
719 "Template template parameter not in template parameter scope!");
720
721 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000722 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000724 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000725 NameLoc.isInvalid()? TmpLoc : NameLoc,
726 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000727 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000728 Param->setAccess(AS_public);
729
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000730 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000731 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000733 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000734 IdResolver.AddDecl(Param);
735 }
736
Douglas Gregor6f526752010-12-16 08:48:57 +0000737 if (Params->size() == 0) {
738 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
739 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
740 Param->setInvalidDecl();
741 }
742
Douglas Gregor61c4d282011-01-05 15:48:55 +0000743 // C++0x [temp.param]p9:
744 // A default template-argument may be specified for any kind of
745 // template-parameter that is not a template parameter pack.
746 if (IsParameterPack && !Default.isInvalid()) {
747 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
748 Default = ParsedTemplateArgument();
749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000751 if (!Default.isInvalid()) {
752 // Check only that we have a template template argument. We don't want to
753 // try to check well-formedness now, because our template template parameter
754 // might have dependent types in its template parameters, which we wouldn't
755 // be able to match now.
756 //
757 // If none of the template template parameter's template arguments mention
758 // other template parameters, we could actually perform more checking here.
759 // However, it isn't worth doing.
760 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
761 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
762 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
763 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000764 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766
Douglas Gregor6f526752010-12-16 08:48:57 +0000767 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000768 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000769 DefaultArg.getArgument().getAsTemplate(),
770 UPPC_DefaultArgument))
771 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000772
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000773 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
John McCalld226f652010-08-21 09:40:31 +0000776 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000777}
778
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779/// ActOnTemplateParameterList - Builds a TemplateParameterList that
780/// contains the template parameters in Params/NumParams.
781Sema::TemplateParamsTy *
782Sema::ActOnTemplateParameterList(unsigned Depth,
783 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000784 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000785 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000786 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000787 SourceLocation RAngleLoc) {
788 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000789 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000790
Douglas Gregorddc29e12009-02-06 22:42:48 +0000791 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000793 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000795
John McCallb6217662010-03-15 10:12:16 +0000796static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
797 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000798 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000799}
800
John McCallf312b1e2010-08-26 23:41:50 +0000801DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000802Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000803 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804 IdentifierInfo *Name, SourceLocation NameLoc,
805 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000806 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000807 AccessSpecifier AS,
808 unsigned NumOuterTemplateParamLists,
809 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000810 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000811 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000812 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000813 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000814
815 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000817 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000818
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000819 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
820 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000821
822 // There is no such thing as an unnamed class template.
823 if (!Name) {
824 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000825 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000826 }
827
828 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000830 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000831 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 if (SS.isNotEmpty() && !SS.isInvalid()) {
833 SemanticContext = computeDeclContext(SS, true);
834 if (!SemanticContext) {
835 // FIXME: Produce a reasonable diagnostic here
836 return true;
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
John McCall77bb1aa2010-05-01 00:40:08 +0000839 if (RequireCompleteDeclContext(SS, SemanticContext))
840 return true;
841
John McCalla24dc2e2009-11-17 02:14:36 +0000842 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000843 } else {
844 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000845 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor57265e32010-04-12 16:00:01 +0000848 if (Previous.isAmbiguous())
849 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850
Douglas Gregorddc29e12009-02-06 22:42:48 +0000851 NamedDecl *PrevDecl = 0;
852 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000853 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000854
Douglas Gregorddc29e12009-02-06 22:42:48 +0000855 // If there is a previous declaration with the same name, check
856 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000857 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000859
860 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000861 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000862 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000863 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000864 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
865 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000867 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
868 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
869 PrevClassTemplate
870 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
871 ->getSpecializedTemplate();
872 }
873 }
874
John McCall65c49462009-12-18 11:25:59 +0000875 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000876 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // [...] When looking for a prior declaration of a class or a function
878 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000879 // function is neither a qualified name nor a template-id, scopes outside
880 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000881 if (!SS.isSet()) {
882 DeclContext *OutermostContext = CurContext;
883 while (!OutermostContext->isFileContext())
884 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000885
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000886 if (PrevDecl &&
887 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
888 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
889 SemanticContext = PrevDecl->getDeclContext();
890 } else {
891 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000892 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000893 // declaration.
894 PrevDecl = PrevClassTemplate = 0;
895 SemanticContext = OutermostContext;
896 }
John McCalle129d442009-12-17 23:21:11 +0000897 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000898
John McCalle129d442009-12-17 23:21:11 +0000899 if (CurContext->isDependentContext()) {
900 // If this is a dependent context, we don't want to link the friend
901 // class template to the template in scope, because that would perform
902 // checking of the template parameter lists that can't be performed
903 // until the outer context is instantiated.
904 PrevDecl = PrevClassTemplate = 0;
905 }
906 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
907 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000908
Douglas Gregorddc29e12009-02-06 22:42:48 +0000909 if (PrevClassTemplate) {
910 // Ensure that the template parameter lists are compatible.
911 if (!TemplateParameterListsAreEqual(TemplateParams,
912 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000913 /*Complain=*/true,
914 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000915 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916
917 // C++ [temp.class]p4:
918 // In a redeclaration, partial specialization, explicit
919 // specialization or explicit instantiation of a class template,
920 // the class-key shall agree in kind with the original class
921 // template declaration (7.1.5.3).
922 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000923 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
924 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000925 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000926 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000927 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000928 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000929 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000930 }
931
Douglas Gregorddc29e12009-02-06 22:42:48 +0000932 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000933 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000934 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000935 Diag(NameLoc, diag::err_redefinition) << Name;
936 Diag(Def->getLocation(), diag::note_previous_definition);
937 // FIXME: Would it make sense to try to "forget" the previous
938 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000939 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000940 }
941 }
942 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
943 // Maybe we will complain about the shadowed template parameter.
944 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
945 // Just pretend that we didn't see the previous declaration.
946 PrevDecl = 0;
947 } else if (PrevDecl) {
948 // C++ [temp]p5:
949 // A class template shall not have the same name as any other
950 // template, class, function, object, enumeration, enumerator,
951 // namespace, or type in the same scope (3.3), except as specified
952 // in (14.5.4).
953 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
954 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000955 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000956 }
957
Douglas Gregord684b002009-02-10 19:49:53 +0000958 // Check the template parameter list of this declaration, possibly
959 // merging in the template parameter list from the previous class
960 // template declaration.
961 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000962 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000963 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000964 SemanticContext->isRecord() &&
965 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000966 ? TPC_ClassTemplateMember
967 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000968 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregor57265e32010-04-12 16:00:01 +0000970 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000971 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000972 // template out-of-line.
973 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
974 !(TUK == TUK_Friend && CurContext->isDependentContext()))
975 Diag(NameLoc, diag::err_member_def_does_not_match)
976 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000977 }
978
Mike Stump1eb44332009-09-09 15:08:12 +0000979 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000980 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000981 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000982 PrevClassTemplate->getTemplatedDecl() : 0,
983 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000984 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000985 if (NumOuterTemplateParamLists > 0)
986 NewClass->setTemplateParameterListsInfo(Context,
987 NumOuterTemplateParamLists,
988 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000989
990 ClassTemplateDecl *NewTemplate
991 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
992 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000993 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000994 NewClass->setDescribedClassTemplate(NewTemplate);
995
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000996 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000997 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000998 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000999 assert(T->isDependentType() && "Class template type is not dependent?");
1000 (void)T;
1001
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001002 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001003 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001004 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001005 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1006 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001007
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001008 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001009 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001010 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregorddc29e12009-02-06 22:42:48 +00001012 // Set the lexical context of these templates
1013 NewClass->setLexicalDeclContext(CurContext);
1014 NewTemplate->setLexicalDeclContext(CurContext);
1015
John McCall0f434ec2009-07-31 02:45:11 +00001016 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001017 NewClass->startDefinition();
1018
1019 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001020 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001021
John McCall05b23ea2009-09-14 21:59:20 +00001022 if (TUK != TUK_Friend)
1023 PushOnScopeChains(NewTemplate, S);
1024 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001025 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001026 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001027 NewClass->setAccess(PrevClassTemplate->getAccess());
1028 }
John McCall05b23ea2009-09-14 21:59:20 +00001029
Douglas Gregord85bea22009-09-26 06:47:28 +00001030 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1031 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001032
John McCall05b23ea2009-09-14 21:59:20 +00001033 // Friend templates are visible in fairly strange ways.
1034 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001035 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001036 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1037 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1038 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001040 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041
Douglas Gregord85bea22009-09-26 06:47:28 +00001042 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1043 NewClass->getLocation(),
1044 NewTemplate,
1045 /*FIXME:*/NewClass->getLocation());
1046 Friend->setAccess(AS_public);
1047 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001048 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001049
Douglas Gregord684b002009-02-10 19:49:53 +00001050 if (Invalid) {
1051 NewTemplate->setInvalidDecl();
1052 NewClass->setInvalidDecl();
1053 }
John McCalld226f652010-08-21 09:40:31 +00001054 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001055}
1056
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001057/// \brief Diagnose the presence of a default template argument on a
1058/// template parameter, which is ill-formed in certain contexts.
1059///
1060/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001061static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001062 Sema::TemplateParamListContext TPC,
1063 SourceLocation ParamLoc,
1064 SourceRange DefArgRange) {
1065 switch (TPC) {
1066 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001067 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001068 return false;
1069
1070 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001071 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001072 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001073 // A default template-argument shall not be specified in a
1074 // function template declaration or a function template
1075 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001076 // If a friend function template declaration specifies a default
1077 // template-argument, that declaration shall be a definition and shall be
1078 // the only declaration of the function template in the translation unit.
1079 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001081 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001082 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001083 << DefArgRange;
1084 return false;
1085
1086 case Sema::TPC_ClassTemplateMember:
1087 // C++0x [temp.param]p9:
1088 // A default template-argument shall not be specified in the
1089 // template-parameter-lists of the definition of a member of a
1090 // class template that appears outside of the member's class.
1091 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1092 << DefArgRange;
1093 return true;
1094
1095 case Sema::TPC_FriendFunctionTemplate:
1096 // C++ [temp.param]p9:
1097 // A default template-argument shall not be specified in a
1098 // friend template declaration.
1099 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1100 << DefArgRange;
1101 return true;
1102
1103 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1104 // for friend function templates if there is only a single
1105 // declaration (and it is a definition). Strange!
1106 }
1107
1108 return false;
1109}
1110
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001111/// \brief Check for unexpanded parameter packs within the template parameters
1112/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001113static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1114 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001115 TemplateParameterList *Params = TTP->getTemplateParameters();
1116 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1117 NamedDecl *P = Params->getParam(I);
1118 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001119 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001120 NTTP->getTypeSourceInfo(),
1121 Sema::UPPC_NonTypeTemplateParameterType))
1122 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001123
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001124 continue;
1125 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001126
1127 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001128 = dyn_cast<TemplateTemplateParmDecl>(P))
1129 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1130 return true;
1131 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001132
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001133 return false;
1134}
1135
Douglas Gregord684b002009-02-10 19:49:53 +00001136/// \brief Checks the validity of a template parameter list, possibly
1137/// considering the template parameter list from a previous
1138/// declaration.
1139///
1140/// If an "old" template parameter list is provided, it must be
1141/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1142/// template parameter list.
1143///
1144/// \param NewParams Template parameter list for a new template
1145/// declaration. This template parameter list will be updated with any
1146/// default arguments that are carried through from the previous
1147/// template parameter list.
1148///
1149/// \param OldParams If provided, template parameter list from a
1150/// previous declaration of the same template. Default template
1151/// arguments will be merged from the old template parameter list to
1152/// the new template parameter list.
1153///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001154/// \param TPC Describes the context in which we are checking the given
1155/// template parameter list.
1156///
Douglas Gregord684b002009-02-10 19:49:53 +00001157/// \returns true if an error occurred, false otherwise.
1158bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001159 TemplateParameterList *OldParams,
1160 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001161 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregord684b002009-02-10 19:49:53 +00001163 // C++ [temp.param]p10:
1164 // The set of default template-arguments available for use with a
1165 // template declaration or definition is obtained by merging the
1166 // default arguments from the definition (if in scope) and all
1167 // declarations in scope in the same way default function
1168 // arguments are (8.3.6).
1169 bool SawDefaultArgument = false;
1170 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001171
Anders Carlsson49d25572009-06-12 23:20:15 +00001172 bool SawParameterPack = false;
1173 SourceLocation ParameterPackLoc;
1174
Mike Stump1a35fde2009-02-11 23:03:27 +00001175 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001176 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001177 if (OldParams)
1178 OldParam = OldParams->begin();
1179
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001180 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001181 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1182 NewParamEnd = NewParams->end();
1183 NewParam != NewParamEnd; ++NewParam) {
1184 // Variables used to diagnose redundant default arguments
1185 bool RedundantDefaultArg = false;
1186 SourceLocation OldDefaultLoc;
1187 SourceLocation NewDefaultLoc;
1188
1189 // Variables used to diagnose missing default arguments
1190 bool MissingDefaultArg = false;
1191
Anders Carlsson49d25572009-06-12 23:20:15 +00001192 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001193 // If a template parameter of a primary class template or alias template
1194 // is a template parameter pack, it shall be the last template parameter.
1195 if (SawParameterPack &&
1196 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001197 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001198 diag::err_template_param_pack_must_be_last_template_parameter);
1199 Invalid = true;
1200 }
1201
Douglas Gregord684b002009-02-10 19:49:53 +00001202 if (TemplateTypeParmDecl *NewTypeParm
1203 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001204 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001205 if (NewTypeParm->hasDefaultArgument() &&
1206 DiagnoseDefaultTemplateArgument(*this, TPC,
1207 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001208 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001209 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001210 NewTypeParm->removeDefaultArgument();
1211
1212 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001213 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001214 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Anders Carlsson49d25572009-06-12 23:20:15 +00001216 if (NewTypeParm->isParameterPack()) {
1217 assert(!NewTypeParm->hasDefaultArgument() &&
1218 "Parameter packs can't have a default argument!");
1219 SawParameterPack = true;
1220 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001221 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001222 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001223 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1224 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1225 SawDefaultArgument = true;
1226 RedundantDefaultArg = true;
1227 PreviousDefaultArgLoc = NewDefaultLoc;
1228 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1229 // Merge the default argument from the old declaration to the
1230 // new declaration.
1231 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001232 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001233 true);
1234 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1235 } else if (NewTypeParm->hasDefaultArgument()) {
1236 SawDefaultArgument = true;
1237 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1238 } else if (SawDefaultArgument)
1239 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001240 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001241 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001242 // Check for unexpanded parameter packs.
1243 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001244 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001245 UPPC_NonTypeTemplateParameterType)) {
1246 Invalid = true;
1247 continue;
1248 }
1249
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001250 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001251 if (NewNonTypeParm->hasDefaultArgument() &&
1252 DiagnoseDefaultTemplateArgument(*this, TPC,
1253 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001254 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001255 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001256 }
1257
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001258 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001259 NonTypeTemplateParmDecl *OldNonTypeParm
1260 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001261 if (NewNonTypeParm->isParameterPack()) {
1262 assert(!NewNonTypeParm->hasDefaultArgument() &&
1263 "Parameter packs can't have a default argument!");
1264 SawParameterPack = true;
1265 ParameterPackLoc = NewNonTypeParm->getLocation();
1266 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001267 NewNonTypeParm->hasDefaultArgument()) {
1268 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1269 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1270 SawDefaultArgument = true;
1271 RedundantDefaultArg = true;
1272 PreviousDefaultArgLoc = NewDefaultLoc;
1273 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1274 // Merge the default argument from the old declaration to the
1275 // new declaration.
1276 SawDefaultArgument = true;
1277 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001278 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001279 // parameter.
1280 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001281 OldNonTypeParm->getDefaultArgument(),
1282 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001283 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1284 } else if (NewNonTypeParm->hasDefaultArgument()) {
1285 SawDefaultArgument = true;
1286 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1287 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001288 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001289 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001290 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001291 TemplateTemplateParmDecl *NewTemplateParm
1292 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001293
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001294 // Check for unexpanded parameter packs, recursively.
1295 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1296 Invalid = true;
1297 continue;
1298 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001299
1300 if (NewTemplateParm->hasDefaultArgument() &&
1301 DiagnoseDefaultTemplateArgument(*this, TPC,
1302 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001303 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001304 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001305
1306 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001307 TemplateTemplateParmDecl *OldTemplateParm
1308 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001309 if (NewTemplateParm->isParameterPack()) {
1310 assert(!NewTemplateParm->hasDefaultArgument() &&
1311 "Parameter packs can't have a default argument!");
1312 SawParameterPack = true;
1313 ParameterPackLoc = NewTemplateParm->getLocation();
1314 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001315 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001316 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1317 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001318 SawDefaultArgument = true;
1319 RedundantDefaultArg = true;
1320 PreviousDefaultArgLoc = NewDefaultLoc;
1321 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1322 // Merge the default argument from the old declaration to the
1323 // new declaration.
1324 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001325 // FIXME: We need to create a new kind of "default argument" expression
1326 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001327 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001328 OldTemplateParm->getDefaultArgument(),
1329 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001330 PreviousDefaultArgLoc
1331 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001332 } else if (NewTemplateParm->hasDefaultArgument()) {
1333 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001334 PreviousDefaultArgLoc
1335 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001336 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001337 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001338 }
1339
1340 if (RedundantDefaultArg) {
1341 // C++ [temp.param]p12:
1342 // A template-parameter shall not be given default arguments
1343 // by two different declarations in the same scope.
1344 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1345 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1346 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001347 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001348 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001349 // If a template-parameter of a class template has a default
1350 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001351 // have a default template-argument supplied or be a template parameter
1352 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001353 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001354 diag::err_template_param_default_arg_missing);
1355 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1356 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001357 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001358 }
1359
1360 // If we have an old template parameter list that we're merging
1361 // in, move on to the next parameter.
1362 if (OldParams)
1363 ++OldParam;
1364 }
1365
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001366 // We were missing some default arguments at the end of the list, so remove
1367 // all of the default arguments.
1368 if (RemoveDefaultArguments) {
1369 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1370 NewParamEnd = NewParams->end();
1371 NewParam != NewParamEnd; ++NewParam) {
1372 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1373 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001374 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001375 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1376 NTTP->removeDefaultArgument();
1377 else
1378 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1379 }
1380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001381
Douglas Gregord684b002009-02-10 19:49:53 +00001382 return Invalid;
1383}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001384
John McCall4e2cbb22010-10-20 05:44:58 +00001385namespace {
1386
1387/// A class which looks for a use of a certain level of template
1388/// parameter.
1389struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1390 typedef RecursiveASTVisitor<DependencyChecker> super;
1391
1392 unsigned Depth;
1393 bool Match;
1394
1395 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1396 NamedDecl *ND = Params->getParam(0);
1397 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1398 Depth = PD->getDepth();
1399 } else if (NonTypeTemplateParmDecl *PD =
1400 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1401 Depth = PD->getDepth();
1402 } else {
1403 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1404 }
1405 }
1406
1407 bool Matches(unsigned ParmDepth) {
1408 if (ParmDepth >= Depth) {
1409 Match = true;
1410 return true;
1411 }
1412 return false;
1413 }
1414
1415 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1416 return !Matches(T->getDepth());
1417 }
1418
1419 bool TraverseTemplateName(TemplateName N) {
1420 if (TemplateTemplateParmDecl *PD =
1421 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1422 if (Matches(PD->getDepth())) return false;
1423 return super::TraverseTemplateName(N);
1424 }
1425
1426 bool VisitDeclRefExpr(DeclRefExpr *E) {
1427 if (NonTypeTemplateParmDecl *PD =
1428 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1429 if (PD->getDepth() == Depth) {
1430 Match = true;
1431 return false;
1432 }
1433 }
1434 return super::VisitDeclRefExpr(E);
1435 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001436
1437 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1438 return TraverseType(T->getInjectedSpecializationType());
1439 }
John McCall4e2cbb22010-10-20 05:44:58 +00001440};
1441}
1442
Douglas Gregorc8406492011-05-10 18:27:06 +00001443/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001444/// list.
1445static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001446DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001447 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001448 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001449 return Checker.Match;
1450}
1451
Douglas Gregorc8406492011-05-10 18:27:06 +00001452// Find the source range corresponding to the named type in the given
1453// nested-name-specifier, if any.
1454static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1455 QualType T,
1456 const CXXScopeSpec &SS) {
1457 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1458 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1459 if (const Type *CurType = NNS->getAsType()) {
1460 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1461 return NNSLoc.getTypeLoc().getSourceRange();
1462 } else
1463 break;
1464
1465 NNSLoc = NNSLoc.getPrefix();
1466 }
1467
1468 return SourceRange();
1469}
1470
Mike Stump1eb44332009-09-09 15:08:12 +00001471/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001472/// specifier, returning the template parameter list that applies to the
1473/// name.
1474///
1475/// \param DeclStartLoc the start of the declaration that has a scope
1476/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001477///
Douglas Gregorc8406492011-05-10 18:27:06 +00001478/// \param DeclLoc The location of the declaration itself.
1479///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001480/// \param SS the scope specifier that will be matched to the given template
1481/// parameter lists. This scope specifier precedes a qualified name that is
1482/// being declared.
1483///
1484/// \param ParamLists the template parameter lists, from the outermost to the
1485/// innermost template parameter lists.
1486///
1487/// \param NumParamLists the number of template parameter lists in ParamLists.
1488///
John McCall77e8b112010-04-13 20:37:33 +00001489/// \param IsFriend Whether to apply the slightly different rules for
1490/// matching template parameters to scope specifiers in friend
1491/// declarations.
1492///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001493/// \param IsExplicitSpecialization will be set true if the entity being
1494/// declared is an explicit specialization, false otherwise.
1495///
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001498/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001499/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001500/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501/// itself a template).
1502TemplateParameterList *
1503Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001504 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505 const CXXScopeSpec &SS,
1506 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001507 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001508 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001509 bool &IsExplicitSpecialization,
1510 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001511 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001512 Invalid = false;
1513
1514 // The sequence of nested types to which we will match up the template
1515 // parameter lists. We first build this list by starting with the type named
1516 // by the nested-name-specifier and walking out until we run out of types.
1517 llvm::SmallVector<QualType, 4> NestedTypes;
1518 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001519 if (SS.getScopeRep()) {
1520 if (CXXRecordDecl *Record
1521 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1522 T = Context.getTypeDeclType(Record);
1523 else
1524 T = QualType(SS.getScopeRep()->getAsType(), 0);
1525 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001526
1527 // If we found an explicit specialization that prevents us from needing
1528 // 'template<>' headers, this will be set to the location of that
1529 // explicit specialization.
1530 SourceLocation ExplicitSpecLoc;
1531
1532 while (!T.isNull()) {
1533 NestedTypes.push_back(T);
1534
1535 // Retrieve the parent of a record type.
1536 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1537 // If this type is an explicit specialization, we're done.
1538 if (ClassTemplateSpecializationDecl *Spec
1539 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1540 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1541 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1542 ExplicitSpecLoc = Spec->getLocation();
1543 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001544 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001545 } else if (Record->getTemplateSpecializationKind()
1546 == TSK_ExplicitSpecialization) {
1547 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001548 break;
1549 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001550
1551 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1552 T = Context.getTypeDeclType(Parent);
1553 else
1554 T = QualType();
1555 continue;
1556 }
1557
1558 if (const TemplateSpecializationType *TST
1559 = T->getAs<TemplateSpecializationType>()) {
1560 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1561 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1562 T = Context.getTypeDeclType(Parent);
1563 else
1564 T = QualType();
1565 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001566 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001567 }
1568
1569 // Look one step prior in a dependent template specialization type.
1570 if (const DependentTemplateSpecializationType *DependentTST
1571 = T->getAs<DependentTemplateSpecializationType>()) {
1572 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1573 T = QualType(NNS->getAsType(), 0);
1574 else
1575 T = QualType();
1576 continue;
1577 }
1578
1579 // Look one step prior in a dependent name type.
1580 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1581 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1582 T = QualType(NNS->getAsType(), 0);
1583 else
1584 T = QualType();
1585 continue;
1586 }
1587
1588 // Retrieve the parent of an enumeration type.
1589 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1590 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1591 // check here.
1592 EnumDecl *Enum = EnumT->getDecl();
1593
1594 // Get to the parent type.
1595 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1596 T = Context.getTypeDeclType(Parent);
1597 else
1598 T = QualType();
1599 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregorc8406492011-05-10 18:27:06 +00001602 T = QualType();
1603 }
1604 // Reverse the nested types list, since we want to traverse from the outermost
1605 // to the innermost while checking template-parameter-lists.
1606 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001607
Douglas Gregorc8406492011-05-10 18:27:06 +00001608 // C++0x [temp.expl.spec]p17:
1609 // A member or a member template may be nested within many
1610 // enclosing class templates. In an explicit specialization for
1611 // such a member, the member declaration shall be preceded by a
1612 // template<> for each enclosing class template that is
1613 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001614 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001615 unsigned ParamIdx = 0;
1616 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1617 ++TypeIdx) {
1618 T = NestedTypes[TypeIdx];
1619
1620 // Whether we expect a 'template<>' header.
1621 bool NeedEmptyTemplateHeader = false;
1622
1623 // Whether we expect a template header with parameters.
1624 bool NeedNonemptyTemplateHeader = false;
1625
1626 // For a dependent type, the set of template parameters that we
1627 // expect to see.
1628 TemplateParameterList *ExpectedTemplateParams = 0;
1629
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001630 // C++0x [temp.expl.spec]p15:
1631 // A member or a member template may be nested within many enclosing
1632 // class templates. In an explicit specialization for such a member, the
1633 // member declaration shall be preceded by a template<> for each
1634 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001635 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1636 if (ClassTemplatePartialSpecializationDecl *Partial
1637 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1638 ExpectedTemplateParams = Partial->getTemplateParameters();
1639 NeedNonemptyTemplateHeader = true;
1640 } else if (Record->isDependentType()) {
1641 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001642 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001643 ->getTemplateParameters();
1644 NeedNonemptyTemplateHeader = true;
1645 }
1646 } else if (ClassTemplateSpecializationDecl *Spec
1647 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1648 // C++0x [temp.expl.spec]p4:
1649 // Members of an explicitly specialized class template are defined
1650 // in the same manner as members of normal classes, and not using
1651 // the template<> syntax.
1652 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1653 NeedEmptyTemplateHeader = true;
1654 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001655 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001656 } else if (Record->getTemplateSpecializationKind()) {
1657 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001658 != TSK_ExplicitSpecialization &&
1659 TypeIdx == NumTypes - 1)
1660 IsExplicitSpecialization = true;
1661
1662 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001663 }
1664 } else if (const TemplateSpecializationType *TST
1665 = T->getAs<TemplateSpecializationType>()) {
1666 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1667 ExpectedTemplateParams = Template->getTemplateParameters();
1668 NeedNonemptyTemplateHeader = true;
1669 }
1670 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1671 // FIXME: We actually could/should check the template arguments here
1672 // against the corresponding template parameter list.
1673 NeedNonemptyTemplateHeader = false;
1674 }
1675
Douglas Gregor89b9f102011-06-06 15:22:55 +00001676 // C++ [temp.expl.spec]p16:
1677 // In an explicit specialization declaration for a member of a class
1678 // template or a member template that ap- pears in namespace scope, the
1679 // member template and some of its enclosing class templates may remain
1680 // unspecialized, except that the declaration shall not explicitly
1681 // specialize a class member template if its en- closing class templates
1682 // are not explicitly specialized as well.
1683 if (ParamIdx < NumParamLists) {
1684 if (ParamLists[ParamIdx]->size() == 0) {
1685 if (SawNonEmptyTemplateParameterList) {
1686 Diag(DeclLoc, diag::err_specialize_member_of_template)
1687 << ParamLists[ParamIdx]->getSourceRange();
1688 Invalid = true;
1689 IsExplicitSpecialization = false;
1690 return 0;
1691 }
1692 } else
1693 SawNonEmptyTemplateParameterList = true;
1694 }
1695
Douglas Gregorc8406492011-05-10 18:27:06 +00001696 if (NeedEmptyTemplateHeader) {
1697 // If we're on the last of the types, and we need a 'template<>' header
1698 // here, then it's an explicit specialization.
1699 if (TypeIdx == NumTypes - 1)
1700 IsExplicitSpecialization = true;
1701
1702 if (ParamIdx < NumParamLists) {
1703 if (ParamLists[ParamIdx]->size() > 0) {
1704 // The header has template parameters when it shouldn't. Complain.
1705 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1706 diag::err_template_param_list_matches_nontemplate)
1707 << T
1708 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1709 ParamLists[ParamIdx]->getRAngleLoc())
1710 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1711 Invalid = true;
1712 return 0;
1713 }
1714
1715 // Consume this template header.
1716 ++ParamIdx;
1717 continue;
1718 }
1719
1720 if (!IsFriend) {
1721 // We don't have a template header, but we should.
1722 SourceLocation ExpectedTemplateLoc;
1723 if (NumParamLists > 0)
1724 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1725 else
1726 ExpectedTemplateLoc = DeclStartLoc;
1727
1728 Diag(DeclLoc, diag::err_template_spec_needs_header)
1729 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1730 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1731 }
1732
1733 continue;
1734 }
1735
1736 if (NeedNonemptyTemplateHeader) {
1737 // In friend declarations we can have template-ids which don't
1738 // depend on the corresponding template parameter lists. But
1739 // assume that empty parameter lists are supposed to match this
1740 // template-id.
1741 if (IsFriend && T->isDependentType()) {
1742 if (ParamIdx < NumParamLists &&
1743 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1744 ExpectedTemplateParams = 0;
1745 else
1746 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001747 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001748
Douglas Gregorc8406492011-05-10 18:27:06 +00001749 if (ParamIdx < NumParamLists) {
1750 // Check the template parameter list, if we can.
1751 if (ExpectedTemplateParams &&
1752 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1753 ExpectedTemplateParams,
1754 true, TPL_TemplateMatch))
1755 Invalid = true;
1756
1757 if (!Invalid &&
1758 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1759 TPC_ClassTemplateMember))
1760 Invalid = true;
1761
1762 ++ParamIdx;
1763 continue;
1764 }
1765
1766 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1767 << T
1768 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1769 Invalid = true;
1770 continue;
1771 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001772 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001773
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001774 // If there were at least as many template-ids as there were template
1775 // parameter lists, then there are no template parameter lists remaining for
1776 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001777 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001778 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001780 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001781 if (ParamIdx < NumParamLists - 1) {
1782 bool HasAnyExplicitSpecHeader = false;
1783 bool AllExplicitSpecHeaders = true;
1784 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1785 if (ParamLists[I]->size() == 0)
1786 HasAnyExplicitSpecHeader = true;
1787 else
1788 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001789 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001790
1791 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1792 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1793 : diag::err_template_spec_extra_headers)
1794 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1795 ParamLists[NumParamLists - 2]->getRAngleLoc());
1796
1797 // If there was a specialization somewhere, such that 'template<>' is
1798 // not required, and there were any 'template<>' headers, note where the
1799 // specialization occurred.
1800 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1801 Diag(ExplicitSpecLoc,
1802 diag::note_explicit_template_spec_does_not_need_header)
1803 << NestedTypes.back();
1804
1805 // We have a template parameter list with no corresponding scope, which
1806 // means that the resulting template declaration can't be instantiated
1807 // properly (we'll end up with dependent nodes when we shouldn't).
1808 if (!AllExplicitSpecHeaders)
1809 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001810 }
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Douglas Gregor89b9f102011-06-06 15:22:55 +00001812 // C++ [temp.expl.spec]p16:
1813 // In an explicit specialization declaration for a member of a class
1814 // template or a member template that ap- pears in namespace scope, the
1815 // member template and some of its enclosing class templates may remain
1816 // unspecialized, except that the declaration shall not explicitly
1817 // specialize a class member template if its en- closing class templates
1818 // are not explicitly specialized as well.
1819 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1820 SawNonEmptyTemplateParameterList) {
1821 Diag(DeclLoc, diag::err_specialize_member_of_template)
1822 << ParamLists[ParamIdx]->getSourceRange();
1823 Invalid = true;
1824 IsExplicitSpecialization = false;
1825 return 0;
1826 }
1827
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001828 // Return the last template parameter list, which corresponds to the
1829 // entity being declared.
1830 return ParamLists[NumParamLists - 1];
1831}
1832
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001833void Sema::NoteAllFoundTemplates(TemplateName Name) {
1834 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1835 Diag(Template->getLocation(), diag::note_template_declared_here)
1836 << (isa<FunctionTemplateDecl>(Template)? 0
1837 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001838 : isa<TypeAliasTemplateDecl>(Template)? 2
1839 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001840 << Template->getDeclName();
1841 return;
1842 }
1843
1844 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1845 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1846 IEnd = OST->end();
1847 I != IEnd; ++I)
1848 Diag((*I)->getLocation(), diag::note_template_declared_here)
1849 << 0 << (*I)->getDeclName();
1850
1851 return;
1852 }
1853}
1854
1855
Douglas Gregor7532dc62009-03-30 22:58:21 +00001856QualType Sema::CheckTemplateIdType(TemplateName Name,
1857 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001858 TemplateArgumentListInfo &TemplateArgs) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001859 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1860 if (DTN && DTN->isIdentifier())
1861 // When building a template-id where the template-name is dependent,
1862 // assume the template is a type template. Either our assumption is
1863 // correct, or the code is ill-formed and will be diagnosed when the
1864 // dependent name is substituted.
1865 return Context.getDependentTemplateSpecializationType(ETK_None,
1866 DTN->getQualifier(),
1867 DTN->getIdentifier(),
1868 TemplateArgs);
1869
Douglas Gregor7532dc62009-03-30 22:58:21 +00001870 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001871 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1872 // We might have a substituted template template parameter pack. If so,
1873 // build a template specialization type for it.
1874 if (Name.getAsSubstTemplateTemplateParmPack())
1875 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001876
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001877 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1878 << Name;
1879 NoteAllFoundTemplates(Name);
1880 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001881 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001882
Douglas Gregor40808ce2009-03-09 23:48:35 +00001883 // Check that the template argument list is well-formed for this
1884 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001885 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001886 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001887 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001888 return QualType();
1889
Douglas Gregor910f8002010-11-07 23:05:16 +00001890 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001891 "Converted template argument list is too short!");
1892
1893 QualType CanonType;
1894
Richard Smith3e4c6c42011-05-05 21:57:07 +00001895 if (TypeAliasTemplateDecl *AliasTemplate
1896 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1897 // Find the canonical type for this type alias template specialization.
1898 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1899 if (Pattern->isInvalidDecl())
1900 return QualType();
1901
1902 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1903 Converted.data(), Converted.size());
1904
1905 // Only substitute for the innermost template argument list.
1906 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001907 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001908 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1909 for (unsigned I = 0; I < Depth; ++I)
1910 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001911
1912 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1913 CanonType = SubstType(Pattern->getUnderlyingType(),
1914 TemplateArgLists, AliasTemplate->getLocation(),
1915 AliasTemplate->getDeclName());
1916 if (CanonType.isNull())
1917 return QualType();
1918 } else if (Name.isDependent() ||
1919 TemplateSpecializationType::anyDependentTemplateArguments(
1920 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001921 // This class template specialization is a dependent
1922 // type. Therefore, its canonical type is another class template
1923 // specialization type that contains all of the converted
1924 // arguments in canonical form. This ensures that, e.g., A<T> and
1925 // A<T, T> have identical types when A is declared as:
1926 //
1927 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001928 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001929 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001930 Converted.data(),
1931 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Douglas Gregor1275ae02009-07-28 23:00:59 +00001933 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001934 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001935 // In the future, we need to teach getTemplateSpecializationType to only
1936 // build the canonical type and return that to us.
1937 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001938
1939 // This might work out to be a current instantiation, in which
1940 // case the canonical type needs to be the InjectedClassNameType.
1941 //
1942 // TODO: in theory this could be a simple hashtable lookup; most
1943 // changes to CurContext don't change the set of current
1944 // instantiations.
1945 if (isa<ClassTemplateDecl>(Template)) {
1946 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1947 // If we get out to a namespace, we're done.
1948 if (Ctx->isFileContext()) break;
1949
1950 // If this isn't a record, keep looking.
1951 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1952 if (!Record) continue;
1953
1954 // Look for one of the two cases with InjectedClassNameTypes
1955 // and check whether it's the same template.
1956 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1957 !Record->getDescribedClassTemplate())
1958 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001959
John McCall31f17ec2010-04-27 00:57:59 +00001960 // Fetch the injected class name type and check whether its
1961 // injected type is equal to the type we just built.
1962 QualType ICNT = Context.getTypeDeclType(Record);
1963 QualType Injected = cast<InjectedClassNameType>(ICNT)
1964 ->getInjectedSpecializationType();
1965
1966 if (CanonType != Injected->getCanonicalTypeInternal())
1967 continue;
1968
1969 // If so, the canonical type of this TST is the injected
1970 // class name type of the record we just found.
1971 assert(ICNT.isCanonical());
1972 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001973 break;
1974 }
1975 }
Mike Stump1eb44332009-09-09 15:08:12 +00001976 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001977 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001978 // Find the class template specialization declaration that
1979 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001980 void *InsertPos = 0;
1981 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001982 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001983 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001984 if (!Decl) {
1985 // This is the first time we have referenced this class template
1986 // specialization. Create the canonical declaration and add it to
1987 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001988 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001989 ClassTemplate->getTemplatedDecl()->getTagKind(),
1990 ClassTemplate->getDeclContext(),
1991 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001992 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001993 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001994 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001995 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001996 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001997 Decl->setLexicalDeclContext(CurContext);
1998 }
1999
2000 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002001 assert(isa<RecordType>(CanonType) &&
2002 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Douglas Gregor40808ce2009-03-09 23:48:35 +00002005 // Build the fully-sugared type for this class template
2006 // specialization, which refers back to the class template
2007 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002008 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002009}
2010
John McCallf312b1e2010-08-26 23:41:50 +00002011TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002012Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2013 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002014 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002015 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002016 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002017 if (SS.isInvalid())
2018 return true;
2019
Douglas Gregor7532dc62009-03-30 22:58:21 +00002020 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002021
Douglas Gregor40808ce2009-03-09 23:48:35 +00002022 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002023 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002024 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002025
Douglas Gregora88f09f2011-02-28 17:23:35 +00002026 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2027 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2028 DTN->getQualifier(),
2029 DTN->getIdentifier(),
2030 TemplateArgs);
2031
2032 // Build type-source information.
2033 TypeLocBuilder TLB;
2034 DependentTemplateSpecializationTypeLoc SpecTL
2035 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002036 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002037 SpecTL.setNameLoc(TemplateLoc);
2038 SpecTL.setLAngleLoc(LAngleLoc);
2039 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002040 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002041 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2042 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2043 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2044 }
2045
John McCalld5532b62009-11-23 01:53:49 +00002046 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002047 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002048
2049 if (Result.isNull())
2050 return true;
2051
Douglas Gregor059101f2011-03-02 00:47:37 +00002052 // Build type-source information.
2053 TypeLocBuilder TLB;
2054 TemplateSpecializationTypeLoc SpecTL
2055 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2056 SpecTL.setTemplateNameLoc(TemplateLoc);
2057 SpecTL.setLAngleLoc(LAngleLoc);
2058 SpecTL.setRAngleLoc(RAngleLoc);
2059 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2060 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002061
Douglas Gregor059101f2011-03-02 00:47:37 +00002062 if (SS.isNotEmpty()) {
2063 // Create an elaborated-type-specifier containing the nested-name-specifier.
2064 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2065 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2066 ElabTL.setKeywordLoc(SourceLocation());
2067 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2068 }
2069
2070 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002071}
John McCallf1bbbb42009-09-04 01:14:41 +00002072
Douglas Gregor059101f2011-03-02 00:47:37 +00002073TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002074 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002075 SourceLocation TagLoc,
2076 CXXScopeSpec &SS,
2077 TemplateTy TemplateD,
2078 SourceLocation TemplateLoc,
2079 SourceLocation LAngleLoc,
2080 ASTTemplateArgsPtr TemplateArgsIn,
2081 SourceLocation RAngleLoc) {
2082 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2083
2084 // Translate the parser's template argument list in our AST format.
2085 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2086 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2087
2088 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002089 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002090 ElaboratedTypeKeyword Keyword
2091 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Douglas Gregor059101f2011-03-02 00:47:37 +00002093 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2094 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2095 DTN->getQualifier(),
2096 DTN->getIdentifier(),
2097 TemplateArgs);
2098
2099 // Build type-source information.
2100 TypeLocBuilder TLB;
2101 DependentTemplateSpecializationTypeLoc SpecTL
2102 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2103 SpecTL.setKeywordLoc(TagLoc);
2104 SpecTL.setNameLoc(TemplateLoc);
2105 SpecTL.setLAngleLoc(LAngleLoc);
2106 SpecTL.setRAngleLoc(RAngleLoc);
2107 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2108 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2109 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2110 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2111 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002112
2113 if (TypeAliasTemplateDecl *TAT =
2114 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2115 // C++0x [dcl.type.elab]p2:
2116 // If the identifier resolves to a typedef-name or the simple-template-id
2117 // resolves to an alias template specialization, the
2118 // elaborated-type-specifier is ill-formed.
2119 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2120 Diag(TAT->getLocation(), diag::note_declared_at);
2121 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002122
2123 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2124 if (Result.isNull())
2125 return TypeResult();
2126
2127 // Check the tag kind
2128 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002129 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002130
John McCall6b2becf2009-09-08 17:47:29 +00002131 IdentifierInfo *Id = D->getIdentifier();
2132 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002133
Richard Trieubbf34c02011-06-10 03:11:26 +00002134 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2135 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002136 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002137 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002138 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002139 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002140 }
2141 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002142
2143 // Provide source-location information for the template specialization.
2144 TypeLocBuilder TLB;
2145 TemplateSpecializationTypeLoc SpecTL
2146 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2147 SpecTL.setTemplateNameLoc(TemplateLoc);
2148 SpecTL.setLAngleLoc(LAngleLoc);
2149 SpecTL.setRAngleLoc(RAngleLoc);
2150 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2151 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002152
Douglas Gregor059101f2011-03-02 00:47:37 +00002153 // Construct an elaborated type containing the nested-name-specifier (if any)
2154 // and keyword.
2155 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2156 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2157 ElabTL.setKeywordLoc(TagLoc);
2158 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2159 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002160}
2161
John McCall60d7b3a2010-08-24 06:29:42 +00002162ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002163 LookupResult &R,
2164 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002165 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002166 // FIXME: Can we do any checking at this point? I guess we could check the
2167 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002168 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002169 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002170 // foo<int> could identify a single function unambiguously
2171 // This approach does NOT work, since f<int>(1);
2172 // gets resolved prior to resorting to overload resolution
2173 // i.e., template<class T> void f(double);
2174 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002175
2176 // These should be filtered out by our callers.
2177 assert(!R.empty() && "empty lookup results when building templateid");
2178 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2179
John McCallc373d482010-01-27 01:50:18 +00002180 // We don't want lookup warnings at this point.
2181 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002182
John McCallf7a1a742009-11-24 19:00:30 +00002183 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002184 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002185 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002186 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002187 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002188 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002189
2190 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002191}
2192
John McCallf7a1a742009-11-24 19:00:30 +00002193// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002194ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002195Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002196 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002197 const TemplateArgumentListInfo &TemplateArgs) {
2198 DeclContext *DC;
2199 if (!(DC = computeDeclContext(SS, false)) ||
2200 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002201 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002202 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002203
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002204 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002205 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002206 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2207 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002208
John McCallf7a1a742009-11-24 19:00:30 +00002209 if (R.isAmbiguous())
2210 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002211
John McCallf7a1a742009-11-24 19:00:30 +00002212 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002213 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2214 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002215 return ExprError();
2216 }
2217
2218 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002219 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2220 << (NestedNameSpecifier*) SS.getScopeRep()
2221 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002222 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2223 return ExprError();
2224 }
2225
2226 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002227}
2228
Douglas Gregorc45c2322009-03-31 00:43:58 +00002229/// \brief Form a dependent template name.
2230///
2231/// This action forms a dependent template name given the template
2232/// name and its (presumably dependent) scope specifier. For
2233/// example, given "MetaFun::template apply", the scope specifier \p
2234/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2235/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002236TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002237 SourceLocation TemplateKWLoc,
2238 CXXScopeSpec &SS,
2239 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002240 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002241 bool EnteringContext,
2242 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002243 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2244 !getLangOptions().CPlusPlus0x)
2245 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002246 << FixItHint::CreateRemoval(TemplateKWLoc);
2247
Douglas Gregor0707bc52010-01-19 16:01:07 +00002248 DeclContext *LookupCtx = 0;
2249 if (SS.isSet())
2250 LookupCtx = computeDeclContext(SS, EnteringContext);
2251 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002252 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002253 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002254 // C++0x [temp.names]p5:
2255 // If a name prefixed by the keyword template is not the name of
2256 // a template, the program is ill-formed. [Note: the keyword
2257 // template may not be applied to non-template members of class
2258 // templates. -end note ] [ Note: as is the case with the
2259 // typename prefix, the template prefix is allowed in cases
2260 // where it is not strictly necessary; i.e., when the
2261 // nested-name-specifier or the expression on the left of the ->
2262 // or . is not dependent on a template-parameter, or the use
2263 // does not appear in the scope of a template. -end note]
2264 //
2265 // Note: C++03 was more strict here, because it banned the use of
2266 // the "template" keyword prior to a template-name that was not a
2267 // dependent name. C++ DR468 relaxed this requirement (the
2268 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002269 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002270 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002271 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2272 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002273 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002274 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2275 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002276 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2277 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002278 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002279 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002280 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002281 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002282 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002283 << Name.getSourceRange()
2284 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002285 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002286 } else {
2287 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002288 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002289 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002290 }
2291
Mike Stump1eb44332009-09-09 15:08:12 +00002292 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002293 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002294
Douglas Gregor014e88d2009-11-03 23:16:33 +00002295 switch (Name.getKind()) {
2296 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002297 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002298 Name.Identifier));
2299 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002300
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002301 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002302 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002303 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002304 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002305
2306 case UnqualifiedId::IK_LiteralOperatorId:
2307 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2308
Douglas Gregor014e88d2009-11-03 23:16:33 +00002309 default:
2310 break;
2311 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002312
2313 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002314 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002315 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002316 << Name.getSourceRange()
2317 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002318 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002319}
2320
Mike Stump1eb44332009-09-09 15:08:12 +00002321bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002322 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002323 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002324 const TemplateArgument &Arg = AL.getArgument();
2325
Anders Carlsson436b1562009-06-13 00:33:33 +00002326 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002327 switch(Arg.getKind()) {
2328 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002329 // C++ [temp.arg.type]p1:
2330 // A template-argument for a template-parameter which is a
2331 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002332 break;
2333 case TemplateArgument::Template: {
2334 // We have a template type parameter but the template argument
2335 // is a template without any arguments.
2336 SourceRange SR = AL.getSourceRange();
2337 TemplateName Name = Arg.getAsTemplate();
2338 Diag(SR.getBegin(), diag::err_template_missing_args)
2339 << Name << SR;
2340 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2341 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002342
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002343 return true;
2344 }
2345 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002346 // We have a template type parameter but the template argument
2347 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002348 SourceRange SR = AL.getSourceRange();
2349 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002350 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Anders Carlsson436b1562009-06-13 00:33:33 +00002352 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002353 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002354 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002355
John McCalla93c9342009-12-07 02:54:59 +00002356 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002357 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Anders Carlsson436b1562009-06-13 00:33:33 +00002359 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002360 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2361
2362 // Objective-C ARC:
2363 // If an explicitly-specified template argument type is a lifetime type
2364 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2365 if (getLangOptions().ObjCAutoRefCount &&
2366 ArgType->isObjCLifetimeType() &&
2367 !ArgType.getObjCLifetime()) {
2368 Qualifiers Qs;
2369 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2370 ArgType = Context.getQualifiedType(ArgType, Qs);
2371 }
2372
2373 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002374 return false;
2375}
2376
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002377/// \brief Substitute template arguments into the default template argument for
2378/// the given template type parameter.
2379///
2380/// \param SemaRef the semantic analysis object for which we are performing
2381/// the substitution.
2382///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002383/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002384/// for.
2385///
2386/// \param TemplateLoc the location of the template name that started the
2387/// template-id we are checking.
2388///
2389/// \param RAngleLoc the location of the right angle bracket ('>') that
2390/// terminates the template-id.
2391///
2392/// \param Param the template template parameter whose default we are
2393/// substituting into.
2394///
2395/// \param Converted the list of template arguments provided for template
2396/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002397/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002398static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002399SubstDefaultTemplateArgument(Sema &SemaRef,
2400 TemplateDecl *Template,
2401 SourceLocation TemplateLoc,
2402 SourceLocation RAngleLoc,
2403 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002404 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002405 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002406
2407 // If the argument type is dependent, instantiate it now based
2408 // on the previously-computed template arguments.
2409 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002410 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002411 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002412
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002413 MultiLevelTemplateArgumentList AllTemplateArgs
2414 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2415
2416 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002417 Template, Converted.data(),
2418 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002419 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002421 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2422 Param->getDefaultArgumentLoc(),
2423 Param->getDeclName());
2424 }
2425
2426 return ArgType;
2427}
2428
2429/// \brief Substitute template arguments into the default template argument for
2430/// the given non-type template parameter.
2431///
2432/// \param SemaRef the semantic analysis object for which we are performing
2433/// the substitution.
2434///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002436/// for.
2437///
2438/// \param TemplateLoc the location of the template name that started the
2439/// template-id we are checking.
2440///
2441/// \param RAngleLoc the location of the right angle bracket ('>') that
2442/// terminates the template-id.
2443///
Douglas Gregor788cd062009-11-11 01:00:40 +00002444/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002445/// substituting into.
2446///
2447/// \param Converted the list of template arguments provided for template
2448/// parameters that precede \p Param in the template parameter list.
2449///
2450/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002451static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002452SubstDefaultTemplateArgument(Sema &SemaRef,
2453 TemplateDecl *Template,
2454 SourceLocation TemplateLoc,
2455 SourceLocation RAngleLoc,
2456 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002457 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002458 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002459 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002460
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002461 MultiLevelTemplateArgumentList AllTemplateArgs
2462 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002463
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002464 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002465 Template, Converted.data(),
2466 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002467 SourceRange(TemplateLoc, RAngleLoc));
2468
2469 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2470}
2471
Douglas Gregor788cd062009-11-11 01:00:40 +00002472/// \brief Substitute template arguments into the default template argument for
2473/// the given template template parameter.
2474///
2475/// \param SemaRef the semantic analysis object for which we are performing
2476/// the substitution.
2477///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002478/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002479/// for.
2480///
2481/// \param TemplateLoc the location of the template name that started the
2482/// template-id we are checking.
2483///
2484/// \param RAngleLoc the location of the right angle bracket ('>') that
2485/// terminates the template-id.
2486///
2487/// \param Param the template template parameter whose default we are
2488/// substituting into.
2489///
2490/// \param Converted the list of template arguments provided for template
2491/// parameters that precede \p Param in the template parameter list.
2492///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002493/// \param QualifierLoc Will be set to the nested-name-specifier (with
2494/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002495///
Douglas Gregor788cd062009-11-11 01:00:40 +00002496/// \returns the substituted template argument, or NULL if an error occurred.
2497static TemplateName
2498SubstDefaultTemplateArgument(Sema &SemaRef,
2499 TemplateDecl *Template,
2500 SourceLocation TemplateLoc,
2501 SourceLocation RAngleLoc,
2502 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002503 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2504 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002505 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002506 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002507
Douglas Gregor788cd062009-11-11 01:00:40 +00002508 MultiLevelTemplateArgumentList AllTemplateArgs
2509 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510
Douglas Gregor788cd062009-11-11 01:00:40 +00002511 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002512 Template, Converted.data(),
2513 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002514 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002515
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002516 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002517 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002518 if (QualifierLoc) {
2519 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2520 AllTemplateArgs);
2521 if (!QualifierLoc)
2522 return TemplateName();
2523 }
2524
Douglas Gregor1d752d72011-03-02 18:46:51 +00002525 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002526 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002528 AllTemplateArgs);
2529}
2530
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002531/// \brief If the given template parameter has a default template
2532/// argument, substitute into that default template argument and
2533/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002535Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2536 SourceLocation TemplateLoc,
2537 SourceLocation RAngleLoc,
2538 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002539 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2540 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002541 if (!TypeParm->hasDefaultArgument())
2542 return TemplateArgumentLoc();
2543
John McCalla93c9342009-12-07 02:54:59 +00002544 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002545 TemplateLoc,
2546 RAngleLoc,
2547 TypeParm,
2548 Converted);
2549 if (DI)
2550 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2551
2552 return TemplateArgumentLoc();
2553 }
2554
2555 if (NonTypeTemplateParmDecl *NonTypeParm
2556 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2557 if (!NonTypeParm->hasDefaultArgument())
2558 return TemplateArgumentLoc();
2559
John McCall60d7b3a2010-08-24 06:29:42 +00002560 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002561 TemplateLoc,
2562 RAngleLoc,
2563 NonTypeParm,
2564 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002565 if (Arg.isInvalid())
2566 return TemplateArgumentLoc();
2567
2568 Expr *ArgE = Arg.takeAs<Expr>();
2569 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2570 }
2571
2572 TemplateTemplateParmDecl *TempTempParm
2573 = cast<TemplateTemplateParmDecl>(Param);
2574 if (!TempTempParm->hasDefaultArgument())
2575 return TemplateArgumentLoc();
2576
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002577
Douglas Gregor1d752d72011-03-02 18:46:51 +00002578 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002579 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002580 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002581 RAngleLoc,
2582 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002583 Converted,
2584 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002585 if (TName.isNull())
2586 return TemplateArgumentLoc();
2587
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002588 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002589 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002590 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2591}
2592
Douglas Gregore7526412009-11-11 19:31:23 +00002593/// \brief Check that the given template argument corresponds to the given
2594/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002595///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002596/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002597/// checked.
2598///
2599/// \param Arg The template argument.
2600///
2601/// \param Template The template in which the template argument resides.
2602///
2603/// \param TemplateLoc The location of the template name for the template
2604/// whose argument list we're matching.
2605///
2606/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2607/// the template argument list.
2608///
2609/// \param ArgumentPackIndex The index into the argument pack where this
2610/// argument will be placed. Only valid if the parameter is a parameter pack.
2611///
2612/// \param Converted The checked, converted argument will be added to the
2613/// end of this small vector.
2614///
2615/// \param CTAK Describes how we arrived at this particular template argument:
2616/// explicitly written, deduced, etc.
2617///
2618/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002619bool Sema::CheckTemplateArgument(NamedDecl *Param,
2620 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002621 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002622 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002623 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002624 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002625 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002626 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002627 // Check template type parameters.
2628 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002629 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630
Douglas Gregord9e15302009-11-11 19:41:09 +00002631 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002633 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002634 // with the template arguments we've seen thus far. But if the
2635 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002636 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002637 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2638 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002640 if (NTTPType->isDependentType() &&
2641 !isa<TemplateTemplateParmDecl>(Template) &&
2642 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002643 // Do substitution on the type of the non-type template parameter.
2644 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002645 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002646 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002647
2648 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002649 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002650 NTTPType = SubstType(NTTPType,
2651 MultiLevelTemplateArgumentList(TemplateArgs),
2652 NTTP->getLocation(),
2653 NTTP->getDeclName());
2654 // If that worked, check the non-type template parameter type
2655 // for validity.
2656 if (!NTTPType.isNull())
2657 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2658 NTTP->getLocation());
2659 if (NTTPType.isNull())
2660 return true;
2661 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662
Douglas Gregore7526412009-11-11 19:31:23 +00002663 switch (Arg.getArgument().getKind()) {
2664 case TemplateArgument::Null:
2665 assert(false && "Should never see a NULL template argument here");
2666 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002667
Douglas Gregore7526412009-11-11 19:31:23 +00002668 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002669 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002670 ExprResult Res =
2671 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2672 Result, CTAK);
2673 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002674 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002675
Douglas Gregor910f8002010-11-07 23:05:16 +00002676 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002677 break;
2678 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002679
Douglas Gregore7526412009-11-11 19:31:23 +00002680 case TemplateArgument::Declaration:
2681 case TemplateArgument::Integral:
2682 // We've already checked this template argument, so just copy
2683 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002684 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002685 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Douglas Gregore7526412009-11-11 19:31:23 +00002687 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002688 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002689 // We were given a template template argument. It may not be ill-formed;
2690 // see below.
2691 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002692 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2693 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002694 // We have a template argument such as \c T::template X, which we
2695 // parsed as a template template argument. However, since we now
2696 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002697 // template name into an expression.
2698
2699 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2700 Arg.getTemplateNameLoc());
2701
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002702 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002703 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002704 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002705 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002706 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707
Douglas Gregora7fc9012011-01-05 18:58:31 +00002708 // If we parsed the template argument as a pack expansion, create a
2709 // pack expansion expression.
2710 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002711 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2712 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002713 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002714 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715
Douglas Gregore7526412009-11-11 19:31:23 +00002716 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002717 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2718 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002719 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002720
Douglas Gregor910f8002010-11-07 23:05:16 +00002721 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002722 break;
2723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002724
Douglas Gregore7526412009-11-11 19:31:23 +00002725 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002726 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002727 // therefore cannot be a non-type template argument.
2728 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2729 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002730
Douglas Gregore7526412009-11-11 19:31:23 +00002731 Diag(Param->getLocation(), diag::note_template_param_here);
2732 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002733
Douglas Gregore7526412009-11-11 19:31:23 +00002734 case TemplateArgument::Type: {
2735 // We have a non-type template parameter but the template
2736 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregore7526412009-11-11 19:31:23 +00002738 // C++ [temp.arg]p2:
2739 // In a template-argument, an ambiguity between a type-id and
2740 // an expression is resolved to a type-id, regardless of the
2741 // form of the corresponding template-parameter.
2742 //
2743 // We warn specifically about this case, since it can be rather
2744 // confusing for users.
2745 QualType T = Arg.getArgument().getAsType();
2746 SourceRange SR = Arg.getSourceRange();
2747 if (T->isFunctionType())
2748 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2749 else
2750 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2751 Diag(Param->getLocation(), diag::note_template_param_here);
2752 return true;
2753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002754
Douglas Gregore7526412009-11-11 19:31:23 +00002755 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002756 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002757 break;
2758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregore7526412009-11-11 19:31:23 +00002760 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761 }
2762
2763
Douglas Gregore7526412009-11-11 19:31:23 +00002764 // Check template template parameters.
2765 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 // Substitute into the template parameter list of the template
2768 // template parameter, since previously-supplied template arguments
2769 // may appear within the template template parameter.
2770 {
2771 // Set up a template instantiation context.
2772 LocalInstantiationScope Scope(*this);
2773 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002774 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002775 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
2777 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002778 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002779 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002781 MultiLevelTemplateArgumentList(TemplateArgs)));
2782 if (!TempParm)
2783 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
Douglas Gregore7526412009-11-11 19:31:23 +00002786 switch (Arg.getArgument().getKind()) {
2787 case TemplateArgument::Null:
2788 assert(false && "Should never see a NULL template argument here");
2789 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790
Douglas Gregore7526412009-11-11 19:31:23 +00002791 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002792 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002793 if (CheckTemplateArgument(TempParm, Arg))
2794 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795
Douglas Gregor910f8002010-11-07 23:05:16 +00002796 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002797 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002798
Douglas Gregore7526412009-11-11 19:31:23 +00002799 case TemplateArgument::Expression:
2800 case TemplateArgument::Type:
2801 // We have a template template parameter but the template
2802 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002803 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2804 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002805 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002806
Douglas Gregore7526412009-11-11 19:31:23 +00002807 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002808 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002809 "Declaration argument with template template parameter");
2810 break;
2811 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002812 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002813 "Integral argument with template template parameter");
2814 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002817 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002818 break;
2819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820
Douglas Gregore7526412009-11-11 19:31:23 +00002821 return false;
2822}
2823
Douglas Gregorc15cb382009-02-09 23:23:08 +00002824/// \brief Check that the given template argument list is well-formed
2825/// for specializing the given template.
2826bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2827 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002828 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002829 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002830 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002831 TemplateParameterList *Params = Template->getTemplateParameters();
2832 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002833 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002834 bool Invalid = false;
2835
John McCalld5532b62009-11-23 01:53:49 +00002836 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2837
Mike Stump1eb44332009-09-09 15:08:12 +00002838 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002839 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002841 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002842 (NumArgs < Params->getMinRequiredArguments() &&
2843 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002844 // FIXME: point at either the first arg beyond what we can handle,
2845 // or the '>', depending on whether we have too many or too few
2846 // arguments.
2847 SourceRange Range;
2848 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002849 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002850 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2851 << (NumArgs > NumParams)
2852 << (isa<ClassTemplateDecl>(Template)? 0 :
2853 isa<FunctionTemplateDecl>(Template)? 1 :
2854 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2855 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002856 Diag(Template->getLocation(), diag::note_template_decl_here)
2857 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002858 Invalid = true;
2859 }
Mike Stump1eb44332009-09-09 15:08:12 +00002860
2861 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002862 // [...] The type and form of each template-argument specified in
2863 // a template-id shall match the type and form specified for the
2864 // corresponding parameter declared by the template in its
2865 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002866 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002867 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2868 TemplateParameterList::iterator Param = Params->begin(),
2869 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002870 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002871 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002872 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002873 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002874 // If we have an expanded parameter pack, make sure we don't have too
2875 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002876 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002877 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002878 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002879 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2880 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2881 << true
2882 << (isa<ClassTemplateDecl>(Template)? 0 :
2883 isa<FunctionTemplateDecl>(Template)? 1 :
2884 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2885 << Template;
2886 Diag(Template->getLocation(), diag::note_template_decl_here)
2887 << Params->getSourceRange();
2888 return true;
2889 }
2890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002891
Douglas Gregorf35f8282009-11-11 21:54:23 +00002892 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002893 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2894 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002895 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002896 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002897
Douglas Gregor14be16b2010-12-20 16:57:52 +00002898 if ((*Param)->isTemplateParameterPack()) {
2899 // The template parameter was a template parameter pack, so take the
2900 // deduced argument and place it on the argument pack. Note that we
2901 // stay on the same template parameter so that we can deduce more
2902 // arguments.
2903 ArgumentPack.push_back(Converted.back());
2904 Converted.pop_back();
2905 } else {
2906 // Move to the next template parameter.
2907 ++Param;
2908 }
2909 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002910 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002911 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002912
Douglas Gregor8735b292011-06-03 02:59:40 +00002913 // If we're checking a partial template argument list, we're done.
2914 if (PartialTemplateArgs) {
2915 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2916 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2917 ArgumentPack.data(),
2918 ArgumentPack.size()));
2919
2920 return Invalid;
2921 }
2922
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002923 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002924 // arguments, just break out now and we'll fill in the argument pack below.
2925 if ((*Param)->isTemplateParameterPack())
2926 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002927
Douglas Gregorf35f8282009-11-11 21:54:23 +00002928 // We have a default template argument that we will use.
2929 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930
Douglas Gregorf35f8282009-11-11 21:54:23 +00002931 // Retrieve the default template argument from the template
2932 // parameter. For each kind of template parameter, we substitute the
2933 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002934 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002935 // the default argument.
2936 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2937 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002938 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002939 break;
2940 }
2941
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002942 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002943 Template,
2944 TemplateLoc,
2945 RAngleLoc,
2946 TTP,
2947 Converted);
2948 if (!ArgType)
2949 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002950
Douglas Gregorf35f8282009-11-11 21:54:23 +00002951 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2952 ArgType);
2953 } else if (NonTypeTemplateParmDecl *NTTP
2954 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2955 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002956 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002957 break;
2958 }
2959
John McCall60d7b3a2010-08-24 06:29:42 +00002960 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002961 TemplateLoc,
2962 RAngleLoc,
2963 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002964 Converted);
2965 if (E.isInvalid())
2966 return true;
2967
2968 Expr *Ex = E.takeAs<Expr>();
2969 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2970 } else {
2971 TemplateTemplateParmDecl *TempParm
2972 = cast<TemplateTemplateParmDecl>(*Param);
2973
2974 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002975 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002976 break;
2977 }
2978
Douglas Gregor1d752d72011-03-02 18:46:51 +00002979 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002980 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002981 TemplateLoc,
2982 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002983 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002984 Converted,
2985 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002986 if (Name.isNull())
2987 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002988
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002989 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2990 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002991 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002992
Douglas Gregorf35f8282009-11-11 21:54:23 +00002993 // Introduce an instantiation record that describes where we are using
2994 // the default template argument.
2995 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002996 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002997 SourceRange(TemplateLoc, RAngleLoc));
2998
Douglas Gregorf35f8282009-11-11 21:54:23 +00002999 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003000 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003001 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003002 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003003
Douglas Gregor67714232011-03-03 02:41:12 +00003004 // Core issue 150 (assumed resolution): if this is a template template
3005 // parameter, keep track of the default template arguments from the
3006 // template definition.
3007 if (isTemplateTemplateParameter)
3008 TemplateArgs.addArgument(Arg);
3009
Douglas Gregor14be16b2010-12-20 16:57:52 +00003010 // Move to the next template parameter and argument.
3011 ++Param;
3012 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003013 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003014
Douglas Gregor14be16b2010-12-20 16:57:52 +00003015 // Form argument packs for each of the parameter packs remaining.
3016 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003017 // If we're checking a partial list of template arguments, don't fill
3018 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003019
3020 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003021 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003022 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003023 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003024 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3025 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003026 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003027 ArgumentPack.clear();
3028 }
3029 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003030
Douglas Gregor14be16b2010-12-20 16:57:52 +00003031 ++Param;
3032 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033
Douglas Gregorc15cb382009-02-09 23:23:08 +00003034 return Invalid;
3035}
3036
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003037namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003038 class UnnamedLocalNoLinkageFinder
3039 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003040 {
3041 Sema &S;
3042 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003043
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003044 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003045
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003046 public:
3047 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3048
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003049 bool Visit(QualType T) {
3050 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003051 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003053#define TYPE(Class, Parent) \
3054 bool Visit##Class##Type(const Class##Type *);
3055#define ABSTRACT_TYPE(Class, Parent) \
3056 bool Visit##Class##Type(const Class##Type *) { return false; }
3057#define NON_CANONICAL_TYPE(Class, Parent) \
3058 bool Visit##Class##Type(const Class##Type *) { return false; }
3059#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003061 bool VisitTagDecl(const TagDecl *Tag);
3062 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3063 };
3064}
3065
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003066bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003067 return false;
3068}
3069
3070bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3071 return Visit(T->getElementType());
3072}
3073
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003074bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003075 return Visit(T->getPointeeType());
3076}
3077
3078bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003080 return Visit(T->getPointeeType());
3081}
3082
3083bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003085 return Visit(T->getPointeeType());
3086}
3087
3088bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003089 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003090 return Visit(T->getPointeeType());
3091}
3092
3093bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003094 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003095 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3096}
3097
3098bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003099 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003100 return Visit(T->getElementType());
3101}
3102
3103bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003105 return Visit(T->getElementType());
3106}
3107
3108bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003109 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003110 return Visit(T->getElementType());
3111}
3112
3113bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003114 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003115 return Visit(T->getElementType());
3116}
3117
3118bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003119 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003120 return Visit(T->getElementType());
3121}
3122
3123bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3124 return Visit(T->getElementType());
3125}
3126
3127bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3128 return Visit(T->getElementType());
3129}
3130
3131bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3132 const FunctionProtoType* T) {
3133 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003134 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003135 A != AEnd; ++A) {
3136 if (Visit(*A))
3137 return true;
3138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003139
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003140 return Visit(T->getResultType());
3141}
3142
3143bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3144 const FunctionNoProtoType* T) {
3145 return Visit(T->getResultType());
3146}
3147
3148bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3149 const UnresolvedUsingType*) {
3150 return false;
3151}
3152
3153bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3154 return false;
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3158 return Visit(T->getUnderlyingType());
3159}
3160
3161bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3162 return false;
3163}
3164
Sean Huntca63c202011-05-24 22:41:36 +00003165bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3166 const UnaryTransformType*) {
3167 return false;
3168}
3169
Richard Smith34b41d92011-02-20 03:19:35 +00003170bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3171 return Visit(T->getDeducedType());
3172}
3173
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003174bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3175 return VisitTagDecl(T->getDecl());
3176}
3177
3178bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3179 return VisitTagDecl(T->getDecl());
3180}
3181
3182bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3183 const TemplateTypeParmType*) {
3184 return false;
3185}
3186
Douglas Gregorc3069d62011-01-14 02:55:32 +00003187bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3188 const SubstTemplateTypeParmPackType *) {
3189 return false;
3190}
3191
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003192bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3193 const TemplateSpecializationType*) {
3194 return false;
3195}
3196
3197bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3198 const InjectedClassNameType* T) {
3199 return VisitTagDecl(T->getDecl());
3200}
3201
3202bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3203 const DependentNameType* T) {
3204 return VisitNestedNameSpecifier(T->getQualifier());
3205}
3206
3207bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3208 const DependentTemplateSpecializationType* T) {
3209 return VisitNestedNameSpecifier(T->getQualifier());
3210}
3211
Douglas Gregor7536dd52010-12-20 02:24:11 +00003212bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3213 const PackExpansionType* T) {
3214 return Visit(T->getPattern());
3215}
3216
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003217bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3218 return false;
3219}
3220
3221bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3222 const ObjCInterfaceType *) {
3223 return false;
3224}
3225
3226bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3227 const ObjCObjectPointerType *) {
3228 return false;
3229}
3230
3231bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3232 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3233 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3234 << S.Context.getTypeDeclType(Tag) << SR;
3235 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003236 }
3237
Richard Smith162e1c12011-04-15 14:24:37 +00003238 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003239 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3240 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3241 return true;
3242 }
3243
3244 return false;
3245}
3246
3247bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3248 NestedNameSpecifier *NNS) {
3249 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3250 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003251
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003252 switch (NNS->getKind()) {
3253 case NestedNameSpecifier::Identifier:
3254 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003255 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003256 case NestedNameSpecifier::Global:
3257 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003258
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003259 case NestedNameSpecifier::TypeSpec:
3260 case NestedNameSpecifier::TypeSpecWithTemplate:
3261 return Visit(QualType(NNS->getAsType(), 0));
3262 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003263 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003264}
3265
3266
Douglas Gregorc15cb382009-02-09 23:23:08 +00003267/// \brief Check a template argument against its corresponding
3268/// template type parameter.
3269///
3270/// This routine implements the semantics of C++ [temp.arg.type]. It
3271/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003272bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003273 TypeSourceInfo *ArgInfo) {
3274 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003275 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003276 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003277
3278 if (Arg->isVariablyModifiedType()) {
3279 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003280 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003281 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003282 }
3283
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003284 // C++03 [temp.arg.type]p2:
3285 // A local type, a type with no linkage, an unnamed type or a type
3286 // compounded from any of these types shall not be used as a
3287 // template-argument for a template type-parameter.
3288 //
3289 // C++0x allows these, and even in C++03 we allow them as an extension with
3290 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003291 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003292 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3293 (void)Finder.Visit(Context.getCanonicalType(Arg));
3294 }
3295
Douglas Gregorc15cb382009-02-09 23:23:08 +00003296 return false;
3297}
3298
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003299/// \brief Checks whether the given template argument is the address
3300/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003301static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003302CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3303 NonTypeTemplateParmDecl *Param,
3304 QualType ParamType,
3305 Expr *ArgIn,
3306 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003307 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003308 Expr *Arg = ArgIn;
3309 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003310
3311 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003312 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003313 Arg = Cast->getSubExpr();
3314
3315 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003316 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003317 // A template-argument for a non-type, non-template
3318 // template-parameter shall be one of: [...]
3319 //
3320 // -- the address of an object or function with external
3321 // linkage, including function templates and function
3322 // template-ids but excluding non-static class members,
3323 // expressed as & id-expression where the & is optional if
3324 // the name refers to a function or array, or if the
3325 // corresponding template-parameter is a reference; or
3326 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003327
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003328 // In C++98/03 mode, give an extension warning on any extra parentheses.
3329 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3330 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003331 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003332 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003333 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003334 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003335 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003336 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003337 }
3338
3339 Arg = Parens->getSubExpr();
3340 }
3341
Douglas Gregorb7a09262010-04-01 18:32:35 +00003342 bool AddressTaken = false;
3343 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003344 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003345 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003346 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3347 // Very common in Microsoft COM headers.
3348 if (S.getLangOptions().Microsoft &&
3349 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3350 Converted = TemplateArgument(ArgIn);
3351 return false;
3352 }
3353
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003354 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003355 AddressTaken = true;
3356 AddrOpLoc = UnOp->getOperatorLoc();
3357 }
Francois Picheta343a412011-04-29 09:08:14 +00003358 } else {
3359 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3360 Converted = TemplateArgument(ArgIn);
3361 return false;
3362 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003363 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003364 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003365 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003366 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3367 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003368 S.Diag(Param->getLocation(), diag::note_template_param_here);
3369 return true;
3370 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003371
3372 // Stop checking the precise nature of the argument if it is value dependent,
3373 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003374 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003375 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003376 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003377 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003378
Douglas Gregorb7a09262010-04-01 18:32:35 +00003379 if (!isa<ValueDecl>(DRE->getDecl())) {
3380 S.Diag(Arg->getSourceRange().getBegin(),
3381 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003383 S.Diag(Param->getLocation(), diag::note_template_param_here);
3384 return true;
3385 }
3386
3387 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388
3389 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003390 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3391 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003392 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003393 S.Diag(Param->getLocation(), diag::note_template_param_here);
3394 return true;
3395 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003396
3397 // Cannot refer to non-static member functions
3398 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003399 if (!Method->isStatic()) {
3400 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003401 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003402 S.Diag(Param->getLocation(), diag::note_template_param_here);
3403 return true;
3404 }
Mike Stump1eb44332009-09-09 15:08:12 +00003405
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003406 // Functions must have external linkage.
3407 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003408 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003409 S.Diag(Arg->getSourceRange().getBegin(),
3410 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003411 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003412 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003413 << true;
3414 return true;
3415 }
3416
3417 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003418 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003419
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 // If the template parameter has pointer type, the function decays.
3421 if (ParamType->isPointerType() && !AddressTaken)
3422 ArgType = S.Context.getPointerType(Func->getType());
3423 else if (AddressTaken && ParamType->isReferenceType()) {
3424 // If we originally had an address-of operator, but the
3425 // parameter has reference type, complain and (if things look
3426 // like they will work) drop the address-of operator.
3427 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3428 ParamType.getNonReferenceType())) {
3429 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3430 << ParamType;
3431 S.Diag(Param->getLocation(), diag::note_template_param_here);
3432 return true;
3433 }
3434
3435 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3436 << ParamType
3437 << FixItHint::CreateRemoval(AddrOpLoc);
3438 S.Diag(Param->getLocation(), diag::note_template_param_here);
3439
3440 ArgType = Func->getType();
3441 }
3442 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003443 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003444 S.Diag(Arg->getSourceRange().getBegin(),
3445 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003446 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003447 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003448 << true;
3449 return true;
3450 }
3451
Douglas Gregorb7a09262010-04-01 18:32:35 +00003452 // A value of reference type is not an object.
3453 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003454 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 diag::err_template_arg_reference_var)
3456 << Var->getType() << Arg->getSourceRange();
3457 S.Diag(Param->getLocation(), diag::note_template_param_here);
3458 return true;
3459 }
3460
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003461 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003462 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463
3464 // If the template parameter has pointer type, we must have taken
3465 // the address of this object.
3466 if (ParamType->isReferenceType()) {
3467 if (AddressTaken) {
3468 // If we originally had an address-of operator, but the
3469 // parameter has reference type, complain and (if things look
3470 // like they will work) drop the address-of operator.
3471 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3472 ParamType.getNonReferenceType())) {
3473 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3474 << ParamType;
3475 S.Diag(Param->getLocation(), diag::note_template_param_here);
3476 return true;
3477 }
3478
3479 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3480 << ParamType
3481 << FixItHint::CreateRemoval(AddrOpLoc);
3482 S.Diag(Param->getLocation(), diag::note_template_param_here);
3483
3484 ArgType = Var->getType();
3485 }
3486 } else if (!AddressTaken && ParamType->isPointerType()) {
3487 if (Var->getType()->isArrayType()) {
3488 // Array-to-pointer decay.
3489 ArgType = S.Context.getArrayDecayedType(Var->getType());
3490 } else {
3491 // If the template parameter has pointer type but the address of
3492 // this object was not taken, complain and (possibly) recover by
3493 // taking the address of the entity.
3494 ArgType = S.Context.getPointerType(Var->getType());
3495 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3496 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3497 << ParamType;
3498 S.Diag(Param->getLocation(), diag::note_template_param_here);
3499 return true;
3500 }
3501
3502 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3503 << ParamType
3504 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3505
3506 S.Diag(Param->getLocation(), diag::note_template_param_here);
3507 }
3508 }
3509 } else {
3510 // We found something else, but we don't know specifically what it is.
3511 S.Diag(Arg->getSourceRange().getBegin(),
3512 diag::err_template_arg_not_object_or_func)
3513 << Arg->getSourceRange();
3514 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3515 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003516 }
Mike Stump1eb44332009-09-09 15:08:12 +00003517
John McCallf85e1932011-06-15 23:02:42 +00003518 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003519 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003520 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003521 S.IsQualificationConversion(ArgType, ParamType, false,
3522 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003523 // For pointer-to-object types, qualification conversions are
3524 // permitted.
3525 } else {
3526 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3527 if (!ParamRef->getPointeeType()->isFunctionType()) {
3528 // C++ [temp.arg.nontype]p5b3:
3529 // For a non-type template-parameter of type reference to
3530 // object, no conversions apply. The type referred to by the
3531 // reference may be more cv-qualified than the (otherwise
3532 // identical) type of the template- argument. The
3533 // template-parameter is bound directly to the
3534 // template-argument, which shall be an lvalue.
3535
3536 // FIXME: Other qualifiers?
3537 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3538 unsigned ArgQuals = ArgType.getCVRQualifiers();
3539
3540 if ((ParamQuals | ArgQuals) != ParamQuals) {
3541 S.Diag(Arg->getSourceRange().getBegin(),
3542 diag::err_template_arg_ref_bind_ignores_quals)
3543 << ParamType << Arg->getType()
3544 << Arg->getSourceRange();
3545 S.Diag(Param->getLocation(), diag::note_template_param_here);
3546 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003547 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003548 }
3549 }
3550
3551 // At this point, the template argument refers to an object or
3552 // function with external linkage. We now need to check whether the
3553 // argument and parameter types are compatible.
3554 if (!S.Context.hasSameUnqualifiedType(ArgType,
3555 ParamType.getNonReferenceType())) {
3556 // We can't perform this conversion or binding.
3557 if (ParamType->isReferenceType())
3558 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3559 << ParamType << Arg->getType() << Arg->getSourceRange();
3560 else
3561 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3562 << Arg->getType() << ParamType << Arg->getSourceRange();
3563 S.Diag(Param->getLocation(), diag::note_template_param_here);
3564 return true;
3565 }
3566 }
3567
3568 // Create the template argument.
3569 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003570 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003571 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003572}
3573
3574/// \brief Checks whether the given template argument is a pointer to
3575/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003576bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003577 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003578 bool Invalid = false;
3579
3580 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003581 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003582 Arg = Cast->getSubExpr();
3583
3584 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003585 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003586 // A template-argument for a non-type, non-template
3587 // template-parameter shall be one of: [...]
3588 //
3589 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003590 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003591
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003592 // In C++98/03 mode, give an extension warning on any extra parentheses.
3593 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3594 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003595 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003596 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003597 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003598 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003599 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003600 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003601 }
3602
3603 Arg = Parens->getSubExpr();
3604 }
3605
Douglas Gregorcaddba02009-11-12 18:38:13 +00003606 // A pointer-to-member constant written &Class::member.
3607 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003608 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003609 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3610 if (DRE && !DRE->getQualifier())
3611 DRE = 0;
3612 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003613 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003614 // A constant of pointer-to-member type.
3615 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3616 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3617 if (VD->getType()->isMemberPointerType()) {
3618 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003619 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003620 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3621 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003622 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003623 else
3624 Converted = TemplateArgument(VD->getCanonicalDecl());
3625 return Invalid;
3626 }
3627 }
3628 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003629
Douglas Gregorcaddba02009-11-12 18:38:13 +00003630 DRE = 0;
3631 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003632
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003633 if (!DRE)
3634 return Diag(Arg->getSourceRange().getBegin(),
3635 diag::err_template_arg_not_pointer_to_member_form)
3636 << Arg->getSourceRange();
3637
3638 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3639 assert((isa<FieldDecl>(DRE->getDecl()) ||
3640 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3641 "Only non-static member pointers can make it here");
3642
3643 // Okay: this is the address of a non-static member, and therefore
3644 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003645 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003646 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003647 else
3648 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003649 return Invalid;
3650 }
3651
3652 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003653 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003654 diag::err_template_arg_not_pointer_to_member_form)
3655 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003656 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003657 diag::note_template_arg_refers_here);
3658 return true;
3659}
3660
Douglas Gregorc15cb382009-02-09 23:23:08 +00003661/// \brief Check a template argument against its corresponding
3662/// non-type template parameter.
3663///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003664/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003665/// If an error occurred, it returns ExprError(); otherwise, it
3666/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003667/// InstantiatedParamType is the type of the non-type template
3668/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003669ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3670 QualType InstantiatedParamType, Expr *Arg,
3671 TemplateArgument &Converted,
3672 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003673 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3674
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003675 // If either the parameter has a dependent type or the argument is
3676 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003677 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3678 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003679 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003680 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003681 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003682
3683 // C++ [temp.arg.nontype]p5:
3684 // The following conversions are performed on each expression used
3685 // as a non-type template-argument. If a non-type
3686 // template-argument cannot be converted to the type of the
3687 // corresponding template-parameter then the program is
3688 // ill-formed.
3689 //
3690 // -- for a non-type template-parameter of integral or
3691 // enumeration type, integral promotions (4.5) and integral
3692 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003693 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003694 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003695 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003696 // C++ [temp.arg.nontype]p1:
3697 // A template-argument for a non-type, non-template
3698 // template-parameter shall be one of:
3699 //
3700 // -- an integral constant-expression of integral or enumeration
3701 // type; or
3702 // -- the name of a non-type template-parameter; or
3703 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003704 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003705 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003706 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003707 diag::err_template_arg_not_integral_or_enumeral)
3708 << ArgType << Arg->getSourceRange();
3709 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003710 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003711 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003712 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003713 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3714 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003715 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003716 }
3717
Douglas Gregor02024a92010-03-28 02:42:43 +00003718 // From here on out, all we care about are the unqualified forms
3719 // of the parameter and argument types.
3720 ParamType = ParamType.getUnqualifiedType();
3721 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003722
3723 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003724 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003725 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003726 } else if (CTAK == CTAK_Deduced) {
3727 // C++ [temp.deduct.type]p17:
3728 // If, in the declaration of a function template with a non-type
3729 // template-parameter, the non-type template- parameter is used
3730 // in an expression in the function parameter-list and, if the
3731 // corresponding template-argument is deduced, the
3732 // template-argument type shall match the type of the
3733 // template-parameter exactly, except that a template-argument
3734 // deduced from an array bound may be of any integral type.
3735 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3736 << ArgType << ParamType;
3737 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003738 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003739 } else if (ParamType->isBooleanType()) {
3740 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003741 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003742 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3743 !ParamType->isEnumeralType()) {
3744 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003745 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003746 } else {
3747 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003748 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003749 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003750 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003751 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003752 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003753 }
3754
Douglas Gregorc7469372011-05-04 21:55:00 +00003755 // Add the value of this argument to the list of converted
3756 // arguments. We use the bitwidth and signedness of the template
3757 // parameter.
3758 if (Arg->isValueDependent()) {
3759 // The argument is value-dependent. Create a new
3760 // TemplateArgument with the converted expression.
3761 Converted = TemplateArgument(Arg);
3762 return Owned(Arg);
3763 }
3764
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003765 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003766 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003767 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003768
Douglas Gregorc7469372011-05-04 21:55:00 +00003769 if (ParamType->isBooleanType()) {
3770 // Value must be zero or one.
3771 Value = Value != 0;
3772 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3773 if (Value.getBitWidth() != AllowedBits)
3774 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003775 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003776 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003777 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003778
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003779 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003780 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003781 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003782 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003783 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003784 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003785
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003786 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003787 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003788 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003789 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3790 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3791 << Arg->getSourceRange();
3792 Diag(Param->getLocation(), diag::note_template_param_here);
3793 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003794
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003795 // Complain if we overflowed the template parameter's type.
3796 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003797 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003798 RequiredBits = OldValue.getActiveBits();
3799 else if (OldValue.isUnsigned())
3800 RequiredBits = OldValue.getActiveBits() + 1;
3801 else
3802 RequiredBits = OldValue.getMinSignedBits();
3803 if (RequiredBits > AllowedBits) {
3804 Diag(Arg->getSourceRange().getBegin(),
3805 diag::warn_template_arg_too_large)
3806 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3807 << Arg->getSourceRange();
3808 Diag(Param->getLocation(), diag::note_template_param_here);
3809 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003810 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003811
John McCall833ca992009-10-29 08:12:44 +00003812 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003813 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003814 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003815 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003816 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003817
John McCall6bb80172010-03-30 21:47:33 +00003818 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3819
Douglas Gregorb7a09262010-04-01 18:32:35 +00003820 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3821 // from a template argument of type std::nullptr_t to a non-type
3822 // template parameter of type pointer to object, pointer to
3823 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003824 if (ArgType->isNullPtrType()) {
3825 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3826 Converted = TemplateArgument((NamedDecl *)0);
3827 return Owned(Arg);
3828 }
3829
3830 if (ParamType->isNullPtrType()) {
3831 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3832 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3833 return Owned(Arg);
3834 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003835 }
3836
Douglas Gregorb86b0572009-02-11 01:18:59 +00003837 // Handle pointer-to-function, reference-to-function, and
3838 // pointer-to-member-function all in (roughly) the same way.
3839 if (// -- For a non-type template-parameter of type pointer to
3840 // function, only the function-to-pointer conversion (4.3) is
3841 // applied. If the template-argument represents a set of
3842 // overloaded functions (or a pointer to such), the matching
3843 // function is selected from the set (13.4).
3844 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003845 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003846 // -- For a non-type template-parameter of type reference to
3847 // function, no conversions apply. If the template-argument
3848 // represents a set of overloaded functions, the matching
3849 // function is selected from the set (13.4).
3850 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003851 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003852 // -- For a non-type template-parameter of type pointer to
3853 // member function, no conversions apply. If the
3854 // template-argument represents a set of overloaded member
3855 // functions, the matching member function is selected from
3856 // the set (13.4).
3857 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003858 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003859 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003860
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003861 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003862 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003863 true,
3864 FoundResult)) {
3865 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003866 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003867
3868 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3869 ArgType = Arg->getType();
3870 } else
John Wiegley429bb272011-04-08 18:41:53 +00003871 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003873
John Wiegley429bb272011-04-08 18:41:53 +00003874 if (!ParamType->isMemberPointerType()) {
3875 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3876 ParamType,
3877 Arg, Converted))
3878 return ExprError();
3879 return Owned(Arg);
3880 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003881
John McCallf85e1932011-06-15 23:02:42 +00003882 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003883 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003884 false, ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003885 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003886 } else if (!Context.hasSameUnqualifiedType(ArgType,
3887 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003888 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003889 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003890 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003891 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003892 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003893 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003894 }
Mike Stump1eb44332009-09-09 15:08:12 +00003895
John Wiegley429bb272011-04-08 18:41:53 +00003896 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3897 return ExprError();
3898 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003899 }
3900
Chris Lattnerfe90de72009-02-20 21:37:53 +00003901 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003902 // -- for a non-type template-parameter of type pointer to
3903 // object, qualification conversions (4.4) and the
3904 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003905 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003906 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003907 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003908
John Wiegley429bb272011-04-08 18:41:53 +00003909 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3910 ParamType,
3911 Arg, Converted))
3912 return ExprError();
3913 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003914 }
Mike Stump1eb44332009-09-09 15:08:12 +00003915
Ted Kremenek6217b802009-07-29 21:53:49 +00003916 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003917 // -- For a non-type template-parameter of type reference to
3918 // object, no conversions apply. The type referred to by the
3919 // reference may be more cv-qualified than the (otherwise
3920 // identical) type of the template-argument. The
3921 // template-parameter is bound directly to the
3922 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003923 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003924 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003925
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003926 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003927 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3928 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003929 true,
3930 FoundResult)) {
3931 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003932 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003933
3934 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3935 ArgType = Arg->getType();
3936 } else
John Wiegley429bb272011-04-08 18:41:53 +00003937 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003938 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003939
John Wiegley429bb272011-04-08 18:41:53 +00003940 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3941 ParamType,
3942 Arg, Converted))
3943 return ExprError();
3944 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003945 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003946
3947 // -- For a non-type template-parameter of type pointer to data
3948 // member, qualification conversions (4.4) are applied.
3949 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3950
John McCallf85e1932011-06-15 23:02:42 +00003951 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003952 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003953 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003954 } else if (IsQualificationConversion(ArgType, ParamType, false,
3955 ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003956 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003957 } else {
3958 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003959 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003960 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003961 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003962 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003963 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003964 }
3965
John Wiegley429bb272011-04-08 18:41:53 +00003966 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3967 return ExprError();
3968 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003969}
3970
3971/// \brief Check a template argument against its corresponding
3972/// template template parameter.
3973///
3974/// This routine implements the semantics of C++ [temp.arg.template].
3975/// It returns true if an error occurred, and false otherwise.
3976bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003977 const TemplateArgumentLoc &Arg) {
3978 TemplateName Name = Arg.getArgument().getAsTemplate();
3979 TemplateDecl *Template = Name.getAsTemplateDecl();
3980 if (!Template) {
3981 // Any dependent template name is fine.
3982 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3983 return false;
3984 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003985
Richard Smith3e4c6c42011-05-05 21:57:07 +00003986 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003987 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003988 // the name of a class template or an alias template, expressed as an
3989 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00003990 // primary class templates are considered when matching the
3991 // template template argument with the corresponding parameter;
3992 // partial specializations are not considered even if their
3993 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003994 //
3995 // Note that we also allow template template parameters here, which
3996 // will happen when we are dealing with, e.g., class template
3997 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003998 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00003999 !isa<TemplateTemplateParmDecl>(Template) &&
4000 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004001 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004002 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004003 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004004 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004005 << Template;
4006 }
4007
4008 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4009 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004010 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004011 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004012 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004013}
4014
Douglas Gregor02024a92010-03-28 02:42:43 +00004015/// \brief Given a non-type template argument that refers to a
4016/// declaration and the type of its corresponding non-type template
4017/// parameter, produce an expression that properly refers to that
4018/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004019ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004020Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4021 QualType ParamType,
4022 SourceLocation Loc) {
4023 assert(Arg.getKind() == TemplateArgument::Declaration &&
4024 "Only declaration template arguments permitted here");
4025 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4026
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004027 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004028 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4029 // If the value is a class member, we might have a pointer-to-member.
4030 // Determine whether the non-type template template parameter is of
4031 // pointer-to-member type. If so, we need to build an appropriate
4032 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4033 // would refer to the member itself.
4034 if (ParamType->isMemberPointerType()) {
4035 QualType ClassType
4036 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4037 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004038 = NestedNameSpecifier::Create(Context, 0, false,
4039 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004040 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004041 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004042
4043 // The actual value-ness of this is unimportant, but for
4044 // internal consistency's sake, references to instance methods
4045 // are r-values.
4046 ExprValueKind VK = VK_LValue;
4047 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4048 VK = VK_RValue;
4049
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004050 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004051 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004052 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004053 Loc,
4054 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004055 if (RefExpr.isInvalid())
4056 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004057
John McCall2de56d12010-08-25 11:45:40 +00004058 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004059
Douglas Gregorc0c83002010-04-30 21:46:38 +00004060 // We might need to perform a trailing qualification conversion, since
4061 // the element type on the parameter could be more qualified than the
4062 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004063 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004064 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004065 ParamType.getUnqualifiedType(), false,
4066 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004067 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068
Douglas Gregor02024a92010-03-28 02:42:43 +00004069 assert(!RefExpr.isInvalid() &&
4070 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004071 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004072 return move(RefExpr);
4073 }
4074 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004075
Douglas Gregor02024a92010-03-28 02:42:43 +00004076 QualType T = VD->getType().getNonReferenceType();
4077 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004078 // When the non-type template parameter is a pointer, take the
4079 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004080 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004081 if (RefExpr.isInvalid())
4082 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004083
4084 if (T->isFunctionType() || T->isArrayType()) {
4085 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004086 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4087 if (RefExpr.isInvalid())
4088 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004089
4090 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004091 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004092
Douglas Gregorb7a09262010-04-01 18:32:35 +00004093 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004094 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004095 }
4096
John McCallf89e55a2010-11-18 06:31:45 +00004097 ExprValueKind VK = VK_RValue;
4098
Douglas Gregor02024a92010-03-28 02:42:43 +00004099 // If the non-type template parameter has reference type, qualify the
4100 // resulting declaration reference with the extra qualifiers on the
4101 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004102 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4103 VK = VK_LValue;
4104 T = Context.getQualifiedType(T,
4105 TargetRef->getPointeeType().getQualifiers());
4106 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004107
John McCallf89e55a2010-11-18 06:31:45 +00004108 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004109}
4110
4111/// \brief Construct a new expression that refers to the given
4112/// integral template argument with the given source-location
4113/// information.
4114///
4115/// This routine takes care of the mapping from an integral template
4116/// argument (which may have any integral type) to the appropriate
4117/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004118ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004119Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4120 SourceLocation Loc) {
4121 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004122 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004123 QualType T = Arg.getIntegralType();
4124 if (T->isCharType() || T->isWideCharType())
4125 return Owned(new (Context) CharacterLiteral(
4126 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004127 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004128 if (T->isBooleanType())
4129 return Owned(new (Context) CXXBoolLiteralExpr(
4130 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004131 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004132
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004133 if (T->isNullPtrType())
4134 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4135
Chris Lattner223de242011-04-25 20:37:58 +00004136 // If this is an enum type that we're instantiating, we need to use an integer
4137 // type the same size as the enumerator. We don't want to build an
4138 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004139 QualType BT;
4140 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004141 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004142 else
4143 BT = T;
4144
4145 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004146 if (T->isEnumeralType()) {
4147 // FIXME: This is a hack. We need a better way to handle substituted
4148 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00004149 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4150 Context.getTrivialTypeSourceInfo(T, Loc),
4151 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004152 }
4153
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004154 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004155}
4156
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004157/// \brief Match two template parameters within template parameter lists.
4158static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4159 bool Complain,
4160 Sema::TemplateParameterListEqualKind Kind,
4161 SourceLocation TemplateArgLoc) {
4162 // Check the actual kind (type, non-type, template).
4163 if (Old->getKind() != New->getKind()) {
4164 if (Complain) {
4165 unsigned NextDiag = diag::err_template_param_different_kind;
4166 if (TemplateArgLoc.isValid()) {
4167 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4168 NextDiag = diag::note_template_param_different_kind;
4169 }
4170 S.Diag(New->getLocation(), NextDiag)
4171 << (Kind != Sema::TPL_TemplateMatch);
4172 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4173 << (Kind != Sema::TPL_TemplateMatch);
4174 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004175
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004176 return false;
4177 }
4178
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004179 // Check that both are parameter packs are neither are parameter packs.
4180 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004181 // template template parameter, the template template parameter can have
4182 // a parameter pack where the template template argument does not.
4183 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4184 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4185 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004186 if (Complain) {
4187 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4188 if (TemplateArgLoc.isValid()) {
4189 S.Diag(TemplateArgLoc,
4190 diag::err_template_arg_template_params_mismatch);
4191 NextDiag = diag::note_template_parameter_pack_non_pack;
4192 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004193
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004194 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4195 : isa<NonTypeTemplateParmDecl>(New)? 1
4196 : 2;
4197 S.Diag(New->getLocation(), NextDiag)
4198 << ParamKind << New->isParameterPack();
4199 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4200 << ParamKind << Old->isParameterPack();
4201 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004202
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004203 return false;
4204 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004206 // For non-type template parameters, check the type of the parameter.
4207 if (NonTypeTemplateParmDecl *OldNTTP
4208 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4209 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004210
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004211 // If we are matching a template template argument to a template
4212 // template parameter and one of the non-type template parameter types
4213 // is dependent, then we must wait until template instantiation time
4214 // to actually compare the arguments.
4215 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4216 (OldNTTP->getType()->isDependentType() ||
4217 NewNTTP->getType()->isDependentType()))
4218 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004219
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004220 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4221 if (Complain) {
4222 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4223 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004224 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004225 diag::err_template_arg_template_params_mismatch);
4226 NextDiag = diag::note_template_nontype_parm_different_type;
4227 }
4228 S.Diag(NewNTTP->getLocation(), NextDiag)
4229 << NewNTTP->getType()
4230 << (Kind != Sema::TPL_TemplateMatch);
4231 S.Diag(OldNTTP->getLocation(),
4232 diag::note_template_nontype_parm_prev_declaration)
4233 << OldNTTP->getType();
4234 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004235
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004236 return false;
4237 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004238
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004239 return true;
4240 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004242 // For template template parameters, check the template parameter types.
4243 // The template parameter lists of template template
4244 // parameters must agree.
4245 if (TemplateTemplateParmDecl *OldTTP
4246 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004247 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004248 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4249 OldTTP->getTemplateParameters(),
4250 Complain,
4251 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004253 : Kind),
4254 TemplateArgLoc);
4255 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004256
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004257 return true;
4258}
Douglas Gregor02024a92010-03-28 02:42:43 +00004259
Douglas Gregora0347822011-01-13 00:08:50 +00004260/// \brief Diagnose a known arity mismatch when comparing template argument
4261/// lists.
4262static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004264 TemplateParameterList *New,
4265 TemplateParameterList *Old,
4266 Sema::TemplateParameterListEqualKind Kind,
4267 SourceLocation TemplateArgLoc) {
4268 unsigned NextDiag = diag::err_template_param_list_different_arity;
4269 if (TemplateArgLoc.isValid()) {
4270 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4271 NextDiag = diag::note_template_param_list_different_arity;
4272 }
4273 S.Diag(New->getTemplateLoc(), NextDiag)
4274 << (New->size() > Old->size())
4275 << (Kind != Sema::TPL_TemplateMatch)
4276 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4277 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4278 << (Kind != Sema::TPL_TemplateMatch)
4279 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4280}
4281
Douglas Gregorddc29e12009-02-06 22:42:48 +00004282/// \brief Determine whether the given template parameter lists are
4283/// equivalent.
4284///
Mike Stump1eb44332009-09-09 15:08:12 +00004285/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004286/// source code as part of a new template declaration.
4287///
4288/// \param Old The old template parameter list, typically found via
4289/// name lookup of the template declared with this template parameter
4290/// list.
4291///
4292/// \param Complain If true, this routine will produce a diagnostic if
4293/// the template parameter lists are not equivalent.
4294///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004295/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004296///
4297/// \param TemplateArgLoc If this source location is valid, then we
4298/// are actually checking the template parameter list of a template
4299/// argument (New) against the template parameter list of its
4300/// corresponding template template parameter (Old). We produce
4301/// slightly different diagnostics in this scenario.
4302///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004303/// \returns True if the template parameter lists are equal, false
4304/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004305bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004306Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4307 TemplateParameterList *Old,
4308 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004309 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004310 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004311 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4312 if (Complain)
4313 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4314 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004315
4316 return false;
4317 }
4318
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004319 // C++0x [temp.arg.template]p3:
4320 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004321 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004322 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004323 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004324 // template-parameter-list of P. [...]
4325 TemplateParameterList::iterator NewParm = New->begin();
4326 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004327 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004328 OldParmEnd = Old->end();
4329 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004330 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4331 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004332 if (NewParm == NewParmEnd) {
4333 if (Complain)
4334 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4335 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004336
Douglas Gregora0347822011-01-13 00:08:50 +00004337 return false;
4338 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004339
Douglas Gregora0347822011-01-13 00:08:50 +00004340 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4341 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004342 return false;
4343
Douglas Gregora0347822011-01-13 00:08:50 +00004344 ++NewParm;
4345 continue;
4346 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004347
Douglas Gregora0347822011-01-13 00:08:50 +00004348 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004349 // [...] When P's template- parameter-list contains a template parameter
4350 // pack (14.5.3), the template parameter pack will match zero or more
4351 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004352 // template-parameter-list of A with the same type and form as the
4353 // template parameter pack in P (ignoring whether those template
4354 // parameters are template parameter packs).
4355 for (; NewParm != NewParmEnd; ++NewParm) {
4356 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4357 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004358 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004359 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004360 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004361
Douglas Gregora0347822011-01-13 00:08:50 +00004362 // Make sure we exhausted all of the arguments.
4363 if (NewParm != NewParmEnd) {
4364 if (Complain)
4365 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4366 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004367
Douglas Gregora0347822011-01-13 00:08:50 +00004368 return false;
4369 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004370
Douglas Gregorddc29e12009-02-06 22:42:48 +00004371 return true;
4372}
4373
4374/// \brief Check whether a template can be declared within this scope.
4375///
4376/// If the template declaration is valid in this scope, returns
4377/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004378bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004379Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004380 // Find the nearest enclosing declaration scope.
4381 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4382 (S->getFlags() & Scope::TemplateParamScope) != 0)
4383 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004384
Douglas Gregorddc29e12009-02-06 22:42:48 +00004385 // C++ [temp]p2:
4386 // A template-declaration can appear only as a namespace scope or
4387 // class scope declaration.
4388 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004389 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4390 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004391 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004392 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004393
Eli Friedman1503f772009-07-31 01:43:05 +00004394 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004395 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004396
4397 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4398 return false;
4399
Mike Stump1eb44332009-09-09 15:08:12 +00004400 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004401 diag::err_template_outside_namespace_or_class_scope)
4402 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004403}
Douglas Gregorcc636682009-02-17 23:15:12 +00004404
Douglas Gregord5cb8762009-10-07 00:13:32 +00004405/// \brief Determine what kind of template specialization the given declaration
4406/// is.
4407static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4408 if (!D)
4409 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410
Douglas Gregorf6b11852009-10-08 15:14:33 +00004411 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4412 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004413 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4414 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004415 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4416 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004417
Douglas Gregord5cb8762009-10-07 00:13:32 +00004418 return TSK_Undeclared;
4419}
4420
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004421/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004422/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004423///
Douglas Gregor9302da62009-10-14 23:50:59 +00004424/// This routine determines whether a template specialization can be declared
4425/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004426///
4427/// \param S the semantic analysis object for which this check is being
4428/// performed.
4429///
4430/// \param Specialized the entity being specialized or instantiated, which
4431/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004433/// member class).
4434///
4435/// \param PrevDecl the previous declaration of this entity, if any.
4436///
4437/// \param Loc the location of the explicit specialization or instantiation of
4438/// this entity.
4439///
4440/// \param IsPartialSpecialization whether this is a partial specialization of
4441/// a class template.
4442///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004443/// \returns true if there was an error that we cannot recover from, false
4444/// otherwise.
4445static bool CheckTemplateSpecializationScope(Sema &S,
4446 NamedDecl *Specialized,
4447 NamedDecl *PrevDecl,
4448 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004449 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004450 // Keep these "kind" numbers in sync with the %select statements in the
4451 // various diagnostics emitted by this routine.
4452 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004453 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004454 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004455 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004456 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004457 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004458 EntityKind = 3;
4459 else if (isa<VarDecl>(Specialized))
4460 EntityKind = 4;
4461 else if (isa<RecordDecl>(Specialized))
4462 EntityKind = 5;
4463 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004464 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4465 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004466 return true;
4467 }
4468
Douglas Gregor88b70942009-02-25 22:02:03 +00004469 // C++ [temp.expl.spec]p2:
4470 // An explicit specialization shall be declared in the namespace
4471 // of which the template is a member, or, for member templates, in
4472 // the namespace of which the enclosing class or enclosing class
4473 // template is a member. An explicit specialization of a member
4474 // function, member class or static data member of a class
4475 // template shall be declared in the namespace of which the class
4476 // template is a member. Such a declaration may also be a
4477 // definition. If the declaration is not a definition, the
4478 // specialization may be defined later in the name- space in which
4479 // the explicit specialization was declared, or in a namespace
4480 // that encloses the one in which the explicit specialization was
4481 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004482 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004483 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004484 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004485 return true;
4486 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004487
Douglas Gregor0a407472009-10-07 17:30:37 +00004488 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichete1e96a62011-05-14 19:17:07 +00004489 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4490 << Specialized;
4491 return true;
Douglas Gregor0a407472009-10-07 17:30:37 +00004492 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004493
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004494 // C++ [temp.class.spec]p6:
4495 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004496 // in any namespace scope in which its definition may be defined (14.5.1
4497 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004498 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004499 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004500 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004501 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004503 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4504 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004505 // C++ [temp.exp.spec]p2:
4506 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004507 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004508 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004509 // An explicit specialization of a member function, member class or
4510 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004511 // namespace of which the class template is a member.
4512 //
4513 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004514 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004515 // the specialized template.
4516 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4517 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004518 bool IsCPlusPlus0xExtension
4519 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004520 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004521 S.Diag(Loc, IsCPlusPlus0xExtension
4522 ? diag::ext_template_spec_decl_out_of_scope_global
4523 : diag::err_template_spec_decl_out_of_scope_global)
4524 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004525 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004526 S.Diag(Loc, IsCPlusPlus0xExtension
4527 ? diag::ext_template_spec_decl_out_of_scope
4528 : diag::err_template_spec_decl_out_of_scope)
4529 << EntityKind << Specialized
4530 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004531
Douglas Gregor9302da62009-10-14 23:50:59 +00004532 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4533 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004534 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004535 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536
4537 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004538 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004539 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004540 // specializations of function templates, static data members, and member
4541 // functions, so we skip the check here for those kinds of entities.
4542 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004543 // Should we refactor that check, so that it occurs later?
4544 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004545 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4546 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004547 if (isa<TranslationUnitDecl>(SpecializedContext))
4548 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4549 << EntityKind << Specialized;
4550 else if (isa<NamespaceDecl>(SpecializedContext))
4551 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4552 << EntityKind << Specialized
4553 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554
Douglas Gregor9302da62009-10-14 23:50:59 +00004555 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004556 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004557
Douglas Gregord5cb8762009-10-07 00:13:32 +00004558 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004559
Douglas Gregor88b70942009-02-25 22:02:03 +00004560 return false;
4561}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004562
Douglas Gregorbacb9492011-01-03 21:13:47 +00004563/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4564/// that checks non-type template partial specialization arguments.
4565static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4566 NonTypeTemplateParmDecl *Param,
4567 const TemplateArgument *Args,
4568 unsigned NumArgs) {
4569 for (unsigned I = 0; I != NumArgs; ++I) {
4570 if (Args[I].getKind() == TemplateArgument::Pack) {
4571 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004572 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004573 Args[I].pack_size()))
4574 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004575
Douglas Gregore94866f2009-06-12 21:21:02 +00004576 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004577 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004578
Douglas Gregorbacb9492011-01-03 21:13:47 +00004579 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004580 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004581 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004582 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004583
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004584 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004585 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4586 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004587
4588 // Strip off any implicit casts we added as part of type checking.
4589 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4590 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591
Douglas Gregore94866f2009-06-12 21:21:02 +00004592 // C++ [temp.class.spec]p8:
4593 // A non-type argument is non-specialized if it is the name of a
4594 // non-type parameter. All other non-type arguments are
4595 // specialized.
4596 //
4597 // Below, we check the two conditions that only apply to
4598 // specialized non-type arguments, so skip any non-specialized
4599 // arguments.
4600 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004601 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004602 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004603
Douglas Gregore94866f2009-06-12 21:21:02 +00004604 // C++ [temp.class.spec]p9:
4605 // Within the argument list of a class template partial
4606 // specialization, the following restrictions apply:
4607 // -- A partially specialized non-type argument expression
4608 // shall not involve a template parameter of the partial
4609 // specialization except when the argument expression is a
4610 // simple identifier.
4611 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004612 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004613 diag::err_dependent_non_type_arg_in_partial_spec)
4614 << ArgExpr->getSourceRange();
4615 return true;
4616 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004617
Douglas Gregore94866f2009-06-12 21:21:02 +00004618 // -- The type of a template parameter corresponding to a
4619 // specialized non-type argument shall not be dependent on a
4620 // parameter of the specialization.
4621 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004622 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004623 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4624 << Param->getType()
4625 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004626 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004627 return true;
4628 }
4629 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004630
Douglas Gregorbacb9492011-01-03 21:13:47 +00004631 return false;
4632}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004633
Douglas Gregorbacb9492011-01-03 21:13:47 +00004634/// \brief Check the non-type template arguments of a class template
4635/// partial specialization according to C++ [temp.class.spec]p9.
4636///
4637/// \param TemplateParams the template parameters of the primary class
4638/// template.
4639///
4640/// \param TemplateArg the template arguments of the class template
4641/// partial specialization.
4642///
4643/// \returns true if there was an error, false otherwise.
4644static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4645 TemplateParameterList *TemplateParams,
4646 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4647 const TemplateArgument *ArgList = TemplateArgs.data();
4648
4649 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4650 NonTypeTemplateParmDecl *Param
4651 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4652 if (!Param)
4653 continue;
4654
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004656 &ArgList[I], 1))
4657 return true;
4658 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004659
4660 return false;
4661}
4662
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004663/// \brief Retrieve the previous declaration of the given declaration.
4664static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4665 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4666 return VD->getPreviousDeclaration();
4667 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4668 return FD->getPreviousDeclaration();
4669 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4670 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004671 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004672 return TD->getPreviousDeclaration();
4673 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4674 return FTD->getPreviousDeclaration();
4675 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4676 return CTD->getPreviousDeclaration();
4677 return 0;
4678}
4679
John McCalld226f652010-08-21 09:40:31 +00004680DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004681Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4682 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004683 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004684 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004685 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004686 SourceLocation TemplateNameLoc,
4687 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004688 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004689 SourceLocation RAngleLoc,
4690 AttributeList *Attr,
4691 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004692 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004693
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004694 // NOTE: KWLoc is the location of the tag keyword. This will instead
4695 // store the location of the outermost template keyword in the declaration.
4696 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4697 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4698
Douglas Gregorcc636682009-02-17 23:15:12 +00004699 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004700 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004701 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004702 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4703
4704 if (!ClassTemplate) {
4705 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004706 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004707 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4708 return true;
4709 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004710
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004711 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004712 bool isPartialSpecialization = false;
4713
Douglas Gregor88b70942009-02-25 22:02:03 +00004714 // Check the validity of the template headers that introduce this
4715 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004716 // FIXME: We probably shouldn't complain about these headers for
4717 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004718 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004719 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004720 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4721 TemplateNameLoc,
4722 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004723 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004724 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004725 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004726 isExplicitSpecialization,
4727 Invalid);
4728 if (Invalid)
4729 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004730
Douglas Gregor05396e22009-08-25 17:23:04 +00004731 if (TemplateParams && TemplateParams->size() > 0) {
4732 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004733
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004734 if (TUK == TUK_Friend) {
4735 Diag(KWLoc, diag::err_partial_specialization_friend)
4736 << SourceRange(LAngleLoc, RAngleLoc);
4737 return true;
4738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004739
Douglas Gregor05396e22009-08-25 17:23:04 +00004740 // C++ [temp.class.spec]p10:
4741 // The template parameter list of a specialization shall not
4742 // contain default template argument values.
4743 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4744 Decl *Param = TemplateParams->getParam(I);
4745 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4746 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004747 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004748 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004749 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004750 }
4751 } else if (NonTypeTemplateParmDecl *NTTP
4752 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4753 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004754 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004755 diag::err_default_arg_in_partial_spec)
4756 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004757 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004758 }
4759 } else {
4760 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004761 if (TTP->hasDefaultArgument()) {
4762 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004763 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004764 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004765 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004766 }
4767 }
4768 }
Douglas Gregora735b202009-10-13 14:39:41 +00004769 } else if (TemplateParams) {
4770 if (TUK == TUK_Friend)
4771 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004772 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004773 SourceRange(TemplateParams->getTemplateLoc(),
4774 TemplateParams->getRAngleLoc()))
4775 << SourceRange(LAngleLoc, RAngleLoc);
4776 else
4777 isExplicitSpecialization = true;
4778 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004779 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004780 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004781 isExplicitSpecialization = true;
4782 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004783
Douglas Gregorcc636682009-02-17 23:15:12 +00004784 // Check that the specialization uses the same tag kind as the
4785 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004786 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4787 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004788 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004789 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004790 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004791 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004792 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004793 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004794 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004795 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004796 diag::note_previous_use);
4797 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4798 }
4799
Douglas Gregor40808ce2009-03-09 23:48:35 +00004800 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004801 TemplateArgumentListInfo TemplateArgs;
4802 TemplateArgs.setLAngleLoc(LAngleLoc);
4803 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004804 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004805
Douglas Gregor925910d2011-01-03 20:35:03 +00004806 // Check for unexpanded parameter packs in any of the template arguments.
4807 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004808 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004809 UPPC_PartialSpecialization))
4810 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004811
Douglas Gregorcc636682009-02-17 23:15:12 +00004812 // Check that the template argument list is well-formed for this
4813 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004814 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004815 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4816 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004817 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004818
Douglas Gregor910f8002010-11-07 23:05:16 +00004819 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004820 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004821
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004822 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004823 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004824 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004825 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004826 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004827 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004828 return true;
4829
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004830 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004831 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004832 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004833 TemplateArgs.size())) {
4834 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4835 << ClassTemplate->getDeclName();
4836 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004837 }
4838 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004839
Douglas Gregorcc636682009-02-17 23:15:12 +00004840 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004841 ClassTemplateSpecializationDecl *PrevDecl = 0;
4842
4843 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004844 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004845 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004846 = ClassTemplate->findPartialSpecialization(Converted.data(),
4847 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004848 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004849 else
4850 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004851 = ClassTemplate->findSpecialization(Converted.data(),
4852 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004853
4854 ClassTemplateSpecializationDecl *Specialization = 0;
4855
Douglas Gregor88b70942009-02-25 22:02:03 +00004856 // Check whether we can declare a class template specialization in
4857 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004858 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004859 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4860 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004861 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004862 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004863
Douglas Gregorb88e8882009-07-30 17:40:51 +00004864 // The canonical type
4865 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004866 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004867 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004868 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004869 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004870 // arguments was referenced but not declared, or we're only
4871 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004872 // declaration node as our own, updating its source location and
4873 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004874 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004875 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004876 if (TemplateParameterLists.size() > 0) {
4877 Specialization->setTemplateParameterListsInfo(Context,
4878 TemplateParameterLists.size(),
4879 (TemplateParameterList**) TemplateParameterLists.release());
4880 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004881 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004882 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004883 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004884 // Build the canonical type that describes the converted template
4885 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004886 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4887 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004888 Converted.data(),
4889 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004890
4891 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004892 ClassTemplate->getInjectedClassNameSpecialization())) {
4893 // C++ [temp.class.spec]p9b3:
4894 //
4895 // -- The argument list of the specialization shall not be identical
4896 // to the implicit argument list of the primary template.
4897 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4898 << (TUK == TUK_Definition)
4899 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4900 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4901 ClassTemplate->getIdentifier(),
4902 TemplateNameLoc,
4903 Attr,
4904 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004905 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004906 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004907 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004908 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004909
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004910 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004911 ClassTemplatePartialSpecializationDecl *PrevPartial
4912 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004913 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004914 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004915 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004916 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004917 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004918 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004919 TemplateParams,
4920 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004921 Converted.data(),
4922 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004923 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004924 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004925 PrevPartial,
4926 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004927 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004928 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004929 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004930 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004931 (TemplateParameterList**) TemplateParameterLists.release());
4932 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004933
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004934 if (!PrevPartial)
4935 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004936 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004937
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004939 // template specialization, make a note of that.
4940 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4941 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942
Douglas Gregor031a5882009-06-13 00:26:55 +00004943 // Check that all of the template parameters of the class template
4944 // partial specialization are deducible from the template
4945 // arguments. If not, this class template partial specialization
4946 // will never be used.
4947 llvm::SmallVector<bool, 8> DeducibleParams;
4948 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004949 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004950 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004951 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004952 unsigned NumNonDeducible = 0;
4953 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4954 if (!DeducibleParams[I])
4955 ++NumNonDeducible;
4956
4957 if (NumNonDeducible) {
4958 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4959 << (NumNonDeducible > 1)
4960 << SourceRange(TemplateNameLoc, RAngleLoc);
4961 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4962 if (!DeducibleParams[I]) {
4963 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4964 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004965 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004966 diag::note_partial_spec_unused_parameter)
4967 << Param->getDeclName();
4968 else
Mike Stump1eb44332009-09-09 15:08:12 +00004969 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004970 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004971 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004972 }
4973 }
4974 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004975 } else {
4976 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004977 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004978 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004979 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004980 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004981 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004982 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004983 Converted.data(),
4984 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004985 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004986 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004987 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004988 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004989 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004990 (TemplateParameterList**) TemplateParameterLists.release());
4991 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004992
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004993 if (!PrevDecl)
4994 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004995
4996 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004997 }
4998
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004999 // C++ [temp.expl.spec]p6:
5000 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005001 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005002 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005003 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005004 // use occurs; no diagnostic is required.
5005 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005006 bool Okay = false;
5007 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5008 // Is there any previous explicit specialization declaration?
5009 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5010 Okay = true;
5011 break;
5012 }
5013 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005014
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005015 if (!Okay) {
5016 SourceRange Range(TemplateNameLoc, RAngleLoc);
5017 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5018 << Context.getTypeDeclType(Specialization) << Range;
5019
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005020 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005021 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005022 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005023 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005024 return true;
5025 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005026 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005028 // If this is not a friend, note that this is an explicit specialization.
5029 if (TUK != TUK_Friend)
5030 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005031
5032 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005033 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005034 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005035 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005036 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005037 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005038 Diag(Def->getLocation(), diag::note_previous_definition);
5039 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005040 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005041 }
5042 }
5043
John McCall7f1b9872010-12-18 03:30:47 +00005044 if (Attr)
5045 ProcessDeclAttributeList(S, Specialization, Attr);
5046
Douglas Gregorfc705b82009-02-26 22:19:44 +00005047 // Build the fully-sugared type for this class template
5048 // specialization as the user wrote in the specialization
5049 // itself. This means that we'll pretty-print the type retrieved
5050 // from the specialization's declaration the way that the user
5051 // actually wrote the specialization, rather than formatting the
5052 // name based on the "canonical" representation used to store the
5053 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005054 TypeSourceInfo *WrittenTy
5055 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5056 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005057 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005058 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005059 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005060 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005061 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005062
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005063 // C++ [temp.expl.spec]p9:
5064 // A template explicit specialization is in the scope of the
5065 // namespace in which the template was defined.
5066 //
5067 // We actually implement this paragraph where we set the semantic
5068 // context (in the creation of the ClassTemplateSpecializationDecl),
5069 // but we also maintain the lexical context where the actual
5070 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005071 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005072
Douglas Gregorcc636682009-02-17 23:15:12 +00005073 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005074 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005075 Specialization->startDefinition();
5076
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005077 if (TUK == TUK_Friend) {
5078 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5079 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005080 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005081 /*FIXME:*/KWLoc);
5082 Friend->setAccess(AS_public);
5083 CurContext->addDecl(Friend);
5084 } else {
5085 // Add the specialization into its lexical context, so that it can
5086 // be seen when iterating through the list of declarations in that
5087 // context. However, specializations are not found by name lookup.
5088 CurContext->addDecl(Specialization);
5089 }
John McCalld226f652010-08-21 09:40:31 +00005090 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005091}
Douglas Gregord57959a2009-03-27 23:10:48 +00005092
John McCalld226f652010-08-21 09:40:31 +00005093Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005094 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005095 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005096 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5097}
5098
John McCalld226f652010-08-21 09:40:31 +00005099Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005100 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005101 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005102 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005103 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005104
Douglas Gregor52591bf2009-06-24 00:54:41 +00005105 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005106 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005107 }
Mike Stump1eb44332009-09-09 15:08:12 +00005108
Douglas Gregor52591bf2009-06-24 00:54:41 +00005109 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005110
John McCalld226f652010-08-21 09:40:31 +00005111 Decl *DP = HandleDeclarator(ParentScope, D,
5112 move(TemplateParameterLists),
5113 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005114 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005115 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005116 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005117 FunctionTemplate->getTemplatedDecl());
5118 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5119 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5120 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005121}
5122
John McCall75042392010-02-11 01:33:53 +00005123/// \brief Strips various properties off an implicit instantiation
5124/// that has just been explicitly specialized.
5125static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005126 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005127
5128 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5129 FD->setInlineSpecified(false);
5130 }
5131}
5132
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005133/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005134/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005135/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005136/// new specialization/instantiation will have any effect.
5137///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005138/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005139/// instantiation.
5140///
5141/// \param NewTSK the kind of the new explicit specialization or instantiation.
5142///
5143/// \param PrevDecl the previous declaration of the entity.
5144///
5145/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5146///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005148/// declaration was instantiated (either implicitly or explicitly).
5149///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005150/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005151/// specialization or instantiation has no effect and should be ignored.
5152///
5153/// \returns true if there was an error that should prevent the introduction of
5154/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005155bool
5156Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5157 TemplateSpecializationKind NewTSK,
5158 NamedDecl *PrevDecl,
5159 TemplateSpecializationKind PrevTSK,
5160 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005161 bool &HasNoEffect) {
5162 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163
Douglas Gregor454885e2009-10-15 15:54:05 +00005164 switch (NewTSK) {
5165 case TSK_Undeclared:
5166 case TSK_ImplicitInstantiation:
5167 assert(false && "Don't check implicit instantiations here");
5168 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005169
Douglas Gregor454885e2009-10-15 15:54:05 +00005170 case TSK_ExplicitSpecialization:
5171 switch (PrevTSK) {
5172 case TSK_Undeclared:
5173 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005175 // explicitly specialized or has merely been mentioned without any
5176 // instantiation.
5177 return false;
5178
5179 case TSK_ImplicitInstantiation:
5180 if (PrevPointOfInstantiation.isInvalid()) {
5181 // The declaration itself has not actually been instantiated, so it is
5182 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005183 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005184 return false;
5185 }
5186 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005187
Douglas Gregor454885e2009-10-15 15:54:05 +00005188 case TSK_ExplicitInstantiationDeclaration:
5189 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005190 assert((PrevTSK == TSK_ImplicitInstantiation ||
5191 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005192 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005193
Douglas Gregor454885e2009-10-15 15:54:05 +00005194 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005195 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005196 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005197 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005198 // implicit instantiation to take place, in every translation unit in
5199 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005200 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5201 // Is there any previous explicit specialization declaration?
5202 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5203 return false;
5204 }
5205
Douglas Gregor0d035142009-10-27 18:42:08 +00005206 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005207 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005208 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005209 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210
Douglas Gregor454885e2009-10-15 15:54:05 +00005211 return true;
5212 }
5213 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005214
Douglas Gregor454885e2009-10-15 15:54:05 +00005215 case TSK_ExplicitInstantiationDeclaration:
5216 switch (PrevTSK) {
5217 case TSK_ExplicitInstantiationDeclaration:
5218 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005219 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005220 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005221
Douglas Gregor454885e2009-10-15 15:54:05 +00005222 case TSK_Undeclared:
5223 case TSK_ImplicitInstantiation:
5224 // We're explicitly instantiating something that may have already been
5225 // implicitly instantiated; that's fine.
5226 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005227
Douglas Gregor454885e2009-10-15 15:54:05 +00005228 case TSK_ExplicitSpecialization:
5229 // C++0x [temp.explicit]p4:
5230 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005232 // specialization for that template, the explicit instantiation has no
5233 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005234 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005235 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236
Douglas Gregor454885e2009-10-15 15:54:05 +00005237 case TSK_ExplicitInstantiationDefinition:
5238 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005239 // If an entity is the subject of both an explicit instantiation
5240 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005241 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005242 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005243 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005245 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005246 assert(PrevPointOfInstantiation.isValid() &&
5247 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005248 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005249 return false;
5250 }
5251 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005252
Douglas Gregor454885e2009-10-15 15:54:05 +00005253 case TSK_ExplicitInstantiationDefinition:
5254 switch (PrevTSK) {
5255 case TSK_Undeclared:
5256 case TSK_ImplicitInstantiation:
5257 // We're explicitly instantiating something that may have already been
5258 // implicitly instantiated; that's fine.
5259 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260
Douglas Gregor454885e2009-10-15 15:54:05 +00005261 case TSK_ExplicitSpecialization:
5262 // C++ DR 259, C++0x [temp.explicit]p4:
5263 // For a given set of template parameters, if an explicit
5264 // instantiation of a template appears after a declaration of
5265 // an explicit specialization for that template, the explicit
5266 // instantiation has no effect.
5267 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005269 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005270 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005271 if (!getLangOptions().CPlusPlus0x) {
5272 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005273 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005274 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005275 diag::note_previous_template_specialization);
5276 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005277 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005278 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 case TSK_ExplicitInstantiationDeclaration:
5281 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005282 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005283 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 case TSK_ExplicitInstantiationDefinition:
5286 // C++0x [temp.spec]p5:
5287 // For a given template and a given set of template-arguments,
5288 // - an explicit instantiation definition shall appear at most once
5289 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005290 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005291 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005293 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005294 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005295 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005296 }
5297 break;
5298 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005299
Douglas Gregor454885e2009-10-15 15:54:05 +00005300 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 return false;
5303}
5304
John McCallaf2094e2010-04-08 09:05:18 +00005305/// \brief Perform semantic analysis for the given dependent function
5306/// template specialization. The only possible way to get a dependent
5307/// function template specialization is with a friend declaration,
5308/// like so:
5309///
5310/// template <class T> void foo(T);
5311/// template <class T> class A {
5312/// friend void foo<>(T);
5313/// };
5314///
5315/// There really isn't any useful analysis we can do here, so we
5316/// just store the information.
5317bool
5318Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5319 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5320 LookupResult &Previous) {
5321 // Remove anything from Previous that isn't a function template in
5322 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005323 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005324 LookupResult::Filter F = Previous.makeFilter();
5325 while (F.hasNext()) {
5326 NamedDecl *D = F.next()->getUnderlyingDecl();
5327 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005328 !FDLookupContext->InEnclosingNamespaceSetOf(
5329 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005330 F.erase();
5331 }
5332 F.done();
5333
5334 // Should this be diagnosed here?
5335 if (Previous.empty()) return true;
5336
5337 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5338 ExplicitTemplateArgs);
5339 return false;
5340}
5341
Abramo Bagnarae03db982010-05-20 15:32:11 +00005342/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005343/// specialization.
5344///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005345/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005346/// explicit function template specialization. On successful completion,
5347/// the function declaration \p FD will become a function template
5348/// specialization.
5349///
5350/// \param FD the function declaration, which will be updated to become a
5351/// function template specialization.
5352///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005353/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5354/// if any. Note that this may be valid info even when 0 arguments are
5355/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5356/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005357///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005358/// \param PrevDecl the set of declarations that may be specialized by
5359/// this function specialization.
5360bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005361Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005362 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005363 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005364 // The set of function template specializations that could match this
5365 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005366 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005367
Sebastian Redl7a126a42010-08-31 00:36:30 +00005368 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005369 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5370 I != E; ++I) {
5371 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5372 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005374 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005375 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5376 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005377 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005378
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005379 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380 // A trailing template-argument can be left unspecified in the
5381 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005382 // provided it can be deduced from the function argument type.
5383 // Perform template argument deduction to determine whether we may be
5384 // specializing this template.
5385 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005386 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005387 FunctionDecl *Specialization = 0;
5388 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005389 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005390 FD->getType(),
5391 Specialization,
5392 Info)) {
5393 // FIXME: Template argument deduction failed; record why it failed, so
5394 // that we can provide nifty diagnostics.
5395 (void)TDK;
5396 continue;
5397 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005399 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005400 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005401 }
5402 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005403
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005404 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005405 UnresolvedSetIterator Result
5406 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005407 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005409 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005410 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005411 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005412 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005413 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005414 return true;
John McCallc373d482010-01-27 01:50:18 +00005415
5416 // Ignore access information; it doesn't figure into redeclaration checking.
5417 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005418
5419 FunctionTemplateSpecializationInfo *SpecInfo
5420 = Specialization->getTemplateSpecializationInfo();
5421 assert(SpecInfo && "Function template specialization info missing?");
5422 {
5423 // Note: do not overwrite location info if previous template
5424 // specialization kind was explicit.
5425 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5426 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5427 Specialization->setLocation(FD->getLocation());
5428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005429
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005430 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005431 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005432
5433 // If this is a friend declaration, then we're not really declaring
5434 // an explicit specialization.
5435 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005436
Douglas Gregord5cb8762009-10-07 00:13:32 +00005437 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005438 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005439 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005440 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005441 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005442 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005443 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005444
5445 // C++ [temp.expl.spec]p6:
5446 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005447 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005448 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005450 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005451 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005452 if (!isFriend &&
5453 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005454 TSK_ExplicitSpecialization,
5455 Specialization,
5456 SpecInfo->getTemplateSpecializationKind(),
5457 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005458 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005459 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005460
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005461 // Mark the prior declaration as an explicit specialization, so that later
5462 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005463 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005464 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005465 MarkUnusedFileScopedDecl(Specialization);
5466 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005467
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005468 // Turn the given function declaration into a function template
5469 // specialization, with the template arguments from the previous
5470 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005471 // Take copies of (semantic and syntactic) template argument lists.
5472 const TemplateArgumentList* TemplArgs = new (Context)
5473 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5474 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5475 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005476 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005477 TemplArgs, /*InsertPos=*/0,
5478 SpecInfo->getTemplateSpecializationKind(),
5479 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005480 FD->setStorageClass(Specialization->getStorageClass());
5481
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005482 // The "previous declaration" for this function template specialization is
5483 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005484 Previous.clear();
5485 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005486 return false;
5487}
5488
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005489/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005490/// specialization.
5491///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005493/// explicit member function specialization. On successful completion,
5494/// the function declaration \p FD will become a member function
5495/// specialization.
5496///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005497/// \param Member the member declaration, which will be updated to become a
5498/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005499///
John McCall68263142009-11-18 22:49:29 +00005500/// \param Previous the set of declarations, one of which may be specialized
5501/// by this function specialization; the set will be modified to contain the
5502/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503bool
John McCall68263142009-11-18 22:49:29 +00005504Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005505 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005506
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005507 // Try to find the member we are instantiating.
5508 NamedDecl *Instantiation = 0;
5509 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005510 MemberSpecializationInfo *MSInfo = 0;
5511
John McCall68263142009-11-18 22:49:29 +00005512 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005513 // Nowhere to look anyway.
5514 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005515 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5516 I != E; ++I) {
5517 NamedDecl *D = (*I)->getUnderlyingDecl();
5518 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005519 if (Context.hasSameType(Function->getType(), Method->getType())) {
5520 Instantiation = Method;
5521 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005522 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005523 break;
5524 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005525 }
5526 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005527 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005528 VarDecl *PrevVar;
5529 if (Previous.isSingleResult() &&
5530 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005531 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005532 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005533 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005534 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005535 }
5536 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005537 CXXRecordDecl *PrevRecord;
5538 if (Previous.isSingleResult() &&
5539 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5540 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005541 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005542 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005543 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005544 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005545
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005546 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005547 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005548 // specializations are always out-of-line, the caller will complain about
5549 // this mismatch later.
5550 return false;
5551 }
John McCall77e8b112010-04-13 20:37:33 +00005552
5553 // If this is a friend, just bail out here before we start turning
5554 // things into explicit specializations.
5555 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5556 // Preserve instantiation information.
5557 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5558 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5559 cast<CXXMethodDecl>(InstantiatedFrom),
5560 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5561 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5562 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5563 cast<CXXRecordDecl>(InstantiatedFrom),
5564 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5565 }
5566
5567 Previous.clear();
5568 Previous.addDecl(Instantiation);
5569 return false;
5570 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005571
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005572 // Make sure that this is a specialization of a member.
5573 if (!InstantiatedFrom) {
5574 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5575 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005576 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5577 return true;
5578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005579
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005580 // C++ [temp.expl.spec]p6:
5581 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005582 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005583 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005584 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005585 // use occurs; no diagnostic is required.
5586 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005587
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005588 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005589 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5590 TSK_ExplicitSpecialization,
5591 Instantiation,
5592 MSInfo->getTemplateSpecializationKind(),
5593 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005594 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005595 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005596
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005597 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005598 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005599 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005600 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005601 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005602 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005603
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005604 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005605 // the original declaration to note that it is an explicit specialization
5606 // (if it was previously an implicit instantiation). This latter step
5607 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005608 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005609 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5610 if (InstantiationFunction->getTemplateSpecializationKind() ==
5611 TSK_ImplicitInstantiation) {
5612 InstantiationFunction->setTemplateSpecializationKind(
5613 TSK_ExplicitSpecialization);
5614 InstantiationFunction->setLocation(Member->getLocation());
5615 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005617 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5618 cast<CXXMethodDecl>(InstantiatedFrom),
5619 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005620 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005621 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005622 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5623 if (InstantiationVar->getTemplateSpecializationKind() ==
5624 TSK_ImplicitInstantiation) {
5625 InstantiationVar->setTemplateSpecializationKind(
5626 TSK_ExplicitSpecialization);
5627 InstantiationVar->setLocation(Member->getLocation());
5628 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005629
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005630 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5631 cast<VarDecl>(InstantiatedFrom),
5632 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005633 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005634 } else {
5635 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005636 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5637 if (InstantiationClass->getTemplateSpecializationKind() ==
5638 TSK_ImplicitInstantiation) {
5639 InstantiationClass->setTemplateSpecializationKind(
5640 TSK_ExplicitSpecialization);
5641 InstantiationClass->setLocation(Member->getLocation());
5642 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005643
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005644 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005645 cast<CXXRecordDecl>(InstantiatedFrom),
5646 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005648
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005649 // Save the caller the trouble of having to figure out which declaration
5650 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005651 Previous.clear();
5652 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005653 return false;
5654}
5655
Douglas Gregor558c0322009-10-14 23:41:34 +00005656/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005657///
5658/// \returns true if a serious error occurs, false otherwise.
5659static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005660 SourceLocation InstLoc,
5661 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005662 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5663 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005664
Douglas Gregor669eed82010-07-13 00:10:04 +00005665 if (CurContext->isRecord()) {
5666 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5667 << D;
5668 return true;
5669 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005670
Douglas Gregor558c0322009-10-14 23:41:34 +00005671 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005672 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005673 // template.
5674 //
5675 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005677 !CurContext->Encloses(OrigContext)) {
5678 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005679 S.Diag(InstLoc,
5680 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005681 diag::err_explicit_instantiation_out_of_scope
5682 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005683 << D << NS;
5684 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005685 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005686 S.getLangOptions().CPlusPlus0x?
5687 diag::err_explicit_instantiation_must_be_global
5688 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005689 << D;
5690 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005691 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005692 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005693
Douglas Gregor558c0322009-10-14 23:41:34 +00005694 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695 // If the name declared in the explicit instantiation is an unqualified
5696 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005697 // its template is declared or, if that namespace is inline (7.3.1), any
5698 // namespace from its enclosing namespace set.
5699 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005700 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005701
5702 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005703 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005704
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005705 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005706 S.getLangOptions().CPlusPlus0x?
5707 diag::err_explicit_instantiation_unqualified_wrong_namespace
5708 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005709 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005710 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005711 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005712}
5713
5714/// \brief Determine whether the given scope specifier has a template-id in it.
5715static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5716 if (!SS.isSet())
5717 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005718
Douglas Gregor558c0322009-10-14 23:41:34 +00005719 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005721 // or a static data member of a class template specialization, the name of
5722 // the class template specialization in the qualified-id for the member
5723 // name shall be a simple-template-id.
5724 //
5725 // C++98 has the same restriction, just worded differently.
5726 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5727 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005728 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005729 if (isa<TemplateSpecializationType>(T))
5730 return true;
5731
5732 return false;
5733}
5734
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005735// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005736DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005737Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005738 SourceLocation ExternLoc,
5739 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005740 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005741 SourceLocation KWLoc,
5742 const CXXScopeSpec &SS,
5743 TemplateTy TemplateD,
5744 SourceLocation TemplateNameLoc,
5745 SourceLocation LAngleLoc,
5746 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005747 SourceLocation RAngleLoc,
5748 AttributeList *Attr) {
5749 // Find the class template we're specializing
5750 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005751 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005752 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5753
5754 // Check that the specialization uses the same tag kind as the
5755 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005756 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5757 assert(Kind != TTK_Enum &&
5758 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005759 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005760 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005761 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005762 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005763 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005764 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005765 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005766 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005767 diag::note_previous_use);
5768 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5769 }
5770
Douglas Gregor558c0322009-10-14 23:41:34 +00005771 // C++0x [temp.explicit]p2:
5772 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005773 // definition and an explicit instantiation declaration. An explicit
5774 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005775 TemplateSpecializationKind TSK
5776 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5777 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005779 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005780 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005781 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005782
5783 // Check that the template argument list is well-formed for this
5784 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005785 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005786 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5787 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005788 return true;
5789
Douglas Gregor910f8002010-11-07 23:05:16 +00005790 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005791 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005792
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005793 // Find the class template specialization declaration that
5794 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005795 void *InsertPos = 0;
5796 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005797 = ClassTemplate->findSpecialization(Converted.data(),
5798 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005799
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005800 TemplateSpecializationKind PrevDecl_TSK
5801 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5802
Douglas Gregord5cb8762009-10-07 00:13:32 +00005803 // C++0x [temp.explicit]p2:
5804 // [...] An explicit instantiation shall appear in an enclosing
5805 // namespace of its template. [...]
5806 //
5807 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005808 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5809 SS.isSet()))
5810 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005812 ClassTemplateSpecializationDecl *Specialization = 0;
5813
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005814 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005815 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005816 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005817 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005818 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005819 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005820 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005821
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005822 // Even though HasNoEffect == true means that this explicit instantiation
5823 // has no effect on semantics, we go on to put its syntax in the AST.
5824
5825 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5826 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005827 // Since the only prior class template specialization with these
5828 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005829 // declaration node as our own, updating the source location
5830 // for the template name to reflect our new declaration.
5831 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005832 Specialization = PrevDecl;
5833 Specialization->setLocation(TemplateNameLoc);
5834 PrevDecl = 0;
5835 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005836 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005837
Douglas Gregor52604ab2009-09-11 21:19:12 +00005838 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005839 // Create a new class template specialization declaration node for
5840 // this explicit specialization.
5841 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005842 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005843 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005844 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005845 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005846 Converted.data(),
5847 Converted.size(),
5848 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005849 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005850
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005851 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005852 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005853 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005854 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005855 }
5856
5857 // Build the fully-sugared type for this explicit instantiation as
5858 // the user wrote in the explicit instantiation itself. This means
5859 // that we'll pretty-print the type retrieved from the
5860 // specialization's declaration the way that the user actually wrote
5861 // the explicit instantiation, rather than formatting the name based
5862 // on the "canonical" representation used to store the template
5863 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005864 TypeSourceInfo *WrittenTy
5865 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5866 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005867 Context.getTypeDeclType(Specialization));
5868 Specialization->setTypeAsWritten(WrittenTy);
5869 TemplateArgsIn.release();
5870
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005871 // Set source locations for keywords.
5872 Specialization->setExternLoc(ExternLoc);
5873 Specialization->setTemplateKeywordLoc(TemplateLoc);
5874
5875 // Add the explicit instantiation into its lexical context. However,
5876 // since explicit instantiations are never found by name lookup, we
5877 // just put it into the declaration context directly.
5878 Specialization->setLexicalDeclContext(CurContext);
5879 CurContext->addDecl(Specialization);
5880
5881 // Syntax is now OK, so return if it has no other effect on semantics.
5882 if (HasNoEffect) {
5883 // Set the template specialization kind.
5884 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005885 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005886 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005887
5888 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005889 // A definition of a class template or class member template
5890 // shall be in scope at the point of the explicit instantiation of
5891 // the class template or class member template.
5892 //
5893 // This check comes when we actually try to perform the
5894 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005895 ClassTemplateSpecializationDecl *Def
5896 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005897 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005898 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005899 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005900 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005901 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005902 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5903 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005904
Douglas Gregor0d035142009-10-27 18:42:08 +00005905 // Instantiate the members of this class template specialization.
5906 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005907 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005908 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005909 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5910
5911 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5912 // TSK_ExplicitInstantiationDefinition
5913 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5914 TSK == TSK_ExplicitInstantiationDefinition)
5915 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005916
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005917 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005918 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005919
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005920 // Set the template specialization kind.
5921 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005922 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005923}
5924
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005925// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005926DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005927Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005928 SourceLocation ExternLoc,
5929 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005930 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005931 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005932 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005933 IdentifierInfo *Name,
5934 SourceLocation NameLoc,
5935 AttributeList *Attr) {
5936
Douglas Gregor402abb52009-05-28 23:31:59 +00005937 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005938 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005939 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005940 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5941 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005942 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005943 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005944 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5945
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005946 if (!TagD)
5947 return true;
5948
John McCalld226f652010-08-21 09:40:31 +00005949 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005950 if (Tag->isEnum()) {
5951 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5952 << Context.getTypeDeclType(Tag);
5953 return true;
5954 }
5955
Douglas Gregord0c87372009-05-27 17:30:49 +00005956 if (Tag->isInvalidDecl())
5957 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005958
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005959 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5960 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5961 if (!Pattern) {
5962 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5963 << Context.getTypeDeclType(Record);
5964 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5965 return true;
5966 }
5967
Douglas Gregor558c0322009-10-14 23:41:34 +00005968 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005969 // If the explicit instantiation is for a class or member class, the
5970 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005971 // simple-template-id.
5972 //
5973 // C++98 has the same restriction, just worded differently.
5974 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005975 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005976 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005977
Douglas Gregor558c0322009-10-14 23:41:34 +00005978 // C++0x [temp.explicit]p2:
5979 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005980 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005981 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005982 TemplateSpecializationKind TSK
5983 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5984 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005985
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005986 // C++0x [temp.explicit]p2:
5987 // [...] An explicit instantiation shall appear in an enclosing
5988 // namespace of its template. [...]
5989 //
5990 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005991 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005992
Douglas Gregor454885e2009-10-15 15:54:05 +00005993 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005994 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005995 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005996 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005997 PrevDecl = Record;
5998 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005999 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006000 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006001 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006002 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006003 PrevDecl,
6004 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006005 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006006 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006007 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006008 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006009 return TagD;
6010 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006011
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006012 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006013 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006014 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006015 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006016 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006017 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006018 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006019 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006020 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006021 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6022 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006023 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6024 << Pattern;
6025 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006026 } else {
6027 if (InstantiateClass(NameLoc, Record, Def,
6028 getTemplateInstantiationArgs(Record),
6029 TSK))
6030 return true;
6031
Douglas Gregor952b0172010-02-11 01:04:33 +00006032 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006033 if (!RecordDef)
6034 return true;
6035 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006036 }
6037
Douglas Gregor0d035142009-10-27 18:42:08 +00006038 // Instantiate all of the members of the class.
6039 InstantiateClassMembers(NameLoc, RecordDef,
6040 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006041
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006042 if (TSK == TSK_ExplicitInstantiationDefinition)
6043 MarkVTableUsed(NameLoc, RecordDef, true);
6044
Mike Stump390b4cc2009-05-16 07:39:55 +00006045 // FIXME: We don't have any representation for explicit instantiations of
6046 // member classes. Such a representation is not needed for compilation, but it
6047 // should be available for clients that want to see all of the declarations in
6048 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006049 return TagD;
6050}
6051
John McCallf312b1e2010-08-26 23:41:50 +00006052DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6053 SourceLocation ExternLoc,
6054 SourceLocation TemplateLoc,
6055 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006056 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006057 // TODO: check if/when DNInfo should replace Name.
6058 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6059 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006060 if (!Name) {
6061 if (!D.isInvalidType())
6062 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6063 diag::err_explicit_instantiation_requires_name)
6064 << D.getDeclSpec().getSourceRange()
6065 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006066
Douglas Gregord5a423b2009-09-25 18:43:00 +00006067 return true;
6068 }
6069
6070 // The scope passed in may not be a decl scope. Zip up the scope tree until
6071 // we find one that is.
6072 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6073 (S->getFlags() & Scope::TemplateParamScope) != 0)
6074 S = S->getParent();
6075
6076 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006077 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6078 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006079 if (R.isNull())
6080 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006081
Douglas Gregore885e182011-05-21 18:53:30 +00006082 // C++ [dcl.stc]p1:
6083 // A storage-class-specifier shall not be specified in [...] an explicit
6084 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006085 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006086 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6087 << Name;
6088 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006089 } else if (D.getDeclSpec().getStorageClassSpec()
6090 != DeclSpec::SCS_unspecified) {
6091 // Complain about then remove the storage class specifier.
6092 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6093 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6094
6095 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006096 }
6097
Douglas Gregor663b5a02009-10-14 20:14:33 +00006098 // C++0x [temp.explicit]p1:
6099 // [...] An explicit instantiation of a function template shall not use the
6100 // inline or constexpr specifiers.
6101 // Presumably, this also applies to member functions of class templates as
6102 // well.
6103 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006104 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006105 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006106 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006107
Douglas Gregor663b5a02009-10-14 20:14:33 +00006108 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006109
Douglas Gregor558c0322009-10-14 23:41:34 +00006110 // C++0x [temp.explicit]p2:
6111 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006112 // definition and an explicit instantiation declaration. An explicit
6113 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006114 TemplateSpecializationKind TSK
6115 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6116 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006117
Abramo Bagnara25777432010-08-11 22:01:17 +00006118 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006119 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006120
6121 if (!R->isFunctionType()) {
6122 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006123 // A [...] static data member of a class template can be explicitly
6124 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006125 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006126 if (Previous.isAmbiguous())
6127 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006128
John McCall1bcee0a2009-12-02 08:25:40 +00006129 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006130 if (!Prev || !Prev->isStaticDataMember()) {
6131 // We expect to see a data data member here.
6132 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6133 << Name;
6134 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6135 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006136 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006137 return true;
6138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006139
Douglas Gregord5a423b2009-09-25 18:43:00 +00006140 if (!Prev->getInstantiatedFromStaticDataMember()) {
6141 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006142 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006143 diag::err_explicit_instantiation_data_member_not_instantiated)
6144 << Prev;
6145 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6146 // FIXME: Can we provide a note showing where this was declared?
6147 return true;
6148 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006149
Douglas Gregor558c0322009-10-14 23:41:34 +00006150 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006151 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006152 // or a static data member of a class template specialization, the name of
6153 // the class template specialization in the qualified-id for the member
6154 // name shall be a simple-template-id.
6155 //
6156 // C++98 has the same restriction, just worded differently.
6157 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006158 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006159 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006160 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006161
Douglas Gregor558c0322009-10-14 23:41:34 +00006162 // Check the scope of this explicit instantiation.
6163 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006164
Douglas Gregor454885e2009-10-15 15:54:05 +00006165 // Verify that it is okay to explicitly instantiate here.
6166 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6167 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006168 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006169 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006170 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006171 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006172 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006173 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006174 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006175 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006176
Douglas Gregord5a423b2009-09-25 18:43:00 +00006177 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006178 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006179 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006180 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006181
Douglas Gregord5a423b2009-09-25 18:43:00 +00006182 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006183 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006184 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006185
6186 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006187 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006188 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006189 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006190 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6191 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006192 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6193 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006194 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6195 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006196 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006197 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006198 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006199 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006200 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201
Douglas Gregord5a423b2009-09-25 18:43:00 +00006202 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006203 // A [...] function [...] can be explicitly instantiated from its template.
6204 // A member function [...] of a class template can be explicitly
6205 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006206 // template.
John McCallc373d482010-01-27 01:50:18 +00006207 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006208 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6209 P != PEnd; ++P) {
6210 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006211 if (!HasExplicitTemplateArgs) {
6212 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6213 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6214 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006215
John McCallc373d482010-01-27 01:50:18 +00006216 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006217 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6218 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006219 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006220 }
6221 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006222
Douglas Gregord5a423b2009-09-25 18:43:00 +00006223 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6224 if (!FunTmpl)
6225 continue;
6226
John McCall5769d612010-02-08 23:07:23 +00006227 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006228 FunctionDecl *Specialization = 0;
6229 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006230 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006231 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006232 R, Specialization, Info)) {
6233 // FIXME: Keep track of almost-matches?
6234 (void)TDK;
6235 continue;
6236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006237
John McCallc373d482010-01-27 01:50:18 +00006238 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006239 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006240
Douglas Gregord5a423b2009-09-25 18:43:00 +00006241 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006242 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006243 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006244 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006245 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6246 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6247 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006248
John McCallc373d482010-01-27 01:50:18 +00006249 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006250 return true;
John McCallc373d482010-01-27 01:50:18 +00006251
6252 // Ignore access control bits, we don't need them for redeclaration checking.
6253 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006254
Douglas Gregor0a897e32009-10-15 17:21:20 +00006255 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006256 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006257 diag::err_explicit_instantiation_member_function_not_instantiated)
6258 << Specialization
6259 << (Specialization->getTemplateSpecializationKind() ==
6260 TSK_ExplicitSpecialization);
6261 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6262 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006263 }
6264
Douglas Gregor0a897e32009-10-15 17:21:20 +00006265 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006266 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6267 PrevDecl = Specialization;
6268
Douglas Gregor0a897e32009-10-15 17:21:20 +00006269 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006270 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006271 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006272 PrevDecl,
6273 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006274 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006275 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006276 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006277
Douglas Gregor0a897e32009-10-15 17:21:20 +00006278 // FIXME: We may still want to build some representation of this
6279 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006280 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006281 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006282 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006283
6284 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006285
Douglas Gregor0a897e32009-10-15 17:21:20 +00006286 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006287 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006288
Douglas Gregor558c0322009-10-14 23:41:34 +00006289 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006290 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006291 // or a static data member of a class template specialization, the name of
6292 // the class template specialization in the qualified-id for the member
6293 // name shall be a simple-template-id.
6294 //
6295 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006296 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006297 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006298 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006299 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006300 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006301 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006302 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006303
Douglas Gregor558c0322009-10-14 23:41:34 +00006304 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006305 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006306 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006307 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006308 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006309
Douglas Gregord5a423b2009-09-25 18:43:00 +00006310 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006311 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006312}
6313
John McCallf312b1e2010-08-26 23:41:50 +00006314TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006315Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6316 const CXXScopeSpec &SS, IdentifierInfo *Name,
6317 SourceLocation TagLoc, SourceLocation NameLoc) {
6318 // This has to hold, because SS is expected to be defined.
6319 assert(Name && "Expected a name in a dependent tag");
6320
6321 NestedNameSpecifier *NNS
6322 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6323 if (!NNS)
6324 return true;
6325
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006326 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006327
Douglas Gregor48c89f42010-04-24 16:38:41 +00006328 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6329 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006330 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006331 return true;
6332 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006333
Douglas Gregor059101f2011-03-02 00:47:37 +00006334 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006335 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006336 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6337
6338 // Create type-source location information for this type.
6339 TypeLocBuilder TLB;
6340 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6341 TL.setKeywordLoc(TagLoc);
6342 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6343 TL.setNameLoc(NameLoc);
6344 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006345}
6346
John McCallf312b1e2010-08-26 23:41:50 +00006347TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006348Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6349 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006350 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006351 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006352 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006353
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006354 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6355 !getLangOptions().CPlusPlus0x)
6356 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006357 << FixItHint::CreateRemoval(TypenameLoc);
6358
Douglas Gregor2494dd02011-03-01 01:34:45 +00006359 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006360 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6361 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006362 if (T.isNull())
6363 return true;
John McCall63b43852010-04-29 23:50:39 +00006364
6365 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6366 if (isa<DependentNameType>(T)) {
6367 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006368 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006369 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006370 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006371 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006372 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006373 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006374 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006375 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006376 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006377
John McCallb3d87482010-08-24 05:47:05 +00006378 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006379}
6380
John McCallf312b1e2010-08-26 23:41:50 +00006381TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006382Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6383 const CXXScopeSpec &SS,
6384 SourceLocation TemplateLoc,
6385 TemplateTy TemplateIn,
6386 SourceLocation TemplateNameLoc,
6387 SourceLocation LAngleLoc,
6388 ASTTemplateArgsPtr TemplateArgsIn,
6389 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006390 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6391 !getLangOptions().CPlusPlus0x)
6392 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006393 << FixItHint::CreateRemoval(TypenameLoc);
6394
6395 // Translate the parser's template argument list in our AST format.
6396 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6397 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6398
6399 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006400 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6401 // Construct a dependent template specialization type.
6402 assert(DTN && "dependent template has non-dependent name?");
6403 assert(DTN->getQualifier()
6404 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6405 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6406 DTN->getQualifier(),
6407 DTN->getIdentifier(),
6408 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006409
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006410 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006411 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006412 DependentTemplateSpecializationTypeLoc SpecTL
6413 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006414 SpecTL.setLAngleLoc(LAngleLoc);
6415 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006416 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006417 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006418 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006419 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6420 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006421 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006422 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006423
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006424 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6425 if (T.isNull())
6426 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006427
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006428 // Provide source-location information for the template specialization
6429 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006430 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006431 TemplateSpecializationTypeLoc SpecTL
6432 = Builder.push<TemplateSpecializationTypeLoc>(T);
6433
6434 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006435 SpecTL.setLAngleLoc(LAngleLoc);
6436 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006437 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006438 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6439 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6440
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006441 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6442 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6443 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006444 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6445
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006446 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6447 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006448}
6449
Douglas Gregora02411e2011-02-27 22:46:49 +00006450
Douglas Gregord57959a2009-03-27 23:10:48 +00006451/// \brief Build the type that describes a C++ typename specifier,
6452/// e.g., "typename T::type".
6453QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006454Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6455 SourceLocation KeywordLoc,
6456 NestedNameSpecifierLoc QualifierLoc,
6457 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006458 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006459 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006460 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006461
John McCall77bb1aa2010-05-01 00:40:08 +00006462 DeclContext *Ctx = computeDeclContext(SS);
6463 if (!Ctx) {
6464 // If the nested-name-specifier is dependent and couldn't be
6465 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006466 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6467 return Context.getDependentNameType(Keyword,
6468 QualifierLoc.getNestedNameSpecifier(),
6469 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006470 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006471
John McCall77bb1aa2010-05-01 00:40:08 +00006472 // If the nested-name-specifier refers to the current instantiation,
6473 // the "typename" keyword itself is superfluous. In C++03, the
6474 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6475 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006476 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006477
John McCall77bb1aa2010-05-01 00:40:08 +00006478 if (RequireCompleteDeclContext(SS, Ctx))
6479 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006480
6481 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006482 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006483 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006484 unsigned DiagID = 0;
6485 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006486 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006487 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006488 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006489 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006490
6491 case LookupResult::FoundUnresolvedValue: {
6492 // We found a using declaration that is a value. Most likely, the using
6493 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006494 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006495 IILoc);
6496 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6497 << Name << Ctx << FullRange;
6498 if (UnresolvedUsingValueDecl *Using
6499 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006500 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006501 Diag(Loc, diag::note_using_value_decl_missing_typename)
6502 << FixItHint::CreateInsertion(Loc, "typename ");
6503 }
6504 }
6505 // Fall through to create a dependent typename type, from which we can recover
6506 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006507
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006508 case LookupResult::NotFoundInCurrentInstantiation:
6509 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006510 return Context.getDependentNameType(Keyword,
6511 QualifierLoc.getNestedNameSpecifier(),
6512 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006513
6514 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006515 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006516 // We found a type. Build an ElaboratedType, since the
6517 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006518 return Context.getElaboratedType(ETK_Typename,
6519 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006520 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006521 }
6522
6523 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006524 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006525 break;
6526
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006527
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006528 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006529 return QualType();
6530
Douglas Gregord57959a2009-03-27 23:10:48 +00006531 case LookupResult::FoundOverloaded:
6532 DiagID = diag::err_typename_nested_not_type;
6533 Referenced = *Result.begin();
6534 break;
6535
John McCall6e247262009-10-10 05:48:19 +00006536 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006537 return QualType();
6538 }
6539
6540 // If we get here, it's because name lookup did not find a
6541 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006542 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006543 IILoc);
6544 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006545 if (Referenced)
6546 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6547 << Name;
6548 return QualType();
6549}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006550
6551namespace {
6552 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006553 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006554 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006555 SourceLocation Loc;
6556 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006557
Douglas Gregor4a959d82009-08-06 16:20:37 +00006558 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006559 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006560
Mike Stump1eb44332009-09-09 15:08:12 +00006561 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006562 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006563 DeclarationName Entity)
6564 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006565 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006566
6567 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006568 /// transformed.
6569 ///
6570 /// For the purposes of type reconstruction, a type has already been
6571 /// transformed if it is NULL or if it is not dependent.
6572 bool AlreadyTransformed(QualType T) {
6573 return T.isNull() || !T->isDependentType();
6574 }
Mike Stump1eb44332009-09-09 15:08:12 +00006575
6576 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006577 /// rebuilt.
6578 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006579
Douglas Gregor4a959d82009-08-06 16:20:37 +00006580 /// \brief Returns the name of the entity whose type is being rebuilt.
6581 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006582
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006583 /// \brief Sets the "base" location and entity when that
6584 /// information is known based on another transformation.
6585 void setBase(SourceLocation Loc, DeclarationName Entity) {
6586 this->Loc = Loc;
6587 this->Entity = Entity;
6588 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006589 };
6590}
6591
Douglas Gregor4a959d82009-08-06 16:20:37 +00006592/// \brief Rebuilds a type within the context of the current instantiation.
6593///
Mike Stump1eb44332009-09-09 15:08:12 +00006594/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006595/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006596/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006597/// partial specialization thereof). This routine will rebuild that type now
6598/// that we have entered the declarator's scope, which may produce different
6599/// canonical types, e.g.,
6600///
6601/// \code
6602/// template<typename T>
6603/// struct X {
6604/// typedef T* pointer;
6605/// pointer data();
6606/// };
6607///
6608/// template<typename T>
6609/// typename X<T>::pointer X<T>::data() { ... }
6610/// \endcode
6611///
Douglas Gregor4714c122010-03-31 17:34:00 +00006612/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006613/// since we do not know that we can look into X<T> when we parsed the type.
6614/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006615/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006616/// as the canonical type of T*, allowing the return types of the out-of-line
6617/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006618TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6619 SourceLocation Loc,
6620 DeclarationName Name) {
6621 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006622 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006623
Douglas Gregor4a959d82009-08-06 16:20:37 +00006624 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6625 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006626}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006627
John McCall60d7b3a2010-08-24 06:29:42 +00006628ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006629 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6630 DeclarationName());
6631 return Rebuilder.TransformExpr(E);
6632}
6633
John McCall63b43852010-04-29 23:50:39 +00006634bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006635 if (SS.isInvalid())
6636 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006637
Douglas Gregor7e384942011-02-25 16:07:42 +00006638 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006639 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6640 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006641 NestedNameSpecifierLoc Rebuilt
6642 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6643 if (!Rebuilt)
6644 return true;
John McCall63b43852010-04-29 23:50:39 +00006645
Douglas Gregor7e384942011-02-25 16:07:42 +00006646 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006647 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006648}
6649
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006650/// \brief Produces a formatted string that describes the binding of
6651/// template parameters to template arguments.
6652std::string
6653Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6654 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006655 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006656}
6657
6658std::string
6659Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6660 const TemplateArgument *Args,
6661 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006662 llvm::SmallString<128> Str;
6663 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006664
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006665 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006666 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006667
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006668 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006669 if (I >= NumArgs)
6670 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006671
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006672 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006673 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006674 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006675 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006676
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006677 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006678 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006679 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006680 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006681 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006682
Douglas Gregor87dd6972010-12-20 16:52:59 +00006683 Out << " = ";
6684 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006685 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006686
6687 Out << ']';
6688 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006689}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006690
6691void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6692 if (!FD)
6693 return;
6694 FD->setLateTemplateParsed(Flag);
6695}
6696
6697bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6698 DeclContext *DC = CurContext;
6699
6700 while (DC) {
6701 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6702 const FunctionDecl *FD = RD->isLocalClass();
6703 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6704 } else if (DC->isTranslationUnit() || DC->isNamespace())
6705 return false;
6706
6707 DC = DC->getParent();
6708 }
6709 return false;
6710}