blob: eb199aced45dee5613e4b95d6972c8d441aa747b [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
252 } else if (SS.isSet()) {
253 // This nested-name-specifier occurs after another nested-name-specifier,
254 // so long into the context associated with the prior nested-name-specifier.
255 LookupCtx = computeDeclContext(SS, EnteringContext);
256 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
John McCallf7a1a742009-11-24 19:00:30 +0000258 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000259 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000260 return;
261 }
262
263 bool ObjectTypeSearchedInScope = false;
264 if (LookupCtx) {
265 // Perform "qualified" name lookup into the declaration context we
266 // computed, which is either the type of the base of a member access
267 // expression or the declaration context associated with a prior
268 // nested-name-specifier.
269 LookupQualifiedName(Found, LookupCtx);
270
271 if (!ObjectType.isNull() && Found.empty()) {
272 // C++ [basic.lookup.classref]p1:
273 // In a class member access expression (5.2.5), if the . or -> token is
274 // immediately followed by an identifier followed by a <, the
275 // identifier must be looked up to determine whether the < is the
276 // beginning of a template argument list (14.2) or a less-than operator.
277 // The identifier is first looked up in the class of the object
278 // expression. If the identifier is not found, it is then looked up in
279 // the context of the entire postfix-expression and shall name a class
280 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000281 if (S) LookupName(Found, S);
282 ObjectTypeSearchedInScope = true;
283 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000284 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000285 // We cannot look into a dependent object type or nested nme
286 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000287 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000288 return;
289 } else {
290 // Perform unqualified name lookup in the current scope.
291 LookupName(Found, S);
292 }
293
Douglas Gregor2e933882010-01-12 17:06:20 +0000294 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000295 // If we did not find any names, attempt to correct any typos.
296 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000297 Found.clear();
298 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
299 Found.getLookupKind(), S, &SS,
300 LookupCtx, false,
301 CTC_CXXCasts)) {
302 Found.setLookupName(Corrected.getCorrection());
303 if (Corrected.getCorrectionDecl())
304 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000306 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000307 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
308 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000309 if (LookupCtx)
310 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000311 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
312 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000313 else
314 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 << Name << CorrectedQuotedStr
316 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000317 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
318 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000319 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000320 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000321 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000322 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000323 }
324 }
325
Douglas Gregor312eadb2011-04-24 05:37:28 +0000326 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000327 if (Found.empty()) {
328 if (isDependent)
329 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000330 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000331 }
John McCallf7a1a742009-11-24 19:00:30 +0000332
333 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
334 // C++ [basic.lookup.classref]p1:
335 // [...] If the lookup in the class of the object expression finds a
336 // template, the name is also looked up in the context of the entire
337 // postfix-expression and [...]
338 //
339 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
340 LookupOrdinaryName);
341 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000342 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000343
John McCallf7a1a742009-11-24 19:00:30 +0000344 if (FoundOuter.empty()) {
345 // - if the name is not found, the name found in the class of the
346 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000347 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
348 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000349 // - if the name is found in the context of the entire
350 // postfix-expression and does not name a class template, the name
351 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000352 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000353 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000354 // - if the name found is a class template, it must refer to the same
355 // entity as the one found in the class of the object expression,
356 // otherwise the program is ill-formed.
357 if (!Found.isSingleResult() ||
358 Found.getFoundDecl()->getCanonicalDecl()
359 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000360 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000361 diag::ext_nested_name_member_ref_lookup_ambiguous)
362 << Found.getLookupName()
363 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000364 Diag(Found.getRepresentativeDecl()->getLocation(),
365 diag::note_ambig_member_ref_object_type)
366 << ObjectType;
367 Diag(FoundOuter.getFoundDecl()->getLocation(),
368 diag::note_ambig_member_ref_scope);
369
370 // Recover by taking the template that we found in the object
371 // expression's type.
372 }
373 }
374 }
375}
376
John McCall2f841ba2009-12-02 03:53:29 +0000377/// ActOnDependentIdExpression - Handle a dependent id-expression that
378/// was just parsed. This is only possible with an explicit scope
379/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000380ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000381Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000382 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000383 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000384 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000385 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386
John McCall2f841ba2009-12-02 03:53:29 +0000387 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000388 isa<CXXMethodDecl>(DC) &&
389 cast<CXXMethodDecl>(DC)->isInstance()) {
390 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000391
John McCallf7a1a742009-11-24 19:00:30 +0000392 // Since the 'this' expression is synthesized, we don't need to
393 // perform the double-lookup check.
394 NamedDecl *FirstQualifierInScope = 0;
395
John McCallaa81e162009-12-01 22:10:20 +0000396 return Owned(CXXDependentScopeMemberExpr::Create(Context,
397 /*This*/ 0, ThisType,
398 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000399 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000400 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000401 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000402 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000403 TemplateArgs));
404 }
405
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000407}
408
John McCall60d7b3a2010-08-24 06:29:42 +0000409ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000410Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000411 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000412 const TemplateArgumentListInfo *TemplateArgs) {
413 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000416 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000417}
418
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
420/// that the template parameter 'PrevDecl' is being shadowed by a new
421/// declaration at location Loc. Returns true to indicate that this is
422/// an error, and false otherwise.
423bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000424 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000425
426 // Microsoft Visual C++ permits template parameters to be shadowed.
427 if (getLangOptions().Microsoft)
428 return false;
429
430 // C++ [temp.local]p4:
431 // A template-parameter shall not be redeclared within its
432 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434 << cast<NamedDecl>(PrevDecl)->getDeclName();
435 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
436 return true;
437}
438
Douglas Gregor2943aed2009-03-03 04:44:36 +0000439/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000440/// the parameter D to reference the templated declaration and return a pointer
441/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000442TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
443 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
444 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000445 return Temp;
446 }
447 return 0;
448}
449
Douglas Gregorba68eca2011-01-05 17:40:24 +0000450ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
451 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000453 "Only template template arguments can be pack expansions here");
454 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
455 "Template template argument pack expansion without packs");
456 ParsedTemplateArgument Result(*this);
457 Result.EllipsisLoc = EllipsisLoc;
458 return Result;
459}
460
Douglas Gregor788cd062009-11-11 01:00:40 +0000461static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
462 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 switch (Arg.getKind()) {
465 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000470 return TemplateArgumentLoc(TemplateArgument(T), DI);
471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000472
Douglas Gregor788cd062009-11-11 01:00:40 +0000473 case ParsedTemplateArgument::NonType: {
474 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
475 return TemplateArgumentLoc(TemplateArgument(E), E);
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000479 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000480 TemplateArgument TArg;
481 if (Arg.getEllipsisLoc().isValid())
482 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
483 else
484 TArg = Template;
485 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000486 Arg.getScopeSpec().getWithLocInContext(
487 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000488 Arg.getLocation(),
489 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 }
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000493 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 return TemplateArgumentLoc();
495}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496
Douglas Gregor788cd062009-11-11 01:00:40 +0000497/// \brief Translates template arguments as provided by the parser
498/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000499void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
500 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000502 TemplateArgs.addArgument(translateTemplateArgument(*this,
503 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000504}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// ActOnTypeParameter - Called when a C++ template type parameter
507/// (e.g., "typename T") has been parsed. Typename specifies whether
508/// the keyword "typename" was used to declare the type parameter
509/// (otherwise, "class" was used), and KeyLoc is the location of the
510/// "class" or "typename" keyword. ParamName is the name of the
511/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000512/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513/// If the type parameter has a default argument, it will be added
514/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000515Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
516 SourceLocation EllipsisLoc,
517 SourceLocation KeyLoc,
518 IdentifierInfo *ParamName,
519 SourceLocation ParamNameLoc,
520 unsigned Depth, unsigned Position,
521 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000522 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 assert(S->isTemplateParamScope() &&
524 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 bool Invalid = false;
526
527 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000528 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000529 LookupOrdinaryName,
530 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000533 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000534 }
535
Douglas Gregorddc29e12009-02-06 22:42:48 +0000536 SourceLocation Loc = ParamNameLoc;
537 if (!ParamName)
538 Loc = KeyLoc;
539
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000541 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000542 KeyLoc, Loc, Depth, Position, ParamName,
543 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000544 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000545 if (Invalid)
546 Param->setInvalidDecl();
547
548 if (ParamName) {
549 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000550 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000551 IdResolver.AddDecl(Param);
552 }
553
Douglas Gregor61c4d282011-01-05 15:48:55 +0000554 // C++0x [temp.param]p9:
555 // A default template-argument may be specified for any kind of
556 // template-parameter that is not a template parameter pack.
557 if (DefaultArg && Ellipsis) {
558 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
559 DefaultArg = ParsedType();
560 }
561
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 // Handle the default argument, if provided.
563 if (DefaultArg) {
564 TypeSourceInfo *DefaultTInfo;
565 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
Douglas Gregor6f526752010-12-16 08:48:57 +0000569 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000570 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000571 UPPC_DefaultArgument))
572 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000573
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000574 // Check the template argument itself.
575 if (CheckTemplateArgument(Param, DefaultTInfo)) {
576 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000577 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000579
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000580 Param->setDefaultArgument(DefaultTInfo, false);
581 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000582
John McCalld226f652010-08-21 09:40:31 +0000583 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000584}
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586/// \brief Check that the type of a non-type template parameter is
587/// well-formed.
588///
589/// \returns the (possibly-promoted) parameter type if valid;
590/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000591QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000592Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000593 // We don't allow variably-modified types as the type of non-type template
594 // parameters.
595 if (T->isVariablyModifiedType()) {
596 Diag(Loc, diag::err_variably_modified_nontype_template_param)
597 << T;
598 return QualType();
599 }
600
Douglas Gregor2943aed2009-03-03 04:44:36 +0000601 // C++ [temp.param]p4:
602 //
603 // A non-type template-parameter shall have one of the following
604 // (optionally cv-qualified) types:
605 //
606 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000607 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000609 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000610 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000611 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000612 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000614 // -- std::nullptr_t.
615 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 // If T is a dependent type, we can't do the check now, so we
617 // assume that it is well-formed.
618 T->isDependentType())
619 return T;
620 // C++ [temp.param]p8:
621 //
622 // A non-type template-parameter of type "array of T" or
623 // "function returning T" is adjusted to be of type "pointer to
624 // T" or "pointer to function returning T", respectively.
625 else if (T->isArrayType())
626 // FIXME: Keep the type prior to promotion?
627 return Context.getArrayDecayedType(T);
628 else if (T->isFunctionType())
629 // FIXME: Keep the type prior to promotion?
630 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000631
Douglas Gregor2943aed2009-03-03 04:44:36 +0000632 Diag(Loc, diag::err_template_nontype_parm_bad_type)
633 << T;
634
635 return QualType();
636}
637
John McCalld226f652010-08-21 09:40:31 +0000638Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
639 unsigned Depth,
640 unsigned Position,
641 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000642 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000643 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
644 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000645
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000646 assert(S->isTemplateParamScope() &&
647 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000648 bool Invalid = false;
649
650 IdentifierInfo *ParamName = D.getIdentifier();
651 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000653 LookupOrdinaryName,
654 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000655 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000656 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000657 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 }
659
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000660 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
661 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000662 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000663 Invalid = true;
664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000665
Douglas Gregor10738d32010-12-23 23:51:58 +0000666 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000667 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000668 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000669 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000670 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000671 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000672 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000673 Param->setAccess(AS_public);
674
Douglas Gregor72c3f312008-12-05 18:15:24 +0000675 if (Invalid)
676 Param->setInvalidDecl();
677
678 if (D.getIdentifier()) {
679 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000680 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000681 IdResolver.AddDecl(Param);
682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
Douglas Gregor61c4d282011-01-05 15:48:55 +0000684 // C++0x [temp.param]p9:
685 // A default template-argument may be specified for any kind of
686 // template-parameter that is not a template parameter pack.
687 if (Default && IsParameterPack) {
688 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
689 Default = 0;
690 }
691
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000692 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000693 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000694 // Check for unexpanded parameter packs.
695 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
696 return Param;
697
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000698 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000699 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
700 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000701 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000702 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 }
John Wiegley429bb272011-04-08 18:41:53 +0000704 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000705
John McCall9ae2f072010-08-23 23:25:46 +0000706 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000708
John McCalld226f652010-08-21 09:40:31 +0000709 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000710}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000711
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712/// ActOnTemplateTemplateParameter - Called when a C++ template template
713/// parameter (e.g. T in template <template <typename> class T> class array)
714/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000715Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
716 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000717 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000718 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000719 IdentifierInfo *Name,
720 SourceLocation NameLoc,
721 unsigned Depth,
722 unsigned Position,
723 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000724 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000725 assert(S->isTemplateParamScope() &&
726 "Template template parameter not in template parameter scope!");
727
728 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000729 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000731 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000732 NameLoc.isInvalid()? TmpLoc : NameLoc,
733 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000734 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000735 Param->setAccess(AS_public);
736
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000737 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000738 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000739 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000740 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 IdResolver.AddDecl(Param);
742 }
743
Douglas Gregor6f526752010-12-16 08:48:57 +0000744 if (Params->size() == 0) {
745 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
746 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
747 Param->setInvalidDecl();
748 }
749
Douglas Gregor61c4d282011-01-05 15:48:55 +0000750 // C++0x [temp.param]p9:
751 // A default template-argument may be specified for any kind of
752 // template-parameter that is not a template parameter pack.
753 if (IsParameterPack && !Default.isInvalid()) {
754 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
755 Default = ParsedTemplateArgument();
756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 if (!Default.isInvalid()) {
759 // Check only that we have a template template argument. We don't want to
760 // try to check well-formedness now, because our template template parameter
761 // might have dependent types in its template parameters, which we wouldn't
762 // be able to match now.
763 //
764 // If none of the template template parameter's template arguments mention
765 // other template parameters, we could actually perform more checking here.
766 // However, it isn't worth doing.
767 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
768 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
769 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
770 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000771 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregor6f526752010-12-16 08:48:57 +0000774 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 DefaultArg.getArgument().getAsTemplate(),
777 UPPC_DefaultArgument))
778 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000779
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000780 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000782
John McCalld226f652010-08-21 09:40:31 +0000783 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000784}
785
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000786/// ActOnTemplateParameterList - Builds a TemplateParameterList that
787/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000788TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000789Sema::ActOnTemplateParameterList(unsigned Depth,
790 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000791 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000793 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation RAngleLoc) {
795 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000796 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000797
Douglas Gregorddc29e12009-02-06 22:42:48 +0000798 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000799 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000800 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000801}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000802
John McCallb6217662010-03-15 10:12:16 +0000803static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
804 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000805 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000806}
807
John McCallf312b1e2010-08-26 23:41:50 +0000808DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000809Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000810 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 IdentifierInfo *Name, SourceLocation NameLoc,
812 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000813 TemplateParameterList *TemplateParams,
Douglas Gregor8d267c52011-09-09 02:06:17 +0000814 AccessSpecifier AS, bool IsModulePrivate,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000815 unsigned NumOuterTemplateParamLists,
816 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000817 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000819 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000820 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000821
822 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000823 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000824 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000825
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000826 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
827 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000828
829 // There is no such thing as an unnamed class template.
830 if (!Name) {
831 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000832 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000833 }
834
835 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000837 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000838 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000839 if (SS.isNotEmpty() && !SS.isInvalid()) {
840 SemanticContext = computeDeclContext(SS, true);
841 if (!SemanticContext) {
842 // FIXME: Produce a reasonable diagnostic here
843 return true;
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
John McCall77bb1aa2010-05-01 00:40:08 +0000846 if (RequireCompleteDeclContext(SS, SemanticContext))
847 return true;
848
John McCalla24dc2e2009-11-17 02:14:36 +0000849 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000850 } else {
851 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000852 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor57265e32010-04-12 16:00:01 +0000855 if (Previous.isAmbiguous())
856 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000857
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858 NamedDecl *PrevDecl = 0;
859 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000860 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861
Douglas Gregorddc29e12009-02-06 22:42:48 +0000862 // If there is a previous declaration with the same name, check
863 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000864 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000865 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000866
867 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000869 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000870 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000871 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
872 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000873 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000874 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
875 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
876 PrevClassTemplate
877 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
878 ->getSpecializedTemplate();
879 }
880 }
881
John McCall65c49462009-12-18 11:25:59 +0000882 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000883 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 // [...] When looking for a prior declaration of a class or a function
885 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000886 // function is neither a qualified name nor a template-id, scopes outside
887 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000888 if (!SS.isSet()) {
889 DeclContext *OutermostContext = CurContext;
890 while (!OutermostContext->isFileContext())
891 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000892
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000893 if (PrevDecl &&
894 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
895 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
896 SemanticContext = PrevDecl->getDeclContext();
897 } else {
898 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000899 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000900 // declaration.
901 PrevDecl = PrevClassTemplate = 0;
902 SemanticContext = OutermostContext;
903 }
John McCalle129d442009-12-17 23:21:11 +0000904 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000905
John McCalle129d442009-12-17 23:21:11 +0000906 if (CurContext->isDependentContext()) {
907 // If this is a dependent context, we don't want to link the friend
908 // class template to the template in scope, because that would perform
909 // checking of the template parameter lists that can't be performed
910 // until the outer context is instantiated.
911 PrevDecl = PrevClassTemplate = 0;
912 }
913 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
914 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 if (PrevClassTemplate) {
917 // Ensure that the template parameter lists are compatible.
918 if (!TemplateParameterListsAreEqual(TemplateParams,
919 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000920 /*Complain=*/true,
921 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000922 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923
924 // C++ [temp.class]p4:
925 // In a redeclaration, partial specialization, explicit
926 // specialization or explicit instantiation of a class template,
927 // the class-key shall agree in kind with the original class
928 // template declaration (7.1.5.3).
929 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000930 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
931 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000933 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000934 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000935 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000936 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938
Douglas Gregorddc29e12009-02-06 22:42:48 +0000939 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000940 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000941 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 Diag(NameLoc, diag::err_redefinition) << Name;
943 Diag(Def->getLocation(), diag::note_previous_definition);
944 // FIXME: Would it make sense to try to "forget" the previous
945 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000946 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000947 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000948 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000949 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
950 // Maybe we will complain about the shadowed template parameter.
951 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
952 // Just pretend that we didn't see the previous declaration.
953 PrevDecl = 0;
954 } else if (PrevDecl) {
955 // C++ [temp]p5:
956 // A class template shall not have the same name as any other
957 // template, class, function, object, enumeration, enumerator,
958 // namespace, or type in the same scope (3.3), except as specified
959 // in (14.5.4).
960 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
961 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000962 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000963 }
964
Douglas Gregord684b002009-02-10 19:49:53 +0000965 // Check the template parameter list of this declaration, possibly
966 // merging in the template parameter list from the previous class
967 // template declaration.
968 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000969 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000970 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000971 SemanticContext->isRecord() &&
972 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000973 ? TPC_ClassTemplateMember
974 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000975 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor57265e32010-04-12 16:00:01 +0000977 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000978 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000979 // template out-of-line.
980 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
981 !(TUK == TUK_Friend && CurContext->isDependentContext()))
982 Diag(NameLoc, diag::err_member_def_does_not_match)
983 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 }
985
Mike Stump1eb44332009-09-09 15:08:12 +0000986 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000987 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000988 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000989 PrevClassTemplate->getTemplatedDecl() : 0,
990 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000991 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000992 if (NumOuterTemplateParamLists > 0)
993 NewClass->setTemplateParameterListsInfo(Context,
994 NumOuterTemplateParamLists,
995 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000996
997 ClassTemplateDecl *NewTemplate
998 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
999 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001000 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001001 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001002
1003 if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
1004 NewTemplate->setModulePrivate();
1005 } else if (IsModulePrivate)
Douglas Gregor8d267c52011-09-09 02:06:17 +00001006 NewTemplate->setModulePrivate();
1007
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001008 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001009 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001010 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001011 assert(T->isDependentType() && "Class template type is not dependent?");
1012 (void)T;
1013
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001014 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001015 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001016 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001017 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1018 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001020 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001021 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001022 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Douglas Gregorddc29e12009-02-06 22:42:48 +00001024 // Set the lexical context of these templates
1025 NewClass->setLexicalDeclContext(CurContext);
1026 NewTemplate->setLexicalDeclContext(CurContext);
1027
John McCall0f434ec2009-07-31 02:45:11 +00001028 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001029 NewClass->startDefinition();
1030
1031 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001032 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033
John McCall05b23ea2009-09-14 21:59:20 +00001034 if (TUK != TUK_Friend)
1035 PushOnScopeChains(NewTemplate, S);
1036 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001037 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001038 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001039 NewClass->setAccess(PrevClassTemplate->getAccess());
1040 }
John McCall05b23ea2009-09-14 21:59:20 +00001041
Douglas Gregord85bea22009-09-26 06:47:28 +00001042 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1043 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001044
John McCall05b23ea2009-09-14 21:59:20 +00001045 // Friend templates are visible in fairly strange ways.
1046 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001047 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001048 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1049 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1050 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001052 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001053
Douglas Gregord85bea22009-09-26 06:47:28 +00001054 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1055 NewClass->getLocation(),
1056 NewTemplate,
1057 /*FIXME:*/NewClass->getLocation());
1058 Friend->setAccess(AS_public);
1059 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001060 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001061
Douglas Gregord684b002009-02-10 19:49:53 +00001062 if (Invalid) {
1063 NewTemplate->setInvalidDecl();
1064 NewClass->setInvalidDecl();
1065 }
John McCalld226f652010-08-21 09:40:31 +00001066 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001067}
1068
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001069/// \brief Diagnose the presence of a default template argument on a
1070/// template parameter, which is ill-formed in certain contexts.
1071///
1072/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001073static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001074 Sema::TemplateParamListContext TPC,
1075 SourceLocation ParamLoc,
1076 SourceRange DefArgRange) {
1077 switch (TPC) {
1078 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001079 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 return false;
1081
1082 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001083 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001084 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085 // A default template-argument shall not be specified in a
1086 // function template declaration or a function template
1087 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001088 // If a friend function template declaration specifies a default
1089 // template-argument, that declaration shall be a definition and shall be
1090 // the only declaration of the function template in the translation unit.
1091 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001092 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001093 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001094 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001095 << DefArgRange;
1096 return false;
1097
1098 case Sema::TPC_ClassTemplateMember:
1099 // C++0x [temp.param]p9:
1100 // A default template-argument shall not be specified in the
1101 // template-parameter-lists of the definition of a member of a
1102 // class template that appears outside of the member's class.
1103 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1104 << DefArgRange;
1105 return true;
1106
1107 case Sema::TPC_FriendFunctionTemplate:
1108 // C++ [temp.param]p9:
1109 // A default template-argument shall not be specified in a
1110 // friend template declaration.
1111 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1112 << DefArgRange;
1113 return true;
1114
1115 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1116 // for friend function templates if there is only a single
1117 // declaration (and it is a definition). Strange!
1118 }
1119
1120 return false;
1121}
1122
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001123/// \brief Check for unexpanded parameter packs within the template parameters
1124/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001125static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1126 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001127 TemplateParameterList *Params = TTP->getTemplateParameters();
1128 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1129 NamedDecl *P = Params->getParam(I);
1130 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001131 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001132 NTTP->getTypeSourceInfo(),
1133 Sema::UPPC_NonTypeTemplateParameterType))
1134 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001135
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001136 continue;
1137 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001138
1139 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001140 = dyn_cast<TemplateTemplateParmDecl>(P))
1141 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1142 return true;
1143 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001144
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001145 return false;
1146}
1147
Douglas Gregord684b002009-02-10 19:49:53 +00001148/// \brief Checks the validity of a template parameter list, possibly
1149/// considering the template parameter list from a previous
1150/// declaration.
1151///
1152/// If an "old" template parameter list is provided, it must be
1153/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1154/// template parameter list.
1155///
1156/// \param NewParams Template parameter list for a new template
1157/// declaration. This template parameter list will be updated with any
1158/// default arguments that are carried through from the previous
1159/// template parameter list.
1160///
1161/// \param OldParams If provided, template parameter list from a
1162/// previous declaration of the same template. Default template
1163/// arguments will be merged from the old template parameter list to
1164/// the new template parameter list.
1165///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001166/// \param TPC Describes the context in which we are checking the given
1167/// template parameter list.
1168///
Douglas Gregord684b002009-02-10 19:49:53 +00001169/// \returns true if an error occurred, false otherwise.
1170bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001171 TemplateParameterList *OldParams,
1172 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001173 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregord684b002009-02-10 19:49:53 +00001175 // C++ [temp.param]p10:
1176 // The set of default template-arguments available for use with a
1177 // template declaration or definition is obtained by merging the
1178 // default arguments from the definition (if in scope) and all
1179 // declarations in scope in the same way default function
1180 // arguments are (8.3.6).
1181 bool SawDefaultArgument = false;
1182 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001183
Anders Carlsson49d25572009-06-12 23:20:15 +00001184 bool SawParameterPack = false;
1185 SourceLocation ParameterPackLoc;
1186
Mike Stump1a35fde2009-02-11 23:03:27 +00001187 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001188 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001189 if (OldParams)
1190 OldParam = OldParams->begin();
1191
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001192 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001193 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1194 NewParamEnd = NewParams->end();
1195 NewParam != NewParamEnd; ++NewParam) {
1196 // Variables used to diagnose redundant default arguments
1197 bool RedundantDefaultArg = false;
1198 SourceLocation OldDefaultLoc;
1199 SourceLocation NewDefaultLoc;
1200
1201 // Variables used to diagnose missing default arguments
1202 bool MissingDefaultArg = false;
1203
Anders Carlsson49d25572009-06-12 23:20:15 +00001204 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001205 // If a template parameter of a primary class template or alias template
1206 // is a template parameter pack, it shall be the last template parameter.
1207 if (SawParameterPack &&
1208 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001209 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001210 diag::err_template_param_pack_must_be_last_template_parameter);
1211 Invalid = true;
1212 }
1213
Douglas Gregord684b002009-02-10 19:49:53 +00001214 if (TemplateTypeParmDecl *NewTypeParm
1215 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001216 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001217 if (NewTypeParm->hasDefaultArgument() &&
1218 DiagnoseDefaultTemplateArgument(*this, TPC,
1219 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001220 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001221 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001222 NewTypeParm->removeDefaultArgument();
1223
1224 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001225 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001226 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Anders Carlsson49d25572009-06-12 23:20:15 +00001228 if (NewTypeParm->isParameterPack()) {
1229 assert(!NewTypeParm->hasDefaultArgument() &&
1230 "Parameter packs can't have a default argument!");
1231 SawParameterPack = true;
1232 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001233 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001234 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001235 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1236 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1237 SawDefaultArgument = true;
1238 RedundantDefaultArg = true;
1239 PreviousDefaultArgLoc = NewDefaultLoc;
1240 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1241 // Merge the default argument from the old declaration to the
1242 // new declaration.
1243 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001244 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001245 true);
1246 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1247 } else if (NewTypeParm->hasDefaultArgument()) {
1248 SawDefaultArgument = true;
1249 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1250 } else if (SawDefaultArgument)
1251 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001252 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001253 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001254 // Check for unexpanded parameter packs.
1255 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001256 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001257 UPPC_NonTypeTemplateParameterType)) {
1258 Invalid = true;
1259 continue;
1260 }
1261
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001262 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001263 if (NewNonTypeParm->hasDefaultArgument() &&
1264 DiagnoseDefaultTemplateArgument(*this, TPC,
1265 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001266 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001267 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001268 }
1269
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001270 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001271 NonTypeTemplateParmDecl *OldNonTypeParm
1272 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001273 if (NewNonTypeParm->isParameterPack()) {
1274 assert(!NewNonTypeParm->hasDefaultArgument() &&
1275 "Parameter packs can't have a default argument!");
1276 SawParameterPack = true;
1277 ParameterPackLoc = NewNonTypeParm->getLocation();
1278 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001279 NewNonTypeParm->hasDefaultArgument()) {
1280 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1281 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1282 SawDefaultArgument = true;
1283 RedundantDefaultArg = true;
1284 PreviousDefaultArgLoc = NewDefaultLoc;
1285 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1286 // Merge the default argument from the old declaration to the
1287 // new declaration.
1288 SawDefaultArgument = true;
1289 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001290 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001291 // parameter.
1292 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001293 OldNonTypeParm->getDefaultArgument(),
1294 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001295 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1296 } else if (NewNonTypeParm->hasDefaultArgument()) {
1297 SawDefaultArgument = true;
1298 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1299 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001300 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001301 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001302 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001303 TemplateTemplateParmDecl *NewTemplateParm
1304 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001305
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001306 // Check for unexpanded parameter packs, recursively.
1307 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1308 Invalid = true;
1309 continue;
1310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001311
1312 if (NewTemplateParm->hasDefaultArgument() &&
1313 DiagnoseDefaultTemplateArgument(*this, TPC,
1314 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001315 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001316 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001317
1318 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001319 TemplateTemplateParmDecl *OldTemplateParm
1320 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001321 if (NewTemplateParm->isParameterPack()) {
1322 assert(!NewTemplateParm->hasDefaultArgument() &&
1323 "Parameter packs can't have a default argument!");
1324 SawParameterPack = true;
1325 ParameterPackLoc = NewTemplateParm->getLocation();
1326 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001327 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001328 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1329 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001330 SawDefaultArgument = true;
1331 RedundantDefaultArg = true;
1332 PreviousDefaultArgLoc = NewDefaultLoc;
1333 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1334 // Merge the default argument from the old declaration to the
1335 // new declaration.
1336 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001337 // FIXME: We need to create a new kind of "default argument" expression
1338 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001339 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001340 OldTemplateParm->getDefaultArgument(),
1341 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001342 PreviousDefaultArgLoc
1343 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001344 } else if (NewTemplateParm->hasDefaultArgument()) {
1345 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001346 PreviousDefaultArgLoc
1347 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001348 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001349 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001350 }
1351
1352 if (RedundantDefaultArg) {
1353 // C++ [temp.param]p12:
1354 // A template-parameter shall not be given default arguments
1355 // by two different declarations in the same scope.
1356 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1357 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1358 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001359 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001360 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001361 // If a template-parameter of a class template has a default
1362 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001363 // have a default template-argument supplied or be a template parameter
1364 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001365 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001366 diag::err_template_param_default_arg_missing);
1367 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1368 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001369 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001370 }
1371
1372 // If we have an old template parameter list that we're merging
1373 // in, move on to the next parameter.
1374 if (OldParams)
1375 ++OldParam;
1376 }
1377
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001378 // We were missing some default arguments at the end of the list, so remove
1379 // all of the default arguments.
1380 if (RemoveDefaultArguments) {
1381 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1382 NewParamEnd = NewParams->end();
1383 NewParam != NewParamEnd; ++NewParam) {
1384 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1385 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001386 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001387 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1388 NTTP->removeDefaultArgument();
1389 else
1390 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1391 }
1392 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001393
Douglas Gregord684b002009-02-10 19:49:53 +00001394 return Invalid;
1395}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001396
John McCall4e2cbb22010-10-20 05:44:58 +00001397namespace {
1398
1399/// A class which looks for a use of a certain level of template
1400/// parameter.
1401struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1402 typedef RecursiveASTVisitor<DependencyChecker> super;
1403
1404 unsigned Depth;
1405 bool Match;
1406
1407 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1408 NamedDecl *ND = Params->getParam(0);
1409 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1410 Depth = PD->getDepth();
1411 } else if (NonTypeTemplateParmDecl *PD =
1412 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1413 Depth = PD->getDepth();
1414 } else {
1415 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1416 }
1417 }
1418
1419 bool Matches(unsigned ParmDepth) {
1420 if (ParmDepth >= Depth) {
1421 Match = true;
1422 return true;
1423 }
1424 return false;
1425 }
1426
1427 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1428 return !Matches(T->getDepth());
1429 }
1430
1431 bool TraverseTemplateName(TemplateName N) {
1432 if (TemplateTemplateParmDecl *PD =
1433 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1434 if (Matches(PD->getDepth())) return false;
1435 return super::TraverseTemplateName(N);
1436 }
1437
1438 bool VisitDeclRefExpr(DeclRefExpr *E) {
1439 if (NonTypeTemplateParmDecl *PD =
1440 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1441 if (PD->getDepth() == Depth) {
1442 Match = true;
1443 return false;
1444 }
1445 }
1446 return super::VisitDeclRefExpr(E);
1447 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001448
1449 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1450 return TraverseType(T->getInjectedSpecializationType());
1451 }
John McCall4e2cbb22010-10-20 05:44:58 +00001452};
1453}
1454
Douglas Gregorc8406492011-05-10 18:27:06 +00001455/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001456/// list.
1457static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001458DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001459 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001460 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001461 return Checker.Match;
1462}
1463
Douglas Gregorc8406492011-05-10 18:27:06 +00001464// Find the source range corresponding to the named type in the given
1465// nested-name-specifier, if any.
1466static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1467 QualType T,
1468 const CXXScopeSpec &SS) {
1469 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1470 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1471 if (const Type *CurType = NNS->getAsType()) {
1472 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1473 return NNSLoc.getTypeLoc().getSourceRange();
1474 } else
1475 break;
1476
1477 NNSLoc = NNSLoc.getPrefix();
1478 }
1479
1480 return SourceRange();
1481}
1482
Mike Stump1eb44332009-09-09 15:08:12 +00001483/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001484/// specifier, returning the template parameter list that applies to the
1485/// name.
1486///
1487/// \param DeclStartLoc the start of the declaration that has a scope
1488/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001489///
Douglas Gregorc8406492011-05-10 18:27:06 +00001490/// \param DeclLoc The location of the declaration itself.
1491///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001492/// \param SS the scope specifier that will be matched to the given template
1493/// parameter lists. This scope specifier precedes a qualified name that is
1494/// being declared.
1495///
1496/// \param ParamLists the template parameter lists, from the outermost to the
1497/// innermost template parameter lists.
1498///
1499/// \param NumParamLists the number of template parameter lists in ParamLists.
1500///
John McCall77e8b112010-04-13 20:37:33 +00001501/// \param IsFriend Whether to apply the slightly different rules for
1502/// matching template parameters to scope specifiers in friend
1503/// declarations.
1504///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001505/// \param IsExplicitSpecialization will be set true if the entity being
1506/// declared is an explicit specialization, false otherwise.
1507///
Mike Stump1eb44332009-09-09 15:08:12 +00001508/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001509/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001510/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001511/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001512/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001513/// itself a template).
1514TemplateParameterList *
1515Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001516 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517 const CXXScopeSpec &SS,
1518 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001519 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001520 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001521 bool &IsExplicitSpecialization,
1522 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001523 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001524 Invalid = false;
1525
1526 // The sequence of nested types to which we will match up the template
1527 // parameter lists. We first build this list by starting with the type named
1528 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001529 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001530 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001531 if (SS.getScopeRep()) {
1532 if (CXXRecordDecl *Record
1533 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1534 T = Context.getTypeDeclType(Record);
1535 else
1536 T = QualType(SS.getScopeRep()->getAsType(), 0);
1537 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001538
1539 // If we found an explicit specialization that prevents us from needing
1540 // 'template<>' headers, this will be set to the location of that
1541 // explicit specialization.
1542 SourceLocation ExplicitSpecLoc;
1543
1544 while (!T.isNull()) {
1545 NestedTypes.push_back(T);
1546
1547 // Retrieve the parent of a record type.
1548 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1549 // If this type is an explicit specialization, we're done.
1550 if (ClassTemplateSpecializationDecl *Spec
1551 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1552 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1553 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1554 ExplicitSpecLoc = Spec->getLocation();
1555 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001556 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001557 } else if (Record->getTemplateSpecializationKind()
1558 == TSK_ExplicitSpecialization) {
1559 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001560 break;
1561 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001562
1563 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1564 T = Context.getTypeDeclType(Parent);
1565 else
1566 T = QualType();
1567 continue;
1568 }
1569
1570 if (const TemplateSpecializationType *TST
1571 = T->getAs<TemplateSpecializationType>()) {
1572 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1573 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1574 T = Context.getTypeDeclType(Parent);
1575 else
1576 T = QualType();
1577 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001578 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001579 }
1580
1581 // Look one step prior in a dependent template specialization type.
1582 if (const DependentTemplateSpecializationType *DependentTST
1583 = T->getAs<DependentTemplateSpecializationType>()) {
1584 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1585 T = QualType(NNS->getAsType(), 0);
1586 else
1587 T = QualType();
1588 continue;
1589 }
1590
1591 // Look one step prior in a dependent name type.
1592 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1593 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1594 T = QualType(NNS->getAsType(), 0);
1595 else
1596 T = QualType();
1597 continue;
1598 }
1599
1600 // Retrieve the parent of an enumeration type.
1601 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1602 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1603 // check here.
1604 EnumDecl *Enum = EnumT->getDecl();
1605
1606 // Get to the parent type.
1607 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1608 T = Context.getTypeDeclType(Parent);
1609 else
1610 T = QualType();
1611 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregorc8406492011-05-10 18:27:06 +00001614 T = QualType();
1615 }
1616 // Reverse the nested types list, since we want to traverse from the outermost
1617 // to the innermost while checking template-parameter-lists.
1618 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001619
Douglas Gregorc8406492011-05-10 18:27:06 +00001620 // C++0x [temp.expl.spec]p17:
1621 // A member or a member template may be nested within many
1622 // enclosing class templates. In an explicit specialization for
1623 // such a member, the member declaration shall be preceded by a
1624 // template<> for each enclosing class template that is
1625 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001626 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001627 unsigned ParamIdx = 0;
1628 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1629 ++TypeIdx) {
1630 T = NestedTypes[TypeIdx];
1631
1632 // Whether we expect a 'template<>' header.
1633 bool NeedEmptyTemplateHeader = false;
1634
1635 // Whether we expect a template header with parameters.
1636 bool NeedNonemptyTemplateHeader = false;
1637
1638 // For a dependent type, the set of template parameters that we
1639 // expect to see.
1640 TemplateParameterList *ExpectedTemplateParams = 0;
1641
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001642 // C++0x [temp.expl.spec]p15:
1643 // A member or a member template may be nested within many enclosing
1644 // class templates. In an explicit specialization for such a member, the
1645 // member declaration shall be preceded by a template<> for each
1646 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001647 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1648 if (ClassTemplatePartialSpecializationDecl *Partial
1649 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1650 ExpectedTemplateParams = Partial->getTemplateParameters();
1651 NeedNonemptyTemplateHeader = true;
1652 } else if (Record->isDependentType()) {
1653 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001654 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001655 ->getTemplateParameters();
1656 NeedNonemptyTemplateHeader = true;
1657 }
1658 } else if (ClassTemplateSpecializationDecl *Spec
1659 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1660 // C++0x [temp.expl.spec]p4:
1661 // Members of an explicitly specialized class template are defined
1662 // in the same manner as members of normal classes, and not using
1663 // the template<> syntax.
1664 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1665 NeedEmptyTemplateHeader = true;
1666 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001667 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001668 } else if (Record->getTemplateSpecializationKind()) {
1669 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001670 != TSK_ExplicitSpecialization &&
1671 TypeIdx == NumTypes - 1)
1672 IsExplicitSpecialization = true;
1673
1674 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001675 }
1676 } else if (const TemplateSpecializationType *TST
1677 = T->getAs<TemplateSpecializationType>()) {
1678 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1679 ExpectedTemplateParams = Template->getTemplateParameters();
1680 NeedNonemptyTemplateHeader = true;
1681 }
1682 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1683 // FIXME: We actually could/should check the template arguments here
1684 // against the corresponding template parameter list.
1685 NeedNonemptyTemplateHeader = false;
1686 }
1687
Douglas Gregor89b9f102011-06-06 15:22:55 +00001688 // C++ [temp.expl.spec]p16:
1689 // In an explicit specialization declaration for a member of a class
1690 // template or a member template that ap- pears in namespace scope, the
1691 // member template and some of its enclosing class templates may remain
1692 // unspecialized, except that the declaration shall not explicitly
1693 // specialize a class member template if its en- closing class templates
1694 // are not explicitly specialized as well.
1695 if (ParamIdx < NumParamLists) {
1696 if (ParamLists[ParamIdx]->size() == 0) {
1697 if (SawNonEmptyTemplateParameterList) {
1698 Diag(DeclLoc, diag::err_specialize_member_of_template)
1699 << ParamLists[ParamIdx]->getSourceRange();
1700 Invalid = true;
1701 IsExplicitSpecialization = false;
1702 return 0;
1703 }
1704 } else
1705 SawNonEmptyTemplateParameterList = true;
1706 }
1707
Douglas Gregorc8406492011-05-10 18:27:06 +00001708 if (NeedEmptyTemplateHeader) {
1709 // If we're on the last of the types, and we need a 'template<>' header
1710 // here, then it's an explicit specialization.
1711 if (TypeIdx == NumTypes - 1)
1712 IsExplicitSpecialization = true;
1713
1714 if (ParamIdx < NumParamLists) {
1715 if (ParamLists[ParamIdx]->size() > 0) {
1716 // The header has template parameters when it shouldn't. Complain.
1717 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1718 diag::err_template_param_list_matches_nontemplate)
1719 << T
1720 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1721 ParamLists[ParamIdx]->getRAngleLoc())
1722 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1723 Invalid = true;
1724 return 0;
1725 }
1726
1727 // Consume this template header.
1728 ++ParamIdx;
1729 continue;
1730 }
1731
1732 if (!IsFriend) {
1733 // We don't have a template header, but we should.
1734 SourceLocation ExpectedTemplateLoc;
1735 if (NumParamLists > 0)
1736 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1737 else
1738 ExpectedTemplateLoc = DeclStartLoc;
1739
1740 Diag(DeclLoc, diag::err_template_spec_needs_header)
1741 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1742 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1743 }
1744
1745 continue;
1746 }
1747
1748 if (NeedNonemptyTemplateHeader) {
1749 // In friend declarations we can have template-ids which don't
1750 // depend on the corresponding template parameter lists. But
1751 // assume that empty parameter lists are supposed to match this
1752 // template-id.
1753 if (IsFriend && T->isDependentType()) {
1754 if (ParamIdx < NumParamLists &&
1755 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1756 ExpectedTemplateParams = 0;
1757 else
1758 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001759 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001760
Douglas Gregorc8406492011-05-10 18:27:06 +00001761 if (ParamIdx < NumParamLists) {
1762 // Check the template parameter list, if we can.
1763 if (ExpectedTemplateParams &&
1764 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1765 ExpectedTemplateParams,
1766 true, TPL_TemplateMatch))
1767 Invalid = true;
1768
1769 if (!Invalid &&
1770 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1771 TPC_ClassTemplateMember))
1772 Invalid = true;
1773
1774 ++ParamIdx;
1775 continue;
1776 }
1777
1778 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1779 << T
1780 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1781 Invalid = true;
1782 continue;
1783 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001784 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001785
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001786 // If there were at least as many template-ids as there were template
1787 // parameter lists, then there are no template parameter lists remaining for
1788 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001789 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001790 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001792 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001793 if (ParamIdx < NumParamLists - 1) {
1794 bool HasAnyExplicitSpecHeader = false;
1795 bool AllExplicitSpecHeaders = true;
1796 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1797 if (ParamLists[I]->size() == 0)
1798 HasAnyExplicitSpecHeader = true;
1799 else
1800 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001801 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001802
1803 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1804 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1805 : diag::err_template_spec_extra_headers)
1806 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1807 ParamLists[NumParamLists - 2]->getRAngleLoc());
1808
1809 // If there was a specialization somewhere, such that 'template<>' is
1810 // not required, and there were any 'template<>' headers, note where the
1811 // specialization occurred.
1812 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1813 Diag(ExplicitSpecLoc,
1814 diag::note_explicit_template_spec_does_not_need_header)
1815 << NestedTypes.back();
1816
1817 // We have a template parameter list with no corresponding scope, which
1818 // means that the resulting template declaration can't be instantiated
1819 // properly (we'll end up with dependent nodes when we shouldn't).
1820 if (!AllExplicitSpecHeaders)
1821 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Douglas Gregor89b9f102011-06-06 15:22:55 +00001824 // C++ [temp.expl.spec]p16:
1825 // In an explicit specialization declaration for a member of a class
1826 // template or a member template that ap- pears in namespace scope, the
1827 // member template and some of its enclosing class templates may remain
1828 // unspecialized, except that the declaration shall not explicitly
1829 // specialize a class member template if its en- closing class templates
1830 // are not explicitly specialized as well.
1831 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1832 SawNonEmptyTemplateParameterList) {
1833 Diag(DeclLoc, diag::err_specialize_member_of_template)
1834 << ParamLists[ParamIdx]->getSourceRange();
1835 Invalid = true;
1836 IsExplicitSpecialization = false;
1837 return 0;
1838 }
1839
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001840 // Return the last template parameter list, which corresponds to the
1841 // entity being declared.
1842 return ParamLists[NumParamLists - 1];
1843}
1844
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001845void Sema::NoteAllFoundTemplates(TemplateName Name) {
1846 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1847 Diag(Template->getLocation(), diag::note_template_declared_here)
1848 << (isa<FunctionTemplateDecl>(Template)? 0
1849 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001850 : isa<TypeAliasTemplateDecl>(Template)? 2
1851 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001852 << Template->getDeclName();
1853 return;
1854 }
1855
1856 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1857 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1858 IEnd = OST->end();
1859 I != IEnd; ++I)
1860 Diag((*I)->getLocation(), diag::note_template_declared_here)
1861 << 0 << (*I)->getDeclName();
1862
1863 return;
1864 }
1865}
1866
1867
Douglas Gregor7532dc62009-03-30 22:58:21 +00001868QualType Sema::CheckTemplateIdType(TemplateName Name,
1869 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001870 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001871 DependentTemplateName *DTN
1872 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001873 if (DTN && DTN->isIdentifier())
1874 // When building a template-id where the template-name is dependent,
1875 // assume the template is a type template. Either our assumption is
1876 // correct, or the code is ill-formed and will be diagnosed when the
1877 // dependent name is substituted.
1878 return Context.getDependentTemplateSpecializationType(ETK_None,
1879 DTN->getQualifier(),
1880 DTN->getIdentifier(),
1881 TemplateArgs);
1882
Douglas Gregor7532dc62009-03-30 22:58:21 +00001883 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001884 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1885 // We might have a substituted template template parameter pack. If so,
1886 // build a template specialization type for it.
1887 if (Name.getAsSubstTemplateTemplateParmPack())
1888 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001889
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001890 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1891 << Name;
1892 NoteAllFoundTemplates(Name);
1893 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001894 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001895
Douglas Gregor40808ce2009-03-09 23:48:35 +00001896 // Check that the template argument list is well-formed for this
1897 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001898 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001899 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001900 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001901 return QualType();
1902
Douglas Gregor910f8002010-11-07 23:05:16 +00001903 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001904 "Converted template argument list is too short!");
1905
1906 QualType CanonType;
1907
Douglas Gregor561f8122011-07-01 01:22:09 +00001908 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001909 if (TypeAliasTemplateDecl *AliasTemplate
1910 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1911 // Find the canonical type for this type alias template specialization.
1912 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1913 if (Pattern->isInvalidDecl())
1914 return QualType();
1915
1916 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1917 Converted.data(), Converted.size());
1918
1919 // Only substitute for the innermost template argument list.
1920 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001921 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001922 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1923 for (unsigned I = 0; I < Depth; ++I)
1924 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001925
1926 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1927 CanonType = SubstType(Pattern->getUnderlyingType(),
1928 TemplateArgLists, AliasTemplate->getLocation(),
1929 AliasTemplate->getDeclName());
1930 if (CanonType.isNull())
1931 return QualType();
1932 } else if (Name.isDependent() ||
1933 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001934 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001935 // This class template specialization is a dependent
1936 // type. Therefore, its canonical type is another class template
1937 // specialization type that contains all of the converted
1938 // arguments in canonical form. This ensures that, e.g., A<T> and
1939 // A<T, T> have identical types when A is declared as:
1940 //
1941 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001942 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001943 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001944 Converted.data(),
1945 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Douglas Gregor1275ae02009-07-28 23:00:59 +00001947 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001948 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001949 // In the future, we need to teach getTemplateSpecializationType to only
1950 // build the canonical type and return that to us.
1951 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001952
1953 // This might work out to be a current instantiation, in which
1954 // case the canonical type needs to be the InjectedClassNameType.
1955 //
1956 // TODO: in theory this could be a simple hashtable lookup; most
1957 // changes to CurContext don't change the set of current
1958 // instantiations.
1959 if (isa<ClassTemplateDecl>(Template)) {
1960 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1961 // If we get out to a namespace, we're done.
1962 if (Ctx->isFileContext()) break;
1963
1964 // If this isn't a record, keep looking.
1965 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1966 if (!Record) continue;
1967
1968 // Look for one of the two cases with InjectedClassNameTypes
1969 // and check whether it's the same template.
1970 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1971 !Record->getDescribedClassTemplate())
1972 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001973
John McCall31f17ec2010-04-27 00:57:59 +00001974 // Fetch the injected class name type and check whether its
1975 // injected type is equal to the type we just built.
1976 QualType ICNT = Context.getTypeDeclType(Record);
1977 QualType Injected = cast<InjectedClassNameType>(ICNT)
1978 ->getInjectedSpecializationType();
1979
1980 if (CanonType != Injected->getCanonicalTypeInternal())
1981 continue;
1982
1983 // If so, the canonical type of this TST is the injected
1984 // class name type of the record we just found.
1985 assert(ICNT.isCanonical());
1986 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001987 break;
1988 }
1989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001991 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001992 // Find the class template specialization declaration that
1993 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001994 void *InsertPos = 0;
1995 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001996 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001997 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001998 if (!Decl) {
1999 // This is the first time we have referenced this class template
2000 // specialization. Create the canonical declaration and add it to
2001 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002002 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002003 ClassTemplate->getTemplatedDecl()->getTagKind(),
2004 ClassTemplate->getDeclContext(),
2005 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002006 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002007 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002008 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002009 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002010 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002011 Decl->setLexicalDeclContext(CurContext);
2012 }
2013
2014 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002015 assert(isa<RecordType>(CanonType) &&
2016 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002017 }
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor40808ce2009-03-09 23:48:35 +00002019 // Build the fully-sugared type for this class template
2020 // specialization, which refers back to the class template
2021 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002022 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002023}
2024
John McCallf312b1e2010-08-26 23:41:50 +00002025TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002026Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2027 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002028 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002029 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002030 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002031 if (SS.isInvalid())
2032 return true;
2033
Douglas Gregor7532dc62009-03-30 22:58:21 +00002034 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002035
Douglas Gregor40808ce2009-03-09 23:48:35 +00002036 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002037 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002038 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002039
Douglas Gregora88f09f2011-02-28 17:23:35 +00002040 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2041 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2042 DTN->getQualifier(),
2043 DTN->getIdentifier(),
2044 TemplateArgs);
2045
2046 // Build type-source information.
2047 TypeLocBuilder TLB;
2048 DependentTemplateSpecializationTypeLoc SpecTL
2049 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002050 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002051 SpecTL.setNameLoc(TemplateLoc);
2052 SpecTL.setLAngleLoc(LAngleLoc);
2053 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002054 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002055 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2056 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2057 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2058 }
2059
John McCalld5532b62009-11-23 01:53:49 +00002060 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002061 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002062
2063 if (Result.isNull())
2064 return true;
2065
Douglas Gregor059101f2011-03-02 00:47:37 +00002066 // Build type-source information.
2067 TypeLocBuilder TLB;
2068 TemplateSpecializationTypeLoc SpecTL
2069 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2070 SpecTL.setTemplateNameLoc(TemplateLoc);
2071 SpecTL.setLAngleLoc(LAngleLoc);
2072 SpecTL.setRAngleLoc(RAngleLoc);
2073 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2074 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002075
Douglas Gregor059101f2011-03-02 00:47:37 +00002076 if (SS.isNotEmpty()) {
2077 // Create an elaborated-type-specifier containing the nested-name-specifier.
2078 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2079 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2080 ElabTL.setKeywordLoc(SourceLocation());
2081 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2082 }
2083
2084 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002085}
John McCallf1bbbb42009-09-04 01:14:41 +00002086
Douglas Gregor059101f2011-03-02 00:47:37 +00002087TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002088 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002089 SourceLocation TagLoc,
2090 CXXScopeSpec &SS,
2091 TemplateTy TemplateD,
2092 SourceLocation TemplateLoc,
2093 SourceLocation LAngleLoc,
2094 ASTTemplateArgsPtr TemplateArgsIn,
2095 SourceLocation RAngleLoc) {
2096 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2097
2098 // Translate the parser's template argument list in our AST format.
2099 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2100 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2101
2102 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002103 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002104 ElaboratedTypeKeyword Keyword
2105 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Douglas Gregor059101f2011-03-02 00:47:37 +00002107 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2108 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2109 DTN->getQualifier(),
2110 DTN->getIdentifier(),
2111 TemplateArgs);
2112
2113 // Build type-source information.
2114 TypeLocBuilder TLB;
2115 DependentTemplateSpecializationTypeLoc SpecTL
2116 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2117 SpecTL.setKeywordLoc(TagLoc);
2118 SpecTL.setNameLoc(TemplateLoc);
2119 SpecTL.setLAngleLoc(LAngleLoc);
2120 SpecTL.setRAngleLoc(RAngleLoc);
2121 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2122 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2123 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2124 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2125 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002126
2127 if (TypeAliasTemplateDecl *TAT =
2128 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2129 // C++0x [dcl.type.elab]p2:
2130 // If the identifier resolves to a typedef-name or the simple-template-id
2131 // resolves to an alias template specialization, the
2132 // elaborated-type-specifier is ill-formed.
2133 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2134 Diag(TAT->getLocation(), diag::note_declared_at);
2135 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002136
2137 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2138 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002139 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002140
2141 // Check the tag kind
2142 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002143 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002144
John McCall6b2becf2009-09-08 17:47:29 +00002145 IdentifierInfo *Id = D->getIdentifier();
2146 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002147
Richard Trieubbf34c02011-06-10 03:11:26 +00002148 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2149 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002150 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002151 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002152 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002153 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002154 }
2155 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002156
2157 // Provide source-location information for the template specialization.
2158 TypeLocBuilder TLB;
2159 TemplateSpecializationTypeLoc SpecTL
2160 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2161 SpecTL.setTemplateNameLoc(TemplateLoc);
2162 SpecTL.setLAngleLoc(LAngleLoc);
2163 SpecTL.setRAngleLoc(RAngleLoc);
2164 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2165 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002166
Douglas Gregor059101f2011-03-02 00:47:37 +00002167 // Construct an elaborated type containing the nested-name-specifier (if any)
2168 // and keyword.
2169 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2170 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2171 ElabTL.setKeywordLoc(TagLoc);
2172 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2173 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002174}
2175
John McCall60d7b3a2010-08-24 06:29:42 +00002176ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002177 LookupResult &R,
2178 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002179 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002180 // FIXME: Can we do any checking at this point? I guess we could check the
2181 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002182 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002183 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002184 // foo<int> could identify a single function unambiguously
2185 // This approach does NOT work, since f<int>(1);
2186 // gets resolved prior to resorting to overload resolution
2187 // i.e., template<class T> void f(double);
2188 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002189
2190 // These should be filtered out by our callers.
2191 assert(!R.empty() && "empty lookup results when building templateid");
2192 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2193
John McCallc373d482010-01-27 01:50:18 +00002194 // We don't want lookup warnings at this point.
2195 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002196
John McCallf7a1a742009-11-24 19:00:30 +00002197 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002198 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002199 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002200 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002201 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002202 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002203
2204 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002205}
2206
John McCallf7a1a742009-11-24 19:00:30 +00002207// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002208ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002209Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002210 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002211 const TemplateArgumentListInfo &TemplateArgs) {
2212 DeclContext *DC;
2213 if (!(DC = computeDeclContext(SS, false)) ||
2214 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002215 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002216 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002218 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002219 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002220 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2221 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002222
John McCallf7a1a742009-11-24 19:00:30 +00002223 if (R.isAmbiguous())
2224 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002225
John McCallf7a1a742009-11-24 19:00:30 +00002226 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002227 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2228 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002229 return ExprError();
2230 }
2231
2232 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002233 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2234 << (NestedNameSpecifier*) SS.getScopeRep()
2235 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002236 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2237 return ExprError();
2238 }
2239
2240 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002241}
2242
Douglas Gregorc45c2322009-03-31 00:43:58 +00002243/// \brief Form a dependent template name.
2244///
2245/// This action forms a dependent template name given the template
2246/// name and its (presumably dependent) scope specifier. For
2247/// example, given "MetaFun::template apply", the scope specifier \p
2248/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2249/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002250TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002251 SourceLocation TemplateKWLoc,
2252 CXXScopeSpec &SS,
2253 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002254 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002255 bool EnteringContext,
2256 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002257 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2258 !getLangOptions().CPlusPlus0x)
2259 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002260 << FixItHint::CreateRemoval(TemplateKWLoc);
2261
Douglas Gregor0707bc52010-01-19 16:01:07 +00002262 DeclContext *LookupCtx = 0;
2263 if (SS.isSet())
2264 LookupCtx = computeDeclContext(SS, EnteringContext);
2265 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002266 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002267 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002268 // C++0x [temp.names]p5:
2269 // If a name prefixed by the keyword template is not the name of
2270 // a template, the program is ill-formed. [Note: the keyword
2271 // template may not be applied to non-template members of class
2272 // templates. -end note ] [ Note: as is the case with the
2273 // typename prefix, the template prefix is allowed in cases
2274 // where it is not strictly necessary; i.e., when the
2275 // nested-name-specifier or the expression on the left of the ->
2276 // or . is not dependent on a template-parameter, or the use
2277 // does not appear in the scope of a template. -end note]
2278 //
2279 // Note: C++03 was more strict here, because it banned the use of
2280 // the "template" keyword prior to a template-name that was not a
2281 // dependent name. C++ DR468 relaxed this requirement (the
2282 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002283 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002284 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002285 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2286 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002287 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002288 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2289 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002290 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2291 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002292 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002293 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002294 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002295 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002296 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002297 << Name.getSourceRange()
2298 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002299 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002300 } else {
2301 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002302 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002303 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002304 }
2305
Mike Stump1eb44332009-09-09 15:08:12 +00002306 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002307 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002308
Douglas Gregor014e88d2009-11-03 23:16:33 +00002309 switch (Name.getKind()) {
2310 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002311 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002312 Name.Identifier));
2313 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002314
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002315 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002316 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002317 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002318 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002319
2320 case UnqualifiedId::IK_LiteralOperatorId:
2321 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2322
Douglas Gregor014e88d2009-11-03 23:16:33 +00002323 default:
2324 break;
2325 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002326
2327 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002328 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002329 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002330 << Name.getSourceRange()
2331 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002332 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002333}
2334
Mike Stump1eb44332009-09-09 15:08:12 +00002335bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002336 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002338 const TemplateArgument &Arg = AL.getArgument();
2339
Anders Carlsson436b1562009-06-13 00:33:33 +00002340 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002341 switch(Arg.getKind()) {
2342 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002343 // C++ [temp.arg.type]p1:
2344 // A template-argument for a template-parameter which is a
2345 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002346 break;
2347 case TemplateArgument::Template: {
2348 // We have a template type parameter but the template argument
2349 // is a template without any arguments.
2350 SourceRange SR = AL.getSourceRange();
2351 TemplateName Name = Arg.getAsTemplate();
2352 Diag(SR.getBegin(), diag::err_template_missing_args)
2353 << Name << SR;
2354 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2355 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002356
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002357 return true;
2358 }
2359 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002360 // We have a template type parameter but the template argument
2361 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002362 SourceRange SR = AL.getSourceRange();
2363 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002364 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Anders Carlsson436b1562009-06-13 00:33:33 +00002366 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002367 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002368 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002369
John McCalla93c9342009-12-07 02:54:59 +00002370 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002371 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Anders Carlsson436b1562009-06-13 00:33:33 +00002373 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002374 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2375
2376 // Objective-C ARC:
2377 // If an explicitly-specified template argument type is a lifetime type
2378 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2379 if (getLangOptions().ObjCAutoRefCount &&
2380 ArgType->isObjCLifetimeType() &&
2381 !ArgType.getObjCLifetime()) {
2382 Qualifiers Qs;
2383 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2384 ArgType = Context.getQualifiedType(ArgType, Qs);
2385 }
2386
2387 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002388 return false;
2389}
2390
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002391/// \brief Substitute template arguments into the default template argument for
2392/// the given template type parameter.
2393///
2394/// \param SemaRef the semantic analysis object for which we are performing
2395/// the substitution.
2396///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002397/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002398/// for.
2399///
2400/// \param TemplateLoc the location of the template name that started the
2401/// template-id we are checking.
2402///
2403/// \param RAngleLoc the location of the right angle bracket ('>') that
2404/// terminates the template-id.
2405///
2406/// \param Param the template template parameter whose default we are
2407/// substituting into.
2408///
2409/// \param Converted the list of template arguments provided for template
2410/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002411/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002412static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002413SubstDefaultTemplateArgument(Sema &SemaRef,
2414 TemplateDecl *Template,
2415 SourceLocation TemplateLoc,
2416 SourceLocation RAngleLoc,
2417 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002418 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002419 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002420
2421 // If the argument type is dependent, instantiate it now based
2422 // on the previously-computed template arguments.
2423 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002425 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002426
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002427 MultiLevelTemplateArgumentList AllTemplateArgs
2428 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2429
2430 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002431 Template, Converted.data(),
2432 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002433 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002434
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002435 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2436 Param->getDefaultArgumentLoc(),
2437 Param->getDeclName());
2438 }
2439
2440 return ArgType;
2441}
2442
2443/// \brief Substitute template arguments into the default template argument for
2444/// the given non-type template parameter.
2445///
2446/// \param SemaRef the semantic analysis object for which we are performing
2447/// the substitution.
2448///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002449/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002450/// for.
2451///
2452/// \param TemplateLoc the location of the template name that started the
2453/// template-id we are checking.
2454///
2455/// \param RAngleLoc the location of the right angle bracket ('>') that
2456/// terminates the template-id.
2457///
Douglas Gregor788cd062009-11-11 01:00:40 +00002458/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002459/// substituting into.
2460///
2461/// \param Converted the list of template arguments provided for template
2462/// parameters that precede \p Param in the template parameter list.
2463///
2464/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002465static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002466SubstDefaultTemplateArgument(Sema &SemaRef,
2467 TemplateDecl *Template,
2468 SourceLocation TemplateLoc,
2469 SourceLocation RAngleLoc,
2470 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002471 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002473 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002474
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002475 MultiLevelTemplateArgumentList AllTemplateArgs
2476 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002477
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002478 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002479 Template, Converted.data(),
2480 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002481 SourceRange(TemplateLoc, RAngleLoc));
2482
2483 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2484}
2485
Douglas Gregor788cd062009-11-11 01:00:40 +00002486/// \brief Substitute template arguments into the default template argument for
2487/// the given template template parameter.
2488///
2489/// \param SemaRef the semantic analysis object for which we are performing
2490/// the substitution.
2491///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002492/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002493/// for.
2494///
2495/// \param TemplateLoc the location of the template name that started the
2496/// template-id we are checking.
2497///
2498/// \param RAngleLoc the location of the right angle bracket ('>') that
2499/// terminates the template-id.
2500///
2501/// \param Param the template template parameter whose default we are
2502/// substituting into.
2503///
2504/// \param Converted the list of template arguments provided for template
2505/// parameters that precede \p Param in the template parameter list.
2506///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002507/// \param QualifierLoc Will be set to the nested-name-specifier (with
2508/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002509///
Douglas Gregor788cd062009-11-11 01:00:40 +00002510/// \returns the substituted template argument, or NULL if an error occurred.
2511static TemplateName
2512SubstDefaultTemplateArgument(Sema &SemaRef,
2513 TemplateDecl *Template,
2514 SourceLocation TemplateLoc,
2515 SourceLocation RAngleLoc,
2516 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002517 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002518 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002519 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002520 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002521
Douglas Gregor788cd062009-11-11 01:00:40 +00002522 MultiLevelTemplateArgumentList AllTemplateArgs
2523 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002524
Douglas Gregor788cd062009-11-11 01:00:40 +00002525 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002526 Template, Converted.data(),
2527 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002528 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002529
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002530 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002531 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002532 if (QualifierLoc) {
2533 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2534 AllTemplateArgs);
2535 if (!QualifierLoc)
2536 return TemplateName();
2537 }
2538
Douglas Gregor1d752d72011-03-02 18:46:51 +00002539 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002540 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002541 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002542 AllTemplateArgs);
2543}
2544
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002545/// \brief If the given template parameter has a default template
2546/// argument, substitute into that default template argument and
2547/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002548TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002549Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2550 SourceLocation TemplateLoc,
2551 SourceLocation RAngleLoc,
2552 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002553 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002554 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002555 if (!TypeParm->hasDefaultArgument())
2556 return TemplateArgumentLoc();
2557
John McCalla93c9342009-12-07 02:54:59 +00002558 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002559 TemplateLoc,
2560 RAngleLoc,
2561 TypeParm,
2562 Converted);
2563 if (DI)
2564 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2565
2566 return TemplateArgumentLoc();
2567 }
2568
2569 if (NonTypeTemplateParmDecl *NonTypeParm
2570 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2571 if (!NonTypeParm->hasDefaultArgument())
2572 return TemplateArgumentLoc();
2573
John McCall60d7b3a2010-08-24 06:29:42 +00002574 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002575 TemplateLoc,
2576 RAngleLoc,
2577 NonTypeParm,
2578 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002579 if (Arg.isInvalid())
2580 return TemplateArgumentLoc();
2581
2582 Expr *ArgE = Arg.takeAs<Expr>();
2583 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2584 }
2585
2586 TemplateTemplateParmDecl *TempTempParm
2587 = cast<TemplateTemplateParmDecl>(Param);
2588 if (!TempTempParm->hasDefaultArgument())
2589 return TemplateArgumentLoc();
2590
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002591
Douglas Gregor1d752d72011-03-02 18:46:51 +00002592 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002593 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002594 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002595 RAngleLoc,
2596 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002597 Converted,
2598 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002599 if (TName.isNull())
2600 return TemplateArgumentLoc();
2601
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002602 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002603 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002604 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2605}
2606
Douglas Gregore7526412009-11-11 19:31:23 +00002607/// \brief Check that the given template argument corresponds to the given
2608/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002609///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002610/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002611/// checked.
2612///
2613/// \param Arg The template argument.
2614///
2615/// \param Template The template in which the template argument resides.
2616///
2617/// \param TemplateLoc The location of the template name for the template
2618/// whose argument list we're matching.
2619///
2620/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2621/// the template argument list.
2622///
2623/// \param ArgumentPackIndex The index into the argument pack where this
2624/// argument will be placed. Only valid if the parameter is a parameter pack.
2625///
2626/// \param Converted The checked, converted argument will be added to the
2627/// end of this small vector.
2628///
2629/// \param CTAK Describes how we arrived at this particular template argument:
2630/// explicitly written, deduced, etc.
2631///
2632/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002633bool Sema::CheckTemplateArgument(NamedDecl *Param,
2634 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002635 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002636 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002637 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002638 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002639 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002640 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002641 // Check template type parameters.
2642 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002643 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644
Douglas Gregord9e15302009-11-11 19:41:09 +00002645 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002646 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002647 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002648 // with the template arguments we've seen thus far. But if the
2649 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002650 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002651 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2652 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002654 if (NTTPType->isDependentType() &&
2655 !isa<TemplateTemplateParmDecl>(Template) &&
2656 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002657 // Do substitution on the type of the non-type template parameter.
2658 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002659 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002660 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661
2662 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002663 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002664 NTTPType = SubstType(NTTPType,
2665 MultiLevelTemplateArgumentList(TemplateArgs),
2666 NTTP->getLocation(),
2667 NTTP->getDeclName());
2668 // If that worked, check the non-type template parameter type
2669 // for validity.
2670 if (!NTTPType.isNull())
2671 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2672 NTTP->getLocation());
2673 if (NTTPType.isNull())
2674 return true;
2675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
Douglas Gregore7526412009-11-11 19:31:23 +00002677 switch (Arg.getArgument().getKind()) {
2678 case TemplateArgument::Null:
2679 assert(false && "Should never see a NULL template argument here");
2680 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002681
Douglas Gregore7526412009-11-11 19:31:23 +00002682 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002683 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002684 ExprResult Res =
2685 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2686 Result, CTAK);
2687 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002688 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002689
Douglas Gregor910f8002010-11-07 23:05:16 +00002690 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002691 break;
2692 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002693
Douglas Gregore7526412009-11-11 19:31:23 +00002694 case TemplateArgument::Declaration:
2695 case TemplateArgument::Integral:
2696 // We've already checked this template argument, so just copy
2697 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002698 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002699 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002700
Douglas Gregore7526412009-11-11 19:31:23 +00002701 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002702 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002703 // We were given a template template argument. It may not be ill-formed;
2704 // see below.
2705 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002706 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2707 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002708 // We have a template argument such as \c T::template X, which we
2709 // parsed as a template template argument. However, since we now
2710 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002711 // template name into an expression.
2712
2713 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2714 Arg.getTemplateNameLoc());
2715
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002716 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002717 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002718 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002719 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002720 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002721
Douglas Gregora7fc9012011-01-05 18:58:31 +00002722 // If we parsed the template argument as a pack expansion, create a
2723 // pack expansion expression.
2724 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002725 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2726 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002727 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002728 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729
Douglas Gregore7526412009-11-11 19:31:23 +00002730 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002731 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2732 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002733 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregor910f8002010-11-07 23:05:16 +00002735 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002736 break;
2737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregore7526412009-11-11 19:31:23 +00002739 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002740 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002741 // therefore cannot be a non-type template argument.
2742 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2743 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregore7526412009-11-11 19:31:23 +00002745 Diag(Param->getLocation(), diag::note_template_param_here);
2746 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747
Douglas Gregore7526412009-11-11 19:31:23 +00002748 case TemplateArgument::Type: {
2749 // We have a non-type template parameter but the template
2750 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002751
Douglas Gregore7526412009-11-11 19:31:23 +00002752 // C++ [temp.arg]p2:
2753 // In a template-argument, an ambiguity between a type-id and
2754 // an expression is resolved to a type-id, regardless of the
2755 // form of the corresponding template-parameter.
2756 //
2757 // We warn specifically about this case, since it can be rather
2758 // confusing for users.
2759 QualType T = Arg.getArgument().getAsType();
2760 SourceRange SR = Arg.getSourceRange();
2761 if (T->isFunctionType())
2762 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2763 else
2764 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2765 Diag(Param->getLocation(), diag::note_template_param_here);
2766 return true;
2767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregore7526412009-11-11 19:31:23 +00002769 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002770 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002771 break;
2772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773
Douglas Gregore7526412009-11-11 19:31:23 +00002774 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002775 }
2776
2777
Douglas Gregore7526412009-11-11 19:31:23 +00002778 // Check template template parameters.
2779 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780
Douglas Gregore7526412009-11-11 19:31:23 +00002781 // Substitute into the template parameter list of the template
2782 // template parameter, since previously-supplied template arguments
2783 // may appear within the template template parameter.
2784 {
2785 // Set up a template instantiation context.
2786 LocalInstantiationScope Scope(*this);
2787 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002788 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002789 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790
2791 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002792 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002793 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002795 MultiLevelTemplateArgumentList(TemplateArgs)));
2796 if (!TempParm)
2797 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799
Douglas Gregore7526412009-11-11 19:31:23 +00002800 switch (Arg.getArgument().getKind()) {
2801 case TemplateArgument::Null:
2802 assert(false && "Should never see a NULL template argument here");
2803 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804
Douglas Gregore7526412009-11-11 19:31:23 +00002805 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002806 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002807 if (CheckTemplateArgument(TempParm, Arg))
2808 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809
Douglas Gregor910f8002010-11-07 23:05:16 +00002810 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002811 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812
Douglas Gregore7526412009-11-11 19:31:23 +00002813 case TemplateArgument::Expression:
2814 case TemplateArgument::Type:
2815 // We have a template template parameter but the template
2816 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002817 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2818 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002819 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820
Douglas Gregore7526412009-11-11 19:31:23 +00002821 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002822 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002823 "Declaration argument with template template parameter");
2824 break;
2825 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002826 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002827 "Integral argument with template template parameter");
2828 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002829
Douglas Gregore7526412009-11-11 19:31:23 +00002830 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002831 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002832 break;
2833 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834
Douglas Gregore7526412009-11-11 19:31:23 +00002835 return false;
2836}
2837
Douglas Gregorc15cb382009-02-09 23:23:08 +00002838/// \brief Check that the given template argument list is well-formed
2839/// for specializing the given template.
2840bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2841 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002842 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002843 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002844 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002845 TemplateParameterList *Params = Template->getTemplateParameters();
2846 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002847 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002848 bool Invalid = false;
2849
John McCalld5532b62009-11-23 01:53:49 +00002850 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2851
Mike Stump1eb44332009-09-09 15:08:12 +00002852 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002853 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002854
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002855 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002856 (NumArgs < Params->getMinRequiredArguments() &&
2857 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002858 // FIXME: point at either the first arg beyond what we can handle,
2859 // or the '>', depending on whether we have too many or too few
2860 // arguments.
2861 SourceRange Range;
2862 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002863 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002864 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2865 << (NumArgs > NumParams)
2866 << (isa<ClassTemplateDecl>(Template)? 0 :
2867 isa<FunctionTemplateDecl>(Template)? 1 :
2868 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2869 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002870 Diag(Template->getLocation(), diag::note_template_decl_here)
2871 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002872 Invalid = true;
2873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
2875 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002876 // [...] The type and form of each template-argument specified in
2877 // a template-id shall match the type and form specified for the
2878 // corresponding parameter declared by the template in its
2879 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002880 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002881 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002882 TemplateParameterList::iterator Param = Params->begin(),
2883 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002884 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002885 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002886 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002887 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002888 // If we have an expanded parameter pack, make sure we don't have too
2889 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002890 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002891 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002892 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002893 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2894 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2895 << true
2896 << (isa<ClassTemplateDecl>(Template)? 0 :
2897 isa<FunctionTemplateDecl>(Template)? 1 :
2898 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2899 << Template;
2900 Diag(Template->getLocation(), diag::note_template_decl_here)
2901 << Params->getSourceRange();
2902 return true;
2903 }
2904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002905
Douglas Gregorf35f8282009-11-11 21:54:23 +00002906 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002907 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2908 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002909 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002910 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002911
Douglas Gregor14be16b2010-12-20 16:57:52 +00002912 if ((*Param)->isTemplateParameterPack()) {
2913 // The template parameter was a template parameter pack, so take the
2914 // deduced argument and place it on the argument pack. Note that we
2915 // stay on the same template parameter so that we can deduce more
2916 // arguments.
2917 ArgumentPack.push_back(Converted.back());
2918 Converted.pop_back();
2919 } else {
2920 // Move to the next template parameter.
2921 ++Param;
2922 }
2923 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002924 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002925 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002926
Douglas Gregor8735b292011-06-03 02:59:40 +00002927 // If we're checking a partial template argument list, we're done.
2928 if (PartialTemplateArgs) {
2929 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2930 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2931 ArgumentPack.data(),
2932 ArgumentPack.size()));
2933
2934 return Invalid;
2935 }
2936
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002938 // arguments, just break out now and we'll fill in the argument pack below.
2939 if ((*Param)->isTemplateParameterPack())
2940 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002941
Douglas Gregorf35f8282009-11-11 21:54:23 +00002942 // We have a default template argument that we will use.
2943 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002944
Douglas Gregorf35f8282009-11-11 21:54:23 +00002945 // Retrieve the default template argument from the template
2946 // parameter. For each kind of template parameter, we substitute the
2947 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002948 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002949 // the default argument.
2950 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2951 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002952 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002953 break;
2954 }
2955
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002956 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002957 Template,
2958 TemplateLoc,
2959 RAngleLoc,
2960 TTP,
2961 Converted);
2962 if (!ArgType)
2963 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002964
Douglas Gregorf35f8282009-11-11 21:54:23 +00002965 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2966 ArgType);
2967 } else if (NonTypeTemplateParmDecl *NTTP
2968 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2969 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002970 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002971 break;
2972 }
2973
John McCall60d7b3a2010-08-24 06:29:42 +00002974 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002975 TemplateLoc,
2976 RAngleLoc,
2977 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002978 Converted);
2979 if (E.isInvalid())
2980 return true;
2981
2982 Expr *Ex = E.takeAs<Expr>();
2983 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2984 } else {
2985 TemplateTemplateParmDecl *TempParm
2986 = cast<TemplateTemplateParmDecl>(*Param);
2987
2988 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002989 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002990 break;
2991 }
2992
Douglas Gregor1d752d72011-03-02 18:46:51 +00002993 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002994 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002995 TemplateLoc,
2996 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002997 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002998 Converted,
2999 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003000 if (Name.isNull())
3001 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003003 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3004 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003006
Douglas Gregorf35f8282009-11-11 21:54:23 +00003007 // Introduce an instantiation record that describes where we are using
3008 // the default template argument.
3009 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003010 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003011 SourceRange(TemplateLoc, RAngleLoc));
3012
Douglas Gregorf35f8282009-11-11 21:54:23 +00003013 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003014 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003015 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003016 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003017
Douglas Gregor67714232011-03-03 02:41:12 +00003018 // Core issue 150 (assumed resolution): if this is a template template
3019 // parameter, keep track of the default template arguments from the
3020 // template definition.
3021 if (isTemplateTemplateParameter)
3022 TemplateArgs.addArgument(Arg);
3023
Douglas Gregor14be16b2010-12-20 16:57:52 +00003024 // Move to the next template parameter and argument.
3025 ++Param;
3026 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003027 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003028
Douglas Gregor14be16b2010-12-20 16:57:52 +00003029 // Form argument packs for each of the parameter packs remaining.
3030 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003031 // If we're checking a partial list of template arguments, don't fill
3032 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033
3034 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003035 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003036 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003037 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003038 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3039 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003040 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003041 ArgumentPack.clear();
3042 }
3043 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003044
Douglas Gregor14be16b2010-12-20 16:57:52 +00003045 ++Param;
3046 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003047
Douglas Gregorc15cb382009-02-09 23:23:08 +00003048 return Invalid;
3049}
3050
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003051namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052 class UnnamedLocalNoLinkageFinder
3053 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003054 {
3055 Sema &S;
3056 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003057
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003058 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003059
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003060 public:
3061 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3062
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063 bool Visit(QualType T) {
3064 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003065 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003066
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003067#define TYPE(Class, Parent) \
3068 bool Visit##Class##Type(const Class##Type *);
3069#define ABSTRACT_TYPE(Class, Parent) \
3070 bool Visit##Class##Type(const Class##Type *) { return false; }
3071#define NON_CANONICAL_TYPE(Class, Parent) \
3072 bool Visit##Class##Type(const Class##Type *) { return false; }
3073#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003074
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003075 bool VisitTagDecl(const TagDecl *Tag);
3076 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3077 };
3078}
3079
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003080bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003081 return false;
3082}
3083
3084bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3085 return Visit(T->getElementType());
3086}
3087
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003088bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003089 return Visit(T->getPointeeType());
3090}
3091
3092bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003093 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003094 return Visit(T->getPointeeType());
3095}
3096
3097bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003098 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003099 return Visit(T->getPointeeType());
3100}
3101
3102bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003103 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003104 return Visit(T->getPointeeType());
3105}
3106
3107bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003108 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003109 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3110}
3111
3112bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003113 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003114 return Visit(T->getElementType());
3115}
3116
3117bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003118 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003119 return Visit(T->getElementType());
3120}
3121
3122bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003123 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003124 return Visit(T->getElementType());
3125}
3126
3127bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003128 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003129 return Visit(T->getElementType());
3130}
3131
3132bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003133 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003134 return Visit(T->getElementType());
3135}
3136
3137bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3138 return Visit(T->getElementType());
3139}
3140
3141bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3142 return Visit(T->getElementType());
3143}
3144
3145bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3146 const FunctionProtoType* T) {
3147 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003148 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003149 A != AEnd; ++A) {
3150 if (Visit(*A))
3151 return true;
3152 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003153
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003154 return Visit(T->getResultType());
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3158 const FunctionNoProtoType* T) {
3159 return Visit(T->getResultType());
3160}
3161
3162bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3163 const UnresolvedUsingType*) {
3164 return false;
3165}
3166
3167bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3168 return false;
3169}
3170
3171bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3172 return Visit(T->getUnderlyingType());
3173}
3174
3175bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3176 return false;
3177}
3178
Sean Huntca63c202011-05-24 22:41:36 +00003179bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3180 const UnaryTransformType*) {
3181 return false;
3182}
3183
Richard Smith34b41d92011-02-20 03:19:35 +00003184bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3185 return Visit(T->getDeducedType());
3186}
3187
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003188bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3189 return VisitTagDecl(T->getDecl());
3190}
3191
3192bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3193 return VisitTagDecl(T->getDecl());
3194}
3195
3196bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3197 const TemplateTypeParmType*) {
3198 return false;
3199}
3200
Douglas Gregorc3069d62011-01-14 02:55:32 +00003201bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3202 const SubstTemplateTypeParmPackType *) {
3203 return false;
3204}
3205
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003206bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3207 const TemplateSpecializationType*) {
3208 return false;
3209}
3210
3211bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3212 const InjectedClassNameType* T) {
3213 return VisitTagDecl(T->getDecl());
3214}
3215
3216bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3217 const DependentNameType* T) {
3218 return VisitNestedNameSpecifier(T->getQualifier());
3219}
3220
3221bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3222 const DependentTemplateSpecializationType* T) {
3223 return VisitNestedNameSpecifier(T->getQualifier());
3224}
3225
Douglas Gregor7536dd52010-12-20 02:24:11 +00003226bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3227 const PackExpansionType* T) {
3228 return Visit(T->getPattern());
3229}
3230
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003231bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3232 return false;
3233}
3234
3235bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3236 const ObjCInterfaceType *) {
3237 return false;
3238}
3239
3240bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3241 const ObjCObjectPointerType *) {
3242 return false;
3243}
3244
3245bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3246 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3247 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3248 << S.Context.getTypeDeclType(Tag) << SR;
3249 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003250 }
3251
Richard Smith162e1c12011-04-15 14:24:37 +00003252 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003253 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3254 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3255 return true;
3256 }
3257
3258 return false;
3259}
3260
3261bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3262 NestedNameSpecifier *NNS) {
3263 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3264 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003265
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003266 switch (NNS->getKind()) {
3267 case NestedNameSpecifier::Identifier:
3268 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003269 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003270 case NestedNameSpecifier::Global:
3271 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003272
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003273 case NestedNameSpecifier::TypeSpec:
3274 case NestedNameSpecifier::TypeSpecWithTemplate:
3275 return Visit(QualType(NNS->getAsType(), 0));
3276 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003277 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003278}
3279
3280
Douglas Gregorc15cb382009-02-09 23:23:08 +00003281/// \brief Check a template argument against its corresponding
3282/// template type parameter.
3283///
3284/// This routine implements the semantics of C++ [temp.arg.type]. It
3285/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003286bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003287 TypeSourceInfo *ArgInfo) {
3288 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003289 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003290 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003291
3292 if (Arg->isVariablyModifiedType()) {
3293 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003294 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003295 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003296 }
3297
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003298 // C++03 [temp.arg.type]p2:
3299 // A local type, a type with no linkage, an unnamed type or a type
3300 // compounded from any of these types shall not be used as a
3301 // template-argument for a template type-parameter.
3302 //
3303 // C++0x allows these, and even in C++03 we allow them as an extension with
3304 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003305 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003306 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3307 (void)Finder.Visit(Context.getCanonicalType(Arg));
3308 }
3309
Douglas Gregorc15cb382009-02-09 23:23:08 +00003310 return false;
3311}
3312
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003313/// \brief Checks whether the given template argument is the address
3314/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003315static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003316CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3317 NonTypeTemplateParmDecl *Param,
3318 QualType ParamType,
3319 Expr *ArgIn,
3320 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003321 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003322 Expr *Arg = ArgIn;
3323 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003324
3325 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003326 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003327
3328 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003329 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003330 // A template-argument for a non-type, non-template
3331 // template-parameter shall be one of: [...]
3332 //
3333 // -- the address of an object or function with external
3334 // linkage, including function templates and function
3335 // template-ids but excluding non-static class members,
3336 // expressed as & id-expression where the & is optional if
3337 // the name refers to a function or array, or if the
3338 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003340 // In C++98/03 mode, give an extension warning on any extra parentheses.
3341 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3342 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003343 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003344 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003345 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003346 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003347 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003348 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003349 }
3350
3351 Arg = Parens->getSubExpr();
3352 }
3353
John McCall91a57552011-07-15 05:09:51 +00003354 while (SubstNonTypeTemplateParmExpr *subst =
3355 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3356 Arg = subst->getReplacement()->IgnoreImpCasts();
3357
Douglas Gregorb7a09262010-04-01 18:32:35 +00003358 bool AddressTaken = false;
3359 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003360 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003361 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003362 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003363 AddressTaken = true;
3364 AddrOpLoc = UnOp->getOperatorLoc();
3365 }
Francois Picheta343a412011-04-29 09:08:14 +00003366 }
John McCall91a57552011-07-15 05:09:51 +00003367
3368 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3369 Converted = TemplateArgument(ArgIn);
3370 return false;
3371 }
3372
3373 while (SubstNonTypeTemplateParmExpr *subst =
3374 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3375 Arg = subst->getReplacement()->IgnoreImpCasts();
3376
3377 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003378 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003379 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3380 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003381 S.Diag(Param->getLocation(), diag::note_template_param_here);
3382 return true;
3383 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003384
3385 // Stop checking the precise nature of the argument if it is value dependent,
3386 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003387 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003388 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003389 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003390 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003391
Douglas Gregorb7a09262010-04-01 18:32:35 +00003392 if (!isa<ValueDecl>(DRE->getDecl())) {
3393 S.Diag(Arg->getSourceRange().getBegin(),
3394 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003395 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003396 S.Diag(Param->getLocation(), diag::note_template_param_here);
3397 return true;
3398 }
3399
3400 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003401
3402 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003403 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3404 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003405 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003406 S.Diag(Param->getLocation(), diag::note_template_param_here);
3407 return true;
3408 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003409
3410 // Cannot refer to non-static member functions
3411 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003412 if (!Method->isStatic()) {
3413 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003414 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003415 S.Diag(Param->getLocation(), diag::note_template_param_here);
3416 return true;
3417 }
Mike Stump1eb44332009-09-09 15:08:12 +00003418
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003419 // Functions must have external linkage.
3420 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003421 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003422 S.Diag(Arg->getSourceRange().getBegin(),
3423 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003424 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003425 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003426 << true;
3427 return true;
3428 }
3429
3430 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003431 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003432
Douglas Gregorb7a09262010-04-01 18:32:35 +00003433 // If the template parameter has pointer type, the function decays.
3434 if (ParamType->isPointerType() && !AddressTaken)
3435 ArgType = S.Context.getPointerType(Func->getType());
3436 else if (AddressTaken && ParamType->isReferenceType()) {
3437 // If we originally had an address-of operator, but the
3438 // parameter has reference type, complain and (if things look
3439 // like they will work) drop the address-of operator.
3440 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3441 ParamType.getNonReferenceType())) {
3442 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3443 << ParamType;
3444 S.Diag(Param->getLocation(), diag::note_template_param_here);
3445 return true;
3446 }
3447
3448 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3449 << ParamType
3450 << FixItHint::CreateRemoval(AddrOpLoc);
3451 S.Diag(Param->getLocation(), diag::note_template_param_here);
3452
3453 ArgType = Func->getType();
3454 }
3455 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003456 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003457 S.Diag(Arg->getSourceRange().getBegin(),
3458 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003459 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003460 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003461 << true;
3462 return true;
3463 }
3464
Douglas Gregorb7a09262010-04-01 18:32:35 +00003465 // A value of reference type is not an object.
3466 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003467 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003468 diag::err_template_arg_reference_var)
3469 << Var->getType() << Arg->getSourceRange();
3470 S.Diag(Param->getLocation(), diag::note_template_param_here);
3471 return true;
3472 }
3473
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003474 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003475 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003476
3477 // If the template parameter has pointer type, we must have taken
3478 // the address of this object.
3479 if (ParamType->isReferenceType()) {
3480 if (AddressTaken) {
3481 // If we originally had an address-of operator, but the
3482 // parameter has reference type, complain and (if things look
3483 // like they will work) drop the address-of operator.
3484 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3485 ParamType.getNonReferenceType())) {
3486 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3487 << ParamType;
3488 S.Diag(Param->getLocation(), diag::note_template_param_here);
3489 return true;
3490 }
3491
3492 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3493 << ParamType
3494 << FixItHint::CreateRemoval(AddrOpLoc);
3495 S.Diag(Param->getLocation(), diag::note_template_param_here);
3496
3497 ArgType = Var->getType();
3498 }
3499 } else if (!AddressTaken && ParamType->isPointerType()) {
3500 if (Var->getType()->isArrayType()) {
3501 // Array-to-pointer decay.
3502 ArgType = S.Context.getArrayDecayedType(Var->getType());
3503 } else {
3504 // If the template parameter has pointer type but the address of
3505 // this object was not taken, complain and (possibly) recover by
3506 // taking the address of the entity.
3507 ArgType = S.Context.getPointerType(Var->getType());
3508 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3509 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3510 << ParamType;
3511 S.Diag(Param->getLocation(), diag::note_template_param_here);
3512 return true;
3513 }
3514
3515 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3516 << ParamType
3517 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3518
3519 S.Diag(Param->getLocation(), diag::note_template_param_here);
3520 }
3521 }
3522 } else {
3523 // We found something else, but we don't know specifically what it is.
3524 S.Diag(Arg->getSourceRange().getBegin(),
3525 diag::err_template_arg_not_object_or_func)
3526 << Arg->getSourceRange();
3527 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3528 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003529 }
Mike Stump1eb44332009-09-09 15:08:12 +00003530
John McCallf85e1932011-06-15 23:02:42 +00003531 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003532 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003533 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003534 S.IsQualificationConversion(ArgType, ParamType, false,
3535 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003536 // For pointer-to-object types, qualification conversions are
3537 // permitted.
3538 } else {
3539 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3540 if (!ParamRef->getPointeeType()->isFunctionType()) {
3541 // C++ [temp.arg.nontype]p5b3:
3542 // For a non-type template-parameter of type reference to
3543 // object, no conversions apply. The type referred to by the
3544 // reference may be more cv-qualified than the (otherwise
3545 // identical) type of the template- argument. The
3546 // template-parameter is bound directly to the
3547 // template-argument, which shall be an lvalue.
3548
3549 // FIXME: Other qualifiers?
3550 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3551 unsigned ArgQuals = ArgType.getCVRQualifiers();
3552
3553 if ((ParamQuals | ArgQuals) != ParamQuals) {
3554 S.Diag(Arg->getSourceRange().getBegin(),
3555 diag::err_template_arg_ref_bind_ignores_quals)
3556 << ParamType << Arg->getType()
3557 << Arg->getSourceRange();
3558 S.Diag(Param->getLocation(), diag::note_template_param_here);
3559 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003560 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003561 }
3562 }
3563
3564 // At this point, the template argument refers to an object or
3565 // function with external linkage. We now need to check whether the
3566 // argument and parameter types are compatible.
3567 if (!S.Context.hasSameUnqualifiedType(ArgType,
3568 ParamType.getNonReferenceType())) {
3569 // We can't perform this conversion or binding.
3570 if (ParamType->isReferenceType())
3571 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003572 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003573 else
3574 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003575 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003576 S.Diag(Param->getLocation(), diag::note_template_param_here);
3577 return true;
3578 }
3579 }
3580
3581 // Create the template argument.
3582 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003583 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003584 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003585}
3586
3587/// \brief Checks whether the given template argument is a pointer to
3588/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003589bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003590 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003591 bool Invalid = false;
3592
3593 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003594 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003595 Arg = Cast->getSubExpr();
3596
3597 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003598 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003599 // A template-argument for a non-type, non-template
3600 // template-parameter shall be one of: [...]
3601 //
3602 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003603 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003604
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003605 // In C++98/03 mode, give an extension warning on any extra parentheses.
3606 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3607 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003608 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003609 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003610 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003611 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003612 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003613 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003614 }
3615
3616 Arg = Parens->getSubExpr();
3617 }
3618
John McCall91a57552011-07-15 05:09:51 +00003619 while (SubstNonTypeTemplateParmExpr *subst =
3620 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3621 Arg = subst->getReplacement()->IgnoreImpCasts();
3622
Douglas Gregorcaddba02009-11-12 18:38:13 +00003623 // A pointer-to-member constant written &Class::member.
3624 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003625 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003626 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3627 if (DRE && !DRE->getQualifier())
3628 DRE = 0;
3629 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003630 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003631 // A constant of pointer-to-member type.
3632 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3633 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3634 if (VD->getType()->isMemberPointerType()) {
3635 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003636 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003637 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3638 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003639 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003640 else
3641 Converted = TemplateArgument(VD->getCanonicalDecl());
3642 return Invalid;
3643 }
3644 }
3645 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003646
Douglas Gregorcaddba02009-11-12 18:38:13 +00003647 DRE = 0;
3648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003649
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003650 if (!DRE)
3651 return Diag(Arg->getSourceRange().getBegin(),
3652 diag::err_template_arg_not_pointer_to_member_form)
3653 << Arg->getSourceRange();
3654
3655 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3656 assert((isa<FieldDecl>(DRE->getDecl()) ||
3657 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3658 "Only non-static member pointers can make it here");
3659
3660 // Okay: this is the address of a non-static member, and therefore
3661 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003662 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003663 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003664 else
3665 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003666 return Invalid;
3667 }
3668
3669 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003670 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003671 diag::err_template_arg_not_pointer_to_member_form)
3672 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003673 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003674 diag::note_template_arg_refers_here);
3675 return true;
3676}
3677
Douglas Gregorc15cb382009-02-09 23:23:08 +00003678/// \brief Check a template argument against its corresponding
3679/// non-type template parameter.
3680///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003681/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003682/// If an error occurred, it returns ExprError(); otherwise, it
3683/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003684/// InstantiatedParamType is the type of the non-type template
3685/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003686ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3687 QualType InstantiatedParamType, Expr *Arg,
3688 TemplateArgument &Converted,
3689 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003690 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3691
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003692 // If either the parameter has a dependent type or the argument is
3693 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003694 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3695 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003696 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003697 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003698 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003699
3700 // C++ [temp.arg.nontype]p5:
3701 // The following conversions are performed on each expression used
3702 // as a non-type template-argument. If a non-type
3703 // template-argument cannot be converted to the type of the
3704 // corresponding template-parameter then the program is
3705 // ill-formed.
3706 //
3707 // -- for a non-type template-parameter of integral or
3708 // enumeration type, integral promotions (4.5) and integral
3709 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003710 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003711 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003712 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003713 // C++ [temp.arg.nontype]p1:
3714 // A template-argument for a non-type, non-template
3715 // template-parameter shall be one of:
3716 //
3717 // -- an integral constant-expression of integral or enumeration
3718 // type; or
3719 // -- the name of a non-type template-parameter; or
3720 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003721 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003722 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003723 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003724 diag::err_template_arg_not_integral_or_enumeral)
3725 << ArgType << Arg->getSourceRange();
3726 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003727 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003728 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003729 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003730 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3731 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003732 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003733 }
3734
Douglas Gregor02024a92010-03-28 02:42:43 +00003735 // From here on out, all we care about are the unqualified forms
3736 // of the parameter and argument types.
3737 ParamType = ParamType.getUnqualifiedType();
3738 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003739
3740 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003741 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003742 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003743 } else if (CTAK == CTAK_Deduced) {
3744 // C++ [temp.deduct.type]p17:
3745 // If, in the declaration of a function template with a non-type
3746 // template-parameter, the non-type template- parameter is used
3747 // in an expression in the function parameter-list and, if the
3748 // corresponding template-argument is deduced, the
3749 // template-argument type shall match the type of the
3750 // template-parameter exactly, except that a template-argument
3751 // deduced from an array bound may be of any integral type.
3752 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3753 << ArgType << ParamType;
3754 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003755 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003756 } else if (ParamType->isBooleanType()) {
3757 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003758 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003759 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3760 !ParamType->isEnumeralType()) {
3761 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003762 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003763 } else {
3764 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003765 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003766 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003767 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003768 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003769 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003770 }
3771
Douglas Gregorc7469372011-05-04 21:55:00 +00003772 // Add the value of this argument to the list of converted
3773 // arguments. We use the bitwidth and signedness of the template
3774 // parameter.
3775 if (Arg->isValueDependent()) {
3776 // The argument is value-dependent. Create a new
3777 // TemplateArgument with the converted expression.
3778 Converted = TemplateArgument(Arg);
3779 return Owned(Arg);
3780 }
3781
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003782 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003783 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003784 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003785
Douglas Gregorc7469372011-05-04 21:55:00 +00003786 if (ParamType->isBooleanType()) {
3787 // Value must be zero or one.
3788 Value = Value != 0;
3789 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3790 if (Value.getBitWidth() != AllowedBits)
3791 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003792 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003793 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003794 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003795
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003796 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003797 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003798 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003799 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003800 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003801 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003802
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003803 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003804 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003805 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003806 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3807 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3808 << Arg->getSourceRange();
3809 Diag(Param->getLocation(), diag::note_template_param_here);
3810 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003811
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003812 // Complain if we overflowed the template parameter's type.
3813 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003814 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003815 RequiredBits = OldValue.getActiveBits();
3816 else if (OldValue.isUnsigned())
3817 RequiredBits = OldValue.getActiveBits() + 1;
3818 else
3819 RequiredBits = OldValue.getMinSignedBits();
3820 if (RequiredBits > AllowedBits) {
3821 Diag(Arg->getSourceRange().getBegin(),
3822 diag::warn_template_arg_too_large)
3823 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3824 << Arg->getSourceRange();
3825 Diag(Param->getLocation(), diag::note_template_param_here);
3826 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003827 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003828
John McCall833ca992009-10-29 08:12:44 +00003829 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003830 ParamType->isEnumeralType()
3831 ? Context.getCanonicalType(ParamType)
3832 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003833 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003834 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003835
John McCall6bb80172010-03-30 21:47:33 +00003836 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3837
Douglas Gregorb7a09262010-04-01 18:32:35 +00003838 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3839 // from a template argument of type std::nullptr_t to a non-type
3840 // template parameter of type pointer to object, pointer to
3841 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003842 if (ArgType->isNullPtrType()) {
3843 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3844 Converted = TemplateArgument((NamedDecl *)0);
3845 return Owned(Arg);
3846 }
3847
3848 if (ParamType->isNullPtrType()) {
3849 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3850 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3851 return Owned(Arg);
3852 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003853 }
3854
Douglas Gregorb86b0572009-02-11 01:18:59 +00003855 // Handle pointer-to-function, reference-to-function, and
3856 // pointer-to-member-function all in (roughly) the same way.
3857 if (// -- For a non-type template-parameter of type pointer to
3858 // function, only the function-to-pointer conversion (4.3) is
3859 // applied. If the template-argument represents a set of
3860 // overloaded functions (or a pointer to such), the matching
3861 // function is selected from the set (13.4).
3862 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003863 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003864 // -- For a non-type template-parameter of type reference to
3865 // function, no conversions apply. If the template-argument
3866 // represents a set of overloaded functions, the matching
3867 // function is selected from the set (13.4).
3868 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003869 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003870 // -- For a non-type template-parameter of type pointer to
3871 // member function, no conversions apply. If the
3872 // template-argument represents a set of overloaded member
3873 // functions, the matching member function is selected from
3874 // the set (13.4).
3875 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003876 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003877 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003878
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003879 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003880 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003881 true,
3882 FoundResult)) {
3883 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003884 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003885
3886 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3887 ArgType = Arg->getType();
3888 } else
John Wiegley429bb272011-04-08 18:41:53 +00003889 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003891
John Wiegley429bb272011-04-08 18:41:53 +00003892 if (!ParamType->isMemberPointerType()) {
3893 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3894 ParamType,
3895 Arg, Converted))
3896 return ExprError();
3897 return Owned(Arg);
3898 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003899
John McCallf85e1932011-06-15 23:02:42 +00003900 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003901 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003902 false, ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003903 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003904 } else if (!Context.hasSameUnqualifiedType(ArgType,
3905 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003906 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003907 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003908 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003909 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003910 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003911 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003912 }
Mike Stump1eb44332009-09-09 15:08:12 +00003913
John Wiegley429bb272011-04-08 18:41:53 +00003914 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3915 return ExprError();
3916 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003917 }
3918
Chris Lattnerfe90de72009-02-20 21:37:53 +00003919 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003920 // -- for a non-type template-parameter of type pointer to
3921 // object, qualification conversions (4.4) and the
3922 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003923 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003924 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003925 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003926
John Wiegley429bb272011-04-08 18:41:53 +00003927 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3928 ParamType,
3929 Arg, Converted))
3930 return ExprError();
3931 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003932 }
Mike Stump1eb44332009-09-09 15:08:12 +00003933
Ted Kremenek6217b802009-07-29 21:53:49 +00003934 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003935 // -- For a non-type template-parameter of type reference to
3936 // object, no conversions apply. The type referred to by the
3937 // reference may be more cv-qualified than the (otherwise
3938 // identical) type of the template-argument. The
3939 // template-parameter is bound directly to the
3940 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003941 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003942 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003943
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003944 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003945 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3946 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003947 true,
3948 FoundResult)) {
3949 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003950 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003951
3952 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3953 ArgType = Arg->getType();
3954 } else
John Wiegley429bb272011-04-08 18:41:53 +00003955 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003956 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003957
John Wiegley429bb272011-04-08 18:41:53 +00003958 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3959 ParamType,
3960 Arg, Converted))
3961 return ExprError();
3962 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003963 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003964
3965 // -- For a non-type template-parameter of type pointer to data
3966 // member, qualification conversions (4.4) are applied.
3967 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3968
John McCallf85e1932011-06-15 23:02:42 +00003969 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003970 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003971 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003972 } else if (IsQualificationConversion(ArgType, ParamType, false,
3973 ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003974 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003975 } else {
3976 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003977 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003978 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003979 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003980 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003981 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003982 }
3983
John Wiegley429bb272011-04-08 18:41:53 +00003984 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3985 return ExprError();
3986 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003987}
3988
3989/// \brief Check a template argument against its corresponding
3990/// template template parameter.
3991///
3992/// This routine implements the semantics of C++ [temp.arg.template].
3993/// It returns true if an error occurred, and false otherwise.
3994bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003995 const TemplateArgumentLoc &Arg) {
3996 TemplateName Name = Arg.getArgument().getAsTemplate();
3997 TemplateDecl *Template = Name.getAsTemplateDecl();
3998 if (!Template) {
3999 // Any dependent template name is fine.
4000 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4001 return false;
4002 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004003
Richard Smith3e4c6c42011-05-05 21:57:07 +00004004 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004005 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004006 // the name of a class template or an alias template, expressed as an
4007 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004008 // primary class templates are considered when matching the
4009 // template template argument with the corresponding parameter;
4010 // partial specializations are not considered even if their
4011 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004012 //
4013 // Note that we also allow template template parameters here, which
4014 // will happen when we are dealing with, e.g., class template
4015 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004016 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004017 !isa<TemplateTemplateParmDecl>(Template) &&
4018 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004019 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004020 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004021 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004022 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004023 << Template;
4024 }
4025
4026 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4027 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004028 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004029 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004030 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004031}
4032
Douglas Gregor02024a92010-03-28 02:42:43 +00004033/// \brief Given a non-type template argument that refers to a
4034/// declaration and the type of its corresponding non-type template
4035/// parameter, produce an expression that properly refers to that
4036/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004037ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004038Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4039 QualType ParamType,
4040 SourceLocation Loc) {
4041 assert(Arg.getKind() == TemplateArgument::Declaration &&
4042 "Only declaration template arguments permitted here");
4043 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4044
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004045 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004046 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4047 // If the value is a class member, we might have a pointer-to-member.
4048 // Determine whether the non-type template template parameter is of
4049 // pointer-to-member type. If so, we need to build an appropriate
4050 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4051 // would refer to the member itself.
4052 if (ParamType->isMemberPointerType()) {
4053 QualType ClassType
4054 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4055 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004056 = NestedNameSpecifier::Create(Context, 0, false,
4057 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004058 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004059 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004060
4061 // The actual value-ness of this is unimportant, but for
4062 // internal consistency's sake, references to instance methods
4063 // are r-values.
4064 ExprValueKind VK = VK_LValue;
4065 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4066 VK = VK_RValue;
4067
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004069 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004070 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004071 Loc,
4072 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004073 if (RefExpr.isInvalid())
4074 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004075
John McCall2de56d12010-08-25 11:45:40 +00004076 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004077
Douglas Gregorc0c83002010-04-30 21:46:38 +00004078 // We might need to perform a trailing qualification conversion, since
4079 // the element type on the parameter could be more qualified than the
4080 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004081 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004082 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004083 ParamType.getUnqualifiedType(), false,
4084 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004085 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004086
Douglas Gregor02024a92010-03-28 02:42:43 +00004087 assert(!RefExpr.isInvalid() &&
4088 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004089 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004090 return move(RefExpr);
4091 }
4092 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004093
Douglas Gregor02024a92010-03-28 02:42:43 +00004094 QualType T = VD->getType().getNonReferenceType();
4095 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004096 // When the non-type template parameter is a pointer, take the
4097 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004098 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004099 if (RefExpr.isInvalid())
4100 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004101
4102 if (T->isFunctionType() || T->isArrayType()) {
4103 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004104 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4105 if (RefExpr.isInvalid())
4106 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004107
4108 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004109 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004110
Douglas Gregorb7a09262010-04-01 18:32:35 +00004111 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004112 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004113 }
4114
John McCallf89e55a2010-11-18 06:31:45 +00004115 ExprValueKind VK = VK_RValue;
4116
Douglas Gregor02024a92010-03-28 02:42:43 +00004117 // If the non-type template parameter has reference type, qualify the
4118 // resulting declaration reference with the extra qualifiers on the
4119 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004120 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4121 VK = VK_LValue;
4122 T = Context.getQualifiedType(T,
4123 TargetRef->getPointeeType().getQualifiers());
4124 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004125
John McCallf89e55a2010-11-18 06:31:45 +00004126 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004127}
4128
4129/// \brief Construct a new expression that refers to the given
4130/// integral template argument with the given source-location
4131/// information.
4132///
4133/// This routine takes care of the mapping from an integral template
4134/// argument (which may have any integral type) to the appropriate
4135/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004136ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004137Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4138 SourceLocation Loc) {
4139 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004140 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004141 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004142 if (T->isAnyCharacterType()) {
4143 CharacterLiteral::CharacterKind Kind;
4144 if (T->isWideCharType())
4145 Kind = CharacterLiteral::Wide;
4146 else if (T->isChar16Type())
4147 Kind = CharacterLiteral::UTF16;
4148 else if (T->isChar32Type())
4149 Kind = CharacterLiteral::UTF32;
4150 else
4151 Kind = CharacterLiteral::Ascii;
4152
Douglas Gregor02024a92010-03-28 02:42:43 +00004153 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004154 Arg.getAsIntegral()->getZExtValue(),
4155 Kind, T, Loc));
4156 }
4157
Douglas Gregor02024a92010-03-28 02:42:43 +00004158 if (T->isBooleanType())
4159 return Owned(new (Context) CXXBoolLiteralExpr(
4160 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004161 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004162
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004163 if (T->isNullPtrType())
4164 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4165
Chris Lattner223de242011-04-25 20:37:58 +00004166 // If this is an enum type that we're instantiating, we need to use an integer
4167 // type the same size as the enumerator. We don't want to build an
4168 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004169 QualType BT;
4170 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004171 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004172 else
4173 BT = T;
4174
John McCall4e9272d2011-07-15 07:47:58 +00004175 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4176 if (T->isEnumeralType()) {
4177 // FIXME: This is a hack. We need a better way to handle substituted
4178 // non-type template parameters.
4179 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4180 Context.getTrivialTypeSourceInfo(T, Loc),
4181 Loc, Loc);
4182 }
4183
4184 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004185}
4186
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004187/// \brief Match two template parameters within template parameter lists.
4188static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4189 bool Complain,
4190 Sema::TemplateParameterListEqualKind Kind,
4191 SourceLocation TemplateArgLoc) {
4192 // Check the actual kind (type, non-type, template).
4193 if (Old->getKind() != New->getKind()) {
4194 if (Complain) {
4195 unsigned NextDiag = diag::err_template_param_different_kind;
4196 if (TemplateArgLoc.isValid()) {
4197 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4198 NextDiag = diag::note_template_param_different_kind;
4199 }
4200 S.Diag(New->getLocation(), NextDiag)
4201 << (Kind != Sema::TPL_TemplateMatch);
4202 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4203 << (Kind != Sema::TPL_TemplateMatch);
4204 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004206 return false;
4207 }
4208
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004209 // Check that both are parameter packs are neither are parameter packs.
4210 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004211 // template template parameter, the template template parameter can have
4212 // a parameter pack where the template template argument does not.
4213 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4214 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4215 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004216 if (Complain) {
4217 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4218 if (TemplateArgLoc.isValid()) {
4219 S.Diag(TemplateArgLoc,
4220 diag::err_template_arg_template_params_mismatch);
4221 NextDiag = diag::note_template_parameter_pack_non_pack;
4222 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004223
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004224 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4225 : isa<NonTypeTemplateParmDecl>(New)? 1
4226 : 2;
4227 S.Diag(New->getLocation(), NextDiag)
4228 << ParamKind << New->isParameterPack();
4229 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4230 << ParamKind << Old->isParameterPack();
4231 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004232
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004233 return false;
4234 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004235
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004236 // For non-type template parameters, check the type of the parameter.
4237 if (NonTypeTemplateParmDecl *OldNTTP
4238 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4239 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004240
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004241 // If we are matching a template template argument to a template
4242 // template parameter and one of the non-type template parameter types
4243 // is dependent, then we must wait until template instantiation time
4244 // to actually compare the arguments.
4245 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4246 (OldNTTP->getType()->isDependentType() ||
4247 NewNTTP->getType()->isDependentType()))
4248 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004249
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004250 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4251 if (Complain) {
4252 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4253 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004254 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004255 diag::err_template_arg_template_params_mismatch);
4256 NextDiag = diag::note_template_nontype_parm_different_type;
4257 }
4258 S.Diag(NewNTTP->getLocation(), NextDiag)
4259 << NewNTTP->getType()
4260 << (Kind != Sema::TPL_TemplateMatch);
4261 S.Diag(OldNTTP->getLocation(),
4262 diag::note_template_nontype_parm_prev_declaration)
4263 << OldNTTP->getType();
4264 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004265
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004266 return false;
4267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004268
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004269 return true;
4270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004272 // For template template parameters, check the template parameter types.
4273 // The template parameter lists of template template
4274 // parameters must agree.
4275 if (TemplateTemplateParmDecl *OldTTP
4276 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004277 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004278 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4279 OldTTP->getTemplateParameters(),
4280 Complain,
4281 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004282 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004283 : Kind),
4284 TemplateArgLoc);
4285 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004287 return true;
4288}
Douglas Gregor02024a92010-03-28 02:42:43 +00004289
Douglas Gregora0347822011-01-13 00:08:50 +00004290/// \brief Diagnose a known arity mismatch when comparing template argument
4291/// lists.
4292static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004293void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004294 TemplateParameterList *New,
4295 TemplateParameterList *Old,
4296 Sema::TemplateParameterListEqualKind Kind,
4297 SourceLocation TemplateArgLoc) {
4298 unsigned NextDiag = diag::err_template_param_list_different_arity;
4299 if (TemplateArgLoc.isValid()) {
4300 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4301 NextDiag = diag::note_template_param_list_different_arity;
4302 }
4303 S.Diag(New->getTemplateLoc(), NextDiag)
4304 << (New->size() > Old->size())
4305 << (Kind != Sema::TPL_TemplateMatch)
4306 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4307 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4308 << (Kind != Sema::TPL_TemplateMatch)
4309 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4310}
4311
Douglas Gregorddc29e12009-02-06 22:42:48 +00004312/// \brief Determine whether the given template parameter lists are
4313/// equivalent.
4314///
Mike Stump1eb44332009-09-09 15:08:12 +00004315/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004316/// source code as part of a new template declaration.
4317///
4318/// \param Old The old template parameter list, typically found via
4319/// name lookup of the template declared with this template parameter
4320/// list.
4321///
4322/// \param Complain If true, this routine will produce a diagnostic if
4323/// the template parameter lists are not equivalent.
4324///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004325/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004326///
4327/// \param TemplateArgLoc If this source location is valid, then we
4328/// are actually checking the template parameter list of a template
4329/// argument (New) against the template parameter list of its
4330/// corresponding template template parameter (Old). We produce
4331/// slightly different diagnostics in this scenario.
4332///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004333/// \returns True if the template parameter lists are equal, false
4334/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004335bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004336Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4337 TemplateParameterList *Old,
4338 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004339 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004340 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004341 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4342 if (Complain)
4343 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4344 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004345
4346 return false;
4347 }
4348
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004349 // C++0x [temp.arg.template]p3:
4350 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004351 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004352 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004353 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004354 // template-parameter-list of P. [...]
4355 TemplateParameterList::iterator NewParm = New->begin();
4356 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004357 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004358 OldParmEnd = Old->end();
4359 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004360 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4361 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004362 if (NewParm == NewParmEnd) {
4363 if (Complain)
4364 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4365 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004366
Douglas Gregora0347822011-01-13 00:08:50 +00004367 return false;
4368 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004369
Douglas Gregora0347822011-01-13 00:08:50 +00004370 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4371 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004372 return false;
4373
Douglas Gregora0347822011-01-13 00:08:50 +00004374 ++NewParm;
4375 continue;
4376 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004377
Douglas Gregora0347822011-01-13 00:08:50 +00004378 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004379 // [...] When P's template- parameter-list contains a template parameter
4380 // pack (14.5.3), the template parameter pack will match zero or more
4381 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004382 // template-parameter-list of A with the same type and form as the
4383 // template parameter pack in P (ignoring whether those template
4384 // parameters are template parameter packs).
4385 for (; NewParm != NewParmEnd; ++NewParm) {
4386 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4387 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004388 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004389 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004391
Douglas Gregora0347822011-01-13 00:08:50 +00004392 // Make sure we exhausted all of the arguments.
4393 if (NewParm != NewParmEnd) {
4394 if (Complain)
4395 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4396 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004397
Douglas Gregora0347822011-01-13 00:08:50 +00004398 return false;
4399 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004400
Douglas Gregorddc29e12009-02-06 22:42:48 +00004401 return true;
4402}
4403
4404/// \brief Check whether a template can be declared within this scope.
4405///
4406/// If the template declaration is valid in this scope, returns
4407/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004408bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004409Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004410 // Find the nearest enclosing declaration scope.
4411 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4412 (S->getFlags() & Scope::TemplateParamScope) != 0)
4413 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004414
Douglas Gregorddc29e12009-02-06 22:42:48 +00004415 // C++ [temp]p2:
4416 // A template-declaration can appear only as a namespace scope or
4417 // class scope declaration.
4418 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004419 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4420 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004421 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004422 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004423
Eli Friedman1503f772009-07-31 01:43:05 +00004424 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004425 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004426
4427 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4428 return false;
4429
Mike Stump1eb44332009-09-09 15:08:12 +00004430 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004431 diag::err_template_outside_namespace_or_class_scope)
4432 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004433}
Douglas Gregorcc636682009-02-17 23:15:12 +00004434
Douglas Gregord5cb8762009-10-07 00:13:32 +00004435/// \brief Determine what kind of template specialization the given declaration
4436/// is.
4437static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4438 if (!D)
4439 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004440
Douglas Gregorf6b11852009-10-08 15:14:33 +00004441 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4442 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004443 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4444 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004445 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4446 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004447
Douglas Gregord5cb8762009-10-07 00:13:32 +00004448 return TSK_Undeclared;
4449}
4450
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004451/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004452/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004453///
Douglas Gregor9302da62009-10-14 23:50:59 +00004454/// This routine determines whether a template specialization can be declared
4455/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004456///
4457/// \param S the semantic analysis object for which this check is being
4458/// performed.
4459///
4460/// \param Specialized the entity being specialized or instantiated, which
4461/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004462/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004463/// member class).
4464///
4465/// \param PrevDecl the previous declaration of this entity, if any.
4466///
4467/// \param Loc the location of the explicit specialization or instantiation of
4468/// this entity.
4469///
4470/// \param IsPartialSpecialization whether this is a partial specialization of
4471/// a class template.
4472///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004473/// \returns true if there was an error that we cannot recover from, false
4474/// otherwise.
4475static bool CheckTemplateSpecializationScope(Sema &S,
4476 NamedDecl *Specialized,
4477 NamedDecl *PrevDecl,
4478 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004479 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004480 // Keep these "kind" numbers in sync with the %select statements in the
4481 // various diagnostics emitted by this routine.
4482 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004483 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004484 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004485 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004486 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004487 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004488 EntityKind = 3;
4489 else if (isa<VarDecl>(Specialized))
4490 EntityKind = 4;
4491 else if (isa<RecordDecl>(Specialized))
4492 EntityKind = 5;
4493 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004494 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4495 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004496 return true;
4497 }
4498
Douglas Gregor88b70942009-02-25 22:02:03 +00004499 // C++ [temp.expl.spec]p2:
4500 // An explicit specialization shall be declared in the namespace
4501 // of which the template is a member, or, for member templates, in
4502 // the namespace of which the enclosing class or enclosing class
4503 // template is a member. An explicit specialization of a member
4504 // function, member class or static data member of a class
4505 // template shall be declared in the namespace of which the class
4506 // template is a member. Such a declaration may also be a
4507 // definition. If the declaration is not a definition, the
4508 // specialization may be defined later in the name- space in which
4509 // the explicit specialization was declared, or in a namespace
4510 // that encloses the one in which the explicit specialization was
4511 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004512 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004513 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004514 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004515 return true;
4516 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004517
Douglas Gregor0a407472009-10-07 17:30:37 +00004518 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004519 if (S.getLangOptions().Microsoft) {
4520 // Do not warn for class scope explicit specialization during
4521 // instantiation, warning was already emitted during pattern
4522 // semantic analysis.
4523 if (!S.ActiveTemplateInstantiations.size())
4524 S.Diag(Loc, diag::ext_function_specialization_in_class)
4525 << Specialized;
4526 } else {
4527 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4528 << Specialized;
4529 return true;
4530 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004531 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004532
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004533 // C++ [temp.class.spec]p6:
4534 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004535 // in any namespace scope in which its definition may be defined (14.5.1
4536 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004537 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004538 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004539 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004540 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004542 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4543 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004544 // C++ [temp.exp.spec]p2:
4545 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004546 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004547 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004548 // An explicit specialization of a member function, member class or
4549 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004550 // namespace of which the class template is a member.
4551 //
4552 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004553 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004554 // the specialized template.
4555 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4556 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004557 bool IsCPlusPlus0xExtension
4558 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004559 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004560 S.Diag(Loc, IsCPlusPlus0xExtension
4561 ? diag::ext_template_spec_decl_out_of_scope_global
4562 : diag::err_template_spec_decl_out_of_scope_global)
4563 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004564 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004565 S.Diag(Loc, IsCPlusPlus0xExtension
4566 ? diag::ext_template_spec_decl_out_of_scope
4567 : diag::err_template_spec_decl_out_of_scope)
4568 << EntityKind << Specialized
4569 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570
Douglas Gregor9302da62009-10-14 23:50:59 +00004571 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4572 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004573 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004574 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004575
4576 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004577 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004578 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004579 // specializations of function templates, static data members, and member
4580 // functions, so we skip the check here for those kinds of entities.
4581 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004582 // Should we refactor that check, so that it occurs later?
4583 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004584 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4585 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004586 if (isa<TranslationUnitDecl>(SpecializedContext))
4587 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4588 << EntityKind << Specialized;
4589 else if (isa<NamespaceDecl>(SpecializedContext))
4590 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4591 << EntityKind << Specialized
4592 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004593
Douglas Gregor9302da62009-10-14 23:50:59 +00004594 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596
Douglas Gregord5cb8762009-10-07 00:13:32 +00004597 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004598
Douglas Gregor88b70942009-02-25 22:02:03 +00004599 return false;
4600}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004601
Douglas Gregorbacb9492011-01-03 21:13:47 +00004602/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4603/// that checks non-type template partial specialization arguments.
4604static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4605 NonTypeTemplateParmDecl *Param,
4606 const TemplateArgument *Args,
4607 unsigned NumArgs) {
4608 for (unsigned I = 0; I != NumArgs; ++I) {
4609 if (Args[I].getKind() == TemplateArgument::Pack) {
4610 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004611 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004612 Args[I].pack_size()))
4613 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004614
Douglas Gregore94866f2009-06-12 21:21:02 +00004615 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004616 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004617
Douglas Gregorbacb9492011-01-03 21:13:47 +00004618 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004619 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004620 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004621 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004622
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004623 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004624 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4625 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004626
4627 // Strip off any implicit casts we added as part of type checking.
4628 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4629 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004630
Douglas Gregore94866f2009-06-12 21:21:02 +00004631 // C++ [temp.class.spec]p8:
4632 // A non-type argument is non-specialized if it is the name of a
4633 // non-type parameter. All other non-type arguments are
4634 // specialized.
4635 //
4636 // Below, we check the two conditions that only apply to
4637 // specialized non-type arguments, so skip any non-specialized
4638 // arguments.
4639 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004640 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004641 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642
Douglas Gregore94866f2009-06-12 21:21:02 +00004643 // C++ [temp.class.spec]p9:
4644 // Within the argument list of a class template partial
4645 // specialization, the following restrictions apply:
4646 // -- A partially specialized non-type argument expression
4647 // shall not involve a template parameter of the partial
4648 // specialization except when the argument expression is a
4649 // simple identifier.
4650 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004651 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004652 diag::err_dependent_non_type_arg_in_partial_spec)
4653 << ArgExpr->getSourceRange();
4654 return true;
4655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004656
Douglas Gregore94866f2009-06-12 21:21:02 +00004657 // -- The type of a template parameter corresponding to a
4658 // specialized non-type argument shall not be dependent on a
4659 // parameter of the specialization.
4660 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004661 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004662 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4663 << Param->getType()
4664 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004665 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004666 return true;
4667 }
4668 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004669
Douglas Gregorbacb9492011-01-03 21:13:47 +00004670 return false;
4671}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004672
Douglas Gregorbacb9492011-01-03 21:13:47 +00004673/// \brief Check the non-type template arguments of a class template
4674/// partial specialization according to C++ [temp.class.spec]p9.
4675///
4676/// \param TemplateParams the template parameters of the primary class
4677/// template.
4678///
4679/// \param TemplateArg the template arguments of the class template
4680/// partial specialization.
4681///
4682/// \returns true if there was an error, false otherwise.
4683static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4684 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004685 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004686 const TemplateArgument *ArgList = TemplateArgs.data();
4687
4688 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4689 NonTypeTemplateParmDecl *Param
4690 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4691 if (!Param)
4692 continue;
4693
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004694 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004695 &ArgList[I], 1))
4696 return true;
4697 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004698
4699 return false;
4700}
4701
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004702/// \brief Retrieve the previous declaration of the given declaration.
4703static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4704 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4705 return VD->getPreviousDeclaration();
4706 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4707 return FD->getPreviousDeclaration();
4708 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4709 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004710 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004711 return TD->getPreviousDeclaration();
4712 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4713 return FTD->getPreviousDeclaration();
4714 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4715 return CTD->getPreviousDeclaration();
4716 return 0;
4717}
4718
John McCalld226f652010-08-21 09:40:31 +00004719DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004720Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4721 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004722 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004723 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004724 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004725 SourceLocation TemplateNameLoc,
4726 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004727 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004728 SourceLocation RAngleLoc,
4729 AttributeList *Attr,
4730 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004731 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004732
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004733 // NOTE: KWLoc is the location of the tag keyword. This will instead
4734 // store the location of the outermost template keyword in the declaration.
4735 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4736 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4737
Douglas Gregorcc636682009-02-17 23:15:12 +00004738 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004739 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004740 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004741 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4742
4743 if (!ClassTemplate) {
4744 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004745 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004746 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4747 return true;
4748 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004749
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004750 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004751 bool isPartialSpecialization = false;
4752
Douglas Gregor88b70942009-02-25 22:02:03 +00004753 // Check the validity of the template headers that introduce this
4754 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004755 // FIXME: We probably shouldn't complain about these headers for
4756 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004757 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004758 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004759 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4760 TemplateNameLoc,
4761 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004762 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004763 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004764 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004765 isExplicitSpecialization,
4766 Invalid);
4767 if (Invalid)
4768 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004769
Douglas Gregor05396e22009-08-25 17:23:04 +00004770 if (TemplateParams && TemplateParams->size() > 0) {
4771 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004772
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004773 if (TUK == TUK_Friend) {
4774 Diag(KWLoc, diag::err_partial_specialization_friend)
4775 << SourceRange(LAngleLoc, RAngleLoc);
4776 return true;
4777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004778
Douglas Gregor05396e22009-08-25 17:23:04 +00004779 // C++ [temp.class.spec]p10:
4780 // The template parameter list of a specialization shall not
4781 // contain default template argument values.
4782 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4783 Decl *Param = TemplateParams->getParam(I);
4784 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4785 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004786 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004787 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004788 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004789 }
4790 } else if (NonTypeTemplateParmDecl *NTTP
4791 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4792 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004793 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004794 diag::err_default_arg_in_partial_spec)
4795 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004796 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004797 }
4798 } else {
4799 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004800 if (TTP->hasDefaultArgument()) {
4801 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004802 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004803 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004804 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004805 }
4806 }
4807 }
Douglas Gregora735b202009-10-13 14:39:41 +00004808 } else if (TemplateParams) {
4809 if (TUK == TUK_Friend)
4810 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004811 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004812 SourceRange(TemplateParams->getTemplateLoc(),
4813 TemplateParams->getRAngleLoc()))
4814 << SourceRange(LAngleLoc, RAngleLoc);
4815 else
4816 isExplicitSpecialization = true;
4817 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004818 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004819 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004820 isExplicitSpecialization = true;
4821 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004822
Douglas Gregorcc636682009-02-17 23:15:12 +00004823 // Check that the specialization uses the same tag kind as the
4824 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004825 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4826 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004827 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004828 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004829 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004830 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004831 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004832 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004833 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004834 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004835 diag::note_previous_use);
4836 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4837 }
4838
Douglas Gregor40808ce2009-03-09 23:48:35 +00004839 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004840 TemplateArgumentListInfo TemplateArgs;
4841 TemplateArgs.setLAngleLoc(LAngleLoc);
4842 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004843 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004844
Douglas Gregor925910d2011-01-03 20:35:03 +00004845 // Check for unexpanded parameter packs in any of the template arguments.
4846 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004847 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004848 UPPC_PartialSpecialization))
4849 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850
Douglas Gregorcc636682009-02-17 23:15:12 +00004851 // Check that the template argument list is well-formed for this
4852 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004853 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004854 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4855 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004856 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004857
Douglas Gregor910f8002010-11-07 23:05:16 +00004858 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004859 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004860
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004861 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004862 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004863 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004864 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004865 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004866 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004867 return true;
4868
Douglas Gregor561f8122011-07-01 01:22:09 +00004869 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004870 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004871 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004872 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004873 TemplateArgs.size(),
4874 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004875 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4876 << ClassTemplate->getDeclName();
4877 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004878 }
4879 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004880
Douglas Gregorcc636682009-02-17 23:15:12 +00004881 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004882 ClassTemplateSpecializationDecl *PrevDecl = 0;
4883
4884 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004885 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004886 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004887 = ClassTemplate->findPartialSpecialization(Converted.data(),
4888 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004889 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004890 else
4891 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004892 = ClassTemplate->findSpecialization(Converted.data(),
4893 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004894
4895 ClassTemplateSpecializationDecl *Specialization = 0;
4896
Douglas Gregor88b70942009-02-25 22:02:03 +00004897 // Check whether we can declare a class template specialization in
4898 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004899 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004900 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4901 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004902 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004903 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004904
Douglas Gregorb88e8882009-07-30 17:40:51 +00004905 // The canonical type
4906 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004907 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004908 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004909 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004910 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004911 // arguments was referenced but not declared, or we're only
4912 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004913 // declaration node as our own, updating its source location and
4914 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004915 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004916 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004917 if (TemplateParameterLists.size() > 0) {
4918 Specialization->setTemplateParameterListsInfo(Context,
4919 TemplateParameterLists.size(),
4920 (TemplateParameterList**) TemplateParameterLists.release());
4921 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004922 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004923 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004924 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004925 // Build the canonical type that describes the converted template
4926 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004927 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4928 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004929 Converted.data(),
4930 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004931
4932 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004933 ClassTemplate->getInjectedClassNameSpecialization())) {
4934 // C++ [temp.class.spec]p9b3:
4935 //
4936 // -- The argument list of the specialization shall not be identical
4937 // to the implicit argument list of the primary template.
4938 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004939 << (TUK == TUK_Definition)
4940 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004941 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4942 ClassTemplate->getIdentifier(),
4943 TemplateNameLoc,
4944 Attr,
4945 TemplateParams,
Douglas Gregor8d267c52011-09-09 02:06:17 +00004946 AS_none, /*IsModulePrivate=*/false,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004947 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004948 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004949 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004950
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004951 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004952 ClassTemplatePartialSpecializationDecl *PrevPartial
4953 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004954 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004955 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004956 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004957 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004958 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004959 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004960 TemplateParams,
4961 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004962 Converted.data(),
4963 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004964 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004965 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004966 PrevPartial,
4967 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004968 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004969 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004970 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004971 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004972 (TemplateParameterList**) TemplateParameterLists.release());
4973 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004974
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004975 if (!PrevPartial)
4976 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004977 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004978
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004979 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004980 // template specialization, make a note of that.
4981 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4982 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004983
Douglas Gregor031a5882009-06-13 00:26:55 +00004984 // Check that all of the template parameters of the class template
4985 // partial specialization are deducible from the template
4986 // arguments. If not, this class template partial specialization
4987 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004988 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00004989 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004991 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004992 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004993 unsigned NumNonDeducible = 0;
4994 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4995 if (!DeducibleParams[I])
4996 ++NumNonDeducible;
4997
4998 if (NumNonDeducible) {
4999 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5000 << (NumNonDeducible > 1)
5001 << SourceRange(TemplateNameLoc, RAngleLoc);
5002 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5003 if (!DeducibleParams[I]) {
5004 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5005 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005006 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005007 diag::note_partial_spec_unused_parameter)
5008 << Param->getDeclName();
5009 else
Mike Stump1eb44332009-09-09 15:08:12 +00005010 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005011 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005012 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005013 }
5014 }
5015 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005016 } else {
5017 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005018 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005019 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005020 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005021 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005022 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005023 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005024 Converted.data(),
5025 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005026 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005027 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005028 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005029 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005030 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005031 (TemplateParameterList**) TemplateParameterLists.release());
5032 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005033
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005034 if (!PrevDecl)
5035 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005036
5037 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005038 }
5039
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005040 // C++ [temp.expl.spec]p6:
5041 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005042 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005043 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005044 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005045 // use occurs; no diagnostic is required.
5046 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005047 bool Okay = false;
5048 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5049 // Is there any previous explicit specialization declaration?
5050 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5051 Okay = true;
5052 break;
5053 }
5054 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005055
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005056 if (!Okay) {
5057 SourceRange Range(TemplateNameLoc, RAngleLoc);
5058 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5059 << Context.getTypeDeclType(Specialization) << Range;
5060
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005061 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005062 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005063 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005064 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005065 return true;
5066 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005067 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005068
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005069 // If this is not a friend, note that this is an explicit specialization.
5070 if (TUK != TUK_Friend)
5071 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005072
5073 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005074 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005075 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005076 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005077 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005078 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005079 Diag(Def->getLocation(), diag::note_previous_definition);
5080 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005081 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005082 }
5083 }
5084
John McCall7f1b9872010-12-18 03:30:47 +00005085 if (Attr)
5086 ProcessDeclAttributeList(S, Specialization, Attr);
5087
Douglas Gregorfc705b82009-02-26 22:19:44 +00005088 // Build the fully-sugared type for this class template
5089 // specialization as the user wrote in the specialization
5090 // itself. This means that we'll pretty-print the type retrieved
5091 // from the specialization's declaration the way that the user
5092 // actually wrote the specialization, rather than formatting the
5093 // name based on the "canonical" representation used to store the
5094 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005095 TypeSourceInfo *WrittenTy
5096 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5097 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005098 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005099 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005100 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005101 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005102 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005103
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005104 // C++ [temp.expl.spec]p9:
5105 // A template explicit specialization is in the scope of the
5106 // namespace in which the template was defined.
5107 //
5108 // We actually implement this paragraph where we set the semantic
5109 // context (in the creation of the ClassTemplateSpecializationDecl),
5110 // but we also maintain the lexical context where the actual
5111 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005112 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005113
Douglas Gregorcc636682009-02-17 23:15:12 +00005114 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005115 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005116 Specialization->startDefinition();
5117
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005118 if (TUK == TUK_Friend) {
5119 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5120 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005121 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005122 /*FIXME:*/KWLoc);
5123 Friend->setAccess(AS_public);
5124 CurContext->addDecl(Friend);
5125 } else {
5126 // Add the specialization into its lexical context, so that it can
5127 // be seen when iterating through the list of declarations in that
5128 // context. However, specializations are not found by name lookup.
5129 CurContext->addDecl(Specialization);
5130 }
John McCalld226f652010-08-21 09:40:31 +00005131 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005132}
Douglas Gregord57959a2009-03-27 23:10:48 +00005133
John McCalld226f652010-08-21 09:40:31 +00005134Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005135 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005136 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005137 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5138}
5139
John McCalld226f652010-08-21 09:40:31 +00005140Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005141 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005142 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005143 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005144 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005145
Douglas Gregor52591bf2009-06-24 00:54:41 +00005146 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005147 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005148 }
Mike Stump1eb44332009-09-09 15:08:12 +00005149
Douglas Gregor52591bf2009-06-24 00:54:41 +00005150 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005151
John McCalld226f652010-08-21 09:40:31 +00005152 Decl *DP = HandleDeclarator(ParentScope, D,
5153 move(TemplateParameterLists),
5154 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005155 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005156 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005157 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005158 FunctionTemplate->getTemplatedDecl());
5159 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5160 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5161 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005162}
5163
John McCall75042392010-02-11 01:33:53 +00005164/// \brief Strips various properties off an implicit instantiation
5165/// that has just been explicitly specialized.
5166static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005167 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005168
5169 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5170 FD->setInlineSpecified(false);
5171 }
5172}
5173
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005175/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005176/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005177/// new specialization/instantiation will have any effect.
5178///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005180/// instantiation.
5181///
5182/// \param NewTSK the kind of the new explicit specialization or instantiation.
5183///
5184/// \param PrevDecl the previous declaration of the entity.
5185///
5186/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5187///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005188/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005189/// declaration was instantiated (either implicitly or explicitly).
5190///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005191/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005192/// specialization or instantiation has no effect and should be ignored.
5193///
5194/// \returns true if there was an error that should prevent the introduction of
5195/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005196bool
5197Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5198 TemplateSpecializationKind NewTSK,
5199 NamedDecl *PrevDecl,
5200 TemplateSpecializationKind PrevTSK,
5201 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005202 bool &HasNoEffect) {
5203 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204
Douglas Gregor454885e2009-10-15 15:54:05 +00005205 switch (NewTSK) {
5206 case TSK_Undeclared:
5207 case TSK_ImplicitInstantiation:
5208 assert(false && "Don't check implicit instantiations here");
5209 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210
Douglas Gregor454885e2009-10-15 15:54:05 +00005211 case TSK_ExplicitSpecialization:
5212 switch (PrevTSK) {
5213 case TSK_Undeclared:
5214 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005215 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005216 // explicitly specialized or has merely been mentioned without any
5217 // instantiation.
5218 return false;
5219
5220 case TSK_ImplicitInstantiation:
5221 if (PrevPointOfInstantiation.isInvalid()) {
5222 // The declaration itself has not actually been instantiated, so it is
5223 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005224 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005225 return false;
5226 }
5227 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005228
Douglas Gregor454885e2009-10-15 15:54:05 +00005229 case TSK_ExplicitInstantiationDeclaration:
5230 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 assert((PrevTSK == TSK_ImplicitInstantiation ||
5232 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005233 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234
Douglas Gregor454885e2009-10-15 15:54:05 +00005235 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005237 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005239 // implicit instantiation to take place, in every translation unit in
5240 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005241 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5242 // Is there any previous explicit specialization declaration?
5243 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5244 return false;
5245 }
5246
Douglas Gregor0d035142009-10-27 18:42:08 +00005247 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005248 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005249 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005250 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251
Douglas Gregor454885e2009-10-15 15:54:05 +00005252 return true;
5253 }
5254 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005255
Douglas Gregor454885e2009-10-15 15:54:05 +00005256 case TSK_ExplicitInstantiationDeclaration:
5257 switch (PrevTSK) {
5258 case TSK_ExplicitInstantiationDeclaration:
5259 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005260 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005261 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005262
Douglas Gregor454885e2009-10-15 15:54:05 +00005263 case TSK_Undeclared:
5264 case TSK_ImplicitInstantiation:
5265 // We're explicitly instantiating something that may have already been
5266 // implicitly instantiated; that's fine.
5267 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268
Douglas Gregor454885e2009-10-15 15:54:05 +00005269 case TSK_ExplicitSpecialization:
5270 // C++0x [temp.explicit]p4:
5271 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005272 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005273 // specialization for that template, the explicit instantiation has no
5274 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005275 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005276 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005277
Douglas Gregor454885e2009-10-15 15:54:05 +00005278 case TSK_ExplicitInstantiationDefinition:
5279 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005280 // If an entity is the subject of both an explicit instantiation
5281 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005282 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005283 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005284 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005285 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005286 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005287 assert(PrevPointOfInstantiation.isValid() &&
5288 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005289 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005290 return false;
5291 }
5292 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005293
Douglas Gregor454885e2009-10-15 15:54:05 +00005294 case TSK_ExplicitInstantiationDefinition:
5295 switch (PrevTSK) {
5296 case TSK_Undeclared:
5297 case TSK_ImplicitInstantiation:
5298 // We're explicitly instantiating something that may have already been
5299 // implicitly instantiated; that's fine.
5300 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 case TSK_ExplicitSpecialization:
5303 // C++ DR 259, C++0x [temp.explicit]p4:
5304 // For a given set of template parameters, if an explicit
5305 // instantiation of a template appears after a declaration of
5306 // an explicit specialization for that template, the explicit
5307 // instantiation has no effect.
5308 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005309 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005310 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005311 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005312 if (!getLangOptions().CPlusPlus0x) {
5313 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005314 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005315 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005316 diag::note_previous_template_specialization);
5317 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005318 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005319 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005320
Douglas Gregor454885e2009-10-15 15:54:05 +00005321 case TSK_ExplicitInstantiationDeclaration:
5322 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005324 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005325
Douglas Gregor454885e2009-10-15 15:54:05 +00005326 case TSK_ExplicitInstantiationDefinition:
5327 // C++0x [temp.spec]p5:
5328 // For a given template and a given set of template-arguments,
5329 // - an explicit instantiation definition shall appear at most once
5330 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005331 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005332 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005333 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005334 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005335 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005336 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005337 }
5338 break;
5339 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregor454885e2009-10-15 15:54:05 +00005341 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005342
Douglas Gregor454885e2009-10-15 15:54:05 +00005343 return false;
5344}
5345
John McCallaf2094e2010-04-08 09:05:18 +00005346/// \brief Perform semantic analysis for the given dependent function
5347/// template specialization. The only possible way to get a dependent
5348/// function template specialization is with a friend declaration,
5349/// like so:
5350///
5351/// template <class T> void foo(T);
5352/// template <class T> class A {
5353/// friend void foo<>(T);
5354/// };
5355///
5356/// There really isn't any useful analysis we can do here, so we
5357/// just store the information.
5358bool
5359Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5360 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5361 LookupResult &Previous) {
5362 // Remove anything from Previous that isn't a function template in
5363 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005364 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005365 LookupResult::Filter F = Previous.makeFilter();
5366 while (F.hasNext()) {
5367 NamedDecl *D = F.next()->getUnderlyingDecl();
5368 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005369 !FDLookupContext->InEnclosingNamespaceSetOf(
5370 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005371 F.erase();
5372 }
5373 F.done();
5374
5375 // Should this be diagnosed here?
5376 if (Previous.empty()) return true;
5377
5378 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5379 ExplicitTemplateArgs);
5380 return false;
5381}
5382
Abramo Bagnarae03db982010-05-20 15:32:11 +00005383/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005384/// specialization.
5385///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005386/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005387/// explicit function template specialization. On successful completion,
5388/// the function declaration \p FD will become a function template
5389/// specialization.
5390///
5391/// \param FD the function declaration, which will be updated to become a
5392/// function template specialization.
5393///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005394/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5395/// if any. Note that this may be valid info even when 0 arguments are
5396/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5397/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005398///
Francois Pichet59e7c562011-07-08 06:21:47 +00005399/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005400/// this function specialization.
5401bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005402Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005403 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005404 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005405 // The set of function template specializations that could match this
5406 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005407 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408
Sebastian Redl7a126a42010-08-31 00:36:30 +00005409 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005410 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5411 I != E; ++I) {
5412 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5413 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005414 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005415 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005416 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5417 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005418 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005419
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005420 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005421 // A trailing template-argument can be left unspecified in the
5422 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005423 // provided it can be deduced from the function argument type.
5424 // Perform template argument deduction to determine whether we may be
5425 // specializing this template.
5426 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005427 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005428 FunctionDecl *Specialization = 0;
5429 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005430 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005431 FD->getType(),
5432 Specialization,
5433 Info)) {
5434 // FIXME: Template argument deduction failed; record why it failed, so
5435 // that we can provide nifty diagnostics.
5436 (void)TDK;
5437 continue;
5438 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005439
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005440 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005441 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005442 }
5443 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005444
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005445 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005446 UnresolvedSetIterator Result
5447 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005448 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005450 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005451 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005452 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005453 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005454 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005455 return true;
John McCallc373d482010-01-27 01:50:18 +00005456
5457 // Ignore access information; it doesn't figure into redeclaration checking.
5458 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005459
5460 FunctionTemplateSpecializationInfo *SpecInfo
5461 = Specialization->getTemplateSpecializationInfo();
5462 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005463
5464 // Note: do not overwrite location info if previous template
5465 // specialization kind was explicit.
5466 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5467 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5468 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005469
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005470 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005471 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005472
5473 // If this is a friend declaration, then we're not really declaring
5474 // an explicit specialization.
5475 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476
Douglas Gregord5cb8762009-10-07 00:13:32 +00005477 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005478 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005479 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005480 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005482 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005483 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005484
5485 // C++ [temp.expl.spec]p6:
5486 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005487 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005488 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005489 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005490 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005491 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005492 if (!isFriend &&
5493 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005494 TSK_ExplicitSpecialization,
5495 Specialization,
5496 SpecInfo->getTemplateSpecializationKind(),
5497 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005498 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005499 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005500
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005501 // Mark the prior declaration as an explicit specialization, so that later
5502 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005503 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005504 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005505 MarkUnusedFileScopedDecl(Specialization);
5506 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005507
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005508 // Turn the given function declaration into a function template
5509 // specialization, with the template arguments from the previous
5510 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005511 // Take copies of (semantic and syntactic) template argument lists.
5512 const TemplateArgumentList* TemplArgs = new (Context)
5513 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5514 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5515 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005516 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005517 TemplArgs, /*InsertPos=*/0,
5518 SpecInfo->getTemplateSpecializationKind(),
5519 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005520 FD->setStorageClass(Specialization->getStorageClass());
5521
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005522 // The "previous declaration" for this function template specialization is
5523 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005524 Previous.clear();
5525 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005526 return false;
5527}
5528
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005529/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005530/// specialization.
5531///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005532/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005533/// explicit member function specialization. On successful completion,
5534/// the function declaration \p FD will become a member function
5535/// specialization.
5536///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005537/// \param Member the member declaration, which will be updated to become a
5538/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005539///
John McCall68263142009-11-18 22:49:29 +00005540/// \param Previous the set of declarations, one of which may be specialized
5541/// by this function specialization; the set will be modified to contain the
5542/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543bool
John McCall68263142009-11-18 22:49:29 +00005544Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005545 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005546
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005547 // Try to find the member we are instantiating.
5548 NamedDecl *Instantiation = 0;
5549 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005550 MemberSpecializationInfo *MSInfo = 0;
5551
John McCall68263142009-11-18 22:49:29 +00005552 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005553 // Nowhere to look anyway.
5554 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005555 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5556 I != E; ++I) {
5557 NamedDecl *D = (*I)->getUnderlyingDecl();
5558 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005559 if (Context.hasSameType(Function->getType(), Method->getType())) {
5560 Instantiation = Method;
5561 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005562 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005563 break;
5564 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005565 }
5566 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005567 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005568 VarDecl *PrevVar;
5569 if (Previous.isSingleResult() &&
5570 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005571 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005572 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005573 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005574 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005575 }
5576 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005577 CXXRecordDecl *PrevRecord;
5578 if (Previous.isSingleResult() &&
5579 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5580 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005581 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005582 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005583 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005584 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005585
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005586 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005587 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005588 // specializations are always out-of-line, the caller will complain about
5589 // this mismatch later.
5590 return false;
5591 }
John McCall77e8b112010-04-13 20:37:33 +00005592
5593 // If this is a friend, just bail out here before we start turning
5594 // things into explicit specializations.
5595 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5596 // Preserve instantiation information.
5597 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5598 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5599 cast<CXXMethodDecl>(InstantiatedFrom),
5600 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5601 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5602 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5603 cast<CXXRecordDecl>(InstantiatedFrom),
5604 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5605 }
5606
5607 Previous.clear();
5608 Previous.addDecl(Instantiation);
5609 return false;
5610 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005611
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005612 // Make sure that this is a specialization of a member.
5613 if (!InstantiatedFrom) {
5614 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5615 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005616 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5617 return true;
5618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005619
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005620 // C++ [temp.expl.spec]p6:
5621 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005622 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005623 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005624 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005625 // use occurs; no diagnostic is required.
5626 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005627
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005628 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005629 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5630 TSK_ExplicitSpecialization,
5631 Instantiation,
5632 MSInfo->getTemplateSpecializationKind(),
5633 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005634 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005635 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005636
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005637 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005638 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005639 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005640 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005641 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005642 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005643
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005644 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005645 // the original declaration to note that it is an explicit specialization
5646 // (if it was previously an implicit instantiation). This latter step
5647 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005648 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005649 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5650 if (InstantiationFunction->getTemplateSpecializationKind() ==
5651 TSK_ImplicitInstantiation) {
5652 InstantiationFunction->setTemplateSpecializationKind(
5653 TSK_ExplicitSpecialization);
5654 InstantiationFunction->setLocation(Member->getLocation());
5655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005656
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005657 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5658 cast<CXXMethodDecl>(InstantiatedFrom),
5659 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005660 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005661 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005662 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5663 if (InstantiationVar->getTemplateSpecializationKind() ==
5664 TSK_ImplicitInstantiation) {
5665 InstantiationVar->setTemplateSpecializationKind(
5666 TSK_ExplicitSpecialization);
5667 InstantiationVar->setLocation(Member->getLocation());
5668 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005669
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005670 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5671 cast<VarDecl>(InstantiatedFrom),
5672 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005673 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005674 } else {
5675 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005676 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5677 if (InstantiationClass->getTemplateSpecializationKind() ==
5678 TSK_ImplicitInstantiation) {
5679 InstantiationClass->setTemplateSpecializationKind(
5680 TSK_ExplicitSpecialization);
5681 InstantiationClass->setLocation(Member->getLocation());
5682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005684 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005685 cast<CXXRecordDecl>(InstantiatedFrom),
5686 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005688
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005689 // Save the caller the trouble of having to figure out which declaration
5690 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005691 Previous.clear();
5692 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005693 return false;
5694}
5695
Douglas Gregor558c0322009-10-14 23:41:34 +00005696/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005697///
5698/// \returns true if a serious error occurs, false otherwise.
5699static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005700 SourceLocation InstLoc,
5701 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005702 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5703 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005704
Douglas Gregor669eed82010-07-13 00:10:04 +00005705 if (CurContext->isRecord()) {
5706 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5707 << D;
5708 return true;
5709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710
Douglas Gregor558c0322009-10-14 23:41:34 +00005711 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005712 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005713 // template.
5714 //
5715 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005716 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005717 !CurContext->Encloses(OrigContext)) {
5718 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005719 S.Diag(InstLoc,
5720 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005721 diag::err_explicit_instantiation_out_of_scope
5722 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005723 << D << NS;
5724 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005725 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005726 S.getLangOptions().CPlusPlus0x?
5727 diag::err_explicit_instantiation_must_be_global
5728 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005729 << D;
5730 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005731 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005732 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005733
Douglas Gregor558c0322009-10-14 23:41:34 +00005734 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005735 // If the name declared in the explicit instantiation is an unqualified
5736 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005737 // its template is declared or, if that namespace is inline (7.3.1), any
5738 // namespace from its enclosing namespace set.
5739 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005740 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005741
5742 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005743 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005744
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005745 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005746 S.getLangOptions().CPlusPlus0x?
5747 diag::err_explicit_instantiation_unqualified_wrong_namespace
5748 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005749 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005750 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005751 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005752}
5753
5754/// \brief Determine whether the given scope specifier has a template-id in it.
5755static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5756 if (!SS.isSet())
5757 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005758
Douglas Gregor558c0322009-10-14 23:41:34 +00005759 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005760 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005761 // or a static data member of a class template specialization, the name of
5762 // the class template specialization in the qualified-id for the member
5763 // name shall be a simple-template-id.
5764 //
5765 // C++98 has the same restriction, just worded differently.
5766 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5767 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005768 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005769 if (isa<TemplateSpecializationType>(T))
5770 return true;
5771
5772 return false;
5773}
5774
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005775// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005776DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005777Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005778 SourceLocation ExternLoc,
5779 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005780 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005781 SourceLocation KWLoc,
5782 const CXXScopeSpec &SS,
5783 TemplateTy TemplateD,
5784 SourceLocation TemplateNameLoc,
5785 SourceLocation LAngleLoc,
5786 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005787 SourceLocation RAngleLoc,
5788 AttributeList *Attr) {
5789 // Find the class template we're specializing
5790 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005791 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005792 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5793
5794 // Check that the specialization uses the same tag kind as the
5795 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005796 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5797 assert(Kind != TTK_Enum &&
5798 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005799 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005800 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005801 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005802 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005803 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005804 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005805 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005806 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005807 diag::note_previous_use);
5808 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5809 }
5810
Douglas Gregor558c0322009-10-14 23:41:34 +00005811 // C++0x [temp.explicit]p2:
5812 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813 // definition and an explicit instantiation declaration. An explicit
5814 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005815 TemplateSpecializationKind TSK
5816 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5817 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005818
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005819 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005820 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005821 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005822
5823 // Check that the template argument list is well-formed for this
5824 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005825 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005826 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5827 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005828 return true;
5829
Douglas Gregor910f8002010-11-07 23:05:16 +00005830 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005831 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005832
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005833 // Find the class template specialization declaration that
5834 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005835 void *InsertPos = 0;
5836 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005837 = ClassTemplate->findSpecialization(Converted.data(),
5838 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005839
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005840 TemplateSpecializationKind PrevDecl_TSK
5841 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5842
Douglas Gregord5cb8762009-10-07 00:13:32 +00005843 // C++0x [temp.explicit]p2:
5844 // [...] An explicit instantiation shall appear in an enclosing
5845 // namespace of its template. [...]
5846 //
5847 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005848 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5849 SS.isSet()))
5850 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005851
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005852 ClassTemplateSpecializationDecl *Specialization = 0;
5853
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005854 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005855 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005856 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005857 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005858 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005859 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005860 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005861
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005862 // Even though HasNoEffect == true means that this explicit instantiation
5863 // has no effect on semantics, we go on to put its syntax in the AST.
5864
5865 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5866 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005867 // Since the only prior class template specialization with these
5868 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005869 // declaration node as our own, updating the source location
5870 // for the template name to reflect our new declaration.
5871 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005872 Specialization = PrevDecl;
5873 Specialization->setLocation(TemplateNameLoc);
5874 PrevDecl = 0;
5875 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005876 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005877
Douglas Gregor52604ab2009-09-11 21:19:12 +00005878 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005879 // Create a new class template specialization declaration node for
5880 // this explicit specialization.
5881 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005882 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005883 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005884 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005885 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005886 Converted.data(),
5887 Converted.size(),
5888 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005889 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005890
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005891 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005892 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005893 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005894 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005895 }
5896
5897 // Build the fully-sugared type for this explicit instantiation as
5898 // the user wrote in the explicit instantiation itself. This means
5899 // that we'll pretty-print the type retrieved from the
5900 // specialization's declaration the way that the user actually wrote
5901 // the explicit instantiation, rather than formatting the name based
5902 // on the "canonical" representation used to store the template
5903 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005904 TypeSourceInfo *WrittenTy
5905 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5906 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005907 Context.getTypeDeclType(Specialization));
5908 Specialization->setTypeAsWritten(WrittenTy);
5909 TemplateArgsIn.release();
5910
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005911 // Set source locations for keywords.
5912 Specialization->setExternLoc(ExternLoc);
5913 Specialization->setTemplateKeywordLoc(TemplateLoc);
5914
5915 // Add the explicit instantiation into its lexical context. However,
5916 // since explicit instantiations are never found by name lookup, we
5917 // just put it into the declaration context directly.
5918 Specialization->setLexicalDeclContext(CurContext);
5919 CurContext->addDecl(Specialization);
5920
5921 // Syntax is now OK, so return if it has no other effect on semantics.
5922 if (HasNoEffect) {
5923 // Set the template specialization kind.
5924 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005925 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005926 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005927
5928 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005929 // A definition of a class template or class member template
5930 // shall be in scope at the point of the explicit instantiation of
5931 // the class template or class member template.
5932 //
5933 // This check comes when we actually try to perform the
5934 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005935 ClassTemplateSpecializationDecl *Def
5936 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005937 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005938 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005939 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005940 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005941 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005942 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5943 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005944
Douglas Gregor0d035142009-10-27 18:42:08 +00005945 // Instantiate the members of this class template specialization.
5946 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005947 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005948 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005949 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5950
5951 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5952 // TSK_ExplicitInstantiationDefinition
5953 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5954 TSK == TSK_ExplicitInstantiationDefinition)
5955 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005956
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005957 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005958 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005959
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005960 // Set the template specialization kind.
5961 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005962 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005963}
5964
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005965// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005966DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005967Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005968 SourceLocation ExternLoc,
5969 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005970 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005971 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005972 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005973 IdentifierInfo *Name,
5974 SourceLocation NameLoc,
5975 AttributeList *Attr) {
5976
Douglas Gregor402abb52009-05-28 23:31:59 +00005977 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005978 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005979 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005980 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregor8d267c52011-09-09 02:06:17 +00005981 /*IsModulePrivate=*/false,
John McCalld226f652010-08-21 09:40:31 +00005982 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005983 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005984 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005985 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5986
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005987 if (!TagD)
5988 return true;
5989
John McCalld226f652010-08-21 09:40:31 +00005990 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005991 if (Tag->isEnum()) {
5992 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5993 << Context.getTypeDeclType(Tag);
5994 return true;
5995 }
5996
Douglas Gregord0c87372009-05-27 17:30:49 +00005997 if (Tag->isInvalidDecl())
5998 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005999
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006000 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6001 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6002 if (!Pattern) {
6003 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6004 << Context.getTypeDeclType(Record);
6005 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6006 return true;
6007 }
6008
Douglas Gregor558c0322009-10-14 23:41:34 +00006009 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010 // If the explicit instantiation is for a class or member class, the
6011 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006012 // simple-template-id.
6013 //
6014 // C++98 has the same restriction, just worded differently.
6015 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006016 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006017 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006018
Douglas Gregor558c0322009-10-14 23:41:34 +00006019 // C++0x [temp.explicit]p2:
6020 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006021 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006022 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006023 TemplateSpecializationKind TSK
6024 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6025 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006026
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006027 // C++0x [temp.explicit]p2:
6028 // [...] An explicit instantiation shall appear in an enclosing
6029 // namespace of its template. [...]
6030 //
6031 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006032 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006033
Douglas Gregor454885e2009-10-15 15:54:05 +00006034 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006035 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006036 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006037 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006038 PrevDecl = Record;
6039 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006040 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006041 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006042 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006043 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006044 PrevDecl,
6045 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006047 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006048 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006049 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006050 return TagD;
6051 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006052
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006053 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006054 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006055 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006056 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006057 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006058 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006059 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006060 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006061 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006062 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6063 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006064 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6065 << Pattern;
6066 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006067 } else {
6068 if (InstantiateClass(NameLoc, Record, Def,
6069 getTemplateInstantiationArgs(Record),
6070 TSK))
6071 return true;
6072
Douglas Gregor952b0172010-02-11 01:04:33 +00006073 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006074 if (!RecordDef)
6075 return true;
6076 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006077 }
6078
Douglas Gregor0d035142009-10-27 18:42:08 +00006079 // Instantiate all of the members of the class.
6080 InstantiateClassMembers(NameLoc, RecordDef,
6081 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006082
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006083 if (TSK == TSK_ExplicitInstantiationDefinition)
6084 MarkVTableUsed(NameLoc, RecordDef, true);
6085
Mike Stump390b4cc2009-05-16 07:39:55 +00006086 // FIXME: We don't have any representation for explicit instantiations of
6087 // member classes. Such a representation is not needed for compilation, but it
6088 // should be available for clients that want to see all of the declarations in
6089 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006090 return TagD;
6091}
6092
John McCallf312b1e2010-08-26 23:41:50 +00006093DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6094 SourceLocation ExternLoc,
6095 SourceLocation TemplateLoc,
6096 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006097 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006098 // TODO: check if/when DNInfo should replace Name.
6099 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6100 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006101 if (!Name) {
6102 if (!D.isInvalidType())
6103 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6104 diag::err_explicit_instantiation_requires_name)
6105 << D.getDeclSpec().getSourceRange()
6106 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006107
Douglas Gregord5a423b2009-09-25 18:43:00 +00006108 return true;
6109 }
6110
6111 // The scope passed in may not be a decl scope. Zip up the scope tree until
6112 // we find one that is.
6113 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6114 (S->getFlags() & Scope::TemplateParamScope) != 0)
6115 S = S->getParent();
6116
6117 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006118 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6119 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006120 if (R.isNull())
6121 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006122
Douglas Gregore885e182011-05-21 18:53:30 +00006123 // C++ [dcl.stc]p1:
6124 // A storage-class-specifier shall not be specified in [...] an explicit
6125 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006126 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006127 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6128 << Name;
6129 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006130 } else if (D.getDeclSpec().getStorageClassSpec()
6131 != DeclSpec::SCS_unspecified) {
6132 // Complain about then remove the storage class specifier.
6133 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6134 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6135
6136 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006137 }
6138
Douglas Gregor663b5a02009-10-14 20:14:33 +00006139 // C++0x [temp.explicit]p1:
6140 // [...] An explicit instantiation of a function template shall not use the
6141 // inline or constexpr specifiers.
6142 // Presumably, this also applies to member functions of class templates as
6143 // well.
6144 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006145 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006146 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006147 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006148
Douglas Gregor663b5a02009-10-14 20:14:33 +00006149 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006150
Douglas Gregor558c0322009-10-14 23:41:34 +00006151 // C++0x [temp.explicit]p2:
6152 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006153 // definition and an explicit instantiation declaration. An explicit
6154 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006155 TemplateSpecializationKind TSK
6156 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6157 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006158
Abramo Bagnara25777432010-08-11 22:01:17 +00006159 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006160 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006161
6162 if (!R->isFunctionType()) {
6163 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006164 // A [...] static data member of a class template can be explicitly
6165 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006166 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006167 if (Previous.isAmbiguous())
6168 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006169
John McCall1bcee0a2009-12-02 08:25:40 +00006170 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006171 if (!Prev || !Prev->isStaticDataMember()) {
6172 // We expect to see a data data member here.
6173 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6174 << Name;
6175 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6176 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006177 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006178 return true;
6179 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006180
Douglas Gregord5a423b2009-09-25 18:43:00 +00006181 if (!Prev->getInstantiatedFromStaticDataMember()) {
6182 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006183 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006184 diag::err_explicit_instantiation_data_member_not_instantiated)
6185 << Prev;
6186 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6187 // FIXME: Can we provide a note showing where this was declared?
6188 return true;
6189 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006190
Douglas Gregor558c0322009-10-14 23:41:34 +00006191 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006192 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006193 // or a static data member of a class template specialization, the name of
6194 // the class template specialization in the qualified-id for the member
6195 // name shall be a simple-template-id.
6196 //
6197 // C++98 has the same restriction, just worded differently.
6198 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006199 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006200 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006201 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006202
Douglas Gregor558c0322009-10-14 23:41:34 +00006203 // Check the scope of this explicit instantiation.
6204 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006205
Douglas Gregor454885e2009-10-15 15:54:05 +00006206 // Verify that it is okay to explicitly instantiate here.
6207 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6208 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006209 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006210 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006211 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006212 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006213 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006214 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006215 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006216 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006217
Douglas Gregord5a423b2009-09-25 18:43:00 +00006218 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006219 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006220 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006221 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006222
Douglas Gregord5a423b2009-09-25 18:43:00 +00006223 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006224 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006226
6227 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006228 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006229 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006230 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006231 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6232 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006233 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6234 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006235 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6236 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006237 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006238 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006239 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006240 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242
Douglas Gregord5a423b2009-09-25 18:43:00 +00006243 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006244 // A [...] function [...] can be explicitly instantiated from its template.
6245 // A member function [...] of a class template can be explicitly
6246 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006247 // template.
John McCallc373d482010-01-27 01:50:18 +00006248 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006249 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6250 P != PEnd; ++P) {
6251 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006252 if (!HasExplicitTemplateArgs) {
6253 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6254 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6255 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006256
John McCallc373d482010-01-27 01:50:18 +00006257 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006258 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6259 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006260 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006261 }
6262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006263
Douglas Gregord5a423b2009-09-25 18:43:00 +00006264 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6265 if (!FunTmpl)
6266 continue;
6267
John McCall5769d612010-02-08 23:07:23 +00006268 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006269 FunctionDecl *Specialization = 0;
6270 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006271 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006272 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006273 R, Specialization, Info)) {
6274 // FIXME: Keep track of almost-matches?
6275 (void)TDK;
6276 continue;
6277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278
John McCallc373d482010-01-27 01:50:18 +00006279 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006281
Douglas Gregord5a423b2009-09-25 18:43:00 +00006282 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006283 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006284 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006285 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006286 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6287 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6288 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006289
John McCallc373d482010-01-27 01:50:18 +00006290 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006291 return true;
John McCallc373d482010-01-27 01:50:18 +00006292
6293 // Ignore access control bits, we don't need them for redeclaration checking.
6294 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006295
Douglas Gregor0a897e32009-10-15 17:21:20 +00006296 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006297 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006298 diag::err_explicit_instantiation_member_function_not_instantiated)
6299 << Specialization
6300 << (Specialization->getTemplateSpecializationKind() ==
6301 TSK_ExplicitSpecialization);
6302 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6303 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006304 }
6305
Douglas Gregor0a897e32009-10-15 17:21:20 +00006306 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006307 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6308 PrevDecl = Specialization;
6309
Douglas Gregor0a897e32009-10-15 17:21:20 +00006310 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006311 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006312 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006313 PrevDecl,
6314 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006315 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006316 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006317 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006318
Douglas Gregor0a897e32009-10-15 17:21:20 +00006319 // FIXME: We may still want to build some representation of this
6320 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006321 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006322 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006323 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006324
6325 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006326
Douglas Gregor0a897e32009-10-15 17:21:20 +00006327 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006328 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329
Douglas Gregor558c0322009-10-14 23:41:34 +00006330 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006331 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006332 // or a static data member of a class template specialization, the name of
6333 // the class template specialization in the qualified-id for the member
6334 // name shall be a simple-template-id.
6335 //
6336 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006337 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006338 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006339 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006340 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006341 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006342 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006343 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006344
Douglas Gregor558c0322009-10-14 23:41:34 +00006345 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006346 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006347 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006348 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006349 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006350
Douglas Gregord5a423b2009-09-25 18:43:00 +00006351 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006352 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006353}
6354
John McCallf312b1e2010-08-26 23:41:50 +00006355TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006356Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6357 const CXXScopeSpec &SS, IdentifierInfo *Name,
6358 SourceLocation TagLoc, SourceLocation NameLoc) {
6359 // This has to hold, because SS is expected to be defined.
6360 assert(Name && "Expected a name in a dependent tag");
6361
6362 NestedNameSpecifier *NNS
6363 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6364 if (!NNS)
6365 return true;
6366
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006367 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006368
Douglas Gregor48c89f42010-04-24 16:38:41 +00006369 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6370 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006371 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006372 return true;
6373 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006374
Douglas Gregor059101f2011-03-02 00:47:37 +00006375 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006376 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006377 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6378
6379 // Create type-source location information for this type.
6380 TypeLocBuilder TLB;
6381 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6382 TL.setKeywordLoc(TagLoc);
6383 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6384 TL.setNameLoc(NameLoc);
6385 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006386}
6387
John McCallf312b1e2010-08-26 23:41:50 +00006388TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006389Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6390 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006391 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006392 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006393 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006394
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006395 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6396 !getLangOptions().CPlusPlus0x)
6397 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006398 << FixItHint::CreateRemoval(TypenameLoc);
6399
Douglas Gregor2494dd02011-03-01 01:34:45 +00006400 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006401 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6402 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006403 if (T.isNull())
6404 return true;
John McCall63b43852010-04-29 23:50:39 +00006405
6406 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6407 if (isa<DependentNameType>(T)) {
6408 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006409 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006410 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006411 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006412 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006413 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006414 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006415 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006416 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006417 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006418
John McCallb3d87482010-08-24 05:47:05 +00006419 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006420}
6421
John McCallf312b1e2010-08-26 23:41:50 +00006422TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006423Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6424 const CXXScopeSpec &SS,
6425 SourceLocation TemplateLoc,
6426 TemplateTy TemplateIn,
6427 SourceLocation TemplateNameLoc,
6428 SourceLocation LAngleLoc,
6429 ASTTemplateArgsPtr TemplateArgsIn,
6430 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006431 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6432 !getLangOptions().CPlusPlus0x)
6433 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006434 << FixItHint::CreateRemoval(TypenameLoc);
6435
6436 // Translate the parser's template argument list in our AST format.
6437 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6438 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6439
6440 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006441 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6442 // Construct a dependent template specialization type.
6443 assert(DTN && "dependent template has non-dependent name?");
6444 assert(DTN->getQualifier()
6445 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6446 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6447 DTN->getQualifier(),
6448 DTN->getIdentifier(),
6449 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006450
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006451 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006452 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006453 DependentTemplateSpecializationTypeLoc SpecTL
6454 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006455 SpecTL.setLAngleLoc(LAngleLoc);
6456 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006457 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006458 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006459 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006460 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6461 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006462 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006463 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006464
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006465 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6466 if (T.isNull())
6467 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006468
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006469 // Provide source-location information for the template specialization
6470 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006471 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006472 TemplateSpecializationTypeLoc SpecTL
6473 = Builder.push<TemplateSpecializationTypeLoc>(T);
6474
6475 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006476 SpecTL.setLAngleLoc(LAngleLoc);
6477 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006478 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006479 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6480 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6481
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006482 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6483 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6484 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006485 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6486
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006487 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6488 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006489}
6490
Douglas Gregora02411e2011-02-27 22:46:49 +00006491
Douglas Gregord57959a2009-03-27 23:10:48 +00006492/// \brief Build the type that describes a C++ typename specifier,
6493/// e.g., "typename T::type".
6494QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006495Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6496 SourceLocation KeywordLoc,
6497 NestedNameSpecifierLoc QualifierLoc,
6498 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006499 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006500 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006501 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006502
John McCall77bb1aa2010-05-01 00:40:08 +00006503 DeclContext *Ctx = computeDeclContext(SS);
6504 if (!Ctx) {
6505 // If the nested-name-specifier is dependent and couldn't be
6506 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006507 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6508 return Context.getDependentNameType(Keyword,
6509 QualifierLoc.getNestedNameSpecifier(),
6510 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006511 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006512
John McCall77bb1aa2010-05-01 00:40:08 +00006513 // If the nested-name-specifier refers to the current instantiation,
6514 // the "typename" keyword itself is superfluous. In C++03, the
6515 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6516 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006517 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006518
John McCall77bb1aa2010-05-01 00:40:08 +00006519 if (RequireCompleteDeclContext(SS, Ctx))
6520 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006521
6522 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006523 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006524 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006525 unsigned DiagID = 0;
6526 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006527 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006528 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006529 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006530 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006531
6532 case LookupResult::FoundUnresolvedValue: {
6533 // We found a using declaration that is a value. Most likely, the using
6534 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006535 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006536 IILoc);
6537 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6538 << Name << Ctx << FullRange;
6539 if (UnresolvedUsingValueDecl *Using
6540 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006541 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006542 Diag(Loc, diag::note_using_value_decl_missing_typename)
6543 << FixItHint::CreateInsertion(Loc, "typename ");
6544 }
6545 }
6546 // Fall through to create a dependent typename type, from which we can recover
6547 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006548
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006549 case LookupResult::NotFoundInCurrentInstantiation:
6550 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006551 return Context.getDependentNameType(Keyword,
6552 QualifierLoc.getNestedNameSpecifier(),
6553 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006554
6555 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006556 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006557 // We found a type. Build an ElaboratedType, since the
6558 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006559 return Context.getElaboratedType(ETK_Typename,
6560 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006561 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006562 }
6563
6564 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006565 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006566 break;
6567
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006568
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006569 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006570 return QualType();
6571
Douglas Gregord57959a2009-03-27 23:10:48 +00006572 case LookupResult::FoundOverloaded:
6573 DiagID = diag::err_typename_nested_not_type;
6574 Referenced = *Result.begin();
6575 break;
6576
John McCall6e247262009-10-10 05:48:19 +00006577 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006578 return QualType();
6579 }
6580
6581 // If we get here, it's because name lookup did not find a
6582 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006583 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006584 IILoc);
6585 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006586 if (Referenced)
6587 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6588 << Name;
6589 return QualType();
6590}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006591
6592namespace {
6593 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006594 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006595 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006596 SourceLocation Loc;
6597 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006598
Douglas Gregor4a959d82009-08-06 16:20:37 +00006599 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006600 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006601
Mike Stump1eb44332009-09-09 15:08:12 +00006602 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006603 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006604 DeclarationName Entity)
6605 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006606 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006607
6608 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006609 /// transformed.
6610 ///
6611 /// For the purposes of type reconstruction, a type has already been
6612 /// transformed if it is NULL or if it is not dependent.
6613 bool AlreadyTransformed(QualType T) {
6614 return T.isNull() || !T->isDependentType();
6615 }
Mike Stump1eb44332009-09-09 15:08:12 +00006616
6617 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006618 /// rebuilt.
6619 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006620
Douglas Gregor4a959d82009-08-06 16:20:37 +00006621 /// \brief Returns the name of the entity whose type is being rebuilt.
6622 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006623
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006624 /// \brief Sets the "base" location and entity when that
6625 /// information is known based on another transformation.
6626 void setBase(SourceLocation Loc, DeclarationName Entity) {
6627 this->Loc = Loc;
6628 this->Entity = Entity;
6629 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006630 };
6631}
6632
Douglas Gregor4a959d82009-08-06 16:20:37 +00006633/// \brief Rebuilds a type within the context of the current instantiation.
6634///
Mike Stump1eb44332009-09-09 15:08:12 +00006635/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006636/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006637/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006638/// partial specialization thereof). This routine will rebuild that type now
6639/// that we have entered the declarator's scope, which may produce different
6640/// canonical types, e.g.,
6641///
6642/// \code
6643/// template<typename T>
6644/// struct X {
6645/// typedef T* pointer;
6646/// pointer data();
6647/// };
6648///
6649/// template<typename T>
6650/// typename X<T>::pointer X<T>::data() { ... }
6651/// \endcode
6652///
Douglas Gregor4714c122010-03-31 17:34:00 +00006653/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006654/// since we do not know that we can look into X<T> when we parsed the type.
6655/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006656/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006657/// as the canonical type of T*, allowing the return types of the out-of-line
6658/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006659TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6660 SourceLocation Loc,
6661 DeclarationName Name) {
6662 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006663 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006664
Douglas Gregor4a959d82009-08-06 16:20:37 +00006665 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6666 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006667}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006668
John McCall60d7b3a2010-08-24 06:29:42 +00006669ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006670 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6671 DeclarationName());
6672 return Rebuilder.TransformExpr(E);
6673}
6674
John McCall63b43852010-04-29 23:50:39 +00006675bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006676 if (SS.isInvalid())
6677 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006678
Douglas Gregor7e384942011-02-25 16:07:42 +00006679 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006680 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6681 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006682 NestedNameSpecifierLoc Rebuilt
6683 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6684 if (!Rebuilt)
6685 return true;
John McCall63b43852010-04-29 23:50:39 +00006686
Douglas Gregor7e384942011-02-25 16:07:42 +00006687 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006688 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006689}
6690
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006691/// \brief Produces a formatted string that describes the binding of
6692/// template parameters to template arguments.
6693std::string
6694Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6695 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006696 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006697}
6698
6699std::string
6700Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6701 const TemplateArgument *Args,
6702 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006703 llvm::SmallString<128> Str;
6704 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006705
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006706 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006707 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006708
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006709 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006710 if (I >= NumArgs)
6711 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006712
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006713 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006714 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006715 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006716 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006717
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006718 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006719 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006720 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006721 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006722 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006723
Douglas Gregor87dd6972010-12-20 16:52:59 +00006724 Out << " = ";
6725 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006726 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006727
6728 Out << ']';
6729 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006730}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006731
6732void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6733 if (!FD)
6734 return;
6735 FD->setLateTemplateParsed(Flag);
6736}
6737
6738bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6739 DeclContext *DC = CurContext;
6740
6741 while (DC) {
6742 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6743 const FunctionDecl *FD = RD->isLocalClass();
6744 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6745 } else if (DC->isTranslationUnit() || DC->isNamespace())
6746 return false;
6747
6748 DC = DC->getParent();
6749 }
6750 return false;
6751}