blob: bcdf32b5bc40196f378b2d74fb8d7b81b2e650b8 [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();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000297 Found.clear();
298 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
299 Found.getLookupKind(), S, &SS,
300 LookupCtx, false,
301 CTC_CXXCasts)) {
302 Found.setLookupName(Corrected.getCorrection());
303 if (Corrected.getCorrectionDecl())
304 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000306 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000307 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
308 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000309 if (LookupCtx)
310 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000311 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
312 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000313 else
314 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 << Name << CorrectedQuotedStr
316 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000317 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
318 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000319 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000320 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000321 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000322 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000323 }
324 }
325
Douglas Gregor312eadb2011-04-24 05:37:28 +0000326 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000327 if (Found.empty()) {
328 if (isDependent)
329 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000330 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000331 }
John McCallf7a1a742009-11-24 19:00:30 +0000332
333 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
334 // C++ [basic.lookup.classref]p1:
335 // [...] If the lookup in the class of the object expression finds a
336 // template, the name is also looked up in the context of the entire
337 // postfix-expression and [...]
338 //
339 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
340 LookupOrdinaryName);
341 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000342 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000343
John McCallf7a1a742009-11-24 19:00:30 +0000344 if (FoundOuter.empty()) {
345 // - if the name is not found, the name found in the class of the
346 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000347 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
348 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000349 // - if the name is found in the context of the entire
350 // postfix-expression and does not name a class template, the name
351 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000352 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000353 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000354 // - if the name found is a class template, it must refer to the same
355 // entity as the one found in the class of the object expression,
356 // otherwise the program is ill-formed.
357 if (!Found.isSingleResult() ||
358 Found.getFoundDecl()->getCanonicalDecl()
359 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000360 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000361 diag::ext_nested_name_member_ref_lookup_ambiguous)
362 << Found.getLookupName()
363 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000364 Diag(Found.getRepresentativeDecl()->getLocation(),
365 diag::note_ambig_member_ref_object_type)
366 << ObjectType;
367 Diag(FoundOuter.getFoundDecl()->getLocation(),
368 diag::note_ambig_member_ref_scope);
369
370 // Recover by taking the template that we found in the object
371 // expression's type.
372 }
373 }
374 }
375}
376
John McCall2f841ba2009-12-02 03:53:29 +0000377/// ActOnDependentIdExpression - Handle a dependent id-expression that
378/// was just parsed. This is only possible with an explicit scope
379/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000380ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000381Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000382 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000383 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000384 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000385 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386
John McCall2f841ba2009-12-02 03:53:29 +0000387 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000388 isa<CXXMethodDecl>(DC) &&
389 cast<CXXMethodDecl>(DC)->isInstance()) {
390 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000391
John McCallf7a1a742009-11-24 19:00:30 +0000392 // Since the 'this' expression is synthesized, we don't need to
393 // perform the double-lookup check.
394 NamedDecl *FirstQualifierInScope = 0;
395
John McCallaa81e162009-12-01 22:10:20 +0000396 return Owned(CXXDependentScopeMemberExpr::Create(Context,
397 /*This*/ 0, ThisType,
398 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000399 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000400 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000401 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000402 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000403 TemplateArgs));
404 }
405
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000407}
408
John McCall60d7b3a2010-08-24 06:29:42 +0000409ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000410Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000411 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000412 const TemplateArgumentListInfo *TemplateArgs) {
413 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000416 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000417}
418
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
420/// that the template parameter 'PrevDecl' is being shadowed by a new
421/// declaration at location Loc. Returns true to indicate that this is
422/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000423void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000424 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000425
426 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000427 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000428 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000429
430 // C++ [temp.local]p4:
431 // A template-parameter shall not be redeclared within its
432 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434 << cast<NamedDecl>(PrevDecl)->getDeclName();
435 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000436 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000437}
438
Douglas Gregor2943aed2009-03-03 04:44:36 +0000439/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000440/// the parameter D to reference the templated declaration and return a pointer
441/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000442TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
443 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
444 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000445 return Temp;
446 }
447 return 0;
448}
449
Douglas Gregorba68eca2011-01-05 17:40:24 +0000450ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
451 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000453 "Only template template arguments can be pack expansions here");
454 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
455 "Template template argument pack expansion without packs");
456 ParsedTemplateArgument Result(*this);
457 Result.EllipsisLoc = EllipsisLoc;
458 return Result;
459}
460
Douglas Gregor788cd062009-11-11 01:00:40 +0000461static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
462 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 switch (Arg.getKind()) {
465 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000470 return TemplateArgumentLoc(TemplateArgument(T), DI);
471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000472
Douglas Gregor788cd062009-11-11 01:00:40 +0000473 case ParsedTemplateArgument::NonType: {
474 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
475 return TemplateArgumentLoc(TemplateArgument(E), E);
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000479 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000480 TemplateArgument TArg;
481 if (Arg.getEllipsisLoc().isValid())
482 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
483 else
484 TArg = Template;
485 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000486 Arg.getScopeSpec().getWithLocInContext(
487 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000488 Arg.getLocation(),
489 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 }
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000493 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 return TemplateArgumentLoc();
495}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496
Douglas Gregor788cd062009-11-11 01:00:40 +0000497/// \brief Translates template arguments as provided by the parser
498/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000499void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
500 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000502 TemplateArgs.addArgument(translateTemplateArgument(*this,
503 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000504}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// ActOnTypeParameter - Called when a C++ template type parameter
507/// (e.g., "typename T") has been parsed. Typename specifies whether
508/// the keyword "typename" was used to declare the type parameter
509/// (otherwise, "class" was used), and KeyLoc is the location of the
510/// "class" or "typename" keyword. ParamName is the name of the
511/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000512/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513/// If the type parameter has a default argument, it will be added
514/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000515Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
516 SourceLocation EllipsisLoc,
517 SourceLocation KeyLoc,
518 IdentifierInfo *ParamName,
519 SourceLocation ParamNameLoc,
520 unsigned Depth, unsigned Position,
521 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000522 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 assert(S->isTemplateParamScope() &&
524 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 bool Invalid = false;
526
527 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000528 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000529 LookupOrdinaryName,
530 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter()) {
532 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
533 PrevDecl = 0;
534 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000535 }
536
Douglas Gregorddc29e12009-02-06 22:42:48 +0000537 SourceLocation Loc = ParamNameLoc;
538 if (!ParamName)
539 Loc = KeyLoc;
540
Douglas Gregor72c3f312008-12-05 18:15:24 +0000541 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000542 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000543 KeyLoc, Loc, Depth, Position, ParamName,
544 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000545 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546 if (Invalid)
547 Param->setInvalidDecl();
548
549 if (ParamName) {
550 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000551 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 IdResolver.AddDecl(Param);
553 }
554
Douglas Gregor61c4d282011-01-05 15:48:55 +0000555 // C++0x [temp.param]p9:
556 // A default template-argument may be specified for any kind of
557 // template-parameter that is not a template parameter pack.
558 if (DefaultArg && Ellipsis) {
559 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
560 DefaultArg = ParsedType();
561 }
562
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 // Handle the default argument, if provided.
564 if (DefaultArg) {
565 TypeSourceInfo *DefaultTInfo;
566 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000569
Douglas Gregor6f526752010-12-16 08:48:57 +0000570 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000572 UPPC_DefaultArgument))
573 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000574
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000575 // Check the template argument itself.
576 if (CheckTemplateArgument(Param, DefaultTInfo)) {
577 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000578 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000580
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000581 Param->setDefaultArgument(DefaultTInfo, false);
582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
John McCalld226f652010-08-21 09:40:31 +0000584 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000585}
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587/// \brief Check that the type of a non-type template parameter is
588/// well-formed.
589///
590/// \returns the (possibly-promoted) parameter type if valid;
591/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000592QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000593Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000594 // We don't allow variably-modified types as the type of non-type template
595 // parameters.
596 if (T->isVariablyModifiedType()) {
597 Diag(Loc, diag::err_variably_modified_nontype_template_param)
598 << T;
599 return QualType();
600 }
601
Douglas Gregor2943aed2009-03-03 04:44:36 +0000602 // C++ [temp.param]p4:
603 //
604 // A non-type template-parameter shall have one of the following
605 // (optionally cv-qualified) types:
606 //
607 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000608 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000609 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000610 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000611 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000612 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000613 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000615 // -- std::nullptr_t.
616 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 // If T is a dependent type, we can't do the check now, so we
618 // assume that it is well-formed.
619 T->isDependentType())
620 return T;
621 // C++ [temp.param]p8:
622 //
623 // A non-type template-parameter of type "array of T" or
624 // "function returning T" is adjusted to be of type "pointer to
625 // T" or "pointer to function returning T", respectively.
626 else if (T->isArrayType())
627 // FIXME: Keep the type prior to promotion?
628 return Context.getArrayDecayedType(T);
629 else if (T->isFunctionType())
630 // FIXME: Keep the type prior to promotion?
631 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000632
Douglas Gregor2943aed2009-03-03 04:44:36 +0000633 Diag(Loc, diag::err_template_nontype_parm_bad_type)
634 << T;
635
636 return QualType();
637}
638
John McCalld226f652010-08-21 09:40:31 +0000639Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
640 unsigned Depth,
641 unsigned Position,
642 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000643 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000644 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
645 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000646
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000647 assert(S->isTemplateParamScope() &&
648 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 bool Invalid = false;
650
651 IdentifierInfo *ParamName = D.getIdentifier();
652 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000653 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000654 LookupOrdinaryName,
655 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000656 if (PrevDecl && PrevDecl->isTemplateParameter()) {
657 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
658 PrevDecl = 0;
659 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 }
661
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000662 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
663 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000664 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000665 Invalid = true;
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor10738d32010-12-23 23:51:58 +0000668 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000669 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000670 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000671 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000672 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000673 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000674 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000675 Param->setAccess(AS_public);
676
Douglas Gregor72c3f312008-12-05 18:15:24 +0000677 if (Invalid)
678 Param->setInvalidDecl();
679
680 if (D.getIdentifier()) {
681 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000682 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000683 IdResolver.AddDecl(Param);
684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
Douglas Gregor61c4d282011-01-05 15:48:55 +0000686 // C++0x [temp.param]p9:
687 // A default template-argument may be specified for any kind of
688 // template-parameter that is not a template parameter pack.
689 if (Default && IsParameterPack) {
690 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
691 Default = 0;
692 }
693
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000695 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000696 // Check for unexpanded parameter packs.
697 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
698 return Param;
699
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000700 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000701 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
702 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000704 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000705 }
John Wiegley429bb272011-04-08 18:41:53 +0000706 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000707
John McCall9ae2f072010-08-23 23:25:46 +0000708 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000710
John McCalld226f652010-08-21 09:40:31 +0000711 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000712}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000713
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714/// ActOnTemplateTemplateParameter - Called when a C++ template template
715/// parameter (e.g. T in template <template <typename> class T> class array)
716/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000717Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
718 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000719 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000720 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000721 IdentifierInfo *Name,
722 SourceLocation NameLoc,
723 unsigned Depth,
724 unsigned Position,
725 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000726 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000727 assert(S->isTemplateParamScope() &&
728 "Template template parameter not in template parameter scope!");
729
730 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000733 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000734 NameLoc.isInvalid()? TmpLoc : NameLoc,
735 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000736 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000737 Param->setAccess(AS_public);
738
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000742 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 IdResolver.AddDecl(Param);
744 }
745
Douglas Gregor6f526752010-12-16 08:48:57 +0000746 if (Params->size() == 0) {
747 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
748 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
749 Param->setInvalidDecl();
750 }
751
Douglas Gregor61c4d282011-01-05 15:48:55 +0000752 // C++0x [temp.param]p9:
753 // A default template-argument may be specified for any kind of
754 // template-parameter that is not a template parameter pack.
755 if (IsParameterPack && !Default.isInvalid()) {
756 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
757 Default = ParsedTemplateArgument();
758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000760 if (!Default.isInvalid()) {
761 // Check only that we have a template template argument. We don't want to
762 // try to check well-formedness now, because our template template parameter
763 // might have dependent types in its template parameters, which we wouldn't
764 // be able to match now.
765 //
766 // If none of the template template parameter's template arguments mention
767 // other template parameters, we could actually perform more checking here.
768 // However, it isn't worth doing.
769 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
770 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
771 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
772 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000773 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000778 DefaultArg.getArgument().getAsTemplate(),
779 UPPC_DefaultArgument))
780 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000782 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000783 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000784
John McCalld226f652010-08-21 09:40:31 +0000785 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000786}
787
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000788/// ActOnTemplateParameterList - Builds a TemplateParameterList that
789/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000790TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000791Sema::ActOnTemplateParameterList(unsigned Depth,
792 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000795 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000796 SourceLocation RAngleLoc) {
797 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000798 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000799
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000801 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000802 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000803}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000804
John McCallb6217662010-03-15 10:12:16 +0000805static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
806 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000807 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000808}
809
John McCallf312b1e2010-08-26 23:41:50 +0000810DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000811Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000812 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 IdentifierInfo *Name, SourceLocation NameLoc,
814 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000816 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000817 unsigned NumOuterTemplateParamLists,
818 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000819 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000820 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000821 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000822 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000823
824 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000825 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000826 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000827
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000828 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
829 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000830
831 // There is no such thing as an unnamed class template.
832 if (!Name) {
833 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000834 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000835 }
836
837 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000838 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000839 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000840 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 if (SS.isNotEmpty() && !SS.isInvalid()) {
842 SemanticContext = computeDeclContext(SS, true);
843 if (!SemanticContext) {
844 // FIXME: Produce a reasonable diagnostic here
845 return true;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCall77bb1aa2010-05-01 00:40:08 +0000848 if (RequireCompleteDeclContext(SS, SemanticContext))
849 return true;
850
Douglas Gregor20606502011-10-14 15:31:12 +0000851 // If we're adding a template to a dependent context, we may need to
852 // rebuilding some of the types used within the template parameter list,
853 // now that we know what the current instantiation is.
854 if (SemanticContext->isDependentContext()) {
855 ContextRAII SavedContext(*this, SemanticContext);
856 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
857 Invalid = true;
858 }
859
John McCalla24dc2e2009-11-17 02:14:36 +0000860 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000861 } else {
862 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000863 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor57265e32010-04-12 16:00:01 +0000866 if (Previous.isAmbiguous())
867 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868
Douglas Gregorddc29e12009-02-06 22:42:48 +0000869 NamedDecl *PrevDecl = 0;
870 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000871 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000872
Douglas Gregorddc29e12009-02-06 22:42:48 +0000873 // If there is a previous declaration with the same name, check
874 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000875 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000876 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000877
878 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000880 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000881 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000882 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
883 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000885 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
886 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
887 PrevClassTemplate
888 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
889 ->getSpecializedTemplate();
890 }
891 }
892
John McCall65c49462009-12-18 11:25:59 +0000893 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000894 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // [...] When looking for a prior declaration of a class or a function
896 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000897 // function is neither a qualified name nor a template-id, scopes outside
898 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000899 if (!SS.isSet()) {
900 DeclContext *OutermostContext = CurContext;
901 while (!OutermostContext->isFileContext())
902 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000903
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000904 if (PrevDecl &&
905 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
906 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
907 SemanticContext = PrevDecl->getDeclContext();
908 } else {
909 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000911 // declaration.
912 PrevDecl = PrevClassTemplate = 0;
913 SemanticContext = OutermostContext;
914 }
John McCalle129d442009-12-17 23:21:11 +0000915 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000916
John McCalle129d442009-12-17 23:21:11 +0000917 if (CurContext->isDependentContext()) {
918 // If this is a dependent context, we don't want to link the friend
919 // class template to the template in scope, because that would perform
920 // checking of the template parameter lists that can't be performed
921 // until the outer context is instantiated.
922 PrevDecl = PrevClassTemplate = 0;
923 }
924 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
925 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000926
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 if (PrevClassTemplate) {
928 // Ensure that the template parameter lists are compatible.
929 if (!TemplateParameterListsAreEqual(TemplateParams,
930 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000931 /*Complain=*/true,
932 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000933 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934
935 // C++ [temp.class]p4:
936 // In a redeclaration, partial specialization, explicit
937 // specialization or explicit instantiation of a class template,
938 // the class-key shall agree in kind with the original class
939 // template declaration (7.1.5.3).
940 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000941 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
942 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000944 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000945 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000946 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000947 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000948 }
949
Douglas Gregorddc29e12009-02-06 22:42:48 +0000950 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000951 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000952 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000953 Diag(NameLoc, diag::err_redefinition) << Name;
954 Diag(Def->getLocation(), diag::note_previous_definition);
955 // FIXME: Would it make sense to try to "forget" the previous
956 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000957 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000958 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000959 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000960 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
961 // Maybe we will complain about the shadowed template parameter.
962 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
963 // Just pretend that we didn't see the previous declaration.
964 PrevDecl = 0;
965 } else if (PrevDecl) {
966 // C++ [temp]p5:
967 // A class template shall not have the same name as any other
968 // template, class, function, object, enumeration, enumerator,
969 // namespace, or type in the same scope (3.3), except as specified
970 // in (14.5.4).
971 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
972 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000973 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 }
975
Douglas Gregord684b002009-02-10 19:49:53 +0000976 // Check the template parameter list of this declaration, possibly
977 // merging in the template parameter list from the previous class
978 // template declaration.
979 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000980 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000981 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000982 SemanticContext->isRecord() &&
983 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000984 ? TPC_ClassTemplateMember
985 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000986 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregor57265e32010-04-12 16:00:01 +0000988 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000989 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000990 // template out-of-line.
991 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +0000992 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +0000993 Diag(NameLoc, diag::err_member_def_does_not_match)
994 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +0000995 Invalid = true;
996 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000997 }
998
Mike Stump1eb44332009-09-09 15:08:12 +0000999 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001000 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001001 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001002 PrevClassTemplate->getTemplatedDecl() : 0,
1003 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001004 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001005 if (NumOuterTemplateParamLists > 0)
1006 NewClass->setTemplateParameterListsInfo(Context,
1007 NumOuterTemplateParamLists,
1008 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009
1010 ClassTemplateDecl *NewTemplate
1011 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1012 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001013 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001014 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001015
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001016 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001017 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001018
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001019 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001020 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001021 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001022 assert(T->isDependentType() && "Class template type is not dependent?");
1023 (void)T;
1024
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001025 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001026 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001027 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001028 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1029 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001031 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001032 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001033 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Douglas Gregorddc29e12009-02-06 22:42:48 +00001035 // Set the lexical context of these templates
1036 NewClass->setLexicalDeclContext(CurContext);
1037 NewTemplate->setLexicalDeclContext(CurContext);
1038
John McCall0f434ec2009-07-31 02:45:11 +00001039 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001040 NewClass->startDefinition();
1041
1042 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001043 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001044
John McCall05b23ea2009-09-14 21:59:20 +00001045 if (TUK != TUK_Friend)
1046 PushOnScopeChains(NewTemplate, S);
1047 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001048 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001049 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001050 NewClass->setAccess(PrevClassTemplate->getAccess());
1051 }
John McCall05b23ea2009-09-14 21:59:20 +00001052
Douglas Gregord85bea22009-09-26 06:47:28 +00001053 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1054 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001055
John McCall05b23ea2009-09-14 21:59:20 +00001056 // Friend templates are visible in fairly strange ways.
1057 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001058 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001059 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1060 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1061 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001062 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001063 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001064
Douglas Gregord85bea22009-09-26 06:47:28 +00001065 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1066 NewClass->getLocation(),
1067 NewTemplate,
1068 /*FIXME:*/NewClass->getLocation());
1069 Friend->setAccess(AS_public);
1070 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001071 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001072
Douglas Gregord684b002009-02-10 19:49:53 +00001073 if (Invalid) {
1074 NewTemplate->setInvalidDecl();
1075 NewClass->setInvalidDecl();
1076 }
John McCalld226f652010-08-21 09:40:31 +00001077 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001078}
1079
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080/// \brief Diagnose the presence of a default template argument on a
1081/// template parameter, which is ill-formed in certain contexts.
1082///
1083/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001084static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085 Sema::TemplateParamListContext TPC,
1086 SourceLocation ParamLoc,
1087 SourceRange DefArgRange) {
1088 switch (TPC) {
1089 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001090 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001091 return false;
1092
1093 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001094 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001096 // A default template-argument shall not be specified in a
1097 // function template declaration or a function template
1098 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001099 // If a friend function template declaration specifies a default
1100 // template-argument, that declaration shall be a definition and shall be
1101 // the only declaration of the function template in the translation unit.
1102 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001103 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1104 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1105 : diag::ext_template_parameter_default_in_function_template)
1106 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001107 return false;
1108
1109 case Sema::TPC_ClassTemplateMember:
1110 // C++0x [temp.param]p9:
1111 // A default template-argument shall not be specified in the
1112 // template-parameter-lists of the definition of a member of a
1113 // class template that appears outside of the member's class.
1114 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1115 << DefArgRange;
1116 return true;
1117
1118 case Sema::TPC_FriendFunctionTemplate:
1119 // C++ [temp.param]p9:
1120 // A default template-argument shall not be specified in a
1121 // friend template declaration.
1122 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1123 << DefArgRange;
1124 return true;
1125
1126 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1127 // for friend function templates if there is only a single
1128 // declaration (and it is a definition). Strange!
1129 }
1130
1131 return false;
1132}
1133
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001134/// \brief Check for unexpanded parameter packs within the template parameters
1135/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001136static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1137 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001138 TemplateParameterList *Params = TTP->getTemplateParameters();
1139 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1140 NamedDecl *P = Params->getParam(I);
1141 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001142 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001143 NTTP->getTypeSourceInfo(),
1144 Sema::UPPC_NonTypeTemplateParameterType))
1145 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001146
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001147 continue;
1148 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001149
1150 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001151 = dyn_cast<TemplateTemplateParmDecl>(P))
1152 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1153 return true;
1154 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001155
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001156 return false;
1157}
1158
Douglas Gregord684b002009-02-10 19:49:53 +00001159/// \brief Checks the validity of a template parameter list, possibly
1160/// considering the template parameter list from a previous
1161/// declaration.
1162///
1163/// If an "old" template parameter list is provided, it must be
1164/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1165/// template parameter list.
1166///
1167/// \param NewParams Template parameter list for a new template
1168/// declaration. This template parameter list will be updated with any
1169/// default arguments that are carried through from the previous
1170/// template parameter list.
1171///
1172/// \param OldParams If provided, template parameter list from a
1173/// previous declaration of the same template. Default template
1174/// arguments will be merged from the old template parameter list to
1175/// the new template parameter list.
1176///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001177/// \param TPC Describes the context in which we are checking the given
1178/// template parameter list.
1179///
Douglas Gregord684b002009-02-10 19:49:53 +00001180/// \returns true if an error occurred, false otherwise.
1181bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182 TemplateParameterList *OldParams,
1183 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001184 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Douglas Gregord684b002009-02-10 19:49:53 +00001186 // C++ [temp.param]p10:
1187 // The set of default template-arguments available for use with a
1188 // template declaration or definition is obtained by merging the
1189 // default arguments from the definition (if in scope) and all
1190 // declarations in scope in the same way default function
1191 // arguments are (8.3.6).
1192 bool SawDefaultArgument = false;
1193 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001194
Mike Stump1a35fde2009-02-11 23:03:27 +00001195 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001196 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001197 if (OldParams)
1198 OldParam = OldParams->begin();
1199
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001200 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001201 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1202 NewParamEnd = NewParams->end();
1203 NewParam != NewParamEnd; ++NewParam) {
1204 // Variables used to diagnose redundant default arguments
1205 bool RedundantDefaultArg = false;
1206 SourceLocation OldDefaultLoc;
1207 SourceLocation NewDefaultLoc;
1208
David Blaikie1368e582011-10-19 05:19:50 +00001209 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001210 bool MissingDefaultArg = false;
1211
David Blaikie1368e582011-10-19 05:19:50 +00001212 // Variable used to diagnose non-final parameter packs
1213 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001214
Douglas Gregord684b002009-02-10 19:49:53 +00001215 if (TemplateTypeParmDecl *NewTypeParm
1216 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001217 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001218 if (NewTypeParm->hasDefaultArgument() &&
1219 DiagnoseDefaultTemplateArgument(*this, TPC,
1220 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001221 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001222 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001223 NewTypeParm->removeDefaultArgument();
1224
1225 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001226 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001227 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Anders Carlsson49d25572009-06-12 23:20:15 +00001229 if (NewTypeParm->isParameterPack()) {
1230 assert(!NewTypeParm->hasDefaultArgument() &&
1231 "Parameter packs can't have a default argument!");
1232 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001233 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001234 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001235 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1236 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1237 SawDefaultArgument = true;
1238 RedundantDefaultArg = true;
1239 PreviousDefaultArgLoc = NewDefaultLoc;
1240 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1241 // Merge the default argument from the old declaration to the
1242 // new declaration.
1243 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001244 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001245 true);
1246 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1247 } else if (NewTypeParm->hasDefaultArgument()) {
1248 SawDefaultArgument = true;
1249 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1250 } else if (SawDefaultArgument)
1251 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001252 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001253 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001254 // Check for unexpanded parameter packs.
1255 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001256 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001257 UPPC_NonTypeTemplateParameterType)) {
1258 Invalid = true;
1259 continue;
1260 }
1261
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001262 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001263 if (NewNonTypeParm->hasDefaultArgument() &&
1264 DiagnoseDefaultTemplateArgument(*this, TPC,
1265 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001266 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001267 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001268 }
1269
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001270 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001271 NonTypeTemplateParmDecl *OldNonTypeParm
1272 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001273 if (NewNonTypeParm->isParameterPack()) {
1274 assert(!NewNonTypeParm->hasDefaultArgument() &&
1275 "Parameter packs can't have a default argument!");
1276 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001277 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001278 NewNonTypeParm->hasDefaultArgument()) {
1279 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1280 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1281 SawDefaultArgument = true;
1282 RedundantDefaultArg = true;
1283 PreviousDefaultArgLoc = NewDefaultLoc;
1284 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1285 // Merge the default argument from the old declaration to the
1286 // new declaration.
1287 SawDefaultArgument = true;
1288 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001289 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001290 // parameter.
1291 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001292 OldNonTypeParm->getDefaultArgument(),
1293 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001294 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1295 } else if (NewNonTypeParm->hasDefaultArgument()) {
1296 SawDefaultArgument = true;
1297 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1298 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001299 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001300 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001301 TemplateTemplateParmDecl *NewTemplateParm
1302 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001303
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001304 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001305 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001306 Invalid = true;
1307 continue;
1308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001309
David Blaikie1368e582011-10-19 05:19:50 +00001310 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001311 if (NewTemplateParm->hasDefaultArgument() &&
1312 DiagnoseDefaultTemplateArgument(*this, TPC,
1313 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001314 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001315 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001316
1317 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001318 TemplateTemplateParmDecl *OldTemplateParm
1319 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001320 if (NewTemplateParm->isParameterPack()) {
1321 assert(!NewTemplateParm->hasDefaultArgument() &&
1322 "Parameter packs can't have a default argument!");
1323 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001324 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001325 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001326 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1327 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001328 SawDefaultArgument = true;
1329 RedundantDefaultArg = true;
1330 PreviousDefaultArgLoc = NewDefaultLoc;
1331 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1332 // Merge the default argument from the old declaration to the
1333 // new declaration.
1334 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001335 // FIXME: We need to create a new kind of "default argument" expression
1336 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001337 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001338 OldTemplateParm->getDefaultArgument(),
1339 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001340 PreviousDefaultArgLoc
1341 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001342 } else if (NewTemplateParm->hasDefaultArgument()) {
1343 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001344 PreviousDefaultArgLoc
1345 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001346 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001347 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001348 }
1349
David Blaikie1368e582011-10-19 05:19:50 +00001350 // C++0x [temp.param]p11:
1351 // If a template parameter of a primary class template or alias template
1352 // is a template parameter pack, it shall be the last template parameter.
1353 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1354 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1355 Diag((*NewParam)->getLocation(),
1356 diag::err_template_param_pack_must_be_last_template_parameter);
1357 Invalid = true;
1358 }
1359
Douglas Gregord684b002009-02-10 19:49:53 +00001360 if (RedundantDefaultArg) {
1361 // C++ [temp.param]p12:
1362 // A template-parameter shall not be given default arguments
1363 // by two different declarations in the same scope.
1364 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1365 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1366 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001367 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001368 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001369 // If a template-parameter of a class template has a default
1370 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001371 // have a default template-argument supplied or be a template parameter
1372 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001373 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001374 diag::err_template_param_default_arg_missing);
1375 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1376 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001377 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001378 }
1379
1380 // If we have an old template parameter list that we're merging
1381 // in, move on to the next parameter.
1382 if (OldParams)
1383 ++OldParam;
1384 }
1385
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001386 // We were missing some default arguments at the end of the list, so remove
1387 // all of the default arguments.
1388 if (RemoveDefaultArguments) {
1389 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1390 NewParamEnd = NewParams->end();
1391 NewParam != NewParamEnd; ++NewParam) {
1392 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1393 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001394 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001395 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1396 NTTP->removeDefaultArgument();
1397 else
1398 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1399 }
1400 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001401
Douglas Gregord684b002009-02-10 19:49:53 +00001402 return Invalid;
1403}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001404
John McCall4e2cbb22010-10-20 05:44:58 +00001405namespace {
1406
1407/// A class which looks for a use of a certain level of template
1408/// parameter.
1409struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1410 typedef RecursiveASTVisitor<DependencyChecker> super;
1411
1412 unsigned Depth;
1413 bool Match;
1414
1415 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1416 NamedDecl *ND = Params->getParam(0);
1417 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1418 Depth = PD->getDepth();
1419 } else if (NonTypeTemplateParmDecl *PD =
1420 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1421 Depth = PD->getDepth();
1422 } else {
1423 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1424 }
1425 }
1426
1427 bool Matches(unsigned ParmDepth) {
1428 if (ParmDepth >= Depth) {
1429 Match = true;
1430 return true;
1431 }
1432 return false;
1433 }
1434
1435 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1436 return !Matches(T->getDepth());
1437 }
1438
1439 bool TraverseTemplateName(TemplateName N) {
1440 if (TemplateTemplateParmDecl *PD =
1441 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1442 if (Matches(PD->getDepth())) return false;
1443 return super::TraverseTemplateName(N);
1444 }
1445
1446 bool VisitDeclRefExpr(DeclRefExpr *E) {
1447 if (NonTypeTemplateParmDecl *PD =
1448 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1449 if (PD->getDepth() == Depth) {
1450 Match = true;
1451 return false;
1452 }
1453 }
1454 return super::VisitDeclRefExpr(E);
1455 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001456
1457 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1458 return TraverseType(T->getInjectedSpecializationType());
1459 }
John McCall4e2cbb22010-10-20 05:44:58 +00001460};
1461}
1462
Douglas Gregorc8406492011-05-10 18:27:06 +00001463/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001464/// list.
1465static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001466DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001467 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001468 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001469 return Checker.Match;
1470}
1471
Douglas Gregorc8406492011-05-10 18:27:06 +00001472// Find the source range corresponding to the named type in the given
1473// nested-name-specifier, if any.
1474static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1475 QualType T,
1476 const CXXScopeSpec &SS) {
1477 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1478 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1479 if (const Type *CurType = NNS->getAsType()) {
1480 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1481 return NNSLoc.getTypeLoc().getSourceRange();
1482 } else
1483 break;
1484
1485 NNSLoc = NNSLoc.getPrefix();
1486 }
1487
1488 return SourceRange();
1489}
1490
Mike Stump1eb44332009-09-09 15:08:12 +00001491/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001492/// specifier, returning the template parameter list that applies to the
1493/// name.
1494///
1495/// \param DeclStartLoc the start of the declaration that has a scope
1496/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001497///
Douglas Gregorc8406492011-05-10 18:27:06 +00001498/// \param DeclLoc The location of the declaration itself.
1499///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001500/// \param SS the scope specifier that will be matched to the given template
1501/// parameter lists. This scope specifier precedes a qualified name that is
1502/// being declared.
1503///
1504/// \param ParamLists the template parameter lists, from the outermost to the
1505/// innermost template parameter lists.
1506///
1507/// \param NumParamLists the number of template parameter lists in ParamLists.
1508///
John McCall77e8b112010-04-13 20:37:33 +00001509/// \param IsFriend Whether to apply the slightly different rules for
1510/// matching template parameters to scope specifiers in friend
1511/// declarations.
1512///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001513/// \param IsExplicitSpecialization will be set true if the entity being
1514/// declared is an explicit specialization, false otherwise.
1515///
Mike Stump1eb44332009-09-09 15:08:12 +00001516/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001518/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001519/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001520/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521/// itself a template).
1522TemplateParameterList *
1523Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001524 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001525 const CXXScopeSpec &SS,
1526 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001527 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001528 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001529 bool &IsExplicitSpecialization,
1530 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001531 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001532 Invalid = false;
1533
1534 // The sequence of nested types to which we will match up the template
1535 // parameter lists. We first build this list by starting with the type named
1536 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001537 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001538 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001539 if (SS.getScopeRep()) {
1540 if (CXXRecordDecl *Record
1541 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1542 T = Context.getTypeDeclType(Record);
1543 else
1544 T = QualType(SS.getScopeRep()->getAsType(), 0);
1545 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001546
1547 // If we found an explicit specialization that prevents us from needing
1548 // 'template<>' headers, this will be set to the location of that
1549 // explicit specialization.
1550 SourceLocation ExplicitSpecLoc;
1551
1552 while (!T.isNull()) {
1553 NestedTypes.push_back(T);
1554
1555 // Retrieve the parent of a record type.
1556 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1557 // If this type is an explicit specialization, we're done.
1558 if (ClassTemplateSpecializationDecl *Spec
1559 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1560 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1561 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1562 ExplicitSpecLoc = Spec->getLocation();
1563 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001564 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001565 } else if (Record->getTemplateSpecializationKind()
1566 == TSK_ExplicitSpecialization) {
1567 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001568 break;
1569 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001570
1571 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1572 T = Context.getTypeDeclType(Parent);
1573 else
1574 T = QualType();
1575 continue;
1576 }
1577
1578 if (const TemplateSpecializationType *TST
1579 = T->getAs<TemplateSpecializationType>()) {
1580 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1581 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1582 T = Context.getTypeDeclType(Parent);
1583 else
1584 T = QualType();
1585 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001586 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001587 }
1588
1589 // Look one step prior in a dependent template specialization type.
1590 if (const DependentTemplateSpecializationType *DependentTST
1591 = T->getAs<DependentTemplateSpecializationType>()) {
1592 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1593 T = QualType(NNS->getAsType(), 0);
1594 else
1595 T = QualType();
1596 continue;
1597 }
1598
1599 // Look one step prior in a dependent name type.
1600 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1601 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1602 T = QualType(NNS->getAsType(), 0);
1603 else
1604 T = QualType();
1605 continue;
1606 }
1607
1608 // Retrieve the parent of an enumeration type.
1609 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1610 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1611 // check here.
1612 EnumDecl *Enum = EnumT->getDecl();
1613
1614 // Get to the parent type.
1615 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1616 T = Context.getTypeDeclType(Parent);
1617 else
1618 T = QualType();
1619 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001620 }
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Douglas Gregorc8406492011-05-10 18:27:06 +00001622 T = QualType();
1623 }
1624 // Reverse the nested types list, since we want to traverse from the outermost
1625 // to the innermost while checking template-parameter-lists.
1626 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001627
Douglas Gregorc8406492011-05-10 18:27:06 +00001628 // C++0x [temp.expl.spec]p17:
1629 // A member or a member template may be nested within many
1630 // enclosing class templates. In an explicit specialization for
1631 // such a member, the member declaration shall be preceded by a
1632 // template<> for each enclosing class template that is
1633 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001634 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001635 unsigned ParamIdx = 0;
1636 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1637 ++TypeIdx) {
1638 T = NestedTypes[TypeIdx];
1639
1640 // Whether we expect a 'template<>' header.
1641 bool NeedEmptyTemplateHeader = false;
1642
1643 // Whether we expect a template header with parameters.
1644 bool NeedNonemptyTemplateHeader = false;
1645
1646 // For a dependent type, the set of template parameters that we
1647 // expect to see.
1648 TemplateParameterList *ExpectedTemplateParams = 0;
1649
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001650 // C++0x [temp.expl.spec]p15:
1651 // A member or a member template may be nested within many enclosing
1652 // class templates. In an explicit specialization for such a member, the
1653 // member declaration shall be preceded by a template<> for each
1654 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001655 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1656 if (ClassTemplatePartialSpecializationDecl *Partial
1657 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1658 ExpectedTemplateParams = Partial->getTemplateParameters();
1659 NeedNonemptyTemplateHeader = true;
1660 } else if (Record->isDependentType()) {
1661 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001662 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001663 ->getTemplateParameters();
1664 NeedNonemptyTemplateHeader = true;
1665 }
1666 } else if (ClassTemplateSpecializationDecl *Spec
1667 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1668 // C++0x [temp.expl.spec]p4:
1669 // Members of an explicitly specialized class template are defined
1670 // in the same manner as members of normal classes, and not using
1671 // the template<> syntax.
1672 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1673 NeedEmptyTemplateHeader = true;
1674 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001675 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001676 } else if (Record->getTemplateSpecializationKind()) {
1677 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001678 != TSK_ExplicitSpecialization &&
1679 TypeIdx == NumTypes - 1)
1680 IsExplicitSpecialization = true;
1681
1682 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001683 }
1684 } else if (const TemplateSpecializationType *TST
1685 = T->getAs<TemplateSpecializationType>()) {
1686 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1687 ExpectedTemplateParams = Template->getTemplateParameters();
1688 NeedNonemptyTemplateHeader = true;
1689 }
1690 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1691 // FIXME: We actually could/should check the template arguments here
1692 // against the corresponding template parameter list.
1693 NeedNonemptyTemplateHeader = false;
1694 }
1695
Douglas Gregor89b9f102011-06-06 15:22:55 +00001696 // C++ [temp.expl.spec]p16:
1697 // In an explicit specialization declaration for a member of a class
1698 // template or a member template that ap- pears in namespace scope, the
1699 // member template and some of its enclosing class templates may remain
1700 // unspecialized, except that the declaration shall not explicitly
1701 // specialize a class member template if its en- closing class templates
1702 // are not explicitly specialized as well.
1703 if (ParamIdx < NumParamLists) {
1704 if (ParamLists[ParamIdx]->size() == 0) {
1705 if (SawNonEmptyTemplateParameterList) {
1706 Diag(DeclLoc, diag::err_specialize_member_of_template)
1707 << ParamLists[ParamIdx]->getSourceRange();
1708 Invalid = true;
1709 IsExplicitSpecialization = false;
1710 return 0;
1711 }
1712 } else
1713 SawNonEmptyTemplateParameterList = true;
1714 }
1715
Douglas Gregorc8406492011-05-10 18:27:06 +00001716 if (NeedEmptyTemplateHeader) {
1717 // If we're on the last of the types, and we need a 'template<>' header
1718 // here, then it's an explicit specialization.
1719 if (TypeIdx == NumTypes - 1)
1720 IsExplicitSpecialization = true;
1721
1722 if (ParamIdx < NumParamLists) {
1723 if (ParamLists[ParamIdx]->size() > 0) {
1724 // The header has template parameters when it shouldn't. Complain.
1725 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1726 diag::err_template_param_list_matches_nontemplate)
1727 << T
1728 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1729 ParamLists[ParamIdx]->getRAngleLoc())
1730 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1731 Invalid = true;
1732 return 0;
1733 }
1734
1735 // Consume this template header.
1736 ++ParamIdx;
1737 continue;
1738 }
1739
1740 if (!IsFriend) {
1741 // We don't have a template header, but we should.
1742 SourceLocation ExpectedTemplateLoc;
1743 if (NumParamLists > 0)
1744 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1745 else
1746 ExpectedTemplateLoc = DeclStartLoc;
1747
1748 Diag(DeclLoc, diag::err_template_spec_needs_header)
1749 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1750 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1751 }
1752
1753 continue;
1754 }
1755
1756 if (NeedNonemptyTemplateHeader) {
1757 // In friend declarations we can have template-ids which don't
1758 // depend on the corresponding template parameter lists. But
1759 // assume that empty parameter lists are supposed to match this
1760 // template-id.
1761 if (IsFriend && T->isDependentType()) {
1762 if (ParamIdx < NumParamLists &&
1763 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1764 ExpectedTemplateParams = 0;
1765 else
1766 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001767 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001768
Douglas Gregorc8406492011-05-10 18:27:06 +00001769 if (ParamIdx < NumParamLists) {
1770 // Check the template parameter list, if we can.
1771 if (ExpectedTemplateParams &&
1772 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1773 ExpectedTemplateParams,
1774 true, TPL_TemplateMatch))
1775 Invalid = true;
1776
1777 if (!Invalid &&
1778 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1779 TPC_ClassTemplateMember))
1780 Invalid = true;
1781
1782 ++ParamIdx;
1783 continue;
1784 }
1785
1786 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1787 << T
1788 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1789 Invalid = true;
1790 continue;
1791 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001792 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001793
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001794 // If there were at least as many template-ids as there were template
1795 // parameter lists, then there are no template parameter lists remaining for
1796 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001797 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001798 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001800 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001801 if (ParamIdx < NumParamLists - 1) {
1802 bool HasAnyExplicitSpecHeader = false;
1803 bool AllExplicitSpecHeaders = true;
1804 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1805 if (ParamLists[I]->size() == 0)
1806 HasAnyExplicitSpecHeader = true;
1807 else
1808 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001809 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001810
1811 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1812 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1813 : diag::err_template_spec_extra_headers)
1814 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1815 ParamLists[NumParamLists - 2]->getRAngleLoc());
1816
1817 // If there was a specialization somewhere, such that 'template<>' is
1818 // not required, and there were any 'template<>' headers, note where the
1819 // specialization occurred.
1820 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1821 Diag(ExplicitSpecLoc,
1822 diag::note_explicit_template_spec_does_not_need_header)
1823 << NestedTypes.back();
1824
1825 // We have a template parameter list with no corresponding scope, which
1826 // means that the resulting template declaration can't be instantiated
1827 // properly (we'll end up with dependent nodes when we shouldn't).
1828 if (!AllExplicitSpecHeaders)
1829 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001830 }
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Douglas Gregor89b9f102011-06-06 15:22:55 +00001832 // C++ [temp.expl.spec]p16:
1833 // In an explicit specialization declaration for a member of a class
1834 // template or a member template that ap- pears in namespace scope, the
1835 // member template and some of its enclosing class templates may remain
1836 // unspecialized, except that the declaration shall not explicitly
1837 // specialize a class member template if its en- closing class templates
1838 // are not explicitly specialized as well.
1839 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1840 SawNonEmptyTemplateParameterList) {
1841 Diag(DeclLoc, diag::err_specialize_member_of_template)
1842 << ParamLists[ParamIdx]->getSourceRange();
1843 Invalid = true;
1844 IsExplicitSpecialization = false;
1845 return 0;
1846 }
1847
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001848 // Return the last template parameter list, which corresponds to the
1849 // entity being declared.
1850 return ParamLists[NumParamLists - 1];
1851}
1852
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001853void Sema::NoteAllFoundTemplates(TemplateName Name) {
1854 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1855 Diag(Template->getLocation(), diag::note_template_declared_here)
1856 << (isa<FunctionTemplateDecl>(Template)? 0
1857 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001858 : isa<TypeAliasTemplateDecl>(Template)? 2
1859 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001860 << Template->getDeclName();
1861 return;
1862 }
1863
1864 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1865 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1866 IEnd = OST->end();
1867 I != IEnd; ++I)
1868 Diag((*I)->getLocation(), diag::note_template_declared_here)
1869 << 0 << (*I)->getDeclName();
1870
1871 return;
1872 }
1873}
1874
1875
Douglas Gregor7532dc62009-03-30 22:58:21 +00001876QualType Sema::CheckTemplateIdType(TemplateName Name,
1877 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001878 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001879 DependentTemplateName *DTN
1880 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001881 if (DTN && DTN->isIdentifier())
1882 // When building a template-id where the template-name is dependent,
1883 // assume the template is a type template. Either our assumption is
1884 // correct, or the code is ill-formed and will be diagnosed when the
1885 // dependent name is substituted.
1886 return Context.getDependentTemplateSpecializationType(ETK_None,
1887 DTN->getQualifier(),
1888 DTN->getIdentifier(),
1889 TemplateArgs);
1890
Douglas Gregor7532dc62009-03-30 22:58:21 +00001891 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001892 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1893 // We might have a substituted template template parameter pack. If so,
1894 // build a template specialization type for it.
1895 if (Name.getAsSubstTemplateTemplateParmPack())
1896 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001897
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001898 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1899 << Name;
1900 NoteAllFoundTemplates(Name);
1901 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001902 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001903
Douglas Gregor40808ce2009-03-09 23:48:35 +00001904 // Check that the template argument list is well-formed for this
1905 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001907 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001908 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001909 return QualType();
1910
Douglas Gregor910f8002010-11-07 23:05:16 +00001911 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001912 "Converted template argument list is too short!");
1913
1914 QualType CanonType;
1915
Douglas Gregor561f8122011-07-01 01:22:09 +00001916 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001917 if (TypeAliasTemplateDecl *AliasTemplate
1918 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1919 // Find the canonical type for this type alias template specialization.
1920 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1921 if (Pattern->isInvalidDecl())
1922 return QualType();
1923
1924 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1925 Converted.data(), Converted.size());
1926
1927 // Only substitute for the innermost template argument list.
1928 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001929 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001930 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1931 for (unsigned I = 0; I < Depth; ++I)
1932 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001933
1934 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1935 CanonType = SubstType(Pattern->getUnderlyingType(),
1936 TemplateArgLists, AliasTemplate->getLocation(),
1937 AliasTemplate->getDeclName());
1938 if (CanonType.isNull())
1939 return QualType();
1940 } else if (Name.isDependent() ||
1941 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001942 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001943 // This class template specialization is a dependent
1944 // type. Therefore, its canonical type is another class template
1945 // specialization type that contains all of the converted
1946 // arguments in canonical form. This ensures that, e.g., A<T> and
1947 // A<T, T> have identical types when A is declared as:
1948 //
1949 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001950 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001951 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001952 Converted.data(),
1953 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Douglas Gregor1275ae02009-07-28 23:00:59 +00001955 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001956 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001957 // In the future, we need to teach getTemplateSpecializationType to only
1958 // build the canonical type and return that to us.
1959 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001960
1961 // This might work out to be a current instantiation, in which
1962 // case the canonical type needs to be the InjectedClassNameType.
1963 //
1964 // TODO: in theory this could be a simple hashtable lookup; most
1965 // changes to CurContext don't change the set of current
1966 // instantiations.
1967 if (isa<ClassTemplateDecl>(Template)) {
1968 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1969 // If we get out to a namespace, we're done.
1970 if (Ctx->isFileContext()) break;
1971
1972 // If this isn't a record, keep looking.
1973 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1974 if (!Record) continue;
1975
1976 // Look for one of the two cases with InjectedClassNameTypes
1977 // and check whether it's the same template.
1978 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1979 !Record->getDescribedClassTemplate())
1980 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001981
John McCall31f17ec2010-04-27 00:57:59 +00001982 // Fetch the injected class name type and check whether its
1983 // injected type is equal to the type we just built.
1984 QualType ICNT = Context.getTypeDeclType(Record);
1985 QualType Injected = cast<InjectedClassNameType>(ICNT)
1986 ->getInjectedSpecializationType();
1987
1988 if (CanonType != Injected->getCanonicalTypeInternal())
1989 continue;
1990
1991 // If so, the canonical type of this TST is the injected
1992 // class name type of the record we just found.
1993 assert(ICNT.isCanonical());
1994 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001995 break;
1996 }
1997 }
Mike Stump1eb44332009-09-09 15:08:12 +00001998 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001999 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002000 // Find the class template specialization declaration that
2001 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002002 void *InsertPos = 0;
2003 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002004 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002005 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002006 if (!Decl) {
2007 // This is the first time we have referenced this class template
2008 // specialization. Create the canonical declaration and add it to
2009 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002010 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002011 ClassTemplate->getTemplatedDecl()->getTagKind(),
2012 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002013 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002014 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002015 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002016 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002017 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002018 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002019 Decl->setLexicalDeclContext(CurContext);
2020 }
2021
2022 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002023 assert(isa<RecordType>(CanonType) &&
2024 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002025 }
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Douglas Gregor40808ce2009-03-09 23:48:35 +00002027 // Build the fully-sugared type for this class template
2028 // specialization, which refers back to the class template
2029 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002030 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002031}
2032
John McCallf312b1e2010-08-26 23:41:50 +00002033TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002034Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2035 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002036 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002037 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002038 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002039 if (SS.isInvalid())
2040 return true;
2041
Douglas Gregor7532dc62009-03-30 22:58:21 +00002042 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002043
Douglas Gregor40808ce2009-03-09 23:48:35 +00002044 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002045 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002046 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002047
Douglas Gregora88f09f2011-02-28 17:23:35 +00002048 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2049 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2050 DTN->getQualifier(),
2051 DTN->getIdentifier(),
2052 TemplateArgs);
2053
2054 // Build type-source information.
2055 TypeLocBuilder TLB;
2056 DependentTemplateSpecializationTypeLoc SpecTL
2057 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002058 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002059 SpecTL.setNameLoc(TemplateLoc);
2060 SpecTL.setLAngleLoc(LAngleLoc);
2061 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002062 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002063 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2064 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2065 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2066 }
2067
John McCalld5532b62009-11-23 01:53:49 +00002068 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002069 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002070
2071 if (Result.isNull())
2072 return true;
2073
Douglas Gregor059101f2011-03-02 00:47:37 +00002074 // Build type-source information.
2075 TypeLocBuilder TLB;
2076 TemplateSpecializationTypeLoc SpecTL
2077 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2078 SpecTL.setTemplateNameLoc(TemplateLoc);
2079 SpecTL.setLAngleLoc(LAngleLoc);
2080 SpecTL.setRAngleLoc(RAngleLoc);
2081 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2082 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002083
Douglas Gregor059101f2011-03-02 00:47:37 +00002084 if (SS.isNotEmpty()) {
2085 // Create an elaborated-type-specifier containing the nested-name-specifier.
2086 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2087 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2088 ElabTL.setKeywordLoc(SourceLocation());
2089 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2090 }
2091
2092 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002093}
John McCallf1bbbb42009-09-04 01:14:41 +00002094
Douglas Gregor059101f2011-03-02 00:47:37 +00002095TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002096 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002097 SourceLocation TagLoc,
2098 CXXScopeSpec &SS,
2099 TemplateTy TemplateD,
2100 SourceLocation TemplateLoc,
2101 SourceLocation LAngleLoc,
2102 ASTTemplateArgsPtr TemplateArgsIn,
2103 SourceLocation RAngleLoc) {
2104 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2105
2106 // Translate the parser's template argument list in our AST format.
2107 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2108 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2109
2110 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002111 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002112 ElaboratedTypeKeyword Keyword
2113 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Douglas Gregor059101f2011-03-02 00:47:37 +00002115 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2116 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2117 DTN->getQualifier(),
2118 DTN->getIdentifier(),
2119 TemplateArgs);
2120
2121 // Build type-source information.
2122 TypeLocBuilder TLB;
2123 DependentTemplateSpecializationTypeLoc SpecTL
2124 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2125 SpecTL.setKeywordLoc(TagLoc);
2126 SpecTL.setNameLoc(TemplateLoc);
2127 SpecTL.setLAngleLoc(LAngleLoc);
2128 SpecTL.setRAngleLoc(RAngleLoc);
2129 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2130 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2131 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2132 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2133 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002134
2135 if (TypeAliasTemplateDecl *TAT =
2136 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2137 // C++0x [dcl.type.elab]p2:
2138 // If the identifier resolves to a typedef-name or the simple-template-id
2139 // resolves to an alias template specialization, the
2140 // elaborated-type-specifier is ill-formed.
2141 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2142 Diag(TAT->getLocation(), diag::note_declared_at);
2143 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002144
2145 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2146 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002147 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002148
2149 // Check the tag kind
2150 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002151 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002152
John McCall6b2becf2009-09-08 17:47:29 +00002153 IdentifierInfo *Id = D->getIdentifier();
2154 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002155
Richard Trieubbf34c02011-06-10 03:11:26 +00002156 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2157 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002158 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002159 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002160 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002161 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002162 }
2163 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002164
2165 // Provide source-location information for the template specialization.
2166 TypeLocBuilder TLB;
2167 TemplateSpecializationTypeLoc SpecTL
2168 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2169 SpecTL.setTemplateNameLoc(TemplateLoc);
2170 SpecTL.setLAngleLoc(LAngleLoc);
2171 SpecTL.setRAngleLoc(RAngleLoc);
2172 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2173 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002174
Douglas Gregor059101f2011-03-02 00:47:37 +00002175 // Construct an elaborated type containing the nested-name-specifier (if any)
2176 // and keyword.
2177 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2178 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2179 ElabTL.setKeywordLoc(TagLoc);
2180 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2181 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002182}
2183
John McCall60d7b3a2010-08-24 06:29:42 +00002184ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002185 LookupResult &R,
2186 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002187 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002188 // FIXME: Can we do any checking at this point? I guess we could check the
2189 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002190 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002191 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002192 // foo<int> could identify a single function unambiguously
2193 // This approach does NOT work, since f<int>(1);
2194 // gets resolved prior to resorting to overload resolution
2195 // i.e., template<class T> void f(double);
2196 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002197
2198 // These should be filtered out by our callers.
2199 assert(!R.empty() && "empty lookup results when building templateid");
2200 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2201
John McCallc373d482010-01-27 01:50:18 +00002202 // We don't want lookup warnings at this point.
2203 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002204
John McCallf7a1a742009-11-24 19:00:30 +00002205 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002206 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002207 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002208 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002209 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002210 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002211
2212 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002213}
2214
John McCallf7a1a742009-11-24 19:00:30 +00002215// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002216ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002217Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002218 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002219 const TemplateArgumentListInfo &TemplateArgs) {
2220 DeclContext *DC;
2221 if (!(DC = computeDeclContext(SS, false)) ||
2222 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002223 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002224 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002226 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002227 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002228 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2229 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002230
John McCallf7a1a742009-11-24 19:00:30 +00002231 if (R.isAmbiguous())
2232 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002233
John McCallf7a1a742009-11-24 19:00:30 +00002234 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002235 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2236 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002237 return ExprError();
2238 }
2239
2240 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002241 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2242 << (NestedNameSpecifier*) SS.getScopeRep()
2243 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002244 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2245 return ExprError();
2246 }
2247
2248 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002249}
2250
Douglas Gregorc45c2322009-03-31 00:43:58 +00002251/// \brief Form a dependent template name.
2252///
2253/// This action forms a dependent template name given the template
2254/// name and its (presumably dependent) scope specifier. For
2255/// example, given "MetaFun::template apply", the scope specifier \p
2256/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2257/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002258TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002259 SourceLocation TemplateKWLoc,
2260 CXXScopeSpec &SS,
2261 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002262 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002263 bool EnteringContext,
2264 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002265 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2266 Diag(TemplateKWLoc,
2267 getLangOptions().CPlusPlus0x ?
2268 diag::warn_cxx98_compat_template_outside_of_template :
2269 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002270 << FixItHint::CreateRemoval(TemplateKWLoc);
2271
Douglas Gregor0707bc52010-01-19 16:01:07 +00002272 DeclContext *LookupCtx = 0;
2273 if (SS.isSet())
2274 LookupCtx = computeDeclContext(SS, EnteringContext);
2275 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002276 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002277 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002278 // C++0x [temp.names]p5:
2279 // If a name prefixed by the keyword template is not the name of
2280 // a template, the program is ill-formed. [Note: the keyword
2281 // template may not be applied to non-template members of class
2282 // templates. -end note ] [ Note: as is the case with the
2283 // typename prefix, the template prefix is allowed in cases
2284 // where it is not strictly necessary; i.e., when the
2285 // nested-name-specifier or the expression on the left of the ->
2286 // or . is not dependent on a template-parameter, or the use
2287 // does not appear in the scope of a template. -end note]
2288 //
2289 // Note: C++03 was more strict here, because it banned the use of
2290 // the "template" keyword prior to a template-name that was not a
2291 // dependent name. C++ DR468 relaxed this requirement (the
2292 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002293 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002294 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002295 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2296 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002297 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002298 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2299 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002300 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2301 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002302 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002303 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002304 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002305 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002306 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002307 << Name.getSourceRange()
2308 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002309 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002310 } else {
2311 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002312 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002313 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002314 }
2315
Mike Stump1eb44332009-09-09 15:08:12 +00002316 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002317 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002318
Douglas Gregor014e88d2009-11-03 23:16:33 +00002319 switch (Name.getKind()) {
2320 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002321 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002322 Name.Identifier));
2323 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002324
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002325 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002326 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002327 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002328 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002329
2330 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002331 llvm_unreachable(
2332 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002333
Douglas Gregor014e88d2009-11-03 23:16:33 +00002334 default:
2335 break;
2336 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002337
2338 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002339 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002340 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002341 << Name.getSourceRange()
2342 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002343 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002344}
2345
Mike Stump1eb44332009-09-09 15:08:12 +00002346bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002347 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002348 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002349 const TemplateArgument &Arg = AL.getArgument();
2350
Anders Carlsson436b1562009-06-13 00:33:33 +00002351 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002352 switch(Arg.getKind()) {
2353 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002354 // C++ [temp.arg.type]p1:
2355 // A template-argument for a template-parameter which is a
2356 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002357 break;
2358 case TemplateArgument::Template: {
2359 // We have a template type parameter but the template argument
2360 // is a template without any arguments.
2361 SourceRange SR = AL.getSourceRange();
2362 TemplateName Name = Arg.getAsTemplate();
2363 Diag(SR.getBegin(), diag::err_template_missing_args)
2364 << Name << SR;
2365 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2366 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002367
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002368 return true;
2369 }
2370 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002371 // We have a template type parameter but the template argument
2372 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002373 SourceRange SR = AL.getSourceRange();
2374 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002375 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Anders Carlsson436b1562009-06-13 00:33:33 +00002377 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002378 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002379 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002380
John McCalla93c9342009-12-07 02:54:59 +00002381 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002382 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Anders Carlsson436b1562009-06-13 00:33:33 +00002384 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002385 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2386
2387 // Objective-C ARC:
2388 // If an explicitly-specified template argument type is a lifetime type
2389 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2390 if (getLangOptions().ObjCAutoRefCount &&
2391 ArgType->isObjCLifetimeType() &&
2392 !ArgType.getObjCLifetime()) {
2393 Qualifiers Qs;
2394 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2395 ArgType = Context.getQualifiedType(ArgType, Qs);
2396 }
2397
2398 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002399 return false;
2400}
2401
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002402/// \brief Substitute template arguments into the default template argument for
2403/// the given template type parameter.
2404///
2405/// \param SemaRef the semantic analysis object for which we are performing
2406/// the substitution.
2407///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002408/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002409/// for.
2410///
2411/// \param TemplateLoc the location of the template name that started the
2412/// template-id we are checking.
2413///
2414/// \param RAngleLoc the location of the right angle bracket ('>') that
2415/// terminates the template-id.
2416///
2417/// \param Param the template template parameter whose default we are
2418/// substituting into.
2419///
2420/// \param Converted the list of template arguments provided for template
2421/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002422/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002423static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002424SubstDefaultTemplateArgument(Sema &SemaRef,
2425 TemplateDecl *Template,
2426 SourceLocation TemplateLoc,
2427 SourceLocation RAngleLoc,
2428 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002429 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002430 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002431
2432 // If the argument type is dependent, instantiate it now based
2433 // on the previously-computed template arguments.
2434 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002436 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002437
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002438 MultiLevelTemplateArgumentList AllTemplateArgs
2439 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2440
2441 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002442 Template, Converted.data(),
2443 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002444 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002445
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002446 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2447 Param->getDefaultArgumentLoc(),
2448 Param->getDeclName());
2449 }
2450
2451 return ArgType;
2452}
2453
2454/// \brief Substitute template arguments into the default template argument for
2455/// the given non-type template parameter.
2456///
2457/// \param SemaRef the semantic analysis object for which we are performing
2458/// the substitution.
2459///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002460/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002461/// for.
2462///
2463/// \param TemplateLoc the location of the template name that started the
2464/// template-id we are checking.
2465///
2466/// \param RAngleLoc the location of the right angle bracket ('>') that
2467/// terminates the template-id.
2468///
Douglas Gregor788cd062009-11-11 01:00:40 +00002469/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002470/// substituting into.
2471///
2472/// \param Converted the list of template arguments provided for template
2473/// parameters that precede \p Param in the template parameter list.
2474///
2475/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002476static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002477SubstDefaultTemplateArgument(Sema &SemaRef,
2478 TemplateDecl *Template,
2479 SourceLocation TemplateLoc,
2480 SourceLocation RAngleLoc,
2481 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002482 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002483 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002484 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002485
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002486 MultiLevelTemplateArgumentList AllTemplateArgs
2487 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002489 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002490 Template, Converted.data(),
2491 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002492 SourceRange(TemplateLoc, RAngleLoc));
2493
2494 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2495}
2496
Douglas Gregor788cd062009-11-11 01:00:40 +00002497/// \brief Substitute template arguments into the default template argument for
2498/// the given template template parameter.
2499///
2500/// \param SemaRef the semantic analysis object for which we are performing
2501/// the substitution.
2502///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002503/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002504/// for.
2505///
2506/// \param TemplateLoc the location of the template name that started the
2507/// template-id we are checking.
2508///
2509/// \param RAngleLoc the location of the right angle bracket ('>') that
2510/// terminates the template-id.
2511///
2512/// \param Param the template template parameter whose default we are
2513/// substituting into.
2514///
2515/// \param Converted the list of template arguments provided for template
2516/// parameters that precede \p Param in the template parameter list.
2517///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002518/// \param QualifierLoc Will be set to the nested-name-specifier (with
2519/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002520///
Douglas Gregor788cd062009-11-11 01:00:40 +00002521/// \returns the substituted template argument, or NULL if an error occurred.
2522static TemplateName
2523SubstDefaultTemplateArgument(Sema &SemaRef,
2524 TemplateDecl *Template,
2525 SourceLocation TemplateLoc,
2526 SourceLocation RAngleLoc,
2527 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002528 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002529 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002531 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002532
Douglas Gregor788cd062009-11-11 01:00:40 +00002533 MultiLevelTemplateArgumentList AllTemplateArgs
2534 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregor788cd062009-11-11 01:00:40 +00002536 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002537 Template, Converted.data(),
2538 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002539 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002541 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002542 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002543 if (QualifierLoc) {
2544 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2545 AllTemplateArgs);
2546 if (!QualifierLoc)
2547 return TemplateName();
2548 }
2549
Douglas Gregor1d752d72011-03-02 18:46:51 +00002550 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002551 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002553 AllTemplateArgs);
2554}
2555
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002556/// \brief If the given template parameter has a default template
2557/// argument, substitute into that default template argument and
2558/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002560Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2561 SourceLocation TemplateLoc,
2562 SourceLocation RAngleLoc,
2563 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002564 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002565 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002566 if (!TypeParm->hasDefaultArgument())
2567 return TemplateArgumentLoc();
2568
John McCalla93c9342009-12-07 02:54:59 +00002569 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002570 TemplateLoc,
2571 RAngleLoc,
2572 TypeParm,
2573 Converted);
2574 if (DI)
2575 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2576
2577 return TemplateArgumentLoc();
2578 }
2579
2580 if (NonTypeTemplateParmDecl *NonTypeParm
2581 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2582 if (!NonTypeParm->hasDefaultArgument())
2583 return TemplateArgumentLoc();
2584
John McCall60d7b3a2010-08-24 06:29:42 +00002585 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002586 TemplateLoc,
2587 RAngleLoc,
2588 NonTypeParm,
2589 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002590 if (Arg.isInvalid())
2591 return TemplateArgumentLoc();
2592
2593 Expr *ArgE = Arg.takeAs<Expr>();
2594 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2595 }
2596
2597 TemplateTemplateParmDecl *TempTempParm
2598 = cast<TemplateTemplateParmDecl>(Param);
2599 if (!TempTempParm->hasDefaultArgument())
2600 return TemplateArgumentLoc();
2601
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002602
Douglas Gregor1d752d72011-03-02 18:46:51 +00002603 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002604 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002605 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002606 RAngleLoc,
2607 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002608 Converted,
2609 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002610 if (TName.isNull())
2611 return TemplateArgumentLoc();
2612
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002613 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002614 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002615 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2616}
2617
Douglas Gregore7526412009-11-11 19:31:23 +00002618/// \brief Check that the given template argument corresponds to the given
2619/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002620///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002621/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002622/// checked.
2623///
2624/// \param Arg The template argument.
2625///
2626/// \param Template The template in which the template argument resides.
2627///
2628/// \param TemplateLoc The location of the template name for the template
2629/// whose argument list we're matching.
2630///
2631/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2632/// the template argument list.
2633///
2634/// \param ArgumentPackIndex The index into the argument pack where this
2635/// argument will be placed. Only valid if the parameter is a parameter pack.
2636///
2637/// \param Converted The checked, converted argument will be added to the
2638/// end of this small vector.
2639///
2640/// \param CTAK Describes how we arrived at this particular template argument:
2641/// explicitly written, deduced, etc.
2642///
2643/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002644bool Sema::CheckTemplateArgument(NamedDecl *Param,
2645 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002646 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002647 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002648 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002649 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002650 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002651 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002652 // Check template type parameters.
2653 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002654 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002655
Douglas Gregord9e15302009-11-11 19:41:09 +00002656 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002657 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002658 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002659 // with the template arguments we've seen thus far. But if the
2660 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002661 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002662 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2663 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002665 if (NTTPType->isDependentType() &&
2666 !isa<TemplateTemplateParmDecl>(Template) &&
2667 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002668 // Do substitution on the type of the non-type template parameter.
2669 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002670 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002671 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002672
2673 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002674 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002675 NTTPType = SubstType(NTTPType,
2676 MultiLevelTemplateArgumentList(TemplateArgs),
2677 NTTP->getLocation(),
2678 NTTP->getDeclName());
2679 // If that worked, check the non-type template parameter type
2680 // for validity.
2681 if (!NTTPType.isNull())
2682 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2683 NTTP->getLocation());
2684 if (NTTPType.isNull())
2685 return true;
2686 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687
Douglas Gregore7526412009-11-11 19:31:23 +00002688 switch (Arg.getArgument().getKind()) {
2689 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002690 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002691
Douglas Gregore7526412009-11-11 19:31:23 +00002692 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002693 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002694 ExprResult Res =
2695 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2696 Result, CTAK);
2697 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002698 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699
Douglas Gregor910f8002010-11-07 23:05:16 +00002700 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002701 break;
2702 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002703
Douglas Gregore7526412009-11-11 19:31:23 +00002704 case TemplateArgument::Declaration:
2705 case TemplateArgument::Integral:
2706 // We've already checked this template argument, so just copy
2707 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002708 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002709 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710
Douglas Gregore7526412009-11-11 19:31:23 +00002711 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002712 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002713 // We were given a template template argument. It may not be ill-formed;
2714 // see below.
2715 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002716 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2717 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002718 // We have a template argument such as \c T::template X, which we
2719 // parsed as a template template argument. However, since we now
2720 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002721 // template name into an expression.
2722
2723 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2724 Arg.getTemplateNameLoc());
2725
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002726 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002727 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002728 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002729 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002730 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731
Douglas Gregora7fc9012011-01-05 18:58:31 +00002732 // If we parsed the template argument as a pack expansion, create a
2733 // pack expansion expression.
2734 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002735 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2736 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002737 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739
Douglas Gregore7526412009-11-11 19:31:23 +00002740 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002741 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2742 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002743 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregor910f8002010-11-07 23:05:16 +00002745 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002746 break;
2747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregore7526412009-11-11 19:31:23 +00002749 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002750 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002751 // therefore cannot be a non-type template argument.
2752 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2753 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002754
Douglas Gregore7526412009-11-11 19:31:23 +00002755 Diag(Param->getLocation(), diag::note_template_param_here);
2756 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757
Douglas Gregore7526412009-11-11 19:31:23 +00002758 case TemplateArgument::Type: {
2759 // We have a non-type template parameter but the template
2760 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761
Douglas Gregore7526412009-11-11 19:31:23 +00002762 // C++ [temp.arg]p2:
2763 // In a template-argument, an ambiguity between a type-id and
2764 // an expression is resolved to a type-id, regardless of the
2765 // form of the corresponding template-parameter.
2766 //
2767 // We warn specifically about this case, since it can be rather
2768 // confusing for users.
2769 QualType T = Arg.getArgument().getAsType();
2770 SourceRange SR = Arg.getSourceRange();
2771 if (T->isFunctionType())
2772 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2773 else
2774 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2775 Diag(Param->getLocation(), diag::note_template_param_here);
2776 return true;
2777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002778
Douglas Gregore7526412009-11-11 19:31:23 +00002779 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002780 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002781 break;
2782 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
Douglas Gregore7526412009-11-11 19:31:23 +00002784 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785 }
2786
2787
Douglas Gregore7526412009-11-11 19:31:23 +00002788 // Check template template parameters.
2789 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790
Douglas Gregore7526412009-11-11 19:31:23 +00002791 // Substitute into the template parameter list of the template
2792 // template parameter, since previously-supplied template arguments
2793 // may appear within the template template parameter.
2794 {
2795 // Set up a template instantiation context.
2796 LocalInstantiationScope Scope(*this);
2797 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002798 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002799 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002800
2801 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002802 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002803 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002805 MultiLevelTemplateArgumentList(TemplateArgs)));
2806 if (!TempParm)
2807 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002808 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809
Douglas Gregore7526412009-11-11 19:31:23 +00002810 switch (Arg.getArgument().getKind()) {
2811 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002812 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002813
Douglas Gregore7526412009-11-11 19:31:23 +00002814 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002815 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002816 if (CheckTemplateArgument(TempParm, Arg))
2817 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818
Douglas Gregor910f8002010-11-07 23:05:16 +00002819 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002820 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002821
Douglas Gregore7526412009-11-11 19:31:23 +00002822 case TemplateArgument::Expression:
2823 case TemplateArgument::Type:
2824 // We have a template template parameter but the template
2825 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002826 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2827 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002828 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002829
Douglas Gregore7526412009-11-11 19:31:23 +00002830 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002831 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002832 "Declaration argument with template template parameter");
2833 break;
2834 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002835 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002836 "Integral argument with template template parameter");
2837 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838
Douglas Gregore7526412009-11-11 19:31:23 +00002839 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002840 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002841 break;
2842 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregore7526412009-11-11 19:31:23 +00002844 return false;
2845}
2846
Douglas Gregorc15cb382009-02-09 23:23:08 +00002847/// \brief Check that the given template argument list is well-formed
2848/// for specializing the given template.
2849bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2850 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002851 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002852 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002853 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002854 TemplateParameterList *Params = Template->getTemplateParameters();
2855 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002856 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002857 bool Invalid = false;
2858
John McCalld5532b62009-11-23 01:53:49 +00002859 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2860
Mike Stump1eb44332009-09-09 15:08:12 +00002861 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002862 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002864 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002865 (NumArgs < Params->getMinRequiredArguments() &&
2866 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002867 // FIXME: point at either the first arg beyond what we can handle,
2868 // or the '>', depending on whether we have too many or too few
2869 // arguments.
2870 SourceRange Range;
2871 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002872 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002873 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2874 << (NumArgs > NumParams)
2875 << (isa<ClassTemplateDecl>(Template)? 0 :
2876 isa<FunctionTemplateDecl>(Template)? 1 :
2877 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2878 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002879 Diag(Template->getLocation(), diag::note_template_decl_here)
2880 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002881 Invalid = true;
2882 }
Mike Stump1eb44332009-09-09 15:08:12 +00002883
2884 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002885 // [...] The type and form of each template-argument specified in
2886 // a template-id shall match the type and form specified for the
2887 // corresponding parameter declared by the template in its
2888 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002889 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002890 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002891 TemplateParameterList::iterator Param = Params->begin(),
2892 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002893 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002894 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002895 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002896 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002897 // If we have an expanded parameter pack, make sure we don't have too
2898 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002899 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002900 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002901 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002902 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2903 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2904 << true
2905 << (isa<ClassTemplateDecl>(Template)? 0 :
2906 isa<FunctionTemplateDecl>(Template)? 1 :
2907 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2908 << Template;
2909 Diag(Template->getLocation(), diag::note_template_decl_here)
2910 << Params->getSourceRange();
2911 return true;
2912 }
2913 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002914
Douglas Gregorf35f8282009-11-11 21:54:23 +00002915 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002916 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2917 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002918 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002919 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002920
Douglas Gregor14be16b2010-12-20 16:57:52 +00002921 if ((*Param)->isTemplateParameterPack()) {
2922 // The template parameter was a template parameter pack, so take the
2923 // deduced argument and place it on the argument pack. Note that we
2924 // stay on the same template parameter so that we can deduce more
2925 // arguments.
2926 ArgumentPack.push_back(Converted.back());
2927 Converted.pop_back();
2928 } else {
2929 // Move to the next template parameter.
2930 ++Param;
2931 }
2932 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002933 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002934 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002935
Douglas Gregor8735b292011-06-03 02:59:40 +00002936 // If we're checking a partial template argument list, we're done.
2937 if (PartialTemplateArgs) {
2938 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2939 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2940 ArgumentPack.data(),
2941 ArgumentPack.size()));
2942
2943 return Invalid;
2944 }
2945
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002946 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002947 // arguments, just break out now and we'll fill in the argument pack below.
2948 if ((*Param)->isTemplateParameterPack())
2949 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002950
Douglas Gregorf35f8282009-11-11 21:54:23 +00002951 // We have a default template argument that we will use.
2952 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002953
Douglas Gregorf35f8282009-11-11 21:54:23 +00002954 // Retrieve the default template argument from the template
2955 // parameter. For each kind of template parameter, we substitute the
2956 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002957 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002958 // the default argument.
2959 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2960 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002961 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002962 break;
2963 }
2964
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002965 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002966 Template,
2967 TemplateLoc,
2968 RAngleLoc,
2969 TTP,
2970 Converted);
2971 if (!ArgType)
2972 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002973
Douglas Gregorf35f8282009-11-11 21:54:23 +00002974 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2975 ArgType);
2976 } else if (NonTypeTemplateParmDecl *NTTP
2977 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2978 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002979 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002980 break;
2981 }
2982
John McCall60d7b3a2010-08-24 06:29:42 +00002983 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002984 TemplateLoc,
2985 RAngleLoc,
2986 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002987 Converted);
2988 if (E.isInvalid())
2989 return true;
2990
2991 Expr *Ex = E.takeAs<Expr>();
2992 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2993 } else {
2994 TemplateTemplateParmDecl *TempParm
2995 = cast<TemplateTemplateParmDecl>(*Param);
2996
2997 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002998 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002999 break;
3000 }
3001
Douglas Gregor1d752d72011-03-02 18:46:51 +00003002 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003003 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003004 TemplateLoc,
3005 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003006 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003007 Converted,
3008 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003009 if (Name.isNull())
3010 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003011
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003012 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3013 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003014 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003015
Douglas Gregorf35f8282009-11-11 21:54:23 +00003016 // Introduce an instantiation record that describes where we are using
3017 // the default template argument.
3018 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003019 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020 SourceRange(TemplateLoc, RAngleLoc));
3021
Douglas Gregorf35f8282009-11-11 21:54:23 +00003022 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003023 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003024 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003025 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
Douglas Gregor67714232011-03-03 02:41:12 +00003027 // Core issue 150 (assumed resolution): if this is a template template
3028 // parameter, keep track of the default template arguments from the
3029 // template definition.
3030 if (isTemplateTemplateParameter)
3031 TemplateArgs.addArgument(Arg);
3032
Douglas Gregor14be16b2010-12-20 16:57:52 +00003033 // Move to the next template parameter and argument.
3034 ++Param;
3035 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003036 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003037
Douglas Gregor14be16b2010-12-20 16:57:52 +00003038 // Form argument packs for each of the parameter packs remaining.
3039 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003040 // If we're checking a partial list of template arguments, don't fill
3041 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
3043 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003044 if (!HasParameterPack)
3045 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003046 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003047 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003048 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003049 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3050 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003051 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003052 ArgumentPack.clear();
3053 }
3054 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003055
Douglas Gregor14be16b2010-12-20 16:57:52 +00003056 ++Param;
3057 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003058
Douglas Gregorc15cb382009-02-09 23:23:08 +00003059 return Invalid;
3060}
3061
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003062namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063 class UnnamedLocalNoLinkageFinder
3064 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003065 {
3066 Sema &S;
3067 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003068
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003069 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003070
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003071 public:
3072 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3073
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003074 bool Visit(QualType T) {
3075 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003077
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003078#define TYPE(Class, Parent) \
3079 bool Visit##Class##Type(const Class##Type *);
3080#define ABSTRACT_TYPE(Class, Parent) \
3081 bool Visit##Class##Type(const Class##Type *) { return false; }
3082#define NON_CANONICAL_TYPE(Class, Parent) \
3083 bool Visit##Class##Type(const Class##Type *) { return false; }
3084#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003085
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003086 bool VisitTagDecl(const TagDecl *Tag);
3087 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3088 };
3089}
3090
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003091bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003092 return false;
3093}
3094
3095bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3096 return Visit(T->getElementType());
3097}
3098
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003099bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003100 return Visit(T->getPointeeType());
3101}
3102
3103bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003105 return Visit(T->getPointeeType());
3106}
3107
3108bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003109 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003110 return Visit(T->getPointeeType());
3111}
3112
3113bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003114 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003115 return Visit(T->getPointeeType());
3116}
3117
3118bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003119 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003120 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3121}
3122
3123bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003124 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003125 return Visit(T->getElementType());
3126}
3127
3128bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003129 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003130 return Visit(T->getElementType());
3131}
3132
3133bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003134 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003135 return Visit(T->getElementType());
3136}
3137
3138bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003139 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003140 return Visit(T->getElementType());
3141}
3142
3143bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003144 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003145 return Visit(T->getElementType());
3146}
3147
3148bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3149 return Visit(T->getElementType());
3150}
3151
3152bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3153 return Visit(T->getElementType());
3154}
3155
3156bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3157 const FunctionProtoType* T) {
3158 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003159 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003160 A != AEnd; ++A) {
3161 if (Visit(*A))
3162 return true;
3163 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003164
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003165 return Visit(T->getResultType());
3166}
3167
3168bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3169 const FunctionNoProtoType* T) {
3170 return Visit(T->getResultType());
3171}
3172
3173bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3174 const UnresolvedUsingType*) {
3175 return false;
3176}
3177
3178bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3179 return false;
3180}
3181
3182bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3183 return Visit(T->getUnderlyingType());
3184}
3185
3186bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3187 return false;
3188}
3189
Sean Huntca63c202011-05-24 22:41:36 +00003190bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3191 const UnaryTransformType*) {
3192 return false;
3193}
3194
Richard Smith34b41d92011-02-20 03:19:35 +00003195bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3196 return Visit(T->getDeducedType());
3197}
3198
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003199bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3200 return VisitTagDecl(T->getDecl());
3201}
3202
3203bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3204 return VisitTagDecl(T->getDecl());
3205}
3206
3207bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3208 const TemplateTypeParmType*) {
3209 return false;
3210}
3211
Douglas Gregorc3069d62011-01-14 02:55:32 +00003212bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3213 const SubstTemplateTypeParmPackType *) {
3214 return false;
3215}
3216
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003217bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3218 const TemplateSpecializationType*) {
3219 return false;
3220}
3221
3222bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3223 const InjectedClassNameType* T) {
3224 return VisitTagDecl(T->getDecl());
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3228 const DependentNameType* T) {
3229 return VisitNestedNameSpecifier(T->getQualifier());
3230}
3231
3232bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3233 const DependentTemplateSpecializationType* T) {
3234 return VisitNestedNameSpecifier(T->getQualifier());
3235}
3236
Douglas Gregor7536dd52010-12-20 02:24:11 +00003237bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3238 const PackExpansionType* T) {
3239 return Visit(T->getPattern());
3240}
3241
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003242bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3243 return false;
3244}
3245
3246bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3247 const ObjCInterfaceType *) {
3248 return false;
3249}
3250
3251bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3252 const ObjCObjectPointerType *) {
3253 return false;
3254}
3255
Eli Friedmanb001de72011-10-06 23:00:33 +00003256bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3257 return Visit(T->getValueType());
3258}
3259
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003260bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3261 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003262 S.Diag(SR.getBegin(),
3263 S.getLangOptions().CPlusPlus0x ?
3264 diag::warn_cxx98_compat_template_arg_local_type :
3265 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003266 << S.Context.getTypeDeclType(Tag) << SR;
3267 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003268 }
3269
Richard Smith162e1c12011-04-15 14:24:37 +00003270 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003271 S.Diag(SR.getBegin(),
3272 S.getLangOptions().CPlusPlus0x ?
3273 diag::warn_cxx98_compat_template_arg_unnamed_type :
3274 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003275 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3276 return true;
3277 }
3278
3279 return false;
3280}
3281
3282bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3283 NestedNameSpecifier *NNS) {
3284 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3285 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003286
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003287 switch (NNS->getKind()) {
3288 case NestedNameSpecifier::Identifier:
3289 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003290 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003291 case NestedNameSpecifier::Global:
3292 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003293
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003294 case NestedNameSpecifier::TypeSpec:
3295 case NestedNameSpecifier::TypeSpecWithTemplate:
3296 return Visit(QualType(NNS->getAsType(), 0));
3297 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003298 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003299}
3300
3301
Douglas Gregorc15cb382009-02-09 23:23:08 +00003302/// \brief Check a template argument against its corresponding
3303/// template type parameter.
3304///
3305/// This routine implements the semantics of C++ [temp.arg.type]. It
3306/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003307bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003308 TypeSourceInfo *ArgInfo) {
3309 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003310 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003311 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003312
3313 if (Arg->isVariablyModifiedType()) {
3314 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003315 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003316 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003317 }
3318
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003319 // C++03 [temp.arg.type]p2:
3320 // A local type, a type with no linkage, an unnamed type or a type
3321 // compounded from any of these types shall not be used as a
3322 // template-argument for a template type-parameter.
3323 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003324 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003325 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003326 if (LangOpts.CPlusPlus0x ?
3327 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3328 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3329 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3330 SR.getBegin()) != DiagnosticsEngine::Ignored :
3331 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003332 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3333 (void)Finder.Visit(Context.getCanonicalType(Arg));
3334 }
3335
Douglas Gregorc15cb382009-02-09 23:23:08 +00003336 return false;
3337}
3338
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003339/// \brief Checks whether the given template argument is the address
3340/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003341static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003342CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3343 NonTypeTemplateParmDecl *Param,
3344 QualType ParamType,
3345 Expr *ArgIn,
3346 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003347 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003348 Expr *Arg = ArgIn;
3349 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350
3351 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003352 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003353
3354 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003355 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003356 // A template-argument for a non-type, non-template
3357 // template-parameter shall be one of: [...]
3358 //
3359 // -- the address of an object or function with external
3360 // linkage, including function templates and function
3361 // template-ids but excluding non-static class members,
3362 // expressed as & id-expression where the & is optional if
3363 // the name refers to a function or array, or if the
3364 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003365
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003366 // In C++98/03 mode, give an extension warning on any extra parentheses.
3367 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3368 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003369 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003370 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003371 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003372 S.getLangOptions().CPlusPlus0x ?
3373 diag::warn_cxx98_compat_template_arg_extra_parens :
3374 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003375 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003376 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003377 }
3378
3379 Arg = Parens->getSubExpr();
3380 }
3381
John McCall91a57552011-07-15 05:09:51 +00003382 while (SubstNonTypeTemplateParmExpr *subst =
3383 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3384 Arg = subst->getReplacement()->IgnoreImpCasts();
3385
Douglas Gregorb7a09262010-04-01 18:32:35 +00003386 bool AddressTaken = false;
3387 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003389 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003390 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003391 AddressTaken = true;
3392 AddrOpLoc = UnOp->getOperatorLoc();
3393 }
Francois Picheta343a412011-04-29 09:08:14 +00003394 }
John McCall91a57552011-07-15 05:09:51 +00003395
Francois Pichet62ec1f22011-09-17 17:15:52 +00003396 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003397 Converted = TemplateArgument(ArgIn);
3398 return false;
3399 }
3400
3401 while (SubstNonTypeTemplateParmExpr *subst =
3402 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3403 Arg = subst->getReplacement()->IgnoreImpCasts();
3404
3405 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003406 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003407 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3408 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003409 S.Diag(Param->getLocation(), diag::note_template_param_here);
3410 return true;
3411 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003412
3413 // Stop checking the precise nature of the argument if it is value dependent,
3414 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003415 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003416 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003417 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003418 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003419
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 if (!isa<ValueDecl>(DRE->getDecl())) {
3421 S.Diag(Arg->getSourceRange().getBegin(),
3422 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003423 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003424 S.Diag(Param->getLocation(), diag::note_template_param_here);
3425 return true;
3426 }
3427
3428 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003429
3430 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003431 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3432 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003433 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003434 S.Diag(Param->getLocation(), diag::note_template_param_here);
3435 return true;
3436 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003437
3438 // Cannot refer to non-static member functions
3439 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003440 if (!Method->isStatic()) {
3441 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003442 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003443 S.Diag(Param->getLocation(), diag::note_template_param_here);
3444 return true;
3445 }
Mike Stump1eb44332009-09-09 15:08:12 +00003446
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003447 // Functions must have external linkage.
3448 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003449 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003450 S.Diag(Arg->getSourceRange().getBegin(),
3451 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003452 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003453 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454 << true;
3455 return true;
3456 }
3457
3458 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003459 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003460
Douglas Gregorb7a09262010-04-01 18:32:35 +00003461 // If the template parameter has pointer type, the function decays.
3462 if (ParamType->isPointerType() && !AddressTaken)
3463 ArgType = S.Context.getPointerType(Func->getType());
3464 else if (AddressTaken && ParamType->isReferenceType()) {
3465 // If we originally had an address-of operator, but the
3466 // parameter has reference type, complain and (if things look
3467 // like they will work) drop the address-of operator.
3468 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3469 ParamType.getNonReferenceType())) {
3470 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3471 << ParamType;
3472 S.Diag(Param->getLocation(), diag::note_template_param_here);
3473 return true;
3474 }
3475
3476 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3477 << ParamType
3478 << FixItHint::CreateRemoval(AddrOpLoc);
3479 S.Diag(Param->getLocation(), diag::note_template_param_here);
3480
3481 ArgType = Func->getType();
3482 }
3483 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003484 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003485 S.Diag(Arg->getSourceRange().getBegin(),
3486 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003487 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003488 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003489 << true;
3490 return true;
3491 }
3492
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 // A value of reference type is not an object.
3494 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003495 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003496 diag::err_template_arg_reference_var)
3497 << Var->getType() << Arg->getSourceRange();
3498 S.Diag(Param->getLocation(), diag::note_template_param_here);
3499 return true;
3500 }
3501
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003502 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003503 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003504
3505 // If the template parameter has pointer type, we must have taken
3506 // the address of this object.
3507 if (ParamType->isReferenceType()) {
3508 if (AddressTaken) {
3509 // If we originally had an address-of operator, but the
3510 // parameter has reference type, complain and (if things look
3511 // like they will work) drop the address-of operator.
3512 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3513 ParamType.getNonReferenceType())) {
3514 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3515 << ParamType;
3516 S.Diag(Param->getLocation(), diag::note_template_param_here);
3517 return true;
3518 }
3519
3520 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3521 << ParamType
3522 << FixItHint::CreateRemoval(AddrOpLoc);
3523 S.Diag(Param->getLocation(), diag::note_template_param_here);
3524
3525 ArgType = Var->getType();
3526 }
3527 } else if (!AddressTaken && ParamType->isPointerType()) {
3528 if (Var->getType()->isArrayType()) {
3529 // Array-to-pointer decay.
3530 ArgType = S.Context.getArrayDecayedType(Var->getType());
3531 } else {
3532 // If the template parameter has pointer type but the address of
3533 // this object was not taken, complain and (possibly) recover by
3534 // taking the address of the entity.
3535 ArgType = S.Context.getPointerType(Var->getType());
3536 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3537 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3538 << ParamType;
3539 S.Diag(Param->getLocation(), diag::note_template_param_here);
3540 return true;
3541 }
3542
3543 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3544 << ParamType
3545 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3546
3547 S.Diag(Param->getLocation(), diag::note_template_param_here);
3548 }
3549 }
3550 } else {
3551 // We found something else, but we don't know specifically what it is.
3552 S.Diag(Arg->getSourceRange().getBegin(),
3553 diag::err_template_arg_not_object_or_func)
3554 << Arg->getSourceRange();
3555 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3556 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003557 }
Mike Stump1eb44332009-09-09 15:08:12 +00003558
John McCallf85e1932011-06-15 23:02:42 +00003559 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003560 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003561 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003562 S.IsQualificationConversion(ArgType, ParamType, false,
3563 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003564 // For pointer-to-object types, qualification conversions are
3565 // permitted.
3566 } else {
3567 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3568 if (!ParamRef->getPointeeType()->isFunctionType()) {
3569 // C++ [temp.arg.nontype]p5b3:
3570 // For a non-type template-parameter of type reference to
3571 // object, no conversions apply. The type referred to by the
3572 // reference may be more cv-qualified than the (otherwise
3573 // identical) type of the template- argument. The
3574 // template-parameter is bound directly to the
3575 // template-argument, which shall be an lvalue.
3576
3577 // FIXME: Other qualifiers?
3578 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3579 unsigned ArgQuals = ArgType.getCVRQualifiers();
3580
3581 if ((ParamQuals | ArgQuals) != ParamQuals) {
3582 S.Diag(Arg->getSourceRange().getBegin(),
3583 diag::err_template_arg_ref_bind_ignores_quals)
3584 << ParamType << Arg->getType()
3585 << Arg->getSourceRange();
3586 S.Diag(Param->getLocation(), diag::note_template_param_here);
3587 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003588 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003589 }
3590 }
3591
3592 // At this point, the template argument refers to an object or
3593 // function with external linkage. We now need to check whether the
3594 // argument and parameter types are compatible.
3595 if (!S.Context.hasSameUnqualifiedType(ArgType,
3596 ParamType.getNonReferenceType())) {
3597 // We can't perform this conversion or binding.
3598 if (ParamType->isReferenceType())
3599 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003600 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003601 else
3602 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003603 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003604 S.Diag(Param->getLocation(), diag::note_template_param_here);
3605 return true;
3606 }
3607 }
3608
3609 // Create the template argument.
3610 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003611 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003612 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003613}
3614
3615/// \brief Checks whether the given template argument is a pointer to
3616/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003617bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003618 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003619 bool Invalid = false;
3620
3621 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003622 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003623 Arg = Cast->getSubExpr();
3624
3625 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003626 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003627 // A template-argument for a non-type, non-template
3628 // template-parameter shall be one of: [...]
3629 //
3630 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003631 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003632
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003633 // In C++98/03 mode, give an extension warning on any extra parentheses.
3634 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3635 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003636 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003637 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003638 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003639 getLangOptions().CPlusPlus0x ?
3640 diag::warn_cxx98_compat_template_arg_extra_parens :
3641 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003642 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003643 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003644 }
3645
3646 Arg = Parens->getSubExpr();
3647 }
3648
John McCall91a57552011-07-15 05:09:51 +00003649 while (SubstNonTypeTemplateParmExpr *subst =
3650 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3651 Arg = subst->getReplacement()->IgnoreImpCasts();
3652
Douglas Gregorcaddba02009-11-12 18:38:13 +00003653 // A pointer-to-member constant written &Class::member.
3654 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003655 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003656 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3657 if (DRE && !DRE->getQualifier())
3658 DRE = 0;
3659 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003660 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003661 // A constant of pointer-to-member type.
3662 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3663 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3664 if (VD->getType()->isMemberPointerType()) {
3665 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003666 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003667 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3668 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003669 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003670 else
3671 Converted = TemplateArgument(VD->getCanonicalDecl());
3672 return Invalid;
3673 }
3674 }
3675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003676
Douglas Gregorcaddba02009-11-12 18:38:13 +00003677 DRE = 0;
3678 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003679
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003680 if (!DRE)
3681 return Diag(Arg->getSourceRange().getBegin(),
3682 diag::err_template_arg_not_pointer_to_member_form)
3683 << Arg->getSourceRange();
3684
3685 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3686 assert((isa<FieldDecl>(DRE->getDecl()) ||
3687 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3688 "Only non-static member pointers can make it here");
3689
3690 // Okay: this is the address of a non-static member, and therefore
3691 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003692 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003693 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003694 else
3695 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003696 return Invalid;
3697 }
3698
3699 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003700 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003701 diag::err_template_arg_not_pointer_to_member_form)
3702 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003703 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003704 diag::note_template_arg_refers_here);
3705 return true;
3706}
3707
Douglas Gregorc15cb382009-02-09 23:23:08 +00003708/// \brief Check a template argument against its corresponding
3709/// non-type template parameter.
3710///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003711/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003712/// If an error occurred, it returns ExprError(); otherwise, it
3713/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003714/// InstantiatedParamType is the type of the non-type template
3715/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003716ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3717 QualType InstantiatedParamType, Expr *Arg,
3718 TemplateArgument &Converted,
3719 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003720 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3721
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003722 // If either the parameter has a dependent type or the argument is
3723 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003724 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3725 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003726 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003727 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003728 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003729
3730 // C++ [temp.arg.nontype]p5:
3731 // The following conversions are performed on each expression used
3732 // as a non-type template-argument. If a non-type
3733 // template-argument cannot be converted to the type of the
3734 // corresponding template-parameter then the program is
3735 // ill-formed.
3736 //
3737 // -- for a non-type template-parameter of integral or
3738 // enumeration type, integral promotions (4.5) and integral
3739 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003740 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003741 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith4f870622011-10-27 22:11:44 +00003742 // FIXME: In C++11, the argument is a converted constant expression of the
3743 // type of the template parameter.
3744 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3745 if (ArgResult.isInvalid())
3746 return ExprError();
3747 Arg = ArgResult.take();
3748
3749 QualType ArgType = Arg->getType();
3750
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003751 // C++ [temp.arg.nontype]p1:
3752 // A template-argument for a non-type, non-template
3753 // template-parameter shall be one of:
3754 //
3755 // -- an integral constant-expression of integral or enumeration
3756 // type; or
3757 // -- the name of a non-type template-parameter; or
3758 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003759 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003760 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003761 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003762 diag::err_template_arg_not_integral_or_enumeral)
3763 << ArgType << Arg->getSourceRange();
3764 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003765 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003766 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003767 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003768 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3769 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003770 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003771 }
3772
Douglas Gregor02024a92010-03-28 02:42:43 +00003773 // From here on out, all we care about are the unqualified forms
3774 // of the parameter and argument types.
3775 ParamType = ParamType.getUnqualifiedType();
3776 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003777
3778 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003779 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003780 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003781 } else if (CTAK == CTAK_Deduced) {
3782 // C++ [temp.deduct.type]p17:
3783 // If, in the declaration of a function template with a non-type
3784 // template-parameter, the non-type template- parameter is used
3785 // in an expression in the function parameter-list and, if the
3786 // corresponding template-argument is deduced, the
3787 // template-argument type shall match the type of the
3788 // template-parameter exactly, except that a template-argument
3789 // deduced from an array bound may be of any integral type.
3790 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3791 << ArgType << ParamType;
3792 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003793 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003794 } else if (ParamType->isBooleanType()) {
3795 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003796 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003797 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3798 !ParamType->isEnumeralType()) {
3799 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003800 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003801 } else {
3802 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003803 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003804 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003805 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003806 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003807 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003808 }
3809
Douglas Gregorc7469372011-05-04 21:55:00 +00003810 // Add the value of this argument to the list of converted
3811 // arguments. We use the bitwidth and signedness of the template
3812 // parameter.
3813 if (Arg->isValueDependent()) {
3814 // The argument is value-dependent. Create a new
3815 // TemplateArgument with the converted expression.
3816 Converted = TemplateArgument(Arg);
3817 return Owned(Arg);
3818 }
3819
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003820 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003821 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003822 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003823
Douglas Gregorc7469372011-05-04 21:55:00 +00003824 if (ParamType->isBooleanType()) {
3825 // Value must be zero or one.
3826 Value = Value != 0;
3827 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3828 if (Value.getBitWidth() != AllowedBits)
3829 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003830 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003831 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003832 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003833
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003834 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003835 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003836 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003837 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003838 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003839 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003840
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003841 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003842 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003843 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003844 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3845 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3846 << Arg->getSourceRange();
3847 Diag(Param->getLocation(), diag::note_template_param_here);
3848 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003849
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003850 // Complain if we overflowed the template parameter's type.
3851 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003852 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003853 RequiredBits = OldValue.getActiveBits();
3854 else if (OldValue.isUnsigned())
3855 RequiredBits = OldValue.getActiveBits() + 1;
3856 else
3857 RequiredBits = OldValue.getMinSignedBits();
3858 if (RequiredBits > AllowedBits) {
3859 Diag(Arg->getSourceRange().getBegin(),
3860 diag::warn_template_arg_too_large)
3861 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3862 << Arg->getSourceRange();
3863 Diag(Param->getLocation(), diag::note_template_param_here);
3864 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003865 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003866
John McCall833ca992009-10-29 08:12:44 +00003867 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003868 ParamType->isEnumeralType()
3869 ? Context.getCanonicalType(ParamType)
3870 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003871 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003872 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003873
Richard Smith4f870622011-10-27 22:11:44 +00003874 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00003875 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3876
Douglas Gregorb7a09262010-04-01 18:32:35 +00003877 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3878 // from a template argument of type std::nullptr_t to a non-type
3879 // template parameter of type pointer to object, pointer to
3880 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003881 if (ArgType->isNullPtrType()) {
3882 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3883 Converted = TemplateArgument((NamedDecl *)0);
3884 return Owned(Arg);
3885 }
3886
3887 if (ParamType->isNullPtrType()) {
3888 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3889 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3890 return Owned(Arg);
3891 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003892 }
3893
Douglas Gregorb86b0572009-02-11 01:18:59 +00003894 // Handle pointer-to-function, reference-to-function, and
3895 // pointer-to-member-function all in (roughly) the same way.
3896 if (// -- For a non-type template-parameter of type pointer to
3897 // function, only the function-to-pointer conversion (4.3) is
3898 // applied. If the template-argument represents a set of
3899 // overloaded functions (or a pointer to such), the matching
3900 // function is selected from the set (13.4).
3901 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003902 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003903 // -- For a non-type template-parameter of type reference to
3904 // function, no conversions apply. If the template-argument
3905 // represents a set of overloaded functions, the matching
3906 // function is selected from the set (13.4).
3907 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003908 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003909 // -- For a non-type template-parameter of type pointer to
3910 // member function, no conversions apply. If the
3911 // template-argument represents a set of overloaded member
3912 // functions, the matching member function is selected from
3913 // the set (13.4).
3914 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003915 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003916 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003917
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003918 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003919 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003920 true,
3921 FoundResult)) {
3922 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003923 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003924
3925 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3926 ArgType = Arg->getType();
3927 } else
John Wiegley429bb272011-04-08 18:41:53 +00003928 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003929 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003930
John Wiegley429bb272011-04-08 18:41:53 +00003931 if (!ParamType->isMemberPointerType()) {
3932 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3933 ParamType,
3934 Arg, Converted))
3935 return ExprError();
3936 return Owned(Arg);
3937 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003938
John McCallf85e1932011-06-15 23:02:42 +00003939 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003940 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003941 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003942 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3943 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003944 } else if (!Context.hasSameUnqualifiedType(ArgType,
3945 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003946 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003947 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003948 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003949 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003950 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003951 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003952 }
Mike Stump1eb44332009-09-09 15:08:12 +00003953
John Wiegley429bb272011-04-08 18:41:53 +00003954 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3955 return ExprError();
3956 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003957 }
3958
Chris Lattnerfe90de72009-02-20 21:37:53 +00003959 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003960 // -- for a non-type template-parameter of type pointer to
3961 // object, qualification conversions (4.4) and the
3962 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003963 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003964 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003965 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003966
John Wiegley429bb272011-04-08 18:41:53 +00003967 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3968 ParamType,
3969 Arg, Converted))
3970 return ExprError();
3971 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003972 }
Mike Stump1eb44332009-09-09 15:08:12 +00003973
Ted Kremenek6217b802009-07-29 21:53:49 +00003974 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003975 // -- For a non-type template-parameter of type reference to
3976 // object, no conversions apply. The type referred to by the
3977 // reference may be more cv-qualified than the (otherwise
3978 // identical) type of the template-argument. The
3979 // template-parameter is bound directly to the
3980 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003981 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003982 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003983
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003984 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003985 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3986 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003987 true,
3988 FoundResult)) {
3989 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003990 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003991
3992 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3993 ArgType = Arg->getType();
3994 } else
John Wiegley429bb272011-04-08 18:41:53 +00003995 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003996 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003997
John Wiegley429bb272011-04-08 18:41:53 +00003998 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3999 ParamType,
4000 Arg, Converted))
4001 return ExprError();
4002 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004003 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004004
4005 // -- For a non-type template-parameter of type pointer to data
4006 // member, qualification conversions (4.4) are applied.
4007 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4008
John McCallf85e1932011-06-15 23:02:42 +00004009 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004010 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004011 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004012 } else if (IsQualificationConversion(ArgType, ParamType, false,
4013 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004014 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4015 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004016 } else {
4017 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004018 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004019 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004020 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004021 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004022 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004023 }
4024
John Wiegley429bb272011-04-08 18:41:53 +00004025 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4026 return ExprError();
4027 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004028}
4029
4030/// \brief Check a template argument against its corresponding
4031/// template template parameter.
4032///
4033/// This routine implements the semantics of C++ [temp.arg.template].
4034/// It returns true if an error occurred, and false otherwise.
4035bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004036 const TemplateArgumentLoc &Arg) {
4037 TemplateName Name = Arg.getArgument().getAsTemplate();
4038 TemplateDecl *Template = Name.getAsTemplateDecl();
4039 if (!Template) {
4040 // Any dependent template name is fine.
4041 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4042 return false;
4043 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004044
Richard Smith3e4c6c42011-05-05 21:57:07 +00004045 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004046 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004047 // the name of a class template or an alias template, expressed as an
4048 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004049 // primary class templates are considered when matching the
4050 // template template argument with the corresponding parameter;
4051 // partial specializations are not considered even if their
4052 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004053 //
4054 // Note that we also allow template template parameters here, which
4055 // will happen when we are dealing with, e.g., class template
4056 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004057 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004058 !isa<TemplateTemplateParmDecl>(Template) &&
4059 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004060 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004061 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004062 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004063 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004064 << Template;
4065 }
4066
4067 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4068 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004069 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004070 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004071 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004072}
4073
Douglas Gregor02024a92010-03-28 02:42:43 +00004074/// \brief Given a non-type template argument that refers to a
4075/// declaration and the type of its corresponding non-type template
4076/// parameter, produce an expression that properly refers to that
4077/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004078ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004079Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4080 QualType ParamType,
4081 SourceLocation Loc) {
4082 assert(Arg.getKind() == TemplateArgument::Declaration &&
4083 "Only declaration template arguments permitted here");
4084 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4085
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004086 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004087 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4088 // If the value is a class member, we might have a pointer-to-member.
4089 // Determine whether the non-type template template parameter is of
4090 // pointer-to-member type. If so, we need to build an appropriate
4091 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4092 // would refer to the member itself.
4093 if (ParamType->isMemberPointerType()) {
4094 QualType ClassType
4095 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4096 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004097 = NestedNameSpecifier::Create(Context, 0, false,
4098 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004099 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004100 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004101
4102 // The actual value-ness of this is unimportant, but for
4103 // internal consistency's sake, references to instance methods
4104 // are r-values.
4105 ExprValueKind VK = VK_LValue;
4106 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4107 VK = VK_RValue;
4108
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004109 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004110 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004111 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004112 Loc,
4113 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004114 if (RefExpr.isInvalid())
4115 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004116
John McCall2de56d12010-08-25 11:45:40 +00004117 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004118
Douglas Gregorc0c83002010-04-30 21:46:38 +00004119 // We might need to perform a trailing qualification conversion, since
4120 // the element type on the parameter could be more qualified than the
4121 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004122 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004123 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004124 ParamType.getUnqualifiedType(), false,
4125 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004126 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004127
Douglas Gregor02024a92010-03-28 02:42:43 +00004128 assert(!RefExpr.isInvalid() &&
4129 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004130 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004131 return move(RefExpr);
4132 }
4133 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134
Douglas Gregor02024a92010-03-28 02:42:43 +00004135 QualType T = VD->getType().getNonReferenceType();
4136 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004137 // When the non-type template parameter is a pointer, take the
4138 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004139 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004140 if (RefExpr.isInvalid())
4141 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004142
4143 if (T->isFunctionType() || T->isArrayType()) {
4144 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004145 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4146 if (RefExpr.isInvalid())
4147 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004148
4149 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004150 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004151
Douglas Gregorb7a09262010-04-01 18:32:35 +00004152 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004153 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004154 }
4155
John McCallf89e55a2010-11-18 06:31:45 +00004156 ExprValueKind VK = VK_RValue;
4157
Douglas Gregor02024a92010-03-28 02:42:43 +00004158 // If the non-type template parameter has reference type, qualify the
4159 // resulting declaration reference with the extra qualifiers on the
4160 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004161 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4162 VK = VK_LValue;
4163 T = Context.getQualifiedType(T,
4164 TargetRef->getPointeeType().getQualifiers());
4165 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004166
John McCallf89e55a2010-11-18 06:31:45 +00004167 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004168}
4169
4170/// \brief Construct a new expression that refers to the given
4171/// integral template argument with the given source-location
4172/// information.
4173///
4174/// This routine takes care of the mapping from an integral template
4175/// argument (which may have any integral type) to the appropriate
4176/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004177ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004178Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4179 SourceLocation Loc) {
4180 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004181 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004182 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004183 if (T->isAnyCharacterType()) {
4184 CharacterLiteral::CharacterKind Kind;
4185 if (T->isWideCharType())
4186 Kind = CharacterLiteral::Wide;
4187 else if (T->isChar16Type())
4188 Kind = CharacterLiteral::UTF16;
4189 else if (T->isChar32Type())
4190 Kind = CharacterLiteral::UTF32;
4191 else
4192 Kind = CharacterLiteral::Ascii;
4193
Douglas Gregor02024a92010-03-28 02:42:43 +00004194 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004195 Arg.getAsIntegral()->getZExtValue(),
4196 Kind, T, Loc));
4197 }
4198
Douglas Gregor02024a92010-03-28 02:42:43 +00004199 if (T->isBooleanType())
4200 return Owned(new (Context) CXXBoolLiteralExpr(
4201 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004202 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004203
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004204 if (T->isNullPtrType())
4205 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4206
Chris Lattner223de242011-04-25 20:37:58 +00004207 // If this is an enum type that we're instantiating, we need to use an integer
4208 // type the same size as the enumerator. We don't want to build an
4209 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004210 QualType BT;
4211 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004212 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004213 else
4214 BT = T;
4215
John McCall4e9272d2011-07-15 07:47:58 +00004216 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4217 if (T->isEnumeralType()) {
4218 // FIXME: This is a hack. We need a better way to handle substituted
4219 // non-type template parameters.
4220 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4221 Context.getTrivialTypeSourceInfo(T, Loc),
4222 Loc, Loc);
4223 }
4224
4225 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004226}
4227
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004228/// \brief Match two template parameters within template parameter lists.
4229static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4230 bool Complain,
4231 Sema::TemplateParameterListEqualKind Kind,
4232 SourceLocation TemplateArgLoc) {
4233 // Check the actual kind (type, non-type, template).
4234 if (Old->getKind() != New->getKind()) {
4235 if (Complain) {
4236 unsigned NextDiag = diag::err_template_param_different_kind;
4237 if (TemplateArgLoc.isValid()) {
4238 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4239 NextDiag = diag::note_template_param_different_kind;
4240 }
4241 S.Diag(New->getLocation(), NextDiag)
4242 << (Kind != Sema::TPL_TemplateMatch);
4243 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4244 << (Kind != Sema::TPL_TemplateMatch);
4245 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004246
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004247 return false;
4248 }
4249
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004250 // Check that both are parameter packs are neither are parameter packs.
4251 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004252 // template template parameter, the template template parameter can have
4253 // a parameter pack where the template template argument does not.
4254 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4255 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4256 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004257 if (Complain) {
4258 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4259 if (TemplateArgLoc.isValid()) {
4260 S.Diag(TemplateArgLoc,
4261 diag::err_template_arg_template_params_mismatch);
4262 NextDiag = diag::note_template_parameter_pack_non_pack;
4263 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004264
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004265 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4266 : isa<NonTypeTemplateParmDecl>(New)? 1
4267 : 2;
4268 S.Diag(New->getLocation(), NextDiag)
4269 << ParamKind << New->isParameterPack();
4270 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4271 << ParamKind << Old->isParameterPack();
4272 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004273
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004274 return false;
4275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004277 // For non-type template parameters, check the type of the parameter.
4278 if (NonTypeTemplateParmDecl *OldNTTP
4279 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4280 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004282 // If we are matching a template template argument to a template
4283 // template parameter and one of the non-type template parameter types
4284 // is dependent, then we must wait until template instantiation time
4285 // to actually compare the arguments.
4286 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4287 (OldNTTP->getType()->isDependentType() ||
4288 NewNTTP->getType()->isDependentType()))
4289 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004290
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004291 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4292 if (Complain) {
4293 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4294 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004295 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004296 diag::err_template_arg_template_params_mismatch);
4297 NextDiag = diag::note_template_nontype_parm_different_type;
4298 }
4299 S.Diag(NewNTTP->getLocation(), NextDiag)
4300 << NewNTTP->getType()
4301 << (Kind != Sema::TPL_TemplateMatch);
4302 S.Diag(OldNTTP->getLocation(),
4303 diag::note_template_nontype_parm_prev_declaration)
4304 << OldNTTP->getType();
4305 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004306
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004307 return false;
4308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004309
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004310 return true;
4311 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004312
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004313 // For template template parameters, check the template parameter types.
4314 // The template parameter lists of template template
4315 // parameters must agree.
4316 if (TemplateTemplateParmDecl *OldTTP
4317 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004318 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004319 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4320 OldTTP->getTemplateParameters(),
4321 Complain,
4322 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004324 : Kind),
4325 TemplateArgLoc);
4326 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004327
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004328 return true;
4329}
Douglas Gregor02024a92010-03-28 02:42:43 +00004330
Douglas Gregora0347822011-01-13 00:08:50 +00004331/// \brief Diagnose a known arity mismatch when comparing template argument
4332/// lists.
4333static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004334void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004335 TemplateParameterList *New,
4336 TemplateParameterList *Old,
4337 Sema::TemplateParameterListEqualKind Kind,
4338 SourceLocation TemplateArgLoc) {
4339 unsigned NextDiag = diag::err_template_param_list_different_arity;
4340 if (TemplateArgLoc.isValid()) {
4341 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4342 NextDiag = diag::note_template_param_list_different_arity;
4343 }
4344 S.Diag(New->getTemplateLoc(), NextDiag)
4345 << (New->size() > Old->size())
4346 << (Kind != Sema::TPL_TemplateMatch)
4347 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4348 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4349 << (Kind != Sema::TPL_TemplateMatch)
4350 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4351}
4352
Douglas Gregorddc29e12009-02-06 22:42:48 +00004353/// \brief Determine whether the given template parameter lists are
4354/// equivalent.
4355///
Mike Stump1eb44332009-09-09 15:08:12 +00004356/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004357/// source code as part of a new template declaration.
4358///
4359/// \param Old The old template parameter list, typically found via
4360/// name lookup of the template declared with this template parameter
4361/// list.
4362///
4363/// \param Complain If true, this routine will produce a diagnostic if
4364/// the template parameter lists are not equivalent.
4365///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004366/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004367///
4368/// \param TemplateArgLoc If this source location is valid, then we
4369/// are actually checking the template parameter list of a template
4370/// argument (New) against the template parameter list of its
4371/// corresponding template template parameter (Old). We produce
4372/// slightly different diagnostics in this scenario.
4373///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004374/// \returns True if the template parameter lists are equal, false
4375/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004376bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004377Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4378 TemplateParameterList *Old,
4379 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004380 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004381 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004382 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4383 if (Complain)
4384 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4385 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004386
4387 return false;
4388 }
4389
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004390 // C++0x [temp.arg.template]p3:
4391 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004392 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004393 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004394 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004395 // template-parameter-list of P. [...]
4396 TemplateParameterList::iterator NewParm = New->begin();
4397 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004398 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004399 OldParmEnd = Old->end();
4400 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004401 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4402 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004403 if (NewParm == NewParmEnd) {
4404 if (Complain)
4405 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4406 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004407
Douglas Gregora0347822011-01-13 00:08:50 +00004408 return false;
4409 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410
Douglas Gregora0347822011-01-13 00:08:50 +00004411 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4412 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004413 return false;
4414
Douglas Gregora0347822011-01-13 00:08:50 +00004415 ++NewParm;
4416 continue;
4417 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004418
Douglas Gregora0347822011-01-13 00:08:50 +00004419 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004420 // [...] When P's template- parameter-list contains a template parameter
4421 // pack (14.5.3), the template parameter pack will match zero or more
4422 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004423 // template-parameter-list of A with the same type and form as the
4424 // template parameter pack in P (ignoring whether those template
4425 // parameters are template parameter packs).
4426 for (; NewParm != NewParmEnd; ++NewParm) {
4427 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4428 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004429 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004430 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004431 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
Douglas Gregora0347822011-01-13 00:08:50 +00004433 // Make sure we exhausted all of the arguments.
4434 if (NewParm != NewParmEnd) {
4435 if (Complain)
4436 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4437 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004438
Douglas Gregora0347822011-01-13 00:08:50 +00004439 return false;
4440 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004441
Douglas Gregorddc29e12009-02-06 22:42:48 +00004442 return true;
4443}
4444
4445/// \brief Check whether a template can be declared within this scope.
4446///
4447/// If the template declaration is valid in this scope, returns
4448/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004449bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004450Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004451 if (!S)
4452 return false;
4453
Douglas Gregorddc29e12009-02-06 22:42:48 +00004454 // Find the nearest enclosing declaration scope.
4455 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4456 (S->getFlags() & Scope::TemplateParamScope) != 0)
4457 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004458
Douglas Gregorddc29e12009-02-06 22:42:48 +00004459 // C++ [temp]p2:
4460 // A template-declaration can appear only as a namespace scope or
4461 // class scope declaration.
4462 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004463 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4464 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004465 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004466 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004467
Eli Friedman1503f772009-07-31 01:43:05 +00004468 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004469 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004470
4471 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4472 return false;
4473
Mike Stump1eb44332009-09-09 15:08:12 +00004474 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004475 diag::err_template_outside_namespace_or_class_scope)
4476 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004477}
Douglas Gregorcc636682009-02-17 23:15:12 +00004478
Douglas Gregord5cb8762009-10-07 00:13:32 +00004479/// \brief Determine what kind of template specialization the given declaration
4480/// is.
4481static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4482 if (!D)
4483 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004484
Douglas Gregorf6b11852009-10-08 15:14:33 +00004485 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4486 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004487 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4488 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004489 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4490 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491
Douglas Gregord5cb8762009-10-07 00:13:32 +00004492 return TSK_Undeclared;
4493}
4494
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004495/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004496/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004497///
Douglas Gregor9302da62009-10-14 23:50:59 +00004498/// This routine determines whether a template specialization can be declared
4499/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004500///
4501/// \param S the semantic analysis object for which this check is being
4502/// performed.
4503///
4504/// \param Specialized the entity being specialized or instantiated, which
4505/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004507/// member class).
4508///
4509/// \param PrevDecl the previous declaration of this entity, if any.
4510///
4511/// \param Loc the location of the explicit specialization or instantiation of
4512/// this entity.
4513///
4514/// \param IsPartialSpecialization whether this is a partial specialization of
4515/// a class template.
4516///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004517/// \returns true if there was an error that we cannot recover from, false
4518/// otherwise.
4519static bool CheckTemplateSpecializationScope(Sema &S,
4520 NamedDecl *Specialized,
4521 NamedDecl *PrevDecl,
4522 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004523 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004524 // Keep these "kind" numbers in sync with the %select statements in the
4525 // various diagnostics emitted by this routine.
4526 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004527 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004528 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004529 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004530 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004531 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004532 EntityKind = 3;
4533 else if (isa<VarDecl>(Specialized))
4534 EntityKind = 4;
4535 else if (isa<RecordDecl>(Specialized))
4536 EntityKind = 5;
4537 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004538 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4539 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004540 return true;
4541 }
4542
Douglas Gregor88b70942009-02-25 22:02:03 +00004543 // C++ [temp.expl.spec]p2:
4544 // An explicit specialization shall be declared in the namespace
4545 // of which the template is a member, or, for member templates, in
4546 // the namespace of which the enclosing class or enclosing class
4547 // template is a member. An explicit specialization of a member
4548 // function, member class or static data member of a class
4549 // template shall be declared in the namespace of which the class
4550 // template is a member. Such a declaration may also be a
4551 // definition. If the declaration is not a definition, the
4552 // specialization may be defined later in the name- space in which
4553 // the explicit specialization was declared, or in a namespace
4554 // that encloses the one in which the explicit specialization was
4555 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004556 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004557 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004558 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004559 return true;
4560 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004561
Douglas Gregor0a407472009-10-07 17:30:37 +00004562 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004563 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004564 // Do not warn for class scope explicit specialization during
4565 // instantiation, warning was already emitted during pattern
4566 // semantic analysis.
4567 if (!S.ActiveTemplateInstantiations.size())
4568 S.Diag(Loc, diag::ext_function_specialization_in_class)
4569 << Specialized;
4570 } else {
4571 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4572 << Specialized;
4573 return true;
4574 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004575 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004576
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004577 if (S.CurContext->isRecord() &&
4578 !S.CurContext->Equals(Specialized->getDeclContext())) {
4579 // Make sure that we're specializing in the right record context.
4580 // Otherwise, things can go horribly wrong.
4581 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4582 << Specialized;
4583 return true;
4584 }
4585
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004586 // C++ [temp.class.spec]p6:
4587 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588 // in any namespace scope in which its definition may be defined (14.5.1
4589 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004590 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004591 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004592 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004593 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004595 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4596 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004597 // C++ [temp.exp.spec]p2:
4598 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004599 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004600 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004601 // An explicit specialization of a member function, member class or
4602 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004603 // namespace of which the class template is a member.
4604 //
4605 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004607 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004608 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4609 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4610 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4611 assert(!IsCPlusPlus0xExtension &&
4612 "DC encloses TU but isn't in enclosing namespace set");
4613 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004614 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004615 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4616 int Diag;
4617 if (!IsCPlusPlus0xExtension)
4618 Diag = diag::err_template_spec_decl_out_of_scope;
4619 else if (!S.getLangOptions().CPlusPlus0x)
4620 Diag = diag::ext_template_spec_decl_out_of_scope;
4621 else
4622 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4623 S.Diag(Loc, Diag)
4624 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4625 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004626
Douglas Gregor9302da62009-10-14 23:50:59 +00004627 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004628 ComplainedAboutScope =
4629 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004630 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004631 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004632
4633 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004634 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004635 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004636 // specializations of function templates, static data members, and member
4637 // functions, so we skip the check here for those kinds of entities.
4638 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004639 // Should we refactor that check, so that it occurs later?
4640 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004641 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4642 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004643 if (isa<TranslationUnitDecl>(SpecializedContext))
4644 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4645 << EntityKind << Specialized;
4646 else if (isa<NamespaceDecl>(SpecializedContext))
4647 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4648 << EntityKind << Specialized
4649 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004650
Douglas Gregor9302da62009-10-14 23:50:59 +00004651 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004652 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004653
Douglas Gregord5cb8762009-10-07 00:13:32 +00004654 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655
Douglas Gregor88b70942009-02-25 22:02:03 +00004656 return false;
4657}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004658
Douglas Gregorbacb9492011-01-03 21:13:47 +00004659/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4660/// that checks non-type template partial specialization arguments.
4661static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4662 NonTypeTemplateParmDecl *Param,
4663 const TemplateArgument *Args,
4664 unsigned NumArgs) {
4665 for (unsigned I = 0; I != NumArgs; ++I) {
4666 if (Args[I].getKind() == TemplateArgument::Pack) {
4667 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004668 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004669 Args[I].pack_size()))
4670 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004671
Douglas Gregore94866f2009-06-12 21:21:02 +00004672 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004674
Douglas Gregorbacb9492011-01-03 21:13:47 +00004675 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004676 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004677 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004678 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004679
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004680 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004681 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4682 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004683
4684 // Strip off any implicit casts we added as part of type checking.
4685 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4686 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004687
Douglas Gregore94866f2009-06-12 21:21:02 +00004688 // C++ [temp.class.spec]p8:
4689 // A non-type argument is non-specialized if it is the name of a
4690 // non-type parameter. All other non-type arguments are
4691 // specialized.
4692 //
4693 // Below, we check the two conditions that only apply to
4694 // specialized non-type arguments, so skip any non-specialized
4695 // arguments.
4696 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004697 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004698 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004699
Douglas Gregore94866f2009-06-12 21:21:02 +00004700 // C++ [temp.class.spec]p9:
4701 // Within the argument list of a class template partial
4702 // specialization, the following restrictions apply:
4703 // -- A partially specialized non-type argument expression
4704 // shall not involve a template parameter of the partial
4705 // specialization except when the argument expression is a
4706 // simple identifier.
4707 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004708 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004709 diag::err_dependent_non_type_arg_in_partial_spec)
4710 << ArgExpr->getSourceRange();
4711 return true;
4712 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004713
Douglas Gregore94866f2009-06-12 21:21:02 +00004714 // -- The type of a template parameter corresponding to a
4715 // specialized non-type argument shall not be dependent on a
4716 // parameter of the specialization.
4717 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004718 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004719 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4720 << Param->getType()
4721 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004722 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004723 return true;
4724 }
4725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004726
Douglas Gregorbacb9492011-01-03 21:13:47 +00004727 return false;
4728}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004729
Douglas Gregorbacb9492011-01-03 21:13:47 +00004730/// \brief Check the non-type template arguments of a class template
4731/// partial specialization according to C++ [temp.class.spec]p9.
4732///
4733/// \param TemplateParams the template parameters of the primary class
4734/// template.
4735///
4736/// \param TemplateArg the template arguments of the class template
4737/// partial specialization.
4738///
4739/// \returns true if there was an error, false otherwise.
4740static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4741 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004742 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004743 const TemplateArgument *ArgList = TemplateArgs.data();
4744
4745 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4746 NonTypeTemplateParmDecl *Param
4747 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4748 if (!Param)
4749 continue;
4750
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004751 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004752 &ArgList[I], 1))
4753 return true;
4754 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004755
4756 return false;
4757}
4758
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004759/// \brief Retrieve the previous declaration of the given declaration.
4760static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4761 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4762 return VD->getPreviousDeclaration();
4763 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4764 return FD->getPreviousDeclaration();
4765 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4766 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004767 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004768 return TD->getPreviousDeclaration();
4769 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4770 return FTD->getPreviousDeclaration();
4771 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4772 return CTD->getPreviousDeclaration();
4773 return 0;
4774}
4775
John McCalld226f652010-08-21 09:40:31 +00004776DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004777Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4778 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004779 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004780 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004781 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004782 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004783 SourceLocation TemplateNameLoc,
4784 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004785 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004786 SourceLocation RAngleLoc,
4787 AttributeList *Attr,
4788 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004789 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004790
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004791 // NOTE: KWLoc is the location of the tag keyword. This will instead
4792 // store the location of the outermost template keyword in the declaration.
4793 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4794 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4795
Douglas Gregorcc636682009-02-17 23:15:12 +00004796 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004797 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004798 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004799 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4800
4801 if (!ClassTemplate) {
4802 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004803 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004804 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4805 return true;
4806 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004807
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004808 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004809 bool isPartialSpecialization = false;
4810
Douglas Gregor88b70942009-02-25 22:02:03 +00004811 // Check the validity of the template headers that introduce this
4812 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004813 // FIXME: We probably shouldn't complain about these headers for
4814 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004815 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004816 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004817 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4818 TemplateNameLoc,
4819 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004820 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004821 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004822 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004823 isExplicitSpecialization,
4824 Invalid);
4825 if (Invalid)
4826 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004827
Douglas Gregor05396e22009-08-25 17:23:04 +00004828 if (TemplateParams && TemplateParams->size() > 0) {
4829 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004830
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004831 if (TUK == TUK_Friend) {
4832 Diag(KWLoc, diag::err_partial_specialization_friend)
4833 << SourceRange(LAngleLoc, RAngleLoc);
4834 return true;
4835 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836
Douglas Gregor05396e22009-08-25 17:23:04 +00004837 // C++ [temp.class.spec]p10:
4838 // The template parameter list of a specialization shall not
4839 // contain default template argument values.
4840 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4841 Decl *Param = TemplateParams->getParam(I);
4842 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4843 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004844 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004845 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004846 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004847 }
4848 } else if (NonTypeTemplateParmDecl *NTTP
4849 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4850 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004851 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004852 diag::err_default_arg_in_partial_spec)
4853 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004854 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004855 }
4856 } else {
4857 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004858 if (TTP->hasDefaultArgument()) {
4859 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004860 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004861 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004862 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004863 }
4864 }
4865 }
Douglas Gregora735b202009-10-13 14:39:41 +00004866 } else if (TemplateParams) {
4867 if (TUK == TUK_Friend)
4868 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004869 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004870 SourceRange(TemplateParams->getTemplateLoc(),
4871 TemplateParams->getRAngleLoc()))
4872 << SourceRange(LAngleLoc, RAngleLoc);
4873 else
4874 isExplicitSpecialization = true;
4875 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004876 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004877 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004878 isExplicitSpecialization = true;
4879 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004880
Douglas Gregorcc636682009-02-17 23:15:12 +00004881 // Check that the specialization uses the same tag kind as the
4882 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004883 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4884 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004885 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004886 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004887 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004888 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004889 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004890 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004891 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004892 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004893 diag::note_previous_use);
4894 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4895 }
4896
Douglas Gregor40808ce2009-03-09 23:48:35 +00004897 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004898 TemplateArgumentListInfo TemplateArgs;
4899 TemplateArgs.setLAngleLoc(LAngleLoc);
4900 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004901 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004902
Douglas Gregor925910d2011-01-03 20:35:03 +00004903 // Check for unexpanded parameter packs in any of the template arguments.
4904 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004905 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004906 UPPC_PartialSpecialization))
4907 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908
Douglas Gregorcc636682009-02-17 23:15:12 +00004909 // Check that the template argument list is well-formed for this
4910 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004911 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004912 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4913 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004914 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004915
Douglas Gregor910f8002010-11-07 23:05:16 +00004916 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004917 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004918
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004919 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004920 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004921 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004922 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004923 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004924 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004925 return true;
4926
Douglas Gregor561f8122011-07-01 01:22:09 +00004927 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004928 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004929 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004930 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004931 TemplateArgs.size(),
4932 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004933 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4934 << ClassTemplate->getDeclName();
4935 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004936 }
4937 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004938
Douglas Gregorcc636682009-02-17 23:15:12 +00004939 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004940 ClassTemplateSpecializationDecl *PrevDecl = 0;
4941
4942 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004943 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004944 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004945 = ClassTemplate->findPartialSpecialization(Converted.data(),
4946 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004947 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004948 else
4949 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004950 = ClassTemplate->findSpecialization(Converted.data(),
4951 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004952
4953 ClassTemplateSpecializationDecl *Specialization = 0;
4954
Douglas Gregor88b70942009-02-25 22:02:03 +00004955 // Check whether we can declare a class template specialization in
4956 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004957 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004958 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4959 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004960 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004961 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004962
Douglas Gregorb88e8882009-07-30 17:40:51 +00004963 // The canonical type
4964 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004965 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004966 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004967 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004968 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004969 // arguments was referenced but not declared, or we're only
4970 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004971 // declaration node as our own, updating its source location and
4972 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004973 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004974 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004975 if (TemplateParameterLists.size() > 0) {
4976 Specialization->setTemplateParameterListsInfo(Context,
4977 TemplateParameterLists.size(),
4978 (TemplateParameterList**) TemplateParameterLists.release());
4979 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004980 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004981 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004982 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004983 // Build the canonical type that describes the converted template
4984 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004985 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4986 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004987 Converted.data(),
4988 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004989
4990 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004991 ClassTemplate->getInjectedClassNameSpecialization())) {
4992 // C++ [temp.class.spec]p9b3:
4993 //
4994 // -- The argument list of the specialization shall not be identical
4995 // to the implicit argument list of the primary template.
4996 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004997 << (TUK == TUK_Definition)
4998 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004999 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5000 ClassTemplate->getIdentifier(),
5001 TemplateNameLoc,
5002 Attr,
5003 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005004 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005005 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005006 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005007 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005008
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005009 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005010 ClassTemplatePartialSpecializationDecl *PrevPartial
5011 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005012 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005013 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005014 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005015 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005016 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005017 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005018 TemplateParams,
5019 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005020 Converted.data(),
5021 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005022 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005023 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005024 PrevPartial,
5025 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005026 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005027 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005028 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005029 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005030 (TemplateParameterList**) TemplateParameterLists.release());
5031 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005032
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005033 if (!PrevPartial)
5034 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005035 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005036
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005037 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005038 // template specialization, make a note of that.
5039 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5040 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005041
Douglas Gregor031a5882009-06-13 00:26:55 +00005042 // Check that all of the template parameters of the class template
5043 // partial specialization are deducible from the template
5044 // arguments. If not, this class template partial specialization
5045 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005046 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005047 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005049 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005050 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005051 unsigned NumNonDeducible = 0;
5052 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5053 if (!DeducibleParams[I])
5054 ++NumNonDeducible;
5055
5056 if (NumNonDeducible) {
5057 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5058 << (NumNonDeducible > 1)
5059 << SourceRange(TemplateNameLoc, RAngleLoc);
5060 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5061 if (!DeducibleParams[I]) {
5062 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5063 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005064 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005065 diag::note_partial_spec_unused_parameter)
5066 << Param->getDeclName();
5067 else
Mike Stump1eb44332009-09-09 15:08:12 +00005068 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005069 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005070 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005071 }
5072 }
5073 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005074 } else {
5075 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005076 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005077 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005078 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005079 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005080 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005081 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005082 Converted.data(),
5083 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005084 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005085 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005086 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005087 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005088 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005089 (TemplateParameterList**) TemplateParameterLists.release());
5090 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005091
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005092 if (!PrevDecl)
5093 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005094
5095 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005096 }
5097
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005098 // C++ [temp.expl.spec]p6:
5099 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005100 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005101 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005102 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005103 // use occurs; no diagnostic is required.
5104 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005105 bool Okay = false;
5106 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5107 // Is there any previous explicit specialization declaration?
5108 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5109 Okay = true;
5110 break;
5111 }
5112 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005113
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005114 if (!Okay) {
5115 SourceRange Range(TemplateNameLoc, RAngleLoc);
5116 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5117 << Context.getTypeDeclType(Specialization) << Range;
5118
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005119 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005120 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005121 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005122 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005123 return true;
5124 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005125 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005126
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005127 // If this is not a friend, note that this is an explicit specialization.
5128 if (TUK != TUK_Friend)
5129 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005130
5131 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005132 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005133 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005134 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005135 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005136 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005137 Diag(Def->getLocation(), diag::note_previous_definition);
5138 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005139 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005140 }
5141 }
5142
John McCall7f1b9872010-12-18 03:30:47 +00005143 if (Attr)
5144 ProcessDeclAttributeList(S, Specialization, Attr);
5145
Douglas Gregord023aec2011-09-09 20:53:38 +00005146 if (ModulePrivateLoc.isValid())
5147 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5148 << (isPartialSpecialization? 1 : 0)
5149 << FixItHint::CreateRemoval(ModulePrivateLoc);
5150
Douglas Gregorfc705b82009-02-26 22:19:44 +00005151 // Build the fully-sugared type for this class template
5152 // specialization as the user wrote in the specialization
5153 // itself. This means that we'll pretty-print the type retrieved
5154 // from the specialization's declaration the way that the user
5155 // actually wrote the specialization, rather than formatting the
5156 // name based on the "canonical" representation used to store the
5157 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005158 TypeSourceInfo *WrittenTy
5159 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5160 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005161 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005162 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005163 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005164 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005165 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005166
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005167 // C++ [temp.expl.spec]p9:
5168 // A template explicit specialization is in the scope of the
5169 // namespace in which the template was defined.
5170 //
5171 // We actually implement this paragraph where we set the semantic
5172 // context (in the creation of the ClassTemplateSpecializationDecl),
5173 // but we also maintain the lexical context where the actual
5174 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005175 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005176
Douglas Gregorcc636682009-02-17 23:15:12 +00005177 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005178 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005179 Specialization->startDefinition();
5180
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005181 if (TUK == TUK_Friend) {
5182 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5183 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005184 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005185 /*FIXME:*/KWLoc);
5186 Friend->setAccess(AS_public);
5187 CurContext->addDecl(Friend);
5188 } else {
5189 // Add the specialization into its lexical context, so that it can
5190 // be seen when iterating through the list of declarations in that
5191 // context. However, specializations are not found by name lookup.
5192 CurContext->addDecl(Specialization);
5193 }
John McCalld226f652010-08-21 09:40:31 +00005194 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005195}
Douglas Gregord57959a2009-03-27 23:10:48 +00005196
John McCalld226f652010-08-21 09:40:31 +00005197Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005198 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005199 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005200 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005201}
5202
John McCalld226f652010-08-21 09:40:31 +00005203Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005204 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005205 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005206 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005207 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005208
Douglas Gregor52591bf2009-06-24 00:54:41 +00005209 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005210 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005211 }
Mike Stump1eb44332009-09-09 15:08:12 +00005212
Douglas Gregor52591bf2009-06-24 00:54:41 +00005213 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005214
Douglas Gregor45fa5602011-11-07 20:56:01 +00005215 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005216 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005217 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005218 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005219 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005220 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005221 FunctionTemplate->getTemplatedDecl());
5222 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5223 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5224 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005225}
5226
John McCall75042392010-02-11 01:33:53 +00005227/// \brief Strips various properties off an implicit instantiation
5228/// that has just been explicitly specialized.
5229static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005230 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005231
5232 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5233 FD->setInlineSpecified(false);
5234 }
5235}
5236
Nico Weberd1d512a2012-01-09 19:52:25 +00005237/// \brief Compute the diagnostic location for an explicit instantiation
5238// declaration or definition.
5239static SourceLocation DiagLocForExplicitInstantiation(
5240 NamedDecl* Decl, SourceLocation PointOfInstantiation) {
5241 // Explicit instantiations following a specialization have no effect and
5242 // hence no PointOfInstantiation. In that case, walk decl backwards
5243 // until a valid name loc is found.
5244 SourceLocation PrevDiagLoc = PointOfInstantiation;
5245 for (NamedDecl *Prev = Decl; Prev && !PrevDiagLoc.isValid();
5246 Prev = getPreviousDecl(Prev)) {
5247 PrevDiagLoc = Prev->getLocation();
5248 }
5249 assert(PrevDiagLoc.isValid() &&
5250 "Explicit instantiation without point of instantiation?");
5251 return PrevDiagLoc;
5252}
5253
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005254/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005255/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005256/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005257/// new specialization/instantiation will have any effect.
5258///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005259/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005260/// instantiation.
5261///
5262/// \param NewTSK the kind of the new explicit specialization or instantiation.
5263///
5264/// \param PrevDecl the previous declaration of the entity.
5265///
5266/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5267///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005269/// declaration was instantiated (either implicitly or explicitly).
5270///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005271/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005272/// specialization or instantiation has no effect and should be ignored.
5273///
5274/// \returns true if there was an error that should prevent the introduction of
5275/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005276bool
5277Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5278 TemplateSpecializationKind NewTSK,
5279 NamedDecl *PrevDecl,
5280 TemplateSpecializationKind PrevTSK,
5281 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005282 bool &HasNoEffect) {
5283 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 switch (NewTSK) {
5286 case TSK_Undeclared:
5287 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005288 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005289
Douglas Gregor454885e2009-10-15 15:54:05 +00005290 case TSK_ExplicitSpecialization:
5291 switch (PrevTSK) {
5292 case TSK_Undeclared:
5293 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005294 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005295 // explicitly specialized or has merely been mentioned without any
5296 // instantiation.
5297 return false;
5298
5299 case TSK_ImplicitInstantiation:
5300 if (PrevPointOfInstantiation.isInvalid()) {
5301 // The declaration itself has not actually been instantiated, so it is
5302 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005303 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005304 return false;
5305 }
5306 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307
Douglas Gregor454885e2009-10-15 15:54:05 +00005308 case TSK_ExplicitInstantiationDeclaration:
5309 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310 assert((PrevTSK == TSK_ImplicitInstantiation ||
5311 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005312 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005313
Douglas Gregor454885e2009-10-15 15:54:05 +00005314 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005316 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005317 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005318 // implicit instantiation to take place, in every translation unit in
5319 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005320 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5321 // Is there any previous explicit specialization declaration?
5322 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5323 return false;
5324 }
5325
Douglas Gregor0d035142009-10-27 18:42:08 +00005326 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005327 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005328 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005329 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005330
Douglas Gregor454885e2009-10-15 15:54:05 +00005331 return true;
5332 }
5333 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005334
Douglas Gregor454885e2009-10-15 15:54:05 +00005335 case TSK_ExplicitInstantiationDeclaration:
5336 switch (PrevTSK) {
5337 case TSK_ExplicitInstantiationDeclaration:
5338 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005339 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005340 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 case TSK_Undeclared:
5343 case TSK_ImplicitInstantiation:
5344 // We're explicitly instantiating something that may have already been
5345 // implicitly instantiated; that's fine.
5346 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005347
Douglas Gregor454885e2009-10-15 15:54:05 +00005348 case TSK_ExplicitSpecialization:
5349 // C++0x [temp.explicit]p4:
5350 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005351 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005352 // specialization for that template, the explicit instantiation has no
5353 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005354 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005355 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356
Douglas Gregor454885e2009-10-15 15:54:05 +00005357 case TSK_ExplicitInstantiationDefinition:
5358 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005359 // If an entity is the subject of both an explicit instantiation
5360 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005361 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005362 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005363 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005364
5365 // Explicit instantiations following a specialization have no effect and
5366 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5367 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005368 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5369 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005370 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005371 return false;
5372 }
5373 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005374
Douglas Gregor454885e2009-10-15 15:54:05 +00005375 case TSK_ExplicitInstantiationDefinition:
5376 switch (PrevTSK) {
5377 case TSK_Undeclared:
5378 case TSK_ImplicitInstantiation:
5379 // We're explicitly instantiating something that may have already been
5380 // implicitly instantiated; that's fine.
5381 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005382
Douglas Gregor454885e2009-10-15 15:54:05 +00005383 case TSK_ExplicitSpecialization:
5384 // C++ DR 259, C++0x [temp.explicit]p4:
5385 // For a given set of template parameters, if an explicit
5386 // instantiation of a template appears after a declaration of
5387 // an explicit specialization for that template, the explicit
5388 // instantiation has no effect.
5389 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005390 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005391 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005392 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005393 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5394 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5395 diag::ext_explicit_instantiation_after_specialization)
5396 << PrevDecl;
5397 Diag(PrevDecl->getLocation(),
5398 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005399 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005400 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005401
Douglas Gregor454885e2009-10-15 15:54:05 +00005402 case TSK_ExplicitInstantiationDeclaration:
5403 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005405
5406 // C++0x [temp.explicit]p4:
5407 // For a given set of template parameters, if an explicit instantiation
5408 // of a template appears after a declaration of an explicit
5409 // specialization for that template, the explicit instantiation has no
5410 // effect.
5411 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5412 // Is there any previous explicit specialization declaration?
5413 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5414 HasNoEffect = true;
5415 break;
5416 }
5417 }
5418
Douglas Gregor454885e2009-10-15 15:54:05 +00005419 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005420
Douglas Gregor454885e2009-10-15 15:54:05 +00005421 case TSK_ExplicitInstantiationDefinition:
5422 // C++0x [temp.spec]p5:
5423 // For a given template and a given set of template-arguments,
5424 // - an explicit instantiation definition shall appear at most once
5425 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005426 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005427 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005428 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005429 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005430 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005431 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005432 }
5433 break;
5434 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005435
David Blaikieb219cfc2011-09-23 05:06:16 +00005436 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005437}
5438
John McCallaf2094e2010-04-08 09:05:18 +00005439/// \brief Perform semantic analysis for the given dependent function
5440/// template specialization. The only possible way to get a dependent
5441/// function template specialization is with a friend declaration,
5442/// like so:
5443///
5444/// template <class T> void foo(T);
5445/// template <class T> class A {
5446/// friend void foo<>(T);
5447/// };
5448///
5449/// There really isn't any useful analysis we can do here, so we
5450/// just store the information.
5451bool
5452Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5453 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5454 LookupResult &Previous) {
5455 // Remove anything from Previous that isn't a function template in
5456 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005457 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005458 LookupResult::Filter F = Previous.makeFilter();
5459 while (F.hasNext()) {
5460 NamedDecl *D = F.next()->getUnderlyingDecl();
5461 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005462 !FDLookupContext->InEnclosingNamespaceSetOf(
5463 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005464 F.erase();
5465 }
5466 F.done();
5467
5468 // Should this be diagnosed here?
5469 if (Previous.empty()) return true;
5470
5471 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5472 ExplicitTemplateArgs);
5473 return false;
5474}
5475
Abramo Bagnarae03db982010-05-20 15:32:11 +00005476/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005477/// specialization.
5478///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005479/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005480/// explicit function template specialization. On successful completion,
5481/// the function declaration \p FD will become a function template
5482/// specialization.
5483///
5484/// \param FD the function declaration, which will be updated to become a
5485/// function template specialization.
5486///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005487/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5488/// if any. Note that this may be valid info even when 0 arguments are
5489/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5490/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005491///
Francois Pichet59e7c562011-07-08 06:21:47 +00005492/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005493/// this function specialization.
5494bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005495Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005496 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005497 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005498 // The set of function template specializations that could match this
5499 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005500 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005501
Sebastian Redl7a126a42010-08-31 00:36:30 +00005502 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005503 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5504 I != E; ++I) {
5505 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5506 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005507 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005508 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005509 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5510 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005511 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005512
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005513 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005514 // A trailing template-argument can be left unspecified in the
5515 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005516 // provided it can be deduced from the function argument type.
5517 // Perform template argument deduction to determine whether we may be
5518 // specializing this template.
5519 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005520 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005521 FunctionDecl *Specialization = 0;
5522 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005523 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005524 FD->getType(),
5525 Specialization,
5526 Info)) {
5527 // FIXME: Template argument deduction failed; record why it failed, so
5528 // that we can provide nifty diagnostics.
5529 (void)TDK;
5530 continue;
5531 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005532
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005533 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005534 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005535 }
5536 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005537
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005538 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005539 UnresolvedSetIterator Result
5540 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005541 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005542 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005543 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005544 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005545 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005546 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005547 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005548 return true;
John McCallc373d482010-01-27 01:50:18 +00005549
5550 // Ignore access information; it doesn't figure into redeclaration checking.
5551 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005552
5553 FunctionTemplateSpecializationInfo *SpecInfo
5554 = Specialization->getTemplateSpecializationInfo();
5555 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005556
5557 // Note: do not overwrite location info if previous template
5558 // specialization kind was explicit.
5559 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5560 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5561 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005562
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005563 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005564 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005565
5566 // If this is a friend declaration, then we're not really declaring
5567 // an explicit specialization.
5568 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005569
Douglas Gregord5cb8762009-10-07 00:13:32 +00005570 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005571 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005572 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005573 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005574 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005575 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005576 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005577
5578 // C++ [temp.expl.spec]p6:
5579 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005580 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005581 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005582 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005583 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005584 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005585 if (!isFriend &&
5586 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005587 TSK_ExplicitSpecialization,
5588 Specialization,
5589 SpecInfo->getTemplateSpecializationKind(),
5590 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005591 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005592 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005593
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005594 // Mark the prior declaration as an explicit specialization, so that later
5595 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005596 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005597 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005598 MarkUnusedFileScopedDecl(Specialization);
5599 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005600
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005601 // Turn the given function declaration into a function template
5602 // specialization, with the template arguments from the previous
5603 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005604 // Take copies of (semantic and syntactic) template argument lists.
5605 const TemplateArgumentList* TemplArgs = new (Context)
5606 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005607 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005608 TemplArgs, /*InsertPos=*/0,
5609 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005610 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005611 FD->setStorageClass(Specialization->getStorageClass());
5612
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005613 // The "previous declaration" for this function template specialization is
5614 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005615 Previous.clear();
5616 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005617 return false;
5618}
5619
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005620/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005621/// specialization.
5622///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005623/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005624/// explicit member function specialization. On successful completion,
5625/// the function declaration \p FD will become a member function
5626/// specialization.
5627///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005628/// \param Member the member declaration, which will be updated to become a
5629/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005630///
John McCall68263142009-11-18 22:49:29 +00005631/// \param Previous the set of declarations, one of which may be specialized
5632/// by this function specialization; the set will be modified to contain the
5633/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005634bool
John McCall68263142009-11-18 22:49:29 +00005635Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005636 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005637
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005638 // Try to find the member we are instantiating.
5639 NamedDecl *Instantiation = 0;
5640 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005641 MemberSpecializationInfo *MSInfo = 0;
5642
John McCall68263142009-11-18 22:49:29 +00005643 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005644 // Nowhere to look anyway.
5645 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005646 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5647 I != E; ++I) {
5648 NamedDecl *D = (*I)->getUnderlyingDecl();
5649 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005650 if (Context.hasSameType(Function->getType(), Method->getType())) {
5651 Instantiation = Method;
5652 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005653 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005654 break;
5655 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005656 }
5657 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005658 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005659 VarDecl *PrevVar;
5660 if (Previous.isSingleResult() &&
5661 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005662 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005663 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005664 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005665 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005666 }
5667 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005668 CXXRecordDecl *PrevRecord;
5669 if (Previous.isSingleResult() &&
5670 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5671 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005673 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005674 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005677 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005678 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005679 // specializations are always out-of-line, the caller will complain about
5680 // this mismatch later.
5681 return false;
5682 }
John McCall77e8b112010-04-13 20:37:33 +00005683
5684 // If this is a friend, just bail out here before we start turning
5685 // things into explicit specializations.
5686 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5687 // Preserve instantiation information.
5688 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5689 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5690 cast<CXXMethodDecl>(InstantiatedFrom),
5691 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5692 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5693 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5694 cast<CXXRecordDecl>(InstantiatedFrom),
5695 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5696 }
5697
5698 Previous.clear();
5699 Previous.addDecl(Instantiation);
5700 return false;
5701 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005703 // Make sure that this is a specialization of a member.
5704 if (!InstantiatedFrom) {
5705 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5706 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005707 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5708 return true;
5709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005711 // C++ [temp.expl.spec]p6:
5712 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005713 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005714 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005716 // use occurs; no diagnostic is required.
5717 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005718
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005719 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005720 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5721 TSK_ExplicitSpecialization,
5722 Instantiation,
5723 MSInfo->getTemplateSpecializationKind(),
5724 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005725 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005726 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005727
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005728 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005729 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005730 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005731 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005732 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005733 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005734
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005735 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005736 // the original declaration to note that it is an explicit specialization
5737 // (if it was previously an implicit instantiation). This latter step
5738 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005739 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005740 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5741 if (InstantiationFunction->getTemplateSpecializationKind() ==
5742 TSK_ImplicitInstantiation) {
5743 InstantiationFunction->setTemplateSpecializationKind(
5744 TSK_ExplicitSpecialization);
5745 InstantiationFunction->setLocation(Member->getLocation());
5746 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005747
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005748 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5749 cast<CXXMethodDecl>(InstantiatedFrom),
5750 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005751 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005752 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005753 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5754 if (InstantiationVar->getTemplateSpecializationKind() ==
5755 TSK_ImplicitInstantiation) {
5756 InstantiationVar->setTemplateSpecializationKind(
5757 TSK_ExplicitSpecialization);
5758 InstantiationVar->setLocation(Member->getLocation());
5759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005760
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005761 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5762 cast<VarDecl>(InstantiatedFrom),
5763 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005764 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005765 } else {
5766 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005767 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5768 if (InstantiationClass->getTemplateSpecializationKind() ==
5769 TSK_ImplicitInstantiation) {
5770 InstantiationClass->setTemplateSpecializationKind(
5771 TSK_ExplicitSpecialization);
5772 InstantiationClass->setLocation(Member->getLocation());
5773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005774
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005775 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005776 cast<CXXRecordDecl>(InstantiatedFrom),
5777 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005779
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005780 // Save the caller the trouble of having to figure out which declaration
5781 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005782 Previous.clear();
5783 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005784 return false;
5785}
5786
Douglas Gregor558c0322009-10-14 23:41:34 +00005787/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005788///
5789/// \returns true if a serious error occurs, false otherwise.
5790static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005791 SourceLocation InstLoc,
5792 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005793 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5794 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795
Douglas Gregor669eed82010-07-13 00:10:04 +00005796 if (CurContext->isRecord()) {
5797 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5798 << D;
5799 return true;
5800 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005801
Richard Smith3e2e91e2011-10-18 02:28:33 +00005802 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005803 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005804 // template. If the name declared in the explicit instantiation is an
5805 // unqualified name, the explicit instantiation shall appear in the
5806 // namespace where its template is declared or, if that namespace is inline
5807 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005808 //
5809 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005810 if (WasQualifiedName) {
5811 if (CurContext->Encloses(OrigContext))
5812 return false;
5813 } else {
5814 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5815 return false;
5816 }
5817
5818 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5819 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005820 S.Diag(InstLoc,
5821 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005822 diag::err_explicit_instantiation_out_of_scope :
5823 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005824 << D << NS;
5825 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005826 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005827 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005828 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5829 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5830 << D << NS;
5831 } else
5832 S.Diag(InstLoc,
5833 S.getLangOptions().CPlusPlus0x?
5834 diag::err_explicit_instantiation_must_be_global :
5835 diag::warn_explicit_instantiation_must_be_global_0x)
5836 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005837 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005838 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005839}
5840
5841/// \brief Determine whether the given scope specifier has a template-id in it.
5842static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5843 if (!SS.isSet())
5844 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005845
Richard Smith3e2e91e2011-10-18 02:28:33 +00005846 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005847 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005848 // or a static data member of a class template specialization, the name of
5849 // the class template specialization in the qualified-id for the member
5850 // name shall be a simple-template-id.
5851 //
5852 // C++98 has the same restriction, just worded differently.
5853 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5854 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005855 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005856 if (isa<TemplateSpecializationType>(T))
5857 return true;
5858
5859 return false;
5860}
5861
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005862// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005863DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005864Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005865 SourceLocation ExternLoc,
5866 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005867 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005868 SourceLocation KWLoc,
5869 const CXXScopeSpec &SS,
5870 TemplateTy TemplateD,
5871 SourceLocation TemplateNameLoc,
5872 SourceLocation LAngleLoc,
5873 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005874 SourceLocation RAngleLoc,
5875 AttributeList *Attr) {
5876 // Find the class template we're specializing
5877 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005878 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005879 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5880
5881 // Check that the specialization uses the same tag kind as the
5882 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005883 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5884 assert(Kind != TTK_Enum &&
5885 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005886 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005887 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005888 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005889 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005890 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005891 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005892 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005893 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005894 diag::note_previous_use);
5895 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5896 }
5897
Douglas Gregor558c0322009-10-14 23:41:34 +00005898 // C++0x [temp.explicit]p2:
5899 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005900 // definition and an explicit instantiation declaration. An explicit
5901 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005902 TemplateSpecializationKind TSK
5903 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5904 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005905
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005906 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005907 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005908 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005909
5910 // Check that the template argument list is well-formed for this
5911 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005912 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005913 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5914 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005915 return true;
5916
Douglas Gregor910f8002010-11-07 23:05:16 +00005917 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005918 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005919
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005920 // Find the class template specialization declaration that
5921 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005922 void *InsertPos = 0;
5923 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005924 = ClassTemplate->findSpecialization(Converted.data(),
5925 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005926
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005927 TemplateSpecializationKind PrevDecl_TSK
5928 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5929
Douglas Gregord5cb8762009-10-07 00:13:32 +00005930 // C++0x [temp.explicit]p2:
5931 // [...] An explicit instantiation shall appear in an enclosing
5932 // namespace of its template. [...]
5933 //
5934 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005935 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5936 SS.isSet()))
5937 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005938
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005939 ClassTemplateSpecializationDecl *Specialization = 0;
5940
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005941 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005942 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005943 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005944 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005945 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005946 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005947 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005948
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005949 // Even though HasNoEffect == true means that this explicit instantiation
5950 // has no effect on semantics, we go on to put its syntax in the AST.
5951
5952 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5953 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005954 // Since the only prior class template specialization with these
5955 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005956 // declaration node as our own, updating the source location
5957 // for the template name to reflect our new declaration.
5958 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005959 Specialization = PrevDecl;
5960 Specialization->setLocation(TemplateNameLoc);
5961 PrevDecl = 0;
5962 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005963 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005964
Douglas Gregor52604ab2009-09-11 21:19:12 +00005965 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005966 // Create a new class template specialization declaration node for
5967 // this explicit specialization.
5968 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005969 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005970 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005971 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005972 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005973 Converted.data(),
5974 Converted.size(),
5975 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005976 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005977
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005978 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005979 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005980 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005981 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005982 }
5983
5984 // Build the fully-sugared type for this explicit instantiation as
5985 // the user wrote in the explicit instantiation itself. This means
5986 // that we'll pretty-print the type retrieved from the
5987 // specialization's declaration the way that the user actually wrote
5988 // the explicit instantiation, rather than formatting the name based
5989 // on the "canonical" representation used to store the template
5990 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005991 TypeSourceInfo *WrittenTy
5992 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5993 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005994 Context.getTypeDeclType(Specialization));
5995 Specialization->setTypeAsWritten(WrittenTy);
5996 TemplateArgsIn.release();
5997
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005998 // Set source locations for keywords.
5999 Specialization->setExternLoc(ExternLoc);
6000 Specialization->setTemplateKeywordLoc(TemplateLoc);
6001
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006002 if (Attr)
6003 ProcessDeclAttributeList(S, Specialization, Attr);
6004
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006005 // Add the explicit instantiation into its lexical context. However,
6006 // since explicit instantiations are never found by name lookup, we
6007 // just put it into the declaration context directly.
6008 Specialization->setLexicalDeclContext(CurContext);
6009 CurContext->addDecl(Specialization);
6010
6011 // Syntax is now OK, so return if it has no other effect on semantics.
6012 if (HasNoEffect) {
6013 // Set the template specialization kind.
6014 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006015 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006016 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006017
6018 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006019 // A definition of a class template or class member template
6020 // shall be in scope at the point of the explicit instantiation of
6021 // the class template or class member template.
6022 //
6023 // This check comes when we actually try to perform the
6024 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006025 ClassTemplateSpecializationDecl *Def
6026 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006027 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006028 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006029 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006030 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006031 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006032 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6033 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006034
Douglas Gregor0d035142009-10-27 18:42:08 +00006035 // Instantiate the members of this class template specialization.
6036 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006037 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006038 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006039 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6040
6041 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6042 // TSK_ExplicitInstantiationDefinition
6043 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6044 TSK == TSK_ExplicitInstantiationDefinition)
6045 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006046
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006047 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006048 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006049
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006050 // Set the template specialization kind.
6051 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006052 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006053}
6054
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006055// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006056DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006057Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006058 SourceLocation ExternLoc,
6059 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006060 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006061 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006062 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006063 IdentifierInfo *Name,
6064 SourceLocation NameLoc,
6065 AttributeList *Attr) {
6066
Douglas Gregor402abb52009-05-28 23:31:59 +00006067 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006068 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006069 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006070 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006071 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006072 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006073 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006074 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006075 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6076
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006077 if (!TagD)
6078 return true;
6079
John McCalld226f652010-08-21 09:40:31 +00006080 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006081 if (Tag->isEnum()) {
6082 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6083 << Context.getTypeDeclType(Tag);
6084 return true;
6085 }
6086
Douglas Gregord0c87372009-05-27 17:30:49 +00006087 if (Tag->isInvalidDecl())
6088 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006089
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006090 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6091 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6092 if (!Pattern) {
6093 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6094 << Context.getTypeDeclType(Record);
6095 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6096 return true;
6097 }
6098
Douglas Gregor558c0322009-10-14 23:41:34 +00006099 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006100 // If the explicit instantiation is for a class or member class, the
6101 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006102 // simple-template-id.
6103 //
6104 // C++98 has the same restriction, just worded differently.
6105 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006106 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006107 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006108
Douglas Gregor558c0322009-10-14 23:41:34 +00006109 // C++0x [temp.explicit]p2:
6110 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006111 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006112 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006113 TemplateSpecializationKind TSK
6114 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6115 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006116
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006117 // C++0x [temp.explicit]p2:
6118 // [...] An explicit instantiation shall appear in an enclosing
6119 // namespace of its template. [...]
6120 //
6121 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006122 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006123
Douglas Gregor454885e2009-10-15 15:54:05 +00006124 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006125 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006126 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006127 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006128 PrevDecl = Record;
6129 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006130 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006131 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006132 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006133 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006134 PrevDecl,
6135 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006136 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006137 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006138 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006139 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006140 return TagD;
6141 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006142
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006143 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006144 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006145 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006146 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006147 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006148 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006149 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006150 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006151 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006152 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6153 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006154 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6155 << Pattern;
6156 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006157 } else {
6158 if (InstantiateClass(NameLoc, Record, Def,
6159 getTemplateInstantiationArgs(Record),
6160 TSK))
6161 return true;
6162
Douglas Gregor952b0172010-02-11 01:04:33 +00006163 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006164 if (!RecordDef)
6165 return true;
6166 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006167 }
6168
Douglas Gregor0d035142009-10-27 18:42:08 +00006169 // Instantiate all of the members of the class.
6170 InstantiateClassMembers(NameLoc, RecordDef,
6171 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006172
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006173 if (TSK == TSK_ExplicitInstantiationDefinition)
6174 MarkVTableUsed(NameLoc, RecordDef, true);
6175
Mike Stump390b4cc2009-05-16 07:39:55 +00006176 // FIXME: We don't have any representation for explicit instantiations of
6177 // member classes. Such a representation is not needed for compilation, but it
6178 // should be available for clients that want to see all of the declarations in
6179 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006180 return TagD;
6181}
6182
John McCallf312b1e2010-08-26 23:41:50 +00006183DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6184 SourceLocation ExternLoc,
6185 SourceLocation TemplateLoc,
6186 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006187 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006188 // TODO: check if/when DNInfo should replace Name.
6189 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6190 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006191 if (!Name) {
6192 if (!D.isInvalidType())
6193 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6194 diag::err_explicit_instantiation_requires_name)
6195 << D.getDeclSpec().getSourceRange()
6196 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006197
Douglas Gregord5a423b2009-09-25 18:43:00 +00006198 return true;
6199 }
6200
6201 // The scope passed in may not be a decl scope. Zip up the scope tree until
6202 // we find one that is.
6203 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6204 (S->getFlags() & Scope::TemplateParamScope) != 0)
6205 S = S->getParent();
6206
6207 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006208 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6209 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006210 if (R.isNull())
6211 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006212
Douglas Gregore885e182011-05-21 18:53:30 +00006213 // C++ [dcl.stc]p1:
6214 // A storage-class-specifier shall not be specified in [...] an explicit
6215 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006216 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006217 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6218 << Name;
6219 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006220 } else if (D.getDeclSpec().getStorageClassSpec()
6221 != DeclSpec::SCS_unspecified) {
6222 // Complain about then remove the storage class specifier.
6223 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6224 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6225
6226 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006227 }
6228
Douglas Gregor663b5a02009-10-14 20:14:33 +00006229 // C++0x [temp.explicit]p1:
6230 // [...] An explicit instantiation of a function template shall not use the
6231 // inline or constexpr specifiers.
6232 // Presumably, this also applies to member functions of class templates as
6233 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006234 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006236 getLangOptions().CPlusPlus0x ?
6237 diag::err_explicit_instantiation_inline :
6238 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006239 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6240 if (D.getDeclSpec().isConstexprSpecified())
6241 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6242 // not already specified.
6243 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6244 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006245
Douglas Gregor558c0322009-10-14 23:41:34 +00006246 // C++0x [temp.explicit]p2:
6247 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006248 // definition and an explicit instantiation declaration. An explicit
6249 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006250 TemplateSpecializationKind TSK
6251 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6252 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253
Abramo Bagnara25777432010-08-11 22:01:17 +00006254 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006255 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006256
6257 if (!R->isFunctionType()) {
6258 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006259 // A [...] static data member of a class template can be explicitly
6260 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006261 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006262 if (Previous.isAmbiguous())
6263 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006264
John McCall1bcee0a2009-12-02 08:25:40 +00006265 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006266 if (!Prev || !Prev->isStaticDataMember()) {
6267 // We expect to see a data data member here.
6268 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6269 << Name;
6270 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6271 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006272 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006273 return true;
6274 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006275
Douglas Gregord5a423b2009-09-25 18:43:00 +00006276 if (!Prev->getInstantiatedFromStaticDataMember()) {
6277 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006279 diag::err_explicit_instantiation_data_member_not_instantiated)
6280 << Prev;
6281 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6282 // FIXME: Can we provide a note showing where this was declared?
6283 return true;
6284 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006285
Douglas Gregor558c0322009-10-14 23:41:34 +00006286 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006287 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006288 // or a static data member of a class template specialization, the name of
6289 // the class template specialization in the qualified-id for the member
6290 // name shall be a simple-template-id.
6291 //
6292 // C++98 has the same restriction, just worded differently.
6293 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006295 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006296 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006297
Douglas Gregor558c0322009-10-14 23:41:34 +00006298 // Check the scope of this explicit instantiation.
6299 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006300
Douglas Gregor454885e2009-10-15 15:54:05 +00006301 // Verify that it is okay to explicitly instantiate here.
6302 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6303 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006304 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006305 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006306 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006307 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006308 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006309 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006310 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006311 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006312
Douglas Gregord5a423b2009-09-25 18:43:00 +00006313 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006314 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006315 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006316 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006317
Douglas Gregord5a423b2009-09-25 18:43:00 +00006318 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006319 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006320 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006321
6322 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006323 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006324 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006325 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006326 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6327 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006328 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6329 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006330 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6331 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006332 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006333 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006334 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006335 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006336 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337
Douglas Gregord5a423b2009-09-25 18:43:00 +00006338 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006339 // A [...] function [...] can be explicitly instantiated from its template.
6340 // A member function [...] of a class template can be explicitly
6341 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006342 // template.
John McCallc373d482010-01-27 01:50:18 +00006343 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006344 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6345 P != PEnd; ++P) {
6346 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006347 if (!HasExplicitTemplateArgs) {
6348 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6349 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6350 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006351
John McCallc373d482010-01-27 01:50:18 +00006352 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006353 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6354 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006355 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006356 }
6357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006358
Douglas Gregord5a423b2009-09-25 18:43:00 +00006359 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6360 if (!FunTmpl)
6361 continue;
6362
John McCall5769d612010-02-08 23:07:23 +00006363 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006364 FunctionDecl *Specialization = 0;
6365 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006366 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006367 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006368 R, Specialization, Info)) {
6369 // FIXME: Keep track of almost-matches?
6370 (void)TDK;
6371 continue;
6372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006373
John McCallc373d482010-01-27 01:50:18 +00006374 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006375 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006376
Douglas Gregord5a423b2009-09-25 18:43:00 +00006377 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006378 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006379 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006380 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006381 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6382 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6383 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006384
John McCallc373d482010-01-27 01:50:18 +00006385 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006386 return true;
John McCallc373d482010-01-27 01:50:18 +00006387
6388 // Ignore access control bits, we don't need them for redeclaration checking.
6389 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006390
Douglas Gregor0a897e32009-10-15 17:21:20 +00006391 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006392 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006393 diag::err_explicit_instantiation_member_function_not_instantiated)
6394 << Specialization
6395 << (Specialization->getTemplateSpecializationKind() ==
6396 TSK_ExplicitSpecialization);
6397 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6398 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006399 }
6400
Douglas Gregor0a897e32009-10-15 17:21:20 +00006401 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006402 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6403 PrevDecl = Specialization;
6404
Douglas Gregor0a897e32009-10-15 17:21:20 +00006405 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006406 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006407 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006408 PrevDecl,
6409 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006410 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006411 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006412 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006413
Douglas Gregor0a897e32009-10-15 17:21:20 +00006414 // FIXME: We may still want to build some representation of this
6415 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006416 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006417 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006418 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006419
6420 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006421 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6422 if (Attr)
6423 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006424
Douglas Gregor0a897e32009-10-15 17:21:20 +00006425 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006426 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006427
Douglas Gregor558c0322009-10-14 23:41:34 +00006428 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006429 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006430 // or a static data member of a class template specialization, the name of
6431 // the class template specialization in the qualified-id for the member
6432 // name shall be a simple-template-id.
6433 //
6434 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006435 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006436 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006437 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006438 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006439 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006440 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006441 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006442
Douglas Gregor558c0322009-10-14 23:41:34 +00006443 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006444 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006445 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006446 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006447 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006448
Douglas Gregord5a423b2009-09-25 18:43:00 +00006449 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006450 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006451}
6452
John McCallf312b1e2010-08-26 23:41:50 +00006453TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006454Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6455 const CXXScopeSpec &SS, IdentifierInfo *Name,
6456 SourceLocation TagLoc, SourceLocation NameLoc) {
6457 // This has to hold, because SS is expected to be defined.
6458 assert(Name && "Expected a name in a dependent tag");
6459
6460 NestedNameSpecifier *NNS
6461 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6462 if (!NNS)
6463 return true;
6464
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006465 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006466
Douglas Gregor48c89f42010-04-24 16:38:41 +00006467 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6468 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006469 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006470 return true;
6471 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006472
Douglas Gregor059101f2011-03-02 00:47:37 +00006473 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006474 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006475 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6476
6477 // Create type-source location information for this type.
6478 TypeLocBuilder TLB;
6479 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6480 TL.setKeywordLoc(TagLoc);
6481 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6482 TL.setNameLoc(NameLoc);
6483 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006484}
6485
John McCallf312b1e2010-08-26 23:41:50 +00006486TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006487Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6488 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006489 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006490 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006491 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006492
Richard Smithebaf0e62011-10-18 20:49:44 +00006493 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6494 Diag(TypenameLoc,
6495 getLangOptions().CPlusPlus0x ?
6496 diag::warn_cxx98_compat_typename_outside_of_template :
6497 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006498 << FixItHint::CreateRemoval(TypenameLoc);
6499
Douglas Gregor2494dd02011-03-01 01:34:45 +00006500 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006501 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6502 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006503 if (T.isNull())
6504 return true;
John McCall63b43852010-04-29 23:50:39 +00006505
6506 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6507 if (isa<DependentNameType>(T)) {
6508 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006509 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006510 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006511 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006512 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006513 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006514 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006515 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006516 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006517 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006518
John McCallb3d87482010-08-24 05:47:05 +00006519 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006520}
6521
John McCallf312b1e2010-08-26 23:41:50 +00006522TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006523Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6524 const CXXScopeSpec &SS,
6525 SourceLocation TemplateLoc,
6526 TemplateTy TemplateIn,
6527 SourceLocation TemplateNameLoc,
6528 SourceLocation LAngleLoc,
6529 ASTTemplateArgsPtr TemplateArgsIn,
6530 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006531 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6532 Diag(TypenameLoc,
6533 getLangOptions().CPlusPlus0x ?
6534 diag::warn_cxx98_compat_typename_outside_of_template :
6535 diag::ext_typename_outside_of_template)
6536 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006537
6538 // Translate the parser's template argument list in our AST format.
6539 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6540 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6541
6542 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006543 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6544 // Construct a dependent template specialization type.
6545 assert(DTN && "dependent template has non-dependent name?");
6546 assert(DTN->getQualifier()
6547 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6548 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6549 DTN->getQualifier(),
6550 DTN->getIdentifier(),
6551 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006552
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006553 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006554 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006555 DependentTemplateSpecializationTypeLoc SpecTL
6556 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006557 SpecTL.setLAngleLoc(LAngleLoc);
6558 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006559 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006560 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006561 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006562 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6563 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006564 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006565 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006566
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006567 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6568 if (T.isNull())
6569 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006570
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006571 // Provide source-location information for the template specialization
6572 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006573 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006574 TemplateSpecializationTypeLoc SpecTL
6575 = Builder.push<TemplateSpecializationTypeLoc>(T);
6576
6577 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006578 SpecTL.setLAngleLoc(LAngleLoc);
6579 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006580 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006581 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6582 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6583
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006584 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6585 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6586 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006587 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6588
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006589 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6590 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006591}
6592
Douglas Gregora02411e2011-02-27 22:46:49 +00006593
Douglas Gregord57959a2009-03-27 23:10:48 +00006594/// \brief Build the type that describes a C++ typename specifier,
6595/// e.g., "typename T::type".
6596QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006597Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6598 SourceLocation KeywordLoc,
6599 NestedNameSpecifierLoc QualifierLoc,
6600 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006601 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006602 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006603 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006604
John McCall77bb1aa2010-05-01 00:40:08 +00006605 DeclContext *Ctx = computeDeclContext(SS);
6606 if (!Ctx) {
6607 // If the nested-name-specifier is dependent and couldn't be
6608 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006609 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6610 return Context.getDependentNameType(Keyword,
6611 QualifierLoc.getNestedNameSpecifier(),
6612 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006613 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006614
John McCall77bb1aa2010-05-01 00:40:08 +00006615 // If the nested-name-specifier refers to the current instantiation,
6616 // the "typename" keyword itself is superfluous. In C++03, the
6617 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6618 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006619 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006620
John McCall77bb1aa2010-05-01 00:40:08 +00006621 if (RequireCompleteDeclContext(SS, Ctx))
6622 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006623
6624 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006625 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006626 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006627 unsigned DiagID = 0;
6628 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006629 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006630 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006631 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006632 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006633
6634 case LookupResult::FoundUnresolvedValue: {
6635 // We found a using declaration that is a value. Most likely, the using
6636 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006637 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006638 IILoc);
6639 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6640 << Name << Ctx << FullRange;
6641 if (UnresolvedUsingValueDecl *Using
6642 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006643 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006644 Diag(Loc, diag::note_using_value_decl_missing_typename)
6645 << FixItHint::CreateInsertion(Loc, "typename ");
6646 }
6647 }
6648 // Fall through to create a dependent typename type, from which we can recover
6649 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006650
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006651 case LookupResult::NotFoundInCurrentInstantiation:
6652 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006653 return Context.getDependentNameType(Keyword,
6654 QualifierLoc.getNestedNameSpecifier(),
6655 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006656
6657 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006658 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006659 // We found a type. Build an ElaboratedType, since the
6660 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006661 return Context.getElaboratedType(ETK_Typename,
6662 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006663 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006664 }
6665
6666 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006667 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006668 break;
6669
6670 case LookupResult::FoundOverloaded:
6671 DiagID = diag::err_typename_nested_not_type;
6672 Referenced = *Result.begin();
6673 break;
6674
John McCall6e247262009-10-10 05:48:19 +00006675 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006676 return QualType();
6677 }
6678
6679 // If we get here, it's because name lookup did not find a
6680 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006681 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006682 IILoc);
6683 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006684 if (Referenced)
6685 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6686 << Name;
6687 return QualType();
6688}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006689
6690namespace {
6691 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006692 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006693 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006694 SourceLocation Loc;
6695 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006696
Douglas Gregor4a959d82009-08-06 16:20:37 +00006697 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006698 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006699
Mike Stump1eb44332009-09-09 15:08:12 +00006700 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006701 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006702 DeclarationName Entity)
6703 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006704 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006705
6706 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006707 /// transformed.
6708 ///
6709 /// For the purposes of type reconstruction, a type has already been
6710 /// transformed if it is NULL or if it is not dependent.
6711 bool AlreadyTransformed(QualType T) {
6712 return T.isNull() || !T->isDependentType();
6713 }
Mike Stump1eb44332009-09-09 15:08:12 +00006714
6715 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006716 /// rebuilt.
6717 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006718
Douglas Gregor4a959d82009-08-06 16:20:37 +00006719 /// \brief Returns the name of the entity whose type is being rebuilt.
6720 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006721
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006722 /// \brief Sets the "base" location and entity when that
6723 /// information is known based on another transformation.
6724 void setBase(SourceLocation Loc, DeclarationName Entity) {
6725 this->Loc = Loc;
6726 this->Entity = Entity;
6727 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006728 };
6729}
6730
Douglas Gregor4a959d82009-08-06 16:20:37 +00006731/// \brief Rebuilds a type within the context of the current instantiation.
6732///
Mike Stump1eb44332009-09-09 15:08:12 +00006733/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006734/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006735/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006736/// partial specialization thereof). This routine will rebuild that type now
6737/// that we have entered the declarator's scope, which may produce different
6738/// canonical types, e.g.,
6739///
6740/// \code
6741/// template<typename T>
6742/// struct X {
6743/// typedef T* pointer;
6744/// pointer data();
6745/// };
6746///
6747/// template<typename T>
6748/// typename X<T>::pointer X<T>::data() { ... }
6749/// \endcode
6750///
Douglas Gregor4714c122010-03-31 17:34:00 +00006751/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006752/// since we do not know that we can look into X<T> when we parsed the type.
6753/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006754/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006755/// as the canonical type of T*, allowing the return types of the out-of-line
6756/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006757TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6758 SourceLocation Loc,
6759 DeclarationName Name) {
6760 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006761 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006762
Douglas Gregor4a959d82009-08-06 16:20:37 +00006763 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6764 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006765}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006766
John McCall60d7b3a2010-08-24 06:29:42 +00006767ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006768 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6769 DeclarationName());
6770 return Rebuilder.TransformExpr(E);
6771}
6772
John McCall63b43852010-04-29 23:50:39 +00006773bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006774 if (SS.isInvalid())
6775 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006776
Douglas Gregor7e384942011-02-25 16:07:42 +00006777 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006778 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6779 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006780 NestedNameSpecifierLoc Rebuilt
6781 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6782 if (!Rebuilt)
6783 return true;
John McCall63b43852010-04-29 23:50:39 +00006784
Douglas Gregor7e384942011-02-25 16:07:42 +00006785 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006786 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006787}
6788
Douglas Gregor20606502011-10-14 15:31:12 +00006789/// \brief Rebuild the template parameters now that we know we're in a current
6790/// instantiation.
6791bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6792 TemplateParameterList *Params) {
6793 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6794 Decl *Param = Params->getParam(I);
6795
6796 // There is nothing to rebuild in a type parameter.
6797 if (isa<TemplateTypeParmDecl>(Param))
6798 continue;
6799
6800 // Rebuild the template parameter list of a template template parameter.
6801 if (TemplateTemplateParmDecl *TTP
6802 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6803 if (RebuildTemplateParamsInCurrentInstantiation(
6804 TTP->getTemplateParameters()))
6805 return true;
6806
6807 continue;
6808 }
6809
6810 // Rebuild the type of a non-type template parameter.
6811 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6812 TypeSourceInfo *NewTSI
6813 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6814 NTTP->getLocation(),
6815 NTTP->getDeclName());
6816 if (!NewTSI)
6817 return true;
6818
6819 if (NewTSI != NTTP->getTypeSourceInfo()) {
6820 NTTP->setTypeSourceInfo(NewTSI);
6821 NTTP->setType(NewTSI->getType());
6822 }
6823 }
6824
6825 return false;
6826}
6827
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006828/// \brief Produces a formatted string that describes the binding of
6829/// template parameters to template arguments.
6830std::string
6831Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6832 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006833 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006834}
6835
6836std::string
6837Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6838 const TemplateArgument *Args,
6839 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006840 llvm::SmallString<128> Str;
6841 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006842
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006843 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006844 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006845
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006846 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006847 if (I >= NumArgs)
6848 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006849
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006850 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006851 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006852 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006853 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006854
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006855 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006856 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006857 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006858 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006859 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006860
Douglas Gregor87dd6972010-12-20 16:52:59 +00006861 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006862 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006863 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006864
6865 Out << ']';
6866 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006867}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006868
6869void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6870 if (!FD)
6871 return;
6872 FD->setLateTemplateParsed(Flag);
6873}
6874
6875bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6876 DeclContext *DC = CurContext;
6877
6878 while (DC) {
6879 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6880 const FunctionDecl *FD = RD->isLocalClass();
6881 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6882 } else if (DC->isTranslationUnit() || DC->isNamespace())
6883 return false;
6884
6885 DC = DC->getParent();
6886 }
6887 return false;
6888}