blob: cd1092a9bccf253ab21c2af9c6899e6f63c467bf [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.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000427 if (getLangOptions().MicrosoftExt)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000428 return false;
429
430 // C++ [temp.local]p4:
431 // A template-parameter shall not be redeclared within its
432 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434 << cast<NamedDecl>(PrevDecl)->getDeclName();
435 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
436 return true;
437}
438
Douglas Gregor2943aed2009-03-03 04:44:36 +0000439/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000440/// the parameter D to reference the templated declaration and return a pointer
441/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000442TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
443 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
444 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000445 return Temp;
446 }
447 return 0;
448}
449
Douglas Gregorba68eca2011-01-05 17:40:24 +0000450ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
451 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000453 "Only template template arguments can be pack expansions here");
454 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
455 "Template template argument pack expansion without packs");
456 ParsedTemplateArgument Result(*this);
457 Result.EllipsisLoc = EllipsisLoc;
458 return Result;
459}
460
Douglas Gregor788cd062009-11-11 01:00:40 +0000461static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
462 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 switch (Arg.getKind()) {
465 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000470 return TemplateArgumentLoc(TemplateArgument(T), DI);
471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000472
Douglas Gregor788cd062009-11-11 01:00:40 +0000473 case ParsedTemplateArgument::NonType: {
474 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
475 return TemplateArgumentLoc(TemplateArgument(E), E);
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000479 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000480 TemplateArgument TArg;
481 if (Arg.getEllipsisLoc().isValid())
482 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
483 else
484 TArg = Template;
485 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000486 Arg.getScopeSpec().getWithLocInContext(
487 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000488 Arg.getLocation(),
489 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 }
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000493 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 return TemplateArgumentLoc();
495}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496
Douglas Gregor788cd062009-11-11 01:00:40 +0000497/// \brief Translates template arguments as provided by the parser
498/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000499void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
500 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000502 TemplateArgs.addArgument(translateTemplateArgument(*this,
503 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000504}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// ActOnTypeParameter - Called when a C++ template type parameter
507/// (e.g., "typename T") has been parsed. Typename specifies whether
508/// the keyword "typename" was used to declare the type parameter
509/// (otherwise, "class" was used), and KeyLoc is the location of the
510/// "class" or "typename" keyword. ParamName is the name of the
511/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000512/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513/// If the type parameter has a default argument, it will be added
514/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000515Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
516 SourceLocation EllipsisLoc,
517 SourceLocation KeyLoc,
518 IdentifierInfo *ParamName,
519 SourceLocation ParamNameLoc,
520 unsigned Depth, unsigned Position,
521 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000522 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 assert(S->isTemplateParamScope() &&
524 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 bool Invalid = false;
526
527 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000528 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000529 LookupOrdinaryName,
530 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000533 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000534 }
535
Douglas Gregorddc29e12009-02-06 22:42:48 +0000536 SourceLocation Loc = ParamNameLoc;
537 if (!ParamName)
538 Loc = KeyLoc;
539
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000541 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000542 KeyLoc, Loc, Depth, Position, ParamName,
543 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000544 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000545 if (Invalid)
546 Param->setInvalidDecl();
547
548 if (ParamName) {
549 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000550 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000551 IdResolver.AddDecl(Param);
552 }
553
Douglas Gregor61c4d282011-01-05 15:48:55 +0000554 // C++0x [temp.param]p9:
555 // A default template-argument may be specified for any kind of
556 // template-parameter that is not a template parameter pack.
557 if (DefaultArg && Ellipsis) {
558 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
559 DefaultArg = ParsedType();
560 }
561
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 // Handle the default argument, if provided.
563 if (DefaultArg) {
564 TypeSourceInfo *DefaultTInfo;
565 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
Douglas Gregor6f526752010-12-16 08:48:57 +0000569 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000570 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000571 UPPC_DefaultArgument))
572 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000573
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000574 // Check the template argument itself.
575 if (CheckTemplateArgument(Param, DefaultTInfo)) {
576 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000577 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000579
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000580 Param->setDefaultArgument(DefaultTInfo, false);
581 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000582
John McCalld226f652010-08-21 09:40:31 +0000583 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000584}
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586/// \brief Check that the type of a non-type template parameter is
587/// well-formed.
588///
589/// \returns the (possibly-promoted) parameter type if valid;
590/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000591QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000592Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000593 // We don't allow variably-modified types as the type of non-type template
594 // parameters.
595 if (T->isVariablyModifiedType()) {
596 Diag(Loc, diag::err_variably_modified_nontype_template_param)
597 << T;
598 return QualType();
599 }
600
Douglas Gregor2943aed2009-03-03 04:44:36 +0000601 // C++ [temp.param]p4:
602 //
603 // A non-type template-parameter shall have one of the following
604 // (optionally cv-qualified) types:
605 //
606 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000607 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000609 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000610 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000611 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000612 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000614 // -- std::nullptr_t.
615 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 // If T is a dependent type, we can't do the check now, so we
617 // assume that it is well-formed.
618 T->isDependentType())
619 return T;
620 // C++ [temp.param]p8:
621 //
622 // A non-type template-parameter of type "array of T" or
623 // "function returning T" is adjusted to be of type "pointer to
624 // T" or "pointer to function returning T", respectively.
625 else if (T->isArrayType())
626 // FIXME: Keep the type prior to promotion?
627 return Context.getArrayDecayedType(T);
628 else if (T->isFunctionType())
629 // FIXME: Keep the type prior to promotion?
630 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000631
Douglas Gregor2943aed2009-03-03 04:44:36 +0000632 Diag(Loc, diag::err_template_nontype_parm_bad_type)
633 << T;
634
635 return QualType();
636}
637
John McCalld226f652010-08-21 09:40:31 +0000638Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
639 unsigned Depth,
640 unsigned Position,
641 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000642 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000643 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
644 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000645
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000646 assert(S->isTemplateParamScope() &&
647 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000648 bool Invalid = false;
649
650 IdentifierInfo *ParamName = D.getIdentifier();
651 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000653 LookupOrdinaryName,
654 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000655 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000656 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000657 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 }
659
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000660 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
661 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000662 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000663 Invalid = true;
664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000665
Douglas Gregor10738d32010-12-23 23:51:58 +0000666 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000667 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000668 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000669 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000670 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000671 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000672 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000673 Param->setAccess(AS_public);
674
Douglas Gregor72c3f312008-12-05 18:15:24 +0000675 if (Invalid)
676 Param->setInvalidDecl();
677
678 if (D.getIdentifier()) {
679 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000680 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000681 IdResolver.AddDecl(Param);
682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
Douglas Gregor61c4d282011-01-05 15:48:55 +0000684 // C++0x [temp.param]p9:
685 // A default template-argument may be specified for any kind of
686 // template-parameter that is not a template parameter pack.
687 if (Default && IsParameterPack) {
688 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
689 Default = 0;
690 }
691
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000692 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000693 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000694 // Check for unexpanded parameter packs.
695 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
696 return Param;
697
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000698 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000699 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
700 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000701 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000702 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 }
John Wiegley429bb272011-04-08 18:41:53 +0000704 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000705
John McCall9ae2f072010-08-23 23:25:46 +0000706 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000708
John McCalld226f652010-08-21 09:40:31 +0000709 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000710}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000711
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712/// ActOnTemplateTemplateParameter - Called when a C++ template template
713/// parameter (e.g. T in template <template <typename> class T> class array)
714/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000715Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
716 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000717 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000718 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000719 IdentifierInfo *Name,
720 SourceLocation NameLoc,
721 unsigned Depth,
722 unsigned Position,
723 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000724 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000725 assert(S->isTemplateParamScope() &&
726 "Template template parameter not in template parameter scope!");
727
728 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000729 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000731 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000732 NameLoc.isInvalid()? TmpLoc : NameLoc,
733 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000734 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000735 Param->setAccess(AS_public);
736
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000737 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000738 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000739 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000740 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 IdResolver.AddDecl(Param);
742 }
743
Douglas Gregor6f526752010-12-16 08:48:57 +0000744 if (Params->size() == 0) {
745 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
746 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
747 Param->setInvalidDecl();
748 }
749
Douglas Gregor61c4d282011-01-05 15:48:55 +0000750 // C++0x [temp.param]p9:
751 // A default template-argument may be specified for any kind of
752 // template-parameter that is not a template parameter pack.
753 if (IsParameterPack && !Default.isInvalid()) {
754 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
755 Default = ParsedTemplateArgument();
756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 if (!Default.isInvalid()) {
759 // Check only that we have a template template argument. We don't want to
760 // try to check well-formedness now, because our template template parameter
761 // might have dependent types in its template parameters, which we wouldn't
762 // be able to match now.
763 //
764 // If none of the template template parameter's template arguments mention
765 // other template parameters, we could actually perform more checking here.
766 // However, it isn't worth doing.
767 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
768 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
769 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
770 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000771 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregor6f526752010-12-16 08:48:57 +0000774 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 DefaultArg.getArgument().getAsTemplate(),
777 UPPC_DefaultArgument))
778 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000779
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000780 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000782
John McCalld226f652010-08-21 09:40:31 +0000783 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000784}
785
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000786/// ActOnTemplateParameterList - Builds a TemplateParameterList that
787/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000788TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000789Sema::ActOnTemplateParameterList(unsigned Depth,
790 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000791 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000793 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation RAngleLoc) {
795 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000796 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000797
Douglas Gregorddc29e12009-02-06 22:42:48 +0000798 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000799 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000800 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000801}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000802
John McCallb6217662010-03-15 10:12:16 +0000803static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
804 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000805 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000806}
807
John McCallf312b1e2010-08-26 23:41:50 +0000808DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000809Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000810 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 IdentifierInfo *Name, SourceLocation NameLoc,
812 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000813 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000814 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000815 unsigned NumOuterTemplateParamLists,
816 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000817 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000819 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000820 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000821
822 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000823 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000824 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000825
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000826 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
827 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000828
829 // There is no such thing as an unnamed class template.
830 if (!Name) {
831 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000832 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000833 }
834
835 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000837 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000838 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000839 if (SS.isNotEmpty() && !SS.isInvalid()) {
840 SemanticContext = computeDeclContext(SS, true);
841 if (!SemanticContext) {
842 // FIXME: Produce a reasonable diagnostic here
843 return true;
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
John McCall77bb1aa2010-05-01 00:40:08 +0000846 if (RequireCompleteDeclContext(SS, SemanticContext))
847 return true;
848
John McCalla24dc2e2009-11-17 02:14:36 +0000849 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000850 } else {
851 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000852 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor57265e32010-04-12 16:00:01 +0000855 if (Previous.isAmbiguous())
856 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000857
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858 NamedDecl *PrevDecl = 0;
859 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000860 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861
Douglas Gregorddc29e12009-02-06 22:42:48 +0000862 // If there is a previous declaration with the same name, check
863 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000864 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000865 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000866
867 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000869 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000870 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000871 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
872 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000873 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000874 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
875 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
876 PrevClassTemplate
877 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
878 ->getSpecializedTemplate();
879 }
880 }
881
John McCall65c49462009-12-18 11:25:59 +0000882 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000883 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 // [...] When looking for a prior declaration of a class or a function
885 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000886 // function is neither a qualified name nor a template-id, scopes outside
887 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000888 if (!SS.isSet()) {
889 DeclContext *OutermostContext = CurContext;
890 while (!OutermostContext->isFileContext())
891 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000892
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000893 if (PrevDecl &&
894 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
895 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
896 SemanticContext = PrevDecl->getDeclContext();
897 } else {
898 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000899 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000900 // declaration.
901 PrevDecl = PrevClassTemplate = 0;
902 SemanticContext = OutermostContext;
903 }
John McCalle129d442009-12-17 23:21:11 +0000904 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000905
John McCalle129d442009-12-17 23:21:11 +0000906 if (CurContext->isDependentContext()) {
907 // If this is a dependent context, we don't want to link the friend
908 // class template to the template in scope, because that would perform
909 // checking of the template parameter lists that can't be performed
910 // until the outer context is instantiated.
911 PrevDecl = PrevClassTemplate = 0;
912 }
913 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
914 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 if (PrevClassTemplate) {
917 // Ensure that the template parameter lists are compatible.
918 if (!TemplateParameterListsAreEqual(TemplateParams,
919 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000920 /*Complain=*/true,
921 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000922 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923
924 // C++ [temp.class]p4:
925 // In a redeclaration, partial specialization, explicit
926 // specialization or explicit instantiation of a class template,
927 // the class-key shall agree in kind with the original class
928 // template declaration (7.1.5.3).
929 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000930 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
931 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000933 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000934 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000935 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000936 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938
Douglas Gregorddc29e12009-02-06 22:42:48 +0000939 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000940 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000941 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 Diag(NameLoc, diag::err_redefinition) << Name;
943 Diag(Def->getLocation(), diag::note_previous_definition);
944 // FIXME: Would it make sense to try to "forget" the previous
945 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000946 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000947 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000948 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000949 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
950 // Maybe we will complain about the shadowed template parameter.
951 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
952 // Just pretend that we didn't see the previous declaration.
953 PrevDecl = 0;
954 } else if (PrevDecl) {
955 // C++ [temp]p5:
956 // A class template shall not have the same name as any other
957 // template, class, function, object, enumeration, enumerator,
958 // namespace, or type in the same scope (3.3), except as specified
959 // in (14.5.4).
960 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
961 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000962 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000963 }
964
Douglas Gregord684b002009-02-10 19:49:53 +0000965 // Check the template parameter list of this declaration, possibly
966 // merging in the template parameter list from the previous class
967 // template declaration.
968 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000969 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000970 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000971 SemanticContext->isRecord() &&
972 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000973 ? TPC_ClassTemplateMember
974 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000975 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor57265e32010-04-12 16:00:01 +0000977 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000978 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000979 // template out-of-line.
980 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
981 !(TUK == TUK_Friend && CurContext->isDependentContext()))
982 Diag(NameLoc, diag::err_member_def_does_not_match)
983 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 }
985
Mike Stump1eb44332009-09-09 15:08:12 +0000986 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000987 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000988 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000989 PrevClassTemplate->getTemplatedDecl() : 0,
990 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000991 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000992 if (NumOuterTemplateParamLists > 0)
993 NewClass->setTemplateParameterListsInfo(Context,
994 NumOuterTemplateParamLists,
995 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000996
997 ClassTemplateDecl *NewTemplate
998 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
999 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001000 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001001 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001002
1003 if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
1004 NewTemplate->setModulePrivate();
Douglas Gregore7612302011-09-09 19:05:14 +00001005 } else if (ModulePrivateLoc.isValid()) {
1006 if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate())
1007 diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate,
1008 ModulePrivateLoc);
1009 else
1010 NewTemplate->setModulePrivate();
1011 }
Douglas Gregor8d267c52011-09-09 02:06:17 +00001012
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001013 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001014 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001015 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001016 assert(T->isDependentType() && "Class template type is not dependent?");
1017 (void)T;
1018
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001020 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001021 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001022 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1023 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001024
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001025 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001026 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001027 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Douglas Gregorddc29e12009-02-06 22:42:48 +00001029 // Set the lexical context of these templates
1030 NewClass->setLexicalDeclContext(CurContext);
1031 NewTemplate->setLexicalDeclContext(CurContext);
1032
John McCall0f434ec2009-07-31 02:45:11 +00001033 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001034 NewClass->startDefinition();
1035
1036 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001037 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001038
John McCall05b23ea2009-09-14 21:59:20 +00001039 if (TUK != TUK_Friend)
1040 PushOnScopeChains(NewTemplate, S);
1041 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001042 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001043 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001044 NewClass->setAccess(PrevClassTemplate->getAccess());
1045 }
John McCall05b23ea2009-09-14 21:59:20 +00001046
Douglas Gregord85bea22009-09-26 06:47:28 +00001047 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1048 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049
John McCall05b23ea2009-09-14 21:59:20 +00001050 // Friend templates are visible in fairly strange ways.
1051 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001052 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001053 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1054 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1055 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001056 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001057 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058
Douglas Gregord85bea22009-09-26 06:47:28 +00001059 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1060 NewClass->getLocation(),
1061 NewTemplate,
1062 /*FIXME:*/NewClass->getLocation());
1063 Friend->setAccess(AS_public);
1064 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001065 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001066
Douglas Gregord684b002009-02-10 19:49:53 +00001067 if (Invalid) {
1068 NewTemplate->setInvalidDecl();
1069 NewClass->setInvalidDecl();
1070 }
John McCalld226f652010-08-21 09:40:31 +00001071 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001072}
1073
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001074/// \brief Diagnose the presence of a default template argument on a
1075/// template parameter, which is ill-formed in certain contexts.
1076///
1077/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001079 Sema::TemplateParamListContext TPC,
1080 SourceLocation ParamLoc,
1081 SourceRange DefArgRange) {
1082 switch (TPC) {
1083 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001084 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085 return false;
1086
1087 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001088 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001089 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001090 // A default template-argument shall not be specified in a
1091 // function template declaration or a function template
1092 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001093 // If a friend function template declaration specifies a default
1094 // template-argument, that declaration shall be a definition and shall be
1095 // the only declaration of the function template in the translation unit.
1096 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001097 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001099 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001100 << DefArgRange;
1101 return false;
1102
1103 case Sema::TPC_ClassTemplateMember:
1104 // C++0x [temp.param]p9:
1105 // A default template-argument shall not be specified in the
1106 // template-parameter-lists of the definition of a member of a
1107 // class template that appears outside of the member's class.
1108 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1109 << DefArgRange;
1110 return true;
1111
1112 case Sema::TPC_FriendFunctionTemplate:
1113 // C++ [temp.param]p9:
1114 // A default template-argument shall not be specified in a
1115 // friend template declaration.
1116 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1117 << DefArgRange;
1118 return true;
1119
1120 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1121 // for friend function templates if there is only a single
1122 // declaration (and it is a definition). Strange!
1123 }
1124
1125 return false;
1126}
1127
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001128/// \brief Check for unexpanded parameter packs within the template parameters
1129/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001130static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1131 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001132 TemplateParameterList *Params = TTP->getTemplateParameters();
1133 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1134 NamedDecl *P = Params->getParam(I);
1135 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001136 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001137 NTTP->getTypeSourceInfo(),
1138 Sema::UPPC_NonTypeTemplateParameterType))
1139 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001140
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001141 continue;
1142 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001143
1144 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001145 = dyn_cast<TemplateTemplateParmDecl>(P))
1146 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1147 return true;
1148 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001149
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001150 return false;
1151}
1152
Douglas Gregord684b002009-02-10 19:49:53 +00001153/// \brief Checks the validity of a template parameter list, possibly
1154/// considering the template parameter list from a previous
1155/// declaration.
1156///
1157/// If an "old" template parameter list is provided, it must be
1158/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1159/// template parameter list.
1160///
1161/// \param NewParams Template parameter list for a new template
1162/// declaration. This template parameter list will be updated with any
1163/// default arguments that are carried through from the previous
1164/// template parameter list.
1165///
1166/// \param OldParams If provided, template parameter list from a
1167/// previous declaration of the same template. Default template
1168/// arguments will be merged from the old template parameter list to
1169/// the new template parameter list.
1170///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001171/// \param TPC Describes the context in which we are checking the given
1172/// template parameter list.
1173///
Douglas Gregord684b002009-02-10 19:49:53 +00001174/// \returns true if an error occurred, false otherwise.
1175bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001176 TemplateParameterList *OldParams,
1177 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001178 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Douglas Gregord684b002009-02-10 19:49:53 +00001180 // C++ [temp.param]p10:
1181 // The set of default template-arguments available for use with a
1182 // template declaration or definition is obtained by merging the
1183 // default arguments from the definition (if in scope) and all
1184 // declarations in scope in the same way default function
1185 // arguments are (8.3.6).
1186 bool SawDefaultArgument = false;
1187 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001188
Anders Carlsson49d25572009-06-12 23:20:15 +00001189 bool SawParameterPack = false;
1190 SourceLocation ParameterPackLoc;
1191
Mike Stump1a35fde2009-02-11 23:03:27 +00001192 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001193 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001194 if (OldParams)
1195 OldParam = OldParams->begin();
1196
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001197 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001198 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1199 NewParamEnd = NewParams->end();
1200 NewParam != NewParamEnd; ++NewParam) {
1201 // Variables used to diagnose redundant default arguments
1202 bool RedundantDefaultArg = false;
1203 SourceLocation OldDefaultLoc;
1204 SourceLocation NewDefaultLoc;
1205
1206 // Variables used to diagnose missing default arguments
1207 bool MissingDefaultArg = false;
1208
Anders Carlsson49d25572009-06-12 23:20:15 +00001209 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001210 // If a template parameter of a primary class template or alias template
1211 // is a template parameter pack, it shall be the last template parameter.
1212 if (SawParameterPack &&
1213 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001214 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001215 diag::err_template_param_pack_must_be_last_template_parameter);
1216 Invalid = true;
1217 }
1218
Douglas Gregord684b002009-02-10 19:49:53 +00001219 if (TemplateTypeParmDecl *NewTypeParm
1220 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001221 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001222 if (NewTypeParm->hasDefaultArgument() &&
1223 DiagnoseDefaultTemplateArgument(*this, TPC,
1224 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001225 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001226 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001227 NewTypeParm->removeDefaultArgument();
1228
1229 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001230 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001231 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Anders Carlsson49d25572009-06-12 23:20:15 +00001233 if (NewTypeParm->isParameterPack()) {
1234 assert(!NewTypeParm->hasDefaultArgument() &&
1235 "Parameter packs can't have a default argument!");
1236 SawParameterPack = true;
1237 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001238 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001239 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001240 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1241 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1242 SawDefaultArgument = true;
1243 RedundantDefaultArg = true;
1244 PreviousDefaultArgLoc = NewDefaultLoc;
1245 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1246 // Merge the default argument from the old declaration to the
1247 // new declaration.
1248 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001249 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001250 true);
1251 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1252 } else if (NewTypeParm->hasDefaultArgument()) {
1253 SawDefaultArgument = true;
1254 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1255 } else if (SawDefaultArgument)
1256 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001258 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001259 // Check for unexpanded parameter packs.
1260 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001261 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001262 UPPC_NonTypeTemplateParameterType)) {
1263 Invalid = true;
1264 continue;
1265 }
1266
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001267 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268 if (NewNonTypeParm->hasDefaultArgument() &&
1269 DiagnoseDefaultTemplateArgument(*this, TPC,
1270 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001271 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001272 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001273 }
1274
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001275 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001276 NonTypeTemplateParmDecl *OldNonTypeParm
1277 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001278 if (NewNonTypeParm->isParameterPack()) {
1279 assert(!NewNonTypeParm->hasDefaultArgument() &&
1280 "Parameter packs can't have a default argument!");
1281 SawParameterPack = true;
1282 ParameterPackLoc = NewNonTypeParm->getLocation();
1283 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001284 NewNonTypeParm->hasDefaultArgument()) {
1285 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1286 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1287 SawDefaultArgument = true;
1288 RedundantDefaultArg = true;
1289 PreviousDefaultArgLoc = NewDefaultLoc;
1290 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1291 // Merge the default argument from the old declaration to the
1292 // new declaration.
1293 SawDefaultArgument = true;
1294 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001295 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001296 // parameter.
1297 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001298 OldNonTypeParm->getDefaultArgument(),
1299 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001300 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1301 } else if (NewNonTypeParm->hasDefaultArgument()) {
1302 SawDefaultArgument = true;
1303 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1304 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001305 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001306 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001307 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001308 TemplateTemplateParmDecl *NewTemplateParm
1309 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001310
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001311 // Check for unexpanded parameter packs, recursively.
1312 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1313 Invalid = true;
1314 continue;
1315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001316
1317 if (NewTemplateParm->hasDefaultArgument() &&
1318 DiagnoseDefaultTemplateArgument(*this, TPC,
1319 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001320 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001321 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001322
1323 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001324 TemplateTemplateParmDecl *OldTemplateParm
1325 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001326 if (NewTemplateParm->isParameterPack()) {
1327 assert(!NewTemplateParm->hasDefaultArgument() &&
1328 "Parameter packs can't have a default argument!");
1329 SawParameterPack = true;
1330 ParameterPackLoc = NewTemplateParm->getLocation();
1331 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001332 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001333 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1334 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001335 SawDefaultArgument = true;
1336 RedundantDefaultArg = true;
1337 PreviousDefaultArgLoc = NewDefaultLoc;
1338 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1339 // Merge the default argument from the old declaration to the
1340 // new declaration.
1341 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001342 // FIXME: We need to create a new kind of "default argument" expression
1343 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001344 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001345 OldTemplateParm->getDefaultArgument(),
1346 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001347 PreviousDefaultArgLoc
1348 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001349 } else if (NewTemplateParm->hasDefaultArgument()) {
1350 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001351 PreviousDefaultArgLoc
1352 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001353 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001354 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001355 }
1356
1357 if (RedundantDefaultArg) {
1358 // C++ [temp.param]p12:
1359 // A template-parameter shall not be given default arguments
1360 // by two different declarations in the same scope.
1361 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1362 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1363 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001364 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001365 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001366 // If a template-parameter of a class template has a default
1367 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001368 // have a default template-argument supplied or be a template parameter
1369 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001370 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001371 diag::err_template_param_default_arg_missing);
1372 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1373 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001374 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001375 }
1376
1377 // If we have an old template parameter list that we're merging
1378 // in, move on to the next parameter.
1379 if (OldParams)
1380 ++OldParam;
1381 }
1382
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001383 // We were missing some default arguments at the end of the list, so remove
1384 // all of the default arguments.
1385 if (RemoveDefaultArguments) {
1386 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1387 NewParamEnd = NewParams->end();
1388 NewParam != NewParamEnd; ++NewParam) {
1389 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1390 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001391 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001392 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1393 NTTP->removeDefaultArgument();
1394 else
1395 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1396 }
1397 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001398
Douglas Gregord684b002009-02-10 19:49:53 +00001399 return Invalid;
1400}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001401
John McCall4e2cbb22010-10-20 05:44:58 +00001402namespace {
1403
1404/// A class which looks for a use of a certain level of template
1405/// parameter.
1406struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1407 typedef RecursiveASTVisitor<DependencyChecker> super;
1408
1409 unsigned Depth;
1410 bool Match;
1411
1412 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1413 NamedDecl *ND = Params->getParam(0);
1414 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1415 Depth = PD->getDepth();
1416 } else if (NonTypeTemplateParmDecl *PD =
1417 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1418 Depth = PD->getDepth();
1419 } else {
1420 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1421 }
1422 }
1423
1424 bool Matches(unsigned ParmDepth) {
1425 if (ParmDepth >= Depth) {
1426 Match = true;
1427 return true;
1428 }
1429 return false;
1430 }
1431
1432 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1433 return !Matches(T->getDepth());
1434 }
1435
1436 bool TraverseTemplateName(TemplateName N) {
1437 if (TemplateTemplateParmDecl *PD =
1438 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1439 if (Matches(PD->getDepth())) return false;
1440 return super::TraverseTemplateName(N);
1441 }
1442
1443 bool VisitDeclRefExpr(DeclRefExpr *E) {
1444 if (NonTypeTemplateParmDecl *PD =
1445 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1446 if (PD->getDepth() == Depth) {
1447 Match = true;
1448 return false;
1449 }
1450 }
1451 return super::VisitDeclRefExpr(E);
1452 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001453
1454 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1455 return TraverseType(T->getInjectedSpecializationType());
1456 }
John McCall4e2cbb22010-10-20 05:44:58 +00001457};
1458}
1459
Douglas Gregorc8406492011-05-10 18:27:06 +00001460/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001461/// list.
1462static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001463DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001464 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001465 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001466 return Checker.Match;
1467}
1468
Douglas Gregorc8406492011-05-10 18:27:06 +00001469// Find the source range corresponding to the named type in the given
1470// nested-name-specifier, if any.
1471static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1472 QualType T,
1473 const CXXScopeSpec &SS) {
1474 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1475 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1476 if (const Type *CurType = NNS->getAsType()) {
1477 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1478 return NNSLoc.getTypeLoc().getSourceRange();
1479 } else
1480 break;
1481
1482 NNSLoc = NNSLoc.getPrefix();
1483 }
1484
1485 return SourceRange();
1486}
1487
Mike Stump1eb44332009-09-09 15:08:12 +00001488/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001489/// specifier, returning the template parameter list that applies to the
1490/// name.
1491///
1492/// \param DeclStartLoc the start of the declaration that has a scope
1493/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001494///
Douglas Gregorc8406492011-05-10 18:27:06 +00001495/// \param DeclLoc The location of the declaration itself.
1496///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497/// \param SS the scope specifier that will be matched to the given template
1498/// parameter lists. This scope specifier precedes a qualified name that is
1499/// being declared.
1500///
1501/// \param ParamLists the template parameter lists, from the outermost to the
1502/// innermost template parameter lists.
1503///
1504/// \param NumParamLists the number of template parameter lists in ParamLists.
1505///
John McCall77e8b112010-04-13 20:37:33 +00001506/// \param IsFriend Whether to apply the slightly different rules for
1507/// matching template parameters to scope specifiers in friend
1508/// declarations.
1509///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001510/// \param IsExplicitSpecialization will be set true if the entity being
1511/// declared is an explicit specialization, false otherwise.
1512///
Mike Stump1eb44332009-09-09 15:08:12 +00001513/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001515/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001516/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001517/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518/// itself a template).
1519TemplateParameterList *
1520Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001521 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522 const CXXScopeSpec &SS,
1523 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001524 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001525 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001526 bool &IsExplicitSpecialization,
1527 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001528 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001529 Invalid = false;
1530
1531 // The sequence of nested types to which we will match up the template
1532 // parameter lists. We first build this list by starting with the type named
1533 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001534 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001535 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001536 if (SS.getScopeRep()) {
1537 if (CXXRecordDecl *Record
1538 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1539 T = Context.getTypeDeclType(Record);
1540 else
1541 T = QualType(SS.getScopeRep()->getAsType(), 0);
1542 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001543
1544 // If we found an explicit specialization that prevents us from needing
1545 // 'template<>' headers, this will be set to the location of that
1546 // explicit specialization.
1547 SourceLocation ExplicitSpecLoc;
1548
1549 while (!T.isNull()) {
1550 NestedTypes.push_back(T);
1551
1552 // Retrieve the parent of a record type.
1553 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1554 // If this type is an explicit specialization, we're done.
1555 if (ClassTemplateSpecializationDecl *Spec
1556 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1557 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1558 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1559 ExplicitSpecLoc = Spec->getLocation();
1560 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001561 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001562 } else if (Record->getTemplateSpecializationKind()
1563 == TSK_ExplicitSpecialization) {
1564 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001565 break;
1566 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001567
1568 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1569 T = Context.getTypeDeclType(Parent);
1570 else
1571 T = QualType();
1572 continue;
1573 }
1574
1575 if (const TemplateSpecializationType *TST
1576 = T->getAs<TemplateSpecializationType>()) {
1577 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1578 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1579 T = Context.getTypeDeclType(Parent);
1580 else
1581 T = QualType();
1582 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001583 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001584 }
1585
1586 // Look one step prior in a dependent template specialization type.
1587 if (const DependentTemplateSpecializationType *DependentTST
1588 = T->getAs<DependentTemplateSpecializationType>()) {
1589 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1590 T = QualType(NNS->getAsType(), 0);
1591 else
1592 T = QualType();
1593 continue;
1594 }
1595
1596 // Look one step prior in a dependent name type.
1597 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1598 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1599 T = QualType(NNS->getAsType(), 0);
1600 else
1601 T = QualType();
1602 continue;
1603 }
1604
1605 // Retrieve the parent of an enumeration type.
1606 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1607 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1608 // check here.
1609 EnumDecl *Enum = EnumT->getDecl();
1610
1611 // Get to the parent type.
1612 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1613 T = Context.getTypeDeclType(Parent);
1614 else
1615 T = QualType();
1616 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Douglas Gregorc8406492011-05-10 18:27:06 +00001619 T = QualType();
1620 }
1621 // Reverse the nested types list, since we want to traverse from the outermost
1622 // to the innermost while checking template-parameter-lists.
1623 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001624
Douglas Gregorc8406492011-05-10 18:27:06 +00001625 // C++0x [temp.expl.spec]p17:
1626 // A member or a member template may be nested within many
1627 // enclosing class templates. In an explicit specialization for
1628 // such a member, the member declaration shall be preceded by a
1629 // template<> for each enclosing class template that is
1630 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001631 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001632 unsigned ParamIdx = 0;
1633 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1634 ++TypeIdx) {
1635 T = NestedTypes[TypeIdx];
1636
1637 // Whether we expect a 'template<>' header.
1638 bool NeedEmptyTemplateHeader = false;
1639
1640 // Whether we expect a template header with parameters.
1641 bool NeedNonemptyTemplateHeader = false;
1642
1643 // For a dependent type, the set of template parameters that we
1644 // expect to see.
1645 TemplateParameterList *ExpectedTemplateParams = 0;
1646
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001647 // C++0x [temp.expl.spec]p15:
1648 // A member or a member template may be nested within many enclosing
1649 // class templates. In an explicit specialization for such a member, the
1650 // member declaration shall be preceded by a template<> for each
1651 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001652 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1653 if (ClassTemplatePartialSpecializationDecl *Partial
1654 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1655 ExpectedTemplateParams = Partial->getTemplateParameters();
1656 NeedNonemptyTemplateHeader = true;
1657 } else if (Record->isDependentType()) {
1658 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001659 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001660 ->getTemplateParameters();
1661 NeedNonemptyTemplateHeader = true;
1662 }
1663 } else if (ClassTemplateSpecializationDecl *Spec
1664 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1665 // C++0x [temp.expl.spec]p4:
1666 // Members of an explicitly specialized class template are defined
1667 // in the same manner as members of normal classes, and not using
1668 // the template<> syntax.
1669 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1670 NeedEmptyTemplateHeader = true;
1671 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001672 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001673 } else if (Record->getTemplateSpecializationKind()) {
1674 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001675 != TSK_ExplicitSpecialization &&
1676 TypeIdx == NumTypes - 1)
1677 IsExplicitSpecialization = true;
1678
1679 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001680 }
1681 } else if (const TemplateSpecializationType *TST
1682 = T->getAs<TemplateSpecializationType>()) {
1683 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1684 ExpectedTemplateParams = Template->getTemplateParameters();
1685 NeedNonemptyTemplateHeader = true;
1686 }
1687 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1688 // FIXME: We actually could/should check the template arguments here
1689 // against the corresponding template parameter list.
1690 NeedNonemptyTemplateHeader = false;
1691 }
1692
Douglas Gregor89b9f102011-06-06 15:22:55 +00001693 // C++ [temp.expl.spec]p16:
1694 // In an explicit specialization declaration for a member of a class
1695 // template or a member template that ap- pears in namespace scope, the
1696 // member template and some of its enclosing class templates may remain
1697 // unspecialized, except that the declaration shall not explicitly
1698 // specialize a class member template if its en- closing class templates
1699 // are not explicitly specialized as well.
1700 if (ParamIdx < NumParamLists) {
1701 if (ParamLists[ParamIdx]->size() == 0) {
1702 if (SawNonEmptyTemplateParameterList) {
1703 Diag(DeclLoc, diag::err_specialize_member_of_template)
1704 << ParamLists[ParamIdx]->getSourceRange();
1705 Invalid = true;
1706 IsExplicitSpecialization = false;
1707 return 0;
1708 }
1709 } else
1710 SawNonEmptyTemplateParameterList = true;
1711 }
1712
Douglas Gregorc8406492011-05-10 18:27:06 +00001713 if (NeedEmptyTemplateHeader) {
1714 // If we're on the last of the types, and we need a 'template<>' header
1715 // here, then it's an explicit specialization.
1716 if (TypeIdx == NumTypes - 1)
1717 IsExplicitSpecialization = true;
1718
1719 if (ParamIdx < NumParamLists) {
1720 if (ParamLists[ParamIdx]->size() > 0) {
1721 // The header has template parameters when it shouldn't. Complain.
1722 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1723 diag::err_template_param_list_matches_nontemplate)
1724 << T
1725 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1726 ParamLists[ParamIdx]->getRAngleLoc())
1727 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1728 Invalid = true;
1729 return 0;
1730 }
1731
1732 // Consume this template header.
1733 ++ParamIdx;
1734 continue;
1735 }
1736
1737 if (!IsFriend) {
1738 // We don't have a template header, but we should.
1739 SourceLocation ExpectedTemplateLoc;
1740 if (NumParamLists > 0)
1741 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1742 else
1743 ExpectedTemplateLoc = DeclStartLoc;
1744
1745 Diag(DeclLoc, diag::err_template_spec_needs_header)
1746 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1747 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1748 }
1749
1750 continue;
1751 }
1752
1753 if (NeedNonemptyTemplateHeader) {
1754 // In friend declarations we can have template-ids which don't
1755 // depend on the corresponding template parameter lists. But
1756 // assume that empty parameter lists are supposed to match this
1757 // template-id.
1758 if (IsFriend && T->isDependentType()) {
1759 if (ParamIdx < NumParamLists &&
1760 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1761 ExpectedTemplateParams = 0;
1762 else
1763 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001764 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001765
Douglas Gregorc8406492011-05-10 18:27:06 +00001766 if (ParamIdx < NumParamLists) {
1767 // Check the template parameter list, if we can.
1768 if (ExpectedTemplateParams &&
1769 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1770 ExpectedTemplateParams,
1771 true, TPL_TemplateMatch))
1772 Invalid = true;
1773
1774 if (!Invalid &&
1775 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1776 TPC_ClassTemplateMember))
1777 Invalid = true;
1778
1779 ++ParamIdx;
1780 continue;
1781 }
1782
1783 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1784 << T
1785 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1786 Invalid = true;
1787 continue;
1788 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001789 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001790
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001791 // If there were at least as many template-ids as there were template
1792 // parameter lists, then there are no template parameter lists remaining for
1793 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001794 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001795 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001797 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001798 if (ParamIdx < NumParamLists - 1) {
1799 bool HasAnyExplicitSpecHeader = false;
1800 bool AllExplicitSpecHeaders = true;
1801 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1802 if (ParamLists[I]->size() == 0)
1803 HasAnyExplicitSpecHeader = true;
1804 else
1805 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001806 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001807
1808 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1809 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1810 : diag::err_template_spec_extra_headers)
1811 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1812 ParamLists[NumParamLists - 2]->getRAngleLoc());
1813
1814 // If there was a specialization somewhere, such that 'template<>' is
1815 // not required, and there were any 'template<>' headers, note where the
1816 // specialization occurred.
1817 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1818 Diag(ExplicitSpecLoc,
1819 diag::note_explicit_template_spec_does_not_need_header)
1820 << NestedTypes.back();
1821
1822 // We have a template parameter list with no corresponding scope, which
1823 // means that the resulting template declaration can't be instantiated
1824 // properly (we'll end up with dependent nodes when we shouldn't).
1825 if (!AllExplicitSpecHeaders)
1826 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001827 }
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Douglas Gregor89b9f102011-06-06 15:22:55 +00001829 // C++ [temp.expl.spec]p16:
1830 // In an explicit specialization declaration for a member of a class
1831 // template or a member template that ap- pears in namespace scope, the
1832 // member template and some of its enclosing class templates may remain
1833 // unspecialized, except that the declaration shall not explicitly
1834 // specialize a class member template if its en- closing class templates
1835 // are not explicitly specialized as well.
1836 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1837 SawNonEmptyTemplateParameterList) {
1838 Diag(DeclLoc, diag::err_specialize_member_of_template)
1839 << ParamLists[ParamIdx]->getSourceRange();
1840 Invalid = true;
1841 IsExplicitSpecialization = false;
1842 return 0;
1843 }
1844
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001845 // Return the last template parameter list, which corresponds to the
1846 // entity being declared.
1847 return ParamLists[NumParamLists - 1];
1848}
1849
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001850void Sema::NoteAllFoundTemplates(TemplateName Name) {
1851 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1852 Diag(Template->getLocation(), diag::note_template_declared_here)
1853 << (isa<FunctionTemplateDecl>(Template)? 0
1854 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001855 : isa<TypeAliasTemplateDecl>(Template)? 2
1856 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001857 << Template->getDeclName();
1858 return;
1859 }
1860
1861 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1862 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1863 IEnd = OST->end();
1864 I != IEnd; ++I)
1865 Diag((*I)->getLocation(), diag::note_template_declared_here)
1866 << 0 << (*I)->getDeclName();
1867
1868 return;
1869 }
1870}
1871
1872
Douglas Gregor7532dc62009-03-30 22:58:21 +00001873QualType Sema::CheckTemplateIdType(TemplateName Name,
1874 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001875 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001876 DependentTemplateName *DTN
1877 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001878 if (DTN && DTN->isIdentifier())
1879 // When building a template-id where the template-name is dependent,
1880 // assume the template is a type template. Either our assumption is
1881 // correct, or the code is ill-formed and will be diagnosed when the
1882 // dependent name is substituted.
1883 return Context.getDependentTemplateSpecializationType(ETK_None,
1884 DTN->getQualifier(),
1885 DTN->getIdentifier(),
1886 TemplateArgs);
1887
Douglas Gregor7532dc62009-03-30 22:58:21 +00001888 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001889 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1890 // We might have a substituted template template parameter pack. If so,
1891 // build a template specialization type for it.
1892 if (Name.getAsSubstTemplateTemplateParmPack())
1893 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001894
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001895 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1896 << Name;
1897 NoteAllFoundTemplates(Name);
1898 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001899 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001900
Douglas Gregor40808ce2009-03-09 23:48:35 +00001901 // Check that the template argument list is well-formed for this
1902 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001903 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001904 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001905 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001906 return QualType();
1907
Douglas Gregor910f8002010-11-07 23:05:16 +00001908 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001909 "Converted template argument list is too short!");
1910
1911 QualType CanonType;
1912
Douglas Gregor561f8122011-07-01 01:22:09 +00001913 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001914 if (TypeAliasTemplateDecl *AliasTemplate
1915 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1916 // Find the canonical type for this type alias template specialization.
1917 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1918 if (Pattern->isInvalidDecl())
1919 return QualType();
1920
1921 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1922 Converted.data(), Converted.size());
1923
1924 // Only substitute for the innermost template argument list.
1925 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001926 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001927 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1928 for (unsigned I = 0; I < Depth; ++I)
1929 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001930
1931 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1932 CanonType = SubstType(Pattern->getUnderlyingType(),
1933 TemplateArgLists, AliasTemplate->getLocation(),
1934 AliasTemplate->getDeclName());
1935 if (CanonType.isNull())
1936 return QualType();
1937 } else if (Name.isDependent() ||
1938 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001939 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001940 // This class template specialization is a dependent
1941 // type. Therefore, its canonical type is another class template
1942 // specialization type that contains all of the converted
1943 // arguments in canonical form. This ensures that, e.g., A<T> and
1944 // A<T, T> have identical types when A is declared as:
1945 //
1946 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001947 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001948 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001949 Converted.data(),
1950 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Douglas Gregor1275ae02009-07-28 23:00:59 +00001952 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001953 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001954 // In the future, we need to teach getTemplateSpecializationType to only
1955 // build the canonical type and return that to us.
1956 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001957
1958 // This might work out to be a current instantiation, in which
1959 // case the canonical type needs to be the InjectedClassNameType.
1960 //
1961 // TODO: in theory this could be a simple hashtable lookup; most
1962 // changes to CurContext don't change the set of current
1963 // instantiations.
1964 if (isa<ClassTemplateDecl>(Template)) {
1965 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1966 // If we get out to a namespace, we're done.
1967 if (Ctx->isFileContext()) break;
1968
1969 // If this isn't a record, keep looking.
1970 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1971 if (!Record) continue;
1972
1973 // Look for one of the two cases with InjectedClassNameTypes
1974 // and check whether it's the same template.
1975 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1976 !Record->getDescribedClassTemplate())
1977 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001978
John McCall31f17ec2010-04-27 00:57:59 +00001979 // Fetch the injected class name type and check whether its
1980 // injected type is equal to the type we just built.
1981 QualType ICNT = Context.getTypeDeclType(Record);
1982 QualType Injected = cast<InjectedClassNameType>(ICNT)
1983 ->getInjectedSpecializationType();
1984
1985 if (CanonType != Injected->getCanonicalTypeInternal())
1986 continue;
1987
1988 // If so, the canonical type of this TST is the injected
1989 // class name type of the record we just found.
1990 assert(ICNT.isCanonical());
1991 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001992 break;
1993 }
1994 }
Mike Stump1eb44332009-09-09 15:08:12 +00001995 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001996 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001997 // Find the class template specialization declaration that
1998 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001999 void *InsertPos = 0;
2000 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002001 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002002 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002003 if (!Decl) {
2004 // This is the first time we have referenced this class template
2005 // specialization. Create the canonical declaration and add it to
2006 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002007 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002008 ClassTemplate->getTemplatedDecl()->getTagKind(),
2009 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002010 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002011 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002012 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002013 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002014 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002015 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002016 Decl->setLexicalDeclContext(CurContext);
2017 }
2018
2019 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002020 assert(isa<RecordType>(CanonType) &&
2021 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002022 }
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Douglas Gregor40808ce2009-03-09 23:48:35 +00002024 // Build the fully-sugared type for this class template
2025 // specialization, which refers back to the class template
2026 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002027 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002028}
2029
John McCallf312b1e2010-08-26 23:41:50 +00002030TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002031Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2032 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002033 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002034 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002035 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002036 if (SS.isInvalid())
2037 return true;
2038
Douglas Gregor7532dc62009-03-30 22:58:21 +00002039 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002040
Douglas Gregor40808ce2009-03-09 23:48:35 +00002041 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002042 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002043 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002044
Douglas Gregora88f09f2011-02-28 17:23:35 +00002045 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2046 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2047 DTN->getQualifier(),
2048 DTN->getIdentifier(),
2049 TemplateArgs);
2050
2051 // Build type-source information.
2052 TypeLocBuilder TLB;
2053 DependentTemplateSpecializationTypeLoc SpecTL
2054 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002055 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002056 SpecTL.setNameLoc(TemplateLoc);
2057 SpecTL.setLAngleLoc(LAngleLoc);
2058 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002059 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002060 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2061 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2062 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2063 }
2064
John McCalld5532b62009-11-23 01:53:49 +00002065 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002066 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002067
2068 if (Result.isNull())
2069 return true;
2070
Douglas Gregor059101f2011-03-02 00:47:37 +00002071 // Build type-source information.
2072 TypeLocBuilder TLB;
2073 TemplateSpecializationTypeLoc SpecTL
2074 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2075 SpecTL.setTemplateNameLoc(TemplateLoc);
2076 SpecTL.setLAngleLoc(LAngleLoc);
2077 SpecTL.setRAngleLoc(RAngleLoc);
2078 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2079 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002080
Douglas Gregor059101f2011-03-02 00:47:37 +00002081 if (SS.isNotEmpty()) {
2082 // Create an elaborated-type-specifier containing the nested-name-specifier.
2083 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2084 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2085 ElabTL.setKeywordLoc(SourceLocation());
2086 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2087 }
2088
2089 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002090}
John McCallf1bbbb42009-09-04 01:14:41 +00002091
Douglas Gregor059101f2011-03-02 00:47:37 +00002092TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002093 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002094 SourceLocation TagLoc,
2095 CXXScopeSpec &SS,
2096 TemplateTy TemplateD,
2097 SourceLocation TemplateLoc,
2098 SourceLocation LAngleLoc,
2099 ASTTemplateArgsPtr TemplateArgsIn,
2100 SourceLocation RAngleLoc) {
2101 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2102
2103 // Translate the parser's template argument list in our AST format.
2104 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2105 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2106
2107 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002108 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002109 ElaboratedTypeKeyword Keyword
2110 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Douglas Gregor059101f2011-03-02 00:47:37 +00002112 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2113 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2114 DTN->getQualifier(),
2115 DTN->getIdentifier(),
2116 TemplateArgs);
2117
2118 // Build type-source information.
2119 TypeLocBuilder TLB;
2120 DependentTemplateSpecializationTypeLoc SpecTL
2121 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2122 SpecTL.setKeywordLoc(TagLoc);
2123 SpecTL.setNameLoc(TemplateLoc);
2124 SpecTL.setLAngleLoc(LAngleLoc);
2125 SpecTL.setRAngleLoc(RAngleLoc);
2126 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2127 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2128 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2129 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2130 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002131
2132 if (TypeAliasTemplateDecl *TAT =
2133 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2134 // C++0x [dcl.type.elab]p2:
2135 // If the identifier resolves to a typedef-name or the simple-template-id
2136 // resolves to an alias template specialization, the
2137 // elaborated-type-specifier is ill-formed.
2138 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2139 Diag(TAT->getLocation(), diag::note_declared_at);
2140 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002141
2142 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2143 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002144 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002145
2146 // Check the tag kind
2147 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002148 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002149
John McCall6b2becf2009-09-08 17:47:29 +00002150 IdentifierInfo *Id = D->getIdentifier();
2151 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002152
Richard Trieubbf34c02011-06-10 03:11:26 +00002153 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2154 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002155 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002156 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002157 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002158 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002159 }
2160 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002161
2162 // Provide source-location information for the template specialization.
2163 TypeLocBuilder TLB;
2164 TemplateSpecializationTypeLoc SpecTL
2165 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2166 SpecTL.setTemplateNameLoc(TemplateLoc);
2167 SpecTL.setLAngleLoc(LAngleLoc);
2168 SpecTL.setRAngleLoc(RAngleLoc);
2169 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2170 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002171
Douglas Gregor059101f2011-03-02 00:47:37 +00002172 // Construct an elaborated type containing the nested-name-specifier (if any)
2173 // and keyword.
2174 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2175 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2176 ElabTL.setKeywordLoc(TagLoc);
2177 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2178 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002179}
2180
John McCall60d7b3a2010-08-24 06:29:42 +00002181ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002182 LookupResult &R,
2183 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002184 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002185 // FIXME: Can we do any checking at this point? I guess we could check the
2186 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002187 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002188 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002189 // foo<int> could identify a single function unambiguously
2190 // This approach does NOT work, since f<int>(1);
2191 // gets resolved prior to resorting to overload resolution
2192 // i.e., template<class T> void f(double);
2193 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002194
2195 // These should be filtered out by our callers.
2196 assert(!R.empty() && "empty lookup results when building templateid");
2197 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2198
John McCallc373d482010-01-27 01:50:18 +00002199 // We don't want lookup warnings at this point.
2200 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002201
John McCallf7a1a742009-11-24 19:00:30 +00002202 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002203 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002204 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002205 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002206 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002207 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002208
2209 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002210}
2211
John McCallf7a1a742009-11-24 19:00:30 +00002212// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002213ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002214Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002215 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002216 const TemplateArgumentListInfo &TemplateArgs) {
2217 DeclContext *DC;
2218 if (!(DC = computeDeclContext(SS, false)) ||
2219 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002220 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002221 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002223 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002224 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002225 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2226 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002227
John McCallf7a1a742009-11-24 19:00:30 +00002228 if (R.isAmbiguous())
2229 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002230
John McCallf7a1a742009-11-24 19:00:30 +00002231 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002232 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2233 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002234 return ExprError();
2235 }
2236
2237 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002238 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2239 << (NestedNameSpecifier*) SS.getScopeRep()
2240 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002241 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2242 return ExprError();
2243 }
2244
2245 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002246}
2247
Douglas Gregorc45c2322009-03-31 00:43:58 +00002248/// \brief Form a dependent template name.
2249///
2250/// This action forms a dependent template name given the template
2251/// name and its (presumably dependent) scope specifier. For
2252/// example, given "MetaFun::template apply", the scope specifier \p
2253/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2254/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002256 SourceLocation TemplateKWLoc,
2257 CXXScopeSpec &SS,
2258 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002259 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002260 bool EnteringContext,
2261 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002262 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2263 !getLangOptions().CPlusPlus0x)
2264 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265 << FixItHint::CreateRemoval(TemplateKWLoc);
2266
Douglas Gregor0707bc52010-01-19 16:01:07 +00002267 DeclContext *LookupCtx = 0;
2268 if (SS.isSet())
2269 LookupCtx = computeDeclContext(SS, EnteringContext);
2270 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002271 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002272 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002273 // C++0x [temp.names]p5:
2274 // If a name prefixed by the keyword template is not the name of
2275 // a template, the program is ill-formed. [Note: the keyword
2276 // template may not be applied to non-template members of class
2277 // templates. -end note ] [ Note: as is the case with the
2278 // typename prefix, the template prefix is allowed in cases
2279 // where it is not strictly necessary; i.e., when the
2280 // nested-name-specifier or the expression on the left of the ->
2281 // or . is not dependent on a template-parameter, or the use
2282 // does not appear in the scope of a template. -end note]
2283 //
2284 // Note: C++03 was more strict here, because it banned the use of
2285 // the "template" keyword prior to a template-name that was not a
2286 // dependent name. C++ DR468 relaxed this requirement (the
2287 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002288 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002289 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002290 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2291 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002292 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002293 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2294 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002295 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2296 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002297 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002298 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002299 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002300 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002301 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002302 << Name.getSourceRange()
2303 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002304 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002305 } else {
2306 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002307 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002308 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002309 }
2310
Mike Stump1eb44332009-09-09 15:08:12 +00002311 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002312 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002313
Douglas Gregor014e88d2009-11-03 23:16:33 +00002314 switch (Name.getKind()) {
2315 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002316 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002317 Name.Identifier));
2318 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002319
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002320 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002321 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002322 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002323 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002324
2325 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002326 llvm_unreachable(
2327 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002328
Douglas Gregor014e88d2009-11-03 23:16:33 +00002329 default:
2330 break;
2331 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002332
2333 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002334 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002335 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002336 << Name.getSourceRange()
2337 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002338 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002339}
2340
Mike Stump1eb44332009-09-09 15:08:12 +00002341bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002342 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002343 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002344 const TemplateArgument &Arg = AL.getArgument();
2345
Anders Carlsson436b1562009-06-13 00:33:33 +00002346 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002347 switch(Arg.getKind()) {
2348 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002349 // C++ [temp.arg.type]p1:
2350 // A template-argument for a template-parameter which is a
2351 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002352 break;
2353 case TemplateArgument::Template: {
2354 // We have a template type parameter but the template argument
2355 // is a template without any arguments.
2356 SourceRange SR = AL.getSourceRange();
2357 TemplateName Name = Arg.getAsTemplate();
2358 Diag(SR.getBegin(), diag::err_template_missing_args)
2359 << Name << SR;
2360 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2361 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002362
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002363 return true;
2364 }
2365 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002366 // We have a template type parameter but the template argument
2367 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002368 SourceRange SR = AL.getSourceRange();
2369 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002370 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Anders Carlsson436b1562009-06-13 00:33:33 +00002372 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002373 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002374 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002375
John McCalla93c9342009-12-07 02:54:59 +00002376 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002377 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Anders Carlsson436b1562009-06-13 00:33:33 +00002379 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002380 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2381
2382 // Objective-C ARC:
2383 // If an explicitly-specified template argument type is a lifetime type
2384 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2385 if (getLangOptions().ObjCAutoRefCount &&
2386 ArgType->isObjCLifetimeType() &&
2387 !ArgType.getObjCLifetime()) {
2388 Qualifiers Qs;
2389 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2390 ArgType = Context.getQualifiedType(ArgType, Qs);
2391 }
2392
2393 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002394 return false;
2395}
2396
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002397/// \brief Substitute template arguments into the default template argument for
2398/// the given template type parameter.
2399///
2400/// \param SemaRef the semantic analysis object for which we are performing
2401/// the substitution.
2402///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002403/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002404/// for.
2405///
2406/// \param TemplateLoc the location of the template name that started the
2407/// template-id we are checking.
2408///
2409/// \param RAngleLoc the location of the right angle bracket ('>') that
2410/// terminates the template-id.
2411///
2412/// \param Param the template template parameter whose default we are
2413/// substituting into.
2414///
2415/// \param Converted the list of template arguments provided for template
2416/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002417/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002418static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002419SubstDefaultTemplateArgument(Sema &SemaRef,
2420 TemplateDecl *Template,
2421 SourceLocation TemplateLoc,
2422 SourceLocation RAngleLoc,
2423 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002424 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002425 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002426
2427 // If the argument type is dependent, instantiate it now based
2428 // on the previously-computed template arguments.
2429 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002430 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002431 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002432
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002433 MultiLevelTemplateArgumentList AllTemplateArgs
2434 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2435
2436 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002437 Template, Converted.data(),
2438 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002439 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002441 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2442 Param->getDefaultArgumentLoc(),
2443 Param->getDeclName());
2444 }
2445
2446 return ArgType;
2447}
2448
2449/// \brief Substitute template arguments into the default template argument for
2450/// the given non-type template parameter.
2451///
2452/// \param SemaRef the semantic analysis object for which we are performing
2453/// the substitution.
2454///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002455/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002456/// for.
2457///
2458/// \param TemplateLoc the location of the template name that started the
2459/// template-id we are checking.
2460///
2461/// \param RAngleLoc the location of the right angle bracket ('>') that
2462/// terminates the template-id.
2463///
Douglas Gregor788cd062009-11-11 01:00:40 +00002464/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002465/// substituting into.
2466///
2467/// \param Converted the list of template arguments provided for template
2468/// parameters that precede \p Param in the template parameter list.
2469///
2470/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002471static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002472SubstDefaultTemplateArgument(Sema &SemaRef,
2473 TemplateDecl *Template,
2474 SourceLocation TemplateLoc,
2475 SourceLocation RAngleLoc,
2476 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002477 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002478 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002479 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002481 MultiLevelTemplateArgumentList AllTemplateArgs
2482 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002483
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002484 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002485 Template, Converted.data(),
2486 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002487 SourceRange(TemplateLoc, RAngleLoc));
2488
2489 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2490}
2491
Douglas Gregor788cd062009-11-11 01:00:40 +00002492/// \brief Substitute template arguments into the default template argument for
2493/// the given template template parameter.
2494///
2495/// \param SemaRef the semantic analysis object for which we are performing
2496/// the substitution.
2497///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002498/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002499/// for.
2500///
2501/// \param TemplateLoc the location of the template name that started the
2502/// template-id we are checking.
2503///
2504/// \param RAngleLoc the location of the right angle bracket ('>') that
2505/// terminates the template-id.
2506///
2507/// \param Param the template template parameter whose default we are
2508/// substituting into.
2509///
2510/// \param Converted the list of template arguments provided for template
2511/// parameters that precede \p Param in the template parameter list.
2512///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002513/// \param QualifierLoc Will be set to the nested-name-specifier (with
2514/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002515///
Douglas Gregor788cd062009-11-11 01:00:40 +00002516/// \returns the substituted template argument, or NULL if an error occurred.
2517static TemplateName
2518SubstDefaultTemplateArgument(Sema &SemaRef,
2519 TemplateDecl *Template,
2520 SourceLocation TemplateLoc,
2521 SourceLocation RAngleLoc,
2522 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002523 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002524 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002526 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527
Douglas Gregor788cd062009-11-11 01:00:40 +00002528 MultiLevelTemplateArgumentList AllTemplateArgs
2529 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530
Douglas Gregor788cd062009-11-11 01:00:40 +00002531 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002532 Template, Converted.data(),
2533 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002534 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002536 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002537 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002538 if (QualifierLoc) {
2539 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2540 AllTemplateArgs);
2541 if (!QualifierLoc)
2542 return TemplateName();
2543 }
2544
Douglas Gregor1d752d72011-03-02 18:46:51 +00002545 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002546 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002548 AllTemplateArgs);
2549}
2550
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002551/// \brief If the given template parameter has a default template
2552/// argument, substitute into that default template argument and
2553/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002554TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002555Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2556 SourceLocation TemplateLoc,
2557 SourceLocation RAngleLoc,
2558 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002559 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002560 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002561 if (!TypeParm->hasDefaultArgument())
2562 return TemplateArgumentLoc();
2563
John McCalla93c9342009-12-07 02:54:59 +00002564 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002565 TemplateLoc,
2566 RAngleLoc,
2567 TypeParm,
2568 Converted);
2569 if (DI)
2570 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2571
2572 return TemplateArgumentLoc();
2573 }
2574
2575 if (NonTypeTemplateParmDecl *NonTypeParm
2576 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2577 if (!NonTypeParm->hasDefaultArgument())
2578 return TemplateArgumentLoc();
2579
John McCall60d7b3a2010-08-24 06:29:42 +00002580 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002581 TemplateLoc,
2582 RAngleLoc,
2583 NonTypeParm,
2584 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002585 if (Arg.isInvalid())
2586 return TemplateArgumentLoc();
2587
2588 Expr *ArgE = Arg.takeAs<Expr>();
2589 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2590 }
2591
2592 TemplateTemplateParmDecl *TempTempParm
2593 = cast<TemplateTemplateParmDecl>(Param);
2594 if (!TempTempParm->hasDefaultArgument())
2595 return TemplateArgumentLoc();
2596
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002597
Douglas Gregor1d752d72011-03-02 18:46:51 +00002598 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002599 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002600 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002601 RAngleLoc,
2602 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002603 Converted,
2604 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002605 if (TName.isNull())
2606 return TemplateArgumentLoc();
2607
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002608 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002609 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002610 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2611}
2612
Douglas Gregore7526412009-11-11 19:31:23 +00002613/// \brief Check that the given template argument corresponds to the given
2614/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002615///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002617/// checked.
2618///
2619/// \param Arg The template argument.
2620///
2621/// \param Template The template in which the template argument resides.
2622///
2623/// \param TemplateLoc The location of the template name for the template
2624/// whose argument list we're matching.
2625///
2626/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2627/// the template argument list.
2628///
2629/// \param ArgumentPackIndex The index into the argument pack where this
2630/// argument will be placed. Only valid if the parameter is a parameter pack.
2631///
2632/// \param Converted The checked, converted argument will be added to the
2633/// end of this small vector.
2634///
2635/// \param CTAK Describes how we arrived at this particular template argument:
2636/// explicitly written, deduced, etc.
2637///
2638/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002639bool Sema::CheckTemplateArgument(NamedDecl *Param,
2640 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002641 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002642 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002643 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002644 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002645 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002646 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002647 // Check template type parameters.
2648 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002649 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002650
Douglas Gregord9e15302009-11-11 19:41:09 +00002651 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002652 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002653 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002654 // with the template arguments we've seen thus far. But if the
2655 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002656 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002657 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2658 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002659
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002660 if (NTTPType->isDependentType() &&
2661 !isa<TemplateTemplateParmDecl>(Template) &&
2662 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002663 // Do substitution on the type of the non-type template parameter.
2664 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002665 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002666 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002667
2668 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002669 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002670 NTTPType = SubstType(NTTPType,
2671 MultiLevelTemplateArgumentList(TemplateArgs),
2672 NTTP->getLocation(),
2673 NTTP->getDeclName());
2674 // If that worked, check the non-type template parameter type
2675 // for validity.
2676 if (!NTTPType.isNull())
2677 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2678 NTTP->getLocation());
2679 if (NTTPType.isNull())
2680 return true;
2681 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002682
Douglas Gregore7526412009-11-11 19:31:23 +00002683 switch (Arg.getArgument().getKind()) {
2684 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002685 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Douglas Gregore7526412009-11-11 19:31:23 +00002687 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002688 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002689 ExprResult Res =
2690 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2691 Result, CTAK);
2692 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002693 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694
Douglas Gregor910f8002010-11-07 23:05:16 +00002695 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002696 break;
2697 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002698
Douglas Gregore7526412009-11-11 19:31:23 +00002699 case TemplateArgument::Declaration:
2700 case TemplateArgument::Integral:
2701 // We've already checked this template argument, so just copy
2702 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002703 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002704 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705
Douglas Gregore7526412009-11-11 19:31:23 +00002706 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002707 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002708 // We were given a template template argument. It may not be ill-formed;
2709 // see below.
2710 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002711 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2712 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002713 // We have a template argument such as \c T::template X, which we
2714 // parsed as a template template argument. However, since we now
2715 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002716 // template name into an expression.
2717
2718 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2719 Arg.getTemplateNameLoc());
2720
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002721 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002722 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002723 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002724 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002725 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002726
Douglas Gregora7fc9012011-01-05 18:58:31 +00002727 // If we parsed the template argument as a pack expansion, create a
2728 // pack expansion expression.
2729 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002730 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2731 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002732 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregore7526412009-11-11 19:31:23 +00002735 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002736 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2737 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002738 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739
Douglas Gregor910f8002010-11-07 23:05:16 +00002740 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002741 break;
2742 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002743
Douglas Gregore7526412009-11-11 19:31:23 +00002744 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002745 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002746 // therefore cannot be a non-type template argument.
2747 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2748 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregore7526412009-11-11 19:31:23 +00002750 Diag(Param->getLocation(), diag::note_template_param_here);
2751 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregore7526412009-11-11 19:31:23 +00002753 case TemplateArgument::Type: {
2754 // We have a non-type template parameter but the template
2755 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002756
Douglas Gregore7526412009-11-11 19:31:23 +00002757 // C++ [temp.arg]p2:
2758 // In a template-argument, an ambiguity between a type-id and
2759 // an expression is resolved to a type-id, regardless of the
2760 // form of the corresponding template-parameter.
2761 //
2762 // We warn specifically about this case, since it can be rather
2763 // confusing for users.
2764 QualType T = Arg.getArgument().getAsType();
2765 SourceRange SR = Arg.getSourceRange();
2766 if (T->isFunctionType())
2767 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2768 else
2769 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2770 Diag(Param->getLocation(), diag::note_template_param_here);
2771 return true;
2772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773
Douglas Gregore7526412009-11-11 19:31:23 +00002774 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002775 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002776 break;
2777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002778
Douglas Gregore7526412009-11-11 19:31:23 +00002779 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780 }
2781
2782
Douglas Gregore7526412009-11-11 19:31:23 +00002783 // Check template template parameters.
2784 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
Douglas Gregore7526412009-11-11 19:31:23 +00002786 // Substitute into the template parameter list of the template
2787 // template parameter, since previously-supplied template arguments
2788 // may appear within the template template parameter.
2789 {
2790 // Set up a template instantiation context.
2791 LocalInstantiationScope Scope(*this);
2792 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002793 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002794 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795
2796 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002797 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002798 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002800 MultiLevelTemplateArgumentList(TemplateArgs)));
2801 if (!TempParm)
2802 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002803 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804
Douglas Gregore7526412009-11-11 19:31:23 +00002805 switch (Arg.getArgument().getKind()) {
2806 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002807 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002808
Douglas Gregore7526412009-11-11 19:31:23 +00002809 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002810 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002811 if (CheckTemplateArgument(TempParm, Arg))
2812 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002813
Douglas Gregor910f8002010-11-07 23:05:16 +00002814 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002815 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
Douglas Gregore7526412009-11-11 19:31:23 +00002817 case TemplateArgument::Expression:
2818 case TemplateArgument::Type:
2819 // We have a template template parameter but the template
2820 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002821 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2822 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002823 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002824
Douglas Gregore7526412009-11-11 19:31:23 +00002825 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002826 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002827 "Declaration argument with template template parameter");
2828 break;
2829 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002830 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002831 "Integral argument with template template parameter");
2832 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002833
Douglas Gregore7526412009-11-11 19:31:23 +00002834 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002835 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002836 break;
2837 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838
Douglas Gregore7526412009-11-11 19:31:23 +00002839 return false;
2840}
2841
Douglas Gregorc15cb382009-02-09 23:23:08 +00002842/// \brief Check that the given template argument list is well-formed
2843/// for specializing the given template.
2844bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2845 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002846 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002847 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002848 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002849 TemplateParameterList *Params = Template->getTemplateParameters();
2850 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002851 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002852 bool Invalid = false;
2853
John McCalld5532b62009-11-23 01:53:49 +00002854 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2855
Mike Stump1eb44332009-09-09 15:08:12 +00002856 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002857 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002858
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002859 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002860 (NumArgs < Params->getMinRequiredArguments() &&
2861 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002862 // FIXME: point at either the first arg beyond what we can handle,
2863 // or the '>', depending on whether we have too many or too few
2864 // arguments.
2865 SourceRange Range;
2866 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002867 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002868 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2869 << (NumArgs > NumParams)
2870 << (isa<ClassTemplateDecl>(Template)? 0 :
2871 isa<FunctionTemplateDecl>(Template)? 1 :
2872 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2873 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002874 Diag(Template->getLocation(), diag::note_template_decl_here)
2875 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002876 Invalid = true;
2877 }
Mike Stump1eb44332009-09-09 15:08:12 +00002878
2879 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002880 // [...] The type and form of each template-argument specified in
2881 // a template-id shall match the type and form specified for the
2882 // corresponding parameter declared by the template in its
2883 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002884 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002885 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002886 TemplateParameterList::iterator Param = Params->begin(),
2887 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002888 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002889 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002890 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002891 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002892 // If we have an expanded parameter pack, make sure we don't have too
2893 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002894 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002895 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002896 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002897 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2898 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2899 << true
2900 << (isa<ClassTemplateDecl>(Template)? 0 :
2901 isa<FunctionTemplateDecl>(Template)? 1 :
2902 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2903 << Template;
2904 Diag(Template->getLocation(), diag::note_template_decl_here)
2905 << Params->getSourceRange();
2906 return true;
2907 }
2908 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002909
Douglas Gregorf35f8282009-11-11 21:54:23 +00002910 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002911 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2912 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002913 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002914 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002915
Douglas Gregor14be16b2010-12-20 16:57:52 +00002916 if ((*Param)->isTemplateParameterPack()) {
2917 // The template parameter was a template parameter pack, so take the
2918 // deduced argument and place it on the argument pack. Note that we
2919 // stay on the same template parameter so that we can deduce more
2920 // arguments.
2921 ArgumentPack.push_back(Converted.back());
2922 Converted.pop_back();
2923 } else {
2924 // Move to the next template parameter.
2925 ++Param;
2926 }
2927 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002928 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002929 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930
Douglas Gregor8735b292011-06-03 02:59:40 +00002931 // If we're checking a partial template argument list, we're done.
2932 if (PartialTemplateArgs) {
2933 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2934 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2935 ArgumentPack.data(),
2936 ArgumentPack.size()));
2937
2938 return Invalid;
2939 }
2940
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002941 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002942 // arguments, just break out now and we'll fill in the argument pack below.
2943 if ((*Param)->isTemplateParameterPack())
2944 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002945
Douglas Gregorf35f8282009-11-11 21:54:23 +00002946 // We have a default template argument that we will use.
2947 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002948
Douglas Gregorf35f8282009-11-11 21:54:23 +00002949 // Retrieve the default template argument from the template
2950 // parameter. For each kind of template parameter, we substitute the
2951 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002952 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002953 // the default argument.
2954 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2955 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002956 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002957 break;
2958 }
2959
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002960 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002961 Template,
2962 TemplateLoc,
2963 RAngleLoc,
2964 TTP,
2965 Converted);
2966 if (!ArgType)
2967 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968
Douglas Gregorf35f8282009-11-11 21:54:23 +00002969 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2970 ArgType);
2971 } else if (NonTypeTemplateParmDecl *NTTP
2972 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2973 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002974 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002975 break;
2976 }
2977
John McCall60d7b3a2010-08-24 06:29:42 +00002978 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002979 TemplateLoc,
2980 RAngleLoc,
2981 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002982 Converted);
2983 if (E.isInvalid())
2984 return true;
2985
2986 Expr *Ex = E.takeAs<Expr>();
2987 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2988 } else {
2989 TemplateTemplateParmDecl *TempParm
2990 = cast<TemplateTemplateParmDecl>(*Param);
2991
2992 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002993 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002994 break;
2995 }
2996
Douglas Gregor1d752d72011-03-02 18:46:51 +00002997 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002998 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002999 TemplateLoc,
3000 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003001 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003002 Converted,
3003 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003004 if (Name.isNull())
3005 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003006
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003007 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3008 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003009 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003010
Douglas Gregorf35f8282009-11-11 21:54:23 +00003011 // Introduce an instantiation record that describes where we are using
3012 // the default template argument.
3013 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003014 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003015 SourceRange(TemplateLoc, RAngleLoc));
3016
Douglas Gregorf35f8282009-11-11 21:54:23 +00003017 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003018 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003019 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003020 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003021
Douglas Gregor67714232011-03-03 02:41:12 +00003022 // Core issue 150 (assumed resolution): if this is a template template
3023 // parameter, keep track of the default template arguments from the
3024 // template definition.
3025 if (isTemplateTemplateParameter)
3026 TemplateArgs.addArgument(Arg);
3027
Douglas Gregor14be16b2010-12-20 16:57:52 +00003028 // Move to the next template parameter and argument.
3029 ++Param;
3030 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003031 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003032
Douglas Gregor14be16b2010-12-20 16:57:52 +00003033 // Form argument packs for each of the parameter packs remaining.
3034 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003035 // If we're checking a partial list of template arguments, don't fill
3036 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003037
3038 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003039 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003040 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003041 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3043 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003044 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003045 ArgumentPack.clear();
3046 }
3047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003048
Douglas Gregor14be16b2010-12-20 16:57:52 +00003049 ++Param;
3050 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003051
Douglas Gregorc15cb382009-02-09 23:23:08 +00003052 return Invalid;
3053}
3054
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003055namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056 class UnnamedLocalNoLinkageFinder
3057 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003058 {
3059 Sema &S;
3060 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003062 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003064 public:
3065 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3066
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003067 bool Visit(QualType T) {
3068 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003069 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003070
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003071#define TYPE(Class, Parent) \
3072 bool Visit##Class##Type(const Class##Type *);
3073#define ABSTRACT_TYPE(Class, Parent) \
3074 bool Visit##Class##Type(const Class##Type *) { return false; }
3075#define NON_CANONICAL_TYPE(Class, Parent) \
3076 bool Visit##Class##Type(const Class##Type *) { return false; }
3077#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003079 bool VisitTagDecl(const TagDecl *Tag);
3080 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3081 };
3082}
3083
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003085 return false;
3086}
3087
3088bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3089 return Visit(T->getElementType());
3090}
3091
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003092bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003093 return Visit(T->getPointeeType());
3094}
3095
3096bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003097 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003098 return Visit(T->getPointeeType());
3099}
3100
3101bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003102 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003103 return Visit(T->getPointeeType());
3104}
3105
3106bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003107 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003108 return Visit(T->getPointeeType());
3109}
3110
3111bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003112 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003113 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3114}
3115
3116bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003117 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003118 return Visit(T->getElementType());
3119}
3120
3121bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003122 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003123 return Visit(T->getElementType());
3124}
3125
3126bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003127 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003128 return Visit(T->getElementType());
3129}
3130
3131bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003132 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003133 return Visit(T->getElementType());
3134}
3135
3136bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003137 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003138 return Visit(T->getElementType());
3139}
3140
3141bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3142 return Visit(T->getElementType());
3143}
3144
3145bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3146 return Visit(T->getElementType());
3147}
3148
3149bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3150 const FunctionProtoType* T) {
3151 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003152 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003153 A != AEnd; ++A) {
3154 if (Visit(*A))
3155 return true;
3156 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003157
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003158 return Visit(T->getResultType());
3159}
3160
3161bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3162 const FunctionNoProtoType* T) {
3163 return Visit(T->getResultType());
3164}
3165
3166bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3167 const UnresolvedUsingType*) {
3168 return false;
3169}
3170
3171bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3172 return false;
3173}
3174
3175bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3176 return Visit(T->getUnderlyingType());
3177}
3178
3179bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3180 return false;
3181}
3182
Sean Huntca63c202011-05-24 22:41:36 +00003183bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3184 const UnaryTransformType*) {
3185 return false;
3186}
3187
Richard Smith34b41d92011-02-20 03:19:35 +00003188bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3189 return Visit(T->getDeducedType());
3190}
3191
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003192bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3193 return VisitTagDecl(T->getDecl());
3194}
3195
3196bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3197 return VisitTagDecl(T->getDecl());
3198}
3199
3200bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3201 const TemplateTypeParmType*) {
3202 return false;
3203}
3204
Douglas Gregorc3069d62011-01-14 02:55:32 +00003205bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3206 const SubstTemplateTypeParmPackType *) {
3207 return false;
3208}
3209
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003210bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3211 const TemplateSpecializationType*) {
3212 return false;
3213}
3214
3215bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3216 const InjectedClassNameType* T) {
3217 return VisitTagDecl(T->getDecl());
3218}
3219
3220bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3221 const DependentNameType* T) {
3222 return VisitNestedNameSpecifier(T->getQualifier());
3223}
3224
3225bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3226 const DependentTemplateSpecializationType* T) {
3227 return VisitNestedNameSpecifier(T->getQualifier());
3228}
3229
Douglas Gregor7536dd52010-12-20 02:24:11 +00003230bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3231 const PackExpansionType* T) {
3232 return Visit(T->getPattern());
3233}
3234
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003235bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3236 return false;
3237}
3238
3239bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3240 const ObjCInterfaceType *) {
3241 return false;
3242}
3243
3244bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3245 const ObjCObjectPointerType *) {
3246 return false;
3247}
3248
Eli Friedmanb001de72011-10-06 23:00:33 +00003249bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3250 return Visit(T->getValueType());
3251}
3252
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003253bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3254 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3255 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3256 << S.Context.getTypeDeclType(Tag) << SR;
3257 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003258 }
3259
Richard Smith162e1c12011-04-15 14:24:37 +00003260 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003261 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3262 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3263 return true;
3264 }
3265
3266 return false;
3267}
3268
3269bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3270 NestedNameSpecifier *NNS) {
3271 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3272 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003273
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003274 switch (NNS->getKind()) {
3275 case NestedNameSpecifier::Identifier:
3276 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003277 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003278 case NestedNameSpecifier::Global:
3279 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003280
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003281 case NestedNameSpecifier::TypeSpec:
3282 case NestedNameSpecifier::TypeSpecWithTemplate:
3283 return Visit(QualType(NNS->getAsType(), 0));
3284 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003285 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003286}
3287
3288
Douglas Gregorc15cb382009-02-09 23:23:08 +00003289/// \brief Check a template argument against its corresponding
3290/// template type parameter.
3291///
3292/// This routine implements the semantics of C++ [temp.arg.type]. It
3293/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003294bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003295 TypeSourceInfo *ArgInfo) {
3296 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003297 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003298 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003299
3300 if (Arg->isVariablyModifiedType()) {
3301 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003302 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003303 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003304 }
3305
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003306 // C++03 [temp.arg.type]p2:
3307 // A local type, a type with no linkage, an unnamed type or a type
3308 // compounded from any of these types shall not be used as a
3309 // template-argument for a template type-parameter.
3310 //
3311 // C++0x allows these, and even in C++03 we allow them as an extension with
3312 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003313 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003314 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3315 (void)Finder.Visit(Context.getCanonicalType(Arg));
3316 }
3317
Douglas Gregorc15cb382009-02-09 23:23:08 +00003318 return false;
3319}
3320
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003321/// \brief Checks whether the given template argument is the address
3322/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003323static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003324CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3325 NonTypeTemplateParmDecl *Param,
3326 QualType ParamType,
3327 Expr *ArgIn,
3328 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003329 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003330 Expr *Arg = ArgIn;
3331 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003332
3333 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003334 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003335
3336 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003337 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003338 // A template-argument for a non-type, non-template
3339 // template-parameter shall be one of: [...]
3340 //
3341 // -- the address of an object or function with external
3342 // linkage, including function templates and function
3343 // template-ids but excluding non-static class members,
3344 // expressed as & id-expression where the & is optional if
3345 // the name refers to a function or array, or if the
3346 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003348 // In C++98/03 mode, give an extension warning on any extra parentheses.
3349 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3350 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003351 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003352 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003354 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003355 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003356 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003357 }
3358
3359 Arg = Parens->getSubExpr();
3360 }
3361
John McCall91a57552011-07-15 05:09:51 +00003362 while (SubstNonTypeTemplateParmExpr *subst =
3363 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3364 Arg = subst->getReplacement()->IgnoreImpCasts();
3365
Douglas Gregorb7a09262010-04-01 18:32:35 +00003366 bool AddressTaken = false;
3367 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003368 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003369 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003370 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003371 AddressTaken = true;
3372 AddrOpLoc = UnOp->getOperatorLoc();
3373 }
Francois Picheta343a412011-04-29 09:08:14 +00003374 }
John McCall91a57552011-07-15 05:09:51 +00003375
Francois Pichet62ec1f22011-09-17 17:15:52 +00003376 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003377 Converted = TemplateArgument(ArgIn);
3378 return false;
3379 }
3380
3381 while (SubstNonTypeTemplateParmExpr *subst =
3382 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3383 Arg = subst->getReplacement()->IgnoreImpCasts();
3384
3385 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003386 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003387 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3388 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003389 S.Diag(Param->getLocation(), diag::note_template_param_here);
3390 return true;
3391 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003392
3393 // Stop checking the precise nature of the argument if it is value dependent,
3394 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003395 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003396 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003397 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003398 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003399
Douglas Gregorb7a09262010-04-01 18:32:35 +00003400 if (!isa<ValueDecl>(DRE->getDecl())) {
3401 S.Diag(Arg->getSourceRange().getBegin(),
3402 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003403 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003404 S.Diag(Param->getLocation(), diag::note_template_param_here);
3405 return true;
3406 }
3407
3408 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003409
3410 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003411 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3412 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003413 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003414 S.Diag(Param->getLocation(), diag::note_template_param_here);
3415 return true;
3416 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003417
3418 // Cannot refer to non-static member functions
3419 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 if (!Method->isStatic()) {
3421 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003422 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003423 S.Diag(Param->getLocation(), diag::note_template_param_here);
3424 return true;
3425 }
Mike Stump1eb44332009-09-09 15:08:12 +00003426
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003427 // Functions must have external linkage.
3428 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003429 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003430 S.Diag(Arg->getSourceRange().getBegin(),
3431 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003432 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003433 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003434 << true;
3435 return true;
3436 }
3437
3438 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003439 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003440
Douglas Gregorb7a09262010-04-01 18:32:35 +00003441 // If the template parameter has pointer type, the function decays.
3442 if (ParamType->isPointerType() && !AddressTaken)
3443 ArgType = S.Context.getPointerType(Func->getType());
3444 else if (AddressTaken && ParamType->isReferenceType()) {
3445 // If we originally had an address-of operator, but the
3446 // parameter has reference type, complain and (if things look
3447 // like they will work) drop the address-of operator.
3448 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3449 ParamType.getNonReferenceType())) {
3450 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3451 << ParamType;
3452 S.Diag(Param->getLocation(), diag::note_template_param_here);
3453 return true;
3454 }
3455
3456 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3457 << ParamType
3458 << FixItHint::CreateRemoval(AddrOpLoc);
3459 S.Diag(Param->getLocation(), diag::note_template_param_here);
3460
3461 ArgType = Func->getType();
3462 }
3463 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003464 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003465 S.Diag(Arg->getSourceRange().getBegin(),
3466 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003467 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003468 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003469 << true;
3470 return true;
3471 }
3472
Douglas Gregorb7a09262010-04-01 18:32:35 +00003473 // A value of reference type is not an object.
3474 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003475 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003476 diag::err_template_arg_reference_var)
3477 << Var->getType() << Arg->getSourceRange();
3478 S.Diag(Param->getLocation(), diag::note_template_param_here);
3479 return true;
3480 }
3481
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003482 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003483 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003484
3485 // If the template parameter has pointer type, we must have taken
3486 // the address of this object.
3487 if (ParamType->isReferenceType()) {
3488 if (AddressTaken) {
3489 // If we originally had an address-of operator, but the
3490 // parameter has reference type, complain and (if things look
3491 // like they will work) drop the address-of operator.
3492 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3493 ParamType.getNonReferenceType())) {
3494 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3495 << ParamType;
3496 S.Diag(Param->getLocation(), diag::note_template_param_here);
3497 return true;
3498 }
3499
3500 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3501 << ParamType
3502 << FixItHint::CreateRemoval(AddrOpLoc);
3503 S.Diag(Param->getLocation(), diag::note_template_param_here);
3504
3505 ArgType = Var->getType();
3506 }
3507 } else if (!AddressTaken && ParamType->isPointerType()) {
3508 if (Var->getType()->isArrayType()) {
3509 // Array-to-pointer decay.
3510 ArgType = S.Context.getArrayDecayedType(Var->getType());
3511 } else {
3512 // If the template parameter has pointer type but the address of
3513 // this object was not taken, complain and (possibly) recover by
3514 // taking the address of the entity.
3515 ArgType = S.Context.getPointerType(Var->getType());
3516 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3517 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3518 << ParamType;
3519 S.Diag(Param->getLocation(), diag::note_template_param_here);
3520 return true;
3521 }
3522
3523 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3524 << ParamType
3525 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3526
3527 S.Diag(Param->getLocation(), diag::note_template_param_here);
3528 }
3529 }
3530 } else {
3531 // We found something else, but we don't know specifically what it is.
3532 S.Diag(Arg->getSourceRange().getBegin(),
3533 diag::err_template_arg_not_object_or_func)
3534 << Arg->getSourceRange();
3535 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3536 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003537 }
Mike Stump1eb44332009-09-09 15:08:12 +00003538
John McCallf85e1932011-06-15 23:02:42 +00003539 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003540 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003541 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003542 S.IsQualificationConversion(ArgType, ParamType, false,
3543 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 // For pointer-to-object types, qualification conversions are
3545 // permitted.
3546 } else {
3547 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3548 if (!ParamRef->getPointeeType()->isFunctionType()) {
3549 // C++ [temp.arg.nontype]p5b3:
3550 // For a non-type template-parameter of type reference to
3551 // object, no conversions apply. The type referred to by the
3552 // reference may be more cv-qualified than the (otherwise
3553 // identical) type of the template- argument. The
3554 // template-parameter is bound directly to the
3555 // template-argument, which shall be an lvalue.
3556
3557 // FIXME: Other qualifiers?
3558 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3559 unsigned ArgQuals = ArgType.getCVRQualifiers();
3560
3561 if ((ParamQuals | ArgQuals) != ParamQuals) {
3562 S.Diag(Arg->getSourceRange().getBegin(),
3563 diag::err_template_arg_ref_bind_ignores_quals)
3564 << ParamType << Arg->getType()
3565 << Arg->getSourceRange();
3566 S.Diag(Param->getLocation(), diag::note_template_param_here);
3567 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003568 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003569 }
3570 }
3571
3572 // At this point, the template argument refers to an object or
3573 // function with external linkage. We now need to check whether the
3574 // argument and parameter types are compatible.
3575 if (!S.Context.hasSameUnqualifiedType(ArgType,
3576 ParamType.getNonReferenceType())) {
3577 // We can't perform this conversion or binding.
3578 if (ParamType->isReferenceType())
3579 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003580 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003581 else
3582 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003583 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003584 S.Diag(Param->getLocation(), diag::note_template_param_here);
3585 return true;
3586 }
3587 }
3588
3589 // Create the template argument.
3590 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003591 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003592 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003593}
3594
3595/// \brief Checks whether the given template argument is a pointer to
3596/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003597bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003598 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003599 bool Invalid = false;
3600
3601 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003602 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003603 Arg = Cast->getSubExpr();
3604
3605 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003606 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003607 // A template-argument for a non-type, non-template
3608 // template-parameter shall be one of: [...]
3609 //
3610 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003611 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003612
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003613 // In C++98/03 mode, give an extension warning on any extra parentheses.
3614 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3615 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003616 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003617 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003618 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003619 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003620 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003621 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003622 }
3623
3624 Arg = Parens->getSubExpr();
3625 }
3626
John McCall91a57552011-07-15 05:09:51 +00003627 while (SubstNonTypeTemplateParmExpr *subst =
3628 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3629 Arg = subst->getReplacement()->IgnoreImpCasts();
3630
Douglas Gregorcaddba02009-11-12 18:38:13 +00003631 // A pointer-to-member constant written &Class::member.
3632 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003633 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003634 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3635 if (DRE && !DRE->getQualifier())
3636 DRE = 0;
3637 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003638 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003639 // A constant of pointer-to-member type.
3640 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3641 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3642 if (VD->getType()->isMemberPointerType()) {
3643 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003644 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003645 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3646 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003647 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003648 else
3649 Converted = TemplateArgument(VD->getCanonicalDecl());
3650 return Invalid;
3651 }
3652 }
3653 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003654
Douglas Gregorcaddba02009-11-12 18:38:13 +00003655 DRE = 0;
3656 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003657
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003658 if (!DRE)
3659 return Diag(Arg->getSourceRange().getBegin(),
3660 diag::err_template_arg_not_pointer_to_member_form)
3661 << Arg->getSourceRange();
3662
3663 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3664 assert((isa<FieldDecl>(DRE->getDecl()) ||
3665 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3666 "Only non-static member pointers can make it here");
3667
3668 // Okay: this is the address of a non-static member, and therefore
3669 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003670 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003671 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003672 else
3673 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003674 return Invalid;
3675 }
3676
3677 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003678 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003679 diag::err_template_arg_not_pointer_to_member_form)
3680 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003681 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003682 diag::note_template_arg_refers_here);
3683 return true;
3684}
3685
Douglas Gregorc15cb382009-02-09 23:23:08 +00003686/// \brief Check a template argument against its corresponding
3687/// non-type template parameter.
3688///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003689/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003690/// If an error occurred, it returns ExprError(); otherwise, it
3691/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003692/// InstantiatedParamType is the type of the non-type template
3693/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003694ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3695 QualType InstantiatedParamType, Expr *Arg,
3696 TemplateArgument &Converted,
3697 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003698 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3699
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003700 // If either the parameter has a dependent type or the argument is
3701 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003702 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3703 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003704 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003705 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003706 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003707
3708 // C++ [temp.arg.nontype]p5:
3709 // The following conversions are performed on each expression used
3710 // as a non-type template-argument. If a non-type
3711 // template-argument cannot be converted to the type of the
3712 // corresponding template-parameter then the program is
3713 // ill-formed.
3714 //
3715 // -- for a non-type template-parameter of integral or
3716 // enumeration type, integral promotions (4.5) and integral
3717 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003718 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003719 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003720 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003721 // C++ [temp.arg.nontype]p1:
3722 // A template-argument for a non-type, non-template
3723 // template-parameter shall be one of:
3724 //
3725 // -- an integral constant-expression of integral or enumeration
3726 // type; or
3727 // -- the name of a non-type template-parameter; or
3728 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003729 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003730 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003731 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003732 diag::err_template_arg_not_integral_or_enumeral)
3733 << ArgType << Arg->getSourceRange();
3734 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003735 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003736 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003737 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003738 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3739 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003740 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003741 }
3742
Douglas Gregor02024a92010-03-28 02:42:43 +00003743 // From here on out, all we care about are the unqualified forms
3744 // of the parameter and argument types.
3745 ParamType = ParamType.getUnqualifiedType();
3746 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003747
3748 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003749 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003750 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003751 } else if (CTAK == CTAK_Deduced) {
3752 // C++ [temp.deduct.type]p17:
3753 // If, in the declaration of a function template with a non-type
3754 // template-parameter, the non-type template- parameter is used
3755 // in an expression in the function parameter-list and, if the
3756 // corresponding template-argument is deduced, the
3757 // template-argument type shall match the type of the
3758 // template-parameter exactly, except that a template-argument
3759 // deduced from an array bound may be of any integral type.
3760 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3761 << ArgType << ParamType;
3762 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003763 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003764 } else if (ParamType->isBooleanType()) {
3765 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003766 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003767 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3768 !ParamType->isEnumeralType()) {
3769 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003770 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003771 } else {
3772 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003773 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003774 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003775 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003776 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003777 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003778 }
3779
Douglas Gregorc7469372011-05-04 21:55:00 +00003780 // Add the value of this argument to the list of converted
3781 // arguments. We use the bitwidth and signedness of the template
3782 // parameter.
3783 if (Arg->isValueDependent()) {
3784 // The argument is value-dependent. Create a new
3785 // TemplateArgument with the converted expression.
3786 Converted = TemplateArgument(Arg);
3787 return Owned(Arg);
3788 }
3789
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003790 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003791 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003792 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003793
Douglas Gregorc7469372011-05-04 21:55:00 +00003794 if (ParamType->isBooleanType()) {
3795 // Value must be zero or one.
3796 Value = Value != 0;
3797 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3798 if (Value.getBitWidth() != AllowedBits)
3799 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003800 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003801 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003802 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003803
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003804 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003805 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003806 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003807 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003808 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003809 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003810
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003811 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003812 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003813 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003814 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3815 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3816 << Arg->getSourceRange();
3817 Diag(Param->getLocation(), diag::note_template_param_here);
3818 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003819
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003820 // Complain if we overflowed the template parameter's type.
3821 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003822 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003823 RequiredBits = OldValue.getActiveBits();
3824 else if (OldValue.isUnsigned())
3825 RequiredBits = OldValue.getActiveBits() + 1;
3826 else
3827 RequiredBits = OldValue.getMinSignedBits();
3828 if (RequiredBits > AllowedBits) {
3829 Diag(Arg->getSourceRange().getBegin(),
3830 diag::warn_template_arg_too_large)
3831 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3832 << Arg->getSourceRange();
3833 Diag(Param->getLocation(), diag::note_template_param_here);
3834 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003835 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003836
John McCall833ca992009-10-29 08:12:44 +00003837 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003838 ParamType->isEnumeralType()
3839 ? Context.getCanonicalType(ParamType)
3840 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003841 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003842 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003843
John McCall6bb80172010-03-30 21:47:33 +00003844 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3845
Douglas Gregorb7a09262010-04-01 18:32:35 +00003846 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3847 // from a template argument of type std::nullptr_t to a non-type
3848 // template parameter of type pointer to object, pointer to
3849 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003850 if (ArgType->isNullPtrType()) {
3851 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3852 Converted = TemplateArgument((NamedDecl *)0);
3853 return Owned(Arg);
3854 }
3855
3856 if (ParamType->isNullPtrType()) {
3857 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3858 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3859 return Owned(Arg);
3860 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003861 }
3862
Douglas Gregorb86b0572009-02-11 01:18:59 +00003863 // Handle pointer-to-function, reference-to-function, and
3864 // pointer-to-member-function all in (roughly) the same way.
3865 if (// -- For a non-type template-parameter of type pointer to
3866 // function, only the function-to-pointer conversion (4.3) is
3867 // applied. If the template-argument represents a set of
3868 // overloaded functions (or a pointer to such), the matching
3869 // function is selected from the set (13.4).
3870 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003871 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003872 // -- For a non-type template-parameter of type reference to
3873 // function, no conversions apply. If the template-argument
3874 // represents a set of overloaded functions, the matching
3875 // function is selected from the set (13.4).
3876 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003877 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003878 // -- For a non-type template-parameter of type pointer to
3879 // member function, no conversions apply. If the
3880 // template-argument represents a set of overloaded member
3881 // functions, the matching member function is selected from
3882 // the set (13.4).
3883 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003884 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003885 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003886
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003887 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003888 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003889 true,
3890 FoundResult)) {
3891 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003892 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003893
3894 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3895 ArgType = Arg->getType();
3896 } else
John Wiegley429bb272011-04-08 18:41:53 +00003897 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003898 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899
John Wiegley429bb272011-04-08 18:41:53 +00003900 if (!ParamType->isMemberPointerType()) {
3901 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3902 ParamType,
3903 Arg, Converted))
3904 return ExprError();
3905 return Owned(Arg);
3906 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003907
John McCallf85e1932011-06-15 23:02:42 +00003908 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003909 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003910 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003911 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3912 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003913 } else if (!Context.hasSameUnqualifiedType(ArgType,
3914 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003915 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003916 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003917 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003918 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003919 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003920 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003921 }
Mike Stump1eb44332009-09-09 15:08:12 +00003922
John Wiegley429bb272011-04-08 18:41:53 +00003923 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3924 return ExprError();
3925 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003926 }
3927
Chris Lattnerfe90de72009-02-20 21:37:53 +00003928 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003929 // -- for a non-type template-parameter of type pointer to
3930 // object, qualification conversions (4.4) and the
3931 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003932 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003933 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003934 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003935
John Wiegley429bb272011-04-08 18:41:53 +00003936 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3937 ParamType,
3938 Arg, Converted))
3939 return ExprError();
3940 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003941 }
Mike Stump1eb44332009-09-09 15:08:12 +00003942
Ted Kremenek6217b802009-07-29 21:53:49 +00003943 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003944 // -- For a non-type template-parameter of type reference to
3945 // object, no conversions apply. The type referred to by the
3946 // reference may be more cv-qualified than the (otherwise
3947 // identical) type of the template-argument. The
3948 // template-parameter is bound directly to the
3949 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003950 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003951 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003952
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003953 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003954 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3955 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003956 true,
3957 FoundResult)) {
3958 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003959 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003960
3961 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3962 ArgType = Arg->getType();
3963 } else
John Wiegley429bb272011-04-08 18:41:53 +00003964 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003965 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003966
John Wiegley429bb272011-04-08 18:41:53 +00003967 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3968 ParamType,
3969 Arg, Converted))
3970 return ExprError();
3971 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003972 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003973
3974 // -- For a non-type template-parameter of type pointer to data
3975 // member, qualification conversions (4.4) are applied.
3976 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3977
John McCallf85e1932011-06-15 23:02:42 +00003978 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003979 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003980 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003981 } else if (IsQualificationConversion(ArgType, ParamType, false,
3982 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003983 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3984 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003985 } else {
3986 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003987 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003988 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003989 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003990 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003991 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003992 }
3993
John Wiegley429bb272011-04-08 18:41:53 +00003994 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3995 return ExprError();
3996 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003997}
3998
3999/// \brief Check a template argument against its corresponding
4000/// template template parameter.
4001///
4002/// This routine implements the semantics of C++ [temp.arg.template].
4003/// It returns true if an error occurred, and false otherwise.
4004bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004005 const TemplateArgumentLoc &Arg) {
4006 TemplateName Name = Arg.getArgument().getAsTemplate();
4007 TemplateDecl *Template = Name.getAsTemplateDecl();
4008 if (!Template) {
4009 // Any dependent template name is fine.
4010 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4011 return false;
4012 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004013
Richard Smith3e4c6c42011-05-05 21:57:07 +00004014 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004015 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004016 // the name of a class template or an alias template, expressed as an
4017 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004018 // primary class templates are considered when matching the
4019 // template template argument with the corresponding parameter;
4020 // partial specializations are not considered even if their
4021 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004022 //
4023 // Note that we also allow template template parameters here, which
4024 // will happen when we are dealing with, e.g., class template
4025 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004026 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004027 !isa<TemplateTemplateParmDecl>(Template) &&
4028 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004029 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004030 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004031 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004032 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004033 << Template;
4034 }
4035
4036 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4037 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004038 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004039 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004040 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004041}
4042
Douglas Gregor02024a92010-03-28 02:42:43 +00004043/// \brief Given a non-type template argument that refers to a
4044/// declaration and the type of its corresponding non-type template
4045/// parameter, produce an expression that properly refers to that
4046/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004047ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004048Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4049 QualType ParamType,
4050 SourceLocation Loc) {
4051 assert(Arg.getKind() == TemplateArgument::Declaration &&
4052 "Only declaration template arguments permitted here");
4053 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4054
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004055 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004056 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4057 // If the value is a class member, we might have a pointer-to-member.
4058 // Determine whether the non-type template template parameter is of
4059 // pointer-to-member type. If so, we need to build an appropriate
4060 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4061 // would refer to the member itself.
4062 if (ParamType->isMemberPointerType()) {
4063 QualType ClassType
4064 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4065 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004066 = NestedNameSpecifier::Create(Context, 0, false,
4067 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004068 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004069 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004070
4071 // The actual value-ness of this is unimportant, but for
4072 // internal consistency's sake, references to instance methods
4073 // are r-values.
4074 ExprValueKind VK = VK_LValue;
4075 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4076 VK = VK_RValue;
4077
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004078 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004079 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004080 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004081 Loc,
4082 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004083 if (RefExpr.isInvalid())
4084 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004085
John McCall2de56d12010-08-25 11:45:40 +00004086 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004087
Douglas Gregorc0c83002010-04-30 21:46:38 +00004088 // We might need to perform a trailing qualification conversion, since
4089 // the element type on the parameter could be more qualified than the
4090 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004091 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004092 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004093 ParamType.getUnqualifiedType(), false,
4094 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004095 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004096
Douglas Gregor02024a92010-03-28 02:42:43 +00004097 assert(!RefExpr.isInvalid() &&
4098 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004099 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004100 return move(RefExpr);
4101 }
4102 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004103
Douglas Gregor02024a92010-03-28 02:42:43 +00004104 QualType T = VD->getType().getNonReferenceType();
4105 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004106 // When the non-type template parameter is a pointer, take the
4107 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004108 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004109 if (RefExpr.isInvalid())
4110 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004111
4112 if (T->isFunctionType() || T->isArrayType()) {
4113 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004114 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4115 if (RefExpr.isInvalid())
4116 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004117
4118 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004119 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004120
Douglas Gregorb7a09262010-04-01 18:32:35 +00004121 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004122 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004123 }
4124
John McCallf89e55a2010-11-18 06:31:45 +00004125 ExprValueKind VK = VK_RValue;
4126
Douglas Gregor02024a92010-03-28 02:42:43 +00004127 // If the non-type template parameter has reference type, qualify the
4128 // resulting declaration reference with the extra qualifiers on the
4129 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004130 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4131 VK = VK_LValue;
4132 T = Context.getQualifiedType(T,
4133 TargetRef->getPointeeType().getQualifiers());
4134 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004135
John McCallf89e55a2010-11-18 06:31:45 +00004136 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004137}
4138
4139/// \brief Construct a new expression that refers to the given
4140/// integral template argument with the given source-location
4141/// information.
4142///
4143/// This routine takes care of the mapping from an integral template
4144/// argument (which may have any integral type) to the appropriate
4145/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004146ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004147Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4148 SourceLocation Loc) {
4149 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004150 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004151 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004152 if (T->isAnyCharacterType()) {
4153 CharacterLiteral::CharacterKind Kind;
4154 if (T->isWideCharType())
4155 Kind = CharacterLiteral::Wide;
4156 else if (T->isChar16Type())
4157 Kind = CharacterLiteral::UTF16;
4158 else if (T->isChar32Type())
4159 Kind = CharacterLiteral::UTF32;
4160 else
4161 Kind = CharacterLiteral::Ascii;
4162
Douglas Gregor02024a92010-03-28 02:42:43 +00004163 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004164 Arg.getAsIntegral()->getZExtValue(),
4165 Kind, T, Loc));
4166 }
4167
Douglas Gregor02024a92010-03-28 02:42:43 +00004168 if (T->isBooleanType())
4169 return Owned(new (Context) CXXBoolLiteralExpr(
4170 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004171 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004172
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004173 if (T->isNullPtrType())
4174 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4175
Chris Lattner223de242011-04-25 20:37:58 +00004176 // If this is an enum type that we're instantiating, we need to use an integer
4177 // type the same size as the enumerator. We don't want to build an
4178 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004179 QualType BT;
4180 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004181 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004182 else
4183 BT = T;
4184
John McCall4e9272d2011-07-15 07:47:58 +00004185 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4186 if (T->isEnumeralType()) {
4187 // FIXME: This is a hack. We need a better way to handle substituted
4188 // non-type template parameters.
4189 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4190 Context.getTrivialTypeSourceInfo(T, Loc),
4191 Loc, Loc);
4192 }
4193
4194 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004195}
4196
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004197/// \brief Match two template parameters within template parameter lists.
4198static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4199 bool Complain,
4200 Sema::TemplateParameterListEqualKind Kind,
4201 SourceLocation TemplateArgLoc) {
4202 // Check the actual kind (type, non-type, template).
4203 if (Old->getKind() != New->getKind()) {
4204 if (Complain) {
4205 unsigned NextDiag = diag::err_template_param_different_kind;
4206 if (TemplateArgLoc.isValid()) {
4207 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4208 NextDiag = diag::note_template_param_different_kind;
4209 }
4210 S.Diag(New->getLocation(), NextDiag)
4211 << (Kind != Sema::TPL_TemplateMatch);
4212 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4213 << (Kind != Sema::TPL_TemplateMatch);
4214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004215
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004216 return false;
4217 }
4218
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004219 // Check that both are parameter packs are neither are parameter packs.
4220 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004221 // template template parameter, the template template parameter can have
4222 // a parameter pack where the template template argument does not.
4223 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4224 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4225 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004226 if (Complain) {
4227 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4228 if (TemplateArgLoc.isValid()) {
4229 S.Diag(TemplateArgLoc,
4230 diag::err_template_arg_template_params_mismatch);
4231 NextDiag = diag::note_template_parameter_pack_non_pack;
4232 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004233
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004234 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4235 : isa<NonTypeTemplateParmDecl>(New)? 1
4236 : 2;
4237 S.Diag(New->getLocation(), NextDiag)
4238 << ParamKind << New->isParameterPack();
4239 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4240 << ParamKind << Old->isParameterPack();
4241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004242
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004243 return false;
4244 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004246 // For non-type template parameters, check the type of the parameter.
4247 if (NonTypeTemplateParmDecl *OldNTTP
4248 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4249 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004250
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004251 // If we are matching a template template argument to a template
4252 // template parameter and one of the non-type template parameter types
4253 // is dependent, then we must wait until template instantiation time
4254 // to actually compare the arguments.
4255 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4256 (OldNTTP->getType()->isDependentType() ||
4257 NewNTTP->getType()->isDependentType()))
4258 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004259
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004260 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4261 if (Complain) {
4262 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4263 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004264 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004265 diag::err_template_arg_template_params_mismatch);
4266 NextDiag = diag::note_template_nontype_parm_different_type;
4267 }
4268 S.Diag(NewNTTP->getLocation(), NextDiag)
4269 << NewNTTP->getType()
4270 << (Kind != Sema::TPL_TemplateMatch);
4271 S.Diag(OldNTTP->getLocation(),
4272 diag::note_template_nontype_parm_prev_declaration)
4273 << OldNTTP->getType();
4274 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004276 return false;
4277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004279 return true;
4280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004282 // For template template parameters, check the template parameter types.
4283 // The template parameter lists of template template
4284 // parameters must agree.
4285 if (TemplateTemplateParmDecl *OldTTP
4286 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004288 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4289 OldTTP->getTemplateParameters(),
4290 Complain,
4291 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004292 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004293 : Kind),
4294 TemplateArgLoc);
4295 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004296
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004297 return true;
4298}
Douglas Gregor02024a92010-03-28 02:42:43 +00004299
Douglas Gregora0347822011-01-13 00:08:50 +00004300/// \brief Diagnose a known arity mismatch when comparing template argument
4301/// lists.
4302static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004303void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004304 TemplateParameterList *New,
4305 TemplateParameterList *Old,
4306 Sema::TemplateParameterListEqualKind Kind,
4307 SourceLocation TemplateArgLoc) {
4308 unsigned NextDiag = diag::err_template_param_list_different_arity;
4309 if (TemplateArgLoc.isValid()) {
4310 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4311 NextDiag = diag::note_template_param_list_different_arity;
4312 }
4313 S.Diag(New->getTemplateLoc(), NextDiag)
4314 << (New->size() > Old->size())
4315 << (Kind != Sema::TPL_TemplateMatch)
4316 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4317 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4318 << (Kind != Sema::TPL_TemplateMatch)
4319 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4320}
4321
Douglas Gregorddc29e12009-02-06 22:42:48 +00004322/// \brief Determine whether the given template parameter lists are
4323/// equivalent.
4324///
Mike Stump1eb44332009-09-09 15:08:12 +00004325/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004326/// source code as part of a new template declaration.
4327///
4328/// \param Old The old template parameter list, typically found via
4329/// name lookup of the template declared with this template parameter
4330/// list.
4331///
4332/// \param Complain If true, this routine will produce a diagnostic if
4333/// the template parameter lists are not equivalent.
4334///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004335/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004336///
4337/// \param TemplateArgLoc If this source location is valid, then we
4338/// are actually checking the template parameter list of a template
4339/// argument (New) against the template parameter list of its
4340/// corresponding template template parameter (Old). We produce
4341/// slightly different diagnostics in this scenario.
4342///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004343/// \returns True if the template parameter lists are equal, false
4344/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004345bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004346Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4347 TemplateParameterList *Old,
4348 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004349 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004350 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004351 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4352 if (Complain)
4353 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4354 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004355
4356 return false;
4357 }
4358
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004359 // C++0x [temp.arg.template]p3:
4360 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004361 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004362 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004363 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004364 // template-parameter-list of P. [...]
4365 TemplateParameterList::iterator NewParm = New->begin();
4366 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004367 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004368 OldParmEnd = Old->end();
4369 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004370 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4371 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004372 if (NewParm == NewParmEnd) {
4373 if (Complain)
4374 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4375 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004376
Douglas Gregora0347822011-01-13 00:08:50 +00004377 return false;
4378 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004379
Douglas Gregora0347822011-01-13 00:08:50 +00004380 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4381 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382 return false;
4383
Douglas Gregora0347822011-01-13 00:08:50 +00004384 ++NewParm;
4385 continue;
4386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004387
Douglas Gregora0347822011-01-13 00:08:50 +00004388 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004389 // [...] When P's template- parameter-list contains a template parameter
4390 // pack (14.5.3), the template parameter pack will match zero or more
4391 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004392 // template-parameter-list of A with the same type and form as the
4393 // template parameter pack in P (ignoring whether those template
4394 // parameters are template parameter packs).
4395 for (; NewParm != NewParmEnd; ++NewParm) {
4396 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4397 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004398 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004399 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004400 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004401
Douglas Gregora0347822011-01-13 00:08:50 +00004402 // Make sure we exhausted all of the arguments.
4403 if (NewParm != NewParmEnd) {
4404 if (Complain)
4405 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4406 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004407
Douglas Gregora0347822011-01-13 00:08:50 +00004408 return false;
4409 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410
Douglas Gregorddc29e12009-02-06 22:42:48 +00004411 return true;
4412}
4413
4414/// \brief Check whether a template can be declared within this scope.
4415///
4416/// If the template declaration is valid in this scope, returns
4417/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004418bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004419Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004420 // Find the nearest enclosing declaration scope.
4421 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4422 (S->getFlags() & Scope::TemplateParamScope) != 0)
4423 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004424
Douglas Gregorddc29e12009-02-06 22:42:48 +00004425 // C++ [temp]p2:
4426 // A template-declaration can appear only as a namespace scope or
4427 // class scope declaration.
4428 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004429 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4430 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004431 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004432 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004433
Eli Friedman1503f772009-07-31 01:43:05 +00004434 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004435 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004436
4437 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4438 return false;
4439
Mike Stump1eb44332009-09-09 15:08:12 +00004440 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004441 diag::err_template_outside_namespace_or_class_scope)
4442 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004443}
Douglas Gregorcc636682009-02-17 23:15:12 +00004444
Douglas Gregord5cb8762009-10-07 00:13:32 +00004445/// \brief Determine what kind of template specialization the given declaration
4446/// is.
4447static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4448 if (!D)
4449 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004450
Douglas Gregorf6b11852009-10-08 15:14:33 +00004451 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4452 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004453 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4454 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004455 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4456 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004457
Douglas Gregord5cb8762009-10-07 00:13:32 +00004458 return TSK_Undeclared;
4459}
4460
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004461/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004462/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004463///
Douglas Gregor9302da62009-10-14 23:50:59 +00004464/// This routine determines whether a template specialization can be declared
4465/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004466///
4467/// \param S the semantic analysis object for which this check is being
4468/// performed.
4469///
4470/// \param Specialized the entity being specialized or instantiated, which
4471/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004472/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004473/// member class).
4474///
4475/// \param PrevDecl the previous declaration of this entity, if any.
4476///
4477/// \param Loc the location of the explicit specialization or instantiation of
4478/// this entity.
4479///
4480/// \param IsPartialSpecialization whether this is a partial specialization of
4481/// a class template.
4482///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004483/// \returns true if there was an error that we cannot recover from, false
4484/// otherwise.
4485static bool CheckTemplateSpecializationScope(Sema &S,
4486 NamedDecl *Specialized,
4487 NamedDecl *PrevDecl,
4488 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004489 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004490 // Keep these "kind" numbers in sync with the %select statements in the
4491 // various diagnostics emitted by this routine.
4492 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004493 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004494 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004495 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004496 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004497 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004498 EntityKind = 3;
4499 else if (isa<VarDecl>(Specialized))
4500 EntityKind = 4;
4501 else if (isa<RecordDecl>(Specialized))
4502 EntityKind = 5;
4503 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004504 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4505 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004506 return true;
4507 }
4508
Douglas Gregor88b70942009-02-25 22:02:03 +00004509 // C++ [temp.expl.spec]p2:
4510 // An explicit specialization shall be declared in the namespace
4511 // of which the template is a member, or, for member templates, in
4512 // the namespace of which the enclosing class or enclosing class
4513 // template is a member. An explicit specialization of a member
4514 // function, member class or static data member of a class
4515 // template shall be declared in the namespace of which the class
4516 // template is a member. Such a declaration may also be a
4517 // definition. If the declaration is not a definition, the
4518 // specialization may be defined later in the name- space in which
4519 // the explicit specialization was declared, or in a namespace
4520 // that encloses the one in which the explicit specialization was
4521 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004522 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004523 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004524 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004525 return true;
4526 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004527
Douglas Gregor0a407472009-10-07 17:30:37 +00004528 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004529 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004530 // Do not warn for class scope explicit specialization during
4531 // instantiation, warning was already emitted during pattern
4532 // semantic analysis.
4533 if (!S.ActiveTemplateInstantiations.size())
4534 S.Diag(Loc, diag::ext_function_specialization_in_class)
4535 << Specialized;
4536 } else {
4537 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4538 << Specialized;
4539 return true;
4540 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004541 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004542
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004543 // C++ [temp.class.spec]p6:
4544 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004545 // in any namespace scope in which its definition may be defined (14.5.1
4546 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004547 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004548 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004549 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004550 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004551 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004552 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4553 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004554 // C++ [temp.exp.spec]p2:
4555 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004556 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004557 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004558 // An explicit specialization of a member function, member class or
4559 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004560 // namespace of which the class template is a member.
4561 //
4562 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004563 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004564 // the specialized template.
4565 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4566 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004567 bool IsCPlusPlus0xExtension
4568 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004569 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004570 S.Diag(Loc, IsCPlusPlus0xExtension
4571 ? diag::ext_template_spec_decl_out_of_scope_global
4572 : diag::err_template_spec_decl_out_of_scope_global)
4573 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004574 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004575 S.Diag(Loc, IsCPlusPlus0xExtension
4576 ? diag::ext_template_spec_decl_out_of_scope
4577 : diag::err_template_spec_decl_out_of_scope)
4578 << EntityKind << Specialized
4579 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004580
Douglas Gregor9302da62009-10-14 23:50:59 +00004581 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4582 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004583 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004584 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004585
4586 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004587 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004589 // specializations of function templates, static data members, and member
4590 // functions, so we skip the check here for those kinds of entities.
4591 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004592 // Should we refactor that check, so that it occurs later?
4593 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004594 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4595 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004596 if (isa<TranslationUnitDecl>(SpecializedContext))
4597 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4598 << EntityKind << Specialized;
4599 else if (isa<NamespaceDecl>(SpecializedContext))
4600 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4601 << EntityKind << Specialized
4602 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004603
Douglas Gregor9302da62009-10-14 23:50:59 +00004604 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004605 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606
Douglas Gregord5cb8762009-10-07 00:13:32 +00004607 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004608
Douglas Gregor88b70942009-02-25 22:02:03 +00004609 return false;
4610}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004611
Douglas Gregorbacb9492011-01-03 21:13:47 +00004612/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4613/// that checks non-type template partial specialization arguments.
4614static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4615 NonTypeTemplateParmDecl *Param,
4616 const TemplateArgument *Args,
4617 unsigned NumArgs) {
4618 for (unsigned I = 0; I != NumArgs; ++I) {
4619 if (Args[I].getKind() == TemplateArgument::Pack) {
4620 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004621 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004622 Args[I].pack_size()))
4623 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004624
Douglas Gregore94866f2009-06-12 21:21:02 +00004625 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004626 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004627
Douglas Gregorbacb9492011-01-03 21:13:47 +00004628 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004629 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004630 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004631 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004632
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004633 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004634 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4635 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004636
4637 // Strip off any implicit casts we added as part of type checking.
4638 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4639 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004640
Douglas Gregore94866f2009-06-12 21:21:02 +00004641 // C++ [temp.class.spec]p8:
4642 // A non-type argument is non-specialized if it is the name of a
4643 // non-type parameter. All other non-type arguments are
4644 // specialized.
4645 //
4646 // Below, we check the two conditions that only apply to
4647 // specialized non-type arguments, so skip any non-specialized
4648 // arguments.
4649 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004650 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004651 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652
Douglas Gregore94866f2009-06-12 21:21:02 +00004653 // C++ [temp.class.spec]p9:
4654 // Within the argument list of a class template partial
4655 // specialization, the following restrictions apply:
4656 // -- A partially specialized non-type argument expression
4657 // shall not involve a template parameter of the partial
4658 // specialization except when the argument expression is a
4659 // simple identifier.
4660 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004661 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004662 diag::err_dependent_non_type_arg_in_partial_spec)
4663 << ArgExpr->getSourceRange();
4664 return true;
4665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004666
Douglas Gregore94866f2009-06-12 21:21:02 +00004667 // -- The type of a template parameter corresponding to a
4668 // specialized non-type argument shall not be dependent on a
4669 // parameter of the specialization.
4670 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004671 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004672 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4673 << Param->getType()
4674 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004675 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004676 return true;
4677 }
4678 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004679
Douglas Gregorbacb9492011-01-03 21:13:47 +00004680 return false;
4681}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004682
Douglas Gregorbacb9492011-01-03 21:13:47 +00004683/// \brief Check the non-type template arguments of a class template
4684/// partial specialization according to C++ [temp.class.spec]p9.
4685///
4686/// \param TemplateParams the template parameters of the primary class
4687/// template.
4688///
4689/// \param TemplateArg the template arguments of the class template
4690/// partial specialization.
4691///
4692/// \returns true if there was an error, false otherwise.
4693static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4694 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004695 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004696 const TemplateArgument *ArgList = TemplateArgs.data();
4697
4698 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4699 NonTypeTemplateParmDecl *Param
4700 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4701 if (!Param)
4702 continue;
4703
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004704 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004705 &ArgList[I], 1))
4706 return true;
4707 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004708
4709 return false;
4710}
4711
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004712/// \brief Retrieve the previous declaration of the given declaration.
4713static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4714 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4715 return VD->getPreviousDeclaration();
4716 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4717 return FD->getPreviousDeclaration();
4718 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4719 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004720 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004721 return TD->getPreviousDeclaration();
4722 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4723 return FTD->getPreviousDeclaration();
4724 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4725 return CTD->getPreviousDeclaration();
4726 return 0;
4727}
4728
John McCalld226f652010-08-21 09:40:31 +00004729DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004730Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4731 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004732 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004733 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004734 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004735 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004736 SourceLocation TemplateNameLoc,
4737 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004738 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004739 SourceLocation RAngleLoc,
4740 AttributeList *Attr,
4741 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004742 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004743
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004744 // NOTE: KWLoc is the location of the tag keyword. This will instead
4745 // store the location of the outermost template keyword in the declaration.
4746 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4747 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4748
Douglas Gregorcc636682009-02-17 23:15:12 +00004749 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004750 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004751 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004752 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4753
4754 if (!ClassTemplate) {
4755 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004756 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004757 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4758 return true;
4759 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004760
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004761 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004762 bool isPartialSpecialization = false;
4763
Douglas Gregor88b70942009-02-25 22:02:03 +00004764 // Check the validity of the template headers that introduce this
4765 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004766 // FIXME: We probably shouldn't complain about these headers for
4767 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004768 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004769 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004770 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4771 TemplateNameLoc,
4772 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004773 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004774 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004775 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004776 isExplicitSpecialization,
4777 Invalid);
4778 if (Invalid)
4779 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004780
Douglas Gregor05396e22009-08-25 17:23:04 +00004781 if (TemplateParams && TemplateParams->size() > 0) {
4782 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004783
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004784 if (TUK == TUK_Friend) {
4785 Diag(KWLoc, diag::err_partial_specialization_friend)
4786 << SourceRange(LAngleLoc, RAngleLoc);
4787 return true;
4788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004789
Douglas Gregor05396e22009-08-25 17:23:04 +00004790 // C++ [temp.class.spec]p10:
4791 // The template parameter list of a specialization shall not
4792 // contain default template argument values.
4793 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4794 Decl *Param = TemplateParams->getParam(I);
4795 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4796 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004797 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004798 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004799 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004800 }
4801 } else if (NonTypeTemplateParmDecl *NTTP
4802 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4803 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004804 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004805 diag::err_default_arg_in_partial_spec)
4806 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004807 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004808 }
4809 } else {
4810 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004811 if (TTP->hasDefaultArgument()) {
4812 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004813 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004814 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004815 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004816 }
4817 }
4818 }
Douglas Gregora735b202009-10-13 14:39:41 +00004819 } else if (TemplateParams) {
4820 if (TUK == TUK_Friend)
4821 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004822 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004823 SourceRange(TemplateParams->getTemplateLoc(),
4824 TemplateParams->getRAngleLoc()))
4825 << SourceRange(LAngleLoc, RAngleLoc);
4826 else
4827 isExplicitSpecialization = true;
4828 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004829 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004830 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004831 isExplicitSpecialization = true;
4832 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004833
Douglas Gregorcc636682009-02-17 23:15:12 +00004834 // Check that the specialization uses the same tag kind as the
4835 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004836 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4837 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004838 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004839 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004840 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004841 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004842 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004843 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004844 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004845 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004846 diag::note_previous_use);
4847 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4848 }
4849
Douglas Gregor40808ce2009-03-09 23:48:35 +00004850 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004851 TemplateArgumentListInfo TemplateArgs;
4852 TemplateArgs.setLAngleLoc(LAngleLoc);
4853 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004854 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004855
Douglas Gregor925910d2011-01-03 20:35:03 +00004856 // Check for unexpanded parameter packs in any of the template arguments.
4857 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004858 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004859 UPPC_PartialSpecialization))
4860 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004861
Douglas Gregorcc636682009-02-17 23:15:12 +00004862 // Check that the template argument list is well-formed for this
4863 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004864 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004865 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4866 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004867 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004868
Douglas Gregor910f8002010-11-07 23:05:16 +00004869 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004870 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004871
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004872 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004873 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004874 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004875 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004876 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004877 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004878 return true;
4879
Douglas Gregor561f8122011-07-01 01:22:09 +00004880 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004881 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004882 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004883 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004884 TemplateArgs.size(),
4885 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004886 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4887 << ClassTemplate->getDeclName();
4888 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004889 }
4890 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004891
Douglas Gregorcc636682009-02-17 23:15:12 +00004892 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004893 ClassTemplateSpecializationDecl *PrevDecl = 0;
4894
4895 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004896 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004897 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004898 = ClassTemplate->findPartialSpecialization(Converted.data(),
4899 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004900 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004901 else
4902 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004903 = ClassTemplate->findSpecialization(Converted.data(),
4904 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004905
4906 ClassTemplateSpecializationDecl *Specialization = 0;
4907
Douglas Gregor88b70942009-02-25 22:02:03 +00004908 // Check whether we can declare a class template specialization in
4909 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004910 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004911 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4912 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004913 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004914 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004915
Douglas Gregorb88e8882009-07-30 17:40:51 +00004916 // The canonical type
4917 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004919 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004920 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004921 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004922 // arguments was referenced but not declared, or we're only
4923 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004924 // declaration node as our own, updating its source location and
4925 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004926 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004927 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004928 if (TemplateParameterLists.size() > 0) {
4929 Specialization->setTemplateParameterListsInfo(Context,
4930 TemplateParameterLists.size(),
4931 (TemplateParameterList**) TemplateParameterLists.release());
4932 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004933 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004934 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004935 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004936 // Build the canonical type that describes the converted template
4937 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004938 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4939 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004940 Converted.data(),
4941 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942
4943 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004944 ClassTemplate->getInjectedClassNameSpecialization())) {
4945 // C++ [temp.class.spec]p9b3:
4946 //
4947 // -- The argument list of the specialization shall not be identical
4948 // to the implicit argument list of the primary template.
4949 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004950 << (TUK == TUK_Definition)
4951 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004952 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4953 ClassTemplate->getIdentifier(),
4954 TemplateNameLoc,
4955 Attr,
4956 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00004957 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004958 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004959 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004960 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004961
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004962 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004963 ClassTemplatePartialSpecializationDecl *PrevPartial
4964 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004965 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004966 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004967 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004968 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004969 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004970 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004971 TemplateParams,
4972 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004973 Converted.data(),
4974 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004975 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004976 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004977 PrevPartial,
4978 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004979 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004980 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004981 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004982 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004983 (TemplateParameterList**) TemplateParameterLists.release());
4984 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004985
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004986 if (!PrevPartial)
4987 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004988 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004989
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004991 // template specialization, make a note of that.
4992 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4993 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004994
Douglas Gregor031a5882009-06-13 00:26:55 +00004995 // Check that all of the template parameters of the class template
4996 // partial specialization are deducible from the template
4997 // arguments. If not, this class template partial specialization
4998 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004999 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005000 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005001 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005002 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005003 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005004 unsigned NumNonDeducible = 0;
5005 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5006 if (!DeducibleParams[I])
5007 ++NumNonDeducible;
5008
5009 if (NumNonDeducible) {
5010 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5011 << (NumNonDeducible > 1)
5012 << SourceRange(TemplateNameLoc, RAngleLoc);
5013 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5014 if (!DeducibleParams[I]) {
5015 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5016 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005017 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005018 diag::note_partial_spec_unused_parameter)
5019 << Param->getDeclName();
5020 else
Mike Stump1eb44332009-09-09 15:08:12 +00005021 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005022 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005023 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005024 }
5025 }
5026 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005027 } else {
5028 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005029 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005030 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005031 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005032 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005033 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005034 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005035 Converted.data(),
5036 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005037 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005038 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005039 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005040 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005041 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005042 (TemplateParameterList**) TemplateParameterLists.release());
5043 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005044
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005045 if (!PrevDecl)
5046 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005047
5048 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005049 }
5050
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005051 // C++ [temp.expl.spec]p6:
5052 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005054 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005056 // use occurs; no diagnostic is required.
5057 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005058 bool Okay = false;
5059 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5060 // Is there any previous explicit specialization declaration?
5061 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5062 Okay = true;
5063 break;
5064 }
5065 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005066
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005067 if (!Okay) {
5068 SourceRange Range(TemplateNameLoc, RAngleLoc);
5069 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5070 << Context.getTypeDeclType(Specialization) << Range;
5071
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005072 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005073 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005074 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005075 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005076 return true;
5077 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005079
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005080 // If this is not a friend, note that this is an explicit specialization.
5081 if (TUK != TUK_Friend)
5082 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005083
5084 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005085 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005086 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005087 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005088 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005089 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005090 Diag(Def->getLocation(), diag::note_previous_definition);
5091 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005092 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005093 }
5094 }
5095
John McCall7f1b9872010-12-18 03:30:47 +00005096 if (Attr)
5097 ProcessDeclAttributeList(S, Specialization, Attr);
5098
Douglas Gregord023aec2011-09-09 20:53:38 +00005099 if (ModulePrivateLoc.isValid())
5100 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5101 << (isPartialSpecialization? 1 : 0)
5102 << FixItHint::CreateRemoval(ModulePrivateLoc);
5103
Douglas Gregorfc705b82009-02-26 22:19:44 +00005104 // Build the fully-sugared type for this class template
5105 // specialization as the user wrote in the specialization
5106 // itself. This means that we'll pretty-print the type retrieved
5107 // from the specialization's declaration the way that the user
5108 // actually wrote the specialization, rather than formatting the
5109 // name based on the "canonical" representation used to store the
5110 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005111 TypeSourceInfo *WrittenTy
5112 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5113 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005114 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005115 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005116 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005117 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005118 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005119
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005120 // C++ [temp.expl.spec]p9:
5121 // A template explicit specialization is in the scope of the
5122 // namespace in which the template was defined.
5123 //
5124 // We actually implement this paragraph where we set the semantic
5125 // context (in the creation of the ClassTemplateSpecializationDecl),
5126 // but we also maintain the lexical context where the actual
5127 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005128 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005129
Douglas Gregorcc636682009-02-17 23:15:12 +00005130 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005131 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005132 Specialization->startDefinition();
5133
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005134 if (TUK == TUK_Friend) {
5135 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5136 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005137 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005138 /*FIXME:*/KWLoc);
5139 Friend->setAccess(AS_public);
5140 CurContext->addDecl(Friend);
5141 } else {
5142 // Add the specialization into its lexical context, so that it can
5143 // be seen when iterating through the list of declarations in that
5144 // context. However, specializations are not found by name lookup.
5145 CurContext->addDecl(Specialization);
5146 }
John McCalld226f652010-08-21 09:40:31 +00005147 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005148}
Douglas Gregord57959a2009-03-27 23:10:48 +00005149
John McCalld226f652010-08-21 09:40:31 +00005150Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005151 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005152 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005153 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5154}
5155
John McCalld226f652010-08-21 09:40:31 +00005156Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005157 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005158 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005159 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005160 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005161
Douglas Gregor52591bf2009-06-24 00:54:41 +00005162 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005163 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005164 }
Mike Stump1eb44332009-09-09 15:08:12 +00005165
Douglas Gregor52591bf2009-06-24 00:54:41 +00005166 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005167
John McCalld226f652010-08-21 09:40:31 +00005168 Decl *DP = HandleDeclarator(ParentScope, D,
5169 move(TemplateParameterLists),
5170 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005171 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005172 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005173 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005174 FunctionTemplate->getTemplatedDecl());
5175 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5176 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5177 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005178}
5179
John McCall75042392010-02-11 01:33:53 +00005180/// \brief Strips various properties off an implicit instantiation
5181/// that has just been explicitly specialized.
5182static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005183 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005184
5185 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5186 FD->setInlineSpecified(false);
5187 }
5188}
5189
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005190/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005191/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005192/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005193/// new specialization/instantiation will have any effect.
5194///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005195/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005196/// instantiation.
5197///
5198/// \param NewTSK the kind of the new explicit specialization or instantiation.
5199///
5200/// \param PrevDecl the previous declaration of the entity.
5201///
5202/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5203///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005205/// declaration was instantiated (either implicitly or explicitly).
5206///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005207/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005208/// specialization or instantiation has no effect and should be ignored.
5209///
5210/// \returns true if there was an error that should prevent the introduction of
5211/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005212bool
5213Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5214 TemplateSpecializationKind NewTSK,
5215 NamedDecl *PrevDecl,
5216 TemplateSpecializationKind PrevTSK,
5217 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005218 bool &HasNoEffect) {
5219 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220
Douglas Gregor454885e2009-10-15 15:54:05 +00005221 switch (NewTSK) {
5222 case TSK_Undeclared:
5223 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005224 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225
Douglas Gregor454885e2009-10-15 15:54:05 +00005226 case TSK_ExplicitSpecialization:
5227 switch (PrevTSK) {
5228 case TSK_Undeclared:
5229 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005230 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005231 // explicitly specialized or has merely been mentioned without any
5232 // instantiation.
5233 return false;
5234
5235 case TSK_ImplicitInstantiation:
5236 if (PrevPointOfInstantiation.isInvalid()) {
5237 // The declaration itself has not actually been instantiated, so it is
5238 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005239 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005240 return false;
5241 }
5242 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005243
Douglas Gregor454885e2009-10-15 15:54:05 +00005244 case TSK_ExplicitInstantiationDeclaration:
5245 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005246 assert((PrevTSK == TSK_ImplicitInstantiation ||
5247 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005248 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249
Douglas Gregor454885e2009-10-15 15:54:05 +00005250 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005252 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005254 // implicit instantiation to take place, in every translation unit in
5255 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005256 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5257 // Is there any previous explicit specialization declaration?
5258 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5259 return false;
5260 }
5261
Douglas Gregor0d035142009-10-27 18:42:08 +00005262 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005263 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005264 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005265 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266
Douglas Gregor454885e2009-10-15 15:54:05 +00005267 return true;
5268 }
5269 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005270
Douglas Gregor454885e2009-10-15 15:54:05 +00005271 case TSK_ExplicitInstantiationDeclaration:
5272 switch (PrevTSK) {
5273 case TSK_ExplicitInstantiationDeclaration:
5274 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005275 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005276 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005277
Douglas Gregor454885e2009-10-15 15:54:05 +00005278 case TSK_Undeclared:
5279 case TSK_ImplicitInstantiation:
5280 // We're explicitly instantiating something that may have already been
5281 // implicitly instantiated; that's fine.
5282 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005283
Douglas Gregor454885e2009-10-15 15:54:05 +00005284 case TSK_ExplicitSpecialization:
5285 // C++0x [temp.explicit]p4:
5286 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005287 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005288 // specialization for that template, the explicit instantiation has no
5289 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005290 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005291 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292
Douglas Gregor454885e2009-10-15 15:54:05 +00005293 case TSK_ExplicitInstantiationDefinition:
5294 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005295 // If an entity is the subject of both an explicit instantiation
5296 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005297 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005298 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005299 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005301 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 assert(PrevPointOfInstantiation.isValid() &&
5303 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005304 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005305 return false;
5306 }
5307 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308
Douglas Gregor454885e2009-10-15 15:54:05 +00005309 case TSK_ExplicitInstantiationDefinition:
5310 switch (PrevTSK) {
5311 case TSK_Undeclared:
5312 case TSK_ImplicitInstantiation:
5313 // We're explicitly instantiating something that may have already been
5314 // implicitly instantiated; that's fine.
5315 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005316
Douglas Gregor454885e2009-10-15 15:54:05 +00005317 case TSK_ExplicitSpecialization:
5318 // C++ DR 259, C++0x [temp.explicit]p4:
5319 // For a given set of template parameters, if an explicit
5320 // instantiation of a template appears after a declaration of
5321 // an explicit specialization for that template, the explicit
5322 // instantiation has no effect.
5323 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005324 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005325 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005326 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005327 if (!getLangOptions().CPlusPlus0x) {
5328 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005329 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005330 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005331 diag::note_previous_template_specialization);
5332 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005333 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005334 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005335
Douglas Gregor454885e2009-10-15 15:54:05 +00005336 case TSK_ExplicitInstantiationDeclaration:
5337 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005338 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005339 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregor454885e2009-10-15 15:54:05 +00005341 case TSK_ExplicitInstantiationDefinition:
5342 // C++0x [temp.spec]p5:
5343 // For a given template and a given set of template-arguments,
5344 // - an explicit instantiation definition shall appear at most once
5345 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005346 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005347 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005348 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005349 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005350 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005351 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005352 }
5353 break;
5354 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005355
David Blaikieb219cfc2011-09-23 05:06:16 +00005356 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005357}
5358
John McCallaf2094e2010-04-08 09:05:18 +00005359/// \brief Perform semantic analysis for the given dependent function
5360/// template specialization. The only possible way to get a dependent
5361/// function template specialization is with a friend declaration,
5362/// like so:
5363///
5364/// template <class T> void foo(T);
5365/// template <class T> class A {
5366/// friend void foo<>(T);
5367/// };
5368///
5369/// There really isn't any useful analysis we can do here, so we
5370/// just store the information.
5371bool
5372Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5373 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5374 LookupResult &Previous) {
5375 // Remove anything from Previous that isn't a function template in
5376 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005377 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005378 LookupResult::Filter F = Previous.makeFilter();
5379 while (F.hasNext()) {
5380 NamedDecl *D = F.next()->getUnderlyingDecl();
5381 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005382 !FDLookupContext->InEnclosingNamespaceSetOf(
5383 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005384 F.erase();
5385 }
5386 F.done();
5387
5388 // Should this be diagnosed here?
5389 if (Previous.empty()) return true;
5390
5391 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5392 ExplicitTemplateArgs);
5393 return false;
5394}
5395
Abramo Bagnarae03db982010-05-20 15:32:11 +00005396/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005397/// specialization.
5398///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005399/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005400/// explicit function template specialization. On successful completion,
5401/// the function declaration \p FD will become a function template
5402/// specialization.
5403///
5404/// \param FD the function declaration, which will be updated to become a
5405/// function template specialization.
5406///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005407/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5408/// if any. Note that this may be valid info even when 0 arguments are
5409/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5410/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005411///
Francois Pichet59e7c562011-07-08 06:21:47 +00005412/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005413/// this function specialization.
5414bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005415Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005416 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005417 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005418 // The set of function template specializations that could match this
5419 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005420 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005421
Sebastian Redl7a126a42010-08-31 00:36:30 +00005422 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005423 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5424 I != E; ++I) {
5425 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5426 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005427 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005428 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005429 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5430 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005431 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005432
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005433 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005434 // A trailing template-argument can be left unspecified in the
5435 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005436 // provided it can be deduced from the function argument type.
5437 // Perform template argument deduction to determine whether we may be
5438 // specializing this template.
5439 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005440 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005441 FunctionDecl *Specialization = 0;
5442 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005443 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005444 FD->getType(),
5445 Specialization,
5446 Info)) {
5447 // FIXME: Template argument deduction failed; record why it failed, so
5448 // that we can provide nifty diagnostics.
5449 (void)TDK;
5450 continue;
5451 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005452
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005453 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005454 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005455 }
5456 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005457
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005458 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005459 UnresolvedSetIterator Result
5460 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005461 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005462 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005463 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005464 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005465 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005466 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005467 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005468 return true;
John McCallc373d482010-01-27 01:50:18 +00005469
5470 // Ignore access information; it doesn't figure into redeclaration checking.
5471 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005472
5473 FunctionTemplateSpecializationInfo *SpecInfo
5474 = Specialization->getTemplateSpecializationInfo();
5475 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005476
5477 // Note: do not overwrite location info if previous template
5478 // specialization kind was explicit.
5479 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5480 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5481 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005482
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005483 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005484 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005485
5486 // If this is a friend declaration, then we're not really declaring
5487 // an explicit specialization.
5488 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005489
Douglas Gregord5cb8762009-10-07 00:13:32 +00005490 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005491 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005493 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005494 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005495 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005496 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005497
5498 // C++ [temp.expl.spec]p6:
5499 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005500 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005501 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005502 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005503 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005504 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005505 if (!isFriend &&
5506 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005507 TSK_ExplicitSpecialization,
5508 Specialization,
5509 SpecInfo->getTemplateSpecializationKind(),
5510 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005511 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005512 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005513
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005514 // Mark the prior declaration as an explicit specialization, so that later
5515 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005516 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005517 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005518 MarkUnusedFileScopedDecl(Specialization);
5519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005520
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005521 // Turn the given function declaration into a function template
5522 // specialization, with the template arguments from the previous
5523 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005524 // Take copies of (semantic and syntactic) template argument lists.
5525 const TemplateArgumentList* TemplArgs = new (Context)
5526 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005527 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005528 TemplArgs, /*InsertPos=*/0,
5529 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005530 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005531 FD->setStorageClass(Specialization->getStorageClass());
5532
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005533 // The "previous declaration" for this function template specialization is
5534 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005535 Previous.clear();
5536 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005537 return false;
5538}
5539
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005540/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005541/// specialization.
5542///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005544/// explicit member function specialization. On successful completion,
5545/// the function declaration \p FD will become a member function
5546/// specialization.
5547///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005548/// \param Member the member declaration, which will be updated to become a
5549/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005550///
John McCall68263142009-11-18 22:49:29 +00005551/// \param Previous the set of declarations, one of which may be specialized
5552/// by this function specialization; the set will be modified to contain the
5553/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005554bool
John McCall68263142009-11-18 22:49:29 +00005555Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005556 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005557
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005558 // Try to find the member we are instantiating.
5559 NamedDecl *Instantiation = 0;
5560 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005561 MemberSpecializationInfo *MSInfo = 0;
5562
John McCall68263142009-11-18 22:49:29 +00005563 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005564 // Nowhere to look anyway.
5565 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005566 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5567 I != E; ++I) {
5568 NamedDecl *D = (*I)->getUnderlyingDecl();
5569 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005570 if (Context.hasSameType(Function->getType(), Method->getType())) {
5571 Instantiation = Method;
5572 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005573 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005574 break;
5575 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005576 }
5577 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005578 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005579 VarDecl *PrevVar;
5580 if (Previous.isSingleResult() &&
5581 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005582 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005583 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005584 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005585 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005586 }
5587 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005588 CXXRecordDecl *PrevRecord;
5589 if (Previous.isSingleResult() &&
5590 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5591 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005592 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005593 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005594 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005596
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005597 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005598 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005599 // specializations are always out-of-line, the caller will complain about
5600 // this mismatch later.
5601 return false;
5602 }
John McCall77e8b112010-04-13 20:37:33 +00005603
5604 // If this is a friend, just bail out here before we start turning
5605 // things into explicit specializations.
5606 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5607 // Preserve instantiation information.
5608 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5609 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5610 cast<CXXMethodDecl>(InstantiatedFrom),
5611 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5612 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5613 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5614 cast<CXXRecordDecl>(InstantiatedFrom),
5615 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5616 }
5617
5618 Previous.clear();
5619 Previous.addDecl(Instantiation);
5620 return false;
5621 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005622
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005623 // Make sure that this is a specialization of a member.
5624 if (!InstantiatedFrom) {
5625 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5626 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005627 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5628 return true;
5629 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005630
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005631 // C++ [temp.expl.spec]p6:
5632 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005633 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005634 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005635 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005636 // use occurs; no diagnostic is required.
5637 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005638
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005639 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005640 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5641 TSK_ExplicitSpecialization,
5642 Instantiation,
5643 MSInfo->getTemplateSpecializationKind(),
5644 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005645 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005646 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005647
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005648 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005649 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005650 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005651 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005652 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005653 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005654
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005655 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005656 // the original declaration to note that it is an explicit specialization
5657 // (if it was previously an implicit instantiation). This latter step
5658 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005659 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005660 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5661 if (InstantiationFunction->getTemplateSpecializationKind() ==
5662 TSK_ImplicitInstantiation) {
5663 InstantiationFunction->setTemplateSpecializationKind(
5664 TSK_ExplicitSpecialization);
5665 InstantiationFunction->setLocation(Member->getLocation());
5666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005668 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5669 cast<CXXMethodDecl>(InstantiatedFrom),
5670 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005671 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005673 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5674 if (InstantiationVar->getTemplateSpecializationKind() ==
5675 TSK_ImplicitInstantiation) {
5676 InstantiationVar->setTemplateSpecializationKind(
5677 TSK_ExplicitSpecialization);
5678 InstantiationVar->setLocation(Member->getLocation());
5679 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005680
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005681 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5682 cast<VarDecl>(InstantiatedFrom),
5683 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005684 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005685 } else {
5686 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005687 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5688 if (InstantiationClass->getTemplateSpecializationKind() ==
5689 TSK_ImplicitInstantiation) {
5690 InstantiationClass->setTemplateSpecializationKind(
5691 TSK_ExplicitSpecialization);
5692 InstantiationClass->setLocation(Member->getLocation());
5693 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005694
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005695 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005696 cast<CXXRecordDecl>(InstantiatedFrom),
5697 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005700 // Save the caller the trouble of having to figure out which declaration
5701 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005702 Previous.clear();
5703 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005704 return false;
5705}
5706
Douglas Gregor558c0322009-10-14 23:41:34 +00005707/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005708///
5709/// \returns true if a serious error occurs, false otherwise.
5710static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005711 SourceLocation InstLoc,
5712 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005713 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5714 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715
Douglas Gregor669eed82010-07-13 00:10:04 +00005716 if (CurContext->isRecord()) {
5717 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5718 << D;
5719 return true;
5720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor558c0322009-10-14 23:41:34 +00005722 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005724 // template.
5725 //
5726 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005727 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005728 !CurContext->Encloses(OrigContext)) {
5729 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730 S.Diag(InstLoc,
5731 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005732 diag::err_explicit_instantiation_out_of_scope
5733 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005734 << D << NS;
5735 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005736 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005737 S.getLangOptions().CPlusPlus0x?
5738 diag::err_explicit_instantiation_must_be_global
5739 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005740 << D;
5741 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005742 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005743 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005744
Douglas Gregor558c0322009-10-14 23:41:34 +00005745 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746 // If the name declared in the explicit instantiation is an unqualified
5747 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005748 // its template is declared or, if that namespace is inline (7.3.1), any
5749 // namespace from its enclosing namespace set.
5750 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005751 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005752
5753 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005754 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005755
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005756 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005757 S.getLangOptions().CPlusPlus0x?
5758 diag::err_explicit_instantiation_unqualified_wrong_namespace
5759 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005760 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005761 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005762 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005763}
5764
5765/// \brief Determine whether the given scope specifier has a template-id in it.
5766static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5767 if (!SS.isSet())
5768 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769
Douglas Gregor558c0322009-10-14 23:41:34 +00005770 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005772 // or a static data member of a class template specialization, the name of
5773 // the class template specialization in the qualified-id for the member
5774 // name shall be a simple-template-id.
5775 //
5776 // C++98 has the same restriction, just worded differently.
5777 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5778 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005779 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005780 if (isa<TemplateSpecializationType>(T))
5781 return true;
5782
5783 return false;
5784}
5785
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005786// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005787DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005788Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005789 SourceLocation ExternLoc,
5790 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005791 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005792 SourceLocation KWLoc,
5793 const CXXScopeSpec &SS,
5794 TemplateTy TemplateD,
5795 SourceLocation TemplateNameLoc,
5796 SourceLocation LAngleLoc,
5797 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005798 SourceLocation RAngleLoc,
5799 AttributeList *Attr) {
5800 // Find the class template we're specializing
5801 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005802 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005803 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5804
5805 // Check that the specialization uses the same tag kind as the
5806 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005807 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5808 assert(Kind != TTK_Enum &&
5809 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005810 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005811 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005812 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005813 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005814 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005815 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005816 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005817 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005818 diag::note_previous_use);
5819 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5820 }
5821
Douglas Gregor558c0322009-10-14 23:41:34 +00005822 // C++0x [temp.explicit]p2:
5823 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005824 // definition and an explicit instantiation declaration. An explicit
5825 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005826 TemplateSpecializationKind TSK
5827 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5828 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005829
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005830 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005831 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005832 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005833
5834 // Check that the template argument list is well-formed for this
5835 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005836 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005837 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5838 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005839 return true;
5840
Douglas Gregor910f8002010-11-07 23:05:16 +00005841 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005842 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005843
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005844 // Find the class template specialization declaration that
5845 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005846 void *InsertPos = 0;
5847 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005848 = ClassTemplate->findSpecialization(Converted.data(),
5849 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005850
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005851 TemplateSpecializationKind PrevDecl_TSK
5852 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5853
Douglas Gregord5cb8762009-10-07 00:13:32 +00005854 // C++0x [temp.explicit]p2:
5855 // [...] An explicit instantiation shall appear in an enclosing
5856 // namespace of its template. [...]
5857 //
5858 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005859 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5860 SS.isSet()))
5861 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005863 ClassTemplateSpecializationDecl *Specialization = 0;
5864
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005865 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005866 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005867 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005868 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005869 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005870 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005871 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005872
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005873 // Even though HasNoEffect == true means that this explicit instantiation
5874 // has no effect on semantics, we go on to put its syntax in the AST.
5875
5876 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5877 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005878 // Since the only prior class template specialization with these
5879 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005880 // declaration node as our own, updating the source location
5881 // for the template name to reflect our new declaration.
5882 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005883 Specialization = PrevDecl;
5884 Specialization->setLocation(TemplateNameLoc);
5885 PrevDecl = 0;
5886 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005887 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005888
Douglas Gregor52604ab2009-09-11 21:19:12 +00005889 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005890 // Create a new class template specialization declaration node for
5891 // this explicit specialization.
5892 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005893 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005894 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005895 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005896 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005897 Converted.data(),
5898 Converted.size(),
5899 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005900 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005901
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005902 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005903 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005904 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005905 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005906 }
5907
5908 // Build the fully-sugared type for this explicit instantiation as
5909 // the user wrote in the explicit instantiation itself. This means
5910 // that we'll pretty-print the type retrieved from the
5911 // specialization's declaration the way that the user actually wrote
5912 // the explicit instantiation, rather than formatting the name based
5913 // on the "canonical" representation used to store the template
5914 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005915 TypeSourceInfo *WrittenTy
5916 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5917 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005918 Context.getTypeDeclType(Specialization));
5919 Specialization->setTypeAsWritten(WrittenTy);
5920 TemplateArgsIn.release();
5921
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005922 // Set source locations for keywords.
5923 Specialization->setExternLoc(ExternLoc);
5924 Specialization->setTemplateKeywordLoc(TemplateLoc);
5925
5926 // Add the explicit instantiation into its lexical context. However,
5927 // since explicit instantiations are never found by name lookup, we
5928 // just put it into the declaration context directly.
5929 Specialization->setLexicalDeclContext(CurContext);
5930 CurContext->addDecl(Specialization);
5931
5932 // Syntax is now OK, so return if it has no other effect on semantics.
5933 if (HasNoEffect) {
5934 // Set the template specialization kind.
5935 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005936 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005937 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005938
5939 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005940 // A definition of a class template or class member template
5941 // shall be in scope at the point of the explicit instantiation of
5942 // the class template or class member template.
5943 //
5944 // This check comes when we actually try to perform the
5945 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005946 ClassTemplateSpecializationDecl *Def
5947 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005948 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005949 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005950 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005951 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005952 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005953 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5954 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005955
Douglas Gregor0d035142009-10-27 18:42:08 +00005956 // Instantiate the members of this class template specialization.
5957 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005958 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005959 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005960 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5961
5962 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5963 // TSK_ExplicitInstantiationDefinition
5964 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5965 TSK == TSK_ExplicitInstantiationDefinition)
5966 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005967
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005968 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005969 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005970
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005971 // Set the template specialization kind.
5972 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005973 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005974}
5975
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005976// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005977DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005978Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005979 SourceLocation ExternLoc,
5980 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005981 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005982 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005983 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005984 IdentifierInfo *Name,
5985 SourceLocation NameLoc,
5986 AttributeList *Attr) {
5987
Douglas Gregor402abb52009-05-28 23:31:59 +00005988 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005989 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005990 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005991 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00005992 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00005993 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005994 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005995 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005996 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5997
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005998 if (!TagD)
5999 return true;
6000
John McCalld226f652010-08-21 09:40:31 +00006001 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006002 if (Tag->isEnum()) {
6003 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6004 << Context.getTypeDeclType(Tag);
6005 return true;
6006 }
6007
Douglas Gregord0c87372009-05-27 17:30:49 +00006008 if (Tag->isInvalidDecl())
6009 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006011 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6012 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6013 if (!Pattern) {
6014 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6015 << Context.getTypeDeclType(Record);
6016 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6017 return true;
6018 }
6019
Douglas Gregor558c0322009-10-14 23:41:34 +00006020 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006021 // If the explicit instantiation is for a class or member class, the
6022 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006023 // simple-template-id.
6024 //
6025 // C++98 has the same restriction, just worded differently.
6026 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006027 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006028 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029
Douglas Gregor558c0322009-10-14 23:41:34 +00006030 // C++0x [temp.explicit]p2:
6031 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006032 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006033 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006034 TemplateSpecializationKind TSK
6035 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6036 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006037
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006038 // C++0x [temp.explicit]p2:
6039 // [...] An explicit instantiation shall appear in an enclosing
6040 // namespace of its template. [...]
6041 //
6042 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006043 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006044
Douglas Gregor454885e2009-10-15 15:54:05 +00006045 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006047 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006048 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006049 PrevDecl = Record;
6050 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006051 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006052 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006053 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006054 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006055 PrevDecl,
6056 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006057 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006058 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006059 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006060 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006061 return TagD;
6062 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006063
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006064 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006065 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006066 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006067 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006068 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006069 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006070 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006071 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006072 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006073 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6074 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006075 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6076 << Pattern;
6077 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006078 } else {
6079 if (InstantiateClass(NameLoc, Record, Def,
6080 getTemplateInstantiationArgs(Record),
6081 TSK))
6082 return true;
6083
Douglas Gregor952b0172010-02-11 01:04:33 +00006084 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006085 if (!RecordDef)
6086 return true;
6087 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006088 }
6089
Douglas Gregor0d035142009-10-27 18:42:08 +00006090 // Instantiate all of the members of the class.
6091 InstantiateClassMembers(NameLoc, RecordDef,
6092 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006093
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006094 if (TSK == TSK_ExplicitInstantiationDefinition)
6095 MarkVTableUsed(NameLoc, RecordDef, true);
6096
Mike Stump390b4cc2009-05-16 07:39:55 +00006097 // FIXME: We don't have any representation for explicit instantiations of
6098 // member classes. Such a representation is not needed for compilation, but it
6099 // should be available for clients that want to see all of the declarations in
6100 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006101 return TagD;
6102}
6103
John McCallf312b1e2010-08-26 23:41:50 +00006104DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6105 SourceLocation ExternLoc,
6106 SourceLocation TemplateLoc,
6107 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006108 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006109 // TODO: check if/when DNInfo should replace Name.
6110 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6111 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006112 if (!Name) {
6113 if (!D.isInvalidType())
6114 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6115 diag::err_explicit_instantiation_requires_name)
6116 << D.getDeclSpec().getSourceRange()
6117 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006118
Douglas Gregord5a423b2009-09-25 18:43:00 +00006119 return true;
6120 }
6121
6122 // The scope passed in may not be a decl scope. Zip up the scope tree until
6123 // we find one that is.
6124 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6125 (S->getFlags() & Scope::TemplateParamScope) != 0)
6126 S = S->getParent();
6127
6128 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006129 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6130 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006131 if (R.isNull())
6132 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006133
Douglas Gregore885e182011-05-21 18:53:30 +00006134 // C++ [dcl.stc]p1:
6135 // A storage-class-specifier shall not be specified in [...] an explicit
6136 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006137 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006138 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6139 << Name;
6140 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006141 } else if (D.getDeclSpec().getStorageClassSpec()
6142 != DeclSpec::SCS_unspecified) {
6143 // Complain about then remove the storage class specifier.
6144 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6145 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6146
6147 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006148 }
6149
Douglas Gregor663b5a02009-10-14 20:14:33 +00006150 // C++0x [temp.explicit]p1:
6151 // [...] An explicit instantiation of a function template shall not use the
6152 // inline or constexpr specifiers.
6153 // Presumably, this also applies to member functions of class templates as
6154 // well.
6155 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006156 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006157 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006158 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006159
Douglas Gregor663b5a02009-10-14 20:14:33 +00006160 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006161
Douglas Gregor558c0322009-10-14 23:41:34 +00006162 // C++0x [temp.explicit]p2:
6163 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006164 // definition and an explicit instantiation declaration. An explicit
6165 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006166 TemplateSpecializationKind TSK
6167 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6168 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006169
Abramo Bagnara25777432010-08-11 22:01:17 +00006170 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006171 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006172
6173 if (!R->isFunctionType()) {
6174 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006175 // A [...] static data member of a class template can be explicitly
6176 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006177 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006178 if (Previous.isAmbiguous())
6179 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006180
John McCall1bcee0a2009-12-02 08:25:40 +00006181 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006182 if (!Prev || !Prev->isStaticDataMember()) {
6183 // We expect to see a data data member here.
6184 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6185 << Name;
6186 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6187 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006188 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006189 return true;
6190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006191
Douglas Gregord5a423b2009-09-25 18:43:00 +00006192 if (!Prev->getInstantiatedFromStaticDataMember()) {
6193 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006194 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006195 diag::err_explicit_instantiation_data_member_not_instantiated)
6196 << Prev;
6197 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6198 // FIXME: Can we provide a note showing where this was declared?
6199 return true;
6200 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201
Douglas Gregor558c0322009-10-14 23:41:34 +00006202 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006203 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006204 // or a static data member of a class template specialization, the name of
6205 // the class template specialization in the qualified-id for the member
6206 // name shall be a simple-template-id.
6207 //
6208 // C++98 has the same restriction, just worded differently.
6209 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006210 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006211 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006212 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006213
Douglas Gregor558c0322009-10-14 23:41:34 +00006214 // Check the scope of this explicit instantiation.
6215 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216
Douglas Gregor454885e2009-10-15 15:54:05 +00006217 // Verify that it is okay to explicitly instantiate here.
6218 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6219 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006220 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006221 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006222 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006223 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006224 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006225 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006226 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006227 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006228
Douglas Gregord5a423b2009-09-25 18:43:00 +00006229 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006230 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006231 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006232 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006233
Douglas Gregord5a423b2009-09-25 18:43:00 +00006234 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006235 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006237
6238 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006239 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006240 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006241 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006242 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6243 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006244 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6245 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006246 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6247 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006248 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006249 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006250 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006251 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006252 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253
Douglas Gregord5a423b2009-09-25 18:43:00 +00006254 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006255 // A [...] function [...] can be explicitly instantiated from its template.
6256 // A member function [...] of a class template can be explicitly
6257 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006258 // template.
John McCallc373d482010-01-27 01:50:18 +00006259 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006260 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6261 P != PEnd; ++P) {
6262 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006263 if (!HasExplicitTemplateArgs) {
6264 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6265 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6266 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006267
John McCallc373d482010-01-27 01:50:18 +00006268 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006269 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6270 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006271 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006272 }
6273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006274
Douglas Gregord5a423b2009-09-25 18:43:00 +00006275 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6276 if (!FunTmpl)
6277 continue;
6278
John McCall5769d612010-02-08 23:07:23 +00006279 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006280 FunctionDecl *Specialization = 0;
6281 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006282 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006283 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006284 R, Specialization, Info)) {
6285 // FIXME: Keep track of almost-matches?
6286 (void)TDK;
6287 continue;
6288 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006289
John McCallc373d482010-01-27 01:50:18 +00006290 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006291 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006292
Douglas Gregord5a423b2009-09-25 18:43:00 +00006293 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006294 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006295 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006296 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006297 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6298 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6299 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006300
John McCallc373d482010-01-27 01:50:18 +00006301 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006302 return true;
John McCallc373d482010-01-27 01:50:18 +00006303
6304 // Ignore access control bits, we don't need them for redeclaration checking.
6305 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006306
Douglas Gregor0a897e32009-10-15 17:21:20 +00006307 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006308 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006309 diag::err_explicit_instantiation_member_function_not_instantiated)
6310 << Specialization
6311 << (Specialization->getTemplateSpecializationKind() ==
6312 TSK_ExplicitSpecialization);
6313 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6314 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006315 }
6316
Douglas Gregor0a897e32009-10-15 17:21:20 +00006317 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006318 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6319 PrevDecl = Specialization;
6320
Douglas Gregor0a897e32009-10-15 17:21:20 +00006321 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006322 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006323 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006324 PrevDecl,
6325 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006326 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006327 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006328 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329
Douglas Gregor0a897e32009-10-15 17:21:20 +00006330 // FIXME: We may still want to build some representation of this
6331 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006332 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006333 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006334 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006335
6336 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337
Douglas Gregor0a897e32009-10-15 17:21:20 +00006338 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006339 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006340
Douglas Gregor558c0322009-10-14 23:41:34 +00006341 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006342 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006343 // or a static data member of a class template specialization, the name of
6344 // the class template specialization in the qualified-id for the member
6345 // name shall be a simple-template-id.
6346 //
6347 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006348 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006349 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006350 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006351 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006352 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006353 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006354 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006355
Douglas Gregor558c0322009-10-14 23:41:34 +00006356 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006357 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006358 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006359 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006360 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006361
Douglas Gregord5a423b2009-09-25 18:43:00 +00006362 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006363 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006364}
6365
John McCallf312b1e2010-08-26 23:41:50 +00006366TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006367Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6368 const CXXScopeSpec &SS, IdentifierInfo *Name,
6369 SourceLocation TagLoc, SourceLocation NameLoc) {
6370 // This has to hold, because SS is expected to be defined.
6371 assert(Name && "Expected a name in a dependent tag");
6372
6373 NestedNameSpecifier *NNS
6374 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6375 if (!NNS)
6376 return true;
6377
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006378 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006379
Douglas Gregor48c89f42010-04-24 16:38:41 +00006380 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6381 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006382 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006383 return true;
6384 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006385
Douglas Gregor059101f2011-03-02 00:47:37 +00006386 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006387 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006388 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6389
6390 // Create type-source location information for this type.
6391 TypeLocBuilder TLB;
6392 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6393 TL.setKeywordLoc(TagLoc);
6394 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6395 TL.setNameLoc(NameLoc);
6396 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006397}
6398
John McCallf312b1e2010-08-26 23:41:50 +00006399TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6401 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006402 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006403 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006404 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006405
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006406 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6407 !getLangOptions().CPlusPlus0x)
6408 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006409 << FixItHint::CreateRemoval(TypenameLoc);
6410
Douglas Gregor2494dd02011-03-01 01:34:45 +00006411 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006412 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6413 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006414 if (T.isNull())
6415 return true;
John McCall63b43852010-04-29 23:50:39 +00006416
6417 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6418 if (isa<DependentNameType>(T)) {
6419 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006420 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006421 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006422 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006423 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006424 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006425 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006426 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006427 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006429
John McCallb3d87482010-08-24 05:47:05 +00006430 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006431}
6432
John McCallf312b1e2010-08-26 23:41:50 +00006433TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006434Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6435 const CXXScopeSpec &SS,
6436 SourceLocation TemplateLoc,
6437 TemplateTy TemplateIn,
6438 SourceLocation TemplateNameLoc,
6439 SourceLocation LAngleLoc,
6440 ASTTemplateArgsPtr TemplateArgsIn,
6441 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006442 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6443 !getLangOptions().CPlusPlus0x)
6444 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006445 << FixItHint::CreateRemoval(TypenameLoc);
6446
6447 // Translate the parser's template argument list in our AST format.
6448 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6449 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6450
6451 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006452 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6453 // Construct a dependent template specialization type.
6454 assert(DTN && "dependent template has non-dependent name?");
6455 assert(DTN->getQualifier()
6456 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6457 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6458 DTN->getQualifier(),
6459 DTN->getIdentifier(),
6460 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006461
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006462 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006463 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006464 DependentTemplateSpecializationTypeLoc SpecTL
6465 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006466 SpecTL.setLAngleLoc(LAngleLoc);
6467 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006468 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006469 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006470 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006471 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6472 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006473 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006474 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006475
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006476 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6477 if (T.isNull())
6478 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006479
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006480 // Provide source-location information for the template specialization
6481 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006482 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006483 TemplateSpecializationTypeLoc SpecTL
6484 = Builder.push<TemplateSpecializationTypeLoc>(T);
6485
6486 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006487 SpecTL.setLAngleLoc(LAngleLoc);
6488 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006489 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006490 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6491 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6492
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006493 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6494 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6495 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006496 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6497
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006498 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6499 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006500}
6501
Douglas Gregora02411e2011-02-27 22:46:49 +00006502
Douglas Gregord57959a2009-03-27 23:10:48 +00006503/// \brief Build the type that describes a C++ typename specifier,
6504/// e.g., "typename T::type".
6505QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006506Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6507 SourceLocation KeywordLoc,
6508 NestedNameSpecifierLoc QualifierLoc,
6509 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006510 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006511 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006512 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006513
John McCall77bb1aa2010-05-01 00:40:08 +00006514 DeclContext *Ctx = computeDeclContext(SS);
6515 if (!Ctx) {
6516 // If the nested-name-specifier is dependent and couldn't be
6517 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006518 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6519 return Context.getDependentNameType(Keyword,
6520 QualifierLoc.getNestedNameSpecifier(),
6521 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006522 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006523
John McCall77bb1aa2010-05-01 00:40:08 +00006524 // If the nested-name-specifier refers to the current instantiation,
6525 // the "typename" keyword itself is superfluous. In C++03, the
6526 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6527 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006528 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006529
John McCall77bb1aa2010-05-01 00:40:08 +00006530 if (RequireCompleteDeclContext(SS, Ctx))
6531 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006532
6533 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006534 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006535 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006536 unsigned DiagID = 0;
6537 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006538 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006539 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006540 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006541 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006542
6543 case LookupResult::FoundUnresolvedValue: {
6544 // We found a using declaration that is a value. Most likely, the using
6545 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006546 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006547 IILoc);
6548 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6549 << Name << Ctx << FullRange;
6550 if (UnresolvedUsingValueDecl *Using
6551 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006552 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006553 Diag(Loc, diag::note_using_value_decl_missing_typename)
6554 << FixItHint::CreateInsertion(Loc, "typename ");
6555 }
6556 }
6557 // Fall through to create a dependent typename type, from which we can recover
6558 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006559
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006560 case LookupResult::NotFoundInCurrentInstantiation:
6561 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006562 return Context.getDependentNameType(Keyword,
6563 QualifierLoc.getNestedNameSpecifier(),
6564 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006565
6566 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006567 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006568 // We found a type. Build an ElaboratedType, since the
6569 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006570 return Context.getElaboratedType(ETK_Typename,
6571 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006572 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006573 }
6574
6575 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006576 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006577 break;
6578
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006579
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006580 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006581 return QualType();
6582
Douglas Gregord57959a2009-03-27 23:10:48 +00006583 case LookupResult::FoundOverloaded:
6584 DiagID = diag::err_typename_nested_not_type;
6585 Referenced = *Result.begin();
6586 break;
6587
John McCall6e247262009-10-10 05:48:19 +00006588 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006589 return QualType();
6590 }
6591
6592 // If we get here, it's because name lookup did not find a
6593 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006594 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006595 IILoc);
6596 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006597 if (Referenced)
6598 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6599 << Name;
6600 return QualType();
6601}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006602
6603namespace {
6604 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006605 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006606 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006607 SourceLocation Loc;
6608 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006609
Douglas Gregor4a959d82009-08-06 16:20:37 +00006610 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006611 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006612
Mike Stump1eb44332009-09-09 15:08:12 +00006613 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006614 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006615 DeclarationName Entity)
6616 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006617 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006618
6619 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006620 /// transformed.
6621 ///
6622 /// For the purposes of type reconstruction, a type has already been
6623 /// transformed if it is NULL or if it is not dependent.
6624 bool AlreadyTransformed(QualType T) {
6625 return T.isNull() || !T->isDependentType();
6626 }
Mike Stump1eb44332009-09-09 15:08:12 +00006627
6628 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006629 /// rebuilt.
6630 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006631
Douglas Gregor4a959d82009-08-06 16:20:37 +00006632 /// \brief Returns the name of the entity whose type is being rebuilt.
6633 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006634
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006635 /// \brief Sets the "base" location and entity when that
6636 /// information is known based on another transformation.
6637 void setBase(SourceLocation Loc, DeclarationName Entity) {
6638 this->Loc = Loc;
6639 this->Entity = Entity;
6640 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006641 };
6642}
6643
Douglas Gregor4a959d82009-08-06 16:20:37 +00006644/// \brief Rebuilds a type within the context of the current instantiation.
6645///
Mike Stump1eb44332009-09-09 15:08:12 +00006646/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006647/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006648/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006649/// partial specialization thereof). This routine will rebuild that type now
6650/// that we have entered the declarator's scope, which may produce different
6651/// canonical types, e.g.,
6652///
6653/// \code
6654/// template<typename T>
6655/// struct X {
6656/// typedef T* pointer;
6657/// pointer data();
6658/// };
6659///
6660/// template<typename T>
6661/// typename X<T>::pointer X<T>::data() { ... }
6662/// \endcode
6663///
Douglas Gregor4714c122010-03-31 17:34:00 +00006664/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006665/// since we do not know that we can look into X<T> when we parsed the type.
6666/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006667/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006668/// as the canonical type of T*, allowing the return types of the out-of-line
6669/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006670TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6671 SourceLocation Loc,
6672 DeclarationName Name) {
6673 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006674 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006675
Douglas Gregor4a959d82009-08-06 16:20:37 +00006676 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6677 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006678}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006679
John McCall60d7b3a2010-08-24 06:29:42 +00006680ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006681 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6682 DeclarationName());
6683 return Rebuilder.TransformExpr(E);
6684}
6685
John McCall63b43852010-04-29 23:50:39 +00006686bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006687 if (SS.isInvalid())
6688 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006689
Douglas Gregor7e384942011-02-25 16:07:42 +00006690 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006691 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6692 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006693 NestedNameSpecifierLoc Rebuilt
6694 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6695 if (!Rebuilt)
6696 return true;
John McCall63b43852010-04-29 23:50:39 +00006697
Douglas Gregor7e384942011-02-25 16:07:42 +00006698 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006699 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006700}
6701
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006702/// \brief Produces a formatted string that describes the binding of
6703/// template parameters to template arguments.
6704std::string
6705Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6706 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006707 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006708}
6709
6710std::string
6711Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6712 const TemplateArgument *Args,
6713 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006714 llvm::SmallString<128> Str;
6715 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006716
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006717 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006718 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006719
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006720 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006721 if (I >= NumArgs)
6722 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006723
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006724 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006725 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006726 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006727 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006728
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006729 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006730 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006731 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006732 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006734
Douglas Gregor87dd6972010-12-20 16:52:59 +00006735 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006736 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006737 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006738
6739 Out << ']';
6740 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006741}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006742
6743void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6744 if (!FD)
6745 return;
6746 FD->setLateTemplateParsed(Flag);
6747}
6748
6749bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6750 DeclContext *DC = CurContext;
6751
6752 while (DC) {
6753 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6754 const FunctionDecl *FD = RD->isLocalClass();
6755 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6756 } else if (DC->isTranslationUnit() || DC->isNamespace())
6757 return false;
6758
6759 DC = DC->getParent();
6760 }
6761 return false;
6762}