blob: 8213f3266f1ff3883ce465954aafa9566b6f342f [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();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000923 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000924 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000925 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000926 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000928 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000929 }
930
Douglas Gregorddc29e12009-02-06 22:42:48 +0000931 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000932 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000933 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934 Diag(NameLoc, diag::err_redefinition) << Name;
935 Diag(Def->getLocation(), diag::note_previous_definition);
936 // FIXME: Would it make sense to try to "forget" the previous
937 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000938 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000939 }
940 }
941 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
942 // Maybe we will complain about the shadowed template parameter.
943 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
944 // Just pretend that we didn't see the previous declaration.
945 PrevDecl = 0;
946 } else if (PrevDecl) {
947 // C++ [temp]p5:
948 // A class template shall not have the same name as any other
949 // template, class, function, object, enumeration, enumerator,
950 // namespace, or type in the same scope (3.3), except as specified
951 // in (14.5.4).
952 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
953 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000954 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000955 }
956
Douglas Gregord684b002009-02-10 19:49:53 +0000957 // Check the template parameter list of this declaration, possibly
958 // merging in the template parameter list from the previous class
959 // template declaration.
960 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000961 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000962 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000963 SemanticContext->isRecord() &&
964 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000965 ? TPC_ClassTemplateMember
966 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000967 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregor57265e32010-04-12 16:00:01 +0000969 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000970 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000971 // template out-of-line.
972 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
973 !(TUK == TUK_Friend && CurContext->isDependentContext()))
974 Diag(NameLoc, diag::err_member_def_does_not_match)
975 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000976 }
977
Mike Stump1eb44332009-09-09 15:08:12 +0000978 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000979 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000980 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000981 PrevClassTemplate->getTemplatedDecl() : 0,
982 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000983 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000984 if (NumOuterTemplateParamLists > 0)
985 NewClass->setTemplateParameterListsInfo(Context,
986 NumOuterTemplateParamLists,
987 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000988
989 ClassTemplateDecl *NewTemplate
990 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
991 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000992 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000993 NewClass->setDescribedClassTemplate(NewTemplate);
994
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000995 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000996 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000997 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000998 assert(T->isDependentType() && "Class template type is not dependent?");
999 (void)T;
1000
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001001 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001002 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001003 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001004 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1005 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001006
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001007 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001009 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregorddc29e12009-02-06 22:42:48 +00001011 // Set the lexical context of these templates
1012 NewClass->setLexicalDeclContext(CurContext);
1013 NewTemplate->setLexicalDeclContext(CurContext);
1014
John McCall0f434ec2009-07-31 02:45:11 +00001015 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001016 NewClass->startDefinition();
1017
1018 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001019 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001020
John McCall05b23ea2009-09-14 21:59:20 +00001021 if (TUK != TUK_Friend)
1022 PushOnScopeChains(NewTemplate, S);
1023 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001025 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001026 NewClass->setAccess(PrevClassTemplate->getAccess());
1027 }
John McCall05b23ea2009-09-14 21:59:20 +00001028
Douglas Gregord85bea22009-09-26 06:47:28 +00001029 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1030 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001031
John McCall05b23ea2009-09-14 21:59:20 +00001032 // Friend templates are visible in fairly strange ways.
1033 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001034 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001035 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1036 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1037 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001039 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001040
Douglas Gregord85bea22009-09-26 06:47:28 +00001041 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1042 NewClass->getLocation(),
1043 NewTemplate,
1044 /*FIXME:*/NewClass->getLocation());
1045 Friend->setAccess(AS_public);
1046 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001047 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001048
Douglas Gregord684b002009-02-10 19:49:53 +00001049 if (Invalid) {
1050 NewTemplate->setInvalidDecl();
1051 NewClass->setInvalidDecl();
1052 }
John McCalld226f652010-08-21 09:40:31 +00001053 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001054}
1055
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001056/// \brief Diagnose the presence of a default template argument on a
1057/// template parameter, which is ill-formed in certain contexts.
1058///
1059/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001061 Sema::TemplateParamListContext TPC,
1062 SourceLocation ParamLoc,
1063 SourceRange DefArgRange) {
1064 switch (TPC) {
1065 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001066 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067 return false;
1068
1069 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001070 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001071 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001072 // A default template-argument shall not be specified in a
1073 // function template declaration or a function template
1074 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001075 // If a friend function template declaration specifies a default
1076 // template-argument, that declaration shall be a definition and shall be
1077 // the only declaration of the function template in the translation unit.
1078 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001079 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001080 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001081 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001082 << DefArgRange;
1083 return false;
1084
1085 case Sema::TPC_ClassTemplateMember:
1086 // C++0x [temp.param]p9:
1087 // A default template-argument shall not be specified in the
1088 // template-parameter-lists of the definition of a member of a
1089 // class template that appears outside of the member's class.
1090 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1091 << DefArgRange;
1092 return true;
1093
1094 case Sema::TPC_FriendFunctionTemplate:
1095 // C++ [temp.param]p9:
1096 // A default template-argument shall not be specified in a
1097 // friend template declaration.
1098 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1099 << DefArgRange;
1100 return true;
1101
1102 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1103 // for friend function templates if there is only a single
1104 // declaration (and it is a definition). Strange!
1105 }
1106
1107 return false;
1108}
1109
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001110/// \brief Check for unexpanded parameter packs within the template parameters
1111/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001112static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1113 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001114 TemplateParameterList *Params = TTP->getTemplateParameters();
1115 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1116 NamedDecl *P = Params->getParam(I);
1117 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001118 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001119 NTTP->getTypeSourceInfo(),
1120 Sema::UPPC_NonTypeTemplateParameterType))
1121 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001122
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001123 continue;
1124 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001125
1126 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001127 = dyn_cast<TemplateTemplateParmDecl>(P))
1128 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1129 return true;
1130 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001131
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001132 return false;
1133}
1134
Douglas Gregord684b002009-02-10 19:49:53 +00001135/// \brief Checks the validity of a template parameter list, possibly
1136/// considering the template parameter list from a previous
1137/// declaration.
1138///
1139/// If an "old" template parameter list is provided, it must be
1140/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1141/// template parameter list.
1142///
1143/// \param NewParams Template parameter list for a new template
1144/// declaration. This template parameter list will be updated with any
1145/// default arguments that are carried through from the previous
1146/// template parameter list.
1147///
1148/// \param OldParams If provided, template parameter list from a
1149/// previous declaration of the same template. Default template
1150/// arguments will be merged from the old template parameter list to
1151/// the new template parameter list.
1152///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001153/// \param TPC Describes the context in which we are checking the given
1154/// template parameter list.
1155///
Douglas Gregord684b002009-02-10 19:49:53 +00001156/// \returns true if an error occurred, false otherwise.
1157bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001158 TemplateParameterList *OldParams,
1159 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001160 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregord684b002009-02-10 19:49:53 +00001162 // C++ [temp.param]p10:
1163 // The set of default template-arguments available for use with a
1164 // template declaration or definition is obtained by merging the
1165 // default arguments from the definition (if in scope) and all
1166 // declarations in scope in the same way default function
1167 // arguments are (8.3.6).
1168 bool SawDefaultArgument = false;
1169 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001170
Anders Carlsson49d25572009-06-12 23:20:15 +00001171 bool SawParameterPack = false;
1172 SourceLocation ParameterPackLoc;
1173
Mike Stump1a35fde2009-02-11 23:03:27 +00001174 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001175 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001176 if (OldParams)
1177 OldParam = OldParams->begin();
1178
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001179 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001180 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1181 NewParamEnd = NewParams->end();
1182 NewParam != NewParamEnd; ++NewParam) {
1183 // Variables used to diagnose redundant default arguments
1184 bool RedundantDefaultArg = false;
1185 SourceLocation OldDefaultLoc;
1186 SourceLocation NewDefaultLoc;
1187
1188 // Variables used to diagnose missing default arguments
1189 bool MissingDefaultArg = false;
1190
Anders Carlsson49d25572009-06-12 23:20:15 +00001191 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001192 // If a template parameter of a primary class template or alias template
1193 // is a template parameter pack, it shall be the last template parameter.
1194 if (SawParameterPack &&
1195 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001196 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001197 diag::err_template_param_pack_must_be_last_template_parameter);
1198 Invalid = true;
1199 }
1200
Douglas Gregord684b002009-02-10 19:49:53 +00001201 if (TemplateTypeParmDecl *NewTypeParm
1202 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001203 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001204 if (NewTypeParm->hasDefaultArgument() &&
1205 DiagnoseDefaultTemplateArgument(*this, TPC,
1206 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001207 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001208 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001209 NewTypeParm->removeDefaultArgument();
1210
1211 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001212 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001213 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Anders Carlsson49d25572009-06-12 23:20:15 +00001215 if (NewTypeParm->isParameterPack()) {
1216 assert(!NewTypeParm->hasDefaultArgument() &&
1217 "Parameter packs can't have a default argument!");
1218 SawParameterPack = true;
1219 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001220 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001221 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001222 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1223 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1224 SawDefaultArgument = true;
1225 RedundantDefaultArg = true;
1226 PreviousDefaultArgLoc = NewDefaultLoc;
1227 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1228 // Merge the default argument from the old declaration to the
1229 // new declaration.
1230 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001231 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001232 true);
1233 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1234 } else if (NewTypeParm->hasDefaultArgument()) {
1235 SawDefaultArgument = true;
1236 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1237 } else if (SawDefaultArgument)
1238 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001239 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001240 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001241 // Check for unexpanded parameter packs.
1242 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001243 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001244 UPPC_NonTypeTemplateParameterType)) {
1245 Invalid = true;
1246 continue;
1247 }
1248
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001249 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001250 if (NewNonTypeParm->hasDefaultArgument() &&
1251 DiagnoseDefaultTemplateArgument(*this, TPC,
1252 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001253 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001254 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001255 }
1256
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001258 NonTypeTemplateParmDecl *OldNonTypeParm
1259 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001260 if (NewNonTypeParm->isParameterPack()) {
1261 assert(!NewNonTypeParm->hasDefaultArgument() &&
1262 "Parameter packs can't have a default argument!");
1263 SawParameterPack = true;
1264 ParameterPackLoc = NewNonTypeParm->getLocation();
1265 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001266 NewNonTypeParm->hasDefaultArgument()) {
1267 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1268 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1269 SawDefaultArgument = true;
1270 RedundantDefaultArg = true;
1271 PreviousDefaultArgLoc = NewDefaultLoc;
1272 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1273 // Merge the default argument from the old declaration to the
1274 // new declaration.
1275 SawDefaultArgument = true;
1276 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001277 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001278 // parameter.
1279 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001280 OldNonTypeParm->getDefaultArgument(),
1281 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001282 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1283 } else if (NewNonTypeParm->hasDefaultArgument()) {
1284 SawDefaultArgument = true;
1285 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1286 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001287 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001288 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001289 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001290 TemplateTemplateParmDecl *NewTemplateParm
1291 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001292
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001293 // Check for unexpanded parameter packs, recursively.
1294 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1295 Invalid = true;
1296 continue;
1297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001298
1299 if (NewTemplateParm->hasDefaultArgument() &&
1300 DiagnoseDefaultTemplateArgument(*this, TPC,
1301 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001302 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001304
1305 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001306 TemplateTemplateParmDecl *OldTemplateParm
1307 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001308 if (NewTemplateParm->isParameterPack()) {
1309 assert(!NewTemplateParm->hasDefaultArgument() &&
1310 "Parameter packs can't have a default argument!");
1311 SawParameterPack = true;
1312 ParameterPackLoc = NewTemplateParm->getLocation();
1313 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001314 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001315 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1316 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001317 SawDefaultArgument = true;
1318 RedundantDefaultArg = true;
1319 PreviousDefaultArgLoc = NewDefaultLoc;
1320 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1321 // Merge the default argument from the old declaration to the
1322 // new declaration.
1323 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001324 // FIXME: We need to create a new kind of "default argument" expression
1325 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001326 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001327 OldTemplateParm->getDefaultArgument(),
1328 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001329 PreviousDefaultArgLoc
1330 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001331 } else if (NewTemplateParm->hasDefaultArgument()) {
1332 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001333 PreviousDefaultArgLoc
1334 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001335 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001336 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001337 }
1338
1339 if (RedundantDefaultArg) {
1340 // C++ [temp.param]p12:
1341 // A template-parameter shall not be given default arguments
1342 // by two different declarations in the same scope.
1343 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1344 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1345 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001346 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001347 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001348 // If a template-parameter of a class template has a default
1349 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001350 // have a default template-argument supplied or be a template parameter
1351 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001352 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001353 diag::err_template_param_default_arg_missing);
1354 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1355 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001356 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001357 }
1358
1359 // If we have an old template parameter list that we're merging
1360 // in, move on to the next parameter.
1361 if (OldParams)
1362 ++OldParam;
1363 }
1364
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001365 // We were missing some default arguments at the end of the list, so remove
1366 // all of the default arguments.
1367 if (RemoveDefaultArguments) {
1368 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1369 NewParamEnd = NewParams->end();
1370 NewParam != NewParamEnd; ++NewParam) {
1371 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1372 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001373 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001374 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1375 NTTP->removeDefaultArgument();
1376 else
1377 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1378 }
1379 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001380
Douglas Gregord684b002009-02-10 19:49:53 +00001381 return Invalid;
1382}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001383
John McCall4e2cbb22010-10-20 05:44:58 +00001384namespace {
1385
1386/// A class which looks for a use of a certain level of template
1387/// parameter.
1388struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1389 typedef RecursiveASTVisitor<DependencyChecker> super;
1390
1391 unsigned Depth;
1392 bool Match;
1393
1394 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1395 NamedDecl *ND = Params->getParam(0);
1396 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1397 Depth = PD->getDepth();
1398 } else if (NonTypeTemplateParmDecl *PD =
1399 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1400 Depth = PD->getDepth();
1401 } else {
1402 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1403 }
1404 }
1405
1406 bool Matches(unsigned ParmDepth) {
1407 if (ParmDepth >= Depth) {
1408 Match = true;
1409 return true;
1410 }
1411 return false;
1412 }
1413
1414 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1415 return !Matches(T->getDepth());
1416 }
1417
1418 bool TraverseTemplateName(TemplateName N) {
1419 if (TemplateTemplateParmDecl *PD =
1420 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1421 if (Matches(PD->getDepth())) return false;
1422 return super::TraverseTemplateName(N);
1423 }
1424
1425 bool VisitDeclRefExpr(DeclRefExpr *E) {
1426 if (NonTypeTemplateParmDecl *PD =
1427 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1428 if (PD->getDepth() == Depth) {
1429 Match = true;
1430 return false;
1431 }
1432 }
1433 return super::VisitDeclRefExpr(E);
1434 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001435
1436 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1437 return TraverseType(T->getInjectedSpecializationType());
1438 }
John McCall4e2cbb22010-10-20 05:44:58 +00001439};
1440}
1441
Douglas Gregorc8406492011-05-10 18:27:06 +00001442/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001443/// list.
1444static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001445DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001446 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001447 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001448 return Checker.Match;
1449}
1450
Douglas Gregorc8406492011-05-10 18:27:06 +00001451// Find the source range corresponding to the named type in the given
1452// nested-name-specifier, if any.
1453static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1454 QualType T,
1455 const CXXScopeSpec &SS) {
1456 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1457 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1458 if (const Type *CurType = NNS->getAsType()) {
1459 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1460 return NNSLoc.getTypeLoc().getSourceRange();
1461 } else
1462 break;
1463
1464 NNSLoc = NNSLoc.getPrefix();
1465 }
1466
1467 return SourceRange();
1468}
1469
Mike Stump1eb44332009-09-09 15:08:12 +00001470/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001471/// specifier, returning the template parameter list that applies to the
1472/// name.
1473///
1474/// \param DeclStartLoc the start of the declaration that has a scope
1475/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001476///
Douglas Gregorc8406492011-05-10 18:27:06 +00001477/// \param DeclLoc The location of the declaration itself.
1478///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001479/// \param SS the scope specifier that will be matched to the given template
1480/// parameter lists. This scope specifier precedes a qualified name that is
1481/// being declared.
1482///
1483/// \param ParamLists the template parameter lists, from the outermost to the
1484/// innermost template parameter lists.
1485///
1486/// \param NumParamLists the number of template parameter lists in ParamLists.
1487///
John McCall77e8b112010-04-13 20:37:33 +00001488/// \param IsFriend Whether to apply the slightly different rules for
1489/// matching template parameters to scope specifiers in friend
1490/// declarations.
1491///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001492/// \param IsExplicitSpecialization will be set true if the entity being
1493/// declared is an explicit specialization, false otherwise.
1494///
Mike Stump1eb44332009-09-09 15:08:12 +00001495/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001496/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001497/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001498/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001499/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001500/// itself a template).
1501TemplateParameterList *
1502Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001503 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504 const CXXScopeSpec &SS,
1505 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001506 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001507 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001508 bool &IsExplicitSpecialization,
1509 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001510 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001511 Invalid = false;
1512
1513 // The sequence of nested types to which we will match up the template
1514 // parameter lists. We first build this list by starting with the type named
1515 // by the nested-name-specifier and walking out until we run out of types.
1516 llvm::SmallVector<QualType, 4> NestedTypes;
1517 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001518 if (SS.getScopeRep()) {
1519 if (CXXRecordDecl *Record
1520 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1521 T = Context.getTypeDeclType(Record);
1522 else
1523 T = QualType(SS.getScopeRep()->getAsType(), 0);
1524 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001525
1526 // If we found an explicit specialization that prevents us from needing
1527 // 'template<>' headers, this will be set to the location of that
1528 // explicit specialization.
1529 SourceLocation ExplicitSpecLoc;
1530
1531 while (!T.isNull()) {
1532 NestedTypes.push_back(T);
1533
1534 // Retrieve the parent of a record type.
1535 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1536 // If this type is an explicit specialization, we're done.
1537 if (ClassTemplateSpecializationDecl *Spec
1538 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1539 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1540 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1541 ExplicitSpecLoc = Spec->getLocation();
1542 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001543 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001544 } else if (Record->getTemplateSpecializationKind()
1545 == TSK_ExplicitSpecialization) {
1546 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001547 break;
1548 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001549
1550 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1551 T = Context.getTypeDeclType(Parent);
1552 else
1553 T = QualType();
1554 continue;
1555 }
1556
1557 if (const TemplateSpecializationType *TST
1558 = T->getAs<TemplateSpecializationType>()) {
1559 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1560 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1561 T = Context.getTypeDeclType(Parent);
1562 else
1563 T = QualType();
1564 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001565 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001566 }
1567
1568 // Look one step prior in a dependent template specialization type.
1569 if (const DependentTemplateSpecializationType *DependentTST
1570 = T->getAs<DependentTemplateSpecializationType>()) {
1571 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1572 T = QualType(NNS->getAsType(), 0);
1573 else
1574 T = QualType();
1575 continue;
1576 }
1577
1578 // Look one step prior in a dependent name type.
1579 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1580 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1581 T = QualType(NNS->getAsType(), 0);
1582 else
1583 T = QualType();
1584 continue;
1585 }
1586
1587 // Retrieve the parent of an enumeration type.
1588 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1589 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1590 // check here.
1591 EnumDecl *Enum = EnumT->getDecl();
1592
1593 // Get to the parent type.
1594 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1595 T = Context.getTypeDeclType(Parent);
1596 else
1597 T = QualType();
1598 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregorc8406492011-05-10 18:27:06 +00001601 T = QualType();
1602 }
1603 // Reverse the nested types list, since we want to traverse from the outermost
1604 // to the innermost while checking template-parameter-lists.
1605 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001606
Douglas Gregorc8406492011-05-10 18:27:06 +00001607 // C++0x [temp.expl.spec]p17:
1608 // A member or a member template may be nested within many
1609 // enclosing class templates. In an explicit specialization for
1610 // such a member, the member declaration shall be preceded by a
1611 // template<> for each enclosing class template that is
1612 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001613 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001614 unsigned ParamIdx = 0;
1615 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1616 ++TypeIdx) {
1617 T = NestedTypes[TypeIdx];
1618
1619 // Whether we expect a 'template<>' header.
1620 bool NeedEmptyTemplateHeader = false;
1621
1622 // Whether we expect a template header with parameters.
1623 bool NeedNonemptyTemplateHeader = false;
1624
1625 // For a dependent type, the set of template parameters that we
1626 // expect to see.
1627 TemplateParameterList *ExpectedTemplateParams = 0;
1628
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001629 // C++0x [temp.expl.spec]p15:
1630 // A member or a member template may be nested within many enclosing
1631 // class templates. In an explicit specialization for such a member, the
1632 // member declaration shall be preceded by a template<> for each
1633 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001634 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1635 if (ClassTemplatePartialSpecializationDecl *Partial
1636 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1637 ExpectedTemplateParams = Partial->getTemplateParameters();
1638 NeedNonemptyTemplateHeader = true;
1639 } else if (Record->isDependentType()) {
1640 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001641 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001642 ->getTemplateParameters();
1643 NeedNonemptyTemplateHeader = true;
1644 }
1645 } else if (ClassTemplateSpecializationDecl *Spec
1646 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1647 // C++0x [temp.expl.spec]p4:
1648 // Members of an explicitly specialized class template are defined
1649 // in the same manner as members of normal classes, and not using
1650 // the template<> syntax.
1651 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1652 NeedEmptyTemplateHeader = true;
1653 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001654 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001655 } else if (Record->getTemplateSpecializationKind()) {
1656 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001657 != TSK_ExplicitSpecialization &&
1658 TypeIdx == NumTypes - 1)
1659 IsExplicitSpecialization = true;
1660
1661 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001662 }
1663 } else if (const TemplateSpecializationType *TST
1664 = T->getAs<TemplateSpecializationType>()) {
1665 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1666 ExpectedTemplateParams = Template->getTemplateParameters();
1667 NeedNonemptyTemplateHeader = true;
1668 }
1669 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1670 // FIXME: We actually could/should check the template arguments here
1671 // against the corresponding template parameter list.
1672 NeedNonemptyTemplateHeader = false;
1673 }
1674
Douglas Gregor89b9f102011-06-06 15:22:55 +00001675 // C++ [temp.expl.spec]p16:
1676 // In an explicit specialization declaration for a member of a class
1677 // template or a member template that ap- pears in namespace scope, the
1678 // member template and some of its enclosing class templates may remain
1679 // unspecialized, except that the declaration shall not explicitly
1680 // specialize a class member template if its en- closing class templates
1681 // are not explicitly specialized as well.
1682 if (ParamIdx < NumParamLists) {
1683 if (ParamLists[ParamIdx]->size() == 0) {
1684 if (SawNonEmptyTemplateParameterList) {
1685 Diag(DeclLoc, diag::err_specialize_member_of_template)
1686 << ParamLists[ParamIdx]->getSourceRange();
1687 Invalid = true;
1688 IsExplicitSpecialization = false;
1689 return 0;
1690 }
1691 } else
1692 SawNonEmptyTemplateParameterList = true;
1693 }
1694
Douglas Gregorc8406492011-05-10 18:27:06 +00001695 if (NeedEmptyTemplateHeader) {
1696 // If we're on the last of the types, and we need a 'template<>' header
1697 // here, then it's an explicit specialization.
1698 if (TypeIdx == NumTypes - 1)
1699 IsExplicitSpecialization = true;
1700
1701 if (ParamIdx < NumParamLists) {
1702 if (ParamLists[ParamIdx]->size() > 0) {
1703 // The header has template parameters when it shouldn't. Complain.
1704 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1705 diag::err_template_param_list_matches_nontemplate)
1706 << T
1707 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1708 ParamLists[ParamIdx]->getRAngleLoc())
1709 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1710 Invalid = true;
1711 return 0;
1712 }
1713
1714 // Consume this template header.
1715 ++ParamIdx;
1716 continue;
1717 }
1718
1719 if (!IsFriend) {
1720 // We don't have a template header, but we should.
1721 SourceLocation ExpectedTemplateLoc;
1722 if (NumParamLists > 0)
1723 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1724 else
1725 ExpectedTemplateLoc = DeclStartLoc;
1726
1727 Diag(DeclLoc, diag::err_template_spec_needs_header)
1728 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1729 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1730 }
1731
1732 continue;
1733 }
1734
1735 if (NeedNonemptyTemplateHeader) {
1736 // In friend declarations we can have template-ids which don't
1737 // depend on the corresponding template parameter lists. But
1738 // assume that empty parameter lists are supposed to match this
1739 // template-id.
1740 if (IsFriend && T->isDependentType()) {
1741 if (ParamIdx < NumParamLists &&
1742 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1743 ExpectedTemplateParams = 0;
1744 else
1745 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001746 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001747
Douglas Gregorc8406492011-05-10 18:27:06 +00001748 if (ParamIdx < NumParamLists) {
1749 // Check the template parameter list, if we can.
1750 if (ExpectedTemplateParams &&
1751 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1752 ExpectedTemplateParams,
1753 true, TPL_TemplateMatch))
1754 Invalid = true;
1755
1756 if (!Invalid &&
1757 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1758 TPC_ClassTemplateMember))
1759 Invalid = true;
1760
1761 ++ParamIdx;
1762 continue;
1763 }
1764
1765 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1766 << T
1767 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1768 Invalid = true;
1769 continue;
1770 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001771 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001772
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001773 // If there were at least as many template-ids as there were template
1774 // parameter lists, then there are no template parameter lists remaining for
1775 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001776 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001777 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001779 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001780 if (ParamIdx < NumParamLists - 1) {
1781 bool HasAnyExplicitSpecHeader = false;
1782 bool AllExplicitSpecHeaders = true;
1783 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1784 if (ParamLists[I]->size() == 0)
1785 HasAnyExplicitSpecHeader = true;
1786 else
1787 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001788 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001789
1790 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1791 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1792 : diag::err_template_spec_extra_headers)
1793 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1794 ParamLists[NumParamLists - 2]->getRAngleLoc());
1795
1796 // If there was a specialization somewhere, such that 'template<>' is
1797 // not required, and there were any 'template<>' headers, note where the
1798 // specialization occurred.
1799 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1800 Diag(ExplicitSpecLoc,
1801 diag::note_explicit_template_spec_does_not_need_header)
1802 << NestedTypes.back();
1803
1804 // We have a template parameter list with no corresponding scope, which
1805 // means that the resulting template declaration can't be instantiated
1806 // properly (we'll end up with dependent nodes when we shouldn't).
1807 if (!AllExplicitSpecHeaders)
1808 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Douglas Gregor89b9f102011-06-06 15:22:55 +00001811 // C++ [temp.expl.spec]p16:
1812 // In an explicit specialization declaration for a member of a class
1813 // template or a member template that ap- pears in namespace scope, the
1814 // member template and some of its enclosing class templates may remain
1815 // unspecialized, except that the declaration shall not explicitly
1816 // specialize a class member template if its en- closing class templates
1817 // are not explicitly specialized as well.
1818 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1819 SawNonEmptyTemplateParameterList) {
1820 Diag(DeclLoc, diag::err_specialize_member_of_template)
1821 << ParamLists[ParamIdx]->getSourceRange();
1822 Invalid = true;
1823 IsExplicitSpecialization = false;
1824 return 0;
1825 }
1826
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001827 // Return the last template parameter list, which corresponds to the
1828 // entity being declared.
1829 return ParamLists[NumParamLists - 1];
1830}
1831
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001832void Sema::NoteAllFoundTemplates(TemplateName Name) {
1833 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1834 Diag(Template->getLocation(), diag::note_template_declared_here)
1835 << (isa<FunctionTemplateDecl>(Template)? 0
1836 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001837 : isa<TypeAliasTemplateDecl>(Template)? 2
1838 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001839 << Template->getDeclName();
1840 return;
1841 }
1842
1843 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1844 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1845 IEnd = OST->end();
1846 I != IEnd; ++I)
1847 Diag((*I)->getLocation(), diag::note_template_declared_here)
1848 << 0 << (*I)->getDeclName();
1849
1850 return;
1851 }
1852}
1853
1854
Douglas Gregor7532dc62009-03-30 22:58:21 +00001855QualType Sema::CheckTemplateIdType(TemplateName Name,
1856 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001857 TemplateArgumentListInfo &TemplateArgs) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001858 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1859 if (DTN && DTN->isIdentifier())
1860 // When building a template-id where the template-name is dependent,
1861 // assume the template is a type template. Either our assumption is
1862 // correct, or the code is ill-formed and will be diagnosed when the
1863 // dependent name is substituted.
1864 return Context.getDependentTemplateSpecializationType(ETK_None,
1865 DTN->getQualifier(),
1866 DTN->getIdentifier(),
1867 TemplateArgs);
1868
Douglas Gregor7532dc62009-03-30 22:58:21 +00001869 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001870 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1871 // We might have a substituted template template parameter pack. If so,
1872 // build a template specialization type for it.
1873 if (Name.getAsSubstTemplateTemplateParmPack())
1874 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001875
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001876 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1877 << Name;
1878 NoteAllFoundTemplates(Name);
1879 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001880 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001881
Douglas Gregor40808ce2009-03-09 23:48:35 +00001882 // Check that the template argument list is well-formed for this
1883 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001884 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001885 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001886 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001887 return QualType();
1888
Douglas Gregor910f8002010-11-07 23:05:16 +00001889 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001890 "Converted template argument list is too short!");
1891
1892 QualType CanonType;
1893
Richard Smith3e4c6c42011-05-05 21:57:07 +00001894 if (TypeAliasTemplateDecl *AliasTemplate
1895 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1896 // Find the canonical type for this type alias template specialization.
1897 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1898 if (Pattern->isInvalidDecl())
1899 return QualType();
1900
1901 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1902 Converted.data(), Converted.size());
1903
1904 // Only substitute for the innermost template argument list.
1905 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001906 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001907 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1908 for (unsigned I = 0; I < Depth; ++I)
1909 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001910
1911 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1912 CanonType = SubstType(Pattern->getUnderlyingType(),
1913 TemplateArgLists, AliasTemplate->getLocation(),
1914 AliasTemplate->getDeclName());
1915 if (CanonType.isNull())
1916 return QualType();
1917 } else if (Name.isDependent() ||
1918 TemplateSpecializationType::anyDependentTemplateArguments(
1919 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001920 // This class template specialization is a dependent
1921 // type. Therefore, its canonical type is another class template
1922 // specialization type that contains all of the converted
1923 // arguments in canonical form. This ensures that, e.g., A<T> and
1924 // A<T, T> have identical types when A is declared as:
1925 //
1926 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001927 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001928 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001929 Converted.data(),
1930 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregor1275ae02009-07-28 23:00:59 +00001932 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001933 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001934 // In the future, we need to teach getTemplateSpecializationType to only
1935 // build the canonical type and return that to us.
1936 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001937
1938 // This might work out to be a current instantiation, in which
1939 // case the canonical type needs to be the InjectedClassNameType.
1940 //
1941 // TODO: in theory this could be a simple hashtable lookup; most
1942 // changes to CurContext don't change the set of current
1943 // instantiations.
1944 if (isa<ClassTemplateDecl>(Template)) {
1945 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1946 // If we get out to a namespace, we're done.
1947 if (Ctx->isFileContext()) break;
1948
1949 // If this isn't a record, keep looking.
1950 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1951 if (!Record) continue;
1952
1953 // Look for one of the two cases with InjectedClassNameTypes
1954 // and check whether it's the same template.
1955 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1956 !Record->getDescribedClassTemplate())
1957 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001958
John McCall31f17ec2010-04-27 00:57:59 +00001959 // Fetch the injected class name type and check whether its
1960 // injected type is equal to the type we just built.
1961 QualType ICNT = Context.getTypeDeclType(Record);
1962 QualType Injected = cast<InjectedClassNameType>(ICNT)
1963 ->getInjectedSpecializationType();
1964
1965 if (CanonType != Injected->getCanonicalTypeInternal())
1966 continue;
1967
1968 // If so, the canonical type of this TST is the injected
1969 // class name type of the record we just found.
1970 assert(ICNT.isCanonical());
1971 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001972 break;
1973 }
1974 }
Mike Stump1eb44332009-09-09 15:08:12 +00001975 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001976 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001977 // Find the class template specialization declaration that
1978 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001979 void *InsertPos = 0;
1980 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001981 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001982 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001983 if (!Decl) {
1984 // This is the first time we have referenced this class template
1985 // specialization. Create the canonical declaration and add it to
1986 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001987 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001988 ClassTemplate->getTemplatedDecl()->getTagKind(),
1989 ClassTemplate->getDeclContext(),
1990 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001991 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001992 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001993 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001994 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001995 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001996 Decl->setLexicalDeclContext(CurContext);
1997 }
1998
1999 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002000 assert(isa<RecordType>(CanonType) &&
2001 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Douglas Gregor40808ce2009-03-09 23:48:35 +00002004 // Build the fully-sugared type for this class template
2005 // specialization, which refers back to the class template
2006 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002007 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002008}
2009
John McCallf312b1e2010-08-26 23:41:50 +00002010TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002011Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2012 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002013 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002014 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002015 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002016 if (SS.isInvalid())
2017 return true;
2018
Douglas Gregor7532dc62009-03-30 22:58:21 +00002019 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002020
Douglas Gregor40808ce2009-03-09 23:48:35 +00002021 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002022 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002023 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002024
Douglas Gregora88f09f2011-02-28 17:23:35 +00002025 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2026 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2027 DTN->getQualifier(),
2028 DTN->getIdentifier(),
2029 TemplateArgs);
2030
2031 // Build type-source information.
2032 TypeLocBuilder TLB;
2033 DependentTemplateSpecializationTypeLoc SpecTL
2034 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002035 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002036 SpecTL.setNameLoc(TemplateLoc);
2037 SpecTL.setLAngleLoc(LAngleLoc);
2038 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002039 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002040 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2041 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2042 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2043 }
2044
John McCalld5532b62009-11-23 01:53:49 +00002045 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002046 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002047
2048 if (Result.isNull())
2049 return true;
2050
Douglas Gregor059101f2011-03-02 00:47:37 +00002051 // Build type-source information.
2052 TypeLocBuilder TLB;
2053 TemplateSpecializationTypeLoc SpecTL
2054 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2055 SpecTL.setTemplateNameLoc(TemplateLoc);
2056 SpecTL.setLAngleLoc(LAngleLoc);
2057 SpecTL.setRAngleLoc(RAngleLoc);
2058 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2059 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002060
Douglas Gregor059101f2011-03-02 00:47:37 +00002061 if (SS.isNotEmpty()) {
2062 // Create an elaborated-type-specifier containing the nested-name-specifier.
2063 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2064 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2065 ElabTL.setKeywordLoc(SourceLocation());
2066 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2067 }
2068
2069 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002070}
John McCallf1bbbb42009-09-04 01:14:41 +00002071
Douglas Gregor059101f2011-03-02 00:47:37 +00002072TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002073 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002074 SourceLocation TagLoc,
2075 CXXScopeSpec &SS,
2076 TemplateTy TemplateD,
2077 SourceLocation TemplateLoc,
2078 SourceLocation LAngleLoc,
2079 ASTTemplateArgsPtr TemplateArgsIn,
2080 SourceLocation RAngleLoc) {
2081 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2082
2083 // Translate the parser's template argument list in our AST format.
2084 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2085 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2086
2087 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002088 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002089 ElaboratedTypeKeyword Keyword
2090 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Douglas Gregor059101f2011-03-02 00:47:37 +00002092 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2093 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2094 DTN->getQualifier(),
2095 DTN->getIdentifier(),
2096 TemplateArgs);
2097
2098 // Build type-source information.
2099 TypeLocBuilder TLB;
2100 DependentTemplateSpecializationTypeLoc SpecTL
2101 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2102 SpecTL.setKeywordLoc(TagLoc);
2103 SpecTL.setNameLoc(TemplateLoc);
2104 SpecTL.setLAngleLoc(LAngleLoc);
2105 SpecTL.setRAngleLoc(RAngleLoc);
2106 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2107 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2108 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2109 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2110 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002111
2112 if (TypeAliasTemplateDecl *TAT =
2113 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2114 // C++0x [dcl.type.elab]p2:
2115 // If the identifier resolves to a typedef-name or the simple-template-id
2116 // resolves to an alias template specialization, the
2117 // elaborated-type-specifier is ill-formed.
2118 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2119 Diag(TAT->getLocation(), diag::note_declared_at);
2120 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002121
2122 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2123 if (Result.isNull())
2124 return TypeResult();
2125
2126 // Check the tag kind
2127 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002128 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002129
John McCall6b2becf2009-09-08 17:47:29 +00002130 IdentifierInfo *Id = D->getIdentifier();
2131 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002132
John McCall6b2becf2009-09-08 17:47:29 +00002133 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
2134 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002135 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002136 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002137 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002138 }
2139 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002140
2141 // Provide source-location information for the template specialization.
2142 TypeLocBuilder TLB;
2143 TemplateSpecializationTypeLoc SpecTL
2144 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2145 SpecTL.setTemplateNameLoc(TemplateLoc);
2146 SpecTL.setLAngleLoc(LAngleLoc);
2147 SpecTL.setRAngleLoc(RAngleLoc);
2148 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2149 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002150
Douglas Gregor059101f2011-03-02 00:47:37 +00002151 // Construct an elaborated type containing the nested-name-specifier (if any)
2152 // and keyword.
2153 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2154 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2155 ElabTL.setKeywordLoc(TagLoc);
2156 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2157 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002158}
2159
John McCall60d7b3a2010-08-24 06:29:42 +00002160ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002161 LookupResult &R,
2162 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002163 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002164 // FIXME: Can we do any checking at this point? I guess we could check the
2165 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002166 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002167 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002168 // foo<int> could identify a single function unambiguously
2169 // This approach does NOT work, since f<int>(1);
2170 // gets resolved prior to resorting to overload resolution
2171 // i.e., template<class T> void f(double);
2172 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002173
2174 // These should be filtered out by our callers.
2175 assert(!R.empty() && "empty lookup results when building templateid");
2176 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2177
John McCallc373d482010-01-27 01:50:18 +00002178 // We don't want lookup warnings at this point.
2179 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002180
John McCallf7a1a742009-11-24 19:00:30 +00002181 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002182 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002183 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002184 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002185 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002186 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002187
2188 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002189}
2190
John McCallf7a1a742009-11-24 19:00:30 +00002191// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002192ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002193Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002194 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002195 const TemplateArgumentListInfo &TemplateArgs) {
2196 DeclContext *DC;
2197 if (!(DC = computeDeclContext(SS, false)) ||
2198 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002199 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002200 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002202 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002203 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002204 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2205 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002206
John McCallf7a1a742009-11-24 19:00:30 +00002207 if (R.isAmbiguous())
2208 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002209
John McCallf7a1a742009-11-24 19:00:30 +00002210 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002211 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2212 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002213 return ExprError();
2214 }
2215
2216 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002217 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2218 << (NestedNameSpecifier*) SS.getScopeRep()
2219 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002220 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2221 return ExprError();
2222 }
2223
2224 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002225}
2226
Douglas Gregorc45c2322009-03-31 00:43:58 +00002227/// \brief Form a dependent template name.
2228///
2229/// This action forms a dependent template name given the template
2230/// name and its (presumably dependent) scope specifier. For
2231/// example, given "MetaFun::template apply", the scope specifier \p
2232/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2233/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002234TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002235 SourceLocation TemplateKWLoc,
2236 CXXScopeSpec &SS,
2237 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002238 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002239 bool EnteringContext,
2240 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002241 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2242 !getLangOptions().CPlusPlus0x)
2243 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002244 << FixItHint::CreateRemoval(TemplateKWLoc);
2245
Douglas Gregor0707bc52010-01-19 16:01:07 +00002246 DeclContext *LookupCtx = 0;
2247 if (SS.isSet())
2248 LookupCtx = computeDeclContext(SS, EnteringContext);
2249 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002250 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002251 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002252 // C++0x [temp.names]p5:
2253 // If a name prefixed by the keyword template is not the name of
2254 // a template, the program is ill-formed. [Note: the keyword
2255 // template may not be applied to non-template members of class
2256 // templates. -end note ] [ Note: as is the case with the
2257 // typename prefix, the template prefix is allowed in cases
2258 // where it is not strictly necessary; i.e., when the
2259 // nested-name-specifier or the expression on the left of the ->
2260 // or . is not dependent on a template-parameter, or the use
2261 // does not appear in the scope of a template. -end note]
2262 //
2263 // Note: C++03 was more strict here, because it banned the use of
2264 // the "template" keyword prior to a template-name that was not a
2265 // dependent name. C++ DR468 relaxed this requirement (the
2266 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002267 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002268 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002269 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2270 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002271 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002272 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2273 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002274 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2275 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002276 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002277 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002278 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002279 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002280 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002281 << Name.getSourceRange()
2282 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002283 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002284 } else {
2285 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002286 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002287 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002288 }
2289
Mike Stump1eb44332009-09-09 15:08:12 +00002290 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002291 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002292
Douglas Gregor014e88d2009-11-03 23:16:33 +00002293 switch (Name.getKind()) {
2294 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002295 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002296 Name.Identifier));
2297 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002298
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002299 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002300 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002301 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002302 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002303
2304 case UnqualifiedId::IK_LiteralOperatorId:
2305 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2306
Douglas Gregor014e88d2009-11-03 23:16:33 +00002307 default:
2308 break;
2309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002310
2311 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002312 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002313 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002314 << Name.getSourceRange()
2315 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002316 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002317}
2318
Mike Stump1eb44332009-09-09 15:08:12 +00002319bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002320 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002321 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002322 const TemplateArgument &Arg = AL.getArgument();
2323
Anders Carlsson436b1562009-06-13 00:33:33 +00002324 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002325 switch(Arg.getKind()) {
2326 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002327 // C++ [temp.arg.type]p1:
2328 // A template-argument for a template-parameter which is a
2329 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002330 break;
2331 case TemplateArgument::Template: {
2332 // We have a template type parameter but the template argument
2333 // is a template without any arguments.
2334 SourceRange SR = AL.getSourceRange();
2335 TemplateName Name = Arg.getAsTemplate();
2336 Diag(SR.getBegin(), diag::err_template_missing_args)
2337 << Name << SR;
2338 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2339 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002340
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002341 return true;
2342 }
2343 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002344 // We have a template type parameter but the template argument
2345 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002346 SourceRange SR = AL.getSourceRange();
2347 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002348 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Anders Carlsson436b1562009-06-13 00:33:33 +00002350 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002351 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002352 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002353
John McCalla93c9342009-12-07 02:54:59 +00002354 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002355 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Anders Carlsson436b1562009-06-13 00:33:33 +00002357 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002358 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002359 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002360 return false;
2361}
2362
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002363/// \brief Substitute template arguments into the default template argument for
2364/// the given template type parameter.
2365///
2366/// \param SemaRef the semantic analysis object for which we are performing
2367/// the substitution.
2368///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002369/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002370/// for.
2371///
2372/// \param TemplateLoc the location of the template name that started the
2373/// template-id we are checking.
2374///
2375/// \param RAngleLoc the location of the right angle bracket ('>') that
2376/// terminates the template-id.
2377///
2378/// \param Param the template template parameter whose default we are
2379/// substituting into.
2380///
2381/// \param Converted the list of template arguments provided for template
2382/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002383/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002384static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002385SubstDefaultTemplateArgument(Sema &SemaRef,
2386 TemplateDecl *Template,
2387 SourceLocation TemplateLoc,
2388 SourceLocation RAngleLoc,
2389 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002390 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002391 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002392
2393 // If the argument type is dependent, instantiate it now based
2394 // on the previously-computed template arguments.
2395 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002396 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002397 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002398
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002399 MultiLevelTemplateArgumentList AllTemplateArgs
2400 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2401
2402 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002403 Template, Converted.data(),
2404 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002405 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002406
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002407 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2408 Param->getDefaultArgumentLoc(),
2409 Param->getDeclName());
2410 }
2411
2412 return ArgType;
2413}
2414
2415/// \brief Substitute template arguments into the default template argument for
2416/// the given non-type template parameter.
2417///
2418/// \param SemaRef the semantic analysis object for which we are performing
2419/// the substitution.
2420///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002421/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002422/// for.
2423///
2424/// \param TemplateLoc the location of the template name that started the
2425/// template-id we are checking.
2426///
2427/// \param RAngleLoc the location of the right angle bracket ('>') that
2428/// terminates the template-id.
2429///
Douglas Gregor788cd062009-11-11 01:00:40 +00002430/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002431/// substituting into.
2432///
2433/// \param Converted the list of template arguments provided for template
2434/// parameters that precede \p Param in the template parameter list.
2435///
2436/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002437static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002438SubstDefaultTemplateArgument(Sema &SemaRef,
2439 TemplateDecl *Template,
2440 SourceLocation TemplateLoc,
2441 SourceLocation RAngleLoc,
2442 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002443 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002445 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002446
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002447 MultiLevelTemplateArgumentList AllTemplateArgs
2448 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002449
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002450 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002451 Template, Converted.data(),
2452 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002453 SourceRange(TemplateLoc, RAngleLoc));
2454
2455 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2456}
2457
Douglas Gregor788cd062009-11-11 01:00:40 +00002458/// \brief Substitute template arguments into the default template argument for
2459/// the given template template parameter.
2460///
2461/// \param SemaRef the semantic analysis object for which we are performing
2462/// the substitution.
2463///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002464/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002465/// for.
2466///
2467/// \param TemplateLoc the location of the template name that started the
2468/// template-id we are checking.
2469///
2470/// \param RAngleLoc the location of the right angle bracket ('>') that
2471/// terminates the template-id.
2472///
2473/// \param Param the template template parameter whose default we are
2474/// substituting into.
2475///
2476/// \param Converted the list of template arguments provided for template
2477/// parameters that precede \p Param in the template parameter list.
2478///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002479/// \param QualifierLoc Will be set to the nested-name-specifier (with
2480/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002481///
Douglas Gregor788cd062009-11-11 01:00:40 +00002482/// \returns the substituted template argument, or NULL if an error occurred.
2483static TemplateName
2484SubstDefaultTemplateArgument(Sema &SemaRef,
2485 TemplateDecl *Template,
2486 SourceLocation TemplateLoc,
2487 SourceLocation RAngleLoc,
2488 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002489 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2490 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002492 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002493
Douglas Gregor788cd062009-11-11 01:00:40 +00002494 MultiLevelTemplateArgumentList AllTemplateArgs
2495 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregor788cd062009-11-11 01:00:40 +00002497 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002498 Template, Converted.data(),
2499 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002500 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002501
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002502 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002503 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002504 if (QualifierLoc) {
2505 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2506 AllTemplateArgs);
2507 if (!QualifierLoc)
2508 return TemplateName();
2509 }
2510
Douglas Gregor1d752d72011-03-02 18:46:51 +00002511 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002512 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002513 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002514 AllTemplateArgs);
2515}
2516
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002517/// \brief If the given template parameter has a default template
2518/// argument, substitute into that default template argument and
2519/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002520TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002521Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2522 SourceLocation TemplateLoc,
2523 SourceLocation RAngleLoc,
2524 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002525 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2526 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002527 if (!TypeParm->hasDefaultArgument())
2528 return TemplateArgumentLoc();
2529
John McCalla93c9342009-12-07 02:54:59 +00002530 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002531 TemplateLoc,
2532 RAngleLoc,
2533 TypeParm,
2534 Converted);
2535 if (DI)
2536 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2537
2538 return TemplateArgumentLoc();
2539 }
2540
2541 if (NonTypeTemplateParmDecl *NonTypeParm
2542 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2543 if (!NonTypeParm->hasDefaultArgument())
2544 return TemplateArgumentLoc();
2545
John McCall60d7b3a2010-08-24 06:29:42 +00002546 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002547 TemplateLoc,
2548 RAngleLoc,
2549 NonTypeParm,
2550 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002551 if (Arg.isInvalid())
2552 return TemplateArgumentLoc();
2553
2554 Expr *ArgE = Arg.takeAs<Expr>();
2555 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2556 }
2557
2558 TemplateTemplateParmDecl *TempTempParm
2559 = cast<TemplateTemplateParmDecl>(Param);
2560 if (!TempTempParm->hasDefaultArgument())
2561 return TemplateArgumentLoc();
2562
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002563
Douglas Gregor1d752d72011-03-02 18:46:51 +00002564 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002565 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002566 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002567 RAngleLoc,
2568 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002569 Converted,
2570 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002571 if (TName.isNull())
2572 return TemplateArgumentLoc();
2573
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002574 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002575 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002576 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2577}
2578
Douglas Gregore7526412009-11-11 19:31:23 +00002579/// \brief Check that the given template argument corresponds to the given
2580/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002581///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002582/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002583/// checked.
2584///
2585/// \param Arg The template argument.
2586///
2587/// \param Template The template in which the template argument resides.
2588///
2589/// \param TemplateLoc The location of the template name for the template
2590/// whose argument list we're matching.
2591///
2592/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2593/// the template argument list.
2594///
2595/// \param ArgumentPackIndex The index into the argument pack where this
2596/// argument will be placed. Only valid if the parameter is a parameter pack.
2597///
2598/// \param Converted The checked, converted argument will be added to the
2599/// end of this small vector.
2600///
2601/// \param CTAK Describes how we arrived at this particular template argument:
2602/// explicitly written, deduced, etc.
2603///
2604/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002605bool Sema::CheckTemplateArgument(NamedDecl *Param,
2606 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002607 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002608 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002609 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002610 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002611 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002612 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002613 // Check template type parameters.
2614 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002615 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616
Douglas Gregord9e15302009-11-11 19:41:09 +00002617 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002618 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002619 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002620 // with the template arguments we've seen thus far. But if the
2621 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002622 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002623 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2624 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002625
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002626 if (NTTPType->isDependentType() &&
2627 !isa<TemplateTemplateParmDecl>(Template) &&
2628 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002629 // Do substitution on the type of the non-type template parameter.
2630 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002631 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002632 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002633
2634 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002635 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002636 NTTPType = SubstType(NTTPType,
2637 MultiLevelTemplateArgumentList(TemplateArgs),
2638 NTTP->getLocation(),
2639 NTTP->getDeclName());
2640 // If that worked, check the non-type template parameter type
2641 // for validity.
2642 if (!NTTPType.isNull())
2643 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2644 NTTP->getLocation());
2645 if (NTTPType.isNull())
2646 return true;
2647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002648
Douglas Gregore7526412009-11-11 19:31:23 +00002649 switch (Arg.getArgument().getKind()) {
2650 case TemplateArgument::Null:
2651 assert(false && "Should never see a NULL template argument here");
2652 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregore7526412009-11-11 19:31:23 +00002654 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002655 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002656 ExprResult Res =
2657 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2658 Result, CTAK);
2659 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002660 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661
Douglas Gregor910f8002010-11-07 23:05:16 +00002662 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002663 break;
2664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002665
Douglas Gregore7526412009-11-11 19:31:23 +00002666 case TemplateArgument::Declaration:
2667 case TemplateArgument::Integral:
2668 // We've already checked this template argument, so just copy
2669 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002670 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002671 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002672
Douglas Gregore7526412009-11-11 19:31:23 +00002673 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002674 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002675 // We were given a template template argument. It may not be ill-formed;
2676 // see below.
2677 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002678 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2679 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002680 // We have a template argument such as \c T::template X, which we
2681 // parsed as a template template argument. However, since we now
2682 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002683 // template name into an expression.
2684
2685 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2686 Arg.getTemplateNameLoc());
2687
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002688 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002689 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002690 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002691 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002692 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002693
Douglas Gregora7fc9012011-01-05 18:58:31 +00002694 // If we parsed the template argument as a pack expansion, create a
2695 // pack expansion expression.
2696 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002697 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2698 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002699 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002701
Douglas Gregore7526412009-11-11 19:31:23 +00002702 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002703 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2704 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002705 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706
Douglas Gregor910f8002010-11-07 23:05:16 +00002707 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002708 break;
2709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710
Douglas Gregore7526412009-11-11 19:31:23 +00002711 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002712 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002713 // therefore cannot be a non-type template argument.
2714 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2715 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002716
Douglas Gregore7526412009-11-11 19:31:23 +00002717 Diag(Param->getLocation(), diag::note_template_param_here);
2718 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002719
Douglas Gregore7526412009-11-11 19:31:23 +00002720 case TemplateArgument::Type: {
2721 // We have a non-type template parameter but the template
2722 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002723
Douglas Gregore7526412009-11-11 19:31:23 +00002724 // C++ [temp.arg]p2:
2725 // In a template-argument, an ambiguity between a type-id and
2726 // an expression is resolved to a type-id, regardless of the
2727 // form of the corresponding template-parameter.
2728 //
2729 // We warn specifically about this case, since it can be rather
2730 // confusing for users.
2731 QualType T = Arg.getArgument().getAsType();
2732 SourceRange SR = Arg.getSourceRange();
2733 if (T->isFunctionType())
2734 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2735 else
2736 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2737 Diag(Param->getLocation(), diag::note_template_param_here);
2738 return true;
2739 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740
Douglas Gregore7526412009-11-11 19:31:23 +00002741 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002742 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002743 break;
2744 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745
Douglas Gregore7526412009-11-11 19:31:23 +00002746 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747 }
2748
2749
Douglas Gregore7526412009-11-11 19:31:23 +00002750 // Check template template parameters.
2751 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregore7526412009-11-11 19:31:23 +00002753 // Substitute into the template parameter list of the template
2754 // template parameter, since previously-supplied template arguments
2755 // may appear within the template template parameter.
2756 {
2757 // Set up a template instantiation context.
2758 LocalInstantiationScope Scope(*this);
2759 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002760 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002761 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762
2763 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002764 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002765 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002767 MultiLevelTemplateArgumentList(TemplateArgs)));
2768 if (!TempParm)
2769 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002770 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002771
Douglas Gregore7526412009-11-11 19:31:23 +00002772 switch (Arg.getArgument().getKind()) {
2773 case TemplateArgument::Null:
2774 assert(false && "Should never see a NULL template argument here");
2775 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
Douglas Gregore7526412009-11-11 19:31:23 +00002777 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002778 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002779 if (CheckTemplateArgument(TempParm, Arg))
2780 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002781
Douglas Gregor910f8002010-11-07 23:05:16 +00002782 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002783 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002784
Douglas Gregore7526412009-11-11 19:31:23 +00002785 case TemplateArgument::Expression:
2786 case TemplateArgument::Type:
2787 // We have a template template parameter but the template
2788 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002789 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2790 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002791 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002792
Douglas Gregore7526412009-11-11 19:31:23 +00002793 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002794 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002795 "Declaration argument with template template parameter");
2796 break;
2797 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002798 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002799 "Integral argument with template template parameter");
2800 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002801
Douglas Gregore7526412009-11-11 19:31:23 +00002802 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002803 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002804 break;
2805 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002806
Douglas Gregore7526412009-11-11 19:31:23 +00002807 return false;
2808}
2809
Douglas Gregorc15cb382009-02-09 23:23:08 +00002810/// \brief Check that the given template argument list is well-formed
2811/// for specializing the given template.
2812bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2813 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002814 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002815 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002816 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002817 TemplateParameterList *Params = Template->getTemplateParameters();
2818 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002819 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002820 bool Invalid = false;
2821
John McCalld5532b62009-11-23 01:53:49 +00002822 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2823
Mike Stump1eb44332009-09-09 15:08:12 +00002824 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002825 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002827 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002828 (NumArgs < Params->getMinRequiredArguments() &&
2829 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002830 // FIXME: point at either the first arg beyond what we can handle,
2831 // or the '>', depending on whether we have too many or too few
2832 // arguments.
2833 SourceRange Range;
2834 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002835 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002836 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2837 << (NumArgs > NumParams)
2838 << (isa<ClassTemplateDecl>(Template)? 0 :
2839 isa<FunctionTemplateDecl>(Template)? 1 :
2840 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2841 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002842 Diag(Template->getLocation(), diag::note_template_decl_here)
2843 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002844 Invalid = true;
2845 }
Mike Stump1eb44332009-09-09 15:08:12 +00002846
2847 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002848 // [...] The type and form of each template-argument specified in
2849 // a template-id shall match the type and form specified for the
2850 // corresponding parameter declared by the template in its
2851 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002852 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002853 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2854 TemplateParameterList::iterator Param = Params->begin(),
2855 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002856 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002857 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002858 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002859 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002860 // If we have an expanded parameter pack, make sure we don't have too
2861 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002862 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002863 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002864 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002865 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2866 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2867 << true
2868 << (isa<ClassTemplateDecl>(Template)? 0 :
2869 isa<FunctionTemplateDecl>(Template)? 1 :
2870 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2871 << Template;
2872 Diag(Template->getLocation(), diag::note_template_decl_here)
2873 << Params->getSourceRange();
2874 return true;
2875 }
2876 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002877
Douglas Gregorf35f8282009-11-11 21:54:23 +00002878 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002879 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2880 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002881 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002882 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002883
Douglas Gregor14be16b2010-12-20 16:57:52 +00002884 if ((*Param)->isTemplateParameterPack()) {
2885 // The template parameter was a template parameter pack, so take the
2886 // deduced argument and place it on the argument pack. Note that we
2887 // stay on the same template parameter so that we can deduce more
2888 // arguments.
2889 ArgumentPack.push_back(Converted.back());
2890 Converted.pop_back();
2891 } else {
2892 // Move to the next template parameter.
2893 ++Param;
2894 }
2895 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002896 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002897 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002898
Douglas Gregor8735b292011-06-03 02:59:40 +00002899 // If we're checking a partial template argument list, we're done.
2900 if (PartialTemplateArgs) {
2901 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2902 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2903 ArgumentPack.data(),
2904 ArgumentPack.size()));
2905
2906 return Invalid;
2907 }
2908
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002909 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002910 // arguments, just break out now and we'll fill in the argument pack below.
2911 if ((*Param)->isTemplateParameterPack())
2912 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002913
Douglas Gregorf968d832011-05-27 01:19:52 +00002914 // If our template is a template template parameter that hasn't acquired
2915 // its proper context yet (e.g., because we're using the template template
2916 // parameter in the signature of a function template, before we've built
2917 // the function template itself), don't attempt substitution of default
2918 // template arguments at this point: we don't have enough context to
2919 // do it properly.
2920 if (isTemplateTemplateParameter &&
2921 Template->getDeclContext()->isTranslationUnit())
2922 break;
2923
Douglas Gregorf35f8282009-11-11 21:54:23 +00002924 // We have a default template argument that we will use.
2925 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002926
Douglas Gregorf35f8282009-11-11 21:54:23 +00002927 // Retrieve the default template argument from the template
2928 // parameter. For each kind of template parameter, we substitute the
2929 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002931 // the default argument.
2932 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2933 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002934 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002935 break;
2936 }
2937
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002938 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002939 Template,
2940 TemplateLoc,
2941 RAngleLoc,
2942 TTP,
2943 Converted);
2944 if (!ArgType)
2945 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002946
Douglas Gregorf35f8282009-11-11 21:54:23 +00002947 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2948 ArgType);
2949 } else if (NonTypeTemplateParmDecl *NTTP
2950 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2951 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002952 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002953 break;
2954 }
2955
John McCall60d7b3a2010-08-24 06:29:42 +00002956 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002957 TemplateLoc,
2958 RAngleLoc,
2959 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002960 Converted);
2961 if (E.isInvalid())
2962 return true;
2963
2964 Expr *Ex = E.takeAs<Expr>();
2965 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2966 } else {
2967 TemplateTemplateParmDecl *TempParm
2968 = cast<TemplateTemplateParmDecl>(*Param);
2969
2970 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002971 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002972 break;
2973 }
2974
Douglas Gregor1d752d72011-03-02 18:46:51 +00002975 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002976 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002977 TemplateLoc,
2978 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002979 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002980 Converted,
2981 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002982 if (Name.isNull())
2983 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002984
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002985 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2986 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002987 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002988
Douglas Gregorf35f8282009-11-11 21:54:23 +00002989 // Introduce an instantiation record that describes where we are using
2990 // the default template argument.
2991 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002992 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002993 SourceRange(TemplateLoc, RAngleLoc));
2994
Douglas Gregorf35f8282009-11-11 21:54:23 +00002995 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002996 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002997 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002998 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002999
Douglas Gregor67714232011-03-03 02:41:12 +00003000 // Core issue 150 (assumed resolution): if this is a template template
3001 // parameter, keep track of the default template arguments from the
3002 // template definition.
3003 if (isTemplateTemplateParameter)
3004 TemplateArgs.addArgument(Arg);
3005
Douglas Gregor14be16b2010-12-20 16:57:52 +00003006 // Move to the next template parameter and argument.
3007 ++Param;
3008 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003009 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003010
Douglas Gregor14be16b2010-12-20 16:57:52 +00003011 // Form argument packs for each of the parameter packs remaining.
3012 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003013 // If we're checking a partial list of template arguments, don't fill
3014 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003015
3016 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003017 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003018 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003019 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3021 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003022 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003023 ArgumentPack.clear();
3024 }
3025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
Douglas Gregor14be16b2010-12-20 16:57:52 +00003027 ++Param;
3028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003029
Douglas Gregorc15cb382009-02-09 23:23:08 +00003030 return Invalid;
3031}
3032
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003033namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003034 class UnnamedLocalNoLinkageFinder
3035 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003036 {
3037 Sema &S;
3038 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003039
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003040 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003041
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003042 public:
3043 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3044
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003045 bool Visit(QualType T) {
3046 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003048
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003049#define TYPE(Class, Parent) \
3050 bool Visit##Class##Type(const Class##Type *);
3051#define ABSTRACT_TYPE(Class, Parent) \
3052 bool Visit##Class##Type(const Class##Type *) { return false; }
3053#define NON_CANONICAL_TYPE(Class, Parent) \
3054 bool Visit##Class##Type(const Class##Type *) { return false; }
3055#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003057 bool VisitTagDecl(const TagDecl *Tag);
3058 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3059 };
3060}
3061
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003062bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003063 return false;
3064}
3065
3066bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3067 return Visit(T->getElementType());
3068}
3069
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003070bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003071 return Visit(T->getPointeeType());
3072}
3073
3074bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 return Visit(T->getPointeeType());
3077}
3078
3079bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003080 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003081 return Visit(T->getPointeeType());
3082}
3083
3084bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003085 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003086 return Visit(T->getPointeeType());
3087}
3088
3089bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003090 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003091 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3092}
3093
3094bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003095 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003096 return Visit(T->getElementType());
3097}
3098
3099bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003100 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003101 return Visit(T->getElementType());
3102}
3103
3104bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003105 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003106 return Visit(T->getElementType());
3107}
3108
3109bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003110 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003111 return Visit(T->getElementType());
3112}
3113
3114bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003115 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003116 return Visit(T->getElementType());
3117}
3118
3119bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3120 return Visit(T->getElementType());
3121}
3122
3123bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3124 return Visit(T->getElementType());
3125}
3126
3127bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3128 const FunctionProtoType* T) {
3129 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003130 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003131 A != AEnd; ++A) {
3132 if (Visit(*A))
3133 return true;
3134 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003136 return Visit(T->getResultType());
3137}
3138
3139bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3140 const FunctionNoProtoType* T) {
3141 return Visit(T->getResultType());
3142}
3143
3144bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3145 const UnresolvedUsingType*) {
3146 return false;
3147}
3148
3149bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3150 return false;
3151}
3152
3153bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3154 return Visit(T->getUnderlyingType());
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3158 return false;
3159}
3160
Sean Huntca63c202011-05-24 22:41:36 +00003161bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3162 const UnaryTransformType*) {
3163 return false;
3164}
3165
Richard Smith34b41d92011-02-20 03:19:35 +00003166bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3167 return Visit(T->getDeducedType());
3168}
3169
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003170bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3171 return VisitTagDecl(T->getDecl());
3172}
3173
3174bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3175 return VisitTagDecl(T->getDecl());
3176}
3177
3178bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3179 const TemplateTypeParmType*) {
3180 return false;
3181}
3182
Douglas Gregorc3069d62011-01-14 02:55:32 +00003183bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3184 const SubstTemplateTypeParmPackType *) {
3185 return false;
3186}
3187
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003188bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3189 const TemplateSpecializationType*) {
3190 return false;
3191}
3192
3193bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3194 const InjectedClassNameType* T) {
3195 return VisitTagDecl(T->getDecl());
3196}
3197
3198bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3199 const DependentNameType* T) {
3200 return VisitNestedNameSpecifier(T->getQualifier());
3201}
3202
3203bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3204 const DependentTemplateSpecializationType* T) {
3205 return VisitNestedNameSpecifier(T->getQualifier());
3206}
3207
Douglas Gregor7536dd52010-12-20 02:24:11 +00003208bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3209 const PackExpansionType* T) {
3210 return Visit(T->getPattern());
3211}
3212
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003213bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3214 return false;
3215}
3216
3217bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3218 const ObjCInterfaceType *) {
3219 return false;
3220}
3221
3222bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3223 const ObjCObjectPointerType *) {
3224 return false;
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3228 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3229 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3230 << S.Context.getTypeDeclType(Tag) << SR;
3231 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003232 }
3233
Richard Smith162e1c12011-04-15 14:24:37 +00003234 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003235 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3236 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3237 return true;
3238 }
3239
3240 return false;
3241}
3242
3243bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3244 NestedNameSpecifier *NNS) {
3245 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3246 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003247
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003248 switch (NNS->getKind()) {
3249 case NestedNameSpecifier::Identifier:
3250 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003251 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003252 case NestedNameSpecifier::Global:
3253 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003254
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003255 case NestedNameSpecifier::TypeSpec:
3256 case NestedNameSpecifier::TypeSpecWithTemplate:
3257 return Visit(QualType(NNS->getAsType(), 0));
3258 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003259 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003260}
3261
3262
Douglas Gregorc15cb382009-02-09 23:23:08 +00003263/// \brief Check a template argument against its corresponding
3264/// template type parameter.
3265///
3266/// This routine implements the semantics of C++ [temp.arg.type]. It
3267/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003268bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003269 TypeSourceInfo *ArgInfo) {
3270 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003271 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003272 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003273
3274 if (Arg->isVariablyModifiedType()) {
3275 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003276 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003277 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003278 }
3279
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003280 // C++03 [temp.arg.type]p2:
3281 // A local type, a type with no linkage, an unnamed type or a type
3282 // compounded from any of these types shall not be used as a
3283 // template-argument for a template type-parameter.
3284 //
3285 // C++0x allows these, and even in C++03 we allow them as an extension with
3286 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003287 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003288 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3289 (void)Finder.Visit(Context.getCanonicalType(Arg));
3290 }
3291
Douglas Gregorc15cb382009-02-09 23:23:08 +00003292 return false;
3293}
3294
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003295/// \brief Checks whether the given template argument is the address
3296/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003297static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003298CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3299 NonTypeTemplateParmDecl *Param,
3300 QualType ParamType,
3301 Expr *ArgIn,
3302 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003303 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003304 Expr *Arg = ArgIn;
3305 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003306
3307 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003308 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003309 Arg = Cast->getSubExpr();
3310
3311 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003312 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003313 // A template-argument for a non-type, non-template
3314 // template-parameter shall be one of: [...]
3315 //
3316 // -- the address of an object or function with external
3317 // linkage, including function templates and function
3318 // template-ids but excluding non-static class members,
3319 // expressed as & id-expression where the & is optional if
3320 // the name refers to a function or array, or if the
3321 // corresponding template-parameter is a reference; or
3322 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003323
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003324 // In C++98/03 mode, give an extension warning on any extra parentheses.
3325 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3326 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003327 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003328 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003329 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003330 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003331 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003332 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003333 }
3334
3335 Arg = Parens->getSubExpr();
3336 }
3337
Douglas Gregorb7a09262010-04-01 18:32:35 +00003338 bool AddressTaken = false;
3339 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003340 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003341 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003342 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3343 // Very common in Microsoft COM headers.
3344 if (S.getLangOptions().Microsoft &&
3345 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3346 Converted = TemplateArgument(ArgIn);
3347 return false;
3348 }
3349
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003351 AddressTaken = true;
3352 AddrOpLoc = UnOp->getOperatorLoc();
3353 }
Francois Picheta343a412011-04-29 09:08:14 +00003354 } else {
3355 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3356 Converted = TemplateArgument(ArgIn);
3357 return false;
3358 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003359 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003360 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003361 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003362 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3363 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003364 S.Diag(Param->getLocation(), diag::note_template_param_here);
3365 return true;
3366 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003367
3368 // Stop checking the precise nature of the argument if it is value dependent,
3369 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003370 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003371 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003372 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003373 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003374
Douglas Gregorb7a09262010-04-01 18:32:35 +00003375 if (!isa<ValueDecl>(DRE->getDecl())) {
3376 S.Diag(Arg->getSourceRange().getBegin(),
3377 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003378 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003379 S.Diag(Param->getLocation(), diag::note_template_param_here);
3380 return true;
3381 }
3382
3383 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003384
3385 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003386 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3387 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003389 S.Diag(Param->getLocation(), diag::note_template_param_here);
3390 return true;
3391 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003392
3393 // Cannot refer to non-static member functions
3394 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003395 if (!Method->isStatic()) {
3396 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003397 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003398 S.Diag(Param->getLocation(), diag::note_template_param_here);
3399 return true;
3400 }
Mike Stump1eb44332009-09-09 15:08:12 +00003401
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003402 // Functions must have external linkage.
3403 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003404 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003405 S.Diag(Arg->getSourceRange().getBegin(),
3406 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003407 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003408 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003409 << true;
3410 return true;
3411 }
3412
3413 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003414 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003415
Douglas Gregorb7a09262010-04-01 18:32:35 +00003416 // If the template parameter has pointer type, the function decays.
3417 if (ParamType->isPointerType() && !AddressTaken)
3418 ArgType = S.Context.getPointerType(Func->getType());
3419 else if (AddressTaken && ParamType->isReferenceType()) {
3420 // If we originally had an address-of operator, but the
3421 // parameter has reference type, complain and (if things look
3422 // like they will work) drop the address-of operator.
3423 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3424 ParamType.getNonReferenceType())) {
3425 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3426 << ParamType;
3427 S.Diag(Param->getLocation(), diag::note_template_param_here);
3428 return true;
3429 }
3430
3431 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3432 << ParamType
3433 << FixItHint::CreateRemoval(AddrOpLoc);
3434 S.Diag(Param->getLocation(), diag::note_template_param_here);
3435
3436 ArgType = Func->getType();
3437 }
3438 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003439 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003440 S.Diag(Arg->getSourceRange().getBegin(),
3441 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003442 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003443 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003444 << true;
3445 return true;
3446 }
3447
Douglas Gregorb7a09262010-04-01 18:32:35 +00003448 // A value of reference type is not an object.
3449 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003450 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003451 diag::err_template_arg_reference_var)
3452 << Var->getType() << Arg->getSourceRange();
3453 S.Diag(Param->getLocation(), diag::note_template_param_here);
3454 return true;
3455 }
3456
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003457 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003458 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003459
3460 // If the template parameter has pointer type, we must have taken
3461 // the address of this object.
3462 if (ParamType->isReferenceType()) {
3463 if (AddressTaken) {
3464 // If we originally had an address-of operator, but the
3465 // parameter has reference type, complain and (if things look
3466 // like they will work) drop the address-of operator.
3467 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3468 ParamType.getNonReferenceType())) {
3469 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3470 << ParamType;
3471 S.Diag(Param->getLocation(), diag::note_template_param_here);
3472 return true;
3473 }
3474
3475 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3476 << ParamType
3477 << FixItHint::CreateRemoval(AddrOpLoc);
3478 S.Diag(Param->getLocation(), diag::note_template_param_here);
3479
3480 ArgType = Var->getType();
3481 }
3482 } else if (!AddressTaken && ParamType->isPointerType()) {
3483 if (Var->getType()->isArrayType()) {
3484 // Array-to-pointer decay.
3485 ArgType = S.Context.getArrayDecayedType(Var->getType());
3486 } else {
3487 // If the template parameter has pointer type but the address of
3488 // this object was not taken, complain and (possibly) recover by
3489 // taking the address of the entity.
3490 ArgType = S.Context.getPointerType(Var->getType());
3491 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3492 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3493 << ParamType;
3494 S.Diag(Param->getLocation(), diag::note_template_param_here);
3495 return true;
3496 }
3497
3498 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3499 << ParamType
3500 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3501
3502 S.Diag(Param->getLocation(), diag::note_template_param_here);
3503 }
3504 }
3505 } else {
3506 // We found something else, but we don't know specifically what it is.
3507 S.Diag(Arg->getSourceRange().getBegin(),
3508 diag::err_template_arg_not_object_or_func)
3509 << Arg->getSourceRange();
3510 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3511 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003512 }
Mike Stump1eb44332009-09-09 15:08:12 +00003513
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003514 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003515 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003516 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003517 // For pointer-to-object types, qualification conversions are
3518 // permitted.
3519 } else {
3520 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3521 if (!ParamRef->getPointeeType()->isFunctionType()) {
3522 // C++ [temp.arg.nontype]p5b3:
3523 // For a non-type template-parameter of type reference to
3524 // object, no conversions apply. The type referred to by the
3525 // reference may be more cv-qualified than the (otherwise
3526 // identical) type of the template- argument. The
3527 // template-parameter is bound directly to the
3528 // template-argument, which shall be an lvalue.
3529
3530 // FIXME: Other qualifiers?
3531 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3532 unsigned ArgQuals = ArgType.getCVRQualifiers();
3533
3534 if ((ParamQuals | ArgQuals) != ParamQuals) {
3535 S.Diag(Arg->getSourceRange().getBegin(),
3536 diag::err_template_arg_ref_bind_ignores_quals)
3537 << ParamType << Arg->getType()
3538 << Arg->getSourceRange();
3539 S.Diag(Param->getLocation(), diag::note_template_param_here);
3540 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003541 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003542 }
3543 }
3544
3545 // At this point, the template argument refers to an object or
3546 // function with external linkage. We now need to check whether the
3547 // argument and parameter types are compatible.
3548 if (!S.Context.hasSameUnqualifiedType(ArgType,
3549 ParamType.getNonReferenceType())) {
3550 // We can't perform this conversion or binding.
3551 if (ParamType->isReferenceType())
3552 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3553 << ParamType << Arg->getType() << Arg->getSourceRange();
3554 else
3555 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3556 << Arg->getType() << ParamType << Arg->getSourceRange();
3557 S.Diag(Param->getLocation(), diag::note_template_param_here);
3558 return true;
3559 }
3560 }
3561
3562 // Create the template argument.
3563 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003564 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003566}
3567
3568/// \brief Checks whether the given template argument is a pointer to
3569/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003570bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003571 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003572 bool Invalid = false;
3573
3574 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003575 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003576 Arg = Cast->getSubExpr();
3577
3578 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003579 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003580 // A template-argument for a non-type, non-template
3581 // template-parameter shall be one of: [...]
3582 //
3583 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003584 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003585
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003586 // In C++98/03 mode, give an extension warning on any extra parentheses.
3587 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3588 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003589 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003590 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003591 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003592 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003593 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003594 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003595 }
3596
3597 Arg = Parens->getSubExpr();
3598 }
3599
Douglas Gregorcaddba02009-11-12 18:38:13 +00003600 // A pointer-to-member constant written &Class::member.
3601 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003602 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003603 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3604 if (DRE && !DRE->getQualifier())
3605 DRE = 0;
3606 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003607 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003608 // A constant of pointer-to-member type.
3609 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3610 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3611 if (VD->getType()->isMemberPointerType()) {
3612 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003613 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003614 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3615 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003616 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003617 else
3618 Converted = TemplateArgument(VD->getCanonicalDecl());
3619 return Invalid;
3620 }
3621 }
3622 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003623
Douglas Gregorcaddba02009-11-12 18:38:13 +00003624 DRE = 0;
3625 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003626
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003627 if (!DRE)
3628 return Diag(Arg->getSourceRange().getBegin(),
3629 diag::err_template_arg_not_pointer_to_member_form)
3630 << Arg->getSourceRange();
3631
3632 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3633 assert((isa<FieldDecl>(DRE->getDecl()) ||
3634 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3635 "Only non-static member pointers can make it here");
3636
3637 // Okay: this is the address of a non-static member, and therefore
3638 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003639 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003640 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003641 else
3642 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003643 return Invalid;
3644 }
3645
3646 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003647 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003648 diag::err_template_arg_not_pointer_to_member_form)
3649 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003650 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003651 diag::note_template_arg_refers_here);
3652 return true;
3653}
3654
Douglas Gregorc15cb382009-02-09 23:23:08 +00003655/// \brief Check a template argument against its corresponding
3656/// non-type template parameter.
3657///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003658/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003659/// If an error occurred, it returns ExprError(); otherwise, it
3660/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003661/// InstantiatedParamType is the type of the non-type template
3662/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003663ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3664 QualType InstantiatedParamType, Expr *Arg,
3665 TemplateArgument &Converted,
3666 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003667 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3668
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003669 // If either the parameter has a dependent type or the argument is
3670 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003671 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3672 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003673 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003674 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003675 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003676
3677 // C++ [temp.arg.nontype]p5:
3678 // The following conversions are performed on each expression used
3679 // as a non-type template-argument. If a non-type
3680 // template-argument cannot be converted to the type of the
3681 // corresponding template-parameter then the program is
3682 // ill-formed.
3683 //
3684 // -- for a non-type template-parameter of integral or
3685 // enumeration type, integral promotions (4.5) and integral
3686 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003687 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003688 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003689 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003690 // C++ [temp.arg.nontype]p1:
3691 // A template-argument for a non-type, non-template
3692 // template-parameter shall be one of:
3693 //
3694 // -- an integral constant-expression of integral or enumeration
3695 // type; or
3696 // -- the name of a non-type template-parameter; or
3697 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003698 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003699 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003700 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003701 diag::err_template_arg_not_integral_or_enumeral)
3702 << ArgType << Arg->getSourceRange();
3703 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003704 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003705 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003706 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003707 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3708 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003709 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003710 }
3711
Douglas Gregor02024a92010-03-28 02:42:43 +00003712 // From here on out, all we care about are the unqualified forms
3713 // of the parameter and argument types.
3714 ParamType = ParamType.getUnqualifiedType();
3715 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003716
3717 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003718 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003719 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003720 } else if (CTAK == CTAK_Deduced) {
3721 // C++ [temp.deduct.type]p17:
3722 // If, in the declaration of a function template with a non-type
3723 // template-parameter, the non-type template- parameter is used
3724 // in an expression in the function parameter-list and, if the
3725 // corresponding template-argument is deduced, the
3726 // template-argument type shall match the type of the
3727 // template-parameter exactly, except that a template-argument
3728 // deduced from an array bound may be of any integral type.
3729 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3730 << ArgType << ParamType;
3731 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003732 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003733 } else if (ParamType->isBooleanType()) {
3734 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003735 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003736 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3737 !ParamType->isEnumeralType()) {
3738 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003739 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003740 } else {
3741 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003742 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003743 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003744 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003745 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003746 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003747 }
3748
Douglas Gregorc7469372011-05-04 21:55:00 +00003749 // Add the value of this argument to the list of converted
3750 // arguments. We use the bitwidth and signedness of the template
3751 // parameter.
3752 if (Arg->isValueDependent()) {
3753 // The argument is value-dependent. Create a new
3754 // TemplateArgument with the converted expression.
3755 Converted = TemplateArgument(Arg);
3756 return Owned(Arg);
3757 }
3758
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003759 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003760 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003761 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003762
Douglas Gregorc7469372011-05-04 21:55:00 +00003763 if (ParamType->isBooleanType()) {
3764 // Value must be zero or one.
3765 Value = Value != 0;
3766 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3767 if (Value.getBitWidth() != AllowedBits)
3768 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003769 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003770 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003771 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003772
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003773 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003774 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003775 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003776 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003777 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003778 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003779
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003780 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003781 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003782 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003783 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3784 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3785 << Arg->getSourceRange();
3786 Diag(Param->getLocation(), diag::note_template_param_here);
3787 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003788
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003789 // Complain if we overflowed the template parameter's type.
3790 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003791 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003792 RequiredBits = OldValue.getActiveBits();
3793 else if (OldValue.isUnsigned())
3794 RequiredBits = OldValue.getActiveBits() + 1;
3795 else
3796 RequiredBits = OldValue.getMinSignedBits();
3797 if (RequiredBits > AllowedBits) {
3798 Diag(Arg->getSourceRange().getBegin(),
3799 diag::warn_template_arg_too_large)
3800 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3801 << Arg->getSourceRange();
3802 Diag(Param->getLocation(), diag::note_template_param_here);
3803 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003804 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003805
John McCall833ca992009-10-29 08:12:44 +00003806 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003807 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003808 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003809 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003810 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003811
John McCall6bb80172010-03-30 21:47:33 +00003812 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3813
Douglas Gregorb7a09262010-04-01 18:32:35 +00003814 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3815 // from a template argument of type std::nullptr_t to a non-type
3816 // template parameter of type pointer to object, pointer to
3817 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003818 if (ArgType->isNullPtrType()) {
3819 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3820 Converted = TemplateArgument((NamedDecl *)0);
3821 return Owned(Arg);
3822 }
3823
3824 if (ParamType->isNullPtrType()) {
3825 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3826 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3827 return Owned(Arg);
3828 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003829 }
3830
Douglas Gregorb86b0572009-02-11 01:18:59 +00003831 // Handle pointer-to-function, reference-to-function, and
3832 // pointer-to-member-function all in (roughly) the same way.
3833 if (// -- For a non-type template-parameter of type pointer to
3834 // function, only the function-to-pointer conversion (4.3) is
3835 // applied. If the template-argument represents a set of
3836 // overloaded functions (or a pointer to such), the matching
3837 // function is selected from the set (13.4).
3838 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003839 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003840 // -- For a non-type template-parameter of type reference to
3841 // function, no conversions apply. If the template-argument
3842 // represents a set of overloaded functions, the matching
3843 // function is selected from the set (13.4).
3844 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003845 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003846 // -- For a non-type template-parameter of type pointer to
3847 // member function, no conversions apply. If the
3848 // template-argument represents a set of overloaded member
3849 // functions, the matching member function is selected from
3850 // the set (13.4).
3851 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003852 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003853 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003854
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003855 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003856 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003857 true,
3858 FoundResult)) {
3859 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003860 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003861
3862 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3863 ArgType = Arg->getType();
3864 } else
John Wiegley429bb272011-04-08 18:41:53 +00003865 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003866 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003867
John Wiegley429bb272011-04-08 18:41:53 +00003868 if (!ParamType->isMemberPointerType()) {
3869 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3870 ParamType,
3871 Arg, Converted))
3872 return ExprError();
3873 return Owned(Arg);
3874 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003875
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003876 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3877 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003878 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003879 } else if (!Context.hasSameUnqualifiedType(ArgType,
3880 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003881 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003882 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003883 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003884 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003885 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003886 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003887 }
Mike Stump1eb44332009-09-09 15:08:12 +00003888
John Wiegley429bb272011-04-08 18:41:53 +00003889 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3890 return ExprError();
3891 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003892 }
3893
Chris Lattnerfe90de72009-02-20 21:37:53 +00003894 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003895 // -- for a non-type template-parameter of type pointer to
3896 // object, qualification conversions (4.4) and the
3897 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003898 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003899 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003900 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003901
John Wiegley429bb272011-04-08 18:41:53 +00003902 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3903 ParamType,
3904 Arg, Converted))
3905 return ExprError();
3906 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003907 }
Mike Stump1eb44332009-09-09 15:08:12 +00003908
Ted Kremenek6217b802009-07-29 21:53:49 +00003909 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003910 // -- For a non-type template-parameter of type reference to
3911 // object, no conversions apply. The type referred to by the
3912 // reference may be more cv-qualified than the (otherwise
3913 // identical) type of the template-argument. The
3914 // template-parameter is bound directly to the
3915 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003916 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003917 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003918
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003919 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003920 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3921 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003922 true,
3923 FoundResult)) {
3924 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003925 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003926
3927 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3928 ArgType = Arg->getType();
3929 } else
John Wiegley429bb272011-04-08 18:41:53 +00003930 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003931 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003932
John Wiegley429bb272011-04-08 18:41:53 +00003933 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3934 ParamType,
3935 Arg, Converted))
3936 return ExprError();
3937 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003938 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003939
3940 // -- For a non-type template-parameter of type pointer to data
3941 // member, qualification conversions (4.4) are applied.
3942 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3943
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003944 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003945 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003946 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003947 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003948 } else {
3949 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003950 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003951 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003952 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003953 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003954 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003955 }
3956
John Wiegley429bb272011-04-08 18:41:53 +00003957 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3958 return ExprError();
3959 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003960}
3961
3962/// \brief Check a template argument against its corresponding
3963/// template template parameter.
3964///
3965/// This routine implements the semantics of C++ [temp.arg.template].
3966/// It returns true if an error occurred, and false otherwise.
3967bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003968 const TemplateArgumentLoc &Arg) {
3969 TemplateName Name = Arg.getArgument().getAsTemplate();
3970 TemplateDecl *Template = Name.getAsTemplateDecl();
3971 if (!Template) {
3972 // Any dependent template name is fine.
3973 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3974 return false;
3975 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003976
Richard Smith3e4c6c42011-05-05 21:57:07 +00003977 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003978 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003979 // the name of a class template or an alias template, expressed as an
3980 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00003981 // primary class templates are considered when matching the
3982 // template template argument with the corresponding parameter;
3983 // partial specializations are not considered even if their
3984 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003985 //
3986 // Note that we also allow template template parameters here, which
3987 // will happen when we are dealing with, e.g., class template
3988 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003989 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00003990 !isa<TemplateTemplateParmDecl>(Template) &&
3991 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003992 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003993 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003994 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003995 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003996 << Template;
3997 }
3998
3999 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4000 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004001 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004002 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004003 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004004}
4005
Douglas Gregor02024a92010-03-28 02:42:43 +00004006/// \brief Given a non-type template argument that refers to a
4007/// declaration and the type of its corresponding non-type template
4008/// parameter, produce an expression that properly refers to that
4009/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004010ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004011Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4012 QualType ParamType,
4013 SourceLocation Loc) {
4014 assert(Arg.getKind() == TemplateArgument::Declaration &&
4015 "Only declaration template arguments permitted here");
4016 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4017
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004018 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004019 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4020 // If the value is a class member, we might have a pointer-to-member.
4021 // Determine whether the non-type template template parameter is of
4022 // pointer-to-member type. If so, we need to build an appropriate
4023 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4024 // would refer to the member itself.
4025 if (ParamType->isMemberPointerType()) {
4026 QualType ClassType
4027 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4028 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004029 = NestedNameSpecifier::Create(Context, 0, false,
4030 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004031 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004032 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004033
4034 // The actual value-ness of this is unimportant, but for
4035 // internal consistency's sake, references to instance methods
4036 // are r-values.
4037 ExprValueKind VK = VK_LValue;
4038 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4039 VK = VK_RValue;
4040
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004041 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004042 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004043 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004044 Loc,
4045 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004046 if (RefExpr.isInvalid())
4047 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004048
John McCall2de56d12010-08-25 11:45:40 +00004049 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004050
Douglas Gregorc0c83002010-04-30 21:46:38 +00004051 // We might need to perform a trailing qualification conversion, since
4052 // the element type on the parameter could be more qualified than the
4053 // element type in the expression we constructed.
4054 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00004055 ParamType.getUnqualifiedType(), false))
4056 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004057
Douglas Gregor02024a92010-03-28 02:42:43 +00004058 assert(!RefExpr.isInvalid() &&
4059 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004060 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004061 return move(RefExpr);
4062 }
4063 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004064
Douglas Gregor02024a92010-03-28 02:42:43 +00004065 QualType T = VD->getType().getNonReferenceType();
4066 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004067 // When the non-type template parameter is a pointer, take the
4068 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004069 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004070 if (RefExpr.isInvalid())
4071 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004072
4073 if (T->isFunctionType() || T->isArrayType()) {
4074 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004075 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4076 if (RefExpr.isInvalid())
4077 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004078
4079 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004080 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081
Douglas Gregorb7a09262010-04-01 18:32:35 +00004082 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004083 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004084 }
4085
John McCallf89e55a2010-11-18 06:31:45 +00004086 ExprValueKind VK = VK_RValue;
4087
Douglas Gregor02024a92010-03-28 02:42:43 +00004088 // If the non-type template parameter has reference type, qualify the
4089 // resulting declaration reference with the extra qualifiers on the
4090 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004091 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4092 VK = VK_LValue;
4093 T = Context.getQualifiedType(T,
4094 TargetRef->getPointeeType().getQualifiers());
4095 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004096
John McCallf89e55a2010-11-18 06:31:45 +00004097 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004098}
4099
4100/// \brief Construct a new expression that refers to the given
4101/// integral template argument with the given source-location
4102/// information.
4103///
4104/// This routine takes care of the mapping from an integral template
4105/// argument (which may have any integral type) to the appropriate
4106/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004107ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004108Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4109 SourceLocation Loc) {
4110 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004111 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004112 QualType T = Arg.getIntegralType();
4113 if (T->isCharType() || T->isWideCharType())
4114 return Owned(new (Context) CharacterLiteral(
4115 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004116 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004117 if (T->isBooleanType())
4118 return Owned(new (Context) CXXBoolLiteralExpr(
4119 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004120 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004121
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004122 if (T->isNullPtrType())
4123 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4124
Chris Lattner223de242011-04-25 20:37:58 +00004125 // If this is an enum type that we're instantiating, we need to use an integer
4126 // type the same size as the enumerator. We don't want to build an
4127 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004128 QualType BT;
4129 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004130 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004131 else
4132 BT = T;
4133
4134 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004135 if (T->isEnumeralType()) {
4136 // FIXME: This is a hack. We need a better way to handle substituted
4137 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00004138 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4139 Context.getTrivialTypeSourceInfo(T, Loc),
4140 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004141 }
4142
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004143 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004144}
4145
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004146/// \brief Match two template parameters within template parameter lists.
4147static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4148 bool Complain,
4149 Sema::TemplateParameterListEqualKind Kind,
4150 SourceLocation TemplateArgLoc) {
4151 // Check the actual kind (type, non-type, template).
4152 if (Old->getKind() != New->getKind()) {
4153 if (Complain) {
4154 unsigned NextDiag = diag::err_template_param_different_kind;
4155 if (TemplateArgLoc.isValid()) {
4156 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4157 NextDiag = diag::note_template_param_different_kind;
4158 }
4159 S.Diag(New->getLocation(), NextDiag)
4160 << (Kind != Sema::TPL_TemplateMatch);
4161 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4162 << (Kind != Sema::TPL_TemplateMatch);
4163 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004164
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004165 return false;
4166 }
4167
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004168 // Check that both are parameter packs are neither are parameter packs.
4169 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004170 // template template parameter, the template template parameter can have
4171 // a parameter pack where the template template argument does not.
4172 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4173 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4174 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004175 if (Complain) {
4176 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4177 if (TemplateArgLoc.isValid()) {
4178 S.Diag(TemplateArgLoc,
4179 diag::err_template_arg_template_params_mismatch);
4180 NextDiag = diag::note_template_parameter_pack_non_pack;
4181 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004182
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004183 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4184 : isa<NonTypeTemplateParmDecl>(New)? 1
4185 : 2;
4186 S.Diag(New->getLocation(), NextDiag)
4187 << ParamKind << New->isParameterPack();
4188 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4189 << ParamKind << Old->isParameterPack();
4190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004191
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004192 return false;
4193 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004194
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004195 // For non-type template parameters, check the type of the parameter.
4196 if (NonTypeTemplateParmDecl *OldNTTP
4197 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4198 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004199
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004200 // If we are matching a template template argument to a template
4201 // template parameter and one of the non-type template parameter types
4202 // is dependent, then we must wait until template instantiation time
4203 // to actually compare the arguments.
4204 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4205 (OldNTTP->getType()->isDependentType() ||
4206 NewNTTP->getType()->isDependentType()))
4207 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004208
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004209 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4210 if (Complain) {
4211 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4212 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004213 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004214 diag::err_template_arg_template_params_mismatch);
4215 NextDiag = diag::note_template_nontype_parm_different_type;
4216 }
4217 S.Diag(NewNTTP->getLocation(), NextDiag)
4218 << NewNTTP->getType()
4219 << (Kind != Sema::TPL_TemplateMatch);
4220 S.Diag(OldNTTP->getLocation(),
4221 diag::note_template_nontype_parm_prev_declaration)
4222 << OldNTTP->getType();
4223 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004224
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004225 return false;
4226 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004228 return true;
4229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004231 // For template template parameters, check the template parameter types.
4232 // The template parameter lists of template template
4233 // parameters must agree.
4234 if (TemplateTemplateParmDecl *OldTTP
4235 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004236 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004237 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4238 OldTTP->getTemplateParameters(),
4239 Complain,
4240 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004242 : Kind),
4243 TemplateArgLoc);
4244 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004246 return true;
4247}
Douglas Gregor02024a92010-03-28 02:42:43 +00004248
Douglas Gregora0347822011-01-13 00:08:50 +00004249/// \brief Diagnose a known arity mismatch when comparing template argument
4250/// lists.
4251static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004253 TemplateParameterList *New,
4254 TemplateParameterList *Old,
4255 Sema::TemplateParameterListEqualKind Kind,
4256 SourceLocation TemplateArgLoc) {
4257 unsigned NextDiag = diag::err_template_param_list_different_arity;
4258 if (TemplateArgLoc.isValid()) {
4259 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4260 NextDiag = diag::note_template_param_list_different_arity;
4261 }
4262 S.Diag(New->getTemplateLoc(), NextDiag)
4263 << (New->size() > Old->size())
4264 << (Kind != Sema::TPL_TemplateMatch)
4265 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4266 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4267 << (Kind != Sema::TPL_TemplateMatch)
4268 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4269}
4270
Douglas Gregorddc29e12009-02-06 22:42:48 +00004271/// \brief Determine whether the given template parameter lists are
4272/// equivalent.
4273///
Mike Stump1eb44332009-09-09 15:08:12 +00004274/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004275/// source code as part of a new template declaration.
4276///
4277/// \param Old The old template parameter list, typically found via
4278/// name lookup of the template declared with this template parameter
4279/// list.
4280///
4281/// \param Complain If true, this routine will produce a diagnostic if
4282/// the template parameter lists are not equivalent.
4283///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004284/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004285///
4286/// \param TemplateArgLoc If this source location is valid, then we
4287/// are actually checking the template parameter list of a template
4288/// argument (New) against the template parameter list of its
4289/// corresponding template template parameter (Old). We produce
4290/// slightly different diagnostics in this scenario.
4291///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004292/// \returns True if the template parameter lists are equal, false
4293/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004294bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004295Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4296 TemplateParameterList *Old,
4297 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004298 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004299 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004300 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4301 if (Complain)
4302 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4303 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004304
4305 return false;
4306 }
4307
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004308 // C++0x [temp.arg.template]p3:
4309 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004310 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004311 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004312 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004313 // template-parameter-list of P. [...]
4314 TemplateParameterList::iterator NewParm = New->begin();
4315 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004316 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004317 OldParmEnd = Old->end();
4318 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004319 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4320 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004321 if (NewParm == NewParmEnd) {
4322 if (Complain)
4323 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4324 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004325
Douglas Gregora0347822011-01-13 00:08:50 +00004326 return false;
4327 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004328
Douglas Gregora0347822011-01-13 00:08:50 +00004329 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4330 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004331 return false;
4332
Douglas Gregora0347822011-01-13 00:08:50 +00004333 ++NewParm;
4334 continue;
4335 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004336
Douglas Gregora0347822011-01-13 00:08:50 +00004337 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004338 // [...] When P's template- parameter-list contains a template parameter
4339 // pack (14.5.3), the template parameter pack will match zero or more
4340 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004341 // template-parameter-list of A with the same type and form as the
4342 // template parameter pack in P (ignoring whether those template
4343 // parameters are template parameter packs).
4344 for (; NewParm != NewParmEnd; ++NewParm) {
4345 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4346 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004347 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004348 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004349 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004350
Douglas Gregora0347822011-01-13 00:08:50 +00004351 // Make sure we exhausted all of the arguments.
4352 if (NewParm != NewParmEnd) {
4353 if (Complain)
4354 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4355 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004356
Douglas Gregora0347822011-01-13 00:08:50 +00004357 return false;
4358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004359
Douglas Gregorddc29e12009-02-06 22:42:48 +00004360 return true;
4361}
4362
4363/// \brief Check whether a template can be declared within this scope.
4364///
4365/// If the template declaration is valid in this scope, returns
4366/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004367bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004368Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004369 // Find the nearest enclosing declaration scope.
4370 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4371 (S->getFlags() & Scope::TemplateParamScope) != 0)
4372 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004373
Douglas Gregorddc29e12009-02-06 22:42:48 +00004374 // C++ [temp]p2:
4375 // A template-declaration can appear only as a namespace scope or
4376 // class scope declaration.
4377 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004378 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4379 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004380 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004381 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004382
Eli Friedman1503f772009-07-31 01:43:05 +00004383 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004384 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004385
4386 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4387 return false;
4388
Mike Stump1eb44332009-09-09 15:08:12 +00004389 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004390 diag::err_template_outside_namespace_or_class_scope)
4391 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004392}
Douglas Gregorcc636682009-02-17 23:15:12 +00004393
Douglas Gregord5cb8762009-10-07 00:13:32 +00004394/// \brief Determine what kind of template specialization the given declaration
4395/// is.
4396static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4397 if (!D)
4398 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004399
Douglas Gregorf6b11852009-10-08 15:14:33 +00004400 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4401 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004402 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4403 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004404 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4405 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406
Douglas Gregord5cb8762009-10-07 00:13:32 +00004407 return TSK_Undeclared;
4408}
4409
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004411/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004412///
Douglas Gregor9302da62009-10-14 23:50:59 +00004413/// This routine determines whether a template specialization can be declared
4414/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004415///
4416/// \param S the semantic analysis object for which this check is being
4417/// performed.
4418///
4419/// \param Specialized the entity being specialized or instantiated, which
4420/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004421/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004422/// member class).
4423///
4424/// \param PrevDecl the previous declaration of this entity, if any.
4425///
4426/// \param Loc the location of the explicit specialization or instantiation of
4427/// this entity.
4428///
4429/// \param IsPartialSpecialization whether this is a partial specialization of
4430/// a class template.
4431///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004432/// \returns true if there was an error that we cannot recover from, false
4433/// otherwise.
4434static bool CheckTemplateSpecializationScope(Sema &S,
4435 NamedDecl *Specialized,
4436 NamedDecl *PrevDecl,
4437 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004438 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004439 // Keep these "kind" numbers in sync with the %select statements in the
4440 // various diagnostics emitted by this routine.
4441 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004442 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004443 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004444 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004445 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004446 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004447 EntityKind = 3;
4448 else if (isa<VarDecl>(Specialized))
4449 EntityKind = 4;
4450 else if (isa<RecordDecl>(Specialized))
4451 EntityKind = 5;
4452 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004453 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4454 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004455 return true;
4456 }
4457
Douglas Gregor88b70942009-02-25 22:02:03 +00004458 // C++ [temp.expl.spec]p2:
4459 // An explicit specialization shall be declared in the namespace
4460 // of which the template is a member, or, for member templates, in
4461 // the namespace of which the enclosing class or enclosing class
4462 // template is a member. An explicit specialization of a member
4463 // function, member class or static data member of a class
4464 // template shall be declared in the namespace of which the class
4465 // template is a member. Such a declaration may also be a
4466 // definition. If the declaration is not a definition, the
4467 // specialization may be defined later in the name- space in which
4468 // the explicit specialization was declared, or in a namespace
4469 // that encloses the one in which the explicit specialization was
4470 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004471 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004472 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004473 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004474 return true;
4475 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004476
Douglas Gregor0a407472009-10-07 17:30:37 +00004477 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichete1e96a62011-05-14 19:17:07 +00004478 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4479 << Specialized;
4480 return true;
Douglas Gregor0a407472009-10-07 17:30:37 +00004481 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004482
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004483 // C++ [temp.class.spec]p6:
4484 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004485 // in any namespace scope in which its definition may be defined (14.5.1
4486 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004487 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004488 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004489 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004490 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004492 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4493 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004494 // C++ [temp.exp.spec]p2:
4495 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004496 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004497 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498 // An explicit specialization of a member function, member class or
4499 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004500 // namespace of which the class template is a member.
4501 //
4502 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004503 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004504 // the specialized template.
4505 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4506 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004507 bool IsCPlusPlus0xExtension
4508 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004509 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004510 S.Diag(Loc, IsCPlusPlus0xExtension
4511 ? diag::ext_template_spec_decl_out_of_scope_global
4512 : diag::err_template_spec_decl_out_of_scope_global)
4513 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004514 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004515 S.Diag(Loc, IsCPlusPlus0xExtension
4516 ? diag::ext_template_spec_decl_out_of_scope
4517 : diag::err_template_spec_decl_out_of_scope)
4518 << EntityKind << Specialized
4519 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004520
Douglas Gregor9302da62009-10-14 23:50:59 +00004521 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4522 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004523 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004525
4526 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004527 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004528 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004529 // specializations of function templates, static data members, and member
4530 // functions, so we skip the check here for those kinds of entities.
4531 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004532 // Should we refactor that check, so that it occurs later?
4533 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004534 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4535 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004536 if (isa<TranslationUnitDecl>(SpecializedContext))
4537 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4538 << EntityKind << Specialized;
4539 else if (isa<NamespaceDecl>(SpecializedContext))
4540 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4541 << EntityKind << Specialized
4542 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004543
Douglas Gregor9302da62009-10-14 23:50:59 +00004544 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004545 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004546
Douglas Gregord5cb8762009-10-07 00:13:32 +00004547 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004548
Douglas Gregor88b70942009-02-25 22:02:03 +00004549 return false;
4550}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004551
Douglas Gregorbacb9492011-01-03 21:13:47 +00004552/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4553/// that checks non-type template partial specialization arguments.
4554static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4555 NonTypeTemplateParmDecl *Param,
4556 const TemplateArgument *Args,
4557 unsigned NumArgs) {
4558 for (unsigned I = 0; I != NumArgs; ++I) {
4559 if (Args[I].getKind() == TemplateArgument::Pack) {
4560 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004561 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004562 Args[I].pack_size()))
4563 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004564
Douglas Gregore94866f2009-06-12 21:21:02 +00004565 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004567
Douglas Gregorbacb9492011-01-03 21:13:47 +00004568 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004569 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004570 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004571 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004572
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004573 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004574 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4575 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004576
4577 // Strip off any implicit casts we added as part of type checking.
4578 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4579 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004580
Douglas Gregore94866f2009-06-12 21:21:02 +00004581 // C++ [temp.class.spec]p8:
4582 // A non-type argument is non-specialized if it is the name of a
4583 // non-type parameter. All other non-type arguments are
4584 // specialized.
4585 //
4586 // Below, we check the two conditions that only apply to
4587 // specialized non-type arguments, so skip any non-specialized
4588 // arguments.
4589 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004590 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004591 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004592
Douglas Gregore94866f2009-06-12 21:21:02 +00004593 // C++ [temp.class.spec]p9:
4594 // Within the argument list of a class template partial
4595 // specialization, the following restrictions apply:
4596 // -- A partially specialized non-type argument expression
4597 // shall not involve a template parameter of the partial
4598 // specialization except when the argument expression is a
4599 // simple identifier.
4600 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004601 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004602 diag::err_dependent_non_type_arg_in_partial_spec)
4603 << ArgExpr->getSourceRange();
4604 return true;
4605 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606
Douglas Gregore94866f2009-06-12 21:21:02 +00004607 // -- The type of a template parameter corresponding to a
4608 // specialized non-type argument shall not be dependent on a
4609 // parameter of the specialization.
4610 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004611 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004612 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4613 << Param->getType()
4614 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004615 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004616 return true;
4617 }
4618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004619
Douglas Gregorbacb9492011-01-03 21:13:47 +00004620 return false;
4621}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004622
Douglas Gregorbacb9492011-01-03 21:13:47 +00004623/// \brief Check the non-type template arguments of a class template
4624/// partial specialization according to C++ [temp.class.spec]p9.
4625///
4626/// \param TemplateParams the template parameters of the primary class
4627/// template.
4628///
4629/// \param TemplateArg the template arguments of the class template
4630/// partial specialization.
4631///
4632/// \returns true if there was an error, false otherwise.
4633static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4634 TemplateParameterList *TemplateParams,
4635 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4636 const TemplateArgument *ArgList = TemplateArgs.data();
4637
4638 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4639 NonTypeTemplateParmDecl *Param
4640 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4641 if (!Param)
4642 continue;
4643
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004644 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004645 &ArgList[I], 1))
4646 return true;
4647 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004648
4649 return false;
4650}
4651
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004652/// \brief Retrieve the previous declaration of the given declaration.
4653static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4654 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4655 return VD->getPreviousDeclaration();
4656 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4657 return FD->getPreviousDeclaration();
4658 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4659 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004660 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004661 return TD->getPreviousDeclaration();
4662 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4663 return FTD->getPreviousDeclaration();
4664 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4665 return CTD->getPreviousDeclaration();
4666 return 0;
4667}
4668
John McCalld226f652010-08-21 09:40:31 +00004669DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004670Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4671 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004672 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004673 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004674 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004675 SourceLocation TemplateNameLoc,
4676 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004677 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004678 SourceLocation RAngleLoc,
4679 AttributeList *Attr,
4680 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004681 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004682
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004683 // NOTE: KWLoc is the location of the tag keyword. This will instead
4684 // store the location of the outermost template keyword in the declaration.
4685 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4686 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4687
Douglas Gregorcc636682009-02-17 23:15:12 +00004688 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004689 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004690 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004691 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4692
4693 if (!ClassTemplate) {
4694 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004695 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004696 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4697 return true;
4698 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004699
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004700 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004701 bool isPartialSpecialization = false;
4702
Douglas Gregor88b70942009-02-25 22:02:03 +00004703 // Check the validity of the template headers that introduce this
4704 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004705 // FIXME: We probably shouldn't complain about these headers for
4706 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004707 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004708 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004709 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4710 TemplateNameLoc,
4711 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004712 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004713 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004714 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004715 isExplicitSpecialization,
4716 Invalid);
4717 if (Invalid)
4718 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004719
Douglas Gregor05396e22009-08-25 17:23:04 +00004720 if (TemplateParams && TemplateParams->size() > 0) {
4721 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004722
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004723 if (TUK == TUK_Friend) {
4724 Diag(KWLoc, diag::err_partial_specialization_friend)
4725 << SourceRange(LAngleLoc, RAngleLoc);
4726 return true;
4727 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728
Douglas Gregor05396e22009-08-25 17:23:04 +00004729 // C++ [temp.class.spec]p10:
4730 // The template parameter list of a specialization shall not
4731 // contain default template argument values.
4732 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4733 Decl *Param = TemplateParams->getParam(I);
4734 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4735 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004736 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004737 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004738 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004739 }
4740 } else if (NonTypeTemplateParmDecl *NTTP
4741 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4742 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004743 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004744 diag::err_default_arg_in_partial_spec)
4745 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004746 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004747 }
4748 } else {
4749 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004750 if (TTP->hasDefaultArgument()) {
4751 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004752 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004753 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004754 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004755 }
4756 }
4757 }
Douglas Gregora735b202009-10-13 14:39:41 +00004758 } else if (TemplateParams) {
4759 if (TUK == TUK_Friend)
4760 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004761 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004762 SourceRange(TemplateParams->getTemplateLoc(),
4763 TemplateParams->getRAngleLoc()))
4764 << SourceRange(LAngleLoc, RAngleLoc);
4765 else
4766 isExplicitSpecialization = true;
4767 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004768 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004769 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004770 isExplicitSpecialization = true;
4771 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004772
Douglas Gregorcc636682009-02-17 23:15:12 +00004773 // Check that the specialization uses the same tag kind as the
4774 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004775 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4776 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004777 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004778 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004779 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004780 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004781 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004782 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004783 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004784 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004785 diag::note_previous_use);
4786 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4787 }
4788
Douglas Gregor40808ce2009-03-09 23:48:35 +00004789 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004790 TemplateArgumentListInfo TemplateArgs;
4791 TemplateArgs.setLAngleLoc(LAngleLoc);
4792 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004793 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004794
Douglas Gregor925910d2011-01-03 20:35:03 +00004795 // Check for unexpanded parameter packs in any of the template arguments.
4796 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004798 UPPC_PartialSpecialization))
4799 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800
Douglas Gregorcc636682009-02-17 23:15:12 +00004801 // Check that the template argument list is well-formed for this
4802 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004803 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004804 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4805 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004806 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004807
Douglas Gregor910f8002010-11-07 23:05:16 +00004808 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004809 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004810
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004811 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004812 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004813 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004814 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004815 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004816 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004817 return true;
4818
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004820 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004821 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004822 TemplateArgs.size())) {
4823 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4824 << ClassTemplate->getDeclName();
4825 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004826 }
4827 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004828
Douglas Gregorcc636682009-02-17 23:15:12 +00004829 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004830 ClassTemplateSpecializationDecl *PrevDecl = 0;
4831
4832 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004833 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004834 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004835 = ClassTemplate->findPartialSpecialization(Converted.data(),
4836 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004837 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004838 else
4839 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004840 = ClassTemplate->findSpecialization(Converted.data(),
4841 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004842
4843 ClassTemplateSpecializationDecl *Specialization = 0;
4844
Douglas Gregor88b70942009-02-25 22:02:03 +00004845 // Check whether we can declare a class template specialization in
4846 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004847 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004848 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4849 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004850 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004851 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004852
Douglas Gregorb88e8882009-07-30 17:40:51 +00004853 // The canonical type
4854 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004855 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004856 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004857 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004858 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004859 // arguments was referenced but not declared, or we're only
4860 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004861 // declaration node as our own, updating its source location and
4862 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004863 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004864 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004865 if (TemplateParameterLists.size() > 0) {
4866 Specialization->setTemplateParameterListsInfo(Context,
4867 TemplateParameterLists.size(),
4868 (TemplateParameterList**) TemplateParameterLists.release());
4869 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004870 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004871 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004872 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004873 // Build the canonical type that describes the converted template
4874 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004875 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4876 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004877 Converted.data(),
4878 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004879
4880 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004881 ClassTemplate->getInjectedClassNameSpecialization())) {
4882 // C++ [temp.class.spec]p9b3:
4883 //
4884 // -- The argument list of the specialization shall not be identical
4885 // to the implicit argument list of the primary template.
4886 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4887 << (TUK == TUK_Definition)
4888 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4889 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4890 ClassTemplate->getIdentifier(),
4891 TemplateNameLoc,
4892 Attr,
4893 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004894 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004895 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004896 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004897 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004898
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004899 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004900 ClassTemplatePartialSpecializationDecl *PrevPartial
4901 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004902 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004903 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004904 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004905 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004906 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004907 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004908 TemplateParams,
4909 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004910 Converted.data(),
4911 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004912 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004913 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004914 PrevPartial,
4915 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004916 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004917 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004918 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004919 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004920 (TemplateParameterList**) TemplateParameterLists.release());
4921 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004922
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004923 if (!PrevPartial)
4924 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004925 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004926
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004927 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004928 // template specialization, make a note of that.
4929 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4930 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004931
Douglas Gregor031a5882009-06-13 00:26:55 +00004932 // Check that all of the template parameters of the class template
4933 // partial specialization are deducible from the template
4934 // arguments. If not, this class template partial specialization
4935 // will never be used.
4936 llvm::SmallVector<bool, 8> DeducibleParams;
4937 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004939 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004940 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004941 unsigned NumNonDeducible = 0;
4942 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4943 if (!DeducibleParams[I])
4944 ++NumNonDeducible;
4945
4946 if (NumNonDeducible) {
4947 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4948 << (NumNonDeducible > 1)
4949 << SourceRange(TemplateNameLoc, RAngleLoc);
4950 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4951 if (!DeducibleParams[I]) {
4952 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4953 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004954 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004955 diag::note_partial_spec_unused_parameter)
4956 << Param->getDeclName();
4957 else
Mike Stump1eb44332009-09-09 15:08:12 +00004958 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004959 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004960 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004961 }
4962 }
4963 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004964 } else {
4965 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004966 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004967 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004968 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004969 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004970 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004971 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004972 Converted.data(),
4973 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004974 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004975 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004976 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004977 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004978 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004979 (TemplateParameterList**) TemplateParameterLists.release());
4980 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004981
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004982 if (!PrevDecl)
4983 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004984
4985 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004986 }
4987
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004988 // C++ [temp.expl.spec]p6:
4989 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004991 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004992 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004993 // use occurs; no diagnostic is required.
4994 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004995 bool Okay = false;
4996 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4997 // Is there any previous explicit specialization declaration?
4998 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4999 Okay = true;
5000 break;
5001 }
5002 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005003
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005004 if (!Okay) {
5005 SourceRange Range(TemplateNameLoc, RAngleLoc);
5006 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5007 << Context.getTypeDeclType(Specialization) << Range;
5008
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005009 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005010 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005011 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005012 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005013 return true;
5014 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005015 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005016
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005017 // If this is not a friend, note that this is an explicit specialization.
5018 if (TUK != TUK_Friend)
5019 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005020
5021 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005022 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005023 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005024 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005025 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005026 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005027 Diag(Def->getLocation(), diag::note_previous_definition);
5028 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005029 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005030 }
5031 }
5032
John McCall7f1b9872010-12-18 03:30:47 +00005033 if (Attr)
5034 ProcessDeclAttributeList(S, Specialization, Attr);
5035
Douglas Gregorfc705b82009-02-26 22:19:44 +00005036 // Build the fully-sugared type for this class template
5037 // specialization as the user wrote in the specialization
5038 // itself. This means that we'll pretty-print the type retrieved
5039 // from the specialization's declaration the way that the user
5040 // actually wrote the specialization, rather than formatting the
5041 // name based on the "canonical" representation used to store the
5042 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005043 TypeSourceInfo *WrittenTy
5044 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5045 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005046 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005047 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005048 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005049 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005050 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005051
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005052 // C++ [temp.expl.spec]p9:
5053 // A template explicit specialization is in the scope of the
5054 // namespace in which the template was defined.
5055 //
5056 // We actually implement this paragraph where we set the semantic
5057 // context (in the creation of the ClassTemplateSpecializationDecl),
5058 // but we also maintain the lexical context where the actual
5059 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005060 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005061
Douglas Gregorcc636682009-02-17 23:15:12 +00005062 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005063 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005064 Specialization->startDefinition();
5065
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005066 if (TUK == TUK_Friend) {
5067 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5068 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005069 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005070 /*FIXME:*/KWLoc);
5071 Friend->setAccess(AS_public);
5072 CurContext->addDecl(Friend);
5073 } else {
5074 // Add the specialization into its lexical context, so that it can
5075 // be seen when iterating through the list of declarations in that
5076 // context. However, specializations are not found by name lookup.
5077 CurContext->addDecl(Specialization);
5078 }
John McCalld226f652010-08-21 09:40:31 +00005079 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005080}
Douglas Gregord57959a2009-03-27 23:10:48 +00005081
John McCalld226f652010-08-21 09:40:31 +00005082Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005083 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005084 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005085 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5086}
5087
John McCalld226f652010-08-21 09:40:31 +00005088Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005089 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005090 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005091 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005092 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005093
Douglas Gregor52591bf2009-06-24 00:54:41 +00005094 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005095 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005096 }
Mike Stump1eb44332009-09-09 15:08:12 +00005097
Douglas Gregor52591bf2009-06-24 00:54:41 +00005098 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005099
John McCalld226f652010-08-21 09:40:31 +00005100 Decl *DP = HandleDeclarator(ParentScope, D,
5101 move(TemplateParameterLists),
5102 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005103 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005104 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005105 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005106 FunctionTemplate->getTemplatedDecl());
5107 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5108 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5109 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005110}
5111
John McCall75042392010-02-11 01:33:53 +00005112/// \brief Strips various properties off an implicit instantiation
5113/// that has just been explicitly specialized.
5114static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005115 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005116
5117 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5118 FD->setInlineSpecified(false);
5119 }
5120}
5121
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005122/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005123/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005124/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005125/// new specialization/instantiation will have any effect.
5126///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005127/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005128/// instantiation.
5129///
5130/// \param NewTSK the kind of the new explicit specialization or instantiation.
5131///
5132/// \param PrevDecl the previous declaration of the entity.
5133///
5134/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5135///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005136/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005137/// declaration was instantiated (either implicitly or explicitly).
5138///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005139/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005140/// specialization or instantiation has no effect and should be ignored.
5141///
5142/// \returns true if there was an error that should prevent the introduction of
5143/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005144bool
5145Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5146 TemplateSpecializationKind NewTSK,
5147 NamedDecl *PrevDecl,
5148 TemplateSpecializationKind PrevTSK,
5149 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005150 bool &HasNoEffect) {
5151 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005152
Douglas Gregor454885e2009-10-15 15:54:05 +00005153 switch (NewTSK) {
5154 case TSK_Undeclared:
5155 case TSK_ImplicitInstantiation:
5156 assert(false && "Don't check implicit instantiations here");
5157 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158
Douglas Gregor454885e2009-10-15 15:54:05 +00005159 case TSK_ExplicitSpecialization:
5160 switch (PrevTSK) {
5161 case TSK_Undeclared:
5162 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005164 // explicitly specialized or has merely been mentioned without any
5165 // instantiation.
5166 return false;
5167
5168 case TSK_ImplicitInstantiation:
5169 if (PrevPointOfInstantiation.isInvalid()) {
5170 // The declaration itself has not actually been instantiated, so it is
5171 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005172 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005173 return false;
5174 }
5175 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005176
Douglas Gregor454885e2009-10-15 15:54:05 +00005177 case TSK_ExplicitInstantiationDeclaration:
5178 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179 assert((PrevTSK == TSK_ImplicitInstantiation ||
5180 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005181 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005182
Douglas Gregor454885e2009-10-15 15:54:05 +00005183 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005184 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005185 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005186 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005187 // implicit instantiation to take place, in every translation unit in
5188 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005189 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5190 // Is there any previous explicit specialization declaration?
5191 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5192 return false;
5193 }
5194
Douglas Gregor0d035142009-10-27 18:42:08 +00005195 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005196 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005197 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005198 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005199
Douglas Gregor454885e2009-10-15 15:54:05 +00005200 return true;
5201 }
5202 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005203
Douglas Gregor454885e2009-10-15 15:54:05 +00005204 case TSK_ExplicitInstantiationDeclaration:
5205 switch (PrevTSK) {
5206 case TSK_ExplicitInstantiationDeclaration:
5207 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005208 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005209 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210
Douglas Gregor454885e2009-10-15 15:54:05 +00005211 case TSK_Undeclared:
5212 case TSK_ImplicitInstantiation:
5213 // We're explicitly instantiating something that may have already been
5214 // implicitly instantiated; that's fine.
5215 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005216
Douglas Gregor454885e2009-10-15 15:54:05 +00005217 case TSK_ExplicitSpecialization:
5218 // C++0x [temp.explicit]p4:
5219 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005221 // specialization for that template, the explicit instantiation has no
5222 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005223 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005224 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225
Douglas Gregor454885e2009-10-15 15:54:05 +00005226 case TSK_ExplicitInstantiationDefinition:
5227 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005228 // If an entity is the subject of both an explicit instantiation
5229 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005230 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005232 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005233 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005234 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005235 assert(PrevPointOfInstantiation.isValid() &&
5236 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005237 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005238 return false;
5239 }
5240 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005241
Douglas Gregor454885e2009-10-15 15:54:05 +00005242 case TSK_ExplicitInstantiationDefinition:
5243 switch (PrevTSK) {
5244 case TSK_Undeclared:
5245 case TSK_ImplicitInstantiation:
5246 // We're explicitly instantiating something that may have already been
5247 // implicitly instantiated; that's fine.
5248 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249
Douglas Gregor454885e2009-10-15 15:54:05 +00005250 case TSK_ExplicitSpecialization:
5251 // C++ DR 259, C++0x [temp.explicit]p4:
5252 // For a given set of template parameters, if an explicit
5253 // instantiation of a template appears after a declaration of
5254 // an explicit specialization for that template, the explicit
5255 // instantiation has no effect.
5256 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005257 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005258 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005259 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005260 if (!getLangOptions().CPlusPlus0x) {
5261 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005262 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005263 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005264 diag::note_previous_template_specialization);
5265 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005266 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005267 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268
Douglas Gregor454885e2009-10-15 15:54:05 +00005269 case TSK_ExplicitInstantiationDeclaration:
5270 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005271 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005272 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005273
Douglas Gregor454885e2009-10-15 15:54:05 +00005274 case TSK_ExplicitInstantiationDefinition:
5275 // C++0x [temp.spec]p5:
5276 // For a given template and a given set of template-arguments,
5277 // - an explicit instantiation definition shall appear at most once
5278 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005279 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005282 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005283 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 }
5286 break;
5287 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005288
Douglas Gregor454885e2009-10-15 15:54:05 +00005289 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005290
Douglas Gregor454885e2009-10-15 15:54:05 +00005291 return false;
5292}
5293
John McCallaf2094e2010-04-08 09:05:18 +00005294/// \brief Perform semantic analysis for the given dependent function
5295/// template specialization. The only possible way to get a dependent
5296/// function template specialization is with a friend declaration,
5297/// like so:
5298///
5299/// template <class T> void foo(T);
5300/// template <class T> class A {
5301/// friend void foo<>(T);
5302/// };
5303///
5304/// There really isn't any useful analysis we can do here, so we
5305/// just store the information.
5306bool
5307Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5308 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5309 LookupResult &Previous) {
5310 // Remove anything from Previous that isn't a function template in
5311 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005312 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005313 LookupResult::Filter F = Previous.makeFilter();
5314 while (F.hasNext()) {
5315 NamedDecl *D = F.next()->getUnderlyingDecl();
5316 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005317 !FDLookupContext->InEnclosingNamespaceSetOf(
5318 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005319 F.erase();
5320 }
5321 F.done();
5322
5323 // Should this be diagnosed here?
5324 if (Previous.empty()) return true;
5325
5326 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5327 ExplicitTemplateArgs);
5328 return false;
5329}
5330
Abramo Bagnarae03db982010-05-20 15:32:11 +00005331/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005332/// specialization.
5333///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005334/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005335/// explicit function template specialization. On successful completion,
5336/// the function declaration \p FD will become a function template
5337/// specialization.
5338///
5339/// \param FD the function declaration, which will be updated to become a
5340/// function template specialization.
5341///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005342/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5343/// if any. Note that this may be valid info even when 0 arguments are
5344/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5345/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005346///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005347/// \param PrevDecl the set of declarations that may be specialized by
5348/// this function specialization.
5349bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005350Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005351 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005352 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005353 // The set of function template specializations that could match this
5354 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005355 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356
Sebastian Redl7a126a42010-08-31 00:36:30 +00005357 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005358 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5359 I != E; ++I) {
5360 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5361 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005362 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005363 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005364 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5365 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005366 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005367
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005368 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005369 // A trailing template-argument can be left unspecified in the
5370 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005371 // provided it can be deduced from the function argument type.
5372 // Perform template argument deduction to determine whether we may be
5373 // specializing this template.
5374 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005375 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005376 FunctionDecl *Specialization = 0;
5377 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005378 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005379 FD->getType(),
5380 Specialization,
5381 Info)) {
5382 // FIXME: Template argument deduction failed; record why it failed, so
5383 // that we can provide nifty diagnostics.
5384 (void)TDK;
5385 continue;
5386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005387
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005388 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005389 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005390 }
5391 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005392
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005393 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005394 UnresolvedSetIterator Result
5395 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005396 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005397 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005398 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005399 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005400 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005401 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005402 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005403 return true;
John McCallc373d482010-01-27 01:50:18 +00005404
5405 // Ignore access information; it doesn't figure into redeclaration checking.
5406 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005407
5408 FunctionTemplateSpecializationInfo *SpecInfo
5409 = Specialization->getTemplateSpecializationInfo();
5410 assert(SpecInfo && "Function template specialization info missing?");
5411 {
5412 // Note: do not overwrite location info if previous template
5413 // specialization kind was explicit.
5414 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5415 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5416 Specialization->setLocation(FD->getLocation());
5417 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005418
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005419 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005420 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005421
5422 // If this is a friend declaration, then we're not really declaring
5423 // an explicit specialization.
5424 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005425
Douglas Gregord5cb8762009-10-07 00:13:32 +00005426 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005427 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005428 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005429 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005430 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005431 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005432 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005433
5434 // C++ [temp.expl.spec]p6:
5435 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005436 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005437 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005438 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005439 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005440 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005441 if (!isFriend &&
5442 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005443 TSK_ExplicitSpecialization,
5444 Specialization,
5445 SpecInfo->getTemplateSpecializationKind(),
5446 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005447 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005448 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005449
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005450 // Mark the prior declaration as an explicit specialization, so that later
5451 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005452 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005453 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005454 MarkUnusedFileScopedDecl(Specialization);
5455 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005456
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005457 // Turn the given function declaration into a function template
5458 // specialization, with the template arguments from the previous
5459 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005460 // Take copies of (semantic and syntactic) template argument lists.
5461 const TemplateArgumentList* TemplArgs = new (Context)
5462 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5463 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5464 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005465 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005466 TemplArgs, /*InsertPos=*/0,
5467 SpecInfo->getTemplateSpecializationKind(),
5468 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005469 FD->setStorageClass(Specialization->getStorageClass());
5470
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005471 // The "previous declaration" for this function template specialization is
5472 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005473 Previous.clear();
5474 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005475 return false;
5476}
5477
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005478/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005479/// specialization.
5480///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005482/// explicit member function specialization. On successful completion,
5483/// the function declaration \p FD will become a member function
5484/// specialization.
5485///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005486/// \param Member the member declaration, which will be updated to become a
5487/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005488///
John McCall68263142009-11-18 22:49:29 +00005489/// \param Previous the set of declarations, one of which may be specialized
5490/// by this function specialization; the set will be modified to contain the
5491/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492bool
John McCall68263142009-11-18 22:49:29 +00005493Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005494 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005495
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005496 // Try to find the member we are instantiating.
5497 NamedDecl *Instantiation = 0;
5498 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005499 MemberSpecializationInfo *MSInfo = 0;
5500
John McCall68263142009-11-18 22:49:29 +00005501 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005502 // Nowhere to look anyway.
5503 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005504 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5505 I != E; ++I) {
5506 NamedDecl *D = (*I)->getUnderlyingDecl();
5507 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005508 if (Context.hasSameType(Function->getType(), Method->getType())) {
5509 Instantiation = Method;
5510 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005511 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005512 break;
5513 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005514 }
5515 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005516 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005517 VarDecl *PrevVar;
5518 if (Previous.isSingleResult() &&
5519 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005520 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005521 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005522 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005523 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005524 }
5525 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005526 CXXRecordDecl *PrevRecord;
5527 if (Previous.isSingleResult() &&
5528 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5529 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005530 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005531 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005532 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005533 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005534
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005535 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005536 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005537 // specializations are always out-of-line, the caller will complain about
5538 // this mismatch later.
5539 return false;
5540 }
John McCall77e8b112010-04-13 20:37:33 +00005541
5542 // If this is a friend, just bail out here before we start turning
5543 // things into explicit specializations.
5544 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5545 // Preserve instantiation information.
5546 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5547 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5548 cast<CXXMethodDecl>(InstantiatedFrom),
5549 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5550 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5551 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5552 cast<CXXRecordDecl>(InstantiatedFrom),
5553 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5554 }
5555
5556 Previous.clear();
5557 Previous.addDecl(Instantiation);
5558 return false;
5559 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005560
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005561 // Make sure that this is a specialization of a member.
5562 if (!InstantiatedFrom) {
5563 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5564 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005565 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5566 return true;
5567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005568
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005569 // C++ [temp.expl.spec]p6:
5570 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005571 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005572 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005573 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005574 // use occurs; no diagnostic is required.
5575 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005576
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005577 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005578 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5579 TSK_ExplicitSpecialization,
5580 Instantiation,
5581 MSInfo->getTemplateSpecializationKind(),
5582 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005583 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005584 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005585
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005586 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005587 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005588 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005589 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005590 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005591 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005592
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005593 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005594 // the original declaration to note that it is an explicit specialization
5595 // (if it was previously an implicit instantiation). This latter step
5596 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005597 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005598 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5599 if (InstantiationFunction->getTemplateSpecializationKind() ==
5600 TSK_ImplicitInstantiation) {
5601 InstantiationFunction->setTemplateSpecializationKind(
5602 TSK_ExplicitSpecialization);
5603 InstantiationFunction->setLocation(Member->getLocation());
5604 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005605
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005606 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5607 cast<CXXMethodDecl>(InstantiatedFrom),
5608 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005609 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005610 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005611 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5612 if (InstantiationVar->getTemplateSpecializationKind() ==
5613 TSK_ImplicitInstantiation) {
5614 InstantiationVar->setTemplateSpecializationKind(
5615 TSK_ExplicitSpecialization);
5616 InstantiationVar->setLocation(Member->getLocation());
5617 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005618
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005619 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5620 cast<VarDecl>(InstantiatedFrom),
5621 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005622 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005623 } else {
5624 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005625 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5626 if (InstantiationClass->getTemplateSpecializationKind() ==
5627 TSK_ImplicitInstantiation) {
5628 InstantiationClass->setTemplateSpecializationKind(
5629 TSK_ExplicitSpecialization);
5630 InstantiationClass->setLocation(Member->getLocation());
5631 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005632
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005633 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005634 cast<CXXRecordDecl>(InstantiatedFrom),
5635 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005636 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005637
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005638 // Save the caller the trouble of having to figure out which declaration
5639 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005640 Previous.clear();
5641 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005642 return false;
5643}
5644
Douglas Gregor558c0322009-10-14 23:41:34 +00005645/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005646///
5647/// \returns true if a serious error occurs, false otherwise.
5648static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005649 SourceLocation InstLoc,
5650 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005651 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5652 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005653
Douglas Gregor669eed82010-07-13 00:10:04 +00005654 if (CurContext->isRecord()) {
5655 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5656 << D;
5657 return true;
5658 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005659
Douglas Gregor558c0322009-10-14 23:41:34 +00005660 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005661 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005662 // template.
5663 //
5664 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005665 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005666 !CurContext->Encloses(OrigContext)) {
5667 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005668 S.Diag(InstLoc,
5669 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005670 diag::err_explicit_instantiation_out_of_scope
5671 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005672 << D << NS;
5673 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005674 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005675 S.getLangOptions().CPlusPlus0x?
5676 diag::err_explicit_instantiation_must_be_global
5677 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005678 << D;
5679 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005680 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005681 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005682
Douglas Gregor558c0322009-10-14 23:41:34 +00005683 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684 // If the name declared in the explicit instantiation is an unqualified
5685 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005686 // its template is declared or, if that namespace is inline (7.3.1), any
5687 // namespace from its enclosing namespace set.
5688 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005689 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005690
5691 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005692 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005693
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005694 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005695 S.getLangOptions().CPlusPlus0x?
5696 diag::err_explicit_instantiation_unqualified_wrong_namespace
5697 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005698 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005699 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005700 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005701}
5702
5703/// \brief Determine whether the given scope specifier has a template-id in it.
5704static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5705 if (!SS.isSet())
5706 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005707
Douglas Gregor558c0322009-10-14 23:41:34 +00005708 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005709 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005710 // or a static data member of a class template specialization, the name of
5711 // the class template specialization in the qualified-id for the member
5712 // name shall be a simple-template-id.
5713 //
5714 // C++98 has the same restriction, just worded differently.
5715 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5716 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005717 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005718 if (isa<TemplateSpecializationType>(T))
5719 return true;
5720
5721 return false;
5722}
5723
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005724// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005725DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005726Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005727 SourceLocation ExternLoc,
5728 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005729 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005730 SourceLocation KWLoc,
5731 const CXXScopeSpec &SS,
5732 TemplateTy TemplateD,
5733 SourceLocation TemplateNameLoc,
5734 SourceLocation LAngleLoc,
5735 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005736 SourceLocation RAngleLoc,
5737 AttributeList *Attr) {
5738 // Find the class template we're specializing
5739 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005740 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005741 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5742
5743 // Check that the specialization uses the same tag kind as the
5744 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005745 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5746 assert(Kind != TTK_Enum &&
5747 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005748 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005749 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005750 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005751 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005752 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005753 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005754 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005755 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005756 diag::note_previous_use);
5757 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5758 }
5759
Douglas Gregor558c0322009-10-14 23:41:34 +00005760 // C++0x [temp.explicit]p2:
5761 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005762 // definition and an explicit instantiation declaration. An explicit
5763 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005764 TemplateSpecializationKind TSK
5765 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5766 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005767
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005768 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005769 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005770 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005771
5772 // Check that the template argument list is well-formed for this
5773 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005774 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005775 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5776 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005777 return true;
5778
Douglas Gregor910f8002010-11-07 23:05:16 +00005779 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005780 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005781
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005782 // Find the class template specialization declaration that
5783 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005784 void *InsertPos = 0;
5785 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005786 = ClassTemplate->findSpecialization(Converted.data(),
5787 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005788
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005789 TemplateSpecializationKind PrevDecl_TSK
5790 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5791
Douglas Gregord5cb8762009-10-07 00:13:32 +00005792 // C++0x [temp.explicit]p2:
5793 // [...] An explicit instantiation shall appear in an enclosing
5794 // namespace of its template. [...]
5795 //
5796 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005797 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5798 SS.isSet()))
5799 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005800
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005801 ClassTemplateSpecializationDecl *Specialization = 0;
5802
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005803 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005804 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005805 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005806 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005807 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005808 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005809 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005810
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005811 // Even though HasNoEffect == true means that this explicit instantiation
5812 // has no effect on semantics, we go on to put its syntax in the AST.
5813
5814 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5815 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005816 // Since the only prior class template specialization with these
5817 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005818 // declaration node as our own, updating the source location
5819 // for the template name to reflect our new declaration.
5820 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005821 Specialization = PrevDecl;
5822 Specialization->setLocation(TemplateNameLoc);
5823 PrevDecl = 0;
5824 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005825 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005826
Douglas Gregor52604ab2009-09-11 21:19:12 +00005827 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005828 // Create a new class template specialization declaration node for
5829 // this explicit specialization.
5830 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005831 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005832 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005833 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005834 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005835 Converted.data(),
5836 Converted.size(),
5837 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005838 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005839
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005840 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005841 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005842 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005843 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005844 }
5845
5846 // Build the fully-sugared type for this explicit instantiation as
5847 // the user wrote in the explicit instantiation itself. This means
5848 // that we'll pretty-print the type retrieved from the
5849 // specialization's declaration the way that the user actually wrote
5850 // the explicit instantiation, rather than formatting the name based
5851 // on the "canonical" representation used to store the template
5852 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005853 TypeSourceInfo *WrittenTy
5854 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5855 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005856 Context.getTypeDeclType(Specialization));
5857 Specialization->setTypeAsWritten(WrittenTy);
5858 TemplateArgsIn.release();
5859
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005860 // Set source locations for keywords.
5861 Specialization->setExternLoc(ExternLoc);
5862 Specialization->setTemplateKeywordLoc(TemplateLoc);
5863
5864 // Add the explicit instantiation into its lexical context. However,
5865 // since explicit instantiations are never found by name lookup, we
5866 // just put it into the declaration context directly.
5867 Specialization->setLexicalDeclContext(CurContext);
5868 CurContext->addDecl(Specialization);
5869
5870 // Syntax is now OK, so return if it has no other effect on semantics.
5871 if (HasNoEffect) {
5872 // Set the template specialization kind.
5873 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005874 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005875 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005876
5877 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005878 // A definition of a class template or class member template
5879 // shall be in scope at the point of the explicit instantiation of
5880 // the class template or class member template.
5881 //
5882 // This check comes when we actually try to perform the
5883 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005884 ClassTemplateSpecializationDecl *Def
5885 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005886 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005887 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005888 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005889 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005890 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005891 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5892 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005893
Douglas Gregor0d035142009-10-27 18:42:08 +00005894 // Instantiate the members of this class template specialization.
5895 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005896 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005897 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005898 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5899
5900 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5901 // TSK_ExplicitInstantiationDefinition
5902 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5903 TSK == TSK_ExplicitInstantiationDefinition)
5904 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005905
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005906 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005907 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005908
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005909 // Set the template specialization kind.
5910 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005911 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005912}
5913
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005914// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005915DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005916Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005917 SourceLocation ExternLoc,
5918 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005919 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005920 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005921 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005922 IdentifierInfo *Name,
5923 SourceLocation NameLoc,
5924 AttributeList *Attr) {
5925
Douglas Gregor402abb52009-05-28 23:31:59 +00005926 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005927 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005928 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005929 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5930 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005931 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005932 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005933 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5934
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005935 if (!TagD)
5936 return true;
5937
John McCalld226f652010-08-21 09:40:31 +00005938 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005939 if (Tag->isEnum()) {
5940 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5941 << Context.getTypeDeclType(Tag);
5942 return true;
5943 }
5944
Douglas Gregord0c87372009-05-27 17:30:49 +00005945 if (Tag->isInvalidDecl())
5946 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005947
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005948 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5949 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5950 if (!Pattern) {
5951 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5952 << Context.getTypeDeclType(Record);
5953 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5954 return true;
5955 }
5956
Douglas Gregor558c0322009-10-14 23:41:34 +00005957 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005958 // If the explicit instantiation is for a class or member class, the
5959 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005960 // simple-template-id.
5961 //
5962 // C++98 has the same restriction, just worded differently.
5963 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005964 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005965 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005966
Douglas Gregor558c0322009-10-14 23:41:34 +00005967 // C++0x [temp.explicit]p2:
5968 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005969 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005970 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005971 TemplateSpecializationKind TSK
5972 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5973 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005974
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005975 // C++0x [temp.explicit]p2:
5976 // [...] An explicit instantiation shall appear in an enclosing
5977 // namespace of its template. [...]
5978 //
5979 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005980 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005981
Douglas Gregor454885e2009-10-15 15:54:05 +00005982 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005983 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005984 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005985 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005986 PrevDecl = Record;
5987 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005988 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005989 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005990 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005991 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005992 PrevDecl,
5993 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005994 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005995 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005996 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005997 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005998 return TagD;
5999 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006000
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006001 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006002 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006003 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006004 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006005 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006006 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006007 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006008 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006009 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006010 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6011 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006012 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6013 << Pattern;
6014 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006015 } else {
6016 if (InstantiateClass(NameLoc, Record, Def,
6017 getTemplateInstantiationArgs(Record),
6018 TSK))
6019 return true;
6020
Douglas Gregor952b0172010-02-11 01:04:33 +00006021 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006022 if (!RecordDef)
6023 return true;
6024 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006025 }
6026
Douglas Gregor0d035142009-10-27 18:42:08 +00006027 // Instantiate all of the members of the class.
6028 InstantiateClassMembers(NameLoc, RecordDef,
6029 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006030
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006031 if (TSK == TSK_ExplicitInstantiationDefinition)
6032 MarkVTableUsed(NameLoc, RecordDef, true);
6033
Mike Stump390b4cc2009-05-16 07:39:55 +00006034 // FIXME: We don't have any representation for explicit instantiations of
6035 // member classes. Such a representation is not needed for compilation, but it
6036 // should be available for clients that want to see all of the declarations in
6037 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006038 return TagD;
6039}
6040
John McCallf312b1e2010-08-26 23:41:50 +00006041DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6042 SourceLocation ExternLoc,
6043 SourceLocation TemplateLoc,
6044 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006045 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006046 // TODO: check if/when DNInfo should replace Name.
6047 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6048 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006049 if (!Name) {
6050 if (!D.isInvalidType())
6051 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6052 diag::err_explicit_instantiation_requires_name)
6053 << D.getDeclSpec().getSourceRange()
6054 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006055
Douglas Gregord5a423b2009-09-25 18:43:00 +00006056 return true;
6057 }
6058
6059 // The scope passed in may not be a decl scope. Zip up the scope tree until
6060 // we find one that is.
6061 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6062 (S->getFlags() & Scope::TemplateParamScope) != 0)
6063 S = S->getParent();
6064
6065 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006066 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6067 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006068 if (R.isNull())
6069 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006070
Douglas Gregore885e182011-05-21 18:53:30 +00006071 // C++ [dcl.stc]p1:
6072 // A storage-class-specifier shall not be specified in [...] an explicit
6073 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006074 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006075 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6076 << Name;
6077 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006078 } else if (D.getDeclSpec().getStorageClassSpec()
6079 != DeclSpec::SCS_unspecified) {
6080 // Complain about then remove the storage class specifier.
6081 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6082 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6083
6084 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006085 }
6086
Douglas Gregor663b5a02009-10-14 20:14:33 +00006087 // C++0x [temp.explicit]p1:
6088 // [...] An explicit instantiation of a function template shall not use the
6089 // inline or constexpr specifiers.
6090 // Presumably, this also applies to member functions of class templates as
6091 // well.
6092 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006093 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006094 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006095 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006096
Douglas Gregor663b5a02009-10-14 20:14:33 +00006097 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006098
Douglas Gregor558c0322009-10-14 23:41:34 +00006099 // C++0x [temp.explicit]p2:
6100 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006101 // definition and an explicit instantiation declaration. An explicit
6102 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006103 TemplateSpecializationKind TSK
6104 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6105 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006106
Abramo Bagnara25777432010-08-11 22:01:17 +00006107 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006108 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006109
6110 if (!R->isFunctionType()) {
6111 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006112 // A [...] static data member of a class template can be explicitly
6113 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006114 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006115 if (Previous.isAmbiguous())
6116 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006117
John McCall1bcee0a2009-12-02 08:25:40 +00006118 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006119 if (!Prev || !Prev->isStaticDataMember()) {
6120 // We expect to see a data data member here.
6121 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6122 << Name;
6123 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6124 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006125 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006126 return true;
6127 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006128
Douglas Gregord5a423b2009-09-25 18:43:00 +00006129 if (!Prev->getInstantiatedFromStaticDataMember()) {
6130 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006131 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006132 diag::err_explicit_instantiation_data_member_not_instantiated)
6133 << Prev;
6134 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6135 // FIXME: Can we provide a note showing where this was declared?
6136 return true;
6137 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006138
Douglas Gregor558c0322009-10-14 23:41:34 +00006139 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006140 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006141 // or a static data member of a class template specialization, the name of
6142 // the class template specialization in the qualified-id for the member
6143 // name shall be a simple-template-id.
6144 //
6145 // C++98 has the same restriction, just worded differently.
6146 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006147 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006148 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006149 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006150
Douglas Gregor558c0322009-10-14 23:41:34 +00006151 // Check the scope of this explicit instantiation.
6152 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006153
Douglas Gregor454885e2009-10-15 15:54:05 +00006154 // Verify that it is okay to explicitly instantiate here.
6155 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6156 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006157 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006158 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006159 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006160 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006161 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006162 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006163 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006164 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006165
Douglas Gregord5a423b2009-09-25 18:43:00 +00006166 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006167 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006168 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006169 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006170
Douglas Gregord5a423b2009-09-25 18:43:00 +00006171 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006172 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006174
6175 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006176 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006177 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006178 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006179 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6180 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006181 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6182 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006183 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6184 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006185 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006186 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006187 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006188 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006189 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006190
Douglas Gregord5a423b2009-09-25 18:43:00 +00006191 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006192 // A [...] function [...] can be explicitly instantiated from its template.
6193 // A member function [...] of a class template can be explicitly
6194 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006195 // template.
John McCallc373d482010-01-27 01:50:18 +00006196 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006197 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6198 P != PEnd; ++P) {
6199 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006200 if (!HasExplicitTemplateArgs) {
6201 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6202 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6203 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006204
John McCallc373d482010-01-27 01:50:18 +00006205 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006206 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6207 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006208 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006209 }
6210 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006211
Douglas Gregord5a423b2009-09-25 18:43:00 +00006212 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6213 if (!FunTmpl)
6214 continue;
6215
John McCall5769d612010-02-08 23:07:23 +00006216 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006217 FunctionDecl *Specialization = 0;
6218 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006219 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006220 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006221 R, Specialization, Info)) {
6222 // FIXME: Keep track of almost-matches?
6223 (void)TDK;
6224 continue;
6225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006226
John McCallc373d482010-01-27 01:50:18 +00006227 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006228 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006229
Douglas Gregord5a423b2009-09-25 18:43:00 +00006230 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006231 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006232 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006233 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006234 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6235 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6236 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006237
John McCallc373d482010-01-27 01:50:18 +00006238 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006239 return true;
John McCallc373d482010-01-27 01:50:18 +00006240
6241 // Ignore access control bits, we don't need them for redeclaration checking.
6242 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006243
Douglas Gregor0a897e32009-10-15 17:21:20 +00006244 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006245 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006246 diag::err_explicit_instantiation_member_function_not_instantiated)
6247 << Specialization
6248 << (Specialization->getTemplateSpecializationKind() ==
6249 TSK_ExplicitSpecialization);
6250 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6251 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006252 }
6253
Douglas Gregor0a897e32009-10-15 17:21:20 +00006254 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006255 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6256 PrevDecl = Specialization;
6257
Douglas Gregor0a897e32009-10-15 17:21:20 +00006258 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006259 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006260 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006261 PrevDecl,
6262 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006263 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006264 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006265 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006266
Douglas Gregor0a897e32009-10-15 17:21:20 +00006267 // FIXME: We may still want to build some representation of this
6268 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006269 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006270 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006271 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006272
6273 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006274
Douglas Gregor0a897e32009-10-15 17:21:20 +00006275 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006276 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006277
Douglas Gregor558c0322009-10-14 23:41:34 +00006278 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006280 // or a static data member of a class template specialization, the name of
6281 // the class template specialization in the qualified-id for the member
6282 // name shall be a simple-template-id.
6283 //
6284 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006285 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006286 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006287 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006288 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006289 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006290 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006291 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006292
Douglas Gregor558c0322009-10-14 23:41:34 +00006293 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006295 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006296 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006297 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006298
Douglas Gregord5a423b2009-09-25 18:43:00 +00006299 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006300 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006301}
6302
John McCallf312b1e2010-08-26 23:41:50 +00006303TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006304Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6305 const CXXScopeSpec &SS, IdentifierInfo *Name,
6306 SourceLocation TagLoc, SourceLocation NameLoc) {
6307 // This has to hold, because SS is expected to be defined.
6308 assert(Name && "Expected a name in a dependent tag");
6309
6310 NestedNameSpecifier *NNS
6311 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6312 if (!NNS)
6313 return true;
6314
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006315 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006316
Douglas Gregor48c89f42010-04-24 16:38:41 +00006317 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6318 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006319 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006320 return true;
6321 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006322
Douglas Gregor059101f2011-03-02 00:47:37 +00006323 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006324 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006325 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6326
6327 // Create type-source location information for this type.
6328 TypeLocBuilder TLB;
6329 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6330 TL.setKeywordLoc(TagLoc);
6331 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6332 TL.setNameLoc(NameLoc);
6333 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006334}
6335
John McCallf312b1e2010-08-26 23:41:50 +00006336TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6338 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006339 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006340 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006341 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006342
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006343 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6344 !getLangOptions().CPlusPlus0x)
6345 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006346 << FixItHint::CreateRemoval(TypenameLoc);
6347
Douglas Gregor2494dd02011-03-01 01:34:45 +00006348 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006349 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6350 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006351 if (T.isNull())
6352 return true;
John McCall63b43852010-04-29 23:50:39 +00006353
6354 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6355 if (isa<DependentNameType>(T)) {
6356 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006357 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006358 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006359 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006360 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006361 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006362 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006363 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006364 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006365 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006366
John McCallb3d87482010-08-24 05:47:05 +00006367 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006368}
6369
John McCallf312b1e2010-08-26 23:41:50 +00006370TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006371Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6372 const CXXScopeSpec &SS,
6373 SourceLocation TemplateLoc,
6374 TemplateTy TemplateIn,
6375 SourceLocation TemplateNameLoc,
6376 SourceLocation LAngleLoc,
6377 ASTTemplateArgsPtr TemplateArgsIn,
6378 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006379 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6380 !getLangOptions().CPlusPlus0x)
6381 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006382 << FixItHint::CreateRemoval(TypenameLoc);
6383
6384 // Translate the parser's template argument list in our AST format.
6385 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6386 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6387
6388 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006389 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6390 // Construct a dependent template specialization type.
6391 assert(DTN && "dependent template has non-dependent name?");
6392 assert(DTN->getQualifier()
6393 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6394 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6395 DTN->getQualifier(),
6396 DTN->getIdentifier(),
6397 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006398
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006399 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006400 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006401 DependentTemplateSpecializationTypeLoc SpecTL
6402 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006403 SpecTL.setLAngleLoc(LAngleLoc);
6404 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006405 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006406 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006407 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006408 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6409 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006410 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006411 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006412
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006413 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6414 if (T.isNull())
6415 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006416
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006417 // Provide source-location information for the template specialization
6418 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006419 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006420 TemplateSpecializationTypeLoc SpecTL
6421 = Builder.push<TemplateSpecializationTypeLoc>(T);
6422
6423 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006424 SpecTL.setLAngleLoc(LAngleLoc);
6425 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006426 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006427 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6428 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6429
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006430 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6431 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6432 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006433 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6434
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006435 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6436 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006437}
6438
Douglas Gregora02411e2011-02-27 22:46:49 +00006439
Douglas Gregord57959a2009-03-27 23:10:48 +00006440/// \brief Build the type that describes a C++ typename specifier,
6441/// e.g., "typename T::type".
6442QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006443Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6444 SourceLocation KeywordLoc,
6445 NestedNameSpecifierLoc QualifierLoc,
6446 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006447 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006448 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006449 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006450
John McCall77bb1aa2010-05-01 00:40:08 +00006451 DeclContext *Ctx = computeDeclContext(SS);
6452 if (!Ctx) {
6453 // If the nested-name-specifier is dependent and couldn't be
6454 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006455 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6456 return Context.getDependentNameType(Keyword,
6457 QualifierLoc.getNestedNameSpecifier(),
6458 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006459 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006460
John McCall77bb1aa2010-05-01 00:40:08 +00006461 // If the nested-name-specifier refers to the current instantiation,
6462 // the "typename" keyword itself is superfluous. In C++03, the
6463 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6464 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006465 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006466
John McCall77bb1aa2010-05-01 00:40:08 +00006467 if (RequireCompleteDeclContext(SS, Ctx))
6468 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006469
6470 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006471 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006472 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006473 unsigned DiagID = 0;
6474 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006475 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006476 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006477 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006478 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006479
6480 case LookupResult::FoundUnresolvedValue: {
6481 // We found a using declaration that is a value. Most likely, the using
6482 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006483 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006484 IILoc);
6485 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6486 << Name << Ctx << FullRange;
6487 if (UnresolvedUsingValueDecl *Using
6488 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006489 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006490 Diag(Loc, diag::note_using_value_decl_missing_typename)
6491 << FixItHint::CreateInsertion(Loc, "typename ");
6492 }
6493 }
6494 // Fall through to create a dependent typename type, from which we can recover
6495 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006496
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006497 case LookupResult::NotFoundInCurrentInstantiation:
6498 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006499 return Context.getDependentNameType(Keyword,
6500 QualifierLoc.getNestedNameSpecifier(),
6501 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006502
6503 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006504 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006505 // We found a type. Build an ElaboratedType, since the
6506 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006507 return Context.getElaboratedType(ETK_Typename,
6508 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006509 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006510 }
6511
6512 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006513 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006514 break;
6515
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006516
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006517 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006518 return QualType();
6519
Douglas Gregord57959a2009-03-27 23:10:48 +00006520 case LookupResult::FoundOverloaded:
6521 DiagID = diag::err_typename_nested_not_type;
6522 Referenced = *Result.begin();
6523 break;
6524
John McCall6e247262009-10-10 05:48:19 +00006525 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006526 return QualType();
6527 }
6528
6529 // If we get here, it's because name lookup did not find a
6530 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006531 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006532 IILoc);
6533 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006534 if (Referenced)
6535 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6536 << Name;
6537 return QualType();
6538}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006539
6540namespace {
6541 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006542 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006543 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006544 SourceLocation Loc;
6545 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006546
Douglas Gregor4a959d82009-08-06 16:20:37 +00006547 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006548 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006549
Mike Stump1eb44332009-09-09 15:08:12 +00006550 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006551 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006552 DeclarationName Entity)
6553 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006554 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006555
6556 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006557 /// transformed.
6558 ///
6559 /// For the purposes of type reconstruction, a type has already been
6560 /// transformed if it is NULL or if it is not dependent.
6561 bool AlreadyTransformed(QualType T) {
6562 return T.isNull() || !T->isDependentType();
6563 }
Mike Stump1eb44332009-09-09 15:08:12 +00006564
6565 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006566 /// rebuilt.
6567 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006568
Douglas Gregor4a959d82009-08-06 16:20:37 +00006569 /// \brief Returns the name of the entity whose type is being rebuilt.
6570 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006571
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006572 /// \brief Sets the "base" location and entity when that
6573 /// information is known based on another transformation.
6574 void setBase(SourceLocation Loc, DeclarationName Entity) {
6575 this->Loc = Loc;
6576 this->Entity = Entity;
6577 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006578 };
6579}
6580
Douglas Gregor4a959d82009-08-06 16:20:37 +00006581/// \brief Rebuilds a type within the context of the current instantiation.
6582///
Mike Stump1eb44332009-09-09 15:08:12 +00006583/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006584/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006585/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006586/// partial specialization thereof). This routine will rebuild that type now
6587/// that we have entered the declarator's scope, which may produce different
6588/// canonical types, e.g.,
6589///
6590/// \code
6591/// template<typename T>
6592/// struct X {
6593/// typedef T* pointer;
6594/// pointer data();
6595/// };
6596///
6597/// template<typename T>
6598/// typename X<T>::pointer X<T>::data() { ... }
6599/// \endcode
6600///
Douglas Gregor4714c122010-03-31 17:34:00 +00006601/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006602/// since we do not know that we can look into X<T> when we parsed the type.
6603/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006604/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006605/// as the canonical type of T*, allowing the return types of the out-of-line
6606/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006607TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6608 SourceLocation Loc,
6609 DeclarationName Name) {
6610 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006611 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006612
Douglas Gregor4a959d82009-08-06 16:20:37 +00006613 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6614 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006615}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006616
John McCall60d7b3a2010-08-24 06:29:42 +00006617ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006618 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6619 DeclarationName());
6620 return Rebuilder.TransformExpr(E);
6621}
6622
John McCall63b43852010-04-29 23:50:39 +00006623bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006624 if (SS.isInvalid())
6625 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006626
Douglas Gregor7e384942011-02-25 16:07:42 +00006627 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006628 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6629 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006630 NestedNameSpecifierLoc Rebuilt
6631 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6632 if (!Rebuilt)
6633 return true;
John McCall63b43852010-04-29 23:50:39 +00006634
Douglas Gregor7e384942011-02-25 16:07:42 +00006635 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006636 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006637}
6638
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006639/// \brief Produces a formatted string that describes the binding of
6640/// template parameters to template arguments.
6641std::string
6642Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6643 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006644 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006645}
6646
6647std::string
6648Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6649 const TemplateArgument *Args,
6650 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006651 llvm::SmallString<128> Str;
6652 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006653
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006654 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006655 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006656
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006657 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006658 if (I >= NumArgs)
6659 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006660
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006661 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006662 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006663 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006664 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006665
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006666 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006667 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006668 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006669 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006671
Douglas Gregor87dd6972010-12-20 16:52:59 +00006672 Out << " = ";
6673 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006674 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006675
6676 Out << ']';
6677 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006678}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006679
6680void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6681 if (!FD)
6682 return;
6683 FD->setLateTemplateParsed(Flag);
6684}
6685
6686bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6687 DeclContext *DC = CurContext;
6688
6689 while (DC) {
6690 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6691 const FunctionDecl *FD = RD->isLocalClass();
6692 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6693 } else if (DC->isTranslationUnit() || DC->isNamespace())
6694 return false;
6695
6696 DC = DC->getParent();
6697 }
6698 return false;
6699}