blob: 006017f5a47d92615ee5e454a216ca1141036766 [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
347 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
348 // - if the name is found in the context of the entire
349 // postfix-expression and does not name a class template, the name
350 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000351 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000352 // - if the name found is a class template, it must refer to the same
353 // entity as the one found in the class of the object expression,
354 // otherwise the program is ill-formed.
355 if (!Found.isSingleResult() ||
356 Found.getFoundDecl()->getCanonicalDecl()
357 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000358 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000359 diag::ext_nested_name_member_ref_lookup_ambiguous)
360 << Found.getLookupName()
361 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000362 Diag(Found.getRepresentativeDecl()->getLocation(),
363 diag::note_ambig_member_ref_object_type)
364 << ObjectType;
365 Diag(FoundOuter.getFoundDecl()->getLocation(),
366 diag::note_ambig_member_ref_scope);
367
368 // Recover by taking the template that we found in the object
369 // expression's type.
370 }
371 }
372 }
373}
374
John McCall2f841ba2009-12-02 03:53:29 +0000375/// ActOnDependentIdExpression - Handle a dependent id-expression that
376/// was just parsed. This is only possible with an explicit scope
377/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000378ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000379Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000380 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000381 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000382 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000383 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000384
John McCall2f841ba2009-12-02 03:53:29 +0000385 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000386 isa<CXXMethodDecl>(DC) &&
387 cast<CXXMethodDecl>(DC)->isInstance()) {
388 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000389
John McCallf7a1a742009-11-24 19:00:30 +0000390 // Since the 'this' expression is synthesized, we don't need to
391 // perform the double-lookup check.
392 NamedDecl *FirstQualifierInScope = 0;
393
John McCallaa81e162009-12-01 22:10:20 +0000394 return Owned(CXXDependentScopeMemberExpr::Create(Context,
395 /*This*/ 0, ThisType,
396 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000397 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000398 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000399 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000400 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000401 TemplateArgs));
402 }
403
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000405}
406
John McCall60d7b3a2010-08-24 06:29:42 +0000407ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000408Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000409 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000410 const TemplateArgumentListInfo *TemplateArgs) {
411 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000412 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000413 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000414 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000415}
416
Douglas Gregor72c3f312008-12-05 18:15:24 +0000417/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
418/// that the template parameter 'PrevDecl' is being shadowed by a new
419/// declaration at location Loc. Returns true to indicate that this is
420/// an error, and false otherwise.
421bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000422 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000423
424 // Microsoft Visual C++ permits template parameters to be shadowed.
425 if (getLangOptions().Microsoft)
426 return false;
427
428 // C++ [temp.local]p4:
429 // A template-parameter shall not be redeclared within its
430 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000431 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000432 << cast<NamedDecl>(PrevDecl)->getDeclName();
433 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
434 return true;
435}
436
Douglas Gregor2943aed2009-03-03 04:44:36 +0000437/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000438/// the parameter D to reference the templated declaration and return a pointer
439/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000440TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
441 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
442 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000443 return Temp;
444 }
445 return 0;
446}
447
Douglas Gregorba68eca2011-01-05 17:40:24 +0000448ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
449 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000450 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000451 "Only template template arguments can be pack expansions here");
452 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
453 "Template template argument pack expansion without packs");
454 ParsedTemplateArgument Result(*this);
455 Result.EllipsisLoc = EllipsisLoc;
456 return Result;
457}
458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
460 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000461
Douglas Gregor788cd062009-11-11 01:00:40 +0000462 switch (Arg.getKind()) {
463 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000464 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000465 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000466 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000467 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000468 return TemplateArgumentLoc(TemplateArgument(T), DI);
469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000470
Douglas Gregor788cd062009-11-11 01:00:40 +0000471 case ParsedTemplateArgument::NonType: {
472 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
473 return TemplateArgumentLoc(TemplateArgument(E), E);
474 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000475
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000477 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000478 TemplateArgument TArg;
479 if (Arg.getEllipsisLoc().isValid())
480 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
481 else
482 TArg = Template;
483 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000484 Arg.getScopeSpec().getWithLocInContext(
485 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000486 Arg.getLocation(),
487 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000488 }
489 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000490
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000491 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000492 return TemplateArgumentLoc();
493}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000494
Douglas Gregor788cd062009-11-11 01:00:40 +0000495/// \brief Translates template arguments as provided by the parser
496/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000497void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
498 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000499 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000500 TemplateArgs.addArgument(translateTemplateArgument(*this,
501 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000502}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000503
Douglas Gregor72c3f312008-12-05 18:15:24 +0000504/// ActOnTypeParameter - Called when a C++ template type parameter
505/// (e.g., "typename T") has been parsed. Typename specifies whether
506/// the keyword "typename" was used to declare the type parameter
507/// (otherwise, "class" was used), and KeyLoc is the location of the
508/// "class" or "typename" keyword. ParamName is the name of the
509/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000510/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511/// If the type parameter has a default argument, it will be added
512/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000513Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
514 SourceLocation EllipsisLoc,
515 SourceLocation KeyLoc,
516 IdentifierInfo *ParamName,
517 SourceLocation ParamNameLoc,
518 unsigned Depth, unsigned Position,
519 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000520 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000521 assert(S->isTemplateParamScope() &&
522 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000523 bool Invalid = false;
524
525 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000526 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000527 LookupOrdinaryName,
528 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000529 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000530 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000531 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 }
533
Douglas Gregorddc29e12009-02-06 22:42:48 +0000534 SourceLocation Loc = ParamNameLoc;
535 if (!ParamName)
536 Loc = KeyLoc;
537
Douglas Gregor72c3f312008-12-05 18:15:24 +0000538 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000539 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000540 KeyLoc, Loc, Depth, Position, ParamName,
541 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000542 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000543 if (Invalid)
544 Param->setInvalidDecl();
545
546 if (ParamName) {
547 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000548 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000549 IdResolver.AddDecl(Param);
550 }
551
Douglas Gregor61c4d282011-01-05 15:48:55 +0000552 // C++0x [temp.param]p9:
553 // A default template-argument may be specified for any kind of
554 // template-parameter that is not a template parameter pack.
555 if (DefaultArg && Ellipsis) {
556 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
557 DefaultArg = ParsedType();
558 }
559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 // Handle the default argument, if provided.
561 if (DefaultArg) {
562 TypeSourceInfo *DefaultTInfo;
563 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000564
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregor6f526752010-12-16 08:48:57 +0000567 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000569 UPPC_DefaultArgument))
570 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000572 // Check the template argument itself.
573 if (CheckTemplateArgument(Param, DefaultTInfo)) {
574 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000575 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000576 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000577
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000578 Param->setDefaultArgument(DefaultTInfo, false);
579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000580
John McCalld226f652010-08-21 09:40:31 +0000581 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000582}
583
Douglas Gregor2943aed2009-03-03 04:44:36 +0000584/// \brief Check that the type of a non-type template parameter is
585/// well-formed.
586///
587/// \returns the (possibly-promoted) parameter type if valid;
588/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000589QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000590Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000591 // We don't allow variably-modified types as the type of non-type template
592 // parameters.
593 if (T->isVariablyModifiedType()) {
594 Diag(Loc, diag::err_variably_modified_nontype_template_param)
595 << T;
596 return QualType();
597 }
598
Douglas Gregor2943aed2009-03-03 04:44:36 +0000599 // C++ [temp.param]p4:
600 //
601 // A non-type template-parameter shall have one of the following
602 // (optionally cv-qualified) types:
603 //
604 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000605 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000606 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000607 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000609 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000610 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000611 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000612 // -- std::nullptr_t.
613 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614 // If T is a dependent type, we can't do the check now, so we
615 // assume that it is well-formed.
616 T->isDependentType())
617 return T;
618 // C++ [temp.param]p8:
619 //
620 // A non-type template-parameter of type "array of T" or
621 // "function returning T" is adjusted to be of type "pointer to
622 // T" or "pointer to function returning T", respectively.
623 else if (T->isArrayType())
624 // FIXME: Keep the type prior to promotion?
625 return Context.getArrayDecayedType(T);
626 else if (T->isFunctionType())
627 // FIXME: Keep the type prior to promotion?
628 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000629
Douglas Gregor2943aed2009-03-03 04:44:36 +0000630 Diag(Loc, diag::err_template_nontype_parm_bad_type)
631 << T;
632
633 return QualType();
634}
635
John McCalld226f652010-08-21 09:40:31 +0000636Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
637 unsigned Depth,
638 unsigned Position,
639 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000640 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000641 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
642 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000643
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000644 assert(S->isTemplateParamScope() &&
645 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000646 bool Invalid = false;
647
648 IdentifierInfo *ParamName = D.getIdentifier();
649 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000650 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000651 LookupOrdinaryName,
652 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000653 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000654 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000655 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000656 }
657
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000658 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
659 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000660 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000661 Invalid = true;
662 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor10738d32010-12-23 23:51:58 +0000664 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000666 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000667 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000668 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000669 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000670 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000671 Param->setAccess(AS_public);
672
Douglas Gregor72c3f312008-12-05 18:15:24 +0000673 if (Invalid)
674 Param->setInvalidDecl();
675
676 if (D.getIdentifier()) {
677 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000678 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000679 IdResolver.AddDecl(Param);
680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000681
Douglas Gregor61c4d282011-01-05 15:48:55 +0000682 // C++0x [temp.param]p9:
683 // A default template-argument may be specified for any kind of
684 // template-parameter that is not a template parameter pack.
685 if (Default && IsParameterPack) {
686 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
687 Default = 0;
688 }
689
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000690 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000691 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000692 // Check for unexpanded parameter packs.
693 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
694 return Param;
695
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000696 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000697 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
698 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000699 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000700 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000701 }
John Wiegley429bb272011-04-08 18:41:53 +0000702 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000703
John McCall9ae2f072010-08-23 23:25:46 +0000704 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000706
John McCalld226f652010-08-21 09:40:31 +0000707 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000708}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000709
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000710/// ActOnTemplateTemplateParameter - Called when a C++ template template
711/// parameter (e.g. T in template <template <typename> class T> class array)
712/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000713Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
714 SourceLocation TmpLoc,
715 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000717 IdentifierInfo *Name,
718 SourceLocation NameLoc,
719 unsigned Depth,
720 unsigned Position,
721 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000722 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 assert(S->isTemplateParamScope() &&
724 "Template template parameter not in template parameter scope!");
725
726 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000727 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000728 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000729 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000730 NameLoc.isInvalid()? TmpLoc : NameLoc,
731 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000732 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000733 Param->setAccess(AS_public);
734
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000735 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000737 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000738 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000739 IdResolver.AddDecl(Param);
740 }
741
Douglas Gregor6f526752010-12-16 08:48:57 +0000742 if (Params->size() == 0) {
743 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
744 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
745 Param->setInvalidDecl();
746 }
747
Douglas Gregor61c4d282011-01-05 15:48:55 +0000748 // C++0x [temp.param]p9:
749 // A default template-argument may be specified for any kind of
750 // template-parameter that is not a template parameter pack.
751 if (IsParameterPack && !Default.isInvalid()) {
752 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
753 Default = ParsedTemplateArgument();
754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000756 if (!Default.isInvalid()) {
757 // Check only that we have a template template argument. We don't want to
758 // try to check well-formedness now, because our template template parameter
759 // might have dependent types in its template parameters, which we wouldn't
760 // be able to match now.
761 //
762 // If none of the template template parameter's template arguments mention
763 // other template parameters, we could actually perform more checking here.
764 // However, it isn't worth doing.
765 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
766 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
767 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
768 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000769 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000770 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000771
Douglas Gregor6f526752010-12-16 08:48:57 +0000772 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000774 DefaultArg.getArgument().getAsTemplate(),
775 UPPC_DefaultArgument))
776 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000778 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000779 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000780
John McCalld226f652010-08-21 09:40:31 +0000781 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000782}
783
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000784/// ActOnTemplateParameterList - Builds a TemplateParameterList that
785/// contains the template parameters in Params/NumParams.
786Sema::TemplateParamsTy *
787Sema::ActOnTemplateParameterList(unsigned Depth,
788 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000789 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000790 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000791 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792 SourceLocation RAngleLoc) {
793 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000794 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000795
Douglas Gregorddc29e12009-02-06 22:42:48 +0000796 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000797 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000798 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000799}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000800
John McCallb6217662010-03-15 10:12:16 +0000801static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
802 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000803 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000804}
805
John McCallf312b1e2010-08-26 23:41:50 +0000806DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000807Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000808 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000809 IdentifierInfo *Name, SourceLocation NameLoc,
810 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000811 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000812 AccessSpecifier AS,
813 unsigned NumOuterTemplateParamLists,
814 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000815 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000817 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000818 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000819
820 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000821 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000822 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000823
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000824 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
825 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000826
827 // There is no such thing as an unnamed class template.
828 if (!Name) {
829 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000830 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000831 }
832
833 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000834 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000835 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000836 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000837 if (SS.isNotEmpty() && !SS.isInvalid()) {
838 SemanticContext = computeDeclContext(SS, true);
839 if (!SemanticContext) {
840 // FIXME: Produce a reasonable diagnostic here
841 return true;
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
John McCall77bb1aa2010-05-01 00:40:08 +0000844 if (RequireCompleteDeclContext(SS, SemanticContext))
845 return true;
846
John McCalla24dc2e2009-11-17 02:14:36 +0000847 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000848 } else {
849 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000850 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor57265e32010-04-12 16:00:01 +0000853 if (Previous.isAmbiguous())
854 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000855
Douglas Gregorddc29e12009-02-06 22:42:48 +0000856 NamedDecl *PrevDecl = 0;
857 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000858 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000859
Douglas Gregorddc29e12009-02-06 22:42:48 +0000860 // If there is a previous declaration with the same name, check
861 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000862 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000863 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000864
865 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000867 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000869 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
870 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000871 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000872 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
873 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
874 PrevClassTemplate
875 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
876 ->getSpecializedTemplate();
877 }
878 }
879
John McCall65c49462009-12-18 11:25:59 +0000880 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000881 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000882 // [...] When looking for a prior declaration of a class or a function
883 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000884 // function is neither a qualified name nor a template-id, scopes outside
885 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000886 if (!SS.isSet()) {
887 DeclContext *OutermostContext = CurContext;
888 while (!OutermostContext->isFileContext())
889 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000890
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000891 if (PrevDecl &&
892 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
893 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
894 SemanticContext = PrevDecl->getDeclContext();
895 } else {
896 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000898 // declaration.
899 PrevDecl = PrevClassTemplate = 0;
900 SemanticContext = OutermostContext;
901 }
John McCalle129d442009-12-17 23:21:11 +0000902 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000903
John McCalle129d442009-12-17 23:21:11 +0000904 if (CurContext->isDependentContext()) {
905 // If this is a dependent context, we don't want to link the friend
906 // class template to the template in scope, because that would perform
907 // checking of the template parameter lists that can't be performed
908 // until the outer context is instantiated.
909 PrevDecl = PrevClassTemplate = 0;
910 }
911 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
912 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000913
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 if (PrevClassTemplate) {
915 // Ensure that the template parameter lists are compatible.
916 if (!TemplateParameterListsAreEqual(TemplateParams,
917 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000918 /*Complain=*/true,
919 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000920 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000921
922 // C++ [temp.class]p4:
923 // In a redeclaration, partial specialization, explicit
924 // specialization or explicit instantiation of a class template,
925 // the class-key shall agree in kind with the original class
926 // template declaration (7.1.5.3).
927 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000928 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
929 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000930 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000931 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000932 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000933 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000934 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000935 }
936
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000938 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000939 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000940 Diag(NameLoc, diag::err_redefinition) << Name;
941 Diag(Def->getLocation(), diag::note_previous_definition);
942 // FIXME: Would it make sense to try to "forget" the previous
943 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000944 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000945 }
946 }
947 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
948 // Maybe we will complain about the shadowed template parameter.
949 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
950 // Just pretend that we didn't see the previous declaration.
951 PrevDecl = 0;
952 } else if (PrevDecl) {
953 // C++ [temp]p5:
954 // A class template shall not have the same name as any other
955 // template, class, function, object, enumeration, enumerator,
956 // namespace, or type in the same scope (3.3), except as specified
957 // in (14.5.4).
958 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
959 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000960 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000961 }
962
Douglas Gregord684b002009-02-10 19:49:53 +0000963 // Check the template parameter list of this declaration, possibly
964 // merging in the template parameter list from the previous class
965 // template declaration.
966 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000967 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000968 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000969 SemanticContext->isRecord() &&
970 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000971 ? TPC_ClassTemplateMember
972 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000973 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor57265e32010-04-12 16:00:01 +0000975 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000976 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000977 // template out-of-line.
978 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
979 !(TUK == TUK_Friend && CurContext->isDependentContext()))
980 Diag(NameLoc, diag::err_member_def_does_not_match)
981 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 }
983
Mike Stump1eb44332009-09-09 15:08:12 +0000984 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000985 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000986 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000987 PrevClassTemplate->getTemplatedDecl() : 0,
988 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000989 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000990 if (NumOuterTemplateParamLists > 0)
991 NewClass->setTemplateParameterListsInfo(Context,
992 NumOuterTemplateParamLists,
993 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000994
995 ClassTemplateDecl *NewTemplate
996 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
997 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000998 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000999 NewClass->setDescribedClassTemplate(NewTemplate);
1000
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001001 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001002 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001003 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001004 assert(T->isDependentType() && "Class template type is not dependent?");
1005 (void)T;
1006
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001007 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001008 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001009 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001010 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1011 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001012
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001013 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001014 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001015 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Douglas Gregorddc29e12009-02-06 22:42:48 +00001017 // Set the lexical context of these templates
1018 NewClass->setLexicalDeclContext(CurContext);
1019 NewTemplate->setLexicalDeclContext(CurContext);
1020
John McCall0f434ec2009-07-31 02:45:11 +00001021 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001022 NewClass->startDefinition();
1023
1024 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001025 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001026
John McCall05b23ea2009-09-14 21:59:20 +00001027 if (TUK != TUK_Friend)
1028 PushOnScopeChains(NewTemplate, S);
1029 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001030 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001031 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001032 NewClass->setAccess(PrevClassTemplate->getAccess());
1033 }
John McCall05b23ea2009-09-14 21:59:20 +00001034
Douglas Gregord85bea22009-09-26 06:47:28 +00001035 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1036 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001037
John McCall05b23ea2009-09-14 21:59:20 +00001038 // Friend templates are visible in fairly strange ways.
1039 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001040 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001041 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1042 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1043 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001044 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001045 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001046
Douglas Gregord85bea22009-09-26 06:47:28 +00001047 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1048 NewClass->getLocation(),
1049 NewTemplate,
1050 /*FIXME:*/NewClass->getLocation());
1051 Friend->setAccess(AS_public);
1052 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001053 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001054
Douglas Gregord684b002009-02-10 19:49:53 +00001055 if (Invalid) {
1056 NewTemplate->setInvalidDecl();
1057 NewClass->setInvalidDecl();
1058 }
John McCalld226f652010-08-21 09:40:31 +00001059 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001060}
1061
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001062/// \brief Diagnose the presence of a default template argument on a
1063/// template parameter, which is ill-formed in certain contexts.
1064///
1065/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001066static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067 Sema::TemplateParamListContext TPC,
1068 SourceLocation ParamLoc,
1069 SourceRange DefArgRange) {
1070 switch (TPC) {
1071 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001072 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001073 return false;
1074
1075 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001076 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001077 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001078 // A default template-argument shall not be specified in a
1079 // function template declaration or a function template
1080 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001081 // If a friend function template declaration specifies a default
1082 // template-argument, that declaration shall be a definition and shall be
1083 // the only declaration of the function template in the translation unit.
1084 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001086 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001087 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001088 << DefArgRange;
1089 return false;
1090
1091 case Sema::TPC_ClassTemplateMember:
1092 // C++0x [temp.param]p9:
1093 // A default template-argument shall not be specified in the
1094 // template-parameter-lists of the definition of a member of a
1095 // class template that appears outside of the member's class.
1096 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1097 << DefArgRange;
1098 return true;
1099
1100 case Sema::TPC_FriendFunctionTemplate:
1101 // C++ [temp.param]p9:
1102 // A default template-argument shall not be specified in a
1103 // friend template declaration.
1104 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1105 << DefArgRange;
1106 return true;
1107
1108 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1109 // for friend function templates if there is only a single
1110 // declaration (and it is a definition). Strange!
1111 }
1112
1113 return false;
1114}
1115
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001116/// \brief Check for unexpanded parameter packs within the template parameters
1117/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001118static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1119 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001120 TemplateParameterList *Params = TTP->getTemplateParameters();
1121 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1122 NamedDecl *P = Params->getParam(I);
1123 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001124 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001125 NTTP->getTypeSourceInfo(),
1126 Sema::UPPC_NonTypeTemplateParameterType))
1127 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001128
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001129 continue;
1130 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001131
1132 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001133 = dyn_cast<TemplateTemplateParmDecl>(P))
1134 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1135 return true;
1136 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001137
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001138 return false;
1139}
1140
Douglas Gregord684b002009-02-10 19:49:53 +00001141/// \brief Checks the validity of a template parameter list, possibly
1142/// considering the template parameter list from a previous
1143/// declaration.
1144///
1145/// If an "old" template parameter list is provided, it must be
1146/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1147/// template parameter list.
1148///
1149/// \param NewParams Template parameter list for a new template
1150/// declaration. This template parameter list will be updated with any
1151/// default arguments that are carried through from the previous
1152/// template parameter list.
1153///
1154/// \param OldParams If provided, template parameter list from a
1155/// previous declaration of the same template. Default template
1156/// arguments will be merged from the old template parameter list to
1157/// the new template parameter list.
1158///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001159/// \param TPC Describes the context in which we are checking the given
1160/// template parameter list.
1161///
Douglas Gregord684b002009-02-10 19:49:53 +00001162/// \returns true if an error occurred, false otherwise.
1163bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001164 TemplateParameterList *OldParams,
1165 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001166 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregord684b002009-02-10 19:49:53 +00001168 // C++ [temp.param]p10:
1169 // The set of default template-arguments available for use with a
1170 // template declaration or definition is obtained by merging the
1171 // default arguments from the definition (if in scope) and all
1172 // declarations in scope in the same way default function
1173 // arguments are (8.3.6).
1174 bool SawDefaultArgument = false;
1175 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001176
Anders Carlsson49d25572009-06-12 23:20:15 +00001177 bool SawParameterPack = false;
1178 SourceLocation ParameterPackLoc;
1179
Mike Stump1a35fde2009-02-11 23:03:27 +00001180 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001181 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001182 if (OldParams)
1183 OldParam = OldParams->begin();
1184
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001185 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001186 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1187 NewParamEnd = NewParams->end();
1188 NewParam != NewParamEnd; ++NewParam) {
1189 // Variables used to diagnose redundant default arguments
1190 bool RedundantDefaultArg = false;
1191 SourceLocation OldDefaultLoc;
1192 SourceLocation NewDefaultLoc;
1193
1194 // Variables used to diagnose missing default arguments
1195 bool MissingDefaultArg = false;
1196
Anders Carlsson49d25572009-06-12 23:20:15 +00001197 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001198 // If a template parameter of a primary class template or alias template
1199 // is a template parameter pack, it shall be the last template parameter.
1200 if (SawParameterPack &&
1201 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001202 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001203 diag::err_template_param_pack_must_be_last_template_parameter);
1204 Invalid = true;
1205 }
1206
Douglas Gregord684b002009-02-10 19:49:53 +00001207 if (TemplateTypeParmDecl *NewTypeParm
1208 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001209 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001210 if (NewTypeParm->hasDefaultArgument() &&
1211 DiagnoseDefaultTemplateArgument(*this, TPC,
1212 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001213 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001214 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001215 NewTypeParm->removeDefaultArgument();
1216
1217 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001218 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001219 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Anders Carlsson49d25572009-06-12 23:20:15 +00001221 if (NewTypeParm->isParameterPack()) {
1222 assert(!NewTypeParm->hasDefaultArgument() &&
1223 "Parameter packs can't have a default argument!");
1224 SawParameterPack = true;
1225 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001226 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001227 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001228 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1229 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1230 SawDefaultArgument = true;
1231 RedundantDefaultArg = true;
1232 PreviousDefaultArgLoc = NewDefaultLoc;
1233 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1234 // Merge the default argument from the old declaration to the
1235 // new declaration.
1236 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001237 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001238 true);
1239 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1240 } else if (NewTypeParm->hasDefaultArgument()) {
1241 SawDefaultArgument = true;
1242 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1243 } else if (SawDefaultArgument)
1244 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001245 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001246 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001247 // Check for unexpanded parameter packs.
1248 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001249 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001250 UPPC_NonTypeTemplateParameterType)) {
1251 Invalid = true;
1252 continue;
1253 }
1254
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001255 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001256 if (NewNonTypeParm->hasDefaultArgument() &&
1257 DiagnoseDefaultTemplateArgument(*this, TPC,
1258 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001259 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001260 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001261 }
1262
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001263 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001264 NonTypeTemplateParmDecl *OldNonTypeParm
1265 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001266 if (NewNonTypeParm->isParameterPack()) {
1267 assert(!NewNonTypeParm->hasDefaultArgument() &&
1268 "Parameter packs can't have a default argument!");
1269 SawParameterPack = true;
1270 ParameterPackLoc = NewNonTypeParm->getLocation();
1271 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001272 NewNonTypeParm->hasDefaultArgument()) {
1273 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1274 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1275 SawDefaultArgument = true;
1276 RedundantDefaultArg = true;
1277 PreviousDefaultArgLoc = NewDefaultLoc;
1278 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1279 // Merge the default argument from the old declaration to the
1280 // new declaration.
1281 SawDefaultArgument = true;
1282 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001283 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001284 // parameter.
1285 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001286 OldNonTypeParm->getDefaultArgument(),
1287 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001288 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1289 } else if (NewNonTypeParm->hasDefaultArgument()) {
1290 SawDefaultArgument = true;
1291 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1292 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001293 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001294 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001295 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001296 TemplateTemplateParmDecl *NewTemplateParm
1297 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001298
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001299 // Check for unexpanded parameter packs, recursively.
1300 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1301 Invalid = true;
1302 continue;
1303 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001304
1305 if (NewTemplateParm->hasDefaultArgument() &&
1306 DiagnoseDefaultTemplateArgument(*this, TPC,
1307 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001308 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001309 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001310
1311 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001312 TemplateTemplateParmDecl *OldTemplateParm
1313 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001314 if (NewTemplateParm->isParameterPack()) {
1315 assert(!NewTemplateParm->hasDefaultArgument() &&
1316 "Parameter packs can't have a default argument!");
1317 SawParameterPack = true;
1318 ParameterPackLoc = NewTemplateParm->getLocation();
1319 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001320 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001321 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1322 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001323 SawDefaultArgument = true;
1324 RedundantDefaultArg = true;
1325 PreviousDefaultArgLoc = NewDefaultLoc;
1326 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1327 // Merge the default argument from the old declaration to the
1328 // new declaration.
1329 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001330 // FIXME: We need to create a new kind of "default argument" expression
1331 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001332 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001333 OldTemplateParm->getDefaultArgument(),
1334 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001335 PreviousDefaultArgLoc
1336 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001337 } else if (NewTemplateParm->hasDefaultArgument()) {
1338 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001339 PreviousDefaultArgLoc
1340 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001341 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001342 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001343 }
1344
1345 if (RedundantDefaultArg) {
1346 // C++ [temp.param]p12:
1347 // A template-parameter shall not be given default arguments
1348 // by two different declarations in the same scope.
1349 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1350 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1351 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001352 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001353 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001354 // If a template-parameter of a class template has a default
1355 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001356 // have a default template-argument supplied or be a template parameter
1357 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001358 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001359 diag::err_template_param_default_arg_missing);
1360 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1361 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001362 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001363 }
1364
1365 // If we have an old template parameter list that we're merging
1366 // in, move on to the next parameter.
1367 if (OldParams)
1368 ++OldParam;
1369 }
1370
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001371 // We were missing some default arguments at the end of the list, so remove
1372 // all of the default arguments.
1373 if (RemoveDefaultArguments) {
1374 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1375 NewParamEnd = NewParams->end();
1376 NewParam != NewParamEnd; ++NewParam) {
1377 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1378 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001379 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001380 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1381 NTTP->removeDefaultArgument();
1382 else
1383 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1384 }
1385 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001386
Douglas Gregord684b002009-02-10 19:49:53 +00001387 return Invalid;
1388}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001389
John McCall4e2cbb22010-10-20 05:44:58 +00001390namespace {
1391
1392/// A class which looks for a use of a certain level of template
1393/// parameter.
1394struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1395 typedef RecursiveASTVisitor<DependencyChecker> super;
1396
1397 unsigned Depth;
1398 bool Match;
1399
1400 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1401 NamedDecl *ND = Params->getParam(0);
1402 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1403 Depth = PD->getDepth();
1404 } else if (NonTypeTemplateParmDecl *PD =
1405 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1406 Depth = PD->getDepth();
1407 } else {
1408 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1409 }
1410 }
1411
1412 bool Matches(unsigned ParmDepth) {
1413 if (ParmDepth >= Depth) {
1414 Match = true;
1415 return true;
1416 }
1417 return false;
1418 }
1419
1420 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1421 return !Matches(T->getDepth());
1422 }
1423
1424 bool TraverseTemplateName(TemplateName N) {
1425 if (TemplateTemplateParmDecl *PD =
1426 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1427 if (Matches(PD->getDepth())) return false;
1428 return super::TraverseTemplateName(N);
1429 }
1430
1431 bool VisitDeclRefExpr(DeclRefExpr *E) {
1432 if (NonTypeTemplateParmDecl *PD =
1433 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1434 if (PD->getDepth() == Depth) {
1435 Match = true;
1436 return false;
1437 }
1438 }
1439 return super::VisitDeclRefExpr(E);
1440 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001441
1442 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1443 return TraverseType(T->getInjectedSpecializationType());
1444 }
John McCall4e2cbb22010-10-20 05:44:58 +00001445};
1446}
1447
Douglas Gregorc8406492011-05-10 18:27:06 +00001448/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001449/// list.
1450static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001451DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001452 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001453 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001454 return Checker.Match;
1455}
1456
Douglas Gregorc8406492011-05-10 18:27:06 +00001457// Find the source range corresponding to the named type in the given
1458// nested-name-specifier, if any.
1459static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1460 QualType T,
1461 const CXXScopeSpec &SS) {
1462 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1463 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1464 if (const Type *CurType = NNS->getAsType()) {
1465 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1466 return NNSLoc.getTypeLoc().getSourceRange();
1467 } else
1468 break;
1469
1470 NNSLoc = NNSLoc.getPrefix();
1471 }
1472
1473 return SourceRange();
1474}
1475
Mike Stump1eb44332009-09-09 15:08:12 +00001476/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001477/// specifier, returning the template parameter list that applies to the
1478/// name.
1479///
1480/// \param DeclStartLoc the start of the declaration that has a scope
1481/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001482///
Douglas Gregorc8406492011-05-10 18:27:06 +00001483/// \param DeclLoc The location of the declaration itself.
1484///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001485/// \param SS the scope specifier that will be matched to the given template
1486/// parameter lists. This scope specifier precedes a qualified name that is
1487/// being declared.
1488///
1489/// \param ParamLists the template parameter lists, from the outermost to the
1490/// innermost template parameter lists.
1491///
1492/// \param NumParamLists the number of template parameter lists in ParamLists.
1493///
John McCall77e8b112010-04-13 20:37:33 +00001494/// \param IsFriend Whether to apply the slightly different rules for
1495/// matching template parameters to scope specifiers in friend
1496/// declarations.
1497///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001498/// \param IsExplicitSpecialization will be set true if the entity being
1499/// declared is an explicit specialization, false otherwise.
1500///
Mike Stump1eb44332009-09-09 15:08:12 +00001501/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001502/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001503/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001504/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001505/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506/// itself a template).
1507TemplateParameterList *
1508Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001509 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 const CXXScopeSpec &SS,
1511 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001512 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001513 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001514 bool &IsExplicitSpecialization,
1515 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001516 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001517 Invalid = false;
1518
1519 // The sequence of nested types to which we will match up the template
1520 // parameter lists. We first build this list by starting with the type named
1521 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001522 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001523 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001524 if (SS.getScopeRep()) {
1525 if (CXXRecordDecl *Record
1526 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1527 T = Context.getTypeDeclType(Record);
1528 else
1529 T = QualType(SS.getScopeRep()->getAsType(), 0);
1530 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001531
1532 // If we found an explicit specialization that prevents us from needing
1533 // 'template<>' headers, this will be set to the location of that
1534 // explicit specialization.
1535 SourceLocation ExplicitSpecLoc;
1536
1537 while (!T.isNull()) {
1538 NestedTypes.push_back(T);
1539
1540 // Retrieve the parent of a record type.
1541 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1542 // If this type is an explicit specialization, we're done.
1543 if (ClassTemplateSpecializationDecl *Spec
1544 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1545 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1546 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1547 ExplicitSpecLoc = Spec->getLocation();
1548 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001549 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001550 } else if (Record->getTemplateSpecializationKind()
1551 == TSK_ExplicitSpecialization) {
1552 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001553 break;
1554 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001555
1556 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1557 T = Context.getTypeDeclType(Parent);
1558 else
1559 T = QualType();
1560 continue;
1561 }
1562
1563 if (const TemplateSpecializationType *TST
1564 = T->getAs<TemplateSpecializationType>()) {
1565 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1566 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1567 T = Context.getTypeDeclType(Parent);
1568 else
1569 T = QualType();
1570 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001571 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001572 }
1573
1574 // Look one step prior in a dependent template specialization type.
1575 if (const DependentTemplateSpecializationType *DependentTST
1576 = T->getAs<DependentTemplateSpecializationType>()) {
1577 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1578 T = QualType(NNS->getAsType(), 0);
1579 else
1580 T = QualType();
1581 continue;
1582 }
1583
1584 // Look one step prior in a dependent name type.
1585 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1586 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1587 T = QualType(NNS->getAsType(), 0);
1588 else
1589 T = QualType();
1590 continue;
1591 }
1592
1593 // Retrieve the parent of an enumeration type.
1594 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1595 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1596 // check here.
1597 EnumDecl *Enum = EnumT->getDecl();
1598
1599 // Get to the parent type.
1600 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1601 T = Context.getTypeDeclType(Parent);
1602 else
1603 T = QualType();
1604 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregorc8406492011-05-10 18:27:06 +00001607 T = QualType();
1608 }
1609 // Reverse the nested types list, since we want to traverse from the outermost
1610 // to the innermost while checking template-parameter-lists.
1611 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001612
Douglas Gregorc8406492011-05-10 18:27:06 +00001613 // C++0x [temp.expl.spec]p17:
1614 // A member or a member template may be nested within many
1615 // enclosing class templates. In an explicit specialization for
1616 // such a member, the member declaration shall be preceded by a
1617 // template<> for each enclosing class template that is
1618 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001619 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001620 unsigned ParamIdx = 0;
1621 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1622 ++TypeIdx) {
1623 T = NestedTypes[TypeIdx];
1624
1625 // Whether we expect a 'template<>' header.
1626 bool NeedEmptyTemplateHeader = false;
1627
1628 // Whether we expect a template header with parameters.
1629 bool NeedNonemptyTemplateHeader = false;
1630
1631 // For a dependent type, the set of template parameters that we
1632 // expect to see.
1633 TemplateParameterList *ExpectedTemplateParams = 0;
1634
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001635 // C++0x [temp.expl.spec]p15:
1636 // A member or a member template may be nested within many enclosing
1637 // class templates. In an explicit specialization for such a member, the
1638 // member declaration shall be preceded by a template<> for each
1639 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001640 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1641 if (ClassTemplatePartialSpecializationDecl *Partial
1642 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1643 ExpectedTemplateParams = Partial->getTemplateParameters();
1644 NeedNonemptyTemplateHeader = true;
1645 } else if (Record->isDependentType()) {
1646 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001647 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001648 ->getTemplateParameters();
1649 NeedNonemptyTemplateHeader = true;
1650 }
1651 } else if (ClassTemplateSpecializationDecl *Spec
1652 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1653 // C++0x [temp.expl.spec]p4:
1654 // Members of an explicitly specialized class template are defined
1655 // in the same manner as members of normal classes, and not using
1656 // the template<> syntax.
1657 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1658 NeedEmptyTemplateHeader = true;
1659 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001660 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001661 } else if (Record->getTemplateSpecializationKind()) {
1662 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001663 != TSK_ExplicitSpecialization &&
1664 TypeIdx == NumTypes - 1)
1665 IsExplicitSpecialization = true;
1666
1667 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001668 }
1669 } else if (const TemplateSpecializationType *TST
1670 = T->getAs<TemplateSpecializationType>()) {
1671 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1672 ExpectedTemplateParams = Template->getTemplateParameters();
1673 NeedNonemptyTemplateHeader = true;
1674 }
1675 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1676 // FIXME: We actually could/should check the template arguments here
1677 // against the corresponding template parameter list.
1678 NeedNonemptyTemplateHeader = false;
1679 }
1680
Douglas Gregor89b9f102011-06-06 15:22:55 +00001681 // C++ [temp.expl.spec]p16:
1682 // In an explicit specialization declaration for a member of a class
1683 // template or a member template that ap- pears in namespace scope, the
1684 // member template and some of its enclosing class templates may remain
1685 // unspecialized, except that the declaration shall not explicitly
1686 // specialize a class member template if its en- closing class templates
1687 // are not explicitly specialized as well.
1688 if (ParamIdx < NumParamLists) {
1689 if (ParamLists[ParamIdx]->size() == 0) {
1690 if (SawNonEmptyTemplateParameterList) {
1691 Diag(DeclLoc, diag::err_specialize_member_of_template)
1692 << ParamLists[ParamIdx]->getSourceRange();
1693 Invalid = true;
1694 IsExplicitSpecialization = false;
1695 return 0;
1696 }
1697 } else
1698 SawNonEmptyTemplateParameterList = true;
1699 }
1700
Douglas Gregorc8406492011-05-10 18:27:06 +00001701 if (NeedEmptyTemplateHeader) {
1702 // If we're on the last of the types, and we need a 'template<>' header
1703 // here, then it's an explicit specialization.
1704 if (TypeIdx == NumTypes - 1)
1705 IsExplicitSpecialization = true;
1706
1707 if (ParamIdx < NumParamLists) {
1708 if (ParamLists[ParamIdx]->size() > 0) {
1709 // The header has template parameters when it shouldn't. Complain.
1710 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1711 diag::err_template_param_list_matches_nontemplate)
1712 << T
1713 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1714 ParamLists[ParamIdx]->getRAngleLoc())
1715 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1716 Invalid = true;
1717 return 0;
1718 }
1719
1720 // Consume this template header.
1721 ++ParamIdx;
1722 continue;
1723 }
1724
1725 if (!IsFriend) {
1726 // We don't have a template header, but we should.
1727 SourceLocation ExpectedTemplateLoc;
1728 if (NumParamLists > 0)
1729 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1730 else
1731 ExpectedTemplateLoc = DeclStartLoc;
1732
1733 Diag(DeclLoc, diag::err_template_spec_needs_header)
1734 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1735 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1736 }
1737
1738 continue;
1739 }
1740
1741 if (NeedNonemptyTemplateHeader) {
1742 // In friend declarations we can have template-ids which don't
1743 // depend on the corresponding template parameter lists. But
1744 // assume that empty parameter lists are supposed to match this
1745 // template-id.
1746 if (IsFriend && T->isDependentType()) {
1747 if (ParamIdx < NumParamLists &&
1748 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1749 ExpectedTemplateParams = 0;
1750 else
1751 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001752 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001753
Douglas Gregorc8406492011-05-10 18:27:06 +00001754 if (ParamIdx < NumParamLists) {
1755 // Check the template parameter list, if we can.
1756 if (ExpectedTemplateParams &&
1757 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1758 ExpectedTemplateParams,
1759 true, TPL_TemplateMatch))
1760 Invalid = true;
1761
1762 if (!Invalid &&
1763 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1764 TPC_ClassTemplateMember))
1765 Invalid = true;
1766
1767 ++ParamIdx;
1768 continue;
1769 }
1770
1771 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1772 << T
1773 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1774 Invalid = true;
1775 continue;
1776 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001777 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001778
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001779 // If there were at least as many template-ids as there were template
1780 // parameter lists, then there are no template parameter lists remaining for
1781 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001782 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001783 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001785 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001786 if (ParamIdx < NumParamLists - 1) {
1787 bool HasAnyExplicitSpecHeader = false;
1788 bool AllExplicitSpecHeaders = true;
1789 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1790 if (ParamLists[I]->size() == 0)
1791 HasAnyExplicitSpecHeader = true;
1792 else
1793 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001794 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001795
1796 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1797 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1798 : diag::err_template_spec_extra_headers)
1799 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1800 ParamLists[NumParamLists - 2]->getRAngleLoc());
1801
1802 // If there was a specialization somewhere, such that 'template<>' is
1803 // not required, and there were any 'template<>' headers, note where the
1804 // specialization occurred.
1805 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1806 Diag(ExplicitSpecLoc,
1807 diag::note_explicit_template_spec_does_not_need_header)
1808 << NestedTypes.back();
1809
1810 // We have a template parameter list with no corresponding scope, which
1811 // means that the resulting template declaration can't be instantiated
1812 // properly (we'll end up with dependent nodes when we shouldn't).
1813 if (!AllExplicitSpecHeaders)
1814 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001815 }
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Douglas Gregor89b9f102011-06-06 15:22:55 +00001817 // C++ [temp.expl.spec]p16:
1818 // In an explicit specialization declaration for a member of a class
1819 // template or a member template that ap- pears in namespace scope, the
1820 // member template and some of its enclosing class templates may remain
1821 // unspecialized, except that the declaration shall not explicitly
1822 // specialize a class member template if its en- closing class templates
1823 // are not explicitly specialized as well.
1824 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1825 SawNonEmptyTemplateParameterList) {
1826 Diag(DeclLoc, diag::err_specialize_member_of_template)
1827 << ParamLists[ParamIdx]->getSourceRange();
1828 Invalid = true;
1829 IsExplicitSpecialization = false;
1830 return 0;
1831 }
1832
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001833 // Return the last template parameter list, which corresponds to the
1834 // entity being declared.
1835 return ParamLists[NumParamLists - 1];
1836}
1837
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001838void Sema::NoteAllFoundTemplates(TemplateName Name) {
1839 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1840 Diag(Template->getLocation(), diag::note_template_declared_here)
1841 << (isa<FunctionTemplateDecl>(Template)? 0
1842 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001843 : isa<TypeAliasTemplateDecl>(Template)? 2
1844 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001845 << Template->getDeclName();
1846 return;
1847 }
1848
1849 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1850 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1851 IEnd = OST->end();
1852 I != IEnd; ++I)
1853 Diag((*I)->getLocation(), diag::note_template_declared_here)
1854 << 0 << (*I)->getDeclName();
1855
1856 return;
1857 }
1858}
1859
1860
Douglas Gregor7532dc62009-03-30 22:58:21 +00001861QualType Sema::CheckTemplateIdType(TemplateName Name,
1862 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001863 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001864 DependentTemplateName *DTN
1865 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001866 if (DTN && DTN->isIdentifier())
1867 // When building a template-id where the template-name is dependent,
1868 // assume the template is a type template. Either our assumption is
1869 // correct, or the code is ill-formed and will be diagnosed when the
1870 // dependent name is substituted.
1871 return Context.getDependentTemplateSpecializationType(ETK_None,
1872 DTN->getQualifier(),
1873 DTN->getIdentifier(),
1874 TemplateArgs);
1875
Douglas Gregor7532dc62009-03-30 22:58:21 +00001876 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001877 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1878 // We might have a substituted template template parameter pack. If so,
1879 // build a template specialization type for it.
1880 if (Name.getAsSubstTemplateTemplateParmPack())
1881 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001882
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001883 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1884 << Name;
1885 NoteAllFoundTemplates(Name);
1886 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001887 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001888
Douglas Gregor40808ce2009-03-09 23:48:35 +00001889 // Check that the template argument list is well-formed for this
1890 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001891 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001892 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001893 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001894 return QualType();
1895
Douglas Gregor910f8002010-11-07 23:05:16 +00001896 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001897 "Converted template argument list is too short!");
1898
1899 QualType CanonType;
1900
Douglas Gregor561f8122011-07-01 01:22:09 +00001901 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001902 if (TypeAliasTemplateDecl *AliasTemplate
1903 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1904 // Find the canonical type for this type alias template specialization.
1905 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1906 if (Pattern->isInvalidDecl())
1907 return QualType();
1908
1909 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1910 Converted.data(), Converted.size());
1911
1912 // Only substitute for the innermost template argument list.
1913 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001914 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001915 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1916 for (unsigned I = 0; I < Depth; ++I)
1917 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001918
1919 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1920 CanonType = SubstType(Pattern->getUnderlyingType(),
1921 TemplateArgLists, AliasTemplate->getLocation(),
1922 AliasTemplate->getDeclName());
1923 if (CanonType.isNull())
1924 return QualType();
1925 } else if (Name.isDependent() ||
1926 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001927 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001928 // This class template specialization is a dependent
1929 // type. Therefore, its canonical type is another class template
1930 // specialization type that contains all of the converted
1931 // arguments in canonical form. This ensures that, e.g., A<T> and
1932 // A<T, T> have identical types when A is declared as:
1933 //
1934 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001935 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001936 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001937 Converted.data(),
1938 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Douglas Gregor1275ae02009-07-28 23:00:59 +00001940 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001941 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001942 // In the future, we need to teach getTemplateSpecializationType to only
1943 // build the canonical type and return that to us.
1944 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001945
1946 // This might work out to be a current instantiation, in which
1947 // case the canonical type needs to be the InjectedClassNameType.
1948 //
1949 // TODO: in theory this could be a simple hashtable lookup; most
1950 // changes to CurContext don't change the set of current
1951 // instantiations.
1952 if (isa<ClassTemplateDecl>(Template)) {
1953 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1954 // If we get out to a namespace, we're done.
1955 if (Ctx->isFileContext()) break;
1956
1957 // If this isn't a record, keep looking.
1958 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1959 if (!Record) continue;
1960
1961 // Look for one of the two cases with InjectedClassNameTypes
1962 // and check whether it's the same template.
1963 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1964 !Record->getDescribedClassTemplate())
1965 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001966
John McCall31f17ec2010-04-27 00:57:59 +00001967 // Fetch the injected class name type and check whether its
1968 // injected type is equal to the type we just built.
1969 QualType ICNT = Context.getTypeDeclType(Record);
1970 QualType Injected = cast<InjectedClassNameType>(ICNT)
1971 ->getInjectedSpecializationType();
1972
1973 if (CanonType != Injected->getCanonicalTypeInternal())
1974 continue;
1975
1976 // If so, the canonical type of this TST is the injected
1977 // class name type of the record we just found.
1978 assert(ICNT.isCanonical());
1979 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001980 break;
1981 }
1982 }
Mike Stump1eb44332009-09-09 15:08:12 +00001983 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001984 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001985 // Find the class template specialization declaration that
1986 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001987 void *InsertPos = 0;
1988 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001989 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001990 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001991 if (!Decl) {
1992 // This is the first time we have referenced this class template
1993 // specialization. Create the canonical declaration and add it to
1994 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001995 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001996 ClassTemplate->getTemplatedDecl()->getTagKind(),
1997 ClassTemplate->getDeclContext(),
1998 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001999 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002000 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002001 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002002 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002003 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002004 Decl->setLexicalDeclContext(CurContext);
2005 }
2006
2007 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002008 assert(isa<RecordType>(CanonType) &&
2009 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Douglas Gregor40808ce2009-03-09 23:48:35 +00002012 // Build the fully-sugared type for this class template
2013 // specialization, which refers back to the class template
2014 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002015 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002016}
2017
John McCallf312b1e2010-08-26 23:41:50 +00002018TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002019Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2020 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002021 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002022 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002023 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002024 if (SS.isInvalid())
2025 return true;
2026
Douglas Gregor7532dc62009-03-30 22:58:21 +00002027 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002028
Douglas Gregor40808ce2009-03-09 23:48:35 +00002029 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002030 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002031 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002032
Douglas Gregora88f09f2011-02-28 17:23:35 +00002033 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2034 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2035 DTN->getQualifier(),
2036 DTN->getIdentifier(),
2037 TemplateArgs);
2038
2039 // Build type-source information.
2040 TypeLocBuilder TLB;
2041 DependentTemplateSpecializationTypeLoc SpecTL
2042 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002043 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002044 SpecTL.setNameLoc(TemplateLoc);
2045 SpecTL.setLAngleLoc(LAngleLoc);
2046 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002047 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002048 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2049 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2050 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2051 }
2052
John McCalld5532b62009-11-23 01:53:49 +00002053 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002054 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002055
2056 if (Result.isNull())
2057 return true;
2058
Douglas Gregor059101f2011-03-02 00:47:37 +00002059 // Build type-source information.
2060 TypeLocBuilder TLB;
2061 TemplateSpecializationTypeLoc SpecTL
2062 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2063 SpecTL.setTemplateNameLoc(TemplateLoc);
2064 SpecTL.setLAngleLoc(LAngleLoc);
2065 SpecTL.setRAngleLoc(RAngleLoc);
2066 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2067 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002068
Douglas Gregor059101f2011-03-02 00:47:37 +00002069 if (SS.isNotEmpty()) {
2070 // Create an elaborated-type-specifier containing the nested-name-specifier.
2071 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2072 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2073 ElabTL.setKeywordLoc(SourceLocation());
2074 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2075 }
2076
2077 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002078}
John McCallf1bbbb42009-09-04 01:14:41 +00002079
Douglas Gregor059101f2011-03-02 00:47:37 +00002080TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002081 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002082 SourceLocation TagLoc,
2083 CXXScopeSpec &SS,
2084 TemplateTy TemplateD,
2085 SourceLocation TemplateLoc,
2086 SourceLocation LAngleLoc,
2087 ASTTemplateArgsPtr TemplateArgsIn,
2088 SourceLocation RAngleLoc) {
2089 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2090
2091 // Translate the parser's template argument list in our AST format.
2092 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2093 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2094
2095 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002096 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002097 ElaboratedTypeKeyword Keyword
2098 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Douglas Gregor059101f2011-03-02 00:47:37 +00002100 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2101 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2102 DTN->getQualifier(),
2103 DTN->getIdentifier(),
2104 TemplateArgs);
2105
2106 // Build type-source information.
2107 TypeLocBuilder TLB;
2108 DependentTemplateSpecializationTypeLoc SpecTL
2109 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2110 SpecTL.setKeywordLoc(TagLoc);
2111 SpecTL.setNameLoc(TemplateLoc);
2112 SpecTL.setLAngleLoc(LAngleLoc);
2113 SpecTL.setRAngleLoc(RAngleLoc);
2114 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2115 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2116 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2117 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2118 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002119
2120 if (TypeAliasTemplateDecl *TAT =
2121 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2122 // C++0x [dcl.type.elab]p2:
2123 // If the identifier resolves to a typedef-name or the simple-template-id
2124 // resolves to an alias template specialization, the
2125 // elaborated-type-specifier is ill-formed.
2126 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2127 Diag(TAT->getLocation(), diag::note_declared_at);
2128 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002129
2130 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2131 if (Result.isNull())
2132 return TypeResult();
2133
2134 // Check the tag kind
2135 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002136 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002137
John McCall6b2becf2009-09-08 17:47:29 +00002138 IdentifierInfo *Id = D->getIdentifier();
2139 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002140
Richard Trieubbf34c02011-06-10 03:11:26 +00002141 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2142 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002143 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002144 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002145 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002146 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002147 }
2148 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002149
2150 // Provide source-location information for the template specialization.
2151 TypeLocBuilder TLB;
2152 TemplateSpecializationTypeLoc SpecTL
2153 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2154 SpecTL.setTemplateNameLoc(TemplateLoc);
2155 SpecTL.setLAngleLoc(LAngleLoc);
2156 SpecTL.setRAngleLoc(RAngleLoc);
2157 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2158 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002159
Douglas Gregor059101f2011-03-02 00:47:37 +00002160 // Construct an elaborated type containing the nested-name-specifier (if any)
2161 // and keyword.
2162 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2163 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2164 ElabTL.setKeywordLoc(TagLoc);
2165 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2166 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002167}
2168
John McCall60d7b3a2010-08-24 06:29:42 +00002169ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002170 LookupResult &R,
2171 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002172 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002173 // FIXME: Can we do any checking at this point? I guess we could check the
2174 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002175 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002176 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002177 // foo<int> could identify a single function unambiguously
2178 // This approach does NOT work, since f<int>(1);
2179 // gets resolved prior to resorting to overload resolution
2180 // i.e., template<class T> void f(double);
2181 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002182
2183 // These should be filtered out by our callers.
2184 assert(!R.empty() && "empty lookup results when building templateid");
2185 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2186
John McCallc373d482010-01-27 01:50:18 +00002187 // We don't want lookup warnings at this point.
2188 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002189
John McCallf7a1a742009-11-24 19:00:30 +00002190 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002191 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002192 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002193 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002194 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002195 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002196
2197 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002198}
2199
John McCallf7a1a742009-11-24 19:00:30 +00002200// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002201ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002202Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002203 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002204 const TemplateArgumentListInfo &TemplateArgs) {
2205 DeclContext *DC;
2206 if (!(DC = computeDeclContext(SS, false)) ||
2207 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002208 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002209 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002211 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002212 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002213 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2214 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002215
John McCallf7a1a742009-11-24 19:00:30 +00002216 if (R.isAmbiguous())
2217 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002218
John McCallf7a1a742009-11-24 19:00:30 +00002219 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002220 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2221 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002222 return ExprError();
2223 }
2224
2225 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002226 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2227 << (NestedNameSpecifier*) SS.getScopeRep()
2228 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002229 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2230 return ExprError();
2231 }
2232
2233 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002234}
2235
Douglas Gregorc45c2322009-03-31 00:43:58 +00002236/// \brief Form a dependent template name.
2237///
2238/// This action forms a dependent template name given the template
2239/// name and its (presumably dependent) scope specifier. For
2240/// example, given "MetaFun::template apply", the scope specifier \p
2241/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2242/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002243TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002244 SourceLocation TemplateKWLoc,
2245 CXXScopeSpec &SS,
2246 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002247 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002248 bool EnteringContext,
2249 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002250 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2251 !getLangOptions().CPlusPlus0x)
2252 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002253 << FixItHint::CreateRemoval(TemplateKWLoc);
2254
Douglas Gregor0707bc52010-01-19 16:01:07 +00002255 DeclContext *LookupCtx = 0;
2256 if (SS.isSet())
2257 LookupCtx = computeDeclContext(SS, EnteringContext);
2258 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002259 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002260 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002261 // C++0x [temp.names]p5:
2262 // If a name prefixed by the keyword template is not the name of
2263 // a template, the program is ill-formed. [Note: the keyword
2264 // template may not be applied to non-template members of class
2265 // templates. -end note ] [ Note: as is the case with the
2266 // typename prefix, the template prefix is allowed in cases
2267 // where it is not strictly necessary; i.e., when the
2268 // nested-name-specifier or the expression on the left of the ->
2269 // or . is not dependent on a template-parameter, or the use
2270 // does not appear in the scope of a template. -end note]
2271 //
2272 // Note: C++03 was more strict here, because it banned the use of
2273 // the "template" keyword prior to a template-name that was not a
2274 // dependent name. C++ DR468 relaxed this requirement (the
2275 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002276 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002277 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002278 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2279 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002280 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002281 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2282 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002283 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2284 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002285 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002286 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002287 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002288 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002289 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002290 << Name.getSourceRange()
2291 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002292 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002293 } else {
2294 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002295 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002296 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002297 }
2298
Mike Stump1eb44332009-09-09 15:08:12 +00002299 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002300 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002301
Douglas Gregor014e88d2009-11-03 23:16:33 +00002302 switch (Name.getKind()) {
2303 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002304 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002305 Name.Identifier));
2306 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002308 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002309 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002310 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002311 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002312
2313 case UnqualifiedId::IK_LiteralOperatorId:
2314 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2315
Douglas Gregor014e88d2009-11-03 23:16:33 +00002316 default:
2317 break;
2318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002319
2320 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002321 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002322 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002323 << Name.getSourceRange()
2324 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002325 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002326}
2327
Mike Stump1eb44332009-09-09 15:08:12 +00002328bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002329 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002330 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002331 const TemplateArgument &Arg = AL.getArgument();
2332
Anders Carlsson436b1562009-06-13 00:33:33 +00002333 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002334 switch(Arg.getKind()) {
2335 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002336 // C++ [temp.arg.type]p1:
2337 // A template-argument for a template-parameter which is a
2338 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002339 break;
2340 case TemplateArgument::Template: {
2341 // We have a template type parameter but the template argument
2342 // is a template without any arguments.
2343 SourceRange SR = AL.getSourceRange();
2344 TemplateName Name = Arg.getAsTemplate();
2345 Diag(SR.getBegin(), diag::err_template_missing_args)
2346 << Name << SR;
2347 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2348 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002349
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002350 return true;
2351 }
2352 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002353 // We have a template type parameter but the template argument
2354 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002355 SourceRange SR = AL.getSourceRange();
2356 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002357 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Anders Carlsson436b1562009-06-13 00:33:33 +00002359 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002360 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002361 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002362
John McCalla93c9342009-12-07 02:54:59 +00002363 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002364 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Anders Carlsson436b1562009-06-13 00:33:33 +00002366 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002367 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2368
2369 // Objective-C ARC:
2370 // If an explicitly-specified template argument type is a lifetime type
2371 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2372 if (getLangOptions().ObjCAutoRefCount &&
2373 ArgType->isObjCLifetimeType() &&
2374 !ArgType.getObjCLifetime()) {
2375 Qualifiers Qs;
2376 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2377 ArgType = Context.getQualifiedType(ArgType, Qs);
2378 }
2379
2380 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002381 return false;
2382}
2383
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002384/// \brief Substitute template arguments into the default template argument for
2385/// the given template type parameter.
2386///
2387/// \param SemaRef the semantic analysis object for which we are performing
2388/// the substitution.
2389///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002390/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002391/// for.
2392///
2393/// \param TemplateLoc the location of the template name that started the
2394/// template-id we are checking.
2395///
2396/// \param RAngleLoc the location of the right angle bracket ('>') that
2397/// terminates the template-id.
2398///
2399/// \param Param the template template parameter whose default we are
2400/// substituting into.
2401///
2402/// \param Converted the list of template arguments provided for template
2403/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002404/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002405static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002406SubstDefaultTemplateArgument(Sema &SemaRef,
2407 TemplateDecl *Template,
2408 SourceLocation TemplateLoc,
2409 SourceLocation RAngleLoc,
2410 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002411 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002412 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002413
2414 // If the argument type is dependent, instantiate it now based
2415 // on the previously-computed template arguments.
2416 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002417 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002418 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002420 MultiLevelTemplateArgumentList AllTemplateArgs
2421 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2422
2423 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002424 Template, Converted.data(),
2425 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002426 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002428 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2429 Param->getDefaultArgumentLoc(),
2430 Param->getDeclName());
2431 }
2432
2433 return ArgType;
2434}
2435
2436/// \brief Substitute template arguments into the default template argument for
2437/// the given non-type template parameter.
2438///
2439/// \param SemaRef the semantic analysis object for which we are performing
2440/// the substitution.
2441///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002443/// for.
2444///
2445/// \param TemplateLoc the location of the template name that started the
2446/// template-id we are checking.
2447///
2448/// \param RAngleLoc the location of the right angle bracket ('>') that
2449/// terminates the template-id.
2450///
Douglas Gregor788cd062009-11-11 01:00:40 +00002451/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002452/// substituting into.
2453///
2454/// \param Converted the list of template arguments provided for template
2455/// parameters that precede \p Param in the template parameter list.
2456///
2457/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002458static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002459SubstDefaultTemplateArgument(Sema &SemaRef,
2460 TemplateDecl *Template,
2461 SourceLocation TemplateLoc,
2462 SourceLocation RAngleLoc,
2463 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002464 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002466 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002468 MultiLevelTemplateArgumentList AllTemplateArgs
2469 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002470
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002471 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002472 Template, Converted.data(),
2473 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002474 SourceRange(TemplateLoc, RAngleLoc));
2475
2476 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2477}
2478
Douglas Gregor788cd062009-11-11 01:00:40 +00002479/// \brief Substitute template arguments into the default template argument for
2480/// the given template template parameter.
2481///
2482/// \param SemaRef the semantic analysis object for which we are performing
2483/// the substitution.
2484///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002485/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002486/// for.
2487///
2488/// \param TemplateLoc the location of the template name that started the
2489/// template-id we are checking.
2490///
2491/// \param RAngleLoc the location of the right angle bracket ('>') that
2492/// terminates the template-id.
2493///
2494/// \param Param the template template parameter whose default we are
2495/// substituting into.
2496///
2497/// \param Converted the list of template arguments provided for template
2498/// parameters that precede \p Param in the template parameter list.
2499///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002500/// \param QualifierLoc Will be set to the nested-name-specifier (with
2501/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002502///
Douglas Gregor788cd062009-11-11 01:00:40 +00002503/// \returns the substituted template argument, or NULL if an error occurred.
2504static TemplateName
2505SubstDefaultTemplateArgument(Sema &SemaRef,
2506 TemplateDecl *Template,
2507 SourceLocation TemplateLoc,
2508 SourceLocation RAngleLoc,
2509 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002510 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002511 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002512 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002513 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514
Douglas Gregor788cd062009-11-11 01:00:40 +00002515 MultiLevelTemplateArgumentList AllTemplateArgs
2516 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002517
Douglas Gregor788cd062009-11-11 01:00:40 +00002518 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002519 Template, Converted.data(),
2520 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002521 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002522
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002523 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002524 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002525 if (QualifierLoc) {
2526 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2527 AllTemplateArgs);
2528 if (!QualifierLoc)
2529 return TemplateName();
2530 }
2531
Douglas Gregor1d752d72011-03-02 18:46:51 +00002532 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002533 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002535 AllTemplateArgs);
2536}
2537
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002538/// \brief If the given template parameter has a default template
2539/// argument, substitute into that default template argument and
2540/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002541TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002542Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2543 SourceLocation TemplateLoc,
2544 SourceLocation RAngleLoc,
2545 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002546 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002547 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002548 if (!TypeParm->hasDefaultArgument())
2549 return TemplateArgumentLoc();
2550
John McCalla93c9342009-12-07 02:54:59 +00002551 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002552 TemplateLoc,
2553 RAngleLoc,
2554 TypeParm,
2555 Converted);
2556 if (DI)
2557 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2558
2559 return TemplateArgumentLoc();
2560 }
2561
2562 if (NonTypeTemplateParmDecl *NonTypeParm
2563 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2564 if (!NonTypeParm->hasDefaultArgument())
2565 return TemplateArgumentLoc();
2566
John McCall60d7b3a2010-08-24 06:29:42 +00002567 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002568 TemplateLoc,
2569 RAngleLoc,
2570 NonTypeParm,
2571 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002572 if (Arg.isInvalid())
2573 return TemplateArgumentLoc();
2574
2575 Expr *ArgE = Arg.takeAs<Expr>();
2576 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2577 }
2578
2579 TemplateTemplateParmDecl *TempTempParm
2580 = cast<TemplateTemplateParmDecl>(Param);
2581 if (!TempTempParm->hasDefaultArgument())
2582 return TemplateArgumentLoc();
2583
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002584
Douglas Gregor1d752d72011-03-02 18:46:51 +00002585 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002586 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002587 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002588 RAngleLoc,
2589 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002590 Converted,
2591 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002592 if (TName.isNull())
2593 return TemplateArgumentLoc();
2594
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002595 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002596 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002597 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2598}
2599
Douglas Gregore7526412009-11-11 19:31:23 +00002600/// \brief Check that the given template argument corresponds to the given
2601/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002602///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002603/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002604/// checked.
2605///
2606/// \param Arg The template argument.
2607///
2608/// \param Template The template in which the template argument resides.
2609///
2610/// \param TemplateLoc The location of the template name for the template
2611/// whose argument list we're matching.
2612///
2613/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2614/// the template argument list.
2615///
2616/// \param ArgumentPackIndex The index into the argument pack where this
2617/// argument will be placed. Only valid if the parameter is a parameter pack.
2618///
2619/// \param Converted The checked, converted argument will be added to the
2620/// end of this small vector.
2621///
2622/// \param CTAK Describes how we arrived at this particular template argument:
2623/// explicitly written, deduced, etc.
2624///
2625/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002626bool Sema::CheckTemplateArgument(NamedDecl *Param,
2627 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002628 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002629 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002630 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002631 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002632 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002633 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002634 // Check template type parameters.
2635 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002636 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637
Douglas Gregord9e15302009-11-11 19:41:09 +00002638 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002640 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002641 // with the template arguments we've seen thus far. But if the
2642 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002643 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002644 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2645 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002646
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002647 if (NTTPType->isDependentType() &&
2648 !isa<TemplateTemplateParmDecl>(Template) &&
2649 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002650 // Do substitution on the type of the non-type template parameter.
2651 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002652 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002653 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002654
2655 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002656 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002657 NTTPType = SubstType(NTTPType,
2658 MultiLevelTemplateArgumentList(TemplateArgs),
2659 NTTP->getLocation(),
2660 NTTP->getDeclName());
2661 // If that worked, check the non-type template parameter type
2662 // for validity.
2663 if (!NTTPType.isNull())
2664 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2665 NTTP->getLocation());
2666 if (NTTPType.isNull())
2667 return true;
2668 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669
Douglas Gregore7526412009-11-11 19:31:23 +00002670 switch (Arg.getArgument().getKind()) {
2671 case TemplateArgument::Null:
2672 assert(false && "Should never see a NULL template argument here");
2673 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002674
Douglas Gregore7526412009-11-11 19:31:23 +00002675 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002676 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002677 ExprResult Res =
2678 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2679 Result, CTAK);
2680 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002681 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002682
Douglas Gregor910f8002010-11-07 23:05:16 +00002683 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002684 break;
2685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Douglas Gregore7526412009-11-11 19:31:23 +00002687 case TemplateArgument::Declaration:
2688 case TemplateArgument::Integral:
2689 // We've already checked this template argument, so just copy
2690 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002691 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002692 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002693
Douglas Gregore7526412009-11-11 19:31:23 +00002694 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002695 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002696 // We were given a template template argument. It may not be ill-formed;
2697 // see below.
2698 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002699 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2700 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002701 // We have a template argument such as \c T::template X, which we
2702 // parsed as a template template argument. However, since we now
2703 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002704 // template name into an expression.
2705
2706 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2707 Arg.getTemplateNameLoc());
2708
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002709 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002710 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002711 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002712 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002713 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714
Douglas Gregora7fc9012011-01-05 18:58:31 +00002715 // If we parsed the template argument as a pack expansion, create a
2716 // pack expansion expression.
2717 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002718 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2719 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002720 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002721 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722
Douglas Gregore7526412009-11-11 19:31:23 +00002723 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002724 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2725 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002726 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727
Douglas Gregor910f8002010-11-07 23:05:16 +00002728 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002729 break;
2730 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731
Douglas Gregore7526412009-11-11 19:31:23 +00002732 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002733 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002734 // therefore cannot be a non-type template argument.
2735 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2736 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregore7526412009-11-11 19:31:23 +00002738 Diag(Param->getLocation(), diag::note_template_param_here);
2739 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740
Douglas Gregore7526412009-11-11 19:31:23 +00002741 case TemplateArgument::Type: {
2742 // We have a non-type template parameter but the template
2743 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregore7526412009-11-11 19:31:23 +00002745 // C++ [temp.arg]p2:
2746 // In a template-argument, an ambiguity between a type-id and
2747 // an expression is resolved to a type-id, regardless of the
2748 // form of the corresponding template-parameter.
2749 //
2750 // We warn specifically about this case, since it can be rather
2751 // confusing for users.
2752 QualType T = Arg.getArgument().getAsType();
2753 SourceRange SR = Arg.getSourceRange();
2754 if (T->isFunctionType())
2755 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2756 else
2757 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2758 Diag(Param->getLocation(), diag::note_template_param_here);
2759 return true;
2760 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761
Douglas Gregore7526412009-11-11 19:31:23 +00002762 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002763 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002764 break;
2765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768 }
2769
2770
Douglas Gregore7526412009-11-11 19:31:23 +00002771 // Check template template parameters.
2772 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773
Douglas Gregore7526412009-11-11 19:31:23 +00002774 // Substitute into the template parameter list of the template
2775 // template parameter, since previously-supplied template arguments
2776 // may appear within the template template parameter.
2777 {
2778 // Set up a template instantiation context.
2779 LocalInstantiationScope Scope(*this);
2780 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002781 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002782 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
2784 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002785 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002786 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002787 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002788 MultiLevelTemplateArgumentList(TemplateArgs)));
2789 if (!TempParm)
2790 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002791 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002792
Douglas Gregore7526412009-11-11 19:31:23 +00002793 switch (Arg.getArgument().getKind()) {
2794 case TemplateArgument::Null:
2795 assert(false && "Should never see a NULL template argument here");
2796 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797
Douglas Gregore7526412009-11-11 19:31:23 +00002798 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002799 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002800 if (CheckTemplateArgument(TempParm, Arg))
2801 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802
Douglas Gregor910f8002010-11-07 23:05:16 +00002803 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002804 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002805
Douglas Gregore7526412009-11-11 19:31:23 +00002806 case TemplateArgument::Expression:
2807 case TemplateArgument::Type:
2808 // We have a template template parameter but the template
2809 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002810 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2811 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002812 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002813
Douglas Gregore7526412009-11-11 19:31:23 +00002814 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002815 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002816 "Declaration argument with template template parameter");
2817 break;
2818 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002819 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002820 "Integral argument with template template parameter");
2821 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002822
Douglas Gregore7526412009-11-11 19:31:23 +00002823 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002824 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002825 break;
2826 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002827
Douglas Gregore7526412009-11-11 19:31:23 +00002828 return false;
2829}
2830
Douglas Gregorc15cb382009-02-09 23:23:08 +00002831/// \brief Check that the given template argument list is well-formed
2832/// for specializing the given template.
2833bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2834 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002835 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002836 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002837 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002838 TemplateParameterList *Params = Template->getTemplateParameters();
2839 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002840 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002841 bool Invalid = false;
2842
John McCalld5532b62009-11-23 01:53:49 +00002843 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2844
Mike Stump1eb44332009-09-09 15:08:12 +00002845 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002846 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002848 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002849 (NumArgs < Params->getMinRequiredArguments() &&
2850 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002851 // FIXME: point at either the first arg beyond what we can handle,
2852 // or the '>', depending on whether we have too many or too few
2853 // arguments.
2854 SourceRange Range;
2855 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002856 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002857 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2858 << (NumArgs > NumParams)
2859 << (isa<ClassTemplateDecl>(Template)? 0 :
2860 isa<FunctionTemplateDecl>(Template)? 1 :
2861 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2862 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002863 Diag(Template->getLocation(), diag::note_template_decl_here)
2864 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002865 Invalid = true;
2866 }
Mike Stump1eb44332009-09-09 15:08:12 +00002867
2868 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002869 // [...] The type and form of each template-argument specified in
2870 // a template-id shall match the type and form specified for the
2871 // corresponding parameter declared by the template in its
2872 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002873 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002874 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002875 TemplateParameterList::iterator Param = Params->begin(),
2876 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002877 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002878 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002879 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002880 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002881 // If we have an expanded parameter pack, make sure we don't have too
2882 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002883 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002884 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002885 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002886 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2887 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2888 << true
2889 << (isa<ClassTemplateDecl>(Template)? 0 :
2890 isa<FunctionTemplateDecl>(Template)? 1 :
2891 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2892 << Template;
2893 Diag(Template->getLocation(), diag::note_template_decl_here)
2894 << Params->getSourceRange();
2895 return true;
2896 }
2897 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002898
Douglas Gregorf35f8282009-11-11 21:54:23 +00002899 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002900 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2901 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002902 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002903 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002904
Douglas Gregor14be16b2010-12-20 16:57:52 +00002905 if ((*Param)->isTemplateParameterPack()) {
2906 // The template parameter was a template parameter pack, so take the
2907 // deduced argument and place it on the argument pack. Note that we
2908 // stay on the same template parameter so that we can deduce more
2909 // arguments.
2910 ArgumentPack.push_back(Converted.back());
2911 Converted.pop_back();
2912 } else {
2913 // Move to the next template parameter.
2914 ++Param;
2915 }
2916 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002917 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002918 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002919
Douglas Gregor8735b292011-06-03 02:59:40 +00002920 // If we're checking a partial template argument list, we're done.
2921 if (PartialTemplateArgs) {
2922 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2923 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2924 ArgumentPack.data(),
2925 ArgumentPack.size()));
2926
2927 return Invalid;
2928 }
2929
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002931 // arguments, just break out now and we'll fill in the argument pack below.
2932 if ((*Param)->isTemplateParameterPack())
2933 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002934
Douglas Gregorf35f8282009-11-11 21:54:23 +00002935 // We have a default template argument that we will use.
2936 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937
Douglas Gregorf35f8282009-11-11 21:54:23 +00002938 // Retrieve the default template argument from the template
2939 // parameter. For each kind of template parameter, we substitute the
2940 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002941 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002942 // the default argument.
2943 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2944 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002945 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002946 break;
2947 }
2948
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002949 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002950 Template,
2951 TemplateLoc,
2952 RAngleLoc,
2953 TTP,
2954 Converted);
2955 if (!ArgType)
2956 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002957
Douglas Gregorf35f8282009-11-11 21:54:23 +00002958 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2959 ArgType);
2960 } else if (NonTypeTemplateParmDecl *NTTP
2961 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2962 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002963 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002964 break;
2965 }
2966
John McCall60d7b3a2010-08-24 06:29:42 +00002967 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968 TemplateLoc,
2969 RAngleLoc,
2970 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002971 Converted);
2972 if (E.isInvalid())
2973 return true;
2974
2975 Expr *Ex = E.takeAs<Expr>();
2976 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2977 } else {
2978 TemplateTemplateParmDecl *TempParm
2979 = cast<TemplateTemplateParmDecl>(*Param);
2980
2981 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002982 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002983 break;
2984 }
2985
Douglas Gregor1d752d72011-03-02 18:46:51 +00002986 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002987 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002988 TemplateLoc,
2989 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002990 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002991 Converted,
2992 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002993 if (Name.isNull())
2994 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002995
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002996 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2997 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002998 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002999
Douglas Gregorf35f8282009-11-11 21:54:23 +00003000 // Introduce an instantiation record that describes where we are using
3001 // the default template argument.
3002 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003003 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003004 SourceRange(TemplateLoc, RAngleLoc));
3005
Douglas Gregorf35f8282009-11-11 21:54:23 +00003006 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003007 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003008 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003009 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003010
Douglas Gregor67714232011-03-03 02:41:12 +00003011 // Core issue 150 (assumed resolution): if this is a template template
3012 // parameter, keep track of the default template arguments from the
3013 // template definition.
3014 if (isTemplateTemplateParameter)
3015 TemplateArgs.addArgument(Arg);
3016
Douglas Gregor14be16b2010-12-20 16:57:52 +00003017 // Move to the next template parameter and argument.
3018 ++Param;
3019 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003020 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003021
Douglas Gregor14be16b2010-12-20 16:57:52 +00003022 // Form argument packs for each of the parameter packs remaining.
3023 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003024 // If we're checking a partial list of template arguments, don't fill
3025 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
3027 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003028 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003029 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003030 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3032 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003033 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003034 ArgumentPack.clear();
3035 }
3036 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003037
Douglas Gregor14be16b2010-12-20 16:57:52 +00003038 ++Param;
3039 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003040
Douglas Gregorc15cb382009-02-09 23:23:08 +00003041 return Invalid;
3042}
3043
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003044namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003045 class UnnamedLocalNoLinkageFinder
3046 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003047 {
3048 Sema &S;
3049 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003050
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003051 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003053 public:
3054 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3055
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056 bool Visit(QualType T) {
3057 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003058 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003059
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003060#define TYPE(Class, Parent) \
3061 bool Visit##Class##Type(const Class##Type *);
3062#define ABSTRACT_TYPE(Class, Parent) \
3063 bool Visit##Class##Type(const Class##Type *) { return false; }
3064#define NON_CANONICAL_TYPE(Class, Parent) \
3065 bool Visit##Class##Type(const Class##Type *) { return false; }
3066#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003067
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003068 bool VisitTagDecl(const TagDecl *Tag);
3069 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3070 };
3071}
3072
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003073bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003074 return false;
3075}
3076
3077bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3078 return Visit(T->getElementType());
3079}
3080
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003081bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003082 return Visit(T->getPointeeType());
3083}
3084
3085bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003086 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003087 return Visit(T->getPointeeType());
3088}
3089
3090bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003091 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003092 return Visit(T->getPointeeType());
3093}
3094
3095bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 return Visit(T->getPointeeType());
3098}
3099
3100bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003101 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003102 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3103}
3104
3105bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003106 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003107 return Visit(T->getElementType());
3108}
3109
3110bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003111 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003112 return Visit(T->getElementType());
3113}
3114
3115bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003116 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003117 return Visit(T->getElementType());
3118}
3119
3120bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003121 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003122 return Visit(T->getElementType());
3123}
3124
3125bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003126 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003127 return Visit(T->getElementType());
3128}
3129
3130bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3131 return Visit(T->getElementType());
3132}
3133
3134bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3135 return Visit(T->getElementType());
3136}
3137
3138bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3139 const FunctionProtoType* T) {
3140 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003141 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003142 A != AEnd; ++A) {
3143 if (Visit(*A))
3144 return true;
3145 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003146
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003147 return Visit(T->getResultType());
3148}
3149
3150bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3151 const FunctionNoProtoType* T) {
3152 return Visit(T->getResultType());
3153}
3154
3155bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3156 const UnresolvedUsingType*) {
3157 return false;
3158}
3159
3160bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3161 return false;
3162}
3163
3164bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3165 return Visit(T->getUnderlyingType());
3166}
3167
3168bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3169 return false;
3170}
3171
Sean Huntca63c202011-05-24 22:41:36 +00003172bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3173 const UnaryTransformType*) {
3174 return false;
3175}
3176
Richard Smith34b41d92011-02-20 03:19:35 +00003177bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3178 return Visit(T->getDeducedType());
3179}
3180
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003181bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3182 return VisitTagDecl(T->getDecl());
3183}
3184
3185bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3186 return VisitTagDecl(T->getDecl());
3187}
3188
3189bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3190 const TemplateTypeParmType*) {
3191 return false;
3192}
3193
Douglas Gregorc3069d62011-01-14 02:55:32 +00003194bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3195 const SubstTemplateTypeParmPackType *) {
3196 return false;
3197}
3198
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003199bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3200 const TemplateSpecializationType*) {
3201 return false;
3202}
3203
3204bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3205 const InjectedClassNameType* T) {
3206 return VisitTagDecl(T->getDecl());
3207}
3208
3209bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3210 const DependentNameType* T) {
3211 return VisitNestedNameSpecifier(T->getQualifier());
3212}
3213
3214bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3215 const DependentTemplateSpecializationType* T) {
3216 return VisitNestedNameSpecifier(T->getQualifier());
3217}
3218
Douglas Gregor7536dd52010-12-20 02:24:11 +00003219bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3220 const PackExpansionType* T) {
3221 return Visit(T->getPattern());
3222}
3223
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003224bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3225 return false;
3226}
3227
3228bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3229 const ObjCInterfaceType *) {
3230 return false;
3231}
3232
3233bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3234 const ObjCObjectPointerType *) {
3235 return false;
3236}
3237
3238bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3239 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3240 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3241 << S.Context.getTypeDeclType(Tag) << SR;
3242 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003243 }
3244
Richard Smith162e1c12011-04-15 14:24:37 +00003245 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003246 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3247 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3248 return true;
3249 }
3250
3251 return false;
3252}
3253
3254bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3255 NestedNameSpecifier *NNS) {
3256 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3257 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003258
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003259 switch (NNS->getKind()) {
3260 case NestedNameSpecifier::Identifier:
3261 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003262 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003263 case NestedNameSpecifier::Global:
3264 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003265
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003266 case NestedNameSpecifier::TypeSpec:
3267 case NestedNameSpecifier::TypeSpecWithTemplate:
3268 return Visit(QualType(NNS->getAsType(), 0));
3269 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003270 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003271}
3272
3273
Douglas Gregorc15cb382009-02-09 23:23:08 +00003274/// \brief Check a template argument against its corresponding
3275/// template type parameter.
3276///
3277/// This routine implements the semantics of C++ [temp.arg.type]. It
3278/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003279bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003280 TypeSourceInfo *ArgInfo) {
3281 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003282 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003283 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003284
3285 if (Arg->isVariablyModifiedType()) {
3286 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003287 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003288 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003289 }
3290
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003291 // C++03 [temp.arg.type]p2:
3292 // A local type, a type with no linkage, an unnamed type or a type
3293 // compounded from any of these types shall not be used as a
3294 // template-argument for a template type-parameter.
3295 //
3296 // C++0x allows these, and even in C++03 we allow them as an extension with
3297 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003298 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003299 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3300 (void)Finder.Visit(Context.getCanonicalType(Arg));
3301 }
3302
Douglas Gregorc15cb382009-02-09 23:23:08 +00003303 return false;
3304}
3305
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003306/// \brief Checks whether the given template argument is the address
3307/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003308static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003309CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3310 NonTypeTemplateParmDecl *Param,
3311 QualType ParamType,
3312 Expr *ArgIn,
3313 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003314 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003315 Expr *Arg = ArgIn;
3316 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003317
3318 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003319 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003320
3321 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003322 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003323 // A template-argument for a non-type, non-template
3324 // template-parameter shall be one of: [...]
3325 //
3326 // -- the address of an object or function with external
3327 // linkage, including function templates and function
3328 // template-ids but excluding non-static class members,
3329 // expressed as & id-expression where the & is optional if
3330 // the name refers to a function or array, or if the
3331 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003333 // In C++98/03 mode, give an extension warning on any extra parentheses.
3334 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3335 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003336 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003337 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003338 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003339 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003340 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003341 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003342 }
3343
3344 Arg = Parens->getSubExpr();
3345 }
3346
John McCall91a57552011-07-15 05:09:51 +00003347 while (SubstNonTypeTemplateParmExpr *subst =
3348 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3349 Arg = subst->getReplacement()->IgnoreImpCasts();
3350
Douglas Gregorb7a09262010-04-01 18:32:35 +00003351 bool AddressTaken = false;
3352 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003353 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003354 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003355 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003356 AddressTaken = true;
3357 AddrOpLoc = UnOp->getOperatorLoc();
3358 }
Francois Picheta343a412011-04-29 09:08:14 +00003359 }
John McCall91a57552011-07-15 05:09:51 +00003360
3361 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3362 Converted = TemplateArgument(ArgIn);
3363 return false;
3364 }
3365
3366 while (SubstNonTypeTemplateParmExpr *subst =
3367 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3368 Arg = subst->getReplacement()->IgnoreImpCasts();
3369
3370 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003371 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003372 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3373 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003374 S.Diag(Param->getLocation(), diag::note_template_param_here);
3375 return true;
3376 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003377
3378 // Stop checking the precise nature of the argument if it is value dependent,
3379 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003380 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003381 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003382 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003383 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003384
Douglas Gregorb7a09262010-04-01 18:32:35 +00003385 if (!isa<ValueDecl>(DRE->getDecl())) {
3386 S.Diag(Arg->getSourceRange().getBegin(),
3387 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003389 S.Diag(Param->getLocation(), diag::note_template_param_here);
3390 return true;
3391 }
3392
3393 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003394
3395 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003396 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3397 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003398 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003399 S.Diag(Param->getLocation(), diag::note_template_param_here);
3400 return true;
3401 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003402
3403 // Cannot refer to non-static member functions
3404 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003405 if (!Method->isStatic()) {
3406 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003407 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003408 S.Diag(Param->getLocation(), diag::note_template_param_here);
3409 return true;
3410 }
Mike Stump1eb44332009-09-09 15:08:12 +00003411
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003412 // Functions must have external linkage.
3413 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003414 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003415 S.Diag(Arg->getSourceRange().getBegin(),
3416 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003417 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003418 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003419 << true;
3420 return true;
3421 }
3422
3423 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003424 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003425
Douglas Gregorb7a09262010-04-01 18:32:35 +00003426 // If the template parameter has pointer type, the function decays.
3427 if (ParamType->isPointerType() && !AddressTaken)
3428 ArgType = S.Context.getPointerType(Func->getType());
3429 else if (AddressTaken && ParamType->isReferenceType()) {
3430 // If we originally had an address-of operator, but the
3431 // parameter has reference type, complain and (if things look
3432 // like they will work) drop the address-of operator.
3433 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3434 ParamType.getNonReferenceType())) {
3435 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3436 << ParamType;
3437 S.Diag(Param->getLocation(), diag::note_template_param_here);
3438 return true;
3439 }
3440
3441 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3442 << ParamType
3443 << FixItHint::CreateRemoval(AddrOpLoc);
3444 S.Diag(Param->getLocation(), diag::note_template_param_here);
3445
3446 ArgType = Func->getType();
3447 }
3448 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003449 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003450 S.Diag(Arg->getSourceRange().getBegin(),
3451 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003452 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003453 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454 << true;
3455 return true;
3456 }
3457
Douglas Gregorb7a09262010-04-01 18:32:35 +00003458 // A value of reference type is not an object.
3459 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003460 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003461 diag::err_template_arg_reference_var)
3462 << Var->getType() << Arg->getSourceRange();
3463 S.Diag(Param->getLocation(), diag::note_template_param_here);
3464 return true;
3465 }
3466
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003467 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003468 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003469
3470 // If the template parameter has pointer type, we must have taken
3471 // the address of this object.
3472 if (ParamType->isReferenceType()) {
3473 if (AddressTaken) {
3474 // If we originally had an address-of operator, but the
3475 // parameter has reference type, complain and (if things look
3476 // like they will work) drop the address-of operator.
3477 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3478 ParamType.getNonReferenceType())) {
3479 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3480 << ParamType;
3481 S.Diag(Param->getLocation(), diag::note_template_param_here);
3482 return true;
3483 }
3484
3485 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3486 << ParamType
3487 << FixItHint::CreateRemoval(AddrOpLoc);
3488 S.Diag(Param->getLocation(), diag::note_template_param_here);
3489
3490 ArgType = Var->getType();
3491 }
3492 } else if (!AddressTaken && ParamType->isPointerType()) {
3493 if (Var->getType()->isArrayType()) {
3494 // Array-to-pointer decay.
3495 ArgType = S.Context.getArrayDecayedType(Var->getType());
3496 } else {
3497 // If the template parameter has pointer type but the address of
3498 // this object was not taken, complain and (possibly) recover by
3499 // taking the address of the entity.
3500 ArgType = S.Context.getPointerType(Var->getType());
3501 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3502 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3503 << ParamType;
3504 S.Diag(Param->getLocation(), diag::note_template_param_here);
3505 return true;
3506 }
3507
3508 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3509 << ParamType
3510 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3511
3512 S.Diag(Param->getLocation(), diag::note_template_param_here);
3513 }
3514 }
3515 } else {
3516 // We found something else, but we don't know specifically what it is.
3517 S.Diag(Arg->getSourceRange().getBegin(),
3518 diag::err_template_arg_not_object_or_func)
3519 << Arg->getSourceRange();
3520 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3521 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003522 }
Mike Stump1eb44332009-09-09 15:08:12 +00003523
John McCallf85e1932011-06-15 23:02:42 +00003524 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003525 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003526 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003527 S.IsQualificationConversion(ArgType, ParamType, false,
3528 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003529 // For pointer-to-object types, qualification conversions are
3530 // permitted.
3531 } else {
3532 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3533 if (!ParamRef->getPointeeType()->isFunctionType()) {
3534 // C++ [temp.arg.nontype]p5b3:
3535 // For a non-type template-parameter of type reference to
3536 // object, no conversions apply. The type referred to by the
3537 // reference may be more cv-qualified than the (otherwise
3538 // identical) type of the template- argument. The
3539 // template-parameter is bound directly to the
3540 // template-argument, which shall be an lvalue.
3541
3542 // FIXME: Other qualifiers?
3543 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3544 unsigned ArgQuals = ArgType.getCVRQualifiers();
3545
3546 if ((ParamQuals | ArgQuals) != ParamQuals) {
3547 S.Diag(Arg->getSourceRange().getBegin(),
3548 diag::err_template_arg_ref_bind_ignores_quals)
3549 << ParamType << Arg->getType()
3550 << Arg->getSourceRange();
3551 S.Diag(Param->getLocation(), diag::note_template_param_here);
3552 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003553 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003554 }
3555 }
3556
3557 // At this point, the template argument refers to an object or
3558 // function with external linkage. We now need to check whether the
3559 // argument and parameter types are compatible.
3560 if (!S.Context.hasSameUnqualifiedType(ArgType,
3561 ParamType.getNonReferenceType())) {
3562 // We can't perform this conversion or binding.
3563 if (ParamType->isReferenceType())
3564 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003565 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003566 else
3567 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003568 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003569 S.Diag(Param->getLocation(), diag::note_template_param_here);
3570 return true;
3571 }
3572 }
3573
3574 // Create the template argument.
3575 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003576 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003577 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003578}
3579
3580/// \brief Checks whether the given template argument is a pointer to
3581/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003582bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003583 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003584 bool Invalid = false;
3585
3586 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003587 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003588 Arg = Cast->getSubExpr();
3589
3590 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003591 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003592 // A template-argument for a non-type, non-template
3593 // template-parameter shall be one of: [...]
3594 //
3595 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003596 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003597
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003598 // In C++98/03 mode, give an extension warning on any extra parentheses.
3599 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3600 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003601 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003602 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003603 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003604 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003605 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003606 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003607 }
3608
3609 Arg = Parens->getSubExpr();
3610 }
3611
John McCall91a57552011-07-15 05:09:51 +00003612 while (SubstNonTypeTemplateParmExpr *subst =
3613 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3614 Arg = subst->getReplacement()->IgnoreImpCasts();
3615
Douglas Gregorcaddba02009-11-12 18:38:13 +00003616 // A pointer-to-member constant written &Class::member.
3617 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003618 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003619 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3620 if (DRE && !DRE->getQualifier())
3621 DRE = 0;
3622 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003623 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003624 // A constant of pointer-to-member type.
3625 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3626 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3627 if (VD->getType()->isMemberPointerType()) {
3628 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003629 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003630 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3631 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003632 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003633 else
3634 Converted = TemplateArgument(VD->getCanonicalDecl());
3635 return Invalid;
3636 }
3637 }
3638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003639
Douglas Gregorcaddba02009-11-12 18:38:13 +00003640 DRE = 0;
3641 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003642
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003643 if (!DRE)
3644 return Diag(Arg->getSourceRange().getBegin(),
3645 diag::err_template_arg_not_pointer_to_member_form)
3646 << Arg->getSourceRange();
3647
3648 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3649 assert((isa<FieldDecl>(DRE->getDecl()) ||
3650 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3651 "Only non-static member pointers can make it here");
3652
3653 // Okay: this is the address of a non-static member, and therefore
3654 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003655 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003656 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003657 else
3658 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003659 return Invalid;
3660 }
3661
3662 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003663 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003664 diag::err_template_arg_not_pointer_to_member_form)
3665 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003666 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003667 diag::note_template_arg_refers_here);
3668 return true;
3669}
3670
Douglas Gregorc15cb382009-02-09 23:23:08 +00003671/// \brief Check a template argument against its corresponding
3672/// non-type template parameter.
3673///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003674/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003675/// If an error occurred, it returns ExprError(); otherwise, it
3676/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003677/// InstantiatedParamType is the type of the non-type template
3678/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003679ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3680 QualType InstantiatedParamType, Expr *Arg,
3681 TemplateArgument &Converted,
3682 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003683 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3684
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003685 // If either the parameter has a dependent type or the argument is
3686 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003687 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3688 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003689 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003690 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003691 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003692
3693 // C++ [temp.arg.nontype]p5:
3694 // The following conversions are performed on each expression used
3695 // as a non-type template-argument. If a non-type
3696 // template-argument cannot be converted to the type of the
3697 // corresponding template-parameter then the program is
3698 // ill-formed.
3699 //
3700 // -- for a non-type template-parameter of integral or
3701 // enumeration type, integral promotions (4.5) and integral
3702 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003703 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003704 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003705 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003706 // C++ [temp.arg.nontype]p1:
3707 // A template-argument for a non-type, non-template
3708 // template-parameter shall be one of:
3709 //
3710 // -- an integral constant-expression of integral or enumeration
3711 // type; or
3712 // -- the name of a non-type template-parameter; or
3713 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003714 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003715 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003716 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003717 diag::err_template_arg_not_integral_or_enumeral)
3718 << ArgType << Arg->getSourceRange();
3719 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003720 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003721 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003722 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003723 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3724 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003725 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003726 }
3727
Douglas Gregor02024a92010-03-28 02:42:43 +00003728 // From here on out, all we care about are the unqualified forms
3729 // of the parameter and argument types.
3730 ParamType = ParamType.getUnqualifiedType();
3731 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003732
3733 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003734 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003735 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003736 } else if (CTAK == CTAK_Deduced) {
3737 // C++ [temp.deduct.type]p17:
3738 // If, in the declaration of a function template with a non-type
3739 // template-parameter, the non-type template- parameter is used
3740 // in an expression in the function parameter-list and, if the
3741 // corresponding template-argument is deduced, the
3742 // template-argument type shall match the type of the
3743 // template-parameter exactly, except that a template-argument
3744 // deduced from an array bound may be of any integral type.
3745 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3746 << ArgType << ParamType;
3747 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003748 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003749 } else if (ParamType->isBooleanType()) {
3750 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003751 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003752 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3753 !ParamType->isEnumeralType()) {
3754 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003755 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003756 } else {
3757 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003758 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003759 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003760 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003761 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003762 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003763 }
3764
Douglas Gregorc7469372011-05-04 21:55:00 +00003765 // Add the value of this argument to the list of converted
3766 // arguments. We use the bitwidth and signedness of the template
3767 // parameter.
3768 if (Arg->isValueDependent()) {
3769 // The argument is value-dependent. Create a new
3770 // TemplateArgument with the converted expression.
3771 Converted = TemplateArgument(Arg);
3772 return Owned(Arg);
3773 }
3774
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003775 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003776 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003777 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003778
Douglas Gregorc7469372011-05-04 21:55:00 +00003779 if (ParamType->isBooleanType()) {
3780 // Value must be zero or one.
3781 Value = Value != 0;
3782 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3783 if (Value.getBitWidth() != AllowedBits)
3784 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003785 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003786 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003787 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003788
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003789 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003790 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003791 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003792 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003793 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003794 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003795
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003796 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003797 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003798 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003799 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3800 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3801 << Arg->getSourceRange();
3802 Diag(Param->getLocation(), diag::note_template_param_here);
3803 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003804
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003805 // Complain if we overflowed the template parameter's type.
3806 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003807 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003808 RequiredBits = OldValue.getActiveBits();
3809 else if (OldValue.isUnsigned())
3810 RequiredBits = OldValue.getActiveBits() + 1;
3811 else
3812 RequiredBits = OldValue.getMinSignedBits();
3813 if (RequiredBits > AllowedBits) {
3814 Diag(Arg->getSourceRange().getBegin(),
3815 diag::warn_template_arg_too_large)
3816 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3817 << Arg->getSourceRange();
3818 Diag(Param->getLocation(), diag::note_template_param_here);
3819 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003820 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003821
John McCall833ca992009-10-29 08:12:44 +00003822 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003823 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003824 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003825 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003826 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003827
John McCall6bb80172010-03-30 21:47:33 +00003828 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3829
Douglas Gregorb7a09262010-04-01 18:32:35 +00003830 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3831 // from a template argument of type std::nullptr_t to a non-type
3832 // template parameter of type pointer to object, pointer to
3833 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003834 if (ArgType->isNullPtrType()) {
3835 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3836 Converted = TemplateArgument((NamedDecl *)0);
3837 return Owned(Arg);
3838 }
3839
3840 if (ParamType->isNullPtrType()) {
3841 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3842 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3843 return Owned(Arg);
3844 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003845 }
3846
Douglas Gregorb86b0572009-02-11 01:18:59 +00003847 // Handle pointer-to-function, reference-to-function, and
3848 // pointer-to-member-function all in (roughly) the same way.
3849 if (// -- For a non-type template-parameter of type pointer to
3850 // function, only the function-to-pointer conversion (4.3) is
3851 // applied. If the template-argument represents a set of
3852 // overloaded functions (or a pointer to such), the matching
3853 // function is selected from the set (13.4).
3854 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003855 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003856 // -- For a non-type template-parameter of type reference to
3857 // function, no conversions apply. If the template-argument
3858 // represents a set of overloaded functions, the matching
3859 // function is selected from the set (13.4).
3860 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003861 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003862 // -- For a non-type template-parameter of type pointer to
3863 // member function, no conversions apply. If the
3864 // template-argument represents a set of overloaded member
3865 // functions, the matching member function is selected from
3866 // the set (13.4).
3867 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003868 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003869 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003870
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003871 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003872 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003873 true,
3874 FoundResult)) {
3875 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003876 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003877
3878 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3879 ArgType = Arg->getType();
3880 } else
John Wiegley429bb272011-04-08 18:41:53 +00003881 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003883
John Wiegley429bb272011-04-08 18:41:53 +00003884 if (!ParamType->isMemberPointerType()) {
3885 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3886 ParamType,
3887 Arg, Converted))
3888 return ExprError();
3889 return Owned(Arg);
3890 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003891
John McCallf85e1932011-06-15 23:02:42 +00003892 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003893 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003894 false, ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003895 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003896 } else if (!Context.hasSameUnqualifiedType(ArgType,
3897 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003898 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003899 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003900 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003901 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003902 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003903 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003904 }
Mike Stump1eb44332009-09-09 15:08:12 +00003905
John Wiegley429bb272011-04-08 18:41:53 +00003906 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3907 return ExprError();
3908 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003909 }
3910
Chris Lattnerfe90de72009-02-20 21:37:53 +00003911 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003912 // -- for a non-type template-parameter of type pointer to
3913 // object, qualification conversions (4.4) and the
3914 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003915 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003916 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003917 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003918
John Wiegley429bb272011-04-08 18:41:53 +00003919 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3920 ParamType,
3921 Arg, Converted))
3922 return ExprError();
3923 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003924 }
Mike Stump1eb44332009-09-09 15:08:12 +00003925
Ted Kremenek6217b802009-07-29 21:53:49 +00003926 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003927 // -- For a non-type template-parameter of type reference to
3928 // object, no conversions apply. The type referred to by the
3929 // reference may be more cv-qualified than the (otherwise
3930 // identical) type of the template-argument. The
3931 // template-parameter is bound directly to the
3932 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003933 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003934 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003935
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003936 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3938 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003939 true,
3940 FoundResult)) {
3941 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003942 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003943
3944 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3945 ArgType = Arg->getType();
3946 } else
John Wiegley429bb272011-04-08 18:41:53 +00003947 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003948 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003949
John Wiegley429bb272011-04-08 18:41:53 +00003950 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3951 ParamType,
3952 Arg, Converted))
3953 return ExprError();
3954 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003955 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003956
3957 // -- For a non-type template-parameter of type pointer to data
3958 // member, qualification conversions (4.4) are applied.
3959 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3960
John McCallf85e1932011-06-15 23:02:42 +00003961 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003962 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003963 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003964 } else if (IsQualificationConversion(ArgType, ParamType, false,
3965 ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003966 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003967 } else {
3968 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003969 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003970 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003971 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003972 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003973 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003974 }
3975
John Wiegley429bb272011-04-08 18:41:53 +00003976 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3977 return ExprError();
3978 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003979}
3980
3981/// \brief Check a template argument against its corresponding
3982/// template template parameter.
3983///
3984/// This routine implements the semantics of C++ [temp.arg.template].
3985/// It returns true if an error occurred, and false otherwise.
3986bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003987 const TemplateArgumentLoc &Arg) {
3988 TemplateName Name = Arg.getArgument().getAsTemplate();
3989 TemplateDecl *Template = Name.getAsTemplateDecl();
3990 if (!Template) {
3991 // Any dependent template name is fine.
3992 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3993 return false;
3994 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003995
Richard Smith3e4c6c42011-05-05 21:57:07 +00003996 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003997 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003998 // the name of a class template or an alias template, expressed as an
3999 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004000 // primary class templates are considered when matching the
4001 // template template argument with the corresponding parameter;
4002 // partial specializations are not considered even if their
4003 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004004 //
4005 // Note that we also allow template template parameters here, which
4006 // will happen when we are dealing with, e.g., class template
4007 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004008 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004009 !isa<TemplateTemplateParmDecl>(Template) &&
4010 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004011 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004012 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004013 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004014 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004015 << Template;
4016 }
4017
4018 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4019 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004020 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004021 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004022 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004023}
4024
Douglas Gregor02024a92010-03-28 02:42:43 +00004025/// \brief Given a non-type template argument that refers to a
4026/// declaration and the type of its corresponding non-type template
4027/// parameter, produce an expression that properly refers to that
4028/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004029ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004030Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4031 QualType ParamType,
4032 SourceLocation Loc) {
4033 assert(Arg.getKind() == TemplateArgument::Declaration &&
4034 "Only declaration template arguments permitted here");
4035 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4036
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004037 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004038 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4039 // If the value is a class member, we might have a pointer-to-member.
4040 // Determine whether the non-type template template parameter is of
4041 // pointer-to-member type. If so, we need to build an appropriate
4042 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4043 // would refer to the member itself.
4044 if (ParamType->isMemberPointerType()) {
4045 QualType ClassType
4046 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4047 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004048 = NestedNameSpecifier::Create(Context, 0, false,
4049 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004050 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004051 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004052
4053 // The actual value-ness of this is unimportant, but for
4054 // internal consistency's sake, references to instance methods
4055 // are r-values.
4056 ExprValueKind VK = VK_LValue;
4057 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4058 VK = VK_RValue;
4059
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004060 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004061 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004062 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004063 Loc,
4064 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004065 if (RefExpr.isInvalid())
4066 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004067
John McCall2de56d12010-08-25 11:45:40 +00004068 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004069
Douglas Gregorc0c83002010-04-30 21:46:38 +00004070 // We might need to perform a trailing qualification conversion, since
4071 // the element type on the parameter could be more qualified than the
4072 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004073 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004074 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004075 ParamType.getUnqualifiedType(), false,
4076 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004077 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004078
Douglas Gregor02024a92010-03-28 02:42:43 +00004079 assert(!RefExpr.isInvalid() &&
4080 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004081 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004082 return move(RefExpr);
4083 }
4084 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004085
Douglas Gregor02024a92010-03-28 02:42:43 +00004086 QualType T = VD->getType().getNonReferenceType();
4087 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004088 // When the non-type template parameter is a pointer, take the
4089 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004090 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004091 if (RefExpr.isInvalid())
4092 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004093
4094 if (T->isFunctionType() || T->isArrayType()) {
4095 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004096 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4097 if (RefExpr.isInvalid())
4098 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004099
4100 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004102
Douglas Gregorb7a09262010-04-01 18:32:35 +00004103 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004104 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004105 }
4106
John McCallf89e55a2010-11-18 06:31:45 +00004107 ExprValueKind VK = VK_RValue;
4108
Douglas Gregor02024a92010-03-28 02:42:43 +00004109 // If the non-type template parameter has reference type, qualify the
4110 // resulting declaration reference with the extra qualifiers on the
4111 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004112 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4113 VK = VK_LValue;
4114 T = Context.getQualifiedType(T,
4115 TargetRef->getPointeeType().getQualifiers());
4116 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004117
John McCallf89e55a2010-11-18 06:31:45 +00004118 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004119}
4120
4121/// \brief Construct a new expression that refers to the given
4122/// integral template argument with the given source-location
4123/// information.
4124///
4125/// This routine takes care of the mapping from an integral template
4126/// argument (which may have any integral type) to the appropriate
4127/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004128ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004129Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4130 SourceLocation Loc) {
4131 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004132 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004133 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004134 if (T->isAnyCharacterType()) {
4135 CharacterLiteral::CharacterKind Kind;
4136 if (T->isWideCharType())
4137 Kind = CharacterLiteral::Wide;
4138 else if (T->isChar16Type())
4139 Kind = CharacterLiteral::UTF16;
4140 else if (T->isChar32Type())
4141 Kind = CharacterLiteral::UTF32;
4142 else
4143 Kind = CharacterLiteral::Ascii;
4144
Douglas Gregor02024a92010-03-28 02:42:43 +00004145 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004146 Arg.getAsIntegral()->getZExtValue(),
4147 Kind, T, Loc));
4148 }
4149
Douglas Gregor02024a92010-03-28 02:42:43 +00004150 if (T->isBooleanType())
4151 return Owned(new (Context) CXXBoolLiteralExpr(
4152 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004153 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004154
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004155 if (T->isNullPtrType())
4156 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4157
Chris Lattner223de242011-04-25 20:37:58 +00004158 // If this is an enum type that we're instantiating, we need to use an integer
4159 // type the same size as the enumerator. We don't want to build an
4160 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004161 QualType BT;
4162 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004163 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004164 else
4165 BT = T;
4166
John McCall4e9272d2011-07-15 07:47:58 +00004167 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4168 if (T->isEnumeralType()) {
4169 // FIXME: This is a hack. We need a better way to handle substituted
4170 // non-type template parameters.
4171 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4172 Context.getTrivialTypeSourceInfo(T, Loc),
4173 Loc, Loc);
4174 }
4175
4176 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004177}
4178
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004179/// \brief Match two template parameters within template parameter lists.
4180static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4181 bool Complain,
4182 Sema::TemplateParameterListEqualKind Kind,
4183 SourceLocation TemplateArgLoc) {
4184 // Check the actual kind (type, non-type, template).
4185 if (Old->getKind() != New->getKind()) {
4186 if (Complain) {
4187 unsigned NextDiag = diag::err_template_param_different_kind;
4188 if (TemplateArgLoc.isValid()) {
4189 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4190 NextDiag = diag::note_template_param_different_kind;
4191 }
4192 S.Diag(New->getLocation(), NextDiag)
4193 << (Kind != Sema::TPL_TemplateMatch);
4194 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4195 << (Kind != Sema::TPL_TemplateMatch);
4196 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004197
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004198 return false;
4199 }
4200
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004201 // Check that both are parameter packs are neither are parameter packs.
4202 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004203 // template template parameter, the template template parameter can have
4204 // a parameter pack where the template template argument does not.
4205 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4206 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4207 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004208 if (Complain) {
4209 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4210 if (TemplateArgLoc.isValid()) {
4211 S.Diag(TemplateArgLoc,
4212 diag::err_template_arg_template_params_mismatch);
4213 NextDiag = diag::note_template_parameter_pack_non_pack;
4214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004215
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004216 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4217 : isa<NonTypeTemplateParmDecl>(New)? 1
4218 : 2;
4219 S.Diag(New->getLocation(), NextDiag)
4220 << ParamKind << New->isParameterPack();
4221 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4222 << ParamKind << Old->isParameterPack();
4223 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004224
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004225 return false;
4226 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004228 // For non-type template parameters, check the type of the parameter.
4229 if (NonTypeTemplateParmDecl *OldNTTP
4230 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4231 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004232
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004233 // If we are matching a template template argument to a template
4234 // template parameter and one of the non-type template parameter types
4235 // is dependent, then we must wait until template instantiation time
4236 // to actually compare the arguments.
4237 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4238 (OldNTTP->getType()->isDependentType() ||
4239 NewNTTP->getType()->isDependentType()))
4240 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004242 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4243 if (Complain) {
4244 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4245 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004246 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004247 diag::err_template_arg_template_params_mismatch);
4248 NextDiag = diag::note_template_nontype_parm_different_type;
4249 }
4250 S.Diag(NewNTTP->getLocation(), NextDiag)
4251 << NewNTTP->getType()
4252 << (Kind != Sema::TPL_TemplateMatch);
4253 S.Diag(OldNTTP->getLocation(),
4254 diag::note_template_nontype_parm_prev_declaration)
4255 << OldNTTP->getType();
4256 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004258 return false;
4259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004261 return true;
4262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004264 // For template template parameters, check the template parameter types.
4265 // The template parameter lists of template template
4266 // parameters must agree.
4267 if (TemplateTemplateParmDecl *OldTTP
4268 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004270 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4271 OldTTP->getTemplateParameters(),
4272 Complain,
4273 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004274 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004275 : Kind),
4276 TemplateArgLoc);
4277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004279 return true;
4280}
Douglas Gregor02024a92010-03-28 02:42:43 +00004281
Douglas Gregora0347822011-01-13 00:08:50 +00004282/// \brief Diagnose a known arity mismatch when comparing template argument
4283/// lists.
4284static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004285void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004286 TemplateParameterList *New,
4287 TemplateParameterList *Old,
4288 Sema::TemplateParameterListEqualKind Kind,
4289 SourceLocation TemplateArgLoc) {
4290 unsigned NextDiag = diag::err_template_param_list_different_arity;
4291 if (TemplateArgLoc.isValid()) {
4292 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4293 NextDiag = diag::note_template_param_list_different_arity;
4294 }
4295 S.Diag(New->getTemplateLoc(), NextDiag)
4296 << (New->size() > Old->size())
4297 << (Kind != Sema::TPL_TemplateMatch)
4298 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4299 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4300 << (Kind != Sema::TPL_TemplateMatch)
4301 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4302}
4303
Douglas Gregorddc29e12009-02-06 22:42:48 +00004304/// \brief Determine whether the given template parameter lists are
4305/// equivalent.
4306///
Mike Stump1eb44332009-09-09 15:08:12 +00004307/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004308/// source code as part of a new template declaration.
4309///
4310/// \param Old The old template parameter list, typically found via
4311/// name lookup of the template declared with this template parameter
4312/// list.
4313///
4314/// \param Complain If true, this routine will produce a diagnostic if
4315/// the template parameter lists are not equivalent.
4316///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004317/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004318///
4319/// \param TemplateArgLoc If this source location is valid, then we
4320/// are actually checking the template parameter list of a template
4321/// argument (New) against the template parameter list of its
4322/// corresponding template template parameter (Old). We produce
4323/// slightly different diagnostics in this scenario.
4324///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004325/// \returns True if the template parameter lists are equal, false
4326/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004327bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004328Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4329 TemplateParameterList *Old,
4330 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004331 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004332 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004333 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4334 if (Complain)
4335 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4336 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004337
4338 return false;
4339 }
4340
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004341 // C++0x [temp.arg.template]p3:
4342 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004343 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004344 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004345 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004346 // template-parameter-list of P. [...]
4347 TemplateParameterList::iterator NewParm = New->begin();
4348 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004349 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004350 OldParmEnd = Old->end();
4351 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004352 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4353 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004354 if (NewParm == NewParmEnd) {
4355 if (Complain)
4356 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4357 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004358
Douglas Gregora0347822011-01-13 00:08:50 +00004359 return false;
4360 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004361
Douglas Gregora0347822011-01-13 00:08:50 +00004362 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4363 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004364 return false;
4365
Douglas Gregora0347822011-01-13 00:08:50 +00004366 ++NewParm;
4367 continue;
4368 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004369
Douglas Gregora0347822011-01-13 00:08:50 +00004370 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004371 // [...] When P's template- parameter-list contains a template parameter
4372 // pack (14.5.3), the template parameter pack will match zero or more
4373 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004374 // template-parameter-list of A with the same type and form as the
4375 // template parameter pack in P (ignoring whether those template
4376 // parameters are template parameter packs).
4377 for (; NewParm != NewParmEnd; ++NewParm) {
4378 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4379 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004380 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004381 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004382 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004383
Douglas Gregora0347822011-01-13 00:08:50 +00004384 // Make sure we exhausted all of the arguments.
4385 if (NewParm != NewParmEnd) {
4386 if (Complain)
4387 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4388 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004389
Douglas Gregora0347822011-01-13 00:08:50 +00004390 return false;
4391 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004392
Douglas Gregorddc29e12009-02-06 22:42:48 +00004393 return true;
4394}
4395
4396/// \brief Check whether a template can be declared within this scope.
4397///
4398/// If the template declaration is valid in this scope, returns
4399/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004400bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004401Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004402 // Find the nearest enclosing declaration scope.
4403 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4404 (S->getFlags() & Scope::TemplateParamScope) != 0)
4405 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004406
Douglas Gregorddc29e12009-02-06 22:42:48 +00004407 // C++ [temp]p2:
4408 // A template-declaration can appear only as a namespace scope or
4409 // class scope declaration.
4410 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004411 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4412 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004413 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004414 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004415
Eli Friedman1503f772009-07-31 01:43:05 +00004416 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004417 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004418
4419 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4420 return false;
4421
Mike Stump1eb44332009-09-09 15:08:12 +00004422 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004423 diag::err_template_outside_namespace_or_class_scope)
4424 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004425}
Douglas Gregorcc636682009-02-17 23:15:12 +00004426
Douglas Gregord5cb8762009-10-07 00:13:32 +00004427/// \brief Determine what kind of template specialization the given declaration
4428/// is.
4429static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4430 if (!D)
4431 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
Douglas Gregorf6b11852009-10-08 15:14:33 +00004433 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4434 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004435 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4436 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004437 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4438 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004439
Douglas Gregord5cb8762009-10-07 00:13:32 +00004440 return TSK_Undeclared;
4441}
4442
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004443/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004444/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004445///
Douglas Gregor9302da62009-10-14 23:50:59 +00004446/// This routine determines whether a template specialization can be declared
4447/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004448///
4449/// \param S the semantic analysis object for which this check is being
4450/// performed.
4451///
4452/// \param Specialized the entity being specialized or instantiated, which
4453/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004454/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004455/// member class).
4456///
4457/// \param PrevDecl the previous declaration of this entity, if any.
4458///
4459/// \param Loc the location of the explicit specialization or instantiation of
4460/// this entity.
4461///
4462/// \param IsPartialSpecialization whether this is a partial specialization of
4463/// a class template.
4464///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004465/// \returns true if there was an error that we cannot recover from, false
4466/// otherwise.
4467static bool CheckTemplateSpecializationScope(Sema &S,
4468 NamedDecl *Specialized,
4469 NamedDecl *PrevDecl,
4470 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004471 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004472 // Keep these "kind" numbers in sync with the %select statements in the
4473 // various diagnostics emitted by this routine.
4474 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004475 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004476 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004477 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004478 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004479 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004480 EntityKind = 3;
4481 else if (isa<VarDecl>(Specialized))
4482 EntityKind = 4;
4483 else if (isa<RecordDecl>(Specialized))
4484 EntityKind = 5;
4485 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004486 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4487 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004488 return true;
4489 }
4490
Douglas Gregor88b70942009-02-25 22:02:03 +00004491 // C++ [temp.expl.spec]p2:
4492 // An explicit specialization shall be declared in the namespace
4493 // of which the template is a member, or, for member templates, in
4494 // the namespace of which the enclosing class or enclosing class
4495 // template is a member. An explicit specialization of a member
4496 // function, member class or static data member of a class
4497 // template shall be declared in the namespace of which the class
4498 // template is a member. Such a declaration may also be a
4499 // definition. If the declaration is not a definition, the
4500 // specialization may be defined later in the name- space in which
4501 // the explicit specialization was declared, or in a namespace
4502 // that encloses the one in which the explicit specialization was
4503 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004504 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004505 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004506 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004507 return true;
4508 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004509
Douglas Gregor0a407472009-10-07 17:30:37 +00004510 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichete1e96a62011-05-14 19:17:07 +00004511 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4512 << Specialized;
4513 return true;
Douglas Gregor0a407472009-10-07 17:30:37 +00004514 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004516 // C++ [temp.class.spec]p6:
4517 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004518 // in any namespace scope in which its definition may be defined (14.5.1
4519 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004520 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004521 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004522 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004523 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004524 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004525 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4526 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004527 // C++ [temp.exp.spec]p2:
4528 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004529 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004530 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004531 // An explicit specialization of a member function, member class or
4532 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004533 // namespace of which the class template is a member.
4534 //
4535 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004537 // the specialized template.
4538 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4539 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004540 bool IsCPlusPlus0xExtension
4541 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004542 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004543 S.Diag(Loc, IsCPlusPlus0xExtension
4544 ? diag::ext_template_spec_decl_out_of_scope_global
4545 : diag::err_template_spec_decl_out_of_scope_global)
4546 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004547 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004548 S.Diag(Loc, IsCPlusPlus0xExtension
4549 ? diag::ext_template_spec_decl_out_of_scope
4550 : diag::err_template_spec_decl_out_of_scope)
4551 << EntityKind << Specialized
4552 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004553
Douglas Gregor9302da62009-10-14 23:50:59 +00004554 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4555 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004556 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004557 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004558
4559 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004560 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004561 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004562 // specializations of function templates, static data members, and member
4563 // functions, so we skip the check here for those kinds of entities.
4564 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004565 // Should we refactor that check, so that it occurs later?
4566 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004567 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4568 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004569 if (isa<TranslationUnitDecl>(SpecializedContext))
4570 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4571 << EntityKind << Specialized;
4572 else if (isa<NamespaceDecl>(SpecializedContext))
4573 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4574 << EntityKind << Specialized
4575 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004576
Douglas Gregor9302da62009-10-14 23:50:59 +00004577 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004579
Douglas Gregord5cb8762009-10-07 00:13:32 +00004580 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004581
Douglas Gregor88b70942009-02-25 22:02:03 +00004582 return false;
4583}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004584
Douglas Gregorbacb9492011-01-03 21:13:47 +00004585/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4586/// that checks non-type template partial specialization arguments.
4587static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4588 NonTypeTemplateParmDecl *Param,
4589 const TemplateArgument *Args,
4590 unsigned NumArgs) {
4591 for (unsigned I = 0; I != NumArgs; ++I) {
4592 if (Args[I].getKind() == TemplateArgument::Pack) {
4593 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004595 Args[I].pack_size()))
4596 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004597
Douglas Gregore94866f2009-06-12 21:21:02 +00004598 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004599 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004600
Douglas Gregorbacb9492011-01-03 21:13:47 +00004601 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004602 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004603 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004604 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004605
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004606 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004607 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4608 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004609
4610 // Strip off any implicit casts we added as part of type checking.
4611 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4612 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004613
Douglas Gregore94866f2009-06-12 21:21:02 +00004614 // C++ [temp.class.spec]p8:
4615 // A non-type argument is non-specialized if it is the name of a
4616 // non-type parameter. All other non-type arguments are
4617 // specialized.
4618 //
4619 // Below, we check the two conditions that only apply to
4620 // specialized non-type arguments, so skip any non-specialized
4621 // arguments.
4622 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004623 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004624 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004625
Douglas Gregore94866f2009-06-12 21:21:02 +00004626 // C++ [temp.class.spec]p9:
4627 // Within the argument list of a class template partial
4628 // specialization, the following restrictions apply:
4629 // -- A partially specialized non-type argument expression
4630 // shall not involve a template parameter of the partial
4631 // specialization except when the argument expression is a
4632 // simple identifier.
4633 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004634 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004635 diag::err_dependent_non_type_arg_in_partial_spec)
4636 << ArgExpr->getSourceRange();
4637 return true;
4638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004639
Douglas Gregore94866f2009-06-12 21:21:02 +00004640 // -- The type of a template parameter corresponding to a
4641 // specialized non-type argument shall not be dependent on a
4642 // parameter of the specialization.
4643 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004644 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004645 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4646 << Param->getType()
4647 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004648 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004649 return true;
4650 }
4651 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652
Douglas Gregorbacb9492011-01-03 21:13:47 +00004653 return false;
4654}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655
Douglas Gregorbacb9492011-01-03 21:13:47 +00004656/// \brief Check the non-type template arguments of a class template
4657/// partial specialization according to C++ [temp.class.spec]p9.
4658///
4659/// \param TemplateParams the template parameters of the primary class
4660/// template.
4661///
4662/// \param TemplateArg the template arguments of the class template
4663/// partial specialization.
4664///
4665/// \returns true if there was an error, false otherwise.
4666static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4667 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004668 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004669 const TemplateArgument *ArgList = TemplateArgs.data();
4670
4671 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4672 NonTypeTemplateParmDecl *Param
4673 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4674 if (!Param)
4675 continue;
4676
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004677 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004678 &ArgList[I], 1))
4679 return true;
4680 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004681
4682 return false;
4683}
4684
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004685/// \brief Retrieve the previous declaration of the given declaration.
4686static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4687 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4688 return VD->getPreviousDeclaration();
4689 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4690 return FD->getPreviousDeclaration();
4691 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4692 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004693 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004694 return TD->getPreviousDeclaration();
4695 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4696 return FTD->getPreviousDeclaration();
4697 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4698 return CTD->getPreviousDeclaration();
4699 return 0;
4700}
4701
John McCalld226f652010-08-21 09:40:31 +00004702DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004703Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4704 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004705 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004706 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004707 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004708 SourceLocation TemplateNameLoc,
4709 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004710 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004711 SourceLocation RAngleLoc,
4712 AttributeList *Attr,
4713 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004714 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004715
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004716 // NOTE: KWLoc is the location of the tag keyword. This will instead
4717 // store the location of the outermost template keyword in the declaration.
4718 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4719 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4720
Douglas Gregorcc636682009-02-17 23:15:12 +00004721 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004722 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004723 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004724 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4725
4726 if (!ClassTemplate) {
4727 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004729 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4730 return true;
4731 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004732
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004733 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004734 bool isPartialSpecialization = false;
4735
Douglas Gregor88b70942009-02-25 22:02:03 +00004736 // Check the validity of the template headers that introduce this
4737 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004738 // FIXME: We probably shouldn't complain about these headers for
4739 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004740 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004741 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004742 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4743 TemplateNameLoc,
4744 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004745 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004746 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004747 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004748 isExplicitSpecialization,
4749 Invalid);
4750 if (Invalid)
4751 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004752
Douglas Gregor05396e22009-08-25 17:23:04 +00004753 if (TemplateParams && TemplateParams->size() > 0) {
4754 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004755
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004756 if (TUK == TUK_Friend) {
4757 Diag(KWLoc, diag::err_partial_specialization_friend)
4758 << SourceRange(LAngleLoc, RAngleLoc);
4759 return true;
4760 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004761
Douglas Gregor05396e22009-08-25 17:23:04 +00004762 // C++ [temp.class.spec]p10:
4763 // The template parameter list of a specialization shall not
4764 // contain default template argument values.
4765 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4766 Decl *Param = TemplateParams->getParam(I);
4767 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4768 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004769 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004770 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004771 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004772 }
4773 } else if (NonTypeTemplateParmDecl *NTTP
4774 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4775 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004776 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004777 diag::err_default_arg_in_partial_spec)
4778 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004779 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004780 }
4781 } else {
4782 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004783 if (TTP->hasDefaultArgument()) {
4784 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004785 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004786 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004787 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004788 }
4789 }
4790 }
Douglas Gregora735b202009-10-13 14:39:41 +00004791 } else if (TemplateParams) {
4792 if (TUK == TUK_Friend)
4793 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004794 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004795 SourceRange(TemplateParams->getTemplateLoc(),
4796 TemplateParams->getRAngleLoc()))
4797 << SourceRange(LAngleLoc, RAngleLoc);
4798 else
4799 isExplicitSpecialization = true;
4800 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004801 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004802 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004803 isExplicitSpecialization = true;
4804 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004805
Douglas Gregorcc636682009-02-17 23:15:12 +00004806 // Check that the specialization uses the same tag kind as the
4807 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004808 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4809 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004810 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004811 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004812 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004813 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004814 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004815 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004816 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004817 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004818 diag::note_previous_use);
4819 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4820 }
4821
Douglas Gregor40808ce2009-03-09 23:48:35 +00004822 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004823 TemplateArgumentListInfo TemplateArgs;
4824 TemplateArgs.setLAngleLoc(LAngleLoc);
4825 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004826 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004827
Douglas Gregor925910d2011-01-03 20:35:03 +00004828 // Check for unexpanded parameter packs in any of the template arguments.
4829 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004830 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004831 UPPC_PartialSpecialization))
4832 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833
Douglas Gregorcc636682009-02-17 23:15:12 +00004834 // Check that the template argument list is well-formed for this
4835 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004836 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004837 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4838 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004839 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004840
Douglas Gregor910f8002010-11-07 23:05:16 +00004841 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004842 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004843
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004844 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004845 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004846 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004847 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004848 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004849 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004850 return true;
4851
Douglas Gregor561f8122011-07-01 01:22:09 +00004852 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004853 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004854 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004855 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004856 TemplateArgs.size(),
4857 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004858 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4859 << ClassTemplate->getDeclName();
4860 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004861 }
4862 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004863
Douglas Gregorcc636682009-02-17 23:15:12 +00004864 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004865 ClassTemplateSpecializationDecl *PrevDecl = 0;
4866
4867 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004868 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004869 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004870 = ClassTemplate->findPartialSpecialization(Converted.data(),
4871 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004872 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004873 else
4874 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004875 = ClassTemplate->findSpecialization(Converted.data(),
4876 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004877
4878 ClassTemplateSpecializationDecl *Specialization = 0;
4879
Douglas Gregor88b70942009-02-25 22:02:03 +00004880 // Check whether we can declare a class template specialization in
4881 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004882 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004883 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4884 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004885 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004886 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004887
Douglas Gregorb88e8882009-07-30 17:40:51 +00004888 // The canonical type
4889 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004890 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004891 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004892 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004893 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004894 // arguments was referenced but not declared, or we're only
4895 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004896 // declaration node as our own, updating its source location and
4897 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004898 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004899 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004900 if (TemplateParameterLists.size() > 0) {
4901 Specialization->setTemplateParameterListsInfo(Context,
4902 TemplateParameterLists.size(),
4903 (TemplateParameterList**) TemplateParameterLists.release());
4904 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004905 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004906 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004907 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004908 // Build the canonical type that describes the converted template
4909 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004910 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4911 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004912 Converted.data(),
4913 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004914
4915 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004916 ClassTemplate->getInjectedClassNameSpecialization())) {
4917 // C++ [temp.class.spec]p9b3:
4918 //
4919 // -- The argument list of the specialization shall not be identical
4920 // to the implicit argument list of the primary template.
4921 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4922 << (TUK == TUK_Definition)
4923 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4924 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4925 ClassTemplate->getIdentifier(),
4926 TemplateNameLoc,
4927 Attr,
4928 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004929 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004930 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004931 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004932 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004933
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004934 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004935 ClassTemplatePartialSpecializationDecl *PrevPartial
4936 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004937 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004938 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004939 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004940 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004941 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004942 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004943 TemplateParams,
4944 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004945 Converted.data(),
4946 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004947 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004948 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004949 PrevPartial,
4950 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004951 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004952 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004953 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004954 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004955 (TemplateParameterList**) TemplateParameterLists.release());
4956 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004957
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004958 if (!PrevPartial)
4959 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004960 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004961
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004962 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004963 // template specialization, make a note of that.
4964 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4965 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregor031a5882009-06-13 00:26:55 +00004967 // Check that all of the template parameters of the class template
4968 // partial specialization are deducible from the template
4969 // arguments. If not, this class template partial specialization
4970 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004971 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00004972 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004973 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004974 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004975 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004976 unsigned NumNonDeducible = 0;
4977 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4978 if (!DeducibleParams[I])
4979 ++NumNonDeducible;
4980
4981 if (NumNonDeducible) {
4982 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4983 << (NumNonDeducible > 1)
4984 << SourceRange(TemplateNameLoc, RAngleLoc);
4985 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4986 if (!DeducibleParams[I]) {
4987 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4988 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004989 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004990 diag::note_partial_spec_unused_parameter)
4991 << Param->getDeclName();
4992 else
Mike Stump1eb44332009-09-09 15:08:12 +00004993 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004994 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004995 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004996 }
4997 }
4998 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004999 } else {
5000 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005001 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005002 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005003 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005004 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005005 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005006 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005007 Converted.data(),
5008 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005009 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005010 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005011 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005012 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005013 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005014 (TemplateParameterList**) TemplateParameterLists.release());
5015 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005016
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005017 if (!PrevDecl)
5018 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005019
5020 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005021 }
5022
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005023 // C++ [temp.expl.spec]p6:
5024 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005025 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005026 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005028 // use occurs; no diagnostic is required.
5029 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005030 bool Okay = false;
5031 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5032 // Is there any previous explicit specialization declaration?
5033 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5034 Okay = true;
5035 break;
5036 }
5037 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005038
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005039 if (!Okay) {
5040 SourceRange Range(TemplateNameLoc, RAngleLoc);
5041 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5042 << Context.getTypeDeclType(Specialization) << Range;
5043
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005044 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005045 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005046 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005047 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005048 return true;
5049 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005050 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005051
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005052 // If this is not a friend, note that this is an explicit specialization.
5053 if (TUK != TUK_Friend)
5054 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005055
5056 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005057 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005058 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005059 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005060 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005061 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005062 Diag(Def->getLocation(), diag::note_previous_definition);
5063 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005064 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005065 }
5066 }
5067
John McCall7f1b9872010-12-18 03:30:47 +00005068 if (Attr)
5069 ProcessDeclAttributeList(S, Specialization, Attr);
5070
Douglas Gregorfc705b82009-02-26 22:19:44 +00005071 // Build the fully-sugared type for this class template
5072 // specialization as the user wrote in the specialization
5073 // itself. This means that we'll pretty-print the type retrieved
5074 // from the specialization's declaration the way that the user
5075 // actually wrote the specialization, rather than formatting the
5076 // name based on the "canonical" representation used to store the
5077 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005078 TypeSourceInfo *WrittenTy
5079 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5080 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005081 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005082 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005083 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005084 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005085 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005086
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005087 // C++ [temp.expl.spec]p9:
5088 // A template explicit specialization is in the scope of the
5089 // namespace in which the template was defined.
5090 //
5091 // We actually implement this paragraph where we set the semantic
5092 // context (in the creation of the ClassTemplateSpecializationDecl),
5093 // but we also maintain the lexical context where the actual
5094 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005095 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005096
Douglas Gregorcc636682009-02-17 23:15:12 +00005097 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005098 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005099 Specialization->startDefinition();
5100
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005101 if (TUK == TUK_Friend) {
5102 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5103 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005104 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005105 /*FIXME:*/KWLoc);
5106 Friend->setAccess(AS_public);
5107 CurContext->addDecl(Friend);
5108 } else {
5109 // Add the specialization into its lexical context, so that it can
5110 // be seen when iterating through the list of declarations in that
5111 // context. However, specializations are not found by name lookup.
5112 CurContext->addDecl(Specialization);
5113 }
John McCalld226f652010-08-21 09:40:31 +00005114 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005115}
Douglas Gregord57959a2009-03-27 23:10:48 +00005116
John McCalld226f652010-08-21 09:40:31 +00005117Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005118 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005119 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005120 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5121}
5122
John McCalld226f652010-08-21 09:40:31 +00005123Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005124 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005125 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005126 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005127 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005128
Douglas Gregor52591bf2009-06-24 00:54:41 +00005129 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005130 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005131 }
Mike Stump1eb44332009-09-09 15:08:12 +00005132
Douglas Gregor52591bf2009-06-24 00:54:41 +00005133 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005134
John McCalld226f652010-08-21 09:40:31 +00005135 Decl *DP = HandleDeclarator(ParentScope, D,
5136 move(TemplateParameterLists),
5137 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005138 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005139 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005140 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005141 FunctionTemplate->getTemplatedDecl());
5142 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5143 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5144 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005145}
5146
John McCall75042392010-02-11 01:33:53 +00005147/// \brief Strips various properties off an implicit instantiation
5148/// that has just been explicitly specialized.
5149static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005150 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005151
5152 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5153 FD->setInlineSpecified(false);
5154 }
5155}
5156
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005157/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005158/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005160/// new specialization/instantiation will have any effect.
5161///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005162/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005163/// instantiation.
5164///
5165/// \param NewTSK the kind of the new explicit specialization or instantiation.
5166///
5167/// \param PrevDecl the previous declaration of the entity.
5168///
5169/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5170///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005171/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005172/// declaration was instantiated (either implicitly or explicitly).
5173///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005175/// specialization or instantiation has no effect and should be ignored.
5176///
5177/// \returns true if there was an error that should prevent the introduction of
5178/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005179bool
5180Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5181 TemplateSpecializationKind NewTSK,
5182 NamedDecl *PrevDecl,
5183 TemplateSpecializationKind PrevTSK,
5184 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005185 bool &HasNoEffect) {
5186 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005187
Douglas Gregor454885e2009-10-15 15:54:05 +00005188 switch (NewTSK) {
5189 case TSK_Undeclared:
5190 case TSK_ImplicitInstantiation:
5191 assert(false && "Don't check implicit instantiations here");
5192 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005193
Douglas Gregor454885e2009-10-15 15:54:05 +00005194 case TSK_ExplicitSpecialization:
5195 switch (PrevTSK) {
5196 case TSK_Undeclared:
5197 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005198 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005199 // explicitly specialized or has merely been mentioned without any
5200 // instantiation.
5201 return false;
5202
5203 case TSK_ImplicitInstantiation:
5204 if (PrevPointOfInstantiation.isInvalid()) {
5205 // The declaration itself has not actually been instantiated, so it is
5206 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005207 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005208 return false;
5209 }
5210 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005211
Douglas Gregor454885e2009-10-15 15:54:05 +00005212 case TSK_ExplicitInstantiationDeclaration:
5213 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005214 assert((PrevTSK == TSK_ImplicitInstantiation ||
5215 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005216 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005217
Douglas Gregor454885e2009-10-15 15:54:05 +00005218 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005219 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005220 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005221 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005222 // implicit instantiation to take place, in every translation unit in
5223 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005224 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5225 // Is there any previous explicit specialization declaration?
5226 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5227 return false;
5228 }
5229
Douglas Gregor0d035142009-10-27 18:42:08 +00005230 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005231 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005232 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005233 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234
Douglas Gregor454885e2009-10-15 15:54:05 +00005235 return true;
5236 }
5237 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238
Douglas Gregor454885e2009-10-15 15:54:05 +00005239 case TSK_ExplicitInstantiationDeclaration:
5240 switch (PrevTSK) {
5241 case TSK_ExplicitInstantiationDeclaration:
5242 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005243 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005244 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005245
Douglas Gregor454885e2009-10-15 15:54:05 +00005246 case TSK_Undeclared:
5247 case TSK_ImplicitInstantiation:
5248 // We're explicitly instantiating something that may have already been
5249 // implicitly instantiated; that's fine.
5250 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251
Douglas Gregor454885e2009-10-15 15:54:05 +00005252 case TSK_ExplicitSpecialization:
5253 // C++0x [temp.explicit]p4:
5254 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005255 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005256 // specialization for that template, the explicit instantiation has no
5257 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005258 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005259 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260
Douglas Gregor454885e2009-10-15 15:54:05 +00005261 case TSK_ExplicitInstantiationDefinition:
5262 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005263 // If an entity is the subject of both an explicit instantiation
5264 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005265 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005267 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005269 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005270 assert(PrevPointOfInstantiation.isValid() &&
5271 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005272 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005273 return false;
5274 }
5275 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005276
Douglas Gregor454885e2009-10-15 15:54:05 +00005277 case TSK_ExplicitInstantiationDefinition:
5278 switch (PrevTSK) {
5279 case TSK_Undeclared:
5280 case TSK_ImplicitInstantiation:
5281 // We're explicitly instantiating something that may have already been
5282 // implicitly instantiated; that's fine.
5283 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 case TSK_ExplicitSpecialization:
5286 // C++ DR 259, C++0x [temp.explicit]p4:
5287 // For a given set of template parameters, if an explicit
5288 // instantiation of a template appears after a declaration of
5289 // an explicit specialization for that template, the explicit
5290 // instantiation has no effect.
5291 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005293 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005294 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005295 if (!getLangOptions().CPlusPlus0x) {
5296 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005297 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005298 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005299 diag::note_previous_template_specialization);
5300 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005301 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005303
Douglas Gregor454885e2009-10-15 15:54:05 +00005304 case TSK_ExplicitInstantiationDeclaration:
5305 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005307 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308
Douglas Gregor454885e2009-10-15 15:54:05 +00005309 case TSK_ExplicitInstantiationDefinition:
5310 // C++0x [temp.spec]p5:
5311 // For a given template and a given set of template-arguments,
5312 // - an explicit instantiation definition shall appear at most once
5313 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005314 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005315 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005316 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005317 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005318 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005319 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005320 }
5321 break;
5322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323
Douglas Gregor454885e2009-10-15 15:54:05 +00005324 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005325
Douglas Gregor454885e2009-10-15 15:54:05 +00005326 return false;
5327}
5328
John McCallaf2094e2010-04-08 09:05:18 +00005329/// \brief Perform semantic analysis for the given dependent function
5330/// template specialization. The only possible way to get a dependent
5331/// function template specialization is with a friend declaration,
5332/// like so:
5333///
5334/// template <class T> void foo(T);
5335/// template <class T> class A {
5336/// friend void foo<>(T);
5337/// };
5338///
5339/// There really isn't any useful analysis we can do here, so we
5340/// just store the information.
5341bool
5342Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5343 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5344 LookupResult &Previous) {
5345 // Remove anything from Previous that isn't a function template in
5346 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005347 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005348 LookupResult::Filter F = Previous.makeFilter();
5349 while (F.hasNext()) {
5350 NamedDecl *D = F.next()->getUnderlyingDecl();
5351 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005352 !FDLookupContext->InEnclosingNamespaceSetOf(
5353 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005354 F.erase();
5355 }
5356 F.done();
5357
5358 // Should this be diagnosed here?
5359 if (Previous.empty()) return true;
5360
5361 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5362 ExplicitTemplateArgs);
5363 return false;
5364}
5365
Abramo Bagnarae03db982010-05-20 15:32:11 +00005366/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005367/// specialization.
5368///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005369/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005370/// explicit function template specialization. On successful completion,
5371/// the function declaration \p FD will become a function template
5372/// specialization.
5373///
5374/// \param FD the function declaration, which will be updated to become a
5375/// function template specialization.
5376///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005377/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5378/// if any. Note that this may be valid info even when 0 arguments are
5379/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5380/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005381///
Francois Pichet59e7c562011-07-08 06:21:47 +00005382/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005383/// this function specialization.
5384bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005385Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005386 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005387 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005388 // The set of function template specializations that could match this
5389 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005390 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005391
Sebastian Redl7a126a42010-08-31 00:36:30 +00005392 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005393 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5394 I != E; ++I) {
5395 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5396 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005397 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005398 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005399 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5400 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005401 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005403 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404 // A trailing template-argument can be left unspecified in the
5405 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005406 // provided it can be deduced from the function argument type.
5407 // Perform template argument deduction to determine whether we may be
5408 // specializing this template.
5409 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005410 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005411 FunctionDecl *Specialization = 0;
5412 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005413 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005414 FD->getType(),
5415 Specialization,
5416 Info)) {
5417 // FIXME: Template argument deduction failed; record why it failed, so
5418 // that we can provide nifty diagnostics.
5419 (void)TDK;
5420 continue;
5421 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005422
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005423 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005424 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005425 }
5426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005427
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005428 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005429 UnresolvedSetIterator Result
5430 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005431 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005432 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005433 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005434 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005435 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005436 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005437 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005438 return true;
John McCallc373d482010-01-27 01:50:18 +00005439
5440 // Ignore access information; it doesn't figure into redeclaration checking.
5441 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005442
5443 FunctionTemplateSpecializationInfo *SpecInfo
5444 = Specialization->getTemplateSpecializationInfo();
5445 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005446
5447 // Note: do not overwrite location info if previous template
5448 // specialization kind was explicit.
5449 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5450 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5451 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005452
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005453 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005454 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005455
5456 // If this is a friend declaration, then we're not really declaring
5457 // an explicit specialization.
5458 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005459
Douglas Gregord5cb8762009-10-07 00:13:32 +00005460 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005461 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005462 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005463 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005464 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005465 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005466 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005467
5468 // C++ [temp.expl.spec]p6:
5469 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005470 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005471 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005472 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005473 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005474 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005475 if (!isFriend &&
5476 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005477 TSK_ExplicitSpecialization,
5478 Specialization,
5479 SpecInfo->getTemplateSpecializationKind(),
5480 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005481 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005482 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005483
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005484 // Mark the prior declaration as an explicit specialization, so that later
5485 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005486 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005487 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005488 MarkUnusedFileScopedDecl(Specialization);
5489 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005490
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005491 // Turn the given function declaration into a function template
5492 // specialization, with the template arguments from the previous
5493 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005494 // Take copies of (semantic and syntactic) template argument lists.
5495 const TemplateArgumentList* TemplArgs = new (Context)
5496 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5497 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5498 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005499 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005500 TemplArgs, /*InsertPos=*/0,
5501 SpecInfo->getTemplateSpecializationKind(),
5502 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005503 FD->setStorageClass(Specialization->getStorageClass());
5504
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005505 // The "previous declaration" for this function template specialization is
5506 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005507 Previous.clear();
5508 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005509 return false;
5510}
5511
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005512/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005513/// specialization.
5514///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005515/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005516/// explicit member function specialization. On successful completion,
5517/// the function declaration \p FD will become a member function
5518/// specialization.
5519///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005520/// \param Member the member declaration, which will be updated to become a
5521/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005522///
John McCall68263142009-11-18 22:49:29 +00005523/// \param Previous the set of declarations, one of which may be specialized
5524/// by this function specialization; the set will be modified to contain the
5525/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005526bool
John McCall68263142009-11-18 22:49:29 +00005527Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005528 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005529
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005530 // Try to find the member we are instantiating.
5531 NamedDecl *Instantiation = 0;
5532 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005533 MemberSpecializationInfo *MSInfo = 0;
5534
John McCall68263142009-11-18 22:49:29 +00005535 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005536 // Nowhere to look anyway.
5537 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005538 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5539 I != E; ++I) {
5540 NamedDecl *D = (*I)->getUnderlyingDecl();
5541 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005542 if (Context.hasSameType(Function->getType(), Method->getType())) {
5543 Instantiation = Method;
5544 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005545 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005546 break;
5547 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005548 }
5549 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005550 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005551 VarDecl *PrevVar;
5552 if (Previous.isSingleResult() &&
5553 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005554 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005555 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005556 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005557 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005558 }
5559 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005560 CXXRecordDecl *PrevRecord;
5561 if (Previous.isSingleResult() &&
5562 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5563 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005564 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005565 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005566 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005568
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005569 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005570 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005571 // specializations are always out-of-line, the caller will complain about
5572 // this mismatch later.
5573 return false;
5574 }
John McCall77e8b112010-04-13 20:37:33 +00005575
5576 // If this is a friend, just bail out here before we start turning
5577 // things into explicit specializations.
5578 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5579 // Preserve instantiation information.
5580 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5581 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5582 cast<CXXMethodDecl>(InstantiatedFrom),
5583 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5584 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5585 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5586 cast<CXXRecordDecl>(InstantiatedFrom),
5587 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5588 }
5589
5590 Previous.clear();
5591 Previous.addDecl(Instantiation);
5592 return false;
5593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005594
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005595 // Make sure that this is a specialization of a member.
5596 if (!InstantiatedFrom) {
5597 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5598 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005599 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5600 return true;
5601 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005602
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005603 // C++ [temp.expl.spec]p6:
5604 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005605 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005606 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005607 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005608 // use occurs; no diagnostic is required.
5609 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005610
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005611 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005612 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5613 TSK_ExplicitSpecialization,
5614 Instantiation,
5615 MSInfo->getTemplateSpecializationKind(),
5616 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005617 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005618 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005619
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005620 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005621 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005622 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005623 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005624 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005625 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005626
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005627 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005628 // the original declaration to note that it is an explicit specialization
5629 // (if it was previously an implicit instantiation). This latter step
5630 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005631 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005632 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5633 if (InstantiationFunction->getTemplateSpecializationKind() ==
5634 TSK_ImplicitInstantiation) {
5635 InstantiationFunction->setTemplateSpecializationKind(
5636 TSK_ExplicitSpecialization);
5637 InstantiationFunction->setLocation(Member->getLocation());
5638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005639
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005640 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5641 cast<CXXMethodDecl>(InstantiatedFrom),
5642 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005643 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005644 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005645 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5646 if (InstantiationVar->getTemplateSpecializationKind() ==
5647 TSK_ImplicitInstantiation) {
5648 InstantiationVar->setTemplateSpecializationKind(
5649 TSK_ExplicitSpecialization);
5650 InstantiationVar->setLocation(Member->getLocation());
5651 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005652
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005653 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5654 cast<VarDecl>(InstantiatedFrom),
5655 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005656 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005657 } else {
5658 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005659 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5660 if (InstantiationClass->getTemplateSpecializationKind() ==
5661 TSK_ImplicitInstantiation) {
5662 InstantiationClass->setTemplateSpecializationKind(
5663 TSK_ExplicitSpecialization);
5664 InstantiationClass->setLocation(Member->getLocation());
5665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005666
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005667 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005668 cast<CXXRecordDecl>(InstantiatedFrom),
5669 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005671
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005672 // Save the caller the trouble of having to figure out which declaration
5673 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005674 Previous.clear();
5675 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005676 return false;
5677}
5678
Douglas Gregor558c0322009-10-14 23:41:34 +00005679/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005680///
5681/// \returns true if a serious error occurs, false otherwise.
5682static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005683 SourceLocation InstLoc,
5684 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005685 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5686 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687
Douglas Gregor669eed82010-07-13 00:10:04 +00005688 if (CurContext->isRecord()) {
5689 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5690 << D;
5691 return true;
5692 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005693
Douglas Gregor558c0322009-10-14 23:41:34 +00005694 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005696 // template.
5697 //
5698 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005700 !CurContext->Encloses(OrigContext)) {
5701 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702 S.Diag(InstLoc,
5703 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005704 diag::err_explicit_instantiation_out_of_scope
5705 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005706 << D << NS;
5707 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005709 S.getLangOptions().CPlusPlus0x?
5710 diag::err_explicit_instantiation_must_be_global
5711 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005712 << D;
5713 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005714 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005715 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005716
Douglas Gregor558c0322009-10-14 23:41:34 +00005717 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005718 // If the name declared in the explicit instantiation is an unqualified
5719 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005720 // its template is declared or, if that namespace is inline (7.3.1), any
5721 // namespace from its enclosing namespace set.
5722 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005723 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005724
5725 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005726 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005727
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005728 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005729 S.getLangOptions().CPlusPlus0x?
5730 diag::err_explicit_instantiation_unqualified_wrong_namespace
5731 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005732 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005733 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005734 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005735}
5736
5737/// \brief Determine whether the given scope specifier has a template-id in it.
5738static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5739 if (!SS.isSet())
5740 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005741
Douglas Gregor558c0322009-10-14 23:41:34 +00005742 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005744 // or a static data member of a class template specialization, the name of
5745 // the class template specialization in the qualified-id for the member
5746 // name shall be a simple-template-id.
5747 //
5748 // C++98 has the same restriction, just worded differently.
5749 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5750 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005751 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005752 if (isa<TemplateSpecializationType>(T))
5753 return true;
5754
5755 return false;
5756}
5757
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005758// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005759DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005760Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005761 SourceLocation ExternLoc,
5762 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005763 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005764 SourceLocation KWLoc,
5765 const CXXScopeSpec &SS,
5766 TemplateTy TemplateD,
5767 SourceLocation TemplateNameLoc,
5768 SourceLocation LAngleLoc,
5769 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005770 SourceLocation RAngleLoc,
5771 AttributeList *Attr) {
5772 // Find the class template we're specializing
5773 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005774 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005775 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5776
5777 // Check that the specialization uses the same tag kind as the
5778 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005779 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5780 assert(Kind != TTK_Enum &&
5781 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005782 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005783 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005784 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005785 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005786 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005787 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005788 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005789 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005790 diag::note_previous_use);
5791 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5792 }
5793
Douglas Gregor558c0322009-10-14 23:41:34 +00005794 // C++0x [temp.explicit]p2:
5795 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005796 // definition and an explicit instantiation declaration. An explicit
5797 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005798 TemplateSpecializationKind TSK
5799 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5800 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005801
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005802 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005803 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005804 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005805
5806 // Check that the template argument list is well-formed for this
5807 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005808 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005809 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5810 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005811 return true;
5812
Douglas Gregor910f8002010-11-07 23:05:16 +00005813 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005814 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005815
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005816 // Find the class template specialization declaration that
5817 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005818 void *InsertPos = 0;
5819 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005820 = ClassTemplate->findSpecialization(Converted.data(),
5821 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005822
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005823 TemplateSpecializationKind PrevDecl_TSK
5824 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5825
Douglas Gregord5cb8762009-10-07 00:13:32 +00005826 // C++0x [temp.explicit]p2:
5827 // [...] An explicit instantiation shall appear in an enclosing
5828 // namespace of its template. [...]
5829 //
5830 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005831 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5832 SS.isSet()))
5833 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005834
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005835 ClassTemplateSpecializationDecl *Specialization = 0;
5836
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005837 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005838 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005839 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005840 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005841 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005842 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005843 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005844
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005845 // Even though HasNoEffect == true means that this explicit instantiation
5846 // has no effect on semantics, we go on to put its syntax in the AST.
5847
5848 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5849 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005850 // Since the only prior class template specialization with these
5851 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005852 // declaration node as our own, updating the source location
5853 // for the template name to reflect our new declaration.
5854 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005855 Specialization = PrevDecl;
5856 Specialization->setLocation(TemplateNameLoc);
5857 PrevDecl = 0;
5858 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005859 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005860
Douglas Gregor52604ab2009-09-11 21:19:12 +00005861 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005862 // Create a new class template specialization declaration node for
5863 // this explicit specialization.
5864 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005865 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005866 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005867 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005868 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005869 Converted.data(),
5870 Converted.size(),
5871 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005872 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005873
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005874 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005875 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005876 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005877 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005878 }
5879
5880 // Build the fully-sugared type for this explicit instantiation as
5881 // the user wrote in the explicit instantiation itself. This means
5882 // that we'll pretty-print the type retrieved from the
5883 // specialization's declaration the way that the user actually wrote
5884 // the explicit instantiation, rather than formatting the name based
5885 // on the "canonical" representation used to store the template
5886 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005887 TypeSourceInfo *WrittenTy
5888 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5889 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005890 Context.getTypeDeclType(Specialization));
5891 Specialization->setTypeAsWritten(WrittenTy);
5892 TemplateArgsIn.release();
5893
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005894 // Set source locations for keywords.
5895 Specialization->setExternLoc(ExternLoc);
5896 Specialization->setTemplateKeywordLoc(TemplateLoc);
5897
5898 // Add the explicit instantiation into its lexical context. However,
5899 // since explicit instantiations are never found by name lookup, we
5900 // just put it into the declaration context directly.
5901 Specialization->setLexicalDeclContext(CurContext);
5902 CurContext->addDecl(Specialization);
5903
5904 // Syntax is now OK, so return if it has no other effect on semantics.
5905 if (HasNoEffect) {
5906 // Set the template specialization kind.
5907 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005908 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005909 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005910
5911 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005912 // A definition of a class template or class member template
5913 // shall be in scope at the point of the explicit instantiation of
5914 // the class template or class member template.
5915 //
5916 // This check comes when we actually try to perform the
5917 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005918 ClassTemplateSpecializationDecl *Def
5919 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005920 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005921 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005922 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005923 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005924 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005925 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5926 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005927
Douglas Gregor0d035142009-10-27 18:42:08 +00005928 // Instantiate the members of this class template specialization.
5929 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005930 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005931 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005932 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5933
5934 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5935 // TSK_ExplicitInstantiationDefinition
5936 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5937 TSK == TSK_ExplicitInstantiationDefinition)
5938 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005939
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005940 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005941 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005942
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005943 // Set the template specialization kind.
5944 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005945 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005946}
5947
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005948// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005949DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005950Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005951 SourceLocation ExternLoc,
5952 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005953 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005954 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005955 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005956 IdentifierInfo *Name,
5957 SourceLocation NameLoc,
5958 AttributeList *Attr) {
5959
Douglas Gregor402abb52009-05-28 23:31:59 +00005960 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005961 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005962 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005963 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5964 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005965 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005966 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005967 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5968
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005969 if (!TagD)
5970 return true;
5971
John McCalld226f652010-08-21 09:40:31 +00005972 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005973 if (Tag->isEnum()) {
5974 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5975 << Context.getTypeDeclType(Tag);
5976 return true;
5977 }
5978
Douglas Gregord0c87372009-05-27 17:30:49 +00005979 if (Tag->isInvalidDecl())
5980 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005981
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005982 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5983 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5984 if (!Pattern) {
5985 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5986 << Context.getTypeDeclType(Record);
5987 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5988 return true;
5989 }
5990
Douglas Gregor558c0322009-10-14 23:41:34 +00005991 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005992 // If the explicit instantiation is for a class or member class, the
5993 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005994 // simple-template-id.
5995 //
5996 // C++98 has the same restriction, just worded differently.
5997 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005998 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005999 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006000
Douglas Gregor558c0322009-10-14 23:41:34 +00006001 // C++0x [temp.explicit]p2:
6002 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006003 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006004 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006005 TemplateSpecializationKind TSK
6006 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6007 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006008
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006009 // C++0x [temp.explicit]p2:
6010 // [...] An explicit instantiation shall appear in an enclosing
6011 // namespace of its template. [...]
6012 //
6013 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006014 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006015
Douglas Gregor454885e2009-10-15 15:54:05 +00006016 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006017 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006018 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006019 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006020 PrevDecl = Record;
6021 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006022 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006023 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006024 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006025 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006026 PrevDecl,
6027 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006028 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006029 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006030 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006031 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006032 return TagD;
6033 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006034
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006035 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006036 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006037 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006038 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006039 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006040 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006041 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006042 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006043 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006044 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6045 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006046 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6047 << Pattern;
6048 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006049 } else {
6050 if (InstantiateClass(NameLoc, Record, Def,
6051 getTemplateInstantiationArgs(Record),
6052 TSK))
6053 return true;
6054
Douglas Gregor952b0172010-02-11 01:04:33 +00006055 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006056 if (!RecordDef)
6057 return true;
6058 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006059 }
6060
Douglas Gregor0d035142009-10-27 18:42:08 +00006061 // Instantiate all of the members of the class.
6062 InstantiateClassMembers(NameLoc, RecordDef,
6063 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006064
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006065 if (TSK == TSK_ExplicitInstantiationDefinition)
6066 MarkVTableUsed(NameLoc, RecordDef, true);
6067
Mike Stump390b4cc2009-05-16 07:39:55 +00006068 // FIXME: We don't have any representation for explicit instantiations of
6069 // member classes. Such a representation is not needed for compilation, but it
6070 // should be available for clients that want to see all of the declarations in
6071 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006072 return TagD;
6073}
6074
John McCallf312b1e2010-08-26 23:41:50 +00006075DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6076 SourceLocation ExternLoc,
6077 SourceLocation TemplateLoc,
6078 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006079 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006080 // TODO: check if/when DNInfo should replace Name.
6081 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6082 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006083 if (!Name) {
6084 if (!D.isInvalidType())
6085 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6086 diag::err_explicit_instantiation_requires_name)
6087 << D.getDeclSpec().getSourceRange()
6088 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006089
Douglas Gregord5a423b2009-09-25 18:43:00 +00006090 return true;
6091 }
6092
6093 // The scope passed in may not be a decl scope. Zip up the scope tree until
6094 // we find one that is.
6095 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6096 (S->getFlags() & Scope::TemplateParamScope) != 0)
6097 S = S->getParent();
6098
6099 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006100 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6101 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006102 if (R.isNull())
6103 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006104
Douglas Gregore885e182011-05-21 18:53:30 +00006105 // C++ [dcl.stc]p1:
6106 // A storage-class-specifier shall not be specified in [...] an explicit
6107 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006108 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006109 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6110 << Name;
6111 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006112 } else if (D.getDeclSpec().getStorageClassSpec()
6113 != DeclSpec::SCS_unspecified) {
6114 // Complain about then remove the storage class specifier.
6115 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6116 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6117
6118 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006119 }
6120
Douglas Gregor663b5a02009-10-14 20:14:33 +00006121 // C++0x [temp.explicit]p1:
6122 // [...] An explicit instantiation of a function template shall not use the
6123 // inline or constexpr specifiers.
6124 // Presumably, this also applies to member functions of class templates as
6125 // well.
6126 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006127 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006128 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006129 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006130
Douglas Gregor663b5a02009-10-14 20:14:33 +00006131 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006132
Douglas Gregor558c0322009-10-14 23:41:34 +00006133 // C++0x [temp.explicit]p2:
6134 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006135 // definition and an explicit instantiation declaration. An explicit
6136 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006137 TemplateSpecializationKind TSK
6138 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6139 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006140
Abramo Bagnara25777432010-08-11 22:01:17 +00006141 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006142 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006143
6144 if (!R->isFunctionType()) {
6145 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006146 // A [...] static data member of a class template can be explicitly
6147 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006148 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006149 if (Previous.isAmbiguous())
6150 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006151
John McCall1bcee0a2009-12-02 08:25:40 +00006152 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006153 if (!Prev || !Prev->isStaticDataMember()) {
6154 // We expect to see a data data member here.
6155 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6156 << Name;
6157 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6158 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006159 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006160 return true;
6161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006162
Douglas Gregord5a423b2009-09-25 18:43:00 +00006163 if (!Prev->getInstantiatedFromStaticDataMember()) {
6164 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006165 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006166 diag::err_explicit_instantiation_data_member_not_instantiated)
6167 << Prev;
6168 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6169 // FIXME: Can we provide a note showing where this was declared?
6170 return true;
6171 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006172
Douglas Gregor558c0322009-10-14 23:41:34 +00006173 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006174 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006175 // or a static data member of a class template specialization, the name of
6176 // the class template specialization in the qualified-id for the member
6177 // name shall be a simple-template-id.
6178 //
6179 // C++98 has the same restriction, just worded differently.
6180 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006181 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006182 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006183 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006184
Douglas Gregor558c0322009-10-14 23:41:34 +00006185 // Check the scope of this explicit instantiation.
6186 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006187
Douglas Gregor454885e2009-10-15 15:54:05 +00006188 // Verify that it is okay to explicitly instantiate here.
6189 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6190 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006191 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006192 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006193 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006194 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006195 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006196 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006197 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006198 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006199
Douglas Gregord5a423b2009-09-25 18:43:00 +00006200 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006201 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006202 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006203 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006204
Douglas Gregord5a423b2009-09-25 18:43:00 +00006205 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006206 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006207 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006208
6209 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006210 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006211 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006212 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006213 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6214 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006215 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6216 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006217 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6218 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006219 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006220 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006221 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006222 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006223 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006224
Douglas Gregord5a423b2009-09-25 18:43:00 +00006225 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006226 // A [...] function [...] can be explicitly instantiated from its template.
6227 // A member function [...] of a class template can be explicitly
6228 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006229 // template.
John McCallc373d482010-01-27 01:50:18 +00006230 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006231 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6232 P != PEnd; ++P) {
6233 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006234 if (!HasExplicitTemplateArgs) {
6235 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6236 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6237 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006238
John McCallc373d482010-01-27 01:50:18 +00006239 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006240 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6241 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006242 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006243 }
6244 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006245
Douglas Gregord5a423b2009-09-25 18:43:00 +00006246 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6247 if (!FunTmpl)
6248 continue;
6249
John McCall5769d612010-02-08 23:07:23 +00006250 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006251 FunctionDecl *Specialization = 0;
6252 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006254 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006255 R, Specialization, Info)) {
6256 // FIXME: Keep track of almost-matches?
6257 (void)TDK;
6258 continue;
6259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006260
John McCallc373d482010-01-27 01:50:18 +00006261 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006263
Douglas Gregord5a423b2009-09-25 18:43:00 +00006264 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006265 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006266 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006267 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006268 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6269 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6270 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006271
John McCallc373d482010-01-27 01:50:18 +00006272 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006273 return true;
John McCallc373d482010-01-27 01:50:18 +00006274
6275 // Ignore access control bits, we don't need them for redeclaration checking.
6276 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006277
Douglas Gregor0a897e32009-10-15 17:21:20 +00006278 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006280 diag::err_explicit_instantiation_member_function_not_instantiated)
6281 << Specialization
6282 << (Specialization->getTemplateSpecializationKind() ==
6283 TSK_ExplicitSpecialization);
6284 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6285 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006286 }
6287
Douglas Gregor0a897e32009-10-15 17:21:20 +00006288 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006289 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6290 PrevDecl = Specialization;
6291
Douglas Gregor0a897e32009-10-15 17:21:20 +00006292 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006293 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006294 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006295 PrevDecl,
6296 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006297 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006298 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006299 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006300
Douglas Gregor0a897e32009-10-15 17:21:20 +00006301 // FIXME: We may still want to build some representation of this
6302 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006303 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006304 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006305 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006306
6307 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006308
Douglas Gregor0a897e32009-10-15 17:21:20 +00006309 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006310 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311
Douglas Gregor558c0322009-10-14 23:41:34 +00006312 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006313 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006314 // or a static data member of a class template specialization, the name of
6315 // the class template specialization in the qualified-id for the member
6316 // name shall be a simple-template-id.
6317 //
6318 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006319 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006320 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006321 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006322 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006323 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006324 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006325 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006326
Douglas Gregor558c0322009-10-14 23:41:34 +00006327 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006328 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006329 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006330 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006331 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006332
Douglas Gregord5a423b2009-09-25 18:43:00 +00006333 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006334 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006335}
6336
John McCallf312b1e2010-08-26 23:41:50 +00006337TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006338Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6339 const CXXScopeSpec &SS, IdentifierInfo *Name,
6340 SourceLocation TagLoc, SourceLocation NameLoc) {
6341 // This has to hold, because SS is expected to be defined.
6342 assert(Name && "Expected a name in a dependent tag");
6343
6344 NestedNameSpecifier *NNS
6345 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6346 if (!NNS)
6347 return true;
6348
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006349 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006350
Douglas Gregor48c89f42010-04-24 16:38:41 +00006351 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6352 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006353 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006354 return true;
6355 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006356
Douglas Gregor059101f2011-03-02 00:47:37 +00006357 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006358 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006359 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6360
6361 // Create type-source location information for this type.
6362 TypeLocBuilder TLB;
6363 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6364 TL.setKeywordLoc(TagLoc);
6365 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6366 TL.setNameLoc(NameLoc);
6367 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006368}
6369
John McCallf312b1e2010-08-26 23:41:50 +00006370TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006371Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6372 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006373 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006374 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006375 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006376
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006377 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6378 !getLangOptions().CPlusPlus0x)
6379 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006380 << FixItHint::CreateRemoval(TypenameLoc);
6381
Douglas Gregor2494dd02011-03-01 01:34:45 +00006382 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006383 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6384 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006385 if (T.isNull())
6386 return true;
John McCall63b43852010-04-29 23:50:39 +00006387
6388 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6389 if (isa<DependentNameType>(T)) {
6390 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006391 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006392 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006393 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006394 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006395 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006396 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006397 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006398 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006399 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400
John McCallb3d87482010-08-24 05:47:05 +00006401 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006402}
6403
John McCallf312b1e2010-08-26 23:41:50 +00006404TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006405Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6406 const CXXScopeSpec &SS,
6407 SourceLocation TemplateLoc,
6408 TemplateTy TemplateIn,
6409 SourceLocation TemplateNameLoc,
6410 SourceLocation LAngleLoc,
6411 ASTTemplateArgsPtr TemplateArgsIn,
6412 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006413 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6414 !getLangOptions().CPlusPlus0x)
6415 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006416 << FixItHint::CreateRemoval(TypenameLoc);
6417
6418 // Translate the parser's template argument list in our AST format.
6419 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6420 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6421
6422 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006423 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6424 // Construct a dependent template specialization type.
6425 assert(DTN && "dependent template has non-dependent name?");
6426 assert(DTN->getQualifier()
6427 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6428 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6429 DTN->getQualifier(),
6430 DTN->getIdentifier(),
6431 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006432
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006433 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006434 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006435 DependentTemplateSpecializationTypeLoc SpecTL
6436 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006437 SpecTL.setLAngleLoc(LAngleLoc);
6438 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006439 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006440 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006441 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006442 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6443 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006444 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006445 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006446
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006447 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6448 if (T.isNull())
6449 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006450
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006451 // Provide source-location information for the template specialization
6452 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006453 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006454 TemplateSpecializationTypeLoc SpecTL
6455 = Builder.push<TemplateSpecializationTypeLoc>(T);
6456
6457 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006458 SpecTL.setLAngleLoc(LAngleLoc);
6459 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006460 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006461 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6462 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6463
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006464 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6465 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6466 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006467 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6468
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006469 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6470 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006471}
6472
Douglas Gregora02411e2011-02-27 22:46:49 +00006473
Douglas Gregord57959a2009-03-27 23:10:48 +00006474/// \brief Build the type that describes a C++ typename specifier,
6475/// e.g., "typename T::type".
6476QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006477Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6478 SourceLocation KeywordLoc,
6479 NestedNameSpecifierLoc QualifierLoc,
6480 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006481 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006482 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006483 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006484
John McCall77bb1aa2010-05-01 00:40:08 +00006485 DeclContext *Ctx = computeDeclContext(SS);
6486 if (!Ctx) {
6487 // If the nested-name-specifier is dependent and couldn't be
6488 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006489 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6490 return Context.getDependentNameType(Keyword,
6491 QualifierLoc.getNestedNameSpecifier(),
6492 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006493 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006494
John McCall77bb1aa2010-05-01 00:40:08 +00006495 // If the nested-name-specifier refers to the current instantiation,
6496 // the "typename" keyword itself is superfluous. In C++03, the
6497 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6498 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006499 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006500
John McCall77bb1aa2010-05-01 00:40:08 +00006501 if (RequireCompleteDeclContext(SS, Ctx))
6502 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006503
6504 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006505 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006506 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006507 unsigned DiagID = 0;
6508 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006509 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006510 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006511 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006512 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006513
6514 case LookupResult::FoundUnresolvedValue: {
6515 // We found a using declaration that is a value. Most likely, the using
6516 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006517 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006518 IILoc);
6519 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6520 << Name << Ctx << FullRange;
6521 if (UnresolvedUsingValueDecl *Using
6522 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006523 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006524 Diag(Loc, diag::note_using_value_decl_missing_typename)
6525 << FixItHint::CreateInsertion(Loc, "typename ");
6526 }
6527 }
6528 // Fall through to create a dependent typename type, from which we can recover
6529 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006530
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006531 case LookupResult::NotFoundInCurrentInstantiation:
6532 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006533 return Context.getDependentNameType(Keyword,
6534 QualifierLoc.getNestedNameSpecifier(),
6535 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006536
6537 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006538 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006539 // We found a type. Build an ElaboratedType, since the
6540 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006541 return Context.getElaboratedType(ETK_Typename,
6542 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006543 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006544 }
6545
6546 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006547 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006548 break;
6549
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006550
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006551 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006552 return QualType();
6553
Douglas Gregord57959a2009-03-27 23:10:48 +00006554 case LookupResult::FoundOverloaded:
6555 DiagID = diag::err_typename_nested_not_type;
6556 Referenced = *Result.begin();
6557 break;
6558
John McCall6e247262009-10-10 05:48:19 +00006559 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006560 return QualType();
6561 }
6562
6563 // If we get here, it's because name lookup did not find a
6564 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006565 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006566 IILoc);
6567 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006568 if (Referenced)
6569 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6570 << Name;
6571 return QualType();
6572}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006573
6574namespace {
6575 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006576 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006577 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006578 SourceLocation Loc;
6579 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006580
Douglas Gregor4a959d82009-08-06 16:20:37 +00006581 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006582 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006583
Mike Stump1eb44332009-09-09 15:08:12 +00006584 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006585 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006586 DeclarationName Entity)
6587 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006588 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006589
6590 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006591 /// transformed.
6592 ///
6593 /// For the purposes of type reconstruction, a type has already been
6594 /// transformed if it is NULL or if it is not dependent.
6595 bool AlreadyTransformed(QualType T) {
6596 return T.isNull() || !T->isDependentType();
6597 }
Mike Stump1eb44332009-09-09 15:08:12 +00006598
6599 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006600 /// rebuilt.
6601 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006602
Douglas Gregor4a959d82009-08-06 16:20:37 +00006603 /// \brief Returns the name of the entity whose type is being rebuilt.
6604 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006605
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006606 /// \brief Sets the "base" location and entity when that
6607 /// information is known based on another transformation.
6608 void setBase(SourceLocation Loc, DeclarationName Entity) {
6609 this->Loc = Loc;
6610 this->Entity = Entity;
6611 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006612 };
6613}
6614
Douglas Gregor4a959d82009-08-06 16:20:37 +00006615/// \brief Rebuilds a type within the context of the current instantiation.
6616///
Mike Stump1eb44332009-09-09 15:08:12 +00006617/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006618/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006619/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006620/// partial specialization thereof). This routine will rebuild that type now
6621/// that we have entered the declarator's scope, which may produce different
6622/// canonical types, e.g.,
6623///
6624/// \code
6625/// template<typename T>
6626/// struct X {
6627/// typedef T* pointer;
6628/// pointer data();
6629/// };
6630///
6631/// template<typename T>
6632/// typename X<T>::pointer X<T>::data() { ... }
6633/// \endcode
6634///
Douglas Gregor4714c122010-03-31 17:34:00 +00006635/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006636/// since we do not know that we can look into X<T> when we parsed the type.
6637/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006638/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006639/// as the canonical type of T*, allowing the return types of the out-of-line
6640/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006641TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6642 SourceLocation Loc,
6643 DeclarationName Name) {
6644 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006645 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006646
Douglas Gregor4a959d82009-08-06 16:20:37 +00006647 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6648 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006649}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006650
John McCall60d7b3a2010-08-24 06:29:42 +00006651ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006652 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6653 DeclarationName());
6654 return Rebuilder.TransformExpr(E);
6655}
6656
John McCall63b43852010-04-29 23:50:39 +00006657bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006658 if (SS.isInvalid())
6659 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006660
Douglas Gregor7e384942011-02-25 16:07:42 +00006661 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006662 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6663 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006664 NestedNameSpecifierLoc Rebuilt
6665 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6666 if (!Rebuilt)
6667 return true;
John McCall63b43852010-04-29 23:50:39 +00006668
Douglas Gregor7e384942011-02-25 16:07:42 +00006669 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006670 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006671}
6672
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006673/// \brief Produces a formatted string that describes the binding of
6674/// template parameters to template arguments.
6675std::string
6676Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6677 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006678 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006679}
6680
6681std::string
6682Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6683 const TemplateArgument *Args,
6684 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006685 llvm::SmallString<128> Str;
6686 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006687
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006688 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006689 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006690
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006691 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006692 if (I >= NumArgs)
6693 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006694
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006695 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006696 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006697 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006698 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006699
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006700 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006701 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006702 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006703 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006704 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006705
Douglas Gregor87dd6972010-12-20 16:52:59 +00006706 Out << " = ";
6707 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006708 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006709
6710 Out << ']';
6711 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006712}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006713
6714void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6715 if (!FD)
6716 return;
6717 FD->setLateTemplateParsed(Flag);
6718}
6719
6720bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6721 DeclContext *DC = CurContext;
6722
6723 while (DC) {
6724 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6725 const FunctionDecl *FD = RD->isLocalClass();
6726 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6727 } else if (DC->isTranslationUnit() || DC->isNamespace())
6728 return false;
6729
6730 DC = DC->getParent();
6731 }
6732 return false;
6733}