blob: a7d74ce7e84cef2178e3c5aedf92cf112e80761f [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,
717 TemplateParamsTy *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.
788Sema::TemplateParamsTy *
789Sema::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,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000814 AccessSpecifier AS,
815 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 }
948 }
949 } 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);
1002
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001003 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001004 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001005 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001006 assert(T->isDependentType() && "Class template type is not dependent?");
1007 (void)T;
1008
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001009 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001010 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001011 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001012 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1013 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001014
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001015 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001016 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001017 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregorddc29e12009-02-06 22:42:48 +00001019 // Set the lexical context of these templates
1020 NewClass->setLexicalDeclContext(CurContext);
1021 NewTemplate->setLexicalDeclContext(CurContext);
1022
John McCall0f434ec2009-07-31 02:45:11 +00001023 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001024 NewClass->startDefinition();
1025
1026 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001027 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001028
John McCall05b23ea2009-09-14 21:59:20 +00001029 if (TUK != TUK_Friend)
1030 PushOnScopeChains(NewTemplate, S);
1031 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001032 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001033 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001034 NewClass->setAccess(PrevClassTemplate->getAccess());
1035 }
John McCall05b23ea2009-09-14 21:59:20 +00001036
Douglas Gregord85bea22009-09-26 06:47:28 +00001037 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1038 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039
John McCall05b23ea2009-09-14 21:59:20 +00001040 // Friend templates are visible in fairly strange ways.
1041 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001042 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001043 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1044 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1045 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001046 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001048
Douglas Gregord85bea22009-09-26 06:47:28 +00001049 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1050 NewClass->getLocation(),
1051 NewTemplate,
1052 /*FIXME:*/NewClass->getLocation());
1053 Friend->setAccess(AS_public);
1054 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001055 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001056
Douglas Gregord684b002009-02-10 19:49:53 +00001057 if (Invalid) {
1058 NewTemplate->setInvalidDecl();
1059 NewClass->setInvalidDecl();
1060 }
John McCalld226f652010-08-21 09:40:31 +00001061 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001062}
1063
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001064/// \brief Diagnose the presence of a default template argument on a
1065/// template parameter, which is ill-formed in certain contexts.
1066///
1067/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001068static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001069 Sema::TemplateParamListContext TPC,
1070 SourceLocation ParamLoc,
1071 SourceRange DefArgRange) {
1072 switch (TPC) {
1073 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001074 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001075 return false;
1076
1077 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001078 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001079 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 // A default template-argument shall not be specified in a
1081 // function template declaration or a function template
1082 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001083 // If a friend function template declaration specifies a default
1084 // template-argument, that declaration shall be a definition and shall be
1085 // the only declaration of the function template in the translation unit.
1086 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001087 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001088 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001089 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001090 << DefArgRange;
1091 return false;
1092
1093 case Sema::TPC_ClassTemplateMember:
1094 // C++0x [temp.param]p9:
1095 // A default template-argument shall not be specified in the
1096 // template-parameter-lists of the definition of a member of a
1097 // class template that appears outside of the member's class.
1098 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1099 << DefArgRange;
1100 return true;
1101
1102 case Sema::TPC_FriendFunctionTemplate:
1103 // C++ [temp.param]p9:
1104 // A default template-argument shall not be specified in a
1105 // friend template declaration.
1106 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1107 << DefArgRange;
1108 return true;
1109
1110 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1111 // for friend function templates if there is only a single
1112 // declaration (and it is a definition). Strange!
1113 }
1114
1115 return false;
1116}
1117
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001118/// \brief Check for unexpanded parameter packs within the template parameters
1119/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001120static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1121 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001122 TemplateParameterList *Params = TTP->getTemplateParameters();
1123 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1124 NamedDecl *P = Params->getParam(I);
1125 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001126 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001127 NTTP->getTypeSourceInfo(),
1128 Sema::UPPC_NonTypeTemplateParameterType))
1129 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001130
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001131 continue;
1132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001133
1134 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001135 = dyn_cast<TemplateTemplateParmDecl>(P))
1136 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1137 return true;
1138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001139
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001140 return false;
1141}
1142
Douglas Gregord684b002009-02-10 19:49:53 +00001143/// \brief Checks the validity of a template parameter list, possibly
1144/// considering the template parameter list from a previous
1145/// declaration.
1146///
1147/// If an "old" template parameter list is provided, it must be
1148/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1149/// template parameter list.
1150///
1151/// \param NewParams Template parameter list for a new template
1152/// declaration. This template parameter list will be updated with any
1153/// default arguments that are carried through from the previous
1154/// template parameter list.
1155///
1156/// \param OldParams If provided, template parameter list from a
1157/// previous declaration of the same template. Default template
1158/// arguments will be merged from the old template parameter list to
1159/// the new template parameter list.
1160///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001161/// \param TPC Describes the context in which we are checking the given
1162/// template parameter list.
1163///
Douglas Gregord684b002009-02-10 19:49:53 +00001164/// \returns true if an error occurred, false otherwise.
1165bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001166 TemplateParameterList *OldParams,
1167 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001168 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Douglas Gregord684b002009-02-10 19:49:53 +00001170 // C++ [temp.param]p10:
1171 // The set of default template-arguments available for use with a
1172 // template declaration or definition is obtained by merging the
1173 // default arguments from the definition (if in scope) and all
1174 // declarations in scope in the same way default function
1175 // arguments are (8.3.6).
1176 bool SawDefaultArgument = false;
1177 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001178
Anders Carlsson49d25572009-06-12 23:20:15 +00001179 bool SawParameterPack = false;
1180 SourceLocation ParameterPackLoc;
1181
Mike Stump1a35fde2009-02-11 23:03:27 +00001182 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001183 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001184 if (OldParams)
1185 OldParam = OldParams->begin();
1186
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001187 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001188 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1189 NewParamEnd = NewParams->end();
1190 NewParam != NewParamEnd; ++NewParam) {
1191 // Variables used to diagnose redundant default arguments
1192 bool RedundantDefaultArg = false;
1193 SourceLocation OldDefaultLoc;
1194 SourceLocation NewDefaultLoc;
1195
1196 // Variables used to diagnose missing default arguments
1197 bool MissingDefaultArg = false;
1198
Anders Carlsson49d25572009-06-12 23:20:15 +00001199 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001200 // If a template parameter of a primary class template or alias template
1201 // is a template parameter pack, it shall be the last template parameter.
1202 if (SawParameterPack &&
1203 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001204 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001205 diag::err_template_param_pack_must_be_last_template_parameter);
1206 Invalid = true;
1207 }
1208
Douglas Gregord684b002009-02-10 19:49:53 +00001209 if (TemplateTypeParmDecl *NewTypeParm
1210 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001211 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001212 if (NewTypeParm->hasDefaultArgument() &&
1213 DiagnoseDefaultTemplateArgument(*this, TPC,
1214 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001215 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001216 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001217 NewTypeParm->removeDefaultArgument();
1218
1219 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001220 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001221 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Anders Carlsson49d25572009-06-12 23:20:15 +00001223 if (NewTypeParm->isParameterPack()) {
1224 assert(!NewTypeParm->hasDefaultArgument() &&
1225 "Parameter packs can't have a default argument!");
1226 SawParameterPack = true;
1227 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001228 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001229 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001230 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1231 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1232 SawDefaultArgument = true;
1233 RedundantDefaultArg = true;
1234 PreviousDefaultArgLoc = NewDefaultLoc;
1235 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1236 // Merge the default argument from the old declaration to the
1237 // new declaration.
1238 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001239 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001240 true);
1241 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1242 } else if (NewTypeParm->hasDefaultArgument()) {
1243 SawDefaultArgument = true;
1244 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1245 } else if (SawDefaultArgument)
1246 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001247 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001248 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001249 // Check for unexpanded parameter packs.
1250 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001251 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001252 UPPC_NonTypeTemplateParameterType)) {
1253 Invalid = true;
1254 continue;
1255 }
1256
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001257 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001258 if (NewNonTypeParm->hasDefaultArgument() &&
1259 DiagnoseDefaultTemplateArgument(*this, TPC,
1260 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001261 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001262 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001263 }
1264
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001265 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001266 NonTypeTemplateParmDecl *OldNonTypeParm
1267 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001268 if (NewNonTypeParm->isParameterPack()) {
1269 assert(!NewNonTypeParm->hasDefaultArgument() &&
1270 "Parameter packs can't have a default argument!");
1271 SawParameterPack = true;
1272 ParameterPackLoc = NewNonTypeParm->getLocation();
1273 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001274 NewNonTypeParm->hasDefaultArgument()) {
1275 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1276 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1277 SawDefaultArgument = true;
1278 RedundantDefaultArg = true;
1279 PreviousDefaultArgLoc = NewDefaultLoc;
1280 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1281 // Merge the default argument from the old declaration to the
1282 // new declaration.
1283 SawDefaultArgument = true;
1284 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001285 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001286 // parameter.
1287 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001288 OldNonTypeParm->getDefaultArgument(),
1289 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001290 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1291 } else if (NewNonTypeParm->hasDefaultArgument()) {
1292 SawDefaultArgument = true;
1293 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1294 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001295 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001296 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001297 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001298 TemplateTemplateParmDecl *NewTemplateParm
1299 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001300
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001301 // Check for unexpanded parameter packs, recursively.
1302 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1303 Invalid = true;
1304 continue;
1305 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001306
1307 if (NewTemplateParm->hasDefaultArgument() &&
1308 DiagnoseDefaultTemplateArgument(*this, TPC,
1309 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001310 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001311 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001312
1313 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001314 TemplateTemplateParmDecl *OldTemplateParm
1315 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001316 if (NewTemplateParm->isParameterPack()) {
1317 assert(!NewTemplateParm->hasDefaultArgument() &&
1318 "Parameter packs can't have a default argument!");
1319 SawParameterPack = true;
1320 ParameterPackLoc = NewTemplateParm->getLocation();
1321 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001322 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001323 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1324 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001325 SawDefaultArgument = true;
1326 RedundantDefaultArg = true;
1327 PreviousDefaultArgLoc = NewDefaultLoc;
1328 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1329 // Merge the default argument from the old declaration to the
1330 // new declaration.
1331 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001332 // FIXME: We need to create a new kind of "default argument" expression
1333 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001334 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001335 OldTemplateParm->getDefaultArgument(),
1336 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001337 PreviousDefaultArgLoc
1338 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001339 } else if (NewTemplateParm->hasDefaultArgument()) {
1340 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001341 PreviousDefaultArgLoc
1342 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001343 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001344 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001345 }
1346
1347 if (RedundantDefaultArg) {
1348 // C++ [temp.param]p12:
1349 // A template-parameter shall not be given default arguments
1350 // by two different declarations in the same scope.
1351 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1352 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1353 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001354 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001355 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001356 // If a template-parameter of a class template has a default
1357 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001358 // have a default template-argument supplied or be a template parameter
1359 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001360 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001361 diag::err_template_param_default_arg_missing);
1362 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1363 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001364 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001365 }
1366
1367 // If we have an old template parameter list that we're merging
1368 // in, move on to the next parameter.
1369 if (OldParams)
1370 ++OldParam;
1371 }
1372
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001373 // We were missing some default arguments at the end of the list, so remove
1374 // all of the default arguments.
1375 if (RemoveDefaultArguments) {
1376 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1377 NewParamEnd = NewParams->end();
1378 NewParam != NewParamEnd; ++NewParam) {
1379 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1380 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001381 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001382 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1383 NTTP->removeDefaultArgument();
1384 else
1385 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1386 }
1387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001388
Douglas Gregord684b002009-02-10 19:49:53 +00001389 return Invalid;
1390}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001391
John McCall4e2cbb22010-10-20 05:44:58 +00001392namespace {
1393
1394/// A class which looks for a use of a certain level of template
1395/// parameter.
1396struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1397 typedef RecursiveASTVisitor<DependencyChecker> super;
1398
1399 unsigned Depth;
1400 bool Match;
1401
1402 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1403 NamedDecl *ND = Params->getParam(0);
1404 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1405 Depth = PD->getDepth();
1406 } else if (NonTypeTemplateParmDecl *PD =
1407 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1408 Depth = PD->getDepth();
1409 } else {
1410 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1411 }
1412 }
1413
1414 bool Matches(unsigned ParmDepth) {
1415 if (ParmDepth >= Depth) {
1416 Match = true;
1417 return true;
1418 }
1419 return false;
1420 }
1421
1422 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1423 return !Matches(T->getDepth());
1424 }
1425
1426 bool TraverseTemplateName(TemplateName N) {
1427 if (TemplateTemplateParmDecl *PD =
1428 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1429 if (Matches(PD->getDepth())) return false;
1430 return super::TraverseTemplateName(N);
1431 }
1432
1433 bool VisitDeclRefExpr(DeclRefExpr *E) {
1434 if (NonTypeTemplateParmDecl *PD =
1435 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1436 if (PD->getDepth() == Depth) {
1437 Match = true;
1438 return false;
1439 }
1440 }
1441 return super::VisitDeclRefExpr(E);
1442 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001443
1444 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1445 return TraverseType(T->getInjectedSpecializationType());
1446 }
John McCall4e2cbb22010-10-20 05:44:58 +00001447};
1448}
1449
Douglas Gregorc8406492011-05-10 18:27:06 +00001450/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001451/// list.
1452static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001453DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001454 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001455 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001456 return Checker.Match;
1457}
1458
Douglas Gregorc8406492011-05-10 18:27:06 +00001459// Find the source range corresponding to the named type in the given
1460// nested-name-specifier, if any.
1461static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1462 QualType T,
1463 const CXXScopeSpec &SS) {
1464 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1465 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1466 if (const Type *CurType = NNS->getAsType()) {
1467 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1468 return NNSLoc.getTypeLoc().getSourceRange();
1469 } else
1470 break;
1471
1472 NNSLoc = NNSLoc.getPrefix();
1473 }
1474
1475 return SourceRange();
1476}
1477
Mike Stump1eb44332009-09-09 15:08:12 +00001478/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001479/// specifier, returning the template parameter list that applies to the
1480/// name.
1481///
1482/// \param DeclStartLoc the start of the declaration that has a scope
1483/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001484///
Douglas Gregorc8406492011-05-10 18:27:06 +00001485/// \param DeclLoc The location of the declaration itself.
1486///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001487/// \param SS the scope specifier that will be matched to the given template
1488/// parameter lists. This scope specifier precedes a qualified name that is
1489/// being declared.
1490///
1491/// \param ParamLists the template parameter lists, from the outermost to the
1492/// innermost template parameter lists.
1493///
1494/// \param NumParamLists the number of template parameter lists in ParamLists.
1495///
John McCall77e8b112010-04-13 20:37:33 +00001496/// \param IsFriend Whether to apply the slightly different rules for
1497/// matching template parameters to scope specifiers in friend
1498/// declarations.
1499///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001500/// \param IsExplicitSpecialization will be set true if the entity being
1501/// declared is an explicit specialization, false otherwise.
1502///
Mike Stump1eb44332009-09-09 15:08:12 +00001503/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001505/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001506/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001507/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001508/// itself a template).
1509TemplateParameterList *
1510Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001511 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001512 const CXXScopeSpec &SS,
1513 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001514 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001515 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001516 bool &IsExplicitSpecialization,
1517 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001518 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001519 Invalid = false;
1520
1521 // The sequence of nested types to which we will match up the template
1522 // parameter lists. We first build this list by starting with the type named
1523 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001524 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001525 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001526 if (SS.getScopeRep()) {
1527 if (CXXRecordDecl *Record
1528 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1529 T = Context.getTypeDeclType(Record);
1530 else
1531 T = QualType(SS.getScopeRep()->getAsType(), 0);
1532 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001533
1534 // If we found an explicit specialization that prevents us from needing
1535 // 'template<>' headers, this will be set to the location of that
1536 // explicit specialization.
1537 SourceLocation ExplicitSpecLoc;
1538
1539 while (!T.isNull()) {
1540 NestedTypes.push_back(T);
1541
1542 // Retrieve the parent of a record type.
1543 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1544 // If this type is an explicit specialization, we're done.
1545 if (ClassTemplateSpecializationDecl *Spec
1546 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1547 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1548 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1549 ExplicitSpecLoc = Spec->getLocation();
1550 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001551 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001552 } else if (Record->getTemplateSpecializationKind()
1553 == TSK_ExplicitSpecialization) {
1554 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001555 break;
1556 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001557
1558 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1559 T = Context.getTypeDeclType(Parent);
1560 else
1561 T = QualType();
1562 continue;
1563 }
1564
1565 if (const TemplateSpecializationType *TST
1566 = T->getAs<TemplateSpecializationType>()) {
1567 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1568 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1569 T = Context.getTypeDeclType(Parent);
1570 else
1571 T = QualType();
1572 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001573 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001574 }
1575
1576 // Look one step prior in a dependent template specialization type.
1577 if (const DependentTemplateSpecializationType *DependentTST
1578 = T->getAs<DependentTemplateSpecializationType>()) {
1579 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1580 T = QualType(NNS->getAsType(), 0);
1581 else
1582 T = QualType();
1583 continue;
1584 }
1585
1586 // Look one step prior in a dependent name type.
1587 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1588 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1589 T = QualType(NNS->getAsType(), 0);
1590 else
1591 T = QualType();
1592 continue;
1593 }
1594
1595 // Retrieve the parent of an enumeration type.
1596 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1597 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1598 // check here.
1599 EnumDecl *Enum = EnumT->getDecl();
1600
1601 // Get to the parent type.
1602 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1603 T = Context.getTypeDeclType(Parent);
1604 else
1605 T = QualType();
1606 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregorc8406492011-05-10 18:27:06 +00001609 T = QualType();
1610 }
1611 // Reverse the nested types list, since we want to traverse from the outermost
1612 // to the innermost while checking template-parameter-lists.
1613 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001614
Douglas Gregorc8406492011-05-10 18:27:06 +00001615 // C++0x [temp.expl.spec]p17:
1616 // A member or a member template may be nested within many
1617 // enclosing class templates. In an explicit specialization for
1618 // such a member, the member declaration shall be preceded by a
1619 // template<> for each enclosing class template that is
1620 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001621 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001622 unsigned ParamIdx = 0;
1623 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1624 ++TypeIdx) {
1625 T = NestedTypes[TypeIdx];
1626
1627 // Whether we expect a 'template<>' header.
1628 bool NeedEmptyTemplateHeader = false;
1629
1630 // Whether we expect a template header with parameters.
1631 bool NeedNonemptyTemplateHeader = false;
1632
1633 // For a dependent type, the set of template parameters that we
1634 // expect to see.
1635 TemplateParameterList *ExpectedTemplateParams = 0;
1636
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001637 // C++0x [temp.expl.spec]p15:
1638 // A member or a member template may be nested within many enclosing
1639 // class templates. In an explicit specialization for such a member, the
1640 // member declaration shall be preceded by a template<> for each
1641 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001642 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1643 if (ClassTemplatePartialSpecializationDecl *Partial
1644 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1645 ExpectedTemplateParams = Partial->getTemplateParameters();
1646 NeedNonemptyTemplateHeader = true;
1647 } else if (Record->isDependentType()) {
1648 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001649 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001650 ->getTemplateParameters();
1651 NeedNonemptyTemplateHeader = true;
1652 }
1653 } else if (ClassTemplateSpecializationDecl *Spec
1654 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1655 // C++0x [temp.expl.spec]p4:
1656 // Members of an explicitly specialized class template are defined
1657 // in the same manner as members of normal classes, and not using
1658 // the template<> syntax.
1659 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1660 NeedEmptyTemplateHeader = true;
1661 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001662 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001663 } else if (Record->getTemplateSpecializationKind()) {
1664 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001665 != TSK_ExplicitSpecialization &&
1666 TypeIdx == NumTypes - 1)
1667 IsExplicitSpecialization = true;
1668
1669 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001670 }
1671 } else if (const TemplateSpecializationType *TST
1672 = T->getAs<TemplateSpecializationType>()) {
1673 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1674 ExpectedTemplateParams = Template->getTemplateParameters();
1675 NeedNonemptyTemplateHeader = true;
1676 }
1677 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1678 // FIXME: We actually could/should check the template arguments here
1679 // against the corresponding template parameter list.
1680 NeedNonemptyTemplateHeader = false;
1681 }
1682
Douglas Gregor89b9f102011-06-06 15:22:55 +00001683 // C++ [temp.expl.spec]p16:
1684 // In an explicit specialization declaration for a member of a class
1685 // template or a member template that ap- pears in namespace scope, the
1686 // member template and some of its enclosing class templates may remain
1687 // unspecialized, except that the declaration shall not explicitly
1688 // specialize a class member template if its en- closing class templates
1689 // are not explicitly specialized as well.
1690 if (ParamIdx < NumParamLists) {
1691 if (ParamLists[ParamIdx]->size() == 0) {
1692 if (SawNonEmptyTemplateParameterList) {
1693 Diag(DeclLoc, diag::err_specialize_member_of_template)
1694 << ParamLists[ParamIdx]->getSourceRange();
1695 Invalid = true;
1696 IsExplicitSpecialization = false;
1697 return 0;
1698 }
1699 } else
1700 SawNonEmptyTemplateParameterList = true;
1701 }
1702
Douglas Gregorc8406492011-05-10 18:27:06 +00001703 if (NeedEmptyTemplateHeader) {
1704 // If we're on the last of the types, and we need a 'template<>' header
1705 // here, then it's an explicit specialization.
1706 if (TypeIdx == NumTypes - 1)
1707 IsExplicitSpecialization = true;
1708
1709 if (ParamIdx < NumParamLists) {
1710 if (ParamLists[ParamIdx]->size() > 0) {
1711 // The header has template parameters when it shouldn't. Complain.
1712 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1713 diag::err_template_param_list_matches_nontemplate)
1714 << T
1715 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1716 ParamLists[ParamIdx]->getRAngleLoc())
1717 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1718 Invalid = true;
1719 return 0;
1720 }
1721
1722 // Consume this template header.
1723 ++ParamIdx;
1724 continue;
1725 }
1726
1727 if (!IsFriend) {
1728 // We don't have a template header, but we should.
1729 SourceLocation ExpectedTemplateLoc;
1730 if (NumParamLists > 0)
1731 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1732 else
1733 ExpectedTemplateLoc = DeclStartLoc;
1734
1735 Diag(DeclLoc, diag::err_template_spec_needs_header)
1736 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1737 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1738 }
1739
1740 continue;
1741 }
1742
1743 if (NeedNonemptyTemplateHeader) {
1744 // In friend declarations we can have template-ids which don't
1745 // depend on the corresponding template parameter lists. But
1746 // assume that empty parameter lists are supposed to match this
1747 // template-id.
1748 if (IsFriend && T->isDependentType()) {
1749 if (ParamIdx < NumParamLists &&
1750 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1751 ExpectedTemplateParams = 0;
1752 else
1753 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001754 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001755
Douglas Gregorc8406492011-05-10 18:27:06 +00001756 if (ParamIdx < NumParamLists) {
1757 // Check the template parameter list, if we can.
1758 if (ExpectedTemplateParams &&
1759 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1760 ExpectedTemplateParams,
1761 true, TPL_TemplateMatch))
1762 Invalid = true;
1763
1764 if (!Invalid &&
1765 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1766 TPC_ClassTemplateMember))
1767 Invalid = true;
1768
1769 ++ParamIdx;
1770 continue;
1771 }
1772
1773 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1774 << T
1775 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1776 Invalid = true;
1777 continue;
1778 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001779 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001780
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001781 // If there were at least as many template-ids as there were template
1782 // parameter lists, then there are no template parameter lists remaining for
1783 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001784 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001785 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001787 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001788 if (ParamIdx < NumParamLists - 1) {
1789 bool HasAnyExplicitSpecHeader = false;
1790 bool AllExplicitSpecHeaders = true;
1791 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1792 if (ParamLists[I]->size() == 0)
1793 HasAnyExplicitSpecHeader = true;
1794 else
1795 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001796 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001797
1798 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1799 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1800 : diag::err_template_spec_extra_headers)
1801 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1802 ParamLists[NumParamLists - 2]->getRAngleLoc());
1803
1804 // If there was a specialization somewhere, such that 'template<>' is
1805 // not required, and there were any 'template<>' headers, note where the
1806 // specialization occurred.
1807 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1808 Diag(ExplicitSpecLoc,
1809 diag::note_explicit_template_spec_does_not_need_header)
1810 << NestedTypes.back();
1811
1812 // We have a template parameter list with no corresponding scope, which
1813 // means that the resulting template declaration can't be instantiated
1814 // properly (we'll end up with dependent nodes when we shouldn't).
1815 if (!AllExplicitSpecHeaders)
1816 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Douglas Gregor89b9f102011-06-06 15:22:55 +00001819 // C++ [temp.expl.spec]p16:
1820 // In an explicit specialization declaration for a member of a class
1821 // template or a member template that ap- pears in namespace scope, the
1822 // member template and some of its enclosing class templates may remain
1823 // unspecialized, except that the declaration shall not explicitly
1824 // specialize a class member template if its en- closing class templates
1825 // are not explicitly specialized as well.
1826 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1827 SawNonEmptyTemplateParameterList) {
1828 Diag(DeclLoc, diag::err_specialize_member_of_template)
1829 << ParamLists[ParamIdx]->getSourceRange();
1830 Invalid = true;
1831 IsExplicitSpecialization = false;
1832 return 0;
1833 }
1834
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001835 // Return the last template parameter list, which corresponds to the
1836 // entity being declared.
1837 return ParamLists[NumParamLists - 1];
1838}
1839
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001840void Sema::NoteAllFoundTemplates(TemplateName Name) {
1841 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1842 Diag(Template->getLocation(), diag::note_template_declared_here)
1843 << (isa<FunctionTemplateDecl>(Template)? 0
1844 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001845 : isa<TypeAliasTemplateDecl>(Template)? 2
1846 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001847 << Template->getDeclName();
1848 return;
1849 }
1850
1851 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1852 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1853 IEnd = OST->end();
1854 I != IEnd; ++I)
1855 Diag((*I)->getLocation(), diag::note_template_declared_here)
1856 << 0 << (*I)->getDeclName();
1857
1858 return;
1859 }
1860}
1861
1862
Douglas Gregor7532dc62009-03-30 22:58:21 +00001863QualType Sema::CheckTemplateIdType(TemplateName Name,
1864 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001865 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001866 DependentTemplateName *DTN
1867 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001868 if (DTN && DTN->isIdentifier())
1869 // When building a template-id where the template-name is dependent,
1870 // assume the template is a type template. Either our assumption is
1871 // correct, or the code is ill-formed and will be diagnosed when the
1872 // dependent name is substituted.
1873 return Context.getDependentTemplateSpecializationType(ETK_None,
1874 DTN->getQualifier(),
1875 DTN->getIdentifier(),
1876 TemplateArgs);
1877
Douglas Gregor7532dc62009-03-30 22:58:21 +00001878 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001879 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1880 // We might have a substituted template template parameter pack. If so,
1881 // build a template specialization type for it.
1882 if (Name.getAsSubstTemplateTemplateParmPack())
1883 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001884
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001885 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1886 << Name;
1887 NoteAllFoundTemplates(Name);
1888 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001889 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001890
Douglas Gregor40808ce2009-03-09 23:48:35 +00001891 // Check that the template argument list is well-formed for this
1892 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001893 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001894 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001895 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001896 return QualType();
1897
Douglas Gregor910f8002010-11-07 23:05:16 +00001898 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001899 "Converted template argument list is too short!");
1900
1901 QualType CanonType;
1902
Douglas Gregor561f8122011-07-01 01:22:09 +00001903 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001904 if (TypeAliasTemplateDecl *AliasTemplate
1905 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1906 // Find the canonical type for this type alias template specialization.
1907 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1908 if (Pattern->isInvalidDecl())
1909 return QualType();
1910
1911 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1912 Converted.data(), Converted.size());
1913
1914 // Only substitute for the innermost template argument list.
1915 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001916 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001917 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1918 for (unsigned I = 0; I < Depth; ++I)
1919 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001920
1921 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1922 CanonType = SubstType(Pattern->getUnderlyingType(),
1923 TemplateArgLists, AliasTemplate->getLocation(),
1924 AliasTemplate->getDeclName());
1925 if (CanonType.isNull())
1926 return QualType();
1927 } else if (Name.isDependent() ||
1928 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001929 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001930 // This class template specialization is a dependent
1931 // type. Therefore, its canonical type is another class template
1932 // specialization type that contains all of the converted
1933 // arguments in canonical form. This ensures that, e.g., A<T> and
1934 // A<T, T> have identical types when A is declared as:
1935 //
1936 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001937 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001938 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001939 Converted.data(),
1940 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregor1275ae02009-07-28 23:00:59 +00001942 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001943 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001944 // In the future, we need to teach getTemplateSpecializationType to only
1945 // build the canonical type and return that to us.
1946 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001947
1948 // This might work out to be a current instantiation, in which
1949 // case the canonical type needs to be the InjectedClassNameType.
1950 //
1951 // TODO: in theory this could be a simple hashtable lookup; most
1952 // changes to CurContext don't change the set of current
1953 // instantiations.
1954 if (isa<ClassTemplateDecl>(Template)) {
1955 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1956 // If we get out to a namespace, we're done.
1957 if (Ctx->isFileContext()) break;
1958
1959 // If this isn't a record, keep looking.
1960 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1961 if (!Record) continue;
1962
1963 // Look for one of the two cases with InjectedClassNameTypes
1964 // and check whether it's the same template.
1965 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1966 !Record->getDescribedClassTemplate())
1967 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001968
John McCall31f17ec2010-04-27 00:57:59 +00001969 // Fetch the injected class name type and check whether its
1970 // injected type is equal to the type we just built.
1971 QualType ICNT = Context.getTypeDeclType(Record);
1972 QualType Injected = cast<InjectedClassNameType>(ICNT)
1973 ->getInjectedSpecializationType();
1974
1975 if (CanonType != Injected->getCanonicalTypeInternal())
1976 continue;
1977
1978 // If so, the canonical type of this TST is the injected
1979 // class name type of the record we just found.
1980 assert(ICNT.isCanonical());
1981 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001982 break;
1983 }
1984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001986 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001987 // Find the class template specialization declaration that
1988 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001989 void *InsertPos = 0;
1990 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001991 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001992 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001993 if (!Decl) {
1994 // This is the first time we have referenced this class template
1995 // specialization. Create the canonical declaration and add it to
1996 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001997 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001998 ClassTemplate->getTemplatedDecl()->getTagKind(),
1999 ClassTemplate->getDeclContext(),
2000 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002001 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002002 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002003 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002004 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002005 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002006 Decl->setLexicalDeclContext(CurContext);
2007 }
2008
2009 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002010 assert(isa<RecordType>(CanonType) &&
2011 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002012 }
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Douglas Gregor40808ce2009-03-09 23:48:35 +00002014 // Build the fully-sugared type for this class template
2015 // specialization, which refers back to the class template
2016 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002017 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002018}
2019
John McCallf312b1e2010-08-26 23:41:50 +00002020TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002021Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2022 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002023 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002024 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002025 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002026 if (SS.isInvalid())
2027 return true;
2028
Douglas Gregor7532dc62009-03-30 22:58:21 +00002029 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002030
Douglas Gregor40808ce2009-03-09 23:48:35 +00002031 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002032 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002033 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002034
Douglas Gregora88f09f2011-02-28 17:23:35 +00002035 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2036 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2037 DTN->getQualifier(),
2038 DTN->getIdentifier(),
2039 TemplateArgs);
2040
2041 // Build type-source information.
2042 TypeLocBuilder TLB;
2043 DependentTemplateSpecializationTypeLoc SpecTL
2044 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002045 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002046 SpecTL.setNameLoc(TemplateLoc);
2047 SpecTL.setLAngleLoc(LAngleLoc);
2048 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002049 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002050 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2051 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2052 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2053 }
2054
John McCalld5532b62009-11-23 01:53:49 +00002055 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002056 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002057
2058 if (Result.isNull())
2059 return true;
2060
Douglas Gregor059101f2011-03-02 00:47:37 +00002061 // Build type-source information.
2062 TypeLocBuilder TLB;
2063 TemplateSpecializationTypeLoc SpecTL
2064 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2065 SpecTL.setTemplateNameLoc(TemplateLoc);
2066 SpecTL.setLAngleLoc(LAngleLoc);
2067 SpecTL.setRAngleLoc(RAngleLoc);
2068 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2069 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002070
Douglas Gregor059101f2011-03-02 00:47:37 +00002071 if (SS.isNotEmpty()) {
2072 // Create an elaborated-type-specifier containing the nested-name-specifier.
2073 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2074 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2075 ElabTL.setKeywordLoc(SourceLocation());
2076 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2077 }
2078
2079 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002080}
John McCallf1bbbb42009-09-04 01:14:41 +00002081
Douglas Gregor059101f2011-03-02 00:47:37 +00002082TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002083 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002084 SourceLocation TagLoc,
2085 CXXScopeSpec &SS,
2086 TemplateTy TemplateD,
2087 SourceLocation TemplateLoc,
2088 SourceLocation LAngleLoc,
2089 ASTTemplateArgsPtr TemplateArgsIn,
2090 SourceLocation RAngleLoc) {
2091 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2092
2093 // Translate the parser's template argument list in our AST format.
2094 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2095 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2096
2097 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002098 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002099 ElaboratedTypeKeyword Keyword
2100 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Douglas Gregor059101f2011-03-02 00:47:37 +00002102 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2103 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2104 DTN->getQualifier(),
2105 DTN->getIdentifier(),
2106 TemplateArgs);
2107
2108 // Build type-source information.
2109 TypeLocBuilder TLB;
2110 DependentTemplateSpecializationTypeLoc SpecTL
2111 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2112 SpecTL.setKeywordLoc(TagLoc);
2113 SpecTL.setNameLoc(TemplateLoc);
2114 SpecTL.setLAngleLoc(LAngleLoc);
2115 SpecTL.setRAngleLoc(RAngleLoc);
2116 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2117 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2118 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2119 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2120 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002121
2122 if (TypeAliasTemplateDecl *TAT =
2123 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2124 // C++0x [dcl.type.elab]p2:
2125 // If the identifier resolves to a typedef-name or the simple-template-id
2126 // resolves to an alias template specialization, the
2127 // elaborated-type-specifier is ill-formed.
2128 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2129 Diag(TAT->getLocation(), diag::note_declared_at);
2130 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002131
2132 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2133 if (Result.isNull())
2134 return TypeResult();
2135
2136 // Check the tag kind
2137 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002138 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002139
John McCall6b2becf2009-09-08 17:47:29 +00002140 IdentifierInfo *Id = D->getIdentifier();
2141 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002142
Richard Trieubbf34c02011-06-10 03:11:26 +00002143 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2144 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002145 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002146 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002147 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002148 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002149 }
2150 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002151
2152 // Provide source-location information for the template specialization.
2153 TypeLocBuilder TLB;
2154 TemplateSpecializationTypeLoc SpecTL
2155 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2156 SpecTL.setTemplateNameLoc(TemplateLoc);
2157 SpecTL.setLAngleLoc(LAngleLoc);
2158 SpecTL.setRAngleLoc(RAngleLoc);
2159 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2160 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002161
Douglas Gregor059101f2011-03-02 00:47:37 +00002162 // Construct an elaborated type containing the nested-name-specifier (if any)
2163 // and keyword.
2164 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2165 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2166 ElabTL.setKeywordLoc(TagLoc);
2167 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2168 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002169}
2170
John McCall60d7b3a2010-08-24 06:29:42 +00002171ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002172 LookupResult &R,
2173 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002174 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002175 // FIXME: Can we do any checking at this point? I guess we could check the
2176 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002177 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002178 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002179 // foo<int> could identify a single function unambiguously
2180 // This approach does NOT work, since f<int>(1);
2181 // gets resolved prior to resorting to overload resolution
2182 // i.e., template<class T> void f(double);
2183 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002184
2185 // These should be filtered out by our callers.
2186 assert(!R.empty() && "empty lookup results when building templateid");
2187 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2188
John McCallc373d482010-01-27 01:50:18 +00002189 // We don't want lookup warnings at this point.
2190 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002191
John McCallf7a1a742009-11-24 19:00:30 +00002192 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002193 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002194 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002195 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002196 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002197 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002198
2199 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002200}
2201
John McCallf7a1a742009-11-24 19:00:30 +00002202// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002203ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002204Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002205 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002206 const TemplateArgumentListInfo &TemplateArgs) {
2207 DeclContext *DC;
2208 if (!(DC = computeDeclContext(SS, false)) ||
2209 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002210 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002211 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002213 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002214 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002215 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2216 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
John McCallf7a1a742009-11-24 19:00:30 +00002218 if (R.isAmbiguous())
2219 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002220
John McCallf7a1a742009-11-24 19:00:30 +00002221 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002222 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2223 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002224 return ExprError();
2225 }
2226
2227 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002228 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2229 << (NestedNameSpecifier*) SS.getScopeRep()
2230 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002231 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2232 return ExprError();
2233 }
2234
2235 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002236}
2237
Douglas Gregorc45c2322009-03-31 00:43:58 +00002238/// \brief Form a dependent template name.
2239///
2240/// This action forms a dependent template name given the template
2241/// name and its (presumably dependent) scope specifier. For
2242/// example, given "MetaFun::template apply", the scope specifier \p
2243/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2244/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002245TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002246 SourceLocation TemplateKWLoc,
2247 CXXScopeSpec &SS,
2248 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002249 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002250 bool EnteringContext,
2251 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002252 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2253 !getLangOptions().CPlusPlus0x)
2254 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255 << FixItHint::CreateRemoval(TemplateKWLoc);
2256
Douglas Gregor0707bc52010-01-19 16:01:07 +00002257 DeclContext *LookupCtx = 0;
2258 if (SS.isSet())
2259 LookupCtx = computeDeclContext(SS, EnteringContext);
2260 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002261 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002262 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002263 // C++0x [temp.names]p5:
2264 // If a name prefixed by the keyword template is not the name of
2265 // a template, the program is ill-formed. [Note: the keyword
2266 // template may not be applied to non-template members of class
2267 // templates. -end note ] [ Note: as is the case with the
2268 // typename prefix, the template prefix is allowed in cases
2269 // where it is not strictly necessary; i.e., when the
2270 // nested-name-specifier or the expression on the left of the ->
2271 // or . is not dependent on a template-parameter, or the use
2272 // does not appear in the scope of a template. -end note]
2273 //
2274 // Note: C++03 was more strict here, because it banned the use of
2275 // the "template" keyword prior to a template-name that was not a
2276 // dependent name. C++ DR468 relaxed this requirement (the
2277 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002278 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002279 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002280 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2281 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002282 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002283 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2284 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002285 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2286 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002287 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002288 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002289 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002290 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002291 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002292 << Name.getSourceRange()
2293 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002294 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002295 } else {
2296 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002297 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002298 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002299 }
2300
Mike Stump1eb44332009-09-09 15:08:12 +00002301 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002302 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002303
Douglas Gregor014e88d2009-11-03 23:16:33 +00002304 switch (Name.getKind()) {
2305 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002306 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002307 Name.Identifier));
2308 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002310 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002311 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002312 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002313 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002314
2315 case UnqualifiedId::IK_LiteralOperatorId:
2316 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2317
Douglas Gregor014e88d2009-11-03 23:16:33 +00002318 default:
2319 break;
2320 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002321
2322 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002323 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002324 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002325 << Name.getSourceRange()
2326 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002327 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002328}
2329
Mike Stump1eb44332009-09-09 15:08:12 +00002330bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002331 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002332 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002333 const TemplateArgument &Arg = AL.getArgument();
2334
Anders Carlsson436b1562009-06-13 00:33:33 +00002335 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002336 switch(Arg.getKind()) {
2337 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002338 // C++ [temp.arg.type]p1:
2339 // A template-argument for a template-parameter which is a
2340 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002341 break;
2342 case TemplateArgument::Template: {
2343 // We have a template type parameter but the template argument
2344 // is a template without any arguments.
2345 SourceRange SR = AL.getSourceRange();
2346 TemplateName Name = Arg.getAsTemplate();
2347 Diag(SR.getBegin(), diag::err_template_missing_args)
2348 << Name << SR;
2349 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2350 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002351
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002352 return true;
2353 }
2354 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002355 // We have a template type parameter but the template argument
2356 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002357 SourceRange SR = AL.getSourceRange();
2358 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002359 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Anders Carlsson436b1562009-06-13 00:33:33 +00002361 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002362 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002363 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002364
John McCalla93c9342009-12-07 02:54:59 +00002365 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002366 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Anders Carlsson436b1562009-06-13 00:33:33 +00002368 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002369 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2370
2371 // Objective-C ARC:
2372 // If an explicitly-specified template argument type is a lifetime type
2373 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2374 if (getLangOptions().ObjCAutoRefCount &&
2375 ArgType->isObjCLifetimeType() &&
2376 !ArgType.getObjCLifetime()) {
2377 Qualifiers Qs;
2378 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2379 ArgType = Context.getQualifiedType(ArgType, Qs);
2380 }
2381
2382 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002383 return false;
2384}
2385
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002386/// \brief Substitute template arguments into the default template argument for
2387/// the given template type parameter.
2388///
2389/// \param SemaRef the semantic analysis object for which we are performing
2390/// the substitution.
2391///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002392/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002393/// for.
2394///
2395/// \param TemplateLoc the location of the template name that started the
2396/// template-id we are checking.
2397///
2398/// \param RAngleLoc the location of the right angle bracket ('>') that
2399/// terminates the template-id.
2400///
2401/// \param Param the template template parameter whose default we are
2402/// substituting into.
2403///
2404/// \param Converted the list of template arguments provided for template
2405/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002406/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002407static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002408SubstDefaultTemplateArgument(Sema &SemaRef,
2409 TemplateDecl *Template,
2410 SourceLocation TemplateLoc,
2411 SourceLocation RAngleLoc,
2412 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002413 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002414 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002415
2416 // If the argument type is dependent, instantiate it now based
2417 // on the previously-computed template arguments.
2418 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002420 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002421
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002422 MultiLevelTemplateArgumentList AllTemplateArgs
2423 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2424
2425 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002426 Template, Converted.data(),
2427 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002428 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002430 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2431 Param->getDefaultArgumentLoc(),
2432 Param->getDeclName());
2433 }
2434
2435 return ArgType;
2436}
2437
2438/// \brief Substitute template arguments into the default template argument for
2439/// the given non-type template parameter.
2440///
2441/// \param SemaRef the semantic analysis object for which we are performing
2442/// the substitution.
2443///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002445/// for.
2446///
2447/// \param TemplateLoc the location of the template name that started the
2448/// template-id we are checking.
2449///
2450/// \param RAngleLoc the location of the right angle bracket ('>') that
2451/// terminates the template-id.
2452///
Douglas Gregor788cd062009-11-11 01:00:40 +00002453/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002454/// substituting into.
2455///
2456/// \param Converted the list of template arguments provided for template
2457/// parameters that precede \p Param in the template parameter list.
2458///
2459/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002460static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002461SubstDefaultTemplateArgument(Sema &SemaRef,
2462 TemplateDecl *Template,
2463 SourceLocation TemplateLoc,
2464 SourceLocation RAngleLoc,
2465 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002466 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002468 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002469
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002470 MultiLevelTemplateArgumentList AllTemplateArgs
2471 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002473 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002474 Template, Converted.data(),
2475 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002476 SourceRange(TemplateLoc, RAngleLoc));
2477
2478 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2479}
2480
Douglas Gregor788cd062009-11-11 01:00:40 +00002481/// \brief Substitute template arguments into the default template argument for
2482/// the given template template parameter.
2483///
2484/// \param SemaRef the semantic analysis object for which we are performing
2485/// the substitution.
2486///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002488/// for.
2489///
2490/// \param TemplateLoc the location of the template name that started the
2491/// template-id we are checking.
2492///
2493/// \param RAngleLoc the location of the right angle bracket ('>') that
2494/// terminates the template-id.
2495///
2496/// \param Param the template template parameter whose default we are
2497/// substituting into.
2498///
2499/// \param Converted the list of template arguments provided for template
2500/// parameters that precede \p Param in the template parameter list.
2501///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002502/// \param QualifierLoc Will be set to the nested-name-specifier (with
2503/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002504///
Douglas Gregor788cd062009-11-11 01:00:40 +00002505/// \returns the substituted template argument, or NULL if an error occurred.
2506static TemplateName
2507SubstDefaultTemplateArgument(Sema &SemaRef,
2508 TemplateDecl *Template,
2509 SourceLocation TemplateLoc,
2510 SourceLocation RAngleLoc,
2511 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002512 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002513 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002515 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516
Douglas Gregor788cd062009-11-11 01:00:40 +00002517 MultiLevelTemplateArgumentList AllTemplateArgs
2518 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002519
Douglas Gregor788cd062009-11-11 01:00:40 +00002520 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002521 Template, Converted.data(),
2522 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002523 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002524
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002525 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002526 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002527 if (QualifierLoc) {
2528 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2529 AllTemplateArgs);
2530 if (!QualifierLoc)
2531 return TemplateName();
2532 }
2533
Douglas Gregor1d752d72011-03-02 18:46:51 +00002534 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002535 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002536 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002537 AllTemplateArgs);
2538}
2539
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002540/// \brief If the given template parameter has a default template
2541/// argument, substitute into that default template argument and
2542/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002543TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002544Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2545 SourceLocation TemplateLoc,
2546 SourceLocation RAngleLoc,
2547 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002548 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002549 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002550 if (!TypeParm->hasDefaultArgument())
2551 return TemplateArgumentLoc();
2552
John McCalla93c9342009-12-07 02:54:59 +00002553 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002554 TemplateLoc,
2555 RAngleLoc,
2556 TypeParm,
2557 Converted);
2558 if (DI)
2559 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2560
2561 return TemplateArgumentLoc();
2562 }
2563
2564 if (NonTypeTemplateParmDecl *NonTypeParm
2565 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2566 if (!NonTypeParm->hasDefaultArgument())
2567 return TemplateArgumentLoc();
2568
John McCall60d7b3a2010-08-24 06:29:42 +00002569 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002570 TemplateLoc,
2571 RAngleLoc,
2572 NonTypeParm,
2573 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002574 if (Arg.isInvalid())
2575 return TemplateArgumentLoc();
2576
2577 Expr *ArgE = Arg.takeAs<Expr>();
2578 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2579 }
2580
2581 TemplateTemplateParmDecl *TempTempParm
2582 = cast<TemplateTemplateParmDecl>(Param);
2583 if (!TempTempParm->hasDefaultArgument())
2584 return TemplateArgumentLoc();
2585
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002586
Douglas Gregor1d752d72011-03-02 18:46:51 +00002587 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002588 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002589 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002590 RAngleLoc,
2591 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002592 Converted,
2593 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002594 if (TName.isNull())
2595 return TemplateArgumentLoc();
2596
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002597 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002598 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002599 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2600}
2601
Douglas Gregore7526412009-11-11 19:31:23 +00002602/// \brief Check that the given template argument corresponds to the given
2603/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002604///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002605/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002606/// checked.
2607///
2608/// \param Arg The template argument.
2609///
2610/// \param Template The template in which the template argument resides.
2611///
2612/// \param TemplateLoc The location of the template name for the template
2613/// whose argument list we're matching.
2614///
2615/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2616/// the template argument list.
2617///
2618/// \param ArgumentPackIndex The index into the argument pack where this
2619/// argument will be placed. Only valid if the parameter is a parameter pack.
2620///
2621/// \param Converted The checked, converted argument will be added to the
2622/// end of this small vector.
2623///
2624/// \param CTAK Describes how we arrived at this particular template argument:
2625/// explicitly written, deduced, etc.
2626///
2627/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002628bool Sema::CheckTemplateArgument(NamedDecl *Param,
2629 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002630 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002631 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002632 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002633 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002634 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002635 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002636 // Check template type parameters.
2637 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002638 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639
Douglas Gregord9e15302009-11-11 19:41:09 +00002640 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002641 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002642 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002643 // with the template arguments we've seen thus far. But if the
2644 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002645 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002646 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2647 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002648
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002649 if (NTTPType->isDependentType() &&
2650 !isa<TemplateTemplateParmDecl>(Template) &&
2651 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002652 // Do substitution on the type of the non-type template parameter.
2653 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002654 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002655 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002656
2657 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002658 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002659 NTTPType = SubstType(NTTPType,
2660 MultiLevelTemplateArgumentList(TemplateArgs),
2661 NTTP->getLocation(),
2662 NTTP->getDeclName());
2663 // If that worked, check the non-type template parameter type
2664 // for validity.
2665 if (!NTTPType.isNull())
2666 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2667 NTTP->getLocation());
2668 if (NTTPType.isNull())
2669 return true;
2670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671
Douglas Gregore7526412009-11-11 19:31:23 +00002672 switch (Arg.getArgument().getKind()) {
2673 case TemplateArgument::Null:
2674 assert(false && "Should never see a NULL template argument here");
2675 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
Douglas Gregore7526412009-11-11 19:31:23 +00002677 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002678 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002679 ExprResult Res =
2680 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2681 Result, CTAK);
2682 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002683 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684
Douglas Gregor910f8002010-11-07 23:05:16 +00002685 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002686 break;
2687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002688
Douglas Gregore7526412009-11-11 19:31:23 +00002689 case TemplateArgument::Declaration:
2690 case TemplateArgument::Integral:
2691 // We've already checked this template argument, so just copy
2692 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002693 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002694 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695
Douglas Gregore7526412009-11-11 19:31:23 +00002696 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002697 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002698 // We were given a template template argument. It may not be ill-formed;
2699 // see below.
2700 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002701 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2702 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002703 // We have a template argument such as \c T::template X, which we
2704 // parsed as a template template argument. However, since we now
2705 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002706 // template name into an expression.
2707
2708 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2709 Arg.getTemplateNameLoc());
2710
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002711 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002712 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002713 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002714 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002715 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002716
Douglas Gregora7fc9012011-01-05 18:58:31 +00002717 // If we parsed the template argument as a pack expansion, create a
2718 // pack expansion expression.
2719 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002720 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2721 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002722 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002724
Douglas Gregore7526412009-11-11 19:31:23 +00002725 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002726 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2727 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002728 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729
Douglas Gregor910f8002010-11-07 23:05:16 +00002730 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002731 break;
2732 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002733
Douglas Gregore7526412009-11-11 19:31:23 +00002734 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002735 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002736 // therefore cannot be a non-type template argument.
2737 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2738 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739
Douglas Gregore7526412009-11-11 19:31:23 +00002740 Diag(Param->getLocation(), diag::note_template_param_here);
2741 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregore7526412009-11-11 19:31:23 +00002743 case TemplateArgument::Type: {
2744 // We have a non-type template parameter but the template
2745 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002746
Douglas Gregore7526412009-11-11 19:31:23 +00002747 // C++ [temp.arg]p2:
2748 // In a template-argument, an ambiguity between a type-id and
2749 // an expression is resolved to a type-id, regardless of the
2750 // form of the corresponding template-parameter.
2751 //
2752 // We warn specifically about this case, since it can be rather
2753 // confusing for users.
2754 QualType T = Arg.getArgument().getAsType();
2755 SourceRange SR = Arg.getSourceRange();
2756 if (T->isFunctionType())
2757 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2758 else
2759 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2760 Diag(Param->getLocation(), diag::note_template_param_here);
2761 return true;
2762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002763
Douglas Gregore7526412009-11-11 19:31:23 +00002764 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002765 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002766 break;
2767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregore7526412009-11-11 19:31:23 +00002769 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002770 }
2771
2772
Douglas Gregore7526412009-11-11 19:31:23 +00002773 // Check template template parameters.
2774 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002775
Douglas Gregore7526412009-11-11 19:31:23 +00002776 // Substitute into the template parameter list of the template
2777 // template parameter, since previously-supplied template arguments
2778 // may appear within the template template parameter.
2779 {
2780 // Set up a template instantiation context.
2781 LocalInstantiationScope Scope(*this);
2782 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002783 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002784 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
2786 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002787 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002788 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002790 MultiLevelTemplateArgumentList(TemplateArgs)));
2791 if (!TempParm)
2792 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794
Douglas Gregore7526412009-11-11 19:31:23 +00002795 switch (Arg.getArgument().getKind()) {
2796 case TemplateArgument::Null:
2797 assert(false && "Should never see a NULL template argument here");
2798 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799
Douglas Gregore7526412009-11-11 19:31:23 +00002800 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002801 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002802 if (CheckTemplateArgument(TempParm, Arg))
2803 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804
Douglas Gregor910f8002010-11-07 23:05:16 +00002805 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002806 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002807
Douglas Gregore7526412009-11-11 19:31:23 +00002808 case TemplateArgument::Expression:
2809 case TemplateArgument::Type:
2810 // We have a template template parameter but the template
2811 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002812 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2813 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002814 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002817 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002818 "Declaration argument with template template parameter");
2819 break;
2820 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002821 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002822 "Integral argument with template template parameter");
2823 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002824
Douglas Gregore7526412009-11-11 19:31:23 +00002825 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002826 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002827 break;
2828 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002829
Douglas Gregore7526412009-11-11 19:31:23 +00002830 return false;
2831}
2832
Douglas Gregorc15cb382009-02-09 23:23:08 +00002833/// \brief Check that the given template argument list is well-formed
2834/// for specializing the given template.
2835bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2836 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002837 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002838 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002839 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002840 TemplateParameterList *Params = Template->getTemplateParameters();
2841 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002842 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002843 bool Invalid = false;
2844
John McCalld5532b62009-11-23 01:53:49 +00002845 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2846
Mike Stump1eb44332009-09-09 15:08:12 +00002847 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002848 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002850 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002851 (NumArgs < Params->getMinRequiredArguments() &&
2852 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002853 // FIXME: point at either the first arg beyond what we can handle,
2854 // or the '>', depending on whether we have too many or too few
2855 // arguments.
2856 SourceRange Range;
2857 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002858 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002859 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2860 << (NumArgs > NumParams)
2861 << (isa<ClassTemplateDecl>(Template)? 0 :
2862 isa<FunctionTemplateDecl>(Template)? 1 :
2863 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2864 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002865 Diag(Template->getLocation(), diag::note_template_decl_here)
2866 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002867 Invalid = true;
2868 }
Mike Stump1eb44332009-09-09 15:08:12 +00002869
2870 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002871 // [...] The type and form of each template-argument specified in
2872 // a template-id shall match the type and form specified for the
2873 // corresponding parameter declared by the template in its
2874 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002875 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002876 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002877 TemplateParameterList::iterator Param = Params->begin(),
2878 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002879 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002880 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002881 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002882 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002883 // If we have an expanded parameter pack, make sure we don't have too
2884 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002885 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002886 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002887 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002888 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2889 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2890 << true
2891 << (isa<ClassTemplateDecl>(Template)? 0 :
2892 isa<FunctionTemplateDecl>(Template)? 1 :
2893 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2894 << Template;
2895 Diag(Template->getLocation(), diag::note_template_decl_here)
2896 << Params->getSourceRange();
2897 return true;
2898 }
2899 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002900
Douglas Gregorf35f8282009-11-11 21:54:23 +00002901 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002902 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2903 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002904 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002905 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002906
Douglas Gregor14be16b2010-12-20 16:57:52 +00002907 if ((*Param)->isTemplateParameterPack()) {
2908 // The template parameter was a template parameter pack, so take the
2909 // deduced argument and place it on the argument pack. Note that we
2910 // stay on the same template parameter so that we can deduce more
2911 // arguments.
2912 ArgumentPack.push_back(Converted.back());
2913 Converted.pop_back();
2914 } else {
2915 // Move to the next template parameter.
2916 ++Param;
2917 }
2918 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002919 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002921
Douglas Gregor8735b292011-06-03 02:59:40 +00002922 // If we're checking a partial template argument list, we're done.
2923 if (PartialTemplateArgs) {
2924 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2925 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2926 ArgumentPack.data(),
2927 ArgumentPack.size()));
2928
2929 return Invalid;
2930 }
2931
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002932 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002933 // arguments, just break out now and we'll fill in the argument pack below.
2934 if ((*Param)->isTemplateParameterPack())
2935 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002936
Douglas Gregorf35f8282009-11-11 21:54:23 +00002937 // We have a default template argument that we will use.
2938 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002939
Douglas Gregorf35f8282009-11-11 21:54:23 +00002940 // Retrieve the default template argument from the template
2941 // parameter. For each kind of template parameter, we substitute the
2942 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002943 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002944 // the default argument.
2945 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2946 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002947 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002948 break;
2949 }
2950
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002952 Template,
2953 TemplateLoc,
2954 RAngleLoc,
2955 TTP,
2956 Converted);
2957 if (!ArgType)
2958 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002959
Douglas Gregorf35f8282009-11-11 21:54:23 +00002960 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2961 ArgType);
2962 } else if (NonTypeTemplateParmDecl *NTTP
2963 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2964 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002965 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002966 break;
2967 }
2968
John McCall60d7b3a2010-08-24 06:29:42 +00002969 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002970 TemplateLoc,
2971 RAngleLoc,
2972 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002973 Converted);
2974 if (E.isInvalid())
2975 return true;
2976
2977 Expr *Ex = E.takeAs<Expr>();
2978 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2979 } else {
2980 TemplateTemplateParmDecl *TempParm
2981 = cast<TemplateTemplateParmDecl>(*Param);
2982
2983 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002984 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002985 break;
2986 }
2987
Douglas Gregor1d752d72011-03-02 18:46:51 +00002988 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002989 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002990 TemplateLoc,
2991 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002992 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002993 Converted,
2994 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002995 if (Name.isNull())
2996 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002997
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002998 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2999 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003000 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003001
Douglas Gregorf35f8282009-11-11 21:54:23 +00003002 // Introduce an instantiation record that describes where we are using
3003 // the default template argument.
3004 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003005 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003006 SourceRange(TemplateLoc, RAngleLoc));
3007
Douglas Gregorf35f8282009-11-11 21:54:23 +00003008 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003009 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003010 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003011 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003012
Douglas Gregor67714232011-03-03 02:41:12 +00003013 // Core issue 150 (assumed resolution): if this is a template template
3014 // parameter, keep track of the default template arguments from the
3015 // template definition.
3016 if (isTemplateTemplateParameter)
3017 TemplateArgs.addArgument(Arg);
3018
Douglas Gregor14be16b2010-12-20 16:57:52 +00003019 // Move to the next template parameter and argument.
3020 ++Param;
3021 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003022 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003023
Douglas Gregor14be16b2010-12-20 16:57:52 +00003024 // Form argument packs for each of the parameter packs remaining.
3025 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003026 // If we're checking a partial list of template arguments, don't fill
3027 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003028
3029 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003030 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003031 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003032 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3034 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003035 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003036 ArgumentPack.clear();
3037 }
3038 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003039
Douglas Gregor14be16b2010-12-20 16:57:52 +00003040 ++Param;
3041 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
Douglas Gregorc15cb382009-02-09 23:23:08 +00003043 return Invalid;
3044}
3045
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003046namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003047 class UnnamedLocalNoLinkageFinder
3048 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003049 {
3050 Sema &S;
3051 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003053 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003054
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003055 public:
3056 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3057
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003058 bool Visit(QualType T) {
3059 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003060 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003062#define TYPE(Class, Parent) \
3063 bool Visit##Class##Type(const Class##Type *);
3064#define ABSTRACT_TYPE(Class, Parent) \
3065 bool Visit##Class##Type(const Class##Type *) { return false; }
3066#define NON_CANONICAL_TYPE(Class, Parent) \
3067 bool Visit##Class##Type(const Class##Type *) { return false; }
3068#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003069
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003070 bool VisitTagDecl(const TagDecl *Tag);
3071 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3072 };
3073}
3074
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 return false;
3077}
3078
3079bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3080 return Visit(T->getElementType());
3081}
3082
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003083bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003084 return Visit(T->getPointeeType());
3085}
3086
3087bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003088 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003089 return Visit(T->getPointeeType());
3090}
3091
3092bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003093 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003094 return Visit(T->getPointeeType());
3095}
3096
3097bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003098 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003099 return Visit(T->getPointeeType());
3100}
3101
3102bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003103 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003104 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3105}
3106
3107bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003108 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003109 return Visit(T->getElementType());
3110}
3111
3112bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003113 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003114 return Visit(T->getElementType());
3115}
3116
3117bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003118 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003119 return Visit(T->getElementType());
3120}
3121
3122bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003123 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003124 return Visit(T->getElementType());
3125}
3126
3127bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003128 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003129 return Visit(T->getElementType());
3130}
3131
3132bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3133 return Visit(T->getElementType());
3134}
3135
3136bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3137 return Visit(T->getElementType());
3138}
3139
3140bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3141 const FunctionProtoType* T) {
3142 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003143 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003144 A != AEnd; ++A) {
3145 if (Visit(*A))
3146 return true;
3147 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003148
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003149 return Visit(T->getResultType());
3150}
3151
3152bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3153 const FunctionNoProtoType* T) {
3154 return Visit(T->getResultType());
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3158 const UnresolvedUsingType*) {
3159 return false;
3160}
3161
3162bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3163 return false;
3164}
3165
3166bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3167 return Visit(T->getUnderlyingType());
3168}
3169
3170bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3171 return false;
3172}
3173
Sean Huntca63c202011-05-24 22:41:36 +00003174bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3175 const UnaryTransformType*) {
3176 return false;
3177}
3178
Richard Smith34b41d92011-02-20 03:19:35 +00003179bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3180 return Visit(T->getDeducedType());
3181}
3182
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003183bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3184 return VisitTagDecl(T->getDecl());
3185}
3186
3187bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3188 return VisitTagDecl(T->getDecl());
3189}
3190
3191bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3192 const TemplateTypeParmType*) {
3193 return false;
3194}
3195
Douglas Gregorc3069d62011-01-14 02:55:32 +00003196bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3197 const SubstTemplateTypeParmPackType *) {
3198 return false;
3199}
3200
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003201bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3202 const TemplateSpecializationType*) {
3203 return false;
3204}
3205
3206bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3207 const InjectedClassNameType* T) {
3208 return VisitTagDecl(T->getDecl());
3209}
3210
3211bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3212 const DependentNameType* T) {
3213 return VisitNestedNameSpecifier(T->getQualifier());
3214}
3215
3216bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3217 const DependentTemplateSpecializationType* T) {
3218 return VisitNestedNameSpecifier(T->getQualifier());
3219}
3220
Douglas Gregor7536dd52010-12-20 02:24:11 +00003221bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3222 const PackExpansionType* T) {
3223 return Visit(T->getPattern());
3224}
3225
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003226bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3227 return false;
3228}
3229
3230bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3231 const ObjCInterfaceType *) {
3232 return false;
3233}
3234
3235bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3236 const ObjCObjectPointerType *) {
3237 return false;
3238}
3239
3240bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3241 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3242 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3243 << S.Context.getTypeDeclType(Tag) << SR;
3244 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245 }
3246
Richard Smith162e1c12011-04-15 14:24:37 +00003247 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003248 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3249 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3250 return true;
3251 }
3252
3253 return false;
3254}
3255
3256bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3257 NestedNameSpecifier *NNS) {
3258 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3259 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003260
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003261 switch (NNS->getKind()) {
3262 case NestedNameSpecifier::Identifier:
3263 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003264 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003265 case NestedNameSpecifier::Global:
3266 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003267
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003268 case NestedNameSpecifier::TypeSpec:
3269 case NestedNameSpecifier::TypeSpecWithTemplate:
3270 return Visit(QualType(NNS->getAsType(), 0));
3271 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003272 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003273}
3274
3275
Douglas Gregorc15cb382009-02-09 23:23:08 +00003276/// \brief Check a template argument against its corresponding
3277/// template type parameter.
3278///
3279/// This routine implements the semantics of C++ [temp.arg.type]. It
3280/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003281bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003282 TypeSourceInfo *ArgInfo) {
3283 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003284 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003285 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003286
3287 if (Arg->isVariablyModifiedType()) {
3288 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003289 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003290 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003291 }
3292
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003293 // C++03 [temp.arg.type]p2:
3294 // A local type, a type with no linkage, an unnamed type or a type
3295 // compounded from any of these types shall not be used as a
3296 // template-argument for a template type-parameter.
3297 //
3298 // C++0x allows these, and even in C++03 we allow them as an extension with
3299 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003300 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003301 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3302 (void)Finder.Visit(Context.getCanonicalType(Arg));
3303 }
3304
Douglas Gregorc15cb382009-02-09 23:23:08 +00003305 return false;
3306}
3307
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003308/// \brief Checks whether the given template argument is the address
3309/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003310static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003311CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3312 NonTypeTemplateParmDecl *Param,
3313 QualType ParamType,
3314 Expr *ArgIn,
3315 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003316 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003317 Expr *Arg = ArgIn;
3318 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319
3320 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003321 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003322
3323 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003324 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003325 // A template-argument for a non-type, non-template
3326 // template-parameter shall be one of: [...]
3327 //
3328 // -- the address of an object or function with external
3329 // linkage, including function templates and function
3330 // template-ids but excluding non-static class members,
3331 // expressed as & id-expression where the & is optional if
3332 // the name refers to a function or array, or if the
3333 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003334
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003335 // In C++98/03 mode, give an extension warning on any extra parentheses.
3336 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3337 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003338 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003339 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003340 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003341 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003342 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003343 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003344 }
3345
3346 Arg = Parens->getSubExpr();
3347 }
3348
John McCall91a57552011-07-15 05:09:51 +00003349 while (SubstNonTypeTemplateParmExpr *subst =
3350 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3351 Arg = subst->getReplacement()->IgnoreImpCasts();
3352
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353 bool AddressTaken = false;
3354 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003355 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003356 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003357 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003358 AddressTaken = true;
3359 AddrOpLoc = UnOp->getOperatorLoc();
3360 }
Francois Picheta343a412011-04-29 09:08:14 +00003361 }
John McCall91a57552011-07-15 05:09:51 +00003362
3363 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3364 Converted = TemplateArgument(ArgIn);
3365 return false;
3366 }
3367
3368 while (SubstNonTypeTemplateParmExpr *subst =
3369 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3370 Arg = subst->getReplacement()->IgnoreImpCasts();
3371
3372 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003373 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003374 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3375 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003376 S.Diag(Param->getLocation(), diag::note_template_param_here);
3377 return true;
3378 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003379
3380 // Stop checking the precise nature of the argument if it is value dependent,
3381 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003382 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003383 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003384 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003385 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003386
Douglas Gregorb7a09262010-04-01 18:32:35 +00003387 if (!isa<ValueDecl>(DRE->getDecl())) {
3388 S.Diag(Arg->getSourceRange().getBegin(),
3389 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003390 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003391 S.Diag(Param->getLocation(), diag::note_template_param_here);
3392 return true;
3393 }
3394
3395 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003396
3397 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003398 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3399 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003400 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003401 S.Diag(Param->getLocation(), diag::note_template_param_here);
3402 return true;
3403 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003404
3405 // Cannot refer to non-static member functions
3406 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003407 if (!Method->isStatic()) {
3408 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003409 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003410 S.Diag(Param->getLocation(), diag::note_template_param_here);
3411 return true;
3412 }
Mike Stump1eb44332009-09-09 15:08:12 +00003413
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003414 // Functions must have external linkage.
3415 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003416 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003417 S.Diag(Arg->getSourceRange().getBegin(),
3418 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003419 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003421 << true;
3422 return true;
3423 }
3424
3425 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003426 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003427
Douglas Gregorb7a09262010-04-01 18:32:35 +00003428 // If the template parameter has pointer type, the function decays.
3429 if (ParamType->isPointerType() && !AddressTaken)
3430 ArgType = S.Context.getPointerType(Func->getType());
3431 else if (AddressTaken && ParamType->isReferenceType()) {
3432 // If we originally had an address-of operator, but the
3433 // parameter has reference type, complain and (if things look
3434 // like they will work) drop the address-of operator.
3435 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3436 ParamType.getNonReferenceType())) {
3437 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3438 << ParamType;
3439 S.Diag(Param->getLocation(), diag::note_template_param_here);
3440 return true;
3441 }
3442
3443 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3444 << ParamType
3445 << FixItHint::CreateRemoval(AddrOpLoc);
3446 S.Diag(Param->getLocation(), diag::note_template_param_here);
3447
3448 ArgType = Func->getType();
3449 }
3450 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003451 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003452 S.Diag(Arg->getSourceRange().getBegin(),
3453 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003456 << true;
3457 return true;
3458 }
3459
Douglas Gregorb7a09262010-04-01 18:32:35 +00003460 // A value of reference type is not an object.
3461 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003462 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463 diag::err_template_arg_reference_var)
3464 << Var->getType() << Arg->getSourceRange();
3465 S.Diag(Param->getLocation(), diag::note_template_param_here);
3466 return true;
3467 }
3468
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003469 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003470 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003471
3472 // If the template parameter has pointer type, we must have taken
3473 // the address of this object.
3474 if (ParamType->isReferenceType()) {
3475 if (AddressTaken) {
3476 // If we originally had an address-of operator, but the
3477 // parameter has reference type, complain and (if things look
3478 // like they will work) drop the address-of operator.
3479 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3480 ParamType.getNonReferenceType())) {
3481 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3482 << ParamType;
3483 S.Diag(Param->getLocation(), diag::note_template_param_here);
3484 return true;
3485 }
3486
3487 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3488 << ParamType
3489 << FixItHint::CreateRemoval(AddrOpLoc);
3490 S.Diag(Param->getLocation(), diag::note_template_param_here);
3491
3492 ArgType = Var->getType();
3493 }
3494 } else if (!AddressTaken && ParamType->isPointerType()) {
3495 if (Var->getType()->isArrayType()) {
3496 // Array-to-pointer decay.
3497 ArgType = S.Context.getArrayDecayedType(Var->getType());
3498 } else {
3499 // If the template parameter has pointer type but the address of
3500 // this object was not taken, complain and (possibly) recover by
3501 // taking the address of the entity.
3502 ArgType = S.Context.getPointerType(Var->getType());
3503 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3504 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3505 << ParamType;
3506 S.Diag(Param->getLocation(), diag::note_template_param_here);
3507 return true;
3508 }
3509
3510 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3511 << ParamType
3512 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3513
3514 S.Diag(Param->getLocation(), diag::note_template_param_here);
3515 }
3516 }
3517 } else {
3518 // We found something else, but we don't know specifically what it is.
3519 S.Diag(Arg->getSourceRange().getBegin(),
3520 diag::err_template_arg_not_object_or_func)
3521 << Arg->getSourceRange();
3522 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3523 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003524 }
Mike Stump1eb44332009-09-09 15:08:12 +00003525
John McCallf85e1932011-06-15 23:02:42 +00003526 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003527 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003528 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003529 S.IsQualificationConversion(ArgType, ParamType, false,
3530 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003531 // For pointer-to-object types, qualification conversions are
3532 // permitted.
3533 } else {
3534 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3535 if (!ParamRef->getPointeeType()->isFunctionType()) {
3536 // C++ [temp.arg.nontype]p5b3:
3537 // For a non-type template-parameter of type reference to
3538 // object, no conversions apply. The type referred to by the
3539 // reference may be more cv-qualified than the (otherwise
3540 // identical) type of the template- argument. The
3541 // template-parameter is bound directly to the
3542 // template-argument, which shall be an lvalue.
3543
3544 // FIXME: Other qualifiers?
3545 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3546 unsigned ArgQuals = ArgType.getCVRQualifiers();
3547
3548 if ((ParamQuals | ArgQuals) != ParamQuals) {
3549 S.Diag(Arg->getSourceRange().getBegin(),
3550 diag::err_template_arg_ref_bind_ignores_quals)
3551 << ParamType << Arg->getType()
3552 << Arg->getSourceRange();
3553 S.Diag(Param->getLocation(), diag::note_template_param_here);
3554 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003555 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003556 }
3557 }
3558
3559 // At this point, the template argument refers to an object or
3560 // function with external linkage. We now need to check whether the
3561 // argument and parameter types are compatible.
3562 if (!S.Context.hasSameUnqualifiedType(ArgType,
3563 ParamType.getNonReferenceType())) {
3564 // We can't perform this conversion or binding.
3565 if (ParamType->isReferenceType())
3566 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003567 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003568 else
3569 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003570 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003571 S.Diag(Param->getLocation(), diag::note_template_param_here);
3572 return true;
3573 }
3574 }
3575
3576 // Create the template argument.
3577 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003578 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003579 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003580}
3581
3582/// \brief Checks whether the given template argument is a pointer to
3583/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003584bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003585 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003586 bool Invalid = false;
3587
3588 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003589 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003590 Arg = Cast->getSubExpr();
3591
3592 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003593 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003594 // A template-argument for a non-type, non-template
3595 // template-parameter shall be one of: [...]
3596 //
3597 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003598 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003599
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003600 // In C++98/03 mode, give an extension warning on any extra parentheses.
3601 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3602 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003603 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003604 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003605 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003606 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003607 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003608 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003609 }
3610
3611 Arg = Parens->getSubExpr();
3612 }
3613
John McCall91a57552011-07-15 05:09:51 +00003614 while (SubstNonTypeTemplateParmExpr *subst =
3615 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3616 Arg = subst->getReplacement()->IgnoreImpCasts();
3617
Douglas Gregorcaddba02009-11-12 18:38:13 +00003618 // A pointer-to-member constant written &Class::member.
3619 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003620 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003621 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3622 if (DRE && !DRE->getQualifier())
3623 DRE = 0;
3624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003625 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003626 // A constant of pointer-to-member type.
3627 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3628 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3629 if (VD->getType()->isMemberPointerType()) {
3630 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003632 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3633 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003634 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003635 else
3636 Converted = TemplateArgument(VD->getCanonicalDecl());
3637 return Invalid;
3638 }
3639 }
3640 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003641
Douglas Gregorcaddba02009-11-12 18:38:13 +00003642 DRE = 0;
3643 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003644
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003645 if (!DRE)
3646 return Diag(Arg->getSourceRange().getBegin(),
3647 diag::err_template_arg_not_pointer_to_member_form)
3648 << Arg->getSourceRange();
3649
3650 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3651 assert((isa<FieldDecl>(DRE->getDecl()) ||
3652 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3653 "Only non-static member pointers can make it here");
3654
3655 // Okay: this is the address of a non-static member, and therefore
3656 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003657 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003658 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003659 else
3660 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003661 return Invalid;
3662 }
3663
3664 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003665 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003666 diag::err_template_arg_not_pointer_to_member_form)
3667 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003668 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003669 diag::note_template_arg_refers_here);
3670 return true;
3671}
3672
Douglas Gregorc15cb382009-02-09 23:23:08 +00003673/// \brief Check a template argument against its corresponding
3674/// non-type template parameter.
3675///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003676/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003677/// If an error occurred, it returns ExprError(); otherwise, it
3678/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003679/// InstantiatedParamType is the type of the non-type template
3680/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003681ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3682 QualType InstantiatedParamType, Expr *Arg,
3683 TemplateArgument &Converted,
3684 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003685 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3686
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003687 // If either the parameter has a dependent type or the argument is
3688 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003689 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3690 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003691 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003692 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003693 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003694
3695 // C++ [temp.arg.nontype]p5:
3696 // The following conversions are performed on each expression used
3697 // as a non-type template-argument. If a non-type
3698 // template-argument cannot be converted to the type of the
3699 // corresponding template-parameter then the program is
3700 // ill-formed.
3701 //
3702 // -- for a non-type template-parameter of integral or
3703 // enumeration type, integral promotions (4.5) and integral
3704 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003705 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003706 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003707 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003708 // C++ [temp.arg.nontype]p1:
3709 // A template-argument for a non-type, non-template
3710 // template-parameter shall be one of:
3711 //
3712 // -- an integral constant-expression of integral or enumeration
3713 // type; or
3714 // -- the name of a non-type template-parameter; or
3715 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003716 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003717 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003718 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003719 diag::err_template_arg_not_integral_or_enumeral)
3720 << ArgType << Arg->getSourceRange();
3721 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003722 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003723 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003724 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003725 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3726 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003727 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003728 }
3729
Douglas Gregor02024a92010-03-28 02:42:43 +00003730 // From here on out, all we care about are the unqualified forms
3731 // of the parameter and argument types.
3732 ParamType = ParamType.getUnqualifiedType();
3733 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003734
3735 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003736 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003737 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003738 } else if (CTAK == CTAK_Deduced) {
3739 // C++ [temp.deduct.type]p17:
3740 // If, in the declaration of a function template with a non-type
3741 // template-parameter, the non-type template- parameter is used
3742 // in an expression in the function parameter-list and, if the
3743 // corresponding template-argument is deduced, the
3744 // template-argument type shall match the type of the
3745 // template-parameter exactly, except that a template-argument
3746 // deduced from an array bound may be of any integral type.
3747 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3748 << ArgType << ParamType;
3749 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003750 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003751 } else if (ParamType->isBooleanType()) {
3752 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003753 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003754 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3755 !ParamType->isEnumeralType()) {
3756 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003757 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003758 } else {
3759 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003760 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003761 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003762 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003763 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003764 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003765 }
3766
Douglas Gregorc7469372011-05-04 21:55:00 +00003767 // Add the value of this argument to the list of converted
3768 // arguments. We use the bitwidth and signedness of the template
3769 // parameter.
3770 if (Arg->isValueDependent()) {
3771 // The argument is value-dependent. Create a new
3772 // TemplateArgument with the converted expression.
3773 Converted = TemplateArgument(Arg);
3774 return Owned(Arg);
3775 }
3776
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003777 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003778 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003779 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003780
Douglas Gregorc7469372011-05-04 21:55:00 +00003781 if (ParamType->isBooleanType()) {
3782 // Value must be zero or one.
3783 Value = Value != 0;
3784 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3785 if (Value.getBitWidth() != AllowedBits)
3786 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003787 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003788 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003789 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003790
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003791 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003792 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003793 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003794 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003795 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003796 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003797
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003798 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003799 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003800 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003801 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3802 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3803 << Arg->getSourceRange();
3804 Diag(Param->getLocation(), diag::note_template_param_here);
3805 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003806
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003807 // Complain if we overflowed the template parameter's type.
3808 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003809 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003810 RequiredBits = OldValue.getActiveBits();
3811 else if (OldValue.isUnsigned())
3812 RequiredBits = OldValue.getActiveBits() + 1;
3813 else
3814 RequiredBits = OldValue.getMinSignedBits();
3815 if (RequiredBits > AllowedBits) {
3816 Diag(Arg->getSourceRange().getBegin(),
3817 diag::warn_template_arg_too_large)
3818 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3819 << Arg->getSourceRange();
3820 Diag(Param->getLocation(), diag::note_template_param_here);
3821 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003822 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003823
John McCall833ca992009-10-29 08:12:44 +00003824 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003825 ParamType->isEnumeralType()
3826 ? Context.getCanonicalType(ParamType)
3827 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003828 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003829 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003830
John McCall6bb80172010-03-30 21:47:33 +00003831 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3832
Douglas Gregorb7a09262010-04-01 18:32:35 +00003833 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3834 // from a template argument of type std::nullptr_t to a non-type
3835 // template parameter of type pointer to object, pointer to
3836 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003837 if (ArgType->isNullPtrType()) {
3838 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3839 Converted = TemplateArgument((NamedDecl *)0);
3840 return Owned(Arg);
3841 }
3842
3843 if (ParamType->isNullPtrType()) {
3844 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3845 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3846 return Owned(Arg);
3847 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003848 }
3849
Douglas Gregorb86b0572009-02-11 01:18:59 +00003850 // Handle pointer-to-function, reference-to-function, and
3851 // pointer-to-member-function all in (roughly) the same way.
3852 if (// -- For a non-type template-parameter of type pointer to
3853 // function, only the function-to-pointer conversion (4.3) is
3854 // applied. If the template-argument represents a set of
3855 // overloaded functions (or a pointer to such), the matching
3856 // function is selected from the set (13.4).
3857 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003858 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003859 // -- For a non-type template-parameter of type reference to
3860 // function, no conversions apply. If the template-argument
3861 // represents a set of overloaded functions, the matching
3862 // function is selected from the set (13.4).
3863 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003864 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003865 // -- For a non-type template-parameter of type pointer to
3866 // member function, no conversions apply. If the
3867 // template-argument represents a set of overloaded member
3868 // functions, the matching member function is selected from
3869 // the set (13.4).
3870 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003871 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003872 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003873
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003874 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003875 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003876 true,
3877 FoundResult)) {
3878 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003879 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003880
3881 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3882 ArgType = Arg->getType();
3883 } else
John Wiegley429bb272011-04-08 18:41:53 +00003884 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003885 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003886
John Wiegley429bb272011-04-08 18:41:53 +00003887 if (!ParamType->isMemberPointerType()) {
3888 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3889 ParamType,
3890 Arg, Converted))
3891 return ExprError();
3892 return Owned(Arg);
3893 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003894
John McCallf85e1932011-06-15 23:02:42 +00003895 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003896 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003897 false, ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003898 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003899 } else if (!Context.hasSameUnqualifiedType(ArgType,
3900 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003901 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003902 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003903 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003904 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003905 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003906 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003907 }
Mike Stump1eb44332009-09-09 15:08:12 +00003908
John Wiegley429bb272011-04-08 18:41:53 +00003909 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3910 return ExprError();
3911 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003912 }
3913
Chris Lattnerfe90de72009-02-20 21:37:53 +00003914 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003915 // -- for a non-type template-parameter of type pointer to
3916 // object, qualification conversions (4.4) and the
3917 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003918 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003919 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003920 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003921
John Wiegley429bb272011-04-08 18:41:53 +00003922 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3923 ParamType,
3924 Arg, Converted))
3925 return ExprError();
3926 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003927 }
Mike Stump1eb44332009-09-09 15:08:12 +00003928
Ted Kremenek6217b802009-07-29 21:53:49 +00003929 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003930 // -- For a non-type template-parameter of type reference to
3931 // object, no conversions apply. The type referred to by the
3932 // reference may be more cv-qualified than the (otherwise
3933 // identical) type of the template-argument. The
3934 // template-parameter is bound directly to the
3935 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003936 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003937 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003938
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003939 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003940 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3941 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003942 true,
3943 FoundResult)) {
3944 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003945 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003946
3947 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3948 ArgType = Arg->getType();
3949 } else
John Wiegley429bb272011-04-08 18:41:53 +00003950 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003951 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003952
John Wiegley429bb272011-04-08 18:41:53 +00003953 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3954 ParamType,
3955 Arg, Converted))
3956 return ExprError();
3957 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003958 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003959
3960 // -- For a non-type template-parameter of type pointer to data
3961 // member, qualification conversions (4.4) are applied.
3962 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3963
John McCallf85e1932011-06-15 23:02:42 +00003964 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003965 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003966 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003967 } else if (IsQualificationConversion(ArgType, ParamType, false,
3968 ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003969 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003970 } else {
3971 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003972 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003973 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003974 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003975 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003976 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003977 }
3978
John Wiegley429bb272011-04-08 18:41:53 +00003979 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3980 return ExprError();
3981 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003982}
3983
3984/// \brief Check a template argument against its corresponding
3985/// template template parameter.
3986///
3987/// This routine implements the semantics of C++ [temp.arg.template].
3988/// It returns true if an error occurred, and false otherwise.
3989bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003990 const TemplateArgumentLoc &Arg) {
3991 TemplateName Name = Arg.getArgument().getAsTemplate();
3992 TemplateDecl *Template = Name.getAsTemplateDecl();
3993 if (!Template) {
3994 // Any dependent template name is fine.
3995 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3996 return false;
3997 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003998
Richard Smith3e4c6c42011-05-05 21:57:07 +00003999 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004000 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004001 // the name of a class template or an alias template, expressed as an
4002 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004003 // primary class templates are considered when matching the
4004 // template template argument with the corresponding parameter;
4005 // partial specializations are not considered even if their
4006 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004007 //
4008 // Note that we also allow template template parameters here, which
4009 // will happen when we are dealing with, e.g., class template
4010 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004011 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004012 !isa<TemplateTemplateParmDecl>(Template) &&
4013 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004014 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004015 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004016 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004017 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004018 << Template;
4019 }
4020
4021 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4022 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004023 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004024 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004025 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004026}
4027
Douglas Gregor02024a92010-03-28 02:42:43 +00004028/// \brief Given a non-type template argument that refers to a
4029/// declaration and the type of its corresponding non-type template
4030/// parameter, produce an expression that properly refers to that
4031/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004032ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004033Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4034 QualType ParamType,
4035 SourceLocation Loc) {
4036 assert(Arg.getKind() == TemplateArgument::Declaration &&
4037 "Only declaration template arguments permitted here");
4038 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4039
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004040 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004041 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4042 // If the value is a class member, we might have a pointer-to-member.
4043 // Determine whether the non-type template template parameter is of
4044 // pointer-to-member type. If so, we need to build an appropriate
4045 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4046 // would refer to the member itself.
4047 if (ParamType->isMemberPointerType()) {
4048 QualType ClassType
4049 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4050 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004051 = NestedNameSpecifier::Create(Context, 0, false,
4052 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004053 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004054 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004055
4056 // The actual value-ness of this is unimportant, but for
4057 // internal consistency's sake, references to instance methods
4058 // are r-values.
4059 ExprValueKind VK = VK_LValue;
4060 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4061 VK = VK_RValue;
4062
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004063 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004064 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004065 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004066 Loc,
4067 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004068 if (RefExpr.isInvalid())
4069 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004070
John McCall2de56d12010-08-25 11:45:40 +00004071 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004072
Douglas Gregorc0c83002010-04-30 21:46:38 +00004073 // We might need to perform a trailing qualification conversion, since
4074 // the element type on the parameter could be more qualified than the
4075 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004076 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004077 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004078 ParamType.getUnqualifiedType(), false,
4079 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004080 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081
Douglas Gregor02024a92010-03-28 02:42:43 +00004082 assert(!RefExpr.isInvalid() &&
4083 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004084 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004085 return move(RefExpr);
4086 }
4087 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004088
Douglas Gregor02024a92010-03-28 02:42:43 +00004089 QualType T = VD->getType().getNonReferenceType();
4090 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004091 // When the non-type template parameter is a pointer, take the
4092 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004093 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004094 if (RefExpr.isInvalid())
4095 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004096
4097 if (T->isFunctionType() || T->isArrayType()) {
4098 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004099 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4100 if (RefExpr.isInvalid())
4101 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004102
4103 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004104 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004105
Douglas Gregorb7a09262010-04-01 18:32:35 +00004106 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004107 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004108 }
4109
John McCallf89e55a2010-11-18 06:31:45 +00004110 ExprValueKind VK = VK_RValue;
4111
Douglas Gregor02024a92010-03-28 02:42:43 +00004112 // If the non-type template parameter has reference type, qualify the
4113 // resulting declaration reference with the extra qualifiers on the
4114 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004115 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4116 VK = VK_LValue;
4117 T = Context.getQualifiedType(T,
4118 TargetRef->getPointeeType().getQualifiers());
4119 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004120
John McCallf89e55a2010-11-18 06:31:45 +00004121 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004122}
4123
4124/// \brief Construct a new expression that refers to the given
4125/// integral template argument with the given source-location
4126/// information.
4127///
4128/// This routine takes care of the mapping from an integral template
4129/// argument (which may have any integral type) to the appropriate
4130/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004131ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004132Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4133 SourceLocation Loc) {
4134 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004135 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004136 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004137 if (T->isAnyCharacterType()) {
4138 CharacterLiteral::CharacterKind Kind;
4139 if (T->isWideCharType())
4140 Kind = CharacterLiteral::Wide;
4141 else if (T->isChar16Type())
4142 Kind = CharacterLiteral::UTF16;
4143 else if (T->isChar32Type())
4144 Kind = CharacterLiteral::UTF32;
4145 else
4146 Kind = CharacterLiteral::Ascii;
4147
Douglas Gregor02024a92010-03-28 02:42:43 +00004148 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004149 Arg.getAsIntegral()->getZExtValue(),
4150 Kind, T, Loc));
4151 }
4152
Douglas Gregor02024a92010-03-28 02:42:43 +00004153 if (T->isBooleanType())
4154 return Owned(new (Context) CXXBoolLiteralExpr(
4155 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004156 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004157
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004158 if (T->isNullPtrType())
4159 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4160
Chris Lattner223de242011-04-25 20:37:58 +00004161 // If this is an enum type that we're instantiating, we need to use an integer
4162 // type the same size as the enumerator. We don't want to build an
4163 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004164 QualType BT;
4165 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004166 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004167 else
4168 BT = T;
4169
John McCall4e9272d2011-07-15 07:47:58 +00004170 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4171 if (T->isEnumeralType()) {
4172 // FIXME: This is a hack. We need a better way to handle substituted
4173 // non-type template parameters.
4174 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4175 Context.getTrivialTypeSourceInfo(T, Loc),
4176 Loc, Loc);
4177 }
4178
4179 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004180}
4181
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004182/// \brief Match two template parameters within template parameter lists.
4183static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4184 bool Complain,
4185 Sema::TemplateParameterListEqualKind Kind,
4186 SourceLocation TemplateArgLoc) {
4187 // Check the actual kind (type, non-type, template).
4188 if (Old->getKind() != New->getKind()) {
4189 if (Complain) {
4190 unsigned NextDiag = diag::err_template_param_different_kind;
4191 if (TemplateArgLoc.isValid()) {
4192 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4193 NextDiag = diag::note_template_param_different_kind;
4194 }
4195 S.Diag(New->getLocation(), NextDiag)
4196 << (Kind != Sema::TPL_TemplateMatch);
4197 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4198 << (Kind != Sema::TPL_TemplateMatch);
4199 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004201 return false;
4202 }
4203
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004204 // Check that both are parameter packs are neither are parameter packs.
4205 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004206 // template template parameter, the template template parameter can have
4207 // a parameter pack where the template template argument does not.
4208 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4209 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4210 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004211 if (Complain) {
4212 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4213 if (TemplateArgLoc.isValid()) {
4214 S.Diag(TemplateArgLoc,
4215 diag::err_template_arg_template_params_mismatch);
4216 NextDiag = diag::note_template_parameter_pack_non_pack;
4217 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004218
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004219 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4220 : isa<NonTypeTemplateParmDecl>(New)? 1
4221 : 2;
4222 S.Diag(New->getLocation(), NextDiag)
4223 << ParamKind << New->isParameterPack();
4224 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4225 << ParamKind << Old->isParameterPack();
4226 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004228 return false;
4229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004231 // For non-type template parameters, check the type of the parameter.
4232 if (NonTypeTemplateParmDecl *OldNTTP
4233 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4234 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004235
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004236 // If we are matching a template template argument to a template
4237 // template parameter and one of the non-type template parameter types
4238 // is dependent, then we must wait until template instantiation time
4239 // to actually compare the arguments.
4240 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4241 (OldNTTP->getType()->isDependentType() ||
4242 NewNTTP->getType()->isDependentType()))
4243 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004244
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004245 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4246 if (Complain) {
4247 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4248 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004249 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004250 diag::err_template_arg_template_params_mismatch);
4251 NextDiag = diag::note_template_nontype_parm_different_type;
4252 }
4253 S.Diag(NewNTTP->getLocation(), NextDiag)
4254 << NewNTTP->getType()
4255 << (Kind != Sema::TPL_TemplateMatch);
4256 S.Diag(OldNTTP->getLocation(),
4257 diag::note_template_nontype_parm_prev_declaration)
4258 << OldNTTP->getType();
4259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004261 return false;
4262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004264 return true;
4265 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004266
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004267 // For template template parameters, check the template parameter types.
4268 // The template parameter lists of template template
4269 // parameters must agree.
4270 if (TemplateTemplateParmDecl *OldTTP
4271 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004272 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004273 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4274 OldTTP->getTemplateParameters(),
4275 Complain,
4276 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004277 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004278 : Kind),
4279 TemplateArgLoc);
4280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004282 return true;
4283}
Douglas Gregor02024a92010-03-28 02:42:43 +00004284
Douglas Gregora0347822011-01-13 00:08:50 +00004285/// \brief Diagnose a known arity mismatch when comparing template argument
4286/// lists.
4287static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004288void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004289 TemplateParameterList *New,
4290 TemplateParameterList *Old,
4291 Sema::TemplateParameterListEqualKind Kind,
4292 SourceLocation TemplateArgLoc) {
4293 unsigned NextDiag = diag::err_template_param_list_different_arity;
4294 if (TemplateArgLoc.isValid()) {
4295 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4296 NextDiag = diag::note_template_param_list_different_arity;
4297 }
4298 S.Diag(New->getTemplateLoc(), NextDiag)
4299 << (New->size() > Old->size())
4300 << (Kind != Sema::TPL_TemplateMatch)
4301 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4302 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4303 << (Kind != Sema::TPL_TemplateMatch)
4304 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4305}
4306
Douglas Gregorddc29e12009-02-06 22:42:48 +00004307/// \brief Determine whether the given template parameter lists are
4308/// equivalent.
4309///
Mike Stump1eb44332009-09-09 15:08:12 +00004310/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004311/// source code as part of a new template declaration.
4312///
4313/// \param Old The old template parameter list, typically found via
4314/// name lookup of the template declared with this template parameter
4315/// list.
4316///
4317/// \param Complain If true, this routine will produce a diagnostic if
4318/// the template parameter lists are not equivalent.
4319///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004320/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004321///
4322/// \param TemplateArgLoc If this source location is valid, then we
4323/// are actually checking the template parameter list of a template
4324/// argument (New) against the template parameter list of its
4325/// corresponding template template parameter (Old). We produce
4326/// slightly different diagnostics in this scenario.
4327///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004328/// \returns True if the template parameter lists are equal, false
4329/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004330bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004331Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4332 TemplateParameterList *Old,
4333 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004334 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004335 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004336 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4337 if (Complain)
4338 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4339 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004340
4341 return false;
4342 }
4343
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004344 // C++0x [temp.arg.template]p3:
4345 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004346 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004347 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004348 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004349 // template-parameter-list of P. [...]
4350 TemplateParameterList::iterator NewParm = New->begin();
4351 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004352 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004353 OldParmEnd = Old->end();
4354 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004355 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4356 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004357 if (NewParm == NewParmEnd) {
4358 if (Complain)
4359 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4360 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004361
Douglas Gregora0347822011-01-13 00:08:50 +00004362 return false;
4363 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004364
Douglas Gregora0347822011-01-13 00:08:50 +00004365 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4366 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004367 return false;
4368
Douglas Gregora0347822011-01-13 00:08:50 +00004369 ++NewParm;
4370 continue;
4371 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004372
Douglas Gregora0347822011-01-13 00:08:50 +00004373 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004374 // [...] When P's template- parameter-list contains a template parameter
4375 // pack (14.5.3), the template parameter pack will match zero or more
4376 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004377 // template-parameter-list of A with the same type and form as the
4378 // template parameter pack in P (ignoring whether those template
4379 // parameters are template parameter packs).
4380 for (; NewParm != NewParmEnd; ++NewParm) {
4381 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4382 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004383 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004384 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004385 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004386
Douglas Gregora0347822011-01-13 00:08:50 +00004387 // Make sure we exhausted all of the arguments.
4388 if (NewParm != NewParmEnd) {
4389 if (Complain)
4390 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4391 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004392
Douglas Gregora0347822011-01-13 00:08:50 +00004393 return false;
4394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004395
Douglas Gregorddc29e12009-02-06 22:42:48 +00004396 return true;
4397}
4398
4399/// \brief Check whether a template can be declared within this scope.
4400///
4401/// If the template declaration is valid in this scope, returns
4402/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004403bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004404Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004405 // Find the nearest enclosing declaration scope.
4406 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4407 (S->getFlags() & Scope::TemplateParamScope) != 0)
4408 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004409
Douglas Gregorddc29e12009-02-06 22:42:48 +00004410 // C++ [temp]p2:
4411 // A template-declaration can appear only as a namespace scope or
4412 // class scope declaration.
4413 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004414 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4415 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004416 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004417 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004418
Eli Friedman1503f772009-07-31 01:43:05 +00004419 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004420 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004421
4422 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4423 return false;
4424
Mike Stump1eb44332009-09-09 15:08:12 +00004425 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004426 diag::err_template_outside_namespace_or_class_scope)
4427 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004428}
Douglas Gregorcc636682009-02-17 23:15:12 +00004429
Douglas Gregord5cb8762009-10-07 00:13:32 +00004430/// \brief Determine what kind of template specialization the given declaration
4431/// is.
4432static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4433 if (!D)
4434 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004435
Douglas Gregorf6b11852009-10-08 15:14:33 +00004436 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4437 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004438 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4439 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004440 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4441 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004442
Douglas Gregord5cb8762009-10-07 00:13:32 +00004443 return TSK_Undeclared;
4444}
4445
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004446/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004447/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004448///
Douglas Gregor9302da62009-10-14 23:50:59 +00004449/// This routine determines whether a template specialization can be declared
4450/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004451///
4452/// \param S the semantic analysis object for which this check is being
4453/// performed.
4454///
4455/// \param Specialized the entity being specialized or instantiated, which
4456/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004457/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004458/// member class).
4459///
4460/// \param PrevDecl the previous declaration of this entity, if any.
4461///
4462/// \param Loc the location of the explicit specialization or instantiation of
4463/// this entity.
4464///
4465/// \param IsPartialSpecialization whether this is a partial specialization of
4466/// a class template.
4467///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004468/// \returns true if there was an error that we cannot recover from, false
4469/// otherwise.
4470static bool CheckTemplateSpecializationScope(Sema &S,
4471 NamedDecl *Specialized,
4472 NamedDecl *PrevDecl,
4473 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004474 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004475 // Keep these "kind" numbers in sync with the %select statements in the
4476 // various diagnostics emitted by this routine.
4477 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004478 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004479 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004480 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004481 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004482 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004483 EntityKind = 3;
4484 else if (isa<VarDecl>(Specialized))
4485 EntityKind = 4;
4486 else if (isa<RecordDecl>(Specialized))
4487 EntityKind = 5;
4488 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004489 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4490 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004491 return true;
4492 }
4493
Douglas Gregor88b70942009-02-25 22:02:03 +00004494 // C++ [temp.expl.spec]p2:
4495 // An explicit specialization shall be declared in the namespace
4496 // of which the template is a member, or, for member templates, in
4497 // the namespace of which the enclosing class or enclosing class
4498 // template is a member. An explicit specialization of a member
4499 // function, member class or static data member of a class
4500 // template shall be declared in the namespace of which the class
4501 // template is a member. Such a declaration may also be a
4502 // definition. If the declaration is not a definition, the
4503 // specialization may be defined later in the name- space in which
4504 // the explicit specialization was declared, or in a namespace
4505 // that encloses the one in which the explicit specialization was
4506 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004507 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004508 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004509 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004510 return true;
4511 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004512
Douglas Gregor0a407472009-10-07 17:30:37 +00004513 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004514 if (S.getLangOptions().Microsoft) {
4515 // Do not warn for class scope explicit specialization during
4516 // instantiation, warning was already emitted during pattern
4517 // semantic analysis.
4518 if (!S.ActiveTemplateInstantiations.size())
4519 S.Diag(Loc, diag::ext_function_specialization_in_class)
4520 << Specialized;
4521 } else {
4522 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4523 << Specialized;
4524 return true;
4525 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004526 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004527
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004528 // C++ [temp.class.spec]p6:
4529 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004530 // in any namespace scope in which its definition may be defined (14.5.1
4531 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004532 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004533 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004534 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004535 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004537 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4538 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004539 // C++ [temp.exp.spec]p2:
4540 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004542 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004543 // An explicit specialization of a member function, member class or
4544 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004545 // namespace of which the class template is a member.
4546 //
4547 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004548 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004549 // the specialized template.
4550 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4551 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004552 bool IsCPlusPlus0xExtension
4553 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004554 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004555 S.Diag(Loc, IsCPlusPlus0xExtension
4556 ? diag::ext_template_spec_decl_out_of_scope_global
4557 : diag::err_template_spec_decl_out_of_scope_global)
4558 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004559 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004560 S.Diag(Loc, IsCPlusPlus0xExtension
4561 ? diag::ext_template_spec_decl_out_of_scope
4562 : diag::err_template_spec_decl_out_of_scope)
4563 << EntityKind << Specialized
4564 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004565
Douglas Gregor9302da62009-10-14 23:50:59 +00004566 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4567 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004568 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570
4571 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004572 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004573 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004574 // specializations of function templates, static data members, and member
4575 // functions, so we skip the check here for those kinds of entities.
4576 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004577 // Should we refactor that check, so that it occurs later?
4578 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004579 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4580 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004581 if (isa<TranslationUnitDecl>(SpecializedContext))
4582 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4583 << EntityKind << Specialized;
4584 else if (isa<NamespaceDecl>(SpecializedContext))
4585 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4586 << EntityKind << Specialized
4587 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588
Douglas Gregor9302da62009-10-14 23:50:59 +00004589 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004590 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591
Douglas Gregord5cb8762009-10-07 00:13:32 +00004592 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004593
Douglas Gregor88b70942009-02-25 22:02:03 +00004594 return false;
4595}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596
Douglas Gregorbacb9492011-01-03 21:13:47 +00004597/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4598/// that checks non-type template partial specialization arguments.
4599static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4600 NonTypeTemplateParmDecl *Param,
4601 const TemplateArgument *Args,
4602 unsigned NumArgs) {
4603 for (unsigned I = 0; I != NumArgs; ++I) {
4604 if (Args[I].getKind() == TemplateArgument::Pack) {
4605 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004607 Args[I].pack_size()))
4608 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004609
Douglas Gregore94866f2009-06-12 21:21:02 +00004610 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004611 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004612
Douglas Gregorbacb9492011-01-03 21:13:47 +00004613 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004614 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004615 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004616 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004617
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004618 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004619 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4620 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004621
4622 // Strip off any implicit casts we added as part of type checking.
4623 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4624 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004625
Douglas Gregore94866f2009-06-12 21:21:02 +00004626 // C++ [temp.class.spec]p8:
4627 // A non-type argument is non-specialized if it is the name of a
4628 // non-type parameter. All other non-type arguments are
4629 // specialized.
4630 //
4631 // Below, we check the two conditions that only apply to
4632 // specialized non-type arguments, so skip any non-specialized
4633 // arguments.
4634 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004635 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004636 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004637
Douglas Gregore94866f2009-06-12 21:21:02 +00004638 // C++ [temp.class.spec]p9:
4639 // Within the argument list of a class template partial
4640 // specialization, the following restrictions apply:
4641 // -- A partially specialized non-type argument expression
4642 // shall not involve a template parameter of the partial
4643 // specialization except when the argument expression is a
4644 // simple identifier.
4645 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004646 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004647 diag::err_dependent_non_type_arg_in_partial_spec)
4648 << ArgExpr->getSourceRange();
4649 return true;
4650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004651
Douglas Gregore94866f2009-06-12 21:21:02 +00004652 // -- The type of a template parameter corresponding to a
4653 // specialized non-type argument shall not be dependent on a
4654 // parameter of the specialization.
4655 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004656 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004657 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4658 << Param->getType()
4659 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004660 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004661 return true;
4662 }
4663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004664
Douglas Gregorbacb9492011-01-03 21:13:47 +00004665 return false;
4666}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004667
Douglas Gregorbacb9492011-01-03 21:13:47 +00004668/// \brief Check the non-type template arguments of a class template
4669/// partial specialization according to C++ [temp.class.spec]p9.
4670///
4671/// \param TemplateParams the template parameters of the primary class
4672/// template.
4673///
4674/// \param TemplateArg the template arguments of the class template
4675/// partial specialization.
4676///
4677/// \returns true if there was an error, false otherwise.
4678static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4679 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004680 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004681 const TemplateArgument *ArgList = TemplateArgs.data();
4682
4683 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4684 NonTypeTemplateParmDecl *Param
4685 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4686 if (!Param)
4687 continue;
4688
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004689 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004690 &ArgList[I], 1))
4691 return true;
4692 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004693
4694 return false;
4695}
4696
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004697/// \brief Retrieve the previous declaration of the given declaration.
4698static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4699 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4700 return VD->getPreviousDeclaration();
4701 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4702 return FD->getPreviousDeclaration();
4703 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4704 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004705 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004706 return TD->getPreviousDeclaration();
4707 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4708 return FTD->getPreviousDeclaration();
4709 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4710 return CTD->getPreviousDeclaration();
4711 return 0;
4712}
4713
John McCalld226f652010-08-21 09:40:31 +00004714DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004715Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4716 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004717 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004718 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004719 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004720 SourceLocation TemplateNameLoc,
4721 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004722 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004723 SourceLocation RAngleLoc,
4724 AttributeList *Attr,
4725 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004726 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004727
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004728 // NOTE: KWLoc is the location of the tag keyword. This will instead
4729 // store the location of the outermost template keyword in the declaration.
4730 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4731 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4732
Douglas Gregorcc636682009-02-17 23:15:12 +00004733 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004734 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004735 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004736 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4737
4738 if (!ClassTemplate) {
4739 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004740 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004741 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4742 return true;
4743 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004744
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004745 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004746 bool isPartialSpecialization = false;
4747
Douglas Gregor88b70942009-02-25 22:02:03 +00004748 // Check the validity of the template headers that introduce this
4749 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004750 // FIXME: We probably shouldn't complain about these headers for
4751 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004752 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004753 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004754 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4755 TemplateNameLoc,
4756 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004757 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004758 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004759 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004760 isExplicitSpecialization,
4761 Invalid);
4762 if (Invalid)
4763 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004764
Douglas Gregor05396e22009-08-25 17:23:04 +00004765 if (TemplateParams && TemplateParams->size() > 0) {
4766 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004767
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004768 if (TUK == TUK_Friend) {
4769 Diag(KWLoc, diag::err_partial_specialization_friend)
4770 << SourceRange(LAngleLoc, RAngleLoc);
4771 return true;
4772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004773
Douglas Gregor05396e22009-08-25 17:23:04 +00004774 // C++ [temp.class.spec]p10:
4775 // The template parameter list of a specialization shall not
4776 // contain default template argument values.
4777 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4778 Decl *Param = TemplateParams->getParam(I);
4779 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4780 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004781 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004782 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004783 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004784 }
4785 } else if (NonTypeTemplateParmDecl *NTTP
4786 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4787 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004788 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004789 diag::err_default_arg_in_partial_spec)
4790 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004791 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004792 }
4793 } else {
4794 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004795 if (TTP->hasDefaultArgument()) {
4796 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004797 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004798 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004799 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004800 }
4801 }
4802 }
Douglas Gregora735b202009-10-13 14:39:41 +00004803 } else if (TemplateParams) {
4804 if (TUK == TUK_Friend)
4805 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004806 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004807 SourceRange(TemplateParams->getTemplateLoc(),
4808 TemplateParams->getRAngleLoc()))
4809 << SourceRange(LAngleLoc, RAngleLoc);
4810 else
4811 isExplicitSpecialization = true;
4812 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004813 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004814 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004815 isExplicitSpecialization = true;
4816 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004817
Douglas Gregorcc636682009-02-17 23:15:12 +00004818 // Check that the specialization uses the same tag kind as the
4819 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004820 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4821 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004822 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004823 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004824 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004825 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004826 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004827 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004828 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004829 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004830 diag::note_previous_use);
4831 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4832 }
4833
Douglas Gregor40808ce2009-03-09 23:48:35 +00004834 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004835 TemplateArgumentListInfo TemplateArgs;
4836 TemplateArgs.setLAngleLoc(LAngleLoc);
4837 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004838 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004839
Douglas Gregor925910d2011-01-03 20:35:03 +00004840 // Check for unexpanded parameter packs in any of the template arguments.
4841 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004842 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004843 UPPC_PartialSpecialization))
4844 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845
Douglas Gregorcc636682009-02-17 23:15:12 +00004846 // Check that the template argument list is well-formed for this
4847 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004848 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004849 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4850 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004851 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004852
Douglas Gregor910f8002010-11-07 23:05:16 +00004853 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004854 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004855
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004856 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004857 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004858 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004859 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004860 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004861 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004862 return true;
4863
Douglas Gregor561f8122011-07-01 01:22:09 +00004864 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004866 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004868 TemplateArgs.size(),
4869 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004870 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4871 << ClassTemplate->getDeclName();
4872 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004873 }
4874 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004875
Douglas Gregorcc636682009-02-17 23:15:12 +00004876 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004877 ClassTemplateSpecializationDecl *PrevDecl = 0;
4878
4879 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004880 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004881 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004882 = ClassTemplate->findPartialSpecialization(Converted.data(),
4883 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004884 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004885 else
4886 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004887 = ClassTemplate->findSpecialization(Converted.data(),
4888 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004889
4890 ClassTemplateSpecializationDecl *Specialization = 0;
4891
Douglas Gregor88b70942009-02-25 22:02:03 +00004892 // Check whether we can declare a class template specialization in
4893 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004894 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004895 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4896 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004897 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004898 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004899
Douglas Gregorb88e8882009-07-30 17:40:51 +00004900 // The canonical type
4901 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004902 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004903 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004904 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004905 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004906 // arguments was referenced but not declared, or we're only
4907 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004908 // declaration node as our own, updating its source location and
4909 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004910 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004911 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004912 if (TemplateParameterLists.size() > 0) {
4913 Specialization->setTemplateParameterListsInfo(Context,
4914 TemplateParameterLists.size(),
4915 (TemplateParameterList**) TemplateParameterLists.release());
4916 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004917 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004918 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004919 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004920 // Build the canonical type that describes the converted template
4921 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004922 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4923 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004924 Converted.data(),
4925 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004926
4927 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004928 ClassTemplate->getInjectedClassNameSpecialization())) {
4929 // C++ [temp.class.spec]p9b3:
4930 //
4931 // -- The argument list of the specialization shall not be identical
4932 // to the implicit argument list of the primary template.
4933 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4934 << (TUK == TUK_Definition)
4935 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4936 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4937 ClassTemplate->getIdentifier(),
4938 TemplateNameLoc,
4939 Attr,
4940 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004941 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004942 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004943 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004944 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004945
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004946 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004947 ClassTemplatePartialSpecializationDecl *PrevPartial
4948 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004949 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004950 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004951 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004952 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004953 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004954 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004955 TemplateParams,
4956 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004957 Converted.data(),
4958 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004959 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004960 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004961 PrevPartial,
4962 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004963 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004964 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004965 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004966 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004967 (TemplateParameterList**) TemplateParameterLists.release());
4968 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004969
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004970 if (!PrevPartial)
4971 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004972 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004973
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004974 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004975 // template specialization, make a note of that.
4976 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4977 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004978
Douglas Gregor031a5882009-06-13 00:26:55 +00004979 // Check that all of the template parameters of the class template
4980 // partial specialization are deducible from the template
4981 // arguments. If not, this class template partial specialization
4982 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004983 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00004984 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004985 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004986 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004987 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004988 unsigned NumNonDeducible = 0;
4989 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4990 if (!DeducibleParams[I])
4991 ++NumNonDeducible;
4992
4993 if (NumNonDeducible) {
4994 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4995 << (NumNonDeducible > 1)
4996 << SourceRange(TemplateNameLoc, RAngleLoc);
4997 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4998 if (!DeducibleParams[I]) {
4999 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5000 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005001 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005002 diag::note_partial_spec_unused_parameter)
5003 << Param->getDeclName();
5004 else
Mike Stump1eb44332009-09-09 15:08:12 +00005005 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005006 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005007 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005008 }
5009 }
5010 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005011 } else {
5012 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005013 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005014 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005015 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005016 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005017 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005018 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005019 Converted.data(),
5020 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005021 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005022 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005023 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005024 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005025 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005026 (TemplateParameterList**) TemplateParameterLists.release());
5027 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005028
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005029 if (!PrevDecl)
5030 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005031
5032 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005033 }
5034
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005035 // C++ [temp.expl.spec]p6:
5036 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005037 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005038 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005039 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005040 // use occurs; no diagnostic is required.
5041 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005042 bool Okay = false;
5043 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5044 // Is there any previous explicit specialization declaration?
5045 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5046 Okay = true;
5047 break;
5048 }
5049 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005050
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005051 if (!Okay) {
5052 SourceRange Range(TemplateNameLoc, RAngleLoc);
5053 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5054 << Context.getTypeDeclType(Specialization) << Range;
5055
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005056 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005057 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005059 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005060 return true;
5061 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005062 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005063
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005064 // If this is not a friend, note that this is an explicit specialization.
5065 if (TUK != TUK_Friend)
5066 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005067
5068 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005069 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005070 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005071 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005072 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005073 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005074 Diag(Def->getLocation(), diag::note_previous_definition);
5075 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005076 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005077 }
5078 }
5079
John McCall7f1b9872010-12-18 03:30:47 +00005080 if (Attr)
5081 ProcessDeclAttributeList(S, Specialization, Attr);
5082
Douglas Gregorfc705b82009-02-26 22:19:44 +00005083 // Build the fully-sugared type for this class template
5084 // specialization as the user wrote in the specialization
5085 // itself. This means that we'll pretty-print the type retrieved
5086 // from the specialization's declaration the way that the user
5087 // actually wrote the specialization, rather than formatting the
5088 // name based on the "canonical" representation used to store the
5089 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005090 TypeSourceInfo *WrittenTy
5091 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5092 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005093 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005094 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005095 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005096 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005097 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005098
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005099 // C++ [temp.expl.spec]p9:
5100 // A template explicit specialization is in the scope of the
5101 // namespace in which the template was defined.
5102 //
5103 // We actually implement this paragraph where we set the semantic
5104 // context (in the creation of the ClassTemplateSpecializationDecl),
5105 // but we also maintain the lexical context where the actual
5106 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005107 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005108
Douglas Gregorcc636682009-02-17 23:15:12 +00005109 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005110 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005111 Specialization->startDefinition();
5112
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005113 if (TUK == TUK_Friend) {
5114 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5115 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005116 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005117 /*FIXME:*/KWLoc);
5118 Friend->setAccess(AS_public);
5119 CurContext->addDecl(Friend);
5120 } else {
5121 // Add the specialization into its lexical context, so that it can
5122 // be seen when iterating through the list of declarations in that
5123 // context. However, specializations are not found by name lookup.
5124 CurContext->addDecl(Specialization);
5125 }
John McCalld226f652010-08-21 09:40:31 +00005126 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005127}
Douglas Gregord57959a2009-03-27 23:10:48 +00005128
John McCalld226f652010-08-21 09:40:31 +00005129Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005130 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005131 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005132 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5133}
5134
John McCalld226f652010-08-21 09:40:31 +00005135Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005136 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005137 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005138 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005139 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005140
Douglas Gregor52591bf2009-06-24 00:54:41 +00005141 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005142 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005143 }
Mike Stump1eb44332009-09-09 15:08:12 +00005144
Douglas Gregor52591bf2009-06-24 00:54:41 +00005145 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005146
John McCalld226f652010-08-21 09:40:31 +00005147 Decl *DP = HandleDeclarator(ParentScope, D,
5148 move(TemplateParameterLists),
5149 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005150 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005151 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005152 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005153 FunctionTemplate->getTemplatedDecl());
5154 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5155 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5156 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005157}
5158
John McCall75042392010-02-11 01:33:53 +00005159/// \brief Strips various properties off an implicit instantiation
5160/// that has just been explicitly specialized.
5161static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005162 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005163
5164 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5165 FD->setInlineSpecified(false);
5166 }
5167}
5168
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005169/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005170/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005171/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005172/// new specialization/instantiation will have any effect.
5173///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005175/// instantiation.
5176///
5177/// \param NewTSK the kind of the new explicit specialization or instantiation.
5178///
5179/// \param PrevDecl the previous declaration of the entity.
5180///
5181/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5182///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005183/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005184/// declaration was instantiated (either implicitly or explicitly).
5185///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005186/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005187/// specialization or instantiation has no effect and should be ignored.
5188///
5189/// \returns true if there was an error that should prevent the introduction of
5190/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005191bool
5192Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5193 TemplateSpecializationKind NewTSK,
5194 NamedDecl *PrevDecl,
5195 TemplateSpecializationKind PrevTSK,
5196 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005197 bool &HasNoEffect) {
5198 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005199
Douglas Gregor454885e2009-10-15 15:54:05 +00005200 switch (NewTSK) {
5201 case TSK_Undeclared:
5202 case TSK_ImplicitInstantiation:
5203 assert(false && "Don't check implicit instantiations here");
5204 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005205
Douglas Gregor454885e2009-10-15 15:54:05 +00005206 case TSK_ExplicitSpecialization:
5207 switch (PrevTSK) {
5208 case TSK_Undeclared:
5209 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005211 // explicitly specialized or has merely been mentioned without any
5212 // instantiation.
5213 return false;
5214
5215 case TSK_ImplicitInstantiation:
5216 if (PrevPointOfInstantiation.isInvalid()) {
5217 // The declaration itself has not actually been instantiated, so it is
5218 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005219 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005220 return false;
5221 }
5222 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005223
Douglas Gregor454885e2009-10-15 15:54:05 +00005224 case TSK_ExplicitInstantiationDeclaration:
5225 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005226 assert((PrevTSK == TSK_ImplicitInstantiation ||
5227 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005228 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229
Douglas Gregor454885e2009-10-15 15:54:05 +00005230 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005232 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005233 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005234 // implicit instantiation to take place, in every translation unit in
5235 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005236 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5237 // Is there any previous explicit specialization declaration?
5238 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5239 return false;
5240 }
5241
Douglas Gregor0d035142009-10-27 18:42:08 +00005242 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005243 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005244 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005245 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005246
Douglas Gregor454885e2009-10-15 15:54:05 +00005247 return true;
5248 }
5249 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005250
Douglas Gregor454885e2009-10-15 15:54:05 +00005251 case TSK_ExplicitInstantiationDeclaration:
5252 switch (PrevTSK) {
5253 case TSK_ExplicitInstantiationDeclaration:
5254 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005255 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005256 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005257
Douglas Gregor454885e2009-10-15 15:54:05 +00005258 case TSK_Undeclared:
5259 case TSK_ImplicitInstantiation:
5260 // We're explicitly instantiating something that may have already been
5261 // implicitly instantiated; that's fine.
5262 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005263
Douglas Gregor454885e2009-10-15 15:54:05 +00005264 case TSK_ExplicitSpecialization:
5265 // C++0x [temp.explicit]p4:
5266 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005267 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005268 // specialization for that template, the explicit instantiation has no
5269 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005270 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005271 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005272
Douglas Gregor454885e2009-10-15 15:54:05 +00005273 case TSK_ExplicitInstantiationDefinition:
5274 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005275 // If an entity is the subject of both an explicit instantiation
5276 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005277 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005279 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005280 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005281 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005282 assert(PrevPointOfInstantiation.isValid() &&
5283 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005284 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 return false;
5286 }
5287 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005288
Douglas Gregor454885e2009-10-15 15:54:05 +00005289 case TSK_ExplicitInstantiationDefinition:
5290 switch (PrevTSK) {
5291 case TSK_Undeclared:
5292 case TSK_ImplicitInstantiation:
5293 // We're explicitly instantiating something that may have already been
5294 // implicitly instantiated; that's fine.
5295 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005296
Douglas Gregor454885e2009-10-15 15:54:05 +00005297 case TSK_ExplicitSpecialization:
5298 // C++ DR 259, C++0x [temp.explicit]p4:
5299 // For a given set of template parameters, if an explicit
5300 // instantiation of a template appears after a declaration of
5301 // an explicit specialization for that template, the explicit
5302 // instantiation has no effect.
5303 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005304 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005305 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005306 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005307 if (!getLangOptions().CPlusPlus0x) {
5308 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005309 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005310 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005311 diag::note_previous_template_specialization);
5312 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005313 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005314 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315
Douglas Gregor454885e2009-10-15 15:54:05 +00005316 case TSK_ExplicitInstantiationDeclaration:
5317 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005318 // were previously asked to suppress instantiations. That's fine.
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_ExplicitInstantiationDefinition:
5322 // C++0x [temp.spec]p5:
5323 // For a given template and a given set of template-arguments,
5324 // - an explicit instantiation definition shall appear at most once
5325 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005326 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005327 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005328 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005329 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005330 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005332 }
5333 break;
5334 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005335
Douglas Gregor454885e2009-10-15 15:54:05 +00005336 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005337
Douglas Gregor454885e2009-10-15 15:54:05 +00005338 return false;
5339}
5340
John McCallaf2094e2010-04-08 09:05:18 +00005341/// \brief Perform semantic analysis for the given dependent function
5342/// template specialization. The only possible way to get a dependent
5343/// function template specialization is with a friend declaration,
5344/// like so:
5345///
5346/// template <class T> void foo(T);
5347/// template <class T> class A {
5348/// friend void foo<>(T);
5349/// };
5350///
5351/// There really isn't any useful analysis we can do here, so we
5352/// just store the information.
5353bool
5354Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5355 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5356 LookupResult &Previous) {
5357 // Remove anything from Previous that isn't a function template in
5358 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005359 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005360 LookupResult::Filter F = Previous.makeFilter();
5361 while (F.hasNext()) {
5362 NamedDecl *D = F.next()->getUnderlyingDecl();
5363 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005364 !FDLookupContext->InEnclosingNamespaceSetOf(
5365 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005366 F.erase();
5367 }
5368 F.done();
5369
5370 // Should this be diagnosed here?
5371 if (Previous.empty()) return true;
5372
5373 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5374 ExplicitTemplateArgs);
5375 return false;
5376}
5377
Abramo Bagnarae03db982010-05-20 15:32:11 +00005378/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005379/// specialization.
5380///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005381/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005382/// explicit function template specialization. On successful completion,
5383/// the function declaration \p FD will become a function template
5384/// specialization.
5385///
5386/// \param FD the function declaration, which will be updated to become a
5387/// function template specialization.
5388///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005389/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5390/// if any. Note that this may be valid info even when 0 arguments are
5391/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5392/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005393///
Francois Pichet59e7c562011-07-08 06:21:47 +00005394/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005395/// this function specialization.
5396bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005397Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005398 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005399 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005400 // The set of function template specializations that could match this
5401 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005402 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005403
Sebastian Redl7a126a42010-08-31 00:36:30 +00005404 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005405 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5406 I != E; ++I) {
5407 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5408 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005409 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005410 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005411 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5412 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005413 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005414
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005415 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005416 // A trailing template-argument can be left unspecified in the
5417 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005418 // provided it can be deduced from the function argument type.
5419 // Perform template argument deduction to determine whether we may be
5420 // specializing this template.
5421 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005422 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005423 FunctionDecl *Specialization = 0;
5424 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005425 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005426 FD->getType(),
5427 Specialization,
5428 Info)) {
5429 // FIXME: Template argument deduction failed; record why it failed, so
5430 // that we can provide nifty diagnostics.
5431 (void)TDK;
5432 continue;
5433 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005434
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005435 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005436 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005437 }
5438 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005439
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005440 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005441 UnresolvedSetIterator Result
5442 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005443 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005444 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005445 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005446 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005447 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005448 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005449 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005450 return true;
John McCallc373d482010-01-27 01:50:18 +00005451
5452 // Ignore access information; it doesn't figure into redeclaration checking.
5453 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005454
5455 FunctionTemplateSpecializationInfo *SpecInfo
5456 = Specialization->getTemplateSpecializationInfo();
5457 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005458
5459 // Note: do not overwrite location info if previous template
5460 // specialization kind was explicit.
5461 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5462 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5463 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005464
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005465 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005466 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005467
5468 // If this is a friend declaration, then we're not really declaring
5469 // an explicit specialization.
5470 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005471
Douglas Gregord5cb8762009-10-07 00:13:32 +00005472 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005473 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005474 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005475 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005477 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005478 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005479
5480 // C++ [temp.expl.spec]p6:
5481 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005482 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005483 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005484 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005485 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005486 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005487 if (!isFriend &&
5488 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005489 TSK_ExplicitSpecialization,
5490 Specialization,
5491 SpecInfo->getTemplateSpecializationKind(),
5492 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005493 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005494 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005495
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005496 // Mark the prior declaration as an explicit specialization, so that later
5497 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005498 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005499 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005500 MarkUnusedFileScopedDecl(Specialization);
5501 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005502
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005503 // Turn the given function declaration into a function template
5504 // specialization, with the template arguments from the previous
5505 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005506 // Take copies of (semantic and syntactic) template argument lists.
5507 const TemplateArgumentList* TemplArgs = new (Context)
5508 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5509 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5510 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005511 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005512 TemplArgs, /*InsertPos=*/0,
5513 SpecInfo->getTemplateSpecializationKind(),
5514 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005515 FD->setStorageClass(Specialization->getStorageClass());
5516
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005517 // The "previous declaration" for this function template specialization is
5518 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005519 Previous.clear();
5520 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005521 return false;
5522}
5523
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005524/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005525/// specialization.
5526///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005527/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005528/// explicit member function specialization. On successful completion,
5529/// the function declaration \p FD will become a member function
5530/// specialization.
5531///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005532/// \param Member the member declaration, which will be updated to become a
5533/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005534///
John McCall68263142009-11-18 22:49:29 +00005535/// \param Previous the set of declarations, one of which may be specialized
5536/// by this function specialization; the set will be modified to contain the
5537/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005538bool
John McCall68263142009-11-18 22:49:29 +00005539Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005540 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005541
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005542 // Try to find the member we are instantiating.
5543 NamedDecl *Instantiation = 0;
5544 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005545 MemberSpecializationInfo *MSInfo = 0;
5546
John McCall68263142009-11-18 22:49:29 +00005547 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005548 // Nowhere to look anyway.
5549 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005550 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5551 I != E; ++I) {
5552 NamedDecl *D = (*I)->getUnderlyingDecl();
5553 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005554 if (Context.hasSameType(Function->getType(), Method->getType())) {
5555 Instantiation = Method;
5556 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005557 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005558 break;
5559 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005560 }
5561 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005562 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005563 VarDecl *PrevVar;
5564 if (Previous.isSingleResult() &&
5565 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005566 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005567 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005568 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005569 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005570 }
5571 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005572 CXXRecordDecl *PrevRecord;
5573 if (Previous.isSingleResult() &&
5574 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5575 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005576 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005577 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005578 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005580
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005581 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005582 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005583 // specializations are always out-of-line, the caller will complain about
5584 // this mismatch later.
5585 return false;
5586 }
John McCall77e8b112010-04-13 20:37:33 +00005587
5588 // If this is a friend, just bail out here before we start turning
5589 // things into explicit specializations.
5590 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5591 // Preserve instantiation information.
5592 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5593 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5594 cast<CXXMethodDecl>(InstantiatedFrom),
5595 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5596 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5597 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5598 cast<CXXRecordDecl>(InstantiatedFrom),
5599 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5600 }
5601
5602 Previous.clear();
5603 Previous.addDecl(Instantiation);
5604 return false;
5605 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005606
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005607 // Make sure that this is a specialization of a member.
5608 if (!InstantiatedFrom) {
5609 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5610 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005611 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5612 return true;
5613 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005614
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005615 // C++ [temp.expl.spec]p6:
5616 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005617 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005618 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005619 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005620 // use occurs; no diagnostic is required.
5621 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005622
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005623 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005624 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5625 TSK_ExplicitSpecialization,
5626 Instantiation,
5627 MSInfo->getTemplateSpecializationKind(),
5628 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005629 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005630 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005631
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005632 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005633 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005634 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005635 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005636 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005637 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005638
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005639 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005640 // the original declaration to note that it is an explicit specialization
5641 // (if it was previously an implicit instantiation). This latter step
5642 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005643 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005644 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5645 if (InstantiationFunction->getTemplateSpecializationKind() ==
5646 TSK_ImplicitInstantiation) {
5647 InstantiationFunction->setTemplateSpecializationKind(
5648 TSK_ExplicitSpecialization);
5649 InstantiationFunction->setLocation(Member->getLocation());
5650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005651
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005652 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5653 cast<CXXMethodDecl>(InstantiatedFrom),
5654 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005655 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005656 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005657 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5658 if (InstantiationVar->getTemplateSpecializationKind() ==
5659 TSK_ImplicitInstantiation) {
5660 InstantiationVar->setTemplateSpecializationKind(
5661 TSK_ExplicitSpecialization);
5662 InstantiationVar->setLocation(Member->getLocation());
5663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005664
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005665 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5666 cast<VarDecl>(InstantiatedFrom),
5667 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005668 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005669 } else {
5670 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005671 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5672 if (InstantiationClass->getTemplateSpecializationKind() ==
5673 TSK_ImplicitInstantiation) {
5674 InstantiationClass->setTemplateSpecializationKind(
5675 TSK_ExplicitSpecialization);
5676 InstantiationClass->setLocation(Member->getLocation());
5677 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005678
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005679 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005680 cast<CXXRecordDecl>(InstantiatedFrom),
5681 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005684 // Save the caller the trouble of having to figure out which declaration
5685 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005686 Previous.clear();
5687 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005688 return false;
5689}
5690
Douglas Gregor558c0322009-10-14 23:41:34 +00005691/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005692///
5693/// \returns true if a serious error occurs, false otherwise.
5694static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005695 SourceLocation InstLoc,
5696 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005697 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5698 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699
Douglas Gregor669eed82010-07-13 00:10:04 +00005700 if (CurContext->isRecord()) {
5701 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5702 << D;
5703 return true;
5704 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005705
Douglas Gregor558c0322009-10-14 23:41:34 +00005706 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005707 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005708 // template.
5709 //
5710 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005711 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005712 !CurContext->Encloses(OrigContext)) {
5713 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714 S.Diag(InstLoc,
5715 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005716 diag::err_explicit_instantiation_out_of_scope
5717 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005718 << D << NS;
5719 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005721 S.getLangOptions().CPlusPlus0x?
5722 diag::err_explicit_instantiation_must_be_global
5723 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005724 << D;
5725 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005726 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005727 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005728
Douglas Gregor558c0322009-10-14 23:41:34 +00005729 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730 // If the name declared in the explicit instantiation is an unqualified
5731 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005732 // its template is declared or, if that namespace is inline (7.3.1), any
5733 // namespace from its enclosing namespace set.
5734 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005735 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005736
5737 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005738 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005739
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005740 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005741 S.getLangOptions().CPlusPlus0x?
5742 diag::err_explicit_instantiation_unqualified_wrong_namespace
5743 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005744 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005745 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005746 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005747}
5748
5749/// \brief Determine whether the given scope specifier has a template-id in it.
5750static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5751 if (!SS.isSet())
5752 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005753
Douglas Gregor558c0322009-10-14 23:41:34 +00005754 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005756 // or a static data member of a class template specialization, the name of
5757 // the class template specialization in the qualified-id for the member
5758 // name shall be a simple-template-id.
5759 //
5760 // C++98 has the same restriction, just worded differently.
5761 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5762 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005763 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005764 if (isa<TemplateSpecializationType>(T))
5765 return true;
5766
5767 return false;
5768}
5769
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005770// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005771DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005772Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005773 SourceLocation ExternLoc,
5774 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005775 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005776 SourceLocation KWLoc,
5777 const CXXScopeSpec &SS,
5778 TemplateTy TemplateD,
5779 SourceLocation TemplateNameLoc,
5780 SourceLocation LAngleLoc,
5781 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005782 SourceLocation RAngleLoc,
5783 AttributeList *Attr) {
5784 // Find the class template we're specializing
5785 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005786 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005787 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5788
5789 // Check that the specialization uses the same tag kind as the
5790 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005791 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5792 assert(Kind != TTK_Enum &&
5793 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005794 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005795 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005796 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005797 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005798 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005799 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005800 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005801 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005802 diag::note_previous_use);
5803 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5804 }
5805
Douglas Gregor558c0322009-10-14 23:41:34 +00005806 // C++0x [temp.explicit]p2:
5807 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005808 // definition and an explicit instantiation declaration. An explicit
5809 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005810 TemplateSpecializationKind TSK
5811 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5812 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005814 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005815 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005816 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005817
5818 // Check that the template argument list is well-formed for this
5819 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005820 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005821 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5822 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005823 return true;
5824
Douglas Gregor910f8002010-11-07 23:05:16 +00005825 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005826 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005827
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005828 // Find the class template specialization declaration that
5829 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005830 void *InsertPos = 0;
5831 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005832 = ClassTemplate->findSpecialization(Converted.data(),
5833 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005834
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005835 TemplateSpecializationKind PrevDecl_TSK
5836 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5837
Douglas Gregord5cb8762009-10-07 00:13:32 +00005838 // C++0x [temp.explicit]p2:
5839 // [...] An explicit instantiation shall appear in an enclosing
5840 // namespace of its template. [...]
5841 //
5842 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005843 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5844 SS.isSet()))
5845 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005846
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005847 ClassTemplateSpecializationDecl *Specialization = 0;
5848
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005849 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005850 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005851 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005852 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005853 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005854 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005855 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005856
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005857 // Even though HasNoEffect == true means that this explicit instantiation
5858 // has no effect on semantics, we go on to put its syntax in the AST.
5859
5860 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5861 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005862 // Since the only prior class template specialization with these
5863 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005864 // declaration node as our own, updating the source location
5865 // for the template name to reflect our new declaration.
5866 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005867 Specialization = PrevDecl;
5868 Specialization->setLocation(TemplateNameLoc);
5869 PrevDecl = 0;
5870 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005871 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005872
Douglas Gregor52604ab2009-09-11 21:19:12 +00005873 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005874 // Create a new class template specialization declaration node for
5875 // this explicit specialization.
5876 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005877 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005878 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005879 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005880 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005881 Converted.data(),
5882 Converted.size(),
5883 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005884 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005885
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005886 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005887 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005888 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005889 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005890 }
5891
5892 // Build the fully-sugared type for this explicit instantiation as
5893 // the user wrote in the explicit instantiation itself. This means
5894 // that we'll pretty-print the type retrieved from the
5895 // specialization's declaration the way that the user actually wrote
5896 // the explicit instantiation, rather than formatting the name based
5897 // on the "canonical" representation used to store the template
5898 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005899 TypeSourceInfo *WrittenTy
5900 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5901 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005902 Context.getTypeDeclType(Specialization));
5903 Specialization->setTypeAsWritten(WrittenTy);
5904 TemplateArgsIn.release();
5905
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005906 // Set source locations for keywords.
5907 Specialization->setExternLoc(ExternLoc);
5908 Specialization->setTemplateKeywordLoc(TemplateLoc);
5909
5910 // Add the explicit instantiation into its lexical context. However,
5911 // since explicit instantiations are never found by name lookup, we
5912 // just put it into the declaration context directly.
5913 Specialization->setLexicalDeclContext(CurContext);
5914 CurContext->addDecl(Specialization);
5915
5916 // Syntax is now OK, so return if it has no other effect on semantics.
5917 if (HasNoEffect) {
5918 // Set the template specialization kind.
5919 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005920 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005921 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005922
5923 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005924 // A definition of a class template or class member template
5925 // shall be in scope at the point of the explicit instantiation of
5926 // the class template or class member template.
5927 //
5928 // This check comes when we actually try to perform the
5929 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005930 ClassTemplateSpecializationDecl *Def
5931 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005932 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005933 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005934 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005935 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005936 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005937 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5938 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005939
Douglas Gregor0d035142009-10-27 18:42:08 +00005940 // Instantiate the members of this class template specialization.
5941 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005942 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005943 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005944 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5945
5946 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5947 // TSK_ExplicitInstantiationDefinition
5948 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5949 TSK == TSK_ExplicitInstantiationDefinition)
5950 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005951
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005952 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005953 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005954
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005955 // Set the template specialization kind.
5956 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005957 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005958}
5959
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005960// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005961DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005962Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005963 SourceLocation ExternLoc,
5964 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005965 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005966 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005967 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005968 IdentifierInfo *Name,
5969 SourceLocation NameLoc,
5970 AttributeList *Attr) {
5971
Douglas Gregor402abb52009-05-28 23:31:59 +00005972 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005973 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005974 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005975 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5976 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005977 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005978 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005979 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5980
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005981 if (!TagD)
5982 return true;
5983
John McCalld226f652010-08-21 09:40:31 +00005984 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005985 if (Tag->isEnum()) {
5986 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5987 << Context.getTypeDeclType(Tag);
5988 return true;
5989 }
5990
Douglas Gregord0c87372009-05-27 17:30:49 +00005991 if (Tag->isInvalidDecl())
5992 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005993
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005994 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5995 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5996 if (!Pattern) {
5997 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5998 << Context.getTypeDeclType(Record);
5999 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6000 return true;
6001 }
6002
Douglas Gregor558c0322009-10-14 23:41:34 +00006003 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006004 // If the explicit instantiation is for a class or member class, the
6005 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006006 // simple-template-id.
6007 //
6008 // C++98 has the same restriction, just worded differently.
6009 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006010 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006011 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006012
Douglas Gregor558c0322009-10-14 23:41:34 +00006013 // C++0x [temp.explicit]p2:
6014 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006015 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006016 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006017 TemplateSpecializationKind TSK
6018 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6019 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006021 // C++0x [temp.explicit]p2:
6022 // [...] An explicit instantiation shall appear in an enclosing
6023 // namespace of its template. [...]
6024 //
6025 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006026 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006027
Douglas Gregor454885e2009-10-15 15:54:05 +00006028 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006030 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006031 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006032 PrevDecl = Record;
6033 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006034 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006035 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006036 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006037 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006038 PrevDecl,
6039 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006040 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006041 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006042 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006043 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006044 return TagD;
6045 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006047 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006048 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006049 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006050 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006051 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006052 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006053 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006054 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006055 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006056 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6057 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006058 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6059 << Pattern;
6060 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006061 } else {
6062 if (InstantiateClass(NameLoc, Record, Def,
6063 getTemplateInstantiationArgs(Record),
6064 TSK))
6065 return true;
6066
Douglas Gregor952b0172010-02-11 01:04:33 +00006067 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006068 if (!RecordDef)
6069 return true;
6070 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006071 }
6072
Douglas Gregor0d035142009-10-27 18:42:08 +00006073 // Instantiate all of the members of the class.
6074 InstantiateClassMembers(NameLoc, RecordDef,
6075 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006076
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006077 if (TSK == TSK_ExplicitInstantiationDefinition)
6078 MarkVTableUsed(NameLoc, RecordDef, true);
6079
Mike Stump390b4cc2009-05-16 07:39:55 +00006080 // FIXME: We don't have any representation for explicit instantiations of
6081 // member classes. Such a representation is not needed for compilation, but it
6082 // should be available for clients that want to see all of the declarations in
6083 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006084 return TagD;
6085}
6086
John McCallf312b1e2010-08-26 23:41:50 +00006087DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6088 SourceLocation ExternLoc,
6089 SourceLocation TemplateLoc,
6090 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006091 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006092 // TODO: check if/when DNInfo should replace Name.
6093 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6094 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006095 if (!Name) {
6096 if (!D.isInvalidType())
6097 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6098 diag::err_explicit_instantiation_requires_name)
6099 << D.getDeclSpec().getSourceRange()
6100 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006101
Douglas Gregord5a423b2009-09-25 18:43:00 +00006102 return true;
6103 }
6104
6105 // The scope passed in may not be a decl scope. Zip up the scope tree until
6106 // we find one that is.
6107 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6108 (S->getFlags() & Scope::TemplateParamScope) != 0)
6109 S = S->getParent();
6110
6111 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006112 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6113 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006114 if (R.isNull())
6115 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006116
Douglas Gregore885e182011-05-21 18:53:30 +00006117 // C++ [dcl.stc]p1:
6118 // A storage-class-specifier shall not be specified in [...] an explicit
6119 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006120 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006121 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6122 << Name;
6123 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006124 } else if (D.getDeclSpec().getStorageClassSpec()
6125 != DeclSpec::SCS_unspecified) {
6126 // Complain about then remove the storage class specifier.
6127 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6128 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6129
6130 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006131 }
6132
Douglas Gregor663b5a02009-10-14 20:14:33 +00006133 // C++0x [temp.explicit]p1:
6134 // [...] An explicit instantiation of a function template shall not use the
6135 // inline or constexpr specifiers.
6136 // Presumably, this also applies to member functions of class templates as
6137 // well.
6138 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006139 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006140 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006141 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006142
Douglas Gregor663b5a02009-10-14 20:14:33 +00006143 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006144
Douglas Gregor558c0322009-10-14 23:41:34 +00006145 // C++0x [temp.explicit]p2:
6146 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006147 // definition and an explicit instantiation declaration. An explicit
6148 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006149 TemplateSpecializationKind TSK
6150 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6151 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006152
Abramo Bagnara25777432010-08-11 22:01:17 +00006153 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006154 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006155
6156 if (!R->isFunctionType()) {
6157 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006158 // A [...] static data member of a class template can be explicitly
6159 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006160 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006161 if (Previous.isAmbiguous())
6162 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006163
John McCall1bcee0a2009-12-02 08:25:40 +00006164 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006165 if (!Prev || !Prev->isStaticDataMember()) {
6166 // We expect to see a data data member here.
6167 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6168 << Name;
6169 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6170 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006171 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006172 return true;
6173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006174
Douglas Gregord5a423b2009-09-25 18:43:00 +00006175 if (!Prev->getInstantiatedFromStaticDataMember()) {
6176 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006177 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006178 diag::err_explicit_instantiation_data_member_not_instantiated)
6179 << Prev;
6180 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6181 // FIXME: Can we provide a note showing where this was declared?
6182 return true;
6183 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006184
Douglas Gregor558c0322009-10-14 23:41:34 +00006185 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006186 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006187 // or a static data member of a class template specialization, the name of
6188 // the class template specialization in the qualified-id for the member
6189 // name shall be a simple-template-id.
6190 //
6191 // C++98 has the same restriction, just worded differently.
6192 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006193 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006194 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006195 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006196
Douglas Gregor558c0322009-10-14 23:41:34 +00006197 // Check the scope of this explicit instantiation.
6198 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006199
Douglas Gregor454885e2009-10-15 15:54:05 +00006200 // Verify that it is okay to explicitly instantiate here.
6201 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6202 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006203 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006204 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006205 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006206 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006207 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006208 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006209 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006210 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006211
Douglas Gregord5a423b2009-09-25 18:43:00 +00006212 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006213 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006214 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006215 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216
Douglas Gregord5a423b2009-09-25 18:43:00 +00006217 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006218 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006219 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006220
6221 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006222 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006223 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006224 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006225 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6226 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006227 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6228 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006229 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6230 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006231 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006232 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006233 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006234 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006235 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006236
Douglas Gregord5a423b2009-09-25 18:43:00 +00006237 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006238 // A [...] function [...] can be explicitly instantiated from its template.
6239 // A member function [...] of a class template can be explicitly
6240 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006241 // template.
John McCallc373d482010-01-27 01:50:18 +00006242 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006243 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6244 P != PEnd; ++P) {
6245 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006246 if (!HasExplicitTemplateArgs) {
6247 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6248 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6249 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006250
John McCallc373d482010-01-27 01:50:18 +00006251 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006252 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6253 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006254 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006255 }
6256 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006257
Douglas Gregord5a423b2009-09-25 18:43:00 +00006258 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6259 if (!FunTmpl)
6260 continue;
6261
John McCall5769d612010-02-08 23:07:23 +00006262 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006263 FunctionDecl *Specialization = 0;
6264 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006265 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006266 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006267 R, Specialization, Info)) {
6268 // FIXME: Keep track of almost-matches?
6269 (void)TDK;
6270 continue;
6271 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006272
John McCallc373d482010-01-27 01:50:18 +00006273 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006274 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006275
Douglas Gregord5a423b2009-09-25 18:43:00 +00006276 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006277 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006278 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006280 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6281 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6282 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006283
John McCallc373d482010-01-27 01:50:18 +00006284 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006285 return true;
John McCallc373d482010-01-27 01:50:18 +00006286
6287 // Ignore access control bits, we don't need them for redeclaration checking.
6288 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006289
Douglas Gregor0a897e32009-10-15 17:21:20 +00006290 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006291 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006292 diag::err_explicit_instantiation_member_function_not_instantiated)
6293 << Specialization
6294 << (Specialization->getTemplateSpecializationKind() ==
6295 TSK_ExplicitSpecialization);
6296 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6297 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006298 }
6299
Douglas Gregor0a897e32009-10-15 17:21:20 +00006300 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006301 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6302 PrevDecl = Specialization;
6303
Douglas Gregor0a897e32009-10-15 17:21:20 +00006304 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006305 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006306 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006307 PrevDecl,
6308 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006309 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006310 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006311 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006312
Douglas Gregor0a897e32009-10-15 17:21:20 +00006313 // FIXME: We may still want to build some representation of this
6314 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006315 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006316 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006317 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006318
6319 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006320
Douglas Gregor0a897e32009-10-15 17:21:20 +00006321 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006322 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006323
Douglas Gregor558c0322009-10-14 23:41:34 +00006324 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006325 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006326 // or a static data member of a class template specialization, the name of
6327 // the class template specialization in the qualified-id for the member
6328 // name shall be a simple-template-id.
6329 //
6330 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006331 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006332 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006333 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006334 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006335 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006336 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006337 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006338
Douglas Gregor558c0322009-10-14 23:41:34 +00006339 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006340 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006341 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006342 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006343 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006344
Douglas Gregord5a423b2009-09-25 18:43:00 +00006345 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006346 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006347}
6348
John McCallf312b1e2010-08-26 23:41:50 +00006349TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006350Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6351 const CXXScopeSpec &SS, IdentifierInfo *Name,
6352 SourceLocation TagLoc, SourceLocation NameLoc) {
6353 // This has to hold, because SS is expected to be defined.
6354 assert(Name && "Expected a name in a dependent tag");
6355
6356 NestedNameSpecifier *NNS
6357 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6358 if (!NNS)
6359 return true;
6360
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006361 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006362
Douglas Gregor48c89f42010-04-24 16:38:41 +00006363 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6364 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006365 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006366 return true;
6367 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006368
Douglas Gregor059101f2011-03-02 00:47:37 +00006369 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006370 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006371 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6372
6373 // Create type-source location information for this type.
6374 TypeLocBuilder TLB;
6375 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6376 TL.setKeywordLoc(TagLoc);
6377 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6378 TL.setNameLoc(NameLoc);
6379 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006380}
6381
John McCallf312b1e2010-08-26 23:41:50 +00006382TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006383Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6384 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006385 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006386 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006387 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006388
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006389 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6390 !getLangOptions().CPlusPlus0x)
6391 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006392 << FixItHint::CreateRemoval(TypenameLoc);
6393
Douglas Gregor2494dd02011-03-01 01:34:45 +00006394 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006395 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6396 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006397 if (T.isNull())
6398 return true;
John McCall63b43852010-04-29 23:50:39 +00006399
6400 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6401 if (isa<DependentNameType>(T)) {
6402 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006403 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006404 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006405 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006406 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006407 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006408 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006409 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006410 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006411 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006412
John McCallb3d87482010-08-24 05:47:05 +00006413 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006414}
6415
John McCallf312b1e2010-08-26 23:41:50 +00006416TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006417Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6418 const CXXScopeSpec &SS,
6419 SourceLocation TemplateLoc,
6420 TemplateTy TemplateIn,
6421 SourceLocation TemplateNameLoc,
6422 SourceLocation LAngleLoc,
6423 ASTTemplateArgsPtr TemplateArgsIn,
6424 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006425 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6426 !getLangOptions().CPlusPlus0x)
6427 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006428 << FixItHint::CreateRemoval(TypenameLoc);
6429
6430 // Translate the parser's template argument list in our AST format.
6431 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6432 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6433
6434 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006435 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6436 // Construct a dependent template specialization type.
6437 assert(DTN && "dependent template has non-dependent name?");
6438 assert(DTN->getQualifier()
6439 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6440 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6441 DTN->getQualifier(),
6442 DTN->getIdentifier(),
6443 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006444
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006445 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006446 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006447 DependentTemplateSpecializationTypeLoc SpecTL
6448 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006449 SpecTL.setLAngleLoc(LAngleLoc);
6450 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006451 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006452 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006453 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006454 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6455 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006456 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006457 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006458
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006459 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6460 if (T.isNull())
6461 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006462
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006463 // Provide source-location information for the template specialization
6464 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006465 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006466 TemplateSpecializationTypeLoc SpecTL
6467 = Builder.push<TemplateSpecializationTypeLoc>(T);
6468
6469 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006470 SpecTL.setLAngleLoc(LAngleLoc);
6471 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006472 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006473 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6474 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6475
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006476 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6477 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6478 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006479 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6480
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006481 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6482 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006483}
6484
Douglas Gregora02411e2011-02-27 22:46:49 +00006485
Douglas Gregord57959a2009-03-27 23:10:48 +00006486/// \brief Build the type that describes a C++ typename specifier,
6487/// e.g., "typename T::type".
6488QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006489Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6490 SourceLocation KeywordLoc,
6491 NestedNameSpecifierLoc QualifierLoc,
6492 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006493 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006494 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006495 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006496
John McCall77bb1aa2010-05-01 00:40:08 +00006497 DeclContext *Ctx = computeDeclContext(SS);
6498 if (!Ctx) {
6499 // If the nested-name-specifier is dependent and couldn't be
6500 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006501 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6502 return Context.getDependentNameType(Keyword,
6503 QualifierLoc.getNestedNameSpecifier(),
6504 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006505 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006506
John McCall77bb1aa2010-05-01 00:40:08 +00006507 // If the nested-name-specifier refers to the current instantiation,
6508 // the "typename" keyword itself is superfluous. In C++03, the
6509 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6510 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006511 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006512
John McCall77bb1aa2010-05-01 00:40:08 +00006513 if (RequireCompleteDeclContext(SS, Ctx))
6514 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006515
6516 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006517 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006518 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006519 unsigned DiagID = 0;
6520 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006521 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006522 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006523 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006524 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006525
6526 case LookupResult::FoundUnresolvedValue: {
6527 // We found a using declaration that is a value. Most likely, the using
6528 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006529 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006530 IILoc);
6531 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6532 << Name << Ctx << FullRange;
6533 if (UnresolvedUsingValueDecl *Using
6534 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006535 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006536 Diag(Loc, diag::note_using_value_decl_missing_typename)
6537 << FixItHint::CreateInsertion(Loc, "typename ");
6538 }
6539 }
6540 // Fall through to create a dependent typename type, from which we can recover
6541 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006542
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006543 case LookupResult::NotFoundInCurrentInstantiation:
6544 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006545 return Context.getDependentNameType(Keyword,
6546 QualifierLoc.getNestedNameSpecifier(),
6547 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006548
6549 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006550 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006551 // We found a type. Build an ElaboratedType, since the
6552 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006553 return Context.getElaboratedType(ETK_Typename,
6554 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006555 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006556 }
6557
6558 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006559 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006560 break;
6561
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006562
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006563 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006564 return QualType();
6565
Douglas Gregord57959a2009-03-27 23:10:48 +00006566 case LookupResult::FoundOverloaded:
6567 DiagID = diag::err_typename_nested_not_type;
6568 Referenced = *Result.begin();
6569 break;
6570
John McCall6e247262009-10-10 05:48:19 +00006571 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006572 return QualType();
6573 }
6574
6575 // If we get here, it's because name lookup did not find a
6576 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006577 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006578 IILoc);
6579 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006580 if (Referenced)
6581 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6582 << Name;
6583 return QualType();
6584}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006585
6586namespace {
6587 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006588 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006589 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006590 SourceLocation Loc;
6591 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006592
Douglas Gregor4a959d82009-08-06 16:20:37 +00006593 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006594 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006595
Mike Stump1eb44332009-09-09 15:08:12 +00006596 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006597 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006598 DeclarationName Entity)
6599 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006600 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006601
6602 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006603 /// transformed.
6604 ///
6605 /// For the purposes of type reconstruction, a type has already been
6606 /// transformed if it is NULL or if it is not dependent.
6607 bool AlreadyTransformed(QualType T) {
6608 return T.isNull() || !T->isDependentType();
6609 }
Mike Stump1eb44332009-09-09 15:08:12 +00006610
6611 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006612 /// rebuilt.
6613 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006614
Douglas Gregor4a959d82009-08-06 16:20:37 +00006615 /// \brief Returns the name of the entity whose type is being rebuilt.
6616 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006617
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006618 /// \brief Sets the "base" location and entity when that
6619 /// information is known based on another transformation.
6620 void setBase(SourceLocation Loc, DeclarationName Entity) {
6621 this->Loc = Loc;
6622 this->Entity = Entity;
6623 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006624 };
6625}
6626
Douglas Gregor4a959d82009-08-06 16:20:37 +00006627/// \brief Rebuilds a type within the context of the current instantiation.
6628///
Mike Stump1eb44332009-09-09 15:08:12 +00006629/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006630/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006631/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006632/// partial specialization thereof). This routine will rebuild that type now
6633/// that we have entered the declarator's scope, which may produce different
6634/// canonical types, e.g.,
6635///
6636/// \code
6637/// template<typename T>
6638/// struct X {
6639/// typedef T* pointer;
6640/// pointer data();
6641/// };
6642///
6643/// template<typename T>
6644/// typename X<T>::pointer X<T>::data() { ... }
6645/// \endcode
6646///
Douglas Gregor4714c122010-03-31 17:34:00 +00006647/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006648/// since we do not know that we can look into X<T> when we parsed the type.
6649/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006650/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006651/// as the canonical type of T*, allowing the return types of the out-of-line
6652/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006653TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6654 SourceLocation Loc,
6655 DeclarationName Name) {
6656 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006657 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006658
Douglas Gregor4a959d82009-08-06 16:20:37 +00006659 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6660 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006661}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006662
John McCall60d7b3a2010-08-24 06:29:42 +00006663ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006664 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6665 DeclarationName());
6666 return Rebuilder.TransformExpr(E);
6667}
6668
John McCall63b43852010-04-29 23:50:39 +00006669bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006670 if (SS.isInvalid())
6671 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006672
Douglas Gregor7e384942011-02-25 16:07:42 +00006673 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006674 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6675 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006676 NestedNameSpecifierLoc Rebuilt
6677 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6678 if (!Rebuilt)
6679 return true;
John McCall63b43852010-04-29 23:50:39 +00006680
Douglas Gregor7e384942011-02-25 16:07:42 +00006681 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006682 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006683}
6684
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006685/// \brief Produces a formatted string that describes the binding of
6686/// template parameters to template arguments.
6687std::string
6688Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6689 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006690 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006691}
6692
6693std::string
6694Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6695 const TemplateArgument *Args,
6696 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006697 llvm::SmallString<128> Str;
6698 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006699
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006700 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006701 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006702
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006703 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006704 if (I >= NumArgs)
6705 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006706
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006707 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006708 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006709 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006710 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006711
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006712 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006713 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006714 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006715 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006717
Douglas Gregor87dd6972010-12-20 16:52:59 +00006718 Out << " = ";
6719 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006720 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006721
6722 Out << ']';
6723 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006724}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006725
6726void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6727 if (!FD)
6728 return;
6729 FD->setLateTemplateParsed(Flag);
6730}
6731
6732bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6733 DeclContext *DC = CurContext;
6734
6735 while (DC) {
6736 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6737 const FunctionDecl *FD = RD->isLocalClass();
6738 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6739 } else if (DC->isTranslationUnit() || DC->isNamespace())
6740 return false;
6741
6742 DC = DC->getParent();
6743 }
6744 return false;
6745}